NeoMutt  2025-01-09-81-g753ae0
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
helpbar.c
Go to the documentation of this file.
1
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 "lib.h"
75#include "color/lib.h"
76#include "key/lib.h"
77#include "menu/lib.h"
78
89static bool make_help(enum MenuType menu, int op, const char *txt, struct Buffer *buf)
90{
91 if (km_expand_key(km_find_func(menu, 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(enum MenuType menu, const struct Mapping *items, struct Buffer *buf)
109{
110 for (int i = 0; items[i].name; i++)
111 {
112 if (!make_help(menu, items[i].value, _(items[i].name), buf))
113 continue;
114
115 buf_addstr(buf, " ");
116 }
117}
118
125static int helpbar_recalc(struct MuttWindow *win)
126{
128 if (!wdata)
129 return 0;
130
131 FREE(&wdata->help_str);
132
133 struct MuttWindow *win_focus = window_get_focus();
134 if (!win_focus)
135 return 0;
136
137 // Ascend the Window tree until we find help_data
138 while (win_focus && !win_focus->help_data)
139 win_focus = win_focus->parent;
140
141 if (!win_focus)
142 return 0;
143
144 struct Buffer *helpstr = buf_pool_get();
145 compile_help(win_focus->help_menu, win_focus->help_data, helpstr);
146
147 wdata->help_menu = win_focus->help_menu;
148 wdata->help_data = win_focus->help_data;
149 wdata->help_str = buf_strdup(helpstr);
150 buf_pool_release(&helpstr);
151
152 win->actions |= WA_REPAINT;
153 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
154 return 0;
155}
156
163static int helpbar_repaint(struct MuttWindow *win)
164{
166 if (!wdata)
167 return 0;
168
170 mutt_window_move(win, 0, 0);
171 mutt_paddstr(win, win->state.cols, wdata->help_str);
173
174 mutt_debug(LL_DEBUG5, "repaint done\n");
175 return 0;
176}
177
185{
186 if (nc->event_type != NT_BINDING)
187 return 0;
188 if (!nc->global_data || !nc->event_data)
189 return -1;
190 if (nc->event_subtype >= NT_MACRO_ADD)
191 return 0;
192
193 struct MuttWindow *win_helpbar = nc->global_data;
194 struct HelpbarWindowData *wdata = helpbar_wdata_get(win_helpbar);
195 if (!wdata)
196 return 0;
197
198 struct EventBinding *ev_b = nc->event_data;
199 if (wdata->help_menu != ev_b->menu)
200 return 0;
201
202 win_helpbar->actions |= WA_RECALC;
203 mutt_debug(LL_DEBUG5, "binding done, request WA_RECALC\n");
204 return 0;
205}
206
214{
215 if (nc->event_type != NT_COLOR)
216 return 0;
217 if (!nc->global_data || !nc->event_data)
218 return -1;
219
220 struct EventColor *ev_c = nc->event_data;
221
222 // MT_COLOR_MAX is sent on `uncolor *`
223 if ((ev_c->cid != MT_COLOR_STATUS) && (ev_c->cid != MT_COLOR_NORMAL) &&
224 (ev_c->cid != MT_COLOR_MAX))
225 {
226 return 0;
227 }
228
229 struct MuttWindow *win_helpbar = nc->global_data;
230
231 win_helpbar->actions |= WA_REPAINT;
232 mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
233 return 0;
234}
235
243{
244 if (nc->event_type != NT_CONFIG)
245 return 0;
246 if (!nc->global_data || !nc->event_data)
247 return -1;
248
249 struct EventConfig *ev_c = nc->event_data;
250 if (!mutt_str_equal(ev_c->name, "help"))
251 return 0;
252
253 struct MuttWindow *win_helpbar = nc->global_data;
254 win_helpbar->state.visible = cs_subset_bool(NeoMutt->sub, "help");
255
256 mutt_window_reflow(win_helpbar->parent);
257 mutt_debug(LL_DEBUG5, "config done: '%s', request WA_REFLOW on parent\n", ev_c->name);
258 return 0;
259}
260
271{
272 if (nc->event_type != NT_WINDOW)
273 return 0;
274 if (!nc->global_data || !nc->event_data)
275 return -1;
276
277 struct MuttWindow *win_helpbar = nc->global_data;
278
280 {
281 if (!mutt_window_is_visible(win_helpbar))
282 return 0;
283
284 win_helpbar->actions |= WA_RECALC;
285 mutt_debug(LL_DEBUG5, "window focus: request WA_RECALC\n");
286 return 0;
287 }
288
289 // The next two notifications must be specifically for us
290 struct EventWindow *ew = nc->event_data;
291 if (ew->win != win_helpbar)
292 return 0;
293
295 {
296 win_helpbar->actions |= WA_RECALC;
297 mutt_debug(LL_DEBUG5, "window state: request WA_RECALC\n");
298 }
299 else if (nc->event_subtype == NT_WINDOW_DELETE)
300 {
305 mutt_debug(LL_DEBUG5, "window delete done\n");
306 }
307
308 return 0;
309}
310
318{
322 win->state.visible = cs_subset_bool(NeoMutt->sub, "help");
323
324 win->recalc = helpbar_recalc;
326
327 win->wdata = helpbar_wdata_new();
329
334 return win;
335}
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:71
void mutt_color_observer_add(observer_t callback, void *global_data)
Add an observer.
Definition: notify.c:61
@ MT_COLOR_MAX
Definition: color.h:99
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:80
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:55
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:343
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static int helpbar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t -.
Definition: helpbar.c:213
static int helpbar_binding_observer(struct NotifyCallback *nc)
Notification that a Key Binding has changed - Implements observer_t -.
Definition: helpbar.c:184
static int helpbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: helpbar.c:270
static int helpbar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: helpbar.c:242
static int helpbar_recalc(struct MuttWindow *win)
Recalculate the display of the Help Bar - Implements MuttWindow::recalc() -.
Definition: helpbar.c:125
static int helpbar_repaint(struct MuttWindow *win)
Redraw the Help Bar - Implements MuttWindow::repaint() -.
Definition: helpbar.c:163
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.
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(enum MenuType menu, const struct Mapping *items, struct Buffer *buf)
Create the text for the help menu.
Definition: helpbar.c:108
static bool make_help(enum MenuType menu, 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:317
struct Keymap * km_find_func(enum MenuType mtype, int func)
Find a function's mapping in a Menu.
Definition: lib.c:483
bool km_expand_key(struct Keymap *map, struct Buffer *buf)
Get the key string bound to a Keymap.
Definition: lib.c:451
Manage keymappings.
@ NT_MACRO_ADD
Key macro has been added.
Definition: lib.h:139
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
#define FREE(x)
Definition: memory.h:55
GUI present the user with a selectable list.
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:661
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?
Definition: mutt_window.c:477
void mutt_window_reflow(struct MuttWindow *win)
Resize a Window and its children.
Definition: mutt_window.c:309
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:182
struct MuttWindow * window_get_focus(void)
Get the currently focused Window.
Definition: mutt_window.c:633
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
Definition: mutt_window.c:300
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110
@ 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:39
@ NT_WINDOW_STATE
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:230
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:229
@ NT_WINDOW_FOCUS
Window focus has changed.
Definition: mutt_window.h:232
#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:53
@ MUTT_WIN_SIZE_FIXED
Window has a fixed size.
Definition: mutt_window.h:48
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:57
@ 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:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
struct MuttWindow * RootWindow
Parent of all Windows.
Definition: rootwin.c:106
GUI display the mailboxes in a side panel.
Key value store.
String manipulation buffer.
Definition: buffer.h:36
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition: subset.h:51
A key binding Event.
Definition: lib.h:120
enum MenuType menu
Menu, e.g. MENU_PAGER.
Definition: lib.h:121
An Event that happened to a Colour.
Definition: notify2.h:55
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:56
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:239
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:240
Help Bar Window data -.
Definition: private.h:34
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: private.h:35
char * help_str
Formatted Help Bar string.
Definition: private.h:37
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
const struct Mapping * help_data
Data for the Help Bar.
Definition: mutt_window.h:142
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:187
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:173
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:159
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: mutt_window.h:141
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:42
struct Notify * notify
Notifications handler.
Definition: neomutt.h:43
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
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:60
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:61
MenuType
Types of GUI selections.
Definition: type.h:36
@ MENU_GENERIC
Generic selection list.
Definition: type.h:46