NeoMutt  2019-12-07-60-g0cfa53
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 "core/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_COMPAT_ALL
 
#define LUA_COMPAT_5_1
 
#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_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.

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

61 {
62  mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
63  mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
64  lua_pop(l, 1);
65  return -1;
66 }
Log at debug level 1.
Definition: logging.h:40
#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 73 of file mutt_lua.c.

74 {
75  mutt_debug(LL_DEBUG1, "lua runtime error: %s\n", lua_tostring(l, -1));
76  mutt_error("Lua runtime error: %s", lua_tostring(l, -1));
77  lua_pop(l, 1);
78  return -1;
79 }
Log at debug level 1.
Definition: logging.h:40
#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 87 of file mutt_lua.c.

88 {
89  mutt_debug(LL_DEBUG2, " * lua_mutt_call()\n");
90  struct Buffer *err = mutt_buffer_pool_get();
91  struct Buffer *token = mutt_buffer_pool_get();
92  char buf[1024] = { 0 };
93  const struct Command *cmd = NULL;
94  int rc = 0;
95 
96  if (lua_gettop(l) == 0)
97  {
98  luaL_error(l, "Error command argument required.");
99  return -1;
100  }
101 
102  cmd = mutt_command_get(lua_tostring(l, 1));
103  if (!cmd)
104  {
105  luaL_error(l, "Error command %s not found.", lua_tostring(l, 1));
106  return -1;
107  }
108 
109  for (int i = 2; i <= lua_gettop(l); i++)
110  {
111  const char *s = lua_tostring(l, i);
112  mutt_str_strncat(buf, sizeof(buf), s, mutt_str_strlen(s));
113  mutt_str_strncat(buf, sizeof(buf), " ", 1);
114  }
115 
116  struct Buffer expn = mutt_buffer_make(0);
117  expn.data = buf;
118  expn.dptr = buf;
119  expn.dsize = mutt_str_strlen(buf);
120 
121  if (cmd->func(token, &expn, cmd->data, err))
122  {
123  luaL_error(l, "NeoMutt error: %s", mutt_b2s(err));
124  rc = -1;
125  }
126  else
127  {
128  if (!lua_pushstring(l, mutt_b2s(err)))
129  handle_error(l);
130  else
131  rc++;
132  }
133 
134  mutt_buffer_pool_release(&token);
136  return rc;
137 }
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:58
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
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:2643
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:73
Log at debug level 2.
Definition: logging.h:41
#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 145 of file mutt_lua.c.

146 {
147  const char *param = lua_tostring(l, -2);
148  mutt_debug(LL_DEBUG2, " * lua_mutt_set(%s)\n", param);
149 
150  if (mutt_str_startswith(param, "my_", CASE_MATCH))
151  {
152  const char *val = lua_tostring(l, -1);
153  myvar_set(param, val);
154  return 0;
155  }
156 
157  struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
158  if (!he)
159  {
160  luaL_error(l, "NeoMutt parameter not found %s", param);
161  return -1;
162  }
163 
164  struct ConfigDef *cdef = he->data;
165 
166  int rc = 0;
167  struct Buffer err = mutt_buffer_make(256);
168 
169  switch (DTYPE(cdef->type))
170  {
171  case DT_ADDRESS:
172  case DT_ENUM:
173  case DT_MBTABLE:
174  case DT_REGEX:
175  case DT_SLIST:
176  case DT_SORT:
177  case DT_STRING:
178  {
179  const char *value = lua_tostring(l, -1);
180  size_t val_size = lua_strlen(l, -1);
181  struct Buffer value_buf = mutt_buffer_make(val_size);
182  mutt_buffer_strcpy_n(&value_buf, value, val_size);
183  if (IS_PATH(he))
184  mutt_buffer_expand_path(&value_buf);
185 
186  int rv = cs_subset_he_string_set(NeoMutt->sub, he, value_buf.data, &err);
187  mutt_buffer_dealloc(&value_buf);
188  if (CSR_RESULT(rv) != CSR_SUCCESS)
189  rc = -1;
190  break;
191  }
192  case DT_NUMBER:
193  case DT_QUAD:
194  {
195  const intptr_t value = lua_tointeger(l, -1);
196  int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, &err);
197  if (CSR_RESULT(rv) != CSR_SUCCESS)
198  rc = -1;
199  break;
200  }
201  case DT_BOOL:
202  {
203  const intptr_t value = lua_toboolean(l, -1);
204  int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, &err);
205  if (CSR_RESULT(rv) != CSR_SUCCESS)
206  rc = -1;
207  break;
208  }
209  default:
210  luaL_error(l, "Unsupported NeoMutt parameter type %d for %s", DTYPE(cdef->type), param);
211  rc = -1;
212  break;
213  }
214 
215  mutt_buffer_dealloc(&err);
216  return rc;
217 }
#define IS_PATH(x)
Definition: types.h:55
#define CSR_RESULT(x)
Definition: set.h:53
#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
#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
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition: subset.c:119
Config item definition.
Definition: set.h:134
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:35
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:43
Log at debug level 2.
Definition: logging.h:41
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:324
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:37
#define DT_ADDRESS
e-mail address
Definition: types.h:29
#define DT_STRING
a string
Definition: types.h:40
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:91
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:137
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:342
#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
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:38
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:221
#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_get()

static int lua_mutt_get ( lua_State *  l)
static

Get a NeoMutt variable.

Parameters
lLua State
Return values
1Success
-1Error

Definition at line 225 of file mutt_lua.c.

226 {
227  const char *param = lua_tostring(l, -1);
228  mutt_debug(LL_DEBUG2, " * lua_mutt_get(%s)\n", param);
229 
230  if (mutt_str_startswith(param, "my_", CASE_MATCH))
231  {
232  const char *mv = myvar_get(param);
233  if (!mv)
234  {
235  luaL_error(l, "NeoMutt parameter not found %s", param);
236  return -1;
237  }
238 
239  lua_pushstring(l, mv);
240  return 1;
241  }
242 
243  struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
244  if (!he)
245  {
246  mutt_debug(LL_DEBUG2, " * error\n");
247  luaL_error(l, "NeoMutt parameter not found %s", param);
248  return -1;
249  }
250 
251  struct ConfigDef *cdef = he->data;
252 
253  switch (DTYPE(cdef->type))
254  {
255  case DT_ADDRESS:
256  case DT_ENUM:
257  case DT_MBTABLE:
258  case DT_REGEX:
259  case DT_SLIST:
260  case DT_SORT:
261  case DT_STRING:
262  {
263  struct Buffer value = mutt_buffer_make(256);
264  int rc = cs_subset_he_string_get(NeoMutt->sub, he, &value);
265  if (CSR_RESULT(rc) != CSR_SUCCESS)
266  {
267  mutt_buffer_dealloc(&value);
268  return -1;
269  }
270 
271  struct Buffer escaped = mutt_buffer_make(256);
272  escape_string(&escaped, value.data);
273  lua_pushstring(l, escaped.data);
274  mutt_buffer_dealloc(&value);
275  mutt_buffer_dealloc(&escaped);
276  return 1;
277  }
278  case DT_QUAD:
279  lua_pushinteger(l, *(unsigned char *) cdef->var);
280  return 1;
281  case DT_NUMBER:
282  lua_pushinteger(l, (signed short) *((unsigned long *) cdef->var));
283  return 1;
284  case DT_BOOL:
285  lua_pushboolean(l, *((bool *) cdef->var));
286  return 1;
287  default:
288  luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
289  return -1;
290  }
291 }
#define CSR_RESULT(x)
Definition: set.h:53
#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
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:292
#define DT_SORT
sorting methods
Definition: types.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
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition: subset.c:119
Config item definition.
Definition: set.h:134
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:35
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:43
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 CSR_SUCCESS
Action completed successfully.
Definition: set.h:37
#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
void * data
Definition: hash.h:46
void * var
Pointer to the global variable.
Definition: set.h:138
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:137
#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
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:38
#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 299 of file mutt_lua.c.

300 {
301  mutt_debug(LL_DEBUG2, " * lua_mutt_enter()\n");
302  struct Buffer *err = mutt_buffer_pool_get();
303  struct Buffer *token = mutt_buffer_pool_get();
304  char *buf = mutt_str_strdup(lua_tostring(l, -1));
305  int rc = 0;
306 
307  if (mutt_parse_rc_line(buf, token, err))
308  {
309  luaL_error(l, "NeoMutt error: %s", mutt_b2s(err));
310  rc = -1;
311  }
312  else
313  {
314  if (!lua_pushstring(l, mutt_b2s(err)))
315  handle_error(l);
316  else
317  rc++;
318  }
319 
320  FREE(&buf);
321  mutt_buffer_pool_release(&token);
323 
324  return rc;
325 }
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
static int handle_error(lua_State *l)
Handle an error in the Lua interpreter.
Definition: mutt_lua.c:73
Log at debug level 2.
Definition: logging.h:41
#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:3252
+ 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 332 of file mutt_lua.c.

333 {
334  mutt_debug(LL_DEBUG2, " * lua_mutt_message()\n");
335  const char *msg = lua_tostring(l, -1);
336  if (msg)
337  mutt_message(msg);
338  return 0;
339 }
#define mutt_message(...)
Definition: logging.h:83
Log at debug level 2.
Definition: logging.h:41
#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 346 of file mutt_lua.c.

347 {
348  mutt_debug(LL_DEBUG2, " * lua_mutt_error()\n");
349  const char *msg = lua_tostring(l, -1);
350  if (msg)
351  mutt_error(msg);
352  return 0;
353 }
Log at debug level 2.
Definition: logging.h:41
#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 360 of file mutt_lua.c.

361 {
362  lua_State *l = (lua_State *) p;
363  char buf[1024];
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: 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 388 of file mutt_lua.c.

389 {
390  mutt_debug(LL_DEBUG2, " * luaopen_mutt()\n");
391  luaL_newlib(l, luaMuttDecl);
392  int lib_idx = lua_gettop(l);
393  /* table_idx, key value, value's type */
394  lua_add_lib_member(l, lib_idx, "VERSION", mutt_make_version(), lua_pushstring);
395  lua_add_lib_member(l, lib_idx, "QUAD_YES", MUTT_YES, lua_pushinteger);
396  lua_add_lib_member(l, lib_idx, "QUAD_NO", MUTT_NO, lua_pushinteger);
397  lua_add_lib_member(l, lib_idx, "QUAD_ASKYES", MUTT_ASKYES, lua_pushinteger);
398  lua_add_lib_member(l, lib_idx, "QUAD_ASKNO", MUTT_ASKNO, lua_pushinteger);
399  return 1;
400 }
#define lua_add_lib_member(LUA, TABLE, KEY, VALUE, DATATYPE_HANDLER)
Definition: mutt_lua.c:378
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:40
Ask the user, defaulting to &#39;Yes&#39;.
Definition: quad.h:42
Log at debug level 2.
Definition: logging.h:41
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:39
Ask the user, defaulting to &#39;No&#39;.
Definition: quad.h:41
static const luaL_Reg luaMuttDecl[]
Definition: mutt_lua.c:371
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:1559
+ 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 406 of file mutt_lua.c.

407 {
408  luaL_requiref(l, "mutt", luaopen_mutt_decl, 1);
409  (void) luaL_dostring(l, "mutt.command = {}");
411 }
static int luaopen_mutt_decl(lua_State *l)
Declare some NeoMutt types to the Lua interpreter.
Definition: mutt_lua.c:388
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:360
+ 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 418 of file mutt_lua.c.

419 {
420  if (!l)
421  return false;
422  if (*l)
423  return true;
424 
425  mutt_debug(LL_DEBUG2, " * lua_init()\n");
426  *l = luaL_newstate();
427 
428  if (!*l)
429  {
430  mutt_error(_("Error: Couldn't load the lua interpreter"));
431  return false;
432  }
433 
434  lua_atpanic(*l, handle_panic);
435 
436  /* load various Lua libraries */
437  luaL_openlibs(*l);
438  luaopen_mutt(*l);
439 
440  return true;
441 }
static int handle_panic(lua_State *l)
Handle a panic in the Lua interpreter.
Definition: mutt_lua.c:60
#define _(a)
Definition: message.h:28
Log at debug level 2.
Definition: logging.h:41
#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:406
+ 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 446 of file mutt_lua.c.

448 {
449  lua_init(&LuaState);
450  mutt_debug(LL_DEBUG2, " * mutt_lua_parse(%s)\n", buf->data);
451 
452  if (luaL_dostring(LuaState, s->dptr))
453  {
454  mutt_debug(LL_DEBUG2, " * %s -> failure\n", s->dptr);
455  mutt_buffer_printf(err, "%s: %s", s->dptr, lua_tostring(LuaState, -1));
456  /* pop error message from the stack */
457  lua_pop(LuaState, 1);
458  return MUTT_CMD_ERROR;
459  }
460  mutt_debug(LL_DEBUG2, " * %s -> success\n", s->dptr);
461  mutt_buffer_reset(s); // Clear the rest of the line
462  return MUTT_CMD_SUCCESS;
463 }
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:369
static bool lua_init(lua_State **l)
Initialise a Lua State.
Definition: mutt_lua.c:418
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
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 468 of file mutt_lua.c.

470 {
471  mutt_debug(LL_DEBUG2, " * mutt_lua_source()\n");
472 
473  lua_init(&LuaState);
474 
475  char path[PATH_MAX];
476 
477  if (mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS) != 0)
478  {
479  mutt_buffer_printf(err, _("source: error at %s"), s->dptr);
480  return MUTT_CMD_ERROR;
481  }
482  if (MoreArgs(s))
483  {
484  mutt_buffer_printf(err, _("%s: too many arguments"), "source");
485  return MUTT_CMD_WARNING;
486  }
487  mutt_str_strfcpy(path, buf->data, sizeof(path));
488  mutt_expand_path(path, sizeof(path));
489 
490  if (luaL_dofile(LuaState, path))
491  {
492  mutt_error(_("Couldn't source lua source: %s"), lua_tostring(LuaState, -1));
493  lua_pop(LuaState, 1);
494  return MUTT_CMD_ERROR;
495  }
496  return MUTT_CMD_SUCCESS;
497 }
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:369
char * mutt_expand_path(char *buf, size_t buflen)
Create the canonical path.
Definition: muttlib.c:133
#define MoreArgs(buf)
Definition: buffer.h:43
static bool lua_init(lua_State **l)
Initialise a Lua State.
Definition: mutt_lua.c:418
Log at debug level 2.
Definition: logging.h:41
#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:2674
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 369 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:145
static int lua_mutt_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition: mutt_lua.c:299
static int lua_mutt_message(lua_State *l)
Display a message in Neomutt.
Definition: mutt_lua.c:332
static int lua_mutt_call(lua_State *l)
Call a NeoMutt command by name.
Definition: mutt_lua.c:87
static int lua_mutt_error(lua_State *l)
Display an error in Neomutt.
Definition: mutt_lua.c:346
static int lua_mutt_get(lua_State *l)
Get a NeoMutt variable.
Definition: mutt_lua.c:225

Definition at line 371 of file mutt_lua.c.