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"
27 #include "lib/debug.h"
30 #include <sys/types.h>
38 #ifdef HAVE_LANGINFO_YESEXPR
42 /* not possible to unget more than one char under some curses libs, and it
43 * is impossible to unget function keys in SLang, so roll our own input
46 size_t UngetCount = 0;
47 static size_t UngetBufLen = 0;
48 static event_t *KeyEvent;
50 void mutt_refresh (void)
52 /* don't refresh when we are waiting for a child. */
53 if (option (OPTKEEPQUIET))
56 /* don't refresh in the middle of macros unless necessary */
57 if (UngetCount && !option (OPTFORCEREFRESH))
64 /* Make sure that the next refresh does a full refresh. This could be
65 optmized by not doing it at all if DISPLAY is set as this might
66 indicate that a GUI based pinentry was used. Having an option to
67 customize this is of course the Mutt way. */
68 void mutt_need_hard_redraw (void)
70 if (!getenv ("DISPLAY")) {
71 keypad (stdscr, TRUE);
72 clearok (stdscr, TRUE);
73 set_option (OPTNEEDREDRAW);
77 event_t mutt_getch (void)
80 event_t err = { -1, OP_NULL }, ret;
82 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
83 return (KeyEvent[--UngetCount]);
87 mutt_allow_interrupt (1);
89 /* ncurses 4.2 sends this when the screen is resized */
91 while (ch == KEY_RESIZE)
92 #endif /* KEY_RESIZE */
94 mutt_allow_interrupt (0);
102 if ((ch & 0x80) && option (OPTMETAKEY)) {
103 /* send ALT-x as ESC-x */
105 mutt_ungetch (ch, 0);
113 return (ch == ctrl ('G') ? err : ret);
116 int _mutt_get_field ( /* const */ char *field, char *buf, size_t buflen,
117 int complete, int multiple, char ***files, int *numfiles)
122 ENTER_STATE *es = mutt_new_enter_state ();
125 CLEARLINE (LINES - 1);
128 getyx (stdscr, y, x);
130 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
134 CLEARLINE (LINES - 1);
135 mutt_free_enter_state (&es);
140 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
144 set_option (OPTUNBUFFEREDINPUT);
145 rc = mutt_get_field (msg, buf, buflen, flags);
146 unset_option (OPTUNBUFFEREDINPUT);
151 void mutt_clear_error (void)
154 if (!option (OPTNOCURSES))
155 CLEARLINE (LINES - 1);
158 static void fix_end_of_file (const char *data)
163 if ((fp = safe_fopen (data, "a+")) == NULL)
165 fseek (fp, -1, SEEK_END);
166 if ((c = fgetc (fp)) != '\n')
171 void mutt_edit_file (const char *editor, const char *data)
173 char cmd[LONG_STRING];
176 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
177 if (mutt_system (cmd) == -1)
178 mutt_error (_("Error running \"%s\"!"), cmd);
179 fix_end_of_file (data);
180 keypad (stdscr, TRUE);
181 clearok (stdscr, TRUE);
184 int mutt_yesorno (const char *msg, int def)
187 char *yes = _("yes");
190 size_t answer_string_len;
192 #ifdef HAVE_LANGINFO_YESEXPR
202 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
203 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
204 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
205 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
208 CLEARLINE (LINES - 1);
211 * In order to prevent the default answer to the question to wrapped
212 * around the screen in the even the question is wider than the screen,
213 * ensure there is enough room for the answer and truncate the question
216 answer_string = mem_malloc (COLS + 1);
217 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
218 def == M_YES ? no : yes);
219 answer_string_len = str_len (answer_string);
220 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
221 mem_free (&answer_string);
226 if (CI_is_return (ch.ch))
233 #ifdef HAVE_LANGINFO_YESEXPR
235 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
239 (tolower (ch.ch) == *yes)) {
244 #ifdef HAVE_LANGINFO_YESEXPR
245 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
247 (tolower (ch.ch) == *no)) {
256 #ifdef HAVE_LANGINFO_YESEXPR
264 addstr ((char *) (def == M_YES ? yes : no));
270 /* this function is called when the user presses the abort key */
271 void mutt_query_exit (void)
276 timeout (-1); /* restore blocking operation */
277 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
286 void mutt_curses_error (const char *fmt, ...)
288 char TmpErrorbuf[STRING];
292 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
295 debug_print (1, ("%s\n", Errorbuf));
296 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
297 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
298 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
300 if (!option (OPTKEEPQUIET)) {
302 SETCOLOR (MT_COLOR_ERROR);
303 mvaddstr (LINES - 1, 0, Errorbuf);
305 SETCOLOR (MT_COLOR_NORMAL);
309 set_option (OPTMSGERR);
312 void mutt_curses_message (const char *fmt, ...)
314 char TmpErrorbuf[STRING];
318 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
321 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
322 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
323 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
325 if (!option (OPTKEEPQUIET)) {
326 SETCOLOR (MT_COLOR_MESSAGE);
327 mvaddstr (LINES - 1, 0, Errorbuf);
329 SETCOLOR (MT_COLOR_NORMAL);
333 unset_option (OPTMSGERR);
336 void mutt_show_error (void)
338 if (option (OPTKEEPQUIET))
341 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
342 CLEARLINE (LINES - 1);
344 SETCOLOR (MT_COLOR_NORMAL);
347 void mutt_endwin (const char *msg)
349 if (!option (OPTNOCURSES)) {
350 CLEARLINE (LINES - 1);
363 void _mutt_perror (const char *s, const char* filename, int line)
365 char *p = strerror (errno);
367 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
368 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
371 int mutt_any_key_to_continue (const char *s)
377 f = open ("/dev/tty", O_RDONLY);
379 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
380 t.c_lflag &= ~(ICANON | ECHO);
383 tcsetattr (f, TCSADRAIN, &t);
388 fputs (_("Press any key to continue..."), stdout);
392 tcsetattr (f, TCSADRAIN, &old);
394 fputs ("\r\n", stdout);
399 int mutt_do_pager (const char *banner,
400 const char *tempfile, int do_color, pager_t * info)
404 if (!Pager || str_cmp (Pager, "builtin") == 0)
405 rc = mutt_pager (banner, tempfile, do_color, info);
410 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
411 if (mutt_system (cmd) == -1) {
412 mutt_error (_("Error running \"%s\"!"), cmd);
417 mutt_unlink (tempfile);
423 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
424 int *redraw, int buffy, int multiple, char ***files,
429 mvaddstr (LINES - 1, 0, (char *) prompt);
430 addstr (_(" ('?' for list): "));
438 CLEARLINE (LINES - 1);
441 else if (ch.ch == '?') {
444 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
446 *redraw = REDRAW_FULL;
449 char *pc = mem_malloc (str_len (prompt) + 3);
451 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
452 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
454 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
458 MAYBE_REDRAW (*redraw);
465 void mutt_ungetch (int ch, int op)
472 if (UngetCount >= UngetBufLen)
473 mem_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof (event_t));
475 KeyEvent[UngetCount++] = tmp;
478 void mutt_flushinp (void)
484 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
485 /* The argument can take 3 values:
486 * -1: restore the value of the last call
487 * 0: make the cursor invisible
488 * 1: make the cursor visible
490 void mutt_curs_set (int cursor)
492 static int SavedCursor = 1;
495 cursor = SavedCursor;
497 SavedCursor = cursor;
499 if (curs_set (cursor) == ERR) {
500 if (cursor == 1) /* cnorm */
501 curs_set (2); /* cvvis */
506 int mutt_multi_choice (char *prompt, char *letters)
512 mvaddstr (LINES - 1, 0, prompt);
517 if (ch.ch == -1 || CI_is_return (ch.ch)) {
522 p = strchr (letters, ch.ch);
524 choice = p - letters + 1;
527 else if (ch.ch <= '9' && ch.ch > '0') {
528 choice = ch.ch - '0';
529 if (choice <= str_len (letters))
535 CLEARLINE (LINES - 1);
541 * addwch would be provided by an up-to-date curses library
544 int mutt_addwch (wchar_t wc)
546 char buf[MB_LEN_MAX * 2];
550 memset (&mbstate, 0, sizeof (mbstate));
551 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
552 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
560 * This formats a string, a bit like
561 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
562 * except that the widths refer to the number of character cells
566 void mutt_format_string (char *dest, size_t destlen,
567 int min_width, int max_width,
568 int right_justify, char m_pad_char,
569 const char *s, size_t n, int arboreal)
575 char scratch[MB_LEN_MAX];
576 mbstate_t mbstate1, mbstate2;
578 memset (&mbstate1, 0, sizeof (mbstate1));
579 memset (&mbstate2, 0, sizeof (mbstate2));
582 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
583 if (k == (size_t) (-1) || k == (size_t) (-2)) {
584 k = (k == (size_t) (-1)) ? 1 : n;
585 wc = replacement_char ();
587 if (arboreal && wc < M_TREE_MAX)
595 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
599 strncpy (p, scratch, k2);
604 w = (int) destlen < min_width ? destlen : min_width;
607 else if (right_justify) {
612 dest[w] = m_pad_char;
622 * This formats a string rather like
623 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
624 * snprintf (dest, destlen, fmt, s);
625 * except that the numbers in the conversion specification refer to
626 * the number of character cells when printed.
629 static void mutt_format_s_x (char *dest,
631 const char *prefix, const char *s, int arboreal)
633 int right_justify = 1;
636 int max_width = INT_MAX;
639 ++prefix, right_justify = 0;
640 min_width = strtol (prefix, &p, 10);
643 max_width = strtol (prefix, &p, 10);
648 mutt_format_string (dest, destlen, min_width, max_width,
649 right_justify, ' ', s, str_len (s), arboreal);
652 void mutt_format_s (char *dest,
653 size_t destlen, const char *prefix, const char *s)
655 mutt_format_s_x (dest, destlen, prefix, s, 0);
658 void mutt_format_s_tree (char *dest,
659 size_t destlen, const char *prefix, const char *s)
661 mutt_format_s_x (dest, destlen, prefix, s, 1);
665 * mutt_paddstr (n, s) is almost equivalent to
666 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
669 void mutt_paddstr (int n, const char *s)
674 size_t len = str_len (s);
677 memset (&mbstate, 0, sizeof (mbstate));
678 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
679 if (k == (size_t) (-1) || k == (size_t) (-2)) {
680 k = (k == (size_t) (-1)) ? 1 : len;
681 wc = replacement_char ();
689 addnstr ((char *) s, k);
698 * mutt_strwidth is like str_len except that it returns the width
699 * refering to the number of characters cells.
702 int mutt_strwidth (const char *s)
714 memset (&mbstate, 0, sizeof (mbstate));
715 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
716 if (k == (size_t) (-1) || k == (size_t) (-2)) {
717 k = (k == (size_t) (-1)) ? 1 : n;
718 wc = replacement_char ();