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.
21 #include "mutt_menu.h"
22 #include "mutt_curses.h"
27 #include <sys/types.h>
35 #ifdef HAVE_LANGINFO_YESEXPR
39 /* not possible to unget more than one char under some curses libs, and it
40 * is impossible to unget function keys in SLang, so roll our own input
43 size_t UngetCount = 0;
44 static size_t UngetBufLen = 0;
45 static event_t *KeyEvent;
47 void mutt_refresh (void)
49 /* don't refresh when we are waiting for a child. */
50 if (option (OPTKEEPQUIET))
53 /* don't refresh in the middle of macros unless necessary */
54 if (UngetCount && !option (OPTFORCEREFRESH))
61 /* Make sure that the next refresh does a full refresh. This could be
62 optmized by not doing it at all if DISPLAY is set as this might
63 indicate that a GUI based pinentry was used. Having an option to
64 customize this is of course the Mutt way. */
65 void mutt_need_hard_redraw (void)
67 if (!getenv ("DISPLAY"))
69 keypad (stdscr, TRUE);
70 clearok (stdscr, TRUE);
71 set_option (OPTNEEDREDRAW);
75 event_t mutt_getch (void)
78 event_t err = {-1, OP_NULL }, ret;
80 if (!option(OPTUNBUFFEREDINPUT) && UngetCount)
81 return (KeyEvent[--UngetCount]);
85 mutt_allow_interrupt (1);
87 /* ncurses 4.2 sends this when the screen is resized */
89 while (ch == KEY_RESIZE)
90 #endif /* KEY_RESIZE */
92 mutt_allow_interrupt (0);
100 if ((ch & 0x80) && option (OPTMETAKEY))
102 /* send ALT-x as ESC-x */
104 mutt_ungetch (ch, 0);
112 return (ch == ctrl ('G') ? err : ret);
115 int _mutt_get_field (/* const */ char *field, char *buf, size_t buflen, int complete, int multiple, char ***files, int *numfiles)
120 ENTER_STATE *es = mutt_new_enter_state();
127 getyx (stdscr, y, x);
128 ret = _mutt_enter_string (buf, buflen, y, x, complete, multiple, files, numfiles, es);
132 mutt_free_enter_state (&es);
137 int mutt_get_password (char *msg, char *buf, size_t buflen)
143 set_option (OPTUNBUFFEREDINPUT);
144 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
145 unset_option (OPTUNBUFFEREDINPUT);
150 void mutt_clear_error (void)
153 if (!option(OPTNOCURSES))
157 static void fix_end_of_file (const char *data)
162 if ((fp = safe_fopen (data, "a+")) == NULL)
164 fseek (fp,-1,SEEK_END);
165 if ((c = fgetc(fp)) != '\n')
170 void mutt_edit_file (const char *editor, const char *data)
172 char cmd[LONG_STRING];
175 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
176 if (mutt_system (cmd) == -1)
177 mutt_error (_("Error running \"%s\"!"), cmd);
178 fix_end_of_file (data);
179 keypad (stdscr, TRUE);
180 clearok (stdscr, TRUE);
183 int mutt_yesorno (const char *msg, int def)
186 char *yes = _("yes");
189 size_t answer_string_len;
191 #ifdef HAVE_LANGINFO_YESEXPR
201 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
202 !regcomp (&reyes, expr, REG_NOSUB|REG_EXTENDED);
203 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
204 !regcomp (&reno, expr, REG_NOSUB|REG_EXTENDED);
210 * In order to prevent the default answer to the question to wrapped
211 * around the screen in the even the question is wider than the screen,
212 * ensure there is enough room for the answer and truncate the question
215 answer_string = safe_malloc (COLS + 1);
216 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no, def == M_YES ? no : yes);
217 answer_string_len = strlen (answer_string);
218 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
219 FREE (&answer_string);
225 if (CI_is_return (ch.ch))
233 #ifdef HAVE_LANGINFO_YESEXPR
236 (regexec (& reyes, answer, 0, 0, 0) == 0) :
240 (tolower (ch.ch) == 'y'))
246 #ifdef HAVE_LANGINFO_YESEXPR
248 (regexec (& reno, answer, 0, 0, 0) == 0) :
250 (tolower (ch.ch) == 'n'))
261 #ifdef HAVE_LANGINFO_YESEXPR
270 addstr ((char *) (def == M_YES ? yes : no));
276 /* this function is called when the user presses the abort key */
277 void mutt_query_exit (void)
282 timeout (-1); /* restore blocking operation */
283 if (mutt_yesorno (_("Exit Mutt?"), M_YES) == M_YES)
293 void mutt_curses_error (const char *fmt, ...)
298 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
301 dprint (1, (debugfile, "%s\n", Errorbuf));
302 mutt_format_string (Errorbuf, sizeof (Errorbuf),
303 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
305 if (!option (OPTKEEPQUIET))
308 SETCOLOR (MT_COLOR_ERROR);
309 mvaddstr (LINES-1, 0, Errorbuf);
311 SETCOLOR (MT_COLOR_NORMAL);
315 set_option (OPTMSGERR);
318 void mutt_curses_message (const char *fmt, ...)
323 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
326 mutt_format_string (Errorbuf, sizeof (Errorbuf),
327 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
329 if (!option (OPTKEEPQUIET))
331 SETCOLOR (MT_COLOR_MESSAGE);
332 mvaddstr (LINES - 1, 0, Errorbuf);
334 SETCOLOR (MT_COLOR_NORMAL);
338 unset_option (OPTMSGERR);
341 void mutt_show_error (void)
343 if (option (OPTKEEPQUIET))
346 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
349 SETCOLOR (MT_COLOR_NORMAL);
352 void mutt_endwin (const char *msg)
354 if (!option (OPTNOCURSES))
356 CLEARLINE (LINES - 1);
370 void mutt_perror (const char *s)
372 char *p = strerror (errno);
374 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
375 p ? p : "unknown error", errno));
376 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
379 int mutt_any_key_to_continue (const char *s)
385 f = open ("/dev/tty", O_RDONLY);
387 memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
388 t.c_lflag &= ~(ICANON | ECHO);
391 tcsetattr (f, TCSADRAIN, &t);
396 fputs (_("Press any key to continue..."), stdout);
400 tcsetattr (f, TCSADRAIN, &old);
402 fputs ("\r\n", stdout);
407 int mutt_do_pager (const char *banner,
408 const char *tempfile,
414 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
415 rc = mutt_pager (banner, tempfile, do_color, info);
421 mutt_expand_file_fmt (cmd, sizeof(cmd), Pager, tempfile);
422 if (mutt_system (cmd) == -1)
424 mutt_error (_("Error running \"%s\"!"), cmd);
429 mutt_unlink (tempfile);
435 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen, int *redraw, int buffy, int multiple, char ***files, int *numfiles)
439 mvaddstr (LINES-1, 0, (char *) prompt);
440 addstr (_(" ('?' for list): "));
452 else if (ch.ch == '?')
456 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
458 *redraw = REDRAW_FULL;
462 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
464 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
465 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
466 if (_mutt_get_field (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files, numfiles)
469 MAYBE_REDRAW (*redraw);
476 void mutt_ungetch (int ch, int op)
483 if (UngetCount >= UngetBufLen)
484 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof(event_t));
486 KeyEvent[UngetCount++] = tmp;
489 void mutt_flushinp (void)
495 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
496 /* The argument can take 3 values:
497 * -1: restore the value of the last call
498 * 0: make the cursor invisible
499 * 1: make the cursor visible
501 void mutt_curs_set (int cursor)
503 static int SavedCursor = 1;
506 cursor = SavedCursor;
508 SavedCursor = cursor;
510 if (curs_set (cursor) == ERR) {
511 if (cursor == 1) /* cnorm */
512 curs_set (2); /* cvvis */
517 int mutt_multi_choice (char *prompt, char *letters)
523 mvaddstr (LINES - 1, 0, prompt);
529 if (ch.ch == -1 || CI_is_return (ch.ch))
536 p = strchr (letters, ch.ch);
539 choice = p - letters + 1;
542 else if (ch.ch <= '9' && ch.ch > '0')
544 choice = ch.ch - '0';
545 if (choice <= mutt_strlen (letters))
551 CLEARLINE (LINES - 1);
557 * addwch would be provided by an up-to-date curses library
560 int mutt_addwch (wchar_t wc)
562 char buf[MB_LEN_MAX*2];
566 memset (&mbstate, 0, sizeof (mbstate));
567 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
568 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
576 * This formats a string, a bit like
577 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
578 * except that the widths refer to the number of character cells
582 void mutt_format_string (char *dest, size_t destlen,
583 int min_width, int max_width,
584 int right_justify, char m_pad_char,
585 const char *s, size_t n,
591 char scratch[MB_LEN_MAX];
595 for (; n; s+=1, n-=1)
607 w = (int)destlen < min_width ? destlen : min_width;
610 else if (right_justify)
616 dest[w] = m_pad_char;
627 * This formats a string rather like
628 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
629 * snprintf (dest, destlen, fmt, s);
630 * except that the numbers in the conversion specification refer to
631 * the number of character cells when printed.
634 static void mutt_format_s_x (char *dest,
640 int right_justify = 1;
643 int max_width = INT_MAX;
646 ++prefix, right_justify = 0;
647 min_width = strtol (prefix, &p, 10);
651 max_width = strtol (prefix, &p, 10);
656 mutt_format_string (dest, destlen, min_width, max_width,
657 right_justify, ' ', s, mutt_strlen (s), arboreal);
660 void mutt_format_s (char *dest,
665 mutt_format_s_x (dest, destlen, prefix, s, 0);
668 void mutt_format_s_tree (char *dest,
673 mutt_format_s_x (dest, destlen, prefix, s, 1);
677 * mutt_paddstr (n, s) is almost equivalent to
678 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
681 void mutt_paddstr (int n, const char *s)
683 size_t len = mutt_strlen (s);
685 for (; len && *s; s += 1, len -= 1)
689 addnstr ((char *)s, 1);
697 * mutt_strwidth is like mutt_strlen except that it returns the width
698 * refering to the number of characters cells.
699 * AK: since we remove all that multibyte-character-stuff, it is equal to mutt_strlen
702 int mutt_strwidth (const char *s)
704 return mutt_strlen(s);