Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2 | /************************************************************************** |
David Lawrence Ramsey | 4005fc6 | 2005-10-08 06:12:41 +0000 | [diff] [blame] | 3 | * text.c * |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 4 | * * |
David Lawrence Ramsey | d8a1d37 | 2007-10-11 05:01:32 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 * |
| 6 | * Free Software Foundation, Inc. * |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
David Lawrence Ramsey | d0035b4 | 2007-08-11 05:17:36 +0000 | [diff] [blame] | 9 | * the Free Software Foundation; either version 3, or (at your option) * |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 10 | * any later version. * |
| 11 | * * |
| 12 | * This program is distributed in the hope that it will be useful, but * |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 15 | * General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with this program; if not, write to the Free Software * |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * |
| 20 | * 02110-1301, USA. * |
| 21 | * * |
| 22 | **************************************************************************/ |
| 23 | |
David Lawrence Ramsey | 034b994 | 2005-12-08 02:47:10 +0000 | [diff] [blame] | 24 | #include "proto.h" |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 25 | |
David Lawrence Ramsey | ee11c6a | 2005-11-02 19:42:02 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 27 | #include <signal.h> |
| 28 | #include <unistd.h> |
| 29 | #include <string.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <sys/wait.h> |
| 32 | #include <errno.h> |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 33 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 34 | #ifndef NANO_TINY |
David Lawrence Ramsey | 8779a17 | 2005-11-08 16:45:22 +0000 | [diff] [blame] | 35 | static pid_t pid = -1; |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 36 | /* The PID of the forked process in execute_command(), for use |
| 37 | * with the cancel_command() signal handler. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 38 | #endif |
| 39 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 40 | static bool prepend_wrap = FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 41 | /* Should we prepend wrapped text to the next line? */ |
| 42 | #endif |
| 43 | #ifndef DISABLE_JUSTIFY |
| 44 | static filestruct *jusbottom = NULL; |
David Lawrence Ramsey | ae4c3a6 | 2005-09-20 19:46:39 +0000 | [diff] [blame] | 45 | /* Pointer to the end of the justify buffer. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 48 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 49 | /* Toggle the mark. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 50 | void do_mark(void) |
| 51 | { |
| 52 | openfile->mark_set = !openfile->mark_set; |
| 53 | if (openfile->mark_set) { |
| 54 | statusbar(_("Mark Set")); |
| 55 | openfile->mark_begin = openfile->current; |
| 56 | openfile->mark_begin_x = openfile->current_x; |
| 57 | } else { |
David Lawrence Ramsey | 7b0531a | 2006-07-31 01:30:31 +0000 | [diff] [blame] | 58 | statusbar(_("Mark Unset")); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 59 | openfile->mark_begin = NULL; |
| 60 | openfile->mark_begin_x = 0; |
| 61 | edit_refresh(); |
| 62 | } |
| 63 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 64 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 65 | |
David Lawrence Ramsey | f1982f0 | 2006-12-10 17:57:09 +0000 | [diff] [blame] | 66 | /* Delete the character under the cursor. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 67 | void do_delete(void) |
| 68 | { |
| 69 | bool do_refresh = FALSE; |
| 70 | /* Do we have to call edit_refresh(), or can we get away with |
David Lawrence Ramsey | 75e9dfe | 2006-05-18 17:50:08 +0000 | [diff] [blame] | 71 | * just update_line()? */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 72 | |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 73 | #ifndef NANO_TINY |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 74 | update_undo(DEL); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 77 | assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data)); |
| 78 | |
| 79 | openfile->placewewant = xplustabs(); |
| 80 | |
| 81 | if (openfile->current->data[openfile->current_x] != '\0') { |
| 82 | int char_buf_len = parse_mbchar(openfile->current->data + |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 83 | openfile->current_x, NULL, NULL); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 84 | size_t line_len = strlen(openfile->current->data + |
| 85 | openfile->current_x); |
| 86 | |
| 87 | assert(openfile->current_x < strlen(openfile->current->data)); |
| 88 | |
| 89 | /* Let's get dangerous. */ |
| 90 | charmove(&openfile->current->data[openfile->current_x], |
| 91 | &openfile->current->data[openfile->current_x + |
| 92 | char_buf_len], line_len - char_buf_len + 1); |
| 93 | |
| 94 | null_at(&openfile->current->data, openfile->current_x + |
| 95 | line_len - char_buf_len); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 96 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 97 | if (openfile->mark_set && openfile->mark_begin == |
| 98 | openfile->current && openfile->current_x < |
| 99 | openfile->mark_begin_x) |
| 100 | openfile->mark_begin_x -= char_buf_len; |
| 101 | #endif |
| 102 | openfile->totsize--; |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 103 | } else if (openfile->current != openfile->filebot) { |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 104 | filestruct *foo = openfile->current->next; |
| 105 | |
| 106 | assert(openfile->current_x == strlen(openfile->current->data)); |
| 107 | |
| 108 | /* If we're deleting at the end of a line, we need to call |
| 109 | * edit_refresh(). */ |
| 110 | if (openfile->current->data[openfile->current_x] == '\0') |
| 111 | do_refresh = TRUE; |
| 112 | |
| 113 | openfile->current->data = charealloc(openfile->current->data, |
| 114 | openfile->current_x + strlen(foo->data) + 1); |
| 115 | strcpy(openfile->current->data + openfile->current_x, |
| 116 | foo->data); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 117 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 118 | if (openfile->mark_set && openfile->mark_begin == |
| 119 | openfile->current->next) { |
| 120 | openfile->mark_begin = openfile->current; |
| 121 | openfile->mark_begin_x += openfile->current_x; |
| 122 | } |
| 123 | #endif |
| 124 | if (openfile->filebot == foo) |
| 125 | openfile->filebot = openfile->current; |
| 126 | |
| 127 | unlink_node(foo); |
| 128 | delete_node(foo); |
| 129 | renumber(openfile->current); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 130 | openfile->totsize--; |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 131 | |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 132 | /* If the NO_NEWLINES flag isn't set, and text has been added to |
| 133 | * the magicline as a result of deleting at the end of the line |
David Lawrence Ramsey | 0f6236f | 2005-11-09 00:08:29 +0000 | [diff] [blame] | 134 | * before filebot, add a new magicline. */ |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 135 | if (!ISSET(NO_NEWLINES) && openfile->current == |
| 136 | openfile->filebot && openfile->current->data[0] != '\0') |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 137 | new_magicline(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 138 | } else |
| 139 | return; |
| 140 | |
David Lawrence Ramsey | 0f6236f | 2005-11-09 00:08:29 +0000 | [diff] [blame] | 141 | set_modified(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 142 | |
| 143 | #ifdef ENABLE_COLOR |
| 144 | /* If color syntaxes are available and turned on, we need to call |
| 145 | * edit_refresh(). */ |
| 146 | if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX)) |
| 147 | do_refresh = TRUE; |
| 148 | #endif |
| 149 | |
| 150 | if (do_refresh) |
| 151 | edit_refresh(); |
| 152 | else |
| 153 | update_line(openfile->current, openfile->current_x); |
| 154 | } |
| 155 | |
David Lawrence Ramsey | 5b7b3e3 | 2005-12-31 21:22:54 +0000 | [diff] [blame] | 156 | /* Backspace over one character. That is, move the cursor left one |
David Lawrence Ramsey | 84d22e3 | 2006-11-08 13:05:50 +0000 | [diff] [blame] | 157 | * character, and then delete the character under the cursor. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 158 | void do_backspace(void) |
| 159 | { |
| 160 | if (openfile->current != openfile->fileage || |
| 161 | openfile->current_x > 0) { |
David Lawrence Ramsey | 1c3bfa9 | 2005-09-13 04:53:44 +0000 | [diff] [blame] | 162 | do_left(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 163 | do_delete(); |
| 164 | } |
| 165 | } |
| 166 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 167 | /* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number |
| 168 | * of spaces that a tab would normally take up. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 169 | void do_tab(void) |
| 170 | { |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 171 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 172 | if (ISSET(TABS_TO_SPACES)) { |
| 173 | char *output; |
David Lawrence Ramsey | 90b07fc | 2005-10-07 15:57:48 +0000 | [diff] [blame] | 174 | size_t output_len = 0, new_pww = xplustabs(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 175 | |
| 176 | do { |
| 177 | new_pww++; |
| 178 | output_len++; |
| 179 | } while (new_pww % tabsize != 0); |
| 180 | |
| 181 | output = charalloc(output_len + 1); |
| 182 | |
| 183 | charset(output, ' ', output_len); |
| 184 | output[output_len] = '\0'; |
| 185 | |
| 186 | do_output(output, output_len, TRUE); |
| 187 | |
| 188 | free(output); |
| 189 | } else { |
| 190 | #endif |
| 191 | do_output("\t", 1, TRUE); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 192 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 193 | } |
| 194 | #endif |
| 195 | } |
| 196 | |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 197 | #ifndef NANO_TINY |
David Lawrence Ramsey | e44cd2d | 2007-01-11 22:54:55 +0000 | [diff] [blame] | 198 | /* Indent or unindent the current line (or, if the mark is on, all lines |
| 199 | * covered by the mark) len columns, depending on whether len is |
| 200 | * positive or negative. If the TABS_TO_SPACES flag is set, indent or |
| 201 | * unindent by len spaces. Otherwise, indent or unindent by (len / |
| 202 | * tabsize) tabs and (len % tabsize) spaces. */ |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 203 | void do_indent(ssize_t cols) |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 204 | { |
| 205 | bool indent_changed = FALSE; |
| 206 | /* Whether any indenting or unindenting was done. */ |
| 207 | bool unindent = FALSE; |
| 208 | /* Whether we're unindenting text. */ |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 209 | char *line_indent = NULL; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 210 | /* The text added to each line in order to indent it. */ |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 211 | size_t line_indent_len = 0; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 212 | /* The length of the text added to each line in order to indent |
| 213 | * it. */ |
| 214 | filestruct *top, *bot, *f; |
| 215 | size_t top_x, bot_x; |
| 216 | |
| 217 | assert(openfile->current != NULL && openfile->current->data != NULL); |
| 218 | |
David Lawrence Ramsey | af9052d | 2006-05-01 17:14:25 +0000 | [diff] [blame] | 219 | /* If cols is zero, get out. */ |
| 220 | if (cols == 0) |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 221 | return; |
| 222 | |
David Lawrence Ramsey | af9052d | 2006-05-01 17:14:25 +0000 | [diff] [blame] | 223 | /* If cols is negative, make it positive and set unindent to |
| 224 | * TRUE. */ |
| 225 | if (cols < 0) { |
| 226 | cols = -cols; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 227 | unindent = TRUE; |
| 228 | /* Otherwise, we're indenting, in which case the file will always be |
| 229 | * modified, so set indent_changed to TRUE. */ |
| 230 | } else |
| 231 | indent_changed = TRUE; |
| 232 | |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 233 | /* If the mark is on, use all lines covered by the mark. */ |
| 234 | if (openfile->mark_set) |
| 235 | mark_order((const filestruct **)&top, &top_x, |
| 236 | (const filestruct **)&bot, &bot_x, NULL); |
| 237 | /* Otherwise, use the current line. */ |
| 238 | else { |
| 239 | top = openfile->current; |
| 240 | bot = top; |
| 241 | } |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 242 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 243 | if (!unindent) { |
| 244 | /* Set up the text we'll be using as indentation. */ |
| 245 | line_indent = charalloc(cols + 1); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 246 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 247 | if (ISSET(TABS_TO_SPACES)) { |
| 248 | /* Set the indentation to cols spaces. */ |
| 249 | charset(line_indent, ' ', cols); |
| 250 | line_indent_len = cols; |
| 251 | } else { |
| 252 | /* Set the indentation to (cols / tabsize) tabs and (cols % |
| 253 | * tabsize) spaces. */ |
| 254 | size_t num_tabs = cols / tabsize; |
| 255 | size_t num_spaces = cols % tabsize; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 256 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 257 | charset(line_indent, '\t', num_tabs); |
| 258 | charset(line_indent + num_tabs, ' ', num_spaces); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 259 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 260 | line_indent_len = num_tabs + num_spaces; |
| 261 | } |
| 262 | |
| 263 | line_indent[line_indent_len] = '\0'; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 264 | } |
| 265 | |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 266 | /* Go through each line of the text. */ |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 267 | for (f = top; f != bot->next; f = f->next) { |
| 268 | size_t line_len = strlen(f->data); |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 269 | size_t indent_len = indent_length(f->data); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 270 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 271 | if (!unindent) { |
| 272 | /* If we're indenting, add the characters in line_indent to |
| 273 | * the beginning of the non-whitespace text of this line. */ |
| 274 | f->data = charealloc(f->data, line_len + |
| 275 | line_indent_len + 1); |
| 276 | charmove(&f->data[indent_len + line_indent_len], |
| 277 | &f->data[indent_len], line_len - indent_len + 1); |
| 278 | strncpy(f->data + indent_len, line_indent, line_indent_len); |
| 279 | openfile->totsize += line_indent_len; |
| 280 | |
| 281 | /* Keep track of the change in the current line. */ |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 282 | if (openfile->mark_set && f == openfile->mark_begin && |
| 283 | openfile->mark_begin_x >= indent_len) |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 284 | openfile->mark_begin_x += line_indent_len; |
| 285 | |
| 286 | if (f == openfile->current && openfile->current_x >= |
| 287 | indent_len) |
| 288 | openfile->current_x += line_indent_len; |
| 289 | |
| 290 | /* If the NO_NEWLINES flag isn't set, and this is the |
| 291 | * magicline, add a new magicline. */ |
| 292 | if (!ISSET(NO_NEWLINES) && f == openfile->filebot) |
| 293 | new_magicline(); |
| 294 | } else { |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 295 | size_t indent_col = strnlenpt(f->data, indent_len); |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 296 | /* The length in columns of the indentation on this |
| 297 | * line. */ |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 298 | |
David Lawrence Ramsey | af9052d | 2006-05-01 17:14:25 +0000 | [diff] [blame] | 299 | if (cols <= indent_col) { |
| 300 | size_t indent_new = actual_x(f->data, indent_col - |
| 301 | cols); |
David Lawrence Ramsey | 5bb7727 | 2006-05-06 14:37:33 +0000 | [diff] [blame] | 302 | /* The length of the indentation remaining on |
| 303 | * this line after we unindent. */ |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 304 | size_t indent_shift = indent_len - indent_new; |
David Lawrence Ramsey | 5bb7727 | 2006-05-06 14:37:33 +0000 | [diff] [blame] | 305 | /* The change in the indentation on this line |
| 306 | * after we unindent. */ |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 307 | |
David Lawrence Ramsey | af9052d | 2006-05-01 17:14:25 +0000 | [diff] [blame] | 308 | /* If we're unindenting, and there's at least cols |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 309 | * columns' worth of indentation at the beginning of the |
| 310 | * non-whitespace text of this line, remove it. */ |
| 311 | charmove(&f->data[indent_new], &f->data[indent_len], |
| 312 | line_len - indent_shift - indent_new + 1); |
| 313 | null_at(&f->data, line_len - indent_shift + 1); |
| 314 | openfile->totsize -= indent_shift; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 315 | |
David Lawrence Ramsey | 2e8fac6 | 2006-04-29 15:44:58 +0000 | [diff] [blame] | 316 | /* Keep track of the change in the current line. */ |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 317 | if (openfile->mark_set && f == openfile->mark_begin && |
David Lawrence Ramsey | eb4f90e | 2006-05-05 14:22:42 +0000 | [diff] [blame] | 318 | openfile->mark_begin_x > indent_new) { |
| 319 | if (openfile->mark_begin_x <= indent_len) |
| 320 | openfile->mark_begin_x = indent_new; |
| 321 | else |
| 322 | openfile->mark_begin_x -= indent_shift; |
| 323 | } |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 324 | |
David Lawrence Ramsey | ac37b04 | 2006-05-03 12:59:05 +0000 | [diff] [blame] | 325 | if (f == openfile->current && openfile->current_x > |
David Lawrence Ramsey | eb4f90e | 2006-05-05 14:22:42 +0000 | [diff] [blame] | 326 | indent_new) { |
| 327 | if (openfile->current_x <= indent_len) |
| 328 | openfile->current_x = indent_new; |
| 329 | else |
| 330 | openfile->current_x -= indent_shift; |
| 331 | } |
David Lawrence Ramsey | 7194a61 | 2006-04-29 16:11:21 +0000 | [diff] [blame] | 332 | |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 333 | /* We've unindented, so set indent_changed to TRUE. */ |
| 334 | if (!indent_changed) |
| 335 | indent_changed = TRUE; |
| 336 | } |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 340 | if (!unindent) |
David Lawrence Ramsey | 2545686 | 2006-05-05 15:43:52 +0000 | [diff] [blame] | 341 | /* Clean up. */ |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 342 | free(line_indent); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 343 | |
| 344 | if (indent_changed) { |
| 345 | /* Mark the file as modified. */ |
| 346 | set_modified(); |
| 347 | |
| 348 | /* Update the screen. */ |
| 349 | edit_refresh(); |
| 350 | } |
| 351 | } |
| 352 | |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 353 | /* Indent the current line, or all lines covered by the mark if the mark |
| 354 | * is on, tabsize columns. */ |
| 355 | void do_indent_void(void) |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 356 | { |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 357 | do_indent(tabsize); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 358 | } |
| 359 | |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 360 | /* Unindent the current line, or all lines covered by the mark if the |
| 361 | * mark is on, tabsize columns. */ |
| 362 | void do_unindent(void) |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 363 | { |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 364 | do_indent(-tabsize); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 365 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 366 | |
| 367 | /* Undo the last thing(s) we did */ |
| 368 | void do_undo(void) |
| 369 | { |
| 370 | undo *u = openfile->current_undo; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 371 | filestruct *f = openfile->current, *t; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 372 | int len = 0; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 373 | char *undidmsg, *data; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 374 | filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 375 | |
| 376 | if (!u) { |
| 377 | statusbar(_("Nothing in undo buffer!")); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | if (u->lineno <= f->lineno) |
| 383 | for (; f->prev != NULL && f->lineno != u->lineno; f = f->prev) |
| 384 | ; |
| 385 | else |
| 386 | for (; f->next != NULL && f->lineno != u->lineno; f = f->next) |
| 387 | ; |
| 388 | if (f->lineno != u->lineno) { |
| 389 | statusbar(_("Couldnt match current undo line")); |
| 390 | return; |
| 391 | } |
| 392 | #ifdef DEBUG |
| 393 | fprintf(stderr, "data we're about to undo = \"%s\"\n", f->data); |
| 394 | fprintf(stderr, "Undo running for type %d\n", u->type); |
| 395 | #endif |
| 396 | |
Chris Allegretta | fa40694 | 2008-07-13 16:44:19 +0000 | [diff] [blame] | 397 | openfile->current_x = u->begin; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 398 | switch(u->type) { |
| 399 | case ADD: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 400 | undidmsg = _("text add"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 401 | len = strlen(f->data) - strlen(u->strdata) + 1; |
| 402 | data = charalloc(len); |
| 403 | strncpy(data, f->data, u->begin); |
| 404 | strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]); |
| 405 | free(f->data); |
| 406 | f->data = data; |
| 407 | break; |
| 408 | case DEL: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 409 | undidmsg = _("text delete"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 410 | len = strlen(f->data) + strlen(u->strdata) + 1; |
| 411 | data = charalloc(len); |
| 412 | |
| 413 | strncpy(data, f->data, u->begin); |
| 414 | strcpy(&data[u->begin], u->strdata); |
| 415 | strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]); |
| 416 | free(f->data); |
| 417 | f->data = data; |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 418 | if (u->xflags == UNDO_DEL_BACKSPACE) |
| 419 | openfile->current_x += strlen(u->strdata); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 420 | break; |
| 421 | case SPLIT: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 422 | undidmsg = _("line split"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 423 | free(f->data); |
| 424 | f->data = mallocstrcpy(NULL, u->strdata); |
| 425 | if (f->next != NULL) { |
| 426 | filestruct *tmp = f->next; |
| 427 | unlink_node(tmp); |
| 428 | delete_node(tmp); |
| 429 | } |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 430 | renumber(f); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 431 | break; |
| 432 | case UNSPLIT: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 433 | undidmsg = _("line join"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 434 | t = make_new_node(f); |
| 435 | t->data = mallocstrcpy(NULL, u->strdata); |
| 436 | data = mallocstrncpy(NULL, f->data, u->begin); |
| 437 | data[u->begin] = '\0'; |
| 438 | free(f->data); |
| 439 | f->data = data; |
| 440 | splice_node(f, t, f->next); |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 441 | renumber(f); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 442 | break; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 443 | case CUT: |
| 444 | case CUTTOEND: |
| 445 | undidmsg = _("text cut"); |
| 446 | cutbuffer = copy_filestruct(u->cutbuffer); |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 447 | |
| 448 | /* Compute cutbottom for the uncut using out copy */ |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 449 | for (cutbottom = cutbuffer; cutbottom->next != NULL; cutbottom = cutbottom->next) |
| 450 | ; |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 451 | |
| 452 | /* Get to where we need to uncut from */ |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 453 | if (u->mark_set && u->mark_begin_lineno < u->lineno) |
| 454 | do_gotolinecolumn(u->mark_begin_lineno, u->mark_begin_x+1, FALSE, FALSE, FALSE, FALSE); |
| 455 | else |
| 456 | do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE); |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 457 | |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 458 | do_uncut_text(); |
| 459 | free_filestruct(cutbuffer); |
Chris Allegretta | 8f76112 | 2008-08-01 06:52:15 +0000 | [diff] [blame] | 460 | cutbuffer = NULL; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 461 | break; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 462 | case INSERT: |
| 463 | undidmsg = _("text insert"); |
| 464 | cutbuffer = NULL; |
| 465 | cutbottom = NULL; |
| 466 | /* When we updated mark_begin_lineno in update_undo, it was effectively how many line |
| 467 | were inserted due to being partitioned before read_file was called. So we |
| 468 | add its value here */ |
| 469 | openfile->mark_begin = fsfromline(u->lineno + u->mark_begin_lineno - 1); |
| 470 | openfile->mark_begin_x = 0; |
| 471 | openfile->mark_set = TRUE; |
| 472 | do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE); |
| 473 | cut_marked(); |
| 474 | u->cutbuffer = cutbuffer; |
| 475 | u->cutbottom = cutbottom; |
| 476 | cutbuffer = oldcutbuffer; |
| 477 | cutbottom = oldcutbottom; |
| 478 | openfile->mark_set = FALSE; |
| 479 | break; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 480 | case REPLACE: |
| 481 | undidmsg = _("text replace"); |
| 482 | data = u->strdata; |
| 483 | u->strdata = f->data; |
| 484 | f->data = data; |
| 485 | break; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 486 | default: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 487 | undidmsg = _("wtf?"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 488 | break; |
| 489 | |
| 490 | } |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 491 | do_gotolinecolumn(u->lineno, u->begin, FALSE, FALSE, FALSE, TRUE); |
| 492 | statusbar(_("Undid action (%s)"), undidmsg); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 493 | openfile->current_undo = openfile->current_undo->next; |
| 494 | |
| 495 | } |
| 496 | |
| 497 | void do_redo(void) |
| 498 | { |
| 499 | undo *u = openfile->undotop; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 500 | filestruct *f = openfile->current, *t; |
Chris Allegretta | ea57787 | 2008-08-03 20:19:42 +0000 | [diff] [blame^] | 501 | filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 502 | int len = 0, i; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 503 | char *undidmsg, *data; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 504 | |
| 505 | for (; u != NULL && u->next != openfile->current_undo; u = u->next) |
| 506 | ; |
| 507 | if (!u) { |
| 508 | statusbar(_("Nothing to re-do!")); |
| 509 | return; |
| 510 | } |
| 511 | if (u->next != openfile->current_undo) { |
| 512 | statusbar(_("Can't find previous undo to re-do, argh")); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | if (u->lineno <= f->lineno) |
| 517 | for (; f->prev != NULL && f->lineno != u->lineno; f = f->prev) |
| 518 | ; |
| 519 | else |
| 520 | for (; f->next != NULL && f->lineno != u->lineno; f = f->next) |
| 521 | ; |
| 522 | if (f->lineno != u->lineno) { |
| 523 | statusbar(_("Couldnt match current undo line")); |
| 524 | return; |
| 525 | } |
| 526 | #ifdef DEBUG |
| 527 | fprintf(stderr, "data we're about to redo = \"%s\"\n", f->data); |
| 528 | fprintf(stderr, "Redo running for type %d\n", u->type); |
| 529 | #endif |
| 530 | |
| 531 | switch(u->type) { |
| 532 | case ADD: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 533 | undidmsg = _("text add"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 534 | len = strlen(f->data) + strlen(u->strdata) + 1; |
| 535 | data = charalloc(len); |
Chris Allegretta | 1f37c45 | 2008-08-01 04:11:57 +0000 | [diff] [blame] | 536 | strncpy(data, f->data, u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 537 | strcpy(&data[u->begin], u->strdata); |
| 538 | strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]); |
| 539 | free(f->data); |
| 540 | f->data = data; |
| 541 | break; |
| 542 | case DEL: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 543 | undidmsg = _("text delete"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 544 | len = strlen(f->data) + strlen(u->strdata) + 1; |
| 545 | data = charalloc(len); |
| 546 | strncpy(data, f->data, u->begin); |
| 547 | strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]); |
| 548 | free(f->data); |
| 549 | f->data = data; |
| 550 | break; |
| 551 | case SPLIT: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 552 | undidmsg = _("line split"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 553 | t = make_new_node(f); |
| 554 | t->data = mallocstrcpy(NULL, u->strdata); |
| 555 | data = mallocstrncpy(NULL, f->data, u->begin); |
| 556 | data[u->begin] = '\0'; |
| 557 | free(f->data); |
| 558 | f->data = data; |
| 559 | splice_node(f, t, f->next); |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 560 | renumber(f); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 561 | break; |
| 562 | case UNSPLIT: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 563 | undidmsg = _("line join"); |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 564 | len = strlen(f->data) + strlen(u->strdata + 1); |
| 565 | data = charalloc(len); |
| 566 | strcpy(data, f->data); |
| 567 | strcat(data, u->strdata); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 568 | free(f->data); |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 569 | f->data = data; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 570 | if (f->next != NULL) { |
| 571 | filestruct *tmp = f->next; |
| 572 | unlink_node(tmp); |
| 573 | delete_node(tmp); |
| 574 | } |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 575 | renumber(f); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 576 | break; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 577 | case CUT: |
| 578 | case CUTTOEND: |
| 579 | undidmsg = _("line cut"); |
| 580 | do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE); |
| 581 | openfile->mark_set = u->mark_set; |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 582 | if (cutbuffer) |
| 583 | free(cutbuffer); |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 584 | cutbuffer = NULL; |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 585 | |
| 586 | /* Move ahead the same # lines we had if a marked cut */ |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 587 | if (u->mark_set) { |
| 588 | for (i = 1, t = openfile->fileage; i != u->mark_begin_lineno; i++) |
| 589 | t = t->next; |
| 590 | openfile->mark_begin = t; |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 591 | } else if (!u->to_end) { |
| 592 | /* Here we have a regular old potentially multi-line ^K cut. We'll |
| 593 | need to trick nano into thinking it's a marked cut to cut more |
| 594 | than one line again */ |
| 595 | #ifdef DEBUG |
| 596 | fprintf(stderr, "Undoing multi-^K cut, u->linescut = %d\n", u->linescut); |
| 597 | #endif |
| 598 | for (i = 0, t = openfile->current; i < u->linescut; i++) { |
| 599 | |
| 600 | #ifdef DEBUG |
| 601 | fprintf(stderr, "Advancing, lineno = %d, data = \"%s\"\n", t->lineno, t->data); |
| 602 | #endif |
| 603 | t = t->next; |
| 604 | } |
| 605 | openfile->mark_begin = t; |
| 606 | openfile->mark_begin_x = 0; |
| 607 | openfile->mark_set = TRUE; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 608 | } |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 609 | |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 610 | openfile->mark_begin_x = u->mark_begin_x; |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 611 | do_cut_text(FALSE, u->to_end, TRUE); |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 612 | openfile->mark_set = FALSE; |
| 613 | openfile->mark_begin = NULL; |
| 614 | openfile->mark_begin_x = 0; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 615 | edit_refresh(); |
| 616 | break; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 617 | case REPLACE: |
| 618 | undidmsg = _("text replace"); |
| 619 | data = u->strdata; |
| 620 | u->strdata = f->data; |
| 621 | f->data = data; |
| 622 | break; |
Chris Allegretta | ea57787 | 2008-08-03 20:19:42 +0000 | [diff] [blame^] | 623 | case INSERT: |
| 624 | undidmsg = _("text insert"); |
| 625 | cutbuffer = u->cutbuffer; |
| 626 | cutbottom = u->cutbottom; |
| 627 | do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE); |
| 628 | do_uncut_text(); |
| 629 | cutbuffer = oldcutbuffer; |
| 630 | cutbottom = oldcutbottom; |
| 631 | break; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 632 | default: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 633 | undidmsg = _("wtf?"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 634 | break; |
| 635 | |
| 636 | } |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 637 | do_gotolinecolumn(u->lineno, u->begin, FALSE, FALSE, FALSE, TRUE); |
| 638 | statusbar(_("Redid action (%s)"), undidmsg); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 639 | |
| 640 | openfile->current_undo = u; |
| 641 | |
| 642 | } |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 643 | #endif /* !NANO_TINY */ |
| 644 | |
David Lawrence Ramsey | b0e04c0 | 2005-12-08 07:24:54 +0000 | [diff] [blame] | 645 | /* Someone hits Enter *gasp!* */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 646 | void do_enter(void) |
| 647 | { |
| 648 | filestruct *newnode = make_new_node(openfile->current); |
| 649 | size_t extra = 0; |
| 650 | |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 651 | assert(openfile->current != NULL && xopenfile->current->data != NULL); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 652 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 653 | #ifndef NANO_TINY |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 654 | update_undo(SPLIT); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 655 | |
| 656 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 657 | /* Do auto-indenting, like the neolithic Turbo Pascal editor. */ |
| 658 | if (ISSET(AUTOINDENT)) { |
| 659 | /* If we are breaking the line in the indentation, the new |
| 660 | * indentation should have only current_x characters, and |
| 661 | * current_x should not change. */ |
| 662 | extra = indent_length(openfile->current->data); |
| 663 | if (extra > openfile->current_x) |
| 664 | extra = openfile->current_x; |
| 665 | } |
| 666 | #endif |
| 667 | newnode->data = charalloc(strlen(openfile->current->data + |
| 668 | openfile->current_x) + extra + 1); |
| 669 | strcpy(&newnode->data[extra], openfile->current->data + |
| 670 | openfile->current_x); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 671 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 672 | if (ISSET(AUTOINDENT)) { |
| 673 | strncpy(newnode->data, openfile->current->data, extra); |
| 674 | openfile->totsize += mbstrlen(newnode->data); |
| 675 | } |
| 676 | #endif |
| 677 | null_at(&openfile->current->data, openfile->current_x); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 678 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 679 | if (openfile->mark_set && openfile->current == |
| 680 | openfile->mark_begin && openfile->current_x < |
| 681 | openfile->mark_begin_x) { |
| 682 | openfile->mark_begin = newnode; |
| 683 | openfile->mark_begin_x += extra - openfile->current_x; |
| 684 | } |
| 685 | #endif |
| 686 | openfile->current_x = extra; |
| 687 | |
| 688 | if (openfile->current == openfile->filebot) |
| 689 | openfile->filebot = newnode; |
| 690 | splice_node(openfile->current, newnode, |
| 691 | openfile->current->next); |
| 692 | |
| 693 | renumber(openfile->current); |
| 694 | openfile->current = newnode; |
| 695 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 696 | openfile->totsize++; |
| 697 | set_modified(); |
David Lawrence Ramsey | feb89db | 2005-09-13 04:45:46 +0000 | [diff] [blame] | 698 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 699 | openfile->placewewant = xplustabs(); |
David Lawrence Ramsey | feb89db | 2005-09-13 04:45:46 +0000 | [diff] [blame] | 700 | |
| 701 | edit_refresh(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 702 | } |
| 703 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 704 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 705 | /* Send a SIGKILL (unconditional kill) to the forked process in |
| 706 | * execute_command(). */ |
David Lawrence Ramsey | 8befda6 | 2005-12-06 19:39:56 +0000 | [diff] [blame] | 707 | RETSIGTYPE cancel_command(int signal) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 708 | { |
| 709 | if (kill(pid, SIGKILL) == -1) |
| 710 | nperror("kill"); |
| 711 | } |
| 712 | |
David Lawrence Ramsey | 0ed7171 | 2005-11-08 23:09:47 +0000 | [diff] [blame] | 713 | /* Execute command in a shell. Return TRUE on success. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 714 | bool execute_command(const char *command) |
| 715 | { |
| 716 | int fd[2]; |
| 717 | FILE *f; |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 718 | char *shellenv; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 719 | struct sigaction oldaction, newaction; |
| 720 | /* Original and temporary handlers for SIGINT. */ |
| 721 | bool sig_failed = FALSE; |
| 722 | /* Did sigaction() fail without changing the signal handlers? */ |
| 723 | |
| 724 | /* Make our pipes. */ |
| 725 | if (pipe(fd) == -1) { |
| 726 | statusbar(_("Could not pipe")); |
| 727 | return FALSE; |
| 728 | } |
| 729 | |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 730 | /* Check $SHELL for the shell to use. If it isn't set, use |
David Lawrence Ramsey | 1932dfb | 2005-11-29 05:52:49 +0000 | [diff] [blame] | 731 | * /bin/sh. Note that $SHELL should contain only a path, with no |
| 732 | * arguments. */ |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 733 | shellenv = getenv("SHELL"); |
| 734 | if (shellenv == NULL) |
| 735 | shellenv = "/bin/sh"; |
| 736 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 737 | /* Fork a child. */ |
| 738 | if ((pid = fork()) == 0) { |
| 739 | close(fd[0]); |
| 740 | dup2(fd[1], fileno(stdout)); |
| 741 | dup2(fd[1], fileno(stderr)); |
| 742 | |
| 743 | /* If execl() returns at all, there was an error. */ |
David Lawrence Ramsey | 5da68ee | 2005-11-29 05:21:06 +0000 | [diff] [blame] | 744 | execl(shellenv, tail(shellenv), "-c", command, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 745 | exit(0); |
| 746 | } |
| 747 | |
David Lawrence Ramsey | c838a4c | 2006-04-26 18:33:50 +0000 | [diff] [blame] | 748 | /* Continue as parent. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 749 | close(fd[1]); |
| 750 | |
| 751 | if (pid == -1) { |
| 752 | close(fd[0]); |
| 753 | statusbar(_("Could not fork")); |
| 754 | return FALSE; |
| 755 | } |
| 756 | |
| 757 | /* Before we start reading the forked command's output, we set |
| 758 | * things up so that Ctrl-C will cancel the new process. */ |
| 759 | |
| 760 | /* Enable interpretation of the special control keys so that we get |
| 761 | * SIGINT when Ctrl-C is pressed. */ |
| 762 | enable_signals(); |
| 763 | |
| 764 | if (sigaction(SIGINT, NULL, &newaction) == -1) { |
| 765 | sig_failed = TRUE; |
| 766 | nperror("sigaction"); |
| 767 | } else { |
| 768 | newaction.sa_handler = cancel_command; |
| 769 | if (sigaction(SIGINT, &newaction, &oldaction) == -1) { |
| 770 | sig_failed = TRUE; |
| 771 | nperror("sigaction"); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /* Note that now oldaction is the previous SIGINT signal handler, |
| 776 | * to be restored later. */ |
| 777 | |
| 778 | f = fdopen(fd[0], "rb"); |
| 779 | if (f == NULL) |
| 780 | nperror("fdopen"); |
| 781 | |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 782 | read_file(f, "stdin", TRUE); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 783 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 784 | if (wait(NULL) == -1) |
| 785 | nperror("wait"); |
| 786 | |
| 787 | if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1) |
| 788 | nperror("sigaction"); |
| 789 | |
David Lawrence Ramsey | 8b9c91b | 2007-12-18 01:28:53 +0000 | [diff] [blame] | 790 | /* Restore the terminal to its previous state. In the process, |
| 791 | * disable interpretation of the special control keys so that we can |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 792 | * use Ctrl-C for other things. */ |
David Lawrence Ramsey | 8b9c91b | 2007-12-18 01:28:53 +0000 | [diff] [blame] | 793 | terminal_init(); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 794 | |
| 795 | return TRUE; |
| 796 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 797 | |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 798 | /* Add a new undo struct to the top of the current pile */ |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 799 | void add_undo(undo_type current_action) |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 800 | { |
Chris Allegretta | 8f76112 | 2008-08-01 06:52:15 +0000 | [diff] [blame] | 801 | undo *u; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 802 | char *data; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 803 | openfilestruct *fs = openfile; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 804 | |
Chris Allegretta | 8f76112 | 2008-08-01 06:52:15 +0000 | [diff] [blame] | 805 | /* Ugh, if we were called while cutting not-to-end, non-marked and on the same lineno, |
| 806 | we need to abort here */ |
| 807 | u = fs->current_undo; |
| 808 | if (u && u->type == CUT && !u->mark_set && u->lineno == fs->current->lineno) |
| 809 | return; |
| 810 | |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 811 | /* Blow away the old undo stack if we are starting from the middle */ |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 812 | while (fs->undotop != NULL && fs->undotop != fs->current_undo) { |
| 813 | undo *u2 = fs->undotop; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 814 | fs->undotop = fs->undotop->next; |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 815 | if (u2->strdata != NULL) |
| 816 | free(u2->strdata); |
Chris Allegretta | ea57787 | 2008-08-03 20:19:42 +0000 | [diff] [blame^] | 817 | if (u2->cutbuffer); |
| 818 | free_filestruct(u2->cutbuffer); |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 819 | free(u2); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Chris Allegretta | 8f76112 | 2008-08-01 06:52:15 +0000 | [diff] [blame] | 822 | /* Allocate and initialize a new undo type */ |
| 823 | u = nmalloc(sizeof(undo)); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 824 | u->type = current_action; |
| 825 | u->lineno = fs->current->lineno; |
| 826 | u->begin = fs->current_x; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 827 | u->next = fs->undotop; |
| 828 | fs->undotop = u; |
| 829 | fs->current_undo = u; |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 830 | u->strdata = NULL; |
| 831 | u->cutbuffer = NULL; |
| 832 | u->cutbottom = NULL; |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 833 | u->mark_begin_lineno = 0; |
| 834 | u->mark_begin_x = 0; |
| 835 | u->linescut = 0; |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 836 | u->xflags = 0; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 837 | |
| 838 | switch (u->type) { |
| 839 | /* We need to start copying data into the undo buffer or we wont be able |
| 840 | to restore it later */ |
| 841 | case ADD: |
| 842 | data = charalloc(2); |
| 843 | data[0] = fs->current->data[fs->current_x]; |
| 844 | data[1] = '\0'; |
| 845 | u->strdata = data; |
| 846 | break; |
| 847 | case DEL: |
| 848 | if (u->begin != strlen(fs->current->data)) { |
| 849 | data = mallocstrncpy(NULL, &fs->current->data[u->begin], 2); |
| 850 | data[1] = '\0'; |
| 851 | u->strdata = data; |
| 852 | break; |
| 853 | } |
| 854 | /* Else purposely fall into unsplit code */ |
| 855 | current_action = u->type = UNSPLIT; |
| 856 | case UNSPLIT: |
| 857 | if (fs->current->next) { |
| 858 | data = mallocstrcpy(NULL, fs->current->next->data); |
| 859 | u->strdata = data; |
| 860 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 861 | break; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 862 | case INSERT: |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 863 | case SPLIT: |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 864 | case REPLACE: |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 865 | data = mallocstrcpy(NULL, fs->current->data); |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 866 | u->strdata = data; |
| 867 | break; |
| 868 | case CUT: |
| 869 | case CUTTOEND: |
| 870 | u->mark_set = openfile->mark_set; |
| 871 | if (u->mark_set) { |
| 872 | u->mark_begin_lineno = openfile->mark_begin->lineno; |
| 873 | u->mark_begin_x = openfile->mark_begin_x; |
| 874 | } |
| 875 | u->to_end = (current_action == CUTTOEND); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 876 | break; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 877 | case UNCUT: |
| 878 | break; |
| 879 | case OTHER: |
| 880 | statusbar(_("OOPS. Tried to add unknown thing to undo struct, I'd save your work")); |
| 881 | break; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | #ifdef DEBUG |
| 885 | fprintf(stderr, "fs->current->data = \"%s\", current_x = %d, u->begin = %d, type = %d\n", |
| 886 | fs->current->data, fs->current_x, u->begin, current_action); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 887 | fprintf(stderr, "left update_add...\n"); |
| 888 | #endif |
| 889 | fs->last_action = current_action; |
| 890 | } |
| 891 | |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 892 | /* Update an undo item, or determine whether a new one |
| 893 | is really needed and bounce the data to add_undo |
| 894 | instead. The latter functionality just feels |
| 895 | gimmicky and may just be more hassle than |
| 896 | it's worth, so it should be axed if needed. */ |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 897 | void update_undo(undo_type action) |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 898 | { |
| 899 | undo *u; |
| 900 | char *data; |
| 901 | int len = 0; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 902 | openfilestruct *fs = openfile; |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 903 | |
| 904 | #ifdef DEBUG |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 905 | fprintf(stderr, "action = %d, fs->last_action = %d, openfile->current->lineno = %d", |
| 906 | action, fs->last_action, openfile->current->lineno); |
| 907 | if (fs->current_undo) |
| 908 | fprintf(stderr, "fs->current_undo->lineno = %d\n", fs->current_undo->lineno); |
| 909 | else |
| 910 | fprintf(stderr, "\n"); |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 911 | #endif |
| 912 | |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 913 | /* Change to an add if we're not using the same undo struct |
| 914 | that we should be using */ |
| 915 | if (action != fs->last_action |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 916 | || (action != CUT && action != CUTTOEND && action != INSERT |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 917 | && openfile->current->lineno != fs->current_undo->lineno)) { |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 918 | add_undo(action); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | |
| 922 | assert(fs->undotop != NULL); |
| 923 | u = fs->undotop; |
| 924 | |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 925 | switch (u->type) { |
| 926 | case ADD: |
| 927 | #ifdef DEBUG |
| 928 | fprintf(stderr, "fs->current->data = \"%s\", current_x = %d, u->begin = %d\n", |
| 929 | fs->current->data, fs->current_x, u->begin); |
| 930 | #endif |
| 931 | len = strlen(u->strdata) + 2; |
| 932 | data = nrealloc((void *) u->strdata, len * sizeof(char *)); |
| 933 | data[len-2] = fs->current->data[fs->current_x]; |
| 934 | data[len-1] = '\0'; |
| 935 | u->strdata = (char *) data; |
| 936 | #ifdef DEBUG |
| 937 | fprintf(stderr, "current undo data now \"%s\"\n", u->strdata); |
| 938 | #endif |
| 939 | break; |
| 940 | case DEL: |
| 941 | len = strlen(u->strdata) + 2; |
| 942 | assert(len > 2); |
| 943 | if (fs->current_x == u->begin) { |
| 944 | /* They're deleting */ |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 945 | if (!u->xflags) |
| 946 | u->xflags = UNDO_DEL_DEL; |
| 947 | else if (u->xflags != UNDO_DEL_DEL) { |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 948 | add_undo(action); |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 949 | return; |
| 950 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 951 | data = charalloc(len); |
| 952 | strcpy(data, u->strdata); |
| 953 | data[len-2] = fs->current->data[fs->current_x];; |
| 954 | data[len-1] = '\0'; |
| 955 | free(u->strdata); |
| 956 | u->strdata = data; |
| 957 | } else if (fs->current_x == u->begin - 1) { |
| 958 | /* They're backspacing */ |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 959 | if (!u->xflags) |
| 960 | u->xflags = UNDO_DEL_BACKSPACE; |
| 961 | else if (u->xflags != UNDO_DEL_BACKSPACE) { |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 962 | add_undo(action); |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 963 | return; |
| 964 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 965 | data = charalloc(len); |
| 966 | data[0] = fs->current->data[fs->current_x]; |
| 967 | strcpy(&data[1], u->strdata); |
| 968 | free(u->strdata); |
| 969 | u->strdata = data; |
| 970 | u->begin--; |
| 971 | } else { |
| 972 | /* They deleted something else on the line */ |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 973 | add_undo(DEL); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | #ifdef DEBUG |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 977 | fprintf(stderr, "current undo data now \"%s\"\nu->begin = %d\n", u->strdata, u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 978 | #endif |
| 979 | break; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 980 | case CUT: |
| 981 | case CUTTOEND: |
| 982 | case UNCUT: |
Chris Allegretta | 8f76112 | 2008-08-01 06:52:15 +0000 | [diff] [blame] | 983 | if (u->cutbuffer) |
| 984 | free(u->cutbuffer); |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 985 | u->cutbuffer = copy_filestruct(cutbuffer); |
| 986 | u->cutbottom = cutbottom; |
Chris Allegretta | d31ddb7 | 2008-08-01 07:29:06 +0000 | [diff] [blame] | 987 | u->linescut++; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 988 | break; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 989 | case REPLACE: |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 990 | add_undo(action); |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 991 | break; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 992 | case INSERT: |
| 993 | u->mark_begin_lineno = openfile->current->lineno; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 994 | case SPLIT: |
| 995 | case UNSPLIT: |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 996 | /* These cases are handled by the earlier check for a new line and action */ |
| 997 | case OTHER: |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 998 | break; |
| 999 | } |
| 1000 | |
| 1001 | #ifdef DEBUG |
| 1002 | fprintf(stderr, "Done in udpate_undo (type was %d)\n", action); |
| 1003 | #endif |
| 1004 | if (fs->last_action != action) { |
| 1005 | #ifdef DEBUG |
| 1006 | fprintf(stderr, "Starting add_undo for new action as it does not match last_action\n"); |
| 1007 | #endif |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 1008 | add_undo(action); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1009 | } |
| 1010 | fs->last_action = action; |
| 1011 | } |
| 1012 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1013 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1014 | |
| 1015 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | ef0d5a7 | 2006-05-22 02:08:49 +0000 | [diff] [blame] | 1016 | /* Unset the prepend_wrap flag. We need to do this as soon as we do |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1017 | * something other than type text. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1018 | void wrap_reset(void) |
| 1019 | { |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1020 | prepend_wrap = FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /* We wrap the given line. Precondition: we assume the cursor has been |
David Lawrence Ramsey | 139fa65 | 2006-05-22 01:26:24 +0000 | [diff] [blame] | 1024 | * moved forward since the last typed character. Return TRUE if we |
| 1025 | * wrapped, and FALSE otherwise. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1026 | bool do_wrap(filestruct *line) |
| 1027 | { |
| 1028 | size_t line_len; |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1029 | /* The length of the line we wrap. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1030 | ssize_t wrap_loc; |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1031 | /* The index of line->data where we wrap. */ |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1032 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1033 | const char *indent_string = NULL; |
| 1034 | /* Indentation to prepend to the new line. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1035 | size_t indent_len = 0; |
| 1036 | /* The length of indent_string. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1037 | #endif |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1038 | const char *after_break; |
| 1039 | /* The text after the wrap point. */ |
| 1040 | size_t after_break_len; |
| 1041 | /* The length of after_break. */ |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1042 | bool prepending = FALSE; |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1043 | /* Do we prepend to the next line? */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1044 | const char *next_line = NULL; |
| 1045 | /* The next line, minus indentation. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1046 | size_t next_line_len = 0; |
| 1047 | /* The length of next_line. */ |
| 1048 | char *new_line = NULL; |
| 1049 | /* The line we create. */ |
| 1050 | size_t new_line_len = 0; |
| 1051 | /* The eventual length of new_line. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1052 | |
| 1053 | /* There are three steps. First, we decide where to wrap. Then, we |
| 1054 | * create the new wrap line. Finally, we clean up. */ |
| 1055 | |
| 1056 | /* Step 1, finding where to wrap. We are going to add a new line |
| 1057 | * after a blank character. In this step, we call break_line() to |
| 1058 | * get the location of the last blank we can break the line at, and |
David Lawrence Ramsey | d4686b8 | 2006-06-11 19:14:14 +0000 | [diff] [blame] | 1059 | * set wrap_loc to the location of the character after it, so that |
| 1060 | * the blank is preserved at the end of the line. |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1061 | * |
| 1062 | * If there is no legal wrap point, or we reach the last character |
| 1063 | * of the line while trying to find one, we should return without |
| 1064 | * wrapping. Note that if autoindent is turned on, we don't break |
| 1065 | * at the end of it! */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1066 | assert(line != NULL && line->data != NULL); |
| 1067 | |
| 1068 | /* Save the length of the line. */ |
| 1069 | line_len = strlen(line->data); |
| 1070 | |
| 1071 | /* Find the last blank where we can break the line. */ |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1072 | wrap_loc = break_line(line->data, fill |
| 1073 | #ifndef DISABLE_HELP |
| 1074 | , FALSE |
| 1075 | #endif |
| 1076 | ); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1077 | |
| 1078 | /* If we couldn't break the line, or we've reached the end of it, we |
| 1079 | * don't wrap. */ |
| 1080 | if (wrap_loc == -1 || line->data[wrap_loc] == '\0') |
| 1081 | return FALSE; |
| 1082 | |
| 1083 | /* Otherwise, move forward to the character just after the blank. */ |
| 1084 | wrap_loc += move_mbright(line->data + wrap_loc, 0); |
| 1085 | |
| 1086 | /* If we've reached the end of the line, we don't wrap. */ |
| 1087 | if (line->data[wrap_loc] == '\0') |
| 1088 | return FALSE; |
| 1089 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1090 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1091 | /* If autoindent is turned on, and we're on the character just after |
| 1092 | * the indentation, we don't wrap. */ |
| 1093 | if (ISSET(AUTOINDENT)) { |
| 1094 | /* Get the indentation of this line. */ |
| 1095 | indent_string = line->data; |
| 1096 | indent_len = indent_length(indent_string); |
| 1097 | |
| 1098 | if (wrap_loc == indent_len) |
| 1099 | return FALSE; |
| 1100 | } |
| 1101 | #endif |
| 1102 | |
| 1103 | /* Step 2, making the new wrap line. It will consist of indentation |
| 1104 | * followed by the text after the wrap point, optionally followed by |
| 1105 | * a space (if the text after the wrap point doesn't end in a blank) |
David Lawrence Ramsey | 03979d7 | 2006-06-11 19:15:59 +0000 | [diff] [blame] | 1106 | * and the text of the next line, if they can fit without wrapping, |
| 1107 | * the next line exists, and the prepend_wrap flag is set. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* after_break is the text that will be wrapped to the next line. */ |
| 1110 | after_break = line->data + wrap_loc; |
| 1111 | after_break_len = line_len - wrap_loc; |
| 1112 | |
| 1113 | assert(strlen(after_break) == after_break_len); |
| 1114 | |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1115 | /* We prepend the wrapped text to the next line, if the prepend_wrap |
| 1116 | * flag is set, there is a next line, and prepending would not make |
| 1117 | * the line too long. */ |
| 1118 | if (prepend_wrap && line != openfile->filebot) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1119 | const char *end = after_break + move_mbleft(after_break, |
| 1120 | after_break_len); |
| 1121 | |
| 1122 | /* If after_break doesn't end in a blank, make sure it ends in a |
| 1123 | * space. */ |
| 1124 | if (!is_blank_mbchar(end)) { |
| 1125 | line_len++; |
| 1126 | line->data = charealloc(line->data, line_len + 1); |
| 1127 | line->data[line_len - 1] = ' '; |
| 1128 | line->data[line_len] = '\0'; |
| 1129 | after_break = line->data + wrap_loc; |
| 1130 | after_break_len++; |
| 1131 | openfile->totsize++; |
| 1132 | } |
| 1133 | |
| 1134 | next_line = line->next->data; |
| 1135 | next_line_len = strlen(next_line); |
| 1136 | |
| 1137 | if (after_break_len + next_line_len <= fill) { |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1138 | prepending = TRUE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1139 | new_line_len += next_line_len; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | /* new_line_len is now the length of the text that will be wrapped |
| 1144 | * to the next line, plus (if we're prepending to it) the length of |
| 1145 | * the text of the next line. */ |
| 1146 | new_line_len += after_break_len; |
| 1147 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1148 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1149 | if (ISSET(AUTOINDENT)) { |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1150 | if (prepending) { |
| 1151 | /* If we're prepending, the indentation will come from the |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1152 | * next line. */ |
| 1153 | indent_string = next_line; |
| 1154 | indent_len = indent_length(indent_string); |
| 1155 | next_line += indent_len; |
| 1156 | } else { |
| 1157 | /* Otherwise, it will come from this line, in which case |
| 1158 | * we should increase new_line_len to make room for it. */ |
| 1159 | new_line_len += indent_len; |
| 1160 | openfile->totsize += mbstrnlen(indent_string, indent_len); |
| 1161 | } |
| 1162 | } |
| 1163 | #endif |
| 1164 | |
| 1165 | /* Now we allocate the new line and copy the text into it. */ |
| 1166 | new_line = charalloc(new_line_len + 1); |
| 1167 | new_line[0] = '\0'; |
| 1168 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1169 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1170 | if (ISSET(AUTOINDENT)) { |
| 1171 | /* Copy the indentation. */ |
| 1172 | strncpy(new_line, indent_string, indent_len); |
| 1173 | new_line[indent_len] = '\0'; |
| 1174 | new_line_len += indent_len; |
| 1175 | } |
| 1176 | #endif |
| 1177 | |
| 1178 | /* Copy all the text after the wrap point of the current line. */ |
| 1179 | strcat(new_line, after_break); |
| 1180 | |
| 1181 | /* Break the current line at the wrap point. */ |
| 1182 | null_at(&line->data, wrap_loc); |
| 1183 | |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1184 | if (prepending) { |
| 1185 | /* If we're prepending, copy the text from the next line, minus |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1186 | * the indentation that we already copied above. */ |
| 1187 | strcat(new_line, next_line); |
| 1188 | |
| 1189 | free(line->next->data); |
| 1190 | line->next->data = new_line; |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1191 | |
| 1192 | /* If the NO_NEWLINES flag isn't set, and text has been added to |
| 1193 | * the magicline, make a new magicline. */ |
| 1194 | if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0') |
| 1195 | new_magicline(); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1196 | } else { |
| 1197 | /* Otherwise, make a new line and copy the text after where we |
| 1198 | * broke this line to the beginning of the new line. */ |
| 1199 | splice_node(openfile->current, make_new_node(openfile->current), |
| 1200 | openfile->current->next); |
| 1201 | |
David Lawrence Ramsey | 219a814 | 2005-11-22 22:08:01 +0000 | [diff] [blame] | 1202 | /* If the current line is the last line of the file, move the |
| 1203 | * last line of the file down to the next line. */ |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1204 | if (openfile->filebot == openfile->current) |
| 1205 | openfile->filebot = openfile->current->next; |
| 1206 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1207 | openfile->current->next->data = new_line; |
| 1208 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1209 | openfile->totsize++; |
| 1210 | } |
| 1211 | |
| 1212 | /* Step 3, clean up. Reposition the cursor and mark, and do some |
| 1213 | * other sundry things. */ |
| 1214 | |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1215 | /* Set the prepend_wrap flag, so that later wraps of this line will |
| 1216 | * be prepended to the next line. */ |
| 1217 | prepend_wrap = TRUE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1218 | |
David Lawrence Ramsey | 2829aae | 2006-05-22 01:24:09 +0000 | [diff] [blame] | 1219 | /* Each line knows its number. We recalculate these if we inserted |
| 1220 | * a new line. */ |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1221 | if (!prepending) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1222 | renumber(line); |
| 1223 | |
| 1224 | /* If the cursor was after the break point, we must move it. We |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1225 | * also clear the prepend_wrap flag in this case. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1226 | if (openfile->current_x > wrap_loc) { |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1227 | prepend_wrap = FALSE; |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1228 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1229 | openfile->current = openfile->current->next; |
| 1230 | openfile->current_x -= wrap_loc |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1231 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1232 | - indent_len |
| 1233 | #endif |
| 1234 | ; |
| 1235 | openfile->placewewant = xplustabs(); |
| 1236 | } |
| 1237 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1238 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1239 | /* If the mark was on this line after the wrap point, we move it |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1240 | * down. If it was on the next line and we prepended to that line, |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1241 | * we move it right. */ |
| 1242 | if (openfile->mark_set) { |
| 1243 | if (openfile->mark_begin == line && openfile->mark_begin_x > |
| 1244 | wrap_loc) { |
| 1245 | openfile->mark_begin = line->next; |
| 1246 | openfile->mark_begin_x -= wrap_loc - indent_len + 1; |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 1247 | } else if (prepending && openfile->mark_begin == line->next) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1248 | openfile->mark_begin_x += after_break_len; |
| 1249 | } |
| 1250 | #endif |
| 1251 | |
| 1252 | return TRUE; |
| 1253 | } |
| 1254 | #endif /* !DISABLE_WRAPPING */ |
| 1255 | |
David Lawrence Ramsey | c7c04bb | 2005-11-29 21:30:00 +0000 | [diff] [blame] | 1256 | #if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1257 | /* We are trying to break a chunk off line. We find the last blank such |
David Lawrence Ramsey | cd9a5f0 | 2005-09-20 06:12:54 +0000 | [diff] [blame] | 1258 | * that the display length to there is at most (goal + 1). If there is |
| 1259 | * no such blank, then we find the first blank. We then take the last |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1260 | * blank in that group of blanks. The terminating '\0' counts as a |
| 1261 | * blank, as does a '\n' if newline is TRUE. */ |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1262 | ssize_t break_line(const char *line, ssize_t goal |
| 1263 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1264 | , bool newln |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1265 | #endif |
| 1266 | ) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1267 | { |
| 1268 | ssize_t blank_loc = -1; |
| 1269 | /* Current tentative return value. Index of the last blank we |
| 1270 | * found with short enough display width. */ |
| 1271 | ssize_t cur_loc = 0; |
| 1272 | /* Current index in line. */ |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 1273 | size_t cur_pos = 0; |
| 1274 | /* Current column position in line. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1275 | int line_len; |
| 1276 | |
| 1277 | assert(line != NULL); |
| 1278 | |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 1279 | while (*line != '\0' && goal >= cur_pos) { |
| 1280 | line_len = parse_mbchar(line, NULL, &cur_pos); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1281 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1282 | if (is_blank_mbchar(line) |
| 1283 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1284 | || (newln && *line == '\n') |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1285 | #endif |
| 1286 | ) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1287 | blank_loc = cur_loc; |
| 1288 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1289 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1290 | if (newln && *line == '\n') |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1291 | break; |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1292 | #endif |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1295 | line += line_len; |
| 1296 | cur_loc += line_len; |
| 1297 | } |
| 1298 | |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 1299 | if (goal >= cur_pos) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1300 | /* In fact, the whole line displays shorter than goal. */ |
| 1301 | return cur_loc; |
| 1302 | |
Chris Allegretta | 09b8124 | 2008-07-12 01:54:49 +0000 | [diff] [blame] | 1303 | #ifndef DISABLE_HELP |
| 1304 | if (newln && blank_loc <= 0) { |
| 1305 | /* If blank was not found or was found only first character, |
| 1306 | * force line break. */ |
| 1307 | cur_loc -= line_len; |
| 1308 | return cur_loc; |
| 1309 | } |
| 1310 | #endif |
| 1311 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1312 | if (blank_loc == -1) { |
| 1313 | /* No blank was found that was short enough. */ |
| 1314 | bool found_blank = FALSE; |
David Lawrence Ramsey | 5ab12ca | 2005-09-20 17:52:52 +0000 | [diff] [blame] | 1315 | ssize_t found_blank_loc = 0; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1316 | |
| 1317 | while (*line != '\0') { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1318 | line_len = parse_mbchar(line, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1319 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1320 | if (is_blank_mbchar(line) |
| 1321 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1322 | || (newln && *line == '\n') |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1323 | #endif |
| 1324 | ) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1325 | if (!found_blank) |
| 1326 | found_blank = TRUE; |
David Lawrence Ramsey | bdc1b9b | 2005-09-20 16:36:08 +0000 | [diff] [blame] | 1327 | found_blank_loc = cur_loc; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1328 | } else if (found_blank) |
David Lawrence Ramsey | bdc1b9b | 2005-09-20 16:36:08 +0000 | [diff] [blame] | 1329 | return found_blank_loc; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1330 | |
| 1331 | line += line_len; |
| 1332 | cur_loc += line_len; |
| 1333 | } |
| 1334 | |
| 1335 | return -1; |
| 1336 | } |
| 1337 | |
| 1338 | /* Move to the last blank after blank_loc, if there is one. */ |
| 1339 | line -= cur_loc; |
| 1340 | line += blank_loc; |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1341 | line_len = parse_mbchar(line, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1342 | line += line_len; |
| 1343 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1344 | while (*line != '\0' && (is_blank_mbchar(line) |
| 1345 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1346 | || (newln && *line == '\n') |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1347 | #endif |
| 1348 | )) { |
David Lawrence Ramsey | cd243f5 | 2006-05-19 23:27:16 +0000 | [diff] [blame] | 1349 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1350 | if (newln && *line == '\n') |
David Lawrence Ramsey | cd243f5 | 2006-05-19 23:27:16 +0000 | [diff] [blame] | 1351 | break; |
| 1352 | #endif |
| 1353 | |
David Lawrence Ramsey | 39bd1b3 | 2006-05-20 13:11:56 +0000 | [diff] [blame] | 1354 | line_len = parse_mbchar(line, NULL, NULL); |
| 1355 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1356 | line += line_len; |
| 1357 | blank_loc += line_len; |
| 1358 | } |
| 1359 | |
| 1360 | return blank_loc; |
| 1361 | } |
David Lawrence Ramsey | c7c04bb | 2005-11-29 21:30:00 +0000 | [diff] [blame] | 1362 | #endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1363 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1364 | #if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1365 | /* The "indentation" of a line is the whitespace between the quote part |
| 1366 | * and the non-whitespace of the line. */ |
| 1367 | size_t indent_length(const char *line) |
| 1368 | { |
| 1369 | size_t len = 0; |
| 1370 | char *blank_mb; |
| 1371 | int blank_mb_len; |
| 1372 | |
| 1373 | assert(line != NULL); |
| 1374 | |
| 1375 | blank_mb = charalloc(mb_cur_max()); |
| 1376 | |
| 1377 | while (*line != '\0') { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1378 | blank_mb_len = parse_mbchar(line, blank_mb, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1379 | |
| 1380 | if (!is_blank_mbchar(blank_mb)) |
| 1381 | break; |
| 1382 | |
| 1383 | line += blank_mb_len; |
| 1384 | len += blank_mb_len; |
| 1385 | } |
| 1386 | |
| 1387 | free(blank_mb); |
| 1388 | |
| 1389 | return len; |
| 1390 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1391 | #endif /* !NANO_TINY || !DISABLE_JUSTIFY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1392 | |
| 1393 | #ifndef DISABLE_JUSTIFY |
| 1394 | /* justify_format() replaces blanks with spaces and multiple spaces by 1 |
| 1395 | * (except it maintains up to 2 after a character in punct optionally |
| 1396 | * followed by a character in brackets, and removes all from the end). |
| 1397 | * |
| 1398 | * justify_format() might make paragraph->data shorter, and change the |
| 1399 | * actual pointer with null_at(). |
| 1400 | * |
| 1401 | * justify_format() will not look at the first skip characters of |
| 1402 | * paragraph. skip should be at most strlen(paragraph->data). The |
| 1403 | * character at paragraph[skip + 1] must not be blank. */ |
| 1404 | void justify_format(filestruct *paragraph, size_t skip) |
| 1405 | { |
| 1406 | char *end, *new_end, *new_paragraph_data; |
| 1407 | size_t shift = 0; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1408 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1409 | size_t mark_shift = 0; |
| 1410 | #endif |
| 1411 | |
| 1412 | /* These four asserts are assumptions about the input data. */ |
| 1413 | assert(paragraph != NULL); |
| 1414 | assert(paragraph->data != NULL); |
| 1415 | assert(skip < strlen(paragraph->data)); |
| 1416 | assert(!is_blank_mbchar(paragraph->data + skip)); |
| 1417 | |
| 1418 | end = paragraph->data + skip; |
| 1419 | new_paragraph_data = charalloc(strlen(paragraph->data) + 1); |
| 1420 | strncpy(new_paragraph_data, paragraph->data, skip); |
| 1421 | new_end = new_paragraph_data + skip; |
| 1422 | |
| 1423 | while (*end != '\0') { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1424 | int end_len; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1425 | |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1426 | /* If this character is blank, change it to a space if |
| 1427 | * necessary, and skip over all blanks after it. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1428 | if (is_blank_mbchar(end)) { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1429 | end_len = parse_mbchar(end, NULL, NULL); |
| 1430 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1431 | *new_end = ' '; |
| 1432 | new_end++; |
| 1433 | end += end_len; |
| 1434 | |
| 1435 | while (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1436 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1437 | |
| 1438 | end += end_len; |
| 1439 | shift += end_len; |
| 1440 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1441 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1442 | /* Keep track of the change in the current line. */ |
| 1443 | if (openfile->mark_set && openfile->mark_begin == |
| 1444 | paragraph && openfile->mark_begin_x >= end - |
| 1445 | paragraph->data) |
| 1446 | mark_shift += end_len; |
| 1447 | #endif |
| 1448 | } |
| 1449 | /* If this character is punctuation optionally followed by a |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1450 | * bracket and then followed by blanks, change no more than two |
| 1451 | * of the blanks to spaces if necessary, and skip over all |
| 1452 | * blanks after them. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1453 | } else if (mbstrchr(punct, end) != NULL) { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1454 | end_len = parse_mbchar(end, NULL, NULL); |
| 1455 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1456 | while (end_len > 0) { |
| 1457 | *new_end = *end; |
| 1458 | new_end++; |
| 1459 | end++; |
| 1460 | end_len--; |
| 1461 | } |
| 1462 | |
| 1463 | if (*end != '\0' && mbstrchr(brackets, end) != NULL) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1464 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1465 | |
| 1466 | while (end_len > 0) { |
| 1467 | *new_end = *end; |
| 1468 | new_end++; |
| 1469 | end++; |
| 1470 | end_len--; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | if (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1475 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1476 | |
| 1477 | *new_end = ' '; |
| 1478 | new_end++; |
| 1479 | end += end_len; |
| 1480 | } |
| 1481 | |
| 1482 | if (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1483 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1484 | |
| 1485 | *new_end = ' '; |
| 1486 | new_end++; |
| 1487 | end += end_len; |
| 1488 | } |
| 1489 | |
| 1490 | while (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1491 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1492 | |
| 1493 | end += end_len; |
| 1494 | shift += end_len; |
| 1495 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1496 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1497 | /* Keep track of the change in the current line. */ |
| 1498 | if (openfile->mark_set && openfile->mark_begin == |
| 1499 | paragraph && openfile->mark_begin_x >= end - |
| 1500 | paragraph->data) |
| 1501 | mark_shift += end_len; |
| 1502 | #endif |
| 1503 | } |
| 1504 | /* If this character is neither blank nor punctuation, leave it |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1505 | * unchanged. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1506 | } else { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1507 | end_len = parse_mbchar(end, NULL, NULL); |
| 1508 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1509 | while (end_len > 0) { |
| 1510 | *new_end = *end; |
| 1511 | new_end++; |
| 1512 | end++; |
| 1513 | end_len--; |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | assert(*end == '\0'); |
| 1519 | |
| 1520 | *new_end = *end; |
| 1521 | |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1522 | /* If there are spaces at the end of the line, remove them. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1523 | while (new_end > new_paragraph_data + skip && |
| 1524 | *(new_end - 1) == ' ') { |
| 1525 | new_end--; |
| 1526 | shift++; |
| 1527 | } |
| 1528 | |
| 1529 | if (shift > 0) { |
| 1530 | openfile->totsize -= shift; |
| 1531 | null_at(&new_paragraph_data, new_end - new_paragraph_data); |
| 1532 | free(paragraph->data); |
| 1533 | paragraph->data = new_paragraph_data; |
| 1534 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1535 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1536 | /* Adjust the mark coordinates to compensate for the change in |
| 1537 | * the current line. */ |
| 1538 | if (openfile->mark_set && openfile->mark_begin == paragraph) { |
| 1539 | openfile->mark_begin_x -= mark_shift; |
| 1540 | if (openfile->mark_begin_x > new_end - new_paragraph_data) |
| 1541 | openfile->mark_begin_x = new_end - new_paragraph_data; |
| 1542 | } |
| 1543 | #endif |
| 1544 | } else |
| 1545 | free(new_paragraph_data); |
| 1546 | } |
| 1547 | |
| 1548 | /* The "quote part" of a line is the largest initial substring matching |
| 1549 | * the quote string. This function returns the length of the quote part |
| 1550 | * of the given line. |
| 1551 | * |
| 1552 | * Note that if !HAVE_REGEX_H then we match concatenated copies of |
| 1553 | * quotestr. */ |
| 1554 | size_t quote_length(const char *line) |
| 1555 | { |
| 1556 | #ifdef HAVE_REGEX_H |
| 1557 | regmatch_t matches; |
| 1558 | int rc = regexec("ereg, line, 1, &matches, 0); |
| 1559 | |
| 1560 | if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1) |
| 1561 | return 0; |
| 1562 | /* matches.rm_so should be 0, since the quote string should start |
| 1563 | * with the caret ^. */ |
| 1564 | return matches.rm_eo; |
| 1565 | #else /* !HAVE_REGEX_H */ |
| 1566 | size_t qdepth = 0; |
| 1567 | |
| 1568 | /* Compute quote depth level. */ |
| 1569 | while (strncmp(line + qdepth, quotestr, quotelen) == 0) |
| 1570 | qdepth += quotelen; |
| 1571 | return qdepth; |
| 1572 | #endif /* !HAVE_REGEX_H */ |
| 1573 | } |
| 1574 | |
| 1575 | /* a_line and b_line are lines of text. The quotation part of a_line is |
| 1576 | * the first a_quote characters. Check that the quotation part of |
| 1577 | * b_line is the same. */ |
| 1578 | bool quotes_match(const char *a_line, size_t a_quote, const char |
| 1579 | *b_line) |
| 1580 | { |
| 1581 | /* Here is the assumption about a_quote. */ |
| 1582 | assert(a_quote == quote_length(a_line)); |
| 1583 | |
| 1584 | return (a_quote == quote_length(b_line) && |
| 1585 | strncmp(a_line, b_line, a_quote) == 0); |
| 1586 | } |
| 1587 | |
| 1588 | /* We assume a_line and b_line have no quote part. Then, we return |
| 1589 | * whether b_line could follow a_line in a paragraph. */ |
| 1590 | bool indents_match(const char *a_line, size_t a_indent, const char |
| 1591 | *b_line, size_t b_indent) |
| 1592 | { |
| 1593 | assert(a_indent == indent_length(a_line)); |
| 1594 | assert(b_indent == indent_length(b_line)); |
| 1595 | |
| 1596 | return (b_indent <= a_indent && |
| 1597 | strncmp(a_line, b_line, b_indent) == 0); |
| 1598 | } |
| 1599 | |
| 1600 | /* Is foo the beginning of a paragraph? |
| 1601 | * |
| 1602 | * A line of text consists of a "quote part", followed by an |
| 1603 | * "indentation part", followed by text. The functions quote_length() |
| 1604 | * and indent_length() calculate these parts. |
| 1605 | * |
| 1606 | * A line is "part of a paragraph" if it has a part not in the quote |
| 1607 | * part or the indentation. |
| 1608 | * |
| 1609 | * A line is "the beginning of a paragraph" if it is part of a |
| 1610 | * paragraph and |
| 1611 | * 1) it is the top line of the file, or |
| 1612 | * 2) the line above it is not part of a paragraph, or |
| 1613 | * 3) the line above it does not have precisely the same quote |
| 1614 | * part, or |
| 1615 | * 4) the indentation of this line is not an initial substring of |
| 1616 | * the indentation of the previous line, or |
| 1617 | * 5) this line has no quote part and some indentation, and |
| 1618 | * autoindent isn't turned on. |
| 1619 | * The reason for number 5) is that if autoindent isn't turned on, |
| 1620 | * then an indented line is expected to start a paragraph, as in |
| 1621 | * books. Thus, nano can justify an indented paragraph only if |
| 1622 | * autoindent is turned on. */ |
| 1623 | bool begpar(const filestruct *const foo) |
| 1624 | { |
David Lawrence Ramsey | 0083bd2 | 2005-11-09 18:26:44 +0000 | [diff] [blame] | 1625 | size_t quote_len, indent_len, temp_id_len; |
| 1626 | |
| 1627 | if (foo == NULL) |
| 1628 | return FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* Case 1). */ |
David Lawrence Ramsey | 0083bd2 | 2005-11-09 18:26:44 +0000 | [diff] [blame] | 1631 | if (foo == openfile->fileage) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1632 | return TRUE; |
| 1633 | |
| 1634 | quote_len = quote_length(foo->data); |
| 1635 | indent_len = indent_length(foo->data + quote_len); |
| 1636 | |
| 1637 | /* Not part of a paragraph. */ |
| 1638 | if (foo->data[quote_len + indent_len] == '\0') |
| 1639 | return FALSE; |
| 1640 | |
| 1641 | /* Case 3). */ |
| 1642 | if (!quotes_match(foo->data, quote_len, foo->prev->data)) |
| 1643 | return TRUE; |
| 1644 | |
| 1645 | temp_id_len = indent_length(foo->prev->data + quote_len); |
| 1646 | |
| 1647 | /* Case 2) or 5) or 4). */ |
| 1648 | if (foo->prev->data[quote_len + temp_id_len] == '\0' || |
| 1649 | (quote_len == 0 && indent_len > 0 |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1650 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1651 | && !ISSET(AUTOINDENT) |
| 1652 | #endif |
| 1653 | ) || !indents_match(foo->prev->data + quote_len, temp_id_len, |
| 1654 | foo->data + quote_len, indent_len)) |
| 1655 | return TRUE; |
| 1656 | |
| 1657 | return FALSE; |
| 1658 | } |
| 1659 | |
| 1660 | /* Is foo inside a paragraph? */ |
| 1661 | bool inpar(const filestruct *const foo) |
| 1662 | { |
| 1663 | size_t quote_len; |
| 1664 | |
| 1665 | if (foo == NULL) |
| 1666 | return FALSE; |
| 1667 | |
| 1668 | quote_len = quote_length(foo->data); |
| 1669 | |
David Lawrence Ramsey | 2101403 | 2005-11-09 20:33:42 +0000 | [diff] [blame] | 1670 | return (foo->data[quote_len + indent_length(foo->data + |
| 1671 | quote_len)] != '\0'); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
David Lawrence Ramsey | af5a999 | 2005-11-09 23:06:44 +0000 | [diff] [blame] | 1674 | /* Move the next par_len lines, starting with first_line, into the |
David Lawrence Ramsey | 8bd960b | 2005-11-09 18:49:16 +0000 | [diff] [blame] | 1675 | * justify buffer, leaving copies of those lines in place. Assume that |
| 1676 | * par_len is greater than zero, and that there are enough lines after |
David Lawrence Ramsey | cd8f735 | 2005-11-10 21:20:32 +0000 | [diff] [blame] | 1677 | * first_line. */ |
| 1678 | void backup_lines(filestruct *first_line, size_t par_len) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1679 | { |
| 1680 | filestruct *top = first_line; |
| 1681 | /* The top of the paragraph we're backing up. */ |
| 1682 | filestruct *bot = first_line; |
| 1683 | /* The bottom of the paragraph we're backing up. */ |
| 1684 | size_t i; |
| 1685 | /* Generic loop variable. */ |
| 1686 | size_t current_x_save = openfile->current_x; |
| 1687 | ssize_t fl_lineno_save = first_line->lineno; |
| 1688 | ssize_t edittop_lineno_save = openfile->edittop->lineno; |
| 1689 | ssize_t current_lineno_save = openfile->current->lineno; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1690 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1691 | bool old_mark_set = openfile->mark_set; |
| 1692 | ssize_t mb_lineno_save = 0; |
| 1693 | size_t mark_begin_x_save = 0; |
| 1694 | |
| 1695 | if (old_mark_set) { |
| 1696 | mb_lineno_save = openfile->mark_begin->lineno; |
| 1697 | mark_begin_x_save = openfile->mark_begin_x; |
| 1698 | } |
| 1699 | #endif |
| 1700 | |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 1701 | /* par_len will be one greater than the number of lines between |
| 1702 | * current and filebot if filebot is the last line in the |
| 1703 | * paragraph. */ |
David Lawrence Ramsey | 8bd960b | 2005-11-09 18:49:16 +0000 | [diff] [blame] | 1704 | assert(par_len > 0 && openfile->current->lineno + par_len <= |
David Lawrence Ramsey | 24777c0 | 2005-12-01 05:49:08 +0000 | [diff] [blame] | 1705 | openfile->filebot->lineno + 1); |
David Lawrence Ramsey | 8bd960b | 2005-11-09 18:49:16 +0000 | [diff] [blame] | 1706 | |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 1707 | /* Move bot down par_len lines to the line after the last line of |
| 1708 | * the paragraph, if there is one. */ |
| 1709 | for (i = par_len; i > 0 && bot != openfile->filebot; i--) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1710 | bot = bot->next; |
| 1711 | |
| 1712 | /* Move the paragraph from the current buffer's filestruct to the |
| 1713 | * justify buffer. */ |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 1714 | move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot, |
David Lawrence Ramsey | f0575cf | 2005-11-09 23:27:51 +0000 | [diff] [blame] | 1715 | (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1716 | |
| 1717 | /* Copy the paragraph back to the current buffer's filestruct from |
| 1718 | * the justify buffer. */ |
| 1719 | copy_from_filestruct(jusbuffer, jusbottom); |
| 1720 | |
| 1721 | /* Move upward from the last line of the paragraph to the first |
| 1722 | * line, putting first_line, edittop, current, and mark_begin at the |
| 1723 | * same lines in the copied paragraph that they had in the original |
| 1724 | * paragraph. */ |
David Lawrence Ramsey | ee43ea6 | 2007-04-22 15:04:05 +0000 | [diff] [blame] | 1725 | if (openfile->current != openfile->fileage) { |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 1726 | top = openfile->current->prev; |
David Lawrence Ramsey | ee43ea6 | 2007-04-22 15:04:05 +0000 | [diff] [blame] | 1727 | #ifndef NANO_TINY |
| 1728 | if (old_mark_set && |
| 1729 | openfile->current->lineno == mb_lineno_save) { |
| 1730 | openfile->mark_begin = openfile->current; |
| 1731 | openfile->mark_begin_x = mark_begin_x_save; |
| 1732 | } |
| 1733 | #endif |
| 1734 | } else |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 1735 | top = openfile->current; |
David Lawrence Ramsey | e8d505b | 2005-11-10 03:40:45 +0000 | [diff] [blame] | 1736 | for (i = par_len; i > 0 && top != NULL; i--) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1737 | if (top->lineno == fl_lineno_save) |
| 1738 | first_line = top; |
| 1739 | if (top->lineno == edittop_lineno_save) |
| 1740 | openfile->edittop = top; |
| 1741 | if (top->lineno == current_lineno_save) |
| 1742 | openfile->current = top; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1743 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1744 | if (old_mark_set && top->lineno == mb_lineno_save) { |
| 1745 | openfile->mark_begin = top; |
| 1746 | openfile->mark_begin_x = mark_begin_x_save; |
| 1747 | } |
| 1748 | #endif |
| 1749 | top = top->prev; |
| 1750 | } |
| 1751 | |
| 1752 | /* Put current_x at the same place in the copied paragraph that it |
| 1753 | * had in the original paragraph. */ |
| 1754 | openfile->current_x = current_x_save; |
| 1755 | |
| 1756 | set_modified(); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 1759 | /* Find the beginning of the current paragraph if we're in one, or the |
| 1760 | * beginning of the next paragraph if we're not. Afterwards, save the |
| 1761 | * quote length and paragraph length in *quote and *par. Return TRUE if |
David Lawrence Ramsey | 139fa65 | 2006-05-22 01:26:24 +0000 | [diff] [blame] | 1762 | * we found a paragraph, and FALSE if there was an error or we didn't |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 1763 | * find a paragraph. |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1764 | * |
| 1765 | * See the comment at begpar() for more about when a line is the |
| 1766 | * beginning of a paragraph. */ |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 1767 | bool find_paragraph(size_t *const quote, size_t *const par) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1768 | { |
| 1769 | size_t quote_len; |
| 1770 | /* Length of the initial quotation of the paragraph we search |
| 1771 | * for. */ |
| 1772 | size_t par_len; |
| 1773 | /* Number of lines in the paragraph we search for. */ |
| 1774 | filestruct *current_save; |
| 1775 | /* The line at the beginning of the paragraph we search for. */ |
| 1776 | ssize_t current_y_save; |
| 1777 | /* The y-coordinate at the beginning of the paragraph we search |
| 1778 | * for. */ |
| 1779 | |
| 1780 | #ifdef HAVE_REGEX_H |
| 1781 | if (quoterc != 0) { |
| 1782 | statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr); |
| 1783 | return FALSE; |
| 1784 | } |
| 1785 | #endif |
| 1786 | |
| 1787 | assert(openfile->current != NULL); |
| 1788 | |
David Lawrence Ramsey | 1be131a | 2005-11-11 03:55:52 +0000 | [diff] [blame] | 1789 | /* If we're at the end of the last line of the file, it means that |
| 1790 | * there aren't any paragraphs left, so get out. */ |
| 1791 | if (openfile->current == openfile->filebot && openfile->current_x == |
| 1792 | strlen(openfile->filebot->data)) |
| 1793 | return FALSE; |
| 1794 | |
| 1795 | /* If the current line isn't in a paragraph, move forward to the |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 1796 | * last line of the next paragraph, if any. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1797 | if (!inpar(openfile->current)) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1798 | do_para_end(FALSE); |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 1799 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 1800 | /* If we end up past the beginning of the line, it means that |
| 1801 | * we're at the end of the last line of the file, and the line |
| 1802 | * isn't blank, in which case the last line of the file is the |
| 1803 | * last line of the next paragraph. |
| 1804 | * |
| 1805 | * Otherwise, if we end up on a line that's in a paragraph, it |
| 1806 | * means that we're on the line after the last line of the next |
| 1807 | * paragraph, in which case we should move back to the last line |
| 1808 | * of the next paragraph. */ |
| 1809 | if (openfile->current_x == 0) { |
| 1810 | if (!inpar(openfile->current->prev)) |
| 1811 | return FALSE; |
| 1812 | if (openfile->current != openfile->fileage) |
| 1813 | openfile->current = openfile->current->prev; |
| 1814 | } |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1815 | } |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 1816 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 1817 | /* If the current line isn't the first line of the paragraph, move |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 1818 | * back to the first line of the paragraph. */ |
| 1819 | if (!begpar(openfile->current)) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1820 | do_para_begin(FALSE); |
| 1821 | |
| 1822 | /* Now current is the first line of the paragraph. Set quote_len to |
| 1823 | * the quotation length of that line, and set par_len to the number |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 1824 | * of lines in this paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1825 | quote_len = quote_length(openfile->current->data); |
| 1826 | current_save = openfile->current; |
| 1827 | current_y_save = openfile->current_y; |
| 1828 | do_para_end(FALSE); |
| 1829 | par_len = openfile->current->lineno - current_save->lineno; |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 1830 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 1831 | /* If we end up past the beginning of the line, it means that we're |
| 1832 | * at the end of the last line of the file, and the line isn't |
| 1833 | * blank, in which case the last line of the file is part of the |
| 1834 | * paragraph. */ |
David Lawrence Ramsey | bdff665 | 2005-11-11 04:14:33 +0000 | [diff] [blame] | 1835 | if (openfile->current_x > 0) |
| 1836 | par_len++; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1837 | openfile->current = current_save; |
| 1838 | openfile->current_y = current_y_save; |
| 1839 | |
| 1840 | /* Save the values of quote_len and par_len. */ |
| 1841 | assert(quote != NULL && par != NULL); |
| 1842 | |
| 1843 | *quote = quote_len; |
| 1844 | *par = par_len; |
| 1845 | |
| 1846 | return TRUE; |
| 1847 | } |
| 1848 | |
| 1849 | /* If full_justify is TRUE, justify the entire file. Otherwise, justify |
| 1850 | * the current paragraph. */ |
| 1851 | void do_justify(bool full_justify) |
| 1852 | { |
| 1853 | filestruct *first_par_line = NULL; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1854 | /* Will be the first line of the justified paragraph(s), if any. |
| 1855 | * For restoring after unjustify. */ |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 1856 | filestruct *last_par_line = NULL; |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 1857 | /* Will be the line after the last line of the justified |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1858 | * paragraph(s), if any. Also for restoring after unjustify. */ |
David Lawrence Ramsey | 82b5deb | 2005-11-10 06:07:57 +0000 | [diff] [blame] | 1859 | bool filebot_inpar = FALSE; |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 1860 | /* Whether the text at filebot is part of the current |
| 1861 | * paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1862 | |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 1863 | /* We save these variables to be restored if the user |
| 1864 | * unjustifies. */ |
David Lawrence Ramsey | 52161ee | 2005-11-10 19:56:26 +0000 | [diff] [blame] | 1865 | filestruct *edittop_save = openfile->edittop; |
| 1866 | filestruct *current_save = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1867 | size_t current_x_save = openfile->current_x; |
| 1868 | size_t pww_save = openfile->placewewant; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1869 | size_t totsize_save = openfile->totsize; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1870 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1871 | filestruct *mark_begin_save = openfile->mark_begin; |
| 1872 | size_t mark_begin_x_save = openfile->mark_begin_x; |
| 1873 | #endif |
David Lawrence Ramsey | 52161ee | 2005-11-10 19:56:26 +0000 | [diff] [blame] | 1874 | bool modified_save = openfile->modified; |
| 1875 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1876 | int kbinput; |
| 1877 | bool meta_key, func_key, s_or_t, ran_func, finished; |
Chris Allegretta | 0018d8e | 2008-03-13 08:23:52 +0000 | [diff] [blame] | 1878 | const sc *s; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1879 | |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 1880 | /* Move to the beginning of the current line, so that justifying at |
David Lawrence Ramsey | bdff665 | 2005-11-11 04:14:33 +0000 | [diff] [blame] | 1881 | * the end of the last line of the file, if that line isn't blank, |
| 1882 | * will work the first time through. */ |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 1883 | openfile->current_x = 0; |
| 1884 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1885 | /* If we're justifying the entire file, start at the beginning. */ |
| 1886 | if (full_justify) |
| 1887 | openfile->current = openfile->fileage; |
| 1888 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1889 | while (TRUE) { |
| 1890 | size_t i; |
| 1891 | /* Generic loop variable. */ |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1892 | filestruct *curr_first_par_line; |
| 1893 | /* The first line of the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1894 | size_t quote_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1895 | /* Length of the initial quotation of the current |
| 1896 | * paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1897 | size_t indent_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1898 | /* Length of the initial indentation of the current |
| 1899 | * paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1900 | size_t par_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1901 | /* Number of lines in the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1902 | ssize_t break_pos; |
| 1903 | /* Where we will break lines. */ |
| 1904 | char *indent_string; |
| 1905 | /* The first indentation that doesn't match the initial |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1906 | * indentation of the current paragraph. This is put at the |
| 1907 | * beginning of every line broken off the first justified |
| 1908 | * line of the paragraph. Note that this works because a |
| 1909 | * paragraph can only contain two indentations at most: the |
| 1910 | * initial one, and a different one starting on a line after |
| 1911 | * the first. See the comment at begpar() for more about |
| 1912 | * when a line is part of a paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1913 | |
| 1914 | /* Find the first line of the paragraph to be justified. That |
| 1915 | * is the start of this paragraph if we're in one, or the start |
| 1916 | * of the next otherwise. Save the quote length and paragraph |
| 1917 | * length (number of lines). Don't refresh the screen yet, |
| 1918 | * since we'll do that after we justify. |
| 1919 | * |
| 1920 | * If the search failed, we do one of two things. If we're |
David Lawrence Ramsey | 8b203d6 | 2005-11-11 03:17:44 +0000 | [diff] [blame] | 1921 | * justifying the whole file, and we've found at least one |
| 1922 | * paragraph, it means that we should justify all the way to the |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1923 | * last line of the file, so set the last line of the text to be |
| 1924 | * justified to the last line of the file and break out of the |
| 1925 | * loop. Otherwise, it means that there are no paragraph(s) to |
| 1926 | * justify, so refresh the screen and get out. */ |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 1927 | if (!find_paragraph("e_len, &par_len)) { |
David Lawrence Ramsey | 8b203d6 | 2005-11-11 03:17:44 +0000 | [diff] [blame] | 1928 | if (full_justify && first_par_line != NULL) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1929 | last_par_line = openfile->filebot; |
| 1930 | break; |
| 1931 | } else { |
| 1932 | edit_refresh(); |
| 1933 | return; |
| 1934 | } |
| 1935 | } |
| 1936 | |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 1937 | /* par_len will be one greater than the number of lines between |
| 1938 | * current and filebot if filebot is the last line in the |
| 1939 | * paragraph. Set filebot_inpar to TRUE if this is the case. */ |
| 1940 | filebot_inpar = (openfile->current->lineno + par_len == |
| 1941 | openfile->filebot->lineno + 1); |
| 1942 | |
David Lawrence Ramsey | cd8f735 | 2005-11-10 21:20:32 +0000 | [diff] [blame] | 1943 | /* If we haven't already done it, move the original paragraph(s) |
| 1944 | * to the justify buffer, splice a copy of the original |
| 1945 | * paragraph(s) into the file in the same place, and set |
| 1946 | * first_par_line to the first line of the copy. */ |
| 1947 | if (first_par_line == NULL) { |
| 1948 | backup_lines(openfile->current, full_justify ? |
David Lawrence Ramsey | 53f641f | 2005-11-10 21:57:56 +0000 | [diff] [blame] | 1949 | openfile->filebot->lineno - openfile->current->lineno + |
| 1950 | ((openfile->filebot->data[0] != '\0') ? 1 : 0) : |
David Lawrence Ramsey | af5a999 | 2005-11-09 23:06:44 +0000 | [diff] [blame] | 1951 | par_len); |
David Lawrence Ramsey | cd8f735 | 2005-11-10 21:20:32 +0000 | [diff] [blame] | 1952 | first_par_line = openfile->current; |
| 1953 | } |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1954 | |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1955 | /* Set curr_first_par_line to the first line of the current |
| 1956 | * paragraph. */ |
| 1957 | curr_first_par_line = openfile->current; |
| 1958 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1959 | /* Initialize indent_string to a blank string. */ |
| 1960 | indent_string = mallocstrcpy(NULL, ""); |
| 1961 | |
| 1962 | /* Find the first indentation in the paragraph that doesn't |
David Lawrence Ramsey | 8602fd6 | 2006-05-28 18:43:21 +0000 | [diff] [blame] | 1963 | * match the indentation of the first line, and save it in |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1964 | * indent_string. If all the indentations are the same, save |
| 1965 | * the indentation of the first line in indent_string. */ |
| 1966 | { |
| 1967 | const filestruct *indent_line = openfile->current; |
| 1968 | bool past_first_line = FALSE; |
| 1969 | |
| 1970 | for (i = 0; i < par_len; i++) { |
| 1971 | indent_len = quote_len + |
| 1972 | indent_length(indent_line->data + quote_len); |
| 1973 | |
| 1974 | if (indent_len != strlen(indent_string)) { |
| 1975 | indent_string = mallocstrncpy(indent_string, |
| 1976 | indent_line->data, indent_len + 1); |
| 1977 | indent_string[indent_len] = '\0'; |
| 1978 | |
| 1979 | if (past_first_line) |
| 1980 | break; |
| 1981 | } |
| 1982 | |
| 1983 | if (indent_line == openfile->current) |
| 1984 | past_first_line = TRUE; |
| 1985 | |
| 1986 | indent_line = indent_line->next; |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | /* Now tack all the lines of the paragraph together, skipping |
| 1991 | * the quoting and indentation on all lines after the first. */ |
| 1992 | for (i = 0; i < par_len - 1; i++) { |
| 1993 | filestruct *next_line = openfile->current->next; |
| 1994 | size_t line_len = strlen(openfile->current->data); |
| 1995 | size_t next_line_len = |
| 1996 | strlen(openfile->current->next->data); |
| 1997 | |
| 1998 | indent_len = quote_len + |
| 1999 | indent_length(openfile->current->next->data + |
| 2000 | quote_len); |
| 2001 | |
| 2002 | next_line_len -= indent_len; |
| 2003 | openfile->totsize -= indent_len; |
| 2004 | |
| 2005 | /* We're just about to tack the next line onto this one. If |
| 2006 | * this line isn't empty, make sure it ends in a space. */ |
| 2007 | if (line_len > 0 && |
| 2008 | openfile->current->data[line_len - 1] != ' ') { |
| 2009 | line_len++; |
| 2010 | openfile->current->data = |
| 2011 | charealloc(openfile->current->data, |
| 2012 | line_len + 1); |
| 2013 | openfile->current->data[line_len - 1] = ' '; |
| 2014 | openfile->current->data[line_len] = '\0'; |
| 2015 | openfile->totsize++; |
| 2016 | } |
| 2017 | |
| 2018 | openfile->current->data = |
| 2019 | charealloc(openfile->current->data, line_len + |
| 2020 | next_line_len + 1); |
| 2021 | strcat(openfile->current->data, next_line->data + |
| 2022 | indent_len); |
| 2023 | |
David Lawrence Ramsey | 9bedc4b | 2005-11-09 19:51:48 +0000 | [diff] [blame] | 2024 | /* Don't destroy edittop or filebot! */ |
David Lawrence Ramsey | 32bd29e | 2005-11-09 03:44:23 +0000 | [diff] [blame] | 2025 | if (next_line == openfile->edittop) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2026 | openfile->edittop = openfile->current; |
David Lawrence Ramsey | 9bedc4b | 2005-11-09 19:51:48 +0000 | [diff] [blame] | 2027 | if (next_line == openfile->filebot) |
| 2028 | openfile->filebot = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2029 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2030 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2031 | /* Adjust the mark coordinates to compensate for the change |
| 2032 | * in the next line. */ |
| 2033 | if (openfile->mark_set && openfile->mark_begin == |
| 2034 | next_line) { |
| 2035 | openfile->mark_begin = openfile->current; |
| 2036 | openfile->mark_begin_x += line_len - indent_len; |
| 2037 | } |
| 2038 | #endif |
| 2039 | |
| 2040 | unlink_node(next_line); |
| 2041 | delete_node(next_line); |
| 2042 | |
| 2043 | /* If we've removed the next line, we need to go through |
| 2044 | * this line again. */ |
| 2045 | i--; |
| 2046 | |
| 2047 | par_len--; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2048 | openfile->totsize--; |
| 2049 | } |
| 2050 | |
| 2051 | /* Call justify_format() on the paragraph, which will remove |
| 2052 | * excess spaces from it and change all blank characters to |
| 2053 | * spaces. */ |
| 2054 | justify_format(openfile->current, quote_len + |
| 2055 | indent_length(openfile->current->data + quote_len)); |
| 2056 | |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 2057 | while (par_len > 0 && strlenpt(openfile->current->data) > |
| 2058 | fill) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2059 | size_t line_len = strlen(openfile->current->data); |
| 2060 | |
| 2061 | indent_len = strlen(indent_string); |
| 2062 | |
| 2063 | /* If this line is too long, try to wrap it to the next line |
| 2064 | * to make it short enough. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 2065 | break_pos = break_line(openfile->current->data + indent_len, |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 2066 | fill - strnlenpt(openfile->current->data, indent_len) |
| 2067 | #ifndef DISABLE_HELP |
| 2068 | , FALSE |
| 2069 | #endif |
| 2070 | ); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2071 | |
| 2072 | /* We can't break the line, or don't need to, so get out. */ |
| 2073 | if (break_pos == -1 || break_pos + indent_len == line_len) |
| 2074 | break; |
| 2075 | |
| 2076 | /* Move forward to the character after the indentation and |
| 2077 | * just after the space. */ |
| 2078 | break_pos += indent_len + 1; |
| 2079 | |
| 2080 | assert(break_pos <= line_len); |
| 2081 | |
| 2082 | /* Make a new line, and copy the text after where we're |
| 2083 | * going to break this line to the beginning of the new |
| 2084 | * line. */ |
| 2085 | splice_node(openfile->current, |
| 2086 | make_new_node(openfile->current), |
| 2087 | openfile->current->next); |
| 2088 | |
| 2089 | /* If this paragraph is non-quoted, and autoindent isn't |
| 2090 | * turned on, set the indentation length to zero so that the |
| 2091 | * indentation is treated as part of the line. */ |
| 2092 | if (quote_len == 0 |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2093 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2094 | && !ISSET(AUTOINDENT) |
| 2095 | #endif |
| 2096 | ) |
| 2097 | indent_len = 0; |
| 2098 | |
| 2099 | /* Copy the text after where we're going to break the |
| 2100 | * current line to the next line. */ |
| 2101 | openfile->current->next->data = charalloc(indent_len + 1 + |
| 2102 | line_len - break_pos); |
| 2103 | strncpy(openfile->current->next->data, indent_string, |
| 2104 | indent_len); |
| 2105 | strcpy(openfile->current->next->data + indent_len, |
| 2106 | openfile->current->data + break_pos); |
| 2107 | |
| 2108 | par_len++; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2109 | openfile->totsize += indent_len + 1; |
| 2110 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2111 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2112 | /* Adjust the mark coordinates to compensate for the change |
| 2113 | * in the current line. */ |
| 2114 | if (openfile->mark_set && openfile->mark_begin == |
| 2115 | openfile->current && openfile->mark_begin_x > |
| 2116 | break_pos) { |
| 2117 | openfile->mark_begin = openfile->current->next; |
| 2118 | openfile->mark_begin_x -= break_pos - indent_len; |
| 2119 | } |
| 2120 | #endif |
| 2121 | |
| 2122 | /* Break the current line. */ |
| 2123 | null_at(&openfile->current->data, break_pos); |
| 2124 | |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 2125 | /* If the current line is the last line of the file, move |
David Lawrence Ramsey | b885c9c | 2005-11-10 05:20:25 +0000 | [diff] [blame] | 2126 | * the last line of the file down to the next line. */ |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 2127 | if (openfile->filebot == openfile->current) |
| 2128 | openfile->filebot = openfile->filebot->next; |
| 2129 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2130 | /* Go to the next line. */ |
| 2131 | par_len--; |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 2132 | openfile->current_y++; |
| 2133 | openfile->current = openfile->current->next; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | /* We're done breaking lines, so we don't need indent_string |
| 2137 | * anymore. */ |
| 2138 | free(indent_string); |
| 2139 | |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 2140 | /* Go to the next line, if possible. If there is no next line, |
| 2141 | * move to the end of the current line. */ |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 2142 | if (openfile->current != openfile->filebot) { |
| 2143 | openfile->current_y++; |
| 2144 | openfile->current = openfile->current->next; |
| 2145 | } else |
| 2146 | openfile->current_x = strlen(openfile->current->data); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2147 | |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2148 | /* Renumber the lines of the now-justified current paragraph, |
| 2149 | * since both find_paragraph() and edit_refresh() need the line |
| 2150 | * numbers to be right. */ |
| 2151 | renumber(curr_first_par_line); |
David Lawrence Ramsey | ad1b64c | 2005-11-29 19:00:09 +0000 | [diff] [blame] | 2152 | |
| 2153 | /* We've just finished justifying the paragraph. If we're not |
| 2154 | * justifying the entire file, break out of the loop. |
| 2155 | * Otherwise, continue the loop so that we justify all the |
| 2156 | * paragraphs in the file. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2157 | if (!full_justify) |
| 2158 | break; |
| 2159 | } |
| 2160 | |
| 2161 | /* We are now done justifying the paragraph or the file, so clean |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 2162 | * up. current_y and totsize have been maintained above. If we |
David Lawrence Ramsey | ad1b64c | 2005-11-29 19:00:09 +0000 | [diff] [blame] | 2163 | * actually justified something, set last_par_line to the new end of |
| 2164 | * the paragraph. */ |
| 2165 | if (first_par_line != NULL) |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 2166 | last_par_line = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2167 | |
| 2168 | edit_refresh(); |
| 2169 | |
David Lawrence Ramsey | fc0f8f8 | 2006-05-10 13:41:53 +0000 | [diff] [blame] | 2170 | #ifndef NANO_TINY |
David Lawrence Ramsey | 1c5af64 | 2006-05-10 15:15:06 +0000 | [diff] [blame] | 2171 | /* We're going to set jump_buf so that we return here after a |
| 2172 | * SIGWINCH instead of to main(). Indicate this. */ |
| 2173 | jump_buf_main = FALSE; |
| 2174 | |
David Lawrence Ramsey | fc0f8f8 | 2006-05-10 13:41:53 +0000 | [diff] [blame] | 2175 | /* Return here after a SIGWINCH. */ |
David Lawrence Ramsey | 1c5af64 | 2006-05-10 15:15:06 +0000 | [diff] [blame] | 2176 | sigsetjmp(jump_buf, 1); |
David Lawrence Ramsey | fc0f8f8 | 2006-05-10 13:41:53 +0000 | [diff] [blame] | 2177 | #endif |
| 2178 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2179 | statusbar(_("Can now UnJustify!")); |
| 2180 | |
| 2181 | /* If constant cursor position display is on, make sure the current |
| 2182 | * cursor position will be properly displayed on the statusbar. */ |
| 2183 | if (ISSET(CONST_UPDATE)) |
| 2184 | do_cursorpos(TRUE); |
| 2185 | |
| 2186 | /* Display the shortcut list with UnJustify. */ |
| 2187 | shortcut_init(TRUE); |
| 2188 | display_main_list(); |
| 2189 | |
| 2190 | /* Now get a keystroke and see if it's unjustify. If not, put back |
| 2191 | * the keystroke and return. */ |
| 2192 | kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func, |
| 2193 | &finished, FALSE); |
Chris Allegretta | 0018d8e | 2008-03-13 08:23:52 +0000 | [diff] [blame] | 2194 | s = get_shortcut(currmenu, &kbinput, &meta_key, &func_key); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2195 | |
Chris Allegretta | 0018d8e | 2008-03-13 08:23:52 +0000 | [diff] [blame] | 2196 | if (s && s->scfunc == do_uncut_text) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2197 | /* Splice the justify buffer back into the file, but only if we |
| 2198 | * actually justified something. */ |
| 2199 | if (first_par_line != NULL) { |
| 2200 | filestruct *top_save; |
| 2201 | |
| 2202 | /* Partition the filestruct so that it contains only the |
| 2203 | * text of the justified paragraph. */ |
| 2204 | filepart = partition_filestruct(first_par_line, 0, |
David Lawrence Ramsey | ccd1b7b | 2005-11-18 20:21:48 +0000 | [diff] [blame] | 2205 | last_par_line, filebot_inpar ? |
| 2206 | strlen(last_par_line->data) : 0); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2207 | |
| 2208 | /* Remove the text of the justified paragraph, and |
David Lawrence Ramsey | af5a999 | 2005-11-09 23:06:44 +0000 | [diff] [blame] | 2209 | * replace it with the text in the justify buffer. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2210 | free_filestruct(openfile->fileage); |
| 2211 | openfile->fileage = jusbuffer; |
| 2212 | openfile->filebot = jusbottom; |
| 2213 | |
| 2214 | top_save = openfile->fileage; |
| 2215 | |
| 2216 | /* Unpartition the filestruct so that it contains all the |
| 2217 | * text again. Note that the justified paragraph has been |
| 2218 | * replaced with the unjustified paragraph. */ |
| 2219 | unpartition_filestruct(&filepart); |
| 2220 | |
| 2221 | /* Renumber starting with the beginning line of the old |
| 2222 | * partition. */ |
| 2223 | renumber(top_save); |
| 2224 | |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 2225 | /* Restore the justify we just did (ungrateful user!). */ |
| 2226 | openfile->edittop = edittop_save; |
| 2227 | openfile->current = current_save; |
| 2228 | openfile->current_x = current_x_save; |
| 2229 | openfile->placewewant = pww_save; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2230 | openfile->totsize = totsize_save; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2231 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2232 | if (openfile->mark_set) { |
| 2233 | openfile->mark_begin = mark_begin_save; |
| 2234 | openfile->mark_begin_x = mark_begin_x_save; |
| 2235 | } |
| 2236 | #endif |
| 2237 | openfile->modified = modified_save; |
| 2238 | |
| 2239 | /* Clear the justify buffer. */ |
| 2240 | jusbuffer = NULL; |
| 2241 | |
| 2242 | if (!openfile->modified) |
| 2243 | titlebar(NULL); |
| 2244 | edit_refresh(); |
| 2245 | } |
| 2246 | } else { |
| 2247 | unget_kbinput(kbinput, meta_key, func_key); |
| 2248 | |
| 2249 | /* Blow away the text in the justify buffer. */ |
| 2250 | free_filestruct(jusbuffer); |
| 2251 | jusbuffer = NULL; |
| 2252 | } |
| 2253 | |
| 2254 | blank_statusbar(); |
| 2255 | |
| 2256 | /* Display the shortcut list with UnCut. */ |
| 2257 | shortcut_init(FALSE); |
| 2258 | display_main_list(); |
| 2259 | } |
| 2260 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2261 | /* Justify the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2262 | void do_justify_void(void) |
| 2263 | { |
| 2264 | do_justify(FALSE); |
| 2265 | } |
| 2266 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2267 | /* Justify the entire file. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2268 | void do_full_justify(void) |
| 2269 | { |
| 2270 | do_justify(TRUE); |
| 2271 | } |
| 2272 | #endif /* !DISABLE_JUSTIFY */ |
| 2273 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2274 | #ifndef DISABLE_SPELLER |
| 2275 | /* A word is misspelled in the file. Let the user replace it. We |
| 2276 | * return FALSE if the user cancels. */ |
| 2277 | bool do_int_spell_fix(const char *word) |
| 2278 | { |
| 2279 | char *save_search, *save_replace; |
| 2280 | size_t match_len, current_x_save = openfile->current_x; |
| 2281 | size_t pww_save = openfile->placewewant; |
Chris Allegretta | 10f868d | 2008-03-14 04:08:51 +0000 | [diff] [blame] | 2282 | bool meta_key = FALSE, func_key = FALSE; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2283 | filestruct *edittop_save = openfile->edittop; |
| 2284 | filestruct *current_save = openfile->current; |
| 2285 | /* Save where we are. */ |
| 2286 | bool canceled = FALSE; |
| 2287 | /* The return value. */ |
| 2288 | bool case_sens_set = ISSET(CASE_SENSITIVE); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2289 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2290 | bool backwards_search_set = ISSET(BACKWARDS_SEARCH); |
| 2291 | #endif |
| 2292 | #ifdef HAVE_REGEX_H |
| 2293 | bool regexp_set = ISSET(USE_REGEXP); |
| 2294 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2295 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2296 | bool old_mark_set = openfile->mark_set; |
| 2297 | bool added_magicline = FALSE; |
| 2298 | /* Whether we added a magicline after filebot. */ |
| 2299 | bool right_side_up = FALSE; |
| 2300 | /* TRUE if (mark_begin, mark_begin_x) is the top of the mark, |
| 2301 | * FALSE if (current, current_x) is. */ |
| 2302 | filestruct *top, *bot; |
| 2303 | size_t top_x, bot_x; |
| 2304 | #endif |
| 2305 | |
| 2306 | /* Make sure spell-check is case sensitive. */ |
| 2307 | SET(CASE_SENSITIVE); |
| 2308 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2309 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2310 | /* Make sure spell-check goes forward only. */ |
| 2311 | UNSET(BACKWARDS_SEARCH); |
| 2312 | #endif |
| 2313 | #ifdef HAVE_REGEX_H |
| 2314 | /* Make sure spell-check doesn't use regular expressions. */ |
| 2315 | UNSET(USE_REGEXP); |
| 2316 | #endif |
| 2317 | |
| 2318 | /* Save the current search/replace strings. */ |
| 2319 | search_init_globals(); |
| 2320 | save_search = last_search; |
| 2321 | save_replace = last_replace; |
| 2322 | |
| 2323 | /* Set the search/replace strings to the misspelled word. */ |
| 2324 | last_search = mallocstrcpy(NULL, word); |
| 2325 | last_replace = mallocstrcpy(NULL, word); |
| 2326 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2327 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2328 | if (old_mark_set) { |
| 2329 | /* If the mark is on, partition the filestruct so that it |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2330 | * contains only the marked text; if the NO_NEWLINES flag isn't |
| 2331 | * set, keep track of whether the text will have a magicline |
| 2332 | * added when we're done correcting misspelled words; and |
| 2333 | * turn the mark off. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2334 | mark_order((const filestruct **)&top, &top_x, |
| 2335 | (const filestruct **)&bot, &bot_x, &right_side_up); |
| 2336 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2337 | if (!ISSET(NO_NEWLINES)) |
| 2338 | added_magicline = (openfile->filebot->data[0] != '\0'); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2339 | openfile->mark_set = FALSE; |
| 2340 | } |
| 2341 | #endif |
| 2342 | |
| 2343 | /* Start from the top of the file. */ |
| 2344 | openfile->edittop = openfile->fileage; |
| 2345 | openfile->current = openfile->fileage; |
| 2346 | openfile->current_x = (size_t)-1; |
| 2347 | openfile->placewewant = 0; |
| 2348 | |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 2349 | /* Find the first whole occurrence of word. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2350 | findnextstr_wrap_reset(); |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 2351 | while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word, |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2352 | &match_len)) { |
| 2353 | if (is_whole_word(openfile->current_x, openfile->current->data, |
| 2354 | word)) { |
| 2355 | size_t xpt = xplustabs(); |
| 2356 | char *exp_word = display_string(openfile->current->data, |
| 2357 | xpt, strnlenpt(openfile->current->data, |
| 2358 | openfile->current_x + match_len) - xpt, FALSE); |
| 2359 | |
| 2360 | edit_refresh(); |
| 2361 | |
| 2362 | do_replace_highlight(TRUE, exp_word); |
| 2363 | |
| 2364 | /* Allow all instances of the word to be corrected. */ |
David Lawrence Ramsey | 9d8c284 | 2006-02-07 21:11:05 +0000 | [diff] [blame] | 2365 | canceled = (do_prompt(FALSE, |
| 2366 | #ifndef DISABLE_TABCOMP |
| 2367 | TRUE, |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2368 | #endif |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 2369 | MSPELL, word, |
Chris Allegretta | 10f868d | 2008-03-14 04:08:51 +0000 | [diff] [blame] | 2370 | &meta_key, &func_key, |
David Lawrence Ramsey | 9d8c284 | 2006-02-07 21:11:05 +0000 | [diff] [blame] | 2371 | #ifndef NANO_TINY |
| 2372 | NULL, |
| 2373 | #endif |
David Lawrence Ramsey | 6816007 | 2006-02-18 21:32:29 +0000 | [diff] [blame] | 2374 | edit_refresh, _("Edit a replacement")) == -1); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2375 | |
| 2376 | do_replace_highlight(FALSE, exp_word); |
| 2377 | |
| 2378 | free(exp_word); |
| 2379 | |
| 2380 | if (!canceled && strcmp(word, answer) != 0) { |
| 2381 | openfile->current_x--; |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 2382 | do_replace_loop(TRUE, &canceled, openfile->current, |
| 2383 | &openfile->current_x, word); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | break; |
| 2387 | } |
| 2388 | } |
| 2389 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2390 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2391 | if (old_mark_set) { |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2392 | /* If the mark was on, the NO_NEWLINES flag isn't set, and we |
| 2393 | * added a magicline, remove it now. */ |
| 2394 | if (!ISSET(NO_NEWLINES) && added_magicline) |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2395 | remove_magicline(); |
| 2396 | |
| 2397 | /* Put the beginning and the end of the mark at the beginning |
| 2398 | * and the end of the spell-checked text. */ |
| 2399 | if (openfile->fileage == openfile->filebot) |
| 2400 | bot_x += top_x; |
| 2401 | if (right_side_up) { |
| 2402 | openfile->mark_begin_x = top_x; |
| 2403 | current_x_save = bot_x; |
| 2404 | } else { |
| 2405 | current_x_save = top_x; |
| 2406 | openfile->mark_begin_x = bot_x; |
| 2407 | } |
| 2408 | |
| 2409 | /* Unpartition the filestruct so that it contains all the text |
| 2410 | * again, and turn the mark back on. */ |
| 2411 | unpartition_filestruct(&filepart); |
| 2412 | openfile->mark_set = TRUE; |
| 2413 | } |
| 2414 | #endif |
| 2415 | |
| 2416 | /* Restore the search/replace strings. */ |
| 2417 | free(last_search); |
| 2418 | last_search = save_search; |
| 2419 | free(last_replace); |
| 2420 | last_replace = save_replace; |
| 2421 | |
| 2422 | /* Restore where we were. */ |
| 2423 | openfile->edittop = edittop_save; |
| 2424 | openfile->current = current_save; |
| 2425 | openfile->current_x = current_x_save; |
| 2426 | openfile->placewewant = pww_save; |
| 2427 | |
| 2428 | /* Restore case sensitivity setting. */ |
| 2429 | if (!case_sens_set) |
| 2430 | UNSET(CASE_SENSITIVE); |
| 2431 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2432 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2433 | /* Restore search/replace direction. */ |
| 2434 | if (backwards_search_set) |
| 2435 | SET(BACKWARDS_SEARCH); |
| 2436 | #endif |
| 2437 | #ifdef HAVE_REGEX_H |
| 2438 | /* Restore regular expression usage setting. */ |
| 2439 | if (regexp_set) |
| 2440 | SET(USE_REGEXP); |
| 2441 | #endif |
| 2442 | |
| 2443 | return !canceled; |
| 2444 | } |
| 2445 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2446 | /* Internal (integrated) spell checking using the spell program, |
| 2447 | * filtered through the sort and uniq programs. Return NULL for normal |
| 2448 | * termination, and the error string otherwise. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2449 | const char *do_int_speller(const char *tempfile_name) |
| 2450 | { |
| 2451 | char *read_buff, *read_buff_ptr, *read_buff_word; |
| 2452 | size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread; |
| 2453 | int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1; |
| 2454 | pid_t pid_spell, pid_sort, pid_uniq; |
| 2455 | int spell_status, sort_status, uniq_status; |
| 2456 | |
| 2457 | /* Create all three pipes up front. */ |
| 2458 | if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || |
| 2459 | pipe(uniq_fd) == -1) |
| 2460 | return _("Could not create pipe"); |
| 2461 | |
| 2462 | statusbar(_("Creating misspelled word list, please wait...")); |
| 2463 | |
| 2464 | /* A new process to run spell in. */ |
| 2465 | if ((pid_spell = fork()) == 0) { |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 2466 | /* Child continues (i.e. future spell process). */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2467 | close(spell_fd[0]); |
| 2468 | |
| 2469 | /* Replace the standard input with the temp file. */ |
| 2470 | if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1) |
| 2471 | goto close_pipes_and_exit; |
| 2472 | |
| 2473 | if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) |
| 2474 | goto close_pipes_and_exit; |
| 2475 | |
| 2476 | close(tempfile_fd); |
| 2477 | |
| 2478 | /* Send spell's standard output to the pipe. */ |
| 2479 | if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 2480 | goto close_pipes_and_exit; |
| 2481 | |
| 2482 | close(spell_fd[1]); |
| 2483 | |
David Lawrence Ramsey | 3239ff2 | 2005-11-29 20:01:06 +0000 | [diff] [blame] | 2484 | /* Start the spell program; we are using $PATH. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2485 | execlp("spell", "spell", NULL); |
| 2486 | |
| 2487 | /* This should not be reached if spell is found. */ |
| 2488 | exit(1); |
| 2489 | } |
| 2490 | |
| 2491 | /* Parent continues here. */ |
| 2492 | close(spell_fd[1]); |
| 2493 | |
| 2494 | /* A new process to run sort in. */ |
| 2495 | if ((pid_sort = fork()) == 0) { |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 2496 | /* Child continues (i.e. future spell process). Replace the |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2497 | * standard input with the standard output of the old pipe. */ |
| 2498 | if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 2499 | goto close_pipes_and_exit; |
| 2500 | |
| 2501 | close(spell_fd[0]); |
| 2502 | |
| 2503 | /* Send sort's standard output to the new pipe. */ |
| 2504 | if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 2505 | goto close_pipes_and_exit; |
| 2506 | |
| 2507 | close(sort_fd[1]); |
| 2508 | |
| 2509 | /* Start the sort program. Use -f to remove mixed case. If |
| 2510 | * this isn't portable, let me know. */ |
| 2511 | execlp("sort", "sort", "-f", NULL); |
| 2512 | |
| 2513 | /* This should not be reached if sort is found. */ |
| 2514 | exit(1); |
| 2515 | } |
| 2516 | |
| 2517 | close(spell_fd[0]); |
| 2518 | close(sort_fd[1]); |
| 2519 | |
| 2520 | /* A new process to run uniq in. */ |
| 2521 | if ((pid_uniq = fork()) == 0) { |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 2522 | /* Child continues (i.e. future uniq process). Replace the |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2523 | * standard input with the standard output of the old pipe. */ |
| 2524 | if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 2525 | goto close_pipes_and_exit; |
| 2526 | |
| 2527 | close(sort_fd[0]); |
| 2528 | |
| 2529 | /* Send uniq's standard output to the new pipe. */ |
| 2530 | if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 2531 | goto close_pipes_and_exit; |
| 2532 | |
| 2533 | close(uniq_fd[1]); |
| 2534 | |
| 2535 | /* Start the uniq program; we are using PATH. */ |
| 2536 | execlp("uniq", "uniq", NULL); |
| 2537 | |
| 2538 | /* This should not be reached if uniq is found. */ |
| 2539 | exit(1); |
| 2540 | } |
| 2541 | |
| 2542 | close(sort_fd[0]); |
| 2543 | close(uniq_fd[1]); |
| 2544 | |
| 2545 | /* The child process was not forked successfully. */ |
| 2546 | if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) { |
| 2547 | close(uniq_fd[0]); |
| 2548 | return _("Could not fork"); |
| 2549 | } |
| 2550 | |
| 2551 | /* Get the system pipe buffer size. */ |
| 2552 | if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) { |
| 2553 | close(uniq_fd[0]); |
| 2554 | return _("Could not get size of pipe buffer"); |
| 2555 | } |
| 2556 | |
| 2557 | /* Read in the returned spelling errors. */ |
| 2558 | read_buff_read = 0; |
| 2559 | read_buff_size = pipe_buff_size + 1; |
| 2560 | read_buff = read_buff_ptr = charalloc(read_buff_size); |
| 2561 | |
| 2562 | while ((bytesread = read(uniq_fd[0], read_buff_ptr, |
| 2563 | pipe_buff_size)) > 0) { |
| 2564 | read_buff_read += bytesread; |
| 2565 | read_buff_size += pipe_buff_size; |
| 2566 | read_buff = read_buff_ptr = charealloc(read_buff, |
| 2567 | read_buff_size); |
| 2568 | read_buff_ptr += read_buff_read; |
| 2569 | } |
| 2570 | |
| 2571 | *read_buff_ptr = '\0'; |
| 2572 | close(uniq_fd[0]); |
| 2573 | |
| 2574 | /* Process the spelling errors. */ |
| 2575 | read_buff_word = read_buff_ptr = read_buff; |
| 2576 | |
| 2577 | while (*read_buff_ptr != '\0') { |
| 2578 | if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) { |
| 2579 | *read_buff_ptr = '\0'; |
| 2580 | if (read_buff_word != read_buff_ptr) { |
| 2581 | if (!do_int_spell_fix(read_buff_word)) { |
| 2582 | read_buff_word = read_buff_ptr; |
| 2583 | break; |
| 2584 | } |
| 2585 | } |
| 2586 | read_buff_word = read_buff_ptr + 1; |
| 2587 | } |
| 2588 | read_buff_ptr++; |
| 2589 | } |
| 2590 | |
| 2591 | /* Special case: the last word doesn't end with '\r' or '\n'. */ |
| 2592 | if (read_buff_word != read_buff_ptr) |
| 2593 | do_int_spell_fix(read_buff_word); |
| 2594 | |
| 2595 | free(read_buff); |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2596 | search_replace_abort(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2597 | edit_refresh(); |
| 2598 | |
| 2599 | /* Process the end of the spell process. */ |
| 2600 | waitpid(pid_spell, &spell_status, 0); |
| 2601 | waitpid(pid_sort, &sort_status, 0); |
| 2602 | waitpid(pid_uniq, &uniq_status, 0); |
| 2603 | |
| 2604 | if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status)) |
| 2605 | return _("Error invoking \"spell\""); |
| 2606 | |
| 2607 | if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status)) |
| 2608 | return _("Error invoking \"sort -f\""); |
| 2609 | |
| 2610 | if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status)) |
| 2611 | return _("Error invoking \"uniq\""); |
| 2612 | |
| 2613 | /* Otherwise... */ |
| 2614 | return NULL; |
| 2615 | |
| 2616 | close_pipes_and_exit: |
| 2617 | /* Don't leak any handles. */ |
| 2618 | close(tempfile_fd); |
| 2619 | close(spell_fd[0]); |
| 2620 | close(spell_fd[1]); |
| 2621 | close(sort_fd[0]); |
| 2622 | close(sort_fd[1]); |
| 2623 | close(uniq_fd[0]); |
| 2624 | close(uniq_fd[1]); |
| 2625 | exit(1); |
| 2626 | } |
| 2627 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2628 | /* External (alternate) spell checking. Return NULL for normal |
| 2629 | * termination, and the error string otherwise. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2630 | const char *do_alt_speller(char *tempfile_name) |
| 2631 | { |
| 2632 | int alt_spell_status; |
| 2633 | size_t current_x_save = openfile->current_x; |
| 2634 | size_t pww_save = openfile->placewewant; |
| 2635 | ssize_t current_y_save = openfile->current_y; |
| 2636 | ssize_t lineno_save = openfile->current->lineno; |
| 2637 | pid_t pid_spell; |
| 2638 | char *ptr; |
| 2639 | static int arglen = 3; |
| 2640 | static char **spellargs = NULL; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2641 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2642 | bool old_mark_set = openfile->mark_set; |
| 2643 | bool added_magicline = FALSE; |
| 2644 | /* Whether we added a magicline after filebot. */ |
| 2645 | bool right_side_up = FALSE; |
| 2646 | /* TRUE if (mark_begin, mark_begin_x) is the top of the mark, |
| 2647 | * FALSE if (current, current_x) is. */ |
| 2648 | filestruct *top, *bot; |
| 2649 | size_t top_x, bot_x; |
| 2650 | ssize_t mb_lineno_save = 0; |
| 2651 | /* We're going to close the current file, and open the output of |
| 2652 | * the alternate spell command. The line that mark_begin points |
| 2653 | * to will be freed, so we save the line number and restore it |
| 2654 | * afterwards. */ |
| 2655 | size_t totsize_save = openfile->totsize; |
| 2656 | /* Our saved value of totsize, used when we spell-check a marked |
| 2657 | * selection. */ |
| 2658 | |
| 2659 | if (old_mark_set) { |
| 2660 | /* If the mark is on, save the number of the line it starts on, |
| 2661 | * and then turn the mark off. */ |
| 2662 | mb_lineno_save = openfile->mark_begin->lineno; |
| 2663 | openfile->mark_set = FALSE; |
| 2664 | } |
| 2665 | #endif |
| 2666 | |
| 2667 | endwin(); |
| 2668 | |
| 2669 | /* Set up an argument list to pass execvp(). */ |
| 2670 | if (spellargs == NULL) { |
| 2671 | spellargs = (char **)nmalloc(arglen * sizeof(char *)); |
| 2672 | |
| 2673 | spellargs[0] = strtok(alt_speller, " "); |
| 2674 | while ((ptr = strtok(NULL, " ")) != NULL) { |
| 2675 | arglen++; |
| 2676 | spellargs = (char **)nrealloc(spellargs, arglen * |
| 2677 | sizeof(char *)); |
| 2678 | spellargs[arglen - 3] = ptr; |
| 2679 | } |
| 2680 | spellargs[arglen - 1] = NULL; |
| 2681 | } |
| 2682 | spellargs[arglen - 2] = tempfile_name; |
| 2683 | |
| 2684 | /* Start a new process for the alternate speller. */ |
| 2685 | if ((pid_spell = fork()) == 0) { |
David Lawrence Ramsey | 2e2112c | 2005-11-29 05:39:31 +0000 | [diff] [blame] | 2686 | /* Start alternate spell program; we are using $PATH. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2687 | execvp(spellargs[0], spellargs); |
| 2688 | |
| 2689 | /* Should not be reached, if alternate speller is found!!! */ |
| 2690 | exit(1); |
| 2691 | } |
| 2692 | |
| 2693 | /* If we couldn't fork, get out. */ |
| 2694 | if (pid_spell < 0) |
| 2695 | return _("Could not fork"); |
| 2696 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2697 | #ifndef NANO_TINY |
David Lawrence Ramsey | b18482e | 2005-07-26 00:06:34 +0000 | [diff] [blame] | 2698 | /* Don't handle a pending SIGWINCH until the alternate spell checker |
David Lawrence Ramsey | 3fe08ac | 2005-07-26 01:17:16 +0000 | [diff] [blame] | 2699 | * is finished and we've loaded the spell-checked file back in. */ |
David Lawrence Ramsey | b18482e | 2005-07-26 00:06:34 +0000 | [diff] [blame] | 2700 | allow_pending_sigwinch(FALSE); |
| 2701 | #endif |
| 2702 | |
| 2703 | /* Wait for the alternate spell checker to finish. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2704 | wait(&alt_spell_status); |
| 2705 | |
David Lawrence Ramsey | 84fdb90 | 2005-08-14 20:08:49 +0000 | [diff] [blame] | 2706 | /* Reenter curses mode. */ |
| 2707 | doupdate(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2708 | |
| 2709 | /* Restore the terminal to its previous state. */ |
| 2710 | terminal_init(); |
| 2711 | |
| 2712 | /* Turn the cursor back on for sure. */ |
| 2713 | curs_set(1); |
| 2714 | |
David Lawrence Ramsey | 3fe08ac | 2005-07-26 01:17:16 +0000 | [diff] [blame] | 2715 | /* The screen might have been resized. If it has, reinitialize all |
| 2716 | * the windows based on the new screen dimensions. */ |
| 2717 | window_init(); |
| 2718 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2719 | if (!WIFEXITED(alt_spell_status) || |
| 2720 | WEXITSTATUS(alt_spell_status) != 0) { |
David Lawrence Ramsey | 9e7b2d5 | 2007-01-11 22:46:22 +0000 | [diff] [blame] | 2721 | char *alt_spell_error; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2722 | char *invoke_error = _("Error invoking \"%s\""); |
| 2723 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2724 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2725 | /* Turn the mark back on if it was on before. */ |
| 2726 | openfile->mark_set = old_mark_set; |
| 2727 | #endif |
| 2728 | |
David Lawrence Ramsey | 9e7b2d5 | 2007-01-11 22:46:22 +0000 | [diff] [blame] | 2729 | alt_spell_error = |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2730 | charalloc(strlen(invoke_error) + |
| 2731 | strlen(alt_speller) + 1); |
David Lawrence Ramsey | 9e7b2d5 | 2007-01-11 22:46:22 +0000 | [diff] [blame] | 2732 | sprintf(alt_spell_error, invoke_error, alt_speller); |
| 2733 | return alt_spell_error; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2736 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2737 | if (old_mark_set) { |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2738 | /* If the mark is on, partition the filestruct so that it |
| 2739 | * contains only the marked text; if the NO_NEWLINES flag isn't |
| 2740 | * set, keep track of whether the text will have a magicline |
| 2741 | * added when we're done correcting misspelled words; and |
| 2742 | * turn the mark off. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2743 | mark_order((const filestruct **)&top, &top_x, |
| 2744 | (const filestruct **)&bot, &bot_x, &right_side_up); |
| 2745 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2746 | if (!ISSET(NO_NEWLINES)) |
| 2747 | added_magicline = (openfile->filebot->data[0] != '\0'); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2748 | |
| 2749 | /* Get the number of characters in the marked text, and subtract |
| 2750 | * it from the saved value of totsize. */ |
| 2751 | totsize_save -= get_totsize(top, bot); |
| 2752 | } |
| 2753 | #endif |
| 2754 | |
David Lawrence Ramsey | 9c984e8 | 2005-11-08 19:15:58 +0000 | [diff] [blame] | 2755 | /* Replace the text of the current buffer with the spell-checked |
| 2756 | * text. */ |
| 2757 | replace_buffer(tempfile_name); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2758 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2759 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2760 | if (old_mark_set) { |
| 2761 | filestruct *top_save = openfile->fileage; |
| 2762 | |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2763 | /* If the mark was on, the NO_NEWLINES flag isn't set, and we |
| 2764 | * added a magicline, remove it now. */ |
| 2765 | if (!ISSET(NO_NEWLINES) && added_magicline) |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2766 | remove_magicline(); |
| 2767 | |
| 2768 | /* Put the beginning and the end of the mark at the beginning |
| 2769 | * and the end of the spell-checked text. */ |
| 2770 | if (openfile->fileage == openfile->filebot) |
| 2771 | bot_x += top_x; |
| 2772 | if (right_side_up) { |
| 2773 | openfile->mark_begin_x = top_x; |
| 2774 | current_x_save = bot_x; |
| 2775 | } else { |
| 2776 | current_x_save = top_x; |
| 2777 | openfile->mark_begin_x = bot_x; |
| 2778 | } |
| 2779 | |
| 2780 | /* Unpartition the filestruct so that it contains all the text |
| 2781 | * again. Note that we've replaced the marked text originally |
| 2782 | * in the partition with the spell-checked marked text in the |
| 2783 | * temp file. */ |
| 2784 | unpartition_filestruct(&filepart); |
| 2785 | |
| 2786 | /* Renumber starting with the beginning line of the old |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 2787 | * partition. Also add the number of characters in the |
| 2788 | * spell-checked marked text to the saved value of totsize, and |
| 2789 | * then make that saved value the actual value. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2790 | renumber(top_save); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2791 | totsize_save += openfile->totsize; |
| 2792 | openfile->totsize = totsize_save; |
| 2793 | |
| 2794 | /* Assign mark_begin to the line where the mark began before. */ |
| 2795 | do_gotopos(mb_lineno_save, openfile->mark_begin_x, |
| 2796 | current_y_save, 0); |
| 2797 | openfile->mark_begin = openfile->current; |
| 2798 | |
| 2799 | /* Assign mark_begin_x to the location in mark_begin where the |
| 2800 | * mark began before, adjusted for any shortening of the |
| 2801 | * line. */ |
| 2802 | openfile->mark_begin_x = openfile->current_x; |
| 2803 | |
| 2804 | /* Turn the mark back on. */ |
| 2805 | openfile->mark_set = TRUE; |
| 2806 | } |
| 2807 | #endif |
| 2808 | |
| 2809 | /* Go back to the old position, and mark the file as modified. */ |
| 2810 | do_gotopos(lineno_save, current_x_save, current_y_save, pww_save); |
| 2811 | set_modified(); |
| 2812 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2813 | #ifndef NANO_TINY |
David Lawrence Ramsey | 3fe08ac | 2005-07-26 01:17:16 +0000 | [diff] [blame] | 2814 | /* Handle a pending SIGWINCH again. */ |
| 2815 | allow_pending_sigwinch(TRUE); |
| 2816 | #endif |
| 2817 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2818 | return NULL; |
| 2819 | } |
| 2820 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2821 | /* Spell check the current file. If an alternate spell checker is |
| 2822 | * specified, use it. Otherwise, use the internal spell checker. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2823 | void do_spell(void) |
| 2824 | { |
David Lawrence Ramsey | 9e7b2d5 | 2007-01-11 22:46:22 +0000 | [diff] [blame] | 2825 | bool status; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2826 | FILE *temp_file; |
| 2827 | char *temp = safe_tempfile(&temp_file); |
| 2828 | const char *spell_msg; |
| 2829 | |
| 2830 | if (temp == NULL) { |
David Lawrence Ramsey | f0e3ca6 | 2006-05-03 13:11:00 +0000 | [diff] [blame] | 2831 | statusbar(_("Error writing temp file: %s"), strerror(errno)); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2832 | return; |
| 2833 | } |
| 2834 | |
David Lawrence Ramsey | 9e7b2d5 | 2007-01-11 22:46:22 +0000 | [diff] [blame] | 2835 | status = |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2836 | #ifndef NANO_TINY |
David Lawrence Ramsey | 0e1df43 | 2007-01-12 02:58:12 +0000 | [diff] [blame] | 2837 | openfile->mark_set ? write_marked_file(temp, temp_file, TRUE, |
David Lawrence Ramsey | b6c4dbf | 2006-11-25 22:38:17 +0000 | [diff] [blame] | 2838 | OVERWRITE) : |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2839 | #endif |
David Lawrence Ramsey | b6c4dbf | 2006-11-25 22:38:17 +0000 | [diff] [blame] | 2840 | write_file(temp, temp_file, TRUE, OVERWRITE, FALSE); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2841 | |
David Lawrence Ramsey | 9e7b2d5 | 2007-01-11 22:46:22 +0000 | [diff] [blame] | 2842 | if (!status) { |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2843 | statusbar(_("Error writing temp file: %s"), strerror(errno)); |
| 2844 | free(temp); |
| 2845 | return; |
| 2846 | } |
| 2847 | |
| 2848 | spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) : |
| 2849 | do_int_speller(temp); |
| 2850 | unlink(temp); |
| 2851 | free(temp); |
| 2852 | |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 2853 | currmenu = MMAIN; |
David Lawrence Ramsey | f32e1dd | 2006-06-09 17:09:51 +0000 | [diff] [blame] | 2854 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2855 | /* If the spell-checker printed any error messages onscreen, make |
| 2856 | * sure that they're cleared off. */ |
David Lawrence Ramsey | f32e1dd | 2006-06-09 17:09:51 +0000 | [diff] [blame] | 2857 | total_refresh(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2858 | |
| 2859 | if (spell_msg != NULL) { |
| 2860 | if (errno == 0) |
| 2861 | /* Don't display an error message of "Success". */ |
| 2862 | statusbar(_("Spell checking failed: %s"), spell_msg); |
| 2863 | else |
| 2864 | statusbar(_("Spell checking failed: %s: %s"), spell_msg, |
| 2865 | strerror(errno)); |
| 2866 | } else |
| 2867 | statusbar(_("Finished checking spelling")); |
| 2868 | } |
| 2869 | #endif /* !DISABLE_SPELLER */ |
| 2870 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2871 | #ifndef NANO_TINY |
David Lawrence Ramsey | d7f0fe9 | 2005-08-10 22:51:49 +0000 | [diff] [blame] | 2872 | /* Our own version of "wc". Note that its character counts are in |
| 2873 | * multibyte characters instead of single-byte characters. */ |
David Lawrence Ramsey | 8e94234 | 2005-07-25 04:21:46 +0000 | [diff] [blame] | 2874 | void do_wordlinechar_count(void) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2875 | { |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 2876 | size_t words = 0, chars = 0; |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 2877 | ssize_t nlines = 0; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2878 | size_t current_x_save = openfile->current_x; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2879 | size_t pww_save = openfile->placewewant; |
| 2880 | filestruct *current_save = openfile->current; |
| 2881 | bool old_mark_set = openfile->mark_set; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2882 | filestruct *top, *bot; |
| 2883 | size_t top_x, bot_x; |
| 2884 | |
| 2885 | if (old_mark_set) { |
| 2886 | /* If the mark is on, partition the filestruct so that it |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 2887 | * contains only the marked text, and turn the mark off. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2888 | mark_order((const filestruct **)&top, &top_x, |
| 2889 | (const filestruct **)&bot, &bot_x, NULL); |
| 2890 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2891 | openfile->mark_set = FALSE; |
| 2892 | } |
| 2893 | |
| 2894 | /* Start at the top of the file. */ |
| 2895 | openfile->current = openfile->fileage; |
| 2896 | openfile->current_x = 0; |
| 2897 | openfile->placewewant = 0; |
| 2898 | |
| 2899 | /* Keep moving to the next word (counting punctuation characters as |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2900 | * part of a word, as "wc -w" does), without updating the screen, |
| 2901 | * until we reach the end of the file, incrementing the total word |
| 2902 | * count whenever we're on a word just before moving. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2903 | while (openfile->current != openfile->filebot || |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 2904 | openfile->current->data[openfile->current_x] != '\0') { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2905 | if (do_next_word(TRUE, FALSE)) |
| 2906 | words++; |
| 2907 | } |
| 2908 | |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2909 | /* Get the total line and character counts, as "wc -l" and "wc -c" |
| 2910 | * do, but get the latter in multibyte characters. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2911 | if (old_mark_set) { |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 2912 | nlines = openfile->filebot->lineno - |
David Lawrence Ramsey | 78a81b2 | 2005-07-25 18:59:24 +0000 | [diff] [blame] | 2913 | openfile->fileage->lineno + 1; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2914 | chars = get_totsize(openfile->fileage, openfile->filebot); |
| 2915 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2916 | /* Unpartition the filestruct so that it contains all the text |
| 2917 | * again, and turn the mark back on. */ |
| 2918 | unpartition_filestruct(&filepart); |
| 2919 | openfile->mark_set = TRUE; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2920 | } else { |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 2921 | nlines = openfile->filebot->lineno; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2922 | chars = openfile->totsize; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | /* Restore where we were. */ |
| 2926 | openfile->current = current_save; |
| 2927 | openfile->current_x = current_x_save; |
| 2928 | openfile->placewewant = pww_save; |
| 2929 | |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2930 | /* Display the total word, line, and character counts on the |
| 2931 | * statusbar. */ |
David Lawrence Ramsey | 7b71f57 | 2005-10-06 20:46:11 +0000 | [diff] [blame] | 2932 | statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ? |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 2933 | _("In Selection: ") : "", (unsigned long)words, (long)nlines, |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 2934 | (unsigned long)chars); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2935 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2936 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2937 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2938 | /* Get verbatim input. */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2939 | void do_verbatim_input(void) |
| 2940 | { |
| 2941 | int *kbinput; |
| 2942 | size_t kbinput_len, i; |
| 2943 | char *output; |
| 2944 | |
David Lawrence Ramsey | f451d6a | 2006-05-27 16:02:48 +0000 | [diff] [blame] | 2945 | /* TRANSLATORS: This is displayed when the next keystroke will be |
| 2946 | * inserted verbatim. */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2947 | statusbar(_("Verbatim Input")); |
| 2948 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2949 | /* Read in all the verbatim characters. */ |
| 2950 | kbinput = get_verbatim_kbinput(edit, &kbinput_len); |
| 2951 | |
David Lawrence Ramsey | a620e68 | 2006-05-27 18:19:03 +0000 | [diff] [blame] | 2952 | /* If constant cursor position display is on, make sure the current |
| 2953 | * cursor position will be properly displayed on the statusbar. |
| 2954 | * Otherwise, blank the statusbar. */ |
| 2955 | if (ISSET(CONST_UPDATE)) |
| 2956 | do_cursorpos(TRUE); |
| 2957 | else { |
| 2958 | blank_statusbar(); |
| 2959 | wnoutrefresh(bottomwin); |
| 2960 | } |
David Lawrence Ramsey | 6fb6689 | 2006-05-27 17:39:19 +0000 | [diff] [blame] | 2961 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2962 | /* Display all the verbatim characters at once, not filtering out |
| 2963 | * control characters. */ |
| 2964 | output = charalloc(kbinput_len + 1); |
| 2965 | |
| 2966 | for (i = 0; i < kbinput_len; i++) |
| 2967 | output[i] = (char)kbinput[i]; |
| 2968 | output[i] = '\0'; |
| 2969 | |
David Lawrence Ramsey | ad36bdc | 2006-12-02 17:22:21 +0000 | [diff] [blame] | 2970 | free(kbinput); |
| 2971 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2972 | do_output(output, kbinput_len, TRUE); |
| 2973 | |
| 2974 | free(output); |
| 2975 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 2976 | |