2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 2004 g10 Code GmbH
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_menu.h"
26 #include "mutt_curses.h"
31 #include <sys/types.h>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
43 #ifdef HAVE_LANGINFO_YESEXPR
47 /* not possible to unget more than one char under some curses libs, and it
48 * is impossible to unget function keys in SLang, so roll our own input
51 size_t UngetCount = 0;
52 static size_t UngetBufLen = 0;
53 static event_t *KeyEvent;
55 void mutt_refresh (void)
57 /* don't refresh when we are waiting for a child. */
58 if (option (OPTKEEPQUIET))
61 /* don't refresh in the middle of macros unless necessary */
62 if (UngetCount && !option (OPTFORCEREFRESH))
69 /* Make sure that the next refresh does a full refresh. This could be
70 optmized by not doing it at all if DISPLAY is set as this might
71 indicate that a GUI based pinentry was used. Having an option to
72 customize this is of course the Mutt way. */
73 void mutt_need_hard_redraw (void)
75 keypad (stdscr, TRUE);
76 clearok (stdscr, TRUE);
77 set_option (OPTNEEDREDRAW);
80 event_t mutt_getch (void)
83 event_t err = {-1, OP_NULL }, ret;
84 event_t timeout = {-2, OP_NULL};
86 if (!option(OPTUNBUFFEREDINPUT) && UngetCount)
87 return (KeyEvent[--UngetCount]);
91 mutt_allow_interrupt (1);
93 /* ncurses 4.2 sends this when the screen is resized */
95 while (ch == KEY_RESIZE)
96 #endif /* KEY_RESIZE */
98 mutt_allow_interrupt (0);
105 /* either timeout or the terminal has been lost */
114 if ((ch & 0x80) && option (OPTMETAKEY))
116 /* send ALT-x as ESC-x */
118 mutt_ungetch (ch, 0);
126 return (ch == ctrl ('G') ? err : ret);
129 int _mutt_get_field (/* const */ char *field, char *buf, size_t buflen, int complete, int multiple, char ***files, int *numfiles)
134 ENTER_STATE *es = mutt_new_enter_state();
141 getyx (stdscr, y, x);
142 ret = _mutt_enter_string (buf, buflen, y, x, complete, multiple, files, numfiles, es);
146 mutt_free_enter_state (&es);
151 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
155 set_option (OPTUNBUFFEREDINPUT);
156 rc = mutt_get_field (msg, buf, buflen, flags);
157 unset_option (OPTUNBUFFEREDINPUT);
162 void mutt_clear_error (void)
165 if (!option(OPTNOCURSES))
169 void mutt_edit_file (const char *editor, const char *data)
171 char cmd[LONG_STRING];
174 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
175 if (mutt_system (cmd))
177 mutt_error (_("Error running \"%s\"!"), cmd);
180 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
181 /* the terminal may have been resized while the editor owned it */
182 mutt_resize_screen ();
184 keypad (stdscr, TRUE);
185 clearok (stdscr, TRUE);
188 int mutt_yesorno (const char *msg, int def)
191 char *yes = _("yes");
194 size_t answer_string_len;
196 #ifdef HAVE_LANGINFO_YESEXPR
206 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
207 !regcomp (&reyes, expr, REG_NOSUB|REG_EXTENDED);
208 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
209 !regcomp (&reno, expr, REG_NOSUB|REG_EXTENDED);
215 * In order to prevent the default answer to the question to wrapped
216 * around the screen in the even the question is wider than the screen,
217 * ensure there is enough room for the answer and truncate the question
220 answer_string = safe_malloc (COLS + 1);
221 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no, def == M_YES ? no : yes);
222 answer_string_len = strlen (answer_string);
223 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
224 FREE (&answer_string);
230 if (CI_is_return (ch.ch))
238 #ifdef HAVE_LANGINFO_YESEXPR
241 (regexec (& reyes, answer, 0, 0, 0) == 0) :
245 (tolower (ch.ch) == 'y'))
251 #ifdef HAVE_LANGINFO_YESEXPR
253 (regexec (& reno, answer, 0, 0, 0) == 0) :
255 (tolower (ch.ch) == 'n'))
266 #ifdef HAVE_LANGINFO_YESEXPR
275 addstr ((char *) (def == M_YES ? yes : no));
281 /* this function is called when the user presses the abort key */
282 void mutt_query_exit (void)
287 timeout (-1); /* restore blocking operation */
288 if (mutt_yesorno (_("Exit Mutt?"), M_YES) == M_YES)
298 void mutt_curses_error (const char *fmt, ...)
301 char scratch[LONG_STRING];
304 vsnprintf (scratch, sizeof (scratch), fmt, ap);
307 dprint (1, (debugfile, "%s\n", scratch));
308 mutt_format_string (Errorbuf, sizeof (Errorbuf),
309 0, COLS-2, FMT_LEFT, 0, scratch, sizeof (scratch), 0);
311 if (!option (OPTKEEPQUIET))
314 SETCOLOR (MT_COLOR_ERROR);
315 mvaddstr (LINES-1, 0, Errorbuf);
317 SETCOLOR (MT_COLOR_NORMAL);
321 set_option (OPTMSGERR);
324 void mutt_curses_message (const char *fmt, ...)
327 char scratch[LONG_STRING];
330 vsnprintf (scratch, sizeof (scratch), fmt, ap);
333 mutt_format_string (Errorbuf, sizeof (Errorbuf),
334 0, COLS-2, FMT_LEFT, 0, scratch, sizeof (scratch), 0);
336 if (!option (OPTKEEPQUIET))
338 SETCOLOR (MT_COLOR_MESSAGE);
339 mvaddstr (LINES - 1, 0, Errorbuf);
341 SETCOLOR (MT_COLOR_NORMAL);
345 unset_option (OPTMSGERR);
348 void mutt_progress_init (progress_t* progress, const char *msg,
349 unsigned short flags, unsigned short inc,
352 struct timeval tv = { 0, 0 };
356 if (option(OPTNOCURSES))
359 memset (progress, 0, sizeof (progress_t));
361 progress->flags = flags;
363 progress->size = size;
364 if (progress->size) {
365 if (progress->flags & M_PROGRESS_SIZE)
366 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
369 snprintf (progress->sizestr, sizeof (progress->sizestr), "%ld",
375 mutt_message ("%s (%s)", msg, progress->sizestr);
380 if (gettimeofday (&tv, NULL) < 0)
381 dprint (1, (debugfile, "gettimeofday failed: %d\n", errno));
382 /* if timestamp is 0 no time-based suppression is done */
384 progress->timestamp = ((unsigned int) tv.tv_sec * 1000)
385 + (unsigned int) (tv.tv_usec / 1000);
386 mutt_progress_update (progress, 0, 0);
389 void mutt_progress_update (progress_t* progress, long pos, int percent)
391 char posstr[SHORT_STRING];
393 struct timeval tv = { 0, 0 };
394 unsigned int now = 0;
396 if (option(OPTNOCURSES))
402 /* refresh if size > inc */
403 if (progress->flags & M_PROGRESS_SIZE &&
404 (pos >= progress->pos + (progress->inc << 10)))
406 else if (pos >= progress->pos + progress->inc)
409 /* skip refresh if not enough time has passed */
410 if (update && progress->timestamp && !gettimeofday (&tv, NULL)) {
411 now = ((unsigned int) tv.tv_sec * 1000)
412 + (unsigned int) (tv.tv_usec / 1000);
413 if (now && now - progress->timestamp < TimeInc)
417 /* always show the first update */
423 if (progress->flags & M_PROGRESS_SIZE)
425 pos = pos / (progress->inc << 10) * (progress->inc << 10);
426 mutt_pretty_size (posstr, sizeof (posstr), pos);
429 snprintf (posstr, sizeof (posstr), "%ld", pos);
431 dprint (5, (debugfile, "updating progress: %s\n", posstr));
435 progress->timestamp = now;
437 if (progress->size > 0)
439 mutt_message ("%s %s/%s (%d%%)", progress->msg, posstr, progress->sizestr,
440 percent > 0 ? percent :
441 (int) (100.0 * (double) progress->pos / progress->size));
446 mutt_message ("%s %s (%d%%)", progress->msg, posstr, percent);
448 mutt_message ("%s %s", progress->msg, posstr);
453 if (pos >= progress->size)
457 void mutt_show_error (void)
459 if (option (OPTKEEPQUIET))
462 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
465 SETCOLOR (MT_COLOR_NORMAL);
468 void mutt_endwin (const char *msg)
472 if (!option (OPTNOCURSES))
474 CLEARLINE (LINES - 1);
490 void mutt_perror (const char *s)
492 char *p = strerror (errno);
494 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
495 p ? p : "unknown error", errno));
496 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
499 int mutt_any_key_to_continue (const char *s)
505 f = open ("/dev/tty", O_RDONLY);
507 memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
508 t.c_lflag &= ~(ICANON | ECHO);
511 tcsetattr (f, TCSADRAIN, &t);
516 fputs (_("Press any key to continue..."), stdout);
520 tcsetattr (f, TCSADRAIN, &old);
522 fputs ("\r\n", stdout);
527 int mutt_do_pager (const char *banner,
528 const char *tempfile,
534 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
535 rc = mutt_pager (banner, tempfile, do_color, info);
541 mutt_expand_file_fmt (cmd, sizeof(cmd), Pager, tempfile);
542 if (mutt_system (cmd) == -1)
544 mutt_error (_("Error running \"%s\"!"), cmd);
549 mutt_unlink (tempfile);
555 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen, int *redraw, int buffy, int multiple, char ***files, int *numfiles)
559 mvaddstr (LINES-1, 0, (char *) prompt);
560 addstr (_(" ('?' for list): "));
572 else if (ch.ch == '?')
576 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
578 *redraw = REDRAW_FULL;
582 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
584 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
585 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
586 if (_mutt_get_field (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files, numfiles)
589 MAYBE_REDRAW (*redraw);
596 void mutt_ungetch (int ch, int op)
603 if (UngetCount >= UngetBufLen)
604 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof(event_t));
606 KeyEvent[UngetCount++] = tmp;
609 void mutt_flushinp (void)
615 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
616 /* The argument can take 3 values:
617 * -1: restore the value of the last call
618 * 0: make the cursor invisible
619 * 1: make the cursor visible
621 void mutt_curs_set (int cursor)
623 static int SavedCursor = 1;
626 cursor = SavedCursor;
628 SavedCursor = cursor;
630 if (curs_set (cursor) == ERR) {
631 if (cursor == 1) /* cnorm */
632 curs_set (2); /* cvvis */
637 int mutt_multi_choice (char *prompt, char *letters)
643 mvaddstr (LINES - 1, 0, prompt);
649 if (ch.ch < 0 || CI_is_return (ch.ch))
656 p = strchr (letters, ch.ch);
659 choice = p - letters + 1;
662 else if (ch.ch <= '9' && ch.ch > '0')
664 choice = ch.ch - '0';
665 if (choice <= mutt_strlen (letters))
671 CLEARLINE (LINES - 1);
677 * addwch would be provided by an up-to-date curses library
680 int mutt_addwch (wchar_t wc)
682 char buf[MB_LEN_MAX*2];
686 memset (&mbstate, 0, sizeof (mbstate));
687 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
688 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
696 * This formats a string, a bit like
697 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
698 * except that the widths refer to the number of character cells
702 void mutt_format_string (char *dest, size_t destlen,
703 int min_width, int max_width,
704 int justify, char m_pad_char,
705 const char *s, size_t n,
712 char scratch[MB_LEN_MAX];
713 mbstate_t mbstate1, mbstate2;
715 memset(&mbstate1, 0, sizeof (mbstate1));
716 memset(&mbstate2, 0, sizeof (mbstate2));
719 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k)
721 if (k == (size_t)(-1) || k == (size_t)(-2))
723 if (k == (size_t)(-1) && errno == EILSEQ)
724 memset (&mbstate1, 0, sizeof (mbstate1));
726 k = (k == (size_t)(-1)) ? 1 : n;
727 wc = replacement_char ();
729 if (arboreal && wc < M_TREE_MAX)
739 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
743 strncpy (p, scratch, k2);
748 w = (int)destlen < min_width ? destlen : min_width;
751 else if (justify == FMT_RIGHT) /* right justify */
757 dest[w] = m_pad_char;
759 else if (justify == FMT_CENTER) /* center */
762 int half = (w+1) / 2; /* half of cushion space */
766 /* move str to center of buffer */
777 dest[half] = m_pad_char;
779 else /* left justify */
788 * This formats a string rather like
789 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
790 * snprintf (dest, destlen, fmt, s);
791 * except that the numbers in the conversion specification refer to
792 * the number of character cells when printed.
795 static void mutt_format_s_x (char *dest,
801 int justify = FMT_RIGHT;
804 int max_width = INT_MAX;
807 ++prefix, justify = FMT_LEFT;
808 else if (*prefix == '=')
809 ++prefix, justify = FMT_CENTER;
810 min_width = strtol (prefix, &p, 10);
814 max_width = strtol (prefix, &p, 10);
819 mutt_format_string (dest, destlen, min_width, max_width,
820 justify, ' ', s, mutt_strlen (s), arboreal);
823 void mutt_format_s (char *dest,
828 mutt_format_s_x (dest, destlen, prefix, s, 0);
831 void mutt_format_s_tree (char *dest,
836 mutt_format_s_x (dest, destlen, prefix, s, 1);
840 * mutt_paddstr (n, s) is almost equivalent to
841 * mutt_format_string (bigbuf, big, n, n, FMT_LEFT, ' ', s, big, 0), addstr (bigbuf)
844 void mutt_paddstr (int n, const char *s)
849 size_t len = mutt_strlen (s);
852 memset (&mbstate, 0, sizeof (mbstate));
853 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k)
855 if (k == (size_t)(-1) || k == (size_t)(-2))
857 if (k == (size_t) (-1))
858 memset (&mbstate, 0, sizeof (mbstate));
859 k = (k == (size_t)(-1)) ? 1 : len;
860 wc = replacement_char ();
869 addnstr ((char *)s, k);
877 /* See how many bytes to copy from string so its at most maxlen bytes
878 * long and maxwid columns wide */
879 int mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *width)
882 int w = 0, l = 0, cl;
889 n = mutt_strlen (src);
891 memset (&mbstate, 0, sizeof (mbstate));
892 for (w = 0; n && (cl = mbrtowc (&wc, src, n, &mbstate)); src += cl, n -= cl)
894 if (cl == (size_t)(-1) || cl == (size_t)(-2))
898 if (cl + l > maxlen || cw + w > maxwid)
910 * returns the number of bytes the first (multibyte) character
912 * < 0 ... conversion error
913 * = 0 ... end of input
914 * > 0 ... length (bytes)
916 int mutt_charlen (const char *s, int *width)
926 memset (&mbstate, 0, sizeof (mbstate));
927 k = mbrtowc (&wc, s, n, &mbstate);
929 *width = wcwidth (wc);
930 return (k == (size_t)(-1) || k == (size_t)(-2)) ? -1 : k;
934 * mutt_strwidth is like mutt_strlen except that it returns the width
935 * refering to the number of characters cells.
938 int mutt_strwidth (const char *s)
949 memset (&mbstate, 0, sizeof (mbstate));
950 for (w=0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k)
952 if (k == (size_t)(-1) || k == (size_t)(-2))
954 k = (k == (size_t)(-1)) ? 1 : n;
955 wc = replacement_char ();