NeoMutt  2025-09-05-70-gcfdde0
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
commands.c
Go to the documentation of this file.
1
22
28
29#ifndef LUA_COMPAT_ALL
30#define LUA_COMPAT_ALL
31#endif
32#ifndef LUA_COMPAT_5_1
33#define LUA_COMPAT_5_1
34#endif
35
36#include "config.h"
37#include <lauxlib.h>
38#include <lua.h>
39#include <lualib.h>
40#include <stdbool.h>
41#include <stdint.h>
42#include <stdio.h>
43#include "mutt/lib.h"
44#include "config/lib.h"
45#include "core/lib.h"
46#include "lib.h"
47#include "parse/lib.h"
48#include "muttlib.h"
49#include "version.h"
50
51extern lua_State *LuaState;
52
53bool lua_init_state(lua_State **l);
54
58static enum CommandResult parse_lua(struct Buffer *buf, struct Buffer *s,
59 intptr_t data, struct Buffer *err)
60{
62 mutt_debug(LL_DEBUG2, "%s\n", buf->data);
63
64 if (luaL_dostring(LuaState, s->dptr))
65 {
66 mutt_debug(LL_DEBUG2, "%s -> failure\n", s->dptr);
67 buf_printf(err, "%s: %s", s->dptr, lua_tostring(LuaState, -1));
68 /* pop error message from the stack */
69 lua_pop(LuaState, 1);
70 return MUTT_CMD_ERROR;
71 }
72 mutt_debug(LL_DEBUG2, "%s -> success\n", s->dptr);
73 buf_reset(s); // Clear the rest of the line
74 return MUTT_CMD_SUCCESS;
75}
76
80static enum CommandResult parse_lua_source(struct Buffer *buf, struct Buffer *s,
81 intptr_t data, struct Buffer *err)
82{
83 mutt_debug(LL_DEBUG2, "enter\n");
84
86
87 if (parse_extract_token(buf, s, TOKEN_NO_FLAGS) != 0)
88 {
89 buf_printf(err, _("source: error at %s"), s->dptr);
90 return MUTT_CMD_ERROR;
91 }
92 if (MoreArgs(s))
93 {
94 buf_printf(err, _("%s: too many arguments"), "source");
95 return MUTT_CMD_WARNING;
96 }
97
98 struct Buffer *path = buf_pool_get();
99 buf_copy(path, buf);
100 buf_expand_path(path);
101
102 if (luaL_dofile(LuaState, buf_string(path)))
103 {
104 mutt_error(_("Couldn't source lua source: %s"), lua_tostring(LuaState, -1));
105 lua_pop(LuaState, 1);
106 buf_pool_release(&path);
107 return MUTT_CMD_ERROR;
108 }
109
110 buf_pool_release(&path);
111 return MUTT_CMD_SUCCESS;
112}
113
117static const struct Command LuaCommands[] = {
118 // clang-format off
119 { "lua", parse_lua, 0 },
120 { "lua-source", parse_lua_source, 0 },
121 { NULL, NULL, 0 },
122 // clang-format on
123};
124
132
136void lua_cleanup(void)
137{
138 if (LuaState)
139 {
140 lua_close(LuaState);
141 LuaState = NULL;
142 }
143}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition buffer.c:601
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
CommandResult
Error codes for command_t parse functions.
Definition command.h:35
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:38
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:36
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:37
Convenience wrapper for the config headers.
bool commands_register(struct CommandArray *ca, const struct Command *cmds)
Add commands to Commands array.
Definition command.c:51
Convenience wrapper for the core headers.
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition extract.c:48
#define MoreArgs(buf)
Definition extract.h:30
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:44
static enum CommandResult parse_lua_source(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'lua-source' command - Implements Command::parse() -.
Definition commands.c:80
static enum CommandResult parse_lua(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'lua' command - Implements Command::parse() -.
Definition commands.c:58
#define mutt_error(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:45
bool lua_init_state(lua_State **l)
Initialise a Lua State.
Definition lua.c:437
void lua_init(void)
Setup feature commands.
Definition commands.c:128
lua_State * LuaState
Global Lua State.
Definition lua.c:57
void lua_cleanup(void)
Clean up Lua.
Definition commands.c:136
static const struct Command LuaCommands[]
List of NeoMutt commands to register.
Definition commands.c:117
Integrated Lua scripting.
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition muttlib.c:314
Some miscellaneous functions.
Text parsing functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:96
String manipulation buffer.
Definition buffer.h:36
char * dptr
Current read/write position.
Definition buffer.h:38
char * data
Pointer to data.
Definition buffer.h:37
intptr_t data
Data or flags to pass to the command.
Definition command.h:66
Container for Accounts, Notifications.
Definition neomutt.h:42
struct CommandArray commands
NeoMutt commands.
Definition neomutt.h:50
Display version and copyright about NeoMutt.