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);
327 int mutt_any_key_to_continue (const char *s)
333 f = open ("/dev/tty", O_RDONLY);
335 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
336 t.c_lflag &= ~(ICANON | ECHO);
339 tcsetattr (f, TCSADRAIN, &t);
344 fputs (_("Press any key to continue..."), stdout);
348 tcsetattr (f, TCSADRAIN, &old);
350 fputs ("\r\n", stdout);
355 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
356 int *redraw, int buffy, int multiple, char ***files,
361 mvwaddstr(stdscr, LINES - 1, 0, (char *) prompt);
362 waddstr(stdscr, _(" ('?' for list): "));
364 waddstr (stdscr, buf);
370 CLEARLINE(stdscr, LINES - 1);
373 else if (ch.ch == '?') {
376 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
378 *redraw = REDRAW_FULL;
381 char *pc = p_new(char, m_strlen(prompt) + 3);
383 sprintf(pc, "%s: ", prompt);
384 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
386 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
390 MAYBE_REDRAW (*redraw);
397 void mutt_ungetch (int ch, int op)
404 if (UngetCount >= UngetBufLen)
405 p_realloc(&KeyEvent, UngetBufLen += 128);
407 KeyEvent[UngetCount++] = tmp;
410 void mutt_flushinp (void)
416 /* The argument can take 3 values:
417 * -1: restore the value of the last call
418 * 0: make the cursor invisible
419 * 1: make the cursor visible
421 void mutt_curs_set (int cursor)
423 static int SavedCursor = 1;
426 cursor = SavedCursor;
428 SavedCursor = cursor;
430 if (curs_set (cursor) == ERR) {
431 if (cursor == 1) /* cnorm */
432 curs_set (2); /* cvvis */
436 int mutt_multi_choice (const char *prompt, const char *letters)
442 mvwaddstr (stdscr, LINES - 1, 0, prompt);
447 if (ch.ch == -1 || CI_is_return (ch.ch)) {
452 p = strchr (letters, ch.ch);
454 choice = p - letters + 1;
457 else if (ch.ch <= '9' && ch.ch > '0') {
458 choice = ch.ch - '0';
459 if (choice <= m_strlen(letters))
465 CLEARLINE(stdscr, LINES - 1);
470 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
473 return m_strcpy(s, len, "0K");
475 if (n < 10189) /* 0.1K - 9.9K */
476 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
478 if (n < 1023949) /* 10K - 999K */
479 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
480 return snprintf(s, len, "%ldK", (n + 51) / 1024);
482 if (n < 10433332) /* 1.0M - 9.9M */
483 return snprintf(s, len, "%3.1fM", n / 1048576.0);
485 /* (10433332 + 52428) / 1048576 = 10 */
486 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
490 * This formats a string, a bit like
491 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
492 * except that the widths refer to the number of character cells
496 void mutt_format_string (char *dest, ssize_t destlen,
497 int min_width, int max_width,
498 int right_justify, char m_pad_char,
499 const char *s, ssize_t n, int arboreal)
505 char scratch[MB_LEN_MAX];
506 mbstate_t mbstate1, mbstate2;
508 p_clear(&mbstate1, 1);
509 p_clear(&mbstate2, 1);
512 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
513 if (k == -1 || k == -2) {
514 k = (k == -1) ? 1 : n;
515 wc = CharsetReplacement;
517 if (arboreal && wc < M_TREE_MAX)
525 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
529 m_strncpy(p, destlen, scratch, k2);
534 w = destlen < min_width ? destlen : min_width;
537 else if (right_justify) {
542 dest[w] = m_pad_char;
552 * This formats a string rather like
553 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
554 * snprintf (dest, destlen, fmt, s);
555 * except that the numbers in the conversion specification refer to
556 * the number of character cells when printed.
559 static void mutt_format_s_x (char *dest, ssize_t destlen,
560 const char *prefix, const char *s, int arboreal)
562 int right_justify = 1;
565 int max_width = INT_MAX;
568 ++prefix, right_justify = 0;
569 min_width = strtol (prefix, &p, 10);
572 max_width = strtol (prefix, &p, 10);
577 mutt_format_string (dest, destlen, min_width, max_width,
578 right_justify, ' ', s, m_strlen(s), arboreal);
581 void mutt_format_s (char *dest, ssize_t destlen,
582 const char *prefix, const char *s)
584 mutt_format_s_x (dest, destlen, prefix, s, 0);
587 void mutt_format_s_tree (char *dest, ssize_t destlen,
588 const char *prefix, const char *s)
590 mutt_format_s_x (dest, destlen, prefix, s, 1);
594 * mutt_paddstr (n, s) is almost equivalent to
595 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (main_w, bigbuf)
598 void mutt_paddstr(WINDOW *win, int n, const char *s)
603 ssize_t len = m_strlen(s);
606 p_clear(&mbstate, 1);
607 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
608 if (k == -1 || k == -2) {
609 k = (k == -1) ? 1 : len;
610 wc = CharsetReplacement;
618 waddnstr(win, (char *) s, k);