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;
64 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
68 set_option (OPTUNBUFFEREDINPUT);
69 rc = mutt_get_field (msg, buf, buflen, flags);
70 unset_option (OPTUNBUFFEREDINPUT);
75 void mutt_clear_error (void)
78 if (!option (OPTNOCURSES))
79 CLEARLINE(stdscr, LINES - 1);
82 static struct timeval const slice = { 0, 1000 * 1000 / 100 };
83 static struct timeval timeval_add(struct timeval a, struct timeval b)
85 int usec = a.tv_usec + b.tv_usec;
87 while (usec > 1000 * 1000) {
95 static int is_expired(struct timeval now, struct timeval expiry)
97 return now.tv_sec > expiry.tv_sec
98 || (now.tv_sec == expiry.tv_sec && now.tv_usec > expiry.tv_usec);
101 void mutt_edit_file(const char *data)
104 const char *args[] = { "/bin/sh", "-c", cmd, NULL };
105 int dirty = 0, ch, res, mh, mw, pty, pid;
109 m_quotefile_fmt(cmd, sizeof(cmd), mod_core.editor, data);
110 getmaxyx(main_w, mh, mw);
113 rt = madtty_create(mh - 2, mw);
114 pid = madtty_forkpty(rt, args[0], args, &pty);
117 mutt_error(_("unable to start editor"));
121 SETCOLOR(main_w, MT_COLOR_SIDEBAR);
122 mvwhline(main_w, 0, 0, ACS_HLINE, mw);
124 nodelay(stdscr, true);
125 gettimeofday(&next, NULL);
127 struct timeval tv = { 0, 1000 * 1000 / 100 };
134 if (select(pty + 1, &rfds, NULL, NULL, &tv) < 0)
137 if (FD_ISSET(pty, &rfds)) {
142 while ((ch = getch()) != ERR) {
143 madtty_keypress(rt, ch); /* pass the keypress for handling */
147 gettimeofday(&tv, NULL);
148 if (dirty && is_expired(tv, next)) {
149 madtty_draw(rt, main_w, 1, 0);
152 next = timeval_add(tv, slice);
155 while (waitpid(pid, &res, 0) < 0 && errno == EINTR);
156 nodelay(stdscr, false);
161 int mutt_yesorno (const char *msg, int def)
164 const char *yes = _("yes");
165 const char *no = _("no");
167 ssize_t answer_string_len;
177 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
178 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
179 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
180 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
182 CLEARLINE(stdscr, LINES - 1);
185 * In order to prevent the default answer to the question to wrapped
186 * around the screen in the even the question is wider than the screen,
187 * ensure there is enough room for the answer and truncate the question
190 answer_string = p_new(char, getmaxx(stdscr) + 1);
191 snprintf (answer_string, getmaxx(stdscr) + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
192 def == M_YES ? no : yes);
193 answer_string_len = m_strlen(answer_string);
194 wprintw (stdscr, "%.*s%s", getmaxx(stdscr) - answer_string_len, msg, answer_string);
195 p_delete(&answer_string);
200 if (CI_is_return (ch.ch))
208 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
214 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
215 (tolower (ch.ch) == *no)) {
229 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
232 CLEARLINE(stdscr, LINES - 1);
236 /* this function is called when the user presses the abort key */
237 void mutt_query_exit (void)
242 wtimeout (stdscr, -1); /* restore blocking operation */
243 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
252 void mutt_curses_error (const char *fmt, ...)
254 char TmpErrorbuf[STRING];
258 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
261 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
262 0, getmaxy(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
263 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
265 if (!option (OPTKEEPQUIET)) {
267 SETCOLOR(stdscr, MT_COLOR_ERROR);
268 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
270 SETCOLOR(stdscr, MT_COLOR_NORMAL);
274 set_option (OPTMSGERR);
277 void mutt_progress_bar (progress_t* progress, long pos) {
282 mutt_message (progress->msg);
285 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
294 if (pos >= progress->pos + (NetInc << 10)) {
296 pos = pos / (NetInc << 10) * (NetInc << 10);
297 mutt_pretty_size (posstr, sizeof (posstr), pos);
299 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
301 mutt_message ("%s %s", progress->msg, posstr);
305 void mutt_curses_message (const char *fmt, ...)
307 char TmpErrorbuf[STRING];
311 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
314 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
315 0, getmaxx(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
316 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
318 if (!option (OPTKEEPQUIET)) {
319 SETCOLOR(stdscr, MT_COLOR_MESSAGE);
320 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
322 SETCOLOR(stdscr, MT_COLOR_NORMAL);
326 unset_option (OPTMSGERR);
329 void mutt_show_error (void)
331 if (option (OPTKEEPQUIET))
334 SETCOLOR(stdscr, option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
335 CLEARLINE(stdscr, LINES - 1);
336 waddstr (stdscr, Errorbuf);
337 SETCOLOR(stdscr, MT_COLOR_NORMAL);
340 void curses_initialize(void)
343 if (start_color() == ERR || !has_colors() || COLORS < 8)
345 madtty_init_colors();
346 madtty_init_vt100_graphics();
350 keypad(stdscr, true);
357 int mutt_any_key_to_continue (const char *s)
363 f = open ("/dev/tty", O_RDONLY);
365 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
366 t.c_lflag &= ~(ICANON | ECHO);
369 tcsetattr (f, TCSADRAIN, &t);
374 fputs (_("Press any key to continue..."), stdout);
378 tcsetattr (f, TCSADRAIN, &old);
380 fputs ("\r\n", stdout);
385 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
386 int *redraw, int buffy, int multiple, char ***files,
391 mvwaddstr(stdscr, LINES - 1, 0, (char *) prompt);
392 waddstr(stdscr, _(" ('?' for list): "));
394 waddstr (stdscr, buf);
400 CLEARLINE(stdscr, LINES - 1);
403 else if (ch.ch == '?') {
406 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
408 *redraw = REDRAW_FULL;
411 char *pc = p_new(char, m_strlen(prompt) + 3);
413 sprintf(pc, "%s: ", prompt);
414 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
416 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
420 MAYBE_REDRAW (*redraw);
427 void mutt_ungetch (int ch, int op)
434 if (UngetCount >= UngetBufLen)
435 p_realloc(&KeyEvent, UngetBufLen += 128);
437 KeyEvent[UngetCount++] = tmp;
440 void mutt_flushinp (void)
446 /* The argument can take 3 values:
447 * -1: restore the value of the last call
448 * 0: make the cursor invisible
449 * 1: make the cursor visible
451 void mutt_curs_set (int cursor)
453 static int SavedCursor = 1;
456 cursor = SavedCursor;
458 SavedCursor = cursor;
460 if (curs_set (cursor) == ERR) {
461 if (cursor == 1) /* cnorm */
462 curs_set (2); /* cvvis */
466 int mutt_multi_choice (const char *prompt, const char *letters)
472 mvwaddstr (stdscr, LINES - 1, 0, prompt);
477 if (ch.ch == -1 || CI_is_return (ch.ch)) {
482 p = strchr (letters, ch.ch);
484 choice = p - letters + 1;
487 else if (ch.ch <= '9' && ch.ch > '0') {
488 choice = ch.ch - '0';
489 if (choice <= m_strlen(letters))
495 CLEARLINE(stdscr, LINES - 1);
500 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
503 return m_strcpy(s, len, "0K");
505 if (n < 10189) /* 0.1K - 9.9K */
506 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
508 if (n < 1023949) /* 10K - 999K */
509 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
510 return snprintf(s, len, "%ldK", (n + 51) / 1024L);
512 if (n < 10433332) /* 1.0M - 9.9M */
513 return snprintf(s, len, "%3.1fM", n / 1048576.0);
515 /* (10433332 + 52428) / 1048576 = 10 */
516 return snprintf (s, len, "%ldM", (n + 52428) / 1048576L);
520 * This formats a string, a bit like
521 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
522 * except that the widths refer to the number of character cells
526 void mutt_format_string (char *dest, ssize_t destlen,
527 int min_width, int max_width,
528 int right_justify, char m_pad_char,
529 const char *s, ssize_t n, int arboreal)
535 char scratch[MB_LEN_MAX];
536 mbstate_t mbstate1, mbstate2;
538 p_clear(&mbstate1, 1);
539 p_clear(&mbstate2, 1);
542 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
543 if (k == -1 || k == -2) {
544 k = (k == -1) ? 1 : n;
545 wc = CharsetReplacement;
547 if (arboreal && wc < M_TREE_MAX)
555 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
559 m_strncpy(p, destlen, scratch, k2);
564 w = destlen < min_width ? destlen : min_width;
567 else if (right_justify) {
572 dest[w] = m_pad_char;
582 * This formats a string rather like
583 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
584 * snprintf (dest, destlen, fmt, s);
585 * except that the numbers in the conversion specification refer to
586 * the number of character cells when printed.
589 static void mutt_format_s_x (char *dest, ssize_t destlen,
590 const char *prefix, const char *s, int arboreal)
592 int right_justify = 1;
595 int max_width = INT_MAX;
598 ++prefix, right_justify = 0;
599 min_width = strtol (prefix, &p, 10);
602 max_width = strtol (prefix, &p, 10);
607 mutt_format_string (dest, destlen, min_width, max_width,
608 right_justify, ' ', s, m_strlen(s), arboreal);
611 void mutt_format_s (char *dest, ssize_t destlen,
612 const char *prefix, const char *s)
614 mutt_format_s_x (dest, destlen, prefix, s, 0);
617 void mutt_format_s_tree (char *dest, ssize_t destlen,
618 const char *prefix, const char *s)
620 mutt_format_s_x (dest, destlen, prefix, s, 1);
624 * mutt_paddstr (n, s) is almost equivalent to
625 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (main_w, bigbuf)
628 void mutt_paddstr(WINDOW *win, int n, const char *s)
633 ssize_t len = m_strlen(s);
636 p_clear(&mbstate, 1);
637 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
638 if (k == -1 || k == -2) {
639 k = (k == -1) ? 1 : len;
640 wc = CharsetReplacement;
648 waddnstr(win, (char *) s, k);