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