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