NeoMutt  2025-12-11-980-ge38c27
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
init.c File Reference

Set up the key bindings. More...

#include "config.h"
#include <string.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "init.h"
#include "commands.h"
#include "keymap.h"
#include "menu.h"
#include "menudef.h"
#include "module_data.h"
+ Include dependency graph for init.c:

Go to the source code of this file.

Functions

struct SubMenukm_register_submenu (const struct MenuFuncOp functions[])
 Register a submenu.
 
struct MenuDefinitionkm_register_menu (int menu, const char *name)
 Register a menu.
 
void km_menu_add_submenu (struct MenuDefinition *md, struct SubMenu *sm)
 Add a SubMenu to a Menu Definition.
 
void km_menu_add_bindings (struct MenuDefinition *md, const struct MenuOpSeq bindings[])
 Add Keybindings to a Menu.
 
int km_config_observer (struct NotifyCallback *nc)
 Notification that a Config Variable has changed - Implements observer_t -.
 
void km_init (struct KeyModuleData *mod_data)
 Initialise all the menu keybindings.
 
static int menu_defs_sort (const void *a, const void *b, void *sdata)
 Compare two MenuDefinitions by their names - Implements sort_t -.
 
void km_sort (void)
 Sort all the menu keybindings.
 
void km_cleanup (struct KeyModuleData *mod_data)
 Free the key maps.
 
void km_set_abort_key (keycode_t *abort_key)
 Parse the abort_key config string.
 

Variables

const struct Command KeyCommands []
 All the registered Menus - moved to KeyModuleData All the registered SubMenus - moved to KeyModuleData AbortKey - moved to KeyModuleData.
 

Detailed Description

Set up the key bindings.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file init.c.

Function Documentation

◆ km_register_submenu()

struct SubMenu * km_register_submenu ( const struct MenuFuncOp functions[])

Register a submenu.

Parameters
functionsFunction definitions
Return values
ptrSubMenu

Register a set of functions. The result can be used in multiple Menus.

Definition at line 88 of file init.c.

89{
91 struct SubMenu *sm = submenu_new();
92 sm->functions = functions;
93 ARRAY_INIT(&sm->keymaps);
94
95 ARRAY_ADD(&mod_data->sub_menus, sm);
96 return *ARRAY_LAST(&mod_data->sub_menus);
97}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
#define ARRAY_LAST(head)
Convenience method to get the last element.
Definition array.h:145
#define ARRAY_INIT(head)
Initialize an array.
Definition array.h:65
struct SubMenu * submenu_new(void)
Create a new SubMenu.
Definition menudef.c:53
@ MODULE_ID_KEY
ModuleKey, Key mappings
Definition module_api.h:74
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:666
Key private Module data.
Definition module_data.h:34
struct SubMenuArray sub_menus
All registered SubMenus.
Definition module_data.h:40
Container for Accounts, Notifications.
Definition neomutt.h:41
Collection of related functions.
Definition menudef.h:33
const struct MenuFuncOp * functions
All available functions.
Definition menudef.h:35
struct KeymapList keymaps
All keybindings.
Definition menudef.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ km_register_menu()

struct MenuDefinition * km_register_menu ( int menu,
const char * name )

Register a menu.

Parameters
menuMenu Type, e.g. MENU_INDEX
nameMenu name, e.g. "index"
Return values
ptrMenu Definition

Definition at line 105 of file init.c.

106{
108
109 struct MenuDefinition *md = menudef_new();
110 md->id = menu;
111 md->name = mutt_str_dup(name);
112 ARRAY_INIT(&md->submenus);
113
114 ARRAY_ADD(&mod_data->menu_defs, md);
115 return *ARRAY_LAST(&mod_data->menu_defs);
116}
struct MenuDefinition * menudef_new(void)
Create a new MenuDefinition.
Definition menudef.c:84
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
struct MenuDefinitionArray menu_defs
All registered Menus.
Definition module_data.h:39
Functions for a Dialog or Window.
Definition menudef.h:44
struct SubMenuArray submenus
Parts making up the Menu.
Definition menudef.h:47
const char * name
Menu name, e.g. "alias".
Definition menudef.h:46
int id
Menu ID, e.g. MENU_ALIAS.
Definition menudef.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ km_menu_add_submenu()

void km_menu_add_submenu ( struct MenuDefinition * md,
struct SubMenu * sm )

Add a SubMenu to a Menu Definition.

Parameters
mdMenu Definition
smSubMenu to add

Definition at line 123 of file init.c.

124{
125 if (!sm->parent)
126 sm->parent = md;
127
128 ARRAY_ADD(&md->submenus, sm);
129}
struct MenuDefinition * parent
Primary parent.
Definition menudef.h:34
+ Here is the caller graph for this function:

◆ km_menu_add_bindings()

void km_menu_add_bindings ( struct MenuDefinition * md,
const struct MenuOpSeq bindings[] )

Add Keybindings to a Menu.

Parameters
mdMenu Definition
bindingsKeybindings to add

Definition at line 136 of file init.c.

137{
138 for (int i = 0; bindings[i].op != OP_NULL; i++)
139 {
140 if (bindings[i].seq)
141 {
142 km_bind(md, bindings[i].seq, bindings[i].op, NULL, NULL, NULL);
143 }
144 }
145}
enum CommandResult km_bind(const struct MenuDefinition *md, const char *key_str, int op, char *macro, char *desc, struct Buffer *err)
Set up a key binding.
Definition menu.c:52
int op
Operation, e.g. OP_DELETE.
Definition menu.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ km_init()

void km_init ( struct KeyModuleData * mod_data)

Initialise all the menu keybindings.

Parameters
mod_dataKey module data

Definition at line 172 of file init.c.

173{
174 ARRAY_INIT(&mod_data->menu_defs);
175 ARRAY_INIT(&mod_data->sub_menus);
176 mod_data->key_names = keymap_get_key_names();
177
179}
int km_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition init.c:150
struct Mapping * keymap_get_key_names(void)
Get the KeyNames lookup table.
Definition keymap.c:119
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
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition notify_type.h:43
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition subset.h:51
struct Mapping * key_names
Key name lookup table.
Definition module_data.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ km_sort()

void km_sort ( void )

Sort all the menu keybindings.

Definition at line 195 of file init.c.

196{
198 ARRAY_SORT(&mod_data->menu_defs, menu_defs_sort, NULL);
199}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition array.h:373
static int menu_defs_sort(const void *a, const void *b, void *sdata)
Compare two MenuDefinitions by their names - Implements sort_t -.
Definition init.c:184
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ km_cleanup()

void km_cleanup ( struct KeyModuleData * mod_data)

Free the key maps.

Parameters
mod_dataKey module data

Definition at line 205 of file init.c.

206{
207 if (NeoMutt && NeoMutt->sub)
209
210 struct MenuDefinition **mdp = NULL;
211 ARRAY_FOREACH(mdp, &mod_data->menu_defs)
212 {
213 menudef_free(mdp);
214 }
215 ARRAY_FREE(&mod_data->menu_defs);
216
217 struct SubMenu **smp = NULL;
218 ARRAY_FOREACH(smp, &mod_data->sub_menus)
219 {
220 submenu_free(smp);
221 }
222 ARRAY_FREE(&mod_data->sub_menus);
223
224 ARRAY_FREE(&mod_data->macro_events);
225 ARRAY_FREE(&mod_data->unget_key_events);
226}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
void submenu_free(struct SubMenu **pptr)
Free a SubMenu.
Definition menudef.c:38
void menudef_free(struct MenuDefinition **pptr)
Free a MenuDefinition.
Definition menudef.c:66
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
struct KeyEventArray macro_events
Macro event buffer.
Definition module_data.h:36
struct KeyEventArray unget_key_events
Unget key event buffer.
Definition module_data.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ km_set_abort_key()

void km_set_abort_key ( keycode_t * abort_key)

Parse the abort_key config string.

Parameters
abort_keyPointer to the abort key variable to set

Parse the string into $abort_key and put the keycode into AbortKey.

Definition at line 234 of file init.c.

235{
236 keycode_t buf[4] = { 0 };
237 const char *const c_abort_key = cs_subset_string(NeoMutt->sub, "abort_key");
238
239 size_t len = parse_keys(c_abort_key, buf, countof(buf));
240 if (len == 0)
241 {
242 mutt_error(_("Abort key is not set, defaulting to Ctrl-G"));
243 *abort_key = ctrl('G');
244 return;
245 }
246
247 if (len > 1)
248 {
249 mutt_warning(_("Specified abort key sequence (%s) will be truncated to first key"),
250 c_abort_key);
251 }
252 *abort_key = buf[0];
253}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
#define mutt_warning(...)
Definition logging2.h:92
#define mutt_error(...)
Definition logging2.h:94
size_t parse_keys(const char *str, keycode_t *d, size_t max)
Parse a key string into key codes.
Definition keymap.c:336
short keycode_t
Type for key storage, the rest of neomutt works fine with int type.
Definition keymap.h:31
#define countof(x)
Definition memory.h:49
#define _(a)
Definition message.h:28
#define ctrl(ch)
Convert a letter to a control character (e.g., 'A' -> Ctrl-A)
Definition mutt_curses.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ KeyCommands

const struct Command KeyCommands[]
Initial value:
= {
{ "bind", CMD_BIND, parse_bind,
N_("Bind a key to a function"),
N_("bind <map>[,<map> ... ] <key> <function>"),
"configuration.html#bind" },
{ "exec", CMD_EXEC, parse_exec,
N_("Execute a function"),
N_("exec <function> [ <function> ... ]"),
"configuration.html#exec" },
{ "macro", CMD_MACRO, parse_macro,
N_("Define a keyboard macro"),
N_("macro <map>[,<map> ... ] <key> <sequence> [ <description> ]"),
"configuration.html#macro" },
{ "push", CMD_PUSH, parse_push,
N_("Push a string into NeoMutt's input queue (simulate typing)"),
N_("push <string>"),
"configuration.html#push" },
{ "unbind", CMD_UNBIND, parse_unbind,
N_("Remove a key binding"),
N_("unbind { * | <map>[,<map> ... ] } [ <key> ]"),
"configuration.html#unbind" },
{ "unmacro", CMD_UNMACRO, parse_unbind,
N_("Remove a keyboard `macro`"),
N_("unmacro { * | <map>[,<map> ... ] } [ <key> ]"),
"configuration.html#unmacro" },
{ NULL, CMD_NONE, NULL, NULL, NULL, NULL, CF_NONE },
}
@ CF_NONE
No flags are set.
Definition command.h:49
@ CMD_EXEC
:exec
Definition command.h:77
@ CMD_UNMACRO
:unmacro
Definition command.h:138
@ CMD_MACRO
:macro
Definition command.h:93
@ CMD_PUSH
:push
Definition command.h:104
@ CMD_NONE
No Command.
Definition command.h:62
@ CMD_BIND
:bind
Definition command.h:70
@ CMD_UNBIND
:unbind
Definition command.h:131
enum CommandResult parse_bind(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'bind' command - Implements Command::parse() -.
Definition commands.c:242
enum CommandResult parse_exec(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'exec' command - Implements Command::parse() -.
Definition commands.c:810
enum CommandResult parse_macro(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'macro' command - Implements Command::parse() -.
Definition commands.c:703
enum CommandResult parse_push(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'push' command - Implements Command::parse() -.
Definition commands.c:205
enum CommandResult parse_unbind(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'unbind' and 'unmacro' commands - Implements Command::parse() -.
Definition commands.c:676
#define N_(a)
Definition message.h:32

All the registered Menus - moved to KeyModuleData All the registered SubMenus - moved to KeyModuleData AbortKey - moved to KeyModuleData.

Key Binding Commands

Definition at line 49 of file init.c.

49 {
50 // clang-format off
51 { "bind", CMD_BIND, parse_bind,
52 N_("Bind a key to a function"),
53 N_("bind <map>[,<map> ... ] <key> <function>"),
54 "configuration.html#bind" },
55 { "exec", CMD_EXEC, parse_exec,
56 N_("Execute a function"),
57 N_("exec <function> [ <function> ... ]"),
58 "configuration.html#exec" },
59 { "macro", CMD_MACRO, parse_macro,
60 N_("Define a keyboard macro"),
61 N_("macro <map>[,<map> ... ] <key> <sequence> [ <description> ]"),
62 "configuration.html#macro" },
63 { "push", CMD_PUSH, parse_push,
64 N_("Push a string into NeoMutt's input queue (simulate typing)"),
65 N_("push <string>"),
66 "configuration.html#push" },
67 { "unbind", CMD_UNBIND, parse_unbind,
68 N_("Remove a key binding"),
69 N_("unbind { * | <map>[,<map> ... ] } [ <key> ]"),
70 "configuration.html#unbind" },
71 { "unmacro", CMD_UNMACRO, parse_unbind,
72 N_("Remove a keyboard `macro`"),
73 N_("unmacro { * | <map>[,<map> ... ] } [ <key> ]"),
74 "configuration.html#unmacro" },
75
76 { NULL, CMD_NONE, NULL, NULL, NULL, NULL, CF_NONE },
77 // clang-format on
78};