blob: 6fcd19f2161e3a4ae4e27df741ebfce9f3943e9a [file] [log] [blame]
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001/* $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
36static 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
42static bool same_line_wrap = FALSE;
43 /* Should we prepend wrapped text to the next line? */
44#endif
45#ifndef DISABLE_JUSTIFY
46static filestruct *jusbottom = NULL;
47 /* Pointer to end of justify buffer. */
48#endif
49
50#ifndef NANO_SMALL
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000051void 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
67void 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 Ramsey96452cb2005-07-26 06:13:45 +000079 openfile->current_x, NULL, NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000080 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 Ramsey7ea09e52005-07-25 02:41:59 +0000130 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
152void do_backspace(void)
153{
154 if (openfile->current != openfile->fileage ||
155 openfile->current_x > 0) {
156 do_left(FALSE);
157 do_delete();
158 }
159}
160
161void do_tab(void)
162{
163#ifndef NANO_SMALL
164 if (ISSET(TABS_TO_SPACES)) {
165 char *output;
166 size_t output_len = 0, new_pww = openfile->placewewant;
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 Ramseyfeb89db2005-09-13 04:45:46 +0000189/* Someone hits Enter *gasp!* */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000190void 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 Ramsey7ea09e52005-07-25 02:41:59 +0000237 openfile->totsize++;
238 set_modified();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000239
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000240 openfile->placewewant = xplustabs();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000241
242 edit_refresh();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000243}
244
245#ifndef NANO_SMALL
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000246void cancel_command(int signal)
247{
248 if (kill(pid, SIGKILL) == -1)
249 nperror("kill");
250}
251
252/* Return TRUE on success. */
253bool 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
335void 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. */
343bool do_wrap(filestruct *line)
344{
345 size_t line_len;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000346 /* The length of the line we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000347 ssize_t wrap_loc;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000348 /* The index of line->data where we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000349#ifndef NANO_SMALL
350 const char *indent_string = NULL;
351 /* Indentation to prepend to the new line. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000352 size_t indent_len = 0;
353 /* The length of indent_string. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000354#endif
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000355 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 Ramsey691698a2005-07-24 19:57:51 +0000361 const char *next_line = NULL;
362 /* The next line, minus indentation. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000363 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 Ramsey691698a2005-07-24 19:57:51 +0000369
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 Ramsey691698a2005-07-24 19:57:51 +0000514 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 Ramsey691698a2005-07-24 19:57:51 +0000560#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
562 * that the display length to there is at most goal + 1. If there is no
563 * such blank, then we find the first blank. We then take the last
564 * blank in that group of blanks. The terminating '\0' counts as a
565 * blank, as does a '\n' if newline is TRUE. */
566ssize_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) {
578 size_t pos = 0;
579
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000580 line_len = parse_mbchar(line, NULL, &pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000581
582 if (is_blank_mbchar(line) || (newline && *line == '\n')) {
583 blank_loc = cur_loc;
584
585 if (newline && *line == '\n')
586 break;
587 }
588
589 goal -= pos;
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;
601
602 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000603 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000604
605 if (is_blank_mbchar(line) || (newline && *line == '\n')) {
606 if (!found_blank)
607 found_blank = TRUE;
608 } else if (found_blank)
609 return cur_loc - line_len;
610
611 line += line_len;
612 cur_loc += line_len;
613 }
614
615 return -1;
616 }
617
618 /* Move to the last blank after blank_loc, if there is one. */
619 line -= cur_loc;
620 line += blank_loc;
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000621 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000622 line += line_len;
623
624 while (*line != '\0' && (is_blank_mbchar(line) ||
625 (newline && *line == '\n'))) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000626 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000627
628 line += line_len;
629 blank_loc += line_len;
630 }
631
632 return blank_loc;
633}
634#endif /* !DISABLE_HELP || !DISABLE_JUSTIFY || !DISABLE_WRAPPING */
635
636#if !defined(NANO_SMALL) || !defined(DISABLE_JUSTIFY)
637/* The "indentation" of a line is the whitespace between the quote part
638 * and the non-whitespace of the line. */
639size_t indent_length(const char *line)
640{
641 size_t len = 0;
642 char *blank_mb;
643 int blank_mb_len;
644
645 assert(line != NULL);
646
647 blank_mb = charalloc(mb_cur_max());
648
649 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000650 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000651
652 if (!is_blank_mbchar(blank_mb))
653 break;
654
655 line += blank_mb_len;
656 len += blank_mb_len;
657 }
658
659 free(blank_mb);
660
661 return len;
662}
663#endif /* !NANO_SMALL || !DISABLE_JUSTIFY */
664
665#ifndef DISABLE_JUSTIFY
666/* justify_format() replaces blanks with spaces and multiple spaces by 1
667 * (except it maintains up to 2 after a character in punct optionally
668 * followed by a character in brackets, and removes all from the end).
669 *
670 * justify_format() might make paragraph->data shorter, and change the
671 * actual pointer with null_at().
672 *
673 * justify_format() will not look at the first skip characters of
674 * paragraph. skip should be at most strlen(paragraph->data). The
675 * character at paragraph[skip + 1] must not be blank. */
676void justify_format(filestruct *paragraph, size_t skip)
677{
678 char *end, *new_end, *new_paragraph_data;
679 size_t shift = 0;
680#ifndef NANO_SMALL
681 size_t mark_shift = 0;
682#endif
683
684 /* These four asserts are assumptions about the input data. */
685 assert(paragraph != NULL);
686 assert(paragraph->data != NULL);
687 assert(skip < strlen(paragraph->data));
688 assert(!is_blank_mbchar(paragraph->data + skip));
689
690 end = paragraph->data + skip;
691 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
692 strncpy(new_paragraph_data, paragraph->data, skip);
693 new_end = new_paragraph_data + skip;
694
695 while (*end != '\0') {
696 int end_len;
697
698 /* If this character is blank, make sure that it's a space with
699 * no blanks after it. */
700 if (is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000701 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000702
703 *new_end = ' ';
704 new_end++;
705 end += end_len;
706
707 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000708 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000709
710 end += end_len;
711 shift += end_len;
712
713#ifndef NANO_SMALL
714 /* Keep track of the change in the current line. */
715 if (openfile->mark_set && openfile->mark_begin ==
716 paragraph && openfile->mark_begin_x >= end -
717 paragraph->data)
718 mark_shift += end_len;
719#endif
720 }
721 /* If this character is punctuation optionally followed by a
722 * bracket and then followed by blanks, make sure there are no
723 * more than two blanks after it, and make sure that the blanks
724 * are spaces. */
725 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000726 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000727
728 while (end_len > 0) {
729 *new_end = *end;
730 new_end++;
731 end++;
732 end_len--;
733 }
734
735 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000736 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000737
738 while (end_len > 0) {
739 *new_end = *end;
740 new_end++;
741 end++;
742 end_len--;
743 }
744 }
745
746 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000747 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000748
749 *new_end = ' ';
750 new_end++;
751 end += end_len;
752 }
753
754 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000755 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000756
757 *new_end = ' ';
758 new_end++;
759 end += end_len;
760 }
761
762 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000763 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000764
765 end += end_len;
766 shift += end_len;
767
768#ifndef NANO_SMALL
769 /* Keep track of the change in the current line. */
770 if (openfile->mark_set && openfile->mark_begin ==
771 paragraph && openfile->mark_begin_x >= end -
772 paragraph->data)
773 mark_shift += end_len;
774#endif
775 }
776 /* If this character is neither blank nor punctuation, leave it
777 * alone. */
778 } else {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000779 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000780
781 while (end_len > 0) {
782 *new_end = *end;
783 new_end++;
784 end++;
785 end_len--;
786 }
787 }
788 }
789
790 assert(*end == '\0');
791
792 *new_end = *end;
793
794 /* Make sure that there are no spaces at the end of the line. */
795 while (new_end > new_paragraph_data + skip &&
796 *(new_end - 1) == ' ') {
797 new_end--;
798 shift++;
799 }
800
801 if (shift > 0) {
802 openfile->totsize -= shift;
803 null_at(&new_paragraph_data, new_end - new_paragraph_data);
804 free(paragraph->data);
805 paragraph->data = new_paragraph_data;
806
807#ifndef NANO_SMALL
808 /* Adjust the mark coordinates to compensate for the change in
809 * the current line. */
810 if (openfile->mark_set && openfile->mark_begin == paragraph) {
811 openfile->mark_begin_x -= mark_shift;
812 if (openfile->mark_begin_x > new_end - new_paragraph_data)
813 openfile->mark_begin_x = new_end - new_paragraph_data;
814 }
815#endif
816 } else
817 free(new_paragraph_data);
818}
819
820/* The "quote part" of a line is the largest initial substring matching
821 * the quote string. This function returns the length of the quote part
822 * of the given line.
823 *
824 * Note that if !HAVE_REGEX_H then we match concatenated copies of
825 * quotestr. */
826size_t quote_length(const char *line)
827{
828#ifdef HAVE_REGEX_H
829 regmatch_t matches;
830 int rc = regexec(&quotereg, line, 1, &matches, 0);
831
832 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
833 return 0;
834 /* matches.rm_so should be 0, since the quote string should start
835 * with the caret ^. */
836 return matches.rm_eo;
837#else /* !HAVE_REGEX_H */
838 size_t qdepth = 0;
839
840 /* Compute quote depth level. */
841 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
842 qdepth += quotelen;
843 return qdepth;
844#endif /* !HAVE_REGEX_H */
845}
846
847/* a_line and b_line are lines of text. The quotation part of a_line is
848 * the first a_quote characters. Check that the quotation part of
849 * b_line is the same. */
850bool quotes_match(const char *a_line, size_t a_quote, const char
851 *b_line)
852{
853 /* Here is the assumption about a_quote. */
854 assert(a_quote == quote_length(a_line));
855
856 return (a_quote == quote_length(b_line) &&
857 strncmp(a_line, b_line, a_quote) == 0);
858}
859
860/* We assume a_line and b_line have no quote part. Then, we return
861 * whether b_line could follow a_line in a paragraph. */
862bool indents_match(const char *a_line, size_t a_indent, const char
863 *b_line, size_t b_indent)
864{
865 assert(a_indent == indent_length(a_line));
866 assert(b_indent == indent_length(b_line));
867
868 return (b_indent <= a_indent &&
869 strncmp(a_line, b_line, b_indent) == 0);
870}
871
872/* Is foo the beginning of a paragraph?
873 *
874 * A line of text consists of a "quote part", followed by an
875 * "indentation part", followed by text. The functions quote_length()
876 * and indent_length() calculate these parts.
877 *
878 * A line is "part of a paragraph" if it has a part not in the quote
879 * part or the indentation.
880 *
881 * A line is "the beginning of a paragraph" if it is part of a
882 * paragraph and
883 * 1) it is the top line of the file, or
884 * 2) the line above it is not part of a paragraph, or
885 * 3) the line above it does not have precisely the same quote
886 * part, or
887 * 4) the indentation of this line is not an initial substring of
888 * the indentation of the previous line, or
889 * 5) this line has no quote part and some indentation, and
890 * autoindent isn't turned on.
891 * The reason for number 5) is that if autoindent isn't turned on,
892 * then an indented line is expected to start a paragraph, as in
893 * books. Thus, nano can justify an indented paragraph only if
894 * autoindent is turned on. */
895bool begpar(const filestruct *const foo)
896{
897 size_t quote_len;
898 size_t indent_len;
899 size_t temp_id_len;
900
901 /* Case 1). */
902 if (foo->prev == NULL)
903 return TRUE;
904
905 quote_len = quote_length(foo->data);
906 indent_len = indent_length(foo->data + quote_len);
907
908 /* Not part of a paragraph. */
909 if (foo->data[quote_len + indent_len] == '\0')
910 return FALSE;
911
912 /* Case 3). */
913 if (!quotes_match(foo->data, quote_len, foo->prev->data))
914 return TRUE;
915
916 temp_id_len = indent_length(foo->prev->data + quote_len);
917
918 /* Case 2) or 5) or 4). */
919 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
920 (quote_len == 0 && indent_len > 0
921#ifndef NANO_SMALL
922 && !ISSET(AUTOINDENT)
923#endif
924 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
925 foo->data + quote_len, indent_len))
926 return TRUE;
927
928 return FALSE;
929}
930
931/* Is foo inside a paragraph? */
932bool inpar(const filestruct *const foo)
933{
934 size_t quote_len;
935
936 if (foo == NULL)
937 return FALSE;
938
939 quote_len = quote_length(foo->data);
940
941 return foo->data[quote_len + indent_length(foo->data +
942 quote_len)] != '\0';
943}
944
945/* Put the next par_len lines, starting with first_line, into the
946 * justify buffer, leaving copies of those lines in place. Assume there
947 * are enough lines after first_line. Return the new copy of
948 * first_line. */
949filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t
950 quote_len)
951{
952 filestruct *top = first_line;
953 /* The top of the paragraph we're backing up. */
954 filestruct *bot = first_line;
955 /* The bottom of the paragraph we're backing up. */
956 size_t i;
957 /* Generic loop variable. */
958 size_t current_x_save = openfile->current_x;
959 ssize_t fl_lineno_save = first_line->lineno;
960 ssize_t edittop_lineno_save = openfile->edittop->lineno;
961 ssize_t current_lineno_save = openfile->current->lineno;
962#ifndef NANO_SMALL
963 bool old_mark_set = openfile->mark_set;
964 ssize_t mb_lineno_save = 0;
965 size_t mark_begin_x_save = 0;
966
967 if (old_mark_set) {
968 mb_lineno_save = openfile->mark_begin->lineno;
969 mark_begin_x_save = openfile->mark_begin_x;
970 }
971#endif
972
973 /* Move bot down par_len lines to the newline after the last line of
974 * the paragraph. */
975 for (i = par_len; i > 0; i--)
976 bot = bot->next;
977
978 /* Move the paragraph from the current buffer's filestruct to the
979 * justify buffer. */
980 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot, 0);
981
982 /* Copy the paragraph back to the current buffer's filestruct from
983 * the justify buffer. */
984 copy_from_filestruct(jusbuffer, jusbottom);
985
986 /* Move upward from the last line of the paragraph to the first
987 * line, putting first_line, edittop, current, and mark_begin at the
988 * same lines in the copied paragraph that they had in the original
989 * paragraph. */
990 top = openfile->current->prev;
991 for (i = par_len; i > 0; i--) {
992 if (top->lineno == fl_lineno_save)
993 first_line = top;
994 if (top->lineno == edittop_lineno_save)
995 openfile->edittop = top;
996 if (top->lineno == current_lineno_save)
997 openfile->current = top;
998#ifndef NANO_SMALL
999 if (old_mark_set && top->lineno == mb_lineno_save) {
1000 openfile->mark_begin = top;
1001 openfile->mark_begin_x = mark_begin_x_save;
1002 }
1003#endif
1004 top = top->prev;
1005 }
1006
1007 /* Put current_x at the same place in the copied paragraph that it
1008 * had in the original paragraph. */
1009 openfile->current_x = current_x_save;
1010
1011 set_modified();
1012
1013 return first_line;
1014}
1015
1016/* Find the beginning of the current paragraph if we're in one, or the
1017 * beginning of the next paragraph if we're not. Afterwards, save the
1018 * quote length and paragraph length in *quote and *par. Return TRUE if
1019 * we found a paragraph, or FALSE if there was an error or we didn't
1020 * find a paragraph.
1021 *
1022 * See the comment at begpar() for more about when a line is the
1023 * beginning of a paragraph. */
1024bool find_paragraph(size_t *const quote, size_t *const par)
1025{
1026 size_t quote_len;
1027 /* Length of the initial quotation of the paragraph we search
1028 * for. */
1029 size_t par_len;
1030 /* Number of lines in the paragraph we search for. */
1031 filestruct *current_save;
1032 /* The line at the beginning of the paragraph we search for. */
1033 ssize_t current_y_save;
1034 /* The y-coordinate at the beginning of the paragraph we search
1035 * for. */
1036
1037#ifdef HAVE_REGEX_H
1038 if (quoterc != 0) {
1039 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1040 return FALSE;
1041 }
1042#endif
1043
1044 assert(openfile->current != NULL);
1045
1046 /* Move back to the beginning of the current line. */
1047 openfile->current_x = 0;
1048 openfile->placewewant = 0;
1049
1050 /* Find the first line of the current or next paragraph. First, if
1051 * the current line isn't in a paragraph, move forward to the line
1052 * after the last line of the next paragraph. If we end up on the
1053 * same line, or the line before that isn't in a paragraph, it means
1054 * that there aren't any paragraphs left, so get out. Otherwise,
1055 * move back to the last line of the paragraph. If the current line
1056 * is in a paragraph and it isn't the first line of that paragraph,
1057 * move back to the first line. */
1058 if (!inpar(openfile->current)) {
1059 current_save = openfile->current;
1060
1061 do_para_end(FALSE);
1062 if (openfile->current == current_save ||
1063 !inpar(openfile->current->prev))
1064 return FALSE;
1065 if (openfile->current->prev != NULL)
1066 openfile->current = openfile->current->prev;
1067 }
1068 if (!begpar(openfile->current))
1069 do_para_begin(FALSE);
1070
1071 /* Now current is the first line of the paragraph. Set quote_len to
1072 * the quotation length of that line, and set par_len to the number
1073 * of lines in this paragraph. */
1074 quote_len = quote_length(openfile->current->data);
1075 current_save = openfile->current;
1076 current_y_save = openfile->current_y;
1077 do_para_end(FALSE);
1078 par_len = openfile->current->lineno - current_save->lineno;
1079 openfile->current = current_save;
1080 openfile->current_y = current_y_save;
1081
1082 /* Save the values of quote_len and par_len. */
1083 assert(quote != NULL && par != NULL);
1084
1085 *quote = quote_len;
1086 *par = par_len;
1087
1088 return TRUE;
1089}
1090
1091/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1092 * the current paragraph. */
1093void do_justify(bool full_justify)
1094{
1095 filestruct *first_par_line = NULL;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001096 /* Will be the first line of the justified paragraph. For
1097 * restoring after unjustify. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001098 filestruct *last_par_line;
1099 /* Will be the line containing the newline after the last line
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001100 * of the justified paragraph. Also for restoring after
1101 * unjustify. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001102
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001103 /* We save these variables to be restored if the user
1104 * unjustifies. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001105 size_t current_x_save = openfile->current_x;
1106 size_t pww_save = openfile->placewewant;
1107 ssize_t current_y_save = openfile->current_y;
1108 bool modified_save = openfile->modified;
1109 size_t totsize_save = openfile->totsize;
1110 filestruct *edittop_save = openfile->edittop;
1111 filestruct *current_save = openfile->current;
1112#ifndef NANO_SMALL
1113 filestruct *mark_begin_save = openfile->mark_begin;
1114 size_t mark_begin_x_save = openfile->mark_begin_x;
1115#endif
1116 int kbinput;
1117 bool meta_key, func_key, s_or_t, ran_func, finished;
1118
1119 /* If we're justifying the entire file, start at the beginning. */
1120 if (full_justify)
1121 openfile->current = openfile->fileage;
1122
1123 last_par_line = openfile->current;
1124
1125 while (TRUE) {
1126 size_t i;
1127 /* Generic loop variable. */
1128 size_t quote_len;
1129 /* Length of the initial quotation of the paragraph we
1130 * justify. */
1131 size_t indent_len;
1132 /* Length of the initial indentation of the paragraph we
1133 * justify. */
1134 size_t par_len;
1135 /* Number of lines in the paragraph we justify. */
1136 ssize_t break_pos;
1137 /* Where we will break lines. */
1138 char *indent_string;
1139 /* The first indentation that doesn't match the initial
1140 * indentation of the paragraph we justify. This is put at
1141 * the beginning of every line broken off the first
1142 * justified line of the paragraph. (Note that this works
1143 * because a paragraph can only contain two indentations at
1144 * most: the initial one, and a different one starting on a
1145 * line after the first. See the comment at begpar() for
1146 * more about when a line is part of a paragraph.) */
1147
1148 /* Find the first line of the paragraph to be justified. That
1149 * is the start of this paragraph if we're in one, or the start
1150 * of the next otherwise. Save the quote length and paragraph
1151 * length (number of lines). Don't refresh the screen yet,
1152 * since we'll do that after we justify.
1153 *
1154 * If the search failed, we do one of two things. If we're
1155 * justifying the whole file, we've found at least one
1156 * paragraph, and the search didn't leave us on the last line of
1157 * the file, it means that we should justify all the way to the
1158 * last line of the file, so set the last line of the text to be
1159 * justified to the last line of the file and break out of the
1160 * loop. Otherwise, it means that there are no paragraph(s) to
1161 * justify, so refresh the screen and get out. */
1162 if (!find_paragraph(&quote_len, &par_len)) {
1163 if (full_justify && first_par_line != NULL &&
1164 first_par_line != openfile->filebot) {
1165 last_par_line = openfile->filebot;
1166 break;
1167 } else {
1168 edit_refresh();
1169 return;
1170 }
1171 }
1172
1173 /* If we haven't already done it, copy the original paragraph(s)
1174 * to the justify buffer. */
1175 if (first_par_line == NULL)
1176 first_par_line = backup_lines(openfile->current,
1177 full_justify ? openfile->filebot->lineno -
1178 openfile->current->lineno : par_len, quote_len);
1179
1180 /* Initialize indent_string to a blank string. */
1181 indent_string = mallocstrcpy(NULL, "");
1182
1183 /* Find the first indentation in the paragraph that doesn't
1184 * match the indentation of the first line, and save it in
1185 * indent_string. If all the indentations are the same, save
1186 * the indentation of the first line in indent_string. */
1187 {
1188 const filestruct *indent_line = openfile->current;
1189 bool past_first_line = FALSE;
1190
1191 for (i = 0; i < par_len; i++) {
1192 indent_len = quote_len +
1193 indent_length(indent_line->data + quote_len);
1194
1195 if (indent_len != strlen(indent_string)) {
1196 indent_string = mallocstrncpy(indent_string,
1197 indent_line->data, indent_len + 1);
1198 indent_string[indent_len] = '\0';
1199
1200 if (past_first_line)
1201 break;
1202 }
1203
1204 if (indent_line == openfile->current)
1205 past_first_line = TRUE;
1206
1207 indent_line = indent_line->next;
1208 }
1209 }
1210
1211 /* Now tack all the lines of the paragraph together, skipping
1212 * the quoting and indentation on all lines after the first. */
1213 for (i = 0; i < par_len - 1; i++) {
1214 filestruct *next_line = openfile->current->next;
1215 size_t line_len = strlen(openfile->current->data);
1216 size_t next_line_len =
1217 strlen(openfile->current->next->data);
1218
1219 indent_len = quote_len +
1220 indent_length(openfile->current->next->data +
1221 quote_len);
1222
1223 next_line_len -= indent_len;
1224 openfile->totsize -= indent_len;
1225
1226 /* We're just about to tack the next line onto this one. If
1227 * this line isn't empty, make sure it ends in a space. */
1228 if (line_len > 0 &&
1229 openfile->current->data[line_len - 1] != ' ') {
1230 line_len++;
1231 openfile->current->data =
1232 charealloc(openfile->current->data,
1233 line_len + 1);
1234 openfile->current->data[line_len - 1] = ' ';
1235 openfile->current->data[line_len] = '\0';
1236 openfile->totsize++;
1237 }
1238
1239 openfile->current->data =
1240 charealloc(openfile->current->data, line_len +
1241 next_line_len + 1);
1242 strcat(openfile->current->data, next_line->data +
1243 indent_len);
1244
1245 /* Don't destroy edittop! */
1246 if (openfile->edittop == next_line)
1247 openfile->edittop = openfile->current;
1248
1249#ifndef NANO_SMALL
1250 /* Adjust the mark coordinates to compensate for the change
1251 * in the next line. */
1252 if (openfile->mark_set && openfile->mark_begin ==
1253 next_line) {
1254 openfile->mark_begin = openfile->current;
1255 openfile->mark_begin_x += line_len - indent_len;
1256 }
1257#endif
1258
1259 unlink_node(next_line);
1260 delete_node(next_line);
1261
1262 /* If we've removed the next line, we need to go through
1263 * this line again. */
1264 i--;
1265
1266 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001267 openfile->totsize--;
1268 }
1269
1270 /* Call justify_format() on the paragraph, which will remove
1271 * excess spaces from it and change all blank characters to
1272 * spaces. */
1273 justify_format(openfile->current, quote_len +
1274 indent_length(openfile->current->data + quote_len));
1275
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001276 while (par_len > 0 && strlenpt(openfile->current->data) >
1277 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001278 size_t line_len = strlen(openfile->current->data);
1279
1280 indent_len = strlen(indent_string);
1281
1282 /* If this line is too long, try to wrap it to the next line
1283 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001284 break_pos = break_line(openfile->current->data + indent_len,
1285 fill - strnlenpt(openfile->current->data, indent_len),
1286 FALSE);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001287
1288 /* We can't break the line, or don't need to, so get out. */
1289 if (break_pos == -1 || break_pos + indent_len == line_len)
1290 break;
1291
1292 /* Move forward to the character after the indentation and
1293 * just after the space. */
1294 break_pos += indent_len + 1;
1295
1296 assert(break_pos <= line_len);
1297
1298 /* Make a new line, and copy the text after where we're
1299 * going to break this line to the beginning of the new
1300 * line. */
1301 splice_node(openfile->current,
1302 make_new_node(openfile->current),
1303 openfile->current->next);
1304
1305 /* If this paragraph is non-quoted, and autoindent isn't
1306 * turned on, set the indentation length to zero so that the
1307 * indentation is treated as part of the line. */
1308 if (quote_len == 0
1309#ifndef NANO_SMALL
1310 && !ISSET(AUTOINDENT)
1311#endif
1312 )
1313 indent_len = 0;
1314
1315 /* Copy the text after where we're going to break the
1316 * current line to the next line. */
1317 openfile->current->next->data = charalloc(indent_len + 1 +
1318 line_len - break_pos);
1319 strncpy(openfile->current->next->data, indent_string,
1320 indent_len);
1321 strcpy(openfile->current->next->data + indent_len,
1322 openfile->current->data + break_pos);
1323
1324 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001325 openfile->totsize += indent_len + 1;
1326
1327#ifndef NANO_SMALL
1328 /* Adjust the mark coordinates to compensate for the change
1329 * in the current line. */
1330 if (openfile->mark_set && openfile->mark_begin ==
1331 openfile->current && openfile->mark_begin_x >
1332 break_pos) {
1333 openfile->mark_begin = openfile->current->next;
1334 openfile->mark_begin_x -= break_pos - indent_len;
1335 }
1336#endif
1337
1338 /* Break the current line. */
1339 null_at(&openfile->current->data, break_pos);
1340
1341 /* Go to the next line. */
1342 par_len--;
1343 openfile->current_y++;
1344 openfile->current = openfile->current->next;
1345 }
1346
1347 /* We're done breaking lines, so we don't need indent_string
1348 * anymore. */
1349 free(indent_string);
1350
1351 /* Go to the next line, the line after the last line of the
1352 * paragraph. */
1353 openfile->current_y++;
1354 openfile->current = openfile->current->next;
1355
1356 /* We've just justified a paragraph. If we're not justifying the
1357 * entire file, break out of the loop. Otherwise, continue the
1358 * loop so that we justify all the paragraphs in the file. */
1359 if (!full_justify)
1360 break;
1361 }
1362
1363 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001364 * up. totsize, and current_y have been maintained above. Set
1365 * last_par_line to the new end of the paragraph, update fileage,
1366 * and renumber, since edit_refresh() needs the line numbers to be
1367 * right (but only do the last two if we actually justified
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001368 * something). */
1369 last_par_line = openfile->current;
1370 if (first_par_line != NULL) {
1371 if (first_par_line->prev == NULL)
1372 openfile->fileage = first_par_line;
1373 renumber(first_par_line);
1374 }
1375
1376 edit_refresh();
1377
1378 statusbar(_("Can now UnJustify!"));
1379
1380 /* If constant cursor position display is on, make sure the current
1381 * cursor position will be properly displayed on the statusbar. */
1382 if (ISSET(CONST_UPDATE))
1383 do_cursorpos(TRUE);
1384
1385 /* Display the shortcut list with UnJustify. */
1386 shortcut_init(TRUE);
1387 display_main_list();
1388
1389 /* Now get a keystroke and see if it's unjustify. If not, put back
1390 * the keystroke and return. */
1391 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
1392 &finished, FALSE);
1393
1394 if (!meta_key && !func_key && s_or_t &&
1395 kbinput == NANO_UNJUSTIFY_KEY) {
1396 /* Restore the justify we just did (ungrateful user!). */
1397 openfile->current = current_save;
1398 openfile->current_x = current_x_save;
1399 openfile->placewewant = pww_save;
1400 openfile->current_y = current_y_save;
1401 openfile->edittop = edittop_save;
1402
1403 /* Splice the justify buffer back into the file, but only if we
1404 * actually justified something. */
1405 if (first_par_line != NULL) {
1406 filestruct *top_save;
1407
1408 /* Partition the filestruct so that it contains only the
1409 * text of the justified paragraph. */
1410 filepart = partition_filestruct(first_par_line, 0,
1411 last_par_line, 0);
1412
1413 /* Remove the text of the justified paragraph, and
1414 * put the text in the justify buffer in its place. */
1415 free_filestruct(openfile->fileage);
1416 openfile->fileage = jusbuffer;
1417 openfile->filebot = jusbottom;
1418
1419 top_save = openfile->fileage;
1420
1421 /* Unpartition the filestruct so that it contains all the
1422 * text again. Note that the justified paragraph has been
1423 * replaced with the unjustified paragraph. */
1424 unpartition_filestruct(&filepart);
1425
1426 /* Renumber starting with the beginning line of the old
1427 * partition. */
1428 renumber(top_save);
1429
1430 /* Restore variables from before the justify. */
1431 openfile->totsize = totsize_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001432#ifndef NANO_SMALL
1433 if (openfile->mark_set) {
1434 openfile->mark_begin = mark_begin_save;
1435 openfile->mark_begin_x = mark_begin_x_save;
1436 }
1437#endif
1438 openfile->modified = modified_save;
1439
1440 /* Clear the justify buffer. */
1441 jusbuffer = NULL;
1442
1443 if (!openfile->modified)
1444 titlebar(NULL);
1445 edit_refresh();
1446 }
1447 } else {
1448 unget_kbinput(kbinput, meta_key, func_key);
1449
1450 /* Blow away the text in the justify buffer. */
1451 free_filestruct(jusbuffer);
1452 jusbuffer = NULL;
1453 }
1454
1455 blank_statusbar();
1456
1457 /* Display the shortcut list with UnCut. */
1458 shortcut_init(FALSE);
1459 display_main_list();
1460}
1461
1462void do_justify_void(void)
1463{
1464 do_justify(FALSE);
1465}
1466
1467void do_full_justify(void)
1468{
1469 do_justify(TRUE);
1470}
1471#endif /* !DISABLE_JUSTIFY */
1472
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001473#ifndef DISABLE_SPELLER
1474/* A word is misspelled in the file. Let the user replace it. We
1475 * return FALSE if the user cancels. */
1476bool do_int_spell_fix(const char *word)
1477{
1478 char *save_search, *save_replace;
1479 size_t match_len, current_x_save = openfile->current_x;
1480 size_t pww_save = openfile->placewewant;
1481 filestruct *edittop_save = openfile->edittop;
1482 filestruct *current_save = openfile->current;
1483 /* Save where we are. */
1484 bool canceled = FALSE;
1485 /* The return value. */
1486 bool case_sens_set = ISSET(CASE_SENSITIVE);
1487#ifndef NANO_SMALL
1488 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
1489#endif
1490#ifdef HAVE_REGEX_H
1491 bool regexp_set = ISSET(USE_REGEXP);
1492#endif
1493#ifndef NANO_SMALL
1494 bool old_mark_set = openfile->mark_set;
1495 bool added_magicline = FALSE;
1496 /* Whether we added a magicline after filebot. */
1497 bool right_side_up = FALSE;
1498 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1499 * FALSE if (current, current_x) is. */
1500 filestruct *top, *bot;
1501 size_t top_x, bot_x;
1502#endif
1503
1504 /* Make sure spell-check is case sensitive. */
1505 SET(CASE_SENSITIVE);
1506
1507#ifndef NANO_SMALL
1508 /* Make sure spell-check goes forward only. */
1509 UNSET(BACKWARDS_SEARCH);
1510#endif
1511#ifdef HAVE_REGEX_H
1512 /* Make sure spell-check doesn't use regular expressions. */
1513 UNSET(USE_REGEXP);
1514#endif
1515
1516 /* Save the current search/replace strings. */
1517 search_init_globals();
1518 save_search = last_search;
1519 save_replace = last_replace;
1520
1521 /* Set the search/replace strings to the misspelled word. */
1522 last_search = mallocstrcpy(NULL, word);
1523 last_replace = mallocstrcpy(NULL, word);
1524
1525#ifndef NANO_SMALL
1526 if (old_mark_set) {
1527 /* If the mark is on, partition the filestruct so that it
1528 * contains only the marked text, keep track of whether the text
1529 * will have a magicline added when we're done correcting
1530 * misspelled words, and turn the mark off. */
1531 mark_order((const filestruct **)&top, &top_x,
1532 (const filestruct **)&bot, &bot_x, &right_side_up);
1533 filepart = partition_filestruct(top, top_x, bot, bot_x);
1534 added_magicline = (openfile->filebot->data[0] != '\0');
1535 openfile->mark_set = FALSE;
1536 }
1537#endif
1538
1539 /* Start from the top of the file. */
1540 openfile->edittop = openfile->fileage;
1541 openfile->current = openfile->fileage;
1542 openfile->current_x = (size_t)-1;
1543 openfile->placewewant = 0;
1544
1545 /* Find the first whole-word occurrence of word. */
1546 findnextstr_wrap_reset();
1547 while (findnextstr(TRUE, TRUE, FALSE, openfile->fileage, 0, word,
1548 &match_len)) {
1549 if (is_whole_word(openfile->current_x, openfile->current->data,
1550 word)) {
1551 size_t xpt = xplustabs();
1552 char *exp_word = display_string(openfile->current->data,
1553 xpt, strnlenpt(openfile->current->data,
1554 openfile->current_x + match_len) - xpt, FALSE);
1555
1556 edit_refresh();
1557
1558 do_replace_highlight(TRUE, exp_word);
1559
1560 /* Allow all instances of the word to be corrected. */
1561 canceled = (statusq(FALSE, spell_list, word,
1562#ifndef NANO_SMALL
1563 NULL,
1564#endif
1565 _("Edit a replacement")) == -1);
1566
1567 do_replace_highlight(FALSE, exp_word);
1568
1569 free(exp_word);
1570
1571 if (!canceled && strcmp(word, answer) != 0) {
1572 openfile->current_x--;
1573 do_replace_loop(word, openfile->current,
1574 &openfile->current_x, TRUE, &canceled);
1575 }
1576
1577 break;
1578 }
1579 }
1580
1581#ifndef NANO_SMALL
1582 if (old_mark_set) {
1583 /* If the mark was on and we added a magicline, remove it
1584 * now. */
1585 if (added_magicline)
1586 remove_magicline();
1587
1588 /* Put the beginning and the end of the mark at the beginning
1589 * and the end of the spell-checked text. */
1590 if (openfile->fileage == openfile->filebot)
1591 bot_x += top_x;
1592 if (right_side_up) {
1593 openfile->mark_begin_x = top_x;
1594 current_x_save = bot_x;
1595 } else {
1596 current_x_save = top_x;
1597 openfile->mark_begin_x = bot_x;
1598 }
1599
1600 /* Unpartition the filestruct so that it contains all the text
1601 * again, and turn the mark back on. */
1602 unpartition_filestruct(&filepart);
1603 openfile->mark_set = TRUE;
1604 }
1605#endif
1606
1607 /* Restore the search/replace strings. */
1608 free(last_search);
1609 last_search = save_search;
1610 free(last_replace);
1611 last_replace = save_replace;
1612
1613 /* Restore where we were. */
1614 openfile->edittop = edittop_save;
1615 openfile->current = current_save;
1616 openfile->current_x = current_x_save;
1617 openfile->placewewant = pww_save;
1618
1619 /* Restore case sensitivity setting. */
1620 if (!case_sens_set)
1621 UNSET(CASE_SENSITIVE);
1622
1623#ifndef NANO_SMALL
1624 /* Restore search/replace direction. */
1625 if (backwards_search_set)
1626 SET(BACKWARDS_SEARCH);
1627#endif
1628#ifdef HAVE_REGEX_H
1629 /* Restore regular expression usage setting. */
1630 if (regexp_set)
1631 SET(USE_REGEXP);
1632#endif
1633
1634 return !canceled;
1635}
1636
1637/* Integrated spell checking using the spell program, filtered through
1638 * the sort and uniq programs. Return NULL for normal termination,
1639 * and the error string otherwise. */
1640const char *do_int_speller(const char *tempfile_name)
1641{
1642 char *read_buff, *read_buff_ptr, *read_buff_word;
1643 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
1644 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
1645 pid_t pid_spell, pid_sort, pid_uniq;
1646 int spell_status, sort_status, uniq_status;
1647
1648 /* Create all three pipes up front. */
1649 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
1650 pipe(uniq_fd) == -1)
1651 return _("Could not create pipe");
1652
1653 statusbar(_("Creating misspelled word list, please wait..."));
1654
1655 /* A new process to run spell in. */
1656 if ((pid_spell = fork()) == 0) {
1657 /* Child continues (i.e, future spell process). */
1658 close(spell_fd[0]);
1659
1660 /* Replace the standard input with the temp file. */
1661 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1662 goto close_pipes_and_exit;
1663
1664 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1665 goto close_pipes_and_exit;
1666
1667 close(tempfile_fd);
1668
1669 /* Send spell's standard output to the pipe. */
1670 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1671 goto close_pipes_and_exit;
1672
1673 close(spell_fd[1]);
1674
1675 /* Start the spell program; we are using PATH. */
1676 execlp("spell", "spell", NULL);
1677
1678 /* This should not be reached if spell is found. */
1679 exit(1);
1680 }
1681
1682 /* Parent continues here. */
1683 close(spell_fd[1]);
1684
1685 /* A new process to run sort in. */
1686 if ((pid_sort = fork()) == 0) {
1687 /* Child continues (i.e, future spell process). Replace the
1688 * standard input with the standard output of the old pipe. */
1689 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1690 goto close_pipes_and_exit;
1691
1692 close(spell_fd[0]);
1693
1694 /* Send sort's standard output to the new pipe. */
1695 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1696 goto close_pipes_and_exit;
1697
1698 close(sort_fd[1]);
1699
1700 /* Start the sort program. Use -f to remove mixed case. If
1701 * this isn't portable, let me know. */
1702 execlp("sort", "sort", "-f", NULL);
1703
1704 /* This should not be reached if sort is found. */
1705 exit(1);
1706 }
1707
1708 close(spell_fd[0]);
1709 close(sort_fd[1]);
1710
1711 /* A new process to run uniq in. */
1712 if ((pid_uniq = fork()) == 0) {
1713 /* Child continues (i.e, future uniq process). Replace the
1714 * standard input with the standard output of the old pipe. */
1715 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
1716 goto close_pipes_and_exit;
1717
1718 close(sort_fd[0]);
1719
1720 /* Send uniq's standard output to the new pipe. */
1721 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1722 goto close_pipes_and_exit;
1723
1724 close(uniq_fd[1]);
1725
1726 /* Start the uniq program; we are using PATH. */
1727 execlp("uniq", "uniq", NULL);
1728
1729 /* This should not be reached if uniq is found. */
1730 exit(1);
1731 }
1732
1733 close(sort_fd[0]);
1734 close(uniq_fd[1]);
1735
1736 /* The child process was not forked successfully. */
1737 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
1738 close(uniq_fd[0]);
1739 return _("Could not fork");
1740 }
1741
1742 /* Get the system pipe buffer size. */
1743 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
1744 close(uniq_fd[0]);
1745 return _("Could not get size of pipe buffer");
1746 }
1747
1748 /* Read in the returned spelling errors. */
1749 read_buff_read = 0;
1750 read_buff_size = pipe_buff_size + 1;
1751 read_buff = read_buff_ptr = charalloc(read_buff_size);
1752
1753 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
1754 pipe_buff_size)) > 0) {
1755 read_buff_read += bytesread;
1756 read_buff_size += pipe_buff_size;
1757 read_buff = read_buff_ptr = charealloc(read_buff,
1758 read_buff_size);
1759 read_buff_ptr += read_buff_read;
1760 }
1761
1762 *read_buff_ptr = '\0';
1763 close(uniq_fd[0]);
1764
1765 /* Process the spelling errors. */
1766 read_buff_word = read_buff_ptr = read_buff;
1767
1768 while (*read_buff_ptr != '\0') {
1769 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
1770 *read_buff_ptr = '\0';
1771 if (read_buff_word != read_buff_ptr) {
1772 if (!do_int_spell_fix(read_buff_word)) {
1773 read_buff_word = read_buff_ptr;
1774 break;
1775 }
1776 }
1777 read_buff_word = read_buff_ptr + 1;
1778 }
1779 read_buff_ptr++;
1780 }
1781
1782 /* Special case: the last word doesn't end with '\r' or '\n'. */
1783 if (read_buff_word != read_buff_ptr)
1784 do_int_spell_fix(read_buff_word);
1785
1786 free(read_buff);
1787 replace_abort();
1788 edit_refresh();
1789
1790 /* Process the end of the spell process. */
1791 waitpid(pid_spell, &spell_status, 0);
1792 waitpid(pid_sort, &sort_status, 0);
1793 waitpid(pid_uniq, &uniq_status, 0);
1794
1795 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
1796 return _("Error invoking \"spell\"");
1797
1798 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
1799 return _("Error invoking \"sort -f\"");
1800
1801 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
1802 return _("Error invoking \"uniq\"");
1803
1804 /* Otherwise... */
1805 return NULL;
1806
1807 close_pipes_and_exit:
1808 /* Don't leak any handles. */
1809 close(tempfile_fd);
1810 close(spell_fd[0]);
1811 close(spell_fd[1]);
1812 close(sort_fd[0]);
1813 close(sort_fd[1]);
1814 close(uniq_fd[0]);
1815 close(uniq_fd[1]);
1816 exit(1);
1817}
1818
1819/* External spell checking. Return value: NULL for normal termination,
1820 * otherwise the error string. */
1821const char *do_alt_speller(char *tempfile_name)
1822{
1823 int alt_spell_status;
1824 size_t current_x_save = openfile->current_x;
1825 size_t pww_save = openfile->placewewant;
1826 ssize_t current_y_save = openfile->current_y;
1827 ssize_t lineno_save = openfile->current->lineno;
1828 pid_t pid_spell;
1829 char *ptr;
1830 static int arglen = 3;
1831 static char **spellargs = NULL;
1832 FILE *f;
1833#ifndef NANO_SMALL
1834 bool old_mark_set = openfile->mark_set;
1835 bool added_magicline = FALSE;
1836 /* Whether we added a magicline after filebot. */
1837 bool right_side_up = FALSE;
1838 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1839 * FALSE if (current, current_x) is. */
1840 filestruct *top, *bot;
1841 size_t top_x, bot_x;
1842 ssize_t mb_lineno_save = 0;
1843 /* We're going to close the current file, and open the output of
1844 * the alternate spell command. The line that mark_begin points
1845 * to will be freed, so we save the line number and restore it
1846 * afterwards. */
1847 size_t totsize_save = openfile->totsize;
1848 /* Our saved value of totsize, used when we spell-check a marked
1849 * selection. */
1850
1851 if (old_mark_set) {
1852 /* If the mark is on, save the number of the line it starts on,
1853 * and then turn the mark off. */
1854 mb_lineno_save = openfile->mark_begin->lineno;
1855 openfile->mark_set = FALSE;
1856 }
1857#endif
1858
1859 endwin();
1860
1861 /* Set up an argument list to pass execvp(). */
1862 if (spellargs == NULL) {
1863 spellargs = (char **)nmalloc(arglen * sizeof(char *));
1864
1865 spellargs[0] = strtok(alt_speller, " ");
1866 while ((ptr = strtok(NULL, " ")) != NULL) {
1867 arglen++;
1868 spellargs = (char **)nrealloc(spellargs, arglen *
1869 sizeof(char *));
1870 spellargs[arglen - 3] = ptr;
1871 }
1872 spellargs[arglen - 1] = NULL;
1873 }
1874 spellargs[arglen - 2] = tempfile_name;
1875
1876 /* Start a new process for the alternate speller. */
1877 if ((pid_spell = fork()) == 0) {
1878 /* Start alternate spell program; we are using PATH. */
1879 execvp(spellargs[0], spellargs);
1880
1881 /* Should not be reached, if alternate speller is found!!! */
1882 exit(1);
1883 }
1884
1885 /* If we couldn't fork, get out. */
1886 if (pid_spell < 0)
1887 return _("Could not fork");
1888
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001889#ifndef NANO_SMALL
1890 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001891 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001892 allow_pending_sigwinch(FALSE);
1893#endif
1894
1895 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001896 wait(&alt_spell_status);
1897
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00001898 /* Reenter curses mode. */
1899 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001900
1901 /* Restore the terminal to its previous state. */
1902 terminal_init();
1903
1904 /* Turn the cursor back on for sure. */
1905 curs_set(1);
1906
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001907 /* The screen might have been resized. If it has, reinitialize all
1908 * the windows based on the new screen dimensions. */
1909 window_init();
1910
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001911 if (!WIFEXITED(alt_spell_status) ||
1912 WEXITSTATUS(alt_spell_status) != 0) {
1913 char *altspell_error;
1914 char *invoke_error = _("Error invoking \"%s\"");
1915
1916#ifndef NANO_SMALL
1917 /* Turn the mark back on if it was on before. */
1918 openfile->mark_set = old_mark_set;
1919#endif
1920
1921 altspell_error =
1922 charalloc(strlen(invoke_error) +
1923 strlen(alt_speller) + 1);
1924 sprintf(altspell_error, invoke_error, alt_speller);
1925 return altspell_error;
1926 }
1927
1928#ifndef NANO_SMALL
1929 if (old_mark_set) {
1930 /* If the mark was on, partition the filestruct so that it
1931 * contains only the marked text, and keep track of whether the
1932 * temp file (which should contain the spell-checked marked
1933 * text) will have a magicline added when it's reloaded. */
1934 mark_order((const filestruct **)&top, &top_x,
1935 (const filestruct **)&bot, &bot_x, &right_side_up);
1936 filepart = partition_filestruct(top, top_x, bot, bot_x);
1937 added_magicline = (openfile->filebot->data[0] != '\0');
1938
1939 /* Get the number of characters in the marked text, and subtract
1940 * it from the saved value of totsize. */
1941 totsize_save -= get_totsize(top, bot);
1942 }
1943#endif
1944
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001945 /* Reinitialize the text of the current buffer. */
1946 free_filestruct(openfile->fileage);
1947 initialize_buffer_text();
1948
1949 /* Reload the temp file. Open it, read it into the current buffer,
1950 * and move back to the first line of the buffer. */
1951 open_file(tempfile_name, FALSE, &f);
1952 read_file(f, tempfile_name);
1953 openfile->current = openfile->fileage;
1954
1955#ifndef NANO_SMALL
1956 if (old_mark_set) {
1957 filestruct *top_save = openfile->fileage;
1958
1959 /* If the mark was on and we added a magicline, remove it
1960 * now. */
1961 if (added_magicline)
1962 remove_magicline();
1963
1964 /* Put the beginning and the end of the mark at the beginning
1965 * and the end of the spell-checked text. */
1966 if (openfile->fileage == openfile->filebot)
1967 bot_x += top_x;
1968 if (right_side_up) {
1969 openfile->mark_begin_x = top_x;
1970 current_x_save = bot_x;
1971 } else {
1972 current_x_save = top_x;
1973 openfile->mark_begin_x = bot_x;
1974 }
1975
1976 /* Unpartition the filestruct so that it contains all the text
1977 * again. Note that we've replaced the marked text originally
1978 * in the partition with the spell-checked marked text in the
1979 * temp file. */
1980 unpartition_filestruct(&filepart);
1981
1982 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001983 * partition. Also add the number of characters in the
1984 * spell-checked marked text to the saved value of totsize, and
1985 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001986 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001987 totsize_save += openfile->totsize;
1988 openfile->totsize = totsize_save;
1989
1990 /* Assign mark_begin to the line where the mark began before. */
1991 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
1992 current_y_save, 0);
1993 openfile->mark_begin = openfile->current;
1994
1995 /* Assign mark_begin_x to the location in mark_begin where the
1996 * mark began before, adjusted for any shortening of the
1997 * line. */
1998 openfile->mark_begin_x = openfile->current_x;
1999
2000 /* Turn the mark back on. */
2001 openfile->mark_set = TRUE;
2002 }
2003#endif
2004
2005 /* Go back to the old position, and mark the file as modified. */
2006 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2007 set_modified();
2008
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002009#ifndef NANO_SMALL
2010 /* Handle a pending SIGWINCH again. */
2011 allow_pending_sigwinch(TRUE);
2012#endif
2013
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002014 return NULL;
2015}
2016
2017void do_spell(void)
2018{
2019 int i;
2020 FILE *temp_file;
2021 char *temp = safe_tempfile(&temp_file);
2022 const char *spell_msg;
2023
2024 if (temp == NULL) {
2025 statusbar(_("Could not create temp file: %s"), strerror(errno));
2026 return;
2027 }
2028
2029#ifndef NANO_SMALL
2030 if (openfile->mark_set)
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002031 i = write_marked_file(temp, temp_file, TRUE, OVERWRITE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002032 else
2033#endif
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002034 i = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002035
2036 if (i == -1) {
2037 statusbar(_("Error writing temp file: %s"), strerror(errno));
2038 free(temp);
2039 return;
2040 }
2041
2042 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2043 do_int_speller(temp);
2044 unlink(temp);
2045 free(temp);
2046
2047 /* If the spell-checker printed any error messages onscreen, make
2048 * sure that they're cleared off. */
2049 total_refresh();
2050
2051 if (spell_msg != NULL) {
2052 if (errno == 0)
2053 /* Don't display an error message of "Success". */
2054 statusbar(_("Spell checking failed: %s"), spell_msg);
2055 else
2056 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2057 strerror(errno));
2058 } else
2059 statusbar(_("Finished checking spelling"));
2060}
2061#endif /* !DISABLE_SPELLER */
2062
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002063#ifndef NANO_SMALL
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002064/* Our own version of "wc". Note that its character counts are in
2065 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002066void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002067{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002068 size_t words = 0, chars = 0;
2069 ssize_t lines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002070 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002071 size_t pww_save = openfile->placewewant;
2072 filestruct *current_save = openfile->current;
2073 bool old_mark_set = openfile->mark_set;
2074 bool added_magicline = FALSE;
2075 /* Whether we added a magicline after filebot. */
2076 filestruct *top, *bot;
2077 size_t top_x, bot_x;
2078
2079 if (old_mark_set) {
2080 /* If the mark is on, partition the filestruct so that it
2081 * contains only the marked text, keep track of whether the text
2082 * will need a magicline added while we're counting words, add
2083 * the magicline if necessary, and turn the mark off. */
2084 mark_order((const filestruct **)&top, &top_x,
2085 (const filestruct **)&bot, &bot_x, NULL);
2086 filepart = partition_filestruct(top, top_x, bot, bot_x);
2087 if ((added_magicline = (openfile->filebot->data[0] != '\0')))
2088 new_magicline();
2089 openfile->mark_set = FALSE;
2090 }
2091
2092 /* Start at the top of the file. */
2093 openfile->current = openfile->fileage;
2094 openfile->current_x = 0;
2095 openfile->placewewant = 0;
2096
2097 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002098 * part of a word, as "wc -w" does), without updating the screen,
2099 * until we reach the end of the file, incrementing the total word
2100 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002101 while (openfile->current != openfile->filebot ||
2102 openfile->current_x != 0) {
2103 if (do_next_word(TRUE, FALSE))
2104 words++;
2105 }
2106
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002107 /* Get the total line and character counts, as "wc -l" and "wc -c"
2108 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002109 if (old_mark_set) {
2110 /* If the mark was on and we added a magicline, remove it
2111 * now. */
2112 if (added_magicline)
2113 remove_magicline();
2114
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002115 lines = openfile->filebot->lineno -
2116 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002117 chars = get_totsize(openfile->fileage, openfile->filebot);
2118
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002119 /* Unpartition the filestruct so that it contains all the text
2120 * again, and turn the mark back on. */
2121 unpartition_filestruct(&filepart);
2122 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002123 } else {
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002124 lines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002125 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002126 }
2127
2128 /* Restore where we were. */
2129 openfile->current = current_save;
2130 openfile->current_x = current_x_save;
2131 openfile->placewewant = pww_save;
2132
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002133 /* Display the total word, line, and character counts on the
2134 * statusbar. */
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002135 statusbar("%sWords: %lu Lines: %ld Chars: %lu", old_mark_set ?
David Lawrence Ramsey815fb0a2005-08-05 19:38:11 +00002136 _("In Selection: ") : "", (unsigned long)words, (long)lines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002137 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002138}
2139#endif /* !NANO_SMALL */