NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
functions.c
Go to the documentation of this file.
1
30#include "config.h"
31#ifdef _MAKEDOC
32#include "docs/makedoc_defs.h"
33#else
34#include <stdbool.h>
35#include <stddef.h>
36#include <stdio.h>
37#include "mutt/lib.h"
38#include "address/lib.h"
39#include "config/lib.h"
40#include "core/lib.h"
41#include "gui/lib.h"
42#include "mutt.h"
43#include "lib.h"
44#include "editor/lib.h"
45#include "history/lib.h"
46#include "key/lib.h"
47#include "menu/lib.h"
48#include "pattern/lib.h"
49#include "question/lib.h"
50#include "alias.h"
51#include "functions.h"
52#include "gui.h"
53#endif
54
55// clang-format off
59const struct MenuFuncOp OpAlias[] = { /* map: alias */
60 { "delete-entry", OP_DELETE },
61 { "exit", OP_EXIT },
62 { "limit", OP_MAIN_LIMIT },
63 { "mail", OP_MAIL },
64 { "sort-alias", OP_SORT },
65 { "sort-alias-reverse", OP_SORT_REVERSE },
66 { "tag-pattern", OP_MAIN_TAG_PATTERN },
67 { "undelete-entry", OP_UNDELETE },
68 { "untag-pattern", OP_MAIN_UNTAG_PATTERN },
69 { NULL, 0 },
70};
71
75const struct MenuFuncOp OpQuery[] = { /* map: query */
76 { "create-alias", OP_CREATE_ALIAS },
77 { "exit", OP_EXIT },
78 { "limit", OP_MAIN_LIMIT },
79 { "mail", OP_MAIL },
80 { "query", OP_QUERY },
81 { "query-append", OP_QUERY_APPEND },
82 { "sort", OP_SORT },
83 { "sort-reverse", OP_SORT_REVERSE },
84 { "tag-pattern", OP_MAIN_TAG_PATTERN },
85 { "untag-pattern", OP_MAIN_UNTAG_PATTERN },
86 { NULL, 0 },
87};
88
92const struct MenuOpSeq AliasDefaultBindings[] = { /* map: alias */
93 { OP_DELETE, "d" },
94 { OP_EXIT, "q" },
95 { OP_MAIL, "m" },
96 { OP_MAIN_LIMIT, "l" },
97 { OP_MAIN_TAG_PATTERN, "T" },
98 { OP_MAIN_UNTAG_PATTERN, "\024" }, // <Ctrl-T>
99 { OP_SORT, "o" },
100 { OP_SORT_REVERSE, "O" },
101 { OP_TAG, "<space>" },
102 { OP_UNDELETE, "u" },
103 { 0, NULL },
104};
105
109const struct MenuOpSeq QueryDefaultBindings[] = { /* map: query */
110 { OP_CREATE_ALIAS, "a" },
111 { OP_EXIT, "q" },
112 { OP_MAIL, "m" },
113 { OP_MAIN_LIMIT, "l" },
114 { OP_MAIN_TAG_PATTERN, "T" },
115 { OP_MAIN_UNTAG_PATTERN, "\024" }, // <Ctrl-T>
116 { OP_QUERY, "Q" },
117 { OP_QUERY_APPEND, "A" },
118 { OP_SORT, "o" },
119 { OP_SORT_REVERSE, "O" },
120 { OP_TAG, "<space>" },
121 { 0, NULL },
122};
123// clang-format on
124
128static int op_create_alias(struct AliasMenuData *mdata, int op)
129{
130 struct Menu *menu = mdata->menu;
131
132 if (menu->tag_prefix)
133 {
134 struct AddressList naddr = TAILQ_HEAD_INITIALIZER(naddr);
135
136 struct AliasView *avp = NULL;
137 ARRAY_FOREACH(avp, &mdata->ava)
138 {
139 if (!avp->is_tagged)
140 continue;
141
142 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
143 if (alias_to_addrlist(&al, avp->alias))
144 {
145 mutt_addrlist_copy(&naddr, &al, false);
147 }
148 }
149
150 alias_create(&naddr, mdata->sub);
151 mutt_addrlist_clear(&naddr);
152 }
153 else
154 {
155 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
156 if (alias_to_addrlist(&al, ARRAY_GET(&mdata->ava, menu_get_index(menu))->alias))
157 {
158 alias_create(&al, mdata->sub);
160 }
161 }
162 return FR_SUCCESS;
163}
164
168static int op_delete(struct AliasMenuData *mdata, int op)
169{
170 struct Menu *menu = mdata->menu;
171
172 if (menu->tag_prefix)
173 {
174 struct AliasView *avp = NULL;
175 ARRAY_FOREACH(avp, &mdata->ava)
176 {
177 if (avp->is_tagged)
178 avp->is_deleted = (op == OP_DELETE);
179 }
181 }
182 else
183 {
184 int index = menu_get_index(menu);
185 ARRAY_GET(&mdata->ava, index)->is_deleted = (op == OP_DELETE);
187 const bool c_resolve = cs_subset_bool(mdata->sub, "resolve");
188 if (c_resolve && (index < (menu->max - 1)))
189 {
190 menu_set_index(menu, index + 1);
192 }
193 }
194 return FR_SUCCESS;
195}
196
200static int op_exit(struct AliasMenuData *mdata, int op)
201{
202 return FR_DONE;
203}
204
214static int op_generic_select_entry(struct AliasMenuData *mdata, int op)
215{
216 struct Menu *menu = mdata->menu;
217 if (menu->tag_prefix)
218 {
219 // Untag any non-visible aliases
220 struct AliasView *avp = NULL;
221 ARRAY_FOREACH(avp, &mdata->ava)
222 {
223 if (avp->is_tagged && !avp->is_visible)
224 avp->is_tagged = false;
225 }
226 }
227 else
228 {
229 // Untag all but the current alias
230 struct AliasView *avp = NULL;
231 const int idx = menu_get_index(menu);
232 ARRAY_FOREACH(avp, &mdata->ava)
233 {
234 avp->is_tagged = (ARRAY_FOREACH_IDX == idx);
235 }
236 }
237
238 return FR_CONTINUE;
239}
240
244static int op_main_limit(struct AliasMenuData *mdata, int op)
245{
246 struct Menu *menu = mdata->menu;
247 int rc = mutt_pattern_alias_func(_("Limit to addresses matching: "), mdata,
248 PAA_VISIBLE, menu);
249 if (rc != 0)
250 return FR_NO_ACTION;
251
252 alias_array_sort(&mdata->ava, mdata->sub);
253 alias_set_title(mdata->sbar, mdata->title, mdata->limit);
255 window_redraw(NULL);
256
257 return FR_SUCCESS;
258}
259
263static int op_main_tag_pattern(struct AliasMenuData *mdata, int op)
264{
265 struct Menu *menu = mdata->menu;
266 int rc = mutt_pattern_alias_func(_("Tag addresses matching: "), mdata, PAA_TAG, menu);
267 if (rc != 0)
268 return FR_NO_ACTION;
269
271 window_redraw(NULL);
272
273 return FR_SUCCESS;
274}
275
279static int op_main_untag_pattern(struct AliasMenuData *mdata, int op)
280{
281 struct Menu *menu = mdata->menu;
282 int rc = mutt_pattern_alias_func(_("Untag addresses matching: "), mdata, PAA_UNTAG, menu);
283 if (rc != 0)
284 return FR_NO_ACTION;
285
287 window_redraw(NULL);
288
289 return FR_SUCCESS;
290}
291
299static int op_query(struct AliasMenuData *mdata, int op)
300{
301 struct Buffer *buf = mdata->query;
302 if ((mw_get_field(_("Query: "), buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) != 0) ||
303 buf_is_empty(buf))
304 {
305 return FR_NO_ACTION;
306 }
307
308 if (op == OP_QUERY)
309 {
310 ARRAY_FREE(&mdata->ava);
311 aliaslist_clear(mdata->al);
312 }
313
314 struct Menu *menu = mdata->menu;
315 struct AliasList al = TAILQ_HEAD_INITIALIZER(al);
316
317 query_run(buf_string(buf), true, &al, mdata->sub);
319 char title[256] = { 0 };
320 snprintf(title, sizeof(title), "%s%s", _("Query: "), buf_string(buf));
321 sbar_set_title(mdata->sbar, title);
322
323 if (TAILQ_EMPTY(&al))
324 {
325 if (op == OP_QUERY)
326 menu->max = 0;
327 return FR_NO_ACTION;
328 }
329
330 struct Alias *np = NULL;
331 struct Alias *tmp = NULL;
332 TAILQ_FOREACH_SAFE(np, &al, entries, tmp)
333 {
334 alias_array_alias_add(&mdata->ava, np);
335 TAILQ_REMOVE(&al, np, entries);
336 TAILQ_INSERT_TAIL(mdata->al, np, entries); // Transfer
337 }
338 alias_array_sort(&mdata->ava, mdata->sub);
339 menu->max = ARRAY_SIZE(&mdata->ava);
340 return FR_SUCCESS;
341}
342
352static int op_search(struct AliasMenuData *mdata, int op)
353{
355 switch (op)
356 {
357 case OP_SEARCH:
358 flags |= SEARCH_PROMPT;
359 mdata->search_state->reverse = false;
360 break;
361 case OP_SEARCH_REVERSE:
362 flags |= SEARCH_PROMPT;
363 mdata->search_state->reverse = true;
364 break;
365 case OP_SEARCH_NEXT:
366 break;
367 case OP_SEARCH_OPPOSITE:
368 flags |= SEARCH_OPPOSITE;
369 break;
370 }
371
372 struct Menu *menu = mdata->menu;
373 int index = menu_get_index(menu);
374 index = mutt_search_alias_command(menu, index, mdata->search_state, flags);
375 if (index == -1)
376 return FR_NO_ACTION;
377
378 menu_set_index(menu, index);
379 return FR_SUCCESS;
380}
381
389static int op_sort(struct AliasMenuData *mdata, int op)
390{
391 int sort = cs_subset_sort(mdata->sub, "sort_alias");
392 bool resort = true;
393 bool reverse = (op == OP_SORT_REVERSE);
394
395 switch (mw_multi_choice(reverse ?
396 /* L10N: The highlighted letters must match the "Sort" options */
397 _("Rev-Sort (a)lias, a(d)dress or (u)nsorted?") :
398 /* L10N: The highlighted letters must match the "Rev-Sort" options */
399 _("Sort (a)lias, a(d)dress or (u)nsorted?"),
400 /* L10N: These must match the highlighted letters from "Sort" and "Rev-Sort" */
401 _("adu")))
402 {
403 case -1: /* abort */
404 resort = false;
405 break;
406
407 case 1: /* (a)lias */
408 sort = SORT_ALIAS;
409 break;
410
411 case 2: /* a(d)dress */
412 sort = SORT_ADDRESS;
413 break;
414
415 case 3: /* (u)nsorted */
416 sort = SORT_ORDER;
417 break;
418 }
419
420 if (resort)
421 {
422 sort |= reverse ? SORT_REVERSE : 0;
423
424 // This will trigger a WA_RECALC
425 cs_subset_str_native_set(mdata->sub, "sort_alias", sort, NULL);
426 }
427
428 return FR_SUCCESS;
429}
430
431// -----------------------------------------------------------------------------
432
436static const struct AliasFunction AliasFunctions[] = {
437 // clang-format off
438 { OP_CREATE_ALIAS, op_create_alias },
439 { OP_DELETE, op_delete },
440 { OP_EXIT, op_exit },
441 { OP_GENERIC_SELECT_ENTRY, op_generic_select_entry },
442 { OP_MAIL, op_generic_select_entry },
443 { OP_MAIN_LIMIT, op_main_limit },
444 { OP_MAIN_TAG_PATTERN, op_main_tag_pattern },
445 { OP_MAIN_UNTAG_PATTERN, op_main_untag_pattern },
446 { OP_QUERY, op_query },
447 { OP_QUERY_APPEND, op_query },
448 { OP_SEARCH, op_search },
449 { OP_SEARCH_NEXT, op_search },
450 { OP_SEARCH_OPPOSITE, op_search },
451 { OP_SEARCH_REVERSE, op_search },
452 { OP_SORT, op_sort },
453 { OP_SORT_REVERSE, op_sort },
454 { OP_UNDELETE, op_delete },
455 { 0, NULL },
456 // clang-format on
457};
458
463{
464 // The Dispatcher may be called on any Window in the Dialog
465 struct MuttWindow *dlg = dialog_find(win);
466 if (!dlg || !dlg->wdata)
467 return FR_ERROR;
468
469 struct Menu *menu = dlg->wdata;
470 struct AliasMenuData *mdata = menu->mdata;
471
472 int rc = FR_UNKNOWN;
473 for (size_t i = 0; AliasFunctions[i].op != OP_NULL; i++)
474 {
475 const struct AliasFunction *fn = &AliasFunctions[i];
476 if (fn->op == op)
477 {
478 rc = fn->function(mdata, op);
479 break;
480 }
481 }
482
483 if (rc == FR_UNKNOWN) // Not our function
484 return rc;
485
486 const char *result = dispatcher_get_retval_name(rc);
487 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
488
489 return rc;
490}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition: address.c:765
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1460
Email Address Handling.
const struct MenuFuncOp OpQuery[]
Functions for the external Query Menu.
Definition: functions.c:75
const struct MenuOpSeq QueryDefaultBindings[]
Key bindings for the external Query Menu.
Definition: functions.c:109
const struct MenuOpSeq AliasDefaultBindings[]
Key bindings for the Alias Menu.
Definition: functions.c:92
static const struct AliasFunction AliasFunctions[]
All the NeoMutt functions that the Alias supports.
Definition: functions.c:436
const struct MenuFuncOp OpAlias[]
Functions for the Alias Menu.
Definition: functions.c:59
void alias_array_sort(struct AliasViewArray *ava, const struct ConfigSubset *sub)
Sort and reindex an AliasViewArray.
Definition: sort.c:168
void alias_create(struct AddressList *al, const struct ConfigSubset *sub)
Create a new Alias from an Address.
Definition: alias.c:367
void aliaslist_clear(struct AliasList *al)
Empty a List of Aliases.
Definition: alias.c:697
Representation of a single alias to an email address.
int alias_array_alias_add(struct AliasViewArray *ava, struct Alias *alias)
Add an Alias to the AliasViewArray.
Definition: array.c:47
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#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_GET(head, idx)
Return the element at index.
Definition: array.h:109
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:266
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
struct MuttWindow * dialog_find(struct MuttWindow *win)
Find the parent Dialog of a Window.
Definition: dialog.c:89
const char * dispatcher_get_retval_name(int rv)
Get the name of a return value.
Definition: dispatcher.c:54
@ FR_SUCCESS
Valid function - successfully performed.
Definition: dispatcher.h:39
@ FR_DONE
Exit the Dialog.
Definition: dispatcher.h:35
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
@ FR_ERROR
Valid function - error occurred.
Definition: dispatcher.h:38
@ FR_CONTINUE
Remain in the Dialog.
Definition: dispatcher.h:34
@ FR_NO_ACTION
Valid function - no action performed.
Definition: dispatcher.h:37
int query_run(const char *s, bool verbose, struct AliasList *al, const struct ConfigSubset *sub)
Run an external program to find Addresses.
Definition: dlg_query.c:275
bool alias_to_addrlist(struct AddressList *al, struct Alias *alias)
Turn an Alias into an AddressList.
Definition: dlg_query.c:121
Edit a string.
static int op_delete(struct AliasMenuData *mdata, int op)
delete the current entry - Implements alias_function_t -
Definition: functions.c:168
static int op_generic_select_entry(struct AliasMenuData *mdata, int op)
select the current entry - Implements alias_function_t -
Definition: functions.c:214
static int op_main_tag_pattern(struct AliasMenuData *mdata, int op)
Tag messages matching a pattern - Implements alias_function_t -.
Definition: functions.c:263
static int op_main_limit(struct AliasMenuData *mdata, int op)
show only messages matching a pattern - Implements alias_function_t -
Definition: functions.c:244
static int op_exit(struct AliasMenuData *mdata, int op)
exit this menu - Implements alias_function_t -
Definition: functions.c:200
static int op_main_untag_pattern(struct AliasMenuData *mdata, int op)
Untag messages matching a pattern - Implements alias_function_t -.
Definition: functions.c:279
static int op_query(struct AliasMenuData *mdata, int op)
query external program for addresses - Implements alias_function_t -
Definition: functions.c:299
static int op_search(struct AliasMenuData *mdata, int op)
search for a regular expression - Implements alias_function_t -
Definition: functions.c:352
static int op_create_alias(struct AliasMenuData *mdata, int op)
create an alias from a message sender - Implements alias_function_t -
Definition: functions.c:128
static int op_sort(struct AliasMenuData *mdata, int op)
sort aliases - Implements alias_function_t -
Definition: functions.c:389
int alias_function_dispatcher(struct MuttWindow *win, int op)
Perform a Alias function - Implements function_dispatcher_t -.
Definition: functions.c:462
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition: window.c:274
int mw_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question -.
Definition: question.c:63
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
Convenience wrapper for the gui headers.
void alias_set_title(struct MuttWindow *sbar, char *menu_name, char *limit)
Create a title string for the Menu.
Definition: gui.c:72
Shared code for the Alias and Query Dialogs.
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
Definition: lib.h:59
Manage keymappings.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:59
#define MENU_REDRAW_INDEX
Redraw the index.
Definition: lib.h:56
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:184
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:160
#define MENU_REDRAW_CURRENT
Redraw the current line of the menu.
Definition: lib.h:58
MenuRedrawFlags menu_set_index(struct Menu *menu, int index)
Set the current selection in the Menu.
Definition: menu.c:174
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
Many unsorted constants and some structs.
#define MUTT_COMP_NO_FLAGS
No flags are set.
Definition: mutt.h:56
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:633
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
Match patterns to emails.
@ PAA_VISIBLE
Set AliasView.is_visible and hide the rest.
Definition: lib.h:189
@ PAA_TAG
Set AliasView.is_tagged, but don't touch the others.
Definition: lib.h:187
@ PAA_UNTAG
Unset AliasView.is_tagged, but don't touch the others.
Definition: lib.h:188
int mutt_pattern_alias_func(char *prompt, struct AliasMenuData *mdata, enum PatternAlias action, struct Menu *menu)
Perform some Pattern matching for Alias.
Definition: pattern.c:191
int mutt_search_alias_command(struct Menu *menu, int cur, struct SearchState *state, SearchFlags flags)
Perform a search.
Definition: pattern.c:636
Ask the user a question.
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:735
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:809
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
#define TAILQ_EMPTY(head)
Definition: queue.h:721
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:227
#define SEARCH_OPPOSITE
Search in the opposite direction.
Definition: search_state.h:46
uint8_t SearchFlags
Flags for a specific search, e.g. SEARCH_PROMPT.
Definition: search_state.h:43
#define SEARCH_NO_FLAGS
No flags are set.
Definition: search_state.h:44
#define SEARCH_PROMPT
Ask for search input.
Definition: search_state.h:45
Sidebar functions.
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:40
@ SORT_ALIAS
Sort by email alias.
Definition: sort2.h:45
@ SORT_ADDRESS
Sort by email address.
Definition: sort2.h:46
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:71
Key value store.
#define NONULL(x)
Definition: string2.h:37
A NeoMutt function.
Definition: functions.h:52
int op
Op code, e.g. OP_SEARCH.
Definition: functions.h:53
alias_function_t function
Function to call.
Definition: functions.h:54
AliasView array wrapper with Pattern information -.
Definition: gui.h:54
struct AliasViewArray ava
All Aliases/Queries.
Definition: gui.h:55
struct SearchState * search_state
State of the current search.
Definition: gui.h:63
struct MuttWindow * sbar
Status Bar.
Definition: gui.h:61
struct Menu * menu
Menu.
Definition: gui.h:58
struct Buffer * query
Query string.
Definition: gui.h:59
struct AliasList * al
Alias data.
Definition: gui.h:56
struct ConfigSubset * sub
Config items.
Definition: gui.h:57
GUI data wrapping an Alias.
Definition: gui.h:38
bool is_visible
Is visible?
Definition: gui.h:45
struct Alias * alias
Alias.
Definition: gui.h:46
bool is_deleted
Is it deleted?
Definition: gui.h:44
bool is_tagged
Is it tagged?
Definition: gui.h:43
A shortcut for an email address or addresses.
Definition: alias.h:35
String manipulation buffer.
Definition: buffer.h:36
Mapping between a function and an operation.
Definition: lib.h:101
Mapping between an operation and a key sequence.
Definition: lib.h:110
int op
Operation, e.g. OP_DELETE.
Definition: lib.h:111
Definition: lib.h:79
void * mdata
Private data.
Definition: lib.h:147
bool tag_prefix
User has pressed <tag-prefix>
Definition: lib.h:85
int max
Number of entries in the menu.
Definition: lib.h:81
void * wdata
Private data.
Definition: mutt_window.h:144
bool reverse
search backwards
Definition: search_state.h:40
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:297