winio.c 110 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/**************************************************************************
2
 *   winio.c  --  This file is part of GNU nano.                          *
Chris Allegretta's avatar
Chris Allegretta committed
3
 *                                                                        *
4
 *   Copyright (C) 1999-2011, 2013-2017 Free Software Foundation, Inc.    *
5
 *   Copyright (C) 2014, 2015, 2016, 2017 Benno Schulenberg               *
6
 *                                                                        *
7
8
9
10
 *   GNU nano is free software: you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published    *
 *   by the Free Software Foundation, either version 3 of the License,    *
 *   or (at your option) any later version.                               *
Chris Allegretta's avatar
Chris Allegretta committed
11
 *                                                                        *
12
13
14
15
 *   GNU nano is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty          *
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
 *   See the GNU General Public License for more details.                 *
Chris Allegretta's avatar
Chris Allegretta committed
16
17
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
18
 *   along with this program.  If not, see http://www.gnu.org/licenses/.  *
Chris Allegretta's avatar
Chris Allegretta committed
19
20
21
 *                                                                        *
 **************************************************************************/

22
#include "proto.h"
23
#include "revision.h"
24

25
#ifdef __linux__
26
#include <sys/ioctl.h>
27
#endif
28

Chris Allegretta's avatar
Chris Allegretta committed
29
#include <string.h>
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
30
#include <ctype.h>
Chris Allegretta's avatar
Chris Allegretta committed
31

32
33
34
35
36
37
#ifdef REVISION
#define BRANDING REVISION
#else
#define BRANDING PACKAGE_STRING
#endif

38
static int *key_buffer = NULL;
39
40
	/* The keystroke buffer, containing all the keystrokes we
	 * haven't handled yet at a given point. */
41
static size_t key_buffer_len = 0;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
42
	/* The length of the keystroke buffer. */
43
44
static bool solitary = FALSE;
	/* Whether an Esc arrived by itself -- not as leader of a sequence. */
45
static bool waiting_mode = TRUE;
46
	/* Whether getting a character will wait for a key to be pressed. */
47
static int statusblank = 0;
48
	/* The number of keystrokes left before we blank the statusbar. */
49
#ifdef USING_OLD_NCURSES
50
51
static bool seen_wide = FALSE;
	/* Whether we've seen a multicolumn character in the current line. */
52
#endif
53

54
55
/* Control character compatibility:
 *
56
57
58
59
60
 * - Ctrl-H is Backspace under ASCII, ANSI, VT100, and VT220.
 * - Ctrl-I is Tab under ASCII, ANSI, VT100, VT220, and VT320.
 * - Ctrl-M is Enter under ASCII, ANSI, VT100, VT220, and VT320.
 * - Ctrl-Q is XON under ASCII, ANSI, VT100, VT220, and VT320.
 * - Ctrl-S is XOFF under ASCII, ANSI, VT100, VT220, and VT320.
61
62
 * - Ctrl-8 (Ctrl-?) is Delete under ASCII, ANSI, VT100, and VT220,
 *          but is Backspace under VT320.
63
 *
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
 * console, the FreeBSD console, the Mach console, xterm, rxvt, Eterm,
79
80
 * and Terminal, and some for iTerm2.  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
    int input;
114
    size_t errcount = 0;
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
126
    input = wgetch(win);

127
#ifndef NANO_TINY
128
    if (the_window_resized) {
129
	ungetch(input);
130
	regenerate_screen();
131
	input = KEY_WINCH;
132
    }
133
134
#endif

135
    if (input == ERR && !waiting_mode)
136
137
138
139
140
141
142
143
	return;

    while (input == ERR) {
	/* If we've failed to get a character MAX_BUF_SIZE times in a row,
	 * assume our input source is gone and die gracefully.  We could
	 * check if errno is set to EIO ("Input/output error") and die in
	 * that case, but it's not always set properly.  Argh. */
	if (++errcount == MAX_BUF_SIZE)
144
	    die(_("Too many errors from stdin"));
145

146
#ifndef NANO_TINY
147
148
	if (the_window_resized) {
	    regenerate_screen();
149
150
	    input = KEY_WINCH;
	    break;
151
	}
152
153
#endif
	input = wgetch(win);
154
    }
155

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

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

169
170
171
172
    /* Read in the remaining characters using non-blocking input. */
    nodelay(win, TRUE);

    while (TRUE) {
173
	input = wgetch(win);
174

175
	/* If there aren't any more characters, stop reading. */
176
	if (input == ERR)
177
178
	    break;

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

187
    /* Restore waiting mode if it was on. */
188
    if (waiting_mode)
189
	nodelay(win, FALSE);
190
191

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
202
/* Return the length of the keystroke buffer. */
203
size_t get_key_buffer_len(void)
204
205
206
207
{
    return key_buffer_len;
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
208
/* Add the keystrokes in input to the keystroke buffer. */
209
void unget_input(int *input, size_t input_len)
210
211
{
    /* If input is empty, get out. */
212
    if (input_len == 0)
213
214
	return;

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
221
222
223
    /* 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. */
224
225
226
    key_buffer_len += input_len;
    key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
	sizeof(int));
227

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
234
    /* Copy input to the beginning of the keystroke buffer. */
235
    memcpy(key_buffer, input, input_len * sizeof(int));
236
237
}

238
239
240
/* Put the character given in kbinput back into the input stream.  If it
 * is a Meta key, also insert an Escape character in front of it. */
void unget_kbinput(int kbinput, bool metakey)
241
{
242
    unget_input(&kbinput, 1);
243

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

250
/* Try to read input_len codes from the keystroke buffer.  If the
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
251
 * keystroke buffer is empty and win isn't NULL, try to read in more
252
 * codes from win and add them to the keystroke buffer before doing
253
 * anything else.  If the keystroke buffer is (still) empty, return NULL. */
254
int *get_input(WINDOW *win, size_t input_len)
255
{
256
    int *input;
257

258
259
    if (key_buffer_len == 0 && win != NULL)
	get_key_buffer(win);
260

261
262
    if (key_buffer_len == 0)
	return NULL;
263

264
    /* Limit the request to the number of available codes in the buffer. */
265
266
267
    if (input_len > key_buffer_len)
	input_len = key_buffer_len;

268
    /* Copy input_len codes from the head of the keystroke buffer. */
269
270
    input = (int *)nmalloc(input_len * sizeof(int));
    memcpy(input, key_buffer, input_len * sizeof(int));
271
    key_buffer_len -= input_len;
272

273
    /* If the keystroke buffer is now empty, mark it as such. */
274
275
276
277
    if (key_buffer_len == 0) {
	free(key_buffer);
	key_buffer = NULL;
    } else {
278
	/* Trim from the buffer the codes that were copied. */
279
	memmove(key_buffer, key_buffer + input_len, key_buffer_len *
280
281
282
		sizeof(int));
	key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
		sizeof(int));
283
284
285
    }

    return input;
286
287
}

288
/* Read in a single keystroke, ignoring any that are invalid. */
289
int get_kbinput(WINDOW *win)
290
{
291
    int kbinput = ERR;
292

293
    /* Extract one keystroke from the input stream. */
294
295
    while (kbinput == ERR)
	kbinput = parse_kbinput(win);
296

297
298
299
300
301
#ifdef DEBUG
    fprintf(stderr, "after parsing:  kbinput = %d, meta_key = %s\n",
	kbinput, meta_key ? "TRUE" : "FALSE");
#endif

302
    /* If we read from the edit window, blank the statusbar if needed. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
303
    if (win == edit)
304
305
	check_statusblank();

306
307
308
    return kbinput;
}

309
310
/* Extract a single keystroke from the input stream.  Translate escape
 * sequences and extended keypad codes into their corresponding values.
311
 * Set meta_key to TRUE when appropriate.  Supported extended keypad values
312
313
314
 * are: [arrow key], Ctrl-[arrow key], Shift-[arrow key], Enter, Backspace,
 * the editing keypad (Insert, Delete, Home, End, PageUp, and PageDown),
 * the function keys (F1-F16), and the numeric keypad with NumLock off. */
315
int parse_kbinput(WINDOW *win)
316
{
317
    static int escapes = 0, byte_digits = 0;
318
    static bool double_esc = FALSE;
319
    int *kbinput, keycode, retval = ERR;
320

321
    meta_key = FALSE;
322
    shift_held = FALSE;
323

324
    /* Read in a character. */
325
326
    kbinput = get_input(win, 1);

327
    if (kbinput == NULL && !waiting_mode)
328
329
330
	return 0;

    while (kbinput == NULL)
331
	kbinput = get_input(win, 1);
332

333
334
335
    keycode = *kbinput;
    free(kbinput);

336
337
338
339
340
#ifdef DEBUG
    fprintf(stderr, "before parsing:  keycode = %d, escapes = %d, byte_digits = %d\n",
	keycode, escapes, byte_digits);
#endif

341
342
343
    if (keycode == ERR)
	return ERR;

344
    if (keycode == ESC_CODE) {
345
346
347
348
349
350
351
	/* Increment the escape counter, but trim an overabundance. */
	escapes++;
	if (escapes > 3)
	    escapes = 1;
	/* Take note when an Esc arrived by itself. */
	solitary = (escapes == 1 && key_buffer_len == 0);
	return ERR;
352
353
    }

354
355
356
357
358
359
    switch (escapes) {
	case 0:
	    /* One non-escape: normal input mode. */
	    retval = keycode;
	    break;
	case 1:
360
361
362
	    if (keycode >= 0x80)
		retval = keycode;
	    else if ((keycode != 'O' && keycode != 'o' && keycode != '[') ||
363
			key_buffer_len == 0 || *key_buffer == ESC_CODE) {
364
365
366
367
368
369
370
371
372
		/* One escape followed by a single non-escape:
		 * meta key sequence mode. */
		if (!solitary || (keycode >= 0x20 && keycode < 0x7F))
		    meta_key = TRUE;
		retval = tolower(keycode);
	    } else
		/* One escape followed by a non-escape, and there
		 * are more codes waiting: escape sequence mode. */
		retval = parse_escape_sequence(win, keycode);
373
	    escapes = 0;
374
375
376
	    break;
	case 2:
	    if (double_esc) {
377
378
		/* An "ESC ESC [ X" sequence from Option+arrow, or
		 * an "ESC ESC [ x" sequence from Shift+Alt+arrow. */
379
380
381
382
383
384
385
386
		switch (keycode) {
		    case 'A':
			retval = KEY_HOME;
			break;
		    case 'B':
			retval = KEY_END;
			break;
		    case 'C':
387
			retval = CONTROL_RIGHT;
388
389
			break;
		    case 'D':
390
			retval = CONTROL_LEFT;
391
			break;
392
#ifndef NANO_TINY
393
394
395
396
397
398
399
400
401
402
403
404
		    case 'a':
			retval = shiftaltup;
			break;
		    case 'b':
			retval = shiftaltdown;
			break;
		    case 'c':
			retval = shiftaltright;
			break;
		    case 'd':
			retval = shiftaltleft;
			break;
405
#endif
406
407
408
		}
		double_esc = FALSE;
		escapes = 0;
409
	    } else if (key_buffer_len == 0) {
410
411
412
413
414
415
416
417
418
419
420
421
422
423
		if (('0' <= keycode && keycode <= '2' &&
			byte_digits == 0) || ('0' <= keycode &&
			keycode <= '9' && byte_digits > 0)) {
		    /* Two escapes followed by one or more decimal
		     * digits, and there aren't any other codes
		     * waiting: byte sequence mode.  If the range
		     * of the byte sequence is limited to 2XX (the
		     * first digit is between '0' and '2' and the
		     * others between '0' and '9', interpret it. */
		    int byte;

		    byte_digits++;
		    byte = get_byte_kbinput(keycode);

424
425
		    /* If the decimal byte value is complete, convert it and
		     * put the obtained byte(s) back into the input buffer. */
426
427
428
429
		    if (byte != ERR) {
			char *byte_mb;
			int byte_mb_len, *seq, i;

430
431
			/* Convert the decimal code to one or two bytes. */
			byte_mb = make_mbchar((long)byte, &byte_mb_len);
432

433
			seq = (int *)nmalloc(byte_mb_len * sizeof(int));
434
435
436
437

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

438
			/* Insert the byte(s) into the input buffer. */
439
440
441
442
			unget_input(seq, byte_mb_len);

			free(byte_mb);
			free(seq);
443
444
445

			byte_digits = 0;
			escapes = 0;
446
		    }
447
448
449
450
451
452
453
454
455
456
457
458
459
		} else {
		    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)
			 * and there aren't any other codes waiting:
			 * control character sequence mode. */
			retval = get_control_kbinput(keycode);
		    else {
			/* An invalid digit in the middle of a byte
			 * sequence: reset the byte sequence counter
			 * and save the code we got as the result. */
			byte_digits = 0;
460
			retval = keycode;
461
		    }
462
		    escapes = 0;
463
464
		}
	    } else if (keycode == '[' && key_buffer_len > 0 &&
465
466
467
			(('A' <= *key_buffer && *key_buffer <= 'D') ||
			('a' <= *key_buffer && *key_buffer <= 'd'))) {
		/* An iTerm2/Eterm/rxvt sequence: ^[ ^[ [ X. */
468
469
470
471
472
		double_esc = TRUE;
	    } else {
		/* Two escapes followed by a non-escape, and there are more
		 * codes waiting: combined meta and escape sequence mode. */
		retval = parse_escape_sequence(win, keycode);
473
474
		meta_key = TRUE;
		escapes = 0;
475
	    }
476
477
	    break;
	case 3:
478
	    if (key_buffer_len == 0)
479
480
481
482
483
484
485
486
487
488
		/* Three escapes followed by a non-escape, and no
		 * other codes are waiting: normal input mode. */
		retval = keycode;
	    else
		/* Three escapes followed by a non-escape, and more
		 * codes are waiting: combined control character and
		 * escape sequence mode.  First interpret the escape
		 * sequence, then the result as a control sequence. */
		retval = get_control_kbinput(
			parse_escape_sequence(win, keycode));
489
	    escapes = 0;
490
491
	    break;
    }
492

493
494
495
    if (retval == ERR)
	return ERR;

496
    if (retval == controlleft)
497
	return CONTROL_LEFT;
498
    else if (retval == controlright)
499
	return CONTROL_RIGHT;
500
    else if (retval == controlup)
501
	return CONTROL_UP;
502
    else if (retval == controldown)
503
	return CONTROL_DOWN;
504
505
506
507
    else if (retval == controlhome)
	return CONTROL_HOME;
    else if (retval == controlend)
	return CONTROL_END;
508
#ifndef NANO_TINY
509
510
    else if (retval == shiftcontrolleft) {
	shift_held = TRUE;
511
	return CONTROL_LEFT;
512
513
    } else if (retval == shiftcontrolright) {
	shift_held = TRUE;
514
	return CONTROL_RIGHT;
515
516
    } else if (retval == shiftcontrolup) {
	shift_held = TRUE;
517
	return CONTROL_UP;
518
519
    } else if (retval == shiftcontroldown) {
	shift_held = TRUE;
520
	return CONTROL_DOWN;
521
522
523
524
525
526
    } else if (retval == shiftcontrolhome) {
	shift_held = TRUE;
	return CONTROL_HOME;
    } else if (retval == shiftcontrolend) {
	shift_held = TRUE;
	return CONTROL_END;
527
528
    } else if (retval == shiftaltleft) {
	shift_held = TRUE;
529
	return KEY_HOME;
530
531
    } else if (retval == shiftaltright) {
	shift_held = TRUE;
532
	return KEY_END;
533
534
    } else if (retval == shiftaltup) {
	shift_held = TRUE;
535
	return KEY_PPAGE;
536
537
    } else if (retval == shiftaltdown) {
	shift_held = TRUE;
538
	return KEY_NPAGE;
539
    }
540
541
#endif

542
#ifdef __linux__
543
    /* When not running under X, check for the bare arrow keys whether
544
545
546
547
     * Shift/Ctrl/Alt are being held together with them. */
    unsigned char modifiers = 6;

    if (console && ioctl(0, TIOCLINUX, &modifiers) >= 0) {
548
549
550
551
552
#ifndef NANO_TINY
	/* Is Shift being held? */
	if (modifiers & 0x01)
	    shift_held = TRUE;
#endif
553
554
	/* Is Ctrl being held? */
	if (modifiers & 0x04) {
555
	    if (retval == KEY_UP)
556
		return CONTROL_UP;
557
	    else if (retval == KEY_DOWN)
558
		return CONTROL_DOWN;
559
	    else if (retval == KEY_LEFT)
560
		return CONTROL_LEFT;
561
	    else if (retval == KEY_RIGHT)
562
		return CONTROL_RIGHT;
563
564
565
566
	    else if (retval == KEY_HOME)
		return CONTROL_HOME;
	    else if (retval == KEY_END)
		return CONTROL_END;
567
	}
568
#ifndef NANO_TINY
569
570
571
	/* Are both Shift and Alt being held? */
	if ((modifiers & 0x09) == 0x09) {
	    if (retval == KEY_UP)
572
		return KEY_PPAGE;
573
	    else if (retval == KEY_DOWN)
574
		return KEY_NPAGE;
575
	    else if (retval == KEY_LEFT)
576
		return KEY_HOME;
577
	    else if (retval == KEY_RIGHT)
578
		return KEY_END;
579
	}
580
#endif
581
    }
582
#endif /* __linux__ */
583

584
    switch (retval) {
585
#ifdef KEY_SLEFT
586
587
	/* Slang doesn't support KEY_SLEFT. */
	case KEY_SLEFT:
588
	    shift_held = TRUE;
589
	    return KEY_LEFT;
590
#endif
591
#ifdef KEY_SRIGHT
592
593
	/* Slang doesn't support KEY_SRIGHT. */
	case KEY_SRIGHT:
594
	    shift_held = TRUE;
595
	    return KEY_RIGHT;
596
#endif
597
#ifdef KEY_SR
598
#ifdef KEY_SUP
599
600
	/* ncurses and Slang don't support KEY_SUP. */
	case KEY_SUP:
601
#endif
602
603
	case KEY_SR:	/* Scroll backward, on Xfce4-terminal. */
	    shift_held = TRUE;
604
	    return KEY_UP;
605
606
#endif
#ifdef KEY_SF
607
#ifdef KEY_SDOWN
608
609
	/* ncurses and Slang don't support KEY_SDOWN. */
	case KEY_SDOWN:
610
#endif
611
612
	case KEY_SF:	/* Scroll forward, on Xfce4-terminal. */
	    shift_held = TRUE;
613
	    return KEY_DOWN;
614
#endif
615
#ifdef KEY_SHOME
616
617
	/* HP-UX 10-11 and Slang don't support KEY_SHOME. */
	case KEY_SHOME:
618
#endif
619
620
	case SHIFT_HOME:
	    shift_held = TRUE;
621
	case KEY_A1:	/* Home (7) on keypad with NumLock off. */
622
	    return KEY_HOME;
623
#ifdef KEY_SEND
624
625
	/* HP-UX 10-11 and Slang don't support KEY_SEND. */
	case KEY_SEND:
626
#endif
627
628
	case SHIFT_END:
	    shift_held = TRUE;
629
	case KEY_C1:	/* End (1) on keypad with NumLock off. */
630
	    return KEY_END;
631
#ifndef NANO_TINY
632
633
634
635
#ifdef KEY_SPREVIOUS
	case KEY_SPREVIOUS:
#endif
	case SHIFT_PAGEUP:	/* Fake key, from Shift+Alt+Up. */
636
637
	    shift_held = TRUE;
#endif
638
	case KEY_A3:	/* PageUp (9) on keypad with NumLock off. */
639
	    return KEY_PPAGE;
640
#ifndef NANO_TINY
641
642
643
#ifdef KEY_SNEXT
	case KEY_SNEXT:
#endif
644
645
646
	case SHIFT_PAGEDOWN:	/* Fake key, from Shift+Alt+Down. */
	    shift_held = TRUE;
#endif
647
	case KEY_C3:	/* PageDown (3) on keypad with NumLock off. */
648
	    return KEY_NPAGE;
649
#ifdef KEY_SDC
650
651
	/* Slang doesn't support KEY_SDC. */
	case KEY_SDC:
652
#endif
653
	case DEL_CODE:
654
	    if (ISSET(REBIND_DELETE))
655
		return the_code_for(do_delete, KEY_DC);
656
	    else
657
		return KEY_BACKSPACE;
658
#ifdef KEY_SIC
659
660
	/* Slang doesn't support KEY_SIC. */
	case KEY_SIC:
661
	    return the_code_for(do_insertfile_void, KEY_IC);
662
#endif
663
#ifdef KEY_SBEG
664
665
	/* Slang doesn't support KEY_SBEG. */
	case KEY_SBEG:
666
#endif
667
#ifdef KEY_BEG
668
669
	/* Slang doesn't support KEY_BEG. */
	case KEY_BEG:
670
#endif
671
672
	case KEY_B2:	/* Center (5) on keypad with NumLock off. */
	    return ERR;
673
#ifdef KEY_CANCEL
674
#ifdef KEY_SCANCEL
675
676
	/* Slang doesn't support KEY_SCANCEL. */
	case KEY_SCANCEL:
677
#endif
678
679
	/* Slang doesn't support KEY_CANCEL. */
	case KEY_CANCEL:
680
	    return the_code_for(do_cancel, 0x03);
681
#endif
682
#ifdef KEY_SUSPEND
683
#ifdef KEY_SSUSPEND
684
685
	/* Slang doesn't support KEY_SSUSPEND. */
	case KEY_SSUSPEND:
686
#endif
687
688
	/* Slang doesn't support KEY_SUSPEND. */
	case KEY_SUSPEND:
689
	    return the_code_for(do_suspend_void, KEY_SUSPEND);
690
691
#endif
#ifdef PDCURSES
692
693
694
695
696
697
698
	case KEY_SHIFT_L:
	case KEY_SHIFT_R:
	case KEY_CONTROL_L:
	case KEY_CONTROL_R:
	case KEY_ALT_L:
	case KEY_ALT_R:
	    return ERR;
699
#endif
700
701
#ifdef KEY_RESIZE
	/* Slang and SunOS 5.7-5.9 don't support KEY_RESIZE. */
702
	case KEY_RESIZE:
703
#endif
704
705
706
707
#if defined(USE_SLANG) && defined(ENABLE_UTF8)
	case KEY_BAD:
#endif
	case KEY_FLUSH:
708
	    return ERR;
709
    }
710

711
712
713
    return retval;
}

714
/* Translate escape sequences, most of which correspond to extended
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
715
 * keypad values, into their corresponding key values.  These sequences
716
717
 * are generated when the keypad doesn't support the needed keys.
 * Assume that Escape has already been read in. */
718
int convert_sequence(const int *seq, size_t seq_len)
719
{
720
    if (seq_len > 1) {
721
	switch (seq[0]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
722
	    case 'O':
723
		switch (seq[1]) {
724
		    case '1':
725
726
			if (seq_len > 4  && seq[2] == ';') {

727
728
	switch (seq[3]) {
	    case '2':
729
730
731
732
733
		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. */
734
			shift_held = TRUE;
735
736
737
738
739
740
741
742
743
744
			return arrow_from_abcd(seq[4]);
		    case 'P': /* Esc O 1 ; 2 P == F13 on Terminal. */
			return KEY_F(13);
		    case 'Q': /* Esc O 1 ; 2 Q == F14 on Terminal. */
			return KEY_F(14);
		    case 'R': /* Esc O 1 ; 2 R == F15 on Terminal. */
			return KEY_F(15);
		    case 'S': /* Esc O 1 ; 2 S == F16 on Terminal. */
			return KEY_F(16);
		}
745
746
		break;
	    case '5':
747
748
749
750
751
752
753
754
755
756
		switch (seq[4]) {
		    case 'A': /* Esc O 1 ; 5 A == Ctrl-Up on Terminal. */
			return CONTROL_UP;
		    case 'B': /* Esc O 1 ; 5 B == Ctrl-Down on Terminal. */
			return CONTROL_DOWN;
		    case 'C': /* Esc O 1 ; 5 C == Ctrl-Right on Terminal. */
			return CONTROL_RIGHT;
		    case 'D': /* Esc O 1 ; 5 D == Ctrl-Left on Terminal. */
			return CONTROL_LEFT;
		}
757
758
		break;
	}
759

760
761
			}
			break;
762
		    case '2':
763
			if (seq_len >= 3) {
764
			    switch (seq[2]) {
765
				case 'P': /* Esc O 2 P == F13 on xterm. */
766
				    return KEY_F(13);
767
				case 'Q': /* Esc O 2 Q == F14 on xterm. */
768
				    return KEY_F(14);
769
				case 'R': /* Esc O 2 R == F15 on xterm. */
770
				    return KEY_F(15);
771
				case 'S': /* Esc O 2 S == F16 on xterm. */
772
				    return KEY_F(16);
773
774
			    }
			}
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
775
			break;
776
777
778
779
780
781
782
783
784
785
786
787
788
		    case '5':
			if (seq_len >= 3) {
			    switch (seq[2]) {
				case 'A': /* Esc O 5 A == Ctrl-Up on Haiku. */
				    return CONTROL_UP;
				case 'B': /* Esc O 5 B == Ctrl-Down on Haiku. */
				    return CONTROL_DOWN;
				case 'C': /* Esc O 5 C == Ctrl-Right on Haiku. */
				    return CONTROL_RIGHT;
				case 'D': /* Esc O 5 D == Ctrl-Left on Haiku. */
				    return CONTROL_LEFT;
			    }
			}
789
		    case 'A': /* Esc O A == Up on VT100/VT320/xterm. */
790
791
792
		    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. */
793
			return arrow_from_abcd(seq[1]);
794
795
		    case 'E': /* Esc O E == Center (5) on numeric keypad
			       * with NumLock off on xterm. */
796
			return KEY_B2;
797
		    case 'F': /* Esc O F == End on xterm/Terminal. */
798
			return KEY_END;
799
		    case 'H': /* Esc O H == Home on xterm/Terminal. */
800
			return KEY_HOME;
801
		    case 'M': /* Esc O M == Enter on numeric keypad with
802
			       * NumLock off on VT100/VT220/VT320/xterm/
803
			       * rxvt/Eterm. */
804
			return KEY_ENTER;
805
		    case 'P': /* Esc O P == F1 on VT100/VT220/VT320/Mach
806
			       * console. */
807
			return KEY_F(1);
808
		    case 'Q': /* Esc O Q == F2 on VT100/VT220/VT320/Mach
809
			       * console. */
810
			return KEY_F(2);
811
		    case 'R': /* Esc O R == F3 on VT100/VT220/VT320/Mach
812
			       * console. */
813
			return KEY_F(3);
814
		    case 'S': /* Esc O S == F4 on VT100/VT220/VT320/Mach
815
			       * console. */
816
			return KEY_F(4);
817
		    case 'T': /* Esc O T == F5 on Mach console. */
818
			return KEY_F(5);
819
		    case 'U': /* Esc O U == F6 on Mach console. */
820
			return KEY_F(6);
821
		    case 'V': /* Esc O V == F7 on Mach console. */
822
			return KEY_F(7);
823
		    case 'W': /* Esc O W == F8 on Mach console. */
824
			return KEY_F(8);
825
		    case 'X': /* Esc O X == F9 on Mach console. */
826
			return KEY_F(9);
827
		    case 'Y': /* Esc O Y == F10 on Mach console. */
828
			return KEY_F(10);
829
		    case 'a': /* Esc O a == Ctrl-Up on rxvt. */
830
			return CONTROL_UP;
831
		    case 'b': /* Esc O b == Ctrl-Down on rxvt. */
832
			return CONTROL_DOWN;
833
		    case 'c': /* Esc O c == Ctrl-Right on rxvt. */
834
			return CONTROL_RIGHT;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
835
		    case 'd': /* Esc O d == Ctrl-Left on rxvt. */
836
			return CONTROL_LEFT;
837
		    case 'j': /* Esc O j == '*' on numeric keypad with
838
			       * NumLock off on VT100/VT220/VT320/xterm/
839
			       * rxvt/Eterm/Terminal. */
840
			return '*';
841
		    case 'k': /* Esc O k == '+' on numeric keypad with
842
			       * NumLock off on VT100/VT220/VT320/xterm/
843
			       * rxvt/Eterm/Terminal. */
844
			return '+';
845
		    case 'l': /* Esc O l == ',' on numeric keypad with
846
			       * NumLock off on VT100/VT220/VT320/xterm/
847
			       * rxvt/Eterm/Terminal. */
848
			return ',';
849
		    case 'm': /* Esc O m == '-' on numeric keypad with
850
			       * NumLock off on VT100/VT220/VT320/xterm/
851
			       * rxvt/Eterm/Terminal. */
852
			return '-';
853
		    case 'n': /* Esc O n == Delete (.) on numeric keypad
854
			       * with NumLock off on VT100/VT220/VT320/
855
			       * xterm/rxvt/Eterm/Terminal. */
856
			return KEY_DC;
857
		    case 'o': /* Esc O o == '/' on numeric keypad with
858
			       * NumLock off on VT100/VT220/VT320/xterm/
859
			       * rxvt/Eterm/Terminal. */
860
			return '/';
861
		    case 'p': /* Esc O p == Insert (0) on numeric keypad
862
			       * with NumLock off on VT100/VT220/VT320/
863
			       * rxvt/Eterm/Terminal. */
864
			return KEY_IC;
865
		    case 'q': /* Esc O q == End (1) on numeric keypad
866
			       * with NumLock off on VT100/VT220/VT320/
867
			       * rxvt/Eterm/Terminal. */
868
			return KEY_END;
869
		    case 'r': /* Esc O r == Down (2) on numeric keypad
870
			       * with NumLock off on VT100/VT220/VT320/
871
			       * rxvt/Eterm/Terminal. */
872
			return KEY_DOWN;
873
		    case 's': /* Esc O s == PageDown (3) on numeric
874
			       * keypad with NumLock off on VT100/VT220/
875
			       * VT320/rxvt/Eterm/Terminal. */
876
			return KEY_NPAGE;
877
		    case 't': /* Esc O t == Left (4) on numeric keypad
878
			       * with NumLock off on VT100/VT220/VT320/
879
			       * rxvt/Eterm/Terminal. */
880
			return KEY_LEFT;
881
		    case 'u': /* Esc O u == Center (5) on numeric keypad
882
883
			       * with NumLock off on VT100/VT220/VT320/
			       * rxvt/Eterm. */
884
			return KEY_B2;
885
		    case 'v': /* Esc O v == Right (6) on numeric keypad
886
			       * with NumLock off on VT100/VT220/VT320/
887
			       * rxvt/Eterm/Terminal. */
888
			return KEY_RIGHT;
889
		    case 'w': /* Esc O w == Home (7) on numeric keypad
890
			       * with NumLock off on VT100/VT220/VT320/
891
			       * rxvt/Eterm/Terminal. */
892
			return KEY_HOME;
893
		    case 'x': /* Esc O x == Up (8) on numeric keypad
894
			       * with NumLock off on VT100/VT220/VT320/
895
			       * rxvt/Eterm/Terminal. */
896
			return KEY_UP;
897
		    case 'y': /* Esc O y == PageUp (9) on numeric keypad
898
			       * with NumLock off on VT100/VT220/VT320/
899
			       * rxvt/Eterm/Terminal. */
900
			return KEY_PPAGE;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
901
902
903
		}
		break;
	    case 'o':
904
		switch (seq[1]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
905
		    case 'a': /* Esc o a == Ctrl-Up on Eterm. */
906
			return CONTROL_UP;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
907
		    case 'b': /* Esc o b == Ctrl-Down on Eterm. */
908
			return CONTROL_DOWN;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
909
		    case 'c': /* Esc o c == Ctrl-Right on Eterm. */
910
			return CONTROL_RIGHT;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
911
		    case 'd': /* Esc o d == Ctrl-Left on Eterm. */
912
			return CONTROL_LEFT;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
913
914
915
		}
		break;
	    case '[':
916
		switch (seq[1]) {
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
917
		    case '1':
918
			if (seq_len > 3 && seq[3] == '~') {
919
			    switch (seq[2]) {
920
				case '1': /* Esc [ 1 1 ~ == F1 on rxvt/Eterm. */
921
				    return KEY_F(1);
922
				case '2': /* Esc [ 1 2 ~ == F2 on rxvt/Eterm. */
923
				    return KEY_F(2);
924
				case '3': /* Esc [ 1 3 ~ == F3 on rxvt/Eterm. */
925
				    return KEY_F(3);
926
				case '4': /* Esc [ 1 4 ~ == F4 on rxvt/Eterm. */
927
				    return KEY_F(4);
928
929
				case '5': /* Esc [ 1 5 ~ == F5 on xterm/
					   * rxvt/Eterm. */
930
				    return KEY_F(5);
931
				case '7': /* Esc [ 1 7 ~ == F6 on
932
933
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
934
				    return KEY_F(6);
935
				case '8': /* Esc [ 1 8 ~ == F7 on
936
937
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
938
				    return KEY_F(7);
939
				case '9': /* Esc [ 1 9 ~ == F8 on
940
941
					   * VT220/VT320/Linux console/
					   * xterm/rxvt/Eterm. */
942
				    return KEY_F(8);
943
944
945
			    }
			} else if (seq_len > 4 && seq[2] == ';') {

946
	switch (seq[3]) {
947
	    case '2':
948
949
950
951
952
		switch (seq[4]) {
		    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. */
953
			shift_held = TRUE;
954
955
			return arrow_from_abcd(seq[4]);
		}
956
		break;
957
958
959
960
961
962
963
964
965
966
#ifndef NANO_TINY
	    case '4':
		/* When the arrow keys are held together with Shift+Meta,
		 * act as if they are Home/End/PgUp/PgDown with Shift. */
		switch (seq[4]) {
		    case 'A': /* Esc [ 1 ; 4 A == Shift-Alt-Up on xterm. */
			return SHIFT_PAGEUP;
		    case 'B': /* Esc [ 1 ; 4 B == Shift-Alt-Down on xterm. */
			return SHIFT_PAGEDOWN;
		    case 'C': /* Esc [ 1 ; 4 C == Shift-Alt-Right on xterm. */
967
			return SHIFT_END;
968
		    case 'D': /* Esc [ 1 ; 4 D == Shift-Alt-Left on xterm. */
969
			return SHIFT_HOME;
970
971
972
		}
		break;
#endif
973
	    case '5':
974
975
976
977
978
979
980
981
982
		switch (seq[4]) {
		    case 'A': /* Esc [ 1 ; 5 A == Ctrl-Up on xterm. */
			return CONTROL_UP;
		    case 'B': /* Esc [ 1 ; 5 B == Ctrl-Down on xterm. */
			return CONTROL_DOWN;
		    case 'C': /* Esc [ 1 ; 5 C == Ctrl-Right on xterm. */
			return CONTROL_RIGHT;
		    case 'D': /* Esc [ 1 ; 5 D == Ctrl-Left on xterm. */
			return CONTROL_LEFT;
983
984
985
986
		    case 'F': /* Esc [ 1 ; 5 F == Ctrl-End on xterm. */
			return CONTROL_END;
		    case 'H': /* Esc [ 1 ; 5 H == Ctrl-Home on xterm. */
			return CONTROL_HOME;
987
		}
988
		break;
989
990
991
992
993
994
995
996
997
998
999
#ifndef NANO_TINY
	    case '6':
		switch (seq[4]) {
		    case 'A': /* Esc [ 1 ; 6 A == Shift-Ctrl-Up on xterm. */
			return shiftcontrolup;
		    case 'B': /* Esc [ 1 ; 6 B == Shift-Ctrl-Down on xterm. */
			return shiftcontroldown;
		    case 'C': /* Esc [ 1 ; 6 C == Shift-Ctrl-Right on xterm. */
			return shiftcontrolright;
		    case 'D': /* Esc [ 1 ; 6 D == Shift-Ctrl-Left on xterm. */
			return shiftcontrolleft;
1000
1001
1002
1003
		    case 'F': /* Esc [ 1 ; 6 F == Shift-Ctrl-End on xterm. */
			return shiftcontrolend;
		    case 'H': /* Esc [ 1 ; 6 H == Shift-Ctrl-Home on xterm. */
			return shiftcontrolhome;
1004
1005
1006
		}
		break;
#endif
1007
	}
1008
1009
1010
1011

			} else if (seq_len > 2 && seq[2] == '~')
			    /* Esc [ 1 ~ == Home on VT320/Linux console. */
			    return KEY_HOME;
1012
1013
			break;
		    case '2':
1014
			if (seq_len > 3 && seq[3] == '~') {
1015
			    switch (seq[2]) {
1016
1017
				case '0': /* Esc [ 2 0 ~ == F9 on VT220/VT320/
					   * Linux console/xterm/rxvt/Eterm. */
1018
				    return KEY_F(9);
1019
1020
				case '1': /* Esc [ 2 1 ~ == F10 on VT220/VT320/
					   * Linux console/xterm/rxvt/Eterm. */
1021
				    return KEY_F(10);
1022
1023
				case '3': /* Esc [ 2 3 ~ == F11 on VT220/VT320/
					   * Linux console/xterm/rxvt/Eterm. */
1024
				    return KEY_F(11);
1025
1026
				case '4': /* Esc [ 2 4 ~ == F12 on VT220/VT320/
					   * Linux console/xterm/rxvt/Eterm. */
1027
				    return KEY_F(12);
1028
1029
				case '5': /* Esc [ 2 5 ~ == F13 on VT220/VT320/
					   * Linux console/rxvt/Eterm. */
1030
				    return KEY_F(13);
1031
1032
				case '6': /* Esc [ 2 6 ~ == F14 on VT220/VT320/
					   * Linux console/rxvt/Eterm. */
1033
				    return KEY_F(14);
1034
1035
				case '8': /* Esc [ 2 8 ~ == F15 on VT220/VT320/
					   * Linux console/rxvt/Eterm. */
1036
				    return KEY_F(15);
1037
1038
				case '9': /* Esc [ 2 9 ~ == F16 on VT220/VT320/
					   * Linux console/rxvt/Eterm. */
1039
				    return KEY_F(16);
1040
			    }
1041
1042
1043
1044
			} else if (seq_len > 2 && seq[2] == '~')
			    /* Esc [ 2 ~ == Insert on VT220/VT320/
			     * Linux console/xterm/Terminal. */
			    return KEY_IC;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1045
			break;
1046
		    case '3': /* Esc [ 3 ~ == Delete on VT220/VT320/
1047
			       * Linux console/xterm/Terminal. */
1048
1049
1050
			if (seq_len > 2 && seq[2] == '~')
			    return KEY_DC;
			break;
1051
		    case '4': /* Esc [ 4 ~ == End on VT220/VT320/Linux
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1052
			       * console/xterm. */
1053
1054
1055
			if (seq_len > 2 && seq[2] == '~')
			    return KEY_END;
			break;
1056
		    case '5': /* Esc [ 5 ~ == PageUp on VT220/VT320/
1057
1058
			       * Linux console/xterm/Terminal;
			       * Esc [ 5 ^ == PageUp on Eterm. */
1059
1060
1061
			if (seq_len > 2 && (seq[2] == '~' || seq[2] == '^'))
			    return KEY_PPAGE;
			break;
1062
		    case '6': /* Esc [ 6 ~ == PageDown on VT220/VT320/
1063
			       * Linux console/xterm/Terminal;
1064
			       * Esc [ 6 ^ == PageDown on Eterm. */
1065
1066
1067
			if (seq_len > 2 && (seq[2] == '~' || seq[2] == '^'))
			    return KEY_NPAGE;
			break;
1068
1069
1070
1071
		    case '7': /* Esc [ 7 ~ == Home on Eterm/rxvt;
			       * Esc [ 7 $ == Shift-Home on Eterm/rxvt;
			       * Esc [ 7 ^ == Control-Home on Eterm/rxvt;
			       * Esc [ 7 @ == Shift-Control-Home on same. */
1072
1073
1074
1075
			if (seq_len > 2 && seq[2] == '~')
			    return KEY_HOME;
			else if (seq_len > 2 && seq[2] == '$')
			    return SHIFT_HOME;
1076
1077
1078
1079
1080
1081
			else if (seq_len > 2 && seq[2] == '^')
			    return CONTROL_HOME;
#ifndef NANO_TINY
			else if (seq_len > 2 && seq[2] == '@')
			    return shiftcontrolhome;
#endif
1082
			break;
1083
1084
1085
1086
		    case '8': /* Esc [ 8 ~ == End on Eterm/rxvt;
			       * Esc [ 8 $ == Shift-End on Eterm/rxvt;
			       * Esc [ 8 ^ == Control-End on Eterm/rxvt;
			       * Esc [ 8 @ == Shift-Control-End on same. */
1087
1088
1089
1090
			if (seq_len > 2 && seq[2] == '~')
			    return KEY_END;
			else if (seq_len > 2 && seq[2] == '$')
			    return SHIFT_END;
1091
1092
1093
1094
1095
1096
			else if (seq_len > 2 && seq[2] == '^')
			    return CONTROL_END;
#ifndef NANO_TINY
			else if (seq_len > 2 && seq[2] == '@')
			    return shiftcontrolend;
#endif
1097
			break;
1098
		    case '9': /* Esc [ 9 == Delete on Mach console. */
1099
			return KEY_DC;
1100
		    case '@': /* Esc [ @ == Insert on Mach console. */
1101
			return KEY_IC;
1102
		    case 'A': /* Esc [ A == Up on ANSI/VT220/Linux
1103
			       * console/FreeBSD console/Mach console/
1104
			       * rxvt/Eterm/Terminal. */
1105
		    case 'B': /* Esc [ B == Down on ANSI/VT220/Linux
1106
			       * console/FreeBSD console/Mach console/
1107
			       * rxvt/Eterm/Terminal. */
1108
		    case 'C': /* Esc [ C == Right on ANSI/VT220/Linux
1109
			       * console/FreeBSD console/Mach console/
1110
			       * rxvt/Eterm/Terminal. */
1111
		    case 'D': /* Esc [ D == Left on ANSI/VT220/Linux
1112
			       * console/FreeBSD console/Mach console/
1113
			       * rxvt/Eterm/Terminal. */
1114
			return arrow_from_abcd(seq[1]);
1115
		    case 'E': /* Esc [ E == Center (5) on numeric keypad
1116
1117
			       * with NumLock off on FreeBSD console/
			       * Terminal. */
1118
			return KEY_B2;
1119
		    case 'F': /* Esc [ F == End on FreeBSD console/Eterm. */
1120
			return KEY_END;
1121
		    case 'G': /* Esc [ G == PageDown on FreeBSD console. */
1122
			return KEY_NPAGE;
1123
		    case 'H': /* Esc [ H == Home on ANSI/VT220/FreeBSD
1124
			       * console/Mach console/Eterm. */
1125
			return KEY_HOME;
1126
		    case 'I': /* Esc [ I == PageUp on FreeBSD console. */
1127
			return KEY_PPAGE;
1128
		    case 'L': /* Esc [ L == Insert on ANSI/FreeBSD console. */
1129
			return KEY_IC;
1130
		    case 'M': /* Esc [ M == F1 on FreeBSD console. */
1131
			return KEY_F(1);
1132
		    case 'N': /* Esc [ N == F2 on FreeBSD console. */
1133
			return KEY_F(2);
1134
		    case 'O':
1135
			if (seq_len > 2) {
1136
			    switch (seq[2]) {
1137
				case 'P': /* Esc [ O P == F1 on xterm. */
1138
				    return KEY_F(1);
1139
				case 'Q': /* Esc [ O Q == F2 on xterm. */
1140
				    return KEY_F(2);
1141
				case 'R': /* Esc [ O R == F3 on xterm. */
1142
				    return KEY_F(3);
1143
				case 'S': /* Esc [ O S == F4 on xterm. */
1144
				    return KEY_F(4);
1145
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1146
			} else
1147
			    /* Esc [ O == F3 on FreeBSD console. */
1148
			    return KEY_F(3);
1149
1150
			break;
		    case 'P': /* Esc [ P == F4 on FreeBSD console. */
1151
			return KEY_F(4);
1152
		    case 'Q': /* Esc [ Q == F5 on FreeBSD console. */
1153
			return KEY_F(5);
1154
		    case 'R': /* Esc [ R == F6 on FreeBSD console. */
1155
			return KEY_F(6);
1156
		    case 'S': /* Esc [ S == F7 on FreeBSD console. */
1157
			return KEY_F(7);
1158
		    case 'T': /* Esc [ T == F8 on FreeBSD console. */
1159
			return KEY_F(8);
1160
		    case 'U': /* Esc [ U == PageDown on Mach console. */
1161
			return KEY_NPAGE;
1162
		    case 'V': /* Esc [ V == PageUp on Mach console. */
1163
			return KEY_PPAGE;
1164
		    case 'W': /* Esc [ W == F11 on FreeBSD console. */
1165
			return KEY_F(11);
1166
		    case 'X': /* Esc [ X == F12 on FreeBSD console. */
1167
			return KEY_F(12);
1168
		    case 'Y': /* Esc [ Y == End on Mach console. */
1169
			return KEY_END;
1170
		    case 'Z': /* Esc [ Z == F14 on FreeBSD console. */
1171
			return KEY_F(14);
1172
		    case 'a': /* Esc [ a == Shift-Up on rxvt/Eterm. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1173
		    case 'b': /* Esc [ b == Shift-Down on rxvt/Eterm. */
1174
		    case 'c': /* Esc [ c == Shift-Right on rxvt/Eterm. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1175
		    case 'd': /* Esc [ d == Shift-Left on rxvt/Eterm. */
1176
			shift_held = TRUE;
1177
			return arrow_from_abcd(seq[1]);
1178
		    case '[':
1179
			if (seq_len > 2 ) {
1180
			    switch (seq[2]) {
1181
1182
				case 'A': /* Esc [ [ A == F1 on Linux
					   * console. */
1183
				    return KEY_F(1);
1184
1185
				case 'B': /* Esc [ [ B == F2 on Linux
					   * console. */
1186
				    return KEY_F(2);
1187
1188
				case 'C': /* Esc [ [ C == F3 on Linux
					   * console. */
1189
				    return KEY_F(3);
1190
1191
				case 'D': /* Esc [ [ D == F4 on Linux
					   * console. */
1192
				    return KEY_F(4);
1193
1194
				case 'E': /* Esc [ [ E == F5 on Linux
					   * console. */
1195
				    return KEY_F(5);
1196
1197
			    }
			}
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1198
1199
1200
1201
			break;
		}
		break;
	}
1202
1203
    }

1204
    return ERR;
1205
1206
}

1207
1208
/* Return the equivalent arrow-key value for the first four letters
 * in the alphabet, common to many escape sequences. */
1209
int arrow_from_abcd(int kbinput)
1210
1211
1212
{
    switch (tolower(kbinput)) {
	case 'a':
1213
	    return KEY_UP;
1214
	case 'b':
1215
	    return KEY_DOWN;
1216
	case 'c':
1217
	    return KEY_RIGHT;
1218
	case 'd':
1219
	    return KEY_LEFT;
1220
1221
1222
1223
1224
	default:
	    return ERR;
    }
}

1225
/* Interpret the escape sequence in the keystroke buffer, the first
1226
1227
 * character of which is kbinput.  Assume that the keystroke buffer
 * isn't empty, and that the initial escape has already been read in. */
1228
int parse_escape_sequence(WINDOW *win, int kbinput)
1229
1230
1231
1232
1233
1234
1235
1236
{
    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);
1237
    seq_len = key_buffer_len;
1238
    seq = get_input(NULL, seq_len);
1239
    retval = convert_sequence(seq, seq_len);
1240
1241
1242

    free(seq);

1243
    /* If we got an unrecognized escape sequence, notify the user. */
1244
    if (retval == ERR) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1245
	if (win == edit) {
1246
1247
	    /* TRANSLATORS: This refers to a sequence of escape codes
	     * (from the keyboard) that nano does not know about. */
1248
	    statusline(ALERT, _("Unknown sequence"));
1249
	    suppress_cursorpos = FALSE;
1250
	    lastmessage = HUSH;
1251
	    if (currmenu == MMAIN) {
1252
		place_the_cursor();
1253
1254
		curs_set(1);
	    }
1255
1256
1257
	}
    }

1258
#ifdef DEBUG
1259
1260
    fprintf(stderr, "parse_escape_sequence(): kbinput = %d, seq_len = %lu, retval = %d\n",
		kbinput, (unsigned long)seq_len, retval);
1261
1262
1263
1264
1265
#endif

    return retval;
}

1266
1267
/* Translate a byte sequence: turn a three-digit decimal number (from
 * 000 to 255) into its corresponding byte value. */
1268
int get_byte_kbinput(int kbinput)
1269
{
1270
    static int byte_digits = 0, byte = 0;
1271
    int retval = ERR;
1272

1273
1274
    /* Increment the byte digit counter. */
    byte_digits++;
1275

1276
    switch (byte_digits) {
1277
	case 1:
1278
1279
	    /* First digit: This must be from zero to two.  Put it in
	     * the 100's position of the byte sequence holder. */
1280
	    if ('0' <= kbinput && kbinput <= '2')
1281
		byte = (kbinput - '0') * 100;
1282
	    else
1283
1284
		/* This isn't the start of a byte sequence.  Return this
		 * character as the result. */
1285
1286
1287
		retval = kbinput;
	    break;
	case 2:
1288
1289
1290
1291
	    /* 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. */
1292
1293
1294
	    if (('0' <= kbinput && kbinput <= '5') || (byte < 200 &&
		'6' <= kbinput && kbinput <= '9'))
		byte += (kbinput - '0') * 10;
1295
	    else
1296
1297
		/* This isn't the second digit of a byte sequence.
		 * Return this character as the result. */
1298
1299
1300
		retval = kbinput;
	    break;
	case 3:
1301
	    /* Third digit: This must be from zero to five if the first
1302
1303
1304
	     * 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. */
1305
1306
	    if (('0' <= kbinput && kbinput <= '5') || (byte < 250 &&
		'6' <= kbinput && kbinput <= '9')) {
1307
		byte += kbinput - '0';
1308
		/* The byte sequence is complete. */
1309
		retval = byte;
1310
	    } else
1311
1312
		/* This isn't the third digit of a byte sequence.
		 * Return this character as the result. */
1313
1314
		retval = kbinput;
	    break;
1315
	default:
1316
1317
1318
	    /* If there are more than three digits, return this
	     * character as the result.  (Maybe we should produce an
	     * error instead?) */
1319
1320
1321
1322
1323
1324
1325
1326
	    retval = kbinput;
	    break;
    }

    /* If we have a result, reset the byte digit counter and the byte
     * sequence holder. */
    if (retval != ERR) {
	byte_digits = 0;
1327
	byte = 0;
1328
1329
1330
    }

#ifdef DEBUG
1331
    fprintf(stderr, "get_byte_kbinput(): kbinput = %d, byte_digits = %d, byte = %d, retval = %d\n", kbinput, byte_digits, byte, retval);
1332
1333
1334
1335
1336
#endif

    return retval;
}

1337
#ifdef ENABLE_UTF8
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1338
/* If the character in kbinput is a valid hexadecimal digit, multiply it
1339
 * by factor and add the result to uni, and return ERR to signify okay. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1340
1341
1342
1343
1344
1345
1346
long add_unicode_digit(int kbinput, long factor, long *uni)
{
    if ('0' <= kbinput && kbinput <= '9')
	*uni += (kbinput - '0') * factor;
    else if ('a' <= tolower(kbinput) && tolower(kbinput) <= 'f')
	*uni += (tolower(kbinput) - 'a' + 10) * factor;
    else
1347
1348
	/* The character isn't hexadecimal; give it as the result. */
	return (long)kbinput;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1349

1350
    return ERR;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1351
1352
}

1353
/* Translate a Unicode sequence: turn a six-digit hexadecimal number
1354
 * (from 000000 to 10FFFF, case-insensitive) into its corresponding
1355
 * multibyte value. */
1356
long get_unicode_kbinput(WINDOW *win, int kbinput)
1357
{
1358
1359
1360
    static int uni_digits = 0;
    static long uni = 0;
    long retval = ERR;
1361

1362
    /* Increment the Unicode digit counter. */
1363
    uni_digits++;
1364

1365
    switch (uni_digits) {
1366
	case 1:
1367
1368
1369
1370
	    /* The first digit must be zero or one.  Put it in the
	     * 0x100000's position of the Unicode sequence holder.
	     * Otherwise, return the character itself as the result. */
	    if (kbinput == '0' || kbinput == '1')
1371
		uni = (kbinput - '0') * 0x100000;
1372
1373
1374
1375
	    else
		retval = kbinput;
	    break;
	case 2:
1376
1377
1378
	    /* The second digit must be zero if the first was one, but
	     * may be any hexadecimal value if the first was zero. */
	    if (kbinput == '0' || uni == 0)
1379
		retval = add_unicode_digit(kbinput, 0x10000, &uni);
1380
1381
1382
1383
	    else
		retval = kbinput;
	    break;
	case 3:
1384
	    /* Later digits may be any hexadecimal value. */
1385
	    retval = add_unicode_digit(kbinput, 0x1000, &uni);
1386
	    break;
1387
	case 4:
1388
	    retval = add_unicode_digit(kbinput, 0x100, &uni);
1389
	    break;
1390
	case 5:
1391
	    retval = add_unicode_digit(kbinput, 0x10, &uni);
1392
	    break;
1393
	case 6:
1394
	    retval = add_unicode_digit(kbinput, 0x1, &uni);
1395
1396
	    /* If also the sixth digit was a valid hexadecimal value, then
	     * the Unicode sequence is complete, so return it. */
1397
	    if (retval == ERR)
1398
		retval = uni;
1399
1400
	    break;
    }
1401

1402
    /* Show feedback only when editing, not when at a prompt. */
1403
    if (retval == ERR && win == edit) {
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
	char partial[7] = "......";

	/* Construct the partial result, right-padding it with dots. */
	snprintf(partial, uni_digits + 1, "%06lX", uni);
	partial[uni_digits] = '.';

	/* TRANSLATORS: This is shown while a six-digit hexadecimal
	 * Unicode character code (%s) is being typed in. */
	statusline(HUSH, _("Unicode Input: %s"), partial);
    }
1414

1415
#ifdef DEBUG
1416
1417
    fprintf(stderr, "get_unicode_kbinput(): kbinput = %d, uni_digits = %d, uni = %ld, retval = %ld\n",
						kbinput, uni_digits, uni, retval);
1418
1419
#endif

1420
1421
1422
1423
    /* If we have an end result, reset the Unicode digit counter. */
    if (retval != ERR)
	uni_digits = 0;

1424
1425
    return retval;
}
1426
#endif /* ENABLE_UTF8 */
1427

1428
1429
1430
1431
1432
1433
/* Translate a control character sequence: turn an ASCII non-control
 * character into its corresponding control character. */
int get_control_kbinput(int kbinput)
{
    int retval;

1434
    /* Ctrl-Space (Ctrl-2, Ctrl-@, Ctrl-`) */
1435
    if (kbinput == ' ' || kbinput == '2')
1436
	retval = 0;
1437
1438
    /* Ctrl-/ (Ctrl-7, Ctrl-_) */
    else if (kbinput == '/')
1439
	retval = 31;
1440
    /* Ctrl-3 (Ctrl-[, Esc) to Ctrl-7 (Ctrl-/, Ctrl-_) */
1441
1442
1443
    else if ('3' <= kbinput && kbinput <= '7')
	retval = kbinput - 24;
    /* Ctrl-8 (Ctrl-?) */
1444
    else if (kbinput == '8' || kbinput == '?')
1445
	retval = DEL_CODE;
1446
1447
    /* Ctrl-@ (Ctrl-Space, Ctrl-2, Ctrl-`) to Ctrl-_ (Ctrl-/, Ctrl-7) */
    else if ('@' <= kbinput && kbinput <= '_')
1448
	retval = kbinput - '@';
1449
1450
    /* Ctrl-` (Ctrl-2, Ctrl-Space, Ctrl-@) to Ctrl-~ (Ctrl-6, Ctrl-^) */
    else if ('`' <= kbinput && kbinput <= '~')
1451
	retval = kbinput - '`';
1452
1453
1454
    else
	retval = kbinput;

1455
#ifdef DEBUG
1456
    fprintf(stderr, "get_control_kbinput(): kbinput = %d, retval = %d\n", kbinput, retval);
1457
1458
#endif

1459
1460
    return retval;
}
1461

1462
/* Read in a stream of characters verbatim, and return the length of the
1463
1464
1465
1466
 * string in kbinput_len.  Assume nodelay(win) is FALSE. */
int *get_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
{
    int *retval;
1467

1468
    /* Turn off flow control characters if necessary so that we can type
1469
1470
     * them in verbatim, and turn the keypad off if necessary so that we
     * don't get extended keypad values. */
1471
1472
    if (ISSET(PRESERVE))
	disable_flow_control();
1473
1474
    if (!ISSET(REBIND_KEYPAD))
	keypad(win, FALSE);
1475

1476
    /* Read in one keycode, or one or two escapes. */
1477
    retval = parse_verbatim_kbinput(win, kbinput_len);
1478

1479
    /* If the code is invalid in the current mode, discard it. */
1480
1481
    if (retval != NULL && ((*retval == '\n' && as_an_at) ||
				(*retval == '\0' && !as_an_at))) {
1482
1483
1484
1485
	*kbinput_len = 0;
	beep();
    }

1486
    /* Turn flow control characters back on if necessary and turn the
1487
     * keypad back on if necessary now that we're done. */
1488
1489
    if (ISSET(PRESERVE))
	enable_flow_control();
1490
1491
1492
1493
1494
1495
    /* Use the global window pointers, because a resize may have freed
     * the data that the win parameter points to. */
    if (!ISSET(REBIND_KEYPAD)) {
	keypad(edit, TRUE);
	keypad(bottomwin, TRUE);
    }
1496

1497
    return retval;
1498
1499
}

1500
1501
1502
1503
/* Read in one control character (or an iTerm/Eterm/rxvt double Escape),
 * or convert a series of six digits into a Unicode codepoint.  Return
 * in count either 1 (for a control character or the first byte of a
 * multibyte sequence), or 2 (for an iTerm/Eterm/rxvt double Escape). */
1504
int *parse_verbatim_kbinput(WINDOW *win, size_t *count)
1505
{
1506
    int *kbinput;
1507

1508
    /* Read in the first code. */
1509
1510
    while ((kbinput = get_input(win, 1)) == NULL)
	;
1511

1512
#ifndef NANO_TINY
1513
1514
1515
    /* When the window was resized, abort and return nothing. */
    if (*kbinput == KEY_WINCH) {
	free(kbinput);
1516
	*count = 0;
1517
1518
	return NULL;
    }
1519
#endif
1520

1521
1522
    *count = 1;

1523
#ifdef ENABLE_UTF8
1524
    if (using_utf8()) {
1525
	/* Check whether the first code is a valid starter digit: 0 or 1. */
1526
	long unicode = get_unicode_kbinput(win, *kbinput);
1527

1528
	/* If the first code isn't the digit 0 nor 1, put it back. */
1529
	if (unicode != ERR)
1530
	    unget_input(kbinput, 1);
1531
1532
	/* Otherwise, continue reading in digits until we have a complete
	 * Unicode value, and put back the corresponding byte(s). */
1533
	else {
1534
	    char *multibyte;
1535
	    int onebyte, i;
1536

1537
	    while (unicode == ERR) {
1538
		free(kbinput);
1539
1540
		while ((kbinput = get_input(win, 1)) == NULL)
		    ;
1541
		unicode = get_unicode_kbinput(win, *kbinput);
1542
	    }
1543

1544
	    /* Convert the Unicode value to a multibyte sequence. */
1545
	    multibyte = make_mbchar(unicode, (int *)count);
1546

1547
	    /* Insert the multibyte sequence into the input buffer. */
1548
	    for (i = *count; i > 0 ; i--) {
1549
		onebyte = (unsigned char)multibyte[i - 1];
1550
1551
		unget_input(&onebyte, 1);
	    }
1552

1553
	    free(multibyte);
1554
	}
1555
    } else
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1556
#endif /* ENABLE_UTF8 */
1557
	/* Put back the first code. */
1558
	unget_input(kbinput, 1);
1559

1560
1561
    free(kbinput);

1562
    /* If this is an iTerm/Eterm/rxvt double escape, take both Escapes. */
1563
1564
1565
1566
1567
    if (key_buffer_len > 3 && *key_buffer == ESC_CODE &&
		key_buffer[1] == ESC_CODE && key_buffer[2] == '[')
	*count = 2;

    return get_input(NULL, *count);
1568
1569
}

1570
#ifdef ENABLE_MOUSE
1571
/* Handle any mouse event that may have occurred.  We currently handle
1572
1573
1574
1575
1576
1577
1578
 * 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
1579
1580
1581
1582
1583
1584
 * 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. */
1585
int get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
1586
1587
{
    MEVENT mevent;
1588
    bool in_bottomwin;
1589
    subnfunc *f;
1590
1591
1592
1593
1594
1595

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

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

1598
    /* Save the screen coordinates where the mouse event took place. */
1599
    *mouse_x = mevent.x - margin;
1600
    *mouse_y = mevent.y;
1601

1602
1603
    in_bottomwin = wenclose(bottomwin, *mouse_y, *mouse_x);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1604
    /* Handle releases/clicks of the first mouse button. */
1605
    if (mevent.bstate & (BUTTON1_RELEASED | BUTTON1_CLICKED)) {
1606
1607
	/* If we're allowing shortcuts, the current shortcut list is
	 * being displayed on the last two lines of the screen, and the
1608
1609
1610
	 * 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. */
1611
	if (allow_shortcuts && !ISSET(NO_HELP) && in_bottomwin) {
1612
1613
1614
1615
	    int i;
		/* The width of all the shortcuts, except for the last
		 * two, in the shortcut list in bottomwin. */
	    int j;
1616
		/* The calculated index number of the clicked item. */
1617
1618
	    size_t number;
		/* The number of available shortcuts in the current menu. */
1619

1620
1621
1622
1623
1624
1625
1626
1627
1628
	    /* 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. */
1629
		*mouse_x = mevent.x - margin;
1630
1631
1632
1633
		*mouse_y = mevent.y;

		return 0;
	    }
1634

1635
	    /* Determine how many shortcuts are being shown. */
1636
	    number = length_of_list(currmenu);
1637

1638
1639
	    if (number > MAIN_VISIBLE)
		number = MAIN_VISIBLE;
1640

1641
1642
1643
	    /* 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. */
1644
	    if (number < 2)
1645
1646
		i = COLS / (MAIN_VISIBLE / 2);
	    else
1647
		i = COLS / ((number / 2) + (number % 2));
1648

1649
1650
	    /* Calculate the one-based index in the shortcut list. */
	    j = (*mouse_x / i) * 2 + *mouse_y;
1651

1652
	    /* Adjust the index if we hit the last two wider ones. */
1653
	    if ((j > number) && (*mouse_x % i < COLS % i))
1654
		j -= 2;
1655
1656
1657
#ifdef DEBUG
	    fprintf(stderr, "Calculated %i as index in shortcut list, currmenu = %x.\n", j, currmenu);
#endif
1658
1659
	    /* Ignore releases/clicks of the first mouse button beyond
	     * the last shortcut. */
1660
	    if (j > number)
1661
		return 2;
1662

1663
1664
	    /* Go through the list of functions to determine which
	     * shortcut in the current menu we released/clicked on. */
1665
	    for (f = allfuncs; f != NULL; f = f->next) {
1666
		if ((f->menus & currmenu) == 0)
1667
1668
		    continue;
		if (first_sc_for(currmenu, f->scfunc) == NULL)
1669
		    continue;
1670
1671
		/* Tick off an actually shown shortcut. */
		j -= 1;
1672
1673
		if (j == 0)
		    break;
1674
	    }
1675
#ifdef DEBUG
1676
	    fprintf(stderr, "Stopped on func %ld present in menus %x\n", (long)f->scfunc, f->menus);
1677
#endif
1678

1679
	    /* And put the corresponding key into the keyboard buffer. */
1680
	    if (f != NULL) {
1681
		const sc *s = first_sc_for(currmenu, f->scfunc);
1682
		unget_kbinput(s->keycode, s->meta);
1683
	    }
1684
	    return 1;
1685
	} else
1686
1687
	    /* Handle releases/clicks of the first mouse button that
	     * aren't on the current shortcut list elsewhere. */
1688
	    return 0;
1689
    }
1690
1691
#if NCURSES_MOUSE_VERSION >= 2
    /* Handle presses of the fourth mouse button (upward rolls of the
1692
1693
1694
     * 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
1695
	bool in_edit = wenclose(edit, *mouse_y, *mouse_x);
1696

1697
1698
1699
1700
	if (in_bottomwin)
	    /* Translate the mouse event coordinates so that they're
	     * relative to bottomwin. */
	    wmouse_trafo(bottomwin, mouse_y, mouse_x, FALSE);
1701

1702
	if (in_edit || (in_bottomwin && *mouse_y == 0)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1703
1704
	    int i;

1705
1706
1707
1708
1709
	    /* 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) ?
1710
				KEY_PPAGE : KEY_NPAGE, FALSE);
1711
1712
1713
1714
1715
1716
1717

	    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;
1718
1719
    }
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1720
1721
1722

    /* Ignore all other mouse events. */
    return 2;
1723
}
1724
#endif /* ENABLE_MOUSE */
1725

1726
1727
1728
1729
/* 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. */
1730
const sc *get_shortcut(int *kbinput)
1731
{
1732
    sc *s;
1733

1734
#ifdef DEBUG
1735
1736
    fprintf(stderr, "get_shortcut(): kbinput = %d, meta_key = %s -- ",
				*kbinput, meta_key ? "TRUE" : "FALSE");
1737
1738
#endif

1739
    for (s = sclist; s != NULL; s = s->next) {
1740
	if ((s->menus & currmenu) && *kbinput == s->keycode &&
1741
					meta_key == s->meta) {
1742
#ifdef DEBUG
1743
1744
	    fprintf (stderr, "matched seq '%s'  (menu is %x from %x)\n",
				s->keystr, currmenu, s->menus);
1745
#endif
1746
	    return s;
1747
1748
	}
    }
1749
#ifdef DEBUG
1750
    fprintf (stderr, "matched nothing\n");
1751
#endif
1752
1753
1754
1755

    return NULL;
}

1756
1757
/* Move to (x, y) in win, and display a line of n spaces with the
 * current attributes. */
1758
void blank_row(WINDOW *win, int y, int x, int n)
1759
1760
{
    wmove(win, y, x);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1761

1762
1763
1764
1765
    for (; n > 0; n--)
	waddch(win, ' ');
}

1766
/* Blank the first line of the top portion of the window. */
1767
void blank_titlebar(void)
Chris Allegretta's avatar
Chris Allegretta committed
1768
{
1769
    blank_row(topwin, 0, 0, COLS);
1770
1771
}

1772
/* Blank all the lines of the middle portion of the window, i.e. the
1773
 * edit window. */
Chris Allegretta's avatar
Chris Allegretta committed
1774
1775
void blank_edit(void)
{
1776
    int row;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1777

1778
1779
    for (row = 0; row < editwinrows; row++)
	blank_row(edit, row, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1780
1781
}

1782
/* Blank the first line of the bottom portion of the window. */
Chris Allegretta's avatar
Chris Allegretta committed
1783
1784
void blank_statusbar(void)
{
1785
    blank_row(bottomwin, 0, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1786
1787
}

1788
1789
/* If the NO_HELP flag isn't set, blank the last two lines of the bottom
 * portion of the window. */
1790
1791
void blank_bottombars(void)
{
1792
    if (!ISSET(NO_HELP) && LINES > 4) {
1793
1794
	blank_row(bottomwin, 1, 0, COLS);
	blank_row(bottomwin, 2, 0, COLS);
1795
1796
1797
    }
}

1798
1799
/* Check if the number of keystrokes needed to blank the statusbar has
 * been pressed.  If so, blank the statusbar, unless constant cursor
1800
 * position display is on and we are in the editing screen. */
1801
void check_statusblank(void)
Chris Allegretta's avatar
Chris Allegretta committed
1802
{
1803
1804
1805
1806
1807
1808
    if (statusblank == 0)
	return;

    statusblank--;

    /* When editing and 'constantshow' is active, skip the blanking. */
1809
    if (currmenu == MMAIN && ISSET(CONSTANT_SHOW))
1810
1811
1812
1813
1814
	return;

    if (statusblank == 0) {
	blank_statusbar();
	wnoutrefresh(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
1815
    }
1816
1817
1818
1819

    /* If the subwindows overlap, make sure to show the edit window now. */
    if (LINES == 1)
	edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
1820
1821
}

1822
1823
1824
1825
1826
1827
1828
/* Convert buf into a string that can be displayed on screen.  The caller
 * wants to display buf starting with the given column, and extending for
 * at most span columns.  column is zero-based, and span is one-based, so
 * span == 0 means you get "" returned.  The returned string is dynamically
 * allocated, and should be freed.  If isdata 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 column, size_t span, bool isdata)
1829
{
1830
1831
1832
1833
    size_t start_index = actual_x(buf, column);
	/* The index of the first character that the caller wishes to show. */
    size_t start_col = strnlenpt(buf, start_index);
	/* The actual column where that first character starts. */
1834
    char *converted;
1835
	/* The expanded string we will return. */
1836
    size_t index = 0;
1837
	/* Current position in converted. */
1838
    size_t beyond = column + span;
1839
	/* The column number just beyond the last shown character. */
1840

1841
#ifdef USING_OLD_NCURSES
1842
    seen_wide = FALSE;
1843
#endif
1844
    buf += start_index;
1845

1846
    /* Allocate enough space for converting the relevant part of the line. */
1847
    converted = charalloc(strlen(buf) * (MAXCHARLEN + tabsize) + 1);
1848

1849
    /* If the first character starts before the left edge, or would be
1850
     * overwritten by a "$" token, then show placeholders instead. */
1851
1852
    if (*buf != '\0' && *buf != '\t' && (start_col < column ||
			(start_col > 0 && isdata && !ISSET(SOFTWRAP)))) {
1853
	if (is_cntrl_mbchar(buf)) {
1854
	    if (start_col < column) {
1855
		converted[index++] = control_mbrep(buf, isdata);
1856
		column++;
1857
		buf += parse_mbchar(buf, NULL, NULL);
1858
	    }
1859
	}
1860
#ifdef ENABLE_UTF8
1861
	else if (mbwidth(buf) == 2) {
1862
	    if (start_col == column) {
1863
		converted[index++] = ' ';
1864
		column++;
1865
1866
	    }

1867
1868
	    /* Display the right half of a two-column character as '<'. */
	    converted[index++] = '<';
1869
	    column++;
1870
	    buf += parse_mbchar(buf, NULL, NULL);
1871
	}
1872
#endif
1873
1874
    }

1875
    while (*buf != '\0' && column < beyond) {
1876
	int charlength, charwidth = 1;
1877

1878
	if (*buf == ' ') {
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
	    /* Show a space as a visible character, or as a space. */
#ifndef NANO_TINY
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i = whitespace_len[0];

		while (i < whitespace_len[0] + whitespace_len[1])
		    converted[index++] = whitespace[i++];
	    } else
#endif
		converted[index++] = ' ';
1889
	    column++;
1890
1891
	    buf++;
	    continue;
1892
	} else if (*buf == '\t') {
1893
	    /* Show a tab as a visible character, or as as a space. */
1894
#ifndef NANO_TINY
1895
	    if (ISSET(WHITESPACE_DISPLAY)) {
1896
		int i = 0;
1897

1898
1899
		while (i < whitespace_len[0])
		    converted[index++] = whitespace[i++];
1900
	    } else
1901
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1902
		converted[index++] = ' ';
1903
	    column++;
1904
	    /* Fill the tab up with the required number of spaces. */
1905
	    while (column % tabsize != 0 && column < beyond) {
1906
		converted[index++] = ' ';
1907
		column++;
1908
	    }
1909
1910
1911
1912
	    buf++;
	    continue;
	}

1913
	charlength = length_of_char(buf, &charwidth);
1914

1915
	/* If buf contains a control character, represent it. */
1916
	if (is_cntrl_mbchar(buf)) {
1917
	    converted[index++] = '^';
1918
	    converted[index++] = control_mbrep(buf, isdata);
1919
	    column += 2;
1920
1921
1922
	    buf += charlength;
	    continue;
	}
1923

1924
1925
1926
1927
	/* If buf contains a valid non-control character, simply copy it. */
	if (charlength > 0) {
	    for (; charlength > 0; charlength--)
		converted[index++] = *(buf++);
1928

1929
	    column += charwidth;
1930
#ifdef USING_OLD_NCURSES
1931
	    if (charwidth > 1)
1932
		seen_wide = TRUE;
1933
#endif
1934
	    continue;
1935
1936
	}

1937
1938
1939
1940
	/* Represent an invalid sequence with the Replacement Character. */
	converted[index++] = '\xEF';
	converted[index++] = '\xBF';
	converted[index++] = '\xBD';
1941
	column++;
1942
1943
1944
1945
1946
	buf++;

	/* For invalid codepoints, skip extra bytes. */
	if (charlength < -1)
	   buf += charlength + 7;
1947
1948
    }

1949
    /* If there is more text than can be shown, make room for the $ or >. */
1950
    if (*buf != '\0' && (column > beyond || (isdata && !ISSET(SOFTWRAP)))) {
1951
	index = move_mbleft(converted, index);
1952

1953
1954
#ifdef ENABLE_UTF8
	/* Display the left half of a two-column character as '>'. */
1955
	if (mbwidth(converted + index) == 2)
1956
1957
1958
1959
	    converted[index++] = '>';
#endif
    }

1960
1961
    /* Null-terminate the converted string. */
    converted[index] = '\0';
1962

1963
    return converted;
1964
1965
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1966
1967
/* 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
1968
1969
1970
 * has been modified on the titlebar.  If path isn't NULL, we're either
 * in the file browser or the help viewer, so show either the current
 * directory or the title of help text, that is: whatever is in path. */
1971
void titlebar(const char *path)
Chris Allegretta's avatar
Chris Allegretta committed
1972
{
1973
1974
1975
1976
1977
1978
    size_t verlen, prefixlen, pathlen, statelen;
	/* The width of the different titlebar elements, in columns. */
    size_t pluglen = 0;
	/* The width that "Modified" would take up. */
    size_t offset = 0;
	/* The position at which the center part of the titlebar starts. */
1979
1980
    const char *branding = BRANDING;
	/* What is shown in the top left corner. */
1981
1982
1983
1984
    const char *prefix = "";
	/* What is shown before the path -- "File:", "DIR:", or "". */
    const char *state = "";
	/* The state of the current buffer -- "Modified", "View", or "". */
1985
1986
    char *caption;
	/* The presentable form of the pathname. */
1987

1988
1989
1990
1991
    /* If the screen is too small, there is no titlebar. */
    if (topwin == NULL)
	return;

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

1994
    wattron(topwin, interface_color_pair[TITLE_BAR]);
1995

1996
    blank_titlebar();
1997
    as_an_at = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
1998

1999
2000
2001
    /* Do as Pico: if there is not enough width available for all items,
     * first sacrifice the version string, then eat up the side spaces,
     * then sacrifice the prefix, and only then start dottifying. */
Chris Allegretta's avatar
Chris Allegretta committed
2002

2003
    /* Figure out the path, prefix and state strings. */
2004
    if (inhelp)
2005
	branding = "";
2006
#ifdef ENABLE_BROWSER
2007
    else if (path != NULL)
2008
2009
	prefix = _("DIR:");
#endif
2010
    else {
2011
2012
2013
2014
2015
2016
	if (openfile->filename[0] == '\0')
	    path = _("New Buffer");
	else {
	    path = openfile->filename;
	    prefix = _("File:");
	}
2017

2018
2019
2020
2021
	if (openfile->modified)
	    state = _("Modified");
	else if (ISSET(VIEW_MODE))
	    state = _("View");
2022

2023
2024
	pluglen = strlenpt(_("Modified")) + 1;
    }
2025

2026
    /* Determine the widths of the four elements, including their padding. */
2027
    verlen = strlenpt(branding) + 3;
2028
2029
2030
    prefixlen = strlenpt(prefix);
    if (prefixlen > 0)
	prefixlen++;
2031
    pathlen = strlenpt(path);
2032
2033
2034
2035
    statelen = strlenpt(state) + 2;
    if (statelen > 2) {
	pathlen++;
	pluglen = 0;
2036
2037
    }

2038
2039
    /* Only print the version message when there is room for it. */
    if (verlen + prefixlen + pathlen + pluglen + statelen <= COLS)
2040
	mvwaddstr(topwin, 0, 2, branding);
2041
2042
2043
2044
2045
2046
2047
2048
2049
    else {
	verlen = 2;
	/* If things don't fit yet, give up the placeholder. */
	if (verlen + prefixlen + pathlen + pluglen + statelen > COLS)
	    pluglen = 0;
	/* If things still don't fit, give up the side spaces. */
	if (verlen + prefixlen + pathlen + pluglen + statelen > COLS) {
	    verlen = 0;
	    statelen -= 2;
2050
2051
2052
	}
    }

2053
2054
2055
2056
    /* If we have side spaces left, center the path name. */
    if (verlen > 0)
	offset = verlen + (COLS - (verlen + pluglen + statelen) -
					(prefixlen + pathlen)) / 2;
2057

2058
2059
2060
2061
2062
2063
2064
2065
2066
    /* Only print the prefix when there is room for it. */
    if (verlen + prefixlen + pathlen + pluglen + statelen <= COLS) {
	mvwaddstr(topwin, 0, offset, prefix);
	if (prefixlen > 0)
	    waddstr(topwin, " ");
    } else
	wmove(topwin, 0, offset);

    /* Print the full path if there's room; otherwise, dottify it. */
2067
2068
2069
2070
2071
    if (pathlen + pluglen + statelen <= COLS) {
	caption = display_string(path, 0, pathlen, FALSE);
	waddstr(topwin, caption);
	free(caption);
    } else if (5 + statelen <= COLS) {
2072
	waddstr(topwin, "...");
2073
	caption = display_string(path, 3 + pathlen - COLS + statelen,
2074
					COLS - statelen, FALSE);
2075
2076
	waddstr(topwin, caption);
	free(caption);
2077
    }
2078

2079
2080
2081
2082
2083
2084
    /* Right-align the state if there's room; otherwise, trim it. */
    if (statelen > 0 && statelen <= COLS)
	mvwaddstr(topwin, 0, COLS - statelen, state);
    else if (statelen > 0)
	mvwaddnstr(topwin, 0, 0, state, actual_x(state, COLS));

2085
    wattroff(topwin, interface_color_pair[TITLE_BAR]);
2086

2087
    wrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
2088
2089
}

2090
2091
2092
2093
2094
2095
/* Display a normal message on the statusbar, quietly. */
void statusbar(const char *msg)
{
    statusline(HUSH, msg);
}

2096
2097
2098
2099
2100
2101
2102
2103
2104
/* Warn the user on the statusbar and pause for a moment, so that the
 * message can be noticed and read. */
void warn_and_shortly_pause(const char *msg)
{
    statusbar(msg);
    beep();
    napms(1800);
}

2105
/* Display a message on the statusbar, and set suppress_cursorpos to
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2106
2107
 * TRUE, so that the message won't be immediately overwritten if
 * constant cursor position display is on. */
2108
void statusline(message_type importance, const char *msg, ...)
2109
2110
{
    va_list ap;
2111
    static int alerts = 0;
2112
    char *compound, *message;
2113
    size_t start_col;
2114
    bool bracketed;
2115
2116
2117
2118
#ifndef NANO_TINY
    bool old_whitespace = ISSET(WHITESPACE_DISPLAY);

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

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

2131
2132
2133
2134
2135
    /* If there already was an alert message, ignore lesser ones. */
    if ((lastmessage == ALERT && importance != ALERT) ||
		(lastmessage == MILD && importance == HUSH))
	return;

2136
2137
2138
2139
2140
2141
    /* If the ALERT status has been reset, reset the counter. */
    if (lastmessage == HUSH)
	alerts = 0;

    /* Shortly pause after each of the first three alert messages,
     * to give the user time to read them. */
2142
    if (lastmessage == ALERT && alerts < 4 && !ISSET(NO_PAUSES))
2143
2144
	napms(1200);

2145
    if (importance == ALERT) {
2146
	if (++alerts > 3 && !ISSET(NO_PAUSES))
2147
	    msg = _("Further warnings were suppressed");
2148
	beep();
2149
    }
2150
2151

    lastmessage = importance;
2152

2153
2154
2155
    /* Turn the cursor off while fiddling in the statusbar. */
    curs_set(0);

2156
2157
    blank_statusbar();

2158
    /* Construct the message out of all the arguments. */
2159
2160
    compound = charalloc(MAXCHARLEN * (COLS + 1));
    vsnprintf(compound, MAXCHARLEN * (COLS + 1), msg, ap);
2161
    va_end(ap);
2162
2163
    message = display_string(compound, 0, COLS, FALSE);
    free(compound);
2164

2165
2166
    start_col = (COLS - strlenpt(message)) / 2;
    bracketed = (start_col > 1);
2167

2168
    wmove(bottomwin, 0, (bracketed ? start_col - 2 : start_col));
2169
    wattron(bottomwin, interface_color_pair[STATUS_BAR]);
2170
2171
    if (bracketed)
	waddstr(bottomwin, "[ ");
2172
2173
    waddstr(bottomwin, message);
    free(message);
2174
2175
    if (bracketed)
	waddstr(bottomwin, " ]");
2176
    wattroff(bottomwin, interface_color_pair[STATUS_BAR]);
2177

2178
2179
2180
2181
    /* Defeat a VTE/Konsole bug, where the cursor can go off-limits. */
    if (ISSET(CONSTANT_SHOW) && ISSET(NO_HELP))
	wmove(bottomwin, 0, 0);

2182
    /* Push the message to the screen straightaway. */
2183
    wrefresh(bottomwin);
2184

2185
    suppress_cursorpos = TRUE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2186

2187
#ifndef NANO_TINY
2188
2189
    if (old_whitespace)
	SET(WHITESPACE_DISPLAY);
2190
#endif
2191
2192
2193

    /* If doing quick blanking, blank the statusbar after just one keystroke.
     * Otherwise, blank it after twenty-six keystrokes, as Pico does. */
2194
2195
2196
2197
    if (ISSET(QUICK_BLANK))
	statusblank = 1;
    else
	statusblank = 26;
2198
2199
}

2200
2201
/* Display the shortcut list corresponding to menu on the last two rows
 * of the bottom portion of the window. */
2202
void bottombars(int menu)
Chris Allegretta's avatar
Chris Allegretta committed
2203
{
2204
    size_t number, itemwidth, i;
2205
2206
    subnfunc *f;
    const sc *s;
2207

2208
2209
2210
    /* Set the global variable to the given menu. */
    currmenu = menu;

2211
    if (ISSET(NO_HELP) || LINES < 5)
Chris Allegretta's avatar
Chris Allegretta committed
2212
2213
	return;

2214
    /* Determine how many shortcuts there are to show. */
2215
    number = length_of_list(menu);
2216

2217
2218
    if (number > MAIN_VISIBLE)
	number = MAIN_VISIBLE;
2219

2220
    /* Compute the width of each keyname-plus-explanation pair. */
2221
    itemwidth = COLS / ((number / 2) + (number % 2));
Chris Allegretta's avatar
Chris Allegretta committed
2222

2223
    /* If there is no room, don't print anything. */
2224
    if (itemwidth == 0)
2225
2226
	return;

2227
    blank_bottombars();
2228

2229
2230
    /* Display the first number of shortcuts in the given menu that
     * have a key combination assigned to them. */
2231
    for (f = allfuncs, i = 0; i < number && f != NULL; f = f->next) {
2232
	if ((f->menus & menu) == 0)
2233
	    continue;
2234

2235
	s = first_sc_for(menu, f->scfunc);
2236
	if (s == NULL)
2237
	    continue;
2238
2239

	wmove(bottomwin, 1 + i % 2, (i / 2) * itemwidth);
2240

2241
	onekey(s->keystr, _(f->desc), itemwidth + (COLS % itemwidth));
2242
	i++;
Chris Allegretta's avatar
Chris Allegretta committed
2243
    }
2244

2245
    /* Defeat a VTE bug by homing the cursor and forcing a screen update. */
2246
    wmove(bottomwin, 0, 0);
2247
    wrefresh(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
2248
2249
}

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

2259
    wattron(bottomwin, interface_color_pair[KEY_COMBO]);
2260
    waddnstr(bottomwin, keystroke, actual_x(keystroke, length));
2261
    wattroff(bottomwin, interface_color_pair[KEY_COMBO]);
2262

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

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

2273
/* Redetermine current_y from the position of current relative to edittop,
2274
 * and put the cursor in the edit window at (current_y, "current_x"). */
2275
void place_the_cursor(void)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2276
{
2277
    ssize_t row = 0;
2278
    size_t col, xpt = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2279

2280
#ifndef NANO_TINY
2281
    if (ISSET(SOFTWRAP)) {
2282
	filestruct *line = openfile->edittop;
2283
	size_t leftedge;
2284

2285
	row -= chunk_for(openfile->firstcolumn, openfile->edittop);
2286

2287
	/* Calculate how many rows the lines from edittop to current use. */
2288
	while (line != NULL && line != openfile->current) {
2289
	    row += number_of_chunks_in(line) + 1;
2290
2291
2292
	    line = line->next;
	}

2293
	/* Add the number of wraps in the current line before the cursor. */
2294
	row += get_chunk_and_edge(xpt, openfile->current, &leftedge);
2295
	col = xpt - leftedge;
2296
2297
2298
    } else
#endif
    {
2299
2300
	row = openfile->current->lineno - openfile->edittop->lineno;
	col = xpt - get_page_start(xpt);
2301
    }
2302
2303
2304
2305

    if (row < editwinrows)
	wmove(edit, row, margin + col);

2306
    openfile->current_y = row;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2307
}
Chris Allegretta's avatar
Chris Allegretta committed
2308

2309
/* edit_draw() takes care of the job of actually painting a line into
2310
 * the edit window.  fileptr is the line to be painted, at row row of
2311
2312
 * the window.  converted is the actual string to be written to the
 * window, with tabs and control characters replaced by strings of
2313
 * regular characters.  from_col is the column number of the first
2314
 * character of this page.  That is, the first character of converted
2315
 * corresponds to character number actual_x(fileptr->data, from_col) of the
2316
 * line. */
2317
void edit_draw(filestruct *fileptr, const char *converted,
2318
	int row, size_t from_col)
Chris Allegretta's avatar
Chris Allegretta committed
2319
{
2320
#if !defined(NANO_TINY) || !defined(DISABLE_COLOR)
2321
    size_t from_x = actual_x(fileptr->data, from_col);
2322
2323
	/* The position in fileptr->data of the leftmost character
	 * that displays at least partially on the window. */
2324
    size_t till_x = actual_x(fileptr->data, from_col + editwincols - 1) + 1;
2325
	/* The position in fileptr->data of the first character that is
2326
2327
	 * completely off the window to the right.  Note that till_x
	 * might be beyond the null terminator of the string. */
2328
2329
#endif

2330
    assert(openfile != NULL && fileptr != NULL && converted != NULL);
2331
2332
2333
    assert(strlenpt(converted) <= editwincols);

#ifdef ENABLE_LINENUMBERS
2334
    /* If line numbering is switched on, put a line number in front of
2335
2336
     * the text -- but only for the parts that are not softwrapped. */
    if (margin > 0) {
2337
	wattron(edit, interface_color_pair[LINE_NUMBER]);
2338
#ifndef NANO_TINY
2339
	if (ISSET(SOFTWRAP) && from_x != 0)
2340
	    mvwprintw(edit, row, 0, "%*s", margin - 1, " ");
2341
2342
	else
#endif
2343
	    mvwprintw(edit, row, 0, "%*ld", margin - 1, (long)fileptr->lineno);
2344
	wattroff(edit, interface_color_pair[LINE_NUMBER]);
2345
2346
    }
#endif
2347

2348
2349
2350
    /* First simply write the converted line -- afterward we'll add colors
     * and the marking highlight on just the pieces that need it. */
    mvwaddstr(edit, row, margin, converted);
2351

2352
#ifdef USING_OLD_NCURSES
2353
2354
2355
    /* 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. */
2356
    if (seen_wide)
2357
	wredrawln(edit, row, 1);
2358
#endif
2359

2360
#ifndef DISABLE_COLOR
2361
    /* If color syntaxes are available and turned on, apply them. */
2362
    if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX)) {
2363
	const colortype *varnish = openfile->colorstrings;
2364

2365
2366
2367
2368
	/* If there are multiline regexes, make sure there is a cache. */
	if (openfile->syntax->nmultis > 0)
	    alloc_multidata_if_needed(fileptr);

2369
	/* Iterate through all the coloring regexes. */
2370
	for (; varnish != NULL; varnish = varnish->next) {
2371
2372
	    size_t index = 0;
		/* Where in the line we currently begin looking for a match. */
2373
	    int start_col;
2374
		/* The starting column of a piece to paint.  Zero-based. */
2375
	    int paintlen = 0;
2376
2377
2378
		/* The number of characters to paint. */
	    const char *thetext;
		/* The place in converted from where painting starts. */
2379
2380
	    regmatch_t match;
		/* The match positions of a single-line regex. */
2381
2382
2383
2384
2385
2386
	    const filestruct *start_line = fileptr->prev;
		/* The first line before fileptr that matches 'start'. */
	    const filestruct *end_line = fileptr;
		/* The line that matches 'end'. */
	    regmatch_t startmatch, endmatch;
		/* The match positions of the start and end regexes. */
2387

2388
2389
2390
	    /* 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. */
2391

2392
2393
	    wattron(edit, varnish->attributes);

2394
2395
	    /* First case: varnish is a single-line expression. */
	    if (varnish->end == NULL) {
2396
		/* We increment index by rm_eo, to move past the end of the
2397
		 * last match.  Even though two matches may overlap, we
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2398
2399
		 * want to ignore them, so that we can highlight e.g. C
		 * strings correctly. */
2400
		while (index < till_x) {
2401
2402
		    /* Note the fifth parameter to regexec().  It says
		     * not to match the beginning-of-line character
2403
		     * unless index is zero.  If regexec() returns
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2404
2405
		     * REG_NOMATCH, there are no more matches in the
		     * line. */
2406
		    if (regexec(varnish->start, &fileptr->data[index], 1,
2407
				&match, (index == 0) ? 0 : REG_NOTBOL) != 0)
2408
			break;
2409

2410
		    /* If the match is of length zero, skip it. */
2411
		    if (match.rm_so == match.rm_eo) {
2412
			index = move_mbright(fileptr->data,
2413
						index + match.rm_eo);
2414
2415
2416
			continue;
		    }

2417
		    /* Translate the match to the beginning of the line. */
2418
2419
2420
		    match.rm_so += index;
		    match.rm_eo += index;
		    index = match.rm_eo;
2421

2422
2423
		    /* If the matching part is not visible, skip it. */
		    if (match.rm_eo <= from_x || match.rm_so >= till_x)
2424
2425
			continue;

2426
2427
2428
		    start_col = (match.rm_so <= from_x) ?
					0 : strnlenpt(fileptr->data,
					match.rm_so) - from_col;
2429

2430
		    thetext = converted + actual_x(converted, start_col);
2431

2432
		    paintlen = actual_x(thetext, strnlenpt(fileptr->data,
2433
					match.rm_eo) - from_col - start_col);
2434

2435
		    mvwaddnstr(edit, row, margin + start_col,
2436
						thetext, paintlen);
Chris Allegretta's avatar
Chris Allegretta committed
2437
		}
2438
2439
2440
2441
		goto tail_of_loop;
	    }

	    /* Second case: varnish is a multiline expression. */
2442

2443
2444
2445
	    /* Assume nothing gets painted until proven otherwise below. */
	    fileptr->multidata[varnish->id] = CNONE;

2446
2447
2448
2449
	    /* First check the multidata of the preceding line -- it tells
	     * us about the situation so far, and thus what to do here. */
	    if (start_line != NULL && start_line->multidata != NULL) {
		if (start_line->multidata[varnish->id] == CWHOLELINE ||
2450
2451
			start_line->multidata[varnish->id] == CENDAFTER ||
			start_line->multidata[varnish->id] == CWOULDBE)
2452
2453
2454
		    goto seek_an_end;
		if (start_line->multidata[varnish->id] == CNONE ||
			start_line->multidata[varnish->id] == CBEGINBEFORE ||
2455
			start_line->multidata[varnish->id] == CSTARTENDHERE)
2456
2457
2458
		    goto step_two;
	    }

2459
2460
	    /* The preceding line has no precalculated multidata.  So, do
	     * some backtracking to find out what to paint. */
2461

2462
2463
	    /* First step: see if there is a line before current that
	     * matches 'start' and is not complemented by an 'end'. */
2464
2465
	    while (start_line != NULL && regexec(varnish->start,
		    start_line->data, 1, &startmatch, 0) == REG_NOMATCH) {
2466
		/* There is no start on this line; but if there is an end,
2467
2468
		 * there is no need to look for starts on earlier lines. */
		if (regexec(varnish->end, start_line->data, 0, NULL, 0) == 0)
2469
		    goto step_two;
2470
2471
		start_line = start_line->prev;
	    }
2472

2473
2474
2475
2476
2477
2478
2479
	    /* If no start was found, skip to the next step. */
	    if (start_line == NULL)
		goto step_two;

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

2484
	    /* Is there an uncomplemented start on the found line? */
2485
	    while (TRUE) {
2486
		/* Begin searching for an end after the start match. */
2487
		index += startmatch.rm_eo;
2488
		/* If there is no end after this last start, good. */
2489
2490
		if (regexec(varnish->end, start_line->data + index, 1, &endmatch,
				(index == 0) ? 0 : REG_NOTBOL) == REG_NOMATCH)
2491
		    break;
2492
2493
		/* Begin searching for a new start after the end match. */
		index += endmatch.rm_eo;
2494
2495
2496
		/* If both start and end match are mere anchors, advance. */
		if (startmatch.rm_so == startmatch.rm_eo &&
				endmatch.rm_so == endmatch.rm_eo) {
2497
		    if (start_line->data[index] == '\0')
2498
			break;
2499
		    index = move_mbright(start_line->data, index);
2500
		}
2501
		/* If there is no later start on this line, next step. */
2502
		if (regexec(varnish->start, start_line->data + index,
2503
				1, &startmatch, REG_NOTBOL) == REG_NOMATCH)
2504
		    goto step_two;
2505
2506
2507
	    }
	    /* Indeed, there is a start without an end on that line. */

2508
  seek_an_end:
2509
2510
	    /* We've already checked that there is no end between the start
	     * and the current line.  But is there an end after the start
2511
2512
2513
2514
2515
	     * at all?  We don't paint unterminated starts. */
	    while (end_line != NULL && regexec(varnish->end, end_line->data,
				 1, &endmatch, 0) == REG_NOMATCH)
		end_line = end_line->next;

2516
	    /* If there is no end, there is nothing to paint. */
2517
2518
	    if (end_line == NULL) {
		fileptr->multidata[varnish->id] = CWOULDBE;
2519
		goto tail_of_loop;
2520
	    }
2521

2522
	    /* If the end is on a later line, paint whole line, and be done. */
2523
	    if (end_line != fileptr) {
2524
		mvwaddnstr(edit, row, margin, converted, -1);
2525
2526
		fileptr->multidata[varnish->id] = CWHOLELINE;
		goto tail_of_loop;
2527
2528
2529
2530
	    }

	    /* Only if it is visible, paint the part to be coloured. */
	    if (endmatch.rm_eo > from_x) {
2531
		paintlen = actual_x(converted, strnlenpt(fileptr->data,
2532
						endmatch.rm_eo) - from_col);
2533
		mvwaddnstr(edit, row, margin, converted, paintlen);
2534
2535
	    }
	    fileptr->multidata[varnish->id] = CBEGINBEFORE;
2536

2537
  step_two:
2538
2539
2540
	    /* Second step: look for starts on this line, but begin
	     * looking only after an end match, if there is one. */
	    index = (paintlen == 0) ? 0 : endmatch.rm_eo;
2541

2542
	    while (regexec(varnish->start, fileptr->data + index,
2543
				1, &startmatch, (index == 0) ?
2544
				0 : REG_NOTBOL) == 0) {
2545
2546
2547
2548
		/* Translate the match to be relative to the
		 * beginning of the line. */
		startmatch.rm_so += index;
		startmatch.rm_eo += index;
2549

2550
		start_col = (startmatch.rm_so <= from_x) ?
2551
				0 : strnlenpt(fileptr->data,
2552
				startmatch.rm_so) - from_col;
2553

2554
		thetext = converted + actual_x(converted, start_col);
2555

2556
2557
		if (regexec(varnish->end, fileptr->data + startmatch.rm_eo,
				1, &endmatch, (startmatch.rm_eo == 0) ?
2558
				0 : REG_NOTBOL) == 0) {
2559
2560
2561
2562
		    /* 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;
2563
2564
		    /* Only paint the match if it is visible on screen and
		     * it is more than zero characters long. */
2565
		    if (endmatch.rm_eo > from_x &&
2566
					endmatch.rm_eo > startmatch.rm_so) {
2567
			paintlen = actual_x(thetext, strnlenpt(fileptr->data,
2568
					endmatch.rm_eo) - from_col - start_col);
2569

2570
			mvwaddnstr(edit, row, margin + start_col,
2571
						thetext, paintlen);
2572

2573
			fileptr->multidata[varnish->id] = CSTARTENDHERE;
2574
		    }
2575
		    index = endmatch.rm_eo;
2576
2577
2578
		    /* If both start and end match are anchors, advance. */
		    if (startmatch.rm_so == startmatch.rm_eo &&
				endmatch.rm_so == endmatch.rm_eo) {
2579
			if (fileptr->data[index] == '\0')
2580
			    break;
2581
			index = move_mbright(fileptr->data, index);
2582
2583
		    }
		    continue;
2584
		}
2585

2586
2587
		/* There is no end on this line.  But maybe on later lines? */
		end_line = fileptr->next;
2588

2589
2590
2591
		while (end_line != NULL && regexec(varnish->end, end_line->data,
					0, NULL, 0) == REG_NOMATCH)
		    end_line = end_line->next;
2592

2593
		/* If there is no end, we're done with this regex. */
2594
2595
		if (end_line == NULL) {
		    fileptr->multidata[varnish->id] = CWOULDBE;
2596
		    break;
2597
		}
2598

2599
		/* Paint the rest of the line, and we're done. */
2600
		mvwaddnstr(edit, row, margin + start_col, thetext, -1);
2601
2602
		fileptr->multidata[varnish->id] = CENDAFTER;
		break;
2603
	    }
2604
  tail_of_loop:
2605
	    wattroff(edit, varnish->attributes);
2606
	}
2607
    }
2608
#endif /* !DISABLE_COLOR */
2609

2610
#ifndef NANO_TINY
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
    /* If the mark is on, and fileptr is at least partially selected, we
     * need to paint it. */
    if (openfile->mark_set &&
		(fileptr->lineno <= openfile->mark_begin->lineno ||
		fileptr->lineno <= openfile->current->lineno) &&
		(fileptr->lineno >= openfile->mark_begin->lineno ||
		fileptr->lineno >= openfile->current->lineno)) {
	const filestruct *top, *bot;
	    /* The lines where the marked region begins and ends. */
	size_t top_x, bot_x;
	    /* The x positions where the marked region begins and ends. */
2622
	int start_col;
2623
2624
2625
2626
2627
	    /* The column where painting starts.  Zero-based. */
	const char *thetext;
	    /* The place in converted from where painting starts. */
	int paintlen = -1;
	    /* The number of characters to paint.  Negative means "all". */
2628

2629
	mark_order(&top, &top_x, &bot, &bot_x, NULL);
2630

2631
2632
2633
2634
	if (top->lineno < fileptr->lineno || top_x < from_x)
	    top_x = from_x;
	if (bot->lineno > fileptr->lineno || bot_x > till_x)
	    bot_x = till_x;
Chris Allegretta's avatar
Chris Allegretta committed
2635

2636
	/* Only paint if the marked part of the line is on this page. */
2637
	if (top_x < till_x && bot_x > from_x) {
2638
2639
	    /* Compute on which screen column to start painting. */
	    start_col = strnlenpt(fileptr->data, top_x) - from_col;
2640

2641
2642
2643
	    if (start_col < 0)
		start_col = 0;

2644
	    thetext = converted + actual_x(converted, start_col);
2645

2646
2647
2648
2649
2650
2651
	    /* If the end of the mark is onscreen, compute how many
	     * characters to paint.  Otherwise, just paint all. */
	    if (bot_x < till_x) {
		size_t end_col = strnlenpt(fileptr->data, bot_x) - from_col;
		paintlen = actual_x(thetext, end_col - start_col);
	    }
2652

2653
	    wattron(edit, interface_color_pair[SELECTED_TEXT]);
2654
	    mvwaddnstr(edit, row, margin + start_col, thetext, paintlen);
2655
	    wattroff(edit, interface_color_pair[SELECTED_TEXT]);
Chris Allegretta's avatar
Chris Allegretta committed
2656
	}
2657
    }
2658
#endif /* !NANO_TINY */
Chris Allegretta's avatar
Chris Allegretta committed
2659
2660
}

2661
2662
2663
/* Redraw the line at fileptr.  The line will be displayed so that the
 * character with the given index is visible -- if necessary, the line
 * will be horizontally scrolled.  In softwrap mode, however, the entire
2664
2665
2666
 * line will be passed to update_softwrapped_line().  Likely values of
 * index are current_x or zero.  Return the number of additional rows
 * consumed (when softwrapping). */
2667
int update_line(filestruct *fileptr, size_t index)
Chris Allegretta's avatar
Chris Allegretta committed
2668
{
2669
    int row = 0;
2670
	/* The row in the edit window we will be updating. */
2671
    char *converted;
2672
	/* The data of the line with tabs and control characters expanded. */
2673
2674
    size_t from_col = 0;
	/* From which column a horizontally scrolled line is displayed. */
Chris Allegretta's avatar
Chris Allegretta committed
2675

2676
#ifndef NANO_TINY
2677
2678
    if (ISSET(SOFTWRAP))
	return update_softwrapped_line(fileptr);
2679
#endif
2680
2681

    row = fileptr->lineno - openfile->edittop->lineno;
2682

2683
    /* If the line is offscreen, don't even try to display it. */
2684
    if (row < 0 || row >= editwinrows) {
2685
#ifndef NANO_TINY
2686
	statusline(ALERT, "Badness: tried to display a line on row %i"
2687
				" -- please report a bug", row);
2688
#endif
2689
	return 0;
2690
    }
2691

2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
    /* First, blank out the row. */
    blank_row(edit, row, 0, COLS);

    /* Next, find out from which column to start displaying the line. */
    from_col = get_page_start(strnlenpt(fileptr->data, index));

    /* Expand the line, replacing tabs with spaces, and control
     * characters with their displayed forms. */
    converted = display_string(fileptr->data, from_col, editwincols, TRUE);

    /* Draw the line. */
    edit_draw(fileptr, converted, row, from_col);
    free(converted);

    if (from_col > 0)
	mvwaddch(edit, row, margin, '$');
    if (strlenpt(fileptr->data) > from_col + editwincols)
	mvwaddch(edit, row, COLS - 1, '$');

2711
    return 1;
Chris Allegretta's avatar
Chris Allegretta committed
2712
2713
}

2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
#ifndef NANO_TINY
/* Redraw all the chunks of the given line (as far as they fit onscreen),
 * unless it's edittop, which will be displayed from column firstcolumn.
 * Return the number of additional rows consumed. */
int update_softwrapped_line(filestruct *fileptr)
{
    int row = 0;
	/* The row in the edit window we will write to. */
    filestruct *line = openfile->edittop;
	/* An iterator needed to find the relevant row. */
    int starting_row;
	/* The first row in the edit window that gets updated. */
    size_t from_col = 0;
	/* The starting column of the current chunk. */
2728
2729
    size_t to_col = 0;
	/* To which column a line is displayed. */
2730
2731
2732
2733
2734
2735
    char *converted;
	/* The data of the chunk with tabs and control characters expanded. */

    if (fileptr == openfile->edittop)
	from_col = openfile->firstcolumn;
    else
2736
	row -= chunk_for(openfile->firstcolumn, openfile->edittop);
2737
2738
2739

    /* Find out on which screen row the target line should be shown. */
    while (line != fileptr && line != NULL) {
2740
	row += number_of_chunks_in(line) + 1;
2741
2742
2743
	line = line->next;
    }

2744
2745
2746
2747
    /* If the first chunk is offscreen, don't even try to display it. */
    if (row < 0 || row >= editwinrows) {
	statusline(ALERT, "Badness: tried to display a chunk on row %i"
				" -- please report a bug", row);
2748
	return 0;
2749
    }
2750
2751
2752

    starting_row = row;

2753
    while (row < editwinrows) {
2754
	bool end_of_line = FALSE;
2755
2756
2757

	to_col = get_softwrap_breakpoint(fileptr->data, from_col, &end_of_line);

2758
2759
2760
	blank_row(edit, row, 0, COLS);

	/* Convert the chunk to its displayable form and draw it. */
2761
	converted = display_string(fileptr->data, from_col, to_col - from_col, TRUE);
2762
2763
2764
	edit_draw(fileptr, converted, row++, from_col);
	free(converted);

2765
2766
2767
2768
	if (end_of_line)
	    break;

	/* If the line is softwrapped before its last column, add a ">" just
2769
2770
2771
	 * after its softwrap breakpoint, unless we're softwrapping at blanks
	 * and not in the middle of a word. */
	if (!ISSET(AT_BLANKS) && to_col - from_col < editwincols)
2772
2773
2774
	    mvwaddch(edit, row - 1, to_col - from_col, '>');

	from_col = to_col;
2775
2776
2777
2778
2779
2780
    }

    return (row - starting_row);
}
#endif

2781
2782
2783
2784
/* Check whether the mark is on, or whether old_column and new_column are on
 * different "pages" (in softwrap mode, only the former applies), which means
 * that the relevant line needs to be redrawn. */
bool line_needs_update(const size_t old_column, const size_t new_column)
2785
{
2786
#ifndef NANO_TINY
2787
2788
2789
    if (openfile->mark_set)
	return TRUE;
    else
2790
#endif
2791
	return (get_page_start(old_column) != get_page_start(new_column));
2792
2793
}

2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
/* Try to move up nrows softwrapped chunks from the given line and the
 * given column (leftedge).  After moving, leftedge will be set to the
 * starting column of the current chunk.  Return the number of chunks we
 * couldn't move up, which will be zero if we completely succeeded. */
int go_back_chunks(int nrows, filestruct **line, size_t *leftedge)
{
    int i;

#ifndef NANO_TINY
    if (ISSET(SOFTWRAP)) {
2804
	/* Recede through the requested number of chunks. */
2805
	for (i = nrows; i > 0; i--) {
2806
2807
2808
2809
2810
2811
	    size_t chunk = chunk_for(*leftedge, *line);

	    *leftedge = 0;

	    if (chunk >= i)
		return go_forward_chunks(chunk - i, line, leftedge);
2812
2813
2814
2815

	    if (*line == openfile->fileage)
		break;

2816
	    i -= chunk;
2817
	    *line = (*line)->prev;
2818
	    *leftedge = HIGHEST_POSITIVE;
2819
2820
	}

2821
2822
	if (*leftedge == HIGHEST_POSITIVE)
	    *leftedge = leftedge_for(*leftedge, *line);
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
    } else
#endif
	for (i = nrows; i > 0 && (*line)->prev != NULL; i--)
	    *line = (*line)->prev;

    return i;
}

/* Try to move down nrows softwrapped chunks from the given line and the
 * given column (leftedge).  After moving, leftedge will be set to the
 * starting column of the current chunk.  Return the number of chunks we
 * couldn't move down, which will be zero if we completely succeeded. */
int go_forward_chunks(int nrows, filestruct **line, size_t *leftedge)
{
    int i;

#ifndef NANO_TINY
    if (ISSET(SOFTWRAP)) {
2841
	size_t current_leftedge = *leftedge;
2842

2843
	/* Advance through the requested number of chunks. */
2844
	for (i = nrows; i > 0; i--) {
2845
	    bool end_of_line = FALSE;
2846

2847
2848
2849
2850
	    current_leftedge = get_softwrap_breakpoint((*line)->data,
					current_leftedge, &end_of_line);

	    if (!end_of_line)
2851
2852
2853
2854
2855
2856
		continue;

	    if (*line == openfile->filebot)
		break;

	    *line = (*line)->next;
2857
	    current_leftedge = 0;
2858
2859
2860
2861
	}

	/* Only change leftedge when we actually could move. */
	if (i < nrows)
2862
	    *leftedge = current_leftedge;
2863
2864
2865
2866
2867
2868
2869
2870
    } else
#endif
	for (i = nrows; i > 0 && (*line)->next != NULL; i--)
	    *line = (*line)->next;

    return i;
}

2871
2872
2873
2874
2875
2876
2877
2878
/* Return TRUE if there are fewer than a screen's worth of lines between
 * the line at line number was_lineno (and column was_leftedge, if we're
 * in softwrap mode) and the line at current[current_x]. */
bool less_than_a_screenful(size_t was_lineno, size_t was_leftedge)
{
#ifndef NANO_TINY
    if (ISSET(SOFTWRAP)) {
	filestruct *line = openfile->current;
2879
	size_t leftedge = leftedge_for(xplustabs(), openfile->current);
2880
2881
2882
2883
2884
2885
2886
2887
2888
	int rows_left = go_back_chunks(editwinrows - 1, &line, &leftedge);

	return (rows_left > 0 || line->lineno < was_lineno ||
		(line->lineno == was_lineno && leftedge <= was_leftedge));
    } else
#endif
	return (openfile->current->lineno - was_lineno < editwinrows);
}

2889
/* Scroll the edit window in the given direction and the given number of rows,
2890
 * and draw new lines on the blank lines left after the scrolling. */
2891
void edit_scroll(scroll_dir direction, int nrows)
2892
{
2893
    filestruct *line;
2894
2895
    size_t leftedge;

2896
2897
    /* Part 1: nrows is the number of rows we're going to scroll the text of
     * the edit window. */
2898

2899
2900
    /* Move the top line of the edit window the requested number of rows up or
     * down, and reduce the number of rows with the amount we couldn't move. */
2901
    if (direction == UPWARD)
2902
	nrows -= go_back_chunks(nrows, &openfile->edittop, &openfile->firstcolumn);
2903
    else
2904
	nrows -= go_forward_chunks(nrows, &openfile->edittop, &openfile->firstcolumn);
2905

2906
    /* Don't bother scrolling zero rows, nor more than the window can hold. */
2907
    if (nrows == 0) {
2908
#ifndef NANO_TINY
2909
	statusline(ALERT, "Underscrolling -- please report a bug");
2910
#endif
2911
	return;
2912
    }
2913
    if (nrows >= editwinrows) {
2914
#ifndef NANO_TINY
2915
2916
	if (editwinrows > 1)
	    statusline(ALERT, "Overscrolling -- please report a bug");
2917
#endif
2918
	refresh_needed = TRUE;
2919
	return;
2920
    }
2921

2922
    /* Scroll the text of the edit window a number of rows up or down. */
2923
    scrollok(edit, TRUE);
2924
    wscrl(edit, (direction == UPWARD) ? -nrows : nrows);
2925
2926
    scrollok(edit, FALSE);

2927
2928
    /* Part 2: nrows is now the number of rows in the scrolled region of the
     * edit window that we need to draw. */
2929

2930
2931
2932
2933
    /* If we're not on the first "page" (when not softwrapping), or the mark
     * is on, the row next to the scrolled region needs to be redrawn too. */
    if (line_needs_update(openfile->placewewant, 0) && nrows < editwinrows)
	nrows++;
2934

2935
    /* If we scrolled backward, start on the first line of the blank region. */
2936
    line = openfile->edittop;
2937
    leftedge = openfile->firstcolumn;
2938

2939
    /* If we scrolled forward, move down to the start of the blank region. */
2940
2941
    if (direction == DOWNWARD)
	go_forward_chunks(editwinrows - nrows, &line, &leftedge);
2942

2943
#ifndef NANO_TINY
2944
2945
2946
    if (ISSET(SOFTWRAP)) {
	/* Compensate for the earlier chunks of a softwrapped line. */
	nrows += chunk_for(leftedge, line);
2947

2948
2949
2950
2951
	/* Don't compensate for the chunks that are offscreen. */
	if (line == openfile->edittop)
	    nrows -= chunk_for(openfile->firstcolumn, line);
    }
2952
#endif
2953
2954
2955

    /* Draw new content on the blank rows inside the scrolled region
     * (and on the bordering row too when it was deemed necessary). */
2956
2957
2958
    while (nrows > 0 && line != NULL) {
	nrows -= update_line(line, (line == openfile->current) ?
					openfile->current_x : 0);
2959
	line = line->next;
2960
2961
2962
    }
}

2963
#ifndef NANO_TINY
2964
2965
2966
2967
2968
2969
2970
/* Get the column number after leftedge where we can break the given text, and
 * return it.  This will always be editwincols or less after leftedge.  Set
 * end_of_line to TRUE if we reach the end of the line while searching the
 * text.  Assume leftedge is the leftmost column of a softwrapped chunk. */
size_t get_softwrap_breakpoint(const char *text, size_t leftedge,
				bool *end_of_line)
{
2971
2972
    size_t index = 0;
	/* Current index in text. */
2973
2974
2975
2976
2977
2978
    size_t column = 0;
	/* Current column position in text. */
    size_t prev_column = 0;
	/* Previous column position in text. */
    size_t goal_column;
	/* Column of the last character where we can break the text. */
2979
2980
2981
2982
2983
2984
    bool found_blank = FALSE;
	/* Did we find at least one blank? */
    size_t lastblank_index = 0;
	/* Current index of the last blank in text. */
    size_t lastblank_column = 0;
	/* Current column position of the last blank in text. */
2985
2986
2987
2988
2989
2990
    int char_len = 0;
	/* Length of current character, in bytes. */

    while (*text != '\0' && column < leftedge)
	text += parse_mbchar(text, NULL, &column);

2991
2992
    /* Use a full screen row for text. */
    goal_column = column + editwincols;
2993
2994

    while (*text != '\0' && column <= goal_column) {
2995
2996
	/* When breaking at blanks, do it *before* the target column. */
	if (ISSET(AT_BLANKS) && is_blank_mbchar(text) && column < goal_column) {
2997
2998
2999
3000
3001
	    found_blank = TRUE;
	    lastblank_index = index;
	    lastblank_column = column;
	}

3002
3003
3004
	prev_column = column;
	char_len = parse_mbchar(text, NULL, &column);
	text += char_len;
3005
	index += char_len;
3006
3007
    }

3008
    /* If we didn't overshoot the target, we've found a breaking point. */
3009
    if (column <= goal_column) {
3010
3011
	/* We've reached EOL if we didn't even reach the target. */
	*end_of_line = (column < goal_column);
3012
3013
3014
	return column;
    }

3015
3016
3017
3018
3019
3020
3021
3022
    /* If we're softwrapping at blanks and we found at least one blank, move
     * the pointer back to the last blank, step beyond it, and we're done. */
    if (found_blank) {
	text = text - index + lastblank_index;
	parse_mbchar(text, NULL, &lastblank_column);
	return lastblank_column;
    }

3023
3024
3025
    /* Otherwise, return the column of the last character that doesn't
     * overshoot the target, since we can't break the text anywhere else. */
    return (editwincols > 1) ? prev_column : column - 1;
3026
3027
3028
3029
3030
}

/* Get the row of the softwrapped chunk of the given line that column is on,
 * relative to the first row (zero-based), and return it.  If leftedge isn't
 * NULL, return the leftmost column of the chunk in it. */
3031
size_t get_chunk_and_edge(size_t column, filestruct *line, size_t *leftedge)
3032
3033
{
    size_t current_chunk = 0, start_col = 0, end_col;
3034
    bool end_of_line = FALSE;
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050

    while (TRUE) {
	end_col = get_softwrap_breakpoint(line->data, start_col, &end_of_line);

	/* We reached the end of the line and/or found column, so get out. */
	if (end_of_line || (column >= start_col && column < end_col)) {
	    if (leftedge != NULL)
		*leftedge = start_col;
	    return current_chunk;
	}

	current_chunk++;
	start_col = end_col;
    }
}

3051
3052
/* Return the row of the softwrapped chunk of the given line that column is on,
 * relative to the first row (zero-based). */
3053
size_t chunk_for(size_t column, filestruct *line)
3054
{
3055
    return get_chunk_and_edge(column, line, NULL);
3056
3057
3058
3059
}

/* Return the leftmost column of the softwrapped chunk of the given line that
 * column is on. */
3060
size_t leftedge_for(size_t column, filestruct *line)
3061
{
3062
3063
    size_t leftedge;

3064
    get_chunk_and_edge(column, line, &leftedge);
3065
3066

    return leftedge;
3067
3068
3069
3070
}

/* Return the row of the last softwrapped chunk of the given line, relative to
 * the first row (zero-based). */
3071
size_t number_of_chunks_in(filestruct *line)
3072
{
3073
    return chunk_for((size_t)-1, line);
3074
3075
}

3076
/* Ensure that firstcolumn is at the starting column of the softwrapped chunk
3077
3078
3079
3080
 * it's on.  We need to do this when the number of columns of the edit window
 * has changed, because then the width of softwrapped chunks has changed. */
void ensure_firstcolumn_is_aligned(void)
{
3081
3082
    openfile->firstcolumn = leftedge_for(openfile->firstcolumn,
						openfile->edittop);
3083
3084
3085

    /* If smooth scrolling is on, make sure the viewport doesn't center. */
    focusing = FALSE;
3086
}
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
#endif /* !NANO_TINY */

/* When in softwrap mode, and the given column is on or after the breakpoint of
 * a softwrapped chunk, shift it back to the last column before the breakpoint.
 * The given column is relative to the given leftedge in current.  The returned
 * column is relative to the start of the text. */
size_t actual_last_column(size_t leftedge, size_t column)
{
#ifndef NANO_TINY
    if (ISSET(SOFTWRAP)) {
	bool last_chunk;
	size_t end_col = get_softwrap_breakpoint(openfile->current->data,
					leftedge, &last_chunk) - leftedge;

	/* If we're not on the last chunk, we're one column past the end of
	 * the row.  Shifting back one column might put us in the middle of
3103
	 * a multi-column character, but actual_x() will fix that later. */
3104
3105
3106
3107
3108
3109
	if (!last_chunk)
	    end_col--;

	if (column > end_col)
	    column = end_col;
    }
3110
#endif
3111

3112
3113
3114
    return leftedge + column;
}

3115
3116
3117
3118
/* Return TRUE if current[current_x] is above the top of the screen, and FALSE
 * otherwise. */
bool current_is_above_screen(void)
{
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
#ifndef NANO_TINY
    if (ISSET(SOFTWRAP))
	/* The cursor is above screen when current[current_x] is before edittop
	 * at column firstcolumn. */
	return (openfile->current->lineno < openfile->edittop->lineno ||
		(openfile->current->lineno == openfile->edittop->lineno &&
		xplustabs() < openfile->firstcolumn));
    else
#endif
	return (openfile->current->lineno < openfile->edittop->lineno);
3129
3130
3131
3132
3133
3134
3135
3136
3137
}

/* Return TRUE if current[current_x] is below the bottom of the screen, and
 * FALSE otherwise. */
bool current_is_below_screen(void)
{
#ifndef NANO_TINY
    if (ISSET(SOFTWRAP)) {
	filestruct *line = openfile->edittop;
3138
	size_t leftedge = openfile->firstcolumn;
3139
3140

	/* If current[current_x] is more than a screen's worth of lines after
3141
	 * edittop at column firstcolumn, it's below the screen. */
3142
3143
3144
	return (go_forward_chunks(editwinrows - 1, &line, &leftedge) == 0 &&
			(line->lineno < openfile->current->lineno ||
			(line->lineno == openfile->current->lineno &&
3145
3146
			leftedge < leftedge_for(xplustabs(),
						openfile->current))));
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
    } else
#endif
	return (openfile->current->lineno >=
			openfile->edittop->lineno + editwinrows);
}

/* Return TRUE if current[current_x] is offscreen relative to edittop, and
 * FALSE otherwise. */
bool current_is_offscreen(void)
{
    return (current_is_above_screen() || current_is_below_screen());
}

3160
/* Update any lines between old_current and current that need to be
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3161
 * updated.  Use this if we've moved without changing any text. */
3162
void edit_redraw(filestruct *old_current)
3163
{
3164
3165
3166
3167
    size_t was_pww = openfile->placewewant;

    openfile->placewewant = xplustabs();

3168
    /* If the current line is offscreen, scroll until it's onscreen. */
3169
    if (current_is_offscreen()) {
3170
	adjust_viewport((focusing || !ISSET(SMOOTH_SCROLL)) ? CENTERING : FLOWING);
3171
	refresh_needed = TRUE;
3172
	return;
3173
    }
3174

3175
#ifndef NANO_TINY
3176
3177
    /* If the mark is on, update all lines between old_current and current. */
    if (openfile->mark_set) {
3178
	filestruct *line = old_current;
3179

3180
3181
	while (line != openfile->current) {
	    update_line(line, 0);
3182

3183
3184
	    line = (line->lineno > openfile->current->lineno) ?
			line->prev : line->next;
3185
	}
3186
3187
3188
3189
3190
3191
    } else
#endif
	/* Otherwise, update old_current only if it differs from current
	 * and was horizontally scrolled. */
	if (old_current != openfile->current && get_page_start(was_pww) > 0)
	    update_line(old_current, 0);
3192

3193
3194
    /* Update current if the mark is on or it has changed "page", or if it
     * differs from old_current and needs to be horizontally scrolled. */
3195
    if (line_needs_update(was_pww, openfile->placewewant) ||
3196
			(old_current != openfile->current &&
3197
			get_page_start(openfile->placewewant) > 0))
3198
	update_line(openfile->current, openfile->current_x);
3199
3200
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3201
3202
/* 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
3203
3204
void edit_refresh(void)
{
3205
3206
    filestruct *line;
    int row = 0;
3207

3208
3209
3210
3211
3212
3213
#ifndef DISABLE_COLOR
    /* When needed, initialize the colors for the current syntax. */
    if (!have_palette)
	color_init();
#endif

3214
    /* If the current line is out of view, get it back on screen. */
3215
    if (current_is_offscreen()) {
3216
#ifdef DEBUG
3217
3218
	fprintf(stderr, "edit-refresh: line = %ld, edittop = %ld and editwinrows = %d\n",
		(long)openfile->current->lineno, (long)openfile->edittop->lineno, editwinrows);
3219
#endif
3220
	adjust_viewport((focusing || !ISSET(SMOOTH_SCROLL)) ? CENTERING : STATIONARY);
3221
    }
Chris Allegretta's avatar
Chris Allegretta committed
3222

3223
#ifdef DEBUG
3224
    fprintf(stderr, "edit-refresh: now edittop = %ld\n", (long)openfile->edittop->lineno);
3225
#endif
3226

3227
3228
3229
3230
    line = openfile->edittop;

    while (row < editwinrows && line != NULL) {
	if (line == openfile->current)
3231
	    row += update_line(line, openfile->current_x);
3232
	else
3233
	    row += update_line(line, 0);
3234
	line = line->next;
3235
3236
    }

3237
    while (row < editwinrows)
3238
	blank_row(edit, row++, 0, COLS);
3239

3240
    place_the_cursor();
3241
    wnoutrefresh(edit);
3242
3243

    refresh_needed = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
3244
3245
}

3246
3247
3248
3249
3250
/* Move edittop so that current is on the screen.  manner says how:
 * STATIONARY means that the cursor should stay on the same screen row,
 * CENTERING means that current should end up in the middle of the screen,
 * and FLOWING means that it should scroll no more than needed to bring
 * current into view. */
3251
void adjust_viewport(update_type manner)
Chris Allegretta's avatar
Chris Allegretta committed
3252
{
3253
    int goal = 0;
3254

3255
    if (manner == STATIONARY)
3256
	goal = openfile->current_y;
3257
3258
3259
3260
    else if (manner == CENTERING)
	goal = editwinrows / 2;
    else if (!current_is_above_screen())
	goal = editwinrows - 1;
3261

3262
    openfile->edittop = openfile->current;
3263
#ifndef NANO_TINY
3264
    if (ISSET(SOFTWRAP))
3265
	openfile->firstcolumn = leftedge_for(xplustabs(), openfile->current);
3266
#endif
3267
3268

    /* Move edittop back goal rows, starting at current[current_x]. */
3269
    go_back_chunks(goal, &openfile->edittop, &openfile->firstcolumn);
3270

3271
#ifdef DEBUG
3272
    fprintf(stderr, "adjust_viewport(): setting edittop to lineno %ld\n", (long)openfile->edittop->lineno);
3273
#endif
Chris Allegretta's avatar
Chris Allegretta committed
3274
3275
}

3276
/* Unconditionally redraw the entire screen. */
3277
void total_redraw(void)
3278
{
3279
3280
3281
3282
3283
3284
#ifdef USE_SLANG
    /* Slang curses emulation brain damage, part 4: Slang doesn't define
     * curscr. */
    SLsmg_touch_screen();
    SLsmg_refresh();
#else
3285
    wrefresh(curscr);
3286
#endif
3287
3288
}

3289
3290
/* Redraw the entire screen, then refresh the title bar and the content of
 * the edit window (when not in the file browser), and the bottom bars. */
3291
3292
void total_refresh(void)
{
3293
    total_redraw();
3294
3295
    if (currmenu != MBROWSER && currmenu != MWHEREISFILE && currmenu != MGOTODIR)
	titlebar(title);
3296
#ifdef ENABLE_HELP
3297
    if (inhelp)
3298
	wrap_the_help_text(TRUE);
3299
3300
    else
#endif
3301
    if (currmenu != MBROWSER && currmenu != MWHEREISFILE && currmenu != MGOTODIR)
3302
	edit_refresh();
3303
    bottombars(currmenu);
3304
3305
}

3306
3307
/* Display the main shortcut list on the last two rows of the bottom
 * portion of the window. */
3308
3309
void display_main_list(void)
{
3310
#ifndef DISABLE_COLOR
3311
3312
    if (openfile->syntax &&
		(openfile->syntax->formatter || openfile->syntax->linter))
3313
	set_lint_or_format_shortcuts();
3314
3315
3316
3317
    else
	set_spell_shortcuts();
#endif

3318
    bottombars(MMAIN);
3319
3320
}

3321
3322
3323
3324
/* Show info about the current cursor position on the statusbar.
 * Do this unconditionally when force is TRUE; otherwise, only if
 * suppress_cursorpos is FALSE.  In any case, reset the latter. */
void do_cursorpos(bool force)
Chris Allegretta's avatar
Chris Allegretta committed
3325
{
3326
3327
    char saved_byte;
    size_t sum, cur_xpt = xplustabs() + 1;
3328
    size_t cur_lenpt = strlenpt(openfile->current->data) + 1;
3329
    int linepct, colpct, charpct;
Chris Allegretta's avatar
Chris Allegretta committed
3330

3331
3332
3333
3334
3335
    /* If the showing needs to be suppressed, don't suppress it next time. */
    if (suppress_cursorpos && !force) {
	suppress_cursorpos = FALSE;
	return;
    }
3336

3337
3338
3339
    /* Hide the cursor while we are calculating. */
    curs_set(0);

3340
    /* Determine the size of the file up to the cursor. */
3341
    saved_byte = openfile->current->data[openfile->current_x];
3342
    openfile->current->data[openfile->current_x] = '\0';
3343

3344
    sum = get_totsize(openfile->fileage, openfile->current);
3345

3346
    openfile->current->data[openfile->current_x] = saved_byte;
3347
3348
3349

    /* When not at EOF, subtract 1 for an overcounted newline. */
    if (openfile->current != openfile->filebot)
3350
	sum--;
3351

3352
    /* Display the current cursor position on the statusbar. */
3353
    linepct = 100 * openfile->current->lineno / openfile->filebot->lineno;
3354
    colpct = 100 * cur_xpt / cur_lenpt;
3355
    charpct = (openfile->totsize == 0) ? 0 : 100 * sum / openfile->totsize;
3356

3357
    statusline(HUSH,
3358
	_("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%lu (%d%%)"),
3359
	(long)openfile->current->lineno,
3360
	(long)openfile->filebot->lineno, linepct,
3361
	(unsigned long)cur_xpt, (unsigned long)cur_lenpt, colpct,
3362
	(unsigned long)sum, (unsigned long)openfile->totsize, charpct);
3363
3364
3365

    /* Displaying the cursor position should not suppress it next time. */
    suppress_cursorpos = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
3366
3367
}

3368
/* Unconditionally display the current cursor position. */
3369
void do_cursorpos_void(void)
3370
{
3371
    do_cursorpos(TRUE);
3372
3373
}

3374
void disable_waiting(void)
3375
{
3376
    waiting_mode = FALSE;
3377
    nodelay(edit, TRUE);
3378
3379
}

3380
void enable_waiting(void)
3381
{
3382
    waiting_mode = TRUE;
3383
    nodelay(edit, FALSE);
3384
3385
}

3386
3387
3388
/* Highlight the text between from_col and to_col when active is TRUE.
 * Remove the highlight when active is FALSE. */
void spotlight(bool active, size_t from_col, size_t to_col)
Chris Allegretta's avatar
Chris Allegretta committed
3389
{
3390
3391
    char *word;
    size_t word_span, room;
Chris Allegretta's avatar
Chris Allegretta committed
3392

3393
    place_the_cursor();
3394

3395
3396
3397
3398
#ifndef NANO_TINY
    if (ISSET(SOFTWRAP)) {
	spotlight_softwrapped(active, from_col, to_col);
	return;
3399
    }
3400
#endif
3401

3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
    /* This is so we can show zero-length matches. */
    if (to_col == from_col) {
	word = mallocstrcpy(NULL, " ");
	to_col++;
    } else
	word = display_string(openfile->current->data, from_col,
				to_col - from_col, FALSE);

    word_span = strlenpt(word);

    /* Compute the number of columns that are available for the word. */
    room = editwincols + get_page_start(from_col) - from_col;

    /* If the word is partially offscreen, reserve space for the "$". */
    if (word_span > room)
	room--;

3419
    if (active)
3420
	wattron(edit, interface_color_pair[SELECTED_TEXT]);
Chris Allegretta's avatar
Chris Allegretta committed
3421

3422
    waddnstr(edit, word, actual_x(word, room));
3423

3424
    if (word_span > room)
3425
	waddch(edit, '$');
Chris Allegretta's avatar
Chris Allegretta committed
3426

3427
    if (active)
3428
	wattroff(edit, interface_color_pair[SELECTED_TEXT]);
3429

3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
    free(word);

    wnoutrefresh(edit);
}

#ifndef NANO_TINY
/* Highlight the text between from_col and to_col when active is TRUE; remove
 * the highlight when active is FALSE.  This will not highlight softwrapped
 * line breaks, since they're not actually part of the spotlighted text. */
void spotlight_softwrapped(bool active, size_t from_col, size_t to_col)
{
3441
    ssize_t row = openfile->current_y;
3442
    size_t leftedge = leftedge_for(from_col, openfile->current);
3443
    size_t break_col;
3444
    bool end_of_line = FALSE;
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
    char *word;

    while (row < editwinrows) {
	break_col = get_softwrap_breakpoint(openfile->current->data,
						leftedge, &end_of_line);

	/* Stop after the end of the word, by pretending the end of the word is
	 * the end of the line. */
	if (break_col >= to_col) {
	    end_of_line = TRUE;
	    break_col = to_col;
	}

	/* This is so we can show zero-length matches. */
	if (break_col == from_col) {
	    word = mallocstrcpy(NULL, " ");
	    break_col++;
	} else
	    word = display_string(openfile->current->data, from_col,
					break_col - from_col, FALSE);

	if (active)
3467
	    wattron(edit, interface_color_pair[SELECTED_TEXT]);
3468
3469
3470
3471

	waddnstr(edit, word, actual_x(word, break_col));

	if (active)
3472
	    wattroff(edit, interface_color_pair[SELECTED_TEXT]);
3473
3474
3475
3476
3477
3478

	free(word);

	if (end_of_line)
	    break;

3479
	wmove(edit, ++row, 0);
3480
3481
3482
3483
3484

	leftedge = break_col;
	from_col = break_col;
    }

3485
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3486
}
3487
#endif
Chris Allegretta's avatar
Chris Allegretta committed
3488

3489
#ifndef DISABLE_EXTRA
3490
3491
#define CREDIT_LEN 54
#define XLCREDIT_LEN 9
3492

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3493
3494
/* Easter egg: Display credits.  Assume nodelay(edit) and scrollok(edit)
 * are FALSE. */
3495
3496
void do_credits(void)
{
3497
3498
    bool old_more_space = ISSET(MORE_SPACE);
    bool old_no_help = ISSET(NO_HELP);
3499
    int kbinput = ERR, crpos = 0, xlpos = 0;
3500
3501
3502
    const char *credits[CREDIT_LEN] = {
	NULL,				/* "The nano text editor" */
	NULL,				/* "version" */
Chris Allegretta's avatar
Chris Allegretta committed
3503
3504
	VERSION,
	"",
3505
	NULL,				/* "Brought to you by:" */
Chris Allegretta's avatar
Chris Allegretta committed
3506
3507
3508
3509
3510
	"Chris Allegretta",
	"Jordi Mallach",
	"Adam Rogoyski",
	"Rob Siemborski",
	"Rocco Corsi",
3511
	"David Lawrence Ramsey",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3512
	"David Benbennick",
Benno Schulenberg's avatar
Benno Schulenberg committed
3513
	"Mark Majeres",
3514
	"Mike Frysinger",
3515
	"Benno Schulenberg",
Chris Allegretta's avatar
Chris Allegretta committed
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
	"Ken Tyler",
	"Sven Guckes",
	"Bill Soudan",
	"Christian Weisgerber",
	"Erik Andersen",
	"Big Gaute",
	"Joshua Jensen",
	"Ryan Krebs",
	"Albert Chin",
	"",
3526
	NULL,				/* "Special thanks to:" */
3527
	"Monique, Brielle & Joseph",
Chris Allegretta's avatar
Chris Allegretta committed
3528
3529
3530
3531
3532
3533
	"Plattsburgh State University",
	"Benet Laboratories",
	"Amy Allegretta",
	"Linda Young",
	"Jeremy Robichaud",
	"Richard Kolb II",
3534
	NULL,				/* "The Free Software Foundation" */
Chris Allegretta's avatar
Chris Allegretta committed
3535
	"Linus Torvalds",
3536
	NULL,				/* "the many translators and the TP" */
3537
	NULL,				/* "For ncurses:" */
3538
3539
3540
3541
	"Thomas Dickey",
	"Pavel Curtis",
	"Zeyd Ben-Halim",
	"Eric S. Raymond",
3542
3543
3544
3545
3546
3547
	NULL,				/* "and anyone else we forgot..." */
	NULL,				/* "Thank you for using nano!" */
	"",
	"",
	"",
	"",
3548
	"(C) 2017",
3549
	"Free Software Foundation, Inc.",
3550
3551
3552
3553
	"",
	"",
	"",
	"",
3554
	"https://nano-editor.org/"
3555
3556
    };

3557
    const char *xlcredits[XLCREDIT_LEN] = {
3558
3559
3560
3561
3562
	N_("The nano text editor"),
	N_("version"),
	N_("Brought to you by:"),
	N_("Special thanks to:"),
	N_("The Free Software Foundation"),
3563
	N_("the many translators and the TP"),
3564
3565
3566
	N_("For ncurses:"),
	N_("and anyone else we forgot..."),
	N_("Thank you for using nano!")
3567
    };
3568

3569
3570
3571
3572
3573
3574
    if (!old_more_space || !old_no_help) {
	SET(MORE_SPACE);
	SET(NO_HELP);
	window_init();
    }

3575
3576
    curs_set(0);
    nodelay(edit, TRUE);
3577

3578
    blank_titlebar();
Chris Allegretta's avatar
Chris Allegretta committed
3579
    blank_edit();
3580
    blank_statusbar();
3581

3582
    wrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
3583
    wrefresh(edit);
3584
    wrefresh(bottomwin);
3585
    napms(700);
3586

3587
    for (crpos = 0; crpos < CREDIT_LEN + editwinrows / 2; crpos++) {
3588
	if ((kbinput = wgetch(edit)) != ERR)
3589
	    break;
3590

3591
	if (crpos < CREDIT_LEN) {
3592
	    const char *what;
3593
	    size_t start_col;
3594

3595
3596
3597
	    if (credits[crpos] == NULL)
		what = _(xlcredits[xlpos++]);
	    else
3598
		what = credits[crpos];
3599

3600
	    start_col = COLS / 2 - strlenpt(what) / 2 - 1;
3601
	    mvwaddstr(edit, editwinrows - 1 - (editwinrows % 2),
3602
						start_col, what);
3603
	}
3604

3605
3606
3607
3608
	wrefresh(edit);

	if ((kbinput = wgetch(edit)) != ERR)
	    break;
3609
	napms(700);
3610

3611
	scrollok(edit, TRUE);
3612
	wscrl(edit, 1);
3613
	scrollok(edit, FALSE);
3614
	wrefresh(edit);
3615

3616
	if ((kbinput = wgetch(edit)) != ERR)
3617
	    break;
3618
	napms(700);
3619

3620
	scrollok(edit, TRUE);
3621
	wscrl(edit, 1);
3622
	scrollok(edit, FALSE);
3623
	wrefresh(edit);
3624
3625
    }

3626
3627
3628
    if (kbinput != ERR)
	ungetch(kbinput);

3629
    if (!old_more_space)
3630
	UNSET(MORE_SPACE);
3631
    if (!old_no_help)
3632
	UNSET(NO_HELP);
3633
    window_init();
3634

3635
    nodelay(edit, FALSE);
3636

3637
    total_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
3638
}
3639
#endif /* !DISABLE_EXTRA */