NeoMutt  2024-04-25-89-g194907
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 <stddef.h>
70#include <stdio.h>
71#include "private.h"
72#include "mutt/lib.h"
73#include "config/lib.h"
74#include "core/lib.h"
75#include "gui/lib.h"
76#include "lib.h"
77#include "color/lib.h"
78#include "key/lib.h"
79#include "menu/lib.h"
80
92static bool make_help(char *buf, size_t buflen, const char *txt, enum MenuType menu, int op)
93{
94 char tmp[128] = { 0 };
95
96 if (km_expand_key(tmp, sizeof(tmp), km_find_func(menu, op)) ||
97 km_expand_key(tmp, sizeof(tmp), km_find_func(MENU_GENERIC, op)))
98 {
99 snprintf(buf, buflen, "%s:%s", tmp, txt);
100 return true;
101 }
102
103 buf[0] = '\0';
104 return false;
105}
106
115static char *compile_help(char *buf, size_t buflen, enum MenuType menu,
116 const struct Mapping *items)
117{
118 char *pbuf = buf;
119
120 for (int i = 0; items[i].name && (buflen > 2); i++)
121 {
122 if (!make_help(pbuf, buflen, _(items[i].name), menu, items[i].value))
123 continue;
124
125 const size_t len = mutt_str_len(pbuf);
126 pbuf += len;
127 buflen -= len;
128
129 *pbuf++ = ' ';
130 *pbuf++ = ' ';
131 buflen -= 2;
132 }
133 return buf;
134}
135
142static int helpbar_recalc(struct MuttWindow *win)
143{
145 if (!wdata)
146 return 0;
147
148 FREE(&wdata->help_str);
149
150 struct MuttWindow *win_focus = window_get_focus();
151 if (!win_focus)
152 return 0;
153
154 // Ascend the Window tree until we find help_data
155 while (win_focus && !win_focus->help_data)
156 win_focus = win_focus->parent;
157
158 if (!win_focus)
159 return 0;
160
161 char helpstr[1024] = { 0 };
162 compile_help(helpstr, sizeof(helpstr), win_focus->help_menu, win_focus->help_data);
163
164 wdata->help_menu = win_focus->help_menu;
165 wdata->help_data = win_focus->help_data;
166 wdata->help_str = mutt_str_dup(helpstr);
167
168 win->actions |= WA_REPAINT;
169 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
170 return 0;
171}
172
179static int helpbar_repaint(struct MuttWindow *win)
180{
182 if (!wdata)
183 return 0;
184
186 mutt_window_move(win, 0, 0);
187 mutt_paddstr(win, win->state.cols, wdata->help_str);
189
190 mutt_debug(LL_DEBUG5, "repaint done\n");
191 return 0;
192}
193
201{
202 if (nc->event_type != NT_BINDING)
203 return 0;
204 if (!nc->global_data || !nc->event_data)
205 return -1;
206 if (nc->event_subtype >= NT_MACRO_ADD)
207 return 0;
208
209 struct MuttWindow *win_helpbar = nc->global_data;
210 struct HelpbarWindowData *wdata = helpbar_wdata_get(win_helpbar);
211 if (!wdata)
212 return 0;
213
214 struct EventBinding *ev_b = nc->event_data;
215 if (wdata->help_menu != ev_b->menu)
216 return 0;
217
218 win_helpbar->actions |= WA_RECALC;
219 mutt_debug(LL_DEBUG5, "binding done, request WA_RECALC\n");
220 return 0;
221}
222
230{
231 if (nc->event_type != NT_COLOR)
232 return 0;
233 if (!nc->global_data || !nc->event_data)
234 return -1;
235
236 struct EventColor *ev_c = nc->event_data;
237
238 // MT_COLOR_MAX is sent on `uncolor *`
239 if ((ev_c->cid != MT_COLOR_STATUS) && (ev_c->cid != MT_COLOR_NORMAL) &&
240 (ev_c->cid != MT_COLOR_MAX))
241 {
242 return 0;
243 }
244
245 struct MuttWindow *win_helpbar = nc->global_data;
246
247 win_helpbar->actions |= WA_REPAINT;
248 mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
249 return 0;
250}
251
259{
260 if (nc->event_type != NT_CONFIG)
261 return 0;
262 if (!nc->global_data || !nc->event_data)
263 return -1;
264
265 struct EventConfig *ev_c = nc->event_data;
266 if (!mutt_str_equal(ev_c->name, "help"))
267 return 0;
268
269 struct MuttWindow *win_helpbar = nc->global_data;
270 win_helpbar->state.visible = cs_subset_bool(NeoMutt->sub, "help");
271
272 mutt_window_reflow(win_helpbar->parent);
273 mutt_debug(LL_DEBUG5, "config done: '%s', request WA_REFLOW on parent\n", ev_c->name);
274 return 0;
275}
276
287{
288 if (nc->event_type != NT_WINDOW)
289 return 0;
290 if (!nc->global_data || !nc->event_data)
291 return -1;
292
293 struct MuttWindow *win_helpbar = nc->global_data;
294
296 {
297 if (!mutt_window_is_visible(win_helpbar))
298 return 0;
299
300 win_helpbar->actions |= WA_RECALC;
301 mutt_debug(LL_DEBUG5, "window focus: request WA_RECALC\n");
302 return 0;
303 }
304
305 // The next two notifications must be specifically for us
306 struct EventWindow *ew = nc->event_data;
307 if (ew->win != win_helpbar)
308 return 0;
309
311 {
312 win_helpbar->actions |= WA_RECALC;
313 mutt_debug(LL_DEBUG5, "window state: request WA_RECALC\n");
314 }
315 else if (nc->event_subtype == NT_WINDOW_DELETE)
316 {
321 mutt_debug(LL_DEBUG5, "window delete done\n");
322 }
323
324 return 0;
325}
326
334{
338 win->state.visible = cs_subset_bool(NeoMutt->sub, "help");
339
340 win->recalc = helpbar_recalc;
342
343 win->wdata = helpbar_wdata_new();
345
350 return win;
351}
Color and attribute parsing.
void mutt_color_observer_remove(observer_t callback, void *global_data)
Remove an observer.
Definition: notify.c:69
void mutt_color_observer_add(observer_t callback, void *global_data)
Add an observer.
Definition: notify.c:59
@ MT_COLOR_MAX
Definition: color.h:94
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:75
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:59
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:341
#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:229
static int helpbar_binding_observer(struct NotifyCallback *nc)
Notification that a Key Binding has changed - Implements observer_t -.
Definition: helpbar.c:200
static int helpbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: helpbar.c:286
static int helpbar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: helpbar.c:258
static int helpbar_recalc(struct MuttWindow *win)
Recalculate the display of the Help Bar - Implements MuttWindow::recalc() -.
Definition: helpbar.c:142
static int helpbar_repaint(struct MuttWindow *win)
Redraw the Help Bar - Implements MuttWindow::repaint() -.
Definition: helpbar.c:179
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 char * compile_help(char *buf, size_t buflen, enum MenuType menu, const struct Mapping *items)
Create the text for the help menu.
Definition: helpbar.c:115
static bool make_help(char *buf, size_t buflen, const char *txt, enum MenuType menu, int op)
Create one entry for the Help Bar.
Definition: helpbar.c:92
struct MuttWindow * helpbar_new(void)
Create the Help Bar Window.
Definition: helpbar.c:333
struct Keymap * km_find_func(enum MenuType mtype, int func)
Find a function's mapping in a Menu.
Definition: lib.c:512
int km_expand_key(char *s, size_t len, struct Keymap *map)
Get the key string bound to a Keymap.
Definition: lib.c:460
Manage keymappings.
@ NT_MACRO_ADD
Key macro has been added.
Definition: lib.h:138
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
#define FREE(x)
Definition: memory.h:45
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
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
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:512
void mutt_window_reflow(struct MuttWindow *win)
Resize a Window and its children.
Definition: mutt_window.c:344
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:668
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:297
#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:38
@ 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:52
@ MUTT_WIN_SIZE_FIXED
Window has a fixed size.
Definition: mutt_window.h:47
@ 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 MuttWindow * RootWindow
Parent of all Windows.
Definition: rootwin.c:106
GUI display the mailboxes in a side panel.
Key value store.
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition: subset.h:52
A key binding Event.
Definition: lib.h:119
enum MenuType menu
Menu, e.g. MENU_PAGER.
Definition: lib.h:120
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: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
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:59
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
MenuType
Types of GUI selections.
Definition: type.h:36
@ MENU_GENERIC
Generic selection list.
Definition: type.h:46