macros.lua (2340B)
1 local util = require('util') 2 3 -- insert 4 local function ins(str) 5 return function () 6 local win = vis.win 7 local pos = win.selection.pos 8 win.file:insert(pos, str) 9 win.selection.pos = pos + #str 10 return true 11 end 12 end 13 14 -- surround selection 15 local function sur(pre, suf, trim) 16 return function () 17 local win = vis.win 18 if trim then vis:feedkeys("_") end 19 for sel in win:selections_iterator() do 20 win.file:insert(sel.range.start, pre) 21 win.file:insert(sel.range.finish, suf) 22 end 23 win:draw() 24 return true 25 end 26 end 27 28 -- inserts a latex environment 29 local lenv = function(env, inner) 30 local out = "\\begin{%s}\n%s\\end{%s}" 31 if (inner) then inner = inner .. "\n" end 32 return ins(out:format(env, (inner or ""), env)) 33 end 34 35 local function macros(win) 36 local m = vis.modes 37 local fc, fk = util.function_chain, util.feedkeys 38 39 local lang = {} 40 lang["latex"] = { 41 { m.NORMAL, "\\al", fc({ lenv("align*"), fk("O\t") }) }, 42 { m.NORMAL, "\\ca", fc({ lenv("cases"), fk("O\t") }) }, 43 { m.NORMAL, "\\cb", fc({ lenv("center", "\\colorboxed{blue}{\n}"), fk("kO\t") }) }, 44 { m.NORMAL, "\\en", fc({ lenv("enumerate","\n\\item \n"), fk("kkA") }) }, 45 { m.NORMAL, "\\eq", fc({ lenv("equation*"), fk("O\t") }) }, 46 { m.NORMAL, "\\fi", fc({ lenv("figure", "\t\\includegraphics[width=\\textwidth]{}"), fk("k$hi") }) }, 47 { m.NORMAL, "\\it", fc({ lenv("itemize", "\n\\item \n"), fk("kkA") }) }, 48 { m.NORMAL, "\\mi", fc({ ins("\\begin{minipage}[c]{0.49\\textwidth}\n\\end{minipage}\\hfill"), fk("O\t") }) }, 49 { m.NORMAL, "\\ne", fc({ lenv("equation"), fk("O\t") }) }, 50 { m.NORMAL, "\\se", fc({ ins("\\section{}"), fk("hi") }) }, 51 { m.NORMAL, "\\su", fc({ ins("\\subsection{}"), fk("hi") }) }, 52 { m.VISUAL, "\\bf", sur("\\textbf{", "}", true) }, 53 { m.VISUAL, "\\cb", sur("\\colorboxed{blue}{\n", "}\n") }, 54 { m.VISUAL, "\\ce", sur("\\begin{center}\n", "\\end{center}\n") }, 55 { m.VISUAL, "\\em", sur("\\emph{", "}", true) }, 56 { m.VISUAL, "\\hl", sur("\\hl{", "}", true) }, 57 { m.VISUAL, "\\mi", sur("\\begin{minipage}[c]{0.49\\textwidth}\n", "\\end{minipage}\\hfill\n") }, 58 } 59 lang["haskell"] = { 60 { m.NORMAL, "gq", "vip:|hindent<Enter><Escape>" }, 61 } 62 63 local binds = lang[win.syntax] 64 if binds == nil then return end 65 66 for _, map in pairs(binds) do 67 win:map(map[1], map[2], map[3]) 68 end 69 end 70 vis.events.subscribe(vis.events.WIN_OPEN, macros)