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.
21 #include <sys/types.h>
29 #ifdef HAVE_LANGINFO_YESEXPR
33 #include <lib-lib/mem.h>
34 #include <lib-lib/str.h>
35 #include <lib-lib/macros.h>
36 #include <lib-lib/file.h>
38 #include <lib-sys/unix.h>
39 #include <lib-sys/mutt_signal.h>
49 /* not possible to unget more than one char under some curses libs, and it
50 * is impossible to unget function keys in SLang, so roll our own input
53 ssize_t UngetCount = 0;
54 static ssize_t UngetBufLen = 0;
55 static event_t *KeyEvent;
57 void mutt_refresh (void)
59 /* don't refresh when we are waiting for a child. */
60 if (option (OPTKEEPQUIET))
63 /* don't refresh in the middle of macros unless necessary */
64 if (UngetCount && !option (OPTFORCEREFRESH))
71 /* Make sure that the next refresh does a full refresh. This could be
72 optmized by not doing it at all if DISPLAY is set as this might
73 indicate that a GUI based pinentry was used. Having an option to
74 customize this is of course the Mutt way. */
75 void mutt_need_hard_redraw (void)
77 if (!getenv ("DISPLAY")) {
78 keypad (stdscr, TRUE);
79 clearok (stdscr, TRUE);
80 set_option (OPTNEEDREDRAW);
84 event_t mutt_getch (void)
87 event_t err = { -1, OP_NULL }, ret;
89 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
90 return (KeyEvent[--UngetCount]);
94 mutt_allow_interrupt (1);
96 mutt_allow_interrupt (0);
104 if ((ch & 0x80) && option (OPTMETAKEY)) {
105 /* send ALT-x as ESC-x */
107 mutt_ungetch (ch, 0);
115 return (ch == ctrl ('G') ? err : ret);
118 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
119 int complete, int multiple, char ***files, int *numfiles)
124 ENTER_STATE *es = mutt_new_enter_state ();
127 CLEARLINE (LINES - 1);
130 getyx (stdscr, y, x);
132 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
136 CLEARLINE (LINES - 1);
137 mutt_free_enter_state (&es);
142 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
146 set_option (OPTUNBUFFEREDINPUT);
147 rc = mutt_get_field (msg, buf, buflen, flags);
148 unset_option (OPTUNBUFFEREDINPUT);
153 void mutt_clear_error (void)
156 if (!option (OPTNOCURSES))
157 CLEARLINE (LINES - 1);
160 void mutt_edit_file (const char *editor, const char *data)
162 char cmd[LONG_STRING];
165 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
166 if (mutt_system (cmd) == -1)
167 mutt_error (_("Error running \"%s\"!"), cmd);
168 keypad (stdscr, TRUE);
169 clearok (stdscr, TRUE);
172 int mutt_yesorno (const char *msg, int def)
175 char *yes = _("yes");
178 ssize_t answer_string_len;
180 #ifdef HAVE_LANGINFO_YESEXPR
190 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
191 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
192 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
193 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
196 CLEARLINE (LINES - 1);
199 * In order to prevent the default answer to the question to wrapped
200 * around the screen in the even the question is wider than the screen,
201 * ensure there is enough room for the answer and truncate the question
204 answer_string = p_new(char, COLS + 1);
205 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
206 def == M_YES ? no : yes);
207 answer_string_len = m_strlen(answer_string);
208 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
209 p_delete(&answer_string);
214 if (CI_is_return (ch.ch))
221 #ifdef HAVE_LANGINFO_YESEXPR
223 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
225 if (tolower (ch.ch) == *yes)
232 #ifdef HAVE_LANGINFO_YESEXPR
233 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
235 (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 Madmutt?"), M_YES) == M_YES) {
273 void mutt_curses_error (const char *fmt, ...)
275 char TmpErrorbuf[STRING];
279 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
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);
380 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
383 int mutt_any_key_to_continue (const char *s)
389 f = open ("/dev/tty", O_RDONLY);
391 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
392 t.c_lflag &= ~(ICANON | ECHO);
395 tcsetattr (f, TCSADRAIN, &t);
400 fputs (_("Press any key to continue..."), stdout);
404 tcsetattr (f, TCSADRAIN, &old);
406 fputs ("\r\n", stdout);
411 int mutt_do_pager (const char *banner,
412 const char *tempfile, int do_color, pager_t * info)
416 if (!Pager || m_strcmp(Pager, "builtin") == 0)
417 rc = mutt_pager (banner, tempfile, do_color, info);
422 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
423 if (mutt_system (cmd) == -1) {
424 mutt_error (_("Error running \"%s\"!"), cmd);
429 mutt_unlink (tempfile);
435 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
436 int *redraw, int buffy, int multiple, char ***files,
441 mvaddstr (LINES - 1, 0, (char *) prompt);
442 addstr (_(" ('?' for list): "));
450 CLEARLINE (LINES - 1);
453 else if (ch.ch == '?') {
456 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
458 *redraw = REDRAW_FULL;
461 char *pc = p_new(char, m_strlen(prompt) + 3);
463 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
464 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
466 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
470 MAYBE_REDRAW (*redraw);
477 void mutt_ungetch (int ch, int op)
484 if (UngetCount >= UngetBufLen)
485 p_realloc(&KeyEvent, UngetBufLen += 128);
487 KeyEvent[UngetCount++] = tmp;
490 void mutt_flushinp (void)
496 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
497 /* The argument can take 3 values:
498 * -1: restore the value of the last call
499 * 0: make the cursor invisible
500 * 1: make the cursor visible
502 void mutt_curs_set (int cursor)
504 static int SavedCursor = 1;
507 cursor = SavedCursor;
509 SavedCursor = cursor;
511 if (curs_set (cursor) == ERR) {
512 if (cursor == 1) /* cnorm */
513 curs_set (2); /* cvvis */
518 int mutt_multi_choice (char *prompt, char *letters)
524 mvaddstr (LINES - 1, 0, prompt);
529 if (ch.ch == -1 || CI_is_return (ch.ch)) {
534 p = strchr (letters, ch.ch);
536 choice = p - letters + 1;
539 else if (ch.ch <= '9' && ch.ch > '0') {
540 choice = ch.ch - '0';
541 if (choice <= m_strlen(letters))
547 CLEARLINE (LINES - 1);
553 * addwch would be provided by an up-to-date curses library
556 int mutt_addwch (wchar_t wc)
558 char buf[MB_LEN_MAX * 2];
562 p_clear(&mbstate, 1);
563 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
564 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
572 * This formats a string, a bit like
573 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
574 * except that the widths refer to the number of character cells
578 void mutt_format_string (char *dest, ssize_t destlen,
579 int min_width, int max_width,
580 int right_justify, char m_pad_char,
581 const char *s, ssize_t n, int arboreal)
587 char scratch[MB_LEN_MAX];
588 mbstate_t mbstate1, mbstate2;
590 p_clear(&mbstate1, 1);
591 p_clear(&mbstate2, 1);
594 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
595 if (k == -1 || k == -2) {
596 k = (k == -1) ? 1 : n;
597 wc = replacement_char ();
599 if (arboreal && wc < M_TREE_MAX)
607 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
611 strncpy (p, scratch, k2);
616 w = destlen < min_width ? destlen : min_width;
619 else if (right_justify) {
624 dest[w] = m_pad_char;
634 * This formats a string rather like
635 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
636 * snprintf (dest, destlen, fmt, s);
637 * except that the numbers in the conversion specification refer to
638 * the number of character cells when printed.
641 static void mutt_format_s_x (char *dest, ssize_t destlen,
642 const char *prefix, const char *s, int arboreal)
644 int right_justify = 1;
647 int max_width = INT_MAX;
650 ++prefix, right_justify = 0;
651 min_width = strtol (prefix, &p, 10);
654 max_width = strtol (prefix, &p, 10);
659 mutt_format_string (dest, destlen, min_width, max_width,
660 right_justify, ' ', s, m_strlen(s), arboreal);
663 void mutt_format_s (char *dest, ssize_t destlen,
664 const char *prefix, const char *s)
666 mutt_format_s_x (dest, destlen, prefix, s, 0);
669 void mutt_format_s_tree (char *dest, ssize_t destlen,
670 const char *prefix, const char *s)
672 mutt_format_s_x (dest, destlen, prefix, s, 1);
676 * mutt_paddstr (n, s) is almost equivalent to
677 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
680 void mutt_paddstr (int n, const char *s)
685 ssize_t len = m_strlen(s);
688 p_clear(&mbstate, 1);
689 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
690 if (k == -1 || k == -2) {
691 k = (k == -1) ? 1 : len;
692 wc = replacement_char ();
700 addnstr ((char *) s, k);
709 * mutt_strwidth is like m_strlenexcept that it returns the width
710 * refering to the number of characters cells.
713 int mutt_strwidth (const char *s)
725 p_clear(&mbstate, 1);
726 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
727 if (k == -1 || k == -2) {
728 k = (k == -1) ? 1 : n;
729 wc = replacement_char ();