blob: 3a92bad0a0b8daee9df6f200854ee179076fd1be [file] [log] [blame]
Chris Allegretta11b00112000-08-06 21:13:45 +00001/* $Id$ */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002/**************************************************************************
3 * winio.c *
4 * *
5 * Copyright (C) 1999 Chris Allegretta *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 1, or (at your option) *
9 * any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 * *
20 **************************************************************************/
21
22#include <stdarg.h>
23#include <string.h>
Chris Allegrettadba37ae2000-07-07 05:13:09 +000024#include <stdlib.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000025#include "config.h"
26#include "proto.h"
27#include "nano.h"
28
29#ifndef NANO_SMALL
30#include <libintl.h>
31#define _(string) gettext(string)
32#else
33#define _(string) (string)
34#endif
35
36static int statblank = 0; /* Number of keystrokes left after
37 we call statubar() before we
38 actually blank the statusbar */
Robert Siemborskid8510b22000-06-06 23:04:06 +000039
40/* Local Function Prototypes for only winio.c */
41inline int get_page_from_virtual(int virtual);
42inline int get_page_start_virtual(int page);
43inline int get_page_end_virtual(int page);
44
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000045/* Window I/O */
46
47int do_first_line(void)
48{
49 current = fileage;
50 placewewant = 0;
51 current_x = 0;
Chris Allegretta234a34d2000-07-29 04:33:38 +000052 edit_update(current, CENTER);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000053 return 1;
54}
55
56int do_last_line(void)
57{
58 current = filebot;
59 placewewant = 0;
60 current_x = 0;
Chris Allegretta234a34d2000-07-29 04:33:38 +000061 edit_update(current, CENTER);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000062 return 1;
63}
64
65/* Like xplustabs, but for a specifc index of a speficific filestruct */
66int xpt(filestruct * fileptr, int index)
67{
68 int i, tabs = 0;
69
70 if (fileptr == NULL || fileptr->data == NULL)
71 return 0;
72
73 for (i = 0; i < index && fileptr->data[i] != 0; i++) {
74 tabs++;
75
76 if (fileptr->data[i] == NANO_CONTROL_I) {
Chris Allegretta6d690a32000-08-03 22:51:21 +000077 if (tabs % tabsize == 0);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000078 else
Chris Allegretta6d690a32000-08-03 22:51:21 +000079 tabs += tabsize - (tabs % tabsize);
Chris Allegretta4da1fc62000-06-21 03:00:43 +000080 } else if (fileptr->data[i] & 0x80)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000081 /* Make 8 bit chars only 1 collumn! */
82 ;
83 else if (fileptr->data[i] < 32)
84 tabs++;
85 }
86
87 return tabs;
88}
89
90
91/* Return the actual place on the screen of current->data[current_x], which
92 should always be > current_x */
93int xplustabs(void)
94{
95 return xpt(current, current_x);
96}
97
98
Robert Siemborskid8510b22000-06-06 23:04:06 +000099/* Return what current_x should be, given xplustabs() for the line,
100 * given a start position in the filestruct's data */
101int actual_x_from_start(filestruct * fileptr, int xplus, int start)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000102{
Robert Siemborskid8510b22000-06-06 23:04:06 +0000103 int i, tot = 1;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000104
105 if (fileptr == NULL || fileptr->data == NULL)
106 return 0;
107
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000108 for (i = start; tot <= xplus && fileptr->data[i] != 0; i++, tot++)
Robert Siemborskid8510b22000-06-06 23:04:06 +0000109 if (fileptr->data[i] == NANO_CONTROL_I) {
Chris Allegretta6d690a32000-08-03 22:51:21 +0000110 if (tot % tabsize == 0)
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000111 tot++;
112 else
Chris Allegretta6d690a32000-08-03 22:51:21 +0000113 tot += tabsize - (tot % tabsize);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000114 } else if (fileptr->data[i] & 0x80)
115 tot++; /* Make 8 bit chars only 1 column (again) */
116 else if (fileptr->data[i] < 32)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000117 tot += 2;
118
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000119#ifdef DEBUG
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000120 fprintf(stderr, _("actual_x_from_start for xplus=%d returned %d\n"),
121 xplus, i);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000122#endif
Robert Siemborskid8510b22000-06-06 23:04:06 +0000123 return i - start;
124}
125
126/* Opposite of xplustabs */
127inline int actual_x(filestruct * fileptr, int xplus)
128{
129 return actual_x_from_start(fileptr, xplus, 0);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000130}
131
132/* a strlen with tabs factored in, similar to xplustabs() */
133int strlenpt(char *buf)
134{
135 int i, tabs = 0;
136
137 if (buf == NULL)
138 return 0;
139
140 for (i = 0; buf[i] != 0; i++) {
141 tabs++;
142
143 if (buf[i] == NANO_CONTROL_I) {
Chris Allegretta6d690a32000-08-03 22:51:21 +0000144 if (tabs % tabsize == 0);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000145 else
Chris Allegretta6d690a32000-08-03 22:51:21 +0000146 tabs += tabsize - (tabs % tabsize);
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000147 } else if (buf[i] & 0x80)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000148 /* Make 8 bit chars only 1 collumn! */
149 ;
150 else if (buf[i] < 32)
151 tabs++;
152 }
153
154 return tabs;
155}
156
157
158/* resets current_y based on the position of current and puts the cursor at
159 (current_y, current_x) */
160void reset_cursor(void)
161{
162 filestruct *ptr = edittop;
163 int x;
164
165 current_y = 0;
166
167 while (ptr != current && ptr != editbot && ptr->next != NULL) {
168 ptr = ptr->next;
169 current_y++;
170 }
171
172 x = xplustabs();
173 if (x <= COLS - 2)
Robert Siemborskid8510b22000-06-06 23:04:06 +0000174 wmove(edit, current_y, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000175 else
Robert Siemborskid8510b22000-06-06 23:04:06 +0000176 wmove(edit, current_y, x -
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000177 get_page_start_virtual(get_page_from_virtual(x)));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000178
179}
180
181void blank_bottombars(void)
182{
183 int i = no_help()? 3 : 1;
184
185 for (; i <= 2; i++)
186 mvwaddstr(bottomwin, i, 0, hblank);
187
188}
189
190void blank_edit(void)
191{
192 int i;
193 for (i = 0; i <= editwinrows - 1; i++)
194 mvwaddstr(edit, i, 0, hblank);
195 wrefresh(edit);
196}
197
198
199void blank_statusbar(void)
200{
201 mvwaddstr(bottomwin, 0, 0, hblank);
202}
203
204void blank_statusbar_refresh(void)
205{
206 blank_statusbar();
207 wrefresh(bottomwin);
208}
209
210void check_statblank(void)
211{
212
213 if (statblank > 1)
214 statblank--;
215 else if (statblank == 1 && !ISSET(CONSTUPDATE)) {
216 statblank--;
217 blank_statusbar_refresh();
218 }
219}
220
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000221/* Repaint the statusbar when getting a character in nanogetstr */
222void nanoget_repaint(char *buf, char *inputbuf, int x)
223{
224 blank_statusbar();
Chris Allegretta31925e42000-11-02 04:40:39 +0000225 if (x <= COLS) {
226 mvwaddstr(bottomwin, 0, 0, buf);
227 waddnstr(bottomwin, inputbuf, COLS - strlen(buf));
228
229 } else if (x > COLS && x <= COLS * 2)
230 mvwaddnstr(bottomwin, 0, 0, &inputbuf[COLS - strlen(buf)], COLS);
231 else
232 mvwaddnstr(bottomwin, 0, 0, &inputbuf[COLS * (x / COLS) -
233 strlen(buf)], COLS);
234
235 wmove(bottomwin, 0, (x % COLS));
236
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000237}
238
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000239/* Get the input from the kb, this should only be called from statusq */
240int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
241{
242 int kbinput = 0, j = 0, x = 0, xend;
Chris Allegretta31925e42000-11-02 04:40:39 +0000243 int x_left = 0, inputlen;
244 char *inputbuf;
245
246 inputbuf = nmalloc(strlen(def) + 1);
247 inputbuf[0] = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000248
249 x_left = strlen(buf);
250 x = strlen(def) + x_left;
251
252 /* Get the input! */
Chris Allegretta31925e42000-11-02 04:40:39 +0000253 if (strlen(def) > 0)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000254 strcpy(inputbuf, def);
Chris Allegretta31925e42000-11-02 04:40:39 +0000255
256 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000257
258 while ((kbinput = wgetch(bottomwin)) != 13) {
259 for (j = 0; j <= slen - 1; j++) {
260 if (kbinput == s[j].val) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000261 strcpy(answer, "");
262 return s[j].val;
263 }
264 }
265 xend = strlen(buf) + strlen(inputbuf);
266
267 switch (kbinput) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000268 /* Stuff we want to equate with <enter>, ASCII 13 */
269 case 343:
Chris Allegrettaf9b6c9b2000-10-18 19:35:59 +0000270 ungetch(13); /* Enter on iris-ansi $TERM, sometimes */
271 break;
272
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000273 case KEY_HOME:
274 x = x_left;
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000275 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000276 break;
277 case KEY_END:
278 x = x_left + strlen(inputbuf);
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000279 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000280 break;
281 case KEY_RIGHT:
282
283 if (x < xend)
284 x++;
285 wmove(bottomwin, 0, x);
286 break;
287 case NANO_CONTROL_D:
288 if (strlen(inputbuf) > 0 && (x - x_left) != strlen(inputbuf)) {
289 memmove(inputbuf + (x - x_left),
290 inputbuf + (x - x_left) + 1,
291 strlen(inputbuf) - (x - x_left) - 1);
292 inputbuf[strlen(inputbuf) - 1] = 0;
293 }
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000294 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000295 break;
296 case NANO_CONTROL_K:
297 case NANO_CONTROL_U:
298 *inputbuf = 0;
299 x = x_left;
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000300 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000301 break;
302 case KEY_BACKSPACE:
303 case KEY_DC:
304 case 127:
305 case NANO_CONTROL_H:
306 if (strlen(inputbuf) > 0) {
307 if (x == (x_left + strlen(inputbuf)))
308 inputbuf[strlen(inputbuf) - 1] = 0;
309 else if (x - x_left) {
310 memmove(inputbuf + (x - x_left) - 1,
311 inputbuf + (x - x_left),
312 strlen(inputbuf) - (x - x_left));
313 inputbuf[strlen(inputbuf) - 1] = 0;
314 }
315 }
Chris Allegretta31925e42000-11-02 04:40:39 +0000316 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000317 case KEY_LEFT:
318 if (x > strlen(buf))
319 x--;
320 wmove(bottomwin, 0, x);
321 break;
322 case KEY_UP:
323 case KEY_DOWN:
324 break;
325
326 case 27:
327 switch (kbinput = wgetch(edit)) {
328 case 79:
329 switch (kbinput = wgetch(edit)) {
330 case 70:
331 x = x_left + strlen(inputbuf);
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000332 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000333 break;
334 case 72:
335 x = x_left;
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000336 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000337 break;
338 }
339 break;
340 case 91:
341 switch (kbinput = wgetch(edit)) {
342 case 'C':
343 if (x < xend)
344 x++;
345 wmove(bottomwin, 0, x);
346 break;
347 case 'D':
348 if (x > strlen(buf))
349 x--;
350 wmove(bottomwin, 0, x);
351 break;
352 case 49:
353 x = x_left;
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000354 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000355 goto skip_126;
356 case 51:
357 if (strlen(inputbuf) > 0
358 && (x - x_left) != strlen(inputbuf)) {
359 memmove(inputbuf + (x - x_left),
360 inputbuf + (x - x_left) + 1,
361 strlen(inputbuf) - (x - x_left) - 1);
362 inputbuf[strlen(inputbuf) - 1] = 0;
363 }
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000364 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000365 goto skip_126;
366 case 52:
367 x = x_left + strlen(inputbuf);
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000368 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000369 goto skip_126;
370 skip_126:
371 nodelay(edit, TRUE);
372 kbinput = wgetch(edit);
373 if (kbinput == 126 || kbinput == ERR)
374 kbinput = -1;
375 nodelay(edit, FALSE);
376 break;
377 }
378 }
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000379 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000380 break;
381
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000382 default:
383 if (kbinput < 32)
384 break;
Chris Allegretta31925e42000-11-02 04:40:39 +0000385
386 inputlen = strlen(inputbuf);
387 inputbuf = nrealloc(inputbuf, inputlen + 2);
388
389 memmove(&inputbuf[x - x_left + 1],
390 &inputbuf[x - x_left],
391 inputlen - (x - x_left) + 1);
392 inputbuf[x - x_left] = kbinput;
393
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000394 x++;
395
Chris Allegrettaa0e957b2000-10-24 22:25:36 +0000396 nanoget_repaint(buf, inputbuf, x);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000397#ifdef DEBUG
398 fprintf(stderr, _("input \'%c\' (%d)\n"), kbinput, kbinput);
399#endif
400 }
401 wrefresh(bottomwin);
402 }
403
Chris Allegretta31925e42000-11-02 04:40:39 +0000404 answer = mallocstrcpy(answer, inputbuf);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000405
Chris Allegretta105da332000-10-31 05:10:10 +0000406 /* Now that the text is editable instead of bracketed, we have to
407 check for answer == def, instead of answer == "" */
408/* if (!strcmp(answer, "")) */
409 if (!strcmp(answer, def))
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000410 return -2;
411 else
412 return 0;
413}
414
415void horizbar(WINDOW * win, int y)
416{
417 wattron(win, A_REVERSE);
418 mvwaddstr(win, 0, 0, hblank);
419 wattroff(win, A_REVERSE);
420}
421
422void titlebar(void)
423{
424 int namelen, space;
425
426 horizbar(topwin, 0);
427 wattron(topwin, A_REVERSE);
428 mvwaddstr(topwin, 0, 3, VERMSG);
429
430 space = COLS - strlen(VERMSG) - strlen(VERSION) - 21;
431
432 namelen = strlen(filename);
433
434 if (!strcmp(filename, ""))
435 mvwaddstr(topwin, 0, center_x - 6, _("New Buffer"));
436 else {
437 if (namelen > space) {
438 waddstr(topwin, _(" File: ..."));
439 waddstr(topwin, &filename[namelen - space]);
440 } else {
441 mvwaddstr(topwin, 0, center_x - (namelen / 2 + 1), "File: ");
442 waddstr(topwin, filename);
443 }
444 }
445 if (ISSET(MODIFIED))
446 mvwaddstr(topwin, 0, COLS - 10, _("Modified"));
447 wattroff(topwin, A_REVERSE);
448 wrefresh(topwin);
449 reset_cursor();
450}
451
452void onekey(char *keystroke, char *desc)
453{
454 char description[80];
455
Chris Allegrettae7034c62000-09-15 03:31:09 +0000456 snprintf(description, 12, " %-10s", desc);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000457 wattron(bottomwin, A_REVERSE);
458 waddstr(bottomwin, keystroke);
459 wattroff(bottomwin, A_REVERSE);
460 waddstr(bottomwin, description);
461}
462
463void clear_bottomwin(void)
464{
465 if (ISSET(NO_HELP))
466 return;
467
468 mvwaddstr(bottomwin, 1, 0, hblank);
469 mvwaddstr(bottomwin, 2, 0, hblank);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000470}
471
472void bottombars(shortcut s[], int slen)
473{
474 int i, j, k;
475 char keystr[10];
476
477 if (ISSET(NO_HELP))
478 return;
479
480 /* Determine how many extra spaces are needed to fill the bottom of the screen */
481 k = COLS / 6 - 13;
482
483 clear_bottomwin();
484 wmove(bottomwin, 1, 0);
485 for (i = 0; i <= slen - 1; i += 2) {
Robert Siemborski6af14312000-07-01 21:34:26 +0000486 snprintf(keystr, 10, "^%c", s[i].val + 64);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000487 onekey(keystr, s[i].desc);
488
489 for (j = 0; j < k; j++)
490 waddch(bottomwin, ' ');
491 }
492
493 wmove(bottomwin, 2, 0);
494 for (i = 1; i <= slen - 1; i += 2) {
Robert Siemborski6af14312000-07-01 21:34:26 +0000495 snprintf(keystr, 10, "^%c", s[i].val + 64);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000496 onekey(keystr, s[i].desc);
497
498 for (j = 0; j < k; j++)
499 waddch(bottomwin, ' ');
500 }
501
502 wrefresh(bottomwin);
503
504}
505
506/* If modified is not already set, set it and update titlebar */
507void set_modified(void)
508{
509 if (!ISSET(MODIFIED)) {
510 SET(MODIFIED);
511 titlebar();
512 wrefresh(topwin);
513 }
514}
515
Robert Siemborski9d584552000-07-08 00:41:29 +0000516/* And so start the display update routines */
517/* Given a column, this returns the "page" it is on */
518/* "page" in the case of the display columns, means which set of 80 */
519/* characters is viewable (ie: page 1 shows from 1 to COLS) */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000520inline int get_page_from_virtual(int virtual)
521{
Robert Siemborskid8510b22000-06-06 23:04:06 +0000522 int page = 2;
523
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000524 if (virtual <= COLS - 2)
525 return 1;
Robert Siemborskie8c6fd02000-06-07 04:40:09 +0000526 virtual -= (COLS - 2);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000527
528 while (virtual > COLS - 2 - 7) {
Robert Siemborskie8c6fd02000-06-07 04:40:09 +0000529 virtual -= (COLS - 2 - 7);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000530 page++;
531 }
532
533 return page;
534}
535
Robert Siemborski9d584552000-07-08 00:41:29 +0000536/* The inverse of the above function */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000537inline int get_page_start_virtual(int page)
538{
Robert Siemborskie8c6fd02000-06-07 04:40:09 +0000539 int virtual;
540 virtual = --page * (COLS - 7);
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000541 if (page)
542 virtual -= 2 * page - 1;
Robert Siemborskie8c6fd02000-06-07 04:40:09 +0000543 return virtual;
Robert Siemborskid8510b22000-06-06 23:04:06 +0000544}
545
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000546inline int get_page_end_virtual(int page)
547{
Robert Siemborskid8510b22000-06-06 23:04:06 +0000548 return get_page_start_virtual(page) + COLS - 1;
549}
550
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000551#ifndef NANO_SMALL
Robert Siemborski9d584552000-07-08 00:41:29 +0000552/* This takes care of the case where there is a mark that covers only */
553/* the current line. */
554
Chris Allegrettab275aac2000-07-08 01:22:33 +0000555/* It expects a line with no tab characers (ie: the type that edit_add */
Robert Siemborski9d584552000-07-08 00:41:29 +0000556/* deals with */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000557void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
558 int virt_cur_x, int this_page)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000559{
Robert Siemborski9d584552000-07-08 00:41:29 +0000560 /*
561 * The general idea is to break the line up into 3 sections: before
562 * the mark, the mark, and after the mark. We then paint each in
563 * turn (for those that are currently visible, of course
564 *
565 * 3 start points: 0 -> begin, begin->end, end->strlen(data)
566 * in data : pre sel post
567 */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000568 int this_page_start = get_page_start_virtual(this_page),
Robert Siemborski53875912000-06-16 04:25:30 +0000569 this_page_end = get_page_end_virtual(this_page);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000570
Robert Siemborskid8510b22000-06-06 23:04:06 +0000571 /* likewise, 3 data lengths */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000572 int pre_data_len = begin, sel_data_len = end - begin, post_data_len = 0; /* Determined from the other two */
Robert Siemborskid8510b22000-06-06 23:04:06 +0000573
574 /* now fix the start locations & lengths according to the cursor's
575 * position (ie: our page) */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000576 if (pre_data_len < this_page_start)
Robert Siemborskia9addc72000-06-17 06:06:35 +0000577 pre_data_len = 0;
578 else
579 pre_data_len -= this_page_start;
Robert Siemborskid8510b22000-06-06 23:04:06 +0000580
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000581 if (begin < this_page_start)
Robert Siemborskia9addc72000-06-17 06:06:35 +0000582 begin = this_page_start;
583
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000584 if (end < this_page_start)
Robert Siemborskia9addc72000-06-17 06:06:35 +0000585 end = this_page_start;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000586
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000587 if (begin > this_page_end)
Robert Siemborskia9addc72000-06-17 06:06:35 +0000588 begin = this_page_end;
Robert Siemborskid8510b22000-06-06 23:04:06 +0000589
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000590 if (end > this_page_end)
Robert Siemborskia9addc72000-06-17 06:06:35 +0000591 end = this_page_end;
Robert Siemborskid8510b22000-06-06 23:04:06 +0000592
Robert Siemborski9d584552000-07-08 00:41:29 +0000593 /* Now calculate the lengths */
Robert Siemborskia9addc72000-06-17 06:06:35 +0000594 sel_data_len = end - begin;
595 post_data_len = this_page_end - end;
Robert Siemborskid8510b22000-06-06 23:04:06 +0000596
Robert Siemborski9d584552000-07-08 00:41:29 +0000597 /* Paint this line! */
Robert Siemborski53875912000-06-16 04:25:30 +0000598 mvwaddnstr(edit, y, 0, &fileptr->data[this_page_start], pre_data_len);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000599 wattron(edit, A_REVERSE);
Robert Siemborskia9addc72000-06-17 06:06:35 +0000600 mvwaddnstr(edit, y, begin - this_page_start,
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000601 &fileptr->data[begin], sel_data_len);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000602 wattroff(edit, A_REVERSE);
Robert Siemborskia9addc72000-06-17 06:06:35 +0000603 mvwaddnstr(edit, y, end - this_page_start,
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000604 &fileptr->data[end], post_data_len);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000605}
606#endif
607
Robert Siemborski9d584552000-07-08 00:41:29 +0000608/* edit_add takes care of the job of actually painting a line into the
609 * edit window.
610 *
611 * Called only from update_line. Expects a converted-to-not-have-tabs
Robert Siemborski53875912000-06-16 04:25:30 +0000612 * line */
613void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000614 int virt_mark_beginx, int this_page)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000615{
616#ifndef NANO_SMALL
Robert Siemborski9d584552000-07-08 00:41:29 +0000617 /* There are quite a few cases that could take place, we'll deal
618 * with them each in turn */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000619 if (ISSET(MARK_ISSET)
Robert Siemborski9d584552000-07-08 00:41:29 +0000620 && !((fileptr->lineno > mark_beginbuf->lineno
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000621 && fileptr->lineno > current->lineno)
622 || (fileptr->lineno < mark_beginbuf->lineno
623 && fileptr->lineno < current->lineno))) {
624 /* If we get here we are on a line that is atleast
625 * partially selected. The lineno checks above determined
626 * that */
627 if (fileptr != mark_beginbuf && fileptr != current) {
628 /* We are on a completely marked line, paint it all
629 * inverse */
630 wattron(edit, A_REVERSE);
631 mvwaddnstr(edit, yval, 0, fileptr->data, COLS);
632 wattroff(edit, A_REVERSE);
633 } else if (fileptr == mark_beginbuf && fileptr == current) {
634 /* Special case, we're still on the same line we started
635 * marking -- so we call our helper function */
636 if (virt_cur_x < virt_mark_beginx) {
637 /* To the right of us is marked */
638 add_marked_sameline(virt_cur_x, virt_mark_beginx,
639 fileptr, yval, virt_cur_x, this_page);
640 } else {
641 /* To the left of us is marked */
642 add_marked_sameline(virt_mark_beginx, virt_cur_x,
643 fileptr, yval, virt_cur_x, this_page);
644 }
645 } else if (fileptr == mark_beginbuf) {
646 /*
647 * we're updating the line that was first marked
648 * but we're not currently on it. So we want to
649 * figur out which half to invert based on our
650 * relative line numbers.
651 *
652 * i.e. If we're above the "beginbuf" line, we want to
653 * mark the left side. Otherwise we're below, so we
654 * mark the right
655 */
656 int target;
657
658 if (mark_beginbuf->lineno > current->lineno)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000659 wattron(edit, A_REVERSE);
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000660
661 target =
662 (virt_mark_beginx <
663 COLS - 1) ? virt_mark_beginx : COLS - 1;
664
665 mvwaddnstr(edit, yval, 0, fileptr->data, target);
666
667 if (mark_beginbuf->lineno < current->lineno)
668 wattron(edit, A_REVERSE);
669 else
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000670 wattroff(edit, A_REVERSE);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000671
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000672 target = (COLS - 1) - virt_mark_beginx;
673 if (target < 0)
674 target = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000675
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000676 mvwaddnstr(edit, yval, virt_mark_beginx,
677 &fileptr->data[virt_mark_beginx], target);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000678
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000679 if (mark_beginbuf->lineno < current->lineno)
680 wattroff(edit, A_REVERSE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000681
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000682 } else if (fileptr == current) {
683 /* we're on the cursors line, but it's not the first
684 * one we marked. Similar to the previous logic. */
685 int this_page_start = get_page_start_virtual(this_page),
686 this_page_end = get_page_end_virtual(this_page);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000687
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000688 if (mark_beginbuf->lineno < current->lineno)
689 wattron(edit, A_REVERSE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000690
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000691 if (virt_cur_x > COLS - 2) {
692 mvwaddnstr(edit, yval, 0,
693 &fileptr->data[this_page_start],
694 virt_cur_x - this_page_start);
695 } else {
696 mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
697 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000698
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000699 if (mark_beginbuf->lineno > current->lineno)
700 wattron(edit, A_REVERSE);
701 else
702 wattroff(edit, A_REVERSE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000703
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000704 if (virt_cur_x > COLS - 2)
705 mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
706 &fileptr->data[virt_cur_x],
707 this_page_end - virt_cur_x);
708 else
709 mvwaddnstr(edit, yval, virt_cur_x,
710 &fileptr->data[virt_cur_x], COLS - virt_cur_x);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000711
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000712 if (mark_beginbuf->lineno > current->lineno)
713 wattroff(edit, A_REVERSE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000714 }
715
716 } else
717#endif
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000718 /* Just paint the string (no mark on this line) */
Robert Siemborskia9addc72000-06-17 06:06:35 +0000719 mvwaddnstr(edit, yval, 0, &fileptr->data[start],
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000720 get_page_end_virtual(this_page) - start);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000721}
722
723/*
Robert Siemborski9d584552000-07-08 00:41:29 +0000724 * Just update one line in the edit buffer. Basically a wrapper for
725 * edit_add
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000726 *
727 * index gives is a place in the string to update starting from.
728 * Likely args are current_x or 0.
729 */
730void update_line(filestruct * fileptr, int index)
731{
732 filestruct *filetmp;
Robert Siemborski53875912000-06-16 04:25:30 +0000733 int line = 0, col = 0;
734 int virt_cur_x = current_x, virt_mark_beginx = mark_beginx;
735 char *realdata, *tmp;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000736 int i, pos, len, page;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000737
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000738 if (!fileptr)
739 return;
Robert Siemborski53154a72000-06-18 00:11:03 +0000740
Robert Siemborski53875912000-06-16 04:25:30 +0000741 /* First, blank out the line (at a minimum) */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000742 for (filetmp = edittop; filetmp != fileptr && filetmp != editbot;
743 filetmp = filetmp->next)
744 line++;
745
746 mvwaddstr(edit, line, 0, hblank);
747
Robert Siemborski53875912000-06-16 04:25:30 +0000748 /* Next, convert all the tabs to spaces so everything else is easy */
749 index = xpt(fileptr, index);
750
751 realdata = fileptr->data;
752 len = strlen(realdata);
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000753 fileptr->data = nmalloc(xpt(fileptr, len) + 1);
Robert Siemborski53875912000-06-16 04:25:30 +0000754
755 pos = 0;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000756 for (i = 0; i < len; i++) {
757 if (realdata[i] == '\t') {
Robert Siemborski53875912000-06-16 04:25:30 +0000758 do {
759 fileptr->data[pos++] = ' ';
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000760 if (i < current_x)
761 virt_cur_x++;
762 if (i < mark_beginx)
763 virt_mark_beginx++;
Chris Allegretta6d690a32000-08-03 22:51:21 +0000764 } while (pos % tabsize);
Robert Siemborski53875912000-06-16 04:25:30 +0000765 /* must decrement once to account for tab-is-one-character */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000766 if (i < current_x)
767 virt_cur_x--;
768 if (i < mark_beginx)
769 virt_mark_beginx--;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000770 } else if (realdata[i] >= 1 && realdata[i] <= 26) {
771 /* Treat control characters as ^letter */
Chris Allegretta6306a112000-09-02 07:55:41 +0000772 fileptr->data[pos++] = '^';
773 fileptr->data[pos++] = realdata[i] + 64;
Robert Siemborski53875912000-06-16 04:25:30 +0000774 } else {
775 fileptr->data[pos++] = realdata[i];
776 }
777 }
778
779 fileptr->data[pos] = '\0';
780
781 /* Now, Paint the line */
782 if (current == fileptr && index > COLS - 2) {
Robert Siemborskia9addc72000-06-17 06:06:35 +0000783 /* This handles when the current line is beyond COLS */
784 /* It requires figureing out what page we're at */
785 page = get_page_from_virtual(index);
Robert Siemborskid8510b22000-06-06 23:04:06 +0000786 col = get_page_start_virtual(page);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000787
Robert Siemborskia9addc72000-06-17 06:06:35 +0000788 edit_add(filetmp, line, col, virt_cur_x, virt_mark_beginx, page);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000789 mvwaddch(edit, line, 0, '$');
790
Robert Siemborskid8510b22000-06-06 23:04:06 +0000791 if (strlenpt(fileptr->data) > get_page_end_virtual(page))
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000792 mvwaddch(edit, line, COLS - 1, '$');
Robert Siemborskid8510b22000-06-06 23:04:06 +0000793 } else {
Robert Siemborskia9addc72000-06-17 06:06:35 +0000794 /* It's not the current line means that it's at x=0 and page=1 */
795 /* If it is the current line, then we're in the same boat */
796 edit_add(filetmp, line, 0, virt_cur_x, virt_mark_beginx, 1);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000797
Robert Siemborski53875912000-06-16 04:25:30 +0000798 if (strlenpt(&filetmp->data[col]) > COLS)
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000799 mvwaddch(edit, line, COLS - 1, '$');
Robert Siemborskid8510b22000-06-06 23:04:06 +0000800 }
Robert Siemborski53875912000-06-16 04:25:30 +0000801
802 /* Clean up our mess */
803 tmp = fileptr->data;
804 fileptr->data = realdata;
805 free(tmp);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000806}
807
808void center_cursor(void)
809{
810 current_y = editwinrows / 2;
811 wmove(edit, current_y, current_x);
812}
813
814/* Refresh the screen without changing the position of lines */
815void edit_refresh(void)
816{
Chris Allegrettaed022162000-08-03 16:54:11 +0000817 static int noloop = 0;
Chris Allegretta95b0b522000-07-28 02:58:06 +0000818 int lines = 0, i = 0, currentcheck = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000819 filestruct *temp, *hold = current;
820
821 if (current == NULL)
822 return;
823
824 temp = edittop;
825
826 while (lines <= editwinrows - 1 && lines <= totlines && temp != NULL) {
827 hold = temp;
828 update_line(temp, current_x);
Chris Allegretta95b0b522000-07-28 02:58:06 +0000829 if (temp == current)
830 currentcheck = 1;
831
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000832 temp = temp->next;
833 lines++;
834 }
Chris Allegrettaed022162000-08-03 16:54:11 +0000835 /* If noloop == 1, then we already did an edit_update without finishing
836 this function. So we don't run edit_update again */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000837 if (!currentcheck && !noloop) { /* Then current has run off the screen... */
Chris Allegrettada721be2000-07-31 01:26:42 +0000838 edit_update(current, CENTER);
Chris Allegrettaed022162000-08-03 16:54:11 +0000839 noloop = 1;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000840 } else if (noloop)
Chris Allegrettaed022162000-08-03 16:54:11 +0000841 noloop = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000842
843 if (lines <= editwinrows - 1)
844 while (lines <= editwinrows - 1) {
845 mvwaddstr(edit, lines, i, hblank);
846 lines++;
847 }
848 if (temp == NULL)
849 editbot = hold;
850 else
851 editbot = temp;
852}
853
854/*
Chris Allegrettaf1d33d32000-08-19 03:53:39 +0000855 * Same as above, but touch the window first so everything is redrawn.
856 */
857void edit_refresh_clearok(void)
858{
859 clearok(edit, TRUE);
860 edit_refresh();
861 clearok(edit, FALSE);
862}
863
864/*
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000865 * Nice generic routine to update the edit buffer given a pointer to the
866 * file struct =)
867 */
Chris Allegretta234a34d2000-07-29 04:33:38 +0000868void edit_update(filestruct * fileptr, int topmidbot)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000869{
Robert Siemborski29e9a762000-07-05 03:16:04 +0000870 int i = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000871 filestruct *temp;
872
873 if (fileptr == NULL)
874 return;
875
876 temp = fileptr;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000877 if (topmidbot == 2);
Chris Allegretta234a34d2000-07-29 04:33:38 +0000878 else if (topmidbot == 0)
879 for (i = 0; i <= editwinrows - 1 && temp->prev != NULL; i++)
880 temp = temp->prev;
881 else
882 for (i = 0; i <= editwinrows / 2 && temp->prev != NULL; i++)
883 temp = temp->prev;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000884
Robert Siemborski29e9a762000-07-05 03:16:04 +0000885 edittop = temp;
886 fix_editbot();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000887
888 edit_refresh();
889}
890
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000891/* This function updates current based on where current_y is, reset_cursor
892 does the opposite */
893void update_cursor(void)
894{
895 int i = 0;
896
897#ifdef DEBUG
898 fprintf(stderr, _("Moved to (%d, %d) in edit buffer\n"), current_y,
899 current_x);
900#endif
901
902 current = edittop;
903 while (i <= current_y - 1 && current->next != NULL) {
904 current = current->next;
905 i++;
906 }
907
908#ifdef DEBUG
909 fprintf(stderr, _("current->data = \"%s\"\n"), current->data);
910#endif
911
912}
913
914/*
915 * Ask a question on the statusbar. Answer will be stored in answer
916 * global. Returns -1 on aborted enter, -2 on a blank string, and 0
917 * otherwise, the valid shortcut key caught, Def is any editable text we
918 * want to put up by default.
919 */
920int statusq(shortcut s[], int slen, char *def, char *msg, ...)
921{
922 va_list ap;
923 char foo[133];
924 int ret;
925
926 bottombars(s, slen);
927
928 va_start(ap, msg);
929 vsnprintf(foo, 132, msg, ap);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000930 va_end(ap);
Chris Allegrettaa4d21622000-07-08 23:57:03 +0000931 strncat(foo, ": ", 132);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000932
933 wattron(bottomwin, A_REVERSE);
934 ret = nanogetstr(foo, def, s, slen, (strlen(foo) + 3));
935 wattroff(bottomwin, A_REVERSE);
936
937 switch (ret) {
938
939 case NANO_FIRSTLINE_KEY:
940 do_first_line();
941 break;
942 case NANO_LASTLINE_KEY:
943 do_last_line();
944 break;
945 case NANO_CANCEL_KEY:
946 return -1;
947 default:
948 blank_statusbar_refresh();
949 }
950
951#ifdef DEBUG
952 fprintf(stderr, _("I got \"%s\"\n"), answer);
953#endif
954
955 return ret;
956}
957
958/*
959 * Ask a simple yes/no question on the statusbar. Returns 1 for Y, 0 for
960 * N, 2 for All (if all is non-zero when passed in) and -1 for abort (^C)
961 */
962int do_yesno(int all, int leavecursor, char *msg, ...)
963{
964 va_list ap;
965 char foo[133];
966 int kbinput, ok = -1;
967
968 /* Write the bottom of the screen */
969 clear_bottomwin();
970 wattron(bottomwin, A_REVERSE);
971 blank_statusbar_refresh();
972 wattroff(bottomwin, A_REVERSE);
973
Jordi Mallach0b0fc492000-06-23 01:00:13 +0000974 /* Remove gettext call for keybindings until we clear the thing up */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000975 if (!ISSET(NO_HELP)) {
976 wmove(bottomwin, 1, 0);
Jordi Mallach0b0fc492000-06-23 01:00:13 +0000977 onekey(" Y", _("Yes"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000978 if (all)
Jordi Mallach0b0fc492000-06-23 01:00:13 +0000979 onekey(" A", _("All"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000980 wmove(bottomwin, 2, 0);
Jordi Mallach0b0fc492000-06-23 01:00:13 +0000981 onekey(" N", _("No"));
982 onekey("^C", _("Cancel"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000983 }
984 va_start(ap, msg);
985 vsnprintf(foo, 132, msg, ap);
986 va_end(ap);
987 wattron(bottomwin, A_REVERSE);
988 mvwaddstr(bottomwin, 0, 0, foo);
989 wattroff(bottomwin, A_REVERSE);
990 wrefresh(bottomwin);
991
992 if (leavecursor == 1)
993 reset_cursor();
994
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000995 while (ok == -1) {
996 kbinput = wgetch(edit);
997
998 switch (kbinput) {
999 case 'Y':
1000 case 'y':
1001 ok = 1;
1002 break;
1003 case 'N':
1004 case 'n':
1005 ok = 0;
1006 break;
1007 case 'A':
1008 case 'a':
1009 if (all)
1010 ok = 2;
1011 break;
1012 case NANO_CONTROL_C:
1013 ok = -2;
1014 break;
1015 }
1016 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001017
1018 /* Then blank the screen */
1019 blank_statusbar_refresh();
1020
1021 if (ok == -2)
1022 return -1;
1023 else
1024 return ok;
1025}
1026
1027void statusbar(char *msg, ...)
1028{
1029 va_list ap;
1030 char foo[133];
1031 int start_x = 0;
1032
1033 va_start(ap, msg);
1034 vsnprintf(foo, 132, msg, ap);
1035 va_end(ap);
1036
1037 start_x = center_x - strlen(foo) / 2 - 1;
1038
1039 /* Blank out line */
1040 blank_statusbar();
1041
1042 wmove(bottomwin, 0, start_x);
1043
1044 wattron(bottomwin, A_REVERSE);
1045
1046 waddstr(bottomwin, "[ ");
1047 waddstr(bottomwin, foo);
1048 waddstr(bottomwin, " ]");
1049 wattroff(bottomwin, A_REVERSE);
1050 wrefresh(bottomwin);
1051
1052 if (ISSET(CONSTUPDATE))
1053 statblank = 1;
1054 else
1055 statblank = 25;
1056}
1057
1058void display_main_list(void)
1059{
1060 bottombars(main_list, MAIN_VISIBLE);
1061}
1062
1063int total_refresh(void)
1064{
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001065 clearok(edit, TRUE);
1066 clearok(topwin, TRUE);
1067 clearok(bottomwin, TRUE);
1068 wnoutrefresh(edit);
1069 wnoutrefresh(topwin);
1070 wnoutrefresh(bottomwin);
1071 doupdate();
1072 clearok(edit, FALSE);
1073 clearok(topwin, FALSE);
1074 clearok(bottomwin, FALSE);
Chris Allegrettaf1d33d32000-08-19 03:53:39 +00001075 edit_refresh();
Chris Allegretta2a42af12000-09-12 23:02:49 +00001076 titlebar();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001077 return 1;
1078}
1079
1080void previous_line(void)
1081{
1082 if (current_y > 0)
1083 current_y--;
1084}
1085
1086int do_cursorpos(void)
1087{
1088 filestruct *fileptr;
1089 float linepct, bytepct;
1090 int i, tot = 0;
1091
1092 if (current == NULL || fileage == NULL)
1093 return 0;
1094
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001095 for (fileptr = fileage; fileptr != current && fileptr != NULL;
1096 fileptr = fileptr->next)
1097 tot += strlen(fileptr->data) + 1;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001098
1099 if (fileptr == NULL)
1100 return -1;
1101
1102 i = tot + current_x;;
1103
1104 for (fileptr = current->next; fileptr != NULL; fileptr = fileptr->next)
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001105 tot += strlen(fileptr->data) + 1;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001106
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001107 if (totlines > 0)
1108 linepct = 100 * current->lineno / totlines;
1109 else
1110 linepct = 0;
1111
1112 if (totsize > 0)
1113 bytepct = 100 * i / totsize;
1114 else
1115 bytepct = 0;
1116
1117#ifdef DEBUG
1118 fprintf(stderr, _("do_cursorpos: linepct = %f, bytepct = %f\n"),
1119 linepct, bytepct);
1120#endif
1121
1122 statusbar(_("line %d of %d (%.0f%%), character %d of %d (%.0f%%)"),
1123 current->lineno, totlines, linepct, i, totsize, bytepct);
1124 reset_cursor();
1125 return 1;
1126}
1127
1128/* Our broken, non-shortcut list compliant help function.
1129 But hey, it's better than nothing, and it's dynamic! */
1130int do_help(void)
1131{
1132#ifndef NANO_SMALL
1133 char *ptr = help_text, *end;
1134 int i, j, row = 0, page = 1, kbinput = 0, no_more = 0;
1135 int no_help_flag = 0;
1136
1137 blank_edit();
1138 curs_set(0);
1139 blank_statusbar();
1140
1141 if (ISSET(NO_HELP)) {
1142
1143 no_help_flag = 1;
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001144 delwin(bottomwin);
1145 bottomwin = newwin(3, COLS, LINES - 3, 0);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001146 keypad(bottomwin, TRUE);
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001147
1148 editwinrows -= no_help();
1149 UNSET(NO_HELP);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001150 bottombars(help_list, HELP_LIST_LEN);
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001151 } else
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001152 bottombars(help_list, HELP_LIST_LEN);
1153
1154 do {
1155 ptr = help_text;
1156 switch (kbinput) {
1157 case NANO_NEXTPAGE_KEY:
1158 case NANO_NEXTPAGE_FKEY:
1159 case KEY_NPAGE:
1160 if (!no_more) {
1161 blank_edit();
1162 page++;
1163 }
1164 break;
1165 case NANO_PREVPAGE_KEY:
1166 case NANO_PREVPAGE_FKEY:
1167 case KEY_PPAGE:
1168 if (page > 1) {
1169 no_more = 0;
1170 blank_edit();
1171 page--;
1172 }
1173 break;
1174 }
1175
1176 /* Calculate where in the text we should be based on the page */
1177 for (i = 1; i < page; i++) {
1178 row = 0;
1179 j = 0;
Chris Allegretta44e73df2000-09-07 03:37:38 +00001180
1181 while (row < editwinrows - 2 && *ptr != '\0') {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001182 if (*ptr == '\n' || j == COLS - 5) {
1183 j = 0;
1184 row++;
1185 }
1186 ptr++;
1187 j++;
1188 }
1189 }
1190
Chris Allegretta44e73df2000-09-07 03:37:38 +00001191 if (i > 1) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001192
Chris Allegretta44e73df2000-09-07 03:37:38 +00001193 }
1194
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001195 i = 0;
1196 j = 0;
1197 while (i < editwinrows && *ptr != '\0') {
1198 end = ptr;
1199 while (*end != '\n' && *end != '\0' && j != COLS - 5) {
1200 end++;
1201 j++;
1202 }
1203 if (j == COLS - 5) {
1204
1205 /* Don't print half a word if we've run of of space */
1206 while (*end != ' ' && *end != '\0') {
1207 end--;
1208 j--;
1209 }
1210 }
1211 mvwaddnstr(edit, i, 0, ptr, j);
1212 j = 0;
1213 i++;
1214 if (*end == '\n')
1215 end++;
1216 ptr = end;
1217 }
1218 if (*ptr == '\0') {
1219 no_more = 1;
1220 continue;
1221 }
1222 } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001223 if (no_help_flag) {
1224 werase(bottomwin);
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001225 wrefresh(bottomwin);
1226 delwin(bottomwin);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001227 SET(NO_HELP);
1228 bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0);
1229 keypad(bottomwin, TRUE);
1230 editwinrows += no_help();
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001231 } else
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001232 display_main_list();
1233
1234 curs_set(1);
1235 edit_refresh();
1236#else
1237 nano_small_msg();
1238#endif
1239
1240 return 1;
1241}
1242
1243/* Dump the current file structure to stderr */
1244void dump_buffer(filestruct * inptr)
1245{
1246#ifdef DEBUG
1247 filestruct *fileptr;
1248
1249 if (inptr == fileage)
1250 fprintf(stderr, _("Dumping file buffer to stderr...\n"));
1251 else if (inptr == cutbuffer)
1252 fprintf(stderr, _("Dumping cutbuffer to stderr...\n"));
1253 else
1254 fprintf(stderr, _("Dumping a buffer to stderr...\n"));
1255
1256 fileptr = inptr;
1257 while (fileptr != NULL) {
1258 fprintf(stderr, "(%ld) %s\n", fileptr->lineno, fileptr->data);
1259 fflush(stderr);
1260 fileptr = fileptr->next;
1261 }
1262#endif /* DEBUG */
1263}
1264
1265void dump_buffer_reverse(filestruct * inptr)
1266{
1267#ifdef DEBUG
1268 filestruct *fileptr;
1269
1270 fileptr = filebot;
1271 while (fileptr != NULL) {
1272 fprintf(stderr, "(%ld) %s\n", fileptr->lineno, fileptr->data);
1273 fflush(stderr);
1274 fileptr = fileptr->prev;
1275 }
1276#endif /* DEBUG */
1277}
Robert Siemborskidd53ec22000-07-04 02:35:19 +00001278
1279/* Fix editbot based on the assumption that edittop is correct */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001280void fix_editbot(void)
1281{
Robert Siemborskidd53ec22000-07-04 02:35:19 +00001282 int i;
1283 editbot = edittop;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001284 for (i = 0; (i <= editwinrows - 1) && (editbot->next != NULL)
1285 && (editbot != filebot); i++, editbot = editbot->next);
Robert Siemborskidd53ec22000-07-04 02:35:19 +00001286}