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 " |
David Lawrence Ramsey | f0a53f0 | 2005-01-03 19:56:56 +0000 | [diff] [blame] | 375 | "will enter the character with the corresponding value. The " |
| 376 | "following keystrokes are available in the main editor window. " |
| 377 | "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 | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1147 | int *kbinput; |
| 1148 | size_t kbinput_len, i; |
| 1149 | char *output; |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1150 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1151 | statusbar(_("Verbatim input")); |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1152 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1153 | /* Read in all the verbatim characters. */ |
| 1154 | kbinput = get_verbatim_kbinput(edit, &kbinput_len); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1155 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1156 | /* Display all the verbatim characters at once. */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1157 | output = charalloc(kbinput_len + 1); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1158 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1159 | for (i = 0; i < kbinput_len; i++) |
| 1160 | output[i] = (char)kbinput[i]; |
| 1161 | output[i] = '\0'; |
| 1162 | |
| 1163 | do_output(output, kbinput_len); |
| 1164 | |
| 1165 | free(output); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1168 | void do_backspace(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1169 | { |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 1170 | if (current != fileage || current_x > 0) { |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1171 | do_left(FALSE); |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 1172 | do_delete(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1173 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1176 | void do_delete(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1177 | { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1178 | bool do_refresh = FALSE; |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1179 | /* Do we have to call edit_refresh(), or can we get away with |
| 1180 | * update_line()? */ |
| 1181 | |
David Lawrence Ramsey | 40a6c8c | 2004-11-27 21:10:11 +0000 | [diff] [blame] | 1182 | assert(current != NULL && current->data != NULL && |
| 1183 | current_x <= strlen(current->data)); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1184 | |
| 1185 | placewewant = xplustabs(); |
| 1186 | |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1187 | if (current->data[current_x] != '\0') { |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1188 | int char_buf_len = parse_mbchar(current->data + current_x, NULL |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 1189 | #ifdef NANO_WIDE |
| 1190 | , NULL |
| 1191 | #endif |
David Lawrence Ramsey | d96851f | 2005-01-07 22:39:43 +0000 | [diff] [blame] | 1192 | , NULL); |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 1193 | size_t line_len = strlen(current->data + current_x); |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1194 | |
| 1195 | assert(current_x < strlen(current->data)); |
| 1196 | |
| 1197 | /* Let's get dangerous. */ |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 1198 | charmove(¤t->data[current_x], |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1199 | ¤t->data[current_x + char_buf_len], |
| 1200 | line_len - char_buf_len + 1); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1201 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1202 | null_at(¤t->data, current_x + line_len - char_buf_len); |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1203 | #ifndef NANO_SMALL |
| 1204 | if (current_x < mark_beginx && mark_beginbuf == current) |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1205 | mark_beginx -= char_buf_len; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1206 | #endif |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1207 | totsize -= char_buf_len; |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1208 | } else if (current != filebot && (current->next != filebot || |
| 1209 | current->data[0] == '\0')) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1210 | /* We can delete the line before filebot only if it is blank: it |
David Lawrence Ramsey | 32d19ce | 2004-05-24 05:05:07 +0000 | [diff] [blame] | 1211 | * becomes the new magicline then. */ |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1212 | filestruct *foo = current->next; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1213 | |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1214 | assert(current_x == strlen(current->data)); |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1215 | |
| 1216 | /* If we're deleting at the end of a line, we need to call |
| 1217 | * edit_refresh(). */ |
| 1218 | if (current->data[current_x] == '\0') |
| 1219 | do_refresh = TRUE; |
| 1220 | |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 1221 | current->data = charealloc(current->data, |
| 1222 | current_x + strlen(foo->data) + 1); |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1223 | strcpy(current->data + current_x, foo->data); |
| 1224 | #ifndef NANO_SMALL |
| 1225 | if (mark_beginbuf == current->next) { |
| 1226 | mark_beginx += current_x; |
| 1227 | mark_beginbuf = current; |
| 1228 | } |
| 1229 | #endif |
David Lawrence Ramsey | b977515 | 2004-03-19 02:15:42 +0000 | [diff] [blame] | 1230 | if (filebot == foo) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1231 | filebot = current; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1232 | |
| 1233 | unlink_node(foo); |
| 1234 | delete_node(foo); |
| 1235 | renumber(current); |
| 1236 | totlines--; |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 1237 | totsize--; |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 1238 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1239 | wrap_reset(); |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 1240 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1241 | } else |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1242 | return; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1243 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1244 | set_modified(); |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1245 | |
| 1246 | #ifdef ENABLE_COLOR |
| 1247 | /* If color syntaxes are turned on, we need to call |
| 1248 | * edit_refresh(). */ |
| 1249 | if (ISSET(COLOR_SYNTAX)) |
| 1250 | do_refresh = TRUE; |
| 1251 | #endif |
| 1252 | |
| 1253 | if (do_refresh) |
| 1254 | edit_refresh(); |
| 1255 | else |
| 1256 | update_line(current, current_x); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1259 | void do_tab(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1260 | { |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1261 | char *kbinput = "\t"; |
| 1262 | |
| 1263 | do_output(kbinput, 1); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1266 | /* Someone hits return *gasp!* */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1267 | void do_enter(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1268 | { |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1269 | filestruct *newnode = make_new_node(current); |
| 1270 | size_t extra = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1271 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1272 | assert(current != NULL && current->data != NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1273 | |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 1274 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1275 | /* Do auto-indenting, like the neolithic Turbo Pascal editor. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1276 | if (ISSET(AUTOINDENT)) { |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1277 | /* If we are breaking the line in the indentation, the new |
| 1278 | * indentation should have only current_x characters, and |
| 1279 | * current_x should not change. */ |
| 1280 | extra = indent_length(current->data); |
| 1281 | if (extra > current_x) |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 1282 | extra = current_x; |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 1283 | totsize += extra; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1284 | } |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1285 | #endif |
| 1286 | newnode->data = charalloc(strlen(current->data + current_x) + |
| 1287 | extra + 1); |
| 1288 | strcpy(&newnode->data[extra], current->data + current_x); |
| 1289 | #ifndef NANO_SMALL |
| 1290 | if (ISSET(AUTOINDENT)) |
| 1291 | strncpy(newnode->data, current->data, extra); |
| 1292 | #endif |
| 1293 | null_at(¤t->data, current_x); |
| 1294 | #ifndef NANO_SMALL |
| 1295 | if (current == mark_beginbuf && current_x < mark_beginx) { |
| 1296 | mark_beginbuf = newnode; |
| 1297 | mark_beginx += extra - current_x; |
| 1298 | } |
| 1299 | #endif |
| 1300 | current_x = extra; |
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 | if (current == filebot) |
Chris Allegretta | e316773 | 2001-03-18 16:59:34 +0000 | [diff] [blame] | 1303 | filebot = newnode; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1304 | splice_node(current, newnode, current->next); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1305 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1306 | renumber(current); |
Chris Allegretta | e316773 | 2001-03-18 16:59:34 +0000 | [diff] [blame] | 1307 | current = newnode; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1308 | |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1309 | edit_refresh(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1310 | |
| 1311 | totlines++; |
David Lawrence Ramsey | 4d97a58 | 2004-12-29 16:42:48 +0000 | [diff] [blame] | 1312 | totsize++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1313 | set_modified(); |
Chris Allegretta | b0ae393 | 2000-06-15 23:39:14 +0000 | [diff] [blame] | 1314 | placewewant = xplustabs(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Chris Allegretta | ad3f478 | 2001-10-02 03:54:13 +0000 | [diff] [blame] | 1317 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1318 | void do_next_word(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1319 | { |
David Lawrence Ramsey | 86e851b | 2004-07-28 20:46:25 +0000 | [diff] [blame] | 1320 | size_t old_pww = placewewant; |
David Lawrence Ramsey | 381d483 | 2004-10-18 01:51:43 +0000 | [diff] [blame] | 1321 | const filestruct *old_current = current; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1322 | assert(current != NULL && current->data != NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1323 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1324 | /* Skip letters in this word first. */ |
| 1325 | while (current->data[current_x] != '\0' && |
David Lawrence Ramsey | d9fb3e6 | 2004-10-21 22:49:51 +0000 | [diff] [blame] | 1326 | isalnum(current->data[current_x])) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1327 | current_x++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1328 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1329 | for (; current != NULL; current = current->next) { |
| 1330 | while (current->data[current_x] != '\0' && |
David Lawrence Ramsey | d9fb3e6 | 2004-10-21 22:49:51 +0000 | [diff] [blame] | 1331 | !isalnum(current->data[current_x])) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1332 | current_x++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1333 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1334 | if (current->data[current_x] != '\0') |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1335 | break; |
| 1336 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1337 | current_x = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1338 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1339 | if (current == NULL) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1340 | current = filebot; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1341 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1342 | placewewant = xplustabs(); |
Chris Allegretta | 9e2934f | 2000-12-01 23:49:48 +0000 | [diff] [blame] | 1343 | |
David Lawrence Ramsey | 381d483 | 2004-10-18 01:51:43 +0000 | [diff] [blame] | 1344 | /* Update the screen. */ |
| 1345 | edit_redraw(old_current, old_pww); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1348 | /* The same thing for backwards. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1349 | void do_prev_word(void) |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1350 | { |
David Lawrence Ramsey | 86e851b | 2004-07-28 20:46:25 +0000 | [diff] [blame] | 1351 | size_t old_pww = placewewant; |
David Lawrence Ramsey | 381d483 | 2004-10-18 01:51:43 +0000 | [diff] [blame] | 1352 | const filestruct *old_current = current; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1353 | assert(current != NULL && current->data != NULL); |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1354 | |
David Lawrence Ramsey | 7a97e18 | 2004-10-30 01:03:15 +0000 | [diff] [blame] | 1355 | current_x++; |
| 1356 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1357 | /* Skip letters in this word first. */ |
David Lawrence Ramsey | 7a97e18 | 2004-10-30 01:03:15 +0000 | [diff] [blame] | 1358 | while (current_x > 0 && isalnum(current->data[current_x - 1])) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1359 | current_x--; |
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 | for (; current != NULL; current = current->prev) { |
David Lawrence Ramsey | 7a97e18 | 2004-10-30 01:03:15 +0000 | [diff] [blame] | 1362 | while (current_x > 0 && !isalnum(current->data[current_x - 1])) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1363 | current_x--; |
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 | if (current_x > 0) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1366 | break; |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1367 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1368 | if (current->prev != NULL) |
David Lawrence Ramsey | 7a97e18 | 2004-10-30 01:03:15 +0000 | [diff] [blame] | 1369 | current_x = strlen(current->prev->data) + 1; |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1370 | } |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1371 | |
David Lawrence Ramsey | 7a97e18 | 2004-10-30 01:03:15 +0000 | [diff] [blame] | 1372 | current_x--; |
| 1373 | |
David Lawrence Ramsey | 1b525e9 | 2004-05-24 02:35:02 +0000 | [diff] [blame] | 1374 | if (current == NULL) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1375 | current = fileage; |
| 1376 | current_x = 0; |
David Lawrence Ramsey | 1b525e9 | 2004-05-24 02:35:02 +0000 | [diff] [blame] | 1377 | } else { |
David Lawrence Ramsey | d9fb3e6 | 2004-10-21 22:49:51 +0000 | [diff] [blame] | 1378 | while (current_x > 0 && isalnum(current->data[current_x - 1])) |
David Lawrence Ramsey | 1b525e9 | 2004-05-24 02:35:02 +0000 | [diff] [blame] | 1379 | current_x--; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1382 | placewewant = xplustabs(); |
| 1383 | |
David Lawrence Ramsey | 381d483 | 2004-10-18 01:51:43 +0000 | [diff] [blame] | 1384 | /* Update the screen. */ |
| 1385 | edit_redraw(old_current, old_pww); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1386 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1387 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1388 | void do_mark(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1389 | { |
David Lawrence Ramsey | f03c78b | 2003-09-28 21:26:49 +0000 | [diff] [blame] | 1390 | TOGGLE(MARK_ISSET); |
| 1391 | if (ISSET(MARK_ISSET)) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1392 | statusbar(_("Mark Set")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1393 | mark_beginbuf = current; |
| 1394 | mark_beginx = current_x; |
| 1395 | } else { |
| 1396 | statusbar(_("Mark UNset")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1397 | edit_refresh(); |
| 1398 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1399 | } |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 1400 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1401 | |
David Lawrence Ramsey | c97acfb | 2003-09-10 20:08:00 +0000 | [diff] [blame] | 1402 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1403 | void wrap_reset(void) |
| 1404 | { |
David Lawrence Ramsey | 85529b3 | 2004-06-22 15:38:47 +0000 | [diff] [blame] | 1405 | same_line_wrap = FALSE; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1406 | } |
David Lawrence Ramsey | c97acfb | 2003-09-10 20:08:00 +0000 | [diff] [blame] | 1407 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1408 | |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 1409 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1410 | /* We wrap the given line. Precondition: we assume the cursor has been |
| 1411 | * moved forward since the last typed character. Return value: whether |
| 1412 | * we wrapped. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1413 | bool do_wrap(filestruct *inptr) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1414 | { |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1415 | size_t len = strlen(inptr->data); |
| 1416 | /* Length of the line we wrap. */ |
| 1417 | size_t i = 0; |
| 1418 | /* Generic loop variable. */ |
David Lawrence Ramsey | b5a7a5a | 2004-11-23 03:42:43 +0000 | [diff] [blame] | 1419 | ssize_t wrap_loc = -1; |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1420 | /* Index of inptr->data where we wrap. */ |
David Lawrence Ramsey | b5a7a5a | 2004-11-23 03:42:43 +0000 | [diff] [blame] | 1421 | ssize_t word_back = -1; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1422 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1423 | const char *indentation = NULL; |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1424 | /* Indentation to prepend to the new line. */ |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1425 | size_t indent_len = 0; /* strlen(indentation) */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1426 | #endif |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1427 | const char *after_break; /* Text after the wrap point. */ |
| 1428 | size_t after_break_len; /* strlen(after_break) */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1429 | bool wrapping = FALSE; /* Do we prepend to the next line? */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1430 | const char *wrap_line = NULL; |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1431 | /* The next line, minus indentation. */ |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1432 | size_t wrap_line_len = 0; /* strlen(wrap_line) */ |
| 1433 | char *newline = NULL; /* The line we create. */ |
| 1434 | size_t new_line_len = 0; /* Eventual length of newline. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1435 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1436 | /* There are three steps. First, we decide where to wrap. Then, we |
| 1437 | * create the new wrap line. Finally, we clean up. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1438 | |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1439 | /* 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] | 1440 | * 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] | 1441 | * location of this replacement. |
| 1442 | * |
David Lawrence Ramsey | b5a7a5a | 2004-11-23 03:42:43 +0000 | [diff] [blame] | 1443 | * 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] | 1444 | * such that the last word before it ended at or before fill. If there |
| 1445 | * is no such point, we settle for the first legal wrap point. |
| 1446 | * |
David Lawrence Ramsey | b5a7a5a | 2004-11-23 03:42:43 +0000 | [diff] [blame] | 1447 | * A legal wrap point is a whitespace character that is not followed by |
| 1448 | * whitespace. |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1449 | * |
| 1450 | * If there is no legal wrap point or we found the last character of the |
| 1451 | * line, we should return without wrapping. |
| 1452 | * |
| 1453 | * Note that the initial indentation does not count as a legal wrap |
| 1454 | * point if we are going to auto-indent! |
| 1455 | * |
David Lawrence Ramsey | 8d91199 | 2004-05-29 17:05:52 +0000 | [diff] [blame] | 1456 | * Note that the code below could be optimized, by not calling |
| 1457 | * strnlenpt() so often. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1458 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1459 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1460 | if (ISSET(AUTOINDENT)) |
| 1461 | i = indent_length(inptr->data); |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1462 | #endif |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1463 | wrap_line = inptr->data + i; |
David Lawrence Ramsey | 4e25410 | 2003-11-05 22:04:08 +0000 | [diff] [blame] | 1464 | for (; i < len; i++, wrap_line++) { |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1465 | /* Record where the last word ended. */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1466 | if (!is_blank_char(*wrap_line)) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1467 | word_back = i; |
David Lawrence Ramsey | b5a7a5a | 2004-11-23 03:42:43 +0000 | [diff] [blame] | 1468 | /* 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] | 1469 | * extends too far, then we stop. */ |
David Lawrence Ramsey | 40a6c8c | 2004-11-27 21:10:11 +0000 | [diff] [blame] | 1470 | if (wrap_loc != -1 && |
| 1471 | strnlenpt(inptr->data, word_back + 1) > fill) |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1472 | break; |
David Lawrence Ramsey | b5a7a5a | 2004-11-23 03:42:43 +0000 | [diff] [blame] | 1473 | /* We record the latest legal wrap point. */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1474 | if (word_back != i && !is_blank_char(wrap_line[1])) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1475 | wrap_loc = i; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1476 | } |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1477 | if (i == len) |
| 1478 | return FALSE; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1479 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1480 | /* Step 2, making the new wrap line. It will consist of indentation |
| 1481 | * + after_break + " " + wrap_line (although indentation and |
| 1482 | * wrap_line are conditional on flags and #defines). */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1483 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1484 | /* after_break is the text that will be moved to the next line. */ |
| 1485 | after_break = inptr->data + wrap_loc + 1; |
| 1486 | after_break_len = len - wrap_loc - 1; |
David Lawrence Ramsey | 6a2f068 | 2004-12-20 01:13:55 +0000 | [diff] [blame] | 1487 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1488 | assert(after_break_len == strlen(after_break)); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1489 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1490 | /* new_line_len will later be increased by the lengths of indentation |
| 1491 | * and wrap_line. */ |
| 1492 | new_line_len = after_break_len; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1493 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1494 | /* We prepend the wrapped text to the next line, if the flag is set, |
| 1495 | * and there is a next line, and prepending would not make the line |
| 1496 | * too long. */ |
David Lawrence Ramsey | c97acfb | 2003-09-10 20:08:00 +0000 | [diff] [blame] | 1497 | if (same_line_wrap && inptr->next) { |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1498 | wrap_line = inptr->next->data; |
| 1499 | wrap_line_len = strlen(wrap_line); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1500 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1501 | /* +1 for the space between after_break and wrap_line. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1502 | if ((new_line_len + 1 + wrap_line_len) <= fill) { |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1503 | wrapping = TRUE; |
David Lawrence Ramsey | 4564170 | 2004-12-18 20:29:42 +0000 | [diff] [blame] | 1504 | new_line_len += 1 + wrap_line_len; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1505 | } |
| 1506 | } |
Chris Allegretta | 56214c6 | 2001-09-27 02:46:53 +0000 | [diff] [blame] | 1507 | |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 1508 | #ifndef NANO_SMALL |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1509 | if (ISSET(AUTOINDENT)) { |
Chris Allegretta | 81dea02 | 2002-09-21 02:19:45 +0000 | [diff] [blame] | 1510 | /* Indentation comes from the next line if wrapping, else from |
| 1511 | * this line. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1512 | indentation = (wrapping ? wrap_line : inptr->data); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1513 | indent_len = indent_length(indentation); |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1514 | if (wrapping) |
Chris Allegretta | 81dea02 | 2002-09-21 02:19:45 +0000 | [diff] [blame] | 1515 | /* The wrap_line text should not duplicate indentation. |
| 1516 | * Note in this case we need not increase new_line_len. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1517 | wrap_line += indent_len; |
| 1518 | else |
| 1519 | new_line_len += indent_len; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1520 | } |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1521 | #endif |
Adam Rogoyski | 1e9183f | 2001-03-13 18:36:03 +0000 | [diff] [blame] | 1522 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1523 | /* Now we allocate the new line and copy into it. */ |
| 1524 | newline = charalloc(new_line_len + 1); /* +1 for \0 */ |
David Lawrence Ramsey | 85529b3 | 2004-06-22 15:38:47 +0000 | [diff] [blame] | 1525 | new_line_len = 0; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1526 | *newline = '\0'; |
| 1527 | |
| 1528 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1529 | if (ISSET(AUTOINDENT)) { |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1530 | strncpy(newline, indentation, indent_len); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1531 | newline[indent_len] = '\0'; |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1532 | new_line_len = indent_len; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1533 | } |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1534 | #endif |
| 1535 | strcat(newline, after_break); |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1536 | new_line_len += after_break_len; |
David Lawrence Ramsey | 6a2f068 | 2004-12-20 01:13:55 +0000 | [diff] [blame] | 1537 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1538 | /* We end the old line after wrap_loc. Note that this does not eat |
| 1539 | * the space. */ |
Chris Allegretta | 67ca2aa | 2002-09-19 23:19:34 +0000 | [diff] [blame] | 1540 | null_at(&inptr->data, wrap_loc + 1); |
| 1541 | totsize++; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1542 | if (wrapping) { |
Chris Allegretta | 67ca2aa | 2002-09-19 23:19:34 +0000 | [diff] [blame] | 1543 | /* In this case, totsize increases by 1 since we add a space |
Chris Allegretta | 81dea02 | 2002-09-21 02:19:45 +0000 | [diff] [blame] | 1544 | * between after_break and wrap_line. If the line already ends |
Chris Allegretta | 4300092 | 2002-09-21 15:41:33 +0000 | [diff] [blame] | 1545 | * in a tab or a space, we don't add a space and decrement |
| 1546 | * totsize to account for that. */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1547 | if (!is_blank_char(newline[new_line_len - 1])) |
Chris Allegretta | 81dea02 | 2002-09-21 02:19:45 +0000 | [diff] [blame] | 1548 | strcat(newline, " "); |
| 1549 | else |
| 1550 | totsize--; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1551 | strcat(newline, wrap_line); |
| 1552 | free(inptr->next->data); |
| 1553 | inptr->next->data = newline; |
| 1554 | } else { |
| 1555 | filestruct *temp = (filestruct *)nmalloc(sizeof(filestruct)); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1556 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1557 | /* In this case, the file size changes by +1 for the new line, |
| 1558 | * and +indent_len for the new indentation. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1559 | #ifndef NANO_SMALL |
| 1560 | totsize += indent_len; |
| 1561 | #endif |
| 1562 | totlines++; |
| 1563 | temp->data = newline; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1564 | temp->prev = inptr; |
| 1565 | temp->next = inptr->next; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1566 | temp->prev->next = temp; |
David Lawrence Ramsey | 6a2f068 | 2004-12-20 01:13:55 +0000 | [diff] [blame] | 1567 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1568 | /* If temp->next is NULL, then temp is the last line of the |
| 1569 | * file, so we must set filebot. */ |
| 1570 | if (temp->next != NULL) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1571 | temp->next->prev = temp; |
| 1572 | else |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1573 | filebot = temp; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1574 | } |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1575 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1576 | /* Step 3, clean up. Here we reposition the cursor and mark, and do |
| 1577 | * some other sundry things. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1578 | |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1579 | /* 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] | 1580 | same_line_wrap = TRUE; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1581 | |
| 1582 | /* Each line knows its line number. We recalculate these if we |
| 1583 | * inserted a new line. */ |
| 1584 | if (!wrapping) |
| 1585 | renumber(inptr); |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1586 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1587 | /* If the cursor was after the break point, we must move it. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1588 | if (current_x > wrap_loc) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1589 | current = current->next; |
| 1590 | current_x -= |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 1591 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1592 | -indent_len + |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 1593 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1594 | wrap_loc + 1; |
| 1595 | wrap_reset(); |
| 1596 | placewewant = xplustabs(); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1599 | #ifndef NANO_SMALL |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1600 | /* If the mark was on this line after the wrap point, we move it |
| 1601 | * 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] | 1602 | * right. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1603 | if (mark_beginbuf == inptr && mark_beginx > wrap_loc) { |
| 1604 | mark_beginbuf = inptr->next; |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 1605 | mark_beginx -= wrap_loc - indent_len + 1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1606 | } else if (wrapping && mark_beginbuf == inptr->next) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1607 | mark_beginx += after_break_len; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1608 | #endif /* !NANO_SMALL */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1609 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1610 | return TRUE; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1611 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1612 | #endif /* !DISABLE_WRAPPING */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1613 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 1614 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | aaad3af | 2003-08-31 16:44:10 +0000 | [diff] [blame] | 1615 | /* 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] | 1616 | * return FALSE if the user cancels. */ |
| 1617 | bool do_int_spell_fix(const char *word) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1618 | { |
David Lawrence Ramsey | d4ca9f2 | 2004-10-26 21:14:56 +0000 | [diff] [blame] | 1619 | char *save_search, *save_replace; |
David Lawrence Ramsey | 2eb7459 | 2004-10-21 16:25:44 +0000 | [diff] [blame] | 1620 | size_t current_x_save = current_x, pww_save = placewewant; |
David Lawrence Ramsey | d4ca9f2 | 2004-10-26 21:14:56 +0000 | [diff] [blame] | 1621 | filestruct *edittop_save = edittop, *current_save = current; |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1622 | /* Save where we are. */ |
David Lawrence Ramsey | 9bf486f | 2004-10-26 20:58:30 +0000 | [diff] [blame] | 1623 | bool canceled = FALSE; |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1624 | /* The return value. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1625 | bool case_sens_set = ISSET(CASE_SENSITIVE); |
David Lawrence Ramsey | 49d5c1b | 2004-10-11 13:55:33 +0000 | [diff] [blame] | 1626 | #ifndef NANO_SMALL |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1627 | bool reverse_search_set = ISSET(REVERSE_SEARCH); |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1628 | #endif |
David Lawrence Ramsey | b112fe6 | 2004-10-09 17:15:46 +0000 | [diff] [blame] | 1629 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1630 | bool regexp_set = ISSET(USE_REGEXP); |
| 1631 | #endif |
David Lawrence Ramsey | 9cf7b28 | 2004-10-15 16:35:34 +0000 | [diff] [blame] | 1632 | #ifndef NANO_SMALL |
| 1633 | bool old_mark_set = ISSET(MARK_ISSET); |
David Lawrence Ramsey | cb09337 | 2004-11-06 00:18:27 +0000 | [diff] [blame] | 1634 | bool added_magicline = FALSE; |
| 1635 | /* Whether we added a magicline after filebot. */ |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 1636 | bool right_side_up = FALSE; |
| 1637 | /* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark, |
| 1638 | * FALSE if (current, current_x) is. */ |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1639 | filestruct *top, *bot; |
David Lawrence Ramsey | 46c604a | 2004-11-05 23:55:58 +0000 | [diff] [blame] | 1640 | size_t top_x, bot_x; |
David Lawrence Ramsey | 9cf7b28 | 2004-10-15 16:35:34 +0000 | [diff] [blame] | 1641 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1642 | |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1643 | /* Make sure spell-check is case sensitive. */ |
Chris Allegretta | 1d8fa1f | 2002-12-10 00:53:21 +0000 | [diff] [blame] | 1644 | SET(CASE_SENSITIVE); |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1645 | |
David Lawrence Ramsey | 49d5c1b | 2004-10-11 13:55:33 +0000 | [diff] [blame] | 1646 | #ifndef NANO_SMALL |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1647 | /* Make sure spell-check goes forward only. */ |
| 1648 | UNSET(REVERSE_SEARCH); |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1649 | #endif |
David Lawrence Ramsey | b112fe6 | 2004-10-09 17:15:46 +0000 | [diff] [blame] | 1650 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1651 | /* Make sure spell-check doesn't use regular expressions. */ |
| 1652 | UNSET(USE_REGEXP); |
| 1653 | #endif |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1654 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1655 | /* Save the current search/replace strings. */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1656 | search_init_globals(); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1657 | save_search = last_search; |
| 1658 | save_replace = last_replace; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1659 | |
David Lawrence Ramsey | f615904 | 2004-11-17 20:47:14 +0000 | [diff] [blame] | 1660 | /* Set the search/replace strings to the misspelled word. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1661 | last_search = mallocstrcpy(NULL, word); |
| 1662 | last_replace = mallocstrcpy(NULL, word); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1663 | |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1664 | #ifndef NANO_SMALL |
| 1665 | if (old_mark_set) { |
David Lawrence Ramsey | f978f04 | 2004-11-04 16:45:48 +0000 | [diff] [blame] | 1666 | /* If the mark is on, partition the filestruct so that it |
David Lawrence Ramsey | cb09337 | 2004-11-06 00:18:27 +0000 | [diff] [blame] | 1667 | * contains only the marked text, keep track of whether the text |
| 1668 | * will have a magicline added when we're done correcting |
| 1669 | * misspelled words, and turn the mark off. */ |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1670 | mark_order((const filestruct **)&top, &top_x, |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 1671 | (const filestruct **)&bot, &bot_x, &right_side_up); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1672 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | cb09337 | 2004-11-06 00:18:27 +0000 | [diff] [blame] | 1673 | added_magicline = (filebot->data[0] != '\0'); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1674 | UNSET(MARK_ISSET); |
| 1675 | } |
| 1676 | #endif |
| 1677 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1678 | /* Start from the top of the file. */ |
David Lawrence Ramsey | 2cc2a57 | 2004-10-18 02:06:53 +0000 | [diff] [blame] | 1679 | edittop = fileage; |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1680 | current = fileage; |
David Lawrence Ramsey | 7a97e18 | 2004-10-30 01:03:15 +0000 | [diff] [blame] | 1681 | current_x = (size_t)-1; |
David Lawrence Ramsey | 2eb7459 | 2004-10-21 16:25:44 +0000 | [diff] [blame] | 1682 | placewewant = 0; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1683 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1684 | /* Find the first whole-word occurrence of word. */ |
David Lawrence Ramsey | e5e88fd | 2004-10-31 13:20:30 +0000 | [diff] [blame] | 1685 | findnextstr_wrap_reset(); |
David Lawrence Ramsey | 77b284a | 2004-10-27 02:21:01 +0000 | [diff] [blame] | 1686 | while (findnextstr(TRUE, TRUE, FALSE, fileage, 0, word, NULL)) { |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1687 | if (is_whole_word(current_x, current->data, word)) { |
| 1688 | edit_refresh(); |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1689 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1690 | do_replace_highlight(TRUE, word); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1691 | |
David Lawrence Ramsey | 4b7e3c3 | 2004-10-15 16:25:56 +0000 | [diff] [blame] | 1692 | /* Allow all instances of the word to be corrected. */ |
David Lawrence Ramsey | 9bf486f | 2004-10-26 20:58:30 +0000 | [diff] [blame] | 1693 | canceled = (statusq(FALSE, spell_list, word, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1694 | #ifndef NANO_SMALL |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1695 | NULL, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1696 | #endif |
David Lawrence Ramsey | 9bf486f | 2004-10-26 20:58:30 +0000 | [diff] [blame] | 1697 | _("Edit a replacement")) == -1); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1698 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1699 | do_replace_highlight(FALSE, word); |
Chris Allegretta | 1bc0c7e | 2002-01-08 15:00:24 +0000 | [diff] [blame] | 1700 | |
David Lawrence Ramsey | 9bf486f | 2004-10-26 20:58:30 +0000 | [diff] [blame] | 1701 | if (!canceled && strcmp(word, answer) != 0) { |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1702 | current_x--; |
David Lawrence Ramsey | 9bf486f | 2004-10-26 20:58:30 +0000 | [diff] [blame] | 1703 | do_replace_loop(word, current, ¤t_x, TRUE, |
| 1704 | &canceled); |
Chris Allegretta | 1bc0c7e | 2002-01-08 15:00:24 +0000 | [diff] [blame] | 1705 | } |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1706 | |
| 1707 | break; |
Chris Allegretta | 8083827 | 2001-12-02 06:03:22 +0000 | [diff] [blame] | 1708 | } |
David Lawrence Ramsey | 53752e8 | 2004-10-18 22:19:22 +0000 | [diff] [blame] | 1709 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1710 | |
David Lawrence Ramsey | 46c604a | 2004-11-05 23:55:58 +0000 | [diff] [blame] | 1711 | #ifndef NANO_SMALL |
| 1712 | if (old_mark_set) { |
David Lawrence Ramsey | 56cf034 | 2004-11-15 18:44:30 +0000 | [diff] [blame] | 1713 | /* If the mark was on and we added a magicline, remove it |
| 1714 | * now. */ |
David Lawrence Ramsey | cb09337 | 2004-11-06 00:18:27 +0000 | [diff] [blame] | 1715 | if (added_magicline) |
| 1716 | remove_magicline(); |
David Lawrence Ramsey | 46c604a | 2004-11-05 23:55:58 +0000 | [diff] [blame] | 1717 | |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 1718 | /* Put the beginning and the end of the mark at the beginning |
| 1719 | * and the end of the spell-checked text. */ |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 1720 | if (fileage == filebot) |
David Lawrence Ramsey | f615904 | 2004-11-17 20:47:14 +0000 | [diff] [blame] | 1721 | bot_x += top_x; |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 1722 | if (right_side_up) { |
David Lawrence Ramsey | f615904 | 2004-11-17 20:47:14 +0000 | [diff] [blame] | 1723 | mark_beginx = top_x; |
| 1724 | current_x_save = bot_x; |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 1725 | } else { |
David Lawrence Ramsey | f615904 | 2004-11-17 20:47:14 +0000 | [diff] [blame] | 1726 | current_x_save = top_x; |
| 1727 | mark_beginx = bot_x; |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 1728 | } |
David Lawrence Ramsey | cb09337 | 2004-11-06 00:18:27 +0000 | [diff] [blame] | 1729 | |
David Lawrence Ramsey | 56cf034 | 2004-11-15 18:44:30 +0000 | [diff] [blame] | 1730 | /* Unpartition the filestruct so that it contains all the text |
| 1731 | * again, and turn the mark back on. */ |
David Lawrence Ramsey | 74d8707 | 2004-11-22 00:16:23 +0000 | [diff] [blame] | 1732 | unpartition_filestruct(&filepart); |
David Lawrence Ramsey | cb09337 | 2004-11-06 00:18:27 +0000 | [diff] [blame] | 1733 | SET(MARK_ISSET); |
David Lawrence Ramsey | 46c604a | 2004-11-05 23:55:58 +0000 | [diff] [blame] | 1734 | } |
| 1735 | #endif |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 1736 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1737 | /* Restore the search/replace strings. */ |
| 1738 | free(last_search); |
| 1739 | last_search = save_search; |
| 1740 | free(last_replace); |
| 1741 | last_replace = save_replace; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1742 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1743 | /* Restore where we were. */ |
David Lawrence Ramsey | 2cc2a57 | 2004-10-18 02:06:53 +0000 | [diff] [blame] | 1744 | edittop = edittop_save; |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1745 | current = current_save; |
| 1746 | current_x = current_x_save; |
David Lawrence Ramsey | 2eb7459 | 2004-10-21 16:25:44 +0000 | [diff] [blame] | 1747 | placewewant = pww_save; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1748 | |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1749 | /* Restore case sensitivity setting. */ |
| 1750 | if (!case_sens_set) |
| 1751 | UNSET(CASE_SENSITIVE); |
| 1752 | |
David Lawrence Ramsey | 49d5c1b | 2004-10-11 13:55:33 +0000 | [diff] [blame] | 1753 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1754 | /* Restore search/replace direction. */ |
Chris Allegretta | 23b74b2 | 2002-01-21 20:32:22 +0000 | [diff] [blame] | 1755 | if (reverse_search_set) |
| 1756 | SET(REVERSE_SEARCH); |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1757 | #endif |
David Lawrence Ramsey | b112fe6 | 2004-10-09 17:15:46 +0000 | [diff] [blame] | 1758 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | d93ee68 | 2004-10-09 16:59:43 +0000 | [diff] [blame] | 1759 | /* Restore regular expression usage setting. */ |
| 1760 | if (regexp_set) |
| 1761 | SET(USE_REGEXP); |
| 1762 | #endif |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1763 | |
David Lawrence Ramsey | 9bf486f | 2004-10-26 20:58:30 +0000 | [diff] [blame] | 1764 | return !canceled; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1765 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1766 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1767 | /* Integrated spell checking using 'spell' program. Return value: NULL |
| 1768 | * for normal termination, otherwise the error string. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1769 | const char *do_int_speller(const char *tempfile_name) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1770 | { |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1771 | char *read_buff, *read_buff_ptr, *read_buff_word; |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1772 | size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread; |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1773 | 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] | 1774 | pid_t pid_spell, pid_sort, pid_uniq; |
| 1775 | int spell_status, sort_status, uniq_status; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1776 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1777 | /* Create all three pipes up front. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1778 | if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || pipe(uniq_fd) == -1) |
| 1779 | return _("Could not create pipe"); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1780 | |
Chris Allegretta | 2a7a9a2 | 2002-12-10 00:16:27 +0000 | [diff] [blame] | 1781 | statusbar(_("Creating misspelled word list, please wait...")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1782 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1783 | /* A new process to run spell in. */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1784 | if ((pid_spell = fork()) == 0) { |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1785 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1786 | /* Child continues (i.e, future spell process). */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1787 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1788 | close(spell_fd[0]); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1789 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1790 | /* Replace the standard input with the temp file. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1791 | if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1) |
| 1792 | goto close_pipes_and_exit; |
| 1793 | |
| 1794 | if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) |
| 1795 | goto close_pipes_and_exit; |
| 1796 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1797 | close(tempfile_fd); |
| 1798 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1799 | /* Send spell's standard output to the pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1800 | if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 1801 | goto close_pipes_and_exit; |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1802 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1803 | close(spell_fd[1]); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1804 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1805 | /* Start spell program; we are using PATH. */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1806 | execlp("spell", "spell", NULL); |
| 1807 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1808 | /* Should not be reached, if spell is found. */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1809 | exit(1); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1812 | /* Parent continues here. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1813 | close(spell_fd[1]); |
| 1814 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1815 | /* A new process to run sort in. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1816 | if ((pid_sort = fork()) == 0) { |
| 1817 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1818 | /* Child continues (i.e, future spell process). Replace the |
| 1819 | * standard input with the standard output of the old pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1820 | if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 1821 | goto close_pipes_and_exit; |
| 1822 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1823 | close(spell_fd[0]); |
| 1824 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1825 | /* Send sort's standard output to the new pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1826 | if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 1827 | goto close_pipes_and_exit; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1828 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1829 | close(sort_fd[1]); |
| 1830 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1831 | /* Start sort program. Use -f to remove mixed case without |
| 1832 | * having to have ANOTHER pipe for tr. If this isn't portable, |
| 1833 | * let me know. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1834 | execlp("sort", "sort", "-f", NULL); |
| 1835 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1836 | /* Should not be reached, if sort is found. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1837 | exit(1); |
| 1838 | } |
| 1839 | |
Chris Allegretta | 2d5fc3a | 2003-01-16 03:11:23 +0000 | [diff] [blame] | 1840 | close(spell_fd[0]); |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1841 | close(sort_fd[1]); |
| 1842 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1843 | /* A new process to run uniq in. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1844 | if ((pid_uniq = fork()) == 0) { |
| 1845 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1846 | /* Child continues (i.e, future uniq process). Replace the |
| 1847 | * standard input with the standard output of the old pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1848 | if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 1849 | goto close_pipes_and_exit; |
| 1850 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1851 | close(sort_fd[0]); |
| 1852 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1853 | /* Send uniq's standard output to the new pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1854 | if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 1855 | goto close_pipes_and_exit; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1856 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1857 | close(uniq_fd[1]); |
| 1858 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1859 | /* Start uniq program; we are using PATH. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1860 | execlp("uniq", "uniq", NULL); |
| 1861 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1862 | /* Should not be reached, if uniq is found. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1863 | exit(1); |
| 1864 | } |
| 1865 | |
Chris Allegretta | 2d5fc3a | 2003-01-16 03:11:23 +0000 | [diff] [blame] | 1866 | close(sort_fd[0]); |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1867 | close(uniq_fd[1]); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1868 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1869 | /* Child process was not forked successfully. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1870 | if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) { |
| 1871 | close(uniq_fd[0]); |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1872 | return _("Could not fork"); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1875 | /* Get system pipe buffer size. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1876 | if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) { |
| 1877 | close(uniq_fd[0]); |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1878 | return _("Could not get size of pipe buffer"); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1879 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1880 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1881 | /* Read in the returned spelling errors. */ |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1882 | read_buff_read = 0; |
| 1883 | read_buff_size = pipe_buff_size + 1; |
| 1884 | read_buff = read_buff_ptr = charalloc(read_buff_size); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1885 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1886 | 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] | 1887 | read_buff_read += bytesread; |
| 1888 | read_buff_size += pipe_buff_size; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1889 | read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size); |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1890 | read_buff_ptr += read_buff_read; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1891 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1892 | } |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1893 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1894 | *read_buff_ptr = (char)NULL; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1895 | close(uniq_fd[0]); |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1896 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1897 | /* Process the spelling errors. */ |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1898 | read_buff_word = read_buff_ptr = read_buff; |
| 1899 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1900 | while (*read_buff_ptr != '\0') { |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1901 | |
| 1902 | if ((*read_buff_ptr == '\n') || (*read_buff_ptr == '\r')) { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1903 | *read_buff_ptr = (char)NULL; |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1904 | if (read_buff_word != read_buff_ptr) { |
| 1905 | if (!do_int_spell_fix(read_buff_word)) { |
| 1906 | read_buff_word = read_buff_ptr; |
| 1907 | break; |
| 1908 | } |
| 1909 | } |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1910 | read_buff_word = read_buff_ptr + 1; |
| 1911 | } |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1912 | read_buff_ptr++; |
| 1913 | } |
| 1914 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1915 | /* Special case where last word doesn't end with \n or \r. */ |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1916 | if (read_buff_word != read_buff_ptr) |
| 1917 | do_int_spell_fix(read_buff_word); |
| 1918 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1919 | free(read_buff); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1920 | replace_abort(); |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1921 | edit_refresh(); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1922 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1923 | /* Process end of spell process. */ |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1924 | waitpid(pid_spell, &spell_status, 0); |
| 1925 | waitpid(pid_sort, &sort_status, 0); |
| 1926 | waitpid(pid_uniq, &uniq_status, 0); |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1927 | |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1928 | if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status)) |
| 1929 | return _("Error invoking \"spell\""); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1930 | |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1931 | if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status)) |
| 1932 | return _("Error invoking \"sort -f\""); |
| 1933 | |
| 1934 | if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status)) |
| 1935 | return _("Error invoking \"uniq\""); |
| 1936 | |
| 1937 | /* Otherwise... */ |
| 1938 | return NULL; |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1939 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1940 | close_pipes_and_exit: |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1941 | /* Don't leak any handles. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1942 | close(tempfile_fd); |
| 1943 | close(spell_fd[0]); |
| 1944 | close(spell_fd[1]); |
| 1945 | close(sort_fd[0]); |
| 1946 | close(sort_fd[1]); |
| 1947 | close(uniq_fd[0]); |
| 1948 | close(uniq_fd[1]); |
| 1949 | exit(1); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1950 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1951 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1952 | /* External spell checking. Return value: NULL for normal termination, |
| 1953 | * otherwise the error string. */ |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1954 | const char *do_alt_speller(char *tempfile_name) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1955 | { |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 1956 | int alt_spell_status, lineno_save = current->lineno; |
| 1957 | size_t current_x_save = current_x, pww_save = placewewant; |
| 1958 | int current_y_save = current_y; |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1959 | pid_t pid_spell; |
Chris Allegretta | 169ee84 | 2001-01-26 01:57:32 +0000 | [diff] [blame] | 1960 | char *ptr; |
| 1961 | static int arglen = 3; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 1962 | static char **spellargs = NULL; |
| 1963 | FILE *f; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1964 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 1965 | bool old_mark_set = ISSET(MARK_ISSET); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1966 | bool added_magicline = FALSE; |
| 1967 | /* Whether we added a magicline after filebot. */ |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 1968 | bool right_side_up = FALSE; |
| 1969 | /* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark, |
| 1970 | * FALSE if (current, current_x) is. */ |
David Lawrence Ramsey | f615904 | 2004-11-17 20:47:14 +0000 | [diff] [blame] | 1971 | filestruct *top, *bot; |
| 1972 | size_t top_x, bot_x; |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 1973 | int mbb_lineno_save = 0; |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1974 | /* 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] | 1975 | * the alternate spell command. The line that mark_beginbuf |
| 1976 | * points to will be freed, so we save the line number and |
| 1977 | * restore afterwards. */ |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1978 | long old_totsize = totsize; |
| 1979 | /* Our saved value of totsize, used when we spell-check a marked |
| 1980 | * selection. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1981 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 1982 | if (old_mark_set) { |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 1983 | /* If the mark is on, save the number of the line it starts on, |
| 1984 | * and then turn the mark off. */ |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 1985 | mbb_lineno_save = mark_beginbuf->lineno; |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1986 | UNSET(MARK_ISSET); |
| 1987 | } |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1988 | #endif |
| 1989 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1990 | endwin(); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1991 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1992 | /* Set up an argument list to pass execvp(). */ |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 1993 | if (spellargs == NULL) { |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1994 | spellargs = (char **)nmalloc(arglen * sizeof(char *)); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1995 | |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 1996 | spellargs[0] = strtok(alt_speller, " "); |
| 1997 | while ((ptr = strtok(NULL, " ")) != NULL) { |
| 1998 | arglen++; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1999 | spellargs = (char **)nrealloc(spellargs, arglen * sizeof(char *)); |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 2000 | spellargs[arglen - 3] = ptr; |
Chris Allegretta | 169ee84 | 2001-01-26 01:57:32 +0000 | [diff] [blame] | 2001 | } |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 2002 | spellargs[arglen - 1] = NULL; |
| 2003 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2004 | spellargs[arglen - 2] = tempfile_name; |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 2005 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2006 | /* Start a new process for the alternate speller. */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 2007 | if ((pid_spell = fork()) == 0) { |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2008 | /* Start alternate spell program; we are using PATH. */ |
Chris Allegretta | 169ee84 | 2001-01-26 01:57:32 +0000 | [diff] [blame] | 2009 | execvp(spellargs[0], spellargs); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 2010 | |
| 2011 | /* Should not be reached, if alternate speller is found!!! */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 2012 | exit(1); |
| 2013 | } |
| 2014 | |
| 2015 | /* Could not fork?? */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 2016 | if (pid_spell < 0) |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 2017 | return _("Could not fork"); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 2018 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2019 | /* Wait for alternate speller to complete. */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 2020 | wait(&alt_spell_status); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2021 | |
David Lawrence Ramsey | 6a2f068 | 2004-12-20 01:13:55 +0000 | [diff] [blame] | 2022 | if (!WIFEXITED(alt_spell_status) || |
| 2023 | WEXITSTATUS(alt_spell_status) != 0) { |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 2024 | char *altspell_error = NULL; |
| 2025 | char *invoke_error = _("Could not invoke \"%s\""); |
David Lawrence Ramsey | f3bea02 | 2004-12-20 01:18:49 +0000 | [diff] [blame] | 2026 | int msglen = strlen(invoke_error) + strlen(alt_speller) + 2; |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 2027 | |
| 2028 | altspell_error = charalloc(msglen); |
| 2029 | snprintf(altspell_error, msglen, invoke_error, alt_speller); |
| 2030 | return altspell_error; |
| 2031 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 2032 | |
Chris Allegretta | 8f6c069 | 2000-07-19 01:16:18 +0000 | [diff] [blame] | 2033 | refresh(); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2034 | |
David Lawrence Ramsey | 439fbe3 | 2004-10-16 04:56:34 +0000 | [diff] [blame] | 2035 | /* Restore the terminal to its previous state. */ |
| 2036 | terminal_init(); |
| 2037 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2038 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2039 | if (old_mark_set) { |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2040 | long part_totsize; |
| 2041 | |
| 2042 | /* If the mark was on, partition the filestruct so that it |
| 2043 | * contains only the marked text, and keep track of whether the |
| 2044 | * temp file (which should contain the spell-checked marked |
| 2045 | * text) will have a magicline added when it's reloaded. */ |
| 2046 | mark_order((const filestruct **)&top, &top_x, |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 2047 | (const filestruct **)&bot, &bot_x, &right_side_up); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2048 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
| 2049 | added_magicline = (filebot->data[0] != '\0'); |
| 2050 | |
David Lawrence Ramsey | 3f35864 | 2004-11-22 16:04:18 +0000 | [diff] [blame] | 2051 | /* Get the number of characters in the marked text, and subtract |
| 2052 | * it from the saved value of totsize. Note that we don't need |
| 2053 | * to save totlines. */ |
| 2054 | get_totals(top, bot, NULL, &part_totsize); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2055 | old_totsize -= part_totsize; |
| 2056 | } |
| 2057 | #endif |
| 2058 | |
| 2059 | /* Reinitialize the filestruct. */ |
| 2060 | free_filestruct(fileage); |
| 2061 | global_init(TRUE); |
| 2062 | |
| 2063 | /* Reload the temp file. Do what load_buffer() would do, except for |
| 2064 | * making a new buffer for the temp file if multibuffer support is |
| 2065 | * available. */ |
| 2066 | open_file(tempfile_name, FALSE, &f); |
| 2067 | read_file(f, tempfile_name); |
| 2068 | current = fileage; |
| 2069 | |
| 2070 | #ifndef NANO_SMALL |
| 2071 | if (old_mark_set) { |
David Lawrence Ramsey | 1539837 | 2004-11-05 15:03:12 +0000 | [diff] [blame] | 2072 | filestruct *top_save = fileage; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2073 | |
David Lawrence Ramsey | 56cf034 | 2004-11-15 18:44:30 +0000 | [diff] [blame] | 2074 | /* If the mark was on and we added a magicline, remove it |
| 2075 | * now. */ |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2076 | if (added_magicline) |
| 2077 | remove_magicline(); |
| 2078 | |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 2079 | /* Put the beginning and the end of the mark at the beginning |
| 2080 | * and the end of the spell-checked text. */ |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 2081 | if (fileage == filebot) |
David Lawrence Ramsey | f615904 | 2004-11-17 20:47:14 +0000 | [diff] [blame] | 2082 | bot_x += top_x; |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 2083 | if (right_side_up) { |
David Lawrence Ramsey | f615904 | 2004-11-17 20:47:14 +0000 | [diff] [blame] | 2084 | mark_beginx = top_x; |
| 2085 | current_x_save = bot_x; |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 2086 | } else { |
David Lawrence Ramsey | f615904 | 2004-11-17 20:47:14 +0000 | [diff] [blame] | 2087 | current_x_save = top_x; |
| 2088 | mark_beginx = bot_x; |
David Lawrence Ramsey | bde3e0e | 2004-11-06 02:37:25 +0000 | [diff] [blame] | 2089 | } |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 2090 | |
David Lawrence Ramsey | 56cf034 | 2004-11-15 18:44:30 +0000 | [diff] [blame] | 2091 | /* Unpartition the filestruct so that it contains all the text |
| 2092 | * again. Note that we've replaced the marked text originally |
| 2093 | * in the partition with the spell-checked marked text in the |
| 2094 | * temp file. */ |
David Lawrence Ramsey | 74d8707 | 2004-11-22 00:16:23 +0000 | [diff] [blame] | 2095 | unpartition_filestruct(&filepart); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2096 | |
| 2097 | /* Renumber starting with the beginning line of the old |
David Lawrence Ramsey | 3f35864 | 2004-11-22 16:04:18 +0000 | [diff] [blame] | 2098 | * partition. Also set totlines to the new number of lines in |
| 2099 | * the file, add the number of characters in the spell-checked |
| 2100 | * marked text to the saved value of totsize, and then make that |
| 2101 | * saved value the actual value. */ |
David Lawrence Ramsey | 1539837 | 2004-11-05 15:03:12 +0000 | [diff] [blame] | 2102 | renumber(top_save); |
David Lawrence Ramsey | 3f35864 | 2004-11-22 16:04:18 +0000 | [diff] [blame] | 2103 | totlines = filebot->lineno; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2104 | old_totsize += totsize; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2105 | totsize = old_totsize; |
| 2106 | |
| 2107 | /* Assign mark_beginbuf to the line where the mark began |
| 2108 | * before. */ |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 2109 | do_gotopos(mbb_lineno_save, mark_beginx, current_y_save, 0); |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2110 | mark_beginbuf = current; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2111 | |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 2112 | /* Assign mark_beginx to the location in mark_beginbuf where the |
| 2113 | * mark began before, adjusted for any shortening of the |
| 2114 | * line. */ |
| 2115 | mark_beginx = current_x; |
| 2116 | |
| 2117 | /* Turn the mark back on. */ |
| 2118 | SET(MARK_ISSET); |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2119 | } |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 2120 | #endif |
| 2121 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2122 | /* Go back to the old position, mark the file as modified, and make |
| 2123 | * sure that the titlebar is refreshed. */ |
David Lawrence Ramsey | 90e59c1 | 2004-11-05 23:03:03 +0000 | [diff] [blame] | 2124 | do_gotopos(lineno_save, current_x_save, current_y_save, pww_save); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2125 | set_modified(); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2126 | clearok(topwin, FALSE); |
| 2127 | titlebar(NULL); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 2128 | |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 2129 | return NULL; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 2130 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 2131 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2132 | void do_spell(void) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 2133 | { |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2134 | int i; |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2135 | char *temp = safe_tempnam(0, "nano."); |
| 2136 | const char *spell_msg; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 2137 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2138 | if (temp == NULL) { |
| 2139 | statusbar(_("Could not create temp file: %s"), strerror(errno)); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2140 | return; |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 2141 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 2142 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2143 | #ifndef NANO_SMALL |
| 2144 | if (ISSET(MARK_ISSET)) |
David Lawrence Ramsey | 2f0d03b | 2004-05-28 00:15:28 +0000 | [diff] [blame] | 2145 | i = write_marked(temp, TRUE, FALSE); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2146 | else |
| 2147 | #endif |
David Lawrence Ramsey | 2f0d03b | 2004-05-28 00:15:28 +0000 | [diff] [blame] | 2148 | i = write_file(temp, TRUE, FALSE, FALSE); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2149 | |
| 2150 | if (i == -1) { |
David Lawrence Ramsey | 95e39e5 | 2004-08-12 02:52:14 +0000 | [diff] [blame] | 2151 | statusbar(_("Error writing temp file: %s"), strerror(errno)); |
Chris Allegretta | bef1297 | 2002-03-06 03:30:40 +0000 | [diff] [blame] | 2152 | free(temp); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2153 | return; |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 2154 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 2155 | |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2156 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2157 | /* Update the current open_files entry before spell-checking, in |
| 2158 | * case any problems occur. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2159 | add_open_file(TRUE); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2160 | #endif |
| 2161 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2162 | spell_msg = alt_speller != NULL ? do_alt_speller(temp) : |
| 2163 | do_int_speller(temp); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 2164 | unlink(temp); |
Chris Allegretta | fdcb9e9 | 2003-02-12 02:52:04 +0000 | [diff] [blame] | 2165 | free(temp); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 2166 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2167 | if (spell_msg != NULL) |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2168 | statusbar(_("Spell checking failed: %s: %s"), spell_msg, |
| 2169 | strerror(errno)); |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2170 | else |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2171 | statusbar(_("Finished checking spelling")); |
Chris Allegretta | 67105eb | 2000-07-03 03:18:32 +0000 | [diff] [blame] | 2172 | } |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 2173 | #endif /* !DISABLE_SPELLER */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2174 | |
David Lawrence Ramsey | 44e7f82 | 2004-03-30 04:17:10 +0000 | [diff] [blame] | 2175 | #if !defined(NANO_SMALL) || !defined(DISABLE_JUSTIFY) |
David Lawrence Ramsey | 8d91199 | 2004-05-29 17:05:52 +0000 | [diff] [blame] | 2176 | /* The "indentation" of a line is the whitespace between the quote part |
| 2177 | * and the non-whitespace of the line. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2178 | size_t indent_length(const char *line) |
| 2179 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2180 | size_t len = 0; |
| 2181 | |
| 2182 | assert(line != NULL); |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 2183 | while (is_blank_char(*line)) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2184 | line++; |
| 2185 | len++; |
| 2186 | } |
| 2187 | return len; |
| 2188 | } |
David Lawrence Ramsey | 44e7f82 | 2004-03-30 04:17:10 +0000 | [diff] [blame] | 2189 | #endif /* !NANO_SMALL || !DISABLE_JUSTIFY */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2190 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 2191 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | f613ca7 | 2004-05-13 22:23:58 +0000 | [diff] [blame] | 2192 | /* justify_format() replaces Tab by Space and multiple spaces by 1 |
David Lawrence Ramsey | fd73c46 | 2004-09-11 21:28:36 +0000 | [diff] [blame] | 2193 | * (except it maintains 2 after a non-repeated character in punct |
| 2194 | * followed by a character in brackets). Note that the terminating \0 |
| 2195 | * counts as a space. |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2196 | * |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2197 | * justify_format() might make line->data shorter, and change the actual |
| 2198 | * pointer with null_at(). |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2199 | * |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 2200 | * 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] | 2201 | * skip should be at most strlen(line->data). The character at |
| 2202 | * line[skip + 1] must not be whitespace. */ |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2203 | void justify_format(filestruct *line, size_t skip) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2204 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2205 | char *back, *front; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2206 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2207 | /* These four asserts are assumptions about the input data. */ |
| 2208 | assert(line != NULL); |
| 2209 | assert(line->data != NULL); |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 2210 | assert(skip < strlen(line->data)); |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 2211 | assert(!is_blank_char(line->data[skip])); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2212 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2213 | back = line->data + skip; |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 2214 | for (front = back; ; front++) { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2215 | bool remove_space = FALSE; |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 2216 | /* Do we want to remove this space? */ |
| 2217 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2218 | if (*front == '\t') |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2219 | *front = ' '; |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2220 | |
David Lawrence Ramsey | 021960d | 2004-05-13 23:19:01 +0000 | [diff] [blame] | 2221 | /* These tests are safe since line->data + skip is not a |
| 2222 | * space. */ |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 2223 | if ((*front == '\0' || *front == ' ') && *(front - 1) == ' ') { |
David Lawrence Ramsey | fd73c46 | 2004-09-11 21:28:36 +0000 | [diff] [blame] | 2224 | const char *bob = back - 2; |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 2225 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2226 | remove_space = TRUE; |
David Lawrence Ramsey | fd73c46 | 2004-09-11 21:28:36 +0000 | [diff] [blame] | 2227 | for (; bob >= line->data + skip; bob--) { |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 2228 | if (strchr(punct, *bob) != NULL) { |
David Lawrence Ramsey | fd73c46 | 2004-09-11 21:28:36 +0000 | [diff] [blame] | 2229 | /* If this character is in punct, don't remove the |
| 2230 | * space unless this character and the character |
| 2231 | * before it are the same. */ |
| 2232 | remove_space = (bob > line->data + skip && |
| 2233 | *bob == *(bob - 1)); |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 2234 | break; |
| 2235 | } |
| 2236 | if (strchr(brackets, *bob) == NULL) |
| 2237 | break; |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | if (remove_space) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2242 | /* 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] | 2243 | * simply failing to assign it to *back. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2244 | #ifndef NANO_SMALL |
| 2245 | if (mark_beginbuf == line && back - line->data < mark_beginx) |
| 2246 | mark_beginx--; |
| 2247 | #endif |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 2248 | if (*front == '\0') |
| 2249 | *(back - 1) = '\0'; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2250 | } else { |
| 2251 | *back = *front; |
| 2252 | back++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2253 | } |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 2254 | if (*front == '\0') |
| 2255 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2256 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2257 | |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 2258 | back--; |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 2259 | assert(*back == '\0' && *front == '\0'); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2260 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2261 | /* Now back is the new end of line->data. */ |
| 2262 | if (back != front) { |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 2263 | totsize -= front - back; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2264 | null_at(&line->data, back - line->data); |
| 2265 | #ifndef NANO_SMALL |
| 2266 | if (mark_beginbuf == line && back - line->data < mark_beginx) |
| 2267 | mark_beginx = back - line->data; |
| 2268 | #endif |
| 2269 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2270 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2271 | |
| 2272 | /* The "quote part" of a line is the largest initial substring matching |
| 2273 | * the quote string. This function returns the length of the quote part |
| 2274 | * of the given line. |
| 2275 | * |
| 2276 | * Note that if !HAVE_REGEX_H then we match concatenated copies of |
| 2277 | * quotestr. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2278 | size_t quote_length(const char *line) |
| 2279 | { |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2280 | #ifdef HAVE_REGEX_H |
| 2281 | regmatch_t matches; |
| 2282 | int rc = regexec("ereg, line, 1, &matches, 0); |
| 2283 | |
| 2284 | if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1) |
| 2285 | return 0; |
| 2286 | /* matches.rm_so should be 0, since the quote string should start |
| 2287 | * with the caret ^. */ |
| 2288 | return matches.rm_eo; |
| 2289 | #else /* !HAVE_REGEX_H */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2290 | size_t qdepth = 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2291 | |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 2292 | /* Compute quote depth level. */ |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2293 | while (strncmp(line + qdepth, quotestr, quotelen) == 0) |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2294 | qdepth += quotelen; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2295 | return qdepth; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2296 | #endif /* !HAVE_REGEX_H */ |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2297 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2298 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2299 | /* a_line and b_line are lines of text. The quotation part of a_line is |
| 2300 | * the first a_quote characters. Check that the quotation part of |
| 2301 | * b_line is the same. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2302 | bool quotes_match(const char *a_line, size_t a_quote, const char |
| 2303 | *b_line) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2304 | { |
David Lawrence Ramsey | 6a2f068 | 2004-12-20 01:13:55 +0000 | [diff] [blame] | 2305 | /* Here is the assumption about a_quote. */ |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2306 | assert(a_quote == quote_length(a_line)); |
David Lawrence Ramsey | 6a2f068 | 2004-12-20 01:13:55 +0000 | [diff] [blame] | 2307 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2308 | return a_quote == quote_length(b_line) && |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2309 | strncmp(a_line, b_line, a_quote) == 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2312 | /* We assume a_line and b_line have no quote part. Then, we return |
| 2313 | * whether b_line could follow a_line in a paragraph. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2314 | 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] | 2315 | *b_line, size_t b_indent) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2316 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2317 | assert(a_indent == indent_length(a_line)); |
| 2318 | assert(b_indent == indent_length(b_line)); |
| 2319 | |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2320 | return b_indent <= a_indent && |
| 2321 | strncmp(a_line, b_line, b_indent) == 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2324 | /* Is foo the beginning of a paragraph? |
| 2325 | * |
| 2326 | * A line of text consists of a "quote part", followed by an |
| 2327 | * "indentation part", followed by text. The functions quote_length() |
| 2328 | * and indent_length() calculate these parts. |
| 2329 | * |
| 2330 | * A line is "part of a paragraph" if it has a part not in the quote |
| 2331 | * part or the indentation. |
| 2332 | * |
| 2333 | * A line is "the beginning of a paragraph" if it is part of a |
| 2334 | * paragraph and |
| 2335 | * 1) it is the top line of the file, or |
| 2336 | * 2) the line above it is not part of a paragraph, or |
| 2337 | * 3) the line above it does not have precisely the same quote |
| 2338 | * part, or |
| 2339 | * 4) the indentation of this line is not an initial substring of |
| 2340 | * the indentation of the previous line, or |
| 2341 | * 5) this line has no quote part and some indentation, and |
| 2342 | * AUTOINDENT is not set. |
| 2343 | * The reason for number 5) is that if AUTOINDENT is not set, then an |
| 2344 | * indented line is expected to start a paragraph, like in books. |
| 2345 | * Thus, nano can justify an indented paragraph only if AUTOINDENT is |
| 2346 | * turned on. */ |
| 2347 | bool begpar(const filestruct *const foo) |
| 2348 | { |
| 2349 | size_t quote_len; |
| 2350 | size_t indent_len; |
| 2351 | size_t temp_id_len; |
| 2352 | |
| 2353 | /* Case 1). */ |
| 2354 | if (foo->prev == NULL) |
| 2355 | return TRUE; |
| 2356 | |
| 2357 | quote_len = quote_length(foo->data); |
| 2358 | indent_len = indent_length(foo->data + quote_len); |
| 2359 | |
| 2360 | /* Not part of a paragraph. */ |
| 2361 | if (foo->data[quote_len + indent_len] == '\0') |
| 2362 | return FALSE; |
| 2363 | |
| 2364 | /* Case 3). */ |
| 2365 | if (!quotes_match(foo->data, quote_len, foo->prev->data)) |
| 2366 | return TRUE; |
| 2367 | |
| 2368 | temp_id_len = indent_length(foo->prev->data + quote_len); |
| 2369 | |
| 2370 | /* Case 2) or 5) or 4). */ |
| 2371 | if (foo->prev->data[quote_len + temp_id_len] == '\0' || |
| 2372 | (quote_len == 0 && indent_len > 0 |
| 2373 | #ifndef NANO_SMALL |
| 2374 | && !ISSET(AUTOINDENT) |
| 2375 | #endif |
| 2376 | ) || !indents_match(foo->prev->data + quote_len, temp_id_len, |
| 2377 | foo->data + quote_len, indent_len)) |
| 2378 | return TRUE; |
| 2379 | |
| 2380 | return FALSE; |
| 2381 | } |
| 2382 | |
| 2383 | /* We find the last beginning-of-paragraph line before the current |
| 2384 | * line. */ |
| 2385 | void do_para_begin(void) |
| 2386 | { |
David Lawrence Ramsey | 52e5f23 | 2005-01-14 21:26:38 +0000 | [diff] [blame^] | 2387 | const filestruct *current_save = current; |
| 2388 | const size_t pww_save = placewewant; |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2389 | |
| 2390 | current_x = 0; |
| 2391 | placewewant = 0; |
| 2392 | |
| 2393 | if (current->prev != NULL) { |
| 2394 | do { |
| 2395 | current = current->prev; |
David Lawrence Ramsey | 50c7f2d | 2004-08-27 17:02:05 +0000 | [diff] [blame] | 2396 | current_y--; |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2397 | } while (!begpar(current)); |
| 2398 | } |
| 2399 | |
David Lawrence Ramsey | 52e5f23 | 2005-01-14 21:26:38 +0000 | [diff] [blame^] | 2400 | edit_redraw(current_save, pww_save); |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | bool inpar(const char *str) |
| 2404 | { |
| 2405 | size_t quote_len = quote_length(str); |
| 2406 | |
| 2407 | return str[quote_len + indent_length(str + quote_len)] != '\0'; |
| 2408 | } |
| 2409 | |
| 2410 | /* A line is the last line of a paragraph if it is in a paragraph, and |
| 2411 | * the next line isn't, or is the beginning of a paragraph. We move |
| 2412 | * down to the end of a paragraph, then one line farther. */ |
| 2413 | void do_para_end(void) |
| 2414 | { |
David Lawrence Ramsey | 52e5f23 | 2005-01-14 21:26:38 +0000 | [diff] [blame^] | 2415 | const filestruct *const current_save = current; |
| 2416 | const size_t pww_save = placewewant; |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2417 | |
| 2418 | current_x = 0; |
| 2419 | placewewant = 0; |
| 2420 | |
| 2421 | while (current->next != NULL && !inpar(current->data)) |
| 2422 | current = current->next; |
| 2423 | |
| 2424 | while (current->next != NULL && inpar(current->next->data) && |
David Lawrence Ramsey | 50c7f2d | 2004-08-27 17:02:05 +0000 | [diff] [blame] | 2425 | !begpar(current->next)) { |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2426 | current = current->next; |
David Lawrence Ramsey | 50c7f2d | 2004-08-27 17:02:05 +0000 | [diff] [blame] | 2427 | current_y++; |
| 2428 | } |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2429 | |
| 2430 | if (current->next != NULL) |
| 2431 | current = current->next; |
| 2432 | |
David Lawrence Ramsey | 52e5f23 | 2005-01-14 21:26:38 +0000 | [diff] [blame^] | 2433 | edit_redraw(current_save, pww_save); |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2436 | /* Put the next par_len lines, starting with first_line, into the |
| 2437 | * justify buffer, leaving copies of those lines in place. Assume there |
| 2438 | * are enough lines after first_line. Return the new copy of |
| 2439 | * first_line. */ |
David Lawrence Ramsey | d4693cb | 2004-05-14 01:17:25 +0000 | [diff] [blame] | 2440 | filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t |
| 2441 | quote_len) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2442 | { |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2443 | filestruct *top = first_line; |
| 2444 | /* The top of the paragraph we're backing up. */ |
| 2445 | filestruct *bot = first_line; |
| 2446 | /* The bottom of the paragraph we're backing up. */ |
| 2447 | size_t i; |
| 2448 | /* Generic loop variable. */ |
| 2449 | size_t current_x_save = current_x; |
| 2450 | int fl_lineno_save = first_line->lineno; |
| 2451 | int edittop_lineno_save = edittop->lineno; |
| 2452 | int current_lineno_save = current->lineno; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2453 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2454 | bool old_mark_set = ISSET(MARK_ISSET); |
| 2455 | int mbb_lineno_save = 0; |
| 2456 | |
| 2457 | if (old_mark_set) |
| 2458 | mbb_lineno_save = mark_beginbuf->lineno; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2459 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2460 | |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2461 | /* Move bot down par_len lines to the newline after the last line of |
| 2462 | * the paragraph. */ |
| 2463 | for (i = par_len; i > 0; i--) |
| 2464 | bot = bot->next; |
| 2465 | |
| 2466 | /* Move the paragraph from the main filestruct to the justify |
| 2467 | * buffer. */ |
| 2468 | move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot, 0); |
| 2469 | |
| 2470 | /* Copy the paragraph from the justify buffer to the main |
| 2471 | * filestruct. */ |
| 2472 | copy_from_filestruct(jusbuffer, jusbottom); |
| 2473 | |
| 2474 | /* Move upward from the last line of the paragraph to the first |
| 2475 | * line, putting first_line, edittop, current, and mark_beginbuf at |
| 2476 | * the same lines in the copied paragraph that they had in the |
| 2477 | * original paragraph. */ |
| 2478 | top = current->prev; |
| 2479 | for (i = par_len; i > 0; i--) { |
| 2480 | if (top->lineno == fl_lineno_save) |
| 2481 | first_line = top; |
| 2482 | if (top->lineno == edittop_lineno_save) |
| 2483 | edittop = top; |
| 2484 | if (top->lineno == current_lineno_save) |
| 2485 | current = top; |
| 2486 | #ifndef NANO_SMALL |
| 2487 | if (old_mark_set && top->lineno == mbb_lineno_save) |
| 2488 | mark_beginbuf = top; |
| 2489 | #endif |
| 2490 | top = top->prev; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2491 | } |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2492 | |
| 2493 | /* Put current_x at the same place in the copied paragraph that it |
| 2494 | * had in the original paragraph. */ |
| 2495 | current_x = current_x_save; |
| 2496 | |
| 2497 | set_modified(); |
| 2498 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2499 | return first_line; |
| 2500 | } |
| 2501 | |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2502 | /* Is it possible to break line at or before goal? */ |
David Lawrence Ramsey | d08348b | 2004-09-18 22:02:21 +0000 | [diff] [blame] | 2503 | bool breakable(const char *line, ssize_t goal) |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2504 | { |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2505 | while (*line != '\0' && goal >= 0) { |
| 2506 | size_t pos = 0; |
| 2507 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 2508 | if (is_blank_char(*line)) |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2509 | return TRUE; |
| 2510 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 2511 | line += parse_mbchar(line, NULL |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2512 | #ifdef NANO_WIDE |
| 2513 | , NULL |
| 2514 | #endif |
David Lawrence Ramsey | d96851f | 2005-01-07 22:39:43 +0000 | [diff] [blame] | 2515 | , &pos); |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2516 | |
| 2517 | goal -= pos; |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2518 | } |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2519 | |
Chris Allegretta | 428f620 | 2003-02-12 03:21:45 +0000 | [diff] [blame] | 2520 | /* If goal is not negative, the whole line (one word) was short |
| 2521 | * enough. */ |
David Lawrence Ramsey | 12054fe | 2005-01-07 22:37:01 +0000 | [diff] [blame] | 2522 | return (goal >= 0); |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2525 | /* 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] | 2526 | * 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] | 2527 | * such space, and force is TRUE, then we find the first space. Anyway, |
| 2528 | * we then take the last space in that group of spaces. The terminating |
| 2529 | * '\0' counts as a space. */ |
David Lawrence Ramsey | fc54d6e | 2004-12-02 17:37:09 +0000 | [diff] [blame] | 2530 | 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] | 2531 | { |
David Lawrence Ramsey | d08348b | 2004-09-18 22:02:21 +0000 | [diff] [blame] | 2532 | ssize_t space_loc = -1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2533 | /* Current tentative return value. Index of the last space we |
| 2534 | * found with short enough display width. */ |
David Lawrence Ramsey | d08348b | 2004-09-18 22:02:21 +0000 | [diff] [blame] | 2535 | ssize_t cur_loc = 0; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2536 | /* Current index in line. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2537 | |
| 2538 | assert(line != NULL); |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2539 | |
| 2540 | while (*line != '\0' && goal >= 0) { |
| 2541 | size_t pos = 0; |
| 2542 | int line_len; |
| 2543 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2544 | if (*line == ' ') |
| 2545 | space_loc = cur_loc; |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2546 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2547 | assert(*line != '\t'); |
| 2548 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 2549 | line_len = parse_mbchar(line, NULL |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2550 | #ifdef NANO_WIDE |
| 2551 | , NULL |
| 2552 | #endif |
David Lawrence Ramsey | d96851f | 2005-01-07 22:39:43 +0000 | [diff] [blame] | 2553 | , &pos); |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2554 | |
| 2555 | goal -= pos; |
| 2556 | line += line_len; |
| 2557 | cur_loc += line_len; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2558 | } |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2559 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2560 | if (goal >= 0) |
| 2561 | /* In fact, the whole line displays shorter than goal. */ |
| 2562 | return cur_loc; |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2563 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2564 | if (space_loc == -1) { |
| 2565 | /* No space found short enough. */ |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2566 | if (force) { |
| 2567 | for (; *line != '\0'; line++, cur_loc++) { |
| 2568 | if (*line == ' ' && *(line + 1) != ' ' && |
| 2569 | *(line + 1) != '\0') |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2570 | return cur_loc; |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2571 | } |
| 2572 | return -1; |
| 2573 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2574 | } |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2575 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2576 | /* Perhaps the character after space_loc is a space. But because |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 2577 | * of justify_format(), there can be only two adjacent. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2578 | if (*(line - cur_loc + space_loc + 1) == ' ' || |
| 2579 | *(line - cur_loc + space_loc + 1) == '\0') |
| 2580 | space_loc++; |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2581 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2582 | return space_loc; |
| 2583 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2584 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2585 | /* Find the beginning of the current paragraph if we're in one, or the |
| 2586 | * beginning of the next paragraph if we're not. Afterwards, save the |
| 2587 | * quote length and paragraph length in *quote and *par. Return FALSE |
| 2588 | * if we found a paragraph, or TRUE if there was an error or we didn't |
| 2589 | * find a paragraph. |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2590 | * |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2591 | * See the comment at begpar() for more about when a line is the |
| 2592 | * beginning of a paragraph. */ |
| 2593 | 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] | 2594 | { |
| 2595 | size_t quote_len; |
| 2596 | /* Length of the initial quotation of the paragraph we |
| 2597 | * search. */ |
| 2598 | size_t par_len; |
| 2599 | /* Number of lines in that paragraph. */ |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 2600 | size_t indent_len; |
| 2601 | /* Generic indentation length. */ |
| 2602 | filestruct *line; |
| 2603 | /* Generic line of text. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2604 | |
| 2605 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2606 | if (quoterc != 0) { |
| 2607 | statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr); |
| 2608 | return TRUE; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2609 | } |
| 2610 | #endif |
| 2611 | |
| 2612 | /* Here is an assumption that is always true anyway. */ |
| 2613 | assert(current != NULL); |
| 2614 | |
| 2615 | current_x = 0; |
| 2616 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2617 | quote_len = quote_length(current->data); |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2618 | indent_len = indent_length(current->data + quote_len); |
| 2619 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2620 | /* Here we find the first line of the paragraph to search. If the |
| 2621 | * current line is in a paragraph, then we move back to the first |
| 2622 | * line of the paragraph. Otherwise, we move to the first line that |
| 2623 | * is in a paragraph. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2624 | if (current->data[quote_len + indent_len] != '\0') { |
| 2625 | /* This line is part of a paragraph. So we must search back to |
| 2626 | * the first line of this paragraph. First we check items 1) |
| 2627 | * and 3) above. */ |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2628 | while (current->prev != NULL && quotes_match(current->data, |
| 2629 | quote_len, current->prev->data)) { |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2630 | size_t temp_id_len = |
David Lawrence Ramsey | bb50b30 | 2004-08-12 21:43:00 +0000 | [diff] [blame] | 2631 | indent_length(current->prev->data + quote_len); |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2632 | /* The indentation length of the previous line. */ |
| 2633 | |
| 2634 | /* Is this line the beginning of a paragraph, according to |
| 2635 | * items 2), 5), or 4) above? If so, stop. */ |
| 2636 | if (current->prev->data[quote_len + temp_id_len] == '\0' || |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2637 | (quote_len == 0 && indent_len > 0 |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2638 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2639 | && !ISSET(AUTOINDENT) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2640 | #endif |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2641 | ) || !indents_match(current->prev->data + quote_len, |
| 2642 | temp_id_len, current->data + quote_len, indent_len)) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2643 | break; |
| 2644 | indent_len = temp_id_len; |
| 2645 | current = current->prev; |
| 2646 | current_y--; |
| 2647 | } |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2648 | } else { |
| 2649 | /* This line is not part of a paragraph. Move down until we get |
| 2650 | * to a non "blank" line. */ |
| 2651 | do { |
| 2652 | /* There is no next paragraph, so nothing to move to. */ |
| 2653 | if (current->next == NULL) { |
| 2654 | placewewant = 0; |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2655 | return TRUE; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2656 | } |
| 2657 | current = current->next; |
| 2658 | current_y++; |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2659 | quote_len = quote_length(current->data); |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2660 | indent_len = indent_length(current->data + quote_len); |
| 2661 | } while (current->data[quote_len + indent_len] == '\0'); |
| 2662 | } |
| 2663 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2664 | /* Now current is the first line of the paragraph, and quote_len is |
| 2665 | * the quotation length of that line. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2666 | |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2667 | /* Next step, compute par_len, the number of lines in this |
| 2668 | * paragraph. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2669 | line = current; |
| 2670 | par_len = 1; |
| 2671 | indent_len = indent_length(line->data + quote_len); |
| 2672 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2673 | while (line->next != NULL && |
| 2674 | quotes_match(current->data, quote_len, line->next->data)) { |
| 2675 | 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] | 2676 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2677 | if (!indents_match(line->data + quote_len, indent_len, |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2678 | line->next->data + quote_len, temp_id_len) || |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2679 | line->next->data[quote_len + temp_id_len] == '\0' || |
| 2680 | (quote_len == 0 && temp_id_len > 0 |
| 2681 | #ifndef NANO_SMALL |
David Lawrence Ramsey | dbde9d7 | 2004-06-27 00:54:08 +0000 | [diff] [blame] | 2682 | && !ISSET(AUTOINDENT) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2683 | #endif |
| 2684 | )) |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2685 | break; |
| 2686 | indent_len = temp_id_len; |
| 2687 | line = line->next; |
| 2688 | par_len++; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2691 | /* Now par_len is the number of lines in this paragraph. We should |
| 2692 | * never call quotes_match() or quote_length() again. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2693 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2694 | /* Save the values of quote_len and par_len. */ |
| 2695 | assert(quote != NULL && par != NULL); |
| 2696 | *quote = quote_len; |
| 2697 | *par = par_len; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2698 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2699 | return FALSE; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2700 | } |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2701 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2702 | /* If full_justify is TRUE, justify the entire file. Otherwise, justify |
| 2703 | * the current paragraph. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2704 | void do_justify(bool full_justify) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2705 | { |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2706 | filestruct *first_par_line = NULL; |
| 2707 | /* Will be the first line of the resulting justified paragraph. |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2708 | * For restoring after unjustify. */ |
David Lawrence Ramsey | 36e363f | 2004-05-17 20:38:00 +0000 | [diff] [blame] | 2709 | filestruct *last_par_line; |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2710 | /* Will be the line containing the newline after the last line |
| 2711 | * of the result. Also for restoring after unjustify. */ |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2712 | bool allow_respacing; |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2713 | /* 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] | 2714 | * after justifying it. This should be TRUE whenever we move |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2715 | * to the next line after justifying the current line. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2716 | |
| 2717 | /* We save these global variables to be restored if the user |
David Lawrence Ramsey | 59f5e04 | 2004-10-29 15:48:40 +0000 | [diff] [blame] | 2718 | * unjustifies. Note that we don't need to save totlines. */ |
David Lawrence Ramsey | 7a97e18 | 2004-10-30 01:03:15 +0000 | [diff] [blame] | 2719 | size_t current_x_save = current_x; |
| 2720 | int current_y_save = current_y; |
David Lawrence Ramsey | 59f5e04 | 2004-10-29 15:48:40 +0000 | [diff] [blame] | 2721 | long flags_save = flags, totsize_save = totsize; |
| 2722 | filestruct *edittop_save = edittop, *current_save = current; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2723 | #ifndef NANO_SMALL |
| 2724 | filestruct *mark_beginbuf_save = mark_beginbuf; |
David Lawrence Ramsey | 687776b | 2004-10-30 01:16:08 +0000 | [diff] [blame] | 2725 | size_t mark_beginx_save = mark_beginx; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2726 | #endif |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2727 | int kbinput; |
David Lawrence Ramsey | cac0293 | 2005-01-11 23:05:05 +0000 | [diff] [blame] | 2728 | bool meta_key, func_key, s_or_t, ran_func, finished; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2729 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2730 | /* If we're justifying the entire file, start at the beginning. */ |
| 2731 | if (full_justify) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2732 | current = fileage; |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2733 | |
| 2734 | last_par_line = current; |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2735 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2736 | while (TRUE) { |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2737 | size_t quote_len; |
| 2738 | /* Length of the initial quotation of the paragraph we |
| 2739 | * justify. */ |
| 2740 | size_t par_len; |
| 2741 | /* Number of lines in that paragraph. */ |
| 2742 | |
| 2743 | /* Find the first line of the paragraph to be justified. That |
| 2744 | * is the start of this paragraph if we're in one, or the start |
| 2745 | * of the next otherwise. Save the quote length and paragraph |
David Lawrence Ramsey | 68ebb61 | 2004-12-04 17:36:14 +0000 | [diff] [blame] | 2746 | * length (number of lines). Don't refresh the screen yet, |
| 2747 | * since we'll do that after we justify. If the search |
| 2748 | * failed, we're justifying the whole file, and the search |
| 2749 | * didn't leave us on the last line of the file, set the last |
| 2750 | * line of the text to be justified to the last line of the file |
| 2751 | * and break out of the loop. Otherwise, refresh the screen and |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2752 | * get out. */ |
| 2753 | if (do_para_search("e_len, &par_len)) { |
David Lawrence Ramsey | 3e81914 | 2004-12-31 04:10:28 +0000 | [diff] [blame] | 2754 | if (full_justify && first_par_line != filebot) { |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2755 | last_par_line = filebot; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2756 | break; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2757 | } else { |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2758 | edit_refresh(); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2759 | return; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | /* Next step, we loop through the lines of this paragraph, |
| 2764 | * justifying each one individually. */ |
| 2765 | for (; par_len > 0; current_y++, par_len--) { |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2766 | size_t indent_len; /* Generic indentation length. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2767 | size_t line_len; |
| 2768 | size_t display_len; |
| 2769 | /* The width of current in screen columns. */ |
David Lawrence Ramsey | d08348b | 2004-09-18 22:02:21 +0000 | [diff] [blame] | 2770 | ssize_t break_pos; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2771 | /* Where we will break the line. */ |
| 2772 | |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2773 | /* We'll be moving to the next line after justifying the |
| 2774 | * current line in almost all cases, so allow changing the |
| 2775 | * spacing at the ends of justified lines by default. */ |
| 2776 | allow_respacing = TRUE; |
| 2777 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2778 | indent_len = quote_len + indent_length(current->data + |
| 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 | /* If we haven't already done it, copy the original |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2782 | * paragraph to the justify buffer. */ |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2783 | if (first_par_line == NULL) |
| 2784 | first_par_line = backup_lines(current, full_justify ? |
David Lawrence Ramsey | 6b769e5 | 2004-11-17 16:59:34 +0000 | [diff] [blame] | 2785 | filebot->lineno - current->lineno : par_len, |
| 2786 | quote_len); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2787 | |
David Lawrence Ramsey | 91bc83a | 2004-06-25 01:52:10 +0000 | [diff] [blame] | 2788 | /* Now we call justify_format() on the current line of the |
| 2789 | * paragraph, which will remove excess spaces from it and |
| 2790 | * change tabs to spaces. */ |
| 2791 | justify_format(current, quote_len + |
| 2792 | indent_length(current->data + quote_len)); |
| 2793 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2794 | line_len = strlen(current->data); |
| 2795 | display_len = strlenpt(current->data); |
| 2796 | |
| 2797 | if (display_len > fill) { |
| 2798 | /* The line is too long. Try to wrap it to the next. */ |
| 2799 | break_pos = break_line(current->data + indent_len, |
David Lawrence Ramsey | 6e738ac | 2004-11-23 22:30:32 +0000 | [diff] [blame] | 2800 | fill - strnlenpt(current->data, indent_len), |
| 2801 | TRUE); |
David Lawrence Ramsey | abc9423 | 2004-12-08 23:24:31 +0000 | [diff] [blame] | 2802 | if (break_pos == -1 || |
| 2803 | break_pos + indent_len == line_len) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2804 | /* We can't break the line, or don't need to, so |
| 2805 | * just go on to the next. */ |
| 2806 | goto continue_loc; |
| 2807 | break_pos += indent_len; |
| 2808 | assert(break_pos < line_len); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2809 | if (par_len == 1) { |
| 2810 | /* There is no next line in this paragraph. We make |
| 2811 | * a new line and copy text after break_pos into |
| 2812 | * it. */ |
David Lawrence Ramsey | 6e738ac | 2004-11-23 22:30:32 +0000 | [diff] [blame] | 2813 | splice_node(current, make_new_node(current), |
| 2814 | current->next); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2815 | /* In a non-quoted paragraph, we copy the indent |
| 2816 | * only if AUTOINDENT is turned on. */ |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2817 | if (quote_len == 0 |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2818 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2819 | && !ISSET(AUTOINDENT) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2820 | #endif |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2821 | ) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2822 | indent_len = 0; |
David Lawrence Ramsey | 6e738ac | 2004-11-23 22:30:32 +0000 | [diff] [blame] | 2823 | current->next->data = charalloc(indent_len + |
| 2824 | line_len - break_pos); |
| 2825 | strncpy(current->next->data, current->data, |
| 2826 | indent_len); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2827 | strcpy(current->next->data + indent_len, |
| 2828 | current->data + break_pos + 1); |
| 2829 | assert(strlen(current->next->data) == |
| 2830 | indent_len + line_len - break_pos - 1); |
| 2831 | totlines++; |
| 2832 | totsize += indent_len; |
| 2833 | par_len++; |
| 2834 | } else { |
| 2835 | size_t next_line_len = strlen(current->next->data); |
| 2836 | |
| 2837 | indent_len = quote_len + |
| 2838 | indent_length(current->next->data + quote_len); |
David Lawrence Ramsey | 6e738ac | 2004-11-23 22:30:32 +0000 | [diff] [blame] | 2839 | current->next->data = |
| 2840 | charealloc(current->next->data, next_line_len + |
| 2841 | line_len - break_pos + 1); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2842 | |
David Lawrence Ramsey | 6e738ac | 2004-11-23 22:30:32 +0000 | [diff] [blame] | 2843 | charmove(current->next->data + indent_len + |
| 2844 | line_len - break_pos, current->next->data + |
| 2845 | indent_len, next_line_len - indent_len + 1); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2846 | strcpy(current->next->data + indent_len, |
| 2847 | current->data + break_pos + 1); |
David Lawrence Ramsey | 537a880 | 2004-06-24 22:39:24 +0000 | [diff] [blame] | 2848 | current->next->data[indent_len + line_len - |
| 2849 | break_pos - 1] = ' '; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2850 | #ifndef NANO_SMALL |
| 2851 | if (mark_beginbuf == current->next) { |
| 2852 | if (mark_beginx < indent_len) |
| 2853 | mark_beginx = indent_len; |
| 2854 | mark_beginx += line_len - break_pos; |
| 2855 | } |
| 2856 | #endif |
| 2857 | } |
| 2858 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 40a6c8c | 2004-11-27 21:10:11 +0000 | [diff] [blame] | 2859 | if (mark_beginbuf == current && |
| 2860 | mark_beginx > break_pos) { |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2861 | mark_beginbuf = current->next; |
| 2862 | mark_beginx -= break_pos + 1 - indent_len; |
| 2863 | } |
| 2864 | #endif |
| 2865 | null_at(¤t->data, break_pos); |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2866 | |
| 2867 | /* Go to the next line. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2868 | current = current->next; |
| 2869 | } else if (display_len < fill && par_len > 1) { |
| 2870 | size_t next_line_len; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2871 | |
| 2872 | indent_len = quote_len + |
| 2873 | indent_length(current->next->data + quote_len); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2874 | /* If we can't pull a word from the next line up to this |
| 2875 | * one, just go on. */ |
| 2876 | if (!breakable(current->next->data + indent_len, |
| 2877 | fill - display_len - 1)) |
| 2878 | goto continue_loc; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2879 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2880 | break_pos = break_line(current->next->data + indent_len, |
| 2881 | fill - display_len - 1, FALSE); |
| 2882 | assert(break_pos != -1); |
| 2883 | |
| 2884 | current->data = charealloc(current->data, |
| 2885 | line_len + break_pos + 2); |
| 2886 | current->data[line_len] = ' '; |
| 2887 | strncpy(current->data + line_len + 1, |
| 2888 | current->next->data + indent_len, break_pos); |
| 2889 | current->data[line_len + break_pos + 1] = '\0'; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2890 | #ifndef NANO_SMALL |
| 2891 | if (mark_beginbuf == current->next) { |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2892 | if (mark_beginx < indent_len + break_pos) { |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2893 | mark_beginbuf = current; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2894 | if (mark_beginx <= indent_len) |
| 2895 | mark_beginx = line_len + 1; |
| 2896 | else |
David Lawrence Ramsey | 2e3aeae | 2004-05-24 05:52:35 +0000 | [diff] [blame] | 2897 | mark_beginx = line_len + 1 + mark_beginx - |
| 2898 | indent_len; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2899 | } else |
| 2900 | mark_beginx -= break_pos + 1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2901 | } |
| 2902 | #endif |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2903 | next_line_len = strlen(current->next->data); |
| 2904 | if (indent_len + break_pos == next_line_len) { |
| 2905 | filestruct *line = current->next; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2906 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2907 | /* Don't destroy edittop! */ |
| 2908 | if (line == edittop) |
| 2909 | edittop = current; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2910 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2911 | unlink_node(line); |
| 2912 | delete_node(line); |
| 2913 | totlines--; |
| 2914 | totsize -= indent_len; |
| 2915 | current_y--; |
David Lawrence Ramsey | 01a6bf4 | 2004-07-03 05:23:19 +0000 | [diff] [blame] | 2916 | |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2917 | /* Don't go to the next line. Accordingly, don't |
| 2918 | * allow changing the spacing at the end of the |
David Lawrence Ramsey | 309fbcb | 2004-07-03 14:34:03 +0000 | [diff] [blame] | 2919 | * previous justified line, so that we don't end up |
| 2920 | * doing it more than once on the same line. */ |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2921 | allow_respacing = FALSE; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2922 | } else { |
| 2923 | charmove(current->next->data + indent_len, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2924 | current->next->data + indent_len + break_pos + 1, |
| 2925 | next_line_len - break_pos - indent_len); |
David Lawrence Ramsey | 6e738ac | 2004-11-23 22:30:32 +0000 | [diff] [blame] | 2926 | null_at(¤t->next->data, next_line_len - |
| 2927 | break_pos); |
David Lawrence Ramsey | 01a6bf4 | 2004-07-03 05:23:19 +0000 | [diff] [blame] | 2928 | |
| 2929 | /* Go to the next line. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2930 | current = current->next; |
| 2931 | } |
| 2932 | } else |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2933 | continue_loc: |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2934 | /* Go to the next line. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2935 | current = current->next; |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2936 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2937 | /* We've moved to the next line after justifying the |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2938 | * current line. If the justified line was not the last |
| 2939 | * line of the paragraph, add a space to the end of it to |
| 2940 | * replace the one removed or left out by justify_format(). |
| 2941 | * If it was the last line of the paragraph, and |
| 2942 | * justify_format() left a space on the end of it, remove |
| 2943 | * the space. */ |
| 2944 | if (allow_respacing) { |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2945 | size_t prev_line_len = strlen(current->prev->data); |
David Lawrence Ramsey | a9a6ce0 | 2004-06-05 22:09:56 +0000 | [diff] [blame] | 2946 | |
| 2947 | if (par_len > 1) { |
| 2948 | current->prev->data = charealloc(current->prev->data, |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2949 | prev_line_len + 2); |
David Lawrence Ramsey | a9a6ce0 | 2004-06-05 22:09:56 +0000 | [diff] [blame] | 2950 | current->prev->data[prev_line_len] = ' '; |
| 2951 | current->prev->data[prev_line_len + 1] = '\0'; |
| 2952 | totsize++; |
| 2953 | } else if (par_len == 1 && |
| 2954 | current->prev->data[prev_line_len - 1] == ' ') { |
David Lawrence Ramsey | 6e738ac | 2004-11-23 22:30:32 +0000 | [diff] [blame] | 2955 | current->prev->data = |
| 2956 | charealloc(current->prev->data, prev_line_len); |
David Lawrence Ramsey | a9a6ce0 | 2004-06-05 22:09:56 +0000 | [diff] [blame] | 2957 | current->prev->data[prev_line_len - 1] = '\0'; |
| 2958 | totsize--; |
| 2959 | } |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2960 | } |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2961 | } |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 2962 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2963 | /* We've just justified a paragraph. If we're not justifying the |
| 2964 | * entire file, break out of the loop. Otherwise, continue the |
| 2965 | * loop so that we justify all the paragraphs in the file. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2966 | if (!full_justify) |
| 2967 | break; |
| 2968 | |
| 2969 | } /* while (TRUE) */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2970 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2971 | /* We are now done justifying the paragraph or the file, so clean |
| 2972 | * up. totlines, totsize, and current_y have been maintained above. |
| 2973 | * 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] | 2974 | * fileage, and renumber() since edit_refresh() needs the line |
| 2975 | * numbers to be right (but only do the last two if we actually |
| 2976 | * justified something). */ |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 2977 | last_par_line = current; |
David Lawrence Ramsey | 8037fe0 | 2004-07-23 12:30:40 +0000 | [diff] [blame] | 2978 | if (first_par_line != NULL) { |
| 2979 | if (first_par_line->prev == NULL) |
| 2980 | fileage = first_par_line; |
| 2981 | renumber(first_par_line); |
| 2982 | } |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2983 | |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2984 | edit_refresh(); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2985 | |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 2986 | statusbar(_("Can now UnJustify!")); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2987 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2988 | /* Display the shortcut list with UnJustify. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2989 | shortcut_init(TRUE); |
Chris Allegretta | 0779835 | 2000-11-27 22:58:23 +0000 | [diff] [blame] | 2990 | display_main_list(); |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 2991 | |
David Lawrence Ramsey | 63e73cb | 2004-11-28 04:52:57 +0000 | [diff] [blame] | 2992 | /* Now get a keystroke and see if it's unjustify. If not, put back |
| 2993 | * the keystroke and return. */ |
David Lawrence Ramsey | cac0293 | 2005-01-11 23:05:05 +0000 | [diff] [blame] | 2994 | kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func, |
| 2995 | &finished, FALSE); |
Chris Allegretta | 5f07180 | 2001-05-06 02:34:31 +0000 | [diff] [blame] | 2996 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 2997 | if (!meta_key && !func_key && s_or_t && |
| 2998 | kbinput == NANO_UNJUSTIFY_KEY) { |
David Lawrence Ramsey | a0b5ba2 | 2004-08-25 15:39:10 +0000 | [diff] [blame] | 2999 | /* Restore the justify we just did (ungrateful user!). */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3000 | current = current_save; |
| 3001 | current_x = current_x_save; |
| 3002 | current_y = current_y_save; |
| 3003 | edittop = edittop_save; |
Chris Allegretta | d022eac | 2000-11-27 02:50:49 +0000 | [diff] [blame] | 3004 | |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3005 | /* 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] | 3006 | * actually justified something. */ |
| 3007 | if (first_par_line != NULL) { |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3008 | filestruct *bot_save; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3009 | |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3010 | /* Partition the filestruct so that it contains only the |
| 3011 | * text of the justified paragraph. */ |
| 3012 | filepart = partition_filestruct(first_par_line, 0, |
| 3013 | last_par_line, 0); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3014 | |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3015 | /* Remove the text of the justified paragraph, and |
| 3016 | * put the text in the justify buffer in its place. */ |
| 3017 | free_filestruct(fileage); |
| 3018 | fileage = jusbuffer; |
| 3019 | filebot = jusbottom; |
| 3020 | |
| 3021 | bot_save = filebot; |
| 3022 | |
| 3023 | /* Unpartition the filestruct so that it contains all the |
| 3024 | * text again. Note that the justified paragraph has been |
| 3025 | * replaced with the unjustified paragraph. */ |
| 3026 | unpartition_filestruct(&filepart); |
| 3027 | |
| 3028 | /* Renumber starting with the ending line of the old |
| 3029 | * partition. */ |
| 3030 | if (bot_save->next != NULL) |
| 3031 | renumber(bot_save->next); |
| 3032 | |
| 3033 | /* Restore global variables from before the justify. */ |
| 3034 | totsize = totsize_save; |
| 3035 | totlines = filebot->lineno; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3036 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3037 | mark_beginbuf = mark_beginbuf_save; |
| 3038 | mark_beginx = mark_beginx_save; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3039 | #endif |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3040 | flags = flags_save; |
| 3041 | |
| 3042 | /* Clear the justify buffer. */ |
| 3043 | jusbuffer = NULL; |
| 3044 | |
| 3045 | if (!ISSET(MODIFIED)) |
| 3046 | titlebar(NULL); |
| 3047 | edit_refresh(); |
| 3048 | } |
David Lawrence Ramsey | a0b5ba2 | 2004-08-25 15:39:10 +0000 | [diff] [blame] | 3049 | } else { |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3050 | unget_kbinput(kbinput, meta_key, func_key); |
David Lawrence Ramsey | d994ad5 | 2004-11-23 21:40:26 +0000 | [diff] [blame] | 3051 | |
David Lawrence Ramsey | 53d3db4 | 2004-11-28 03:53:01 +0000 | [diff] [blame] | 3052 | /* Blow away the text in the justify buffer. */ |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3053 | free_filestruct(jusbuffer); |
| 3054 | jusbuffer = NULL; |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 3055 | } |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 3056 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 3057 | blank_statusbar(); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 3058 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 3059 | /* Display the shortcut list with UnCut. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 3060 | shortcut_init(FALSE); |
Chris Allegretta | 4a9c858 | 2000-11-27 22:59:40 +0000 | [diff] [blame] | 3061 | display_main_list(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3062 | } |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 3063 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3064 | void do_justify_void(void) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 3065 | { |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3066 | do_justify(FALSE); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3069 | void do_full_justify(void) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 3070 | { |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3071 | do_justify(TRUE); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 3072 | } |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3073 | #endif /* !DISABLE_JUSTIFY */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3074 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3075 | void do_exit(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3076 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3077 | int i; |
Chris Allegretta | 13fd44b | 2002-01-02 13:59:11 +0000 | [diff] [blame] | 3078 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3079 | if (!ISSET(MODIFIED)) |
| 3080 | i = 0; /* Pretend the user chose not to save. */ |
David Lawrence Ramsey | edab0cc | 2004-07-03 03:09:12 +0000 | [diff] [blame] | 3081 | else if (ISSET(TEMP_FILE)) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3082 | i = 1; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 3083 | else |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3084 | i = do_yesno(FALSE, |
| 3085 | _("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES) ? ")); |
| 3086 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3087 | #ifdef DEBUG |
| 3088 | dump_buffer(fileage); |
| 3089 | #endif |
| 3090 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3091 | if (i == 0 || (i == 1 && do_writeout(TRUE) > 0)) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3092 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 3ece0b9 | 2004-12-01 15:11:27 +0000 | [diff] [blame] | 3093 | /* Exit only if there are no more open file buffers. */ |
David Lawrence Ramsey | 3e189a8 | 2004-10-03 19:18:48 +0000 | [diff] [blame] | 3094 | if (!close_open_file()) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3095 | #endif |
David Lawrence Ramsey | da14106 | 2004-05-25 19:41:11 +0000 | [diff] [blame] | 3096 | finish(); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3097 | } else if (i != 1) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3098 | statusbar(_("Cancelled")); |
| 3099 | |
| 3100 | display_main_list(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | void signal_init(void) |
| 3104 | { |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 3105 | /* Trap SIGINT and SIGQUIT because we want them to do useful |
| 3106 | * things. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3107 | memset(&act, 0, sizeof(struct sigaction)); |
| 3108 | act.sa_handler = SIG_IGN; |
| 3109 | sigaction(SIGINT, &act, NULL); |
David Lawrence Ramsey | 2897d2b | 2004-01-26 20:56:20 +0000 | [diff] [blame] | 3110 | sigaction(SIGQUIT, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3111 | |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 3112 | /* 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] | 3113 | act.sa_handler = handle_hupterm; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3114 | sigaction(SIGHUP, &act, NULL); |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 3115 | sigaction(SIGTERM, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3116 | |
| 3117 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 3118 | /* Trap SIGWINCH because we want to handle window resizes. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3119 | act.sa_handler = handle_sigwinch; |
| 3120 | sigaction(SIGWINCH, &act, NULL); |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 3121 | allow_pending_sigwinch(FALSE); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3122 | #endif |
| 3123 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3124 | /* Trap normal suspend (^Z) so we can handle it ourselves. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3125 | if (!ISSET(SUSPEND)) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3126 | act.sa_handler = SIG_IGN; |
| 3127 | sigaction(SIGTSTP, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3128 | } else { |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 3129 | /* Block all other signals in the suspend and continue handlers. |
| 3130 | * If we don't do this, other stuff interrupts them! */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3131 | sigfillset(&act.sa_mask); |
| 3132 | |
| 3133 | act.sa_handler = do_suspend; |
| 3134 | sigaction(SIGTSTP, &act, NULL); |
| 3135 | |
| 3136 | act.sa_handler = do_cont; |
| 3137 | sigaction(SIGCONT, &act, NULL); |
| 3138 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3141 | /* Handler for SIGHUP (hangup) and SIGTERM (terminate). */ |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 3142 | RETSIGTYPE handle_hupterm(int signal) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3143 | { |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 3144 | die(_("Received SIGHUP or SIGTERM\n")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3147 | /* Handler for SIGTSTP (suspend). */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3148 | RETSIGTYPE do_suspend(int signal) |
| 3149 | { |
| 3150 | endwin(); |
Chris Allegretta | 7641708 | 2003-02-12 23:54:34 +0000 | [diff] [blame] | 3151 | 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] | 3152 | fflush(stdout); |
| 3153 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3154 | /* Restore the old terminal settings. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3155 | tcsetattr(0, TCSANOW, &oldterm); |
| 3156 | |
David Lawrence Ramsey | 99bede3 | 2003-09-29 07:21:11 +0000 | [diff] [blame] | 3157 | /* 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] | 3158 | * suspended. */ |
David Lawrence Ramsey | 99bede3 | 2003-09-29 07:21:11 +0000 | [diff] [blame] | 3159 | act.sa_handler = handle_hupterm; |
| 3160 | sigaction(SIGHUP, &act, NULL); |
| 3161 | sigaction(SIGTERM, &act, NULL); |
| 3162 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3163 | /* Do what mutt does: send ourselves a SIGSTOP. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3164 | kill(0, SIGSTOP); |
| 3165 | } |
| 3166 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3167 | /* Handler for SIGCONT (continue after suspend). */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3168 | RETSIGTYPE do_cont(int signal) |
| 3169 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3170 | #ifndef NANO_SMALL |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3171 | /* Perhaps the user resized the window while we slept. Handle it |
| 3172 | * and update the screen in the process. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3173 | handle_sigwinch(0); |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 3174 | #else |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3175 | /* Just update the screen. */ |
| 3176 | doupdate(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3177 | #endif |
| 3178 | } |
| 3179 | |
| 3180 | #ifndef NANO_SMALL |
| 3181 | void handle_sigwinch(int s) |
| 3182 | { |
| 3183 | const char *tty = ttyname(0); |
| 3184 | int fd; |
| 3185 | int result = 0; |
| 3186 | struct winsize win; |
| 3187 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3188 | if (tty == NULL) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3189 | return; |
| 3190 | fd = open(tty, O_RDWR); |
| 3191 | if (fd == -1) |
| 3192 | return; |
| 3193 | result = ioctl(fd, TIOCGWINSZ, &win); |
| 3194 | close(fd); |
| 3195 | if (result == -1) |
| 3196 | return; |
| 3197 | |
| 3198 | /* Could check whether the COLS or LINES changed, and return |
| 3199 | * otherwise. EXCEPT, that COLS and LINES are ncurses global |
| 3200 | * variables, and in some cases ncurses has already updated them. |
| 3201 | * But not in all cases, argh. */ |
| 3202 | COLS = win.ws_col; |
| 3203 | LINES = win.ws_row; |
Chris Allegretta | f8f2d58 | 2003-02-10 02:43:48 +0000 | [diff] [blame] | 3204 | editwinrows = LINES - 5 + no_help(); |
| 3205 | if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3206 | die_too_small(); |
| 3207 | |
| 3208 | #ifndef DISABLE_WRAPJUSTIFY |
| 3209 | fill = wrap_at; |
| 3210 | if (fill <= 0) |
| 3211 | fill += COLS; |
Chris Allegretta | f8f2d58 | 2003-02-10 02:43:48 +0000 | [diff] [blame] | 3212 | if (fill < 0) |
| 3213 | fill = 0; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3214 | #endif |
| 3215 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 3216 | hblank = charealloc(hblank, COLS + 1); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3217 | memset(hblank, ' ', COLS); |
| 3218 | hblank[COLS] = '\0'; |
| 3219 | |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 3220 | /* If we've partitioned the filestruct, unpartition it now. */ |
| 3221 | if (filepart != NULL) |
David Lawrence Ramsey | 74d8707 | 2004-11-22 00:16:23 +0000 | [diff] [blame] | 3222 | unpartition_filestruct(&filepart); |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 3223 | |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3224 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | bc3b926 | 2004-11-23 04:17:19 +0000 | [diff] [blame] | 3225 | /* If the justify buffer isn't empty, blow away the text in it and |
| 3226 | * display the shortcut list with UnCut. */ |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 3227 | if (jusbuffer != NULL) { |
| 3228 | free_filestruct(jusbuffer); |
| 3229 | jusbuffer = NULL; |
| 3230 | shortcut_init(FALSE); |
| 3231 | } |
| 3232 | #endif |
| 3233 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3234 | #ifdef USE_SLANG |
| 3235 | /* Slang curses emulation brain damage, part 1: If we just do what |
| 3236 | * curses does here, it'll only work properly if the resize made the |
| 3237 | * window smaller. Do what mutt does: Leave and immediately reenter |
| 3238 | * Slang screen management mode. */ |
| 3239 | SLsmg_reset_smg(); |
| 3240 | SLsmg_init_smg(); |
| 3241 | #else |
| 3242 | /* Do the equivalent of what Minimum Profit does: Leave and |
| 3243 | * immediately reenter curses mode. */ |
| 3244 | endwin(); |
| 3245 | refresh(); |
| 3246 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3247 | |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 3248 | /* Restore the terminal to its previous state. */ |
| 3249 | terminal_init(); |
| 3250 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3251 | /* Do the equivalent of what both mutt and Minimum Profit do: |
| 3252 | * Reinitialize all the windows based on the new screen |
| 3253 | * dimensions. */ |
| 3254 | window_init(); |
| 3255 | |
David Lawrence Ramsey | 907725f | 2004-11-12 00:09:20 +0000 | [diff] [blame] | 3256 | /* Redraw the contents of the windows that need it. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3257 | blank_statusbar(); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3258 | display_main_list(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3259 | total_refresh(); |
| 3260 | |
David Lawrence Ramsey | 273d2ce | 2004-01-30 04:20:28 +0000 | [diff] [blame] | 3261 | /* Turn cursor back on for sure. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3262 | curs_set(1); |
| 3263 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 3264 | /* Reset all the input routines that rely on character sequences. */ |
| 3265 | reset_kbinput(); |
| 3266 | |
David Lawrence Ramsey | 273d2ce | 2004-01-30 04:20:28 +0000 | [diff] [blame] | 3267 | /* Jump back to the main loop. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3268 | siglongjmp(jmpbuf, 1); |
| 3269 | } |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 3270 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 3271 | void allow_pending_sigwinch(bool allow) |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 3272 | { |
| 3273 | sigset_t winch; |
| 3274 | sigemptyset(&winch); |
| 3275 | sigaddset(&winch, SIGWINCH); |
| 3276 | if (allow) |
| 3277 | sigprocmask(SIG_UNBLOCK, &winch, NULL); |
| 3278 | else |
| 3279 | sigprocmask(SIG_BLOCK, &winch, NULL); |
| 3280 | } |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 3281 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 3282 | |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 3283 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 3284 | void do_toggle(const toggle *which) |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 3285 | { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 3286 | bool enabled; |
Chris Allegretta | f0f63a8 | 2000-09-02 18:44:21 +0000 | [diff] [blame] | 3287 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 3288 | TOGGLE(which->flag); |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 3289 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 3290 | switch (which->val) { |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 3291 | case TOGGLE_SUSPEND_KEY: |
| 3292 | signal_init(); |
| 3293 | break; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3294 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 3295 | case TOGGLE_MOUSE_KEY: |
| 3296 | mouse_init(); |
| 3297 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3298 | #endif |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 3299 | case TOGGLE_NOHELP_KEY: |
| 3300 | blank_statusbar(); |
| 3301 | blank_bottombars(); |
| 3302 | wrefresh(bottomwin); |
| 3303 | window_init(); |
| 3304 | edit_refresh(); |
| 3305 | display_main_list(); |
| 3306 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3307 | #ifdef ENABLE_COLOR |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 3308 | case TOGGLE_SYNTAX_KEY: |
| 3309 | edit_refresh(); |
| 3310 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3311 | #endif |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 3312 | #ifdef ENABLE_NANORC |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 3313 | case TOGGLE_WHITESPACE_KEY: |
David Lawrence Ramsey | 846658e | 2004-12-05 04:18:26 +0000 | [diff] [blame] | 3314 | titlebar(NULL); |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 3315 | edit_refresh(); |
| 3316 | break; |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 3317 | #endif |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 3318 | } |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 3319 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3320 | /* We are assuming here that shortcut_init() above didn't free and |
| 3321 | * reallocate the toggles. */ |
| 3322 | enabled = ISSET(which->flag); |
| 3323 | if (which->val == TOGGLE_NOHELP_KEY || which->val == TOGGLE_WRAP_KEY) |
| 3324 | enabled = !enabled; |
| 3325 | statusbar("%s %s", which->desc, |
| 3326 | enabled ? _("enabled") : _("disabled")); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 3327 | } |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 3328 | #endif /* !NANO_SMALL */ |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 3329 | |
David Lawrence Ramsey | 013344c | 2004-09-22 22:45:08 +0000 | [diff] [blame] | 3330 | void disable_extended_input(void) |
| 3331 | { |
| 3332 | struct termios term; |
| 3333 | |
| 3334 | tcgetattr(0, &term); |
| 3335 | term.c_lflag &= ~IEXTEN; |
| 3336 | tcsetattr(0, TCSANOW, &term); |
| 3337 | } |
| 3338 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3339 | void disable_signals(void) |
| 3340 | { |
| 3341 | struct termios term; |
| 3342 | |
| 3343 | tcgetattr(0, &term); |
| 3344 | term.c_lflag &= ~ISIG; |
| 3345 | tcsetattr(0, TCSANOW, &term); |
| 3346 | } |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3347 | |
| 3348 | #ifndef NANO_SMALL |
| 3349 | void enable_signals(void) |
| 3350 | { |
| 3351 | struct termios term; |
| 3352 | |
| 3353 | tcgetattr(0, &term); |
| 3354 | term.c_lflag |= ISIG; |
| 3355 | tcsetattr(0, TCSANOW, &term); |
| 3356 | } |
| 3357 | #endif |
| 3358 | |
| 3359 | void disable_flow_control(void) |
| 3360 | { |
| 3361 | struct termios term; |
| 3362 | |
| 3363 | tcgetattr(0, &term); |
| 3364 | term.c_iflag &= ~(IXON|IXOFF); |
| 3365 | tcsetattr(0, TCSANOW, &term); |
| 3366 | } |
| 3367 | |
| 3368 | void enable_flow_control(void) |
| 3369 | { |
| 3370 | struct termios term; |
| 3371 | |
| 3372 | tcgetattr(0, &term); |
| 3373 | term.c_iflag |= (IXON|IXOFF); |
| 3374 | tcsetattr(0, TCSANOW, &term); |
| 3375 | } |
| 3376 | |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 3377 | /* Set up the terminal state. Put the terminal in cbreak mode (read one |
| 3378 | * character at a time and interpret the special control keys), disable |
| 3379 | * translation of carriage return (^M) into newline (^J) so that we can |
| 3380 | * 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] | 3381 | * echoing of characters as they're typed. Finally, disable extended |
| 3382 | * input processing, disable interpretation of the special control keys, |
| 3383 | * and if we're not in preserve mode, disable interpretation of the flow |
| 3384 | * control characters too. */ |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 3385 | void terminal_init(void) |
| 3386 | { |
| 3387 | cbreak(); |
| 3388 | nonl(); |
| 3389 | noecho(); |
David Lawrence Ramsey | 013344c | 2004-09-22 22:45:08 +0000 | [diff] [blame] | 3390 | disable_extended_input(); |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 3391 | disable_signals(); |
| 3392 | if (!ISSET(PRESERVE)) |
| 3393 | disable_flow_control(); |
| 3394 | } |
| 3395 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3396 | int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool |
David Lawrence Ramsey | cac0293 | 2005-01-11 23:05:05 +0000 | [diff] [blame] | 3397 | *ran_func, bool *finished, bool allow_funcs) |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3398 | { |
| 3399 | int input; |
| 3400 | /* The character we read in. */ |
| 3401 | static int *kbinput = NULL; |
| 3402 | /* The input buffer. */ |
| 3403 | static size_t kbinput_len = 0; |
| 3404 | /* The length of the input buffer. */ |
| 3405 | const shortcut *s; |
| 3406 | bool have_shortcut; |
| 3407 | #ifndef NANO_SMALL |
| 3408 | const toggle *t; |
| 3409 | bool have_toggle; |
| 3410 | #endif |
| 3411 | |
| 3412 | *s_or_t = FALSE; |
David Lawrence Ramsey | cac0293 | 2005-01-11 23:05:05 +0000 | [diff] [blame] | 3413 | *ran_func = FALSE; |
David Lawrence Ramsey | c13b7f0 | 2005-01-01 07:28:15 +0000 | [diff] [blame] | 3414 | *finished = FALSE; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3415 | |
| 3416 | /* Read in a character. */ |
| 3417 | input = get_kbinput(edit, meta_key, func_key); |
| 3418 | |
| 3419 | #ifndef DISABLE_MOUSE |
| 3420 | /* If we got a mouse click and it was on a shortcut, read in the |
| 3421 | * shortcut character. */ |
David Lawrence Ramsey | b8a2a6d | 2005-01-02 21:13:36 +0000 | [diff] [blame] | 3422 | if (allow_funcs && *func_key == TRUE && input == KEY_MOUSE) { |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3423 | if (do_mouse()) |
| 3424 | input = get_kbinput(edit, meta_key, func_key); |
| 3425 | else |
| 3426 | input = ERR; |
| 3427 | } |
| 3428 | #endif |
| 3429 | |
| 3430 | /* Check for a shortcut in the main list. */ |
| 3431 | s = get_shortcut(main_list, &input, meta_key, func_key); |
| 3432 | |
| 3433 | /* If we got a shortcut from the main list, or a "universal" |
| 3434 | * edit window shortcut, set have_shortcut to TRUE. */ |
| 3435 | have_shortcut = (s != NULL || input == NANO_XON_KEY || |
| 3436 | input == NANO_XOFF_KEY || input == NANO_SUSPEND_KEY); |
| 3437 | |
| 3438 | #ifndef NANO_SMALL |
| 3439 | /* Check for a toggle in the main list. */ |
| 3440 | t = get_toggle(input, *meta_key); |
| 3441 | |
| 3442 | /* If we got a toggle from the main list, set have_toggle to |
| 3443 | * TRUE. */ |
| 3444 | have_toggle = (t != NULL); |
| 3445 | #endif |
| 3446 | |
| 3447 | /* Set s_or_t to TRUE if we got a shortcut or toggle. */ |
| 3448 | *s_or_t = (have_shortcut |
| 3449 | #ifndef NANO_SMALL |
| 3450 | || have_toggle |
| 3451 | #endif |
| 3452 | ); |
| 3453 | |
| 3454 | if (allow_funcs) { |
| 3455 | /* If we got a character, and it isn't a shortcut, toggle, or |
| 3456 | * control character, it's a normal text character. Display the |
| 3457 | * warning if we're in view mode, or add the character to the |
| 3458 | * input buffer if we're not. */ |
| 3459 | if (input != ERR && *s_or_t == FALSE && !is_cntrl_char(input)) { |
| 3460 | if (ISSET(VIEW_MODE)) |
| 3461 | print_view_warning(); |
| 3462 | else { |
| 3463 | kbinput_len++; |
| 3464 | kbinput = (int *)nrealloc(kbinput, kbinput_len * |
| 3465 | sizeof(int)); |
| 3466 | kbinput[kbinput_len - 1] = input; |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | /* If we got a shortcut or toggle, or if there aren't any other |
| 3471 | * characters waiting after the one we read in, we need to |
| 3472 | * display all the characters in the input buffer if it isn't |
| 3473 | * empty. Note that it should be empty if we're in view |
| 3474 | * mode. */ |
| 3475 | if (*s_or_t == TRUE || get_buffer_len() == 0) { |
| 3476 | if (kbinput != NULL) { |
| 3477 | /* Display all the characters in the input buffer at |
| 3478 | * once. */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3479 | char *output = charalloc(kbinput_len + 1); |
| 3480 | size_t i; |
| 3481 | |
| 3482 | for (i = 0; i < kbinput_len; i++) |
| 3483 | output[i] = (char)kbinput[i]; |
| 3484 | output[i] = '\0'; |
| 3485 | |
| 3486 | do_output(output, kbinput_len); |
| 3487 | |
| 3488 | free(output); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3489 | |
| 3490 | /* Empty the input buffer. */ |
| 3491 | kbinput_len = 0; |
| 3492 | free(kbinput); |
| 3493 | kbinput = NULL; |
| 3494 | } |
| 3495 | } |
| 3496 | |
| 3497 | if (have_shortcut) { |
| 3498 | switch (input) { |
David Lawrence Ramsey | 775d46d | 2005-01-11 23:08:36 +0000 | [diff] [blame] | 3499 | /* Handle the "universal" statusbar prompt shortcuts. */ |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3500 | case NANO_XON_KEY: |
| 3501 | statusbar(_("XON ignored, mumble mumble.")); |
| 3502 | break; |
| 3503 | case NANO_XOFF_KEY: |
| 3504 | statusbar(_("XOFF ignored, mumble mumble.")); |
| 3505 | break; |
| 3506 | #ifndef NANO_SMALL |
| 3507 | case NANO_SUSPEND_KEY: |
| 3508 | if (ISSET(SUSPEND)) |
| 3509 | do_suspend(0); |
| 3510 | break; |
| 3511 | #endif |
David Lawrence Ramsey | c13b7f0 | 2005-01-01 07:28:15 +0000 | [diff] [blame] | 3512 | /* Handle the normal edit window shortcuts, setting |
David Lawrence Ramsey | cac0293 | 2005-01-11 23:05:05 +0000 | [diff] [blame] | 3513 | * ran_func to TRUE if we try to run their associated |
| 3514 | * functions and setting finished to TRUE to indicate |
| 3515 | * that we're done after trying to run their associated |
David Lawrence Ramsey | c13b7f0 | 2005-01-01 07:28:15 +0000 | [diff] [blame] | 3516 | * functions. */ |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3517 | default: |
| 3518 | /* Blow away the text in the cutbuffer if we aren't |
| 3519 | * cutting text. */ |
| 3520 | if (s->func != do_cut_text) |
| 3521 | cutbuffer_reset(); |
| 3522 | |
David Lawrence Ramsey | 6a2f068 | 2004-12-20 01:13:55 +0000 | [diff] [blame] | 3523 | if (s->func != NULL) { |
David Lawrence Ramsey | cac0293 | 2005-01-11 23:05:05 +0000 | [diff] [blame] | 3524 | *ran_func = TRUE; |
David Lawrence Ramsey | 6a2f068 | 2004-12-20 01:13:55 +0000 | [diff] [blame] | 3525 | if (ISSET(VIEW_MODE) && !s->viewok) |
| 3526 | print_view_warning(); |
| 3527 | else |
| 3528 | s->func(); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3529 | } |
David Lawrence Ramsey | c13b7f0 | 2005-01-01 07:28:15 +0000 | [diff] [blame] | 3530 | *finished = TRUE; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3531 | break; |
| 3532 | } |
| 3533 | } |
| 3534 | #ifndef NANO_SMALL |
| 3535 | else if (have_toggle) { |
| 3536 | /* Blow away the text in the cutbuffer, since we aren't |
| 3537 | * cutting text. */ |
| 3538 | cutbuffer_reset(); |
| 3539 | /* Toggle the flag associated with this shortcut. */ |
| 3540 | if (allow_funcs) |
| 3541 | do_toggle(t); |
| 3542 | } |
| 3543 | #endif |
| 3544 | else |
| 3545 | /* Blow away the text in the cutbuffer, since we aren't |
| 3546 | * cutting text. */ |
| 3547 | cutbuffer_reset(); |
| 3548 | } |
| 3549 | |
| 3550 | return input; |
| 3551 | } |
| 3552 | |
| 3553 | #ifndef DISABLE_MOUSE |
| 3554 | bool do_mouse(void) |
| 3555 | { |
| 3556 | int mouse_x, mouse_y; |
| 3557 | bool retval; |
| 3558 | |
| 3559 | retval = get_mouseinput(&mouse_x, &mouse_y, TRUE); |
| 3560 | |
| 3561 | if (!retval) { |
| 3562 | /* We can click in the edit window to move the cursor. */ |
| 3563 | if (wenclose(edit, mouse_y, mouse_x)) { |
| 3564 | bool sameline; |
| 3565 | /* Did they click on the line with the cursor? If they |
| 3566 | * clicked on the cursor, we set the mark. */ |
| 3567 | size_t xcur; |
| 3568 | /* The character they clicked on. */ |
| 3569 | |
| 3570 | /* Subtract out the size of topwin. Perhaps we need a |
| 3571 | * constant somewhere? */ |
| 3572 | mouse_y -= 2; |
| 3573 | |
| 3574 | sameline = (mouse_y == current_y); |
| 3575 | |
| 3576 | /* Move to where the click occurred. */ |
| 3577 | for (; current_y < mouse_y && current->next != NULL; current_y++) |
| 3578 | current = current->next; |
| 3579 | for (; current_y > mouse_y && current->prev != NULL; current_y--) |
| 3580 | current = current->prev; |
| 3581 | |
| 3582 | xcur = actual_x(current->data, get_page_start(xplustabs()) + |
| 3583 | mouse_x); |
| 3584 | |
| 3585 | #ifndef NANO_SMALL |
| 3586 | /* Clicking where the cursor is toggles the mark, as does |
| 3587 | * clicking beyond the line length with the cursor at the |
| 3588 | * end of the line. */ |
| 3589 | if (sameline && xcur == current_x) { |
| 3590 | if (ISSET(VIEW_MODE)) { |
| 3591 | print_view_warning(); |
| 3592 | return retval; |
| 3593 | } |
| 3594 | do_mark(); |
| 3595 | } |
| 3596 | #endif |
| 3597 | |
| 3598 | current_x = xcur; |
| 3599 | placewewant = xplustabs(); |
| 3600 | edit_refresh(); |
| 3601 | } |
| 3602 | } |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3603 | |
| 3604 | return retval; |
| 3605 | } |
| 3606 | #endif /* !DISABLE_MOUSE */ |
| 3607 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3608 | /* The user typed kbinput_len multibyte characters. Add them to the |
| 3609 | * edit buffer. */ |
| 3610 | void do_output(char *output, size_t output_len) |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3611 | { |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3612 | size_t current_len = strlen(current->data), i = 0; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3613 | bool old_constupdate = ISSET(CONSTUPDATE); |
| 3614 | bool do_refresh = FALSE; |
| 3615 | /* Do we have to call edit_refresh(), or can we get away with |
| 3616 | * update_line()? */ |
| 3617 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3618 | char *char_buf = charalloc(mb_cur_max()); |
| 3619 | int char_buf_len; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3620 | |
| 3621 | assert(current != NULL && current->data != NULL); |
| 3622 | |
David Lawrence Ramsey | 846658e | 2004-12-05 04:18:26 +0000 | [diff] [blame] | 3623 | /* Turn off constant cursor position display. */ |
| 3624 | UNSET(CONSTUPDATE); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3625 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3626 | while (i < output_len) { |
David Lawrence Ramsey | abc9423 | 2004-12-08 23:24:31 +0000 | [diff] [blame] | 3627 | /* Null to newline, if needed. */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3628 | if (output[i] == '\0') |
| 3629 | output[i] = '\n'; |
David Lawrence Ramsey | abc9423 | 2004-12-08 23:24:31 +0000 | [diff] [blame] | 3630 | /* Newline to Enter, if needed. */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3631 | else if (output[i] == '\n') { |
David Lawrence Ramsey | abc9423 | 2004-12-08 23:24:31 +0000 | [diff] [blame] | 3632 | do_enter(); |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3633 | i++; |
David Lawrence Ramsey | abc9423 | 2004-12-08 23:24:31 +0000 | [diff] [blame] | 3634 | continue; |
| 3635 | } |
| 3636 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3637 | /* Interpret the next multibyte character. If it's an invalid |
| 3638 | * multibyte character, interpret it as though it's a byte |
| 3639 | * character. */ |
| 3640 | char_buf_len = parse_mbchar(output + i, char_buf |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3641 | #ifdef NANO_WIDE |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3642 | , NULL |
| 3643 | #endif |
| 3644 | , NULL); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3645 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3646 | i += char_buf_len; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3647 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3648 | /* When a character is inserted on the current magicline, it |
| 3649 | * means we need a new one! */ |
| 3650 | if (filebot == current) |
| 3651 | new_magicline(); |
| 3652 | |
| 3653 | /* More dangerousness fun =) */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3654 | current->data = charealloc(current->data, current_len + |
| 3655 | (char_buf_len * 2)); |
David Lawrence Ramsey | 3e81914 | 2004-12-31 04:10:28 +0000 | [diff] [blame] | 3656 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3657 | assert(current_x <= current_len); |
David Lawrence Ramsey | 3e81914 | 2004-12-31 04:10:28 +0000 | [diff] [blame] | 3658 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3659 | charmove(¤t->data[current_x + char_buf_len], |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3660 | ¤t->data[current_x], |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3661 | current_len - current_x + char_buf_len); |
| 3662 | charcpy(¤t->data[current_x], char_buf, char_buf_len); |
| 3663 | current_len += char_buf_len; |
| 3664 | totsize += char_buf_len; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3665 | set_modified(); |
| 3666 | |
| 3667 | #ifndef NANO_SMALL |
| 3668 | /* Note that current_x has not yet been incremented. */ |
| 3669 | if (current == mark_beginbuf && current_x < mark_beginx) |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3670 | mark_beginx += char_buf_len; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3671 | #endif |
| 3672 | |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 3673 | do_right(FALSE); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3674 | |
| 3675 | #ifndef DISABLE_WRAPPING |
| 3676 | /* If we're wrapping text, we need to call edit_refresh(). */ |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3677 | if (!ISSET(NO_WRAP) && output[i] != '\t') { |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3678 | bool do_refresh_save = do_refresh; |
| 3679 | |
| 3680 | do_refresh = do_wrap(current); |
| 3681 | |
| 3682 | /* If we needed to call edit_refresh() before this, we'll |
| 3683 | * still need to after this. */ |
| 3684 | if (do_refresh_save) |
| 3685 | do_refresh = TRUE; |
| 3686 | } |
| 3687 | #endif |
| 3688 | |
| 3689 | #ifdef ENABLE_COLOR |
| 3690 | /* If color syntaxes are turned on, we need to call |
| 3691 | * edit_refresh(). */ |
| 3692 | if (ISSET(COLOR_SYNTAX)) |
| 3693 | do_refresh = TRUE; |
| 3694 | #endif |
| 3695 | } |
| 3696 | |
David Lawrence Ramsey | 846658e | 2004-12-05 04:18:26 +0000 | [diff] [blame] | 3697 | /* Turn constant cursor position display back on if it was on |
| 3698 | * before. */ |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3699 | if (old_constupdate) |
| 3700 | SET(CONSTUPDATE); |
| 3701 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 3702 | free(char_buf); |
David Lawrence Ramsey | 78ea5e4 | 2004-12-12 19:04:56 +0000 | [diff] [blame] | 3703 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 3704 | if (do_refresh) |
| 3705 | edit_refresh(); |
| 3706 | else |
| 3707 | update_line(current, current_x); |
| 3708 | } |
| 3709 | |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 3710 | int main(int argc, char **argv) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3711 | { |
| 3712 | int optchr; |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 3713 | int startline = 0; |
| 3714 | /* Line to try and start at. */ |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame] | 3715 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 3716 | bool fill_flag_used = FALSE; |
| 3717 | /* Was the fill option used? */ |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame] | 3718 | #endif |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 3719 | #ifdef ENABLE_MULTIBUFFER |
| 3720 | bool old_multibuffer; |
| 3721 | /* The old value of the multibuffer option, restored after we |
| 3722 | * load all files on the command line. */ |
| 3723 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3724 | #ifdef HAVE_GETOPT_LONG |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3725 | const struct option long_options[] = { |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3726 | {"help", 0, 0, 'h'}, |
| 3727 | #ifdef ENABLE_MULTIBUFFER |
| 3728 | {"multibuffer", 0, 0, 'F'}, |
| 3729 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3730 | #ifdef ENABLE_NANORC |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 3731 | #ifndef NANO_SMALL |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 3732 | {"historylog", 0, 0, 'H'}, |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 3733 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3734 | {"ignorercfiles", 0, 0, 'I'}, |
| 3735 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3736 | #ifndef DISABLE_JUSTIFY |
| 3737 | {"quotestr", 1, 0, 'Q'}, |
| 3738 | #endif |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 3739 | #ifdef HAVE_REGEX_H |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 3740 | {"regexp", 0, 0, 'R'}, |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 3741 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3742 | {"tabsize", 1, 0, 'T'}, |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3743 | {"version", 0, 0, 'V'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3744 | #ifdef ENABLE_COLOR |
| 3745 | {"syntax", 1, 0, 'Y'}, |
| 3746 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3747 | {"const", 0, 0, 'c'}, |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 3748 | {"rebinddelete", 0, 0, 'd'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3749 | {"nofollow", 0, 0, 'l'}, |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3750 | #ifndef DISABLE_MOUSE |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3751 | {"mouse", 0, 0, 'm'}, |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3752 | #endif |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 3753 | #ifndef DISABLE_OPERATINGDIR |
| 3754 | {"operatingdir", 1, 0, 'o'}, |
| 3755 | #endif |
Chris Allegretta | 6cd143d | 2003-01-05 23:35:44 +0000 | [diff] [blame] | 3756 | {"preserve", 0, 0, 'p'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3757 | #ifndef DISABLE_WRAPJUSTIFY |
| 3758 | {"fill", 1, 0, 'r'}, |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 3759 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3760 | #ifndef DISABLE_SPELLER |
| 3761 | {"speller", 1, 0, 's'}, |
| 3762 | #endif |
| 3763 | {"tempfile", 0, 0, 't'}, |
| 3764 | {"view", 0, 0, 'v'}, |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 3765 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3766 | {"nowrap", 0, 0, 'w'}, |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 3767 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3768 | {"nohelp", 0, 0, 'x'}, |
| 3769 | {"suspend", 0, 0, 'z'}, |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3770 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7acf69 | 2004-05-22 20:15:20 +0000 | [diff] [blame] | 3771 | {"smarthome", 0, 0, 'A'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3772 | {"backup", 0, 0, 'B'}, |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3773 | {"backupdir", 1, 0, 'E'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3774 | {"noconvert", 0, 0, 'N'}, |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3775 | {"smooth", 0, 0, 'S'}, |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3776 | {"restricted", 0, 0, 'Z'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3777 | {"autoindent", 0, 0, 'i'}, |
| 3778 | {"cut", 0, 0, 'k'}, |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3779 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3780 | {0, 0, 0, 0} |
| 3781 | }; |
| 3782 | #endif |
| 3783 | |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 3784 | #ifdef NANO_WIDE |
| 3785 | { |
| 3786 | /* If the locale set doesn't exist, or it exists but doesn't |
| 3787 | * include the string "UTF-8", we shouldn't use UTF-8 |
| 3788 | * support. */ |
| 3789 | char *locale = setlocale(LC_ALL, ""); |
| 3790 | |
| 3791 | if (locale == NULL || (locale != NULL && |
| 3792 | strstr(locale, "UTF-8") == NULL)) |
| 3793 | SET(NO_UTF8); |
| 3794 | } |
| 3795 | #else |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3796 | setlocale(LC_ALL, ""); |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 3797 | #endif |
| 3798 | |
David Lawrence Ramsey | ad1fd0d | 2004-07-27 15:46:58 +0000 | [diff] [blame] | 3799 | #ifdef ENABLE_NLS |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3800 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 3801 | textdomain(PACKAGE); |
| 3802 | #endif |
| 3803 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3804 | #if !defined(ENABLE_NANORC) && defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING) |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 3805 | /* If we don't have rcfile support, we're root, and |
| 3806 | * --disable-wrapping-as-root is used, turn wrapping off. */ |
David Lawrence Ramsey | a619ae6 | 2004-03-04 06:33:52 +0000 | [diff] [blame] | 3807 | if (geteuid() == NANO_ROOT_UID) |
David Lawrence Ramsey | dc60b72 | 2002-10-25 16:08:53 +0000 | [diff] [blame] | 3808 | SET(NO_WRAP); |
| 3809 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3810 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3811 | while ((optchr = |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3812 | #ifdef HAVE_GETOPT_LONG |
David Lawrence Ramsey | 9ec76e5 | 2004-12-23 19:55:57 +0000 | [diff] [blame] | 3813 | 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] | 3814 | #else |
David Lawrence Ramsey | 9ec76e5 | 2004-12-23 19:55:57 +0000 | [diff] [blame] | 3815 | getopt(argc, argv, "h?ABE:FHINQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz") |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3816 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3817 | ) != -1) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3818 | |
| 3819 | switch (optchr) { |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3820 | case 'a': |
| 3821 | case 'b': |
| 3822 | case 'e': |
| 3823 | case 'f': |
| 3824 | case 'g': |
| 3825 | case 'j': |
| 3826 | /* Pico compatibility flags. */ |
| 3827 | break; |
Chris Allegretta | 7004c28 | 2001-09-22 00:42:10 +0000 | [diff] [blame] | 3828 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3829 | case 'A': |
| 3830 | SET(SMART_HOME); |
| 3831 | break; |
| 3832 | case 'B': |
| 3833 | SET(BACKUP_FILE); |
| 3834 | break; |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3835 | case 'E': |
| 3836 | backup_dir = mallocstrcpy(backup_dir, optarg); |
| 3837 | break; |
Chris Allegretta | 7004c28 | 2001-09-22 00:42:10 +0000 | [diff] [blame] | 3838 | #endif |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 3839 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3840 | case 'F': |
| 3841 | SET(MULTIBUFFER); |
| 3842 | break; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 3843 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3844 | #ifdef ENABLE_NANORC |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 3845 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3846 | case 'H': |
| 3847 | SET(HISTORYLOG); |
| 3848 | break; |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 3849 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3850 | case 'I': |
| 3851 | SET(NO_RCFILE); |
| 3852 | break; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3853 | #endif |
Chris Allegretta | 8fa1e28 | 2001-09-22 04:20:25 +0000 | [diff] [blame] | 3854 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3855 | case 'N': |
| 3856 | SET(NO_CONVERT); |
| 3857 | break; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 3858 | #endif |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 3859 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3860 | case 'Q': |
| 3861 | quotestr = mallocstrcpy(quotestr, optarg); |
| 3862 | break; |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 3863 | #endif |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 3864 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3865 | case 'R': |
| 3866 | SET(USE_REGEXP); |
| 3867 | break; |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 3868 | #endif |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3869 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3870 | case 'S': |
| 3871 | SET(SMOOTHSCROLL); |
| 3872 | break; |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3873 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3874 | case 'T': |
| 3875 | if (!parse_num(optarg, &tabsize) || tabsize <= 0) { |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 3876 | fprintf(stderr, _("Requested tab size %s invalid"), optarg); |
| 3877 | fprintf(stderr, "\n"); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3878 | exit(1); |
| 3879 | } |
| 3880 | break; |
| 3881 | case 'V': |
| 3882 | version(); |
| 3883 | exit(0); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 3884 | #ifdef ENABLE_COLOR |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3885 | case 'Y': |
| 3886 | syntaxstr = mallocstrcpy(syntaxstr, optarg); |
| 3887 | break; |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 3888 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3889 | case 'Z': |
| 3890 | SET(RESTRICTED); |
| 3891 | break; |
| 3892 | case 'c': |
| 3893 | SET(CONSTUPDATE); |
| 3894 | break; |
| 3895 | case 'd': |
| 3896 | SET(REBIND_DELETE); |
| 3897 | break; |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 3898 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3899 | case 'i': |
| 3900 | SET(AUTOINDENT); |
| 3901 | break; |
| 3902 | case 'k': |
| 3903 | SET(CUT_TO_END); |
| 3904 | break; |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 3905 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3906 | case 'l': |
| 3907 | SET(NOFOLLOW_SYMLINKS); |
| 3908 | break; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3909 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3910 | case 'm': |
| 3911 | SET(USE_MOUSE); |
| 3912 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3913 | #endif |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 3914 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3915 | case 'o': |
| 3916 | operating_dir = mallocstrcpy(operating_dir, optarg); |
| 3917 | break; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 3918 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3919 | case 'p': |
| 3920 | SET(PRESERVE); |
| 3921 | break; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 3922 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3923 | case 'r': |
| 3924 | if (!parse_num(optarg, &wrap_at)) { |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 3925 | fprintf(stderr, _("Requested fill size %s invalid"), optarg); |
| 3926 | fprintf(stderr, "\n"); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3927 | exit(1); |
| 3928 | } |
| 3929 | fill_flag_used = TRUE; |
| 3930 | break; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 3931 | #endif |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 3932 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3933 | case 's': |
| 3934 | alt_speller = mallocstrcpy(alt_speller, optarg); |
| 3935 | break; |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 3936 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3937 | case 't': |
| 3938 | SET(TEMP_FILE); |
| 3939 | break; |
| 3940 | case 'v': |
| 3941 | SET(VIEW_MODE); |
| 3942 | break; |
David Lawrence Ramsey | 95e0cf5 | 2003-01-02 16:32:20 +0000 | [diff] [blame] | 3943 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3944 | case 'w': |
| 3945 | SET(NO_WRAP); |
| 3946 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3947 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3948 | case 'x': |
| 3949 | SET(NO_HELP); |
| 3950 | break; |
| 3951 | case 'z': |
| 3952 | SET(SUSPEND); |
| 3953 | break; |
| 3954 | default: |
| 3955 | usage(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3956 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 3959 | /* If the executable filename starts with 'r', we use restricted |
| 3960 | * mode. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3961 | if (*(tail(argv[0])) == 'r') |
| 3962 | SET(RESTRICTED); |
| 3963 | |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 3964 | /* If we're using restricted mode, disable suspending, backups, and |
| 3965 | * reading rcfiles, since they all would allow reading from or |
| 3966 | * writing to files not specified on the command line. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3967 | if (ISSET(RESTRICTED)) { |
| 3968 | UNSET(SUSPEND); |
| 3969 | UNSET(BACKUP_FILE); |
| 3970 | SET(NO_RCFILE); |
| 3971 | } |
| 3972 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3973 | /* 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] | 3974 | * and values that are set, and read the rcfile(s). If the values |
| 3975 | * haven't changed afterward, restore the backed-up values. */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3976 | #ifdef ENABLE_NANORC |
| 3977 | if (!ISSET(NO_RCFILE)) { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 3978 | #ifndef DISABLE_OPERATINGDIR |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3979 | char *operating_dir_cpy = operating_dir; |
| 3980 | #endif |
David Lawrence Ramsey | 7e8dd19 | 2004-08-12 20:06:20 +0000 | [diff] [blame] | 3981 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 3982 | ssize_t wrap_at_cpy = wrap_at; |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3983 | #endif |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3984 | #ifndef NANO_SMALL |
| 3985 | char *backup_dir_cpy = backup_dir; |
| 3986 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3987 | #ifndef DISABLE_JUSTIFY |
| 3988 | char *quotestr_cpy = quotestr; |
| 3989 | #endif |
| 3990 | #ifndef DISABLE_SPELLER |
| 3991 | char *alt_speller_cpy = alt_speller; |
| 3992 | #endif |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 3993 | ssize_t tabsize_cpy = tabsize; |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3994 | long flags_cpy = flags; |
| 3995 | |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 3996 | #ifndef DISABLE_OPERATINGDIR |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3997 | operating_dir = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 3998 | #endif |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3999 | #ifndef NANO_SMALL |
| 4000 | backup_dir = NULL; |
| 4001 | #endif |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 4002 | #ifndef DISABLE_JUSTIFY |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4003 | quotestr = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 4004 | #endif |
| 4005 | #ifndef DISABLE_SPELLER |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4006 | alt_speller = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 4007 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4008 | |
| 4009 | do_rcfile(); |
| 4010 | |
| 4011 | #ifndef DISABLE_OPERATINGDIR |
| 4012 | if (operating_dir_cpy != NULL) { |
| 4013 | free(operating_dir); |
| 4014 | operating_dir = operating_dir_cpy; |
| 4015 | } |
| 4016 | #endif |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame] | 4017 | #ifndef DISABLE_WRAPJUSTIFY |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4018 | if (fill_flag_used) |
| 4019 | wrap_at = wrap_at_cpy; |
| 4020 | #endif |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 4021 | #ifndef NANO_SMALL |
| 4022 | if (backup_dir_cpy != NULL) { |
| 4023 | free(backup_dir); |
| 4024 | backup_dir = backup_dir_cpy; |
| 4025 | } |
| 4026 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4027 | #ifndef DISABLE_JUSTIFY |
| 4028 | if (quotestr_cpy != NULL) { |
| 4029 | free(quotestr); |
| 4030 | quotestr = quotestr_cpy; |
| 4031 | } |
| 4032 | #endif |
| 4033 | #ifndef DISABLE_SPELLER |
| 4034 | if (alt_speller_cpy != NULL) { |
| 4035 | free(alt_speller); |
| 4036 | alt_speller = alt_speller_cpy; |
| 4037 | } |
| 4038 | #endif |
David Lawrence Ramsey | 04419b9 | 2004-07-18 18:13:54 +0000 | [diff] [blame] | 4039 | if (tabsize_cpy != -1) |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4040 | tabsize = tabsize_cpy; |
| 4041 | flags |= flags_cpy; |
| 4042 | } |
| 4043 | #if defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING) |
David Lawrence Ramsey | a619ae6 | 2004-03-04 06:33:52 +0000 | [diff] [blame] | 4044 | else if (geteuid() == NANO_ROOT_UID) |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4045 | SET(NO_WRAP); |
| 4046 | #endif |
| 4047 | #endif /* ENABLE_NANORC */ |
| 4048 | |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 4049 | #ifndef NANO_SMALL |
| 4050 | history_init(); |
| 4051 | #ifdef ENABLE_NANORC |
| 4052 | if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG)) |
| 4053 | load_history(); |
| 4054 | #endif |
| 4055 | #endif |
| 4056 | |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 4057 | #ifndef NANO_SMALL |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 4058 | /* Set up the backup directory (unless we're using restricted mode, |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 4059 | * in which case backups are disabled, since they would allow |
| 4060 | * reading from or writing to files not specified on the command |
| 4061 | * line). This entails making sure it exists and is a directory, so |
| 4062 | * that backup files will be saved there. */ |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 4063 | if (!ISSET(RESTRICTED)) |
| 4064 | init_backup_dir(); |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 4065 | #endif |
| 4066 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4067 | #ifndef DISABLE_OPERATINGDIR |
| 4068 | /* Set up the operating directory. This entails chdir()ing there, |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 4069 | * so that file reads and writes will be based there. */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 4070 | init_operating_dir(); |
| 4071 | #endif |
| 4072 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4073 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 4074 | if (punct == NULL) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 4075 | punct = mallocstrcpy(punct, ".?!"); |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 4076 | |
| 4077 | if (brackets == NULL) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 4078 | brackets = mallocstrcpy(brackets, "'\")}]>"); |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 4079 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4080 | if (quotestr == NULL) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 4081 | quotestr = mallocstrcpy(NULL, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4082 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 4083 | "^([ \t]*[|>:}#])+" |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4084 | #else |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 4085 | "> " |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4086 | #endif |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 4087 | ); |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 4088 | #ifdef HAVE_REGEX_H |
| 4089 | quoterc = regcomp("ereg, quotestr, REG_EXTENDED); |
| 4090 | |
| 4091 | if (quoterc == 0) { |
| 4092 | /* We no longer need quotestr, just quotereg. */ |
| 4093 | free(quotestr); |
| 4094 | quotestr = NULL; |
| 4095 | } else { |
| 4096 | size_t size = regerror(quoterc, "ereg, NULL, 0); |
| 4097 | |
| 4098 | quoteerr = charalloc(size); |
| 4099 | regerror(quoterc, "ereg, quoteerr, size); |
| 4100 | } |
| 4101 | #else |
| 4102 | quotelen = strlen(quotestr); |
| 4103 | #endif /* !HAVE_REGEX_H */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4104 | #endif /* !DISABLE_JUSTIFY */ |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 4105 | |
David Lawrence Ramsey | 3aedb36 | 2004-05-28 22:42:41 +0000 | [diff] [blame] | 4106 | #ifndef DISABLE_SPELLER |
| 4107 | /* 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] | 4108 | * 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] | 4109 | * does (unless we're using restricted mode, in which case spell |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 4110 | * checking is disabled, since it would allow reading from or |
| 4111 | * writing to files not specified on the command line). */ |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 4112 | if (!ISSET(RESTRICTED) && alt_speller == NULL) { |
David Lawrence Ramsey | 3aedb36 | 2004-05-28 22:42:41 +0000 | [diff] [blame] | 4113 | char *spellenv = getenv("SPELL"); |
| 4114 | if (spellenv != NULL) |
| 4115 | alt_speller = mallocstrcpy(NULL, spellenv); |
| 4116 | } |
| 4117 | #endif |
| 4118 | |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 4119 | #if !defined(NANO_SMALL) && defined(ENABLE_NANORC) |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 4120 | /* If whitespace wasn't specified, set its default value. */ |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 4121 | if (whitespace == NULL) |
| 4122 | whitespace = mallocstrcpy(NULL, " "); |
| 4123 | #endif |
| 4124 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 4125 | /* If tabsize wasn't specified, set its default value. */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4126 | if (tabsize == -1) |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 4127 | tabsize = WIDTH_OF_TAB; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 4128 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 4129 | /* Back up the old terminal settings so that they can be restored. */ |
Chris Allegretta | 9b4055c | 2002-03-29 16:00:59 +0000 | [diff] [blame] | 4130 | tcgetattr(0, &oldterm); |
Chris Allegretta | 6ca01b1 | 2002-03-29 15:38:17 +0000 | [diff] [blame] | 4131 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 4132 | /* Curses initialization stuff: Start curses and set up the |
| 4133 | * terminal state. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4134 | initscr(); |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 4135 | terminal_init(); |
David Lawrence Ramsey | c53a92d | 2004-01-12 03:28:06 +0000 | [diff] [blame] | 4136 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 4137 | /* Set up the global variables and the shortcuts. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 4138 | global_init(FALSE); |
| 4139 | shortcut_init(FALSE); |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 4140 | |
| 4141 | /* Set up the signal handlers. */ |
Chris Allegretta | 9b4055c | 2002-03-29 16:00:59 +0000 | [diff] [blame] | 4142 | signal_init(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4143 | |
| 4144 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 4145 | fprintf(stderr, "Main: set up windows\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4146 | #endif |
| 4147 | |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 4148 | window_init(); |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 4149 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 4150 | mouse_init(); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 4151 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4152 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4153 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 4154 | fprintf(stderr, "Main: open file\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4155 | #endif |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 4156 | |
David Lawrence Ramsey | bf1346f | 2004-10-22 20:25:56 +0000 | [diff] [blame] | 4157 | /* If there's a +LINE flag here, it is the first non-option |
| 4158 | * argument, and it is followed by at least one other argument, the |
| 4159 | * filename it applies to. */ |
| 4160 | if (0 < optind && optind < argc - 1 && argv[optind][0] == '+') { |
| 4161 | startline = atoi(&argv[optind][1]); |
| 4162 | optind++; |
| 4163 | } |
| 4164 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4165 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 4166 | old_multibuffer = ISSET(MULTIBUFFER); |
| 4167 | SET(MULTIBUFFER); |
| 4168 | |
| 4169 | /* Read all the files after the first one on the command line into |
| 4170 | * new buffers. */ |
| 4171 | { |
David Lawrence Ramsey | bf1346f | 2004-10-22 20:25:56 +0000 | [diff] [blame] | 4172 | int i = optind + 1, iline = 0; |
| 4173 | for (; i < argc; i++) { |
| 4174 | /* If there's a +LINE flag here, it is followed by at least |
| 4175 | * one other argument, the filename it applies to. */ |
| 4176 | if (i < argc - 1 && argv[i][0] == '+' && iline == 0) { |
| 4177 | iline = atoi(&argv[i][1]); |
| 4178 | } else { |
| 4179 | load_buffer(argv[i]); |
| 4180 | if (iline > 0) { |
| 4181 | do_gotoline(iline, FALSE); |
| 4182 | iline = 0; |
| 4183 | } |
| 4184 | } |
| 4185 | } |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 4186 | } |
| 4187 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4188 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 4189 | /* Read the first file on the command line into either the current |
| 4190 | * buffer or a new buffer, depending on whether multibuffer mode is |
| 4191 | * enabled. */ |
| 4192 | if (optind < argc) |
| 4193 | load_buffer(argv[optind]); |
| 4194 | |
| 4195 | /* We didn't open any files if all the command line arguments were |
| 4196 | * invalid files like directories or if there were no command line |
| 4197 | * arguments given. In this case, we have to load a blank buffer. |
| 4198 | * Also, we unset view mode to allow editing. */ |
| 4199 | if (filename == NULL) { |
| 4200 | filename = mallocstrcpy(NULL, ""); |
| 4201 | new_file(); |
| 4202 | UNSET(VIEW_MODE); |
| 4203 | |
| 4204 | /* Add this new entry to the open_files structure if we have |
| 4205 | * multibuffer support, or to the main filestruct if we don't. */ |
| 4206 | load_file(); |
| 4207 | } |
| 4208 | |
| 4209 | #ifdef ENABLE_MULTIBUFFER |
| 4210 | if (!old_multibuffer) |
| 4211 | UNSET(MULTIBUFFER); |
| 4212 | #endif |
| 4213 | |
| 4214 | #ifdef DEBUG |
| 4215 | fprintf(stderr, "Main: top and bottom win\n"); |
| 4216 | #endif |
| 4217 | |
| 4218 | titlebar(NULL); |
| 4219 | display_main_list(); |
| 4220 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4221 | if (startline > 0) |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 4222 | do_gotoline(startline, FALSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4223 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 4224 | #ifndef NANO_SMALL |
| 4225 | /* Return here after a SIGWINCH. */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 4226 | sigsetjmp(jmpbuf, 1); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 4227 | #endif |
Chris Allegretta | 0802088 | 2001-01-29 23:37:54 +0000 | [diff] [blame] | 4228 | |
Robert Siemborski | 6967eec | 2000-07-08 14:23:32 +0000 | [diff] [blame] | 4229 | edit_refresh(); |
Robert Siemborski | 6967eec | 2000-07-08 14:23:32 +0000 | [diff] [blame] | 4230 | |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 4231 | while (TRUE) { |
David Lawrence Ramsey | cac0293 | 2005-01-11 23:05:05 +0000 | [diff] [blame] | 4232 | bool meta_key, func_key, s_or_t, ran_func, finished; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 4233 | |
| 4234 | /* Make sure the cursor is in the edit window. */ |
David Lawrence Ramsey | aea4dab | 2004-07-13 17:09:24 +0000 | [diff] [blame] | 4235 | reset_cursor(); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 4236 | |
| 4237 | /* If constant cursor position display is on, display the cursor |
| 4238 | * position. */ |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 4239 | if (ISSET(CONSTUPDATE)) |
David Lawrence Ramsey | 74912c6 | 2004-07-01 19:41:09 +0000 | [diff] [blame] | 4240 | do_cursorpos(TRUE); |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 4241 | |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 4242 | currshortcut = main_list; |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 4243 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 4244 | /* Read in and interpret characters. */ |
David Lawrence Ramsey | cac0293 | 2005-01-11 23:05:05 +0000 | [diff] [blame] | 4245 | do_input(&meta_key, &func_key, &s_or_t, &ran_func, &finished, |
| 4246 | TRUE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4247 | } |
David Lawrence Ramsey | fd1768a | 2004-03-19 21:57:56 +0000 | [diff] [blame] | 4248 | assert(FALSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 4249 | } |