NeoMutt  2025-12-11-872-g385a04
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sidebar.c
Go to the documentation of this file.
1
25
31
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 "color/lib.h"
42#include "index/lib.h"
43#include "module_data.h"
44
48const struct Command SbCommands[] = {
49 // clang-format off
50 { "sidebar-pin", CMD_SIDEBAR_PIN, parse_sidebar_pin,
51 N_("Pin a mailbox in the sidebar (keep visible)"),
52 N_("sidebar-pin <mailbox> [ <mailbox> ... ]"),
53 "optionalfeatures.html#sidebar-pin" },
54 { "sidebar-unpin", CMD_SIDEBAR_UNPIN, parse_sidebar_unpin,
55 N_("Unpin a previously pinned mailbox in the sidebar"),
56 N_("sidebar-unpin { * | <mailbox> ... }"),
57 "optionalfeatures.html#sidebar-pin" },
58
59 // Deprecated
60 { "sidebar_pin", CMD_NONE, NULL, "sidebar-pin", NULL, NULL, CF_SYNONYM },
61 { "sidebar_unpin", CMD_NONE, NULL, "sidebar-unpin", NULL, NULL, CF_SYNONYM },
62 { "sidebar_whitelist", CMD_NONE, NULL, "sidebar-pin", NULL, NULL, CF_SYNONYM },
63 { "unsidebar_whitelist", CMD_NONE, NULL, "sidebar-unpin", NULL, NULL, CF_SYNONYM },
64
65 { NULL, CMD_NONE, NULL, NULL, NULL, NULL, CF_NONE },
66 // clang-format on
67};
68
75{
76 const bool c_sidebar_visible = cs_subset_bool(NeoMutt->sub, "sidebar_visible");
77 if (!c_sidebar_visible)
78 return NULL;
79
81 if (!wdata || (wdata->hil_index < 0))
82 return NULL;
83
84 struct SbEntry **sbep = ARRAY_GET(&wdata->entries, wdata->hil_index);
85 if (!sbep)
86 return NULL;
87
88 return (*sbep)->mailbox;
89}
90
99void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
100{
101 if (!m)
102 return;
103
104 struct SbEntry **sbep = NULL;
105 ARRAY_FOREACH(sbep, &wdata->entries)
106 {
107 if ((*sbep)->mailbox == m)
108 return;
109 }
110
111 /* Any new/deleted mailboxes will cause a refresh. As long as
112 * they're valid, our pointers will be updated in prepare_sidebar() */
113
114 struct IndexSharedData *shared = wdata->shared;
115 struct SbEntry *entry = MUTT_MEM_CALLOC(1, struct SbEntry);
116 entry->mailbox = m;
117 entry->score = -1;
118
119 if (wdata->top_index < 0)
120 wdata->top_index = ARRAY_SIZE(&wdata->entries);
121 if (wdata->bot_index < 0)
122 wdata->bot_index = ARRAY_SIZE(&wdata->entries);
123 if ((wdata->opn_index < 0) && shared->mailbox &&
125 {
126 wdata->opn_index = ARRAY_SIZE(&wdata->entries);
127 }
128
129 ARRAY_ADD(&wdata->entries, entry);
130}
131
137void sb_remove_mailbox(struct SidebarWindowData *wdata, const struct Mailbox *m)
138{
139 struct SbEntry **sbep = NULL;
140 ARRAY_FOREACH(sbep, &wdata->entries)
141 {
142 if ((*sbep)->mailbox != m)
143 continue;
144
145 struct SbEntry *sbe_remove = *sbep;
146 ARRAY_REMOVE(&wdata->entries, sbep);
147 FREE(&sbe_remove);
148
149 if (wdata->opn_index == ARRAY_FOREACH_IDX_sbep)
150 {
151 // Open item was deleted
152 wdata->opn_index = -1;
153 }
154 else if ((wdata->opn_index > 0) && (wdata->opn_index > ARRAY_FOREACH_IDX_sbep))
155 {
156 // Open item is still visible, so adjust the index
157 wdata->opn_index--;
158 }
159
160 if (wdata->hil_index == ARRAY_FOREACH_IDX_sbep)
161 {
162 // If possible, keep the highlight where it is
163 struct SbEntry **sbep_cur = ARRAY_GET(&wdata->entries, ARRAY_FOREACH_IDX_sbep);
164 if (!sbep_cur)
165 {
166 // The last entry was deleted, so backtrack
167 sb_prev(wdata);
168 }
169 else if ((*sbep_cur)->is_hidden)
170 {
171 // Find the next unhidden entry, or the previous
172 if (!sb_next(wdata) && !sb_prev(wdata))
173 wdata->hil_index = -1;
174 }
175 }
176 else if ((wdata->hil_index > 0) && (wdata->hil_index > ARRAY_FOREACH_IDX_sbep))
177 {
178 // Highlighted item is still visible, so adjust the index
179 wdata->hil_index--;
180 }
181 break;
182 }
183}
184
191{
192 wdata->opn_index = -1;
193
194 struct SbEntry **sbep = NULL;
195 ARRAY_FOREACH(sbep, &wdata->entries)
196 {
197 if (m && m->visible)
198 {
199 if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
200 {
201 wdata->opn_index = ARRAY_FOREACH_IDX_sbep;
202 wdata->hil_index = ARRAY_FOREACH_IDX_sbep;
203 break;
204 }
205 }
206 (*sbep)->is_hidden = !(*sbep)->mailbox->visible;
207 }
208}
209
214void sb_init(struct MuttWindow *all_dialogs_window)
215{
216 // Set a default style, if unset
218 if (!attr_color_is_set(ac))
219 ac->attrs = A_UNDERLINE;
220
221 if (all_dialogs_window)
222 {
223 // Listen for dialog creation events
224 notify_observer_add(all_dialogs_window->notify, NT_WINDOW,
226 }
227}
228
234void sb_cleanup(struct ListHead *sidebar_pinned, struct MuttWindow *all_dialogs_window)
235{
236 if (all_dialogs_window)
238 mutt_list_free(sidebar_pinned);
239}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
#define ARRAY_REMOVE(head, elem)
Remove an entry from the array, shifting down the subsequent entries.
Definition array.h:355
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_GET(head, idx)
Return the element at index.
Definition array.h:109
bool attr_color_is_set(const struct AttrColor *ac)
Is the object coloured?
Definition attr.c:178
Color and attribute parsing.
struct AttrColor * simple_color_get(enum ColorId cid)
Get the colour of an object by its ID.
Definition simple.c:98
@ MT_COLOR_SIDEBAR_HIGHLIGHT
Select cursor.
Definition color.h:71
@ CF_SYNONYM
Command is a synonym for another command.
Definition command.h:50
@ CF_NONE
No flags are set.
Definition command.h:49
@ CMD_SIDEBAR_PIN
:sidebar-pin
Definition command.h:114
@ CMD_SIDEBAR_UNPIN
:sidebar-unpin
Definition command.h:115
@ CMD_NONE
No Command.
Definition command.h:62
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.
enum CommandResult parse_sidebar_pin(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'sidebar-pin' command - Implements Command::parse() -.
Definition commands.c:45
enum CommandResult parse_sidebar_unpin(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'sidebar-unpin' command - Implements Command::parse() -.
Definition commands.c:76
int sb_insertion_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition observer.c:485
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:123
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
Convenience wrapper for the library headers.
#define N_(a)
Definition message.h:32
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition notify.c:230
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:191
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition notify_type.h:58
bool sb_prev(struct SidebarWindowData *wdata)
Find the previous unhidden Mailbox.
Definition functions.c:194
bool sb_next(struct SidebarWindowData *wdata)
Find the next unhidden Mailbox.
Definition functions.c:119
GUI display the mailboxes in a side panel.
const struct Command SbCommands[]
Sidebar Commands.
Definition sidebar.c:48
Sidebar private Module data.
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:77
void sb_remove_mailbox(struct SidebarWindowData *wdata, const struct Mailbox *m)
Remove a Mailbox from the Sidebar.
Definition sidebar.c:137
void sb_init(struct MuttWindow *all_dialogs_window)
Set up the Sidebar.
Definition sidebar.c:214
void sb_set_current_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Set the current Mailbox.
Definition sidebar.c:190
void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Add a Mailbox to the Sidebar.
Definition sidebar.c:99
struct Mailbox * sb_get_highlight(struct MuttWindow *win)
Get the Mailbox that's highlighted in the sidebar.
Definition sidebar.c:74
void sb_cleanup(struct ListHead *sidebar_pinned, struct MuttWindow *all_dialogs_window)
Clean up the Sidebar.
Definition sidebar.c:234
A curses colour and its attributes.
Definition attr.h:65
int attrs
Text attributes, e.g. A_BOLD.
Definition attr.h:68
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:81
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition mailbox.h:83
bool visible
True if a result of "mailboxes".
Definition mailbox.h:132
void * wdata
Private data.
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Info about folders in the sidebar.
Definition private.h:40
int score
Fuzzy-match score.
Definition private.h:47
struct Mailbox * mailbox
Mailbox this represents.
Definition private.h:44
bool is_hidden
Don't show, e.g. $sidebar_new_mail_only.
Definition private.h:45
Sidebar private Window data -.
Definition private.h:89
int top_index
First mailbox visible in sidebar.
Definition private.h:94
int bot_index
Last mailbox visible in sidebar.
Definition private.h:97
int hil_index
Highlighted mailbox.
Definition private.h:96
struct IndexSharedData * shared
Shared Index Data.
Definition private.h:91
int opn_index
Current (open) mailbox.
Definition private.h:95
struct MuttWindow * win
Sidebar Window.
Definition private.h:90
struct SbEntryArray entries
Items to display in the sidebar.
Definition private.h:92