enter.c 17 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2000 Michael R. Elkins <[email protected]>
3
 * Copyright (C) 2000 Edmund Grimley Evans <[email protected]>
Thomas Roessler's avatar
Thomas Roessler committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 * 
 *     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
17
 *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
Thomas Roessler's avatar
Thomas Roessler committed
18 19
 */ 

20 21 22 23
#if HAVE_CONFIG_H
# include "config.h"
#endif

Thomas Roessler's avatar
Thomas Roessler committed
24 25 26 27
#include "mutt.h"
#include "mutt_menu.h"
#include "mutt_curses.h"
#include "keymap.h"
Thomas Roessler's avatar
Thomas Roessler committed
28
#include "history.h"
Thomas Roessler's avatar
Thomas Roessler committed
29 30 31 32 33 34

#include <string.h>

/* redraw flags for mutt_enter_string() */
enum
{
35 36
  M_REDRAW_INIT = 1,	/* go to end of line and redraw */
  M_REDRAW_LINE		/* redraw entire line */
Thomas Roessler's avatar
Thomas Roessler committed
37 38
};

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

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

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

static int my_addwch (wchar_t wc)
{
64
  int n = wcwidth (wc);
65
  if (IsWPrint (wc) && n > 0)
66 67 68 69 70 71
    return mutt_addwch (wc);
  if (!(wc & ~0x7f))
    return printw ("^%c", ((int)wc + 0x40) & 0x7f);
  if (!(wc & ~0xffff))
    return printw ("\\u%04x", (int)wc);
  return printw ("\\u%08x", (int)wc);
72 73 74 75 76 77 78 79 80 81 82 83
}

static size_t width_ceiling (const wchar_t *s, size_t n, int w1)
{
  const wchar_t *s0 = s;
  int w = 0;
  for (; n; s++, n--)
    if ((w += my_wcwidth (*s)) > w1)
      break;
  return s - s0;  
}

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

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 93
    if ((k = wcrtomb (dest, *src, &st)) == (size_t)(-1))
      break;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

  /* If this works, we can stop now */
  if (dlen >= MB_LEN_MAX) {
    wcrtomb (dest, 0, &st);
    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--)
      if ((k = wcrtomb (p, *src, &st)) == (size_t)(-1))
	break;
    p += wcrtomb (p, 0, &st);

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

    /* Otherwise we truncate the string in an ugly fashion */
    memcpy (dest, buf, dlen);
    dest[dlen - 1] = '\0'; /* assume original dlen > 0 */
  }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
}

size_t my_mbstowcs (wchar_t **pwbuf, size_t *pwbuflen, size_t i, char *buf)
{
  wchar_t wc;
  mbstate_t st;
  size_t k;
  wchar_t *wbuf;
  size_t wbuflen;

  wbuf = *pwbuf, wbuflen = *pwbuflen;
  memset (&st, 0, sizeof (st));
  for (; (k = mbrtowc (&wc, buf, MB_LEN_MAX, &st)) &&
	 k != (size_t)(-1) && k != (size_t)(-2); buf += k)
  {
    if (i >= wbuflen)
    {
      wbuflen = i + 20;
139
      safe_realloc (&wbuf, wbuflen * sizeof (*wbuf));
140 141 142 143 144 145 146 147
    }
    wbuf[i++] = wc;
  }
  *pwbuf = wbuf, *pwbuflen = wbuflen;
  return i;
}

/*
148
 * Replace part of the wchar_t buffer, from FROM to CURPOS, by BUF.
149 150
 */

151
static void replace_part (ENTER_STATE *state, size_t from, char *buf)
152 153
{
  /* Save the suffix */
154
  size_t savelen = state->lastchar - state->curpos;
155
  wchar_t *savebuf = safe_calloc (savelen, sizeof (wchar_t));
156
  memcpy (savebuf, state->wbuf + state->curpos, savelen * sizeof (wchar_t));
157 158

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

  /* Make space for suffix */
162
  if (state->curpos + savelen > state->wbuflen)
163
  {
164
    state->wbuflen = state->curpos + savelen;
165
    safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
166 167 168
  }

  /* Restore suffix */
169 170
  memcpy (state->wbuf + state->curpos, savebuf, savelen * sizeof (wchar_t));
  state->lastchar = state->curpos + savelen;
171

172
  FREE (&savebuf);
173 174 175 176
}

/*
 * Returns:
Thomas Roessler's avatar
Thomas Roessler committed
177 178 179 180
 *	1 need to redraw the screen and call me again
 *	0 if input was given
 * 	-1 if abort.
 */
181

182 183 184 185 186 187 188 189 190
int  mutt_enter_string(char *buf, size_t buflen, int y, int x, int flags)
{
  int rv;
  ENTER_STATE *es = mutt_new_enter_state ();
  rv = _mutt_enter_string (buf, buflen, y, x, flags, 0, NULL, NULL, es);
  mutt_free_enter_state (&es);
  return rv;
}

191
int _mutt_enter_string (char *buf, size_t buflen, int y, int x,
192 193
			int flags, int multiple, char ***files, int *numfiles,
			ENTER_STATE *state)
Thomas Roessler's avatar
Thomas Roessler committed
194
{
195 196 197
  int width = COLS - x - 1;
  int redraw;
  int pass = (flags & M_PASS);
Thomas Roessler's avatar
Thomas Roessler committed
198
  int first = 1;
199 200 201 202 203
  int ch, w, r;
  size_t i;
  wchar_t *tempbuf = 0;
  size_t templen = 0;
  history_class_t hclass;
204 205
  wchar_t wc;
  mbstate_t mbstate;
206

207
  int rv = 0;
208
  memset (&mbstate, 0, sizeof (mbstate));
209 210
  
  if (state->wbuf)
211 212 213 214 215
  {
    /* Coming back after return 1 */
    redraw = M_REDRAW_LINE;
  }
  else
216
  {
217
    /* Initialise wbuf from buf */
218
    state->wbuflen = 0;
219
    state->lastchar = my_mbstowcs (&state->wbuf, &state->wbuflen, 0, buf);
220
    redraw = M_REDRAW_INIT;
221 222
  }

223
  if (flags & (M_FILE | M_EFILE))
Thomas Roessler's avatar
Thomas Roessler committed
224
    hclass = HC_FILE;
225
  else if (flags & M_CMD)
Thomas Roessler's avatar
Thomas Roessler committed
226
    hclass = HC_CMD;
227
  else if (flags & M_ALIAS)
Thomas Roessler's avatar
Thomas Roessler committed
228
    hclass = HC_ALIAS;
229
  else if (flags & M_COMMAND)
230
    hclass = HC_COMMAND;
231 232
  else if (flags & M_PATTERN)
    hclass = HC_PATTERN;
Thomas Roessler's avatar
Thomas Roessler committed
233 234
  else 
    hclass = HC_OTHER;
235 236
    
  for (;;)
Thomas Roessler's avatar
Thomas Roessler committed
237
  {
238
    if (redraw && !pass)
Thomas Roessler's avatar
Thomas Roessler committed
239 240 241
    {
      if (redraw == M_REDRAW_INIT)
      {
242
	/* Go to end of line */
243 244
	state->curpos = state->lastchar;
	state->begin = width_ceiling (state->wbuf, state->lastchar, my_wcswidth (state->wbuf, state->lastchar) - width + 1);
245
      } 
246 247
      if (state->curpos < state->begin ||
	  my_wcswidth (state->wbuf + state->begin, state->curpos - state->begin) >= width)
248
	state->begin = width_ceiling (state->wbuf, state->lastchar, my_wcswidth (state->wbuf, state->curpos) - width / 2);
249 250
      move (y, x);
      w = 0;
251
      for (i = state->begin; i < state->lastchar; i++)
Thomas Roessler's avatar
Thomas Roessler committed
252
      {
253
	w += my_wcwidth (state->wbuf[i]);
254
	if (w > width)
Thomas Roessler's avatar
Thomas Roessler committed
255
	  break;
256
	my_addwch (state->wbuf[i]);
257
      }
Thomas Roessler's avatar
Thomas Roessler committed
258
      clrtoeol ();
259
      move (y, x + my_wcswidth (state->wbuf + state->begin, state->curpos - state->begin));
Thomas Roessler's avatar
Thomas Roessler committed
260 261 262
    }
    mutt_refresh ();

263
    if ((ch = km_dokey (MENU_EDITOR)) == -1)
Thomas Roessler's avatar
Thomas Roessler committed
264
    {
265 266
      rv = -1; 
      goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
267 268
    }

269
    if (ch != OP_NULL)
Thomas Roessler's avatar
Thomas Roessler committed
270
    {
271
      first = 0;
272
      if (ch != OP_EDITOR_COMPLETE)
273
	state->tabs = 0;
274
      redraw = M_REDRAW_LINE;
Thomas Roessler's avatar
Thomas Roessler committed
275 276 277
      switch (ch)
      {
	case OP_EDITOR_HISTORY_UP:
278
	  state->curpos = state->lastchar;
279
	  replace_part (state, 0, mutt_history_prev (hclass));
280
	  redraw = M_REDRAW_INIT;
Thomas Roessler's avatar
Thomas Roessler committed
281
	  break;
282

Thomas Roessler's avatar
Thomas Roessler committed
283
	case OP_EDITOR_HISTORY_DOWN:
284
	  state->curpos = state->lastchar;
Thomas Roessler's avatar
Thomas Roessler committed
285
	  replace_part (state, 0, mutt_history_next (hclass));
286
	  redraw = M_REDRAW_INIT;
Thomas Roessler's avatar
Thomas Roessler committed
287
	  break;
288

Thomas Roessler's avatar
Thomas Roessler committed
289
	case OP_EDITOR_BACKSPACE:
290
	  if (state->curpos == 0)
Thomas Roessler's avatar
Thomas Roessler committed
291
	    BEEP ();
292
	  else
Thomas Roessler's avatar
Thomas Roessler committed
293
	  {
294
	    i = state->curpos;
295
	    while (i && COMB_CHAR (state->wbuf[i - 1]))
296 297 298
	      --i;
	    if (i)
	      --i;
299
	    memmove (state->wbuf + i, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
300
	    state->lastchar -= state->curpos - i;
301
	    state->curpos = i;
Thomas Roessler's avatar
Thomas Roessler committed
302 303
	  }
	  break;
304

Thomas Roessler's avatar
Thomas Roessler committed
305
	case OP_EDITOR_BOL:
306
	  state->curpos = 0;
Thomas Roessler's avatar
Thomas Roessler committed
307
	  break;
308

Thomas Roessler's avatar
Thomas Roessler committed
309
	case OP_EDITOR_EOL:
310
	  redraw= M_REDRAW_INIT;
Thomas Roessler's avatar
Thomas Roessler committed
311
	  break;
312

Thomas Roessler's avatar
Thomas Roessler committed
313
	case OP_EDITOR_KILL_LINE:
314
	  state->curpos = state->lastchar = 0;
Thomas Roessler's avatar
Thomas Roessler committed
315
	  break;
316

Thomas Roessler's avatar
Thomas Roessler committed
317
	case OP_EDITOR_KILL_EOL:
318
	  state->lastchar = state->curpos;
Thomas Roessler's avatar
Thomas Roessler committed
319
	  break;
320

Thomas Roessler's avatar
Thomas Roessler committed
321
	case OP_EDITOR_BACKWARD_CHAR:
322
	  if (state->curpos == 0)
Thomas Roessler's avatar
Thomas Roessler committed
323 324 325
	    BEEP ();
	  else
	  {
326
	    while (state->curpos && COMB_CHAR (state->wbuf[state->curpos - 1]))
327 328 329
	      state->curpos--;
	    if (state->curpos)
	      state->curpos--;
Thomas Roessler's avatar
Thomas Roessler committed
330 331
	  }
	  break;
332

Thomas Roessler's avatar
Thomas Roessler committed
333
	case OP_EDITOR_FORWARD_CHAR:
334
	  if (state->curpos == state->lastchar)
Thomas Roessler's avatar
Thomas Roessler committed
335 336 337
	    BEEP ();
	  else
	  {
338
	    ++state->curpos;
339
	    while (state->curpos < state->lastchar && COMB_CHAR (state->wbuf[state->curpos]))
340
	      ++state->curpos;
Thomas Roessler's avatar
Thomas Roessler committed
341 342
	  }
	  break;
343

344
	case OP_EDITOR_BACKWARD_WORD:
345
	  if (state->curpos == 0)
346 347 348
	    BEEP ();
	  else
	  {
349
	    while (state->curpos && iswspace (state->wbuf[state->curpos - 1]))
350
	      --state->curpos;
351
	    while (state->curpos && !iswspace (state->wbuf[state->curpos - 1]))
352
	      --state->curpos;
353 354 355 356
	  }
	  break;

	case OP_EDITOR_FORWARD_WORD:
357
	  if (state->curpos == state->lastchar)
358 359 360
	    BEEP ();
	  else
	  {
361
	    while (state->curpos < state->lastchar && iswspace (state->wbuf[state->curpos]))
362
	      ++state->curpos;
363
	    while (state->curpos < state->lastchar && !iswspace (state->wbuf[state->curpos]))
364
	      ++state->curpos;
365 366 367
	  }
	  break;

368 369 370
	case OP_EDITOR_CAPITALIZE_WORD:
	case OP_EDITOR_UPCASE_WORD:
	case OP_EDITOR_DOWNCASE_WORD:
371
	  if (state->curpos == state->lastchar)
372
	  {
373
	    BEEP ();
374 375
	    break;
	  }
376
	  while (state->curpos && !iswspace (state->wbuf[state->curpos]))
377
	    --state->curpos;
378
	  while (state->curpos < state->lastchar && iswspace (state->wbuf[state->curpos]))
379
	    ++state->curpos;
380
	  while (state->curpos < state->lastchar && !iswspace (state->wbuf[state->curpos]))
381
	  {
382
	    if (ch == OP_EDITOR_DOWNCASE_WORD)
383
	      state->wbuf[state->curpos] = towlower (state->wbuf[state->curpos]);
384
	    else
385
	    {
386
	      state->wbuf[state->curpos] = towupper (state->wbuf[state->curpos]);
387 388
	      if (ch == OP_EDITOR_CAPITALIZE_WORD)
		ch = OP_EDITOR_DOWNCASE_WORD;
389
	    }
390
	    state->curpos++;
391 392
	  }
	  break;
393

Thomas Roessler's avatar
Thomas Roessler committed
394
	case OP_EDITOR_DELETE_CHAR:
395
	  if (state->curpos == state->lastchar)
Thomas Roessler's avatar
Thomas Roessler committed
396
	    BEEP ();
397
	  else
Thomas Roessler's avatar
Thomas Roessler committed
398
	  {
399
	    i = state->curpos;
400
	    while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
401
	      ++i;
402
	    if (i < state->lastchar)
403
	      ++i;
404
	    while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
405
	      ++i;
406
	    memmove (state->wbuf + state->curpos, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t));
407
	    state->lastchar -= i - state->curpos;
Thomas Roessler's avatar
Thomas Roessler committed
408 409
	  }
	  break;
410

411 412 413 414
	case OP_EDITOR_KILL_WORD:
	  /* delete to begining of word */
	  if (state->curpos != 0)
	  {
415 416 417 418
	    i = state->curpos;
	    while (i && iswspace (state->wbuf[i - 1]))
	      --i;
	    if (i)
419
	    {
420
	      if (iswalnum (state->wbuf[i - 1]))
421
	      {
422
		for (--i; i && iswalnum (state->wbuf[i - 1]); i--)
423 424 425
		  ;
	      }
	      else
426
		--i;
427
	    }
428 429 430 431
	    memmove (state->wbuf + i, state->wbuf + state->curpos,
		     (state->lastchar - state->curpos) * sizeof (wchar_t));
	    state->lastchar += i - state->curpos;
	    state->curpos = i;
432 433 434 435 436
	  }
	  break;

	case OP_EDITOR_KILL_EOW:
	  /* delete to end of word */
437 438
	  for (i = state->curpos;
	       i < state->lastchar && iswspace (state->wbuf[i]); i++)
439
	    ;
440
	  for (; i < state->lastchar && !iswspace (state->wbuf[i]); i++)
441
	    ;
442 443 444
	  memmove (state->wbuf + state->curpos, state->wbuf + i,
		   (state->lastchar - i) * sizeof (wchar_t));
	  state->lastchar += state->curpos - i;
445
	  break;
446

Thomas Roessler's avatar
Thomas Roessler committed
447 448 449 450
	case OP_EDITOR_BUFFY_CYCLE:
	  if (flags & M_EFILE)
	  {
	    first = 1; /* clear input if user types a real key later */
451
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
452
	    mutt_buffy (buf, buflen);
453
	    state->curpos = state->lastchar = my_mbstowcs (&state->wbuf, &state->wbuflen, 0, buf);
Thomas Roessler's avatar
Thomas Roessler committed
454 455 456 457 458 459 460
	    break;
	  }
	  else if (!(flags & M_FILE))
	    goto self_insert;
	  /* fall through to completion routine (M_FILE) */

	case OP_EDITOR_COMPLETE:
461
	  state->tabs++;
Thomas Roessler's avatar
Thomas Roessler committed
462 463
	  if (flags & M_CMD)
	  {
464
	    for (i = state->curpos; i && state->wbuf[i-1] != ' '; i--)
465
	      ;
466
	    my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
467
	    if (tempbuf && templen == state->lastchar - i &&
468
		!memcmp (tempbuf, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t)))
Thomas Roessler's avatar
Thomas Roessler committed
469
	    {
470
	      mutt_select_file (buf, buflen, (flags & M_EFILE) ? M_SEL_FOLDER : 0);
Thomas Roessler's avatar
Thomas Roessler committed
471
	      set_option (OPTNEEDREDRAW);
472
	      if (*buf)
473
		replace_part (state, i, buf);
474 475
	      rv = 1; 
	      goto bye;
476 477 478
	    }
	    if (!mutt_complete (buf, buflen))
	    {
479
	      templen = state->lastchar - i;
480
	      safe_realloc (&tempbuf, templen * sizeof (wchar_t));
Thomas Roessler's avatar
Thomas Roessler committed
481 482 483
	    }
	    else
	      BEEP ();
484

485
	    replace_part (state, i, buf);
Thomas Roessler's avatar
Thomas Roessler committed
486 487 488 489
	  }
	  else if (flags & M_ALIAS)
	  {
	    /* invoke the alias-menu to get more addresses */
490 491
	    for (i = state->curpos; i && state->wbuf[i-1] != ',' && 
		 state->wbuf[i-1] != ':'; i--)
492
	      ;
493
	    for (; i < state->lastchar && state->wbuf[i] == ' '; i++)
494
	      ;
495
	    my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
496
	    r = mutt_alias_complete (buf, buflen);
497
	    replace_part (state, i, buf);
498
	    if (!r)
499 500 501 502
	    {
	      rv = 1;
	      goto bye;
	    }
503
	    break;
Thomas Roessler's avatar
Thomas Roessler committed
504
	  }
Thomas Roessler's avatar
Thomas Roessler committed
505 506
	  else if (flags & M_COMMAND)
	  {
507
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
508
	    i = strlen (buf);
509
	    if (i && buf[i - 1] == '=' &&
510
		mutt_var_value_complete (buf, buflen, i))
511 512
	      state->tabs = 0;
	    else if (!mutt_command_complete (buf, buflen, i, state->tabs))
513
	      BEEP ();
514
	    replace_part (state, 0, buf);
Thomas Roessler's avatar
Thomas Roessler committed
515
	  }
Thomas Roessler's avatar
Thomas Roessler committed
516 517
	  else if (flags & (M_FILE | M_EFILE))
	  {
518
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
Thomas Roessler's avatar
Thomas Roessler committed
519 520

	    /* see if the path has changed from the last time */
521
	    if ((!tempbuf && !state->lastchar) || (tempbuf && templen == state->lastchar &&
522
		!memcmp (tempbuf, state->wbuf, state->lastchar * sizeof (wchar_t))))
Thomas Roessler's avatar
Thomas Roessler committed
523
	    {
524 525 526
	      _mutt_select_file (buf, buflen, 
				 ((flags & M_EFILE) ? M_SEL_FOLDER : 0) | (multiple ? M_SEL_MULTI : 0), 
				 files, numfiles);
Thomas Roessler's avatar
Thomas Roessler committed
527
	      set_option (OPTNEEDREDRAW);
528
	      if (*buf)
Thomas Roessler's avatar
Thomas Roessler committed
529
	      {
530 531 532
		mutt_pretty_mailbox (buf);
		if (!pass)
		  mutt_history_add (hclass, buf);
533 534
		rv = 0;
		goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
535
	      }
536 537

	      /* file selection cancelled */
538 539
	      rv = 1;
	      goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
540 541
	    }

542 543
	    if (!mutt_complete (buf, buflen))
	    {
544
	      templen = state->lastchar;
545
	      safe_realloc (&tempbuf, templen * sizeof (wchar_t));
546
	      memcpy (tempbuf, state->wbuf, templen * sizeof (wchar_t));
547
	    }
Thomas Roessler's avatar
Thomas Roessler committed
548 549
	    else
	      BEEP (); /* let the user know that nothing matched */
550
	    replace_part (state, 0, buf);
Thomas Roessler's avatar
Thomas Roessler committed
551 552 553 554 555 556 557 558 559
	  }
	  else
	    goto self_insert;
	  break;

	case OP_EDITOR_COMPLETE_QUERY:
	  if (flags & M_ALIAS)
	  {
	    /* invoke the query-menu to get more addresses */
560
	    if ((i = state->curpos))
Thomas Roessler's avatar
Thomas Roessler committed
561
	    {
562
	      for (; i && state->wbuf[i - 1] != ','; i--)
563
		;
564
	      for (; i < state->curpos && state->wbuf[i] == ' '; i++)
565 566
		;
	    }
567 568 569 570 571

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

572 573
	    rv = 1; 
	    goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
574 575 576 577 578
	  }
	  else
	    goto self_insert;

	case OP_EDITOR_QUOTE_CHAR:
579
	  {
580 581 582 583 584 585 586 587
	    event_t event;
	    /*ADDCH (LastKey);*/
	    event = mutt_getch ();
	    if (event.ch != -1)
	    {
	      LastKey = event.ch;
	      goto self_insert;
	    }
588 589
	  }

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	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;

Thomas Roessler's avatar
Thomas Roessler committed
608 609 610 611 612 613
	default:
	  BEEP ();
      }
    }
    else
    {
614
      
Thomas Roessler's avatar
Thomas Roessler committed
615 616
self_insert:

617
      state->tabs = 0;
Thomas Roessler's avatar
Thomas Roessler committed
618 619 620
      /* use the raw keypress */
      ch = LastKey;

Thomas Roessler's avatar
Thomas Roessler committed
621 622 623 624 625 626 627
#ifdef KEY_ENTER
      /* treat ENTER the same as RETURN */
      if (ch == KEY_ENTER)
	ch = '\r';
#endif

      /* quietly ignore all other function keys */
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
      if (ch & ~0xff)
	continue;

      /* gather the octets into a wide character */
      {
	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;
	}
      }

Thomas Roessler's avatar
Thomas Roessler committed
647 648 649
      if (first && (flags & M_CLEAR))
      {
	first = 0;
650
	if (IsWPrint (wc)) /* why? */
651
	  state->curpos = state->lastchar = 0;
Thomas Roessler's avatar
Thomas Roessler committed
652 653
      }

654
      if (wc == '\r' || wc == '\n')
Thomas Roessler's avatar
Thomas Roessler committed
655
      {
656
	/* Convert from wide characters */
657
	my_wcstombs (buf, buflen, state->wbuf, state->lastchar);
Thomas Roessler's avatar
Thomas Roessler committed
658
	if (!pass)
659 660
	  mutt_history_add (hclass, buf);

661 662 663 664
	if (multiple)
	{
	  char **tfiles;
	  *numfiles = 1;
665
	  tfiles = safe_calloc (*numfiles, sizeof (char *));
666 667
	  mutt_expand_path (buf, buflen);
	  tfiles[0] = safe_strdup (buf);
668 669
	  *files = tfiles;
	}
670 671
	rv = 0; 
	goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
672
      }
673
      else if (wc && (wc < ' ' || IsWPrint (wc))) /* why? */
Thomas Roessler's avatar
Thomas Roessler committed
674
      {
675
	if (state->lastchar >= state->wbuflen)
Thomas Roessler's avatar
Thomas Roessler committed
676
	{
677
	  state->wbuflen = state->lastchar + 20;
678
	  safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
Thomas Roessler's avatar
Thomas Roessler committed
679
	}
680
	memmove (state->wbuf + state->curpos + 1, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
681
	state->wbuf[state->curpos++] = wc;
682
	state->lastchar++;
Thomas Roessler's avatar
Thomas Roessler committed
683 684 685 686 687 688 689 690
      }
      else
      {
	mutt_flushinp ();
	BEEP ();
      }
    }
  }
691 692 693
  
  bye:
  
694
  FREE (&tempbuf);
695 696 697 698 699 700 701
  return rv;
}

void mutt_free_enter_state (ENTER_STATE **esp)
{
  if (!esp) return;
  
702 703
  FREE (&(*esp)->wbuf);
  FREE (esp);
Thomas Roessler's avatar
Thomas Roessler committed
704
}
705 706 707 708 709 710 711

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