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