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>
19 #include <lib-lua/lib-lua.h>
20 #include <lib-sys/unix.h>
21 #include <lib-sys/mutt_signal.h>
31 /* not possible to unget more than one char under some curses libs, and it
32 * is impossible to unget function keys in SLang, so roll our own input
35 ssize_t UngetCount = 0;
36 static ssize_t UngetBufLen = 0;
37 static event_t *KeyEvent;
39 void mutt_refresh (void)
41 /* don't refresh when we are waiting for a child. */
42 if (option (OPTKEEPQUIET))
45 /* don't refresh in the middle of macros unless necessary */
46 if (UngetCount && !option (OPTFORCEREFRESH))
53 /* Make sure that the next refresh does a full refresh. This could be
54 optmized by not doing it at all if DISPLAY is set as this might
55 indicate that a GUI based pinentry was used. Having an option to
56 customize this is of course the Mutt way. */
57 void mutt_need_hard_redraw (void)
59 if (!getenv ("DISPLAY")) {
60 keypad (stdscr, TRUE);
61 clearok (stdscr, TRUE);
62 set_option (OPTNEEDREDRAW);
66 event_t mutt_getch (void)
69 event_t err = { -1, OP_NULL }, ret;
71 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
72 return (KeyEvent[--UngetCount]);
76 mutt_allow_interrupt (1);
78 mutt_allow_interrupt (0);
86 if ((ch & 0x80) && option (OPTMETAKEY)) {
87 /* send ALT-x as ESC-x */
97 return (ch == ctrl ('G') ? err : ret);
100 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
101 int complete, int multiple, char ***files, int *numfiles)
106 ENTER_STATE *es = mutt_new_enter_state ();
109 CLEARLINE (LINES - 1);
112 getyx (stdscr, y, x);
113 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
116 CLEARLINE (LINES - 1);
117 mutt_free_enter_state (&es);
122 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
126 set_option (OPTUNBUFFEREDINPUT);
127 rc = mutt_get_field (msg, buf, buflen, flags);
128 unset_option (OPTUNBUFFEREDINPUT);
133 void mutt_clear_error (void)
136 if (!option (OPTNOCURSES))
137 CLEARLINE (LINES - 1);
140 void mutt_edit_file(const char *data)
142 char cmd[LONG_STRING];
145 m_quotefile_fmt(cmd, sizeof (cmd), MCore.editor, data);
146 if (mutt_system (cmd) == -1)
147 mutt_error (_("Error running \"%s\"!"), cmd);
148 keypad (stdscr, TRUE);
149 clearok (stdscr, TRUE);
152 int mutt_yesorno (const char *msg, int def)
155 const char *yes = _("yes");
156 const char *no = _("no");
158 ssize_t answer_string_len;
168 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
169 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
170 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
171 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
173 CLEARLINE (LINES - 1);
176 * In order to prevent the default answer to the question to wrapped
177 * around the screen in the even the question is wider than the screen,
178 * ensure there is enough room for the answer and truncate the question
181 answer_string = p_new(char, COLS + 1);
182 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
183 def == M_YES ? no : yes);
184 answer_string_len = m_strlen(answer_string);
185 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
186 p_delete(&answer_string);
191 if (CI_is_return (ch.ch))
199 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
205 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
206 (tolower (ch.ch) == *no)) {
220 addstr ((char *) (def == M_YES ? yes : no));
226 /* this function is called when the user presses the abort key */
227 void mutt_query_exit (void)
232 timeout (-1); /* restore blocking operation */
233 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
242 void mutt_curses_error (const char *fmt, ...)
244 char TmpErrorbuf[STRING];
248 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
251 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
252 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
253 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
255 if (!option (OPTKEEPQUIET)) {
257 SETCOLOR (MT_COLOR_ERROR);
258 mvaddstr (LINES - 1, 0, Errorbuf);
260 SETCOLOR (MT_COLOR_NORMAL);
264 set_option (OPTMSGERR);
267 void mutt_progress_bar (progress_t* progress, long pos) {
272 mutt_message (progress->msg);
275 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
284 if (pos >= progress->pos + (NetInc << 10)) {
286 pos = pos / (NetInc << 10) * (NetInc << 10);
287 mutt_pretty_size (posstr, sizeof (posstr), pos);
289 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
291 mutt_message ("%s %s", progress->msg, posstr);
295 void mutt_curses_message (const char *fmt, ...)
297 char TmpErrorbuf[STRING];
301 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
304 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
305 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
306 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
308 if (!option (OPTKEEPQUIET)) {
309 SETCOLOR (MT_COLOR_MESSAGE);
310 mvaddstr (LINES - 1, 0, Errorbuf);
312 SETCOLOR (MT_COLOR_NORMAL);
316 unset_option (OPTMSGERR);
319 void mutt_show_error (void)
321 if (option (OPTKEEPQUIET))
324 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
325 CLEARLINE (LINES - 1);
327 SETCOLOR (MT_COLOR_NORMAL);
330 void mutt_endwin (const char *msg)
332 if (!option (OPTNOCURSES)) {
333 CLEARLINE (LINES - 1);
346 void _mutt_perror (const char *s, const char* filename, int line)
348 char *p = strerror (errno);
349 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
352 int mutt_any_key_to_continue (const char *s)
358 f = open ("/dev/tty", O_RDONLY);
360 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
361 t.c_lflag &= ~(ICANON | ECHO);
364 tcsetattr (f, TCSADRAIN, &t);
369 fputs (_("Press any key to continue..."), stdout);
373 tcsetattr (f, TCSADRAIN, &old);
375 fputs ("\r\n", stdout);
380 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
381 int *redraw, int buffy, int multiple, char ***files,
386 mvaddstr (LINES - 1, 0, (char *) prompt);
387 addstr (_(" ('?' for list): "));
395 CLEARLINE (LINES - 1);
398 else if (ch.ch == '?') {
401 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
403 *redraw = REDRAW_FULL;
406 char *pc = p_new(char, m_strlen(prompt) + 3);
408 sprintf(pc, "%s: ", prompt);
409 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
411 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
415 MAYBE_REDRAW (*redraw);
422 void mutt_ungetch (int ch, int op)
429 if (UngetCount >= UngetBufLen)
430 p_realloc(&KeyEvent, UngetBufLen += 128);
432 KeyEvent[UngetCount++] = tmp;
435 void mutt_flushinp (void)
441 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
442 /* The argument can take 3 values:
443 * -1: restore the value of the last call
444 * 0: make the cursor invisible
445 * 1: make the cursor visible
447 void mutt_curs_set (int cursor)
449 static int SavedCursor = 1;
452 cursor = SavedCursor;
454 SavedCursor = cursor;
456 if (curs_set (cursor) == ERR) {
457 if (cursor == 1) /* cnorm */
458 curs_set (2); /* cvvis */
463 int mutt_multi_choice (const char *prompt, const char *letters)
469 mvaddstr (LINES - 1, 0, prompt);
474 if (ch.ch == -1 || CI_is_return (ch.ch)) {
479 p = strchr (letters, ch.ch);
481 choice = p - letters + 1;
484 else if (ch.ch <= '9' && ch.ch > '0') {
485 choice = ch.ch - '0';
486 if (choice <= m_strlen(letters))
492 CLEARLINE (LINES - 1);
498 * addwch would be provided by an up-to-date curses library
501 int mutt_addwch (wchar_t wc)
503 char buf[MB_LEN_MAX * 2];
507 p_clear(&mbstate, 1);
508 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
509 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
515 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
518 return m_strcpy(s, len, "0K");
520 if (n < 10189) /* 0.1K - 9.9K */
521 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
523 if (n < 1023949) /* 10K - 999K */
524 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
525 return snprintf(s, len, "%ldK", (n + 51) / 1024);
527 if (n < 10433332) /* 1.0M - 9.9M */
528 return snprintf(s, len, "%3.1fM", n / 1048576.0);
530 /* (10433332 + 52428) / 1048576 = 10 */
531 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
535 * This formats a string, a bit like
536 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
537 * except that the widths refer to the number of character cells
541 void mutt_format_string (char *dest, ssize_t destlen,
542 int min_width, int max_width,
543 int right_justify, char m_pad_char,
544 const char *s, ssize_t n, int arboreal)
550 char scratch[MB_LEN_MAX];
551 mbstate_t mbstate1, mbstate2;
553 p_clear(&mbstate1, 1);
554 p_clear(&mbstate2, 1);
557 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
558 if (k == -1 || k == -2) {
559 k = (k == -1) ? 1 : n;
560 wc = CharsetReplacement;
562 if (arboreal && wc < M_TREE_MAX)
570 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
574 m_strncpy(p, destlen, scratch, k2);
579 w = destlen < min_width ? destlen : min_width;
582 else if (right_justify) {
587 dest[w] = m_pad_char;
597 * This formats a string rather like
598 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
599 * snprintf (dest, destlen, fmt, s);
600 * except that the numbers in the conversion specification refer to
601 * the number of character cells when printed.
604 static void mutt_format_s_x (char *dest, ssize_t destlen,
605 const char *prefix, const char *s, int arboreal)
607 int right_justify = 1;
610 int max_width = INT_MAX;
613 ++prefix, right_justify = 0;
614 min_width = strtol (prefix, &p, 10);
617 max_width = strtol (prefix, &p, 10);
622 mutt_format_string (dest, destlen, min_width, max_width,
623 right_justify, ' ', s, m_strlen(s), arboreal);
626 void mutt_format_s (char *dest, ssize_t destlen,
627 const char *prefix, const char *s)
629 mutt_format_s_x (dest, destlen, prefix, s, 0);
632 void mutt_format_s_tree (char *dest, ssize_t destlen,
633 const char *prefix, const char *s)
635 mutt_format_s_x (dest, destlen, prefix, s, 1);
639 * mutt_paddstr (n, s) is almost equivalent to
640 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
643 void mutt_paddstr (int n, const char *s)
648 ssize_t len = m_strlen(s);
651 p_clear(&mbstate, 1);
652 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
653 if (k == -1 || k == -2) {
654 k = (k == -1) ? 1 : len;
655 wc = CharsetReplacement;
663 addnstr ((char *) s, k);