• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

neomutt / neomutt / 21576993800

02 Feb 2026 01:15AM UTC coverage: 42.169% (+0.2%) from 42.019%
21576993800

push

github

flatcap
build: force all-docs before validation

11846 of 28092 relevant lines covered (42.17%)

447.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.62
/lua/lua.c
1
/**
2
 * @file
3
 * Integrated Lua scripting
4
 *
5
 * @authors
6
 * Copyright (C) 2016-2017 Bernard Pratz <z+mutt+pub@m0g.net>
7
 * Copyright (C) 2017-2025 Richard Russon <rich@flatcap.org>
8
 * Copyright (C) 2018 Victor Fernandes <criw@pm.me>
9
 * Copyright (C) 2019 Ian Zimmerman <itz@no-use.mooo.com>
10
 * Copyright (C) 2019-2020 Pietro Cerutti <gahr@gahr.ch>
11
 * Copyright (C) 2023 Rayford Shireman
12
 *
13
 * @copyright
14
 * This program is free software: you can redistribute it and/or modify it under
15
 * the terms of the GNU General Public License as published by the Free Software
16
 * Foundation, either version 2 of the License, or (at your option) any later
17
 * version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
22
 * details.
23
 *
24
 * You should have received a copy of the GNU General Public License along with
25
 * this program.  If not, see <http://www.gnu.org/licenses/>.
26
 */
27

28
/**
29
 * @page lua_lua Integrated Lua scripting
30
 *
31
 * Integrated Lua scripting
32
 */
33

34
#ifndef LUA_COMPAT_ALL
35
#define LUA_COMPAT_ALL
36
#endif
37
#ifndef LUA_COMPAT_5_1
38
#define LUA_COMPAT_5_1
39
#endif
40

41
#include "config.h"
42
#include <lauxlib.h>
43
#include <lua.h>
44
#include <lualib.h>
45
#include <stdbool.h>
46
#include <stdint.h>
47
#include <stdio.h>
48
#include "mutt/lib.h"
49
#include "config/lib.h"
50
#include "core/lib.h"
51
#include "parse/lib.h"
52
#include "muttlib.h"
53
#include "version.h"
54

55
/// Global Lua State
56
lua_State *LuaState = NULL;
57

58
/**
59
 * lua_handle_panic - Handle a panic in the Lua interpreter
60
 * @param l Lua State
61
 * @retval -1 Always
62
 */
63
static int lua_handle_panic(lua_State *l)
×
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
}
70

71
/**
72
 * lua_handle_error - Handle an error in the Lua interpreter
73
 * @param l Lua State
74
 * @retval -1 Always
75
 */
76
static int lua_handle_error(lua_State *l)
×
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
}
83

84
/**
85
 * lua_cb_global_call - Call a NeoMutt command by name
86
 * @param l Lua State
87
 * @retval >=0 Success
88
 * @retval -1 Error
89
 */
90
static int lua_cb_global_call(lua_State *l)
×
91
{
92
  mutt_debug(LL_DEBUG2, "enter\n");
×
93
  struct Buffer *buf = buf_pool_get();
×
94
  struct ParseContext *pc = parse_context_new();
×
95
  struct ParseError *pe = parse_error_new();
×
96
  const struct Command *cmd = NULL;
97
  int rc = 0;
98

99
  if (lua_gettop(l) == 0)
×
100
  {
101
    luaL_error(l, "Error command argument required");
×
102
    return -1;
×
103
  }
104

105
  cmd = commands_get(&NeoMutt->commands, lua_tostring(l, 1));
×
106
  if (!cmd)
×
107
  {
108
    luaL_error(l, "Error command %s not found", lua_tostring(l, 1));
×
109
    return -1;
×
110
  }
111

112
  for (int i = 2; i <= lua_gettop(l); i++)
×
113
  {
114
    buf_addstr(buf, lua_tostring(l, i));
×
115
    buf_addch(buf, ' ');
×
116
  }
117
  buf_seek(buf, 0);
×
118

119
  if (cmd->parse(cmd, buf, pc, pe))
×
120
  {
121
    luaL_error(l, "NeoMutt error: %s", buf_string(pe->message));
×
122
    rc = -1;
123
  }
124
  else
125
  {
126
    if (!lua_pushstring(l, buf_string(pe->message)))
×
127
      lua_handle_error(l);
×
128
    else
129
      rc++;
130
  }
131

132
  buf_pool_release(&buf);
×
133
  parse_context_free(&pc);
×
134
  parse_error_free(&pe);
×
135
  return rc;
×
136
}
137

138
/**
139
 * lua_cb_global_set - Set a NeoMutt variable
140
 * @param l Lua State
141
 * @retval  0 Success
142
 * @retval -1 Error
143
 */
144
static int lua_cb_global_set(lua_State *l)
×
145
{
146
  const char *param = lua_tostring(l, -2);
×
147
  mutt_debug(LL_DEBUG2, "%s\n", param);
×
148

149
  struct Buffer *err = buf_pool_get();
×
150
  struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
×
151
  if (!he)
×
152
  {
153
    // In case it is a my_var, we have to create it
154
    if (mutt_str_startswith(param, "my_"))
×
155
    {
156
      struct ConfigDef my_cdef = { 0 };
×
157
      my_cdef.name = param;
×
158
      my_cdef.type = DT_MYVAR;
×
159
      he = cs_create_variable(NeoMutt->sub->cs, &my_cdef, err);
×
160
      if (!he)
×
161
        return -1;
×
162
    }
163
    else
164
    {
165
      luaL_error(l, "NeoMutt parameter not found %s", param);
×
166
      return -1;
×
167
    }
168
  }
169

170
  struct ConfigDef *cdef = he->data;
×
171

172
  int rc = 0;
173

174
  switch (CONFIG_TYPE(cdef->type))
×
175
  {
176
    case DT_ADDRESS:
×
177
    case DT_ENUM:
178
    case DT_EXPANDO:
179
    case DT_MBTABLE:
180
    case DT_MYVAR:
181
    case DT_PATH:
182
    case DT_REGEX:
183
    case DT_SLIST:
184
    case DT_SORT:
185
    case DT_STRING:
186
    {
187
      const char *value = lua_tostring(l, -1);
×
188
      size_t val_size = lua_rawlen(l, -1);
×
189
      struct Buffer *value_buf = buf_pool_get();
×
190
      buf_strcpy_n(value_buf, value, val_size);
×
191
      if (CONFIG_TYPE(he->type) == DT_PATH)
×
192
        expand_path(value_buf, false);
×
193

194
      int rv = cs_subset_he_string_set(NeoMutt->sub, he, buf_string(value_buf), err);
×
195
      buf_pool_release(&value_buf);
×
196
      if (CSR_RESULT(rv) != CSR_SUCCESS)
×
197
        rc = -1;
198
      break;
199
    }
200
    case DT_LONG:
×
201
    case DT_NUMBER:
202
    case DT_QUAD:
203
    {
204
      const intptr_t value = lua_tointeger(l, -1);
×
205
      int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
×
206
      if (CSR_RESULT(rv) != CSR_SUCCESS)
×
207
        rc = -1;
208
      break;
209
    }
210
    case DT_BOOL:
×
211
    {
212
      const intptr_t value = lua_toboolean(l, -1);
×
213
      int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
×
214
      if (CSR_RESULT(rv) != CSR_SUCCESS)
×
215
        rc = -1;
216
      break;
217
    }
218
    default:
×
219
      luaL_error(l, "Unsupported NeoMutt parameter type %d for %s",
×
220
                 CONFIG_TYPE(cdef->type), param);
221
      rc = -1;
222
      break;
×
223
  }
224

225
  buf_pool_release(&err);
×
226
  return rc;
×
227
}
228

229
/**
230
 * lua_cb_global_get - Get a NeoMutt variable
231
 * @param l Lua State
232
 * @retval  1 Success
233
 * @retval -1 Error
234
 */
235
static int lua_cb_global_get(lua_State *l)
×
236
{
237
  const char *param = lua_tostring(l, -1);
×
238
  mutt_debug(LL_DEBUG2, "%s\n", param);
×
239

240
  struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
×
241
  if (!he)
×
242
  {
243
    mutt_debug(LL_DEBUG2, "error\n");
×
244
    luaL_error(l, "NeoMutt parameter not found %s", param);
×
245
    return -1;
×
246
  }
247

248
  struct ConfigDef *cdef = he->data;
×
249

250
  switch (CONFIG_TYPE(cdef->type))
×
251
  {
252
    case DT_ADDRESS:
×
253
    case DT_ENUM:
254
    case DT_EXPANDO:
255
    case DT_MBTABLE:
256
    case DT_MYVAR:
257
    case DT_PATH:
258
    case DT_REGEX:
259
    case DT_SLIST:
260
    case DT_SORT:
261
    case DT_STRING:
262
    {
263
      struct Buffer *value = buf_pool_get();
×
264
      int rc = cs_subset_he_string_get(NeoMutt->sub, he, value);
×
265
      if (CSR_RESULT(rc) != CSR_SUCCESS)
×
266
      {
267
        buf_pool_release(&value);
×
268
        return -1;
×
269
      }
270

271
      struct Buffer *escaped = buf_pool_get();
×
272
      escape_string(escaped, buf_string(value));
×
273
      lua_pushstring(l, buf_string(escaped));
×
274
      buf_pool_release(&value);
×
275
      buf_pool_release(&escaped);
×
276
      return 1;
×
277
    }
278
    case DT_QUAD:
×
279
      lua_pushinteger(l, (unsigned char) cdef->var);
×
280
      return 1;
×
281
    case DT_LONG:
×
282
      lua_pushinteger(l, (signed long) cdef->var);
×
283
      return 1;
×
284
    case DT_NUMBER:
×
285
      lua_pushinteger(l, (signed short) cdef->var);
×
286
      return 1;
×
287
    case DT_BOOL:
×
288
      lua_pushboolean(l, (bool) cdef->var);
×
289
      return 1;
×
290
    default:
×
291
      luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
×
292
      return -1;
×
293
  }
294
}
295

296
/**
297
 * lua_cb_global_enter - Execute NeoMutt config from Lua
298
 * @param l Lua State
299
 * @retval >=0 Success
300
 * @retval -1  Error
301
 */
302
static int lua_cb_global_enter(lua_State *l)
×
303
{
304
  mutt_debug(LL_DEBUG2, "enter\n");
×
305
  struct Buffer *line = buf_pool_get();
×
306
  struct ParseContext *pc = parse_context_new();
×
307
  struct ParseError *pe = parse_error_new();
×
308

309
  buf_strcpy(line, lua_tostring(l, -1));
×
310
  int rc = 0;
311

312
  if (parse_rc_line(line, pc, pe))
×
313
  {
314
    luaL_error(l, "NeoMutt error: %s", buf_string(pe->message));
×
315
    rc = -1;
316
  }
317
  else
318
  {
319
    if (!lua_pushstring(l, buf_string(pe->message)))
×
320
      lua_handle_error(l);
×
321
    else
322
      rc++;
323
  }
324

325
  buf_pool_release(&line);
×
326
  parse_context_free(&pc);
×
327
  parse_error_free(&pe);
×
328

329
  return rc;
×
330
}
331

332
/**
333
 * lua_cb_global_message - Display a message in NeoMutt
334
 * @param l Lua State
335
 * @retval 0 Always
336
 */
337
static int lua_cb_global_message(lua_State *l)
×
338
{
339
  mutt_debug(LL_DEBUG2, "enter\n");
×
340
  const char *msg = lua_tostring(l, -1);
×
341
  if (msg)
×
342
    mutt_message("%s", msg);
×
343
  return 0;
×
344
}
345

346
/**
347
 * lua_cb_global_error - Display an error in NeoMutt
348
 * @param l Lua State
349
 * @retval 0 Always
350
 */
351
static int lua_cb_global_error(lua_State *l)
×
352
{
353
  mutt_debug(LL_DEBUG2, "enter\n");
×
354
  const char *msg = lua_tostring(l, -1);
×
355
  if (msg)
×
356
    mutt_error("%s", msg);
×
357
  return 0;
×
358
}
359

360
/**
361
 * lua_expose_command - Expose a NeoMutt command to the Lua interpreter
362
 * @param l   Lua state
363
 * @param cmd NeoMutt Command
364
 */
365
static void lua_expose_command(lua_State *l, const struct Command *cmd)
×
366
{
367
  char buf[1024] = { 0 };
×
368
  snprintf(buf, sizeof(buf), "mutt.command.%s = function (...); mutt.call('%s', ...); end",
369
           cmd->name, cmd->name);
×
370
  (void) luaL_dostring(l, buf);
×
371
}
×
372

373
/**
374
 * LuaMuttCommands - List of Lua commands to register
375
 *
376
 * In NeoMutt, run:
377
 *
378
 * `:lua mutt.message('hello')`
379
 *
380
 * and it will call lua_cb_global_message()
381
 */
382
static const luaL_Reg LuaMuttCommands[] = {
383
  // clang-format off
384
  { "set",     lua_cb_global_set },
385
  { "get",     lua_cb_global_get },
386
  { "call",    lua_cb_global_call },
387
  { "enter",   lua_cb_global_enter },
388
  { "print",   lua_cb_global_message },
389
  { "message", lua_cb_global_message },
390
  { "error",   lua_cb_global_error },
391
  { NULL, NULL },
392
  // clang-format on
393
};
394

395
/**
396
 * lua_expose_commands - Declare some NeoMutt types to the Lua interpreter
397
 * @param l Lua State
398
 * @retval 1 Always
399
 */
400
static int lua_expose_commands(lua_State *l)
2✔
401
{
402
  mutt_debug(LL_DEBUG2, "enter\n");
2✔
403
  luaL_newlib(l, LuaMuttCommands);
2✔
404
  int lib_idx = lua_gettop(l);
2✔
405

406
  // clang-format off
407
  lua_pushstring(l, "VERSION");     lua_pushstring(l, mutt_make_version()); lua_settable(l, lib_idx);
2✔
408
  lua_pushstring(l, "QUAD_YES");    lua_pushinteger(l, MUTT_YES);           lua_settable(l, lib_idx);
2✔
409
  lua_pushstring(l, "QUAD_NO");     lua_pushinteger(l, MUTT_NO);            lua_settable(l, lib_idx);
2✔
410
  lua_pushstring(l, "QUAD_ASKYES"); lua_pushinteger(l, MUTT_ASKYES);        lua_settable(l, lib_idx);
2✔
411
  lua_pushstring(l, "QUAD_ASKNO");  lua_pushinteger(l, MUTT_ASKNO);         lua_settable(l, lib_idx);
2✔
412
  // clang-format on
413

414
  return 1;
2✔
415
}
416

417
/**
418
 * lua_expose_mutt - Expose a 'Mutt' object to the Lua interpreter
419
 * @param l Lua State
420
 */
421
static void lua_expose_mutt(lua_State *l)
2✔
422
{
423
  luaL_requiref(l, "mutt", lua_expose_commands, 1);
2✔
424
  (void) luaL_dostring(l, "mutt.command = {}");
2✔
425

426
  const struct Command **cp = NULL;
427
  ARRAY_FOREACH(cp, &NeoMutt->commands)
2✔
428
  {
429
    const struct Command *cmd = *cp;
×
430

431
    lua_expose_command(l, cmd);
×
432
  }
433
}
2✔
434

435
/**
436
 * lua_init_state - Initialise a Lua State
437
 * @param[out] l Lua State
438
 * @retval true Successful
439
 */
440
bool lua_init_state(lua_State **l)
2✔
441
{
442
  if (!l)
2✔
443
    return false;
444
  if (*l)
2✔
445
    return true;
446

447
  mutt_debug(LL_DEBUG2, "enter\n");
2✔
448
  *l = luaL_newstate();
2✔
449

450
  if (!*l)
2✔
451
  {
452
    mutt_error(_("Error: Couldn't load the lua interpreter"));
×
453
    return false;
×
454
  }
455

456
  lua_atpanic(*l, lua_handle_panic);
2✔
457

458
  /* load various Lua libraries */
459
  luaL_openlibs(*l);
2✔
460
  lua_expose_mutt(*l);
2✔
461

462
  return true;
2✔
463
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc