NeoMutt  2025-09-05-43-g177ed6
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 <stdbool.h>
32#include <stdint.h>
33#include <stdio.h>
34#include "mutt/lib.h"
35#include "core/lib.h"
36#include "lib.h"
37#include "menu/lib.h"
38#include "pager/lib.h"
39#include "parse/lib.h"
40
47static int print_bind(enum MenuType menu, FILE *fp)
48{
49 struct BindingInfoArray bia_bind = ARRAY_HEAD_INITIALIZER;
50
51 gather_menu(menu, &bia_bind, NULL);
52 if (ARRAY_EMPTY(&bia_bind))
53 return 0;
54
55 ARRAY_SORT(&bia_bind, binding_sort, NULL);
56 const int wb0 = measure_column(&bia_bind, 0);
57 const int wb1 = measure_column(&bia_bind, 1);
58
59 const char *menu_name = mutt_map_get_name(menu, MenuNames);
60
61 struct BindingInfo *bi = NULL;
62 ARRAY_FOREACH(bi, &bia_bind)
63 {
64 //XXX use description?
65 fprintf(fp, "bind %s %*s %*s # %s\n", menu_name, -wb0, bi->a[0], -wb1,
66 bi->a[1], bi->a[2]);
67 }
68
69 const int count = ARRAY_SIZE(&bia_bind);
70 ARRAY_FOREACH(bi, &bia_bind)
71 {
72 // we only need to free the keybinding
73 FREE(&bi->a[0]);
74 }
75
76 return count;
77}
78
84static void colon_bind(enum MenuType menu, FILE *fp)
85{
86 if (menu == MENU_MAX)
87 {
88 for (enum MenuType i = 1; i < MENU_MAX; i++)
89 {
90 if (print_bind(i, fp) > 0)
91 fprintf(fp, "\n");
92 }
93 }
94 else
95 {
96 print_bind(menu, fp);
97 }
98}
99
106static int print_macro(enum MenuType menu, FILE *fp)
107{
108 struct BindingInfoArray bia_macro = ARRAY_HEAD_INITIALIZER;
109
110 gather_menu(menu, NULL, &bia_macro);
111 if (ARRAY_EMPTY(&bia_macro))
112 return 0;
113
114 ARRAY_SORT(&bia_macro, binding_sort, NULL);
115 const int wm0 = measure_column(&bia_macro, 0);
116
117 const char *menu_name = mutt_map_get_name(menu, MenuNames);
118
119 struct BindingInfo *bi = NULL;
120 ARRAY_FOREACH(bi, &bia_macro)
121 {
122 if (bi->a[2]) // description
123 {
124 fprintf(fp, "macro %s %*s \"%s\" \"%s\"\n", menu_name, -wm0, bi->a[0],
125 bi->a[1], bi->a[2]);
126 }
127 else
128 {
129 fprintf(fp, "macro %s %*s \"%s\"\n", menu_name, -wm0, bi->a[0], bi->a[1]);
130 }
131 }
132
133 const int count = ARRAY_SIZE(&bia_macro);
134 ARRAY_FOREACH(bi, &bia_macro)
135 {
136 // free the keybinding and the macro text
137 FREE(&bi->a[0]);
138 FREE(&bi->a[1]);
139 }
140
141 ARRAY_FREE(&bia_macro);
142 return count;
143}
144
150static void colon_macro(enum MenuType menu, FILE *fp)
151{
152 if (menu == MENU_MAX)
153 {
154 for (enum MenuType i = 1; i < MENU_MAX; i++)
155 {
156 if (print_macro(i, fp) > 0)
157 {
158 //XXX need to elide last blank line
159 fprintf(fp, "\n");
160 }
161 }
162 }
163 else
164 {
165 print_macro(menu, fp);
166 }
167}
168
172enum CommandResult dump_bind_macro(struct Buffer *buf, struct Buffer *s,
173 intptr_t data, struct Buffer *err)
174{
175 FILE *fp = NULL;
176 struct Buffer *tempfile = NULL;
177 bool dump_all = false;
178 bool bind = (data == 0);
179 int rc = MUTT_CMD_ERROR;
180
181 if (!MoreArgs(s))
182 dump_all = true;
183 else
185
186 if (MoreArgs(s))
187 {
188 /* More arguments potentially means the user is using the
189 * ::command_t :bind command thus we delegate the task. */
190 goto done;
191 }
192
193 tempfile = buf_pool_get();
194 buf_mktemp(tempfile);
195 fp = mutt_file_fopen(buf_string(tempfile), "w");
196 if (!fp)
197 {
198 // L10N: '%s' is the file name of the temporary file
199 buf_printf(err, _("Could not create temporary file %s"), buf_string(tempfile));
200 goto done;
201 }
202
203 if (dump_all || mutt_istr_equal(buf_string(buf), "all"))
204 {
205 if (bind)
206 colon_bind(MENU_MAX, fp);
207 else
209 }
210 else
211 {
212 const int menu = mutt_map_get_value(buf_string(buf), MenuNames);
213 if (menu == -1)
214 {
215 // L10N: '%s' is the (misspelled) name of the menu, e.g. 'index' or 'pager'
216 buf_printf(err, _("%s: no such menu"), buf_string(buf));
217 goto done;
218 }
219
220 if (bind)
221 colon_bind(menu, fp);
222 else
223 colon_macro(menu, fp);
224 }
225
226 if (ftello(fp) == 0)
227 {
228 // L10N: '%s' is the name of the menu, e.g. 'index' or 'pager',
229 // it might also be 'all' when all menus are affected.
230 buf_printf(err, bind ? _("%s: no binds for this menu") : _("%s: no macros for this menu"),
231 dump_all ? "all" : buf_string(buf));
232 goto done;
233 }
234 mutt_file_fclose(&fp);
235
236 struct PagerData pdata = { 0 };
237 struct PagerView pview = { &pdata };
238
239 pdata.fname = buf_string(tempfile);
240
241 pview.banner = bind ? "bind" : "macro";
243 pview.mode = PAGER_MODE_OTHER;
244
245 mutt_do_pager(&pview, NULL);
246 rc = MUTT_CMD_SUCCESS;
247
248done:
249 mutt_file_fclose(&fp);
250 buf_pool_release(&tempfile);
251
252 return rc;
253}
#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_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
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.
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 *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
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
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() -.
Definition dump.c:172
int binding_sort(const void *a, const void *b, void *sdata)
Compare two BindingInfo by their keybinding - Implements sort_t -.
Definition lib.c:411
static void colon_macro(enum MenuType menu, FILE *fp)
Dump the macros.
Definition dump.c:150
static void colon_bind(enum MenuType menu, FILE *fp)
Dump the key bindings.
Definition dump.c:84
static int print_bind(enum MenuType menu, FILE *fp)
Display the bindings for one menu.
Definition dump.c:47
static int print_macro(enum MenuType menu, FILE *fp)
Display the macros for one menu.
Definition dump.c:106
int measure_column(struct BindingInfoArray *bia, int col)
Measure one column of a table.
Definition lib.c:430
void gather_menu(enum MenuType menu, struct BindingInfoArray *bia_bind, struct BindingInfoArray *bia_macro)
Gather info about one menu.
Definition lib.c:638
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 FREE(x)
Definition memory.h:62
GUI present the user with a selectable list.
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:672
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
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
char * data
Pointer to data.
Definition buffer.h:37
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:36
@ MENU_MAX
Definition type.h:53