Code: Select all
-- Ручная раскраска блоков в редакторе
local F = far.Flags
-- Для хранения раскраски в альтернативном потоке заменить в следующей строке точку на двоеточие
local FarColorExt = '.BAX_FarColor'
local nColor = {}
local a0Colors = {}
local FarColorerID = 'D2F36B62-A470-418D-83A3-ED7A3710E5B5'
-- Если скрипт конфликтует с колорером - включить в следующей строке
local SwitchOffColorer = false --Plugin.Exist(FarColorerID)
Macro{
id="9F32BC77-1611-41A5-8159-2B2EC205B791";
area="Editor";
key="AltShiftC";
description="&1. Раскрасить блок";
flags="EVSelection";
action=function(data)
local EditorId = tostring(editor.GetInfo().EditorID)
if not a0Colors[EditorId] then
a0Colors[EditorId] = {}
if SwitchOffColorer then
a0Colors[EditorId].Colorer = Plugin.SyncCall(FarColorerID, 'Editor', 'Status', false)
end
end
local aColor = a0Colors[EditorId]
nColor = far.ColorDialog(nColor)
if nColor then
local Sel = editor.GetSelection()
editor.Select(nil, F.BTYPE_NONE)
for iLine=Sel.StartLine,Sel.EndLine do
local StartPos, EndPos= (Sel.BlockType==F.BTYPE_COLUMN or iLine==Sel.StartLine) and Sel.StartPos or 1,
(Sel.BlockType==F.BTYPE_COLUMN or iLine==Sel.EndLine) and Sel.EndPos or 10000
table.insert( aColor, {Line=iLine; StartPos=StartPos; EndPos=EndPos, Color=nColor})
editor.AddColor(nil, iLine, StartPos, EndPos, F.ECF_TABMARKFIRST, nColor)
end
end
end;
}
---------------------------------------------------
Macro{
id="D15986B1-CF31-45F6-A7F1-D78CDE4A9071";
area="Editor";
key="AltShiftC";
description="&2. Убрать раскраску блока";
flags="EVSelection";
action=function(data)
local EditorId = tostring(editor.GetInfo().EditorID)
a0Colors[EditorId] = a0Colors[EditorId] or {}
local Sel = editor.GetSelection()
editor.Select(nil, F.BTYPE_NONE)
local aColor = a0Colors[EditorId]
for iLine=Sel.StartLine,Sel.EndLine do
local StartPos= (Sel.BlockType==F.BTYPE_COLUMN or iLine==Sel.StartLine) and Sel.StartPos or 1
for i=#aColor,1,-1 do
if aColor[i].Line == iLine and aColor[i].StartPos == StartPos then table.remove(aColor, i) end
end
editor.DelColor(nil, iLine, StartPos)
end
end;
}
---------------------------------------------------
Macro{
id="C58D66F6-0F5B-44DF-9AAB-7F8F0CD2BCEA";
area="Editor";
key="AltShiftC";
description="&3. Убрать всю раскраску файла";
action=function(data)
if far.Message('Вы уверены, что хотите удалить всю раскраску файла?\nОтменить это действие будет невозможно…',
'Ручная раскраска файла',';YesNo') == 1 then
local fn = Editor.FileName .. FarColorExt
local sEditorID = tostring(editor.GetInfo().EditorID)
if SwitchOffColorer and a0Colors[sEditorID] then
Plugin.SyncCall(FarColorerID, 'Editor', 'Status', a0Colors[sEditorID].Colorer)
end
a0Colors[sEditorID] = nil
for i=1,Editor.Lines do
editor.DelColor(nil, i, 0)
end
if mf.fexist(fn) then win.DeleteFile(fn) end
end
end;
}
---------------------------------------------------
Event {
id = "44A61972-A54D-42F6-B8E2-0BDBA5FC433C";
group = "EditorEvent";
description = "Восстановление раскраски после открытия";
condition = function(EditorID, Event, Param)
return Event == F.EE_READ
end;
action = function(EditorID, Event, Param)
local cEditorID = tostring(EditorID)
local fn = Editor.FileName .. FarColorExt
if mf.fexist(fn) then
local h = io.open(fn, 'r')
if h then
local aColor = {}
for l in h:lines() do
local ok,aTmp = pcall(loadstring, 'return ' ..l)
if ok then ok,aTmp = pcall(aTmp) end
if ok then
table.insert(aColor, aTmp)
end
end
h:close()
if next(aColor) then
if SwitchOffColorer then
aColor.Colorer = Plugin.SyncCall(FarColorerID, 'Editor', 'Status', false)
end
--table.sort(aColor, function(a1,a2) return a1 and a1.Line < a2.Line end)
a0Colors[cEditorID] = aColor
for _,v in ipairs(aColor) do
editor.AddColor(EditorID, v.Line, v.StartPos, v.EndPos, F.ECF_TABMARKFIRST, v.Color)
end
end -- if next(aColor)
end -- if h
end -- if mf.fexist
end;
}
---------------------------------------------------
Event {
id = "35A97F43-7D6F-4628-A9D2-9F9D4216147B";
group = "EditorEvent";
description = "Сохранение раскраски при сохранении файла";
condition = function(EditorID, Event, Param)
return Event == F.EE_SAVE
end;
action = function(EditorID, Event, Param)
local function TabToStr(aTab)
local i,v = next(aTab)
local aRes = {}
while i do
if type(v) == 'table' then
table.insert( aRes, i .. '=' .. TabToStr(v) )
elseif type(v) == 'string' then
table.insert( aRes, i .. '=' .. ('%q'):format(v) )
else
table.insert( aRes, i .. '=' .. tostring(v) )
end
i,v = next(aTab, i)
end
return '{' .. table.concat(aRes, ';') .. '}'
end
local fn = Param.FileName .. FarColorExt
local aColor = a0Colors[tostring(EditorID)]
if aColor then
local h = io.open(fn, 'w')
if h then
for _,v in ipairs(aColor) do
v = TabToStr(v)
h:write(v .. '\n')
end
h:close()
end
end
end;
}