NeoMutt  2024-04-25-1-g3de005
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dump.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stdint.h>
33#include <stdio.h>
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "core/lib.h"
37#include "gui/lib.h"
38#include "key/lib.h"
39#include "menu/lib.h"
40#include "pager/lib.h"
41#include "parse/lib.h"
42
51static bool dump_bind(struct Buffer *buf, enum MenuType menu, const char *name)
52{
53 bool empty = true;
54 struct Keymap *map = NULL;
55
56 STAILQ_FOREACH(map, &Keymaps[menu], entries)
57 {
58 if (map->op == OP_MACRO)
59 continue;
60
61 char key_binding[128] = { 0 };
62 const char *fn_name = NULL;
63
64 km_expand_key(key_binding, sizeof(key_binding), map);
65 if (map->op == OP_NULL)
66 {
67 buf_add_printf(buf, "bind %s %s noop\n", name, key_binding);
68 continue;
69 }
70
71 /* The pager and editor menus don't use the generic map,
72 * however for other menus try generic first. */
73 if ((menu != MENU_PAGER) && (menu != MENU_EDITOR) && (menu != MENU_GENERIC))
74 {
75 fn_name = mutt_get_func(OpGeneric, map->op);
76 }
77
78 /* if it's one of the menus above or generic doesn't find
79 * the function, try with its own menu. */
80 if (!fn_name)
81 {
82 const struct MenuFuncOp *funcs = km_get_table(menu);
83 if (!funcs)
84 continue;
85
86 fn_name = mutt_get_func(funcs, map->op);
87 }
88
89 buf_add_printf(buf, "bind %s %s %s\n", name, key_binding, fn_name);
90 empty = false;
91 }
92
93 return empty;
94}
95
100static void dump_all_binds(struct Buffer *buf)
101{
102 for (enum MenuType i = 1; i < MENU_MAX; i++)
103 {
104 const bool empty = dump_bind(buf, i, mutt_map_get_name(i, MenuNames));
105
106 /* Add a new line for readability between menus. */
107 if (!empty && (i < (MENU_MAX - 1)))
108 buf_addch(buf, '\n');
109 }
110}
111
120static bool dump_macro(struct Buffer *buf, enum MenuType menu, const char *name)
121{
122 bool empty = true;
123 struct Keymap *map = NULL;
124
125 STAILQ_FOREACH(map, &Keymaps[menu], entries)
126 {
127 if (map->op != OP_MACRO)
128 continue;
129
130 char key_binding[128] = { 0 };
131 km_expand_key(key_binding, sizeof(key_binding), map);
132
133 struct Buffer *tmp = buf_pool_get();
134 escape_string(tmp, map->macro);
135
136 if (map->desc)
137 {
138 buf_add_printf(buf, "macro %s %s \"%s\" \"%s\"\n", name, key_binding,
139 tmp->data, map->desc);
140 }
141 else
142 {
143 buf_add_printf(buf, "macro %s %s \"%s\"\n", name, key_binding, tmp->data);
144 }
145
146 buf_pool_release(&tmp);
147 empty = false;
148 }
149
150 return empty;
151}
152
157static void dump_all_macros(struct Buffer *buf)
158{
159 for (enum MenuType i = 1; i < MENU_MAX; i++)
160 {
161 const bool empty = dump_macro(buf, i, mutt_map_get_name(i, MenuNames));
162
163 /* Add a new line for legibility between menus. */
164 if (!empty && (i < (MENU_MAX - 1)))
165 buf_addch(buf, '\n');
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_out = NULL;
176 bool dump_all = false, bind = (data == 0);
177
178 if (!MoreArgs(s))
179 dump_all = true;
180 else
182
183 if (MoreArgs(s))
184 {
185 /* More arguments potentially means the user is using the
186 * ::command_t :bind command thus we delegate the task. */
187 return MUTT_CMD_ERROR;
188 }
189
190 struct Buffer *filebuf = buf_pool_get();
191 if (dump_all || mutt_istr_equal(buf_string(buf), "all"))
192 {
193 if (bind)
194 dump_all_binds(filebuf);
195 else
196 dump_all_macros(filebuf);
197 }
198 else
199 {
200 const int menu_index = mutt_map_get_value(buf_string(buf), MenuNames);
201 if (menu_index == -1)
202 {
203 // L10N: '%s' is the (misspelled) name of the menu, e.g. 'index' or 'pager'
204 buf_printf(err, _("%s: no such menu"), buf_string(buf));
205 buf_pool_release(&filebuf);
206 return MUTT_CMD_ERROR;
207 }
208
209 if (bind)
210 dump_bind(filebuf, menu_index, buf_string(buf));
211 else
212 dump_macro(filebuf, menu_index, buf_string(buf));
213 }
214
215 if (buf_is_empty(filebuf))
216 {
217 // L10N: '%s' is the name of the menu, e.g. 'index' or 'pager',
218 // it might also be 'all' when all menus are affected.
219 buf_printf(err, bind ? _("%s: no binds for this menu") : _("%s: no macros for this menu"),
220 dump_all ? "all" : buf_string(buf));
221 buf_pool_release(&filebuf);
222 return MUTT_CMD_ERROR;
223 }
224
225 struct Buffer *tempfile = buf_pool_get();
226 buf_mktemp(tempfile);
227 fp_out = mutt_file_fopen(buf_string(tempfile), "w");
228 if (!fp_out)
229 {
230 // L10N: '%s' is the file name of the temporary file
231 buf_printf(err, _("Could not create temporary file %s"), buf_string(tempfile));
232 buf_pool_release(&filebuf);
233 buf_pool_release(&tempfile);
234 return MUTT_CMD_ERROR;
235 }
236 fputs(buf_string(filebuf), fp_out);
237
238 mutt_file_fclose(&fp_out);
239 buf_pool_release(&filebuf);
240
241 struct PagerData pdata = { 0 };
242 struct PagerView pview = { &pdata };
243
244 pdata.fname = buf_string(tempfile);
245
246 pview.banner = (bind) ? "bind" : "macro";
248 pview.mode = PAGER_MODE_OTHER;
249
250 mutt_do_pager(&pview, NULL);
251 buf_pool_release(&tempfile);
252
253 return MUTT_CMD_SUCCESS;
254}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:290
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
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:36
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
size_t escape_string(struct Buffer *buf, const char *src)
Write a string to a buffer, escaping special characters.
Definition: dump.c:48
Convenience wrapper for the config headers.
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:123
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:50
#define MoreArgs(buf)
Definition: extract.h:32
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:46
#define mutt_file_fclose(FP)
Definition: file.h:147
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:146
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
const struct MenuFuncOp OpGeneric[]
Functions for the Generic Menu.
Definition: functions.c:68
Convenience wrapper for the gui headers.
static void dump_all_macros(struct Buffer *buf)
Dumps all the macros inside every menu.
Definition: dump.c:157
static void dump_all_binds(struct Buffer *buf)
Dumps all the binds inside every menu.
Definition: dump.c:100
static bool dump_bind(struct Buffer *buf, enum MenuType menu, const char *name)
Dumps all the binds maps of a menu into a buffer.
Definition: dump.c:51
static bool dump_macro(struct Buffer *buf, enum MenuType menu, const char *name)
Dumps all the macros maps of a menu into a buffer.
Definition: dump.c:120
struct KeymapList Keymaps[MENU_MAX]
Array of key mappings, one for each MenuType.
Definition: lib.c:128
int km_expand_key(char *s, size_t len, struct Keymap *map)
Get the key string bound to a Keymap.
Definition: lib.c:460
const char * mutt_get_func(const struct MenuFuncOp *funcs, int op)
Get the name of a function.
Definition: lib.c:324
const struct MenuFuncOp * km_get_table(enum MenuType mtype)
Lookup a Menu's functions.
Definition: lib.c:528
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
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:666
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:142
Text parsing functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
A keyboard mapping.
Definition: lib.h:65
char * macro
Macro expansion (op == OP_MACRO)
Definition: lib.h:66
char * desc
Description of a macro for the help menu.
Definition: lib.h:67
short op
Operation to perform.
Definition: lib.h:68
Mapping between a function and an operation.
Definition: lib.h:101
const char * name
Name of the function.
Definition: lib.h:102
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
const struct Mapping MenuNames[]
Menu name lookup table.
Definition: type.c:37
MenuType
Types of GUI selections.
Definition: type.h:36
@ MENU_GENERIC
Generic selection list.
Definition: type.h:46
@ MENU_PAGER
Pager pager (email viewer)
Definition: type.h:55
@ MENU_MAX
Definition: type.h:60
@ MENU_EDITOR
Text entry area.
Definition: type.h:44