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 dprint (1, (debugfile, "Updating progress: %ld\n", pos));
424 if (progress->flags & M_PROGRESS_SIZE)
426 pos = pos / (progress->inc << 10) * (progress->inc << 10);
427 mutt_pretty_size (posstr, sizeof (posstr), pos);
430 snprintf (posstr, sizeof (posstr), "%ld", pos);
434 progress->timestamp = now;
436 if (progress->size > 0)
438 mutt_message ("%s %s/%s (%d%%)", progress->msg, posstr, progress->sizestr,
439 percent > 0 ? percent :
440 (int) (100.0 * (double) progress->pos / progress->size));
445 mutt_message ("%s %s (%d%%)", progress->msg, posstr, percent);
447 mutt_message ("%s %s", progress->msg, posstr);
452 if (pos >= progress->size)
456 void mutt_show_error (void)
458 if (option (OPTKEEPQUIET))
461 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
464 SETCOLOR (MT_COLOR_NORMAL);
467 void mutt_endwin (const char *msg)
471 if (!option (OPTNOCURSES))
473 CLEARLINE (LINES - 1);
489 void mutt_perror (const char *s)
491 char *p = strerror (errno);
493 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
494 p ? p : "unknown error", errno));
495 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
498 int mutt_any_key_to_continue (const char *s)
504 f = open ("/dev/tty", O_RDONLY);
506 memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
507 t.c_lflag &= ~(ICANON | ECHO);
510 tcsetattr (f, TCSADRAIN, &t);
515 fputs (_("Press any key to continue..."), stdout);
519 tcsetattr (f, TCSADRAIN, &old);
521 fputs ("\r\n", stdout);
526 int mutt_do_pager (const char *banner,
527 const char *tempfile,
533 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
534 rc = mutt_pager (banner, tempfile, do_color, info);
540 mutt_expand_file_fmt (cmd, sizeof(cmd), Pager, tempfile);
541 if (mutt_system (cmd) == -1)
543 mutt_error (_("Error running \"%s\"!"), cmd);
548 mutt_unlink (tempfile);
554 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen, int *redraw, int buffy, int multiple, char ***files, int *numfiles)
558 mvaddstr (LINES-1, 0, (char *) prompt);
559 addstr (_(" ('?' for list): "));
571 else if (ch.ch == '?')
575 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
577 *redraw = REDRAW_FULL;
581 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
583 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
584 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
585 if (_mutt_get_field (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files, numfiles)
588 MAYBE_REDRAW (*redraw);
595 void mutt_ungetch (int ch, int op)
602 if (UngetCount >= UngetBufLen)
603 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof(event_t));
605 KeyEvent[UngetCount++] = tmp;
608 void mutt_flushinp (void)
614 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
615 /* The argument can take 3 values:
616 * -1: restore the value of the last call
617 * 0: make the cursor invisible
618 * 1: make the cursor visible
620 void mutt_curs_set (int cursor)
622 static int SavedCursor = 1;
625 cursor = SavedCursor;
627 SavedCursor = cursor;
629 if (curs_set (cursor) == ERR) {
630 if (cursor == 1) /* cnorm */
631 curs_set (2); /* cvvis */
636 int mutt_multi_choice (char *prompt, char *letters)
642 mvaddstr (LINES - 1, 0, prompt);
648 if (ch.ch < 0 || CI_is_return (ch.ch))
655 p = strchr (letters, ch.ch);
658 choice = p - letters + 1;
661 else if (ch.ch <= '9' && ch.ch > '0')
663 choice = ch.ch - '0';
664 if (choice <= mutt_strlen (letters))
670 CLEARLINE (LINES - 1);
676 * addwch would be provided by an up-to-date curses library
679 int mutt_addwch (wchar_t wc)
681 char buf[MB_LEN_MAX*2];
685 memset (&mbstate, 0, sizeof (mbstate));
686 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
687 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
695 * This formats a string, a bit like
696 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
697 * except that the widths refer to the number of character cells
701 void mutt_format_string (char *dest, size_t destlen,
702 int min_width, int max_width,
703 int justify, char m_pad_char,
704 const char *s, size_t n,
711 char scratch[MB_LEN_MAX];
712 mbstate_t mbstate1, mbstate2;
714 memset(&mbstate1, 0, sizeof (mbstate1));
715 memset(&mbstate2, 0, sizeof (mbstate2));
718 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k)
720 if (k == (size_t)(-1) || k == (size_t)(-2))
722 if (k == (size_t)(-1) && errno == EILSEQ)
723 memset (&mbstate1, 0, sizeof (mbstate1));
725 k = (k == (size_t)(-1)) ? 1 : n;
726 wc = replacement_char ();
728 if (arboreal && wc < M_TREE_MAX)
738 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
742 strncpy (p, scratch, k2);
747 w = (int)destlen < min_width ? destlen : min_width;
750 else if (justify == FMT_RIGHT) /* right justify */
756 dest[w] = m_pad_char;
758 else if (justify == FMT_CENTER) /* center */
761 int half = (w+1) / 2; /* half of cushion space */
765 /* move str to center of buffer */
776 dest[half] = m_pad_char;
778 else /* left justify */
787 * This formats a string rather like
788 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
789 * snprintf (dest, destlen, fmt, s);
790 * except that the numbers in the conversion specification refer to
791 * the number of character cells when printed.
794 static void mutt_format_s_x (char *dest,
800 int justify = FMT_RIGHT;
803 int max_width = INT_MAX;
806 ++prefix, justify = FMT_LEFT;
807 else if (*prefix == '=')
808 ++prefix, justify = FMT_CENTER;
809 min_width = strtol (prefix, &p, 10);
813 max_width = strtol (prefix, &p, 10);
818 mutt_format_string (dest, destlen, min_width, max_width,
819 justify, ' ', s, mutt_strlen (s), arboreal);
822 void mutt_format_s (char *dest,
827 mutt_format_s_x (dest, destlen, prefix, s, 0);
830 void mutt_format_s_tree (char *dest,
835 mutt_format_s_x (dest, destlen, prefix, s, 1);
839 * mutt_paddstr (n, s) is almost equivalent to
840 * mutt_format_string (bigbuf, big, n, n, FMT_LEFT, ' ', s, big, 0), addstr (bigbuf)
843 void mutt_paddstr (int n, const char *s)
848 size_t len = mutt_strlen (s);
851 memset (&mbstate, 0, sizeof (mbstate));
852 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k)
854 if (k == (size_t)(-1) || k == (size_t)(-2))
856 if (k == (size_t) (-1))
857 memset (&mbstate, 0, sizeof (mbstate));
858 k = (k == (size_t)(-1)) ? 1 : len;
859 wc = replacement_char ();
868 addnstr ((char *)s, k);
876 /* See how many bytes to copy from string so its at most maxlen bytes
877 * long and maxwid columns wide */
878 int mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *width)
881 int w = 0, l = 0, cl;
888 n = mutt_strlen (src);
890 memset (&mbstate, 0, sizeof (mbstate));
891 for (w = 0; n && (cl = mbrtowc (&wc, src, n, &mbstate)); src += cl, n -= cl)
893 if (cl == (size_t)(-1) || cl == (size_t)(-2))
897 if (cl + l > maxlen || cw + w > maxwid)
909 * returns the number of bytes the first (multibyte) character
911 * < 0 ... conversion error
912 * = 0 ... end of input
913 * > 0 ... length (bytes)
915 int mutt_charlen (const char *s, int *width)
925 memset (&mbstate, 0, sizeof (mbstate));
926 k = mbrtowc (&wc, s, n, &mbstate);
928 *width = wcwidth (wc);
929 return (k == (size_t)(-1) || k == (size_t)(-2)) ? -1 : k;
933 * mutt_strwidth is like mutt_strlen except that it returns the width
934 * refering to the number of characters cells.
937 int mutt_strwidth (const char *s)
948 memset (&mbstate, 0, sizeof (mbstate));
949 for (w=0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k)
951 if (k == (size_t)(-1) || k == (size_t)(-2))
953 k = (k == (size_t)(-1)) ? 1 : n;
954 wc = replacement_char ();