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>
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 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_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
144 set_option (OPTUNBUFFEREDINPUT);
145 rc = mutt_get_field (msg, buf, buflen, flags);
146 unset_option (OPTUNBUFFEREDINPUT);
151 void mutt_clear_error (void)
154 if (!option (OPTNOCURSES))
155 CLEARLINE (LINES - 1);
158 void mutt_edit_file (const char *editor, const char *data)
160 char cmd[LONG_STRING];
163 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
164 if (mutt_system (cmd) == -1)
165 mutt_error (_("Error running \"%s\"!"), cmd);
166 keypad (stdscr, TRUE);
167 clearok (stdscr, TRUE);
170 int mutt_yesorno (const char *msg, int def)
173 char *yes = _("yes");
176 size_t answer_string_len;
178 #ifdef HAVE_LANGINFO_YESEXPR
188 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
189 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
190 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
191 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
194 CLEARLINE (LINES - 1);
197 * In order to prevent the default answer to the question to wrapped
198 * around the screen in the even the question is wider than the screen,
199 * ensure there is enough room for the answer and truncate the question
202 answer_string = p_new(char, COLS + 1);
203 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
204 def == M_YES ? no : yes);
205 answer_string_len = m_strlen(answer_string);
206 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
207 p_delete(&answer_string);
212 if (CI_is_return (ch.ch))
219 #ifdef HAVE_LANGINFO_YESEXPR
221 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
225 (tolower (ch.ch) == *yes)) {
230 #ifdef HAVE_LANGINFO_YESEXPR
231 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
233 (tolower (ch.ch) == *no)) {
242 #ifdef HAVE_LANGINFO_YESEXPR
250 addstr ((char *) (def == M_YES ? yes : no));
256 /* this function is called when the user presses the abort key */
257 void mutt_query_exit (void)
262 timeout (-1); /* restore blocking operation */
263 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
272 void mutt_curses_error (const char *fmt, ...)
274 char TmpErrorbuf[STRING];
278 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
281 debug_print (1, ("%s\n", Errorbuf));
282 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
283 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
284 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
286 if (!option (OPTKEEPQUIET)) {
288 SETCOLOR (MT_COLOR_ERROR);
289 mvaddstr (LINES - 1, 0, Errorbuf);
291 SETCOLOR (MT_COLOR_NORMAL);
295 set_option (OPTMSGERR);
298 void mutt_progress_bar (progress_t* progress, long pos) {
299 char posstr[SHORT_STRING];
303 mutt_message (progress->msg);
306 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
315 if (pos >= progress->pos + (NetInc << 10)) {
317 pos = pos / (NetInc << 10) * (NetInc << 10);
318 mutt_pretty_size (posstr, sizeof (posstr), pos);
320 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
322 mutt_message ("%s %s", progress->msg, posstr);
326 void mutt_curses_message (const char *fmt, ...)
328 char TmpErrorbuf[STRING];
332 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
335 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
336 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
337 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
339 if (!option (OPTKEEPQUIET)) {
340 SETCOLOR (MT_COLOR_MESSAGE);
341 mvaddstr (LINES - 1, 0, Errorbuf);
343 SETCOLOR (MT_COLOR_NORMAL);
347 unset_option (OPTMSGERR);
350 void mutt_show_error (void)
352 if (option (OPTKEEPQUIET))
355 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
356 CLEARLINE (LINES - 1);
358 SETCOLOR (MT_COLOR_NORMAL);
361 void mutt_endwin (const char *msg)
363 if (!option (OPTNOCURSES)) {
364 CLEARLINE (LINES - 1);
377 void _mutt_perror (const char *s, const char* filename, int line)
379 char *p = strerror (errno);
381 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
382 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
385 int mutt_any_key_to_continue (const char *s)
391 f = open ("/dev/tty", O_RDONLY);
393 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
394 t.c_lflag &= ~(ICANON | ECHO);
397 tcsetattr (f, TCSADRAIN, &t);
402 fputs (_("Press any key to continue..."), stdout);
406 tcsetattr (f, TCSADRAIN, &old);
408 fputs ("\r\n", stdout);
413 int mutt_do_pager (const char *banner,
414 const char *tempfile, int do_color, pager_t * info)
418 if (!Pager || m_strcmp(Pager, "builtin") == 0)
419 rc = mutt_pager (banner, tempfile, do_color, info);
424 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
425 if (mutt_system (cmd) == -1) {
426 mutt_error (_("Error running \"%s\"!"), cmd);
431 mutt_unlink (tempfile);
437 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
438 int *redraw, int buffy, int multiple, char ***files,
443 mvaddstr (LINES - 1, 0, (char *) prompt);
444 addstr (_(" ('?' for list): "));
452 CLEARLINE (LINES - 1);
455 else if (ch.ch == '?') {
458 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
460 *redraw = REDRAW_FULL;
463 char *pc = p_new(char, m_strlen(prompt) + 3);
465 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
466 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
468 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
472 MAYBE_REDRAW (*redraw);
479 void mutt_ungetch (int ch, int op)
486 if (UngetCount >= UngetBufLen)
487 p_realloc(&KeyEvent, UngetBufLen += 128);
489 KeyEvent[UngetCount++] = tmp;
492 void mutt_flushinp (void)
498 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
499 /* The argument can take 3 values:
500 * -1: restore the value of the last call
501 * 0: make the cursor invisible
502 * 1: make the cursor visible
504 void mutt_curs_set (int cursor)
506 static int SavedCursor = 1;
509 cursor = SavedCursor;
511 SavedCursor = cursor;
513 if (curs_set (cursor) == ERR) {
514 if (cursor == 1) /* cnorm */
515 curs_set (2); /* cvvis */
520 int mutt_multi_choice (char *prompt, char *letters)
526 mvaddstr (LINES - 1, 0, prompt);
531 if (ch.ch == -1 || CI_is_return (ch.ch)) {
536 p = strchr (letters, ch.ch);
538 choice = p - letters + 1;
541 else if (ch.ch <= '9' && ch.ch > '0') {
542 choice = ch.ch - '0';
543 if (choice <= m_strlen(letters))
549 CLEARLINE (LINES - 1);
555 * addwch would be provided by an up-to-date curses library
558 int mutt_addwch (wchar_t wc)
560 char buf[MB_LEN_MAX * 2];
564 p_clear(&mbstate, 1);
565 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
566 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
574 * This formats a string, a bit like
575 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
576 * except that the widths refer to the number of character cells
580 void mutt_format_string (char *dest, ssize_t destlen,
581 int min_width, int max_width,
582 int right_justify, char m_pad_char,
583 const char *s, ssize_t n, int arboreal)
589 char scratch[MB_LEN_MAX];
590 mbstate_t mbstate1, mbstate2;
592 p_clear(&mbstate1, 1);
593 p_clear(&mbstate2, 1);
596 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
597 if (k == -1 || k == -2) {
598 k = (k == -1) ? 1 : n;
599 wc = replacement_char ();
601 if (arboreal && wc < M_TREE_MAX)
609 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
613 strncpy (p, scratch, k2);
618 w = destlen < min_width ? destlen : min_width;
621 else if (right_justify) {
626 dest[w] = m_pad_char;
636 * This formats a string rather like
637 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
638 * snprintf (dest, destlen, fmt, s);
639 * except that the numbers in the conversion specification refer to
640 * the number of character cells when printed.
643 static void mutt_format_s_x (char *dest,
645 const char *prefix, const char *s, int arboreal)
647 int right_justify = 1;
650 int max_width = INT_MAX;
653 ++prefix, right_justify = 0;
654 min_width = strtol (prefix, &p, 10);
657 max_width = strtol (prefix, &p, 10);
662 mutt_format_string (dest, destlen, min_width, max_width,
663 right_justify, ' ', s, m_strlen(s), arboreal);
666 void mutt_format_s (char *dest,
667 size_t destlen, const char *prefix, const char *s)
669 mutt_format_s_x (dest, destlen, prefix, s, 0);
672 void mutt_format_s_tree (char *dest,
673 size_t destlen, const char *prefix, const char *s)
675 mutt_format_s_x (dest, destlen, prefix, s, 1);
679 * mutt_paddstr (n, s) is almost equivalent to
680 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
683 void mutt_paddstr (int n, const char *s)
688 size_t len = m_strlen(s);
691 p_clear(&mbstate, 1);
692 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
693 if (k == (size_t) (-1) || k == (size_t) (-2)) {
694 k = (k == (size_t) (-1)) ? 1 : len;
695 wc = replacement_char ();
703 addnstr ((char *) s, k);
712 * mutt_strwidth is like m_strlenexcept that it returns the width
713 * refering to the number of characters cells.
716 int mutt_strwidth (const char *s)
728 p_clear(&mbstate, 1);
729 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
730 if (k == (size_t) (-1) || k == (size_t) (-2)) {
731 k = (k == (size_t) (-1)) ? 1 : n;
732 wc = replacement_char ();