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