NeoMutt  2019-11-11
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/mutt.h"
#include "config/lib.h"
#include "mutt.h"
#include "mutt_lua.h"
#include "globals.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_add_lib_member(LUA, TABLE, KEY, VALUE, DATATYPE_HANDLER)
 

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...
 
enum CommandResult mutt_lua_parse (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
 Parse the 'lua' command - Implements command_t. More...
 
enum CommandResult mutt_lua_source_file (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
 Parse the 'lua-source' command - Implements command_t. More...
 

Variables

lua_State * LuaState = NULL
 
static const luaL_Reg luaMuttDecl []
 

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_add_lib_member

#define lua_add_lib_member (   LUA,
  TABLE,
  KEY,
  VALUE,
  DATATYPE_HANDLER 
)
Value:
lua_pushstring(LUA, KEY); \
DATATYPE_HANDLER(LUA, VALUE); \
lua_settable(LUA, TABLE);

Definition at line 363 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 52 of file mutt_lua.c.

53 {
54  mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
55  mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
56  lua_pop(l, 1);
57  return -1;
58 }
Log at debug level 1.
Definition: logging.h:56
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
+ 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 65 of file mutt_lua.c.

66 {
67  mutt_debug(LL_DEBUG1, "lua runtime error: %s\n", lua_tostring(l, -1));
68  mutt_error("Lua runtime error: %s", lua_tostring(l, -1));
69  lua_pop(l, 1);
70  return -1;
71 }
Log at debug level 1.
Definition: logging.h:56
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
+ 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 79 of file mutt_lua.c.

80 {
81  mutt_debug(LL_DEBUG2, " * lua_mutt_call()\n");
82  struct Buffer *err = mutt_buffer_pool_get();
83  struct Buffer *token = mutt_buffer_pool_get();
84  char buf[1024] = { 0 };
85  const struct Command *cmd = NULL;
86  int rc = 0;
87 
88  if (lua_gettop(l) == 0)
89  {
90  luaL_error(l, "Error command argument required.");
91  return -1;
92  }
93 
94  cmd = mutt_command_get(lua_tostring(l, 1));
95  if (!cmd)
96  {
97  luaL_error(l, "Error command %s not found.", lua_tostring(l, 1));
98  return -1;
99  }
100 
101  for (int i = 2; i <= lua_gettop(l); i++)
102  {
103  const char *s = lua_tostring(l, i);
104  mutt_str_strncat(buf, sizeof(buf), s, mutt_str_strlen(s));
105  mutt_str_strncat(buf, sizeof(buf), " ", 1);
106  }
107 
108  struct Buffer expn = mutt_buffer_make(0);
109  expn.data = buf;
110  expn.dptr = buf;
111  expn.dsize = mutt_str_strlen(buf);
112 
113  if (cmd->func(token, &expn, cmd->data, err))
114  {
115  luaL_error(l, "NeoMutt error: %s", mutt_b2s(err));
116  rc = -1;
117  }
118  else
119  {
120  if (!lua_pushstring(l, mutt_b2s(err)))
121  handle_error(l);
122  else
123  rc++;
124  }
125 
126  mutt_buffer_pool_release(&token);
128  return rc;
129 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:100
intptr_t data
Data or flags to pass to the command.
Definition: mutt_commands.h:58
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:111
command_t func
Function to parse the command.
Definition: mutt_commands.h:57
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
A user-callable command.
Definition: mutt_commands.h:54
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:424
const struct Command * mutt_command_get(const char *s)
Get a Command by its name.
Definition: init.c:2624
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:65
Log at debug level 2.
Definition: logging.h:57
#define mutt_b2s(buf)
Definition: buffer.h:41
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
+ 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 137 of file mutt_lua.c.

138 {
139  const char *param = lua_tostring(l, -2);
140  mutt_debug(LL_DEBUG2, " * lua_mutt_set(%s)\n", param);
141 
142  if (mutt_str_startswith(param, "my_", CASE_MATCH))
143  {
144  const char *val = lua_tostring(l, -1);
145  myvar_set(param, val);
146  return 0;
147  }
148 
149  struct HashElem *he = cs_get_elem(Config, param);
150  if (!he)
151  {
152  luaL_error(l, "NeoMutt parameter not found %s", param);
153  return -1;
154  }
155 
156  struct ConfigDef *cdef = he->data;
157 
158  int rc = 0;
159  struct Buffer err = mutt_buffer_make(256);
160 
161  switch (DTYPE(cdef->type))
162  {
163  case DT_ADDRESS:
164  case DT_ENUM:
165  case DT_MBTABLE:
166  case DT_REGEX:
167  case DT_SLIST:
168  case DT_SORT:
169  case DT_STRING:
170  {
171  const char *value = lua_tostring(l, -1);
172  int rv = cs_he_string_set(Config, he, value, &err);
173  if (CSR_RESULT(rv) != CSR_SUCCESS)
174  rc = -1;
175  break;
176  }
177  case DT_NUMBER:
178  case DT_QUAD:
179  {
180  const intptr_t value = lua_tointeger(l, -1);
181  int rv = cs_he_native_set(Config, he, value, &err);
182  if (CSR_RESULT(rv) != CSR_SUCCESS)
183  rc = -1;
184  break;
185  }
186  case DT_BOOL:
187  {
188  const intptr_t value = lua_toboolean(l, -1);
189  int rv = cs_he_native_set(Config, he, value, &err);
190  if (CSR_RESULT(rv) != CSR_SUCCESS)
191  rc = -1;
192  break;
193  }
194  default:
195  luaL_error(l, "Unsupported NeoMutt parameter type %d for %s", DTYPE(cdef->type), param);
196  rc = -1;
197  break;
198  }
199 
200  mutt_buffer_dealloc(&err);
201  return rc;
202 }
#define CSR_RESULT(x)
Definition: set.h:62
#define DT_REGEX
regular expressions
Definition: types.h:37
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:39
WHERE struct ConfigSet * Config
Wrapper around the user&#39;s config settings.
Definition: globals.h:41
#define DT_SLIST
a list of strings
Definition: types.h:38
Match case when comparing strings.
Definition: string2.h:67
#define DT_MBTABLE
multibyte char table
Definition: types.h:34
Config item definition.
Definition: set.h:153
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:36
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:43
Log at debug level 2.
Definition: logging.h:57
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
int cs_he_native_set(const struct ConfigSet *cs, struct HashElem *he, intptr_t value, struct Buffer *err)
Natively set the value of a HashElem config item.
Definition: set.c:721
struct HashElem * cs_get_elem(const struct ConfigSet *cs, const char *name)
Get the HashElem representing a config item.
Definition: set.c:216
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:46
void myvar_set(const char *var, const char *val)
Set the value of a "my_" variable.
Definition: myvar.c:91
#define DT_ADDRESS
e-mail address
Definition: types.h:29
#define DT_STRING
a string
Definition: types.h:40
size_t mutt_str_startswith(const char *str, const char *prefix, enum CaseSensitivity cs)
Check whether a string starts with a prefix.
Definition: string.c:168
void * data
Definition: hash.h:46
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:156
#define DT_ENUM
an enumeration
Definition: types.h:31
The item stored in a Hash Table.
Definition: hash.h:42
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
#define DT_NUMBER
a number
Definition: types.h:35
#define DT_BOOL
boolean option
Definition: types.h:30
int cs_he_string_set(const struct ConfigSet *cs, struct HashElem *he, const char *value, struct Buffer *err)
Set a config item by string.
Definition: set.c:576
+ 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 210 of file mutt_lua.c.

211 {
212  const char *param = lua_tostring(l, -1);
213  mutt_debug(LL_DEBUG2, " * lua_mutt_get(%s)\n", param);
214 
215  if (mutt_str_startswith(param, "my_", CASE_MATCH))
216  {
217  const char *mv = myvar_get(param);
218  if (!mv)
219  {
220  luaL_error(l, "NeoMutt parameter not found %s", param);
221  return -1;
222  }
223 
224  lua_pushstring(l, mv);
225  return 1;
226  }
227 
228  struct HashElem *he = cs_get_elem(Config, param);
229  if (!he)
230  {
231  mutt_debug(LL_DEBUG2, " * error\n");
232  luaL_error(l, "NeoMutt parameter not found %s", param);
233  return -1;
234  }
235 
236  struct ConfigDef *cdef = he->data;
237 
238  switch (DTYPE(cdef->type))
239  {
240  case DT_ADDRESS:
241  case DT_ENUM:
242  case DT_MBTABLE:
243  case DT_REGEX:
244  case DT_SLIST:
245  case DT_SORT:
246  case DT_STRING:
247  {
248  struct Buffer value = mutt_buffer_make(256);
249  int rc = cs_he_string_get(Config, he, &value);
250  if (CSR_RESULT(rc) != CSR_SUCCESS)
251  {
252  mutt_buffer_dealloc(&value);
253  return -1;
254  }
255 
256  struct Buffer escaped = mutt_buffer_make(256);
257  escape_string(&escaped, value.data);
258  lua_pushstring(l, escaped.data);
259  mutt_buffer_dealloc(&value);
260  mutt_buffer_dealloc(&escaped);
261  return 1;
262  }
263  case DT_QUAD:
264  lua_pushinteger(l, *(unsigned char *) cdef->var);
265  return 1;
266  case DT_NUMBER:
267  lua_pushinteger(l, (signed short) *((unsigned long *) cdef->var));
268  return 1;
269  case DT_BOOL:
270  lua_pushboolean(l, *((bool *) cdef->var));
271  return 1;
272  default:
273  luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
274  return -1;
275  }
276 }
#define CSR_RESULT(x)
Definition: set.h:62
#define DT_REGEX
regular expressions
Definition: types.h:37
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:39
WHERE struct ConfigSet * Config
Wrapper around the user&#39;s config settings.
Definition: globals.h:41
#define DT_SLIST
a list of strings
Definition: types.h:38
Match case when comparing strings.
Definition: string2.h:67
#define DT_MBTABLE
multibyte char table
Definition: types.h:34
Config item definition.
Definition: set.h:153
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:36
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:43
Log at debug level 2.
Definition: logging.h:57
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
struct HashElem * cs_get_elem(const struct ConfigSet *cs, const char *name)
Get the HashElem representing a config item.
Definition: set.c:216
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:46
#define DT_ADDRESS
e-mail address
Definition: types.h:29
#define DT_STRING
a string
Definition: types.h:40
char * data
Pointer to data.
Definition: buffer.h:35
const char * myvar_get(const char *var)
Get the value of a "my_" variable.
Definition: myvar.c:73
size_t mutt_str_startswith(const char *str, const char *prefix, enum CaseSensitivity cs)
Check whether a string starts with a prefix.
Definition: string.c:168
int cs_he_string_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition: set.c:650
void * data
Definition: hash.h:46
void * var
Pointer to the global variable.
Definition: set.h:157
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:156
#define DT_ENUM
an enumeration
Definition: types.h:31
size_t escape_string(struct Buffer *buf, const char *src)
Write a string to a buffer, escaping special characters.
Definition: dump.c:46
The item stored in a Hash Table.
Definition: hash.h:42
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
#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 284 of file mutt_lua.c.

285 {
286  mutt_debug(LL_DEBUG2, " * lua_mutt_enter()\n");
287  struct Buffer *err = mutt_buffer_pool_get();
288  struct Buffer *token = mutt_buffer_pool_get();
289  char *buf = mutt_str_strdup(lua_tostring(l, -1));
290  int rc = 0;
291 
292  if (mutt_parse_rc_line(buf, token, err))
293  {
294  luaL_error(l, "NeoMutt error: %s", mutt_b2s(err));
295  rc = -1;
296  }
297  else
298  {
299  if (!lua_pushstring(l, mutt_b2s(err)))
300  handle_error(l);
301  else
302  rc++;
303  }
304 
305  FREE(&buf);
306  mutt_buffer_pool_release(&token);
308 
309  return rc;
310 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:100
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:111
String manipulation buffer.
Definition: buffer.h:33
static int handle_error(lua_State *l)
Handle an error in the Lua interpreter.
Definition: mutt_lua.c:65
Log at debug level 2.
Definition: logging.h:57
#define mutt_b2s(buf)
Definition: buffer.h:41
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
#define FREE(x)
Definition: memory.h:40
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
enum CommandResult mutt_parse_rc_line(char *line, struct Buffer *token, struct Buffer *err)
Parse a line of user config.
Definition: init.c:3232
+ 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 317 of file mutt_lua.c.

318 {
319  mutt_debug(LL_DEBUG2, " * lua_mutt_message()\n");
320  const char *msg = lua_tostring(l, -1);
321  if (msg)
322  mutt_message(msg);
323  return 0;
324 }
#define mutt_message(...)
Definition: logging.h:83
Log at debug level 2.
Definition: logging.h:57
#define mutt_debug(LEVEL,...)
Definition: logging.h:81

◆ 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 331 of file mutt_lua.c.

332 {
333  mutt_debug(LL_DEBUG2, " * lua_mutt_error()\n");
334  const char *msg = lua_tostring(l, -1);
335  if (msg)
336  mutt_error(msg);
337  return 0;
338 }
Log at debug level 2.
Definition: logging.h:57
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81

◆ 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 345 of file mutt_lua.c.

346 {
347  lua_State *l = (lua_State *) p;
348  char buf[1024];
349  snprintf(buf, sizeof(buf), "mutt.command.%s = function (...); mutt.call('%s', ...); end",
350  cmd->name, cmd->name);
351  (void) luaL_dostring(l, buf);
352 }
const char * name
Name of the command.
Definition: mutt_commands.h:56
+ 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 373 of file mutt_lua.c.

374 {
375  mutt_debug(LL_DEBUG2, " * luaopen_mutt()\n");
376  luaL_newlib(l, luaMuttDecl);
377  int lib_idx = lua_gettop(l);
378  /* table_idx, key value, value's type */
379  lua_add_lib_member(l, lib_idx, "VERSION", mutt_make_version(), lua_pushstring);
380  lua_add_lib_member(l, lib_idx, "QUAD_YES", MUTT_YES, lua_pushinteger);
381  lua_add_lib_member(l, lib_idx, "QUAD_NO", MUTT_NO, lua_pushinteger);
382  lua_add_lib_member(l, lib_idx, "QUAD_ASKYES", MUTT_ASKYES, lua_pushinteger);
383  lua_add_lib_member(l, lib_idx, "QUAD_ASKNO", MUTT_ASKNO, lua_pushinteger);
384  return 1;
385 }
#define lua_add_lib_member(LUA, TABLE, KEY, VALUE, DATATYPE_HANDLER)
Definition: mutt_lua.c:363
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39
Ask the user, defaulting to &#39;Yes&#39;.
Definition: quad.h:41
Log at debug level 2.
Definition: logging.h:57
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:38
Ask the user, defaulting to &#39;No&#39;.
Definition: quad.h:40
static const luaL_Reg luaMuttDecl[]
Definition: mutt_lua.c:356
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:1560
+ 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 391 of file mutt_lua.c.

392 {
393  luaL_requiref(l, "mutt", luaopen_mutt_decl, 1);
394  (void) luaL_dostring(l, "mutt.command = {}");
396 }
static int luaopen_mutt_decl(lua_State *l)
Declare some NeoMutt types to the Lua interpreter.
Definition: mutt_lua.c:373
void mutt_commands_apply(void *data, void(*application)(void *, const struct Command *))
static void lua_expose_command(void *p, const struct Command *cmd)
Expose a NeoMutt command to the Lua interpreter.
Definition: mutt_lua.c:345
+ 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
trueIf successful

Definition at line 403 of file mutt_lua.c.

404 {
405  if (!l)
406  return false;
407  if (*l)
408  return true;
409 
410  mutt_debug(LL_DEBUG2, " * lua_init()\n");
411  *l = luaL_newstate();
412 
413  if (!*l)
414  {
415  mutt_error(_("Error: Couldn't load the lua interpreter"));
416  return false;
417  }
418 
419  lua_atpanic(*l, handle_panic);
420 
421  /* load various Lua libraries */
422  luaL_openlibs(*l);
423  luaopen_mutt(*l);
424 
425  return true;
426 }
static int handle_panic(lua_State *l)
Handle a panic in the Lua interpreter.
Definition: mutt_lua.c:52
#define _(a)
Definition: message.h:28
Log at debug level 2.
Definition: logging.h:57
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
static void luaopen_mutt(lua_State *l)
Expose a &#39;Mutt&#39; object to the Lua interpreter.
Definition: mutt_lua.c:391
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_lua_parse()

enum CommandResult mutt_lua_parse ( struct Buffer buf,
struct Buffer s,
unsigned long  data,
struct Buffer err 
)

Parse the 'lua' command - Implements command_t.

Definition at line 431 of file mutt_lua.c.

433 {
434  lua_init(&LuaState);
435  mutt_debug(LL_DEBUG2, " * mutt_lua_parse(%s)\n", buf->data);
436 
437  if (luaL_dostring(LuaState, s->dptr))
438  {
439  mutt_debug(LL_DEBUG2, " * %s -> failure\n", s->dptr);
440  mutt_buffer_printf(err, "%s: %s", s->dptr, lua_tostring(LuaState, -1));
441  /* pop error message from the stack */
442  lua_pop(LuaState, 1);
443  return MUTT_CMD_ERROR;
444  }
445  mutt_debug(LL_DEBUG2, " * %s -> success\n", s->dptr);
446  mutt_buffer_reset(s); // Clear the rest of the line
447  return MUTT_CMD_SUCCESS;
448 }
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
Error: Can&#39;t help the user.
Definition: mutt_commands.h:35
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
lua_State * LuaState
Definition: mutt_lua.c:354
static bool lua_init(lua_State **l)
Initialise a Lua State.
Definition: mutt_lua.c:403
Log at debug level 2.
Definition: logging.h:57
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
Success: Command worked.
Definition: mutt_commands.h:37
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
+ Here is the call graph for this function:

◆ mutt_lua_source_file()

enum CommandResult mutt_lua_source_file ( struct Buffer buf,
struct Buffer s,
unsigned long  data,
struct Buffer err 
)

Parse the 'lua-source' command - Implements command_t.

Definition at line 453 of file mutt_lua.c.

455 {
456  mutt_debug(LL_DEBUG2, " * mutt_lua_source()\n");
457 
458  lua_init(&LuaState);
459 
460  char path[PATH_MAX];
461 
462  if (mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS) != 0)
463  {
464  mutt_buffer_printf(err, _("source: error at %s"), s->dptr);
465  return MUTT_CMD_ERROR;
466  }
467  if (MoreArgs(s))
468  {
469  mutt_buffer_printf(err, _("%s: too many arguments"), "source");
470  return MUTT_CMD_WARNING;
471  }
472  mutt_str_strfcpy(path, buf->data, sizeof(path));
473  mutt_expand_path(path, sizeof(path));
474 
475  if (luaL_dofile(LuaState, path))
476  {
477  mutt_error(_("Couldn't source lua source: %s"), lua_tostring(LuaState, -1));
478  lua_pop(LuaState, 1);
479  return MUTT_CMD_ERROR;
480  }
481  return MUTT_CMD_SUCCESS;
482 }
Error: Can&#39;t help the user.
Definition: mutt_commands.h:35
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
lua_State * LuaState
Definition: mutt_lua.c:354
char * mutt_expand_path(char *buf, size_t buflen)
Create the canonical path.
Definition: muttlib.c:134
#define MoreArgs(buf)
Definition: buffer.h:43
static bool lua_init(lua_State **l)
Initialise a Lua State.
Definition: mutt_lua.c:403
Log at debug level 2.
Definition: logging.h:57
#define PATH_MAX
Definition: mutt.h:50
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:2655
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
size_t mutt_str_strfcpy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:750
Success: Command worked.
Definition: mutt_commands.h:37
Warning: Help given to the user.
Definition: mutt_commands.h:36
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:76
+ Here is the call graph for this function:

Variable Documentation

◆ LuaState

lua_State* LuaState = NULL

Definition at line 354 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:137
static int lua_mutt_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition: mutt_lua.c:284
static int lua_mutt_message(lua_State *l)
Display a message in Neomutt.
Definition: mutt_lua.c:317
static int lua_mutt_call(lua_State *l)
Call a NeoMutt command by name.
Definition: mutt_lua.c:79
static int lua_mutt_error(lua_State *l)
Display an error in Neomutt.
Definition: mutt_lua.c:331
static int lua_mutt_get(lua_State *l)
Get a NeoMutt variable.
Definition: mutt_lua.c:210

Definition at line 356 of file mutt_lua.c.