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