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