winio.c 45 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
 *   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.            *
 *                                                                        *
 **************************************************************************/

22
23
#include "config.h"

Chris Allegretta's avatar
Chris Allegretta committed
24
25
#include <stdarg.h>
#include <string.h>
26
#include <stdlib.h>
27
#include <unistd.h>
Chris Allegretta's avatar
Chris Allegretta committed
28
29
30
31
32
33
34
35
36
37
#include "proto.h"
#include "nano.h"

#ifndef NANO_SMALL
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif

38
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
45
46
47
48

/* Local Function Prototypes for only winio.c */
inline int get_page_from_virtual(int virtual);
inline int get_page_start_virtual(int page);
inline int get_page_end_virtual(int page);

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

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

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

69
/* Like xplustabs, but for a specific index of a specific filestruct */
Chris Allegretta's avatar
Chris Allegretta committed
70
71
72
73
74
75
76
77
78
79
80
int xpt(filestruct * fileptr, int index)
{
    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) {
81
	    if (tabs % tabsize == 0);
Chris Allegretta's avatar
Chris Allegretta committed
82
	    else
83
		tabs += tabsize - (tabs % tabsize);
Chris Allegretta's avatar
Chris Allegretta committed
84
	} else if (fileptr->data[i] & 0x80)
85
	    /* Make 8 bit chars only 1 column! */
Chris Allegretta's avatar
Chris Allegretta committed
86
	    ;
Chris Allegretta's avatar
Chris Allegretta committed
87
	else if (fileptr->data[i] < 32 || fileptr->data[i] == 127)
Chris Allegretta's avatar
Chris Allegretta committed
88
89
90
91
92
93
94
95
96
97
98
99
100
	    tabs++;
    }

    return tabs;
}

/* Return the actual place on the screen of current->data[current_x], which 
   should always be > current_x */
int xplustabs(void)
{
    return xpt(current, current_x);
}

101
102
103
/* Return what current_x should be, given xplustabs() for the line, 
 * given a start position in the filestruct's data */
int actual_x_from_start(filestruct * fileptr, int xplus, int start)
Chris Allegretta's avatar
Chris Allegretta committed
104
{
105
    int i, tot = 1;
Chris Allegretta's avatar
Chris Allegretta committed
106
107
108
109

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

Chris Allegretta's avatar
Chris Allegretta committed
110
    for (i = start; tot <= xplus && fileptr->data[i] != 0; i++, tot++)
111
	if (fileptr->data[i] == NANO_CONTROL_I) {
Chris Allegretta's avatar
Chris Allegretta committed
112
	    if (tot % tabsize != 0)
113
		tot += tabsize - (tot % tabsize);
114
115
	} else if (fileptr->data[i] & 0x80)
	    tot++;		/* Make 8 bit chars only 1 column (again) */
Chris Allegretta's avatar
Chris Allegretta committed
116
117
	else if (fileptr->data[i] < 32 || fileptr->data[i] == 127) {
	    i++;
Chris Allegretta's avatar
Chris Allegretta committed
118
	    tot += 2;
Chris Allegretta's avatar
Chris Allegretta committed
119
	}
Chris Allegretta's avatar
Chris Allegretta committed
120
121

#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
122
123
    fprintf(stderr, _("actual_x_from_start for xplus=%d returned %d\n"),
	    xplus, i);
Chris Allegretta's avatar
Chris Allegretta committed
124
#endif
Chris Allegretta's avatar
Chris Allegretta committed
125

126
127
128
129
    return i - start;
}

/* Opposite of xplustabs */
130
int actual_x(filestruct * fileptr, int xplus)
131
132
{
    return actual_x_from_start(fileptr, xplus, 0);
Chris Allegretta's avatar
Chris Allegretta committed
133
134
135
}

/* a strlen with tabs factored in, similar to xplustabs() */
136
int strnlenpt(char *buf, int size)
Chris Allegretta's avatar
Chris Allegretta committed
137
138
139
140
141
142
{
    int i, tabs = 0;

    if (buf == NULL)
	return 0;

143
    for (i = 0; i < size; i++) {
Chris Allegretta's avatar
Chris Allegretta committed
144
145
146
	tabs++;

	if (buf[i] == NANO_CONTROL_I) {
147
	    if (tabs % tabsize == 0);
Chris Allegretta's avatar
Chris Allegretta committed
148
	    else
149
		tabs += tabsize - (tabs % tabsize);
Chris Allegretta's avatar
Chris Allegretta committed
150
	} else if (buf[i] & 0x80)
151
	    /* Make 8 bit chars only 1 column! */
Chris Allegretta's avatar
Chris Allegretta committed
152
	    ;
Chris Allegretta's avatar
Chris Allegretta committed
153
	else if (buf[i] < 32 || buf[i] == 127)
Chris Allegretta's avatar
Chris Allegretta committed
154
155
156
157
158
159
	    tabs++;
    }

    return tabs;
}

160
161
162
163
164
int strlenpt(char *buf)
{
    return strnlenpt(buf, strlen(buf));
}

165
/* resets current_y, based on the position of current, and puts the cursor at 
Chris Allegretta's avatar
Chris Allegretta committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
   (current_y, current_x) */
void reset_cursor(void)
{
    filestruct *ptr = edittop;
    int x;

    current_y = 0;

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

    x = xplustabs();
    if (x <= COLS - 2)
181
	wmove(edit, current_y, x);
Chris Allegretta's avatar
Chris Allegretta committed
182
    else
183
	wmove(edit, current_y, x -
Chris Allegretta's avatar
Chris Allegretta committed
184
	      get_page_start_virtual(get_page_from_virtual(x)));
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
216
217
218
219
220
221
222
223
224
225
226

}

void blank_bottombars(void)
{
    int i = no_help()? 3 : 1;

    for (; i <= 2; i++)
	mvwaddstr(bottomwin, i, 0, hblank);

}

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();
    }
}

227
228
229
/* Repaint the statusbar when getting a character in nanogetstr */
void nanoget_repaint(char *buf, char *inputbuf, int x)
{
230
231
    int len = strlen(buf);
    int wid = COLS - len;
232

233
234
#ifdef ENABLE_COLOR
    color_on(bottomwin, COLOR_STATUSBAR);
235
236
#else
    wattron(bottomwin, A_REVERSE);
237
#endif
238
    blank_statusbar();
239

240
241
242
    if (x <= COLS - 1) {
	/* Black magic */
	buf[len - 1] = ' ';
243

244
245
246
	mvwaddstr(bottomwin, 0, 0, buf);
	waddnstr(bottomwin, inputbuf, wid);
	wmove(bottomwin, 0, (x % COLS));
Chris Allegretta's avatar
Chris Allegretta committed
247
    } else {
248
249
	/* Black magic */
	buf[len - 1] = '$';
250

251
252
253
254
	mvwaddstr(bottomwin, 0, 0, buf);
	waddnstr(bottomwin, &inputbuf[wid * ((x - len) / (wid))], wid);
	wmove(bottomwin, 0, ((x - len) % wid) + len);
    }
255
256
257

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_STATUSBAR);
258
259
#else
    wattroff(bottomwin, A_REVERSE);
260
#endif
261
262
}

263
/* Get the input from the kb; this should only be called from statusq */
264
int nanogetstr(int allowtabs, char *buf, char *def, shortcut *s,
265
	       int start_x, int list)
Chris Allegretta's avatar
Chris Allegretta committed
266
{
267
    int kbinput = 0, x = 0, xend, slen;
268
    int x_left = 0, inputlen, tabbed = 0;
269
    char *inputbuf;
270
    shortcut *t;
271
#ifndef DISABLE_TABCOMP
272
273
    int shift = 0;
#endif
Chris Allegretta's avatar
Chris Allegretta committed
274

275
    slen = length_of_list(s);
276
    inputbuf = charalloc(strlen(def) + 1);
277
    inputbuf[0] = 0;
Chris Allegretta's avatar
Chris Allegretta committed
278
279
280
281

    x_left = strlen(buf);
    x = strlen(def) + x_left;

282
#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
283
    currshortcut = s;
284
285
#endif

Chris Allegretta's avatar
Chris Allegretta committed
286
    /* Get the input! */
287
    if (strlen(def) > 0)
Chris Allegretta's avatar
Chris Allegretta committed
288
	strcpy(inputbuf, def);
289
290

    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
291

292
293
294
    /* Make sure any editor screen updates are displayed before getting input */
    wrefresh(edit);

Chris Allegretta's avatar
Chris Allegretta committed
295
    while ((kbinput = wgetch(bottomwin)) != 13) {
296
	for (t = s; t != NULL; t = t->next) {
297
298
299
300
#ifdef DEBUG
	    fprintf(stderr, _("Aha! \'%c\' (%d)\n"), kbinput, kbinput);
#endif

301
	    if (kbinput == t->val && kbinput < 32) {
302

303
304
#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
305
		if (kbinput == NANO_HELP_KEY || kbinput == NANO_HELP_FKEY) {
306
307
308
309
310
		    do_help();
		    break;
		}
#endif

311
312
313
		/* We shouldn't discard the answer it gave, just because
		   we hit a keystroke, GEEZ! */
		answer = mallocstrcpy(answer, inputbuf);
314
		free(inputbuf);
315
		return t->val;
Chris Allegretta's avatar
Chris Allegretta committed
316
317
318
319
	    }
	}
	xend = strlen(buf) + strlen(inputbuf);

Chris Allegretta's avatar
Chris Allegretta committed
320
321
322
	if (kbinput != '\t')
	    tabbed = 0;

Chris Allegretta's avatar
Chris Allegretta committed
323
	switch (kbinput) {
324

Chris Allegretta's avatar
Chris Allegretta committed
325
	    /* Stuff we want to equate with <enter>, ASCII 13 */
326
	case 343:
327
328
	    ungetch(13);	/* Enter on iris-ansi $TERM, sometimes */
	    break;
Chris Allegretta's avatar
Chris Allegretta committed
329
	    /* Stuff we want to ignore */
330
331
332
#ifdef PDCURSES
	case 541:
	case 542:
Chris Allegretta's avatar
Chris Allegretta committed
333
	case 543:		/* Right ctrl again */
334
	case 544:
Chris Allegretta's avatar
Chris Allegretta committed
335
	case 545:		/* Right alt again */
336
337
	    break;
#endif
338
#ifndef DISABLE_MOUSE
339
340
341
342
343
#ifdef NCURSES_MOUSE_VERSION
	case KEY_MOUSE:
	    do_mouse();
	    break;
#endif
344
#endif
345
	case NANO_HOME_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
346
347
348
	case KEY_HOME:
	    x = x_left;
	    break;
349
	case NANO_END_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
350
351
352
353
	case KEY_END:
	    x = x_left + strlen(inputbuf);
	    break;
	case KEY_RIGHT:
354
	case NANO_FORWARD_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

	    if (x < xend)
		x++;
	    wmove(bottomwin, 0, x);
	    break;
	case NANO_CONTROL_D:
	    if (strlen(inputbuf) > 0 && (x - x_left) != strlen(inputbuf)) {
		memmove(inputbuf + (x - x_left),
			inputbuf + (x - x_left) + 1,
			strlen(inputbuf) - (x - x_left) - 1);
		inputbuf[strlen(inputbuf) - 1] = 0;
	    }
	    break;
	case NANO_CONTROL_K:
	case NANO_CONTROL_U:
	    *inputbuf = 0;
	    x = x_left;
	    break;
	case KEY_BACKSPACE:
	case 127:
	case NANO_CONTROL_H:
	    if (strlen(inputbuf) > 0) {
		if (x == (x_left + strlen(inputbuf)))
		    inputbuf[strlen(inputbuf) - 1] = 0;
		else if (x - x_left) {
		    memmove(inputbuf + (x - x_left) - 1,
			    inputbuf + (x - x_left),
			    strlen(inputbuf) - (x - x_left));
		    inputbuf[strlen(inputbuf) - 1] = 0;
		}
	    }
Chris Allegretta's avatar
Chris Allegretta committed
386
387
388
	    if (x > strlen(buf))
		x--;
	    break;
389
#ifndef DISABLE_TABCOMP
Chris Allegretta's avatar
Chris Allegretta committed
390
	case NANO_CONTROL_I:
391
	    if (allowtabs) {
392
		shift = 0;
Chris Allegretta's avatar
Chris Allegretta committed
393
394
		inputbuf = input_tab(inputbuf, (x - x_left),
				     &tabbed, &shift, &list);
395
		x += shift;
396
397
		if (x - x_left > strlen(inputbuf))
		    x = strlen(inputbuf) + x_left;
398
	    }
Chris Allegretta's avatar
Chris Allegretta committed
399
	    break;
400
#endif
Chris Allegretta's avatar
Chris Allegretta committed
401
	case KEY_LEFT:
402
	case NANO_BACK_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
403
404
405
406
407
408
409
410
	    if (x > strlen(buf))
		x--;
	    wmove(bottomwin, 0, x);
	    break;
	case KEY_UP:
	case KEY_DOWN:
	    break;

411
412
413
	case KEY_DC:
	    goto do_deletekey;

Chris Allegretta's avatar
Chris Allegretta committed
414
415
	case 27:
	    switch (kbinput = wgetch(edit)) {
416
	    case 'O':
Chris Allegretta's avatar
Chris Allegretta committed
417
		switch (kbinput = wgetch(edit)) {
418
		case 'F':
Chris Allegretta's avatar
Chris Allegretta committed
419
420
		    x = x_left + strlen(inputbuf);
		    break;
421
		case 'H':
Chris Allegretta's avatar
Chris Allegretta committed
422
423
424
425
		    x = x_left;
		    break;
		}
		break;
426
	    case '[':
Chris Allegretta's avatar
Chris Allegretta committed
427
428
429
430
431
432
433
434
435
436
437
		switch (kbinput = wgetch(edit)) {
		case 'C':
		    if (x < xend)
			x++;
		    wmove(bottomwin, 0, x);
		    break;
		case 'D':
		    if (x > strlen(buf))
			x--;
		    wmove(bottomwin, 0, x);
		    break;
438
439
		case '1':
		case '7':
Chris Allegretta's avatar
Chris Allegretta committed
440
		    x = x_left;
441
442
443
		    goto skip_tilde;
		case '3':
		  do_deletekey:
Chris Allegretta's avatar
Chris Allegretta committed
444
445
446
447
448
449
450
		    if (strlen(inputbuf) > 0
			&& (x - x_left) != strlen(inputbuf)) {
			memmove(inputbuf + (x - x_left),
				inputbuf + (x - x_left) + 1,
				strlen(inputbuf) - (x - x_left) - 1);
			inputbuf[strlen(inputbuf) - 1] = 0;
		    }
451
452
453
		    goto skip_tilde;
		case '4':
		case '8':
Chris Allegretta's avatar
Chris Allegretta committed
454
		    x = x_left + strlen(inputbuf);
455
456
		    goto skip_tilde;
		  skip_tilde:
Chris Allegretta's avatar
Chris Allegretta committed
457
458
		    nodelay(edit, TRUE);
		    kbinput = wgetch(edit);
459
		    if (kbinput == '~' || kbinput == ERR)
Chris Allegretta's avatar
Chris Allegretta committed
460
461
462
463
			kbinput = -1;
		    nodelay(edit, FALSE);
		    break;
		}
464
465
	    default:

466
		for (t = s; t != NULL; t = t->next) {
467
#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
468
469
		    fprintf(stderr, _("Aha! \'%c\' (%d)\n"), kbinput,
			    kbinput);
470
#endif
471
		    if (kbinput == t->val || kbinput == t->val - 32) {
472
473
474
475
476
477

			/* 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... */
			answer = mallocstrcpy(answer, inputbuf);
			free(inputbuf);
478
			return t->val;
479
480
481
		    }
		}

Chris Allegretta's avatar
Chris Allegretta committed
482
483
484
485
	    }
	    break;

	default:
486

Chris Allegretta's avatar
Chris Allegretta committed
487
488
	    if (kbinput < 32)
		break;
489
490
491
492

	    inputlen = strlen(inputbuf);
	    inputbuf = nrealloc(inputbuf, inputlen + 2);

Chris Allegretta's avatar
Chris Allegretta committed
493
494
	    memmove(&inputbuf[x - x_left + 1],
		    &inputbuf[x - x_left], inputlen - (x - x_left) + 1);
495
496
	    inputbuf[x - x_left] = kbinput;

Chris Allegretta's avatar
Chris Allegretta committed
497
498
499
500
501
502
	    x++;

#ifdef DEBUG
	    fprintf(stderr, _("input \'%c\' (%d)\n"), kbinput, kbinput);
#endif
	}
503
	nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
504
505
506
	wrefresh(bottomwin);
    }

507
    answer = mallocstrcpy(answer, inputbuf);
508
    free(inputbuf);
Chris Allegretta's avatar
Chris Allegretta committed
509

Chris Allegretta's avatar
Chris Allegretta committed
510
    /* In pico mode, just check for a blank answer here */
Chris Allegretta's avatar
Chris Allegretta committed
511
    if (ISSET(PICO_MODE) && answer[0] == '\0')
Chris Allegretta's avatar
Chris Allegretta committed
512
513
514
515
516
517
518
519
520
521
522
523
	return -2;
    else
	return 0;
}

void horizbar(WINDOW * win, int y)
{
    wattron(win, A_REVERSE);
    mvwaddstr(win, 0, 0, hblank);
    wattroff(win, A_REVERSE);
}

Chris Allegretta's avatar
Chris Allegretta committed
524
void titlebar(char *path)
Chris Allegretta's avatar
Chris Allegretta committed
525
526
{
    int namelen, space;
Chris Allegretta's avatar
Chris Allegretta committed
527
528
529
530
    char *what = path;

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

532
533
534
535
#ifdef ENABLE_COLOR
    color_on(topwin, COLOR_TITLEBAR);
    mvwaddstr(topwin, 0, 0, hblank);
#else
Chris Allegretta's avatar
Chris Allegretta committed
536
537
    horizbar(topwin, 0);
    wattron(topwin, A_REVERSE);
538
539
540
#endif


Chris Allegretta's avatar
Chris Allegretta committed
541
542
543
544
    mvwaddstr(topwin, 0, 3, VERMSG);

    space = COLS - strlen(VERMSG) - strlen(VERSION) - 21;

Chris Allegretta's avatar
Chris Allegretta committed
545
    namelen = strlen(what);
Chris Allegretta's avatar
Chris Allegretta committed
546

Chris Allegretta's avatar
Chris Allegretta committed
547
    if (what[0] == '\0')
548
	mvwaddstr(topwin, 0, COLS / 2 - 6, _("New Buffer"));
Chris Allegretta's avatar
Chris Allegretta committed
549
550
    else {
	if (namelen > space) {
Chris Allegretta's avatar
Chris Allegretta committed
551
552
553
554
555
	    if (path == NULL)
		waddstr(topwin, _("  File: ..."));
	    else
		waddstr(topwin, _("   DIR: ..."));
	    waddstr(topwin, &what[namelen - space]);
Chris Allegretta's avatar
Chris Allegretta committed
556
	} else {
Chris Allegretta's avatar
Chris Allegretta committed
557
	    if (path == NULL)
Chris Allegretta's avatar
Chris Allegretta committed
558
559
		mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1),
			  _("File: "));
Chris Allegretta's avatar
Chris Allegretta committed
560
	    else
Chris Allegretta's avatar
Chris Allegretta committed
561
562
		mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1),
			  _(" DIR: "));
Chris Allegretta's avatar
Chris Allegretta committed
563
	    waddstr(topwin, what);
Chris Allegretta's avatar
Chris Allegretta committed
564
565
566
567
	}
    }
    if (ISSET(MODIFIED))
	mvwaddstr(topwin, 0, COLS - 10, _("Modified"));
Chris Allegretta's avatar
Chris Allegretta committed
568
569
    else if (ISSET(VIEW_MODE))
	mvwaddstr(topwin, 0, COLS - 10, _("View"));
570
571
572
573

#ifdef ENABLE_COLOR
    color_off(topwin, COLOR_TITLEBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
574
    wattroff(topwin, A_REVERSE);
575
576
#endif

Chris Allegretta's avatar
Chris Allegretta committed
577
578
579
580
    wrefresh(topwin);
    reset_cursor();
}

581
void onekey(char *keystroke, const char *desc, int len)
Chris Allegretta's avatar
Chris Allegretta committed
582
{
583
    int i;
Chris Allegretta's avatar
Chris Allegretta committed
584
585
586
587

    wattron(bottomwin, A_REVERSE);
    waddstr(bottomwin, keystroke);
    wattroff(bottomwin, A_REVERSE);
588
589
590
591
    waddch(bottomwin, ' ');
    waddnstr(bottomwin, desc, len - 3);
    for (i = strlen(desc); i < len - 3; i++)
        waddch(bottomwin, ' ');
Chris Allegretta's avatar
Chris Allegretta committed
592
593
594
595
596
597
598
599
600
601
602
}

void clear_bottomwin(void)
{
    if (ISSET(NO_HELP))
	return;

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

603
void bottombars(shortcut *s)
Chris Allegretta's avatar
Chris Allegretta committed
604
{
605
    int i, j, numcols;
Chris Allegretta's avatar
Chris Allegretta committed
606
    char keystr[10];
607
608
609
610
611
612
613
    shortcut *t;
    int slen;

    if (s == main_list)
	slen = MAIN_VISIBLE;
    else
	slen = length_of_list(s);
Chris Allegretta's avatar
Chris Allegretta committed
614
615
616
617

    if (ISSET(NO_HELP))
	return;

Chris Allegretta's avatar
Chris Allegretta committed
618
#ifdef ENABLE_COLOR
619
    color_on(bottomwin, COLOR_BOTTOMBARS);
620
    if (!colors[COLOR_BOTTOMBARS - FIRST_COLORNUM].set ||
Chris Allegretta's avatar
Chris Allegretta committed
621
622
	colors[COLOR_BOTTOMBARS - FIRST_COLORNUM].fg != COLOR_BLACK)
	wattroff(bottomwin, A_REVERSE);
623
624
#endif

Chris Allegretta's avatar
Chris Allegretta committed
625
    /* Determine how many extra spaces are needed to fill the bottom of the screen */
626
    if (slen < 2)
627
	numcols = 6;
628
    else
629
	numcols = (slen + (slen % 2)) / 2;
Chris Allegretta's avatar
Chris Allegretta committed
630
631

    clear_bottomwin();
632

633
    t = s;
634
635
    for (i = 0; i < numcols; i++) {
	for (j = 0; j <= 1; j++) {
Chris Allegretta's avatar
Chris Allegretta committed
636

637
	    wmove(bottomwin, 1 + j, i * ((COLS - 1) / numcols));
638

639
640
641
642
	    if (t->val < 97)
		snprintf(keystr, 10, "^%c", t->val + 64);
	    else
		snprintf(keystr, 10, "M-%c", t->val - 32);
643

644
	    onekey(keystr, t->desc, (COLS - 1) / numcols);
Chris Allegretta's avatar
Chris Allegretta committed
645

646
647
648
649
	    if (t->next == NULL)
		break;
	    t = t->next;
	}
650
	
Chris Allegretta's avatar
Chris Allegretta committed
651
652
    }

Chris Allegretta's avatar
Chris Allegretta committed
653
#ifdef ENABLE_COLOR
654
655
656
    color_off(bottomwin, COLOR_BOTTOMBARS);
#endif

Chris Allegretta's avatar
Chris Allegretta committed
657
658
659
660
661
662
663
664
665
    wrefresh(bottomwin);

}

/* If modified is not already set, set it and update titlebar */
void set_modified(void)
{
    if (!ISSET(MODIFIED)) {
	SET(MODIFIED);
Chris Allegretta's avatar
Chris Allegretta committed
666
	titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
667
668
669
670
	wrefresh(topwin);
    }
}

671
672
673
/* 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 */
674
/* characters is viewable (e.g.: page 1 shows from 1 to COLS) */
Chris Allegretta's avatar
Chris Allegretta committed
675
676
inline int get_page_from_virtual(int virtual)
{
677
    int page = 2;
Chris Allegretta's avatar
Chris Allegretta committed
678

Chris Allegretta's avatar
Chris Allegretta committed
679
680
    if (virtual <= COLS - 2)
	return 1;
681
    virtual -= (COLS - 2);
682
683

    while (virtual > COLS - 2 - 7) {
684
	virtual -= (COLS - 2 - 7);
685
	page++;
Chris Allegretta's avatar
Chris Allegretta committed
686
687
    }

688
689
690
    return page;
}

691
/* The inverse of the above function */
Chris Allegretta's avatar
Chris Allegretta committed
692
693
inline int get_page_start_virtual(int page)
{
694
695
    int virtual;
    virtual = --page * (COLS - 7);
Chris Allegretta's avatar
Chris Allegretta committed
696
697
    if (page)
	virtual -= 2 * page - 1;
698
    return virtual;
699
700
}

Chris Allegretta's avatar
Chris Allegretta committed
701
702
inline int get_page_end_virtual(int page)
{
703
704
705
706
    return get_page_start_virtual(page) + COLS - 1;
}

#ifndef NANO_SMALL
707
708
709
/* This takes care of the case where there is a mark that covers only */
/* the current line. */

710
/* It expects a line with no tab characters (i.e.: the type that edit_add */
711
/* deals with */
Chris Allegretta's avatar
Chris Allegretta committed
712
713
void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
			 int virt_cur_x, int this_page)
714
{
715
716
717
    /*
     * 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
718
     * turn (for those that are currently visible, of course)
719
720
721
722
     *
     * 3 start points: 0 -> begin, begin->end, end->strlen(data)
     *    in data    :    pre          sel           post        
     */
Chris Allegretta's avatar
Chris Allegretta committed
723
    int this_page_start = get_page_start_virtual(this_page),
724
	this_page_end = get_page_end_virtual(this_page);
725
726

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

    /* now fix the start locations & lengths according to the cursor's 
730
     * position (i.e.: our page) */
Chris Allegretta's avatar
Chris Allegretta committed
731
    if (pre_data_len < this_page_start)
732
733
734
	pre_data_len = 0;
    else
	pre_data_len -= this_page_start;
735

Chris Allegretta's avatar
Chris Allegretta committed
736
    if (begin < this_page_start)
737
738
	begin = this_page_start;

Chris Allegretta's avatar
Chris Allegretta committed
739
    if (end < this_page_start)
740
	end = this_page_start;
741

Chris Allegretta's avatar
Chris Allegretta committed
742
    if (begin > this_page_end)
743
	begin = this_page_end;
744

Chris Allegretta's avatar
Chris Allegretta committed
745
    if (end > this_page_end)
746
	end = this_page_end;
747

748
    /* Now calculate the lengths */
749
750
    sel_data_len = end - begin;
    post_data_len = this_page_end - end;
751

752
753
754
#ifdef ENABLE_COLOR
    color_on(edit, COLOR_MARKER);
#else
755
    wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
756
#endif				/* ENABLE_COLOR */
757

758
    mvwaddnstr(edit, y, begin - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
759
	       &fileptr->data[begin], sel_data_len);
760
761
762
763

#ifdef ENABLE_COLOR
    color_off(edit, COLOR_MARKER);
#else
764
    wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
765
#endif				/* ENABLE_COLOR */
766

Chris Allegretta's avatar
Chris Allegretta committed
767
768
769
}
#endif

770
771
772
773
/* 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
774
775
 * line */
void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
776
	      int virt_mark_beginx, int this_page)
Chris Allegretta's avatar
Chris Allegretta committed
777
{
778

Chris Allegretta's avatar
Chris Allegretta committed
779
#ifdef ENABLE_COLOR
780
781
    colortype *tmpcolor = NULL;
    int k, paintlen;
782
783
    filestruct *e, *s;
    regoff_t ematch, smatch;
784
785
786
#endif

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

Chris Allegretta's avatar
Chris Allegretta committed
791
#ifdef ENABLE_COLOR
792
    if (colorstrings != NULL)
Chris Allegretta's avatar
Chris Allegretta committed
793
794
	for (tmpcolor = colorstrings; tmpcolor != NULL;
	     tmpcolor = tmpcolor->next) {
795

796
	    if (tmpcolor->end == NULL) {
797

798
799
		/* First, highlight all single-line regexes */
		k = start;
800
801
802
		regcomp(&color_regexp, tmpcolor->start, 0);
		while (!regexec(&color_regexp, &fileptr->data[k], 1,
				colormatches, 0)) {
803

804
		    if (colormatches[0].rm_eo - colormatches[0].rm_so < 1) {
805
			statusbar(_("Refusing 0 length regex match"));
806
807
			break;
		    }
808
#ifdef DEBUG
809
		    fprintf(stderr, "Match! (%d chars) \"%s\"\n",
810
811
			    colormatches[0].rm_eo - colormatches[0].rm_so,
			    &fileptr->data[k + colormatches[0].rm_so]);
812
#endif
813
		    if (colormatches[0].rm_so < COLS - 1) {
814
815
816
817
			if (tmpcolor->bright)
			    wattron(edit, A_BOLD);
			wattron(edit, COLOR_PAIR(tmpcolor->pairnum));

818
			if (colormatches[0].rm_eo + k <= COLS) {
819
			    paintlen =
820
				colormatches[0].rm_eo - colormatches[0].rm_so;
821
822
823
824
825
826
827
#ifdef DEBUG
			    fprintf(stderr, "paintlen (%d) = eo (%d) - so (%d)\n", 
				paintlen, colormatches[0].rm_eo, colormatches[0].rm_so);
#endif

			}
			else {
828
			    paintlen = COLS - k - colormatches[0].rm_so - 1;
829
830
831
832
833
#ifdef DEBUG
			    fprintf(stderr, "paintlen (%d) = COLS (%d) - k (%d), - rm.so (%d) - 1\n", 
					paintlen, COLS, k, colormatches[0].rm_so);
#endif
			}
834

835
836
			mvwaddnstr(edit, yval, colormatches[0].rm_so + k,
				   &fileptr->data[k + colormatches[0].rm_so],
837
838
839
				   paintlen);

		    }
840

841
842
843
		    if (tmpcolor->bright)
			wattroff(edit, A_BOLD);
		    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
844

845
		    k += colormatches[0].rm_eo;
846

Chris Allegretta's avatar
Chris Allegretta committed
847
		}
848
849
850
851
852
853
854
855
856
857
	    }
	    /* 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) {
858
		    regcomp(&color_regexp, tmpcolor->start, 0);
859
		    if (!regexec
860
			(&color_regexp, s->data, 1, colormatches, 0))
861
862
863
864
865
866
			break;
		    s = s->prev;
		}

		if (s != NULL) {
		    /* We found a start, mark it */
867
		    smatch = colormatches[0].rm_so;
868
869
870

		    e = s;
		    while (e != NULL && e != fileptr) {
871
			regcomp(&color_regexp, tmpcolor->end, 0);
872
			if (!regexec
873
			    (&color_regexp, e->data, 1, colormatches, 0))
874
875
876
			    break;
			e = e->next;
		    }
877

878
879
880
881
		    if (e != fileptr)
			continue;	/* There's an end before us */
		    else {	/* Keep looking for an end */
			while (e != NULL) {
882
			    regcomp(&color_regexp, tmpcolor->end, 0);
883
			    if (!regexec
884
				(&color_regexp, e->data, 1, colormatches,
885
886
887
888
889
890
891
892
				 0))
				break;
			    e = e->next;
			}

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

			    while (e != NULL) {
896
				regcomp(&color_regexp, tmpcolor->end, 0);
897
				if (!regexec
898
899
				    (&color_regexp, e->data, 1,
				     colormatches, 0))
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
				    break;
				e = e->next;
			    }

			    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));

915
			    if (s == fileptr && e == fileptr && ematch < COLS) {
916
917
918
				mvwaddnstr(edit, yval, start + smatch, 
					&fileptr->data[start + smatch],
					ematch - smatch);
919
920
921
922
923
924
#ifdef DEBUG
			fprintf(stderr, "start = %d, smatch = %d, ematch = %d\n", start,
				smatch, ematch);
#endif

		    	    } else if (s == fileptr)
925
926
927
928
929
930
				mvwaddnstr(edit, yval, start + smatch, 
					&fileptr->data[start + smatch],
					COLS - smatch);
			    else if (e == fileptr)
				mvwaddnstr(edit, yval, start, 
					&fileptr->data[start],
931
					COLS - start);
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
			    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! =) */
		}
948
949

	    }
950

951
	}
Chris Allegretta's avatar
Chris Allegretta committed
952
#endif				/* ENABLE_COLOR */
Chris Allegretta's avatar
Chris Allegretta committed
953
#ifndef NANO_SMALL
954

955
    /* There are quite a few cases that could take place; we'll deal
956
     * with them each in turn */
Chris Allegretta's avatar
Chris Allegretta committed
957
    if (ISSET(MARK_ISSET) &&
958
	!((fileptr->lineno > mark_beginbuf->lineno
Chris Allegretta's avatar
Chris Allegretta committed
959
960
961
	   && fileptr->lineno > current->lineno)
	  || (fileptr->lineno < mark_beginbuf->lineno
	      && fileptr->lineno < current->lineno))) {
962
	/* If we get here we are on a line that is at least
963
964
965
966
967
	 * 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 */
968
969
970
#ifdef ENABLE_COLOR
	    color_on(edit, COLOR_MARKER);
#else
971
	    wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
972
#endif				/* ENABLE_COLOR */
973

974
	    mvwaddnstr(edit, yval, 0, fileptr->data, COLS);
975
976
977
978

#ifdef ENABLE_COLOR
	    color_off(edit, COLOR_MARKER);
#else
979
	    wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
980
#endif				/* ENABLE_COLOR */
981

982
983
984
985
986
987
988
989
990
991
992
993
994
995
	} 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) {
	    /*
996
	     * We're updating the line that was first marked,
997
	     * but we're not currently on it.  So we want to
998
	     * figure out which half to invert based on our
999
1000
	     * relative line numbers.
	     *
1001
1002
1003
	     * 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.
1004
1005
1006
	     */
	    int target;

1007
1008
1009
1010
	    if (mark_beginbuf->lineno > current->lineno) {
#ifdef ENABLE_COLOR
		color_on(edit, COLOR_MARKER);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1011
		wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1012
#endif				/* ENABLE_COLOR */
1013

1014
		target =
Chris Allegretta's avatar
Chris Allegretta committed
1015
1016
		    (virt_mark_beginx <
		     COLS - 1) ? virt_mark_beginx : COLS - 1;
1017

1018
		mvwaddnstr(edit, yval, 0, fileptr->data, target);
1019
1020

#ifdef ENABLE_COLOR
1021
		color_off(edit, COLOR_MARKER);
1022
#else
1023
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1024
#endif				/* ENABLE_COLOR */
1025
1026


1027
1028
1029
	    }

	    if (mark_beginbuf->lineno < current->lineno) {
1030
#ifdef ENABLE_COLOR
1031
		color_on(edit, COLOR_MARKER);
1032
#else
1033
		wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1034
#endif				/* ENABLE_COLOR */
1035

1036
		target = (COLS - 1) - virt_mark_beginx;
1037

1038
1039
		if (target < 0)
		    target = 0;
1040

1041
		mvwaddnstr(edit, yval, virt_mark_beginx,
Chris Allegretta's avatar
Chris Allegretta committed
1042
			   &fileptr->data[virt_mark_beginx], target);
1043

1044
1045
1046
#ifdef ENABLE_COLOR
		color_off(edit, COLOR_MARKER);
#else
1047
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1048
#endif				/* ENABLE_COLOR */
1049
1050

	    }
1051
1052

	} else if (fileptr == current) {
1053
	    /* We're on the cursor's line, but it's not the first
1054
1055
1056
1057
	     * 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);

1058
1059
1060
1061
1062
	    if (mark_beginbuf->lineno < current->lineno) {

#ifdef ENABLE_COLOR
		color_on(edit, COLOR_MARKER);
#else
1063
		wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1064
#endif				/* ENABLE_COLOR */
1065

1066
1067
		if (virt_cur_x > COLS - 2) {
		    mvwaddnstr(edit, yval, 0,
Chris Allegretta's avatar
Chris Allegretta committed
1068
1069
			       &fileptr->data[this_page_start],
			       virt_cur_x - this_page_start);
1070
1071
		} else
		    mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
1072
1073

#ifdef ENABLE_COLOR
1074
		color_off(edit, COLOR_MARKER);
1075
#else
1076
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1077
#endif				/* ENABLE_COLOR */
1078

1079
1080
1081
	    }

	    if (mark_beginbuf->lineno > current->lineno) {
1082
1083

#ifdef ENABLE_COLOR
1084
		color_on(edit, COLOR_MARKER);
1085
#else
1086
		wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1087
#endif				/* ENABLE_COLOR */
1088

1089
1090
		if (virt_cur_x > COLS - 2)
		    mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
1091
1092
			       &fileptr->data[virt_cur_x],
			       this_page_end - virt_cur_x);
1093
1094
		else
		    mvwaddnstr(edit, yval, virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
1095
1096
			       &fileptr->data[virt_cur_x],
			       COLS - virt_cur_x);
1097

1098
1099
1100
#ifdef ENABLE_COLOR
		color_off(edit, COLOR_MARKER);
#else
1101
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1102
#endif				/* ENABLE_COLOR */
1103
1104

	    }
Chris Allegretta's avatar
Chris Allegretta committed
1105
	}
1106
1107
1108
    }
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1109
1110
1111
}

/*
1112
1113
 * Just update one line in the edit buffer.  Basically a wrapper for
 * edit_add
Chris Allegretta's avatar
Chris Allegretta committed
1114
 *
1115
 * index gives us a place in the string to update starting from.
Chris Allegretta's avatar
Chris Allegretta committed
1116
1117
1118
1119
1120
 * Likely args are current_x or 0.
 */
void update_line(filestruct * fileptr, int index)
{
    filestruct *filetmp;
1121
1122
1123
    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
1124
    int i, pos, len, page;
Chris Allegretta's avatar
Chris Allegretta committed
1125

Chris Allegretta's avatar
Chris Allegretta committed
1126
1127
    if (!fileptr)
	return;
1128

1129
    /* First, blank out the line (at a minimum) */
Chris Allegretta's avatar
Chris Allegretta committed
1130
1131
1132
1133
1134
    for (filetmp = edittop; filetmp != fileptr && filetmp != editbot;
	 filetmp = filetmp->next)
	line++;

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

1136
    /* Next, convert all the tabs to spaces, so everything else is easy */
1137
1138
1139
1140
    index = xpt(fileptr, index);

    realdata = fileptr->data;
    len = strlen(realdata);
1141
    fileptr->data = charalloc(xpt(fileptr, len) + 1);
1142
1143

    pos = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1144
1145
    for (i = 0; i < len; i++) {
	if (realdata[i] == '\t') {
1146
1147
	    do {
		fileptr->data[pos++] = ' ';
Chris Allegretta's avatar
Chris Allegretta committed
1148
1149
1150
1151
		if (i < current_x)
		    virt_cur_x++;
		if (i < mark_beginx)
		    virt_mark_beginx++;
1152
	    } while (pos % tabsize);
1153
	    /* must decrement once to account for tab-is-one-character */
Chris Allegretta's avatar
Chris Allegretta committed
1154
1155
1156
1157
	    if (i < current_x)
		virt_cur_x--;
	    if (i < mark_beginx)
		virt_mark_beginx--;
Chris Allegretta's avatar
Chris Allegretta committed
1158
1159
1160
1161
	} else if (realdata[i] == 127) {
	    /* Treat control characters as ^symbol (ASCII 1 - 31, 127) */
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = '?';
1162
	} else if (realdata[i] >= 1 && realdata[i] <= 31) {
1163
1164
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = realdata[i] + 64;
1165
1166
1167
1168
1169
1170
	} else {
	    fileptr->data[pos++] = realdata[i];
	}
    }

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

Chris Allegretta's avatar
Chris Allegretta committed
1172
    /* Now, paint the line */
1173
    if (current == fileptr && index > COLS - 2) {
1174
	/* This handles when the current line is beyond COLS */
1175
	/* It requires figuring out what page we're on      */
1176
	page = get_page_from_virtual(index);
1177
	col = get_page_start_virtual(page);
Chris Allegretta's avatar
Chris Allegretta committed
1178

1179
	edit_add(filetmp, line, col, virt_cur_x, virt_mark_beginx, page);
Chris Allegretta's avatar
Chris Allegretta committed
1180
1181
	mvwaddch(edit, line, 0, '$');

Chris Allegretta's avatar
Chris Allegretta committed
1182
	if (strlenpt(fileptr->data) > get_page_end_virtual(page) + 1)
Chris Allegretta's avatar
Chris Allegretta committed
1183
	    mvwaddch(edit, line, COLS - 1, '$');
1184
    } else {
1185
1186
1187
	/* 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
1188

1189
	if (strlenpt(&filetmp->data[col]) > COLS)
Chris Allegretta's avatar
Chris Allegretta committed
1190
	    mvwaddch(edit, line, COLS - 1, '$');
1191
    }
1192
1193
1194
1195
1196

    /* Clean up our mess */
    tmp = fileptr->data;
    fileptr->data = realdata;
    free(tmp);
Chris Allegretta's avatar
Chris Allegretta committed
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
}

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

/* Refresh the screen without changing the position of lines */
void edit_refresh(void)
{
Chris Allegretta's avatar
Chris Allegretta committed
1208
    static int noloop = 0;
1209
    int nlines = 0, i = 0, currentcheck = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1210
1211
1212
1213
1214
1215
1216
    filestruct *temp, *hold = current;

    if (current == NULL)
	return;

    temp = edittop;

1217
    while (nlines <= editwinrows - 1 && nlines <= totlines && temp != NULL) {
Chris Allegretta's avatar
Chris Allegretta committed
1218
1219
	hold = temp;
	update_line(temp, current_x);
1220
1221
1222
	if (temp == current)
	    currentcheck = 1;

Chris Allegretta's avatar
Chris Allegretta committed
1223
	temp = temp->next;
1224
	nlines++;
Chris Allegretta's avatar
Chris Allegretta committed
1225
    }
Chris Allegretta's avatar
Chris Allegretta committed
1226
1227
    /* If noloop == 1, then we already did an edit_update without finishing
       this function.  So we don't run edit_update again */
1228
    if (!currentcheck && !noloop) {	/* Then current has run off the screen... */
1229
	edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
1230
	noloop = 1;
1231
    } else if (noloop)
Chris Allegretta's avatar
Chris Allegretta committed
1232
	noloop = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1233

1234
1235
1236
1237
    if (nlines <= editwinrows - 1)
	while (nlines <= editwinrows - 1) {
	    mvwaddstr(edit, nlines, i, hblank);
	    nlines++;
Chris Allegretta's avatar
Chris Allegretta committed
1238
1239
1240
1241
1242
	}
    if (temp == NULL)
	editbot = hold;
    else
	editbot = temp;
1243
1244

    /* What the hell are we expecting to update the screen if this isn't 
Chris Allegretta's avatar
Chris Allegretta committed
1245
       here? luck?? */
1246
    wrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
1247
1248
}

1249
/*
1250
 * Same as above, but touch the window first, so everything is redrawn.
1251
1252
1253
1254
1255
1256
1257
1258
 */
void edit_refresh_clearok(void)
{
    clearok(edit, TRUE);
    edit_refresh();
    clearok(edit, FALSE);
}

Chris Allegretta's avatar
Chris Allegretta committed
1259
/*
1260
 * Nice generic routine to update the edit buffer, given a pointer to the
Chris Allegretta's avatar
Chris Allegretta committed
1261
1262
 * file struct =) 
 */
1263
void edit_update(filestruct * fileptr, int topmidbotnone)
Chris Allegretta's avatar
Chris Allegretta committed
1264
{
1265
    int i = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1266
1267
1268
1269
1270
1271
    filestruct *temp;

    if (fileptr == NULL)
	return;

    temp = fileptr;
1272
1273
1274
1275
1276
    if (topmidbotnone == TOP);
    else if (topmidbotnone == NONE)
	for (i = 0; i <= current_y - 1 && temp->prev != NULL; i++)
	    temp = temp->prev;
    else if (topmidbotnone == BOTTOM)
1277
1278
1279
1280
1281
	for (i = 0; i <= editwinrows - 1 && temp->prev != NULL; i++)
	    temp = temp->prev;
    else
	for (i = 0; i <= editwinrows / 2 && temp->prev != NULL; i++)
	    temp = temp->prev;
Chris Allegretta's avatar
Chris Allegretta committed
1282

1283
1284
    edittop = temp;
    fix_editbot();
Chris Allegretta's avatar
Chris Allegretta committed
1285
1286
1287
1288

    edit_refresh();
}

1289
/* This function updates current, based on where current_y is; reset_cursor 
Chris Allegretta's avatar
Chris Allegretta committed
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
   does the opposite */
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;
    while (i <= current_y - 1 && current->next != NULL) {
	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
1315
 * otherwise, the valid shortcut key caught.  Def is any editable text we
Chris Allegretta's avatar
Chris Allegretta committed
1316
 * want to put up by default.
1317
1318
 *
 * New arg tabs tells whether or not to allow tab completion.
Chris Allegretta's avatar
Chris Allegretta committed
1319
 */
1320
int statusq(int tabs, shortcut *s, char *def, char *msg, ...)
Chris Allegretta's avatar
Chris Allegretta committed
1321
1322
1323
{
    va_list ap;
    char foo[133];
1324
    int ret;
Chris Allegretta's avatar
Chris Allegretta committed
1325

1326
#ifndef DISABLE_TABCOMP
1327
    int list = 0;
1328
1329
#endif

1330
    bottombars(s);
Chris Allegretta's avatar
Chris Allegretta committed
1331
1332
1333
1334

    va_start(ap, msg);
    vsnprintf(foo, 132, msg, ap);
    va_end(ap);
1335
    strncat(foo, ": ", 132);
Chris Allegretta's avatar
Chris Allegretta committed
1336

1337
1338
1339
#ifdef ENABLE_COLOR
    color_on(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1340
    wattron(bottomwin, A_REVERSE);
1341
1342
1343
#endif


1344
#ifndef DISABLE_TABCOMP
1345
    ret = nanogetstr(tabs, foo, def, s, (strlen(foo) + 3), list);
1346
1347
1348
#else
    /* if we've disabled tab completion, the value of list won't be
       used at all, so it's safe to use 0 (NULL) as a placeholder */
1349
    ret = nanogetstr(tabs, foo, def, s, (strlen(foo) + 3), 0);
1350
#endif
1351
1352
1353
1354

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1355
    wattroff(bottomwin, A_REVERSE);
1356
1357
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367

    switch (ret) {

    case NANO_FIRSTLINE_KEY:
	do_first_line();
	break;
    case NANO_LASTLINE_KEY:
	do_last_line();
	break;
    case NANO_CANCEL_KEY:
1368
1369
1370
1371
1372
1373
1374
#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
1375
1376
	return -1;
    default:
1377
	blank_statusbar();
Chris Allegretta's avatar
Chris Allegretta committed
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
    }

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

    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)
 */
int do_yesno(int all, int leavecursor, char *msg, ...)
{
    va_list ap;
    char foo[133];
1395
1396
1397
1398
1399
    int kbinput, ok = -1, i;
    char *yesstr;		/* String of yes characters accepted */
    char *nostr;		/* Same for no */
    char *allstr;		/* And all, surprise! */
    char shortstr[5];		/* Temp string for above */
1400
#ifndef DISABLE_MOUSE
Chris Allegretta's avatar
Chris Allegretta committed
1401
1402
1403
1404
1405
#ifdef NCURSES_MOUSE_VERSION
    MEVENT mevent;
#endif
#endif

1406
1407

    /* Yes, no and all are strings of any length.  Each string consists of
Chris Allegretta's avatar
Chris Allegretta committed
1408
1409
       all characters accepted as a valid character for that value.
       The first value will be the one displayed in the shortcuts. */
1410
1411
1412
    yesstr = _("Yy");
    nostr = _("Nn");
    allstr = _("Aa");
Chris Allegretta's avatar
Chris Allegretta committed
1413
1414
1415

    /* Write the bottom of the screen */
    clear_bottomwin();
1416
1417
1418
1419

#ifdef ENABLE_COLOR
    color_on(bottomwin, COLOR_BOTTOMBARS);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1420

Jordi Mallach's avatar
   
Jordi Mallach committed
1421
    /* Remove gettext call for keybindings until we clear the thing up */
Chris Allegretta's avatar
Chris Allegretta committed
1422
1423
    if (!ISSET(NO_HELP)) {
	wmove(bottomwin, 1, 0);
1424
1425

	snprintf(shortstr, 3, " %c", yesstr[0]);
1426
	onekey(shortstr, _("Yes"), 16);
1427
1428
1429

	if (all) {
	    snprintf(shortstr, 3, " %c", allstr[0]);
1430
	    onekey(shortstr, _("All"), 16);
1431
	}
Chris Allegretta's avatar
Chris Allegretta committed
1432
	wmove(bottomwin, 2, 0);
1433
1434

	snprintf(shortstr, 3, " %c", nostr[0]);
1435
	onekey(shortstr, _("No"), 16);
1436

1437
	onekey("^C", _("Cancel"), 16);
Chris Allegretta's avatar
Chris Allegretta committed
1438
1439
1440
1441
    }
    va_start(ap, msg);
    vsnprintf(foo, 132, msg, ap);
    va_end(ap);
1442
1443
1444
1445
1446

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_BOTTOMBARS);
    color_on(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1447
    wattron(bottomwin, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1448
#endif				/* ENABLE_COLOR */
1449
1450

    blank_statusbar();
Chris Allegretta's avatar
Chris Allegretta committed
1451
    mvwaddstr(bottomwin, 0, 0, foo);
1452
1453
1454
1455

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1456
    wattroff(bottomwin, A_REVERSE);
1457
1458
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1459
1460
1461
1462
1463
1464
1465
1466
1467
    wrefresh(bottomwin);

    if (leavecursor == 1)
	reset_cursor();

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

	switch (kbinput) {
1468
#ifndef DISABLE_MOUSE
Chris Allegretta's avatar
Chris Allegretta committed
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
#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
		   of possible return keystrokes based on the x and y values */
		if (all) {
		    char yesnosquare[2][2] = {
Chris Allegretta's avatar
Chris Allegretta committed
1486
1487
1488
			{yesstr[0], allstr[0]},
			{nostr[0], NANO_CONTROL_C}
		    };
Chris Allegretta's avatar
Chris Allegretta committed
1489

Chris Allegretta's avatar
Chris Allegretta committed
1490
		    ungetch(yesnosquare[mevent.y][mevent.x / (COLS / 6)]);
Chris Allegretta's avatar
Chris Allegretta committed
1491
1492
1493
		} else {
		    char yesnosquare[2][2] = {
			{yesstr[0], '\0'},
Chris Allegretta's avatar
Chris Allegretta committed
1494
1495
			{nostr[0], NANO_CONTROL_C}
		    };
Chris Allegretta's avatar
Chris Allegretta committed
1496

Chris Allegretta's avatar
Chris Allegretta committed
1497
		    ungetch(yesnosquare[mevent.y][mevent.x / (COLS / 6)]);
Chris Allegretta's avatar
Chris Allegretta committed
1498
1499
1500
1501
1502
		}
	    }
	    break;
#endif
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1503
1504
1505
	case NANO_CONTROL_C:
	    ok = -2;
	    break;
1506
1507
	default:

1508
	    /* Look for the kbinput in the yes, no and (optimally) all str */
Chris Allegretta's avatar
Chris Allegretta committed
1509
	    for (i = 0; yesstr[i] != 0 && yesstr[i] != kbinput; i++);
1510
	    if (yesstr[i] != 0) {
1511
		ok = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1512
		break;
1513
1514
	    }

Chris Allegretta's avatar
Chris Allegretta committed
1515
	    for (i = 0; nostr[i] != 0 && nostr[i] != kbinput; i++);
1516
1517
	    if (nostr[i] != 0) {
		ok = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1518
		break;
1519
1520
1521
	    }

	    if (all) {
Chris Allegretta's avatar
Chris Allegretta committed
1522
		for (i = 0; allstr[i] != 0 && allstr[i] != kbinput; i++);
1523
1524
		if (allstr[i] != 0) {
		    ok = 2;
Chris Allegretta's avatar
Chris Allegretta committed
1525
		    break;
1526
1527
		}
	    }
Chris Allegretta's avatar
Chris Allegretta committed
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
	}
    }

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

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

void statusbar(char *msg, ...)
{
    va_list ap;
    char foo[133];
    int start_x = 0;

    va_start(ap, msg);
    vsnprintf(foo, 132, msg, ap);
    va_end(ap);

1550
    start_x = COLS / 2 - strlen(foo) / 2 - 1;
Chris Allegretta's avatar
Chris Allegretta committed
1551
1552
1553
1554
1555
1556

    /* Blank out line */
    blank_statusbar();

    wmove(bottomwin, 0, start_x);

1557
1558
1559
#ifdef ENABLE_COLOR
    color_on(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1560
    wattron(bottomwin, A_REVERSE);
1561
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1562
1563
1564
1565

    waddstr(bottomwin, "[ ");
    waddstr(bottomwin, foo);
    waddstr(bottomwin, " ]");
1566
1567
1568
1569

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1570
    wattroff(bottomwin, A_REVERSE);
1571
1572
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
    wrefresh(bottomwin);

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

void display_main_list(void)
{
1583
    bottombars(main_list);
Chris Allegretta's avatar
Chris Allegretta committed
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
}

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);
1598
    edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
1599
    titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
1600
1601
1602
1603
1604
1605
1606
1607
1608
    return 1;
}

void previous_line(void)
{
    if (current_y > 0)
	current_y--;
}

1609
int do_cursorpos(int constant)
Chris Allegretta's avatar
Chris Allegretta committed
1610
1611
{
    filestruct *fileptr;
1612
    float linepct = 0.0, bytepct = 0.0, colpct = 0.0;
1613
    long i = 0, j = 0;
1614
    static long old_i = -1, old_totsize = -1;
Chris Allegretta's avatar
Chris Allegretta committed
1615
1616
1617
1618

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

1619
1620
1621
1622
1623
1624
    if (old_i == -1)
	old_i = i;

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

1625
    colpct = 100 * (xplustabs() + 1) / (xpt(current, strlen(current->data)) + 1);
Chris Allegretta's avatar
Chris Allegretta committed
1626

1627
1628
1629
    for (fileptr = fileage; fileptr != current && fileptr != NULL;
	 fileptr = fileptr->next)
	i += strlen(fileptr->data) + 1;
1630

1631
1632
    if (fileptr == NULL)
	return -1;
1633

1634
    i += current_x;
1635

1636
    j = totsize;
1637

1638
1639
    if (totsize > 0)
	bytepct = 100 * i / totsize;
1640
1641
1642

    if (totlines > 0)
	 linepct = 100 * current->lineno / totlines;
Chris Allegretta's avatar
Chris Allegretta committed
1643
1644
1645
1646
1647
1648

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

1649
1650
1651
1652
    /* 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
1653
	statusbar(_
1654
		  ("line %d/%d (%.0f%%), col %ld/%ld (%.0f%%), char %ld/%ld (%.0f%%)"),
1655
1656
		  current->lineno, totlines, linepct, xplustabs() + 1, 
		  xpt(current, strlen(current->data)) + 1, colpct, i, j, bytepct);
1657
1658
1659
1660
1661
    }

    old_i = i;
    old_totsize = totsize;

Chris Allegretta's avatar
Chris Allegretta committed
1662
1663
1664
1665
    reset_cursor();
    return 1;
}

1666
1667
1668
1669
1670
int do_cursorpos_void(void)
{
    return do_cursorpos(0);
}

Chris Allegretta's avatar
Chris Allegretta committed
1671
/* Our broken, non-shortcut list compliant help function.
1672
   But, hey, it's better than nothing, and it's dynamic! */
Chris Allegretta's avatar
Chris Allegretta committed
1673
1674
int do_help(void)
{
1675
#ifndef DISABLE_HELP
1676
    char *ptr, *end;
1677
    int i, j, row = 0, page = 1, kbinput = 0, no_more = 0, kp, kp2;
Chris Allegretta's avatar
Chris Allegretta committed
1678
    int no_help_flag = 0;
1679
    shortcut *oldshortcut;
Chris Allegretta's avatar
Chris Allegretta committed
1680
1681
1682

    blank_edit();
    curs_set(0);
1683
    wattroff(bottomwin, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1684
1685
    blank_statusbar();

1686
1687
1688
1689
1690
    help_init();
    ptr = help_text;

    oldshortcut = currshortcut;

1691
    currshortcut = help_list;
1692

1693
    kp = keypad_on(edit, 1);
1694
    kp2 = keypad_on(bottomwin, 1);
1695

Chris Allegretta's avatar
Chris Allegretta committed
1696
1697
    if (ISSET(NO_HELP)) {

1698
	/* Well, if we're going to do this, we should at least
1699
	   do it the right way */
Chris Allegretta's avatar
Chris Allegretta committed
1700
	no_help_flag = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1701
	UNSET(NO_HELP);
1702
	window_init();
1703
	bottombars(help_list);
1704

Chris Allegretta's avatar
Chris Allegretta committed
1705
    } else
1706
	bottombars(help_list);
Chris Allegretta's avatar
Chris Allegretta committed
1707
1708
1709
1710

    do {
	ptr = help_text;
	switch (kbinput) {
1711
#ifndef DISABLE_MOUSE
1712
#ifdef NCURSES_MOUSE_VERSION
Chris Allegretta's avatar
Chris Allegretta committed
1713
1714
1715
	case KEY_MOUSE:
	    do_mouse();
	    break;
1716
1717
#endif
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
	case NANO_NEXTPAGE_KEY:
	case NANO_NEXTPAGE_FKEY:
	case KEY_NPAGE:
	    if (!no_more) {
		blank_edit();
		page++;
	    }
	    break;
	case NANO_PREVPAGE_KEY:
	case NANO_PREVPAGE_FKEY:
	case KEY_PPAGE:
	    if (page > 1) {
		no_more = 0;
		blank_edit();
		page--;
	    }
	    break;
	}

1737
	/* Calculate where in the text we should be, based on the page */
Chris Allegretta's avatar
Chris Allegretta committed
1738
1739
1740
	for (i = 1; i < page; i++) {
	    row = 0;
	    j = 0;
1741
1742

	    while (row < editwinrows - 2 && *ptr != '\0') {
Chris Allegretta's avatar
Chris Allegretta committed
1743
1744
1745
1746
1747
1748
1749
1750
1751
		if (*ptr == '\n' || j == COLS - 5) {
		    j = 0;
		    row++;
		}
		ptr++;
		j++;
	    }
	}

1752
	if (i > 1) {
1753

1754
1755
	}

Chris Allegretta's avatar
Chris Allegretta committed
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
	i = 0;
	j = 0;
	while (i < editwinrows && *ptr != '\0') {
	    end = ptr;
	    while (*end != '\n' && *end != '\0' && j != COLS - 5) {
		end++;
		j++;
	    }
	    if (j == COLS - 5) {

1766
		/* Don't print half a word if we've run out of space */
Chris Allegretta's avatar
Chris Allegretta committed
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
		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
1783
1784
    } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY &&
	     kbinput != NANO_EXIT_FKEY);
Chris Allegretta's avatar
Chris Allegretta committed
1785

1786
1787
    currshortcut = oldshortcut;

Chris Allegretta's avatar
Chris Allegretta committed
1788
    if (no_help_flag) {
1789
	blank_bottombars();
Chris Allegretta's avatar
Chris Allegretta committed
1790
	wrefresh(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
1791
	SET(NO_HELP);
1792
	window_init();
Chris Allegretta's avatar
Chris Allegretta committed
1793
    } else
1794
	bottombars(currshortcut);
Chris Allegretta's avatar
Chris Allegretta committed
1795
1796
1797

    curs_set(1);
    edit_refresh();
1798
    kp = keypad_on(edit, kp);
1799
    kp2 = keypad_on(bottomwin, kp2);
1800

1801
1802
#elif defined(DISABLE_HELP)
    nano_disabled_msg();
Chris Allegretta's avatar
Chris Allegretta committed
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
#endif

    return 1;
}

/* Dump the current file structure to stderr */
void dump_buffer(filestruct * inptr)
{
#ifdef DEBUG
    filestruct *fileptr;

    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"));

    fileptr = inptr;
    while (fileptr != NULL) {
1823
	fprintf(stderr, "(%d) %s\n", fileptr->lineno, fileptr->data);
Chris Allegretta's avatar
Chris Allegretta committed
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
	fflush(stderr);
	fileptr = fileptr->next;
    }
#endif				/* DEBUG */
}

void dump_buffer_reverse(filestruct * inptr)
{
#ifdef DEBUG
    filestruct *fileptr;

    fileptr = filebot;
    while (fileptr != NULL) {
1837
	fprintf(stderr, "(%d) %s\n", fileptr->lineno, fileptr->data);
Chris Allegretta's avatar
Chris Allegretta committed
1838
1839
1840
1841
1842
	fflush(stderr);
	fileptr = fileptr->prev;
    }
#endif				/* DEBUG */
}
Robert Siemborski's avatar
Robert Siemborski committed
1843

1844
/* Fix editbot, based on the assumption that edittop is correct */
1845
1846
void fix_editbot(void)
{
Robert Siemborski's avatar
Robert Siemborski committed
1847
1848
    int i;
    editbot = edittop;
1849
1850
    for (i = 0; (i <= editwinrows - 1) && (editbot->next != NULL)
	 && (editbot != filebot); i++, editbot = editbot->next);
Robert Siemborski's avatar
Robert Siemborski committed
1851
}
1852

Chris Allegretta's avatar
Chris Allegretta committed
1853
1854
1855
1856
1857
1858
/* highlight the current word being replaced or spell checked */
void do_replace_highlight(int highlight_flag, char *word)
{
    char *highlight_word = NULL;
    int x, y;

Chris Allegretta's avatar
Chris Allegretta committed
1859
1860
    highlight_word =
	mallocstrcpy(highlight_word, &current->data[current_x]);
Chris Allegretta's avatar
Chris Allegretta committed
1861
1862
    highlight_word[strlen(word)] = '\0';

1863
    /* adjust output when word extends beyond screen */
Chris Allegretta's avatar
Chris Allegretta committed
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875

    x = xplustabs();
    y = get_page_end_virtual(get_page_from_virtual(x)) + 1;

    if ((COLS - (y - x) + strlen(word)) > COLS) {
	highlight_word[y - x - 1] = '$';
	highlight_word[y - x] = '\0';
    }

    /* OK display the output */

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

Chris Allegretta's avatar
Chris Allegretta committed
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
    if (highlight_flag)
	wattron(edit, A_REVERSE);

    waddstr(edit, highlight_word);

    if (highlight_flag)
	wattroff(edit, A_REVERSE);

    free(highlight_word);
}

1888
#ifdef NANO_EXTRA
1889
#define CREDIT_LEN 52
1890
1891
void do_credits(void)
{
Chris Allegretta's avatar
Chris Allegretta committed
1892
    int i, j = 0, k, place = 0, start_x;
1893
1894
1895
1896
1897
1898
1899
    char *what;

    char *nanotext = _("The nano text editor");
    char *version = _("version ");
    char *brought = _("Brought to you by:");
    char *specialthx = _("Special thanks to:");
    char *fsf = _("The Free Software Foundation");
1900
    char *ncurses = _("For ncurses:");
1901
1902
1903
    char *anyonelse = _("and anyone else we forgot...");
    char *thankyou = _("Thank you for using nano!\n");

Chris Allegretta's avatar
Chris Allegretta committed
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
    char *credits[CREDIT_LEN] = { nanotext,
	version,
	VERSION,
	"",
	brought,
	"Chris Allegretta",
	"Jordi Mallach",
	"Adam Rogoyski",
	"Rob Siemborski",
	"Rocco Corsi",
1914
	"David Lawrence Ramsey",
Chris Allegretta's avatar
Chris Allegretta committed
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
	"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,
1940
1941
1942
1943
	"Thomas Dickey",
	"Pavel Curtis",
	"Zeyd Ben-Halim",
	"Eric S. Raymond",
Chris Allegretta's avatar
Chris Allegretta committed
1944
1945
1946
1947
1948
1949
	anyonelse,
	thankyou,
	"", "", "", "",
	"(c) 1999-2002 Chris Allegretta",
	"", "", "", "",
	"www.nano-editor.org"
1950
1951
1952
1953
1954
1955
    };

    curs_set(0);
    nodelay(edit, TRUE);
    blank_bottombars();
    mvwaddstr(topwin, 0, 0, hblank);
Chris Allegretta's avatar
Chris Allegretta committed
1956
1957
    blank_edit();
    wrefresh(edit);
1958
1959
1960
1961
    wrefresh(bottomwin);
    wrefresh(topwin);

    while (wgetch(edit) == ERR) {
Chris Allegretta's avatar
Chris Allegretta committed
1962
1963
	for (k = 0; k <= 1; k++) {
	    blank_edit();
Chris Allegretta's avatar
Chris Allegretta committed
1964
1965
	    for (i = editwinrows / 2 - 1; i >= (editwinrows / 2 - 1 - j);
		 i--) {
Chris Allegretta's avatar
Chris Allegretta committed
1966
1967
1968
1969
1970
1971
1972
		mvwaddstr(edit, i * 2 - k, 0, hblank);

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

1973
		start_x = COLS / 2 - strlen(what) / 2 - 1;
Chris Allegretta's avatar
Chris Allegretta committed
1974
1975
1976
1977
		mvwaddstr(edit, i * 2 - k, start_x, what);
	    }
	    usleep(700000);
	    wrefresh(edit);
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
	}
	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
1992
}
1993
#endif
1994

Chris Allegretta's avatar
Chris Allegretta committed
1995
int keypad_on(WINDOW * win, int newval)
1996
1997
{

1998
/* This is taken right from aumix.  Don't sue me. */
1999
#ifdef HAVE_USEKEYPAD
Chris Allegretta's avatar
Chris Allegretta committed
2000
    int old;
2001
2002

    old = win->_use_keypad;
2003
    keypad(win, newval);
2004
2005
    return old;
#else
2006
    keypad(win, newval);
2007
    return 1;
Chris Allegretta's avatar
Chris Allegretta committed
2008
#endif				/* HAVE_USEKEYPAD */
2009
2010

}