NeoMutt  2024-04-25-97-g7d2481
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#include <stdbool.h>
32#include <stddef.h>
33#include <stdint.h>
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "core/lib.h"
37#include "gui/lib.h"
38#include "mutt.h"
39#include "functions.h"
40#include "lib.h"
41#include "editor/lib.h"
42#include "history/lib.h"
43#include "protos.h"
44#include "type.h"
45
46extern char *SearchBuffers[];
47
48#define MUTT_SEARCH_UP 1
49#define MUTT_SEARCH_DOWN 2
50
58static int search(struct Menu *menu, int op)
59{
60 int rc = -1;
61 int wrap = 0;
62 int search_dir;
63 regex_t re = { 0 };
64 struct Buffer *buf = buf_pool_get();
65
66 char *search_buf = ((menu->type < MENU_MAX)) ? SearchBuffers[menu->type] : NULL;
67
68 if (!(search_buf && *search_buf) || ((op != OP_SEARCH_NEXT) && (op != OP_SEARCH_OPPOSITE)))
69 {
70 buf_strcpy(buf, search_buf && (search_buf[0] != '\0') ? search_buf : "");
71 if ((mw_get_field(((op == OP_SEARCH) || (op == OP_SEARCH_NEXT)) ? _("Search for: ") : _("Reverse search for: "),
72 buf, MUTT_COMP_CLEAR, HC_OTHER, NULL, NULL) != 0) ||
73 buf_is_empty(buf))
74 {
75 goto done;
76 }
77 if (menu->type < MENU_MAX)
78 {
80 search_buf = SearchBuffers[menu->type];
81 }
82 menu->search_dir = ((op == OP_SEARCH) || (op == OP_SEARCH_NEXT)) ?
85 }
86
87 search_dir = (menu->search_dir == MUTT_SEARCH_UP) ? -1 : 1;
88 if (op == OP_SEARCH_OPPOSITE)
89 search_dir = -search_dir;
90
91 if (search_buf)
92 {
93 uint16_t flags = mutt_mb_is_lower(search_buf) ? REG_ICASE : 0;
94 rc = REG_COMP(&re, search_buf, REG_NOSUB | flags);
95 }
96
97 if (rc != 0)
98 {
99 regerror(rc, &re, buf->data, buf->dsize);
100 mutt_error("%s", buf_string(buf));
101 rc = -1;
102 goto done;
103 }
104
105 rc = menu->current + search_dir;
106search_next:
107 if (wrap)
108 mutt_message(_("Search wrapped to top"));
109 while ((rc >= 0) && (rc < menu->max))
110 {
111 if (menu->search(menu, &re, rc) == 0)
112 {
113 regfree(&re);
114 goto done;
115 }
116
117 rc += search_dir;
118 }
119
120 const bool c_wrap_search = cs_subset_bool(menu->sub, "wrap_search");
121 if (c_wrap_search && (wrap++ == 0))
122 {
123 rc = (search_dir == 1) ? 0 : menu->max - 1;
124 goto search_next;
125 }
126 regfree(&re);
127 mutt_error(_("Not found"));
128 rc = -1;
129
130done:
131 buf_pool_release(&buf);
132 return rc;
133}
134
135// -----------------------------------------------------------------------------
136
140static int menu_movement(struct Menu *menu, int op)
141{
142 switch (op)
143 {
144 case OP_BOTTOM_PAGE:
145 menu_bottom_page(menu);
146 return FR_SUCCESS;
147
148 case OP_CURRENT_BOTTOM:
150 return FR_SUCCESS;
151
152 case OP_CURRENT_MIDDLE:
154 return FR_SUCCESS;
155
156 case OP_CURRENT_TOP:
157 menu_current_top(menu);
158 return FR_SUCCESS;
159
160 case OP_FIRST_ENTRY:
161 menu_first_entry(menu);
162 return FR_SUCCESS;
163
164 case OP_HALF_DOWN:
165 menu_half_down(menu);
166 return FR_SUCCESS;
167
168 case OP_HALF_UP:
169 menu_half_up(menu);
170 return FR_SUCCESS;
171
172 case OP_LAST_ENTRY:
173 menu_last_entry(menu);
174 return FR_SUCCESS;
175
176 case OP_MIDDLE_PAGE:
177 menu_middle_page(menu);
178 return FR_SUCCESS;
179
180 case OP_NEXT_ENTRY:
181 menu_next_entry(menu);
182 return FR_SUCCESS;
183
184 case OP_NEXT_LINE:
185 menu_next_line(menu);
186 return FR_SUCCESS;
187
188 case OP_NEXT_PAGE:
189 menu_next_page(menu);
190 return FR_SUCCESS;
191
192 case OP_PREV_ENTRY:
193 menu_prev_entry(menu);
194 return FR_SUCCESS;
195
196 case OP_PREV_LINE:
197 menu_prev_line(menu);
198 return FR_SUCCESS;
199
200 case OP_PREV_PAGE:
201 menu_prev_page(menu);
202 return FR_SUCCESS;
203
204 case OP_TOP_PAGE:
205 menu_top_page(menu);
206 return FR_SUCCESS;
207
208 default:
209 return FR_UNKNOWN;
210 }
211}
212
216static int menu_search(struct Menu *menu, int op)
217{
218 if (menu->search)
219 {
220 int index = search(menu, op);
221 if (index != -1)
222 menu_set_index(menu, index);
223 }
224 return FR_SUCCESS;
225}
226
230static int op_help(struct Menu *menu, int op)
231{
232 mutt_help(menu->type);
233 menu->redraw = MENU_REDRAW_FULL;
234 return FR_SUCCESS;
235}
236
240static int op_jump(struct Menu *menu, int op)
241{
242 if (menu->max == 0)
243 {
244 mutt_error(_("No entries"));
245 return FR_SUCCESS;
246 }
247
248 const int digit = op - OP_JUMP;
249 if ((digit > 0) && (digit < 10))
250 {
251 mutt_unget_ch('0' + digit);
252 }
253
254 struct Buffer *buf = buf_pool_get();
255 if ((mw_get_field(_("Jump to: "), buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) == 0) &&
256 !buf_is_empty(buf))
257 {
258 int n = 0;
259 if (mutt_str_atoi_full(buf_string(buf), &n) && (n > 0) && (n < (menu->max + 1)))
260 {
261 menu_set_index(menu, n - 1); // msg numbers are 0-based
262 }
263 else
264 {
265 mutt_error(_("Invalid index number"));
266 }
267 }
268
269 buf_pool_release(&buf);
270 return FR_SUCCESS;
271}
272
273// -----------------------------------------------------------------------------
274
278static const struct MenuFunction MenuFunctions[] = {
279 // clang-format off
280 { OP_BOTTOM_PAGE, menu_movement },
281 { OP_CURRENT_BOTTOM, menu_movement },
282 { OP_CURRENT_MIDDLE, menu_movement },
283 { OP_CURRENT_TOP, menu_movement },
284 { OP_FIRST_ENTRY, menu_movement },
285 { OP_HALF_DOWN, menu_movement },
286 { OP_HALF_UP, menu_movement },
287 { OP_HELP, op_help },
288 { OP_JUMP, op_jump },
289 { OP_JUMP_1, op_jump },
290 { OP_JUMP_2, op_jump },
291 { OP_JUMP_3, op_jump },
292 { OP_JUMP_4, op_jump },
293 { OP_JUMP_5, op_jump },
294 { OP_JUMP_6, op_jump },
295 { OP_JUMP_7, op_jump },
296 { OP_JUMP_8, op_jump },
297 { OP_JUMP_9, op_jump },
298 { OP_LAST_ENTRY, menu_movement },
299 { OP_MIDDLE_PAGE, menu_movement },
300 { OP_NEXT_ENTRY, menu_movement },
301 { OP_NEXT_LINE, menu_movement },
302 { OP_NEXT_PAGE, menu_movement },
303 { OP_PREV_ENTRY, menu_movement },
304 { OP_PREV_LINE, menu_movement },
305 { OP_PREV_PAGE, menu_movement },
306 { OP_SEARCH, menu_search },
307 { OP_SEARCH_NEXT, menu_search },
308 { OP_SEARCH_OPPOSITE, menu_search },
309 { OP_SEARCH_REVERSE, menu_search },
310 { OP_TOP_PAGE, menu_movement },
311 { 0, NULL },
312 // clang-format on
313};
314
319{
320 if (!win || !win->wdata)
321 return FR_UNKNOWN;
322
323 struct Menu *menu = win->wdata;
324
325 int rc = FR_UNKNOWN;
326 for (size_t i = 0; MenuFunctions[i].op != OP_NULL; i++)
327 {
328 const struct MenuFunction *fn = &MenuFunctions[i];
329 if (fn->op == op)
330 {
331 rc = fn->function(menu, op);
332 break;
333 }
334 }
335
336 if (rc == FR_UNKNOWN) // Not our function
337 return rc;
338
339 const char *result = dispatcher_get_retval_name(rc);
340 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
341
342 return rc;
343}
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
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
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
int digit(const char *s)
void mutt_unget_ch(int ch)
Return a keystroke to the input buffer.
Definition: get.c:115
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_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
Edit a string.
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:318
static int op_help(struct EnterWindowData *wdata, int op)
Display Help - Implements enter_function_t -.
Definition: functions.c:424
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
static int op_jump(struct IndexSharedData *shared, struct IndexPrivateData *priv, int op)
Jump to an index number - Implements index_function_t -.
Definition: functions.c:908
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static int menu_search(struct Menu *menu, int op)
Handle Menu searching - Implements menu_function_t -.
Definition: functions.c:216
static int menu_movement(struct Menu *menu, int op)
Handle all the common Menu movements - Implements menu_function_t -.
Definition: functions.c:140
Convenience wrapper for the gui headers.
void mutt_help(enum MenuType menu)
Display the help menu.
Definition: help.c:467
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
Definition: lib.h:56
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition: mbyte.c:354
#define MUTT_SEARCH_DOWN
Definition: functions.c:49
#define MUTT_SEARCH_UP
Definition: functions.c:48
static const struct MenuFunction MenuFunctions[]
All the NeoMutt functions that the Menu supports.
Definition: functions.c:278
char * SearchBuffers[]
Previous search string, one for each MenuType.
Definition: menu.c:43
static int search(struct Menu *menu, int op)
Search a menu.
Definition: functions.c:58
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:59
MenuRedrawFlags menu_next_page(struct Menu *menu)
Move the focus to the next page in the menu.
Definition: move.c:562
MenuRedrawFlags menu_bottom_page(struct Menu *menu)
Move the focus to the bottom of the page.
Definition: move.c:363
MenuRedrawFlags menu_half_up(struct Menu *menu)
Move the focus up half a page in the menu.
Definition: move.c:506
MenuRedrawFlags menu_prev_line(struct Menu *menu)
Move the view up one line, keeping the selection the same.
Definition: move.c:526
MenuRedrawFlags menu_current_bottom(struct Menu *menu)
Move the current selection to the bottom of the window.
Definition: move.c:484
MenuRedrawFlags menu_current_middle(struct Menu *menu)
Move the current selection to the centre of the window.
Definition: move.c:464
MenuRedrawFlags menu_middle_page(struct Menu *menu)
Move the focus to the centre of the page.
Definition: move.c:343
MenuRedrawFlags menu_first_entry(struct Menu *menu)
Move the focus to the first entry in the menu.
Definition: move.c:410
MenuRedrawFlags menu_half_down(struct Menu *menu)
Move the focus down half a page in the menu.
Definition: move.c:516
MenuRedrawFlags menu_top_page(struct Menu *menu)
Move the focus to the top of the page.
Definition: move.c:333
MenuRedrawFlags menu_last_entry(struct Menu *menu)
Move the focus to the last entry in the menu.
Definition: move.c:426
MenuRedrawFlags menu_prev_page(struct Menu *menu)
Move the focus to the previous page in the menu.
Definition: move.c:552
MenuRedrawFlags menu_next_line(struct Menu *menu)
Move the view down one line, keeping the selection the same.
Definition: move.c:539
MenuRedrawFlags menu_prev_entry(struct Menu *menu)
Move the focus to the previous item in the menu.
Definition: move.c:382
MenuRedrawFlags menu_current_top(struct Menu *menu)
Move the current selection to the top of the window.
Definition: move.c:443
MenuRedrawFlags menu_set_index(struct Menu *menu, int index)
Set the current selection in the Menu.
Definition: menu.c:174
MenuRedrawFlags menu_next_entry(struct Menu *menu)
Move the focus to the next item in the menu.
Definition: move.c:396
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
Many unsorted constants and some structs.
#define MUTT_COMP_NO_FLAGS
No flags are set.
Definition: mutt.h:56
#define MUTT_COMP_CLEAR
Clear input if printable character is pressed.
Definition: mutt.h:57
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
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
Prototypes for many functions.
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition: regex3.h:49
Sidebar functions.
Key value store.
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:36
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
A NeoMutt function.
Definition: functions.h:44
menu_function_t function
Function to call.
Definition: functions.h:46
int op
Op code, e.g. OP_SEARCH.
Definition: functions.h:45
Definition: lib.h:79
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:86
int search_dir
Direction of search.
Definition: lib.h:92
int current
Current entry.
Definition: lib.h:80
MenuRedrawFlags redraw
When to redraw the screen.
Definition: lib.h:82
int(* search)(struct Menu *menu, regex_t *rx, int line)
Definition: lib.h:119
enum MenuType type
Menu definition for keymap entries.
Definition: lib.h:83
struct ConfigSubset * sub
Inherited config items.
Definition: lib.h:87
int max
Number of entries in the menu.
Definition: lib.h:81
void * wdata
Private data.
Definition: mutt_window.h:144
Menu types.
@ MENU_MAX
Definition: type.h:57