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