vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

complete-word.lua

(1246B)


      1 -- complete word at primary selection location using vis-complete(1)
      2 
      3 vis:map(vis.modes.INSERT, "<C-n>", function()
      4 	local win = vis.win
      5 	local file = win.file
      6 	local pos = win.selection.pos
      7 	if not pos then return end
      8 
      9 	local range = file:text_object_word(pos > 0 and pos-1 or pos);
     10 	if not range then return end
     11 	if range.finish > pos then range.finish = pos end
     12 	if range.start == range.finish then return end
     13 	local prefix = file:content(range)
     14 	if not prefix then return end
     15 
     16 	vis:feedkeys("<vis-selections-save><Escape><Escape>")
     17 	-- collect words starting with prefix
     18 	vis:command("x/\\b" .. prefix .. "\\w+/")
     19 	local candidates = {}
     20 	for sel in win:selections_iterator() do
     21 		table.insert(candidates, file:content(sel.range))
     22 	end
     23 	vis:feedkeys("<Escape><Escape><vis-selections-restore>")
     24 	if #candidates == 1 and candidates[1] == "\n" then return end
     25 	candidates = table.concat(candidates, "\n")
     26 
     27 	local status, out, err = vis:pipe(candidates, "sort -u | vis-menu")
     28 	if status ~= 0 or not out then
     29 		if err then vis:info(err) end
     30 		return
     31 	end
     32 	out = out:sub(#prefix + 1, #out - 1)
     33 	file:insert(pos, out)
     34 	win.selection.pos = pos + #out
     35 	-- restore mode to what it was on entry
     36 	vis.mode = vis.modes.INSERT
     37 end, "Complete word in file")