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>
31 /* not possible to unget more than one char under some curses libs, and it
32 * is impossible to unget function keys in SLang, so roll our own input
35 ssize_t UngetCount = 0;
36 static ssize_t UngetBufLen = 0;
37 static event_t *KeyEvent;
39 event_t mutt_getch (void)
42 event_t err = { -1, OP_NULL }, ret;
44 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
45 return (KeyEvent[--UngetCount]);
49 mutt_allow_interrupt (1);
51 mutt_allow_interrupt (0);
61 return (ch == ctrl ('G') ? err : ret);
65 int waddwch(WINDOW *win, wchar_t wc)
67 char buf[MB_LEN_MAX * 2];
72 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1
73 || (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
75 return waddstr(win, buf);
80 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
81 int complete, int multiple, char ***files, int *numfiles)
86 ENTER_STATE *es = mutt_new_enter_state ();
89 CLEARLINE(stdscr, LINES - 1);
90 waddstr (stdscr, field);
93 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
96 CLEARLINE(stdscr, LINES - 1);
97 mutt_free_enter_state (&es);
102 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
106 set_option (OPTUNBUFFEREDINPUT);
107 rc = mutt_get_field (msg, buf, buflen, flags);
108 unset_option (OPTUNBUFFEREDINPUT);
113 void mutt_clear_error (void)
116 if (!option (OPTNOCURSES))
117 CLEARLINE(stdscr, LINES - 1);
120 void mutt_edit_file(const char *data)
122 char cmd[LONG_STRING];
125 m_quotefile_fmt(cmd, sizeof (cmd), mod_core.editor, data);
126 if (mutt_system (cmd) == -1)
127 mutt_error (_("Error running \"%s\"!"), cmd);
128 keypad (stdscr, TRUE);
129 clearok (stdscr, TRUE);
132 int mutt_yesorno (const char *msg, int def)
135 const char *yes = _("yes");
136 const char *no = _("no");
138 ssize_t answer_string_len;
148 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
149 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
150 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
151 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
153 CLEARLINE(stdscr, LINES - 1);
156 * In order to prevent the default answer to the question to wrapped
157 * around the screen in the even the question is wider than the screen,
158 * ensure there is enough room for the answer and truncate the question
161 answer_string = p_new(char, getmaxx(stdscr) + 1);
162 snprintf (answer_string, getmaxx(stdscr) + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
163 def == M_YES ? no : yes);
164 answer_string_len = m_strlen(answer_string);
165 wprintw (stdscr, "%.*s%s", getmaxx(stdscr) - answer_string_len, msg, answer_string);
166 p_delete(&answer_string);
171 if (CI_is_return (ch.ch))
179 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
185 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
186 (tolower (ch.ch) == *no)) {
200 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
203 CLEARLINE(stdscr, LINES - 1);
207 /* this function is called when the user presses the abort key */
208 void mutt_query_exit (void)
213 wtimeout (stdscr, -1); /* restore blocking operation */
214 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
223 void mutt_curses_error (const char *fmt, ...)
225 char TmpErrorbuf[STRING];
229 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
232 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
233 0, getmaxy(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
234 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
236 if (!option (OPTKEEPQUIET)) {
238 SETCOLOR(stdscr, MT_COLOR_ERROR);
239 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
241 SETCOLOR(stdscr, MT_COLOR_NORMAL);
245 set_option (OPTMSGERR);
248 void mutt_progress_bar (progress_t* progress, long pos) {
253 mutt_message (progress->msg);
256 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
265 if (pos >= progress->pos + (NetInc << 10)) {
267 pos = pos / (NetInc << 10) * (NetInc << 10);
268 mutt_pretty_size (posstr, sizeof (posstr), pos);
270 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
272 mutt_message ("%s %s", progress->msg, posstr);
276 void mutt_curses_message (const char *fmt, ...)
278 char TmpErrorbuf[STRING];
282 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
285 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
286 0, getmaxx(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
287 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
289 if (!option (OPTKEEPQUIET)) {
290 SETCOLOR(stdscr, MT_COLOR_MESSAGE);
291 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
293 SETCOLOR(stdscr, MT_COLOR_NORMAL);
297 unset_option (OPTMSGERR);
300 void mutt_show_error (void)
302 if (option (OPTKEEPQUIET))
305 SETCOLOR(stdscr, option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
306 CLEARLINE(stdscr, LINES - 1);
307 waddstr (stdscr, Errorbuf);
308 SETCOLOR(stdscr, MT_COLOR_NORMAL);
311 void curses_initialize(void)
314 if (start_color() == ERR || !has_colors() || COLORS < 8)
316 madtty_init_colors();
320 keypad(stdscr, true);
328 * prompts the user to enter a keystroke, and displays the octal value back
331 void mutt_what_key (void)
335 mvwprintw(stdscr, LINES - 1, 0, _("Enter keys (^G to abort): "));
338 if (ch != ERR && ch != ctrl ('G')) {
339 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
340 km_keyname (ch), ch, ch);
343 while (ch != ERR && ch != ctrl ('G'));
348 int mutt_any_key_to_continue (const char *s)
354 f = open ("/dev/tty", O_RDONLY);
356 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
357 t.c_lflag &= ~(ICANON | ECHO);
360 tcsetattr (f, TCSADRAIN, &t);
365 fputs (_("Press any key to continue..."), stdout);
369 tcsetattr (f, TCSADRAIN, &old);
371 fputs ("\r\n", stdout);
376 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
377 int *redraw, int buffy, int multiple, char ***files,
382 mvwaddstr(stdscr, LINES - 1, 0, (char *) prompt);
383 waddstr(stdscr, _(" ('?' for list): "));
385 waddstr (stdscr, buf);
391 CLEARLINE(stdscr, LINES - 1);
394 else if (ch.ch == '?') {
397 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
399 *redraw = REDRAW_FULL;
402 char *pc = p_new(char, m_strlen(prompt) + 3);
404 sprintf(pc, "%s: ", prompt);
405 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
407 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
411 MAYBE_REDRAW (*redraw);
418 void mutt_ungetch (int ch, int op)
425 if (UngetCount >= UngetBufLen)
426 p_realloc(&KeyEvent, UngetBufLen += 128);
428 KeyEvent[UngetCount++] = tmp;
431 void mutt_flushinp (void)
437 /* The argument can take 3 values:
438 * -1: restore the value of the last call
439 * 0: make the cursor invisible
440 * 1: make the cursor visible
442 void mutt_curs_set (int cursor)
444 static int SavedCursor = 1;
447 cursor = SavedCursor;
449 SavedCursor = cursor;
451 if (curs_set (cursor) == ERR) {
452 if (cursor == 1) /* cnorm */
453 curs_set (2); /* cvvis */
457 int mutt_multi_choice (const char *prompt, const char *letters)
463 mvwaddstr (stdscr, LINES - 1, 0, prompt);
468 if (ch.ch == -1 || CI_is_return (ch.ch)) {
473 p = strchr (letters, ch.ch);
475 choice = p - letters + 1;
478 else if (ch.ch <= '9' && ch.ch > '0') {
479 choice = ch.ch - '0';
480 if (choice <= m_strlen(letters))
486 CLEARLINE(stdscr, LINES - 1);
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 (main_w, bigbuf)
619 void mutt_paddstr(WINDOW *win, 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(win, (char *) s, k);