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>
46 /* not possible to unget more than one char under some curses libs, and it
47 * is impossible to unget function keys in SLang, so roll our own input
50 size_t UngetCount = 0;
51 static size_t UngetBufLen = 0;
52 static event_t *KeyEvent;
54 void mutt_refresh (void)
56 /* don't refresh when we are waiting for a child. */
57 if (option (OPTKEEPQUIET))
60 /* don't refresh in the middle of macros unless necessary */
61 if (UngetCount && !option (OPTFORCEREFRESH))
68 /* Make sure that the next refresh does a full refresh. This could be
69 optmized by not doing it at all if DISPLAY is set as this might
70 indicate that a GUI based pinentry was used. Having an option to
71 customize this is of course the Mutt way. */
72 void mutt_need_hard_redraw (void)
74 if (!getenv ("DISPLAY")) {
75 keypad (stdscr, TRUE);
76 clearok (stdscr, TRUE);
77 set_option (OPTNEEDREDRAW);
81 event_t mutt_getch (void)
84 event_t err = { -1, OP_NULL }, ret;
86 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
87 return (KeyEvent[--UngetCount]);
91 mutt_allow_interrupt (1);
93 mutt_allow_interrupt (0);
101 if ((ch & 0x80) && option (OPTMETAKEY)) {
102 /* send ALT-x as ESC-x */
104 mutt_ungetch (ch, 0);
112 return (ch == ctrl ('G') ? err : ret);
115 int _mutt_get_field ( const char *field, char *buf, size_t buflen,
116 int complete, int multiple, char ***files, int *numfiles)
121 ENTER_STATE *es = mutt_new_enter_state ();
124 CLEARLINE (LINES - 1);
127 getyx (stdscr, y, x);
129 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
133 CLEARLINE (LINES - 1);
134 mutt_free_enter_state (&es);
139 int mutt_get_field_unbuffered (char *msg, char *buf, size_t buflen, int flags)
143 set_option (OPTUNBUFFEREDINPUT);
144 rc = mutt_get_field (msg, buf, buflen, flags);
145 unset_option (OPTUNBUFFEREDINPUT);
150 void mutt_clear_error (void)
153 if (!option (OPTNOCURSES))
154 CLEARLINE (LINES - 1);
157 void mutt_edit_file (const char *editor, const char *data)
159 char cmd[LONG_STRING];
162 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
163 if (mutt_system (cmd) == -1)
164 mutt_error (_("Error running \"%s\"!"), cmd);
165 keypad (stdscr, TRUE);
166 clearok (stdscr, TRUE);
169 int mutt_yesorno (const char *msg, int def)
172 char *yes = _("yes");
175 size_t answer_string_len;
177 #ifdef HAVE_LANGINFO_YESEXPR
187 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
188 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
189 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
190 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
193 CLEARLINE (LINES - 1);
196 * In order to prevent the default answer to the question to wrapped
197 * around the screen in the even the question is wider than the screen,
198 * ensure there is enough room for the answer and truncate the question
201 answer_string = p_new(char, COLS + 1);
202 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
203 def == M_YES ? no : yes);
204 answer_string_len = m_strlen(answer_string);
205 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
206 p_delete(&answer_string);
211 if (CI_is_return (ch.ch))
218 #ifdef HAVE_LANGINFO_YESEXPR
220 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
224 (tolower (ch.ch) == *yes)) {
229 #ifdef HAVE_LANGINFO_YESEXPR
230 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
232 (tolower (ch.ch) == *no)) {
241 #ifdef HAVE_LANGINFO_YESEXPR
249 addstr ((char *) (def == M_YES ? yes : no));
255 /* this function is called when the user presses the abort key */
256 void mutt_query_exit (void)
261 timeout (-1); /* restore blocking operation */
262 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
271 void mutt_curses_error (const char *fmt, ...)
273 char TmpErrorbuf[STRING];
277 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
280 debug_print (1, ("%s\n", Errorbuf));
281 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
282 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
283 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
285 if (!option (OPTKEEPQUIET)) {
287 SETCOLOR (MT_COLOR_ERROR);
288 mvaddstr (LINES - 1, 0, Errorbuf);
290 SETCOLOR (MT_COLOR_NORMAL);
294 set_option (OPTMSGERR);
297 void mutt_progress_bar (progress_t* progress, long pos) {
298 char posstr[SHORT_STRING];
302 mutt_message (progress->msg);
305 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
314 if (pos >= progress->pos + (NetInc << 10)) {
316 pos = pos / (NetInc << 10) * (NetInc << 10);
317 mutt_pretty_size (posstr, sizeof (posstr), pos);
319 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
321 mutt_message ("%s %s", progress->msg, posstr);
325 void mutt_curses_message (const char *fmt, ...)
327 char TmpErrorbuf[STRING];
331 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
334 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
335 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
336 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
338 if (!option (OPTKEEPQUIET)) {
339 SETCOLOR (MT_COLOR_MESSAGE);
340 mvaddstr (LINES - 1, 0, Errorbuf);
342 SETCOLOR (MT_COLOR_NORMAL);
346 unset_option (OPTMSGERR);
349 void mutt_show_error (void)
351 if (option (OPTKEEPQUIET))
354 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
355 CLEARLINE (LINES - 1);
357 SETCOLOR (MT_COLOR_NORMAL);
360 void mutt_endwin (const char *msg)
362 if (!option (OPTNOCURSES)) {
363 CLEARLINE (LINES - 1);
376 void _mutt_perror (const char *s, const char* filename, int line)
378 char *p = strerror (errno);
380 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
381 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
384 int mutt_any_key_to_continue (const char *s)
390 f = open ("/dev/tty", O_RDONLY);
392 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
393 t.c_lflag &= ~(ICANON | ECHO);
396 tcsetattr (f, TCSADRAIN, &t);
401 fputs (_("Press any key to continue..."), stdout);
405 tcsetattr (f, TCSADRAIN, &old);
407 fputs ("\r\n", stdout);
412 int mutt_do_pager (const char *banner,
413 const char *tempfile, int do_color, pager_t * info)
417 if (!Pager || m_strcmp(Pager, "builtin") == 0)
418 rc = mutt_pager (banner, tempfile, do_color, info);
423 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
424 if (mutt_system (cmd) == -1) {
425 mutt_error (_("Error running \"%s\"!"), cmd);
430 mutt_unlink (tempfile);
436 int _mutt_enter_fname (const char *prompt, char *buf, size_t blen,
437 int *redraw, int buffy, int multiple, char ***files,
442 mvaddstr (LINES - 1, 0, (char *) prompt);
443 addstr (_(" ('?' for list): "));
451 CLEARLINE (LINES - 1);
454 else if (ch.ch == '?') {
457 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
459 *redraw = REDRAW_FULL;
462 char *pc = p_new(char, m_strlen(prompt) + 3);
464 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
465 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
467 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
471 MAYBE_REDRAW (*redraw);
478 void mutt_ungetch (int ch, int op)
485 if (UngetCount >= UngetBufLen)
486 p_realloc(&KeyEvent, UngetBufLen += 128);
488 KeyEvent[UngetCount++] = tmp;
491 void mutt_flushinp (void)
497 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
498 /* The argument can take 3 values:
499 * -1: restore the value of the last call
500 * 0: make the cursor invisible
501 * 1: make the cursor visible
503 void mutt_curs_set (int cursor)
505 static int SavedCursor = 1;
508 cursor = SavedCursor;
510 SavedCursor = cursor;
512 if (curs_set (cursor) == ERR) {
513 if (cursor == 1) /* cnorm */
514 curs_set (2); /* cvvis */
519 int mutt_multi_choice (char *prompt, char *letters)
525 mvaddstr (LINES - 1, 0, prompt);
530 if (ch.ch == -1 || CI_is_return (ch.ch)) {
535 p = strchr (letters, ch.ch);
537 choice = p - letters + 1;
540 else if (ch.ch <= '9' && ch.ch > '0') {
541 choice = ch.ch - '0';
542 if (choice <= m_strlen(letters))
548 CLEARLINE (LINES - 1);
554 * addwch would be provided by an up-to-date curses library
557 int mutt_addwch (wchar_t wc)
559 char buf[MB_LEN_MAX * 2];
563 p_clear(&mbstate, 1);
564 if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t) (-1) ||
565 (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t) (-1))
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 = replacement_char ();
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,
644 const char *prefix, const char *s, int arboreal)
646 int right_justify = 1;
649 int max_width = INT_MAX;
652 ++prefix, right_justify = 0;
653 min_width = strtol (prefix, &p, 10);
656 max_width = strtol (prefix, &p, 10);
661 mutt_format_string (dest, destlen, min_width, max_width,
662 right_justify, ' ', s, m_strlen(s), arboreal);
665 void mutt_format_s (char *dest,
666 size_t destlen, const char *prefix, const char *s)
668 mutt_format_s_x (dest, destlen, prefix, s, 0);
671 void mutt_format_s_tree (char *dest,
672 size_t destlen, const char *prefix, const char *s)
674 mutt_format_s_x (dest, destlen, prefix, s, 1);
678 * mutt_paddstr (n, s) is almost equivalent to
679 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
682 void mutt_paddstr (int n, const char *s)
687 size_t len = m_strlen(s);
690 p_clear(&mbstate, 1);
691 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
692 if (k == (size_t) (-1) || k == (size_t) (-2)) {
693 k = (k == (size_t) (-1)) ? 1 : len;
694 wc = replacement_char ();
702 addnstr ((char *) s, k);
711 * mutt_strwidth is like m_strlenexcept that it returns the width
712 * refering to the number of characters cells.
715 int mutt_strwidth (const char *s)
727 p_clear(&mbstate, 1);
728 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
729 if (k == (size_t) (-1) || k == (size_t) (-2)) {
730 k = (k == (size_t) (-1)) ? 1 : n;
731 wc = replacement_char ();