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"))
73 keypad (stdscr, TRUE);
74 clearok (stdscr, TRUE);
75 set_option (OPTNEEDREDRAW);
79 event_t mutt_getch (void)
82 event_t err = {-1, OP_NULL }, ret;
84 if (!option(OPTUNBUFFEREDINPUT) && UngetCount)
85 return (KeyEvent[--UngetCount]);
89 mutt_allow_interrupt (1);
91 /* ncurses 4.2 sends this when the screen is resized */
93 while (ch == KEY_RESIZE)
94 #endif /* KEY_RESIZE */
96 mutt_allow_interrupt (0);
104 if ((ch & 0x80) && option (OPTMETAKEY))
106 /* send ALT-x as ESC-x */
108 mutt_ungetch (ch, 0);
116 return (ch == ctrl ('G') ? err : ret);
119 int _mutt_get_field (/* const */ char *field, char *buf, size_t buflen, int complete, int multiple, char ***files, int *numfiles)
124 ENTER_STATE *es = mutt_new_enter_state();
131 getyx (stdscr, y, x);
132 ret = _mutt_enter_string (buf, buflen, y, x, complete, multiple, files, numfiles, es);
136 mutt_free_enter_state (&es);
141 int mutt_get_password (char *msg, char *buf, size_t buflen)
147 set_option (OPTUNBUFFEREDINPUT);
148 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
149 unset_option (OPTUNBUFFEREDINPUT);
154 void mutt_clear_error (void)
157 if (!option(OPTNOCURSES))
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);
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, def == M_YES ? no : yes);
221 answer_string_len = strlen (answer_string);
222 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
223 FREE (&answer_string);
229 if (CI_is_return (ch.ch))
237 #ifdef HAVE_LANGINFO_YESEXPR
240 (regexec (& reyes, answer, 0, 0, 0) == 0) :
244 (tolower (ch.ch) == 'y'))
250 #ifdef HAVE_LANGINFO_YESEXPR
252 (regexec (& reno, answer, 0, 0, 0) == 0) :
254 (tolower (ch.ch) == 'n'))
265 #ifdef HAVE_LANGINFO_YESEXPR
274 addstr ((char *) (def == M_YES ? yes : no));
280 /* this function is called when the user presses the abort key */
281 void mutt_query_exit (void)
286 timeout (-1); /* restore blocking operation */
287 if (mutt_yesorno (_("Exit Mutt?"), M_YES) == M_YES)
297 void mutt_curses_error (const char *fmt, ...)
302 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
305 dprint (1, (debugfile, "%s\n", Errorbuf));
306 mutt_format_string (Errorbuf, sizeof (Errorbuf),
307 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
309 if (!option (OPTKEEPQUIET))
312 SETCOLOR (MT_COLOR_ERROR);
313 mvaddstr (LINES-1, 0, Errorbuf);
315 SETCOLOR (MT_COLOR_NORMAL);
319 set_option (OPTMSGERR);
322 void mutt_curses_message (const char *fmt, ...)
327 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
330 mutt_format_string (Errorbuf, sizeof (Errorbuf),
331 0, COLS-2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
333 if (!option (OPTKEEPQUIET))
335 SETCOLOR (MT_COLOR_MESSAGE);
336 mvaddstr (LINES - 1, 0, Errorbuf);
338 SETCOLOR (MT_COLOR_NORMAL);
342 unset_option (OPTMSGERR);
345 void mutt_show_error (void)
347 if (option (OPTKEEPQUIET))
350 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
353 SETCOLOR (MT_COLOR_NORMAL);
356 void mutt_endwin (const char *msg)
358 if (!option (OPTNOCURSES))
360 CLEARLINE (LINES - 1);
374 void mutt_perror (const char *s)
376 char *p = strerror (errno);
378 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
379 p ? p : "unknown error", errno));
380 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
383 int mutt_any_key_to_continue (const char *s)
389 f = open ("/dev/tty", O_RDONLY);
391 memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
392 t.c_lflag &= ~(ICANON | ECHO);
395 tcsetattr (f, TCSADRAIN, &t);
400 fputs (_("Press any key to continue..."), stdout);
404 tcsetattr (f, TCSADRAIN, &old);
406 fputs ("\r\n", stdout);
411 int mutt_do_pager (const char *banner,
412 const char *tempfile,
418 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
419 rc = mutt_pager (banner, tempfile, do_color, info);
425 mutt_expand_file_fmt (cmd, sizeof(cmd), Pager, tempfile);
426 if (mutt_system (cmd) == -1)
428 mutt_error (_("Error running \"%s\"!"), cmd);
433 mutt_unlink (tempfile);
439 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen, int *redraw, int buffy, int multiple, char ***files, int *numfiles)
443 mvaddstr (LINES-1, 0, (char *) prompt);
444 addstr (_(" ('?' for list): "));
456 else if (ch.ch == '?')
460 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
462 *redraw = REDRAW_FULL;
466 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
468 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
469 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
470 if (_mutt_get_field (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files, numfiles)
473 MAYBE_REDRAW (*redraw);
480 void mutt_ungetch (int ch, int op)
487 if (UngetCount >= UngetBufLen)
488 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof(event_t));
490 KeyEvent[UngetCount++] = tmp;
493 void mutt_flushinp (void)
499 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
500 /* The argument can take 3 values:
501 * -1: restore the value of the last call
502 * 0: make the cursor invisible
503 * 1: make the cursor visible
505 void mutt_curs_set (int cursor)
507 static int SavedCursor = 1;
510 cursor = SavedCursor;
512 SavedCursor = cursor;
514 if (curs_set (cursor) == ERR) {
515 if (cursor == 1) /* cnorm */
516 curs_set (2); /* cvvis */
521 int mutt_multi_choice (char *prompt, char *letters)
527 mvaddstr (LINES - 1, 0, prompt);
533 if (ch.ch == -1 || CI_is_return (ch.ch))
540 p = strchr (letters, ch.ch);
543 choice = p - letters + 1;
546 else if (ch.ch <= '9' && ch.ch > '0')
548 choice = ch.ch - '0';
549 if (choice <= mutt_strlen (letters))
555 CLEARLINE (LINES - 1);
561 * addwch would be provided by an up-to-date curses library
564 int mutt_addwch (wchar_t wc)
566 char buf[MB_LEN_MAX*2];
570 memset (&mbstate, 0, sizeof (mbstate));
571 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
572 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
580 * This formats a string, a bit like
581 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
582 * except that the widths refer to the number of character cells
586 void mutt_format_string (char *dest, size_t destlen,
587 int min_width, int max_width,
588 int right_justify, char m_pad_char,
589 const char *s, size_t n,
595 char scratch[MB_LEN_MAX];
599 for (; n; s+=1, n-=1)
611 w = (int)destlen < min_width ? destlen : min_width;
614 else if (right_justify)
620 dest[w] = m_pad_char;
631 * This formats a string rather like
632 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
633 * snprintf (dest, destlen, fmt, s);
634 * except that the numbers in the conversion specification refer to
635 * the number of character cells when printed.
638 static void mutt_format_s_x (char *dest,
644 int right_justify = 1;
647 int max_width = INT_MAX;
650 ++prefix, right_justify = 0;
651 min_width = strtol (prefix, &p, 10);
655 max_width = strtol (prefix, &p, 10);
660 mutt_format_string (dest, destlen, min_width, max_width,
661 right_justify, ' ', s, mutt_strlen (s), arboreal);
664 void mutt_format_s (char *dest,
669 mutt_format_s_x (dest, destlen, prefix, s, 0);
672 void mutt_format_s_tree (char *dest,
677 mutt_format_s_x (dest, destlen, prefix, s, 1);
681 * mutt_paddstr (n, s) is almost equivalent to
682 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
685 void mutt_paddstr (int n, const char *s)
687 size_t len = mutt_strlen (s);
689 for (; len && *s; s += 1, len -= 1)
693 addnstr ((char *)s, 1);
701 * mutt_strwidth is like mutt_strlen except that it returns the width
702 * refering to the number of characters cells.
703 * AK: since we remove all that multibyte-character-stuff, it is equal to mutt_strlen
706 int mutt_strwidth (const char *s)
708 return mutt_strlen(s);