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>
25 #include "mutt_menu.h"
26 #include "mutt_curses.h"
30 #include "lib/debug.h"
34 #include <sys/types.h>
42 #ifdef HAVE_LANGINFO_YESEXPR
46 /* not possible to unget more than one char under some curses libs, and it
47 * is impossible to unget function keys in SLang, so roll our own input
50 size_t UngetCount = 0;
51 static size_t UngetBufLen = 0;
52 static event_t *KeyEvent;
54 void mutt_refresh (void)
56 /* don't refresh when we are waiting for a child. */
57 if (option (OPTKEEPQUIET))
60 /* don't refresh in the middle of macros unless necessary */
61 if (UngetCount && !option (OPTFORCEREFRESH))
68 /* Make sure that the next refresh does a full refresh. This could be
69 optmized by not doing it at all if DISPLAY is set as this might
70 indicate that a GUI based pinentry was used. Having an option to
71 customize this is of course the Mutt way. */
72 void mutt_need_hard_redraw (void)
74 if (!getenv ("DISPLAY")) {
75 keypad (stdscr, TRUE);
76 clearok (stdscr, TRUE);
77 set_option (OPTNEEDREDRAW);
81 event_t mutt_getch (void)
84 event_t err = { -1, OP_NULL }, ret;
86 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
87 return (KeyEvent[--UngetCount]);
91 mutt_allow_interrupt (1);
93 /* ncurses 4.2 sends this when the screen is resized */
95 while (ch == KEY_RESIZE)
96 #endif /* KEY_RESIZE */
98 mutt_allow_interrupt (0);
106 if ((ch & 0x80) && option (OPTMETAKEY)) {
107 /* send ALT-x as ESC-x */
109 mutt_ungetch (ch, 0);
117 return (ch == ctrl ('G') ? err : ret);
120 int _mutt_get_field ( const char *field, char *buf, size_t buflen,
121 int complete, int multiple, char ***files, int *numfiles)
126 ENTER_STATE *es = mutt_new_enter_state ();
129 CLEARLINE (LINES - 1);
132 getyx (stdscr, y, x);
134 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
138 CLEARLINE (LINES - 1);
139 mutt_free_enter_state (&es);
144 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
148 set_option (OPTUNBUFFEREDINPUT);
149 rc = mutt_get_field (msg, buf, buflen, flags);
150 unset_option (OPTUNBUFFEREDINPUT);
155 void mutt_clear_error (void)
158 if (!option (OPTNOCURSES))
159 CLEARLINE (LINES - 1);
162 void mutt_edit_file (const char *editor, const char *data)
164 char cmd[LONG_STRING];
167 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
168 if (mutt_system (cmd) == -1)
169 mutt_error (_("Error running \"%s\"!"), cmd);
170 keypad (stdscr, TRUE);
171 clearok (stdscr, TRUE);
174 int mutt_yesorno (const char *msg, int def)
177 char *yes = _("yes");
180 size_t answer_string_len;
182 #ifdef HAVE_LANGINFO_YESEXPR
192 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
193 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
194 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
195 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
198 CLEARLINE (LINES - 1);
201 * In order to prevent the default answer to the question to wrapped
202 * around the screen in the even the question is wider than the screen,
203 * ensure there is enough room for the answer and truncate the question
206 answer_string = p_new(char, COLS + 1);
207 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
208 def == M_YES ? no : yes);
209 answer_string_len = m_strlen(answer_string);
210 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
211 p_delete(&answer_string);
216 if (CI_is_return (ch.ch))
223 #ifdef HAVE_LANGINFO_YESEXPR
225 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
229 (tolower (ch.ch) == *yes)) {
234 #ifdef HAVE_LANGINFO_YESEXPR
235 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
237 (tolower (ch.ch) == *no)) {
246 #ifdef HAVE_LANGINFO_YESEXPR
254 addstr ((char *) (def == M_YES ? yes : no));
260 /* this function is called when the user presses the abort key */
261 void mutt_query_exit (void)
266 timeout (-1); /* restore blocking operation */
267 if (mutt_yesorno (_("Exit Mutt-ng?"), M_YES) == M_YES) {
276 void mutt_curses_error (const char *fmt, ...)
278 char TmpErrorbuf[STRING];
282 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
285 debug_print (1, ("%s\n", Errorbuf));
286 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
287 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
288 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
290 if (!option (OPTKEEPQUIET)) {
292 SETCOLOR (MT_COLOR_ERROR);
293 mvaddstr (LINES - 1, 0, Errorbuf);
295 SETCOLOR (MT_COLOR_NORMAL);
299 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);
332 void mutt_curses_message (const char *fmt, ...)
334 char TmpErrorbuf[STRING];
338 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
341 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
342 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
343 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
345 if (!option (OPTKEEPQUIET)) {
346 SETCOLOR (MT_COLOR_MESSAGE);
347 mvaddstr (LINES - 1, 0, Errorbuf);
349 SETCOLOR (MT_COLOR_NORMAL);
353 unset_option (OPTMSGERR);
356 void mutt_show_error (void)
358 if (option (OPTKEEPQUIET))
361 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
362 CLEARLINE (LINES - 1);
364 SETCOLOR (MT_COLOR_NORMAL);
367 void mutt_endwin (const char *msg)
369 if (!option (OPTNOCURSES)) {
370 CLEARLINE (LINES - 1);
383 void _mutt_perror (const char *s, const char* filename, int line)
385 char *p = strerror (errno);
387 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
388 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
391 int mutt_any_key_to_continue (const char *s)
397 f = open ("/dev/tty", O_RDONLY);
399 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
400 t.c_lflag &= ~(ICANON | ECHO);
403 tcsetattr (f, TCSADRAIN, &t);
408 fputs (_("Press any key to continue..."), stdout);
412 tcsetattr (f, TCSADRAIN, &old);
414 fputs ("\r\n", stdout);
419 int mutt_do_pager (const char *banner,
420 const char *tempfile, int do_color, pager_t * info)
424 if (!Pager || m_strcmp(Pager, "builtin") == 0)
425 rc = mutt_pager (banner, tempfile, do_color, info);
430 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
431 if (mutt_system (cmd) == -1) {
432 mutt_error (_("Error running \"%s\"!"), cmd);
437 mutt_unlink (tempfile);
443 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
444 int *redraw, int buffy, int multiple, char ***files,
449 mvaddstr (LINES - 1, 0, (char *) prompt);
450 addstr (_(" ('?' for list): "));
458 CLEARLINE (LINES - 1);
461 else if (ch.ch == '?') {
464 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
466 *redraw = REDRAW_FULL;
469 char *pc = p_new(char, m_strlen(prompt) + 3);
471 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
472 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
474 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
478 MAYBE_REDRAW (*redraw);
485 void mutt_ungetch (int ch, int op)
492 if (UngetCount >= UngetBufLen)
493 p_realloc(&KeyEvent, UngetBufLen += 128);
495 KeyEvent[UngetCount++] = tmp;
498 void mutt_flushinp (void)
504 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
505 /* The argument can take 3 values:
506 * -1: restore the value of the last call
507 * 0: make the cursor invisible
508 * 1: make the cursor visible
510 void mutt_curs_set (int cursor)
512 static int SavedCursor = 1;
515 cursor = SavedCursor;
517 SavedCursor = cursor;
519 if (curs_set (cursor) == ERR) {
520 if (cursor == 1) /* cnorm */
521 curs_set (2); /* cvvis */
526 int mutt_multi_choice (char *prompt, char *letters)
532 mvaddstr (LINES - 1, 0, prompt);
537 if (ch.ch == -1 || CI_is_return (ch.ch)) {
542 p = strchr (letters, ch.ch);
544 choice = p - letters + 1;
547 else if (ch.ch <= '9' && ch.ch > '0') {
548 choice = ch.ch - '0';
549 if (choice <= m_strlen(letters))
555 CLEARLINE (LINES - 1);
561 * addwch would be provided by an up-to-date curses library
564 int mutt_addwch (wchar_t wc)
566 char buf[MB_LEN_MAX * 2];
570 p_clear(&mbstate, 1);
571 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
572 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
580 * This formats a string, a bit like
581 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
582 * except that the widths refer to the number of character cells
586 void mutt_format_string (char *dest, size_t destlen,
587 int min_width, int max_width,
588 int right_justify, char m_pad_char,
589 const char *s, size_t n, int arboreal)
595 char scratch[MB_LEN_MAX];
596 mbstate_t mbstate1, mbstate2;
598 p_clear(&mbstate1, 1);
599 p_clear(&mbstate2, 1);
602 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
603 if (k == (size_t) (-1) || k == (size_t) (-2)) {
604 k = (k == (size_t) (-1)) ? 1 : n;
605 wc = replacement_char ();
607 if (arboreal && wc < M_TREE_MAX)
615 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
619 strncpy (p, scratch, k2);
624 w = (int) destlen < min_width ? destlen : min_width;
627 else if (right_justify) {
632 dest[w] = m_pad_char;
642 * This formats a string rather like
643 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
644 * snprintf (dest, destlen, fmt, s);
645 * except that the numbers in the conversion specification refer to
646 * the number of character cells when printed.
649 static void mutt_format_s_x (char *dest,
651 const char *prefix, const char *s, int arboreal)
653 int right_justify = 1;
656 int max_width = INT_MAX;
659 ++prefix, right_justify = 0;
660 min_width = strtol (prefix, &p, 10);
663 max_width = strtol (prefix, &p, 10);
668 mutt_format_string (dest, destlen, min_width, max_width,
669 right_justify, ' ', s, m_strlen(s), arboreal);
672 void mutt_format_s (char *dest,
673 size_t destlen, const char *prefix, const char *s)
675 mutt_format_s_x (dest, destlen, prefix, s, 0);
678 void mutt_format_s_tree (char *dest,
679 size_t destlen, const char *prefix, const char *s)
681 mutt_format_s_x (dest, destlen, prefix, s, 1);
685 * mutt_paddstr (n, s) is almost equivalent to
686 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
689 void mutt_paddstr (int n, const char *s)
694 size_t len = m_strlen(s);
697 p_clear(&mbstate, 1);
698 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
699 if (k == (size_t) (-1) || k == (size_t) (-2)) {
700 k = (k == (size_t) (-1)) ? 1 : len;
701 wc = replacement_char ();
709 addnstr ((char *) s, k);
718 * mutt_strwidth is like m_strlenexcept that it returns the width
719 * refering to the number of characters cells.
722 int mutt_strwidth (const char *s)
734 p_clear(&mbstate, 1);
735 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
736 if (k == (size_t) (-1) || k == (size_t) (-2)) {
737 k = (k == (size_t) (-1)) ? 1 : n;
738 wc = replacement_char ();