lua: arrays use __next and __pairs metamethods

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-08-10 16:14:35 +02:00
parent 4ab499fe26
commit a5806d5e61
2 changed files with 28 additions and 3 deletions

View file

@ -1052,10 +1052,9 @@ function widget.taglist.label.all(t, args)
if sel and sel.tags[t] then if sel and sel.tags[t] then
background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarefw.png\"" background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarefw.png\""
elseif bg_urgent and fg_urgent then elseif bg_urgent and fg_urgent then
local cli = t.clients for k, c in pairs(t.clients) do
for k = 1, #cli do
background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarew.png\"" background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarew.png\""
if cli[k].urgent then if c.urgent then
bg_color = bg_urgent bg_color = bg_urgent
fg_color = fg_urgent fg_color = fg_urgent
break break

26
lua.h
View file

@ -304,6 +304,27 @@ luaA_generic_pairs(lua_State *L)
lua_pushfstring(L, "["typename" udata(%p)]", value); \ lua_pushfstring(L, "["typename" udata(%p)]", value); \
return 1; \ 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 \ static inline void \
luaA_##pfx##_array_export(lua_State *L, atype *arr) \ 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_setfield(L, -2, "__index"); \
lua_pushcfunction(L, luaA_##pfx##_array_tostring); \ lua_pushcfunction(L, luaA_##pfx##_array_tostring); \
lua_setfield(L, -2, "__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_pushcfunction(L, luaA_##pfx##_array_newindex); \
lua_setfield(L, -2, "__newindex"); \ lua_setfield(L, -2, "__newindex"); \
lua_pushcfunction(L, luaA_##pfx##_array_len); \ lua_pushcfunction(L, luaA_##pfx##_array_len); \