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_SYS_IOCTL_H
18 # include <sys/ioctl.h>
19 #elif defined(HAVE_IOCTL_H)
24 #include <lib-lua/lib-lua.h>
25 #include <lib-sys/unix.h>
26 #include <lib-sys/mutt_signal.h>
36 /* not possible to unget more than one char under some curses libs, and it
37 * is impossible to unget function keys in SLang, so roll our own input
40 ssize_t UngetCount = 0;
41 static ssize_t UngetBufLen = 0;
42 static event_t *KeyEvent;
44 void mutt_refresh (void)
46 /* don't refresh when we are waiting for a child. */
47 if (option (OPTKEEPQUIET))
50 /* don't refresh in the middle of macros unless necessary */
51 if (UngetCount && !option (OPTFORCEREFRESH))
58 void mutt_need_hard_redraw (void)
60 keypad (stdscr, TRUE);
61 clearok (stdscr, TRUE);
62 set_option (OPTNEEDREDRAW);
65 event_t mutt_getch (void)
68 event_t err = { -1, OP_NULL }, ret;
70 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
71 return (KeyEvent[--UngetCount]);
75 mutt_allow_interrupt (1);
77 mutt_allow_interrupt (0);
87 return (ch == ctrl ('G') ? err : ret);
90 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
91 int complete, int multiple, char ***files, int *numfiles)
96 ENTER_STATE *es = mutt_new_enter_state ();
99 CLEARLINE (LINES - 1);
100 waddstr (stdscr, field);
102 getyx (stdscr, y, x);
103 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
106 CLEARLINE (LINES - 1);
107 mutt_free_enter_state (&es);
112 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
116 set_option (OPTUNBUFFEREDINPUT);
117 rc = mutt_get_field (msg, buf, buflen, flags);
118 unset_option (OPTUNBUFFEREDINPUT);
123 void mutt_clear_error (void)
126 if (!option (OPTNOCURSES))
127 CLEARLINE (LINES - 1);
130 void mutt_edit_file(const char *data)
132 char cmd[LONG_STRING];
135 m_quotefile_fmt(cmd, sizeof (cmd), mod_core.editor, data);
136 if (mutt_system (cmd) == -1)
137 mutt_error (_("Error running \"%s\"!"), cmd);
138 keypad (stdscr, TRUE);
139 clearok (stdscr, TRUE);
142 int mutt_yesorno (const char *msg, int def)
145 const char *yes = _("yes");
146 const char *no = _("no");
148 ssize_t answer_string_len;
158 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
159 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
160 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
161 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
163 CLEARLINE (LINES - 1);
166 * In order to prevent the default answer to the question to wrapped
167 * around the screen in the even the question is wider than the screen,
168 * ensure there is enough room for the answer and truncate the question
171 answer_string = p_new(char, COLS + 1);
172 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
173 def == M_YES ? no : yes);
174 answer_string_len = m_strlen(answer_string);
175 wprintw (stdscr, "%.*s%s", COLS - answer_string_len, msg, answer_string);
176 p_delete(&answer_string);
181 if (CI_is_return (ch.ch))
189 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
195 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
196 (tolower (ch.ch) == *no)) {
210 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
216 /* this function is called when the user presses the abort key */
217 void mutt_query_exit (void)
222 wtimeout (stdscr, -1); /* restore blocking operation */
223 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
232 void mutt_curses_error (const char *fmt, ...)
234 char TmpErrorbuf[STRING];
238 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
241 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
242 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
243 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
245 if (!option (OPTKEEPQUIET)) {
247 SETCOLOR (MT_COLOR_ERROR);
248 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
250 SETCOLOR (MT_COLOR_NORMAL);
254 set_option (OPTMSGERR);
257 void mutt_progress_bar (progress_t* progress, long pos) {
262 mutt_message (progress->msg);
265 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
274 if (pos >= progress->pos + (NetInc << 10)) {
276 pos = pos / (NetInc << 10) * (NetInc << 10);
277 mutt_pretty_size (posstr, sizeof (posstr), pos);
279 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
281 mutt_message ("%s %s", progress->msg, posstr);
285 void mutt_curses_message (const char *fmt, ...)
287 char TmpErrorbuf[STRING];
291 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
294 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
295 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
296 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
298 if (!option (OPTKEEPQUIET)) {
299 SETCOLOR (MT_COLOR_MESSAGE);
300 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
302 SETCOLOR (MT_COLOR_NORMAL);
306 unset_option (OPTMSGERR);
309 void mutt_show_error (void)
311 if (option (OPTKEEPQUIET))
314 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
315 CLEARLINE (LINES - 1);
316 waddstr (stdscr, Errorbuf);
317 SETCOLOR (MT_COLOR_NORMAL);
320 void curses_initialize(void)
323 keypad(stdscr, TRUE);
326 typeahead (-1); /* simulate smooth scrolling */
330 void curses_install(void)
332 SETCOLOR(MT_COLOR_NORMAL);
334 mutt_error = mutt_curses_error;
335 mutt_message = mutt_curses_message;
338 void mutt_endwin (const char *msg)
340 if (!option (OPTNOCURSES)) {
341 CLEARLINE (LINES - 1);
343 wattrset (stdscr, A_NORMAL);
354 void _mutt_perror (const char *s, const char* filename, int line)
356 char *p = strerror (errno);
357 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
361 * prompts the user to enter a keystroke, and displays the octal value back
364 void mutt_what_key (void)
368 mvwprintw (stdscr, LINES - 1, 0, _("Enter keys (^G to abort): "));
370 ch = wgetch (stdscr);
371 if (ch != ERR && ch != ctrl ('G')) {
372 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
373 km_keyname (ch), ch, ch);
376 while (ch != ERR && ch != ctrl ('G'));
381 int mutt_any_key_to_continue (const char *s)
387 f = open ("/dev/tty", O_RDONLY);
389 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
390 t.c_lflag &= ~(ICANON | ECHO);
393 tcsetattr (f, TCSADRAIN, &t);
398 fputs (_("Press any key to continue..."), stdout);
402 tcsetattr (f, TCSADRAIN, &old);
404 fputs ("\r\n", stdout);
409 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
410 int *redraw, int buffy, int multiple, char ***files,
415 mvwaddstr (stdscr, LINES - 1, 0, (char *) prompt);
416 waddstr (stdscr, _(" ('?' for list): "));
418 waddstr (stdscr, buf);
424 CLEARLINE (LINES - 1);
427 else if (ch.ch == '?') {
430 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
432 *redraw = REDRAW_FULL;
435 char *pc = p_new(char, m_strlen(prompt) + 3);
437 sprintf(pc, "%s: ", prompt);
438 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
440 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
444 MAYBE_REDRAW (*redraw);
451 void mutt_ungetch (int ch, int op)
458 if (UngetCount >= UngetBufLen)
459 p_realloc(&KeyEvent, UngetBufLen += 128);
461 KeyEvent[UngetCount++] = tmp;
464 void mutt_flushinp (void)
470 /* The argument can take 3 values:
471 * -1: restore the value of the last call
472 * 0: make the cursor invisible
473 * 1: make the cursor visible
475 void mutt_curs_set (int cursor)
477 static int SavedCursor = 1;
480 cursor = SavedCursor;
482 SavedCursor = cursor;
484 if (curs_set (cursor) == ERR) {
485 if (cursor == 1) /* cnorm */
486 curs_set (2); /* cvvis */
490 int mutt_multi_choice (const char *prompt, const char *letters)
496 mvwaddstr (stdscr, LINES - 1, 0, prompt);
501 if (ch.ch == -1 || CI_is_return (ch.ch)) {
506 p = strchr (letters, ch.ch);
508 choice = p - letters + 1;
511 else if (ch.ch <= '9' && ch.ch > '0') {
512 choice = ch.ch - '0';
513 if (choice <= m_strlen(letters))
519 CLEARLINE (LINES - 1);
525 * addwch would be provided by an up-to-date curses library
528 int mutt_addwch (wchar_t wc)
530 char buf[MB_LEN_MAX * 2];
534 p_clear(&mbstate, 1);
535 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
536 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
539 return waddstr (stdscr, buf);
542 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
545 return m_strcpy(s, len, "0K");
547 if (n < 10189) /* 0.1K - 9.9K */
548 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
550 if (n < 1023949) /* 10K - 999K */
551 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
552 return snprintf(s, len, "%ldK", (n + 51) / 1024);
554 if (n < 10433332) /* 1.0M - 9.9M */
555 return snprintf(s, len, "%3.1fM", n / 1048576.0);
557 /* (10433332 + 52428) / 1048576 = 10 */
558 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
562 * This formats a string, a bit like
563 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
564 * except that the widths refer to the number of character cells
568 void mutt_format_string (char *dest, ssize_t destlen,
569 int min_width, int max_width,
570 int right_justify, char m_pad_char,
571 const char *s, ssize_t n, int arboreal)
577 char scratch[MB_LEN_MAX];
578 mbstate_t mbstate1, mbstate2;
580 p_clear(&mbstate1, 1);
581 p_clear(&mbstate2, 1);
584 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
585 if (k == -1 || k == -2) {
586 k = (k == -1) ? 1 : n;
587 wc = CharsetReplacement;
589 if (arboreal && wc < M_TREE_MAX)
597 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
601 m_strncpy(p, destlen, scratch, k2);
606 w = destlen < min_width ? destlen : min_width;
609 else if (right_justify) {
614 dest[w] = m_pad_char;
624 * This formats a string rather like
625 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
626 * snprintf (dest, destlen, fmt, s);
627 * except that the numbers in the conversion specification refer to
628 * the number of character cells when printed.
631 static void mutt_format_s_x (char *dest, ssize_t destlen,
632 const char *prefix, const char *s, int arboreal)
634 int right_justify = 1;
637 int max_width = INT_MAX;
640 ++prefix, right_justify = 0;
641 min_width = strtol (prefix, &p, 10);
644 max_width = strtol (prefix, &p, 10);
649 mutt_format_string (dest, destlen, min_width, max_width,
650 right_justify, ' ', s, m_strlen(s), arboreal);
653 void mutt_format_s (char *dest, ssize_t destlen,
654 const char *prefix, const char *s)
656 mutt_format_s_x (dest, destlen, prefix, s, 0);
659 void mutt_format_s_tree (char *dest, ssize_t destlen,
660 const char *prefix, const char *s)
662 mutt_format_s_x (dest, destlen, prefix, s, 1);
666 * mutt_paddstr (n, s) is almost equivalent to
667 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (stdscr, bigbuf)
670 void mutt_paddstr (int n, const char *s)
675 ssize_t len = m_strlen(s);
678 p_clear(&mbstate, 1);
679 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
680 if (k == -1 || k == -2) {
681 k = (k == -1) ? 1 : len;
682 wc = CharsetReplacement;
690 waddnstr (stdscr, (char *) s, k);
695 waddch (stdscr, ' ');
698 /* this routine should be called after receiving SIGWINCH */
699 void mutt_resize_screen (void)
709 if ((fd = open ("/dev/tty", O_RDONLY)) != -1) {
710 if (ioctl (fd, TIOCGWINSZ, &w) != -1) {
717 if ((cp = getenv ("LINES")) != NULL) {
724 if ((cp = getenv ("COLUMNS")) != NULL)
729 resizeterm (rows, cols);