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