NeoMutt
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#include <stddef.h>
31#include <stdbool.h>
32#include "private.h"
33#include "mutt/lib.h"
34#include "config/lib.h"
35#include "core/lib.h"
36#include "gui/lib.h"
37#include "functions.h"
38#include "lib.h"
39#include "index/lib.h"
40
46bool sb_next(struct SidebarWindowData *wdata)
47{
48 struct SbEntry **sbep = NULL;
49 ARRAY_FOREACH_FROM(sbep, &wdata->entries, wdata->hil_index + 1)
50 {
51 if (!(*sbep)->is_hidden)
52 {
53 wdata->hil_index = ARRAY_FOREACH_IDX;
54 return true;
55 }
56 }
57
58 return false;
59}
60
69static struct SbEntry **sb_next_new(struct SidebarWindowData *wdata, size_t begin, size_t end)
70{
71 struct SbEntry **sbep = NULL;
72 ARRAY_FOREACH_FROM_TO(sbep, &wdata->entries, begin, end)
73 {
74 if ((*sbep)->mailbox->has_new || ((*sbep)->mailbox->msg_unread != 0))
75 return sbep;
76 }
77 return NULL;
78}
79
85bool sb_prev(struct SidebarWindowData *wdata)
86{
87 struct SbEntry **sbep = NULL, **prev = NULL;
88 ARRAY_FOREACH_TO(sbep, &wdata->entries, wdata->hil_index)
89 {
90 if (!(*sbep)->is_hidden)
91 prev = sbep;
92 }
93
94 if (prev)
95 {
96 wdata->hil_index = ARRAY_IDX(&wdata->entries, prev);
97 return true;
98 }
99
100 return false;
101}
102
111static struct SbEntry **sb_prev_new(struct SidebarWindowData *wdata, size_t begin, size_t end)
112{
113 struct SbEntry **sbep = NULL, **prev = NULL;
114 ARRAY_FOREACH_FROM_TO(sbep, &wdata->entries, begin, end)
115 {
116 if ((*sbep)->mailbox->has_new || ((*sbep)->mailbox->msg_unread != 0))
117 prev = sbep;
118 }
119
120 return prev;
121}
122
123// -----------------------------------------------------------------------------
124
128static int op_sidebar_first(struct SidebarWindowData *wdata, int op)
129{
130 if (!mutt_window_is_visible(wdata->win))
131 return FR_NO_ACTION;
132
133 if (ARRAY_EMPTY(&wdata->entries) || (wdata->hil_index < 0))
134 return FR_NO_ACTION;
135
136 int orig_hil_index = wdata->hil_index;
137
138 wdata->hil_index = 0;
139 if ((*ARRAY_GET(&wdata->entries, wdata->hil_index))->is_hidden)
140 if (!sb_next(wdata))
141 wdata->hil_index = orig_hil_index;
142
143 if (orig_hil_index == wdata->hil_index)
144 return FR_NO_ACTION;
145
146 wdata->win->actions |= WA_RECALC;
147 return FR_SUCCESS;
148}
149
153static int op_sidebar_last(struct SidebarWindowData *wdata, int op)
154{
155 if (!mutt_window_is_visible(wdata->win))
156 return FR_NO_ACTION;
157
158 if (ARRAY_EMPTY(&wdata->entries) || (wdata->hil_index < 0))
159 return FR_NO_ACTION;
160
161 int orig_hil_index = wdata->hil_index;
162
163 wdata->hil_index = ARRAY_SIZE(&wdata->entries);
164 if (!sb_prev(wdata))
165 wdata->hil_index = orig_hil_index;
166
167 if (orig_hil_index == wdata->hil_index)
168 return FR_NO_ACTION;
169
170 wdata->win->actions |= WA_RECALC;
171 return FR_SUCCESS;
172}
173
177static int op_sidebar_next(struct SidebarWindowData *wdata, int op)
178{
179 if (!mutt_window_is_visible(wdata->win))
180 return FR_NO_ACTION;
181
182 if (ARRAY_EMPTY(&wdata->entries) || (wdata->hil_index < 0))
183 return FR_NO_ACTION;
184
185 if (!sb_next(wdata))
186 return FR_NO_ACTION;
187
188 wdata->win->actions |= WA_RECALC;
189 return FR_SUCCESS;
190}
191
197static int op_sidebar_next_new(struct SidebarWindowData *wdata, int op)
198{
199 if (!mutt_window_is_visible(wdata->win))
200 return FR_NO_ACTION;
201
202 const size_t max_entries = ARRAY_SIZE(&wdata->entries);
203 if ((max_entries == 0) || (wdata->hil_index < 0))
204 return FR_NO_ACTION;
205
206 const bool c_sidebar_next_new_wrap = cs_subset_bool(NeoMutt->sub, "sidebar_next_new_wrap");
207 struct SbEntry **sbep = NULL;
208 if ((sbep = sb_next_new(wdata, wdata->hil_index + 1, max_entries)) ||
209 (c_sidebar_next_new_wrap && (sbep = sb_next_new(wdata, 0, wdata->hil_index))))
210 {
211 wdata->hil_index = ARRAY_IDX(&wdata->entries, sbep);
212 wdata->win->actions |= WA_RECALC;
213 return FR_SUCCESS;
214 }
215
216 return FR_NO_ACTION;
217}
218
222static int op_sidebar_open(struct SidebarWindowData *wdata, int op)
223{
224 struct MuttWindow *win_sidebar = wdata->win;
225 if (!mutt_window_is_visible(win_sidebar))
226 return FR_NO_ACTION;
227
228 struct MuttWindow *dlg = dialog_find(win_sidebar);
229 index_change_folder(dlg, sb_get_highlight(win_sidebar));
230 return FR_SUCCESS;
231}
232
237{
238 if (!mutt_window_is_visible(wdata->win))
239 return FR_NO_ACTION;
240
241 if (ARRAY_EMPTY(&wdata->entries) || (wdata->bot_index < 0))
242 return FR_NO_ACTION;
243
244 int orig_hil_index = wdata->hil_index;
245
246 wdata->hil_index = wdata->bot_index;
247 sb_next(wdata);
248 /* If the rest of the entries are hidden, go up to the last unhidden one */
249 if ((*ARRAY_GET(&wdata->entries, wdata->hil_index))->is_hidden)
250 sb_prev(wdata);
251
252 if (orig_hil_index == wdata->hil_index)
253 return FR_NO_ACTION;
254
255 wdata->win->actions |= WA_RECALC;
256 return FR_SUCCESS;
257}
258
262static int op_sidebar_page_up(struct SidebarWindowData *wdata, int op)
263{
264 if (!mutt_window_is_visible(wdata->win))
265 return FR_NO_ACTION;
266
267 if (ARRAY_EMPTY(&wdata->entries) || (wdata->top_index < 0))
268 return FR_NO_ACTION;
269
270 int orig_hil_index = wdata->hil_index;
271
272 wdata->hil_index = wdata->top_index;
273 sb_prev(wdata);
274 /* If the rest of the entries are hidden, go down to the last unhidden one */
275 if ((*ARRAY_GET(&wdata->entries, wdata->hil_index))->is_hidden)
276 sb_next(wdata);
277
278 if (orig_hil_index == wdata->hil_index)
279 return FR_NO_ACTION;
280
281 wdata->win->actions |= WA_RECALC;
282 return FR_SUCCESS;
283}
284
288static int op_sidebar_prev(struct SidebarWindowData *wdata, int op)
289{
290 if (!mutt_window_is_visible(wdata->win))
291 return FR_NO_ACTION;
292
293 if (ARRAY_EMPTY(&wdata->entries) || (wdata->hil_index < 0))
294 return FR_NO_ACTION;
295
296 if (!sb_prev(wdata))
297 return FR_NO_ACTION;
298
299 wdata->win->actions |= WA_RECALC;
300 return FR_SUCCESS;
301}
302
308static int op_sidebar_prev_new(struct SidebarWindowData *wdata, int op)
309{
310 if (!mutt_window_is_visible(wdata->win))
311 return FR_NO_ACTION;
312
313 const size_t max_entries = ARRAY_SIZE(&wdata->entries);
314 if ((max_entries == 0) || (wdata->hil_index < 0))
315 return FR_NO_ACTION;
316
317 const bool c_sidebar_next_new_wrap = cs_subset_bool(NeoMutt->sub, "sidebar_next_new_wrap");
318 struct SbEntry **sbep = NULL;
319 if ((sbep = sb_prev_new(wdata, 0, wdata->hil_index)) ||
320 (c_sidebar_next_new_wrap &&
321 (sbep = sb_prev_new(wdata, wdata->hil_index + 1, max_entries))))
322 {
323 wdata->hil_index = ARRAY_IDX(&wdata->entries, sbep);
324 wdata->win->actions |= WA_RECALC;
325 return FR_SUCCESS;
326 }
327
328 return FR_NO_ACTION;
329}
330
334static int op_sidebar_toggle_visible(struct SidebarWindowData *wdata, int op)
335{
336 bool_str_toggle(NeoMutt->sub, "sidebar_visible", NULL);
337 mutt_window_reflow(NULL);
338 return FR_SUCCESS;
339}
340
344static int op_sidebar_toggle_virtual(struct SidebarWindowData *wdata, int op)
345{
346 return FR_SUCCESS;
347}
348
349// -----------------------------------------------------------------------------
350
354static const struct SidebarFunction SidebarFunctions[] = {
355 // clang-format off
356 { OP_SIDEBAR_FIRST, op_sidebar_first },
357 { OP_SIDEBAR_LAST, op_sidebar_last },
358 { OP_SIDEBAR_NEXT, op_sidebar_next },
359 { OP_SIDEBAR_NEXT_NEW, op_sidebar_next_new },
360 { OP_SIDEBAR_OPEN, op_sidebar_open },
361 { OP_SIDEBAR_PAGE_DOWN, op_sidebar_page_down },
362 { OP_SIDEBAR_PAGE_UP, op_sidebar_page_up },
363 { OP_SIDEBAR_PREV, op_sidebar_prev },
364 { OP_SIDEBAR_PREV_NEW, op_sidebar_prev_new },
365 { OP_SIDEBAR_TOGGLE_VIRTUAL, op_sidebar_toggle_virtual },
366 { OP_SIDEBAR_TOGGLE_VISIBLE, op_sidebar_toggle_visible },
367 { 0, NULL },
368 // clang-format on
369};
370
375{
376 if (!win || !win->wdata)
377 return FR_UNKNOWN;
378
379 struct SidebarWindowData *wdata = win->wdata;
380 int rc = FR_UNKNOWN;
381 for (size_t i = 0; SidebarFunctions[i].op != OP_NULL; i++)
382 {
383 const struct SidebarFunction *fn = &SidebarFunctions[i];
384 if (fn->op == op)
385 {
386 rc = fn->function(wdata, op);
387 break;
388 }
389 }
390
391 if (rc == FR_UNKNOWN) // Not our function
392 return rc;
393
394 const char *result = dispatcher_get_retval_name(rc);
395 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
396
397 return FR_SUCCESS; // Whatever the outcome, we handled it
398}
#define ARRAY_IDX(head, elem)
Return the index of an element of the array.
Definition: array.h:258
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition: array.h:73
#define ARRAY_FOREACH_TO(elem, head, to)
Iterate from the beginning to an index.
Definition: array.h:233
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:86
#define ARRAY_FOREACH_FROM(elem, head, from)
Iterate from an index to the end.
Definition: array.h:222
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:108
#define ARRAY_FOREACH_FROM_TO(elem, head, from, to)
Iterate between two indexes.
Definition: array.h:246
int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:214
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
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_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
@ FR_NO_ACTION
Valid function - no action performed.
Definition: dispatcher.h:37
void index_change_folder(struct MuttWindow *dlg, struct Mailbox *m)
Change the current folder, cautiously.
Definition: dlg_index.c:1433
int sb_function_dispatcher(struct MuttWindow *win, int op)
Perform a Sidebar function - Implements function_dispatcher_t -.
Definition: functions.c:374
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static int op_sidebar_page_up(struct SidebarWindowData *wdata, int op)
Selects the last entry in the previous page of mailboxes - Implements sidebar_function_t -.
Definition: functions.c:262
static int op_sidebar_page_down(struct SidebarWindowData *wdata, int op)
Selects the first entry in the next page of mailboxes - Implements sidebar_function_t -.
Definition: functions.c:236
static int op_sidebar_toggle_virtual(struct SidebarWindowData *wdata, int op)
Deprecated - Implements sidebar_function_t -.
Definition: functions.c:344
static int op_sidebar_next_new(struct SidebarWindowData *wdata, int op)
Selects the next new mailbox - Implements sidebar_function_t -.
Definition: functions.c:197
static int op_sidebar_next(struct SidebarWindowData *wdata, int op)
Selects the next unhidden mailbox - Implements sidebar_function_t -.
Definition: functions.c:177
static int op_sidebar_toggle_visible(struct SidebarWindowData *wdata, int op)
Make the sidebar (in)visible - Implements sidebar_function_t -.
Definition: functions.c:334
static int op_sidebar_prev(struct SidebarWindowData *wdata, int op)
Selects the previous unhidden mailbox - Implements sidebar_function_t -.
Definition: functions.c:288
static int op_sidebar_prev_new(struct SidebarWindowData *wdata, int op)
Selects the previous new mailbox - Implements sidebar_function_t -.
Definition: functions.c:308
static int op_sidebar_first(struct SidebarWindowData *wdata, int op)
Selects the first unhidden mailbox - Implements sidebar_function_t -.
Definition: functions.c:128
static int op_sidebar_open(struct SidebarWindowData *wdata, int op)
Open highlighted mailbox - Implements sidebar_function_t -.
Definition: functions.c:222
static int op_sidebar_last(struct SidebarWindowData *wdata, int op)
Selects the last unhidden mailbox - Implements sidebar_function_t -.
Definition: functions.c:153
Convenience wrapper for the gui headers.
GUI manage the main index (list of emails)
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
Convenience wrapper for the library headers.
bool mutt_window_is_visible(struct MuttWindow *win)
Is the Window visible?
Definition: mutt_window.c:512
void mutt_window_reflow(struct MuttWindow *win)
Resize a Window and its children.
Definition: mutt_window.c:344
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
static const struct SidebarFunction SidebarFunctions[]
All the NeoMutt functions that the Sidebar supports.
Definition: functions.c:354
bool sb_prev(struct SidebarWindowData *wdata)
Find the previous unhidden Mailbox.
Definition: functions.c:85
static struct SbEntry ** sb_prev_new(struct SidebarWindowData *wdata, size_t begin, size_t end)
Return the previous mailbox with new messages.
Definition: functions.c:111
bool sb_next(struct SidebarWindowData *wdata)
Find the next unhidden Mailbox.
Definition: functions.c:46
static struct SbEntry ** sb_next_new(struct SidebarWindowData *wdata, size_t begin, size_t end)
Return the next mailbox with new messages.
Definition: functions.c:69
Sidebar functions.
GUI display the mailboxes in a side panel.
struct Mailbox * sb_get_highlight(struct MuttWindow *win)
Get the Mailbox that's highlighted in the sidebar.
Definition: sidebar.c:63
Key value store.
#define NONULL(x)
Definition: string2.h:37
void * wdata
Private data.
Definition: mutt_window.h:145
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
Info about folders in the sidebar.
Definition: private.h:41
A NeoMutt function.
Definition: functions.h:44
int op
Op code, e.g. OP_SIDEBAR_NEXT.
Definition: functions.h:45
sidebar_function_t function
Function to call.
Definition: functions.h:46
Sidebar private Window data -.
Definition: private.h:64
int hil_index
Highlighted mailbox.
Definition: private.h:71
struct MuttWindow * win
Sidebar Window.
Definition: private.h:65
struct SbEntryArray entries
Items to display in the sidebar.
Definition: private.h:67