diff --git a/screen.c b/screen.c index 96a1a7a2..316562b9 100644 --- a/screen.c +++ b/screen.c @@ -366,7 +366,7 @@ luaA_screen_module_index(lua_State *L) return luaA_pushscreen(L, &globalconf.screens.tab[screen]); } -/** Get or set screen tags. +/** Get screen tags. * \param L The Lua VM state. * \return The number of elements pushed on stack. * \luastack @@ -377,23 +377,16 @@ luaA_screen_module_index(lua_State *L) static int luaA_screen_tags(lua_State *L) { - int i; screen_t *s = luaL_checkudata(L, 1, "screen"); if(lua_gettop(L) == 2) { luaA_checktable(L, 2); - /* remove current tags */ - foreach(tag, s->tags) - tag_set_screen(*tag, NULL); + /* Detach all tags, but go backward since the array len will change */ + for(int i = s->tags.len - 1; i >= 0; i--) + tag_remove_from_screen(s->tags.tab[i]); - tag_array_wipe(&s->tags); - tag_array_init(&s->tags); - - s->need_reban = true; - - /* push new tags */ lua_pushnil(L); while(lua_next(L, 2)) tag_append_to_screen(L, -1, s); @@ -401,7 +394,7 @@ luaA_screen_tags(lua_State *L) else { lua_createtable(L, s->tags.len, 0); - for(i = 0; i < s->tags.len; i++) + for(int i = 0; i < s->tags.len; i++) { luaA_object_push(L, s->tags.tab[i]); lua_rawseti(L, -2, i + 1); diff --git a/tag.c b/tag.c index 31357f90..84256c3d 100644 --- a/tag.c +++ b/tag.c @@ -121,7 +121,10 @@ tag_append_to_screen(lua_State *L, int udx, screen_t *s) /* can't attach a tag twice */ if(tag->screen) + { + lua_remove(L, udx); return; + } int screen_index = screen_array_indexof(&globalconf.screens, s); int phys_screen = screen_virttophys(screen_index); @@ -152,9 +155,12 @@ tag_append_to_screen(lua_State *L, int udx, screen_t *s) /** Remove a tag from screen. Tag must be on a screen and have no clients. * \param tag The tag to remove. */ -static void +void tag_remove_from_screen(tag_t *tag) { + if(!tag->screen) + return; + int screen_index = screen_array_indexof(&globalconf.screens, tag->screen); int phys_screen = screen_virttophys(screen_index); tag_array_t *tags = &tag->screen->tags; @@ -431,8 +437,7 @@ luaA_tag_set_screen(lua_State *L, tag_t *tag) luaA_checkscreen(screen); } - if(tag->screen) - tag_remove_from_screen(tag); + tag_remove_from_screen(tag); if(screen != -1) tag_append_to_screen(L, -3, &globalconf.screens.tab[screen]); diff --git a/tag.h b/tag.h index 63ffcd5f..0bd99d7f 100644 --- a/tag.h +++ b/tag.h @@ -30,6 +30,7 @@ void untag_client(client_t *, tag_t *); bool is_client_tagged(client_t *, tag_t *); void tag_view_only_byindex(screen_t *, int); void tag_append_to_screen(lua_State *, int, screen_t *); +void tag_remove_from_screen(tag_t *); void tag_unref_simplified(tag_t **); ARRAY_FUNCS(tag_t *, tag, tag_unref_simplified) @@ -38,7 +39,6 @@ void tag_class_setup(lua_State *); bool tag_get_selected(tag_t *); char *tag_get_name(tag_t *); -void tag_set_screen(tag_t *, screen_t *); #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80