winio.c 28.3 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**************************************************************************
 *   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>
23
#include <stdlib.h>
Chris Allegretta's avatar
Chris Allegretta committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "config.h"
#include "proto.h"
#include "nano.h"

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

static int statblank = 0;	/* Number of keystrokes left after
				   we call statubar() before we
				   actually blank the statusbar */
38
39
40
41
42
43

/* 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Window I/O */

int do_first_line(void)
{
    current = fileage;
    placewewant = 0;
    current_x = 0;
    edit_update(current);
    return 1;
}

int do_last_line(void)
{
    current = filebot;
    placewewant = 0;
    current_x = 0;
    edit_update(current);
    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) {
Chris Allegretta's avatar
Chris Allegretta committed
76
	    if (tabs % TABSIZE == 0);
Chris Allegretta's avatar
Chris Allegretta committed
77
	    else
Chris Allegretta's avatar
Chris Allegretta committed
78
		tabs += TABSIZE - (tabs % TABSIZE);
Chris Allegretta's avatar
Chris Allegretta committed
79
	} else if (fileptr->data[i] & 0x80)
Chris Allegretta's avatar
Chris Allegretta committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
	    /* 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);
}


98
99
100
/* 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
101
{
102
    int i, tot = 1;
Chris Allegretta's avatar
Chris Allegretta committed
103
104
105
106

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

Chris Allegretta's avatar
Chris Allegretta committed
107
    for (i = start; tot <= xplus && fileptr->data[i] != 0; i++, tot++)
108
	if (fileptr->data[i] == NANO_CONTROL_I) {
Chris Allegretta's avatar
Chris Allegretta committed
109
110
111
112
	    if (tot % TABSIZE == 0)
		tot++;
	    else
		tot += TABSIZE - (tot % TABSIZE);
113
114
115
	} 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
116
117
118
	    tot += 2;

#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
119
120
    fprintf(stderr, _("actual_x_from_start for xplus=%d returned %d\n"),
	    xplus, i);
Chris Allegretta's avatar
Chris Allegretta committed
121
#endif
122
123
124
125
126
127
128
    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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
}

/* 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) {
Chris Allegretta's avatar
Chris Allegretta committed
143
	    if (tabs % TABSIZE == 0);
Chris Allegretta's avatar
Chris Allegretta committed
144
	    else
Chris Allegretta's avatar
Chris Allegretta committed
145
		tabs += TABSIZE - (tabs % TABSIZE);
Chris Allegretta's avatar
Chris Allegretta committed
146
	} else if (buf[i] & 0x80)
Chris Allegretta's avatar
Chris Allegretta committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	    /* 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)
173
	wmove(edit, current_y, x);
Chris Allegretta's avatar
Chris Allegretta committed
174
    else
175
	wmove(edit, current_y, x -
Chris Allegretta's avatar
Chris Allegretta committed
176
	      get_page_start_virtual(get_page_from_virtual(x)));
Chris Allegretta's avatar
Chris Allegretta committed
177
178
179
180
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

}

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


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

/* Get the input from the kb, this should only be called from statusq */
int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
{
    int kbinput = 0, j = 0, x = 0, xend;
    int x_left = 0;
    char inputstr[132], inputbuf[132] = "";

    blank_statusbar();
    mvwaddstr(bottomwin, 0, 0, buf);
    if (strlen(def) > 0)
	waddstr(bottomwin, def);
    wrefresh(bottomwin);

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

    /* Get the input! */
    if (strlen(def) > 0) {
	strcpy(answer, def);
	strcpy(inputbuf, def);
    }

    while ((kbinput = wgetch(bottomwin)) != 13) {
	for (j = 0; j <= slen - 1; j++) {
	    if (kbinput == s[j].val) {
		strcpy(answer, "");
		return s[j].val;
	    }
	}
	xend = strlen(buf) + strlen(inputbuf);

	switch (kbinput) {
	case KEY_HOME:
	    x = x_left;
	    blank_statusbar();
	    mvwaddstr(bottomwin, 0, 0, buf);
	    waddstr(bottomwin, inputbuf);
	    wmove(bottomwin, 0, x);
	    break;
	case KEY_END:
	    x = x_left + strlen(inputbuf);
	    blank_statusbar();
	    mvwaddstr(bottomwin, 0, 0, buf);
	    waddstr(bottomwin, inputbuf);
	    wmove(bottomwin, 0, x);
	    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;
	    }
	    blank_statusbar();
	    mvwaddstr(bottomwin, 0, 0, buf);
	    waddstr(bottomwin, inputbuf);
	    wmove(bottomwin, 0, x);
	    break;
	case NANO_CONTROL_K:
	case NANO_CONTROL_U:
	    *inputbuf = 0;
	    x = x_left;
	    blank_statusbar();
	    mvwaddstr(bottomwin, 0, 0, buf);
	    waddstr(bottomwin, inputbuf);
	    wmove(bottomwin, 0, x);
	    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;
		}
	    }
	    blank_statusbar();
	    mvwaddstr(bottomwin, 0, 0, buf);
	    waddstr(bottomwin, inputbuf);
	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);
		    blank_statusbar();
		    mvwaddstr(bottomwin, 0, 0, buf);
		    waddstr(bottomwin, inputbuf);
		    wmove(bottomwin, 0, x);
		    break;
		case 72:
		    x = x_left;
		    blank_statusbar();
		    mvwaddstr(bottomwin, 0, 0, buf);
		    waddstr(bottomwin, inputbuf);
		    wmove(bottomwin, 0, x);
		    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;
		    blank_statusbar();
		    mvwaddstr(bottomwin, 0, 0, buf);
		    waddstr(bottomwin, inputbuf);
		    wmove(bottomwin, 0, x);
		    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;
		    }
		    blank_statusbar();
		    mvwaddstr(bottomwin, 0, 0, buf);
		    waddstr(bottomwin, inputbuf);
		    wmove(bottomwin, 0, x);
		    goto skip_126;
		case 52:
		    x = x_left + strlen(inputbuf);
		    blank_statusbar();
		    mvwaddstr(bottomwin, 0, 0, buf);
		    waddstr(bottomwin, inputbuf);
		    wmove(bottomwin, 0, x);
		    goto skip_126;
		  skip_126:
		    nodelay(edit, TRUE);
		    kbinput = wgetch(edit);
		    if (kbinput == 126 || kbinput == ERR)
			kbinput = -1;
		    nodelay(edit, FALSE);
		    break;
		}
	    }
	    blank_statusbar();
	    mvwaddstr(bottomwin, 0, 0, buf);
	    waddstr(bottomwin, inputbuf);
	    wmove(bottomwin, 0, x);
	    break;


	default:
	    if (kbinput < 32)
		break;
	    strcpy(inputstr, inputbuf);
	    inputstr[x - strlen(buf)] = kbinput;
	    strcpy(&inputstr[x - strlen(buf) + 1],
		   &inputbuf[x - strlen(buf)]);
	    strcpy(inputbuf, inputstr);
	    x++;

	    mvwaddstr(bottomwin, 0, 0, buf);
	    waddstr(bottomwin, inputbuf);
	    wmove(bottomwin, 0, x);

#ifdef DEBUG
	    fprintf(stderr, _("input \'%c\' (%d)\n"), kbinput, kbinput);
#endif
	}
	wrefresh(bottomwin);
    }

    strncpy(answer, inputbuf, 132);

    if (!strcmp(answer, ""))
	return -2;
    else
	return 0;
}

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

void titlebar(void)
{
    int namelen, space;

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

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

    namelen = strlen(filename);

    if (!strcmp(filename, ""))
	mvwaddstr(topwin, 0, center_x - 6, _("New Buffer"));
    else {
	if (namelen > space) {
	    waddstr(topwin, _("  File: ..."));
	    waddstr(topwin, &filename[namelen - space]);
	} else {
	    mvwaddstr(topwin, 0, center_x - (namelen / 2 + 1), "File: ");
	    waddstr(topwin, filename);
	}
    }
    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];

    snprintf(description, 12, " %-11s", desc);
    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) {
494
	snprintf(keystr, 10, "^%c", s[i].val + 64);
Chris Allegretta's avatar
Chris Allegretta committed
495
496
497
498
499
500
501
502
	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) {
503
	snprintf(keystr, 10, "^%c", s[i].val + 64);
Chris Allegretta's avatar
Chris Allegretta committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
	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);
	titlebar();
	wrefresh(topwin);
    }
}

Chris Allegretta's avatar
Chris Allegretta committed
524
525
inline int get_page_from_virtual(int virtual)
{
526
    int page = 2;
Chris Allegretta's avatar
Chris Allegretta committed
527

Chris Allegretta's avatar
Chris Allegretta committed
528
529
    if (virtual <= COLS - 2)
	return 1;
530
    virtual -= (COLS - 2);
531
532

    while (virtual > COLS - 2 - 7) {
533
	virtual -= (COLS - 2 - 7);
534
	page++;
Chris Allegretta's avatar
Chris Allegretta committed
535
536
    }

537
538
539
    return page;
}

Chris Allegretta's avatar
Chris Allegretta committed
540
541
inline int get_page_start_virtual(int page)
{
542
543
    int virtual;
    virtual = --page * (COLS - 7);
Chris Allegretta's avatar
Chris Allegretta committed
544
545
    if (page)
	virtual -= 2 * page - 1;
546
    return virtual;
547
548
}

Chris Allegretta's avatar
Chris Allegretta committed
549
550
inline int get_page_end_virtual(int page)
{
551
552
553
554
    return get_page_start_virtual(page) + COLS - 1;
}

#ifndef NANO_SMALL
555
556
/* Called only from edit_add, and therefore expects a
 * converted-to-only-spaces line */
Chris Allegretta's avatar
Chris Allegretta committed
557
558
void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
			 int virt_cur_x, int this_page)
559
{
Chris Allegretta's avatar
Chris Allegretta committed
560
    int this_page_start = get_page_start_virtual(this_page),
561
	this_page_end = get_page_end_virtual(this_page);
562
563
564
565

    /* 3 start points: 0 -> begin, begin->end, end->strlen(data) */
    /*    in data    :    pre          sel           post        */
    /* likewise, 3 data lengths */
Chris Allegretta's avatar
Chris Allegretta committed
566
    int pre_data_len = begin, sel_data_len = end - begin, post_data_len = 0;	/* Determined from the other two */
567
568
569

    /* now fix the start locations & lengths according to the cursor's 
     * position (ie: our page) */
Chris Allegretta's avatar
Chris Allegretta committed
570
    if (pre_data_len < this_page_start)
571
572
573
	pre_data_len = 0;
    else
	pre_data_len -= this_page_start;
574

Chris Allegretta's avatar
Chris Allegretta committed
575
    if (begin < this_page_start)
576
577
	begin = this_page_start;

Chris Allegretta's avatar
Chris Allegretta committed
578
    if (end < this_page_start)
579
	end = this_page_start;
580
581
582

    /* we don't care about end, because it will just get cropped
     * due to length */
Chris Allegretta's avatar
Chris Allegretta committed
583
    if (begin > this_page_end)
584
	begin = this_page_end;
585

Chris Allegretta's avatar
Chris Allegretta committed
586
    if (end > this_page_end)
587
	end = this_page_end;
588

589
590
    sel_data_len = end - begin;
    post_data_len = this_page_end - end;
591

592
    mvwaddnstr(edit, y, 0, &fileptr->data[this_page_start], pre_data_len);
593
    wattron(edit, A_REVERSE);
594
    mvwaddnstr(edit, y, begin - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
595
	       &fileptr->data[begin], sel_data_len);
596
    wattroff(edit, A_REVERSE);
597
    mvwaddnstr(edit, y, end - this_page_start,
Chris Allegretta's avatar
Chris Allegretta committed
598
	       &fileptr->data[end], post_data_len);
Chris Allegretta's avatar
Chris Allegretta committed
599
600
601
}
#endif

602
603
604
/* Called only from update_line.  Expects a converted-to-not-have-tabs
 * line */
void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
605
	      int virt_mark_beginx, int this_page)
Chris Allegretta's avatar
Chris Allegretta committed
606
607
608
609
610
611
612
613
614
{
#ifndef NANO_SMALL
    if (ISSET(MARK_ISSET)) {
	if ((fileptr->lineno > mark_beginbuf->lineno
	     && fileptr->lineno > current->lineno)
	    || (fileptr->lineno < mark_beginbuf->lineno
		&& fileptr->lineno < current->lineno)) {

	    /* We're on a normal, unselected line */
615
	    mvwaddnstr(edit, yval, 0, fileptr->data, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
616
617
	} else {
	    if (fileptr != mark_beginbuf && fileptr != current) {
618
		/* We're on selected text */
Chris Allegretta's avatar
Chris Allegretta committed
619
		wattron(edit, A_REVERSE);
620
		mvwaddnstr(edit, yval, 0, fileptr->data, COLS);
Chris Allegretta's avatar
Chris Allegretta committed
621
		wattroff(edit, A_REVERSE);
622
	    } else if (fileptr == mark_beginbuf && fileptr == current) {
Chris Allegretta's avatar
Chris Allegretta committed
623
		/* Special case, we're still on the same line we started
624
		 * marking */
625
626
		if (virt_cur_x < virt_mark_beginx)
		    add_marked_sameline(virt_cur_x, virt_mark_beginx,
Chris Allegretta's avatar
Chris Allegretta committed
627
628
					fileptr, yval, virt_cur_x,
					this_page);
629
		else
630
		    add_marked_sameline(virt_mark_beginx, virt_cur_x,
Chris Allegretta's avatar
Chris Allegretta committed
631
632
					fileptr, yval, virt_cur_x,
					this_page);
Chris Allegretta's avatar
Chris Allegretta committed
633
	    } else if (fileptr == mark_beginbuf) {
634
		/* we're updating the line that was first marked */
635
636
		int target;

Chris Allegretta's avatar
Chris Allegretta committed
637
638
639
		if (mark_beginbuf->lineno > current->lineno)
		    wattron(edit, A_REVERSE);

Chris Allegretta's avatar
Chris Allegretta committed
640
641
642
		target =
		    (virt_mark_beginx <
		     COLS - 1) ? virt_mark_beginx : COLS - 1;
643

644
		mvwaddnstr(edit, yval, 0, fileptr->data, target);
Chris Allegretta's avatar
Chris Allegretta committed
645
646
647
648
649
650

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

651
		target = (COLS - 1) - virt_mark_beginx;
Chris Allegretta's avatar
Chris Allegretta committed
652
653
		if (target < 0)
		    target = 0;
Chris Allegretta's avatar
Chris Allegretta committed
654

655
		mvwaddnstr(edit, yval, virt_mark_beginx,
Chris Allegretta's avatar
Chris Allegretta committed
656
			   &fileptr->data[virt_mark_beginx], target);
Chris Allegretta's avatar
Chris Allegretta committed
657
658
659
660
661

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

	    } else if (fileptr == current) {
662
663
		/* we're on the cursors line, but it's not the first
		 * one we marked... */
664
		int this_page_start = get_page_start_virtual(this_page),
665
666
		    this_page_end = get_page_end_virtual(this_page);

Chris Allegretta's avatar
Chris Allegretta committed
667
668
669
		if (mark_beginbuf->lineno < current->lineno)
		    wattron(edit, A_REVERSE);

670
		if (virt_cur_x > COLS - 2) {
Chris Allegretta's avatar
Chris Allegretta committed
671
672
673
		    mvwaddnstr(edit, yval, 0,
			       &fileptr->data[this_page_start],
			       virt_cur_x - this_page_start);
674
675
676
		} else {
		    mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
		}
Chris Allegretta's avatar
Chris Allegretta committed
677
678
679
680
681
682

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

683
		if (virt_cur_x > COLS - 2)
684
685
		    mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
			       &fileptr->data[virt_cur_x],
Chris Allegretta's avatar
Chris Allegretta committed
686
			       this_page_end - virt_cur_x);
687
		else
688
689
690
		    mvwaddnstr(edit, yval, virt_cur_x,
			       &fileptr->data[virt_cur_x],
			       COLS - virt_cur_x);
Chris Allegretta's avatar
Chris Allegretta committed
691
692
693
694
695
696
697
698

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

    } else
#endif
699
	mvwaddnstr(edit, yval, 0, &fileptr->data[start],
Chris Allegretta's avatar
Chris Allegretta committed
700
		   get_page_end_virtual(this_page) - start);
Chris Allegretta's avatar
Chris Allegretta committed
701
702
703
704
705
706
707
708
709
710
711
}

/*
 * Just update one line in the edit buffer 
 *
 * 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;
712
713
714
    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
715
    int i, pos, len, page;
Chris Allegretta's avatar
Chris Allegretta committed
716

Chris Allegretta's avatar
Chris Allegretta committed
717
718
    if (!fileptr)
	return;
719

720
    /* First, blank out the line (at a minimum) */
Chris Allegretta's avatar
Chris Allegretta committed
721
722
723
724
725
    for (filetmp = edittop; filetmp != fileptr && filetmp != editbot;
	 filetmp = filetmp->next)
	line++;

    mvwaddstr(edit, line, 0, hblank);
726
727
728
729
730
731

    /* 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
732
    fileptr->data = nmalloc(xpt(fileptr, len) + 1);
733
734

    pos = 0;
Chris Allegretta's avatar
Chris Allegretta committed
735
736
    for (i = 0; i < len; i++) {
	if (realdata[i] == '\t') {
737
738
	    do {
		fileptr->data[pos++] = ' ';
Chris Allegretta's avatar
Chris Allegretta committed
739
740
741
742
743
		if (i < current_x)
		    virt_cur_x++;
		if (i < mark_beginx)
		    virt_mark_beginx++;
	    } while (pos % TABSIZE);
744
	    /* must decrement once to account for tab-is-one-character */
Chris Allegretta's avatar
Chris Allegretta committed
745
746
747
748
	    if (i < current_x)
		virt_cur_x--;
	    if (i < mark_beginx)
		virt_mark_beginx--;
749
750
751
752
753
754
	} else {
	    fileptr->data[pos++] = realdata[i];
	}
    }

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

756
757
    /* Now, Paint the line */
    if (current == fileptr && index > COLS - 2) {
758
759
760
	/* This handles when the current line is beyond COLS */
	/* It requires figureing out what page we're at      */
	page = get_page_from_virtual(index);
761
	col = get_page_start_virtual(page);
Chris Allegretta's avatar
Chris Allegretta committed
762

763
	edit_add(filetmp, line, col, virt_cur_x, virt_mark_beginx, page);
Chris Allegretta's avatar
Chris Allegretta committed
764
765
	mvwaddch(edit, line, 0, '$');

766
	if (strlenpt(fileptr->data) > get_page_end_virtual(page))
Chris Allegretta's avatar
Chris Allegretta committed
767
	    mvwaddch(edit, line, COLS - 1, '$');
768
    } else {
769
770
771
	/* 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
772

773
	if (strlenpt(&filetmp->data[col]) > COLS)
Chris Allegretta's avatar
Chris Allegretta committed
774
	    mvwaddch(edit, line, COLS - 1, '$');
775
    }
776
777
778
779
780

    /* Clean up our mess */
    tmp = fileptr->data;
    fileptr->data = realdata;
    free(tmp);
Chris Allegretta's avatar
Chris Allegretta committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
}

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)
{
    int lines = 0, i = 0;
    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);
	temp = temp->next;
	lines++;
    }

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

/*
 * Nice generic routine to update the edit buffer given a pointer to the
 * file struct =) 
 */
void edit_update(filestruct * fileptr)
{
824
    int i = 0;
Chris Allegretta's avatar
Chris Allegretta committed
825
826
827
828
829
830
831
832
833
834
835
    filestruct *temp;

    if (fileptr == NULL)
	return;

    temp = fileptr;
    while (i <= editwinrows / 2 && temp->prev != NULL) {
	i++;
	temp = temp->prev;
    }

836
837
    edittop = temp;
    fix_editbot();
Chris Allegretta's avatar
Chris Allegretta committed
838
839
840
841
842
843
844
845
846
847

    edit_refresh();
}

/* Now we want to update the screen using this struct as the top of the edit buffer */
void edit_update_top(filestruct * fileptr)
{
    int i;
    filestruct *temp = fileptr;

848
    if (fileptr == NULL)
Chris Allegretta's avatar
Chris Allegretta committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
	return;

    i = 0;
    while (i <= editwinrows / 2 && temp->next != NULL) {
	i++;
	temp = temp->next;
    }
    edit_update(temp);
}

/* And also for the bottom... */
void edit_update_bot(filestruct * fileptr)
{
    int i;
    filestruct *temp = fileptr;

    i = 0;
    while (i <= editwinrows / 2 - 1 && temp->prev != NULL) {
	i++;
	temp = temp->prev;
    }
    edit_update(temp);
}

/* 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.
 */
int statusq(shortcut s[], int slen, char *def, char *msg, ...)
{
    va_list ap;
    char foo[133];
    int ret;

    bottombars(s, slen);

    va_start(ap, msg);
    vsnprintf(foo, 132, msg, ap);
    strncat(foo, ": ", 132);
    va_end(ap);

    wattron(bottomwin, A_REVERSE);
    ret = nanogetstr(foo, def, s, slen, (strlen(foo) + 3));
    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:
	blank_statusbar_refresh();
    }

#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];
    int kbinput, ok = -1;

    /* 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
956
    /* Remove gettext call for keybindings until we clear the thing up */
Chris Allegretta's avatar
Chris Allegretta committed
957
958
    if (!ISSET(NO_HELP)) {
	wmove(bottomwin, 1, 0);
Jordi Mallach's avatar
   
Jordi Mallach committed
959
	onekey(" Y", _("Yes"));
Chris Allegretta's avatar
Chris Allegretta committed
960
	if (all)
Jordi Mallach's avatar
   
Jordi Mallach committed
961
	    onekey(" A", _("All"));
Chris Allegretta's avatar
Chris Allegretta committed
962
	wmove(bottomwin, 2, 0);
Jordi Mallach's avatar
   
Jordi Mallach committed
963
964
	onekey(" N", _("No"));
	onekey("^C", _("Cancel"));
Chris Allegretta's avatar
Chris Allegretta committed
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
    }
    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 'Y':
	case 'y':
	    ok = 1;
	    break;
	case 'N':
	case 'n':
	    ok = 0;
	    break;
	case 'A':
	case 'a':
	    if (all)
		ok = 2;
	    break;
	case NANO_CONTROL_C:
	    ok = -2;
	    break;
	}
    }

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

    start_x = center_x - strlen(foo) / 2 - 1;

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

    return 1;
}

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

int do_cursorpos(void)
{
    filestruct *fileptr;
    float linepct, bytepct;
    int i, tot = 0;

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

Chris Allegretta's avatar
Chris Allegretta committed
1076
1077
1078
    for (fileptr = fileage; fileptr != current && fileptr != NULL;
	 fileptr = fileptr->next)
	tot += strlen(fileptr->data) + 1;
Chris Allegretta's avatar
Chris Allegretta committed
1079
1080
1081
1082
1083
1084
1085

    if (fileptr == NULL)
	return -1;

    i = tot + current_x;;

    for (fileptr = current->next; fileptr != NULL; fileptr = fileptr->next)
Chris Allegretta's avatar
Chris Allegretta committed
1086
	tot += strlen(fileptr->data) + 1;
Chris Allegretta's avatar
Chris Allegretta committed
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124

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

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

#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)
{
#ifndef NANO_SMALL
    char *ptr = help_text, *end;
    int i, j, row = 0, page = 1, kbinput = 0, no_more = 0;
    int no_help_flag = 0;

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

    if (ISSET(NO_HELP)) {

	no_help_flag = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1125
1126
	delwin(bottomwin);
	bottomwin = newwin(3, COLS, LINES - 3, 0);
Chris Allegretta's avatar
Chris Allegretta committed
1127
	keypad(bottomwin, TRUE);
Chris Allegretta's avatar
Chris Allegretta committed
1128
1129
1130

	editwinrows -= no_help();
	UNSET(NO_HELP);
Chris Allegretta's avatar
Chris Allegretta committed
1131
	bottombars(help_list, HELP_LIST_LEN);
Chris Allegretta's avatar
Chris Allegretta committed
1132
    } else
Chris Allegretta's avatar
Chris Allegretta committed
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
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
	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;
	    while (row < editwinrows && *ptr != '\0') {
		if (*ptr == '\n' || j == COLS - 5) {
		    j = 0;
		    row++;
		}
		ptr++;
		j++;
	    }
	}

	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;
	}
    } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY);

    if (no_help_flag) {
	werase(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
1202
1203
	wrefresh(bottomwin);
	delwin(bottomwin);
Chris Allegretta's avatar
Chris Allegretta committed
1204
1205
1206
1207
	SET(NO_HELP);
	bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0);
	keypad(bottomwin, TRUE);
	editwinrows += no_help();
Chris Allegretta's avatar
Chris Allegretta committed
1208
    } else
Chris Allegretta's avatar
Chris Allegretta committed
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
	display_main_list();

    curs_set(1);
    edit_refresh();
#else
    nano_small_msg();
#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
1255
1256
1257
1258
1259
1260
1261
1262
1263

/* Fix editbot based on the assumption that edittop is correct */
void fix_editbot(void) {
    int i;
    editbot = edittop;
    for(i = 0; (i <= editwinrows - 1) && (editbot->next != NULL)
	&& (editbot != filebot); i++, editbot = editbot->next);
}