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>
19 #include <lib-lua/lib-lua.h>
20 #include <lib-sys/unix.h>
21 #include <lib-sys/mutt_signal.h>
30 /* not possible to unget more than one char under some curses libs, and it
31 * is impossible to unget function keys in SLang, so roll our own input
34 ssize_t UngetCount = 0;
35 static ssize_t UngetBufLen = 0;
36 static event_t *KeyEvent;
38 event_t mutt_getch (void)
41 event_t err = { -1, OP_NULL }, ret;
43 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
44 return (KeyEvent[--UngetCount]);
48 mutt_allow_interrupt (1);
50 mutt_allow_interrupt (0);
60 return (ch == ctrl ('G') ? err : ret);
63 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
64 int complete, int multiple, char ***files, int *numfiles)
69 ENTER_STATE *es = mutt_new_enter_state ();
72 CLEARLINE (LINES - 1);
73 waddstr (stdscr, field);
76 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
79 CLEARLINE (LINES - 1);
80 mutt_free_enter_state (&es);
85 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
89 set_option (OPTUNBUFFEREDINPUT);
90 rc = mutt_get_field (msg, buf, buflen, flags);
91 unset_option (OPTUNBUFFEREDINPUT);
96 void mutt_clear_error (void)
99 if (!option (OPTNOCURSES))
100 CLEARLINE (LINES - 1);
103 void mutt_edit_file(const char *data)
105 char cmd[LONG_STRING];
108 m_quotefile_fmt(cmd, sizeof (cmd), mod_core.editor, data);
109 if (mutt_system (cmd) == -1)
110 mutt_error (_("Error running \"%s\"!"), cmd);
111 keypad (stdscr, TRUE);
112 clearok (stdscr, TRUE);
115 int mutt_yesorno (const char *msg, int def)
118 const char *yes = _("yes");
119 const char *no = _("no");
121 ssize_t answer_string_len;
131 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
132 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
133 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
134 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
136 CLEARLINE (LINES - 1);
139 * In order to prevent the default answer to the question to wrapped
140 * around the screen in the even the question is wider than the screen,
141 * ensure there is enough room for the answer and truncate the question
144 answer_string = p_new(char, COLS + 1);
145 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
146 def == M_YES ? no : yes);
147 answer_string_len = m_strlen(answer_string);
148 wprintw (stdscr, "%.*s%s", COLS - answer_string_len, msg, answer_string);
149 p_delete(&answer_string);
154 if (CI_is_return (ch.ch))
162 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
168 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
169 (tolower (ch.ch) == *no)) {
183 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
189 /* this function is called when the user presses the abort key */
190 void mutt_query_exit (void)
195 wtimeout (stdscr, -1); /* restore blocking operation */
196 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
205 void mutt_curses_error (const char *fmt, ...)
207 char TmpErrorbuf[STRING];
211 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
214 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
215 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
216 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
218 if (!option (OPTKEEPQUIET)) {
220 SETCOLOR (MT_COLOR_ERROR);
221 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
223 SETCOLOR (MT_COLOR_NORMAL);
227 set_option (OPTMSGERR);
230 void mutt_progress_bar (progress_t* progress, long pos) {
235 mutt_message (progress->msg);
238 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
247 if (pos >= progress->pos + (NetInc << 10)) {
249 pos = pos / (NetInc << 10) * (NetInc << 10);
250 mutt_pretty_size (posstr, sizeof (posstr), pos);
252 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
254 mutt_message ("%s %s", progress->msg, posstr);
258 void mutt_curses_message (const char *fmt, ...)
260 char TmpErrorbuf[STRING];
264 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
267 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
268 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
269 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
271 if (!option (OPTKEEPQUIET)) {
272 SETCOLOR (MT_COLOR_MESSAGE);
273 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
275 SETCOLOR (MT_COLOR_NORMAL);
279 unset_option (OPTMSGERR);
282 void mutt_show_error (void)
284 if (option (OPTKEEPQUIET))
287 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
288 CLEARLINE (LINES - 1);
289 waddstr (stdscr, Errorbuf);
290 SETCOLOR (MT_COLOR_NORMAL);
293 void curses_initialize(void)
296 keypad(stdscr, TRUE);
299 typeahead (-1); /* simulate smooth scrolling */
303 void _mutt_perror (const char *s, const char* filename, int line)
305 char *p = strerror (errno);
306 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
310 * prompts the user to enter a keystroke, and displays the octal value back
313 void mutt_what_key (void)
317 mvwprintw (stdscr, LINES - 1, 0, _("Enter keys (^G to abort): "));
319 ch = wgetch (stdscr);
320 if (ch != ERR && ch != ctrl ('G')) {
321 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
322 km_keyname (ch), ch, ch);
325 while (ch != ERR && ch != ctrl ('G'));
330 int mutt_any_key_to_continue (const char *s)
336 f = open ("/dev/tty", O_RDONLY);
338 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
339 t.c_lflag &= ~(ICANON | ECHO);
342 tcsetattr (f, TCSADRAIN, &t);
347 fputs (_("Press any key to continue..."), stdout);
351 tcsetattr (f, TCSADRAIN, &old);
353 fputs ("\r\n", stdout);
358 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
359 int *redraw, int buffy, int multiple, char ***files,
364 mvwaddstr (stdscr, LINES - 1, 0, (char *) prompt);
365 waddstr (stdscr, _(" ('?' for list): "));
367 waddstr (stdscr, buf);
373 CLEARLINE (LINES - 1);
376 else if (ch.ch == '?') {
379 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
381 *redraw = REDRAW_FULL;
384 char *pc = p_new(char, m_strlen(prompt) + 3);
386 sprintf(pc, "%s: ", prompt);
387 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
389 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
393 MAYBE_REDRAW (*redraw);
400 void mutt_ungetch (int ch, int op)
407 if (UngetCount >= UngetBufLen)
408 p_realloc(&KeyEvent, UngetBufLen += 128);
410 KeyEvent[UngetCount++] = tmp;
413 void mutt_flushinp (void)
419 /* The argument can take 3 values:
420 * -1: restore the value of the last call
421 * 0: make the cursor invisible
422 * 1: make the cursor visible
424 void mutt_curs_set (int cursor)
426 static int SavedCursor = 1;
429 cursor = SavedCursor;
431 SavedCursor = cursor;
433 if (curs_set (cursor) == ERR) {
434 if (cursor == 1) /* cnorm */
435 curs_set (2); /* cvvis */
439 int mutt_multi_choice (const char *prompt, const char *letters)
445 mvwaddstr (stdscr, LINES - 1, 0, prompt);
450 if (ch.ch == -1 || CI_is_return (ch.ch)) {
455 p = strchr (letters, ch.ch);
457 choice = p - letters + 1;
460 else if (ch.ch <= '9' && ch.ch > '0') {
461 choice = ch.ch - '0';
462 if (choice <= m_strlen(letters))
468 CLEARLINE (LINES - 1);
474 * addwch would be provided by an up-to-date curses library
477 int mutt_addwch (wchar_t wc)
479 char buf[MB_LEN_MAX * 2];
483 p_clear(&mbstate, 1);
484 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
485 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
488 return waddstr (stdscr, buf);
491 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
494 return m_strcpy(s, len, "0K");
496 if (n < 10189) /* 0.1K - 9.9K */
497 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
499 if (n < 1023949) /* 10K - 999K */
500 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
501 return snprintf(s, len, "%ldK", (n + 51) / 1024);
503 if (n < 10433332) /* 1.0M - 9.9M */
504 return snprintf(s, len, "%3.1fM", n / 1048576.0);
506 /* (10433332 + 52428) / 1048576 = 10 */
507 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
511 * This formats a string, a bit like
512 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
513 * except that the widths refer to the number of character cells
517 void mutt_format_string (char *dest, ssize_t destlen,
518 int min_width, int max_width,
519 int right_justify, char m_pad_char,
520 const char *s, ssize_t n, int arboreal)
526 char scratch[MB_LEN_MAX];
527 mbstate_t mbstate1, mbstate2;
529 p_clear(&mbstate1, 1);
530 p_clear(&mbstate2, 1);
533 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
534 if (k == -1 || k == -2) {
535 k = (k == -1) ? 1 : n;
536 wc = CharsetReplacement;
538 if (arboreal && wc < M_TREE_MAX)
546 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
550 m_strncpy(p, destlen, scratch, k2);
555 w = destlen < min_width ? destlen : min_width;
558 else if (right_justify) {
563 dest[w] = m_pad_char;
573 * This formats a string rather like
574 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
575 * snprintf (dest, destlen, fmt, s);
576 * except that the numbers in the conversion specification refer to
577 * the number of character cells when printed.
580 static void mutt_format_s_x (char *dest, ssize_t destlen,
581 const char *prefix, const char *s, int arboreal)
583 int right_justify = 1;
586 int max_width = INT_MAX;
589 ++prefix, right_justify = 0;
590 min_width = strtol (prefix, &p, 10);
593 max_width = strtol (prefix, &p, 10);
598 mutt_format_string (dest, destlen, min_width, max_width,
599 right_justify, ' ', s, m_strlen(s), arboreal);
602 void mutt_format_s (char *dest, ssize_t destlen,
603 const char *prefix, const char *s)
605 mutt_format_s_x (dest, destlen, prefix, s, 0);
608 void mutt_format_s_tree (char *dest, ssize_t destlen,
609 const char *prefix, const char *s)
611 mutt_format_s_x (dest, destlen, prefix, s, 1);
615 * mutt_paddstr (n, s) is almost equivalent to
616 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (stdscr, bigbuf)
619 void mutt_paddstr (int n, const char *s)
624 ssize_t len = m_strlen(s);
627 p_clear(&mbstate, 1);
628 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
629 if (k == -1 || k == -2) {
630 k = (k == -1) ? 1 : len;
631 wc = CharsetReplacement;
639 waddnstr (stdscr, (char *) s, k);
644 waddch (stdscr, ' ');