vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 17a9ee170624338c1a440b1d084f1b3565c46727 parent ce506bb7fa36b1e975cb83574fd887a5128defb7 Author: Marc André Tanner <mat@brain-dump.org> Date: Sun, 5 Mar 2017 11:04:38 +0100 lua: reimplement word and file name completion in lua The file name completion does not yet behave the same way as the previous C code because the completion prefix is currently simply calculated using the `iw` text object which does not handle common path elements (e.g. `.`, `/`, `~`, etc). Diffstat:
| A | lua/plugins/complete-filename.lua | | | 23 | +++++++++++++++++++++++ |
| A | lua/plugins/complete-word.lua | | | 22 | ++++++++++++++++++++++ |
| M | lua/vis-std.lua | | | 2 | ++ |
3 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/lua/plugins/complete-filename.lua b/lua/plugins/complete-filename.lua @@ -0,0 +1,23 @@ +-- complete file name at primary cursor location using vis-complete(1) + +vis:map(vis.modes.INSERT, "<C-x><C-f>", function() + local win = vis.win + local file = win.file + local pos = win.cursor.pos + if not pos then return end + -- TODO do something clever here + local range = file:text_object_word(pos > 0 and pos-1 or pos); + if not range then return end + if range.finish > pos then range.finish = pos end + if range.start == range.finish then return end + local prefix = file:content(range) + if not prefix then return end + local cmd = string.format("vis-complete --file '%s'", prefix:gsub("'", "'\\''")) + local status, out, err = vis:pipe(file, { start = 0, finish = 0 }, cmd) + if status ~= 0 or not out then + if err then vis:info(err) end + return + end + file:insert(pos, out) + win.cursor.pos = pos + #out +end, "Complete file name") diff --git a/lua/plugins/complete-word.lua b/lua/plugins/complete-word.lua @@ -0,0 +1,22 @@ +-- complete word at primary cursor location using vis-complete(1) + +vis:map(vis.modes.INSERT, "<C-n>", function() + local win = vis.win + local file = win.file + local pos = win.cursor.pos + if not pos then return end + local range = file:text_object_word(pos > 0 and pos-1 or pos); + if not range then return end + if range.finish > pos then range.finish = pos end + if range.start == range.finish then return end + local prefix = file:content(range) + if not prefix then return end + local cmd = string.format("vis-complete --word '%s'", prefix:gsub("'", "'\\''")) + local status, out, err = vis:pipe(file, { start = 0, finish = file.size }, cmd) + if status ~= 0 or not out then + if err then vis:info(err) end + return + end + file:insert(pos, out) + win.cursor.pos = pos + #out +end, "Complete word in file") diff --git a/lua/vis-std.lua b/lua/vis-std.lua @@ -131,3 +131,5 @@ require('plugins/filetype') require('plugins/textobject-lexer') require('plugins/digraph') require('plugins/number-inc-dec') +require('plugins/complete-word') +require('plugins/complete-filename')