- --[[ Буняк А.Х., ©BAX
- Макрос, переводящий редактор в "сжатый" режим
- (аналогично плагину Condenced View). Показывает только те строки.
- у которых первый непробельный символ находится на позиции курсора
- или левее.
- --]]
- -- +
- -- Константы
- -- -
- local F = far.Flags
- local color={Flags=bit64.bor(F.FCF_FG_4BIT,F.FCF_BG_4BIT),ForegroundColor=0,BackgroundColor=0xFF0003}
- -- +
- -- Настройки
- local function Settings()
- return{
- OnKey="AltF4",
- OffKey="F10 Esc",
- GoKey="Enter",
- SetupKey='AltF4'
- }
- end
- local function MergeSettings(Old, New)
- if New then
- for i,v in pairs(New) do
- Old[i]=v
- end
- end
- return Old
- end
- local CfgName=(...):match("(.*)%.lua")..'.cfg'
- local CfgProc=loadfile(CfgName)
- local S=MergeSettings( Settings(), CfgProc and CfgProc() or nil)
- -- -
- -- Список параметров характеристик редакторов,
- -- у которых включён Condensed режим
- local EditorsCondView={}
- -- +
- -- Проверка, активен ли макрос (редактор в сжатом виде).
- -- -
- local function IsMacroActive()
- local ei=editor.GetInfo()
- if not ei then return end;
- return EditorsCondView[ei.EditorID]
- end;
- --
- local OnProcess = {}
- --
- -- +
- local function GoToLinePos(EID, ln, cl, tsl, lpc)
- editor.SetPosition(EID, ln, -1, cl, tsl , lpc)
- editor.Redraw(EID)
- end
- -- -
- -- +
- -- Установка режима R/O
- -- -
- local function SetReadOnly(EID, abRO) -- abRO - True или False
- editor.SetParam(EID, F.ESPT_LOCKMODE, abRO)
- end -- SetReadOnly
- local function ProcessEditorInput()
- local ei=editor.GetInfo()
- if not ei then return end
- local EID=ei.EditorID
- local jj=ei.CurLine;
- local ii
- if not EditorsCondView[EID] then
- return
- end;
- if EditorsCondView[EID].Ends then
- return
- end;
- if EditorsCondView[EID].LastColored then
- if EditorsCondView[EID].LastColored == jj then
- return;
- end;
- editor.DelColor(EID,EditorsCondView[EID].LastColored, nil)
- EditorsCondView[EID].LastColored = nil
- end;
- ii = EditorsCondView[EID].Lines[jj]
- EditorsCondView[EID].LastColored = jj
- editor.SetTitle(EID, string.gsub(editor.GetFileName(EID),".*\\(.*)","%1")..string.char(17)..ii)
- editor.AddColor(EID, jj, 1, ei.WindowSizeX, 0, color, 200)
- editor.Redraw(EID)
- end; -- ProcessEditorInput
- -- +
- -- Включить сжатый режим
- -- -
- local function CondViewOn(ei, EID)
- local x0,y0,yTopl, xLeftC = ei.CurTabPos,ei.CurLine,ei.TopScreenLine, ei.LeftPos
- local need
- local bReadOnly = band(ei.CurState,F.ECSTATE_LOCKED)~=0
- local bProcessed=false
- if OnProcess[EID] or EditorsCondView[EID] then return false end -- редактор занят или уже овраплен? уйдём
- OnProcess[EID] = true -- запомним, что в работе
- EditorsCondView[EID] =
- {Id=EID, ColPos=x0,LinePos=y0, TopScreenLine=yTopl, LeftPos=xLeftC,
- processed=false, ReadOnly0=bReadOnly,
- SelectInfo=editor.GetSelection(EID), Lines={},
- Bookmarks=editor.GetBookmarks(EID)
- }
- SetReadOnly(EID, false)
- editor.UndoRedo(EID,F.EUR_BEGIN) -- начнём блок отмены
- -- Удаление строк {
- local y1=y0
- -- Заполнение таблицы исходных номеров строк
- for i=1,ei.TotalLines do
- EditorsCondView[EID].Lines[i]=i
- end
- for i=ei.TotalLines,1,-1 do
- editor.ExpandTabs(EID, i)
- local sss = editor.GetString(EID,i,1)
- if string.match(sss.StringText, "^%s*$") then
- need=i<ei.TotalLines
- else
- need=true
- for j =1,x0 do
- if string.byte(sss.StringText, j)~=32 then
- need=false
- break
- end
- end
- end
- if need then
- editor.DeleteString(EID, i)
- table.remove(EditorsCondView[EID].Lines, i)
- bProcessed = true
- if i < y1 then
- y1 = y1 - 1
- end
- end
- end -- for i=ei.TotalLines,1,-1
- if bProcessed then
- editor.Redraw(EID)
- EditorsCondView[EID].processed = true
- GoToLinePos(EID, y1, x0, EditorsCondView[EID].TopScreenLine, EditorsCondView[EID].LeftPos)
- end
- -- Удаление строк }
- editor.UndoRedo(EID,F.EUR_END) -- закроем блок отмены
- SetReadOnly(EID, true)
- editor.SetTitle(EID, editor.GetFileName(EID).." Condenced")
- OnProcess[EID] = nil -- процесс обработки окончен
- ei = editor.GetInfo(EID)
- if ei then
- ProcessEditorInput();
- end
- return true
- end -- CondViewOn(ei, EID)
- -- +
- -- Выключить сжатый режим
- -- -
- local function CondViewOff(ei, EID, bNewPos)
- --local ei=editor.GetInfo()
- --if not ei then return end
- --local EID=ei.EditorID
- local lnp,clp, tsl, lpc
- local ScrPos=ei.CurLine-ei.TopScreenLine
- if EditorsCondView[EID].LastColored then
- editor.DelColor(EID,EditorsCondView[EID].LastColored, nil)
- editor.Redraw(EID)
- EditorsCondView[EID].LastColored = nil
- end;
- EditorsCondView[EID].Ends = true;
- if bNewPos and ei then
- -- Заношу новый номер строки в EditorsCondView[EID]
- EditorsCondView[EID].LinePos = EditorsCondView[EID].Lines[ei.CurLine]
- end
- if OnProcess[EID] or not EditorsCondView[EID] then -- редактор занят или не овраплен? уйдём
- return false
- end
- OnProcess[EID] = true -- запомним, что в работе
- if EditorsCondView[EID].processed then
- SetReadOnly(EID, false)
- editor.UndoRedo(EID, F.EUR_UNDO)
- end
- SetReadOnly(EID, EditorsCondView[EID].ReadOnly0)
- editor.SetTitle(EID, editor.GetFileName(EID))
- lnp=EditorsCondView[EID].LinePos
- clp=EditorsCondView[EID].ColPos
- tsl = lnp - ScrPos
- lpc = EditorsCondView[EID].LeftPos
- OnProcess[EID] = nil -- процесс обработки окончен
- -- Восстановление закладок
- for i,v in pairs(EditorsCondView[EID].Bookmarks) do
- if v.Line>0 and v.Cursor>0 then
- editor.SetPosition(EID, v.Line, -1, v.Cursor, v.Line-v.ScreenLine+1, v.LeftPos)
- Keys('RCtrl'..tostring(i-1))
- end
- end
- if EditorsCondView[EID].SelectInfo then
- editor.Select(EID, EditorsCondView[EID].SelectInfo.BlockType,
- EditorsCondView[EID].SelectInfo.StartLine,
- EditorsCondView[EID].SelectInfo.StartPos,
- EditorsCondView[EID].SelectInfo.EndPos-EditorsCondView[EID].SelectInfo.StartPos+1,
- EditorsCondView[EID].SelectInfo.EndLine-EditorsCondView[EID].SelectInfo.StartLine+1
- )
- end
- GoToLinePos(EID, lnp, clp, tsl, lpc)
- EditorsCondView[EID] = nil
- return true
- end -- CondViewOff
- -- +
- -- Процедура обработки событий при сжатом виде (запрет на сохранение)
- -- -
- local function ProcessEditorEvent(EID,event)
- local ei=editor.GetInfo(EID)
- if not EditorsCondView[EID] then return end
- if event==F.EE_CLOSE then -- Приберёмся перед уходом
- EditorsCondView[EID] = nil -- уничтожим управляющую структуру
- end
- if event == F.EE_SAVE then
- -- Несмотря на явный запрет F2, ShiftF2 остаётся возможность
- -- сохранения файла (например, макрокоманда CtrlS вызывает сохранение)
- -- В этом случае просто отменяю сжатый режим.
- CondViewOff(ei, EID, false)
- end;
- end -- ProcessEditorEvent
- -- +
- -- Процедура сброса всех сжатых режимов
- -- -
- local function ExitScript()
- local ei, EID
- for ii in pairs(EditorsCondView) do
- if EditorsCondView[ii] then
- EID = ii
- ei = editor.GetInfo(ii)
- CondViewOff(ei, EID, false)
- end
- end
- end -- ExitScript
- -- +
- -- ФУнкция настроек
- local function CondViewSetting()
- local I, ID, y = {}, {}
- local SS=nil
- local guid = win.Uuid("212f0e54-459c-41f7-ab9c-6473f0bcc338")
- table.insert(I, { F.DI_DOUBLEBOX, 1, 1, 60, 15, 0, 0, 0, 0, 'Настройки горячих клавиш для макроса CondView' })
- y=3
- table.insert(I, { F.DI_TEXT, 3, y, 0, y, 0, 0, 0, 0, '&1. Включение «сжатого» режима' })
- table.insert(I, { F.DI_EDIT, 37, y, 58, y, 0, "", 0, 0, S.OnKey })
- ID.OnKey=#I
- y=y+2
- table.insert(I, { F.DI_TEXT, 3, y, 0, y, 0, 0, 0, 0, '&2. Выключение «сжатого» режима' })
- table.insert(I, { F.DI_EDIT, 37, y, 58, y, 0, "", 0, 0, S.OffKey })
- ID.OffKey=#I
- y=y+1
- table.insert(I, { F.DI_TEXT, 6, y, 0, y, 0, 0, 0, 0, 'с возвратом на прежнюю позицию' })
- y=y+2
- table.insert(I, { F.DI_TEXT, 3, y, 0, y, 0, 0, 0, 0, '&3. Выключение «сжатого» режима' })
- table.insert(I, { F.DI_EDIT, 37, y, 58, y, 0, "", 0, 0, S.GoKey })
- ID.GoKey=#I
- y=y+1
- table.insert(I, { F.DI_TEXT, 6, y, 0, y, 0, 0, 0, 0, 'с переходом на новую позицию' })
- y=y+2
- table.insert(I, { F.DI_TEXT, 3, y, 0, y, 0, 0, 0, 0, '&4. Настройка «горячих» клавиш' })
- table.insert(I, { F.DI_EDIT, 37, y, 58, y, 0, "", 0, 0, S.SetupKey })
- ID.SetupKey=#I
- y=y+2
- table.insert(I, { F.DI_BUTTON, 15, y, 0, y, 0, 0,0, F.DIF_DEFAULTBUTTON,"Сохранить" })
- ID.SaveBtn = #I
- table.insert(I, { F.DI_BUTTON, 45, y, 0, y, 0, 0,0, 0,"Отмена" })
- local function DlgProc(hDlg, Msg, _, _)
- local function __GetDialogData()
- SS.OnKey = far.SendDlgMessage(hDlg, "DM_GETTEXT", ID.OnKey, 0)
- SS.OffKey = far.SendDlgMessage(hDlg, "DM_GETTEXT", ID.OffKey, 0)
- SS.GoKey = far.SendDlgMessage(hDlg, "DM_GETTEXT", ID.GoKey, 0)
- SS.SetupKey = far.SendDlgMessage(hDlg, "DM_GETTEXT", ID.SetupKey, 0)
- end
- if not SS then
- SS={}
- __GetDialogData()
- end
- local function __tableFind(tbl, itm)
- for _,v in pairs(tbl) do
- if v==itm then return true end
- end
- return false
- end
- if __tableFind({F.DN_BTNCLICK, F.DN_EDITCHANGE}, Msg) then
- __GetDialogData()
- end
- end
- local dRes = far.Dialog(guid, -1, -1, 63, 17, nil, I, 0 , DlgProc)
- if dRes == ID.SaveBtn then
- MergeSettings(S, SS)
- local sSet='return {\n'
- for i,v in pairs(S) do
- sSet = sSet .. i .. '=' .. string.format('%q', v) .. ',\n'
- end
- sSet = sSet .. '}'
- local fCfg=io.open(CfgName, 'w')
- fCfg:write(sSet)
- fCfg:close()
- if far.Message("Новые назначения вступят в силу после перезагрузки макросов\n"..
- "Перезагрузить ВСЕ макросы сейчас?", "", ";YesNo", "w") == 1 then
- ExitScript()
- local msg = Far.GetConfig('Language.Help')=="Russian" and "Перезагрузка макросов" or "Reload macros"
- far.Message(msg,"","")
- far.MacroLoadAll()
- win.Sleep(200)
- far.AdvControl("ACTL_REDRAWALL")
- end
- end
- end -- CondViewSetting
- -- -
- Macro {
- description="Condenced View"; area="Editor"; key=S.OnKey or "AltF4";
- condition=function()
- return not IsMacroActive()
- end;
- action=
- function()
- local ei=editor.GetInfo()
- if ei == nil then return end
- local EID=ei.EditorID
- --return EditorsCondView[EID] and CondViewOff(ei, EID, false) or CondViewOn(ei, EID)
- return CondViewOn(ei, EID)
- end;
- }
- Macro {
- description="Condenced View - Escape"; area="Editor"; key=S.OffKey or "Esc F10";
- condition=IsMacroActive;
- action=
- function()
- local ei=editor.GetInfo()
- if not ei then return end
- local EID=ei.EditorID
- CondViewOff(ei, EID, false)
- end;
- }
- Macro {
- description="Condenced View - ignore SaveFile"; area="Editor"; key="F2 ShiftF2";
- condition=IsMacroActive;
- action=
- function()
- far.Message('Сохранение файла в режиме\nсжатого просмотра невозможно','Condenced View',';Ok','w')
- end;
- }
- Macro {
- description="Condenced View - ignore ReadOnly mode off CtrlL)"; area="Editor"; key="CtrlL";
- condition=IsMacroActive;
- action=
- function()
- end;
- }
- Macro {
- description="Condenced View - GoTo NewPos"; area="Editor"; key=S.GoKey or "Enter";
- condition=IsMacroActive;
- action=
- function()
- local ei=editor.GetInfo()
- if not ei then return end
- local EID=ei.EditorID
- CondViewOff(ei, EID, true)
- end;
- }
- Macro {
- description="Condenced View - Settings"; area="Editor"; key=S.SetupKey;
- condition=IsMacroActive;
- action=CondViewSetting;
- }
- Event {
- group="EditorEvent";
- condition=
- function(id)
- return EditorsCondView[id]
- end;
- action=ProcessEditorEvent;
- }
- Event {
- group="EditorInput";
- condition=IsMacroActive;
- action=ProcessEditorInput;
- }
- Event {
- group="ExitFAR";
- condition=
- function()
- return EditorsCondView and table.maxn(EditorsCondView) > 0
- end;
- action=ExitScript;
- }
GeSHi © Codebox Plus Extension
- Включение «сжатого» режима
AltF4
В сжатом режиме показываются только те строки, которые начинаются с позиции курсора или левее. "Начинаются" - позиция первого непрбельного (и не Tab) символа. Также скрываются пустые строки.
Выключение «сжатого» режима с возвратом на прежнюю позициюF10 Esc
Выключение «сжатого» режима с переходом на новую позициюEnter
Настройка «горячих» клавишAltF4
- CondView.7z
- (4.36 KiB) Downloaded 124 times