diff --git a/client.h b/client.h index 40b64e5c..2efd84a6 100644 --- a/client.h +++ b/client.h @@ -51,6 +51,7 @@ int luaA_client_newindex(lua_State *); int luaA_client_userdata_new(lua_State *, client_t *); DO_SLIST(client_t, client, client_unref) +DO_LUA_EXPORT_ARRAY(client, "client_array", client_t, client_array_t, luaA_client_userdata_new) #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/lib/awful.lua.in b/lib/awful.lua.in index 8d2f20be..b80ef810 100644 --- a/lib/awful.lua.in +++ b/lib/awful.lua.in @@ -1052,9 +1052,10 @@ 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 - for k, c in pairs(t.clients) do + local cli = t.clients + for k = 1, #cli do background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarew.png\"" - if c.urgent then + if cli[k].urgent then bg_color = bg_urgent fg_color = fg_urgent break diff --git a/lua.h b/lua.h index 0fa0fb9e..44d7c9ab 100644 --- a/lua.h +++ b/lua.h @@ -259,5 +259,66 @@ luaA_generic_pairs(lua_State *L) return 3; } +#define DO_LUA_EXPORT_ARRAY(pfx, typename, type, atype, ctor) \ + static inline int \ + luaA_##pfx##_array_index(lua_State *L) \ + { \ + atype *value = lua_touserdata(L, 1); \ + type **obj; \ + int idx; \ + \ + if ((obj = lua_touserdata(L, 2))) \ + { \ + for (idx = 0; idx < value->len; idx++) \ + if (value->tab[idx] == *obj) \ + return ctor(L, value->tab[idx]); \ + return 0; \ + } \ + \ + idx = luaL_checknumber(L, 2) - 1; \ + if (idx < 0 || idx >= value->len) \ + return 0; \ + \ + return ctor(L, value->tab[idx]); \ + } \ + static inline int \ + luaA_##pfx##_array_newindex(lua_State *L) \ + { \ + atype *value = lua_touserdata(L, 1); \ + int idx = luaL_checknumber(L, 2); \ + type **elem = luaL_checkudata(L, 3, #pfx); \ + pfx##_array_splice(value, idx - 1, 1, elem, 1); \ + return 0; \ + } \ + static inline int \ + luaA_##pfx##_array_len(lua_State *L) \ + { \ + atype *value = lua_touserdata(L, 1); \ + lua_pushnumber(L, value->len); \ + return 1; \ + } \ + static inline int \ + luaA_##pfx##_array_tostring(lua_State *L) \ + { \ + atype *value = lua_touserdata(L, 1); \ + lua_pushfstring(L, "["typename" udata(%p)]", value); \ + return 1; \ + } \ + static inline void \ + luaA_##pfx##_array_export(lua_State *L, atype *arr) \ + { \ + lua_pushlightuserdata(L, arr); \ + lua_newtable(L); \ + lua_pushcfunction(L, luaA_##pfx##_array_index); \ + lua_setfield(L, -2, "__index"); \ + lua_pushcfunction(L, luaA_##pfx##_array_tostring); \ + lua_setfield(L, -2, "__tostring"); \ + lua_pushcfunction(L, luaA_##pfx##_array_newindex); \ + lua_setfield(L, -2, "__newindex"); \ + lua_pushcfunction(L, luaA_##pfx##_array_len); \ + lua_setfield(L, -2, "__len"); \ + lua_setmetatable(L, -2); \ + } + #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/tag.c b/tag.c index 23167eb6..55adefa4 100644 --- a/tag.c +++ b/tag.c @@ -349,8 +349,6 @@ luaA_tag_index(lua_State *L) size_t len; tag_t **tag = luaA_checkudata(L, 1, "tag"); const char *attr; - client_array_t *clients; - int i; if(luaA_usemetatable(L, 1, 2)) return 1; @@ -383,14 +381,7 @@ luaA_tag_index(lua_State *L) lua_pushnumber(L, (*tag)->ncol); break; case A_TK_CLIENTS: - clients = &(*tag)->clients; - luaA_otable_new(L); - for(i = 0; i < clients->len; i++) - { - luaA_client_userdata_new(L, clients->tab[i]); - luaA_client_userdata_new(L, clients->tab[i]); - lua_rawset(L, -3); - } + luaA_client_array_export(L, &(*tag)->clients); break; default: return 0;