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/macros.h>
23 #include "mutt_menu.h"
24 #include "mutt_curses.h"
29 #include "lib/debug.h"
33 #include <sys/types.h>
41 #ifdef HAVE_LANGINFO_YESEXPR
45 /* not possible to unget more than one char under some curses libs, and it
46 * is impossible to unget function keys in SLang, so roll our own input
49 size_t UngetCount = 0;
50 static size_t UngetBufLen = 0;
51 static event_t *KeyEvent;
53 void mutt_refresh (void)
55 /* don't refresh when we are waiting for a child. */
56 if (option (OPTKEEPQUIET))
59 /* don't refresh in the middle of macros unless necessary */
60 if (UngetCount && !option (OPTFORCEREFRESH))
67 /* Make sure that the next refresh does a full refresh. This could be
68 optmized by not doing it at all if DISPLAY is set as this might
69 indicate that a GUI based pinentry was used. Having an option to
70 customize this is of course the Mutt way. */
71 void mutt_need_hard_redraw (void)
73 if (!getenv ("DISPLAY")) {
74 keypad (stdscr, TRUE);
75 clearok (stdscr, TRUE);
76 set_option (OPTNEEDREDRAW);
80 event_t mutt_getch (void)
83 event_t err = { -1, OP_NULL }, ret;
85 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
86 return (KeyEvent[--UngetCount]);
90 mutt_allow_interrupt (1);
92 /* ncurses 4.2 sends this when the screen is resized */
94 while (ch == KEY_RESIZE)
95 #endif /* KEY_RESIZE */
97 mutt_allow_interrupt (0);
105 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,
120 int complete, int multiple, char ***files, int *numfiles)
125 ENTER_STATE *es = mutt_new_enter_state ();
128 CLEARLINE (LINES - 1);
131 getyx (stdscr, y, x);
133 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
137 CLEARLINE (LINES - 1);
138 mutt_free_enter_state (&es);
143 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
147 set_option (OPTUNBUFFEREDINPUT);
148 rc = mutt_get_field (msg, buf, buflen, flags);
149 unset_option (OPTUNBUFFEREDINPUT);
154 void mutt_clear_error (void)
157 if (!option (OPTNOCURSES))
158 CLEARLINE (LINES - 1);
161 void mutt_edit_file (const char *editor, const char *data)
163 char cmd[LONG_STRING];
166 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
167 if (mutt_system (cmd) == -1)
168 mutt_error (_("Error running \"%s\"!"), cmd);
169 keypad (stdscr, TRUE);
170 clearok (stdscr, TRUE);
173 int mutt_yesorno (const char *msg, int def)
176 char *yes = _("yes");
179 size_t answer_string_len;
181 #ifdef HAVE_LANGINFO_YESEXPR
191 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
192 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
193 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
194 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
197 CLEARLINE (LINES - 1);
200 * In order to prevent the default answer to the question to wrapped
201 * around the screen in the even the question is wider than the screen,
202 * ensure there is enough room for the answer and truncate the question
205 answer_string = p_new(char, COLS + 1);
206 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
207 def == M_YES ? no : yes);
208 answer_string_len = str_len (answer_string);
209 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
210 p_delete(&answer_string);
215 if (CI_is_return (ch.ch))
222 #ifdef HAVE_LANGINFO_YESEXPR
224 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
228 (tolower (ch.ch) == *yes)) {
233 #ifdef HAVE_LANGINFO_YESEXPR
234 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
236 (tolower (ch.ch) == *no)) {
245 #ifdef HAVE_LANGINFO_YESEXPR
253 addstr ((char *) (def == M_YES ? yes : no));
259 /* this function is called when the user presses the abort key */
260 void mutt_query_exit (void)
265 timeout (-1); /* restore blocking operation */
266 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
275 void mutt_curses_error (const char *fmt, ...)
277 char TmpErrorbuf[STRING];
281 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
284 debug_print (1, ("%s\n", Errorbuf));
285 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
286 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
287 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
289 if (!option (OPTKEEPQUIET)) {
291 SETCOLOR (MT_COLOR_ERROR);
292 mvaddstr (LINES - 1, 0, Errorbuf);
294 SETCOLOR (MT_COLOR_NORMAL);
298 set_option (OPTMSGERR);
302 void mutt_progress_bar (progress_t* progress, long pos) {
303 char posstr[SHORT_STRING];
307 mutt_message (progress->msg);
310 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
319 if (pos >= progress->pos + (NetInc << 10)) {
321 pos = pos / (NetInc << 10) * (NetInc << 10);
322 mutt_pretty_size (posstr, sizeof (posstr), pos);
324 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
326 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 || str_cmp (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, str_len(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 <= str_len (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 memset (&mbstate, 0, sizeof (mbstate));
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, size_t destlen,
586 int min_width, int max_width,
587 int right_justify, char m_pad_char,
588 const char *s, size_t n, int arboreal)
594 char scratch[MB_LEN_MAX];
595 mbstate_t mbstate1, mbstate2;
597 memset (&mbstate1, 0, sizeof (mbstate1));
598 memset (&mbstate2, 0, sizeof (mbstate2));
601 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
602 if (k == (size_t) (-1) || k == (size_t) (-2)) {
603 k = (k == (size_t) (-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 = (int) 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, str_len (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 = str_len (s);
696 memset (&mbstate, 0, sizeof (mbstate));
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 str_len except that it returns the width
718 * refering to the number of characters cells.
721 int mutt_strwidth (const char *s)
733 memset (&mbstate, 0, sizeof (mbstate));
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 ();