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-lib/lib-lib.h>
17 #ifdef HAVE_SYS_IOCTL_H
18 # include <sys/ioctl.h>
19 #elif defined(HAVE_IOCTL_H)
24 #include <lib-lua/lib-lua.h>
25 #include <lib-sys/unix.h>
26 #include <lib-sys/mutt_signal.h>
36 /* not possible to unget more than one char under some curses libs, and it
37 * is impossible to unget function keys in SLang, so roll our own input
40 ssize_t UngetCount = 0;
41 static ssize_t UngetBufLen = 0;
42 static event_t *KeyEvent;
44 void mutt_refresh (void)
46 /* don't refresh when we are waiting for a child. */
47 if (option (OPTKEEPQUIET))
50 /* don't refresh in the middle of macros unless necessary */
51 if (UngetCount && !option (OPTFORCEREFRESH))
58 void mutt_need_hard_redraw (void)
60 keypad (stdscr, TRUE);
61 clearok (stdscr, TRUE);
62 set_option (OPTNEEDREDRAW);
65 event_t mutt_getch (void)
68 event_t err = { -1, OP_NULL }, ret;
70 if (!option (OPTUNBUFFEREDINPUT) && UngetCount)
71 return (KeyEvent[--UngetCount]);
75 mutt_allow_interrupt (1);
77 mutt_allow_interrupt (0);
87 return (ch == ctrl ('G') ? err : ret);
90 int _mutt_get_field ( const char *field, char *buf, ssize_t buflen,
91 int complete, int multiple, char ***files, int *numfiles)
96 ENTER_STATE *es = mutt_new_enter_state ();
99 CLEARLINE (LINES - 1);
102 getyx (stdscr, y, x);
103 ret = _mutt_enter_string(buf, buflen, y, x, complete, multiple, files,
106 CLEARLINE (LINES - 1);
107 mutt_free_enter_state (&es);
112 int mutt_get_field_unbuffered (char *msg, char *buf, ssize_t buflen, int flags)
116 set_option (OPTUNBUFFEREDINPUT);
117 rc = mutt_get_field (msg, buf, buflen, flags);
118 unset_option (OPTUNBUFFEREDINPUT);
123 void mutt_clear_error (void)
126 if (!option (OPTNOCURSES))
127 CLEARLINE (LINES - 1);
130 void mutt_edit_file(const char *data)
132 char cmd[LONG_STRING];
135 m_quotefile_fmt(cmd, sizeof (cmd), mod_core.editor, data);
136 if (mutt_system (cmd) == -1)
137 mutt_error (_("Error running \"%s\"!"), cmd);
138 keypad (stdscr, TRUE);
139 clearok (stdscr, TRUE);
142 int mutt_yesorno (const char *msg, int def)
145 const char *yes = _("yes");
146 const char *no = _("no");
148 ssize_t answer_string_len;
158 reyes_ok = (expr = nl_langinfo (YESEXPR)) && expr[0] == '^' &&
159 !regcomp (&reyes, expr, REG_NOSUB | REG_EXTENDED);
160 reno_ok = (expr = nl_langinfo (NOEXPR)) && expr[0] == '^' &&
161 !regcomp (&reno, expr, REG_NOSUB | REG_EXTENDED);
163 CLEARLINE (LINES - 1);
166 * In order to prevent the default answer to the question to wrapped
167 * around the screen in the even the question is wider than the screen,
168 * ensure there is enough room for the answer and truncate the question
171 answer_string = p_new(char, COLS + 1);
172 snprintf (answer_string, COLS + 1, " ([%s]/%s): ", def == M_YES ? yes : no,
173 def == M_YES ? no : yes);
174 answer_string_len = m_strlen(answer_string);
175 printw ("%.*s%s", COLS - answer_string_len, msg, answer_string);
176 p_delete(&answer_string);
181 if (CI_is_return (ch.ch))
189 if (reyes_ok ? (regexec (&reyes, answer, 0, 0, 0) == 0) : tolower (ch.ch) == *yes)
195 reno_ok ? (regexec (&reno, answer, 0, 0, 0) == 0) :
196 (tolower (ch.ch) == *no)) {
210 addstr ((char *) (def == M_YES ? yes : no));
216 /* this function is called when the user presses the abort key */
217 void mutt_query_exit (void)
222 timeout (-1); /* restore blocking operation */
223 if (mutt_yesorno (_("Exit Madmutt?"), M_YES) == M_YES) {
232 void mutt_curses_error (const char *fmt, ...)
234 char TmpErrorbuf[STRING];
238 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
241 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
242 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
243 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
245 if (!option (OPTKEEPQUIET)) {
247 SETCOLOR (MT_COLOR_ERROR);
248 mvaddstr (LINES - 1, 0, Errorbuf);
250 SETCOLOR (MT_COLOR_NORMAL);
254 set_option (OPTMSGERR);
257 void mutt_progress_bar (progress_t* progress, long pos) {
262 mutt_message (progress->msg);
265 mutt_pretty_size (progress->sizestr, sizeof (progress->sizestr),
274 if (pos >= progress->pos + (NetInc << 10)) {
276 pos = pos / (NetInc << 10) * (NetInc << 10);
277 mutt_pretty_size (posstr, sizeof (posstr), pos);
279 mutt_message ("%s %s/%s", progress->msg, posstr, progress->sizestr);
281 mutt_message ("%s %s", progress->msg, posstr);
285 void mutt_curses_message (const char *fmt, ...)
287 char TmpErrorbuf[STRING];
291 vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
294 mutt_format_string (TmpErrorbuf, sizeof (TmpErrorbuf),
295 0, COLS - 2, 0, 0, Errorbuf, sizeof (Errorbuf), 0);
296 snprintf (Errorbuf, sizeof (Errorbuf), "%s", TmpErrorbuf); /* overkill */
298 if (!option (OPTKEEPQUIET)) {
299 SETCOLOR (MT_COLOR_MESSAGE);
300 mvaddstr (LINES - 1, 0, Errorbuf);
302 SETCOLOR (MT_COLOR_NORMAL);
306 unset_option (OPTMSGERR);
309 void mutt_show_error (void)
311 if (option (OPTKEEPQUIET))
314 SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
315 CLEARLINE (LINES - 1);
317 SETCOLOR (MT_COLOR_NORMAL);
320 void mutt_endwin (const char *msg)
322 if (!option (OPTNOCURSES)) {
323 CLEARLINE (LINES - 1);
336 void _mutt_perror (const char *s, const char* filename, int line)
338 char *p = strerror (errno);
339 mutt_error ("%s: %s (errno = %d) from %s:%i", s, p ? p : _("unknown error"), errno, filename, line);
342 int mutt_any_key_to_continue (const char *s)
348 f = open ("/dev/tty", O_RDONLY);
350 memcpy ((void *) &old, (void *) &t, sizeof (struct termios)); /* save original state */
351 t.c_lflag &= ~(ICANON | ECHO);
354 tcsetattr (f, TCSADRAIN, &t);
359 fputs (_("Press any key to continue..."), stdout);
363 tcsetattr (f, TCSADRAIN, &old);
365 fputs ("\r\n", stdout);
370 int _mutt_enter_fname (const char *prompt, char *buf, ssize_t blen,
371 int *redraw, int buffy, int multiple, char ***files,
376 mvaddstr (LINES - 1, 0, (char *) prompt);
377 addstr (_(" ('?' for list): "));
385 CLEARLINE (LINES - 1);
388 else if (ch.ch == '?') {
391 mutt_select_file (buf, blen, M_SEL_FOLDER | (multiple ? M_SEL_MULTI : 0),
393 *redraw = REDRAW_FULL;
396 char *pc = p_new(char, m_strlen(prompt) + 3);
398 sprintf(pc, "%s: ", prompt);
399 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
401 (pc, buf, blen, (buffy ? M_EFILE : M_FILE) | M_CLEAR, multiple, files,
405 MAYBE_REDRAW (*redraw);
412 void mutt_ungetch (int ch, int op)
419 if (UngetCount >= UngetBufLen)
420 p_realloc(&KeyEvent, UngetBufLen += 128);
422 KeyEvent[UngetCount++] = tmp;
425 void mutt_flushinp (void)
431 /* The argument can take 3 values:
432 * -1: restore the value of the last call
433 * 0: make the cursor invisible
434 * 1: make the cursor visible
436 void mutt_curs_set (int cursor)
438 static int SavedCursor = 1;
441 cursor = SavedCursor;
443 SavedCursor = cursor;
445 if (curs_set (cursor) == ERR) {
446 if (cursor == 1) /* cnorm */
447 curs_set (2); /* cvvis */
451 int mutt_multi_choice (const char *prompt, const char *letters)
457 mvaddstr (LINES - 1, 0, prompt);
462 if (ch.ch == -1 || CI_is_return (ch.ch)) {
467 p = strchr (letters, ch.ch);
469 choice = p - letters + 1;
472 else if (ch.ch <= '9' && ch.ch > '0') {
473 choice = ch.ch - '0';
474 if (choice <= m_strlen(letters))
480 CLEARLINE (LINES - 1);
486 * addwch would be provided by an up-to-date curses library
489 int mutt_addwch (wchar_t wc)
491 char buf[MB_LEN_MAX * 2];
495 p_clear(&mbstate, 1);
496 if ((n1 = wcrtomb(buf, wc, &mbstate)) == -1 ||
497 (n2 = wcrtomb(buf + n1, 0, &mbstate)) == -1)
503 ssize_t mutt_pretty_size(char *s, ssize_t len, ssize_t n)
506 return m_strcpy(s, len, "0K");
508 if (n < 10189) /* 0.1K - 9.9K */
509 return snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
511 if (n < 1023949) /* 10K - 999K */
512 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
513 return snprintf(s, len, "%ldK", (n + 51) / 1024);
515 if (n < 10433332) /* 1.0M - 9.9M */
516 return snprintf(s, len, "%3.1fM", n / 1048576.0);
518 /* (10433332 + 52428) / 1048576 = 10 */
519 return snprintf (s, len, "%ldM", (n + 52428) / 1048576);
523 * This formats a string, a bit like
524 * snprintf (dest, destlen, "%-*.*s", min_width, max_width, s),
525 * except that the widths refer to the number of character cells
529 void mutt_format_string (char *dest, ssize_t destlen,
530 int min_width, int max_width,
531 int right_justify, char m_pad_char,
532 const char *s, ssize_t n, int arboreal)
538 char scratch[MB_LEN_MAX];
539 mbstate_t mbstate1, mbstate2;
541 p_clear(&mbstate1, 1);
542 p_clear(&mbstate2, 1);
545 for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k) {
546 if (k == -1 || k == -2) {
547 k = (k == -1) ? 1 : n;
548 wc = CharsetReplacement;
550 if (arboreal && wc < M_TREE_MAX)
558 if (w > max_width || (k2 = wcrtomb (scratch, wc, &mbstate2)) > destlen)
562 m_strncpy(p, destlen, scratch, k2);
567 w = destlen < min_width ? destlen : min_width;
570 else if (right_justify) {
575 dest[w] = m_pad_char;
585 * This formats a string rather like
586 * snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
587 * snprintf (dest, destlen, fmt, s);
588 * except that the numbers in the conversion specification refer to
589 * the number of character cells when printed.
592 static void mutt_format_s_x (char *dest, ssize_t destlen,
593 const char *prefix, const char *s, int arboreal)
595 int right_justify = 1;
598 int max_width = INT_MAX;
601 ++prefix, right_justify = 0;
602 min_width = strtol (prefix, &p, 10);
605 max_width = strtol (prefix, &p, 10);
610 mutt_format_string (dest, destlen, min_width, max_width,
611 right_justify, ' ', s, m_strlen(s), arboreal);
614 void mutt_format_s (char *dest, ssize_t destlen,
615 const char *prefix, const char *s)
617 mutt_format_s_x (dest, destlen, prefix, s, 0);
620 void mutt_format_s_tree (char *dest, ssize_t destlen,
621 const char *prefix, const char *s)
623 mutt_format_s_x (dest, destlen, prefix, s, 1);
627 * mutt_paddstr (n, s) is almost equivalent to
628 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
631 void mutt_paddstr (int n, const char *s)
636 ssize_t len = m_strlen(s);
639 p_clear(&mbstate, 1);
640 for (; len && (k = mbrtowc (&wc, s, len, &mbstate)); s += k, len -= k) {
641 if (k == -1 || k == -2) {
642 k = (k == -1) ? 1 : len;
643 wc = CharsetReplacement;
651 addnstr ((char *) s, k);
659 /* this routine should be called after receiving SIGWINCH */
660 void mutt_resize_screen (void)
670 if ((fd = open ("/dev/tty", O_RDONLY)) != -1) {
671 if (ioctl (fd, TIOCGWINSZ, &w) != -1) {
678 if ((cp = getenv ("LINES")) != NULL) {
685 if ((cp = getenv ("COLUMNS")) != NULL)
690 resizeterm (rows, cols);