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