snippets.lua (1170B)
1 -- vis-snippets: Insert predefined text snippets 2 -- 3 -- Usage from INSERT mode: 4 -- Press the snipleader (default @@) and a key specified in the snippets 5 -- table. This will insert the corresponding value string. Optionally, a 6 -- cursor position can be specified with '<1>'. 7 -- 8 -- Author: Anders Damsgaard <anders@adamsgaard.dk>, adc on #vis-editor 9 10 local snippets = { 11 ae = 'æ', 12 AE = 'Æ', 13 oe = 'ø', 14 OE = 'Ø', 15 aa = 'å', 16 AA = 'Å', 17 q = "\\emph{``<1>''}", 18 i = '\\item ', 19 l = '\\label{<1>}', 20 r = '\\ref{<1>}', 21 sig = '<1>\n-- \n' .. 22 'Anders Damsgaard\n\n' .. 23 'Academia: https://adamsgaard.dk\n' .. 24 'Code: https://src.adamsgaard.dk\n' .. 25 'Photography: https://andersdamsgaard.com' 26 } 27 28 local snippets_leader = '@@' 29 snippets_cursor = '<1>' 30 31 for k, v in pairs(snippets) do 32 if string.find(v, snippets_cursor) then 33 vis:map(vis.modes.INSERT, snippets_leader..k, 34 function() 35 _, j = string.find(v, snippets_cursor) 36 vis:insert(string.gsub(v, snippets_cursor, '')) 37 vis:feedkeys('<Escape>') 38 for _ = j+1, string.len(v) do 39 vis:feedkeys('h') 40 end 41 vis:feedkeys('i') 42 end) 43 else 44 vis:map(vis.modes.INSERT, snippets_leader..k, v) 45 end 46 end