winio.c 105 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
/**************************************************************************
 *   winio.c                                                              *
 *                                                                        *
5
6
 *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,  *
 *   2008, 2009 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    if (nodelay_mode) {
	if ((input =  wgetch(win)) == ERR)
           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
    while ((kbinput = parse_kbinput(win, meta_key, func_key)) == ERR);
315

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

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

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

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

344
345
346
347
348
349
350
351
352
353
354
    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. */
355
356
		case 3:
		    /* Three escapes: wait for more input. */
357
358
		    break;
		default:
359
360
361
		    /* More than three escapes: limit the escape counter
		     * to no more than two, and wait for more input. */
		    escapes %= 3;
362
363
364
365
366
	    }
	    break;
	default:
	    switch (escapes) {
		case 0:
367
368
369
		    /* One non-escape: normal input mode.  Save the
		     * non-escape character as the result. */
		    retval = *kbinput;
370
371
		    break;
		case 1:
372
		    /* Reset the escape counter. */
373
		    escapes = 0;
374
		    if (get_key_buffer_len() == 0) {
375
			/* One escape followed by a non-escape, and
376
377
378
379
			 * 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. */
380
381
			*meta_key = TRUE;
			retval = tolower(*kbinput);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
382
		    } else
383
			/* One escape followed by a non-escape, and
384
385
386
			 * there are other keystrokes waiting: escape
			 * sequence mode.  Interpret the escape
			 * sequence. */
387
388
			retval = parse_escape_seq_kbinput(win,
				*kbinput);
389
390
		    break;
		case 2:
391
392
393
394
395
396
		    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
397
398
399
400
401
402
403
404
405
			     * 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. */
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
			    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. */
441
			    escapes = 0;
442
443
444
445
446
447
			    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
448
449
450
451
452
				 * other keystrokes waiting: control
				 * character sequence mode.  Interpret
				 * the control sequence and save the
				 * corresponding control character as
				 * the result. */
453
454
455
456
457
458
459
460
461
				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;
			    }
462
			}
463
		    } else {
464
			/* Two escapes followed by a non-escape, and
465
466
467
468
			 * 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. */
469
			escapes = 0;
470
			*meta_key = TRUE;
471
472
			retval = parse_escape_seq_kbinput(win,
				*kbinput);
473
		    }
474
		    break;
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
		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));
493
		    break;
494
495
	    }
    }
496

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

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

#ifdef DEBUG
644
    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);
645
646
#endif

647
648
    free(kbinput);

649
    /* Return the result. */
650
651
652
    return retval;
}

653
/* Translate escape sequences, most of which correspond to extended
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
654
 * keypad values, into their corresponding key values.  These sequences
655
656
657
 * 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)
658
{
659
    int retval = ERR;
David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
660

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

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

1213
    return retval;
1214
1215
}

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

1235
/* Interpret the escape sequence in the keystroke buffer, the first
1236
1237
1238
 * 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)
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
{
    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);
1249
    retval = get_escape_seq_kbinput(seq, seq_len);
1250
1251
1252

    free(seq);

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

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

    return retval;
}

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

1275
1276
    /* Increment the byte digit counter. */
    byte_digits++;
1277

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

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

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

    return retval;
}

1339
#ifdef ENABLE_UTF8
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
/* 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;
}

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

1367
    /* Increment the Unicode digit counter. */
1368
    uni_digits++;
1369

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

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

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

1438
1439
    return retval;
}
1440
#endif /* ENABLE_UTF8 */
1441

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

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

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

1473
1474
    return retval;
}
1475

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

1483
1484
1485
1486
    if (output_len == 0)
	return;

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

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

1491
    unget_input(input, output_len);
1492

1493
    free(input);
1494
1495
}

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

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

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

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

1520
    return retval;
1521
1522
}

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

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

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

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

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

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

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

		uni = get_unicode_kbinput(*kbinput);
	    }
1562

1563
1564
1565
	    /* Put back the multibyte equivalent of the Unicode
	     * value. */
	    uni_mb = make_mbchar(uni, &uni_mb_len);
1566

1567
	    seq = (int *)nmalloc(uni_mb_len * sizeof(int));
1568

1569
1570
	    for (i = 0; i < uni_mb_len; i++)
		seq[i] = (unsigned char)uni_mb[i];
1571

1572
	    unget_input(seq, uni_mb_len);
1573

1574
1575
	    free(seq);
	    free(uni_mb);
1576
	}
1577
    } else
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1578
1579
#endif /* ENABLE_UTF8 */

1580
1581
	/* Put back the first keystroke. */
	unget_input(kbinput, 1);
1582

1583
1584
    free(kbinput);

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

    return retval;
}

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

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

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

1621
1622
1623
    /* Save the screen coordinates where the mouse event took place. */
    *mouse_x = mevent.x;
    *mouse_y = mevent.y;
1624

1625
1626
    in_bottomwin = wenclose(bottomwin, *mouse_y, *mouse_x);

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

1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
	    /* 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;
	    }
1659
1660
1661
1662
1663

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

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

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

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

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

1690
	    /* Adjust j if we released on the last two shortcuts. */
1691
1692
	    if ((j >= currslen) && (*mouse_x % i < COLS % i))
		j -= 2;
1693
1694
1695
#ifdef DEBUG
	    fprintf(stderr, "Calculated %i as index in shortcut list, currmenu = %x.\n", j, currmenu);
#endif
1696
1697
	    /* Ignore releases/clicks of the first mouse button beyond
	     * the last shortcut. */
1698
	    if (j >= currslen)
1699
		return 2;
1700

1701
1702
	    /* Go through the list of functions to determine which
	     * shortcut in the current menu we released/clicked on. */
1703
	    f = allfuncs;
1704

1705
1706
	    while (TRUE) {
		while ((f->menus & currmenu) == 0
1707
#ifndef DISABLE_HELP
1708
			|| strlen(f->help) == 0
1709
#endif
1710
1711
1712
1713
1714
1715
			)
		    f = f->next;
		if (j == 0)
		    break;
		f = f->next;
		j -= 1;
1716
	    }
1717
#ifdef DEBUG
1718
	    fprintf(stderr, "Stopped on func %ld present in menus %x\n", (long)f->scfunc, f->menus);
1719
#endif
1720

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

1739
1740
1741
1742
	if (in_bottomwin)
	    /* Translate the mouse event coordinates so that they're
	     * relative to bottomwin. */
	    wmouse_trafo(bottomwin, mouse_y, mouse_x, FALSE);
1743

1744
	if (in_edit || (in_bottomwin && *mouse_y == 0)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1745
1746
	    int i;

1747
1748
1749
1750
1751
	    /* 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) ?
1752
1753
			 sc_seq_or(do_up_void, 0) : sc_seq_or(do_down_void, 0),
			FALSE, FALSE);
1754
1755
1756
1757
1758
1759
1760

	    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;
1761
1762
    }
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1763
1764
1765

    /* Ignore all other mouse events. */
    return 2;
1766
}
1767
1768
#endif /* !DISABLE_MOUSE */

1769
1770
1771
1772
1773
/* 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)
1774
{
1775
    sc *s;
1776

1777
#ifdef DEBUG
1778
    fprintf(stderr, "get_shortcut(): kbinput = %d, meta_key = %s -- ", *kbinput, *meta_key ? "TRUE" : "FALSE");
1779
1780
#endif

1781
    /* Check for shortcuts. */
1782
1783
1784
1785
1786
    for (s = sclist; s != NULL; s = s->next) {
        if ((menu & s->menu)
		&& ((s->type == META && *meta_key == TRUE && *kbinput == s->seq)
		|| (s->type != META && *kbinput == s->seq))) {
#ifdef DEBUG
1787
1788
	    fprintf (stderr, "matched seq \"%s\", and btw meta was %d (menu is %x from %x)\n",
			     s->keystr, *meta_key, menu, s->menu);
1789
#endif
1790
	    return s;
1791
1792
	}
    }
1793
#ifdef DEBUG
1794
    fprintf (stderr, "matched nothing, btw meta was %d\n", *meta_key);
1795
#endif
1796
1797
1798
1799

    return NULL;
}

1800
/* Try to get a function back from a window.  Just a wrapper. */
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
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;

1811
    s = get_shortcut(currmenu, &kbinput, &meta_key);
1812
1813
1814
    if (!s)
	return NULL;

1815
    return sctofunc((sc *) s);
1816
1817
}

1818
1819
1820
1821
1822
/* 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
1823

1824
1825
1826
1827
    for (; n > 0; n--)
	waddch(win, ' ');
}

1828
/* Blank the first line of the top portion of the window. */
1829
void blank_titlebar(void)
Chris Allegretta's avatar
Chris Allegretta committed
1830
{
1831
    blank_line(topwin, 0, 0, COLS);
1832
1833
}

1834
1835
/* If the MORE_SPACE flag isn't set, blank the second line of the top
 * portion of the window. */
1836
1837
1838
void blank_topbar(void)
{
    if (!ISSET(MORE_SPACE))
1839
	blank_line(topwin, 1, 0, COLS);
1840
1841
}

1842
/* Blank all the lines of the middle portion of the window, i.e. the
1843
 * edit window. */
Chris Allegretta's avatar
Chris Allegretta committed
1844
1845
void blank_edit(void)
{
1846
    int i;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1847

1848
    for (i = 0; i < editwinrows; i++)
1849
	blank_line(edit, i, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1850
1851
}

1852
/* Blank the first line of the bottom portion of the window. */
Chris Allegretta's avatar
Chris Allegretta committed
1853
1854
void blank_statusbar(void)
{
1855
    blank_line(bottomwin, 0, 0, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
1856
1857
}

1858
1859
/* If the NO_HELP flag isn't set, blank the last two lines of the bottom
 * portion of the window. */
1860
1861
1862
void blank_bottombars(void)
{
    if (!ISSET(NO_HELP)) {
1863
1864
	blank_line(bottomwin, 1, 0, COLS);
	blank_line(bottomwin, 2, 0, COLS);
1865
1866
1867
    }
}

1868
1869
1870
/* 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. */
1871
void check_statusblank(void)
Chris Allegretta's avatar
Chris Allegretta committed
1872
{
1873
    if (statusblank > 0) {
1874
	statusblank--;
1875

1876
1877
1878
1879
1880
1881
	if (statusblank == 0 && !ISSET(CONST_UPDATE)) {
	    blank_statusbar();
	    wnoutrefresh(bottomwin);
	    reset_cursor();
	    wnoutrefresh(edit);
	}
Chris Allegretta's avatar
Chris Allegretta committed
1882
1883
1884
    }
}

1885
1886
1887
1888
/* 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
1889
1890
1891
1892
1893
 * 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)
1894
1895
{
    size_t start_index;
1896
	/* Index in buf of the first character shown. */
1897
    size_t column;
1898
	/* Screen column that start_index corresponds to. */
1899
1900
1901
1902
1903
1904
    size_t alloc_len;
	/* The length of memory allocated for converted. */
    char *converted;
	/* The string we return. */
    size_t index;
	/* Current position in converted. */
1905
    char *buf_mb;
1906
1907
    int buf_mb_len;

1908
1909
1910
1911
1912
    /* If dollars is TRUE, make room for the "$" at the end of the
     * line. */
    if (dollars && len > 0 && strlenpt(buf) > start_col + len)
	len--;

1913
1914
1915
    if (len == 0)
	return mallocstrcpy(NULL, "");

1916
1917
    buf_mb = charalloc(mb_cur_max());

1918
1919
    start_index = actual_x(buf, start_col);
    column = strnlenpt(buf, start_index);
1920

1921
    assert(column <= start_col);
1922

1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
    /* 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);
1938

1939
1940
    index = 0;

1941
1942
    if (buf[start_index] != '\0' && buf[start_index] != '\t' &&
	(column < start_col || (dollars && column > 0))) {
1943
1944
	/* We don't display all of buf[start_index] since it starts to
	 * the left of the screen. */
1945
	buf_mb_len = parse_mbchar(buf + start_index, buf_mb, NULL);
1946

1947
	if (is_cntrl_mbchar(buf_mb)) {
1948
	    if (column < start_col) {
1949
1950
		char *ctrl_buf_mb = charalloc(mb_cur_max());
		int ctrl_buf_mb_len, i;
1951

1952
1953
		ctrl_buf_mb = control_mbrep(buf_mb, ctrl_buf_mb,
			&ctrl_buf_mb_len);
1954

1955
1956
		for (i = 0; i < ctrl_buf_mb_len; i++)
		    converted[index++] = ctrl_buf_mb[i];
1957

1958
		start_col += mbwidth(ctrl_buf_mb);
1959

1960
		free(ctrl_buf_mb);
1961

1962
		start_index += buf_mb_len;
1963
	    }
1964
	}
1965
#ifdef ENABLE_UTF8
1966
1967
1968
1969
1970
1971
	else if (using_utf8() && mbwidth(buf_mb) == 2) {
	    if (column >= start_col) {
		converted[index++] = ' ';
		start_col++;
	    }

1972
	    converted[index++] = ' ';
1973
	    start_col++;
1974
1975

	    start_index += buf_mb_len;
1976
	}
1977
#endif
1978
1979
    }

1980
    while (buf[start_index] != '\0') {
1981
	buf_mb_len = parse_mbchar(buf + start_index, buf_mb, NULL);
1982

1983
1984
1985
1986
1987
1988
1989
1990
	/* 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);
	}

1991
	/* If buf contains a tab character, interpret it. */
1992
	if (*buf_mb == '\t') {
1993
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
1994
1995
1996
1997
1998
1999
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i;

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

2014
	    converted[index++] = '^';
2015
2016
	    start_col++;

2017
2018
	    ctrl_buf_mb = control_mbrep(buf_mb, ctrl_buf_mb,
		&ctrl_buf_mb_len);
2019

2020
2021
	    for (i = 0; i < ctrl_buf_mb_len; i++)
		converted[index++] = ctrl_buf_mb[i];
2022

2023
	    start_col += mbwidth(ctrl_buf_mb);
2024

2025
	    free(ctrl_buf_mb);
2026
	/* If buf contains a space character, interpret it. */
2027
	} else if (*buf_mb == ' ') {
2028
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
2029
2030
2031
2032
2033
2034
2035
	    if (ISSET(WHITESPACE_DISPLAY)) {
		int i;

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

2046
2047
	    nctrl_buf_mb = mbrep(buf_mb, nctrl_buf_mb,
		&nctrl_buf_mb_len);
2048

2049
2050
2051
2052
2053
2054
	    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);
2055
2056
	}

2057
	start_index += buf_mb_len;
2058
2059
    }

2060
2061
    free(buf_mb);

2062
2063
    assert(alloc_len >= index + 1);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2064
    /* Null-terminate converted. */
2065
    converted[index] = '\0';
2066
2067
2068

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

2071
    return converted;
2072
2073
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2074
2075
2076
2077
2078
2079
/* 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. */
2080
void titlebar(const char *path)
Chris Allegretta's avatar
Chris Allegretta committed
2081
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2082
    int space = COLS;
2083
	/* The space we have available for display. */
2084
    size_t verlen = strlenpt(PACKAGE_STRING) + 1;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2085
2086
	/* The length of the version message in columns, plus one for
	 * padding. */
2087
    const char *prefix;
2088
	/* "DIR:", "File:", or "New Buffer".  Goes before filename. */
2089
    size_t prefixlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2090
	/* The length of the prefix in columns, plus one for padding. */
2091
    const char *state;
2092
2093
	/* "Modified", "View", or "".  Shows the state of this
	 * buffer. */
2094
    size_t statelen = 0;
2095
	/* The length of the state in columns, or the length of
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2096
2097
	 * "Modified" if the state is blank and we're not in the file
	 * browser. */
2098
    char *exppath = NULL;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2099
	/* The filename, expanded for display. */
2100
    bool newfie = FALSE;
2101
	/* Do we say "New Buffer"? */
2102
    bool dots = FALSE;
2103
2104
	/* Do we put an ellipsis before the path? */

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

2107
    wattron(topwin, reverse_attr);
2108

2109
    blank_titlebar();
Chris Allegretta's avatar
Chris Allegretta committed
2110

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2111
2112
2113
2114
    /* 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)
2115
2116
	space = 0;
    else {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2117
2118
2119
2120
	/* 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;
2121
    }
Chris Allegretta's avatar
Chris Allegretta committed
2122

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

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

2134
2135
2136
2137
2138
2139
2140
2141
2142
#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
2143
    statelen = strlenpt((*state == '\0' && path == NULL) ?
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2144
	_("Modified") : state);
2145

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2146
2147
    /* If possible, add a space before state. */
    if (space > 0 && statelen < space)
2148
	statelen++;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2149
    else
2150
2151
2152
	goto the_end;

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

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2166
    /* If newfie is FALSE, add a space after prefix. */
2167
    if (!newfie && prefixlen + statelen < space)
2168
2169
	prefixlen++;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2170
    /* If we're not in the file browser, set path to the current
2171
     * filename. */
2172
    if (path == NULL)
2173
	path = openfile->filename;
2174

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

2182
    if (!newfie) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2183
	size_t lenpt = strlenpt(path), start_col;
2184

2185
2186
2187
	/* 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
2188
	dots = (space >= 8 && lenpt >= space);
2189
2190
2191
2192
2193
2194
2195
2196

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

	exppath = display_string(path, start_col, space, FALSE);
2197
2198
    }

2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
    /* 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 {
2212
2213
2214
	size_t exppathlen = newfie ? 0 : strlenpt(exppath);
	    /* The length of the expanded filename. */

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

  the_end:
    free(exppath);

2227
    if (state[0] != '\0') {
2228
	if (statelen >= COLS - 1)
2229
2230
	    mvwaddnstr(topwin, 0, 0, state, actual_x(state, COLS));
	else {
2231
	    assert(COLS - statelen - 1 >= 0);
2232

2233
	    mvwaddnstr(topwin, 0, COLS - statelen - 1, state,
2234
		actual_x(state, statelen));
2235
	}
2236
    }
2237

2238
    wattroff(topwin, reverse_attr);
2239

2240
    wnoutrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
2241
    reset_cursor();
2242
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
2243
2244
}

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2268
2269
2270
/* 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. */
2271
2272
2273
void statusbar(const char *msg, ...)
{
    va_list ap;
2274
2275
    char *bar, *foo;
    size_t start_x, foo_len;
2276
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
2277
2278
    bool old_whitespace;
#endif
2279
2280
2281
2282
2283

    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(). */
2284
    if (isendwin()) {
2285
2286
2287
2288
2289
2290
2291
	vfprintf(stderr, msg, ap);
	va_end(ap);
	return;
    }

    blank_statusbar();

2292
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
2293
2294
    old_whitespace = ISSET(WHITESPACE_DISPLAY);
    UNSET(WHITESPACE_DISPLAY);
2295
#endif
2296
2297
2298
2299
    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);
2300
#if !defined(NANO_TINY) && !defined(DISABLE_NANORC)
2301
2302
    if (old_whitespace)
	SET(WHITESPACE_DISPLAY);
2303
#endif
2304
2305
2306
    free(bar);
    foo_len = strlenpt(foo);
    start_x = (COLS - foo_len - 4) / 2;
2307

2308
    wmove(bottomwin, 0, start_x);
2309
    wattron(bottomwin, reverse_attr);
2310
2311
2312
2313
    waddstr(bottomwin, "[ ");
    waddstr(bottomwin, foo);
    free(foo);
    waddstr(bottomwin, " ]");
2314
    wattroff(bottomwin, reverse_attr);
2315
2316
2317
2318
2319
    wnoutrefresh(bottomwin);
    reset_cursor();
    wnoutrefresh(edit);
	/* Leave the cursor at its position in the edit window, not in
	 * the statusbar. */
2320

2321
    disable_cursorpos = TRUE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2322
2323
2324

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

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

Chris Allegretta's avatar
Chris Allegretta committed
2342
2343
2344
    if (ISSET(NO_HELP))
	return;

2345
    if (menu == MMAIN) {
2346
	slen = MAIN_VISIBLE;
2347

2348
	assert(slen <= length_of_list(menu));
2349
    } else {
2350
	slen = length_of_list(menu);
2351

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

2357
2358
2359
2360
    /* 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. */
2361
    colwidth = COLS / ((slen / 2) + (slen % 2));
Chris Allegretta's avatar
Chris Allegretta committed
2362

2363
    blank_bottombars();
2364

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

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

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

2377
        if (!f->desc || strlen(f->desc) == 0)
2378
2379
	    continue;

2380
#ifdef DEBUG
2381
        fprintf(stderr, "found one! f->menus = %x, desc = \"%s\"\n", f->menus, f->desc);
2382
2383
2384
2385
2386
2387
2388
2389
#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;
        }
2390
	wmove(bottomwin, 1 + i % 2, (i / 2) * colwidth);
2391
2392
2393
#ifdef DEBUG
        fprintf(stderr, "Calling onekey with keystr \"%s\" and desc \"%s\"\n", s->keystr, f->desc);
#endif
2394
	onekey(s->keystr, _(f->desc), colwidth + (COLS % colwidth));
2395
        i++;
Chris Allegretta's avatar
Chris Allegretta committed
2396
    }
2397

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

2403
2404
2405
2406
2407
2408
/* 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
2409
{
2410
2411
    size_t keystroke_len = strlenpt(keystroke) + 1;

2412
2413
    assert(keystroke != NULL && desc != NULL);

2414
    wattron(bottomwin, reverse_attr);
2415
    waddnstr(bottomwin, keystroke, actual_x(keystroke, len));
2416
    wattroff(bottomwin, reverse_attr);
2417
2418
2419
2420
2421
2422

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

2423
2424
    if (len > 0) {
	waddch(bottomwin, ' ');
2425
	waddnstr(bottomwin, desc, actual_x(desc, len));
Chris Allegretta's avatar
Chris Allegretta committed
2426
2427
2428
    }
}

2429
2430
/* 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
2431
2432
void reset_cursor(void)
{
2433
    size_t xpt;
2434
2435
    /* If we haven't opened any files yet, put the cursor in the top
     * left corner of the edit window and get out. */
2436
    if (openfile == NULL) {
2437
	wmove(edit, 0, 0);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2438
	return;
2439
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2440

2441
    xpt = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2442

2443
2444
    if (ISSET(SOFTWRAP)) {
	filestruct *tmp;
2445
2446
	openfile->current_y = 0;

2447
	for (tmp = openfile->edittop; tmp && tmp != openfile->current; tmp = tmp->next)
2448
	    openfile->current_y += 1 + strlenpt(tmp->data) / COLS;
2449

2450
	openfile->current_y += xplustabs() / COLS;
2451
	if (openfile->current_y < editwinrows)
2452
	    wmove(edit, openfile->current_y, xpt % COLS);
2453
2454
2455
2456
2457
2458
2459
    } else {
	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
2460
}
Chris Allegretta's avatar
Chris Allegretta committed
2461

2462
2463
2464
2465
2466
2467
2468
2469
/* 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. */
2470
void edit_draw(filestruct *fileptr, const char *converted, int
2471
	line, size_t start)
Chris Allegretta's avatar
Chris Allegretta committed
2472
{
2473
#if !defined(NANO_TINY) || !defined(DISABLE_COLOR)
2474
2475
2476
2477
2478
2479
2480
2481
2482
    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. */
2483
2484
#endif

2485
    assert(openfile != NULL && fileptr != NULL && converted != NULL);
2486
    assert(strlenpt(converted) <= COLS);
2487

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

2492
#ifndef DISABLE_COLOR
2493
2494
2495
2496
    /* 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;
2497

2498
	/* Set up multi-line color data for this line if it's not yet calculated. */
2499
        if (fileptr->multidata == NULL && openfile->syntax
2500
		&& openfile->syntax->nmultis > 0) {
2501
	    int i;
2502
	    fileptr->multidata = (short *) nmalloc(openfile->syntax->nmultis * sizeof(short));
2503
            for (i = 0; i < openfile->syntax->nmultis; i++)
2504
		fileptr->multidata[i] = -1;	/* Assume this applies until we know otherwise. */
2505
	}
2506
2507
2508
2509
	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
2510
2511
		/* Number of chars to paint on this line.  There are
		 * COLS characters on a whole line. */
2512
	    size_t index;
2513
		/* Index in converted where we paint. */
2514
2515
2516
2517
	    regmatch_t startmatch;
		/* Match position for start_regex. */
	    regmatch_t endmatch;
		/* Match position for end_regex. */
2518
2519
2520
2521

	    if (tmpcolor->bright)
		wattron(edit, A_BOLD);
	    wattron(edit, COLOR_PAIR(tmpcolor->pairnum));
2522
2523
2524
	    /* 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. */
2525

2526
	    /* First case, tmpcolor is a single-line expression. */
2527
	    if (tmpcolor->end == NULL) {
2528
2529
2530
		size_t k = 0;

		/* We increment k by rm_eo, to move past the end of the
2531
		 * last match.  Even though two matches may overlap, we
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2532
2533
		 * want to ignore them, so that we can highlight e.g. C
		 * strings correctly. */
2534
2535
2536
		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
2537
2538
2539
		     * unless k is zero.  If regexec() returns
		     * REG_NOMATCH, there are no more matches in the
		     * line. */
2540
		    if (regexec(tmpcolor->start, &fileptr->data[k], 1,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2541
2542
			&startmatch, (k == 0) ? 0 : REG_NOTBOL) ==
			REG_NOMATCH)
2543
			break;
2544
2545
		    /* Translate the match to the beginning of the
		     * line. */
2546
2547
		    startmatch.rm_so += k;
		    startmatch.rm_eo += k;
2548
2549
2550

		    /* Skip over a zero-length regex match. */
		    if (startmatch.rm_so == startmatch.rm_eo)
2551
			startmatch.rm_eo++;
2552
		    else if (startmatch.rm_so < endpos &&
2553
			startmatch.rm_eo > startpos) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2554
2555
			x_start = (startmatch.rm_so <= startpos) ? 0 :
				strnlenpt(fileptr->data,
2556
				startmatch.rm_so) - start;
2557

2558
2559
2560
			index = actual_x(converted, x_start);

			paintlen = actual_x(converted + index,
2561
2562
				strnlenpt(fileptr->data,
				startmatch.rm_eo) - start - x_start);
2563
2564
2565

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

2566
			mvwaddnstr(edit, line, x_start, converted +
2567
				index, paintlen);
2568
		    }
2569
		    k = startmatch.rm_eo;
Chris Allegretta's avatar
Chris Allegretta committed
2570
		}
2571
	    } else if (fileptr->multidata != NULL && fileptr->multidata[tmpcolor->id] != CNONE) {
2572
		/* This is a multi-line regex.  There are two steps.
2573
2574
2575
2576
2577
2578
		 * 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
2579
2580
2581
2582
		 * 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. */
2583
		const filestruct *start_line = fileptr->prev;
2584
		    /* The first line before fileptr matching start. */
2585
		regoff_t start_col;
2586
		    /* Where it starts in that line. */
2587
		const filestruct *end_line;
2588
2589
2590
		short md = fileptr->multidata[tmpcolor->id];

		if (md == -1)
2591
		    fileptr->multidata[tmpcolor->id] = CNONE; /* until we find out otherwise */
2592
2593
2594
2595
2596
		else if (md == CNONE)
		    continue;
		else if (md == CWHOLELINE) {
		    mvwaddnstr(edit, line, 0, converted, -1);
		    continue;
2597
2598
2599
2600
2601
2602
		} 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);
		    continue;
2603
		}
2604

2605
		while (start_line != NULL && regexec(tmpcolor->start,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2606
2607
			start_line->data, 1, &startmatch, 0) ==
			REG_NOMATCH) {
2608
2609
		    /* If there is an end on this line, there is no need
		     * to look for starts on earlier lines. */
2610
2611
		    if (regexec(tmpcolor->end, start_line->data, 0,
			NULL, 0) == 0)
2612
2613
2614
			goto step_two;
		    start_line = start_line->prev;
		}
2615
2616
2617

		/* Skip over a zero-length regex match. */
		if (startmatch.rm_so == startmatch.rm_eo)
2618
		    startmatch.rm_eo++;
2619
		else {
2620
2621
		    /* No start found, so skip to the next step. */
		    if (start_line == NULL)
2622
			goto step_two;
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
		    /* 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
2652
			end_line->data, 1, &endmatch, 0) == REG_NOMATCH)
2653
			end_line = end_line->next;
2654

2655
2656
		    /* No end found, or it is too early. */
		    if (end_line == NULL || (end_line == fileptr &&
2657
			endmatch.rm_eo <= startpos))
2658
			goto step_two;
2659

2660
2661
2662
2663
2664
2665
2666
		    /* 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. */
2667
		    if (end_line != fileptr) {
2668
			paintlen = -1;
2669
2670
			fileptr->multidata[tmpcolor->id] = CWHOLELINE;
		    } else {
2671
2672
2673
			paintlen = actual_x(converted,
				strnlenpt(fileptr->data,
				endmatch.rm_eo) - start);
2674
2675
			fileptr->multidata[tmpcolor->id] = CBEGINBEFORE;
		    }
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
		    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
2689
			 * beginning of the line. */
2690
2691
2692
2693
			startmatch.rm_so += start_col;
			startmatch.rm_eo += start_col;

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

2697
			index = actual_x(converted, x_start);
2698

2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
			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);
2716

2717
2718
2719
2720
				assert(0 <= x_start && x_start < COLS);

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

2724
2725
2726
2727
2728
2729
2730
2731
			    }
			} 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 &&
2732
2733
				regexec(tmpcolor->end, end_line->data,
				0, NULL, 0) == REG_NOMATCH)
2734
				end_line = end_line->next;
2735

2736
2737
			    if (end_line != NULL) {
				assert(0 <= x_start && x_start < COLS);
2738

2739
2740
2741
2742
2743
				mvwaddnstr(edit, line, x_start,
					converted + index, -1);
				/* We painted to the end of the line, so
				 * don't bother checking any more
				 * starts. */
2744
				fileptr->multidata[tmpcolor->id] = CENDAFTER;
2745
2746
				break;
			    }
2747
			}
2748
			start_col = startmatch.rm_so + 1;
2749
		    }
2750
2751
		}
	    }
2752

2753
2754
	    wattroff(edit, A_BOLD);
	    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
2755
	}
2756
    }
2757
#endif /* !DISABLE_COLOR */
2758

2759
#ifndef NANO_TINY
2760
    /* If the mark is on, we need to display it. */
2761
    if (openfile->mark_set && (fileptr->lineno <=
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2762
	openfile->mark_begin->lineno || fileptr->lineno <=
2763
	openfile->current->lineno) && (fileptr->lineno >=
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2764
	openfile->mark_begin->lineno || fileptr->lineno >=
2765
	openfile->current->lineno)) {
2766
	/* fileptr is at least partially selected. */
2767
	const filestruct *top;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2768
	    /* Either current or mark_begin, whichever is first. */
2769
	size_t top_x;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2770
	    /* current_x or mark_begin_x, corresponding to top. */
2771
2772
	const filestruct *bot;
	size_t bot_x;
2773
	int x_start;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2774
	    /* Starting column for mvwaddnstr().  Zero-based. */
2775
	int paintlen;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2776
2777
	    /* Number of characters to paint on this line.  There are
	     * COLS characters on a whole line. */
2778
	size_t index;
2779
	    /* Index in converted where we paint. */
2780

2781
	mark_order(&top, &top_x, &bot, &bot_x, NULL);
2782
2783
2784
2785
2786

	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
2787

2788
	/* The selected bit of fileptr is on this page. */
2789
2790
	if (top_x < endpos && bot_x > startpos) {
	    assert(startpos <= top_x);
2791
2792
2793
2794

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

2796
2797
2798
2799
2800
	    /* 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. */
2801
2802
2803
2804
2805
	    if (bot_x >= endpos)
		paintlen = -1;
	    else
		paintlen = strnlenpt(fileptr->data, bot_x) - (x_start +
			start);
2806
2807
2808
2809
2810
2811
2812
2813

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

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

2817
	    index = actual_x(converted, x_start);
2818

2819
2820
2821
	    if (paintlen > 0)
		paintlen = actual_x(converted + index, paintlen);

2822
	    wattron(edit, reverse_attr);
2823
	    mvwaddnstr(edit, line, x_start, converted + index,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
2824
		paintlen);
2825
	    wattroff(edit, reverse_attr);
Chris Allegretta's avatar
Chris Allegretta committed
2826
	}
2827
    }
2828
#endif /* !NANO_TINY */
Chris Allegretta's avatar
Chris Allegretta committed
2829
2830
}

2831
/* Just update one line in the edit buffer.  This is basically a wrapper
2832
 * for edit_draw().  The line will be displayed starting with
2833
 * fileptr->data[index].  Likely arguments are current_x or zero.
2834
 * Returns: Number of additional lines consumed (needed for SOFTWRAP). */
2835
int update_line(filestruct *fileptr, size_t index)
Chris Allegretta's avatar
Chris Allegretta committed
2836
{
2837
    int line = 0;
2838
	/* The line in the edit window that we want to update. */
2839
    int extralinesused = 0;
2840
2841
2842
2843
    char *converted;
	/* fileptr->data converted to have tabs and control characters
	 * expanded. */
    size_t page_start;
2844
    filestruct *tmp;
Chris Allegretta's avatar
Chris Allegretta committed
2845

2846
    assert(fileptr != NULL);
2847

2848
    if (ISSET(SOFTWRAP)) {
2849
	for (tmp = openfile->edittop; tmp && tmp != fileptr; tmp = tmp->next) {
2850
2851
2852
2853
2854
	    line += 1 + (strlenpt(tmp->data) / COLS);
	}
    } else
	line = fileptr->lineno - openfile->edittop->lineno;

2855
    if (line < 0 || line >= editwinrows)
Chris Allegretta's avatar
Chris Allegretta committed
2856
	return 1;
2857

2858
    /* First, blank out the line. */
2859
    blank_line(edit, line, 0, COLS);
2860

2861
2862
    /* Next, convert variables that index the line to their equivalent
     * positions in the expanded line. */
2863
2864
2865
2866
    if (ISSET(SOFTWRAP))
	index = 0;
    else
	index = strnlenpt(fileptr->data, index);
2867
    page_start = get_page_start(index);
2868

2869
2870
    /* Expand the line, replacing tabs with spaces, and control
     * characters with their displayed forms. */
2871
2872
2873
2874
2875
2876
2877
    converted = display_string(fileptr->data, page_start, COLS, !ISSET(SOFTWRAP));

#ifdef DEBUG
    if (ISSET(SOFTWRAP) && strlen(converted) >= COLS - 2)
	    fprintf(stderr, "update_line(): converted(1) line = %s\n", converted);
#endif

Chris Allegretta's avatar
Chris Allegretta committed
2878

2879
    /* Paint the line. */
2880
    edit_draw(fileptr, converted, line, page_start);
2881
    free(converted);
Chris Allegretta's avatar
Chris Allegretta committed
2882

2883
2884
2885
2886
2887
2888
2889
    if (!ISSET(SOFTWRAP)) {
	if (page_start > 0)
	    mvwaddch(edit, line, 0, '$');
	if (strlenpt(fileptr->data) > page_start + COLS)
	    mvwaddch(edit, line, COLS - 1, '$');
    } else {
        int full_length = strlenpt(fileptr->data);
2890
	for (index += COLS; index <= full_length && line < editwinrows; index += COLS) {
2891
2892
	    line++;
#ifdef DEBUG
2893
	    fprintf(stderr, "update_line(): Softwrap code, moving to %d index %lu\n", line, (unsigned long) index);
2894
2895
2896
2897
2898
#endif
 	    blank_line(edit, line, 0, COLS);

	    /* Expand the line, replacing tabs with spaces, and control
 	     * characters with their displayed forms. */
2899
2900
2901
2902
2903
	    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
2904
2905
2906
2907
2908
2909
2910
2911

	    /* Paint the line. */
	    edit_draw(fileptr, converted, line, index);
 	    free(converted);
	    extralinesused++;
	}
    }
    return extralinesused;
Chris Allegretta's avatar
Chris Allegretta committed
2912
2913
}

2914
/* Return TRUE if we need an update after moving horizontally, and FALSE
2915
 * otherwise.  We need one if the mark is on or if pww_save and
2916
 * placewewant are on different pages. */
2917
bool need_horizontal_update(size_t pww_save)
2918
2919
{
    return
2920
#ifndef NANO_TINY
2921
	openfile->mark_set ||
2922
#endif
2923
	get_page_start(pww_save) !=
2924
	get_page_start(openfile->placewewant);
2925
2926
}

2927
/* Return TRUE if we need an update after moving vertically, and FALSE
2928
 * otherwise.  We need one if the mark is on or if pww_save and
2929
 * placewewant are on different pages. */
2930
bool need_vertical_update(size_t pww_save)
2931
2932
{
    return
2933
#ifndef NANO_TINY
2934
	openfile->mark_set ||
2935
#endif
2936
	get_page_start(pww_save) !=
2937
	get_page_start(openfile->placewewant);
2938
2939
}

2940
/* When edittop changes, try and figure out how many lines
2941
 * we really have to work with (i.e. set maxrows). */
2942
2943
2944
2945
2946
void compute_maxrows(void)
{
    int n;
    filestruct *foo = openfile->edittop;

2947
2948
2949
2950
2951
    if (!ISSET(SOFTWRAP)) {
	maxrows = editwinrows;
	return;
    }

2952
2953
    maxrows = 0;
    for (n = 0; n < editwinrows && foo; n++) {
2954
	maxrows++;
2955
	n += strlenpt(foo->data) / COLS;
2956
2957
2958
	foo = foo->next;
    }

2959
2960
2961
    if (n < editwinrows)
	maxrows += editwinrows - n;

2962
#ifdef DEBUG
2963
    fprintf(stderr, "compute_maxrows(): maxrows = %i\n", maxrows);
2964
2965
2966
#endif
}

2967
2968
/* 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
2969
2970
2971
2972
 * 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. */
2973
void edit_scroll(scroll_dir direction, ssize_t nlines)
2974
{
2975
    ssize_t i;
2976
    filestruct *foo;
2977
    bool do_redraw = FALSE;
2978

2979
2980
    /* Don't bother scrolling less than one line. */
    if (nlines < 1)
2981
2982
	return;

2983
    if (need_vertical_update(0))
2984
2985
	do_redraw = TRUE;

2986
2987
2988
    /* Part 1: nlines is the number of lines we're going to scroll the
     * text of the edit window. */

2989
    /* Move the top line of the edit window up or down (depending on the
2990
2991
     * value of direction) nlines lines, or as many lines as we can if
     * there are fewer than nlines lines available. */
2992
    for (i = nlines; i > 0; i--) {
2993
	if (direction == UP_DIR) {
2994
	    if (openfile->edittop == openfile->fileage)
2995
		break;
2996
	    openfile->edittop = openfile->edittop->prev;
2997
	} else {
2998
	    if (openfile->edittop == openfile->filebot)
2999
		break;
3000
	    openfile->edittop = openfile->edittop->next;
3001
	}
3002
	/* Don't over-scroll on long lines. */
3003
	if (ISSET(SOFTWRAP) && (direction == UP_DIR)) {
3004
3005
3006
3007
3008
	    ssize_t len = strlenpt(openfile->edittop->data) / COLS;
	    i -=  len;
	    if (len > 0)
		do_redraw = TRUE;
	}
3009
3010
    }

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3014
3015
    /* 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
3016
     * call edit_refresh() beforehand if we need to. */
3017
    if (nlines == 0 || do_redraw || nlines >= editwinrows) {
3018
	if (do_redraw || nlines >= editwinrows)
3019
	    edit_refresh_needed = TRUE;
3020
3021
	return;
    }
3022
3023
3024

    /* Scroll the text of the edit window up or down nlines lines,
     * depending on the value of direction. */
3025
    scrollok(edit, TRUE);
3026
    wscrl(edit, (direction == UP_DIR) ? -nlines : nlines);
3027
3028
    scrollok(edit, FALSE);

3029
3030
3031
    /* Part 2: nlines is the number of lines in the scrolled region of
     * the edit window that we need to draw. */

3032
3033
    /* If the top or bottom line of the file is now visible in the edit
     * window, we need to draw the entire edit window. */
3034
3035
3036
3037
    if ((direction == UP_DIR && openfile->edittop ==
	openfile->fileage) || (direction == DOWN_DIR &&
	openfile->edittop->lineno + editwinrows - 1 >=
	openfile->filebot->lineno))
3038
	nlines = editwinrows;
3039

3040
3041
3042
3043
3044
3045
    /* 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
3046

3047
3048
    if (nlines > editwinrows)
	nlines = editwinrows;
3049
3050
3051

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

3054
3055
    /* If we scrolled down, move down to the line before the scrolled
     * region. */
3056
    if (direction == DOWN_DIR) {
3057
	for (i = editwinrows - nlines; i > 0 && foo != NULL; i--)
3058
3059
3060
	    foo = foo->next;
    }

3061
3062
3063
3064
3065
3066
    /* 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--) {
3067
3068
	if ((i == nlines && direction == DOWN_DIR) || (i == 1 &&
		direction == UP_DIR)) {
3069
3070
3071
3072
3073
	    if (do_redraw)
		update_line(foo, (foo == openfile->current) ?
			openfile->current_x : 0);
	} else
	    update_line(foo, (foo == openfile->current) ?
3074
		openfile->current_x : 0);
3075
	foo = foo->next;
3076
    }
3077
    compute_maxrows();
3078
3079
3080
}

/* Update any lines between old_current and current that need to be
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3081
 * updated.  Use this if we've moved without changing any text. */
3082
void edit_redraw(filestruct *old_current, size_t pww_save)
3083
{
3084
    bool do_redraw = need_vertical_update(0) ||
3085
	need_vertical_update(pww_save);
3086
    filestruct *foo = NULL;
3087

3088
3089
    /* If either old_current or current is offscreen, scroll the edit
     * window until it's onscreen and get out. */
3090
3091
    if (old_current->lineno < openfile->edittop->lineno ||
	old_current->lineno >= openfile->edittop->lineno +
3092
	maxrows || openfile->current->lineno <
3093
	openfile->edittop->lineno || openfile->current->lineno >=
3094
	openfile->edittop->lineno + maxrows) {
3095

3096
#ifdef DEBUG
3097
3098
    fprintf(stderr, "edit_redraw(): line %d was offscreen, oldcurrent = %d edittop = %d",
	openfile->current->lineno, old_current->lineno, openfile->edittop->lineno);
3099
#endif
3100

3101
3102
#ifndef NANO_TINY
	/* If the mark is on, update all the lines between old_current
3103
3104
	 * and either the old first line or old last line (depending on
	 * whether we've scrolled up or down) of the edit window. */
3105
	if (openfile->mark_set) {
3106
	    ssize_t old_lineno;
3107
	    filestruct *old_edittop = openfile->edittop;
3108
3109
3110
3111

	    if (old_edittop->lineno < openfile->edittop->lineno)
		old_lineno = old_edittop->lineno;
	    else
3112
		old_lineno = (old_edittop->lineno + maxrows <=
3113
3114
3115
			openfile->filebot->lineno) ?
			old_edittop->lineno + editwinrows :
			openfile->filebot->lineno;
3116
3117
3118

	    foo = old_current;

3119
	    while (foo->lineno != old_lineno) {
3120
3121
		update_line(foo, 0);

3122
		foo = (foo->lineno > old_lineno) ? foo->prev :
3123
3124
3125
3126
3127
			foo->next;
	    }
	}
#endif /* !NANO_TINY */

3128
3129
3130
	/* 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. */
3131
	edit_update(CENTER);
3132

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3133
3134
	/* Update old_current if we're not on the same page as
	 * before. */
3135
3136
3137
	if (do_redraw)
	    update_line(old_current, 0);

3138
#ifndef NANO_TINY
3139
3140
3141
	/* 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. */
3142
	if (openfile->mark_set) {
3143
	    while (foo->lineno != openfile->current->lineno) {
3144
3145
		update_line(foo, 0);

3146
		foo = (foo->lineno > openfile->current->lineno) ?
3147
3148
3149
3150
3151
			foo->prev : foo->next;
	    }
	}
#endif /* !NANO_TINY */

3152
3153
3154
	return;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3155
3156
3157
    /* 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. */
3158
    foo = old_current;
3159

3160
    while (foo != openfile->current) {
3161
	if (do_redraw)
3162
	    update_line(foo, 0);
3163

3164
#ifndef NANO_TINY
3165
	if (!openfile->mark_set)
3166
3167
#endif
	    break;
3168

3169
#ifndef NANO_TINY
3170
3171
	foo = (foo->lineno > openfile->current->lineno) ? foo->prev :
		foo->next;
3172
#endif
3173
    }
3174

3175
    if (do_redraw)
3176
	update_line(openfile->current, openfile->current_x);
3177
3178
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3179
3180
/* 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
3181
3182
void edit_refresh(void)
{
3183
    filestruct *foo;
3184
    int nlines;
3185

3186
    /* Figure out what maxrows should really be. */
3187
    compute_maxrows();
3188

3189
3190
    if (openfile->current->lineno < openfile->edittop->lineno ||
	openfile->current->lineno >= openfile->edittop->lineno +
3191
3192
3193
3194
3195
3196
	maxrows) {

#ifdef DEBUG
    fprintf(stderr, "edit_refresh(): line = %d, edittop %d + maxrows %d\n", openfile->current->lineno, openfile->edittop->lineno, maxrows);
#endif

3197
3198
	/* Put the top line of the edit window in range of the current
	 * line. */
3199
	edit_update(CENTER);
3200
    }
Chris Allegretta's avatar
Chris Allegretta committed
3201

3202
3203
    foo = openfile->edittop;

3204
#ifdef DEBUG
3205
    fprintf(stderr, "edit_refresh(): edittop->lineno = %ld\n", (long)openfile->edittop->lineno);
3206
#endif
3207

3208
    for (nlines = 0; nlines < editwinrows && foo != NULL; nlines++) {
3209
	nlines += update_line(foo, (foo == openfile->current) ?
3210
		openfile->current_x : 0);
3211
3212
3213
	foo = foo->next;
    }

3214
    for (; nlines < editwinrows; nlines++)
3215
3216
3217
	blank_line(edit, nlines, 0, COLS);

    reset_cursor();
3218
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3219
3220
}

3221
3222
3223
3224
/* 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. */
3225
void edit_update(update_type location)
Chris Allegretta's avatar
Chris Allegretta committed
3226
{
3227
    filestruct *foo = openfile->current;
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
    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)
3239
	goal = editwinrows / 2;
3240
3241
    else {
	goal = openfile->current_y;
3242

3243
	/* Limit goal to (editwinrows - 1) lines maximum. */
3244
3245
	if (goal > editwinrows - 1)
	    goal = editwinrows - 1;
Chris Allegretta's avatar
Chris Allegretta committed
3246
    }
3247

3248
    for (; goal > 0 && foo->prev != NULL; goal--) {
3249
	foo = foo->prev;
3250
3251
	if (ISSET(SOFTWRAP) && foo)
	    goal -= strlenpt(foo->data) / COLS;
3252
    }
3253
    openfile->edittop = foo;
3254
3255
3256
#ifdef DEBUG
    fprintf(stderr, "edit_udpate(), setting edittop to lineno %d\n", openfile->edittop->lineno);
#endif
3257
    compute_maxrows();
3258
    edit_refresh_needed = TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
3259
3260
}

3261
/* Unconditionally redraw the entire screen. */
3262
void total_redraw(void)
3263
{
3264
3265
3266
3267
3268
3269
#ifdef USE_SLANG
    /* Slang curses emulation brain damage, part 4: Slang doesn't define
     * curscr. */
    SLsmg_touch_screen();
    SLsmg_refresh();
#else
3270
    wrefresh(curscr);
3271
#endif
3272
3273
}

3274
3275
/* Unconditionally redraw the entire screen, and then refresh it using
 * the current file. */
3276
3277
void total_refresh(void)
{
3278
    total_redraw();
3279
    titlebar(NULL);
3280
    edit_refresh();
3281
    bottombars(currmenu);
3282
3283
}

3284
3285
/* Display the main shortcut list on the last two rows of the bottom
 * portion of the window. */
3286
3287
void display_main_list(void)
{
3288
#ifndef DISABLE_COLOR
3289
3290
3291
3292
3293
3294
    if (openfile->syntax && openfile->syntax->linter)
	set_lint_shortcuts();
    else
	set_spell_shortcuts();
#endif

3295
    bottombars(MMAIN);
3296
3297
}

3298
3299
3300
3301
3302
3303
/* 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. */
3304
void do_cursorpos(bool constant)
Chris Allegretta's avatar
Chris Allegretta committed
3305
{
3306
    filestruct *f;
3307
    char c;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3308
    size_t i, cur_xpt = xplustabs() + 1;
3309
    size_t cur_lenpt = strlenpt(openfile->current->data) + 1;
3310
    int linepct, colpct, charpct;
Chris Allegretta's avatar
Chris Allegretta committed
3311

3312
    assert(openfile->fileage != NULL && openfile->current != NULL);
3313

3314
    f = openfile->current->next;
3315
    c = openfile->current->data[openfile->current_x];
3316
3317

    openfile->current->next = NULL;
3318
    openfile->current->data[openfile->current_x] = '\0';
3319
3320
3321

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

3322
    openfile->current->data[openfile->current_x] = c;
3323
    openfile->current->next = f;
3324

3325
3326
    if (constant && disable_cursorpos) {
	disable_cursorpos = FALSE;
3327
	return;
3328
    }
Chris Allegretta's avatar
Chris Allegretta committed
3329

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3330
    /* Display the current cursor position on the statusbar, and set
3331
     * disable_cursorpos to FALSE. */
3332
3333
    linepct = 100 * openfile->current->lineno /
	openfile->filebot->lineno;
3334
    colpct = 100 * cur_xpt / cur_lenpt;
3335
3336
    charpct = (openfile->totsize == 0) ? 0 : 100 * i /
	openfile->totsize;
3337
3338

    statusbar(
3339
	_("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%lu (%d%%)"),
3340
	(long)openfile->current->lineno,
3341
	(long)openfile->filebot->lineno, linepct,
3342
	(unsigned long)cur_xpt, (unsigned long)cur_lenpt, colpct,
3343
	(unsigned long)i, (unsigned long)openfile->totsize, charpct);
3344

3345
    disable_cursorpos = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
3346
3347
}

3348
/* Unconditionally display the current cursor position. */
3349
void do_cursorpos_void(void)
3350
{
3351
    do_cursorpos(FALSE);
3352
3353
}

3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
void enable_nodelay(void)
{
   nodelay_mode = TRUE;
   nodelay(edit, TRUE);
}

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

3366
3367
/* Highlight the current word being replaced or spell checked.  We
 * expect word to have tabs and control characters expanded. */
3368
void do_replace_highlight(bool highlight, const char *word)
Chris Allegretta's avatar
Chris Allegretta committed
3369
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3370
    size_t y = xplustabs(), word_len = strlenpt(word);
Chris Allegretta's avatar
Chris Allegretta committed
3371

3372
    y = get_page_start(y) + COLS - y;
3373
	/* Now y is the number of columns that we can display on this
3374
	 * line. */
Chris Allegretta's avatar
Chris Allegretta committed
3375

3376
3377
3378
3379
3380
    assert(y > 0);

    if (word_len > y)
	y--;

Chris Allegretta's avatar
Chris Allegretta committed
3381
    reset_cursor();
3382
    wnoutrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
3383

3384
    if (highlight)
3385
	wattron(edit, reverse_attr);
Chris Allegretta's avatar
Chris Allegretta committed
3386

3387
    /* This is so we can show zero-length matches. */
3388
    if (word_len == 0)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3389
	waddch(edit, ' ');
3390
    else
3391
	waddnstr(edit, word, actual_x(word, y));
3392
3393
3394

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

3396
    if (highlight)
3397
	wattroff(edit, reverse_attr);
Chris Allegretta's avatar
Chris Allegretta committed
3398
3399
}

3400
#ifndef DISABLE_EXTRA
3401
#define CREDIT_LEN 57
3402
3403
#define XLCREDIT_LEN 8

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3404
3405
/* Easter egg: Display credits.  Assume nodelay(edit) and scrollok(edit)
 * are FALSE. */
3406
3407
void do_credits(void)
{
3408
3409
    bool old_more_space = ISSET(MORE_SPACE);
    bool old_no_help = ISSET(NO_HELP);
3410
    int kbinput = ERR, crpos = 0, xlpos = 0;
3411
3412
3413
    const char *credits[CREDIT_LEN] = {
	NULL,				/* "The nano text editor" */
	NULL,				/* "version" */
Chris Allegretta's avatar
Chris Allegretta committed
3414
3415
	VERSION,
	"",
3416
	NULL,				/* "Brought to you by:" */
Chris Allegretta's avatar
Chris Allegretta committed
3417
3418
3419
3420
3421
	"Chris Allegretta",
	"Jordi Mallach",
	"Adam Rogoyski",
	"Rob Siemborski",
	"Rocco Corsi",
3422
	"David Lawrence Ramsey",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3423
	"David Benbennick",
3424
	"Mike Frysinger",
3425
	"Benno Schulenberg",
Chris Allegretta's avatar
Chris Allegretta committed
3426
3427
	"Ken Tyler",
	"Sven Guckes",
3428
	NULL,				/* credits[16], handled below. */
Chris Allegretta's avatar
Chris Allegretta committed
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
	"Pauli Virtanen",
	"Daniele Medri",
	"Clement Laforet",
	"Tedi Heriyanto",
	"Bill Soudan",
	"Christian Weisgerber",
	"Erik Andersen",
	"Big Gaute",
	"Joshua Jensen",
	"Ryan Krebs",
	"Albert Chin",
	"",
3441
	NULL,				/* "Special thanks to:" */
3442
	"Monique, Brielle & Joseph",
Chris Allegretta's avatar
Chris Allegretta committed
3443
3444
3445
3446
3447
3448
	"Plattsburgh State University",
	"Benet Laboratories",
	"Amy Allegretta",
	"Linda Young",
	"Jeremy Robichaud",
	"Richard Kolb II",
3449
	NULL,				/* "The Free Software Foundation" */
Chris Allegretta's avatar
Chris Allegretta committed
3450
	"Linus Torvalds",
3451
	NULL,				/* "For ncurses:" */
3452
3453
3454
3455
	"Thomas Dickey",
	"Pavel Curtis",
	"Zeyd Ben-Halim",
	"Eric S. Raymond",
3456
3457
3458
3459
3460
3461
	NULL,				/* "and anyone else we forgot..." */
	NULL,				/* "Thank you for using nano!" */
	"",
	"",
	"",
	"",
3462
	"(C) 1999 - 2014",
3463
	"Free Software Foundation, Inc.",
3464
3465
3466
3467
	"",
	"",
	"",
	"",
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3468
	"http://www.nano-editor.org/"
3469
3470
    };

3471
    const char *xlcredits[XLCREDIT_LEN] = {
3472
3473
3474
3475
3476
3477
3478
3479
	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!")
3480
    };
3481

3482
    /* credits[16]: Make sure this name is displayed properly, since we
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
3483
3484
     * can't dynamically assign it above, using Unicode 00F6 (Latin
     * Small Letter O with Diaresis) if applicable. */
3485
    credits[16] =
3486
#ifdef ENABLE_UTF8
3487
	 using_utf8() ? "Florian K\xC3\xB6nig" :
3488
3489
3490
#endif
	"Florian K\xF6nig";

3491
3492
3493
3494
3495
3496
    if (!old_more_space || !old_no_help) {
	SET(MORE_SPACE);
	SET(NO_HELP);
	window_init();
    }

3497
3498
    curs_set(0);
    nodelay(edit, TRUE);
3499

3500
    blank_titlebar();
3501
    blank_topbar();
Chris Allegretta's avatar
Chris Allegretta committed
3502
    blank_edit();
3503
3504
    blank_statusbar();
    blank_bottombars();
3505

3506
    wrefresh(topwin);
Chris Allegretta's avatar
Chris Allegretta committed
3507
    wrefresh(edit);
3508
    wrefresh(bottomwin);
3509
    napms(700);
3510

3511
    for (crpos = 0; crpos < CREDIT_LEN + editwinrows / 2; crpos++) {
3512
	if ((kbinput = wgetch(edit)) != ERR)
3513
	    break;
3514

3515
	if (crpos < CREDIT_LEN) {
3516
	    const char *what;
3517
3518
	    size_t start_x;

3519
	    if (credits[crpos] == NULL) {
3520
		assert(0 <= xlpos && xlpos < XLCREDIT_LEN);
3521

3522
		what = _(xlcredits[xlpos]);
3523
		xlpos++;
3524
	    } else
3525
		what = credits[crpos];
3526

3527
	    start_x = COLS / 2 - strlenpt(what) / 2 - 1;
3528
3529
	    mvwaddstr(edit, editwinrows - 1 - (editwinrows % 2),
		start_x, what);
3530
	}
3531

3532
3533
3534
3535
	wrefresh(edit);

	if ((kbinput = wgetch(edit)) != ERR)
	    break;
3536
	napms(700);
3537

3538
	scrollok(edit, TRUE);
3539
	wscrl(edit, 1);
3540
	scrollok(edit, FALSE);
3541
	wrefresh(edit);
3542

3543
	if ((kbinput = wgetch(edit)) != ERR)
3544
	    break;
3545
	napms(700);
3546

3547
	scrollok(edit, TRUE);
3548
	wscrl(edit, 1);
3549
	scrollok(edit, FALSE);
3550
	wrefresh(edit);
3551
3552
    }

3553
3554
3555
    if (kbinput != ERR)
	ungetch(kbinput);

3556
3557
3558
3559
3560
3561
    if (!old_more_space || !old_no_help) {
	UNSET(MORE_SPACE);
	UNSET(NO_HELP);
	window_init();
    }

3562
    curs_set(1);
3563
    nodelay(edit, FALSE);
3564

3565
    total_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
3566
}
3567
#endif /* !DISABLE_EXTRA */