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-lua/lib-lua.h>
22 #include <lib-sys/unix.h>
23 #include <lib-sys/mutt_signal.h>
33 /* not possible to unget more than one char under some curses libs, and it
34 * is impossible to unget function keys in SLang, so roll our own input
37 ssize_t UngetCount = 0;
38 static ssize_t UngetBufLen = 0;
39 static event_t *KeyEvent;
41 void mutt_refresh (void)
43 /* don't refresh when we are waiting for a child. */
44 if (option (OPTKEEPQUIET))
47 /* don't refresh in the middle of macros unless necessary */
48 if (UngetCount && !option (OPTFORCEREFRESH))
55 /* Make sure that the next refresh does a full refresh. This could be
56 optmized by not doing it at all if DISPLAY is set as this might
57 indicate that a GUI based pinentry was used. Having an option to
58 customize this is of course the Mutt way. */
59 void mutt_need_hard_redraw (void)
61 if (!getenv ("DISPLAY")) {
62 keypad (stdscr, TRUE);
63 clearok (stdscr, TRUE);
64 set_option (OPTNEEDREDRAW);
68 event_t mutt_getch (void)
71 event_t err = { -1, OP_NULL }, ret;
73 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
74 return (KeyEvent[--UngetCount]);
78 mutt_allow_interrupt (1);
80 mutt_allow_interrupt (0);
88 if ((ch & 0x80) && option (OPTMETAKEY)) {
89 /* send ALT-x as ESC-x */
99 return (ch == ctrl ('G') ? err : ret);
102 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
103 int complete, int multiple, char ***files, int *numfiles)
108 ENTER_STATE *es = mutt_new_enter_state ();
111 CLEARLINE (LINES - 1);
114 getyx (stdscr, y, x);
115 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
118 CLEARLINE (LINES - 1);
119 mutt_free_enter_state (&es);
124 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
128 set_option (OPTUNBUFFEREDINPUT);
129 rc = mutt_get_field (msg, buf, buflen, flags);
130 unset_option (OPTUNBUFFEREDINPUT);
135 void mutt_clear_error (void)
138 if (!option (OPTNOCURSES))
139 CLEARLINE (LINES - 1);
142 void mutt_edit_file(const char *data)
144 char cmd[LONG_STRING];
147 m_quotefile_fmt(cmd, sizeof (cmd), MCore.editor, data);
148 if (mutt_system (cmd) == -1)
149 mutt_error (_("Error running \"%s\"!"), cmd);
150 keypad (stdscr, TRUE);
151 clearok (stdscr, TRUE);
154 int mutt_yesorno (const char *msg, int def)
157 char *yes = _("yes");
160 ssize_t answer_string_len;
162 #ifdef HAVE_LANGINFO_YESEXPR
172 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
173 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
174 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
175 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
178 CLEARLINE (LINES - 1);
181 * In order to prevent the default answer to the question to wrapped
182 * around the screen in the even the question is wider than the screen,
183 * ensure there is enough room for the answer and truncate the question
186 answer_string = p_new(char, COLS + 1);
187 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
188 def == M_YES ? no : yes);
189 answer_string_len = m_strlen(answer_string);
190 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
191 p_delete(&answer_string);
196 if (CI_is_return (ch.ch))
203 #ifdef HAVE_LANGINFO_YESEXPR
205 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
207 if (tolower (ch.ch) == *yes)
214 #ifdef HAVE_LANGINFO_YESEXPR
215 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
217 (tolower (ch.ch) == *no)) {
225 #ifdef HAVE_LANGINFO_YESEXPR
233 addstr ((char *) (def == M_YES ? yes : no));
239 /* this function is called when the user presses the abort key */
240 void mutt_query_exit (void)
245 timeout (-1); /* restore blocking operation */
246 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
255 void mutt_curses_error (const char *fmt, ...)
257 char TmpErrorbuf[STRING];
261 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
264 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
265 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
266 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
268 if (!option (OPTKEEPQUIET)) {
270 SETCOLOR (MT_COLOR_ERROR);
271 mvaddstr (LINES - 1, 0, Errorbuf);
273 SETCOLOR (MT_COLOR_NORMAL);
277 set_option (OPTMSGERR);
280 void mutt_progress_bar (progress_t* progress, long pos) {
285 mutt_message (progress->msg);
288 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
297 if (pos >= progress->pos + (NetInc << 10)) {
299 pos = pos / (NetInc << 10) * (NetInc << 10);
300 mutt_pretty_size (posstr, sizeof (posstr), pos);
302 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
304 mutt_message ("%s %s", progress->msg, posstr);
308 void mutt_curses_message (const char *fmt, ...)
310 char TmpErrorbuf[STRING];
314 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
317 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
318 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
319 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
321 if (!option (OPTKEEPQUIET)) {
322 SETCOLOR (MT_COLOR_MESSAGE);
323 mvaddstr (LINES - 1, 0, Errorbuf);
325 SETCOLOR (MT_COLOR_NORMAL);
329 unset_option (OPTMSGERR);
332 void mutt_show_error (void)
334 if (option (OPTKEEPQUIET))
337 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
338 CLEARLINE (LINES - 1);
340 SETCOLOR (MT_COLOR_NORMAL);
343 void mutt_endwin (const char *msg)
345 if (!option (OPTNOCURSES)) {
346 CLEARLINE (LINES - 1);
359 void _mutt_perror (const char *s, const char* filename, int line)
361 char *p = strerror (errno);
362 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
365 int mutt_any_key_to_continue (const char *s)
371 f = open ("/dev/tty", O_RDONLY);
373 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
374 t.c_lflag &= ~(ICANON | ECHO);
377 tcsetattr (f, TCSADRAIN, &t);
382 fputs (_("Press any key to continue..."), stdout);
386 tcsetattr (f, TCSADRAIN, &old);
388 fputs ("\r\n", stdout);
393 int mutt_do_pager (const char *banner,
394 const char *tempfile, int do_color, pager_t * info)
398 if (!Pager || m_strcmp(Pager, "builtin") == 0)
399 rc = mutt_pager (banner, tempfile, do_color, info);
404 m_quotefile_fmt(cmd, sizeof (cmd), Pager, tempfile);
405 if (mutt_system (cmd) == -1) {
406 mutt_error (_("Error running \"%s\"!"), cmd);
411 mutt_unlink (tempfile);
417 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
418 int *redraw, int buffy, int multiple, char ***files,
423 mvaddstr (LINES - 1, 0, (char *) prompt);
424 addstr (_(" ('?' for list): "));
432 CLEARLINE (LINES - 1);
435 else if (ch.ch == '?') {
438 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
440 *redraw = REDRAW_FULL;
443 char *pc = p_new(char, m_strlen(prompt) + 3);
445 sprintf(pc, "%s: ", prompt);
446 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
448 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
452 MAYBE_REDRAW (*redraw);
459 void mutt_ungetch (int ch, int op)
466 if (UngetCount >= UngetBufLen)
467 p_realloc(&KeyEvent, UngetBufLen += 128);
469 KeyEvent[UngetCount++] = tmp;
472 void mutt_flushinp (void)
478 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
479 /* The argument can take 3 values:
480 * -1: restore the value of the last call
481 * 0: make the cursor invisible
482 * 1: make the cursor visible
484 void mutt_curs_set (int cursor)
486 static int SavedCursor = 1;
489 cursor = SavedCursor;
491 SavedCursor = cursor;
493 if (curs_set (cursor) == ERR) {
494 if (cursor == 1) /* cnorm */
495 curs_set (2); /* cvvis */
500 int mutt_multi_choice (char *prompt, char *letters)
506 mvaddstr (LINES - 1, 0, prompt);
511 if (ch.ch == -1 || CI_is_return (ch.ch)) {
516 p = strchr (letters, ch.ch);
518 choice = p - letters + 1;
521 else if (ch.ch <= '9' && ch.ch > '0') {
522 choice = ch.ch - '0';
523 if (choice <= m_strlen(letters))
529 CLEARLINE (LINES - 1);
535 * addwch would be provided by an up-to-date curses library
538 int mutt_addwch (wchar_t wc)
540 char buf[MB_LEN_MAX * 2];
544 p_clear(&mbstate, 1);
545 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
546 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
552 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
555 return m_strcpy(s, len, "0K");
557 if (n < 10189) /* 0.1K - 9.9K */
558 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
560 if (n < 1023949) /* 10K - 999K */
561 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
562 return snprintf(s, len, "%ldK", (n + 51) / 1024);
564 if (n < 10433332) /* 1.0M - 9.9M */
565 return snprintf(s, len, "%3.1fM", n / 1048576.0);
567 /* (10433332 + 52428) / 1048576 = 10 */
568 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
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 = CharsetReplacement;
599 if (arboreal && wc < M_TREE_MAX)
607 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
611 m_strncpy(p, destlen, 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 = CharsetReplacement;
700 addnstr ((char *) s, k);