winio.c 35.1 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**************************************************************************
 *   winio.c                                                              *
 *                                                                        *
 *   Copyright (C) 1999 Chris Allegretta                                  *
 *   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 *
 *   the Free Software Foundation; either version 1, or (at your option)  *
 *   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>
Chris Allegretta's avatar
Chris Allegretta committed
26
27
28
29
30
31
32
33
34
35
36
#include "config.h"
#include "proto.h"
#include "nano.h"

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

37
38

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

/* 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
48
49
50
51
52
53
54
/* Window I/O */

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

int do_last_line(void)
{
    current = filebot;
    placewewant = 0;
    current_x = 0;
64
    edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    return 1;
}

/* Like xplustabs, but for a specifc index of a speficific filestruct */
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) {
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
	} else if (fileptr->data[i] & 0x80)
Chris Allegretta's avatar
Chris Allegretta committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	    /* Make 8 bit chars only 1 collumn! */
	    ;
	else if (fileptr->data[i] < 32)
	    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);
}


102
103
104
/* 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
105
{
106
    int i, tot = 1;
Chris Allegretta's avatar
Chris Allegretta committed
107
108
109
110

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

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

#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
123
124
    fprintf(stderr, _("actual_x_from_start for xplus=%d returned %d\n"),
	    xplus, i);
Chris Allegretta's avatar
Chris Allegretta committed
125
#endif
126
127
128
129
130
131
132
    return i - start;
}

/* Opposite of xplustabs */
inline int actual_x(filestruct * fileptr, int xplus)
{
    return actual_x_from_start(fileptr, xplus, 0);
Chris Allegretta's avatar
Chris Allegretta committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
}

/* a strlen with tabs factored in, similar to xplustabs() */
int strlenpt(char *buf)
{
    int i, tabs = 0;

    if (buf == NULL)
	return 0;

    for (i = 0; buf[i] != 0; i++) {
	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)
Chris Allegretta's avatar
Chris Allegretta committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
	    /* Make 8 bit chars only 1 collumn! */
	    ;
	else if (buf[i] < 32)
	    tabs++;
    }

    return tabs;
}


/* resets current_y based on the position of current and puts the cursor at 
   (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)
177
	wmove(edit, current_y, x);
Chris Allegretta's avatar
Chris Allegretta committed
178
    else
179
	wmove(edit, current_y, x -
Chris Allegretta's avatar
Chris Allegretta committed
180
	      get_page_start_virtual(get_page_from_virtual(x)));
Chris Allegretta's avatar
Chris Allegretta committed
181
182
183
184
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

}

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

223
224
225
/* Repaint the statusbar when getting a character in nanogetstr */
void nanoget_repaint(char *buf, char *inputbuf, int x)
{
226
227
    int len = strlen(buf);
    int wid = COLS - len;
228

229
230
231
232
    blank_statusbar();
    if (x <= COLS - 1) {
	/* Black magic */
	buf[len - 1] = ' ';
233

234
235
236
237
238
239
240
	mvwaddstr(bottomwin, 0, 0, buf);
	waddnstr(bottomwin, inputbuf, wid);
	wmove(bottomwin, 0, (x % COLS));
    }
    else {
	/* Black magic */
	buf[len - 1] = '$';
241

242
243
244
245
	mvwaddstr(bottomwin, 0, 0, buf);
	waddnstr(bottomwin, &inputbuf[wid * ((x - len) / (wid))], wid);
	wmove(bottomwin, 0, ((x - len) % wid) + len);
    }
246
247
}

Chris Allegretta's avatar
Chris Allegretta committed
248
/* Get the input from the kb, this should only be called from statusq */
249
250
int nanogetstr(int allowtabs, char *buf, char *def, shortcut s[], int slen, 
	       int start_x)
Chris Allegretta's avatar
Chris Allegretta committed
251
252
{
    int kbinput = 0, j = 0, x = 0, xend;
253
    int x_left = 0, inputlen, tabbed = 0;
254
    char *inputbuf;
255
#ifndef DISABLE_TABCOMP
256
257
    int shift = 0;
#endif
258
259
260
    
    inputbuf = nmalloc(strlen(def) + 1);
    inputbuf[0] = 0;
Chris Allegretta's avatar
Chris Allegretta committed
261
262
263
264
265

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

    /* Get the input! */
266
    if (strlen(def) > 0)
Chris Allegretta's avatar
Chris Allegretta committed
267
	strcpy(inputbuf, def);
268
269

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

271
272
273
    /* Make sure any editor screen updates are displayed before getting input */
    wrefresh(edit);

Chris Allegretta's avatar
Chris Allegretta committed
274
275
276
    while ((kbinput = wgetch(bottomwin)) != 13) {
	for (j = 0; j <= slen - 1; j++) {
	    if (kbinput == s[j].val) {
277
278
279
280

		/* We shouldn't discard the answer it gave, just because
		   we hit a keystroke, GEEZ! */
		answer = mallocstrcpy(answer, inputbuf);
281
		free(inputbuf);
Chris Allegretta's avatar
Chris Allegretta committed
282
283
284
285
286
		return s[j].val;
	    }
	}
	xend = strlen(buf) + strlen(inputbuf);

Chris Allegretta's avatar
Chris Allegretta committed
287
288
289
	if (kbinput != '\t')
	    tabbed = 0;

Chris Allegretta's avatar
Chris Allegretta committed
290
	switch (kbinput) {
291
292
	    /* Stuff we want to equate with <enter>, ASCII 13 */
	case 343:
293
294
	    ungetch(13);	/* Enter on iris-ansi $TERM, sometimes */
	    break;
295
	/* Stuff we want to ignore */
296
297
298
#ifdef PDCURSES
	case 541:
	case 542:
299
	case 543:			/* Right ctrl again */
300
	case 544:
301
	case 545: 			/* Right alt again */
302
#endif
303
		break;
Chris Allegretta's avatar
Chris Allegretta committed
304
305
	case KEY_HOME:
	    x = x_left;
306
	    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
307
308
309
	    break;
	case KEY_END:
	    x = x_left + strlen(inputbuf);
310
	    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	    break;
	case KEY_RIGHT:

	    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;
	    }
325
	    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
326
327
328
329
330
	    break;
	case NANO_CONTROL_K:
	case NANO_CONTROL_U:
	    *inputbuf = 0;
	    x = x_left;
331
	    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
	    break;
	case KEY_BACKSPACE:
	case KEY_DC:
	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
347
348
349
350
	    if (x > strlen(buf))
		x--;
	    nanoget_repaint(buf, inputbuf, x);
	    break;
351
#ifndef DISABLE_TABCOMP
Chris Allegretta's avatar
Chris Allegretta committed
352
	case NANO_CONTROL_I:
353
	    if (allowtabs) {
354
355
		shift = 0;
		inputbuf = input_tab(inputbuf, (x - x_left), 
356
				&tabbed, &shift);
357
		x += shift;
358
359
		if (x - x_left > strlen(inputbuf))
		    x = strlen(inputbuf) + x_left;
360
361
		nanoget_repaint(buf, inputbuf, x);
	    }
Chris Allegretta's avatar
Chris Allegretta committed
362
	    break;
363
#endif
Chris Allegretta's avatar
Chris Allegretta committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
	case KEY_LEFT:
	    if (x > strlen(buf))
		x--;
	    wmove(bottomwin, 0, x);
	    break;
	case KEY_UP:
	case KEY_DOWN:
	    break;

	case 27:
	    switch (kbinput = wgetch(edit)) {
	    case 79:
		switch (kbinput = wgetch(edit)) {
		case 70:
		    x = x_left + strlen(inputbuf);
379
		    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
380
381
382
		    break;
		case 72:
		    x = x_left;
383
		    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
		    break;
		}
		break;
	    case 91:
		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;
		case 49:
		    x = x_left;
401
		    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
402
403
404
405
406
407
408
409
410
		    goto skip_126;
		case 51:
		    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;
		    }
411
		    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
412
413
414
		    goto skip_126;
		case 52:
		    x = x_left + strlen(inputbuf);
415
		    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
416
417
418
419
420
421
422
423
424
425
		    goto skip_126;
		  skip_126:
		    nodelay(edit, TRUE);
		    kbinput = wgetch(edit);
		    if (kbinput == 126 || kbinput == ERR)
			kbinput = -1;
		    nodelay(edit, FALSE);
		    break;
		}
	    }
426
	    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
427
428
429
430
431
	    break;

	default:
	    if (kbinput < 32)
		break;
432
433
434
435
436
437
438
439
440

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

	    memmove(&inputbuf[x - x_left + 1], 
			&inputbuf[x - x_left],
                        inputlen - (x - x_left) + 1);
	    inputbuf[x - x_left] = kbinput;

Chris Allegretta's avatar
Chris Allegretta committed
441
442
	    x++;

443
	    nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
444
445
446
447
448
449
450
#ifdef DEBUG
	    fprintf(stderr, _("input \'%c\' (%d)\n"), kbinput, kbinput);
#endif
	}
	wrefresh(bottomwin);
    }

451
    answer = mallocstrcpy(answer, inputbuf);
452
    free(inputbuf);
Chris Allegretta's avatar
Chris Allegretta committed
453

454
455
    /* Now that the text is editable instead of bracketed, we have to 
       check for answer == def, instead of answer == "" */
456
457
    if  (((ISSET(PICO_MODE)) && !strcmp(answer, "")) || 
	((!ISSET(PICO_MODE)) && !strcmp(answer, def)))
Chris Allegretta's avatar
Chris Allegretta committed
458
459
460
461
462
463
464
465
466
467
468
469
	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
470
void titlebar(char *path)
Chris Allegretta's avatar
Chris Allegretta committed
471
472
{
    int namelen, space;
Chris Allegretta's avatar
Chris Allegretta committed
473
474
475
476
    char *what = path;

    if (path == NULL)
	what = filename;
Chris Allegretta's avatar
Chris Allegretta committed
477
478
479
480
481
482
483

    horizbar(topwin, 0);
    wattron(topwin, A_REVERSE);
    mvwaddstr(topwin, 0, 3, VERMSG);

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

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

Chris Allegretta's avatar
Chris Allegretta committed
486
    if (!strcmp(what, ""))
487
	mvwaddstr(topwin, 0, COLS / 2 - 6, _("New Buffer"));
Chris Allegretta's avatar
Chris Allegretta committed
488
489
    else {
	if (namelen > space) {
Chris Allegretta's avatar
Chris Allegretta committed
490
491
492
493
494
	    if (path == NULL)
		waddstr(topwin, _("  File: ..."));
	    else
		waddstr(topwin, _("   DIR: ..."));
	    waddstr(topwin, &what[namelen - space]);
Chris Allegretta's avatar
Chris Allegretta committed
495
	} else {
Chris Allegretta's avatar
Chris Allegretta committed
496
	    if (path == NULL)
497
		mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1), "File: ");
Chris Allegretta's avatar
Chris Allegretta committed
498
	    else
499
	 	mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1), " DIR: ");
Chris Allegretta's avatar
Chris Allegretta committed
500
	    waddstr(topwin, what);
Chris Allegretta's avatar
Chris Allegretta committed
501
502
503
504
505
506
507
508
509
510
511
512
513
	}
    }
    if (ISSET(MODIFIED))
	mvwaddstr(topwin, 0, COLS - 10, _("Modified"));
    wattroff(topwin, A_REVERSE);
    wrefresh(topwin);
    reset_cursor();
}

void onekey(char *keystroke, char *desc)
{
    char description[80];

514
    snprintf(description, 12, " %-10s", desc);
Chris Allegretta's avatar
Chris Allegretta committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
    wattron(bottomwin, A_REVERSE);
    waddstr(bottomwin, keystroke);
    wattroff(bottomwin, A_REVERSE);
    waddstr(bottomwin, description);
}

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

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

void bottombars(shortcut s[], int slen)
{
    int i, j, k;
    char keystr[10];

    if (ISSET(NO_HELP))
	return;

    /* Determine how many extra spaces are needed to fill the bottom of the screen */
    k = COLS / 6 - 13;

    clear_bottomwin();
    wmove(bottomwin, 1, 0);
    for (i = 0; i <= slen - 1; i += 2) {
544
	snprintf(keystr, 10, "^%c", s[i].val + 64);
Chris Allegretta's avatar
Chris Allegretta committed
545
546
547
548
549
550
551
552
	onekey(keystr, s[i].desc);

	for (j = 0; j < k; j++)
	    waddch(bottomwin, ' ');
    }

    wmove(bottomwin, 2, 0);
    for (i = 1; i <= slen - 1; i += 2) {
553
	snprintf(keystr, 10, "^%c", s[i].val + 64);
Chris Allegretta's avatar
Chris Allegretta committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
	onekey(keystr, s[i].desc);

	for (j = 0; j < k; j++)
	    waddch(bottomwin, ' ');
    }

    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
569
	titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
570
571
572
573
	wrefresh(topwin);
    }
}

574
575
576
577
/* 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 */
/* characters is viewable (ie: page 1 shows from 1 to COLS) */
Chris Allegretta's avatar
Chris Allegretta committed
578
579
inline int get_page_from_virtual(int virtual)
{
580
    int page = 2;
Chris Allegretta's avatar
Chris Allegretta committed
581

Chris Allegretta's avatar
Chris Allegretta committed
582
583
    if (virtual <= COLS - 2)
	return 1;
584
    virtual -= (COLS - 2);
585
586

    while (virtual > COLS - 2 - 7) {
587
	virtual -= (COLS - 2 - 7);
588
	page++;
Chris Allegretta's avatar
Chris Allegretta committed
589
590
    }

591
592
593
    return page;
}

594
/* The inverse of the above function */
Chris Allegretta's avatar
Chris Allegretta committed
595
596
inline int get_page_start_virtual(int page)
{
597
598
    int virtual;
    virtual = --page * (COLS - 7);
Chris Allegretta's avatar
Chris Allegretta committed
599
600
    if (page)
	virtual -= 2 * page - 1;
601
    return virtual;
602
603
}

Chris Allegretta's avatar
Chris Allegretta committed
604
605
inline int get_page_end_virtual(int page)
{
606
607
608
609
    return get_page_start_virtual(page) + COLS - 1;
}

#ifndef NANO_SMALL
610
611
612
/* This takes care of the case where there is a mark that covers only */
/* the current line. */

613
/* It expects a line with no tab characers (ie: the type that edit_add */
614
/* deals with */
Chris Allegretta's avatar
Chris Allegretta committed
615
616
void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
			 int virt_cur_x, int this_page)
617
{
618
619
620
621
622
623
624
625
    /*
     * 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
     * turn (for those that are currently visible, of course
     *
     * 3 start points: 0 -> begin, begin->end, end->strlen(data)
     *    in data    :    pre          sel           post        
     */
Chris Allegretta's avatar
Chris Allegretta committed
626
    int this_page_start = get_page_start_virtual(this_page),
627
	this_page_end = get_page_end_virtual(this_page);
628
629

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

    /* now fix the start locations & lengths according to the cursor's 
     * position (ie: our page) */
Chris Allegretta's avatar
Chris Allegretta committed
634
    if (pre_data_len < this_page_start)
635
636
637
	pre_data_len = 0;
    else
	pre_data_len -= this_page_start;
638

Chris Allegretta's avatar
Chris Allegretta committed
639
    if (begin < this_page_start)
640
641
	begin = this_page_start;

Chris Allegretta's avatar
Chris Allegretta committed
642
    if (end < this_page_start)
643
	end = this_page_start;
644

Chris Allegretta's avatar
Chris Allegretta committed
645
    if (begin > this_page_end)
646
	begin = this_page_end;
647

Chris Allegretta's avatar
Chris Allegretta committed
648
    if (end > this_page_end)
649
	end = this_page_end;
650

651
    /* Now calculate the lengths */
652
653
    sel_data_len = end - begin;
    post_data_len = this_page_end - end;
654

655
    /* Paint this line! */
656
    mvwaddnstr(edit, y, 0, &fileptr->data[this_page_start], pre_data_len);
657
    wattron(edit, A_REVERSE);
658
    mvwaddnstr(edit, y, begin - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
659
	       &fileptr->data[begin], sel_data_len);
660
    wattroff(edit, A_REVERSE);
661
    mvwaddnstr(edit, y, end - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
662
	       &fileptr->data[end], post_data_len);
Chris Allegretta's avatar
Chris Allegretta committed
663
664
665
}
#endif

666
667
668
669
/* 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
670
671
 * line */
void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
672
	      int virt_mark_beginx, int this_page)
Chris Allegretta's avatar
Chris Allegretta committed
673
674
{
#ifndef NANO_SMALL
675
676
    /* There are quite a few cases that could take place, we'll deal
     * with them each in turn */
677
    if (ISSET(MARK_ISSET)
678
	&& !((fileptr->lineno > mark_beginbuf->lineno
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
	      && fileptr->lineno > current->lineno)
	     || (fileptr->lineno < mark_beginbuf->lineno
		 && fileptr->lineno < current->lineno))) {
	/* If we get here we are on a line that is atleast
	 * 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 */
	    wattron(edit, A_REVERSE);
	    mvwaddnstr(edit, yval, 0, fileptr->data, COLS);
	    wattroff(edit, A_REVERSE);
	} 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) {
	    /*
	     * we're updating the line that was first marked
	     * but we're not currently on it.  So we want to
	     * figur out which half to invert based on our
	     * relative line numbers.
	     *
	     * 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
	     */
	    int target;

	    if (mark_beginbuf->lineno > current->lineno)
Chris Allegretta's avatar
Chris Allegretta committed
717
		wattron(edit, A_REVERSE);
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

	    target =
		(virt_mark_beginx <
		 COLS - 1) ? virt_mark_beginx : COLS - 1;

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

	    if (mark_beginbuf->lineno < current->lineno)
		wattron(edit, A_REVERSE);
	    else
		wattroff(edit, A_REVERSE);

	    target = (COLS - 1) - virt_mark_beginx;
	    if (target < 0)
		target = 0;

	    mvwaddnstr(edit, yval, virt_mark_beginx,
		       &fileptr->data[virt_mark_beginx], target);

	    if (mark_beginbuf->lineno < current->lineno)
		wattroff(edit, A_REVERSE);

	} else if (fileptr == current) {
	    /* we're on the cursors line, but it's not the first
	     * 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);

	    if (mark_beginbuf->lineno < current->lineno)
		wattron(edit, A_REVERSE);

	    if (virt_cur_x > COLS - 2) {
		mvwaddnstr(edit, yval, 0,
			   &fileptr->data[this_page_start],
			   virt_cur_x - this_page_start);
	    } else {
		mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
	    }

	    if (mark_beginbuf->lineno > current->lineno)
		wattron(edit, A_REVERSE);
	    else
Chris Allegretta's avatar
Chris Allegretta committed
760
761
		wattroff(edit, A_REVERSE);

762
763
764
765
766
767
768
769
770
771
	    if (virt_cur_x > COLS - 2)
		mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
			   &fileptr->data[virt_cur_x],
			   this_page_end - virt_cur_x);
	    else
		mvwaddnstr(edit, yval, virt_cur_x,
			   &fileptr->data[virt_cur_x], COLS - virt_cur_x);

	    if (mark_beginbuf->lineno > current->lineno)
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
772
773
774
775
	}

    } else
#endif
776
	/* Just paint the string (no mark on this line) */
777
	mvwaddnstr(edit, yval, 0, &fileptr->data[start],
778
		   get_page_end_virtual(this_page) - start + 1);
Chris Allegretta's avatar
Chris Allegretta committed
779
780
781
}

/*
782
783
 * Just update one line in the edit buffer.  Basically a wrapper for
 * edit_add
Chris Allegretta's avatar
Chris Allegretta committed
784
785
786
787
788
789
790
 *
 * index gives is a place in the string to update starting from.
 * Likely args are current_x or 0.
 */
void update_line(filestruct * fileptr, int index)
{
    filestruct *filetmp;
791
792
793
    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
794
    int i, pos, len, page;
Chris Allegretta's avatar
Chris Allegretta committed
795

Chris Allegretta's avatar
Chris Allegretta committed
796
797
    if (!fileptr)
	return;
798

799
    /* First, blank out the line (at a minimum) */
Chris Allegretta's avatar
Chris Allegretta committed
800
801
802
803
804
    for (filetmp = edittop; filetmp != fileptr && filetmp != editbot;
	 filetmp = filetmp->next)
	line++;

    mvwaddstr(edit, line, 0, hblank);
805
806
807
808
809
810

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

    realdata = fileptr->data;
    len = strlen(realdata);
Chris Allegretta's avatar
Chris Allegretta committed
811
    fileptr->data = nmalloc(xpt(fileptr, len) + 1);
812
813

    pos = 0;
Chris Allegretta's avatar
Chris Allegretta committed
814
815
    for (i = 0; i < len; i++) {
	if (realdata[i] == '\t') {
816
817
	    do {
		fileptr->data[pos++] = ' ';
Chris Allegretta's avatar
Chris Allegretta committed
818
819
820
821
		if (i < current_x)
		    virt_cur_x++;
		if (i < mark_beginx)
		    virt_mark_beginx++;
822
	    } while (pos % tabsize);
823
	    /* must decrement once to account for tab-is-one-character */
Chris Allegretta's avatar
Chris Allegretta committed
824
825
826
827
	    if (i < current_x)
		virt_cur_x--;
	    if (i < mark_beginx)
		virt_mark_beginx--;
828
829
	} else if (realdata[i] >= 1 && realdata[i] <= 26) {
	    /* Treat control characters as ^letter */
830
831
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = realdata[i] + 64;
832
833
834
835
836
837
	} else {
	    fileptr->data[pos++] = realdata[i];
	}
    }

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

839
840
    /* Now, Paint the line */
    if (current == fileptr && index > COLS - 2) {
841
842
843
	/* This handles when the current line is beyond COLS */
	/* It requires figureing out what page we're at      */
	page = get_page_from_virtual(index);
844
	col = get_page_start_virtual(page);
Chris Allegretta's avatar
Chris Allegretta committed
845

846
	edit_add(filetmp, line, col, virt_cur_x, virt_mark_beginx, page);
Chris Allegretta's avatar
Chris Allegretta committed
847
848
	mvwaddch(edit, line, 0, '$');

Chris Allegretta's avatar
Chris Allegretta committed
849
	if (strlenpt(fileptr->data) > get_page_end_virtual(page) + 1)
Chris Allegretta's avatar
Chris Allegretta committed
850
	    mvwaddch(edit, line, COLS - 1, '$');
851
    } else {
852
853
854
	/* 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
855

856
	if (strlenpt(&filetmp->data[col]) > COLS)
Chris Allegretta's avatar
Chris Allegretta committed
857
	    mvwaddch(edit, line, COLS - 1, '$');
858
    }
859
860
861
862
863

    /* Clean up our mess */
    tmp = fileptr->data;
    fileptr->data = realdata;
    free(tmp);
Chris Allegretta's avatar
Chris Allegretta committed
864
865
866
867
868
869
870
871
872
873
874
}

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
875
    static int noloop = 0;
876
    int lines = 0, i = 0, currentcheck = 0;
Chris Allegretta's avatar
Chris Allegretta committed
877
878
879
880
881
882
883
884
885
886
    filestruct *temp, *hold = current;

    if (current == NULL)
	return;

    temp = edittop;

    while (lines <= editwinrows - 1 && lines <= totlines && temp != NULL) {
	hold = temp;
	update_line(temp, current_x);
887
888
889
	if (temp == current)
	    currentcheck = 1;

Chris Allegretta's avatar
Chris Allegretta committed
890
891
892
	temp = temp->next;
	lines++;
    }
Chris Allegretta's avatar
Chris Allegretta committed
893
894
    /* If noloop == 1, then we already did an edit_update without finishing
       this function.  So we don't run edit_update again */
895
    if (!currentcheck && !noloop) {	/* Then current has run off the screen... */
896
	edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
897
	noloop = 1;
898
    } else if (noloop)
Chris Allegretta's avatar
Chris Allegretta committed
899
	noloop = 0;
Chris Allegretta's avatar
Chris Allegretta committed
900
901
902
903
904
905
906
907
908
909
910
911

    if (lines <= editwinrows - 1)
	while (lines <= editwinrows - 1) {
	    mvwaddstr(edit, lines, i, hblank);
	    lines++;
	}
    if (temp == NULL)
	editbot = hold;
    else
	editbot = temp;
}

912
913
914
915
916
917
918
919
920
921
/*
 * Same as above, but touch the window first so everything is redrawn.
 */
void edit_refresh_clearok(void)
{
    clearok(edit, TRUE);
    edit_refresh();
    clearok(edit, FALSE);
}

Chris Allegretta's avatar
Chris Allegretta committed
922
923
924
925
/*
 * Nice generic routine to update the edit buffer given a pointer to the
 * file struct =) 
 */
926
void edit_update(filestruct * fileptr, int topmidbot)
Chris Allegretta's avatar
Chris Allegretta committed
927
{
928
    int i = 0;
Chris Allegretta's avatar
Chris Allegretta committed
929
930
931
932
933
934
    filestruct *temp;

    if (fileptr == NULL)
	return;

    temp = fileptr;
935
    if (topmidbot == 2);
936
937
938
939
940
941
    else if (topmidbot == 0)
	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
942

943
944
    edittop = temp;
    fix_editbot();
Chris Allegretta's avatar
Chris Allegretta committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976

    edit_refresh();
}

/* This function updates current based on where current_y is, reset_cursor 
   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
 * otherwise, the valid shortcut key caught, Def is any editable text we
 * want to put up by default.
977
978
 *
 * New arg tabs tells whether or not to allow tab completion.
Chris Allegretta's avatar
Chris Allegretta committed
979
 */
980
int statusq(int tabs, shortcut s[], int slen, char *def, char *msg, ...)
Chris Allegretta's avatar
Chris Allegretta committed
981
982
983
984
985
986
987
988
989
990
{
    va_list ap;
    char foo[133];
    int ret;

    bottombars(s, slen);

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

    wattron(bottomwin, A_REVERSE);
994
    ret = nanogetstr(tabs, foo, def, s, slen, (strlen(foo) + 3));
Chris Allegretta's avatar
Chris Allegretta committed
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
    wattroff(bottomwin, A_REVERSE);

    switch (ret) {

    case NANO_FIRSTLINE_KEY:
	do_first_line();
	break;
    case NANO_LASTLINE_KEY:
	do_last_line();
	break;
    case NANO_CANCEL_KEY:
	return -1;
    default:
1008
	blank_statusbar();
Chris Allegretta's avatar
Chris Allegretta committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
    }

#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];
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
    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 */

    /* Yes, no and all are strings of any length.  Each string consists of
	all characters accepted as a valid character for that value.
	The first value will be the one displayed in the shortcuts. */
    yesstr = _("Yy");
    nostr = _("Nn");
    allstr = _("Aa");
Chris Allegretta's avatar
Chris Allegretta committed
1038
1039
1040
1041
1042
1043
1044

    /* Write the bottom of the screen */
    clear_bottomwin();
    wattron(bottomwin, A_REVERSE);
    blank_statusbar_refresh();
    wattroff(bottomwin, A_REVERSE);

Jordi Mallach's avatar
   
Jordi Mallach committed
1045
    /* Remove gettext call for keybindings until we clear the thing up */
Chris Allegretta's avatar
Chris Allegretta committed
1046
1047
    if (!ISSET(NO_HELP)) {
	wmove(bottomwin, 1, 0);
1048
1049
1050
1051
1052
1053
1054
1055

	snprintf(shortstr, 3, " %c", yesstr[0]);
	onekey(shortstr, _("Yes"));

	if (all) {
	    snprintf(shortstr, 3, " %c", allstr[0]);
	    onekey(shortstr, _("All"));
	}
Chris Allegretta's avatar
Chris Allegretta committed
1056
	wmove(bottomwin, 2, 0);
1057
1058
1059
1060

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

Jordi Mallach's avatar
   
Jordi Mallach committed
1061
	onekey("^C", _("Cancel"));
Chris Allegretta's avatar
Chris Allegretta committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
    }
    va_start(ap, msg);
    vsnprintf(foo, 132, msg, ap);
    va_end(ap);
    wattron(bottomwin, A_REVERSE);
    mvwaddstr(bottomwin, 0, 0, foo);
    wattroff(bottomwin, A_REVERSE);
    wrefresh(bottomwin);

    if (leavecursor == 1)
	reset_cursor();

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

	switch (kbinput) {
	case NANO_CONTROL_C:
	    ok = -2;
	    break;
1081
1082
1083
1084
1085
1086
	default:

	    /* Look for the kbinput in the yes, no and (optinally) all str */
	    for (i = 0; yesstr[i] != 0 && yesstr[i] != kbinput; i++)
		;
	    if (yesstr[i] != 0) {
1087
		ok = 1;
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
	 	break;
	    }

	    for (i = 0; nostr[i] != 0 && nostr[i] != kbinput; i++)
		;
	    if (nostr[i] != 0) {
		ok = 0;
	 	break;
	    }

	    if (all) {
	        for (i = 0; allstr[i] != 0 && allstr[i] != kbinput; i++)
		    ;
		if (allstr[i] != 0) {
		    ok = 2;
	 	    break;
		}
	    }
Chris Allegretta's avatar
Chris Allegretta committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
	}
    }

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

1128
    start_x = COLS / 2 - strlen(foo) / 2 - 1;
Chris Allegretta's avatar
Chris Allegretta committed
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165

    /* Blank out line */
    blank_statusbar();

    wmove(bottomwin, 0, start_x);

    wattron(bottomwin, A_REVERSE);

    waddstr(bottomwin, "[ ");
    waddstr(bottomwin, foo);
    waddstr(bottomwin, " ]");
    wattroff(bottomwin, A_REVERSE);
    wrefresh(bottomwin);

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

void display_main_list(void)
{
    bottombars(main_list, MAIN_VISIBLE);
}

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);
1166
    edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
1167
    titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
    return 1;
}

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

int do_cursorpos(void)
{
    filestruct *fileptr;
Chris Allegretta's avatar
Chris Allegretta committed
1180
1181
    float linepct = 0.0, bytepct = 0.0;
    int i = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1182
1183
1184
1185

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

Chris Allegretta's avatar
Chris Allegretta committed
1186
1187
    for (fileptr = fileage; fileptr != current && fileptr != NULL;
	 fileptr = fileptr->next)
Chris Allegretta's avatar
Chris Allegretta committed
1188
	i += strlen(fileptr->data) + 1;
Chris Allegretta's avatar
Chris Allegretta committed
1189
1190
1191
1192

    if (fileptr == NULL)
	return -1;

Chris Allegretta's avatar
Chris Allegretta committed
1193
    i += current_x;
Chris Allegretta's avatar
Chris Allegretta committed
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215

    if (totlines > 0)
	linepct = 100 * current->lineno / totlines;

    if (totsize > 0)
	bytepct = 100 * i / totsize;

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

    statusbar(_("line %d of %d (%.0f%%), character %d of %d (%.0f%%)"),
	      current->lineno, totlines, linepct, i, totsize, bytepct);
    reset_cursor();
    return 1;
}

/* Our broken, non-shortcut list compliant help function.
   But hey, it's better than nothing, and it's dynamic! */
int do_help(void)
{
1216
#ifndef DISABLE_HELP
Chris Allegretta's avatar
Chris Allegretta committed
1217
    char *ptr = help_text, *end;
1218
    int i, j, row = 0, page = 1, kbinput = 0, no_more = 0, kp;
Chris Allegretta's avatar
Chris Allegretta committed
1219
1220
1221
1222
1223
1224
    int no_help_flag = 0;

    blank_edit();
    curs_set(0);
    blank_statusbar();

1225
1226
    kp = keypad_on(edit, 1);

Chris Allegretta's avatar
Chris Allegretta committed
1227
1228
    if (ISSET(NO_HELP)) {

1229
1230
	/* Well if we're going to do this, we should at least
	   do it the right way */
Chris Allegretta's avatar
Chris Allegretta committed
1231
	no_help_flag = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1232
	UNSET(NO_HELP);
1233
	window_init();
Chris Allegretta's avatar
Chris Allegretta committed
1234
	bottombars(help_list, HELP_LIST_LEN);
1235

Chris Allegretta's avatar
Chris Allegretta committed
1236
    } else
Chris Allegretta's avatar
Chris Allegretta committed
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
	bottombars(help_list, HELP_LIST_LEN);

    do {
	ptr = help_text;
	switch (kbinput) {
	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;
	}

	/* Calculate where in the text we should be based on the page */
	for (i = 1; i < page; i++) {
	    row = 0;
	    j = 0;
1265
1266

	    while (row < editwinrows - 2 && *ptr != '\0') {
Chris Allegretta's avatar
Chris Allegretta committed
1267
1268
1269
1270
1271
1272
1273
1274
1275
		if (*ptr == '\n' || j == COLS - 5) {
		    j = 0;
		    row++;
		}
		ptr++;
		j++;
	    }
	}

1276
	if (i > 1) {
1277

1278
1279
	}

Chris Allegretta's avatar
Chris Allegretta committed
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
	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) {

		/* Don't print half a word if we've run of of space */
		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
1307
1308
1309
    } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY && 
      kbinput != NANO_EXIT_FKEY);

Chris Allegretta's avatar
Chris Allegretta committed
1310
    if (no_help_flag) {
1311
	blank_bottombars();
Chris Allegretta's avatar
Chris Allegretta committed
1312
	wrefresh(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
1313
	SET(NO_HELP);
1314
1315
1316
	window_init();
    }
    display_main_list();
Chris Allegretta's avatar
Chris Allegretta committed
1317
1318
1319

    curs_set(1);
    edit_refresh();
1320
1321
    kp = keypad_on(edit, kp);

1322
1323
#elif defined(DISABLE_HELP)
    nano_disabled_msg();
Chris Allegretta's avatar
Chris Allegretta committed
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
#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) {
	fprintf(stderr, "(%ld) %s\n", fileptr->lineno, fileptr->data);
	fflush(stderr);
	fileptr = fileptr->next;
    }
#endif				/* DEBUG */
}

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

    fileptr = filebot;
    while (fileptr != NULL) {
	fprintf(stderr, "(%ld) %s\n", fileptr->lineno, fileptr->data);
	fflush(stderr);
	fileptr = fileptr->prev;
    }
#endif				/* DEBUG */
}
Robert Siemborski's avatar
Robert Siemborski committed
1364
1365

/* Fix editbot based on the assumption that edittop is correct */
1366
1367
void fix_editbot(void)
{
Robert Siemborski's avatar
Robert Siemborski committed
1368
1369
    int i;
    editbot = edittop;
1370
1371
    for (i = 0; (i <= editwinrows - 1) && (editbot->next != NULL)
	 && (editbot != filebot); i++, editbot = editbot->next);
Robert Siemborski's avatar
Robert Siemborski committed
1372
}
1373

Chris Allegretta's avatar
Chris Allegretta committed
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
/* 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;

    highlight_word = mallocstrcpy(highlight_word, &current->data[current_x]);
    highlight_word[strlen(word)] = '\0';

    /* adjust output when word extends beyond screen*/

    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();
    
    if (highlight_flag)
	wattron(edit, A_REVERSE);

    waddstr(edit, highlight_word);

    if (highlight_flag)
	wattroff(edit, A_REVERSE);

    free(highlight_word);
}

1408
#ifdef NANO_EXTRA
1409
#define CREDIT_LEN 45
1410
1411
void do_credits(void)
{
Chris Allegretta's avatar
Chris Allegretta committed
1412
    int i, j = 0, k, place = 0, start_x;
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
    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");
    char *ncurses = _("Pavel Curtis, Zeyd Ben-Halim and Eric S. Raymond for ncurses");
    char *anyonelse = _("and anyone else we forgot...");
    char *thankyou = _("Thank you for using nano!\n");

    char *credits[CREDIT_LEN] = {nanotext, 
			version, 
			VERSION, 
			"",
			brought,
			"Chris Allegretta",
			"Jordi Mallach",
			"Adam Rogoyski",
			"Rob Siemborski",
			"Rocco Corsi",
			"Ken Tyler",
			"Sven Guckes",
			"Florian Knig",
			"Pauli Virtanen",
			"Daniele Medri",
			"Clement Laforet",
			"Tedi Heriyanto",
Chris Allegretta's avatar
Chris Allegretta committed
1441
			"Bill Soudan",
1442
			"Christian Weisgerber",
Chris Allegretta's avatar
Chris Allegretta committed
1443
			"Erik Andersen",
1444
1445
			"Big Gaute",
			"Joshua Jensen",
Chris Allegretta's avatar
Chris Allegretta committed
1446
			"Ryan Krebs",
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
			"",
			specialthx,
			"Plattsburgh State University",
			"Benet Laboratories",
			"Amy Allegretta",
			"Linda Young",
			"Jeremy Robichaud",
			"Richard Kolb II",
			fsf,
			"Linus Torvalds",
			ncurses,
			anyonelse,
			thankyou,
			"", "", "", "",
			"(c) 2000 Chris Allegretta",
			"", "", "", "",
			"www.nano-editor.org"
    };

    curs_set(0);
    nodelay(edit, TRUE);
    blank_bottombars();
    mvwaddstr(topwin, 0, 0, hblank);
Chris Allegretta's avatar
Chris Allegretta committed
1470
1471
    blank_edit();
    wrefresh(edit);
1472
1473
1474
1475
    wrefresh(bottomwin);
    wrefresh(topwin);

    while (wgetch(edit) == ERR) {
Chris Allegretta's avatar
Chris Allegretta committed
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
	for (k = 0; k <= 1; k++) {
	    blank_edit();
	    for (i = editwinrows / 2 - 1; i >= (editwinrows / 2 - 1 - j); i--) {
		mvwaddstr(edit, i * 2 - k, 0, hblank);

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

1486
		start_x = COLS / 2 - strlen(what) / 2 - 1;
Chris Allegretta's avatar
Chris Allegretta committed
1487
1488
1489
1490
		mvwaddstr(edit, i * 2 - k, start_x, what);
	    }
	    usleep(700000);
	    wrefresh(edit);
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
	}
	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();
 }
#endif
1507

1508
int             keypad_on(WINDOW * win, int newval)
1509
1510
1511
1512
1513
1514
1515
{

/* This is taken right from aumix.  Don't sue me */
#ifdef HAVE_USEKEYPAD
    int             old;

    old = win->_use_keypad;
1516
    keypad(win, newval);
1517
1518
    return old;
#else
1519
    keypad(win, newval);
1520
1521
1522
1523
1524
1525
1526
    return 1;
#endif                          /* HAVE_USEKEYPAD */

}