NeoMutt  2023-05-17-56-ga67199
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 "cbar.h"
72#include "color/lib.h"
73#include "index/lib.h"
74#include "menu/lib.h"
75#include "attach_data.h"
76#include "cbar_data.h"
77#include "format_flags.h"
78#include "globals.h" // IWYU pragma: keep
79#include "muttlib.h"
80#include "shared_data.h"
81
88{
89 if (!adata || !adata->menu)
90 return 0;
91 return adata->menu->max;
92}
93
104static const char *compose_format_str(char *buf, size_t buflen, size_t col, int cols,
105 char op, const char *src, const char *prec,
106 const char *if_str, const char *else_str,
107 intptr_t data, MuttFormatFlags flags)
108{
109 char fmt[128], tmp[128];
110 bool optional = (flags & MUTT_FORMAT_OPTIONAL);
111 struct ComposeSharedData *shared = (struct ComposeSharedData *) data;
112
113 *buf = '\0';
114 switch (op)
115 {
116 case 'a': /* total number of attachments */
117 snprintf(fmt, sizeof(fmt), "%%%sd", prec);
118 snprintf(buf, buflen, fmt, num_attachments(shared->adata));
119 break;
120
121 case 'h': /* hostname */
122 snprintf(fmt, sizeof(fmt), "%%%ss", prec);
123 snprintf(buf, buflen, fmt, NONULL(ShortHostname));
124 break;
125
126 case 'l': /* approx length of current message in bytes */
127 snprintf(fmt, sizeof(fmt), "%%%ss", prec);
128 mutt_str_pretty_size(tmp, sizeof(tmp),
129 cum_attachs_size(shared->sub, shared->adata));
130 snprintf(buf, buflen, fmt, tmp);
131 break;
132
133 case 'v':
134 snprintf(buf, buflen, "%s", mutt_make_version());
135 break;
136
137 case 0:
138 *buf = '\0';
139 return src;
140
141 default:
142 snprintf(buf, buflen, "%%%s%c", prec, op);
143 break;
144 }
145
146 if (optional)
147 {
148 mutt_expando_format(buf, buflen, col, cols, if_str, compose_format_str, data, flags);
149 }
150 // This format function doesn't have any optional expandos,
151 // so there's no `else if (flags & MUTT_FORMAT_OPTIONAL)` clause
152
153 return src;
154}
155
161static int cbar_recalc(struct MuttWindow *win)
162{
163 char buf[1024] = { 0 };
164 struct ComposeSharedData *shared = win->parent->wdata;
165
166 const char *const c_compose_format = cs_subset_string(shared->sub, "compose_format");
167 mutt_expando_format(buf, sizeof(buf), 0, win->state.cols, NONULL(c_compose_format),
168 compose_format_str, (intptr_t) shared, MUTT_FORMAT_NO_FLAGS);
169
170 struct ComposeBarData *cbar_data = win->wdata;
171 if (!mutt_str_equal(buf, cbar_data->compose_format))
172 {
173 mutt_str_replace(&cbar_data->compose_format, buf);
174 win->actions |= WA_REPAINT;
175 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
176 }
177
178 return 0;
179}
180
184static int cbar_repaint(struct MuttWindow *win)
185{
186 struct ComposeBarData *cbar_data = win->wdata;
187
188 mutt_window_move(win, 0, 0);
191
192 mutt_window_move(win, 0, 0);
193 mutt_draw_statusline(win, win->state.cols, cbar_data->compose_format,
194 mutt_str_len(cbar_data->compose_format));
196 mutt_debug(LL_DEBUG5, "repaint done\n");
197
198 return 0;
199}
200
205{
206 if (nc->event_type != NT_COLOR)
207 return 0;
208 if (!nc->global_data || !nc->event_data)
209 return -1;
210
211 struct EventColor *ev_c = nc->event_data;
212
213 // MT_COLOR_MAX is sent on `uncolor *`
214 if ((ev_c->cid != MT_COLOR_STATUS) && (ev_c->cid != MT_COLOR_NORMAL) &&
215 (ev_c->cid != MT_COLOR_MAX))
216 {
217 return 0;
218 }
219
220 struct MuttWindow *win_cbar = nc->global_data;
221 win_cbar->actions |= WA_REPAINT;
222 mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
223
224 return 0;
225}
226
231{
232 if (nc->event_type != NT_CONFIG)
233 return 0;
234 if (!nc->global_data || !nc->event_data)
235 return -1;
236
237 struct EventConfig *ev_c = nc->event_data;
238 if (!mutt_str_equal(ev_c->name, "compose_format"))
239 return 0;
240
241 struct MuttWindow *win_cbar = nc->global_data;
242 win_cbar->actions |= WA_RECALC;
243 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC\n");
244
245 return 0;
246}
247
252{
253 if (nc->event_type != NT_EMAIL)
254 return 0;
255 if (!nc->global_data || !nc->event_data)
256 return -1;
257
258 struct MuttWindow *win_cbar = nc->global_data;
259 win_cbar->actions |= WA_RECALC;
260 mutt_debug(LL_DEBUG5, "compose done, request WA_RECALC\n");
261
262 return 0;
263}
264
269{
270 if (nc->event_type != NT_WINDOW)
271 return 0;
272 if (!nc->global_data || !nc->event_data)
273 return -1;
274
275 struct MuttWindow *win_cbar = nc->global_data;
276 struct EventWindow *ev_w = nc->event_data;
277 if (ev_w->win != win_cbar)
278 return 0;
279
281 {
282 win_cbar->actions |= WA_RECALC;
283 mutt_debug(LL_DEBUG5, "window state done, request WA_RECALC\n");
284 }
285 else if (nc->event_subtype == NT_WINDOW_DELETE)
286 {
287 struct MuttWindow *dlg = win_cbar->parent;
288 struct ComposeSharedData *shared = dlg->wdata;
289
294
295 mutt_debug(LL_DEBUG5, "window delete done\n");
296 }
297
298 return 0;
299}
300
305struct MuttWindow *cbar_new(struct ComposeSharedData *shared)
306{
310
311 win_cbar->wdata = cbar_data_new();
312 win_cbar->wdata_free = cbar_data_free;
313 win_cbar->recalc = cbar_recalc;
314 win_cbar->repaint = cbar_repaint;
315
320
321 return win_cbar;
322}
Compose Attach Data.
int num_attachments(struct ComposeAttachData *adata)
Count the number of attachments.
Definition: cbar.c:87
struct MuttWindow * cbar_new(struct ComposeSharedData *shared)
Create the Compose Bar (status)
Definition: cbar.c:305
Compose Bar.
struct ComposeBarData * cbar_data_new(void)
Free the private data attached to the MuttWindow.
Definition: cbar_data.c:54
Compose Bar Data.
Color and attribute parsing.
@ MT_COLOR_MAX
Definition: color.h:92
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:75
@ 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:926
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
char * ShortHostname
Short version of the hostname.
Definition: globals.c:40
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:104
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:742
#define mutt_debug(LEVEL,...)
Definition: logging2.h:87
int cbar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t -.
Definition: cbar.c:204
int cbar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: cbar.c:230
static int cbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: cbar.c:268
static int cbar_email_observer(struct NotifyCallback *nc)
Notification that the Email has changed - Implements observer_t -.
Definition: cbar.c:251
static int cbar_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition: cbar.c:161
static int cbar_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition: cbar.c:184
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:38
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: logging2.h:47
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:798
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:568
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:327
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
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
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:294
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:241
#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:206
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:205
#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:1439
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: muttlib.c:1647
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:71
const char * name
Name of config item that changed.
Definition: subset.h:73
An Event that happened to a Window.
Definition: mutt_window.h:215
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:216
int max
Number of entries in the menu.
Definition: lib.h:72
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:181
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:170
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:159
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