enter.c 20.6 KB
Newer Older
1
/*
2 3
 * Copyright (C) 1996-2000,2007,2011,2013 Michael R. Elkins <me@mutt.org>
 * Copyright (C) 2000-2001 Edmund Grimley Evans <edmundo@rano.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */ 

#if HAVE_CONFIG_H
# include "config.h"
#endif
23 24

#include "mutt.h"
25
#include "mutt_menu.h"
26
#include "mutt_curses.h"
27 28 29 30
#include "keymap.h"
#include "history.h"

#include <string.h>
31 32 33 34

/* redraw flags for mutt_enter_string() */
enum
{
35 36
  MUTT_REDRAW_INIT = 1,	/* go to end of line and redraw */
  MUTT_REDRAW_LINE		/* redraw entire line */
37 38
};

39
static int my_wcwidth (wchar_t wc)
40
{
41 42
  int n = wcwidth (wc);
  if (IsWPrint (wc) && n > 0)
43 44 45 46 47 48 49 50 51
    return n;
  if (!(wc & ~0x7f))
    return 2;
  if (!(wc & ~0xffff))
    return 6;
  return 10;
}

/* combining mark / non-spacing character */
52
#define COMB_CHAR(wc) (IsWPrint (wc) && !wcwidth (wc))
53

54
static int my_wcswidth (const wchar_t *s, size_t n)
55 56 57
{
  int w = 0;
  while (n--)
58
    w += my_wcwidth (*s++);
59 60 61
  return w;
}

62
static int my_addwch (wchar_t wc)
63
{
64 65 66
  int n = wcwidth (wc);
  if (IsWPrint (wc) && n > 0)
    return mutt_addwch (wc);
67
  if (!(wc & ~0x7f))
68
    return printw ("^%c", ((int)wc + 0x40) & 0x7f);
69
  if (!(wc & ~0xffff))
70 71
    return printw ("\\u%04x", (int)wc);
  return printw ("\\u%08x", (int)wc);
72 73
}

74
static size_t width_ceiling (const wchar_t *s, size_t n, int w1)
75 76 77 78
{
  const wchar_t *s0 = s;
  int w = 0;
  for (; n; s++, n--)
79
    if ((w += my_wcwidth (*s)) > w1)
80
      break;
81
  return s - s0;  
82 83
}

84
static void my_wcstombs (char *dest, size_t dlen, const wchar_t *src, size_t slen)
85 86 87 88 89
{
  mbstate_t st;
  size_t k;

  /* 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))
93 94 95
      break;

  /* If this works, we can stop now */
96 97
  if (dlen >= MB_LEN_MAX) {
    wcrtomb (dest, 0, &st);
98 99 100 101 102 103 104 105 106
    return;
  }

  /* Otherwise convert any remaining data into a local buffer */
  {
    char buf[3 * MB_LEN_MAX];
    char *p = buf;

    for (; slen && p - buf < dlen; p += k, src++, slen--)
107 108 109
      if ((k = wcrtomb (p, *src, &st)) == (size_t)(-1))
	break;
    p += wcrtomb (p, 0, &st);
110 111

    /* If it fits into the destination buffer, we can stop now */
112 113
    if (p - buf <= dlen) {
      memcpy (dest, buf, p - buf);
114 115 116 117
      return;
    }

    /* Otherwise we truncate the string in an ugly fashion */
118
    memcpy (dest, buf, dlen);
119 120 121 122
    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)
124 125 126 127
{
  wchar_t wc;
  mbstate_t st;
  size_t k;
128
  wchar_t *wbuf;
129 130 131
  size_t wbuflen;

  wbuf = *pwbuf, wbuflen = *pwbuflen;
132
  
133 134
  while (*buf)
  {
135 136 137
    memset (&st, 0, sizeof (st));
    for (; (k = mbrtowc (&wc, buf, MB_LEN_MAX, &st)) &&
	 k != (size_t)(-1) && k != (size_t)(-2); buf += k)
138 139 140
    {
      if (i >= wbuflen)
      {
141 142
	wbuflen = i + 20;
	safe_realloc (&wbuf, wbuflen * sizeof (*wbuf));
143 144 145 146 147
      }
      wbuf[i++] = wc;
    }
    if (*buf && (k == (size_t) -1 || k == (size_t) -2))
    {
148
      if (i >= wbuflen) 
149
      {
150 151
	wbuflen = i + 20;
	safe_realloc (&wbuf, wbuflen * sizeof (*wbuf));
152 153 154 155 156 157 158 159 160 161 162 163
      }
      wbuf[i++] = replacement_char();
      buf++;
    }
  }
  *pwbuf = wbuf, *pwbuflen = wbuflen;
  return i;
}

/*
 * Replace part of the wchar_t buffer, from FROM to CURPOS, by BUF.
 */
164 165

static void replace_part (ENTER_STATE *state, size_t from, char *buf)
166 167 168
{
  /* Save the suffix */
  size_t savelen = state->lastchar - state->curpos;
169 170 171 172
  wchar_t *savebuf = NULL;

  if (savelen)
  {
173 174
    savebuf = safe_calloc (savelen, sizeof (wchar_t));
    memcpy (savebuf, state->wbuf + state->curpos, savelen * sizeof (wchar_t));
175
  }
176 177

  /* Convert to wide characters */
178
  state->curpos = my_mbstowcs (&state->wbuf, &state->wbuflen, from, buf);
179

180
  if (savelen)
181
  {
182 183 184 185
    /* Make space for suffix */
    if (state->curpos + savelen > state->wbuflen)
    {
      state->wbuflen = state->curpos + savelen;
186
      safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
187 188 189
    }

    /* Restore suffix */
190 191
    memcpy (state->wbuf + state->curpos, savebuf, savelen * sizeof (wchar_t));
    FREE (&savebuf);
192 193 194 195 196 197 198 199 200 201
  }

  state->lastchar = state->curpos + savelen;
}

/*
 * Return 1 if the character is not typically part of a pathname
 */
static inline int is_shell_char(wchar_t ch)
{
202
  static const wchar_t shell_chars[] = L"<>&()$?*;{}| "; /* ! not included because it can be part of a pathname in Mutt */
203 204 205
  return wcschr(shell_chars, ch) != NULL;
}

206 207 208 209 210
/* This function is for very basic input, currently used only by the
 * built-in editor.  It does not handle screen redrawing on resizes
 * well, because there is no active menu for the built-in editor.
 * Most callers should prefer mutt_get_field() instead.
 *
211
 * Returns:
212 213
 *	0 if input was given
 * 	-1 if abort.
214
 */
215
int  mutt_enter_string(char *buf, size_t buflen, int col, int flags)
216 217
{
  int rv;
218
  ENTER_STATE *es = mutt_new_enter_state ();
219 220
  do
  {
221
#if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
222 223 224
    if (SigWinch)
    {
      SigWinch = 0;
225
      mutt_resize_screen ();
226 227 228
      clearok(stdscr, TRUE);
    }
#endif
229 230 231 232
    rv = _mutt_enter_string (buf, buflen, col, flags, 0, NULL, NULL, es);
  }
  while (rv == 1);
  mutt_free_enter_state (&es);
233 234 235
  return rv;
}

236 237 238
/*
 * Returns:
 *      1 need to redraw the screen and call me again
239 240
 *	0 if input was given
 * 	-1 if abort.
241
 */
242 243 244
int _mutt_enter_string (char *buf, size_t buflen, int col,
			int flags, int multiple, char ***files, int *numfiles,
			ENTER_STATE *state)
245
{
246
  int width = MuttMessageWindow->cols - col - 1;
247
  int redraw;
248
  int pass = (flags & MUTT_PASS);
249 250 251
  int first = 1;
  int ch, w, r;
  size_t i;
252
  wchar_t *tempbuf = 0;
253 254 255 256 257 258
  size_t templen = 0;
  history_class_t hclass;
  wchar_t wc;
  mbstate_t mbstate;

  int rv = 0;
259 260
  memset (&mbstate, 0, sizeof (mbstate));
  
261 262 263
  if (state->wbuf)
  {
    /* Coming back after return 1 */
264
    redraw = MUTT_REDRAW_LINE;
265 266 267 268 269 270
    first = 0;
  }
  else
  {
    /* Initialise wbuf from buf */
    state->wbuflen = 0;
271
    state->lastchar = my_mbstowcs (&state->wbuf, &state->wbuflen, 0, buf);
272
    redraw = MUTT_REDRAW_INIT;
273 274
  }

275
  if (flags & MUTT_FILE)
276
    hclass = HC_FILE;
277
  else if (flags & MUTT_EFILE)
278
    hclass = HC_MBOX;
279
  else if (flags & MUTT_CMD)
280
    hclass = HC_CMD;
281
  else if (flags & MUTT_ALIAS)
282
    hclass = HC_ALIAS;
283
  else if (flags & MUTT_COMMAND)
284
    hclass = HC_COMMAND;
285
  else if (flags & MUTT_PATTERN)
286
    hclass = HC_PATTERN;
287
  else 
288
    hclass = HC_OTHER;
289
    
290 291 292 293
  for (;;)
  {
    if (redraw && !pass)
    {
294
      if (redraw == MUTT_REDRAW_INIT)
295
      {
296 297 298 299
	/* Go to end of line */
	state->curpos = state->lastchar;
	state->begin = width_ceiling (state->wbuf, state->lastchar, my_wcswidth (state->wbuf, state->lastchar) - width + 1);
      } 
300
      if (state->curpos < state->begin ||
301 302 303
	  my_wcswidth (state->wbuf + state->begin, state->curpos - state->begin) >= width)
	state->begin = width_ceiling (state->wbuf, state->lastchar, my_wcswidth (state->wbuf, state->curpos) - width / 2);
      mutt_window_move (MuttMessageWindow, 0, col);
304 305 306
      w = 0;
      for (i = state->begin; i < state->lastchar; i++)
      {
307 308 309 310
	w += my_wcwidth (state->wbuf[i]);
	if (w > width)
	  break;
	my_addwch (state->wbuf[i]);
311
      }
312 313 314
      mutt_window_clrtoeol (MuttMessageWindow);
      mutt_window_move (MuttMessageWindow, 0,
          col + my_wcswidth (state->wbuf + state->begin, state->curpos - state->begin));
315
    }
316
    mutt_refresh ();
317

318
    if ((ch = km_dokey (MENU_EDITOR)) < 0)
319
    {
320
      rv = (SigWinch && ch == -2) ? 1 : -1;
321 322 323 324 325 326 327
      goto bye;
    }

    if (ch != OP_NULL)
    {
      first = 0;
      if (ch != OP_EDITOR_COMPLETE && ch != OP_EDITOR_COMPLETE_QUERY)
328
	state->tabs = 0;
329
      redraw = MUTT_REDRAW_LINE;
330 331
      switch (ch)
      {
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	case OP_EDITOR_HISTORY_UP:
	  state->curpos = state->lastchar;
	  if (mutt_history_at_scratch (hclass))
	  {
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
	    mutt_history_save_scratch (hclass, buf);
	  }
	  replace_part (state, 0, mutt_history_prev (hclass));
	  redraw = MUTT_REDRAW_INIT;
	  break;

	case OP_EDITOR_HISTORY_DOWN:
	  state->curpos = state->lastchar;
	  if (mutt_history_at_scratch (hclass))
	  {
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
	    mutt_history_save_scratch (hclass, buf);
	  }
	  replace_part (state, 0, mutt_history_next (hclass));
	  redraw = MUTT_REDRAW_INIT;
	  break;

354 355 356 357 358 359 360 361 362
        case OP_EDITOR_HISTORY_SEARCH:
          state->curpos = state->lastchar;
          my_wcstombs (buf, buflen, state->wbuf, state->curpos);
          mutt_history_complete (buf, buflen, hclass);
          replace_part (state, 0, buf);
          rv = 1;
          goto bye;
          break;

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	case OP_EDITOR_BACKSPACE:
	  if (state->curpos == 0)
	    BEEP ();
	  else
	  {
	    i = state->curpos;
	    while (i && COMB_CHAR (state->wbuf[i - 1]))
	      --i;
	    if (i)
	      --i;
	    memmove (state->wbuf + i, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
	    state->lastchar -= state->curpos - i;
	    state->curpos = i;
	  }
	  break;

	case OP_EDITOR_BOL:
	  state->curpos = 0;
	  break;

	case OP_EDITOR_EOL:
	  redraw= MUTT_REDRAW_INIT;
	  break;

	case OP_EDITOR_KILL_LINE:
	  state->curpos = state->lastchar = 0;
	  break;

	case OP_EDITOR_KILL_EOL:
	  state->lastchar = state->curpos;
	  break;

	case OP_EDITOR_BACKWARD_CHAR:
	  if (state->curpos == 0)
	    BEEP ();
	  else
	  {
	    while (state->curpos && COMB_CHAR (state->wbuf[state->curpos - 1]))
	      state->curpos--;
	    if (state->curpos)
	      state->curpos--;
	  }
	  break;

	case OP_EDITOR_FORWARD_CHAR:
	  if (state->curpos == state->lastchar)
	    BEEP ();
	  else
	  {
	    ++state->curpos;
	    while (state->curpos < state->lastchar && COMB_CHAR (state->wbuf[state->curpos]))
	      ++state->curpos;
	  }
	  break;

	case OP_EDITOR_BACKWARD_WORD:
	  if (state->curpos == 0)
	    BEEP ();
	  else
	  {
	    while (state->curpos && iswspace (state->wbuf[state->curpos - 1]))
	      --state->curpos;
	    while (state->curpos && !iswspace (state->wbuf[state->curpos - 1]))
	      --state->curpos;
	  }
	  break;

	case OP_EDITOR_FORWARD_WORD:
	  if (state->curpos == state->lastchar)
	    BEEP ();
	  else
	  {
	    while (state->curpos < state->lastchar && iswspace (state->wbuf[state->curpos]))
	      ++state->curpos;
	    while (state->curpos < state->lastchar && !iswspace (state->wbuf[state->curpos]))
	      ++state->curpos;
	  }
	  break;

	case OP_EDITOR_CAPITALIZE_WORD:
	case OP_EDITOR_UPCASE_WORD:
	case OP_EDITOR_DOWNCASE_WORD:
	  if (state->curpos == state->lastchar)
	  {
	    BEEP ();
	    break;
	  }
	  while (state->curpos && !iswspace (state->wbuf[state->curpos]))
	    --state->curpos;
	  while (state->curpos < state->lastchar && iswspace (state->wbuf[state->curpos]))
	    ++state->curpos;
	  while (state->curpos < state->lastchar && !iswspace (state->wbuf[state->curpos]))
	  {
	    if (ch == OP_EDITOR_DOWNCASE_WORD)
	      state->wbuf[state->curpos] = towlower (state->wbuf[state->curpos]);
	    else
	    {
	      state->wbuf[state->curpos] = towupper (state->wbuf[state->curpos]);
	      if (ch == OP_EDITOR_CAPITALIZE_WORD)
		ch = OP_EDITOR_DOWNCASE_WORD;
	    }
	    state->curpos++;
	  }
	  break;

	case OP_EDITOR_DELETE_CHAR:
	  if (state->curpos == state->lastchar)
	    BEEP ();
	  else
	  {
	    i = state->curpos;
	    while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
	      ++i;
	    if (i < state->lastchar)
	      ++i;
	    while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
	      ++i;
	    memmove (state->wbuf + state->curpos, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t));
	    state->lastchar -= i - state->curpos;
	  }
	  break;

	case OP_EDITOR_KILL_WORD:
	  /* delete to beginning of word */
	  if (state->curpos != 0)
	  {
	    i = state->curpos;
	    while (i && iswspace (state->wbuf[i - 1]))
	      --i;
	    if (i)
	    {
	      if (iswalnum (state->wbuf[i - 1]))
	      {
		for (--i; i && iswalnum (state->wbuf[i - 1]); i--)
		  ;
	      }
	      else
		--i;
	    }
	    memmove (state->wbuf + i, state->wbuf + state->curpos,
		     (state->lastchar - state->curpos) * sizeof (wchar_t));
	    state->lastchar += i - state->curpos;
	    state->curpos = i;
	  }
	  break;

	case OP_EDITOR_KILL_EOW:
	  /* delete to end of word */

	  /* first skip over whitespace */
	  for (i = state->curpos;
	       i < state->lastchar && iswspace (state->wbuf[i]); i++)
	    ;

	  /* if there are any characters left.. */
	  if (i < state->lastchar)
	  {
	    /* if the current character is alphanumeric.. */
	    if (iswalnum (state->wbuf[i]))
	    {
	      /* skip over the rest of the word consistent of only alphanumerics */
	      for (; i < state->lastchar && iswalnum (state->wbuf[i]); i++)
		;
	    }
	    else
	    {
	      /* skip over one non-alphanumeric character */
	      ++i;
	    }
	  }

	  memmove (state->wbuf + state->curpos, state->wbuf + i,
		   (state->lastchar - i) * sizeof (wchar_t));
	  state->lastchar += state->curpos - i;
	  break;

	case OP_EDITOR_BUFFY_CYCLE:
	  if (flags & MUTT_EFILE)
	  {
	    first = 1; /* clear input if user types a real key later */
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
	    mutt_buffy (buf, buflen);
	    state->curpos = state->lastchar = my_mbstowcs (&state->wbuf, &state->wbuflen, 0, buf);
	    break;
	  }
	  else if (!(flags & MUTT_FILE))
	    goto self_insert;
	  /* fall through to completion routine (MUTT_FILE) */

	case OP_EDITOR_COMPLETE:
	case OP_EDITOR_COMPLETE_QUERY:
	  state->tabs++;
	  if (flags & MUTT_CMD)
	  {
	    for (i = state->curpos; i && !is_shell_char(state->wbuf[i-1]); i--)
	      ;
	    my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
	    if (tempbuf && templen == state->lastchar - i &&
		!memcmp (tempbuf, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t)))
	    {
	      mutt_select_file (buf, buflen, (flags & MUTT_EFILE) ? MUTT_SEL_FOLDER : 0);
	      if (*buf)
		replace_part (state, i, buf);
	      rv = 1; 
	      goto bye;
	    }
	    if (!mutt_complete (buf, buflen))
	    {
	      templen = state->lastchar - i;
	      safe_realloc (&tempbuf, templen * sizeof (wchar_t));
	    }
	    else
	      BEEP ();

	    replace_part (state, i, buf);
	  }
	  else if (flags & MUTT_ALIAS && ch == OP_EDITOR_COMPLETE)
	  {
	    /* invoke the alias-menu to get more addresses */
	    for (i = state->curpos; i && state->wbuf[i-1] != ',' && 
		 state->wbuf[i-1] != ':'; i--)
	      ;
	    for (; i < state->lastchar && state->wbuf[i] == ' '; i++)
	      ;
	    my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
	    r = mutt_alias_complete (buf, buflen);
	    replace_part (state, i, buf);
	    if (!r)
	    {
	      rv = 1;
	      goto bye;
	    }
	    break;
	  }
	  else if (flags & MUTT_LABEL && ch == OP_EDITOR_COMPLETE)
	  {
	    for (i = state->curpos; i && state->wbuf[i-1] != ',' && 
		 state->wbuf[i-1] != ':'; i--)
	      ;
	    for (; i < state->lastchar && state->wbuf[i] == ' '; i++)
	      ;
	    my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
	    r = mutt_label_complete (buf, buflen, state->tabs);
	    replace_part (state, i, buf);
	    if (!r)
	    {
	      rv = 1;
	      goto bye;
	    }
	    break;
	  }
614 615
          else if (flags & MUTT_PATTERN && ch == OP_EDITOR_COMPLETE)
          {
616
            for (i = state->curpos; i && state->wbuf[i-1] != '~'; i--)
617
              ;
618
            if (i && i < state->curpos && state->wbuf[i-1] == '~' && state->wbuf[i] == 'y')
619 620
            {
              i++;
621 622 623
              my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
              r = mutt_label_complete (buf, buflen, state->tabs);
              replace_part (state, i, buf);
624 625 626 627 628 629 630 631 632 633
              if (!r)
              {
                rv = 1;
                goto bye;
              }
            }
            else
              goto self_insert;
            break;
          }
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	  else if (flags & MUTT_ALIAS && ch == OP_EDITOR_COMPLETE_QUERY)
	  {
	    /* invoke the query-menu to get more addresses */
	    if ((i = state->curpos))
	    {
	      for (; i && state->wbuf[i - 1] != ','; i--)
		;
	      for (; i < state->curpos && state->wbuf[i] == ' '; i++)
		;
	    }

	    my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
	    mutt_query_complete (buf, buflen);
	    replace_part (state, i, buf);

	    rv = 1; 
	    goto bye;
	  }
	  else if (flags & MUTT_COMMAND)
	  {
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
	    i = strlen (buf);
	    if (i && buf[i - 1] == '=' &&
		mutt_var_value_complete (buf, buflen, i))
	      state->tabs = 0;
	    else if (!mutt_command_complete (buf, buflen, i, state->tabs))
	      BEEP ();
	    replace_part (state, 0, buf);
	  }
	  else if (flags & (MUTT_FILE | MUTT_EFILE))
	  {
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);

	    /* see if the path has changed from the last time */
	    if ((!tempbuf && !state->lastchar) || (tempbuf && templen == state->lastchar &&
		!memcmp (tempbuf, state->wbuf, state->lastchar * sizeof (wchar_t))))
	    {
	      _mutt_select_file (buf, buflen, 
				 ((flags & MUTT_EFILE) ? MUTT_SEL_FOLDER : 0) | (multiple ? MUTT_SEL_MULTI : 0), 
				 files, numfiles);
	      if (*buf)
	      {
		mutt_pretty_mailbox (buf, buflen);
		if (!pass)
		  mutt_history_add (hclass, buf, 1);
		rv = 0;
		goto bye;
	      }

	      /* file selection cancelled */
	      rv = 1;
	      goto bye;
	    }

	    if (!mutt_complete (buf, buflen))
	    {
	      templen = state->lastchar;
	      safe_realloc (&tempbuf, templen * sizeof (wchar_t));
	      memcpy (tempbuf, state->wbuf, templen * sizeof (wchar_t));
	    }
	    else
	      BEEP (); /* let the user know that nothing matched */
	    replace_part (state, 0, buf);
	  }
	  else
	    goto self_insert;
	  break;

	case OP_EDITOR_QUOTE_CHAR:
	  {
	    event_t event;
	    /*ADDCH (LastKey);*/
	    event = mutt_getch ();
	    if (event.ch >= 0)
	    {
	      LastKey = event.ch;
	      goto self_insert;
	    }
	  }

	case OP_EDITOR_TRANSPOSE_CHARS:
	  if (state->lastchar < 2)
	    BEEP ();
	  else
	{
	    wchar_t t;

	    if (state->curpos == 0)
	      state->curpos = 2;
	    else if (state->curpos < state->lastchar)
	      ++state->curpos;

	    t = state->wbuf[state->curpos - 2];
	    state->wbuf[state->curpos - 2] = state->wbuf[state->curpos - 1];
	    state->wbuf[state->curpos - 1] = t;
	  }
	  break;

	default:
	  BEEP ();
734 735 736 737
      }
    }
    else
    {
738 739
      
self_insert:
740 741 742 743 744

      state->tabs = 0;
      /* use the raw keypress */
      ch = LastKey;

745 746 747 748 749 750
#ifdef KEY_ENTER
      /* treat ENTER the same as RETURN */
      if (ch == KEY_ENTER)
	ch = '\r';
#endif

751 752
      /* quietly ignore all other function keys */
      if (ch & ~0xff)
753
	continue;
754 755 756

      /* gather the octets into a wide character */
      {
757 758 759 760 761 762 763 764 765 766 767 768
	char c;
	size_t k;

	c = ch;
	k = mbrtowc (&wc, &c, 1, &mbstate);
	if (k == (size_t)(-2))
	  continue;
	else if (k && k != 1)
	{
	  memset (&mbstate, 0, sizeof (mbstate));
	  continue;
	}
769 770
      }

771
      if (first && (flags & MUTT_CLEAR))
772
      {
773 774 775
	first = 0;
	if (IsWPrint (wc)) /* why? */
	  state->curpos = state->lastchar = 0;
776 777 778 779
      }

      if (wc == '\r' || wc == '\n')
      {
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	/* Convert from wide characters */
	my_wcstombs (buf, buflen, state->wbuf, state->lastchar);
	if (!pass)
	  mutt_history_add (hclass, buf, 1);

	if (multiple)
	{
	  char **tfiles;
	  *numfiles = 1;
	  tfiles = safe_calloc (*numfiles, sizeof (char *));
	  mutt_expand_path (buf, buflen);
	  tfiles[0] = safe_strdup (buf);
	  *files = tfiles;
	}
	rv = 0; 
	goto bye;
796
      }
797
      else if (wc && (wc < ' ' || IsWPrint (wc))) /* why? */
798
      {
799 800 801 802 803 804 805 806
	if (state->lastchar >= state->wbuflen)
	{
	  state->wbuflen = state->lastchar + 20;
	  safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
	}
	memmove (state->wbuf + state->curpos + 1, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
	state->wbuf[state->curpos++] = wc;
	state->lastchar++;
807 808 809
      }
      else
      {
810 811
	mutt_flushinp ();
	BEEP ();
812 813 814
      }
    }
  }
815 816
  
  bye:
817

818 819
  mutt_reset_history_state (hclass);
  FREE (&tempbuf);
820 821 822
  return rv;
}

823
void mutt_free_enter_state (ENTER_STATE **esp)
824
{
825 826 827 828
  if (!esp) return;
  
  FREE (&(*esp)->wbuf);
  FREE (esp);		/* __FREE_CHECKED__ */
829 830 831 832 833 834 835 836
}

/*
 * TODO:
 * very narrow screen might crash it
 * sort out the input side
 * unprintable chars
 */