awful.completion: sort matches
[awesome.git] / tag.c
blob22e0fb1a1e33a3d5ec698b50c42338ba4432ca35
1 /*
2 * tag.c - tag management
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "screen.h"
23 #include "tag.h"
24 #include "client.h"
25 #include "ewmh.h"
26 #include "widget.h"
28 void
29 tag_unref_simplified(tag_t **tag)
31 tag_unref(globalconf.L, *tag);
34 /** Garbage collect a tag.
35 * \param L The Lua VM state.
36 * \return 0.
38 static int
39 luaA_tag_gc(lua_State *L)
41 tag_t *tag = luaL_checkudata(L, 1, "tag");
42 client_array_wipe(&tag->clients);
43 p_delete(&tag->name);
44 return luaA_object_gc(L);
47 /** View or unview a tag.
48 * \param tag the tag
49 * \param view set visible or not
51 static void
52 tag_view(tag_t *tag, bool view)
54 if(tag->selected != view)
56 int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
58 tag->selected = view;
59 tag->screen->need_reban = true;
60 ewmh_update_net_current_desktop(screen_virttophys(screen_index));
62 if(globalconf.hooks.tags != LUA_REFNIL)
64 lua_pushnumber(globalconf.L, screen_index + 1);
65 tag_push(globalconf.L, tag);
66 if(view)
67 lua_pushliteral(globalconf.L, "select");
68 else
69 lua_pushliteral(globalconf.L, "unselect");
70 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
75 /** Append a tag which on top of the stack to a screen.
76 * \param s screen the screen id
78 void
79 tag_append_to_screen(screen_t *s)
81 tag_t *tag = luaL_checkudata(globalconf.L, -1, "tag");
83 /* can't attach a tag twice */
84 if(tag->screen)
85 return;
87 int screen_index = screen_array_indexof(&globalconf.screens, s);
88 int phys_screen = screen_virttophys(screen_index);
90 tag->screen = s;
91 tag_array_append(&s->tags, tag_ref(globalconf.L, -1));
92 ewmh_update_net_numbers_of_desktop(phys_screen);
93 ewmh_update_net_desktop_names(phys_screen);
94 ewmh_update_workarea(phys_screen);
96 /* call hook */
97 if(globalconf.hooks.tags != LUA_REFNIL)
99 lua_pushnumber(globalconf.L, screen_index + 1);
100 tag_push(globalconf.L, tag);
101 lua_pushliteral(globalconf.L, "add");
102 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
106 /** Remove a tag from screen. Tag must be on a screen and have no clients.
107 * \param tag The tag to remove.
109 static void
110 tag_remove_from_screen(tag_t *tag)
112 int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
113 int phys_screen = screen_virttophys(screen_index);
114 tag_array_t *tags = &tag->screen->tags;
116 for(int i = 0; i < tags->len; i++)
117 if(tags->tab[i] == tag)
119 tag_array_take(tags, i);
120 break;
122 ewmh_update_net_numbers_of_desktop(phys_screen);
123 ewmh_update_net_desktop_names(phys_screen);
124 ewmh_update_workarea(phys_screen);
126 /* call hook */
127 if(globalconf.hooks.tags != LUA_REFNIL)
129 lua_pushnumber(globalconf.L, screen_index + 1);
130 tag_push(globalconf.L, tag);
131 lua_pushliteral(globalconf.L, "remove");
132 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
135 tag->screen = NULL;
137 tag_unref(globalconf.L, tag);
140 /** Tag a client with the tag on top of the stack.
141 * \param c the client to tag
143 void
144 tag_client(client_t *c)
146 tag_t *t = tag_ref(globalconf.L, -1);
148 /* don't tag twice */
149 if(is_client_tagged(c, t))
151 tag_unref(globalconf.L, t);
152 return;
155 client_array_append(&t->clients, c);
156 ewmh_client_update_desktop(c);
157 client_need_reban(c);
159 /* call hook */
160 if(globalconf.hooks.tagged != LUA_REFNIL)
162 client_push(globalconf.L, c);
163 tag_push(globalconf.L, t);
164 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tagged, 2, 0);
168 /** Untag a client with specified tag.
169 * \param c the client to tag
170 * \param t the tag to tag the client with
172 void
173 untag_client(client_t *c, tag_t *t)
175 for(int i = 0; i < t->clients.len; i++)
176 if(t->clients.tab[i] == c)
178 client_need_reban(c);
179 client_array_take(&t->clients, i);
180 client_need_reban(c);
181 ewmh_client_update_desktop(c);
182 /* call hook */
183 if(globalconf.hooks.tagged != LUA_REFNIL)
185 client_push(globalconf.L, c);
186 tag_push(globalconf.L, t);
187 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tagged, 2, 0);
189 tag_unref(globalconf.L, t);
190 return;
194 /** Check if a client is tagged with the specified tag.
195 * \param c the client
196 * \param t the tag
197 * \return true if the client is tagged with the tag, false otherwise.
199 bool
200 is_client_tagged(client_t *c, tag_t *t)
202 for(int i = 0; i < t->clients.len; i++)
203 if(t->clients.tab[i] == c)
204 return true;
206 return false;
209 /** Get the index of the first selected tag.
210 * \param screen Screen.
211 * \return Its index.
214 tags_get_first_selected_index(screen_t *screen)
216 foreach(tag, screen->tags)
217 if((*tag)->selected)
218 return tag_array_indexof(&screen->tags, tag);
219 return 0;
222 /** Set a tag to be the only one viewed.
223 * \param target the tag to see
225 static void
226 tag_view_only(tag_t *target)
228 if(target)
229 foreach(tag, target->screen->tags)
230 tag_view(*tag, *tag == target);
233 /** View only a tag, selected by its index.
234 * \param screen Screen.
235 * \param dindex The index.
237 void
238 tag_view_only_byindex(screen_t *screen, int dindex)
240 tag_array_t *tags = &screen->tags;
242 if(dindex < 0 || dindex >= tags->len)
243 return;
244 tag_view_only(tags->tab[dindex]);
247 /** Create a new tag.
248 * \param L The Lua VM state.
249 * \luastack
250 * \lparam A name.
251 * \lreturn A new tag object.
253 static int
254 luaA_tag_new(lua_State *L)
256 size_t len;
257 const char *name = luaL_checklstring(L, 2, &len);
258 tag_t *tag = tag_new(globalconf.L);
260 a_iso2utf8(name, len, &tag->name, NULL);
262 return 1;
265 /** Get or set the clients attached to this tag.
266 * \param L The Lua VM state.
267 * \return The number of elements pushed on stack.
268 * \luastack
269 * \lparam None or a table of clients to set.
270 * \lreturn A table with the clients attached to this tags.
272 static int
273 luaA_tag_clients(lua_State *L)
275 tag_t *tag = luaL_checkudata(L, 1, "tag");
276 client_array_t *clients = &tag->clients;
277 int i;
279 if(lua_gettop(L) == 2)
281 luaA_checktable(L, 2);
282 foreach(c, tag->clients)
283 untag_client(*c, tag);
284 lua_pushnil(L);
285 while(lua_next(L, 2))
287 client_t *c = luaA_client_checkudata(L, -1);
288 /* push tag on top of the stack */
289 lua_pushvalue(L, 1);
290 tag_client(c);
291 lua_pop(L, 1);
295 lua_createtable(L, clients->len, 0);
296 for(i = 0; i < clients->len; i++)
298 client_push(L, clients->tab[i]);
299 lua_rawseti(L, -2, i + 1);
302 return 1;
305 /** Tag object.
306 * \param L The Lua VM state.
307 * \return The number of elements pushed on stack.
308 * \luastack
309 * \lfield name Tag name.
310 * \lfield screen Screen number of the tag.
311 * \lfield layout Tag layout.
312 * \lfield selected True if the client is selected to be viewed.
314 static int
315 luaA_tag_index(lua_State *L)
317 size_t len;
318 tag_t *tag = luaL_checkudata(L, 1, "tag");
319 const char *attr;
321 if(luaA_usemetatable(L, 1, 2))
322 return 1;
324 attr = luaL_checklstring(L, 2, &len);
326 switch(a_tokenize(attr, len))
328 case A_TK_NAME:
329 lua_pushstring(L, tag->name);
330 break;
331 case A_TK_SCREEN:
332 if(!tag->screen)
333 return 0;
334 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, tag->screen) + 1);
335 break;
336 case A_TK_SELECTED:
337 lua_pushboolean(L, tag->selected);
338 break;
339 default:
340 return 0;
343 return 1;
346 /** Tag newindex.
347 * \param L The Lua VM state.
348 * \return The number of elements pushed on stack.
350 static int
351 luaA_tag_newindex(lua_State *L)
353 size_t len;
354 tag_t *tag = luaL_checkudata(L, 1, "tag");
355 const char *attr = luaL_checklstring(L, 2, &len);
357 switch(a_tokenize(attr, len))
359 int screen;
361 case A_TK_NAME:
363 const char *buf = luaL_checklstring(L, 3, &len);
364 p_delete(&tag->name);
365 a_iso2utf8(buf, len, &tag->name, NULL);
367 break;
368 case A_TK_SCREEN:
369 if(!lua_isnil(L, 3))
371 screen = luaL_checknumber(L, 3) - 1;
372 luaA_checkscreen(screen);
374 else
375 screen = -1;
377 if(tag->screen)
378 tag_remove_from_screen(tag);
380 if(screen != -1)
382 /* push tag on top of the stack */
383 lua_pushvalue(L, 1);
384 tag_append_to_screen(&globalconf.screens.tab[screen]);
386 break;
387 case A_TK_SELECTED:
388 if(tag->screen)
389 tag_view(tag, luaA_checkboolean(L, 3));
390 return 0;
391 default:
392 return 0;
395 return 0;
398 const struct luaL_reg awesome_tag_methods[] =
400 LUA_CLASS_METHODS(tag)
401 { "__call", luaA_tag_new },
402 { NULL, NULL }
404 const struct luaL_reg awesome_tag_meta[] =
406 LUA_OBJECT_META(tag)
407 { "clients", luaA_tag_clients },
408 { "__index", luaA_tag_index },
409 { "__newindex", luaA_tag_newindex },
410 { "__gc", luaA_tag_gc },
411 { NULL, NULL },
414 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80