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

Dump key bindings. More...

#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
+ Include dependency graph for dump.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BindingInfo
 Info about one keybinding. More...
 

Functions

 ARRAY_HEAD (BindingInfoArray, struct BindingInfo)
 
int binding_sort (const void *a, const void *b, void *sdata)
 Compare two BindingInfo by their keybinding - Implements sort_t -.
 
void colon_bind (const struct MenuDefinition *md, FILE *fp)
 Dump the key bindings.
 
void colon_macro (const struct MenuDefinition *md, FILE *fp)
 Dump the macros.
 
void dump_bind_macro (const struct Command *cmd, const struct MenuDefinition *md, struct Buffer *buf, struct Buffer *err)
 Dump a Menu's binds or macros to the Pager.
 
void escape_macro (const char *macro, struct Buffer *buf)
 Escape any special characters in a macro.
 
void gather_menu (const struct MenuDefinition *md, struct BindingInfoArray *bia_bind, struct BindingInfoArray *bia_macro, bool one_submenu)
 Gather info about one menu.
 
int gather_unbound (const struct MenuDefinition *md, struct BindingInfoArray *bia_unbound)
 Gather info about unbound functions for one menu.
 
const char * help_lookup_function (const struct MenuDefinition *md, int op)
 Find a keybinding for an operation.
 
struct StringArray km_get_func_array (const struct MenuDefinition *md)
 Get array of function names for a Menu.
 
int measure_column (struct BindingInfoArray *bia, int col)
 Measure one column of a table.
 
int print_bind (const struct MenuDefinition *md, FILE *fp)
 Display the bindings for one menu.
 
int print_macro (const struct MenuDefinition *md, FILE *fp)
 Display the macros for one menu.
 

Detailed Description

Dump key bindings.

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

Function Documentation

◆ ARRAY_HEAD()

ARRAY_HEAD ( BindingInfoArray ,
struct BindingInfo  )
+ Here is the call graph for this function:

◆ colon_bind()

void colon_bind ( const struct MenuDefinition * md,
FILE * fp )

Dump the key bindings.

Parameters
mdMenu Definition, NULL for all Menus
fpFile to write to

Definition at line 289 of file dump.c.

290{
291 if (md)
292 {
293 print_bind(md, fp);
294 }
295 else
296 {
297 struct MenuDefinition **mdp = NULL;
299 {
300 if (print_bind((*mdp), fp) > 0)
301 {
302 fprintf(fp, "\n");
303 }
304 }
305 }
306}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
struct MenuDefinitionArray MenuDefs
All the registered Menus.
Definition init.c:42
int print_bind(const struct MenuDefinition *md, FILE *fp)
Display the bindings for one menu.
Definition dump.c:249
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:

◆ colon_macro()

void colon_macro ( const struct MenuDefinition * md,
FILE * fp )

Dump the macros.

Parameters
mdMenu Definition, NULL for all Menus
fpFile to write to

Definition at line 361 of file dump.c.

362{
363 if (md)
364 {
365 print_macro(md, fp);
366 }
367 else
368 {
369 struct MenuDefinition **mdp = NULL;
371 {
372 if (print_macro((*mdp), fp) > 0)
373 {
374 fprintf(fp, "\n");
375 }
376 }
377 }
378}
int print_macro(const struct MenuDefinition *md, FILE *fp)
Display the macros for one menu.
Definition dump.c:314
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dump_bind_macro()

void dump_bind_macro ( const struct Command * cmd,
const struct MenuDefinition * md,
struct Buffer * buf,
struct Buffer * err )

Dump a Menu's binds or macros to the Pager.

Parameters
cmdCommand
mdMenu Definition, NULL for all Menus
bufMenu name, e.g. "index"
errBuffer for errors

Definition at line 387 of file dump.c.

389{
390 bool dump_all = (md == NULL);
391
392 struct Buffer *tempfile = buf_pool_get();
393 buf_mktemp(tempfile);
394 FILE *fp = mutt_file_fopen(buf_string(tempfile), "w");
395 if (!fp)
396 {
397 // L10N: '%s' is the file name of the temporary file
398 buf_printf(err, _("Could not create temporary file %s"), buf_string(tempfile));
399 goto done;
400 }
401
402 if (cmd->id == CMD_BIND)
403 colon_bind(md, fp);
404 else
405 colon_macro(md, fp);
406
407 if (ftello(fp) == 0)
408 {
409 // L10N: '%s' is the name of the menu, e.g. 'index' or 'pager',
410 // it might also be 'all' when all menus are affected.
411 buf_printf(err, (cmd->id == CMD_BIND) ? _("%s: no binds for this menu") : _("%s: no macros for this menu"),
412 dump_all ? "all" : buf_string(buf));
413 goto done;
414 }
415 mutt_file_fclose(&fp);
416
417 struct PagerData pdata = { 0 };
418 struct PagerView pview = { &pdata };
419
420 pdata.fname = buf_string(tempfile);
421
422 pview.banner = cmd->name;
424 pview.mode = PAGER_MODE_OTHER;
425
426 mutt_do_pager(&pview, NULL);
427
428done:
429 mutt_file_fclose(&fp);
430 buf_pool_release(&tempfile);
431}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
@ CMD_BIND
:bind
Definition command.h:67
int mutt_do_pager(struct PagerView *pview, struct Email *e)
Display some page-able text to the user (help or attachment)
Definition do_pager.c:122
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
void colon_macro(const struct MenuDefinition *md, FILE *fp)
Dump the macros.
Definition dump.c:361
void colon_bind(const struct MenuDefinition *md, FILE *fp)
Dump the key bindings.
Definition dump.c:289
#define _(a)
Definition message.h:28
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition lib.h:62
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition lib.h:142
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
String manipulation buffer.
Definition buffer.h:36
const char * name
Name of the Command.
Definition command.h:159
enum CommandId id
ID of the Command.
Definition command.h:160
Data to be displayed by PagerView.
Definition lib.h:161
const char * fname
Name of the file to read.
Definition lib.h:165
Paged view into some data.
Definition lib.h:172
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition lib.h:173
enum PagerMode mode
Pager mode.
Definition lib.h:174
PagerFlags flags
Additional settings to tweak pager's function.
Definition lib.h:175
const char * banner
Title to display in status bar.
Definition lib.h:176
#define buf_mktemp(buf)
Definition tmp.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ escape_macro()

void escape_macro ( const char * macro,
struct Buffer * buf )

Escape any special characters in a macro.

Parameters
[in]macroMacro string
[out]bufBuffer for the result

Replace characters, such as <Enter> with the literal "\n"

Definition at line 54 of file dump.c.

55{
56 wchar_t wc = 0;
57 size_t k;
58 size_t len = mutt_str_len(macro);
59 mbstate_t mbstate1 = { 0 };
60 mbstate_t mbstate2 = { 0 };
61
62 for (; (len > 0) && (k = mbrtowc(&wc, macro, MB_LEN_MAX, &mbstate1)); macro += k, len -= k)
63 {
64 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
65 {
66 if (k == ICONV_ILLEGAL_SEQ)
67 memset(&mbstate1, 0, sizeof(mbstate1));
68 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : len;
69 wc = ReplacementChar;
70 }
71
72 const int w = wcwidth(wc);
73 if (IsWPrint(wc) && (w >= 0))
74 {
75 char tmp[MB_LEN_MAX * 2] = { 0 };
76 if (wcrtomb(tmp, wc, &mbstate2) != ICONV_ILLEGAL_SEQ)
77 {
78 buf_addstr(buf, tmp);
79 }
80 }
81 else if ((wc < 0x20) || (wc == 0x7f))
82 {
83 if (wc == '\033') // Escape
84 buf_addstr(buf, "\\e");
85 else if (wc == '\n')
86 buf_addstr(buf, "\\n");
87 else if (wc == '\r')
88 buf_addstr(buf, "\\r");
89 else if (wc == '\t')
90 buf_addstr(buf, "\\t");
91 else
92 buf_add_printf(buf, "^%c", (char) ((wc + '@') & 0x7f));
93 }
94 else
95 {
96 buf_addch(buf, '?');
97 }
98 }
99}
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
#define IsWPrint(wc)
Definition mbyte.h:40
wchar_t ReplacementChar
When a Unicode character can't be displayed, use this instead.
Definition charset.c:61
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition charset.h:116
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition charset.h:114
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:500
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ gather_menu()

void gather_menu ( const struct MenuDefinition * md,
struct BindingInfoArray * bia_bind,
struct BindingInfoArray * bia_macro,
bool one_submenu )

Gather info about one menu.

Parameters
[in]mdMenu Definition
[out]bia_bindArray for bind results (may be NULL)
[out]bia_macroArray for macro results (may be NULL)
[in]one_submenuOnly parse the first SubMenu

Definition at line 134 of file dump.c.

136{
137 struct Buffer *key_binding = buf_pool_get();
138 struct Buffer *macro = buf_pool_get();
139
140 struct SubMenu **smp = NULL;
141
142 ARRAY_FOREACH(smp, &md->submenus)
143 {
144 struct SubMenu *sm = *smp;
145 const char *name = sm->parent->name;
146
147 struct BindingInfo bi_label = { ARRAY_FOREACH_IDX_smp, { NULL, NULL, name } };
148
149 if (bia_bind)
150 ARRAY_ADD(bia_bind, bi_label);
151 if (bia_macro)
152 ARRAY_ADD(bia_macro, bi_label);
153
154 struct Keymap *map = NULL;
155 STAILQ_FOREACH(map, &sm->keymaps, entries)
156 {
157 struct BindingInfo bi = { ARRAY_FOREACH_IDX_smp, { NULL, NULL, NULL } };
158
159 buf_reset(key_binding);
160 keymap_expand_key(map, key_binding);
161
162 if (map->op == OP_MACRO)
163 {
164 if (!bia_macro || (map->op == OP_NULL))
165 continue;
166
167 buf_reset(macro);
168 escape_macro(map->macro, macro);
169 bi.a[0] = buf_strdup(key_binding);
170 bi.a[1] = buf_strdup(macro);
171 bi.a[2] = map->desc;
172 ARRAY_ADD(bia_macro, bi);
173 }
174 else
175 {
176 if (!bia_bind)
177 continue;
178
179 if (map->op == OP_NULL)
180 {
181 bi.a[0] = buf_strdup(key_binding);
182 bi.a[1] = "noop";
183 ARRAY_ADD(bia_bind, bi);
184 continue;
185 }
186
187 bi.a[0] = buf_strdup(key_binding);
188 bi.a[1] = help_lookup_function(md, map->op);
189 bi.a[2] = _(opcodes_get_description(map->op));
190 ARRAY_ADD(bia_bind, bi);
191 }
192 }
193
194 if (one_submenu)
195 break;
196 }
197
198 buf_pool_release(&key_binding);
199 buf_pool_release(&macro);
200}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
void escape_macro(const char *macro, struct Buffer *buf)
Escape any special characters in a macro.
Definition dump.c:54
const char * help_lookup_function(const struct MenuDefinition *md, int op)
Find a keybinding for an operation.
Definition dump.c:108
bool keymap_expand_key(struct Keymap *km, struct Buffer *buf)
Get the key string bound to a Keymap.
Definition keymap.c:229
const char * opcodes_get_description(int op)
Get the description of an opcode.
Definition opcodes.c:68
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
Info about one keybinding.
Definition dump.h:40
const char * a[3]
Array of info.
Definition dump.h:42
A keyboard mapping.
Definition keymap.h:43
char * macro
Macro expansion (op == OP_MACRO)
Definition keymap.h:44
char * desc
Description of a macro for the help menu.
Definition keymap.h:45
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
struct MenuDefinition * parent
Primary parent.
Definition menu.h:69
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ gather_unbound()

int gather_unbound ( const struct MenuDefinition * md,
struct BindingInfoArray * bia_unbound )

Gather info about unbound functions for one menu.

Parameters
[in]mdMenu Definition
[out]bia_unboundUnbound functions
Return values
numNumber of unbound functions

Definition at line 439 of file dump.c.

440{
441 if (!bia_unbound)
442 return 0;
443
444 struct SubMenu **smp = NULL;
445
446 ARRAY_FOREACH(smp, &md->submenus)
447 {
448 struct SubMenu *sm = *smp;
449
450 for (int i = 0; sm->functions[i].name; i++)
451 {
452 const struct MenuFuncOp *mfo = &sm->functions[i];
453
454 if (mfo->flags & MFF_DEPRECATED)
455 continue;
456
457 if (is_bound(md, mfo->op))
458 continue;
459
460 struct BindingInfo bi = { 0 };
461 bi.a[0] = NULL;
462 bi.a[1] = mfo->name;
463 bi.a[2] = _(opcodes_get_description(mfo->op));
464 ARRAY_ADD(bia_unbound, bi);
465 }
466 }
467
468 return ARRAY_SIZE(bia_unbound);
469}
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define MFF_DEPRECATED
Redraw the pager.
Definition get.h:44
bool is_bound(const struct MenuDefinition *md, int op)
Does a function have a keybinding?
Definition menu.c:287
Mapping between a function and an operation.
Definition menu.h:38
const char * name
Name of the function.
Definition menu.h:39
MenuFuncFlags flags
Flags, e.g. MFF_DEPRECATED.
Definition menu.h:41
int op
Operation, e.g. OP_DELETE.
Definition menu.h:40
const struct MenuFuncOp * functions
All available functions.
Definition menu.h:70
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ help_lookup_function()

const char * help_lookup_function ( const struct MenuDefinition * md,
int op )

Find a keybinding for an operation.

Parameters
mdMenu Definition
opOperation, e.g. OP_DELETE
Return values
strKey binding
NULLNo key binding found

Definition at line 108 of file dump.c.

109{
110 struct SubMenu **smp = NULL;
111
112 ARRAY_FOREACH(smp, &md->submenus)
113 {
114 struct SubMenu *sm = *smp;
115
116 for (int i = 0; sm->functions[i].name; i++)
117 {
118 const struct MenuFuncOp *mfo = &sm->functions[i];
119 if (mfo->op == op)
120 return mfo->name;
121 }
122 }
123
124 return "UNKNOWN";
125}
+ Here is the caller graph for this function:

◆ km_get_func_array()

struct StringArray km_get_func_array ( const struct MenuDefinition * md)

Get array of function names for a Menu.

Parameters
mdMenu Definition

Definition at line 475 of file dump.c.

476{
477 struct StringArray fna = ARRAY_HEAD_INITIALIZER;
478
479 struct SubMenu **smp = NULL;
480 ARRAY_FOREACH(smp, &md->submenus)
481 {
482 struct SubMenu *sm = *smp;
483
484 for (int i = 0; sm->functions[i].name; i++)
485 {
486 ARRAY_ADD(&fna, sm->functions[i].name);
487 }
488 }
489
490 return fna;
491}
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ measure_column()

int measure_column ( struct BindingInfoArray * bia,
int col )

Measure one column of a table.

Parameters
biaArray of binding info
colColumn to measure
Return values
numWidth of widest column

Definition at line 229 of file dump.c.

230{
231 int max_width = 0;
232
233 struct BindingInfo *bi = NULL;
234 ARRAY_FOREACH(bi, bia)
235 {
236 const int col_width = mutt_strwidth(bi->a[col]);
237 max_width = MAX(max_width, col_width);
238 }
239
240 return max_width;
241}
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition curs_lib.c:444
#define MAX(a, b)
Return the maximum of two values.
Definition memory.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_bind()

int print_bind ( const struct MenuDefinition * md,
FILE * fp )

Display the bindings for one menu.

Parameters
mdMenu Definition
fpFile to write to
Return values
numNumber of bindings

Definition at line 249 of file dump.c.

250{
251 struct BindingInfoArray bia_bind = ARRAY_HEAD_INITIALIZER;
252
253 gather_menu(md, &bia_bind, NULL, true);
254 if (ARRAY_EMPTY(&bia_bind))
255 return 0;
256
257 ARRAY_SORT(&bia_bind, binding_sort, NULL);
258 const int wb0 = measure_column(&bia_bind, 0);
259 const int wb1 = measure_column(&bia_bind, 1);
260
261 const char *menu_name = md->name;
262
263 struct BindingInfo *bi = NULL;
264 ARRAY_FOREACH(bi, &bia_bind)
265 {
266 if (!bi->a[0])
267 continue;
268
269 fprintf(fp, "bind %s %*s %*s # %s\n", menu_name, -wb0, bi->a[0], -wb1,
270 bi->a[1], bi->a[2]);
271 }
272
273 const int count = ARRAY_SIZE(&bia_bind);
274 ARRAY_FOREACH(bi, &bia_bind)
275 {
276 // we only need to free the keybinding
277 FREE(&bi->a[0]);
278 }
279
280 ARRAY_FREE(&bia_bind);
281 return count - 1;
282}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition array.h:373
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition array.h:74
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
int binding_sort(const void *a, const void *b, void *sdata)
Compare two BindingInfo by their keybinding - Implements sort_t -.
Definition dump.c:205
int measure_column(struct BindingInfoArray *bia, int col)
Measure one column of a table.
Definition dump.c:229
void gather_menu(const struct MenuDefinition *md, struct BindingInfoArray *bia_bind, struct BindingInfoArray *bia_macro, bool one_submenu)
Gather info about one menu.
Definition dump.c:134
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_macro()

int print_macro ( const struct MenuDefinition * md,
FILE * fp )

Display the macros for one menu.

Parameters
mdMenu Definition
fpFile to write to
Return values
numNumber of macros

Definition at line 314 of file dump.c.

315{
316 struct BindingInfoArray bia_macro = ARRAY_HEAD_INITIALIZER;
317
318 gather_menu(md, NULL, &bia_macro, true);
319 if (ARRAY_EMPTY(&bia_macro))
320 return 0;
321
322 ARRAY_SORT(&bia_macro, binding_sort, NULL);
323 const int wm0 = measure_column(&bia_macro, 0);
324
325 const char *menu_name = md->name;
326
327 struct BindingInfo *bi = NULL;
328 ARRAY_FOREACH(bi, &bia_macro)
329 {
330 if (!bi->a[0])
331 continue;
332
333 if (bi->a[2]) // description
334 {
335 fprintf(fp, "macro %s %*s \"%s\" \"%s\"\n", menu_name, -wm0, bi->a[0],
336 bi->a[1], bi->a[2]);
337 }
338 else
339 {
340 fprintf(fp, "macro %s %*s \"%s\"\n", menu_name, -wm0, bi->a[0], bi->a[1]);
341 }
342 }
343
344 const int count = ARRAY_SIZE(&bia_macro);
345 ARRAY_FOREACH(bi, &bia_macro)
346 {
347 // free the keybinding and the macro text
348 FREE(&bi->a[0]);
349 FREE(&bi->a[1]);
350 }
351
352 ARRAY_FREE(&bia_macro);
353 return count - 1;
354}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: