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