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