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);
64 int waddwch(WINDOW *win, wchar_t wc)
66 char buf[MB_LEN_MAX * 2];
71 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1
72 || (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
74 return waddstr(win, buf);
79 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
80 int complete, int multiple, char ***files, int *numfiles)
85 ENTER_STATE *es = mutt_new_enter_state ();
88 CLEARLINE(stdscr, LINES - 1);
89 waddstr (stdscr, field);
92 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
95 CLEARLINE(stdscr, LINES - 1);
96 mutt_free_enter_state (&es);
101 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
105 set_option (OPTUNBUFFEREDINPUT);
106 rc = mutt_get_field (msg, buf, buflen, flags);
107 unset_option (OPTUNBUFFEREDINPUT);
112 void mutt_clear_error (void)
115 if (!option (OPTNOCURSES))
116 CLEARLINE(stdscr, LINES - 1);
119 void mutt_edit_file(const char *data)
121 char cmd[LONG_STRING];
124 m_quotefile_fmt(cmd, sizeof (cmd), mod_core.editor, data);
125 if (mutt_system (cmd) == -1)
126 mutt_error (_("Error running \"%s\"!"), cmd);
127 keypad (stdscr, TRUE);
128 clearok (stdscr, TRUE);
131 int mutt_yesorno (const char *msg, int def)
134 const char *yes = _("yes");
135 const char *no = _("no");
137 ssize_t answer_string_len;
147 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
148 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
149 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
150 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
152 CLEARLINE(stdscr, LINES - 1);
155 * In order to prevent the default answer to the question to wrapped
156 * around the screen in the even the question is wider than the screen,
157 * ensure there is enough room for the answer and truncate the question
160 answer_string = p_new(char, getmaxx(stdscr) + 1);
161 snprintf (answer_string, getmaxx(stdscr) + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
162 def == M_YES ? no : yes);
163 answer_string_len = m_strlen(answer_string);
164 wprintw (stdscr, "%.*s%s", getmaxx(stdscr) - answer_string_len, msg, answer_string);
165 p_delete(&answer_string);
170 if (CI_is_return (ch.ch))
178 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
184 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
185 (tolower (ch.ch) == *no)) {
199 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
202 CLEARLINE(stdscr, LINES - 1);
206 /* this function is called when the user presses the abort key */
207 void mutt_query_exit (void)
212 wtimeout (stdscr, -1); /* restore blocking operation */
213 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
222 void mutt_curses_error (const char *fmt, ...)
224 char TmpErrorbuf[STRING];
228 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
231 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
232 0, getmaxy(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
233 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
235 if (!option (OPTKEEPQUIET)) {
237 SETCOLOR(stdscr, MT_COLOR_ERROR);
238 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
240 SETCOLOR(stdscr, MT_COLOR_NORMAL);
244 set_option (OPTMSGERR);
247 void mutt_progress_bar (progress_t* progress, long pos) {
252 mutt_message (progress->msg);
255 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
264 if (pos >= progress->pos + (NetInc << 10)) {
266 pos = pos / (NetInc << 10) * (NetInc << 10);
267 mutt_pretty_size (posstr, sizeof (posstr), pos);
269 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
271 mutt_message ("%s %s", progress->msg, posstr);
275 void mutt_curses_message (const char *fmt, ...)
277 char TmpErrorbuf[STRING];
281 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
284 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
285 0, getmaxx(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
286 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
288 if (!option (OPTKEEPQUIET)) {
289 SETCOLOR(stdscr, MT_COLOR_MESSAGE);
290 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
292 SETCOLOR(stdscr, MT_COLOR_NORMAL);
296 unset_option (OPTMSGERR);
299 void mutt_show_error (void)
301 if (option (OPTKEEPQUIET))
304 SETCOLOR(stdscr, option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
305 CLEARLINE(stdscr, LINES - 1);
306 waddstr (stdscr, Errorbuf);
307 SETCOLOR(stdscr, MT_COLOR_NORMAL);
310 void curses_initialize(void)
312 if (initscr () == NULL) {
313 puts _("Error initializing terminal.");
317 keypad(stdscr, true);
320 typeahead (-1); /* simulate smooth scrolling */
325 * prompts the user to enter a keystroke, and displays the octal value back
328 void mutt_what_key (void)
332 mvwprintw(stdscr, LINES - 1, 0, _("Enter keys (^G to abort): "));
335 if (ch != ERR && ch != ctrl ('G')) {
336 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
337 km_keyname (ch), ch, ch);
340 while (ch != ERR && ch != ctrl ('G'));
345 int mutt_any_key_to_continue (const char *s)
351 f = open ("/dev/tty", O_RDONLY);
353 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
354 t.c_lflag &= ~(ICANON | ECHO);
357 tcsetattr (f, TCSADRAIN, &t);
362 fputs (_("Press any key to continue..."), stdout);
366 tcsetattr (f, TCSADRAIN, &old);
368 fputs ("\r\n", stdout);
373 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
374 int *redraw, int buffy, int multiple, char ***files,
379 mvwaddstr(stdscr, LINES - 1, 0, (char *) prompt);
380 waddstr(stdscr, _(" ('?' for list): "));
382 waddstr (stdscr, buf);
388 CLEARLINE(stdscr, LINES - 1);
391 else if (ch.ch == '?') {
394 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
396 *redraw = REDRAW_FULL;
399 char *pc = p_new(char, m_strlen(prompt) + 3);
401 sprintf(pc, "%s: ", prompt);
402 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
404 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
408 MAYBE_REDRAW (*redraw);
415 void mutt_ungetch (int ch, int op)
422 if (UngetCount >= UngetBufLen)
423 p_realloc(&KeyEvent, UngetBufLen += 128);
425 KeyEvent[UngetCount++] = tmp;
428 void mutt_flushinp (void)
434 /* The argument can take 3 values:
435 * -1: restore the value of the last call
436 * 0: make the cursor invisible
437 * 1: make the cursor visible
439 void mutt_curs_set (int cursor)
441 static int SavedCursor = 1;
444 cursor = SavedCursor;
446 SavedCursor = cursor;
448 if (curs_set (cursor) == ERR) {
449 if (cursor == 1) /* cnorm */
450 curs_set (2); /* cvvis */
454 int mutt_multi_choice (const char *prompt, const char *letters)
460 mvwaddstr (stdscr, LINES - 1, 0, prompt);
465 if (ch.ch == -1 || CI_is_return (ch.ch)) {
470 p = strchr (letters, ch.ch);
472 choice = p - letters + 1;
475 else if (ch.ch <= '9' && ch.ch > '0') {
476 choice = ch.ch - '0';
477 if (choice <= m_strlen(letters))
483 CLEARLINE(stdscr, LINES - 1);
488 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
491 return m_strcpy(s, len, "0K");
493 if (n < 10189) /* 0.1K - 9.9K */
494 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
496 if (n < 1023949) /* 10K - 999K */
497 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
498 return snprintf(s, len, "%ldK", (n + 51) / 1024);
500 if (n < 10433332) /* 1.0M - 9.9M */
501 return snprintf(s, len, "%3.1fM", n / 1048576.0);
503 /* (10433332 + 52428) / 1048576 = 10 */
504 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
508 * This formats a string, a bit like
509 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
510 * except that the widths refer to the number of character cells
514 void mutt_format_string (char *dest, ssize_t destlen,
515 int min_width, int max_width,
516 int right_justify, char m_pad_char,
517 const char *s, ssize_t n, int arboreal)
523 char scratch[MB_LEN_MAX];
524 mbstate_t mbstate1, mbstate2;
526 p_clear(&mbstate1, 1);
527 p_clear(&mbstate2, 1);
530 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
531 if (k == -1 || k == -2) {
532 k = (k == -1) ? 1 : n;
533 wc = CharsetReplacement;
535 if (arboreal && wc < M_TREE_MAX)
543 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
547 m_strncpy(p, destlen, scratch, k2);
552 w = destlen < min_width ? destlen : min_width;
555 else if (right_justify) {
560 dest[w] = m_pad_char;
570 * This formats a string rather like
571 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
572 * snprintf (dest, destlen, fmt, s);
573 * except that the numbers in the conversion specification refer to
574 * the number of character cells when printed.
577 static void mutt_format_s_x (char *dest, ssize_t destlen,
578 const char *prefix, const char *s, int arboreal)
580 int right_justify = 1;
583 int max_width = INT_MAX;
586 ++prefix, right_justify = 0;
587 min_width = strtol (prefix, &p, 10);
590 max_width = strtol (prefix, &p, 10);
595 mutt_format_string (dest, destlen, min_width, max_width,
596 right_justify, ' ', s, m_strlen(s), arboreal);
599 void mutt_format_s (char *dest, ssize_t destlen,
600 const char *prefix, const char *s)
602 mutt_format_s_x (dest, destlen, prefix, s, 0);
605 void mutt_format_s_tree (char *dest, ssize_t destlen,
606 const char *prefix, const char *s)
608 mutt_format_s_x (dest, destlen, prefix, s, 1);
612 * mutt_paddstr (n, s) is almost equivalent to
613 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (main_w, bigbuf)
616 void mutt_paddstr(WINDOW *win, int n, const char *s)
621 ssize_t len = m_strlen(s);
624 p_clear(&mbstate, 1);
625 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
626 if (k == -1 || k == -2) {
627 k = (k == -1) ? 1 : len;
628 wc = CharsetReplacement;
636 waddnstr(win, (char *) s, k);