2 * luah.c - Lua helper functions
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
5 * Copyright (C) 2008-2009 Julien Danjou <julien@danjou.info>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "common/util.h"
25 #include "common/lualib.h"
31 luaH_modifier_table_push(lua_State
*L
, guint state
) {
34 if (state
& GDK_MODIFIER_MASK
) {
36 #define MODKEY(key, name) \
37 if (state & GDK_##key##_MASK) { \
38 lua_pushstring(L, name); \
39 lua_rawseti(L, -2, i++); \
42 MODKEY(SHIFT
, "Shift");
44 MODKEY(CONTROL
, "Control");
57 luaH_keystr_push(lua_State
*L
, guint keyval
)
61 guint32 ukval
= gdk_keyval_to_unicode(keyval
);
63 /* check for printable unicode character */
64 if (g_unichar_isgraph(ukval
)) {
65 ulen
= g_unichar_to_utf8(ukval
, ucs
);
67 lua_pushstring(L
, ucs
);
69 /* sent keysym for non-printable characters */
71 lua_pushstring(L
, gdk_keyval_name(keyval
));
74 /* UTF-8 aware string length computing.
75 * Returns the number of elements pushed on the stack. */
77 luaH_utf8_strlen(lua_State
*L
)
79 const gchar
*cmd
= luaL_checkstring(L
, 1);
80 lua_pushnumber(L
, (ssize_t
) g_utf8_strlen(NONULL(cmd
), -1));
84 /* Overload standard Lua next function to use __next key on metatable.
85 * Returns the number of elements pushed on stack. */
87 luaHe_next(lua_State
*L
)
89 if(luaL_getmetafield(L
, 1, "__next")) {
91 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
94 luaL_checktype(L
, 1, LUA_TTABLE
);
102 /* Overload lua_next() function by using __next metatable field to get
103 * next elements. `idx` is the index number of elements in stack.
104 * Returns 1 if more elements to come, 0 otherwise. */
106 luaH_next(lua_State
*L
, gint idx
)
108 if(luaL_getmetafield(L
, idx
, "__next")) {
109 /* if idx is relative, reduce it since we got __next */
111 /* copy table and then move key */
112 lua_pushvalue(L
, idx
);
113 lua_pushvalue(L
, -3);
115 lua_pcall(L
, 2, 2, 0);
116 /* next returned nil, it's the end */
117 if(lua_isnil(L
, -1)) {
124 else if(lua_istable(L
, idx
))
125 return lua_next(L
, idx
);
131 /* Generic pairs function.
132 * Returns the number of elements pushed on stack. */
134 luaH_generic_pairs(lua_State
*L
)
136 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
137 lua_pushvalue(L
, 1); /* state, */
138 lua_pushnil(L
); /* and initial value */
142 /* Overload standard pairs function to use __pairs field of metatables.
143 * Returns the number of elements pushed on stack. */
145 luaHe_pairs(lua_State
*L
)
147 if(luaL_getmetafield(L
, 1, "__pairs")) {
149 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
150 return lua_gettop(L
);
152 luaL_checktype(L
, 1, LUA_TTABLE
);
153 return luaH_generic_pairs(L
);
157 luaH_ipairs_aux(lua_State
*L
)
159 gint i
= luaL_checkint(L
, 2) + 1;
160 luaL_checktype(L
, 1, LUA_TTABLE
);
161 lua_pushinteger(L
, i
);
162 lua_rawgeti(L
, 1, i
);
163 return (lua_isnil(L
, -1)) ? 0 : 2;
166 /* Overload standard ipairs function to use __ipairs field of metatables.
167 * Returns the number of elements pushed on stack. */
169 luaHe_ipairs(lua_State
*L
)
171 if(luaL_getmetafield(L
, 1, "__ipairs")) {
173 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
174 return lua_gettop(L
);
177 luaL_checktype(L
, 1, LUA_TTABLE
);
178 lua_pushvalue(L
, lua_upvalueindex(1));
180 lua_pushinteger(L
, 0); /* and initial value */
184 /* Enhanced type() function which recognize luakit objects.
185 * \param L The Lua VM state.
186 * \return The number of arguments pushed on the stack.
189 luaHe_type(lua_State
*L
)
192 lua_pushstring(L
, luaH_typename(L
, 1));
197 /* Fix up and add handy standard lib functions */
199 luaH_fixups(lua_State
*L
)
201 /* export string.wlen */
202 lua_getglobal(L
, "string");
203 lua_pushcfunction(L
, &luaH_utf8_strlen
);
204 lua_setfield(L
, -2, "wlen");
207 lua_pushliteral(L
, "next");
208 lua_pushcfunction(L
, luaHe_next
);
209 lua_settable(L
, LUA_GLOBALSINDEX
);
211 lua_pushliteral(L
, "pairs");
212 lua_pushcfunction(L
, luaHe_next
);
213 lua_pushcclosure(L
, luaHe_pairs
, 1); /* pairs get next as upvalue */
214 lua_settable(L
, LUA_GLOBALSINDEX
);
216 lua_pushliteral(L
, "ipairs");
217 lua_pushcfunction(L
, luaH_ipairs_aux
);
218 lua_pushcclosure(L
, luaHe_ipairs
, 1);
219 lua_settable(L
, LUA_GLOBALSINDEX
);
221 lua_pushliteral(L
, "type");
222 lua_pushcfunction(L
, luaHe_type
);
223 lua_settable(L
, LUA_GLOBALSINDEX
);
226 /* Look for an item: table, function, etc.
227 * \param L The Lua VM state.
228 * \param item The pointer item.
231 luaH_hasitem(lua_State
*L
, gconstpointer item
)
234 while(luaH_next(L
, -2)) {
235 if(lua_topointer(L
, -1) == item
) {
236 /* remove value and key */
240 if(lua_istable(L
, -1))
241 if(luaH_hasitem(L
, item
)) {
242 /* remove key and value */
252 /* Browse a table pushed on top of the index, and put all its table and
253 * sub-table ginto an array.
254 * \param L The Lua VM state.
255 * \param elems The elements array.
256 * \return False if we encounter an elements already in list.
259 luaH_isloop_check(lua_State
*L
, GPtrArray
*elems
)
261 if(lua_istable(L
, -1)) {
262 gconstpointer object
= lua_topointer(L
, -1);
264 /* Check that the object table is not already in the list */
265 for(guint i
= 0; i
< elems
->len
; i
++)
266 if(elems
->pdata
[i
] == object
)
269 /* push the table in the elements list */
270 g_ptr_array_add(elems
, (gpointer
) object
);
272 /* look every object in the "table" */
274 while(luaH_next(L
, -2)) {
275 if(!luaH_isloop_check(L
, elems
)) {
276 /* remove key and value */
280 /* remove value, keep key for next iteration */
287 /* Check if a table is a loop. When using tables as direct acyclic digram,
289 * \param L The Lua VM state.
290 * \param idx The index of the table in the stack
291 * \return True if the table loops.
294 luaH_isloop(lua_State
*L
, gint idx
)
296 /* elems is an elements array that we will fill with all array we
297 * encounter while browsing the tables */
298 GPtrArray
*elems
= g_ptr_array_new();
300 /* push table on top */
301 lua_pushvalue(L
, idx
);
303 gboolean ret
= luaH_isloop_check(L
, elems
);
305 /* remove pushed table */
308 g_ptr_array_free(elems
, TRUE
);
313 /* Returns a string from X selection.
314 * \param L The Lua VM state.
315 * \return The number of elements pushed on stack (1).
318 luaH_luakit_selection(lua_State
*L
)
320 int n
= lua_gettop(L
);
321 GdkAtom atom
= GDK_SELECTION_PRIMARY
;
324 const gchar
*arg
= luaL_checkstring(L
, 1);
325 /* Follow xclip(1) behavior: check only the first character of argument */
330 atom
= GDK_SELECTION_SECONDARY
;
333 atom
= GDK_SELECTION_CLIPBOARD
;
336 luaL_argerror(L
, 1, "should be 'primary', 'secondary' or 'clipboard'");
340 GtkClipboard
*selection
= gtk_clipboard_get(atom
);
341 gchar
*text
= gtk_clipboard_wait_for_text(selection
);
342 lua_pushstring(L
, text
);
347 /* luakit global table.
348 * \param L The Lua VM state.
349 * \return The number of elements pushed on stack.
351 * \lfield font The default font.
352 * \lfield font_height The default font height.
353 * \lfield conffile The configuration file which has been loaded.
356 luaH_luakit_index(lua_State
*L
)
358 if(luaH_usemetatable(L
, 1, 2))
363 const gchar
*prop
= luaL_checklstring(L
, 2, &len
);
364 luakit_token_t token
= l_tokenize(prop
, len
);
370 for (guint i
= 0; i
< globalconf
.windows
->len
; i
++) {
371 w
= globalconf
.windows
->pdata
[i
];
372 luaH_object_push(L
, w
->ref
);
373 lua_rawseti(L
, -2, i
+1);
378 lua_pushcfunction(L
, luaH_luakit_selection
);
381 case L_TK_INSTALL_PATH
:
382 lua_pushstring(L
, LUAKIT_INSTALL_PATH
);
391 /* Newindex function for the luakit global table.
392 * \param L The Lua VM state.
393 * \return The number of elements pushed on stack.
396 luaH_luakit_newindex(lua_State
*L
)
398 if(luaH_usemetatable(L
, 1, 2))
402 const gchar
*buf
= luaL_checklstring(L
, 2, &len
);
404 debug("Luakit newindex %s", buf
);
409 /* Add a global signal.
410 * Returns the number of elements pushed on stack.
412 * \lparam A string with the event name.
413 * \lparam The function to call.
416 luaH_luakit_add_signal(lua_State
*L
)
418 const gchar
*name
= luaL_checkstring(L
, 1);
419 luaH_checkfunction(L
, 2);
420 signal_add(globalconf
.signals
, name
, luaH_object_ref(L
, 2));
424 /* Remove a global signal.
425 * \param L The Lua VM state.
426 * \return The number of elements pushed on stack.
428 * \lparam A string with the event name.
429 * \lparam The function to call.
432 luaH_luakit_remove_signal(lua_State
*L
)
434 const gchar
*name
= luaL_checkstring(L
, 1);
435 luaH_checkfunction(L
, 2);
436 gpointer func
= (gpointer
) lua_topointer(L
, 2);
437 signal_remove(globalconf
.signals
, name
, func
);
438 luaH_object_unref(L
, (void *) func
);
442 /* Emit a global signal.
443 * \param L The Lua VM state.
444 * \return The number of elements pushed on stack.
446 * \lparam A string with the event name.
447 * \lparam The function to call.
450 luaH_luakit_emit_signal(lua_State
*L
)
452 signal_object_emit(L
, globalconf
.signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
457 luaH_panic(lua_State
*L
)
459 warn("unprotected error in call to Lua API (%s)", lua_tostring(L
, -1));
464 luaH_quit(lua_State
*L
)
472 luaH_dofunction_on_error(lua_State
*L
)
474 /* duplicate string error */
475 lua_pushvalue(L
, -1);
476 /* emit error signal */
477 signal_object_emit(L
, globalconf
.signals
, "debug::error", 1);
479 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
481 /* Move traceback before error */
483 /* Insert sentence */
484 lua_pushliteral(L
, "\nerror: ");
485 /* Move it before error */
497 static const struct luaL_reg luakit_lib
[] = {
498 { "quit", luaH_quit
},
499 { "add_signal", luaH_luakit_add_signal
},
500 { "remove_signal", luaH_luakit_remove_signal
},
501 { "emit_signal", luaH_luakit_emit_signal
},
502 { "__index", luaH_luakit_index
},
503 { "__newindex", luaH_luakit_newindex
},
508 L
= globalconf
.L
= luaL_newstate();
510 /* Set panic fuction */
511 lua_atpanic(L
, luaH_panic
);
513 /* Set error handling function */
514 lualib_dofunction_on_error
= luaH_dofunction_on_error
;
520 luaH_object_setup(L
);
522 /* Export luakit lib */
523 luaH_openlib(L
, "luakit", luakit_lib
, luakit_lib
);
526 widget_class_setup(L
);
528 /* add Lua search paths */
529 lua_getglobal(L
, "package");
530 if(LUA_TTABLE
!= lua_type(L
, 1)) {
531 warn("package is not a table");
534 lua_getfield(L
, 1, "path");
535 if(LUA_TSTRING
!= lua_type(L
, 2)) {
536 warn("package.path is not a string");
541 /* compile list of package search paths */
542 GPtrArray
*paths
= g_ptr_array_new_with_free_func(g_free
);
544 #if DEVELOPMENT_PATHS
545 /* allows for testing luakit in the project directory */
546 g_ptr_array_add(paths
, g_strdup("./lib"));
549 /* add users config dir (see: XDG_CONFIG_DIR) */
550 g_ptr_array_add(paths
, g_build_filename(globalconf
.config_dir
, "lib", NULL
));
552 /* add system config dirs (see: XDG_CONFIG_DIRS) */
553 const gchar
* const *config_dirs
= g_get_system_config_dirs();
554 for (; *config_dirs
; config_dirs
++)
555 g_ptr_array_add(paths
, g_build_filename(*config_dirs
, "luakit", "lib", NULL
));
557 /* add luakit install path */
558 g_ptr_array_add(paths
, g_build_filename(LUAKIT_INSTALL_PATH
, "lib", NULL
));
560 for (gpointer
*path
= paths
->pdata
; *path
; path
++) {
561 lua_pushliteral(L
, ";");
562 lua_pushstring(L
, *path
);
563 lua_pushliteral(L
, "/?.lua");
566 lua_pushliteral(L
, ";");
567 lua_pushstring(L
, *path
);
568 lua_pushliteral(L
, "/?/init.lua");
571 /* concat with package.path */
575 g_ptr_array_free(paths
, TRUE
);
577 /* package.path = "concatenated string" */
578 lua_setfield(L
, 1, "path");
582 luaH_loadrc(const gchar
*confpath
, gboolean run
)
584 debug("Loading rc: %s", confpath
);
585 lua_State
*L
= globalconf
.L
;
586 if(!luaL_loadfile(L
, confpath
)) {
588 if(lua_pcall(L
, 0, LUA_MULTRET
, 0)) {
589 g_fprintf(stderr
, "%s\n", lua_tostring(L
, -1));
596 g_fprintf(stderr
, "%s\n", lua_tostring(L
, -1));
600 /* Load a configuration file. */
602 luaH_parserc(const gchar
*confpath
, gboolean run
)
604 const gchar
* const *config_dirs
= NULL
;
605 gboolean ret
= FALSE
;
607 /* try to load, return if it's ok */
609 if(luaH_loadrc(confpath
, run
))
614 /* compile list of config search paths */
615 GPtrArray
*paths
= g_ptr_array_new_with_free_func(g_free
);
617 #if DEVELOPMENT_PATHS
618 /* allows for testing luakit in the project directory */
619 g_ptr_array_add(paths
, g_strdup("./rc.lua"));
622 /* search users config dir (see: XDG_CONFIG_HOME) */
623 g_ptr_array_add(paths
, g_build_filename(globalconf
.config_dir
, "rc.lua", NULL
));
625 /* search system config dirs (see: XDG_CONFIG_DIRS) */
626 config_dirs
= g_get_system_config_dirs();
627 for(; *config_dirs
; config_dirs
++)
628 g_ptr_array_add(paths
, g_build_filename(*config_dirs
, "luakit", "rc.lua", NULL
));
630 for (gpointer
*path
= paths
->pdata
; *path
; path
++) {
631 if (file_exists(*path
)) {
632 if(luaH_loadrc(*path
, run
)) {
633 globalconf
.confpath
= g_strdup(*path
);
643 if (paths
) g_ptr_array_free(paths
, TRUE
);
648 luaH_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
651 signal_object_emit(L
, globalconf
.signals
, "debug::index::miss", 2);
656 luaH_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
659 signal_object_emit(L
, globalconf
.signals
, "debug::newindex::miss", 3);
663 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80