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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 #include "mutt_menu.h"
26 #include "mutt_curses.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"))
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))
106 /* send ALT-x as ESC-x */
108 mutt_ungetch (ch, 0);
116 return (ch == ctrl ('G') ? err : ret);
119 int _mutt_get_field (/* const */ char *field, char *buf, size_t buflen, int complete, int multiple, char ***files, int *numfiles)
124 ENTER_STATE *es = mutt_new_enter_state();
131 getyx (stdscr, y, x);
132 ret = _mutt_enter_string (buf, buflen, y, x, complete, multiple, files, numfiles, es);
136 mutt_free_enter_state (&es);
141 int mutt_get_password (char *msg, char *buf, size_t buflen)
147 set_option (OPTUNBUFFEREDINPUT);
148 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
149 unset_option (OPTUNBUFFEREDINPUT);
154 void mutt_clear_error (void)
157 if (!option(OPTNOCURSES))
161 static void fix_end_of_file (const char *data)
166 if ((fp = safe_fopen (data, "a+")) == NULL)
168 fseek (fp,-1,SEEK_END);
169 if ((c = fgetc(fp)) != '\n')
174 void mutt_edit_file (const char *editor, const char *data)
176 char cmd[LONG_STRING];
179 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
180 if (mutt_system (cmd) == -1)
181 mutt_error (_("Error running \"%s\"!"), cmd);
182 fix_end_of_file (data);
183 keypad (stdscr, TRUE);
184 clearok (stdscr, TRUE);
187 int mutt_yesorno (const char *msg, int def)
190 char *yes = _("yes");
193 size_t answer_string_len;
195 #ifdef HAVE_LANGINFO_YESEXPR
205 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
206 !regcomp (&reyes, expr, REG_NOSUB|REG_EXTENDED);
207 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
208 !regcomp (&reno, expr, REG_NOSUB|REG_EXTENDED);
214 * In order to prevent the default answer to the question to wrapped
215 * around the screen in the even the question is wider than the screen,
216 * ensure there is enough room for the answer and truncate the question
219 answer_string = safe_malloc (COLS + 1);
220 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no, def == M_YES ? no : yes);
221 answer_string_len = strlen (answer_string);
222 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
223 FREE (&answer_string);
229 if (CI_is_return (ch.ch))
237 #ifdef HAVE_LANGINFO_YESEXPR
240 (regexec (& reyes, answer, 0, 0, 0) == 0) :
244 (tolower (ch.ch) == 'y'))
250 #ifdef HAVE_LANGINFO_YESEXPR
252 (regexec (& reno, answer, 0, 0, 0) == 0) :
254 (tolower (ch.ch) == 'n'))
265 #ifdef HAVE_LANGINFO_YESEXPR
274 addstr ((char *) (def == M_YES ? yes : no));
280 /* this function is called when the user presses the abort key */
281 void mutt_query_exit (void)
286 timeout (-1); /* restore blocking operation */
287 if (mutt_yesorno (_("Exit Mutt?"), M_YES) == M_YES)
297 void mutt_curses_error (const char *fmt, ...)
299 char TmpErrorbuf[STRING];
303 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
306 dprint (1, (debugfile, "%s\n", Errorbuf));
307 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
308 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
309 snprintf(Errorbuf,sizeof(Errorbuf),"%s",TmpErrorbuf); /* overkill */
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, ...)
326 char TmpErrorbuf[STRING];
330 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
333 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
334 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
335 snprintf(Errorbuf,sizeof(Errorbuf),"%s",TmpErrorbuf); /* overkill */
337 if (!option (OPTKEEPQUIET))
339 SETCOLOR (MT_COLOR_MESSAGE);
340 mvaddstr (LINES - 1, 0, Errorbuf);
342 SETCOLOR (MT_COLOR_NORMAL);
346 unset_option (OPTMSGERR);
349 void mutt_show_error (void)
351 if (option (OPTKEEPQUIET))
354 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
357 SETCOLOR (MT_COLOR_NORMAL);
360 void mutt_endwin (const char *msg)
362 if (!option (OPTNOCURSES))
364 CLEARLINE (LINES - 1);
378 void mutt_perror (const char *s)
380 char *p = strerror (errno);
382 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
383 p ? p : "unknown error", errno));
384 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
387 int mutt_any_key_to_continue (const char *s)
393 f = open ("/dev/tty", O_RDONLY);
395 memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
396 t.c_lflag &= ~(ICANON | ECHO);
399 tcsetattr (f, TCSADRAIN, &t);
404 fputs (_("Press any key to continue..."), stdout);
408 tcsetattr (f, TCSADRAIN, &old);
410 fputs ("\r\n", stdout);
415 int mutt_do_pager (const char *banner,
416 const char *tempfile,
422 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
423 rc = mutt_pager (banner, tempfile, do_color, info);
429 mutt_expand_file_fmt (cmd, sizeof(cmd), Pager, tempfile);
430 if (mutt_system (cmd) == -1)
432 mutt_error (_("Error running \"%s\"!"), cmd);
437 mutt_unlink (tempfile);
443 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen, int *redraw, int buffy, int multiple, char ***files, int *numfiles)
447 mvaddstr (LINES-1, 0, (char *) prompt);
448 addstr (_(" ('?' for list): "));
460 else if (ch.ch == '?')
464 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
466 *redraw = REDRAW_FULL;
470 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
472 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
473 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
474 if (_mutt_get_field (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files, numfiles)
477 MAYBE_REDRAW (*redraw);
484 void mutt_ungetch (int ch, int op)
491 if (UngetCount >= UngetBufLen)
492 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof(event_t));
494 KeyEvent[UngetCount++] = tmp;
497 void mutt_flushinp (void)
503 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
504 /* The argument can take 3 values:
505 * -1: restore the value of the last call
506 * 0: make the cursor invisible
507 * 1: make the cursor visible
509 void mutt_curs_set (int cursor)
511 static int SavedCursor = 1;
514 cursor = SavedCursor;
516 SavedCursor = cursor;
518 if (curs_set (cursor) == ERR) {
519 if (cursor == 1) /* cnorm */
520 curs_set (2); /* cvvis */
525 int mutt_multi_choice (char *prompt, char *letters)
531 mvaddstr (LINES - 1, 0, prompt);
537 if (ch.ch == -1 || CI_is_return (ch.ch))
544 p = strchr (letters, ch.ch);
547 choice = p - letters + 1;
550 else if (ch.ch <= '9' && ch.ch > '0')
552 choice = ch.ch - '0';
553 if (choice <= mutt_strlen (letters))
559 CLEARLINE (LINES - 1);
565 * addwch would be provided by an up-to-date curses library
568 int mutt_addwch (wchar_t wc)
570 char buf[MB_LEN_MAX*2];
574 memset (&mbstate, 0, sizeof (mbstate));
575 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
576 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
584 * This formats a string, a bit like
585 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
586 * except that the widths refer to the number of character cells
590 void mutt_format_string (char *dest, size_t destlen,
591 int min_width, int max_width,
592 int right_justify, char m_pad_char,
593 const char *s, size_t n,
600 char scratch[MB_LEN_MAX];
601 mbstate_t mbstate1, mbstate2;
603 memset(&mbstate1, 0, sizeof (mbstate1));
604 memset(&mbstate2, 0, sizeof (mbstate2));
607 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k)
609 if (k == (size_t)(-1) || k == (size_t)(-2))
611 k = (k == (size_t)(-1)) ? 1 : n;
612 wc = replacement_char ();
614 if (arboreal && wc < M_TREE_MAX)
624 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
628 strncpy (p, scratch, k2);
633 w = (int)destlen < min_width ? destlen : min_width;
636 else if (right_justify)
642 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,
666 int right_justify = 1;
669 int max_width = INT_MAX;
672 ++prefix, right_justify = 0;
673 min_width = strtol (prefix, &p, 10);
677 max_width = strtol (prefix, &p, 10);
682 mutt_format_string (dest, destlen, min_width, max_width,
683 right_justify, ' ', s, mutt_strlen (s), arboreal);
686 void mutt_format_s (char *dest,
691 mutt_format_s_x (dest, destlen, prefix, s, 0);
694 void mutt_format_s_tree (char *dest,
699 mutt_format_s_x (dest, destlen, prefix, s, 1);
703 * mutt_paddstr (n, s) is almost equivalent to
704 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
707 void mutt_paddstr (int n, const char *s)
712 size_t len = mutt_strlen (s);
715 memset (&mbstate, 0, sizeof (mbstate));
716 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k)
718 if (k == (size_t)(-1) || k == (size_t)(-2))
720 k = (k == (size_t)(-1)) ? 1 : len;
721 wc = replacement_char ();
730 addnstr ((char *)s, 1);
739 * mutt_strwidth is like mutt_strlen except that it returns the width
740 * refering to the number of characters cells.
743 int mutt_strwidth (const char *s)
754 memset (&mbstate, 0, sizeof (mbstate));
755 for (w=0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k)
757 if (k == (size_t)(-1) || k == (size_t)(-2))
759 k = (k == (size_t)(-1)) ? 1 : n;
760 wc = replacement_char ();