winio.c 43.8 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
/**************************************************************************
 *   winio.c                                                              *
 *                                                                        *
5
 *   Copyright (C) 1999-2002 Chris Allegretta                             *
Chris Allegretta's avatar
Chris Allegretta committed
6
7
 *   This program is free software; you can redistribute it and/or modify *
 *   it under the terms of the GNU General Public License as published by *
8
 *   the Free Software Foundation; either version 2, or (at your option)  *
Chris Allegretta's avatar
Chris Allegretta committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *   any later version.                                                   *
 *                                                                        *
 *   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.                         *
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
 *                                                                        *
 **************************************************************************/

#include <stdarg.h>
#include <string.h>
24
#include <stdlib.h>
25
#include <unistd.h>
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
26
#include <ctype.h>
27
#include <assert.h>
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
28
#include "config.h"
Chris Allegretta's avatar
Chris Allegretta committed
29
30
31
#include "proto.h"
#include "nano.h"

32
#ifdef ENABLE_NLS
Chris Allegretta's avatar
Chris Allegretta committed
33
34
35
36
37
38
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif

39
/* winio.c statics */
Chris Allegretta's avatar
Chris Allegretta committed
40
static int statblank = 0;	/* Number of keystrokes left after
41
				   we call statusbar(), before we
Chris Allegretta's avatar
Chris Allegretta committed
42
				   actually blank the statusbar */
43
44

/* Local Function Prototypes for only winio.c */
Chris Allegretta's avatar
Chris Allegretta committed
45
static int get_page_start(int column);
46

Chris Allegretta's avatar
Chris Allegretta committed
47
48
49
50
51
52
53
/* Window I/O */

int do_first_line(void)
{
    current = fileage;
    placewewant = 0;
    current_x = 0;
54
    edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
55
56
57
58
59
60
61
62
    return 1;
}

int do_last_line(void)
{
    current = filebot;
    placewewant = 0;
    current_x = 0;
63
    edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
64
65
66
    return 1;
}

Chris Allegretta's avatar
Chris Allegretta committed
67

68
/* Like xplustabs, but for a specific index of a specific filestruct */
69
int xpt(const filestruct *fileptr, int index)
Chris Allegretta's avatar
Chris Allegretta committed
70
71
72
73
74
75
76
77
78
79
{
    int i, tabs = 0;

    if (fileptr == NULL || fileptr->data == NULL)
	return 0;

    for (i = 0; i < index && fileptr->data[i] != 0; i++) {
	tabs++;

	if (fileptr->data[i] == NANO_CONTROL_I) {
80
	    if (tabs % tabsize == 0);
Chris Allegretta's avatar
Chris Allegretta committed
81
	    else
82
		tabs += tabsize - (tabs % tabsize);
Chris Allegretta's avatar
Chris Allegretta committed
83
84
85
	} else if (is_cntrl_char((int)fileptr->data[i]))
	    tabs++;
	else if (fileptr->data[i] & 0x80)
86
	    /* Make 8 bit chars only 1 column! */
Chris Allegretta's avatar
Chris Allegretta committed
87
	    ;
Chris Allegretta's avatar
Chris Allegretta committed
88
89
    }

Chris Allegretta's avatar
Chris Allegretta committed
90
91
92
    return tabs;
}

Chris Allegretta's avatar
Chris Allegretta committed
93
94
95
96
/* Return the placewewant associated with current_x.  That is, xplustabs
 * is the zero-based column position of the cursor.  Value is no smaller
 * than current_x. */
size_t xplustabs(void)
Chris Allegretta's avatar
Chris Allegretta committed
97
{
Chris Allegretta's avatar
Chris Allegretta committed
98
    return strnlenpt(current->data, current_x);
Chris Allegretta's avatar
Chris Allegretta committed
99
100
}

Chris Allegretta's avatar
Chris Allegretta committed
101
102
/* Return what current_x should be, given xplustabs() for the line. */
size_t actual_x(const filestruct *fileptr, size_t xplus)
Chris Allegretta's avatar
Chris Allegretta committed
103
{
Chris Allegretta's avatar
Chris Allegretta committed
104
105
106
107
108
109
110
111
112
113
114
115
    size_t i = 0;
	/* the position in fileptr->data, returned */
    size_t length = 0;
	/* the screen display width to data[i] */
    char *c;
	/* fileptr->data + i */

    assert(fileptr != NULL && fileptr->data != NULL);

    for (c = fileptr->data; length < xplus && *c != '\0'; i++, c++) {
	if (*c == '\t')
	    length += tabsize - length % tabsize;
Chris Allegretta's avatar
Chris Allegretta committed
116
	else if (is_cntrl_char((int)*c))
Chris Allegretta's avatar
Chris Allegretta committed
117
118
119
120
121
122
	    length += 2;
	else
	    length++;
    }
    assert(length == strnlenpt(fileptr->data, i));
    assert(i <= strlen(fileptr->data));
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
123

Chris Allegretta's avatar
Chris Allegretta committed
124
125
    if (length > xplus)
	i--;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
126

Chris Allegretta's avatar
Chris Allegretta committed
127
#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
128
    fprintf(stderr, _("actual_x for xplus=%d returns %d\n"), xplus, i);
Chris Allegretta's avatar
Chris Allegretta committed
129
#endif
Chris Allegretta's avatar
Chris Allegretta committed
130

Chris Allegretta's avatar
Chris Allegretta committed
131
    return i;
132
133
}

Chris Allegretta's avatar
Chris Allegretta committed
134
135
/* A strlen with tabs factored in, similar to xplustabs(). */
size_t strnlenpt(const char *buf, size_t size)
136
{
Chris Allegretta's avatar
Chris Allegretta committed
137
138
139
140
141
142
    size_t length = 0;

    if (buf != NULL)
	for (; *buf != '\0' && size != 0; size--, buf++) {
	    if (*buf == '\t')
		length += tabsize - (length % tabsize);
Chris Allegretta's avatar
Chris Allegretta committed
143
	    else if (is_cntrl_char((int)*buf))
Chris Allegretta's avatar
Chris Allegretta committed
144
		length += 2;
Chris Allegretta's avatar
Chris Allegretta committed
145
	    else
Chris Allegretta's avatar
Chris Allegretta committed
146
147
148
		length++;
	}
    return length;
Chris Allegretta's avatar
Chris Allegretta committed
149
150
}

Chris Allegretta's avatar
Chris Allegretta committed
151
size_t strlenpt(const char *buf)
152
{
Chris Allegretta's avatar
Chris Allegretta committed
153
    return strnlenpt(buf, -1);
154
155
}

Chris Allegretta's avatar
Chris Allegretta committed
156
157
/* Resets current_y, based on the position of current, and puts the
 * cursor at (current_y, current_x). */
Chris Allegretta's avatar
Chris Allegretta committed
158
159
void reset_cursor(void)
{
Chris Allegretta's avatar
Chris Allegretta committed
160
161
162
163
164
165
166
    const filestruct *ptr = edittop;
    size_t x;

    /* Yuck.  This condition can be true after open_file when opening the
     * first file. */
    if (edittop == NULL)
	return;
Chris Allegretta's avatar
Chris Allegretta committed
167
168
169
170
171
172
173
174
175

    current_y = 0;

    while (ptr != current && ptr != editbot && ptr->next != NULL) {
	ptr = ptr->next;
	current_y++;
    }

    x = xplustabs();
Chris Allegretta's avatar
Chris Allegretta committed
176
    wmove(edit, current_y, x - get_page_start(x));
Chris Allegretta's avatar
Chris Allegretta committed
177
178
179
180
}

void blank_bottombars(void)
{
Chris Allegretta's avatar
Chris Allegretta committed
181
182
183
184
    if (!no_help()) {
	mvwaddstr(bottomwin, 1, 0, hblank);
	mvwaddstr(bottomwin, 2, 0, hblank);
    }
Chris Allegretta's avatar
Chris Allegretta committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
}

void blank_edit(void)
{
    int i;
    for (i = 0; i <= editwinrows - 1; i++)
	mvwaddstr(edit, i, 0, hblank);
}


void blank_statusbar(void)
{
    mvwaddstr(bottomwin, 0, 0, hblank);
}

void blank_statusbar_refresh(void)
{
    blank_statusbar();
    wrefresh(bottomwin);
}

void check_statblank(void)
{
    if (statblank > 1)
	statblank--;
    else if (statblank == 1 && !ISSET(CONSTUPDATE)) {
	statblank--;
	blank_statusbar_refresh();
    }
}

Chris Allegretta's avatar
Chris Allegretta committed
216
217
218
219
220
221
222
/* Repaint the statusbar when getting a character in nanogetstr.  buf
 * should be no longer than COLS - 4.
 *
 * Note that we must turn on A_REVERSE here, since do_help turns it
 * off! */
static void nanoget_repaint(const char *buf, const char *inputbuf,
	int x)
223
{
Chris Allegretta's avatar
Chris Allegretta committed
224
    int len = strlen(buf) + 2;
225
    int wid = COLS - len;
226

Chris Allegretta's avatar
Chris Allegretta committed
227
228
229
    assert(wid >= 2);
    assert(0 <= x && x <= strlen(inputbuf));

230
    wattron(bottomwin, A_REVERSE);
231
    blank_statusbar();
Chris Allegretta's avatar
Chris Allegretta committed
232
233
234
235
236
    mvwaddstr(bottomwin, 0, 0, buf);
    waddch(bottomwin, ':');
    waddch(bottomwin, x < wid ? ' ' : '$');
    waddnstr(bottomwin, &inputbuf[wid * (x / wid)], wid);
    wmove(bottomwin, 0, (x % wid) + len);
237
    wattroff(bottomwin, A_REVERSE);
238
239
}

Chris Allegretta's avatar
Chris Allegretta committed
240
241
242
243
/* Get the input from the kb; this should only be called from
 * statusq(). */
static int nanogetstr(int allowtabs, const char *buf, const char *def,
			const shortcut *s
244
#ifndef DISABLE_TABCOMP
Chris Allegretta's avatar
Chris Allegretta committed
245
			, int *list
246
#endif
Chris Allegretta's avatar
Chris Allegretta committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
			)
{
    int kbinput;
    int x;
	/* the cursor position in 'answer' */
    int xend;
	/* length of 'answer', the status bar text */
    int tabbed = 0;
	/* used by input_tab() */
    const shortcut *t;

    xend = strlen(def);
    x = xend;
    answer = (char *)nrealloc(answer, xend + 1);
    if (xend > 0)
	strcpy(answer, def);
    else
	answer[0] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
265

266
#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
267
    currshortcut = s;
268
269
#endif

Chris Allegretta's avatar
Chris Allegretta committed
270
    /* Get the input! */
271

Chris Allegretta's avatar
Chris Allegretta committed
272
    nanoget_repaint(buf, answer, x);
Chris Allegretta's avatar
Chris Allegretta committed
273

274
275
276
    /* Make sure any editor screen updates are displayed before getting input */
    wrefresh(edit);

Chris Allegretta's avatar
Chris Allegretta committed
277
    while ((kbinput = wgetch(bottomwin)) != 13) {
278
	for (t = s; t != NULL; t = t->next) {
279
280
281
282
#ifdef DEBUG
	    fprintf(stderr, _("Aha! \'%c\' (%d)\n"), kbinput, kbinput);
#endif

283
	    if (kbinput == t->val && kbinput < 32) {
284

285
286
#ifndef DISABLE_HELP
		/* Have to do this here, it would be too late to do it in statusq */
Chris Allegretta's avatar
Chris Allegretta committed
287
		if (kbinput == NANO_HELP_KEY || kbinput == NANO_HELP_FKEY) {
288
289
290
291
		    do_help();
		    break;
		}
#endif
292
		return t->val;
Chris Allegretta's avatar
Chris Allegretta committed
293
294
	    }
	}
Chris Allegretta's avatar
Chris Allegretta committed
295
	assert(0 <= x && x <= xend && xend == strlen(answer));
Chris Allegretta's avatar
Chris Allegretta committed
296

Chris Allegretta's avatar
Chris Allegretta committed
297
298
299
	if (kbinput != '\t')
	    tabbed = 0;

Chris Allegretta's avatar
Chris Allegretta committed
300
	switch (kbinput) {
301

Chris Allegretta's avatar
Chris Allegretta committed
302
	    /* Stuff we want to equate with <enter>, ASCII 13 */
303
	case 343:
304
305
	    ungetch(13);	/* Enter on iris-ansi $TERM, sometimes */
	    break;
Chris Allegretta's avatar
Chris Allegretta committed
306
	    /* Stuff we want to ignore */
307
308
309
#ifdef PDCURSES
	case 541:
	case 542:
Chris Allegretta's avatar
Chris Allegretta committed
310
	case 543:		/* Right ctrl again */
311
	case 544:
Chris Allegretta's avatar
Chris Allegretta committed
312
	case 545:		/* Right alt again */
313
314
	    break;
#endif
315
#ifndef DISABLE_MOUSE
316
317
318
319
320
#ifdef NCURSES_MOUSE_VERSION
	case KEY_MOUSE:
	    do_mouse();
	    break;
#endif
321
#endif
322
	case NANO_HOME_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
323
	case KEY_HOME:
Chris Allegretta's avatar
Chris Allegretta committed
324
	    x = 0;
Chris Allegretta's avatar
Chris Allegretta committed
325
	    break;
326
	case NANO_END_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
327
	case KEY_END:
Chris Allegretta's avatar
Chris Allegretta committed
328
	    x = xend;
Chris Allegretta's avatar
Chris Allegretta committed
329
330
	    break;
	case KEY_RIGHT:
331
	case NANO_FORWARD_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
332
333
334
335
	    if (x < xend)
		x++;
	    break;
	case NANO_CONTROL_D:
Chris Allegretta's avatar
Chris Allegretta committed
336
337
338
	    if (x < xend) {
		memmove(answer + x, answer + x + 1, xend - x);
		xend--;
Chris Allegretta's avatar
Chris Allegretta committed
339
340
341
342
	    }
	    break;
	case NANO_CONTROL_K:
	case NANO_CONTROL_U:
Chris Allegretta's avatar
Chris Allegretta committed
343
344
345
	    null_at(&answer, 0);
	    xend = 0;
	    x = 0;
Chris Allegretta's avatar
Chris Allegretta committed
346
347
348
349
	    break;
	case KEY_BACKSPACE:
	case 127:
	case NANO_CONTROL_H:
Chris Allegretta's avatar
Chris Allegretta committed
350
351
	    if (x > 0) {
		memmove(answer + x - 1, answer + x, xend - x + 1);
Chris Allegretta's avatar
Chris Allegretta committed
352
		x--;
Chris Allegretta's avatar
Chris Allegretta committed
353
354
		xend--;
	    }
Chris Allegretta's avatar
Chris Allegretta committed
355
	    break;
356
#ifndef DISABLE_TABCOMP
Chris Allegretta's avatar
Chris Allegretta committed
357
	case NANO_CONTROL_I:
358
	    if (allowtabs) {
Chris Allegretta's avatar
Chris Allegretta committed
359
360
361
362
		int shift = 0;

		answer = input_tab(answer, x, &tabbed, &shift, list);
		xend = strlen(answer);
363
		x += shift;
Chris Allegretta's avatar
Chris Allegretta committed
364
365
		if (x > xend)
		    x = xend;
366
	    }
Chris Allegretta's avatar
Chris Allegretta committed
367
	    break;
368
#endif
Chris Allegretta's avatar
Chris Allegretta committed
369
	case KEY_LEFT:
370
	case NANO_BACK_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
371
	    if (x > 0)
Chris Allegretta's avatar
Chris Allegretta committed
372
373
374
375
376
377
		x--;
	    break;
	case KEY_UP:
	case KEY_DOWN:
	    break;

378
379
380
	case KEY_DC:
	    goto do_deletekey;

Chris Allegretta's avatar
Chris Allegretta committed
381
382
	case 27:
	    switch (kbinput = wgetch(edit)) {
383
	    case 'O':
Chris Allegretta's avatar
Chris Allegretta committed
384
		switch (kbinput = wgetch(edit)) {
385
		case 'F':
Chris Allegretta's avatar
Chris Allegretta committed
386
		    x = xend;
Chris Allegretta's avatar
Chris Allegretta committed
387
		    break;
388
		case 'H':
Chris Allegretta's avatar
Chris Allegretta committed
389
		    x = 0;
Chris Allegretta's avatar
Chris Allegretta committed
390
391
392
		    break;
		}
		break;
393
	    case '[':
Chris Allegretta's avatar
Chris Allegretta committed
394
395
396
397
398
399
		switch (kbinput = wgetch(edit)) {
		case 'C':
		    if (x < xend)
			x++;
		    break;
		case 'D':
Chris Allegretta's avatar
Chris Allegretta committed
400
		    if (x > 0)
Chris Allegretta's avatar
Chris Allegretta committed
401
402
			x--;
		    break;
403
404
		case '1':
		case '7':
Chris Allegretta's avatar
Chris Allegretta committed
405
		    x = 0;
406
407
408
		    goto skip_tilde;
		case '3':
		  do_deletekey:
Chris Allegretta's avatar
Chris Allegretta committed
409
410
411
		    if (x < xend) {
			memmove(answer + x, answer + x + 1, xend - x);
			xend--;
Chris Allegretta's avatar
Chris Allegretta committed
412
		    }
413
414
415
		    goto skip_tilde;
		case '4':
		case '8':
Chris Allegretta's avatar
Chris Allegretta committed
416
		    x = xend;
417
418
		    goto skip_tilde;
		  skip_tilde:
Chris Allegretta's avatar
Chris Allegretta committed
419
420
		    nodelay(edit, TRUE);
		    kbinput = wgetch(edit);
421
		    if (kbinput == '~' || kbinput == ERR)
Chris Allegretta's avatar
Chris Allegretta committed
422
423
424
425
			kbinput = -1;
		    nodelay(edit, FALSE);
		    break;
		}
Chris Allegretta's avatar
Chris Allegretta committed
426
		break;
427
428
	    default:

429
		for (t = s; t != NULL; t = t->next) {
430
#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
431
432
		    fprintf(stderr, _("Aha! \'%c\' (%d)\n"), kbinput,
			    kbinput);
433
#endif
434
		    if (kbinput == t->val || kbinput == t->val - 32) {
435
436
437
			/* We hit an Alt key.   Do like above.  We don't
			   just ungetch the letter and let it get caught
			   above cause that screws the keypad... */
438
			return t->val;
439
440
		    }
		}
Chris Allegretta's avatar
Chris Allegretta committed
441
442
443
444
445
446
	    }
	    break;

	default:
	    if (kbinput < 32)
		break;
Chris Allegretta's avatar
Chris Allegretta committed
447
448
449
450
	    answer = nrealloc(answer, xend + 2);
	    memmove(answer + x + 1, answer + x, xend - x + 1);
	    xend++;
	    answer[x] = kbinput;
Chris Allegretta's avatar
Chris Allegretta committed
451
452
453
454
455
456
	    x++;

#ifdef DEBUG
	    fprintf(stderr, _("input \'%c\' (%d)\n"), kbinput, kbinput);
#endif
	}
Chris Allegretta's avatar
Chris Allegretta committed
457
	nanoget_repaint(buf, answer, x);
Chris Allegretta's avatar
Chris Allegretta committed
458
	wrefresh(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
459
    } /* while (kbinput ...) */
Chris Allegretta's avatar
Chris Allegretta committed
460

Chris Allegretta's avatar
Chris Allegretta committed
461
    /* In Pico mode, just check for a blank answer here */
Chris Allegretta's avatar
Chris Allegretta committed
462
    if (ISSET(PICO_MODE) && answer[0] == '\0')
Chris Allegretta's avatar
Chris Allegretta committed
463
464
465
466
467
	return -2;
    else
	return 0;
}

Chris Allegretta's avatar
Chris Allegretta committed
468
void titlebar(const char *path)
Chris Allegretta's avatar
Chris Allegretta committed
469
470
{
    int namelen, space;
Chris Allegretta's avatar
Chris Allegretta committed
471
    const char *what = path;
Chris Allegretta's avatar
Chris Allegretta committed
472
473
474

    if (path == NULL)
	what = filename;
Chris Allegretta's avatar
Chris Allegretta committed
475
476

    wattron(topwin, A_REVERSE);
477

Chris Allegretta's avatar
Chris Allegretta committed
478
    mvwaddstr(topwin, 0, 0, hblank);
479
    mvwaddnstr(topwin, 0, 2, VERMSG, COLS - 3);
Chris Allegretta's avatar
Chris Allegretta committed
480

Chris Allegretta's avatar
Chris Allegretta committed
481
    space = COLS - sizeof(VERMSG) - 22;
Chris Allegretta's avatar
Chris Allegretta committed
482

Chris Allegretta's avatar
Chris Allegretta committed
483
    namelen = strlen(what);
Chris Allegretta's avatar
Chris Allegretta committed
484

485
486
    if (space > 0) {
        if (what[0] == '\0')
Chris Allegretta's avatar
Chris Allegretta committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
      	    mvwaddnstr(topwin, 0, COLS / 2 - 6, _("New Buffer"),
			COLS / 2 + COLS % 2 - 6);
        else if (namelen > space) {
	    if (path == NULL)
		waddstr(topwin, _("  File: ..."));
	    else
		waddstr(topwin, _("   DIR: ..."));
	    waddstr(topwin, &what[namelen - space]);
	} else {
	    if (path == NULL)
		mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1),
				_("File: "));
	    else
		mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1),
				_(" DIR: "));
	    waddstr(topwin, what);
Chris Allegretta's avatar
Chris Allegretta committed
503
	}
504
    } /* If we don't have space, we shouldn't bother */
Chris Allegretta's avatar
Chris Allegretta committed
505
    if (ISSET(MODIFIED))
Chris Allegretta's avatar
Chris Allegretta committed
506
	mvwaddnstr(topwin, 0, COLS - 11, _(" Modified "), 11);
Chris Allegretta's avatar
Chris Allegretta committed
507
    else if (ISSET(VIEW_MODE))
Chris Allegretta's avatar
Chris Allegretta committed
508
	mvwaddnstr(topwin, 0, COLS - 11, _(" View "), 11);
509

Chris Allegretta's avatar
Chris Allegretta committed
510
    wattroff(topwin, A_REVERSE);
511

Chris Allegretta's avatar
Chris Allegretta committed
512
513
514
515
    wrefresh(topwin);
    reset_cursor();
}

516
517
518
519
520
/* 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 exactly len characters, even if len is
 * very small and keystroke and desc are long. */
static void onekey(const char *keystroke, const char *desc, int len)
Chris Allegretta's avatar
Chris Allegretta committed
521
522
{
    wattron(bottomwin, A_REVERSE);
523
    waddnstr(bottomwin, keystroke, len);
Chris Allegretta's avatar
Chris Allegretta committed
524
    wattroff(bottomwin, A_REVERSE);
525
526
527
528
529
530
531
532
533
    len -= strlen(keystroke);
    if (len > 0) {
	waddch(bottomwin, ' ');
	len--;
	waddnstr(bottomwin, desc, len);
	len -= strlen(desc);
	for (; len > 0; len--)
	    waddch(bottomwin, ' ');
    }
Chris Allegretta's avatar
Chris Allegretta committed
534
535
}

Chris Allegretta's avatar
Chris Allegretta committed
536
static void clear_bottomwin(void)
Chris Allegretta's avatar
Chris Allegretta committed
537
538
539
540
541
542
543
544
{
    if (ISSET(NO_HELP))
	return;

    mvwaddstr(bottomwin, 1, 0, hblank);
    mvwaddstr(bottomwin, 2, 0, hblank);
}

545
void bottombars(const shortcut *s)
Chris Allegretta's avatar
Chris Allegretta committed
546
{
547
    int i, j, numcols;
548
    char keystr[4];
549
550
    int slen;

Chris Allegretta's avatar
Chris Allegretta committed
551
552
553
    if (ISSET(NO_HELP))
	return;

554
555
556
557
558
559
560
561
    if (s == main_list) {
	slen = MAIN_VISIBLE;
	assert(MAIN_VISIBLE <= length_of_list(s));
    } else
	slen = length_of_list(s);

    /* There will be this many columns of shortcuts */
    numcols = (slen + (slen % 2)) / 2;
Chris Allegretta's avatar
Chris Allegretta committed
562
563

    clear_bottomwin();
564

565
566
    for (i = 0; i < numcols; i++) {
	for (j = 0; j <= 1; j++) {
Chris Allegretta's avatar
Chris Allegretta committed
567

568
	    wmove(bottomwin, 1 + j, i * (COLS / numcols));
569

570
571
572
#ifndef NANO_SMALL
	    if (s->val == NANO_CONTROL_SPACE)
		strcpy(keystr, "^ ");
573
	    else
574
575
576
577
578
579
580
581
#endif /* !NANO_SMALL */
	    if (s->val > 0) {
		if (s->val < 64)
		    sprintf(keystr, "^%c", s->val + 64);
		else
		    sprintf(keystr, "M-%c", s->val - 32);
	    } else if (s->altval > 0)
		sprintf(keystr, "M-%c", s->altval);
582

583
	    onekey(keystr, s->desc, COLS / numcols);
Chris Allegretta's avatar
Chris Allegretta committed
584

585
586
587
588
	    s = s->next;
	    if (s == NULL)
		goto break_completely_out;
	}	
Chris Allegretta's avatar
Chris Allegretta committed
589
    }
590

Chris Allegretta's avatar
Chris Allegretta committed
591
  break_completely_out:
Chris Allegretta's avatar
Chris Allegretta committed
592
593
594
    wrefresh(bottomwin);
}

Chris Allegretta's avatar
Chris Allegretta committed
595
/* If modified is not already set, set it and update titlebar. */
Chris Allegretta's avatar
Chris Allegretta committed
596
597
598
599
void set_modified(void)
{
    if (!ISSET(MODIFIED)) {
	SET(MODIFIED);
Chris Allegretta's avatar
Chris Allegretta committed
600
	titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
601
602
603
604
	wrefresh(topwin);
    }
}

605
606
607
/* And so start the display update routines */
/* Given a column, this returns the "page" it is on  */
/* "page" in the case of the display columns, means which set of 80 */
608
/* characters is viewable (e.g.: page 1 shows from 1 to COLS) */
Chris Allegretta's avatar
Chris Allegretta committed
609
int get_page_from_virtual(int virtual)
Chris Allegretta's avatar
Chris Allegretta committed
610
{
611
    int page = 2;
Chris Allegretta's avatar
Chris Allegretta committed
612

Chris Allegretta's avatar
Chris Allegretta committed
613
614
    if (virtual <= COLS - 2)
	return 1;
615
    virtual -= (COLS - 2);
616
617

    while (virtual > COLS - 2 - 7) {
618
	virtual -= (COLS - 2 - 7);
619
	page++;
Chris Allegretta's avatar
Chris Allegretta committed
620
621
    }

622
623
624
    return page;
}

625
/* The inverse of the above function */
Chris Allegretta's avatar
Chris Allegretta committed
626
int get_page_start_virtual(int page)
Chris Allegretta's avatar
Chris Allegretta committed
627
{
628
629
    int virtual;
    virtual = --page * (COLS - 7);
Chris Allegretta's avatar
Chris Allegretta committed
630
631
    if (page)
	virtual -= 2 * page - 1;
632
    return virtual;
633
634
}

Chris Allegretta's avatar
Chris Allegretta committed
635
int get_page_end_virtual(int page)
Chris Allegretta's avatar
Chris Allegretta committed
636
{
637
638
639
    return get_page_start_virtual(page) + COLS - 1;
}

Chris Allegretta's avatar
Chris Allegretta committed
640
641
642
643
644
645
646
static int get_page_start(int column)
{
    assert(COLS > 9);
    return column < COLS - 1 ? 0 : column - 7 - (column - 8) % (COLS - 9);
}


647
#ifndef NANO_SMALL
648
649
650
/* This takes care of the case where there is a mark that covers only */
/* the current line. */

651
/* It expects a line with no tab characters (i.e.: the type that edit_add */
652
/* deals with */
Chris Allegretta's avatar
Chris Allegretta committed
653
654
void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
			 int virt_cur_x, int this_page)
655
{
656
657
658
    /*
     * The general idea is to break the line up into 3 sections: before
     * the mark, the mark, and after the mark.  We then paint each in
659
     * turn (for those that are currently visible, of course)
660
661
662
663
     *
     * 3 start points: 0 -> begin, begin->end, end->strlen(data)
     *    in data    :    pre          sel           post        
     */
Chris Allegretta's avatar
Chris Allegretta committed
664
    int this_page_start = get_page_start_virtual(this_page),
665
	this_page_end = get_page_end_virtual(this_page);
666
667

    /* likewise, 3 data lengths */
Chris Allegretta's avatar
Chris Allegretta committed
668
    int pre_data_len = begin, sel_data_len = end - begin, post_data_len = 0;	/* Determined from the other two */
669
670

    /* now fix the start locations & lengths according to the cursor's 
671
     * position (i.e.: our page) */
Chris Allegretta's avatar
Chris Allegretta committed
672
    if (pre_data_len < this_page_start)
673
674
675
	pre_data_len = 0;
    else
	pre_data_len -= this_page_start;
676

Chris Allegretta's avatar
Chris Allegretta committed
677
    if (begin < this_page_start)
678
679
	begin = this_page_start;

Chris Allegretta's avatar
Chris Allegretta committed
680
    if (end < this_page_start)
681
	end = this_page_start;
682

Chris Allegretta's avatar
Chris Allegretta committed
683
    if (begin > this_page_end)
684
	begin = this_page_end;
685

Chris Allegretta's avatar
Chris Allegretta committed
686
    if (end > this_page_end)
687
	end = this_page_end;
688

689
    /* Now calculate the lengths */
690
691
    sel_data_len = end - begin;
    post_data_len = this_page_end - end;
692
693

    wattron(edit, A_REVERSE);
694
    mvwaddnstr(edit, y, begin - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
695
	       &fileptr->data[begin], sel_data_len);
696

697
    wattroff(edit, A_REVERSE);
698

Chris Allegretta's avatar
Chris Allegretta committed
699
700
701
}
#endif

702
703
704
705
/* edit_add takes care of the job of actually painting a line into the
 * edit window.
 * 
 * Called only from update_line.  Expects a converted-to-not-have-tabs
706
707
 * line */
void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
708
	      int virt_mark_beginx, int this_page)
Chris Allegretta's avatar
Chris Allegretta committed
709
{
710

Chris Allegretta's avatar
Chris Allegretta committed
711
#ifdef ENABLE_COLOR
Chris Allegretta's avatar
Chris Allegretta committed
712
    const colortype *tmpcolor = NULL;
713
    int k, paintlen;
714
715
    filestruct *e, *s;
    regoff_t ematch, smatch;
716
717
718
#endif

    /* Just paint the string in any case (we'll add color or reverse on
Chris Allegretta's avatar
Chris Allegretta committed
719
       just the text that needs it */
720
    mvwaddnstr(edit, yval, 0, &fileptr->data[start],
Chris Allegretta's avatar
Chris Allegretta committed
721
	       get_page_end_virtual(this_page) - start + 1);
722

Chris Allegretta's avatar
Chris Allegretta committed
723
#ifdef ENABLE_COLOR
724
    if (colorstrings != NULL)
Chris Allegretta's avatar
Chris Allegretta committed
725
726
	for (tmpcolor = colorstrings; tmpcolor != NULL;
	     tmpcolor = tmpcolor->next) {
727

728
	    if (tmpcolor->end == NULL) {
729

730
731
		/* First, highlight all single-line regexes */
		k = start;
732
733
734
		regcomp(&color_regexp, tmpcolor->start, 0);
		while (!regexec(&color_regexp, &fileptr->data[k], 1,
				colormatches, 0)) {
735

736
		    if (colormatches[0].rm_eo - colormatches[0].rm_so < 1) {
737
			statusbar(_("Refusing 0 length regex match"));
738
739
			break;
		    }
740
#ifdef DEBUG
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
741
		    fprintf(stderr, _("Match! (%d chars) \"%s\"\n"),
742
743
			    colormatches[0].rm_eo - colormatches[0].rm_so,
			    &fileptr->data[k + colormatches[0].rm_so]);
744
#endif
745
		    if (colormatches[0].rm_so < COLS - 1) {
746
747
748
749
			if (tmpcolor->bright)
			    wattron(edit, A_BOLD);
			wattron(edit, COLOR_PAIR(tmpcolor->pairnum));

750
			if (colormatches[0].rm_eo + k <= COLS) {
751
			    paintlen =
752
				colormatches[0].rm_eo - colormatches[0].rm_so;
753
#ifdef DEBUG
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
754
			    fprintf(stderr, _("paintlen (%d) = eo (%d) - so (%d)\n"), 
755
756
757
758
759
				paintlen, colormatches[0].rm_eo, colormatches[0].rm_so);
#endif

			}
			else {
760
			    paintlen = COLS - k - colormatches[0].rm_so - 1;
761
#ifdef DEBUG
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
762
			    fprintf(stderr, _("paintlen (%d) = COLS (%d) - k (%d), - rm.so (%d) - 1\n"), 
763
764
765
					paintlen, COLS, k, colormatches[0].rm_so);
#endif
			}
766

767
768
			mvwaddnstr(edit, yval, colormatches[0].rm_so + k,
				   &fileptr->data[k + colormatches[0].rm_so],
769
770
771
				   paintlen);

		    }
772

773
774
775
		    if (tmpcolor->bright)
			wattroff(edit, A_BOLD);
		    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
776

777
		    k += colormatches[0].rm_eo;
778

Chris Allegretta's avatar
Chris Allegretta committed
779
		}
780
781
		regfree(&color_regexp);

782
783
784
785
786
787
788
789
790
791
	    }
	    /* Now, if there's an 'end' somewhere below, and a 'start'
	       somewhere above, things get really fun.  We have to look
	       down for an end, make sure there's not a start before 
	       the end after us, and then look up for a start, 
	       and see if there's an end after the start, before us :) */
	    else {

		s = fileptr;
		while (s != NULL) {
792
		    regcomp(&color_regexp, tmpcolor->start, 0);
793
		    if (!regexec
794
795
			(&color_regexp, s->data, 1, colormatches, 0)) {
			regfree(&color_regexp);
796
			break;
797
		    }
798
		    s = s->prev;
799
		    regfree(&color_regexp);
800
801
802
803
		}

		if (s != NULL) {
		    /* We found a start, mark it */
804
		    smatch = colormatches[0].rm_so;
805
806
807

		    e = s;
		    while (e != NULL && e != fileptr) {
808
			regcomp(&color_regexp, tmpcolor->end, 0);
809
			if (!regexec
810
811
			    (&color_regexp, e->data, 1, colormatches, 0)) {
			    regfree(&color_regexp);
812
			    break;
813
			}
814
			e = e->next;
815
			regfree(&color_regexp);
816
		    }
817

818
819
820
821
		    if (e != fileptr)
			continue;	/* There's an end before us */
		    else {	/* Keep looking for an end */
			while (e != NULL) {
822
			    regcomp(&color_regexp, tmpcolor->end, 0);
823
			    if (!regexec
824
				(&color_regexp, e->data, 1, colormatches,
825
826
				 0)) {
				regfree(&color_regexp);
827
				break;
828
			    }
829
			    e = e->next;
830
			    regfree(&color_regexp);
831
832
833
834
835
			}

			if (e == NULL)
			    continue;	/* There's no start before the end :) */
			else {	/* Okay, we found an end, mark it! */
836
			    ematch = colormatches[0].rm_eo;
837
838

			    while (e != NULL) {
839
				regcomp(&color_regexp, tmpcolor->end, 0);
840
				if (!regexec
841
				    (&color_regexp, e->data, 1,
842
843
				     colormatches, 0)) {
				    regfree(&color_regexp);
844
				    break;
845
846
				} e = e->next;
				regfree(&color_regexp);
847
848
849
850
851
852
853
854
855
856
857
858
859
			    }

			    if (e == NULL)
				continue;	/* No end, oh well :) */

			    /* Didn't find another end, we must be in the 
			       middle of a highlighted bit */

			    if (tmpcolor->bright)
				wattron(edit, A_BOLD);

			    wattron(edit, COLOR_PAIR(tmpcolor->pairnum));

860
			    if (s == fileptr && e == fileptr && ematch < COLS) {
861
862
863
				mvwaddnstr(edit, yval, start + smatch, 
					&fileptr->data[start + smatch],
					ematch - smatch);
864
#ifdef DEBUG
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
865
			fprintf(stderr, _("start = %d, smatch = %d, ematch = %d\n"), start,
866
867
868
869
				smatch, ematch);
#endif

		    	    } else if (s == fileptr)
870
871
872
873
874
875
				mvwaddnstr(edit, yval, start + smatch, 
					&fileptr->data[start + smatch],
					COLS - smatch);
			    else if (e == fileptr)
				mvwaddnstr(edit, yval, start, 
					&fileptr->data[start],
876
					COLS - start);
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
			    else
				mvwaddnstr(edit, yval, start, 
					&fileptr->data[start],
					COLS);

			    if (tmpcolor->bright)
				wattroff(edit, A_BOLD);

			    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));

			}

		    }

		    /* Else go to the next string, yahoo! =) */
		}
893
894

	    }
895

896
	}
897

Chris Allegretta's avatar
Chris Allegretta committed
898
#endif				/* ENABLE_COLOR */
Chris Allegretta's avatar
Chris Allegretta committed
899
#ifndef NANO_SMALL
900

901
    /* There are quite a few cases that could take place; we'll deal
902
     * with them each in turn */
Chris Allegretta's avatar
Chris Allegretta committed
903
    if (ISSET(MARK_ISSET) &&
904
	!((fileptr->lineno > mark_beginbuf->lineno
Chris Allegretta's avatar
Chris Allegretta committed
905
906
907
	   && fileptr->lineno > current->lineno)
	  || (fileptr->lineno < mark_beginbuf->lineno
	      && fileptr->lineno < current->lineno))) {
908
	/* If we get here we are on a line that is at least
909
910
911
912
913
	 * partially selected.  The lineno checks above determined
	 * that */
	if (fileptr != mark_beginbuf && fileptr != current) {
	    /* We are on a completely marked line, paint it all
	     * inverse */
Chris Allegretta's avatar
Chris Allegretta committed
914

915
	    wattron(edit, A_REVERSE);
916

917
	    mvwaddnstr(edit, yval, 0, fileptr->data, COLS);
918

919
	    wattroff(edit, A_REVERSE);
920

921
922
923
924
925
926
927
928
929
930
931
932
933
934
	} else if (fileptr == mark_beginbuf && fileptr == current) {
	    /* Special case, we're still on the same line we started
	     * marking -- so we call our helper function */
	    if (virt_cur_x < virt_mark_beginx) {
		/* To the right of us is marked */
		add_marked_sameline(virt_cur_x, virt_mark_beginx,
				    fileptr, yval, virt_cur_x, this_page);
	    } else {
		/* To the left of us is marked */
		add_marked_sameline(virt_mark_beginx, virt_cur_x,
				    fileptr, yval, virt_cur_x, this_page);
	    }
	} else if (fileptr == mark_beginbuf) {
	    /*
935
	     * We're updating the line that was first marked,
936
	     * but we're not currently on it.  So we want to
937
	     * figure out which half to invert based on our
938
939
	     * relative line numbers.
	     *
940
941
942
	     * I.e. if we're above the "beginbuf" line, we want to
	     * mark the left side.  Otherwise, we're below, so we
	     * mark the right.
943
944
945
	     */
	    int target;

946
	    if (mark_beginbuf->lineno > current->lineno) {
Chris Allegretta's avatar
Chris Allegretta committed
947

Chris Allegretta's avatar
Chris Allegretta committed
948
		wattron(edit, A_REVERSE);
949

950
		target =
Chris Allegretta's avatar
Chris Allegretta committed
951
952
		    (virt_mark_beginx <
		     COLS - 1) ? virt_mark_beginx : COLS - 1;
953

954
		mvwaddnstr(edit, yval, 0, fileptr->data, target);
955

956
		wattroff(edit, A_REVERSE);
957

958
959
960
	    }

	    if (mark_beginbuf->lineno < current->lineno) {
961

Chris Allegretta's avatar
Chris Allegretta committed
962
		wattron(edit, A_REVERSE);
963
		target = (COLS - 1) - virt_mark_beginx;
964

965
966
		if (target < 0)
		    target = 0;
967

968
		mvwaddnstr(edit, yval, virt_mark_beginx,
Chris Allegretta's avatar
Chris Allegretta committed
969
			   &fileptr->data[virt_mark_beginx], target);
970
971

		wattroff(edit, A_REVERSE);
972
	    }
973
974

	} else if (fileptr == current) {
975
	    /* We're on the cursor's line, but it's not the first
976
977
978
979
	     * one we marked.  Similar to the previous logic. */
	    int this_page_start = get_page_start_virtual(this_page),
		this_page_end = get_page_end_virtual(this_page);

980
981
	    if (mark_beginbuf->lineno < current->lineno) {

982
		wattron(edit, A_REVERSE);
983

984
985
		if (virt_cur_x > COLS - 2) {
		    mvwaddnstr(edit, yval, 0,
Chris Allegretta's avatar
Chris Allegretta committed
986
987
			       &fileptr->data[this_page_start],
			       virt_cur_x - this_page_start);
988
989
		} else
		    mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
990

991
		wattroff(edit, A_REVERSE);
992

993
994
995
	    }

	    if (mark_beginbuf->lineno > current->lineno) {
996

997
998
999
		wattron(edit, A_REVERSE);
		if (virt_cur_x > COLS - 2)
		    mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
1000
1001
			       &fileptr->data[virt_cur_x],
			       this_page_end - virt_cur_x);
1002
1003
		else
		    mvwaddnstr(edit, yval, virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
1004
1005
			       &fileptr->data[virt_cur_x],
			       COLS - virt_cur_x);
1006
1007

		wattroff(edit, A_REVERSE);
1008
1009

	    }
Chris Allegretta's avatar
Chris Allegretta committed
1010
	}
1011
1012
1013
    }
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1014
1015
1016
}

/*
1017
1018
 * Just update one line in the edit buffer.  Basically a wrapper for
 * edit_add
Chris Allegretta's avatar
Chris Allegretta committed
1019
 *
1020
 * index gives us a place in the string to update starting from.
Chris Allegretta's avatar
Chris Allegretta committed
1021
1022
1023
1024
1025
 * Likely args are current_x or 0.
 */
void update_line(filestruct * fileptr, int index)
{
    filestruct *filetmp;
1026
1027
1028
    int line = 0, col = 0;
    int virt_cur_x = current_x, virt_mark_beginx = mark_beginx;
    char *realdata, *tmp;
Chris Allegretta's avatar
Chris Allegretta committed
1029
    int i, pos, len, page;
Chris Allegretta's avatar
Chris Allegretta committed
1030

Chris Allegretta's avatar
Chris Allegretta committed
1031
1032
    if (!fileptr)
	return;
1033

1034
    /* First, blank out the line (at a minimum) */
Chris Allegretta's avatar
Chris Allegretta committed
1035
1036
1037
1038
1039
    for (filetmp = edittop; filetmp != fileptr && filetmp != editbot;
	 filetmp = filetmp->next)
	line++;

    mvwaddstr(edit, line, 0, hblank);
1040

1041
    /* Next, convert all the tabs to spaces, so everything else is easy */
1042
1043
1044
1045
    index = xpt(fileptr, index);

    realdata = fileptr->data;
    len = strlen(realdata);
1046
    fileptr->data = charalloc(xpt(fileptr, len) + 1);
1047
1048

    pos = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1049
1050
    for (i = 0; i < len; i++) {
	if (realdata[i] == '\t') {
1051
1052
	    do {
		fileptr->data[pos++] = ' ';
Chris Allegretta's avatar
Chris Allegretta committed
1053
1054
1055
1056
		if (i < current_x)
		    virt_cur_x++;
		if (i < mark_beginx)
		    virt_mark_beginx++;
1057
	    } while (pos % tabsize);
1058
	    /* must decrement once to account for tab-is-one-character */
Chris Allegretta's avatar
Chris Allegretta committed
1059
1060
1061
1062
	    if (i < current_x)
		virt_cur_x--;
	    if (i < mark_beginx)
		virt_mark_beginx--;
Chris Allegretta's avatar
Chris Allegretta committed
1063
	} else if (realdata[i] == 127) {
Chris Allegretta's avatar
Chris Allegretta committed
1064
	    /* Treat delete characters (ASCII 127's) as ^?'s */
Chris Allegretta's avatar
Chris Allegretta committed
1065
1066
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = '?';
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
	    if (i < current_x)
		virt_cur_x++;
	    if (i < mark_beginx)
		virt_mark_beginx++;
	} else if (realdata[i] == 10) {
	    /* Treat newlines (ASCII 10's) embedded in a line as encoded
	       nulls (ASCII 0's); the line in question should be run
	       through unsunder() before reaching here */
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = '@';
	    if (i < current_x)
		virt_cur_x++;
	    if (i < mark_beginx)
		virt_mark_beginx++;
Chris Allegretta's avatar
Chris Allegretta committed
1081
1082
1083
1084
1085
1086
1087
1088
	} else if (is_cntrl_char(realdata[i])) {
	    /* Treat control characters as ^symbol's */
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = realdata[i] + 64;
	    if (i < current_x)
		virt_cur_x++;
	    if (i < mark_beginx)
		virt_mark_beginx++;
1089
1090
1091
1092
1093
1094
	} else {
	    fileptr->data[pos++] = realdata[i];
	}
    }

    fileptr->data[pos] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
1095

Chris Allegretta's avatar
Chris Allegretta committed
1096
    /* Now, paint the line */
1097
    if (current == fileptr && index > COLS - 2) {
1098
	/* This handles when the current line is beyond COLS */
1099
	/* It requires figuring out what page we're on      */
1100
	page = get_page_from_virtual(index);
1101
	col = get_page_start_virtual(page);
Chris Allegretta's avatar
Chris Allegretta committed
1102

1103
	edit_add(filetmp, line, col, virt_cur_x, virt_mark_beginx, page);
Chris Allegretta's avatar
Chris Allegretta committed
1104
1105
	mvwaddch(edit, line, 0, '$');

Chris Allegretta's avatar
Chris Allegretta committed
1106
	if (strlenpt(fileptr->data) > get_page_end_virtual(page) + 1)
Chris Allegretta's avatar
Chris Allegretta committed
1107
	    mvwaddch(edit, line, COLS - 1, '$');
1108
    } else {
1109
1110
1111
	/* It's not the current line means that it's at x=0 and page=1 */
	/* If it is the current line, then we're in the same boat      */
	edit_add(filetmp, line, 0, virt_cur_x, virt_mark_beginx, 1);
Chris Allegretta's avatar
Chris Allegretta committed
1112

1113
	if (strlenpt(&filetmp->data[col]) > COLS)
Chris Allegretta's avatar
Chris Allegretta committed
1114
	    mvwaddch(edit, line, COLS - 1, '$');
1115
    }
1116
1117
1118
1119
1120

    /* Clean up our mess */
    tmp = fileptr->data;
    fileptr->data = realdata;
    free(tmp);
Chris Allegretta's avatar
Chris Allegretta committed
1121
1122
1123
1124
1125
1126
1127
1128
}

void center_cursor(void)
{
    current_y = editwinrows / 2;
    wmove(edit, current_y, current_x);
}

Chris Allegretta's avatar
Chris Allegretta committed
1129
/* Refresh the screen without changing the position of lines. */
Chris Allegretta's avatar
Chris Allegretta committed
1130
1131
void edit_refresh(void)
{
Chris Allegretta's avatar
Chris Allegretta committed
1132
    static int noloop = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1133
    int nlines = 0, currentcheck = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1134

Chris Allegretta's avatar
Chris Allegretta committed
1135
1136
1137
    /* Neither of these conditions should occur, but they do.  edittop is
     * NULL when you open an existing file on the command line, and
     * ENABLE_COLOR is defined.  Yuck. */
Chris Allegretta's avatar
Chris Allegretta committed
1138
1139
    if (current == NULL)
	return;
Chris Allegretta's avatar
Chris Allegretta committed
1140
1141
    if (edittop == NULL)
	edittop = current;
Chris Allegretta's avatar
Chris Allegretta committed
1142

Chris Allegretta's avatar
Chris Allegretta committed
1143
1144
1145
1146
    editbot = edittop;
    while (nlines < editwinrows) {
	update_line(editbot, current_x);
	if (editbot == current)
1147
1148
	    currentcheck = 1;

1149
	nlines++;
Chris Allegretta's avatar
Chris Allegretta committed
1150
1151
1152
1153

	if (editbot->next == NULL)
	    break;
	editbot = editbot->next;
Chris Allegretta's avatar
Chris Allegretta committed
1154
    }
Chris Allegretta's avatar
Chris Allegretta committed
1155
1156
    /* If noloop == 1, then we already did an edit_update without finishing
       this function.  So we don't run edit_update again */
Chris Allegretta's avatar
Chris Allegretta committed
1157
1158
    if (!currentcheck && !noloop) {
		/* Then current has run off the screen... */
1159
	edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
1160
	noloop = 1;
1161
    } else if (noloop)
Chris Allegretta's avatar
Chris Allegretta committed
1162
	noloop = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1163

Chris Allegretta's avatar
Chris Allegretta committed
1164
1165
1166
1167
    while (nlines < editwinrows) {
	mvwaddstr(edit, nlines, 0, hblank);
	nlines++;
    }
1168
1169

    /* What the hell are we expecting to update the screen if this isn't 
Chris Allegretta's avatar
Chris Allegretta committed
1170
       here? Luck?? */
1171
    wrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
1172
1173
}

1174
/*
1175
 * Same as above, but touch the window first, so everything is redrawn.
1176
1177
1178
1179
1180
1181
1182
1183
 */
void edit_refresh_clearok(void)
{
    clearok(edit, TRUE);
    edit_refresh();
    clearok(edit, FALSE);
}

Chris Allegretta's avatar
Chris Allegretta committed
1184
/*
1185
 * Nice generic routine to update the edit buffer, given a pointer to the
Chris Allegretta's avatar
Chris Allegretta committed
1186
1187
 * file struct =) 
 */
Chris Allegretta's avatar
Chris Allegretta committed
1188
void edit_update(filestruct *fileptr, topmidbotnone location)
Chris Allegretta's avatar
Chris Allegretta committed
1189
1190
1191
1192
{
    if (fileptr == NULL)
	return;

Chris Allegretta's avatar
Chris Allegretta committed
1193
1194
    if (location != TOP) {
	int goal = location == NONE ? current_y - 1 : editwinrows / 2;
Chris Allegretta's avatar
Chris Allegretta committed
1195

Chris Allegretta's avatar
Chris Allegretta committed
1196
1197
1198
1199
	for (; goal >= 0 && fileptr->prev != NULL; goal--)
	    fileptr = fileptr->prev;
    }
    edittop = fileptr;
1200
    fix_editbot();
Chris Allegretta's avatar
Chris Allegretta committed
1201
1202
1203
1204

    edit_refresh();
}

Chris Allegretta's avatar
Chris Allegretta committed
1205
1206
/* This function updates current, based on where current_y is;
 * reset_cursor() does the opposite. */
Chris Allegretta's avatar
Chris Allegretta committed
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
void update_cursor(void)
{
    int i = 0;

#ifdef DEBUG
    fprintf(stderr, _("Moved to (%d, %d) in edit buffer\n"), current_y,
	    current_x);
#endif

    current = edittop;
Chris Allegretta's avatar
Chris Allegretta committed
1217
    while (i < current_y && current->next != NULL) {
Chris Allegretta's avatar
Chris Allegretta committed
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
	current = current->next;
	i++;
    }

#ifdef DEBUG
    fprintf(stderr, _("current->data = \"%s\"\n"), current->data);
#endif
}

/*
 * Ask a question on the statusbar.  Answer will be stored in answer
 * global.  Returns -1 on aborted enter, -2 on a blank string, and 0
1230
 * otherwise, the valid shortcut key caught.  Def is any editable text we
Chris Allegretta's avatar
Chris Allegretta committed
1231
 * want to put up by default.
1232
1233
 *
 * New arg tabs tells whether or not to allow tab completion.
Chris Allegretta's avatar
Chris Allegretta committed
1234
 */
Chris Allegretta's avatar
Chris Allegretta committed
1235
1236
int statusq(int tabs, const shortcut *s, const char *def,
		const char *msg, ...)
Chris Allegretta's avatar
Chris Allegretta committed
1237
1238
{
    va_list ap;
Chris Allegretta's avatar
Chris Allegretta committed
1239
    char *foo = charalloc(COLS - 3);
1240
    int ret;
1241
#ifndef DISABLE_TABCOMP
1242
    int list = 0;
1243
1244
#endif

1245
    bottombars(s);
Chris Allegretta's avatar
Chris Allegretta committed
1246
1247

    va_start(ap, msg);
Chris Allegretta's avatar
Chris Allegretta committed
1248
    vsnprintf(foo, COLS - 4, msg, ap);
Chris Allegretta's avatar
Chris Allegretta committed
1249
    va_end(ap);
Chris Allegretta's avatar
Chris Allegretta committed
1250
    foo[COLS - 4] = '\0';
1251

1252
#ifndef DISABLE_TABCOMP
Chris Allegretta's avatar
Chris Allegretta committed
1253
    ret = nanogetstr(tabs, foo, def, s, &list);
1254
#else
Chris Allegretta's avatar
Chris Allegretta committed
1255
    ret = nanogetstr(tabs, foo, def, s);
1256
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1257
    free(foo);
Chris Allegretta's avatar
Chris Allegretta committed
1258
1259
1260
1261
1262
1263
1264
1265
1266

    switch (ret) {
    case NANO_FIRSTLINE_KEY:
	do_first_line();
	break;
    case NANO_LASTLINE_KEY:
	do_last_line();
	break;
    case NANO_CANCEL_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
1267
1268
	ret = -1;
	break;
Chris Allegretta's avatar
Chris Allegretta committed
1269
    default:
1270
	blank_statusbar();
Chris Allegretta's avatar
Chris Allegretta committed
1271
1272
1273
1274
1275
1276
    }

#ifdef DEBUG
    fprintf(stderr, _("I got \"%s\"\n"), answer);
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1277
1278
1279
1280
1281
1282
1283
1284
#ifndef DISABLE_TABCOMP
	/* if we've done tab completion, there might be a list of
	   filename matches on the edit window at this point; make sure
	   they're cleared off */
	if (list)
	    edit_refresh();
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1285
1286
1287
1288
1289
1290
1291
    return ret;
}

/*
 * Ask a simple yes/no question on the statusbar.  Returns 1 for Y, 0 for
 * N, 2 for All (if all is non-zero when passed in) and -1 for abort (^C)
 */
Chris Allegretta's avatar
Chris Allegretta committed
1292
int do_yesno(int all, int leavecursor, const char *msg, ...)
Chris Allegretta's avatar
Chris Allegretta committed
1293
1294
1295
{
    va_list ap;
    char foo[133];
1296
    int kbinput, ok = -1, i;
Chris Allegretta's avatar
Chris Allegretta committed
1297
1298
1299
    const char *yesstr;		/* String of yes characters accepted */
    const char *nostr;		/* Same for no */
    const char *allstr;		/* And all, surprise! */
1300
#ifndef DISABLE_MOUSE
Chris Allegretta's avatar
Chris Allegretta committed
1301
1302
1303
1304
1305
#ifdef NCURSES_MOUSE_VERSION
    MEVENT mevent;
#endif
#endif

1306
    /* Yes, no and all are strings of any length.  Each string consists of
Chris Allegretta's avatar
Chris Allegretta committed
1307
1308
       all characters accepted as a valid character for that value.
       The first value will be the one displayed in the shortcuts. */
1309
1310
1311
    yesstr = _("Yy");
    nostr = _("Nn");
    allstr = _("Aa");
Chris Allegretta's avatar
Chris Allegretta committed
1312
1313
1314

    /* Write the bottom of the screen */
    clear_bottomwin();
1315

Jordi Mallach's avatar
   
Jordi Mallach committed
1316
    /* Remove gettext call for keybindings until we clear the thing up */
Chris Allegretta's avatar
Chris Allegretta committed
1317
    if (!ISSET(NO_HELP)) {
1318
1319
	char shortstr[3];		/* Temp string for Y, N, A */

Chris Allegretta's avatar
Chris Allegretta committed
1320
	wmove(bottomwin, 1, 0);
1321

1322
	sprintf(shortstr, " %c", yesstr[0]);
1323
	onekey(shortstr, _("Yes"), 16);
1324
1325

	if (all) {
1326
	    shortstr[1] = allstr[0];
1327
	    onekey(shortstr, _("All"), 16);
1328
	}
Chris Allegretta's avatar
Chris Allegretta committed
1329
	wmove(bottomwin, 2, 0);
1330

1331
	shortstr[1] = nostr[0];
1332
	onekey(shortstr, _("No"), 16);
1333

1334
	onekey("^C", _("Cancel"), 16);
Chris Allegretta's avatar
Chris Allegretta committed
1335
1336
1337
1338
    }
    va_start(ap, msg);
    vsnprintf(foo, 132, msg, ap);
    va_end(ap);
1339

Chris Allegretta's avatar
Chris Allegretta committed
1340
    wattron(bottomwin, A_REVERSE);
1341
1342

    blank_statusbar();
Chris Allegretta's avatar
Chris Allegretta committed
1343
    mvwaddstr(bottomwin, 0, 0, foo);
1344

Chris Allegretta's avatar
Chris Allegretta committed
1345
    wattroff(bottomwin, A_REVERSE);
1346

Chris Allegretta's avatar
Chris Allegretta committed
1347
1348
1349
1350
1351
1352
1353
1354
1355
    wrefresh(bottomwin);

    if (leavecursor == 1)
	reset_cursor();

    while (ok == -1) {
	kbinput = wgetch(edit);

	switch (kbinput) {
1356
#ifndef DISABLE_MOUSE
Chris Allegretta's avatar
Chris Allegretta committed
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
#ifdef NCURSES_MOUSE_VERSION
	case KEY_MOUSE:

	    /* Look ma!  We get to duplicate lots of code from do_mouse!! */
	    if (getmouse(&mevent) == ERR)
		break;
	    if (!wenclose(bottomwin, mevent.y, mevent.x) || ISSET(NO_HELP))
		break;
	    mevent.y -= editwinrows + 3;
	    if (mevent.y < 0)
		break;
	    else {

		/* Rather than a bunch of if statements, set up a matrix
Chris Allegretta's avatar
Chris Allegretta committed
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
		   of possible return keystrokes based on the x and y
		   values */ 
		char yesnosquare[2][2];
		yesnosquare[0][0] = yesstr[0];
		if (all)
		    yesnosquare[0][1] = allstr[0];
		else
		    yesnosquare[0][1] = '\0';
		yesnosquare[1][0] = nostr[0];
		yesnosquare[1][1] = NANO_CONTROL_C;
		ungetch(yesnosquare[mevent.y][mevent.x / (COLS / 6)]);
Chris Allegretta's avatar
Chris Allegretta committed
1382
1383
1384
1385
	    }
	    break;
#endif
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1386
1387
1388
	case NANO_CONTROL_C:
	    ok = -2;
	    break;
1389
1390
	default:

1391
	    /* Look for the kbinput in the yes, no and (optimally) all str */
Chris Allegretta's avatar
Chris Allegretta committed
1392
	    for (i = 0; yesstr[i] != 0 && yesstr[i] != kbinput; i++);
1393
	    if (yesstr[i] != 0) {
1394
		ok = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1395
		break;
1396
1397
	    }

Chris Allegretta's avatar
Chris Allegretta committed
1398
	    for (i = 0; nostr[i] != 0 && nostr[i] != kbinput; i++);
1399
1400
	    if (nostr[i] != 0) {
		ok = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1401
		break;
1402
1403
1404
	    }

	    if (all) {
Chris Allegretta's avatar
Chris Allegretta committed
1405
		for (i = 0; allstr[i] != 0 && allstr[i] != kbinput; i++);
1406
1407
		if (allstr[i] != 0) {
		    ok = 2;
Chris Allegretta's avatar
Chris Allegretta committed
1408
		    break;
1409
1410
		}
	    }
Chris Allegretta's avatar
Chris Allegretta committed
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
	}
    }

    /* Then blank the screen */
    blank_statusbar_refresh();

    if (ok == -2)
	return -1;
    else
	return ok;
}

Chris Allegretta's avatar
Chris Allegretta committed
1423
void statusbar(const char *msg, ...)
Chris Allegretta's avatar
Chris Allegretta committed
1424
1425
{
    va_list ap;
Chris Allegretta's avatar
Chris Allegretta committed
1426
    char *foo;
Chris Allegretta's avatar
Chris Allegretta committed
1427
    int start_x = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1428
1429
1430
1431
    size_t foo_len;

    assert(COLS >= 4);
    foo = charalloc(COLS - 3);
Chris Allegretta's avatar
Chris Allegretta committed
1432
1433

    va_start(ap, msg);
Chris Allegretta's avatar
Chris Allegretta committed
1434
    vsnprintf(foo, COLS - 3, msg, ap);
Chris Allegretta's avatar
Chris Allegretta committed
1435
1436
    va_end(ap);

Chris Allegretta's avatar
Chris Allegretta committed
1437
1438
1439
    foo[COLS - 4] = '\0';
    foo_len = strlen(foo);
    start_x = (COLS - foo_len - 4) / 2;
Chris Allegretta's avatar
Chris Allegretta committed
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449

    /* Blank out line */
    blank_statusbar();

    wmove(bottomwin, 0, start_x);

    wattron(bottomwin, A_REVERSE);

    waddstr(bottomwin, "[ ");
    waddstr(bottomwin, foo);
Chris Allegretta's avatar
Chris Allegretta committed
1450
    free(foo);
Chris Allegretta's avatar
Chris Allegretta committed
1451
    waddstr(bottomwin, " ]");
1452

Chris Allegretta's avatar
Chris Allegretta committed
1453
    wattroff(bottomwin, A_REVERSE);
1454

Chris Allegretta's avatar
Chris Allegretta committed
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
    wrefresh(bottomwin);

    if (ISSET(CONSTUPDATE))
	statblank = 1;
    else
	statblank = 25;
}

void display_main_list(void)
{
1465
    bottombars(main_list);
Chris Allegretta's avatar
Chris Allegretta committed
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
}

int total_refresh(void)
{
    clearok(edit, TRUE);
    clearok(topwin, TRUE);
    clearok(bottomwin, TRUE);
    wnoutrefresh(edit);
    wnoutrefresh(topwin);
    wnoutrefresh(bottomwin);
    doupdate();
    clearok(edit, FALSE);
    clearok(topwin, FALSE);
    clearok(bottomwin, FALSE);
1480
    edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
1481
    titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
1482
1483
1484
    return 1;
}

1485
int do_cursorpos(int constant)
Chris Allegretta's avatar
Chris Allegretta committed
1486
1487
{
    filestruct *fileptr;
1488
    float linepct = 0.0, bytepct = 0.0, colpct = 0.0;
1489
    long i = 0, j = 0;
1490
    static long old_i = -1, old_totsize = -1;
Chris Allegretta's avatar
Chris Allegretta committed
1491
1492
1493
1494

    if (current == NULL || fileage == NULL)
	return 0;

1495
1496
1497
1498
1499
1500
    if (old_i == -1)
	old_i = i;

    if (old_totsize == -1)
	old_totsize = totsize;

Chris Allegretta's avatar
Chris Allegretta committed
1501
    colpct = 100 * (xplustabs() + 1) / (strlenpt(current->data) + 1);
Chris Allegretta's avatar
Chris Allegretta committed
1502

1503
1504
1505
    for (fileptr = fileage; fileptr != current && fileptr != NULL;
	 fileptr = fileptr->next)
	i += strlen(fileptr->data) + 1;
1506

1507
1508
    if (fileptr == NULL)
	return -1;
1509

1510
    i += current_x;
1511

1512
    j = totsize;
1513

1514
1515
    if (totsize > 0)
	bytepct = 100 * i / totsize;
1516
1517
1518

    if (totlines > 0)
	 linepct = 100 * current->lineno / totlines;
Chris Allegretta's avatar
Chris Allegretta committed
1519
1520
1521
1522
1523
1524

#ifdef DEBUG
    fprintf(stderr, _("do_cursorpos: linepct = %f, bytepct = %f\n"),
	    linepct, bytepct);
#endif

1525
1526
1527
1528
    /* if constant is zero, display the position on the statusbar
       unconditionally; otherwise, only display the position when the
       character values have changed */
    if (!constant || (old_i != i || old_totsize != totsize)) {
Chris Allegretta's avatar
Chris Allegretta committed
1529
	statusbar(_
1530
		  ("line %d/%d (%.0f%%), col %ld/%ld (%.0f%%), char %ld/%ld (%.0f%%)"),
1531
		  current->lineno, totlines, linepct, xplustabs() + 1, 
Chris Allegretta's avatar
Chris Allegretta committed
1532
		  strlenpt(current->data) + 1, colpct, i, j, bytepct);
1533
1534
1535
1536
1537
    }

    old_i = i;
    old_totsize = totsize;

Chris Allegretta's avatar
Chris Allegretta committed
1538
1539
1540
1541
    reset_cursor();
    return 1;
}

1542
1543
1544
1545
1546
int do_cursorpos_void(void)
{
    return do_cursorpos(0);
}

1547
1548
/* Our shortcut-list-compliant help function, which is
 * better than nothing, and dynamic! */
Chris Allegretta's avatar
Chris Allegretta committed
1549
1550
int do_help(void)
{
1551
#ifndef DISABLE_HELP
1552
    int i, j, row = 0, page = 1, kbinput = 0, no_more = 0, kp, kp2;
Chris Allegretta's avatar
Chris Allegretta committed
1553
    int no_help_flag = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1554
    const shortcut *oldshortcut;
Chris Allegretta's avatar
Chris Allegretta committed
1555
1556
1557

    blank_edit();
    curs_set(0);
1558
    wattroff(bottomwin, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1559
1560
    blank_statusbar();

1561
    /* set help_text as the string to display */
1562
    help_init();
1563
    assert(help_text != NULL);
1564
1565
1566

    oldshortcut = currshortcut;

1567
    currshortcut = help_list;
1568

1569
    kp = keypad_on(edit, 1);
1570
    kp2 = keypad_on(bottomwin, 1);
1571

Chris Allegretta's avatar
Chris Allegretta committed
1572
1573
    if (ISSET(NO_HELP)) {

1574
	/* Well, if we're going to do this, we should at least
1575
	   do it the right way */
Chris Allegretta's avatar
Chris Allegretta committed
1576
	no_help_flag = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1577
	UNSET(NO_HELP);
1578
	window_init();
1579
	bottombars(help_list);
1580

Chris Allegretta's avatar
Chris Allegretta committed
1581
    } else
1582
	bottombars(help_list);
Chris Allegretta's avatar
Chris Allegretta committed
1583
1584

    do {
1585
1586
	const char *ptr = help_text;

Chris Allegretta's avatar
Chris Allegretta committed
1587
	switch (kbinput) {
1588
#ifndef DISABLE_MOUSE
1589
#ifdef NCURSES_MOUSE_VERSION
Chris Allegretta's avatar
Chris Allegretta committed
1590
1591
1592
	case KEY_MOUSE:
	    do_mouse();
	    break;
1593
1594
#endif
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
	case 27:
	    kbinput = wgetch(edit);
	    switch(kbinput) {
	    case '[':
		kbinput = wgetch(edit);
		switch(kbinput) {
		    case '5':	/* Alt-[-5 = Page Up */
			wgetch(edit);
			goto do_pageupkey;
			break;
		    case 'V':	/* Alt-[-V = Page Up in Hurd Console */
		    case 'I':	/* Alt-[-I = Page Up - FreeBSD Console */
			goto do_pageupkey;
			break;
		    case '6':	/* Alt-[-6 = Page Down */
			wgetch(edit);
			goto do_pagedownkey;
			break;
		    case 'U':	/* Alt-[-U = Page Down in Hurd Console */
		    case 'G':	/* Alt-[-G = Page Down - FreeBSD Console */
			goto do_pagedownkey;
			break;
		}
		break;
	    }
	    break;
Chris Allegretta's avatar
Chris Allegretta committed
1621
1622
1623
	case NANO_NEXTPAGE_KEY:
	case NANO_NEXTPAGE_FKEY:
	case KEY_NPAGE:
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1624
	  do_pagedownkey:
Chris Allegretta's avatar
Chris Allegretta committed
1625
1626
1627
1628
1629
1630
1631
1632
	    if (!no_more) {
		blank_edit();
		page++;
	    }
	    break;
	case NANO_PREVPAGE_KEY:
	case NANO_PREVPAGE_FKEY:
	case KEY_PPAGE:
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1633
	  do_pageupkey:
Chris Allegretta's avatar
Chris Allegretta committed
1634
1635
1636
1637
1638
1639
1640
1641
	    if (page > 1) {
		no_more = 0;
		blank_edit();
		page--;
	    }
	    break;
	}

1642
	/* Calculate where in the text we should be, based on the page */
Chris Allegretta's avatar
Chris Allegretta committed
1643
1644
1645
	for (i = 1; i < page; i++) {
	    row = 0;
	    j = 0;
1646
1647

	    while (row < editwinrows - 2 && *ptr != '\0') {
Chris Allegretta's avatar
Chris Allegretta committed
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
		if (*ptr == '\n' || j == COLS - 5) {
		    j = 0;
		    row++;
		}
		ptr++;
		j++;
	    }
	}

	i = 0;
	j = 0;
	while (i < editwinrows && *ptr != '\0') {
1660
	    const char *end = ptr;
Chris Allegretta's avatar
Chris Allegretta committed
1661
1662
1663
1664
1665
1666
	    while (*end != '\n' && *end != '\0' && j != COLS - 5) {
		end++;
		j++;
	    }
	    if (j == COLS - 5) {

1667
		/* Don't print half a word if we've run out of space */
Chris Allegretta's avatar
Chris Allegretta committed
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
		while (*end != ' ' && *end != '\0') {
		    end--;
		    j--;
		}
	    }
	    mvwaddnstr(edit, i, 0, ptr, j);
	    j = 0;
	    i++;
	    if (*end == '\n')
		end++;
	    ptr = end;
	}
	if (*ptr == '\0') {
	    no_more = 1;
	    continue;
	}
Chris Allegretta's avatar
Chris Allegretta committed
1684
1685
    } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY &&
	     kbinput != NANO_EXIT_FKEY);
Chris Allegretta's avatar
Chris Allegretta committed
1686

1687
1688
    currshortcut = oldshortcut;

Chris Allegretta's avatar
Chris Allegretta committed
1689
    if (no_help_flag) {
1690
	blank_bottombars();
Chris Allegretta's avatar
Chris Allegretta committed
1691
	wrefresh(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
1692
	SET(NO_HELP);
1693
	window_init();
Chris Allegretta's avatar
Chris Allegretta committed
1694
    } else
1695
	bottombars(currshortcut);
Chris Allegretta's avatar
Chris Allegretta committed
1696
1697
1698

    curs_set(1);
    edit_refresh();
1699
    kp = keypad_on(edit, kp);
1700
    kp2 = keypad_on(bottomwin, kp2);
1701

1702
1703
1704
1705
1706
    /* The help_init() at the beginning allocated help_text, which has
       now been written to screen. */
    free(help_text);
    help_text = NULL;

Chris Allegretta's avatar
Chris Allegretta committed
1707
1708
1709
1710
#elif defined(DISABLE_HELP)
    nano_disabled_msg();
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1711
1712
1713
1714
    return 1;
}

#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
1715
1716
/* Dump the current file structure to stderr */
void dump_buffer(const filestruct *inptr) {
Chris Allegretta's avatar
Chris Allegretta committed
1717
1718
1719
1720
1721
1722
1723
    if (inptr == fileage)
	fprintf(stderr, _("Dumping file buffer to stderr...\n"));
    else if (inptr == cutbuffer)
	fprintf(stderr, _("Dumping cutbuffer to stderr...\n"));
    else
	fprintf(stderr, _("Dumping a buffer to stderr...\n"));

Chris Allegretta's avatar
Chris Allegretta committed
1724
1725
1726
    while (inptr != NULL) {
	fprintf(stderr, "(%d) %s\n", inptr->lineno, inptr->data);
	inptr = inptr->next;
Chris Allegretta's avatar
Chris Allegretta committed
1727
1728
    }
}
Chris Allegretta's avatar
Chris Allegretta committed
1729
#endif /* DEBUG */
Chris Allegretta's avatar
Chris Allegretta committed
1730
1731

#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
1732
1733
void dump_buffer_reverse(void) {
    const filestruct *fileptr = filebot;
Chris Allegretta's avatar
Chris Allegretta committed
1734
1735

    while (fileptr != NULL) {
1736
	fprintf(stderr, "(%d) %s\n", fileptr->lineno, fileptr->data);
Chris Allegretta's avatar
Chris Allegretta committed
1737
1738
1739
	fileptr = fileptr->prev;
    }
}
Chris Allegretta's avatar
Chris Allegretta committed
1740
#endif /* DEBUG */
Robert Siemborski's avatar
Robert Siemborski committed
1741

1742
/* Fix editbot, based on the assumption that edittop is correct */
1743
1744
void fix_editbot(void)
{
Robert Siemborski's avatar
Robert Siemborski committed
1745
    int i;
Chris Allegretta's avatar
Chris Allegretta committed
1746

Robert Siemborski's avatar
Robert Siemborski committed
1747
    editbot = edittop;
Chris Allegretta's avatar
Chris Allegretta committed
1748
1749
    for (i = 0; i < editwinrows && editbot->next != NULL; i++)
	editbot = editbot->next;
Robert Siemborski's avatar
Robert Siemborski committed
1750
}
1751

Chris Allegretta's avatar
Chris Allegretta committed
1752
/* highlight the current word being replaced or spell checked */
Chris Allegretta's avatar
Chris Allegretta committed
1753
void do_replace_highlight(int highlight_flag, const char *word)
Chris Allegretta's avatar
Chris Allegretta committed
1754
1755
{
    char *highlight_word = NULL;
Chris Allegretta's avatar
Chris Allegretta committed
1756
    int x, y, word_len;
Chris Allegretta's avatar
Chris Allegretta committed
1757

Chris Allegretta's avatar
Chris Allegretta committed
1758
1759
    highlight_word =
	mallocstrcpy(highlight_word, &current->data[current_x]);
Chris Allegretta's avatar
Chris Allegretta committed
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770

#ifdef HAVE_REGEX_H
    if (ISSET(USE_REGEXP))
	/* if we're using regexps, the highlight is the length of the
	   search result, not the length of the regexp string */
	word_len = regmatches[0].rm_eo - regmatches[0].rm_so;
    else
#endif
	word_len = strlen(word);

    highlight_word[word_len] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
1771

1772
    /* adjust output when word extends beyond screen */
Chris Allegretta's avatar
Chris Allegretta committed
1773
1774

    x = xplustabs();
Chris Allegretta's avatar
Chris Allegretta committed
1775
    y = get_page_start(x) + COLS;
Chris Allegretta's avatar
Chris Allegretta committed
1776

Chris Allegretta's avatar
Chris Allegretta committed
1777
    if ((COLS - (y - x) + word_len) > COLS) {
Chris Allegretta's avatar
Chris Allegretta committed
1778
1779
1780
1781
1782
1783
1784
	highlight_word[y - x - 1] = '$';
	highlight_word[y - x] = '\0';
    }

    /* OK display the output */

    reset_cursor();
Chris Allegretta's avatar
Chris Allegretta committed
1785

Chris Allegretta's avatar
Chris Allegretta committed
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
    if (highlight_flag)
	wattron(edit, A_REVERSE);

    waddstr(edit, highlight_word);

    if (highlight_flag)
	wattroff(edit, A_REVERSE);

    free(highlight_word);
}

1797
#ifdef NANO_EXTRA
1798
#define CREDIT_LEN 52
1799
1800
void do_credits(void)
{
Chris Allegretta's avatar
Chris Allegretta committed
1801
    int i, j = 0, k, place = 0, start_x;
Chris Allegretta's avatar
Chris Allegretta committed
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
    const char *what;
    const char *nanotext = _("The nano text editor");
    const char *version = _("version ");
    const char *brought = _("Brought to you by:");
    const char *specialthx = _("Special thanks to:");
    const char *fsf = _("The Free Software Foundation");
    const char *ncurses = _("For ncurses:");
    const char *anyonelse = _("and anyone else we forgot...");
    const char *thankyou = _("Thank you for using nano!\n");

    const char *credits[CREDIT_LEN] = { nanotext,
Chris Allegretta's avatar
Chris Allegretta committed
1813
1814
1815
1816
1817
1818
1819
1820
1821
	version,
	VERSION,
	"",
	brought,
	"Chris Allegretta",
	"Jordi Mallach",
	"Adam Rogoyski",
	"Rob Siemborski",
	"Rocco Corsi",
1822
	"David Lawrence Ramsey",
Chris Allegretta's avatar
Chris Allegretta committed
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
	"Ken Tyler",
	"Sven Guckes",
	"Florian Knig",
	"Pauli Virtanen",
	"Daniele Medri",
	"Clement Laforet",
	"Tedi Heriyanto",
	"Bill Soudan",
	"Christian Weisgerber",
	"Erik Andersen",
	"Big Gaute",
	"Joshua Jensen",
	"Ryan Krebs",
	"Albert Chin",
	"",
	specialthx,
	"Plattsburgh State University",
	"Benet Laboratories",
	"Amy Allegretta",
	"Linda Young",
	"Jeremy Robichaud",
	"Richard Kolb II",
	fsf,
	"Linus Torvalds",
	ncurses,
1848
1849
1850
1851
	"Thomas Dickey",
	"Pavel Curtis",
	"Zeyd Ben-Halim",
	"Eric S. Raymond",
Chris Allegretta's avatar
Chris Allegretta committed
1852
1853
1854
1855
1856
1857
	anyonelse,
	thankyou,
	"", "", "", "",
	"(c) 1999-2002 Chris Allegretta",
	"", "", "", "",
	"www.nano-editor.org"
1858
1859
1860
1861
1862
1863
    };

    curs_set(0);
    nodelay(edit, TRUE);
    blank_bottombars();
    mvwaddstr(topwin, 0, 0, hblank);
Chris Allegretta's avatar
Chris Allegretta committed
1864
1865
    blank_edit();
    wrefresh(edit);
1866
1867
1868
1869
    wrefresh(bottomwin);
    wrefresh(topwin);

    while (wgetch(edit) == ERR) {
Chris Allegretta's avatar
Chris Allegretta committed
1870
1871
	for (k = 0; k <= 1; k++) {
	    blank_edit();
Chris Allegretta's avatar
Chris Allegretta committed
1872
1873
	    for (i = editwinrows / 2 - 1; i >= (editwinrows / 2 - 1 - j);
		 i--) {
Chris Allegretta's avatar
Chris Allegretta committed
1874
1875
1876
1877
1878
1879
1880
		mvwaddstr(edit, i * 2 - k, 0, hblank);

		if (place - (editwinrows / 2 - 1 - i) < CREDIT_LEN)
		    what = credits[place - (editwinrows / 2 - 1 - i)];
		else
		    what = "";

1881
		start_x = COLS / 2 - strlen(what) / 2 - 1;
Chris Allegretta's avatar
Chris Allegretta committed
1882
1883
1884
1885
		mvwaddstr(edit, i * 2 - k, start_x, what);
	    }
	    usleep(700000);
	    wrefresh(edit);
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
	}
	if (j < editwinrows / 2 - 1)
	    j++;

	place++;

	if (place >= CREDIT_LEN + editwinrows / 2)
	    break;
    }

    nodelay(edit, FALSE);
    curs_set(1);
    display_main_list();
    total_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
1900
}
1901
#endif
1902

Chris Allegretta's avatar
Chris Allegretta committed
1903
int keypad_on(WINDOW * win, int newval)
1904
{
1905
/* This is taken right from aumix.  Don't sue me. */
1906
#ifdef HAVE_USEKEYPAD
Chris Allegretta's avatar
Chris Allegretta committed
1907
    int old = win->_use_keypad;
1908
    keypad(win, newval);
1909
1910
    return old;
#else
1911
    keypad(win, newval);
1912
    return 1;
Chris Allegretta's avatar
Chris Allegretta committed
1913
#endif /* HAVE_USEKEYPAD */
1914
}