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.
14 #include <lib-lib/lib-lib.h>
17 #ifdef HAVE_LANGINFO_YESEXPR
21 #include <lib-sys/unix.h>
22 #include <lib-sys/mutt_signal.h>
32 /* not possible to unget more than one char under some curses libs, and it
33 * is impossible to unget function keys in SLang, so roll our own input
36 ssize_t UngetCount = 0;
37 static ssize_t UngetBufLen = 0;
38 static event_t *KeyEvent;
40 void mutt_refresh (void)
42 /* don't refresh when we are waiting for a child. */
43 if (option (OPTKEEPQUIET))
46 /* don't refresh in the middle of macros unless necessary */
47 if (UngetCount && !option (OPTFORCEREFRESH))
54 /* Make sure that the next refresh does a full refresh. This could be
55 optmized by not doing it at all if DISPLAY is set as this might
56 indicate that a GUI based pinentry was used. Having an option to
57 customize this is of course the Mutt way. */
58 void mutt_need_hard_redraw (void)
60 if (!getenv ("DISPLAY")) {
61 keypad (stdscr, TRUE);
62 clearok (stdscr, TRUE);
63 set_option (OPTNEEDREDRAW);
67 event_t mutt_getch (void)
70 event_t err = { -1, OP_NULL }, ret;
72 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
73 return (KeyEvent[--UngetCount]);
77 mutt_allow_interrupt (1);
79 mutt_allow_interrupt (0);
87 if ((ch & 0x80) && option (OPTMETAKEY)) {
88 /* send ALT-x as ESC-x */
98 return (ch == ctrl ('G') ? err : ret);
101 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
102 int complete, int multiple, char ***files, int *numfiles)
107 ENTER_STATE *es = mutt_new_enter_state ();
110 CLEARLINE (LINES - 1);
113 getyx (stdscr, y, x);
115 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
119 CLEARLINE (LINES - 1);
120 mutt_free_enter_state (&es);
125 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
129 set_option (OPTUNBUFFEREDINPUT);
130 rc = mutt_get_field (msg, buf, buflen, flags);
131 unset_option (OPTUNBUFFEREDINPUT);
136 void mutt_clear_error (void)
139 if (!option (OPTNOCURSES))
140 CLEARLINE (LINES - 1);
143 void mutt_edit_file (const char *editor, const char *data)
145 char cmd[LONG_STRING];
148 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
149 if (mutt_system (cmd) == -1)
150 mutt_error (_("Error running \"%s\"!"), cmd);
151 keypad (stdscr, TRUE);
152 clearok (stdscr, TRUE);
155 int mutt_yesorno (const char *msg, int def)
158 char *yes = _("yes");
161 ssize_t answer_string_len;
163 #ifdef HAVE_LANGINFO_YESEXPR
173 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
174 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
175 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
176 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
179 CLEARLINE (LINES - 1);
182 * In order to prevent the default answer to the question to wrapped
183 * around the screen in the even the question is wider than the screen,
184 * ensure there is enough room for the answer and truncate the question
187 answer_string = p_new(char, COLS + 1);
188 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
189 def == M_YES ? no : yes);
190 answer_string_len = m_strlen(answer_string);
191 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
192 p_delete(&answer_string);
197 if (CI_is_return (ch.ch))
204 #ifdef HAVE_LANGINFO_YESEXPR
206 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
208 if (tolower (ch.ch) == *yes)
215 #ifdef HAVE_LANGINFO_YESEXPR
216 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
218 (tolower (ch.ch) == *no)) {
226 #ifdef HAVE_LANGINFO_YESEXPR
234 addstr ((char *) (def == M_YES ? yes : no));
240 /* this function is called when the user presses the abort key */
241 void mutt_query_exit (void)
246 timeout (-1); /* restore blocking operation */
247 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
256 void mutt_curses_error (const char *fmt, ...)
258 char TmpErrorbuf[STRING];
262 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
265 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
266 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
267 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
269 if (!option (OPTKEEPQUIET)) {
271 SETCOLOR (MT_COLOR_ERROR);
272 mvaddstr (LINES - 1, 0, Errorbuf);
274 SETCOLOR (MT_COLOR_NORMAL);
278 set_option (OPTMSGERR);
281 void mutt_progress_bar (progress_t* progress, long pos) {
282 char posstr[SHORT_STRING];
286 mutt_message (progress->msg);
289 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
298 if (pos >= progress->pos + (NetInc << 10)) {
300 pos = pos / (NetInc << 10) * (NetInc << 10);
301 mutt_pretty_size (posstr, sizeof (posstr), pos);
303 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
305 mutt_message ("%s %s", progress->msg, posstr);
309 void mutt_curses_message (const char *fmt, ...)
311 char TmpErrorbuf[STRING];
315 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
318 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
319 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
320 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
322 if (!option (OPTKEEPQUIET)) {
323 SETCOLOR (MT_COLOR_MESSAGE);
324 mvaddstr (LINES - 1, 0, Errorbuf);
326 SETCOLOR (MT_COLOR_NORMAL);
330 unset_option (OPTMSGERR);
333 void mutt_show_error (void)
335 if (option (OPTKEEPQUIET))
338 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
339 CLEARLINE (LINES - 1);
341 SETCOLOR (MT_COLOR_NORMAL);
344 void mutt_endwin (const char *msg)
346 if (!option (OPTNOCURSES)) {
347 CLEARLINE (LINES - 1);
360 void _mutt_perror (const char *s, const char* filename, int line)
362 char *p = strerror (errno);
363 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
366 int mutt_any_key_to_continue (const char *s)
372 f = open ("/dev/tty", O_RDONLY);
374 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
375 t.c_lflag &= ~(ICANON | ECHO);
378 tcsetattr (f, TCSADRAIN, &t);
383 fputs (_("Press any key to continue..."), stdout);
387 tcsetattr (f, TCSADRAIN, &old);
389 fputs ("\r\n", stdout);
394 int mutt_do_pager (const char *banner,
395 const char *tempfile, int do_color, pager_t * info)
399 if (!Pager || m_strcmp(Pager, "builtin") == 0)
400 rc = mutt_pager (banner, tempfile, do_color, info);
405 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
406 if (mutt_system (cmd) == -1) {
407 mutt_error (_("Error running \"%s\"!"), cmd);
412 mutt_unlink (tempfile);
418 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
419 int *redraw, int buffy, int multiple, char ***files,
424 mvaddstr (LINES - 1, 0, (char *) prompt);
425 addstr (_(" ('?' for list): "));
433 CLEARLINE (LINES - 1);
436 else if (ch.ch == '?') {
439 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
441 *redraw = REDRAW_FULL;
444 char *pc = p_new(char, m_strlen(prompt) + 3);
446 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
447 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
449 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
453 MAYBE_REDRAW (*redraw);
460 void mutt_ungetch (int ch, int op)
467 if (UngetCount >= UngetBufLen)
468 p_realloc(&KeyEvent, UngetBufLen += 128);
470 KeyEvent[UngetCount++] = tmp;
473 void mutt_flushinp (void)
479 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
480 /* The argument can take 3 values:
481 * -1: restore the value of the last call
482 * 0: make the cursor invisible
483 * 1: make the cursor visible
485 void mutt_curs_set (int cursor)
487 static int SavedCursor = 1;
490 cursor = SavedCursor;
492 SavedCursor = cursor;
494 if (curs_set (cursor) == ERR) {
495 if (cursor == 1) /* cnorm */
496 curs_set (2); /* cvvis */
501 int mutt_multi_choice (char *prompt, char *letters)
507 mvaddstr (LINES - 1, 0, prompt);
512 if (ch.ch == -1 || CI_is_return (ch.ch)) {
517 p = strchr (letters, ch.ch);
519 choice = p - letters + 1;
522 else if (ch.ch <= '9' && ch.ch > '0') {
523 choice = ch.ch - '0';
524 if (choice <= m_strlen(letters))
530 CLEARLINE (LINES - 1);
536 * addwch would be provided by an up-to-date curses library
539 int mutt_addwch (wchar_t wc)
541 char buf[MB_LEN_MAX * 2];
545 p_clear(&mbstate, 1);
546 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
547 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
553 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
556 return m_strcpy(s, len, "0K");
558 if (n < 10189) /* 0.1K - 9.9K */
559 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
561 if (n < 1023949) /* 10K - 999K */
562 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
563 return snprintf(s, len, "%ldK", (n + 51) / 1024);
565 if (n < 10433332) /* 1.0M - 9.9M */
566 return snprintf(s, len, "%3.1fM", n / 1048576.0);
568 /* (10433332 + 52428) / 1048576 = 10 */
569 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
573 * This formats a string, a bit like
574 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
575 * except that the widths refer to the number of character cells
579 void mutt_format_string (char *dest, ssize_t destlen,
580 int min_width, int max_width,
581 int right_justify, char m_pad_char,
582 const char *s, ssize_t n, int arboreal)
588 char scratch[MB_LEN_MAX];
589 mbstate_t mbstate1, mbstate2;
591 p_clear(&mbstate1, 1);
592 p_clear(&mbstate2, 1);
595 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
596 if (k == -1 || k == -2) {
597 k = (k == -1) ? 1 : n;
598 wc = CharsetReplacement;
600 if (arboreal && wc < M_TREE_MAX)
608 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
612 strncpy (p, scratch, k2);
617 w = destlen < min_width ? destlen : min_width;
620 else if (right_justify) {
625 dest[w] = m_pad_char;
635 * This formats a string rather like
636 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
637 * snprintf (dest, destlen, fmt, s);
638 * except that the numbers in the conversion specification refer to
639 * the number of character cells when printed.
642 static void mutt_format_s_x (char *dest, ssize_t destlen,
643 const char *prefix, const char *s, int arboreal)
645 int right_justify = 1;
648 int max_width = INT_MAX;
651 ++prefix, right_justify = 0;
652 min_width = strtol (prefix, &p, 10);
655 max_width = strtol (prefix, &p, 10);
660 mutt_format_string (dest, destlen, min_width, max_width,
661 right_justify, ' ', s, m_strlen(s), arboreal);
664 void mutt_format_s (char *dest, ssize_t destlen,
665 const char *prefix, const char *s)
667 mutt_format_s_x (dest, destlen, prefix, s, 0);
670 void mutt_format_s_tree (char *dest, ssize_t destlen,
671 const char *prefix, const char *s)
673 mutt_format_s_x (dest, destlen, prefix, s, 1);
677 * mutt_paddstr (n, s) is almost equivalent to
678 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
681 void mutt_paddstr (int n, const char *s)
686 ssize_t len = m_strlen(s);
689 p_clear(&mbstate, 1);
690 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
691 if (k == -1 || k == -2) {
692 k = (k == -1) ? 1 : len;
693 wc = CharsetReplacement;
701 addnstr ((char *) s, k);
710 * mutt_strwidth is like m_strlenexcept that it returns the width
711 * refering to the number of characters cells.
714 int mutt_strwidth (const char *s)
726 p_clear(&mbstate, 1);
727 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
728 if (k == -1 || k == -2) {
729 k = (k == -1) ? 1 : n;
730 wc = CharsetReplacement;