NeoMutt  2025-01-09-41-g086358
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
curs_lib.c
Go to the documentation of this file.
1
33#include "config.h"
34#include <errno.h>
35#include <fcntl.h>
36#include <limits.h>
37#include <stdbool.h>
38#include <stdio.h>
39#include <string.h>
40#include <termios.h>
41#include <unistd.h>
42#include <wchar.h>
43#include "mutt/lib.h"
44#include "config/lib.h"
45#include "core/lib.h"
46#include "mutt.h"
47#include "curs_lib.h"
48#include "browser/lib.h"
49#include "color/lib.h"
50#include "editor/lib.h"
51#include "history/lib.h"
52#include "key/lib.h"
53#include "question/lib.h"
54#include "globals.h"
55#include "msgcont.h"
56#include "msgwin.h"
57#include "mutt_curses.h"
58#include "mutt_logging.h"
59#include "mutt_thread.h"
60#include "mutt_window.h"
61#include "opcodes.h"
62#include "protos.h"
63
68void mutt_beep(bool force)
69{
70 const bool c_beep = cs_subset_bool(NeoMutt->sub, "beep");
71 if (force || c_beep)
72 beep();
73}
74
78void mutt_refresh(void)
79{
80 /* don't refresh when we are waiting for a child. */
81 if (OptKeepQuiet)
82 return;
83
84 /* don't refresh in the middle of macros unless necessary */
86 return;
87
88 /* else */
89 refresh();
90}
91
101{
102 // Forcibly switch to the alternate screen.
103 // Using encryption can leave ncurses confused about which mode it's in.
104 fputs("\033[?1049h", stdout);
105 fflush(stdout);
106 keypad(stdscr, true);
107 clearok(stdscr, true);
108 window_redraw(NULL);
109}
110
116void mutt_edit_file(const char *editor, const char *file)
117{
118 struct Buffer *cmd = buf_pool_get();
119
120 mutt_endwin();
121 buf_file_expand_fmt_quote(cmd, editor, file);
122 if (mutt_system(buf_string(cmd)) != 0)
123 {
124 mutt_error(_("Error running \"%s\""), buf_string(cmd));
125 }
126 /* the terminal may have been resized while the editor owned it */
128
129 buf_pool_release(&cmd);
130}
131
138{
140 if (query_yesorno(_("Exit NeoMutt without saving?"), MUTT_YES) == MUTT_YES)
141 {
142 mutt_exit(0); /* This call never returns */
143 }
145 SigInt = false;
146}
147
151void mutt_endwin(void)
152{
153 if (OptNoCurses)
154 return;
155
156 int e = errno;
157
158 /* at least in some situations (screen + xterm under SuSE11/12) endwin()
159 * doesn't properly flush the screen without an explicit call. */
160 mutt_refresh();
161 endwin();
162 SigWinch = true;
163
164 errno = e;
165}
166
173int mutt_any_key_to_continue(const char *s)
174{
175 struct termios term = { 0 };
176 struct termios old = { 0 };
177
178 int fd = open("/dev/tty", O_RDONLY);
179 if (fd < 0)
180 return EOF;
181
182 tcgetattr(fd, &old); // Save the current tty settings
183
184 term = old;
185 term.c_lflag &= ~(ICANON | ECHO); // Canonical (not line-buffered), don't echo the characters
186 term.c_cc[VMIN] = 1; // Wait for at least one character
187 term.c_cc[VTIME] = 255; // Wait for 25.5s
188 tcsetattr(fd, TCSANOW, &term);
189
190 if (s)
191 fputs(s, stdout);
192 else
193 fputs(_("Press any key to continue..."), stdout);
194 fflush(stdout);
195
196 char ch = '\0';
197 // Wait for a character. This might timeout, so loop.
198 while (read(fd, &ch, 1) == 0)
199 ; // do nothing
200
201 // Change the tty settings to be non-blocking
202 term.c_cc[VMIN] = 0; // Returning with zero characters is acceptable
203 term.c_cc[VTIME] = 0; // Don't wait
204 tcsetattr(fd, TCSANOW, &term);
205
206 char buf[64] = { 0 };
207 while (read(fd, buf, sizeof(buf)) > 0)
208 ; // Mop up any remaining chars
209
210 tcsetattr(fd, TCSANOW, &old); // Restore the previous tty settings
211 close(fd);
212
213 fputs("\r\n", stdout);
215 return (ch >= 0) ? ch : EOF;
216}
217
236int mw_enter_fname(const char *prompt, struct Buffer *fname, bool mailbox,
237 struct Mailbox *m, bool multiple, char ***files,
238 int *numfiles, SelectFileFlags flags)
239{
240 struct MuttWindow *win = msgwin_new(true);
241 if (!win)
242 return -1;
243
244 int rc = -1;
245
246 struct Buffer *text = buf_pool_get();
247 const struct AttrColor *ac_normal = simple_color_get(MT_COLOR_NORMAL);
248 const struct AttrColor *ac_prompt = merged_color_overlay(ac_normal,
250
251 msgwin_add_text(win, prompt, ac_prompt);
252 msgwin_add_text(win, _(" ('?' for list): "), ac_prompt);
253 if (!buf_is_empty(fname))
254 msgwin_add_text(win, buf_string(fname), ac_normal);
255
257 struct MuttWindow *old_focus = window_set_focus(win);
258
259 struct KeyEvent event = { 0, OP_NULL };
260 do
261 {
262 window_redraw(NULL);
263 event = mutt_getch(GETCH_NO_FLAGS);
264 } while ((event.op == OP_TIMEOUT) || (event.op == OP_REPAINT));
265
266 mutt_refresh();
267 win = msgcont_pop_window();
268 window_set_focus(old_focus);
269 mutt_window_free(&win);
270
271 if (event.ch < 0)
272 goto done;
273
274 if (event.ch == '?')
275 {
276 buf_reset(fname);
277
278 if (flags == MUTT_SEL_NO_FLAGS)
279 flags = MUTT_SEL_FOLDER;
280 if (multiple)
281 flags |= MUTT_SEL_MULTI;
282 if (mailbox)
283 flags |= MUTT_SEL_MAILBOX;
284 dlg_browser(fname, flags, m, files, numfiles);
285 }
286 else
287 {
288 char *pc = NULL;
289 mutt_str_asprintf(&pc, "%s: ", prompt);
290 if (event.op == OP_NULL)
291 mutt_unget_ch(event.ch);
292 else
293 mutt_unget_op(event.op);
294
295 buf_alloc(fname, 1024);
296 struct FileCompletionData cdata = { multiple, m, files, numfiles };
297 enum HistoryClass hclass = mailbox ? HC_MAILBOX : HC_FILE;
298 if (mw_get_field(pc, fname, MUTT_COMP_CLEAR, hclass, &CompleteMailboxOps, &cdata) != 0)
299 {
300 buf_reset(fname);
301 }
302 FREE(&pc);
303 }
304
305 rc = 0;
306
307done:
308 buf_pool_release(&text);
309 return rc;
310}
311
319int mutt_addwch(struct MuttWindow *win, wchar_t wc)
320{
321 char buf[MB_LEN_MAX * 2];
322 mbstate_t mbstate = { 0 };
323 size_t n1, n2;
324
325 if (((n1 = wcrtomb(buf, wc, &mbstate)) == ICONV_ILLEGAL_SEQ) ||
326 ((n2 = wcrtomb(buf + n1, 0, &mbstate)) == ICONV_ILLEGAL_SEQ))
327 {
328 return -1; /* ERR */
329 }
330 else
331 {
332 return mutt_window_addstr(win, buf);
333 }
334}
335
342void mutt_paddstr(struct MuttWindow *win, int n, const char *s)
343{
344 wchar_t wc = 0;
345 size_t k;
346 size_t len = mutt_str_len(s);
347 mbstate_t mbstate = { 0 };
348
349 for (; len && (k = mbrtowc(&wc, s, len, &mbstate)); s += k, len -= k)
350 {
351 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
352 {
353 if (k == ICONV_ILLEGAL_SEQ)
354 memset(&mbstate, 0, sizeof(mbstate));
355 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : len;
356 wc = ReplacementChar;
357 }
358 if (!IsWPrint(wc))
359 wc = '?';
360 const int w = wcwidth(wc);
361 if (w >= 0)
362 {
363 if (w > n)
364 break;
365 mutt_addwch(win, wc);
366 n -= w;
367 }
368 }
369 while (n-- > 0)
370 mutt_window_addch(win, ' ');
371}
372
384size_t mutt_wstr_trunc(const char *src, size_t maxlen, size_t maxwid, size_t *width)
385{
386 wchar_t wc = 0;
387 size_t n, w = 0, l = 0, cl;
388 int cw;
389 mbstate_t mbstate = { 0 };
390
391 if (!src)
392 goto out;
393
394 n = mutt_str_len(src);
395
396 for (w = 0; n && (cl = mbrtowc(&wc, src, n, &mbstate)); src += cl, n -= cl)
397 {
398 if (cl == ICONV_ILLEGAL_SEQ)
399 {
400 memset(&mbstate, 0, sizeof(mbstate));
401 cl = 1;
402 wc = ReplacementChar;
403 }
404 else if (cl == ICONV_BUF_TOO_SMALL)
405 {
406 cl = n;
407 wc = ReplacementChar;
408 }
409
410 cw = wcwidth(wc);
411 /* hack because MUTT_TREE symbols aren't turned into characters
412 * until rendered by print_enriched_string() */
413 if ((cw < 0) && (src[0] == MUTT_SPECIAL_INDEX))
414 {
415 cl = 2; /* skip the index coloring sequence */
416 cw = 0;
417 }
418 else if ((cw < 0) && (cl == 1) && (src[0] != '\0') && (src[0] < MUTT_TREE_MAX))
419 {
420 cw = 1;
421 }
422 else if (cw < 0)
423 {
424 cw = 0; /* unprintable wchar */
425 }
426 if (wc == '\n')
427 break;
428 if (((cl + l) > maxlen) || ((cw + w) > maxwid))
429 break;
430 l += cl;
431 w += cw;
432 }
433out:
434 if (width)
435 *width = w;
436 return l;
437}
438
444size_t mutt_strwidth(const char *s)
445{
446 if (!s)
447 return 0;
448 return mutt_strnwidth(s, mutt_str_len(s));
449}
450
457size_t mutt_strnwidth(const char *s, size_t n)
458{
459 if (!s)
460 return 0;
461
462 wchar_t wc = 0;
463 int w;
464 size_t k;
465 mbstate_t mbstate = { 0 };
466
467 for (w = 0; n && (k = mbrtowc(&wc, s, n, &mbstate)); s += k, n -= k)
468 {
469 if (*s == MUTT_SPECIAL_INDEX)
470 {
471 s += 2; /* skip the index coloring sequence */
472 k = 0;
473 continue;
474 }
475
476 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
477 {
478 if (k == ICONV_ILLEGAL_SEQ)
479 memset(&mbstate, 0, sizeof(mbstate));
480 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : n;
481 wc = ReplacementChar;
482 }
483 if (!IsWPrint(wc))
484 wc = '?';
485 w += wcwidth(wc);
486 }
487 return w;
488}
489
500void mw_what_key(void)
501{
502 struct MuttWindow *win = msgwin_new(true);
503 if (!win)
504 return;
505
506 struct Buffer *key = buf_pool_get();
507 struct Buffer *prompt = buf_pool_get();
508 struct Buffer *text = buf_pool_get();
509
510 km_keyname(AbortKey, key);
511
512 buf_printf(prompt, _("Enter keys (%s to abort): "), buf_string(key));
514
516 struct MuttWindow *old_focus = window_set_focus(win);
517 window_redraw(win);
518
519 const struct AttrColor *ac_normal = simple_color_get(MT_COLOR_NORMAL);
520 const struct AttrColor *ac_prompt = simple_color_get(MT_COLOR_PROMPT);
521
522 // ---------------------------------------------------------------------------
523 // Event Loop
524 timeout(1000); // 1 second
525 while (true)
526 {
527 int ch = getch();
528 if (ch == AbortKey)
529 break;
530
531 if (ch == KEY_RESIZE)
532 {
533 timeout(0);
534 while ((ch = getch()) == KEY_RESIZE)
535 {
536 // do nothing
537 }
538 }
539
540 if (ch == ERR)
541 {
542 if (!isatty(0)) // terminal was lost
543 mutt_exit(1);
544
545 if (SigWinch)
546 {
547 SigWinch = false;
549 window_redraw(NULL);
550 }
551 else
552 {
554 }
555
556 continue;
557 }
558
560
561 buf_reset(key);
562 km_keyname(ch, key);
563
564 buf_printf(text, _("Char = %s, Octal = %o, Decimal = %d\n"), buf_string(key), ch, ch);
565
566 msgwin_add_text(win, buf_string(text), ac_normal);
567 msgwin_add_text(win, buf_string(prompt), ac_prompt);
568 msgwin_add_text(win, NULL, NULL);
569 window_redraw(NULL);
570 }
571 // ---------------------------------------------------------------------------
572
573 buf_pool_release(&key);
574 buf_pool_release(&prompt);
575 buf_pool_release(&text);
576
577 win = msgcont_pop_window();
578 window_set_focus(old_focus);
579 mutt_window_free(&win);
580}
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition: array.h:74
const struct CompleteOps CompleteMailboxOps
Auto-Completion of Files / Mailboxes.
Definition: complete.c:160
Select a Mailbox from a list.
#define MUTT_SEL_MAILBOX
Select a mailbox.
Definition: lib.h:58
#define MUTT_SEL_FOLDER
Select a local directory.
Definition: lib.h:60
#define MUTT_SEL_MULTI
Multi-selection is enabled.
Definition: lib.h:59
#define MUTT_SEL_NO_FLAGS
No flags are set.
Definition: lib.h:57
uint8_t SelectFileFlags
Flags for mutt_select_file(), e.g. MUTT_SEL_MAILBOX.
Definition: lib.h:56
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:337
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
Color and attribute parsing.
struct AttrColor * simple_color_get(enum ColorId cid)
Get the colour of an object by its ID.
Definition: simple.c:95
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:55
@ MT_COLOR_PROMPT
Question/user input.
Definition: color.h:58
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_edit_file(const char *editor, const char *file)
Let the user edit a file.
Definition: curs_lib.c:116
size_t mutt_strnwidth(const char *s, size_t n)
Measure a string's width in screen cells.
Definition: curs_lib.c:457
size_t mutt_wstr_trunc(const char *src, size_t maxlen, size_t maxwid, size_t *width)
Work out how to truncate a widechar string.
Definition: curs_lib.c:384
void mutt_paddstr(struct MuttWindow *win, int n, const char *s)
Display a string on screen, padded if necessary.
Definition: curs_lib.c:342
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:78
int mutt_addwch(struct MuttWindow *win, wchar_t wc)
Addwch would be provided by an up-to-date curses library.
Definition: curs_lib.c:319
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:173
void mutt_need_hard_redraw(void)
Force a hard refresh.
Definition: curs_lib.c:100
void mutt_query_exit(void)
Ask the user if they want to leave NeoMutt.
Definition: curs_lib.c:137
void mutt_endwin(void)
Shutdown curses.
Definition: curs_lib.c:151
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:68
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition: curs_lib.c:444
GUI miscellaneous curses (window drawing) routines.
void mutt_flushinp(void)
Empty all the keyboard buffers.
Definition: get.c:58
struct KeyEvent mutt_getch(GetChFlags flags)
Read a character from the input buffer.
Definition: get.c:210
void mutt_unget_op(int op)
Return an operation to the input buffer.
Definition: get.c:126
void mutt_unget_ch(int ch)
Return a keystroke to the input buffer.
Definition: get.c:115
Edit a string.
void buf_file_expand_fmt_quote(struct Buffer *dest, const char *fmt, const char *src)
Replace s in a string with a filename.
Definition: file.c:1349
struct KeyEventArray MacroEvents
These are used for macros and exec/push commands.
Definition: get.c:49
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: globals.c:63
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:69
bool OptForceRefresh
(pseudo) refresh even during macros
Definition: globals.c:62
void dlg_browser(struct Buffer *file, SelectFileFlags flags, struct Mailbox *m, char ***files, int *numfiles)
Let the user select a file -.
Definition: dlg_browser.c:853
void mw_what_key(void)
Display the value of a key -.
Definition: curs_lib.c:500
int mw_enter_fname(const char *prompt, struct Buffer *fname, bool mailbox, struct Mailbox *m, bool multiple, char ***files, int *numfiles, SelectFileFlags flags)
Ask the user to select a file -.
Definition: curs_lib.c:236
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition: window.c:273
#define mutt_error(...)
Definition: logging2.h:92
Read/write command history from/to a file.
HistoryClass
Type to differentiate different histories.
Definition: lib.h:52
@ HC_FILE
Files.
Definition: lib.h:56
@ HC_MAILBOX
Mailboxes.
Definition: lib.h:59
keycode_t AbortKey
code of key to abort prompts, normally Ctrl-G
Definition: lib.c:121
void km_keyname(int c, struct Buffer *buf)
Get the human name for a key.
Definition: lib.c:407
Manage keymappings.
#define GETCH_NO_FLAGS
No flags are set.
Definition: lib.h:52
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:266
#define IsWPrint(wc)
Definition: mbyte.h:41
#define FREE(x)
Definition: memory.h:55
const struct AttrColor * merged_color_overlay(const struct AttrColor *base, const struct AttrColor *over)
Combine two colours.
Definition: merged.c:107
void msgcont_push_window(struct MuttWindow *win)
Add a window to the Container Stack.
Definition: msgcont.c:93
struct MuttWindow * msgcont_pop_window(void)
Remove the last Window from the Container Stack.
Definition: msgcont.c:57
Message Window.
struct MuttWindow * msgwin_new(bool interactive)
Create the Message Window.
Definition: msgwin.c:371
void msgwin_clear_text(struct MuttWindow *win)
Clear the text in the Message Window.
Definition: msgwin.c:519
void msgwin_add_text(struct MuttWindow *win, const char *text, const struct AttrColor *ac_color)
Add text to the Message Window.
Definition: msgwin.c:419
void msgwin_set_text(struct MuttWindow *win, const char *text, enum ColorId color)
Set the text for the Message Window.
Definition: msgwin.c:484
Message Window.
wchar_t ReplacementChar
When a Unicode character can't be displayed, use this instead.
Definition: charset.c:61
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition: charset.h:98
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition: charset.h:96
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
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
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:803
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
Many unsorted constants and some structs.
#define MUTT_COMP_CLEAR
Clear input if printable character is pressed.
Definition: mutt.h:57
Define wrapper functions around Curses.
void mutt_resize_screen(void)
Update NeoMutt's opinion about the window size.
Definition: resize.c:76
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
NeoMutt Logging.
Create/manipulate threading in emails.
@ MUTT_TREE_MAX
Definition: mutt_thread.h:71
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition: mutt_thread.h:73
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:596
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
Definition: mutt_window.c:202
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:646
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
Definition: mutt_window.c:378
int mutt_window_addch(struct MuttWindow *win, int ch)
Write one character to a Window.
Definition: mutt_window.c:350
Window management.
@ NT_TIMEOUT
Timeout has occurred.
Definition: notify_type.h:56
@ NT_RESIZE
Window has been resized.
Definition: notify_type.h:52
All user-callable functions.
#define OP_TIMEOUT
1 second with no events
Definition: opcodes.h:36
#define OP_REPAINT
Repaint is needed.
Definition: opcodes.h:34
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
Prototypes for many functions.
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:52
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
Ask the user a question.
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:327
volatile sig_atomic_t SigWinch
true after SIGWINCH is received
Definition: signal.c:67
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition: signal.c:66
int endwin(void)
A curses colour and its attributes.
Definition: attr.h:66
String manipulation buffer.
Definition: buffer.h:36
Input for the file completion function.
Definition: curs_lib.h:40
char *** files
List of files selected.
Definition: curs_lib.h:43
struct Mailbox * mailbox
Mailbox.
Definition: curs_lib.h:42
bool multiple
Allow multiple selections.
Definition: curs_lib.h:41
int * numfiles
Number of files selected.
Definition: curs_lib.h:44
An event such as a keypress.
Definition: lib.h:82
int op
Function opcode, e.g. OP_HELP.
Definition: lib.h:84
int ch
Raw key pressed.
Definition: lib.h:83
A mailbox.
Definition: mailbox.h:79
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct Notify * notify_timeout
Timeout notifications handler.
Definition: neomutt.h:45
struct Notify * notify_resize
Window resize notifications handler.
Definition: neomutt.h:44
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46