NeoMutt  2022-04-29-247-gc6aae8
Teaching an old dog new tricks
DOXYGEN
sidebar.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <stdbool.h>
34#include <stdio.h>
35#include "private.h"
36#include "mutt/lib.h"
37#include "config/lib.h"
38#include "core/lib.h"
39#include "gui/lib.h"
40#include "lib.h"
41#include "index/lib.h"
42#include "mutt_commands.h"
43
45
46static const struct Command sb_commands[] = {
47 // clang-format off
48 { "sidebar_whitelist", sb_parse_whitelist, 0 },
49 { "unsidebar_whitelist", sb_parse_unwhitelist, 0 },
50 // clang-format on
51};
52
59{
60 const bool c_sidebar_visible = cs_subset_bool(NeoMutt->sub, "sidebar_visible");
61 if (!c_sidebar_visible)
62 return NULL;
63
65 if (wdata->hil_index < 0)
66 return NULL;
67
68 struct SbEntry **sbep = ARRAY_GET(&wdata->entries, wdata->hil_index);
69 if (!sbep)
70 return NULL;
71
72 return (*sbep)->mailbox;
73}
74
83void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
84{
85 if (!m)
86 return;
87
88 struct SbEntry **sbep = NULL;
89 ARRAY_FOREACH(sbep, &wdata->entries)
90 {
91 if ((*sbep)->mailbox == m)
92 return;
93 }
94
95 /* Any new/deleted mailboxes will cause a refresh. As long as
96 * they're valid, our pointers will be updated in prepare_sidebar() */
97
98 struct IndexSharedData *shared = wdata->shared;
99 struct SbEntry *entry = mutt_mem_calloc(1, sizeof(struct SbEntry));
100 entry->mailbox = m;
101
102 if (wdata->top_index < 0)
103 wdata->top_index = ARRAY_SIZE(&wdata->entries);
104 if (wdata->bot_index < 0)
105 wdata->bot_index = ARRAY_SIZE(&wdata->entries);
106 if ((wdata->opn_index < 0) && shared->mailbox &&
108 {
109 wdata->opn_index = ARRAY_SIZE(&wdata->entries);
110 }
111
112 ARRAY_ADD(&wdata->entries, entry);
113}
114
120void sb_remove_mailbox(struct SidebarWindowData *wdata, const struct Mailbox *m)
121{
122 struct SbEntry **sbep = NULL;
123 ARRAY_FOREACH(sbep, &wdata->entries)
124 {
125 if ((*sbep)->mailbox != m)
126 continue;
127
128 struct SbEntry *sbe_remove = *sbep;
129 ARRAY_REMOVE(&wdata->entries, sbep);
130 FREE(&sbe_remove);
131
132 if (wdata->opn_index == ARRAY_FOREACH_IDX)
133 {
134 // Open item was deleted
135 wdata->opn_index = -1;
136 }
137 else if ((wdata->opn_index > 0) && (wdata->opn_index > ARRAY_FOREACH_IDX))
138 {
139 // Open item is still visible, so adjust the index
140 wdata->opn_index--;
141 }
142
143 if (wdata->hil_index == ARRAY_FOREACH_IDX)
144 {
145 // If possible, keep the highlight where it is
146 struct SbEntry **sbep_cur = ARRAY_GET(&wdata->entries, ARRAY_FOREACH_IDX);
147 if (!sbep_cur)
148 {
149 // The last entry was deleted, so backtrack
150 sb_prev(wdata);
151 }
152 else if ((*sbep)->is_hidden)
153 {
154 // Find the next unhidden entry, or the previous
155 if (!sb_next(wdata) && !sb_prev(wdata))
156 wdata->hil_index = -1;
157 }
158 }
159 else if ((wdata->hil_index > 0) && (wdata->hil_index > ARRAY_FOREACH_IDX))
160 {
161 // Highlighted item is still visible, so adjust the index
162 wdata->hil_index--;
163 }
164 break;
165 }
166}
167
174{
175 wdata->opn_index = -1;
176
177 struct SbEntry **sbep = NULL;
178 ARRAY_FOREACH(sbep, &wdata->entries)
179 {
180 if (m && m->visible)
181 {
182 if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
183 {
184 wdata->opn_index = ARRAY_FOREACH_IDX;
185 wdata->hil_index = ARRAY_FOREACH_IDX;
186 break;
187 }
188 }
189 (*sbep)->is_hidden = !(*sbep)->mailbox->visible;
190 }
191}
192
196void sb_init(void)
197{
199
200 // Listen for dialog creation events
203}
204
208void sb_shutdown(void)
209{
213}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:155
#define ARRAY_REMOVE(head, elem)
Remove an entry from the array, shifting down the subsequent entries.
Definition: array.h:266
#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_GET(head, idx)
Return the element at index.
Definition: array.h:108
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
struct MuttWindow * AllDialogsWindow
Parent of all Dialogs.
Definition: dialog.c:74
enum CommandResult sb_parse_whitelist(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'sidebar_whitelist' command - Implements Command::parse() -.
Definition: commands.c:41
enum CommandResult sb_parse_unwhitelist(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'unsidebar_whitelist' command - Implements Command::parse() -.
Definition: commands.c:60
int sb_insertion_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: observer.c:473
Convenience wrapper for the gui headers.
GUI manage the main index (list of emails)
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition: notify.c:228
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:189
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
Definitions of NeoMutt commands.
#define COMMANDS_REGISTER(cmds)
Definition: mutt_commands.h:47
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:55
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
bool sb_prev(struct SidebarWindowData *wdata)
Find the previous unhidden Mailbox.
Definition: functions.c:86
bool sb_next(struct SidebarWindowData *wdata)
Find the next unhidden Mailbox.
Definition: functions.c:47
GUI display the mailboxes in a side panel.
struct SidebarWindowData * sb_wdata_get(struct MuttWindow *win)
Get the Sidebar data for this window.
Definition: wdata.c:73
void sb_init(void)
Set up the Sidebar.
Definition: sidebar.c:196
void sb_remove_mailbox(struct SidebarWindowData *wdata, const struct Mailbox *m)
Remove a Mailbox from the Sidebar.
Definition: sidebar.c:120
static const struct Command sb_commands[]
Definition: sidebar.c:46
void sb_set_current_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Set the current Mailbox.
Definition: sidebar.c:173
struct ListHead SidebarWhitelist
List of mailboxes to always display in the sidebar.
Definition: sidebar.c:44
void sb_shutdown(void)
Clean up the Sidebar.
Definition: sidebar.c:208
void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Add a Mailbox to the Sidebar.
Definition: sidebar.c:83
struct Mailbox * sb_get_highlight(struct MuttWindow *win)
Get the Mailbox that's highlighted in the sidebar.
Definition: sidebar.c:58
Key value store.
Data shared between Index, Pager and Sidebar.
Definition: shared_data.h:37
struct Mailbox * mailbox
Current Mailbox.
Definition: shared_data.h:41
A mailbox.
Definition: mailbox.h:79
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:81
bool visible
True if a result of "mailboxes".
Definition: mailbox.h:130
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Info about folders in the sidebar.
Definition: private.h:41
struct Mailbox * mailbox
Mailbox this represents.
Definition: private.h:45
Sidebar private Window data -.
Definition: private.h:65
int top_index
First mailbox visible in sidebar.
Definition: private.h:70
int bot_index
Last mailbox visible in sidebar.
Definition: private.h:73
int hil_index
Highlighted mailbox.
Definition: private.h:72
struct IndexSharedData * shared
Shared Index Data.
Definition: private.h:67
int opn_index
Current (open) mailbox.
Definition: private.h:71
struct MuttWindow * win
Sidebar Window.
Definition: private.h:66
struct SbEntryArray entries
Items to display in the sidebar.
Definition: private.h:68