NeoMutt  2025-12-11-769-g906513
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
get.c File Reference

Get a key from the user. More...

#include "config.h"
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "get.h"
#include "menu/lib.h"
#include "globals.h"
#include "keymap.h"
#include "menu.h"
#include "module_data.h"
#include "monitor.h"
+ Include dependency graph for get.c:

Go to the source code of this file.

Functions

void mutt_flushinp (void)
 MacroEvents moved to KeyModuleData UngetKeyEvents moved to KeyModuleData.
 
struct KeyEventarray_pop (struct KeyEventArray *a)
 Remove an event from the array.
 
void array_add (struct KeyEventArray *a, int ch, int op)
 Add an event to the end of the array.
 
void array_to_endcond (struct KeyEventArray *a)
 Clear the array until an OP_END_COND.
 
void mutt_unget_ch (int ch)
 Return a keystroke to the input buffer.
 
void mutt_unget_op (int op)
 Return an operation to the input buffer.
 
void mutt_push_macro_event (int ch, int op)
 Add the character/operation to the macro buffer.
 
void mutt_flush_macro_to_endcond (void)
 Drop a macro from the input buffer.
 
int mutt_monitor_getch (void)
 Get a character and poll the filesystem monitor.
 
struct KeyEvent mutt_getch (GetChFlags flags)
 Read a character from the input buffer.
 
void km_error_key (const struct MenuDefinition *md)
 Handle an unbound key sequence.
 
void generic_tokenize_push_string (char *s)
 Parse and queue a 'push' command.
 
KeyGatherFlags gather_functions (const struct MenuDefinition *md, const keycode_t *keys, int key_len, struct KeymapMatchArray *kma)
 Find functions whose keybindings match.
 
struct KeyEvent km_dokey (const struct MenuDefinition *md, GetChFlags flags)
 Determine what a keypress should do.
 

Variables

static const int MaxKeyLoop = 10
 XXX.
 

Detailed Description

Get a key from the user.

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 get.c.

Function Documentation

◆ mutt_flushinp()

void mutt_flushinp ( void )

MacroEvents moved to KeyModuleData UngetKeyEvents moved to KeyModuleData.

Empty all the keyboard buffers

Definition at line 60 of file get.c.

61{
64 ARRAY_SHRINK(&mod_data->macro_events, ARRAY_SIZE(&mod_data->macro_events));
65 flushinp();
66}
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_SHRINK(head, n)
Mark a number of slots at the end of the array as unused.
Definition array.h:174
@ MODULE_ID_KEY
ModuleKey, Key mappings
Definition module_api.h:73
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:665
Key private Module data.
Definition module_data.h:34
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
Container for Accounts, Notifications.
Definition neomutt.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ array_pop()

struct KeyEvent * array_pop ( struct KeyEventArray * a)

Remove an event from the array.

Parameters
aArray
Return values
ptrEvent

Definition at line 73 of file get.c.

74{
75 if (ARRAY_EMPTY(a))
76 {
77 return NULL;
78 }
79
80 struct KeyEvent *event = ARRAY_LAST(a);
81 ARRAY_SHRINK(a, 1);
82 return event;
83}
#define ARRAY_LAST(head)
Convenience method to get the last element.
Definition array.h:145
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition array.h:74
An event such as a keypress.
Definition get.h:50
+ Here is the caller graph for this function:

◆ array_add()

void array_add ( struct KeyEventArray * a,
int ch,
int op )

Add an event to the end of the array.

Parameters
aArray
chCharacter
opOperation, e.g. OP_DELETE

Definition at line 91 of file get.c.

92{
93 struct KeyEvent event = { ch, op };
94 ARRAY_ADD(a, event);
95}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
int op
Function opcode, e.g. OP_HELP.
Definition get.h:52
int ch
Raw key pressed.
Definition get.h:51
+ Here is the caller graph for this function:

◆ array_to_endcond()

void array_to_endcond ( struct KeyEventArray * a)

Clear the array until an OP_END_COND.

Parameters
aArray

Definition at line 101 of file get.c.

102{
103 while (!ARRAY_EMPTY(a))
104 {
105 if (array_pop(a)->op == OP_END_COND)
106 {
107 return;
108 }
109 }
110}
struct KeyEvent * array_pop(struct KeyEventArray *a)
Remove an event from the array.
Definition get.c:73
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_unget_ch()

void mutt_unget_ch ( int ch)

Return a keystroke to the input buffer.

Parameters
chKey press

This puts events into the UngetKeyEvents buffer

Definition at line 118 of file get.c.

119{
121 array_add(&mod_data->unget_key_events, ch, OP_NULL);
122}
void array_add(struct KeyEventArray *a, int ch, int op)
Add an event to the end of the array.
Definition get.c:91
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_unget_op()

void mutt_unget_op ( int op)

Return an operation to the input buffer.

Parameters
opOperation, e.g. OP_DELETE

This puts events into the UngetKeyEvents buffer

Definition at line 130 of file get.c.

131{
133 array_add(&mod_data->unget_key_events, 0, op);
134}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_push_macro_event()

void mutt_push_macro_event ( int ch,
int op )

Add the character/operation to the macro buffer.

Parameters
chCharacter to add
opOperation to add

Adds the ch/op to the macro buffer. This should be used for macros, push, and exec commands only.

Definition at line 144 of file get.c.

145{
147 array_add(&mod_data->macro_events, ch, op);
148}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_flush_macro_to_endcond()

void mutt_flush_macro_to_endcond ( void )

Drop a macro from the input buffer.

All the macro text is deleted until an OP_END_COND command, or the buffer is empty.

Definition at line 156 of file get.c.

157{
159 array_to_endcond(&mod_data->macro_events);
160}
void array_to_endcond(struct KeyEventArray *a)
Clear the array until an OP_END_COND.
Definition get.c:101
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_monitor_getch()

int mutt_monitor_getch ( void )

Get a character and poll the filesystem monitor.

Return values
numCharacter pressed
ERRTimeout

Definition at line 168 of file get.c.

169{
170 /* ncurses has its own internal buffer, so before we perform a poll,
171 * we need to make sure there isn't a character waiting */
172 timeout(0);
173 int ch = getch();
174 timeout(1000); // 1 second
175 if (ch == ERR)
176 {
177 if (mutt_monitor_poll() != 0)
178 ch = ERR;
179 else
180 ch = getch();
181 }
182 return ch;
183}
int mutt_monitor_poll(void)
Check for filesystem changes.
Definition monitor.c:401
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_getch()

struct KeyEvent mutt_getch ( GetChFlags flags)

Read a character from the input buffer.

Parameters
flagsFlags, e.g. GETCH_IGNORE_MACRO
Return values
objKeyEvent to process

The priority for reading events is:

  1. UngetKeyEvents buffer
  2. MacroEvents buffer
  3. Keyboard

This function can return:

  • Abort { 0, OP_ABORT }
  • Repaint { 0, OP_REPAINT }
  • Timeout { 0, OP_TIMEOUT }

Definition at line 201 of file get.c.

202{
203 static const struct KeyEvent event_abort = { 0, OP_ABORT };
204 static const struct KeyEvent event_repaint = { 0, OP_REPAINT };
205 static const struct KeyEvent event_timeout = { 0, OP_TIMEOUT };
206
207 if (!OptGui)
208 return event_abort;
209
211
212 struct KeyEvent *event_key = array_pop(&mod_data->unget_key_events);
213 if (event_key)
214 return *event_key;
215
216 if (!(flags & GETCH_IGNORE_MACRO))
217 {
218 event_key = array_pop(&mod_data->macro_events);
219 if (event_key)
220 return *event_key;
221 }
222
223 int ch;
224 SigInt = false;
226 timeout(1000); // 1 second
227#ifdef USE_INOTIFY
229#else
230 ch = getch();
231#endif
233
234 if (SigInt)
235 {
237 return event_abort;
238 }
239
240 if (ch == KEY_RESIZE)
241 {
242 timeout(0);
243 while ((ch = getch()) == KEY_RESIZE)
244 {
245 // do nothing
246 }
247 }
248
249 if (ch == ERR)
250 {
251 if (!isatty(STDIN_FILENO)) // terminal was lost
252 mutt_exit(1);
253
254 if (SigWinch)
255 {
256 SigWinch = false;
258 return event_repaint;
259 }
260
262 return event_timeout;
263 }
264
265 if (ch == mod_data->abort_key)
266 return event_abort;
267
268 if (ch & 0x80)
269 {
270 const bool c_meta_key = cs_subset_bool(NeoMutt->sub, "meta_key");
271 if (c_meta_key)
272 {
273 /* send ALT-x as ESC-x */
274 ch &= ~0x80;
276 return (struct KeyEvent) { '\033', OP_NULL }; // Escape
277 }
278 }
279
280 return (struct KeyEvent) { ch, OP_NULL };
281}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
void mutt_query_exit(void)
Ask the user if they want to leave NeoMutt.
Definition curs_lib.c:139
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition exit.c:41
int mutt_monitor_getch(void)
Get a character and poll the filesystem monitor.
Definition get.c:168
void mutt_unget_ch(int ch)
Return a keystroke to the input buffer.
Definition get.c:118
#define GETCH_IGNORE_MACRO
Don't use MacroEvents.
Definition get.h:35
bool OptGui
(pseudo) when the gui (and curses) are started
Definition globals.c:48
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition notify.c:173
@ NT_TIMEOUT
Timeout has occurred.
Definition notify_type.h:56
@ NT_RESIZE
Window has been resized.
Definition notify_type.h:52
#define OP_TIMEOUT
1 second with no events
Definition opcodes.h:35
#define OP_REPAINT
Repaint is needed.
Definition opcodes.h:34
#define OP_ABORT
$abort_key pressed (Ctrl-G)
Definition opcodes.h:36
volatile sig_atomic_t SigWinch
true after SIGWINCH is received
Definition signal.c:69
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition signal.c:68
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition signal.c:315
keycode_t abort_key
Key to abort prompts, normally Ctrl-G.
Definition module_data.h:38
struct Notify * notify_timeout
Timeout notifications handler.
Definition neomutt.h:47
struct Notify * notify_resize
Window resize notifications handler.
Definition neomutt.h:46
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_error_key()

void km_error_key ( const struct MenuDefinition * md)

Handle an unbound key sequence.

Parameters
mdMenu Definition

Definition at line 287 of file get.c.

288{
289 if (!md)
290 return;
291
292 struct Keymap *key = km_find_func(md, OP_HELP);
293 if (!key)
294 {
295 mutt_error(_("Key is not bound"));
296 return;
297 }
298
299 struct Buffer *buf = buf_pool_get();
300 keymap_expand_key(key, buf);
301 mutt_error(_("Key is not bound. Press '%s' for help."), buf_string(buf));
302 buf_pool_release(&buf);
303}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
#define mutt_error(...)
Definition logging2.h:94
bool keymap_expand_key(struct Keymap *km, struct Buffer *buf)
Get the key string bound to a Keymap.
Definition keymap.c:239
struct Keymap * km_find_func(const struct MenuDefinition *md, int func)
Find a function's mapping in a Menu.
Definition menu.c:157
#define _(a)
Definition message.h:28
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
A keyboard mapping.
Definition keymap.h:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generic_tokenize_push_string()

void generic_tokenize_push_string ( char * s)

Parse and queue a 'push' command.

Parameters
sString to push into the key queue

Parses s for <function> syntax and adds the whole sequence the macro buffer.

Definition at line 311 of file get.c.

312{
314 char *pp = NULL;
315 char *p = s + mutt_str_len(s) - 1;
316 size_t l;
317 int i, op = OP_NULL;
318
319 while (p >= s)
320 {
321 /* if we see something like "<PageUp>", look to see if it is a real
322 * function name and return the corresponding value */
323 if (*p == '>')
324 {
325 for (pp = p - 1; pp >= s && *pp != '<'; pp--)
326 ; // do nothing
327
328 if (pp >= s)
329 {
330 i = parse_fkey(pp);
331 if (i > 0)
332 {
333 mutt_push_macro_event(KEY_F(i), 0);
334 p = pp - 1;
335 continue;
336 }
337
338 l = p - pp + 1;
339 for (i = 0; mod_data->key_names[i].name; i++)
340 {
341 if (mutt_istrn_equal(pp, mod_data->key_names[i].name, l))
342 break;
343 }
344 if (mod_data->key_names[i].name)
345 {
346 /* found a match */
347 mutt_push_macro_event(mod_data->key_names[i].value, 0);
348 p = pp - 1;
349 continue;
350 }
351
352 /* See if it is a valid command
353 * skip the '<' and the '>' when comparing */
354 struct Buffer *buf = buf_pool_get();
355 buf_strcpy_n(buf, pp + 1, l - 2);
356
357 for (enum MenuType j = 0; j < MENU_MAX; j++)
358 {
359 op = km_get_op_menu(j, buf_string(buf));
360 if (op != OP_NULL)
361 break;
362 }
363
364 buf_pool_release(&buf);
365
366 if (op != OP_NULL)
367 {
369 p = pp - 1;
370 continue;
371 }
372 }
373 }
374 mutt_push_macro_event((unsigned char) *p--, 0); /* independent 8 bits chars */
375 }
376}
size_t buf_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition buffer.c:416
void mutt_push_macro_event(int ch, int op)
Add the character/operation to the macro buffer.
Definition get.c:144
int parse_fkey(char *str)
Parse a function key string.
Definition keymap.c:276
int km_get_op_menu(int mtype, const char *func)
Get the OpCode for a Function from a Menu.
Definition menu.c:214
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
bool mutt_istrn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings ignoring case (to a maximum), safely.
Definition string.c:457
struct Mapping * key_names
Key name lookup table.
Definition module_data.h:41
int value
Integer value.
Definition mapping.h:35
const char * name
String value.
Definition mapping.h:34
MenuType
Types of GUI selections.
Definition type.h:33
@ MENU_MAX
Definition type.h:51
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ gather_functions()

KeyGatherFlags gather_functions ( const struct MenuDefinition * md,
const keycode_t * keys,
int key_len,
struct KeymapMatchArray * kma )

Find functions whose keybindings match.

Parameters
[in]mdMenu Definition of all valid functions
[in]keysUser-entered key string to match
[in]key_lenLength of key string
[out]kmaArray for the results
Return values
flagsResults, e.g. KEY_GATHER_MATCH

Definition at line 386 of file get.c.

388{
389 if (!md || !keys || !kma)
390 return 0;
391
392 struct SubMenu **smp = NULL;
394
395 ARRAY_FOREACH(smp, &md->submenus)
396 {
397 struct Keymap *km = NULL;
398
399 STAILQ_FOREACH(km, &(*smp)->keymaps, entries)
400 {
401 bool match = true;
402
403 for (int i = 0; i < key_len; i++)
404 {
405 if (keys[i] != km->keys[i])
406 {
407 match = false;
408 break;
409 }
410 }
411
412 if (match)
413 {
415 if (km->len == key_len)
416 fmatch = KEY_GATHER_MATCH;
417 else
418 fmatch = KEY_GATHER_LONGER;
419
420 flags |= fmatch;
421
422 struct KeymapMatch kmatch = { md->id, fmatch, km };
423 ARRAY_ADD(kma, kmatch);
424 }
425 }
426 }
427
428 return flags;
429}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define KEY_GATHER_LONGER
No bindings match, but longer strings might.
Definition get.h:40
#define KEY_GATHER_NO_MATCH
No bindings match the search string.
Definition get.h:38
uint8_t KeyGatherFlags
Flags for gather_functions(), e.g. KEY_GATHER_NO_MATCH.
Definition get.h:37
#define KEY_GATHER_MATCH
Binding matches the search string.
Definition get.h:39
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
Result of Matching Keybinding.
Definition get.h:62
KeyGatherFlags flags
Flags, e.g. KEY_GATHER_MATCH.
Definition get.h:64
keycode_t * keys
Key sequence.
Definition keymap.h:49
short len
Length of key sequence (unit: sizeof (keycode_t))
Definition keymap.h:48
int id
Menu ID, e.g. MENU_ALIAS.
Definition menu.h:81
struct SubMenuPArray submenus
Parts making up the Menu.
Definition menu.h:83
Collection of related functions.
Definition menu.h:68
+ Here is the caller graph for this function:

◆ km_dokey()

struct KeyEvent km_dokey ( const struct MenuDefinition * md,
GetChFlags flags )

Determine what a keypress should do.

Parameters
mdMenu Definition
flagsFlags, e.g. GETCH_IGNORE_MACRO
Return values
ptrEvent

Definition at line 437 of file get.c.

438{
439 struct KeyEvent event = { 0, OP_NULL };
440 int pos = 0;
441 keycode_t keys[MAX_SEQ] = { 0 };
442
443 if (!md)
444 return event;
445
446 for (int n = 0; n < MaxKeyLoop; n++)
447 {
448 event = mutt_getch(flags);
449 mutt_debug(LL_DEBUG1, "KEY: \n");
450
451 // abort, timeout, repaint
452 if (event.op < OP_NULL)
453 {
454 mutt_debug(LL_DEBUG1, "KEY: getch() %s\n", opcodes_get_name(event.op));
455 return event;
456 }
457
458 mutt_debug(LL_DEBUG1, "KEY: getch() '%c'\n", isprint(event.ch) ? event.ch : '?');
459
460 keys[pos] = event.ch;
461 struct KeymapMatchArray kma = ARRAY_HEAD_INITIALIZER;
462 KeyGatherFlags kfg = gather_functions(md, keys, pos + 1, &kma);
463
464 mutt_debug(LL_DEBUG1, "KEY: flags = %x\n", kfg);
465
466 if (kfg == KEY_GATHER_NO_MATCH)
467 {
468 mutt_debug(LL_DEBUG1, "KEY: \033[1;31mFAIL1: ('%c', %s)\033[0m\n",
469 isprint(event.ch) ? event.ch : '?', opcodes_get_name(event.op));
470 return event;
471 }
472
473 if ((kfg & KEY_GATHER_MATCH) == KEY_GATHER_MATCH)
474 {
475 struct KeymapMatch *kmatch = NULL;
476
477 ARRAY_FOREACH(kmatch, &kma)
478 {
479 if (kmatch->flags == KEY_GATHER_MATCH)
480 {
481 struct Keymap *map = kmatch->keymap;
482
483 if (map->op != OP_MACRO)
484 {
485 mutt_debug(LL_DEBUG1, "KEY: \033[1;32mSUCCESS: ('%c', %s)\033[0m\n",
486 isprint(event.ch) ? event.ch : '?', opcodes_get_name(map->op));
487 ARRAY_FREE(&kma);
488 return (struct KeyEvent) { event.ch, map->op };
489 }
490
491 /* #GETCH_IGNORE_MACRO turns off processing the MacroEvents buffer
492 * in mutt_getch(). Generating new macro events during that time would
493 * result in undesired behavior once the option is turned off.
494 *
495 * Originally this returned -1, however that results in an unbuffered
496 * username or password prompt being aborted. Returning OP_NULL allows
497 * mw_get_field() to display the keybinding pressed instead.
498 *
499 * It may be unexpected for a macro's keybinding to be returned,
500 * but less so than aborting the prompt. */
501 if (flags & GETCH_IGNORE_MACRO)
502 {
503 ARRAY_FREE(&kma);
504 return (struct KeyEvent) { event.ch, OP_NULL };
505 }
506
508 pos = 0;
509 ARRAY_FREE(&kma);
510 break;
511 }
512 }
513 }
514 else
515 {
516 mutt_debug(LL_DEBUG1, "KEY: \033[1;33mLONGER: getch() '%c'\033[0m\n",
517 isprint(event.ch) ? event.ch : '?');
518 pos++;
519 }
520
521 ARRAY_FREE(&kma);
522 }
523
525 mutt_error(_("Macro loop detected"));
526 return (struct KeyEvent) { '\0', OP_ABORT };
527}
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
KeyGatherFlags gather_functions(const struct MenuDefinition *md, const keycode_t *keys, int key_len, struct KeymapMatchArray *kma)
Find functions whose keybindings match.
Definition get.c:386
void generic_tokenize_push_string(char *s)
Parse and queue a 'push' command.
Definition get.c:311
void mutt_flushinp(void)
MacroEvents moved to KeyModuleData UngetKeyEvents moved to KeyModuleData.
Definition get.c:60
static const int MaxKeyLoop
XXX.
Definition get.c:49
struct KeyEvent mutt_getch(GetChFlags flags)
Read a character from the input buffer.
Definition get.c:201
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
short keycode_t
Type for key storage, the rest of neomutt works fine with int type.
Definition keymap.h:31
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define MAX_SEQ
Maximum length of a key binding sequence used for buffer in km_bind.
Definition menu.h:32
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition opcodes.c:48
struct Keymap * keymap
Keymap defining bind or `macro.
Definition get.h:65
char * macro
Macro expansion (op == OP_MACRO)
Definition keymap.h:44
short op
Operation to perform.
Definition keymap.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ MaxKeyLoop

const int MaxKeyLoop = 10
static

XXX.

Definition at line 49 of file get.c.