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_field_att (/* const */ char *field, char *buf, size_t buflen, int complete, int multiple, char ***files, int *numfiles)
127 ENTER_STATE *es = mutt_new_enter_state();
134 getyx (stdscr, y, x);
135 ret = _mutt_enter_string_att (buf, buflen, y, x, complete, multiple, files, numfiles, es);
139 mutt_free_enter_state (&es);
145 int mutt_get_password (char *msg, char *buf, size_t buflen)
151 set_option (OPTUNBUFFEREDINPUT);
152 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
153 unset_option (OPTUNBUFFEREDINPUT);
158 void mutt_clear_error (void)
161 if (!option(OPTNOCURSES))
165 void mutt_edit_file (const char *editor, const char *data)
167 char cmd[LONG_STRING];
170 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
171 if (mutt_system (cmd) == -1)
172 mutt_error (_("Error running \"%s\"!"), cmd);
173 keypad (stdscr, TRUE);
174 clearok (stdscr, TRUE);
177 int mutt_yesorno (const char *msg, int def)
180 char *yes = _("yes");
183 size_t answer_string_len;
185 #ifdef HAVE_LANGINFO_YESEXPR
195 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
196 !regcomp (&reyes, expr, REG_NOSUB|REG_EXTENDED);
197 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
198 !regcomp (&reno, expr, REG_NOSUB|REG_EXTENDED);
204 * In order to prevent the default answer to the question to wrapped
205 * around the screen in the even the question is wider than the screen,
206 * ensure there is enough room for the answer and truncate the question
209 answer_string = safe_malloc (COLS + 1);
210 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no, def == M_YES ? no : yes);
211 answer_string_len = strlen (answer_string);
212 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
213 FREE (&answer_string);
219 if (CI_is_return (ch.ch))
227 #ifdef HAVE_LANGINFO_YESEXPR
230 (regexec (& reyes, answer, 0, 0, 0) == 0) :
234 (tolower (ch.ch) == 'y'))
240 #ifdef HAVE_LANGINFO_YESEXPR
242 (regexec (& reno, answer, 0, 0, 0) == 0) :
244 (tolower (ch.ch) == 'n'))
255 #ifdef HAVE_LANGINFO_YESEXPR
264 addstr ((char *) (def == M_YES ? yes : no));
270 /* this function is called when the user presses the abort key */
271 void mutt_query_exit (void)
276 timeout (-1); /* restore blocking operation */
277 if (mutt_yesorno (_("Exit Mutt?"), M_YES) == M_YES)
287 void mutt_curses_error (const char *fmt, ...)
292 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
295 dprint (1, (debugfile, "%s\n", Errorbuf));
296 mutt_format_string (Errorbuf, sizeof (Errorbuf),
297 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
299 if (!option (OPTKEEPQUIET))
302 SETCOLOR (MT_COLOR_ERROR);
303 mvaddstr (LINES-1, 0, Errorbuf);
305 SETCOLOR (MT_COLOR_NORMAL);
309 set_option (OPTMSGERR);
312 void mutt_curses_message (const char *fmt, ...)
317 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
320 mutt_format_string (Errorbuf, sizeof (Errorbuf),
321 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
323 if (!option (OPTKEEPQUIET))
325 SETCOLOR (MT_COLOR_MESSAGE);
326 mvaddstr (LINES - 1, 0, Errorbuf);
328 SETCOLOR (MT_COLOR_NORMAL);
332 unset_option (OPTMSGERR);
335 void mutt_show_error (void)
337 if (option (OPTKEEPQUIET))
340 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
343 SETCOLOR (MT_COLOR_NORMAL);
346 void mutt_endwin (const char *msg)
348 if (!option (OPTNOCURSES))
350 CLEARLINE (LINES - 1);
364 void mutt_perror (const char *s)
366 char *p = strerror (errno);
368 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
369 p ? p : "unknown error", errno));
370 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
373 int mutt_any_key_to_continue (const char *s)
379 f = open ("/dev/tty", O_RDONLY);
381 memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
382 t.c_lflag &= ~(ICANON | ECHO);
385 tcsetattr (f, TCSADRAIN, &t);
390 fputs (_("Press any key to continue..."), stdout);
394 tcsetattr (f, TCSADRAIN, &old);
396 fputs ("\r\n", stdout);
401 int mutt_do_pager (const char *banner,
402 const char *tempfile,
408 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
409 rc = mutt_pager (banner, tempfile, do_color, info);
415 mutt_expand_file_fmt (cmd, sizeof(cmd), Pager, tempfile);
416 if (mutt_system (cmd) == -1)
418 mutt_error (_("Error running \"%s\"!"), cmd);
423 mutt_unlink (tempfile);
429 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen, int *redraw, int buffy, int multiple, char ***files, int *numfiles)
433 mvaddstr (LINES-1, 0, (char *) prompt);
434 addstr (_(" ('?' for list): "));
446 else if (ch.ch == '?')
450 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
452 *redraw = REDRAW_FULL;
456 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
458 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
459 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
460 if (_mutt_get_field (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files, numfiles)
463 MAYBE_REDRAW (*redraw);
470 void mutt_ungetch (int ch, int op)
477 if (UngetCount >= UngetBufLen)
478 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof(event_t));
480 KeyEvent[UngetCount++] = tmp;
483 void mutt_flushinp (void)
489 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
490 /* The argument can take 3 values:
491 * -1: restore the value of the last call
492 * 0: make the cursor invisible
493 * 1: make the cursor visible
495 void mutt_curs_set (int cursor)
497 static int SavedCursor = 1;
500 cursor = SavedCursor;
502 SavedCursor = cursor;
504 if (curs_set (cursor) == ERR) {
505 if (cursor == 1) /* cnorm */
506 curs_set (2); /* cvvis */
511 int mutt_multi_choice (char *prompt, char *letters)
517 mvaddstr (LINES - 1, 0, prompt);
523 if (ch.ch == -1 || CI_is_return (ch.ch))
530 p = strchr (letters, ch.ch);
533 choice = p - letters + 1;
536 else if (ch.ch <= '9' && ch.ch > '0')
538 choice = ch.ch - '0';
539 if (choice <= mutt_strlen (letters))
545 CLEARLINE (LINES - 1);
551 * addwch would be provided by an up-to-date curses library
554 int mutt_addwch (wchar_t wc)
556 char buf[MB_LEN_MAX*2];
560 memset (&mbstate, 0, sizeof (mbstate));
561 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
562 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
570 * This formats a string, a bit like
571 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
572 * except that the widths refer to the number of character cells
576 void mutt_format_string (char *dest, size_t destlen,
577 int min_width, int max_width,
578 int right_justify, char m_pad_char,
579 const char *s, size_t n,
585 char scratch[MB_LEN_MAX];
589 for (; n; s+=1, n-=1)
601 w = (int)destlen < min_width ? destlen : min_width;
604 else if (right_justify)
610 dest[w] = m_pad_char;
621 * This formats a string rather like
622 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
623 * snprintf (dest, destlen, fmt, s);
624 * except that the numbers in the conversion specification refer to
625 * the number of character cells when printed.
628 static void mutt_format_s_x (char *dest,
634 int right_justify = 1;
637 int max_width = INT_MAX;
640 ++prefix, right_justify = 0;
641 min_width = strtol (prefix, &p, 10);
645 max_width = strtol (prefix, &p, 10);
650 mutt_format_string (dest, destlen, min_width, max_width,
651 right_justify, ' ', s, mutt_strlen (s), arboreal);
654 void mutt_format_s (char *dest,
659 mutt_format_s_x (dest, destlen, prefix, s, 0);
662 void mutt_format_s_tree (char *dest,
667 mutt_format_s_x (dest, destlen, prefix, s, 1);
671 * mutt_paddstr (n, s) is almost equivalent to
672 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
675 void mutt_paddstr (int n, const char *s)
677 size_t len = mutt_strlen (s);
679 for (; len && *s; s += 1, len -= 1)
683 addnstr ((char *)s, 1);
691 * mutt_strwidth is like mutt_strlen except that it returns the width
692 * refering to the number of characters cells.
693 * AK: since we remove all that multibyte-character-stuff, it is equal to mutt_strlen
696 int mutt_strwidth (const char *s)
698 return mutt_strlen(s);