awful.widget.taglist: use attached_add_signal
[awesome.git] / common / lualib.h
blobdce2d5d394b1251071678b725554374cfbe2a1eb
1 /*
2 * lualib.h - useful functions and type for Lua
4 * Copyright © 2009 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 #ifndef AWESOME_COMMON_LUALIB
23 #define AWESOME_COMMON_LUALIB
25 #include <lauxlib.h>
26 #include "common/util.h"
28 #define luaA_checkfunction(L, n) \
29 do { \
30 if(!lua_isfunction(L, n)) \
31 luaL_typerror(L, n, "function"); \
32 } while(0)
34 /** Dump the Lua stack. Useful for debugging.
35 * \param L The Lua VM state.
37 static inline void
38 luaA_dumpstack(lua_State *L)
40 fprintf(stderr, "-------- Lua stack dump ---------\n");
41 for(int i = lua_gettop(L); i; i--)
43 int t = lua_type(L, i);
44 switch (t)
46 case LUA_TSTRING:
47 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
48 break;
49 case LUA_TBOOLEAN:
50 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
51 break;
52 case LUA_TNUMBER:
53 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
54 break;
55 case LUA_TNIL:
56 fprintf(stderr, "%d: nil\n", i);
57 break;
58 default:
59 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
60 (int) lua_objlen(L, i),
61 lua_topointer(L, i));
62 break;
65 fprintf(stderr, "------- Lua stack dump end ------\n");
68 /** Convert s stack index to positive.
69 * \param L The Lua VM state.
70 * \param ud The index.
71 * \return A positive index.
73 static inline int
74 luaA_absindex(lua_State *L, int ud)
76 return (ud > 0 || ud <= LUA_REGISTRYINDEX) ? ud : lua_gettop(L) + ud + 1;
79 static inline int
80 luaA_dofunction_error(lua_State *L)
82 if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
84 /* Move traceback before error */
85 lua_insert(L, -2);
86 /* Insert sentence */
87 lua_pushliteral(L, "\nerror: ");
88 /* Move it before error */
89 lua_insert(L, -2);
90 lua_concat(L, 3);
92 return 1;
95 /** Execute an Lua function on top of the stack.
96 * \param L The Lua stack.
97 * \param nargs The number of arguments for the Lua function.
98 * \param nret The number of returned value from the Lua function.
99 * \return True on no error, false otherwise.
101 static inline bool
102 luaA_dofunction(lua_State *L, int nargs, int nret)
104 /* Move function before arguments */
105 lua_insert(L, - nargs - 1);
106 /* Push error handling function */
107 lua_pushcfunction(L, luaA_dofunction_error);
108 /* Move error handling function before args and function */
109 lua_insert(L, - nargs - 2);
110 int error_func_pos = lua_gettop(L) - nargs - 1;
111 if(lua_pcall(L, nargs, nret, - nargs - 2))
113 warn("%s", lua_tostring(L, -1));
114 /* Remove error function and error string */
115 lua_pop(L, 2);
116 return false;
118 /* Remove error function */
119 lua_remove(L, error_func_pos);
120 return true;
123 #define luaA_checktable(L, n) \
124 do { \
125 if(!lua_istable(L, n)) \
126 luaL_typerror(L, n, "table"); \
127 } while(0)
129 #endif
131 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80