NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
cbar.c
Go to the documentation of this file.
1 
63 #include "config.h"
64 #include <stdbool.h>
65 #include <stdint.h>
66 #include <stdio.h>
67 #include "private.h"
68 #include "mutt/lib.h"
69 #include "config/lib.h"
70 #include "core/lib.h"
71 #include "gui/lib.h"
72 #include "index/lib.h"
73 #include "menu/lib.h"
74 #include "attach_data.h"
75 #include "cbar_data.h"
76 #include "format_flags.h"
77 #include "mutt_globals.h"
78 #include "muttlib.h"
79 #include "shared_data.h"
80 
87 {
88  if (!adata || !adata->menu)
89  return 0;
90  return adata->menu->max;
91 }
92 
103 static const char *compose_format_str(char *buf, size_t buflen, size_t col, int cols,
104  char op, const char *src, const char *prec,
105  const char *if_str, const char *else_str,
106  intptr_t data, MuttFormatFlags flags)
107 {
108  char fmt[128], tmp[128];
109  bool optional = (flags & MUTT_FORMAT_OPTIONAL);
110  struct ComposeSharedData *shared = (struct ComposeSharedData *) data;
111 
112  *buf = '\0';
113  switch (op)
114  {
115  case 'a': /* total number of attachments */
116  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
117  snprintf(buf, buflen, fmt, num_attachments(shared->adata));
118  break;
119 
120  case 'h': /* hostname */
121  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
122  snprintf(buf, buflen, fmt, NONULL(ShortHostname));
123  break;
124 
125  case 'l': /* approx length of current message in bytes */
126  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
127  mutt_str_pretty_size(tmp, sizeof(tmp),
128  cum_attachs_size(shared->sub, shared->adata));
129  snprintf(buf, buflen, fmt, tmp);
130  break;
131 
132  case 'v':
133  snprintf(buf, buflen, "%s", mutt_make_version());
134  break;
135 
136  case 0:
137  *buf = '\0';
138  return src;
139 
140  default:
141  snprintf(buf, buflen, "%%%s%c", prec, op);
142  break;
143  }
144 
145  if (optional)
146  {
147  mutt_expando_format(buf, buflen, col, cols, if_str, compose_format_str, data, flags);
148  }
149  // This format function doesn't have any optional expandos,
150  // so there's no `else if (flags & MUTT_FORMAT_OPTIONAL)` clause
151 
152  return src;
153 }
154 
158 static int cbar_recalc(struct MuttWindow *win)
159 {
160  char buf[1024] = { 0 };
161  struct ComposeSharedData *shared = win->parent->wdata;
162 
163  const char *const c_compose_format =
164  cs_subset_string(shared->sub, "compose_format");
165  mutt_expando_format(buf, sizeof(buf), 0, win->state.cols, NONULL(c_compose_format),
166  compose_format_str, (intptr_t) shared, MUTT_FORMAT_NO_FLAGS);
167 
168  struct ComposeBarData *cbar_data = win->wdata;
169  if (!mutt_str_equal(buf, cbar_data->compose_format))
170  {
171  mutt_str_replace(&cbar_data->compose_format, buf);
172  win->actions |= WA_REPAINT;
173  mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
174  }
175 
176  return 0;
177 }
178 
182 static int cbar_repaint(struct MuttWindow *win)
183 {
184  if (!mutt_window_is_visible(win))
185  return 0;
186 
187  struct ComposeBarData *cbar_data = win->wdata;
188 
189  mutt_window_move(win, 0, 0);
192 
193  mutt_window_move(win, 0, 0);
194  mutt_draw_statusline(win, win->state.cols, cbar_data->compose_format,
195  mutt_str_len(cbar_data->compose_format));
197  mutt_debug(LL_DEBUG5, "repaint done\n");
198 
199  return 0;
200 }
201 
206 {
207  if ((nc->event_type != NT_COLOR) || !nc->global_data || !nc->event_data)
208  return -1;
209 
210  struct EventColor *ev_c = nc->event_data;
211 
212  // MT_COLOR_MAX is sent on `uncolor *`
213  if ((ev_c->color != MT_COLOR_STATUS) && (ev_c->color != MT_COLOR_MAX))
214  return 0;
215 
216  struct MuttWindow *win_cbar = nc->global_data;
217  win_cbar->actions |= WA_REPAINT;
218  mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
219 
220  return 0;
221 }
222 
227 {
228  if ((nc->event_type != NT_COMPOSE) || !nc->global_data || !nc->event_data)
229  return -1;
230 
231  struct MuttWindow *win_cbar = nc->global_data;
232  win_cbar->actions |= WA_REPAINT;
233  mutt_debug(LL_DEBUG5, "compose done, request WA_REPAINT\n");
234 
235  return 0;
236 }
237 
242 {
243  if ((nc->event_type != NT_CONFIG) || !nc->global_data || !nc->event_data)
244  return -1;
245 
246  struct EventConfig *ev_c = nc->event_data;
247  if (!mutt_str_equal(ev_c->name, "compose_format"))
248  return 0;
249 
250  struct MuttWindow *win_cbar = nc->global_data;
251  win_cbar->actions |= WA_RECALC;
252  mutt_debug(LL_DEBUG5, "config done, request WA_RECALC\n");
253 
254  return 0;
255 }
256 
261 {
262  if ((nc->event_type != NT_WINDOW) || !nc->global_data || !nc->event_data)
263  return -1;
264 
265  struct MuttWindow *win_cbar = nc->global_data;
266  struct EventWindow *ev_w = nc->event_data;
267  if (ev_w->win != win_cbar)
268  return 0;
269 
270  if (nc->event_subtype == NT_WINDOW_STATE)
271  {
272  win_cbar->actions |= WA_RECALC;
273  mutt_debug(LL_DEBUG5, "window state done, request WA_RECALC\n");
274  }
275  else if (nc->event_subtype == NT_WINDOW_DELETE)
276  {
277  struct MuttWindow *dlg = win_cbar->parent;
278  struct ComposeSharedData *shared = dlg->wdata;
279 
283  notify_observer_remove(win_cbar->notify, cbar_window_observer, win_cbar);
284 
285  mutt_debug(LL_DEBUG5, "window delete done\n");
286  }
287 
288  return 0;
289 }
290 
296 struct MuttWindow *cbar_new(struct MuttWindow *parent, struct ComposeSharedData *shared)
297 {
298  struct MuttWindow *win_cbar =
301 
302  win_cbar->wdata = cbar_data_new();
303  win_cbar->wdata_free = cbar_data_free;
304  win_cbar->recalc = cbar_recalc;
305  win_cbar->repaint = cbar_repaint;
306 
311 
312  return win_cbar;
313 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
Convenience wrapper for the gui headers.
int cbar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t.
Definition: cbar.c:205
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string -.
Definition: muttlib.c:780
uint8_t MuttFormatFlags
Flags for mutt_expando_format(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: format_flags.h:29
#define NONULL(x)
Definition: string2.h:37
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:160
#define WA_REPAINT
Redraw the contents of the Window.
Definition: mutt_window.h:111
Data passed to a notification function.
Definition: observer.h:39
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
An Event that happened to a Colour.
Definition: color.h:122
An Event that happened to a Window.
Definition: mutt_window.h:215
Data to fill the Compose Attach Window.
Definition: attach_data.h:32
Compose Bar Data.
Compose Shared Data.
Window uses all available vertical space.
Definition: mutt_window.h:38
int cbar_compose_observer(struct NotifyCallback *nc)
Notification that the Compose data has changed - Implements observer_t.
Definition: cbar.c:226
struct ComposeBarData * cbar_data_new(void)
Free the private data attached to the MuttWindow.
Definition: cbar_data.c:55
void mutt_curses_set_color(enum ColorId color)
Set the current colour for text.
Definition: mutt_curses.c:56
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:259
A config-change event.
Definition: subset.h:69
Shared Compose Data.
Definition: shared_data.h:29
int num_attachments(struct ComposeAttachData *adata)
Count the number of attachments.
Definition: cbar.c:86
static const char * compose_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Create the status bar string for compose mode - Implements format_t -Expando Description %a Total nu...
Definition: cbar.c:103
Window is about to be deleted.
Definition: mutt_window.h:206
Flags to control mutt_expando_format()
Container for Accounts, Notifications.
Definition: neomutt.h:36
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: format_flags.h:30
static int cbar_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition: cbar.c:158
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
Convenience wrapper for the config headers.
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:310
static int cbar_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition: cbar.c:182
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
Some miscellaneous functions.
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Convenience wrapper for the core headers.
unsigned long cum_attachs_size(struct ConfigSubset *sub, struct ComposeAttachData *adata)
Cumulative Attachments Size.
Definition: attach.c:89
Compose Attach Data.
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
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
Window has a fixed size.
Definition: mutt_window.h:47
Plain text.
Definition: color.h:58
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:207
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:39
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
bool mutt_window_is_visible(struct MuttWindow *win)
Is the Window visible?
Definition: mutt_window.c:529
Status bar (takes a pattern)
Definition: color.h:75
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:52
WHERE char * ShortHostname
Short version of the hostname.
Definition: mutt_globals.h:46
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: muttlib.c:1679
void mutt_draw_statusline(struct MuttWindow *win, int cols, const char *buf, size_t buflen)
Draw a highlighted status bar.
Definition: dlg_index.c:875
int cbar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t.
Definition: cbar.c:241
enum ColorId color
Definition: color.h:124
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
Compose data has changed, NotifyCompose.
Definition: notify_type.h:41
Compose Private Data.
Data to fill the Compose Bar Window.
Definition: cbar_data.h:33
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
struct MuttWindow * cbar_new(struct MuttWindow *parent, struct ComposeSharedData *shared)
Create the Compose Bar (status)
Definition: cbar.c:296
void * event_data
Data from notify_send()
Definition: observer.h:44
int max
Number of entries in the menu.
Definition: lib.h:71
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:446
struct Menu * menu
Definition: attach_data.h:35
struct Notify * notify
Notifications: NotifyCompose.
Definition: shared_data.h:36
int cbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t.
Definition: cbar.c:260
struct MuttWindow * parent
Parent Window.
Definition: mutt_window.h:135
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:182
#define MUTT_FORMAT_OPTIONAL
Allow optional field processing.
Definition: format_flags.h:33
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:171
Hundreds of global variables to back the user variables.
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
Definition: mutt_window.c:180
char * compose_format
Cached status string.
Definition: cbar_data.h:35
Log at debug level 5.
Definition: logging.h:44
Convenience wrapper for the library headers.
GUI manage the main index (list of emails)
void * wdata
Private data.
Definition: mutt_window.h:145
const char * name
Name of config item that changed.
Definition: subset.h:72
struct ConfigSubset * sub
Config set to use.
Definition: shared_data.h:31
void cbar_data_free(struct MuttWindow *win, void **ptr)
Free the private data attached to the MuttWindow - Implements MuttWindow::wdata_free() -...
Definition: cbar_data.c:40
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:1476
struct ComposeAttachData * adata
Attachments.
Definition: shared_data.h:34
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110