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