Chris Allegretta | 11b0011 | 2000-08-06 21:13:45 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2 | /************************************************************************** |
| 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 Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | #include <ctype.h> |
| 36 | #include <locale.h> |
| 37 | #include <limits.h> |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 38 | #include <assert.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 39 | |
| 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 Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 64 | int fill = 0; /* Fill - where to wrap lines, basically */ |
| 65 | static char *alt_speller; /* Alternative spell command */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 66 | struct termios oldterm; /* The user's original term settings */ |
Chris Allegretta | 18f8be0 | 2000-09-04 03:20:38 +0000 | [diff] [blame] | 67 | static struct sigaction act; /* For all out fun signal handlers */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 68 | |
Chris Allegretta | 6512163 | 2000-12-18 07:05:27 +0000 | [diff] [blame] | 69 | #if !defined(NANO_SMALL) && !defined(DISABLE_HELP) |
| 70 | static char *help_text_init = ""; /* Initial message, not including shortcuts */ |
| 71 | #endif |
| 72 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 73 | char *last_search = NULL; /* Last string we searched for */ |
| 74 | char *last_replace = NULL; /* Last replacement string */ |
| 75 | int search_last_line; /* Is this the last search line? */ |
| 76 | |
Chris Allegretta | 1cc0b7f | 2000-11-03 01:29:04 +0000 | [diff] [blame] | 77 | void keypad_on(int yesno) |
| 78 | { |
| 79 | keypad(edit, yesno); |
| 80 | keypad(bottomwin, yesno); |
| 81 | } |
| 82 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 83 | /* What we do when we're all set to exit */ |
| 84 | RETSIGTYPE finish(int sigage) |
| 85 | { |
| 86 | if (!ISSET(NO_HELP)) { |
| 87 | mvwaddstr(bottomwin, 1, 0, hblank); |
| 88 | mvwaddstr(bottomwin, 2, 0, hblank); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 89 | } else |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 90 | mvwaddstr(bottomwin, 0, 0, hblank); |
Chris Allegretta | 1cc0b7f | 2000-11-03 01:29:04 +0000 | [diff] [blame] | 91 | |
| 92 | /* Apparently you REALLY can't get away with not calling keypad() |
| 93 | or your window looks awful when it exits. so we just call it right |
| 94 | before we exit, muhaha :-) */ |
| 95 | keypad_on(TRUE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 96 | wrefresh(bottomwin); |
| 97 | endwin(); |
| 98 | |
| 99 | /* Restore the old term settings */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 100 | tcsetattr(0, TCSANOW, &oldterm); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 101 | |
| 102 | exit(sigage); |
| 103 | } |
| 104 | |
| 105 | /* Die (gracefully?) */ |
| 106 | void die(char *msg, ...) |
| 107 | { |
| 108 | va_list ap; |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 109 | char *name; |
| 110 | int i; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 111 | |
| 112 | va_start(ap, msg); |
| 113 | vfprintf(stderr, msg, ap); |
| 114 | va_end(ap); |
| 115 | |
| 116 | /* if we can't save we have REAL bad problems, |
Robert Siemborski | fcf32bf | 2000-07-17 03:04:54 +0000 | [diff] [blame] | 117 | * but we might as well TRY. */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 118 | if (filename[0] == '\0') { |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 119 | name = "nano.save"; |
| 120 | i = write_file(name, 1); |
Robert Siemborski | fcf32bf | 2000-07-17 03:04:54 +0000 | [diff] [blame] | 121 | } else { |
Chris Allegretta | e7a5893 | 2000-12-02 02:36:22 +0000 | [diff] [blame] | 122 | |
| 123 | char *buf = nmalloc(strlen(filename) + 6); |
| 124 | strcpy(buf, filename); |
| 125 | strcat(buf, ".save"); |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 126 | i = write_file(buf, 1); |
| 127 | name = buf; |
Robert Siemborski | fcf32bf | 2000-07-17 03:04:54 +0000 | [diff] [blame] | 128 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 129 | /* Restore the old term settings */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 130 | tcsetattr(0, TCSANOW, &oldterm); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 131 | |
| 132 | clear(); |
| 133 | refresh(); |
| 134 | resetty(); |
| 135 | endwin(); |
| 136 | |
| 137 | fprintf(stderr, msg); |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 138 | if (i != -1) |
| 139 | fprintf(stderr, _("\nBuffer written to %s\n"), name); |
| 140 | else |
Chris Allegretta | 9756d62 | 2000-12-03 03:06:45 +0000 | [diff] [blame] | 141 | fprintf(stderr, _("\nNo %s written (file exists?)\n"), name); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 142 | |
| 143 | exit(1); /* We have a problem: exit w/ errorlevel(1) */ |
| 144 | } |
| 145 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 146 | void print_view_warning(void) |
| 147 | { |
| 148 | statusbar(_("Key illegal in VIEW mode")); |
| 149 | } |
| 150 | |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 151 | void clear_filename(void) |
| 152 | { |
| 153 | if (filename != NULL) |
| 154 | free(filename); |
| 155 | filename = nmalloc(1); |
| 156 | filename[0] = 0; |
| 157 | } |
Chris Allegretta | 1cc0b7f | 2000-11-03 01:29:04 +0000 | [diff] [blame] | 158 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 159 | /* Initialize global variables - no better way for now */ |
| 160 | void global_init(void) |
| 161 | { |
| 162 | int i; |
| 163 | |
| 164 | center_x = COLS / 2; |
| 165 | center_y = LINES / 2; |
| 166 | current_x = 0; |
| 167 | current_y = 0; |
| 168 | editwinrows = LINES - 5 + no_help(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 169 | fileage = NULL; |
| 170 | cutbuffer = NULL; |
| 171 | current = NULL; |
| 172 | edittop = NULL; |
| 173 | editbot = NULL; |
| 174 | totlines = 0; |
| 175 | placewewant = 0; |
| 176 | if (!fill) |
| 177 | fill = COLS - 8; |
| 178 | hblank = nmalloc(COLS + 1); |
| 179 | |
| 180 | /* Thanks BG for this bit... */ |
| 181 | for (i = 0; i <= COLS - 1; i++) |
| 182 | hblank[i] = ' '; |
| 183 | hblank[i] = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void init_help_msg(void) |
| 187 | { |
| 188 | |
Chris Allegretta | 3bc8c72 | 2000-12-10 17:03:25 +0000 | [diff] [blame] | 189 | #if !defined(NANO_SMALL) && !defined(DISABLE_HELP) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 190 | |
| 191 | help_text_init = |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 192 | _(" nano help text\n\n " |
| 193 | "The nano editor is designed to emulate the functionality and " |
| 194 | "ease-of-use of the UW Pico text editor. There are four main " |
| 195 | "sections of the editor: The top line shows the program " |
| 196 | "version, the current filename being edited, and whether " |
| 197 | "or not the file has been modified. Next is the main editor " |
| 198 | "window showing the file being edited. The status line is " |
| 199 | "the third line from the bottom and shows important messages. " |
| 200 | "The bottom two lines show the most commonly used shortcuts " |
| 201 | "in the editor.\n\n " |
| 202 | "The notation for shortcuts is as follows: Control-key " |
Chris Allegretta | e49f123 | 2000-09-02 07:20:39 +0000 | [diff] [blame] | 203 | "sequences are notated with a caret (^) symbol and are entered " |
Chris Allegretta | d56bd79 | 2000-09-02 07:27:10 +0000 | [diff] [blame] | 204 | "with the Control (Ctrl) key. Escape-key sequences are notated " |
| 205 | "with the Meta (M) symbol and can be entered using either the " |
| 206 | "Esc, Alt or Meta key depending on your keyboard setup. The " |
| 207 | "following keystrokes are available in the main editor window. " |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 208 | "Optional keys are shown in parentheses:\n\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 209 | #endif |
| 210 | |
| 211 | } |
| 212 | |
| 213 | /* Make a copy of a node to a pointer (space will be malloc()ed) */ |
| 214 | filestruct *copy_node(filestruct * src) |
| 215 | { |
| 216 | filestruct *dst; |
| 217 | |
| 218 | dst = nmalloc(sizeof(filestruct)); |
| 219 | dst->data = nmalloc(strlen(src->data) + 1); |
| 220 | |
| 221 | dst->next = src->next; |
| 222 | dst->prev = src->prev; |
| 223 | |
| 224 | strcpy(dst->data, src->data); |
| 225 | dst->lineno = src->lineno; |
| 226 | |
| 227 | return dst; |
| 228 | } |
| 229 | |
| 230 | /* Unlink a node from the rest of the struct */ |
| 231 | void unlink_node(filestruct * fileptr) |
| 232 | { |
| 233 | if (fileptr->prev != NULL) |
| 234 | fileptr->prev->next = fileptr->next; |
| 235 | |
| 236 | if (fileptr->next != NULL) |
| 237 | fileptr->next->prev = fileptr->prev; |
| 238 | } |
| 239 | |
| 240 | void delete_node(filestruct * fileptr) |
| 241 | { |
| 242 | if (fileptr->data != NULL) |
| 243 | free(fileptr->data); |
| 244 | free(fileptr); |
| 245 | } |
| 246 | |
| 247 | /* Okay, now let's duplicate a whole struct! */ |
| 248 | filestruct *copy_filestruct(filestruct * src) |
| 249 | { |
| 250 | filestruct *dst, *tmp, *head, *prev; |
| 251 | |
| 252 | head = copy_node(src); |
| 253 | dst = head; /* Else we barf on copying just one line */ |
| 254 | head->prev = NULL; |
| 255 | tmp = src->next; |
| 256 | prev = head; |
| 257 | |
| 258 | while (tmp != NULL) { |
| 259 | dst = copy_node(tmp); |
| 260 | dst->prev = prev; |
| 261 | prev->next = dst; |
| 262 | |
| 263 | prev = dst; |
| 264 | tmp = tmp->next; |
| 265 | } |
| 266 | |
| 267 | dst->next = NULL; |
| 268 | return head; |
| 269 | } |
| 270 | |
| 271 | /* Free() a single node */ |
| 272 | int free_node(filestruct * src) |
| 273 | { |
| 274 | if (src == NULL) |
| 275 | return 0; |
| 276 | |
| 277 | if (src->next != NULL) |
| 278 | free(src->data); |
| 279 | free(src); |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | int free_filestruct(filestruct * src) |
| 284 | { |
| 285 | filestruct *fileptr = src; |
| 286 | |
| 287 | if (src == NULL) |
| 288 | return 0; |
| 289 | |
| 290 | while (fileptr->next != NULL) { |
| 291 | fileptr = fileptr->next; |
| 292 | free_node(fileptr->prev); |
| 293 | |
| 294 | #ifdef DEBUG |
| 295 | fprintf(stderr, _("free_node(): free'd a node, YAY!\n")); |
| 296 | #endif |
| 297 | } |
| 298 | free_node(fileptr); |
| 299 | #ifdef DEBUG |
| 300 | fprintf(stderr, _("free_node(): free'd last node.\n")); |
| 301 | #endif |
| 302 | |
| 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | int renumber_all(void) |
| 307 | { |
| 308 | filestruct *temp; |
| 309 | long i = 1; |
| 310 | |
| 311 | for (temp = fileage; temp != NULL; temp = temp->next) { |
| 312 | temp->lineno = i++; |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | int renumber(filestruct * fileptr) |
| 319 | { |
| 320 | filestruct *temp; |
| 321 | |
| 322 | if (fileptr == NULL || fileptr->prev == NULL || fileptr == fileage) { |
| 323 | renumber_all(); |
| 324 | return 0; |
| 325 | } |
| 326 | for (temp = fileptr; temp != NULL; temp = temp->next) { |
Chris Allegretta | 5146fec | 2000-12-10 05:44:02 +0000 | [diff] [blame] | 327 | if (temp->prev != NULL) |
| 328 | temp->lineno = temp->prev->lineno + 1; |
| 329 | else |
| 330 | temp->lineno = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /* Fix the memory allocation for a string */ |
| 337 | void align(char **strp) |
| 338 | { |
| 339 | /* There was a serious bug here: the new address was never |
| 340 | stored anywhere... */ |
| 341 | |
| 342 | *strp = nrealloc(*strp, strlen(*strp) + 1); |
| 343 | } |
| 344 | |
Chris Allegretta | 6925bbd | 2000-07-28 01:41:29 +0000 | [diff] [blame] | 345 | /* Null a string at a certain index and align it */ |
| 346 | void null_at(char *data, int index) |
| 347 | { |
| 348 | data[index] = 0; |
| 349 | align(&data); |
| 350 | } |
| 351 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 352 | void usage(void) |
| 353 | { |
| 354 | #ifdef HAVE_GETOPT_LONG |
| 355 | printf(_("Usage: nano [GNU long option] [option] +LINE <file>\n\n")); |
| 356 | printf(_("Option Long option Meaning\n")); |
Chris Allegretta | 6724a7e | 2000-06-19 23:19:07 +0000 | [diff] [blame] | 357 | printf(_ |
| 358 | (" -T --tabsize=[num] Set width of a tab to num\n")); |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 359 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 360 | printf(_ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 361 | (" -R --regexp Use regular expressions for search\n")); |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 362 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 363 | printf |
| 364 | (_ |
| 365 | (" -V --version Print version information and exit\n")); |
| 366 | printf(_ |
| 367 | (" -c --const Constantly show cursor position\n")); |
| 368 | printf(_ |
| 369 | (" -h --help Show this message\n")); |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 370 | #ifndef NANO_SMALL |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 371 | printf(_ |
Chris Allegretta | 627de19 | 2000-07-12 02:09:17 +0000 | [diff] [blame] | 372 | (" -k --cut Let ^K cut from cursor to end of line\n")); |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 373 | #endif |
Chris Allegretta | 627de19 | 2000-07-12 02:09:17 +0000 | [diff] [blame] | 374 | printf(_ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 375 | (" -i --autoindent Automatically indent new lines\n")); |
| 376 | printf(_ |
Chris Allegretta | 71348ee | 2000-10-02 04:21:23 +0000 | [diff] [blame] | 377 | (" -l --nofollow Don't follow symbolic links, overwrite\n")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 378 | #ifndef NANO_SMALL |
| 379 | #ifdef NCURSES_MOUSE_VERSION |
| 380 | printf(_(" -m --mouse Enable mouse\n")); |
| 381 | #endif |
| 382 | #endif |
| 383 | printf |
| 384 | (_ |
| 385 | (" -r [#cols] --fill=[#cols] Set fill cols to (wrap lines at) #cols\n")); |
| 386 | printf(_ |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 387 | (" -p --pico Emulate Pico as closely as possible\n")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 388 | printf(_ |
| 389 | (" -s [prog] --speller=[prog] Enable alternate speller\n")); |
| 390 | printf(_ |
| 391 | (" -t --tempfile Auto save on exit, don't prompt\n")); |
| 392 | printf(_ |
| 393 | (" -v --view View (read only) mode\n")); |
| 394 | printf(_ |
| 395 | (" -w --nowrap Don't wrap long lines\n")); |
| 396 | printf(_ |
| 397 | (" -x --nohelp Don't show help window\n")); |
| 398 | printf(_ |
| 399 | (" -z --suspend Enable suspend\n")); |
| 400 | printf(_ |
| 401 | (" +LINE Start at line number LINE\n")); |
| 402 | #else |
| 403 | printf(_("Usage: nano [option] +LINE <file>\n\n")); |
| 404 | printf(_("Option Meaning\n")); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 405 | printf(_(" -T [num] Set width of a tab to num\n")); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 406 | printf(_(" -R Use regular expressions for search\n")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 407 | printf(_(" -V Print version information and exit\n")); |
| 408 | printf(_(" -c Constantly show cursor position\n")); |
| 409 | printf(_(" -h Show this message\n")); |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 410 | #ifndef NANO_SMALL |
Chris Allegretta | 627de19 | 2000-07-12 02:09:17 +0000 | [diff] [blame] | 411 | printf(_(" -k Let ^K cut from cursor to end of line\n")); |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 412 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 413 | printf(_(" -i Automatically indent new lines\n")); |
| 414 | printf(_ |
Chris Allegretta | 71348ee | 2000-10-02 04:21:23 +0000 | [diff] [blame] | 415 | (" -l Don't follow symbolic links, overwrite\n")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 416 | #ifndef NANO_SMALL |
| 417 | #ifdef NCURSES_MOUSE_VERSION |
| 418 | printf(_(" -m Enable mouse\n")); |
| 419 | #endif |
| 420 | #endif |
| 421 | printf(_ |
| 422 | (" -r [#cols] Set fill cols to (wrap lines at) #cols\n")); |
| 423 | printf(_(" -s [prog] Enable alternate speller\n")); |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 424 | printf(_(" -p Emulate Pico as closely as possible\n")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 425 | printf(_(" -t Auto save on exit, don't prompt\n")); |
| 426 | printf(_(" -v View (read only) mode\n")); |
| 427 | printf(_(" -w Don't wrap long lines\n")); |
| 428 | printf(_(" -x Don't show help window\n")); |
| 429 | printf(_(" -z Enable suspend\n")); |
| 430 | printf(_(" +LINE Start at line number LINE\n")); |
| 431 | #endif |
| 432 | exit(0); |
| 433 | } |
| 434 | |
| 435 | void version(void) |
| 436 | { |
| 437 | printf(_(" nano version %s by Chris Allegretta (compiled %s, %s)\n"), |
| 438 | VERSION, __TIME__, __DATE__); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 439 | printf(_ |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 440 | (" Email: nano@nano-editor.org Web: http://www.nano-editor.org")); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 441 | printf(_("\n Compiled options:")); |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 442 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 443 | #ifdef NANO_SMALL |
| 444 | printf(" --enable-tiny"); |
| 445 | #endif |
| 446 | #ifdef NANO_EXTRA |
| 447 | printf(" --enable-extra"); |
| 448 | #endif |
| 449 | #ifdef DISABLE_TABCOMP |
| 450 | printf(" --disable-tabcomp"); |
| 451 | #endif |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 452 | #ifdef DISABLE_JUSTIFY |
| 453 | printf(" --disable-justify"); |
| 454 | #endif |
Chris Allegretta | 7b36c52 | 2000-12-06 01:08:10 +0000 | [diff] [blame] | 455 | #ifdef DISABLE_SPELLER |
| 456 | printf(" --disable-speller"); |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 457 | #endif |
Chris Allegretta | b7d00ef | 2000-12-18 05:36:51 +0000 | [diff] [blame] | 458 | #ifdef DISABLE_HELP |
| 459 | printf(" --disable-help"); |
| 460 | #endif |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 461 | #ifdef USE_SLANG |
| 462 | printf(" --with-slang"); |
| 463 | #endif |
| 464 | printf("\n"); |
| 465 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 468 | filestruct *make_new_node(filestruct * prevnode) |
| 469 | { |
| 470 | filestruct *newnode; |
| 471 | |
| 472 | newnode = nmalloc(sizeof(filestruct)); |
| 473 | newnode->data = NULL; |
| 474 | |
| 475 | newnode->prev = prevnode; |
| 476 | newnode->next = NULL; |
| 477 | |
| 478 | if (prevnode != NULL) |
| 479 | newnode->lineno = prevnode->lineno + 1; |
| 480 | |
| 481 | return newnode; |
| 482 | } |
| 483 | |
Chris Allegretta | 7975ed8 | 2000-07-28 00:58:35 +0000 | [diff] [blame] | 484 | /* Splice a node into an existing filestruct */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 485 | void splice_node(filestruct * begin, filestruct * new, filestruct * end) |
Chris Allegretta | 7975ed8 | 2000-07-28 00:58:35 +0000 | [diff] [blame] | 486 | { |
| 487 | new->next = end; |
| 488 | new->prev = begin; |
| 489 | begin->next = new; |
| 490 | if (end != NULL) |
| 491 | end->prev = new; |
| 492 | } |
| 493 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 494 | int do_mark() |
| 495 | { |
| 496 | #ifdef NANO_SMALL |
| 497 | nano_small_msg(); |
| 498 | #else |
| 499 | if (!ISSET(MARK_ISSET)) { |
| 500 | statusbar(_("Mark Set")); |
| 501 | SET(MARK_ISSET); |
| 502 | mark_beginbuf = current; |
| 503 | mark_beginx = current_x; |
| 504 | } else { |
| 505 | statusbar(_("Mark UNset")); |
| 506 | UNSET(MARK_ISSET); |
| 507 | mark_beginbuf = NULL; |
| 508 | mark_beginx = 0; |
| 509 | |
| 510 | edit_refresh(); |
| 511 | } |
| 512 | #endif |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | int no_help(void) |
| 517 | { |
| 518 | if ISSET |
| 519 | (NO_HELP) |
| 520 | return 2; |
| 521 | else |
| 522 | return 0; |
| 523 | } |
| 524 | |
Chris Allegretta | 4eb7aa0 | 2000-12-01 18:57:11 +0000 | [diff] [blame] | 525 | #ifdef NANO_SMALL |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 526 | void nano_small_msg(void) |
| 527 | { |
| 528 | statusbar("Sorry, this function not available with nano-tiny option"); |
| 529 | } |
Chris Allegretta | 4eb7aa0 | 2000-12-01 18:57:11 +0000 | [diff] [blame] | 530 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 531 | |
Chris Allegretta | 3bc8c72 | 2000-12-10 17:03:25 +0000 | [diff] [blame] | 532 | #if defined(DISABLE_JUSTIFY) || defined(DISABLE_SPELLER) || defined(DISABLE_HELP) |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 533 | void nano_disabled_msg(void) |
| 534 | { |
| 535 | statusbar("Sorry, support for this function has been disabled"); |
| 536 | } |
Chris Allegretta | 4eb7aa0 | 2000-12-01 18:57:11 +0000 | [diff] [blame] | 537 | #endif |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 538 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 539 | /* The user typed a printable character; add it to the edit buffer */ |
| 540 | void do_char(char ch) |
| 541 | { |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 542 | /* magic-line: when a character is inserted on the current magic line, |
| 543 | * it means we need a new one! */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 544 | if (filebot == current && current->data[0] == '\0') { |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 545 | new_magicline(); |
Chris Allegretta | 28a0f89 | 2000-07-05 22:47:54 +0000 | [diff] [blame] | 546 | fix_editbot(); |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 549 | /* More dangerousness fun =) */ |
| 550 | current->data = nrealloc(current->data, strlen(current->data) + 2); |
| 551 | memmove(¤t->data[current_x + 1], |
| 552 | ¤t->data[current_x], |
| 553 | strlen(current->data) - current_x + 1); |
| 554 | current->data[current_x] = ch; |
| 555 | do_right(); |
| 556 | |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 557 | if (!ISSET(NO_WRAP) && (ch != '\t')) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 558 | check_wrap(current, ch); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 559 | set_modified(); |
| 560 | check_statblank(); |
| 561 | UNSET(KEEP_CUTBUFFER); |
| 562 | totsize++; |
| 563 | |
| 564 | } |
| 565 | |
| 566 | /* Someone hits return *gasp!* */ |
| 567 | int do_enter(filestruct * inptr) |
| 568 | { |
| 569 | filestruct *new; |
| 570 | char *tmp, *spc; |
| 571 | int extra = 0; |
| 572 | |
| 573 | new = make_new_node(inptr); |
| 574 | tmp = ¤t->data[current_x]; |
| 575 | current_x = 0; |
| 576 | |
| 577 | /* Do auto-indenting, like the neolithic Turbo Pascal editor */ |
| 578 | if (ISSET(AUTOINDENT)) { |
| 579 | spc = current->data; |
| 580 | if (spc) { |
| 581 | while ((*spc == ' ') || (*spc == '\t')) { |
| 582 | extra++; |
| 583 | spc++; |
| 584 | current_x++; |
| 585 | } |
| 586 | new->data = nmalloc(strlen(tmp) + extra + 1); |
| 587 | strncpy(new->data, current->data, extra); |
| 588 | strcpy(&new->data[extra], tmp); |
| 589 | } |
| 590 | } else { |
| 591 | new->data = nmalloc(strlen(tmp) + 1); |
| 592 | strcpy(new->data, tmp); |
| 593 | } |
| 594 | *tmp = 0; |
| 595 | |
Chris Allegretta | da721be | 2000-07-31 01:26:42 +0000 | [diff] [blame] | 596 | if (inptr->next == NULL) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 597 | filebot = new; |
| 598 | editbot = new; |
| 599 | } |
Chris Allegretta | 7975ed8 | 2000-07-28 00:58:35 +0000 | [diff] [blame] | 600 | splice_node(inptr, new, inptr->next); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 601 | |
| 602 | totsize++; |
| 603 | renumber(current); |
| 604 | current = new; |
| 605 | align(¤t->data); |
| 606 | |
Robert Siemborski | dd53ec2 | 2000-07-04 02:35:19 +0000 | [diff] [blame] | 607 | /* The logic here is as follows: |
| 608 | * -> If we are at the bottom of the buffer, we want to recenter |
| 609 | * (read: rebuild) the screen and forcably move the cursor. |
| 610 | * -> otherwise, we want simply to redraw the screen and update |
| 611 | * where we think the cursor is. |
| 612 | */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 613 | if (current_y == editwinrows - 1) { |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 614 | edit_update(current, CENTER); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 615 | reset_cursor(); |
Robert Siemborski | dd53ec2 | 2000-07-04 02:35:19 +0000 | [diff] [blame] | 616 | } else { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 617 | current_y++; |
Robert Siemborski | dd53ec2 | 2000-07-04 02:35:19 +0000 | [diff] [blame] | 618 | edit_refresh(); |
| 619 | update_cursor(); |
| 620 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 621 | |
| 622 | totlines++; |
| 623 | set_modified(); |
| 624 | |
Chris Allegretta | b0ae393 | 2000-06-15 23:39:14 +0000 | [diff] [blame] | 625 | placewewant = xplustabs(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 626 | return 1; |
| 627 | } |
| 628 | |
| 629 | int do_enter_void(void) |
| 630 | { |
| 631 | return do_enter(current); |
| 632 | } |
| 633 | |
| 634 | void do_next_word(void) |
| 635 | { |
Chris Allegretta | 9e2934f | 2000-12-01 23:49:48 +0000 | [diff] [blame] | 636 | filestruct *fileptr, *old; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 637 | int i; |
| 638 | |
| 639 | if (current == NULL) |
| 640 | return; |
| 641 | |
Chris Allegretta | 9e2934f | 2000-12-01 23:49:48 +0000 | [diff] [blame] | 642 | old = current; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 643 | i = current_x; |
| 644 | for (fileptr = current; fileptr != NULL; fileptr = fileptr->next) { |
| 645 | if (fileptr == current) { |
| 646 | while (isalnum((int) fileptr->data[i]) |
| 647 | && fileptr->data[i] != 0) |
| 648 | i++; |
| 649 | |
| 650 | if (fileptr->data[i] == 0) { |
| 651 | i = 0; |
| 652 | continue; |
| 653 | } |
| 654 | } |
| 655 | while (!isalnum((int) fileptr->data[i]) && fileptr->data[i] != 0) |
| 656 | i++; |
| 657 | |
| 658 | if (fileptr->data[i] != 0) |
| 659 | break; |
| 660 | |
| 661 | i = 0; |
| 662 | } |
| 663 | if (fileptr == NULL) |
| 664 | current = filebot; |
| 665 | else |
| 666 | current = fileptr; |
| 667 | |
| 668 | current_x = i; |
| 669 | placewewant = xplustabs(); |
Chris Allegretta | 9e2934f | 2000-12-01 23:49:48 +0000 | [diff] [blame] | 670 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 671 | if (current->lineno >= editbot->lineno) |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 672 | edit_update(current, CENTER); |
Chris Allegretta | 9e2934f | 2000-12-01 23:49:48 +0000 | [diff] [blame] | 673 | else { |
| 674 | /* If we've jumped lines, refresh the old line. We can't just use |
| 675 | * current->prev here, because we may have skipped over some blank |
| 676 | * lines, in which case the previous line is the wrong one. |
| 677 | */ |
| 678 | if (current != old) |
| 679 | update_line(old, 0); |
| 680 | |
| 681 | update_line(current, current_x); |
| 682 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 683 | |
| 684 | } |
| 685 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 686 | void do_wrap(filestruct * inptr, char input_char) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 687 | { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 688 | int i = 0; /* Index into ->data for line. */ |
| 689 | int i_tabs = 0; /* Screen position of ->data[i]. */ |
| 690 | int last_word_end = -1; /* Location of end of last word found. */ |
| 691 | int current_word_start = -1; /* Location of start of current word. */ |
| 692 | int current_word_start_t = -1; /* Location of start of current word screen position. */ |
| 693 | int current_word_end = -1; /* Location of end of current word */ |
| 694 | int current_word_end_t = -1; /* Location of end of current word screen position. */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 695 | int len = strlen(inptr->data); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 696 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 697 | int down = 0; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 698 | int right = 0; |
| 699 | struct filestruct *temp = NULL; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 700 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 701 | assert(strlenpt(inptr->data) > fill); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 702 | |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 703 | for (i = 0, i_tabs = 0; i < len; i++, i_tabs++) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 704 | if (!isspace((int) inptr->data[i])) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 705 | last_word_end = current_word_end; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 706 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 707 | current_word_start = i; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 708 | current_word_start_t = i_tabs; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 709 | |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 710 | while (!isspace((int) inptr->data[i]) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 711 | && inptr->data[i]) { |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 712 | i++; |
| 713 | i_tabs++; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 714 | if (inptr->data[i] < 32) |
| 715 | i_tabs++; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 716 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 717 | |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 718 | if (inptr->data[i]) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 719 | current_word_end = i; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 720 | current_word_end_t = i_tabs; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 721 | } else { |
| 722 | current_word_end = i - 1; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 723 | current_word_end_t = i_tabs - 1; |
| 724 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 725 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 726 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 727 | if (inptr->data[i] == NANO_CONTROL_I) { |
Chris Allegretta | 6d690a3 | 2000-08-03 22:51:21 +0000 | [diff] [blame] | 728 | if (i_tabs % tabsize != 0); |
| 729 | i_tabs += tabsize - (i_tabs % tabsize); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 730 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 731 | |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 732 | if (current_word_end_t > fill) |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 733 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Adam Rogoyski | 3d449b4 | 2000-06-19 17:30:14 +0000 | [diff] [blame] | 736 | /* There are a few (ever changing) cases of what the line could look like. |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 737 | * 1) only one word on the line before wrap point. |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 738 | * a) one word takes up the whole line with no starting spaces. |
| 739 | * - do nothing and return. |
| 740 | * b) cursor is on word or before word at wrap point and there are spaces at beginning. |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 741 | * - word starts new line. |
| 742 | * - keep white space on original line up to the cursor. |
| 743 | * *) cursor is after word at wrap point |
| 744 | * - either it's all white space after word, and this routine isn't called. |
| 745 | * - or we are actually in case 2 (2 words). |
| 746 | * 2) Two or more words on the line before wrap point. |
Adam Rogoyski | 3d449b4 | 2000-06-19 17:30:14 +0000 | [diff] [blame] | 747 | * a) cursor is at a word or space before wrap point |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 748 | * - word at wrap point starts a new line. |
Adam Rogoyski | 3d449b4 | 2000-06-19 17:30:14 +0000 | [diff] [blame] | 749 | * - white space at end of original line is cleared, unless |
| 750 | * it is all spaces between previous word and next word which appears after fill. |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 751 | * b) cursor is at the word at the wrap point. |
| 752 | * - word at wrap point starts a new line. |
Adam Rogoyski | 3d449b4 | 2000-06-19 17:30:14 +0000 | [diff] [blame] | 753 | * 1. pressed a space and at first character of wrap point word. |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 754 | * - white space on original line is kept to where cursor was. |
Adam Rogoyski | 3d449b4 | 2000-06-19 17:30:14 +0000 | [diff] [blame] | 755 | * 2. pressed non space (or space elsewhere). |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 756 | * - white space at end of original line is cleared. |
| 757 | * c) cursor is past the word at the wrap point. |
| 758 | * - word at wrap point starts a new line. |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 759 | * - white space at end of original line is cleared |
| 760 | */ |
| 761 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 762 | temp = nmalloc(sizeof(filestruct)); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 763 | |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 764 | /* Category 1a: one word taking up the whole line with no beginning spaces. */ |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 765 | if ((last_word_end == -1) && (!isspace((int) inptr->data[0]))) { |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 766 | for (i = current_word_end; i < len; i++) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 767 | if (!isspace((int) inptr->data[i]) && i < len) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 768 | current_word_start = i; |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 769 | while (!isspace((int) inptr->data[i]) && (i < len)) { |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 770 | i++; |
| 771 | } |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 772 | last_word_end = current_word_end; |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 773 | current_word_end = i; |
| 774 | break; |
| 775 | } |
| 776 | } |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 777 | |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 778 | if (last_word_end == -1) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 779 | free(temp); |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 780 | return; |
| 781 | } |
| 782 | if (current_x >= last_word_end) { |
| 783 | right = (current_x - current_word_start) + 1; |
| 784 | current_x = last_word_end; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 785 | down = 1; |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | temp->data = nmalloc(strlen(&inptr->data[current_word_start]) + 1); |
| 789 | strcpy(temp->data, &inptr->data[current_word_start]); |
| 790 | inptr->data = nrealloc(inptr->data, last_word_end + 2); |
| 791 | inptr->data[last_word_end + 1] = 0; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 792 | } else |
| 793 | /* Category 1b: one word on the line and word not taking up whole line |
| 794 | (i.e. there are spaces at the beginning of the line) */ |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 795 | if (last_word_end == -1) { |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 796 | temp->data = nmalloc(strlen(&inptr->data[current_word_start]) + 1); |
| 797 | strcpy(temp->data, &inptr->data[current_word_start]); |
| 798 | |
| 799 | /* Inside word, remove it from original, and move cursor to right spot. */ |
| 800 | if (current_x >= current_word_start) { |
| 801 | right = current_x - current_word_start; |
| 802 | current_x = 0; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 803 | down = 1; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Chris Allegretta | 6925bbd | 2000-07-28 01:41:29 +0000 | [diff] [blame] | 806 | null_at(inptr->data, current_x); |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 807 | |
| 808 | if (ISSET(MARK_ISSET) && (mark_beginbuf == inptr)) { |
| 809 | mark_beginbuf = temp; |
| 810 | mark_beginx = 0; |
| 811 | } |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /* Category 2: two or more words on the line. */ |
| 815 | else { |
| 816 | |
| 817 | /* Case 2a: cursor before word at wrap point. */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 818 | if (current_x < current_word_start) { |
| 819 | temp->data = |
| 820 | nmalloc(strlen(&inptr->data[current_word_start]) + 1); |
| 821 | strcpy(temp->data, &inptr->data[current_word_start]); |
| 822 | |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 823 | if (!isspace((int) input_char)) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 824 | i = current_word_start - 1; |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 825 | while (isspace((int) inptr->data[i])) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 826 | i--; |
| 827 | assert(i >= 0); |
| 828 | } |
| 829 | } else if (current_x <= last_word_end) |
| 830 | i = last_word_end - 1; |
| 831 | else |
| 832 | i = current_x; |
| 833 | |
| 834 | inptr->data = nrealloc(inptr->data, i + 2); |
| 835 | inptr->data[i + 1] = 0; |
| 836 | } |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 837 | |
| 838 | |
| 839 | /* Case 2b: cursor at word at wrap point. */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 840 | else if ((current_x >= current_word_start) |
| 841 | && (current_x <= (current_word_end + 1))) { |
| 842 | temp->data = |
| 843 | nmalloc(strlen(&inptr->data[current_word_start]) + 1); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 844 | strcpy(temp->data, &inptr->data[current_word_start]); |
| 845 | |
| 846 | down = 1; |
| 847 | |
| 848 | right = current_x - current_word_start; |
| 849 | i = current_word_start - 1; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 850 | if (isspace((int) input_char) |
| 851 | && (current_x == current_word_start)) { |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 852 | current_x = current_word_start; |
| 853 | |
Chris Allegretta | 6925bbd | 2000-07-28 01:41:29 +0000 | [diff] [blame] | 854 | null_at(inptr->data, current_word_start); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 855 | } else { |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 856 | |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 857 | while (isspace((int) inptr->data[i])) { |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 858 | i--; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 859 | assert(i >= 0); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 860 | } |
| 861 | inptr->data = nrealloc(inptr->data, i + 2); |
| 862 | inptr->data[i + 1] = 0; |
| 863 | } |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 864 | } |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 865 | |
| 866 | |
| 867 | /* Case 2c: cursor past word at wrap point. */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 868 | else { |
| 869 | temp->data = |
| 870 | nmalloc(strlen(&inptr->data[current_word_start]) + 1); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 871 | strcpy(temp->data, &inptr->data[current_word_start]); |
| 872 | |
| 873 | down = 1; |
| 874 | right = current_x - current_word_start; |
| 875 | |
| 876 | current_x = current_word_start; |
| 877 | i = current_word_start - 1; |
| 878 | |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 879 | while (isspace((int) inptr->data[i])) { |
Adam Rogoyski | 3d449b4 | 2000-06-19 17:30:14 +0000 | [diff] [blame] | 880 | i--; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 881 | assert(i >= 0); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 882 | inptr->data = nrealloc(inptr->data, i + 2); |
| 883 | inptr->data[i + 1] = 0; |
| 884 | } |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 885 | } |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 888 | /* We pre-pend wrapped part to next line. */ |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 889 | if (ISSET(SAMELINEWRAP) && inptr->next) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 890 | /* Plus one for the space which concatenates the two lines together plus 1 for \0. */ |
| 891 | char *p = |
| 892 | nmalloc(strlen(temp->data) + strlen(inptr->next->data) + 2); |
Adam Rogoyski | 9aeb9da | 2000-06-16 01:19:31 +0000 | [diff] [blame] | 893 | int old_x = current_x, old_y = current_y; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 894 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 895 | strcpy(p, temp->data); |
| 896 | strcat(p, " "); |
| 897 | strcat(p, inptr->next->data); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 898 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 899 | free(inptr->next->data); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 900 | inptr->next->data = p; |
| 901 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 902 | free(temp->data); |
| 903 | free(temp); |
Adam Rogoyski | 9aeb9da | 2000-06-16 01:19:31 +0000 | [diff] [blame] | 904 | |
Adam Rogoyski | 9aeb9da | 2000-06-16 01:19:31 +0000 | [diff] [blame] | 905 | current_x = old_x; |
| 906 | current_y = old_y; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 907 | } |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 908 | /* Else we start a new line. */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 909 | else { |
| 910 | temp->prev = inptr; |
| 911 | temp->next = inptr->next; |
| 912 | |
| 913 | if (inptr->next) |
| 914 | inptr->next->prev = temp; |
| 915 | inptr->next = temp; |
| 916 | |
| 917 | if (!temp->next) |
| 918 | filebot = temp; |
| 919 | |
| 920 | SET(SAMELINEWRAP); |
| 921 | } |
| 922 | |
| 923 | |
| 924 | totlines++; |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 925 | /* Everything about it makes me want this line here but it causes |
| 926 | * totsize to be high by one for some reason. Sigh. (Rob) */ |
| 927 | /* totsize++; */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 928 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 929 | renumber(inptr); |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 930 | edit_update(edittop, TOP); |
Adam Rogoyski | 0223d6f | 2000-06-17 20:36:35 +0000 | [diff] [blame] | 931 | |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 932 | |
| 933 | /* Move the cursor to the new line if appropriate. */ |
| 934 | if (down) { |
| 935 | do_right(); |
| 936 | } |
| 937 | |
| 938 | /* Move the cursor to the correct spot in the line if appropriate. */ |
| 939 | while (right--) { |
| 940 | do_right(); |
| 941 | } |
| 942 | |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 943 | edit_update(edittop, TOP); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 944 | reset_cursor(); |
| 945 | edit_refresh(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /* Check to see if we've just caused the line to wrap to a new line */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 949 | void check_wrap(filestruct * inptr, char ch) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 950 | { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 951 | int len = strlenpt(inptr->data); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 952 | #ifdef DEBUG |
| 953 | fprintf(stderr, _("check_wrap called with inptr->data=\"%s\"\n"), |
| 954 | inptr->data); |
| 955 | #endif |
| 956 | |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 957 | if (len <= fill) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 958 | return; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 959 | else { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 960 | int i = actual_x(inptr, fill); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 961 | |
| 962 | /* Do not wrap if there are no words on or after wrap point. */ |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 963 | int char_found = 0; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 964 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 965 | while (isspace((int) inptr->data[i]) && inptr->data[i]) |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 966 | i++; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 967 | |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 968 | if (!inptr->data[i]) |
| 969 | return; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 970 | |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 971 | /* String must be at least 1 character long. */ |
| 972 | for (i = strlen(inptr->data) - 1; i >= 0; i--) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 973 | if (isspace((int) inptr->data[i])) { |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 974 | if (!char_found) |
| 975 | continue; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 976 | char_found = 2; /* 2 for yes do wrap. */ |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 977 | break; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 978 | } else |
| 979 | char_found = 1; /* 1 for yes found a word, but must check further. */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 980 | } |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 981 | |
| 982 | if (char_found == 2) |
| 983 | do_wrap(inptr, ch); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 984 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /* Stuff we do when we abort from programs and want to clean up the |
| 988 | * screen. This doesnt do much right now. |
| 989 | */ |
| 990 | void do_early_abort(void) |
| 991 | { |
| 992 | blank_statusbar_refresh(); |
| 993 | } |
| 994 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 995 | int do_backspace(void) |
| 996 | { |
| 997 | filestruct *previous, *tmp; |
| 998 | |
| 999 | if (current_x != 0) { |
| 1000 | /* Let's get dangerous */ |
| 1001 | memmove(¤t->data[current_x - 1], ¤t->data[current_x], |
| 1002 | strlen(current->data) - current_x + 1); |
| 1003 | #ifdef DEBUG |
| 1004 | fprintf(stderr, _("current->data now = \"%s\"\n"), current->data); |
| 1005 | #endif |
| 1006 | align(¤t->data); |
| 1007 | do_left(); |
| 1008 | } else { |
| 1009 | if (current == fileage) |
| 1010 | return 0; /* Can't delete past top of file */ |
| 1011 | |
| 1012 | previous = current->prev; |
| 1013 | current_x = strlen(previous->data); |
| 1014 | previous->data = nrealloc(previous->data, |
| 1015 | strlen(previous->data) + |
| 1016 | strlen(current->data) + 1); |
| 1017 | strcat(previous->data, current->data); |
| 1018 | |
| 1019 | tmp = current; |
| 1020 | unlink_node(current); |
| 1021 | delete_node(current); |
| 1022 | if (current == edittop) { |
| 1023 | if (previous->next) |
| 1024 | current = previous->next; |
| 1025 | else |
| 1026 | current = previous; |
Chris Allegretta | da721be | 2000-07-31 01:26:42 +0000 | [diff] [blame] | 1027 | page_up_center(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1028 | } else { |
| 1029 | if (previous->next) |
| 1030 | current = previous->next; |
| 1031 | else |
| 1032 | current = previous; |
| 1033 | update_line(current, current_x); |
| 1034 | } |
| 1035 | |
| 1036 | /* Ooops, sanity check */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1037 | if (tmp == filebot) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1038 | filebot = current; |
| 1039 | editbot = current; |
Chris Allegretta | 28a0f89 | 2000-07-05 22:47:54 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* Recreate the magic line if we're deleting it AND if the |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1042 | line we're on now is NOT blank. if it is blank we |
| 1043 | can just use IT for the magic line. This is how Pico |
| 1044 | appears to do it, in any case */ |
Chris Allegretta | 28a0f89 | 2000-07-05 22:47:54 +0000 | [diff] [blame] | 1045 | if (strcmp(current->data, "")) { |
| 1046 | new_magicline(); |
| 1047 | fix_editbot(); |
| 1048 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | current = previous; |
| 1052 | renumber(current); |
| 1053 | previous_line(); |
| 1054 | totlines--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1055 | #ifdef DEBUG |
| 1056 | fprintf(stderr, _("After, data = \"%s\"\n"), current->data); |
| 1057 | #endif |
| 1058 | |
| 1059 | } |
| 1060 | |
| 1061 | totsize--; |
| 1062 | set_modified(); |
| 1063 | UNSET(KEEP_CUTBUFFER); |
| 1064 | edit_refresh(); |
| 1065 | return 1; |
| 1066 | } |
| 1067 | |
| 1068 | int do_delete(void) |
| 1069 | { |
| 1070 | filestruct *foo; |
| 1071 | |
| 1072 | if (current_x != strlen(current->data)) { |
| 1073 | /* Let's get dangerous */ |
| 1074 | memmove(¤t->data[current_x], ¤t->data[current_x + 1], |
| 1075 | strlen(current->data) - current_x); |
| 1076 | |
| 1077 | align(¤t->data); |
| 1078 | |
| 1079 | } else if (current->next != NULL) { |
| 1080 | current->data = nrealloc(current->data, |
| 1081 | strlen(current->data) + |
| 1082 | strlen(current->next->data) + 1); |
| 1083 | strcat(current->data, current->next->data); |
| 1084 | |
| 1085 | foo = current->next; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1086 | if (filebot == foo) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1087 | filebot = current; |
| 1088 | editbot = current; |
| 1089 | } |
| 1090 | |
| 1091 | unlink_node(foo); |
| 1092 | delete_node(foo); |
| 1093 | update_line(current, current_x); |
| 1094 | |
Chris Allegretta | 28a0f89 | 2000-07-05 22:47:54 +0000 | [diff] [blame] | 1095 | /* Please see the comment in do_basckspace if you don't understand |
| 1096 | this test */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1097 | if (current == filebot && strcmp(current->data, "")) { |
Chris Allegretta | 28a0f89 | 2000-07-05 22:47:54 +0000 | [diff] [blame] | 1098 | new_magicline(); |
| 1099 | fix_editbot(); |
Chris Allegretta | 5537387 | 2000-07-06 22:38:37 +0000 | [diff] [blame] | 1100 | totsize++; |
Chris Allegretta | 28a0f89 | 2000-07-05 22:47:54 +0000 | [diff] [blame] | 1101 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1102 | renumber(current); |
| 1103 | totlines--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1104 | } else |
| 1105 | return 0; |
| 1106 | |
| 1107 | totsize--; |
| 1108 | set_modified(); |
| 1109 | UNSET(KEEP_CUTBUFFER); |
| 1110 | edit_refresh(); |
| 1111 | return 1; |
| 1112 | } |
| 1113 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1114 | void wrap_reset(void) |
| 1115 | { |
| 1116 | UNSET(SAMELINEWRAP); |
| 1117 | } |
| 1118 | |
Chris Allegretta | 7b36c52 | 2000-12-06 01:08:10 +0000 | [diff] [blame] | 1119 | #if !defined(NANO_SMALL) && !defined(DISABLE_SPELLER) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1120 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1121 | int do_int_spell_fix(char *word) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1122 | { |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1123 | char *prevanswer = NULL, *save_search = NULL, *save_replace = NULL; |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1124 | filestruct *begin; |
| 1125 | int i = 0, j = 0, beginx, beginx_top; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1126 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1127 | /* save where we are */ |
| 1128 | begin = current; |
| 1129 | beginx = current_x + 1; |
| 1130 | |
| 1131 | /* save the current search/replace strings */ |
| 1132 | search_init_globals(); |
| 1133 | save_search = mallocstrcpy(save_search, last_search); |
| 1134 | save_replace = mallocstrcpy(save_replace, last_replace); |
| 1135 | |
| 1136 | /* set search/replace strings to mis-spelt word */ |
| 1137 | prevanswer = mallocstrcpy(prevanswer, word); |
| 1138 | last_search = mallocstrcpy(last_search, word); |
| 1139 | last_replace = mallocstrcpy(last_replace, word); |
| 1140 | |
| 1141 | /* start from the top of file */ |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1142 | current = fileage; |
| 1143 | current_x = beginx_top = -1; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1144 | |
| 1145 | search_last_line = FALSE; |
| 1146 | |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1147 | edit_update(fileage, TOP); |
| 1148 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1149 | /* make sure word is still mis-spelt (i.e. when multi-errors) */ |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1150 | if (findnextstr(TRUE, fileage, beginx_top, prevanswer) != NULL) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1151 | { |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1152 | do_replace_highlight(TRUE, prevanswer); |
| 1153 | |
| 1154 | /* allow replace word to be corrected */ |
| 1155 | i = statusq(0, spell_list, SPELL_LIST_LEN, last_replace, |
| 1156 | _("Edit a replacement")); |
| 1157 | |
| 1158 | do_replace_highlight(FALSE, prevanswer); |
| 1159 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1160 | /* start from the start of this line again */ |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1161 | current = fileage; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1162 | current_x = beginx_top; |
| 1163 | |
| 1164 | search_last_line = FALSE; |
| 1165 | |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1166 | j = i; |
| 1167 | do_replace_loop(prevanswer, fileage, &beginx_top, TRUE, &j); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1168 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1169 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1170 | /* restore the search/replace strings */ |
| 1171 | last_search = mallocstrcpy(last_search, save_search); |
| 1172 | last_replace = mallocstrcpy(last_replace, save_replace); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1173 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1174 | /* restore where we were */ |
| 1175 | current = begin; |
| 1176 | current_x = beginx - 1; |
| 1177 | |
| 1178 | edit_update(current, CENTER); |
| 1179 | |
| 1180 | if (i == -1) |
| 1181 | return FALSE; |
| 1182 | |
| 1183 | return TRUE; |
| 1184 | } |
| 1185 | #endif |
| 1186 | |
Chris Allegretta | 7b36c52 | 2000-12-06 01:08:10 +0000 | [diff] [blame] | 1187 | #if !defined(NANO_SMALL) && !defined(DISABLE_SPELLER) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* Integrated spell checking using 'spell' program */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1190 | int do_int_speller(char *tempfile_name) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1191 | { |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1192 | char *read_buff, *read_buff_ptr, *read_buff_word; |
| 1193 | long pipe_buff_size; |
| 1194 | int in_fd[2], tempfile_fd; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1195 | int spell_status; |
| 1196 | pid_t pid_spell; |
| 1197 | ssize_t bytesread; |
| 1198 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1199 | /* Create a pipe to spell program */ |
| 1200 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1201 | if (pipe(in_fd) == -1) |
| 1202 | return FALSE; |
| 1203 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1204 | /* A new process to run spell in */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1205 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1206 | if ( (pid_spell = fork()) == 0) { |
| 1207 | |
| 1208 | /* Child continues, (i.e. future spell process) */ |
| 1209 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1210 | close(in_fd[0]); |
| 1211 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1212 | /* replace the standard in with the tempfile */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1213 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1214 | if ( (tempfile_fd = open(tempfile_name, O_RDONLY)) == -1) { |
| 1215 | |
| 1216 | close(in_fd[1]); |
| 1217 | exit(1); |
| 1218 | } |
| 1219 | |
| 1220 | if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) { |
| 1221 | |
| 1222 | close(tempfile_fd); |
| 1223 | close(in_fd[1]); |
| 1224 | exit(1); |
| 1225 | } |
| 1226 | close(tempfile_fd); |
| 1227 | |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1228 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1229 | /* send spell's standard out to the pipe */ |
| 1230 | |
| 1231 | if (dup2(in_fd[1], STDOUT_FILENO) != STDOUT_FILENO) { |
| 1232 | |
| 1233 | close(in_fd[1]); |
| 1234 | exit(1); |
| 1235 | } |
| 1236 | close(in_fd[1]); |
| 1237 | |
| 1238 | /* Start spell program, we are using the PATH here!?!? */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1239 | execlp("spell", "spell", NULL); |
| 1240 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1241 | /* Should not be reached, if spell is found!!! */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1242 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1243 | exit(1); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /* Parent continues here */ |
| 1247 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1248 | close(in_fd[1]); |
| 1249 | |
| 1250 | /* Child process was not forked successfully */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1251 | |
| 1252 | if (pid_spell < 0) { |
| 1253 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1254 | close(in_fd[0]); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1255 | return FALSE; |
| 1256 | } |
| 1257 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1258 | /* Get system pipe buffer size */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1259 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1260 | if ( (pipe_buff_size = fpathconf(in_fd[0], _PC_PIPE_BUF)) < 1) { |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1261 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1262 | close(in_fd[0]); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1263 | return FALSE; |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1264 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1265 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1266 | read_buff = nmalloc( pipe_buff_size + 1 ); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1267 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1268 | /* Process the returned spelling errors */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1269 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1270 | while ( (bytesread = read(in_fd[0], read_buff, pipe_buff_size)) > 0) { |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1271 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1272 | read_buff[bytesread] = (char) NULL; |
| 1273 | read_buff_word = read_buff_ptr = read_buff; |
| 1274 | |
| 1275 | while (*read_buff_ptr != (char) NULL) { |
| 1276 | |
| 1277 | /* Windows version may need to process additional char '\r' */ |
| 1278 | |
| 1279 | /* Possible problem here if last word not followed by '\n' */ |
| 1280 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1281 | if (*read_buff_ptr == '\n') { |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1282 | *read_buff_ptr = (char) NULL; |
| 1283 | if (!do_int_spell_fix(read_buff_word)) { |
| 1284 | |
| 1285 | close(in_fd[0]); |
| 1286 | free(read_buff); |
| 1287 | replace_abort(); |
| 1288 | |
| 1289 | return TRUE; |
| 1290 | } |
| 1291 | read_buff_word = read_buff_ptr; |
| 1292 | read_buff_word++; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | read_buff_ptr++; |
| 1296 | } |
| 1297 | } |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1298 | |
| 1299 | close(in_fd[0]); |
| 1300 | free(read_buff); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1301 | replace_abort(); |
| 1302 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1303 | /* Process end of spell process */ |
| 1304 | |
| 1305 | wait(&spell_status); |
| 1306 | if (WIFEXITED(spell_status)) { |
| 1307 | if (WEXITSTATUS(spell_status) != 0) |
| 1308 | return FALSE; |
| 1309 | } |
| 1310 | else |
| 1311 | return FALSE; |
| 1312 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1313 | return TRUE; |
| 1314 | } |
| 1315 | #endif |
| 1316 | |
Chris Allegretta | 7b36c52 | 2000-12-06 01:08:10 +0000 | [diff] [blame] | 1317 | #if !defined(NANO_SMALL) && !defined(DISABLE_SPELLER) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1318 | |
| 1319 | /* External spell checking */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1320 | int do_alt_speller(char *file_name) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1321 | { |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1322 | int alt_spell_status; |
| 1323 | pid_t pid_spell; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1324 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1325 | endwin(); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1326 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1327 | /* Start a new process for the alternate speller */ |
| 1328 | |
| 1329 | if ( (pid_spell = fork()) == 0) { |
| 1330 | |
| 1331 | /* Start alternate spell program, we are using the PATH here!?!? */ |
| 1332 | execlp(alt_speller, alt_speller, file_name, NULL); |
| 1333 | |
| 1334 | /* Should not be reached, if alternate speller is found!!! */ |
| 1335 | |
| 1336 | exit(1); |
| 1337 | } |
| 1338 | |
| 1339 | /* Could not fork?? */ |
| 1340 | |
| 1341 | if (pid_spell < 0) |
| 1342 | return FALSE; |
| 1343 | |
| 1344 | /* Wait for alternate speller to complete */ |
| 1345 | |
| 1346 | wait(&alt_spell_status); |
| 1347 | if (WIFEXITED(alt_spell_status)) { |
| 1348 | if (WEXITSTATUS(alt_spell_status) != 0) |
| 1349 | return FALSE; |
| 1350 | } |
| 1351 | else |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1352 | return FALSE; |
| 1353 | |
Chris Allegretta | 8f6c069 | 2000-07-19 01:16:18 +0000 | [diff] [blame] | 1354 | refresh(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1355 | free_filestruct(fileage); |
| 1356 | global_init(); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1357 | open_file(file_name, 0, 1); |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 1358 | edit_update(fileage, CENTER); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1359 | display_main_list(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1360 | set_modified(); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1361 | |
| 1362 | return TRUE; |
| 1363 | } |
| 1364 | #endif |
| 1365 | |
| 1366 | int do_spell(void) |
| 1367 | { |
| 1368 | |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 1369 | #if defined(NANO_SMALL) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1370 | nano_small_msg(); |
| 1371 | return (TRUE); |
Chris Allegretta | 7b36c52 | 2000-12-06 01:08:10 +0000 | [diff] [blame] | 1372 | #elif defined(DISABLE_SPELLER) |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 1373 | nano_disabled_msg(); |
| 1374 | return (TRUE); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1375 | #else |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1376 | char *temp; |
| 1377 | int spell_res; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1378 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1379 | if ((temp = tempnam(0, "nano.")) == NULL) { |
| 1380 | statusbar(_("Could not create a temporary filename: %s"), |
| 1381 | strerror(errno)); |
| 1382 | return 0; |
| 1383 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1384 | |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 1385 | if (write_file(temp, 1) == -1) { |
| 1386 | statusbar(_("Spell checking failed: unable to write temp file!")); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1387 | return 0; |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 1388 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1389 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1390 | if (alt_speller) |
| 1391 | spell_res = do_alt_speller(temp); |
| 1392 | else |
| 1393 | spell_res = do_int_speller(temp); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1394 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1395 | remove(temp); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (spell_res) |
| 1398 | statusbar(_("Finished checking spelling")); |
| 1399 | else |
| 1400 | statusbar(_("Spell checking failed")); |
| 1401 | |
| 1402 | return spell_res; |
| 1403 | |
Chris Allegretta | dbc12b2 | 2000-07-03 03:10:14 +0000 | [diff] [blame] | 1404 | #endif |
Chris Allegretta | 67105eb | 2000-07-03 03:18:32 +0000 | [diff] [blame] | 1405 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1406 | |
| 1407 | int do_exit(void) |
| 1408 | { |
| 1409 | int i; |
| 1410 | |
| 1411 | if (!ISSET(MODIFIED)) |
| 1412 | finish(0); |
| 1413 | |
Chris Allegretta | 3088555 | 2000-07-14 01:20:12 +0000 | [diff] [blame] | 1414 | if (ISSET(TEMP_OPT)) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1415 | i = 1; |
| 1416 | } else { |
| 1417 | i = |
| 1418 | do_yesno(0, 0, |
| 1419 | _ |
| 1420 | ("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES) ? ")); |
| 1421 | } |
| 1422 | |
| 1423 | #ifdef DEBUG |
| 1424 | dump_buffer(fileage); |
| 1425 | #endif |
| 1426 | |
| 1427 | if (i == 1) { |
| 1428 | if (do_writeout(1) > 0) |
| 1429 | finish(0); |
| 1430 | } else if (i == 0) |
| 1431 | finish(0); |
| 1432 | else |
| 1433 | statusbar(_("Cancelled")); |
| 1434 | |
| 1435 | display_main_list(); |
| 1436 | return 1; |
| 1437 | } |
| 1438 | |
| 1439 | #ifndef NANO_SMALL |
| 1440 | #ifdef NCURSES_MOUSE_VERSION |
| 1441 | void do_mouse(void) |
| 1442 | { |
| 1443 | MEVENT mevent; |
Chris Allegretta | e10debd | 2000-08-22 01:26:42 +0000 | [diff] [blame] | 1444 | int foo = 0, tab_found = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1445 | |
| 1446 | if (getmouse(&mevent) == ERR) |
| 1447 | return; |
| 1448 | |
| 1449 | /* If mouse not in edit window, return (add help selection later). */ |
| 1450 | if (!wenclose(edit, mevent.y, mevent.x)) |
| 1451 | return; |
| 1452 | |
| 1453 | /* Subtract out size of topwin. Perhaps we need a constant somewhere? */ |
| 1454 | mevent.y -= 2; |
| 1455 | |
| 1456 | /* Selecting where the cursor is sets the mark. |
| 1457 | * Selecting beyond the line length with the cursor at the end of the |
| 1458 | * line sets the mark as well. |
| 1459 | */ |
| 1460 | if ((mevent.y == current_y) && |
| 1461 | ((mevent.x == current_x) || (current_x == strlen(current->data) |
| 1462 | && (mevent.x > |
| 1463 | strlen(current->data))))) { |
| 1464 | if (ISSET(VIEW_MODE)) { |
| 1465 | print_view_warning(); |
| 1466 | return; |
| 1467 | } |
| 1468 | do_mark(); |
| 1469 | } else if (mevent.y > current_y) { |
| 1470 | while (mevent.y > current_y) { |
| 1471 | if (current->next != NULL) |
| 1472 | current = current->next; |
| 1473 | else |
| 1474 | break; |
| 1475 | current_y++; |
| 1476 | } |
| 1477 | } else if (mevent.y < current_y) { |
| 1478 | while (mevent.y < current_y) { |
| 1479 | if (current->prev != NULL) |
| 1480 | current = current->prev; |
| 1481 | else |
| 1482 | break; |
| 1483 | current_y--; |
| 1484 | } |
| 1485 | } |
| 1486 | current_x = mevent.x; |
Chris Allegretta | e10debd | 2000-08-22 01:26:42 +0000 | [diff] [blame] | 1487 | placewewant = current_x; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1488 | while (foo < current_x) { |
| 1489 | if (current->data[foo] == NANO_CONTROL_I) { |
Chris Allegretta | e10debd | 2000-08-22 01:26:42 +0000 | [diff] [blame] | 1490 | current_x -= tabsize - (foo % tabsize); |
| 1491 | tab_found = 1; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1492 | } else if (current->data[foo] & 0x80); |
| 1493 | else if (current->data[foo] < 32) |
Chris Allegretta | e10debd | 2000-08-22 01:26:42 +0000 | [diff] [blame] | 1494 | current_x--; |
| 1495 | foo++; |
| 1496 | } |
| 1497 | /* This is where tab_found comes in. I can't figure out why, |
| 1498 | * but without it any line with a tab will place the cursor |
| 1499 | * one character behind. Whatever, this fixes it. */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1500 | if (tab_found == 1) |
Chris Allegretta | e10debd | 2000-08-22 01:26:42 +0000 | [diff] [blame] | 1501 | current_x++; |
| 1502 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1503 | if (current_x > strlen(current->data)) |
| 1504 | current_x = strlen(current->data); |
| 1505 | |
| 1506 | update_cursor(); |
| 1507 | edit_refresh(); |
| 1508 | |
| 1509 | } |
| 1510 | #endif |
| 1511 | #endif |
| 1512 | |
| 1513 | /* Handler for SIGHUP */ |
| 1514 | RETSIGTYPE handle_hup(int signal) |
| 1515 | { |
Chris Allegretta | e7a5893 | 2000-12-02 02:36:22 +0000 | [diff] [blame] | 1516 | die(_("Received SIGHUP")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Chris Allegretta | 18f8be0 | 2000-09-04 03:20:38 +0000 | [diff] [blame] | 1519 | /* What do we do when we catch the suspend signal */ |
| 1520 | RETSIGTYPE do_suspend(int signal) |
| 1521 | { |
| 1522 | |
| 1523 | act.sa_handler = SIG_DFL; |
| 1524 | sigemptyset(&act.sa_mask); |
| 1525 | sigaction(SIGTSTP, &act, NULL); |
| 1526 | |
| 1527 | endwin(); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1528 | fprintf(stderr, "\n\n\n\n\nUse \"fg\" to return to nano\n"); |
Chris Allegretta | 18f8be0 | 2000-09-04 03:20:38 +0000 | [diff] [blame] | 1529 | raise(SIGTSTP); |
| 1530 | } |
| 1531 | |
| 1532 | /* Restore the suspend handler when we come back into the prog */ |
| 1533 | RETSIGTYPE do_cont(int signal) |
| 1534 | { |
| 1535 | |
| 1536 | act.sa_handler = do_suspend; |
| 1537 | sigemptyset(&act.sa_mask); |
| 1538 | sigaction(SIGTSTP, &act, NULL); |
| 1539 | initscr(); |
| 1540 | total_refresh(); |
| 1541 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1542 | |
| 1543 | void handle_sigwinch(int s) |
| 1544 | { |
| 1545 | #ifndef NANO_SMALL |
| 1546 | char *tty = NULL; |
| 1547 | int fd = 0; |
| 1548 | int result = 0; |
| 1549 | int i = 0; |
| 1550 | struct winsize win; |
| 1551 | |
| 1552 | tty = ttyname(0); |
| 1553 | if (!tty) |
| 1554 | return; |
| 1555 | fd = open(tty, O_RDWR); |
| 1556 | if (fd == -1) |
| 1557 | return; |
| 1558 | result = ioctl(fd, TIOCGWINSZ, &win); |
| 1559 | if (result == -1) |
| 1560 | return; |
| 1561 | |
| 1562 | |
| 1563 | COLS = win.ws_col; |
| 1564 | LINES = win.ws_row; |
| 1565 | |
| 1566 | center_x = COLS / 2; |
| 1567 | center_y = LINES / 2; |
| 1568 | editwinrows = LINES - 5 + no_help(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1569 | fill = COLS - 8; |
| 1570 | |
| 1571 | free(hblank); |
| 1572 | hblank = nmalloc(COLS + 1); |
| 1573 | |
| 1574 | for (i = 0; i <= COLS - 1; i++) |
| 1575 | hblank[i] = ' '; |
| 1576 | hblank[i] = 0; |
| 1577 | |
| 1578 | #ifdef HAVE_NCURSES_H |
| 1579 | resizeterm(LINES, COLS); |
| 1580 | #ifdef HAVE_WRESIZE |
| 1581 | if (wresize(topwin, 2, COLS) == ERR) |
| 1582 | die(_("Cannot resize top win")); |
| 1583 | if (mvwin(topwin, 0, 0) == ERR) |
| 1584 | die(_("Cannot move top win")); |
| 1585 | if (wresize(edit, editwinrows, COLS) == ERR) |
| 1586 | die(_("Cannot resize edit win")); |
| 1587 | if (mvwin(edit, 2, 0) == ERR) |
| 1588 | die(_("Cannot move edit win")); |
| 1589 | if (wresize(bottomwin, 3 - no_help(), COLS) == ERR) |
| 1590 | die(_("Cannot resize bottom win")); |
| 1591 | if (mvwin(bottomwin, LINES - 3 + no_help(), 0) == ERR) |
| 1592 | die(_("Cannot move bottom win")); |
| 1593 | #endif /* HAVE_WRESIZE */ |
| 1594 | #endif /* HAVE_NCURSES_H */ |
| 1595 | |
Robert Siemborski | dd53ec2 | 2000-07-04 02:35:19 +0000 | [diff] [blame] | 1596 | fix_editbot(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1597 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1598 | if (current_y > editwinrows - 1) { |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 1599 | edit_update(editbot, CENTER); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1600 | } |
| 1601 | erase(); |
Chris Allegretta | 97accc6 | 2000-06-19 05:45:52 +0000 | [diff] [blame] | 1602 | |
| 1603 | /* Do these b/c width may have changed... */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1604 | refresh(); |
Chris Allegretta | 97accc6 | 2000-06-19 05:45:52 +0000 | [diff] [blame] | 1605 | titlebar(); |
| 1606 | edit_refresh(); |
| 1607 | display_main_list(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1608 | total_refresh(); |
| 1609 | #endif |
| 1610 | } |
| 1611 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1612 | void signal_init(void) |
| 1613 | { |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1614 | |
| 1615 | /* Trap SIGINT and SIGQUIT cuz we want them to do useful things. */ |
| 1616 | memset(&act, 0, sizeof(struct sigaction)); |
| 1617 | act.sa_handler = SIG_IGN; |
| 1618 | sigaction(SIGINT, &act, NULL); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1619 | |
Chris Allegretta | 18f8be0 | 2000-09-04 03:20:38 +0000 | [diff] [blame] | 1620 | if (!ISSET(SUSPEND)) { |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1621 | sigaction(SIGTSTP, &act, NULL); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1622 | } else { |
Chris Allegretta | 18f8be0 | 2000-09-04 03:20:38 +0000 | [diff] [blame] | 1623 | act.sa_handler = do_suspend; |
| 1624 | sigaction(SIGTSTP, &act, NULL); |
| 1625 | |
| 1626 | act.sa_handler = do_cont; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1627 | sigaction(SIGCONT, &act, NULL); |
Chris Allegretta | 18f8be0 | 2000-09-04 03:20:38 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1630 | |
| 1631 | /* Trap SIGHUP cuz we want to write the file out. */ |
| 1632 | act.sa_handler = handle_hup; |
| 1633 | sigaction(SIGHUP, &act, NULL); |
| 1634 | |
| 1635 | act.sa_handler = handle_sigwinch; |
| 1636 | sigaction(SIGWINCH, &act, NULL); |
| 1637 | |
| 1638 | } |
| 1639 | |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 1640 | void window_init(void) |
| 1641 | { |
Chris Allegretta | 92c9dd2 | 2000-09-13 14:03:27 +0000 | [diff] [blame] | 1642 | editwinrows = LINES - 5 + no_help(); |
| 1643 | |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 1644 | /* Setup up the main text window */ |
| 1645 | edit = newwin(editwinrows, COLS, 2, 0); |
| 1646 | |
| 1647 | /* And the other windows */ |
| 1648 | topwin = newwin(2, COLS, 0, 0); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1649 | bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0); |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1652 | void mouse_init(void) |
| 1653 | { |
| 1654 | #ifndef NANO_SMALL |
| 1655 | #ifdef NCURSES_MOUSE_VERSION |
| 1656 | if (ISSET(USE_MOUSE)) { |
Chris Allegretta | 1cc0b7f | 2000-11-03 01:29:04 +0000 | [diff] [blame] | 1657 | keypad_on(TRUE); |
Chris Allegretta | 0b88ce0 | 2000-09-15 15:46:32 +0000 | [diff] [blame] | 1658 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1659 | mousemask(BUTTON1_RELEASED, NULL); |
| 1660 | mouseinterval(50); |
Chris Allegretta | 0b88ce0 | 2000-09-15 15:46:32 +0000 | [diff] [blame] | 1661 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1662 | } else { |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1663 | mousemask(0, NULL); |
Chris Allegretta | 1cc0b7f | 2000-11-03 01:29:04 +0000 | [diff] [blame] | 1664 | keypad_on(FALSE); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1665 | } |
| 1666 | #endif |
| 1667 | #endif |
| 1668 | |
| 1669 | } |
| 1670 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1671 | int do_tab(void) |
| 1672 | { |
| 1673 | do_char('\t'); |
| 1674 | return 1; |
| 1675 | } |
| 1676 | |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 1677 | #if !defined(NANO_SMALL) && !defined(DISABLE_JUSTIFY) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1678 | int empty_line(const char *data) |
| 1679 | { |
| 1680 | while (*data) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 1681 | if (!isspace((int) *data)) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1682 | return 0; |
| 1683 | |
| 1684 | data++; |
| 1685 | } |
| 1686 | |
| 1687 | return 1; |
| 1688 | } |
| 1689 | |
| 1690 | int no_spaces(const char *data) |
| 1691 | { |
| 1692 | while (*data) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 1693 | if (isspace((int) *data)) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1694 | return 0; |
| 1695 | |
| 1696 | data++; |
| 1697 | } |
| 1698 | |
| 1699 | return 1; |
| 1700 | } |
| 1701 | |
| 1702 | void justify_format(char *data) |
| 1703 | { |
| 1704 | int i = 0; |
| 1705 | int len = strlen(data); |
| 1706 | |
| 1707 | /* Skip first character regardless and leading whitespace. */ |
| 1708 | for (i = 1; i < len; i++) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 1709 | if (!isspace((int) data[i])) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1710 | break; |
| 1711 | } |
| 1712 | |
| 1713 | i++; /* (i) is now at least 2. */ |
| 1714 | |
| 1715 | /* No double spaces allowed unless following a period. Tabs -> space. No double tabs. */ |
| 1716 | for (; i < len; i++) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 1717 | if (isspace((int) data[i]) && isspace((int) data[i - 1]) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1718 | && (data[i - 2] != '.')) { |
| 1719 | memmove(data + i, data + i + 1, len - i); |
| 1720 | len--; |
| 1721 | i--; |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | #endif |
| 1726 | |
| 1727 | int do_justify(void) |
| 1728 | { |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 1729 | #ifdef NANO_SMALL |
| 1730 | nano_small_msg(); |
| 1731 | return 1; |
| 1732 | #elif defined(DISABLE_JUSTIFY) |
| 1733 | nano_disabled_msg(); |
| 1734 | return 1; |
| 1735 | #else |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1736 | int slen = 0; /* length of combined lines on one line. */ |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1737 | int initial_y, kbinput; |
| 1738 | filestruct *initial = NULL, *tmpjust = NULL, *cutbak, *tmptop, *tmpbot; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1739 | |
| 1740 | if (empty_line(current->data)) { |
| 1741 | /* Justify starting at first non-empty line. */ |
| 1742 | do { |
| 1743 | if (!current->next) |
| 1744 | return 1; |
| 1745 | |
| 1746 | current = current->next; |
| 1747 | current_y++; |
| 1748 | } |
| 1749 | while (empty_line(current->data)); |
| 1750 | } else { |
| 1751 | /* Search back for the beginning of the paragraph, where |
| 1752 | * Paragraph is 1) A line with leading whitespace |
| 1753 | * or 2) A line following an empty line. |
| 1754 | */ |
| 1755 | while (current->prev != NULL) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 1756 | if (isspace((int) current->data[0]) || !current->data[0]) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1757 | break; |
| 1758 | |
| 1759 | current = current->prev; |
| 1760 | current_y--; |
| 1761 | } |
| 1762 | |
| 1763 | /* First line with leading whitespace may be empty. */ |
| 1764 | if (empty_line(current->data)) { |
| 1765 | if (current->next) { |
| 1766 | current = current->next; |
| 1767 | current_y++; |
| 1768 | } else |
| 1769 | return 1; |
| 1770 | } |
| 1771 | } |
| 1772 | initial = current; |
| 1773 | initial_y = current_y; |
| 1774 | |
| 1775 | set_modified(); |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1776 | cutbak = cutbuffer; /* Got to like cutbak ;) */ |
| 1777 | cutbuffer = NULL; |
| 1778 | |
| 1779 | tmptop = current; |
| 1780 | tmpjust = copy_node(current); |
| 1781 | add_to_cutbuffer(tmpjust); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1782 | /* Put the whole paragraph into one big line. */ |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 1783 | while (current->next && !isspace((int) current->next->data[0]) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1784 | && current->next->data[0]) { |
| 1785 | filestruct *tmpnode = current->next; |
| 1786 | int len = strlen(current->data); |
| 1787 | int len2 = strlen(current->next->data); |
| 1788 | |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1789 | tmpjust = NULL; |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1790 | tmpjust = copy_node(current->next); |
| 1791 | add_to_cutbuffer(tmpjust); |
| 1792 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1793 | /* length of both strings plus space between strings and ending \0. */ |
| 1794 | current->data = nrealloc(current->data, len + len2 + 2); |
| 1795 | current->data[len++] = ' '; |
| 1796 | current->data[len] = '\0'; |
| 1797 | |
| 1798 | strncat(current->data, current->next->data, len2); |
| 1799 | |
| 1800 | unlink_node(tmpnode); |
| 1801 | delete_node(tmpnode); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1804 | totsize -= strlen(current->data); |
| 1805 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1806 | justify_format(current->data); |
| 1807 | |
| 1808 | slen = strlen(current->data); |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1809 | totsize += slen; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1810 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1811 | if ((strlenpt(current->data) > (fill)) |
| 1812 | && !no_spaces(current->data)) { |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1813 | do { |
| 1814 | int i = 0; |
| 1815 | int len2 = 0; |
| 1816 | filestruct *tmpline = nmalloc(sizeof(filestruct)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1817 | |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1818 | /* Start at fill , unless line isn't that long (but it |
| 1819 | * appears at least fill long with tabs. |
| 1820 | */ |
| 1821 | if (slen > fill) |
| 1822 | i = fill; |
| 1823 | else |
| 1824 | i = slen; |
| 1825 | for (; i > 0; i--) { |
Chris Allegretta | 9e7efa3 | 2000-10-02 03:42:55 +0000 | [diff] [blame] | 1826 | if (isspace((int) current->data[i]) && |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1827 | ((strlenpt(current->data) - strlen(current->data + i)) |
| 1828 | <= fill)) |
| 1829 | break; |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1830 | } |
| 1831 | if (!i) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1832 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1833 | |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1834 | current->data[i] = '\0'; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1835 | |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1836 | len2 = strlen(current->data + i + 1); |
| 1837 | tmpline->data = nmalloc(len2 + 1); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1838 | |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1839 | /* Skip the white space in current. */ |
| 1840 | memcpy(tmpline->data, current->data + i + 1, len2); |
| 1841 | tmpline->data[len2] = '\0'; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1842 | |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1843 | current->data = nrealloc(current->data, i + 1); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1844 | |
Robert Siemborski | a417ddc | 2000-07-24 23:18:48 +0000 | [diff] [blame] | 1845 | tmpline->prev = current; |
| 1846 | tmpline->next = current->next; |
| 1847 | if (current->next != NULL) |
| 1848 | current->next->prev = tmpline; |
| 1849 | |
| 1850 | current->next = tmpline; |
| 1851 | current = tmpline; |
| 1852 | slen -= i + 1; |
| 1853 | current_y++; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1854 | } while ((strlenpt(current->data) > (fill)) |
| 1855 | && !no_spaces(current->data)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1856 | } |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1857 | tmpbot = current; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1858 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1859 | if (current->next) |
| 1860 | current = current->next; |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 1861 | else |
| 1862 | filebot = current; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1863 | current_x = 0; |
| 1864 | placewewant = 0; |
| 1865 | |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 1866 | renumber(initial); |
| 1867 | totlines = filebot->lineno; |
| 1868 | |
| 1869 | werase(edit); |
| 1870 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1871 | if ((current_y < 0) || (current_y >= editwinrows - 1) |
| 1872 | || (initial_y <= 0)) { |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 1873 | edit_update(current, CENTER); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1874 | center_cursor(); |
| 1875 | } else { |
Robert Siemborski | dd53ec2 | 2000-07-04 02:35:19 +0000 | [diff] [blame] | 1876 | fix_editbot(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Adam Rogoyski | 09f9796 | 2000-06-20 02:50:33 +0000 | [diff] [blame] | 1879 | edit_refresh(); |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1880 | statusbar(_("Can now UnJustify!")); |
Chris Allegretta | 0779835 | 2000-11-27 22:58:23 +0000 | [diff] [blame] | 1881 | /* Change the shortcut list to display the unjustify code */ |
| 1882 | shortcut_init(1); |
| 1883 | display_main_list(); |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1884 | reset_cursor(); |
| 1885 | |
Chris Allegretta | 0779835 | 2000-11-27 22:58:23 +0000 | [diff] [blame] | 1886 | /* Now get a keystroke and see if it's unjustify, if not unget the keytroke |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1887 | and return */ |
Chris Allegretta | 4a9c858 | 2000-11-27 22:59:40 +0000 | [diff] [blame] | 1888 | if ((kbinput = wgetch(edit)) != NANO_UNJUSTIFY_KEY) |
Chris Allegretta | 0779835 | 2000-11-27 22:58:23 +0000 | [diff] [blame] | 1889 | ungetch(kbinput); |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1890 | else { |
| 1891 | /* Else restore the justify we just did (ungrateful user!) */ |
| 1892 | if (tmptop->prev != NULL) |
| 1893 | tmptop->prev->next = tmpbot->next; |
Chris Allegretta | d022eac | 2000-11-27 02:50:49 +0000 | [diff] [blame] | 1894 | else |
| 1895 | fileage = current; |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1896 | tmpbot->next->prev = tmptop->prev; |
| 1897 | current = tmpbot->next; |
| 1898 | tmpbot->next = NULL; |
| 1899 | do_uncut_text(); |
Chris Allegretta | d022eac | 2000-11-27 02:50:49 +0000 | [diff] [blame] | 1900 | if (tmptop->prev == NULL) |
| 1901 | edit_refresh(); |
| 1902 | |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1903 | free_filestruct(tmptop); |
| 1904 | blank_statusbar_refresh(); |
| 1905 | } |
Chris Allegretta | 4a9c858 | 2000-11-27 22:59:40 +0000 | [diff] [blame] | 1906 | shortcut_init(0); |
| 1907 | display_main_list(); |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 1908 | free_filestruct(cutbuffer); |
| 1909 | cutbuffer = cutbak; |
| 1910 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1911 | return 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1912 | #endif |
| 1913 | } |
| 1914 | |
Chris Allegretta | 3bc8c72 | 2000-12-10 17:03:25 +0000 | [diff] [blame] | 1915 | #if !defined(NANO_SMALL) && !defined(DISABLE_HELP) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1916 | void help_init(void) |
| 1917 | { |
| 1918 | int i, sofar = 0; |
| 1919 | long allocsize = 1; /* How much space we're gonna need for the help text */ |
Chris Allegretta | 220ba69 | 2000-12-18 03:40:00 +0000 | [diff] [blame] | 1920 | char buf[BUFSIZ] = ""; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1921 | |
| 1922 | /* Compute the space needed for the shortcut lists - we add 15 to |
| 1923 | have room for the shortcut abbrev and its possible alternate keys */ |
Chris Allegretta | 220ba69 | 2000-12-18 03:40:00 +0000 | [diff] [blame] | 1924 | for (i = 0; i <= MAIN_LIST_LEN - 1; i++) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1925 | if (main_list[i].help != NULL) |
| 1926 | allocsize += strlen(main_list[i].help) + 15; |
| 1927 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1928 | /* And for the toggle list, we also allocate space for extra text. */ |
Chris Allegretta | 423cbfd | 2000-09-04 16:21:29 +0000 | [diff] [blame] | 1929 | for (i = 0; i <= TOGGLE_LEN - 1; i++) |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1930 | if (toggles[i].desc != NULL) |
| 1931 | allocsize += strlen(toggles[i].desc) + 30; |
| 1932 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1933 | allocsize += strlen(help_text_init); |
| 1934 | |
| 1935 | if (help_text != NULL) |
| 1936 | free(help_text); |
| 1937 | |
| 1938 | /* Allocate space for the help text */ |
| 1939 | help_text = nmalloc(allocsize); |
| 1940 | |
| 1941 | /* Now add the text we want */ |
| 1942 | strcpy(help_text, help_text_init); |
| 1943 | |
| 1944 | /* Now add our shortcut info */ |
Chris Allegretta | 220ba69 | 2000-12-18 03:40:00 +0000 | [diff] [blame] | 1945 | for (i = 0; i <= MAIN_LIST_LEN - 1; i++) { |
Robert Siemborski | 6af1431 | 2000-07-01 21:34:26 +0000 | [diff] [blame] | 1946 | sofar = snprintf(buf, BUFSIZ, "^%c ", main_list[i].val + 64); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1947 | |
| 1948 | if (main_list[i].misc1 > KEY_F0 && main_list[i].misc1 <= KEY_F(64)) |
Robert Siemborski | 6af1431 | 2000-07-01 21:34:26 +0000 | [diff] [blame] | 1949 | sofar += snprintf(&buf[sofar], BUFSIZ - sofar, "(F%d) ", |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1950 | main_list[i].misc1 - KEY_F0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1951 | else |
Robert Siemborski | 6af1431 | 2000-07-01 21:34:26 +0000 | [diff] [blame] | 1952 | sofar += snprintf(&buf[sofar], BUFSIZ - sofar, " "); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1953 | |
| 1954 | if (main_list[i].altval > 0) |
Chris Allegretta | e49f123 | 2000-09-02 07:20:39 +0000 | [diff] [blame] | 1955 | sofar += snprintf(&buf[sofar], BUFSIZ - sofar, "(M-%c) ", |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1956 | main_list[i].altval - 32); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1957 | else |
Robert Siemborski | 6af1431 | 2000-07-01 21:34:26 +0000 | [diff] [blame] | 1958 | sofar += snprintf(&buf[sofar], BUFSIZ - sofar, " "); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1959 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1960 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1961 | if (main_list[i].help != NULL) |
Robert Siemborski | 976847c | 2000-07-06 03:43:05 +0000 | [diff] [blame] | 1962 | snprintf(&buf[sofar], BUFSIZ - sofar, "%s", main_list[i].help); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1963 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1964 | |
| 1965 | strcat(help_text, buf); |
| 1966 | strcat(help_text, "\n"); |
| 1967 | } |
| 1968 | |
| 1969 | /* And the toggles... */ |
Chris Allegretta | 423cbfd | 2000-09-04 16:21:29 +0000 | [diff] [blame] | 1970 | for (i = 0; i <= TOGGLE_LEN - 1; i++) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1971 | sofar = snprintf(buf, BUFSIZ, |
| 1972 | "M-%c ", toggles[i].val - 32); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1973 | |
| 1974 | if (toggles[i].desc != NULL) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1975 | snprintf(&buf[sofar], BUFSIZ - sofar, _("%s enable/disable"), |
| 1976 | toggles[i].desc); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1977 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1978 | strcat(help_text, buf); |
Robert Siemborski | 976847c | 2000-07-06 03:43:05 +0000 | [diff] [blame] | 1979 | strcat(help_text, "\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | } |
Chris Allegretta | 3bc8c72 | 2000-12-10 17:03:25 +0000 | [diff] [blame] | 1983 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1984 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1985 | void do_toggle(int which) |
| 1986 | { |
| 1987 | #ifndef NANO_SMALL |
Jordi Mallach | 2dc0f6b | 2000-09-07 10:48:00 +0000 | [diff] [blame] | 1988 | char *enabled = _("enabled"); |
| 1989 | char *disabled = _("disabled"); |
Chris Allegretta | f0f63a8 | 2000-09-02 18:44:21 +0000 | [diff] [blame] | 1990 | |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 1991 | if (ISSET(toggles[which].flag)) |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1992 | UNSET(toggles[which].flag); |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 1993 | else |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1994 | SET(toggles[which].flag); |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 1995 | |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1996 | switch (toggles[which].val) { |
| 1997 | case TOGGLE_PICOMODE_KEY: |
Chris Allegretta | 0779835 | 2000-11-27 22:58:23 +0000 | [diff] [blame] | 1998 | shortcut_init(0); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1999 | display_main_list(); |
| 2000 | break; |
| 2001 | case TOGGLE_SUSPEND_KEY: |
| 2002 | signal_init(); |
| 2003 | break; |
| 2004 | case TOGGLE_MOUSE_KEY: |
| 2005 | mouse_init(); |
| 2006 | break; |
| 2007 | case TOGGLE_NOHELP_KEY: |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2008 | wclear(bottomwin); |
| 2009 | wrefresh(bottomwin); |
| 2010 | window_init(); |
Chris Allegretta | affeda8 | 2000-12-18 04:03:48 +0000 | [diff] [blame] | 2011 | fix_editbot(); |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2012 | edit_refresh(); |
| 2013 | display_main_list(); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2014 | break; |
| 2015 | } |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2016 | |
| 2017 | if (!ISSET(toggles[which].flag)) { |
| 2018 | if (toggles[which].val == TOGGLE_NOHELP_KEY || |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2019 | toggles[which].val == TOGGLE_WRAP_KEY) |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2020 | statusbar("%s %s", toggles[which].desc, enabled); |
| 2021 | else |
| 2022 | statusbar("%s %s", toggles[which].desc, disabled); |
| 2023 | } else { |
| 2024 | if (toggles[which].val == TOGGLE_NOHELP_KEY || |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2025 | toggles[which].val == TOGGLE_WRAP_KEY) |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2026 | statusbar("%s %s", toggles[which].desc, disabled); |
| 2027 | else |
| 2028 | statusbar("%s %s", toggles[which].desc, enabled); |
| 2029 | } |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2030 | SET(DISABLE_CURPOS); |
| 2031 | |
Chris Allegretta | 4c780be | 2000-09-01 17:59:18 +0000 | [diff] [blame] | 2032 | #else |
| 2033 | nano_small_msg(); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2034 | #endif |
| 2035 | } |
| 2036 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2037 | int main(int argc, char *argv[]) |
| 2038 | { |
| 2039 | int optchr; |
| 2040 | int kbinput; /* Input from keyboard */ |
| 2041 | long startline = 0; /* Line to try and start at */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2042 | int keyhandled = 0; /* Have we handled the keystroke yet? */ |
Chris Allegretta | 51b3eec | 2000-12-18 02:23:50 +0000 | [diff] [blame] | 2043 | int i, modify_control_seq = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2044 | char *argv0; |
Chris Allegretta | ad1dacc | 2000-09-21 04:25:45 +0000 | [diff] [blame] | 2045 | #ifdef _POSIX_VDISABLE |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2046 | struct termios term; |
Chris Allegretta | ad1dacc | 2000-09-21 04:25:45 +0000 | [diff] [blame] | 2047 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2048 | |
| 2049 | #ifdef HAVE_GETOPT_LONG |
| 2050 | int option_index = 0; |
| 2051 | struct option long_options[] = { |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 2052 | #ifdef HAVE_REGEX_H |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2053 | {"regexp", 0, 0, 'R'}, |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 2054 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2055 | {"version", 0, 0, 'V'}, |
| 2056 | {"const", 0, 0, 'c'}, |
| 2057 | {"suspend", 0, 0, 'z'}, |
| 2058 | {"nowrap", 0, 0, 'w'}, |
| 2059 | {"nohelp", 0, 0, 'x'}, |
| 2060 | {"help", 0, 0, 'h'}, |
Chris Allegretta | 7492cec | 2000-12-18 04:55:21 +0000 | [diff] [blame] | 2061 | {"view", 0, 0, 'v'}, |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 2062 | #ifndef NANO_SMALL |
Chris Allegretta | 627de19 | 2000-07-12 02:09:17 +0000 | [diff] [blame] | 2063 | {"cut", 0, 0, 'k'}, |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 2064 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2065 | {"autoindent", 0, 0, 'i'}, |
| 2066 | {"tempfile", 0, 0, 't'}, |
| 2067 | {"speller", 1, 0, 's'}, |
| 2068 | {"fill", 1, 0, 'r'}, |
| 2069 | {"mouse", 0, 0, 'm'}, |
| 2070 | {"pico", 0, 0, 'p'}, |
| 2071 | {"nofollow", 0, 0, 'l'}, |
Chris Allegretta | 4dbcc3c | 2000-08-04 15:44:29 +0000 | [diff] [blame] | 2072 | {"tabsize", 1, 0, 'T'}, |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2073 | {0, 0, 0, 0} |
| 2074 | }; |
| 2075 | #endif |
| 2076 | |
| 2077 | /* Flag inits... */ |
| 2078 | SET(FOLLOW_SYMLINKS); |
| 2079 | |
| 2080 | #ifndef NANO_SMALL |
| 2081 | setlocale(LC_ALL, ""); |
| 2082 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 2083 | textdomain(PACKAGE); |
| 2084 | #endif |
| 2085 | |
| 2086 | #ifdef HAVE_GETOPT_LONG |
Chris Allegretta | 51b3eec | 2000-12-18 02:23:50 +0000 | [diff] [blame] | 2087 | while ((optchr = getopt_long(argc, argv, "?T:RVbcefhiklmpr:s:tvwxz", |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2088 | long_options, &option_index)) != EOF) { |
| 2089 | #else |
Chris Allegretta | 51b3eec | 2000-12-18 02:23:50 +0000 | [diff] [blame] | 2090 | while ((optchr = getopt(argc, argv, "h?T:RVbcefiklmpr:s:tvwxz")) != EOF) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2091 | #endif |
| 2092 | |
| 2093 | switch (optchr) { |
Chris Allegretta | 6724a7e | 2000-06-19 23:19:07 +0000 | [diff] [blame] | 2094 | case 'T': |
Chris Allegretta | 99bf73f | 2000-08-04 00:22:08 +0000 | [diff] [blame] | 2095 | tabsize = atoi(optarg); |
| 2096 | if (tabsize <= 0) { |
Chris Allegretta | 6724a7e | 2000-06-19 23:19:07 +0000 | [diff] [blame] | 2097 | usage(); /* To stop bogus data for tab width */ |
| 2098 | finish(1); |
| 2099 | } |
| 2100 | break; |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 2101 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 2102 | case 'R': |
| 2103 | SET(USE_REGEXP); |
| 2104 | break; |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 2105 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2106 | case 'V': |
| 2107 | version(); |
| 2108 | exit(0); |
Chris Allegretta | 51b3eec | 2000-12-18 02:23:50 +0000 | [diff] [blame] | 2109 | case 'b': |
| 2110 | case 'e': |
| 2111 | case 'f': |
| 2112 | /* Pico compatibility flags */ |
| 2113 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2114 | case 'c': |
| 2115 | SET(CONSTUPDATE); |
| 2116 | break; |
| 2117 | case 'h': |
| 2118 | case '?': |
| 2119 | usage(); |
| 2120 | exit(0); |
| 2121 | case 'i': |
| 2122 | SET(AUTOINDENT); |
| 2123 | break; |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 2124 | #ifndef NANO_SMALL |
Chris Allegretta | 627de19 | 2000-07-12 02:09:17 +0000 | [diff] [blame] | 2125 | case 'k': |
| 2126 | SET(CUT_TO_END); |
| 2127 | break; |
Chris Allegretta | 18bd029 | 2000-07-28 01:18:10 +0000 | [diff] [blame] | 2128 | #else |
| 2129 | case 'k': |
| 2130 | usage(); /* Oops! You dont really have that option */ |
| 2131 | finish(1); |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 2132 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2133 | case 'l': |
| 2134 | UNSET(FOLLOW_SYMLINKS); |
| 2135 | break; |
| 2136 | case 'm': |
| 2137 | SET(USE_MOUSE); |
| 2138 | break; |
| 2139 | case 'p': |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 2140 | SET(PICO_MODE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2141 | break; |
| 2142 | case 'r': |
| 2143 | fill = atoi(optarg); |
| 2144 | if (fill <= 0) { |
| 2145 | usage(); /* To stop bogus data (like a string) */ |
| 2146 | finish(1); |
| 2147 | } |
| 2148 | break; |
| 2149 | case 's': |
| 2150 | alt_speller = nmalloc(strlen(optarg) + 1); |
| 2151 | strcpy(alt_speller, optarg); |
| 2152 | break; |
| 2153 | case 't': |
Chris Allegretta | 3088555 | 2000-07-14 01:20:12 +0000 | [diff] [blame] | 2154 | SET(TEMP_OPT); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2155 | break; |
| 2156 | case 'v': |
| 2157 | SET(VIEW_MODE); |
| 2158 | break; |
| 2159 | case 'w': |
| 2160 | SET(NO_WRAP); |
| 2161 | break; |
| 2162 | case 'x': |
| 2163 | SET(NO_HELP); |
| 2164 | break; |
| 2165 | case 'z': |
| 2166 | SET(SUSPEND); |
| 2167 | break; |
| 2168 | default: |
| 2169 | usage(); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 2170 | exit(0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | } |
| 2174 | |
| 2175 | argv0 = strrchr(argv[0], '/'); |
| 2176 | if ((argv0 && strstr(argv0, "pico")) |
| 2177 | || (!argv0 && strstr(argv[0], "pico"))) |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 2178 | SET(PICO_MODE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2179 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2180 | /* See if there's a non-option in argv (first non-option is the |
| 2181 | filename, if +LINE is not given) */ |
| 2182 | if (argc == 1 || argc <= optind) |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 2183 | clear_filename(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2184 | else { |
| 2185 | /* Look for the +line flag... */ |
| 2186 | if (argv[optind][0] == '+') { |
| 2187 | startline = atoi(&argv[optind][1]); |
| 2188 | optind++; |
| 2189 | if (argc == 1 || argc <= optind) |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 2190 | clear_filename(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2191 | else |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 2192 | filename = mallocstrcpy(filename, argv[optind]); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2193 | |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 2194 | } else |
| 2195 | filename = mallocstrcpy(filename, argv[optind]); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | |
| 2199 | /* First back up the old settings so they can be restored, duh */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 2200 | tcgetattr(0, &oldterm); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2201 | |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 2202 | #ifdef _POSIX_VDISABLE |
Chris Allegretta | 8f6c069 | 2000-07-19 01:16:18 +0000 | [diff] [blame] | 2203 | term = oldterm; |
| 2204 | term.c_cc[VINTR] = _POSIX_VDISABLE; |
| 2205 | term.c_cc[VQUIT] = _POSIX_VDISABLE; |
| 2206 | term.c_lflag &= ~IEXTEN; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 2207 | tcsetattr(0, TCSANOW, &term); |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 2208 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2209 | |
| 2210 | /* now ncurses init stuff... */ |
| 2211 | initscr(); |
| 2212 | savetty(); |
| 2213 | nonl(); |
| 2214 | cbreak(); |
| 2215 | noecho(); |
| 2216 | timeout(0); |
| 2217 | |
| 2218 | /* Set up some global variables */ |
| 2219 | global_init(); |
Chris Allegretta | 0779835 | 2000-11-27 22:58:23 +0000 | [diff] [blame] | 2220 | shortcut_init(0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2221 | init_help_msg(); |
Chris Allegretta | 3bc8c72 | 2000-12-10 17:03:25 +0000 | [diff] [blame] | 2222 | #if !defined(NANO_SMALL) && !defined(DISABLE_HELP) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2223 | help_init(); |
Chris Allegretta | 3bc8c72 | 2000-12-10 17:03:25 +0000 | [diff] [blame] | 2224 | #endif |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2225 | signal_init(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2226 | |
| 2227 | #ifdef DEBUG |
| 2228 | fprintf(stderr, _("Main: set up windows\n")); |
| 2229 | #endif |
| 2230 | |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2231 | window_init(); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2232 | mouse_init(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2233 | |
Chris Allegretta | ad1dacc | 2000-09-21 04:25:45 +0000 | [diff] [blame] | 2234 | #ifdef PDCURSES |
| 2235 | /* Must have this for the arrow, et al, keys to even work in |
| 2236 | PDCurses+cygwin under Windows */ |
Chris Allegretta | 1cc0b7f | 2000-11-03 01:29:04 +0000 | [diff] [blame] | 2237 | keypad_on(TRUE); |
Chris Allegretta | ad1dacc | 2000-09-21 04:25:45 +0000 | [diff] [blame] | 2238 | #endif |
| 2239 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2240 | #ifdef DEBUG |
| 2241 | fprintf(stderr, _("Main: bottom win\n")); |
| 2242 | #endif |
| 2243 | /* Set up up bottom of window */ |
| 2244 | display_main_list(); |
| 2245 | |
| 2246 | #ifdef DEBUG |
| 2247 | fprintf(stderr, _("Main: open file\n")); |
| 2248 | #endif |
| 2249 | |
| 2250 | titlebar(); |
Chris Allegretta | 31c7666 | 2000-11-21 06:20:20 +0000 | [diff] [blame] | 2251 | |
| 2252 | /* Now we check to see if argv[optind] is non-null to determine if |
| 2253 | we're dealing with a new file or not, not argc == 1... */ |
| 2254 | if (argv[optind] == NULL) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2255 | new_file(); |
| 2256 | else |
| 2257 | open_file(filename, 0, 0); |
| 2258 | |
| 2259 | if (startline > 0) |
| 2260 | do_gotoline(startline); |
| 2261 | else |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 2262 | edit_update(fileage, CENTER); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2263 | |
Robert Siemborski | 6967eec | 2000-07-08 14:23:32 +0000 | [diff] [blame] | 2264 | edit_refresh(); |
| 2265 | reset_cursor(); |
| 2266 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2267 | while (1) { |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 2268 | |
| 2269 | #ifndef _POSIX_VDISABLE |
| 2270 | /* We're going to have to do it the old way, i.e. on cygwin */ |
| 2271 | raw(); |
| 2272 | #endif |
| 2273 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2274 | kbinput = wgetch(edit); |
| 2275 | if (kbinput == 27) { /* Grab Alt-key stuff first */ |
| 2276 | switch (kbinput = wgetch(edit)) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2277 | /* Alt-O, suddenly very important ;) */ |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2278 | case 79: |
| 2279 | kbinput = wgetch(edit); |
| 2280 | if (kbinput <= 'S' && kbinput >= 'P') |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2281 | kbinput = KEY_F(kbinput - 79); |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2282 | #ifdef DEBUG |
| 2283 | else { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2284 | fprintf(stderr, _("I got Alt-O-%c! (%d)\n"), |
| 2285 | kbinput, kbinput); |
| 2286 | break; |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2287 | } |
| 2288 | #endif |
| 2289 | break; |
Chris Allegretta | 51b3eec | 2000-12-18 02:23:50 +0000 | [diff] [blame] | 2290 | case 27: |
| 2291 | /* If we get Alt-Alt, the next keystroke should be the same as a |
| 2292 | control sequence */ |
| 2293 | modify_control_seq = 1; |
| 2294 | keyhandled = 1; |
| 2295 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2296 | case 91: |
| 2297 | |
| 2298 | switch (kbinput = wgetch(edit)) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2299 | case '1': /* Alt-[-1-[0-5,7-9] = F1-F8 in X at least */ |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2300 | kbinput = wgetch(edit); |
| 2301 | if (kbinput >= '1' && kbinput <= '5') { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2302 | kbinput = KEY_F(kbinput - 48); |
| 2303 | wgetch(edit); |
| 2304 | } else if (kbinput >= '7' && kbinput <= '9') { |
| 2305 | kbinput = KEY_F(kbinput - 49); |
| 2306 | wgetch(edit); |
| 2307 | } else if (kbinput == 126) |
| 2308 | kbinput = KEY_HOME; |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2309 | |
| 2310 | #ifdef DEBUG |
| 2311 | else { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2312 | fprintf(stderr, _("I got Alt-[-1-%c! (%d)\n"), |
| 2313 | kbinput, kbinput); |
| 2314 | break; |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2315 | } |
| 2316 | #endif |
| 2317 | |
| 2318 | break; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2319 | case '2': /* Alt-[-2-[0,1,3,4] = F9-F12 in many terms */ |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2320 | kbinput = wgetch(edit); |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2321 | switch (kbinput) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2322 | case '0': |
| 2323 | kbinput = KEY_F(9); |
| 2324 | wgetch(edit); |
| 2325 | break; |
| 2326 | case '1': |
| 2327 | kbinput = KEY_F(10); |
| 2328 | wgetch(edit); |
| 2329 | break; |
| 2330 | case '3': |
| 2331 | kbinput = KEY_F(11); |
| 2332 | wgetch(edit); |
| 2333 | break; |
| 2334 | case '4': |
| 2335 | kbinput = KEY_F(12); |
| 2336 | wgetch(edit); |
| 2337 | break; |
| 2338 | case 126: /* Hack, make insert key do something |
| 2339 | usefile, like insert file */ |
| 2340 | do_insertfile(); |
| 2341 | keyhandled = 1; |
| 2342 | break; |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2343 | #ifdef DEBUG |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2344 | default: |
| 2345 | fprintf(stderr, _("I got Alt-[-2-%c! (%d)\n"), |
| 2346 | kbinput, kbinput); |
| 2347 | break; |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2348 | #endif |
| 2349 | |
| 2350 | } |
| 2351 | break; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2352 | case '3': /* Alt-[-3 = Delete? */ |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2353 | kbinput = NANO_DELETE_KEY; |
| 2354 | wgetch(edit); |
| 2355 | break; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2356 | case '4': /* Alt-[-4 = End? */ |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2357 | kbinput = NANO_END_KEY; |
| 2358 | wgetch(edit); |
| 2359 | break; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2360 | case '5': /* Alt-[-5 = Page Up */ |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2361 | kbinput = KEY_PPAGE; |
| 2362 | wgetch(edit); |
| 2363 | break; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2364 | case '6': /* Alt-[-6 = Page Down */ |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2365 | kbinput = KEY_NPAGE; |
| 2366 | wgetch(edit); |
| 2367 | break; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2368 | case '[': /* Alt-[-[-[A-E], F1-F5 in linux console */ |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2369 | kbinput = wgetch(edit); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2370 | if (kbinput >= 'A' && kbinput <= 'E') |
| 2371 | kbinput = KEY_F(kbinput - 64); |
Chris Allegretta | 16e4168 | 2000-09-11 22:33:54 +0000 | [diff] [blame] | 2372 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2373 | case 'A': |
| 2374 | kbinput = KEY_UP; |
| 2375 | break; |
| 2376 | case 'B': |
| 2377 | kbinput = KEY_DOWN; |
| 2378 | break; |
| 2379 | case 'C': |
| 2380 | kbinput = KEY_RIGHT; |
| 2381 | break; |
| 2382 | case 'D': |
| 2383 | kbinput = KEY_LEFT; |
| 2384 | break; |
| 2385 | case 'H': |
| 2386 | kbinput = KEY_HOME; |
| 2387 | break; |
| 2388 | case 'F': |
| 2389 | kbinput = KEY_END; |
| 2390 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2391 | default: |
| 2392 | #ifdef DEBUG |
| 2393 | fprintf(stderr, _("I got Alt-[-%c! (%d)\n"), |
| 2394 | kbinput, kbinput); |
| 2395 | #endif |
| 2396 | break; |
| 2397 | } |
| 2398 | break; |
| 2399 | default: |
| 2400 | |
| 2401 | /* Check for the altkey defs.... */ |
| 2402 | for (i = 0; i <= MAIN_LIST_LEN - 1; i++) |
| 2403 | if (kbinput == main_list[i].altval || |
| 2404 | kbinput == main_list[i].altval - 32) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2405 | kbinput = main_list[i].val; |
| 2406 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2407 | } |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2408 | #ifndef NANO_SMALL |
| 2409 | /* And for toggle switches */ |
| 2410 | for (i = 0; i <= TOGGLE_LEN - 1 && !keyhandled; i++) |
| 2411 | if (kbinput == toggles[i].val || |
| 2412 | kbinput == toggles[i].val - 32) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2413 | do_toggle(i); |
| 2414 | keyhandled = 1; |
| 2415 | break; |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2416 | } |
| 2417 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2418 | #ifdef DEBUG |
| 2419 | fprintf(stderr, _("I got Alt-%c! (%d)\n"), kbinput, |
| 2420 | kbinput); |
| 2421 | #endif |
| 2422 | break; |
| 2423 | } |
| 2424 | } |
Chris Allegretta | 51b3eec | 2000-12-18 02:23:50 +0000 | [diff] [blame] | 2425 | /* If the modify_control_seq is set, we received an Alt-Alt |
| 2426 | sequence before this, so we make this key a control sequence |
| 2427 | by subtracting 64 or 96, depending on its value. */ |
| 2428 | if (!keyhandled && modify_control_seq) { |
| 2429 | if (kbinput >= 'A' && kbinput < 'a') |
| 2430 | kbinput -= 64; |
| 2431 | else if (kbinput >= 'a' && kbinput <= 'z') |
| 2432 | kbinput -= 96; |
| 2433 | |
| 2434 | modify_control_seq = 0; |
| 2435 | } |
| 2436 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2437 | /* Look through the main shortcut list to see if we've hit a |
| 2438 | shortcut key */ |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2439 | for (i = 0; i < MAIN_LIST_LEN && !keyhandled; i++) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2440 | if (kbinput == main_list[i].val || |
| 2441 | (main_list[i].misc1 && kbinput == main_list[i].misc1) || |
| 2442 | (main_list[i].misc2 && kbinput == main_list[i].misc2)) { |
| 2443 | if (ISSET(VIEW_MODE) && !main_list[i].viewok) |
| 2444 | print_view_warning(); |
| 2445 | else |
| 2446 | main_list[i].func(); |
| 2447 | keyhandled = 1; |
| 2448 | } |
| 2449 | } |
Chris Allegretta | 51b3eec | 2000-12-18 02:23:50 +0000 | [diff] [blame] | 2450 | /* If we're in raw mode or using Alt-Alt-x, we have to catch |
| 2451 | Control-S and Control-Q */ |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 2452 | if (kbinput == 17 || kbinput == 19) |
| 2453 | keyhandled = 1; |
| 2454 | |
Chris Allegretta | 51b3eec | 2000-12-18 02:23:50 +0000 | [diff] [blame] | 2455 | /* Catch ^Z by hand when triggered also */ |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 2456 | if (kbinput == 26) { |
| 2457 | if (ISSET(SUSPEND)) |
| 2458 | do_suspend(0); |
| 2459 | keyhandled = 1; |
| 2460 | } |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 2461 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2462 | /* Last gasp, stuff that's not in the main lists */ |
| 2463 | if (!keyhandled) |
| 2464 | switch (kbinput) { |
| 2465 | #ifndef NANO_SMALL |
| 2466 | #ifdef NCURSES_MOUSE_VERSION |
| 2467 | case KEY_MOUSE: |
| 2468 | do_mouse(); |
| 2469 | break; |
| 2470 | #endif |
| 2471 | #endif |
| 2472 | case 0: /* Erg */ |
| 2473 | do_next_word(); |
| 2474 | break; |
| 2475 | case 331: /* Stuff that we don't want to do squat */ |
| 2476 | case -1: |
| 2477 | case 410: /* Must ignore this, it gets sent when we resize */ |
Chris Allegretta | ad1dacc | 2000-09-21 04:25:45 +0000 | [diff] [blame] | 2478 | #ifdef PDCURSES |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2479 | case 541: /* ???? */ |
| 2480 | case 542: /* Control and alt in Windows *shrug* */ |
Chris Allegretta | 7262358 | 2000-11-29 23:43:28 +0000 | [diff] [blame] | 2481 | case 543: /* Right ctrl key */ |
Chris Allegretta | ad1dacc | 2000-09-21 04:25:45 +0000 | [diff] [blame] | 2482 | case 544: |
Chris Allegretta | 7262358 | 2000-11-29 23:43:28 +0000 | [diff] [blame] | 2483 | case 545: /* Right alt key */ |
Chris Allegretta | ad1dacc | 2000-09-21 04:25:45 +0000 | [diff] [blame] | 2484 | #endif |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2485 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2486 | break; |
| 2487 | default: |
| 2488 | #ifdef DEBUG |
| 2489 | fprintf(stderr, "I got %c (%d)!\n", kbinput, kbinput); |
| 2490 | #endif |
| 2491 | /* We no longer stop unhandled sequences so that people with |
| 2492 | odd character sets can type... */ |
| 2493 | |
| 2494 | if (ISSET(VIEW_MODE)) { |
| 2495 | print_view_warning(); |
| 2496 | break; |
| 2497 | } |
| 2498 | do_char(kbinput); |
| 2499 | } |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2500 | if (ISSET(CONSTUPDATE)) { |
| 2501 | if (ISSET(DISABLE_CURPOS)) |
| 2502 | UNSET(DISABLE_CURPOS); |
| 2503 | else |
| 2504 | do_cursorpos(); |
| 2505 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2506 | |
| 2507 | reset_cursor(); |
| 2508 | wrefresh(edit); |
| 2509 | keyhandled = 0; |
| 2510 | } |
| 2511 | |
| 2512 | getchar(); |
| 2513 | finish(0); |
| 2514 | |
| 2515 | } |