2 * Copyright (C) 1996-2000,2007 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 2000-1 Edmund Grimley Evans <edmundo@rano.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_menu.h"
26 #include "mutt_curses.h"
32 /* redraw flags for mutt_enter_string() */
35 M_REDRAW_INIT = 1, /* go to end of line and redraw */
36 M_REDRAW_LINE /* redraw entire line */
39 static int my_wcwidth (wchar_t wc)
42 if (IsWPrint (wc) && n > 0)
51 /* combining mark / non-spacing character */
52 #define COMB_CHAR(wc) (IsWPrint (wc) && !wcwidth (wc))
54 static int my_wcswidth (const wchar_t *s, size_t n)
58 w += my_wcwidth (*s++);
62 static int my_addwch (wchar_t wc)
65 if (IsWPrint (wc) && n > 0)
66 return mutt_addwch (wc);
68 return printw ("^%c", ((int)wc + 0x40) & 0x7f);
70 return printw ("\\u%04x", (int)wc);
71 return printw ("\\u%08x", (int)wc);
74 static size_t width_ceiling (const wchar_t *s, size_t n, int w1)
76 const wchar_t *s0 = s;
79 if ((w += my_wcwidth (*s)) > w1)
84 static void my_wcstombs (char *dest, size_t dlen, const wchar_t *src, size_t slen)
89 /* First convert directly into the destination buffer */
90 memset (&st, 0, sizeof (st));
91 for (; slen && dlen >= MB_LEN_MAX; dest += k, dlen -= k, src++, slen--)
92 if ((k = wcrtomb (dest, *src, &st)) == (size_t)(-1))
95 /* If this works, we can stop now */
96 if (dlen >= MB_LEN_MAX) {
97 wcrtomb (dest, 0, &st);
101 /* Otherwise convert any remaining data into a local buffer */
103 char buf[3 * MB_LEN_MAX];
106 for (; slen && p - buf < dlen; p += k, src++, slen--)
107 if ((k = wcrtomb (p, *src, &st)) == (size_t)(-1))
109 p += wcrtomb (p, 0, &st);
111 /* If it fits into the destination buffer, we can stop now */
112 if (p - buf <= dlen) {
113 memcpy (dest, buf, p - buf);
117 /* Otherwise we truncate the string in an ugly fashion */
118 memcpy (dest, buf, dlen);
119 dest[dlen - 1] = '\0'; /* assume original dlen > 0 */
123 static size_t my_mbstowcs (wchar_t **pwbuf, size_t *pwbuflen, size_t i, char *buf)
131 wbuf = *pwbuf, wbuflen = *pwbuflen;
135 memset (&st, 0, sizeof (st));
136 for (; (k = mbrtowc (&wc, buf, MB_LEN_MAX, &st)) &&
137 k != (size_t)(-1) && k != (size_t)(-2); buf += k)
142 safe_realloc (&wbuf, wbuflen * sizeof (*wbuf));
146 if (*buf && (k == (size_t) -1 || k == (size_t) -2))
151 safe_realloc (&wbuf, wbuflen * sizeof (*wbuf));
153 wbuf[i++] = replacement_char();
157 *pwbuf = wbuf, *pwbuflen = wbuflen;
162 * Replace part of the wchar_t buffer, from FROM to CURPOS, by BUF.
165 static void replace_part (ENTER_STATE *state, size_t from, char *buf)
167 /* Save the suffix */
168 size_t savelen = state->lastchar - state->curpos;
169 wchar_t *savebuf = safe_calloc (savelen, sizeof (wchar_t));
170 memcpy (savebuf, state->wbuf + state->curpos, savelen * sizeof (wchar_t));
172 /* Convert to wide characters */
173 state->curpos = my_mbstowcs (&state->wbuf, &state->wbuflen, from, buf);
175 /* Make space for suffix */
176 if (state->curpos + savelen > state->wbuflen)
178 state->wbuflen = state->curpos + savelen;
179 safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
183 memcpy (state->wbuf + state->curpos, savebuf, savelen * sizeof (wchar_t));
184 state->lastchar = state->curpos + savelen;
190 * Return 1 if the character is not typically part of a pathname
192 static inline int is_shell_char(wchar_t ch)
194 static wchar_t shell_chars[] = L"<>&()$?*;{}| "; /* ! not included because it can be part of a pathname in Mutt */
195 return wcschr(shell_chars, ch) != NULL;
200 * 1 need to redraw the screen and call me again
201 * 0 if input was given
205 int mutt_enter_string(char *buf, size_t buflen, int y, int x, int flags)
208 ENTER_STATE *es = mutt_new_enter_state ();
209 rv = _mutt_enter_string (buf, buflen, y, x, flags, 0, NULL, NULL, es);
210 mutt_free_enter_state (&es);
214 int _mutt_enter_string (char *buf, size_t buflen, int y, int x,
215 int flags, int multiple, char ***files, int *numfiles,
218 int width = COLS - x - 1;
220 int pass = (flags & M_PASS);
224 wchar_t *tempbuf = 0;
226 history_class_t hclass;
231 memset (&mbstate, 0, sizeof (mbstate));
235 /* Coming back after return 1 */
236 redraw = M_REDRAW_LINE;
241 /* Initialise wbuf from buf */
243 state->lastchar = my_mbstowcs (&state->wbuf, &state->wbuflen, 0, buf);
244 redraw = M_REDRAW_INIT;
249 else if (flags & M_EFILE)
251 else if (flags & M_CMD)
253 else if (flags & M_ALIAS)
255 else if (flags & M_COMMAND)
257 else if (flags & M_PATTERN)
266 if (redraw == M_REDRAW_INIT)
268 /* Go to end of line */
269 state->curpos = state->lastchar;
270 state->begin = width_ceiling (state->wbuf, state->lastchar, my_wcswidth (state->wbuf, state->lastchar) - width + 1);
272 if (state->curpos < state->begin ||
273 my_wcswidth (state->wbuf + state->begin, state->curpos - state->begin) >= width)
274 state->begin = width_ceiling (state->wbuf, state->lastchar, my_wcswidth (state->wbuf, state->curpos) - width / 2);
277 for (i = state->begin; i < state->lastchar; i++)
279 w += my_wcwidth (state->wbuf[i]);
282 my_addwch (state->wbuf[i]);
285 move (y, x + my_wcswidth (state->wbuf + state->begin, state->curpos - state->begin));
289 if ((ch = km_dokey (MENU_EDITOR)) == -1)
298 if (ch != OP_EDITOR_COMPLETE && ch != OP_EDITOR_COMPLETE_QUERY)
300 redraw = M_REDRAW_LINE;
303 case OP_EDITOR_HISTORY_UP:
304 state->curpos = state->lastchar;
305 replace_part (state, 0, mutt_history_prev (hclass));
306 redraw = M_REDRAW_INIT;
309 case OP_EDITOR_HISTORY_DOWN:
310 state->curpos = state->lastchar;
311 replace_part (state, 0, mutt_history_next (hclass));
312 redraw = M_REDRAW_INIT;
315 case OP_EDITOR_BACKSPACE:
316 if (state->curpos == 0)
321 while (i && COMB_CHAR (state->wbuf[i - 1]))
325 memmove (state->wbuf + i, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
326 state->lastchar -= state->curpos - i;
336 redraw= M_REDRAW_INIT;
339 case OP_EDITOR_KILL_LINE:
340 state->curpos = state->lastchar = 0;
343 case OP_EDITOR_KILL_EOL:
344 state->lastchar = state->curpos;
347 case OP_EDITOR_BACKWARD_CHAR:
348 if (state->curpos == 0)
352 while (state->curpos && COMB_CHAR (state->wbuf[state->curpos - 1]))
359 case OP_EDITOR_FORWARD_CHAR:
360 if (state->curpos == state->lastchar)
365 while (state->curpos < state->lastchar && COMB_CHAR (state->wbuf[state->curpos]))
370 case OP_EDITOR_BACKWARD_WORD:
371 if (state->curpos == 0)
375 while (state->curpos && iswspace (state->wbuf[state->curpos - 1]))
377 while (state->curpos && !iswspace (state->wbuf[state->curpos - 1]))
382 case OP_EDITOR_FORWARD_WORD:
383 if (state->curpos == state->lastchar)
387 while (state->curpos < state->lastchar && iswspace (state->wbuf[state->curpos]))
389 while (state->curpos < state->lastchar && !iswspace (state->wbuf[state->curpos]))
394 case OP_EDITOR_CAPITALIZE_WORD:
395 case OP_EDITOR_UPCASE_WORD:
396 case OP_EDITOR_DOWNCASE_WORD:
397 if (state->curpos == state->lastchar)
402 while (state->curpos && !iswspace (state->wbuf[state->curpos]))
404 while (state->curpos < state->lastchar && iswspace (state->wbuf[state->curpos]))
406 while (state->curpos < state->lastchar && !iswspace (state->wbuf[state->curpos]))
408 if (ch == OP_EDITOR_DOWNCASE_WORD)
409 state->wbuf[state->curpos] = towlower (state->wbuf[state->curpos]);
412 state->wbuf[state->curpos] = towupper (state->wbuf[state->curpos]);
413 if (ch == OP_EDITOR_CAPITALIZE_WORD)
414 ch = OP_EDITOR_DOWNCASE_WORD;
420 case OP_EDITOR_DELETE_CHAR:
421 if (state->curpos == state->lastchar)
426 while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
428 if (i < state->lastchar)
430 while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
432 memmove (state->wbuf + state->curpos, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t));
433 state->lastchar -= i - state->curpos;
437 case OP_EDITOR_KILL_WORD:
438 /* delete to begining of word */
439 if (state->curpos != 0)
442 while (i && iswspace (state->wbuf[i - 1]))
446 if (iswalnum (state->wbuf[i - 1]))
448 for (--i; i && iswalnum (state->wbuf[i - 1]); i--)
454 memmove (state->wbuf + i, state->wbuf + state->curpos,
455 (state->lastchar - state->curpos) * sizeof (wchar_t));
456 state->lastchar += i - state->curpos;
461 case OP_EDITOR_KILL_EOW:
462 /* delete to end of word */
463 for (i = state->curpos;
464 i < state->lastchar && iswspace (state->wbuf[i]); i++)
466 for (; i < state->lastchar && !iswspace (state->wbuf[i]); i++)
468 memmove (state->wbuf + state->curpos, state->wbuf + i,
469 (state->lastchar - i) * sizeof (wchar_t));
470 state->lastchar += state->curpos - i;
473 case OP_EDITOR_BUFFY_CYCLE:
476 first = 1; /* clear input if user types a real key later */
477 my_wcstombs (buf, buflen, state->wbuf, state->curpos);
478 mutt_buffy (buf, buflen);
479 state->curpos = state->lastchar = my_mbstowcs (&state->wbuf, &state->wbuflen, 0, buf);
482 else if (!(flags & M_FILE))
484 /* fall through to completion routine (M_FILE) */
486 case OP_EDITOR_COMPLETE:
487 case OP_EDITOR_COMPLETE_QUERY:
491 for (i = state->curpos; i && !is_shell_char(state->wbuf[i-1]); i--)
493 my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
494 if (tempbuf && templen == state->lastchar - i &&
495 !memcmp (tempbuf, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t)))
497 mutt_select_file (buf, buflen, (flags & M_EFILE) ? M_SEL_FOLDER : 0);
498 set_option (OPTNEEDREDRAW);
500 replace_part (state, i, buf);
504 if (!mutt_complete (buf, buflen))
506 templen = state->lastchar - i;
507 safe_realloc (&tempbuf, templen * sizeof (wchar_t));
512 replace_part (state, i, buf);
514 else if (flags & M_ALIAS && ch == OP_EDITOR_COMPLETE)
516 /* invoke the alias-menu to get more addresses */
517 for (i = state->curpos; i && state->wbuf[i-1] != ',' &&
518 state->wbuf[i-1] != ':'; i--)
520 for (; i < state->lastchar && state->wbuf[i] == ' '; i++)
522 my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
523 r = mutt_alias_complete (buf, buflen);
524 replace_part (state, i, buf);
532 else if (flags & M_ALIAS && ch == OP_EDITOR_COMPLETE_QUERY)
534 /* invoke the query-menu to get more addresses */
535 if ((i = state->curpos))
537 for (; i && state->wbuf[i - 1] != ','; i--)
539 for (; i < state->curpos && state->wbuf[i] == ' '; i++)
543 my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
544 mutt_query_complete (buf, buflen);
545 replace_part (state, i, buf);
550 else if (flags & M_COMMAND)
552 my_wcstombs (buf, buflen, state->wbuf, state->curpos);
554 if (i && buf[i - 1] == '=' &&
555 mutt_var_value_complete (buf, buflen, i))
557 else if (!mutt_command_complete (buf, buflen, i, state->tabs))
559 replace_part (state, 0, buf);
561 else if (flags & (M_FILE | M_EFILE))
563 my_wcstombs (buf, buflen, state->wbuf, state->curpos);
565 /* see if the path has changed from the last time */
566 if ((!tempbuf && !state->lastchar) || (tempbuf && templen == state->lastchar &&
567 !memcmp (tempbuf, state->wbuf, state->lastchar * sizeof (wchar_t))))
569 _mutt_select_file (buf, buflen,
570 ((flags & M_EFILE) ? M_SEL_FOLDER : 0) | (multiple ? M_SEL_MULTI : 0),
572 set_option (OPTNEEDREDRAW);
575 mutt_pretty_mailbox (buf, buflen);
577 mutt_history_add (hclass, buf, 1);
582 /* file selection cancelled */
587 if (!mutt_complete (buf, buflen))
589 templen = state->lastchar;
590 safe_realloc (&tempbuf, templen * sizeof (wchar_t));
591 memcpy (tempbuf, state->wbuf, templen * sizeof (wchar_t));
594 BEEP (); /* let the user know that nothing matched */
595 replace_part (state, 0, buf);
601 case OP_EDITOR_QUOTE_CHAR:
605 event = mutt_getch ();
613 case OP_EDITOR_TRANSPOSE_CHARS:
614 if (state->lastchar < 2)
620 if (state->curpos == 0)
622 else if (state->curpos < state->lastchar)
625 t = state->wbuf[state->curpos - 2];
626 state->wbuf[state->curpos - 2] = state->wbuf[state->curpos - 1];
627 state->wbuf[state->curpos - 1] = t;
641 /* use the raw keypress */
645 /* treat ENTER the same as RETURN */
650 /* quietly ignore all other function keys */
654 /* gather the octets into a wide character */
660 k = mbrtowc (&wc, &c, 1, &mbstate);
661 if (k == (size_t)(-2))
663 else if (k && k != 1)
665 memset (&mbstate, 0, sizeof (mbstate));
670 if (first && (flags & M_CLEAR))
673 if (IsWPrint (wc)) /* why? */
674 state->curpos = state->lastchar = 0;
677 if (wc == '\r' || wc == '\n')
679 /* Convert from wide characters */
680 my_wcstombs (buf, buflen, state->wbuf, state->lastchar);
682 mutt_history_add (hclass, buf, 1);
688 tfiles = safe_calloc (*numfiles, sizeof (char *));
689 mutt_expand_path (buf, buflen);
690 tfiles[0] = safe_strdup (buf);
696 else if (wc && (wc < ' ' || IsWPrint (wc))) /* why? */
698 if (state->lastchar >= state->wbuflen)
700 state->wbuflen = state->lastchar + 20;
701 safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
703 memmove (state->wbuf + state->curpos + 1, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
704 state->wbuf[state->curpos++] = wc;
721 void mutt_free_enter_state (ENTER_STATE **esp)
725 FREE (&(*esp)->wbuf);
726 FREE (esp); /* __FREE_CHECKED__ */
731 * very narrow screen might crash it
732 * sort out the input side