diff --git a/lib/awful/completion.lua.in b/lib/awful/completion.lua.in index dce9dd96..740d1951 100644 --- a/lib/awful/completion.lua.in +++ b/lib/awful/completion.lua.in @@ -1,12 +1,14 @@ --------------------------------------------------------------------------- -- @author Julien Danjou <julien@danjou.info> --- @copyright 2008 Julien Danjou +-- @author Sébastien Gross <seb-awesome@chezwam.org> +-- @copyright 2008 Julien Danjou, Sébastien Gross -- @release @AWESOME_VERSION@ --------------------------------------------------------------------------- -- Grab environment we need local io = io local table = table +local math = math --- Completion module for awful module("awful.completion") @@ -15,6 +17,10 @@ module("awful.completion") local bashcomp_funcs = {} local bashcomp_src = "/etc/bash_completion" +--- keywords table use for the generic completion +-- should not be local +keywords = {} + --- Enable programmable bash completion in awful.completion.bash at the price of -- a slight overhead -- @param src The bash completion source file, /etc/bash_completion by default. @@ -109,4 +115,42 @@ function bash(command, cur_pos, ncomp) return str, cur_pos end +--- Run a generic completion. +-- For this function to run properly the awful.completion.keyword table should +-- be fed up with all keywords. The completion is run against these keywords. +-- @param text The current text the user had typed yet. +-- @param cur_pos The current cursor position. +-- @param ncomp The number of yet requested completion using current text. +-- @return The new match and the new cursor position. +function generic(text, cur_pos, ncomp) + -- The keywords table may be empty + if #keywords == 0 then + return text, #text + 1 + end + + -- if no text had been typed yet, then we could start cycling around all + -- keywords with out filtering and move the cursor at the end of keyword + if text == nil or #text == 0 then + ncomp = math.mod(ncomp - 1, #keywords) + 1 + return keywords[ncomp], #keywords[ncomp] + 2 + end + + -- Filter out only keywords starting with text + local matches = {} + table.foreach(keywords, function(_, x) + if x:sub(1 , #text) == text then + table.insert(matches, x) + end + end) + + -- if there are no matches just leave out with the current text and position + if #matches == 0 then + return text, #text + 1 + end + + -- cycle around all matches + ncomp = math.mod(ncomp - 1, #matches) + 1 + return matches[ncomp], #matches[ncomp] + 1 +end + -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80