NeoMutt  2022-04-29-323-g5fcc6c
Teaching an old dog new tricks
DOXYGEN
cbar.c
Go to the documentation of this file.
1
61#include "config.h"
62#include <stdbool.h>
63#include <stdint.h>
64#include <stdio.h>
65#include "private.h"
66#include "mutt/lib.h"
67#include "config/lib.h"
68#include "email/lib.h"
69#include "core/lib.h"
70#include "gui/lib.h"
71#include "color/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
103static 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
160static int cbar_recalc(struct MuttWindow *win)
161{
162 char buf[1024] = { 0 };
163 struct ComposeSharedData *shared = win->parent->wdata;
164
165 const char *const c_compose_format = cs_subset_string(shared->sub, "compose_format");
166 mutt_expando_format(buf, sizeof(buf), 0, win->state.cols, NONULL(c_compose_format),
167 compose_format_str, (intptr_t) shared, MUTT_FORMAT_NO_FLAGS);
168
169 struct ComposeBarData *cbar_data = win->wdata;
170 if (!mutt_str_equal(buf, cbar_data->compose_format))
171 {
172 mutt_str_replace(&cbar_data->compose_format, buf);
173 win->actions |= WA_REPAINT;
174 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
175 }
176
177 return 0;
178}
179
183static int cbar_repaint(struct MuttWindow *win)
184{
185 struct ComposeBarData *cbar_data = win->wdata;
186
187 mutt_window_move(win, 0, 0);
190
191 mutt_window_move(win, 0, 0);
192 mutt_draw_statusline(win, win->state.cols, cbar_data->compose_format,
193 mutt_str_len(cbar_data->compose_format));
195 mutt_debug(LL_DEBUG5, "repaint done\n");
196
197 return 0;
198}
199
204{
205 if (nc->event_type != NT_COLOR)
206 return 0;
207 if (!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->cid != MT_COLOR_STATUS) && (ev_c->cid != MT_COLOR_NORMAL) &&
214 (ev_c->cid != MT_COLOR_MAX))
215 {
216 return 0;
217 }
218
219 struct MuttWindow *win_cbar = nc->global_data;
220 win_cbar->actions |= WA_REPAINT;
221 mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
222
223 return 0;
224}
225
230{
231 if (nc->event_type != NT_CONFIG)
232 return 0;
233 if (!nc->global_data || !nc->event_data)
234 return -1;
235
236 struct EventConfig *ev_c = nc->event_data;
237 if (!mutt_str_equal(ev_c->name, "compose_format"))
238 return 0;
239
240 struct MuttWindow *win_cbar = nc->global_data;
241 win_cbar->actions |= WA_RECALC;
242 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC\n");
243
244 return 0;
245}
246
251{
252 if (nc->event_type != NT_EMAIL)
253 return 0;
254 if (!nc->global_data || !nc->event_data)
255 return -1;
256
257 struct MuttWindow *win_cbar = nc->global_data;
258 win_cbar->actions |= WA_RECALC;
259 mutt_debug(LL_DEBUG5, "compose done, request WA_RECALC\n");
260
261 return 0;
262}
263
268{
269 if (nc->event_type != NT_WINDOW)
270 return 0;
271 if (!nc->global_data || !nc->event_data)
272 return -1;
273
274 struct MuttWindow *win_cbar = nc->global_data;
275 struct EventWindow *ev_w = nc->event_data;
276 if (ev_w->win != win_cbar)
277 return 0;
278
280 {
281 win_cbar->actions |= WA_RECALC;
282 mutt_debug(LL_DEBUG5, "window state done, request WA_RECALC\n");
283 }
284 else if (nc->event_subtype == NT_WINDOW_DELETE)
285 {
286 struct MuttWindow *dlg = win_cbar->parent;
287 struct ComposeSharedData *shared = dlg->wdata;
288
293
294 mutt_debug(LL_DEBUG5, "window delete done\n");
295 }
296
297 return 0;
298}
299
304struct MuttWindow *cbar_new(struct ComposeSharedData *shared)
305{
309
310 win_cbar->wdata = cbar_data_new();
311 win_cbar->wdata_free = cbar_data_free;
312 win_cbar->recalc = cbar_recalc;
313 win_cbar->repaint = cbar_repaint;
314
319
320 return win_cbar;
321}
Compose Attach Data.
int num_attachments(struct ComposeAttachData *adata)
Count the number of attachments.
Definition: cbar.c:86
struct MuttWindow * cbar_new(struct ComposeSharedData *shared)
Create the Compose Bar (status)
Definition: cbar.c:304
struct ComposeBarData * cbar_data_new(void)
Free the private data attached to the MuttWindow.
Definition: cbar_data.c:55
Compose Bar Data.
Color and attribute parsing.
@ MT_COLOR_MAX
Definition: color.h:91
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:74
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:57
unsigned long cum_attachs_size(struct ConfigSubset *sub, struct ComposeAttachData *adata)
Cumulative Attachments Size.
Definition: attach.c:85
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
void mutt_draw_statusline(struct MuttWindow *win, int cols, const char *buf, size_t buflen)
Draw a highlighted status bar.
Definition: dlg_index.c:922
Structs that make up an email.
Flags to control mutt_expando_format()
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: format_flags.h:30
#define MUTT_FORMAT_OPTIONAL
Allow optional field processing.
Definition: format_flags.h:33
uint8_t MuttFormatFlags
Flags for mutt_expando_format(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: format_flags.h:29
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 -.
Definition: cbar.c:103
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:777
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
int cbar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t -.
Definition: cbar.c:203
int cbar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: cbar.c:229
int cbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: cbar.c:267
int cbar_email_observer(struct NotifyCallback *nc)
Notification that the Email has changed - Implements observer_t -.
Definition: cbar.c:250
static int cbar_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition: cbar.c:160
static int cbar_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition: cbar.c:183
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
Convenience wrapper for the gui headers.
GUI manage the main index (list of emails)
Data shared between Index, Pager and Sidebar.
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
GUI present the user with a selectable list.
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
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:567
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
struct AttrColor * mutt_curses_set_normal_backed_color_by_id(enum ColorId cid)
Set the colour and attributes by the colour id.
Definition: mutt_curses.c:65
struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the colour id.
Definition: mutt_curses.c:81
Hundreds of global variables to back the user variables.
char * ShortHostname
Short version of the hostname.
Definition: mutt_globals.h:50
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:181
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:293
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:242
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition: mutt_window.h:38
@ NT_WINDOW_STATE
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:207
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:206
#define WA_REPAINT
Redraw the contents of the Window.
Definition: mutt_window.h:111
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:52
@ MUTT_WIN_SIZE_FIXED
Window has a fixed size.
Definition: mutt_window.h:47
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:1470
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: muttlib.c:1673
Some miscellaneous functions.
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:55
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:41
@ NT_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:44
GUI display the mailboxes in a side panel.
#define NONULL(x)
Definition: string2.h:37
Data to fill the Compose Attach Window.
Definition: attach_data.h:33
struct Menu * menu
Menu displaying the attachments.
Definition: attach_data.h:35
Data to fill the Compose Bar Window.
Definition: cbar_data.h:34
char * compose_format
Cached status string.
Definition: cbar_data.h:35
Shared Compose Data.
Definition: shared_data.h:33
struct ConfigSubset * sub
Config set to use.
Definition: shared_data.h:34
int flags
Flags, e.g. MUTT_COMPOSE_NOFREEHEADER.
Definition: shared_data.h:41
struct ComposeAttachData * adata
Attachments.
Definition: shared_data.h:37
struct Email * email
Email being composed.
Definition: shared_data.h:36
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:71
An Event that happened to a Colour.
Definition: notify2.h:53
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:54
A config-change event.
Definition: subset.h:70
const char * name
Name of config item that changed.
Definition: subset.h:72
An Event that happened to a Window.
Definition: mutt_window.h:216
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
int max
Number of entries in the menu.
Definition: lib.h:71
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:182
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:171
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:160
struct MuttWindow * parent
Parent Window.
Definition: mutt_window.h:135
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
Data passed to a notification function.
Definition: observer.h:34
void * event_data
Data from notify_send()
Definition: observer.h:38
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:36
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:37
void * global_data
Data from notify_observer_add()
Definition: observer.h:39
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60