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