2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 2004 g10 Code GmbH
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
16 #include "mutt_menu.h"
17 #include "mutt_curses.h"
22 #include <sys/types.h>
30 #ifdef HAVE_LANGINFO_YESEXPR
34 /* not possible to unget more than one char under some curses libs, and it
35 * is impossible to unget function keys in SLang, so roll our own input
38 size_t UngetCount = 0;
39 static size_t UngetBufLen = 0;
40 static event_t *KeyEvent;
42 void mutt_refresh (void)
44 /* don't refresh when we are waiting for a child. */
45 if (option (OPTKEEPQUIET))
48 /* don't refresh in the middle of macros unless necessary */
49 if (UngetCount && !option (OPTFORCEREFRESH))
56 /* Make sure that the next refresh does a full refresh. This could be
57 optmized by not doing it at all if DISPLAY is set as this might
58 indicate that a GUI based pinentry was used. Having an option to
59 customize this is of course the Mutt way. */
60 void mutt_need_hard_redraw (void)
62 if (!getenv ("DISPLAY")) {
63 keypad (stdscr, TRUE);
64 clearok (stdscr, TRUE);
65 set_option (OPTNEEDREDRAW);
69 event_t mutt_getch (void)
72 event_t err = { -1, OP_NULL }, ret;
74 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
75 return (KeyEvent[--UngetCount]);
79 mutt_allow_interrupt (1);
81 /* ncurses 4.2 sends this when the screen is resized */
83 while (ch == KEY_RESIZE)
84 #endif /* KEY_RESIZE */
86 mutt_allow_interrupt (0);
94 if ((ch & 0x80) && option (OPTMETAKEY)) {
95 /* send ALT-x as ESC-x */
105 return (ch == ctrl ('G') ? err : ret);
108 int _mutt_get_field ( /* const */ char *field, char *buf, size_t buflen,
109 int complete, int multiple, char ***files, int *numfiles)
114 ENTER_STATE *es = mutt_new_enter_state ();
117 CLEARLINE (LINES - 1);
120 getyx (stdscr, y, x);
122 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
126 CLEARLINE (LINES - 1);
127 mutt_free_enter_state (&es);
132 int mutt_get_password (char *msg, char *buf, size_t buflen)
136 CLEARLINE (LINES - 1);
138 set_option (OPTUNBUFFEREDINPUT);
139 rc = mutt_enter_string (buf, buflen, LINES - 1, mutt_strlen (msg), M_PASS);
140 unset_option (OPTUNBUFFEREDINPUT);
141 CLEARLINE (LINES - 1);
145 void mutt_clear_error (void)
148 if (!option (OPTNOCURSES))
149 CLEARLINE (LINES - 1);
152 static void fix_end_of_file (const char *data)
157 if ((fp = safe_fopen (data, "a+")) == NULL)
159 fseek (fp, -1, SEEK_END);
160 if ((c = fgetc (fp)) != '\n')
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 fix_end_of_file (data);
174 keypad (stdscr, TRUE);
175 clearok (stdscr, TRUE);
178 int mutt_yesorno (const char *msg, int def)
181 char *yes = _("yes");
184 size_t answer_string_len;
186 #ifdef HAVE_LANGINFO_YESEXPR
196 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
197 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
198 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
199 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
202 CLEARLINE (LINES - 1);
205 * In order to prevent the default answer to the question to wrapped
206 * around the screen in the even the question is wider than the screen,
207 * ensure there is enough room for the answer and truncate the question
210 answer_string = safe_malloc (COLS + 1);
211 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
212 def == M_YES ? no : yes);
213 answer_string_len = strlen (answer_string);
214 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
215 FREE (&answer_string);
220 if (CI_is_return (ch.ch))
227 #ifdef HAVE_LANGINFO_YESEXPR
229 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
233 (tolower (ch.ch) == 'y')) {
238 #ifdef HAVE_LANGINFO_YESEXPR
239 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
241 (tolower (ch.ch) == 'n')) {
250 #ifdef HAVE_LANGINFO_YESEXPR
258 addstr ((char *) (def == M_YES ? yes : no));
264 /* this function is called when the user presses the abort key */
265 void mutt_query_exit (void)
270 timeout (-1); /* restore blocking operation */
271 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
280 void mutt_curses_error (const char *fmt, ...)
282 char TmpErrorbuf[STRING];
286 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
289 dprint (1, (debugfile, "%s\n", Errorbuf));
290 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
291 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
292 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
294 if (!option (OPTKEEPQUIET)) {
296 SETCOLOR (MT_COLOR_ERROR);
297 mvaddstr (LINES - 1, 0, Errorbuf);
299 SETCOLOR (MT_COLOR_NORMAL);
303 set_option (OPTMSGERR);
306 void mutt_curses_message (const char *fmt, ...)
308 char TmpErrorbuf[STRING];
312 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
315 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
316 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
317 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
319 if (!option (OPTKEEPQUIET)) {
320 SETCOLOR (MT_COLOR_MESSAGE);
321 mvaddstr (LINES - 1, 0, Errorbuf);
323 SETCOLOR (MT_COLOR_NORMAL);
327 unset_option (OPTMSGERR);
330 void mutt_show_error (void)
332 if (option (OPTKEEPQUIET))
335 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
336 CLEARLINE (LINES - 1);
338 SETCOLOR (MT_COLOR_NORMAL);
341 void mutt_endwin (const char *msg)
343 if (!option (OPTNOCURSES)) {
344 CLEARLINE (LINES - 1);
357 void mutt_perror (const char *s)
359 char *p = strerror (errno);
361 dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
362 p ? p : "unknown error", errno));
363 mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
366 int mutt_any_key_to_continue (const char *s)
372 f = open ("/dev/tty", O_RDONLY);
374 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
375 t.c_lflag &= ~(ICANON | ECHO);
378 tcsetattr (f, TCSADRAIN, &t);
383 fputs (_("Press any key to continue..."), stdout);
387 tcsetattr (f, TCSADRAIN, &old);
389 fputs ("\r\n", stdout);
394 int mutt_do_pager (const char *banner,
395 const char *tempfile, int do_color, pager_t * info)
399 if (!Pager || mutt_strcmp (Pager, "builtin") == 0)
400 rc = mutt_pager (banner, tempfile, do_color, info);
405 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
406 if (mutt_system (cmd) == -1) {
407 mutt_error (_("Error running \"%s\"!"), cmd);
412 mutt_unlink (tempfile);
418 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
419 int *redraw, int buffy, int multiple, char ***files,
424 mvaddstr (LINES - 1, 0, (char *) prompt);
425 addstr (_(" ('?' for list): "));
433 CLEARLINE (LINES - 1);
436 else if (ch.ch == '?') {
439 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
441 *redraw = REDRAW_FULL;
444 char *pc = safe_malloc (mutt_strlen (prompt) + 3);
446 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
447 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
449 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
453 MAYBE_REDRAW (*redraw);
460 void mutt_ungetch (int ch, int op)
467 if (UngetCount >= UngetBufLen)
468 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof (event_t));
470 KeyEvent[UngetCount++] = tmp;
473 void mutt_flushinp (void)
479 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
480 /* The argument can take 3 values:
481 * -1: restore the value of the last call
482 * 0: make the cursor invisible
483 * 1: make the cursor visible
485 void mutt_curs_set (int cursor)
487 static int SavedCursor = 1;
490 cursor = SavedCursor;
492 SavedCursor = cursor;
494 if (curs_set (cursor) == ERR) {
495 if (cursor == 1) /* cnorm */
496 curs_set (2); /* cvvis */
501 int mutt_multi_choice (char *prompt, char *letters)
507 mvaddstr (LINES - 1, 0, prompt);
512 if (ch.ch == -1 || CI_is_return (ch.ch)) {
517 p = strchr (letters, ch.ch);
519 choice = p - letters + 1;
522 else if (ch.ch <= '9' && ch.ch > '0') {
523 choice = ch.ch - '0';
524 if (choice <= mutt_strlen (letters))
530 CLEARLINE (LINES - 1);
536 * addwch would be provided by an up-to-date curses library
539 int mutt_addwch (wchar_t wc)
541 char buf[MB_LEN_MAX * 2];
545 memset (&mbstate, 0, sizeof (mbstate));
546 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
547 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
555 * This formats a string, a bit like
556 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
557 * except that the widths refer to the number of character cells
561 void mutt_format_string (char *dest, size_t destlen,
562 int min_width, int max_width,
563 int right_justify, char m_pad_char,
564 const char *s, size_t n, int arboreal)
570 char scratch[MB_LEN_MAX];
571 mbstate_t mbstate1, mbstate2;
573 memset (&mbstate1, 0, sizeof (mbstate1));
574 memset (&mbstate2, 0, sizeof (mbstate2));
577 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
578 if (k == (size_t) (-1) || k == (size_t) (-2)) {
579 k = (k == (size_t) (-1)) ? 1 : n;
580 wc = replacement_char ();
582 if (arboreal && wc < M_TREE_MAX)
590 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
594 strncpy (p, scratch, k2);
599 w = (int) destlen < min_width ? destlen : min_width;
602 else if (right_justify) {
607 dest[w] = m_pad_char;
617 * This formats a string rather like
618 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
619 * snprintf (dest, destlen, fmt, s);
620 * except that the numbers in the conversion specification refer to
621 * the number of character cells when printed.
624 static void mutt_format_s_x (char *dest,
626 const char *prefix, const char *s, int arboreal)
628 int right_justify = 1;
631 int max_width = INT_MAX;
634 ++prefix, right_justify = 0;
635 min_width = strtol (prefix, &p, 10);
638 max_width = strtol (prefix, &p, 10);
643 mutt_format_string (dest, destlen, min_width, max_width,
644 right_justify, ' ', s, mutt_strlen (s), arboreal);
647 void mutt_format_s (char *dest,
648 size_t destlen, const char *prefix, const char *s)
650 mutt_format_s_x (dest, destlen, prefix, s, 0);
653 void mutt_format_s_tree (char *dest,
654 size_t destlen, const char *prefix, const char *s)
656 mutt_format_s_x (dest, destlen, prefix, s, 1);
660 * mutt_paddstr (n, s) is almost equivalent to
661 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
664 void mutt_paddstr (int n, const char *s)
669 size_t len = mutt_strlen (s);
672 memset (&mbstate, 0, sizeof (mbstate));
673 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
674 if (k == (size_t) (-1) || k == (size_t) (-2)) {
675 k = (k == (size_t) (-1)) ? 1 : len;
676 wc = replacement_char ();
684 addnstr ((char *) s, k);
693 * mutt_strwidth is like mutt_strlen except that it returns the width
694 * refering to the number of characters cells.
697 int mutt_strwidth (const char *s)
709 memset (&mbstate, 0, sizeof (mbstate));
710 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
711 if (k == (size_t) (-1) || k == (size_t) (-2)) {
712 k = (k == (size_t) (-1)) ? 1 : n;
713 wc = replacement_char ();