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 * Parts were written/modified by:
7 * Nico Golde <nico@ngolde.de>
9 * This file is part of mutt-ng, see http://www.muttng.org/.
10 * It's licensed under the GNU General Public License,
11 * please see the file GPL in the top level source directory.
19 #include "mutt_menu.h"
20 #include "mutt_curses.h"
27 #include "lib/debug.h"
30 #include <sys/types.h>
38 #ifdef HAVE_LANGINFO_YESEXPR
42 /* not possible to unget more than one char under some curses libs, and it
43 * is impossible to unget function keys in SLang, so roll our own input
46 size_t UngetCount = 0;
47 static size_t UngetBufLen = 0;
48 static event_t *KeyEvent;
50 void mutt_refresh (void)
52 /* don't refresh when we are waiting for a child. */
53 if (option (OPTKEEPQUIET))
56 /* don't refresh in the middle of macros unless necessary */
57 if (UngetCount && !option (OPTFORCEREFRESH))
64 /* Make sure that the next refresh does a full refresh. This could be
65 optmized by not doing it at all if DISPLAY is set as this might
66 indicate that a GUI based pinentry was used. Having an option to
67 customize this is of course the Mutt way. */
68 void mutt_need_hard_redraw (void)
70 if (!getenv ("DISPLAY")) {
71 keypad (stdscr, TRUE);
72 clearok (stdscr, TRUE);
73 set_option (OPTNEEDREDRAW);
77 event_t mutt_getch (void)
80 event_t err = { -1, OP_NULL }, ret;
82 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
83 return (KeyEvent[--UngetCount]);
87 mutt_allow_interrupt (1);
89 /* ncurses 4.2 sends this when the screen is resized */
91 while (ch == KEY_RESIZE)
92 #endif /* KEY_RESIZE */
94 mutt_allow_interrupt (0);
102 if ((ch & 0x80) && option (OPTMETAKEY)) {
103 /* send ALT-x as ESC-x */
105 mutt_ungetch (ch, 0);
113 return (ch == ctrl ('G') ? err : ret);
116 int _mutt_get_field ( /* const */ char *field, char *buf, size_t buflen,
117 int complete, int multiple, char ***files, int *numfiles)
122 ENTER_STATE *es = mutt_new_enter_state ();
125 CLEARLINE (LINES - 1);
128 getyx (stdscr, y, x);
130 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
134 CLEARLINE (LINES - 1);
135 mutt_free_enter_state (&es);
140 int mutt_get_password (char *msg, char *buf, size_t buflen)
144 CLEARLINE (LINES - 1);
146 set_option (OPTUNBUFFEREDINPUT);
147 rc = mutt_enter_string (buf, buflen, LINES - 1, safe_strlen (msg), M_PASS);
148 unset_option (OPTUNBUFFEREDINPUT);
149 CLEARLINE (LINES - 1);
153 void mutt_clear_error (void)
156 if (!option (OPTNOCURSES))
157 CLEARLINE (LINES - 1);
160 static void fix_end_of_file (const char *data)
165 if ((fp = safe_fopen (data, "a+")) == NULL)
167 fseek (fp, -1, SEEK_END);
168 if ((c = fgetc (fp)) != '\n')
173 void mutt_edit_file (const char *editor, const char *data)
175 char cmd[LONG_STRING];
178 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
179 if (mutt_system (cmd) == -1)
180 mutt_error (_("Error running \"%s\"!"), cmd);
181 fix_end_of_file (data);
182 keypad (stdscr, TRUE);
183 clearok (stdscr, TRUE);
186 int mutt_yesorno (const char *msg, int def)
189 char *yes = _("yes");
192 size_t answer_string_len;
194 #ifdef HAVE_LANGINFO_YESEXPR
204 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
205 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
206 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
207 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
210 CLEARLINE (LINES - 1);
213 * In order to prevent the default answer to the question to wrapped
214 * around the screen in the even the question is wider than the screen,
215 * ensure there is enough room for the answer and truncate the question
218 answer_string = safe_malloc (COLS + 1);
219 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
220 def == M_YES ? no : yes);
221 answer_string_len = safe_strlen (answer_string);
222 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
223 FREE (&answer_string);
228 if (CI_is_return (ch.ch))
235 #ifdef HAVE_LANGINFO_YESEXPR
237 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
241 (tolower (ch.ch) == 'y')) {
246 #ifdef HAVE_LANGINFO_YESEXPR
247 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
249 (tolower (ch.ch) == 'n')) {
258 #ifdef HAVE_LANGINFO_YESEXPR
266 addstr ((char *) (def == M_YES ? yes : no));
272 /* this function is called when the user presses the abort key */
273 void mutt_query_exit (void)
278 timeout (-1); /* restore blocking operation */
279 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
288 void mutt_curses_error (const char *fmt, ...)
290 char TmpErrorbuf[STRING];
294 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
297 debug_print (1, ("%s\n", Errorbuf));
298 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
299 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
300 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
302 if (!option (OPTKEEPQUIET)) {
304 SETCOLOR (MT_COLOR_ERROR);
305 mvaddstr (LINES - 1, 0, Errorbuf);
307 SETCOLOR (MT_COLOR_NORMAL);
311 set_option (OPTMSGERR);
314 void mutt_curses_message (const char *fmt, ...)
316 char TmpErrorbuf[STRING];
320 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
323 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
324 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
325 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
327 if (!option (OPTKEEPQUIET)) {
328 SETCOLOR (MT_COLOR_MESSAGE);
329 mvaddstr (LINES - 1, 0, Errorbuf);
331 SETCOLOR (MT_COLOR_NORMAL);
335 unset_option (OPTMSGERR);
338 void mutt_show_error (void)
340 if (option (OPTKEEPQUIET))
343 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
344 CLEARLINE (LINES - 1);
346 SETCOLOR (MT_COLOR_NORMAL);
349 void mutt_endwin (const char *msg)
351 if (!option (OPTNOCURSES)) {
352 CLEARLINE (LINES - 1);
365 void _mutt_perror (const char *s, const char* filename, int line)
367 char *p = strerror (errno);
369 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
370 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
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, int do_color, pager_t * info)
406 if (!Pager || safe_strcmp (Pager, "builtin") == 0)
407 rc = mutt_pager (banner, tempfile, do_color, info);
412 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
413 if (mutt_system (cmd) == -1) {
414 mutt_error (_("Error running \"%s\"!"), cmd);
419 mutt_unlink (tempfile);
425 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
426 int *redraw, int buffy, int multiple, char ***files,
431 mvaddstr (LINES - 1, 0, (char *) prompt);
432 addstr (_(" ('?' for list): "));
440 CLEARLINE (LINES - 1);
443 else if (ch.ch == '?') {
446 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
448 *redraw = REDRAW_FULL;
451 char *pc = safe_malloc (safe_strlen (prompt) + 3);
453 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
454 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
456 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
460 MAYBE_REDRAW (*redraw);
467 void mutt_ungetch (int ch, int op)
474 if (UngetCount >= UngetBufLen)
475 safe_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof (event_t));
477 KeyEvent[UngetCount++] = tmp;
480 void mutt_flushinp (void)
486 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
487 /* The argument can take 3 values:
488 * -1: restore the value of the last call
489 * 0: make the cursor invisible
490 * 1: make the cursor visible
492 void mutt_curs_set (int cursor)
494 static int SavedCursor = 1;
497 cursor = SavedCursor;
499 SavedCursor = cursor;
501 if (curs_set (cursor) == ERR) {
502 if (cursor == 1) /* cnorm */
503 curs_set (2); /* cvvis */
508 int mutt_multi_choice (char *prompt, char *letters)
514 mvaddstr (LINES - 1, 0, prompt);
519 if (ch.ch == -1 || CI_is_return (ch.ch)) {
524 p = strchr (letters, ch.ch);
526 choice = p - letters + 1;
529 else if (ch.ch <= '9' && ch.ch > '0') {
530 choice = ch.ch - '0';
531 if (choice <= safe_strlen (letters))
537 CLEARLINE (LINES - 1);
543 * addwch would be provided by an up-to-date curses library
546 int mutt_addwch (wchar_t wc)
548 char buf[MB_LEN_MAX * 2];
552 memset (&mbstate, 0, sizeof (mbstate));
553 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
554 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
562 * This formats a string, a bit like
563 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
564 * except that the widths refer to the number of character cells
568 void mutt_format_string (char *dest, size_t destlen,
569 int min_width, int max_width,
570 int right_justify, char m_pad_char,
571 const char *s, size_t n, int arboreal)
577 char scratch[MB_LEN_MAX];
578 mbstate_t mbstate1, mbstate2;
580 memset (&mbstate1, 0, sizeof (mbstate1));
581 memset (&mbstate2, 0, sizeof (mbstate2));
584 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
585 if (k == (size_t) (-1) || k == (size_t) (-2)) {
586 k = (k == (size_t) (-1)) ? 1 : n;
587 wc = replacement_char ();
589 if (arboreal && wc < M_TREE_MAX)
597 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
601 strncpy (p, scratch, k2);
606 w = (int) destlen < min_width ? destlen : min_width;
609 else if (right_justify) {
614 dest[w] = m_pad_char;
624 * This formats a string rather like
625 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
626 * snprintf (dest, destlen, fmt, s);
627 * except that the numbers in the conversion specification refer to
628 * the number of character cells when printed.
631 static void mutt_format_s_x (char *dest,
633 const char *prefix, const char *s, int arboreal)
635 int right_justify = 1;
638 int max_width = INT_MAX;
641 ++prefix, right_justify = 0;
642 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, safe_strlen (s), arboreal);
654 void mutt_format_s (char *dest,
655 size_t destlen, const char *prefix, const char *s)
657 mutt_format_s_x (dest, destlen, prefix, s, 0);
660 void mutt_format_s_tree (char *dest,
661 size_t destlen, const char *prefix, const char *s)
663 mutt_format_s_x (dest, destlen, prefix, s, 1);
667 * mutt_paddstr (n, s) is almost equivalent to
668 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
671 void mutt_paddstr (int n, const char *s)
676 size_t len = safe_strlen (s);
679 memset (&mbstate, 0, sizeof (mbstate));
680 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
681 if (k == (size_t) (-1) || k == (size_t) (-2)) {
682 k = (k == (size_t) (-1)) ? 1 : len;
683 wc = replacement_char ();
691 addnstr ((char *) s, k);
700 * mutt_strwidth is like safe_strlen except that it returns the width
701 * refering to the number of characters cells.
704 int mutt_strwidth (const char *s)
716 memset (&mbstate, 0, sizeof (mbstate));
717 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
718 if (k == (size_t) (-1) || k == (size_t) (-2)) {
719 k = (k == (size_t) (-1)) ? 1 : n;
720 wc = replacement_char ();