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 static struct timeval const slice = { 0, 1000 * 1000 / 100 };
121 static struct timeval timeval_add(struct timeval a, struct timeval b)
123 int usec = a.tv_usec + b.tv_usec;
124 a.tv_sec += b.tv_sec;
125 while (usec > 1000 * 1000) {
133 static int is_expired(struct timeval now, struct timeval expiry)
135 return now.tv_sec > expiry.tv_sec
136 || (now.tv_sec == expiry.tv_sec && now.tv_usec > expiry.tv_usec);
139 void mutt_edit_file(const char *data)
142 const char *args[] = { "/bin/sh", "-c", cmd, NULL };
143 int dirty = 0, ch, res, mh, mw, pty, pid;
147 m_quotefile_fmt(cmd, sizeof(cmd), mod_core.editor, data);
148 getmaxyx(main_w, mh, mw);
151 rt = madtty_create(mh - 1, mw);
152 pid = madtty_forkpty(rt, args[0], args, &pty);
155 mutt_error(_("unable to start editor"));
159 nodelay(stdscr, true);
160 gettimeofday(&next, NULL);
162 struct timeval tv = { 0, 1000 * 1000 / 100 };
169 if (select(pty + 1, &rfds, NULL, NULL, &tv) < 0)
172 if (FD_ISSET(pty, &rfds)) {
177 while ((ch = getch()) != ERR) {
178 madtty_keypress(rt, ch); /* pass the keypress for handling */
182 gettimeofday(&tv, NULL);
183 if (dirty && is_expired(tv, next)) {
184 madtty_draw(rt, main_w, 0, 0);
187 next = timeval_add(tv, slice);
190 while (waitpid(pid, &res, 0) < 0 && errno == EINTR);
191 nodelay(stdscr, false);
196 int mutt_yesorno (const char *msg, int def)
199 const char *yes = _("yes");
200 const char *no = _("no");
202 ssize_t answer_string_len;
212 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
213 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
214 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
215 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
217 CLEARLINE(stdscr, LINES - 1);
220 * In order to prevent the default answer to the question to wrapped
221 * around the screen in the even the question is wider than the screen,
222 * ensure there is enough room for the answer and truncate the question
225 answer_string = p_new(char, getmaxx(stdscr) + 1);
226 snprintf (answer_string, getmaxx(stdscr) + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
227 def == M_YES ? no : yes);
228 answer_string_len = m_strlen(answer_string);
229 wprintw (stdscr, "%.*s%s", getmaxx(stdscr) - answer_string_len, msg, answer_string);
230 p_delete(&answer_string);
235 if (CI_is_return (ch.ch))
243 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
249 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
250 (tolower (ch.ch) == *no)) {
264 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
267 CLEARLINE(stdscr, LINES - 1);
271 /* this function is called when the user presses the abort key */
272 void mutt_query_exit (void)
277 wtimeout (stdscr, -1); /* restore blocking operation */
278 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
287 void mutt_curses_error (const char *fmt, ...)
289 char TmpErrorbuf[STRING];
293 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
296 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
297 0, getmaxy(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
298 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
300 if (!option (OPTKEEPQUIET)) {
302 SETCOLOR(stdscr, MT_COLOR_ERROR);
303 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
305 SETCOLOR(stdscr, MT_COLOR_NORMAL);
309 set_option (OPTMSGERR);
312 void mutt_progress_bar (progress_t* progress, long pos) {
317 mutt_message (progress->msg);
320 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
329 if (pos >= progress->pos + (NetInc << 10)) {
331 pos = pos / (NetInc << 10) * (NetInc << 10);
332 mutt_pretty_size (posstr, sizeof (posstr), pos);
334 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
336 mutt_message ("%s %s", progress->msg, posstr);
340 void mutt_curses_message (const char *fmt, ...)
342 char TmpErrorbuf[STRING];
346 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
349 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
350 0, getmaxx(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
351 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
353 if (!option (OPTKEEPQUIET)) {
354 SETCOLOR(stdscr, MT_COLOR_MESSAGE);
355 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
357 SETCOLOR(stdscr, MT_COLOR_NORMAL);
361 unset_option (OPTMSGERR);
364 void mutt_show_error (void)
366 if (option (OPTKEEPQUIET))
369 SETCOLOR(stdscr, option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
370 CLEARLINE(stdscr, LINES - 1);
371 waddstr (stdscr, Errorbuf);
372 SETCOLOR(stdscr, MT_COLOR_NORMAL);
375 void curses_initialize(void)
378 if (start_color() == ERR || !has_colors() || COLORS < 8)
380 madtty_init_colors();
384 keypad(stdscr, true);
391 int mutt_any_key_to_continue (const char *s)
397 f = open ("/dev/tty", O_RDONLY);
399 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
400 t.c_lflag &= ~(ICANON | ECHO);
403 tcsetattr (f, TCSADRAIN, &t);
408 fputs (_("Press any key to continue..."), stdout);
412 tcsetattr (f, TCSADRAIN, &old);
414 fputs ("\r\n", stdout);
419 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
420 int *redraw, int buffy, int multiple, char ***files,
425 mvwaddstr(stdscr, LINES - 1, 0, (char *) prompt);
426 waddstr(stdscr, _(" ('?' for list): "));
428 waddstr (stdscr, buf);
434 CLEARLINE(stdscr, LINES - 1);
437 else if (ch.ch == '?') {
440 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
442 *redraw = REDRAW_FULL;
445 char *pc = p_new(char, m_strlen(prompt) + 3);
447 sprintf(pc, "%s: ", prompt);
448 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
450 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
454 MAYBE_REDRAW (*redraw);
461 void mutt_ungetch (int ch, int op)
468 if (UngetCount >= UngetBufLen)
469 p_realloc(&KeyEvent, UngetBufLen += 128);
471 KeyEvent[UngetCount++] = tmp;
474 void mutt_flushinp (void)
480 /* The argument can take 3 values:
481 * -1: restore the value of the last call
482 * 0: make the cursor invisible
483 * 1: make the cursor visible
485 void mutt_curs_set (int cursor)
487 static int SavedCursor = 1;
490 cursor = SavedCursor;
492 SavedCursor = cursor;
494 if (curs_set (cursor) == ERR) {
495 if (cursor == 1) /* cnorm */
496 curs_set (2); /* cvvis */
500 int mutt_multi_choice (const char *prompt, const char *letters)
506 mvwaddstr (stdscr, LINES - 1, 0, prompt);
511 if (ch.ch == -1 || CI_is_return (ch.ch)) {
516 p = strchr (letters, ch.ch);
518 choice = p - letters + 1;
521 else if (ch.ch <= '9' && ch.ch > '0') {
522 choice = ch.ch - '0';
523 if (choice <= m_strlen(letters))
529 CLEARLINE(stdscr, LINES - 1);
534 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
537 return m_strcpy(s, len, "0K");
539 if (n < 10189) /* 0.1K - 9.9K */
540 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
542 if (n < 1023949) /* 10K - 999K */
543 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
544 return snprintf(s, len, "%ldK", (n + 51) / 1024);
546 if (n < 10433332) /* 1.0M - 9.9M */
547 return snprintf(s, len, "%3.1fM", n / 1048576.0);
549 /* (10433332 + 52428) / 1048576 = 10 */
550 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
554 * This formats a string, a bit like
555 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
556 * except that the widths refer to the number of character cells
560 void mutt_format_string (char *dest, ssize_t destlen,
561 int min_width, int max_width,
562 int right_justify, char m_pad_char,
563 const char *s, ssize_t n, int arboreal)
569 char scratch[MB_LEN_MAX];
570 mbstate_t mbstate1, mbstate2;
572 p_clear(&mbstate1, 1);
573 p_clear(&mbstate2, 1);
576 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
577 if (k == -1 || k == -2) {
578 k = (k == -1) ? 1 : n;
579 wc = CharsetReplacement;
581 if (arboreal && wc < M_TREE_MAX)
589 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
593 m_strncpy(p, destlen, scratch, k2);
598 w = destlen < min_width ? destlen : min_width;
601 else if (right_justify) {
606 dest[w] = m_pad_char;
616 * This formats a string rather like
617 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
618 * snprintf (dest, destlen, fmt, s);
619 * except that the numbers in the conversion specification refer to
620 * the number of character cells when printed.
623 static void mutt_format_s_x (char *dest, ssize_t destlen,
624 const char *prefix, const char *s, int arboreal)
626 int right_justify = 1;
629 int max_width = INT_MAX;
632 ++prefix, right_justify = 0;
633 min_width = strtol (prefix, &p, 10);
636 max_width = strtol (prefix, &p, 10);
641 mutt_format_string (dest, destlen, min_width, max_width,
642 right_justify, ' ', s, m_strlen(s), arboreal);
645 void mutt_format_s (char *dest, ssize_t destlen,
646 const char *prefix, const char *s)
648 mutt_format_s_x (dest, destlen, prefix, s, 0);
651 void mutt_format_s_tree (char *dest, ssize_t destlen,
652 const char *prefix, const char *s)
654 mutt_format_s_x (dest, destlen, prefix, s, 1);
658 * mutt_paddstr (n, s) is almost equivalent to
659 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (main_w, bigbuf)
662 void mutt_paddstr(WINDOW *win, int n, const char *s)
667 ssize_t len = m_strlen(s);
670 p_clear(&mbstate, 1);
671 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
672 if (k == -1 || k == -2) {
673 k = (k == -1) ? 1 : len;
674 wc = CharsetReplacement;
682 waddnstr(win, (char *) s, k);