-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomplete-dict.lua
36 lines (34 loc) · 1.23 KB
/
complete-dict.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- completes words in dictionary file defined by syntax.
dictfiles = {
dirname = '$HOME/.local/share/dict/',
-- text = '/usr/share/words',
}
vis:map(vis.modes.INSERT, "<C-x><C-k>", function()
local win = vis.win
local file = win.file
local pos = win.selection.pos
if not pos then return end
local range = file:text_object_longword(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 _, j = string.find(prefix, "[([{'\",;=]+")
if j then prefix = prefix:sub(j + 1) end
local syntax = win.syntax or 'bash' -- useful in the command prompt
local dict = dictfiles[syntax] or dictfiles["dirname"] .. syntax
local cmd = string.format("grep '^%s.' %s | vis-menu -i -b -p 'dictionary:'",
prefix, dict)
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
local out = out:gsub("\n$", ""):gsub("^"..prefix, "")
if vis.mode == vis.modes.INSERT then
vis:insert(out)
elseif vis.mode == vis.modes.REPLACE then
vis:replace(out)
end
end, "Complete word in dictionary file")