Является упрощенным аналогом плагина TrueTemplate:
http://plugring.farmanager.com/plugin.php?pid=227
Клавиатурные команды:
Space - Раскрыть шаблон
Сtrl+J - Раскрыть ближайший шаблон
СtrlShift+J - Список всех шаблонов
Нативная LUA версия:
Макрос:
- --
- -- Шаблоны автоподстановки
- -- (c) 2013, Max Rusov
- -- https://code.google.com/p/far-macro-library/wiki/Editor_EasyTemplate
- --
- -- Макрос является упрощенным аналогом плагина TrueTemplate:
- -- http://plugring.farmanager.com/plugin.php?pid=227
- --
- -- Клавиатурные команды:
- --
- -- Space Раскрыть шаблон
- -- Сtrl+J Раскрыть ближайший шаблон
- -- СtrlShift+J Список всех шаблонов
- --
- local Settings = require "Edt-EasyTemplate-Common"
- --local UserSettings = require "Edt-EasyTemplate-User"
- -------------------------------------------------------------------------------
- -- Общие функции
- local function StrIn(AStr, AListStr, APart)
- local iStr
- for iStr in AListStr:gmatch("[^%s,]+") do
- if (iStr == AStr) or (APart and (iStr:find(AStr) == 1)) then
- return true
- end
- end
- return false
- end
- local function FindTemplate(AName, AWord, APart)
- local vExt = mf.fsplit(AName, 8):sub(2):lower()
- local vTpl = {}
- for i, vRow in ipairs(Settings) do
- if StrIn(vExt, vRow[1]) then
- for j, vTempl in ipairs(vRow[2]) do
- if not AWord or StrIn(AWord, vTempl[1], APart) then
- vTpl[#vTpl + 1] = vTempl
- end
- end
- end
- end
- if #vTpl > 1 then
- local vItems = ""
- for i = 1, #vTpl do
- vItems = vItems .. "&" .. (i <= 9 and i or string.char(65 + i - 10) ) .. " " .. vTpl[i][2] .. "\n"
- end
- local vRes = Menu.Show(vItems, "Templates", 8)
- if vRes == 0 then
- return false
- end
- return vTpl[vRes]
- else
- return vTpl[1]
- end
- end
- local function InsertTemplate(Id, AOldLen, ATemplate)
- editor.UndoRedo(Id, "EUR_BEGIN")
- if AOldLen == 0 then
- print(" ")
- AOldLen = 1
- end
- local vInfo = editor.GetInfo()
- local vRow1 = vInfo.CurLine
- local vPos1 = vInfo.CurPos
- if AOldLen > 0 then
- vPos1 = vPos1 - AOldLen
- editor.Select(Id, 1, vRow1, vPos1, AOldLen, 1)
- end
- local vRowStr = editor.GetString(Id).StringText;
- local vPreStr = vRowStr:sub(0, vPos1-1):gsub("%S", " ")
- vPat = ATemplate[3]
- local vSetCol1, vSetCol2, vSetRow1, vSetRow2
- local n = 0
- while vPat do
- local i = mf.index(vPat, "\n")
- if i >= 0 then
- vStr = vPat:sub(0, i)
- vPat = vPat:sub(i + 2)
- else
- vStr = vPat
- vPat = nil
- end
- if vPat == "" then
- vPat = nil
- end
- vStr = vStr:gsub("%%=(.-);",
- function(Expr)
- local Func = loadstring("return "..Expr)
- return Func()
- end
- )
- local j = mf.index(vStr, "%_")
- if j >= 0 then
- vStr = vStr:gsub("%%_", "")
- vSetCol1 = j
- vSetRow1 = n
- end
- local j = mf.index(vStr, "_%")
- if j >= 0 then
- vStr = vStr:gsub("_%%", "")
- vSetCol2 = j
- vSetRow2 = n
- end
- if n > 0 then
- vStr = vPreStr .. vStr
- end
- if vPat then
- vStr = vStr .. "\n"
- end
- print(vStr)
- n = n + 1
- end
- if vSetRow1 then
- if vSetRow2 then
- editor.Select(Id, 1, vRow1 + vSetRow1, vPos1 + vSetCol1, vSetCol2 - vSetCol1, vSetRow2 - vSetRow1 + 1)
- end
- editor.SetPosition(Id, vRow1 + vSetRow1, vPos1 + vSetCol1)
- end
- editor.UndoRedo(Id, "EUR_END")
- end
- local function EasyTemplateExpand(Id)
- local vRowStr = mf.trim(editor.GetString(Id).StringText, 2)
- if editor.GetInfo().CurPos - 1 ~= vRowStr:len() then
- return false
- end
- -- local vWord = vRowStr:gsub("^.-%s-([^%s]+)%s*$", "%1")
- -- local vWord = vRowStr:gsub("^.*%s+([^%s]+)$", "%1")
- local vWord = mf.trim(vRowStr)
- local vTemplate = FindTemplate( Editor.FileName, vWord )
- if not vTemplate then
- return false
- end
- InsertTemplate(Id, vWord:len(), vTemplate)
- return true
- end
- local function EasyTemplateList(Id)
- local vRowStr = editor.GetString(Id).StringText;
- local n = editor.GetInfo().CurPos - 1
- local i = n
- while (i > 0) and far.LIsAlphanum(vRowStr:sub(i, i)) do
- i = i - 1
- end
- local vWord = vRowStr:sub(i + 1, n)
- local vTemplate = FindTemplate( Editor.FileName, vWord, true )
- if not vTemplate then
- if vTemplate == nil then
- mf.beep()
- end
- return false
- end
- InsertTemplate(Id, vWord:len(), vTemplate)
- return true
- end
- local function EasyTemplateListAll(Id)
- local vTemplate = FindTemplate( Editor.FileName )
- if not vTemplate then
- return false
- end
- InsertTemplate(Id, 0, vTemplate)
- return true
- end
- local function AppendArray(A1, A2)
- for i = 1, #A2 do
- A1[#A1 + 1] = A2[i]
- end
- return A1
- end
- if UserSettings then
- Settings = AppendArray(Settings, UserSettings)
- end
- -------------------------------------------------------------------------------
- -- Макросы вызова
- Macro
- {
- description="Easy Template: expand template"; area="Editor"; key="Space";
- action=function()
- if not EasyTemplateExpand(nil) then
- Keys("Space")
- end
- end;
- }
- Macro
- {
- description="Easy Template: expand nearest template"; area="Editor"; key="CtrlJ";
- action=function()
- EasyTemplateList(nil)
- end;
- }
- Macro
- {
- description="Easy Template: List of all templates"; area="Editor"; key="CtrlShiftJ";
- action=function()
- EasyTemplateListAll(nil)
- end;
- }
GeSHi © Codebox Plus Extension
Библиотека шаблонов (в каталог %FarProfile%\Macros\modules):
- return {
- -----------------------------
- {"pas,pp,dpr", {
- { "proc,procedure", "procedure",
- [[
- procedure %_Name_%();
- begin
- end;
- ]]},
- { "func,function", "function",
- [[
- function %_Name_%() :Integer;
- begin
- Result := 0;
- end;
- ]]},
- { "beg,begin", "begin-end",
- [[
- begin
- %_
- end;
- ]]},
- { "if", "if-then",
- [[
- if %_ then
- ;
- ]]},
- { "ife", "if-then-else",
- [[
- if %_ then
- else
- ;
- ]]},
- { "for,fori", "for",
- [[
- for i = 0 to %_Count - 1 do begin
- end;
- ]]},
- { "ford", "for downto",
- [[
- for i = %_Count - 1 downto 0 do begin
- end;
- ]]},
- { "while", "while",
- [[
- while %_Condition_% do begin
- end;
- ]]},
- { "repeat", "repeat-until",
- [[
- repeat
- until %_Condition_%;
- ]]},
- { "case", "case",
- [[
- case %_Value_% of
- 0: {};
- 1: {};
- else
- {}
- end;
- ]]},
- { "try,tryf", "try-finally",
- [[
- try
- %_
- finally
- end;
- ]]},
- { "trye", "try-except",
- [[
- try
- %_
- except
- end;
- ]]},
- }}, -- pas,pp,dpr
- -----------------------------
- {"c,cpp,h,hpp",{
- { "if", "if-then",
- [[
- if (%_)
- {
- }
- ]]},
- { "ife", "if-then-else",
- [[
- if (%_)
- {
- }
- else
- {
- }
- ]]},
- { "for,fori", "for",
- [[
- for (int i = 0; i < %_Count; i++)
- {
- }
- ]]},
- { "while", "while",
- [[
- while (%_Condition_%)
- {
- }
- ]]},
- }}, -- c,cpp,h,hpp
- -----------------------------
- {"fml,fmlua", {
- { "macro", "macro definition",
- [[
- macro Descr="%_Описание_%" Key="" Area=""
- {{
- -- Текст макроса...
- }}
- ]]}
- }}, -- fml,fmlua
- -----------------------------
- {"lua,fmlua", {
- { "func,function", "function",
- [[
- function %_Name_%()
- end
- ]]},
- { "if", "if-then",
- [[
- if %_ then
- end
- ]]},
- { "ife", "if-then-else",
- [[
- if %_ then
- else
- end
- ]]},
- { "for,fori", "for - simple",
- [[
- for i = %_1, Count do
- end
- ]]},
- { "fora", "for - generic",
- [[
- for i, iVal in ipairs(aVal) do
- %_
- end
- ]]},
- { "while", "while",
- [[
- while %_Condition_% do
- end
- ]]},
- { "repeat", "repeat-until",
- [[
- repeat
- until %_Condition_%
- ]]},
- }}, -- lua,fmlua
- -----------------------------
- {"lua", {
- { "macro", "macro definition",
- [[
- Macro {
- description="%_Описание_%"; key=""; area="";
- action = function()
- -- Текст макроса...
- end
- }
- ]]},
- }}, -- lua
- }
GeSHi © Codebox Plus Extension
FML версия макроса:
http://code.google.com/p/far-macro-libr ... syTemplate