NeoMutt  2025-12-11-58-g09398d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dump.c
Go to the documentation of this file.
1
23
29
30#include "config.h"
31#include <limits.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <string.h>
35#include <wchar.h>
36#include "mutt/lib.h"
37#include "core/lib.h"
38#include "gui/lib.h"
39#include "lib.h"
40#include "menu/lib.h"
41#include "pager/lib.h"
42#include "parse/lib.h"
43
50static int print_bind(enum MenuType menu, FILE *fp)
51{
52 struct BindingInfoArray bia_bind = ARRAY_HEAD_INITIALIZER;
53
54 gather_menu(menu, &bia_bind, NULL);
55 if (ARRAY_EMPTY(&bia_bind))
56 return 0;
57
58 ARRAY_SORT(&bia_bind, binding_sort, NULL);
59 const int wb0 = measure_column(&bia_bind, 0);
60 const int wb1 = measure_column(&bia_bind, 1);
61
62 const char *menu_name = mutt_map_get_name(menu, MenuNames);
63
64 struct BindingInfo *bi = NULL;
65 ARRAY_FOREACH(bi, &bia_bind)
66 {
67 //XXX use description?
68 fprintf(fp, "bind %s %*s %*s # %s\n", menu_name, -wb0, bi->a[0], -wb1,
69 bi->a[1], bi->a[2]);
70 }
71
72 const int count = ARRAY_SIZE(&bia_bind);
73 ARRAY_FOREACH(bi, &bia_bind)
74 {
75 // we only need to free the keybinding
76 FREE(&bi->a[0]);
77 }
78
79 return count;
80}
81
87static void colon_bind(enum MenuType menu, FILE *fp)
88{
89 if (menu == MENU_MAX)
90 {
91 for (enum MenuType i = 1; i < MENU_MAX; i++)
92 {
93 if (print_bind(i, fp) > 0)
94 fprintf(fp, "\n");
95 }
96 }
97 else
98 {
99 print_bind(menu, fp);
100 }
101}
102
109static int print_macro(enum MenuType menu, FILE *fp)
110{
111 struct BindingInfoArray bia_macro = ARRAY_HEAD_INITIALIZER;
112
113 gather_menu(menu, NULL, &bia_macro);
114 if (ARRAY_EMPTY(&bia_macro))
115 return 0;
116
117 ARRAY_SORT(&bia_macro, binding_sort, NULL);
118 const int wm0 = measure_column(&bia_macro, 0);
119
120 const char *menu_name = mutt_map_get_name(menu, MenuNames);
121
122 struct BindingInfo *bi = NULL;
123 ARRAY_FOREACH(bi, &bia_macro)
124 {
125 if (bi->a[2]) // description
126 {
127 fprintf(fp, "macro %s %*s \"%s\" \"%s\"\n", menu_name, -wm0, bi->a[0],
128 bi->a[1], bi->a[2]);
129 }
130 else
131 {
132 fprintf(fp, "macro %s %*s \"%s\"\n", menu_name, -wm0, bi->a[0], bi->a[1]);
133 }
134 }
135
136 const int count = ARRAY_SIZE(&bia_macro);
137 ARRAY_FOREACH(bi, &bia_macro)
138 {
139 // free the keybinding and the macro text
140 FREE(&bi->a[0]);
141 FREE(&bi->a[1]);
142 }
143
144 ARRAY_FREE(&bia_macro);
145 return count;
146}
147
153static void colon_macro(enum MenuType menu, FILE *fp)
154{
155 if (menu == MENU_MAX)
156 {
157 for (enum MenuType i = 1; i < MENU_MAX; i++)
158 {
159 if (print_macro(i, fp) > 0)
160 {
161 //XXX need to elide last blank line
162 fprintf(fp, "\n");
163 }
164 }
165 }
166 else
167 {
168 print_macro(menu, fp);
169 }
170}
171
176 struct Buffer *line, struct Buffer *err)
177{
178 FILE *fp = NULL;
179 bool dump_all = false;
180 bool bind = (cmd->data == 0);
181 struct Buffer *token = buf_pool_get();
182 struct Buffer *tempfile = NULL;
184
185 if (!MoreArgs(line))
186 dump_all = true;
187 else
189
190 if (MoreArgs(line))
191 {
192 /* More arguments potentially means the user is using the
193 * ::command_t :bind command thus we delegate the task. */
194 goto done;
195 }
196
197 tempfile = buf_pool_get();
198 buf_mktemp(tempfile);
199 fp = mutt_file_fopen(buf_string(tempfile), "w");
200 if (!fp)
201 {
202 // L10N: '%s' is the file name of the temporary file
203 buf_printf(err, _("Could not create temporary file %s"), buf_string(tempfile));
204 goto done;
205 }
206
207 if (dump_all || mutt_istr_equal(buf_string(token), "all"))
208 {
209 if (bind)
210 colon_bind(MENU_MAX, fp);
211 else
213 }
214 else
215 {
216 const int menu = mutt_map_get_value(buf_string(token), MenuNames);
217 if (menu == -1)
218 {
219 // L10N: '%s' is the (misspelled) name of the menu, e.g. 'index' or 'pager'
220 buf_printf(err, _("%s: no such menu"), buf_string(token));
221 goto done;
222 }
223
224 if (bind)
225 colon_bind(menu, fp);
226 else
227 colon_macro(menu, fp);
228 }
229
230 if (ftello(fp) == 0)
231 {
232 // L10N: '%s' is the name of the menu, e.g. 'index' or 'pager',
233 // it might also be 'all' when all menus are affected.
234 buf_printf(err, bind ? _("%s: no binds for this menu") : _("%s: no macros for this menu"),
235 dump_all ? "all" : buf_string(token));
236 goto done;
237 }
238 mutt_file_fclose(&fp);
239
240 struct PagerData pdata = { 0 };
241 struct PagerView pview = { &pdata };
242
243 pdata.fname = buf_string(tempfile);
244
245 pview.banner = cmd->name;
247 pview.mode = PAGER_MODE_OTHER;
248
249 mutt_do_pager(&pview, NULL);
250 rc = MUTT_CMD_SUCCESS;
251
252done:
253 mutt_file_fclose(&fp);
254 buf_pool_release(&token);
255 buf_pool_release(&tempfile);
256
257 return rc;
258}
259
263int binding_sort(const void *a, const void *b, void *sdata)
264{
265 const struct BindingInfo *x = (const struct BindingInfo *) a;
266 const struct BindingInfo *y = (const struct BindingInfo *) b;
267
268 int rc = mutt_str_cmp(x->a[0], y->a[0]);
269 if (rc != 0)
270 return rc;
271
272 // No binding, sort by function instead
273 return mutt_str_cmp(x->a[1], y->a[1]);
274}
275
283void escape_macro(const char *macro, struct Buffer *buf)
284{
285 wchar_t wc = 0;
286 size_t k;
287 size_t len = mutt_str_len(macro);
288 mbstate_t mbstate1 = { 0 };
289 mbstate_t mbstate2 = { 0 };
290
291 for (; (len > 0) && (k = mbrtowc(&wc, macro, MB_LEN_MAX, &mbstate1)); macro += k, len -= k)
292 {
293 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
294 {
295 if (k == ICONV_ILLEGAL_SEQ)
296 memset(&mbstate1, 0, sizeof(mbstate1));
297 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : len;
298 wc = ReplacementChar;
299 }
300
301 const int w = wcwidth(wc);
302 if (IsWPrint(wc) && (w >= 0))
303 {
304 char tmp[MB_LEN_MAX * 2] = { 0 };
305 if (wcrtomb(tmp, wc, &mbstate2) != ICONV_ILLEGAL_SEQ)
306 {
307 buf_addstr(buf, tmp);
308 }
309 }
310 else if ((wc < 0x20) || (wc == 0x7f))
311 {
312 if (wc == '\033') // Escape
313 buf_addstr(buf, "\\e");
314 else if (wc == '\n')
315 buf_addstr(buf, "\\n");
316 else if (wc == '\r')
317 buf_addstr(buf, "\\r");
318 else if (wc == '\t')
319 buf_addstr(buf, "\\t");
320 else
321 buf_add_printf(buf, "^%c", (char) ((wc + '@') & 0x7f));
322 }
323 else
324 {
325 buf_addch(buf, '?');
326 }
327 }
328}
329
337static const char *help_lookup_function(int op, enum MenuType menu)
338{
339 if ((menu != MENU_PAGER) && (menu != MENU_EDITOR) && (menu != MENU_GENERIC))
340 {
341 /* first look in the generic map for the function */
342 const char *fn_name = mutt_get_func(OpGeneric, op);
343 if (fn_name)
344 return fn_name;
345 }
346
347 const struct MenuFuncOp *funcs = km_get_table(menu);
348
349 return mutt_get_func(funcs, op);
350}
351
358void gather_menu(enum MenuType menu, struct BindingInfoArray *bia_bind,
359 struct BindingInfoArray *bia_macro)
360{
361 struct Buffer *key_binding = buf_pool_get();
362 struct Buffer *macro = buf_pool_get();
363
364 struct Keymap *map = NULL;
365 STAILQ_FOREACH(map, &Keymaps[menu], entries)
366 {
367 struct BindingInfo bi = { 0 };
368
369 buf_reset(key_binding);
370 km_expand_key(map, key_binding);
371
372 if (map->op == OP_MACRO)
373 {
374 if (!bia_macro || (map->op == OP_NULL))
375 continue;
376
377 buf_reset(macro);
378 escape_macro(map->macro, macro);
379 bi.a[0] = buf_strdup(key_binding);
380 bi.a[1] = buf_strdup(macro);
381 bi.a[2] = map->desc;
382 ARRAY_ADD(bia_macro, bi);
383 }
384 else
385 {
386 if (!bia_bind)
387 continue;
388
389 if (map->op == OP_NULL)
390 {
391 bi.a[0] = buf_strdup(key_binding);
392 bi.a[1] = "noop";
393 ARRAY_ADD(bia_bind, bi);
394 continue;
395 }
396
397 bi.a[0] = buf_strdup(key_binding);
398 bi.a[1] = help_lookup_function(map->op, menu);
399 bi.a[2] = _(opcodes_get_description(map->op));
400 ARRAY_ADD(bia_bind, bi);
401 }
402 }
403
404 buf_pool_release(&key_binding);
405 buf_pool_release(&macro);
406}
407
414int measure_column(struct BindingInfoArray *bia, int col)
415{
416 int max_width = 0;
417
418 struct BindingInfo *bi = NULL;
419 ARRAY_FOREACH(bi, bia)
420 {
421 const int col_width = mutt_strwidth(bi->a[col]);
422 max_width = MAX(max_width, col_width);
423 }
424
425 return max_width;
426}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition array.h:335
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:156
#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_FREE(head)
Release all memory.
Definition array.h:204
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
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
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
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
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
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
Convenience wrapper for the core headers.
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition curs_lib.c:445
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
int parse_extract_token(struct Buffer *dest, struct Buffer *line, 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
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
enum CommandResult parse_bind_macro(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
Parse 'bind' and 'macro' commands - Implements Command::parse() -.
Definition dump.c:175
int binding_sort(const void *a, const void *b, void *sdata)
Compare two BindingInfo by their keybinding - Implements sort_t -.
Definition dump.c:263
const struct MenuFuncOp OpGeneric[]
Functions for the Generic Menu.
Definition functions.c:69
Convenience wrapper for the gui headers.
static void colon_macro(enum MenuType menu, FILE *fp)
Dump the macros.
Definition dump.c:153
int measure_column(struct BindingInfoArray *bia, int col)
Measure one column of a table.
Definition dump.c:414
static const char * help_lookup_function(int op, enum MenuType menu)
Find a keybinding for an operation.
Definition dump.c:337
void gather_menu(enum MenuType menu, struct BindingInfoArray *bia_bind, struct BindingInfoArray *bia_macro)
Gather info about one menu.
Definition dump.c:358
static void colon_bind(enum MenuType menu, FILE *fp)
Dump the key bindings.
Definition dump.c:87
static int print_bind(enum MenuType menu, FILE *fp)
Display the bindings for one menu.
Definition dump.c:50
static int print_macro(enum MenuType menu, FILE *fp)
Display the macros for one menu.
Definition dump.c:109
void escape_macro(const char *macro, struct Buffer *buf)
Escape any special characters in a macro.
Definition dump.c:283
struct KeymapList Keymaps[MENU_MAX]
Array of key mappings, one for each MenuType.
Definition lib.c:125
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:485
bool km_expand_key(struct Keymap *map, struct Buffer *buf)
Get the key string bound to a Keymap.
Definition lib.c:437
Manage keymappings.
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition mapping.c:85
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition mapping.c:42
#define IsWPrint(wc)
Definition mbyte.h:41
#define FREE(x)
Definition memory.h:62
#define MAX(a, b)
Definition memory.h:36
GUI present the user with a selectable list.
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
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition string.c:401
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:672
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:498
const char * opcodes_get_description(int op)
Get the description of an opcode.
Definition opcodes.c:68
GUI display a file/email/help in a viewport with paging.
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition lib.h:60
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition lib.h:140
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
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
Info about one keybinding.
Definition lib.h:95
const char * a[3]
Array of info.
Definition lib.h:96
String manipulation buffer.
Definition buffer.h:36
intptr_t data
Data or flags to pass to the command.
Definition command.h:77
const char * name
Name of the command.
Definition command.h:59
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
Mapping between a function and an operation.
Definition lib.h:117
int op
Operation, e.g. OP_DELETE.
Definition lib.h:119
Data to be displayed by PagerView.
Definition lib.h:159
const char * fname
Name of the file to read.
Definition lib.h:163
Paged view into some data.
Definition lib.h:170
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition lib.h:171
enum PagerMode mode
Pager mode.
Definition lib.h:172
PagerFlags flags
Additional settings to tweak pager's function.
Definition lib.h:173
const char * banner
Title to display in status bar.
Definition lib.h:174
#define buf_mktemp(buf)
Definition tmp.h:33
const struct Mapping MenuNames[]
Menu name lookup table.
Definition type.c:37
MenuType
Types of GUI selections.
Definition type.h:35
@ MENU_GENERIC
Generic selection list.
Definition type.h:45
@ MENU_PAGER
Pager pager (email viewer)
Definition type.h:47
@ MENU_MAX
Definition type.h:52
@ MENU_EDITOR
Text entry area.
Definition type.h:44