NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
mutt_lua.c File Reference

Integrated Lua scripting. More...

#include "config.h"
#include <lauxlib.h>
#include <limits.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 "mutt.h"
#include "mutt_lua.h"
#include "init.h"
#include "mutt_commands.h"
#include "muttlib.h"
#include "myvar.h"
+ Include dependency graph for mutt_lua.c:

Go to the source code of this file.

Macros

#define LUA_COMPAT_ALL
 
#define LUA_COMPAT_5_1
 

Functions

static int handle_panic (lua_State *l)
 Handle a panic in the Lua interpreter. More...
 
static int handle_error (lua_State *l)
 Handle an error in the Lua interpreter. More...
 
static int lua_mutt_call (lua_State *l)
 Call a NeoMutt command by name. More...
 
static int lua_mutt_set (lua_State *l)
 Set a NeoMutt variable. More...
 
static int lua_mutt_get (lua_State *l)
 Get a NeoMutt variable. More...
 
static int lua_mutt_enter (lua_State *l)
 Execute NeoMutt config from Lua. More...
 
static int lua_mutt_message (lua_State *l)
 Display a message in Neomutt. More...
 
static int lua_mutt_error (lua_State *l)
 Display an error in Neomutt. More...
 
static void lua_expose_command (void *p, const struct Command *cmd)
 Expose a NeoMutt command to the Lua interpreter. More...
 
static int luaopen_mutt_decl (lua_State *l)
 Declare some NeoMutt types to the Lua interpreter. More...
 
static void luaopen_mutt (lua_State *l)
 Expose a 'Mutt' object to the Lua interpreter. More...
 
static bool lua_init (lua_State **l)
 Initialise a Lua State. More...
 
void mutt_lua_init (void)
 Setup feature commands. More...
 
enum CommandResult mutt_lua_parse (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'lua' command - Implements Command::parse() -. More...
 
enum CommandResult mutt_lua_source_file (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'lua-source' command - Implements Command::parse() -. More...
 

Variables

lua_State * LuaState = NULL
 Global Lua State. More...
 
static const struct Command lua_commands []
 List of NeoMutt commands to register. More...
 
static const luaL_Reg luaMuttDecl []
 List of Lua commands to register. More...
 

Detailed Description

Integrated Lua scripting.

Authors
  • Richard Russon
  • Bernard Pratz

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 mutt_lua.c.

Macro Definition Documentation

◆ LUA_COMPAT_ALL

#define LUA_COMPAT_ALL

Definition at line 31 of file mutt_lua.c.

◆ LUA_COMPAT_5_1

#define LUA_COMPAT_5_1

Definition at line 34 of file mutt_lua.c.

Function Documentation

◆ handle_panic()

static int handle_panic ( lua_State *  l)
static

Handle a panic in the Lua interpreter.

Parameters
lLua State
Return values
-1Always

Definition at line 73 of file mutt_lua.c.

74 {
75  mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
76  mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
77  lua_pop(l, 1);
78  return -1;
79 }
#define mutt_error(...)
Definition: logging.h:88
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 1.
Definition: logging.h:40
+ Here is the caller graph for this function:

◆ handle_error()

static int handle_error ( lua_State *  l)
static

Handle an error in the Lua interpreter.

Parameters
lLua State
Return values
-1Always

Definition at line 86 of file mutt_lua.c.

87 {
88  mutt_debug(LL_DEBUG1, "lua runtime error: %s\n", lua_tostring(l, -1));
89  mutt_error("Lua runtime error: %s", lua_tostring(l, -1));
90  lua_pop(l, 1);
91  return -1;
92 }
#define mutt_error(...)
Definition: logging.h:88
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 1.
Definition: logging.h:40
+ Here is the caller graph for this function:

◆ lua_mutt_call()

static int lua_mutt_call ( lua_State *  l)
static

Call a NeoMutt command by name.

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

Definition at line 100 of file mutt_lua.c.

101 {
102  mutt_debug(LL_DEBUG2, " * lua_mutt_call()\n");
103  struct Buffer *err = mutt_buffer_pool_get();
104  struct Buffer *token = mutt_buffer_pool_get();
105  char buf[1024] = { 0 };
106  const struct Command *cmd = NULL;
107  int rc = 0;
108 
109  if (lua_gettop(l) == 0)
110  {
111  luaL_error(l, "Error command argument required.");
112  return -1;
113  }
114 
115  cmd = mutt_command_get(lua_tostring(l, 1));
116  if (!cmd)
117  {
118  luaL_error(l, "Error command %s not found.", lua_tostring(l, 1));
119  return -1;
120  }
121 
122  for (int i = 2; i <= lua_gettop(l); i++)
123  {
124  const char *s = lua_tostring(l, i);
125  mutt_strn_cat(buf, sizeof(buf), s, mutt_str_len(s));
126  mutt_strn_cat(buf, sizeof(buf), " ", 1);
127  }
128 
129  struct Buffer expn = mutt_buffer_make(0);
130  expn.data = buf;
131  expn.dptr = buf;
132  expn.dsize = mutt_str_len(buf);
133 
134  if (cmd->parse(token, &expn, cmd->data, err))
135  {
136  luaL_error(l, "NeoMutt error: %s", mutt_buffer_string(err));
137  rc = -1;
138  }
139  else
140  {
141  if (!lua_pushstring(l, mutt_buffer_string(err)))
142  handle_error(l);
143  else
144  rc++;
145  }
146 
147  mutt_buffer_pool_release(&token);
149  return rc;
150 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
intptr_t data
Data or flags to pass to the command.
Definition: mutt_commands.h:64
char * mutt_strn_cat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:414
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
String manipulation buffer.
Definition: buffer.h:33
enum CommandResult(* parse)(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Definition: mutt_commands.h:62
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
size_t dsize
Length of data.
Definition: buffer.h:37
static int handle_error(lua_State *l)
Handle an error in the Lua interpreter.
Definition: mutt_lua.c:86
Log at debug level 2.
Definition: logging.h:41
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
struct Command * mutt_command_get(const char *s)
Get a Command by its name.
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
+ Here is the call graph for this function:

◆ lua_mutt_set()

static int lua_mutt_set ( lua_State *  l)
static

Set a NeoMutt variable.

Parameters
lLua State
Return values
0Success
-1Error

Definition at line 158 of file mutt_lua.c.

159 {
160  const char *param = lua_tostring(l, -2);
161  mutt_debug(LL_DEBUG2, " * lua_mutt_set(%s)\n", param);
162 
163  if (mutt_str_startswith(param, "my_"))
164  {
165  const char *val = lua_tostring(l, -1);
166  myvar_set(param, val);
167  return 0;
168  }
169 
170  struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
171  if (!he)
172  {
173  luaL_error(l, "NeoMutt parameter not found %s", param);
174  return -1;
175  }
176 
177  struct ConfigDef *cdef = he->data;
178 
179  int rc = 0;
180  struct Buffer err = mutt_buffer_make(256);
181 
182  switch (DTYPE(cdef->type))
183  {
184  case DT_ADDRESS:
185  case DT_ENUM:
186  case DT_MBTABLE:
187  case DT_PATH:
188  case DT_REGEX:
189  case DT_SLIST:
190  case DT_SORT:
191  case DT_STRING:
192  {
193  const char *value = lua_tostring(l, -1);
194  size_t val_size = lua_rawlen(l, -1);
195  struct Buffer value_buf = mutt_buffer_make(val_size);
196  mutt_buffer_strcpy_n(&value_buf, value, val_size);
197  if (DTYPE(he->type) == DT_PATH)
198  mutt_buffer_expand_path(&value_buf);
199 
200  int rv = cs_subset_he_string_set(NeoMutt->sub, he, value_buf.data, &err);
201  mutt_buffer_dealloc(&value_buf);
202  if (CSR_RESULT(rv) != CSR_SUCCESS)
203  rc = -1;
204  break;
205  }
206  case DT_NUMBER:
207  case DT_QUAD:
208  {
209  const intptr_t value = lua_tointeger(l, -1);
210  int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, &err);
211  if (CSR_RESULT(rv) != CSR_SUCCESS)
212  rc = -1;
213  break;
214  }
215  case DT_BOOL:
216  {
217  const intptr_t value = lua_toboolean(l, -1);
218  int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, &err);
219  if (CSR_RESULT(rv) != CSR_SUCCESS)
220  rc = -1;
221  break;
222  }
223  default:
224  luaL_error(l, "Unsupported NeoMutt parameter type %d for %s", DTYPE(cdef->type), param);
225  rc = -1;
226  break;
227  }
228 
229  mutt_buffer_dealloc(&err);
230  return rc;
231 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define DT_REGEX
regular expressions
Definition: types.h:38
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
String manipulation buffer.
Definition: buffer.h:33
#define DT_SORT
sorting methods
Definition: types.h:40
#define DT_SLIST
a list of strings
Definition: types.h:39
#define DT_MBTABLE
multibyte char table
Definition: types.h:34
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition: subset.c:179
Definition: set.h:63
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:37
Container for Accounts, Notifications.
Definition: neomutt.h:36
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:44
#define CSR_RESULT(x)
Definition: set.h:52
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:386
Log at debug level 2.
Definition: logging.h:41
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
#define DT_ADDRESS
e-mail address
Definition: types.h:29
#define DT_STRING
a string
Definition: types.h:41
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:160
#define DT_PATH
a path to a file/directory
Definition: types.h:36
size_t mutt_buffer_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition: buffer.c:327
char * data
Pointer to data.
Definition: buffer.h:35
void myvar_set(const char *var, const char *val)
Set the value of a "my_" variable.
Definition: myvar.c:109
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void * data
User-supplied data.
Definition: hash.h:47
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:322
#define DT_ENUM
an enumeration
Definition: types.h:31
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
The item stored in a Hash Table.
Definition: hash.h:43
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
#define DT_NUMBER
a number
Definition: types.h:35
#define DT_BOOL
boolean option
Definition: types.h:30
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:283
+ Here is the call graph for this function:

◆ lua_mutt_get()

static int lua_mutt_get ( lua_State *  l)
static

Get a NeoMutt variable.

Parameters
lLua State
Return values
1Success
-1Error

Definition at line 239 of file mutt_lua.c.

240 {
241  const char *param = lua_tostring(l, -1);
242  mutt_debug(LL_DEBUG2, " * lua_mutt_get(%s)\n", param);
243 
244  if (mutt_str_startswith(param, "my_"))
245  {
246  const char *mv = myvar_get(param);
247  if (!mv)
248  {
249  luaL_error(l, "NeoMutt parameter not found %s", param);
250  return -1;
251  }
252 
253  lua_pushstring(l, mv);
254  return 1;
255  }
256 
257  struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
258  if (!he)
259  {
260  mutt_debug(LL_DEBUG2, " * error\n");
261  luaL_error(l, "NeoMutt parameter not found %s", param);
262  return -1;
263  }
264 
265  struct ConfigDef *cdef = he->data;
266 
267  switch (DTYPE(cdef->type))
268  {
269  case DT_ADDRESS:
270  case DT_ENUM:
271  case DT_MBTABLE:
272  case DT_REGEX:
273  case DT_SLIST:
274  case DT_SORT:
275  case DT_STRING:
276  {
277  struct Buffer value = mutt_buffer_make(256);
278  int rc = cs_subset_he_string_get(NeoMutt->sub, he, &value);
279  if (CSR_RESULT(rc) != CSR_SUCCESS)
280  {
281  mutt_buffer_dealloc(&value);
282  return -1;
283  }
284 
285  struct Buffer escaped = mutt_buffer_make(256);
286  escape_string(&escaped, value.data);
287  lua_pushstring(l, escaped.data);
288  mutt_buffer_dealloc(&value);
289  mutt_buffer_dealloc(&escaped);
290  return 1;
291  }
292  case DT_QUAD:
293  lua_pushinteger(l, (unsigned char) cdef->var);
294  return 1;
295  case DT_NUMBER:
296  lua_pushinteger(l, (signed short) cdef->var);
297  return 1;
298  case DT_BOOL:
299  lua_pushboolean(l, (bool) cdef->var);
300  return 1;
301  default:
302  luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
303  return -1;
304  }
305 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define DT_REGEX
regular expressions
Definition: types.h:38
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
String manipulation buffer.
Definition: buffer.h:33
#define DT_SORT
sorting methods
Definition: types.h:40
#define DT_SLIST
a list of strings
Definition: types.h:39
#define DT_MBTABLE
multibyte char table
Definition: types.h:34
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition: subset.c:179
Definition: set.h:63
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:37
Container for Accounts, Notifications.
Definition: neomutt.h:36
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:44
#define CSR_RESULT(x)
Definition: set.h:52
Log at debug level 2.
Definition: logging.h:41
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:354
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
#define DT_ADDRESS
e-mail address
Definition: types.h:29
#define DT_STRING
a string
Definition: types.h:41
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:160
char * data
Pointer to data.
Definition: buffer.h:35
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
const char * myvar_get(const char *var)
Get the value of a "my_" variable.
Definition: myvar.c:92
void * data
User-supplied data.
Definition: hash.h:47
intptr_t var
Storage for the variable.
Definition: set.h:85
size_t escape_string(struct Buffer *buf, const char *src)
Write a string to a buffer, escaping special characters.
Definition: dump.c:46
#define DT_ENUM
an enumeration
Definition: types.h:31
The item stored in a Hash Table.
Definition: hash.h:43
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
#define DT_NUMBER
a number
Definition: types.h:35
#define DT_BOOL
boolean option
Definition: types.h:30
+ Here is the call graph for this function:

◆ lua_mutt_enter()

static int lua_mutt_enter ( lua_State *  l)
static

Execute NeoMutt config from Lua.

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

Definition at line 313 of file mutt_lua.c.

314 {
315  mutt_debug(LL_DEBUG2, " * lua_mutt_enter()\n");
316  struct Buffer *err = mutt_buffer_pool_get();
317  char *buf = mutt_str_dup(lua_tostring(l, -1));
318  int rc = 0;
319 
320  if (mutt_parse_rc_line(buf, err))
321  {
322  luaL_error(l, "NeoMutt error: %s", mutt_buffer_string(err));
323  rc = -1;
324  }
325  else
326  {
327  if (!lua_pushstring(l, mutt_buffer_string(err)))
328  handle_error(l);
329  else
330  rc++;
331  }
332 
333  FREE(&buf);
335 
336  return rc;
337 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
enum CommandResult mutt_parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: init.c:1039
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
static int handle_error(lua_State *l)
Handle an error in the Lua interpreter.
Definition: mutt_lua.c:86
Log at debug level 2.
Definition: logging.h:41
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:

◆ lua_mutt_message()

static int lua_mutt_message ( lua_State *  l)
static

Display a message in Neomutt.

Parameters
lLua State
Return values
0Always

Definition at line 344 of file mutt_lua.c.

345 {
346  mutt_debug(LL_DEBUG2, " * lua_mutt_message()\n");
347  const char *msg = lua_tostring(l, -1);
348  if (msg)
349  mutt_message(msg);
350  return 0;
351 }
Log at debug level 2.
Definition: logging.h:41
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
#define mutt_message(...)
Definition: logging.h:87

◆ lua_mutt_error()

static int lua_mutt_error ( lua_State *  l)
static

Display an error in Neomutt.

Parameters
lLua State
Return values
0Always

Definition at line 358 of file mutt_lua.c.

359 {
360  mutt_debug(LL_DEBUG2, " * lua_mutt_error()\n");
361  const char *msg = lua_tostring(l, -1);
362  if (msg)
363  mutt_error(msg);
364  return 0;
365 }
#define mutt_error(...)
Definition: logging.h:88
Log at debug level 2.
Definition: logging.h:41
#define mutt_debug(LEVEL,...)
Definition: logging.h:85

◆ lua_expose_command()

static void lua_expose_command ( void *  p,
const struct Command cmd 
)
static

Expose a NeoMutt command to the Lua interpreter.

Parameters
pLua state
cmdNeoMutt Command

Definition at line 372 of file mutt_lua.c.

373 {
374  lua_State *l = (lua_State *) p;
375  char buf[1024];
376  snprintf(buf, sizeof(buf), "mutt.command.%s = function (...); mutt.call('%s', ...); end",
377  cmd->name, cmd->name);
378  (void) luaL_dostring(l, buf);
379 }
const char * name
Name of the command.
Definition: mutt_commands.h:49
+ Here is the caller graph for this function:

◆ luaopen_mutt_decl()

static int luaopen_mutt_decl ( lua_State *  l)
static

Declare some NeoMutt types to the Lua interpreter.

Parameters
lLua State
Return values
1Always

Definition at line 408 of file mutt_lua.c.

409 {
410  mutt_debug(LL_DEBUG2, " * luaopen_mutt()\n");
411  luaL_newlib(l, luaMuttDecl);
412  int lib_idx = lua_gettop(l);
413 
414  // clang-format off
415  lua_pushstring(l, "VERSION"); lua_pushstring(l, mutt_make_version()); lua_settable(l, lib_idx);;
416  lua_pushstring(l, "QUAD_YES"); lua_pushinteger(l, MUTT_YES); lua_settable(l, lib_idx);;
417  lua_pushstring(l, "QUAD_NO"); lua_pushinteger(l, MUTT_NO); lua_settable(l, lib_idx);;
418  lua_pushstring(l, "QUAD_ASKYES"); lua_pushinteger(l, MUTT_ASKYES); lua_settable(l, lib_idx);;
419  lua_pushstring(l, "QUAD_ASKNO"); lua_pushinteger(l, MUTT_ASKNO); lua_settable(l, lib_idx);;
420  // clang-format on
421 
422  return 1;
423 }
Ask the user, defaulting to &#39;No&#39;.
Definition: quad.h:40
Log at debug level 2.
Definition: logging.h:41
Ask the user, defaulting to &#39;Yes&#39;.
Definition: quad.h:41
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
static const luaL_Reg luaMuttDecl[]
List of Lua commands to register.
Definition: mutt_lua.c:390
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:38
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:1476
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ luaopen_mutt()

static void luaopen_mutt ( lua_State *  l)
static

Expose a 'Mutt' object to the Lua interpreter.

Parameters
lLua State

Definition at line 429 of file mutt_lua.c.

430 {
431  luaL_requiref(l, "mutt", luaopen_mutt_decl, 1);
432  (void) luaL_dostring(l, "mutt.command = {}");
434 }
void mutt_commands_apply(void *data, void(*application)(void *, const struct Command *))
Run a callback function on every Command.
static int luaopen_mutt_decl(lua_State *l)
Declare some NeoMutt types to the Lua interpreter.
Definition: mutt_lua.c:408
static void lua_expose_command(void *p, const struct Command *cmd)
Expose a NeoMutt command to the Lua interpreter.
Definition: mutt_lua.c:372
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lua_init()

static bool lua_init ( lua_State **  l)
static

Initialise a Lua State.

Parameters
[out]lLua State
Return values
trueSuccessful

Definition at line 441 of file mutt_lua.c.

442 {
443  if (!l)
444  return false;
445  if (*l)
446  return true;
447 
448  mutt_debug(LL_DEBUG2, " * lua_init()\n");
449  *l = luaL_newstate();
450 
451  if (!*l)
452  {
453  mutt_error(_("Error: Couldn't load the lua interpreter"));
454  return false;
455  }
456 
457  lua_atpanic(*l, handle_panic);
458 
459  /* load various Lua libraries */
460  luaL_openlibs(*l);
461  luaopen_mutt(*l);
462 
463  return true;
464 }
#define mutt_error(...)
Definition: logging.h:88
static int handle_panic(lua_State *l)
Handle a panic in the Lua interpreter.
Definition: mutt_lua.c:73
#define _(a)
Definition: message.h:28
Log at debug level 2.
Definition: logging.h:41
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
static void luaopen_mutt(lua_State *l)
Expose a &#39;Mutt&#39; object to the Lua interpreter.
Definition: mutt_lua.c:429
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_lua_init()

void mutt_lua_init ( void  )

Setup feature commands.

Definition at line 469 of file mutt_lua.c.

470 {
472 }
#define COMMANDS_REGISTER(cmds)
Definition: mutt_commands.h:82
static const struct Command lua_commands[]
List of NeoMutt commands to register.
Definition: mutt_lua.c:61
+ Here is the caller graph for this function:

Variable Documentation

◆ LuaState

lua_State* LuaState = NULL

Global Lua State.

Definition at line 56 of file mutt_lua.c.

◆ lua_commands

const struct Command lua_commands[]
static
Initial value:
= {
{ "lua", mutt_lua_parse, 0 },
{ "lua-source", mutt_lua_source_file, 0 },
}
enum CommandResult mutt_lua_parse(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;lua&#39; command - Implements Command::parse() -.
Definition: mutt_lua.c:477
enum CommandResult mutt_lua_source_file(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;lua-source&#39; command - Implements Command::parse() -.
Definition: mutt_lua.c:499

List of NeoMutt commands to register.

Definition at line 61 of file mutt_lua.c.

◆ luaMuttDecl

const luaL_Reg luaMuttDecl[]
static
Initial value:
= {
{ "set", lua_mutt_set },
{ "get", lua_mutt_get },
{ "call", lua_mutt_call },
{ "enter", lua_mutt_enter },
{ "print", lua_mutt_message },
{ "message", lua_mutt_message },
{ "error", lua_mutt_error },
{ NULL, NULL },
}
static int lua_mutt_set(lua_State *l)
Set a NeoMutt variable.
Definition: mutt_lua.c:158
static int lua_mutt_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition: mutt_lua.c:313
static int lua_mutt_message(lua_State *l)
Display a message in Neomutt.
Definition: mutt_lua.c:344
static int lua_mutt_call(lua_State *l)
Call a NeoMutt command by name.
Definition: mutt_lua.c:100
static int lua_mutt_error(lua_State *l)
Display an error in Neomutt.
Definition: mutt_lua.c:358
static int lua_mutt_get(lua_State *l)
Get a NeoMutt variable.
Definition: mutt_lua.c:239

List of Lua commands to register.

In NeoMutt, run:

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

and it will call lua_mutt_message()

Definition at line 390 of file mutt_lua.c.