NeoMutt  2025-09-05-70-gcfdde0
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
lua.c File Reference

Integrated Lua scripting. More...

#include "config.h"
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "lib.h"
#include "parse/lib.h"
#include "muttlib.h"
#include "version.h"
+ Include dependency graph for lua.c:

Go to the source code of this file.

Macros

#define LUA_COMPAT_ALL
 
#define LUA_COMPAT_5_1
 

Functions

static int lua_handle_panic (lua_State *l)
 Handle a panic in the Lua interpreter.
 
static int lua_handle_error (lua_State *l)
 Handle an error in the Lua interpreter.
 
static int lua_cb_global_call (lua_State *l)
 Call a NeoMutt command by name.
 
static int lua_cb_global_set (lua_State *l)
 Set a NeoMutt variable.
 
static int lua_cb_global_get (lua_State *l)
 Get a NeoMutt variable.
 
static int lua_cb_global_enter (lua_State *l)
 Execute NeoMutt config from Lua.
 
static int lua_cb_global_message (lua_State *l)
 Display a message in NeoMutt.
 
static int lua_cb_global_error (lua_State *l)
 Display an error in NeoMutt.
 
static void lua_expose_command (lua_State *l, const struct Command *cmd)
 Expose a NeoMutt command to the Lua interpreter.
 
static int lua_expose_commands (lua_State *l)
 Declare some NeoMutt types to the Lua interpreter.
 
static void lua_expose_mutt (lua_State *l)
 Expose a 'Mutt' object to the Lua interpreter.
 
bool lua_init_state (lua_State **l)
 Initialise a Lua State.
 

Variables

lua_State * LuaState = NULL
 Global Lua State.
 
static const luaL_Reg LuaMuttCommands []
 List of Lua commands to register.
 

Detailed Description

Integrated Lua scripting.

Authors
  • Bernard Pratz
  • Richard Russon
  • Victor Fernandes
  • Ian Zimmerman
  • Pietro Cerutti
  • Rayford Shireman

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file lua.c.

Macro Definition Documentation

◆ LUA_COMPAT_ALL

#define LUA_COMPAT_ALL

Definition at line 35 of file lua.c.

◆ LUA_COMPAT_5_1

#define LUA_COMPAT_5_1

Definition at line 38 of file lua.c.

Function Documentation

◆ lua_handle_panic()

static int lua_handle_panic ( lua_State * l)
static

Handle a panic in the Lua interpreter.

Parameters
lLua State
Return values
-1Always

Definition at line 64 of file lua.c.

65{
66 mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
67 mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
68 lua_pop(l, 1);
69 return -1;
70}
#define mutt_error(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:44
+ Here is the caller graph for this function:

◆ lua_handle_error()

static int lua_handle_error ( lua_State * l)
static

Handle an error in the Lua interpreter.

Parameters
lLua State
Return values
-1Always

Definition at line 77 of file lua.c.

78{
79 mutt_debug(LL_DEBUG1, "lua runtime error: %s\n", lua_tostring(l, -1));
80 mutt_error("Lua runtime error: %s", lua_tostring(l, -1));
81 lua_pop(l, 1);
82 return -1;
83}
+ Here is the caller graph for this function:

◆ lua_cb_global_call()

static int lua_cb_global_call ( lua_State * l)
static

Call a NeoMutt command by name.

Parameters
lLua State
Return values
>=0Success
-1Error

Definition at line 91 of file lua.c.

92{
93 mutt_debug(LL_DEBUG2, "enter\n");
94 struct Buffer *err = buf_pool_get();
95 struct Buffer *token = buf_pool_get();
96 struct Buffer *buf = buf_pool_get();
97 const struct Command *cmd = NULL;
98 int rc = 0;
99
100 if (lua_gettop(l) == 0)
101 {
102 luaL_error(l, "Error command argument required");
103 return -1;
104 }
105
106 cmd = commands_get(&NeoMutt->commands, lua_tostring(l, 1));
107 if (!cmd)
108 {
109 luaL_error(l, "Error command %s not found", lua_tostring(l, 1));
110 return -1;
111 }
112
113 for (int i = 2; i <= lua_gettop(l); i++)
114 {
115 buf_addstr(buf, lua_tostring(l, i));
116 buf_addch(buf, ' ');
117 }
118 buf_seek(buf, 0);
119
120 if (cmd->parse(token, buf, cmd->data, err))
121 {
122 luaL_error(l, "NeoMutt error: %s", buf_string(err));
123 rc = -1;
124 }
125 else
126 {
127 if (!lua_pushstring(l, buf_string(err)))
129 else
130 rc++;
131 }
132
133 buf_pool_release(&buf);
134 buf_pool_release(&token);
135 buf_pool_release(&err);
136 return rc;
137}
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition buffer.c:622
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const struct Command * commands_get(struct CommandArray *ca, const char *name)
Get a Command by its name.
Definition command.c:82
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:45
static int lua_handle_error(lua_State *l)
Handle an error in the Lua interpreter.
Definition lua.c:77
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:96
String manipulation buffer.
Definition buffer.h:36
enum CommandResult(* parse)(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Definition command.h:64
intptr_t data
Data or flags to pass to the command.
Definition command.h:66
Container for Accounts, Notifications.
Definition neomutt.h:42
struct CommandArray commands
NeoMutt commands.
Definition neomutt.h:50
+ Here is the call graph for this function:

◆ lua_cb_global_set()

static int lua_cb_global_set ( lua_State * l)
static

Set a NeoMutt variable.

Parameters
lLua State
Return values
0Success
-1Error

Definition at line 145 of file lua.c.

146{
147 const char *param = lua_tostring(l, -2);
148 mutt_debug(LL_DEBUG2, "%s\n", param);
149
150 struct Buffer *err = buf_pool_get();
151 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
152 if (!he)
153 {
154 // In case it is a my_var, we have to create it
155 if (mutt_str_startswith(param, "my_"))
156 {
157 struct ConfigDef my_cdef = { 0 };
158 my_cdef.name = param;
159 my_cdef.type = DT_MYVAR;
160 he = cs_create_variable(NeoMutt->sub->cs, &my_cdef, err);
161 if (!he)
162 return -1;
163 }
164 else
165 {
166 luaL_error(l, "NeoMutt parameter not found %s", param);
167 return -1;
168 }
169 }
170
171 struct ConfigDef *cdef = he->data;
172
173 int rc = 0;
174
175 switch (CONFIG_TYPE(cdef->type))
176 {
177 case DT_ADDRESS:
178 case DT_ENUM:
179 case DT_EXPANDO:
180 case DT_MBTABLE:
181 case DT_MYVAR:
182 case DT_PATH:
183 case DT_REGEX:
184 case DT_SLIST:
185 case DT_SORT:
186 case DT_STRING:
187 {
188 const char *value = lua_tostring(l, -1);
189 size_t val_size = lua_rawlen(l, -1);
190 struct Buffer *value_buf = buf_pool_get();
191 buf_strcpy_n(value_buf, value, val_size);
192 if (CONFIG_TYPE(he->type) == DT_PATH)
193 buf_expand_path(value_buf);
194
195 int rv = cs_subset_he_string_set(NeoMutt->sub, he, buf_string(value_buf), err);
196 buf_pool_release(&value_buf);
197 if (CSR_RESULT(rv) != CSR_SUCCESS)
198 rc = -1;
199 break;
200 }
201 case DT_LONG:
202 case DT_NUMBER:
203 case DT_QUAD:
204 {
205 const intptr_t value = lua_tointeger(l, -1);
206 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
207 if (CSR_RESULT(rv) != CSR_SUCCESS)
208 rc = -1;
209 break;
210 }
211 case DT_BOOL:
212 {
213 const intptr_t value = lua_toboolean(l, -1);
214 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
215 if (CSR_RESULT(rv) != CSR_SUCCESS)
216 rc = -1;
217 break;
218 }
219 default:
220 luaL_error(l, "Unsupported NeoMutt parameter type %d for %s",
221 CONFIG_TYPE(cdef->type), param);
222 rc = -1;
223 break;
224 }
225
226 buf_pool_release(&err);
227 return rc;
228}
size_t buf_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition buffer.c:416
struct HashElem * cs_create_variable(const struct ConfigSet *cs, struct ConfigDef *cdef, struct Buffer *err)
Create and register one config item.
Definition set.c:326
#define CSR_RESULT(x)
Definition set.h:50
#define CSR_SUCCESS
Action completed successfully.
Definition set.h:33
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:232
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition muttlib.c:314
const char * name
User-visible name.
Definition set.h:63
uint32_t type
Variable type, e.g. DT_STRING.
Definition set.h:64
struct ConfigSet * cs
Parent ConfigSet.
Definition subset.h:50
The item stored in a Hash Table.
Definition hash.h:44
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition hash.h:45
void * data
User-supplied data.
Definition hash.h:47
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:46
int cs_subset_he_native_set(const struct ConfigSubset *sub, struct HashElem *he, intptr_t value, struct Buffer *err)
Natively set the value of a HashElem config item.
Definition subset.c:281
int cs_subset_he_string_set(const struct ConfigSubset *sub, struct HashElem *he, const char *value, struct Buffer *err)
Set a config item by string.
Definition subset.c:370
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition subset.c:193
#define CONFIG_TYPE(t)
Definition types.h:49
@ DT_NUMBER
a number
Definition types.h:38
@ DT_SLIST
a list of strings
Definition types.h:42
@ DT_BOOL
boolean option
Definition types.h:32
@ DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition types.h:40
@ DT_STRING
a string
Definition types.h:44
@ DT_SORT
sorting methods
Definition types.h:43
@ DT_MYVAR
a user-defined variable (my_foo)
Definition types.h:37
@ DT_MBTABLE
multibyte char table
Definition types.h:36
@ DT_ADDRESS
e-mail address
Definition types.h:31
@ DT_LONG
a number (long)
Definition types.h:35
@ DT_EXPANDO
an expando
Definition types.h:34
@ DT_ENUM
an enumeration
Definition types.h:33
@ DT_REGEX
regular expressions
Definition types.h:41
@ DT_PATH
a path to a file/directory
Definition types.h:39
+ Here is the call graph for this function:

◆ lua_cb_global_get()

static int lua_cb_global_get ( lua_State * l)
static

Get a NeoMutt variable.

Parameters
lLua State
Return values
1Success
-1Error

Definition at line 236 of file lua.c.

237{
238 const char *param = lua_tostring(l, -1);
239 mutt_debug(LL_DEBUG2, "%s\n", param);
240
241 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
242 if (!he)
243 {
244 mutt_debug(LL_DEBUG2, "error\n");
245 luaL_error(l, "NeoMutt parameter not found %s", param);
246 return -1;
247 }
248
249 struct ConfigDef *cdef = he->data;
250
251 switch (CONFIG_TYPE(cdef->type))
252 {
253 case DT_ADDRESS:
254 case DT_ENUM:
255 case DT_EXPANDO:
256 case DT_MBTABLE:
257 case DT_MYVAR:
258 case DT_PATH:
259 case DT_REGEX:
260 case DT_SLIST:
261 case DT_SORT:
262 case DT_STRING:
263 {
264 struct Buffer *value = buf_pool_get();
265 int rc = cs_subset_he_string_get(NeoMutt->sub, he, value);
266 if (CSR_RESULT(rc) != CSR_SUCCESS)
267 {
268 buf_pool_release(&value);
269 return -1;
270 }
271
272 struct Buffer *escaped = buf_pool_get();
273 escape_string(escaped, buf_string(value));
274 lua_pushstring(l, buf_string(escaped));
275 buf_pool_release(&value);
276 buf_pool_release(&escaped);
277 return 1;
278 }
279 case DT_QUAD:
280 lua_pushinteger(l, (unsigned char) cdef->var);
281 return 1;
282 case DT_LONG:
283 lua_pushinteger(l, (signed long) cdef->var);
284 return 1;
285 case DT_NUMBER:
286 lua_pushinteger(l, (signed short) cdef->var);
287 return 1;
288 case DT_BOOL:
289 lua_pushboolean(l, (bool) cdef->var);
290 return 1;
291 default:
292 luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
293 return -1;
294 }
295}
size_t escape_string(struct Buffer *buf, const char *src)
Write a string to a buffer, escaping special characters.
Definition dump.c:48
intptr_t var
Storage for the variable.
Definition set.h:82
int cs_subset_he_string_get(const struct ConfigSubset *sub, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition subset.c:338
+ Here is the call graph for this function:

◆ lua_cb_global_enter()

static int lua_cb_global_enter ( lua_State * l)
static

Execute NeoMutt config from Lua.

Parameters
lLua State
Return values
>=0Success
-1Error

Definition at line 303 of file lua.c.

304{
305 mutt_debug(LL_DEBUG2, "enter\n");
306 struct Buffer *err = buf_pool_get();
307 char *buf = mutt_str_dup(lua_tostring(l, -1));
308 int rc = 0;
309
310 if (parse_rc_line(buf, err))
311 {
312 luaL_error(l, "NeoMutt error: %s", buf_string(err));
313 rc = -1;
314 }
315 else
316 {
317 if (!lua_pushstring(l, buf_string(err)))
319 else
320 rc++;
321 }
322
323 FREE(&buf);
324 buf_pool_release(&err);
325
326 return rc;
327}
#define FREE(x)
Definition memory.h:62
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:255
enum CommandResult parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition rc.c:109
+ Here is the call graph for this function:

◆ lua_cb_global_message()

static int lua_cb_global_message ( lua_State * l)
static

Display a message in NeoMutt.

Parameters
lLua State
Return values
0Always

Definition at line 334 of file lua.c.

335{
336 mutt_debug(LL_DEBUG2, "enter\n");
337 const char *msg = lua_tostring(l, -1);
338 if (msg)
339 mutt_message("%s", msg);
340 return 0;
341}
#define mutt_message(...)
Definition logging2.h:92

◆ lua_cb_global_error()

static int lua_cb_global_error ( lua_State * l)
static

Display an error in NeoMutt.

Parameters
lLua State
Return values
0Always

Definition at line 348 of file lua.c.

349{
350 mutt_debug(LL_DEBUG2, "enter\n");
351 const char *msg = lua_tostring(l, -1);
352 if (msg)
353 mutt_error("%s", msg);
354 return 0;
355}

◆ lua_expose_command()

static void lua_expose_command ( lua_State * l,
const struct Command * cmd )
static

Expose a NeoMutt command to the Lua interpreter.

Parameters
lLua state
cmdNeoMutt Command

Definition at line 362 of file lua.c.

363{
364 char buf[1024] = { 0 };
365 snprintf(buf, sizeof(buf), "mutt.command.%s = function (...); mutt.call('%s', ...); end",
366 cmd->name, cmd->name);
367 (void) luaL_dostring(l, buf);
368}
const char * name
Name of the command.
Definition command.h:51
+ Here is the caller graph for this function:

◆ lua_expose_commands()

static int lua_expose_commands ( lua_State * l)
static

Declare some NeoMutt types to the Lua interpreter.

Parameters
lLua State
Return values
1Always

Definition at line 397 of file lua.c.

398{
399 mutt_debug(LL_DEBUG2, "enter\n");
400 luaL_newlib(l, LuaMuttCommands);
401 int lib_idx = lua_gettop(l);
402
403 // clang-format off
404 lua_pushstring(l, "VERSION"); lua_pushstring(l, mutt_make_version()); lua_settable(l, lib_idx);
405 lua_pushstring(l, "QUAD_YES"); lua_pushinteger(l, MUTT_YES); lua_settable(l, lib_idx);
406 lua_pushstring(l, "QUAD_NO"); lua_pushinteger(l, MUTT_NO); lua_settable(l, lib_idx);
407 lua_pushstring(l, "QUAD_ASKYES"); lua_pushinteger(l, MUTT_ASKYES); lua_settable(l, lib_idx);
408 lua_pushstring(l, "QUAD_ASKNO"); lua_pushinteger(l, MUTT_ASKNO); lua_settable(l, lib_idx);
409 // clang-format on
410
411 return 1;
412}
static const luaL_Reg LuaMuttCommands[]
List of Lua commands to register.
Definition lua.c:379
@ MUTT_ASKNO
Ask the user, defaulting to 'No'.
Definition quad.h:40
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition quad.h:38
@ MUTT_ASKYES
Ask the user, defaulting to 'Yes'.
Definition quad.h:41
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition version.c:295
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lua_expose_mutt()

static void lua_expose_mutt ( lua_State * l)
static

Expose a 'Mutt' object to the Lua interpreter.

Parameters
lLua State

Definition at line 418 of file lua.c.

419{
420 luaL_requiref(l, "mutt", lua_expose_commands, 1);
421 (void) luaL_dostring(l, "mutt.command = {}");
422
423 const struct Command **cp = NULL;
425 {
426 const struct Command *cmd = *cp;
427
428 lua_expose_command(l, cmd);
429 }
430}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:214
static int lua_expose_commands(lua_State *l)
Declare some NeoMutt types to the Lua interpreter.
Definition lua.c:397
static void lua_expose_command(lua_State *l, const struct Command *cmd)
Expose a NeoMutt command to the Lua interpreter.
Definition lua.c:362
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lua_init_state()

bool lua_init_state ( lua_State ** l)

Initialise a Lua State.

Parameters
[out]lLua State
Return values
trueSuccessful

Definition at line 437 of file lua.c.

438{
439 if (!l)
440 return false;
441 if (*l)
442 return true;
443
444 mutt_debug(LL_DEBUG2, "enter\n");
445 *l = luaL_newstate();
446
447 if (!*l)
448 {
449 mutt_error(_("Error: Couldn't load the lua interpreter"));
450 return false;
451 }
452
453 lua_atpanic(*l, lua_handle_panic);
454
455 /* load various Lua libraries */
456 luaL_openlibs(*l);
457 lua_expose_mutt(*l);
458
459 return true;
460}
static void lua_expose_mutt(lua_State *l)
Expose a 'Mutt' object to the Lua interpreter.
Definition lua.c:418
static int lua_handle_panic(lua_State *l)
Handle a panic in the Lua interpreter.
Definition lua.c:64
#define _(a)
Definition message.h:28
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ LuaState

lua_State* LuaState = NULL

Global Lua State.

Definition at line 57 of file lua.c.

◆ LuaMuttCommands

const luaL_Reg LuaMuttCommands[]
static
Initial value:
= {
{ "set", lua_cb_global_set },
{ "get", lua_cb_global_get },
{ "call", lua_cb_global_call },
{ "enter", lua_cb_global_enter },
{ "print", lua_cb_global_message },
{ "message", lua_cb_global_message },
{ "error", lua_cb_global_error },
{ NULL, NULL },
}
static int lua_cb_global_set(lua_State *l)
Set a NeoMutt variable.
Definition lua.c:145
static int lua_cb_global_message(lua_State *l)
Display a message in NeoMutt.
Definition lua.c:334
static int lua_cb_global_error(lua_State *l)
Display an error in NeoMutt.
Definition lua.c:348
static int lua_cb_global_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition lua.c:303
static int lua_cb_global_get(lua_State *l)
Get a NeoMutt variable.
Definition lua.c:236
static int lua_cb_global_call(lua_State *l)
Call a NeoMutt command by name.
Definition lua.c:91

List of Lua commands to register.

In NeoMutt, run:

‘:lua mutt.message('hello’)`

and it will call lua_cb_global_message()

Definition at line 379 of file lua.c.

379 {
380 // clang-format off
381 { "set", lua_cb_global_set },
382 { "get", lua_cb_global_get },
383 { "call", lua_cb_global_call },
384 { "enter", lua_cb_global_enter },
385 { "print", lua_cb_global_message },
386 { "message", lua_cb_global_message },
387 { "error", lua_cb_global_error },
388 { NULL, NULL },
389 // clang-format on
390};