Автор: vladvro
Дата сообщения: 31.03.2007 13:53
обновил скрипт для макросов [more]
macro_support.lua
Код: [no]--[[[/no]
[no]Macros support for SciTE[/no]
[no]Version 2.0[/no]
[no]Author: VladVRO[/no]
[no]---------------------------------------------------[/no]
[no]Description:[/no]
[no] macros recording, storing and playing support[/no]
[no] (Поддержка записи и воспроизведения макросов)[/no]
[no]Using:[/no]
[no] add next line into SciTEGlobal.properties:[/no]
[no] ext.lua.startup.script=$(SciteDefaultHome)\macro_support.lua[/no]
[no] or if you already have startup script than add next line in it:[/no]
[no] require ("macro_support.lua")[/no]
[no] and next lines into SciTEUser.properties:[/no]
[no] command.name.40.*=Macro Load From Selection[/no]
[no] command.40.*=MacroLoadFromSelection[/no]
[no] command.mode.40.*=subsystem:lua,savebefore:no[/no]
[no] command.name.41.*=Macro Fill To Buffer[/no]
[no] command.41.*=MacroFillToBuffer[/no]
[no] command.mode.41.*=subsystem:lua,savebefore:no[/no]
[no] command.name.42.*=Macro Fill To Buffer (LUA code)[/no]
[no] command.42.*=MacroFillToBuffer LUA[/no]
[no] command.mode.42.*=subsystem:lua,savebefore:no[/no]
[no]---------------------------------------------------[/no]
[no]]][/no]
scite.Perform([no]"macroenable:1"[/no])
[no]-- global tables[/no]
local glb_macro_buf = {}
local glb_macros_table = {}
local glb_macros_name_table = {}
[no]-- position in list for new recorded macro[/no]
[no]-- -1 - last position[/no]
[no]-- default = 1[/no]
local new_position = props[[no]'macro.new.record.position'[/no]]
if new_position == [no]""[/no] then
new_position = 1
else
new_position = tonumber(new_position)
end
[no]-- path to file with macros[/no]
macro_file = props[[no]'macro.file.path'[/no]]
if macro_file == [no]""[/no] then
macro_file = props[[no]'scite.userhome'[/no]]
if macro_file == [no]""[/no] then macro_file = props[[no]'SciteDefaultHome'[/no]] end
macro_file = macro_file..[no]"\\SciTE.macro"[/no]
end
function OnMacro(cmd, msg)
if cmd == [no]"macro:run"[/no] then
if msg == [no]"<clean list>"[/no] then
scite.Perform([no]"currentmacro:"[/no])
while table.getn(glb_macros_name_table) > 0 do
glb_macros_table[glb_macros_name_table[1]] = nil
table.remove(glb_macros_name_table,1)
end
else
local macro = glb_macros_table[msg]
if macro then
editor:BeginUndoAction()
for _,val in macro do
local c,lp,wp = unpack(val)
if IFACE_FUNCTIONS_USE_WP[c] then
scite.SendEditor(c,wp,lp)
else
scite.SendEditor(c,lp,0)
end
end
editor:EndUndoAction()
end
end
elseif cmd == [no]"macro:record"[/no] then
for c,wp,_,lp in string.gfind(msg, [no]"(%d+);(%d+);(%d+);(.*)"[/no]) do
table.insert(glb_macro_buf, {tonumber(c),lp,wp})
end
elseif cmd == [no]"macro:startrecord"[/no] then
table_clear(glb_macro_buf)
[no]-- visualization[/no]
if props[[no]'style.*.33.normal'[/no]] == [no]""[/no] then props[[no]'style.*.33.normal'[/no]] = props[[no]'style.*.33'[/no]] end
if props[[no]'macro.recording.numfield.style'[/no]] ~= [no]""[/no] then
props[[no]"style.*.33"[/no]] = props[[no]'macro.recording.numfield.style'[/no]]
scite.Perform([no]"reloadproperties:"[/no])
end
elseif cmd == [no]"macro:stoprecord"[/no] then
local name = MacroAddToList(glb_macro_buf, nil, new_position)
scite.Perform([no]"currentmacro:"[/no]..ifnil(name,[no]""[/no]))
table_clear(glb_macro_buf)
[no]-- visualization[/no]
if props[[no]'macro.recording.numfield.style'[/no]] ~= [no]""[/no] then
props[[no]'style.*.33'[/no]] = props[[no]'style.*.33.normal'[/no]]
scite.Perform([no]"reloadproperties:"[/no])
end
[no]-- autosave[/no]
if props[[no]'macro.autosave'[/no]] == [no]"1"[/no] then
MacroSaveToFile(macro_file)
end
elseif cmd == [no]"macro:getlist"[/no] then
if table.getn(glb_macros_name_table) > 0 then
local list = [no]""[/no]
for _,name in glb_macros_name_table do
list = list..name..[no]";"[/no]
end
list = list..[no]"<clean list>"[/no]
local old_sep = editor.AutoCSeparator
editor.AutoCSeparator = string.byte([no]';'[/no])
scite.Perform([no]"macrolist:"[/no]..list)
editor.AutoCSeparator = old_sep
else
print([no]"> no macros yet!"[/no])
end
end
end
function MacroAddToList(macro, name, pos)
if table.getn(macro) > 0 then
if not name then
local i = table.getn(glb_macros_name_table)
repeat
i = i + 1
name = [no]"record"[/no]..i
until glb_macros_table[name] == nil or i > 9999
end
if not glb_macros_table[name] or table.getn(glb_macros_name_table) == 0 then
if pos then
table.insert(glb_macros_name_table, pos, name)
else
table.insert(glb_macros_name_table, name)
end
end
glb_macros_table[name] = table_icopy({},macro)
end
return name
end
local function macro_to_string(mode)
local text = [no]""[/no]
local iface_num2name = {}
for name,num in IFACE_FUNCTIONS do
iface_num2name[num] = name
end
if mode == [no]"LUA"[/no] then
mode = true
else
mode = false
end
for _,name in glb_macros_name_table do
macro = glb_macros_table[name]
if macro then
text = text..[no]"\n--- "[/no]..name..[no]" ---\n"[/no]
for _,val in macro do
local c,lp,wp = unpack(val)
if string.len(lp) > 0 then
for _,v in MACRO_CONVERT_CHARS do
lp = string.gsub(lp,unpack(v))
end
lp = [no]"'"[/no]..lp..[no]"'"[/no]
end
if mode then
if IFACE_FUNCTIONS_USE_WP[c] then
if string.len(lp) > 0 then
wp = wp..[no]", "[/no]
end
else
wp = [no]""[/no]
end
text = text..[no]"editor:"[/no]..ifnil(iface_num2name[c],c)..[no]"("[/no]..wp..lp..[no]")\n"[/no]
else
text = text..ifnil(iface_num2name[c],c)..[no]";"[/no]..wp..[no]";"[/no]..lp..[no]"\n"[/no]
end
end
end
end
return text
end
local function macro_load(text)
local macro = {}
local name = nil
local text = text..[no]"\n"[/no]
for str in string.gfind(text, [no]"([^\n]*)[\n]"[/no]) do
if string.sub(str, 1, 3) == [no]"---"[/no] then
MacroAddToList(macro, name)
macro = {}
name = nil
for a in string.gfind(str, [no]"([%w_]+)"[/no]) do
name = a; break;
end
else
str = string.gsub(str, [no]"\r"[/no], [no]""[/no])
for fnc,wp,lp in string.gfind(str, [no]"(%w+);(%d+);(.*)"[/no]) do
local c = IFACE_FUNCTIONS[fnc]
if c then
if string.len(lp) > 0 then
lp = dostring([no]"return "[/no]..lp)
end
table.insert(macro, {c,lp,wp})
else
print([no]"> undefined function "[/no]..fnc)
return false
end
end
end
end
MacroAddToList(macro, name)
end
MACRO_CONVERT_CHARS = {
{[no]"\\"[/no], [no]"\\\\"[/no]},
{[no]"\'"[/no], [no]"\\\'"[/no]},
{[no]"\r"[/no], [no]"\\r"[/no]},
{[no]"\n"[/no], [no]"\\n"[/no]},
}
IFACE_FUNCTIONS = {
[[no]"AddText"[/no]] = 2001,
[[no]"AppendText"[/no]] = 2282,
[[no]"BackTab"[/no]] = 2328,
[[no]"Cancel"[/no]] = 2325,
[[no]"CharLeft"[/no]] = 2304,
[[no]"CharLeftExtend"[/no]] = 2305,
[[no]"CharLeftRectExtend"[/no]] = 2428,
[[no]"CharRight"[/no]] = 2306,
[[no]"CharRightExtend"[/no]] = 2307,
[[no]"CharRightRectExtend"[/no]] = 2429,
[[no]"Clear"[/no]] = 2180,
[[no]"ClearAll"[/no]] = 2004,
[[no]"Copy"[/no]] = 2178,
[[no]"CopyRange"[/no]] = 2419,
[[no]"Cut"[/no]] = 2177,
[[no]"DelLineLeft"[/no]] = 2395,
[[no]"DelLineRight"[/no]] = 2396,
[[no]"DelWordLeft"[/no]] = 2335,
[[no]"DelWordRight"[/no]] = 2336,
[[no]"DeleteBack"[/no]] = 2326,
[[no]"DeleteBackNotLine"[/no]] = 2344,
[[no]"DocumentEnd"[/no]] = 2318,
[[no]"DocumentEndExtend"[/no]] = 2319,
[[no]"DocumentStart"[/no]] = 2316,
[[no]"DocumentStartExtend"[/no]] = 2317,
[[no]"EditToggleOvertype"[/no]] = 2324,
[[no]"FormFeed"[/no]] = 2330,
[[no]"GotoLine"[/no]] = 2024,
[[no]"GotoPos"[/no]] = 2025,
[[no]"Home"[/no]] = 2312,
[[no]"HomeDisplay"[/no]] = 2345,
[[no]"HomeDisplayExtend"[/no]] = 2346,
[[no]"HomeExtend"[/no]] = 2313,
[[no]"HomeRectExtend"[/no]] = 2430,
[[no]"HomeWrap"[/no]] = 2349,
[[no]"HomeWrapExtend"[/no]] = 2450,
[[no]"InsertText"[/no]] = 2003,
[[no]"LineCopy"[/no]] = 2455,
[[no]"LineCut"[/no]] = 2337,
[[no]"LineDelete"[/no]] = 2338,
[[no]"LineDown"[/no]] = 2300,
[[no]"LineDownExtend"[/no]] = 2301,
[[no]"LineDownRectExtend"[/no]] = 2426,
[[no]"LineDuplicate"[/no]] = 2404,
[[no]"LineEnd"[/no]] = 2314,
[[no]"LineEndDisplay"[/no]] = 2347,
[[no]"LineEndDisplayExtend"[/no]] = 2348,
[[no]"LineEndExtend"[/no]] = 2315,
[[no]"LineEndRectExtend"[/no]] = 2432,
[[no]"LineEndWrap"[/no]] = 2451,
[[no]"LineEndWrapExtend"[/no]] = 2452,
[[no]"LineScrollDown"[/no]] = 2342,
[[no]"LineScrollUp"[/no]] = 2343,
[[no]"LineTranspose"[/no]] = 2339,
[[no]"LineUp"[/no]] = 2302,
[[no]"LineUpExtend"[/no]] = 2303,
[[no]"LineUpRectExtend"[/no]] = 2427,
[[no]"LinesJoin"[/no]] = 2288,
[[no]"LinesSplit"[/no]] = 2289,
[[no]"LoadLexerLibrary"[/no]] = 4007,
[[no]"LowerCase"[/no]] = 2340,
[[no]"MarkerAdd"[/no]] = 2043,
[[no]"MarkerAddSet"[/no]] = 2466,
[[no]"MarkerDefine"[/no]] = 2040,
[[no]"MarkerDefinePixmap"[/no]] = 2049,
[[no]"MarkerDelete"[/no]] = 2044,
[[no]"MarkerDeleteAll"[/no]] = 2045,
[[no]"MarkerDeleteHandle"[/no]] = 2018,
[[no]"MarkerGet"[/no]] = 2046,
[[no]"MarkerLineFromHandle"[/no]] = 2017,
[[no]"MarkerNext"[/no]] = 2047,
[[no]"MarkerPrevious"[/no]] = 2048,
[[no]"MoveCaretInsideView"[/no]] = 2401,
[[no]"NewLine"[/no]] = 2329,
[[no]"Null"[/no]] = 2172,
[[no]"PageDown"[/no]] = 2322,
[[no]"PageDownExtend"[/no]] = 2323,
[[no]"PageDownRectExtend"[/no]] = 2434,
[[no]"PageUp"[/no]] = 2320,
[[no]"PageUpExtend"[/no]] = 2321,
[[no]"PageUpRectExtend"[/no]] = 2433,
[[no]"ParaDown"[/no]] = 2413,
[[no]"ParaDownExtend"[/no]] = 2414,
[[no]"ParaUp"[/no]] = 2415,
[[no]"ParaUpExtend"[/no]] = 2416,
[[no]"Paste"[/no]] = 2179,
[[no]"ReplaceSel"[/no]] = 2170,
[[no]"ReplaceTarget"[/no]] = 2194,
[[no]"ReplaceTargetRE"[/no]] = 2195,
[[no]"ScrollCaret"[/no]] = 2169,
[[no]"SearchAnchor"[/no]] = 2366,
[[no]"SearchInTarget"[/no]] = 2197,
[[no]"SearchNext"[/no]] = 2367,
[[no]"SearchPrev"[/no]] = 2368,
[[no]"SelectAll"[/no]] = 2013,
[[no]"SelectionDuplicate"[/no]] = 2469,
[[no]"SetCharsDefault"[/no]] = 2444,
[[no]"SetSavePoint"[/no]] = 2014,
[[no]"SetSel"[/no]] = 2160,
[[no]"SetText"[/no]] = 2181,
[[no]"StutteredPageDown"[/no]] = 2437,
[[no]"StutteredPageDownExtend"[/no]] = 2438,
[[no]"StutteredPageUp"[/no]] = 2435,
[[no]"StutteredPageUpExtend"[/no]] = 2436,
[[no]"Tab"[/no]] = 2327,
[[no]"TargetFromSelection"[/no]] = 2287,
[[no]"UpperCase"[/no]] = 2341,
[[no]"VCHome"[/no]] = 2331,
[[no]"VCHomeExtend"[/no]] = 2332,
[[no]"VCHomeRectExtend"[/no]] = 2431,
[[no]"VCHomeWrap"[/no]] = 2453,
[[no]"VCHomeWrapExtend"[/no]] = 2454,
[[no]"WordLeft"[/no]] = 2308,
[[no]"WordLeftEnd"[/no]] = 2439,
[[no]"WordLeftEndExtend"[/no]] = 2440,
[[no]"WordLeftExtend"[/no]] = 2309,
[[no]"WordPartLeft"[/no]] = 2390,
[[no]"WordPartLeftExtend"[/no]] = 2391,
[[no]"WordPartRight"[/no]] = 2392,
[[no]"WordPartRightExtend"[/no]] = 2393,
[[no]"WordRight"[/no]] = 2310,
[[no]"WordRightEnd"[/no]] = 2441,
[[no]"WordRightEndExtend"[/no]] = 2442,
[[no]"WordRightExtend"[/no]] = 2311,
[[no]"WordStartPosition"[/no]] = 2266,
}
IFACE_FUNCTIONS_USE_WP = {
[IFACE_FUNCTIONS[[no]"CopyRange"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"GotoLine"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"GotoPos"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"InsertText"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"LinesSplit"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerAdd"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerAddSet"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerDefine"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerDefinePixmap"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerDelete"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerDeleteAll"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerDeleteHandle"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerGet"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerLineFromHandle"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerNext"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"MarkerPrevious"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"SearchNext"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"SearchPrev"[/no]]] = true,
[IFACE_FUNCTIONS[[no]"SetSel"[/no]]] = true,
}
[no]----------------------------------------------------------[/no]
[no]-- io functions[/no]
function MacroFillToBuffer(mode)
editor:append(macro_to_string(mode))
end
function MacroLoadFromSelection()
macro_load(editor:GetSelText())
[no]-- autosave[/no]
if props[[no]'macro.autosave'[/no]] == [no]"1"[/no] then
MacroSaveToFile(macro_file)
end
end
function MacroLoadFromFile(filename)
local fl = io.input(filename)
fl:seek([no]"set"[/no])
macro_load(fl:read([no]"*a"[/no]))
end
function MacroSaveToFile(filename)
io.output(filename)
io.write(macro_to_string())
io.close()
end
[no]----------------------------------------------------------[/no]
[no]-- common functions[/no]
function table_clear(tbl)
while table.getn(tbl) > 0 do table.remove(tbl) end
end
function table_icopy(tbl,from_tbl)
for _,v in from_tbl do table.insert(tbl,v) end
return tbl
end
function ifnil(Val, defVal)
if (Val == nil) then
return defVal;
else
return Val;
end
end
[no]----------------------------------------------------------[/no]
[no]-- load macros at startup[/no]
if props[[no]'macro.load.on.startup'[/no]] == [no]"1"[/no] then
MacroLoadFromFile(macro_file)
end