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