complete-filename.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. -- complete file path at primary selection location using vis-complete(1)
  2. vis:map(vis.modes.INSERT, "<C-x><C-f>", 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. -- TODO do something clever here
  8. local range = file:text_object_word(pos > 0 and pos-1 or pos);
  9. if not range then return end
  10. if range.finish > pos then range.finish = pos end
  11. if range.start == range.finish then return end
  12. local prefix = file:content(range)
  13. if not prefix then return end
  14. local cmd = string.format("vis-complete --file '%s'", prefix:gsub("'", "'\\''"))
  15. local status, out, err = vis:pipe(file, { start = 0, finish = 0 }, cmd)
  16. if status ~= 0 or not out then
  17. if err then vis:info(err) end
  18. return
  19. end
  20. file:insert(pos, out)
  21. win.selection.pos = pos + #out
  22. end, "Complete file path")
  23. -- complete file path at primary selection location using vis-open(1)
  24. vis:map(vis.modes.INSERT, "<C-x><C-o>", function()
  25. local win = vis.win
  26. local file = win.file
  27. local pos = win.selection.pos
  28. if not pos then return end
  29. local range = file:text_object_word(pos > 0 and pos-1 or pos);
  30. if not range then return end
  31. if range.finish > pos then range.finish = pos end
  32. local prefix = file:content(range)
  33. if not prefix then return end
  34. if prefix:match("^%s*$") then
  35. prefix = ""
  36. range.start = pos
  37. range.finish = pos
  38. end
  39. local cmd = string.format("vis-open -- '%s'*", prefix:gsub("'", "'\\''"))
  40. local status, out, err = vis:pipe(file, { start = 0, finish = 0 }, cmd)
  41. if status ~= 0 or not out then
  42. if err then vis:info(err) end
  43. return
  44. end
  45. out = out:gsub("\n$", "")
  46. file:delete(range)
  47. file:insert(range.start, out)
  48. win.selection.pos = range.start + #out
  49. end, "Complete file name")