-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdoskey_hinter.lua
61 lines (54 loc) · 1.87 KB
/
doskey_hinter.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- Shows an optional preview of a doskey alias entered in the input line.
--
-- By default the preview is disabled.
--
-- Run `clink set doskey.show_preview true` to enable the preview.
--
-- Requires Clink v1.7.0 or higher.
if not clink.hinter then
log.info("doskey_hinter requires a newer version of Clink; please upgrade.")
return
end
settings.add("doskey.show_preview", true, "Show preview of doskey alias in the input line",
"When both the comment_row.show_hints and doskey.show_preview settings are\n"..
"enabled and the cursor is on a doskey alias, then a preview is shown in the\n"..
"comment row.")
local enabled
local function init_enabled()
enabled = settings.get("doskey.show_preview")
end
clink.onbeginedit(init_enabled)
local function expand_ctrl(text)
local s = ""
if text then
local i = 1
while i <= #text do
local next = text:find("[\x01-\x1f]")
if not next then
s = s..text:sub(i)
break
end
s = s.."^"..string.char(64 + text:byte(next))
i = next + 1
end
end
return s
end
local hinter = clink.hinter(1)
function hinter:gethint(line_state) -- luacheck: no self
if not enabled then
return
end
local cursorpos = line_state:getcursor() or 1
local commandwordindex = line_state:getcommandwordindex()
local info = line_state:getwordinfo(commandwordindex)
if info and info.alias and info.offset <= cursorpos and cursorpos <= info.offset + info.length then
local alias = line_state:getword(commandwordindex) or ""
if alias ~= "" then
local command = os.getalias(alias) or ""
if command ~= "" then
return "Alias "..expand_ctrl(alias).." = "..expand_ctrl(os.getalias(alias)), info.offset
end
end
end
end