winio.c 106 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
/**************************************************************************
 *   winio.c                                                              *
 *                                                                        *
5
 *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,  *
6
 *   2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.    *
Chris Allegretta's avatar
Chris Allegretta committed
7
8
 *   This program is free software; you can redistribute it and/or modify *
 *   it under the terms of the GNU General Public License as published by *
9
 *   the Free Software Foundation; either version 3, or (at your option)  *
Chris Allegretta's avatar
Chris Allegretta committed
10
11
 *   any later version.                                                   *
 *                                                                        *
12
13
14
15
 *   This program is distributed in the hope that it will be useful, but  *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    *
 *   General Public License for more details.                             *
Chris Allegretta's avatar
Chris Allegretta committed
16
17
18
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
19
20
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
 *   02110-1301, USA.                                                     *
Chris Allegretta's avatar
Chris Allegretta committed
21
22
23
 *                                                                        *
 **************************************************************************/

24
#include "proto.h"
25

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

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

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

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

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

115
#ifndef NANO_TINY
116
117
    allow_pending_sigwinch(TRUE);
#endif
118

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

123
    /* Read in the first character using whatever mode we're in. */
124
    errcount = 0;
125
    if (nodelay_mode) {
126
	if ((input = wgetch(win)) == ERR)
127
128
129
130
131
132
133
134
135
136
137
138
139
           return;
    } else
	while ((input = wgetch(win)) == ERR) {
	    errcount++;

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

141
#ifndef NANO_TINY
142
143
    allow_pending_sigwinch(FALSE);
#endif
144

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

151
152
153
154
    /* Read in the remaining characters using non-blocking input. */
    nodelay(win, TRUE);

    while (TRUE) {
155
#ifndef NANO_TINY
156
	allow_pending_sigwinch(TRUE);
157
#endif
158

159
	input = wgetch(win);
160

161
	/* If there aren't any more characters, stop reading. */
162
	if (input == ERR)
163
164
	    break;

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

172
#ifndef NANO_TINY
173
174
	allow_pending_sigwinch(FALSE);
#endif
175
176
    }

177
    /* Switch back to waiting mode for input. */
178
    nodelay(win, FALSE);
179
180

#ifdef DEBUG
181
    fprintf(stderr, "get_key_buffer(): key_buffer_len = %lu\n", (unsigned long)key_buffer_len);
182
#endif
183
}
184

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
185
/* Return the length of the keystroke buffer. */
186
size_t get_key_buffer_len(void)
187
188
189
190
{
    return key_buffer_len;
}

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

199
    /* If input is empty, get out. */
200
    if (input_len == 0)
201
202
	return;

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
209
210
211
    /* 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. */
212
213
214
    key_buffer_len += input_len;
    key_buffer = (int *)nrealloc(key_buffer, key_buffer_len *
	sizeof(int));
215

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
222
    /* Copy input to the beginning of the keystroke buffer. */
223
    memcpy(key_buffer, input, input_len * sizeof(int));
224
225
}

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

235
    unget_input(&kbinput, 1);
236
237

    if (meta_key) {
238
239
	kbinput = NANO_CONTROL_3;
	unget_input(&kbinput, 1);
240
241
242
    }
}

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

252
#ifndef NANO_TINY
253
    allow_pending_sigwinch(TRUE);
254
255
256
    allow_pending_sigwinch(FALSE);
#endif

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

261
262
263
	    if (key_buffer_len == 0)
		return NULL;
	} else
264
265
266
	    return NULL;
    }

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

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

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

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

    return input;
297
298
}

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

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

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

322
323
324
325
326
327
    return kbinput;
}

/* Translate ASCII characters, extended keypad values, and escape
 * sequences into their corresponding key values.  Set meta_key to TRUE
 * when we get a meta key sequence, and set func_key to TRUE when we get
328
 * a function key. */
329
int parse_kbinput(WINDOW *win, bool *meta_key, bool *func_key)
330
{
331
    static int escapes = 0, byte_digits = 0;
332
    int *kbinput, retval = ERR;
333

334
335
    *meta_key = FALSE;
    *func_key = FALSE;
336

337
    /* Read in a character. */
338
339
340
341
342
    if (nodelay_mode) {
	kbinput = get_input(win, 1);
	if (kbinput == 0)
	    return 0;
    } else
343
344
	while ((kbinput = get_input(win, 1)) == NULL)
	    ;
345

346
347
348
349
350
351
352
353
354
355
356
    switch (*kbinput) {
	case ERR:
	    break;
	case NANO_CONTROL_3:
	    /* Increment the escape counter. */
	    escapes++;
	    switch (escapes) {
		case 1:
		    /* One escape: wait for more input. */
		case 2:
		    /* Two escapes: wait for more input. */
357
358
		case 3:
		    /* Three escapes: wait for more input. */
359
360
		    break;
		default:
361
362
363
		    /* More than three escapes: limit the escape counter
		     * to no more than two, and wait for more input. */
		    escapes %= 3;
364
365
366
367
368
	    }
	    break;
	default:
	    switch (escapes) {
		case 0:
369
370
371
		    /* One non-escape: normal input mode.  Save the
		     * non-escape character as the result. */
		    retval = *kbinput;
372
373
		    break;
		case 1:
374
		    /* Reset the escape counter. */
375
		    escapes = 0;
376
		    if (get_key_buffer_len() == 0) {
377
			/* One escape followed by a non-escape, and
378
379
380
381
			 * there aren't any other keystrokes waiting:
			 * meta key sequence mode.  Set meta_key to
			 * TRUE, and save the lowercase version of the
			 * non-escape character as the result. */
382
383
			*meta_key = TRUE;
			retval = tolower(*kbinput);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
384
		    } else
385
			/* One escape followed by a non-escape, and
386
387
388
			 * there are other keystrokes waiting: escape
			 * sequence mode.  Interpret the escape
			 * sequence. */
389
390
			retval = parse_escape_seq_kbinput(win,
				*kbinput);
391
392
		    break;
		case 2:
393
394
395
396
397
398
		    if (get_key_buffer_len() == 0) {
			if (('0' <= *kbinput && *kbinput <= '2' &&
				byte_digits == 0) || ('0' <= *kbinput &&
				*kbinput <= '9' && byte_digits > 0)) {
			    /* Two escapes followed by one or more
			     * decimal digits, and there aren't any
399
400
401
402
403
404
405
406
407
			     * other keystrokes waiting: byte sequence
			     * mode.  If the byte sequence's range is
			     * limited to 2XX (the first digit is in the
			     * '0' to '2' range and it's the first
			     * digit, or it's in the '0' to '9' range
			     * and it's not the first digit), increment
			     * the byte sequence counter and interpret
			     * the digit.  If the byte sequence's range
			     * is not limited to 2XX, fall through. */
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
			    int byte;

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

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

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

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

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

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

				unget_input(seq, byte_mb_len);

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

499
    if (retval != ERR) {
500
501
	switch (retval) {
	    case NANO_CONTROL_8:
502
503
		retval = ISSET(REBIND_DELETE) ? sc_seq_or(do_delete, 0) :
			sc_seq_or(do_backspace, 0);
504
505
		break;
	    case KEY_DOWN:
506
507
508
509
#ifdef KEY_SDOWN
	    /* ncurses and Slang don't support KEY_SDOWN. */
	    case KEY_SDOWN:
#endif
510
		retval = sc_seq_or(do_down_void, *kbinput);
511
512
		break;
	    case KEY_UP:
513
514
515
516
#ifdef KEY_SUP
	    /* ncurses and Slang don't support KEY_SUP. */
	    case KEY_SUP:
#endif
517
		retval = sc_seq_or(do_up_void, *kbinput);
518
519
		break;
	    case KEY_LEFT:
520
521
522
523
#ifdef KEY_SLEFT
	    /* Slang doesn't support KEY_SLEFT. */
	    case KEY_SLEFT:
#endif
524
		retval = sc_seq_or(do_left, *kbinput);
525
526
		break;
	    case KEY_RIGHT:
527
528
529
530
#ifdef KEY_SRIGHT
	    /* Slang doesn't support KEY_SRIGHT. */
	    case KEY_SRIGHT:
#endif
531
		retval = sc_seq_or(do_right, *kbinput);
532
		break;
533
534
535
536
537
538
#ifdef KEY_SHOME
	    /* HP-UX 10-11 and Slang don't support KEY_SHOME. */
	    case KEY_SHOME:
#endif
	    case KEY_A1:	/* Home (7) on numeric keypad with
				 * NumLock off. */
539
		retval = sc_seq_or(do_home, *kbinput);
540
		break;
541
	    case KEY_BACKSPACE:
542
		retval = sc_seq_or(do_backspace, *kbinput);
543
		break;
544
545
546
547
#ifdef KEY_SDC
	    /* Slang doesn't support KEY_SDC. */
	    case KEY_SDC:
		if (ISSET(REBIND_DELETE))
548
		    retval = sc_seq_or(do_delete, *kbinput);
549
		else
550
		    retval = sc_seq_or(do_backspace, *kbinput);
551
		break;
552
#endif
553
554
555
#ifdef KEY_SIC
	    /* Slang doesn't support KEY_SIC. */
	    case KEY_SIC:
556
		retval = sc_seq_or(do_insertfile_void, *kbinput);
557
		break;
558
#endif
559
	    case KEY_C3:	/* PageDown (4) on numeric keypad with
560
				 * NumLock off. */
561
		retval = sc_seq_or(do_page_down, *kbinput);
562
563
564
		break;
	    case KEY_A3:	/* PageUp (9) on numeric keypad with
				 * NumLock off. */
565
		retval = sc_seq_or(do_page_up, *kbinput);
566
567
		break;
	    case KEY_ENTER:
568
		retval = sc_seq_or(do_enter_void, *kbinput);
569
570
571
572
573
574
575
		break;
	    case KEY_B2:	/* Center (5) on numeric keypad with
				 * NumLock off. */
		retval = ERR;
		break;
	    case KEY_C1:	/* End (1) on numeric keypad with
				 * NumLock off. */
576
577
578
579
#ifdef KEY_SEND
	    /* HP-UX 10-11 and Slang don't support KEY_SEND. */
	    case KEY_SEND:
#endif
580
		retval = sc_seq_or(do_end, *kbinput);
581
582
583
584
585
586
587
588
		break;
#ifdef KEY_BEG
	    /* Slang doesn't support KEY_BEG. */
	    case KEY_BEG:	/* Center (5) on numeric keypad with
				 * NumLock off. */
		retval = ERR;
		break;
#endif
589
590
591
#ifdef KEY_CANCEL
	    /* Slang doesn't support KEY_CANCEL. */
	    case KEY_CANCEL:
592
593
594
#ifdef KEY_SCANCEL
	    /* Slang doesn't support KEY_SCANCEL. */
	    case KEY_SCANCEL:
595
#endif
596
		retval = first_sc_for(currmenu, do_cancel)->seq;
597
598
599
600
601
602
603
604
605
606
607
608
		break;
#endif
#ifdef KEY_SBEG
	    /* Slang doesn't support KEY_SBEG. */
	    case KEY_SBEG:	/* Center (5) on numeric keypad with
				 * NumLock off. */
		retval = ERR;
		break;
#endif
#ifdef KEY_SSUSPEND
	    /* Slang doesn't support KEY_SSUSPEND. */
	    case KEY_SSUSPEND:
609
		retval = sc_seq_or(do_suspend_void, 0);
610
611
612
613
614
		break;
#endif
#ifdef KEY_SUSPEND
	    /* Slang doesn't support KEY_SUSPEND. */
	    case KEY_SUSPEND:
615
		retval = sc_seq_or(do_suspend_void, 0);
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
		break;
#endif
#ifdef PDCURSES
	    case KEY_SHIFT_L:
	    case KEY_SHIFT_R:
	    case KEY_CONTROL_L:
	    case KEY_CONTROL_R:
	    case KEY_ALT_L:
	    case KEY_ALT_R:
		retval = ERR;
		break;
#endif
#if !defined(NANO_TINY) && defined(KEY_RESIZE)
	    /* Since we don't change the default SIGWINCH handler when
	     * NANO_TINY is defined, KEY_RESIZE is never generated.
	     * Also, Slang and SunOS 5.7-5.9 don't support
	     * KEY_RESIZE. */
	    case KEY_RESIZE:
		retval = ERR;
		break;
#endif
	}
638

639
	/* If our result is an extended keypad value (i.e. a value
640
	 * outside of byte range), set func_key to TRUE. */
641
642
	if (retval != ERR)
	    *func_key = !is_byte(retval);
643
    }
644
645

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

649
650
    free(kbinput);

651
    /* Return the result. */
652
653
654
    return retval;
}

655
/* Translate escape sequences, most of which correspond to extended
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
656
 * keypad values, into their corresponding key values.  These sequences
657
658
659
 * are generated when the keypad doesn't support the needed keys.
 * Assume that Escape has already been read in. */
int get_escape_seq_kbinput(const int *seq, size_t seq_len)
660
{
661
    int retval = ERR;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
662

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

1211
#ifdef DEBUG
1212
    fprintf(stderr, "get_escape_seq_kbinput(): retval = %d\n", retval);
1213
#endif
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1214

1215
    return retval;
1216
1217
}

1218
/* Return the equivalent arrow key value for the case-insensitive
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1219
 * letters A (up), B (down), C (right), and D (left).  These are common
1220
1221
1222
1223
1224
 * to many escape sequences. */
int get_escape_seq_abcd(int kbinput)
{
    switch (tolower(kbinput)) {
	case 'a':
1225
	    return sc_seq_or(do_up_void, 0);
1226
	case 'b':
1227
	    return sc_seq_or(do_down_void, 0);
1228
	case 'c':
1229
	    return sc_seq_or(do_right, 0);
1230
	case 'd':
1231
	    return sc_seq_or(do_left, 0);
1232
1233
1234
1235
1236
	default:
	    return ERR;
    }
}

1237
/* Interpret the escape sequence in the keystroke buffer, the first
1238
1239
1240
 * character of which is kbinput.  Assume that the keystroke buffer
 * isn't empty, and that the initial escape has already been read in. */
int parse_escape_seq_kbinput(WINDOW *win, int kbinput)
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
{
    int retval, *seq;
    size_t seq_len;

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

    free(seq);

1255
1256
    /* If we got an unrecognized escape sequence, throw it out. */
    if (retval == ERR) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1257
	if (win == edit) {
1258
1259
1260
1261
1262
	    statusbar(_("Unknown Command"));
	    beep();
	}
    }

1263
#ifdef DEBUG
1264
    fprintf(stderr, "parse_escape_seq_kbinput(): kbinput = %d, seq_len = %lu, retval = %d\n", kbinput, (unsigned long)seq_len, retval);
1265
1266
1267
1268
1269
#endif

    return retval;
}

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

1277
1278
    /* Increment the byte digit counter. */
    byte_digits++;
1279

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

    /* If we have a result, reset the byte digit counter and the byte
     * sequence holder. */
    if (retval != ERR) {
	byte_digits = 0;
1331
	byte = 0;
1332
1333
1334
    }

#ifdef DEBUG
1335
    fprintf(stderr, "get_byte_kbinput(): kbinput = %d, byte_digits = %d, byte = %d, retval = %d\n", kbinput, byte_digits, byte, retval);
1336
1337
1338
1339
1340
#endif

    return retval;
}

1341
#ifdef ENABLE_UTF8
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
/* If the character in kbinput is a valid hexadecimal digit, multiply it
 * by factor and add the result to uni. */
long add_unicode_digit(int kbinput, long factor, long *uni)
{
    long retval = ERR;

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

    return retval;
}

1360
/* Translate a Unicode sequence: turn a six-digit hexadecimal number
1361
 * (from 000000 to 10FFFF, case-insensitive) into its corresponding
1362
 * multibyte value. */
1363
long get_unicode_kbinput(int kbinput)
1364
{
1365
1366
1367
    static int uni_digits = 0;
    static long uni = 0;
    long retval = ERR;
1368

1369
    /* Increment the Unicode digit counter. */
1370
    uni_digits++;
1371

1372
    switch (uni_digits) {
1373
	case 1:
1374
1375
	    /* First digit: This must be zero or one.  Put it in the
	     * 0x100000's position of the Unicode sequence holder. */
1376
	    if ('0' <= kbinput && kbinput <= '1')
1377
		uni = (kbinput - '0') * 0x100000;
1378
	    else
1379
1380
		/* This isn't the first digit of a Unicode sequence.
		 * Return this character as the result. */
1381
1382
1383
		retval = kbinput;
	    break;
	case 2:
1384
1385
1386
1387
1388
1389
	    /* Second digit: This must be zero if the first was one, and
	     * may be any hexadecimal value if the first was zero.  Put
	     * it in the 0x10000's position of the Unicode sequence
	     * holder. */
	    if (uni == 0 || '0' == kbinput)
		retval = add_unicode_digit(kbinput, 0x10000, &uni);
1390
	    else
1391
1392
		/* This isn't the second digit of a Unicode sequence.
		 * Return this character as the result. */
1393
1394
1395
		retval = kbinput;
	    break;
	case 3:
1396
1397
1398
1399
	    /* Third digit: This may be any hexadecimal value.  Put it
	     * in the 0x1000's position of the Unicode sequence
	     * holder. */
	    retval = add_unicode_digit(kbinput, 0x1000, &uni);
1400
	    break;
1401
	case 4:
1402
1403
1404
1405
	    /* Fourth digit: This may be any hexadecimal value.  Put it
	     * in the 0x100's position of the Unicode sequence
	     * holder. */
	    retval = add_unicode_digit(kbinput, 0x100, &uni);
1406
	    break;
1407
	case 5:
1408
1409
1410
	    /* Fifth digit: This may be any hexadecimal value.  Put it
	     * in the 0x10's position of the Unicode sequence holder. */
	    retval = add_unicode_digit(kbinput, 0x10, &uni);
1411
	    break;
1412
	case 6:
1413
1414
1415
1416
1417
1418
	    /* Sixth digit: This may be any hexadecimal value.  Put it
	     * in the 0x1's position of the Unicode sequence holder. */
	    retval = add_unicode_digit(kbinput, 0x1, &uni);
	    /* If this character is a valid hexadecimal value, then the
	     * Unicode sequence is complete. */
	    if (retval == ERR)
1419
		retval = uni;
1420
1421
	    break;
	default:
1422
1423
1424
	    /* If there are more than six digits, return this character
	     * as the result.  (Maybe we should produce an error
	     * instead?) */
1425
1426
1427
	    retval = kbinput;
	    break;
    }
1428

1429
1430
    /* If we have a result, reset the Unicode digit counter and the
     * Unicode sequence holder. */
1431
    if (retval != ERR) {
1432
1433
	uni_digits = 0;
	uni = 0;
1434
    }
1435

1436
#ifdef DEBUG
1437
    fprintf(stderr, "get_unicode_kbinput(): kbinput = %d, uni_digits = %d, uni = %ld, retval = %ld\n", kbinput, uni_digits, uni, retval);
1438
1439
#endif

1440
1441
    return retval;
}
1442
#endif /* ENABLE_UTF8 */
1443

1444
1445
1446
1447
1448
1449
/* Translate a control character sequence: turn an ASCII non-control
 * character into its corresponding control character. */
int get_control_kbinput(int kbinput)
{
    int retval;

1450
     /* Ctrl-Space (Ctrl-2, Ctrl-@, Ctrl-`) */
1451
1452
    if (kbinput == ' ' || kbinput == '2')
	retval = NANO_CONTROL_SPACE;
1453
1454
    /* Ctrl-/ (Ctrl-7, Ctrl-_) */
    else if (kbinput == '/')
1455
	retval = NANO_CONTROL_7;
1456
    /* Ctrl-3 (Ctrl-[, Esc) to Ctrl-7 (Ctrl-/, Ctrl-_) */
1457
1458
1459
    else if ('3' <= kbinput && kbinput <= '7')
	retval = kbinput - 24;
    /* Ctrl-8 (Ctrl-?) */
1460
1461
    else if (kbinput == '8' || kbinput == '?')
	retval = NANO_CONTROL_8;
1462
1463
    /* Ctrl-@ (Ctrl-Space, Ctrl-2, Ctrl-`) to Ctrl-_ (Ctrl-/, Ctrl-7) */
    else if ('@' <= kbinput && kbinput <= '_')
1464
	retval = kbinput - '@';
1465
1466
    /* Ctrl-` (Ctrl-2, Ctrl-Space, Ctrl-@) to Ctrl-~ (Ctrl-6, Ctrl-^) */
    else if ('`' <= kbinput && kbinput <= '~')
1467
	retval = kbinput - '`';
1468
1469
1470
    else
	retval = kbinput;

1471
#ifdef DEBUG
1472
    fprintf(stderr, "get_control_kbinput(): kbinput = %d, retval = %d\n", kbinput, retval);
1473
1474
#endif

1475
1476
    return retval;
}
1477

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1478
1479
/* Put the output-formatted characters in output back into the keystroke
 * buffer, so that they can be parsed and displayed as output again. */
1480
void unparse_kbinput(char *output, size_t output_len)
1481
{
1482
1483
    int *input;
    size_t i;
1484

1485
1486
1487
1488
    if (output_len == 0)
	return;

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

1490
1491
    for (i = 0; i < output_len; i++)
	input[i] = (int)output[i];
1492

1493
    unget_input(input, output_len);
1494

1495
    free(input);
1496
1497
}

1498
/* Read in a stream of characters verbatim, and return the length of the
1499
1500
1501
1502
 * string in kbinput_len.  Assume nodelay(win) is FALSE. */
int *get_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
{
    int *retval;
1503

1504
    /* Turn off flow control characters if necessary so that we can type
1505
1506
     * them in verbatim, and turn the keypad off if necessary so that we
     * don't get extended keypad values. */
1507
1508
    if (ISSET(PRESERVE))
	disable_flow_control();
1509
1510
    if (!ISSET(REBIND_KEYPAD))
	keypad(win, FALSE);
1511
1512
1513

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

    /* Turn flow control characters back on if necessary and turn the
1516
     * keypad back on if necessary now that we're done. */
1517
1518
    if (ISSET(PRESERVE))
	enable_flow_control();
1519
1520
    if (!ISSET(REBIND_KEYPAD))
	keypad(win, TRUE);
1521

1522
    return retval;
1523
1524
}

1525
1526
/* Read in a stream of all available characters, and return the length
 * of the string in kbinput_len.  Translate the first few characters of
1527
 * the input into the corresponding multibyte value if possible.  After
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1528
 * that, leave the input as-is. */
1529
int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
1530
{
1531
    int *kbinput, *retval;
1532

1533
    /* Read in the first keystroke. */
1534
1535
    while ((kbinput = get_input(win, 1)) == NULL)
	;
1536

1537
#ifdef ENABLE_UTF8
1538
1539
1540
    if (using_utf8()) {
	/* Check whether the first keystroke is a valid hexadecimal
	 * digit. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1541
	long uni = get_unicode_kbinput(*kbinput);
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560

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

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

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

	    while (uni == ERR) {
1561
1562
		while ((kbinput = get_input(win, 1)) == NULL)
		    ;
1563
1564
		uni = get_unicode_kbinput(*kbinput);
	    }
1565

1566
1567
1568
	    /* Put back the multibyte equivalent of the Unicode
	     * value. */
	    uni_mb = make_mbchar(uni, &uni_mb_len);
1569

1570
	    seq = (int *)nmalloc(uni_mb_len * sizeof(int));
1571

1572
1573
	    for (i = 0; i < uni_mb_len; i++)
		seq[i] = (unsigned char)uni_mb[i];
1574

1575
	    unget_input(seq, uni_mb_len);
1576

1577
1578
	    free(seq);
	    free(uni_mb);
1579
	}
1580
    } else
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1581
1582
#endif /* ENABLE_UTF8 */

1583
1584
	/* Put back the first keystroke. */
	unget_input(kbinput, 1);
1585

1586
1587
    free(kbinput);

1588
    /* Get the complete sequence, and save the characters in it as the
1589
     * result. */
1590
    *kbinput_len = get_key_buffer_len();
1591
    retval = get_input(NULL, *kbinput_len);
1592
1593
1594
1595

    return retval;
}

1596
#ifndef DISABLE_MOUSE
1597
/* Handle any mouse event that may have occurred.  We currently handle
1598
1599
1600
1601
1602
1603
1604
 * 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
1605
1606
1607
1608
1609
1610
 * 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. */
1611
int get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
1612
1613
{
    MEVENT mevent;
1614
    bool in_bottomwin;
1615
    subnfunc *f;
1616
1617
1618
1619
1620
1621

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

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

1624
1625
1626
    /* Save the screen coordinates where the mouse event took place. */
    *mouse_x = mevent.x;
    *mouse_y = mevent.y;
1627

1628
1629
    in_bottomwin = wenclose(bottomwin, *mouse_y, *mouse_x);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1630
    /* Handle releases/clicks of the first mouse button. */
1631
    if (mevent.bstate & (BUTTON1_RELEASED | BUTTON1_CLICKED)) {
1632
1633
	/* If we're allowing shortcuts, the current shortcut list is
	 * being displayed on the last two lines of the screen, and the
1634
1635
1636
	 * 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. */
1637
	if (allow_shortcuts && !ISSET(NO_HELP) && in_bottomwin) {
1638
1639
1640
1641
	    int i;
		/* The width of all the shortcuts, except for the last
		 * two, in the shortcut list in bottomwin. */
	    int j;
1642
		/* The calculated index number of the clicked item. */
1643
1644
1645
1646
	    size_t currslen;
		/* The number of shortcuts in the current shortcut
		 * list. */

1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
	    /* Translate the mouse event coordinates so that they're
	     * relative to bottomwin. */
	    wmouse_trafo(bottomwin, mouse_y, mouse_x, FALSE);

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

		return 0;
	    }
1661

1662
	    /* Get the shortcut lists' length. */
1663
	    if (currmenu == MMAIN)
1664
		currslen = MAIN_VISIBLE;
1665
	    else {
1666
		currslen = length_of_list(currmenu);
1667

1668
1669
1670
1671
1672
		/* We don't show any more shortcuts than the main list
		 * does. */
		if (currslen > MAIN_VISIBLE)
		    currslen = MAIN_VISIBLE;
	    }
1673

1674
1675
1676
1677
1678
1679
1680
1681
	    /* Calculate the width of all of the shortcuts in the list
	     * except for the last two, which are longer by (COLS % i)
	     * columns so as to not waste space. */
	    if (currslen < 2)
		i = COLS / (MAIN_VISIBLE / 2);
	    else
		i = COLS / ((currslen / 2) + (currslen % 2));

1682
1683
	    /* Calculate the one-based index in the shortcut list. */
	    j = (*mouse_x / i) * 2 + *mouse_y;
1684

1685
1686
	    /* Adjust the index if we hit the last two wider ones. */
	    if ((j > currslen) && (*mouse_x % i < COLS % i))
1687
		j -= 2;
1688
1689
1690
#ifdef DEBUG
	    fprintf(stderr, "Calculated %i as index in shortcut list, currmenu = %x.\n", j, currmenu);
#endif
1691
1692
	    /* Ignore releases/clicks of the first mouse button beyond
	     * the last shortcut. */
1693
	    if (j > currslen)
1694
		return 2;
1695

1696
1697
	    /* Go through the list of functions to determine which
	     * shortcut in the current menu we released/clicked on. */
1698
1699
1700
	    for (f = allfuncs; f != NULL; f = f->next) {
	        if ((f->menus & currmenu) == 0)
		    continue;
1701
#ifndef DISABLE_HELP
1702
1703
		if (!f->help || strlen(f->help) == 0)
		    continue;
1704
#endif
1705
1706
1707
1708
		if (first_sc_for(currmenu, f->scfunc) == NULL)
	            continue;
		/* Tick off an actually shown shortcut. */
		j -= 1;
1709
1710
		if (j == 0)
		    break;
1711
	    }
1712
#ifdef DEBUG
1713
	    fprintf(stderr, "Stopped on func %ld present in menus %x\n", (long)f->scfunc, f->menus);
1714
#endif
1715

1716
	    /* And put the corresponding key into the keyboard buffer. */
1717
	    if (f != NULL) {
1718
                const sc *s = first_sc_for(currmenu, f->scfunc);
1719
		unget_kbinput(s->seq, s->type == META, s->type == FKEY);
1720
	    }
1721
	    return 1;
1722
	} else
1723
1724
	    /* Handle releases/clicks of the first mouse button that
	     * aren't on the current shortcut list elsewhere. */
1725
	    return 0;
1726
    }
1727
1728
#if NCURSES_MOUSE_VERSION >= 2
    /* Handle presses of the fourth mouse button (upward rolls of the
1729
1730
1731
     * 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
1732
	bool in_edit = wenclose(edit, *mouse_y, *mouse_x);
1733

1734
1735
1736
1737
	if (in_bottomwin)
	    /* Translate the mouse event coordinates so that they're
	     * relative to bottomwin. */
	    wmouse_trafo(bottomwin, mouse_y, mouse_x, FALSE);
1738

1739
	if (in_edit || (in_bottomwin && *mouse_y == 0)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1740
1741
	    int i;

1742
1743
1744
1745
1746
	    /* 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) ?
1747
			sc_seq_or(do_up_void, 0) : sc_seq_or(do_down_void, 0),
1748
			FALSE, FALSE);
1749
1750
1751
1752
1753
1754
1755

	    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;
1756
1757
    }
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1758
1759
1760

    /* Ignore all other mouse events. */
    return 2;
1761
}
1762
1763
#endif /* !DISABLE_MOUSE */

1764
1765
1766
1767
1768
/* 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. */
const sc *get_shortcut(int menu, int *kbinput, bool *meta_key)
1769
{
1770
    sc *s;
1771

1772
#ifdef DEBUG
1773
    fprintf(stderr, "get_shortcut(): kbinput = %d, meta_key = %s -- ", *kbinput, *meta_key ? "TRUE" : "FALSE");
1774
1775
#endif

1776
    for (s = sclist; s != NULL; s = s->next) {
1777
1778
        if ((menu & s->menu) && *kbinput == s->seq
		&& *meta_key == (s->type == META)) {
1779
#ifdef DEBUG
1780
1781
	    fprintf (stderr, "matched seq \"%s\", and btw meta was %d (menu is %x from %x)\n",
			     s->keystr, *meta_key, menu, s->menu);
1782
#endif
1783
	    return s;
1784
1785
	}
    }
1786
#ifdef DEBUG
1787
    fprintf (stderr, "matched nothing, btw meta was %d\n", *meta_key);
1788
#endif
1789
1790
1791
1792

    return NULL;
}

1793
/* Try to get a function back from a window.  Just a wrapper. */
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
const subnfunc *getfuncfromkey(WINDOW *win)
{
    int kbinput;
    bool func_key = FALSE, meta_key = FALSE;
    const sc *s;

    kbinput = parse_kbinput(win, &meta_key, &func_key);
    if (kbinput == 0)
	return NULL;

1804
    s = get_shortcut(currmenu, &kbinput, &meta_key);
1805
1806
1807
    if (!s)
	return NULL;

1808
    return sctofunc((sc *) s);
1809
1810
}

1811
1812
1813
1814
1815
/* Move to (x, y) in win, and display a line of n spaces with the
 * current attributes. */
void blank_line(WINDOW *win, int y, int x, int n)
{
    wmove(win, y, x);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1816

1817
1818
1819
1820
    for (; n > 0; n--)
	waddch(win, ' ');
}

1821
/* Blank the first line of the top portion of the window. */
1822
void blank_titlebar(void)
Chris Allegretta's avatar
Chris Allegretta committed
1823
{
1824
    blank_line(topwin, 0, 0, COLS);
1825
1826
}

1827
1828
/* If the MORE_SPACE flag isn't set, blank the second line of the top
 * portion of the window. */
1829
1830
1831
void blank_topbar(void)
{
    if (!ISSET(MORE_SPACE))
1832
	blank_line(topwin, 1, 0, COLS);
1833
1834
}

1835
/* Blank all the lines of the middle portion of the window, i.e. the
1836
 * edit window. */
Chris Allegretta's avatar
Chris Allegretta committed
1837
1838
void blank_edit(void)
{
1839
    int i;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1840

1841
    for (i = 0; i < editwinrows; i++)
1842
	blank_line(edit, i, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1843
1844
}

1845
/* Blank the first line of the bottom portion of the window. */
Chris Allegretta's avatar
Chris Allegretta committed
1846
1847
void blank_statusbar(void)
{
1848
    blank_line(bottomwin, 0, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1849
1850
}

1851
1852
/* If the NO_HELP flag isn't set, blank the last two lines of the bottom
 * portion of the window. */
1853
1854
1855
void blank_bottombars(void)
{
    if (!ISSET(NO_HELP)) {
1856
1857
	blank_line(bottomwin, 1, 0, COLS);
	blank_line(bottomwin, 2, 0, COLS);
1858
1859
1860
    }
}

1861
1862
1863
/* Check if the number of keystrokes needed to blank the statusbar has
 * been pressed.  If so, blank the statusbar, unless constant cursor
 * position display is on. */
1864
void check_statusblank(void)
Chris Allegretta's avatar
Chris Allegretta committed
1865
{
1866
    if (statusblank > 0) {
1867
	statusblank--;
1868

1869
1870
1871
1872
1873
1874
	if (statusblank == 0 && !ISSET(CONST_UPDATE)) {
	    blank_statusbar();
	    wnoutrefresh(bottomwin);
	    reset_cursor();
	    wnoutrefresh(edit);
	}
Chris Allegretta's avatar
Chris Allegretta committed
1875
1876
1877
    }
}

1878
1879
1880
1881
/* Convert buf into a string that can be displayed on screen.  The
 * caller wants to display buf starting with column start_col, and
 * extending for at most len columns.  start_col is zero-based.  len is
 * one-based, so len == 0 means you get "" returned.  The returned
1882
1883
1884
1885
1886
 * string is dynamically allocated, and should be freed.  If dollars is
 * TRUE, the caller might put "$" at the beginning or end of the line if
 * it's too long. */
char *display_string(const char *buf, size_t start_col, size_t len, bool
	dollars)
1887
1888
{
    size_t start_index;
1889
	/* Index in buf of the first character shown. */
1890
    size_t column;
1891
	/* Screen column that start_index corresponds to. */
1892
1893
1894
1895
1896
1897
    size_t alloc_len;
	/* The length of memory allocated for converted. */
    char *converted;
	/* The string we return. */
    size_t index;
	/* Current position in converted. */
1898
    char *buf_mb;
1899
1900
    int buf_mb_len;

1901
1902
1903
1904
1905
    /* If dollars is TRUE, make room for the "$" at the end of the
     * line. */
    if (dollars && len > 0 && strlenpt(buf) > start_col + len)
	len--;

1906
1907
1908
    if (len == 0)
	return mallocstrcpy(NULL, "");

1909
1910
    buf_mb = charalloc(mb_cur_max());

1911
1912
    start_index = actual_x(buf, start_col);
    column = strnlenpt(buf, start_index);
1913

1914
    assert(column <= start_col);
1915

1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
    /* Make sure there's enough room for the initial character, whether
     * it's a multibyte control character, a non-control multibyte
     * character, a tab character, or a null terminator.  Rationale:
     *
     * multibyte control character followed by a null terminator:
     *     1 byte ('^') + mb_cur_max() bytes + 1 byte ('\0')
     * multibyte non-control character followed by a null terminator:
     *     mb_cur_max() bytes + 1 byte ('\0')
     * tab character followed by a null terminator:
     *     mb_cur_max() bytes + (tabsize - 1) bytes + 1 byte ('\0')
     *
     * Since tabsize has a minimum value of 1, it can substitute for 1
     * byte above. */
    alloc_len = (mb_cur_max() + tabsize + 1) * MAX_BUF_SIZE;
    converted = charalloc(alloc_len);
1931

1932
1933
    index = 0;

1934
1935
    if (buf[start_index] != '\0' && buf[start_index] != '\t' &&
	(column < start_col || (dollars && column > 0))) {
1936
1937
	/* We don't display all of buf[start_index] since it starts to
	 * the left of the screen. */
1938
	buf_mb_len = parse_mbchar(buf + start_index, buf_mb, NULL);
1939

1940
	if (is_cntrl_mbchar(buf_mb)) {
1941
	    if (column < start_col) {
1942
1943
		char *ctrl_buf_mb = charalloc(mb_cur_max());
		int ctrl_buf_mb_len, i;
1944

1945
1946
		ctrl_buf_mb = control_mbrep(buf_mb, ctrl_buf_mb,
			&ctrl_buf_mb_len);
1947

1948
1949
		for (i = 0; i < ctrl_buf_mb_len; i++)
		    converted[index++] = ctrl_buf_mb[i];
1950

1951
		start_col += mbwidth(ctrl_buf_mb);
1952

1953
		free(ctrl_buf_mb);
1954

1955
		start_index += buf_mb_len;
1956
	    }
1957
	}
1958
#ifdef ENABLE_UTF8
1959
1960
1961
1962
1963
1964
	else if (using_utf8() && mbwidth(buf_mb) == 2) {
	    if (column >= start_col) {
		converted[index++] = ' ';
		start_col++;
	    }

1965
	    converted[index++] = ' ';
1966
	    start_col++;
1967
1968

	    start_index += buf_mb_len;
1969
	}
1970
#endif
1971
1972
    }

1973
    while (buf[start_index] != '\0') {
1974
	buf_mb_len = parse_mbchar(buf + start_index, buf_mb, NULL);
1975

1976
1977
1978
1979
1980
1981
1982
1983
	/* Make sure there's enough room for the next character, whether
	 * it's a multibyte control character, a non-control multibyte
	 * character, a tab character, or a null terminator. */
	if (index + mb_cur_max() + tabsize + 1 >= alloc_len - 1) {
	    alloc_len += (mb_cur_max() + tabsize + 1) * MAX_BUF_SIZE;
	    converted = charealloc(converted, alloc_len);
	}

1984
	/* If buf contains a tab character, interpret it. */
1985
	if (*buf_mb == '\t') {
1986
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
1987
1988
1989
1990
1991
1992
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i;

		for (i = 0; i < whitespace_len[0]; i++)
		    converted[index++] = whitespace[i];
	    } else
1993
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1994
		converted[index++] = ' ';
1995
	    start_col++;
1996
	    while (start_col % tabsize != 0) {
1997
		converted[index++] = ' ';
1998
1999
		start_col++;
	    }
2000
	/* If buf contains a control character, interpret it.  If buf
2001
	 * contains an invalid multibyte control character, display it
Benno Schulenberg's avatar
Benno Schulenberg committed
2002
	 * as such. */
2003
	} else if (is_cntrl_mbchar(buf_mb)) {
2004
2005
	    char *ctrl_buf_mb = charalloc(mb_cur_max());
	    int ctrl_buf_mb_len, i;
2006

2007
	    converted[index++] = '^';
2008
2009
	    start_col++;

2010
2011
	    ctrl_buf_mb = control_mbrep(buf_mb, ctrl_buf_mb,
		&ctrl_buf_mb_len);
2012

2013
2014
	    for (i = 0; i < ctrl_buf_mb_len; i++)
		converted[index++] = ctrl_buf_mb[i];
2015

2016
	    start_col += mbwidth(ctrl_buf_mb);
2017

2018
	    free(ctrl_buf_mb);
2019
	/* If buf contains a space character, interpret it. */
2020
	} else if (*buf_mb == ' ') {
2021
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
2022
2023
2024
2025
2026
2027
2028
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i;

		for (i = whitespace_len[0]; i < whitespace_len[0] +
			whitespace_len[1]; i++)
		    converted[index++] = whitespace[i];
	    } else
2029
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2030
		converted[index++] = ' ';
2031
	    start_col++;
2032
2033
2034
	/* If buf contains a non-control character, interpret it.  If
	 * buf contains an invalid multibyte non-control character,
	 * display it as such. */
2035
	} else {
2036
2037
	    char *nctrl_buf_mb = charalloc(mb_cur_max());
	    int nctrl_buf_mb_len, i;
2038

2039
2040
	    nctrl_buf_mb = mbrep(buf_mb, nctrl_buf_mb,
		&nctrl_buf_mb_len);
2041

2042
2043
2044
2045
2046
2047
	    for (i = 0; i < nctrl_buf_mb_len; i++)
		converted[index++] = nctrl_buf_mb[i];

	    start_col += mbwidth(nctrl_buf_mb);

	    free(nctrl_buf_mb);
2048
2049
	}

2050
	start_index += buf_mb_len;
2051
2052
    }

2053
2054
    free(buf_mb);

2055
2056
    assert(alloc_len >= index + 1);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2057
    /* Null-terminate converted. */
2058
    converted[index] = '\0';
2059
2060
2061

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

2064
    return converted;
2065
2066
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2067
2068
2069
2070
2071
2072
/* If path is NULL, we're in normal editing mode, so display the current
 * version of nano, the current filename, and whether the current file
 * has been modified on the titlebar.  If path isn't NULL, we're in the
 * file browser, and path contains the directory to start the file
 * browser in, so display the current version of nano and the contents
 * of path on the titlebar. */
2073
void titlebar(const char *path)
Chris Allegretta's avatar
Chris Allegretta committed
2074
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2075
    int space = COLS;
2076
	/* The space we have available for display. */
2077
    size_t verlen = strlenpt(PACKAGE_STRING) + 1;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2078
2079
	/* The length of the version message in columns, plus one for
	 * padding. */
2080
    const char *prefix;
2081
	/* "DIR:", "File:", or "New Buffer".  Goes before filename. */
2082
    size_t prefixlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2083
	/* The length of the prefix in columns, plus one for padding. */
2084
    const char *state;
2085
2086
	/* "Modified", "View", or "".  Shows the state of this
	 * buffer. */
2087
    size_t statelen = 0;
2088
	/* The length of the state in columns, or the length of
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2089
2090
	 * "Modified" if the state is blank and we're not in the file
	 * browser. */
2091
    char *exppath = NULL;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2092
	/* The filename, expanded for display. */
2093
    bool newfie = FALSE;
2094
	/* Do we say "New Buffer"? */
2095
    bool dots = FALSE;
2096
2097
	/* Do we put an ellipsis before the path? */

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

2100
2101
2102
    if (interface_color_pair[TITLE_BAR].bright)
	wattron(topwin, A_BOLD);
    wattron(topwin, interface_color_pair[TITLE_BAR].pairnum);
2103

2104
    blank_titlebar();
Chris Allegretta's avatar
Chris Allegretta committed
2105

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2106
2107
2108
2109
    /* space has to be at least 4: two spaces before the version message,
     * at least one character of the version message, and one space
     * after the version message. */
    if (space < 4)
2110
2111
	space = 0;
    else {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2112
2113
2114
2115
	/* Limit verlen to 1/3 the length of the screen in columns,
	 * minus three columns for spaces. */
	if (verlen > (COLS / 3) - 3)
	    verlen = (COLS / 3) - 3;
2116
    }
Chris Allegretta's avatar
Chris Allegretta committed
2117

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2118
    if (space >= 4) {
2119
2120
	/* Add a space after the version message, and account for both
	 * it and the two spaces before it. */
2121
2122
	mvwaddnstr(topwin, 0, 2, PACKAGE_STRING,
		actual_x(PACKAGE_STRING, verlen));
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2123
2124
2125
2126
	verlen += 3;

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

2129
2130
2131
2132
2133
2134
2135
2136
2137
#ifndef DISABLE_BROWSER
    /* Don't display the state if we're in the file browser. */
    if (path != NULL)
	state = "";
    else
#endif
	state = openfile->modified ? _("Modified") : ISSET(VIEW_MODE) ?
		_("View") : "";

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2141
2142
    /* If possible, add a space before state. */
    if (space > 0 && statelen < space)
2143
	statelen++;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2144
    else
2145
2146
2147
	goto the_end;

#ifndef DISABLE_BROWSER
2148
    /* path should be a directory if we're in the file browser. */
2149
2150
2151
2152
    if (path != NULL)
	prefix = _("DIR:");
    else
#endif
2153
    if (openfile->filename[0] == '\0') {
2154
	prefix = _("New Buffer");
2155
	newfie = TRUE;
2156
2157
    } else
	prefix = _("File:");
2158

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2161
    /* If newfie is FALSE, add a space after prefix. */
2162
    if (!newfie && prefixlen + statelen < space)
2163
2164
	prefixlen++;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2165
    /* If we're not in the file browser, set path to the current
2166
     * filename. */
2167
    if (path == NULL)
2168
	path = openfile->filename;
2169

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2170
    /* Account for the full lengths of the prefix and the state. */
2171
2172
2173
2174
    if (space >= prefixlen + statelen)
	space -= prefixlen + statelen;
    else
	space = 0;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2175
	/* space is now the room we have for the filename. */
2176

2177
    if (!newfie) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2178
	size_t lenpt = strlenpt(path), start_col;
2179

2180
2181
2182
	/* Don't set dots to TRUE if we have fewer than eight columns
	 * (i.e. one column for padding, plus seven columns for a
	 * filename). */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2183
	dots = (space >= 8 && lenpt >= space);
2184
2185
2186
2187
2188
2189
2190
2191

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

	exppath = display_string(path, start_col, space, FALSE);
2192
2193
    }

2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
    /* If dots is TRUE, we will display something like "File:
     * ...ename". */
    if (dots) {
	mvwaddnstr(topwin, 0, verlen - 1, prefix, actual_x(prefix,
		prefixlen));
	if (space <= -3 || newfie)
	    goto the_end;
	waddch(topwin, ' ');
	waddnstr(topwin, "...", space + 3);
	if (space <= 0)
	    goto the_end;
	waddstr(topwin, exppath);
    } else {
2207
2208
2209
	size_t exppathlen = newfie ? 0 : strlenpt(exppath);
	    /* The length of the expanded filename. */

2210
	/* There is room for the whole filename, so we center it. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2211
2212
	mvwaddnstr(topwin, 0, verlen + ((space - exppathlen) / 3),
		prefix, actual_x(prefix, prefixlen));
2213
	if (!newfie) {
2214
2215
2216
2217
2218
2219
2220
2221
	    waddch(topwin, ' ');
	    waddstr(topwin, exppath);
	}
    }

  the_end:
    free(exppath);

2222
    if (state[0] != '\0') {
2223
	if (statelen >= COLS - 1)
2224
2225
	    mvwaddnstr(topwin, 0, 0, state, actual_x(state, COLS));
	else {
2226
	    assert(COLS - statelen - 1 >= 0);
2227

2228
	    mvwaddnstr(topwin, 0, COLS - statelen - 1, state,
2229
		actual_x(state, statelen));
2230
	}
2231
    }
2232

2233
2234
    wattroff(topwin, A_BOLD);
    wattroff(topwin, interface_color_pair[TITLE_BAR].pairnum);
2235

2236
    wnoutrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
2237
    reset_cursor();
2238
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
2239
2240
}

2241
2242
/* Mark the current file as modified if it isn't already, and then
 * update the titlebar to display the file's new status. */
2243
2244
void set_modified(void)
{
2245
2246
    if (!openfile->modified) {
	openfile->modified = TRUE;
2247
	titlebar(NULL);
2248
#ifndef NANO_TINY
2249
	if (ISSET(LOCKING)) {
2250
	    if (openfile->filename[0] == '\0')
2251
2252
		return;
	    else if (openfile->lock_filename == NULL) {
2253
                /* TRANSLATORS: Try to keep this at most 76 characters. */
2254
2255
2256
2257
2258
2259
                statusbar(_("Warning: Modifying a file which is not locked, check directory permission?"));
	    } else {
		write_lockfile(openfile->lock_filename,
                               get_full_path(openfile->filename), TRUE);
            }
	}
2260
#endif
2261
2262
2263
    }
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2264
2265
2266
/* Display a message on the statusbar, and set disable_cursorpos to
 * TRUE, so that the message won't be immediately overwritten if
 * constant cursor position display is on. */
2267
2268
2269
void statusbar(const char *msg, ...)
{
    va_list ap;
2270
    char *bar, *foo;
Benno Schulenberg's avatar
Benno Schulenberg committed
2271
    size_t start_x;
2272
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
2273
2274
    bool old_whitespace;
#endif
2275
2276
2277
2278
2279

    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(). */
2280
    if (isendwin()) {
2281
2282
2283
2284
2285
2286
2287
	vfprintf(stderr, msg, ap);
	va_end(ap);
	return;
    }

    blank_statusbar();

2288
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
2289
2290
    old_whitespace = ISSET(WHITESPACE_DISPLAY);
    UNSET(WHITESPACE_DISPLAY);
2291
#endif
2292
2293
2294
2295
    bar = charalloc(mb_cur_max() * (COLS - 3));
    vsnprintf(bar, mb_cur_max() * (COLS - 3), msg, ap);
    va_end(ap);
    foo = display_string(bar, 0, COLS - 4, FALSE);
Benno Schulenberg's avatar
Benno Schulenberg committed
2296
    free(bar);
2297
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
2298
2299
    if (old_whitespace)
	SET(WHITESPACE_DISPLAY);
2300
#endif
Benno Schulenberg's avatar
Benno Schulenberg committed
2301
    start_x = (COLS - strlenpt(foo) - 4) / 2;
2302

2303
    wmove(bottomwin, 0, start_x);
2304
2305
2306
    if (interface_color_pair[STATUS_BAR].bright)
	wattron(bottomwin, A_BOLD);
    wattron(bottomwin, interface_color_pair[STATUS_BAR].pairnum);
2307
2308
2309
2310
    waddstr(bottomwin, "[ ");
    waddstr(bottomwin, foo);
    free(foo);
    waddstr(bottomwin, " ]");
2311
2312
    wattroff(bottomwin, A_BOLD);
    wattroff(bottomwin, interface_color_pair[STATUS_BAR].pairnum);
2313
2314
2315
2316
2317
    wnoutrefresh(bottomwin);
    reset_cursor();
    wnoutrefresh(edit);
	/* Leave the cursor at its position in the edit window, not in
	 * the statusbar. */
2318

2319
    disable_cursorpos = TRUE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2320
2321
2322

    /* If we're doing quick statusbar blanking, and constant cursor
     * position display is off, blank the statusbar after only one
2323
2324
     * keystroke.  Otherwise, blank it after twenty-six keystrokes, as
     * Pico does. */
2325
    statusblank =
2326
#ifndef NANO_TINY
2327
	ISSET(QUICK_BLANK) && !ISSET(CONST_UPDATE) ? 1 :
2328
#endif
2329
	26;
2330
2331
}

2332
2333
/* Display the shortcut list in s on the last two rows of the bottom
 * portion of the window. */
2334
void bottombars(int menu)
Chris Allegretta's avatar
Chris Allegretta committed
2335
{
2336
    size_t i, colwidth, slen;
2337
2338
    subnfunc *f;
    const sc *s;
2339

Chris Allegretta's avatar
Chris Allegretta committed
2340
2341
2342
    if (ISSET(NO_HELP))
	return;

2343
    if (menu == MMAIN) {
2344
	slen = MAIN_VISIBLE;
2345

2346
	assert(slen <= length_of_list(menu));
2347
    } else {
2348
	slen = length_of_list(menu);
2349

2350
	/* Don't show any more shortcuts than the main list does. */
2351
2352
2353
2354
	if (slen > MAIN_VISIBLE)
	    slen = MAIN_VISIBLE;
    }

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

2361
    blank_bottombars();
2362

2363
2364
2365
#ifdef DEBUG
    fprintf(stderr, "In bottombars, and slen == \"%d\"\n", (int) slen);
#endif
2366

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

2369
2370
2371
2372
2373
#ifdef DEBUG
        fprintf(stderr, "Checking menu items....");
#endif
        if ((f->menus & menu) == 0)
	    continue;
2374

2375
        if (!f->desc || strlen(f->desc) == 0)
2376
2377
	    continue;

2378
#ifdef DEBUG
2379
        fprintf(stderr, "found one! f->menus = %x, desc = \"%s\"\n", f->menus, f->desc);
2380
2381
2382
2383
2384
2385
2386
2387
#endif
        s = first_sc_for(menu, f->scfunc);
        if (s == NULL) {
#ifdef DEBUG
	    fprintf(stderr, "Whoops, guess not, no shortcut key found for func!\n");
#endif
            continue;
        }
2388
	wmove(bottomwin, 1 + i % 2, (i / 2) * colwidth);
2389
2390
2391
#ifdef DEBUG
        fprintf(stderr, "Calling onekey with keystr \"%s\" and desc \"%s\"\n", s->keystr, f->desc);
#endif
2392
	onekey(s->keystr, _(f->desc), colwidth + (COLS % colwidth));
2393
        i++;
Chris Allegretta's avatar
Chris Allegretta committed
2394
    }
2395

2396
2397
    wnoutrefresh(bottomwin);
    reset_cursor();
2398
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
2399
2400
}

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

2410
2411
    assert(keystroke != NULL && desc != NULL);

2412
2413
2414
    if (interface_color_pair[KEY_COMBO].bright)
	wattron(bottomwin, A_BOLD);
    wattron(bottomwin, interface_color_pair[KEY_COMBO].pairnum);
2415
    waddnstr(bottomwin, keystroke, actual_x(keystroke, len));
2416
2417
    wattroff(bottomwin, A_BOLD);
    wattroff(bottomwin, interface_color_pair[KEY_COMBO].pairnum);
2418
2419
2420
2421
2422
2423

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

2424
2425
    if (len > 0) {
	waddch(bottomwin, ' ');
2426
2427
2428
	if (interface_color_pair[FUNCTION_TAG].bright)
	    wattron(bottomwin, A_BOLD);
	wattron(bottomwin, interface_color_pair[FUNCTION_TAG].pairnum);
2429
	waddnstr(bottomwin, desc, actual_x(desc, len));
2430
2431
	wattroff(bottomwin, A_BOLD);
	wattroff(bottomwin, interface_color_pair[FUNCTION_TAG].pairnum);
Chris Allegretta's avatar
Chris Allegretta committed
2432
2433
2434
    }
}

2435
2436
/* Reset current_y, based on the position of current, and put the cursor
 * in the edit window at (current_y, current_x). */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2437
2438
void reset_cursor(void)
{
2439
    size_t xpt;
2440
2441
    /* If we haven't opened any files yet, put the cursor in the top
     * left corner of the edit window and get out. */
2442
    if (openfile == NULL) {
2443
	wmove(edit, 0, 0);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2444
	return;
2445
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2446

2447
    xpt = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2448

2449
#ifndef NANO_TINY
2450
2451
    if (ISSET(SOFTWRAP)) {
	filestruct *tmp;
2452
2453
	openfile->current_y = 0;

2454
	for (tmp = openfile->edittop; tmp && tmp != openfile->current; tmp = tmp->next)
2455
	    openfile->current_y += (strlenpt(tmp->data) / COLS) + 1;
2456

2457
	openfile->current_y += xplustabs() / COLS;
2458
	if (openfile->current_y < editwinrows)
2459
	    wmove(edit, openfile->current_y, xpt % COLS);
2460
2461
2462
    } else
#endif
    {
2463
2464
2465
2466
2467
2468
	openfile->current_y = openfile->current->lineno -
	    openfile->edittop->lineno;

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

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

2494
    assert(openfile != NULL && fileptr != NULL && converted != NULL);
2495
    assert(strlenpt(converted) <= COLS);
2496

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

2501
#ifndef USE_SLANG
2502
2503
2504
    /* 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. */
2505
    wredrawln(edit, line, 1);
2506
#endif
2507

2508
#ifndef DISABLE_COLOR
2509
2510
2511
2512
    /* If color syntaxes are available and turned on, we need to display
     * them. */
    if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX)) {
	const colortype *tmpcolor = openfile->colorstrings;
2513

2514
2515
2516
	/* Set up multi-line color data for this line if it's not yet
	 * calculated. */
	if (fileptr->multidata == NULL && openfile->syntax
2517
		&& openfile->syntax->nmultis > 0) {
2518
	    int i;
2519
2520
2521
2522
	    fileptr->multidata = (short *)nmalloc(openfile->syntax->nmultis * sizeof(short));
	    for (i = 0; i < openfile->syntax->nmultis; i++)
		/* Assume this applies until we know otherwise. */
		fileptr->multidata[i] = -1;
2523
	}
2524
2525
2526
2527
	for (; tmpcolor != NULL; tmpcolor = tmpcolor->next) {
	    int x_start;
		/* Starting column for mvwaddnstr.  Zero-based. */
	    int paintlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2528
2529
		/* Number of chars to paint on this line.  There are
		 * COLS characters on a whole line. */
2530
	    size_t index;
2531
		/* Index in converted where we paint. */
2532
2533
2534
2535
	    regmatch_t startmatch;
		/* Match position for start_regex. */
	    regmatch_t endmatch;
		/* Match position for end_regex. */
2536
2537
2538
2539

	    if (tmpcolor->bright)
		wattron(edit, A_BOLD);
	    wattron(edit, COLOR_PAIR(tmpcolor->pairnum));
2540
2541
2542
	    /* 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. */
2543

2544
	    /* First case, tmpcolor is a single-line expression. */
2545
	    if (tmpcolor->end == NULL) {
2546
2547
2548
		size_t k = 0;

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

		    /* Skip over a zero-length regex match. */
		    if (startmatch.rm_so == startmatch.rm_eo)
2569
			startmatch.rm_eo++;
2570
		    else if (startmatch.rm_so < endpos &&
2571
			startmatch.rm_eo > startpos) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2572
2573
			x_start = (startmatch.rm_so <= startpos) ? 0 :
				strnlenpt(fileptr->data,
2574
				startmatch.rm_so) - start;
2575

2576
2577
2578
			index = actual_x(converted, x_start);

			paintlen = actual_x(converted + index,
2579
2580
				strnlenpt(fileptr->data,
				startmatch.rm_eo) - start - x_start);
2581
2582
2583

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

2584
			mvwaddnstr(edit, line, x_start, converted +
2585
				index, paintlen);
2586
		    }
2587
		    k = startmatch.rm_eo;
Chris Allegretta's avatar
Chris Allegretta committed
2588
		}
2589
	    } else if (fileptr->multidata != NULL && fileptr->multidata[tmpcolor->id] != CNONE) {
2590
		/* This is a multi-line regex.  There are two steps.
2591
2592
2593
2594
2595
2596
		 * First, we have to see if the beginning of the line is
		 * colored by a start on an earlier line, and an end on
		 * this line or later.
		 *
		 * We find the first line before fileptr matching the
		 * start.  If every match on that line is followed by an
2597
2598
2599
2600
		 * end, then go to step two.  Otherwise, find the next
		 * line after start_line matching the end.  If that line
		 * is not before fileptr, then paint the beginning of
		 * this line. */
2601
		const filestruct *start_line = fileptr->prev;
2602
		    /* The first line before fileptr matching start. */
2603
		regoff_t start_col;
2604
		    /* Where it starts in that line. */
2605
		const filestruct *end_line;
2606
2607
2608
		short md = fileptr->multidata[tmpcolor->id];

		if (md == -1)
2609
2610
		    /* Assume this until we know otherwise. */
		    fileptr->multidata[tmpcolor->id] = CNONE;
2611
		else if (md == CNONE)
2612
		    goto end_of_loop;
2613
2614
		else if (md == CWHOLELINE) {
		    mvwaddnstr(edit, line, 0, converted, -1);
2615
		    goto end_of_loop;
2616
2617
2618
2619
2620
		} else if (md == CBEGINBEFORE) {
		    regexec(tmpcolor->end, fileptr->data, 1, &endmatch, 0);
		    paintlen = actual_x(converted, strnlenpt(fileptr->data,
			endmatch.rm_eo) - start);
		    mvwaddnstr(edit, line, 0, converted, paintlen);
2621
		    goto end_of_loop;
2622
		}
2623

2624
		while (start_line != NULL && regexec(tmpcolor->start,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2625
2626
			start_line->data, 1, &startmatch, 0) ==
			REG_NOMATCH) {
2627
2628
		    /* If there is an end on this line, there is no need
		     * to look for starts on earlier lines. */
2629
2630
		    if (regexec(tmpcolor->end, start_line->data, 0,
			NULL, 0) == 0)
2631
2632
2633
			goto step_two;
		    start_line = start_line->prev;
		}
2634
2635
2636

		/* Skip over a zero-length regex match. */
		if (startmatch.rm_so == startmatch.rm_eo)
2637
		    startmatch.rm_eo++;
2638
		else {
2639
2640
		    /* No start found, so skip to the next step. */
		    if (start_line == NULL)
2641
			goto step_two;
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
		    /* Now start_line is the first line before fileptr
		     * containing a start match.  Is there a start on
		     * this line not followed by an end on this line? */
		    start_col = 0;
		    while (TRUE) {
			start_col += startmatch.rm_so;
			startmatch.rm_eo -= startmatch.rm_so;
			if (regexec(tmpcolor->end, start_line->data +
				start_col + startmatch.rm_eo, 0, NULL,
				(start_col + startmatch.rm_eo == 0) ?
				0 : REG_NOTBOL) == REG_NOMATCH)
			    /* No end found after this start. */
			    break;
			start_col++;
			if (regexec(tmpcolor->start, start_line->data +
				start_col, 1, &startmatch,
				REG_NOTBOL) == REG_NOMATCH)
			    /* No later start on this line. */
			    goto step_two;
		    }
		    /* Indeed, there is a start not followed on this
		     * line by an end. */

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

2674
2675
		    /* No end found, or it is too early. */
		    if (end_line == NULL || (end_line == fileptr &&
2676
			endmatch.rm_eo <= startpos))
2677
			goto step_two;
2678

2679
2680
2681
2682
2683
2684
2685
		    /* Now paint the start of fileptr.  If the start of
		     * fileptr is on a different line from the end,
		     * paintlen is -1, meaning that everything on the
		     * line gets painted.  Otherwise, paintlen is the
		     * expanded location of the end of the match minus
		     * the expanded location of the beginning of the
		     * page. */
2686
		    if (end_line != fileptr) {
2687
			paintlen = -1;
2688
2689
			fileptr->multidata[tmpcolor->id] = CWHOLELINE;
		    } else {
2690
2691
2692
			paintlen = actual_x(converted,
				strnlenpt(fileptr->data,
				endmatch.rm_eo) - start);
2693
2694
			fileptr->multidata[tmpcolor->id] = CBEGINBEFORE;
		    }
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
		    mvwaddnstr(edit, line, 0, converted, paintlen);
  step_two:
		    /* Second step, we look for starts on this line. */
		    start_col = 0;

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

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

2716
			index = actual_x(converted, x_start);
2717

2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
			if (regexec(tmpcolor->end, fileptr->data +
				startmatch.rm_eo, 1, &endmatch,
				(startmatch.rm_eo == 0) ? 0 :
				REG_NOTBOL) == 0) {
			    /* Translate the end match to be relative to
			     * the beginning of the line. */
			    endmatch.rm_so += startmatch.rm_eo;
			    endmatch.rm_eo += startmatch.rm_eo;
			    /* There is an end on this line.  But does
			     * it appear on this page, and is the match
			     * more than zero characters long? */
			    if (endmatch.rm_eo > startpos &&
				endmatch.rm_eo > startmatch.rm_so) {
				paintlen = actual_x(converted + index,
					strnlenpt(fileptr->data,
					endmatch.rm_eo) - start -
					x_start);
2735

2736
2737
2738
2739
				assert(0 <= x_start && x_start < COLS);

				mvwaddnstr(edit, line, x_start,
					converted + index, paintlen);
2740
				if (paintlen > 0)
2741
				    fileptr->multidata[tmpcolor->id] = CSTARTENDHERE;
2742

2743
2744
2745
2746
2747
2748
2749
2750
			    }
			} else {
			    /* There is no end on this line.  But we
			     * haven't yet looked for one on later
			     * lines. */
			    end_line = fileptr->next;

			    while (end_line != NULL &&
2751
2752
				regexec(tmpcolor->end, end_line->data,
				0, NULL, 0) == REG_NOMATCH)
2753
				end_line = end_line->next;
2754

2755
2756
			    if (end_line != NULL) {
				assert(0 <= x_start && x_start < COLS);
2757

2758
2759
2760
2761
2762
				mvwaddnstr(edit, line, x_start,
					converted + index, -1);
				/* We painted to the end of the line, so
				 * don't bother checking any more
				 * starts. */
2763
				fileptr->multidata[tmpcolor->id] = CENDAFTER;
2764
2765
				break;
			    }
2766
			}
2767
			start_col = startmatch.rm_so + 1;
2768
		    }
2769
2770
		}
	    }
2771
  end_of_loop:
2772
2773
	    wattroff(edit, A_BOLD);
	    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
2774
	}
2775
    }
2776
#endif /* !DISABLE_COLOR */
2777

2778
#ifndef NANO_TINY
2779
    /* If the mark is on, we need to display it. */
2780
    if (openfile->mark_set && (fileptr->lineno <=
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2781
	openfile->mark_begin->lineno || fileptr->lineno <=
2782
	openfile->current->lineno) && (fileptr->lineno >=
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2783
	openfile->mark_begin->lineno || fileptr->lineno >=
2784
	openfile->current->lineno)) {
2785
	/* fileptr is at least partially selected. */
2786
	const filestruct *top;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2787
	    /* Either current or mark_begin, whichever is first. */
2788
	size_t top_x;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2789
	    /* current_x or mark_begin_x, corresponding to top. */
2790
2791
	const filestruct *bot;
	size_t bot_x;
2792
	int x_start;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2793
	    /* Starting column for mvwaddnstr().  Zero-based. */
2794
	int paintlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2795
2796
	    /* Number of characters to paint on this line.  There are
	     * COLS characters on a whole line. */
2797
	size_t index;
2798
	    /* Index in converted where we paint. */
2799

2800
	mark_order(&top, &top_x, &bot, &bot_x, NULL);
2801
2802
2803
2804
2805

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

2807
	/* The selected bit of fileptr is on this page. */
2808
2809
	if (top_x < endpos && bot_x > startpos) {
	    assert(startpos <= top_x);
2810
2811
2812
2813

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

2815
2816
2817
2818
2819
	    /* If the end of the mark is off the page, paintlen is -1,
	     * meaning that everything on the line gets painted.
	     * Otherwise, paintlen is the expanded location of the end
	     * of the mark minus the expanded location of the beginning
	     * of the mark. */
2820
2821
2822
2823
2824
	    if (bot_x >= endpos)
		paintlen = -1;
	    else
		paintlen = strnlenpt(fileptr->data, bot_x) - (x_start +
			start);
2825
2826
2827
2828
2829
2830
2831
2832

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

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

2836
	    index = actual_x(converted, x_start);
2837

2838
2839
2840
	    if (paintlen > 0)
		paintlen = actual_x(converted + index, paintlen);

2841
	    wattron(edit, hilite_attribute);
2842
	    mvwaddnstr(edit, line, x_start, converted + index,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2843
		paintlen);
2844
	    wattroff(edit, hilite_attribute);
Chris Allegretta's avatar
Chris Allegretta committed
2845
	}
2846
    }
2847
#endif /* !NANO_TINY */
Chris Allegretta's avatar
Chris Allegretta committed
2848
2849
}

2850
/* Just update one line in the edit buffer.  This is basically a wrapper
2851
 * for edit_draw().  The line will be displayed starting with
2852
 * fileptr->data[index].  Likely arguments are current_x or zero.
2853
 * Returns: Number of additional lines consumed (needed for SOFTWRAP). */
2854
int update_line(filestruct *fileptr, size_t index)
Chris Allegretta's avatar
Chris Allegretta committed
2855
{
2856
    int line = 0;
2857
	/* The line in the edit window that we want to update. */
2858
    int extralinesused = 0;
2859
2860
2861
2862
    char *converted;
	/* fileptr->data converted to have tabs and control characters
	 * expanded. */
    size_t page_start;
Chris Allegretta's avatar
Chris Allegretta committed
2863

2864
    assert(fileptr != NULL);
2865

2866
#ifndef NANO_TINY
2867
    if (ISSET(SOFTWRAP)) {
2868
2869
	filestruct *tmp;

2870
2871
	for (tmp = openfile->edittop; tmp && tmp != fileptr; tmp = tmp->next)
	    line += (strlenpt(tmp->data) / COLS) + 1;
2872
    } else
2873
#endif
2874
2875
	line = fileptr->lineno - openfile->edittop->lineno;

2876
    if (line < 0 || line >= editwinrows)
Chris Allegretta's avatar
Chris Allegretta committed
2877
	return 1;
2878

2879
    /* First, blank out the line. */
2880
    blank_line(edit, line, 0, COLS);
2881

2882
2883
    /* Next, convert variables that index the line to their equivalent
     * positions in the expanded line. */
2884
#ifndef NANO_TINY
2885
2886
2887
    if (ISSET(SOFTWRAP))
	index = 0;
    else
2888
#endif
2889
	index = strnlenpt(fileptr->data, index);
2890
    page_start = get_page_start(index);
2891

2892
2893
    /* Expand the line, replacing tabs with spaces, and control
     * characters with their displayed forms. */
2894
2895
2896
#ifdef NANO_TINY
    converted = display_string(fileptr->data, page_start, COLS, TRUE);
#else
2897
2898
2899
    converted = display_string(fileptr->data, page_start, COLS, !ISSET(SOFTWRAP));
#ifdef DEBUG
    if (ISSET(SOFTWRAP) && strlen(converted) >= COLS - 2)
2900
	fprintf(stderr, "update_line(): converted(1) line = %s\n", converted);
2901
#endif
2902
#endif /* !NANO_TINY */
2903

2904
    /* Paint the line. */
2905
    edit_draw(fileptr, converted, line, page_start);
2906
    free(converted);
Chris Allegretta's avatar
Chris Allegretta committed
2907

2908
#ifndef NANO_TINY
2909
    if (!ISSET(SOFTWRAP)) {
2910
#endif
2911
2912
2913
2914
	if (page_start > 0)
	    mvwaddch(edit, line, 0, '$');
	if (strlenpt(fileptr->data) > page_start + COLS)
	    mvwaddch(edit, line, COLS - 1, '$');
2915
#ifndef NANO_TINY
2916
    } else {
2917
	size_t full_length = strlenpt(fileptr->data);
2918
	for (index += COLS; index <= full_length && line < editwinrows; index += COLS) {
2919
2920
	    line++;
#ifdef DEBUG
2921
	    fprintf(stderr, "update_line(): softwrap code, moving to %d index %lu\n", line, (unsigned long)index);
2922
#endif
2923
	    blank_line(edit, line, 0, COLS);
2924
2925

	    /* Expand the line, replacing tabs with spaces, and control
2926
	     * characters with their displayed forms. */
2927
2928
2929
2930
2931
	    converted = display_string(fileptr->data, index, COLS, !ISSET(SOFTWRAP));
#ifdef DEBUG
	    if (ISSET(SOFTWRAP) && strlen(converted) >= COLS - 2)
		fprintf(stderr, "update_line(): converted(2) line = %s\n", converted);
#endif
2932
2933
2934

	    /* Paint the line. */
	    edit_draw(fileptr, converted, line, index);
2935
	    free(converted);
2936
2937
2938
	    extralinesused++;
	}
    }
2939
#endif /* !NANO_TINY */
2940
    return extralinesused;
Chris Allegretta's avatar
Chris Allegretta committed
2941
2942
}

2943
/* Return TRUE if we need an update after moving horizontally, and FALSE
2944
 * otherwise.  We need one if the mark is on or if pww_save and
2945
 * placewewant are on different pages. */
2946
bool need_horizontal_update(size_t pww_save)
2947
2948
{
    return
2949
#ifndef NANO_TINY
2950
	openfile->mark_set ||
2951
#endif
2952
	get_page_start(pww_save) !=
2953
	get_page_start(openfile->placewewant);
2954
2955
}

2956
/* Return TRUE if we need an update after moving vertically, and FALSE
2957
 * otherwise.  We need one if the mark is on or if pww_save and
2958
 * placewewant are on different pages. */
2959
bool need_vertical_update(size_t pww_save)
2960
2961
{
    return
2962
#ifndef NANO_TINY
2963
	openfile->mark_set ||
2964
#endif
2965
	get_page_start(pww_save) !=
2966
	get_page_start(openfile->placewewant);
2967
2968
}

2969
/* When edittop changes, try and figure out how many lines
2970
 * we really have to work with (i.e. set maxrows). */
2971
2972
2973
2974
2975
void compute_maxrows(void)
{
    int n;
    filestruct *foo = openfile->edittop;

2976
2977
2978
2979
2980
    if (!ISSET(SOFTWRAP)) {
	maxrows = editwinrows;
	return;
    }

2981
2982
    maxrows = 0;
    for (n = 0; n < editwinrows && foo; n++) {
2983
	maxrows++;
2984
	n += strlenpt(foo->data) / COLS;
2985
2986
2987
	foo = foo->next;
    }

2988
2989
2990
    if (n < editwinrows)
	maxrows += editwinrows - n;

2991
#ifdef DEBUG
2992
    fprintf(stderr, "compute_maxrows(): maxrows = %d\n", maxrows);
2993
2994
2995
#endif
}

2996
2997
/* Scroll the edit window in the given direction and the given number
 * of lines, and draw new lines on the blank lines left after the
2998
2999
3000
3001
 * scrolling.  direction is the direction to scroll, either UP_DIR or
 * DOWN_DIR, and nlines is the number of lines to scroll.  We change
 * edittop, and assume that current and current_x are up to date.  We
 * also assume that scrollok(edit) is FALSE. */
3002
void edit_scroll(scroll_dir direction, ssize_t nlines)
3003
{
3004
    ssize_t i;
3005
    filestruct *foo;
3006
    bool do_redraw = FALSE;
3007

3008
3009
    /* Don't bother scrolling less than one line. */
    if (nlines < 1)
3010
3011
	return;

3012
    if (need_vertical_update(0))
3013
3014
	do_redraw = TRUE;

3015
3016
3017
    /* Part 1: nlines is the number of lines we're going to scroll the
     * text of the edit window. */

3018
    /* Move the top line of the edit window up or down (depending on the
3019
3020
     * value of direction) nlines lines, or as many lines as we can if
     * there are fewer than nlines lines available. */
3021
    for (i = nlines; i > 0; i--) {
3022
	if (direction == UP_DIR) {
3023
	    if (openfile->edittop == openfile->fileage)
3024
		break;
3025
	    openfile->edittop = openfile->edittop->prev;
3026
	} else {
3027
	    if (openfile->edittop == openfile->filebot)
3028
		break;
3029
	    openfile->edittop = openfile->edittop->next;
3030
	}
3031
3032

#ifndef NANO_TINY
3033
	/* Don't over-scroll on long lines. */
3034
	if (ISSET(SOFTWRAP) && direction == UP_DIR) {
3035
	    ssize_t len = strlenpt(openfile->edittop->data) / COLS;
3036
	    i -= len;
3037
3038
3039
	    if (len > 0)
		do_redraw = TRUE;
	}
3040
#endif
3041
3042
    }

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3046
3047
    /* Don't bother scrolling zero lines or more than the number of
     * lines in the edit window minus one; in both cases, get out, and
3048
     * call edit_refresh() beforehand if we need to. */
3049
    if (nlines == 0 || do_redraw || nlines >= editwinrows) {
3050
	if (do_redraw || nlines >= editwinrows)
3051
	    edit_refresh_needed = TRUE;
3052
3053
	return;
    }
3054
3055
3056

    /* Scroll the text of the edit window up or down nlines lines,
     * depending on the value of direction. */
3057
    scrollok(edit, TRUE);
3058
    wscrl(edit, (direction == UP_DIR) ? -nlines : nlines);
3059
3060
    scrollok(edit, FALSE);

3061
3062
3063
    /* Part 2: nlines is the number of lines in the scrolled region of
     * the edit window that we need to draw. */

3064
3065
    /* If the top or bottom line of the file is now visible in the edit
     * window, we need to draw the entire edit window. */
3066
3067
3068
3069
    if ((direction == UP_DIR && openfile->edittop ==
	openfile->fileage) || (direction == DOWN_DIR &&
	openfile->edittop->lineno + editwinrows - 1 >=
	openfile->filebot->lineno))
3070
	nlines = editwinrows;
3071

3072
3073
3074
3075
3076
3077
    /* If the scrolled region contains only one line, and the line
     * before it is visible in the edit window, we need to draw it too.
     * If the scrolled region contains more than one line, and the lines
     * before and after the scrolled region are visible in the edit
     * window, we need to draw them too. */
    nlines += (nlines == 1) ? 1 : 2;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3078

3079
3080
    if (nlines > editwinrows)
	nlines = editwinrows;
3081
3082
3083

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

3086
3087
    /* If we scrolled down, move down to the line before the scrolled
     * region. */
3088
    if (direction == DOWN_DIR) {
3089
	for (i = editwinrows - nlines; i > 0 && foo != NULL; i--)
3090
3091
3092
	    foo = foo->next;
    }

3093
3094
3095
3096
3097
3098
    /* Draw new lines on any blank lines before or inside the scrolled
     * region.  If we scrolled down and we're on the top line, or if we
     * scrolled up and we're on the bottom line, the line won't be
     * blank, so we don't need to draw it unless the mark is on or we're
     * not on the first page. */
    for (i = nlines; i > 0 && foo != NULL; i--) {
3099
3100
	if ((i == nlines && direction == DOWN_DIR) || (i == 1 &&
		direction == UP_DIR)) {
3101
3102
3103
3104
3105
	    if (do_redraw)
		update_line(foo, (foo == openfile->current) ?
			openfile->current_x : 0);
	} else
	    update_line(foo, (foo == openfile->current) ?
3106
		openfile->current_x : 0);
3107
	foo = foo->next;
3108
    }
3109
    compute_maxrows();
3110
3111
3112
}

/* Update any lines between old_current and current that need to be
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3113
 * updated.  Use this if we've moved without changing any text. */
3114
void edit_redraw(filestruct *old_current, size_t pww_save)
3115
{
3116
    bool do_redraw = need_vertical_update(0) ||
3117
	need_vertical_update(pww_save);
3118
    filestruct *foo = NULL;
3119

3120
3121
    /* If either old_current or current is offscreen, scroll the edit
     * window until it's onscreen and get out. */
3122
3123
    if (old_current->lineno < openfile->edittop->lineno ||
	old_current->lineno >= openfile->edittop->lineno +
3124
	maxrows || openfile->current->lineno <
3125
	openfile->edittop->lineno || openfile->current->lineno >=
3126
	openfile->edittop->lineno + maxrows) {
3127
#ifdef DEBUG
3128
3129
	fprintf(stderr, "edit_redraw(): line %ld was offscreen, oldcurrent = %ld edittop = %ld",
		(long)openfile->current->lineno, (long)old_current->lineno, (long)openfile->edittop->lineno);
3130
#endif
3131

3132
3133
#ifndef NANO_TINY
	/* If the mark is on, update all the lines between old_current
3134
3135
	 * and either the old first line or old last line (depending on
	 * whether we've scrolled up or down) of the edit window. */
3136
	if (openfile->mark_set) {
3137
	    ssize_t old_lineno;
3138
	    filestruct *old_edittop = openfile->edittop;
3139
3140
3141
3142

	    if (old_edittop->lineno < openfile->edittop->lineno)
		old_lineno = old_edittop->lineno;
	    else
3143
		old_lineno = (old_edittop->lineno + maxrows <=
3144
3145
3146
			openfile->filebot->lineno) ?
			old_edittop->lineno + editwinrows :
			openfile->filebot->lineno;
3147
3148
3149

	    foo = old_current;

3150
	    while (foo->lineno != old_lineno) {
3151
3152
		update_line(foo, 0);

3153
		foo = (foo->lineno > old_lineno) ? foo->prev :
3154
3155
3156
3157
3158
			foo->next;
	    }
	}
#endif /* !NANO_TINY */

3159
3160
3161
	/* Put edittop in range of current, get the difference in lines
	 * between the original edittop and the current edittop, and
	 * then restore the original edittop. */
3162
	edit_update(CENTER);
3163

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3164
3165
	/* Update old_current if we're not on the same page as
	 * before. */
3166
3167
3168
	if (do_redraw)
	    update_line(old_current, 0);

3169
#ifndef NANO_TINY
3170
3171
3172
	/* If the mark is on, update all the lines between the old first
	 * line or old last line of the edit window (depending on
	 * whether we've scrolled up or down) and current. */
3173
	if (openfile->mark_set) {
3174
	    while (foo->lineno != openfile->current->lineno) {
3175
3176
		update_line(foo, 0);

3177
		foo = (foo->lineno > openfile->current->lineno) ?
3178
3179
3180
3181
3182
			foo->prev : foo->next;
	    }
	}
#endif /* !NANO_TINY */

3183
3184
3185
	return;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3186
3187
3188
    /* Update old_current and current if we're not on the same page as
     * before.  If the mark is on, update all the lines between
     * old_current and current too. */
3189
    foo = old_current;
3190

3191
    while (foo != openfile->current) {
3192
	if (do_redraw)
3193
	    update_line(foo, 0);
3194

3195
#ifndef NANO_TINY
3196
	if (!openfile->mark_set)
3197
3198
#endif
	    break;
3199

3200
#ifndef NANO_TINY
3201
3202
	foo = (foo->lineno > openfile->current->lineno) ? foo->prev :
		foo->next;
3203
#endif
3204
    }
3205

3206
    if (do_redraw)
3207
	update_line(openfile->current, openfile->current_x);
3208
3209
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3210
3211
/* 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
3212
3213
void edit_refresh(void)
{
3214
    filestruct *foo;
3215
    int nlines;
3216

3217
    /* Figure out what maxrows should really be. */
3218
    compute_maxrows();
3219

3220
3221
    if (openfile->current->lineno < openfile->edittop->lineno ||
	openfile->current->lineno >= openfile->edittop->lineno +
3222
3223
	maxrows) {
#ifdef DEBUG
3224
3225
	fprintf(stderr, "edit_refresh(): line = %ld, edittop %ld + maxrows %d\n",
		(long)openfile->current->lineno, (long)openfile->edittop->lineno, maxrows);
3226
3227
#endif

3228
3229
	/* Put the top line of the edit window in range of the current
	 * line. */
3230
	edit_update(CENTER);
3231
    }
Chris Allegretta's avatar
Chris Allegretta committed
3232

3233
3234
    foo = openfile->edittop;

3235
#ifdef DEBUG
3236
    fprintf(stderr, "edit_refresh(): edittop->lineno = %ld\n", (long)openfile->edittop->lineno);
3237
#endif
3238

3239
    for (nlines = 0; nlines < editwinrows && foo != NULL; nlines++) {
3240
	nlines += update_line(foo, (foo == openfile->current) ?
3241
		openfile->current_x : 0);
3242
3243
3244
	foo = foo->next;
    }

3245
    for (; nlines < editwinrows; nlines++)
3246
3247
3248
	blank_line(edit, nlines, 0, COLS);

    reset_cursor();
3249
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3250
3251
}

3252
3253
3254
3255
/* Move edittop to put it in range of current, keeping current in the
 * same place.  location determines how we move it: if it's CENTER, we
 * center current, and if it's NONE, we put current current_y lines
 * below edittop. */
3256
void edit_update(update_type location)
Chris Allegretta's avatar
Chris Allegretta committed
3257
{
3258
    filestruct *foo = openfile->current;
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
    int goal;

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

3274
	/* Limit goal to (editwinrows - 1) lines maximum. */
3275
3276
	if (goal > editwinrows - 1)
	    goal = editwinrows - 1;
Chris Allegretta's avatar
Chris Allegretta committed
3277
    }
3278

3279
    for (; goal > 0 && foo->prev != NULL; goal--) {
3280
	foo = foo->prev;
3281
#ifndef NANO_TINY
3282
3283
	if (ISSET(SOFTWRAP) && foo)
	    goal -= strlenpt(foo->data) / COLS;
3284
#endif
3285
    }
3286
    openfile->edittop = foo;
3287
#ifdef DEBUG
3288
    fprintf(stderr, "edit_update(): setting edittop to lineno %ld\n", (long)openfile->edittop->lineno);
3289
#endif
3290
    compute_maxrows();
3291
    edit_refresh_needed = TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
3292
3293
}

3294
/* Unconditionally redraw the entire screen. */
3295
void total_redraw(void)
3296
{
3297
3298
3299
3300
3301
3302
#ifdef USE_SLANG
    /* Slang curses emulation brain damage, part 4: Slang doesn't define
     * curscr. */
    SLsmg_touch_screen();
    SLsmg_refresh();
#else
3303
    wrefresh(curscr);
3304
#endif
3305
3306
}

3307
3308
/* Unconditionally redraw the entire screen, and then refresh it using
 * the current file. */
3309
3310
void total_refresh(void)
{
3311
    total_redraw();
3312
    titlebar(NULL);
3313
    edit_refresh();
3314
    bottombars(currmenu);
3315
3316
}

3317
3318
/* Display the main shortcut list on the last two rows of the bottom
 * portion of the window. */
3319
3320
void display_main_list(void)
{
3321
#ifndef DISABLE_COLOR
3322
3323
3324
3325
3326
3327
    if (openfile->syntax && openfile->syntax->linter)
	set_lint_shortcuts();
    else
	set_spell_shortcuts();
#endif

3328
    bottombars(MMAIN);
3329
3330
}

3331
3332
3333
3334
3335
3336
/* If constant is TRUE, we display the current cursor position only if
 * disable_cursorpos is FALSE.  Otherwise, we display it
 * unconditionally and set disable_cursorpos to FALSE.  If constant is
 * TRUE and disable_cursorpos is TRUE, we also set disable_cursorpos to
 * FALSE, so that we leave the current statusbar alone this time, and
 * display the current cursor position next time. */
3337
void do_cursorpos(bool constant)
Chris Allegretta's avatar
Chris Allegretta committed
3338
{
3339
    filestruct *f;
3340
    char c;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3341
    size_t i, cur_xpt = xplustabs() + 1;
3342
    size_t cur_lenpt = strlenpt(openfile->current->data) + 1;
3343
    int linepct, colpct, charpct;
Chris Allegretta's avatar
Chris Allegretta committed
3344

3345
    assert(openfile->fileage != NULL && openfile->current != NULL);
3346

3347
    f = openfile->current->next;
3348
    c = openfile->current->data[openfile->current_x];
3349
3350

    openfile->current->next = NULL;
3351
    openfile->current->data[openfile->current_x] = '\0';
3352
3353
3354

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

3355
    openfile->current->data[openfile->current_x] = c;
3356
    openfile->current->next = f;
3357

3358
3359
    if (constant && disable_cursorpos) {
	disable_cursorpos = FALSE;
3360
	return;
3361
    }
Chris Allegretta's avatar
Chris Allegretta committed
3362

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3363
    /* Display the current cursor position on the statusbar, and set
3364
     * disable_cursorpos to FALSE. */
3365
3366
    linepct = 100 * openfile->current->lineno /
	openfile->filebot->lineno;
3367
    colpct = 100 * cur_xpt / cur_lenpt;
3368
3369
    charpct = (openfile->totsize == 0) ? 0 : 100 * i /
	openfile->totsize;
3370
3371

    statusbar(
3372
	_("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%lu (%d%%)"),
3373
	(long)openfile->current->lineno,
3374
	(long)openfile->filebot->lineno, linepct,
3375
	(unsigned long)cur_xpt, (unsigned long)cur_lenpt, colpct,
3376
	(unsigned long)i, (unsigned long)openfile->totsize, charpct);
3377

3378
    disable_cursorpos = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
3379
3380
}

3381
/* Unconditionally display the current cursor position. */
3382
void do_cursorpos_void(void)
3383
{
3384
    do_cursorpos(FALSE);
3385
3386
}

3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
void enable_nodelay(void)
{
   nodelay_mode = TRUE;
   nodelay(edit, TRUE);
}

void disable_nodelay(void)
{
   nodelay_mode = FALSE;
   nodelay(edit, FALSE);
}

3399
3400
/* Highlight the current word being replaced or spell checked.  We
 * expect word to have tabs and control characters expanded. */
3401
void do_replace_highlight(bool highlight, const char *word)
Chris Allegretta's avatar
Chris Allegretta committed
3402
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3403
    size_t y = xplustabs(), word_len = strlenpt(word);
Chris Allegretta's avatar
Chris Allegretta committed
3404

3405
    y = get_page_start(y) + COLS - y;
3406
	/* Now y is the number of columns that we can display on this
3407
	 * line. */
Chris Allegretta's avatar
Chris Allegretta committed
3408

3409
3410
3411
3412
3413
    assert(y > 0);

    if (word_len > y)
	y--;

Chris Allegretta's avatar
Chris Allegretta committed
3414
    reset_cursor();
3415
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3416

3417
    if (highlight)
3418
	wattron(edit, hilite_attribute);
Chris Allegretta's avatar
Chris Allegretta committed
3419

3420
    /* This is so we can show zero-length matches. */
3421
    if (word_len == 0)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3422
	waddch(edit, ' ');
3423
    else
3424
	waddnstr(edit, word, actual_x(word, y));
3425
3426
3427

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

3429
    if (highlight)
3430
	wattroff(edit, hilite_attribute);
Chris Allegretta's avatar
Chris Allegretta committed
3431
3432
}

3433
#ifndef DISABLE_EXTRA
3434
#define CREDIT_LEN 57
3435
3436
#define XLCREDIT_LEN 8

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3437
3438
/* Easter egg: Display credits.  Assume nodelay(edit) and scrollok(edit)
 * are FALSE. */
3439
3440
void do_credits(void)
{
3441
3442
    bool old_more_space = ISSET(MORE_SPACE);
    bool old_no_help = ISSET(NO_HELP);
3443
    int kbinput = ERR, crpos = 0, xlpos = 0;
3444
3445
3446
    const char *credits[CREDIT_LEN] = {
	NULL,				/* "The nano text editor" */
	NULL,				/* "version" */
Chris Allegretta's avatar
Chris Allegretta committed
3447
3448
	VERSION,
	"",
3449
	NULL,				/* "Brought to you by:" */
Chris Allegretta's avatar
Chris Allegretta committed
3450
3451
3452
3453
3454
	"Chris Allegretta",
	"Jordi Mallach",
	"Adam Rogoyski",
	"Rob Siemborski",
	"Rocco Corsi",
3455
	"David Lawrence Ramsey",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3456
	"David Benbennick",
3457
	"Mike Frysinger",
3458
	"Benno Schulenberg",
Chris Allegretta's avatar
Chris Allegretta committed
3459
3460
	"Ken Tyler",
	"Sven Guckes",
3461
	NULL,				/* credits[16], handled below. */
Chris Allegretta's avatar
Chris Allegretta committed
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
	"Pauli Virtanen",
	"Daniele Medri",
	"Clement Laforet",
	"Tedi Heriyanto",
	"Bill Soudan",
	"Christian Weisgerber",
	"Erik Andersen",
	"Big Gaute",
	"Joshua Jensen",
	"Ryan Krebs",
	"Albert Chin",
	"",
3474
	NULL,				/* "Special thanks to:" */
3475
	"Monique, Brielle & Joseph",
Chris Allegretta's avatar
Chris Allegretta committed
3476
3477
3478
3479
3480
3481
	"Plattsburgh State University",
	"Benet Laboratories",
	"Amy Allegretta",
	"Linda Young",
	"Jeremy Robichaud",
	"Richard Kolb II",
3482
	NULL,				/* "The Free Software Foundation" */
Chris Allegretta's avatar
Chris Allegretta committed
3483
	"Linus Torvalds",
3484
	NULL,				/* "For ncurses:" */
3485
3486
3487
3488
	"Thomas Dickey",
	"Pavel Curtis",
	"Zeyd Ben-Halim",
	"Eric S. Raymond",
3489
3490
3491
3492
3493
3494
	NULL,				/* "and anyone else we forgot..." */
	NULL,				/* "Thank you for using nano!" */
	"",
	"",
	"",
	"",
3495
	"(C) 1999 - 2014",
3496
	"Free Software Foundation, Inc.",
3497
3498
3499
3500
	"",
	"",
	"",
	"",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3501
	"http://www.nano-editor.org/"
3502
3503
    };

3504
    const char *xlcredits[XLCREDIT_LEN] = {
3505
3506
3507
3508
3509
3510
3511
3512
	N_("The nano text editor"),
	N_("version"),
	N_("Brought to you by:"),
	N_("Special thanks to:"),
	N_("The Free Software Foundation"),
	N_("For ncurses:"),
	N_("and anyone else we forgot..."),
	N_("Thank you for using nano!")
3513
    };
3514

3515
    /* credits[16]: Make sure this name is displayed properly, since we
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3516
3517
     * can't dynamically assign it above, using Unicode 00F6 (Latin
     * Small Letter O with Diaresis) if applicable. */
3518
    credits[16] =
3519
#ifdef ENABLE_UTF8
3520
	using_utf8() ? "Florian K\xC3\xB6nig" :
3521
3522
3523
#endif
	"Florian K\xF6nig";

3524
3525
3526
3527
3528
3529
    if (!old_more_space || !old_no_help) {
	SET(MORE_SPACE);
	SET(NO_HELP);
	window_init();
    }

3530
3531
    curs_set(0);
    nodelay(edit, TRUE);
3532

3533
    blank_titlebar();
3534
    blank_topbar();
Chris Allegretta's avatar
Chris Allegretta committed
3535
    blank_edit();
3536
3537
    blank_statusbar();
    blank_bottombars();
3538

3539
    wrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
3540
    wrefresh(edit);
3541
    wrefresh(bottomwin);
3542
    napms(700);
3543

3544
    for (crpos = 0; crpos < CREDIT_LEN + editwinrows / 2; crpos++) {
3545
	if ((kbinput = wgetch(edit)) != ERR)
3546
	    break;
3547

3548
	if (crpos < CREDIT_LEN) {
3549
	    const char *what;
3550
3551
	    size_t start_x;

3552
	    if (credits[crpos] == NULL) {
3553
		assert(0 <= xlpos && xlpos < XLCREDIT_LEN);
3554

3555
		what = _(xlcredits[xlpos]);
3556
		xlpos++;
3557
	    } else
3558
		what = credits[crpos];
3559

3560
	    start_x = COLS / 2 - strlenpt(what) / 2 - 1;
3561
3562
	    mvwaddstr(edit, editwinrows - 1 - (editwinrows % 2),
		start_x, what);
3563
	}
3564

3565
3566
3567
3568
	wrefresh(edit);

	if ((kbinput = wgetch(edit)) != ERR)
	    break;
3569
	napms(700);
3570

3571
	scrollok(edit, TRUE);
3572
	wscrl(edit, 1);
3573
	scrollok(edit, FALSE);
3574
	wrefresh(edit);
3575

3576
	if ((kbinput = wgetch(edit)) != ERR)
3577
	    break;
3578
	napms(700);
3579

3580
	scrollok(edit, TRUE);
3581
	wscrl(edit, 1);
3582
	scrollok(edit, FALSE);
3583
	wrefresh(edit);
3584
3585
    }

3586
3587
3588
    if (kbinput != ERR)
	ungetch(kbinput);

3589
3590
3591
3592
3593
3594
    if (!old_more_space || !old_no_help) {
	UNSET(MORE_SPACE);
	UNSET(NO_HELP);
	window_init();
    }

3595
    curs_set(1);
3596
    nodelay(edit, FALSE);
3597

3598
    total_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
3599
}
3600
#endif /* !DISABLE_EXTRA */