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