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);
114 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
117 CLEARLINE (LINES - 1);
118 mutt_free_enter_state (&es);
123 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
127 set_option (OPTUNBUFFEREDINPUT);
128 rc = mutt_get_field (msg, buf, buflen, flags);
129 unset_option (OPTUNBUFFEREDINPUT);
134 void mutt_clear_error (void)
137 if (!option (OPTNOCURSES))
138 CLEARLINE (LINES - 1);
141 void mutt_edit_file (const char *editor, const char *data)
143 char cmd[LONG_STRING];
146 m_quotefile_fmt(cmd, sizeof (cmd), editor, data);
147 if (mutt_system (cmd) == -1)
148 mutt_error (_("Error running \"%s\"!"), cmd);
149 keypad (stdscr, TRUE);
150 clearok (stdscr, TRUE);
153 int mutt_yesorno (const char *msg, int def)
156 char *yes = _("yes");
159 ssize_t answer_string_len;
161 #ifdef HAVE_LANGINFO_YESEXPR
171 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
172 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
173 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
174 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
177 CLEARLINE (LINES - 1);
180 * In order to prevent the default answer to the question to wrapped
181 * around the screen in the even the question is wider than the screen,
182 * ensure there is enough room for the answer and truncate the question
185 answer_string = p_new(char, COLS + 1);
186 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
187 def == M_YES ? no : yes);
188 answer_string_len = m_strlen(answer_string);
189 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
190 p_delete(&answer_string);
195 if (CI_is_return (ch.ch))
202 #ifdef HAVE_LANGINFO_YESEXPR
204 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
206 if (tolower (ch.ch) == *yes)
213 #ifdef HAVE_LANGINFO_YESEXPR
214 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
216 (tolower (ch.ch) == *no)) {
224 #ifdef HAVE_LANGINFO_YESEXPR
232 addstr ((char *) (def == M_YES ? yes : no));
238 /* this function is called when the user presses the abort key */
239 void mutt_query_exit (void)
244 timeout (-1); /* restore blocking operation */
245 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
254 void mutt_curses_error (const char *fmt, ...)
256 char TmpErrorbuf[STRING];
260 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
263 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
264 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
265 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
267 if (!option (OPTKEEPQUIET)) {
269 SETCOLOR (MT_COLOR_ERROR);
270 mvaddstr (LINES - 1, 0, Errorbuf);
272 SETCOLOR (MT_COLOR_NORMAL);
276 set_option (OPTMSGERR);
279 void mutt_progress_bar (progress_t* progress, long pos) {
284 mutt_message (progress->msg);
287 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
296 if (pos >= progress->pos + (NetInc << 10)) {
298 pos = pos / (NetInc << 10) * (NetInc << 10);
299 mutt_pretty_size (posstr, sizeof (posstr), pos);
301 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
303 mutt_message ("%s %s", progress->msg, posstr);
307 void mutt_curses_message (const char *fmt, ...)
309 char TmpErrorbuf[STRING];
313 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
316 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
317 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
318 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
320 if (!option (OPTKEEPQUIET)) {
321 SETCOLOR (MT_COLOR_MESSAGE);
322 mvaddstr (LINES - 1, 0, Errorbuf);
324 SETCOLOR (MT_COLOR_NORMAL);
328 unset_option (OPTMSGERR);
331 void mutt_show_error (void)
333 if (option (OPTKEEPQUIET))
336 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
337 CLEARLINE (LINES - 1);
339 SETCOLOR (MT_COLOR_NORMAL);
342 void mutt_endwin (const char *msg)
344 if (!option (OPTNOCURSES)) {
345 CLEARLINE (LINES - 1);
358 void _mutt_perror (const char *s, const char* filename, int line)
360 char *p = strerror (errno);
361 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
364 int mutt_any_key_to_continue (const char *s)
370 f = open ("/dev/tty", O_RDONLY);
372 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
373 t.c_lflag &= ~(ICANON | ECHO);
376 tcsetattr (f, TCSADRAIN, &t);
381 fputs (_("Press any key to continue..."), stdout);
385 tcsetattr (f, TCSADRAIN, &old);
387 fputs ("\r\n", stdout);
392 int mutt_do_pager (const char *banner,
393 const char *tempfile, int do_color, pager_t * info)
397 if (!Pager || m_strcmp(Pager, "builtin") == 0)
398 rc = mutt_pager (banner, tempfile, do_color, info);
403 m_quotefile_fmt(cmd, sizeof (cmd), Pager, tempfile);
404 if (mutt_system (cmd) == -1) {
405 mutt_error (_("Error running \"%s\"!"), cmd);
410 mutt_unlink (tempfile);
416 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
417 int *redraw, int buffy, int multiple, char ***files,
422 mvaddstr (LINES - 1, 0, (char *) prompt);
423 addstr (_(" ('?' for list): "));
431 CLEARLINE (LINES - 1);
434 else if (ch.ch == '?') {
437 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
439 *redraw = REDRAW_FULL;
442 char *pc = p_new(char, m_strlen(prompt) + 3);
444 sprintf(pc, "%s: ", prompt);
445 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
447 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
451 MAYBE_REDRAW (*redraw);
458 void mutt_ungetch (int ch, int op)
465 if (UngetCount >= UngetBufLen)
466 p_realloc(&KeyEvent, UngetBufLen += 128);
468 KeyEvent[UngetCount++] = tmp;
471 void mutt_flushinp (void)
477 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
478 /* The argument can take 3 values:
479 * -1: restore the value of the last call
480 * 0: make the cursor invisible
481 * 1: make the cursor visible
483 void mutt_curs_set (int cursor)
485 static int SavedCursor = 1;
488 cursor = SavedCursor;
490 SavedCursor = cursor;
492 if (curs_set (cursor) == ERR) {
493 if (cursor == 1) /* cnorm */
494 curs_set (2); /* cvvis */
499 int mutt_multi_choice (char *prompt, char *letters)
505 mvaddstr (LINES - 1, 0, prompt);
510 if (ch.ch == -1 || CI_is_return (ch.ch)) {
515 p = strchr (letters, ch.ch);
517 choice = p - letters + 1;
520 else if (ch.ch <= '9' && ch.ch > '0') {
521 choice = ch.ch - '0';
522 if (choice <= m_strlen(letters))
528 CLEARLINE (LINES - 1);
534 * addwch would be provided by an up-to-date curses library
537 int mutt_addwch (wchar_t wc)
539 char buf[MB_LEN_MAX * 2];
543 p_clear(&mbstate, 1);
544 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
545 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
551 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
554 return m_strcpy(s, len, "0K");
556 if (n < 10189) /* 0.1K - 9.9K */
557 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
559 if (n < 1023949) /* 10K - 999K */
560 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
561 return snprintf(s, len, "%ldK", (n + 51) / 1024);
563 if (n < 10433332) /* 1.0M - 9.9M */
564 return snprintf(s, len, "%3.1fM", n / 1048576.0);
566 /* (10433332 + 52428) / 1048576 = 10 */
567 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
571 * This formats a string, a bit like
572 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
573 * except that the widths refer to the number of character cells
577 void mutt_format_string (char *dest, ssize_t destlen,
578 int min_width, int max_width,
579 int right_justify, char m_pad_char,
580 const char *s, ssize_t n, int arboreal)
586 char scratch[MB_LEN_MAX];
587 mbstate_t mbstate1, mbstate2;
589 p_clear(&mbstate1, 1);
590 p_clear(&mbstate2, 1);
593 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
594 if (k == -1 || k == -2) {
595 k = (k == -1) ? 1 : n;
596 wc = CharsetReplacement;
598 if (arboreal && wc < M_TREE_MAX)
606 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
610 m_strncpy(p, destlen, scratch, k2);
615 w = destlen < min_width ? destlen : min_width;
618 else if (right_justify) {
623 dest[w] = m_pad_char;
633 * This formats a string rather like
634 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
635 * snprintf (dest, destlen, fmt, s);
636 * except that the numbers in the conversion specification refer to
637 * the number of character cells when printed.
640 static void mutt_format_s_x (char *dest, ssize_t destlen,
641 const char *prefix, const char *s, int arboreal)
643 int right_justify = 1;
646 int max_width = INT_MAX;
649 ++prefix, right_justify = 0;
650 min_width = strtol (prefix, &p, 10);
653 max_width = strtol (prefix, &p, 10);
658 mutt_format_string (dest, destlen, min_width, max_width,
659 right_justify, ' ', s, m_strlen(s), arboreal);
662 void mutt_format_s (char *dest, ssize_t destlen,
663 const char *prefix, const char *s)
665 mutt_format_s_x (dest, destlen, prefix, s, 0);
668 void mutt_format_s_tree (char *dest, ssize_t destlen,
669 const char *prefix, const char *s)
671 mutt_format_s_x (dest, destlen, prefix, s, 1);
675 * mutt_paddstr (n, s) is almost equivalent to
676 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
679 void mutt_paddstr (int n, const char *s)
684 ssize_t len = m_strlen(s);
687 p_clear(&mbstate, 1);
688 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
689 if (k == -1 || k == -2) {
690 k = (k == -1) ? 1 : len;
691 wc = CharsetReplacement;
699 addnstr ((char *) s, k);