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);
63 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
67 set_option (OPTUNBUFFEREDINPUT);
68 rc = mutt_get_field (msg, buf, buflen, flags);
69 unset_option (OPTUNBUFFEREDINPUT);
74 void mutt_clear_error (void)
77 if (!option (OPTNOCURSES))
78 CLEARLINE(stdscr, LINES - 1);
81 static struct timeval const slice = { 0, 1000 * 1000 / 100 };
82 static struct timeval timeval_add(struct timeval a, struct timeval b)
84 int usec = a.tv_usec + b.tv_usec;
86 while (usec > 1000 * 1000) {
94 static int is_expired(struct timeval now, struct timeval expiry)
96 return now.tv_sec > expiry.tv_sec
97 || (now.tv_sec == expiry.tv_sec && now.tv_usec > expiry.tv_usec);
100 void mutt_edit_file(const char *data)
103 const char *args[] = { "/bin/sh", "-c", cmd, NULL };
104 int dirty = 0, ch, res, mh, mw, pty, pid;
108 m_quotefile_fmt(cmd, sizeof(cmd), mod_core.editor, data);
109 getmaxyx(main_w, mh, mw);
112 rt = madtty_create(mh - 2, mw);
113 pid = madtty_forkpty(rt, args[0], args, &pty);
116 mutt_error(_("unable to start editor"));
120 SETCOLOR(main_w, MT_COLOR_SIDEBAR);
121 mvwhline(main_w, 0, 0, ACS_HLINE, mw);
123 nodelay(stdscr, true);
124 gettimeofday(&next, NULL);
126 struct timeval tv = { 0, 1000 * 1000 / 100 };
133 if (select(pty + 1, &rfds, NULL, NULL, &tv) < 0)
136 if (FD_ISSET(pty, &rfds)) {
141 while ((ch = getch()) != ERR) {
142 madtty_keypress(rt, ch); /* pass the keypress for handling */
146 gettimeofday(&tv, NULL);
147 if (dirty && is_expired(tv, next)) {
148 madtty_draw(rt, main_w, 1, 0);
151 next = timeval_add(tv, slice);
154 while (waitpid(pid, &res, 0) < 0 && errno == EINTR);
155 nodelay(stdscr, false);
160 int mutt_yesorno (const char *msg, int def)
163 const char *yes = _("yes");
164 const char *no = _("no");
166 ssize_t answer_string_len;
176 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
177 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
178 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
179 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
181 CLEARLINE(stdscr, LINES - 1);
184 * In order to prevent the default answer to the question to wrapped
185 * around the screen in the even the question is wider than the screen,
186 * ensure there is enough room for the answer and truncate the question
189 answer_string = p_new(char, getmaxx(stdscr) + 1);
190 snprintf (answer_string, getmaxx(stdscr) + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
191 def == M_YES ? no : yes);
192 answer_string_len = m_strlen(answer_string);
193 wprintw (stdscr, "%.*s%s", getmaxx(stdscr) - answer_string_len, msg, answer_string);
194 p_delete(&answer_string);
199 if (CI_is_return (ch.ch))
207 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
213 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
214 (tolower (ch.ch) == *no)) {
228 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
231 CLEARLINE(stdscr, LINES - 1);
235 /* this function is called when the user presses the abort key */
236 void mutt_query_exit (void)
241 wtimeout (stdscr, -1); /* restore blocking operation */
242 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
251 void mutt_curses_error (const char *fmt, ...)
253 char TmpErrorbuf[STRING];
257 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
260 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
261 0, getmaxy(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
262 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
264 if (!option (OPTKEEPQUIET)) {
266 SETCOLOR(stdscr, MT_COLOR_ERROR);
267 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
269 SETCOLOR(stdscr, MT_COLOR_NORMAL);
273 set_option (OPTMSGERR);
276 void mutt_progress_bar (progress_t* progress, long pos) {
281 mutt_message (progress->msg);
284 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
293 if (pos >= progress->pos + (NetInc << 10)) {
295 pos = pos / (NetInc << 10) * (NetInc << 10);
296 mutt_pretty_size (posstr, sizeof (posstr), pos);
298 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
300 mutt_message ("%s %s", progress->msg, posstr);
304 void mutt_curses_message (const char *fmt, ...)
306 char TmpErrorbuf[STRING];
310 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
313 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
314 0, getmaxx(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
315 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
317 if (!option (OPTKEEPQUIET)) {
318 SETCOLOR(stdscr, MT_COLOR_MESSAGE);
319 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
321 SETCOLOR(stdscr, MT_COLOR_NORMAL);
325 unset_option (OPTMSGERR);
328 void mutt_show_error (void)
330 if (option (OPTKEEPQUIET))
333 SETCOLOR(stdscr, option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
334 CLEARLINE(stdscr, LINES - 1);
335 waddstr (stdscr, Errorbuf);
336 SETCOLOR(stdscr, MT_COLOR_NORMAL);
339 void curses_initialize(void)
342 if (start_color() == ERR || !has_colors() || COLORS < 8)
344 madtty_init_colors();
348 keypad(stdscr, true);
355 int mutt_any_key_to_continue (const char *s)
361 f = open ("/dev/tty", O_RDONLY);
363 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
364 t.c_lflag &= ~(ICANON | ECHO);
367 tcsetattr (f, TCSADRAIN, &t);
372 fputs (_("Press any key to continue..."), stdout);
376 tcsetattr (f, TCSADRAIN, &old);
378 fputs ("\r\n", stdout);
383 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
384 int *redraw, int buffy, int multiple, char ***files,
389 mvwaddstr(stdscr, LINES - 1, 0, (char *) prompt);
390 waddstr(stdscr, _(" ('?' for list): "));
392 waddstr (stdscr, buf);
398 CLEARLINE(stdscr, LINES - 1);
401 else if (ch.ch == '?') {
404 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
406 *redraw = REDRAW_FULL;
409 char *pc = p_new(char, m_strlen(prompt) + 3);
411 sprintf(pc, "%s: ", prompt);
412 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
414 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
418 MAYBE_REDRAW (*redraw);
425 void mutt_ungetch (int ch, int op)
432 if (UngetCount >= UngetBufLen)
433 p_realloc(&KeyEvent, UngetBufLen += 128);
435 KeyEvent[UngetCount++] = tmp;
438 void mutt_flushinp (void)
444 /* The argument can take 3 values:
445 * -1: restore the value of the last call
446 * 0: make the cursor invisible
447 * 1: make the cursor visible
449 void mutt_curs_set (int cursor)
451 static int SavedCursor = 1;
454 cursor = SavedCursor;
456 SavedCursor = cursor;
458 if (curs_set (cursor) == ERR) {
459 if (cursor == 1) /* cnorm */
460 curs_set (2); /* cvvis */
464 int mutt_multi_choice (const char *prompt, const char *letters)
470 mvwaddstr (stdscr, LINES - 1, 0, prompt);
475 if (ch.ch == -1 || CI_is_return (ch.ch)) {
480 p = strchr (letters, ch.ch);
482 choice = p - letters + 1;
485 else if (ch.ch <= '9' && ch.ch > '0') {
486 choice = ch.ch - '0';
487 if (choice <= m_strlen(letters))
493 CLEARLINE(stdscr, LINES - 1);
498 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
501 return m_strcpy(s, len, "0K");
503 if (n < 10189) /* 0.1K - 9.9K */
504 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
506 if (n < 1023949) /* 10K - 999K */
507 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
508 return snprintf(s, len, "%ldK", (n + 51) / 1024);
510 if (n < 10433332) /* 1.0M - 9.9M */
511 return snprintf(s, len, "%3.1fM", n / 1048576.0);
513 /* (10433332 + 52428) / 1048576 = 10 */
514 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
518 * This formats a string, a bit like
519 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
520 * except that the widths refer to the number of character cells
524 void mutt_format_string (char *dest, ssize_t destlen,
525 int min_width, int max_width,
526 int right_justify, char m_pad_char,
527 const char *s, ssize_t n, int arboreal)
533 char scratch[MB_LEN_MAX];
534 mbstate_t mbstate1, mbstate2;
536 p_clear(&mbstate1, 1);
537 p_clear(&mbstate2, 1);
540 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
541 if (k == -1 || k == -2) {
542 k = (k == -1) ? 1 : n;
543 wc = CharsetReplacement;
545 if (arboreal && wc < M_TREE_MAX)
553 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
557 m_strncpy(p, destlen, scratch, k2);
562 w = destlen < min_width ? destlen : min_width;
565 else if (right_justify) {
570 dest[w] = m_pad_char;
580 * This formats a string rather like
581 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
582 * snprintf (dest, destlen, fmt, s);
583 * except that the numbers in the conversion specification refer to
584 * the number of character cells when printed.
587 static void mutt_format_s_x (char *dest, ssize_t destlen,
588 const char *prefix, const char *s, int arboreal)
590 int right_justify = 1;
593 int max_width = INT_MAX;
596 ++prefix, right_justify = 0;
597 min_width = strtol (prefix, &p, 10);
600 max_width = strtol (prefix, &p, 10);
605 mutt_format_string (dest, destlen, min_width, max_width,
606 right_justify, ' ', s, m_strlen(s), arboreal);
609 void mutt_format_s (char *dest, ssize_t destlen,
610 const char *prefix, const char *s)
612 mutt_format_s_x (dest, destlen, prefix, s, 0);
615 void mutt_format_s_tree (char *dest, ssize_t destlen,
616 const char *prefix, const char *s)
618 mutt_format_s_x (dest, destlen, prefix, s, 1);
622 * mutt_paddstr (n, s) is almost equivalent to
623 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (main_w, bigbuf)
626 void mutt_paddstr(WINDOW *win, int n, const char *s)
631 ssize_t len = m_strlen(s);
634 p_clear(&mbstate, 1);
635 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
636 if (k == -1 || k == -2) {
637 k = (k == -1) ? 1 : len;
638 wc = CharsetReplacement;
646 waddnstr(win, (char *) s, k);