Completion for the Lua eval prompt

This isn't very useful, maybe, but it's kinda cool that it is even possible!

Use this function as the completion_callback argument to awful.prompt.run.

function lua_completion (line, cur_pos, ncomp) -- Only complete at the end of the line, for now if cur_pos ~= #line + 1 then return line, cur_pos end

-- We're really interested in the part following the last (, [, comma or space  local lastsep = #line - (line:reverse:find('[[(, ]') or #line)   local lastidentifier   if lastsep ~= 0 then      lastidentifier = line:sub(lastsep + 2)   else      lastidentifier = line   end

local environment = _G

-- String up to last dot is our current environment local lastdot = #lastidentifier - (lastidentifier:reverse:find('.', 1, true) or #lastidentifier) if lastdot ~= 0 then -- We have an environment; for each component in it, descend into it     for env in lastidentifier:sub(1, lastdot):gmatch('([^.]+)') do         if not environment[env] then -- Oops, no such subenvironment, bail out return line, cur_pos end environment = environment[env] end end

local tocomplete = lastidentifier:sub(lastdot + 1) if tocomplete:sub(1, 1) == '.' then tocomplete = tocomplete:sub(2) end

local completions = {} for k, v in pairs(environment) do     if type(k) == "string" and k:sub(1, #tocomplete) == tocomplete then table.insert(completions, k)     end end

if #completions == 0 then return line, cur_pos end while ncomp > #completions do     ncomp = ncomp - #completions end

local str = "" if lastdot + lastsep ~= 0 then str = line:sub(1, lastsep + lastdot + 1) end str = str .. completions[ncomp] cur_pos = #str + 1 return str, cur_pos end