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.
18 #include <lib-lib/mem.h>
19 #include <lib-lib/str.h>
20 #include <lib-lib/macros.h>
21 #include <lib-lib/file.h>
23 #include <lib-ui/curses.h>
24 #include <lib-ui/enter.h>
27 #include "mutt_menu.h"
31 #include "lib/debug.h"
35 #include <sys/types.h>
43 #ifdef HAVE_LANGINFO_YESEXPR
47 /* not possible to unget more than one char under some curses libs, and it
48 * is impossible to unget function keys in SLang, so roll our own input
51 size_t UngetCount = 0;
52 static size_t UngetBufLen = 0;
53 static event_t *KeyEvent;
55 void mutt_refresh (void)
57 /* don't refresh when we are waiting for a child. */
58 if (option (OPTKEEPQUIET))
61 /* don't refresh in the middle of macros unless necessary */
62 if (UngetCount && !option (OPTFORCEREFRESH))
69 /* Make sure that the next refresh does a full refresh. This could be
70 optmized by not doing it at all if DISPLAY is set as this might
71 indicate that a GUI based pinentry was used. Having an option to
72 customize this is of course the Mutt way. */
73 void mutt_need_hard_redraw (void)
75 if (!getenv ("DISPLAY")) {
76 keypad (stdscr, TRUE);
77 clearok (stdscr, TRUE);
78 set_option (OPTNEEDREDRAW);
82 event_t mutt_getch (void)
85 event_t err = { -1, OP_NULL }, ret;
87 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
88 return (KeyEvent[--UngetCount]);
92 mutt_allow_interrupt (1);
94 /* ncurses 4.2 sends this when the screen is resized */
96 while (ch == KEY_RESIZE)
97 #endif /* KEY_RESIZE */
99 mutt_allow_interrupt (0);
107 if ((ch & 0x80) && option (OPTMETAKEY)) {
108 /* send ALT-x as ESC-x */
110 mutt_ungetch (ch, 0);
118 return (ch == ctrl ('G') ? err : ret);
121 int _mutt_get_field ( const char *field, char *buf, size_t buflen,
122 int complete, int multiple, char ***files, int *numfiles)
127 ENTER_STATE *es = mutt_new_enter_state ();
130 CLEARLINE (LINES - 1);
133 getyx (stdscr, y, x);
135 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
139 CLEARLINE (LINES - 1);
140 mutt_free_enter_state (&es);
145 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
149 set_option (OPTUNBUFFEREDINPUT);
150 rc = mutt_get_field (msg, buf, buflen, flags);
151 unset_option (OPTUNBUFFEREDINPUT);
156 void mutt_clear_error (void)
159 if (!option (OPTNOCURSES))
160 CLEARLINE (LINES - 1);
163 void mutt_edit_file (const char *editor, const char *data)
165 char cmd[LONG_STRING];
168 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
169 if (mutt_system (cmd) == -1)
170 mutt_error (_("Error running \"%s\"!"), cmd);
171 keypad (stdscr, TRUE);
172 clearok (stdscr, TRUE);
175 int mutt_yesorno (const char *msg, int def)
178 char *yes = _("yes");
181 size_t answer_string_len;
183 #ifdef HAVE_LANGINFO_YESEXPR
193 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
194 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
195 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
196 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
199 CLEARLINE (LINES - 1);
202 * In order to prevent the default answer to the question to wrapped
203 * around the screen in the even the question is wider than the screen,
204 * ensure there is enough room for the answer and truncate the question
207 answer_string = p_new(char, COLS + 1);
208 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
209 def == M_YES ? no : yes);
210 answer_string_len = m_strlen(answer_string);
211 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
212 p_delete(&answer_string);
217 if (CI_is_return (ch.ch))
224 #ifdef HAVE_LANGINFO_YESEXPR
226 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
230 (tolower (ch.ch) == *yes)) {
235 #ifdef HAVE_LANGINFO_YESEXPR
236 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
238 (tolower (ch.ch) == *no)) {
247 #ifdef HAVE_LANGINFO_YESEXPR
255 addstr ((char *) (def == M_YES ? yes : no));
261 /* this function is called when the user presses the abort key */
262 void mutt_query_exit (void)
267 timeout (-1); /* restore blocking operation */
268 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
277 void mutt_curses_error (const char *fmt, ...)
279 char TmpErrorbuf[STRING];
283 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
286 debug_print (1, ("%s\n", Errorbuf));
287 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
288 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
289 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
291 if (!option (OPTKEEPQUIET)) {
293 SETCOLOR (MT_COLOR_ERROR);
294 mvaddstr (LINES - 1, 0, Errorbuf);
296 SETCOLOR (MT_COLOR_NORMAL);
300 set_option (OPTMSGERR);
303 void mutt_progress_bar (progress_t* progress, long pos) {
304 char posstr[SHORT_STRING];
308 mutt_message (progress->msg);
311 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
320 if (pos >= progress->pos + (NetInc << 10)) {
322 pos = pos / (NetInc << 10) * (NetInc << 10);
323 mutt_pretty_size (posstr, sizeof (posstr), pos);
325 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
327 mutt_message ("%s %s", progress->msg, posstr);
331 void mutt_curses_message (const char *fmt, ...)
333 char TmpErrorbuf[STRING];
337 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
340 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
341 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
342 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
344 if (!option (OPTKEEPQUIET)) {
345 SETCOLOR (MT_COLOR_MESSAGE);
346 mvaddstr (LINES - 1, 0, Errorbuf);
348 SETCOLOR (MT_COLOR_NORMAL);
352 unset_option (OPTMSGERR);
355 void mutt_show_error (void)
357 if (option (OPTKEEPQUIET))
360 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
361 CLEARLINE (LINES - 1);
363 SETCOLOR (MT_COLOR_NORMAL);
366 void mutt_endwin (const char *msg)
368 if (!option (OPTNOCURSES)) {
369 CLEARLINE (LINES - 1);
382 void _mutt_perror (const char *s, const char* filename, int line)
384 char *p = strerror (errno);
386 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
387 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
390 int mutt_any_key_to_continue (const char *s)
396 f = open ("/dev/tty", O_RDONLY);
398 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
399 t.c_lflag &= ~(ICANON | ECHO);
402 tcsetattr (f, TCSADRAIN, &t);
407 fputs (_("Press any key to continue..."), stdout);
411 tcsetattr (f, TCSADRAIN, &old);
413 fputs ("\r\n", stdout);
418 int mutt_do_pager (const char *banner,
419 const char *tempfile, int do_color, pager_t * info)
423 if (!Pager || m_strcmp(Pager, "builtin") == 0)
424 rc = mutt_pager (banner, tempfile, do_color, info);
429 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
430 if (mutt_system (cmd) == -1) {
431 mutt_error (_("Error running \"%s\"!"), cmd);
436 mutt_unlink (tempfile);
442 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
443 int *redraw, int buffy, int multiple, char ***files,
448 mvaddstr (LINES - 1, 0, (char *) prompt);
449 addstr (_(" ('?' for list): "));
457 CLEARLINE (LINES - 1);
460 else if (ch.ch == '?') {
463 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
465 *redraw = REDRAW_FULL;
468 char *pc = p_new(char, m_strlen(prompt) + 3);
470 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
471 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
473 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
477 MAYBE_REDRAW (*redraw);
484 void mutt_ungetch (int ch, int op)
491 if (UngetCount >= UngetBufLen)
492 p_realloc(&KeyEvent, UngetBufLen += 128);
494 KeyEvent[UngetCount++] = tmp;
497 void mutt_flushinp (void)
503 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
504 /* The argument can take 3 values:
505 * -1: restore the value of the last call
506 * 0: make the cursor invisible
507 * 1: make the cursor visible
509 void mutt_curs_set (int cursor)
511 static int SavedCursor = 1;
514 cursor = SavedCursor;
516 SavedCursor = cursor;
518 if (curs_set (cursor) == ERR) {
519 if (cursor == 1) /* cnorm */
520 curs_set (2); /* cvvis */
525 int mutt_multi_choice (char *prompt, char *letters)
531 mvaddstr (LINES - 1, 0, prompt);
536 if (ch.ch == -1 || CI_is_return (ch.ch)) {
541 p = strchr (letters, ch.ch);
543 choice = p - letters + 1;
546 else if (ch.ch <= '9' && ch.ch > '0') {
547 choice = ch.ch - '0';
548 if (choice <= m_strlen(letters))
554 CLEARLINE (LINES - 1);
560 * addwch would be provided by an up-to-date curses library
563 int mutt_addwch (wchar_t wc)
565 char buf[MB_LEN_MAX * 2];
569 p_clear(&mbstate, 1);
570 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
571 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
579 * This formats a string, a bit like
580 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
581 * except that the widths refer to the number of character cells
585 void mutt_format_string (char *dest, ssize_t destlen,
586 int min_width, int max_width,
587 int right_justify, char m_pad_char,
588 const char *s, ssize_t n, int arboreal)
594 char scratch[MB_LEN_MAX];
595 mbstate_t mbstate1, mbstate2;
597 p_clear(&mbstate1, 1);
598 p_clear(&mbstate2, 1);
601 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
602 if (k == -1 || k == -2) {
603 k = (k == -1) ? 1 : n;
604 wc = replacement_char ();
606 if (arboreal && wc < M_TREE_MAX)
614 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
618 strncpy (p, scratch, k2);
623 w = destlen < min_width ? destlen : min_width;
626 else if (right_justify) {
631 dest[w] = m_pad_char;
641 * This formats a string rather like
642 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
643 * snprintf (dest, destlen, fmt, s);
644 * except that the numbers in the conversion specification refer to
645 * the number of character cells when printed.
648 static void mutt_format_s_x (char *dest,
650 const char *prefix, const char *s, int arboreal)
652 int right_justify = 1;
655 int max_width = INT_MAX;
658 ++prefix, right_justify = 0;
659 min_width = strtol (prefix, &p, 10);
662 max_width = strtol (prefix, &p, 10);
667 mutt_format_string (dest, destlen, min_width, max_width,
668 right_justify, ' ', s, m_strlen(s), arboreal);
671 void mutt_format_s (char *dest,
672 size_t destlen, const char *prefix, const char *s)
674 mutt_format_s_x (dest, destlen, prefix, s, 0);
677 void mutt_format_s_tree (char *dest,
678 size_t destlen, const char *prefix, const char *s)
680 mutt_format_s_x (dest, destlen, prefix, s, 1);
684 * mutt_paddstr (n, s) is almost equivalent to
685 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
688 void mutt_paddstr (int n, const char *s)
693 size_t len = m_strlen(s);
696 p_clear(&mbstate, 1);
697 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
698 if (k == (size_t) (-1) || k == (size_t) (-2)) {
699 k = (k == (size_t) (-1)) ? 1 : len;
700 wc = replacement_char ();
708 addnstr ((char *) s, k);
717 * mutt_strwidth is like m_strlenexcept that it returns the width
718 * refering to the number of characters cells.
721 int mutt_strwidth (const char *s)
733 p_clear(&mbstate, 1);
734 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
735 if (k == (size_t) (-1) || k == (size_t) (-2)) {
736 k = (k == (size_t) (-1)) ? 1 : n;
737 wc = replacement_char ();