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-filename.lua (1418B)


  1. local complete_filename = function(expand)
  2. local win = vis.win
  3. local file = win.file
  4. local pos = win.selection.pos
  5. if not pos then return end
  6. -- TODO do something clever here
  7. local range = file:text_object_longword(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. local prefix = file:content(range)
  11. if not prefix then return end
  12. -- Strip leading delimiters for some programming languages
  13. local _, j = prefix:find(".*[{[(<'\"]+")
  14. if not expand and j then prefix = prefix:sub(j + 1) end
  15. if prefix:match("^%s*$") then
  16. prefix = ""
  17. range.start = pos
  18. range.finish = pos
  19. end
  20. local cmdfmt = "vis-complete --file '%s'"
  21. if expand then cmdfmt = "vis-open -- '%s'*" end
  22. local status, out, err = vis:pipe(cmdfmt:format(prefix:gsub("'", "'\\''")))
  23. if status ~= 0 or not out then
  24. if err then vis:info(err) end
  25. return
  26. end
  27. out = out:gsub("\n$", "")
  28. if expand then
  29. file:delete(range)
  30. pos = range.start
  31. end
  32. file:insert(pos, out)
  33. win.selection.pos = pos + #out
  34. end
  35. -- complete file path at primary selection location using vis-complete(1)
  36. vis:map(vis.modes.INSERT, "<C-x><C-f>", function()
  37. complete_filename(false);
  38. end, "Complete file name")
  39. -- complete file path at primary selection location using vis-open(1)
  40. vis:map(vis.modes.INSERT, "<C-x><C-o>", function()
  41. complete_filename(true);
  42. end, "Complete file name (expands path)")