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.
20 #include "mutt_menu.h"
21 #include "mutt_curses.h"
28 #include "lib/debug.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")) {
72 keypad (stdscr, TRUE);
73 clearok (stdscr, TRUE);
74 set_option (OPTNEEDREDRAW);
78 event_t mutt_getch (void)
81 event_t err = { -1, OP_NULL }, ret;
83 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
84 return (KeyEvent[--UngetCount]);
88 mutt_allow_interrupt (1);
90 /* ncurses 4.2 sends this when the screen is resized */
92 while (ch == KEY_RESIZE)
93 #endif /* KEY_RESIZE */
95 mutt_allow_interrupt (0);
103 if ((ch & 0x80) && option (OPTMETAKEY)) {
104 /* send ALT-x as ESC-x */
106 mutt_ungetch (ch, 0);
114 return (ch == ctrl ('G') ? err : ret);
117 int _mutt_get_field ( /* const */ char *field, char *buf, size_t buflen,
118 int complete, int multiple, char ***files, int *numfiles)
123 ENTER_STATE *es = mutt_new_enter_state ();
126 CLEARLINE (LINES - 1);
129 getyx (stdscr, y, x);
131 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
135 CLEARLINE (LINES - 1);
136 mutt_free_enter_state (&es);
141 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
145 set_option (OPTUNBUFFEREDINPUT);
146 rc = mutt_get_field (msg, buf, buflen, flags);
147 unset_option (OPTUNBUFFEREDINPUT);
152 void mutt_clear_error (void)
155 if (!option (OPTNOCURSES))
156 CLEARLINE (LINES - 1);
159 void mutt_edit_file (const char *editor, const char *data)
161 char cmd[LONG_STRING];
164 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
165 if (mutt_system (cmd) == -1)
166 mutt_error (_("Error running \"%s\"!"), cmd);
167 keypad (stdscr, TRUE);
168 clearok (stdscr, TRUE);
171 int mutt_yesorno (const char *msg, int def)
174 char *yes = _("yes");
177 size_t answer_string_len;
179 #ifdef HAVE_LANGINFO_YESEXPR
189 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
190 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
191 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
192 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
195 CLEARLINE (LINES - 1);
198 * In order to prevent the default answer to the question to wrapped
199 * around the screen in the even the question is wider than the screen,
200 * ensure there is enough room for the answer and truncate the question
203 answer_string = mem_malloc (COLS + 1);
204 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
205 def == M_YES ? no : yes);
206 answer_string_len = str_len (answer_string);
207 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
208 mem_free (&answer_string);
213 if (CI_is_return (ch.ch))
220 #ifdef HAVE_LANGINFO_YESEXPR
222 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
226 (tolower (ch.ch) == *yes)) {
231 #ifdef HAVE_LANGINFO_YESEXPR
232 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
234 (tolower (ch.ch) == *no)) {
243 #ifdef HAVE_LANGINFO_YESEXPR
251 addstr ((char *) (def == M_YES ? yes : no));
257 /* this function is called when the user presses the abort key */
258 void mutt_query_exit (void)
263 timeout (-1); /* restore blocking operation */
264 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
273 void mutt_curses_error (const char *fmt, ...)
275 char TmpErrorbuf[STRING];
279 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
282 debug_print (1, ("%s\n", Errorbuf));
283 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
284 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
285 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
287 if (!option (OPTKEEPQUIET)) {
289 SETCOLOR (MT_COLOR_ERROR);
290 mvaddstr (LINES - 1, 0, Errorbuf);
292 SETCOLOR (MT_COLOR_NORMAL);
296 set_option (OPTMSGERR);
300 void mutt_progress_bar (progress_t* progress, long pos) {
301 char posstr[SHORT_STRING];
305 mutt_message (progress->msg);
308 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
317 if (pos >= progress->pos + (NetInc << 10)) {
319 pos = pos / (NetInc << 10) * (NetInc << 10);
320 mutt_pretty_size (posstr, sizeof (posstr), pos);
322 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
324 mutt_message ("%s %s", progress->msg, posstr);
329 void mutt_curses_message (const char *fmt, ...)
331 char TmpErrorbuf[STRING];
335 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
338 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
339 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
340 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
342 if (!option (OPTKEEPQUIET)) {
343 SETCOLOR (MT_COLOR_MESSAGE);
344 mvaddstr (LINES - 1, 0, Errorbuf);
346 SETCOLOR (MT_COLOR_NORMAL);
350 unset_option (OPTMSGERR);
353 void mutt_show_error (void)
355 if (option (OPTKEEPQUIET))
358 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
359 CLEARLINE (LINES - 1);
361 SETCOLOR (MT_COLOR_NORMAL);
364 void mutt_endwin (const char *msg)
366 if (!option (OPTNOCURSES)) {
367 CLEARLINE (LINES - 1);
380 void _mutt_perror (const char *s, const char* filename, int line)
382 char *p = strerror (errno);
384 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
385 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
388 int mutt_any_key_to_continue (const char *s)
394 f = open ("/dev/tty", O_RDONLY);
396 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
397 t.c_lflag &= ~(ICANON | ECHO);
400 tcsetattr (f, TCSADRAIN, &t);
405 fputs (_("Press any key to continue..."), stdout);
409 tcsetattr (f, TCSADRAIN, &old);
411 fputs ("\r\n", stdout);
416 int mutt_do_pager (const char *banner,
417 const char *tempfile, int do_color, pager_t * info)
421 if (!Pager || str_cmp (Pager, "builtin") == 0)
422 rc = mutt_pager (banner, tempfile, do_color, info);
427 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
428 if (mutt_system (cmd) == -1) {
429 mutt_error (_("Error running \"%s\"!"), cmd);
434 mutt_unlink (tempfile);
440 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
441 int *redraw, int buffy, int multiple, char ***files,
446 mvaddstr (LINES - 1, 0, (char *) prompt);
447 addstr (_(" ('?' for list): "));
455 CLEARLINE (LINES - 1);
458 else if (ch.ch == '?') {
461 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
463 *redraw = REDRAW_FULL;
466 char *pc = mem_malloc (str_len (prompt) + 3);
468 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
469 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
471 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
475 MAYBE_REDRAW (*redraw);
482 void mutt_ungetch (int ch, int op)
489 if (UngetCount >= UngetBufLen)
490 mem_realloc (&KeyEvent, (UngetBufLen += 128) * sizeof (event_t));
492 KeyEvent[UngetCount++] = tmp;
495 void mutt_flushinp (void)
501 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
502 /* The argument can take 3 values:
503 * -1: restore the value of the last call
504 * 0: make the cursor invisible
505 * 1: make the cursor visible
507 void mutt_curs_set (int cursor)
509 static int SavedCursor = 1;
512 cursor = SavedCursor;
514 SavedCursor = cursor;
516 if (curs_set (cursor) == ERR) {
517 if (cursor == 1) /* cnorm */
518 curs_set (2); /* cvvis */
523 int mutt_multi_choice (char *prompt, char *letters)
529 mvaddstr (LINES - 1, 0, prompt);
534 if (ch.ch == -1 || CI_is_return (ch.ch)) {
539 p = strchr (letters, ch.ch);
541 choice = p - letters + 1;
544 else if (ch.ch <= '9' && ch.ch > '0') {
545 choice = ch.ch - '0';
546 if (choice <= str_len (letters))
552 CLEARLINE (LINES - 1);
558 * addwch would be provided by an up-to-date curses library
561 int mutt_addwch (wchar_t wc)
563 char buf[MB_LEN_MAX * 2];
567 memset (&mbstate, 0, sizeof (mbstate));
568 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
569 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
577 * This formats a string, a bit like
578 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
579 * except that the widths refer to the number of character cells
583 void mutt_format_string (char *dest, size_t destlen,
584 int min_width, int max_width,
585 int right_justify, char m_pad_char,
586 const char *s, size_t n, int arboreal)
592 char scratch[MB_LEN_MAX];
593 mbstate_t mbstate1, mbstate2;
595 memset (&mbstate1, 0, sizeof (mbstate1));
596 memset (&mbstate2, 0, sizeof (mbstate2));
599 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
600 if (k == (size_t) (-1) || k == (size_t) (-2)) {
601 k = (k == (size_t) (-1)) ? 1 : n;
602 wc = replacement_char ();
604 if (arboreal && wc < M_TREE_MAX)
612 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
616 strncpy (p, scratch, k2);
621 w = (int) destlen < min_width ? destlen : min_width;
624 else if (right_justify) {
629 dest[w] = m_pad_char;
639 * This formats a string rather like
640 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
641 * snprintf (dest, destlen, fmt, s);
642 * except that the numbers in the conversion specification refer to
643 * the number of character cells when printed.
646 static void mutt_format_s_x (char *dest,
648 const char *prefix, const char *s, int arboreal)
650 int right_justify = 1;
653 int max_width = INT_MAX;
656 ++prefix, right_justify = 0;
657 min_width = strtol (prefix, &p, 10);
660 max_width = strtol (prefix, &p, 10);
665 mutt_format_string (dest, destlen, min_width, max_width,
666 right_justify, ' ', s, str_len (s), arboreal);
669 void mutt_format_s (char *dest,
670 size_t destlen, const char *prefix, const char *s)
672 mutt_format_s_x (dest, destlen, prefix, s, 0);
675 void mutt_format_s_tree (char *dest,
676 size_t destlen, const char *prefix, const char *s)
678 mutt_format_s_x (dest, destlen, prefix, s, 1);
682 * mutt_paddstr (n, s) is almost equivalent to
683 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
686 void mutt_paddstr (int n, const char *s)
691 size_t len = str_len (s);
694 memset (&mbstate, 0, sizeof (mbstate));
695 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
696 if (k == (size_t) (-1) || k == (size_t) (-2)) {
697 k = (k == (size_t) (-1)) ? 1 : len;
698 wc = replacement_char ();
706 addnstr ((char *) s, k);
715 * mutt_strwidth is like str_len except that it returns the width
716 * refering to the number of characters cells.
719 int mutt_strwidth (const char *s)
731 memset (&mbstate, 0, sizeof (mbstate));
732 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
733 if (k == (size_t) (-1) || k == (size_t) (-2)) {
734 k = (k == (size_t) (-1)) ? 1 : n;
735 wc = replacement_char ();