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