NeoMutt  2018-07-16 +2225-8687db
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 <stdio.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "mutt.h"
#include "mutt_lua.h"
#include "globals.h"
#include "mutt_commands.h"
#include "mutt_options.h"
#include "muttlib.h"
#include "mx.h"
#include "myvar.h"

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

static const luaL_Reg luaMuttDecl []
 
lua_State * Lua = NULL
 

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

#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

static int handle_panic ( lua_State *  l)
static

Handle a panic in the Lua interpreter.

Parameters
lLua State
Return values
-1Always

Definition at line 54 of file mutt_lua.c.

55 {
56  mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
57  mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
58  lua_pop(l, 1);
59  return -1;
60 }
Log at debug level 1.
Definition: logging.h:56
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
static int handle_error ( lua_State *  l)
static

Handle an error in the Lua interpreter.

Parameters
lLua State
Return values
-1Always

Definition at line 67 of file mutt_lua.c.

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

82 {
83  mutt_debug(LL_DEBUG2, " * lua_mutt_call()\n");
84  struct Buffer *err = mutt_buffer_pool_get();
85  struct Buffer *token = mutt_buffer_pool_get();
86  char buf[1024] = { 0 };
87  const struct Command *cmd = NULL;
88  int rc = 0;
89 
90  if (lua_gettop(l) == 0)
91  {
92  luaL_error(l, "Error command argument required.");
93  return -1;
94  }
95 
96  cmd = mutt_command_get(lua_tostring(l, 1));
97  if (!cmd)
98  {
99  luaL_error(l, "Error command %s not found.", lua_tostring(l, 1));
100  return -1;
101  }
102 
103  for (int i = 2; i <= lua_gettop(l); i++)
104  {
105  const char *s = lua_tostring(l, i);
106  mutt_str_strncat(buf, sizeof(buf), s, mutt_str_strlen(s));
107  mutt_str_strncat(buf, sizeof(buf), " ", 1);
108  }
109 
110  struct Buffer expn = { 0 };
111  expn.data = buf;
112  expn.dptr = buf;
113  expn.dsize = mutt_str_strlen(buf);
114 
115  if (cmd->func(token, &expn, cmd->data, err))
116  {
117  luaL_error(l, "NeoMutt error: %s", mutt_b2s(err));
118  rc = -1;
119  }
120  else
121  {
122  if (!lua_pushstring(l, mutt_b2s(err)))
123  handle_error(l);
124  else
125  rc++;
126  }
127 
128  mutt_buffer_pool_release(&token);
130  return rc;
131 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:75
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:86
command_t func
Function to parse the command.
Definition: mutt_commands.h:55
String manipulation buffer.
Definition: buffer.h:33
A user-callable command.
Definition: mutt_commands.h:52
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:2583
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:67
Log at debug level 2.
Definition: logging.h:57
#define mutt_b2s(buf)
Definition: buffer.h:42
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
unsigned long data
Data or flags to pass to the command.
Definition: mutt_commands.h:56
static int lua_mutt_set ( lua_State *  l)
static

Set a NeoMutt variable.

Parameters
lLua State
Return values
0Success
-1Error

Definition at line 139 of file mutt_lua.c.

140 {
141  const char *param = lua_tostring(l, -2);
142  mutt_debug(LL_DEBUG2, " * lua_mutt_set(%s)\n", param);
143 
144  if (mutt_str_startswith(param, "my_", CASE_MATCH))
145  {
146  const char *val = lua_tostring(l, -1);
147  myvar_set(param, val);
148  return 0;
149  }
150 
151  struct HashElem *he = cs_get_elem(Config, param);
152  if (!he)
153  {
154  luaL_error(l, "NeoMutt parameter not found %s", param);
155  return -1;
156  }
157 
158  struct ConfigDef *cdef = he->data;
159 
160  int rc = 0;
161  struct Buffer *err = mutt_buffer_alloc(256);
162 
163  switch (DTYPE(cdef->type))
164  {
165  case DT_ADDRESS:
166  case DT_ENUM:
167  case DT_MBTABLE:
168  case DT_REGEX:
169  case DT_SLIST:
170  case DT_SORT:
171  case DT_STRING:
172  {
173  const char *value = lua_tostring(l, -1);
174  int rv = cs_he_string_set(Config, he, value, err);
175  if (CSR_RESULT(rv) != CSR_SUCCESS)
176  rc = -1;
177  break;
178  }
179  case DT_NUMBER:
180  case DT_QUAD:
181  {
182  const intptr_t value = lua_tointeger(l, -1);
183  int rv = cs_he_native_set(Config, he, value, err);
184  if (CSR_RESULT(rv) != CSR_SUCCESS)
185  rc = -1;
186  break;
187  }
188  case DT_BOOL:
189  {
190  const intptr_t value = lua_toboolean(l, -1);
191  int rv = cs_he_native_set(Config, he, value, err);
192  if (CSR_RESULT(rv) != CSR_SUCCESS)
193  rc = -1;
194  break;
195  }
196  default:
197  luaL_error(l, "Unsupported NeoMutt parameter type %d for %s", DTYPE(cdef->type), param);
198  rc = -1;
199  break;
200  }
201 
202  mutt_buffer_free(&err);
203  return rc;
204 }
#define CSR_RESULT(x)
Definition: set.h:62
#define DT_REGEX
regular expressions
Definition: types.h:37
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:39
#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
struct Buffer * mutt_buffer_alloc(size_t size)
Create a new Buffer.
Definition: buffer.c:296
Log at debug level 2.
Definition: logging.h:57
void mutt_buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: buffer.c:134
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:720
struct HashElem * cs_get_elem(const struct ConfigSet *cs, const char *name)
Get the HashElem representing a config item.
Definition: set.c:215
#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:60
#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:575
static int lua_mutt_get ( lua_State *  l)
static

Get a NeoMutt variable.

Parameters
lLua State
Return values
1Success
-1Error

Definition at line 212 of file mutt_lua.c.

213 {
214  const char *param = lua_tostring(l, -1);
215  mutt_debug(LL_DEBUG2, " * lua_mutt_get(%s)\n", param);
216 
217  if (mutt_str_startswith(param, "my_", CASE_MATCH))
218  {
219  const char *mv = myvar_get(param);
220  if (!mv)
221  {
222  luaL_error(l, "NeoMutt parameter not found %s", param);
223  return -1;
224  }
225 
226  lua_pushstring(l, mv);
227  return 1;
228  }
229 
230  struct HashElem *he = cs_get_elem(Config, param);
231  if (!he)
232  {
233  mutt_debug(LL_DEBUG2, " * error\n");
234  luaL_error(l, "NeoMutt parameter not found %s", param);
235  return -1;
236  }
237 
238  struct ConfigDef *cdef = he->data;
239 
240  switch (DTYPE(cdef->type))
241  {
242  case DT_ADDRESS:
243  case DT_ENUM:
244  case DT_MBTABLE:
245  case DT_REGEX:
246  case DT_SLIST:
247  case DT_SORT:
248  case DT_STRING:
249  {
250  struct Buffer *value = mutt_buffer_alloc(256);
251  int rc = cs_he_string_get(Config, he, value);
252  if (CSR_RESULT(rc) != CSR_SUCCESS)
253  {
254  mutt_buffer_free(&value);
255  return -1;
256  }
257 
258  struct Buffer *escaped = mutt_buffer_alloc(256);
259  escape_string(escaped, value->data);
260  lua_pushstring(l, escaped->data);
261  mutt_buffer_free(&value);
262  mutt_buffer_free(&escaped);
263  return 1;
264  }
265  case DT_QUAD:
266  lua_pushinteger(l, *(unsigned char *) cdef->var);
267  return 1;
268  case DT_NUMBER:
269  lua_pushinteger(l, (signed short) *((unsigned long *) cdef->var));
270  return 1;
271  case DT_BOOL:
272  lua_pushboolean(l, *((bool *) cdef->var));
273  return 1;
274  default:
275  luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
276  return -1;
277  }
278 }
#define CSR_RESULT(x)
Definition: set.h:62
#define DT_REGEX
regular expressions
Definition: types.h:37
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:39
#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
struct Buffer * mutt_buffer_alloc(size_t size)
Create a new Buffer.
Definition: buffer.c:296
Log at debug level 2.
Definition: logging.h:57
void mutt_buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: buffer.c:134
struct HashElem * cs_get_elem(const struct ConfigSet *cs, const char *name)
Get the HashElem representing a config item.
Definition: set.c:215
#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:42
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:649
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:47
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
static int lua_mutt_enter ( lua_State *  l)
static

Execute NeoMutt config from Lua.

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

Definition at line 286 of file mutt_lua.c.

287 {
288  mutt_debug(LL_DEBUG2, " * lua_mutt_enter()\n");
289  struct Buffer *err = mutt_buffer_pool_get();
290  struct Buffer *token = mutt_buffer_pool_get();
291  char *buf = mutt_str_strdup(lua_tostring(l, -1));
292  int rc = 0;
293 
294  if (mutt_parse_rc_line(buf, token, err))
295  {
296  luaL_error(l, "NeoMutt error: %s", mutt_b2s(err));
297  rc = -1;
298  }
299  else
300  {
301  if (!lua_pushstring(l, mutt_b2s(err)))
302  handle_error(l);
303  else
304  rc++;
305  }
306 
307  FREE(&buf);
308  mutt_buffer_pool_release(&token);
310 
311  return rc;
312 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:75
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:86
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:67
Log at debug level 2.
Definition: logging.h:57
#define mutt_b2s(buf)
Definition: buffer.h:42
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:3211
static int lua_mutt_message ( lua_State *  l)
static

Display a message in Neomutt.

Parameters
lLua State
Return values
0Always

Definition at line 319 of file mutt_lua.c.

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

Display an error in Neomutt.

Parameters
lLua State
Return values
0Always

Definition at line 333 of file mutt_lua.c.

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

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

437 {
438  lua_init(&Lua);
439  mutt_debug(LL_DEBUG2, " * mutt_lua_parse(%s)\n", buf->data);
440 
441  if (luaL_dostring(Lua, s->dptr))
442  {
443  mutt_debug(LL_DEBUG2, " * %s -> failure\n", s->dptr);
444  mutt_buffer_printf(err, "%s: %s", s->dptr, lua_tostring(Lua, -1));
445  /* pop error message from the stack */
446  lua_pop(Lua, 1);
447  return MUTT_CMD_ERROR;
448  }
449  mutt_debug(LL_DEBUG2, " * %s -> success\n", s->dptr);
450  mutt_buffer_reset(s); // Clear the rest of the line
451  return MUTT_CMD_SUCCESS;
452 }
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
lua_State * Lua
Definition: mutt_lua.c:430
Error: Can&#39;t help the user.
Definition: mutt_commands.h:33
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:191
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:35
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
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 457 of file mutt_lua.c.

459 {
460  mutt_debug(LL_DEBUG2, " * mutt_lua_source()\n");
461 
462  lua_init(&Lua);
463 
464  char path[PATH_MAX];
465 
466  if (mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS) != 0)
467  {
468  mutt_buffer_printf(err, _("source: error at %s"), s->dptr);
469  return MUTT_CMD_ERROR;
470  }
471  if (MoreArgs(s))
472  {
473  mutt_buffer_printf(err, _("%s: too many arguments"), "source");
474  return MUTT_CMD_WARNING;
475  }
476  mutt_str_strfcpy(path, buf->data, sizeof(path));
477  mutt_expand_path(path, sizeof(path));
478 
479  if (luaL_dofile(Lua, path))
480  {
481  mutt_error(_("Couldn't source lua source: %s"), lua_tostring(Lua, -1));
482  lua_pop(Lua, 1);
483  return MUTT_CMD_ERROR;
484  }
485  return MUTT_CMD_SUCCESS;
486 }
lua_State * Lua
Definition: mutt_lua.c:430
Error: Can&#39;t help the user.
Definition: mutt_commands.h:33
#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:191
char * mutt_expand_path(char *buf, size_t buflen)
Create the canonical path.
Definition: muttlib.c:128
#define MoreArgs(buf)
Definition: buffer.h:44
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:2614
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:35
Warning: Help given to the user.
Definition: mutt_commands.h:34
#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

Variable Documentation

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:139
static int lua_mutt_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition: mutt_lua.c:286
static int lua_mutt_message(lua_State *l)
Display a message in Neomutt.
Definition: mutt_lua.c:319
static int lua_mutt_call(lua_State *l)
Call a NeoMutt command by name.
Definition: mutt_lua.c:81
static int lua_mutt_error(lua_State *l)
Display an error in Neomutt.
Definition: mutt_lua.c:333
static int lua_mutt_get(lua_State *l)
Get a NeoMutt variable.
Definition: mutt_lua.c:212

Definition at line 356 of file mutt_lua.c.

lua_State* Lua = NULL

Definition at line 430 of file mutt_lua.c.