2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 #include "mutt_menu.h"
21 #include "mutt_curses.h"
26 #include <sys/types.h>
34 #ifdef HAVE_LANGINFO_YESEXPR
38 /* not possible to unget more than one char under some curses libs, and it
39 * is impossible to unget function keys in SLang, so roll our own input
42 size_t UngetCount = 0;
43 static size_t UngetBufLen = 0;
44 static event_t *KeyEvent;
46 void mutt_refresh (void)
48 /* don't refresh when we are waiting for a child. */
49 if (option (OPTKEEPQUIET))
52 /* don't refresh in the middle of macros unless necessary */
53 if (UngetCount && !option (OPTFORCEREFRESH))
60 event_t mutt_getch (void)
63 event_t err = {-1, OP_NULL }, ret;
65 if (!option(OPTUNBUFFEREDINPUT) && UngetCount)
66 return (KeyEvent[--UngetCount]);
70 mutt_allow_interrupt (1);
72 /* ncurses 4.2 sends this when the screen is resized */
74 while (ch == KEY_RESIZE)
75 #endif /* KEY_RESIZE */
77 mutt_allow_interrupt (0);
85 if ((ch & 0x80) && option (OPTMETAKEY))
87 /* send ALT-x as ESC-x */
97 return (ch == ctrl ('G') ? err : ret);
100 int _mutt_get_field (/* const */ char *field, char *buf, size_t buflen, int complete, int multiple, char ***files, int *numfiles)
105 ENTER_STATE *es = mutt_new_enter_state();
112 getyx (stdscr, y, x);
113 ret = _mutt_enter_string (buf, buflen, y, x, complete, multiple, files, numfiles, es);
117 mutt_free_enter_state (&es);
122 int mutt_get_password (char *msg, char *buf, size_t buflen)
128 set_option (OPTUNBUFFEREDINPUT);
129 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
130 unset_option (OPTUNBUFFEREDINPUT);
135 void mutt_clear_error (void)
138 if (!option(OPTNOCURSES))
142 void mutt_edit_file (const char *editor, const char *data)
144 char cmd[LONG_STRING];
147 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
148 if (mutt_system (cmd) == -1)
149 mutt_error (_("Error running \"%s\"!"), cmd);
150 keypad (stdscr, TRUE);
151 clearok (stdscr, TRUE);
154 int mutt_yesorno (const char *msg, int def)
157 char *yes = _("yes");
160 size_t answer_string_len;
162 #ifdef HAVE_LANGINFO_YESEXPR
172 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
173 !regcomp (&reyes, expr, REG_NOSUB|REG_EXTENDED);
174 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
175 !regcomp (&reno, expr, REG_NOSUB|REG_EXTENDED);
181 * In order to prevent the default answer to the question to wrapped
182 * around the screen in the even the question is wider than the screen,
183 * ensure there is enough room for the answer and truncate the question
186 answer_string = safe_malloc (COLS + 1);
187 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no, def == M_YES ? no : yes);
188 answer_string_len = strlen (answer_string);
189 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
190 FREE (&answer_string);
196 if (CI_is_return (ch.ch))
204 #ifdef HAVE_LANGINFO_YESEXPR
207 (regexec (& reyes, answer, 0, 0, 0) == 0) :
211 (tolower (ch.ch) == 'y'))
217 #ifdef HAVE_LANGINFO_YESEXPR
219 (regexec (& reno, answer, 0, 0, 0) == 0) :
221 (tolower (ch.ch) == 'n'))
232 #ifdef HAVE_LANGINFO_YESEXPR
241 addstr ((char *) (def == M_YES ? yes : no));
247 /* this function is called when the user presses the abort key */
248 void mutt_query_exit (void)
253 timeout (-1); /* restore blocking operation */
254 if (mutt_yesorno (_("Exit Mutt?"), M_YES) == M_YES)
264 void mutt_curses_error (const char *fmt, ...)
269 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
272 dprint (1, (debugfile, "%s\n", Errorbuf));
273 mutt_format_string (Errorbuf, sizeof (Errorbuf),
274 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
276 if (!option (OPTKEEPQUIET))
279 SETCOLOR (MT_COLOR_ERROR);
280 mvaddstr (LINES-1, 0, Errorbuf);
282 SETCOLOR (MT_COLOR_NORMAL);
286 set_option (OPTMSGERR);
289 void mutt_curses_message (const char *fmt, ...)
294 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
297 mutt_format_string (Errorbuf, sizeof (Errorbuf),
298 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
300 if (!option (OPTKEEPQUIET))
302 SETCOLOR (MT_COLOR_MESSAGE);
303 mvaddstr (LINES - 1, 0, Errorbuf);
305 SETCOLOR (MT_COLOR_NORMAL);
309 unset_option (OPTMSGERR);
312 void mutt_show_error (void)
314 if (option (OPTKEEPQUIET))
317 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
320 SETCOLOR (MT_COLOR_NORMAL);
323 void mutt_endwin (const char *msg)
325 if (!option (OPTNOCURSES))
327 CLEARLINE (LINES - 1);
341 void mutt_perror (const char *s)
343 char *p = strerror (errno);
345 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
346 p ? p : "unknown error", errno));
347 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
350 int mutt_any_key_to_continue (const char *s)
356 f = open ("/dev/tty", O_RDONLY);
358 memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
359 t.c_lflag &= ~(ICANON | ECHO);
362 tcsetattr (f, TCSADRAIN, &t);
367 fputs (_("Press any key to continue..."), stdout);
371 tcsetattr (f, TCSADRAIN, &old);
373 fputs ("\r\n", stdout);
378 int mutt_do_pager (const char *banner,
379 const char *tempfile,
385 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
386 rc = mutt_pager (banner, tempfile, do_color, info);
392 mutt_expand_file_fmt (cmd, sizeof(cmd), Pager, tempfile);
393 if (mutt_system (cmd) == -1)
395 mutt_error (_("Error running \"%s\"!"), cmd);
400 mutt_unlink (tempfile);
406 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen, int *redraw, int buffy, int multiple, char ***files, int *numfiles)
410 mvaddstr (LINES-1, 0, (char *) prompt);
411 addstr (_(" ('?' for list): "));
423 else if (ch.ch == '?')
427 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
429 *redraw = REDRAW_FULL;
433 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
435 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
436 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
437 if (_mutt_get_field (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files, numfiles)
440 MAYBE_REDRAW (*redraw);
447 void mutt_ungetch (int ch, int op)
454 if (UngetCount >= UngetBufLen)
455 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof(event_t));
457 KeyEvent[UngetCount++] = tmp;
460 void mutt_flushinp (void)
466 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
467 /* The argument can take 3 values:
468 * -1: restore the value of the last call
469 * 0: make the cursor invisible
470 * 1: make the cursor visible
472 void mutt_curs_set (int cursor)
474 static int SavedCursor = 1;
477 cursor = SavedCursor;
479 SavedCursor = cursor;
481 if (curs_set (cursor) == ERR) {
482 if (cursor == 1) /* cnorm */
483 curs_set (2); /* cvvis */
488 int mutt_multi_choice (char *prompt, char *letters)
494 mvaddstr (LINES - 1, 0, prompt);
500 if (ch.ch == -1 || CI_is_return (ch.ch))
507 p = strchr (letters, ch.ch);
510 choice = p - letters + 1;
513 else if (ch.ch <= '9' && ch.ch > '0')
515 choice = ch.ch - '0';
516 if (choice <= mutt_strlen (letters))
522 CLEARLINE (LINES - 1);
528 * addwch would be provided by an up-to-date curses library
531 int mutt_addwch (wchar_t wc)
533 char buf[MB_LEN_MAX*2];
537 memset (&mbstate, 0, sizeof (mbstate));
538 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
539 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
547 * This formats a string, a bit like
548 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
549 * except that the widths refer to the number of character cells
553 void mutt_format_string (char *dest, size_t destlen,
554 int min_width, int max_width,
555 int right_justify, char m_pad_char,
556 const char *s, size_t n,
562 char scratch[MB_LEN_MAX];
566 for (; n; s+=1, n-=1)
578 w = (int)destlen < min_width ? destlen : min_width;
581 else if (right_justify)
587 dest[w] = m_pad_char;
598 * This formats a string rather like
599 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
600 * snprintf (dest, destlen, fmt, s);
601 * except that the numbers in the conversion specification refer to
602 * the number of character cells when printed.
605 static void mutt_format_s_x (char *dest,
611 int right_justify = 1;
614 int max_width = INT_MAX;
617 ++prefix, right_justify = 0;
618 min_width = strtol (prefix, &p, 10);
622 max_width = strtol (prefix, &p, 10);
627 mutt_format_string (dest, destlen, min_width, max_width,
628 right_justify, ' ', s, mutt_strlen (s), arboreal);
631 void mutt_format_s (char *dest,
636 mutt_format_s_x (dest, destlen, prefix, s, 0);
639 void mutt_format_s_tree (char *dest,
644 mutt_format_s_x (dest, destlen, prefix, s, 1);
648 * mutt_paddstr (n, s) is almost equivalent to
649 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
652 void mutt_paddstr (int n, const char *s)
654 size_t len = mutt_strlen (s);
656 for (; len && *s; s += 1, len -= 1)
660 addnstr ((char *)s, 1);
668 * mutt_strwidth is like mutt_strlen except that it returns the width
669 * refering to the number of characters cells.
670 * AK: since we remove all that multibyte-character-stuff, it is equal to mutt_strlen
673 int mutt_strwidth (const char *s)
675 return mutt_strlen(s);