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.
20 #include <sys/types.h>
28 #ifdef HAVE_LANGINFO_YESEXPR
32 #include <lib-lib/mem.h>
33 #include <lib-lib/str.h>
34 #include <lib-lib/macros.h>
35 #include <lib-lib/file.h>
36 #include <lib-lib/debug.h>
38 #include <lib-sys/mutt_signal.h>
48 /* not possible to unget more than one char under some curses libs, and it
49 * is impossible to unget function keys in SLang, so roll our own input
52 size_t UngetCount = 0;
53 static size_t UngetBufLen = 0;
54 static event_t *KeyEvent;
56 void mutt_refresh (void)
58 /* don't refresh when we are waiting for a child. */
59 if (option (OPTKEEPQUIET))
62 /* don't refresh in the middle of macros unless necessary */
63 if (UngetCount && !option (OPTFORCEREFRESH))
70 /* Make sure that the next refresh does a full refresh. This could be
71 optmized by not doing it at all if DISPLAY is set as this might
72 indicate that a GUI based pinentry was used. Having an option to
73 customize this is of course the Mutt way. */
74 void mutt_need_hard_redraw (void)
76 if (!getenv ("DISPLAY")) {
77 keypad (stdscr, TRUE);
78 clearok (stdscr, TRUE);
79 set_option (OPTNEEDREDRAW);
83 event_t mutt_getch (void)
86 event_t err = { -1, OP_NULL }, ret;
88 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
89 return (KeyEvent[--UngetCount]);
93 mutt_allow_interrupt (1);
95 mutt_allow_interrupt (0);
103 if ((ch & 0x80) && option (OPTMETAKEY)) {
104 /* send ALT-x as ESC-x */
106 mutt_ungetch (ch, 0);
114 return (ch == ctrl ('G') ? err : ret);
117 int _mutt_get_field ( const char *field, char *buf, size_t buflen,
118 int complete, int multiple, char ***files, int *numfiles)
123 ENTER_STATE *es = mutt_new_enter_state ();
126 CLEARLINE (LINES - 1);
129 getyx (stdscr, y, x);
131 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
135 CLEARLINE (LINES - 1);
136 mutt_free_enter_state (&es);
141 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
145 set_option (OPTUNBUFFEREDINPUT);
146 rc = mutt_get_field (msg, buf, buflen, flags);
147 unset_option (OPTUNBUFFEREDINPUT);
152 void mutt_clear_error (void)
155 if (!option (OPTNOCURSES))
156 CLEARLINE (LINES - 1);
159 void mutt_edit_file (const char *editor, const char *data)
161 char cmd[LONG_STRING];
164 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
165 if (mutt_system (cmd) == -1)
166 mutt_error (_("Error running \"%s\"!"), cmd);
167 keypad (stdscr, TRUE);
168 clearok (stdscr, TRUE);
171 int mutt_yesorno (const char *msg, int def)
174 char *yes = _("yes");
177 size_t answer_string_len;
179 #ifdef HAVE_LANGINFO_YESEXPR
189 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
190 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
191 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
192 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
195 CLEARLINE (LINES - 1);
198 * In order to prevent the default answer to the question to wrapped
199 * around the screen in the even the question is wider than the screen,
200 * ensure there is enough room for the answer and truncate the question
203 answer_string = p_new(char, COLS + 1);
204 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
205 def == M_YES ? no : yes);
206 answer_string_len = m_strlen(answer_string);
207 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
208 p_delete(&answer_string);
213 if (CI_is_return (ch.ch))
220 #ifdef HAVE_LANGINFO_YESEXPR
222 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
226 (tolower (ch.ch) == *yes)) {
231 #ifdef HAVE_LANGINFO_YESEXPR
232 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
234 (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 debug_print (1, ("%s\n", Errorbuf));
283 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
284 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
285 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
287 if (!option (OPTKEEPQUIET)) {
289 SETCOLOR (MT_COLOR_ERROR);
290 mvaddstr (LINES - 1, 0, Errorbuf);
292 SETCOLOR (MT_COLOR_NORMAL);
296 set_option (OPTMSGERR);
299 void mutt_progress_bar (progress_t* progress, long pos) {
300 char posstr[SHORT_STRING];
304 mutt_message (progress->msg);
307 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
316 if (pos >= progress->pos + (NetInc << 10)) {
318 pos = pos / (NetInc << 10) * (NetInc << 10);
319 mutt_pretty_size (posstr, sizeof (posstr), pos);
321 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
323 mutt_message ("%s %s", progress->msg, posstr);
327 void mutt_curses_message (const char *fmt, ...)
329 char TmpErrorbuf[STRING];
333 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
336 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
337 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
338 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
340 if (!option (OPTKEEPQUIET)) {
341 SETCOLOR (MT_COLOR_MESSAGE);
342 mvaddstr (LINES - 1, 0, Errorbuf);
344 SETCOLOR (MT_COLOR_NORMAL);
348 unset_option (OPTMSGERR);
351 void mutt_show_error (void)
353 if (option (OPTKEEPQUIET))
356 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
357 CLEARLINE (LINES - 1);
359 SETCOLOR (MT_COLOR_NORMAL);
362 void mutt_endwin (const char *msg)
364 if (!option (OPTNOCURSES)) {
365 CLEARLINE (LINES - 1);
378 void _mutt_perror (const char *s, const char* filename, int line)
380 char *p = strerror (errno);
382 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
383 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
386 int mutt_any_key_to_continue (const char *s)
392 f = open ("/dev/tty", O_RDONLY);
394 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
395 t.c_lflag &= ~(ICANON | ECHO);
398 tcsetattr (f, TCSADRAIN, &t);
403 fputs (_("Press any key to continue..."), stdout);
407 tcsetattr (f, TCSADRAIN, &old);
409 fputs ("\r\n", stdout);
414 int mutt_do_pager (const char *banner,
415 const char *tempfile, int do_color, pager_t * info)
419 if (!Pager || m_strcmp(Pager, "builtin") == 0)
420 rc = mutt_pager (banner, tempfile, do_color, info);
425 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
426 if (mutt_system (cmd) == -1) {
427 mutt_error (_("Error running \"%s\"!"), cmd);
432 mutt_unlink (tempfile);
438 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
439 int *redraw, int buffy, int multiple, char ***files,
444 mvaddstr (LINES - 1, 0, (char *) prompt);
445 addstr (_(" ('?' for list): "));
453 CLEARLINE (LINES - 1);
456 else if (ch.ch == '?') {
459 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
461 *redraw = REDRAW_FULL;
464 char *pc = p_new(char, m_strlen(prompt) + 3);
466 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
467 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
469 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
473 MAYBE_REDRAW (*redraw);
480 void mutt_ungetch (int ch, int op)
487 if (UngetCount >= UngetBufLen)
488 p_realloc(&KeyEvent, UngetBufLen += 128);
490 KeyEvent[UngetCount++] = tmp;
493 void mutt_flushinp (void)
499 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
500 /* The argument can take 3 values:
501 * -1: restore the value of the last call
502 * 0: make the cursor invisible
503 * 1: make the cursor visible
505 void mutt_curs_set (int cursor)
507 static int SavedCursor = 1;
510 cursor = SavedCursor;
512 SavedCursor = cursor;
514 if (curs_set (cursor) == ERR) {
515 if (cursor == 1) /* cnorm */
516 curs_set (2); /* cvvis */
521 int mutt_multi_choice (char *prompt, char *letters)
527 mvaddstr (LINES - 1, 0, prompt);
532 if (ch.ch == -1 || CI_is_return (ch.ch)) {
537 p = strchr (letters, ch.ch);
539 choice = p - letters + 1;
542 else if (ch.ch <= '9' && ch.ch > '0') {
543 choice = ch.ch - '0';
544 if (choice <= m_strlen(letters))
550 CLEARLINE (LINES - 1);
556 * addwch would be provided by an up-to-date curses library
559 int mutt_addwch (wchar_t wc)
561 char buf[MB_LEN_MAX * 2];
565 p_clear(&mbstate, 1);
566 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
567 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
575 * This formats a string, a bit like
576 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
577 * except that the widths refer to the number of character cells
581 void mutt_format_string (char *dest, ssize_t destlen,
582 int min_width, int max_width,
583 int right_justify, char m_pad_char,
584 const char *s, ssize_t n, int arboreal)
590 char scratch[MB_LEN_MAX];
591 mbstate_t mbstate1, mbstate2;
593 p_clear(&mbstate1, 1);
594 p_clear(&mbstate2, 1);
597 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
598 if (k == -1 || k == -2) {
599 k = (k == -1) ? 1 : n;
600 wc = replacement_char ();
602 if (arboreal && wc < M_TREE_MAX)
610 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
614 strncpy (p, scratch, k2);
619 w = destlen < min_width ? destlen : min_width;
622 else if (right_justify) {
627 dest[w] = m_pad_char;
637 * This formats a string rather like
638 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
639 * snprintf (dest, destlen, fmt, s);
640 * except that the numbers in the conversion specification refer to
641 * the number of character cells when printed.
644 static void mutt_format_s_x (char *dest,
646 const char *prefix, const char *s, int arboreal)
648 int right_justify = 1;
651 int max_width = INT_MAX;
654 ++prefix, right_justify = 0;
655 min_width = strtol (prefix, &p, 10);
658 max_width = strtol (prefix, &p, 10);
663 mutt_format_string (dest, destlen, min_width, max_width,
664 right_justify, ' ', s, m_strlen(s), arboreal);
667 void mutt_format_s (char *dest,
668 size_t destlen, const char *prefix, const char *s)
670 mutt_format_s_x (dest, destlen, prefix, s, 0);
673 void mutt_format_s_tree (char *dest,
674 size_t destlen, const char *prefix, const char *s)
676 mutt_format_s_x (dest, destlen, prefix, s, 1);
680 * mutt_paddstr (n, s) is almost equivalent to
681 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
684 void mutt_paddstr (int n, const char *s)
689 size_t len = m_strlen(s);
692 p_clear(&mbstate, 1);
693 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
694 if (k == (size_t) (-1) || k == (size_t) (-2)) {
695 k = (k == (size_t) (-1)) ? 1 : len;
696 wc = replacement_char ();
704 addnstr ((char *) s, k);
713 * mutt_strwidth is like m_strlenexcept that it returns the width
714 * refering to the number of characters cells.
717 int mutt_strwidth (const char *s)
729 p_clear(&mbstate, 1);
730 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
731 if (k == (size_t) (-1) || k == (size_t) (-2)) {
732 k = (k == (size_t) (-1)) ? 1 : n;
733 wc = replacement_char ();