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