NeoMutt  2025-09-05-55-g97fc89
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dump.c File Reference

Dump key bindings. More...

#include "config.h"
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "menu/lib.h"
#include "pager/lib.h"
#include "parse/lib.h"
+ Include dependency graph for dump.c:

Go to the source code of this file.

Functions

static int print_bind (enum MenuType menu, FILE *fp)
 Display the bindings for one menu.
 
static void colon_bind (enum MenuType menu, FILE *fp)
 Dump the key bindings.
 
static int print_macro (enum MenuType menu, FILE *fp)
 Display the macros for one menu.
 
static void colon_macro (enum MenuType menu, FILE *fp)
 Dump the macros.
 
enum CommandResult dump_bind_macro (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse 'bind' and 'macro' commands - Implements Command::parse() -.
 
int binding_sort (const void *a, const void *b, void *sdata)
 Compare two BindingInfo by their keybinding - Implements sort_t -.
 
void escape_macro (const char *macro, struct Buffer *buf)
 Escape any special characters in a macro.
 
static const char * help_lookup_function (int op, enum MenuType menu)
 Find a keybinding for an operation.
 
void gather_menu (enum MenuType menu, struct BindingInfoArray *bia_bind, struct BindingInfoArray *bia_macro)
 Gather info about one menu.
 
int measure_column (struct BindingInfoArray *bia, int col)
 Measure one column of a table.
 

Detailed Description

Dump key bindings.

Authors
  • Richard Russon
  • Dennis Schön

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.c.

Function Documentation

◆ print_bind()

static int print_bind ( enum MenuType menu,
FILE * fp )
static

Display the bindings for one menu.

Parameters
menuMenu type
fpFile to write to
Return values
numNumber of bindings

Definition at line 51 of file dump.c.

52{
53 struct BindingInfoArray bia_bind = ARRAY_HEAD_INITIALIZER;
54
55 gather_menu(menu, &bia_bind, NULL);
56 if (ARRAY_EMPTY(&bia_bind))
57 return 0;
58
59 ARRAY_SORT(&bia_bind, binding_sort, NULL);
60 const int wb0 = measure_column(&bia_bind, 0);
61 const int wb1 = measure_column(&bia_bind, 1);
62
63 const char *menu_name = mutt_map_get_name(menu, MenuNames);
64
65 struct BindingInfo *bi = NULL;
66 ARRAY_FOREACH(bi, &bia_bind)
67 {
68 //XXX use description?
69 fprintf(fp, "bind %s %*s %*s # %s\n", menu_name, -wb0, bi->a[0], -wb1,
70 bi->a[1], bi->a[2]);
71 }
72
73 const int count = ARRAY_SIZE(&bia_bind);
74 ARRAY_FOREACH(bi, &bia_bind)
75 {
76 // we only need to free the keybinding
77 FREE(&bi->a[0]);
78 }
79
80 return count;
81}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition array.h:335
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:214
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition array.h:74
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
int binding_sort(const void *a, const void *b, void *sdata)
Compare two BindingInfo by their keybinding - Implements sort_t -.
Definition dump.c:262
int measure_column(struct BindingInfoArray *bia, int col)
Measure one column of a table.
Definition dump.c:413
void gather_menu(enum MenuType menu, struct BindingInfoArray *bia_bind, struct BindingInfoArray *bia_macro)
Gather info about one menu.
Definition dump.c:357
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition mapping.c:42
#define FREE(x)
Definition memory.h:62
Info about one keybinding.
Definition lib.h:95
const char * a[3]
Array of info.
Definition lib.h:96
const struct Mapping MenuNames[]
Menu name lookup table.
Definition type.c:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ colon_bind()

static void colon_bind ( enum MenuType menu,
FILE * fp )
static

Dump the key bindings.

Parameters
menuMenu type
fpFile to write to

Definition at line 88 of file dump.c.

89{
90 if (menu == MENU_MAX)
91 {
92 for (enum MenuType i = 1; i < MENU_MAX; i++)
93 {
94 if (print_bind(i, fp) > 0)
95 fprintf(fp, "\n");
96 }
97 }
98 else
99 {
100 print_bind(menu, fp);
101 }
102}
static int print_bind(enum MenuType menu, FILE *fp)
Display the bindings for one menu.
Definition dump.c:51
MenuType
Types of GUI selections.
Definition type.h:35
@ MENU_MAX
Definition type.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_macro()

static int print_macro ( enum MenuType menu,
FILE * fp )
static

Display the macros for one menu.

Parameters
menuMenu type
fpFile to write to
Return values
numNumber of macros

Definition at line 110 of file dump.c.

111{
112 struct BindingInfoArray bia_macro = ARRAY_HEAD_INITIALIZER;
113
114 gather_menu(menu, NULL, &bia_macro);
115 if (ARRAY_EMPTY(&bia_macro))
116 return 0;
117
118 ARRAY_SORT(&bia_macro, binding_sort, NULL);
119 const int wm0 = measure_column(&bia_macro, 0);
120
121 const char *menu_name = mutt_map_get_name(menu, MenuNames);
122
123 struct BindingInfo *bi = NULL;
124 ARRAY_FOREACH(bi, &bia_macro)
125 {
126 if (bi->a[2]) // description
127 {
128 fprintf(fp, "macro %s %*s \"%s\" \"%s\"\n", menu_name, -wm0, bi->a[0],
129 bi->a[1], bi->a[2]);
130 }
131 else
132 {
133 fprintf(fp, "macro %s %*s \"%s\"\n", menu_name, -wm0, bi->a[0], bi->a[1]);
134 }
135 }
136
137 const int count = ARRAY_SIZE(&bia_macro);
138 ARRAY_FOREACH(bi, &bia_macro)
139 {
140 // free the keybinding and the macro text
141 FREE(&bi->a[0]);
142 FREE(&bi->a[1]);
143 }
144
145 ARRAY_FREE(&bia_macro);
146 return count;
147}
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:204
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ colon_macro()

static void colon_macro ( enum MenuType menu,
FILE * fp )
static

Dump the macros.

Parameters
menuMenu type
fpFile to write to

Definition at line 154 of file dump.c.

155{
156 if (menu == MENU_MAX)
157 {
158 for (enum MenuType i = 1; i < MENU_MAX; i++)
159 {
160 if (print_macro(i, fp) > 0)
161 {
162 //XXX need to elide last blank line
163 fprintf(fp, "\n");
164 }
165 }
166 }
167 else
168 {
169 print_macro(menu, fp);
170 }
171}
static int print_macro(enum MenuType menu, FILE *fp)
Display the macros for one menu.
Definition dump.c:110
+ 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 282 of file dump.c.

283{
284 wchar_t wc = 0;
285 size_t k;
286 size_t len = mutt_str_len(macro);
287 mbstate_t mbstate1 = { 0 };
288 mbstate_t mbstate2 = { 0 };
289
290 for (; (len > 0) && (k = mbrtowc(&wc, macro, MB_LEN_MAX, &mbstate1)); macro += k, len -= k)
291 {
292 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
293 {
294 if (k == ICONV_ILLEGAL_SEQ)
295 memset(&mbstate1, 0, sizeof(mbstate1));
296 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : len;
297 wc = ReplacementChar;
298 }
299
300 const int w = wcwidth(wc);
301 if (IsWPrint(wc) && (w >= 0))
302 {
303 char tmp[MB_LEN_MAX * 2] = { 0 };
304 if (wcrtomb(tmp, wc, &mbstate2) != ICONV_ILLEGAL_SEQ)
305 {
306 buf_addstr(buf, tmp);
307 }
308 }
309 else if ((wc < 0x20) || (wc == 0x7f))
310 {
311 if (wc == '\033') // Escape
312 buf_addstr(buf, "\\e");
313 else if (wc == '\n')
314 buf_addstr(buf, "\\n");
315 else if (wc == '\r')
316 buf_addstr(buf, "\\r");
317 else if (wc == '\t')
318 buf_addstr(buf, "\\t");
319 else
320 buf_add_printf(buf, "^%c", (char) ((wc + '@') & 0x7f));
321 }
322 else
323 {
324 buf_addch(buf, '?');
325 }
326 }
327}
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:41
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:98
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition charset.h:96
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:498
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ help_lookup_function()

static const char * help_lookup_function ( int op,
enum MenuType menu )
static

Find a keybinding for an operation.

Parameters
opOperation, e.g. OP_DELETE
menuCurrent Menu, e.g. MENU_PAGER
Return values
strKey binding
NULLNo key binding found

Definition at line 336 of file dump.c.

337{
338 if ((menu != MENU_PAGER) && (menu != MENU_EDITOR) && (menu != MENU_GENERIC))
339 {
340 /* first look in the generic map for the function */
341 const char *fn_name = mutt_get_func(OpGeneric, op);
342 if (fn_name)
343 return fn_name;
344 }
345
346 const struct MenuFuncOp *funcs = km_get_table(menu);
347
348 return mutt_get_func(funcs, op);
349}
const struct MenuFuncOp OpGeneric[]
Functions for the Generic Menu.
Definition functions.c:69
const char * mutt_get_func(const struct MenuFuncOp *funcs, int op)
Get the name of a function.
Definition lib.c:321
const struct MenuFuncOp * km_get_table(enum MenuType mtype)
Lookup a Menu's functions.
Definition lib.c:482
Mapping between a function and an operation.
Definition lib.h:115
int op
Operation, e.g. OP_DELETE.
Definition lib.h:117
@ MENU_GENERIC
Generic selection list.
Definition type.h:45
@ MENU_PAGER
Pager pager (email viewer)
Definition type.h:47
@ MENU_EDITOR
Text entry area.
Definition type.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ gather_menu()

void gather_menu ( enum MenuType menu,
struct BindingInfoArray * bia_bind,
struct BindingInfoArray * bia_macro )

Gather info about one menu.

Parameters
menuMenu type
bia_bindArray for bind results (may be NULL)
bia_macroArray for macro results (may be NULL)

Definition at line 357 of file dump.c.

359{
360 struct Buffer *key_binding = buf_pool_get();
361 struct Buffer *macro = buf_pool_get();
362
363 struct Keymap *map = NULL;
364 STAILQ_FOREACH(map, &Keymaps[menu], entries)
365 {
366 struct BindingInfo bi = { 0 };
367
368 buf_reset(key_binding);
369 km_expand_key(map, key_binding);
370
371 if (map->op == OP_MACRO)
372 {
373 if (!bia_macro || (map->op == OP_NULL))
374 continue;
375
376 buf_reset(macro);
377 escape_macro(map->macro, macro);
378 bi.a[0] = buf_strdup(key_binding);
379 bi.a[1] = buf_strdup(macro);
380 bi.a[2] = map->desc;
381 ARRAY_ADD(bia_macro, bi);
382 }
383 else
384 {
385 if (!bia_bind)
386 continue;
387
388 if (map->op == OP_NULL)
389 {
390 bi.a[0] = buf_strdup(key_binding);
391 bi.a[1] = "noop";
392 ARRAY_ADD(bia_bind, bi);
393 continue;
394 }
395
396 bi.a[0] = buf_strdup(key_binding);
397 bi.a[1] = help_lookup_function(map->op, menu);
398 bi.a[2] = _(opcodes_get_description(map->op));
399 ARRAY_ADD(bia_bind, bi);
400 }
401 }
402
403 buf_pool_release(&key_binding);
404 buf_pool_release(&macro);
405}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:156
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
static const char * help_lookup_function(int op, enum MenuType menu)
Find a keybinding for an operation.
Definition dump.c:336
void escape_macro(const char *macro, struct Buffer *buf)
Escape any special characters in a macro.
Definition dump.c:282
struct KeymapList Keymaps[MENU_MAX]
Array of key mappings, one for each MenuType.
Definition lib.c:125
bool km_expand_key(struct Keymap *map, struct Buffer *buf)
Get the key string bound to a Keymap.
Definition lib.c:434
#define _(a)
Definition message.h:28
const char * opcodes_get_description(int op)
Get the description of an opcode.
Definition opcodes.c:68
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
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
String manipulation buffer.
Definition buffer.h:36
A keyboard mapping.
Definition lib.h:67
char * macro
Macro expansion (op == OP_MACRO)
Definition lib.h:68
char * desc
Description of a macro for the help menu.
Definition lib.h:69
short op
Operation to perform.
Definition lib.h:70
+ 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 413 of file dump.c.

414{
415 int max_width = 0;
416
417 struct BindingInfo *bi = NULL;
418 ARRAY_FOREACH(bi, bia)
419 {
420 const int col_width = mutt_strwidth(bi->a[col]);
421 max_width = MAX(max_width, col_width);
422 }
423
424 return max_width;
425}
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition curs_lib.c:445
#define MAX(a, b)
Definition memory.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function: