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"
31 #include <sys/types.h>
39 #ifdef HAVE_LANGINFO_YESEXPR
43 /* not possible to unget more than one char under some curses libs, and it
44 * is impossible to unget function keys in SLang, so roll our own input
47 size_t UngetCount = 0;
48 static size_t UngetBufLen = 0;
49 static event_t *KeyEvent;
51 void mutt_refresh (void)
53 /* don't refresh when we are waiting for a child. */
54 if (option (OPTKEEPQUIET))
57 /* don't refresh in the middle of macros unless necessary */
58 if (UngetCount && !option (OPTFORCEREFRESH))
65 /* Make sure that the next refresh does a full refresh. This could be
66 optmized by not doing it at all if DISPLAY is set as this might
67 indicate that a GUI based pinentry was used. Having an option to
68 customize this is of course the Mutt way. */
69 void mutt_need_hard_redraw (void)
71 if (!getenv ("DISPLAY")) {
72 keypad (stdscr, TRUE);
73 clearok (stdscr, TRUE);
74 set_option (OPTNEEDREDRAW);
78 event_t mutt_getch (void)
81 event_t err = { -1, OP_NULL }, ret;
83 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
84 return (KeyEvent[--UngetCount]);
88 mutt_allow_interrupt (1);
90 /* ncurses 4.2 sends this when the screen is resized */
92 while (ch == KEY_RESIZE)
93 #endif /* KEY_RESIZE */
95 mutt_allow_interrupt (0);
103 if ((ch & 0x80) && option (OPTMETAKEY)) {
104 /* send ALT-x as ESC-x */
106 mutt_ungetch (ch, 0);
114 return (ch == ctrl ('G') ? err : ret);
117 int _mutt_get_field ( /* const */ char *field, char *buf, size_t buflen,
118 int complete, int multiple, char ***files, int *numfiles)
123 ENTER_STATE *es = mutt_new_enter_state ();
126 CLEARLINE (LINES - 1);
129 getyx (stdscr, y, x);
131 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
135 CLEARLINE (LINES - 1);
136 mutt_free_enter_state (&es);
141 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
145 set_option (OPTUNBUFFEREDINPUT);
146 rc = mutt_get_field (msg, buf, buflen, flags);
147 unset_option (OPTUNBUFFEREDINPUT);
152 void mutt_clear_error (void)
155 if (!option (OPTNOCURSES))
156 CLEARLINE (LINES - 1);
159 static void fix_end_of_file (const char *data)
164 if ((fp = safe_fopen (data, "a+")) == NULL)
166 fseek (fp, -1, SEEK_END);
167 if ((c = fgetc (fp)) != '\n')
172 void mutt_edit_file (const char *editor, const char *data)
174 char cmd[LONG_STRING];
177 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
178 if (mutt_system (cmd) == -1)
179 mutt_error (_("Error running \"%s\"!"), cmd);
180 fix_end_of_file (data);
181 keypad (stdscr, TRUE);
182 clearok (stdscr, TRUE);
185 int mutt_yesorno (const char *msg, int def)
188 char *yes = _("yes");
191 size_t answer_string_len;
193 #ifdef HAVE_LANGINFO_YESEXPR
203 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
204 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
205 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
206 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
209 CLEARLINE (LINES - 1);
212 * In order to prevent the default answer to the question to wrapped
213 * around the screen in the even the question is wider than the screen,
214 * ensure there is enough room for the answer and truncate the question
217 answer_string = mem_malloc (COLS + 1);
218 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
219 def == M_YES ? no : yes);
220 answer_string_len = str_len (answer_string);
221 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
222 mem_free (&answer_string);
227 if (CI_is_return (ch.ch))
234 #ifdef HAVE_LANGINFO_YESEXPR
236 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
240 (tolower (ch.ch) == *yes)) {
245 #ifdef HAVE_LANGINFO_YESEXPR
246 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
248 (tolower (ch.ch) == *no)) {
257 #ifdef HAVE_LANGINFO_YESEXPR
265 addstr ((char *) (def == M_YES ? yes : no));
271 /* this function is called when the user presses the abort key */
272 void mutt_query_exit (void)
277 timeout (-1); /* restore blocking operation */
278 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
287 void mutt_curses_error (const char *fmt, ...)
289 char TmpErrorbuf[STRING];
293 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
296 debug_print (1, ("%s\n", Errorbuf));
297 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
298 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
299 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
301 if (!option (OPTKEEPQUIET)) {
303 SETCOLOR (MT_COLOR_ERROR);
304 mvaddstr (LINES - 1, 0, Errorbuf);
306 SETCOLOR (MT_COLOR_NORMAL);
310 set_option (OPTMSGERR);
314 void mutt_progress_bar (progress_t* progress, long pos) {
315 char posstr[SHORT_STRING];
319 mutt_message (progress->msg);
322 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
331 if (pos >= progress->pos + (NetInc << 10)) {
333 pos = pos / (NetInc << 10) * (NetInc << 10);
334 mutt_pretty_size (posstr, sizeof (posstr), pos);
336 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
338 mutt_message ("%s %s", progress->msg, posstr);
343 void mutt_curses_message (const char *fmt, ...)
345 char TmpErrorbuf[STRING];
349 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
352 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
353 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
354 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
356 if (!option (OPTKEEPQUIET)) {
357 SETCOLOR (MT_COLOR_MESSAGE);
358 mvaddstr (LINES - 1, 0, Errorbuf);
360 SETCOLOR (MT_COLOR_NORMAL);
364 unset_option (OPTMSGERR);
367 void mutt_show_error (void)
369 if (option (OPTKEEPQUIET))
372 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
373 CLEARLINE (LINES - 1);
375 SETCOLOR (MT_COLOR_NORMAL);
378 void mutt_endwin (const char *msg)
380 if (!option (OPTNOCURSES)) {
381 CLEARLINE (LINES - 1);
394 void _mutt_perror (const char *s, const char* filename, int line)
396 char *p = strerror (errno);
398 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
399 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
402 int mutt_any_key_to_continue (const char *s)
408 f = open ("/dev/tty", O_RDONLY);
410 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
411 t.c_lflag &= ~(ICANON | ECHO);
414 tcsetattr (f, TCSADRAIN, &t);
419 fputs (_("Press any key to continue..."), stdout);
423 tcsetattr (f, TCSADRAIN, &old);
425 fputs ("\r\n", stdout);
430 int mutt_do_pager (const char *banner,
431 const char *tempfile, int do_color, pager_t * info)
435 if (!Pager || str_cmp (Pager, "builtin") == 0)
436 rc = mutt_pager (banner, tempfile, do_color, info);
441 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
442 if (mutt_system (cmd) == -1) {
443 mutt_error (_("Error running \"%s\"!"), cmd);
448 mutt_unlink (tempfile);
454 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
455 int *redraw, int buffy, int multiple, char ***files,
460 mvaddstr (LINES - 1, 0, (char *) prompt);
461 addstr (_(" ('?' for list): "));
469 CLEARLINE (LINES - 1);
472 else if (ch.ch == '?') {
475 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
477 *redraw = REDRAW_FULL;
480 char *pc = mem_malloc (str_len (prompt) + 3);
482 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
483 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
485 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
489 MAYBE_REDRAW (*redraw);
496 void mutt_ungetch (int ch, int op)
503 if (UngetCount >= UngetBufLen)
504 mem_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof (event_t));
506 KeyEvent[UngetCount++] = tmp;
509 void mutt_flushinp (void)
515 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
516 /* The argument can take 3 values:
517 * -1: restore the value of the last call
518 * 0: make the cursor invisible
519 * 1: make the cursor visible
521 void mutt_curs_set (int cursor)
523 static int SavedCursor = 1;
526 cursor = SavedCursor;
528 SavedCursor = cursor;
530 if (curs_set (cursor) == ERR) {
531 if (cursor == 1) /* cnorm */
532 curs_set (2); /* cvvis */
537 int mutt_multi_choice (char *prompt, char *letters)
543 mvaddstr (LINES - 1, 0, prompt);
548 if (ch.ch == -1 || CI_is_return (ch.ch)) {
553 p = strchr (letters, ch.ch);
555 choice = p - letters + 1;
558 else if (ch.ch <= '9' && ch.ch > '0') {
559 choice = ch.ch - '0';
560 if (choice <= str_len (letters))
566 CLEARLINE (LINES - 1);
572 * addwch would be provided by an up-to-date curses library
575 int mutt_addwch (wchar_t wc)
577 char buf[MB_LEN_MAX * 2];
581 memset (&mbstate, 0, sizeof (mbstate));
582 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
583 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
591 * This formats a string, a bit like
592 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
593 * except that the widths refer to the number of character cells
597 void mutt_format_string (char *dest, size_t destlen,
598 int min_width, int max_width,
599 int right_justify, char m_pad_char,
600 const char *s, size_t n, int arboreal)
606 char scratch[MB_LEN_MAX];
607 mbstate_t mbstate1, mbstate2;
609 memset (&mbstate1, 0, sizeof (mbstate1));
610 memset (&mbstate2, 0, sizeof (mbstate2));
613 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
614 if (k == (size_t) (-1) || k == (size_t) (-2)) {
615 k = (k == (size_t) (-1)) ? 1 : n;
616 wc = replacement_char ();
618 if (arboreal && wc < M_TREE_MAX)
626 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
630 strncpy (p, scratch, k2);
635 w = (int) destlen < min_width ? destlen : min_width;
638 else if (right_justify) {
643 dest[w] = m_pad_char;
653 * This formats a string rather like
654 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
655 * snprintf (dest, destlen, fmt, s);
656 * except that the numbers in the conversion specification refer to
657 * the number of character cells when printed.
660 static void mutt_format_s_x (char *dest,
662 const char *prefix, const char *s, int arboreal)
664 int right_justify = 1;
667 int max_width = INT_MAX;
670 ++prefix, right_justify = 0;
671 min_width = strtol (prefix, &p, 10);
674 max_width = strtol (prefix, &p, 10);
679 mutt_format_string (dest, destlen, min_width, max_width,
680 right_justify, ' ', s, str_len (s), arboreal);
683 void mutt_format_s (char *dest,
684 size_t destlen, const char *prefix, const char *s)
686 mutt_format_s_x (dest, destlen, prefix, s, 0);
689 void mutt_format_s_tree (char *dest,
690 size_t destlen, const char *prefix, const char *s)
692 mutt_format_s_x (dest, destlen, prefix, s, 1);
696 * mutt_paddstr (n, s) is almost equivalent to
697 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
700 void mutt_paddstr (int n, const char *s)
705 size_t len = str_len (s);
708 memset (&mbstate, 0, sizeof (mbstate));
709 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
710 if (k == (size_t) (-1) || k == (size_t) (-2)) {
711 k = (k == (size_t) (-1)) ? 1 : len;
712 wc = replacement_char ();
720 addnstr ((char *) s, k);
729 * mutt_strwidth is like str_len except that it returns the width
730 * refering to the number of characters cells.
733 int mutt_strwidth (const char *s)
745 memset (&mbstate, 0, sizeof (mbstate));
746 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
747 if (k == (size_t) (-1) || k == (size_t) (-2)) {
748 k = (k == (size_t) (-1)) ? 1 : n;
749 wc = replacement_char ();