Commit: 128e5010fb102b64ef6969af91784e8edfaa1053
Parent: ab38d5e4e39c493d3a06f4bc5d88fccbd5464cc2
Author: Randy Palamar
Date: Fri, 27 Jan 2023 15:07:53 -0700
vis: macros.lua: simplify macro handling
now each file type specific macro defines a function rather than some
keys to feed through.
all existing (tex) macros are related to inserting text and positioning
the cursor somewhere. this was redone by inserting a string before the
current cursor position and then doing a vis:feedkeys() to move the cursor
and change to insert mode. this is done with the (local) fi() function.
the single haskell macro can also use fi() but insert a "" string.
Diffstat:
1 file changed, 23 insertions(+), 10 deletions(-)
diff --git a/.config/vis/macros.lua b/.config/vis/macros.lua
@@ -1,19 +1,32 @@
require('util')
local function macros(win)
+ local function fi(str, fkeys)
+ return function ()
+ local win = vis.win
+ local pos = win.selection.pos
+ win.file:insert(pos, str)
+ win.selection.pos = pos + #str
+ vis:feedkeys(fkeys)
+ return true
+ end
+ end
+
+ local m = vis.modes
local lang = {}
lang['.tex'] = {
- { 'normal', '\\bf', 'i\\\\textbf{}<Escape>hi' },
- { 'normal', '\\ca', 'i\\begin{cases}<Enter>\\end{cases}<Escape>O' },
- { 'normal', '\\do', 'i\\begin{document}<Enter><Enter><Enter>\\end{document}<Escape>kO\\item' },
- { 'normal', '\\en', 'i\\begin{enumerate}<Enter><Enter><Enter>\\end{enumerate}<Escape>kO\\item' },
- { 'normal', '\\eq', 'i\\begin{equation}<Enter>\\end{equation}<Escape>O' },
- { 'normal', '\\it', 'i\\begin{itemize}<Enter><Enter><Enter>\\end{itemize}<Escape>kO\\item' },
- { 'normal', '\\se', 'i\\section{}<Escape>hi' },
- { 'normal', '\\su', 'i\\subsection{}<Escape>hi' },
+ { m.NORMAL, "\\al", fi("\\begin{align*}\n\\end{align*}", "O") },
+ { m.NORMAL, "\\bf", fi("\\textbf{}", "hi") },
+ { m.NORMAL, "\\ca", fi("\\begin{cases}\n\\end{cases}", "O") },
+ { m.NORMAL, "\\cb", fi("\\begin{center}\n\\colorboxed{blue}{\n}\n\\end{center}", "kO") },
+ { m.NORMAL, "\\en", fi("\\begin{enumerate}\n\n\\item \n\n\\end{enumerate}", "kkA") },
+ { m.NORMAL, "\\eq", fi("\\begin{equation}\n\\end{equation}", "O") },
+ { m.NORMAL, "\\it", fi("\\begin{itemize}\n\n\\item \n\n\\end{itemize}", "kkA") },
+ { m.NORMAL, "\\se", fi("\\section{}", "hi") },
+ { m.NORMAL, "\\su", fi("\\subsection{}", "hi") },
}
lang['.hs'] = {
- { 'normal', 'gq', 'vip:|hindent<Enter><Escape>'},
+ { m.NORMAL, "gq", fi("", "vip:|hindent<Enter><Escape>") },
}
local _, e = util:splitext(win.file.name)
@@ -22,7 +35,7 @@ local function macros(win)
if binds == nil then return end
for _, map in pairs(binds) do
- vis:command(string.format('map! %s %s %s', map[1], map[2], map[3]))
+ vis:map(map[1], map[2], map[3])
end
end
vis.events.subscribe(vis.events.WIN_OPEN, macros)