Use `get_settings_object(..)` to get correct settings GObject
[luakit.git] / luah.h
blobd4adf08e6bfa03afb13854c11060e6b2b440b48c
1 /*
2 * luah.h - 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/>.
22 #ifndef LUAKIT_LUA_H
23 #define LUAKIT_LUA_H
25 #include <lua.h>
26 #include <basedir.h>
27 #include "globalconf.h"
28 #include "common/luaobject.h"
29 #include "common/lualib.h"
31 #define luaH_deprecate(L, repl) \
32 do { \
33 luaH_warn(L, "%s: This function is deprecated and will be removed, see %s", \
34 __FUNCTION__, repl); \
35 lua_pushlstring(L, __FUNCTION__, sizeof(__FUNCTION__)); \
36 signal_object_emit(L, &globalconf.signals, "debug::deprecation", 1); \
37 } while(0)
39 static inline gboolean
40 luaH_checkboolean(lua_State *L, gint n) {
41 if(!lua_isboolean(L, n))
42 luaL_typerror(L, n, "boolean");
43 return lua_toboolean(L, n);
46 static inline gboolean
47 luaH_optboolean(lua_State *L, gint idx, gboolean def) {
48 return luaL_opt(L, luaH_checkboolean, idx, def);
51 static inline lua_Number
52 luaH_getopt_number(lua_State *L, gint idx, const gchar *name, lua_Number def) {
53 lua_getfield(L, idx, name);
54 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
55 def = luaL_optnumber(L, -1, def);
56 lua_pop(L, 1);
57 return def;
60 static inline const gchar *
61 luaH_getopt_lstring(lua_State *L, gint idx, const gchar *name, const gchar *def, size_t *len) {
62 lua_getfield(L, idx, name);
63 const gchar *s = luaL_optlstring(L, -1, def, len);
64 lua_pop(L, 1);
65 return s;
68 static inline gboolean
69 luaH_getopt_boolean(lua_State *L, gint idx, const gchar *name, gboolean def) {
70 lua_getfield(L, idx, name);
71 gboolean b = luaH_optboolean(L, -1, def);
72 lua_pop(L, 1);
73 return b;
76 /* Register an Lua object.
77 * \param L The Lua stack.
78 * \param idx Index of the object in the stack.
79 * \param ref A gint address: it will be filled with the gint
80 * registered. If the address points to an already registered object, it will
81 * be unregistered.
82 * \return Always 0.
84 static inline gint
85 luaH_register(lua_State *L, gint idx, gint *ref) {
86 lua_pushvalue(L, idx);
87 if(*ref != LUA_REFNIL)
88 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
89 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
90 return 0;
93 /* Unregister a Lua object.
94 * \param L The Lua stack.
95 * \param ref A reference to an Lua object.
97 static inline void
98 luaH_unregister(lua_State *L, gint *ref) {
99 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
100 *ref = LUA_REFNIL;
103 /* Register a function.
104 * \param L The Lua stack.
105 * \param idx Index of the function in the stack.
106 * \param fct A gint address: it will be filled with the gint
107 * registered. If the address points to an already registered function, it will
108 * be unregistered.
109 * \return luaH_register value.
111 static inline gint
112 luaH_registerfct(lua_State *L, gint idx, gint *fct) {
113 luaH_checkfunction(L, idx);
114 return luaH_register(L, idx, fct);
117 /* Grab a function from the registry and execute it.
118 * \param L The Lua stack.
119 * \param ref The function reference.
120 * \param nargs The number of arguments for the Lua function.
121 * \param nret The number of returned value from the Lua function.
122 * \return True on no error, false otherwise.
124 static inline gboolean
125 luaH_dofunction_from_registry(lua_State *L, gint ref, gint nargs, gint nret) {
126 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
127 return luaH_dofunction(L, nargs, nret);
130 /* Print a warning about some Lua code.
131 * This is less mean than luaL_error() which setjmp via lua_error() and kills
132 * everything. This only warn, it's up to you to then do what's should be done.
133 * \param L The Lua VM state.
134 * \param fmt The warning message.
136 static inline void __attribute__ ((format(printf, 2, 3)))
137 luaH_warn(lua_State *L, const gchar *fmt, ...) {
138 va_list ap;
139 luaL_where(L, 1);
140 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
141 lua_pop(L, 1);
142 va_start(ap, fmt);
143 vfprintf(stderr, fmt, ap);
144 va_end(ap);
145 fprintf(stderr, "\n");
148 void luaH_init(xdgHandle *);
149 gboolean luaH_parserc(xdgHandle *, const gchar *, gboolean);
150 gboolean luaH_hasitem(lua_State *, gconstpointer);
151 gint luaH_next(lua_State *, gint);
152 gboolean luaH_isloop(lua_State *, gint);
154 gint luaH_class_index_miss_property(lua_State *, lua_object_t *);
155 gint luaH_class_newindex_miss_property(lua_State *, lua_object_t *);
156 void luaH_modifier_table_push(lua_State *, guint);
157 void luaH_keystr_push(lua_State *, guint);
159 #endif
160 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80