David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /************************************************************************** |
David Lawrence Ramsey | 4005fc6 | 2005-10-08 06:12:41 +0000 | [diff] [blame] | 3 | * text.c * |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 4 | * * |
David Lawrence Ramsey | cb801d6 | 2006-11-10 02:47:11 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Chris Allegretta * |
| 6 | * Copyright (C) 2005, 2006 David Lawrence Ramsey * |
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 * |
| 9 | * the Free Software Foundation; either version 2, or (at your option) * |
| 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 | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 66 | /* Delete one character. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 67 | void do_delete(void) |
| 68 | { |
| 69 | bool do_refresh = FALSE; |
| 70 | /* Do we have to call edit_refresh(), or can we get away with |
David Lawrence Ramsey | 75e9dfe | 2006-05-18 17:50:08 +0000 | [diff] [blame] | 71 | * just update_line()? */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 72 | |
| 73 | assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data)); |
| 74 | |
| 75 | openfile->placewewant = xplustabs(); |
| 76 | |
| 77 | if (openfile->current->data[openfile->current_x] != '\0') { |
| 78 | int char_buf_len = parse_mbchar(openfile->current->data + |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 79 | openfile->current_x, NULL, NULL); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 80 | size_t line_len = strlen(openfile->current->data + |
| 81 | openfile->current_x); |
| 82 | |
| 83 | assert(openfile->current_x < strlen(openfile->current->data)); |
| 84 | |
| 85 | /* Let's get dangerous. */ |
| 86 | charmove(&openfile->current->data[openfile->current_x], |
| 87 | &openfile->current->data[openfile->current_x + |
| 88 | char_buf_len], line_len - char_buf_len + 1); |
| 89 | |
| 90 | null_at(&openfile->current->data, openfile->current_x + |
| 91 | line_len - char_buf_len); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 92 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 93 | if (openfile->mark_set && openfile->mark_begin == |
| 94 | openfile->current && openfile->current_x < |
| 95 | openfile->mark_begin_x) |
| 96 | openfile->mark_begin_x -= char_buf_len; |
| 97 | #endif |
| 98 | openfile->totsize--; |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 99 | } else if (openfile->current != openfile->filebot) { |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 100 | filestruct *foo = openfile->current->next; |
| 101 | |
| 102 | assert(openfile->current_x == strlen(openfile->current->data)); |
| 103 | |
| 104 | /* If we're deleting at the end of a line, we need to call |
| 105 | * edit_refresh(). */ |
| 106 | if (openfile->current->data[openfile->current_x] == '\0') |
| 107 | do_refresh = TRUE; |
| 108 | |
| 109 | openfile->current->data = charealloc(openfile->current->data, |
| 110 | openfile->current_x + strlen(foo->data) + 1); |
| 111 | strcpy(openfile->current->data + openfile->current_x, |
| 112 | foo->data); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 113 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 114 | if (openfile->mark_set && openfile->mark_begin == |
| 115 | openfile->current->next) { |
| 116 | openfile->mark_begin = openfile->current; |
| 117 | openfile->mark_begin_x += openfile->current_x; |
| 118 | } |
| 119 | #endif |
| 120 | if (openfile->filebot == foo) |
| 121 | openfile->filebot = openfile->current; |
| 122 | |
| 123 | unlink_node(foo); |
| 124 | delete_node(foo); |
| 125 | renumber(openfile->current); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 126 | openfile->totsize--; |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 127 | |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 128 | /* If the NO_NEWLINES flag isn't set, and text has been added to |
| 129 | * 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] | 130 | * before filebot, add a new magicline. */ |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 131 | if (!ISSET(NO_NEWLINES) && openfile->current == |
| 132 | openfile->filebot && openfile->current->data[0] != '\0') |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 133 | new_magicline(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 134 | } else |
| 135 | return; |
| 136 | |
David Lawrence Ramsey | 0f6236f | 2005-11-09 00:08:29 +0000 | [diff] [blame] | 137 | set_modified(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 138 | |
| 139 | #ifdef ENABLE_COLOR |
| 140 | /* If color syntaxes are available and turned on, we need to call |
| 141 | * edit_refresh(). */ |
| 142 | if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX)) |
| 143 | do_refresh = TRUE; |
| 144 | #endif |
| 145 | |
| 146 | if (do_refresh) |
| 147 | edit_refresh(); |
| 148 | else |
| 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 |
| 187 | do_output("\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 | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 194 | /* Indent or unindent the current line (or all lines covered by the mark |
| 195 | * if the mark is on) len columns, depending on whether len is positive |
| 196 | * or negative. If the TABS_TO_SPACES flag is set, indent/unindent by |
| 197 | * len spaces. Otherwise, indent/unindent by (len / tabsize) tabs and |
| 198 | * (len % tabsize) spaces. */ |
| 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. */ |
| 345 | edit_refresh(); |
| 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 | } |
| 362 | #endif /* !NANO_TINY */ |
| 363 | |
David Lawrence Ramsey | b0e04c0 | 2005-12-08 07:24:54 +0000 | [diff] [blame] | 364 | /* Someone hits Enter *gasp!* */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 365 | void do_enter(void) |
| 366 | { |
| 367 | filestruct *newnode = make_new_node(openfile->current); |
| 368 | size_t extra = 0; |
| 369 | |
| 370 | assert(openfile->current != NULL && openfile->current->data != NULL); |
| 371 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 372 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 373 | /* Do auto-indenting, like the neolithic Turbo Pascal editor. */ |
| 374 | if (ISSET(AUTOINDENT)) { |
| 375 | /* If we are breaking the line in the indentation, the new |
| 376 | * indentation should have only current_x characters, and |
| 377 | * current_x should not change. */ |
| 378 | extra = indent_length(openfile->current->data); |
| 379 | if (extra > openfile->current_x) |
| 380 | extra = openfile->current_x; |
| 381 | } |
| 382 | #endif |
| 383 | newnode->data = charalloc(strlen(openfile->current->data + |
| 384 | openfile->current_x) + extra + 1); |
| 385 | strcpy(&newnode->data[extra], openfile->current->data + |
| 386 | openfile->current_x); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 387 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 388 | if (ISSET(AUTOINDENT)) { |
| 389 | strncpy(newnode->data, openfile->current->data, extra); |
| 390 | openfile->totsize += mbstrlen(newnode->data); |
| 391 | } |
| 392 | #endif |
| 393 | null_at(&openfile->current->data, openfile->current_x); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 394 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 395 | if (openfile->mark_set && openfile->current == |
| 396 | openfile->mark_begin && openfile->current_x < |
| 397 | openfile->mark_begin_x) { |
| 398 | openfile->mark_begin = newnode; |
| 399 | openfile->mark_begin_x += extra - openfile->current_x; |
| 400 | } |
| 401 | #endif |
| 402 | openfile->current_x = extra; |
| 403 | |
| 404 | if (openfile->current == openfile->filebot) |
| 405 | openfile->filebot = newnode; |
| 406 | splice_node(openfile->current, newnode, |
| 407 | openfile->current->next); |
| 408 | |
| 409 | renumber(openfile->current); |
| 410 | openfile->current = newnode; |
| 411 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 412 | openfile->totsize++; |
| 413 | set_modified(); |
David Lawrence Ramsey | feb89db | 2005-09-13 04:45:46 +0000 | [diff] [blame] | 414 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 415 | openfile->placewewant = xplustabs(); |
David Lawrence Ramsey | feb89db | 2005-09-13 04:45:46 +0000 | [diff] [blame] | 416 | |
| 417 | edit_refresh(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 418 | } |
| 419 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 420 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 421 | /* Send a SIGKILL (unconditional kill) to the forked process in |
| 422 | * execute_command(). */ |
David Lawrence Ramsey | 8befda6 | 2005-12-06 19:39:56 +0000 | [diff] [blame] | 423 | RETSIGTYPE cancel_command(int signal) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 424 | { |
| 425 | if (kill(pid, SIGKILL) == -1) |
| 426 | nperror("kill"); |
| 427 | } |
| 428 | |
David Lawrence Ramsey | 0ed7171 | 2005-11-08 23:09:47 +0000 | [diff] [blame] | 429 | /* Execute command in a shell. Return TRUE on success. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 430 | bool execute_command(const char *command) |
| 431 | { |
| 432 | int fd[2]; |
| 433 | FILE *f; |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 434 | char *shellenv; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 435 | struct sigaction oldaction, newaction; |
| 436 | /* Original and temporary handlers for SIGINT. */ |
| 437 | bool sig_failed = FALSE; |
| 438 | /* Did sigaction() fail without changing the signal handlers? */ |
| 439 | |
| 440 | /* Make our pipes. */ |
| 441 | if (pipe(fd) == -1) { |
| 442 | statusbar(_("Could not pipe")); |
| 443 | return FALSE; |
| 444 | } |
| 445 | |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 446 | /* 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] | 447 | * /bin/sh. Note that $SHELL should contain only a path, with no |
| 448 | * arguments. */ |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 449 | shellenv = getenv("SHELL"); |
| 450 | if (shellenv == NULL) |
| 451 | shellenv = "/bin/sh"; |
| 452 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 453 | /* Fork a child. */ |
| 454 | if ((pid = fork()) == 0) { |
| 455 | close(fd[0]); |
| 456 | dup2(fd[1], fileno(stdout)); |
| 457 | dup2(fd[1], fileno(stderr)); |
| 458 | |
| 459 | /* If execl() returns at all, there was an error. */ |
David Lawrence Ramsey | 5da68ee | 2005-11-29 05:21:06 +0000 | [diff] [blame] | 460 | execl(shellenv, tail(shellenv), "-c", command, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 461 | exit(0); |
| 462 | } |
| 463 | |
David Lawrence Ramsey | c838a4c | 2006-04-26 18:33:50 +0000 | [diff] [blame] | 464 | /* Continue as parent. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 465 | close(fd[1]); |
| 466 | |
| 467 | if (pid == -1) { |
| 468 | close(fd[0]); |
| 469 | statusbar(_("Could not fork")); |
| 470 | return FALSE; |
| 471 | } |
| 472 | |
| 473 | /* Before we start reading the forked command's output, we set |
| 474 | * things up so that Ctrl-C will cancel the new process. */ |
| 475 | |
| 476 | /* Enable interpretation of the special control keys so that we get |
| 477 | * SIGINT when Ctrl-C is pressed. */ |
| 478 | enable_signals(); |
| 479 | |
| 480 | if (sigaction(SIGINT, NULL, &newaction) == -1) { |
| 481 | sig_failed = TRUE; |
| 482 | nperror("sigaction"); |
| 483 | } else { |
| 484 | newaction.sa_handler = cancel_command; |
| 485 | if (sigaction(SIGINT, &newaction, &oldaction) == -1) { |
| 486 | sig_failed = TRUE; |
| 487 | nperror("sigaction"); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* Note that now oldaction is the previous SIGINT signal handler, |
| 492 | * to be restored later. */ |
| 493 | |
| 494 | f = fdopen(fd[0], "rb"); |
| 495 | if (f == NULL) |
| 496 | nperror("fdopen"); |
| 497 | |
| 498 | read_file(f, "stdin"); |
| 499 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 500 | if (wait(NULL) == -1) |
| 501 | nperror("wait"); |
| 502 | |
| 503 | if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1) |
| 504 | nperror("sigaction"); |
| 505 | |
| 506 | /* Disable interpretation of the special control keys so that we can |
| 507 | * use Ctrl-C for other things. */ |
| 508 | disable_signals(); |
| 509 | |
| 510 | return TRUE; |
| 511 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 512 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 513 | |
| 514 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | ef0d5a7 | 2006-05-22 02:08:49 +0000 | [diff] [blame] | 515 | /* 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] | 516 | * something other than type text. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 517 | void wrap_reset(void) |
| 518 | { |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 519 | prepend_wrap = FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* 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] | 523 | * moved forward since the last typed character. Return TRUE if we |
| 524 | * wrapped, and FALSE otherwise. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 525 | bool do_wrap(filestruct *line) |
| 526 | { |
| 527 | size_t line_len; |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 528 | /* The length of the line we wrap. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 529 | ssize_t wrap_loc; |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 530 | /* The index of line->data where we wrap. */ |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 531 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 532 | const char *indent_string = NULL; |
| 533 | /* Indentation to prepend to the new line. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 534 | size_t indent_len = 0; |
| 535 | /* The length of indent_string. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 536 | #endif |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 537 | const char *after_break; |
| 538 | /* The text after the wrap point. */ |
| 539 | size_t after_break_len; |
| 540 | /* The length of after_break. */ |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 541 | bool prepending = FALSE; |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 542 | /* Do we prepend to the next line? */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 543 | const char *next_line = NULL; |
| 544 | /* The next line, minus indentation. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 545 | size_t next_line_len = 0; |
| 546 | /* The length of next_line. */ |
| 547 | char *new_line = NULL; |
| 548 | /* The line we create. */ |
| 549 | size_t new_line_len = 0; |
| 550 | /* The eventual length of new_line. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 551 | |
| 552 | /* There are three steps. First, we decide where to wrap. Then, we |
| 553 | * create the new wrap line. Finally, we clean up. */ |
| 554 | |
| 555 | /* Step 1, finding where to wrap. We are going to add a new line |
| 556 | * after a blank character. In this step, we call break_line() to |
| 557 | * 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] | 558 | * set wrap_loc to the location of the character after it, so that |
| 559 | * the blank is preserved at the end of the line. |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 560 | * |
| 561 | * If there is no legal wrap point, or we reach the last character |
| 562 | * of the line while trying to find one, we should return without |
| 563 | * wrapping. Note that if autoindent is turned on, we don't break |
| 564 | * at the end of it! */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 565 | assert(line != NULL && line->data != NULL); |
| 566 | |
| 567 | /* Save the length of the line. */ |
| 568 | line_len = strlen(line->data); |
| 569 | |
| 570 | /* Find the last blank where we can break the line. */ |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 571 | wrap_loc = break_line(line->data, fill |
| 572 | #ifndef DISABLE_HELP |
| 573 | , FALSE |
| 574 | #endif |
| 575 | ); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 576 | |
| 577 | /* If we couldn't break the line, or we've reached the end of it, we |
| 578 | * don't wrap. */ |
| 579 | if (wrap_loc == -1 || line->data[wrap_loc] == '\0') |
| 580 | return FALSE; |
| 581 | |
| 582 | /* Otherwise, move forward to the character just after the blank. */ |
| 583 | wrap_loc += move_mbright(line->data + wrap_loc, 0); |
| 584 | |
| 585 | /* If we've reached the end of the line, we don't wrap. */ |
| 586 | if (line->data[wrap_loc] == '\0') |
| 587 | return FALSE; |
| 588 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 589 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 590 | /* If autoindent is turned on, and we're on the character just after |
| 591 | * the indentation, we don't wrap. */ |
| 592 | if (ISSET(AUTOINDENT)) { |
| 593 | /* Get the indentation of this line. */ |
| 594 | indent_string = line->data; |
| 595 | indent_len = indent_length(indent_string); |
| 596 | |
| 597 | if (wrap_loc == indent_len) |
| 598 | return FALSE; |
| 599 | } |
| 600 | #endif |
| 601 | |
| 602 | /* Step 2, making the new wrap line. It will consist of indentation |
| 603 | * followed by the text after the wrap point, optionally followed by |
| 604 | * 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] | 605 | * and the text of the next line, if they can fit without wrapping, |
| 606 | * the next line exists, and the prepend_wrap flag is set. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 607 | |
| 608 | /* after_break is the text that will be wrapped to the next line. */ |
| 609 | after_break = line->data + wrap_loc; |
| 610 | after_break_len = line_len - wrap_loc; |
| 611 | |
| 612 | assert(strlen(after_break) == after_break_len); |
| 613 | |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 614 | /* We prepend the wrapped text to the next line, if the prepend_wrap |
| 615 | * flag is set, there is a next line, and prepending would not make |
| 616 | * the line too long. */ |
| 617 | if (prepend_wrap && line != openfile->filebot) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 618 | const char *end = after_break + move_mbleft(after_break, |
| 619 | after_break_len); |
| 620 | |
| 621 | /* If after_break doesn't end in a blank, make sure it ends in a |
| 622 | * space. */ |
| 623 | if (!is_blank_mbchar(end)) { |
| 624 | line_len++; |
| 625 | line->data = charealloc(line->data, line_len + 1); |
| 626 | line->data[line_len - 1] = ' '; |
| 627 | line->data[line_len] = '\0'; |
| 628 | after_break = line->data + wrap_loc; |
| 629 | after_break_len++; |
| 630 | openfile->totsize++; |
| 631 | } |
| 632 | |
| 633 | next_line = line->next->data; |
| 634 | next_line_len = strlen(next_line); |
| 635 | |
| 636 | if (after_break_len + next_line_len <= fill) { |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 637 | prepending = TRUE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 638 | new_line_len += next_line_len; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* new_line_len is now the length of the text that will be wrapped |
| 643 | * to the next line, plus (if we're prepending to it) the length of |
| 644 | * the text of the next line. */ |
| 645 | new_line_len += after_break_len; |
| 646 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 647 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 648 | if (ISSET(AUTOINDENT)) { |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 649 | if (prepending) { |
| 650 | /* If we're prepending, the indentation will come from the |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 651 | * next line. */ |
| 652 | indent_string = next_line; |
| 653 | indent_len = indent_length(indent_string); |
| 654 | next_line += indent_len; |
| 655 | } else { |
| 656 | /* Otherwise, it will come from this line, in which case |
| 657 | * we should increase new_line_len to make room for it. */ |
| 658 | new_line_len += indent_len; |
| 659 | openfile->totsize += mbstrnlen(indent_string, indent_len); |
| 660 | } |
| 661 | } |
| 662 | #endif |
| 663 | |
| 664 | /* Now we allocate the new line and copy the text into it. */ |
| 665 | new_line = charalloc(new_line_len + 1); |
| 666 | new_line[0] = '\0'; |
| 667 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 668 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 669 | if (ISSET(AUTOINDENT)) { |
| 670 | /* Copy the indentation. */ |
| 671 | strncpy(new_line, indent_string, indent_len); |
| 672 | new_line[indent_len] = '\0'; |
| 673 | new_line_len += indent_len; |
| 674 | } |
| 675 | #endif |
| 676 | |
| 677 | /* Copy all the text after the wrap point of the current line. */ |
| 678 | strcat(new_line, after_break); |
| 679 | |
| 680 | /* Break the current line at the wrap point. */ |
| 681 | null_at(&line->data, wrap_loc); |
| 682 | |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 683 | if (prepending) { |
| 684 | /* 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] | 685 | * the indentation that we already copied above. */ |
| 686 | strcat(new_line, next_line); |
| 687 | |
| 688 | free(line->next->data); |
| 689 | line->next->data = new_line; |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 690 | |
| 691 | /* If the NO_NEWLINES flag isn't set, and text has been added to |
| 692 | * the magicline, make a new magicline. */ |
| 693 | if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0') |
| 694 | new_magicline(); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 695 | } else { |
| 696 | /* Otherwise, make a new line and copy the text after where we |
| 697 | * broke this line to the beginning of the new line. */ |
| 698 | splice_node(openfile->current, make_new_node(openfile->current), |
| 699 | openfile->current->next); |
| 700 | |
David Lawrence Ramsey | 219a814 | 2005-11-22 22:08:01 +0000 | [diff] [blame] | 701 | /* If the current line is the last line of the file, move the |
| 702 | * last line of the file down to the next line. */ |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 703 | if (openfile->filebot == openfile->current) |
| 704 | openfile->filebot = openfile->current->next; |
| 705 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 706 | openfile->current->next->data = new_line; |
| 707 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 708 | openfile->totsize++; |
| 709 | } |
| 710 | |
| 711 | /* Step 3, clean up. Reposition the cursor and mark, and do some |
| 712 | * other sundry things. */ |
| 713 | |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 714 | /* Set the prepend_wrap flag, so that later wraps of this line will |
| 715 | * be prepended to the next line. */ |
| 716 | prepend_wrap = TRUE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 717 | |
David Lawrence Ramsey | 2829aae | 2006-05-22 01:24:09 +0000 | [diff] [blame] | 718 | /* Each line knows its number. We recalculate these if we inserted |
| 719 | * a new line. */ |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 720 | if (!prepending) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 721 | renumber(line); |
| 722 | |
| 723 | /* 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] | 724 | * also clear the prepend_wrap flag in this case. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 725 | if (openfile->current_x > wrap_loc) { |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 726 | prepend_wrap = FALSE; |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 727 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 728 | openfile->current = openfile->current->next; |
| 729 | openfile->current_x -= wrap_loc |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 730 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 731 | - indent_len |
| 732 | #endif |
| 733 | ; |
| 734 | openfile->placewewant = xplustabs(); |
| 735 | } |
| 736 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 737 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 738 | /* 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] | 739 | * 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] | 740 | * we move it right. */ |
| 741 | if (openfile->mark_set) { |
| 742 | if (openfile->mark_begin == line && openfile->mark_begin_x > |
| 743 | wrap_loc) { |
| 744 | openfile->mark_begin = line->next; |
| 745 | openfile->mark_begin_x -= wrap_loc - indent_len + 1; |
David Lawrence Ramsey | 615f4c7 | 2005-11-22 21:48:24 +0000 | [diff] [blame] | 746 | } else if (prepending && openfile->mark_begin == line->next) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 747 | openfile->mark_begin_x += after_break_len; |
| 748 | } |
| 749 | #endif |
| 750 | |
| 751 | return TRUE; |
| 752 | } |
| 753 | #endif /* !DISABLE_WRAPPING */ |
| 754 | |
David Lawrence Ramsey | c7c04bb | 2005-11-29 21:30:00 +0000 | [diff] [blame] | 755 | #if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 756 | /* 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] | 757 | * that the display length to there is at most (goal + 1). If there is |
| 758 | * 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] | 759 | * blank in that group of blanks. The terminating '\0' counts as a |
| 760 | * blank, as does a '\n' if newline is TRUE. */ |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 761 | ssize_t break_line(const char *line, ssize_t goal |
| 762 | #ifndef DISABLE_HELP |
| 763 | , bool newline |
| 764 | #endif |
| 765 | ) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 766 | { |
| 767 | ssize_t blank_loc = -1; |
| 768 | /* Current tentative return value. Index of the last blank we |
| 769 | * found with short enough display width. */ |
| 770 | ssize_t cur_loc = 0; |
| 771 | /* Current index in line. */ |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 772 | size_t cur_pos = 0; |
| 773 | /* Current column position in line. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 774 | int line_len; |
| 775 | |
| 776 | assert(line != NULL); |
| 777 | |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 778 | while (*line != '\0' && goal >= cur_pos) { |
| 779 | line_len = parse_mbchar(line, NULL, &cur_pos); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 780 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 781 | if (is_blank_mbchar(line) |
| 782 | #ifndef DISABLE_HELP |
| 783 | || (newline && *line == '\n') |
| 784 | #endif |
| 785 | ) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 786 | blank_loc = cur_loc; |
| 787 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 788 | #ifndef DISABLE_HELP |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 789 | if (newline && *line == '\n') |
| 790 | break; |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 791 | #endif |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 792 | } |
| 793 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 794 | line += line_len; |
| 795 | cur_loc += line_len; |
| 796 | } |
| 797 | |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 798 | if (goal >= cur_pos) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 799 | /* In fact, the whole line displays shorter than goal. */ |
| 800 | return cur_loc; |
| 801 | |
| 802 | if (blank_loc == -1) { |
| 803 | /* No blank was found that was short enough. */ |
| 804 | bool found_blank = FALSE; |
David Lawrence Ramsey | 5ab12ca | 2005-09-20 17:52:52 +0000 | [diff] [blame] | 805 | ssize_t found_blank_loc = 0; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 806 | |
| 807 | while (*line != '\0') { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 808 | line_len = parse_mbchar(line, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 809 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 810 | if (is_blank_mbchar(line) |
| 811 | #ifndef DISABLE_HELP |
| 812 | || (newline && *line == '\n') |
| 813 | #endif |
| 814 | ) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 815 | if (!found_blank) |
| 816 | found_blank = TRUE; |
David Lawrence Ramsey | bdc1b9b | 2005-09-20 16:36:08 +0000 | [diff] [blame] | 817 | found_blank_loc = cur_loc; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 818 | } else if (found_blank) |
David Lawrence Ramsey | bdc1b9b | 2005-09-20 16:36:08 +0000 | [diff] [blame] | 819 | return found_blank_loc; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 820 | |
| 821 | line += line_len; |
| 822 | cur_loc += line_len; |
| 823 | } |
| 824 | |
| 825 | return -1; |
| 826 | } |
| 827 | |
| 828 | /* Move to the last blank after blank_loc, if there is one. */ |
| 829 | line -= cur_loc; |
| 830 | line += blank_loc; |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 831 | line_len = parse_mbchar(line, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 832 | line += line_len; |
| 833 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 834 | while (*line != '\0' && (is_blank_mbchar(line) |
| 835 | #ifndef DISABLE_HELP |
| 836 | || (newline && *line == '\n') |
| 837 | #endif |
| 838 | )) { |
David Lawrence Ramsey | cd243f5 | 2006-05-19 23:27:16 +0000 | [diff] [blame] | 839 | #ifndef DISABLE_HELP |
| 840 | if (newline && *line == '\n') |
| 841 | break; |
| 842 | #endif |
| 843 | |
David Lawrence Ramsey | 39bd1b3 | 2006-05-20 13:11:56 +0000 | [diff] [blame] | 844 | line_len = parse_mbchar(line, NULL, NULL); |
| 845 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 846 | line += line_len; |
| 847 | blank_loc += line_len; |
| 848 | } |
| 849 | |
| 850 | return blank_loc; |
| 851 | } |
David Lawrence Ramsey | c7c04bb | 2005-11-29 21:30:00 +0000 | [diff] [blame] | 852 | #endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 853 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 854 | #if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 855 | /* The "indentation" of a line is the whitespace between the quote part |
| 856 | * and the non-whitespace of the line. */ |
| 857 | size_t indent_length(const char *line) |
| 858 | { |
| 859 | size_t len = 0; |
| 860 | char *blank_mb; |
| 861 | int blank_mb_len; |
| 862 | |
| 863 | assert(line != NULL); |
| 864 | |
| 865 | blank_mb = charalloc(mb_cur_max()); |
| 866 | |
| 867 | while (*line != '\0') { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 868 | blank_mb_len = parse_mbchar(line, blank_mb, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 869 | |
| 870 | if (!is_blank_mbchar(blank_mb)) |
| 871 | break; |
| 872 | |
| 873 | line += blank_mb_len; |
| 874 | len += blank_mb_len; |
| 875 | } |
| 876 | |
| 877 | free(blank_mb); |
| 878 | |
| 879 | return len; |
| 880 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 881 | #endif /* !NANO_TINY || !DISABLE_JUSTIFY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 882 | |
| 883 | #ifndef DISABLE_JUSTIFY |
| 884 | /* justify_format() replaces blanks with spaces and multiple spaces by 1 |
| 885 | * (except it maintains up to 2 after a character in punct optionally |
| 886 | * followed by a character in brackets, and removes all from the end). |
| 887 | * |
| 888 | * justify_format() might make paragraph->data shorter, and change the |
| 889 | * actual pointer with null_at(). |
| 890 | * |
| 891 | * justify_format() will not look at the first skip characters of |
| 892 | * paragraph. skip should be at most strlen(paragraph->data). The |
| 893 | * character at paragraph[skip + 1] must not be blank. */ |
| 894 | void justify_format(filestruct *paragraph, size_t skip) |
| 895 | { |
| 896 | char *end, *new_end, *new_paragraph_data; |
| 897 | size_t shift = 0; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 898 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 899 | size_t mark_shift = 0; |
| 900 | #endif |
| 901 | |
| 902 | /* These four asserts are assumptions about the input data. */ |
| 903 | assert(paragraph != NULL); |
| 904 | assert(paragraph->data != NULL); |
| 905 | assert(skip < strlen(paragraph->data)); |
| 906 | assert(!is_blank_mbchar(paragraph->data + skip)); |
| 907 | |
| 908 | end = paragraph->data + skip; |
| 909 | new_paragraph_data = charalloc(strlen(paragraph->data) + 1); |
| 910 | strncpy(new_paragraph_data, paragraph->data, skip); |
| 911 | new_end = new_paragraph_data + skip; |
| 912 | |
| 913 | while (*end != '\0') { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 914 | int end_len; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 915 | |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 916 | /* If this character is blank, change it to a space if |
| 917 | * necessary, and skip over all blanks after it. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 918 | if (is_blank_mbchar(end)) { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 919 | end_len = parse_mbchar(end, NULL, NULL); |
| 920 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 921 | *new_end = ' '; |
| 922 | new_end++; |
| 923 | end += end_len; |
| 924 | |
| 925 | while (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 926 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 927 | |
| 928 | end += end_len; |
| 929 | shift += end_len; |
| 930 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 931 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 932 | /* Keep track of the change in the current line. */ |
| 933 | if (openfile->mark_set && openfile->mark_begin == |
| 934 | paragraph && openfile->mark_begin_x >= end - |
| 935 | paragraph->data) |
| 936 | mark_shift += end_len; |
| 937 | #endif |
| 938 | } |
| 939 | /* If this character is punctuation optionally followed by a |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 940 | * bracket and then followed by blanks, change no more than two |
| 941 | * of the blanks to spaces if necessary, and skip over all |
| 942 | * blanks after them. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 943 | } else if (mbstrchr(punct, end) != NULL) { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 944 | end_len = parse_mbchar(end, NULL, NULL); |
| 945 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 946 | while (end_len > 0) { |
| 947 | *new_end = *end; |
| 948 | new_end++; |
| 949 | end++; |
| 950 | end_len--; |
| 951 | } |
| 952 | |
| 953 | if (*end != '\0' && mbstrchr(brackets, end) != NULL) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 954 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 955 | |
| 956 | while (end_len > 0) { |
| 957 | *new_end = *end; |
| 958 | new_end++; |
| 959 | end++; |
| 960 | end_len--; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | if (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 965 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 966 | |
| 967 | *new_end = ' '; |
| 968 | new_end++; |
| 969 | end += end_len; |
| 970 | } |
| 971 | |
| 972 | if (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 973 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 974 | |
| 975 | *new_end = ' '; |
| 976 | new_end++; |
| 977 | end += end_len; |
| 978 | } |
| 979 | |
| 980 | while (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 981 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 982 | |
| 983 | end += end_len; |
| 984 | shift += end_len; |
| 985 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 986 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 987 | /* Keep track of the change in the current line. */ |
| 988 | if (openfile->mark_set && openfile->mark_begin == |
| 989 | paragraph && openfile->mark_begin_x >= end - |
| 990 | paragraph->data) |
| 991 | mark_shift += end_len; |
| 992 | #endif |
| 993 | } |
| 994 | /* If this character is neither blank nor punctuation, leave it |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 995 | * unchanged. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 996 | } else { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 997 | end_len = parse_mbchar(end, NULL, NULL); |
| 998 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 999 | while (end_len > 0) { |
| 1000 | *new_end = *end; |
| 1001 | new_end++; |
| 1002 | end++; |
| 1003 | end_len--; |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | assert(*end == '\0'); |
| 1009 | |
| 1010 | *new_end = *end; |
| 1011 | |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1012 | /* 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] | 1013 | while (new_end > new_paragraph_data + skip && |
| 1014 | *(new_end - 1) == ' ') { |
| 1015 | new_end--; |
| 1016 | shift++; |
| 1017 | } |
| 1018 | |
| 1019 | if (shift > 0) { |
| 1020 | openfile->totsize -= shift; |
| 1021 | null_at(&new_paragraph_data, new_end - new_paragraph_data); |
| 1022 | free(paragraph->data); |
| 1023 | paragraph->data = new_paragraph_data; |
| 1024 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1025 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1026 | /* Adjust the mark coordinates to compensate for the change in |
| 1027 | * the current line. */ |
| 1028 | if (openfile->mark_set && openfile->mark_begin == paragraph) { |
| 1029 | openfile->mark_begin_x -= mark_shift; |
| 1030 | if (openfile->mark_begin_x > new_end - new_paragraph_data) |
| 1031 | openfile->mark_begin_x = new_end - new_paragraph_data; |
| 1032 | } |
| 1033 | #endif |
| 1034 | } else |
| 1035 | free(new_paragraph_data); |
| 1036 | } |
| 1037 | |
| 1038 | /* The "quote part" of a line is the largest initial substring matching |
| 1039 | * the quote string. This function returns the length of the quote part |
| 1040 | * of the given line. |
| 1041 | * |
| 1042 | * Note that if !HAVE_REGEX_H then we match concatenated copies of |
| 1043 | * quotestr. */ |
| 1044 | size_t quote_length(const char *line) |
| 1045 | { |
| 1046 | #ifdef HAVE_REGEX_H |
| 1047 | regmatch_t matches; |
| 1048 | int rc = regexec("ereg, line, 1, &matches, 0); |
| 1049 | |
| 1050 | if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1) |
| 1051 | return 0; |
| 1052 | /* matches.rm_so should be 0, since the quote string should start |
| 1053 | * with the caret ^. */ |
| 1054 | return matches.rm_eo; |
| 1055 | #else /* !HAVE_REGEX_H */ |
| 1056 | size_t qdepth = 0; |
| 1057 | |
| 1058 | /* Compute quote depth level. */ |
| 1059 | while (strncmp(line + qdepth, quotestr, quotelen) == 0) |
| 1060 | qdepth += quotelen; |
| 1061 | return qdepth; |
| 1062 | #endif /* !HAVE_REGEX_H */ |
| 1063 | } |
| 1064 | |
| 1065 | /* a_line and b_line are lines of text. The quotation part of a_line is |
| 1066 | * the first a_quote characters. Check that the quotation part of |
| 1067 | * b_line is the same. */ |
| 1068 | bool quotes_match(const char *a_line, size_t a_quote, const char |
| 1069 | *b_line) |
| 1070 | { |
| 1071 | /* Here is the assumption about a_quote. */ |
| 1072 | assert(a_quote == quote_length(a_line)); |
| 1073 | |
| 1074 | return (a_quote == quote_length(b_line) && |
| 1075 | strncmp(a_line, b_line, a_quote) == 0); |
| 1076 | } |
| 1077 | |
| 1078 | /* We assume a_line and b_line have no quote part. Then, we return |
| 1079 | * whether b_line could follow a_line in a paragraph. */ |
| 1080 | bool indents_match(const char *a_line, size_t a_indent, const char |
| 1081 | *b_line, size_t b_indent) |
| 1082 | { |
| 1083 | assert(a_indent == indent_length(a_line)); |
| 1084 | assert(b_indent == indent_length(b_line)); |
| 1085 | |
| 1086 | return (b_indent <= a_indent && |
| 1087 | strncmp(a_line, b_line, b_indent) == 0); |
| 1088 | } |
| 1089 | |
| 1090 | /* Is foo the beginning of a paragraph? |
| 1091 | * |
| 1092 | * A line of text consists of a "quote part", followed by an |
| 1093 | * "indentation part", followed by text. The functions quote_length() |
| 1094 | * and indent_length() calculate these parts. |
| 1095 | * |
| 1096 | * A line is "part of a paragraph" if it has a part not in the quote |
| 1097 | * part or the indentation. |
| 1098 | * |
| 1099 | * A line is "the beginning of a paragraph" if it is part of a |
| 1100 | * paragraph and |
| 1101 | * 1) it is the top line of the file, or |
| 1102 | * 2) the line above it is not part of a paragraph, or |
| 1103 | * 3) the line above it does not have precisely the same quote |
| 1104 | * part, or |
| 1105 | * 4) the indentation of this line is not an initial substring of |
| 1106 | * the indentation of the previous line, or |
| 1107 | * 5) this line has no quote part and some indentation, and |
| 1108 | * autoindent isn't turned on. |
| 1109 | * The reason for number 5) is that if autoindent isn't turned on, |
| 1110 | * then an indented line is expected to start a paragraph, as in |
| 1111 | * books. Thus, nano can justify an indented paragraph only if |
| 1112 | * autoindent is turned on. */ |
| 1113 | bool begpar(const filestruct *const foo) |
| 1114 | { |
David Lawrence Ramsey | 0083bd2 | 2005-11-09 18:26:44 +0000 | [diff] [blame] | 1115 | size_t quote_len, indent_len, temp_id_len; |
| 1116 | |
| 1117 | if (foo == NULL) |
| 1118 | return FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1119 | |
| 1120 | /* Case 1). */ |
David Lawrence Ramsey | 0083bd2 | 2005-11-09 18:26:44 +0000 | [diff] [blame] | 1121 | if (foo == openfile->fileage) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1122 | return TRUE; |
| 1123 | |
| 1124 | quote_len = quote_length(foo->data); |
| 1125 | indent_len = indent_length(foo->data + quote_len); |
| 1126 | |
| 1127 | /* Not part of a paragraph. */ |
| 1128 | if (foo->data[quote_len + indent_len] == '\0') |
| 1129 | return FALSE; |
| 1130 | |
| 1131 | /* Case 3). */ |
| 1132 | if (!quotes_match(foo->data, quote_len, foo->prev->data)) |
| 1133 | return TRUE; |
| 1134 | |
| 1135 | temp_id_len = indent_length(foo->prev->data + quote_len); |
| 1136 | |
| 1137 | /* Case 2) or 5) or 4). */ |
| 1138 | if (foo->prev->data[quote_len + temp_id_len] == '\0' || |
| 1139 | (quote_len == 0 && indent_len > 0 |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1140 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1141 | && !ISSET(AUTOINDENT) |
| 1142 | #endif |
| 1143 | ) || !indents_match(foo->prev->data + quote_len, temp_id_len, |
| 1144 | foo->data + quote_len, indent_len)) |
| 1145 | return TRUE; |
| 1146 | |
| 1147 | return FALSE; |
| 1148 | } |
| 1149 | |
| 1150 | /* Is foo inside a paragraph? */ |
| 1151 | bool inpar(const filestruct *const foo) |
| 1152 | { |
| 1153 | size_t quote_len; |
| 1154 | |
| 1155 | if (foo == NULL) |
| 1156 | return FALSE; |
| 1157 | |
| 1158 | quote_len = quote_length(foo->data); |
| 1159 | |
David Lawrence Ramsey | 2101403 | 2005-11-09 20:33:42 +0000 | [diff] [blame] | 1160 | return (foo->data[quote_len + indent_length(foo->data + |
| 1161 | quote_len)] != '\0'); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
David Lawrence Ramsey | af5a999 | 2005-11-09 23:06:44 +0000 | [diff] [blame] | 1164 | /* 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] | 1165 | * justify buffer, leaving copies of those lines in place. Assume that |
| 1166 | * 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] | 1167 | * first_line. */ |
| 1168 | void backup_lines(filestruct *first_line, size_t par_len) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1169 | { |
| 1170 | filestruct *top = first_line; |
| 1171 | /* The top of the paragraph we're backing up. */ |
| 1172 | filestruct *bot = first_line; |
| 1173 | /* The bottom of the paragraph we're backing up. */ |
| 1174 | size_t i; |
| 1175 | /* Generic loop variable. */ |
| 1176 | size_t current_x_save = openfile->current_x; |
| 1177 | ssize_t fl_lineno_save = first_line->lineno; |
| 1178 | ssize_t edittop_lineno_save = openfile->edittop->lineno; |
| 1179 | ssize_t current_lineno_save = openfile->current->lineno; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1180 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1181 | bool old_mark_set = openfile->mark_set; |
| 1182 | ssize_t mb_lineno_save = 0; |
| 1183 | size_t mark_begin_x_save = 0; |
| 1184 | |
| 1185 | if (old_mark_set) { |
| 1186 | mb_lineno_save = openfile->mark_begin->lineno; |
| 1187 | mark_begin_x_save = openfile->mark_begin_x; |
| 1188 | } |
| 1189 | #endif |
| 1190 | |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 1191 | /* par_len will be one greater than the number of lines between |
| 1192 | * current and filebot if filebot is the last line in the |
| 1193 | * paragraph. */ |
David Lawrence Ramsey | 8bd960b | 2005-11-09 18:49:16 +0000 | [diff] [blame] | 1194 | assert(par_len > 0 && openfile->current->lineno + par_len <= |
David Lawrence Ramsey | 24777c0 | 2005-12-01 05:49:08 +0000 | [diff] [blame] | 1195 | openfile->filebot->lineno + 1); |
David Lawrence Ramsey | 8bd960b | 2005-11-09 18:49:16 +0000 | [diff] [blame] | 1196 | |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 1197 | /* Move bot down par_len lines to the line after the last line of |
| 1198 | * the paragraph, if there is one. */ |
| 1199 | for (i = par_len; i > 0 && bot != openfile->filebot; i--) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1200 | bot = bot->next; |
| 1201 | |
| 1202 | /* Move the paragraph from the current buffer's filestruct to the |
| 1203 | * justify buffer. */ |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 1204 | move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot, |
David Lawrence Ramsey | f0575cf | 2005-11-09 23:27:51 +0000 | [diff] [blame] | 1205 | (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1206 | |
| 1207 | /* Copy the paragraph back to the current buffer's filestruct from |
| 1208 | * the justify buffer. */ |
| 1209 | copy_from_filestruct(jusbuffer, jusbottom); |
| 1210 | |
| 1211 | /* Move upward from the last line of the paragraph to the first |
| 1212 | * line, putting first_line, edittop, current, and mark_begin at the |
| 1213 | * same lines in the copied paragraph that they had in the original |
| 1214 | * paragraph. */ |
David Lawrence Ramsey | 5d6f127 | 2005-11-10 03:32:59 +0000 | [diff] [blame] | 1215 | if (openfile->current != openfile->fileage) |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 1216 | top = openfile->current->prev; |
| 1217 | else |
| 1218 | top = openfile->current; |
David Lawrence Ramsey | e8d505b | 2005-11-10 03:40:45 +0000 | [diff] [blame] | 1219 | for (i = par_len; i > 0 && top != NULL; i--) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1220 | if (top->lineno == fl_lineno_save) |
| 1221 | first_line = top; |
| 1222 | if (top->lineno == edittop_lineno_save) |
| 1223 | openfile->edittop = top; |
| 1224 | if (top->lineno == current_lineno_save) |
| 1225 | openfile->current = top; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1226 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1227 | if (old_mark_set && top->lineno == mb_lineno_save) { |
| 1228 | openfile->mark_begin = top; |
| 1229 | openfile->mark_begin_x = mark_begin_x_save; |
| 1230 | } |
| 1231 | #endif |
| 1232 | top = top->prev; |
| 1233 | } |
| 1234 | |
| 1235 | /* Put current_x at the same place in the copied paragraph that it |
| 1236 | * had in the original paragraph. */ |
| 1237 | openfile->current_x = current_x_save; |
| 1238 | |
| 1239 | set_modified(); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 1242 | /* Find the beginning of the current paragraph if we're in one, or the |
| 1243 | * beginning of the next paragraph if we're not. Afterwards, save the |
| 1244 | * 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] | 1245 | * 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] | 1246 | * find a paragraph. |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1247 | * |
| 1248 | * See the comment at begpar() for more about when a line is the |
| 1249 | * beginning of a paragraph. */ |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 1250 | bool find_paragraph(size_t *const quote, size_t *const par) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1251 | { |
| 1252 | size_t quote_len; |
| 1253 | /* Length of the initial quotation of the paragraph we search |
| 1254 | * for. */ |
| 1255 | size_t par_len; |
| 1256 | /* Number of lines in the paragraph we search for. */ |
| 1257 | filestruct *current_save; |
| 1258 | /* The line at the beginning of the paragraph we search for. */ |
| 1259 | ssize_t current_y_save; |
| 1260 | /* The y-coordinate at the beginning of the paragraph we search |
| 1261 | * for. */ |
| 1262 | |
| 1263 | #ifdef HAVE_REGEX_H |
| 1264 | if (quoterc != 0) { |
| 1265 | statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr); |
| 1266 | return FALSE; |
| 1267 | } |
| 1268 | #endif |
| 1269 | |
| 1270 | assert(openfile->current != NULL); |
| 1271 | |
David Lawrence Ramsey | 1be131a | 2005-11-11 03:55:52 +0000 | [diff] [blame] | 1272 | /* If we're at the end of the last line of the file, it means that |
| 1273 | * there aren't any paragraphs left, so get out. */ |
| 1274 | if (openfile->current == openfile->filebot && openfile->current_x == |
| 1275 | strlen(openfile->filebot->data)) |
| 1276 | return FALSE; |
| 1277 | |
| 1278 | /* 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] | 1279 | * last line of the next paragraph, if any. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1280 | if (!inpar(openfile->current)) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1281 | do_para_end(FALSE); |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 1282 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 1283 | /* If we end up past the beginning of the line, it means that |
| 1284 | * we're at the end of the last line of the file, and the line |
| 1285 | * isn't blank, in which case the last line of the file is the |
| 1286 | * last line of the next paragraph. |
| 1287 | * |
| 1288 | * Otherwise, if we end up on a line that's in a paragraph, it |
| 1289 | * means that we're on the line after the last line of the next |
| 1290 | * paragraph, in which case we should move back to the last line |
| 1291 | * of the next paragraph. */ |
| 1292 | if (openfile->current_x == 0) { |
| 1293 | if (!inpar(openfile->current->prev)) |
| 1294 | return FALSE; |
| 1295 | if (openfile->current != openfile->fileage) |
| 1296 | openfile->current = openfile->current->prev; |
| 1297 | } |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1298 | } |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 1299 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 1300 | /* 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] | 1301 | * back to the first line of the paragraph. */ |
| 1302 | if (!begpar(openfile->current)) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1303 | do_para_begin(FALSE); |
| 1304 | |
| 1305 | /* Now current is the first line of the paragraph. Set quote_len to |
| 1306 | * 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] | 1307 | * of lines in this paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1308 | quote_len = quote_length(openfile->current->data); |
| 1309 | current_save = openfile->current; |
| 1310 | current_y_save = openfile->current_y; |
| 1311 | do_para_end(FALSE); |
| 1312 | par_len = openfile->current->lineno - current_save->lineno; |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 1313 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 1314 | /* If we end up past the beginning of the line, it means that we're |
| 1315 | * at the end of the last line of the file, and the line isn't |
| 1316 | * blank, in which case the last line of the file is part of the |
| 1317 | * paragraph. */ |
David Lawrence Ramsey | bdff665 | 2005-11-11 04:14:33 +0000 | [diff] [blame] | 1318 | if (openfile->current_x > 0) |
| 1319 | par_len++; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1320 | openfile->current = current_save; |
| 1321 | openfile->current_y = current_y_save; |
| 1322 | |
| 1323 | /* Save the values of quote_len and par_len. */ |
| 1324 | assert(quote != NULL && par != NULL); |
| 1325 | |
| 1326 | *quote = quote_len; |
| 1327 | *par = par_len; |
| 1328 | |
| 1329 | return TRUE; |
| 1330 | } |
| 1331 | |
| 1332 | /* If full_justify is TRUE, justify the entire file. Otherwise, justify |
| 1333 | * the current paragraph. */ |
| 1334 | void do_justify(bool full_justify) |
| 1335 | { |
| 1336 | filestruct *first_par_line = NULL; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1337 | /* Will be the first line of the justified paragraph(s), if any. |
| 1338 | * For restoring after unjustify. */ |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 1339 | filestruct *last_par_line = NULL; |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 1340 | /* Will be the line after the last line of the justified |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1341 | * paragraph(s), if any. Also for restoring after unjustify. */ |
David Lawrence Ramsey | 82b5deb | 2005-11-10 06:07:57 +0000 | [diff] [blame] | 1342 | bool filebot_inpar = FALSE; |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 1343 | /* Whether the text at filebot is part of the current |
| 1344 | * paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1345 | |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 1346 | /* We save these variables to be restored if the user |
| 1347 | * unjustifies. */ |
David Lawrence Ramsey | 52161ee | 2005-11-10 19:56:26 +0000 | [diff] [blame] | 1348 | filestruct *edittop_save = openfile->edittop; |
| 1349 | filestruct *current_save = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1350 | size_t current_x_save = openfile->current_x; |
| 1351 | size_t pww_save = openfile->placewewant; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1352 | size_t totsize_save = openfile->totsize; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1353 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1354 | filestruct *mark_begin_save = openfile->mark_begin; |
| 1355 | size_t mark_begin_x_save = openfile->mark_begin_x; |
| 1356 | #endif |
David Lawrence Ramsey | 52161ee | 2005-11-10 19:56:26 +0000 | [diff] [blame] | 1357 | bool modified_save = openfile->modified; |
| 1358 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1359 | int kbinput; |
| 1360 | bool meta_key, func_key, s_or_t, ran_func, finished; |
| 1361 | |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 1362 | /* 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] | 1363 | * the end of the last line of the file, if that line isn't blank, |
| 1364 | * will work the first time through. */ |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 1365 | openfile->current_x = 0; |
| 1366 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1367 | /* If we're justifying the entire file, start at the beginning. */ |
| 1368 | if (full_justify) |
| 1369 | openfile->current = openfile->fileage; |
| 1370 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1371 | while (TRUE) { |
| 1372 | size_t i; |
| 1373 | /* Generic loop variable. */ |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1374 | filestruct *curr_first_par_line; |
| 1375 | /* The first line of the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1376 | size_t quote_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1377 | /* Length of the initial quotation of the current |
| 1378 | * paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1379 | size_t indent_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1380 | /* Length of the initial indentation of the current |
| 1381 | * paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1382 | size_t par_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1383 | /* Number of lines in the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1384 | ssize_t break_pos; |
| 1385 | /* Where we will break lines. */ |
| 1386 | char *indent_string; |
| 1387 | /* The first indentation that doesn't match the initial |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1388 | * indentation of the current paragraph. This is put at the |
| 1389 | * beginning of every line broken off the first justified |
| 1390 | * line of the paragraph. Note that this works because a |
| 1391 | * paragraph can only contain two indentations at most: the |
| 1392 | * initial one, and a different one starting on a line after |
| 1393 | * the first. See the comment at begpar() for more about |
| 1394 | * when a line is part of a paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* Find the first line of the paragraph to be justified. That |
| 1397 | * is the start of this paragraph if we're in one, or the start |
| 1398 | * of the next otherwise. Save the quote length and paragraph |
| 1399 | * length (number of lines). Don't refresh the screen yet, |
| 1400 | * since we'll do that after we justify. |
| 1401 | * |
| 1402 | * 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] | 1403 | * justifying the whole file, and we've found at least one |
| 1404 | * 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] | 1405 | * last line of the file, so set the last line of the text to be |
| 1406 | * justified to the last line of the file and break out of the |
| 1407 | * loop. Otherwise, it means that there are no paragraph(s) to |
| 1408 | * justify, so refresh the screen and get out. */ |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 1409 | if (!find_paragraph("e_len, &par_len)) { |
David Lawrence Ramsey | 8b203d6 | 2005-11-11 03:17:44 +0000 | [diff] [blame] | 1410 | if (full_justify && first_par_line != NULL) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1411 | last_par_line = openfile->filebot; |
| 1412 | break; |
| 1413 | } else { |
| 1414 | edit_refresh(); |
| 1415 | return; |
| 1416 | } |
| 1417 | } |
| 1418 | |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 1419 | /* par_len will be one greater than the number of lines between |
| 1420 | * current and filebot if filebot is the last line in the |
| 1421 | * paragraph. Set filebot_inpar to TRUE if this is the case. */ |
| 1422 | filebot_inpar = (openfile->current->lineno + par_len == |
| 1423 | openfile->filebot->lineno + 1); |
| 1424 | |
David Lawrence Ramsey | cd8f735 | 2005-11-10 21:20:32 +0000 | [diff] [blame] | 1425 | /* If we haven't already done it, move the original paragraph(s) |
| 1426 | * to the justify buffer, splice a copy of the original |
| 1427 | * paragraph(s) into the file in the same place, and set |
| 1428 | * first_par_line to the first line of the copy. */ |
| 1429 | if (first_par_line == NULL) { |
| 1430 | backup_lines(openfile->current, full_justify ? |
David Lawrence Ramsey | 53f641f | 2005-11-10 21:57:56 +0000 | [diff] [blame] | 1431 | openfile->filebot->lineno - openfile->current->lineno + |
| 1432 | ((openfile->filebot->data[0] != '\0') ? 1 : 0) : |
David Lawrence Ramsey | af5a999 | 2005-11-09 23:06:44 +0000 | [diff] [blame] | 1433 | par_len); |
David Lawrence Ramsey | cd8f735 | 2005-11-10 21:20:32 +0000 | [diff] [blame] | 1434 | first_par_line = openfile->current; |
| 1435 | } |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1436 | |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1437 | /* Set curr_first_par_line to the first line of the current |
| 1438 | * paragraph. */ |
| 1439 | curr_first_par_line = openfile->current; |
| 1440 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1441 | /* Initialize indent_string to a blank string. */ |
| 1442 | indent_string = mallocstrcpy(NULL, ""); |
| 1443 | |
| 1444 | /* Find the first indentation in the paragraph that doesn't |
David Lawrence Ramsey | 8602fd6 | 2006-05-28 18:43:21 +0000 | [diff] [blame] | 1445 | * match the indentation of the first line, and save it in |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1446 | * indent_string. If all the indentations are the same, save |
| 1447 | * the indentation of the first line in indent_string. */ |
| 1448 | { |
| 1449 | const filestruct *indent_line = openfile->current; |
| 1450 | bool past_first_line = FALSE; |
| 1451 | |
| 1452 | for (i = 0; i < par_len; i++) { |
| 1453 | indent_len = quote_len + |
| 1454 | indent_length(indent_line->data + quote_len); |
| 1455 | |
| 1456 | if (indent_len != strlen(indent_string)) { |
| 1457 | indent_string = mallocstrncpy(indent_string, |
| 1458 | indent_line->data, indent_len + 1); |
| 1459 | indent_string[indent_len] = '\0'; |
| 1460 | |
| 1461 | if (past_first_line) |
| 1462 | break; |
| 1463 | } |
| 1464 | |
| 1465 | if (indent_line == openfile->current) |
| 1466 | past_first_line = TRUE; |
| 1467 | |
| 1468 | indent_line = indent_line->next; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | /* Now tack all the lines of the paragraph together, skipping |
| 1473 | * the quoting and indentation on all lines after the first. */ |
| 1474 | for (i = 0; i < par_len - 1; i++) { |
| 1475 | filestruct *next_line = openfile->current->next; |
| 1476 | size_t line_len = strlen(openfile->current->data); |
| 1477 | size_t next_line_len = |
| 1478 | strlen(openfile->current->next->data); |
| 1479 | |
| 1480 | indent_len = quote_len + |
| 1481 | indent_length(openfile->current->next->data + |
| 1482 | quote_len); |
| 1483 | |
| 1484 | next_line_len -= indent_len; |
| 1485 | openfile->totsize -= indent_len; |
| 1486 | |
| 1487 | /* We're just about to tack the next line onto this one. If |
| 1488 | * this line isn't empty, make sure it ends in a space. */ |
| 1489 | if (line_len > 0 && |
| 1490 | openfile->current->data[line_len - 1] != ' ') { |
| 1491 | line_len++; |
| 1492 | openfile->current->data = |
| 1493 | charealloc(openfile->current->data, |
| 1494 | line_len + 1); |
| 1495 | openfile->current->data[line_len - 1] = ' '; |
| 1496 | openfile->current->data[line_len] = '\0'; |
| 1497 | openfile->totsize++; |
| 1498 | } |
| 1499 | |
| 1500 | openfile->current->data = |
| 1501 | charealloc(openfile->current->data, line_len + |
| 1502 | next_line_len + 1); |
| 1503 | strcat(openfile->current->data, next_line->data + |
| 1504 | indent_len); |
| 1505 | |
David Lawrence Ramsey | 9bedc4b | 2005-11-09 19:51:48 +0000 | [diff] [blame] | 1506 | /* Don't destroy edittop or filebot! */ |
David Lawrence Ramsey | 32bd29e | 2005-11-09 03:44:23 +0000 | [diff] [blame] | 1507 | if (next_line == openfile->edittop) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1508 | openfile->edittop = openfile->current; |
David Lawrence Ramsey | 9bedc4b | 2005-11-09 19:51:48 +0000 | [diff] [blame] | 1509 | if (next_line == openfile->filebot) |
| 1510 | openfile->filebot = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1511 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1512 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1513 | /* Adjust the mark coordinates to compensate for the change |
| 1514 | * in the next line. */ |
| 1515 | if (openfile->mark_set && openfile->mark_begin == |
| 1516 | next_line) { |
| 1517 | openfile->mark_begin = openfile->current; |
| 1518 | openfile->mark_begin_x += line_len - indent_len; |
| 1519 | } |
| 1520 | #endif |
| 1521 | |
| 1522 | unlink_node(next_line); |
| 1523 | delete_node(next_line); |
| 1524 | |
| 1525 | /* If we've removed the next line, we need to go through |
| 1526 | * this line again. */ |
| 1527 | i--; |
| 1528 | |
| 1529 | par_len--; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1530 | openfile->totsize--; |
| 1531 | } |
| 1532 | |
| 1533 | /* Call justify_format() on the paragraph, which will remove |
| 1534 | * excess spaces from it and change all blank characters to |
| 1535 | * spaces. */ |
| 1536 | justify_format(openfile->current, quote_len + |
| 1537 | indent_length(openfile->current->data + quote_len)); |
| 1538 | |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1539 | while (par_len > 0 && strlenpt(openfile->current->data) > |
| 1540 | fill) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1541 | size_t line_len = strlen(openfile->current->data); |
| 1542 | |
| 1543 | indent_len = strlen(indent_string); |
| 1544 | |
| 1545 | /* If this line is too long, try to wrap it to the next line |
| 1546 | * to make it short enough. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1547 | break_pos = break_line(openfile->current->data + indent_len, |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1548 | fill - strnlenpt(openfile->current->data, indent_len) |
| 1549 | #ifndef DISABLE_HELP |
| 1550 | , FALSE |
| 1551 | #endif |
| 1552 | ); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1553 | |
| 1554 | /* We can't break the line, or don't need to, so get out. */ |
| 1555 | if (break_pos == -1 || break_pos + indent_len == line_len) |
| 1556 | break; |
| 1557 | |
| 1558 | /* Move forward to the character after the indentation and |
| 1559 | * just after the space. */ |
| 1560 | break_pos += indent_len + 1; |
| 1561 | |
| 1562 | assert(break_pos <= line_len); |
| 1563 | |
| 1564 | /* Make a new line, and copy the text after where we're |
| 1565 | * going to break this line to the beginning of the new |
| 1566 | * line. */ |
| 1567 | splice_node(openfile->current, |
| 1568 | make_new_node(openfile->current), |
| 1569 | openfile->current->next); |
| 1570 | |
| 1571 | /* If this paragraph is non-quoted, and autoindent isn't |
| 1572 | * turned on, set the indentation length to zero so that the |
| 1573 | * indentation is treated as part of the line. */ |
| 1574 | if (quote_len == 0 |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1575 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1576 | && !ISSET(AUTOINDENT) |
| 1577 | #endif |
| 1578 | ) |
| 1579 | indent_len = 0; |
| 1580 | |
| 1581 | /* Copy the text after where we're going to break the |
| 1582 | * current line to the next line. */ |
| 1583 | openfile->current->next->data = charalloc(indent_len + 1 + |
| 1584 | line_len - break_pos); |
| 1585 | strncpy(openfile->current->next->data, indent_string, |
| 1586 | indent_len); |
| 1587 | strcpy(openfile->current->next->data + indent_len, |
| 1588 | openfile->current->data + break_pos); |
| 1589 | |
| 1590 | par_len++; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1591 | openfile->totsize += indent_len + 1; |
| 1592 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1593 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1594 | /* Adjust the mark coordinates to compensate for the change |
| 1595 | * in the current line. */ |
| 1596 | if (openfile->mark_set && openfile->mark_begin == |
| 1597 | openfile->current && openfile->mark_begin_x > |
| 1598 | break_pos) { |
| 1599 | openfile->mark_begin = openfile->current->next; |
| 1600 | openfile->mark_begin_x -= break_pos - indent_len; |
| 1601 | } |
| 1602 | #endif |
| 1603 | |
| 1604 | /* Break the current line. */ |
| 1605 | null_at(&openfile->current->data, break_pos); |
| 1606 | |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 1607 | /* 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] | 1608 | * the last line of the file down to the next line. */ |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 1609 | if (openfile->filebot == openfile->current) |
| 1610 | openfile->filebot = openfile->filebot->next; |
| 1611 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1612 | /* Go to the next line. */ |
| 1613 | par_len--; |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 1614 | openfile->current_y++; |
| 1615 | openfile->current = openfile->current->next; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* We're done breaking lines, so we don't need indent_string |
| 1619 | * anymore. */ |
| 1620 | free(indent_string); |
| 1621 | |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 1622 | /* Go to the next line, if possible. If there is no next line, |
| 1623 | * move to the end of the current line. */ |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 1624 | if (openfile->current != openfile->filebot) { |
| 1625 | openfile->current_y++; |
| 1626 | openfile->current = openfile->current->next; |
| 1627 | } else |
| 1628 | openfile->current_x = strlen(openfile->current->data); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1629 | |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 1630 | /* Renumber the lines of the now-justified current paragraph, |
| 1631 | * since both find_paragraph() and edit_refresh() need the line |
| 1632 | * numbers to be right. */ |
| 1633 | renumber(curr_first_par_line); |
David Lawrence Ramsey | ad1b64c | 2005-11-29 19:00:09 +0000 | [diff] [blame] | 1634 | |
| 1635 | /* We've just finished justifying the paragraph. If we're not |
| 1636 | * justifying the entire file, break out of the loop. |
| 1637 | * Otherwise, continue the loop so that we justify all the |
| 1638 | * paragraphs in the file. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1639 | if (!full_justify) |
| 1640 | break; |
| 1641 | } |
| 1642 | |
| 1643 | /* 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] | 1644 | * up. current_y and totsize have been maintained above. If we |
David Lawrence Ramsey | ad1b64c | 2005-11-29 19:00:09 +0000 | [diff] [blame] | 1645 | * actually justified something, set last_par_line to the new end of |
| 1646 | * the paragraph. */ |
| 1647 | if (first_par_line != NULL) |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 1648 | last_par_line = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1649 | |
| 1650 | edit_refresh(); |
| 1651 | |
David Lawrence Ramsey | fc0f8f8 | 2006-05-10 13:41:53 +0000 | [diff] [blame] | 1652 | #ifndef NANO_TINY |
David Lawrence Ramsey | 1c5af64 | 2006-05-10 15:15:06 +0000 | [diff] [blame] | 1653 | /* We're going to set jump_buf so that we return here after a |
| 1654 | * SIGWINCH instead of to main(). Indicate this. */ |
| 1655 | jump_buf_main = FALSE; |
| 1656 | |
David Lawrence Ramsey | fc0f8f8 | 2006-05-10 13:41:53 +0000 | [diff] [blame] | 1657 | /* Return here after a SIGWINCH. */ |
David Lawrence Ramsey | 1c5af64 | 2006-05-10 15:15:06 +0000 | [diff] [blame] | 1658 | sigsetjmp(jump_buf, 1); |
David Lawrence Ramsey | fc0f8f8 | 2006-05-10 13:41:53 +0000 | [diff] [blame] | 1659 | #endif |
| 1660 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1661 | statusbar(_("Can now UnJustify!")); |
| 1662 | |
| 1663 | /* If constant cursor position display is on, make sure the current |
| 1664 | * cursor position will be properly displayed on the statusbar. */ |
| 1665 | if (ISSET(CONST_UPDATE)) |
| 1666 | do_cursorpos(TRUE); |
| 1667 | |
| 1668 | /* Display the shortcut list with UnJustify. */ |
| 1669 | shortcut_init(TRUE); |
| 1670 | display_main_list(); |
| 1671 | |
| 1672 | /* Now get a keystroke and see if it's unjustify. If not, put back |
| 1673 | * the keystroke and return. */ |
| 1674 | kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func, |
| 1675 | &finished, FALSE); |
| 1676 | |
David Lawrence Ramsey | b22c80a | 2006-05-06 13:41:59 +0000 | [diff] [blame] | 1677 | if (s_or_t && kbinput == NANO_UNJUSTIFY_KEY) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1678 | /* Splice the justify buffer back into the file, but only if we |
| 1679 | * actually justified something. */ |
| 1680 | if (first_par_line != NULL) { |
| 1681 | filestruct *top_save; |
| 1682 | |
| 1683 | /* Partition the filestruct so that it contains only the |
| 1684 | * text of the justified paragraph. */ |
| 1685 | filepart = partition_filestruct(first_par_line, 0, |
David Lawrence Ramsey | ccd1b7b | 2005-11-18 20:21:48 +0000 | [diff] [blame] | 1686 | last_par_line, filebot_inpar ? |
| 1687 | strlen(last_par_line->data) : 0); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1688 | |
| 1689 | /* Remove the text of the justified paragraph, and |
David Lawrence Ramsey | af5a999 | 2005-11-09 23:06:44 +0000 | [diff] [blame] | 1690 | * replace it with the text in the justify buffer. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1691 | free_filestruct(openfile->fileage); |
| 1692 | openfile->fileage = jusbuffer; |
| 1693 | openfile->filebot = jusbottom; |
| 1694 | |
| 1695 | top_save = openfile->fileage; |
| 1696 | |
| 1697 | /* Unpartition the filestruct so that it contains all the |
| 1698 | * text again. Note that the justified paragraph has been |
| 1699 | * replaced with the unjustified paragraph. */ |
| 1700 | unpartition_filestruct(&filepart); |
| 1701 | |
| 1702 | /* Renumber starting with the beginning line of the old |
| 1703 | * partition. */ |
| 1704 | renumber(top_save); |
| 1705 | |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 1706 | /* Restore the justify we just did (ungrateful user!). */ |
| 1707 | openfile->edittop = edittop_save; |
| 1708 | openfile->current = current_save; |
| 1709 | openfile->current_x = current_x_save; |
| 1710 | openfile->placewewant = pww_save; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1711 | openfile->totsize = totsize_save; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1712 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1713 | if (openfile->mark_set) { |
| 1714 | openfile->mark_begin = mark_begin_save; |
| 1715 | openfile->mark_begin_x = mark_begin_x_save; |
| 1716 | } |
| 1717 | #endif |
| 1718 | openfile->modified = modified_save; |
| 1719 | |
| 1720 | /* Clear the justify buffer. */ |
| 1721 | jusbuffer = NULL; |
| 1722 | |
| 1723 | if (!openfile->modified) |
| 1724 | titlebar(NULL); |
| 1725 | edit_refresh(); |
| 1726 | } |
| 1727 | } else { |
| 1728 | unget_kbinput(kbinput, meta_key, func_key); |
| 1729 | |
| 1730 | /* Blow away the text in the justify buffer. */ |
| 1731 | free_filestruct(jusbuffer); |
| 1732 | jusbuffer = NULL; |
| 1733 | } |
| 1734 | |
| 1735 | blank_statusbar(); |
| 1736 | |
| 1737 | /* Display the shortcut list with UnCut. */ |
| 1738 | shortcut_init(FALSE); |
| 1739 | display_main_list(); |
| 1740 | } |
| 1741 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1742 | /* Justify the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1743 | void do_justify_void(void) |
| 1744 | { |
| 1745 | do_justify(FALSE); |
| 1746 | } |
| 1747 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1748 | /* Justify the entire file. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1749 | void do_full_justify(void) |
| 1750 | { |
| 1751 | do_justify(TRUE); |
| 1752 | } |
| 1753 | #endif /* !DISABLE_JUSTIFY */ |
| 1754 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1755 | #ifndef DISABLE_SPELLER |
| 1756 | /* A word is misspelled in the file. Let the user replace it. We |
| 1757 | * return FALSE if the user cancels. */ |
| 1758 | bool do_int_spell_fix(const char *word) |
| 1759 | { |
| 1760 | char *save_search, *save_replace; |
| 1761 | size_t match_len, current_x_save = openfile->current_x; |
| 1762 | size_t pww_save = openfile->placewewant; |
| 1763 | filestruct *edittop_save = openfile->edittop; |
| 1764 | filestruct *current_save = openfile->current; |
| 1765 | /* Save where we are. */ |
| 1766 | bool canceled = FALSE; |
| 1767 | /* The return value. */ |
| 1768 | bool case_sens_set = ISSET(CASE_SENSITIVE); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1769 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1770 | bool backwards_search_set = ISSET(BACKWARDS_SEARCH); |
| 1771 | #endif |
| 1772 | #ifdef HAVE_REGEX_H |
| 1773 | bool regexp_set = ISSET(USE_REGEXP); |
| 1774 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1775 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1776 | bool old_mark_set = openfile->mark_set; |
| 1777 | bool added_magicline = FALSE; |
| 1778 | /* Whether we added a magicline after filebot. */ |
| 1779 | bool right_side_up = FALSE; |
| 1780 | /* TRUE if (mark_begin, mark_begin_x) is the top of the mark, |
| 1781 | * FALSE if (current, current_x) is. */ |
| 1782 | filestruct *top, *bot; |
| 1783 | size_t top_x, bot_x; |
| 1784 | #endif |
| 1785 | |
| 1786 | /* Make sure spell-check is case sensitive. */ |
| 1787 | SET(CASE_SENSITIVE); |
| 1788 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1789 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1790 | /* Make sure spell-check goes forward only. */ |
| 1791 | UNSET(BACKWARDS_SEARCH); |
| 1792 | #endif |
| 1793 | #ifdef HAVE_REGEX_H |
| 1794 | /* Make sure spell-check doesn't use regular expressions. */ |
| 1795 | UNSET(USE_REGEXP); |
| 1796 | #endif |
| 1797 | |
| 1798 | /* Save the current search/replace strings. */ |
| 1799 | search_init_globals(); |
| 1800 | save_search = last_search; |
| 1801 | save_replace = last_replace; |
| 1802 | |
| 1803 | /* Set the search/replace strings to the misspelled word. */ |
| 1804 | last_search = mallocstrcpy(NULL, word); |
| 1805 | last_replace = mallocstrcpy(NULL, word); |
| 1806 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1807 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1808 | if (old_mark_set) { |
| 1809 | /* If the mark is on, partition the filestruct so that it |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 1810 | * contains only the marked text; if the NO_NEWLINES flag isn't |
| 1811 | * set, keep track of whether the text will have a magicline |
| 1812 | * added when we're done correcting misspelled words; and |
| 1813 | * turn the mark off. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1814 | mark_order((const filestruct **)&top, &top_x, |
| 1815 | (const filestruct **)&bot, &bot_x, &right_side_up); |
| 1816 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 1817 | if (!ISSET(NO_NEWLINES)) |
| 1818 | added_magicline = (openfile->filebot->data[0] != '\0'); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1819 | openfile->mark_set = FALSE; |
| 1820 | } |
| 1821 | #endif |
| 1822 | |
| 1823 | /* Start from the top of the file. */ |
| 1824 | openfile->edittop = openfile->fileage; |
| 1825 | openfile->current = openfile->fileage; |
| 1826 | openfile->current_x = (size_t)-1; |
| 1827 | openfile->placewewant = 0; |
| 1828 | |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 1829 | /* Find the first whole occurrence of word. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1830 | findnextstr_wrap_reset(); |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 1831 | while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word, |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1832 | &match_len)) { |
| 1833 | if (is_whole_word(openfile->current_x, openfile->current->data, |
| 1834 | word)) { |
| 1835 | size_t xpt = xplustabs(); |
| 1836 | char *exp_word = display_string(openfile->current->data, |
| 1837 | xpt, strnlenpt(openfile->current->data, |
| 1838 | openfile->current_x + match_len) - xpt, FALSE); |
| 1839 | |
| 1840 | edit_refresh(); |
| 1841 | |
| 1842 | do_replace_highlight(TRUE, exp_word); |
| 1843 | |
| 1844 | /* Allow all instances of the word to be corrected. */ |
David Lawrence Ramsey | 9d8c284 | 2006-02-07 21:11:05 +0000 | [diff] [blame] | 1845 | canceled = (do_prompt(FALSE, |
| 1846 | #ifndef DISABLE_TABCOMP |
| 1847 | TRUE, |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1848 | #endif |
David Lawrence Ramsey | 9d8c284 | 2006-02-07 21:11:05 +0000 | [diff] [blame] | 1849 | spell_list, word, |
| 1850 | #ifndef NANO_TINY |
| 1851 | NULL, |
| 1852 | #endif |
David Lawrence Ramsey | 6816007 | 2006-02-18 21:32:29 +0000 | [diff] [blame] | 1853 | edit_refresh, _("Edit a replacement")) == -1); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1854 | |
| 1855 | do_replace_highlight(FALSE, exp_word); |
| 1856 | |
| 1857 | free(exp_word); |
| 1858 | |
| 1859 | if (!canceled && strcmp(word, answer) != 0) { |
| 1860 | openfile->current_x--; |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 1861 | do_replace_loop(TRUE, &canceled, openfile->current, |
| 1862 | &openfile->current_x, word); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | break; |
| 1866 | } |
| 1867 | } |
| 1868 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1869 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1870 | if (old_mark_set) { |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 1871 | /* If the mark was on, the NO_NEWLINES flag isn't set, and we |
| 1872 | * added a magicline, remove it now. */ |
| 1873 | if (!ISSET(NO_NEWLINES) && added_magicline) |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1874 | remove_magicline(); |
| 1875 | |
| 1876 | /* Put the beginning and the end of the mark at the beginning |
| 1877 | * and the end of the spell-checked text. */ |
| 1878 | if (openfile->fileage == openfile->filebot) |
| 1879 | bot_x += top_x; |
| 1880 | if (right_side_up) { |
| 1881 | openfile->mark_begin_x = top_x; |
| 1882 | current_x_save = bot_x; |
| 1883 | } else { |
| 1884 | current_x_save = top_x; |
| 1885 | openfile->mark_begin_x = bot_x; |
| 1886 | } |
| 1887 | |
| 1888 | /* Unpartition the filestruct so that it contains all the text |
| 1889 | * again, and turn the mark back on. */ |
| 1890 | unpartition_filestruct(&filepart); |
| 1891 | openfile->mark_set = TRUE; |
| 1892 | } |
| 1893 | #endif |
| 1894 | |
| 1895 | /* Restore the search/replace strings. */ |
| 1896 | free(last_search); |
| 1897 | last_search = save_search; |
| 1898 | free(last_replace); |
| 1899 | last_replace = save_replace; |
| 1900 | |
| 1901 | /* Restore where we were. */ |
| 1902 | openfile->edittop = edittop_save; |
| 1903 | openfile->current = current_save; |
| 1904 | openfile->current_x = current_x_save; |
| 1905 | openfile->placewewant = pww_save; |
| 1906 | |
| 1907 | /* Restore case sensitivity setting. */ |
| 1908 | if (!case_sens_set) |
| 1909 | UNSET(CASE_SENSITIVE); |
| 1910 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1911 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1912 | /* Restore search/replace direction. */ |
| 1913 | if (backwards_search_set) |
| 1914 | SET(BACKWARDS_SEARCH); |
| 1915 | #endif |
| 1916 | #ifdef HAVE_REGEX_H |
| 1917 | /* Restore regular expression usage setting. */ |
| 1918 | if (regexp_set) |
| 1919 | SET(USE_REGEXP); |
| 1920 | #endif |
| 1921 | |
| 1922 | return !canceled; |
| 1923 | } |
| 1924 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1925 | /* Internal (integrated) spell checking using the spell program, |
| 1926 | * filtered through the sort and uniq programs. Return NULL for normal |
| 1927 | * termination, and the error string otherwise. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1928 | const char *do_int_speller(const char *tempfile_name) |
| 1929 | { |
| 1930 | char *read_buff, *read_buff_ptr, *read_buff_word; |
| 1931 | size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread; |
| 1932 | int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1; |
| 1933 | pid_t pid_spell, pid_sort, pid_uniq; |
| 1934 | int spell_status, sort_status, uniq_status; |
| 1935 | |
| 1936 | /* Create all three pipes up front. */ |
| 1937 | if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || |
| 1938 | pipe(uniq_fd) == -1) |
| 1939 | return _("Could not create pipe"); |
| 1940 | |
| 1941 | statusbar(_("Creating misspelled word list, please wait...")); |
| 1942 | |
| 1943 | /* A new process to run spell in. */ |
| 1944 | if ((pid_spell = fork()) == 0) { |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 1945 | /* Child continues (i.e. future spell process). */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1946 | close(spell_fd[0]); |
| 1947 | |
| 1948 | /* Replace the standard input with the temp file. */ |
| 1949 | if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1) |
| 1950 | goto close_pipes_and_exit; |
| 1951 | |
| 1952 | if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) |
| 1953 | goto close_pipes_and_exit; |
| 1954 | |
| 1955 | close(tempfile_fd); |
| 1956 | |
| 1957 | /* Send spell's standard output to the pipe. */ |
| 1958 | if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 1959 | goto close_pipes_and_exit; |
| 1960 | |
| 1961 | close(spell_fd[1]); |
| 1962 | |
David Lawrence Ramsey | 3239ff2 | 2005-11-29 20:01:06 +0000 | [diff] [blame] | 1963 | /* Start the spell program; we are using $PATH. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1964 | execlp("spell", "spell", NULL); |
| 1965 | |
| 1966 | /* This should not be reached if spell is found. */ |
| 1967 | exit(1); |
| 1968 | } |
| 1969 | |
| 1970 | /* Parent continues here. */ |
| 1971 | close(spell_fd[1]); |
| 1972 | |
| 1973 | /* A new process to run sort in. */ |
| 1974 | if ((pid_sort = fork()) == 0) { |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 1975 | /* Child continues (i.e. future spell process). Replace the |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 1976 | * standard input with the standard output of the old pipe. */ |
| 1977 | if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 1978 | goto close_pipes_and_exit; |
| 1979 | |
| 1980 | close(spell_fd[0]); |
| 1981 | |
| 1982 | /* Send sort's standard output to the new pipe. */ |
| 1983 | if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 1984 | goto close_pipes_and_exit; |
| 1985 | |
| 1986 | close(sort_fd[1]); |
| 1987 | |
| 1988 | /* Start the sort program. Use -f to remove mixed case. If |
| 1989 | * this isn't portable, let me know. */ |
| 1990 | execlp("sort", "sort", "-f", NULL); |
| 1991 | |
| 1992 | /* This should not be reached if sort is found. */ |
| 1993 | exit(1); |
| 1994 | } |
| 1995 | |
| 1996 | close(spell_fd[0]); |
| 1997 | close(sort_fd[1]); |
| 1998 | |
| 1999 | /* A new process to run uniq in. */ |
| 2000 | if ((pid_uniq = fork()) == 0) { |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 2001 | /* Child continues (i.e. future uniq process). Replace the |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2002 | * standard input with the standard output of the old pipe. */ |
| 2003 | if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 2004 | goto close_pipes_and_exit; |
| 2005 | |
| 2006 | close(sort_fd[0]); |
| 2007 | |
| 2008 | /* Send uniq's standard output to the new pipe. */ |
| 2009 | if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 2010 | goto close_pipes_and_exit; |
| 2011 | |
| 2012 | close(uniq_fd[1]); |
| 2013 | |
| 2014 | /* Start the uniq program; we are using PATH. */ |
| 2015 | execlp("uniq", "uniq", NULL); |
| 2016 | |
| 2017 | /* This should not be reached if uniq is found. */ |
| 2018 | exit(1); |
| 2019 | } |
| 2020 | |
| 2021 | close(sort_fd[0]); |
| 2022 | close(uniq_fd[1]); |
| 2023 | |
| 2024 | /* The child process was not forked successfully. */ |
| 2025 | if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) { |
| 2026 | close(uniq_fd[0]); |
| 2027 | return _("Could not fork"); |
| 2028 | } |
| 2029 | |
| 2030 | /* Get the system pipe buffer size. */ |
| 2031 | if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) { |
| 2032 | close(uniq_fd[0]); |
| 2033 | return _("Could not get size of pipe buffer"); |
| 2034 | } |
| 2035 | |
| 2036 | /* Read in the returned spelling errors. */ |
| 2037 | read_buff_read = 0; |
| 2038 | read_buff_size = pipe_buff_size + 1; |
| 2039 | read_buff = read_buff_ptr = charalloc(read_buff_size); |
| 2040 | |
| 2041 | while ((bytesread = read(uniq_fd[0], read_buff_ptr, |
| 2042 | pipe_buff_size)) > 0) { |
| 2043 | read_buff_read += bytesread; |
| 2044 | read_buff_size += pipe_buff_size; |
| 2045 | read_buff = read_buff_ptr = charealloc(read_buff, |
| 2046 | read_buff_size); |
| 2047 | read_buff_ptr += read_buff_read; |
| 2048 | } |
| 2049 | |
| 2050 | *read_buff_ptr = '\0'; |
| 2051 | close(uniq_fd[0]); |
| 2052 | |
| 2053 | /* Process the spelling errors. */ |
| 2054 | read_buff_word = read_buff_ptr = read_buff; |
| 2055 | |
| 2056 | while (*read_buff_ptr != '\0') { |
| 2057 | if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) { |
| 2058 | *read_buff_ptr = '\0'; |
| 2059 | if (read_buff_word != read_buff_ptr) { |
| 2060 | if (!do_int_spell_fix(read_buff_word)) { |
| 2061 | read_buff_word = read_buff_ptr; |
| 2062 | break; |
| 2063 | } |
| 2064 | } |
| 2065 | read_buff_word = read_buff_ptr + 1; |
| 2066 | } |
| 2067 | read_buff_ptr++; |
| 2068 | } |
| 2069 | |
| 2070 | /* Special case: the last word doesn't end with '\r' or '\n'. */ |
| 2071 | if (read_buff_word != read_buff_ptr) |
| 2072 | do_int_spell_fix(read_buff_word); |
| 2073 | |
| 2074 | free(read_buff); |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2075 | search_replace_abort(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2076 | edit_refresh(); |
| 2077 | |
| 2078 | /* Process the end of the spell process. */ |
| 2079 | waitpid(pid_spell, &spell_status, 0); |
| 2080 | waitpid(pid_sort, &sort_status, 0); |
| 2081 | waitpid(pid_uniq, &uniq_status, 0); |
| 2082 | |
| 2083 | if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status)) |
| 2084 | return _("Error invoking \"spell\""); |
| 2085 | |
| 2086 | if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status)) |
| 2087 | return _("Error invoking \"sort -f\""); |
| 2088 | |
| 2089 | if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status)) |
| 2090 | return _("Error invoking \"uniq\""); |
| 2091 | |
| 2092 | /* Otherwise... */ |
| 2093 | return NULL; |
| 2094 | |
| 2095 | close_pipes_and_exit: |
| 2096 | /* Don't leak any handles. */ |
| 2097 | close(tempfile_fd); |
| 2098 | close(spell_fd[0]); |
| 2099 | close(spell_fd[1]); |
| 2100 | close(sort_fd[0]); |
| 2101 | close(sort_fd[1]); |
| 2102 | close(uniq_fd[0]); |
| 2103 | close(uniq_fd[1]); |
| 2104 | exit(1); |
| 2105 | } |
| 2106 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2107 | /* External (alternate) spell checking. Return NULL for normal |
| 2108 | * termination, and the error string otherwise. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2109 | const char *do_alt_speller(char *tempfile_name) |
| 2110 | { |
| 2111 | int alt_spell_status; |
| 2112 | size_t current_x_save = openfile->current_x; |
| 2113 | size_t pww_save = openfile->placewewant; |
| 2114 | ssize_t current_y_save = openfile->current_y; |
| 2115 | ssize_t lineno_save = openfile->current->lineno; |
| 2116 | pid_t pid_spell; |
| 2117 | char *ptr; |
| 2118 | static int arglen = 3; |
| 2119 | static char **spellargs = NULL; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2120 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2121 | bool old_mark_set = openfile->mark_set; |
| 2122 | bool added_magicline = FALSE; |
| 2123 | /* Whether we added a magicline after filebot. */ |
| 2124 | bool right_side_up = FALSE; |
| 2125 | /* TRUE if (mark_begin, mark_begin_x) is the top of the mark, |
| 2126 | * FALSE if (current, current_x) is. */ |
| 2127 | filestruct *top, *bot; |
| 2128 | size_t top_x, bot_x; |
| 2129 | ssize_t mb_lineno_save = 0; |
| 2130 | /* We're going to close the current file, and open the output of |
| 2131 | * the alternate spell command. The line that mark_begin points |
| 2132 | * to will be freed, so we save the line number and restore it |
| 2133 | * afterwards. */ |
| 2134 | size_t totsize_save = openfile->totsize; |
| 2135 | /* Our saved value of totsize, used when we spell-check a marked |
| 2136 | * selection. */ |
| 2137 | |
| 2138 | if (old_mark_set) { |
| 2139 | /* If the mark is on, save the number of the line it starts on, |
| 2140 | * and then turn the mark off. */ |
| 2141 | mb_lineno_save = openfile->mark_begin->lineno; |
| 2142 | openfile->mark_set = FALSE; |
| 2143 | } |
| 2144 | #endif |
| 2145 | |
| 2146 | endwin(); |
| 2147 | |
| 2148 | /* Set up an argument list to pass execvp(). */ |
| 2149 | if (spellargs == NULL) { |
| 2150 | spellargs = (char **)nmalloc(arglen * sizeof(char *)); |
| 2151 | |
| 2152 | spellargs[0] = strtok(alt_speller, " "); |
| 2153 | while ((ptr = strtok(NULL, " ")) != NULL) { |
| 2154 | arglen++; |
| 2155 | spellargs = (char **)nrealloc(spellargs, arglen * |
| 2156 | sizeof(char *)); |
| 2157 | spellargs[arglen - 3] = ptr; |
| 2158 | } |
| 2159 | spellargs[arglen - 1] = NULL; |
| 2160 | } |
| 2161 | spellargs[arglen - 2] = tempfile_name; |
| 2162 | |
| 2163 | /* Start a new process for the alternate speller. */ |
| 2164 | if ((pid_spell = fork()) == 0) { |
David Lawrence Ramsey | 2e2112c | 2005-11-29 05:39:31 +0000 | [diff] [blame] | 2165 | /* Start alternate spell program; we are using $PATH. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2166 | execvp(spellargs[0], spellargs); |
| 2167 | |
| 2168 | /* Should not be reached, if alternate speller is found!!! */ |
| 2169 | exit(1); |
| 2170 | } |
| 2171 | |
| 2172 | /* If we couldn't fork, get out. */ |
| 2173 | if (pid_spell < 0) |
| 2174 | return _("Could not fork"); |
| 2175 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2176 | #ifndef NANO_TINY |
David Lawrence Ramsey | b18482e | 2005-07-26 00:06:34 +0000 | [diff] [blame] | 2177 | /* Don't handle a pending SIGWINCH until the alternate spell checker |
David Lawrence Ramsey | 3fe08ac | 2005-07-26 01:17:16 +0000 | [diff] [blame] | 2178 | * 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] | 2179 | allow_pending_sigwinch(FALSE); |
| 2180 | #endif |
| 2181 | |
| 2182 | /* Wait for the alternate spell checker to finish. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2183 | wait(&alt_spell_status); |
| 2184 | |
David Lawrence Ramsey | 84fdb90 | 2005-08-14 20:08:49 +0000 | [diff] [blame] | 2185 | /* Reenter curses mode. */ |
| 2186 | doupdate(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2187 | |
| 2188 | /* Restore the terminal to its previous state. */ |
| 2189 | terminal_init(); |
| 2190 | |
| 2191 | /* Turn the cursor back on for sure. */ |
| 2192 | curs_set(1); |
| 2193 | |
David Lawrence Ramsey | 3fe08ac | 2005-07-26 01:17:16 +0000 | [diff] [blame] | 2194 | /* The screen might have been resized. If it has, reinitialize all |
| 2195 | * the windows based on the new screen dimensions. */ |
| 2196 | window_init(); |
| 2197 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2198 | if (!WIFEXITED(alt_spell_status) || |
| 2199 | WEXITSTATUS(alt_spell_status) != 0) { |
| 2200 | char *altspell_error; |
| 2201 | char *invoke_error = _("Error invoking \"%s\""); |
| 2202 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2203 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2204 | /* Turn the mark back on if it was on before. */ |
| 2205 | openfile->mark_set = old_mark_set; |
| 2206 | #endif |
| 2207 | |
| 2208 | altspell_error = |
| 2209 | charalloc(strlen(invoke_error) + |
| 2210 | strlen(alt_speller) + 1); |
| 2211 | sprintf(altspell_error, invoke_error, alt_speller); |
| 2212 | return altspell_error; |
| 2213 | } |
| 2214 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2215 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2216 | if (old_mark_set) { |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2217 | /* If the mark is on, partition the filestruct so that it |
| 2218 | * contains only the marked text; if the NO_NEWLINES flag isn't |
| 2219 | * set, keep track of whether the text will have a magicline |
| 2220 | * added when we're done correcting misspelled words; and |
| 2221 | * turn the mark off. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2222 | mark_order((const filestruct **)&top, &top_x, |
| 2223 | (const filestruct **)&bot, &bot_x, &right_side_up); |
| 2224 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2225 | if (!ISSET(NO_NEWLINES)) |
| 2226 | added_magicline = (openfile->filebot->data[0] != '\0'); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2227 | |
| 2228 | /* Get the number of characters in the marked text, and subtract |
| 2229 | * it from the saved value of totsize. */ |
| 2230 | totsize_save -= get_totsize(top, bot); |
| 2231 | } |
| 2232 | #endif |
| 2233 | |
David Lawrence Ramsey | 9c984e8 | 2005-11-08 19:15:58 +0000 | [diff] [blame] | 2234 | /* Replace the text of the current buffer with the spell-checked |
| 2235 | * text. */ |
| 2236 | replace_buffer(tempfile_name); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2237 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2238 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2239 | if (old_mark_set) { |
| 2240 | filestruct *top_save = openfile->fileage; |
| 2241 | |
David Lawrence Ramsey | 1e0e235 | 2005-11-08 18:34:12 +0000 | [diff] [blame] | 2242 | /* If the mark was on, the NO_NEWLINES flag isn't set, and we |
| 2243 | * added a magicline, remove it now. */ |
| 2244 | if (!ISSET(NO_NEWLINES) && added_magicline) |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2245 | remove_magicline(); |
| 2246 | |
| 2247 | /* Put the beginning and the end of the mark at the beginning |
| 2248 | * and the end of the spell-checked text. */ |
| 2249 | if (openfile->fileage == openfile->filebot) |
| 2250 | bot_x += top_x; |
| 2251 | if (right_side_up) { |
| 2252 | openfile->mark_begin_x = top_x; |
| 2253 | current_x_save = bot_x; |
| 2254 | } else { |
| 2255 | current_x_save = top_x; |
| 2256 | openfile->mark_begin_x = bot_x; |
| 2257 | } |
| 2258 | |
| 2259 | /* Unpartition the filestruct so that it contains all the text |
| 2260 | * again. Note that we've replaced the marked text originally |
| 2261 | * in the partition with the spell-checked marked text in the |
| 2262 | * temp file. */ |
| 2263 | unpartition_filestruct(&filepart); |
| 2264 | |
| 2265 | /* Renumber starting with the beginning line of the old |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 2266 | * partition. Also add the number of characters in the |
| 2267 | * spell-checked marked text to the saved value of totsize, and |
| 2268 | * then make that saved value the actual value. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2269 | renumber(top_save); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2270 | totsize_save += openfile->totsize; |
| 2271 | openfile->totsize = totsize_save; |
| 2272 | |
| 2273 | /* Assign mark_begin to the line where the mark began before. */ |
| 2274 | do_gotopos(mb_lineno_save, openfile->mark_begin_x, |
| 2275 | current_y_save, 0); |
| 2276 | openfile->mark_begin = openfile->current; |
| 2277 | |
| 2278 | /* Assign mark_begin_x to the location in mark_begin where the |
| 2279 | * mark began before, adjusted for any shortening of the |
| 2280 | * line. */ |
| 2281 | openfile->mark_begin_x = openfile->current_x; |
| 2282 | |
| 2283 | /* Turn the mark back on. */ |
| 2284 | openfile->mark_set = TRUE; |
| 2285 | } |
| 2286 | #endif |
| 2287 | |
| 2288 | /* Go back to the old position, and mark the file as modified. */ |
| 2289 | do_gotopos(lineno_save, current_x_save, current_y_save, pww_save); |
| 2290 | set_modified(); |
| 2291 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2292 | #ifndef NANO_TINY |
David Lawrence Ramsey | 3fe08ac | 2005-07-26 01:17:16 +0000 | [diff] [blame] | 2293 | /* Handle a pending SIGWINCH again. */ |
| 2294 | allow_pending_sigwinch(TRUE); |
| 2295 | #endif |
| 2296 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2297 | return NULL; |
| 2298 | } |
| 2299 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2300 | /* Spell check the current file. If an alternate spell checker is |
| 2301 | * specified, use it. Otherwise, use the internal spell checker. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2302 | void do_spell(void) |
| 2303 | { |
| 2304 | int i; |
| 2305 | FILE *temp_file; |
| 2306 | char *temp = safe_tempfile(&temp_file); |
| 2307 | const char *spell_msg; |
| 2308 | |
| 2309 | if (temp == NULL) { |
David Lawrence Ramsey | f0e3ca6 | 2006-05-03 13:11:00 +0000 | [diff] [blame] | 2310 | statusbar(_("Error writing temp file: %s"), strerror(errno)); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2311 | return; |
| 2312 | } |
| 2313 | |
David Lawrence Ramsey | b6c4dbf | 2006-11-25 22:38:17 +0000 | [diff] [blame] | 2314 | i = |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2315 | #ifndef NANO_TINY |
David Lawrence Ramsey | b6c4dbf | 2006-11-25 22:38:17 +0000 | [diff] [blame] | 2316 | (openfile->mark_set) ? write_marked_file(temp, temp_file, TRUE, |
| 2317 | OVERWRITE) : |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2318 | #endif |
David Lawrence Ramsey | b6c4dbf | 2006-11-25 22:38:17 +0000 | [diff] [blame] | 2319 | write_file(temp, temp_file, TRUE, OVERWRITE, FALSE); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2320 | |
| 2321 | if (i == -1) { |
| 2322 | statusbar(_("Error writing temp file: %s"), strerror(errno)); |
| 2323 | free(temp); |
| 2324 | return; |
| 2325 | } |
| 2326 | |
| 2327 | spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) : |
| 2328 | do_int_speller(temp); |
| 2329 | unlink(temp); |
| 2330 | free(temp); |
| 2331 | |
David Lawrence Ramsey | f32e1dd | 2006-06-09 17:09:51 +0000 | [diff] [blame] | 2332 | currshortcut = main_list; |
| 2333 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2334 | /* If the spell-checker printed any error messages onscreen, make |
| 2335 | * sure that they're cleared off. */ |
David Lawrence Ramsey | f32e1dd | 2006-06-09 17:09:51 +0000 | [diff] [blame] | 2336 | total_refresh(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2337 | |
| 2338 | if (spell_msg != NULL) { |
| 2339 | if (errno == 0) |
| 2340 | /* Don't display an error message of "Success". */ |
| 2341 | statusbar(_("Spell checking failed: %s"), spell_msg); |
| 2342 | else |
| 2343 | statusbar(_("Spell checking failed: %s: %s"), spell_msg, |
| 2344 | strerror(errno)); |
| 2345 | } else |
| 2346 | statusbar(_("Finished checking spelling")); |
| 2347 | } |
| 2348 | #endif /* !DISABLE_SPELLER */ |
| 2349 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2350 | #ifndef NANO_TINY |
David Lawrence Ramsey | d7f0fe9 | 2005-08-10 22:51:49 +0000 | [diff] [blame] | 2351 | /* Our own version of "wc". Note that its character counts are in |
| 2352 | * multibyte characters instead of single-byte characters. */ |
David Lawrence Ramsey | 8e94234 | 2005-07-25 04:21:46 +0000 | [diff] [blame] | 2353 | void do_wordlinechar_count(void) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2354 | { |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 2355 | size_t words = 0, chars = 0; |
| 2356 | ssize_t lines = 0; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2357 | size_t current_x_save = openfile->current_x; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2358 | size_t pww_save = openfile->placewewant; |
| 2359 | filestruct *current_save = openfile->current; |
| 2360 | bool old_mark_set = openfile->mark_set; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2361 | filestruct *top, *bot; |
| 2362 | size_t top_x, bot_x; |
| 2363 | |
| 2364 | if (old_mark_set) { |
| 2365 | /* If the mark is on, partition the filestruct so that it |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 2366 | * contains only the marked text, and turn the mark off. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2367 | mark_order((const filestruct **)&top, &top_x, |
| 2368 | (const filestruct **)&bot, &bot_x, NULL); |
| 2369 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2370 | openfile->mark_set = FALSE; |
| 2371 | } |
| 2372 | |
| 2373 | /* Start at the top of the file. */ |
| 2374 | openfile->current = openfile->fileage; |
| 2375 | openfile->current_x = 0; |
| 2376 | openfile->placewewant = 0; |
| 2377 | |
| 2378 | /* Keep moving to the next word (counting punctuation characters as |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2379 | * part of a word, as "wc -w" does), without updating the screen, |
| 2380 | * until we reach the end of the file, incrementing the total word |
| 2381 | * count whenever we're on a word just before moving. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2382 | while (openfile->current != openfile->filebot || |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 2383 | openfile->current->data[openfile->current_x] != '\0') { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2384 | if (do_next_word(TRUE, FALSE)) |
| 2385 | words++; |
| 2386 | } |
| 2387 | |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2388 | /* Get the total line and character counts, as "wc -l" and "wc -c" |
| 2389 | * do, but get the latter in multibyte characters. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2390 | if (old_mark_set) { |
David Lawrence Ramsey | 78a81b2 | 2005-07-25 18:59:24 +0000 | [diff] [blame] | 2391 | lines = openfile->filebot->lineno - |
| 2392 | openfile->fileage->lineno + 1; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2393 | chars = get_totsize(openfile->fileage, openfile->filebot); |
| 2394 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2395 | /* Unpartition the filestruct so that it contains all the text |
| 2396 | * again, and turn the mark back on. */ |
| 2397 | unpartition_filestruct(&filepart); |
| 2398 | openfile->mark_set = TRUE; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2399 | } else { |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 2400 | lines = openfile->filebot->lineno; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2401 | chars = openfile->totsize; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | /* Restore where we were. */ |
| 2405 | openfile->current = current_save; |
| 2406 | openfile->current_x = current_x_save; |
| 2407 | openfile->placewewant = pww_save; |
| 2408 | |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 2409 | /* Display the total word, line, and character counts on the |
| 2410 | * statusbar. */ |
David Lawrence Ramsey | 7b71f57 | 2005-10-06 20:46:11 +0000 | [diff] [blame] | 2411 | statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ? |
David Lawrence Ramsey | 815fb0a | 2005-08-05 19:38:11 +0000 | [diff] [blame] | 2412 | _("In Selection: ") : "", (unsigned long)words, (long)lines, |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 2413 | (unsigned long)chars); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2414 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2415 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2416 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2417 | /* Get verbatim input. */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2418 | void do_verbatim_input(void) |
| 2419 | { |
| 2420 | int *kbinput; |
| 2421 | size_t kbinput_len, i; |
| 2422 | char *output; |
| 2423 | |
David Lawrence Ramsey | f451d6a | 2006-05-27 16:02:48 +0000 | [diff] [blame] | 2424 | /* TRANSLATORS: This is displayed when the next keystroke will be |
| 2425 | * inserted verbatim. */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2426 | statusbar(_("Verbatim Input")); |
| 2427 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2428 | /* Read in all the verbatim characters. */ |
| 2429 | kbinput = get_verbatim_kbinput(edit, &kbinput_len); |
| 2430 | |
David Lawrence Ramsey | a620e68 | 2006-05-27 18:19:03 +0000 | [diff] [blame] | 2431 | /* If constant cursor position display is on, make sure the current |
| 2432 | * cursor position will be properly displayed on the statusbar. |
| 2433 | * Otherwise, blank the statusbar. */ |
| 2434 | if (ISSET(CONST_UPDATE)) |
| 2435 | do_cursorpos(TRUE); |
| 2436 | else { |
| 2437 | blank_statusbar(); |
| 2438 | wnoutrefresh(bottomwin); |
| 2439 | } |
David Lawrence Ramsey | 6fb6689 | 2006-05-27 17:39:19 +0000 | [diff] [blame] | 2440 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2441 | /* Display all the verbatim characters at once, not filtering out |
| 2442 | * control characters. */ |
| 2443 | output = charalloc(kbinput_len + 1); |
| 2444 | |
| 2445 | for (i = 0; i < kbinput_len; i++) |
| 2446 | output[i] = (char)kbinput[i]; |
| 2447 | output[i] = '\0'; |
| 2448 | |
David Lawrence Ramsey | ad36bdc | 2006-12-02 17:22:21 +0000 | [diff] [blame^] | 2449 | free(kbinput); |
| 2450 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 2451 | do_output(output, kbinput_len, TRUE); |
| 2452 | |
| 2453 | free(output); |
| 2454 | } |