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.
21 #include <sys/types.h>
29 #ifdef HAVE_LANGINFO_YESEXPR
33 #include <lib-lib/mem.h>
34 #include <lib-lib/str.h>
35 #include <lib-lib/macros.h>
36 #include <lib-lib/file.h>
37 #include <lib-lib/debug.h>
39 #include <lib-sys/unix.h>
40 #include <lib-sys/mutt_signal.h>
50 /* not possible to unget more than one char under some curses libs, and it
51 * is impossible to unget function keys in SLang, so roll our own input
54 ssize_t UngetCount = 0;
55 static ssize_t UngetBufLen = 0;
56 static event_t *KeyEvent;
58 void mutt_refresh (void)
60 /* don't refresh when we are waiting for a child. */
61 if (option (OPTKEEPQUIET))
64 /* don't refresh in the middle of macros unless necessary */
65 if (UngetCount && !option (OPTFORCEREFRESH))
72 /* Make sure that the next refresh does a full refresh. This could be
73 optmized by not doing it at all if DISPLAY is set as this might
74 indicate that a GUI based pinentry was used. Having an option to
75 customize this is of course the Mutt way. */
76 void mutt_need_hard_redraw (void)
78 if (!getenv ("DISPLAY")) {
79 keypad (stdscr, TRUE);
80 clearok (stdscr, TRUE);
81 set_option (OPTNEEDREDRAW);
85 event_t mutt_getch (void)
88 event_t err = { -1, OP_NULL }, ret;
90 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
91 return (KeyEvent[--UngetCount]);
95 mutt_allow_interrupt (1);
97 mutt_allow_interrupt (0);
105 if ((ch & 0x80) && option (OPTMETAKEY)) {
106 /* send ALT-x as ESC-x */
108 mutt_ungetch (ch, 0);
116 return (ch == ctrl ('G') ? err : ret);
119 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
120 int complete, int multiple, char ***files, int *numfiles)
125 ENTER_STATE *es = mutt_new_enter_state ();
128 CLEARLINE (LINES - 1);
131 getyx (stdscr, y, x);
133 _mutt_enter_string (buf, buflen, y, x, complete, multiple, files,
137 CLEARLINE (LINES - 1);
138 mutt_free_enter_state (&es);
143 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
147 set_option (OPTUNBUFFEREDINPUT);
148 rc = mutt_get_field (msg, buf, buflen, flags);
149 unset_option (OPTUNBUFFEREDINPUT);
154 void mutt_clear_error (void)
157 if (!option (OPTNOCURSES))
158 CLEARLINE (LINES - 1);
161 void mutt_edit_file (const char *editor, const char *data)
163 char cmd[LONG_STRING];
166 mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
167 if (mutt_system (cmd) == -1)
168 mutt_error (_("Error running \"%s\"!"), cmd);
169 keypad (stdscr, TRUE);
170 clearok (stdscr, TRUE);
173 int mutt_yesorno (const char *msg, int def)
176 char *yes = _("yes");
179 ssize_t answer_string_len;
181 #ifdef HAVE_LANGINFO_YESEXPR
191 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
192 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
193 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
194 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
197 CLEARLINE (LINES - 1);
200 * In order to prevent the default answer to the question to wrapped
201 * around the screen in the even the question is wider than the screen,
202 * ensure there is enough room for the answer and truncate the question
205 answer_string = p_new(char, COLS + 1);
206 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
207 def == M_YES ? no : yes);
208 answer_string_len = m_strlen(answer_string);
209 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
210 p_delete(&answer_string);
215 if (CI_is_return (ch.ch))
222 #ifdef HAVE_LANGINFO_YESEXPR
224 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) :
228 (tolower (ch.ch) == *yes)) {
233 #ifdef HAVE_LANGINFO_YESEXPR
234 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
236 (tolower (ch.ch) == *no)) {
245 #ifdef HAVE_LANGINFO_YESEXPR
253 addstr ((char *) (def == M_YES ? yes : no));
259 /* this function is called when the user presses the abort key */
260 void mutt_query_exit (void)
265 timeout (-1); /* restore blocking operation */
266 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
275 void mutt_curses_error (const char *fmt, ...)
277 char TmpErrorbuf[STRING];
281 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
284 debug_print (1, ("%s\n", Errorbuf));
285 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
286 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
287 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
289 if (!option (OPTKEEPQUIET)) {
291 SETCOLOR (MT_COLOR_ERROR);
292 mvaddstr (LINES - 1, 0, Errorbuf);
294 SETCOLOR (MT_COLOR_NORMAL);
298 set_option (OPTMSGERR);
301 void mutt_progress_bar (progress_t* progress, long pos) {
302 char posstr[SHORT_STRING];
306 mutt_message (progress->msg);
309 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
318 if (pos >= progress->pos + (NetInc << 10)) {
320 pos = pos / (NetInc << 10) * (NetInc << 10);
321 mutt_pretty_size (posstr, sizeof (posstr), pos);
323 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
325 mutt_message ("%s %s", progress->msg, posstr);
329 void mutt_curses_message (const char *fmt, ...)
331 char TmpErrorbuf[STRING];
335 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
338 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
339 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
340 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
342 if (!option (OPTKEEPQUIET)) {
343 SETCOLOR (MT_COLOR_MESSAGE);
344 mvaddstr (LINES - 1, 0, Errorbuf);
346 SETCOLOR (MT_COLOR_NORMAL);
350 unset_option (OPTMSGERR);
353 void mutt_show_error (void)
355 if (option (OPTKEEPQUIET))
358 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
359 CLEARLINE (LINES - 1);
361 SETCOLOR (MT_COLOR_NORMAL);
364 void mutt_endwin (const char *msg)
366 if (!option (OPTNOCURSES)) {
367 CLEARLINE (LINES - 1);
380 void _mutt_perror (const char *s, const char* filename, int line)
382 char *p = strerror (errno);
384 debug_print (1, ("%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno));
385 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
388 int mutt_any_key_to_continue (const char *s)
394 f = open ("/dev/tty", O_RDONLY);
396 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
397 t.c_lflag &= ~(ICANON | ECHO);
400 tcsetattr (f, TCSADRAIN, &t);
405 fputs (_("Press any key to continue..."), stdout);
409 tcsetattr (f, TCSADRAIN, &old);
411 fputs ("\r\n", stdout);
416 int mutt_do_pager (const char *banner,
417 const char *tempfile, int do_color, pager_t * info)
421 if (!Pager || m_strcmp(Pager, "builtin") == 0)
422 rc = mutt_pager (banner, tempfile, do_color, info);
427 mutt_expand_file_fmt (cmd, sizeof (cmd), Pager, tempfile);
428 if (mutt_system (cmd) == -1) {
429 mutt_error (_("Error running \"%s\"!"), cmd);
434 mutt_unlink (tempfile);
440 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
441 int *redraw, int buffy, int multiple, char ***files,
446 mvaddstr (LINES - 1, 0, (char *) prompt);
447 addstr (_(" ('?' for list): "));
455 CLEARLINE (LINES - 1);
458 else if (ch.ch == '?') {
461 _mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
463 *redraw = REDRAW_FULL;
466 char *pc = p_new(char, m_strlen(prompt) + 3);
468 sprintf (pc, "%s: ", prompt); /* __SPRINTF_CHECKED__ */
469 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
471 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
475 MAYBE_REDRAW (*redraw);
482 void mutt_ungetch (int ch, int op)
489 if (UngetCount >= UngetBufLen)
490 p_realloc(&KeyEvent, UngetBufLen += 128);
492 KeyEvent[UngetCount++] = tmp;
495 void mutt_flushinp (void)
501 #if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
502 /* The argument can take 3 values:
503 * -1: restore the value of the last call
504 * 0: make the cursor invisible
505 * 1: make the cursor visible
507 void mutt_curs_set (int cursor)
509 static int SavedCursor = 1;
512 cursor = SavedCursor;
514 SavedCursor = cursor;
516 if (curs_set (cursor) == ERR) {
517 if (cursor == 1) /* cnorm */
518 curs_set (2); /* cvvis */
523 int mutt_multi_choice (char *prompt, char *letters)
529 mvaddstr (LINES - 1, 0, prompt);
534 if (ch.ch == -1 || CI_is_return (ch.ch)) {
539 p = strchr (letters, ch.ch);
541 choice = p - letters + 1;
544 else if (ch.ch <= '9' && ch.ch > '0') {
545 choice = ch.ch - '0';
546 if (choice <= m_strlen(letters))
552 CLEARLINE (LINES - 1);
558 * addwch would be provided by an up-to-date curses library
561 int mutt_addwch (wchar_t wc)
563 char buf[MB_LEN_MAX * 2];
567 p_clear(&mbstate, 1);
568 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
569 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
577 * This formats a string, a bit like
578 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
579 * except that the widths refer to the number of character cells
583 void mutt_format_string (char *dest, ssize_t destlen,
584 int min_width, int max_width,
585 int right_justify, char m_pad_char,
586 const char *s, ssize_t n, int arboreal)
592 char scratch[MB_LEN_MAX];
593 mbstate_t mbstate1, mbstate2;
595 p_clear(&mbstate1, 1);
596 p_clear(&mbstate2, 1);
599 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
600 if (k == -1 || k == -2) {
601 k = (k == -1) ? 1 : n;
602 wc = replacement_char ();
604 if (arboreal && wc < M_TREE_MAX)
612 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
616 strncpy (p, scratch, k2);
621 w = destlen < min_width ? destlen : min_width;
624 else if (right_justify) {
629 dest[w] = m_pad_char;
639 * This formats a string rather like
640 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
641 * snprintf (dest, destlen, fmt, s);
642 * except that the numbers in the conversion specification refer to
643 * the number of character cells when printed.
646 static void mutt_format_s_x (char *dest, ssize_t destlen,
647 const char *prefix, const char *s, int arboreal)
649 int right_justify = 1;
652 int max_width = INT_MAX;
655 ++prefix, right_justify = 0;
656 min_width = strtol (prefix, &p, 10);
659 max_width = strtol (prefix, &p, 10);
664 mutt_format_string (dest, destlen, min_width, max_width,
665 right_justify, ' ', s, m_strlen(s), arboreal);
668 void mutt_format_s (char *dest, ssize_t destlen,
669 const char *prefix, const char *s)
671 mutt_format_s_x (dest, destlen, prefix, s, 0);
674 void mutt_format_s_tree (char *dest, ssize_t destlen,
675 const char *prefix, const char *s)
677 mutt_format_s_x (dest, destlen, prefix, s, 1);
681 * mutt_paddstr (n, s) is almost equivalent to
682 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
685 void mutt_paddstr (int n, const char *s)
690 ssize_t len = m_strlen(s);
693 p_clear(&mbstate, 1);
694 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
695 if (k == -1 || k == -2) {
696 k = (k == -1) ? 1 : len;
697 wc = replacement_char ();
705 addnstr ((char *) s, k);
714 * mutt_strwidth is like m_strlenexcept that it returns the width
715 * refering to the number of characters cells.
718 int mutt_strwidth (const char *s)
730 p_clear(&mbstate, 1);
731 for (w = 0; n && (k = mbrtowc (&wc, s, n, &mbstate)); s += k, n -= k) {
732 if (k == -1 || k == -2) {
733 k = (k == -1) ? 1 : n;
734 wc = replacement_char ();