NeoMutt  2023-11-03-107-g582dc1
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
functions.c
Go to the documentation of this file.
1
29#include "config.h"
30#ifdef _MAKEDOC
31#include "docs/makedoc_defs.h"
32#else
33#include <stddef.h>
34#include <stdbool.h>
35#include <stdio.h>
36#include "mutt/lib.h"
37#include "address/lib.h"
38#include "config/lib.h"
39#include "core/lib.h"
40#include "gui/lib.h"
41#include "mutt.h"
42#include "lib.h"
43#include "editor/lib.h"
44#include "history/lib.h"
45#include "key/lib.h"
46#include "menu/lib.h"
47#include "pattern/lib.h"
48#include "question/lib.h"
49#include "alias.h"
50#include "functions.h"
51#include "gui.h"
52#endif
53
54// clang-format off
58const struct MenuFuncOp OpAlias[] = { /* map: alias */
59 { "delete-entry", OP_DELETE },
60 { "exit", OP_EXIT },
61 { "limit", OP_MAIN_LIMIT },
62 { "mail", OP_MAIL },
63 { "sort-alias", OP_SORT },
64 { "sort-alias-reverse", OP_SORT_REVERSE },
65 { "undelete-entry", OP_UNDELETE },
66 { NULL, 0 },
67};
68
72const struct MenuFuncOp OpQuery[] = { /* map: query */
73 { "create-alias", OP_CREATE_ALIAS },
74 { "exit", OP_EXIT },
75 { "limit", OP_MAIN_LIMIT },
76 { "mail", OP_MAIL },
77 { "query", OP_QUERY },
78 { "query-append", OP_QUERY_APPEND },
79 { "sort", OP_SORT },
80 { "sort-reverse", OP_SORT_REVERSE },
81 { NULL, 0 },
82};
83
87const struct MenuOpSeq AliasDefaultBindings[] = { /* map: alias */
88 { OP_DELETE, "d" },
89 { OP_EXIT, "q" },
90 { OP_MAIL, "m" },
91 { OP_MAIN_LIMIT, "l" },
92 { OP_SORT, "o" },
93 { OP_SORT_REVERSE, "O" },
94 { OP_TAG, "<space>" },
95 { OP_UNDELETE, "u" },
96 { 0, NULL },
97};
98
102const struct MenuOpSeq QueryDefaultBindings[] = { /* map: query */
103 { OP_CREATE_ALIAS, "a" },
104 { OP_EXIT, "q" },
105 { OP_MAIL, "m" },
106 { OP_MAIN_LIMIT, "l" },
107 { OP_QUERY, "Q" },
108 { OP_QUERY_APPEND, "A" },
109 { OP_SORT, "o" },
110 { OP_SORT_REVERSE, "O" },
111 { 0, NULL },
112};
113// clang-format on
114
118static int op_create_alias(struct AliasMenuData *mdata, int op)
119{
120 struct Menu *menu = mdata->menu;
121
122 if (menu->tag_prefix)
123 {
124 struct AddressList naddr = TAILQ_HEAD_INITIALIZER(naddr);
125
126 struct AliasView *avp = NULL;
127 ARRAY_FOREACH(avp, &mdata->ava)
128 {
129 if (!avp->is_tagged)
130 continue;
131
132 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
133 if (alias_to_addrlist(&al, avp->alias))
134 {
135 mutt_addrlist_copy(&naddr, &al, false);
137 }
138 }
139
140 alias_create(&naddr, mdata->sub);
141 mutt_addrlist_clear(&naddr);
142 }
143 else
144 {
145 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
146 if (alias_to_addrlist(&al, ARRAY_GET(&mdata->ava, menu_get_index(menu))->alias))
147 {
148 alias_create(&al, mdata->sub);
150 }
151 }
152 return FR_SUCCESS;
153}
154
158static int op_delete(struct AliasMenuData *mdata, int op)
159{
160 struct Menu *menu = mdata->menu;
161
162 if (menu->tag_prefix)
163 {
164 struct AliasView *avp = NULL;
165 ARRAY_FOREACH(avp, &mdata->ava)
166 {
167 if (avp->is_tagged)
168 avp->is_deleted = (op == OP_DELETE);
169 }
171 }
172 else
173 {
174 int index = menu_get_index(menu);
175 ARRAY_GET(&mdata->ava, index)->is_deleted = (op == OP_DELETE);
177 const bool c_resolve = cs_subset_bool(mdata->sub, "resolve");
178 if (c_resolve && (index < (menu->max - 1)))
179 {
180 menu_set_index(menu, index + 1);
182 }
183 }
184 return FR_SUCCESS;
185}
186
190static int op_exit(struct AliasMenuData *mdata, int op)
191{
192 return FR_DONE;
193}
194
204static int op_generic_select_entry(struct AliasMenuData *mdata, int op)
205{
206 struct Menu *menu = mdata->menu;
207 if (menu->tag_prefix)
208 {
209 // Untag any non-visible aliases
210 struct AliasView *avp = NULL;
211 ARRAY_FOREACH(avp, &mdata->ava)
212 {
213 if (avp->is_tagged && !avp->is_visible)
214 avp->is_tagged = false;
215 }
216 }
217 else
218 {
219 // Untag all but the current alias
220 struct AliasView *avp = NULL;
221 const int idx = menu_get_index(menu);
222 ARRAY_FOREACH(avp, &mdata->ava)
223 {
224 avp->is_tagged = (ARRAY_FOREACH_IDX == idx);
225 }
226 }
227
228 return FR_CONTINUE;
229}
230
234static int op_main_limit(struct AliasMenuData *mdata, int op)
235{
236 struct Menu *menu = mdata->menu;
237 int rc = mutt_pattern_alias_func(_("Limit to addresses matching: "), mdata, menu);
238 if (rc != 0)
239 return FR_NO_ACTION;
240
241 alias_array_sort(&mdata->ava, mdata->sub);
242 alias_set_title(mdata->sbar, mdata->title, mdata->limit);
244 window_redraw(NULL);
245
246 return FR_SUCCESS;
247}
248
256static int op_query(struct AliasMenuData *mdata, int op)
257{
258 struct Buffer *buf = mdata->query;
259 if ((mw_get_field(_("Query: "), buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) != 0) ||
260 buf_is_empty(buf))
261 {
262 return FR_NO_ACTION;
263 }
264
265 if (op == OP_QUERY)
266 {
267 ARRAY_FREE(&mdata->ava);
268 aliaslist_clear(mdata->al);
269 }
270
271 struct Menu *menu = mdata->menu;
272 struct AliasList al = TAILQ_HEAD_INITIALIZER(al);
273
274 query_run(buf_string(buf), true, &al, mdata->sub);
276 char title[256] = { 0 };
277 snprintf(title, sizeof(title), "%s%s", _("Query: "), buf_string(buf));
278 sbar_set_title(mdata->sbar, title);
279
280 if (TAILQ_EMPTY(&al))
281 {
282 if (op == OP_QUERY)
283 menu->max = 0;
284 return FR_NO_ACTION;
285 }
286
287 struct Alias *np = NULL;
288 struct Alias *tmp = NULL;
289 TAILQ_FOREACH_SAFE(np, &al, entries, tmp)
290 {
291 alias_array_alias_add(&mdata->ava, np);
292 TAILQ_REMOVE(&al, np, entries);
293 TAILQ_INSERT_TAIL(mdata->al, np, entries); // Transfer
294 }
295 alias_array_sort(&mdata->ava, mdata->sub);
296 menu->max = ARRAY_SIZE(&mdata->ava);
297 return FR_SUCCESS;
298}
299
309static int op_search(struct AliasMenuData *mdata, int op)
310{
312 switch (op)
313 {
314 case OP_SEARCH:
315 flags |= SEARCH_PROMPT;
316 mdata->search_state->reverse = false;
317 break;
318 case OP_SEARCH_REVERSE:
319 flags |= SEARCH_PROMPT;
320 mdata->search_state->reverse = true;
321 break;
322 case OP_SEARCH_NEXT:
323 break;
324 case OP_SEARCH_OPPOSITE:
325 flags |= SEARCH_OPPOSITE;
326 break;
327 }
328
329 struct Menu *menu = mdata->menu;
330 int index = menu_get_index(menu);
331 index = mutt_search_alias_command(menu, index, mdata->search_state, flags);
332 if (index == -1)
333 return FR_NO_ACTION;
334
335 menu_set_index(menu, index);
336 return FR_SUCCESS;
337}
338
346static int op_sort(struct AliasMenuData *mdata, int op)
347{
348 int sort = cs_subset_sort(mdata->sub, "sort_alias");
349 bool resort = true;
350 bool reverse = (op == OP_SORT_REVERSE);
351
352 switch (mw_multi_choice(reverse ?
353 /* L10N: The highlighted letters must match the "Sort" options */
354 _("Rev-Sort (a)lias, a(d)dress or (u)nsorted?") :
355 /* L10N: The highlighted letters must match the "Rev-Sort" options */
356 _("Sort (a)lias, a(d)dress or (u)nsorted?"),
357 /* L10N: These must match the highlighted letters from "Sort" and "Rev-Sort" */
358 _("adu")))
359 {
360 case -1: /* abort */
361 resort = false;
362 break;
363
364 case 1: /* (a)lias */
365 sort = SORT_ALIAS;
366 break;
367
368 case 2: /* a(d)dress */
369 sort = SORT_ADDRESS;
370 break;
371
372 case 3: /* (u)nsorted */
373 sort = SORT_ORDER;
374 break;
375 }
376
377 if (resort)
378 {
379 sort |= reverse ? SORT_REVERSE : 0;
380
381 // This will trigger a WA_RECALC
382 cs_subset_str_native_set(mdata->sub, "sort_alias", sort, NULL);
383 }
384
385 return FR_SUCCESS;
386}
387
388// -----------------------------------------------------------------------------
389
393static const struct AliasFunction AliasFunctions[] = {
394 // clang-format off
395 { OP_CREATE_ALIAS, op_create_alias },
396 { OP_DELETE, op_delete },
397 { OP_EXIT, op_exit },
398 { OP_GENERIC_SELECT_ENTRY, op_generic_select_entry },
399 { OP_MAIL, op_generic_select_entry },
400 { OP_MAIN_LIMIT, op_main_limit },
401 { OP_QUERY, op_query },
402 { OP_QUERY_APPEND, op_query },
403 { OP_SEARCH, op_search },
404 { OP_SEARCH_NEXT, op_search },
405 { OP_SEARCH_OPPOSITE, op_search },
406 { OP_SEARCH_REVERSE, op_search },
407 { OP_SORT, op_sort },
408 { OP_SORT_REVERSE, op_sort },
409 { OP_UNDELETE, op_delete },
410 { 0, NULL },
411 // clang-format on
412};
413
418{
419 if (!win || !win->wdata)
420 return FR_UNKNOWN;
421
422 struct Menu *menu = win->wdata;
423 struct AliasMenuData *mdata = menu->mdata;
424 int rc = FR_UNKNOWN;
425 for (size_t i = 0; AliasFunctions[i].op != OP_NULL; i++)
426 {
427 const struct AliasFunction *fn = &AliasFunctions[i];
428 if (fn->op == op)
429 {
430 rc = fn->function(mdata, op);
431 break;
432 }
433 }
434
435 if (rc == FR_UNKNOWN) // Not our function
436 return rc;
437
438 const char *result = dispatcher_get_retval_name(rc);
439 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
440
441 return rc;
442}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition: address.c:762
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1461
Email Address Handling.
const struct MenuFuncOp OpQuery[]
Functions for the external Query Menu.
Definition: functions.c:72
const struct MenuOpSeq QueryDefaultBindings[]
Key bindings for the external Query Menu.
Definition: functions.c:102
const struct MenuOpSeq AliasDefaultBindings[]
Key bindings for the Alias Menu.
Definition: functions.c:87
static const struct AliasFunction AliasFunctions[]
All the NeoMutt functions that the Alias supports.
Definition: functions.c:393
const struct MenuFuncOp OpAlias[]
Functions for the Alias Menu.
Definition: functions.c:58
void alias_array_sort(struct AliasViewArray *ava, const struct ConfigSubset *sub)
Sort and reindex an AliasViewArray.
Definition: sort.c:166
void alias_create(struct AddressList *al, const struct ConfigSubset *sub)
Create a new Alias from an Address.
Definition: alias.c:370
void aliaslist_clear(struct AliasList *al)
Empty a List of Aliases.
Definition: alias.c:664
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:211
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:86
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:203
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:108
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:303
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:267
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
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_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:260
bool alias_to_addrlist(struct AddressList *al, struct Alias *alias)
Turn an Alias into an AddressList.
Definition: dlg_query.c:120
Edit a string.
static int op_delete(struct AliasMenuData *mdata, int op)
delete the current entry - Implements alias_function_t -
Definition: functions.c:158
static int op_generic_select_entry(struct AliasMenuData *mdata, int op)
select the current entry - Implements alias_function_t -
Definition: functions.c:204
static int op_main_limit(struct AliasMenuData *mdata, int op)
show only messages matching a pattern - Implements alias_function_t -
Definition: functions.c:234
static int op_exit(struct AliasMenuData *mdata, int op)
exit this menu - Implements alias_function_t -
Definition: functions.c:190
static int op_query(struct AliasMenuData *mdata, int op)
query external program for addresses - Implements alias_function_t -
Definition: functions.c:256
static int op_search(struct AliasMenuData *mdata, int op)
search for a regular expression - Implements alias_function_t -
Definition: functions.c:309
static int op_create_alias(struct AliasMenuData *mdata, int op)
create an alias from a message sender - Implements alias_function_t -
Definition: functions.c:118
static int op_sort(struct AliasMenuData *mdata, int op)
sort aliases - Implements alias_function_t -
Definition: functions.c:346
int alias_function_dispatcher(struct MuttWindow *win, int op)
Perform a Alias function - Implements function_dispatcher_t -.
Definition: functions.c:417
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:275
int mw_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question -.
Definition: question.c:60
#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:69
Shared code for the Alias and Query Dialogs.
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
Definition: lib.h:56
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:60
#define MENU_REDRAW_INDEX
Redraw the index.
Definition: lib.h:57
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:180
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:156
#define MENU_REDRAW_CURRENT
Redraw the current line of the menu.
Definition: lib.h:59
MenuRedrawFlags menu_set_index(struct Menu *menu, int index)
Set the current selection in the Menu.
Definition: menu.c:170
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:55
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:634
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
Match patterns to emails.
int mutt_search_alias_command(struct Menu *menu, int cur, struct SearchState *state, SearchFlags flags)
Perform a search.
Definition: pattern.c:596
int mutt_pattern_alias_func(char *prompt, struct AliasMenuData *mdata, struct Menu *menu)
Perform some Pattern matching for Alias.
Definition: pattern.c:189
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:45
uint8_t SearchFlags
Flags for a specific search, e.g. SEARCH_PROMPT.
Definition: search_state.h:42
#define SEARCH_NO_FLAGS
No flags are set.
Definition: search_state.h:43
#define SEARCH_PROMPT
Ask for search input.
Definition: search_state.h:44
Sidebar functions.
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:44
@ SORT_ALIAS
Sort by email alias.
Definition: sort2.h:49
@ SORT_ADDRESS
Sort by email address.
Definition: sort2.h:50
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:75
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:52
struct AliasViewArray ava
All Aliases/Queries.
Definition: gui.h:53
struct SearchState * search_state
State of the current search.
Definition: gui.h:61
struct MuttWindow * sbar
Status Bar.
Definition: gui.h:59
struct Menu * menu
Menu.
Definition: gui.h:56
struct Buffer * query
Query string.
Definition: gui.h:57
struct AliasList * al
Alias data.
Definition: gui.h:54
struct ConfigSubset * sub
Config items.
Definition: gui.h:55
GUI data wrapping an Alias.
Definition: gui.h:36
bool is_visible
Is visible?
Definition: gui.h:43
struct Alias * alias
Alias.
Definition: gui.h:44
bool is_deleted
Is it deleted?
Definition: gui.h:42
bool is_tagged
Is it tagged?
Definition: gui.h:41
A shortcut for an email address or addresses.
Definition: alias.h:34
String manipulation buffer.
Definition: buffer.h:34
Mapping between a function and an operation.
Definition: lib.h:102
Mapping between an operation and a key sequence.
Definition: lib.h:111
int op
Operation, e.g. OP_DELETE.
Definition: lib.h:112
Definition: lib.h:70
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:77
void * mdata
Private data.
Definition: lib.h:137
bool tag_prefix
User has pressed <tag-prefix>
Definition: lib.h:76
int max
Number of entries in the menu.
Definition: lib.h:72
void * wdata
Private data.
Definition: mutt_window.h:145
bool reverse
search backwards
Definition: search_state.h:39
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:304