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

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

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

39
40

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

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

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

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

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	if (i > strlen(fileptr->data))
	    i = strlen(fileptr->data);

	/* see if we're in the x-plus-tabs column of xplus; if not, look
	   for the closest column to it */
	if (xpt(fileptr, i) < xplus) {
	    while (xpt(fileptr, i) < xplus && i < strlen(fileptr->data))
	        i++;
	}
	else if (xpt(fileptr, i) > xplus) {
	    while (xpt(fileptr, i) > xplus && i > start)
	        i--;
	}

Chris Allegretta's avatar
Chris Allegretta committed
136
#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
137
138
    fprintf(stderr, _("actual_x_from_start for xplus=%d returned %d\n"),
	    xplus, i);
Chris Allegretta's avatar
Chris Allegretta committed
139
#endif
Chris Allegretta's avatar
Chris Allegretta committed
140

141
142
143
144
    return i - start;
}

/* Opposite of xplustabs */
145
int actual_x(filestruct * fileptr, int xplus)
146
147
{
    return actual_x_from_start(fileptr, xplus, 0);
Chris Allegretta's avatar
Chris Allegretta committed
148
149
150
}

/* a strlen with tabs factored in, similar to xplustabs() */
151
int strnlenpt(char *buf, int size)
Chris Allegretta's avatar
Chris Allegretta committed
152
153
154
155
156
157
{
    int i, tabs = 0;

    if (buf == NULL)
	return 0;

158
    for (i = 0; i < size; i++) {
Chris Allegretta's avatar
Chris Allegretta committed
159
160
161
	tabs++;

	if (buf[i] == NANO_CONTROL_I) {
162
	    if (tabs % tabsize == 0);
Chris Allegretta's avatar
Chris Allegretta committed
163
	    else
164
		tabs += tabsize - (tabs % tabsize);
Chris Allegretta's avatar
Chris Allegretta committed
165
	} else if (buf[i] & 0x80)
166
	    /* Make 8 bit chars only 1 column! */
Chris Allegretta's avatar
Chris Allegretta committed
167
	    ;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
168
	else if (iscntrl((int) buf[i]))
Chris Allegretta's avatar
Chris Allegretta committed
169
170
171
172
173
174
	    tabs++;
    }

    return tabs;
}

175
176
177
178
179
int strlenpt(char *buf)
{
    return strnlenpt(buf, strlen(buf));
}

180
/* resets current_y, based on the position of current, and puts the cursor at 
Chris Allegretta's avatar
Chris Allegretta committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
   (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)
196
	wmove(edit, current_y, x);
Chris Allegretta's avatar
Chris Allegretta committed
197
    else
198
	wmove(edit, current_y, x -
Chris Allegretta's avatar
Chris Allegretta committed
199
	      get_page_start_virtual(get_page_from_virtual(x)));
Chris Allegretta's avatar
Chris Allegretta committed
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240

}

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

241
242
243
/* Repaint the statusbar when getting a character in nanogetstr */
void nanoget_repaint(char *buf, char *inputbuf, int x)
{
244
245
    int len = strlen(buf);
    int wid = COLS - len;
246

247
248
#ifdef ENABLE_COLOR
    color_on(bottomwin, COLOR_STATUSBAR);
249
250
#else
    wattron(bottomwin, A_REVERSE);
251
#endif
252
    blank_statusbar();
253

254
255
256
    if (x <= COLS - 1) {
	/* Black magic */
	buf[len - 1] = ' ';
257

258
259
260
	mvwaddstr(bottomwin, 0, 0, buf);
	waddnstr(bottomwin, inputbuf, wid);
	wmove(bottomwin, 0, (x % COLS));
Chris Allegretta's avatar
Chris Allegretta committed
261
    } else {
262
263
	/* Black magic */
	buf[len - 1] = '$';
264

265
266
267
268
	mvwaddstr(bottomwin, 0, 0, buf);
	waddnstr(bottomwin, &inputbuf[wid * ((x - len) / (wid))], wid);
	wmove(bottomwin, 0, ((x - len) % wid) + len);
    }
269
270
271

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_STATUSBAR);
272
273
#else
    wattroff(bottomwin, A_REVERSE);
274
#endif
275
276
}

277
/* Get the input from the kb; this should only be called from statusq */
278
int nanogetstr(int allowtabs, char *buf, char *def, shortcut *s,
279
	       int start_x, int list)
Chris Allegretta's avatar
Chris Allegretta committed
280
{
281
    int kbinput = 0, x = 0, xend, slen;
282
    int x_left = 0, inputlen, tabbed = 0;
283
    char *inputbuf;
284
    shortcut *t;
285
#ifndef DISABLE_TABCOMP
286
287
    int shift = 0;
#endif
Chris Allegretta's avatar
Chris Allegretta committed
288

289
    slen = length_of_list(s);
290
    inputbuf = charalloc(strlen(def) + 1);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
291
    inputbuf[0] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
292
293
294
295

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

296
#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
297
    currshortcut = s;
298
299
#endif

Chris Allegretta's avatar
Chris Allegretta committed
300
    /* Get the input! */
301
    if (strlen(def) > 0)
Chris Allegretta's avatar
Chris Allegretta committed
302
	strcpy(inputbuf, def);
303
304

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

306
307
308
    /* Make sure any editor screen updates are displayed before getting input */
    wrefresh(edit);

Chris Allegretta's avatar
Chris Allegretta committed
309
    while ((kbinput = wgetch(bottomwin)) != 13) {
310
	for (t = s; t != NULL; t = t->next) {
311
312
313
314
#ifdef DEBUG
	    fprintf(stderr, _("Aha! \'%c\' (%d)\n"), kbinput, kbinput);
#endif

315
	    if (kbinput == t->val && kbinput < 32) {
316

317
318
#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
319
		if (kbinput == NANO_HELP_KEY || kbinput == NANO_HELP_FKEY) {
320
321
322
323
324
		    do_help();
		    break;
		}
#endif

325
326
327
		/* We shouldn't discard the answer it gave, just because
		   we hit a keystroke, GEEZ! */
		answer = mallocstrcpy(answer, inputbuf);
328
		free(inputbuf);
329
		return t->val;
Chris Allegretta's avatar
Chris Allegretta committed
330
331
332
333
	    }
	}
	xend = strlen(buf) + strlen(inputbuf);

Chris Allegretta's avatar
Chris Allegretta committed
334
335
336
	if (kbinput != '\t')
	    tabbed = 0;

Chris Allegretta's avatar
Chris Allegretta committed
337
	switch (kbinput) {
338

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

	    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);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
379
		inputbuf[strlen(inputbuf) - 1] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
380
381
382
383
384
385
386
387
388
389
390
391
	    }
	    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)))
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
392
		    inputbuf[strlen(inputbuf) - 1] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
393
394
395
396
		else if (x - x_left) {
		    memmove(inputbuf + (x - x_left) - 1,
			    inputbuf + (x - x_left),
			    strlen(inputbuf) - (x - x_left));
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
397
		    inputbuf[strlen(inputbuf) - 1] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
398
399
		}
	    }
Chris Allegretta's avatar
Chris Allegretta committed
400
401
402
	    if (x > strlen(buf))
		x--;
	    break;
403
#ifndef DISABLE_TABCOMP
Chris Allegretta's avatar
Chris Allegretta committed
404
	case NANO_CONTROL_I:
405
	    if (allowtabs) {
406
		shift = 0;
Chris Allegretta's avatar
Chris Allegretta committed
407
408
		inputbuf = input_tab(inputbuf, (x - x_left),
				     &tabbed, &shift, &list);
409
		x += shift;
410
411
		if (x - x_left > strlen(inputbuf))
		    x = strlen(inputbuf) + x_left;
412
	    }
Chris Allegretta's avatar
Chris Allegretta committed
413
	    break;
414
#endif
Chris Allegretta's avatar
Chris Allegretta committed
415
	case KEY_LEFT:
416
	case NANO_BACK_KEY:
Chris Allegretta's avatar
Chris Allegretta committed
417
418
419
420
421
422
423
424
	    if (x > strlen(buf))
		x--;
	    wmove(bottomwin, 0, x);
	    break;
	case KEY_UP:
	case KEY_DOWN:
	    break;

425
426
427
	case KEY_DC:
	    goto do_deletekey;

Chris Allegretta's avatar
Chris Allegretta committed
428
429
	case 27:
	    switch (kbinput = wgetch(edit)) {
430
	    case 'O':
Chris Allegretta's avatar
Chris Allegretta committed
431
		switch (kbinput = wgetch(edit)) {
432
		case 'F':
Chris Allegretta's avatar
Chris Allegretta committed
433
434
		    x = x_left + strlen(inputbuf);
		    break;
435
		case 'H':
Chris Allegretta's avatar
Chris Allegretta committed
436
437
438
439
		    x = x_left;
		    break;
		}
		break;
440
	    case '[':
Chris Allegretta's avatar
Chris Allegretta committed
441
442
443
444
445
446
447
448
449
450
451
		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;
452
453
		case '1':
		case '7':
Chris Allegretta's avatar
Chris Allegretta committed
454
		    x = x_left;
455
456
457
		    goto skip_tilde;
		case '3':
		  do_deletekey:
Chris Allegretta's avatar
Chris Allegretta committed
458
459
460
461
462
		    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);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
463
			inputbuf[strlen(inputbuf) - 1] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
464
		    }
465
466
467
		    goto skip_tilde;
		case '4':
		case '8':
Chris Allegretta's avatar
Chris Allegretta committed
468
		    x = x_left + strlen(inputbuf);
469
470
		    goto skip_tilde;
		  skip_tilde:
Chris Allegretta's avatar
Chris Allegretta committed
471
472
		    nodelay(edit, TRUE);
		    kbinput = wgetch(edit);
473
		    if (kbinput == '~' || kbinput == ERR)
Chris Allegretta's avatar
Chris Allegretta committed
474
475
476
477
			kbinput = -1;
		    nodelay(edit, FALSE);
		    break;
		}
478
479
	    default:

480
		for (t = s; t != NULL; t = t->next) {
481
#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
482
483
		    fprintf(stderr, _("Aha! \'%c\' (%d)\n"), kbinput,
			    kbinput);
484
#endif
485
		    if (kbinput == t->val || kbinput == t->val - 32) {
486
487
488
489
490
491

			/* 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);
492
			return t->val;
493
494
495
		    }
		}

Chris Allegretta's avatar
Chris Allegretta committed
496
497
498
499
	    }
	    break;

	default:
500

Chris Allegretta's avatar
Chris Allegretta committed
501
502
	    if (kbinput < 32)
		break;
503
504
505
506

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

Chris Allegretta's avatar
Chris Allegretta committed
507
508
	    memmove(&inputbuf[x - x_left + 1],
		    &inputbuf[x - x_left], inputlen - (x - x_left) + 1);
509
510
	    inputbuf[x - x_left] = kbinput;

Chris Allegretta's avatar
Chris Allegretta committed
511
512
513
514
515
516
	    x++;

#ifdef DEBUG
	    fprintf(stderr, _("input \'%c\' (%d)\n"), kbinput, kbinput);
#endif
	}
517
	nanoget_repaint(buf, inputbuf, x);
Chris Allegretta's avatar
Chris Allegretta committed
518
519
	wrefresh(bottomwin);
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
520
521
522
523
524
525
526
#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
527

528
    answer = mallocstrcpy(answer, inputbuf);
529
    free(inputbuf);
Chris Allegretta's avatar
Chris Allegretta committed
530

Chris Allegretta's avatar
Chris Allegretta committed
531
    /* In pico mode, just check for a blank answer here */
Chris Allegretta's avatar
Chris Allegretta committed
532
    if (ISSET(PICO_MODE) && answer[0] == '\0')
Chris Allegretta's avatar
Chris Allegretta committed
533
534
535
536
537
538
539
540
541
542
543
544
	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
545
void titlebar(char *path)
Chris Allegretta's avatar
Chris Allegretta committed
546
547
{
    int namelen, space;
Chris Allegretta's avatar
Chris Allegretta committed
548
549
550
551
    char *what = path;

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

553
554
555
556
#ifdef ENABLE_COLOR
    color_on(topwin, COLOR_TITLEBAR);
    mvwaddstr(topwin, 0, 0, hblank);
#else
Chris Allegretta's avatar
Chris Allegretta committed
557
558
    horizbar(topwin, 0);
    wattron(topwin, A_REVERSE);
559
560
561
#endif


Chris Allegretta's avatar
Chris Allegretta committed
562
563
564
565
    mvwaddstr(topwin, 0, 3, VERMSG);

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

Chris Allegretta's avatar
Chris Allegretta committed
566
    namelen = strlen(what);
Chris Allegretta's avatar
Chris Allegretta committed
567

Chris Allegretta's avatar
Chris Allegretta committed
568
    if (what[0] == '\0')
569
	mvwaddstr(topwin, 0, COLS / 2 - 6, _("New Buffer"));
Chris Allegretta's avatar
Chris Allegretta committed
570
571
    else {
	if (namelen > space) {
Chris Allegretta's avatar
Chris Allegretta committed
572
573
574
575
576
	    if (path == NULL)
		waddstr(topwin, _("  File: ..."));
	    else
		waddstr(topwin, _("   DIR: ..."));
	    waddstr(topwin, &what[namelen - space]);
Chris Allegretta's avatar
Chris Allegretta committed
577
	} else {
Chris Allegretta's avatar
Chris Allegretta committed
578
	    if (path == NULL)
Chris Allegretta's avatar
Chris Allegretta committed
579
580
		mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1),
			  _("File: "));
Chris Allegretta's avatar
Chris Allegretta committed
581
	    else
Chris Allegretta's avatar
Chris Allegretta committed
582
583
		mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1),
			  _(" DIR: "));
Chris Allegretta's avatar
Chris Allegretta committed
584
	    waddstr(topwin, what);
Chris Allegretta's avatar
Chris Allegretta committed
585
586
587
588
	}
    }
    if (ISSET(MODIFIED))
	mvwaddstr(topwin, 0, COLS - 10, _("Modified"));
Chris Allegretta's avatar
Chris Allegretta committed
589
590
    else if (ISSET(VIEW_MODE))
	mvwaddstr(topwin, 0, COLS - 10, _("View"));
591
592
593
594

#ifdef ENABLE_COLOR
    color_off(topwin, COLOR_TITLEBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
595
    wattroff(topwin, A_REVERSE);
596
597
#endif

Chris Allegretta's avatar
Chris Allegretta committed
598
599
600
601
    wrefresh(topwin);
    reset_cursor();
}

602
603
604
605
606
/* Write a shortcut key to the help area at the bottom of the window. 
 * keystroke is e.g. "^G" and desc is e.g. "Get Help".
 * We are careful to write exactly len characters, even if len is
 * very small and keystroke and desc are long. */
static void onekey(const char *keystroke, const char *desc, int len)
Chris Allegretta's avatar
Chris Allegretta committed
607
608
{
    wattron(bottomwin, A_REVERSE);
609
    waddnstr(bottomwin, keystroke, len);
Chris Allegretta's avatar
Chris Allegretta committed
610
    wattroff(bottomwin, A_REVERSE);
611
612
613
614
615
616
617
618
619
    len -= strlen(keystroke);
    if (len > 0) {
	waddch(bottomwin, ' ');
	len--;
	waddnstr(bottomwin, desc, len);
	len -= strlen(desc);
	for (; len > 0; len--)
	    waddch(bottomwin, ' ');
    }
Chris Allegretta's avatar
Chris Allegretta committed
620
621
622
623
624
625
626
627
628
629
630
}

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

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

631
void bottombars(const shortcut *s)
Chris Allegretta's avatar
Chris Allegretta committed
632
{
633
    int i, j, numcols;
634
    char keystr[4];
635
636
    int slen;

Chris Allegretta's avatar
Chris Allegretta committed
637
638
639
    if (ISSET(NO_HELP))
	return;

640
641
642
643
644
645
    if (s == main_list) {
	slen = MAIN_VISIBLE;
	assert(MAIN_VISIBLE <= length_of_list(s));
    } else
	slen = length_of_list(s);

Chris Allegretta's avatar
Chris Allegretta committed
646
#ifdef ENABLE_COLOR
647
    color_on(bottomwin, COLOR_BOTTOMBARS);
648
    if (!colors[COLOR_BOTTOMBARS - FIRST_COLORNUM].set ||
Chris Allegretta's avatar
Chris Allegretta committed
649
650
	colors[COLOR_BOTTOMBARS - FIRST_COLORNUM].fg != COLOR_BLACK)
	wattroff(bottomwin, A_REVERSE);
651
652
#endif

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

    clear_bottomwin();
657

658
659
    for (i = 0; i < numcols; i++) {
	for (j = 0; j <= 1; j++) {
Chris Allegretta's avatar
Chris Allegretta committed
660

661
	    wmove(bottomwin, 1 + j, i * (COLS / numcols));
662

663
664
665
#ifndef NANO_SMALL
	    if (s->val == NANO_CONTROL_SPACE)
		strcpy(keystr, "^ ");
666
	    else
667
668
669
670
671
672
673
674
#endif /* !NANO_SMALL */
	    if (s->val > 0) {
		if (s->val < 64)
		    sprintf(keystr, "^%c", s->val + 64);
		else
		    sprintf(keystr, "M-%c", s->val - 32);
	    } else if (s->altval > 0)
		sprintf(keystr, "M-%c", s->altval);
675

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

678
679
680
681
	    s = s->next;
	    if (s == NULL)
		goto break_completely_out;
	}	
Chris Allegretta's avatar
Chris Allegretta committed
682
    }
683
break_completely_out:
Chris Allegretta's avatar
Chris Allegretta committed
684

Chris Allegretta's avatar
Chris Allegretta committed
685
#ifdef ENABLE_COLOR
686
687
688
    color_off(bottomwin, COLOR_BOTTOMBARS);
#endif

Chris Allegretta's avatar
Chris Allegretta committed
689
690
691
692
693
694
695
696
    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
697
	titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
698
699
700
701
	wrefresh(topwin);
    }
}

702
703
704
/* 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 */
705
/* characters is viewable (e.g.: page 1 shows from 1 to COLS) */
Chris Allegretta's avatar
Chris Allegretta committed
706
707
inline int get_page_from_virtual(int virtual)
{
708
    int page = 2;
Chris Allegretta's avatar
Chris Allegretta committed
709

Chris Allegretta's avatar
Chris Allegretta committed
710
711
    if (virtual <= COLS - 2)
	return 1;
712
    virtual -= (COLS - 2);
713
714

    while (virtual > COLS - 2 - 7) {
715
	virtual -= (COLS - 2 - 7);
716
	page++;
Chris Allegretta's avatar
Chris Allegretta committed
717
718
    }

719
720
721
    return page;
}

722
/* The inverse of the above function */
Chris Allegretta's avatar
Chris Allegretta committed
723
724
inline int get_page_start_virtual(int page)
{
725
726
    int virtual;
    virtual = --page * (COLS - 7);
Chris Allegretta's avatar
Chris Allegretta committed
727
728
    if (page)
	virtual -= 2 * page - 1;
729
    return virtual;
730
731
}

Chris Allegretta's avatar
Chris Allegretta committed
732
733
inline int get_page_end_virtual(int page)
{
734
735
736
737
    return get_page_start_virtual(page) + COLS - 1;
}

#ifndef NANO_SMALL
738
739
740
/* This takes care of the case where there is a mark that covers only */
/* the current line. */

741
/* It expects a line with no tab characters (i.e.: the type that edit_add */
742
/* deals with */
Chris Allegretta's avatar
Chris Allegretta committed
743
744
void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
			 int virt_cur_x, int this_page)
745
{
746
747
748
    /*
     * 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
749
     * turn (for those that are currently visible, of course)
750
751
752
753
     *
     * 3 start points: 0 -> begin, begin->end, end->strlen(data)
     *    in data    :    pre          sel           post        
     */
Chris Allegretta's avatar
Chris Allegretta committed
754
    int this_page_start = get_page_start_virtual(this_page),
755
	this_page_end = get_page_end_virtual(this_page);
756
757

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

    /* now fix the start locations & lengths according to the cursor's 
761
     * position (i.e.: our page) */
Chris Allegretta's avatar
Chris Allegretta committed
762
    if (pre_data_len < this_page_start)
763
764
765
	pre_data_len = 0;
    else
	pre_data_len -= this_page_start;
766

Chris Allegretta's avatar
Chris Allegretta committed
767
    if (begin < this_page_start)
768
769
	begin = this_page_start;

Chris Allegretta's avatar
Chris Allegretta committed
770
    if (end < this_page_start)
771
	end = this_page_start;
772

Chris Allegretta's avatar
Chris Allegretta committed
773
    if (begin > this_page_end)
774
	begin = this_page_end;
775

Chris Allegretta's avatar
Chris Allegretta committed
776
    if (end > this_page_end)
777
	end = this_page_end;
778

779
    /* Now calculate the lengths */
780
781
    sel_data_len = end - begin;
    post_data_len = this_page_end - end;
782

783
784
785
#ifdef ENABLE_COLOR
    color_on(edit, COLOR_MARKER);
#else
786
    wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
787
#endif				/* ENABLE_COLOR */
788

789
    mvwaddnstr(edit, y, begin - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
790
	       &fileptr->data[begin], sel_data_len);
791
792
793
794

#ifdef ENABLE_COLOR
    color_off(edit, COLOR_MARKER);
#else
795
    wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
796
#endif				/* ENABLE_COLOR */
797

Chris Allegretta's avatar
Chris Allegretta committed
798
799
800
}
#endif

801
802
803
804
/* 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
805
806
 * line */
void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
807
	      int virt_mark_beginx, int this_page)
Chris Allegretta's avatar
Chris Allegretta committed
808
{
809

Chris Allegretta's avatar
Chris Allegretta committed
810
#ifdef ENABLE_COLOR
811
812
    colortype *tmpcolor = NULL;
    int k, paintlen;
813
814
    filestruct *e, *s;
    regoff_t ematch, smatch;
815
816
817
#endif

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

Chris Allegretta's avatar
Chris Allegretta committed
822
#ifdef ENABLE_COLOR
823
    if (colorstrings != NULL)
Chris Allegretta's avatar
Chris Allegretta committed
824
825
	for (tmpcolor = colorstrings; tmpcolor != NULL;
	     tmpcolor = tmpcolor->next) {
826

827
	    if (tmpcolor->end == NULL) {
828

829
830
		/* First, highlight all single-line regexes */
		k = start;
831
832
833
		regcomp(&color_regexp, tmpcolor->start, 0);
		while (!regexec(&color_regexp, &fileptr->data[k], 1,
				colormatches, 0)) {
834

835
		    if (colormatches[0].rm_eo - colormatches[0].rm_so < 1) {
836
			statusbar(_("Refusing 0 length regex match"));
837
838
			break;
		    }
839
#ifdef DEBUG
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
840
		    fprintf(stderr, _("Match! (%d chars) \"%s\"\n"),
841
842
			    colormatches[0].rm_eo - colormatches[0].rm_so,
			    &fileptr->data[k + colormatches[0].rm_so]);
843
#endif
844
		    if (colormatches[0].rm_so < COLS - 1) {
845
846
847
848
			if (tmpcolor->bright)
			    wattron(edit, A_BOLD);
			wattron(edit, COLOR_PAIR(tmpcolor->pairnum));

849
			if (colormatches[0].rm_eo + k <= COLS) {
850
			    paintlen =
851
				colormatches[0].rm_eo - colormatches[0].rm_so;
852
#ifdef DEBUG
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
853
			    fprintf(stderr, _("paintlen (%d) = eo (%d) - so (%d)\n"), 
854
855
856
857
858
				paintlen, colormatches[0].rm_eo, colormatches[0].rm_so);
#endif

			}
			else {
859
			    paintlen = COLS - k - colormatches[0].rm_so - 1;
860
#ifdef DEBUG
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
861
			    fprintf(stderr, _("paintlen (%d) = COLS (%d) - k (%d), - rm.so (%d) - 1\n"), 
862
863
864
					paintlen, COLS, k, colormatches[0].rm_so);
#endif
			}
865

866
867
			mvwaddnstr(edit, yval, colormatches[0].rm_so + k,
				   &fileptr->data[k + colormatches[0].rm_so],
868
869
870
				   paintlen);

		    }
871

872
873
874
		    if (tmpcolor->bright)
			wattroff(edit, A_BOLD);
		    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
875

876
		    k += colormatches[0].rm_eo;
877

Chris Allegretta's avatar
Chris Allegretta committed
878
		}
879
880
		regfree(&color_regexp);

881
882
883
884
885
886
887
888
889
890
	    }
	    /* 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) {
891
		    regcomp(&color_regexp, tmpcolor->start, 0);
892
		    if (!regexec
893
894
			(&color_regexp, s->data, 1, colormatches, 0)) {
			regfree(&color_regexp);
895
			break;
896
		    }
897
		    s = s->prev;
898
		    regfree(&color_regexp);
899
900
901
902
		}

		if (s != NULL) {
		    /* We found a start, mark it */
903
		    smatch = colormatches[0].rm_so;
904
905
906

		    e = s;
		    while (e != NULL && e != fileptr) {
907
			regcomp(&color_regexp, tmpcolor->end, 0);
908
			if (!regexec
909
910
			    (&color_regexp, e->data, 1, colormatches, 0)) {
			    regfree(&color_regexp);
911
			    break;
912
			}
913
			e = e->next;
914
			regfree(&color_regexp);
915
		    }
916

917
918
919
920
		    if (e != fileptr)
			continue;	/* There's an end before us */
		    else {	/* Keep looking for an end */
			while (e != NULL) {
921
			    regcomp(&color_regexp, tmpcolor->end, 0);
922
			    if (!regexec
923
				(&color_regexp, e->data, 1, colormatches,
924
925
				 0)) {
				regfree(&color_regexp);
926
				break;
927
			    }
928
			    e = e->next;
929
			    regfree(&color_regexp);
930
931
932
933
934
			}

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

			    while (e != NULL) {
938
				regcomp(&color_regexp, tmpcolor->end, 0);
939
				if (!regexec
940
				    (&color_regexp, e->data, 1,
941
942
				     colormatches, 0)) {
				    regfree(&color_regexp);
943
				    break;
944
945
				} e = e->next;
				regfree(&color_regexp);
946
947
948
949
950
951
952
953
954
955
956
957
958
			    }

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

959
			    if (s == fileptr && e == fileptr && ematch < COLS) {
960
961
962
				mvwaddnstr(edit, yval, start + smatch, 
					&fileptr->data[start + smatch],
					ematch - smatch);
963
#ifdef DEBUG
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
964
			fprintf(stderr, _("start = %d, smatch = %d, ematch = %d\n"), start,
965
966
967
968
				smatch, ematch);
#endif

		    	    } else if (s == fileptr)
969
970
971
972
973
974
				mvwaddnstr(edit, yval, start + smatch, 
					&fileptr->data[start + smatch],
					COLS - smatch);
			    else if (e == fileptr)
				mvwaddnstr(edit, yval, start, 
					&fileptr->data[start],
975
					COLS - start);
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
			    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! =) */
		}
992
993

	    }
994

995
	}
996

Chris Allegretta's avatar
Chris Allegretta committed
997
#endif				/* ENABLE_COLOR */
Chris Allegretta's avatar
Chris Allegretta committed
998
#ifndef NANO_SMALL
999

1000
    /* There are quite a few cases that could take place; we'll deal
1001
     * with them each in turn */
Chris Allegretta's avatar
Chris Allegretta committed
1002
    if (ISSET(MARK_ISSET) &&
1003
	!((fileptr->lineno > mark_beginbuf->lineno
Chris Allegretta's avatar
Chris Allegretta committed
1004
1005
1006
	   && fileptr->lineno > current->lineno)
	  || (fileptr->lineno < mark_beginbuf->lineno
	      && fileptr->lineno < current->lineno))) {
1007
	/* If we get here we are on a line that is at least
1008
1009
1010
1011
1012
	 * 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 */
1013
1014
1015
#ifdef ENABLE_COLOR
	    color_on(edit, COLOR_MARKER);
#else
1016
	    wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1017
#endif				/* ENABLE_COLOR */
1018

1019
	    mvwaddnstr(edit, yval, 0, fileptr->data, COLS);
1020
1021
1022
1023

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

1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
	} 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) {
	    /*
1041
	     * We're updating the line that was first marked,
1042
	     * but we're not currently on it.  So we want to
1043
	     * figure out which half to invert based on our
1044
1045
	     * relative line numbers.
	     *
1046
1047
1048
	     * 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.
1049
1050
1051
	     */
	    int target;

1052
1053
1054
1055
	    if (mark_beginbuf->lineno > current->lineno) {
#ifdef ENABLE_COLOR
		color_on(edit, COLOR_MARKER);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1056
		wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1057
#endif				/* ENABLE_COLOR */
1058

1059
		target =
Chris Allegretta's avatar
Chris Allegretta committed
1060
1061
		    (virt_mark_beginx <
		     COLS - 1) ? virt_mark_beginx : COLS - 1;
1062

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

#ifdef ENABLE_COLOR
1066
		color_off(edit, COLOR_MARKER);
1067
#else
1068
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1069
#endif				/* ENABLE_COLOR */
1070
1071


1072
1073
1074
	    }

	    if (mark_beginbuf->lineno < current->lineno) {
1075
#ifdef ENABLE_COLOR
1076
		color_on(edit, COLOR_MARKER);
1077
#else
1078
		wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1079
#endif				/* ENABLE_COLOR */
1080

1081
		target = (COLS - 1) - virt_mark_beginx;
1082

1083
1084
		if (target < 0)
		    target = 0;
1085

1086
		mvwaddnstr(edit, yval, virt_mark_beginx,
Chris Allegretta's avatar
Chris Allegretta committed
1087
			   &fileptr->data[virt_mark_beginx], target);
1088

1089
1090
1091
#ifdef ENABLE_COLOR
		color_off(edit, COLOR_MARKER);
#else
1092
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1093
#endif				/* ENABLE_COLOR */
1094
1095

	    }
1096
1097

	} else if (fileptr == current) {
1098
	    /* We're on the cursor's line, but it's not the first
1099
1100
1101
1102
	     * 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);

1103
1104
1105
1106
1107
	    if (mark_beginbuf->lineno < current->lineno) {

#ifdef ENABLE_COLOR
		color_on(edit, COLOR_MARKER);
#else
1108
		wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1109
#endif				/* ENABLE_COLOR */
1110

1111
1112
		if (virt_cur_x > COLS - 2) {
		    mvwaddnstr(edit, yval, 0,
Chris Allegretta's avatar
Chris Allegretta committed
1113
1114
			       &fileptr->data[this_page_start],
			       virt_cur_x - this_page_start);
1115
1116
		} else
		    mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
1117
1118

#ifdef ENABLE_COLOR
1119
		color_off(edit, COLOR_MARKER);
1120
#else
1121
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1122
#endif				/* ENABLE_COLOR */
1123

1124
1125
1126
	    }

	    if (mark_beginbuf->lineno > current->lineno) {
1127
1128

#ifdef ENABLE_COLOR
1129
		color_on(edit, COLOR_MARKER);
1130
#else
1131
		wattron(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1132
#endif				/* ENABLE_COLOR */
1133

1134
1135
		if (virt_cur_x > COLS - 2)
		    mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
1136
1137
			       &fileptr->data[virt_cur_x],
			       this_page_end - virt_cur_x);
1138
1139
		else
		    mvwaddnstr(edit, yval, virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
1140
1141
			       &fileptr->data[virt_cur_x],
			       COLS - virt_cur_x);
1142

1143
1144
1145
#ifdef ENABLE_COLOR
		color_off(edit, COLOR_MARKER);
#else
1146
		wattroff(edit, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1147
#endif				/* ENABLE_COLOR */
1148
1149

	    }
Chris Allegretta's avatar
Chris Allegretta committed
1150
	}
1151
1152
1153
    }
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1154
1155
1156
}

/*
1157
1158
 * Just update one line in the edit buffer.  Basically a wrapper for
 * edit_add
Chris Allegretta's avatar
Chris Allegretta committed
1159
 *
1160
 * index gives us a place in the string to update starting from.
Chris Allegretta's avatar
Chris Allegretta committed
1161
1162
1163
1164
1165
 * Likely args are current_x or 0.
 */
void update_line(filestruct * fileptr, int index)
{
    filestruct *filetmp;
1166
1167
1168
    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
1169
    int i, pos, len, page;
Chris Allegretta's avatar
Chris Allegretta committed
1170

Chris Allegretta's avatar
Chris Allegretta committed
1171
1172
    if (!fileptr)
	return;
1173

1174
    /* First, blank out the line (at a minimum) */
Chris Allegretta's avatar
Chris Allegretta committed
1175
1176
1177
1178
1179
    for (filetmp = edittop; filetmp != fileptr && filetmp != editbot;
	 filetmp = filetmp->next)
	line++;

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

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

    realdata = fileptr->data;
    len = strlen(realdata);
1186
    fileptr->data = charalloc(xpt(fileptr, len) + 1);
1187
1188

    pos = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1189
1190
    for (i = 0; i < len; i++) {
	if (realdata[i] == '\t') {
1191
1192
	    do {
		fileptr->data[pos++] = ' ';
Chris Allegretta's avatar
Chris Allegretta committed
1193
1194
1195
1196
		if (i < current_x)
		    virt_cur_x++;
		if (i < mark_beginx)
		    virt_mark_beginx++;
1197
	    } while (pos % tabsize);
1198
	    /* must decrement once to account for tab-is-one-character */
Chris Allegretta's avatar
Chris Allegretta committed
1199
1200
1201
1202
	    if (i < current_x)
		virt_cur_x--;
	    if (i < mark_beginx)
		virt_mark_beginx--;
Chris Allegretta's avatar
Chris Allegretta committed
1203
	} else if (realdata[i] == 127) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1204
1205
	    /* Treat control characters as ^symbol (ASCII 1 - 31 omitting
	       10, 127) */
Chris Allegretta's avatar
Chris Allegretta committed
1206
1207
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = '?';
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1208
1209
1210
1211
1212
	    if (i < current_x)
		virt_cur_x++;
	    if (i < mark_beginx)
		virt_mark_beginx++;
	} else if (realdata[i] >= 1 && realdata[i] <= 31 && realdata[i] != 10) {
1213
1214
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = realdata[i] + 64;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
	    if (i < current_x)
		virt_cur_x++;
	    if (i < mark_beginx)
		virt_mark_beginx++;
	} else if (realdata[i] == 10) {
	    /* Treat newlines (ASCII 10's) embedded in a line as encoded
	       nulls (ASCII 0's); the line in question should be run
	       through unsunder() before reaching here */
	    fileptr->data[pos++] = '^';
	    fileptr->data[pos++] = '@';
	    if (i < current_x)
		virt_cur_x++;
	    if (i < mark_beginx)
		virt_mark_beginx++;
1229
1230
1231
1232
1233
1234
	} else {
	    fileptr->data[pos++] = realdata[i];
	}
    }

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

Chris Allegretta's avatar
Chris Allegretta committed
1236
    /* Now, paint the line */
1237
    if (current == fileptr && index > COLS - 2) {
1238
	/* This handles when the current line is beyond COLS */
1239
	/* It requires figuring out what page we're on      */
1240
	page = get_page_from_virtual(index);
1241
	col = get_page_start_virtual(page);
Chris Allegretta's avatar
Chris Allegretta committed
1242

1243
	edit_add(filetmp, line, col, virt_cur_x, virt_mark_beginx, page);
Chris Allegretta's avatar
Chris Allegretta committed
1244
1245
	mvwaddch(edit, line, 0, '$');

Chris Allegretta's avatar
Chris Allegretta committed
1246
	if (strlenpt(fileptr->data) > get_page_end_virtual(page) + 1)
Chris Allegretta's avatar
Chris Allegretta committed
1247
	    mvwaddch(edit, line, COLS - 1, '$');
1248
    } else {
1249
1250
1251
	/* 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
1252

1253
	if (strlenpt(&filetmp->data[col]) > COLS)
Chris Allegretta's avatar
Chris Allegretta committed
1254
	    mvwaddch(edit, line, COLS - 1, '$');
1255
    }
1256
1257
1258
1259
1260

    /* Clean up our mess */
    tmp = fileptr->data;
    fileptr->data = realdata;
    free(tmp);
Chris Allegretta's avatar
Chris Allegretta committed
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
}

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
1272
    static int noloop = 0;
1273
    int nlines = 0, i = 0, currentcheck = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1274
1275
1276
1277
1278
1279
1280
    filestruct *temp, *hold = current;

    if (current == NULL)
	return;

    temp = edittop;

1281
    while (nlines <= editwinrows - 1 && nlines <= totlines && temp != NULL) {
Chris Allegretta's avatar
Chris Allegretta committed
1282
1283
	hold = temp;
	update_line(temp, current_x);
1284
1285
1286
	if (temp == current)
	    currentcheck = 1;

Chris Allegretta's avatar
Chris Allegretta committed
1287
	temp = temp->next;
1288
	nlines++;
Chris Allegretta's avatar
Chris Allegretta committed
1289
    }
Chris Allegretta's avatar
Chris Allegretta committed
1290
1291
    /* If noloop == 1, then we already did an edit_update without finishing
       this function.  So we don't run edit_update again */
1292
    if (!currentcheck && !noloop) {	/* Then current has run off the screen... */
1293
	edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
1294
	noloop = 1;
1295
    } else if (noloop)
Chris Allegretta's avatar
Chris Allegretta committed
1296
	noloop = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1297

1298
1299
1300
1301
    if (nlines <= editwinrows - 1)
	while (nlines <= editwinrows - 1) {
	    mvwaddstr(edit, nlines, i, hblank);
	    nlines++;
Chris Allegretta's avatar
Chris Allegretta committed
1302
1303
1304
1305
1306
	}
    if (temp == NULL)
	editbot = hold;
    else
	editbot = temp;
1307
1308

    /* What the hell are we expecting to update the screen if this isn't 
Chris Allegretta's avatar
Chris Allegretta committed
1309
       here? luck?? */
1310
    wrefresh(edit);
Chris Allegretta's avatar
Chris Allegretta committed
1311
1312
}

1313
/*
1314
 * Same as above, but touch the window first, so everything is redrawn.
1315
1316
1317
1318
1319
1320
1321
1322
 */
void edit_refresh_clearok(void)
{
    clearok(edit, TRUE);
    edit_refresh();
    clearok(edit, FALSE);
}

Chris Allegretta's avatar
Chris Allegretta committed
1323
/*
1324
 * Nice generic routine to update the edit buffer, given a pointer to the
Chris Allegretta's avatar
Chris Allegretta committed
1325
1326
 * file struct =) 
 */
1327
void edit_update(filestruct * fileptr, int topmidbotnone)
Chris Allegretta's avatar
Chris Allegretta committed
1328
{
1329
    int i = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1330
1331
1332
1333
1334
1335
    filestruct *temp;

    if (fileptr == NULL)
	return;

    temp = fileptr;
1336
1337
1338
1339
1340
    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)
1341
1342
1343
1344
1345
	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
1346

1347
1348
    edittop = temp;
    fix_editbot();
Chris Allegretta's avatar
Chris Allegretta committed
1349
1350
1351
1352

    edit_refresh();
}

1353
/* This function updates current, based on where current_y is; reset_cursor 
Chris Allegretta's avatar
Chris Allegretta committed
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
   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
1379
 * otherwise, the valid shortcut key caught.  Def is any editable text we
Chris Allegretta's avatar
Chris Allegretta committed
1380
 * want to put up by default.
1381
1382
 *
 * New arg tabs tells whether or not to allow tab completion.
Chris Allegretta's avatar
Chris Allegretta committed
1383
 */
1384
int statusq(int tabs, shortcut *s, char *def, char *msg, ...)
Chris Allegretta's avatar
Chris Allegretta committed
1385
1386
1387
{
    va_list ap;
    char foo[133];
1388
    int ret;
Chris Allegretta's avatar
Chris Allegretta committed
1389

1390
#ifndef DISABLE_TABCOMP
1391
    int list = 0;
1392
1393
#endif

1394
    bottombars(s);
Chris Allegretta's avatar
Chris Allegretta committed
1395
1396
1397
1398

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

1401
1402
1403
#ifdef ENABLE_COLOR
    color_on(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1404
    wattron(bottomwin, A_REVERSE);
1405
1406
1407
#endif


1408
#ifndef DISABLE_TABCOMP
1409
    ret = nanogetstr(tabs, foo, def, s, (strlen(foo) + 3), list);
1410
1411
1412
#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 */
1413
    ret = nanogetstr(tabs, foo, def, s, (strlen(foo) + 3), 0);
1414
#endif
1415
1416
1417
1418

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1419
    wattroff(bottomwin, A_REVERSE);
1420
1421
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431

    switch (ret) {

    case NANO_FIRSTLINE_KEY:
	do_first_line();
	break;
    case NANO_LASTLINE_KEY:
	do_last_line();
	break;
    case NANO_CANCEL_KEY:
1432
1433
1434
1435
1436
1437
1438
#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
1439
1440
	return -1;
    default:
1441
	blank_statusbar();
Chris Allegretta's avatar
Chris Allegretta committed
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
    }

#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];
1459
1460
1461
1462
    int kbinput, ok = -1, i;
    char *yesstr;		/* String of yes characters accepted */
    char *nostr;		/* Same for no */
    char *allstr;		/* And all, surprise! */
1463
#ifndef DISABLE_MOUSE
Chris Allegretta's avatar
Chris Allegretta committed
1464
1465
1466
1467
1468
#ifdef NCURSES_MOUSE_VERSION
    MEVENT mevent;
#endif
#endif

1469
1470

    /* Yes, no and all are strings of any length.  Each string consists of
Chris Allegretta's avatar
Chris Allegretta committed
1471
1472
       all characters accepted as a valid character for that value.
       The first value will be the one displayed in the shortcuts. */
1473
1474
1475
    yesstr = _("Yy");
    nostr = _("Nn");
    allstr = _("Aa");
Chris Allegretta's avatar
Chris Allegretta committed
1476
1477
1478

    /* Write the bottom of the screen */
    clear_bottomwin();
1479
1480
1481
1482

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

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

Chris Allegretta's avatar
Chris Allegretta committed
1488
	wmove(bottomwin, 1, 0);
1489

1490
	sprintf(shortstr, " %c", yesstr[0]);
1491
	onekey(shortstr, _("Yes"), 16);
1492
1493

	if (all) {
1494
	    shortstr[1] = allstr[0];
1495
	    onekey(shortstr, _("All"), 16);
1496
	}
Chris Allegretta's avatar
Chris Allegretta committed
1497
	wmove(bottomwin, 2, 0);
1498

1499
	shortstr[1] = nostr[0];
1500
	onekey(shortstr, _("No"), 16);
1501

1502
	onekey("^C", _("Cancel"), 16);
Chris Allegretta's avatar
Chris Allegretta committed
1503
1504
1505
1506
    }
    va_start(ap, msg);
    vsnprintf(foo, 132, msg, ap);
    va_end(ap);
1507
1508
1509
1510
1511

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_BOTTOMBARS);
    color_on(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1512
    wattron(bottomwin, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1513
#endif				/* ENABLE_COLOR */
1514
1515

    blank_statusbar();
Chris Allegretta's avatar
Chris Allegretta committed
1516
    mvwaddstr(bottomwin, 0, 0, foo);
1517
1518
1519
1520

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1521
    wattroff(bottomwin, A_REVERSE);
1522
1523
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1524
1525
1526
1527
1528
1529
1530
1531
1532
    wrefresh(bottomwin);

    if (leavecursor == 1)
	reset_cursor();

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

	switch (kbinput) {
1533
#ifndef DISABLE_MOUSE
Chris Allegretta's avatar
Chris Allegretta committed
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
#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
1551
1552
1553
			{yesstr[0], allstr[0]},
			{nostr[0], NANO_CONTROL_C}
		    };
Chris Allegretta's avatar
Chris Allegretta committed
1554

Chris Allegretta's avatar
Chris Allegretta committed
1555
		    ungetch(yesnosquare[mevent.y][mevent.x / (COLS / 6)]);
Chris Allegretta's avatar
Chris Allegretta committed
1556
1557
1558
		} else {
		    char yesnosquare[2][2] = {
			{yesstr[0], '\0'},
Chris Allegretta's avatar
Chris Allegretta committed
1559
1560
			{nostr[0], NANO_CONTROL_C}
		    };
Chris Allegretta's avatar
Chris Allegretta committed
1561

Chris Allegretta's avatar
Chris Allegretta committed
1562
		    ungetch(yesnosquare[mevent.y][mevent.x / (COLS / 6)]);
Chris Allegretta's avatar
Chris Allegretta committed
1563
1564
1565
1566
1567
		}
	    }
	    break;
#endif
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1568
1569
1570
	case NANO_CONTROL_C:
	    ok = -2;
	    break;
1571
1572
	default:

1573
	    /* Look for the kbinput in the yes, no and (optimally) all str */
Chris Allegretta's avatar
Chris Allegretta committed
1574
	    for (i = 0; yesstr[i] != 0 && yesstr[i] != kbinput; i++);
1575
	    if (yesstr[i] != 0) {
1576
		ok = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1577
		break;
1578
1579
	    }

Chris Allegretta's avatar
Chris Allegretta committed
1580
	    for (i = 0; nostr[i] != 0 && nostr[i] != kbinput; i++);
1581
1582
	    if (nostr[i] != 0) {
		ok = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1583
		break;
1584
1585
1586
	    }

	    if (all) {
Chris Allegretta's avatar
Chris Allegretta committed
1587
		for (i = 0; allstr[i] != 0 && allstr[i] != kbinput; i++);
1588
1589
		if (allstr[i] != 0) {
		    ok = 2;
Chris Allegretta's avatar
Chris Allegretta committed
1590
		    break;
1591
1592
		}
	    }
Chris Allegretta's avatar
Chris Allegretta committed
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
	}
    }

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

1615
    start_x = COLS / 2 - strlen(foo) / 2 - 1;
Chris Allegretta's avatar
Chris Allegretta committed
1616
1617
1618
1619
1620
1621

    /* Blank out line */
    blank_statusbar();

    wmove(bottomwin, 0, start_x);

1622
1623
1624
#ifdef ENABLE_COLOR
    color_on(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1625
    wattron(bottomwin, A_REVERSE);
1626
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1627
1628
1629
1630

    waddstr(bottomwin, "[ ");
    waddstr(bottomwin, foo);
    waddstr(bottomwin, " ]");
1631
1632
1633
1634

#ifdef ENABLE_COLOR
    color_off(bottomwin, COLOR_STATUSBAR);
#else
Chris Allegretta's avatar
Chris Allegretta committed
1635
    wattroff(bottomwin, A_REVERSE);
1636
1637
#endif

Chris Allegretta's avatar
Chris Allegretta committed
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
    wrefresh(bottomwin);

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

void display_main_list(void)
{
1648
    bottombars(main_list);
Chris Allegretta's avatar
Chris Allegretta committed
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
}

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);
1663
    edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
1664
    titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
1665
1666
1667
1668
1669
1670
1671
1672
1673
    return 1;
}

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

1674
int do_cursorpos(int constant)
Chris Allegretta's avatar
Chris Allegretta committed
1675
1676
{
    filestruct *fileptr;
1677
    float linepct = 0.0, bytepct = 0.0, colpct = 0.0;
1678
    long i = 0, j = 0;
1679
    static long old_i = -1, old_totsize = -1;
Chris Allegretta's avatar
Chris Allegretta committed
1680
1681
1682
1683

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

1684
1685
1686
1687
1688
1689
    if (old_i == -1)
	old_i = i;

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

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

1692
1693
1694
    for (fileptr = fileage; fileptr != current && fileptr != NULL;
	 fileptr = fileptr->next)
	i += strlen(fileptr->data) + 1;
1695

1696
1697
    if (fileptr == NULL)
	return -1;
1698

1699
    i += current_x;
1700

1701
    j = totsize;
1702

1703
1704
    if (totsize > 0)
	bytepct = 100 * i / totsize;
1705
1706
1707

    if (totlines > 0)
	 linepct = 100 * current->lineno / totlines;
Chris Allegretta's avatar
Chris Allegretta committed
1708
1709
1710
1711
1712
1713

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

1714
1715
1716
1717
    /* 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
1718
	statusbar(_
1719
		  ("line %d/%d (%.0f%%), col %ld/%ld (%.0f%%), char %ld/%ld (%.0f%%)"),
1720
1721
		  current->lineno, totlines, linepct, xplustabs() + 1, 
		  xpt(current, strlen(current->data)) + 1, colpct, i, j, bytepct);
1722
1723
1724
1725
1726
    }

    old_i = i;
    old_totsize = totsize;

Chris Allegretta's avatar
Chris Allegretta committed
1727
1728
1729
1730
    reset_cursor();
    return 1;
}

1731
1732
1733
1734
1735
int do_cursorpos_void(void)
{
    return do_cursorpos(0);
}

Chris Allegretta's avatar
Chris Allegretta committed
1736
/* Our broken, non-shortcut list compliant help function.
1737
   But, hey, it's better than nothing, and it's dynamic! */
Chris Allegretta's avatar
Chris Allegretta committed
1738
1739
int do_help(void)
{
1740
#ifndef DISABLE_HELP
1741
    char *ptr, *end;
1742
    int i, j, row = 0, page = 1, kbinput = 0, no_more = 0, kp, kp2;
Chris Allegretta's avatar
Chris Allegretta committed
1743
    int no_help_flag = 0;
1744
    shortcut *oldshortcut;
Chris Allegretta's avatar
Chris Allegretta committed
1745
1746
1747

    blank_edit();
    curs_set(0);
1748
    wattroff(bottomwin, A_REVERSE);
Chris Allegretta's avatar
Chris Allegretta committed
1749
1750
    blank_statusbar();

1751
1752
1753
1754
1755
    help_init();
    ptr = help_text;

    oldshortcut = currshortcut;

1756
    currshortcut = help_list;
1757

1758
    kp = keypad_on(edit, 1);
1759
    kp2 = keypad_on(bottomwin, 1);
1760

Chris Allegretta's avatar
Chris Allegretta committed
1761
1762
    if (ISSET(NO_HELP)) {

1763
	/* Well, if we're going to do this, we should at least
1764
	   do it the right way */
Chris Allegretta's avatar
Chris Allegretta committed
1765
	no_help_flag = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1766
	UNSET(NO_HELP);
1767
	window_init();
1768
	bottombars(help_list);
1769

Chris Allegretta's avatar
Chris Allegretta committed
1770
    } else
1771
	bottombars(help_list);
Chris Allegretta's avatar
Chris Allegretta committed
1772
1773
1774
1775

    do {
	ptr = help_text;
	switch (kbinput) {
1776
#ifndef DISABLE_MOUSE
1777
#ifdef NCURSES_MOUSE_VERSION
Chris Allegretta's avatar
Chris Allegretta committed
1778
1779
1780
	case KEY_MOUSE:
	    do_mouse();
	    break;
1781
1782
#endif
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
	case 27:
	    kbinput = wgetch(edit);
	    switch(kbinput) {
	    case '[':
		kbinput = wgetch(edit);
		switch(kbinput) {
		    case '5':	/* Alt-[-5 = Page Up */
			wgetch(edit);
			goto do_pageupkey;
			break;
		    case 'V':	/* Alt-[-V = Page Up in Hurd Console */
		    case 'I':	/* Alt-[-I = Page Up - FreeBSD Console */
			goto do_pageupkey;
			break;
		    case '6':	/* Alt-[-6 = Page Down */
			wgetch(edit);
			goto do_pagedownkey;
			break;
		    case 'U':	/* Alt-[-U = Page Down in Hurd Console */
		    case 'G':	/* Alt-[-G = Page Down - FreeBSD Console */
			goto do_pagedownkey;
			break;
		}
		break;
	    }
	    break;
Chris Allegretta's avatar
Chris Allegretta committed
1809
1810
1811
	case NANO_NEXTPAGE_KEY:
	case NANO_NEXTPAGE_FKEY:
	case KEY_NPAGE:
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1812
	  do_pagedownkey:
Chris Allegretta's avatar
Chris Allegretta committed
1813
1814
1815
1816
1817
1818
1819
1820
	    if (!no_more) {
		blank_edit();
		page++;
	    }
	    break;
	case NANO_PREVPAGE_KEY:
	case NANO_PREVPAGE_FKEY:
	case KEY_PPAGE:
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1821
	  do_pageupkey:
Chris Allegretta's avatar
Chris Allegretta committed
1822
1823
1824
1825
1826
1827
1828
1829
	    if (page > 1) {
		no_more = 0;
		blank_edit();
		page--;
	    }
	    break;
	}

1830
	/* Calculate where in the text we should be, based on the page */
Chris Allegretta's avatar
Chris Allegretta committed
1831
1832
1833
	for (i = 1; i < page; i++) {
	    row = 0;
	    j = 0;
1834
1835

	    while (row < editwinrows - 2 && *ptr != '\0') {
Chris Allegretta's avatar
Chris Allegretta committed
1836
1837
1838
1839
1840
1841
1842
1843
1844
		if (*ptr == '\n' || j == COLS - 5) {
		    j = 0;
		    row++;
		}
		ptr++;
		j++;
	    }
	}

1845
	if (i > 1) {
1846

1847
1848
	}

Chris Allegretta's avatar
Chris Allegretta committed
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
	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) {

1859
		/* Don't print half a word if we've run out of space */
Chris Allegretta's avatar
Chris Allegretta committed
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
		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
1876
1877
    } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY &&
	     kbinput != NANO_EXIT_FKEY);
Chris Allegretta's avatar
Chris Allegretta committed
1878

1879
1880
    currshortcut = oldshortcut;

Chris Allegretta's avatar
Chris Allegretta committed
1881
    if (no_help_flag) {
1882
	blank_bottombars();
Chris Allegretta's avatar
Chris Allegretta committed
1883
	wrefresh(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
1884
	SET(NO_HELP);
1885
	window_init();
Chris Allegretta's avatar
Chris Allegretta committed
1886
    } else
1887
	bottombars(currshortcut);
Chris Allegretta's avatar
Chris Allegretta committed
1888
1889
1890

    curs_set(1);
    edit_refresh();
1891
    kp = keypad_on(edit, kp);
1892
    kp2 = keypad_on(bottomwin, kp2);
1893

1894
1895
#elif defined(DISABLE_HELP)
    nano_disabled_msg();
Chris Allegretta's avatar
Chris Allegretta committed
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
#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) {
1916
	fprintf(stderr, "(%d) %s\n", fileptr->lineno, fileptr->data);
Chris Allegretta's avatar
Chris Allegretta committed
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
	fflush(stderr);
	fileptr = fileptr->next;
    }
#endif				/* DEBUG */
}

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

    fileptr = filebot;
    while (fileptr != NULL) {
1930
	fprintf(stderr, "(%d) %s\n", fileptr->lineno, fileptr->data);
Chris Allegretta's avatar
Chris Allegretta committed
1931
1932
1933
1934
1935
	fflush(stderr);
	fileptr = fileptr->prev;
    }
#endif				/* DEBUG */
}
Robert Siemborski's avatar
Robert Siemborski committed
1936

1937
/* Fix editbot, based on the assumption that edittop is correct */
1938
1939
void fix_editbot(void)
{
Robert Siemborski's avatar
Robert Siemborski committed
1940
1941
    int i;
    editbot = edittop;
1942
1943
    for (i = 0; (i <= editwinrows - 1) && (editbot->next != NULL)
	 && (editbot != filebot); i++, editbot = editbot->next);
Robert Siemborski's avatar
Robert Siemborski committed
1944
}
1945

Chris Allegretta's avatar
Chris Allegretta committed
1946
1947
1948
1949
1950
1951
/* 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
1952
1953
    highlight_word =
	mallocstrcpy(highlight_word, &current->data[current_x]);
Chris Allegretta's avatar
Chris Allegretta committed
1954
1955
    highlight_word[strlen(word)] = '\0';

1956
    /* adjust output when word extends beyond screen */
Chris Allegretta's avatar
Chris Allegretta committed
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968

    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
1969

Chris Allegretta's avatar
Chris Allegretta committed
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
    if (highlight_flag)
	wattron(edit, A_REVERSE);

    waddstr(edit, highlight_word);

    if (highlight_flag)
	wattroff(edit, A_REVERSE);

    free(highlight_word);
}

1981
#ifdef NANO_EXTRA
1982
#define CREDIT_LEN 52
1983
1984
void do_credits(void)
{
Chris Allegretta's avatar
Chris Allegretta committed
1985
    int i, j = 0, k, place = 0, start_x;
1986
1987
1988
1989
1990
1991
1992
    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");
1993
    char *ncurses = _("For ncurses:");
1994
1995
1996
    char *anyonelse = _("and anyone else we forgot...");
    char *thankyou = _("Thank you for using nano!\n");

Chris Allegretta's avatar
Chris Allegretta committed
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
    char *credits[CREDIT_LEN] = { nanotext,
	version,
	VERSION,
	"",
	brought,
	"Chris Allegretta",
	"Jordi Mallach",
	"Adam Rogoyski",
	"Rob Siemborski",
	"Rocco Corsi",
2007
	"David Lawrence Ramsey",
Chris Allegretta's avatar
Chris Allegretta committed
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
	"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,
2033
2034
2035
2036
	"Thomas Dickey",
	"Pavel Curtis",
	"Zeyd Ben-Halim",
	"Eric S. Raymond",
Chris Allegretta's avatar
Chris Allegretta committed
2037
2038
2039
2040
2041
2042
	anyonelse,
	thankyou,
	"", "", "", "",
	"(c) 1999-2002 Chris Allegretta",
	"", "", "", "",
	"www.nano-editor.org"
2043
2044
2045
2046
2047
2048
    };

    curs_set(0);
    nodelay(edit, TRUE);
    blank_bottombars();
    mvwaddstr(topwin, 0, 0, hblank);
Chris Allegretta's avatar
Chris Allegretta committed
2049
2050
    blank_edit();
    wrefresh(edit);
2051
2052
2053
2054
    wrefresh(bottomwin);
    wrefresh(topwin);

    while (wgetch(edit) == ERR) {
Chris Allegretta's avatar
Chris Allegretta committed
2055
2056
	for (k = 0; k <= 1; k++) {
	    blank_edit();
Chris Allegretta's avatar
Chris Allegretta committed
2057
2058
	    for (i = editwinrows / 2 - 1; i >= (editwinrows / 2 - 1 - j);
		 i--) {
Chris Allegretta's avatar
Chris Allegretta committed
2059
2060
2061
2062
2063
2064
2065
		mvwaddstr(edit, i * 2 - k, 0, hblank);

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

2066
		start_x = COLS / 2 - strlen(what) / 2 - 1;
Chris Allegretta's avatar
Chris Allegretta committed
2067
2068
2069
2070
		mvwaddstr(edit, i * 2 - k, start_x, what);
	    }
	    usleep(700000);
	    wrefresh(edit);
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
	}
	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
2085
}
2086
#endif
2087

Chris Allegretta's avatar
Chris Allegretta committed
2088
int keypad_on(WINDOW * win, int newval)
2089
2090
{

2091
/* This is taken right from aumix.  Don't sue me. */
2092
#ifdef HAVE_USEKEYPAD
Chris Allegretta's avatar
Chris Allegretta committed
2093
    int old;
2094
2095

    old = win->_use_keypad;
2096
    keypad(win, newval);
2097
2098
    return old;
#else
2099
    keypad(win, newval);
2100
    return 1;
Chris Allegretta's avatar
Chris Allegretta committed
2101
#endif				/* HAVE_USEKEYPAD */
2102
2103

}