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/>.
23 #include <basedir_fs.h>
25 #include "common/util.h"
26 #include "common/lualib.h"
32 luaH_modifier_table_push(lua_State
*L
, guint state
) {
35 if (state
& GDK_MODIFIER_MASK
) {
37 #define MODKEY(key, name) \
38 if (state & GDK_##key##_MASK) { \
39 lua_pushstring(L, name); \
40 lua_rawseti(L, -2, i++); \
43 MODKEY(SHIFT
, "Shift");
45 MODKEY(CONTROL
, "Control");
58 luaH_keystr_push(lua_State
*L
, guint keyval
)
62 guint32 ukval
= gdk_keyval_to_unicode(keyval
);
64 /* check for printable unicode character */
65 if (g_unichar_isgraph(ukval
)) {
66 ulen
= g_unichar_to_utf8(ukval
, ucs
);
68 lua_pushstring(L
, ucs
);
70 /* sent keysym for non-printable characters */
72 lua_pushstring(L
, gdk_keyval_name(keyval
));
75 /* UTF-8 aware string length computing.
76 * Returns the number of elements pushed on the stack. */
78 luaH_utf8_strlen(lua_State
*L
)
80 const gchar
*cmd
= luaL_checkstring(L
, 1);
81 lua_pushnumber(L
, (ssize_t
) g_utf8_strlen(NONULL(cmd
), -1));
85 /* Overload standard Lua next function to use __next key on metatable.
86 * Returns the number of elements pushed on stack. */
88 luaHe_next(lua_State
*L
)
90 if(luaL_getmetafield(L
, 1, "__next")) {
92 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
95 luaL_checktype(L
, 1, LUA_TTABLE
);
103 /* Overload lua_next() function by using __next metatable field to get
104 * next elements. `idx` is the index number of elements in stack.
105 * Returns 1 if more elements to come, 0 otherwise. */
107 luaH_next(lua_State
*L
, gint idx
)
109 if(luaL_getmetafield(L
, idx
, "__next")) {
110 /* if idx is relative, reduce it since we got __next */
112 /* copy table and then move key */
113 lua_pushvalue(L
, idx
);
114 lua_pushvalue(L
, -3);
116 lua_pcall(L
, 2, 2, 0);
117 /* next returned nil, it's the end */
118 if(lua_isnil(L
, -1)) {
125 else if(lua_istable(L
, idx
))
126 return lua_next(L
, idx
);
132 /* Generic pairs function.
133 * Returns the number of elements pushed on stack. */
135 luaH_generic_pairs(lua_State
*L
)
137 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
138 lua_pushvalue(L
, 1); /* state, */
139 lua_pushnil(L
); /* and initial value */
143 /* Overload standard pairs function to use __pairs field of metatables.
144 * Returns the number of elements pushed on stack. */
146 luaHe_pairs(lua_State
*L
)
148 if(luaL_getmetafield(L
, 1, "__pairs")) {
150 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
151 return lua_gettop(L
);
153 luaL_checktype(L
, 1, LUA_TTABLE
);
154 return luaH_generic_pairs(L
);
158 luaH_ipairs_aux(lua_State
*L
)
160 gint i
= luaL_checkint(L
, 2) + 1;
161 luaL_checktype(L
, 1, LUA_TTABLE
);
162 lua_pushinteger(L
, i
);
163 lua_rawgeti(L
, 1, i
);
164 return (lua_isnil(L
, -1)) ? 0 : 2;
167 /* Overload standard ipairs function to use __ipairs field of metatables.
168 * Returns the number of elements pushed on stack. */
170 luaHe_ipairs(lua_State
*L
)
172 if(luaL_getmetafield(L
, 1, "__ipairs")) {
174 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
175 return lua_gettop(L
);
178 luaL_checktype(L
, 1, LUA_TTABLE
);
179 lua_pushvalue(L
, lua_upvalueindex(1));
181 lua_pushinteger(L
, 0); /* and initial value */
185 /* Enhanced type() function which recognize luakit objects.
186 * \param L The Lua VM state.
187 * \return The number of arguments pushed on the stack.
190 luaHe_type(lua_State
*L
)
193 lua_pushstring(L
, luaH_typename(L
, 1));
198 /* Fix up and add handy standard lib functions */
200 luaH_fixups(lua_State
*L
)
202 /* export string.wlen */
203 lua_getglobal(L
, "string");
204 lua_pushcfunction(L
, &luaH_utf8_strlen
);
205 lua_setfield(L
, -2, "wlen");
208 lua_pushliteral(L
, "next");
209 lua_pushcfunction(L
, luaHe_next
);
210 lua_settable(L
, LUA_GLOBALSINDEX
);
212 lua_pushliteral(L
, "pairs");
213 lua_pushcfunction(L
, luaHe_next
);
214 lua_pushcclosure(L
, luaHe_pairs
, 1); /* pairs get next as upvalue */
215 lua_settable(L
, LUA_GLOBALSINDEX
);
217 lua_pushliteral(L
, "ipairs");
218 lua_pushcfunction(L
, luaH_ipairs_aux
);
219 lua_pushcclosure(L
, luaHe_ipairs
, 1);
220 lua_settable(L
, LUA_GLOBALSINDEX
);
222 lua_pushliteral(L
, "type");
223 lua_pushcfunction(L
, luaHe_type
);
224 lua_settable(L
, LUA_GLOBALSINDEX
);
227 /* Look for an item: table, function, etc.
228 * \param L The Lua VM state.
229 * \param item The pointer item.
232 luaH_hasitem(lua_State
*L
, gconstpointer item
)
235 while(luaH_next(L
, -2)) {
236 if(lua_topointer(L
, -1) == item
) {
237 /* remove value and key */
241 if(lua_istable(L
, -1))
242 if(luaH_hasitem(L
, item
)) {
243 /* remove key and value */
253 /* Browse a table pushed on top of the index, and put all its table and
254 * sub-table ginto an array.
255 * \param L The Lua VM state.
256 * \param elems The elements array.
257 * \return False if we encounter an elements already in list.
260 luaH_isloop_check(lua_State
*L
, GPtrArray
*elems
)
262 if(lua_istable(L
, -1)) {
263 gconstpointer object
= lua_topointer(L
, -1);
265 /* Check that the object table is not already in the list */
266 for(guint i
= 0; i
< elems
->len
; i
++)
267 if(elems
->pdata
[i
] == object
)
270 /* push the table in the elements list */
271 g_ptr_array_add(elems
, (gpointer
) object
);
273 /* look every object in the "table" */
275 while(luaH_next(L
, -2)) {
276 if(!luaH_isloop_check(L
, elems
)) {
277 /* remove key and value */
281 /* remove value, keep key for next iteration */
288 /* Check if a table is a loop. When using tables as direct acyclic digram,
290 * \param L The Lua VM state.
291 * \param idx The index of the table in the stack
292 * \return True if the table loops.
295 luaH_isloop(lua_State
*L
, gint idx
)
297 /* elems is an elements array that we will fill with all array we
298 * encounter while browsing the tables */
299 GPtrArray
*elems
= g_ptr_array_new();
301 /* push table on top */
302 lua_pushvalue(L
, idx
);
304 gboolean ret
= luaH_isloop_check(L
, elems
);
306 /* remove pushed table */
309 g_ptr_array_free(elems
, TRUE
);
314 /* Returns a string from X selection.
315 * \param L The Lua VM state.
316 * \return The number of elements pushed on stack (1).
319 luaH_luakit_selection(lua_State
*L
)
321 int n
= lua_gettop(L
);
322 GdkAtom atom
= GDK_SELECTION_PRIMARY
;
325 const gchar
*arg
= luaL_checkstring(L
, 1);
326 /* Follow xclip(1) behavior: check only the first character of argument */
331 atom
= GDK_SELECTION_SECONDARY
;
334 atom
= GDK_SELECTION_CLIPBOARD
;
337 luaL_argerror(L
, 1, "should be 'primary', 'secondary' or 'clipboard'");
341 GtkClipboard
*selection
= gtk_clipboard_get(atom
);
342 gchar
*text
= gtk_clipboard_wait_for_text(selection
);
343 lua_pushstring(L
, text
);
348 /* luakit global table.
349 * \param L The Lua VM state.
350 * \return The number of elements pushed on stack.
352 * \lfield font The default font.
353 * \lfield font_height The default font height.
354 * \lfield conffile The configuration file which has been loaded.
357 luaH_luakit_index(lua_State
*L
)
359 if(luaH_usemetatable(L
, 1, 2))
364 const gchar
*prop
= luaL_checklstring(L
, 2, &len
);
365 luakit_token_t token
= l_tokenize(prop
, len
);
371 for (guint i
= 0; i
< globalconf
.windows
->len
; i
++) {
372 w
= globalconf
.windows
->pdata
[i
];
373 luaH_object_push(L
, w
->ref
);
374 lua_rawseti(L
, -2, i
+1);
379 lua_pushcfunction(L
, luaH_luakit_selection
);
382 case L_TK_INSTALL_PATH
:
383 lua_pushstring(L
, LUAKIT_INSTALL_PATH
);
392 /* Newindex function for the luakit global table.
393 * \param L The Lua VM state.
394 * \return The number of elements pushed on stack.
397 luaH_luakit_newindex(lua_State
*L
)
399 if(luaH_usemetatable(L
, 1, 2))
403 const gchar
*buf
= luaL_checklstring(L
, 2, &len
);
405 debug("Luakit newindex %s", buf
);
410 /* Add a global signal.
411 * Returns the number of elements pushed on stack.
413 * \lparam A string with the event name.
414 * \lparam The function to call.
417 luaH_luakit_add_signal(lua_State
*L
)
419 const gchar
*name
= luaL_checkstring(L
, 1);
420 luaH_checkfunction(L
, 2);
421 signal_add(globalconf
.signals
, name
, luaH_object_ref(L
, 2));
425 /* Remove a global signal.
426 * \param L The Lua VM state.
427 * \return The number of elements pushed on stack.
429 * \lparam A string with the event name.
430 * \lparam The function to call.
433 luaH_luakit_remove_signal(lua_State
*L
)
435 const gchar
*name
= luaL_checkstring(L
, 1);
436 luaH_checkfunction(L
, 2);
437 gpointer func
= (gpointer
) lua_topointer(L
, 2);
438 signal_remove(globalconf
.signals
, name
, func
);
439 luaH_object_unref(L
, (void *) func
);
443 /* Emit a global signal.
444 * \param L The Lua VM state.
445 * \return The number of elements pushed on stack.
447 * \lparam A string with the event name.
448 * \lparam The function to call.
451 luaH_luakit_emit_signal(lua_State
*L
)
453 signal_object_emit(L
, globalconf
.signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
458 luaH_panic(lua_State
*L
)
460 warn("unprotected error in call to Lua API (%s)", lua_tostring(L
, -1));
465 luaH_quit(lua_State
*L
)
473 luaH_dofunction_on_error(lua_State
*L
)
475 /* duplicate string error */
476 lua_pushvalue(L
, -1);
477 /* emit error signal */
478 signal_object_emit(L
, globalconf
.signals
, "debug::error", 1);
480 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
482 /* Move traceback before error */
484 /* Insert sentence */
485 lua_pushliteral(L
, "\nerror: ");
486 /* Move it before error */
494 luaH_init(xdgHandle
*xdg
)
498 static const struct luaL_reg luakit_lib
[] = {
499 { "quit", luaH_quit
},
500 { "add_signal", luaH_luakit_add_signal
},
501 { "remove_signal", luaH_luakit_remove_signal
},
502 { "emit_signal", luaH_luakit_emit_signal
},
503 { "__index", luaH_luakit_index
},
504 { "__newindex", luaH_luakit_newindex
},
509 L
= globalconf
.L
= luaL_newstate();
511 /* Set panic fuction */
512 lua_atpanic(L
, luaH_panic
);
514 /* Set error handling function */
515 lualib_dofunction_on_error
= luaH_dofunction_on_error
;
521 luaH_object_setup(L
);
523 /* Export luakit lib */
524 luaH_openlib(L
, "luakit", luakit_lib
, luakit_lib
);
527 widget_class_setup(L
);
529 /* add Lua search paths */
530 lua_getglobal(L
, "package");
531 if(LUA_TTABLE
!= lua_type(L
, 1)) {
532 warn("package is not a table");
535 lua_getfield(L
, 1, "path");
536 if(LUA_TSTRING
!= lua_type(L
, 2)) {
537 warn("package.path is not a string");
542 /* allows for testing luakit in the project directory
543 * (i.e. `./luakit -c rc.lua ...` */
544 lua_pushliteral(L
, ";./lib/?.lua;");
545 lua_pushliteral(L
, ";./lib/?/init.lua");
548 /* add XDG_CONFIG_DIR as an include path */
549 const gchar
* const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
550 for(; *xdgconfigdirs
; xdgconfigdirs
++)
552 size_t len
= l_strlen(*xdgconfigdirs
);
553 lua_pushliteral(L
, ";");
554 lua_pushlstring(L
, *xdgconfigdirs
, len
);
555 lua_pushliteral(L
, "/luakit/?.lua");
558 lua_pushliteral(L
, ";");
559 lua_pushlstring(L
, *xdgconfigdirs
, len
);
560 lua_pushliteral(L
, "/luakit/?/init.lua");
563 lua_concat(L
, 3); /* concatenate with package.path */
566 /* add Lua lib path (/usr/share/luakit/lib by default) */
567 lua_pushliteral(L
, ";" LUAKIT_INSTALL_PATH
"/lib/?.lua");
568 lua_pushliteral(L
, ";" LUAKIT_INSTALL_PATH
"/lib/?/init.lua");
569 lua_concat(L
, 3); /* concatenate with package.path */
570 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
574 luaH_loadrc(const gchar
*confpath
, gboolean run
)
576 lua_State
*L
= globalconf
.L
;
578 if(!luaL_loadfile(L
, confpath
)) {
580 if(lua_pcall(L
, 0, LUA_MULTRET
, 0)) {
581 g_fprintf(stderr
, "%s\n", lua_tostring(L
, -1));
588 g_fprintf(stderr
, "%s\n", lua_tostring(L
, -1));
592 /* Load a configuration file.
594 * param xdg An xdg handle to use to get XDG basedir.
595 * param confpatharg The configuration file to load.
596 * param run Run the configuration file.
599 luaH_parserc(xdgHandle
* xdg
, const gchar
*confpatharg
, gboolean run
)
601 gchar
*confpath
= NULL
;
602 gboolean ret
= FALSE
;
604 /* try to load, return if it's ok */
606 debug("Attempting to load rc file: %s", confpatharg
);
607 if(luaH_loadrc(confpatharg
, run
))
611 confpath
= xdgConfigFind("luakit/rc.lua", xdg
);
612 gchar
*tmp
= confpath
;
614 /* confpath is "string1\0string2\0string3\0\0" */
616 debug("Loading rc file: %s", tmp
);
617 if(luaH_loadrc(tmp
, run
)) {
618 globalconf
.confpath
= g_strdup(tmp
);
623 tmp
+= l_strlen(tmp
) + 1;
628 if (confpath
) g_free(confpath
);
633 luaH_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
636 signal_object_emit(L
, globalconf
.signals
, "debug::index::miss", 2);
641 luaH_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
644 signal_object_emit(L
, globalconf
.signals
, "debug::newindex::miss", 3);
648 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80