On Fri, Feb 17, 2012 at 4:50 PM, Uli Schlachter <psyc...@znc.in> wrote: >> +function table.apply(t, filter, callback) >> + for _, i in pairs(t) do >> + if filter(i) and callback(i) == false then return end >> + end >> +end > > Dunno how often something like that is needed. Only thing which I can think of > right would be awful.rules.
Rules, together with menu and naughty, is my favorite part :). > Anyway, what do you think about also using this for searching? The previous patch series was headed in the wrong direction -- I was dressing up _for_ without any significant gains. The attached patches better reflect what I was trying to do. Something like: local urxvt = function (c) return awful.rule.match(c, {class = "URxvt")) for c in awful.client.cycle(urxvt) do awful.client.floating.set(c) end Now you can do this using rules, but my function adds a more dynamic, natural, and concise feel to it. You can use it create menus on the fly. Makes it more fun to script awesome from the shell with awesome-client: local filter = function (c) return awful.rule.match(c, {class = "URxvt", except = {instance = "term"}}) end for c in awful.client.cycle(urxvt) do c:kill() end My own selfish reason to push this into awful is to simplify run or raise implementation. I think it is a nice to have feature but people (including myself) have all sort of complicated implementations (just see the wiki page) which causes new users to shy away from it. >> The functionality to terminate matching by returning false can be >> useful if one wants to take an action on only the first matching item, >> like raising a client, etc. > > Hm. For something like this, my table.find() might be a better fit. Possibly. With the new implementation, we won't really need it, because all awful is providing is an easy way to iterate a select list of clients. Do whatever you want in the body of 'for': break iteration, return from function ... whatever suits the need. -- Anurag Priyam
From 3106c952a52d80c3ef30e7cc0c37eacb1e84dcca Mon Sep 17 00:00:00 2001 From: Anurag Priyam <anurag08pri...@gmail.com> Date: Fri, 17 Feb 2012 02:30:41 +0530 Subject: [PATCH 1/2] add awful.util.table.cycle to iterate through elements that match given condition This will help writing concise code when one wants to apply a function to (read, take some action) on a select list of elements in a table (of say, clients and tags). Signed-off-by: Anurag Priyam <anurag08pri...@gmail.com> --- lib/awful/util.lua.in | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-) diff --git a/lib/awful/util.lua.in b/lib/awful/util.lua.in index 1c719a7..026e7d8 100644 --- a/lib/awful/util.lua.in +++ b/lib/awful/util.lua.in @@ -367,4 +367,25 @@ function table.clone(t) return c end +--- +-- Returns an iterator to cycle through, starting from the first element or the +-- given index, all elments of a table that match a given criteria. +-- @param t the table to iterate +-- @param filter a function that returns true to indicate a positive match +-- @param start what index to start iterating from. Default is 1 (=> start of +-- the table) +function table.cycle(t, filter, start) + local count = 0 + local index = start or 1 + local length = #t + + return function () + while count < length do + local item = t[index] + index = cycle(#t, index + 1) + count = count + 1 + if filter(item) then return item end + end + end +end -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- 1.7.9
From a1256797c2c292edd894bfa4c6f0bbac952e1ba5 Mon Sep 17 00:00:00 2001 From: Anurag Priyam <anurag08pri...@gmail.com> Date: Fri, 17 Feb 2012 02:32:28 +0530 Subject: [PATCH 2/2] add awful.client.cycle to iterate through clients that match a given condition A common use case is to cycle through clients that match a given rule and take certain action on them: raise, set or get property, etc.; see usage example in the docs. Signed-off-by: Anurag Priyam <anurag08pri...@gmail.com> --- lib/awful/client.lua.in | 24 ++++++++++++++++++++++++ 1 files changed, 24 insertions(+), 0 deletions(-) diff --git a/lib/awful/client.lua.in b/lib/awful/client.lua.in index 2d5ec45..aae9144 100644 --- a/lib/awful/client.lua.in +++ b/lib/awful/client.lua.in @@ -862,6 +862,30 @@ function property.set(c, prop, value) c:emit_signal("property::" .. prop) end +--- +-- Returns an iterator to cycle through, starting from the client in focus or +-- the given index, all clients that match a given criteria. +-- @param filter a function that returns true to indicate a positive match +-- @param start what index to start iterating from. Defaults to using the +-- index of the currently focused client. +-- @usage e.g.: un-minimize all urxvt instances +-- <p><code> +-- local urxvt = function (c) <br/> +-- return awful.rules.match(c, {class = "URxvt"}) <br/> +-- end <br/> +-- </br> +-- for c in awful.client.cycle(urxvt) do <br/> +-- c.minimized = false <br/> +-- end <br/> +-- +-- </code></p> +function cycle(filter, start) + local clients = capi.client.get() + local focused = capi.client.focus + local start = start or util.table.hasitem(clients, focussed) + return util.table.cycle(clients, filter, start) +end + -- Register standards signals capi.client.add_signal("property::floating_geometry") capi.client.add_signal("property::floating") -- 1.7.9