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