winio.c 101 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
/**************************************************************************
 *   winio.c                                                              *
 *                                                                        *
5
6
 *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007   *
 *   Free Software Foundation, Inc.                                       *
Chris Allegretta's avatar
Chris Allegretta committed
7
8
 *   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 *
9
 *   the Free Software Foundation; either version 3, or (at your option)  *
Chris Allegretta's avatar
Chris Allegretta committed
10
11
 *   any later version.                                                   *
 *                                                                        *
12
13
14
15
 *   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.                             *
Chris Allegretta's avatar
Chris Allegretta committed
16
17
18
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
19
20
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
 *   02110-1301, USA.                                                     *
Chris Allegretta's avatar
Chris Allegretta committed
21
22
23
 *                                                                        *
 **************************************************************************/

24
#include "proto.h"
25

26
#include <stdio.h>
Chris Allegretta's avatar
Chris Allegretta committed
27
28
#include <stdarg.h>
#include <string.h>
29
#include <unistd.h>
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
30
#include <ctype.h>
Chris Allegretta's avatar
Chris Allegretta committed
31

32
static int *key_buffer = NULL;
33
34
	/* The keystroke buffer, containing all the keystrokes we
	 * haven't handled yet at a given point. */
35
static size_t key_buffer_len = 0;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
36
	/* The length of the keystroke buffer. */
37
static int statusblank = 0;
38
39
	/* The number of keystrokes left after we call statusbar(),
	 * before we actually blank the statusbar. */
40
static bool disable_cursorpos = FALSE;
41
42
	/* Should we temporarily disable constant cursor position
	 * display? */
43

44
45
46
47
48
49
50
51
52
53
54
55
/* Control character compatibility:
 *
 * - NANO_BACKSPACE_KEY is Ctrl-H, which is Backspace under ASCII, ANSI,
 *   VT100, and VT220.
 * - NANO_TAB_KEY is Ctrl-I, which is Tab under ASCII, ANSI, VT100,
 *   VT220, and VT320.
 * - NANO_ENTER_KEY is Ctrl-M, which is Enter under ASCII, ANSI, VT100,
 *   VT220, and VT320.
 * - NANO_XON_KEY is Ctrl-Q, which is XON under ASCII, ANSI, VT100,
 *   VT220, and VT320.
 * - NANO_XOFF_KEY is Ctrl-S, which is XOFF under ASCII, ANSI, VT100,
 *   VT220, and VT320.
56
 * - NANO_CONTROL_8 is Ctrl-8 (Ctrl-?), which is Delete under ASCII,
57
58
 *   ANSI, VT100, and VT220, and which is Backspace under VT320.
 *
59
 * Note: VT220 and VT320 also generate Esc [ 3 ~ for Delete.  By
60
61
 * default, xterm assumes it's running on a VT320 and generates Ctrl-8
 * (Ctrl-?) for Backspace and Esc [ 3 ~ for Delete.  This causes
62
 * problems for VT100-derived terminals such as the FreeBSD console,
63
 * which expect Ctrl-H for Backspace and Ctrl-8 (Ctrl-?) for Delete, and
64
65
66
67
68
69
70
71
72
 * on which the VT320 sequences are translated by the keypad to KEY_DC
 * and [nothing].  We work around this conflict via the REBIND_DELETE
 * flag: if it's not set, we assume VT320 compatibility, and if it is,
 * we assume VT100 compatibility.  Thanks to Lee Nelson and Wouter van
 * Hemel for helping work this conflict out.
 *
 * Escape sequence compatibility:
 *
 * We support escape sequences for ANSI, VT100, VT220, VT320, the Linux
73
74
75
 * console, the FreeBSD console, the Mach console, xterm, rxvt, Eterm,
 * and Terminal.  Among these, there are several conflicts and
 * omissions, outlined as follows:
76
77
78
79
80
81
 *
 * - Tab on ANSI == PageUp on FreeBSD console; the former is omitted.
 *   (Ctrl-I is also Tab on ANSI, which we already support.)
 * - PageDown on FreeBSD console == Center (5) on numeric keypad with
 *   NumLock off on Linux console; the latter is omitted.  (The editing
 *   keypad key is more important to have working than the numeric
82
 *   keypad key, because the latter has no value when NumLock is off.)
83
84
85
86
 * - F1 on FreeBSD console == the mouse key on xterm/rxvt/Eterm; the
 *   latter is omitted.  (Mouse input will only work properly if the
 *   extended keypad value KEY_MOUSE is generated on mouse events
 *   instead of the escape sequence.)
87
 * - F9 on FreeBSD console == PageDown on Mach console; the former is
88
89
90
 *   omitted.  (The editing keypad is more important to have working
 *   than the function keys, because the functions of the former are not
 *   arbitrary and the functions of the latter are.)
91
 * - F10 on FreeBSD console == PageUp on Mach console; the former is
92
 *   omitted.  (Same as above.)
93
 * - F13 on FreeBSD console == End on Mach console; the former is
94
 *   omitted.  (Same as above.)
95
96
97
98
99
100
 * - F15 on FreeBSD console == Shift-Up on rxvt/Eterm; the former is
 *   omitted.  (The arrow keys, with or without modifiers, are more
 *   important to have working than the function keys, because the
 *   functions of the former are not arbitrary and the functions of the
 *   latter are.)
 * - F16 on FreeBSD console == Shift-Down on rxvt/Eterm; the former is
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
101
 *   omitted.  (Same as above.) */
102

103
/* Read in a sequence of keystrokes from win and save them in the
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
104
105
 * keystroke buffer.  This should only be called when the keystroke
 * buffer is empty. */
106
void get_key_buffer(WINDOW *win)
107
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
108
109
    int input;
    size_t errcount;
110
111
112
113
114
115

    /* If the keystroke buffer isn't empty, get out. */
    if (key_buffer != NULL)
	return;

    /* Read in the first character using blocking input. */
116
#ifndef NANO_TINY
117
118
    allow_pending_sigwinch(TRUE);
#endif
119

120
121
122
123
    /* Just before reading in the first character, display any pending
     * screen updates. */
    doupdate();

124
    errcount = 0;
125
    while ((input = wgetch(win)) == ERR) {
126
127
	errcount++;

128
129
	/* If we've failed to get a character MAX_BUF_SIZE times in a
	 * row, assume that the input source we were using is gone and
130
131
132
	 * die gracefully.  We could check if errno is set to EIO
	 * ("Input/output error") and die gracefully in that case, but
	 * it's not always set properly.  Argh. */
133
	if (errcount == MAX_BUF_SIZE)
134
135
	    handle_hupterm(0);
    }
136

137
#ifndef NANO_TINY
138
139
    allow_pending_sigwinch(FALSE);
#endif
140

141
142
    /* Increment the length of the keystroke buffer, and save the value
     * of the keystroke at the end of it. */
143
    key_buffer_len++;
144
145
    key_buffer = (int *)nmalloc(sizeof(int));
    key_buffer[0] = input;
146

147
148
149
150
    /* Read in the remaining characters using non-blocking input. */
    nodelay(win, TRUE);

    while (TRUE) {
151
#ifndef NANO_TINY
152
	allow_pending_sigwinch(TRUE);
153
#endif
154

155
	input = wgetch(win);
156

157
	/* If there aren't any more characters, stop reading. */
158
	if (input == ERR)
159
160
	    break;

161
162
	/* Otherwise, increment the length of the keystroke buffer, and
	 * save the value of the keystroke at the end of it. */
163
	key_buffer_len++;
164
165
166
	key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
		sizeof(int));
	key_buffer[key_buffer_len - 1] = input;
167

168
#ifndef NANO_TINY
169
170
	allow_pending_sigwinch(FALSE);
#endif
171
172
173
174
    }

    /* Switch back to non-blocking input. */
    nodelay(win, FALSE);
175
176

#ifdef DEBUG
177
    fprintf(stderr, "get_key_buffer(): key_buffer_len = %lu\n", (unsigned long)key_buffer_len);
178
#endif
179
}
180

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
181
/* Return the length of the keystroke buffer. */
182
size_t get_key_buffer_len(void)
183
184
185
186
{
    return key_buffer_len;
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
187
/* Add the keystrokes in input to the keystroke buffer. */
188
void unget_input(int *input, size_t input_len)
189
{
190
#ifndef NANO_TINY
191
192
    allow_pending_sigwinch(TRUE);
    allow_pending_sigwinch(FALSE);
193
#endif
194

195
    /* If input is empty, get out. */
196
    if (input_len == 0)
197
198
	return;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
199
200
    /* If adding input would put the keystroke buffer beyond maximum
     * capacity, only add enough of input to put it at maximum
201
     * capacity. */
202
203
    if (key_buffer_len + input_len < key_buffer_len)
	input_len = (size_t)-1 - key_buffer_len;
204

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
205
206
207
    /* Add the length of input to the length of the keystroke buffer,
     * and reallocate the keystroke buffer so that it has enough room
     * for input. */
208
209
210
    key_buffer_len += input_len;
    key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
	sizeof(int));
211

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
212
213
    /* If the keystroke buffer wasn't empty before, move its beginning
     * forward far enough so that we can add input to its beginning. */
214
215
216
    if (key_buffer_len > input_len)
	memmove(key_buffer + input_len, key_buffer,
		(key_buffer_len - input_len) * sizeof(int));
217

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
218
    /* Copy input to the beginning of the keystroke buffer. */
219
    memcpy(key_buffer, input, input_len * sizeof(int));
220
221
}

222
223
224
225
/* Put back the character stored in kbinput, putting it in byte range
 * beforehand.  If meta_key is TRUE, put back the Escape character after
 * putting back kbinput.  If func_key is TRUE, put back the function key
 * (a value outside byte range) without putting it in byte range. */
226
227
void unget_kbinput(int kbinput, bool meta_key, bool func_key)
{
228
229
    if (!func_key)
	kbinput = (char)kbinput;
230

231
    unget_input(&kbinput, 1);
232
233

    if (meta_key) {
234
235
	kbinput = NANO_CONTROL_3;
	unget_input(&kbinput, 1);
236
237
238
    }
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
239
240
241
242
243
/* Try to read input_len characters from the keystroke buffer.  If the
 * keystroke buffer is empty and win isn't NULL, try to read in more
 * characters from win and add them to the keystroke buffer before doing
 * anything else.  If the keystroke buffer is empty and win is NULL,
 * return NULL. */
244
int *get_input(WINDOW *win, size_t input_len)
245
{
246
    int *input;
247

248
#ifndef NANO_TINY
249
    allow_pending_sigwinch(TRUE);
250
251
252
    allow_pending_sigwinch(FALSE);
#endif

253
    if (key_buffer_len == 0) {
254
	if (win != NULL) {
255
	    get_key_buffer(win);
256

257
258
259
	    if (key_buffer_len == 0)
		return NULL;
	} else
260
261
262
	    return NULL;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
263
264
    /* If input_len is greater than the length of the keystroke buffer,
     * only read the number of characters in the keystroke buffer. */
265
266
267
    if (input_len > key_buffer_len)
	input_len = key_buffer_len;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
268
269
270
    /* Subtract input_len from the length of the keystroke buffer, and
     * allocate input so that it has enough room for input_len
     * keystrokes. */
271
    key_buffer_len -= input_len;
272
    input = (int *)nmalloc(input_len * sizeof(int));
273

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
274
275
    /* Copy input_len keystrokes from the beginning of the keystroke
     * buffer into input. */
276
    memcpy(input, key_buffer, input_len * sizeof(int));
277

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
278
    /* If the keystroke buffer is empty, mark it as such. */
279
280
281
    if (key_buffer_len == 0) {
	free(key_buffer);
	key_buffer = NULL;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
282
283
284
    /* If the keystroke buffer isn't empty, move its beginning forward
     * far enough so that the keystrokes in input are no longer at its
     * beginning. */
285
286
    } else {
	memmove(key_buffer, key_buffer + input_len, key_buffer_len *
287
288
289
		sizeof(int));
	key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
		sizeof(int));
290
291
292
    }

    return input;
293
294
}

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Read in a single character.  If it's ignored, swallow it and go on.
 * Otherwise, try to translate it from ASCII, meta key sequences, escape
 * sequences, and/or extended keypad values.  Set meta_key to TRUE when
 * we get a meta key sequence, and set func_key to TRUE when we get an
 * extended keypad value.  Supported extended keypad values consist of
 * [arrow key], Ctrl-[arrow key], Shift-[arrow key], Enter, Backspace,
 * the editing keypad (Insert, Delete, Home, End, PageUp, and PageDown),
 * the function keypad (F1-F16), and the numeric keypad with NumLock
 * off.  Assume nodelay(win) is FALSE. */
int get_kbinput(WINDOW *win, bool *meta_key, bool *func_key)
{
    int kbinput;

    /* Read in a character and interpret it.  Continue doing this until
     * we get a recognized value or sequence. */
310
    while ((kbinput = parse_kbinput(win, meta_key, func_key)) == ERR);
311

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
312
313
314
    /* If we read from the edit window, blank the statusbar if we need
     * to. */
    if (win == edit)
315
316
	check_statusblank();

317
318
319
320
321
322
323
    return kbinput;
}

/* Translate ASCII characters, extended keypad values, and escape
 * sequences into their corresponding key values.  Set meta_key to TRUE
 * when we get a meta key sequence, and set func_key to TRUE when we get
 * a function key.  Assume nodelay(win) is FALSE. */
324
int parse_kbinput(WINDOW *win, bool *meta_key, bool *func_key)
325
{
326
    static int escapes = 0, byte_digits = 0;
327
    int *kbinput, retval = ERR;
328

329
330
    *meta_key = FALSE;
    *func_key = FALSE;
331

332
333
334
    /* Read in a character. */
    while ((kbinput = get_input(win, 1)) == NULL);

335
336
337
338
339
340
341
342
343
344
345
    switch (*kbinput) {
	case ERR:
	    break;
	case NANO_CONTROL_3:
	    /* Increment the escape counter. */
	    escapes++;
	    switch (escapes) {
		case 1:
		    /* One escape: wait for more input. */
		case 2:
		    /* Two escapes: wait for more input. */
346
347
		case 3:
		    /* Three escapes: wait for more input. */
348
349
		    break;
		default:
350
351
352
		    /* More than three escapes: limit the escape counter
		     * to no more than two, and wait for more input. */
		    escapes %= 3;
353
354
355
356
357
	    }
	    break;
	default:
	    switch (escapes) {
		case 0:
358
359
360
		    /* One non-escape: normal input mode.  Save the
		     * non-escape character as the result. */
		    retval = *kbinput;
361
362
		    break;
		case 1:
363
		    /* Reset the escape counter. */
364
		    escapes = 0;
365
		    if (get_key_buffer_len() == 0) {
366
			/* One escape followed by a non-escape, and
367
368
369
370
			 * there aren't any other keystrokes waiting:
			 * meta key sequence mode.  Set meta_key to
			 * TRUE, and save the lowercase version of the
			 * non-escape character as the result. */
371
372
			*meta_key = TRUE;
			retval = tolower(*kbinput);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
373
		    } else
374
			/* One escape followed by a non-escape, and
375
376
377
			 * there are other keystrokes waiting: escape
			 * sequence mode.  Interpret the escape
			 * sequence. */
378
379
			retval = parse_escape_seq_kbinput(win,
				*kbinput);
380
381
		    break;
		case 2:
382
383
384
385
386
387
		    if (get_key_buffer_len() == 0) {
			if (('0' <= *kbinput && *kbinput <= '2' &&
				byte_digits == 0) || ('0' <= *kbinput &&
				*kbinput <= '9' && byte_digits > 0)) {
			    /* Two escapes followed by one or more
			     * decimal digits, and there aren't any
388
389
390
391
392
393
394
395
396
			     * other keystrokes waiting: byte sequence
			     * mode.  If the byte sequence's range is
			     * limited to 2XX (the first digit is in the
			     * '0' to '2' range and it's the first
			     * digit, or it's in the '0' to '9' range
			     * and it's not the first digit), increment
			     * the byte sequence counter and interpret
			     * the digit.  If the byte sequence's range
			     * is not limited to 2XX, fall through. */
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
			    int byte;

			    byte_digits++;
			    byte = get_byte_kbinput(*kbinput);

			    if (byte != ERR) {
				char *byte_mb;
				int byte_mb_len, *seq, i;

				/* If we've read in a complete byte
				 * sequence, reset the escape counter
				 * and the byte sequence counter, and
				 * put back the corresponding byte
				 * value. */
				escapes = 0;
				byte_digits = 0;

				/* Put back the multibyte equivalent of
				 * the byte value. */
				byte_mb = make_mbchar((long)byte,
					&byte_mb_len);

				seq = (int *)nmalloc(byte_mb_len *
					sizeof(int));

				for (i = 0; i < byte_mb_len; i++)
				    seq[i] = (unsigned char)byte_mb[i];

				unget_input(seq, byte_mb_len);

				free(byte_mb);
				free(seq);
			    }
			} else {
			    /* Reset the escape counter. */
432
			    escapes = 0;
433
434
435
436
437
438
			    if (byte_digits == 0)
				/* Two escapes followed by a non-decimal
				 * digit or a decimal digit that would
				 * create a byte sequence greater than
				 * 2XX, we're not in the middle of a
				 * byte sequence, and there aren't any
439
440
441
442
443
				 * other keystrokes waiting: control
				 * character sequence mode.  Interpret
				 * the control sequence and save the
				 * corresponding control character as
				 * the result. */
444
445
446
447
448
449
450
451
452
				retval = get_control_kbinput(*kbinput);
			    else {
				/* If we're in the middle of a byte
				 * sequence, reset the byte sequence
				 * counter and save the character we got
				 * as the result. */
				byte_digits = 0;
				retval = *kbinput;
			    }
453
			}
454
		    } else {
455
			/* Two escapes followed by a non-escape, and
456
457
458
459
			 * there are other keystrokes waiting: combined
			 * meta and escape sequence mode.  Reset the
			 * escape counter, set meta_key to TRUE, and
			 * interpret the escape sequence. */
460
			escapes = 0;
461
			*meta_key = TRUE;
462
463
			retval = parse_escape_seq_kbinput(win,
				*kbinput);
464
		    }
465
		    break;
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
		case 3:
		    /* Reset the escape counter. */
		    escapes = 0;
		    if (get_key_buffer_len() == 0)
			/* Three escapes followed by a non-escape, and
			 * there aren't any other keystrokes waiting:
			 * normal input mode.  Save the non-escape
			 * character as the result. */
			retval = *kbinput;
		    else
			/* Three escapes followed by a non-escape, and
			 * there are other keystrokes waiting: combined
			 * control character and escape sequence mode.
			 * Interpret the escape sequence, and interpret
			 * the result as a control sequence. */
			retval = get_control_kbinput(
				parse_escape_seq_kbinput(win,
				*kbinput));
484
		    break;
485
486
	    }
    }
487

488
    if (retval != ERR) {
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
	switch (retval) {
	    case NANO_CONTROL_8:
		retval = ISSET(REBIND_DELETE) ? NANO_DELETE_KEY :
			NANO_BACKSPACE_KEY;
		break;
	    case KEY_DOWN:
		retval = NANO_NEXTLINE_KEY;
		break;
	    case KEY_UP:
		retval = NANO_PREVLINE_KEY;
		break;
	    case KEY_LEFT:
		retval = NANO_BACK_KEY;
		break;
	    case KEY_RIGHT:
		retval = NANO_FORWARD_KEY;
		break;
#ifdef KEY_HOME
	    /* HP-UX 10-11 doesn't support KEY_HOME. */
	    case KEY_HOME:
		retval = NANO_HOME_KEY;
		break;
#endif
	    case KEY_BACKSPACE:
		retval = NANO_BACKSPACE_KEY;
		break;
	    case KEY_DC:
		retval = ISSET(REBIND_DELETE) ? NANO_BACKSPACE_KEY :
			NANO_DELETE_KEY;
		break;
	    case KEY_IC:
		retval = NANO_INSERTFILE_KEY;
		break;
	    case KEY_NPAGE:
		retval = NANO_NEXTPAGE_KEY;
		break;
	    case KEY_PPAGE:
		retval = NANO_PREVPAGE_KEY;
		break;
	    case KEY_ENTER:
		retval = NANO_ENTER_KEY;
		break;
	    case KEY_A1:	/* Home (7) on numeric keypad with
				 * NumLock off. */
		retval = NANO_HOME_KEY;
		break;
	    case KEY_A3:	/* PageUp (9) on numeric keypad with
				 * NumLock off. */
		retval = NANO_PREVPAGE_KEY;
		break;
	    case KEY_B2:	/* Center (5) on numeric keypad with
				 * NumLock off. */
		retval = ERR;
		break;
	    case KEY_C1:	/* End (1) on numeric keypad with
				 * NumLock off. */
		retval = NANO_END_KEY;
		break;
	    case KEY_C3:	/* PageDown (4) on numeric keypad with
				 * NumLock off. */
		retval = NANO_NEXTPAGE_KEY;
		break;
#ifdef KEY_BEG
	    /* Slang doesn't support KEY_BEG. */
	    case KEY_BEG:	/* Center (5) on numeric keypad with
				 * NumLock off. */
		retval = ERR;
		break;
#endif
558
559
560
561
562
563
#ifdef KEY_CANCEL
	    /* Slang doesn't support KEY_CANCEL. */
	    case KEY_CANCEL:
		retval = NANO_CANCEL_KEY;
		break;
#endif
564
565
566
567
568
569
570
571
572
573
574
575
576
#ifdef KEY_END
	    /* HP-UX 10-11 doesn't support KEY_END. */
	    case KEY_END:
		retval = NANO_END_KEY;
		break;
#endif
#ifdef KEY_SBEG
	    /* Slang doesn't support KEY_SBEG. */
	    case KEY_SBEG:	/* Center (5) on numeric keypad with
				 * NumLock off. */
		retval = ERR;
		break;
#endif
577
578
579
580
581
582
#ifdef KEY_SCANCEL
	    /* Slang doesn't support KEY_SCANCEL. */
	    case KEY_SCANCEL:
		retval = NANO_CANCEL_KEY;
		break;
#endif
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
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
#ifdef KEY_SDC
	    /* Slang doesn't support KEY_SDC. */
	    case KEY_SDC:
		retval = ISSET(REBIND_DELETE) ? NANO_BACKSPACE_KEY :
			NANO_DELETE_KEY;
		break;
#endif
#ifdef KEY_SEND
	    /* HP-UX 10-11 and Slang don't support KEY_SEND. */
	    case KEY_SEND:
		retval = NANO_END_KEY;
		break;
#endif
#ifdef KEY_SHOME
	    /* HP-UX 10-11 and Slang don't support KEY_SHOME. */
	    case KEY_SHOME:
		retval = NANO_HOME_KEY;
		break;
#endif
#ifdef KEY_SIC
	    /* Slang doesn't support KEY_SIC. */
	    case KEY_SIC:
		retval = NANO_INSERTFILE_KEY;
		break;
#endif
#ifdef KEY_SDOWN
	    /* ncurses and Slang don't support KEY_SDOWN. */
	    case KEY_SDOWN:
		retval = NANO_NEXTLINE_KEY;
		break;
#endif
#ifdef KEY_SUP
	    /* ncurses and Slang don't support KEY_SUP. */
	    case KEY_SUP:
		retval = NANO_PREVLINE_KEY;
		break;
#endif
#ifdef KEY_SLEFT
	    /* Slang doesn't support KEY_SLEFT. */
	    case KEY_SLEFT:
		retval = NANO_BACK_KEY;
		break;
#endif
#ifdef KEY_SRIGHT
	    /* Slang doesn't support KEY_SRIGHT. */
	    case KEY_SRIGHT:
		retval = NANO_FORWARD_KEY;
		break;
#endif
#ifdef KEY_SSUSPEND
	    /* Slang doesn't support KEY_SSUSPEND. */
	    case KEY_SSUSPEND:
		retval = NANO_SUSPEND_KEY;
		break;
#endif
#ifdef KEY_SUSPEND
	    /* Slang doesn't support KEY_SUSPEND. */
	    case KEY_SUSPEND:
		retval = NANO_SUSPEND_KEY;
		break;
#endif
#ifdef PDCURSES
	    case KEY_SHIFT_L:
	    case KEY_SHIFT_R:
	    case KEY_CONTROL_L:
	    case KEY_CONTROL_R:
	    case KEY_ALT_L:
	    case KEY_ALT_R:
		retval = ERR;
		break;
#endif
#if !defined(NANO_TINY) && defined(KEY_RESIZE)
	    /* Since we don't change the default SIGWINCH handler when
	     * NANO_TINY is defined, KEY_RESIZE is never generated.
	     * Also, Slang and SunOS 5.7-5.9 don't support
	     * KEY_RESIZE. */
	    case KEY_RESIZE:
		retval = ERR;
		break;
#endif
	}
664

665
	/* If our result is an extended keypad value (i.e. a value
666
	 * outside of byte range), set func_key to TRUE. */
667
668
	if (retval != ERR)
	    *func_key = !is_byte(retval);
669
    }
670
671

#ifdef DEBUG
672
    fprintf(stderr, "parse_kbinput(): kbinput = %d, meta_key = %s, func_key = %s, escapes = %d, byte_digits = %d, retval = %d\n", *kbinput, *meta_key ? "TRUE" : "FALSE", *func_key ? "TRUE" : "FALSE", escapes, byte_digits, retval);
673
674
#endif

675
676
    free(kbinput);

677
    /* Return the result. */
678
679
680
    return retval;
}

681
/* Translate escape sequences, most of which correspond to extended
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
682
 * keypad values, into their corresponding key values.  These sequences
683
684
685
 * are generated when the keypad doesn't support the needed keys.
 * Assume that Escape has already been read in. */
int get_escape_seq_kbinput(const int *seq, size_t seq_len)
686
{
687
    int retval = ERR;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
688

689
    if (seq_len > 1) {
690
	switch (seq[0]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
691
	    case 'O':
692
		switch (seq[1]) {
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
		    case '1':
			if (seq_len >= 3) {
			    switch (seq[2]) {
				case ';':
    if (seq_len >= 4) {
	switch (seq[3]) {
	    case '2':
		if (seq_len >= 5) {
		    switch (seq[4]) {
			case 'A': /* Esc O 1 ; 2 A == Shift-Up on
				   * Terminal. */
			case 'B': /* Esc O 1 ; 2 B == Shift-Down on
				   * Terminal. */
			case 'C': /* Esc O 1 ; 2 C == Shift-Right on
				   * Terminal. */
			case 'D': /* Esc O 1 ; 2 D == Shift-Left on
				   * Terminal. */
			    retval = get_escape_seq_abcd(seq[4]);
			    break;
			case 'P': /* Esc O 1 ; 2 P == F13 on
				   * Terminal. */
			    retval = KEY_F(13);
			    break;
			case 'Q': /* Esc O 1 ; 2 Q == F14 on
				   * Terminal. */
			    retval = KEY_F(14);
			    break;
			case 'R': /* Esc O 1 ; 2 R == F15 on
				   * Terminal. */
			    retval = KEY_F(15);
			    break;
			case 'S': /* Esc O 1 ; 2 S == F16 on
				   * Terminal. */
			    retval = KEY_F(16);
			    break;
		    }
		}
		break;
	    case '5':
		if (seq_len >= 5) {
		    switch (seq[4]) {
			case 'A': /* Esc O 1 ; 5 A == Ctrl-Up on
				   * Terminal. */
			case 'B': /* Esc O 1 ; 5 B == Ctrl-Down on
				   * Terminal. */
			case 'C': /* Esc O 1 ; 5 C == Ctrl-Right on
				   * Terminal. */
			case 'D': /* Esc O 1 ; 5 D == Ctrl-Left on
				   * Terminal. */
			    retval = get_escape_seq_abcd(seq[4]);
			    break;
		    }
		}
		break;
	}
    }
				    break;
			    }
			}
			break;
753
		    case '2':
754
			if (seq_len >= 3) {
755
			    switch (seq[2]) {
756
757
				case 'P': /* Esc O 2 P == F13 on
					   * xterm. */
758
				    retval = KEY_F(13);
759
760
761
				    break;
				case 'Q': /* Esc O 2 Q == F14 on
					   * xterm. */
762
				    retval = KEY_F(14);
763
				    break;
764
765
766
767
768
769
770
771
				case 'R': /* Esc O 2 R == F15 on
					   * xterm. */
				    retval = KEY_F(15);
				    break;
				case 'S': /* Esc O 2 S == F16 on
					   * xterm. */
				    retval = KEY_F(16);
				    break;
772
773
			    }
			}
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
774
			break;
775
776
777
778
779
780
781
		    case 'A': /* Esc O A == Up on VT100/VT320/xterm. */
		    case 'B': /* Esc O B == Down on
			       * VT100/VT320/xterm. */
		    case 'C': /* Esc O C == Right on
			       * VT100/VT320/xterm. */
		    case 'D': /* Esc O D == Left on
			       * VT100/VT320/xterm. */
782
			retval = get_escape_seq_abcd(seq[1]);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
783
			break;
784
785
		    case 'E': /* Esc O E == Center (5) on numeric keypad
			       * with NumLock off on xterm. */
786
			retval = KEY_B2;
787
			break;
788
		    case 'F': /* Esc O F == End on xterm/Terminal. */
789
			retval = NANO_END_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
790
			break;
791
		    case 'H': /* Esc O H == Home on xterm/Terminal. */
792
			retval = NANO_HOME_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
793
			break;
794
		    case 'M': /* Esc O M == Enter on numeric keypad with
795
			       * NumLock off on VT100/VT220/VT320/xterm/
796
			       * rxvt/Eterm. */
797
			retval = NANO_ENTER_KEY;
798
			break;
799
		    case 'P': /* Esc O P == F1 on VT100/VT220/VT320/Mach
800
			       * console. */
801
			retval = KEY_F(1);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
802
			break;
803
		    case 'Q': /* Esc O Q == F2 on VT100/VT220/VT320/Mach
804
			       * console. */
805
			retval = KEY_F(2);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
806
			break;
807
		    case 'R': /* Esc O R == F3 on VT100/VT220/VT320/Mach
808
			       * console. */
809
			retval = KEY_F(3);
810
			break;
811
		    case 'S': /* Esc O S == F4 on VT100/VT220/VT320/Mach
812
			       * console. */
813
			retval = KEY_F(4);
814
			break;
815
		    case 'T': /* Esc O T == F5 on Mach console. */
816
			retval = KEY_F(5);
817
			break;
818
		    case 'U': /* Esc O U == F6 on Mach console. */
819
			retval = KEY_F(6);
820
			break;
821
		    case 'V': /* Esc O V == F7 on Mach console. */
822
			retval = KEY_F(7);
823
			break;
824
		    case 'W': /* Esc O W == F8 on Mach console. */
825
			retval = KEY_F(8);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
826
			break;
827
		    case 'X': /* Esc O X == F9 on Mach console. */
828
			retval = KEY_F(9);
829
			break;
830
		    case 'Y': /* Esc O Y == F10 on Mach console. */
831
			retval = KEY_F(10);
832
833
834
835
			break;
		    case 'a': /* Esc O a == Ctrl-Up on rxvt. */
		    case 'b': /* Esc O b == Ctrl-Down on rxvt. */
		    case 'c': /* Esc O c == Ctrl-Right on rxvt. */
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
836
		    case 'd': /* Esc O d == Ctrl-Left on rxvt. */
837
			retval = get_escape_seq_abcd(seq[1]);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
838
			break;
839
		    case 'j': /* Esc O j == '*' on numeric keypad with
840
			       * NumLock off on VT100/VT220/VT320/xterm/
841
			       * rxvt/Eterm/Terminal. */
842
			retval = '*';
843
844
			break;
		    case 'k': /* Esc O k == '+' on numeric keypad with
845
			       * NumLock off on VT100/VT220/VT320/xterm/
846
			       * rxvt/Eterm/Terminal. */
847
			retval = '+';
848
849
			break;
		    case 'l': /* Esc O l == ',' on numeric keypad with
850
			       * NumLock off on VT100/VT220/VT320/xterm/
851
			       * rxvt/Eterm/Terminal. */
852
			retval = ',';
853
854
			break;
		    case 'm': /* Esc O m == '-' on numeric keypad with
855
			       * NumLock off on VT100/VT220/VT320/xterm/
856
			       * rxvt/Eterm/Terminal. */
857
			retval = '-';
858
859
			break;
		    case 'n': /* Esc O n == Delete (.) on numeric keypad
860
			       * with NumLock off on VT100/VT220/VT320/
861
			       * xterm/rxvt/Eterm/Terminal. */
862
			retval = NANO_DELETE_KEY;
863
864
			break;
		    case 'o': /* Esc O o == '/' on numeric keypad with
865
			       * NumLock off on VT100/VT220/VT320/xterm/
866
			       * rxvt/Eterm/Terminal. */
867
			retval = '/';
868
869
			break;
		    case 'p': /* Esc O p == Insert (0) on numeric keypad
870
			       * with NumLock off on VT100/VT220/VT320/
871
			       * rxvt/Eterm/Terminal. */
872
			retval = NANO_INSERTFILE_KEY;
873
874
			break;
		    case 'q': /* Esc O q == End (1) on numeric keypad
875
			       * with NumLock off on VT100/VT220/VT320/
876
			       * rxvt/Eterm/Terminal. */
877
			retval = NANO_END_KEY;
878
879
			break;
		    case 'r': /* Esc O r == Down (2) on numeric keypad
880
			       * with NumLock off on VT100/VT220/VT320/
881
			       * rxvt/Eterm/Terminal. */
882
			retval = NANO_NEXTLINE_KEY;
883
884
			break;
		    case 's': /* Esc O s == PageDown (3) on numeric
885
			       * keypad with NumLock off on VT100/VT220/
886
			       * VT320/rxvt/Eterm/Terminal. */
887
			retval = NANO_NEXTPAGE_KEY;
888
889
			break;
		    case 't': /* Esc O t == Left (4) on numeric keypad
890
			       * with NumLock off on VT100/VT220/VT320/
891
			       * rxvt/Eterm/Terminal. */
892
			retval = NANO_BACK_KEY;
893
894
			break;
		    case 'u': /* Esc O u == Center (5) on numeric keypad
895
896
			       * with NumLock off on VT100/VT220/VT320/
			       * rxvt/Eterm. */
897
			retval = KEY_B2;
898
899
			break;
		    case 'v': /* Esc O v == Right (6) on numeric keypad
900
			       * with NumLock off on VT100/VT220/VT320/
901
			       * rxvt/Eterm/Terminal. */
902
			retval = NANO_FORWARD_KEY;
903
904
			break;
		    case 'w': /* Esc O w == Home (7) on numeric keypad
905
			       * with NumLock off on VT100/VT220/VT320/
906
			       * rxvt/Eterm/Terminal. */
907
			retval = NANO_HOME_KEY;
908
909
			break;
		    case 'x': /* Esc O x == Up (8) on numeric keypad
910
			       * with NumLock off on VT100/VT220/VT320/
911
			       * rxvt/Eterm/Terminal. */
912
			retval = NANO_PREVLINE_KEY;
913
914
			break;
		    case 'y': /* Esc O y == PageUp (9) on numeric keypad
915
			       * with NumLock off on VT100/VT220/VT320/
916
			       * rxvt/Eterm/Terminal. */
917
			retval = NANO_PREVPAGE_KEY;
918
			break;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
919
920
921
		}
		break;
	    case 'o':
922
		switch (seq[1]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
923
924
925
926
		    case 'a': /* Esc o a == Ctrl-Up on Eterm. */
		    case 'b': /* Esc o b == Ctrl-Down on Eterm. */
		    case 'c': /* Esc o c == Ctrl-Right on Eterm. */
		    case 'd': /* Esc o d == Ctrl-Left on Eterm. */
927
			retval = get_escape_seq_abcd(seq[1]);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
928
929
930
931
			break;
		}
		break;
	    case '[':
932
		switch (seq[1]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
933
		    case '1':
934
			if (seq_len >= 3) {
935
			    switch (seq[2]) {
936
937
				case '1': /* Esc [ 1 1 ~ == F1 on rxvt/
					   * Eterm. */
938
				    retval = KEY_F(1);
939
				    break;
940
941
				case '2': /* Esc [ 1 2 ~ == F2 on rxvt/
					   * Eterm. */
942
				    retval = KEY_F(2);
943
				    break;
944
945
				case '3': /* Esc [ 1 3 ~ == F3 on rxvt/
					   * Eterm. */
946
				    retval = KEY_F(3);
947
				    break;
948
949
				case '4': /* Esc [ 1 4 ~ == F4 on rxvt/
					   * Eterm. */
950
				    retval = KEY_F(4);
951
				    break;
952
953
				case '5': /* Esc [ 1 5 ~ == F5 on xterm/
					   * rxvt/Eterm. */
954
				    retval = KEY_F(5);
955
				    break;
956
				case '7': /* Esc [ 1 7 ~ == F6 on
957
958
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
959
				    retval = KEY_F(6);
960
				    break;
961
				case '8': /* Esc [ 1 8 ~ == F7 on
962
963
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
964
				    retval = KEY_F(7);
965
				    break;
966
				case '9': /* Esc [ 1 9 ~ == F8 on
967
968
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
969
				    retval = KEY_F(8);
970
				    break;
971
				case ';':
972
    if (seq_len >= 4) {
973
	switch (seq[3]) {
974
	    case '2':
975
		if (seq_len >= 5) {
976
		    switch (seq[4]) {
977
978
979
980
981
982
983
984
			case 'A': /* Esc [ 1 ; 2 A == Shift-Up on
				   * xterm. */
			case 'B': /* Esc [ 1 ; 2 B == Shift-Down on
				   * xterm. */
			case 'C': /* Esc [ 1 ; 2 C == Shift-Right on
				   * xterm. */
			case 'D': /* Esc [ 1 ; 2 D == Shift-Left on
				   * xterm. */
985
			    retval = get_escape_seq_abcd(seq[4]);
986
987
988
989
990
			    break;
		    }
		}
		break;
	    case '5':
991
		if (seq_len >= 5) {
992
		    switch (seq[4]) {
993
994
995
996
997
998
999
1000
			case 'A': /* Esc [ 1 ; 5 A == Ctrl-Up on
				   * xterm. */
			case 'B': /* Esc [ 1 ; 5 B == Ctrl-Down on
				   * xterm. */
			case 'C': /* Esc [ 1 ; 5 C == Ctrl-Right on
				   * xterm. */
			case 'D': /* Esc [ 1 ; 5 D == Ctrl-Left on
				   * xterm. */
1001
			    retval = get_escape_seq_abcd(seq[4]);
1002
1003
1004
1005
1006
1007
1008
			    break;
		    }
		}
		break;
	}
    }
				    break;
1009
1010
				default: /* Esc [ 1 ~ == Home on
					  * VT320/Linux console. */
1011
				    retval = NANO_HOME_KEY;
1012
1013
1014
1015
1016
				    break;
			    }
			}
			break;
		    case '2':
1017
			if (seq_len >= 3) {
1018
			    switch (seq[2]) {
1019
				case '0': /* Esc [ 2 0 ~ == F9 on
1020
1021
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
1022
				    retval = KEY_F(9);
1023
				    break;
1024
				case '1': /* Esc [ 2 1 ~ == F10 on
1025
1026
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
1027
				    retval = KEY_F(10);
1028
				    break;
1029
				case '3': /* Esc [ 2 3 ~ == F11 on
1030
1031
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
1032
				    retval = KEY_F(11);
1033
				    break;
1034
				case '4': /* Esc [ 2 4 ~ == F12 on
1035
1036
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
1037
				    retval = KEY_F(12);
1038
				    break;
1039
				case '5': /* Esc [ 2 5 ~ == F13 on
1040
1041
					   * VT220/VT320/Linux console/
					   * rxvt/Eterm. */
1042
				    retval = KEY_F(13);
1043
				    break;
1044
				case '6': /* Esc [ 2 6 ~ == F14 on
1045
1046
					   * VT220/VT320/Linux console/
					   * rxvt/Eterm. */
1047
				    retval = KEY_F(14);
1048
				    break;
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
				case '8': /* Esc [ 2 8 ~ == F15 on
					   * VT220/VT320/Linux console/
					   * rxvt/Eterm. */
				    retval = KEY_F(15);
				    break;
				case '9': /* Esc [ 2 9 ~ == F16 on
					   * VT220/VT320/Linux console/
					   * rxvt/Eterm. */
				    retval = KEY_F(16);
				    break;
1059
				default: /* Esc [ 2 ~ == Insert on
1060
					  * VT220/VT320/Linux console/
1061
					  * xterm/Terminal. */
1062
				    retval = NANO_INSERTFILE_KEY;
1063
1064
				    break;
			    }
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1065
1066
			}
			break;
1067
		    case '3': /* Esc [ 3 ~ == Delete on VT220/VT320/
1068
			       * Linux console/xterm/Terminal. */
1069
			retval = NANO_DELETE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1070
			break;
1071
		    case '4': /* Esc [ 4 ~ == End on VT220/VT320/Linux
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1072
			       * console/xterm. */
1073
			retval = NANO_END_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1074
			break;
1075
		    case '5': /* Esc [ 5 ~ == PageUp on VT220/VT320/
1076
1077
			       * Linux console/xterm/Terminal;
			       * Esc [ 5 ^ == PageUp on Eterm. */
1078
			retval = NANO_PREVPAGE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1079
			break;
1080
		    case '6': /* Esc [ 6 ~ == PageDown on VT220/VT320/
1081
1082
			       * Linux console/xterm/Terminal;
			        * Esc [ 6 ^ == PageDown on Eterm. */
1083
			retval = NANO_NEXTPAGE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1084
1085
			break;
		    case '7': /* Esc [ 7 ~ == Home on rxvt. */
1086
			retval = NANO_HOME_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1087
1088
			break;
		    case '8': /* Esc [ 8 ~ == End on rxvt. */
1089
			retval = NANO_END_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1090
			break;
1091
		    case '9': /* Esc [ 9 == Delete on Mach console. */
1092
			retval = NANO_DELETE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1093
			break;
1094
		    case '@': /* Esc [ @ == Insert on Mach console. */
1095
			retval = NANO_INSERTFILE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1096
			break;
1097
		    case 'A': /* Esc [ A == Up on ANSI/VT220/Linux
1098
			       * console/FreeBSD console/Mach console/
1099
			       * rxvt/Eterm/Terminal. */
1100
		    case 'B': /* Esc [ B == Down on ANSI/VT220/Linux
1101
			       * console/FreeBSD console/Mach console/
1102
			       * rxvt/Eterm/Terminal. */
1103
		    case 'C': /* Esc [ C == Right on ANSI/VT220/Linux
1104
			       * console/FreeBSD console/Mach console/
1105
			       * rxvt/Eterm/Terminal. */
1106
		    case 'D': /* Esc [ D == Left on ANSI/VT220/Linux
1107
			       * console/FreeBSD console/Mach console/
1108
			       * rxvt/Eterm/Terminal. */
1109
			retval = get_escape_seq_abcd(seq[1]);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1110
			break;
1111
		    case 'E': /* Esc [ E == Center (5) on numeric keypad
1112
1113
			       * with NumLock off on FreeBSD console/
			       * Terminal. */
1114
			retval = KEY_B2;
1115
			break;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1116
1117
		    case 'F': /* Esc [ F == End on FreeBSD
			       * console/Eterm. */
1118
			retval = NANO_END_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1119
1120
			break;
		    case 'G': /* Esc [ G == PageDown on FreeBSD
1121
			       * console. */
1122
			retval = NANO_NEXTPAGE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1123
			break;
1124
		    case 'H': /* Esc [ H == Home on ANSI/VT220/FreeBSD
1125
			       * console/Mach console/Eterm. */
1126
			retval = NANO_HOME_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1127
1128
1129
			break;
		    case 'I': /* Esc [ I == PageUp on FreeBSD
			       * console. */
1130
			retval = NANO_PREVPAGE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1131
			break;
1132
		    case 'L': /* Esc [ L == Insert on ANSI/FreeBSD
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1133
			       * console. */
1134
			retval = NANO_INSERTFILE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1135
			break;
1136
		    case 'M': /* Esc [ M == F1 on FreeBSD console. */
1137
			retval = KEY_F(1);
1138
1139
			break;
		    case 'N': /* Esc [ N == F2 on FreeBSD console. */
1140
			retval = KEY_F(2);
1141
1142
			break;
		    case 'O':
1143
			if (seq_len >= 3) {
1144
			    switch (seq[2]) {
1145
1146
				case 'P': /* Esc [ O P == F1 on
					   * xterm. */
1147
				    retval = KEY_F(1);
1148
1149
1150
				    break;
				case 'Q': /* Esc [ O Q == F2 on
					   * xterm. */
1151
				    retval = KEY_F(2);
1152
1153
1154
				    break;
				case 'R': /* Esc [ O R == F3 on
					   * xterm. */
1155
				    retval = KEY_F(3);
1156
1157
1158
				    break;
				case 'S': /* Esc [ O S == F4 on
					   * xterm. */
1159
				    retval = KEY_F(4);
1160
1161
				    break;
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1162
			} else
1163
1164
			    /* Esc [ O == F3 on FreeBSD console. */
			    retval = KEY_F(3);
1165
1166
			break;
		    case 'P': /* Esc [ P == F4 on FreeBSD console. */
1167
			retval = KEY_F(4);
1168
1169
			break;
		    case 'Q': /* Esc [ Q == F5 on FreeBSD console. */
1170
			retval = KEY_F(5);
1171
1172
			break;
		    case 'R': /* Esc [ R == F6 on FreeBSD console. */
1173
			retval = KEY_F(6);
1174
1175
			break;
		    case 'S': /* Esc [ S == F7 on FreeBSD console. */
1176
			retval = KEY_F(7);
1177
1178
			break;
		    case 'T': /* Esc [ T == F8 on FreeBSD console. */
1179
			retval = KEY_F(8);
1180
			break;
1181
		    case 'U': /* Esc [ U == PageDown on Mach console. */
1182
			retval = NANO_NEXTPAGE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1183
			break;
1184
		    case 'V': /* Esc [ V == PageUp on Mach console. */
1185
			retval = NANO_PREVPAGE_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1186
			break;
1187
		    case 'W': /* Esc [ W == F11 on FreeBSD console. */
1188
			retval = KEY_F(11);
1189
1190
			break;
		    case 'X': /* Esc [ X == F12 on FreeBSD console. */
1191
			retval = KEY_F(12);
1192
			break;
1193
		    case 'Y': /* Esc [ Y == End on Mach console. */
1194
			retval = NANO_END_KEY;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1195
			break;
1196
		    case 'Z': /* Esc [ Z == F14 on FreeBSD console. */
1197
			retval = KEY_F(14);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1198
			break;
1199
		    case 'a': /* Esc [ a == Shift-Up on rxvt/Eterm. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1200
		    case 'b': /* Esc [ b == Shift-Down on rxvt/Eterm. */
1201
1202
		    case 'c': /* Esc [ c == Shift-Right on rxvt/
			       * Eterm. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1203
		    case 'd': /* Esc [ d == Shift-Left on rxvt/Eterm. */
1204
			retval = get_escape_seq_abcd(seq[1]);
1205
1206
			break;
		    case '[':
1207
			if (seq_len >= 3) {
1208
			    switch (seq[2]) {
1209
1210
				case 'A': /* Esc [ [ A == F1 on Linux
					   * console. */
1211
				    retval = KEY_F(1);
1212
1213
1214
				    break;
				case 'B': /* Esc [ [ B == F2 on Linux
					   * console. */
1215
				    retval = KEY_F(2);
1216
1217
1218
				    break;
				case 'C': /* Esc [ [ C == F3 on Linux
					   * console. */
1219
				    retval = KEY_F(3);
1220
1221
1222
				    break;
				case 'D': /* Esc [ [ D == F4 on Linux
					   * console. */
1223
				    retval = KEY_F(4);
1224
1225
1226
				    break;
				case 'E': /* Esc [ [ E == F5 on Linux
					   * console. */
1227
				    retval = KEY_F(5);
1228
1229
1230
				    break;
			    }
			}
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1231
1232
1233
1234
			break;
		}
		break;
	}
1235
1236
    }

1237
#ifdef DEBUG
1238
    fprintf(stderr, "get_escape_seq_kbinput(): retval = %d\n", retval);
1239
#endif
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1240

1241
    return retval;
1242
1243
}

1244
/* Return the equivalent arrow key value for the case-insensitive
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1245
 * letters A (up), B (down), C (right), and D (left).  These are common
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
 * to many escape sequences. */
int get_escape_seq_abcd(int kbinput)
{
    switch (tolower(kbinput)) {
	case 'a':
	    return NANO_PREVLINE_KEY;
	case 'b':
	    return NANO_NEXTLINE_KEY;
	case 'c':
	    return NANO_FORWARD_KEY;
	case 'd':
	    return NANO_BACK_KEY;
	default:
	    return ERR;
    }
}

1263
/* Interpret the escape sequence in the keystroke buffer, the first
1264
1265
1266
 * character of which is kbinput.  Assume that the keystroke buffer
 * isn't empty, and that the initial escape has already been read in. */
int parse_escape_seq_kbinput(WINDOW *win, int kbinput)
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
{
    int retval, *seq;
    size_t seq_len;

    /* Put back the non-escape character, get the complete escape
     * sequence, translate the sequence into its corresponding key
     * value, and save that as the result. */
    unget_input(&kbinput, 1);
    seq_len = get_key_buffer_len();
    seq = get_input(NULL, seq_len);
1277
    retval = get_escape_seq_kbinput(seq, seq_len);
1278
1279
1280

    free(seq);

1281
1282
    /* If we got an unrecognized escape sequence, throw it out. */
    if (retval == ERR) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1283
	if (win == edit) {
1284
1285
1286
1287
1288
	    statusbar(_("Unknown Command"));
	    beep();
	}
    }

1289
#ifdef DEBUG
1290
    fprintf(stderr, "parse_escape_seq_kbinput(): kbinput = %d, seq_len = %lu, retval = %d\n", kbinput, (unsigned long)seq_len, retval);
1291
1292
1293
1294
1295
#endif

    return retval;
}

1296
1297
/* Translate a byte sequence: turn a three-digit decimal number (from
 * 000 to 255) into its corresponding byte value. */
1298
int get_byte_kbinput(int kbinput)
1299
{
1300
    static int byte_digits = 0, byte = 0;
1301
    int retval = ERR;
1302

1303
1304
    /* Increment the byte digit counter. */
    byte_digits++;
1305

1306
    switch (byte_digits) {
1307
	case 1:
1308
1309
	    /* First digit: This must be from zero to two.  Put it in
	     * the 100's position of the byte sequence holder. */
1310
	    if ('0' <= kbinput && kbinput <= '2')
1311
		byte = (kbinput - '0') * 100;
1312
	    else
1313
1314
		/* This isn't the start of a byte sequence.  Return this
		 * character as the result. */
1315
1316
1317
		retval = kbinput;
	    break;
	case 2:
1318
1319
1320
1321
	    /* Second digit: This must be from zero to five if the first
	     * was two, and may be any decimal value if the first was
	     * zero or one.  Put it in the 10's position of the byte
	     * sequence holder. */
1322
1323
1324
	    if (('0' <= kbinput && kbinput <= '5') || (byte < 200 &&
		'6' <= kbinput && kbinput <= '9'))
		byte += (kbinput - '0') * 10;
1325
	    else
1326
1327
		/* This isn't the second digit of a byte sequence.
		 * Return this character as the result. */
1328
1329
1330
		retval = kbinput;
	    break;
	case 3:
1331
1332
1333
1334
1335
	    /* Third digit: This must be from zero to five if the first
	     * was two and the second was between zero and five, and may
	     * be any decimal value if the first was zero or one and the
	     * second was between six and nine.  Put it in the 1's
	     * position of the byte sequence holder. */
1336
1337
	    if (('0' <= kbinput && kbinput <= '5') || (byte < 250 &&
		'6' <= kbinput && kbinput <= '9')) {
1338
		byte += kbinput - '0';
1339
1340
		/* If this character is a valid decimal value, then the
		 * byte sequence is complete. */
1341
		retval = byte;
1342
	    } else
1343
1344
		/* This isn't the third digit of a byte sequence.
		 * Return this character as the result. */
1345
1346
		retval = kbinput;
	    break;
1347
	default:
1348
1349
1350
	    /* If there are more than three digits, return this
	     * character as the result.  (Maybe we should produce an
	     * error instead?) */
1351
1352
1353
1354
1355
1356
1357
1358
	    retval = kbinput;
	    break;
    }

    /* If we have a result, reset the byte digit counter and the byte
     * sequence holder. */
    if (retval != ERR) {
	byte_digits = 0;
1359
	byte = 0;
1360
1361
1362
    }

#ifdef DEBUG
1363
    fprintf(stderr, "get_byte_kbinput(): kbinput = %d, byte_digits = %d, byte = %d, retval = %d\n", kbinput, byte_digits, byte, retval);
1364
1365
1366
1367
1368
#endif

    return retval;
}

1369
#ifdef ENABLE_UTF8
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
/* If the character in kbinput is a valid hexadecimal digit, multiply it
 * by factor and add the result to uni. */
long add_unicode_digit(int kbinput, long factor, long *uni)
{
    long retval = ERR;

    if ('0' <= kbinput && kbinput <= '9')
	*uni += (kbinput - '0') * factor;
    else if ('a' <= tolower(kbinput) && tolower(kbinput) <= 'f')
	*uni += (tolower(kbinput) - 'a' + 10) * factor;
    else
	/* If this character isn't a valid hexadecimal value, save it as
	 * the result. */
	retval = kbinput;

    return retval;
}

1388
/* Translate a Unicode sequence: turn a six-digit hexadecimal number
1389
 * (from 000000 to 10FFFF, case-insensitive) into its corresponding
1390
 * multibyte value. */
1391
long get_unicode_kbinput(int kbinput)
1392
{
1393
1394
1395
    static int uni_digits = 0;
    static long uni = 0;
    long retval = ERR;
1396

1397
    /* Increment the Unicode digit counter. */
1398
    uni_digits++;
1399

1400
    switch (uni_digits) {
1401
	case 1:
1402
1403
	    /* First digit: This must be zero or one.  Put it in the
	     * 0x100000's position of the Unicode sequence holder. */
1404
	    if ('0' <= kbinput && kbinput <= '1')
1405
		uni = (kbinput - '0') * 0x100000;
1406
	    else
1407
1408
		/* This isn't the first digit of a Unicode sequence.
		 * Return this character as the result. */
1409
1410
1411
		retval = kbinput;
	    break;
	case 2:
1412
1413
1414
1415
1416
1417
	    /* Second digit: This must be zero if the first was one, and
	     * may be any hexadecimal value if the first was zero.  Put
	     * it in the 0x10000's position of the Unicode sequence
	     * holder. */
	    if (uni == 0 || '0' == kbinput)
		retval = add_unicode_digit(kbinput, 0x10000, &uni);
1418
	    else
1419
1420
		/* This isn't the second digit of a Unicode sequence.
		 * Return this character as the result. */
1421
1422
1423
		retval = kbinput;
	    break;
	case 3:
1424
1425
1426
1427
	    /* Third digit: This may be any hexadecimal value.  Put it
	     * in the 0x1000's position of the Unicode sequence
	     * holder. */
	    retval = add_unicode_digit(kbinput, 0x1000, &uni);
1428
	    break;
1429
	case 4:
1430
1431
1432
1433
	    /* Fourth digit: This may be any hexadecimal value.  Put it
	     * in the 0x100's position of the Unicode sequence
	     * holder. */
	    retval = add_unicode_digit(kbinput, 0x100, &uni);
1434
	    break;
1435
	case 5:
1436
1437
1438
	    /* Fifth digit: This may be any hexadecimal value.  Put it
	     * in the 0x10's position of the Unicode sequence holder. */
	    retval = add_unicode_digit(kbinput, 0x10, &uni);
1439
	    break;
1440
	case 6:
1441
1442
1443
1444
1445
1446
	    /* Sixth digit: This may be any hexadecimal value.  Put it
	     * in the 0x1's position of the Unicode sequence holder. */
	    retval = add_unicode_digit(kbinput, 0x1, &uni);
	    /* If this character is a valid hexadecimal value, then the
	     * Unicode sequence is complete. */
	    if (retval == ERR)
1447
		retval = uni;
1448
1449
	    break;
	default:
1450
1451
1452
	    /* If there are more than six digits, return this character
	     * as the result.  (Maybe we should produce an error
	     * instead?) */
1453
1454
1455
	    retval = kbinput;
	    break;
    }
1456

1457
1458
    /* If we have a result, reset the Unicode digit counter and the
     * Unicode sequence holder. */
1459
    if (retval != ERR) {
1460
1461
	uni_digits = 0;
	uni = 0;
1462
    }
1463

1464
#ifdef DEBUG
1465
    fprintf(stderr, "get_unicode_kbinput(): kbinput = %d, uni_digits = %d, uni = %ld, retval = %ld\n", kbinput, uni_digits, uni, retval);
1466
1467
#endif

1468
1469
    return retval;
}
1470
#endif /* ENABLE_UTF8 */
1471

1472
1473
1474
1475
1476
1477
/* Translate a control character sequence: turn an ASCII non-control
 * character into its corresponding control character. */
int get_control_kbinput(int kbinput)
{
    int retval;

1478
     /* Ctrl-Space (Ctrl-2, Ctrl-@, Ctrl-`) */
1479
1480
    if (kbinput == ' ' || kbinput == '2')
	retval = NANO_CONTROL_SPACE;
1481
1482
    /* Ctrl-/ (Ctrl-7, Ctrl-_) */
    else if (kbinput == '/')
1483
	retval = NANO_CONTROL_7;
1484
    /* Ctrl-3 (Ctrl-[, Esc) to Ctrl-7 (Ctrl-/, Ctrl-_) */
1485
1486
1487
    else if ('3' <= kbinput && kbinput <= '7')
	retval = kbinput - 24;
    /* Ctrl-8 (Ctrl-?) */
1488
1489
    else if (kbinput == '8' || kbinput == '?')
	retval = NANO_CONTROL_8;
1490
1491
    /* Ctrl-@ (Ctrl-Space, Ctrl-2, Ctrl-`) to Ctrl-_ (Ctrl-/, Ctrl-7) */
    else if ('@' <= kbinput && kbinput <= '_')
1492
	retval = kbinput - '@';
1493
1494
    /* Ctrl-` (Ctrl-2, Ctrl-Space, Ctrl-@) to Ctrl-~ (Ctrl-6, Ctrl-^) */
    else if ('`' <= kbinput && kbinput <= '~')
1495
	retval = kbinput - '`';
1496
1497
1498
    else
	retval = kbinput;

1499
#ifdef DEBUG
1500
    fprintf(stderr, "get_control_kbinput(): kbinput = %d, retval = %d\n", kbinput, retval);
1501
1502
#endif

1503
1504
    return retval;
}
1505

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1506
1507
/* Put the output-formatted characters in output back into the keystroke
 * buffer, so that they can be parsed and displayed as output again. */
1508
void unparse_kbinput(char *output, size_t output_len)
1509
{
1510
1511
    int *input;
    size_t i;
1512

1513
1514
1515
1516
    if (output_len == 0)
	return;

    input = (int *)nmalloc(output_len * sizeof(int));
1517

1518
1519
    for (i = 0; i < output_len; i++)
	input[i] = (int)output[i];
1520

1521
    unget_input(input, output_len);
1522

1523
    free(input);
1524
1525
}

1526
/* Read in a stream of characters verbatim, and return the length of the
1527
1528
1529
1530
 * string in kbinput_len.  Assume nodelay(win) is FALSE. */
int *get_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
{
    int *retval;
1531

1532
    /* Turn off flow control characters if necessary so that we can type
1533
1534
     * them in verbatim, and turn the keypad off if necessary so that we
     * don't get extended keypad values. */
1535
1536
    if (ISSET(PRESERVE))
	disable_flow_control();
1537
1538
    if (!ISSET(REBIND_KEYPAD))
	keypad(win, FALSE);
1539
1540
1541

    /* Read in a stream of characters and interpret it if possible. */
    retval = parse_verbatim_kbinput(win, kbinput_len);
1542
1543

    /* Turn flow control characters back on if necessary and turn the
1544
     * keypad back on if necessary now that we're done. */
1545
1546
    if (ISSET(PRESERVE))
	enable_flow_control();
1547
1548
    if (!ISSET(REBIND_KEYPAD))
	keypad(win, TRUE);
1549

1550
    return retval;
1551
1552
}

1553
1554
/* Read in a stream of all available characters, and return the length
 * of the string in kbinput_len.  Translate the first few characters of
1555
 * the input into the corresponding multibyte value if possible.  After
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1556
 * that, leave the input as-is. */
1557
int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
1558
{
1559
    int *kbinput, *retval;
1560

1561
1562
1563
    /* Read in the first keystroke. */
    while ((kbinput = get_input(win, 1)) == NULL);

1564
#ifdef ENABLE_UTF8
1565
1566
1567
    if (using_utf8()) {
	/* Check whether the first keystroke is a valid hexadecimal
	 * digit. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1568
	long uni = get_unicode_kbinput(*kbinput);
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591

	/* If the first keystroke isn't a valid hexadecimal digit, put
	 * back the first keystroke. */
	if (uni != ERR)
	    unget_input(kbinput, 1);

	/* Otherwise, read in keystrokes until we have a complete
	 * Unicode sequence, and put back the corresponding Unicode
	 * value. */
	else {
	    char *uni_mb;
	    int uni_mb_len, *seq, i;

	    if (win == edit)
		/* TRANSLATORS: This is displayed during the input of a
		 * six-digit hexadecimal Unicode character code. */
		statusbar(_("Unicode Input"));

	    while (uni == ERR) {
		while ((kbinput = get_input(win, 1)) == NULL);

		uni = get_unicode_kbinput(*kbinput);
	    }
1592

1593
1594
1595
	    /* Put back the multibyte equivalent of the Unicode
	     * value. */
	    uni_mb = make_mbchar(uni, &uni_mb_len);
1596

1597
	    seq = (int *)nmalloc(uni_mb_len * sizeof(int));
1598

1599
1600
	    for (i = 0; i < uni_mb_len; i++)
		seq[i] = (unsigned char)uni_mb[i];
1601

1602
	    unget_input(seq, uni_mb_len);
1603

1604
1605
	    free(seq);
	    free(uni_mb);
1606
	}
1607
    } else
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1608
1609
#endif /* ENABLE_UTF8 */

1610
1611
	/* Put back the first keystroke. */
	unget_input(kbinput, 1);
1612

1613
1614
    free(kbinput);

1615
    /* Get the complete sequence, and save the characters in it as the
1616
     * result. */
1617
    *kbinput_len = get_key_buffer_len();
1618
    retval = get_input(NULL, *kbinput_len);
1619
1620
1621
1622

    return retval;
}

1623
#ifndef DISABLE_MOUSE
1624
/* Handle any mouse event that may have occurred.  We currently handle
1625
1626
1627
1628
1629
1630
1631
 * releases/clicks of the first mouse button.  If allow_shortcuts is
 * TRUE, releasing/clicking on a visible shortcut will put back the
 * keystroke associated with that shortcut.  If NCURSES_MOUSE_VERSION is
 * at least 2, we also currently handle presses of the fourth mouse
 * button (upward rolls of the mouse wheel) by putting back the
 * keystrokes to move up, and presses of the fifth mouse button
 * (downward rolls of the mouse wheel) by putting back the keystrokes to
1632
1633
1634
1635
1636
1637
 * move down.  We also store the coordinates of a mouse event that needs
 * to be handled in mouse_x and mouse_y, relative to the entire screen.
 * Return -1 on error, 0 if the mouse event needs to be handled, 1 if
 * it's been handled by putting back keystrokes that need to be handled.
 * or 2 if it's been ignored.  Assume that KEY_MOUSE has already been
 * read in. */
1638
int get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
1639
1640
{
    MEVENT mevent;
1641
    bool in_bottomwin;
1642
1643
1644
1645
1646
1647

    *mouse_x = -1;
    *mouse_y = -1;

    /* First, get the actual mouse event. */
    if (getmouse(&mevent) == ERR)
1648
	return -1;
1649

1650
1651
1652
    /* Save the screen coordinates where the mouse event took place. */
    *mouse_x = mevent.x;
    *mouse_y = mevent.y;
1653

1654
1655
    in_bottomwin = wenclose(bottomwin, *mouse_y, *mouse_x);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1656
    /* Handle releases/clicks of the first mouse button. */
1657
    if (mevent.bstate & (BUTTON1_RELEASED | BUTTON1_CLICKED)) {
1658
1659
	/* If we're allowing shortcuts, the current shortcut list is
	 * being displayed on the last two lines of the screen, and the
1660
1661
1662
	 * first mouse button was released on/clicked inside it, we need
	 * to figure out which shortcut was released on/clicked and put
	 * back the equivalent keystroke(s) for it. */
1663
	if (allow_shortcuts && !ISSET(NO_HELP) && in_bottomwin) {
1664
1665
1666
1667
1668
1669
	    int i;
		/* The width of all the shortcuts, except for the last
		 * two, in the shortcut list in bottomwin. */
	    int j;
		/* The y-coordinate relative to the beginning of the
		 * shortcut list in bottomwin. */
1670
1671
1672
	    size_t currslen;
		/* The number of shortcuts in the current shortcut
		 * list. */
1673
	    const shortcut *s;
1674
		/* The actual shortcut we released on, starting at the
1675
1676
		 * first one in the current shortcut list. */

1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
	    /* Translate the mouse event coordinates so that they're
	     * relative to bottomwin. */
	    wmouse_trafo(bottomwin, mouse_y, mouse_x, FALSE);

	    /* Handle releases/clicks of the first mouse button on the
	     * statusbar elsewhere. */
	    if (*mouse_y == 0) {
		/* Restore the untranslated mouse event coordinates, so
		 * that they're relative to the entire screen again. */
		*mouse_x = mevent.x;
		*mouse_y = mevent.y;

		return 0;
	    }
1691
1692
1693
1694
1695

	    /* Calculate the y-coordinate relative to the beginning of
	     * the shortcut list in bottomwin. */
	    j = *mouse_y - 1;

1696
1697
	    /* Get the shortcut lists' length. */
	    if (currshortcut == main_list)
1698
		currslen = MAIN_VISIBLE;
1699
1700
	    else {
		currslen = length_of_list(currshortcut);
1701

1702
1703
1704
1705
1706
		/* We don't show any more shortcuts than the main list
		 * does. */
		if (currslen > MAIN_VISIBLE)
		    currslen = MAIN_VISIBLE;
	    }
1707

1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
	    /* Calculate the width of all of the shortcuts in the list
	     * except for the last two, which are longer by (COLS % i)
	     * columns so as to not waste space. */
	    if (currslen < 2)
		i = COLS / (MAIN_VISIBLE / 2);
	    else
		i = COLS / ((currslen / 2) + (currslen % 2));

	    /* Calculate the x-coordinate relative to the beginning of
	     * the shortcut list in bottomwin, and add it to j.  j
	     * should now be the index in the shortcut list of the
1719
	     * shortcut we released/clicked on. */
1720
1721
	    j = (*mouse_x / i) * 2 + j;

1722
	    /* Adjust j if we released on the last two shortcuts. */
1723
1724
1725
	    if ((j >= currslen) && (*mouse_x % i < COLS % i))
		j -= 2;

1726
1727
	    /* Ignore releases/clicks of the first mouse button beyond
	     * the last shortcut. */
1728
	    if (j >= currslen)
1729
		return 2;
1730
1731

	    /* Go through the shortcut list to determine which shortcut
1732
	     * we released/clicked on. */
1733
1734
	    s = currshortcut;

1735
1736
1737
1738
1739
1740
1741
1742
	    for (; j > 0; j--)
		s = s->next;

	    /* And put back the equivalent key.  Assume that each
	     * shortcut has, at the very least, an equivalent control
	     * key, an equivalent primary meta key sequence, or both. */
	    if (s->ctrlval != NANO_NO_KEY) {
		unget_kbinput(s->ctrlval, FALSE, FALSE);
1743
		return 1;
1744
1745
	    } else if (s->metaval != NANO_NO_KEY) {
		unget_kbinput(s->metaval, TRUE, FALSE);
1746
		return 1;
1747
1748
	    }
	} else
1749
1750
	    /* Handle releases/clicks of the first mouse button that
	     * aren't on the current shortcut list elsewhere. */
1751
	    return 0;
1752
    }
1753
1754
#if NCURSES_MOUSE_VERSION >= 2
    /* Handle presses of the fourth mouse button (upward rolls of the
1755
1756
1757
     * mouse wheel) and presses of the fifth mouse button (downward
     * rolls of the mouse wheel) . */
    else if (mevent.bstate & (BUTTON4_PRESSED | BUTTON5_PRESSED)) {
1758
	bool in_edit = wenclose(edit, mouse_y, mouse_x);
1759

1760
1761
1762
1763
	if (in_bottomwin)
	    /* Translate the mouse event coordinates so that they're
	     * relative to bottomwin. */
	    wmouse_trafo(bottomwin, mouse_y, mouse_x, FALSE);
1764

1765
	if (in_edit || (in_bottomwin && *mouse_y == 0)) {
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
	    /* One upward roll of the mouse wheel is equivalent to
	     * moving up three lines, and one downward roll of the mouse
	     * wheel is equivalent to moving down three lines. */
	    for (i = 0; i < 3; i++)
		unget_kbinput((mevent.bstate & BUTTON4_PRESSED) ?
			NANO_PREVLINE_KEY : NANO_NEXTLINE_KEY, FALSE,
			FALSE);

	    return 1;
	} else
	    /* Ignore presses of the fourth mouse button and presses of
	     * the fifth mouse buttons that aren't on the edit window or
	     * the statusbar. */
	    return 2;
1780
1781
    }
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1782
1783
1784

    /* Ignore all other mouse events. */
    return 2;
1785
}
1786
1787
#endif /* !DISABLE_MOUSE */

1788
1789
1790
1791
1792
1793
1794
1795
/* Return the shortcut corresponding to the values of kbinput (the key
 * itself), meta_key (whether the key is a meta sequence), and func_key
 * (whether the key is a function key), if any.  The shortcut will be
 * the first one in the list (control key, meta key sequence, function
 * key, other meta key sequence) for the corresponding function.  For
 * example, passing in a meta key sequence that corresponds to a
 * function with a control key, a function key, and a meta key sequence
 * will return the control key corresponding to that function. */
1796
const shortcut *get_shortcut(const shortcut *s_list, int *kbinput, bool
1797
	*meta_key, bool *func_key)
1798
1799
1800
1801
{
    const shortcut *s = s_list;
    size_t slen = length_of_list(s_list);

1802
#ifdef DEBUG
1803
    fprintf(stderr, "get_shortcut(): kbinput = %d, meta_key = %s, func_key = %s\n", *kbinput, *meta_key ? "TRUE" : "FALSE", *func_key ? "TRUE" : "FALSE");
1804
1805
#endif

1806
1807
1808
1809
1810
1811
    /* Check for shortcuts. */
    for (; slen > 0; slen--) {
	/* We've found a shortcut if:
	 *
	 * 1. The key exists.
	 * 2. The key is a control key in the shortcut list.
1812
1813
1814
1815
1816
	 * 3. meta_key is TRUE and the key is the primary or
	 *    miscellaneous meta sequence in the shortcut list.
	 * 4. func_key is TRUE and the key is a function key in the
	 *    shortcut list. */

1817
	if (*kbinput != NANO_NO_KEY && (*kbinput == s->ctrlval ||
1818
1819
1820
		(*meta_key && (*kbinput == s->metaval || *kbinput ==
		s->miscval)) || (*func_key && *kbinput ==
		s->funcval))) {
1821
1822
1823
1824
1825
1826
1827
1828
	    break;
	}

	s = s->next;
    }

    /* Translate the shortcut to either its control key or its meta key
     * equivalent.  Assume that the shortcut has an equivalent control
1829
     * key, an equivalent primary meta key sequence, or both. */
1830
1831
1832
    if (slen > 0) {
	if (s->ctrlval != NANO_NO_KEY) {
	    *meta_key = FALSE;
1833
	    *func_key = FALSE;
1834
	    *kbinput = s->ctrlval;
1835
	    return s;
1836
1837
	} else if (s->metaval != NANO_NO_KEY) {
	    *meta_key = TRUE;
1838
	    *func_key = FALSE;
1839
	    *kbinput = s->metaval;
1840
	    return s;
1841
1842
1843
1844
1845
1846
	}
    }

    return NULL;
}

1847
#ifndef NANO_TINY
1848
1849
1850
/* Return the global toggle corresponding to the values of kbinput (the
 * key itself) and meta_key (whether the key is a meta sequence), if
 * any. */
1851
1852
1853
1854
const toggle *get_toggle(int kbinput, bool meta_key)
{
    const toggle *t = toggles;

1855
#ifdef DEBUG
1856
    fprintf(stderr, "get_toggle(): kbinput = %d, meta_key = %s\n", kbinput, meta_key ? "TRUE" : "FALSE");
1857
1858
#endif

1859
1860
    /* Check for toggles. */
    for (; t != NULL; t = t->next) {
1861
1862
	/* We've found a toggle if the key exists, meta_key is TRUE, and
	 * the key is in the meta key toggle list. */
1863
	if (t->val != TOGGLE_NO_KEY && meta_key && kbinput == t->val)
1864
1865
1866
1867
1868
	    break;
    }

    return t;
}
1869
#endif /* !NANO_TINY */
1870

1871
1872
1873
1874
1875
/* Move to (x, y) in win, and display a line of n spaces with the
 * current attributes. */
void blank_line(WINDOW *win, int y, int x, int n)
{
    wmove(win, y, x);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1876

1877
1878
1879
1880
    for (; n > 0; n--)
	waddch(win, ' ');
}

1881
/* Blank the first line of the top portion of the window. */
1882
void blank_titlebar(void)
Chris Allegretta's avatar
Chris Allegretta committed
1883
{
1884
    blank_line(topwin, 0, 0, COLS);
1885
1886
}

1887
1888
/* If the MORE_SPACE flag isn't set, blank the second line of the top
 * portion of the window. */
1889
1890
1891
void blank_topbar(void)
{
    if (!ISSET(MORE_SPACE))
1892
	blank_line(topwin, 1, 0, COLS);
1893
1894
}

1895
/* Blank all the lines of the middle portion of the window, i.e. the
1896
 * edit window. */
Chris Allegretta's avatar
Chris Allegretta committed
1897
1898
void blank_edit(void)
{
1899
    int i;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1900

1901
    for (i = 0; i < editwinrows; i++)
1902
	blank_line(edit, i, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1903
1904
}

1905
/* Blank the first line of the bottom portion of the window. */
Chris Allegretta's avatar
Chris Allegretta committed
1906
1907
void blank_statusbar(void)
{
1908
    blank_line(bottomwin, 0, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1909
1910
}

1911
1912
/* If the NO_HELP flag isn't set, blank the last two lines of the bottom
 * portion of the window. */
1913
1914
1915
void blank_bottombars(void)
{
    if (!ISSET(NO_HELP)) {
1916
1917
	blank_line(bottomwin, 1, 0, COLS);
	blank_line(bottomwin, 2, 0, COLS);
1918
1919
1920
    }
}

1921
1922
1923
/* Check if the number of keystrokes needed to blank the statusbar has
 * been pressed.  If so, blank the statusbar, unless constant cursor
 * position display is on. */
1924
void check_statusblank(void)
Chris Allegretta's avatar
Chris Allegretta committed
1925
{
1926
    if (statusblank > 0) {
1927
	statusblank--;
1928

1929
1930
1931
1932
1933
1934
	if (statusblank == 0 && !ISSET(CONST_UPDATE)) {
	    blank_statusbar();
	    wnoutrefresh(bottomwin);
	    reset_cursor();
	    wnoutrefresh(edit);
	}
Chris Allegretta's avatar
Chris Allegretta committed
1935
1936
1937
    }
}

1938
1939
1940
1941
/* Convert buf into a string that can be displayed on screen.  The
 * caller wants to display buf starting with column start_col, and
 * extending for at most len columns.  start_col is zero-based.  len is
 * one-based, so len == 0 means you get "" returned.  The returned
1942
1943
1944
1945
1946
 * string is dynamically allocated, and should be freed.  If dollars is
 * TRUE, the caller might put "$" at the beginning or end of the line if
 * it's too long. */
char *display_string(const char *buf, size_t start_col, size_t len, bool
	dollars)
1947
1948
{
    size_t start_index;
1949
	/* Index in buf of the first character shown. */
1950
    size_t column;
1951
	/* Screen column that start_index corresponds to. */
1952
1953
1954
1955
1956
1957
    size_t alloc_len;
	/* The length of memory allocated for converted. */
    char *converted;
	/* The string we return. */
    size_t index;
	/* Current position in converted. */
1958
    char *buf_mb;
1959
1960
    int buf_mb_len;

1961
1962
1963
1964
1965
    /* If dollars is TRUE, make room for the "$" at the end of the
     * line. */
    if (dollars && len > 0 && strlenpt(buf) > start_col + len)
	len--;

1966
1967
1968
    if (len == 0)
	return mallocstrcpy(NULL, "");

1969
1970
    buf_mb = charalloc(mb_cur_max());

1971
1972
    start_index = actual_x(buf, start_col);
    column = strnlenpt(buf, start_index);
1973

1974
    assert(column <= start_col);
1975

1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
    /* Make sure there's enough room for the initial character, whether
     * it's a multibyte control character, a non-control multibyte
     * character, a tab character, or a null terminator.  Rationale:
     *
     * multibyte control character followed by a null terminator:
     *     1 byte ('^') + mb_cur_max() bytes + 1 byte ('\0')
     * multibyte non-control character followed by a null terminator:
     *     mb_cur_max() bytes + 1 byte ('\0')
     * tab character followed by a null terminator:
     *     mb_cur_max() bytes + (tabsize - 1) bytes + 1 byte ('\0')
     *
     * Since tabsize has a minimum value of 1, it can substitute for 1
     * byte above. */
    alloc_len = (mb_cur_max() + tabsize + 1) * MAX_BUF_SIZE;
    converted = charalloc(alloc_len);
1991

1992
1993
    index = 0;

1994
1995
    if (buf[start_index] != '\0' && buf[start_index] != '\t' &&
	(column < start_col || (dollars && column > 0))) {
1996
1997
	/* We don't display all of buf[start_index] since it starts to
	 * the left of the screen. */
1998
	buf_mb_len = parse_mbchar(buf + start_index, buf_mb, NULL);
1999

2000
	if (is_cntrl_mbchar(buf_mb)) {
2001
	    if (column < start_col) {
2002
2003
		char *ctrl_buf_mb = charalloc(mb_cur_max());
		int ctrl_buf_mb_len, i;
2004

2005
2006
		ctrl_buf_mb = control_mbrep(buf_mb, ctrl_buf_mb,
			&ctrl_buf_mb_len);
2007

2008
2009
		for (i = 0; i < ctrl_buf_mb_len; i++)
		    converted[index++] = ctrl_buf_mb[i];
2010

2011
		start_col += mbwidth(ctrl_buf_mb);
2012

2013
		free(ctrl_buf_mb);
2014

2015
		start_index += buf_mb_len;
2016
	    }
2017
	}
2018
#ifdef ENABLE_UTF8
2019
2020
2021
2022
2023
2024
	else if (using_utf8() && mbwidth(buf_mb) == 2) {
	    if (column >= start_col) {
		converted[index++] = ' ';
		start_col++;
	    }

2025
	    converted[index++] = ' ';
2026
	    start_col++;
2027
2028

	    start_index += buf_mb_len;
2029
	}
2030
#endif
2031
2032
    }

2033
    while (buf[start_index] != '\0') {
2034
	buf_mb_len = parse_mbchar(buf + start_index, buf_mb, NULL);
2035

2036
2037
2038
2039
2040
2041
2042
2043
	/* Make sure there's enough room for the next character, whether
	 * it's a multibyte control character, a non-control multibyte
	 * character, a tab character, or a null terminator. */
	if (index + mb_cur_max() + tabsize + 1 >= alloc_len - 1) {
	    alloc_len += (mb_cur_max() + tabsize + 1) * MAX_BUF_SIZE;
	    converted = charealloc(converted, alloc_len);
	}

2044
	/* If buf contains a tab character, interpret it. */
2045
	if (*buf_mb == '\t') {
2046
#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
2047
2048
2049
2050
2051
2052
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i;

		for (i = 0; i < whitespace_len[0]; i++)
		    converted[index++] = whitespace[i];
	    } else
2053
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2054
		converted[index++] = ' ';
2055
	    start_col++;
2056
	    while (start_col % tabsize != 0) {
2057
		converted[index++] = ' ';
2058
2059
		start_col++;
	    }
2060
	/* If buf contains a control character, interpret it.  If buf
2061
2062
	 * contains an invalid multibyte control character, display it
	 * as such.*/
2063
	} else if (is_cntrl_mbchar(buf_mb)) {
2064
2065
	    char *ctrl_buf_mb = charalloc(mb_cur_max());
	    int ctrl_buf_mb_len, i;
2066

2067
	    converted[index++] = '^';
2068
2069
	    start_col++;

2070
2071
	    ctrl_buf_mb = control_mbrep(buf_mb, ctrl_buf_mb,
		&ctrl_buf_mb_len);
2072

2073
2074
	    for (i = 0; i < ctrl_buf_mb_len; i++)
		converted[index++] = ctrl_buf_mb[i];
2075

2076
	    start_col += mbwidth(ctrl_buf_mb);
2077

2078
	    free(ctrl_buf_mb);
2079
	/* If buf contains a space character, interpret it. */
2080
	} else if (*buf_mb == ' ') {
2081
#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
2082
2083
2084
2085
2086
2087
2088
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i;

		for (i = whitespace_len[0]; i < whitespace_len[0] +
			whitespace_len[1]; i++)
		    converted[index++] = whitespace[i];
	    } else
2089
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2090
		converted[index++] = ' ';
2091
	    start_col++;
2092
2093
2094
	/* If buf contains a non-control character, interpret it.  If
	 * buf contains an invalid multibyte non-control character,
	 * display it as such. */
2095
	} else {
2096
2097
	    char *nctrl_buf_mb = charalloc(mb_cur_max());
	    int nctrl_buf_mb_len, i;
2098

2099
2100
	    nctrl_buf_mb = mbrep(buf_mb, nctrl_buf_mb,
		&nctrl_buf_mb_len);
2101

2102
2103
2104
2105
2106
2107
	    for (i = 0; i < nctrl_buf_mb_len; i++)
		converted[index++] = nctrl_buf_mb[i];

	    start_col += mbwidth(nctrl_buf_mb);

	    free(nctrl_buf_mb);
2108
2109
	}

2110
	start_index += buf_mb_len;
2111
2112
    }

2113
2114
    free(buf_mb);

2115
2116
    assert(alloc_len >= index + 1);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2117
    /* Null-terminate converted. */
2118
    converted[index] = '\0';
2119
2120
2121

    /* Make sure converted takes up no more than len columns. */
    index = actual_x(converted, len);
2122
    null_at(&converted, index);
2123

2124
    return converted;
2125
2126
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2127
2128
2129
2130
2131
2132
/* If path is NULL, we're in normal editing mode, so display the current
 * version of nano, the current filename, and whether the current file
 * has been modified on the titlebar.  If path isn't NULL, we're in the
 * file browser, and path contains the directory to start the file
 * browser in, so display the current version of nano and the contents
 * of path on the titlebar. */
2133
void titlebar(const char *path)
Chris Allegretta's avatar
Chris Allegretta committed
2134
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2135
    int space = COLS;
2136
	/* The space we have available for display. */
2137
    size_t verlen = strlenpt(PACKAGE_STRING) + 1;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2138
2139
	/* The length of the version message in columns, plus one for
	 * padding. */
2140
    const char *prefix;
2141
	/* "DIR:", "File:", or "New Buffer".  Goes before filename. */
2142
    size_t prefixlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2143
	/* The length of the prefix in columns, plus one for padding. */
2144
    const char *state;
2145
2146
	/* "Modified", "View", or "".  Shows the state of this
	 * buffer. */
2147
    size_t statelen = 0;
2148
	/* The length of the state in columns, or the length of
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2149
2150
	 * "Modified" if the state is blank and we're not in the file
	 * browser. */
2151
    char *exppath = NULL;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2152
	/* The filename, expanded for display. */
2153
    bool newfie = FALSE;
2154
	/* Do we say "New Buffer"? */
2155
    bool dots = FALSE;
2156
2157
	/* Do we put an ellipsis before the path? */

2158
    assert(path != NULL || openfile->filename != NULL);
Chris Allegretta's avatar
Chris Allegretta committed
2159

2160
    wattron(topwin, reverse_attr);
2161

2162
    blank_titlebar();
Chris Allegretta's avatar
Chris Allegretta committed
2163

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2164
2165
2166
2167
    /* space has to be at least 4: two spaces before the version message,
     * at least one character of the version message, and one space
     * after the version message. */
    if (space < 4)
2168
2169
	space = 0;
    else {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2170
2171
2172
2173
	/* Limit verlen to 1/3 the length of the screen in columns,
	 * minus three columns for spaces. */
	if (verlen > (COLS / 3) - 3)
	    verlen = (COLS / 3) - 3;
2174
    }
Chris Allegretta's avatar
Chris Allegretta committed
2175

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2176
    if (space >= 4) {
2177
2178
	/* Add a space after the version message, and account for both
	 * it and the two spaces before it. */
2179
2180
	mvwaddnstr(topwin, 0, 2, PACKAGE_STRING,
		actual_x(PACKAGE_STRING, verlen));
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2181
2182
2183
2184
	verlen += 3;

	/* Account for the full length of the version message. */
	space -= verlen;
2185
    }
Chris Allegretta's avatar
Chris Allegretta committed
2186

2187
2188
2189
2190
2191
2192
2193
2194
2195
#ifndef DISABLE_BROWSER
    /* Don't display the state if we're in the file browser. */
    if (path != NULL)
	state = "";
    else
#endif
	state = openfile->modified ? _("Modified") : ISSET(VIEW_MODE) ?
		_("View") : "";

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2196
    statelen = strlenpt((*state == '\0' && path == NULL) ?
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2197
	_("Modified") : state);
2198

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2199
2200
    /* If possible, add a space before state. */
    if (space > 0 && statelen < space)
2201
	statelen++;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2202
    else
2203
2204
2205
	goto the_end;

#ifndef DISABLE_BROWSER
2206
    /* path should be a directory if we're in the file browser. */
2207
2208
2209
2210
    if (path != NULL)
	prefix = _("DIR:");
    else
#endif
2211
    if (openfile->filename[0] == '\0') {
2212
	prefix = _("New Buffer");
2213
	newfie = TRUE;
2214
2215
    } else
	prefix = _("File:");
2216

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2217
    prefixlen = strnlenpt(prefix, space - statelen) + 1;
2218

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2219
    /* If newfie is FALSE, add a space after prefix. */
2220
    if (!newfie && prefixlen + statelen < space)
2221
2222
	prefixlen++;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2223
    /* If we're not in the file browser, set path to the current
2224
     * filename. */
2225
    if (path == NULL)
2226
	path = openfile->filename;
2227

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2228
    /* Account for the full lengths of the prefix and the state. */
2229
2230
2231
2232
    if (space >= prefixlen + statelen)
	space -= prefixlen + statelen;
    else
	space = 0;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2233
	/* space is now the room we have for the filename. */
2234

2235
    if (!newfie) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2236
	size_t lenpt = strlenpt(path), start_col;
2237

2238
2239
2240
	/* Don't set dots to TRUE if we have fewer than eight columns
	 * (i.e. one column for padding, plus seven columns for a
	 * filename). */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2241
	dots = (space >= 8 && lenpt >= space);
2242
2243
2244
2245
2246
2247
2248
2249

	if (dots) {
	    start_col = lenpt - space + 3;
	    space -= 3;
	} else
	    start_col = 0;

	exppath = display_string(path, start_col, space, FALSE);
2250
2251
    }

2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
    /* If dots is TRUE, we will display something like "File:
     * ...ename". */
    if (dots) {
	mvwaddnstr(topwin, 0, verlen - 1, prefix, actual_x(prefix,
		prefixlen));
	if (space <= -3 || newfie)
	    goto the_end;
	waddch(topwin, ' ');
	waddnstr(topwin, "...", space + 3);
	if (space <= 0)
	    goto the_end;
	waddstr(topwin, exppath);
    } else {
2265
2266
2267
	size_t exppathlen = newfie ? 0 : strlenpt(exppath);
	    /* The length of the expanded filename. */

2268
	/* There is room for the whole filename, so we center it. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2269
2270
	mvwaddnstr(topwin, 0, verlen + ((space - exppathlen) / 3),
		prefix, actual_x(prefix, prefixlen));
2271
	if (!newfie) {
2272
2273
2274
2275
2276
2277
2278
2279
	    waddch(topwin, ' ');
	    waddstr(topwin, exppath);
	}
    }

  the_end:
    free(exppath);

2280
    if (state[0] != '\0') {
2281
	if (statelen >= COLS - 1)
2282
2283
	    mvwaddnstr(topwin, 0, 0, state, actual_x(state, COLS));
	else {
2284
	    assert(COLS - statelen - 1 >= 0);
2285

2286
	    mvwaddnstr(topwin, 0, COLS - statelen - 1, state,
2287
		actual_x(state, statelen));
2288
	}
2289
    }
2290

2291
    wattroff(topwin, reverse_attr);
2292

2293
    wnoutrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
2294
    reset_cursor();
2295
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
2296
2297
}

2298
2299
/* Mark the current file as modified if it isn't already, and then
 * update the titlebar to display the file's new status. */
2300
2301
void set_modified(void)
{
2302
2303
    if (!openfile->modified) {
	openfile->modified = TRUE;
2304
2305
2306
2307
	titlebar(NULL);
    }
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2308
2309
2310
/* Display a message on the statusbar, and set disable_cursorpos to
 * TRUE, so that the message won't be immediately overwritten if
 * constant cursor position display is on. */
2311
2312
2313
void statusbar(const char *msg, ...)
{
    va_list ap;
2314
2315
    char *bar, *foo;
    size_t start_x, foo_len;
2316
#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
2317
2318
    bool old_whitespace;
#endif
2319
2320
2321
2322
2323

    va_start(ap, msg);

    /* Curses mode is turned off.  If we use wmove() now, it will muck
     * up the terminal settings.  So we just use vfprintf(). */
2324
    if (isendwin()) {
2325
2326
2327
2328
2329
2330
2331
	vfprintf(stderr, msg, ap);
	va_end(ap);
	return;
    }

    blank_statusbar();

2332
#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
2333
2334
    old_whitespace = ISSET(WHITESPACE_DISPLAY);
    UNSET(WHITESPACE_DISPLAY);
2335
#endif
2336
2337
2338
2339
    bar = charalloc(mb_cur_max() * (COLS - 3));
    vsnprintf(bar, mb_cur_max() * (COLS - 3), msg, ap);
    va_end(ap);
    foo = display_string(bar, 0, COLS - 4, FALSE);
2340
#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
2341
2342
    if (old_whitespace)
	SET(WHITESPACE_DISPLAY);
2343
#endif
2344
2345
2346
    free(bar);
    foo_len = strlenpt(foo);
    start_x = (COLS - foo_len - 4) / 2;
2347

2348
    wmove(bottomwin, 0, start_x);
2349
    wattron(bottomwin, reverse_attr);
2350
2351
2352
2353
    waddstr(bottomwin, "[ ");
    waddstr(bottomwin, foo);
    free(foo);
    waddstr(bottomwin, " ]");
2354
    wattroff(bottomwin, reverse_attr);
2355
2356
2357
2358
2359
    wnoutrefresh(bottomwin);
    reset_cursor();
    wnoutrefresh(edit);
	/* Leave the cursor at its position in the edit window, not in
	 * the statusbar. */
2360

2361
    disable_cursorpos = TRUE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2362
2363
2364

    /* If we're doing quick statusbar blanking, and constant cursor
     * position display is off, blank the statusbar after only one
2365
2366
     * keystroke.  Otherwise, blank it after twenty-six keystrokes, as
     * Pico does. */
2367
    statusblank =
2368
#ifndef NANO_TINY
2369
	ISSET(QUICK_BLANK) && !ISSET(CONST_UPDATE) ? 1 :
2370
#endif
2371
	26;
2372
2373
}

2374
2375
/* Display the shortcut list in s on the last two rows of the bottom
 * portion of the window. */
2376
void bottombars(const shortcut *s)
Chris Allegretta's avatar
Chris Allegretta committed
2377
{
2378
    size_t i, colwidth, slen;
2379

Chris Allegretta's avatar
Chris Allegretta committed
2380
2381
2382
    if (ISSET(NO_HELP))
	return;

2383
2384
    if (s == main_list) {
	slen = MAIN_VISIBLE;
2385

2386
	assert(slen <= length_of_list(s));
2387
    } else {
2388
2389
	slen = length_of_list(s);

2390
	/* Don't show any more shortcuts than the main list does. */
2391
2392
2393
2394
	if (slen > MAIN_VISIBLE)
	    slen = MAIN_VISIBLE;
    }

2395
2396
2397
2398
    /* There will be this many characters per column, except for the
     * last two, which will be longer by (COLS % colwidth) columns so as
     * to not waste space.  We need at least three columns to display
     * anything properly. */
2399
    colwidth = COLS / ((slen / 2) + (slen % 2));
Chris Allegretta's avatar
Chris Allegretta committed
2400

2401
    blank_bottombars();
2402

2403
    for (i = 0; i < slen; i++, s = s->next) {
2404
	const char *keystr;
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
	char foo[4] = "";

	/* Yucky sentinel values that we can't handle a better way. */
	if (s->ctrlval == NANO_CONTROL_SPACE)
	    strcpy(foo, "^ ");
	else if (s->ctrlval == NANO_CONTROL_8)
	    strcpy(foo, "^?");
	/* Normal values.  Assume that the shortcut has an equivalent
	 * control key, meta key sequence, or both. */
	else if (s->ctrlval != NANO_NO_KEY)
	    sprintf(foo, "^%c", s->ctrlval + 64);
	else if (s->metaval != NANO_NO_KEY)
	    sprintf(foo, "M-%c", toupper(s->metaval));

	keystr = foo;
2420
2421

	wmove(bottomwin, 1 + i % 2, (i / 2) * colwidth);
2422
	onekey(keystr, s->desc, colwidth + (COLS % colwidth));
Chris Allegretta's avatar
Chris Allegretta committed
2423
    }
2424

2425
2426
    wnoutrefresh(bottomwin);
    reset_cursor();
2427
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
2428
2429
}

2430
2431
2432
2433
2434
2435
/* Write a shortcut key to the help area at the bottom of the window.
 * keystroke is e.g. "^G" and desc is e.g. "Get Help".  We are careful
 * to write at most len characters, even if len is very small and
 * keystroke and desc are long.  Note that waddnstr(,,(size_t)-1) adds
 * the whole string!  We do not bother padding the entry with blanks. */
void onekey(const char *keystroke, const char *desc, size_t len)
Chris Allegretta's avatar
Chris Allegretta committed
2436
{
2437
2438
    size_t keystroke_len = strlenpt(keystroke) + 1;

2439
2440
    assert(keystroke != NULL && desc != NULL);

2441
    wattron(bottomwin, reverse_attr);
2442
    waddnstr(bottomwin, keystroke, actual_x(keystroke, len));
2443
    wattroff(bottomwin, reverse_attr);
2444
2445
2446
2447
2448
2449

    if (len > keystroke_len)
	len -= keystroke_len;
    else
	len = 0;

2450
2451
    if (len > 0) {
	waddch(bottomwin, ' ');
2452
	waddnstr(bottomwin, desc, actual_x(desc, len));
Chris Allegretta's avatar
Chris Allegretta committed
2453
2454
2455
    }
}

2456
2457
/* Reset current_y, based on the position of current, and put the cursor
 * in the edit window at (current_y, current_x). */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2458
2459
void reset_cursor(void)
{
2460
2461
    /* If we haven't opened any files yet, put the cursor in the top
     * left corner of the edit window and get out. */
2462
    if (openfile == NULL) {
2463
	wmove(edit, 0, 0);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2464
	return;
2465
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2466

2467
2468
2469
    openfile->current_y = openfile->current->lineno -
	openfile->edittop->lineno;
    if (openfile->current_y < editwinrows) {
2470
	size_t xpt = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2471

2472
	wmove(edit, openfile->current_y, xpt - get_page_start(xpt));
2473
     }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2474
}
Chris Allegretta's avatar
Chris Allegretta committed
2475

2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
/* edit_draw() takes care of the job of actually painting a line into
 * the edit window.  fileptr is the line to be painted, at row line of
 * the window.  converted is the actual string to be written to the
 * window, with tabs and control characters replaced by strings of
 * regular characters.  start is the column number of the first
 * character of this page.  That is, the first character of converted
 * corresponds to character number actual_x(fileptr->data, start) of the
 * line. */
void edit_draw(const filestruct *fileptr, const char *converted, int
	line, size_t start)
Chris Allegretta's avatar
Chris Allegretta committed
2486
{
2487
#if !defined(NANO_TINY) || defined(ENABLE_COLOR)
2488
2489
2490
2491
2492
2493
2494
2495
2496
    size_t startpos = actual_x(fileptr->data, start);
	/* The position in fileptr->data of the leftmost character
	 * that displays at least partially on the window. */
    size_t endpos = actual_x(fileptr->data, start + COLS - 1) + 1;
	/* The position in fileptr->data of the first character that is
	 * completely off the window to the right.
	 *
	 * Note that endpos might be beyond the null terminator of the
	 * string. */
2497
2498
#endif

2499
    assert(openfile != NULL && fileptr != NULL && converted != NULL);
2500
    assert(strlenpt(converted) <= COLS);
2501

2502
    /* Just paint the string in any case (we'll add color or reverse on
2503
     * just the text that needs it). */
2504
    mvwaddstr(edit, line, 0, converted);
2505

Chris Allegretta's avatar
Chris Allegretta committed
2506
#ifdef ENABLE_COLOR
2507
2508
2509
2510
    /* If color syntaxes are available and turned on, we need to display
     * them. */
    if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX)) {
	const colortype *tmpcolor = openfile->colorstrings;
2511
2512
2513
2514
2515

	for (; tmpcolor != NULL; tmpcolor = tmpcolor->next) {
	    int x_start;
		/* Starting column for mvwaddnstr.  Zero-based. */
	    int paintlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2516
2517
		/* Number of chars to paint on this line.  There are
		 * COLS characters on a whole line. */
2518
	    size_t index;
2519
		/* Index in converted where we paint. */
2520
2521
2522
2523
	    regmatch_t startmatch;
		/* Match position for start_regex. */
	    regmatch_t endmatch;
		/* Match position for end_regex. */
2524
2525
2526
2527

	    if (tmpcolor->bright)
		wattron(edit, A_BOLD);
	    wattron(edit, COLOR_PAIR(tmpcolor->pairnum));
2528
2529
2530
	    /* Two notes about regexec().  A return value of zero means
	     * that there is a match.  Also, rm_eo is the first
	     * non-matching character after the match. */
2531
2532

	    /* First case, tmpcolor is a single-line expression. */
2533
	    if (tmpcolor->end == NULL) {
2534
2535
2536
		size_t k = 0;

		/* We increment k by rm_eo, to move past the end of the
2537
		 * last match.  Even though two matches may overlap, we
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2538
2539
		 * want to ignore them, so that we can highlight e.g. C
		 * strings correctly. */
2540
2541
2542
		while (k < endpos) {
		    /* Note the fifth parameter to regexec().  It says
		     * not to match the beginning-of-line character
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2543
2544
2545
		     * unless k is zero.  If regexec() returns
		     * REG_NOMATCH, there are no more matches in the
		     * line. */
2546
		    if (regexec(tmpcolor->start, &fileptr->data[k], 1,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2547
2548
			&startmatch, (k == 0) ? 0 : REG_NOTBOL) ==
			REG_NOMATCH)
2549
			break;
2550
2551
		    /* Translate the match to the beginning of the
		     * line. */
2552
2553
		    startmatch.rm_so += k;
		    startmatch.rm_eo += k;
2554
2555
2556

		    /* Skip over a zero-length regex match. */
		    if (startmatch.rm_so == startmatch.rm_eo)
2557
			startmatch.rm_eo++;
2558
		    else if (startmatch.rm_so < endpos &&
2559
			startmatch.rm_eo > startpos) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2560
2561
			x_start = (startmatch.rm_so <= startpos) ? 0 :
				strnlenpt(fileptr->data,
2562
				startmatch.rm_so) - start;
2563

2564
2565
2566
			index = actual_x(converted, x_start);

			paintlen = actual_x(converted + index,
2567
2568
				strnlenpt(fileptr->data,
				startmatch.rm_eo) - start - x_start);
2569
2570
2571

			assert(0 <= x_start && 0 <= paintlen);

2572
2573
			mvwaddnstr(edit, line, x_start, converted +
				index, paintlen);
2574
		    }
2575
		    k = startmatch.rm_eo;
Chris Allegretta's avatar
Chris Allegretta committed
2576
		}
2577
	    } else {
2578
		/* This is a multi-line regex.  There are two steps.
2579
2580
2581
2582
2583
2584
		 * First, we have to see if the beginning of the line is
		 * colored by a start on an earlier line, and an end on
		 * this line or later.
		 *
		 * We find the first line before fileptr matching the
		 * start.  If every match on that line is followed by an
2585
2586
2587
2588
		 * end, then go to step two.  Otherwise, find the next
		 * line after start_line matching the end.  If that line
		 * is not before fileptr, then paint the beginning of
		 * this line. */
2589
		const filestruct *start_line = fileptr->prev;
2590
		    /* The first line before fileptr matching start. */
2591
		regoff_t start_col;
2592
		    /* Where it starts in that line. */
2593
2594
		const filestruct *end_line;

2595
		while (start_line != NULL && regexec(tmpcolor->start,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2596
2597
			start_line->data, 1, &startmatch, 0) ==
			REG_NOMATCH) {
2598
2599
		    /* If there is an end on this line, there is no need
		     * to look for starts on earlier lines. */
2600
2601
		    if (regexec(tmpcolor->end, start_line->data, 0,
			NULL, 0) == 0)
2602
2603
2604
			goto step_two;
		    start_line = start_line->prev;
		}
2605
2606
2607

		/* Skip over a zero-length regex match. */
		if (startmatch.rm_so == startmatch.rm_eo)
2608
		    startmatch.rm_eo++;
2609
		else {
2610
2611
		    /* No start found, so skip to the next step. */
		    if (start_line == NULL)
2612
			goto step_two;
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
		    /* Now start_line is the first line before fileptr
		     * containing a start match.  Is there a start on
		     * this line not followed by an end on this line? */
		    start_col = 0;
		    while (TRUE) {
			start_col += startmatch.rm_so;
			startmatch.rm_eo -= startmatch.rm_so;
			if (regexec(tmpcolor->end, start_line->data +
				start_col + startmatch.rm_eo, 0, NULL,
				(start_col + startmatch.rm_eo == 0) ?
				0 : REG_NOTBOL) == REG_NOMATCH)
			    /* No end found after this start. */
			    break;
			start_col++;
			if (regexec(tmpcolor->start, start_line->data +
				start_col, 1, &startmatch,
				REG_NOTBOL) == REG_NOMATCH)
			    /* No later start on this line. */
			    goto step_two;
		    }
		    /* Indeed, there is a start not followed on this
		     * line by an end. */

		    /* We have already checked that there is no end
		     * before fileptr and after the start.  Is there an
		     * end after the start at all?  We don't paint
		     * unterminated starts. */
		    end_line = fileptr;
		    while (end_line != NULL && regexec(tmpcolor->end,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2642
			end_line->data, 1, &endmatch, 0) == REG_NOMATCH)
2643
			end_line = end_line->next;
2644

2645
2646
		    /* No end found, or it is too early. */
		    if (end_line == NULL || (end_line == fileptr &&
2647
			endmatch.rm_eo <= startpos))
2648
			goto step_two;
2649

2650
2651
2652
2653
2654
2655
2656
		    /* Now paint the start of fileptr.  If the start of
		     * fileptr is on a different line from the end,
		     * paintlen is -1, meaning that everything on the
		     * line gets painted.  Otherwise, paintlen is the
		     * expanded location of the end of the match minus
		     * the expanded location of the beginning of the
		     * page. */
2657
2658
2659
2660
2661
2662
		    if (end_line != fileptr)
			paintlen = -1;
		    else
			paintlen = actual_x(converted,
				strnlenpt(fileptr->data,
				endmatch.rm_eo) - start);
2663

2664
		    mvwaddnstr(edit, line, 0, converted, paintlen);
2665

2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
  step_two:
		    /* Second step, we look for starts on this line. */
		    start_col = 0;

		    while (start_col < endpos) {
			if (regexec(tmpcolor->start, fileptr->data +
				start_col, 1, &startmatch, (start_col ==
				0) ? 0 : REG_NOTBOL) == REG_NOMATCH ||
				start_col + startmatch.rm_so >= endpos)
			    /* No more starts on this line. */
			    break;
			/* Translate the match to be relative to the
2678
			 * beginning of the line. */
2679
2680
2681
2682
			startmatch.rm_so += start_col;
			startmatch.rm_eo += start_col;

			x_start = (startmatch.rm_so <= startpos) ? 0 :
2683
				strnlenpt(fileptr->data,
2684
				startmatch.rm_so) - start;
2685

2686
			index = actual_x(converted, x_start);
2687

2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
			if (regexec(tmpcolor->end, fileptr->data +
				startmatch.rm_eo, 1, &endmatch,
				(startmatch.rm_eo == 0) ? 0 :
				REG_NOTBOL) == 0) {
			    /* Translate the end match to be relative to
			     * the beginning of the line. */
			    endmatch.rm_so += startmatch.rm_eo;
			    endmatch.rm_eo += startmatch.rm_eo;
			    /* There is an end on this line.  But does
			     * it appear on this page, and is the match
			     * more than zero characters long? */
			    if (endmatch.rm_eo > startpos &&
				endmatch.rm_eo > startmatch.rm_so) {
				paintlen = actual_x(converted + index,
					strnlenpt(fileptr->data,
					endmatch.rm_eo) - start -
					x_start);
2705

2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
				assert(0 <= x_start && x_start < COLS);

				mvwaddnstr(edit, line, x_start,
					converted + index, paintlen);
			    }
			} else {
			    /* There is no end on this line.  But we
			     * haven't yet looked for one on later
			     * lines. */
			    end_line = fileptr->next;

			    while (end_line != NULL &&
2718
2719
				regexec(tmpcolor->end, end_line->data,
				0, NULL, 0) == REG_NOMATCH)
2720
				end_line = end_line->next;
2721

2722
2723
			    if (end_line != NULL) {
				assert(0 <= x_start && x_start < COLS);
2724

2725
2726
2727
2728
2729
2730
2731
				mvwaddnstr(edit, line, x_start,
					converted + index, -1);
				/* We painted to the end of the line, so
				 * don't bother checking any more
				 * starts. */
				break;
			    }
2732
			}
2733
			start_col = startmatch.rm_so + 1;
2734
		    }
2735
2736
		}
	    }
2737

2738
2739
	    wattroff(edit, A_BOLD);
	    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
2740
	}
2741
    }
2742
#endif /* ENABLE_COLOR */
2743

2744
#ifndef NANO_TINY
2745
    /* If the mark is on, we need to display it. */
2746
    if (openfile->mark_set && (fileptr->lineno <=
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2747
	openfile->mark_begin->lineno || fileptr->lineno <=
2748
	openfile->current->lineno) && (fileptr->lineno >=
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2749
	openfile->mark_begin->lineno || fileptr->lineno >=
2750
	openfile->current->lineno)) {
2751
	/* fileptr is at least partially selected. */
2752
	const filestruct *top;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2753
	    /* Either current or mark_begin, whichever is first. */
2754
	size_t top_x;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2755
	    /* current_x or mark_begin_x, corresponding to top. */
2756
2757
	const filestruct *bot;
	size_t bot_x;
2758
	int x_start;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2759
	    /* Starting column for mvwaddnstr().  Zero-based. */
2760
	int paintlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2761
2762
	    /* Number of characters to paint on this line.  There are
	     * COLS characters on a whole line. */
2763
	size_t index;
2764
	    /* Index in converted where we paint. */
2765

2766
	mark_order(&top, &top_x, &bot, &bot_x, NULL);
2767
2768
2769
2770
2771

	if (top->lineno < fileptr->lineno || top_x < startpos)
	    top_x = startpos;
	if (bot->lineno > fileptr->lineno || bot_x > endpos)
	    bot_x = endpos;
Chris Allegretta's avatar
Chris Allegretta committed
2772

2773
	/* The selected bit of fileptr is on this page. */
2774
2775
	if (top_x < endpos && bot_x > startpos) {
	    assert(startpos <= top_x);
2776
2777
2778
2779

	    /* x_start is the expanded location of the beginning of the
	     * mark minus the beginning of the page. */
	    x_start = strnlenpt(fileptr->data, top_x) - start;
2780

2781
2782
2783
2784
2785
	    /* If the end of the mark is off the page, paintlen is -1,
	     * meaning that everything on the line gets painted.
	     * Otherwise, paintlen is the expanded location of the end
	     * of the mark minus the expanded location of the beginning
	     * of the mark. */
2786
2787
2788
2789
2790
	    if (bot_x >= endpos)
		paintlen = -1;
	    else
		paintlen = strnlenpt(fileptr->data, bot_x) - (x_start +
			start);
2791
2792
2793
2794
2795
2796
2797
2798

	    /* If x_start is before the beginning of the page, shift
	     * paintlen x_start characters to compensate, and put
	     * x_start at the beginning of the page. */
	    if (x_start < 0) {
		paintlen += x_start;
		x_start = 0;
	    }
2799
2800
2801

	    assert(x_start >= 0 && x_start <= strlen(converted));

2802
	    index = actual_x(converted, x_start);
2803

2804
2805
2806
	    if (paintlen > 0)
		paintlen = actual_x(converted + index, paintlen);

2807
	    wattron(edit, reverse_attr);
2808
	    mvwaddnstr(edit, line, x_start, converted + index,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2809
		paintlen);
2810
	    wattroff(edit, reverse_attr);
Chris Allegretta's avatar
Chris Allegretta committed
2811
	}
2812
    }
2813
#endif /* !NANO_TINY */
Chris Allegretta's avatar
Chris Allegretta committed
2814
2815
}

2816
/* Just update one line in the edit buffer.  This is basically a wrapper
2817
2818
 * for edit_draw().  The line will be displayed starting with
 * fileptr->data[index].  Likely arguments are current_x or zero. */
2819
void update_line(const filestruct *fileptr, size_t index)
Chris Allegretta's avatar
Chris Allegretta committed
2820
{
2821
    int line;
2822
	/* The line in the edit window that we want to update. */
2823
2824
2825
2826
    char *converted;
	/* fileptr->data converted to have tabs and control characters
	 * expanded. */
    size_t page_start;
Chris Allegretta's avatar
Chris Allegretta committed
2827

2828
    assert(fileptr != NULL);
2829

2830
    line = fileptr->lineno - openfile->edittop->lineno;
Chris Allegretta's avatar
Chris Allegretta committed
2831

2832
2833
    if (line < 0 || line >= editwinrows)
	return;
2834

2835
    /* First, blank out the line. */
2836
    blank_line(edit, line, 0, COLS);
2837

2838
2839
    /* Next, convert variables that index the line to their equivalent
     * positions in the expanded line. */
2840
    index = strnlenpt(fileptr->data, index);
2841
    page_start = get_page_start(index);
2842

2843
2844
    /* Expand the line, replacing tabs with spaces, and control
     * characters with their displayed forms. */
2845
    converted = display_string(fileptr->data, page_start, COLS, TRUE);
Chris Allegretta's avatar
Chris Allegretta committed
2846

2847
    /* Paint the line. */
2848
    edit_draw(fileptr, converted, line, page_start);
2849
    free(converted);
Chris Allegretta's avatar
Chris Allegretta committed
2850

2851
    if (page_start > 0)
Chris Allegretta's avatar
Chris Allegretta committed
2852
	mvwaddch(edit, line, 0, '$');
2853
    if (strlenpt(fileptr->data) > page_start + COLS)
2854
	mvwaddch(edit, line, COLS - 1, '$');
Chris Allegretta's avatar
Chris Allegretta committed
2855
2856
}

2857
/* Return TRUE if we need an update after moving horizontally, and FALSE
2858
 * otherwise.  We need one if the mark is on or if pww_save and
2859
 * placewewant are on different pages. */
2860
bool need_horizontal_update(size_t pww_save)
2861
2862
{
    return
2863
#ifndef NANO_TINY
2864
	openfile->mark_set ||
2865
#endif
2866
	get_page_start(pww_save) !=
2867
	get_page_start(openfile->placewewant);
2868
2869
}

2870
/* Return TRUE if we need an update after moving vertically, and FALSE
2871
 * otherwise.  We need one if the mark is on or if pww_save and
2872
 * placewewant are on different pages. */
2873
bool need_vertical_update(size_t pww_save)
2874
2875
{
    return
2876
#ifndef NANO_TINY
2877
	openfile->mark_set ||
2878
#endif
2879
	get_page_start(pww_save) !=
2880
	get_page_start(openfile->placewewant);
2881
2882
2883
2884
}

/* Scroll the edit window in the given direction and the given number
 * of lines, and draw new lines on the blank lines left after the
2885
2886
2887
2888
 * scrolling.  direction is the direction to scroll, either UP_DIR or
 * DOWN_DIR, and nlines is the number of lines to scroll.  We change
 * edittop, and assume that current and current_x are up to date.  We
 * also assume that scrollok(edit) is FALSE. */
2889
void edit_scroll(scroll_dir direction, ssize_t nlines)
2890
{
2891
    bool do_redraw = need_vertical_update(0);
2892
    const filestruct *foo;
2893
    ssize_t i;
2894

2895
2896
    /* Don't bother scrolling less than one line. */
    if (nlines < 1)
2897
2898
	return;

2899
2900
2901
    /* Part 1: nlines is the number of lines we're going to scroll the
     * text of the edit window. */

2902
    /* Move the top line of the edit window up or down (depending on the
2903
2904
     * value of direction) nlines lines, or as many lines as we can if
     * there are fewer than nlines lines available. */
2905
    for (i = nlines; i > 0; i--) {
2906
	if (direction == UP_DIR) {
2907
	    if (openfile->edittop == openfile->fileage)
2908
		break;
2909
	    openfile->edittop = openfile->edittop->prev;
2910
	} else {
2911
	    if (openfile->edittop == openfile->filebot)
2912
		break;
2913
	    openfile->edittop = openfile->edittop->next;
2914
2915
2916
	}
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2917
    /* Limit nlines to the number of lines we could scroll. */
2918
    nlines -= i;
2919

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2920
2921
    /* Don't bother scrolling zero lines or more than the number of
     * lines in the edit window minus one; in both cases, get out, and
2922
2923
2924
2925
     * call edit_refresh() beforehand if we need to. */
    if (nlines == 0 || nlines >= editwinrows) {
	if (do_redraw || nlines >= editwinrows)
	    edit_refresh();
2926
2927
	return;
    }
2928
2929
2930

    /* Scroll the text of the edit window up or down nlines lines,
     * depending on the value of direction. */
2931
    scrollok(edit, TRUE);
2932
    wscrl(edit, (direction == UP_DIR) ? -nlines : nlines);
2933
2934
    scrollok(edit, FALSE);

2935
2936
2937
    /* Part 2: nlines is the number of lines in the scrolled region of
     * the edit window that we need to draw. */

2938
2939
    /* If the top or bottom line of the file is now visible in the edit
     * window, we need to draw the entire edit window. */
2940
2941
2942
2943
    if ((direction == UP_DIR && openfile->edittop ==
	openfile->fileage) || (direction == DOWN_DIR &&
	openfile->edittop->lineno + editwinrows - 1 >=
	openfile->filebot->lineno))
2944
	nlines = editwinrows;
2945

2946
2947
2948
2949
2950
2951
    /* If the scrolled region contains only one line, and the line
     * before it is visible in the edit window, we need to draw it too.
     * If the scrolled region contains more than one line, and the lines
     * before and after the scrolled region are visible in the edit
     * window, we need to draw them too. */
    nlines += (nlines == 1) ? 1 : 2;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2952

2953
2954
    if (nlines > editwinrows)
	nlines = editwinrows;
2955
2956
2957

    /* If we scrolled up, we're on the line before the scrolled
     * region. */
2958
    foo = openfile->edittop;
2959

2960
2961
    /* If we scrolled down, move down to the line before the scrolled
     * region. */
2962
    if (direction == DOWN_DIR) {
2963
	for (i = editwinrows - nlines; i > 0 && foo != NULL; i--)
2964
2965
2966
	    foo = foo->next;
    }

2967
2968
2969
2970
2971
2972
    /* Draw new lines on any blank lines before or inside the scrolled
     * region.  If we scrolled down and we're on the top line, or if we
     * scrolled up and we're on the bottom line, the line won't be
     * blank, so we don't need to draw it unless the mark is on or we're
     * not on the first page. */
    for (i = nlines; i > 0 && foo != NULL; i--) {
2973
2974
	if ((i == nlines && direction == DOWN_DIR) || (i == 1 &&
		direction == UP_DIR)) {
2975
2976
2977
2978
2979
	    if (do_redraw)
		update_line(foo, (foo == openfile->current) ?
			openfile->current_x : 0);
	} else
	    update_line(foo, (foo == openfile->current) ?
2980
		openfile->current_x : 0);
2981
	foo = foo->next;
2982
2983
2984
2985
    }
}

/* Update any lines between old_current and current that need to be
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2986
 * updated.  Use this if we've moved without changing any text. */
2987
void edit_redraw(const filestruct *old_current, size_t pww_save)
2988
{
2989
    bool do_redraw = need_vertical_update(0) ||
2990
	need_vertical_update(pww_save);
2991
    const filestruct *foo = NULL;
2992

2993
2994
    /* If either old_current or current is offscreen, scroll the edit
     * window until it's onscreen and get out. */
2995
2996
2997
2998
2999
    if (old_current->lineno < openfile->edittop->lineno ||
	old_current->lineno >= openfile->edittop->lineno +
	editwinrows || openfile->current->lineno <
	openfile->edittop->lineno || openfile->current->lineno >=
	openfile->edittop->lineno + editwinrows) {
3000
3001
3002
	filestruct *old_edittop = openfile->edittop;
	ssize_t nlines;

3003
3004
#ifndef NANO_TINY
	/* If the mark is on, update all the lines between old_current
3005
3006
	 * and either the old first line or old last line (depending on
	 * whether we've scrolled up or down) of the edit window. */
3007
	if (openfile->mark_set) {
3008
3009
3010
3011
3012
3013
3014
3015
3016
	    ssize_t old_lineno;

	    if (old_edittop->lineno < openfile->edittop->lineno)
		old_lineno = old_edittop->lineno;
	    else
		old_lineno = (old_edittop->lineno + editwinrows <=
			openfile->filebot->lineno) ?
			old_edittop->lineno + editwinrows :
			openfile->filebot->lineno;
3017
3018
3019

	    foo = old_current;

3020
	    while (foo->lineno != old_lineno) {
3021
3022
		update_line(foo, 0);

3023
		foo = (foo->lineno > old_lineno) ? foo->prev :
3024
3025
3026
3027
3028
			foo->next;
	    }
	}
#endif /* !NANO_TINY */

3029
3030
3031
3032
	/* Put edittop in range of current, get the difference in lines
	 * between the original edittop and the current edittop, and
	 * then restore the original edittop. */
	edit_update(
3033
#ifndef NANO_TINY
3034
3035
3036
3037
3038
3039
3040
3041
		ISSET(SMOOTH_SCROLL) ? NONE :
#endif
		CENTER);

	nlines = openfile->edittop->lineno - old_edittop->lineno;

	openfile->edittop = old_edittop;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3042
3043
	/* Update old_current if we're not on the same page as
	 * before. */
3044
3045
3046
	if (do_redraw)
	    update_line(old_current, 0);

3047
3048
	/* Scroll the edit window up or down until edittop is in range
	 * of current. */
3049
	if (nlines < 0)
3050
	    edit_scroll(UP_DIR, -nlines);
3051
	else
3052
	    edit_scroll(DOWN_DIR, nlines);
3053

3054
#ifndef NANO_TINY
3055
3056
3057
	/* If the mark is on, update all the lines between the old first
	 * line or old last line of the edit window (depending on
	 * whether we've scrolled up or down) and current. */
3058
	if (openfile->mark_set) {
3059
	    while (foo->lineno != openfile->current->lineno) {
3060
3061
		update_line(foo, 0);

3062
		foo = (foo->lineno > openfile->current->lineno) ?
3063
3064
3065
3066
3067
			foo->prev : foo->next;
	    }
	}
#endif /* !NANO_TINY */

3068
3069
3070
	return;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3071
3072
3073
    /* Update old_current and current if we're not on the same page as
     * before.  If the mark is on, update all the lines between
     * old_current and current too. */
3074
    foo = old_current;
3075

3076
    while (foo != openfile->current) {
3077
	if (do_redraw)
3078
	    update_line(foo, 0);
3079

3080
#ifndef NANO_TINY
3081
	if (!openfile->mark_set)
3082
3083
#endif
	    break;
3084

3085
#ifndef NANO_TINY
3086
3087
	foo = (foo->lineno > openfile->current->lineno) ? foo->prev :
		foo->next;
3088
#endif
3089
    }
3090

3091
    if (do_redraw)
3092
	update_line(openfile->current, openfile->current_x);
3093
3094
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3095
3096
/* Refresh the screen without changing the position of lines.  Use this
 * if we've moved and changed text. */
Chris Allegretta's avatar
Chris Allegretta committed
3097
3098
void edit_refresh(void)
{
3099
3100
    const filestruct *foo;
    int nlines;
3101

3102
3103
3104
    if (openfile->current->lineno < openfile->edittop->lineno ||
	openfile->current->lineno >= openfile->edittop->lineno +
	editwinrows)
3105
3106
	/* Put the top line of the edit window in range of the current
	 * line. */
3107
	edit_update(
3108
#ifndef NANO_TINY
3109
		ISSET(SMOOTH_SCROLL) ? NONE :
3110
3111
#endif
		CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
3112

3113
3114
    foo = openfile->edittop;

3115
#ifdef DEBUG
3116
    fprintf(stderr, "edit_refresh(): edittop->lineno = %ld\n", (long)openfile->edittop->lineno);
3117
#endif
3118

3119
    for (nlines = 0; nlines < editwinrows && foo != NULL; nlines++) {
3120
	update_line(foo, (foo == openfile->current) ?
3121
		openfile->current_x : 0);
3122
3123
3124
	foo = foo->next;
    }

3125
    for (; nlines < editwinrows; nlines++)
3126
3127
3128
	blank_line(edit, nlines, 0, COLS);

    reset_cursor();
3129
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3130
3131
}

3132
3133
3134
3135
/* Move edittop to put it in range of current, keeping current in the
 * same place.  location determines how we move it: if it's CENTER, we
 * center current, and if it's NONE, we put current current_y lines
 * below edittop. */
3136
void edit_update(update_type location)
Chris Allegretta's avatar
Chris Allegretta committed
3137
{
3138
    filestruct *foo = openfile->current;
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
    int goal;

    /* If location is CENTER, we move edittop up (editwinrows / 2)
     * lines.  This puts current at the center of the screen.  If
     * location is NONE, we move edittop up current_y lines if current_y
     * is in range of the screen, 0 lines if current_y is less than 0,
     * or (editwinrows - 1) lines if current_y is greater than
     * (editwinrows - 1).  This puts current at the same place on the
     * screen as before, or at the top or bottom of the screen if
     * edittop is beyond either. */
    if (location == CENTER)
	goal = editwinrows / 2;
    else {
	goal = openfile->current_y;
3153

3154
3155
3156
	/* Limit goal to (editwinrows - 1) lines maximum. */
	if (goal > editwinrows - 1)
	    goal = editwinrows - 1;
Chris Allegretta's avatar
Chris Allegretta committed
3157
    }
3158

3159
    for (; goal > 0 && foo->prev != NULL; goal--)
3160
3161
	foo = foo->prev;

3162
    openfile->edittop = foo;
Chris Allegretta's avatar
Chris Allegretta committed
3163
3164
}

3165
/* Unconditionally redraw the entire screen. */
3166
void total_redraw(void)
3167
{
3168
#ifdef USE_SLANG
3169
    /* Slang curses emulation brain damage, part 4: Slang doesn't define
3170
3171
3172
3173
3174
3175
     * curscr. */
    SLsmg_touch_screen();
    SLsmg_refresh();
#else
    wrefresh(curscr);
#endif
3176
3177
}

3178
3179
/* Unconditionally redraw the entire screen, and then refresh it using
 * the current file. */
3180
3181
void total_refresh(void)
{
3182
    total_redraw();
3183
    titlebar(NULL);
3184
    edit_refresh();
3185
    bottombars(currshortcut);
3186
3187
}

3188
3189
/* Display the main shortcut list on the last two rows of the bottom
 * portion of the window. */
3190
3191
3192
3193
3194
void display_main_list(void)
{
    bottombars(main_list);
}

3195
3196
3197
3198
3199
3200
/* If constant is TRUE, we display the current cursor position only if
 * disable_cursorpos is FALSE.  Otherwise, we display it
 * unconditionally and set disable_cursorpos to FALSE.  If constant is
 * TRUE and disable_cursorpos is TRUE, we also set disable_cursorpos to
 * FALSE, so that we leave the current statusbar alone this time, and
 * display the current cursor position next time. */
3201
void do_cursorpos(bool constant)
Chris Allegretta's avatar
Chris Allegretta committed
3202
{
3203
    filestruct *f;
3204
    char c;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3205
    size_t i, cur_xpt = xplustabs() + 1;
3206
    size_t cur_lenpt = strlenpt(openfile->current->data) + 1;
3207
    int linepct, colpct, charpct;
Chris Allegretta's avatar
Chris Allegretta committed
3208

3209
    assert(openfile->fileage != NULL && openfile->current != NULL);
3210

3211
    f = openfile->current->next;
3212
    c = openfile->current->data[openfile->current_x];
3213
3214

    openfile->current->next = NULL;
3215
    openfile->current->data[openfile->current_x] = '\0';
3216
3217
3218

    i = get_totsize(openfile->fileage, openfile->current);

3219
    openfile->current->data[openfile->current_x] = c;
3220
    openfile->current->next = f;
3221

3222
3223
    if (constant && disable_cursorpos) {
	disable_cursorpos = FALSE;
3224
	return;
3225
    }
Chris Allegretta's avatar
Chris Allegretta committed
3226

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3227
    /* Display the current cursor position on the statusbar, and set
3228
     * disable_cursorpos to FALSE. */
3229
3230
    linepct = 100 * openfile->current->lineno /
	openfile->filebot->lineno;
3231
    colpct = 100 * cur_xpt / cur_lenpt;
3232
3233
    charpct = (openfile->totsize == 0) ? 0 : 100 * i /
	openfile->totsize;
3234
3235

    statusbar(
3236
	_("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%lu (%d%%)"),
3237
	(long)openfile->current->lineno,
3238
	(long)openfile->filebot->lineno, linepct,
3239
	(unsigned long)cur_xpt, (unsigned long)cur_lenpt, colpct,
3240
	(unsigned long)i, (unsigned long)openfile->totsize, charpct);
3241

3242
    disable_cursorpos = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
3243
3244
}

3245
/* Unconditionally display the current cursor position. */
3246
void do_cursorpos_void(void)
3247
{
3248
    do_cursorpos(FALSE);
3249
3250
}

3251
3252
/* Highlight the current word being replaced or spell checked.  We
 * expect word to have tabs and control characters expanded. */
3253
void do_replace_highlight(bool highlight, const char *word)
Chris Allegretta's avatar
Chris Allegretta committed
3254
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3255
    size_t y = xplustabs(), word_len = strlenpt(word);
Chris Allegretta's avatar
Chris Allegretta committed
3256

3257
    y = get_page_start(y) + COLS - y;
3258
	/* Now y is the number of columns that we can display on this
3259
	 * line. */
Chris Allegretta's avatar
Chris Allegretta committed
3260

3261
3262
3263
3264
3265
    assert(y > 0);

    if (word_len > y)
	y--;

Chris Allegretta's avatar
Chris Allegretta committed
3266
    reset_cursor();
3267
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3268

3269
    if (highlight)
3270
	wattron(edit, reverse_attr);
Chris Allegretta's avatar
Chris Allegretta committed
3271

3272
    /* This is so we can show zero-length matches. */
3273
    if (word_len == 0)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3274
	waddch(edit, ' ');
3275
    else
3276
	waddnstr(edit, word, actual_x(word, y));
3277
3278
3279

    if (word_len > y)
	waddch(edit, '$');
Chris Allegretta's avatar
Chris Allegretta committed
3280

3281
    if (highlight)
3282
	wattroff(edit, reverse_attr);
Chris Allegretta's avatar
Chris Allegretta committed
3283
3284
}

3285
#ifdef NANO_EXTRA
3286
#define CREDIT_LEN 55
3287
3288
#define XLCREDIT_LEN 8

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3289
3290
/* Easter egg: Display credits.  Assume nodelay(edit) and scrollok(edit)
 * are FALSE. */
3291
3292
void do_credits(void)
{
3293
3294
    bool old_more_space = ISSET(MORE_SPACE);
    bool old_no_help = ISSET(NO_HELP);
3295
    int kbinput = ERR, crpos = 0, xlpos = 0;
3296
3297
3298
    const char *credits[CREDIT_LEN] = {
	NULL,				/* "The nano text editor" */
	NULL,				/* "version" */
Chris Allegretta's avatar
Chris Allegretta committed
3299
3300
	VERSION,
	"",
3301
	NULL,				/* "Brought to you by:" */
Chris Allegretta's avatar
Chris Allegretta committed
3302
3303
3304
3305
3306
	"Chris Allegretta",
	"Jordi Mallach",
	"Adam Rogoyski",
	"Rob Siemborski",
	"Rocco Corsi",
3307
	"David Lawrence Ramsey",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3308
	"David Benbennick",
3309
	"Mike Frysinger",
Chris Allegretta's avatar
Chris Allegretta committed
3310
3311
	"Ken Tyler",
	"Sven Guckes",
3312
	NULL,				/* credits[15], handled below. */
Chris Allegretta's avatar
Chris Allegretta committed
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
	"Pauli Virtanen",
	"Daniele Medri",
	"Clement Laforet",
	"Tedi Heriyanto",
	"Bill Soudan",
	"Christian Weisgerber",
	"Erik Andersen",
	"Big Gaute",
	"Joshua Jensen",
	"Ryan Krebs",
	"Albert Chin",
	"",
3325
	NULL,				/* "Special thanks to:" */
Chris Allegretta's avatar
Chris Allegretta committed
3326
3327
3328
3329
3330
3331
	"Plattsburgh State University",
	"Benet Laboratories",
	"Amy Allegretta",
	"Linda Young",
	"Jeremy Robichaud",
	"Richard Kolb II",
3332
	NULL,				/* "The Free Software Foundation" */
Chris Allegretta's avatar
Chris Allegretta committed
3333
	"Linus Torvalds",
3334
	NULL,				/* "For ncurses:" */
3335
3336
3337
3338
	"Thomas Dickey",
	"Pavel Curtis",
	"Zeyd Ben-Halim",
	"Eric S. Raymond",
3339
3340
3341
3342
3343
3344
	NULL,				/* "and anyone else we forgot..." */
	NULL,				/* "Thank you for using nano!" */
	"",
	"",
	"",
	"",
3345
3346
	"(C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007",
	"Free Software Foundation, Inc.",
3347
3348
3349
3350
	"",
	"",
	"",
	"",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3351
	"http://www.nano-editor.org/"
3352
3353
    };

3354
    const char *xlcredits[XLCREDIT_LEN] = {
3355
3356
3357
3358
3359
3360
3361
3362
	N_("The nano text editor"),
	N_("version"),
	N_("Brought to you by:"),
	N_("Special thanks to:"),
	N_("The Free Software Foundation"),
	N_("For ncurses:"),
	N_("and anyone else we forgot..."),
	N_("Thank you for using nano!")
3363
    };
3364

3365
    /* credits[15]: Make sure this name is displayed properly, since we
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3366
3367
     * can't dynamically assign it above, using Unicode 00F6 (Latin
     * Small Letter O with Diaresis) if applicable. */
3368
    credits[15] =
3369
#ifdef ENABLE_UTF8
3370
	 using_utf8() ? "Florian K\xC3\xB6nig" :
3371
3372
3373
#endif
	"Florian K\xF6nig";

3374
3375
3376
3377
3378
3379
    if (!old_more_space || !old_no_help) {
	SET(MORE_SPACE);
	SET(NO_HELP);
	window_init();
    }

3380
3381
    curs_set(0);
    nodelay(edit, TRUE);
3382

3383
    blank_titlebar();
3384
    blank_topbar();
Chris Allegretta's avatar
Chris Allegretta committed
3385
    blank_edit();
3386
3387
    blank_statusbar();
    blank_bottombars();
3388

3389
    wrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
3390
    wrefresh(edit);
3391
    wrefresh(bottomwin);
3392
    napms(700);
3393

3394
    for (crpos = 0; crpos < CREDIT_LEN + editwinrows / 2; crpos++) {
3395
	if ((kbinput = wgetch(edit)) != ERR)
3396
	    break;
3397

3398
	if (crpos < CREDIT_LEN) {
3399
	    const char *what;
3400
3401
	    size_t start_x;

3402
	    if (credits[crpos] == NULL) {
3403
		assert(0 <= xlpos && xlpos < XLCREDIT_LEN);
3404

3405
		what = _(xlcredits[xlpos]);
3406
		xlpos++;
3407
	    } else
3408
		what = credits[crpos];
3409

3410
	    start_x = COLS / 2 - strlenpt(what) / 2 - 1;
3411
3412
	    mvwaddstr(edit, editwinrows - 1 - (editwinrows % 2),
		start_x, what);
3413
	}
3414

3415
3416
3417
3418
	wrefresh(edit);

	if ((kbinput = wgetch(edit)) != ERR)
	    break;
3419
	napms(700);
3420

3421
	scrollok(edit, TRUE);
3422
	wscrl(edit, 1);
3423
	scrollok(edit, FALSE);
3424
	wrefresh(edit);
3425

3426
	if ((kbinput = wgetch(edit)) != ERR)
3427
	    break;
3428
	napms(700);
3429

3430
	scrollok(edit, TRUE);
3431
	wscrl(edit, 1);
3432
	scrollok(edit, FALSE);
3433
	wrefresh(edit);
3434
3435
    }

3436
3437
3438
    if (kbinput != ERR)
	ungetch(kbinput);

3439
3440
3441
3442
3443
3444
    if (!old_more_space || !old_no_help) {
	UNSET(MORE_SPACE);
	UNSET(NO_HELP);
	window_init();
    }

3445
    curs_set(1);
3446
    nodelay(edit, FALSE);
3447

3448
    total_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
3449
}
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3450
#endif /* NANO_EXTRA */