blob: 6df7ced9c492661261a6240bfe61aae423a3ff32 [file] [log] [blame]
Chris Allegretta11b00112000-08-06 21:13:45 +00001/* $Id$ */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002/**************************************************************************
3 * nano.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 <stdio.h>
23#include <stdlib.h>
24#include <stdarg.h>
25#include <signal.h>
26#include <unistd.h>
27#include <string.h>
28#include <fcntl.h>
29#include <sys/stat.h>
30#include <sys/ioctl.h>
31#include <sys/param.h>
Chris Allegretta27eb13f2000-11-05 16:52:21 +000032#include <sys/types.h>
33#include <sys/wait.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000034#include <errno.h>
35#include <ctype.h>
36#include <locale.h>
37#include <limits.h>
Adam Rogoyski77f36de2000-06-07 03:56:54 +000038#include <assert.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000039
40#include "config.h"
41#include "proto.h"
42#include "nano.h"
43
44#ifndef NANO_SMALL
45#include <libintl.h>
46#define _(string) gettext(string)
47#else
48#define _(string) (string)
49#endif
50
51#ifdef HAVE_TERMIOS_H
52#include <termios.h>
53#endif
54
55#ifdef HAVE_TERMIO_H
56#include <termio.h>
57#endif
58
59#ifdef HAVE_GETOPT_H
60#include <getopt.h>
61#endif
62
63/* Former globals, now static */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000064int fill = 0; /* Fill - where to wrap lines, basically */
Rocco Corsiaf5c3022001-01-12 07:51:05 +000065
66#ifndef DISABLE_SPELLER
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000067static char *alt_speller; /* Alternative spell command */
Rocco Corsiaf5c3022001-01-12 07:51:05 +000068#endif
69
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000070struct termios oldterm; /* The user's original term settings */
Chris Allegretta18f8be02000-09-04 03:20:38 +000071static struct sigaction act; /* For all out fun signal handlers */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000072
Rocco Corsiaf5c3022001-01-12 07:51:05 +000073#ifndef DISABLE_HELP
Chris Allegretta65121632000-12-18 07:05:27 +000074static char *help_text_init = ""; /* Initial message, not including shortcuts */
75#endif
76
Chris Allegretta27eb13f2000-11-05 16:52:21 +000077char *last_search = NULL; /* Last string we searched for */
78char *last_replace = NULL; /* Last replacement string */
79int search_last_line; /* Is this the last search line? */
80
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000081/* What we do when we're all set to exit */
82RETSIGTYPE finish(int sigage)
83{
Chris Allegrettac08f50d2001-01-06 18:12:43 +000084
85 keypad(edit, TRUE);
86 keypad(bottomwin, TRUE);
87
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000088 if (!ISSET(NO_HELP)) {
89 mvwaddstr(bottomwin, 1, 0, hblank);
90 mvwaddstr(bottomwin, 2, 0, hblank);
Chris Allegretta4da1fc62000-06-21 03:00:43 +000091 } else
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000092 mvwaddstr(bottomwin, 0, 0, hblank);
Chris Allegretta1cc0b7f2000-11-03 01:29:04 +000093
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000094 wrefresh(bottomwin);
95 endwin();
96
97 /* Restore the old term settings */
Chris Allegretta4da1fc62000-06-21 03:00:43 +000098 tcsetattr(0, TCSANOW, &oldterm);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000099
100 exit(sigage);
101}
102
103/* Die (gracefully?) */
104void die(char *msg, ...)
105{
106 va_list ap;
Chris Allegretta3dbb2782000-12-02 04:36:50 +0000107 char *name;
108 int i;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000109
110 va_start(ap, msg);
111 vfprintf(stderr, msg, ap);
112 va_end(ap);
113
114 /* if we can't save we have REAL bad problems,
Robert Siemborskifcf32bf2000-07-17 03:04:54 +0000115 * but we might as well TRY. */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000116 if (filename[0] == '\0') {
Chris Allegretta3dbb2782000-12-02 04:36:50 +0000117 name = "nano.save";
118 i = write_file(name, 1);
Robert Siemborskifcf32bf2000-07-17 03:04:54 +0000119 } else {
Chris Allegrettae7a58932000-12-02 02:36:22 +0000120
121 char *buf = nmalloc(strlen(filename) + 6);
122 strcpy(buf, filename);
123 strcat(buf, ".save");
Chris Allegretta3dbb2782000-12-02 04:36:50 +0000124 i = write_file(buf, 1);
125 name = buf;
Robert Siemborskifcf32bf2000-07-17 03:04:54 +0000126 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000127 /* Restore the old term settings */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000128 tcsetattr(0, TCSANOW, &oldterm);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000129
130 clear();
131 refresh();
132 resetty();
133 endwin();
134
135 fprintf(stderr, msg);
Chris Allegretta3dbb2782000-12-02 04:36:50 +0000136 if (i != -1)
137 fprintf(stderr, _("\nBuffer written to %s\n"), name);
138 else
Chris Allegretta9756d622000-12-03 03:06:45 +0000139 fprintf(stderr, _("\nNo %s written (file exists?)\n"), name);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000140
141 exit(1); /* We have a problem: exit w/ errorlevel(1) */
142}
143
Chris Allegrettae61e8302001-01-14 05:18:27 +0000144/* Die with an error message that the screen was too small if, well, the
145 screen is too small */
146void die_too_small(void)
147{
148 char *too_small_msg = _("Window size is too small for Nano...");
149
150 die(too_small_msg);
151
152}
153
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000154void print_view_warning(void)
155{
156 statusbar(_("Key illegal in VIEW mode"));
157}
158
Chris Allegretta1a6e9042000-12-14 13:56:28 +0000159void clear_filename(void)
160{
161 if (filename != NULL)
162 free(filename);
163 filename = nmalloc(1);
164 filename[0] = 0;
165}
Chris Allegretta1cc0b7f2000-11-03 01:29:04 +0000166
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000167/* Initialize global variables - no better way for now */
168void global_init(void)
169{
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000170 current_x = 0;
171 current_y = 0;
Chris Allegrettae61e8302001-01-14 05:18:27 +0000172
173 if ((editwinrows = LINES - 5 + no_help()) < MIN_EDITOR_ROWS)
174 die_too_small();
175
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000176 fileage = NULL;
177 cutbuffer = NULL;
178 current = NULL;
179 edittop = NULL;
180 editbot = NULL;
181 totlines = 0;
182 placewewant = 0;
Chris Allegrettae61e8302001-01-14 05:18:27 +0000183
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000184 if (!fill)
Chris Allegrettae61e8302001-01-14 05:18:27 +0000185 fill = COLS - CHARS_FROM_EOL;
186
187 if (fill < MIN_FILL_LENGTH)
188 die_too_small();
189
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000190 hblank = nmalloc(COLS + 1);
Chris Allegretta0a06e072001-01-23 02:35:04 +0000191 memset(hblank, ' ', COLS);
192 hblank[COLS] = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000193}
194
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000195#ifndef DISABLE_HELP
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000196void init_help_msg(void)
197{
198
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000199 help_text_init =
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000200 _(" nano help text\n\n "
201 "The nano editor is designed to emulate the functionality and "
202 "ease-of-use of the UW Pico text editor. There are four main "
203 "sections of the editor: The top line shows the program "
204 "version, the current filename being edited, and whether "
205 "or not the file has been modified. Next is the main editor "
206 "window showing the file being edited. The status line is "
207 "the third line from the bottom and shows important messages. "
208 "The bottom two lines show the most commonly used shortcuts "
209 "in the editor.\n\n "
210 "The notation for shortcuts is as follows: Control-key "
Chris Allegrettae49f1232000-09-02 07:20:39 +0000211 "sequences are notated with a caret (^) symbol and are entered "
Chris Allegrettad56bd792000-09-02 07:27:10 +0000212 "with the Control (Ctrl) key. Escape-key sequences are notated "
213 "with the Meta (M) symbol and can be entered using either the "
214 "Esc, Alt or Meta key depending on your keyboard setup. The "
215 "following keystrokes are available in the main editor window. "
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000216 "Optional keys are shown in parentheses:\n\n");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000217
218}
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000219#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000220
221/* Make a copy of a node to a pointer (space will be malloc()ed) */
222filestruct *copy_node(filestruct * src)
223{
224 filestruct *dst;
225
226 dst = nmalloc(sizeof(filestruct));
227 dst->data = nmalloc(strlen(src->data) + 1);
228
229 dst->next = src->next;
230 dst->prev = src->prev;
231
232 strcpy(dst->data, src->data);
233 dst->lineno = src->lineno;
234
235 return dst;
236}
237
238/* Unlink a node from the rest of the struct */
239void unlink_node(filestruct * fileptr)
240{
241 if (fileptr->prev != NULL)
242 fileptr->prev->next = fileptr->next;
243
244 if (fileptr->next != NULL)
245 fileptr->next->prev = fileptr->prev;
246}
247
248void delete_node(filestruct * fileptr)
249{
Chris Allegretta17dcb722001-01-20 21:40:07 +0000250 if (fileptr == NULL)
251 return;
252
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000253 if (fileptr->data != NULL)
Chris Allegretta17dcb722001-01-20 21:40:07 +0000254 free(fileptr->data);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000255 free(fileptr);
256}
257
258/* Okay, now let's duplicate a whole struct! */
259filestruct *copy_filestruct(filestruct * src)
260{
261 filestruct *dst, *tmp, *head, *prev;
262
263 head = copy_node(src);
264 dst = head; /* Else we barf on copying just one line */
265 head->prev = NULL;
266 tmp = src->next;
267 prev = head;
268
269 while (tmp != NULL) {
270 dst = copy_node(tmp);
271 dst->prev = prev;
272 prev->next = dst;
273
274 prev = dst;
275 tmp = tmp->next;
276 }
277
278 dst->next = NULL;
279 return head;
280}
281
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000282int free_filestruct(filestruct * src)
283{
284 filestruct *fileptr = src;
285
286 if (src == NULL)
287 return 0;
288
289 while (fileptr->next != NULL) {
290 fileptr = fileptr->next;
Chris Allegretta17dcb722001-01-20 21:40:07 +0000291 delete_node(fileptr->prev);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000292
293#ifdef DEBUG
Chris Allegretta17dcb722001-01-20 21:40:07 +0000294 fprintf(stderr, _("delete_node(): free'd a node, YAY!\n"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000295#endif
296 }
Chris Allegretta17dcb722001-01-20 21:40:07 +0000297 delete_node(fileptr);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000298#ifdef DEBUG
Chris Allegretta17dcb722001-01-20 21:40:07 +0000299 fprintf(stderr, _("delete_node(): free'd last node.\n"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000300#endif
301
302 return 1;
303}
304
305int renumber_all(void)
306{
307 filestruct *temp;
308 long i = 1;
309
310 for (temp = fileage; temp != NULL; temp = temp->next) {
311 temp->lineno = i++;
312 }
313
314 return 0;
315}
316
317int renumber(filestruct * fileptr)
318{
319 filestruct *temp;
320
321 if (fileptr == NULL || fileptr->prev == NULL || fileptr == fileage) {
322 renumber_all();
323 return 0;
324 }
325 for (temp = fileptr; temp != NULL; temp = temp->next) {
Chris Allegretta5146fec2000-12-10 05:44:02 +0000326 if (temp->prev != NULL)
327 temp->lineno = temp->prev->lineno + 1;
328 else
329 temp->lineno = 1;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000330 }
331
332 return 0;
333}
334
335/* Fix the memory allocation for a string */
336void align(char **strp)
337{
338 /* There was a serious bug here: the new address was never
339 stored anywhere... */
340
341 *strp = nrealloc(*strp, strlen(*strp) + 1);
342}
343
Chris Allegretta6925bbd2000-07-28 01:41:29 +0000344/* Null a string at a certain index and align it */
345void null_at(char *data, int index)
346{
347 data[index] = 0;
348 align(&data);
349}
350
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000351void usage(void)
352{
353#ifdef HAVE_GETOPT_LONG
354 printf(_("Usage: nano [GNU long option] [option] +LINE <file>\n\n"));
355 printf(_("Option Long option Meaning\n"));
Chris Allegretta6724a7e2000-06-19 23:19:07 +0000356 printf(_
Chris Allegrettae9a2d032001-01-21 16:08:21 +0000357 (" -T [num] --tabsize=[num] Set width of a tab to num\n"));
Chris Allegretta805c26d2000-09-06 13:39:17 +0000358#ifdef HAVE_REGEX_H
Chris Allegretta9fc8d432000-07-07 01:49:52 +0000359 printf(_
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000360 (" -R --regexp Use regular expressions for search\n"));
Chris Allegretta47805612000-07-07 02:35:34 +0000361#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000362 printf
363 (_
364 (" -V --version Print version information and exit\n"));
365 printf(_
366 (" -c --const Constantly show cursor position\n"));
367 printf(_
368 (" -h --help Show this message\n"));
Chris Allegrettad55655f2000-12-27 03:36:47 +0000369 printf(_
370 (" -i --autoindent Automatically indent new lines\n"));
Chris Allegrettad19e9912000-07-12 18:14:51 +0000371#ifndef NANO_SMALL
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000372 printf(_
Chris Allegretta627de192000-07-12 02:09:17 +0000373 (" -k --cut Let ^K cut from cursor to end of line\n"));
Chris Allegrettad19e9912000-07-12 18:14:51 +0000374#endif
Chris Allegretta627de192000-07-12 02:09:17 +0000375 printf(_
Chris Allegretta71348ee2000-10-02 04:21:23 +0000376 (" -l --nofollow Don't follow symbolic links, overwrite\n"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000377#ifndef NANO_SMALL
378#ifdef NCURSES_MOUSE_VERSION
379 printf(_(" -m --mouse Enable mouse\n"));
380#endif
381#endif
Chris Allegrettad55655f2000-12-27 03:36:47 +0000382 printf(_
383 (" -p --pico Emulate Pico as closely as possible\n"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000384 printf
385 (_
386 (" -r [#cols] --fill=[#cols] Set fill cols to (wrap lines at) #cols\n"));
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000387#ifndef DISABLE_SPELLER
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000388 printf(_
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000389 (" -s [prog] --speller=[prog] Enable alternate speller\n"));
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000390#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000391 printf(_
392 (" -t --tempfile Auto save on exit, don't prompt\n"));
393 printf(_
394 (" -v --view View (read only) mode\n"));
395 printf(_
396 (" -w --nowrap Don't wrap long lines\n"));
397 printf(_
398 (" -x --nohelp Don't show help window\n"));
399 printf(_
400 (" -z --suspend Enable suspend\n"));
401 printf(_
402 (" +LINE Start at line number LINE\n"));
403#else
404 printf(_("Usage: nano [option] +LINE <file>\n\n"));
405 printf(_("Option Meaning\n"));
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000406 printf(_(" -T [num] Set width of a tab to num\n"));
Chris Allegretta9fc8d432000-07-07 01:49:52 +0000407 printf(_(" -R Use regular expressions for search\n"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000408 printf(_(" -V Print version information and exit\n"));
409 printf(_(" -c Constantly show cursor position\n"));
410 printf(_(" -h Show this message\n"));
Chris Allegrettad55655f2000-12-27 03:36:47 +0000411 printf(_(" -i Automatically indent new lines\n"));
Chris Allegrettad19e9912000-07-12 18:14:51 +0000412#ifndef NANO_SMALL
Chris Allegretta627de192000-07-12 02:09:17 +0000413 printf(_(" -k Let ^K cut from cursor to end of line\n"));
Chris Allegrettad19e9912000-07-12 18:14:51 +0000414#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000415 printf(_
Chris Allegretta71348ee2000-10-02 04:21:23 +0000416 (" -l Don't follow symbolic links, overwrite\n"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000417#ifndef NANO_SMALL
418#ifdef NCURSES_MOUSE_VERSION
419 printf(_(" -m Enable mouse\n"));
420#endif
421#endif
Chris Allegrettabf9a8cc2000-11-17 01:37:39 +0000422 printf(_(" -p Emulate Pico as closely as possible\n"));
Chris Allegrettad55655f2000-12-27 03:36:47 +0000423 printf(_(" -r [#cols] Set fill cols to (wrap lines at) #cols\n"));
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000424#ifndef DISABLE_SPELLER
Chris Allegrettad55655f2000-12-27 03:36:47 +0000425 printf(_(" -s [prog] Enable alternate speller\n"));
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000426#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000427 printf(_(" -t Auto save on exit, don't prompt\n"));
428 printf(_(" -v View (read only) mode\n"));
429 printf(_(" -w Don't wrap long lines\n"));
430 printf(_(" -x Don't show help window\n"));
431 printf(_(" -z Enable suspend\n"));
432 printf(_(" +LINE Start at line number LINE\n"));
433#endif
434 exit(0);
435}
436
437void version(void)
438{
439 printf(_(" nano version %s by Chris Allegretta (compiled %s, %s)\n"),
440 VERSION, __TIME__, __DATE__);
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000441 printf(_
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000442 (" Email: nano@nano-editor.org Web: http://www.nano-editor.org"));
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000443 printf(_("\n Compiled options:"));
Chris Allegrettaff269f82000-12-01 18:46:01 +0000444
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000445#ifdef NANO_EXTRA
446 printf(" --enable-extra");
447#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000448
449#ifdef NANO_SMALL
450 printf(" --enable-tiny");
451#else
452 #ifdef DISABLE_BROWSER
Chris Allegretta6636dc32001-01-05 05:41:07 +0000453 printf(" --disable-browser");
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000454 #endif
455 #ifdef DISABLE_TABCOMP
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000456 printf(" --disable-tabcomp");
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000457 #endif
458 #ifdef DISABLE_JUSTIFY
Chris Allegrettaff269f82000-12-01 18:46:01 +0000459 printf(" --disable-justify");
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000460 #endif
461 #ifdef DISABLE_SPELLER
Chris Allegretta7b36c522000-12-06 01:08:10 +0000462 printf(" --disable-speller");
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000463 #endif
464 #ifdef DISABLE_HELP
Chris Allegrettab7d00ef2000-12-18 05:36:51 +0000465 printf(" --disable-help");
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000466 #endif
Chris Allegrettab7d00ef2000-12-18 05:36:51 +0000467#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000468
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000469#ifdef USE_SLANG
470 printf(" --with-slang");
471#endif
472 printf("\n");
473
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000474}
475
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000476filestruct *make_new_node(filestruct * prevnode)
477{
478 filestruct *newnode;
479
480 newnode = nmalloc(sizeof(filestruct));
481 newnode->data = NULL;
482
483 newnode->prev = prevnode;
484 newnode->next = NULL;
485
486 if (prevnode != NULL)
487 newnode->lineno = prevnode->lineno + 1;
488
489 return newnode;
490}
491
Chris Allegretta7975ed82000-07-28 00:58:35 +0000492/* Splice a node into an existing filestruct */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000493void splice_node(filestruct * begin, filestruct * new, filestruct * end)
Chris Allegretta7975ed82000-07-28 00:58:35 +0000494{
495 new->next = end;
496 new->prev = begin;
497 begin->next = new;
498 if (end != NULL)
499 end->prev = new;
500}
501
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000502int do_mark()
503{
504#ifdef NANO_SMALL
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000505 nano_disabled_msg();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000506#else
507 if (!ISSET(MARK_ISSET)) {
508 statusbar(_("Mark Set"));
509 SET(MARK_ISSET);
510 mark_beginbuf = current;
511 mark_beginx = current_x;
512 } else {
513 statusbar(_("Mark UNset"));
514 UNSET(MARK_ISSET);
515 mark_beginbuf = NULL;
516 mark_beginx = 0;
517
518 edit_refresh();
519 }
520#endif
521 return 1;
522}
523
524int no_help(void)
525{
526 if ISSET
527 (NO_HELP)
528 return 2;
529 else
530 return 0;
531}
532
Chris Allegretta3bc8c722000-12-10 17:03:25 +0000533#if defined(DISABLE_JUSTIFY) || defined(DISABLE_SPELLER) || defined(DISABLE_HELP)
Chris Allegrettaff269f82000-12-01 18:46:01 +0000534void nano_disabled_msg(void)
535{
536 statusbar("Sorry, support for this function has been disabled");
537}
Chris Allegretta4eb7aa02000-12-01 18:57:11 +0000538#endif
Chris Allegrettaff269f82000-12-01 18:46:01 +0000539
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000540/* The user typed a printable character; add it to the edit buffer */
541void do_char(char ch)
542{
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000543 /* magic-line: when a character is inserted on the current magic line,
544 * it means we need a new one! */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000545 if (filebot == current && current->data[0] == '\0') {
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000546 new_magicline();
Chris Allegretta28a0f892000-07-05 22:47:54 +0000547 fix_editbot();
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000548 }
549
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000550 /* More dangerousness fun =) */
551 current->data = nrealloc(current->data, strlen(current->data) + 2);
552 memmove(&current->data[current_x + 1],
553 &current->data[current_x],
554 strlen(current->data) - current_x + 1);
555 current->data[current_x] = ch;
556 do_right();
557
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000558 if (!ISSET(NO_WRAP) && (ch != '\t'))
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000559 check_wrap(current, ch);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000560 set_modified();
561 check_statblank();
562 UNSET(KEEP_CUTBUFFER);
563 totsize++;
564
565}
566
567/* Someone hits return *gasp!* */
568int do_enter(filestruct * inptr)
569{
570 filestruct *new;
571 char *tmp, *spc;
572 int extra = 0;
573
574 new = make_new_node(inptr);
575 tmp = &current->data[current_x];
576 current_x = 0;
577
578 /* Do auto-indenting, like the neolithic Turbo Pascal editor */
579 if (ISSET(AUTOINDENT)) {
580 spc = current->data;
581 if (spc) {
582 while ((*spc == ' ') || (*spc == '\t')) {
583 extra++;
584 spc++;
585 current_x++;
586 }
587 new->data = nmalloc(strlen(tmp) + extra + 1);
588 strncpy(new->data, current->data, extra);
589 strcpy(&new->data[extra], tmp);
590 }
591 } else {
592 new->data = nmalloc(strlen(tmp) + 1);
593 strcpy(new->data, tmp);
594 }
595 *tmp = 0;
596
Chris Allegrettada721be2000-07-31 01:26:42 +0000597 if (inptr->next == NULL) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000598 filebot = new;
599 editbot = new;
600 }
Chris Allegretta7975ed82000-07-28 00:58:35 +0000601 splice_node(inptr, new, inptr->next);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000602
603 totsize++;
604 renumber(current);
605 current = new;
606 align(&current->data);
607
Robert Siemborskidd53ec22000-07-04 02:35:19 +0000608 /* The logic here is as follows:
609 * -> If we are at the bottom of the buffer, we want to recenter
610 * (read: rebuild) the screen and forcably move the cursor.
611 * -> otherwise, we want simply to redraw the screen and update
612 * where we think the cursor is.
613 */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000614 if (current_y == editwinrows - 1) {
Chris Allegretta234a34d2000-07-29 04:33:38 +0000615 edit_update(current, CENTER);
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000616 reset_cursor();
Robert Siemborskidd53ec22000-07-04 02:35:19 +0000617 } else {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000618 current_y++;
Robert Siemborskidd53ec22000-07-04 02:35:19 +0000619 edit_refresh();
620 update_cursor();
621 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000622
623 totlines++;
624 set_modified();
625
Chris Allegrettab0ae3932000-06-15 23:39:14 +0000626 placewewant = xplustabs();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000627 return 1;
628}
629
630int do_enter_void(void)
631{
632 return do_enter(current);
633}
634
635void do_next_word(void)
636{
Chris Allegretta9e2934f2000-12-01 23:49:48 +0000637 filestruct *fileptr, *old;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000638 int i;
639
640 if (current == NULL)
641 return;
642
Chris Allegretta9e2934f2000-12-01 23:49:48 +0000643 old = current;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000644 i = current_x;
645 for (fileptr = current; fileptr != NULL; fileptr = fileptr->next) {
646 if (fileptr == current) {
647 while (isalnum((int) fileptr->data[i])
648 && fileptr->data[i] != 0)
649 i++;
650
651 if (fileptr->data[i] == 0) {
652 i = 0;
653 continue;
654 }
655 }
656 while (!isalnum((int) fileptr->data[i]) && fileptr->data[i] != 0)
657 i++;
658
659 if (fileptr->data[i] != 0)
660 break;
661
662 i = 0;
663 }
664 if (fileptr == NULL)
665 current = filebot;
666 else
667 current = fileptr;
668
669 current_x = i;
670 placewewant = xplustabs();
Chris Allegretta9e2934f2000-12-01 23:49:48 +0000671
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000672 if (current->lineno >= editbot->lineno)
Chris Allegretta234a34d2000-07-29 04:33:38 +0000673 edit_update(current, CENTER);
Chris Allegretta9e2934f2000-12-01 23:49:48 +0000674 else {
675 /* If we've jumped lines, refresh the old line. We can't just use
676 * current->prev here, because we may have skipped over some blank
677 * lines, in which case the previous line is the wrong one.
678 */
679 if (current != old)
680 update_line(old, 0);
681
682 update_line(current, current_x);
683 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000684
685}
686
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000687void do_wrap(filestruct * inptr, char input_char)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000688{
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000689 int i = 0; /* Index into ->data for line. */
690 int i_tabs = 0; /* Screen position of ->data[i]. */
691 int last_word_end = -1; /* Location of end of last word found. */
692 int current_word_start = -1; /* Location of start of current word. */
693 int current_word_start_t = -1; /* Location of start of current word screen position. */
694 int current_word_end = -1; /* Location of end of current word */
695 int current_word_end_t = -1; /* Location of end of current word screen position. */
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000696 int len = strlen(inptr->data);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000697
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000698 int down = 0;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000699 int right = 0;
700 struct filestruct *temp = NULL;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000701
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000702 assert(strlenpt(inptr->data) > fill);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000703
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000704 for (i = 0, i_tabs = 0; i < len; i++, i_tabs++) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000705 if (!isspace((int) inptr->data[i])) {
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000706 last_word_end = current_word_end;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000707
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000708 current_word_start = i;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000709 current_word_start_t = i_tabs;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000710
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000711 while (!isspace((int) inptr->data[i])
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000712 && inptr->data[i]) {
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000713 i++;
714 i_tabs++;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000715 if (inptr->data[i] < 32)
716 i_tabs++;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000717 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000718
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000719 if (inptr->data[i]) {
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000720 current_word_end = i;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000721 current_word_end_t = i_tabs;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000722 } else {
723 current_word_end = i - 1;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000724 current_word_end_t = i_tabs - 1;
725 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000726 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000727
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000728 if (inptr->data[i] == NANO_CONTROL_I) {
Chris Allegretta6d690a32000-08-03 22:51:21 +0000729 if (i_tabs % tabsize != 0);
730 i_tabs += tabsize - (i_tabs % tabsize);
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000731 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000732
Adam Rogoyski09f97962000-06-20 02:50:33 +0000733 if (current_word_end_t > fill)
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000734 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000735 }
736
Adam Rogoyski3d449b42000-06-19 17:30:14 +0000737 /* There are a few (ever changing) cases of what the line could look like.
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000738 * 1) only one word on the line before wrap point.
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000739 * a) one word takes up the whole line with no starting spaces.
740 * - do nothing and return.
741 * b) cursor is on word or before word at wrap point and there are spaces at beginning.
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000742 * - word starts new line.
743 * - keep white space on original line up to the cursor.
744 * *) cursor is after word at wrap point
745 * - either it's all white space after word, and this routine isn't called.
746 * - or we are actually in case 2 (2 words).
747 * 2) Two or more words on the line before wrap point.
Adam Rogoyski3d449b42000-06-19 17:30:14 +0000748 * a) cursor is at a word or space before wrap point
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000749 * - word at wrap point starts a new line.
Adam Rogoyski3d449b42000-06-19 17:30:14 +0000750 * - white space at end of original line is cleared, unless
751 * it is all spaces between previous word and next word which appears after fill.
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000752 * b) cursor is at the word at the wrap point.
753 * - word at wrap point starts a new line.
Adam Rogoyski3d449b42000-06-19 17:30:14 +0000754 * 1. pressed a space and at first character of wrap point word.
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000755 * - white space on original line is kept to where cursor was.
Adam Rogoyski3d449b42000-06-19 17:30:14 +0000756 * 2. pressed non space (or space elsewhere).
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000757 * - white space at end of original line is cleared.
758 * c) cursor is past the word at the wrap point.
759 * - word at wrap point starts a new line.
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000760 * - white space at end of original line is cleared
761 */
762
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000763 temp = nmalloc(sizeof(filestruct));
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000764
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000765 /* Category 1a: one word taking up the whole line with no beginning spaces. */
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000766 if ((last_word_end == -1) && (!isspace((int) inptr->data[0]))) {
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000767 for (i = current_word_end; i < len; i++) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000768 if (!isspace((int) inptr->data[i]) && i < len) {
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000769 current_word_start = i;
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000770 while (!isspace((int) inptr->data[i]) && (i < len)) {
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000771 i++;
772 }
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000773 last_word_end = current_word_end;
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000774 current_word_end = i;
775 break;
776 }
777 }
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000778
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000779 if (last_word_end == -1) {
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000780 free(temp);
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000781 return;
782 }
783 if (current_x >= last_word_end) {
784 right = (current_x - current_word_start) + 1;
785 current_x = last_word_end;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000786 down = 1;
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000787 }
788
789 temp->data = nmalloc(strlen(&inptr->data[current_word_start]) + 1);
790 strcpy(temp->data, &inptr->data[current_word_start]);
791 inptr->data = nrealloc(inptr->data, last_word_end + 2);
792 inptr->data[last_word_end + 1] = 0;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000793 } else
794 /* Category 1b: one word on the line and word not taking up whole line
795 (i.e. there are spaces at the beginning of the line) */
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000796 if (last_word_end == -1) {
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000797 temp->data = nmalloc(strlen(&inptr->data[current_word_start]) + 1);
798 strcpy(temp->data, &inptr->data[current_word_start]);
799
800 /* Inside word, remove it from original, and move cursor to right spot. */
801 if (current_x >= current_word_start) {
802 right = current_x - current_word_start;
803 current_x = 0;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000804 down = 1;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000805 }
806
Chris Allegretta6925bbd2000-07-28 01:41:29 +0000807 null_at(inptr->data, current_x);
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000808
809 if (ISSET(MARK_ISSET) && (mark_beginbuf == inptr)) {
810 mark_beginbuf = temp;
811 mark_beginx = 0;
812 }
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000813 }
814
815 /* Category 2: two or more words on the line. */
816 else {
817
818 /* Case 2a: cursor before word at wrap point. */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000819 if (current_x < current_word_start) {
820 temp->data =
821 nmalloc(strlen(&inptr->data[current_word_start]) + 1);
822 strcpy(temp->data, &inptr->data[current_word_start]);
823
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000824 if (!isspace((int) input_char)) {
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000825 i = current_word_start - 1;
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000826 while (isspace((int) inptr->data[i])) {
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000827 i--;
828 assert(i >= 0);
829 }
830 } else if (current_x <= last_word_end)
831 i = last_word_end - 1;
832 else
833 i = current_x;
834
835 inptr->data = nrealloc(inptr->data, i + 2);
836 inptr->data[i + 1] = 0;
837 }
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000838
839
840 /* Case 2b: cursor at word at wrap point. */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000841 else if ((current_x >= current_word_start)
842 && (current_x <= (current_word_end + 1))) {
843 temp->data =
844 nmalloc(strlen(&inptr->data[current_word_start]) + 1);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000845 strcpy(temp->data, &inptr->data[current_word_start]);
846
847 down = 1;
848
849 right = current_x - current_word_start;
850 i = current_word_start - 1;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000851 if (isspace((int) input_char)
852 && (current_x == current_word_start)) {
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000853 current_x = current_word_start;
854
Chris Allegretta6925bbd2000-07-28 01:41:29 +0000855 null_at(inptr->data, current_word_start);
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000856 } else {
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000857
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000858 while (isspace((int) inptr->data[i])) {
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000859 i--;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000860 assert(i >= 0);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000861 }
862 inptr->data = nrealloc(inptr->data, i + 2);
863 inptr->data[i + 1] = 0;
864 }
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000865 }
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000866
867
868 /* Case 2c: cursor past word at wrap point. */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000869 else {
870 temp->data =
871 nmalloc(strlen(&inptr->data[current_word_start]) + 1);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000872 strcpy(temp->data, &inptr->data[current_word_start]);
873
874 down = 1;
875 right = current_x - current_word_start;
876
877 current_x = current_word_start;
878 i = current_word_start - 1;
879
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000880 while (isspace((int) inptr->data[i])) {
Adam Rogoyski3d449b42000-06-19 17:30:14 +0000881 i--;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000882 assert(i >= 0);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000883 inptr->data = nrealloc(inptr->data, i + 2);
884 inptr->data[i + 1] = 0;
885 }
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000886 }
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000887 }
888
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000889 /* We pre-pend wrapped part to next line. */
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000890 if (ISSET(SAMELINEWRAP) && inptr->next) {
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000891 /* Plus one for the space which concatenates the two lines together plus 1 for \0. */
892 char *p =
893 nmalloc(strlen(temp->data) + strlen(inptr->next->data) + 2);
Adam Rogoyski9aeb9da2000-06-16 01:19:31 +0000894 int old_x = current_x, old_y = current_y;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000895
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000896 strcpy(p, temp->data);
897 strcat(p, " ");
898 strcat(p, inptr->next->data);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000899
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000900 free(inptr->next->data);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000901 inptr->next->data = p;
902
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000903 free(temp->data);
904 free(temp);
Adam Rogoyski9aeb9da2000-06-16 01:19:31 +0000905
Adam Rogoyski9aeb9da2000-06-16 01:19:31 +0000906 current_x = old_x;
907 current_y = old_y;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000908 }
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000909 /* Else we start a new line. */
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000910 else {
911 temp->prev = inptr;
912 temp->next = inptr->next;
913
914 if (inptr->next)
915 inptr->next->prev = temp;
916 inptr->next = temp;
917
918 if (!temp->next)
919 filebot = temp;
920
921 SET(SAMELINEWRAP);
922 }
923
924
925 totlines++;
Robert Siemborskia417ddc2000-07-24 23:18:48 +0000926 /* Everything about it makes me want this line here but it causes
927 * totsize to be high by one for some reason. Sigh. (Rob) */
928 /* totsize++; */
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000929
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000930 renumber(inptr);
Chris Allegretta234a34d2000-07-29 04:33:38 +0000931 edit_update(edittop, TOP);
Adam Rogoyski0223d6f2000-06-17 20:36:35 +0000932
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000933
934 /* Move the cursor to the new line if appropriate. */
935 if (down) {
936 do_right();
937 }
938
939 /* Move the cursor to the correct spot in the line if appropriate. */
940 while (right--) {
941 do_right();
942 }
943
Chris Allegretta234a34d2000-07-29 04:33:38 +0000944 edit_update(edittop, TOP);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000945 reset_cursor();
946 edit_refresh();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000947}
948
949/* Check to see if we've just caused the line to wrap to a new line */
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000950void check_wrap(filestruct * inptr, char ch)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000951{
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000952 int len = strlenpt(inptr->data);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000953#ifdef DEBUG
954 fprintf(stderr, _("check_wrap called with inptr->data=\"%s\"\n"),
955 inptr->data);
956#endif
957
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000958 if (len <= fill)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000959 return;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000960 else {
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000961 int i = actual_x(inptr, fill);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000962
963 /* Do not wrap if there are no words on or after wrap point. */
Adam Rogoyski09f97962000-06-20 02:50:33 +0000964 int char_found = 0;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000965
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000966 while (isspace((int) inptr->data[i]) && inptr->data[i])
Adam Rogoyski09f97962000-06-20 02:50:33 +0000967 i++;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000968
Adam Rogoyski09f97962000-06-20 02:50:33 +0000969 if (!inptr->data[i])
970 return;
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000971
Adam Rogoyski09f97962000-06-20 02:50:33 +0000972 /* String must be at least 1 character long. */
973 for (i = strlen(inptr->data) - 1; i >= 0; i--) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +0000974 if (isspace((int) inptr->data[i])) {
Adam Rogoyski09f97962000-06-20 02:50:33 +0000975 if (!char_found)
976 continue;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000977 char_found = 2; /* 2 for yes do wrap. */
Adam Rogoyski09f97962000-06-20 02:50:33 +0000978 break;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000979 } else
980 char_found = 1; /* 1 for yes found a word, but must check further. */
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000981 }
Adam Rogoyski09f97962000-06-20 02:50:33 +0000982
983 if (char_found == 2)
984 do_wrap(inptr, ch);
Adam Rogoyski77f36de2000-06-07 03:56:54 +0000985 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000986}
987
988/* Stuff we do when we abort from programs and want to clean up the
989 * screen. This doesnt do much right now.
990 */
991void do_early_abort(void)
992{
993 blank_statusbar_refresh();
994}
995
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000996int do_backspace(void)
997{
998 filestruct *previous, *tmp;
999
1000 if (current_x != 0) {
1001 /* Let's get dangerous */
1002 memmove(&current->data[current_x - 1], &current->data[current_x],
1003 strlen(current->data) - current_x + 1);
1004#ifdef DEBUG
1005 fprintf(stderr, _("current->data now = \"%s\"\n"), current->data);
1006#endif
1007 align(&current->data);
1008 do_left();
1009 } else {
1010 if (current == fileage)
1011 return 0; /* Can't delete past top of file */
1012
1013 previous = current->prev;
1014 current_x = strlen(previous->data);
1015 previous->data = nrealloc(previous->data,
1016 strlen(previous->data) +
1017 strlen(current->data) + 1);
1018 strcat(previous->data, current->data);
1019
1020 tmp = current;
1021 unlink_node(current);
1022 delete_node(current);
1023 if (current == edittop) {
1024 if (previous->next)
1025 current = previous->next;
1026 else
1027 current = previous;
Chris Allegrettada721be2000-07-31 01:26:42 +00001028 page_up_center();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001029 } else {
1030 if (previous->next)
1031 current = previous->next;
1032 else
1033 current = previous;
1034 update_line(current, current_x);
1035 }
1036
1037 /* Ooops, sanity check */
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001038 if (tmp == filebot) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001039 filebot = current;
1040 editbot = current;
Chris Allegretta28a0f892000-07-05 22:47:54 +00001041
1042 /* Recreate the magic line if we're deleting it AND if the
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001043 line we're on now is NOT blank. if it is blank we
1044 can just use IT for the magic line. This is how Pico
1045 appears to do it, in any case */
Chris Allegretta28a0f892000-07-05 22:47:54 +00001046 if (strcmp(current->data, "")) {
1047 new_magicline();
1048 fix_editbot();
1049 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001050 }
1051
1052 current = previous;
1053 renumber(current);
1054 previous_line();
1055 totlines--;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001056#ifdef DEBUG
1057 fprintf(stderr, _("After, data = \"%s\"\n"), current->data);
1058#endif
1059
1060 }
1061
1062 totsize--;
1063 set_modified();
1064 UNSET(KEEP_CUTBUFFER);
1065 edit_refresh();
1066 return 1;
1067}
1068
1069int do_delete(void)
1070{
1071 filestruct *foo;
1072
1073 if (current_x != strlen(current->data)) {
1074 /* Let's get dangerous */
1075 memmove(&current->data[current_x], &current->data[current_x + 1],
1076 strlen(current->data) - current_x);
1077
1078 align(&current->data);
1079
1080 } else if (current->next != NULL) {
1081 current->data = nrealloc(current->data,
1082 strlen(current->data) +
1083 strlen(current->next->data) + 1);
1084 strcat(current->data, current->next->data);
1085
1086 foo = current->next;
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001087 if (filebot == foo) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001088 filebot = current;
1089 editbot = current;
1090 }
1091
1092 unlink_node(foo);
1093 delete_node(foo);
1094 update_line(current, current_x);
1095
Chris Allegretta28a0f892000-07-05 22:47:54 +00001096 /* Please see the comment in do_basckspace if you don't understand
1097 this test */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001098 if (current == filebot && strcmp(current->data, "")) {
Chris Allegretta28a0f892000-07-05 22:47:54 +00001099 new_magicline();
1100 fix_editbot();
Chris Allegretta55373872000-07-06 22:38:37 +00001101 totsize++;
Chris Allegretta28a0f892000-07-05 22:47:54 +00001102 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001103 renumber(current);
1104 totlines--;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001105 } else
1106 return 0;
1107
1108 totsize--;
1109 set_modified();
1110 UNSET(KEEP_CUTBUFFER);
1111 edit_refresh();
1112 return 1;
1113}
1114
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001115void wrap_reset(void)
1116{
1117 UNSET(SAMELINEWRAP);
1118}
1119
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001120#ifndef DISABLE_SPELLER
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001121
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001122int do_int_spell_fix(char *word)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001123{
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001124 char *prevanswer = NULL, *save_search = NULL, *save_replace = NULL;
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001125 filestruct *begin;
1126 int i = 0, j = 0, beginx, beginx_top;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001127
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001128 /* save where we are */
1129 begin = current;
1130 beginx = current_x + 1;
1131
1132 /* save the current search/replace strings */
1133 search_init_globals();
1134 save_search = mallocstrcpy(save_search, last_search);
1135 save_replace = mallocstrcpy(save_replace, last_replace);
1136
1137 /* set search/replace strings to mis-spelt word */
1138 prevanswer = mallocstrcpy(prevanswer, word);
1139 last_search = mallocstrcpy(last_search, word);
1140 last_replace = mallocstrcpy(last_replace, word);
1141
1142 /* start from the top of file */
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001143 current = fileage;
1144 current_x = beginx_top = -1;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001145
1146 search_last_line = FALSE;
1147
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001148 edit_update(fileage, TOP);
1149
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001150 /* make sure word is still mis-spelt (i.e. when multi-errors) */
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001151 if (findnextstr(TRUE, fileage, beginx_top, prevanswer) != NULL)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001152 {
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001153 do_replace_highlight(TRUE, prevanswer);
1154
1155 /* allow replace word to be corrected */
1156 i = statusq(0, spell_list, SPELL_LIST_LEN, last_replace,
1157 _("Edit a replacement"));
1158
1159 do_replace_highlight(FALSE, prevanswer);
1160
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001161 /* start from the start of this line again */
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001162 current = fileage;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001163 current_x = beginx_top;
1164
1165 search_last_line = FALSE;
1166
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001167 j = i;
1168 do_replace_loop(prevanswer, fileage, &beginx_top, TRUE, &j);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001169 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001170
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001171 /* restore the search/replace strings */
1172 last_search = mallocstrcpy(last_search, save_search);
1173 last_replace = mallocstrcpy(last_replace, save_replace);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001174
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001175 /* restore where we were */
1176 current = begin;
1177 current_x = beginx - 1;
1178
1179 edit_update(current, CENTER);
1180
1181 if (i == -1)
1182 return FALSE;
1183
1184 return TRUE;
1185}
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001186
1187/* Integrated spell checking using 'spell' program */
Chris Allegretta271e9722000-11-10 18:15:43 +00001188int do_int_speller(char *tempfile_name)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001189{
Chris Allegretta271e9722000-11-10 18:15:43 +00001190 char *read_buff, *read_buff_ptr, *read_buff_word;
1191 long pipe_buff_size;
1192 int in_fd[2], tempfile_fd;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001193 int spell_status;
1194 pid_t pid_spell;
1195 ssize_t bytesread;
1196
Chris Allegretta271e9722000-11-10 18:15:43 +00001197 /* Create a pipe to spell program */
1198
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001199 if (pipe(in_fd) == -1)
1200 return FALSE;
1201
Chris Allegretta271e9722000-11-10 18:15:43 +00001202 /* A new process to run spell in */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001203
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001204 if ( (pid_spell = fork()) == 0) {
1205
1206 /* Child continues, (i.e. future spell process) */
1207
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001208 close(in_fd[0]);
1209
Chris Allegretta271e9722000-11-10 18:15:43 +00001210 /* replace the standard in with the tempfile */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001211
Chris Allegretta271e9722000-11-10 18:15:43 +00001212 if ( (tempfile_fd = open(tempfile_name, O_RDONLY)) == -1) {
1213
1214 close(in_fd[1]);
1215 exit(1);
1216 }
1217
1218 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) {
1219
1220 close(tempfile_fd);
1221 close(in_fd[1]);
1222 exit(1);
1223 }
1224 close(tempfile_fd);
1225
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001226
Chris Allegretta271e9722000-11-10 18:15:43 +00001227 /* send spell's standard out to the pipe */
1228
1229 if (dup2(in_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
1230
1231 close(in_fd[1]);
1232 exit(1);
1233 }
1234 close(in_fd[1]);
1235
1236 /* Start spell program, we are using the PATH here!?!? */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001237 execlp("spell", "spell", NULL);
1238
Chris Allegretta271e9722000-11-10 18:15:43 +00001239 /* Should not be reached, if spell is found!!! */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001240
Chris Allegretta271e9722000-11-10 18:15:43 +00001241 exit(1);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001242 }
1243
1244 /* Parent continues here */
1245
Chris Allegretta271e9722000-11-10 18:15:43 +00001246 close(in_fd[1]);
1247
1248 /* Child process was not forked successfully */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001249
1250 if (pid_spell < 0) {
1251
Chris Allegretta271e9722000-11-10 18:15:43 +00001252 close(in_fd[0]);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001253 return FALSE;
1254 }
1255
Chris Allegretta271e9722000-11-10 18:15:43 +00001256 /* Get system pipe buffer size */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001257
Chris Allegretta271e9722000-11-10 18:15:43 +00001258 if ( (pipe_buff_size = fpathconf(in_fd[0], _PC_PIPE_BUF)) < 1) {
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001259
Chris Allegretta271e9722000-11-10 18:15:43 +00001260 close(in_fd[0]);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001261 return FALSE;
Chris Allegretta271e9722000-11-10 18:15:43 +00001262 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001263
Chris Allegretta271e9722000-11-10 18:15:43 +00001264 read_buff = nmalloc( pipe_buff_size + 1 );
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001265
Chris Allegretta271e9722000-11-10 18:15:43 +00001266 /* Process the returned spelling errors */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001267
Chris Allegretta271e9722000-11-10 18:15:43 +00001268 while ( (bytesread = read(in_fd[0], read_buff, pipe_buff_size)) > 0) {
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001269
Chris Allegretta271e9722000-11-10 18:15:43 +00001270 read_buff[bytesread] = (char) NULL;
1271 read_buff_word = read_buff_ptr = read_buff;
1272
1273 while (*read_buff_ptr != (char) NULL) {
1274
1275 /* Windows version may need to process additional char '\r' */
1276
1277 /* Possible problem here if last word not followed by '\n' */
1278
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001279 if (*read_buff_ptr == '\n') {
Chris Allegretta271e9722000-11-10 18:15:43 +00001280 *read_buff_ptr = (char) NULL;
1281 if (!do_int_spell_fix(read_buff_word)) {
1282
1283 close(in_fd[0]);
1284 free(read_buff);
1285 replace_abort();
1286
1287 return TRUE;
1288 }
1289 read_buff_word = read_buff_ptr;
1290 read_buff_word++;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001291 }
1292
1293 read_buff_ptr++;
1294 }
1295 }
Chris Allegretta271e9722000-11-10 18:15:43 +00001296
1297 close(in_fd[0]);
1298 free(read_buff);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001299 replace_abort();
1300
Chris Allegretta271e9722000-11-10 18:15:43 +00001301 /* Process end of spell process */
1302
1303 wait(&spell_status);
1304 if (WIFEXITED(spell_status)) {
1305 if (WEXITSTATUS(spell_status) != 0)
1306 return FALSE;
1307 }
1308 else
1309 return FALSE;
1310
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001311 return TRUE;
1312}
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001313
1314/* External spell checking */
Chris Allegretta271e9722000-11-10 18:15:43 +00001315int do_alt_speller(char *file_name)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001316{
Chris Allegretta271e9722000-11-10 18:15:43 +00001317 int alt_spell_status;
1318 pid_t pid_spell;
Chris Allegretta169ee842001-01-26 01:57:32 +00001319 char *ptr;
1320 static int arglen = 3;
1321 static char **spellargs = (char **) NULL;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001322
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001323 endwin();
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001324
Chris Allegretta271e9722000-11-10 18:15:43 +00001325 /* Start a new process for the alternate speller */
1326
1327 if ( (pid_spell = fork()) == 0) {
1328
Chris Allegretta169ee842001-01-26 01:57:32 +00001329 /* Set up an argument list to pass the execvp function */
1330 if (spellargs == NULL) {
1331 spellargs = nmalloc(arglen * sizeof(char *));
1332
1333 spellargs[0] = strtok(alt_speller, " ");
1334 while ((ptr = strtok(NULL, " ")) != NULL) {
1335 arglen++;
1336 spellargs = nrealloc(spellargs, arglen * sizeof(char *));
1337 spellargs[arglen - 3] = ptr;
1338 }
1339 spellargs[arglen - 1] = NULL;
1340 }
1341 spellargs[arglen - 2] = file_name;
1342
Chris Allegretta271e9722000-11-10 18:15:43 +00001343 /* Start alternate spell program, we are using the PATH here!?!? */
Chris Allegretta169ee842001-01-26 01:57:32 +00001344 execvp(spellargs[0], spellargs);
Chris Allegretta271e9722000-11-10 18:15:43 +00001345
1346 /* Should not be reached, if alternate speller is found!!! */
1347
1348 exit(1);
1349 }
1350
1351 /* Could not fork?? */
1352
1353 if (pid_spell < 0)
1354 return FALSE;
1355
1356 /* Wait for alternate speller to complete */
1357
1358 wait(&alt_spell_status);
1359 if (WIFEXITED(alt_spell_status)) {
1360 if (WEXITSTATUS(alt_spell_status) != 0)
1361 return FALSE;
1362 }
1363 else
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001364 return FALSE;
1365
Chris Allegretta8f6c0692000-07-19 01:16:18 +00001366 refresh();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001367 free_filestruct(fileage);
1368 global_init();
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001369 open_file(file_name, 0, 1);
Chris Allegretta234a34d2000-07-29 04:33:38 +00001370 edit_update(fileage, CENTER);
Chris Allegretta271e9722000-11-10 18:15:43 +00001371 display_main_list();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001372 set_modified();
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001373
1374 return TRUE;
1375}
1376#endif
1377
1378int do_spell(void)
1379{
1380
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001381#ifdef DISABLE_SPELLER
Chris Allegrettaff269f82000-12-01 18:46:01 +00001382 nano_disabled_msg();
1383 return (TRUE);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001384#else
Chris Allegretta271e9722000-11-10 18:15:43 +00001385 char *temp;
1386 int spell_res;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001387
Chris Allegretta271e9722000-11-10 18:15:43 +00001388 if ((temp = tempnam(0, "nano.")) == NULL) {
1389 statusbar(_("Could not create a temporary filename: %s"),
1390 strerror(errno));
1391 return 0;
1392 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001393
Chris Allegretta3dbb2782000-12-02 04:36:50 +00001394 if (write_file(temp, 1) == -1) {
1395 statusbar(_("Spell checking failed: unable to write temp file!"));
Chris Allegretta271e9722000-11-10 18:15:43 +00001396 return 0;
Chris Allegretta3dbb2782000-12-02 04:36:50 +00001397 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001398
Chris Allegretta271e9722000-11-10 18:15:43 +00001399 if (alt_speller)
1400 spell_res = do_alt_speller(temp);
1401 else
1402 spell_res = do_int_speller(temp);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001403
Chris Allegretta271e9722000-11-10 18:15:43 +00001404 remove(temp);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001405
1406 if (spell_res)
1407 statusbar(_("Finished checking spelling"));
1408 else
1409 statusbar(_("Spell checking failed"));
1410
1411 return spell_res;
1412
Chris Allegrettadbc12b22000-07-03 03:10:14 +00001413#endif
Chris Allegretta67105eb2000-07-03 03:18:32 +00001414}
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001415
1416int do_exit(void)
1417{
1418 int i;
1419
1420 if (!ISSET(MODIFIED))
1421 finish(0);
1422
Chris Allegretta30885552000-07-14 01:20:12 +00001423 if (ISSET(TEMP_OPT)) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001424 i = 1;
1425 } else {
1426 i =
1427 do_yesno(0, 0,
1428 _
1429 ("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES) ? "));
1430 }
1431
1432#ifdef DEBUG
1433 dump_buffer(fileage);
1434#endif
1435
1436 if (i == 1) {
Chris Allegrettae1ebaf32001-01-07 05:50:36 +00001437 if (do_writeout(filename, 1) > 0)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001438 finish(0);
1439 } else if (i == 0)
1440 finish(0);
1441 else
1442 statusbar(_("Cancelled"));
1443
1444 display_main_list();
1445 return 1;
1446}
1447
1448#ifndef NANO_SMALL
1449#ifdef NCURSES_MOUSE_VERSION
1450void do_mouse(void)
1451{
1452 MEVENT mevent;
Chris Allegrettae10debd2000-08-22 01:26:42 +00001453 int foo = 0, tab_found = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001454
1455 if (getmouse(&mevent) == ERR)
1456 return;
1457
1458 /* If mouse not in edit window, return (add help selection later). */
1459 if (!wenclose(edit, mevent.y, mevent.x))
1460 return;
1461
1462 /* Subtract out size of topwin. Perhaps we need a constant somewhere? */
1463 mevent.y -= 2;
1464
1465 /* Selecting where the cursor is sets the mark.
1466 * Selecting beyond the line length with the cursor at the end of the
1467 * line sets the mark as well.
1468 */
1469 if ((mevent.y == current_y) &&
1470 ((mevent.x == current_x) || (current_x == strlen(current->data)
1471 && (mevent.x >
1472 strlen(current->data))))) {
1473 if (ISSET(VIEW_MODE)) {
1474 print_view_warning();
1475 return;
1476 }
1477 do_mark();
1478 } else if (mevent.y > current_y) {
1479 while (mevent.y > current_y) {
1480 if (current->next != NULL)
1481 current = current->next;
1482 else
1483 break;
1484 current_y++;
1485 }
1486 } else if (mevent.y < current_y) {
1487 while (mevent.y < current_y) {
1488 if (current->prev != NULL)
1489 current = current->prev;
1490 else
1491 break;
1492 current_y--;
1493 }
1494 }
1495 current_x = mevent.x;
Chris Allegrettae10debd2000-08-22 01:26:42 +00001496 placewewant = current_x;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001497 while (foo < current_x) {
1498 if (current->data[foo] == NANO_CONTROL_I) {
Chris Allegrettae10debd2000-08-22 01:26:42 +00001499 current_x -= tabsize - (foo % tabsize);
1500 tab_found = 1;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001501 } else if (current->data[foo] & 0x80);
1502 else if (current->data[foo] < 32)
Chris Allegrettae10debd2000-08-22 01:26:42 +00001503 current_x--;
1504 foo++;
1505 }
1506 /* This is where tab_found comes in. I can't figure out why,
1507 * but without it any line with a tab will place the cursor
1508 * one character behind. Whatever, this fixes it. */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001509 if (tab_found == 1)
Chris Allegrettae10debd2000-08-22 01:26:42 +00001510 current_x++;
1511
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001512 if (current_x > strlen(current->data))
1513 current_x = strlen(current->data);
1514
1515 update_cursor();
1516 edit_refresh();
1517
1518}
1519#endif
1520#endif
1521
1522/* Handler for SIGHUP */
1523RETSIGTYPE handle_hup(int signal)
1524{
Chris Allegrettae7a58932000-12-02 02:36:22 +00001525 die(_("Received SIGHUP"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001526}
1527
Chris Allegretta18f8be02000-09-04 03:20:38 +00001528/* What do we do when we catch the suspend signal */
1529RETSIGTYPE do_suspend(int signal)
1530{
1531
1532 act.sa_handler = SIG_DFL;
1533 sigemptyset(&act.sa_mask);
1534 sigaction(SIGTSTP, &act, NULL);
1535
1536 endwin();
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001537 fprintf(stderr, "\n\n\n\n\nUse \"fg\" to return to nano\n");
Chris Allegretta18f8be02000-09-04 03:20:38 +00001538 raise(SIGTSTP);
1539}
1540
1541/* Restore the suspend handler when we come back into the prog */
1542RETSIGTYPE do_cont(int signal)
1543{
1544
1545 act.sa_handler = do_suspend;
1546 sigemptyset(&act.sa_mask);
1547 sigaction(SIGTSTP, &act, NULL);
1548 initscr();
1549 total_refresh();
1550}
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001551
1552void handle_sigwinch(int s)
1553{
1554#ifndef NANO_SMALL
1555 char *tty = NULL;
1556 int fd = 0;
1557 int result = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001558 struct winsize win;
1559
1560 tty = ttyname(0);
1561 if (!tty)
1562 return;
1563 fd = open(tty, O_RDWR);
1564 if (fd == -1)
1565 return;
1566 result = ioctl(fd, TIOCGWINSZ, &win);
1567 if (result == -1)
1568 return;
1569
1570
1571 COLS = win.ws_col;
1572 LINES = win.ws_row;
1573
Chris Allegrettae61e8302001-01-14 05:18:27 +00001574 if ((editwinrows = LINES - 5 + no_help()) < MIN_EDITOR_ROWS)
1575 die_too_small();
1576
1577 if ((fill = COLS - CHARS_FROM_EOL) < MIN_FILL_LENGTH)
1578 die_too_small();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001579
Chris Allegretta0a06e072001-01-23 02:35:04 +00001580 hblank = nrealloc(hblank, COLS + 1);
1581 memset(hblank, ' ', COLS);
1582 hblank[COLS] = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001583
1584#ifdef HAVE_NCURSES_H
1585 resizeterm(LINES, COLS);
1586#ifdef HAVE_WRESIZE
1587 if (wresize(topwin, 2, COLS) == ERR)
1588 die(_("Cannot resize top win"));
1589 if (mvwin(topwin, 0, 0) == ERR)
1590 die(_("Cannot move top win"));
1591 if (wresize(edit, editwinrows, COLS) == ERR)
1592 die(_("Cannot resize edit win"));
1593 if (mvwin(edit, 2, 0) == ERR)
1594 die(_("Cannot move edit win"));
1595 if (wresize(bottomwin, 3 - no_help(), COLS) == ERR)
1596 die(_("Cannot resize bottom win"));
1597 if (mvwin(bottomwin, LINES - 3 + no_help(), 0) == ERR)
1598 die(_("Cannot move bottom win"));
1599#endif /* HAVE_WRESIZE */
1600#endif /* HAVE_NCURSES_H */
1601
Robert Siemborskidd53ec22000-07-04 02:35:19 +00001602 fix_editbot();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001603
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001604 if (current_y > editwinrows - 1) {
Chris Allegretta234a34d2000-07-29 04:33:38 +00001605 edit_update(editbot, CENTER);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001606 }
1607 erase();
Chris Allegretta97accc62000-06-19 05:45:52 +00001608
1609 /* Do these b/c width may have changed... */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001610 refresh();
Chris Allegrettaf4b96012001-01-03 07:11:47 +00001611 titlebar(NULL);
Chris Allegretta97accc62000-06-19 05:45:52 +00001612 edit_refresh();
1613 display_main_list();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001614 total_refresh();
1615#endif
1616}
1617
Chris Allegretta756f2202000-09-01 13:32:47 +00001618void signal_init(void)
1619{
Chris Allegretta756f2202000-09-01 13:32:47 +00001620
1621 /* Trap SIGINT and SIGQUIT cuz we want them to do useful things. */
1622 memset(&act, 0, sizeof(struct sigaction));
1623 act.sa_handler = SIG_IGN;
1624 sigaction(SIGINT, &act, NULL);
Chris Allegretta756f2202000-09-01 13:32:47 +00001625
Chris Allegretta18f8be02000-09-04 03:20:38 +00001626 if (!ISSET(SUSPEND)) {
Chris Allegretta756f2202000-09-01 13:32:47 +00001627 sigaction(SIGTSTP, &act, NULL);
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001628 } else {
Chris Allegretta18f8be02000-09-04 03:20:38 +00001629 act.sa_handler = do_suspend;
1630 sigaction(SIGTSTP, &act, NULL);
1631
1632 act.sa_handler = do_cont;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001633 sigaction(SIGCONT, &act, NULL);
Chris Allegretta18f8be02000-09-04 03:20:38 +00001634 }
1635
Chris Allegretta756f2202000-09-01 13:32:47 +00001636
1637 /* Trap SIGHUP cuz we want to write the file out. */
1638 act.sa_handler = handle_hup;
1639 sigaction(SIGHUP, &act, NULL);
1640
1641 act.sa_handler = handle_sigwinch;
1642 sigaction(SIGWINCH, &act, NULL);
1643
1644}
1645
Chris Allegretta2a42af12000-09-12 23:02:49 +00001646void window_init(void)
1647{
Chris Allegrettae61e8302001-01-14 05:18:27 +00001648 if ((editwinrows = LINES - 5 + no_help()) < MIN_EDITOR_ROWS)
1649 die_too_small();
Chris Allegretta92c9dd22000-09-13 14:03:27 +00001650
Chris Allegretta2a42af12000-09-12 23:02:49 +00001651 /* Setup up the main text window */
1652 edit = newwin(editwinrows, COLS, 2, 0);
1653
1654 /* And the other windows */
1655 topwin = newwin(2, COLS, 0, 0);
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001656 bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0);
Chris Allegretta63c8ab92001-01-04 02:33:52 +00001657
Chris Allegretta155d6202001-01-08 01:50:37 +00001658#ifdef PDCURSES
1659 /* Oops, I guess we need this again.
1660 Moved here so the keypad still works after a Meta-X, for example */
1661 keypad(edit, TRUE);
1662 keypad(bottomwin, TRUE);
1663#endif
1664
Chris Allegretta2a42af12000-09-12 23:02:49 +00001665}
1666
Chris Allegretta756f2202000-09-01 13:32:47 +00001667void mouse_init(void)
1668{
1669#ifndef NANO_SMALL
1670#ifdef NCURSES_MOUSE_VERSION
1671 if (ISSET(USE_MOUSE)) {
Chris Allegrettac08f50d2001-01-06 18:12:43 +00001672 keypad_on(edit, 1);
1673
Chris Allegretta756f2202000-09-01 13:32:47 +00001674 mousemask(BUTTON1_RELEASED, NULL);
1675 mouseinterval(50);
Chris Allegretta0b88ce02000-09-15 15:46:32 +00001676
Chris Allegretta63c8ab92001-01-04 02:33:52 +00001677 } else
Chris Allegretta756f2202000-09-01 13:32:47 +00001678 mousemask(0, NULL);
Chris Allegretta63c8ab92001-01-04 02:33:52 +00001679
Chris Allegretta756f2202000-09-01 13:32:47 +00001680#endif
1681#endif
1682
1683}
1684
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001685int do_tab(void)
1686{
1687 do_char('\t');
1688 return 1;
1689}
1690
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001691#ifndef DISABLE_JUSTIFY
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001692int empty_line(const char *data)
1693{
1694 while (*data) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +00001695 if (!isspace((int) *data))
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001696 return 0;
1697
1698 data++;
1699 }
1700
1701 return 1;
1702}
1703
1704int no_spaces(const char *data)
1705{
1706 while (*data) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +00001707 if (isspace((int) *data))
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001708 return 0;
1709
1710 data++;
1711 }
1712
1713 return 1;
1714}
1715
1716void justify_format(char *data)
1717{
1718 int i = 0;
1719 int len = strlen(data);
1720
1721 /* Skip first character regardless and leading whitespace. */
1722 for (i = 1; i < len; i++) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +00001723 if (!isspace((int) data[i]))
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001724 break;
1725 }
1726
1727 i++; /* (i) is now at least 2. */
1728
1729 /* No double spaces allowed unless following a period. Tabs -> space. No double tabs. */
1730 for (; i < len; i++) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +00001731 if (isspace((int) data[i]) && isspace((int) data[i - 1])
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001732 && (data[i - 2] != '.')) {
1733 memmove(data + i, data + i + 1, len - i);
1734 len--;
1735 i--;
1736 }
1737 }
1738}
1739#endif
1740
1741int do_justify(void)
1742{
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001743#ifdef DISABLE_JUSTIFY
Chris Allegrettaff269f82000-12-01 18:46:01 +00001744 nano_disabled_msg();
1745 return 1;
1746#else
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001747 int slen = 0; /* length of combined lines on one line. */
Chris Allegretta17dcb722001-01-20 21:40:07 +00001748 int initial_y, kbinput = 0, totbak;
Chris Allegretta9149e612000-11-27 00:23:41 +00001749 filestruct *initial = NULL, *tmpjust = NULL, *cutbak, *tmptop, *tmpbot;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001750
1751 if (empty_line(current->data)) {
1752 /* Justify starting at first non-empty line. */
1753 do {
1754 if (!current->next)
1755 return 1;
1756
1757 current = current->next;
1758 current_y++;
1759 }
1760 while (empty_line(current->data));
1761 } else {
1762 /* Search back for the beginning of the paragraph, where
1763 * Paragraph is 1) A line with leading whitespace
1764 * or 2) A line following an empty line.
1765 */
1766 while (current->prev != NULL) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +00001767 if (isspace((int) current->data[0]) || !current->data[0])
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001768 break;
1769
1770 current = current->prev;
1771 current_y--;
1772 }
1773
1774 /* First line with leading whitespace may be empty. */
1775 if (empty_line(current->data)) {
1776 if (current->next) {
1777 current = current->next;
1778 current_y++;
1779 } else
1780 return 1;
1781 }
1782 }
1783 initial = current;
1784 initial_y = current_y;
1785
1786 set_modified();
Chris Allegretta9149e612000-11-27 00:23:41 +00001787 cutbak = cutbuffer; /* Got to like cutbak ;) */
Chris Allegretta17dcb722001-01-20 21:40:07 +00001788 totbak = totsize;
Chris Allegretta9149e612000-11-27 00:23:41 +00001789 cutbuffer = NULL;
1790
1791 tmptop = current;
1792 tmpjust = copy_node(current);
Robert Siemborski60cd6aa2001-01-21 23:23:48 +00001793
1794 /* This is annoying because it mucks with totsize */
Chris Allegretta9149e612000-11-27 00:23:41 +00001795 add_to_cutbuffer(tmpjust);
Robert Siemborski60cd6aa2001-01-21 23:23:48 +00001796
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001797 /* Put the whole paragraph into one big line. */
Chris Allegretta9e7efa32000-10-02 03:42:55 +00001798 while (current->next && !isspace((int) current->next->data[0])
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001799 && current->next->data[0]) {
1800 filestruct *tmpnode = current->next;
1801 int len = strlen(current->data);
1802 int len2 = strlen(current->next->data);
1803
Chris Allegretta9149e612000-11-27 00:23:41 +00001804 tmpjust = NULL;
Chris Allegretta9149e612000-11-27 00:23:41 +00001805 tmpjust = copy_node(current->next);
1806 add_to_cutbuffer(tmpjust);
1807
Robert Siemborski60cd6aa2001-01-21 23:23:48 +00001808 /* Wiping out a newline */
1809 totsize--;
1810
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001811 /* length of both strings plus space between strings and ending \0. */
1812 current->data = nrealloc(current->data, len + len2 + 2);
1813 current->data[len++] = ' ';
1814 current->data[len] = '\0';
1815
1816 strncat(current->data, current->next->data, len2);
1817
1818 unlink_node(tmpnode);
1819 delete_node(tmpnode);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001820 }
1821
1822 justify_format(current->data);
1823
1824 slen = strlen(current->data);
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001825 totsize += slen;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001826
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001827 if ((strlenpt(current->data) > (fill))
1828 && !no_spaces(current->data)) {
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001829 do {
1830 int i = 0;
1831 int len2 = 0;
1832 filestruct *tmpline = nmalloc(sizeof(filestruct));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001833
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001834 /* Start at fill , unless line isn't that long (but it
1835 * appears at least fill long with tabs.
1836 */
1837 if (slen > fill)
1838 i = fill;
1839 else
1840 i = slen;
Robert Siemborski60cd6aa2001-01-21 23:23:48 +00001841
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001842 for (; i > 0; i--) {
Chris Allegretta9e7efa32000-10-02 03:42:55 +00001843 if (isspace((int) current->data[i]) &&
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001844 ((strlenpt(current->data) - strlen(current->data + i))
1845 <= fill))
1846 break;
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001847 }
Robert Siemborski60cd6aa2001-01-21 23:23:48 +00001848
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001849 if (!i)
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001850 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001851
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001852 current->data[i] = '\0';
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001853
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001854 len2 = strlen(current->data + i + 1);
1855 tmpline->data = nmalloc(len2 + 1);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001856
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001857 /* Skip the white space in current. */
1858 memcpy(tmpline->data, current->data + i + 1, len2);
1859 tmpline->data[len2] = '\0';
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001860
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001861 current->data = nrealloc(current->data, i + 1);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001862
Robert Siemborskia417ddc2000-07-24 23:18:48 +00001863 tmpline->prev = current;
1864 tmpline->next = current->next;
1865 if (current->next != NULL)
1866 current->next->prev = tmpline;
1867
1868 current->next = tmpline;
1869 current = tmpline;
1870 slen -= i + 1;
1871 current_y++;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001872 } while ((strlenpt(current->data) > (fill))
1873 && !no_spaces(current->data));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001874 }
Chris Allegretta9149e612000-11-27 00:23:41 +00001875 tmpbot = current;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001876
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001877 if (current->next)
1878 current = current->next;
Adam Rogoyski09f97962000-06-20 02:50:33 +00001879 else
1880 filebot = current;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001881 current_x = 0;
1882 placewewant = 0;
1883
Adam Rogoyski09f97962000-06-20 02:50:33 +00001884 renumber(initial);
1885 totlines = filebot->lineno;
1886
1887 werase(edit);
1888
Chris Allegretta4da1fc62000-06-21 03:00:43 +00001889 if ((current_y < 0) || (current_y >= editwinrows - 1)
1890 || (initial_y <= 0)) {
Chris Allegretta234a34d2000-07-29 04:33:38 +00001891 edit_update(current, CENTER);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001892 center_cursor();
1893 } else {
Robert Siemborskidd53ec22000-07-04 02:35:19 +00001894 fix_editbot();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001895 }
1896
Adam Rogoyski09f97962000-06-20 02:50:33 +00001897 edit_refresh();
Chris Allegretta9149e612000-11-27 00:23:41 +00001898 statusbar(_("Can now UnJustify!"));
Chris Allegretta07798352000-11-27 22:58:23 +00001899 /* Change the shortcut list to display the unjustify code */
1900 shortcut_init(1);
1901 display_main_list();
Chris Allegretta9149e612000-11-27 00:23:41 +00001902 reset_cursor();
1903
Chris Allegretta07798352000-11-27 22:58:23 +00001904 /* Now get a keystroke and see if it's unjustify, if not unget the keytroke
Chris Allegretta9149e612000-11-27 00:23:41 +00001905 and return */
Chris Allegretta4a9c8582000-11-27 22:59:40 +00001906 if ((kbinput = wgetch(edit)) != NANO_UNJUSTIFY_KEY)
Chris Allegretta07798352000-11-27 22:58:23 +00001907 ungetch(kbinput);
Chris Allegretta9149e612000-11-27 00:23:41 +00001908 else {
1909 /* Else restore the justify we just did (ungrateful user!) */
1910 if (tmptop->prev != NULL)
1911 tmptop->prev->next = tmpbot->next;
Chris Allegrettad022eac2000-11-27 02:50:49 +00001912 else
1913 fileage = current;
Chris Allegretta9149e612000-11-27 00:23:41 +00001914 tmpbot->next->prev = tmptop->prev;
1915 current = tmpbot->next;
1916 tmpbot->next = NULL;
1917 do_uncut_text();
Chris Allegrettad022eac2000-11-27 02:50:49 +00001918 if (tmptop->prev == NULL)
1919 edit_refresh();
1920
Chris Allegretta17dcb722001-01-20 21:40:07 +00001921 /* Restore totsize from befure justify */
1922 totsize = totbak;
Chris Allegretta9149e612000-11-27 00:23:41 +00001923 free_filestruct(tmptop);
1924 blank_statusbar_refresh();
1925 }
Chris Allegretta4a9c8582000-11-27 22:59:40 +00001926 shortcut_init(0);
1927 display_main_list();
Chris Allegretta9149e612000-11-27 00:23:41 +00001928 free_filestruct(cutbuffer);
1929 cutbuffer = cutbak;
1930
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001931 return 1;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001932#endif
1933}
1934
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001935#ifndef DISABLE_HELP
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001936void help_init(void)
1937{
1938 int i, sofar = 0;
1939 long allocsize = 1; /* How much space we're gonna need for the help text */
Chris Allegretta220ba692000-12-18 03:40:00 +00001940 char buf[BUFSIZ] = "";
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001941
1942 /* Compute the space needed for the shortcut lists - we add 15 to
1943 have room for the shortcut abbrev and its possible alternate keys */
Chris Allegretta220ba692000-12-18 03:40:00 +00001944 for (i = 0; i <= MAIN_LIST_LEN - 1; i++)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001945 if (main_list[i].help != NULL)
1946 allocsize += strlen(main_list[i].help) + 15;
1947
Chris Allegretta756f2202000-09-01 13:32:47 +00001948 /* And for the toggle list, we also allocate space for extra text. */
Chris Allegretta423cbfd2000-09-04 16:21:29 +00001949 for (i = 0; i <= TOGGLE_LEN - 1; i++)
Chris Allegretta756f2202000-09-01 13:32:47 +00001950 if (toggles[i].desc != NULL)
1951 allocsize += strlen(toggles[i].desc) + 30;
1952
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001953 allocsize += strlen(help_text_init);
1954
1955 if (help_text != NULL)
1956 free(help_text);
1957
1958 /* Allocate space for the help text */
1959 help_text = nmalloc(allocsize);
1960
1961 /* Now add the text we want */
1962 strcpy(help_text, help_text_init);
1963
1964 /* Now add our shortcut info */
Chris Allegretta220ba692000-12-18 03:40:00 +00001965 for (i = 0; i <= MAIN_LIST_LEN - 1; i++) {
Robert Siemborski6af14312000-07-01 21:34:26 +00001966 sofar = snprintf(buf, BUFSIZ, "^%c ", main_list[i].val + 64);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001967
1968 if (main_list[i].misc1 > KEY_F0 && main_list[i].misc1 <= KEY_F(64))
Robert Siemborski6af14312000-07-01 21:34:26 +00001969 sofar += snprintf(&buf[sofar], BUFSIZ - sofar, "(F%d) ",
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001970 main_list[i].misc1 - KEY_F0);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001971 else
Robert Siemborski6af14312000-07-01 21:34:26 +00001972 sofar += snprintf(&buf[sofar], BUFSIZ - sofar, " ");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001973
1974 if (main_list[i].altval > 0)
Chris Allegrettae49f1232000-09-02 07:20:39 +00001975 sofar += snprintf(&buf[sofar], BUFSIZ - sofar, "(M-%c) ",
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001976 main_list[i].altval - 32);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001977 else
Robert Siemborski6af14312000-07-01 21:34:26 +00001978 sofar += snprintf(&buf[sofar], BUFSIZ - sofar, " ");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001979
Chris Allegretta756f2202000-09-01 13:32:47 +00001980
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001981 if (main_list[i].help != NULL)
Robert Siemborski976847c2000-07-06 03:43:05 +00001982 snprintf(&buf[sofar], BUFSIZ - sofar, "%s", main_list[i].help);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001983
Chris Allegretta756f2202000-09-01 13:32:47 +00001984
1985 strcat(help_text, buf);
1986 strcat(help_text, "\n");
1987 }
1988
1989 /* And the toggles... */
Chris Allegretta423cbfd2000-09-04 16:21:29 +00001990 for (i = 0; i <= TOGGLE_LEN - 1; i++) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001991 sofar = snprintf(buf, BUFSIZ,
1992 "M-%c ", toggles[i].val - 32);
Chris Allegretta756f2202000-09-01 13:32:47 +00001993
1994 if (toggles[i].desc != NULL)
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00001995 snprintf(&buf[sofar], BUFSIZ - sofar, _("%s enable/disable"),
1996 toggles[i].desc);
Chris Allegretta756f2202000-09-01 13:32:47 +00001997
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001998 strcat(help_text, buf);
Robert Siemborski976847c2000-07-06 03:43:05 +00001999 strcat(help_text, "\n");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002000 }
2001
2002}
Chris Allegretta3bc8c722000-12-10 17:03:25 +00002003#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002004
Chris Allegretta756f2202000-09-01 13:32:47 +00002005void do_toggle(int which)
2006{
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002007#ifdef NANO_SMALL
2008 nano_disabled_msg();
2009#else
Jordi Mallach2dc0f6b2000-09-07 10:48:00 +00002010 char *enabled = _("enabled");
2011 char *disabled = _("disabled");
Chris Allegrettaf0f63a82000-09-02 18:44:21 +00002012
Chris Allegretta2a42af12000-09-12 23:02:49 +00002013 if (ISSET(toggles[which].flag))
Chris Allegretta756f2202000-09-01 13:32:47 +00002014 UNSET(toggles[which].flag);
Chris Allegretta2a42af12000-09-12 23:02:49 +00002015 else
Chris Allegretta756f2202000-09-01 13:32:47 +00002016 SET(toggles[which].flag);
Chris Allegretta2a42af12000-09-12 23:02:49 +00002017
Chris Allegretta756f2202000-09-01 13:32:47 +00002018 switch (toggles[which].val) {
2019 case TOGGLE_PICOMODE_KEY:
Chris Allegretta07798352000-11-27 22:58:23 +00002020 shortcut_init(0);
Chris Allegretta756f2202000-09-01 13:32:47 +00002021 display_main_list();
2022 break;
2023 case TOGGLE_SUSPEND_KEY:
2024 signal_init();
2025 break;
2026 case TOGGLE_MOUSE_KEY:
2027 mouse_init();
2028 break;
2029 case TOGGLE_NOHELP_KEY:
Chris Allegretta2a42af12000-09-12 23:02:49 +00002030 wclear(bottomwin);
2031 wrefresh(bottomwin);
2032 window_init();
Chris Allegrettaaffeda82000-12-18 04:03:48 +00002033 fix_editbot();
Chris Allegretta2a42af12000-09-12 23:02:49 +00002034 edit_refresh();
2035 display_main_list();
Chris Allegretta756f2202000-09-01 13:32:47 +00002036 break;
2037 }
Chris Allegretta2a42af12000-09-12 23:02:49 +00002038
2039 if (!ISSET(toggles[which].flag)) {
2040 if (toggles[which].val == TOGGLE_NOHELP_KEY ||
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002041 toggles[which].val == TOGGLE_WRAP_KEY)
Chris Allegretta2a42af12000-09-12 23:02:49 +00002042 statusbar("%s %s", toggles[which].desc, enabled);
2043 else
2044 statusbar("%s %s", toggles[which].desc, disabled);
2045 } else {
2046 if (toggles[which].val == TOGGLE_NOHELP_KEY ||
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002047 toggles[which].val == TOGGLE_WRAP_KEY)
Chris Allegretta2a42af12000-09-12 23:02:49 +00002048 statusbar("%s %s", toggles[which].desc, disabled);
2049 else
2050 statusbar("%s %s", toggles[which].desc, enabled);
2051 }
Chris Allegretta756f2202000-09-01 13:32:47 +00002052 SET(DISABLE_CURPOS);
2053
2054#endif
2055}
2056
Chris Allegretta201d9bf2001-01-14 03:17:53 +00002057/* If the NumLock key has made the keypad gone awry, print an error
2058 message, hopefully we can address it later. */
2059void print_numlock_warning(void)
2060{
2061 static int didmsg = 0;
2062 if (!didmsg) {
Chris Allegretta40973792001-01-15 20:25:24 +00002063 statusbar(_("NumLock glitch detected. Keypad will malfunction with NumLock off"));
Chris Allegretta201d9bf2001-01-14 03:17:53 +00002064 didmsg = 1;
2065 }
2066}
2067
Chris Allegretta1748cd12001-01-13 17:22:54 +00002068/* This function returns the correct keystroke, given the A,B,C or D
2069 input key. This is a common sequence of many terms which send
2070 Esc-O-[A-D] or Esc-[-[A-D]. */
2071int ABCD(int input)
2072{
2073 switch(input)
2074 {
2075 case 'A':
2076 return(KEY_UP);
2077 case 'B':
2078 return(KEY_DOWN);
2079 case 'C':
2080 return(KEY_RIGHT);
2081 case 'D':
2082 return(KEY_LEFT);
2083 default:
2084 return 0;
2085 }
2086}
2087
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002088int main(int argc, char *argv[])
2089{
2090 int optchr;
2091 int kbinput; /* Input from keyboard */
2092 long startline = 0; /* Line to try and start at */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002093 int keyhandled = 0; /* Have we handled the keystroke yet? */
Chris Allegretta51b3eec2000-12-18 02:23:50 +00002094 int i, modify_control_seq = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002095 char *argv0;
Chris Allegrettaad1dacc2000-09-21 04:25:45 +00002096#ifdef _POSIX_VDISABLE
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002097 struct termios term;
Chris Allegrettaad1dacc2000-09-21 04:25:45 +00002098#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002099
2100#ifdef HAVE_GETOPT_LONG
2101 int option_index = 0;
2102 struct option long_options[] = {
Chris Allegretta805c26d2000-09-06 13:39:17 +00002103#ifdef HAVE_REGEX_H
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002104 {"regexp", 0, 0, 'R'},
Chris Allegretta47805612000-07-07 02:35:34 +00002105#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002106 {"version", 0, 0, 'V'},
2107 {"const", 0, 0, 'c'},
2108 {"suspend", 0, 0, 'z'},
2109 {"nowrap", 0, 0, 'w'},
2110 {"nohelp", 0, 0, 'x'},
2111 {"help", 0, 0, 'h'},
Chris Allegretta7492cec2000-12-18 04:55:21 +00002112 {"view", 0, 0, 'v'},
Chris Allegrettad19e9912000-07-12 18:14:51 +00002113#ifndef NANO_SMALL
Chris Allegretta627de192000-07-12 02:09:17 +00002114 {"cut", 0, 0, 'k'},
Chris Allegrettad19e9912000-07-12 18:14:51 +00002115#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002116 {"autoindent", 0, 0, 'i'},
2117 {"tempfile", 0, 0, 't'},
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002118#ifndef DISABLE_SPELLER
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002119 {"speller", 1, 0, 's'},
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002120#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002121 {"fill", 1, 0, 'r'},
2122 {"mouse", 0, 0, 'm'},
2123 {"pico", 0, 0, 'p'},
2124 {"nofollow", 0, 0, 'l'},
Chris Allegretta4dbcc3c2000-08-04 15:44:29 +00002125 {"tabsize", 1, 0, 'T'},
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002126 {0, 0, 0, 0}
2127 };
2128#endif
2129
2130 /* Flag inits... */
2131 SET(FOLLOW_SYMLINKS);
2132
2133#ifndef NANO_SMALL
2134 setlocale(LC_ALL, "");
2135 bindtextdomain(PACKAGE, LOCALEDIR);
2136 textdomain(PACKAGE);
2137#endif
2138
2139#ifdef HAVE_GETOPT_LONG
Chris Allegretta51b3eec2000-12-18 02:23:50 +00002140 while ((optchr = getopt_long(argc, argv, "?T:RVbcefhiklmpr:s:tvwxz",
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002141 long_options, &option_index)) != EOF) {
2142#else
Chris Allegretta51b3eec2000-12-18 02:23:50 +00002143 while ((optchr = getopt(argc, argv, "h?T:RVbcefiklmpr:s:tvwxz")) != EOF) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002144#endif
2145
2146 switch (optchr) {
Chris Allegretta6724a7e2000-06-19 23:19:07 +00002147 case 'T':
Chris Allegretta99bf73f2000-08-04 00:22:08 +00002148 tabsize = atoi(optarg);
2149 if (tabsize <= 0) {
Chris Allegretta6724a7e2000-06-19 23:19:07 +00002150 usage(); /* To stop bogus data for tab width */
2151 finish(1);
2152 }
2153 break;
Chris Allegretta805c26d2000-09-06 13:39:17 +00002154#ifdef HAVE_REGEX_H
Chris Allegretta9fc8d432000-07-07 01:49:52 +00002155 case 'R':
2156 SET(USE_REGEXP);
2157 break;
Chris Allegretta47805612000-07-07 02:35:34 +00002158#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002159 case 'V':
2160 version();
2161 exit(0);
Chris Allegretta51b3eec2000-12-18 02:23:50 +00002162 case 'b':
2163 case 'e':
2164 case 'f':
2165 /* Pico compatibility flags */
2166 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002167 case 'c':
2168 SET(CONSTUPDATE);
2169 break;
2170 case 'h':
2171 case '?':
2172 usage();
2173 exit(0);
2174 case 'i':
2175 SET(AUTOINDENT);
2176 break;
Chris Allegrettad19e9912000-07-12 18:14:51 +00002177#ifndef NANO_SMALL
Chris Allegretta627de192000-07-12 02:09:17 +00002178 case 'k':
2179 SET(CUT_TO_END);
2180 break;
Chris Allegrettad19e9912000-07-12 18:14:51 +00002181#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002182 case 'l':
2183 UNSET(FOLLOW_SYMLINKS);
2184 break;
2185 case 'm':
2186 SET(USE_MOUSE);
2187 break;
2188 case 'p':
Chris Allegrettabf9a8cc2000-11-17 01:37:39 +00002189 SET(PICO_MODE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002190 break;
2191 case 'r':
2192 fill = atoi(optarg);
2193 if (fill <= 0) {
2194 usage(); /* To stop bogus data (like a string) */
2195 finish(1);
2196 }
2197 break;
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002198#ifndef DISABLE_SPELLER
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002199 case 's':
2200 alt_speller = nmalloc(strlen(optarg) + 1);
2201 strcpy(alt_speller, optarg);
2202 break;
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002203#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002204 case 't':
Chris Allegretta30885552000-07-14 01:20:12 +00002205 SET(TEMP_OPT);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002206 break;
2207 case 'v':
2208 SET(VIEW_MODE);
2209 break;
2210 case 'w':
2211 SET(NO_WRAP);
2212 break;
2213 case 'x':
2214 SET(NO_HELP);
2215 break;
2216 case 'z':
2217 SET(SUSPEND);
2218 break;
2219 default:
2220 usage();
Chris Allegretta4da1fc62000-06-21 03:00:43 +00002221 exit(0);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002222 }
2223
2224 }
2225
2226 argv0 = strrchr(argv[0], '/');
2227 if ((argv0 && strstr(argv0, "pico"))
2228 || (!argv0 && strstr(argv[0], "pico")))
Chris Allegrettabf9a8cc2000-11-17 01:37:39 +00002229 SET(PICO_MODE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002230
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002231 /* See if there's a non-option in argv (first non-option is the
2232 filename, if +LINE is not given) */
2233 if (argc == 1 || argc <= optind)
Chris Allegretta1a6e9042000-12-14 13:56:28 +00002234 clear_filename();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002235 else {
2236 /* Look for the +line flag... */
2237 if (argv[optind][0] == '+') {
2238 startline = atoi(&argv[optind][1]);
2239 optind++;
2240 if (argc == 1 || argc <= optind)
Chris Allegretta1a6e9042000-12-14 13:56:28 +00002241 clear_filename();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002242 else
Chris Allegretta1a6e9042000-12-14 13:56:28 +00002243 filename = mallocstrcpy(filename, argv[optind]);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002244
Chris Allegretta1a6e9042000-12-14 13:56:28 +00002245 } else
2246 filename = mallocstrcpy(filename, argv[optind]);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002247 }
2248
2249
2250 /* First back up the old settings so they can be restored, duh */
Chris Allegretta4da1fc62000-06-21 03:00:43 +00002251 tcgetattr(0, &oldterm);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002252
Chris Allegretta9239d742000-09-06 15:19:18 +00002253#ifdef _POSIX_VDISABLE
Chris Allegretta8f6c0692000-07-19 01:16:18 +00002254 term = oldterm;
2255 term.c_cc[VINTR] = _POSIX_VDISABLE;
2256 term.c_cc[VQUIT] = _POSIX_VDISABLE;
2257 term.c_lflag &= ~IEXTEN;
Chris Allegretta4da1fc62000-06-21 03:00:43 +00002258 tcsetattr(0, TCSANOW, &term);
Chris Allegretta9239d742000-09-06 15:19:18 +00002259#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002260
2261 /* now ncurses init stuff... */
2262 initscr();
2263 savetty();
2264 nonl();
2265 cbreak();
2266 noecho();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002267
2268 /* Set up some global variables */
2269 global_init();
Chris Allegretta07798352000-11-27 22:58:23 +00002270 shortcut_init(0);
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002271#ifndef DISABLE_HELP
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002272 init_help_msg();
2273 help_init();
Chris Allegretta3bc8c722000-12-10 17:03:25 +00002274#endif
Chris Allegretta756f2202000-09-01 13:32:47 +00002275 signal_init();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002276
2277#ifdef DEBUG
2278 fprintf(stderr, _("Main: set up windows\n"));
2279#endif
2280
Chris Allegretta2a42af12000-09-12 23:02:49 +00002281 window_init();
Chris Allegretta756f2202000-09-01 13:32:47 +00002282 mouse_init();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002283
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002284#ifdef DEBUG
2285 fprintf(stderr, _("Main: bottom win\n"));
2286#endif
2287 /* Set up up bottom of window */
2288 display_main_list();
2289
2290#ifdef DEBUG
2291 fprintf(stderr, _("Main: open file\n"));
2292#endif
2293
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002294 titlebar(NULL);
Chris Allegretta31c76662000-11-21 06:20:20 +00002295
2296 /* Now we check to see if argv[optind] is non-null to determine if
2297 we're dealing with a new file or not, not argc == 1... */
2298 if (argv[optind] == NULL)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002299 new_file();
2300 else
2301 open_file(filename, 0, 0);
2302
2303 if (startline > 0)
2304 do_gotoline(startline);
2305 else
Chris Allegretta234a34d2000-07-29 04:33:38 +00002306 edit_update(fileage, CENTER);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002307
Robert Siemborski6967eec2000-07-08 14:23:32 +00002308 edit_refresh();
2309 reset_cursor();
2310
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002311 while (1) {
Chris Allegretta9239d742000-09-06 15:19:18 +00002312
2313#ifndef _POSIX_VDISABLE
2314 /* We're going to have to do it the old way, i.e. on cygwin */
2315 raw();
2316#endif
2317
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002318 kbinput = wgetch(edit);
Chris Allegrettac08f50d2001-01-06 18:12:43 +00002319#ifdef DEBUG
2320 fprintf(stderr, "AHA! %c (%d)\n", kbinput, kbinput);
2321#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002322 if (kbinput == 27) { /* Grab Alt-key stuff first */
2323 switch (kbinput = wgetch(edit)) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002324 /* Alt-O, suddenly very important ;) */
Chris Allegretta16e41682000-09-11 22:33:54 +00002325 case 79:
2326 kbinput = wgetch(edit);
Chris Allegretta1748cd12001-01-13 17:22:54 +00002327 if (kbinput <= 'D' && kbinput >= 'A')
2328 kbinput = ABCD(kbinput);
Chris Allegretta201d9bf2001-01-14 03:17:53 +00002329 else if (kbinput <= 'z' && kbinput >= 'j')
2330 print_numlock_warning();
2331 else if (kbinput <= 'S' && kbinput >= 'P')
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002332 kbinput = KEY_F(kbinput - 79);
Chris Allegretta16e41682000-09-11 22:33:54 +00002333#ifdef DEBUG
2334 else {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002335 fprintf(stderr, _("I got Alt-O-%c! (%d)\n"),
2336 kbinput, kbinput);
2337 break;
Chris Allegretta16e41682000-09-11 22:33:54 +00002338 }
2339#endif
2340 break;
Chris Allegretta51b3eec2000-12-18 02:23:50 +00002341 case 27:
2342 /* If we get Alt-Alt, the next keystroke should be the same as a
2343 control sequence */
2344 modify_control_seq = 1;
2345 keyhandled = 1;
2346 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002347 case 91:
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002348 switch (kbinput = wgetch(edit)) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002349 case '1': /* Alt-[-1-[0-5,7-9] = F1-F8 in X at least */
Chris Allegretta16e41682000-09-11 22:33:54 +00002350 kbinput = wgetch(edit);
2351 if (kbinput >= '1' && kbinput <= '5') {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002352 kbinput = KEY_F(kbinput - 48);
2353 wgetch(edit);
2354 } else if (kbinput >= '7' && kbinput <= '9') {
2355 kbinput = KEY_F(kbinput - 49);
2356 wgetch(edit);
2357 } else if (kbinput == 126)
2358 kbinput = KEY_HOME;
Chris Allegretta16e41682000-09-11 22:33:54 +00002359
2360#ifdef DEBUG
2361 else {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002362 fprintf(stderr, _("I got Alt-[-1-%c! (%d)\n"),
2363 kbinput, kbinput);
2364 break;
Chris Allegretta16e41682000-09-11 22:33:54 +00002365 }
2366#endif
2367
2368 break;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002369 case '2': /* Alt-[-2-[0,1,3,4] = F9-F12 in many terms */
Chris Allegretta16e41682000-09-11 22:33:54 +00002370 kbinput = wgetch(edit);
Chris Allegretta16e41682000-09-11 22:33:54 +00002371 switch (kbinput) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002372 case '0':
2373 kbinput = KEY_F(9);
2374 wgetch(edit);
2375 break;
2376 case '1':
2377 kbinput = KEY_F(10);
2378 wgetch(edit);
2379 break;
2380 case '3':
2381 kbinput = KEY_F(11);
2382 wgetch(edit);
2383 break;
2384 case '4':
2385 kbinput = KEY_F(12);
2386 wgetch(edit);
2387 break;
2388 case 126: /* Hack, make insert key do something
2389 usefile, like insert file */
2390 do_insertfile();
2391 keyhandled = 1;
2392 break;
Chris Allegretta16e41682000-09-11 22:33:54 +00002393#ifdef DEBUG
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002394 default:
2395 fprintf(stderr, _("I got Alt-[-2-%c! (%d)\n"),
2396 kbinput, kbinput);
2397 break;
Chris Allegretta16e41682000-09-11 22:33:54 +00002398#endif
2399
2400 }
2401 break;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002402 case '3': /* Alt-[-3 = Delete? */
Chris Allegretta16e41682000-09-11 22:33:54 +00002403 kbinput = NANO_DELETE_KEY;
2404 wgetch(edit);
2405 break;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002406 case '4': /* Alt-[-4 = End? */
Chris Allegretta16e41682000-09-11 22:33:54 +00002407 kbinput = NANO_END_KEY;
2408 wgetch(edit);
2409 break;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002410 case '5': /* Alt-[-5 = Page Up */
Chris Allegretta16e41682000-09-11 22:33:54 +00002411 kbinput = KEY_PPAGE;
2412 wgetch(edit);
2413 break;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002414 case '6': /* Alt-[-6 = Page Down */
Chris Allegretta16e41682000-09-11 22:33:54 +00002415 kbinput = KEY_NPAGE;
2416 wgetch(edit);
2417 break;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002418 case '[': /* Alt-[-[-[A-E], F1-F5 in linux console */
Chris Allegretta16e41682000-09-11 22:33:54 +00002419 kbinput = wgetch(edit);
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002420 if (kbinput >= 'A' && kbinput <= 'E')
2421 kbinput = KEY_F(kbinput - 64);
Chris Allegretta16e41682000-09-11 22:33:54 +00002422 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002423 case 'A':
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002424 case 'B':
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002425 case 'C':
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002426 case 'D':
Chris Allegretta1748cd12001-01-13 17:22:54 +00002427 kbinput = ABCD(kbinput);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002428 break;
2429 case 'H':
2430 kbinput = KEY_HOME;
2431 break;
2432 case 'F':
2433 kbinput = KEY_END;
2434 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002435 default:
2436#ifdef DEBUG
2437 fprintf(stderr, _("I got Alt-[-%c! (%d)\n"),
2438 kbinput, kbinput);
2439#endif
2440 break;
2441 }
2442 break;
2443 default:
2444
2445 /* Check for the altkey defs.... */
2446 for (i = 0; i <= MAIN_LIST_LEN - 1; i++)
2447 if (kbinput == main_list[i].altval ||
2448 kbinput == main_list[i].altval - 32) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002449 kbinput = main_list[i].val;
2450 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002451 }
Chris Allegretta756f2202000-09-01 13:32:47 +00002452#ifndef NANO_SMALL
2453 /* And for toggle switches */
2454 for (i = 0; i <= TOGGLE_LEN - 1 && !keyhandled; i++)
2455 if (kbinput == toggles[i].val ||
2456 kbinput == toggles[i].val - 32) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002457 do_toggle(i);
2458 keyhandled = 1;
2459 break;
Chris Allegretta756f2202000-09-01 13:32:47 +00002460 }
2461#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002462#ifdef DEBUG
2463 fprintf(stderr, _("I got Alt-%c! (%d)\n"), kbinput,
2464 kbinput);
2465#endif
2466 break;
2467 }
2468 }
Chris Allegretta51b3eec2000-12-18 02:23:50 +00002469 /* If the modify_control_seq is set, we received an Alt-Alt
2470 sequence before this, so we make this key a control sequence
2471 by subtracting 64 or 96, depending on its value. */
2472 if (!keyhandled && modify_control_seq) {
2473 if (kbinput >= 'A' && kbinput < 'a')
2474 kbinput -= 64;
2475 else if (kbinput >= 'a' && kbinput <= 'z')
2476 kbinput -= 96;
2477
2478 modify_control_seq = 0;
2479 }
2480
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002481 /* Look through the main shortcut list to see if we've hit a
2482 shortcut key */
Chris Allegretta756f2202000-09-01 13:32:47 +00002483 for (i = 0; i < MAIN_LIST_LEN && !keyhandled; i++) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002484 if (kbinput == main_list[i].val ||
2485 (main_list[i].misc1 && kbinput == main_list[i].misc1) ||
2486 (main_list[i].misc2 && kbinput == main_list[i].misc2)) {
2487 if (ISSET(VIEW_MODE) && !main_list[i].viewok)
2488 print_view_warning();
2489 else
2490 main_list[i].func();
2491 keyhandled = 1;
2492 }
2493 }
Chris Allegretta51b3eec2000-12-18 02:23:50 +00002494 /* If we're in raw mode or using Alt-Alt-x, we have to catch
2495 Control-S and Control-Q */
Chris Allegretta9239d742000-09-06 15:19:18 +00002496 if (kbinput == 17 || kbinput == 19)
2497 keyhandled = 1;
2498
Chris Allegretta51b3eec2000-12-18 02:23:50 +00002499 /* Catch ^Z by hand when triggered also */
Chris Allegretta9239d742000-09-06 15:19:18 +00002500 if (kbinput == 26) {
2501 if (ISSET(SUSPEND))
2502 do_suspend(0);
2503 keyhandled = 1;
2504 }
Chris Allegretta9239d742000-09-06 15:19:18 +00002505
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002506 /* Last gasp, stuff that's not in the main lists */
2507 if (!keyhandled)
2508 switch (kbinput) {
2509#ifndef NANO_SMALL
2510#ifdef NCURSES_MOUSE_VERSION
2511 case KEY_MOUSE:
2512 do_mouse();
2513 break;
2514#endif
2515#endif
2516 case 0: /* Erg */
2517 do_next_word();
2518 break;
Chris Allegrettaf4f7e042001-01-04 16:56:15 +00002519
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002520 case 331: /* Stuff that we don't want to do squat */
2521 case -1:
2522 case 410: /* Must ignore this, it gets sent when we resize */
Chris Allegrettaad1dacc2000-09-21 04:25:45 +00002523#ifdef PDCURSES
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002524 case 541: /* ???? */
2525 case 542: /* Control and alt in Windows *shrug* */
Chris Allegretta72623582000-11-29 23:43:28 +00002526 case 543: /* Right ctrl key */
Chris Allegrettaad1dacc2000-09-21 04:25:45 +00002527 case 544:
Chris Allegretta72623582000-11-29 23:43:28 +00002528 case 545: /* Right alt key */
Chris Allegrettaad1dacc2000-09-21 04:25:45 +00002529#endif
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00002530
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002531 break;
2532 default:
2533#ifdef DEBUG
2534 fprintf(stderr, "I got %c (%d)!\n", kbinput, kbinput);
2535#endif
2536 /* We no longer stop unhandled sequences so that people with
2537 odd character sets can type... */
2538
2539 if (ISSET(VIEW_MODE)) {
2540 print_view_warning();
2541 break;
2542 }
2543 do_char(kbinput);
2544 }
Chris Allegretta756f2202000-09-01 13:32:47 +00002545 if (ISSET(CONSTUPDATE)) {
2546 if (ISSET(DISABLE_CURPOS))
2547 UNSET(DISABLE_CURPOS);
2548 else
2549 do_cursorpos();
2550 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002551
2552 reset_cursor();
2553 wrefresh(edit);
2554 keyhandled = 0;
2555 }
2556
2557 getchar();
2558 finish(0);
2559
2560}