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-ui/lib-ui.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>
35 /* not possible to unget more than one char under some curses libs, and it
36 * is impossible to unget function keys in SLang, so roll our own input
39 ssize_t UngetCount = 0;
40 static ssize_t UngetBufLen = 0;
41 static event_t *KeyEvent;
43 void mutt_refresh (void)
45 /* don't refresh when we are waiting for a child. */
46 if (option (OPTKEEPQUIET))
49 /* don't refresh in the middle of macros unless necessary */
50 if (UngetCount && !option (OPTFORCEREFRESH))
57 void mutt_need_hard_redraw (void)
59 keypad (stdscr, TRUE);
60 clearok (stdscr, TRUE);
61 set_option (OPTNEEDREDRAW);
64 event_t mutt_getch (void)
67 event_t err = { -1, OP_NULL }, ret;
69 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
70 return (KeyEvent[--UngetCount]);
74 mutt_allow_interrupt (1);
76 mutt_allow_interrupt (0);
86 return (ch == ctrl ('G') ? err : ret);
89 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
90 int complete, int multiple, char ***files, int *numfiles)
95 ENTER_STATE *es = mutt_new_enter_state ();
98 CLEARLINE (LINES - 1);
99 waddstr (stdscr, field);
101 getyx (stdscr, y, x);
102 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
105 CLEARLINE (LINES - 1);
106 mutt_free_enter_state (&es);
111 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
115 set_option (OPTUNBUFFEREDINPUT);
116 rc = mutt_get_field (msg, buf, buflen, flags);
117 unset_option (OPTUNBUFFEREDINPUT);
122 void mutt_clear_error (void)
125 if (!option (OPTNOCURSES))
126 CLEARLINE (LINES - 1);
129 void mutt_edit_file(const char *data)
131 char cmd[LONG_STRING];
134 m_quotefile_fmt(cmd, sizeof (cmd), mod_core.editor, data);
135 if (mutt_system (cmd) == -1)
136 mutt_error (_("Error running \"%s\"!"), cmd);
137 keypad (stdscr, TRUE);
138 clearok (stdscr, TRUE);
141 int mutt_yesorno (const char *msg, int def)
144 const char *yes = _("yes");
145 const char *no = _("no");
147 ssize_t answer_string_len;
157 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
158 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
159 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
160 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
162 CLEARLINE (LINES - 1);
165 * In order to prevent the default answer to the question to wrapped
166 * around the screen in the even the question is wider than the screen,
167 * ensure there is enough room for the answer and truncate the question
170 answer_string = p_new(char, COLS + 1);
171 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
172 def == M_YES ? no : yes);
173 answer_string_len = m_strlen(answer_string);
174 wprintw (stdscr, "%.*s%s", COLS - answer_string_len, msg, answer_string);
175 p_delete(&answer_string);
180 if (CI_is_return (ch.ch))
188 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
194 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
195 (tolower (ch.ch) == *no)) {
209 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
215 /* this function is called when the user presses the abort key */
216 void mutt_query_exit (void)
221 wtimeout (stdscr, -1); /* restore blocking operation */
222 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
231 void mutt_curses_error (const char *fmt, ...)
233 char TmpErrorbuf[STRING];
237 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
240 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
241 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
242 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
244 if (!option (OPTKEEPQUIET)) {
246 SETCOLOR (MT_COLOR_ERROR);
247 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
249 SETCOLOR (MT_COLOR_NORMAL);
253 set_option (OPTMSGERR);
256 void mutt_progress_bar (progress_t* progress, long pos) {
261 mutt_message (progress->msg);
264 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
273 if (pos >= progress->pos + (NetInc << 10)) {
275 pos = pos / (NetInc << 10) * (NetInc << 10);
276 mutt_pretty_size (posstr, sizeof (posstr), pos);
278 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
280 mutt_message ("%s %s", progress->msg, posstr);
284 void mutt_curses_message (const char *fmt, ...)
286 char TmpErrorbuf[STRING];
290 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
293 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
294 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
295 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
297 if (!option (OPTKEEPQUIET)) {
298 SETCOLOR (MT_COLOR_MESSAGE);
299 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
301 SETCOLOR (MT_COLOR_NORMAL);
305 unset_option (OPTMSGERR);
308 void mutt_show_error (void)
310 if (option (OPTKEEPQUIET))
313 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
314 CLEARLINE (LINES - 1);
315 waddstr (stdscr, Errorbuf);
316 SETCOLOR (MT_COLOR_NORMAL);
319 void curses_initialize(void)
322 keypad(stdscr, TRUE);
325 typeahead (-1); /* simulate smooth scrolling */
329 void curses_install(void)
331 SETCOLOR(MT_COLOR_NORMAL);
333 mutt_error = mutt_curses_error;
334 mutt_message = mutt_curses_message;
337 void mutt_endwin (const char *msg)
339 if (!option (OPTNOCURSES)) {
340 CLEARLINE (LINES - 1);
342 wattrset (stdscr, A_NORMAL);
353 void _mutt_perror (const char *s, const char* filename, int line)
355 char *p = strerror (errno);
356 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
360 * prompts the user to enter a keystroke, and displays the octal value back
363 void mutt_what_key (void)
367 mvwprintw (stdscr, LINES - 1, 0, _("Enter keys (^G to abort): "));
369 ch = wgetch (stdscr);
370 if (ch != ERR && ch != ctrl ('G')) {
371 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
372 km_keyname (ch), ch, ch);
375 while (ch != ERR && ch != ctrl ('G'));
380 int mutt_any_key_to_continue (const char *s)
386 f = open ("/dev/tty", O_RDONLY);
388 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
389 t.c_lflag &= ~(ICANON | ECHO);
392 tcsetattr (f, TCSADRAIN, &t);
397 fputs (_("Press any key to continue..."), stdout);
401 tcsetattr (f, TCSADRAIN, &old);
403 fputs ("\r\n", stdout);
408 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
409 int *redraw, int buffy, int multiple, char ***files,
414 mvwaddstr (stdscr, LINES - 1, 0, (char *) prompt);
415 waddstr (stdscr, _(" ('?' for list): "));
417 waddstr (stdscr, buf);
423 CLEARLINE (LINES - 1);
426 else if (ch.ch == '?') {
429 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
431 *redraw = REDRAW_FULL;
434 char *pc = p_new(char, m_strlen(prompt) + 3);
436 sprintf(pc, "%s: ", prompt);
437 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
439 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
443 MAYBE_REDRAW (*redraw);
450 void mutt_ungetch (int ch, int op)
457 if (UngetCount >= UngetBufLen)
458 p_realloc(&KeyEvent, UngetBufLen += 128);
460 KeyEvent[UngetCount++] = tmp;
463 void mutt_flushinp (void)
469 /* The argument can take 3 values:
470 * -1: restore the value of the last call
471 * 0: make the cursor invisible
472 * 1: make the cursor visible
474 void mutt_curs_set (int cursor)
476 static int SavedCursor = 1;
479 cursor = SavedCursor;
481 SavedCursor = cursor;
483 if (curs_set (cursor) == ERR) {
484 if (cursor == 1) /* cnorm */
485 curs_set (2); /* cvvis */
489 int mutt_multi_choice (const char *prompt, const char *letters)
495 mvwaddstr (stdscr, LINES - 1, 0, prompt);
500 if (ch.ch == -1 || CI_is_return (ch.ch)) {
505 p = strchr (letters, ch.ch);
507 choice = p - letters + 1;
510 else if (ch.ch <= '9' && ch.ch > '0') {
511 choice = ch.ch - '0';
512 if (choice <= m_strlen(letters))
518 CLEARLINE (LINES - 1);
524 * addwch would be provided by an up-to-date curses library
527 int mutt_addwch (wchar_t wc)
529 char buf[MB_LEN_MAX * 2];
533 p_clear(&mbstate, 1);
534 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
535 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
538 return waddstr (stdscr, buf);
541 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
544 return m_strcpy(s, len, "0K");
546 if (n < 10189) /* 0.1K - 9.9K */
547 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
549 if (n < 1023949) /* 10K - 999K */
550 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
551 return snprintf(s, len, "%ldK", (n + 51) / 1024);
553 if (n < 10433332) /* 1.0M - 9.9M */
554 return snprintf(s, len, "%3.1fM", n / 1048576.0);
556 /* (10433332 + 52428) / 1048576 = 10 */
557 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
561 * This formats a string, a bit like
562 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
563 * except that the widths refer to the number of character cells
567 void mutt_format_string (char *dest, ssize_t destlen,
568 int min_width, int max_width,
569 int right_justify, char m_pad_char,
570 const char *s, ssize_t n, int arboreal)
576 char scratch[MB_LEN_MAX];
577 mbstate_t mbstate1, mbstate2;
579 p_clear(&mbstate1, 1);
580 p_clear(&mbstate2, 1);
583 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
584 if (k == -1 || k == -2) {
585 k = (k == -1) ? 1 : n;
586 wc = CharsetReplacement;
588 if (arboreal && wc < M_TREE_MAX)
596 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
600 m_strncpy(p, destlen, scratch, k2);
605 w = destlen < min_width ? destlen : min_width;
608 else if (right_justify) {
613 dest[w] = m_pad_char;
623 * This formats a string rather like
624 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
625 * snprintf (dest, destlen, fmt, s);
626 * except that the numbers in the conversion specification refer to
627 * the number of character cells when printed.
630 static void mutt_format_s_x (char *dest, ssize_t destlen,
631 const char *prefix, const char *s, int arboreal)
633 int right_justify = 1;
636 int max_width = INT_MAX;
639 ++prefix, right_justify = 0;
640 min_width = strtol (prefix, &p, 10);
643 max_width = strtol (prefix, &p, 10);
648 mutt_format_string (dest, destlen, min_width, max_width,
649 right_justify, ' ', s, m_strlen(s), arboreal);
652 void mutt_format_s (char *dest, ssize_t destlen,
653 const char *prefix, const char *s)
655 mutt_format_s_x (dest, destlen, prefix, s, 0);
658 void mutt_format_s_tree (char *dest, ssize_t destlen,
659 const char *prefix, const char *s)
661 mutt_format_s_x (dest, destlen, prefix, s, 1);
665 * mutt_paddstr (n, s) is almost equivalent to
666 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (stdscr, bigbuf)
669 void mutt_paddstr (int n, const char *s)
674 ssize_t len = m_strlen(s);
677 p_clear(&mbstate, 1);
678 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
679 if (k == -1 || k == -2) {
680 k = (k == -1) ? 1 : len;
681 wc = CharsetReplacement;
689 waddnstr (stdscr, (char *) s, k);
694 waddch (stdscr, ' ');
697 /* this routine should be called after receiving SIGWINCH */
698 void mutt_resize_screen (void)
708 if ((fd = open ("/dev/tty", O_RDONLY)) != -1) {
709 if (ioctl (fd, TIOCGWINSZ, &w) != -1) {
716 if ((cp = getenv ("LINES")) != NULL) {
723 if ((cp = getenv ("COLUMNS")) != NULL)
728 resizeterm (rows, cols);