NeoMutt  2021-02-05-89-gabe350
Teaching an old dog new tricks
DOXYGEN
sidebar.c
Go to the documentation of this file.
1 
32 #include "config.h"
33 #include <stdio.h>
34 #include "private.h"
35 #include "mutt/lib.h"
36 #include "core/lib.h"
37 #include "gui/lib.h"
38 #include "lib.h"
39 #include "context.h"
40 #include "mutt_globals.h"
41 
43 
44 static const struct Command sb_commands[] = {
45  // clang-format off
46  { "sidebar_whitelist", sb_parse_whitelist, 0 },
47  { "unsidebar_whitelist", sb_parse_unwhitelist, 0 },
48  // clang-format on
49 };
50 
56 struct Mailbox *sb_get_highlight(struct MuttWindow *win)
57 {
58  const bool c_sidebar_visible =
59  cs_subset_bool(NeoMutt->sub, "sidebar_visible");
60  if (!c_sidebar_visible)
61  return NULL;
62 
63  struct SidebarWindowData *wdata = sb_wdata_get(win);
64  if (wdata->hil_index < 0)
65  return NULL;
66 
67  struct SbEntry **sbep = ARRAY_GET(&wdata->entries, wdata->hil_index);
68  if (!sbep)
69  return NULL;
70 
71  return (*sbep)->mailbox;
72 }
73 
82 void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
83 {
84  if (!m)
85  return;
86 
87  /* Any new/deleted mailboxes will cause a refresh. As long as
88  * they're valid, our pointers will be updated in prepare_sidebar() */
89 
90  struct SbEntry *entry = mutt_mem_calloc(1, sizeof(struct SbEntry));
91  entry->mailbox = m;
92 
93  if (wdata->top_index < 0)
94  wdata->top_index = ARRAY_SIZE(&wdata->entries);
95  if (wdata->bot_index < 0)
96  wdata->bot_index = ARRAY_SIZE(&wdata->entries);
97  if ((wdata->opn_index < 0) && Context &&
99  {
100  wdata->opn_index = ARRAY_SIZE(&wdata->entries);
101  }
102 
103  ARRAY_ADD(&wdata->entries, entry);
104 }
105 
111 void sb_remove_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
112 {
113  struct SbEntry **sbep = NULL;
114  ARRAY_FOREACH(sbep, &wdata->entries)
115  {
116  if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
117  {
118  struct SbEntry *sbe_remove = *sbep;
119  ARRAY_REMOVE(&wdata->entries, sbep);
120  FREE(&sbe_remove);
121 
122  if (wdata->opn_index == ARRAY_FOREACH_IDX)
123  {
124  // Open item was deleted
125  wdata->opn_index = -1;
126  }
127  else if ((wdata->opn_index > 0) && (wdata->opn_index > ARRAY_FOREACH_IDX))
128  {
129  // Open item is still visible, so adjust the index
130  wdata->opn_index--;
131  }
132 
133  if (wdata->hil_index == ARRAY_FOREACH_IDX)
134  {
135  // If possible, keep the highlight where it is
136  struct SbEntry **sbep_cur = ARRAY_GET(&wdata->entries, ARRAY_FOREACH_IDX);
137  if (!sbep_cur)
138  {
139  // The last entry was deleted, so backtrack
140  select_prev(wdata);
141  }
142  else if ((*sbep)->is_hidden)
143  {
144  // Find the next unhidden entry, or the previous
145  if (!select_next(wdata))
146  if (!select_prev(wdata))
147  wdata->hil_index = -1;
148  }
149  }
150  else if ((wdata->hil_index > 0) && (wdata->hil_index > ARRAY_FOREACH_IDX))
151  {
152  // Highlighted item is still visible, so adjust the index
153  wdata->hil_index--;
154  }
155  break;
156  }
157  }
158 }
159 
165 void sb_set_current_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
166 {
167  wdata->opn_index = -1;
168 
169  struct SbEntry **sbep = NULL;
170  ARRAY_FOREACH(sbep, &wdata->entries)
171  {
172  if (m)
173  {
174  if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
175  {
176  wdata->opn_index = ARRAY_FOREACH_IDX;
177  wdata->hil_index = ARRAY_FOREACH_IDX;
178  break;
179  }
180  }
181  (*sbep)->is_hidden = ((*sbep)->mailbox->flags & MB_HIDDEN);
182  }
183 }
184 
188 void sb_init(void)
189 {
191 
192  // Listen for dialog creation events
194 }
195 
199 void sb_shutdown(void)
200 {
201  if (AllDialogsWindow)
204 }
sb_parse_whitelist
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:42
mutt_mem_calloc
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Mailbox
A mailbox.
Definition: mailbox.h:81
ARRAY_ADD
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:152
sb_add_mailbox
void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Add a Mailbox to the Sidebar.
Definition: sidebar.c:82
MuttWindow
A division of the screen.
Definition: mutt_window.h:115
cs_subset_bool
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:69
sb_wdata_get
struct SidebarWindowData * sb_wdata_get(struct MuttWindow *win)
Get the Sidebar data for this window.
Definition: wdata.c:70
AllDialogsWindow
struct MuttWindow * AllDialogsWindow
Parent of all Dialogs.
Definition: mutt_window.c:46
Context
The "current" mailbox.
Definition: context.h:38
mutt_globals.h
FREE
#define FREE(x)
Definition: memory.h:40
private.h
select_next
bool select_next(struct SidebarWindowData *wdata)
Selects the next unhidden mailbox.
Definition: functions.c:44
SidebarWindowData
Sidebar private Window data -.
Definition: private.h:61
ARRAY_SIZE
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:83
SidebarWhitelist
struct ListHead SidebarWhitelist
List of mailboxes to always display in the sidebar.
Definition: sidebar.c:42
ARRAY_GET
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:105
select_prev
bool select_prev(struct SidebarWindowData *wdata)
Selects the previous unhidden mailbox.
Definition: functions.c:112
NT_WINDOW
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:43
mutt_str_equal
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
SidebarWindowData::top_index
int top_index
First mailbox visible in sidebar.
Definition: private.h:65
MB_HIDDEN
#define MB_HIDDEN
Definition: mailbox.h:38
sb_commands
static const struct Command sb_commands[]
Definition: sidebar.c:44
Command
A user-callable command.
Definition: mutt_commands.h:45
ARRAY_REMOVE
#define ARRAY_REMOVE(head, elem)
Remove an entry from the array, shifting down the subsequent entries.
Definition: array.h:261
MuttWindow::notify
struct Notify * notify
Notifications system.
Definition: mutt_window.h:131
ARRAY_FOREACH
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:206
lib.h
notify_observer_remove
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:207
lib.h
Context::mailbox
struct Mailbox * mailbox
Definition: context.h:50
sb_shutdown
void sb_shutdown(void)
Clean up the Sidebar.
Definition: sidebar.c:199
notify_observer_add
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:169
Mailbox::realpath
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:84
SidebarWindowData::opn_index
int opn_index
Current (open) mailbox.
Definition: private.h:66
sb_parse_unwhitelist
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:61
STAILQ_HEAD_INITIALIZER
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
NeoMutt
Container for Accounts, Notifications.
Definition: neomutt.h:36
sb_remove_mailbox
void sb_remove_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Remove a Mailbox from the Sidebar.
Definition: sidebar.c:111
SidebarWindowData::hil_index
int hil_index
Highlighted mailbox.
Definition: private.h:67
SbEntry::mailbox
struct Mailbox * mailbox
Mailbox this represents.
Definition: private.h:43
sb_set_current_mailbox
void sb_set_current_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Set the current Mailbox.
Definition: sidebar.c:165
context.h
SbEntry
Info about folders in the sidebar.
Definition: private.h:38
NeoMutt::sub
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
sb_get_highlight
struct Mailbox * sb_get_highlight(struct MuttWindow *win)
Get the Mailbox that's highlighted in the sidebar.
Definition: sidebar.c:56
sb_init
void sb_init(void)
Set up the Sidebar.
Definition: sidebar.c:188
SidebarWindowData::bot_index
int bot_index
Last mailbox visible in sidebar.
Definition: private.h:68
lib.h
sb_insertion_observer
int sb_insertion_observer(struct NotifyCallback *nc)
Listen for new Dialogs - Implements observer_t.
Definition: observer.c:442
mutt_list_free
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
COMMANDS_REGISTER
#define COMMANDS_REGISTER(cmds)
Definition: mutt_commands.h:77
MuttWindow::wdata
void * wdata
Private data.
Definition: mutt_window.h:138