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 ( const char *field, char *buf, ssize_t buflen,
65 int complete, int multiple, char ***files, int *numfiles)
70 ENTER_STATE *es = mutt_new_enter_state ();
73 CLEARLINE(stdscr, LINES - 1);
74 waddstr (stdscr, field);
77 ret = mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
80 CLEARLINE(stdscr, LINES - 1);
81 mutt_free_enter_state (&es);
86 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
90 set_option (OPTUNBUFFEREDINPUT);
91 rc = mutt_get_field (msg, buf, buflen, flags);
92 unset_option (OPTUNBUFFEREDINPUT);
97 void mutt_clear_error (void)
100 if (!option (OPTNOCURSES))
101 CLEARLINE(stdscr, LINES - 1);
104 static struct timeval const slice = { 0, 1000 * 1000 / 100 };
105 static struct timeval timeval_add(struct timeval a, struct timeval b)
107 int usec = a.tv_usec + b.tv_usec;
108 a.tv_sec += b.tv_sec;
109 while (usec > 1000 * 1000) {
117 static int is_expired(struct timeval now, struct timeval expiry)
119 return now.tv_sec > expiry.tv_sec
120 || (now.tv_sec == expiry.tv_sec && now.tv_usec > expiry.tv_usec);
123 void mutt_edit_file(const char *data)
126 const char *args[] = { "/bin/sh", "-c", cmd, NULL };
127 int dirty = 0, ch, res, mh, mw, pty, pid;
131 m_quotefile_fmt(cmd, sizeof(cmd), mod_core.editor, data);
132 getmaxyx(main_w, mh, mw);
135 rt = madtty_create(mh - 2, mw);
136 pid = madtty_forkpty(rt, args[0], args, &pty);
139 mutt_error(_("unable to start editor"));
143 SETCOLOR(main_w, MT_COLOR_SIDEBAR);
144 mvwhline(main_w, 0, 0, ACS_HLINE, mw);
146 nodelay(stdscr, true);
147 gettimeofday(&next, NULL);
149 struct timeval tv = { 0, 1000 * 1000 / 100 };
156 if (select(pty + 1, &rfds, NULL, NULL, &tv) < 0)
159 if (FD_ISSET(pty, &rfds)) {
164 while ((ch = getch()) != ERR) {
165 madtty_keypress(rt, ch); /* pass the keypress for handling */
169 gettimeofday(&tv, NULL);
170 if (dirty && is_expired(tv, next)) {
171 madtty_draw(rt, main_w, 1, 0);
174 next = timeval_add(tv, slice);
177 while (waitpid(pid, &res, 0) < 0 && errno == EINTR);
178 nodelay(stdscr, false);
183 int mutt_yesorno (const char *msg, int def)
186 const char *yes = _("yes");
187 const char *no = _("no");
189 ssize_t answer_string_len;
199 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
200 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
201 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
202 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
204 CLEARLINE(stdscr, LINES - 1);
207 * In order to prevent the default answer to the question to wrapped
208 * around the screen in the even the question is wider than the screen,
209 * ensure there is enough room for the answer and truncate the question
212 answer_string = p_new(char, getmaxx(stdscr) + 1);
213 snprintf (answer_string, getmaxx(stdscr) + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
214 def == M_YES ? no : yes);
215 answer_string_len = m_strlen(answer_string);
216 wprintw (stdscr, "%.*s%s", getmaxx(stdscr) - answer_string_len, msg, answer_string);
217 p_delete(&answer_string);
222 if (CI_is_return (ch.ch))
230 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
236 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
237 (tolower (ch.ch) == *no)) {
251 waddstr (stdscr, (char *) (def == M_YES ? yes : no));
254 CLEARLINE(stdscr, LINES - 1);
258 /* this function is called when the user presses the abort key */
259 void mutt_query_exit (void)
264 wtimeout (stdscr, -1); /* restore blocking operation */
265 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
274 void mutt_curses_error (const char *fmt, ...)
276 char TmpErrorbuf[STRING];
280 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
283 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
284 0, getmaxy(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
285 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
287 if (!option (OPTKEEPQUIET)) {
289 SETCOLOR(stdscr, MT_COLOR_ERROR);
290 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
292 SETCOLOR(stdscr, MT_COLOR_NORMAL);
296 set_option (OPTMSGERR);
299 void mutt_progress_bar (progress_t* progress, long pos) {
304 mutt_message (progress->msg);
307 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
316 if (pos >= progress->pos + (NetInc << 10)) {
318 pos = pos / (NetInc << 10) * (NetInc << 10);
319 mutt_pretty_size (posstr, sizeof (posstr), pos);
321 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
323 mutt_message ("%s %s", progress->msg, posstr);
327 void mutt_curses_message (const char *fmt, ...)
329 char TmpErrorbuf[STRING];
333 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
336 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
337 0, getmaxx(stdscr) - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
338 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
340 if (!option (OPTKEEPQUIET)) {
341 SETCOLOR(stdscr, MT_COLOR_MESSAGE);
342 mvwaddstr (stdscr, LINES - 1, 0, Errorbuf);
344 SETCOLOR(stdscr, MT_COLOR_NORMAL);
348 unset_option (OPTMSGERR);
351 void mutt_show_error (void)
353 if (option (OPTKEEPQUIET))
356 SETCOLOR(stdscr, option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
357 CLEARLINE(stdscr, LINES - 1);
358 waddstr (stdscr, Errorbuf);
359 SETCOLOR(stdscr, MT_COLOR_NORMAL);
362 void curses_initialize(void)
365 if (start_color() == ERR || !has_colors() || COLORS < 8)
367 madtty_init_colors();
371 keypad(stdscr, true);
378 int mutt_any_key_to_continue (const char *s)
384 f = open ("/dev/tty", O_RDONLY);
386 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
387 t.c_lflag &= ~(ICANON | ECHO);
390 tcsetattr (f, TCSADRAIN, &t);
395 fputs (_("Press any key to continue..."), stdout);
399 tcsetattr (f, TCSADRAIN, &old);
401 fputs ("\r\n", stdout);
406 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
407 int *redraw, int buffy, int multiple, char ***files,
412 mvwaddstr(stdscr, LINES - 1, 0, (char *) prompt);
413 waddstr(stdscr, _(" ('?' for list): "));
415 waddstr (stdscr, buf);
421 CLEARLINE(stdscr, LINES - 1);
424 else if (ch.ch == '?') {
427 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
429 *redraw = REDRAW_FULL;
432 char *pc = p_new(char, m_strlen(prompt) + 3);
434 sprintf(pc, "%s: ", prompt);
435 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
437 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
441 MAYBE_REDRAW (*redraw);
448 void mutt_ungetch (int ch, int op)
455 if (UngetCount >= UngetBufLen)
456 p_realloc(&KeyEvent, UngetBufLen += 128);
458 KeyEvent[UngetCount++] = tmp;
461 void mutt_flushinp (void)
467 /* The argument can take 3 values:
468 * -1: restore the value of the last call
469 * 0: make the cursor invisible
470 * 1: make the cursor visible
472 void mutt_curs_set (int cursor)
474 static int SavedCursor = 1;
477 cursor = SavedCursor;
479 SavedCursor = cursor;
481 if (curs_set (cursor) == ERR) {
482 if (cursor == 1) /* cnorm */
483 curs_set (2); /* cvvis */
487 int mutt_multi_choice (const char *prompt, const char *letters)
493 mvwaddstr (stdscr, LINES - 1, 0, prompt);
498 if (ch.ch == -1 || CI_is_return (ch.ch)) {
503 p = strchr (letters, ch.ch);
505 choice = p - letters + 1;
508 else if (ch.ch <= '9' && ch.ch > '0') {
509 choice = ch.ch - '0';
510 if (choice <= m_strlen(letters))
516 CLEARLINE(stdscr, LINES - 1);
521 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
524 return m_strcpy(s, len, "0K");
526 if (n < 10189) /* 0.1K - 9.9K */
527 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
529 if (n < 1023949) /* 10K - 999K */
530 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
531 return snprintf(s, len, "%ldK", (n + 51) / 1024);
533 if (n < 10433332) /* 1.0M - 9.9M */
534 return snprintf(s, len, "%3.1fM", n / 1048576.0);
536 /* (10433332 + 52428) / 1048576 = 10 */
537 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
541 * This formats a string, a bit like
542 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
543 * except that the widths refer to the number of character cells
547 void mutt_format_string (char *dest, ssize_t destlen,
548 int min_width, int max_width,
549 int right_justify, char m_pad_char,
550 const char *s, ssize_t n, int arboreal)
556 char scratch[MB_LEN_MAX];
557 mbstate_t mbstate1, mbstate2;
559 p_clear(&mbstate1, 1);
560 p_clear(&mbstate2, 1);
563 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
564 if (k == -1 || k == -2) {
565 k = (k == -1) ? 1 : n;
566 wc = CharsetReplacement;
568 if (arboreal && wc < M_TREE_MAX)
576 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
580 m_strncpy(p, destlen, scratch, k2);
585 w = destlen < min_width ? destlen : min_width;
588 else if (right_justify) {
593 dest[w] = m_pad_char;
603 * This formats a string rather like
604 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
605 * snprintf (dest, destlen, fmt, s);
606 * except that the numbers in the conversion specification refer to
607 * the number of character cells when printed.
610 static void mutt_format_s_x (char *dest, ssize_t destlen,
611 const char *prefix, const char *s, int arboreal)
613 int right_justify = 1;
616 int max_width = INT_MAX;
619 ++prefix, right_justify = 0;
620 min_width = strtol (prefix, &p, 10);
623 max_width = strtol (prefix, &p, 10);
628 mutt_format_string (dest, destlen, min_width, max_width,
629 right_justify, ' ', s, m_strlen(s), arboreal);
632 void mutt_format_s (char *dest, ssize_t destlen,
633 const char *prefix, const char *s)
635 mutt_format_s_x (dest, destlen, prefix, s, 0);
638 void mutt_format_s_tree (char *dest, ssize_t destlen,
639 const char *prefix, const char *s)
641 mutt_format_s_x (dest, destlen, prefix, s, 1);
645 * mutt_paddstr (n, s) is almost equivalent to
646 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), waddstr (main_w, bigbuf)
649 void mutt_paddstr(WINDOW *win, int n, const char *s)
654 ssize_t len = m_strlen(s);
657 p_clear(&mbstate, 1);
658 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
659 if (k == -1 || k == -2) {
660 k = (k == -1) ? 1 : len;
661 wc = CharsetReplacement;
669 waddnstr(win, (char *) s, k);