NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
helpbar.c
Go to the documentation of this file.
1 
69 #include "config.h"
70 #include <stddef.h>
71 #include <stdbool.h>
72 #include <stdio.h>
73 #include "private.h"
74 #include "mutt/lib.h"
75 #include "config/lib.h"
76 #include "core/lib.h"
77 #include "gui/lib.h"
78 #include "lib.h"
79 #include "menu/lib.h"
80 #include "keymap.h"
81 
93 static bool make_help(char *buf, size_t buflen, const char *txt, enum MenuType menu, int op)
94 {
95  char tmp[128];
96 
97  if (km_expand_key(tmp, sizeof(tmp), km_find_func(menu, op)) ||
98  km_expand_key(tmp, sizeof(tmp), km_find_func(MENU_GENERIC, op)))
99  {
100  snprintf(buf, buflen, "%s:%s", tmp, txt);
101  return true;
102  }
103 
104  buf[0] = '\0';
105  return false;
106 }
107 
116 static char *compile_help(char *buf, size_t buflen, enum MenuType menu,
117  const struct Mapping *items)
118 {
119  char *pbuf = buf;
120 
121  for (int i = 0; items[i].name && (buflen > 2); i++)
122  {
123  if (!make_help(pbuf, buflen, _(items[i].name), menu, items[i].value))
124  continue;
125 
126  const size_t len = mutt_str_len(pbuf);
127  pbuf += len;
128  buflen -= len;
129 
130  *pbuf++ = ' ';
131  *pbuf++ = ' ';
132  buflen -= 2;
133  }
134  return buf;
135 }
136 
143 static int helpbar_recalc(struct MuttWindow *win)
144 {
146  if (!wdata)
147  return 0;
148 
149  FREE(&wdata->help_str);
150 
152  if (!win_focus)
153  return 0;
154 
155  // Ascend the Window tree until we find help_data
156  while (win_focus && !win_focus->help_data)
157  win_focus = win_focus->parent;
158 
159  if (!win_focus)
160  return 0;
161 
162  char helpstr[1024] = { 0 };
163  compile_help(helpstr, sizeof(helpstr), win_focus->help_menu, win_focus->help_data);
164 
165  wdata->help_menu = win_focus->help_menu;
166  wdata->help_data = win_focus->help_data;
167  wdata->help_str = mutt_str_dup(helpstr);
168 
169  win->actions |= WA_REPAINT;
170  mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
171  return 0;
172 }
173 
180 static int helpbar_repaint(struct MuttWindow *win)
181 {
182  if (!mutt_window_is_visible(win))
183  return 0;
184 
186  if (!wdata)
187  return 0;
188 
190  mutt_window_move(win, 0, 0);
191  mutt_paddstr(win, win->state.cols, wdata->help_str);
193 
194  mutt_debug(LL_DEBUG5, "repaint done\n");
195  return 0;
196 }
197 
205 {
206  if ((nc->event_type != NT_BINDING) || !nc->global_data || !nc->event_data)
207  return -1;
208 
209  if (nc->event_subtype >= NT_MACRO_ADD)
210  return 0;
211 
212  struct MuttWindow *win_helpbar = nc->global_data;
213  struct HelpbarWindowData *wdata = helpbar_wdata_get(win_helpbar);
214  if (!wdata)
215  return 0;
216 
217  struct EventBinding *ev_b = nc->event_data;
218  if (wdata->help_menu != ev_b->menu)
219  return 0;
220 
221  win_helpbar->actions |= WA_RECALC;
222  mutt_debug(LL_DEBUG5, "binding done, request WA_RECALC\n");
223  return 0;
224 }
225 
233 {
234  if ((nc->event_type != NT_COLOR) || !nc->global_data || !nc->event_data)
235  return -1;
236 
237  struct EventColor *ev_c = nc->event_data;
238 
239  // MT_COLOR_MAX is sent on `uncolor *`
240  if ((ev_c->color != MT_COLOR_STATUS) && (ev_c->color != MT_COLOR_MAX))
241  return 0;
242 
243  struct MuttWindow *win_helpbar = nc->global_data;
244 
245  win_helpbar->actions |= WA_REPAINT;
246  mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
247  return 0;
248 }
249 
257 {
258  if ((nc->event_type != NT_CONFIG) || !nc->global_data || !nc->event_data)
259  return -1;
260 
261  struct EventConfig *ev_c = nc->event_data;
262  if (!mutt_str_equal(ev_c->name, "help"))
263  return 0;
264 
265  struct MuttWindow *win_helpbar = nc->global_data;
266  win_helpbar->state.visible = cs_subset_bool(NeoMutt->sub, "help");
267 
268  mutt_window_reflow(win_helpbar->parent);
269  mutt_debug(LL_DEBUG5, "config done: '%s', request WA_REFLOW on parent\n", ev_c->name);
270  return 0;
271 }
272 
283 {
284  if ((nc->event_type != NT_WINDOW) || !nc->global_data || !nc->event_data)
285  return -1;
286 
287  struct MuttWindow *win_helpbar = nc->global_data;
288 
289  if (nc->event_subtype == NT_WINDOW_FOCUS)
290  {
291  if (!mutt_window_is_visible(win_helpbar))
292  return 0;
293 
294  win_helpbar->actions |= WA_RECALC;
295  mutt_debug(LL_DEBUG5, "window focus: request WA_RECALC\n");
296  return 0;
297  }
298 
299  // The next two notifications must be specifically for us
300  struct EventWindow *ew = nc->event_data;
301  if (ew->win != win_helpbar)
302  return 0;
303 
304  if (nc->event_subtype == NT_WINDOW_STATE)
305  {
306  win_helpbar->actions |= WA_RECALC;
307  mutt_debug(LL_DEBUG5, "window state: request WA_RECALC\n");
308  }
309  else if (nc->event_subtype == NT_WINDOW_DELETE)
310  {
315  mutt_debug(LL_DEBUG5, "window delete done\n");
316  }
317 
318  return 0;
319 }
320 
327 struct MuttWindow *helpbar_new(void)
328 {
329  struct MuttWindow *win =
332  win->state.visible = cs_subset_bool(NeoMutt->sub, "help");
333 
334  win->recalc = helpbar_recalc;
335  win->repaint = helpbar_repaint;
336 
337  win->wdata = helpbar_wdata_new();
339 
344  return win;
345 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
Convenience wrapper for the gui headers.
struct Keymap * km_find_func(enum MenuType mtype, int func)
Find a function&#39;s mapping in a Menu.
Definition: keymap.c:946
Key macro has been added.
Definition: keymap.h:121
Manage keymappings.
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
MenuType
Types of GUI selections.
Definition: type.h:35
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:160
#define WA_REPAINT
Redraw the contents of the Window.
Definition: mutt_window.h:111
Data passed to a notification function.
Definition: observer.h:39
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
An Event that happened to a Colour.
Definition: color.h:122
An Event that happened to a Window.
Definition: mutt_window.h:215
Generic selection list.
Definition: type.h:45
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: mutt_window.h:141
Window uses all available vertical space.
Definition: mutt_window.h:38
void mutt_curses_set_color(enum ColorId color)
Set the current colour for text.
Definition: mutt_curses.c:56
A config-change event.
Definition: subset.h:69
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define _(a)
Definition: message.h:28
Window is about to be deleted.
Definition: mutt_window.h:206
Window focus has changed.
Definition: mutt_window.h:209
struct MuttWindow * helpbar_new(void)
Create the Help Bar Window.
Definition: helpbar.c:327
char * help_str
Formatted Help Bar string.
Definition: private.h:37
struct HelpbarWindowData * helpbar_wdata_new(void)
Create new Window data for the Helpbar.
Definition: wdata.c:39
Container for Accounts, Notifications.
Definition: neomutt.h:36
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
Convenience wrapper for the config headers.
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:310
struct HelpbarWindowData * helpbar_wdata_get(struct MuttWindow *win)
Get the Helpbar data for this window.
Definition: wdata.c:61
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
static struct MuttWindow * win_focus
Definition: window.c:36
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Shared constants/structs that are private to the Help Bar.
static int helpbar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t.
Definition: helpbar.c:256
struct MuttWindow * window_get_focus(void)
Get the currently focussed Window.
Definition: mutt_window.c:664
Convenience wrapper for the core headers.
Key binding has changed, NotifyBinding, EventBinding.
Definition: notify_type.h:38
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
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: private.h:35
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
Window has a fixed size.
Definition: mutt_window.h:47
Plain text.
Definition: color.h:58
static int helpbar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t.
Definition: helpbar.c:232
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:207
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
enum MenuType menu
Menu, e.g. MENU_PAGER.
Definition: keymap.h:103
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:39
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
bool visible
Window is visible.
Definition: mutt_window.h:59
bool mutt_window_is_visible(struct MuttWindow *win)
Is the Window visible?
Definition: mutt_window.c:529
Status bar (takes a pattern)
Definition: color.h:75
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:52
int km_expand_key(char *s, size_t len, struct Keymap *map)
Get the key string bound to a Keymap.
Definition: keymap.c:918
const struct Mapping * help_data
Data for the Help Bar.
Definition: mutt_window.h:142
Help Bar containing list of useful key bindings.
Definition: mutt_window.h:96
const struct Mapping * help_data
Data for the Help Bar.
Definition: private.h:36
Help Bar Window data -.
Definition: private.h:33
enum ColorId color
Definition: color.h:124
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void * event_data
Data from notify_send()
Definition: observer.h:44
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:116
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
static int helpbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t.
Definition: helpbar.c:282
struct MuttWindow * RootWindow
Parent of all Windows.
Definition: rootwin.c:105
void mutt_window_reflow(struct MuttWindow *win)
Resize a Window and its children.
Definition: mutt_window.c:361
struct MuttWindow * parent
Parent Window.
Definition: mutt_window.h:135
void helpbar_wdata_free(struct MuttWindow *win, void **ptr)
Free Helpbar Window data - Implements MuttWindow::wdata_free() -.
Definition: wdata.c:47
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:182
#define FREE(x)
Definition: memory.h:40
Mapping between user-readable string and a constant.
Definition: mapping.h:31
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
static int helpbar_recalc(struct MuttWindow *win)
Recalculate the display of the Help Bar - Implements MuttWindow::recalc() -Generate the help string f...
Definition: helpbar.c:143
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:171
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
static int helpbar_repaint(struct MuttWindow *win)
Redraw the Help Bar - Implements MuttWindow::repaint() -The Help Bar is drawn from the data cached in...
Definition: helpbar.c:180
const char * name
Definition: mapping.h:33
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
Log at debug level 5.
Definition: logging.h:44
Convenience wrapper for the library headers.
void * wdata
Private data.
Definition: mutt_window.h:145
const char * name
Name of config item that changed.
Definition: subset.h:72
static unsigned char * pbuf
Cache PGP data packet.
Definition: pgppacket.c:38
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:93
static int helpbar_binding_observer(struct NotifyCallback *nc)
Notification that a Key Binding has changed - Implements observer_t.
Definition: helpbar.c:204
A key binding Event.
Definition: keymap.h:101
void mutt_paddstr(struct MuttWindow *win, int n, const char *s)
Display a string on screen, padded if necessary.
Definition: curs_lib.c:890
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110