From a5806d5e617fe4bf76da765c95a796e5292ba5e2 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sun, 10 Aug 2008 16:14:35 +0200 Subject: [PATCH] lua: arrays use __next and __pairs metamethods Signed-off-by: Julien Danjou --- lib/awful.lua.in | 5 ++--- lua.h | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/awful.lua.in b/lib/awful.lua.in index b80ef8100..8d2f20be8 100644 --- a/lib/awful.lua.in +++ b/lib/awful.lua.in @@ -1052,10 +1052,9 @@ function widget.taglist.label.all(t, args) if sel and sel.tags[t] then background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarefw.png\"" elseif bg_urgent and fg_urgent then - local cli = t.clients - for k = 1, #cli do + for k, c in pairs(t.clients) do background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarew.png\"" - if cli[k].urgent then + if c.urgent then bg_color = bg_urgent fg_color = fg_urgent break diff --git a/lua.h b/lua.h index 44d7c9abb..34ea95478 100644 --- a/lua.h +++ b/lua.h @@ -304,6 +304,27 @@ luaA_generic_pairs(lua_State *L) lua_pushfstring(L, "["typename" udata(%p)]", value); \ return 1; \ } \ + static inline int \ + luaA_##pfx##_array_next(lua_State *L) \ + { \ + atype *value = lua_touserdata(L, 1); \ + if(value) \ + { \ + lua_settop(L, 2); \ + if(lua_isnumber(L, 2)) \ + { \ + int idx = lua_tonumber(L, 2); \ + if(idx >= 0 && idx < value->len) \ + return ctor(L, value->tab[idx]); \ + } \ + else if(lua_isnil(L, 2)) \ + { \ + if(value->len) \ + return ctor(L, value->tab[0]); \ + } \ + } \ + return 0; \ + } \ static inline void \ luaA_##pfx##_array_export(lua_State *L, atype *arr) \ { \ @@ -313,6 +334,11 @@ luaA_generic_pairs(lua_State *L) lua_setfield(L, -2, "__index"); \ lua_pushcfunction(L, luaA_##pfx##_array_tostring); \ lua_setfield(L, -2, "__tostring"); \ + lua_pushcfunction(L, luaA_##pfx##_array_next); \ + lua_setfield(L, -2, "__next"); \ + lua_pushcfunction(L, luaA_##pfx##_array_next); \ + lua_pushcclosure(L, luaA_generic_pairs, 1); \ + lua_setfield(L, -2, "__pairs"); \ lua_pushcfunction(L, luaA_##pfx##_array_newindex); \ lua_setfield(L, -2, "__newindex"); \ lua_pushcfunction(L, luaA_##pfx##_array_len); \