2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 2004 g10 Code GmbH
6 * Parts were written/modified by:
7 * Nico Golde <nico@ngolde.de>
9 * This file is part of mutt-ng, see http://www.muttng.org/.
10 * It's licensed under the GNU General Public License,
11 * please see the file GPL in the top level source directory.
19 #include "mutt_menu.h"
20 #include "mutt_curses.h"
25 #include <sys/types.h>
33 #ifdef HAVE_LANGINFO_YESEXPR
37 /* not possible to unget more than one char under some curses libs, and it
38 * is impossible to unget function keys in SLang, so roll our own input
41 size_t UngetCount = 0;
42 static size_t UngetBufLen = 0;
43 static event_t *KeyEvent;
45 void mutt_refresh (void)
47 /* don't refresh when we are waiting for a child. */
48 if (option (OPTKEEPQUIET))
51 /* don't refresh in the middle of macros unless necessary */
52 if (UngetCount && !option (OPTFORCEREFRESH))
59 /* Make sure that the next refresh does a full refresh. This could be
60 optmized by not doing it at all if DISPLAY is set as this might
61 indicate that a GUI based pinentry was used. Having an option to
62 customize this is of course the Mutt way. */
63 void mutt_need_hard_redraw (void)
65 if (!getenv ("DISPLAY")) {
66 keypad (stdscr, TRUE);
67 clearok (stdscr, TRUE);
68 set_option (OPTNEEDREDRAW);
72 event_t mutt_getch (void)
75 event_t err = { -1, OP_NULL }, ret;
77 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
78 return (KeyEvent[--UngetCount]);
82 mutt_allow_interrupt (1);
84 /* ncurses 4.2 sends this when the screen is resized */
86 while (ch == KEY_RESIZE)
87 #endif /* KEY_RESIZE */
89 mutt_allow_interrupt (0);
97 if ((ch & 0x80) && option (OPTMETAKEY)) {
98 /* send ALT-x as ESC-x */
100 mutt_ungetch (ch, 0);
108 return (ch == ctrl ('G') ? err : ret);
111 int _mutt_get_field ( /* const */ char *field, char *buf, size_t buflen,
112 int complete, int multiple, char ***files, int *numfiles)
117 ENTER_STATE *es = mutt_new_enter_state ();
120 CLEARLINE (LINES - 1);
123 getyx (stdscr, y, x);
125 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
129 CLEARLINE (LINES - 1);
130 mutt_free_enter_state (&es);
135 int mutt_get_password (char *msg, char *buf, size_t buflen)
139 CLEARLINE (LINES - 1);
141 set_option (OPTUNBUFFEREDINPUT);
142 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
143 unset_option (OPTUNBUFFEREDINPUT);
144 CLEARLINE (LINES - 1);
148 void mutt_clear_error (void)
151 if (!option (OPTNOCURSES))
152 CLEARLINE (LINES - 1);
155 static void fix_end_of_file (const char *data)
160 if ((fp = safe_fopen (data, "a+")) == NULL)
162 fseek (fp, -1, SEEK_END);
163 if ((c = fgetc (fp)) != '\n')
168 void mutt_edit_file (const char *editor, const char *data)
170 char cmd[LONG_STRING];
173 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
174 if (mutt_system (cmd) == -1)
175 mutt_error (_("Error running \"%s\"!"), cmd);
176 fix_end_of_file (data);
177 keypad (stdscr, TRUE);
178 clearok (stdscr, TRUE);
181 int mutt_yesorno (const char *msg, int def)
184 char *yes = _("yes");
187 size_t answer_string_len;
189 #ifdef HAVE_LANGINFO_YESEXPR
199 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
200 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
201 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
202 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
205 CLEARLINE (LINES - 1);
208 * In order to prevent the default answer to the question to wrapped
209 * around the screen in the even the question is wider than the screen,
210 * ensure there is enough room for the answer and truncate the question
213 answer_string = safe_malloc (COLS + 1);
214 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
215 def == M_YES ? no : yes);
216 answer_string_len = mutt_strlen (answer_string);
217 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
218 FREE (&answer_string);
223 if (CI_is_return (ch.ch))
230 #ifdef HAVE_LANGINFO_YESEXPR
232 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
236 (tolower (ch.ch) == 'y')) {
241 #ifdef HAVE_LANGINFO_YESEXPR
242 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
244 (tolower (ch.ch) == 'n')) {
253 #ifdef HAVE_LANGINFO_YESEXPR
261 addstr ((char *) (def == M_YES ? yes : no));
267 /* this function is called when the user presses the abort key */
268 void mutt_query_exit (void)
273 timeout (-1); /* restore blocking operation */
274 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
283 void mutt_curses_error (const char *fmt, ...)
285 char TmpErrorbuf[STRING];
289 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
292 dprint (1, (debugfile, "%s\n", Errorbuf));
293 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
294 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
295 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
297 if (!option (OPTKEEPQUIET)) {
299 SETCOLOR (MT_COLOR_ERROR);
300 mvaddstr (LINES - 1, 0, Errorbuf);
302 SETCOLOR (MT_COLOR_NORMAL);
306 set_option (OPTMSGERR);
309 void mutt_curses_message (const char *fmt, ...)
311 char TmpErrorbuf[STRING];
315 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
318 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
319 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
320 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
322 if (!option (OPTKEEPQUIET)) {
323 SETCOLOR (MT_COLOR_MESSAGE);
324 mvaddstr (LINES - 1, 0, Errorbuf);
326 SETCOLOR (MT_COLOR_NORMAL);
330 unset_option (OPTMSGERR);
333 void mutt_show_error (void)
335 if (option (OPTKEEPQUIET))
338 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
339 CLEARLINE (LINES - 1);
341 SETCOLOR (MT_COLOR_NORMAL);
344 void mutt_endwin (const char *msg)
346 if (!option (OPTNOCURSES)) {
347 CLEARLINE (LINES - 1);
360 void mutt_perror (const char *s)
362 char *p = strerror (errno);
364 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
365 p ? p : "unknown error", errno));
366 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
369 int mutt_any_key_to_continue (const char *s)
375 f = open ("/dev/tty", O_RDONLY);
377 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
378 t.c_lflag &= ~(ICANON | ECHO);
381 tcsetattr (f, TCSADRAIN, &t);
386 fputs (_("Press any key to continue..."), stdout);
390 tcsetattr (f, TCSADRAIN, &old);
392 fputs ("\r\n", stdout);
397 int mutt_do_pager (const char *banner,
398 const char *tempfile, int do_color, pager_t * info)
402 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
403 rc = mutt_pager (banner, tempfile, do_color, info);
408 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
409 if (mutt_system (cmd) == -1) {
410 mutt_error (_("Error running \"%s\"!"), cmd);
415 mutt_unlink (tempfile);
421 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
422 int *redraw, int buffy, int multiple, char ***files,
427 mvaddstr (LINES - 1, 0, (char *) prompt);
428 addstr (_(" ('?' for list): "));
436 CLEARLINE (LINES - 1);
439 else if (ch.ch == '?') {
442 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
444 *redraw = REDRAW_FULL;
447 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
449 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
450 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
452 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
456 MAYBE_REDRAW (*redraw);
463 void mutt_ungetch (int ch, int op)
470 if (UngetCount >= UngetBufLen)
471 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof (event_t));
473 KeyEvent[UngetCount++] = tmp;
476 void mutt_flushinp (void)
482 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
483 /* The argument can take 3 values:
484 * -1: restore the value of the last call
485 * 0: make the cursor invisible
486 * 1: make the cursor visible
488 void mutt_curs_set (int cursor)
490 static int SavedCursor = 1;
493 cursor = SavedCursor;
495 SavedCursor = cursor;
497 if (curs_set (cursor) == ERR) {
498 if (cursor == 1) /* cnorm */
499 curs_set (2); /* cvvis */
504 int mutt_multi_choice (char *prompt, char *letters)
510 mvaddstr (LINES - 1, 0, prompt);
515 if (ch.ch == -1 || CI_is_return (ch.ch)) {
520 p = strchr (letters, ch.ch);
522 choice = p - letters + 1;
525 else if (ch.ch <= '9' && ch.ch > '0') {
526 choice = ch.ch - '0';
527 if (choice <= mutt_strlen (letters))
533 CLEARLINE (LINES - 1);
539 * addwch would be provided by an up-to-date curses library
542 int mutt_addwch (wchar_t wc)
544 char buf[MB_LEN_MAX * 2];
548 memset (&mbstate, 0, sizeof (mbstate));
549 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
550 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
558 * This formats a string, a bit like
559 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
560 * except that the widths refer to the number of character cells
564 void mutt_format_string (char *dest, size_t destlen,
565 int min_width, int max_width,
566 int right_justify, char m_pad_char,
567 const char *s, size_t n, int arboreal)
573 char scratch[MB_LEN_MAX];
574 mbstate_t mbstate1, mbstate2;
576 memset (&mbstate1, 0, sizeof (mbstate1));
577 memset (&mbstate2, 0, sizeof (mbstate2));
580 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
581 if (k == (size_t) (-1) || k == (size_t) (-2)) {
582 k = (k == (size_t) (-1)) ? 1 : n;
583 wc = replacement_char ();
585 if (arboreal && wc < M_TREE_MAX)
593 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
597 strncpy (p, scratch, k2);
602 w = (int) destlen < min_width ? destlen : min_width;
605 else if (right_justify) {
610 dest[w] = m_pad_char;
620 * This formats a string rather like
621 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
622 * snprintf (dest, destlen, fmt, s);
623 * except that the numbers in the conversion specification refer to
624 * the number of character cells when printed.
627 static void mutt_format_s_x (char *dest,
629 const char *prefix, const char *s, int arboreal)
631 int right_justify = 1;
634 int max_width = INT_MAX;
637 ++prefix, right_justify = 0;
638 min_width = strtol (prefix, &p, 10);
641 max_width = strtol (prefix, &p, 10);
646 mutt_format_string (dest, destlen, min_width, max_width,
647 right_justify, ' ', s, mutt_strlen (s), arboreal);
650 void mutt_format_s (char *dest,
651 size_t destlen, const char *prefix, const char *s)
653 mutt_format_s_x (dest, destlen, prefix, s, 0);
656 void mutt_format_s_tree (char *dest,
657 size_t destlen, const char *prefix, const char *s)
659 mutt_format_s_x (dest, destlen, prefix, s, 1);
663 * mutt_paddstr (n, s) is almost equivalent to
664 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
667 void mutt_paddstr (int n, const char *s)
672 size_t len = mutt_strlen (s);
675 memset (&mbstate, 0, sizeof (mbstate));
676 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
677 if (k == (size_t) (-1) || k == (size_t) (-2)) {
678 k = (k == (size_t) (-1)) ? 1 : len;
679 wc = replacement_char ();
687 addnstr ((char *) s, k);
696 * mutt_strwidth is like mutt_strlen except that it returns the width
697 * refering to the number of characters cells.
700 int mutt_strwidth (const char *s)
712 memset (&mbstate, 0, sizeof (mbstate));
713 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
714 if (k == (size_t) (-1) || k == (size_t) (-2)) {
715 k = (k == (size_t) (-1)) ? 1 : n;
716 wc = replacement_char ();