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