NeoMutt  2025-12-11-58-g09398d
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 "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 63 of file lua.c.

64{
65 mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
66 mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
67 lua_pop(l, 1);
68 return -1;
69}
#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 76 of file lua.c.

77{
78 mutt_debug(LL_DEBUG1, "lua runtime error: %s\n", lua_tostring(l, -1));
79 mutt_error("Lua runtime error: %s", lua_tostring(l, -1));
80 lua_pop(l, 1);
81 return -1;
82}
+ 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 90 of file lua.c.

91{
92 mutt_debug(LL_DEBUG2, "enter\n");
93 struct Buffer *err = buf_pool_get();
94 struct Buffer *buf = buf_pool_get();
95 const struct Command *cmd = NULL;
96 int rc = 0;
97
98 if (lua_gettop(l) == 0)
99 {
100 luaL_error(l, "Error command argument required");
101 return -1;
102 }
103
104 cmd = commands_get(&NeoMutt->commands, lua_tostring(l, 1));
105 if (!cmd)
106 {
107 luaL_error(l, "Error command %s not found", lua_tostring(l, 1));
108 return -1;
109 }
110
111 for (int i = 2; i <= lua_gettop(l); i++)
112 {
113 buf_addstr(buf, lua_tostring(l, i));
114 buf_addch(buf, ' ');
115 }
116 buf_seek(buf, 0);
117
118 if (cmd->parse(cmd, buf, err))
119 {
120 luaL_error(l, "NeoMutt error: %s", buf_string(err));
121 rc = -1;
122 }
123 else
124 {
125 if (!lua_pushstring(l, buf_string(err)))
127 else
128 rc++;
129 }
130
131 buf_pool_release(&buf);
132 buf_pool_release(&err);
133 return rc;
134}
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:76
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)(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
Definition command.h:75
Container for Accounts, Notifications.
Definition neomutt.h:43
struct CommandArray commands
NeoMutt commands.
Definition neomutt.h:51
+ 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 142 of file lua.c.

143{
144 const char *param = lua_tostring(l, -2);
145 mutt_debug(LL_DEBUG2, "%s\n", param);
146
147 struct Buffer *err = buf_pool_get();
148 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
149 if (!he)
150 {
151 // In case it is a my_var, we have to create it
152 if (mutt_str_startswith(param, "my_"))
153 {
154 struct ConfigDef my_cdef = { 0 };
155 my_cdef.name = param;
156 my_cdef.type = DT_MYVAR;
157 he = cs_create_variable(NeoMutt->sub->cs, &my_cdef, err);
158 if (!he)
159 return -1;
160 }
161 else
162 {
163 luaL_error(l, "NeoMutt parameter not found %s", param);
164 return -1;
165 }
166 }
167
168 struct ConfigDef *cdef = he->data;
169
170 int rc = 0;
171
172 switch (CONFIG_TYPE(cdef->type))
173 {
174 case DT_ADDRESS:
175 case DT_ENUM:
176 case DT_EXPANDO:
177 case DT_MBTABLE:
178 case DT_MYVAR:
179 case DT_PATH:
180 case DT_REGEX:
181 case DT_SLIST:
182 case DT_SORT:
183 case DT_STRING:
184 {
185 const char *value = lua_tostring(l, -1);
186 size_t val_size = lua_rawlen(l, -1);
187 struct Buffer *value_buf = buf_pool_get();
188 buf_strcpy_n(value_buf, value, val_size);
189 if (CONFIG_TYPE(he->type) == DT_PATH)
190 buf_expand_path(value_buf);
191
192 int rv = cs_subset_he_string_set(NeoMutt->sub, he, buf_string(value_buf), err);
193 buf_pool_release(&value_buf);
194 if (CSR_RESULT(rv) != CSR_SUCCESS)
195 rc = -1;
196 break;
197 }
198 case DT_LONG:
199 case DT_NUMBER:
200 case DT_QUAD:
201 {
202 const intptr_t value = lua_tointeger(l, -1);
203 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
204 if (CSR_RESULT(rv) != CSR_SUCCESS)
205 rc = -1;
206 break;
207 }
208 case DT_BOOL:
209 {
210 const intptr_t value = lua_toboolean(l, -1);
211 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
212 if (CSR_RESULT(rv) != CSR_SUCCESS)
213 rc = -1;
214 break;
215 }
216 default:
217 luaL_error(l, "Unsupported NeoMutt parameter type %d for %s",
218 CONFIG_TYPE(cdef->type), param);
219 rc = -1;
220 break;
221 }
222
223 buf_pool_release(&err);
224 return rc;
225}
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:47
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 233 of file lua.c.

234{
235 const char *param = lua_tostring(l, -1);
236 mutt_debug(LL_DEBUG2, "%s\n", param);
237
238 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
239 if (!he)
240 {
241 mutt_debug(LL_DEBUG2, "error\n");
242 luaL_error(l, "NeoMutt parameter not found %s", param);
243 return -1;
244 }
245
246 struct ConfigDef *cdef = he->data;
247
248 switch (CONFIG_TYPE(cdef->type))
249 {
250 case DT_ADDRESS:
251 case DT_ENUM:
252 case DT_EXPANDO:
253 case DT_MBTABLE:
254 case DT_MYVAR:
255 case DT_PATH:
256 case DT_REGEX:
257 case DT_SLIST:
258 case DT_SORT:
259 case DT_STRING:
260 {
261 struct Buffer *value = buf_pool_get();
262 int rc = cs_subset_he_string_get(NeoMutt->sub, he, value);
263 if (CSR_RESULT(rc) != CSR_SUCCESS)
264 {
265 buf_pool_release(&value);
266 return -1;
267 }
268
269 struct Buffer *escaped = buf_pool_get();
270 escape_string(escaped, buf_string(value));
271 lua_pushstring(l, buf_string(escaped));
272 buf_pool_release(&value);
273 buf_pool_release(&escaped);
274 return 1;
275 }
276 case DT_QUAD:
277 lua_pushinteger(l, (unsigned char) cdef->var);
278 return 1;
279 case DT_LONG:
280 lua_pushinteger(l, (signed long) cdef->var);
281 return 1;
282 case DT_NUMBER:
283 lua_pushinteger(l, (signed short) cdef->var);
284 return 1;
285 case DT_BOOL:
286 lua_pushboolean(l, (bool) cdef->var);
287 return 1;
288 default:
289 luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
290 return -1;
291 }
292}
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 300 of file lua.c.

301{
302 mutt_debug(LL_DEBUG2, "enter\n");
303 struct Buffer *err = buf_pool_get();
304 struct Buffer *line = buf_pool_get();
305
306 buf_strcpy(line, lua_tostring(l, -1));
307 int rc = 0;
308
309 if (parse_rc_line(line, err))
310 {
311 luaL_error(l, "NeoMutt error: %s", buf_string(err));
312 rc = -1;
313 }
314 else
315 {
316 if (!lua_pushstring(l, buf_string(err)))
318 else
319 rc++;
320 }
321
322 buf_pool_release(&line);
323 buf_pool_release(&err);
324
325 return rc;
326}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
enum CommandResult parse_rc_line(struct Buffer *line, struct Buffer *err)
Parse a line of user config.
Definition rc.c:45
+ 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 333 of file lua.c.

334{
335 mutt_debug(LL_DEBUG2, "enter\n");
336 const char *msg = lua_tostring(l, -1);
337 if (msg)
338 mutt_message("%s", msg);
339 return 0;
340}
#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 347 of file lua.c.

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

◆ 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 361 of file lua.c.

362{
363 char buf[1024] = { 0 };
364 snprintf(buf, sizeof(buf), "mutt.command.%s = function (...); mutt.call('%s', ...); end",
365 cmd->name, cmd->name);
366 (void) luaL_dostring(l, buf);
367}
const char * name
Name of the command.
Definition command.h:59
+ 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 396 of file lua.c.

397{
398 mutt_debug(LL_DEBUG2, "enter\n");
399 luaL_newlib(l, LuaMuttCommands);
400 int lib_idx = lua_gettop(l);
401
402 // clang-format off
403 lua_pushstring(l, "VERSION"); lua_pushstring(l, mutt_make_version()); lua_settable(l, lib_idx);
404 lua_pushstring(l, "QUAD_YES"); lua_pushinteger(l, MUTT_YES); lua_settable(l, lib_idx);
405 lua_pushstring(l, "QUAD_NO"); lua_pushinteger(l, MUTT_NO); lua_settable(l, lib_idx);
406 lua_pushstring(l, "QUAD_ASKYES"); lua_pushinteger(l, MUTT_ASKYES); lua_settable(l, lib_idx);
407 lua_pushstring(l, "QUAD_ASKNO"); lua_pushinteger(l, MUTT_ASKNO); lua_settable(l, lib_idx);
408 // clang-format on
409
410 return 1;
411}
static const luaL_Reg LuaMuttCommands[]
List of Lua commands to register.
Definition lua.c:378
@ 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 417 of file lua.c.

418{
419 luaL_requiref(l, "mutt", lua_expose_commands, 1);
420 (void) luaL_dostring(l, "mutt.command = {}");
421
422 const struct Command **cp = NULL;
424 {
425 const struct Command *cmd = *cp;
426
427 lua_expose_command(l, cmd);
428 }
429}
#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:396
static void lua_expose_command(lua_State *l, const struct Command *cmd)
Expose a NeoMutt command to the Lua interpreter.
Definition lua.c:361
+ 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 436 of file lua.c.

437{
438 if (!l)
439 return false;
440 if (*l)
441 return true;
442
443 mutt_debug(LL_DEBUG2, "enter\n");
444 *l = luaL_newstate();
445
446 if (!*l)
447 {
448 mutt_error(_("Error: Couldn't load the lua interpreter"));
449 return false;
450 }
451
452 lua_atpanic(*l, lua_handle_panic);
453
454 /* load various Lua libraries */
455 luaL_openlibs(*l);
456 lua_expose_mutt(*l);
457
458 return true;
459}
static void lua_expose_mutt(lua_State *l)
Expose a 'Mutt' object to the Lua interpreter.
Definition lua.c:417
static int lua_handle_panic(lua_State *l)
Handle a panic in the Lua interpreter.
Definition lua.c:63
#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 56 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:142
static int lua_cb_global_message(lua_State *l)
Display a message in NeoMutt.
Definition lua.c:333
static int lua_cb_global_error(lua_State *l)
Display an error in NeoMutt.
Definition lua.c:347
static int lua_cb_global_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition lua.c:300
static int lua_cb_global_get(lua_State *l)
Get a NeoMutt variable.
Definition lua.c:233
static int lua_cb_global_call(lua_State *l)
Call a NeoMutt command by name.
Definition lua.c:90

List of Lua commands to register.

In NeoMutt, run:

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

and it will call lua_cb_global_message()

Definition at line 378 of file lua.c.

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