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

23
#include "proto.h"
24

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

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

45
#ifndef NANO_TINY
46
static sig_atomic_t sigwinch_counter_save = 0;
47
#endif
48

49
50
51
52
53
54
55
56
57
58
59
60
/* 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.
61
 * - NANO_CONTROL_8 is Ctrl-8 (Ctrl-?), which is Delete under ASCII,
62
63
 *   ANSI, VT100, and VT220, and which is Backspace under VT320.
 *
64
 * Note: VT220 and VT320 also generate Esc [ 3 ~ for Delete.  By
65
66
 * default, xterm assumes it's running on a VT320 and generates Ctrl-8
 * (Ctrl-?) for Backspace and Esc [ 3 ~ for Delete.  This causes
67
 * problems for VT100-derived terminals such as the FreeBSD console,
68
 * which expect Ctrl-H for Backspace and Ctrl-8 (Ctrl-?) for Delete, and
69
70
71
72
73
74
75
76
77
 * 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
78
79
80
 * console, the FreeBSD console, the Mach console, xterm, rxvt, Eterm,
 * and Terminal.  Among these, there are several conflicts and
 * omissions, outlined as follows:
81
82
83
84
85
86
 *
 * - 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
87
 *   keypad key, because the latter has no value when NumLock is off.)
88
89
90
91
 * - 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.)
92
 * - F9 on FreeBSD console == PageDown on Mach console; the former is
93
94
95
 *   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.)
96
 * - F10 on FreeBSD console == PageUp on Mach console; the former is
97
 *   omitted.  (Same as above.)
98
 * - F13 on FreeBSD console == End on Mach console; the former is
99
 *   omitted.  (Same as above.)
100
101
102
103
104
105
 * - 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
106
 *   omitted.  (Same as above.) */
107

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

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

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

124
    /* Read in the first character using whatever mode we're in. */
125
    errcount = 0;
126
    if (nodelay_mode) {
127
	if ((input = wgetch(win)) == ERR)
128
	    return;
129
    } else {
130
	while ((input = wgetch(win)) == ERR) {
131
132
133
134
135
136
137
138
139
#ifndef NANO_TINY
	    /* Did we get SIGWINCH since we were last here? */
	    if (sigwinch_counter != sigwinch_counter_save) {
		sigwinch_counter_save = sigwinch_counter;
		regenerate_screen();
		input = KEY_WINCH;
		break;
	    } else
#endif
140
141
142
143
144
145
146
147
148
149
	    errcount++;

	    /* 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
	     * 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. */
	    if (errcount == MAX_BUF_SIZE)
		handle_hupterm(0);
	}
150
    }
151

152
153
    /* Increment the length of the keystroke buffer, and save the value
     * of the keystroke at the end of it. */
154
    key_buffer_len++;
155
156
    key_buffer = (int *)nmalloc(sizeof(int));
    key_buffer[0] = input;
157

158
159
160
161
162
163
164
#ifndef NANO_TINY
    /* If we got SIGWINCH, get out immediately since the win argument is
     * no longer valid. */
    if (input == KEY_WINCH)
	return;
#endif

165
166
167
168
    /* Read in the remaining characters using non-blocking input. */
    nodelay(win, TRUE);

    while (TRUE) {
169
	input = wgetch(win);
170

171
	/* If there aren't any more characters, stop reading. */
172
	if (input == ERR)
173
174
	    break;

175
176
	/* Otherwise, increment the length of the keystroke buffer, and
	 * save the value of the keystroke at the end of it. */
177
	key_buffer_len++;
178
179
180
	key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
		sizeof(int));
	key_buffer[key_buffer_len - 1] = input;
181
182
    }

183
    /* Switch back to waiting mode for input. */
184
    nodelay(win, FALSE);
185
186

#ifdef DEBUG
187
188
    {
	size_t i;
189
	fprintf(stderr, "\nget_key_buffer(): the sequence of hex codes:");
190
191
192
193
	for (i = 0; i < key_buffer_len; i++)
	    fprintf(stderr, " %3x", key_buffer[i]);
	fprintf(stderr, "\n");
    }
194
#endif
195
}
196

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
197
/* Return the length of the keystroke buffer. */
198
size_t get_key_buffer_len(void)
199
200
201
202
{
    return key_buffer_len;
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
203
/* Add the keystrokes in input to the keystroke buffer. */
204
void unget_input(int *input, size_t input_len)
205
206
{
    /* If input is empty, get out. */
207
    if (input_len == 0)
208
209
	return;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
210
211
    /* If adding input would put the keystroke buffer beyond maximum
     * capacity, only add enough of input to put it at maximum
212
     * capacity. */
213
214
    if (key_buffer_len + input_len < key_buffer_len)
	input_len = (size_t)-1 - key_buffer_len;
215

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
216
217
218
    /* 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. */
219
220
221
    key_buffer_len += input_len;
    key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
	sizeof(int));
222

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
223
224
    /* If the keystroke buffer wasn't empty before, move its beginning
     * forward far enough so that we can add input to its beginning. */
225
226
227
    if (key_buffer_len > input_len)
	memmove(key_buffer + input_len, key_buffer,
		(key_buffer_len - input_len) * sizeof(int));
228

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
229
    /* Copy input to the beginning of the keystroke buffer. */
230
    memcpy(key_buffer, input, input_len * sizeof(int));
231
232
}

233
/* Put back the character stored in kbinput, putting it in byte range
234
235
 * beforehand.  If metakey is TRUE, put back the Escape character after
 * putting back kbinput.  If funckey is TRUE, put back the function key
236
 * (a value outside byte range) without putting it in byte range. */
237
void unget_kbinput(int kbinput, bool metakey, bool funckey)
238
{
239
    if (!funckey)
240
	kbinput = (char)kbinput;
241

242
    unget_input(&kbinput, 1);
243

244
    if (metakey) {
245
246
	kbinput = NANO_CONTROL_3;
	unget_input(&kbinput, 1);
247
248
249
    }
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
250
251
252
253
254
/* 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. */
255
int *get_input(WINDOW *win, size_t input_len)
256
{
257
    int *input;
258
259

    if (key_buffer_len == 0) {
260
	if (win != NULL) {
261
	    get_key_buffer(win);
262

263
264
265
	    if (key_buffer_len == 0)
		return NULL;
	} else
266
267
268
	    return NULL;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
269
270
    /* If input_len is greater than the length of the keystroke buffer,
     * only read the number of characters in the keystroke buffer. */
271
272
273
    if (input_len > key_buffer_len)
	input_len = key_buffer_len;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
274
275
276
    /* Subtract input_len from the length of the keystroke buffer, and
     * allocate input so that it has enough room for input_len
     * keystrokes. */
277
    key_buffer_len -= input_len;
278
    input = (int *)nmalloc(input_len * sizeof(int));
279

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
280
281
    /* Copy input_len keystrokes from the beginning of the keystroke
     * buffer into input. */
282
    memcpy(input, key_buffer, input_len * sizeof(int));
283

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
284
    /* If the keystroke buffer is empty, mark it as such. */
285
286
287
    if (key_buffer_len == 0) {
	free(key_buffer);
	key_buffer = NULL;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
288
289
290
    /* 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. */
291
292
    } else {
	memmove(key_buffer, key_buffer + input_len, key_buffer_len *
293
294
295
		sizeof(int));
	key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
		sizeof(int));
296
297
298
    }

    return input;
299
300
}

301
302
/* 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
303
304
 * sequences, and/or extended keypad values.  Supported extended keypad
 * values consist of
305
306
307
 * [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
308
 * off. */
309
int get_kbinput(WINDOW *win)
310
311
312
313
314
{
    int kbinput;

    /* Read in a character and interpret it.  Continue doing this until
     * we get a recognized value or sequence. */
315
    while ((kbinput = parse_kbinput(win)) == ERR)
316
	;
317

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
318
319
320
    /* If we read from the edit window, blank the statusbar if we need
     * to. */
    if (win == edit)
321
322
	check_statusblank();

323
324
325
326
327
328
    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
329
 * a function key. */
330
int parse_kbinput(WINDOW *win)
331
{
332
    static int escapes = 0, byte_digits = 0;
333
    static bool double_esc = FALSE;
334
    int *kbinput, retval = ERR;
335

336
337
    meta_key = FALSE;
    func_key = FALSE;
338

339
    /* Read in a character. */
340
341
342
343
344
345
    kbinput = get_input(win, 1);

    if (kbinput == NULL && nodelay_mode)
	return 0;

    while (kbinput == NULL)
346
	kbinput = get_input(win, 1);
347

348
349
350
351
352
353
354
355
356
357
358
    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. */
359
360
		case 3:
		    /* Three escapes: wait for more input. */
361
362
		    break;
		default:
363
364
365
		    /* More than three escapes: limit the escape counter
		     * to no more than two, and wait for more input. */
		    escapes %= 3;
366
367
368
369
370
	    }
	    break;
	default:
	    switch (escapes) {
		case 0:
371
372
373
		    /* One non-escape: normal input mode.  Save the
		     * non-escape character as the result. */
		    retval = *kbinput;
374
375
		    break;
		case 1:
376
		    /* Reset the escape counter. */
377
		    escapes = 0;
378
		    if (get_key_buffer_len() == 0) {
379
			/* One escape followed by a non-escape, and
380
381
382
383
			 * 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. */
384
			meta_key = TRUE;
385
			retval = tolower(*kbinput);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
386
		    } else
387
			/* One escape followed by a non-escape, and
388
389
390
			 * there are other keystrokes waiting: escape
			 * sequence mode.  Interpret the escape
			 * sequence. */
391
			retval = parse_escape_sequence(win, *kbinput);
392
393
		    break;
		case 2:
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
		    if (double_esc) {
			/* An "ESC ESC [ X" sequence from Option+arrow. */
			switch (*kbinput) {
			    case 'A':
				retval = KEY_HOME;
				break;
			    case 'B':
				retval = KEY_END;
				break;
#ifndef NANO_TINY
			    case 'C':
				retval = controlright;
				break;
			    case 'D':
				retval = controlleft;
				break;
#endif
			    default:
				retval = ERR;
				break;
			}
			double_esc = FALSE;
			escapes = 0;
		    } else if (get_key_buffer_len() == 0) {
418
419
420
421
422
			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
423
424
425
426
427
428
429
430
431
			     * 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. */
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
			    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. */
467
			    escapes = 0;
468
469
470
471
472
473
			    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
474
475
476
477
478
				 * other keystrokes waiting: control
				 * character sequence mode.  Interpret
				 * the control sequence and save the
				 * corresponding control character as
				 * the result. */
479
480
481
482
483
484
485
486
487
				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;
			    }
488
			}
489
490
491
		    } else if (*kbinput=='[') {
			/* This is an iTerm2 sequence: ^[ ^[ [ X. */
			double_esc = TRUE;
492
		    } else {
493
			/* Two escapes followed by a non-escape, and
494
495
496
497
			 * 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. */
498
			escapes = 0;
499
			meta_key = TRUE;
500
			retval = parse_escape_sequence(win, *kbinput);
501
		    }
502
		    break;
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
		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(
519
				parse_escape_sequence(win, *kbinput));
520
		    break;
521
522
	    }
    }
523

524
    if (retval != ERR) {
525
526
	switch (retval) {
	    case NANO_CONTROL_8:
527
528
		retval = ISSET(REBIND_DELETE) ? sc_seq_or(do_delete, 0) :
			sc_seq_or(do_backspace, 0);
529
530
		break;
	    case KEY_DOWN:
531
532
533
534
#ifdef KEY_SDOWN
	    /* ncurses and Slang don't support KEY_SDOWN. */
	    case KEY_SDOWN:
#endif
535
		retval = sc_seq_or(do_down_void, *kbinput);
536
537
		break;
	    case KEY_UP:
538
539
540
541
#ifdef KEY_SUP
	    /* ncurses and Slang don't support KEY_SUP. */
	    case KEY_SUP:
#endif
542
		retval = sc_seq_or(do_up_void, *kbinput);
543
544
		break;
	    case KEY_LEFT:
545
546
547
548
#ifdef KEY_SLEFT
	    /* Slang doesn't support KEY_SLEFT. */
	    case KEY_SLEFT:
#endif
549
		retval = sc_seq_or(do_left, *kbinput);
550
551
		break;
	    case KEY_RIGHT:
552
553
554
555
#ifdef KEY_SRIGHT
	    /* Slang doesn't support KEY_SRIGHT. */
	    case KEY_SRIGHT:
#endif
556
		retval = sc_seq_or(do_right, *kbinput);
557
		break;
558
559
560
561
562
563
#ifdef KEY_SHOME
	    /* HP-UX 10-11 and Slang don't support KEY_SHOME. */
	    case KEY_SHOME:
#endif
	    case KEY_A1:	/* Home (7) on numeric keypad with
				 * NumLock off. */
564
		retval = sc_seq_or(do_home, *kbinput);
565
		break;
566
	    case KEY_BACKSPACE:
567
		retval = sc_seq_or(do_backspace, *kbinput);
568
		break;
569
570
571
572
#ifdef KEY_SDC
	    /* Slang doesn't support KEY_SDC. */
	    case KEY_SDC:
		if (ISSET(REBIND_DELETE))
573
		    retval = sc_seq_or(do_delete, *kbinput);
574
		else
575
		    retval = sc_seq_or(do_backspace, *kbinput);
576
		break;
577
#endif
578
579
580
#ifdef KEY_SIC
	    /* Slang doesn't support KEY_SIC. */
	    case KEY_SIC:
581
		retval = sc_seq_or(do_insertfile_void, *kbinput);
582
		break;
583
#endif
584
	    case KEY_C3:	/* PageDown (4) on numeric keypad with
585
				 * NumLock off. */
586
		retval = sc_seq_or(do_page_down, *kbinput);
587
588
589
		break;
	    case KEY_A3:	/* PageUp (9) on numeric keypad with
				 * NumLock off. */
590
		retval = sc_seq_or(do_page_up, *kbinput);
591
592
		break;
	    case KEY_ENTER:
593
		retval = sc_seq_or(do_enter, *kbinput);
594
595
596
597
598
599
600
		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. */
601
602
603
604
#ifdef KEY_SEND
	    /* HP-UX 10-11 and Slang don't support KEY_SEND. */
	    case KEY_SEND:
#endif
605
		retval = sc_seq_or(do_end, *kbinput);
606
607
608
609
610
611
612
613
		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
614
615
616
#ifdef KEY_CANCEL
	    /* Slang doesn't support KEY_CANCEL. */
	    case KEY_CANCEL:
617
618
619
#ifdef KEY_SCANCEL
	    /* Slang doesn't support KEY_SCANCEL. */
	    case KEY_SCANCEL:
620
#endif
621
		retval = first_sc_for(currmenu, do_cancel)->seq;
622
623
624
625
626
627
628
629
630
631
632
633
		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
#ifdef KEY_SSUSPEND
	    /* Slang doesn't support KEY_SSUSPEND. */
	    case KEY_SSUSPEND:
634
		retval = sc_seq_or(do_suspend_void, 0);
635
636
637
638
639
		break;
#endif
#ifdef KEY_SUSPEND
	    /* Slang doesn't support KEY_SUSPEND. */
	    case KEY_SUSPEND:
640
		retval = sc_seq_or(do_suspend_void, 0);
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
		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;
661
#endif
662
	}
663

664
665
666
667
668
669
670
#ifndef NANO_TINY
	if (retval == controlleft)
	    retval = sc_seq_or(do_prev_word_void, 0);
	else if (retval == controlright)
	    retval = sc_seq_or(do_next_word_void, 0);
#endif

671
	/* If our result is an extended keypad value (i.e. a value
672
	 * outside of byte range), set func_key to TRUE. */
673
	if (retval != ERR)
674
	    func_key = !is_byte(retval);
675
    }
676
677

#ifdef DEBUG
678
    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);
679
680
#endif

681
682
    free(kbinput);

683
    /* Return the result. */
684
685
686
    return retval;
}

687
/* Translate escape sequences, most of which correspond to extended
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
688
 * keypad values, into their corresponding key values.  These sequences
689
690
 * are generated when the keypad doesn't support the needed keys.
 * Assume that Escape has already been read in. */
691
int convert_sequence(const int *seq, size_t seq_len)
692
{
693
    if (seq_len > 1) {
694
	switch (seq[0]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
695
	    case 'O':
696
		switch (seq[1]) {
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
		    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. */
714
			    return arrow_from_abcd(seq[4]);
715
			case 'P': /* Esc O 1 ; 2 P == F13 on Terminal. */
716
			    return KEY_F(13);
717
			case 'Q': /* Esc O 1 ; 2 Q == F14 on Terminal. */
718
			    return KEY_F(14);
719
			case 'R': /* Esc O 1 ; 2 R == F15 on Terminal. */
720
			    return KEY_F(15);
721
			case 'S': /* Esc O 1 ; 2 S == F16 on Terminal. */
722
			    return KEY_F(16);
723
724
725
726
727
728
		    }
		}
		break;
	    case '5':
		if (seq_len >= 5) {
		    switch (seq[4]) {
729
730
			case 'A': /* Esc O 1 ; 5 A == Ctrl-Up on Terminal. */
			case 'B': /* Esc O 1 ; 5 B == Ctrl-Down on Terminal. */
731
			    return arrow_from_abcd(seq[4]);
732
			case 'C': /* Esc O 1 ; 5 C == Ctrl-Right on Terminal. */
733
			    return CONTROL_RIGHT;
734
			case 'D': /* Esc O 1 ; 5 D == Ctrl-Left on Terminal. */
735
			    return CONTROL_LEFT;
736
737
738
739
740
741
742
743
744
		    }
		}
		break;
	}
    }
				    break;
			    }
			}
			break;
745
		    case '2':
746
			if (seq_len >= 3) {
747
			    switch (seq[2]) {
748
				case 'P': /* Esc O 2 P == F13 on xterm. */
749
				    return KEY_F(13);
750
				case 'Q': /* Esc O 2 Q == F14 on xterm. */
751
				    return KEY_F(14);
752
				case 'R': /* Esc O 2 R == F15 on xterm. */
753
				    return KEY_F(15);
754
				case 'S': /* Esc O 2 S == F16 on xterm. */
755
				    return KEY_F(16);
756
757
			    }
			}
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
758
			break;
759
		    case 'A': /* Esc O A == Up on VT100/VT320/xterm. */
760
761
762
		    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. */
763
			return arrow_from_abcd(seq[1]);
764
765
		    case 'E': /* Esc O E == Center (5) on numeric keypad
			       * with NumLock off on xterm. */
766
			return KEY_B2;
767
		    case 'F': /* Esc O F == End on xterm/Terminal. */
768
			return sc_seq_or(do_end, 0);
769
		    case 'H': /* Esc O H == Home on xterm/Terminal. */
770
			return sc_seq_or(do_home, 0);
771
		    case 'M': /* Esc O M == Enter on numeric keypad with
772
			       * NumLock off on VT100/VT220/VT320/xterm/
773
			       * rxvt/Eterm. */
774
			return sc_seq_or(do_home, 0);
775
		    case 'P': /* Esc O P == F1 on VT100/VT220/VT320/Mach
776
			       * console. */
777
			return KEY_F(1);
778
		    case 'Q': /* Esc O Q == F2 on VT100/VT220/VT320/Mach
779
			       * console. */
780
			return KEY_F(2);
781
		    case 'R': /* Esc O R == F3 on VT100/VT220/VT320/Mach
782
			       * console. */
783
			return KEY_F(3);
784
		    case 'S': /* Esc O S == F4 on VT100/VT220/VT320/Mach
785
			       * console. */
786
			return KEY_F(4);
787
		    case 'T': /* Esc O T == F5 on Mach console. */
788
			return KEY_F(5);
789
		    case 'U': /* Esc O U == F6 on Mach console. */
790
			return KEY_F(6);
791
		    case 'V': /* Esc O V == F7 on Mach console. */
792
			return KEY_F(7);
793
		    case 'W': /* Esc O W == F8 on Mach console. */
794
			return KEY_F(8);
795
		    case 'X': /* Esc O X == F9 on Mach console. */
796
			return KEY_F(9);
797
		    case 'Y': /* Esc O Y == F10 on Mach console. */
798
			return KEY_F(10);
799
800
		    case 'a': /* Esc O a == Ctrl-Up on rxvt. */
		    case 'b': /* Esc O b == Ctrl-Down on rxvt. */
801
			return arrow_from_abcd(seq[1]);
802
		    case 'c': /* Esc O c == Ctrl-Right on rxvt. */
803
			return CONTROL_RIGHT;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
804
		    case 'd': /* Esc O d == Ctrl-Left on rxvt. */
805
			return CONTROL_LEFT;
806
		    case 'j': /* Esc O j == '*' on numeric keypad with
807
			       * NumLock off on VT100/VT220/VT320/xterm/
808
			       * rxvt/Eterm/Terminal. */
809
			return '*';
810
		    case 'k': /* Esc O k == '+' on numeric keypad with
811
			       * NumLock off on VT100/VT220/VT320/xterm/
812
			       * rxvt/Eterm/Terminal. */
813
			return '+';
814
		    case 'l': /* Esc O l == ',' on numeric keypad with
815
			       * NumLock off on VT100/VT220/VT320/xterm/
816
			       * rxvt/Eterm/Terminal. */
817
			return ',';
818
		    case 'm': /* Esc O m == '-' on numeric keypad with
819
			       * NumLock off on VT100/VT220/VT320/xterm/
820
			       * rxvt/Eterm/Terminal. */
821
			return '-';
822
		    case 'n': /* Esc O n == Delete (.) on numeric keypad
823
			       * with NumLock off on VT100/VT220/VT320/
824
			       * xterm/rxvt/Eterm/Terminal. */
825
			return sc_seq_or(do_delete, 0);
826
		    case 'o': /* Esc O o == '/' on numeric keypad with
827
			       * NumLock off on VT100/VT220/VT320/xterm/
828
			       * rxvt/Eterm/Terminal. */
829
			return '/';
830
		    case 'p': /* Esc O p == Insert (0) on numeric keypad
831
			       * with NumLock off on VT100/VT220/VT320/
832
			       * rxvt/Eterm/Terminal. */
833
			return sc_seq_or(do_insertfile_void, 0);
834
		    case 'q': /* Esc O q == End (1) on numeric keypad
835
			       * with NumLock off on VT100/VT220/VT320/
836
			       * rxvt/Eterm/Terminal. */
837
			return sc_seq_or(do_end, 0);
838
		    case 'r': /* Esc O r == Down (2) on numeric keypad
839
			       * with NumLock off on VT100/VT220/VT320/
840
			       * rxvt/Eterm/Terminal. */
841
			return sc_seq_or(do_down_void, 0);
842
		    case 's': /* Esc O s == PageDown (3) on numeric
843
			       * keypad with NumLock off on VT100/VT220/
844
			       * VT320/rxvt/Eterm/Terminal. */
845
			return sc_seq_or(do_page_down, 0);
846
		    case 't': /* Esc O t == Left (4) on numeric keypad
847
			       * with NumLock off on VT100/VT220/VT320/
848
			       * rxvt/Eterm/Terminal. */
849
			return sc_seq_or(do_left, 0);
850
		    case 'u': /* Esc O u == Center (5) on numeric keypad
851
852
			       * with NumLock off on VT100/VT220/VT320/
			       * rxvt/Eterm. */
853
			return KEY_B2;
854
		    case 'v': /* Esc O v == Right (6) on numeric keypad
855
			       * with NumLock off on VT100/VT220/VT320/
856
			       * rxvt/Eterm/Terminal. */
857
			return sc_seq_or(do_right, 0);
858
		    case 'w': /* Esc O w == Home (7) on numeric keypad
859
			       * with NumLock off on VT100/VT220/VT320/
860
			       * rxvt/Eterm/Terminal. */
861
			return sc_seq_or(do_home, 0);
862
		    case 'x': /* Esc O x == Up (8) on numeric keypad
863
			       * with NumLock off on VT100/VT220/VT320/
864
			       * rxvt/Eterm/Terminal. */
865
			return sc_seq_or(do_up_void, 0);
866
		    case 'y': /* Esc O y == PageUp (9) on numeric keypad
867
			       * with NumLock off on VT100/VT220/VT320/
868
			       * rxvt/Eterm/Terminal. */
869
			return sc_seq_or(do_page_up, 0);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
870
871
872
		}
		break;
	    case 'o':
873
		switch (seq[1]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
874
875
		    case 'a': /* Esc o a == Ctrl-Up on Eterm. */
		    case 'b': /* Esc o b == Ctrl-Down on Eterm. */
876
			return arrow_from_abcd(seq[1]);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
877
		    case 'c': /* Esc o c == Ctrl-Right on Eterm. */
878
			return CONTROL_RIGHT;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
879
		    case 'd': /* Esc o d == Ctrl-Left on Eterm. */
880
			return CONTROL_LEFT;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
881
882
883
		}
		break;
	    case '[':
884
		switch (seq[1]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
885
		    case '1':
886
			if (seq_len >= 3) {
887
			    switch (seq[2]) {
888
				case '1': /* Esc [ 1 1 ~ == F1 on rxvt/Eterm. */
889
				    return KEY_F(1);
890
				case '2': /* Esc [ 1 2 ~ == F2 on rxvt/Eterm. */
891
				    return KEY_F(2);
892
				case '3': /* Esc [ 1 3 ~ == F3 on rxvt/Eterm. */
893
				    return KEY_F(3);
894
				case '4': /* Esc [ 1 4 ~ == F4 on rxvt/Eterm. */
895
				    return KEY_F(4);
896
897
				case '5': /* Esc [ 1 5 ~ == F5 on xterm/
					   * rxvt/Eterm. */
898
				    return KEY_F(5);
899
				case '7': /* Esc [ 1 7 ~ == F6 on
900
901
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
902
				    return KEY_F(6);
903
				case '8': /* Esc [ 1 8 ~ == F7 on
904
905
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
906
				    return KEY_F(7);
907
				case '9': /* Esc [ 1 9 ~ == F8 on
908
909
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
910
				    return KEY_F(8);
911
				case ';':
912
    if (seq_len >= 4) {
913
	switch (seq[3]) {
914
	    case '2':
915
		if (seq_len >= 5) {
916
		    switch (seq[4]) {
917
918
919
920
			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. */
921
			    return arrow_from_abcd(seq[4]);
922
923
924
925
		    }
		}
		break;
	    case '5':
926
		if (seq_len >= 5) {
927
		    switch (seq[4]) {
928
929
			case 'A': /* Esc [ 1 ; 5 A == Ctrl-Up on xterm. */
			case 'B': /* Esc [ 1 ; 5 B == Ctrl-Down on xterm. */
930
			    return arrow_from_abcd(seq[4]);
931
			case 'C': /* Esc [ 1 ; 5 C == Ctrl-Right on xterm. */
932
			    return CONTROL_RIGHT;
933
			case 'D': /* Esc [ 1 ; 5 D == Ctrl-Left on xterm. */
934
			    return CONTROL_LEFT;
935
936
937
938
939
940
		    }
		}
		break;
	}
    }
				    break;
941
942
				default: /* Esc [ 1 ~ == Home on
					  * VT320/Linux console. */
943
				    return sc_seq_or(do_home, 0);
944
945
946
947
			    }
			}
			break;
		    case '2':
948
			if (seq_len >= 3) {
949
			    switch (seq[2]) {
950
951
				case '0': /* Esc [ 2 0 ~ == F9 on VT220/VT320/
					   * Linux console/xterm/rxvt/Eterm. */
952
				    return KEY_F(9);
953
954
				case '1': /* Esc [ 2 1 ~ == F10 on VT220/VT320/
					   * Linux console/xterm/rxvt/Eterm. */
955
				    return KEY_F(10);
956
957
				case '3': /* Esc [ 2 3 ~ == F11 on VT220/VT320/
					   * Linux console/xterm/rxvt/Eterm. */
958
				    return KEY_F(11);
959
960
				case '4': /* Esc [ 2 4 ~ == F12 on VT220/VT320/
					   * Linux console/xterm/rxvt/Eterm. */
961
				    return KEY_F(12);
962
963
				case '5': /* Esc [ 2 5 ~ == F13 on VT220/VT320/
					   * Linux console/rxvt/Eterm. */
964
				    return KEY_F(13);
965
966
				case '6': /* Esc [ 2 6 ~ == F14 on VT220/VT320/
					   * Linux console/rxvt/Eterm. */
967
				    return KEY_F(14);
968
969
				case '8': /* Esc [ 2 8 ~ == F15 on VT220/VT320/
					   * Linux console/rxvt/Eterm. */
970
				    return KEY_F(15);
971
972
				case '9': /* Esc [ 2 9 ~ == F16 on VT220/VT320/
					   * Linux console/rxvt/Eterm. */
973
				    return KEY_F(16);
974
975
				default: /* Esc [ 2 ~ == Insert on VT220/VT320/
					  * Linux console/xterm/Terminal. */
976
				    return sc_seq_or(do_insertfile_void, 0);
977
			    }
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
978
979
			}
			break;
980
		    case '3': /* Esc [ 3 ~ == Delete on VT220/VT320/
981
			       * Linux console/xterm/Terminal. */
982
			return sc_seq_or(do_delete, 0);
983
		    case '4': /* Esc [ 4 ~ == End on VT220/VT320/Linux
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
984
			       * console/xterm. */
985
			return sc_seq_or(do_end, 0);
986
		    case '5': /* Esc [ 5 ~ == PageUp on VT220/VT320/
987
988
			       * Linux console/xterm/Terminal;
			       * Esc [ 5 ^ == PageUp on Eterm. */
989
			return sc_seq_or(do_page_up, 0);
990
		    case '6': /* Esc [ 6 ~ == PageDown on VT220/VT320/
991
			       * Linux console/xterm/Terminal;
992
			       * Esc [ 6 ^ == PageDown on Eterm. */
993
			return sc_seq_or(do_page_down, 0);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
994
		    case '7': /* Esc [ 7 ~ == Home on rxvt. */
995
			return sc_seq_or(do_home, 0);
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
996
		    case '8': /* Esc [ 8 ~ == End on rxvt. */
997
			return sc_seq_or(do_end, 0);
998
		    case '9': /* Esc [ 9 == Delete on Mach console. */
999
			return sc_seq_or(do_delete, 0);
1000
		    case '@': /* Esc [ @ == Insert on Mach console. */
1001
			return sc_seq_or(do_insertfile_void, 0);
1002
		    case 'A': /* Esc [ A == Up on ANSI/VT220/Linux
1003
			       * console/FreeBSD console/Mach console/
1004
			       * rxvt/Eterm/Terminal. */
1005
		    case 'B': /* Esc [ B == Down on ANSI/VT220/Linux
1006
			       * console/FreeBSD console/Mach console/
1007
			       * rxvt/Eterm/Terminal. */
1008
		    case 'C': /* Esc [ C == Right on ANSI/VT220/Linux
1009
			       * console/FreeBSD console/Mach console/
1010
			       * rxvt/Eterm/Terminal. */
1011
		    case 'D': /* Esc [ D == Left on ANSI/VT220/Linux
1012
			       * console/FreeBSD console/Mach console/
1013
			       * rxvt/Eterm/Terminal. */
1014
			return arrow_from_abcd(seq[1]);
1015
		    case 'E': /* Esc [ E == Center (5) on numeric keypad
1016
1017
			       * with NumLock off on FreeBSD console/
			       * Terminal. */
1018
			return KEY_B2;
1019
		    case 'F': /* Esc [ F == End on FreeBSD console/Eterm. */
1020
			return sc_seq_or(do_end, 0);
1021
		    case 'G': /* Esc [ G == PageDown on FreeBSD console. */
1022
			return sc_seq_or(do_page_down, 0);
1023
		    case 'H': /* Esc [ H == Home on ANSI/VT220/FreeBSD
1024
			       * console/Mach console/Eterm. */
1025
			return sc_seq_or(do_home, 0);
1026
		    case 'I': /* Esc [ I == PageUp on FreeBSD console. */
1027
			return sc_seq_or(do_page_up, 0);
1028
		    case 'L': /* Esc [ L == Insert on ANSI/FreeBSD console. */
1029
			return sc_seq_or(do_insertfile_void, 0);
1030
		    case 'M': /* Esc [ M == F1 on FreeBSD console. */
1031
			return KEY_F(1);
1032
		    case 'N': /* Esc [ N == F2 on FreeBSD console. */
1033
			return KEY_F(2);
1034
		    case 'O':
1035
			if (seq_len >= 3) {
1036
			    switch (seq[2]) {
1037
				case 'P': /* Esc [ O P == F1 on xterm. */
1038
				    return KEY_F(1);
1039
				case 'Q': /* Esc [ O Q == F2 on xterm. */
1040
				    return KEY_F(2);
1041
				case 'R': /* Esc [ O R == F3 on xterm. */
1042
				    return KEY_F(3);
1043
				case 'S': /* Esc [ O S == F4 on xterm. */
1044
				    return KEY_F(4);
1045
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1046
			} else
1047
			    /* Esc [ O == F3 on FreeBSD console. */
1048
			    return KEY_F(3);
1049
1050
			break;
		    case 'P': /* Esc [ P == F4 on FreeBSD console. */
1051
			return KEY_F(4);
1052
		    case 'Q': /* Esc [ Q == F5 on FreeBSD console. */
1053
			return KEY_F(5);
1054
		    case 'R': /* Esc [ R == F6 on FreeBSD console. */
1055
			return KEY_F(6);
1056
		    case 'S': /* Esc [ S == F7 on FreeBSD console. */
1057
			return KEY_F(7);
1058
		    case 'T': /* Esc [ T == F8 on FreeBSD console. */
1059
			return KEY_F(8);
1060
		    case 'U': /* Esc [ U == PageDown on Mach console. */
1061
			return sc_seq_or(do_page_down, 0);
1062
		    case 'V': /* Esc [ V == PageUp on Mach console. */
1063
			return sc_seq_or(do_page_up, 0);
1064
		    case 'W': /* Esc [ W == F11 on FreeBSD console. */
1065
			return KEY_F(11);
1066
		    case 'X': /* Esc [ X == F12 on FreeBSD console. */
1067
			return KEY_F(12);
1068
		    case 'Y': /* Esc [ Y == End on Mach console. */
1069
			return sc_seq_or(do_end, 0);
1070
		    case 'Z': /* Esc [ Z == F14 on FreeBSD console. */
1071
			return KEY_F(14);
1072
		    case 'a': /* Esc [ a == Shift-Up on rxvt/Eterm. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1073
		    case 'b': /* Esc [ b == Shift-Down on rxvt/Eterm. */
1074
		    case 'c': /* Esc [ c == Shift-Right on rxvt/Eterm. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1075
		    case 'd': /* Esc [ d == Shift-Left on rxvt/Eterm. */
1076
			return arrow_from_abcd(seq[1]);
1077
		    case '[':
1078
			if (seq_len >= 3) {
1079
			    switch (seq[2]) {
1080
1081
				case 'A': /* Esc [ [ A == F1 on Linux
					   * console. */
1082
				    return KEY_F(1);
1083
1084
				case 'B': /* Esc [ [ B == F2 on Linux
					   * console. */
1085
				    return KEY_F(2);
1086
1087
				case 'C': /* Esc [ [ C == F3 on Linux
					   * console. */
1088
				    return KEY_F(3);
1089
1090
				case 'D': /* Esc [ [ D == F4 on Linux
					   * console. */
1091
				    return KEY_F(4);
1092
1093
				case 'E': /* Esc [ [ E == F5 on Linux
					   * console. */
1094
				    return KEY_F(5);
1095
1096
			    }
			}
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1097
1098
1099
1100
			break;
		}
		break;
	}
1101
1102
    }

1103
    return ERR;
1104
1105
}

1106
/* Return the equivalent arrow key value for the case-insensitive
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1107
 * letters A (up), B (down), C (right), and D (left).  These are common
1108
 * to many escape sequences. */
1109
int arrow_from_abcd(int kbinput)
1110
1111
1112
{
    switch (tolower(kbinput)) {
	case 'a':
1113
	    return sc_seq_or(do_up_void, 0);
1114
	case 'b':
1115
	    return sc_seq_or(do_down_void, 0);
1116
	case 'c':
1117
	    return sc_seq_or(do_right, 0);
1118
	case 'd':
1119
	    return sc_seq_or(do_left, 0);
1120
1121
1122
1123
1124
	default:
	    return ERR;
    }
}

1125
/* Interpret the escape sequence in the keystroke buffer, the first
1126
1127
 * character of which is kbinput.  Assume that the keystroke buffer
 * isn't empty, and that the initial escape has already been read in. */
1128
int parse_escape_sequence(WINDOW *win, int kbinput)
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
{
    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);
1139
    retval = convert_sequence(seq, seq_len);
1140
1141
1142

    free(seq);

1143
    /* If we got an unrecognized escape sequence, notify the user. */
1144
    if (retval == ERR) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1145
	if (win == edit) {
1146
	    statusbar(_("Unknown Command"));
1147
	    reset_cursor();
1148
	    curs_set(1);
1149
1150
1151
1152
	    beep();
	}
    }

1153
#ifdef DEBUG
1154
1155
    fprintf(stderr, "parse_escape_sequence(): kbinput = %d, seq_len = %lu, retval = %d\n",
		kbinput, (unsigned long)seq_len, retval);
1156
1157
1158
1159
1160
#endif

    return retval;
}

1161
1162
/* Translate a byte sequence: turn a three-digit decimal number (from
 * 000 to 255) into its corresponding byte value. */
1163
int get_byte_kbinput(int kbinput)
1164
{
1165
    static int byte_digits = 0, byte = 0;
1166
    int retval = ERR;
1167

1168
1169
    /* Increment the byte digit counter. */
    byte_digits++;
1170

1171
    switch (byte_digits) {
1172
	case 1:
1173
1174
	    /* First digit: This must be from zero to two.  Put it in
	     * the 100's position of the byte sequence holder. */
1175
	    if ('0' <= kbinput && kbinput <= '2')
1176
		byte = (kbinput - '0') * 100;
1177
	    else
1178
1179
		/* This isn't the start of a byte sequence.  Return this
		 * character as the result. */
1180
1181
1182
		retval = kbinput;
	    break;
	case 2:
1183
1184
1185
1186
	    /* 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. */
1187
1188
1189
	    if (('0' <= kbinput && kbinput <= '5') || (byte < 200 &&
		'6' <= kbinput && kbinput <= '9'))
		byte += (kbinput - '0') * 10;
1190
	    else
1191
1192
		/* This isn't the second digit of a byte sequence.
		 * Return this character as the result. */
1193
1194
1195
		retval = kbinput;
	    break;
	case 3:
1196
	    /* Third digit: This must be from zero to five if the first
1197
1198
1199
	     * was two and the second was five, and may be any decimal
	     * value otherwise.  Put it in the 1's position of the byte
	     * sequence holder. */
1200
1201
	    if (('0' <= kbinput && kbinput <= '5') || (byte < 250 &&
		'6' <= kbinput && kbinput <= '9')) {
1202
		byte += kbinput - '0';
1203
		/* The byte sequence is complete. */
1204
		retval = byte;
1205
	    } else
1206
1207
		/* This isn't the third digit of a byte sequence.
		 * Return this character as the result. */
1208
1209
		retval = kbinput;
	    break;
1210
	default:
1211
1212
1213
	    /* If there are more than three digits, return this
	     * character as the result.  (Maybe we should produce an
	     * error instead?) */
1214
1215
1216
1217
1218
1219
1220
1221
	    retval = kbinput;
	    break;
    }

    /* If we have a result, reset the byte digit counter and the byte
     * sequence holder. */
    if (retval != ERR) {
	byte_digits = 0;
1222
	byte = 0;
1223
1224
1225
    }

#ifdef DEBUG
1226
    fprintf(stderr, "get_byte_kbinput(): kbinput = %d, byte_digits = %d, byte = %d, retval = %d\n", kbinput, byte_digits, byte, retval);
1227
1228
1229
1230
1231
#endif

    return retval;
}

1232
#ifdef ENABLE_UTF8
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
/* 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;
}

1251
/* Translate a Unicode sequence: turn a six-digit hexadecimal number
1252
 * (from 000000 to 10FFFF, case-insensitive) into its corresponding
1253
 * multibyte value. */
1254
long get_unicode_kbinput(int kbinput)
1255
{
1256
1257
1258
    static int uni_digits = 0;
    static long uni = 0;
    long retval = ERR;
1259

1260
    /* Increment the Unicode digit counter. */
1261
    uni_digits++;
1262

1263
    switch (uni_digits) {
1264
	case 1:
1265
1266
	    /* First digit: This must be zero or one.  Put it in the
	     * 0x100000's position of the Unicode sequence holder. */
1267
	    if ('0' <= kbinput && kbinput <= '1')
1268
		uni = (kbinput - '0') * 0x100000;
1269
	    else
1270
1271
		/* This isn't the first digit of a Unicode sequence.
		 * Return this character as the result. */
1272
1273
1274
		retval = kbinput;
	    break;
	case 2:
1275
1276
1277
1278
1279
1280
	    /* 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);
1281
	    else
1282
1283
		/* This isn't the second digit of a Unicode sequence.
		 * Return this character as the result. */
1284
1285
1286
		retval = kbinput;
	    break;
	case 3:
1287
1288
1289
1290
	    /* 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);
1291
	    break;
1292
	case 4:
1293
1294
1295
1296
	    /* 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);
1297
	    break;
1298
	case 5:
1299
1300
1301
	    /* 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);
1302
	    break;
1303
	case 6:
1304
1305
1306
1307
1308
1309
	    /* 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)
1310
		retval = uni;
1311
1312
	    break;
	default:
1313
1314
1315
	    /* If there are more than six digits, return this character
	     * as the result.  (Maybe we should produce an error
	     * instead?) */
1316
1317
1318
	    retval = kbinput;
	    break;
    }
1319

1320
1321
    /* If we have a result, reset the Unicode digit counter and the
     * Unicode sequence holder. */
1322
    if (retval != ERR) {
1323
1324
	uni_digits = 0;
	uni = 0;
1325
    }
1326

1327
#ifdef DEBUG
1328
    fprintf(stderr, "get_unicode_kbinput(): kbinput = %d, uni_digits = %d, uni = %ld, retval = %ld\n", kbinput, uni_digits, uni, retval);
1329
1330
#endif

1331
1332
    return retval;
}
1333
#endif /* ENABLE_UTF8 */
1334

1335
1336
1337
1338
1339
1340
/* Translate a control character sequence: turn an ASCII non-control
 * character into its corresponding control character. */
int get_control_kbinput(int kbinput)
{
    int retval;

1341
     /* Ctrl-Space (Ctrl-2, Ctrl-@, Ctrl-`) */
1342
1343
    if (kbinput == ' ' || kbinput == '2')
	retval = NANO_CONTROL_SPACE;
1344
1345
    /* Ctrl-/ (Ctrl-7, Ctrl-_) */
    else if (kbinput == '/')
1346
	retval = NANO_CONTROL_7;
1347
    /* Ctrl-3 (Ctrl-[, Esc) to Ctrl-7 (Ctrl-/, Ctrl-_) */
1348
1349
1350
    else if ('3' <= kbinput && kbinput <= '7')
	retval = kbinput - 24;
    /* Ctrl-8 (Ctrl-?) */
1351
1352
    else if (kbinput == '8' || kbinput == '?')
	retval = NANO_CONTROL_8;
1353
1354
    /* Ctrl-@ (Ctrl-Space, Ctrl-2, Ctrl-`) to Ctrl-_ (Ctrl-/, Ctrl-7) */
    else if ('@' <= kbinput && kbinput <= '_')
1355
	retval = kbinput - '@';
1356
1357
    /* Ctrl-` (Ctrl-2, Ctrl-Space, Ctrl-@) to Ctrl-~ (Ctrl-6, Ctrl-^) */
    else if ('`' <= kbinput && kbinput <= '~')
1358
	retval = kbinput - '`';
1359
1360
1361
    else
	retval = kbinput;

1362
#ifdef DEBUG
1363
    fprintf(stderr, "get_control_kbinput(): kbinput = %d, retval = %d\n", kbinput, retval);
1364
1365
#endif

1366
1367
    return retval;
}
1368

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1369
1370
/* Put the output-formatted characters in output back into the keystroke
 * buffer, so that they can be parsed and displayed as output again. */
1371
void unparse_kbinput(char *output, size_t output_len)
1372
{
1373
1374
    int *input;
    size_t i;
1375

1376
1377
1378
1379
    if (output_len == 0)
	return;

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

1381
1382
    for (i = 0; i < output_len; i++)
	input[i] = (int)output[i];
1383

1384
    unget_input(input, output_len);
1385

1386
    free(input);
1387
1388
}

1389
/* Read in a stream of characters verbatim, and return the length of the
1390
1391
1392
1393
 * string in kbinput_len.  Assume nodelay(win) is FALSE. */
int *get_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
{
    int *retval;
1394

1395
    /* Turn off flow control characters if necessary so that we can type
1396
1397
     * them in verbatim, and turn the keypad off if necessary so that we
     * don't get extended keypad values. */
1398
1399
    if (ISSET(PRESERVE))
	disable_flow_control();
1400
1401
    if (!ISSET(REBIND_KEYPAD))
	keypad(win, FALSE);
1402
1403
1404

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

    /* Turn flow control characters back on if necessary and turn the
1407
     * keypad back on if necessary now that we're done. */
1408
1409
    if (ISSET(PRESERVE))
	enable_flow_control();
1410
1411
    if (!ISSET(REBIND_KEYPAD))
	keypad(win, TRUE);
1412

1413
    return retval;
1414
1415
}

1416
1417
/* Read in a stream of all available characters, and return the length
 * of the string in kbinput_len.  Translate the first few characters of
1418
 * the input into the corresponding multibyte value if possible.  After
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1419
 * that, leave the input as-is. */
1420
int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
1421
{
1422
    int *kbinput, *retval;
1423

1424
    /* Read in the first keystroke. */
1425
1426
    while ((kbinput = get_input(win, 1)) == NULL)
	;
1427

1428
#ifdef ENABLE_UTF8
1429
1430
1431
    if (using_utf8()) {
	/* Check whether the first keystroke is a valid hexadecimal
	 * digit. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1432
	long uni = get_unicode_kbinput(*kbinput);
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451

	/* 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) {
1452
1453
		while ((kbinput = get_input(win, 1)) == NULL)
		    ;
1454
1455
		uni = get_unicode_kbinput(*kbinput);
	    }
1456

1457
1458
1459
	    /* Put back the multibyte equivalent of the Unicode
	     * value. */
	    uni_mb = make_mbchar(uni, &uni_mb_len);
1460

1461
	    seq = (int *)nmalloc(uni_mb_len * sizeof(int));
1462

1463
1464
	    for (i = 0; i < uni_mb_len; i++)
		seq[i] = (unsigned char)uni_mb[i];
1465

1466
	    unget_input(seq, uni_mb_len);
1467

1468
1469
	    free(seq);
	    free(uni_mb);
1470
	}
1471
    } else
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1472
1473
#endif /* ENABLE_UTF8 */

1474
1475
	/* Put back the first keystroke. */
	unget_input(kbinput, 1);
1476

1477
1478
    free(kbinput);

1479
    /* Get the complete sequence, and save the characters in it as the
1480
     * result. */
1481
    *kbinput_len = get_key_buffer_len();
1482
    retval = get_input(NULL, *kbinput_len);
1483
1484
1485
1486

    return retval;
}

1487
#ifndef DISABLE_MOUSE
1488
/* Handle any mouse event that may have occurred.  We currently handle
1489
1490
1491
1492
1493
1494
1495
 * 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
1496
1497
1498
1499
1500
1501
 * 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. */
1502
int get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
1503
1504
{
    MEVENT mevent;
1505
    bool in_bottomwin;
1506
    subnfunc *f;
1507
1508
1509
1510
1511
1512

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

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

1515
1516
1517
    /* Save the screen coordinates where the mouse event took place. */
    *mouse_x = mevent.x;
    *mouse_y = mevent.y;
1518

1519
1520
    in_bottomwin = wenclose(bottomwin, *mouse_y, *mouse_x);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1521
    /* Handle releases/clicks of the first mouse button. */
1522
    if (mevent.bstate & (BUTTON1_RELEASED | BUTTON1_CLICKED)) {
1523
1524
	/* If we're allowing shortcuts, the current shortcut list is
	 * being displayed on the last two lines of the screen, and the
1525
1526
1527
	 * 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. */
1528
	if (allow_shortcuts && !ISSET(NO_HELP) && in_bottomwin) {
1529
1530
1531
1532
	    int i;
		/* The width of all the shortcuts, except for the last
		 * two, in the shortcut list in bottomwin. */
	    int j;
1533
		/* The calculated index number of the clicked item. */
1534
1535
1536
1537
	    size_t currslen;
		/* The number of shortcuts in the current shortcut
		 * list. */

1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
	    /* 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;
	    }
1552

1553
	    /* Get the shortcut lists' length. */
1554
	    if (currmenu == MMAIN)
1555
		currslen = MAIN_VISIBLE;
1556
	    else {
1557
		currslen = length_of_list(currmenu);
1558

1559
1560
1561
1562
1563
		/* We don't show any more shortcuts than the main list
		 * does. */
		if (currslen > MAIN_VISIBLE)
		    currslen = MAIN_VISIBLE;
	    }
1564

1565
1566
1567
1568
1569
1570
1571
1572
	    /* 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));

1573
1574
	    /* Calculate the one-based index in the shortcut list. */
	    j = (*mouse_x / i) * 2 + *mouse_y;
1575

1576
1577
	    /* Adjust the index if we hit the last two wider ones. */
	    if ((j > currslen) && (*mouse_x % i < COLS % i))
1578
		j -= 2;
1579
1580
1581
#ifdef DEBUG
	    fprintf(stderr, "Calculated %i as index in shortcut list, currmenu = %x.\n", j, currmenu);
#endif
1582
1583
	    /* Ignore releases/clicks of the first mouse button beyond
	     * the last shortcut. */
1584
	    if (j > currslen)
1585
		return 2;
1586

1587
1588
	    /* Go through the list of functions to determine which
	     * shortcut in the current menu we released/clicked on. */
1589
	    for (f = allfuncs; f != NULL; f = f->next) {
1590
		if ((f->menus & currmenu) == 0)
1591
1592
		    continue;
		if (first_sc_for(currmenu, f->scfunc) == NULL)
1593
		    continue;
1594
1595
		/* Tick off an actually shown shortcut. */
		j -= 1;
1596
1597
		if (j == 0)
		    break;
1598
	    }
1599
#ifdef DEBUG
1600
	    fprintf(stderr, "Stopped on func %ld present in menus %x\n", (long)f->scfunc, f->menus);
1601
#endif
1602

1603
	    /* And put the corresponding key into the keyboard buffer. */
1604
	    if (f != NULL) {
1605
		const sc *s = first_sc_for(currmenu, f->scfunc);
1606
		unget_kbinput(s->seq, s->type == META, s->type == FKEY);
1607
	    }
1608
	    return 1;
1609
	} else
1610
1611
	    /* Handle releases/clicks of the first mouse button that
	     * aren't on the current shortcut list elsewhere. */
1612
	    return 0;
1613
    }
1614
1615
#if NCURSES_MOUSE_VERSION >= 2
    /* Handle presses of the fourth mouse button (upward rolls of the
1616
1617
1618
     * mouse wheel) and presses of the fifth mouse button (downward
     * rolls of the mouse wheel) . */
    else if (mevent.bstate & (BUTTON4_PRESSED | BUTTON5_PRESSED)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1619
	bool in_edit = wenclose(edit, *mouse_y, *mouse_x);
1620

1621
1622
1623
1624
	if (in_bottomwin)
	    /* Translate the mouse event coordinates so that they're
	     * relative to bottomwin. */
	    wmouse_trafo(bottomwin, mouse_y, mouse_x, FALSE);
1625

1626
	if (in_edit || (in_bottomwin && *mouse_y == 0)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1627
1628
	    int i;

1629
1630
1631
1632
1633
	    /* 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) ?
1634
			sc_seq_or(do_up_void, 0) : sc_seq_or(do_down_void, 0),
1635
			FALSE, FALSE);
1636
1637
1638
1639
1640
1641
1642

	    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;
1643
1644
    }
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1645
1646
1647

    /* Ignore all other mouse events. */
    return 2;
1648
}
1649
1650
#endif /* !DISABLE_MOUSE */

1651
1652
1653
1654
/* Return the shortcut that corresponds to the values of kbinput (the
 * key itself) and meta_key (whether the key is a meta sequence).  The
 * returned shortcut will be the first in the list that corresponds to
 * the given sequence. */
1655
const sc *get_shortcut(int *kbinput)
1656
{
1657
    sc *s;
1658

1659
#ifdef DEBUG
1660
    fprintf(stderr, "get_shortcut(): kbinput = %d, meta_key = %s -- ", *kbinput, meta_key ? "TRUE" : "FALSE");
1661
1662
#endif

1663
    for (s = sclist; s != NULL; s = s->next) {
1664
	if ((s->menus & currmenu) && *kbinput == s->seq
1665
		&& meta_key == (s->type == META)) {
1666
#ifdef DEBUG
1667
	    fprintf (stderr, "matched seq \"%s\", and btw meta was %d (menu is %x from %x)\n",
1668
			     s->keystr, meta_key, currmenu, s->menus);
1669
#endif
1670
	    return s;
1671
1672
	}
    }
1673
#ifdef DEBUG
1674
    fprintf (stderr, "matched nothing, btw meta was %d\n", meta_key);
1675
#endif
1676
1677
1678
1679

    return NULL;
}

1680
1681
1682
1683
1684
/* 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
1685

1686
1687
1688
1689
    for (; n > 0; n--)
	waddch(win, ' ');
}

1690
/* Blank the first line of the top portion of the window. */
1691
void blank_titlebar(void)
Chris Allegretta's avatar
Chris Allegretta committed
1692
{
1693
    blank_line(topwin, 0, 0, COLS);
1694
1695
}

1696
1697
/* If the MORE_SPACE flag isn't set, blank the second line of the top
 * portion of the window. */
1698
1699
1700
void blank_topbar(void)
{
    if (!ISSET(MORE_SPACE))
1701
	blank_line(topwin, 1, 0, COLS);
1702
1703
}

1704
/* Blank all the lines of the middle portion of the window, i.e. the
1705
 * edit window. */
Chris Allegretta's avatar
Chris Allegretta committed
1706
1707
void blank_edit(void)
{
1708
    int i;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1709

1710
    for (i = 0; i < editwinrows; i++)
1711
	blank_line(edit, i, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1712
1713
}

1714
/* Blank the first line of the bottom portion of the window. */
Chris Allegretta's avatar
Chris Allegretta committed
1715
1716
void blank_statusbar(void)
{
1717
    blank_line(bottomwin, 0, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1718
1719
}

1720
1721
/* If the NO_HELP flag isn't set, blank the last two lines of the bottom
 * portion of the window. */
1722
1723
1724
void blank_bottombars(void)
{
    if (!ISSET(NO_HELP)) {
1725
1726
	blank_line(bottomwin, 1, 0, COLS);
	blank_line(bottomwin, 2, 0, COLS);
1727
1728
1729
    }
}

1730
1731
1732
/* 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. */
1733
void check_statusblank(void)
Chris Allegretta's avatar
Chris Allegretta committed
1734
{
1735
    if (statusblank > 0) {
1736
	statusblank--;
1737

1738
1739
1740
1741
1742
1743
	if (statusblank == 0 && !ISSET(CONST_UPDATE)) {
	    blank_statusbar();
	    wnoutrefresh(bottomwin);
	    reset_cursor();
	    wnoutrefresh(edit);
	}
Chris Allegretta's avatar
Chris Allegretta committed
1744
1745
1746
    }
}

1747
1748
1749
1750
/* 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
1751
1752
1753
1754
1755
 * 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)
1756
1757
{
    size_t start_index;
1758
	/* Index in buf of the first character shown. */
1759
    size_t column;
1760
	/* Screen column that start_index corresponds to. */
1761
1762
1763
1764
1765
1766
    size_t alloc_len;
	/* The length of memory allocated for converted. */
    char *converted;
	/* The string we return. */
    size_t index;
	/* Current position in converted. */
1767
    char *buf_mb;
1768
1769
    int buf_mb_len;

1770
1771
1772
1773
1774
    /* If dollars is TRUE, make room for the "$" at the end of the
     * line. */
    if (dollars && len > 0 && strlenpt(buf) > start_col + len)
	len--;

1775
1776
1777
    if (len == 0)
	return mallocstrcpy(NULL, "");

1778
1779
    buf_mb = charalloc(mb_cur_max());

1780
1781
    start_index = actual_x(buf, start_col);
    column = strnlenpt(buf, start_index);
1782

1783
    assert(column <= start_col);
1784

1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
    /* 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);
1800

1801
    index = 0;
1802
    seen_wide = FALSE;
1803

1804
1805
    if (buf[start_index] != '\0' && buf[start_index] != '\t' &&
	(column < start_col || (dollars && column > 0))) {
1806
1807
	/* We don't display all of buf[start_index] since it starts to
	 * the left of the screen. */
1808
	buf_mb_len = parse_mbchar(buf + start_index, buf_mb, NULL);
1809

1810
	if (is_cntrl_mbchar(buf_mb)) {
1811
	    if (column < start_col) {
1812
1813
		char *ctrl_buf_mb = charalloc(mb_cur_max());
		int ctrl_buf_mb_len, i;
1814

1815
1816
		ctrl_buf_mb = control_mbrep(buf_mb, ctrl_buf_mb,
			&ctrl_buf_mb_len);
1817

1818
1819
		for (i = 0; i < ctrl_buf_mb_len; i++)
		    converted[index++] = ctrl_buf_mb[i];
1820

1821
		start_col += mbwidth(ctrl_buf_mb);
1822

1823
		free(ctrl_buf_mb);
1824

1825
		start_index += buf_mb_len;
1826
	    }
1827
	}
1828
#ifdef ENABLE_UTF8
1829
1830
1831
1832
1833
1834
	else if (using_utf8() && mbwidth(buf_mb) == 2) {
	    if (column >= start_col) {
		converted[index++] = ' ';
		start_col++;
	    }

1835
	    converted[index++] = ' ';
1836
	    start_col++;
1837
1838

	    start_index += buf_mb_len;
1839
	}
1840
#endif
1841
1842
    }

1843
    while (buf[start_index] != '\0') {
1844
	buf_mb_len = parse_mbchar(buf + start_index, buf_mb, NULL);
1845

1846
1847
1848
	if (mbwidth(buf + start_index) > 1)
	    seen_wide = TRUE;

1849
1850
1851
1852
1853
1854
1855
1856
	/* 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);
	}

1857
	/* If buf contains a tab character, interpret it. */
1858
	if (*buf_mb == '\t') {
1859
#ifndef NANO_TINY
1860
1861
1862
1863
1864
1865
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i;

		for (i = 0; i < whitespace_len[0]; i++)
		    converted[index++] = whitespace[i];
	    } else
1866
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1867
		converted[index++] = ' ';
1868
	    start_col++;
1869
	    while (start_col % tabsize != 0) {
1870
		converted[index++] = ' ';
1871
1872
		start_col++;
	    }
1873
	/* If buf contains a control character, interpret it. */
1874
	} else if (is_cntrl_mbchar(buf_mb)) {
1875
1876
	    char *ctrl_buf_mb = charalloc(mb_cur_max());
	    int ctrl_buf_mb_len, i;
1877

1878
	    converted[index++] = '^';
1879
1880
	    start_col++;

1881
1882
	    ctrl_buf_mb = control_mbrep(buf_mb, ctrl_buf_mb,
		&ctrl_buf_mb_len);
1883

1884
1885
	    for (i = 0; i < ctrl_buf_mb_len; i++)
		converted[index++] = ctrl_buf_mb[i];
1886

1887
	    start_col += mbwidth(ctrl_buf_mb);
1888

1889
	    free(ctrl_buf_mb);
1890
	/* If buf contains a space character, interpret it. */
1891
	} else if (*buf_mb == ' ') {
1892
#ifndef NANO_TINY
1893
1894
1895
1896
1897
1898
1899
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i;

		for (i = whitespace_len[0]; i < whitespace_len[0] +
			whitespace_len[1]; i++)
		    converted[index++] = whitespace[i];
	    } else
1900
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1901
		converted[index++] = ' ';
1902
	    start_col++;
1903
1904
	/* If buf contains a non-control character, interpret it.  If buf
	 * contains an invalid multibyte sequence, display it as such. */
1905
	} else {
1906
1907
	    char *nctrl_buf_mb = charalloc(mb_cur_max());
	    int nctrl_buf_mb_len, i;
1908

1909
#ifdef ENABLE_UTF8
1910
1911
1912
	    /* Make sure an invalid sequence-starter byte is properly
	     * terminated, so that it doesn't pick up lingering bytes
	     * of any previous content. */
1913
1914
1915
	    if (using_utf8() && buf_mb_len == 1)
		buf_mb[1] = '\0';
#endif
1916

1917
1918
	    nctrl_buf_mb = mbrep(buf_mb, nctrl_buf_mb,
		&nctrl_buf_mb_len);
1919

1920
1921
1922
1923
1924
1925
	    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);
1926
1927
	}

1928
	start_index += buf_mb_len;
1929
1930
    }

1931
1932
    free(buf_mb);

1933
1934
    assert(alloc_len >= index + 1);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1935
    /* Null-terminate converted. */
1936
    converted[index] = '\0';
1937
1938
1939

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

1942
    return converted;
1943
1944
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1945
1946
1947
1948
1949
1950
/* 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. */
1951
void titlebar(const char *path)
Chris Allegretta's avatar
Chris Allegretta committed
1952
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1953
    int space = COLS;
1954
	/* The space we have available for display. */
1955
1956
    size_t verlen = strlenpt(BRANDING);
	/* The length of the version message in columns. */
1957
    const char *prefix;
1958
	/* "DIR:", "File:", or "New Buffer".  Goes before filename. */
1959
    size_t prefixlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1960
	/* The length of the prefix in columns, plus one for padding. */
1961
    const char *state;
1962
1963
	/* "Modified", "View", or "".  Shows the state of this
	 * buffer. */
1964
    size_t statelen = 0;
1965
	/* The length of the state in columns, or the length of
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1966
1967
	 * "Modified" if the state is blank and we're not in the file
	 * browser. */
1968
    char *exppath = NULL;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1969
	/* The filename, expanded for display. */
1970
    bool newfie = FALSE;
1971
	/* Do we say "New Buffer"? */
1972
    bool dots = FALSE;
1973
1974
	/* Do we put an ellipsis before the path? */

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

1977
1978
1979
    if (interface_color_pair[TITLE_BAR].bright)
	wattron(topwin, A_BOLD);
    wattron(topwin, interface_color_pair[TITLE_BAR].pairnum);
1980

1981
    blank_titlebar();
Chris Allegretta's avatar
Chris Allegretta committed
1982

1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
    /* Limit the length of the version message to a third of the width of
     * the screen, minus three columns for spaces. */
    if (verlen > (COLS / 3) - 3)
	verlen = (COLS / 3) - 3;

    /* Leave two spaces before the version message, and account also
     * for the space after it. */
     mvwaddnstr(topwin, 0, 2, BRANDING, actual_x(BRANDING, verlen));
     verlen += 3;

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

1996
1997
1998
1999
2000
2001
2002
2003
2004
#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
2005
    statelen = strlenpt((*state == '\0' && path == NULL) ?
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2006
	_("Modified") : state);
2007

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2008
2009
    /* If possible, add a space before state. */
    if (space > 0 && statelen < space)
2010
	statelen++;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2011
    else
2012
2013
2014
	goto the_end;

#ifndef DISABLE_BROWSER
2015
    /* path should be a directory if we're in the file browser. */
2016
2017
2018
2019
    if (path != NULL)
	prefix = _("DIR:");
    else
#endif
2020
    if (openfile->filename[0] == '\0') {
2021
	prefix = _("New Buffer");
2022
	newfie = TRUE;
2023
2024
    } else
	prefix = _("File:");
2025

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2028
    /* If newfie is FALSE, add a space after prefix. */
2029
    if (!newfie && prefixlen + statelen < space)
2030
2031
	prefixlen++;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2032
    /* If we're not in the file browser, set path to the current
2033
     * filename. */
2034
    if (path == NULL)
2035
	path = openfile->filename;
2036

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2037
    /* Account for the full lengths of the prefix and the state. */
2038
2039
2040
2041
    if (space >= prefixlen + statelen)
	space -= prefixlen + statelen;
    else
	space = 0;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2042
	/* space is now the room we have for the filename. */
2043

2044
    if (!newfie) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2045
	size_t lenpt = strlenpt(path), start_col;
2046

2047
2048
2049
	/* 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
2050
	dots = (space >= 8 && lenpt >= space);
2051
2052
2053
2054
2055
2056
2057
2058

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

	exppath = display_string(path, start_col, space, FALSE);
2059
2060
    }

2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
    /* 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 {
2074
2075
2076
	size_t exppathlen = newfie ? 0 : strlenpt(exppath);
	    /* The length of the expanded filename. */

2077
	/* There is room for the whole filename, so we center it. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2078
2079
	mvwaddnstr(topwin, 0, verlen + ((space - exppathlen) / 3),
		prefix, actual_x(prefix, prefixlen));
2080
	if (!newfie) {
2081
2082
2083
2084
2085
2086
2087
2088
	    waddch(topwin, ' ');
	    waddstr(topwin, exppath);
	}
    }

  the_end:
    free(exppath);

2089
    if (state[0] != '\0') {
2090
	if (statelen >= COLS - 1)
2091
2092
	    mvwaddnstr(topwin, 0, 0, state, actual_x(state, COLS));
	else {
2093
	    assert(COLS - statelen - 1 >= 0);
2094

2095
	    mvwaddnstr(topwin, 0, COLS - statelen - 1, state,
2096
		actual_x(state, statelen));
2097
	}
2098
    }
2099

2100
2101
    wattroff(topwin, A_BOLD);
    wattroff(topwin, interface_color_pair[TITLE_BAR].pairnum);
2102

2103
    wnoutrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
2104
    reset_cursor();
2105
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
2106
2107
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2108
2109
2110
/* 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. */
2111
2112
2113
void statusbar(const char *msg, ...)
{
    va_list ap;
2114
    char *bar, *foo;
Benno Schulenberg's avatar
Benno Schulenberg committed
2115
    size_t start_x;
2116
2117
2118
2119
#ifndef NANO_TINY
    bool old_whitespace = ISSET(WHITESPACE_DISPLAY);

    UNSET(WHITESPACE_DISPLAY);
2120
#endif
2121
2122
2123
2124
2125

    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(). */
2126
    if (isendwin()) {
2127
2128
2129
2130
2131
	vfprintf(stderr, msg, ap);
	va_end(ap);
	return;
    }

2132
2133
2134
    /* Turn the cursor off while fiddling in the statusbar. */
    curs_set(0);

2135
2136
    blank_statusbar();

2137
2138
2139
2140
    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);
Benno Schulenberg's avatar
Benno Schulenberg committed
2141
    free(bar);
2142
2143

#ifndef NANO_TINY
2144
2145
    if (old_whitespace)
	SET(WHITESPACE_DISPLAY);
2146
#endif
Benno Schulenberg's avatar
Benno Schulenberg committed
2147
    start_x = (COLS - strlenpt(foo) - 4) / 2;
2148

2149
    wmove(bottomwin, 0, start_x);
2150
2151
2152
    if (interface_color_pair[STATUS_BAR].bright)
	wattron(bottomwin, A_BOLD);
    wattron(bottomwin, interface_color_pair[STATUS_BAR].pairnum);
2153
2154
2155
2156
    waddstr(bottomwin, "[ ");
    waddstr(bottomwin, foo);
    free(foo);
    waddstr(bottomwin, " ]");
2157
2158
    wattroff(bottomwin, A_BOLD);
    wattroff(bottomwin, interface_color_pair[STATUS_BAR].pairnum);
2159

2160
    wnoutrefresh(bottomwin);
2161

2162
    disable_cursorpos = TRUE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2163

2164
2165
2166
    /* Push the message to the screen straightaway. */
    doupdate();

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2167
2168
    /* If we're doing quick statusbar blanking, and constant cursor
     * position display is off, blank the statusbar after only one
2169
2170
     * keystroke.  Otherwise, blank it after twenty-six keystrokes, as
     * Pico does. */
2171
    statusblank =
2172
#ifndef NANO_TINY
2173
	ISSET(QUICK_BLANK) && !ISSET(CONST_UPDATE) ? 1 :
2174
#endif
2175
	26;
2176
2177
}

2178
2179
/* Display the shortcut list corresponding to menu on the last two rows
 * of the bottom portion of the window. */
2180
void bottombars(int menu)
Chris Allegretta's avatar
Chris Allegretta committed
2181
{
2182
    size_t i, colwidth, slen;
2183
2184
    subnfunc *f;
    const sc *s;
2185

2186
2187
2188
    /* Set the global variable to the given menu. */
    currmenu = menu;

Chris Allegretta's avatar
Chris Allegretta committed
2189
2190
2191
    if (ISSET(NO_HELP))
	return;

2192
    if (menu == MMAIN) {
2193
	slen = MAIN_VISIBLE;
2194

2195
	assert(slen <= length_of_list(menu));
2196
    } else {
2197
	slen = length_of_list(menu);
2198

2199
	/* Don't show any more shortcuts than the main list does. */
2200
2201
2202
2203
	if (slen > MAIN_VISIBLE)
	    slen = MAIN_VISIBLE;
    }

2204
2205
2206
2207
    /* 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. */
2208
    colwidth = COLS / ((slen / 2) + (slen % 2));
Chris Allegretta's avatar
Chris Allegretta committed
2209

2210
    blank_bottombars();
2211

2212
2213
2214
#ifdef DEBUG
    fprintf(stderr, "In bottombars, and slen == \"%d\"\n", (int) slen);
#endif
2215

2216
    for (f = allfuncs, i = 0; i < slen && f != NULL; f = f->next) {
2217

2218
#ifdef DEBUG
2219
	fprintf(stderr, "Checking menu items....");
2220
#endif
2221
	if ((f->menus & menu) == 0)
2222
	    continue;
2223

2224
#ifdef DEBUG
2225
	fprintf(stderr, "found one! f->menus = %x, desc = \"%s\"\n", f->menus, f->desc);
2226
#endif
2227
2228
	s = first_sc_for(menu, f->scfunc);
	if (s == NULL) {
2229
2230
2231
#ifdef DEBUG
	    fprintf(stderr, "Whoops, guess not, no shortcut key found for func!\n");
#endif
2232
2233
	    continue;
	}
2234
	wmove(bottomwin, 1 + i % 2, (i / 2) * colwidth);
2235
#ifdef DEBUG
2236
	fprintf(stderr, "Calling onekey with keystr \"%s\" and desc \"%s\"\n", s->keystr, f->desc);
2237
#endif
2238
	onekey(s->keystr, _(f->desc), colwidth + (COLS % colwidth));
2239
	i++;
Chris Allegretta's avatar
Chris Allegretta committed
2240
    }
2241

2242
2243
    wnoutrefresh(bottomwin);
    reset_cursor();
2244
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
2245
2246
}

2247
2248
/* 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
2249
 * to write at most length characters, even if length is very small and
2250
2251
 * keystroke and desc are long.  Note that waddnstr(,,(size_t)-1) adds
 * the whole string!  We do not bother padding the entry with blanks. */
2252
void onekey(const char *keystroke, const char *desc, int length)
Chris Allegretta's avatar
Chris Allegretta committed
2253
{
2254
2255
    assert(keystroke != NULL && desc != NULL);

2256
2257
2258
    if (interface_color_pair[KEY_COMBO].bright)
	wattron(bottomwin, A_BOLD);
    wattron(bottomwin, interface_color_pair[KEY_COMBO].pairnum);
2259
    waddnstr(bottomwin, keystroke, actual_x(keystroke, length));
2260
2261
    wattroff(bottomwin, A_BOLD);
    wattroff(bottomwin, interface_color_pair[KEY_COMBO].pairnum);
2262

2263
    length -= strlenpt(keystroke) + 1;
2264

2265
    if (length > 0) {
2266
	waddch(bottomwin, ' ');
2267
2268
2269
	if (interface_color_pair[FUNCTION_TAG].bright)
	    wattron(bottomwin, A_BOLD);
	wattron(bottomwin, interface_color_pair[FUNCTION_TAG].pairnum);
2270
	waddnstr(bottomwin, desc, actual_x(desc, length));
2271
2272
	wattroff(bottomwin, A_BOLD);
	wattroff(bottomwin, interface_color_pair[FUNCTION_TAG].pairnum);
Chris Allegretta's avatar
Chris Allegretta committed
2273
2274
2275
    }
}

2276
2277
/* Redetermine current_y from the position of current relative to edittop,
 * and put the cursor in the edit window at (current_y, current_x). */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2278
2279
void reset_cursor(void)
{
2280
    size_t xpt = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2281

2282
#ifndef NANO_TINY
2283
    if (ISSET(SOFTWRAP)) {
2284
	filestruct *line = openfile->edittop;
2285
2286
	openfile->current_y = 0;

2287
2288
2289
2290
	while (line != NULL && line != openfile->current) {
	    openfile->current_y += strlenpt(line->data) / COLS + 1;
	    line = line->next;
	}
2291
	openfile->current_y += xpt / COLS;
2292

2293
	if (openfile->current_y < editwinrows)
2294
	    wmove(edit, openfile->current_y, xpt % COLS);
2295
2296
2297
    } else
#endif
    {
2298
	openfile->current_y = openfile->current->lineno -
2299
				openfile->edittop->lineno;
2300
2301
2302
2303

	if (openfile->current_y < editwinrows)
	    wmove(edit, openfile->current_y, xpt - get_page_start(xpt));
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2304
}
Chris Allegretta's avatar
Chris Allegretta committed
2305

2306
2307
2308
2309
2310
2311
2312
2313
/* 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. */
2314
void edit_draw(filestruct *fileptr, const char *converted, int
2315
	line, size_t start)
Chris Allegretta's avatar
Chris Allegretta committed
2316
{
2317
#if !defined(NANO_TINY) || !defined(DISABLE_COLOR)
2318
2319
2320
2321
2322
2323
2324
2325
2326
    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. */
2327
2328
#endif

2329
    assert(openfile != NULL && fileptr != NULL && converted != NULL);
2330
    assert(strlenpt(converted) <= COLS);
2331

2332
2333
2334
    /* First simply paint the line -- then we'll add colors or the
     * marking highlight on just the pieces that need it. */
    mvwaddstr(edit, line, 0, converted);
2335

2336
#ifndef USE_SLANG
2337
2338
2339
    /* Tell ncurses to really redraw the line without trying to optimize
     * for what it thinks is already there, because it gets it wrong in
     * the case of a wide character in column zero.  See bug #31743. */
2340
2341
    if (seen_wide)
	wredrawln(edit, line, 1);
2342
#endif
2343

2344
#ifndef DISABLE_COLOR
2345
2346
2347
    /* If color syntaxes are available and turned on, we need to display
     * them. */
    if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX)) {
2348
	const colortype *varnish = openfile->colorstrings;
2349

2350
2351
2352
2353
	/* If there are multiline regexes, make sure there is a cache. */
	if (openfile->syntax->nmultis > 0)
	    alloc_multidata_if_needed(fileptr);

2354
	for (; varnish != NULL; varnish = varnish->next) {
2355
2356
	    int x_start;
		/* Starting column for mvwaddnstr.  Zero-based. */
2357
	    int paintlen = 0;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2358
2359
		/* Number of chars to paint on this line.  There are
		 * COLS characters on a whole line. */
2360
	    size_t index;
2361
		/* Index in converted where we paint. */
2362
2363
2364
2365
	    regmatch_t startmatch;
		/* Match position for start_regex. */
	    regmatch_t endmatch;
		/* Match position for end_regex. */
2366

2367
	    if (varnish->bright)
2368
		wattron(edit, A_BOLD);
2369
	    wattron(edit, COLOR_PAIR(varnish->pairnum));
2370
2371
2372
	    /* 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. */
2373

2374
2375
	    /* First case: varnish is a single-line expression. */
	    if (varnish->end == NULL) {
2376
2377
2378
		size_t k = 0;

		/* We increment k by rm_eo, to move past the end of the
2379
		 * last match.  Even though two matches may overlap, we
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2380
2381
		 * want to ignore them, so that we can highlight e.g. C
		 * strings correctly. */
2382
2383
2384
		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
2385
2386
2387
		     * unless k is zero.  If regexec() returns
		     * REG_NOMATCH, there are no more matches in the
		     * line. */
2388
		    if (regexec(varnish->start, &fileptr->data[k], 1,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2389
2390
			&startmatch, (k == 0) ? 0 : REG_NOTBOL) ==
			REG_NOMATCH)
2391
			break;
2392
2393
		    /* Translate the match to the beginning of the
		     * line. */
2394
2395
		    startmatch.rm_so += k;
		    startmatch.rm_eo += k;
2396
2397
2398

		    /* Skip over a zero-length regex match. */
		    if (startmatch.rm_so == startmatch.rm_eo)
2399
			startmatch.rm_eo++;
2400
		    else if (startmatch.rm_so < endpos &&
2401
			startmatch.rm_eo > startpos) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2402
2403
			x_start = (startmatch.rm_so <= startpos) ? 0 :
				strnlenpt(fileptr->data,
2404
				startmatch.rm_so) - start;
2405

2406
2407
2408
			index = actual_x(converted, x_start);

			paintlen = actual_x(converted + index,
2409
2410
				strnlenpt(fileptr->data,
				startmatch.rm_eo) - start - x_start);
2411
2412
2413

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

2414
			mvwaddnstr(edit, line, x_start, converted +
2415
				index, paintlen);
2416
		    }
2417
		    k = startmatch.rm_eo;
Chris Allegretta's avatar
Chris Allegretta committed
2418
		}
2419
	    } else {	/* Second case: varnish is a multiline expression. */
2420
		const filestruct *start_line = fileptr->prev;
2421
		    /* The first line before fileptr that matches 'start'. */
2422
		regoff_t start_col;
2423
		    /* Where the match starts in that line. */
2424
		const filestruct *end_line;
2425
		    /* The line that matches 'end'. */
2426

2427
		/* First see if the multidata was maybe already calculated. */
2428
		if (fileptr->multidata[varnish->id] == CNONE)
2429
		    goto tail_of_loop;
2430
		else if (fileptr->multidata[varnish->id] == CWHOLELINE) {
2431
		    mvwaddnstr(edit, line, 0, converted, -1);
2432
		    goto tail_of_loop;
2433
2434
		} else if (fileptr->multidata[varnish->id] == CBEGINBEFORE) {
		    regexec(varnish->end, fileptr->data, 1, &endmatch, 0);
2435
2436
		    /* If the coloured part is scrolled off, skip it. */
		    if (endmatch.rm_eo <= startpos)
2437
			goto tail_of_loop;
2438
2439
2440
		    paintlen = actual_x(converted, strnlenpt(fileptr->data,
			endmatch.rm_eo) - start);
		    mvwaddnstr(edit, line, 0, converted, paintlen);
2441
		    goto tail_of_loop;
2442
		} if (fileptr->multidata[varnish->id] == -1)
2443
		    /* Assume this until proven otherwise below. */
2444
		    fileptr->multidata[varnish->id] = CNONE;
2445

2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
		/* There is no precalculated multidata, so find it out now.
		 * First check if the beginning of the line is colored by a
		 * start on an earlier line, and an end on this line or later.
		 *
		 * So: find the first line before fileptr matching the start.
		 * If every match on that line is followed by an end, then go
		 * to step two.  Otherwise, find a line after start_line that
		 * matches the end.  If that line is not before fileptr, then
		 * paint the beginning of this line. */

2456
		while (start_line != NULL && regexec(varnish->start,
2457
2458
2459
			start_line->data, 1, &startmatch, 0) == REG_NOMATCH) {
		    /* There is no start; but if there is an end on this line,
		     * there is no need to look for starts on earlier lines. */
2460
		    if (regexec(varnish->end, start_line->data, 0, NULL, 0) == 0)
2461
2462
2463
			goto step_two;
		    start_line = start_line->prev;
		}
2464

2465
2466
2467
2468
		/* If no start was found, skip to the next step. */
		if (start_line == NULL)
		    goto step_two;

2469
		/* If a found start has been qualified as an end earlier,
2470
		 * believe it and skip to the next step. */
2471
		if (start_line->multidata != NULL &&
2472
2473
			(start_line->multidata[varnish->id] == CBEGINBEFORE ||
			start_line->multidata[varnish->id] == CSTARTENDHERE))
2474
2475
		    goto step_two;

2476
		/* Skip over a zero-length regex match. */
2477
		if (startmatch.rm_so == startmatch.rm_eo)
2478
2479
		    goto tail_of_loop;

2480
2481
2482
2483
2484
2485
2486
		/* Now start_line is the first line before fileptr containing
		 * a start match.  Is there a start on that line not followed
		 * by an end on that line? */
		start_col = 0;
		while (TRUE) {
		    start_col += startmatch.rm_so;
		    startmatch.rm_eo -= startmatch.rm_so;
2487
		    if (regexec(varnish->end, start_line->data +
2488
2489
2490
				start_col + startmatch.rm_eo, 0, NULL,
				(start_col + startmatch.rm_eo == 0) ?
				0 : REG_NOTBOL) == REG_NOMATCH)
2491
2492
2493
			/* No end found after this start. */
			break;
		    start_col++;
2494
		    if (regexec(varnish->start, start_line->data +
2495
2496
				start_col, 1, &startmatch,
				REG_NOTBOL) == REG_NOMATCH)
2497
2498
2499
2500
2501
2502
2503
2504
2505
			/* No later start on this line. */
			goto step_two;
		}
		/* Indeed, there is a start without an end on that line. */

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

2510
2511
2512
2513
		/* If no end was found, or it is too early, next step. */
		if (end_line == NULL)
		    goto step_two;
		if (end_line == fileptr && endmatch.rm_eo <= startpos) {
2514
		    fileptr->multidata[varnish->id] = CBEGINBEFORE;
2515
2516
		    goto step_two;
		}
2517

2518
2519
2520
2521
2522
2523
2524
		/* Now paint the start of fileptr.  If the start of fileptr
		 * is on a different line from the end, paintlen is -1, which
		 * means 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. */
		if (end_line != fileptr) {
		    paintlen = -1;
2525
		    fileptr->multidata[varnish->id] = CWHOLELINE;
2526
#ifdef DEBUG
2527
    fprintf(stderr, "  Marking for id %i  line %i as CWHOLELINE\n", varnish->id, line);
2528
#endif
2529
2530
2531
		} else {
		    paintlen = actual_x(converted, strnlenpt(fileptr->data,
						endmatch.rm_eo) - start);
2532
		    fileptr->multidata[varnish->id] = CBEGINBEFORE;
2533
#ifdef DEBUG
2534
    fprintf(stderr, "  Marking for id %i  line %i as CBEGINBEFORE\n", varnish->id, line);
2535
#endif
2536
2537
2538
2539
2540
2541
		}
		mvwaddnstr(edit, line, 0, converted, paintlen);
		/* If the whole line has been painted, don't bother looking
		 * for any more starts. */
		if (paintlen < 0)
		    goto tail_of_loop;
2542
  step_two:
2543
2544
2545
2546
2547
		/* Second step: look for starts on this line, but start
		 * looking only after an end match, if there is one. */
		start_col = (paintlen == 0) ? 0 : endmatch.rm_eo;

		while (start_col < endpos) {
2548
		    if (regexec(varnish->start, fileptr->data + start_col,
2549
2550
				1, &startmatch, (start_col == 0) ?
				0 : REG_NOTBOL) == REG_NOMATCH ||
2551
				start_col + startmatch.rm_so >= endpos)
2552
2553
			/* No more starts on this line. */
			break;
2554

2555
2556
2557
2558
2559
2560
2561
		    /* Translate the match to be relative to the
		     * beginning of the line. */
		    startmatch.rm_so += start_col;
		    startmatch.rm_eo += start_col;

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

2564
		    index = actual_x(converted, x_start);
2565

2566
		    if (regexec(varnish->end, fileptr->data +
2567
				startmatch.rm_eo, 1, &endmatch,
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
				(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 &&
2578
				endmatch.rm_eo > startmatch.rm_so) {
2579
			    paintlen = actual_x(converted + index,
2580
					strnlenpt(fileptr->data,
2581
					endmatch.rm_eo) - start - x_start);
2582

2583
			    assert(0 <= x_start && x_start < COLS);
2584

2585
			    mvwaddnstr(edit, line, x_start,
2586
					converted + index, paintlen);
2587
			    if (paintlen > 0) {
2588
				fileptr->multidata[varnish->id] = CSTARTENDHERE;
2589
#ifdef DEBUG
2590
    fprintf(stderr, "  Marking for id %i  line %i as CSTARTENDHERE\n", varnish->id, line);
2591
#endif
2592
			    }
2593
2594
			}
			start_col = endmatch.rm_eo;
2595
2596
2597
			/* Skip over a zero-length match. */
			if (endmatch.rm_so == endmatch.rm_eo)
			    start_col += 1;
2598
2599
2600
2601
		    } else {
			/* There is no end on this line.  But we haven't yet
			 * looked for one on later lines. */
			end_line = fileptr->next;
2602

2603
			while (end_line != NULL &&
2604
				regexec(varnish->end, end_line->data,
2605
				0, NULL, 0) == REG_NOMATCH)
2606
			    end_line = end_line->next;
2607

2608
2609
2610
			/* If there is no end, we're done on this line. */
			if (end_line == NULL)
			    break;
2611

2612
2613
2614
2615
			assert(0 <= x_start && x_start < COLS);

			/* Paint the rest of the line. */
			mvwaddnstr(edit, line, x_start, converted + index, -1);
2616
			fileptr->multidata[varnish->id] = CENDAFTER;
2617
#ifdef DEBUG
2618
    fprintf(stderr, "  Marking for id %i  line %i as CENDAFTER\n", varnish->id, line);
2619
#endif
2620
2621
2622
			/* We've painted to the end of the line, so don't
			 * bother checking for any more starts. */
			break;
2623
		    }
2624
2625
		}
	    }
2626
  tail_of_loop:
2627
	    wattroff(edit, A_BOLD);
2628
	    wattroff(edit, COLOR_PAIR(varnish->pairnum));
2629
	}
2630
    }
2631
#endif /* !DISABLE_COLOR */
2632

2633
#ifndef NANO_TINY
2634
    /* If the mark is on, we need to display it. */
2635
    if (openfile->mark_set && (fileptr->lineno <=
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2636
	openfile->mark_begin->lineno || fileptr->lineno <=
2637
	openfile->current->lineno) && (fileptr->lineno >=
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2638
	openfile->mark_begin->lineno || fileptr->lineno >=
2639
	openfile->current->lineno)) {
2640
	/* fileptr is at least partially selected. */
2641
	const filestruct *top;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2642
	    /* Either current or mark_begin, whichever is first. */
2643
	size_t top_x;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2644
	    /* current_x or mark_begin_x, corresponding to top. */
2645
2646
	const filestruct *bot;
	size_t bot_x;
2647
	int x_start;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2648
	    /* Starting column for mvwaddnstr().  Zero-based. */
2649
	int paintlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2650
2651
	    /* Number of characters to paint on this line.  There are
	     * COLS characters on a whole line. */
2652
	size_t index;
2653
	    /* Index in converted where we paint. */
2654

2655
	mark_order(&top, &top_x, &bot, &bot_x, NULL);
2656
2657
2658
2659
2660

	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
2661

2662
	/* The selected bit of fileptr is on this page. */
2663
2664
	if (top_x < endpos && bot_x > startpos) {
	    assert(startpos <= top_x);
2665
2666
2667
2668

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

2670
2671
2672
2673
2674
	    /* 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. */
2675
2676
2677
2678
2679
	    if (bot_x >= endpos)
		paintlen = -1;
	    else
		paintlen = strnlenpt(fileptr->data, bot_x) - (x_start +
			start);
2680
2681
2682
2683
2684
2685
2686
2687

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

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

2691
	    index = actual_x(converted, x_start);
2692

2693
2694
2695
	    if (paintlen > 0)
		paintlen = actual_x(converted + index, paintlen);

2696
	    wattron(edit, hilite_attribute);
2697
	    mvwaddnstr(edit, line, x_start, converted + index,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2698
		paintlen);
2699
	    wattroff(edit, hilite_attribute);
Chris Allegretta's avatar
Chris Allegretta committed
2700
	}
2701
    }
2702
#endif /* !NANO_TINY */
Chris Allegretta's avatar
Chris Allegretta committed
2703
2704
}

2705
/* Just update one line in the edit buffer.  This is basically a wrapper
2706
 * for edit_draw().  The line will be displayed starting with
2707
 * fileptr->data[index].  Likely arguments are current_x or zero.
2708
 * Returns: Number of additional lines consumed (needed for SOFTWRAP). */
2709
int update_line(filestruct *fileptr, size_t index)
Chris Allegretta's avatar
Chris Allegretta committed
2710
{
2711
    int line = 0;
2712
	/* The line in the edit window that we want to update. */
2713
    int extralinesused = 0;
2714
2715
2716
2717
    char *converted;
	/* fileptr->data converted to have tabs and control characters
	 * expanded. */
    size_t page_start;
Chris Allegretta's avatar
Chris Allegretta committed
2718

2719
    assert(fileptr != NULL);
2720

2721
#ifndef NANO_TINY
2722
    if (ISSET(SOFTWRAP)) {
2723
2724
	filestruct *tmp;

2725
2726
	for (tmp = openfile->edittop; tmp && tmp != fileptr; tmp = tmp->next)
	    line += (strlenpt(tmp->data) / COLS) + 1;
2727
    } else
2728
#endif
2729
2730
	line = fileptr->lineno - openfile->edittop->lineno;

2731
    if (line < 0 || line >= editwinrows)
Chris Allegretta's avatar
Chris Allegretta committed
2732
	return 1;
2733

2734
    /* First, blank out the line. */
2735
    blank_line(edit, line, 0, COLS);
2736

2737
2738
    /* Next, convert variables that index the line to their equivalent
     * positions in the expanded line. */
2739
#ifndef NANO_TINY
2740
2741
2742
    if (ISSET(SOFTWRAP))
	index = 0;
    else
2743
#endif
2744
	index = strnlenpt(fileptr->data, index);
2745
    page_start = get_page_start(index);
2746

2747
2748
    /* Expand the line, replacing tabs with spaces, and control
     * characters with their displayed forms. */
2749
2750
2751
#ifdef NANO_TINY
    converted = display_string(fileptr->data, page_start, COLS, TRUE);
#else
2752
2753
2754
    converted = display_string(fileptr->data, page_start, COLS, !ISSET(SOFTWRAP));
#ifdef DEBUG
    if (ISSET(SOFTWRAP) && strlen(converted) >= COLS - 2)
2755
	fprintf(stderr, "update_line(): converted(1) line = %s\n", converted);
2756
#endif
2757
#endif /* !NANO_TINY */
2758

2759
    /* Paint the line. */
2760
    edit_draw(fileptr, converted, line, page_start);
2761
    free(converted);
Chris Allegretta's avatar
Chris Allegretta committed
2762

2763
#ifndef NANO_TINY
2764
    if (!ISSET(SOFTWRAP)) {
2765
#endif
2766
2767
2768
2769
	if (page_start > 0)
	    mvwaddch(edit, line, 0, '$');
	if (strlenpt(fileptr->data) > page_start + COLS)
	    mvwaddch(edit, line, COLS - 1, '$');
2770
#ifndef NANO_TINY
2771
    } else {
2772
	size_t full_length = strlenpt(fileptr->data);
2773
	for (index += COLS; index <= full_length && line < editwinrows - 1; index += COLS) {
2774
2775
	    line++;
#ifdef DEBUG
2776
	    fprintf(stderr, "update_line(): softwrap code, moving to %d index %lu\n", line, (unsigned long)index);
2777
#endif
2778
	    blank_line(edit, line, 0, COLS);
2779
2780

	    /* Expand the line, replacing tabs with spaces, and control
2781
	     * characters with their displayed forms. */
2782
2783
2784
2785
2786
	    converted = display_string(fileptr->data, index, COLS, !ISSET(SOFTWRAP));
#ifdef DEBUG
	    if (ISSET(SOFTWRAP) && strlen(converted) >= COLS - 2)
		fprintf(stderr, "update_line(): converted(2) line = %s\n", converted);
#endif
2787
2788
2789

	    /* Paint the line. */
	    edit_draw(fileptr, converted, line, index);
2790
	    free(converted);
2791
2792
2793
	    extralinesused++;
	}
    }
2794
#endif /* !NANO_TINY */
2795
    return extralinesused;
Chris Allegretta's avatar
Chris Allegretta committed
2796
2797
}

2798
2799
2800
2801
/* Return TRUE if we need an update after moving the cursor, and
 * FALSE otherwise.  We need an update if the mark is on, or if
 * pww_save and placewewant are on different pages. */
bool need_screen_update(size_t pww_save)
2802
2803
{
    return
2804
#ifndef NANO_TINY
2805
	openfile->mark_set ||
2806
#endif
2807
	get_page_start(pww_save) != get_page_start(openfile->placewewant);
2808
2809
}

2810
/* When edittop changes, try and figure out how many lines
2811
 * we really have to work with (i.e. set maxrows). */
2812
2813
2814
2815
2816
void compute_maxrows(void)
{
    int n;
    filestruct *foo = openfile->edittop;

2817
2818
2819
2820
2821
    if (!ISSET(SOFTWRAP)) {
	maxrows = editwinrows;
	return;
    }

2822
2823
    maxrows = 0;
    for (n = 0; n < editwinrows && foo; n++) {
2824
	maxrows++;
2825
	n += strlenpt(foo->data) / COLS;
2826
2827
2828
	foo = foo->next;
    }

2829
2830
2831
    if (n < editwinrows)
	maxrows += editwinrows - n;

2832
#ifdef DEBUG
2833
    fprintf(stderr, "compute_maxrows(): maxrows = %d\n", maxrows);
2834
2835
2836
#endif
}

2837
2838
/* 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
2839
2840
 * scrolling.  direction is the direction to scroll, either UPWARD or
 * DOWNWARD, and nlines is the number of lines to scroll.  We change
2841
2842
 * edittop, and assume that current and current_x are up to date.  We
 * also assume that scrollok(edit) is FALSE. */
2843
void edit_scroll(scroll_dir direction, ssize_t nlines)
2844
{
2845
    ssize_t i;
2846
    filestruct *foo;
2847

2848
    assert(nlines > 0);
2849

2850
2851
2852
    /* Part 1: nlines is the number of lines we're going to scroll the
     * text of the edit window. */

2853
    /* Move the top line of the edit window up or down (depending on the
2854
2855
     * value of direction) nlines lines, or as many lines as we can if
     * there are fewer than nlines lines available. */
2856
    for (i = nlines; i > 0; i--) {
2857
	if (direction == UPWARD) {
2858
	    if (openfile->edittop == openfile->fileage)
2859
		break;
2860
	    openfile->edittop = openfile->edittop->prev;
2861
	} else {
2862
	    if (openfile->edittop == openfile->filebot)
2863
		break;
2864
	    openfile->edittop = openfile->edittop->next;
2865
	}
2866
2867

#ifndef NANO_TINY
2868
	/* Don't over-scroll on long lines. */
2869
	if (ISSET(SOFTWRAP) && direction == UPWARD) {
2870
	    ssize_t len = strlenpt(openfile->edittop->data) / COLS;
2871
	    i -= len;
2872
	    if (len > 0)
2873
		refresh_needed = TRUE;
2874
	}
2875
#endif
2876
2877
    }

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

2881
2882
2883
2884
    /* Don't bother scrolling zero lines, nor more than the window can hold. */
    if (nlines == 0)
	return;
    if (nlines >= editwinrows)
2885
	refresh_needed = TRUE;
2886

2887
    if (refresh_needed == TRUE)
2888
	return;
2889
2890
2891

    /* Scroll the text of the edit window up or down nlines lines,
     * depending on the value of direction. */
2892
    scrollok(edit, TRUE);
2893
    wscrl(edit, (direction == UPWARD) ? -nlines : nlines);
2894
2895
    scrollok(edit, FALSE);

2896
2897
2898
    /* Part 2: nlines is the number of lines in the scrolled region of
     * the edit window that we need to draw. */

2899
2900
    /* If the top or bottom line of the file is now visible in the edit
     * window, we need to draw the entire edit window. */
2901
2902
    if ((direction == UPWARD && openfile->edittop ==
	openfile->fileage) || (direction == DOWNWARD &&
2903
2904
	openfile->edittop->lineno + editwinrows - 1 >=
	openfile->filebot->lineno))
2905
	nlines = editwinrows;
2906

2907
2908
2909
2910
2911
2912
    /* 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
2913

2914
2915
    if (nlines > editwinrows)
	nlines = editwinrows;
2916
2917
2918

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

2921
2922
    /* If we scrolled down, move down to the line before the scrolled
     * region. */
2923
    if (direction == DOWNWARD) {
2924
	for (i = editwinrows - nlines; i > 0 && foo != NULL; i--)
2925
2926
2927
	    foo = foo->next;
    }

2928
2929
2930
2931
2932
2933
    /* 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--) {
2934
2935
	if ((i == nlines && direction == DOWNWARD) || (i == 1 &&
		direction == UPWARD)) {
2936
	    if (need_screen_update(0))
2937
2938
2939
2940
		update_line(foo, (foo == openfile->current) ?
			openfile->current_x : 0);
	} else
	    update_line(foo, (foo == openfile->current) ?
2941
		openfile->current_x : 0);
2942
	foo = foo->next;
2943
    }
2944
    compute_maxrows();
2945
2946
2947
}

/* Update any lines between old_current and current that need to be
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2948
 * updated.  Use this if we've moved without changing any text. */
2949
void edit_redraw(filestruct *old_current)
2950
{
2951
2952
2953
2954
    size_t was_pww = openfile->placewewant;

    openfile->placewewant = xplustabs();

2955
2956
    /* If the current line is offscreen, scroll until it's onscreen. */
    if (openfile->current->lineno >= openfile->edittop->lineno + maxrows ||
2957
		openfile->current->lineno < openfile->edittop->lineno) {
2958
	edit_update((focusing || !ISSET(SMOOTH_SCROLL)) ? CENTERING : FLOWING);
2959
	refresh_needed = TRUE;
2960
    }
2961

2962
#ifndef NANO_TINY
2963
2964
2965
    /* If the mark is on, update all lines between old_current and current. */
    if (openfile->mark_set) {
	filestruct *foo = old_current;
2966

2967
	while (foo != openfile->current) {
2968
	    update_line(foo, 0);
2969

2970
2971
2972
	    foo = (foo->lineno > openfile->current->lineno) ?
			foo->prev : foo->next;
	}
2973
    }
2974
#endif /* !NANO_TINY */
2975

2976
2977
2978
2979
    /* Update old_current only if it differs from current, and if not
     * already done above, and if it was horizontally scrolled. */
    if (old_current != openfile->current && !openfile->mark_set &&
			get_page_start(was_pww) > 0)
2980
	update_line(old_current, 0);
2981
2982
2983
2984
2985

    /* Update current if we've changed page, or if it differs from
     * old_current and needs to be horizontally scrolled. */
    if (need_screen_update(was_pww) || (old_current != openfile->current &&
			get_page_start(openfile->placewewant) > 0))
2986
	update_line(openfile->current, openfile->current_x);
2987
2988
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2989
2990
/* 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
2991
2992
void edit_refresh(void)
{
2993
    filestruct *foo;
2994
    int nlines;
2995

2996
    /* Figure out what maxrows should really be. */
2997
    compute_maxrows();
2998

2999
3000
    if (openfile->current->lineno < openfile->edittop->lineno ||
	openfile->current->lineno >= openfile->edittop->lineno +
3001
3002
	maxrows) {
#ifdef DEBUG
3003
3004
	fprintf(stderr, "edit_refresh(): line = %ld, edittop %ld + maxrows %d\n",
		(long)openfile->current->lineno, (long)openfile->edittop->lineno, maxrows);
3005
3006
#endif

3007
	/* Make sure the current line is on the screen. */
3008
	edit_update((focusing || !ISSET(SMOOTH_SCROLL)) ? CENTERING : STATIONARY);
3009
    }
Chris Allegretta's avatar
Chris Allegretta committed
3010

3011
3012
    foo = openfile->edittop;

3013
#ifdef DEBUG
3014
    fprintf(stderr, "edit_refresh(): edittop->lineno = %ld\n", (long)openfile->edittop->lineno);
3015
#endif
3016

3017
    for (nlines = 0; nlines < editwinrows && foo != NULL; nlines++) {
3018
	nlines += update_line(foo, (foo == openfile->current) ?
3019
		openfile->current_x : 0);
3020
3021
3022
	foo = foo->next;
    }

3023
    for (; nlines < editwinrows; nlines++)
3024
3025
3026
	blank_line(edit, nlines, 0, COLS);

    reset_cursor();
3027
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3028
3029
}

3030
3031
3032
3033
3034
3035
/* Move edittop so that current is on the screen.  manner says how it
 * should be moved: CENTERING means that current should end up in the
 * middle of the screen, STATIONARY means that it should stay at the
 * same vertical position, and FLOWING means that it should scroll no
 * more than needed to bring current into view. */
void edit_update(update_type manner)
Chris Allegretta's avatar
Chris Allegretta committed
3036
{
3037
    int goal = 0;
3038

3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
    /* If manner is CENTERING, move edittop half the number of window
     * lines back from current.  If manner is STATIONARY, move edittop
     * back current_y lines if current_y is in range of the screen,
     * 0 lines if current_y is below zero, or (editwinrows - 1) lines
     * if current_y is too big.  This puts current at the same place
     * on the screen as before, or at the top or bottom if current_y is
     * beyond either.  If manner is FLOWING, move edittop back 0 lines
     * or (editwinrows - 1) lines, depending or where current has moved.
     * This puts the cursor on the first or the last line. */
    if (manner == CENTERING)
3049
	goal = editwinrows / 2;
3050
    else if (manner == FLOWING) {
3051
	if (openfile->current->lineno >= openfile->edittop->lineno)
3052
3053
	    goal = editwinrows - 1;
    } else {
3054
	goal = openfile->current_y;
3055

3056
	/* Limit goal to (editwinrows - 1) lines maximum. */
3057
3058
	if (goal > editwinrows - 1)
	    goal = editwinrows - 1;
Chris Allegretta's avatar
Chris Allegretta committed
3059
    }
3060

3061
3062
3063
3064
3065
    openfile->edittop = openfile->current;

    while (goal > 0 && openfile->edittop->prev != NULL) {
	openfile->edittop = openfile->edittop->prev;
	goal --;
3066
#ifndef NANO_TINY
3067
3068
	if (ISSET(SOFTWRAP))
	    goal -= strlenpt(openfile->edittop->data) / COLS;
3069
#endif
3070
    }
3071
#ifdef DEBUG
3072
    fprintf(stderr, "edit_update(): setting edittop to lineno %ld\n", (long)openfile->edittop->lineno);
3073
#endif
3074
    compute_maxrows();
Chris Allegretta's avatar
Chris Allegretta committed
3075
3076
}

3077
/* Unconditionally redraw the entire screen. */
3078
void total_redraw(void)
3079
{
3080
3081
3082
3083
3084
3085
#ifdef USE_SLANG
    /* Slang curses emulation brain damage, part 4: Slang doesn't define
     * curscr. */
    SLsmg_touch_screen();
    SLsmg_refresh();
#else
3086
    wrefresh(curscr);
3087
#endif
3088
3089
}

3090
3091
/* Unconditionally redraw the entire screen, and then refresh it using
 * the current file. */
3092
3093
void total_refresh(void)
{
3094
    total_redraw();
3095
    titlebar(NULL);
3096
    edit_refresh();
3097
    bottombars(currmenu);
3098
3099
}

3100
3101
/* Display the main shortcut list on the last two rows of the bottom
 * portion of the window. */
3102
3103
void display_main_list(void)
{
3104
#ifndef DISABLE_COLOR
3105
3106
3107
    if (openfile->syntax
	  && (openfile->syntax->formatter || openfile->syntax->linter))
	set_lint_or_format_shortcuts();
3108
3109
3110
3111
    else
	set_spell_shortcuts();
#endif

3112
    bottombars(MMAIN);
3113
3114
}

3115
3116
3117
3118
3119
3120
/* 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. */
3121
void do_cursorpos(bool constant)
Chris Allegretta's avatar
Chris Allegretta committed
3122
{
3123
    filestruct *f;
3124
    char c;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3125
    size_t i, cur_xpt = xplustabs() + 1;
3126
    size_t cur_lenpt = strlenpt(openfile->current->data) + 1;
3127
    int linepct, colpct, charpct;
Chris Allegretta's avatar
Chris Allegretta committed
3128

3129
    assert(openfile->fileage != NULL && openfile->current != NULL);
3130

3131
    f = openfile->current->next;
3132
    c = openfile->current->data[openfile->current_x];
3133
3134

    openfile->current->next = NULL;
3135
    openfile->current->data[openfile->current_x] = '\0';
3136
3137
3138

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

3139
    openfile->current->data[openfile->current_x] = c;
3140
    openfile->current->next = f;
3141

3142
3143
    if (constant && disable_cursorpos) {
	disable_cursorpos = FALSE;
3144
	return;
3145
    }
Chris Allegretta's avatar
Chris Allegretta committed
3146

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3147
    /* Display the current cursor position on the statusbar, and set
3148
     * disable_cursorpos to FALSE. */
3149
    linepct = 100 * openfile->current->lineno / openfile->filebot->lineno;
3150
    colpct = 100 * cur_xpt / cur_lenpt;
3151
    charpct = (openfile->totsize == 0) ? 0 : 100 * i / openfile->totsize;
3152
3153

    statusbar(
3154
	_("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%lu (%d%%)"),
3155
	(long)openfile->current->lineno,
3156
	(long)openfile->filebot->lineno, linepct,
3157
	(unsigned long)cur_xpt, (unsigned long)cur_lenpt, colpct,
3158
	(unsigned long)i, (unsigned long)openfile->totsize, charpct);
3159

3160
    disable_cursorpos = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
3161
3162
}

3163
/* Unconditionally display the current cursor position. */
3164
void do_cursorpos_void(void)
3165
{
3166
    do_cursorpos(FALSE);
3167
3168
}

3169
3170
void enable_nodelay(void)
{
3171
3172
    nodelay_mode = TRUE;
    nodelay(edit, TRUE);
3173
3174
3175
3176
}

void disable_nodelay(void)
{
3177
3178
    nodelay_mode = FALSE;
    nodelay(edit, FALSE);
3179
3180
}

3181
3182
/* Highlight the current word being replaced or spell checked.  We
 * expect word to have tabs and control characters expanded. */
3183
void spotlight(bool active, const char *word)
Chris Allegretta's avatar
Chris Allegretta committed
3184
{
3185
    size_t word_len = strlenpt(word), room;
Chris Allegretta's avatar
Chris Allegretta committed
3186

3187
3188
   /* Compute the number of columns that are available for the word. */
    room = COLS + get_page_start(xplustabs()) - xplustabs();
Chris Allegretta's avatar
Chris Allegretta committed
3189

3190
    assert(room > 0);
3191

3192
3193
    if (word_len > room)
	room--;
3194

Chris Allegretta's avatar
Chris Allegretta committed
3195
    reset_cursor();
3196
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3197

3198
    if (active)
3199
	wattron(edit, hilite_attribute);
Chris Allegretta's avatar
Chris Allegretta committed
3200

3201
    /* This is so we can show zero-length matches. */
3202
    if (word_len == 0)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3203
	waddch(edit, ' ');
3204
    else
3205
	waddnstr(edit, word, actual_x(word, room));
3206

3207
    if (word_len > room)
3208
	waddch(edit, '$');
Chris Allegretta's avatar
Chris Allegretta committed
3209

3210
    if (active)
3211
	wattroff(edit, hilite_attribute);
Chris Allegretta's avatar
Chris Allegretta committed
3212
3213
}

3214
#ifndef DISABLE_EXTRA
3215
3216
#define CREDIT_LEN 54
#define XLCREDIT_LEN 9
3217

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3218
3219
/* Easter egg: Display credits.  Assume nodelay(edit) and scrollok(edit)
 * are FALSE. */
3220
3221
void do_credits(void)
{
3222
3223
    bool old_more_space = ISSET(MORE_SPACE);
    bool old_no_help = ISSET(NO_HELP);
3224
    int kbinput = ERR, crpos = 0, xlpos = 0;
3225
3226
3227
    const char *credits[CREDIT_LEN] = {
	NULL,				/* "The nano text editor" */
	NULL,				/* "version" */
Chris Allegretta's avatar
Chris Allegretta committed
3228
3229
	VERSION,
	"",
3230
	NULL,				/* "Brought to you by:" */
Chris Allegretta's avatar
Chris Allegretta committed
3231
3232
3233
3234
3235
	"Chris Allegretta",
	"Jordi Mallach",
	"Adam Rogoyski",
	"Rob Siemborski",
	"Rocco Corsi",
3236
	"David Lawrence Ramsey",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3237
	"David Benbennick",
Benno Schulenberg's avatar
Benno Schulenberg committed
3238
	"Mark Majeres",
3239
	"Mike Frysinger",
3240
	"Benno Schulenberg",
Chris Allegretta's avatar
Chris Allegretta committed
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
	"Ken Tyler",
	"Sven Guckes",
	"Bill Soudan",
	"Christian Weisgerber",
	"Erik Andersen",
	"Big Gaute",
	"Joshua Jensen",
	"Ryan Krebs",
	"Albert Chin",
	"",
3251
	NULL,				/* "Special thanks to:" */
3252
	"Monique, Brielle & Joseph",
Chris Allegretta's avatar
Chris Allegretta committed
3253
3254
3255
3256
3257
3258
	"Plattsburgh State University",
	"Benet Laboratories",
	"Amy Allegretta",
	"Linda Young",
	"Jeremy Robichaud",
	"Richard Kolb II",
3259
	NULL,				/* "The Free Software Foundation" */
Chris Allegretta's avatar
Chris Allegretta committed
3260
	"Linus Torvalds",
3261
	NULL,				/* "the many translators and the TP" */
3262
	NULL,				/* "For ncurses:" */
3263
3264
3265
3266
	"Thomas Dickey",
	"Pavel Curtis",
	"Zeyd Ben-Halim",
	"Eric S. Raymond",
3267
3268
3269
3270
3271
3272
	NULL,				/* "and anyone else we forgot..." */
	NULL,				/* "Thank you for using nano!" */
	"",
	"",
	"",
	"",
3273
	"(C) 1999 - 2016",
3274
	"Free Software Foundation, Inc.",
3275
3276
3277
3278
	"",
	"",
	"",
	"",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3279
	"http://www.nano-editor.org/"
3280
3281
    };

3282
    const char *xlcredits[XLCREDIT_LEN] = {
3283
3284
3285
3286
3287
	N_("The nano text editor"),
	N_("version"),
	N_("Brought to you by:"),
	N_("Special thanks to:"),
	N_("The Free Software Foundation"),
3288
	N_("the many translators and the TP"),
3289
3290
3291
	N_("For ncurses:"),
	N_("and anyone else we forgot..."),
	N_("Thank you for using nano!")
3292
    };
3293

3294
3295
3296
3297
3298
3299
    if (!old_more_space || !old_no_help) {
	SET(MORE_SPACE);
	SET(NO_HELP);
	window_init();
    }

3300
3301
    curs_set(0);
    nodelay(edit, TRUE);
3302

3303
    blank_titlebar();
3304
    blank_topbar();
Chris Allegretta's avatar
Chris Allegretta committed
3305
    blank_edit();
3306
3307
    blank_statusbar();
    blank_bottombars();
3308

3309
    wrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
3310
    wrefresh(edit);
3311
    wrefresh(bottomwin);
3312
    napms(700);
3313

3314
    for (crpos = 0; crpos < CREDIT_LEN + editwinrows / 2; crpos++) {
3315
	if ((kbinput = wgetch(edit)) != ERR)
3316
	    break;
3317

3318
	if (crpos < CREDIT_LEN) {
3319
	    const char *what;
3320
3321
	    size_t start_x;

3322
	    if (credits[crpos] == NULL) {
3323
		assert(0 <= xlpos && xlpos < XLCREDIT_LEN);
3324

3325
		what = _(xlcredits[xlpos]);
3326
		xlpos++;
3327
	    } else
3328
		what = credits[crpos];
3329

3330
	    start_x = COLS / 2 - strlenpt(what) / 2 - 1;
3331
3332
	    mvwaddstr(edit, editwinrows - 1 - (editwinrows % 2),
		start_x, what);
3333
	}
3334

3335
3336
3337
3338
	wrefresh(edit);

	if ((kbinput = wgetch(edit)) != ERR)
	    break;
3339
	napms(700);
3340

3341
	scrollok(edit, TRUE);
3342
	wscrl(edit, 1);
3343
	scrollok(edit, FALSE);
3344
	wrefresh(edit);
3345

3346
	if ((kbinput = wgetch(edit)) != ERR)
3347
	    break;
3348
	napms(700);
3349

3350
	scrollok(edit, TRUE);
3351
	wscrl(edit, 1);
3352
	scrollok(edit, FALSE);
3353
	wrefresh(edit);
3354
3355
    }

3356
3357
3358
    if (kbinput != ERR)
	ungetch(kbinput);

3359
    if (!old_more_space)
3360
	UNSET(MORE_SPACE);
3361
    if (!old_no_help)
3362
	UNSET(NO_HELP);
3363
    window_init();
3364

3365
    nodelay(edit, FALSE);
3366

3367
    total_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
3368
}
3369
#endif /* !DISABLE_EXTRA */