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