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.
20 #include "mutt_menu.h"
21 #include "mutt_curses.h"
28 #include "lib/debug.h"
32 #include <sys/types.h>
40 #ifdef HAVE_LANGINFO_YESEXPR
44 /* not possible to unget more than one char under some curses libs, and it
45 * is impossible to unget function keys in SLang, so roll our own input
48 size_t UngetCount = 0;
49 static size_t UngetBufLen = 0;
50 static event_t *KeyEvent;
52 void mutt_refresh (void)
54 /* don't refresh when we are waiting for a child. */
55 if (option (OPTKEEPQUIET))
58 /* don't refresh in the middle of macros unless necessary */
59 if (UngetCount && !option (OPTFORCEREFRESH))
66 /* Make sure that the next refresh does a full refresh. This could be
67 optmized by not doing it at all if DISPLAY is set as this might
68 indicate that a GUI based pinentry was used. Having an option to
69 customize this is of course the Mutt way. */
70 void mutt_need_hard_redraw (void)
72 if (!getenv ("DISPLAY")) {
73 keypad (stdscr, TRUE);
74 clearok (stdscr, TRUE);
75 set_option (OPTNEEDREDRAW);
79 event_t mutt_getch (void)
82 event_t err = { -1, OP_NULL }, ret;
84 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
85 return (KeyEvent[--UngetCount]);
89 mutt_allow_interrupt (1);
91 /* ncurses 4.2 sends this when the screen is resized */
93 while (ch == KEY_RESIZE)
94 #endif /* KEY_RESIZE */
96 mutt_allow_interrupt (0);
104 if ((ch & 0x80) && option (OPTMETAKEY)) {
105 /* send ALT-x as ESC-x */
107 mutt_ungetch (ch, 0);
115 return (ch == ctrl ('G') ? err : ret);
118 int _mutt_get_field ( const char *field, char *buf, size_t buflen,
119 int complete, int multiple, char ***files, int *numfiles)
124 ENTER_STATE *es = mutt_new_enter_state ();
127 CLEARLINE (LINES - 1);
130 getyx (stdscr, y, x);
132 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
136 CLEARLINE (LINES - 1);
137 mutt_free_enter_state (&es);
142 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
146 set_option (OPTUNBUFFEREDINPUT);
147 rc = mutt_get_field (msg, buf, buflen, flags);
148 unset_option (OPTUNBUFFEREDINPUT);
153 void mutt_clear_error (void)
156 if (!option (OPTNOCURSES))
157 CLEARLINE (LINES - 1);
160 void mutt_edit_file (const char *editor, const char *data)
162 char cmd[LONG_STRING];
165 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
166 if (mutt_system (cmd) == -1)
167 mutt_error (_("Error running \"%s\"!"), cmd);
168 keypad (stdscr, TRUE);
169 clearok (stdscr, TRUE);
172 int mutt_yesorno (const char *msg, int def)
175 char *yes = _("yes");
178 size_t answer_string_len;
180 #ifdef HAVE_LANGINFO_YESEXPR
190 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
191 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
192 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
193 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
196 CLEARLINE (LINES - 1);
199 * In order to prevent the default answer to the question to wrapped
200 * around the screen in the even the question is wider than the screen,
201 * ensure there is enough room for the answer and truncate the question
204 answer_string = mem_malloc (COLS + 1);
205 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
206 def == M_YES ? no : yes);
207 answer_string_len = str_len (answer_string);
208 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
209 mem_free (&answer_string);
214 if (CI_is_return (ch.ch))
221 #ifdef HAVE_LANGINFO_YESEXPR
223 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
227 (tolower (ch.ch) == *yes)) {
232 #ifdef HAVE_LANGINFO_YESEXPR
233 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
235 (tolower (ch.ch) == *no)) {
244 #ifdef HAVE_LANGINFO_YESEXPR
252 addstr ((char *) (def == M_YES ? yes : no));
258 /* this function is called when the user presses the abort key */
259 void mutt_query_exit (void)
264 timeout (-1); /* restore blocking operation */
265 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
274 void mutt_curses_error (const char *fmt, ...)
276 char TmpErrorbuf[STRING];
280 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
283 debug_print (1, ("%s\n", Errorbuf));
284 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
285 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
286 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
288 if (!option (OPTKEEPQUIET)) {
290 SETCOLOR (MT_COLOR_ERROR);
291 mvaddstr (LINES - 1, 0, Errorbuf);
293 SETCOLOR (MT_COLOR_NORMAL);
297 set_option (OPTMSGERR);
301 void mutt_progress_bar (progress_t* progress, long pos) {
302 char posstr[SHORT_STRING];
306 mutt_message (progress->msg);
309 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
318 if (pos >= progress->pos + (NetInc << 10)) {
320 pos = pos / (NetInc << 10) * (NetInc << 10);
321 mutt_pretty_size (posstr, sizeof (posstr), pos);
323 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
325 mutt_message ("%s %s", progress->msg, posstr);
330 void mutt_curses_message (const char *fmt, ...)
332 char TmpErrorbuf[STRING];
336 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
339 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
340 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
341 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
343 if (!option (OPTKEEPQUIET)) {
344 SETCOLOR (MT_COLOR_MESSAGE);
345 mvaddstr (LINES - 1, 0, Errorbuf);
347 SETCOLOR (MT_COLOR_NORMAL);
351 unset_option (OPTMSGERR);
354 void mutt_show_error (void)
356 if (option (OPTKEEPQUIET))
359 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
360 CLEARLINE (LINES - 1);
362 SETCOLOR (MT_COLOR_NORMAL);
365 void mutt_endwin (const char *msg)
367 if (!option (OPTNOCURSES)) {
368 CLEARLINE (LINES - 1);
381 void _mutt_perror (const char *s, const char* filename, int line)
383 char *p = strerror (errno);
385 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
386 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
389 int mutt_any_key_to_continue (const char *s)
395 f = open ("/dev/tty", O_RDONLY);
397 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
398 t.c_lflag &= ~(ICANON | ECHO);
401 tcsetattr (f, TCSADRAIN, &t);
406 fputs (_("Press any key to continue..."), stdout);
410 tcsetattr (f, TCSADRAIN, &old);
412 fputs ("\r\n", stdout);
417 int mutt_do_pager (const char *banner,
418 const char *tempfile, int do_color, pager_t * info)
422 if (!Pager || str_cmp (Pager, "builtin") == 0)
423 rc = mutt_pager (banner, tempfile, do_color, info);
428 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
429 if (mutt_system (cmd) == -1) {
430 mutt_error (_("Error running \"%s\"!"), cmd);
435 mutt_unlink (tempfile);
441 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
442 int *redraw, int buffy, int multiple, char ***files,
447 mvaddstr (LINES - 1, 0, (char *) prompt);
448 addstr (_(" ('?' for list): "));
456 CLEARLINE (LINES - 1);
459 else if (ch.ch == '?') {
462 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
464 *redraw = REDRAW_FULL;
467 char *pc = mem_malloc (str_len (prompt) + 3);
469 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
470 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
472 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
476 MAYBE_REDRAW (*redraw);
483 void mutt_ungetch (int ch, int op)
490 if (UngetCount >= UngetBufLen)
491 mem_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof (event_t));
493 KeyEvent[UngetCount++] = tmp;
496 void mutt_flushinp (void)
502 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
503 /* The argument can take 3 values:
504 * -1: restore the value of the last call
505 * 0: make the cursor invisible
506 * 1: make the cursor visible
508 void mutt_curs_set (int cursor)
510 static int SavedCursor = 1;
513 cursor = SavedCursor;
515 SavedCursor = cursor;
517 if (curs_set (cursor) == ERR) {
518 if (cursor == 1) /* cnorm */
519 curs_set (2); /* cvvis */
524 int mutt_multi_choice (char *prompt, char *letters)
530 mvaddstr (LINES - 1, 0, prompt);
535 if (ch.ch == -1 || CI_is_return (ch.ch)) {
540 p = strchr (letters, ch.ch);
542 choice = p - letters + 1;
545 else if (ch.ch <= '9' && ch.ch > '0') {
546 choice = ch.ch - '0';
547 if (choice <= str_len (letters))
553 CLEARLINE (LINES - 1);
559 * addwch would be provided by an up-to-date curses library
562 int mutt_addwch (wchar_t wc)
564 char buf[MB_LEN_MAX * 2];
568 memset (&mbstate, 0, sizeof (mbstate));
569 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
570 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
578 * This formats a string, a bit like
579 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
580 * except that the widths refer to the number of character cells
584 void mutt_format_string (char *dest, size_t destlen,
585 int min_width, int max_width,
586 int right_justify, char m_pad_char,
587 const char *s, size_t n, int arboreal)
593 char scratch[MB_LEN_MAX];
594 mbstate_t mbstate1, mbstate2;
596 memset (&mbstate1, 0, sizeof (mbstate1));
597 memset (&mbstate2, 0, sizeof (mbstate2));
600 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
601 if (k == (size_t) (-1) || k == (size_t) (-2)) {
602 k = (k == (size_t) (-1)) ? 1 : n;
603 wc = replacement_char ();
605 if (arboreal && wc < M_TREE_MAX)
613 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
617 strncpy (p, scratch, k2);
622 w = (int) destlen < min_width ? destlen : min_width;
625 else if (right_justify) {
630 dest[w] = m_pad_char;
640 * This formats a string rather like
641 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
642 * snprintf (dest, destlen, fmt, s);
643 * except that the numbers in the conversion specification refer to
644 * the number of character cells when printed.
647 static void mutt_format_s_x (char *dest,
649 const char *prefix, const char *s, int arboreal)
651 int right_justify = 1;
654 int max_width = INT_MAX;
657 ++prefix, right_justify = 0;
658 min_width = strtol (prefix, &p, 10);
661 max_width = strtol (prefix, &p, 10);
666 mutt_format_string (dest, destlen, min_width, max_width,
667 right_justify, ' ', s, str_len (s), arboreal);
670 void mutt_format_s (char *dest,
671 size_t destlen, const char *prefix, const char *s)
673 mutt_format_s_x (dest, destlen, prefix, s, 0);
676 void mutt_format_s_tree (char *dest,
677 size_t destlen, const char *prefix, const char *s)
679 mutt_format_s_x (dest, destlen, prefix, s, 1);
683 * mutt_paddstr (n, s) is almost equivalent to
684 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
687 void mutt_paddstr (int n, const char *s)
692 size_t len = str_len (s);
695 memset (&mbstate, 0, sizeof (mbstate));
696 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
697 if (k == (size_t) (-1) || k == (size_t) (-2)) {
698 k = (k == (size_t) (-1)) ? 1 : len;
699 wc = replacement_char ();
707 addnstr ((char *) s, k);
716 * mutt_strwidth is like str_len except that it returns the width
717 * refering to the number of characters cells.
720 int mutt_strwidth (const char *s)
732 memset (&mbstate, 0, sizeof (mbstate));
733 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
734 if (k == (size_t) (-1) || k == (size_t) (-2)) {
735 k = (k == (size_t) (-1)) ? 1 : n;
736 wc = replacement_char ();