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_field_att (/* const */ char *field, char *buf, size_t buflen, int complete, int multiple, char ***files, int *numfiles)
142 ENTER_STATE *es = mutt_new_enter_state();
149 getyx (stdscr, y, x);
150 ret = _mutt_enter_string_att (buf, buflen, y, x, complete, multiple, files, numfiles, es);
154 mutt_free_enter_state (&es);
160 int mutt_get_password (char *msg, char *buf, size_t buflen)
166 set_option (OPTUNBUFFEREDINPUT);
167 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
168 unset_option (OPTUNBUFFEREDINPUT);
173 void mutt_clear_error (void)
176 if (!option(OPTNOCURSES))
180 static void fix_end_of_file (const char *data)
185 if ((fp = safe_fopen (data, "a+")) == NULL)
187 fseek (fp,-1,SEEK_END);
188 if ((c = fgetc(fp)) != '\n')
193 void mutt_edit_file (const char *editor, const char *data)
195 char cmd[LONG_STRING];
198 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
199 if (mutt_system (cmd) == -1)
200 mutt_error (_("Error running \"%s\"!"), cmd);
201 fix_end_of_file (data);
202 keypad (stdscr, TRUE);
203 clearok (stdscr, TRUE);
206 int mutt_yesorno (const char *msg, int def)
209 char *yes = _("yes");
212 size_t answer_string_len;
214 #ifdef HAVE_LANGINFO_YESEXPR
224 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
225 !regcomp (&reyes, expr, REG_NOSUB|REG_EXTENDED);
226 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
227 !regcomp (&reno, expr, REG_NOSUB|REG_EXTENDED);
233 * In order to prevent the default answer to the question to wrapped
234 * around the screen in the even the question is wider than the screen,
235 * ensure there is enough room for the answer and truncate the question
238 answer_string = safe_malloc (COLS + 1);
239 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no, def == M_YES ? no : yes);
240 answer_string_len = strlen (answer_string);
241 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
242 FREE (&answer_string);
248 if (CI_is_return (ch.ch))
256 #ifdef HAVE_LANGINFO_YESEXPR
259 (regexec (& reyes, answer, 0, 0, 0) == 0) :
263 (tolower (ch.ch) == 'y'))
269 #ifdef HAVE_LANGINFO_YESEXPR
271 (regexec (& reno, answer, 0, 0, 0) == 0) :
273 (tolower (ch.ch) == 'n'))
284 #ifdef HAVE_LANGINFO_YESEXPR
293 addstr ((char *) (def == M_YES ? yes : no));
299 /* this function is called when the user presses the abort key */
300 void mutt_query_exit (void)
305 timeout (-1); /* restore blocking operation */
306 if (mutt_yesorno (_("Exit Mutt?"), M_YES) == M_YES)
316 void mutt_curses_error (const char *fmt, ...)
321 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
324 dprint (1, (debugfile, "%s\n", Errorbuf));
325 mutt_format_string (Errorbuf, sizeof (Errorbuf),
326 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
328 if (!option (OPTKEEPQUIET))
331 SETCOLOR (MT_COLOR_ERROR);
332 mvaddstr (LINES-1, 0, Errorbuf);
334 SETCOLOR (MT_COLOR_NORMAL);
338 set_option (OPTMSGERR);
341 void mutt_curses_message (const char *fmt, ...)
346 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
349 mutt_format_string (Errorbuf, sizeof (Errorbuf),
350 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
352 if (!option (OPTKEEPQUIET))
354 SETCOLOR (MT_COLOR_MESSAGE);
355 mvaddstr (LINES - 1, 0, Errorbuf);
357 SETCOLOR (MT_COLOR_NORMAL);
361 unset_option (OPTMSGERR);
364 void mutt_show_error (void)
366 if (option (OPTKEEPQUIET))
369 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
372 SETCOLOR (MT_COLOR_NORMAL);
375 void mutt_endwin (const char *msg)
377 if (!option (OPTNOCURSES))
379 CLEARLINE (LINES - 1);
393 void mutt_perror (const char *s)
395 char *p = strerror (errno);
397 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
398 p ? p : "unknown error", errno));
399 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
402 int mutt_any_key_to_continue (const char *s)
408 f = open ("/dev/tty", O_RDONLY);
410 memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
411 t.c_lflag &= ~(ICANON | ECHO);
414 tcsetattr (f, TCSADRAIN, &t);
419 fputs (_("Press any key to continue..."), stdout);
423 tcsetattr (f, TCSADRAIN, &old);
425 fputs ("\r\n", stdout);
430 int mutt_do_pager (const char *banner,
431 const char *tempfile,
437 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
438 rc = mutt_pager (banner, tempfile, do_color, info);
444 mutt_expand_file_fmt (cmd, sizeof(cmd), Pager, tempfile);
445 if (mutt_system (cmd) == -1)
447 mutt_error (_("Error running \"%s\"!"), cmd);
452 mutt_unlink (tempfile);
458 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen, int *redraw, int buffy, int multiple, char ***files, int *numfiles)
462 mvaddstr (LINES-1, 0, (char *) prompt);
463 addstr (_(" ('?' for list): "));
475 else if (ch.ch == '?')
479 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
481 *redraw = REDRAW_FULL;
485 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
487 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
488 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
489 if (_mutt_get_field (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files, numfiles)
492 MAYBE_REDRAW (*redraw);
499 void mutt_ungetch (int ch, int op)
506 if (UngetCount >= UngetBufLen)
507 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof(event_t));
509 KeyEvent[UngetCount++] = tmp;
512 void mutt_flushinp (void)
518 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
519 /* The argument can take 3 values:
520 * -1: restore the value of the last call
521 * 0: make the cursor invisible
522 * 1: make the cursor visible
524 void mutt_curs_set (int cursor)
526 static int SavedCursor = 1;
529 cursor = SavedCursor;
531 SavedCursor = cursor;
533 if (curs_set (cursor) == ERR) {
534 if (cursor == 1) /* cnorm */
535 curs_set (2); /* cvvis */
540 int mutt_multi_choice (char *prompt, char *letters)
546 mvaddstr (LINES - 1, 0, prompt);
552 if (ch.ch == -1 || CI_is_return (ch.ch))
559 p = strchr (letters, ch.ch);
562 choice = p - letters + 1;
565 else if (ch.ch <= '9' && ch.ch > '0')
567 choice = ch.ch - '0';
568 if (choice <= mutt_strlen (letters))
574 CLEARLINE (LINES - 1);
580 * addwch would be provided by an up-to-date curses library
583 int mutt_addwch (wchar_t wc)
585 char buf[MB_LEN_MAX*2];
589 memset (&mbstate, 0, sizeof (mbstate));
590 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
591 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
599 * This formats a string, a bit like
600 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
601 * except that the widths refer to the number of character cells
605 void mutt_format_string (char *dest, size_t destlen,
606 int min_width, int max_width,
607 int right_justify, char m_pad_char,
608 const char *s, size_t n,
614 char scratch[MB_LEN_MAX];
618 for (; n; s+=1, n-=1)
630 w = (int)destlen < min_width ? destlen : min_width;
633 else if (right_justify)
639 dest[w] = m_pad_char;
650 * This formats a string rather like
651 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
652 * snprintf (dest, destlen, fmt, s);
653 * except that the numbers in the conversion specification refer to
654 * the number of character cells when printed.
657 static void mutt_format_s_x (char *dest,
663 int right_justify = 1;
666 int max_width = INT_MAX;
669 ++prefix, right_justify = 0;
670 min_width = strtol (prefix, &p, 10);
674 max_width = strtol (prefix, &p, 10);
679 mutt_format_string (dest, destlen, min_width, max_width,
680 right_justify, ' ', s, mutt_strlen (s), arboreal);
683 void mutt_format_s (char *dest,
688 mutt_format_s_x (dest, destlen, prefix, s, 0);
691 void mutt_format_s_tree (char *dest,
696 mutt_format_s_x (dest, destlen, prefix, s, 1);
700 * mutt_paddstr (n, s) is almost equivalent to
701 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
704 void mutt_paddstr (int n, const char *s)
706 size_t len = mutt_strlen (s);
708 for (; len && *s; s += 1, len -= 1)
712 addnstr ((char *)s, 1);
720 * mutt_strwidth is like mutt_strlen except that it returns the width
721 * refering to the number of characters cells.
722 * AK: since we remove all that multibyte-character-stuff, it is equal to mutt_strlen
725 int mutt_strwidth (const char *s)
727 return mutt_strlen(s);