enter.c 19.3 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2000,2007,2011,2013 Michael R. Elkins <me@mutt.org>
3
 * Copyright (C) 2000-2001 Edmund Grimley Evans <edmundo@rano.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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
{
Derek Martin's avatar
Derek Martin committed
35 36
  MUTT_REDRAW_INIT = 1,	/* go to end of line and redraw */
  MUTT_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

  /* If this works, we can stop now */
  if (dlen >= MB_LEN_MAX) {
Michael Elkins's avatar
Michael Elkins committed
97
    wcrtomb (dest, 0, &st);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    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
static size_t my_mbstowcs (wchar_t **pwbuf, size_t *pwbuflen, size_t i, char *buf)
124 125 126 127 128 129 130 131
{
  wchar_t wc;
  mbstate_t st;
  size_t k;
  wchar_t *wbuf;
  size_t wbuflen;

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

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

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

  if (savelen)
  {
    savebuf = safe_calloc (savelen, sizeof (wchar_t));
    memcpy (savebuf, state->wbuf + state->curpos, savelen * sizeof (wchar_t));
  }
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 186 187 188 189 190 191
    /* Make space for suffix */
    if (state->curpos + savelen > state->wbuflen)
    {
      state->wbuflen = state->curpos + savelen;
      safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
    }

    /* Restore suffix */
    memcpy (state->wbuf + state->curpos, savebuf, savelen * sizeof (wchar_t));
    FREE (&savebuf);
192 193
  }

194
  state->lastchar = state->curpos + savelen;
195 196
}

197 198 199
/*
 * Return 1 if the character is not typically part of a pathname
 */
200
static inline int is_shell_char(wchar_t ch)
201
{
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
/*
 * Returns:
Thomas Roessler's avatar
Thomas Roessler committed
208 209 210 211
 *	1 need to redraw the screen and call me again
 *	0 if input was given
 * 	-1 if abort.
 */
212

213
int  mutt_enter_string(char *buf, size_t buflen, int col, int flags)
214 215 216
{
  int rv;
  ENTER_STATE *es = mutt_new_enter_state ();
217
  rv = _mutt_enter_string (buf, buflen, col, flags, 0, NULL, NULL, es);
218 219 220 221
  mutt_free_enter_state (&es);
  return rv;
}

222
int _mutt_enter_string (char *buf, size_t buflen, int col,
223 224
			int flags, int multiple, char ***files, int *numfiles,
			ENTER_STATE *state)
Thomas Roessler's avatar
Thomas Roessler committed
225
{
226
  int width = MuttMessageWindow->cols - col - 1;
227
  int redraw;
Derek Martin's avatar
Derek Martin committed
228
  int pass = (flags & MUTT_PASS);
Thomas Roessler's avatar
Thomas Roessler committed
229
  int first = 1;
230 231 232 233 234
  int ch, w, r;
  size_t i;
  wchar_t *tempbuf = 0;
  size_t templen = 0;
  history_class_t hclass;
235 236
  wchar_t wc;
  mbstate_t mbstate;
237

238
  int rv = 0;
239
  memset (&mbstate, 0, sizeof (mbstate));
240 241
  
  if (state->wbuf)
242 243
  {
    /* Coming back after return 1 */
Derek Martin's avatar
Derek Martin committed
244
    redraw = MUTT_REDRAW_LINE;
245
    first = 0;
246 247
  }
  else
248
  {
249
    /* Initialise wbuf from buf */
250
    state->wbuflen = 0;
251
    state->lastchar = my_mbstowcs (&state->wbuf, &state->wbuflen, 0, buf);
Derek Martin's avatar
Derek Martin committed
252
    redraw = MUTT_REDRAW_INIT;
253 254
  }

Derek Martin's avatar
Derek Martin committed
255
  if (flags & MUTT_FILE)
Thomas Roessler's avatar
Thomas Roessler committed
256
    hclass = HC_FILE;
Derek Martin's avatar
Derek Martin committed
257
  else if (flags & MUTT_EFILE)
258
    hclass = HC_MBOX;
Derek Martin's avatar
Derek Martin committed
259
  else if (flags & MUTT_CMD)
Thomas Roessler's avatar
Thomas Roessler committed
260
    hclass = HC_CMD;
Derek Martin's avatar
Derek Martin committed
261
  else if (flags & MUTT_ALIAS)
Thomas Roessler's avatar
Thomas Roessler committed
262
    hclass = HC_ALIAS;
Derek Martin's avatar
Derek Martin committed
263
  else if (flags & MUTT_COMMAND)
264
    hclass = HC_COMMAND;
Derek Martin's avatar
Derek Martin committed
265
  else if (flags & MUTT_PATTERN)
266
    hclass = HC_PATTERN;
Thomas Roessler's avatar
Thomas Roessler committed
267 268
  else 
    hclass = HC_OTHER;
269 270
    
  for (;;)
Thomas Roessler's avatar
Thomas Roessler committed
271
  {
272
    if (redraw && !pass)
Thomas Roessler's avatar
Thomas Roessler committed
273
    {
Derek Martin's avatar
Derek Martin committed
274
      if (redraw == MUTT_REDRAW_INIT)
Thomas Roessler's avatar
Thomas Roessler committed
275
      {
276
	/* Go to end of line */
277 278
	state->curpos = state->lastchar;
	state->begin = width_ceiling (state->wbuf, state->lastchar, my_wcswidth (state->wbuf, state->lastchar) - width + 1);
279
      } 
280 281
      if (state->curpos < state->begin ||
	  my_wcswidth (state->wbuf + state->begin, state->curpos - state->begin) >= width)
282
	state->begin = width_ceiling (state->wbuf, state->lastchar, my_wcswidth (state->wbuf, state->curpos) - width / 2);
283
      mutt_window_move (MuttMessageWindow, 0, col);
284
      w = 0;
285
      for (i = state->begin; i < state->lastchar; i++)
Thomas Roessler's avatar
Thomas Roessler committed
286
      {
287
	w += my_wcwidth (state->wbuf[i]);
288
	if (w > width)
Thomas Roessler's avatar
Thomas Roessler committed
289
	  break;
290
	my_addwch (state->wbuf[i]);
291
      }
292 293 294
      mutt_window_clrtoeol (MuttMessageWindow);
      mutt_window_move (MuttMessageWindow, 0,
          col + my_wcswidth (state->wbuf + state->begin, state->curpos - state->begin));
Thomas Roessler's avatar
Thomas Roessler committed
295 296 297
    }
    mutt_refresh ();

298
    if ((ch = km_dokey (MENU_EDITOR)) == -1)
Thomas Roessler's avatar
Thomas Roessler committed
299
    {
300 301
      rv = -1; 
      goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
302 303
    }

304
    if (ch != OP_NULL)
Thomas Roessler's avatar
Thomas Roessler committed
305
    {
306
      first = 0;
307
      if (ch != OP_EDITOR_COMPLETE && ch != OP_EDITOR_COMPLETE_QUERY)
308
	state->tabs = 0;
Derek Martin's avatar
Derek Martin committed
309
      redraw = MUTT_REDRAW_LINE;
Thomas Roessler's avatar
Thomas Roessler committed
310 311 312
      switch (ch)
      {
	case OP_EDITOR_HISTORY_UP:
313
	  state->curpos = state->lastchar;
314 315 316 317 318
	  if (mutt_history_at_scratch (hclass))
	  {
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
	    mutt_history_save_scratch (hclass, buf);
	  }
319
	  replace_part (state, 0, mutt_history_prev (hclass));
Derek Martin's avatar
Derek Martin committed
320
	  redraw = MUTT_REDRAW_INIT;
Thomas Roessler's avatar
Thomas Roessler committed
321
	  break;
322

Thomas Roessler's avatar
Thomas Roessler committed
323
	case OP_EDITOR_HISTORY_DOWN:
324
	  state->curpos = state->lastchar;
325 326 327 328 329
	  if (mutt_history_at_scratch (hclass))
	  {
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
	    mutt_history_save_scratch (hclass, buf);
	  }
Thomas Roessler's avatar
Thomas Roessler committed
330
	  replace_part (state, 0, mutt_history_next (hclass));
Derek Martin's avatar
Derek Martin committed
331
	  redraw = MUTT_REDRAW_INIT;
Thomas Roessler's avatar
Thomas Roessler committed
332
	  break;
333

Thomas Roessler's avatar
Thomas Roessler committed
334
	case OP_EDITOR_BACKSPACE:
335
	  if (state->curpos == 0)
Thomas Roessler's avatar
Thomas Roessler committed
336
	    BEEP ();
337
	  else
Thomas Roessler's avatar
Thomas Roessler committed
338
	  {
339
	    i = state->curpos;
340
	    while (i && COMB_CHAR (state->wbuf[i - 1]))
341 342 343
	      --i;
	    if (i)
	      --i;
344
	    memmove (state->wbuf + i, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
345
	    state->lastchar -= state->curpos - i;
346
	    state->curpos = i;
Thomas Roessler's avatar
Thomas Roessler committed
347 348
	  }
	  break;
349

Thomas Roessler's avatar
Thomas Roessler committed
350
	case OP_EDITOR_BOL:
351
	  state->curpos = 0;
Thomas Roessler's avatar
Thomas Roessler committed
352
	  break;
353

Thomas Roessler's avatar
Thomas Roessler committed
354
	case OP_EDITOR_EOL:
Derek Martin's avatar
Derek Martin committed
355
	  redraw= MUTT_REDRAW_INIT;
Thomas Roessler's avatar
Thomas Roessler committed
356
	  break;
357

Thomas Roessler's avatar
Thomas Roessler committed
358
	case OP_EDITOR_KILL_LINE:
359
	  state->curpos = state->lastchar = 0;
Thomas Roessler's avatar
Thomas Roessler committed
360
	  break;
361

Thomas Roessler's avatar
Thomas Roessler committed
362
	case OP_EDITOR_KILL_EOL:
363
	  state->lastchar = state->curpos;
Thomas Roessler's avatar
Thomas Roessler committed
364
	  break;
365

Thomas Roessler's avatar
Thomas Roessler committed
366
	case OP_EDITOR_BACKWARD_CHAR:
367
	  if (state->curpos == 0)
Thomas Roessler's avatar
Thomas Roessler committed
368 369 370
	    BEEP ();
	  else
	  {
371
	    while (state->curpos && COMB_CHAR (state->wbuf[state->curpos - 1]))
372 373 374
	      state->curpos--;
	    if (state->curpos)
	      state->curpos--;
Thomas Roessler's avatar
Thomas Roessler committed
375 376
	  }
	  break;
377

Thomas Roessler's avatar
Thomas Roessler committed
378
	case OP_EDITOR_FORWARD_CHAR:
379
	  if (state->curpos == state->lastchar)
Thomas Roessler's avatar
Thomas Roessler committed
380 381 382
	    BEEP ();
	  else
	  {
383
	    ++state->curpos;
384
	    while (state->curpos < state->lastchar && COMB_CHAR (state->wbuf[state->curpos]))
385
	      ++state->curpos;
Thomas Roessler's avatar
Thomas Roessler committed
386 387
	  }
	  break;
388

389
	case OP_EDITOR_BACKWARD_WORD:
390
	  if (state->curpos == 0)
391 392 393
	    BEEP ();
	  else
	  {
394
	    while (state->curpos && iswspace (state->wbuf[state->curpos - 1]))
395
	      --state->curpos;
396
	    while (state->curpos && !iswspace (state->wbuf[state->curpos - 1]))
397
	      --state->curpos;
398 399 400 401
	  }
	  break;

	case OP_EDITOR_FORWARD_WORD:
402
	  if (state->curpos == state->lastchar)
403 404 405
	    BEEP ();
	  else
	  {
406
	    while (state->curpos < state->lastchar && iswspace (state->wbuf[state->curpos]))
407
	      ++state->curpos;
408
	    while (state->curpos < state->lastchar && !iswspace (state->wbuf[state->curpos]))
409
	      ++state->curpos;
410 411 412
	  }
	  break;

413 414 415
	case OP_EDITOR_CAPITALIZE_WORD:
	case OP_EDITOR_UPCASE_WORD:
	case OP_EDITOR_DOWNCASE_WORD:
416
	  if (state->curpos == state->lastchar)
417
	  {
418
	    BEEP ();
419 420
	    break;
	  }
421
	  while (state->curpos && !iswspace (state->wbuf[state->curpos]))
422
	    --state->curpos;
423
	  while (state->curpos < state->lastchar && iswspace (state->wbuf[state->curpos]))
424
	    ++state->curpos;
425
	  while (state->curpos < state->lastchar && !iswspace (state->wbuf[state->curpos]))
426
	  {
427
	    if (ch == OP_EDITOR_DOWNCASE_WORD)
428
	      state->wbuf[state->curpos] = towlower (state->wbuf[state->curpos]);
429
	    else
430
	    {
431
	      state->wbuf[state->curpos] = towupper (state->wbuf[state->curpos]);
432 433
	      if (ch == OP_EDITOR_CAPITALIZE_WORD)
		ch = OP_EDITOR_DOWNCASE_WORD;
434
	    }
435
	    state->curpos++;
436 437
	  }
	  break;
438

Thomas Roessler's avatar
Thomas Roessler committed
439
	case OP_EDITOR_DELETE_CHAR:
440
	  if (state->curpos == state->lastchar)
Thomas Roessler's avatar
Thomas Roessler committed
441
	    BEEP ();
442
	  else
Thomas Roessler's avatar
Thomas Roessler committed
443
	  {
444
	    i = state->curpos;
445
	    while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
446
	      ++i;
447
	    if (i < state->lastchar)
448
	      ++i;
449
	    while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
450
	      ++i;
451
	    memmove (state->wbuf + state->curpos, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t));
452
	    state->lastchar -= i - state->curpos;
Thomas Roessler's avatar
Thomas Roessler committed
453 454
	  }
	  break;
455

456
	case OP_EDITOR_KILL_WORD:
457
	  /* delete to beginning of word */
458 459
	  if (state->curpos != 0)
	  {
460 461 462 463
	    i = state->curpos;
	    while (i && iswspace (state->wbuf[i - 1]))
	      --i;
	    if (i)
464
	    {
465
	      if (iswalnum (state->wbuf[i - 1]))
466
	      {
467
		for (--i; i && iswalnum (state->wbuf[i - 1]); i--)
468 469 470
		  ;
	      }
	      else
471
		--i;
472
	    }
473 474 475 476
	    memmove (state->wbuf + i, state->wbuf + state->curpos,
		     (state->lastchar - state->curpos) * sizeof (wchar_t));
	    state->lastchar += i - state->curpos;
	    state->curpos = i;
477 478 479 480 481
	  }
	  break;

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

	  /* first skip over whitespace */
484 485
	  for (i = state->curpos;
	       i < state->lastchar && iswspace (state->wbuf[i]); i++)
486
	    ;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

	  /* 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;
	    }
	  }

505 506 507
	  memmove (state->wbuf + state->curpos, state->wbuf + i,
		   (state->lastchar - i) * sizeof (wchar_t));
	  state->lastchar += state->curpos - i;
508
	  break;
509

Thomas Roessler's avatar
Thomas Roessler committed
510
	case OP_EDITOR_BUFFY_CYCLE:
Derek Martin's avatar
Derek Martin committed
511
	  if (flags & MUTT_EFILE)
Thomas Roessler's avatar
Thomas Roessler committed
512 513
	  {
	    first = 1; /* clear input if user types a real key later */
514
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
515
	    mutt_buffy (buf, buflen);
516
	    state->curpos = state->lastchar = my_mbstowcs (&state->wbuf, &state->wbuflen, 0, buf);
Thomas Roessler's avatar
Thomas Roessler committed
517 518
	    break;
	  }
Derek Martin's avatar
Derek Martin committed
519
	  else if (!(flags & MUTT_FILE))
Thomas Roessler's avatar
Thomas Roessler committed
520
	    goto self_insert;
Derek Martin's avatar
Derek Martin committed
521
	  /* fall through to completion routine (MUTT_FILE) */
Thomas Roessler's avatar
Thomas Roessler committed
522 523

	case OP_EDITOR_COMPLETE:
524
	case OP_EDITOR_COMPLETE_QUERY:
525
	  state->tabs++;
Derek Martin's avatar
Derek Martin committed
526
	  if (flags & MUTT_CMD)
Thomas Roessler's avatar
Thomas Roessler committed
527
	  {
528
	    for (i = state->curpos; i && !is_shell_char(state->wbuf[i-1]); i--)
529
	      ;
530
	    my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
531
	    if (tempbuf && templen == state->lastchar - i &&
532
		!memcmp (tempbuf, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t)))
Thomas Roessler's avatar
Thomas Roessler committed
533
	    {
Derek Martin's avatar
Derek Martin committed
534
	      mutt_select_file (buf, buflen, (flags & MUTT_EFILE) ? MUTT_SEL_FOLDER : 0);
Thomas Roessler's avatar
Thomas Roessler committed
535
	      set_option (OPTNEEDREDRAW);
536
	      if (*buf)
537
		replace_part (state, i, buf);
538 539
	      rv = 1; 
	      goto bye;
540 541 542
	    }
	    if (!mutt_complete (buf, buflen))
	    {
543
	      templen = state->lastchar - i;
544
	      safe_realloc (&tempbuf, templen * sizeof (wchar_t));
Thomas Roessler's avatar
Thomas Roessler committed
545 546 547
	    }
	    else
	      BEEP ();
548

549
	    replace_part (state, i, buf);
Thomas Roessler's avatar
Thomas Roessler committed
550
	  }
Derek Martin's avatar
Derek Martin committed
551
	  else if (flags & MUTT_ALIAS && ch == OP_EDITOR_COMPLETE)
Thomas Roessler's avatar
Thomas Roessler committed
552 553
	  {
	    /* invoke the alias-menu to get more addresses */
554 555
	    for (i = state->curpos; i && state->wbuf[i-1] != ',' && 
		 state->wbuf[i-1] != ':'; i--)
556
	      ;
557
	    for (; i < state->lastchar && state->wbuf[i] == ' '; i++)
558
	      ;
559
	    my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
560
	    r = mutt_alias_complete (buf, buflen);
561
	    replace_part (state, i, buf);
562
	    if (!r)
563 564 565 566
	    {
	      rv = 1;
	      goto bye;
	    }
567
	    break;
Thomas Roessler's avatar
Thomas Roessler committed
568
	  }
David Champion's avatar
David Champion committed
569 570 571 572 573 574 575 576 577
	  else if (flags & MUTT_LABEL && 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);
578
	    r = mutt_label_complete (buf, buflen, strlen (buf), state->tabs);
David Champion's avatar
David Champion committed
579 580 581 582 583 584 585 586
	    replace_part (state, i, buf);
	    if (!r)
	    {
	      rv = 1;
	      goto bye;
	    }
	    break;
	  }
Derek Martin's avatar
Derek Martin committed
587
	  else if (flags & MUTT_ALIAS && ch == OP_EDITOR_COMPLETE_QUERY)
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	  {
	    /* 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;
	  }
Derek Martin's avatar
Derek Martin committed
605
	  else if (flags & MUTT_COMMAND)
Thomas Roessler's avatar
Thomas Roessler committed
606
	  {
607
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
608
	    i = strlen (buf);
609
	    if (i && buf[i - 1] == '=' &&
610
		mutt_var_value_complete (buf, buflen, i))
611 612
	      state->tabs = 0;
	    else if (!mutt_command_complete (buf, buflen, i, state->tabs))
613
	      BEEP ();
614
	    replace_part (state, 0, buf);
Thomas Roessler's avatar
Thomas Roessler committed
615
	  }
Derek Martin's avatar
Derek Martin committed
616
	  else if (flags & (MUTT_FILE | MUTT_EFILE))
Thomas Roessler's avatar
Thomas Roessler committed
617
	  {
618
	    my_wcstombs (buf, buflen, state->wbuf, state->curpos);
Thomas Roessler's avatar
Thomas Roessler committed
619 620

	    /* see if the path has changed from the last time */
621
	    if ((!tempbuf && !state->lastchar) || (tempbuf && templen == state->lastchar &&
622
		!memcmp (tempbuf, state->wbuf, state->lastchar * sizeof (wchar_t))))
Thomas Roessler's avatar
Thomas Roessler committed
623
	    {
624
	      _mutt_select_file (buf, buflen, 
Derek Martin's avatar
Derek Martin committed
625
				 ((flags & MUTT_EFILE) ? MUTT_SEL_FOLDER : 0) | (multiple ? MUTT_SEL_MULTI : 0), 
626
				 files, numfiles);
Thomas Roessler's avatar
Thomas Roessler committed
627
	      set_option (OPTNEEDREDRAW);
628
	      if (*buf)
Thomas Roessler's avatar
Thomas Roessler committed
629
	      {
630
		mutt_pretty_mailbox (buf, buflen);
631
		if (!pass)
632
		  mutt_history_add (hclass, buf, 1);
633 634
		rv = 0;
		goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
635
	      }
636 637

	      /* file selection cancelled */
638 639
	      rv = 1;
	      goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
640 641
	    }

642 643
	    if (!mutt_complete (buf, buflen))
	    {
644
	      templen = state->lastchar;
645
	      safe_realloc (&tempbuf, templen * sizeof (wchar_t));
646
	      memcpy (tempbuf, state->wbuf, templen * sizeof (wchar_t));
647
	    }
Thomas Roessler's avatar
Thomas Roessler committed
648 649
	    else
	      BEEP (); /* let the user know that nothing matched */
650
	    replace_part (state, 0, buf);
Thomas Roessler's avatar
Thomas Roessler committed
651 652 653 654 655 656
	  }
	  else
	    goto self_insert;
	  break;

	case OP_EDITOR_QUOTE_CHAR:
657
	  {
658 659 660
	    event_t event;
	    /*ADDCH (LastKey);*/
	    event = mutt_getch ();
661
	    if (event.ch >= 0)
662 663 664 665
	    {
	      LastKey = event.ch;
	      goto self_insert;
	    }
666 667
	  }

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	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
686 687 688 689 690 691
	default:
	  BEEP ();
      }
    }
    else
    {
692
      
Thomas Roessler's avatar
Thomas Roessler committed
693 694
self_insert:

695
      state->tabs = 0;
Thomas Roessler's avatar
Thomas Roessler committed
696 697 698
      /* use the raw keypress */
      ch = LastKey;

Thomas Roessler's avatar
Thomas Roessler committed
699 700 701 702 703 704 705
#ifdef KEY_ENTER
      /* treat ENTER the same as RETURN */
      if (ch == KEY_ENTER)
	ch = '\r';
#endif

      /* quietly ignore all other function keys */
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
      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;
	}
      }

Derek Martin's avatar
Derek Martin committed
725
      if (first && (flags & MUTT_CLEAR))
Thomas Roessler's avatar
Thomas Roessler committed
726 727
      {
	first = 0;
728
	if (IsWPrint (wc)) /* why? */
729
	  state->curpos = state->lastchar = 0;
Thomas Roessler's avatar
Thomas Roessler committed
730 731
      }

732
      if (wc == '\r' || wc == '\n')
Thomas Roessler's avatar
Thomas Roessler committed
733
      {
734
	/* Convert from wide characters */
735
	my_wcstombs (buf, buflen, state->wbuf, state->lastchar);
Thomas Roessler's avatar
Thomas Roessler committed
736
	if (!pass)
737
	  mutt_history_add (hclass, buf, 1);
738

739 740 741 742
	if (multiple)
	{
	  char **tfiles;
	  *numfiles = 1;
743
	  tfiles = safe_calloc (*numfiles, sizeof (char *));
744 745
	  mutt_expand_path (buf, buflen);
	  tfiles[0] = safe_strdup (buf);
746 747
	  *files = tfiles;
	}
748 749
	rv = 0; 
	goto bye;
Thomas Roessler's avatar
Thomas Roessler committed
750
      }
751
      else if (wc && (wc < ' ' || IsWPrint (wc))) /* why? */
Thomas Roessler's avatar
Thomas Roessler committed
752
      {
753
	if (state->lastchar >= state->wbuflen)
Thomas Roessler's avatar
Thomas Roessler committed
754
	{
755
	  state->wbuflen = state->lastchar + 20;
756
	  safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
Thomas Roessler's avatar
Thomas Roessler committed
757
	}
758
	memmove (state->wbuf + state->curpos + 1, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
759
	state->wbuf[state->curpos++] = wc;
760
	state->lastchar++;
Thomas Roessler's avatar
Thomas Roessler committed
761 762 763 764 765 766 767 768
      }
      else
      {
	mutt_flushinp ();
	BEEP ();
      }
    }
  }
769 770 771
  
  bye:
  
772
  mutt_reset_history_state (hclass);
773
  FREE (&tempbuf);
774 775 776 777 778 779 780
  return rv;
}

void mutt_free_enter_state (ENTER_STATE **esp)
{
  if (!esp) return;
  
781
  FREE (&(*esp)->wbuf);
782
  FREE (esp);		/* __FREE_CHECKED__ */
Thomas Roessler's avatar
Thomas Roessler committed
783
}
784 785 786 787 788 789 790

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