logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

complete-word.lua (1283B)


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