NeoMutt  2024-03-23-147-g885fbc
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 <stddef.h>
35#include <stdbool.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 { "undelete-entry", OP_UNDELETE },
67 { NULL, 0 },
68};
69
73const struct MenuFuncOp OpQuery[] = { /* map: query */
74 { "create-alias", OP_CREATE_ALIAS },
75 { "exit", OP_EXIT },
76 { "limit", OP_MAIN_LIMIT },
77 { "mail", OP_MAIL },
78 { "query", OP_QUERY },
79 { "query-append", OP_QUERY_APPEND },
80 { "sort", OP_SORT },
81 { "sort-reverse", OP_SORT_REVERSE },
82 { NULL, 0 },
83};
84
88const struct MenuOpSeq AliasDefaultBindings[] = { /* map: alias */
89 { OP_DELETE, "d" },
90 { OP_EXIT, "q" },
91 { OP_MAIL, "m" },
92 { OP_MAIN_LIMIT, "l" },
93 { OP_SORT, "o" },
94 { OP_SORT_REVERSE, "O" },
95 { OP_TAG, "<space>" },
96 { OP_UNDELETE, "u" },
97 { 0, NULL },
98};
99
103const struct MenuOpSeq QueryDefaultBindings[] = { /* map: query */
104 { OP_CREATE_ALIAS, "a" },
105 { OP_EXIT, "q" },
106 { OP_MAIL, "m" },
107 { OP_MAIN_LIMIT, "l" },
108 { OP_QUERY, "Q" },
109 { OP_QUERY_APPEND, "A" },
110 { OP_SORT, "o" },
111 { OP_SORT_REVERSE, "O" },
112 { 0, NULL },
113};
114// clang-format on
115
119static int op_create_alias(struct AliasMenuData *mdata, int op)
120{
121 struct Menu *menu = mdata->menu;
122
123 if (menu->tag_prefix)
124 {
125 struct AddressList naddr = TAILQ_HEAD_INITIALIZER(naddr);
126
127 struct AliasView *avp = NULL;
128 ARRAY_FOREACH(avp, &mdata->ava)
129 {
130 if (!avp->is_tagged)
131 continue;
132
133 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
134 if (alias_to_addrlist(&al, avp->alias))
135 {
136 mutt_addrlist_copy(&naddr, &al, false);
138 }
139 }
140
141 alias_create(&naddr, mdata->sub);
142 mutt_addrlist_clear(&naddr);
143 }
144 else
145 {
146 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
147 if (alias_to_addrlist(&al, ARRAY_GET(&mdata->ava, menu_get_index(menu))->alias))
148 {
149 alias_create(&al, mdata->sub);
151 }
152 }
153 return FR_SUCCESS;
154}
155
159static int op_delete(struct AliasMenuData *mdata, int op)
160{
161 struct Menu *menu = mdata->menu;
162
163 if (menu->tag_prefix)
164 {
165 struct AliasView *avp = NULL;
166 ARRAY_FOREACH(avp, &mdata->ava)
167 {
168 if (avp->is_tagged)
169 avp->is_deleted = (op == OP_DELETE);
170 }
172 }
173 else
174 {
175 int index = menu_get_index(menu);
176 ARRAY_GET(&mdata->ava, index)->is_deleted = (op == OP_DELETE);
178 const bool c_resolve = cs_subset_bool(mdata->sub, "resolve");
179 if (c_resolve && (index < (menu->max - 1)))
180 {
181 menu_set_index(menu, index + 1);
183 }
184 }
185 return FR_SUCCESS;
186}
187
191static int op_exit(struct AliasMenuData *mdata, int op)
192{
193 return FR_DONE;
194}
195
205static int op_generic_select_entry(struct AliasMenuData *mdata, int op)
206{
207 struct Menu *menu = mdata->menu;
208 if (menu->tag_prefix)
209 {
210 // Untag any non-visible aliases
211 struct AliasView *avp = NULL;
212 ARRAY_FOREACH(avp, &mdata->ava)
213 {
214 if (avp->is_tagged && !avp->is_visible)
215 avp->is_tagged = false;
216 }
217 }
218 else
219 {
220 // Untag all but the current alias
221 struct AliasView *avp = NULL;
222 const int idx = menu_get_index(menu);
223 ARRAY_FOREACH(avp, &mdata->ava)
224 {
225 avp->is_tagged = (ARRAY_FOREACH_IDX == idx);
226 }
227 }
228
229 return FR_CONTINUE;
230}
231
235static int op_main_limit(struct AliasMenuData *mdata, int op)
236{
237 struct Menu *menu = mdata->menu;
238 int rc = mutt_pattern_alias_func(_("Limit to addresses matching: "), mdata, menu);
239 if (rc != 0)
240 return FR_NO_ACTION;
241
242 alias_array_sort(&mdata->ava, mdata->sub);
243 alias_set_title(mdata->sbar, mdata->title, mdata->limit);
245 window_redraw(NULL);
246
247 return FR_SUCCESS;
248}
249
257static int op_query(struct AliasMenuData *mdata, int op)
258{
259 struct Buffer *buf = mdata->query;
260 if ((mw_get_field(_("Query: "), buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) != 0) ||
261 buf_is_empty(buf))
262 {
263 return FR_NO_ACTION;
264 }
265
266 if (op == OP_QUERY)
267 {
268 ARRAY_FREE(&mdata->ava);
269 aliaslist_clear(mdata->al);
270 }
271
272 struct Menu *menu = mdata->menu;
273 struct AliasList al = TAILQ_HEAD_INITIALIZER(al);
274
275 query_run(buf_string(buf), true, &al, mdata->sub);
277 char title[256] = { 0 };
278 snprintf(title, sizeof(title), "%s%s", _("Query: "), buf_string(buf));
279 sbar_set_title(mdata->sbar, title);
280
281 if (TAILQ_EMPTY(&al))
282 {
283 if (op == OP_QUERY)
284 menu->max = 0;
285 return FR_NO_ACTION;
286 }
287
288 struct Alias *np = NULL;
289 struct Alias *tmp = NULL;
290 TAILQ_FOREACH_SAFE(np, &al, entries, tmp)
291 {
292 alias_array_alias_add(&mdata->ava, np);
293 TAILQ_REMOVE(&al, np, entries);
294 TAILQ_INSERT_TAIL(mdata->al, np, entries); // Transfer
295 }
296 alias_array_sort(&mdata->ava, mdata->sub);
297 menu->max = ARRAY_SIZE(&mdata->ava);
298 return FR_SUCCESS;
299}
300
310static int op_search(struct AliasMenuData *mdata, int op)
311{
313 switch (op)
314 {
315 case OP_SEARCH:
316 flags |= SEARCH_PROMPT;
317 mdata->search_state->reverse = false;
318 break;
319 case OP_SEARCH_REVERSE:
320 flags |= SEARCH_PROMPT;
321 mdata->search_state->reverse = true;
322 break;
323 case OP_SEARCH_NEXT:
324 break;
325 case OP_SEARCH_OPPOSITE:
326 flags |= SEARCH_OPPOSITE;
327 break;
328 }
329
330 struct Menu *menu = mdata->menu;
331 int index = menu_get_index(menu);
332 index = mutt_search_alias_command(menu, index, mdata->search_state, flags);
333 if (index == -1)
334 return FR_NO_ACTION;
335
336 menu_set_index(menu, index);
337 return FR_SUCCESS;
338}
339
347static int op_sort(struct AliasMenuData *mdata, int op)
348{
349 int sort = cs_subset_sort(mdata->sub, "sort_alias");
350 bool resort = true;
351 bool reverse = (op == OP_SORT_REVERSE);
352
353 switch (mw_multi_choice(reverse ?
354 /* L10N: The highlighted letters must match the "Sort" options */
355 _("Rev-Sort (a)lias, a(d)dress or (u)nsorted?") :
356 /* L10N: The highlighted letters must match the "Rev-Sort" options */
357 _("Sort (a)lias, a(d)dress or (u)nsorted?"),
358 /* L10N: These must match the highlighted letters from "Sort" and "Rev-Sort" */
359 _("adu")))
360 {
361 case -1: /* abort */
362 resort = false;
363 break;
364
365 case 1: /* (a)lias */
366 sort = SORT_ALIAS;
367 break;
368
369 case 2: /* a(d)dress */
370 sort = SORT_ADDRESS;
371 break;
372
373 case 3: /* (u)nsorted */
374 sort = SORT_ORDER;
375 break;
376 }
377
378 if (resort)
379 {
380 sort |= reverse ? SORT_REVERSE : 0;
381
382 // This will trigger a WA_RECALC
383 cs_subset_str_native_set(mdata->sub, "sort_alias", sort, NULL);
384 }
385
386 return FR_SUCCESS;
387}
388
389// -----------------------------------------------------------------------------
390
394static const struct AliasFunction AliasFunctions[] = {
395 // clang-format off
396 { OP_CREATE_ALIAS, op_create_alias },
397 { OP_DELETE, op_delete },
398 { OP_EXIT, op_exit },
399 { OP_GENERIC_SELECT_ENTRY, op_generic_select_entry },
400 { OP_MAIL, op_generic_select_entry },
401 { OP_MAIN_LIMIT, op_main_limit },
402 { OP_QUERY, op_query },
403 { OP_QUERY_APPEND, op_query },
404 { OP_SEARCH, op_search },
405 { OP_SEARCH_NEXT, op_search },
406 { OP_SEARCH_OPPOSITE, op_search },
407 { OP_SEARCH_REVERSE, op_search },
408 { OP_SORT, op_sort },
409 { OP_SORT_REVERSE, op_sort },
410 { OP_UNDELETE, op_delete },
411 { 0, NULL },
412 // clang-format on
413};
414
419{
420 if (!win || !win->wdata)
421 return FR_UNKNOWN;
422
423 struct Menu *menu = win->wdata;
424 struct AliasMenuData *mdata = menu->mdata;
425 int rc = FR_UNKNOWN;
426 for (size_t i = 0; AliasFunctions[i].op != OP_NULL; i++)
427 {
428 const struct AliasFunction *fn = &AliasFunctions[i];
429 if (fn->op == op)
430 {
431 rc = fn->function(mdata, op);
432 break;
433 }
434 }
435
436 if (rc == FR_UNKNOWN) // Not our function
437 return rc;
438
439 const char *result = dispatcher_get_retval_name(rc);
440 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
441
442 return rc;
443}
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:73
const struct MenuOpSeq QueryDefaultBindings[]
Key bindings for the external Query Menu.
Definition: functions.c:103
const struct MenuOpSeq AliasDefaultBindings[]
Key bindings for the Alias Menu.
Definition: functions.c:88
static const struct AliasFunction AliasFunctions[]
All the NeoMutt functions that the Alias supports.
Definition: functions.c:394
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:290
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: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:273
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:159
static int op_generic_select_entry(struct AliasMenuData *mdata, int op)
select the current entry - Implements alias_function_t -
Definition: functions.c:205
static int op_main_limit(struct AliasMenuData *mdata, int op)
show only messages matching a pattern - Implements alias_function_t -
Definition: functions.c:235
static int op_exit(struct AliasMenuData *mdata, int op)
exit this menu - Implements alias_function_t -
Definition: functions.c:191
static int op_query(struct AliasMenuData *mdata, int op)
query external program for addresses - Implements alias_function_t -
Definition: functions.c:257
static int op_search(struct AliasMenuData *mdata, int op)
search for a regular expression - Implements alias_function_t -
Definition: functions.c:310
static int op_create_alias(struct AliasMenuData *mdata, int op)
create an alias from a message sender - Implements alias_function_t -
Definition: functions.c:119
static int op_sort(struct AliasMenuData *mdata, int op)
sort aliases - Implements alias_function_t -
Definition: functions.c:347
int alias_function_dispatcher(struct MuttWindow *win, int op)
Perform a Alias function - Implements function_dispatcher_t -.
Definition: functions.c:418
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:64
#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: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: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:614
int mutt_pattern_alias_func(char *prompt, struct AliasMenuData *mdata, struct Menu *menu)
Perform some Pattern matching for Alias.
Definition: pattern.c:190
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
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:86
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:145
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