NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
helpbar.c
Go to the documentation of this file.
1
22
66
67#include "config.h"
68#include <stdbool.h>
69#include "private.h"
70#include "mutt/lib.h"
71#include "config/lib.h"
72#include "core/lib.h"
73#include "gui/lib.h"
74#include "gui/module_data.h"
75#include "lib.h"
76#include "color/lib.h"
77#include "key/lib.h"
78
89static bool make_help(const struct MenuDefinition *md, int op, const char *txt,
90 struct Buffer *buf)
91{
92 if (keymap_expand_key(km_find_func(md, op), buf))
93 {
94 buf_addch(buf, ':');
95 buf_addstr(buf, txt);
96 return true;
97 }
98
99 return false;
100}
101
108static void compile_help(const struct MenuDefinition *md,
109 const struct Mapping *items, struct Buffer *buf)
110{
111 for (int i = 0; items[i].name; i++)
112 {
113 if (!make_help(md, items[i].value, _(items[i].name), buf))
114 continue;
115
116 buf_addstr(buf, " ");
117 }
118}
119
126static int helpbar_recalc(struct MuttWindow *win)
127{
129 if (!wdata)
130 return 0;
131
132 FREE(&wdata->help_str);
133
134 struct MuttWindow *win_focus = window_get_focus();
135 if (!win_focus)
136 return 0;
137
138 // Ascend the Window tree until we find help_data
139 while (win_focus && !win_focus->help_data)
140 win_focus = win_focus->parent;
141
142 if (!win_focus)
143 return 0;
144
145 struct Buffer *helpstr = buf_pool_get();
146 compile_help(win_focus->help_md, win_focus->help_data, helpstr);
147
148 wdata->help_md = win_focus->help_md;
149 wdata->help_data = win_focus->help_data;
150 wdata->help_str = buf_strdup(helpstr);
151 buf_pool_release(&helpstr);
152
153 win->actions |= WA_REPAINT;
154 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
155 return 0;
156}
157
164static int helpbar_repaint(struct MuttWindow *win)
165{
167 if (!wdata)
168 return 0;
169
171 mutt_window_move(win, 0, 0);
172 mutt_paddstr(win, win->state.cols, wdata->help_str);
174
175 mutt_debug(LL_DEBUG5, "repaint done\n");
176 return 0;
177}
178
186{
187 if (nc->event_type != NT_BINDING)
188 return 0;
189 if (!nc->global_data || !nc->event_data)
190 return -1;
191 if (nc->event_subtype >= NT_MACRO_ADD)
192 return 0;
193
194 struct MuttWindow *win_helpbar = nc->global_data;
195 struct HelpbarWindowData *wdata = helpbar_wdata_get(win_helpbar);
196 if (!wdata)
197 return 0;
198
199 struct EventBinding *ev_b = nc->event_data;
200 if (wdata->help_md && (wdata->help_md != ev_b->menu))
201 return 0;
202
203 win_helpbar->actions |= WA_RECALC;
204 mutt_debug(LL_DEBUG5, "binding done, request WA_RECALC\n");
205 return 0;
206}
207
215{
216 if (nc->event_type != NT_COLOR)
217 return 0;
218 if (!nc->global_data || !nc->event_data)
219 return -1;
220
221 struct EventColor *ev_c = nc->event_data;
222
223 // MT_COLOR_MAX is sent on `uncolor *`
224 if ((ev_c->cid != MT_COLOR_STATUS) && (ev_c->cid != MT_COLOR_NORMAL) &&
225 (ev_c->cid != MT_COLOR_MAX))
226 {
227 return 0;
228 }
229
230 struct MuttWindow *win_helpbar = nc->global_data;
231
232 win_helpbar->actions |= WA_REPAINT;
233 mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
234 return 0;
235}
236
244{
245 if (nc->event_type != NT_CONFIG)
246 return 0;
247 if (!nc->global_data || !nc->event_data)
248 return -1;
249
250 struct EventConfig *ev_c = nc->event_data;
251 if (!mutt_str_equal(ev_c->name, "help"))
252 return 0;
253
254 struct MuttWindow *win_helpbar = nc->global_data;
255 win_helpbar->state.visible = cs_subset_bool(NeoMutt->sub, "help");
256
257 mutt_window_reflow(win_helpbar->parent);
258 mutt_debug(LL_DEBUG5, "config done: '%s', request WA_REFLOW on parent\n", ev_c->name);
259 return 0;
260}
261
272{
273 if (nc->event_type != NT_WINDOW)
274 return 0;
275 if (!nc->global_data || !nc->event_data)
276 return -1;
277
278 struct MuttWindow *win_helpbar = nc->global_data;
279
281 {
282 if (!mutt_window_is_visible(win_helpbar))
283 return 0;
284
285 win_helpbar->actions |= WA_RECALC;
286 mutt_debug(LL_DEBUG5, "window focus: request WA_RECALC\n");
287 return 0;
288 }
289
290 // The next two notifications must be specifically for us
291 struct EventWindow *ew = nc->event_data;
292 if (ew->win != win_helpbar)
293 return 0;
294
296 {
297 win_helpbar->actions |= WA_RECALC;
298 mutt_debug(LL_DEBUG5, "window state: request WA_RECALC\n");
299 }
300 else if (nc->event_subtype == NT_WINDOW_DELETE)
301 {
306 if (mod_data && mod_data->root_window)
308 helpbar_window_observer, win_helpbar);
309 mutt_debug(LL_DEBUG5, "window delete done\n");
310 }
311
312 return 0;
313}
314
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
Color and attribute parsing.
void mutt_color_observer_remove(observer_t callback, void *global_data)
Remove an observer.
Definition notify.c:73
void mutt_color_observer_add(observer_t callback, void *global_data)
Add an observer.
Definition notify.c:62
@ MT_COLOR_MAX
Definition color.h:97
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition color.h:78
@ MT_COLOR_NORMAL
Plain text.
Definition color.h:53
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.
void mutt_paddstr(struct MuttWindow *win, int n, const char *s)
Display a string on screen, padded if necessary.
Definition curs_lib.c:344
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
static int helpbar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t -.
Definition helpbar.c:214
static int helpbar_binding_observer(struct NotifyCallback *nc)
Notification that a Key Binding has changed - Implements observer_t -.
Definition helpbar.c:185
static int helpbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition helpbar.c:271
static int helpbar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition helpbar.c:243
static int helpbar_recalc(struct MuttWindow *win)
Recalculate the display of the Help Bar - Implements MuttWindow::recalc() -.
Definition helpbar.c:126
static int helpbar_repaint(struct MuttWindow *win)
Redraw the Help Bar - Implements MuttWindow::repaint() -.
Definition helpbar.c:164
void helpbar_wdata_free(struct MuttWindow *win, void **ptr)
Free Helpbar Window data - Implements MuttWindow::wdata_free() -.
Definition wdata.c:47
Convenience wrapper for the gui headers.
Gui private Module data.
Help Bar Convenience wrapper for the Help Bar headers.
Shared constants/structs that are private to the Help Bar.
struct HelpbarWindowData * helpbar_wdata_get(struct MuttWindow *win)
Get the Helpbar data for this window.
Definition wdata.c:64
struct HelpbarWindowData * helpbar_wdata_new(void)
Create new Window data for the Helpbar.
Definition wdata.c:39
static void compile_help(const struct MenuDefinition *md, const struct Mapping *items, struct Buffer *buf)
Create the text for the help menu.
Definition helpbar.c:108
static bool make_help(const struct MenuDefinition *md, int op, const char *txt, struct Buffer *buf)
Create one entry for the Help Bar.
Definition helpbar.c:89
struct MuttWindow * helpbar_new(void)
Create the Help Bar Window.
Definition helpbar.c:321
bool keymap_expand_key(struct Keymap *km, struct Buffer *buf)
Get the key string bound to a Keymap.
Definition keymap.c:248
Manage keymappings.
struct Keymap * km_find_func(const struct MenuDefinition *md, int func)
Find a function's mapping in a Menu.
Definition menu.c:141
@ NT_MACRO_ADD
Key macro has been added.
Definition notify.h:50
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
@ MODULE_ID_GUI
ModuleGui, Graphical code
Definition module_api.h:45
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
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
const 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:63
const struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the Colour ID.
Definition mutt_curses.c:79
bool mutt_window_is_visible(struct MuttWindow *win)
Is the Window visible?
void mutt_window_reflow(struct MuttWindow *win)
Resize a Window and its children.
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
struct MuttWindow * window_get_focus(void)
Get the currently focused Window.
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
@ WT_HELP_BAR
Help Bar containing list of useful key bindings.
Definition mutt_window.h:96
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition mutt_window.h:38
@ WA_REPAINT
Redraw the contents of the Window.
@ WA_RECALC
Recalculate the contents of the Window.
@ NT_WINDOW_STATE
Window state has changed, e.g. WN_VISIBLE.
@ NT_WINDOW_DELETE
Window is about to be deleted.
@ NT_WINDOW_FOCUS
Window focus has changed.
#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
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition notify_type.h:58
@ 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_BINDING
Key binding has changed, NotifyBinding, EventBinding.
Definition notify_type.h:40
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:36
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition subset.h:51
A key binding Event.
Definition notify.h:33
const struct MenuDefinition * menu
Menu Definition.
Definition notify.h:34
An Event that happened to a Colour.
Definition notify2.h:52
enum ColorId cid
Colour ID that has changed.
Definition notify2.h:53
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.
struct MuttWindow * win
Window that changed.
Gui private Module data.
Definition module_data.h:32
struct MuttWindow * root_window
Parent of all Windows.
Definition module_data.h:41
Help Bar Window data -.
Definition private.h:34
char * help_str
Formatted Help Bar string.
Definition private.h:37
const struct MenuDefinition * help_md
Menu Definition for key bindings.
Definition private.h:35
const struct Mapping * help_data
Data for the Help Bar.
Definition private.h:36
Mapping between user-readable string and a constant.
Definition mapping.h:33
const char * name
String value.
Definition mapping.h:34
Functions for a Dialog or Window.
Definition menu.h:77
const struct Mapping * help_data
Data for the Help Bar.
const struct MenuDefinition * help_md
Menu Definition for key bindings.
int(* repaint)(struct MuttWindow *win)
struct WindowState state
Current state of the Window.
void * wdata
Private data.
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
int(* recalc)(struct MuttWindow *win)
void(* wdata_free)(struct MuttWindow *win, void **ptr)
struct MuttWindow * parent
Parent Window.
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Container for Accounts, Notifications.
Definition neomutt.h:41
struct Notify * notify
Notifications handler.
Definition neomutt.h:45
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
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
bool visible
Window is visible.
Definition mutt_window.h:59
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition mutt_window.h:60