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")) {
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_password (char *msg, char *buf, size_t buflen)
145 CLEARLINE (LINES - 1);
147 set_option (OPTUNBUFFEREDINPUT);
148 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
149 unset_option (OPTUNBUFFEREDINPUT);
150 CLEARLINE (LINES - 1);
154 void mutt_clear_error (void)
157 if (!option (OPTNOCURSES))
158 CLEARLINE (LINES - 1);
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);
211 CLEARLINE (LINES - 1);
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,
221 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);
229 if (CI_is_return (ch.ch))
236 #ifdef HAVE_LANGINFO_YESEXPR
238 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
242 (tolower (ch.ch) == 'y')) {
247 #ifdef HAVE_LANGINFO_YESEXPR
248 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
250 (tolower (ch.ch) == 'n')) {
259 #ifdef HAVE_LANGINFO_YESEXPR
267 addstr ((char *) (def == M_YES ? yes : no));
273 /* this function is called when the user presses the abort key */
274 void mutt_query_exit (void)
279 timeout (-1); /* restore blocking operation */
280 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
289 void mutt_curses_error (const char *fmt, ...)
291 char TmpErrorbuf[STRING];
295 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
298 dprint (1, (debugfile, "%s\n", Errorbuf));
299 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
300 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
301 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
303 if (!option (OPTKEEPQUIET)) {
305 SETCOLOR (MT_COLOR_ERROR);
306 mvaddstr (LINES - 1, 0, Errorbuf);
308 SETCOLOR (MT_COLOR_NORMAL);
312 set_option (OPTMSGERR);
315 void mutt_curses_message (const char *fmt, ...)
317 char TmpErrorbuf[STRING];
321 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
324 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
325 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
326 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
328 if (!option (OPTKEEPQUIET)) {
329 SETCOLOR (MT_COLOR_MESSAGE);
330 mvaddstr (LINES - 1, 0, Errorbuf);
332 SETCOLOR (MT_COLOR_NORMAL);
336 unset_option (OPTMSGERR);
339 void mutt_show_error (void)
341 if (option (OPTKEEPQUIET))
344 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
345 CLEARLINE (LINES - 1);
347 SETCOLOR (MT_COLOR_NORMAL);
350 void mutt_endwin (const char *msg)
352 if (!option (OPTNOCURSES)) {
353 CLEARLINE (LINES - 1);
366 void mutt_perror (const char *s)
368 char *p = strerror (errno);
370 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
371 p ? p : "unknown error", errno));
372 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
375 int mutt_any_key_to_continue (const char *s)
381 f = open ("/dev/tty", O_RDONLY);
383 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
384 t.c_lflag &= ~(ICANON | ECHO);
387 tcsetattr (f, TCSADRAIN, &t);
392 fputs (_("Press any key to continue..."), stdout);
396 tcsetattr (f, TCSADRAIN, &old);
398 fputs ("\r\n", stdout);
403 int mutt_do_pager (const char *banner,
404 const char *tempfile, int do_color, pager_t * info)
408 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
409 rc = mutt_pager (banner, tempfile, do_color, info);
414 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
415 if (mutt_system (cmd) == -1) {
416 mutt_error (_("Error running \"%s\"!"), cmd);
421 mutt_unlink (tempfile);
427 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
428 int *redraw, int buffy, int multiple, char ***files,
433 mvaddstr (LINES - 1, 0, (char *) prompt);
434 addstr (_(" ('?' for list): "));
442 CLEARLINE (LINES - 1);
445 else if (ch.ch == '?') {
448 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
450 *redraw = REDRAW_FULL;
453 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
455 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
456 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
458 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
462 MAYBE_REDRAW (*redraw);
469 void mutt_ungetch (int ch, int op)
476 if (UngetCount >= UngetBufLen)
477 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof (event_t));
479 KeyEvent[UngetCount++] = tmp;
482 void mutt_flushinp (void)
488 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
489 /* The argument can take 3 values:
490 * -1: restore the value of the last call
491 * 0: make the cursor invisible
492 * 1: make the cursor visible
494 void mutt_curs_set (int cursor)
496 static int SavedCursor = 1;
499 cursor = SavedCursor;
501 SavedCursor = cursor;
503 if (curs_set (cursor) == ERR) {
504 if (cursor == 1) /* cnorm */
505 curs_set (2); /* cvvis */
510 int mutt_multi_choice (char *prompt, char *letters)
516 mvaddstr (LINES - 1, 0, prompt);
521 if (ch.ch == -1 || CI_is_return (ch.ch)) {
526 p = strchr (letters, ch.ch);
528 choice = p - letters + 1;
531 else if (ch.ch <= '9' && ch.ch > '0') {
532 choice = ch.ch - '0';
533 if (choice <= mutt_strlen (letters))
539 CLEARLINE (LINES - 1);
545 * addwch would be provided by an up-to-date curses library
548 int mutt_addwch (wchar_t wc)
550 char buf[MB_LEN_MAX * 2];
554 memset (&mbstate, 0, sizeof (mbstate));
555 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
556 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
564 * This formats a string, a bit like
565 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
566 * except that the widths refer to the number of character cells
570 void mutt_format_string (char *dest, size_t destlen,
571 int min_width, int max_width,
572 int right_justify, char m_pad_char,
573 const char *s, size_t n, int arboreal)
579 char scratch[MB_LEN_MAX];
580 mbstate_t mbstate1, mbstate2;
582 memset (&mbstate1, 0, sizeof (mbstate1));
583 memset (&mbstate2, 0, sizeof (mbstate2));
586 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
587 if (k == (size_t) (-1) || k == (size_t) (-2)) {
588 k = (k == (size_t) (-1)) ? 1 : n;
589 wc = replacement_char ();
591 if (arboreal && wc < M_TREE_MAX)
599 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
603 strncpy (p, scratch, k2);
608 w = (int) destlen < min_width ? destlen : min_width;
611 else if (right_justify) {
616 dest[w] = m_pad_char;
626 * This formats a string rather like
627 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
628 * snprintf (dest, destlen, fmt, s);
629 * except that the numbers in the conversion specification refer to
630 * the number of character cells when printed.
633 static void mutt_format_s_x (char *dest,
635 const char *prefix, const char *s, int arboreal)
637 int right_justify = 1;
640 int max_width = INT_MAX;
643 ++prefix, right_justify = 0;
644 min_width = strtol (prefix, &p, 10);
647 max_width = strtol (prefix, &p, 10);
652 mutt_format_string (dest, destlen, min_width, max_width,
653 right_justify, ' ', s, mutt_strlen (s), arboreal);
656 void mutt_format_s (char *dest,
657 size_t destlen, const char *prefix, const char *s)
659 mutt_format_s_x (dest, destlen, prefix, s, 0);
662 void mutt_format_s_tree (char *dest,
663 size_t destlen, const char *prefix, const char *s)
665 mutt_format_s_x (dest, destlen, prefix, s, 1);
669 * mutt_paddstr (n, s) is almost equivalent to
670 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
673 void mutt_paddstr (int n, const char *s)
678 size_t len = mutt_strlen (s);
681 memset (&mbstate, 0, sizeof (mbstate));
682 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
683 if (k == (size_t) (-1) || k == (size_t) (-2)) {
684 k = (k == (size_t) (-1)) ? 1 : len;
685 wc = replacement_char ();
693 addnstr ((char *) s, k);
702 * mutt_strwidth is like mutt_strlen except that it returns the width
703 * refering to the number of characters cells.
706 int mutt_strwidth (const char *s)
718 memset (&mbstate, 0, sizeof (mbstate));
719 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
720 if (k == (size_t) (-1) || k == (size_t) (-2)) {
721 k = (k == (size_t) (-1)) ? 1 : n;
722 wc = replacement_char ();