NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
commands.h File Reference

Parse key binding commands. More...

#include "core/lib.h"
+ Include dependency graph for commands.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

enum CommandResult km_bind (struct MenuDefinition *md, const char *key_str, int op, char *macro, char *desc, struct Buffer *err)
 Set up a key binding.
 
char * parse_keymap (const struct Command *cmd, struct MenuDefinitionArray *mda, struct Buffer *line, struct Buffer *err)
 Parse a user-config key binding.
 
void parse_menu (struct MenuDefinitionArray *menus, const char *s, struct Buffer *err)
 Parse menu-names into an array.
 
enum CommandResult parse_bind (const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
 Parse the 'bind' command - Implements Command::parse() -.
 
enum CommandResult parse_exec (const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
 Parse the 'exec' command - Implements Command::parse() -.
 
enum CommandResult parse_macro (const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
 Parse the 'macro' command - Implements Command::parse() -.
 
enum CommandResult parse_push (const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
 Parse the 'push' command - Implements Command::parse() -.
 
enum CommandResult parse_unbind (const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
 Parse the 'unbind' and 'unmacro' commands - Implements Command::parse() -.
 

Detailed Description

Parse key binding commands.

Authors
  • Richard Russon

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 commands.h.

Function Documentation

◆ km_bind()

enum CommandResult km_bind ( struct MenuDefinition * md,
const char * key_str,
int op,
char * macro,
char * desc,
struct Buffer * err )

Set up a key binding.

Parameters
mdMenu Definition
key_strKey string
opOperation, e.g. OP_DELETE
macroMacro string
descDescription of macro (OPTIONAL)
errBuffer for error message
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Insert a key sequence into the specified map. The map is sorted by ASCII value (lowest to highest)

Definition at line 51 of file menu.c.

53{
54 if (!md || ARRAY_EMPTY(&md->submenus))
55 return MUTT_CMD_ERROR;
56
58 struct Keymap *last = NULL;
59 struct Keymap *np = NULL;
60 struct Keymap *compare = NULL;
61 keycode_t buf[MAX_SEQ] = { 0 };
62 size_t pos = 0;
63 size_t lastpos = 0;
64
65 struct SubMenu *sm = *ARRAY_FIRST(&md->submenus);
66 struct KeymapList *kml = &sm->keymaps;
67
68 size_t len = parse_keys(key_str, buf, MAX_SEQ);
69
70 struct Keymap *map = keymap_alloc(len, buf);
71 map->op = op;
72 map->macro = mutt_str_dup(macro);
73 map->desc = mutt_str_dup(desc);
74
75 /* find position to place new keymap */
76 STAILQ_FOREACH(np, kml, entries)
77 {
78 compare = keymap_compare(map, np, &pos);
79
80 if (compare == map) /* map's keycode is bigger */
81 {
82 last = np;
83 lastpos = pos;
84 if (pos > np->eq)
85 pos = np->eq;
86 }
87 else if (compare == np) /* np's keycode is bigger, found insert location */
88 {
89 map->eq = pos;
90 break;
91 }
92 else /* equal keycodes */
93 {
94 /* Don't warn on overwriting a 'noop' binding */
95 if ((np->len != len) && (np->op != OP_NULL))
96 {
97 static const char *guide_link = "https://neomutt.org/guide/configuration.html#bind-warnings";
98 /* Overwrite with the different lengths, warn */
99 struct Buffer *old_binding = buf_pool_get();
100 struct Buffer *new_binding = buf_pool_get();
101
102 keymap_expand_key(map, old_binding);
103 keymap_expand_key(np, new_binding);
104
105 char *err_msg = _("Binding '%s' will alias '%s' Before, try: 'bind %s %s noop'");
106 if (err)
107 {
108 /* err was passed, put the string there */
109 buf_printf(err, err_msg, buf_string(old_binding),
110 buf_string(new_binding), md->name, buf_string(new_binding));
111 buf_add_printf(err, " %s", guide_link);
112 }
113 else
114 {
115 struct Buffer *tmp = buf_pool_get();
116 buf_printf(tmp, err_msg, buf_string(old_binding),
117 buf_string(new_binding), md->name, buf_string(new_binding));
118 buf_add_printf(tmp, " %s", guide_link);
119 mutt_error("%s", buf_string(tmp));
120 buf_pool_release(&tmp);
121 }
122 rc = MUTT_CMD_WARNING;
123
124 buf_pool_release(&old_binding);
125 buf_pool_release(&new_binding);
126 }
127
128 map->eq = np->eq;
129 STAILQ_REMOVE(kml, np, Keymap, entries);
130 keymap_free(&np);
131 break;
132 }
133 }
134
135 if (last) /* if queue has at least one entry */
136 {
137 if (STAILQ_NEXT(last, entries))
138 STAILQ_INSERT_AFTER(kml, last, map, entries);
139 else /* last entry in the queue */
140 STAILQ_INSERT_TAIL(kml, map, entries);
141 last->eq = lastpos;
142 }
143 else /* queue is empty, so insert from head */
144 {
145 STAILQ_INSERT_HEAD(kml, map, entries);
146 }
147
148 return rc;
149}
#define ARRAY_FIRST(head)
Convenience method to get the first element.
Definition array.h:136
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition array.h:74
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
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:37
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:40
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:38
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:39
#define mutt_error(...)
Definition logging2.h:94
bool keymap_expand_key(struct Keymap *km, struct Buffer *buf)
Get the key string bound to a Keymap.
Definition keymap.c:229
void keymap_free(struct Keymap **pptr)
Free a Keymap.
Definition keymap.c:127
struct Keymap * keymap_compare(struct Keymap *km1, struct Keymap *km2, size_t *pos)
Compare two keymaps' keyscodes and return the bigger one.
Definition keymap.c:162
size_t parse_keys(const char *str, keycode_t *d, size_t max)
Parse a key string into key codes.
Definition keymap.c:318
struct Keymap * keymap_alloc(size_t len, keycode_t *keys)
Allocate space for a sequence of keys.
Definition keymap.c:112
short keycode_t
Type for key storage, the rest of neomutt works fine with int type.
Definition keymap.h:31
#define MAX_SEQ
Maximum length of a key binding sequence used for buffer in km_bind.
Definition menu.h:32
#define _(a)
Definition message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
#define STAILQ_REMOVE(head, elm, type, field)
Definition queue.h:441
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:427
#define STAILQ_INSERT_HEAD(head, elm, field)
Definition queue.h:421
#define STAILQ_NEXT(elm, field)
Definition queue.h:439
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field)
Definition queue.h:415
String manipulation buffer.
Definition buffer.h:36
A keyboard mapping.
Definition keymap.h:43
char * macro
Macro expansion (op == OP_MACRO)
Definition keymap.h:44
short eq
Number of leading keys equal to next entry.
Definition keymap.h:47
char * desc
Description of a macro for the help menu.
Definition keymap.h:45
short len
Length of key sequence (unit: sizeof (keycode_t))
Definition keymap.h:48
short op
Operation to perform.
Definition keymap.h:46
const char * name
Menu name, e.g. "alias".
Definition menu.h:82
struct SubMenuPArray submenus
Parts making up the Menu.
Definition menu.h:83
Collection of related functions.
Definition menu.h:68
struct KeymapList keymaps
All keybindings.
Definition menu.h:71
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_keymap()

char * parse_keymap ( const struct Command * cmd,
struct MenuDefinitionArray * mda,
struct Buffer * line,
struct Buffer * err )

Parse a user-config key binding.

Parameters
cmdCommand being processed
mdaArray for results
lineBuffer containing config string
errBuffer for error messages
Return values
ptrKey string for the binding

Expects to see: <menu-string>,<menu-string>,... <key-string>

Note
Caller needs to free the returned string

Definition at line 115 of file commands.c.

117{
118 struct Buffer *token = buf_pool_get();
119 char *q = NULL;
120 char *result = NULL;
121
122 /* menu name */
124 char *p = token->data;
125 if (MoreArgs(line))
126 {
127 while (true)
128 {
129 q = strchr(p, ',');
130 if (q)
131 *q = '\0';
132
133 struct MenuDefinition *md = menu_find_by_name(p);
134 if (!md)
135 {
136 buf_printf(err, _("%s: no such menu"), p);
137 goto done;
138 }
139 ARRAY_ADD(mda, md);
140 if (q)
141 p = q + 1;
142 else
143 break;
144 }
145 /* key sequence */
147
148 if (buf_at(token, 0) == '\0')
149 {
150 buf_printf(err, _("%s: null key sequence"), cmd->name);
151 }
152 else if (MoreArgs(line))
153 {
154 result = buf_strdup(token);
155 goto done;
156 }
157 }
158 else
159 {
160 buf_printf(err, _("%s: too few arguments"), cmd->name);
161 }
162done:
163 buf_pool_release(&token);
164 return result;
165}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
char buf_at(const struct Buffer *buf, size_t offset)
Return the character at the given offset.
Definition buffer.c:668
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
int parse_extract_token(struct Buffer *dest, struct Buffer *line, TokenFlags flags)
Extract one token from a string.
Definition extract.c:49
#define MoreArgs(buf)
Definition extract.h:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:45
struct MenuDefinition * menu_find_by_name(const char *name)
Find a Menu Definition by its name.
Definition menu.c:264
char * data
Pointer to data.
Definition buffer.h:37
const char * name
Name of the Command.
Definition command.h:159
Functions for a Dialog or Window.
Definition menu.h:80
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_menu()

void parse_menu ( struct MenuDefinitionArray * menus,
const char * s,
struct Buffer * err )

Parse menu-names into an array.

Parameters
menusArray for results
sString containing menu-names
errBuffer for error messages

Expects to see: <menu-string>[,<menu-string>]

Definition at line 175 of file commands.c.

176{
177 char *menu_names_dup = mutt_str_dup(s);
178 char *marker = menu_names_dup;
179 char *menu_name = NULL;
180
181 while ((menu_name = mutt_str_sep(&marker, ",")))
182 {
183 struct MenuDefinition *md = menu_find_by_name(menu_name);
184 if (!md)
185 {
186 buf_printf(err, _("%s: no such menu"), menu_name);
187 break;
188 }
189 else
190 {
191 ARRAY_ADD(menus, md);
192 }
193 }
194
195 FREE(&menu_names_dup);
196}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
char * mutt_str_sep(char **stringp, const char *delim)
Find first occurrence of any of delim characters in *stringp.
Definition string.c:190
+ Here is the call graph for this function:
+ Here is the caller graph for this function: