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