/usr/share/luakit/lib/completion.lua is in luakit 2012.09.13-r1-8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | ------------------------------------------------------------
-- Command completion --
-- © 2010-2011 Mason Larobina <mason.larobina@gmail.com> --
-- © 2010 Fabian Streitel <karottenreibe@gmail.com> --
------------------------------------------------------------
-- Get Lua environment
local ipairs = ipairs
local setmetatable = setmetatable
local string = string
local table = table
local unpack = unpack
-- Get luakit environment
local lousy = require "lousy"
local history = require "history"
local bookmarks = require "bookmarks"
local escape = lousy.util.escape
local new_mode, get_mode = new_mode, get_mode
local add_binds = add_binds
local capi = { luakit = luakit }
module "completion"
-- Store completion state (indexed by window)
local data = setmetatable({}, { __mode = "k" })
-- Add completion start trigger
local key = lousy.bind.key
add_binds("command", {
key({}, "Tab", function (w) w:set_mode("completion") end),
})
-- Return to command mode with original text and with original cursor position
function exit_completion(w)
local state = data[w]
w:enter_cmd(state.orig_text, { pos = state.orig_pos })
end
-- Command completion binds
add_binds("completion", {
key({}, "Tab", "Select next matching completion item.",
function (w) w.menu:move_down() end),
key({"Shift"}, "Tab", "Select previous matching completion item.",
function (w) w.menu:move_up() end),
key({}, "Up", "Select next matching completion item.",
function (w) w.menu:move_up() end),
key({}, "Down", "Select previous matching completion item.",
function (w) w.menu:move_down() end),
key({"Control"}, "j", "Select next matching completion item.",
function (w) w.menu:move_down() end),
key({"Control"}, "k", "Select previous matching completion item.",
function (w) w.menu:move_up() end),
key({}, "Escape", "Stop completion and restore original command.",
exit_completion),
key({"Control"}, "[", "Stop completion and restore original command.",
exit_completion),
})
function update_completions(w, text, pos)
local state = data[w]
-- Other parts of the code are triggering input changed events
if state.lock then return end
local input = w.ibar.input
local text, pos = text or input.text, pos or input.position
-- Don't rebuild the menu if the text & cursor position are the same
if text == state.text and pos == state.pos then return end
-- Exit completion if cursor outside a word
if string.sub(text, pos, pos) == " " then
w:enter_cmd(text, { pos = pos })
end
-- Update left and right strings
state.text, state.pos = text, pos
state.left = string.sub(text, 2, pos)
state.right = string.sub(text, pos + 1)
-- Call each completion function
local groups = {}
for _, func in ipairs(_M.order) do
table.insert(groups, func(state) or {})
end
-- Join all result tables
rows = lousy.util.table.join(unpack(groups))
if rows[1] then
-- Prevent callbacks triggering recursive updates.
state.lock = true
w.menu:build(rows)
w.menu:show()
if not state.built then
state.built = true
if rows[2] then w.menu:move_down() end
end
state.lock = false
elseif not state.built then
exit_completion(w)
else
w.menu:hide()
end
end
new_mode("completion", {
enter = function (w)
-- Clear state
local state = {}
data[w] = state
-- Save original text and cursor position
local input = w.ibar.input
state.orig_text = input.text
state.orig_pos = input.position
-- Update input text when scrolling through completion menu items
w.menu:add_signal("changed", function (m, row)
state.lock = true
if row then
input.text = row.left .. " " .. state.right
input.position = #row.left
else
input.text = state.orig_text
input.position = state.orig_pos
end
state.lock = false
end)
update_completions(w)
end,
changed = function (w, text)
if not data[w].lock then
update_completions(w, text)
end
end,
move_cursor = function (w, pos)
if not data[w].lock then
update_completions(w, nil, pos)
end
end,
leave = function (w)
w.menu:hide()
w.menu:remove_signals("changed")
end,
activate = function (w, text)
local pos = w.ibar.input.position
if string.sub(text, pos+1, pos+1) == " " then pos = pos+1 end
w:enter_cmd(text, { pos = pos })
end,
})
-- Completion functions
funcs = {
-- Add command completion items to the menu
command = function (state)
-- We are only interested in the first word
if string.match(state.left, "%s") then return end
-- Check each command binding for matches
local pat = "^" .. state.left
local cmds = {}
for _, b in ipairs(get_mode("command").binds) do
if b.cmds then
for i, cmd in ipairs(b.cmds) do
if string.match(cmd, pat) then
if i == 1 then
cmd = ":" .. cmd
else
cmd = string.format(":%s (:%s)", cmd, b.cmds[1])
end
cmds[cmd] = { escape(cmd), escape(b.desc) or "", left = ":" .. b.cmds[1] }
break
end
end
end
end
-- Sort commands
local keys = lousy.util.table.keys(cmds)
-- Return if no results
if not keys[1] then return end
-- Build completion menu items
local ret = {{ "Command", "Description", title = true }}
for _, cmd in ipairs(keys) do
table.insert(ret, cmds[cmd])
end
return ret
end,
-- Add history completion items to the menu
history = function (state)
-- Find word under cursor (also checks not first word)
local term = string.match(state.left, "%s(%S+)$")
if not term then return end
local sql = [[
SELECT uri, title, lower(uri||title) AS text
FROM history WHERE text GLOB ?
ORDER BY visits DESC LIMIT 25
]]
local rows = history.db:exec(sql, { string.format("*%s*", term) })
if not rows[1] then return end
-- Strip last word (so that we can append the completion uri)
local left = ":" .. string.sub(state.left, 1,
string.find(state.left, "%s(%S+)$"))
-- Build rows
local ret = {{ "History", "URI", title = true }}
for _, row in ipairs(rows) do
table.insert(ret, { escape(row.title), escape(row.uri),
left = left .. row.uri })
end
return ret
end,
-- add bookmarks completion to the menu
bookmarks = function (state)
-- Find word under cursor (also checks not first word)
local term = string.match(state.left, "%s(%S+)$")
if not term then return end
local sql = [[
SELECT uri, title, lower(uri||title||tags) AS text
FROM bookmarks WHERE text GLOB ?
ORDER BY title DESC LIMIT 25
]]
local rows = bookmarks.db:exec(sql, { string.format("*%s*", term) })
if not rows[1] then return end
-- Strip last word (so that we can append the completion uri)
local left = ":" .. string.sub(state.left, 1,
string.find(state.left, "%s(%S+)$"))
-- Build rows
local ret = {{ "Bookmarks", "URI", title = true }}
for _, row in ipairs(rows) do
local title = row.title ~= "" and row.title or row.uri
table.insert(ret, { escape(title), escape(row.uri),
left = left .. row.uri })
end
return ret
end,
}
-- Order of completion items
order = {
funcs.command,
funcs.history,
funcs.bookmarks,
}
-- vim: et:sw=4:ts=8:sts=4:tw=80
|