blob: 15715078371a24e6f8eddbfaa5a69d7dd77c4386 [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! */
386
387 assert(line != NULL && line->data != NULL);
388
389 /* Save the length of the line. */
390 line_len = strlen(line->data);
391
392 /* Find the last blank where we can break the line. */
393 wrap_loc = break_line(line->data, fill, FALSE);
394
395 /* If we couldn't break the line, or we've reached the end of it, we
396 * don't wrap. */
397 if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
398 return FALSE;
399
400 /* Otherwise, move forward to the character just after the blank. */
401 wrap_loc += move_mbright(line->data + wrap_loc, 0);
402
403 /* If we've reached the end of the line, we don't wrap. */
404 if (line->data[wrap_loc] == '\0')
405 return FALSE;
406
407#ifndef NANO_SMALL
408 /* If autoindent is turned on, and we're on the character just after
409 * the indentation, we don't wrap. */
410 if (ISSET(AUTOINDENT)) {
411 /* Get the indentation of this line. */
412 indent_string = line->data;
413 indent_len = indent_length(indent_string);
414
415 if (wrap_loc == indent_len)
416 return FALSE;
417 }
418#endif
419
420 /* Step 2, making the new wrap line. It will consist of indentation
421 * followed by the text after the wrap point, optionally followed by
422 * a space (if the text after the wrap point doesn't end in a blank)
423 * and the text of the next line, if they can fit without
424 * wrapping, the next line exists, and the same_line_wrap flag is
425 * set. */
426
427 /* after_break is the text that will be wrapped to the next line. */
428 after_break = line->data + wrap_loc;
429 after_break_len = line_len - wrap_loc;
430
431 assert(strlen(after_break) == after_break_len);
432
433 /* We prepend the wrapped text to the next line, if the
434 * same_line_wrap flag is set, there is a next line, and prepending
435 * would not make the line too long. */
436 if (same_line_wrap && line->next != NULL) {
437 const char *end = after_break + move_mbleft(after_break,
438 after_break_len);
439
440 /* If after_break doesn't end in a blank, make sure it ends in a
441 * space. */
442 if (!is_blank_mbchar(end)) {
443 line_len++;
444 line->data = charealloc(line->data, line_len + 1);
445 line->data[line_len - 1] = ' ';
446 line->data[line_len] = '\0';
447 after_break = line->data + wrap_loc;
448 after_break_len++;
449 openfile->totsize++;
450 }
451
452 next_line = line->next->data;
453 next_line_len = strlen(next_line);
454
455 if (after_break_len + next_line_len <= fill) {
456 wrapping = TRUE;
457 new_line_len += next_line_len;
458 }
459 }
460
461 /* new_line_len is now the length of the text that will be wrapped
462 * to the next line, plus (if we're prepending to it) the length of
463 * the text of the next line. */
464 new_line_len += after_break_len;
465
466#ifndef NANO_SMALL
467 if (ISSET(AUTOINDENT)) {
468 if (wrapping) {
469 /* If we're wrapping, the indentation will come from the
470 * next line. */
471 indent_string = next_line;
472 indent_len = indent_length(indent_string);
473 next_line += indent_len;
474 } else {
475 /* Otherwise, it will come from this line, in which case
476 * we should increase new_line_len to make room for it. */
477 new_line_len += indent_len;
478 openfile->totsize += mbstrnlen(indent_string, indent_len);
479 }
480 }
481#endif
482
483 /* Now we allocate the new line and copy the text into it. */
484 new_line = charalloc(new_line_len + 1);
485 new_line[0] = '\0';
486
487#ifndef NANO_SMALL
488 if (ISSET(AUTOINDENT)) {
489 /* Copy the indentation. */
490 strncpy(new_line, indent_string, indent_len);
491 new_line[indent_len] = '\0';
492 new_line_len += indent_len;
493 }
494#endif
495
496 /* Copy all the text after the wrap point of the current line. */
497 strcat(new_line, after_break);
498
499 /* Break the current line at the wrap point. */
500 null_at(&line->data, wrap_loc);
501
502 if (wrapping) {
503 /* If we're wrapping, copy the text from the next line, minus
504 * the indentation that we already copied above. */
505 strcat(new_line, next_line);
506
507 free(line->next->data);
508 line->next->data = new_line;
509 } else {
510 /* Otherwise, make a new line and copy the text after where we
511 * broke this line to the beginning of the new line. */
512 splice_node(openfile->current, make_new_node(openfile->current),
513 openfile->current->next);
514
515 openfile->current->next->data = new_line;
516
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000517 openfile->totsize++;
518 }
519
520 /* Step 3, clean up. Reposition the cursor and mark, and do some
521 * other sundry things. */
522
523 /* Set the same_line_wrap flag, so that later wraps of this line
524 * will be prepended to the next line. */
525 same_line_wrap = TRUE;
526
527 /* Each line knows its line number. We recalculate these if we
528 * inserted a new line. */
529 if (!wrapping)
530 renumber(line);
531
532 /* If the cursor was after the break point, we must move it. We
533 * also clear the same_line_wrap flag in this case. */
534 if (openfile->current_x > wrap_loc) {
535 same_line_wrap = FALSE;
536 openfile->current = openfile->current->next;
537 openfile->current_x -= wrap_loc
538#ifndef NANO_SMALL
539 - indent_len
540#endif
541 ;
542 openfile->placewewant = xplustabs();
543 }
544
545#ifndef NANO_SMALL
546 /* If the mark was on this line after the wrap point, we move it
547 * down. If it was on the next line and we wrapped onto that line,
548 * we move it right. */
549 if (openfile->mark_set) {
550 if (openfile->mark_begin == line && openfile->mark_begin_x >
551 wrap_loc) {
552 openfile->mark_begin = line->next;
553 openfile->mark_begin_x -= wrap_loc - indent_len + 1;
554 } else if (wrapping && openfile->mark_begin == line->next)
555 openfile->mark_begin_x += after_break_len;
556 }
557#endif
558
559 return TRUE;
560}
561#endif /* !DISABLE_WRAPPING */
562
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000563#if !defined(DISABLE_HELP) || !defined(DISABLE_JUSTIFY) || !defined(DISABLE_WRAPPING)
564/* We are trying to break a chunk off line. We find the last blank such
David Lawrence Ramseycd9a5f02005-09-20 06:12:54 +0000565 * that the display length to there is at most (goal + 1). If there is
566 * no such blank, then we find the first blank. We then take the last
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000567 * blank in that group of blanks. The terminating '\0' counts as a
568 * blank, as does a '\n' if newline is TRUE. */
569ssize_t break_line(const char *line, ssize_t goal, bool newline)
570{
571 ssize_t blank_loc = -1;
572 /* Current tentative return value. Index of the last blank we
573 * found with short enough display width. */
574 ssize_t cur_loc = 0;
575 /* Current index in line. */
576 int line_len;
577
578 assert(line != NULL);
579
580 while (*line != '\0' && goal >= 0) {
David Lawrence Ramsey95ef3372005-09-20 19:44:19 +0000581 size_t pos = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000582
David Lawrence Ramseyca37ee62005-09-20 17:47:27 +0000583 line_len = parse_mbchar(line, NULL, &pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000584
585 if (is_blank_mbchar(line) || (newline && *line == '\n')) {
586 blank_loc = cur_loc;
587
588 if (newline && *line == '\n')
589 break;
590 }
591
David Lawrence Ramsey95ef3372005-09-20 19:44:19 +0000592 goal -= pos;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000593 line += line_len;
594 cur_loc += line_len;
595 }
596
597 if (goal >= 0)
598 /* In fact, the whole line displays shorter than goal. */
599 return cur_loc;
600
601 if (blank_loc == -1) {
602 /* No blank was found that was short enough. */
603 bool found_blank = FALSE;
David Lawrence Ramsey5ab12ca2005-09-20 17:52:52 +0000604 ssize_t found_blank_loc = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000605
606 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000607 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000608
609 if (is_blank_mbchar(line) || (newline && *line == '\n')) {
610 if (!found_blank)
611 found_blank = TRUE;
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +0000612 found_blank_loc = cur_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000613 } else if (found_blank)
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +0000614 return found_blank_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000615
616 line += line_len;
617 cur_loc += line_len;
618 }
619
620 return -1;
621 }
622
623 /* Move to the last blank after blank_loc, if there is one. */
624 line -= cur_loc;
625 line += blank_loc;
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 line += line_len;
628
629 while (*line != '\0' && (is_blank_mbchar(line) ||
630 (newline && *line == '\n'))) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000631 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000632
633 line += line_len;
634 blank_loc += line_len;
635 }
636
637 return blank_loc;
638}
639#endif /* !DISABLE_HELP || !DISABLE_JUSTIFY || !DISABLE_WRAPPING */
640
641#if !defined(NANO_SMALL) || !defined(DISABLE_JUSTIFY)
642/* The "indentation" of a line is the whitespace between the quote part
643 * and the non-whitespace of the line. */
644size_t indent_length(const char *line)
645{
646 size_t len = 0;
647 char *blank_mb;
648 int blank_mb_len;
649
650 assert(line != NULL);
651
652 blank_mb = charalloc(mb_cur_max());
653
654 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000655 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000656
657 if (!is_blank_mbchar(blank_mb))
658 break;
659
660 line += blank_mb_len;
661 len += blank_mb_len;
662 }
663
664 free(blank_mb);
665
666 return len;
667}
668#endif /* !NANO_SMALL || !DISABLE_JUSTIFY */
669
670#ifndef DISABLE_JUSTIFY
671/* justify_format() replaces blanks with spaces and multiple spaces by 1
672 * (except it maintains up to 2 after a character in punct optionally
673 * followed by a character in brackets, and removes all from the end).
674 *
675 * justify_format() might make paragraph->data shorter, and change the
676 * actual pointer with null_at().
677 *
678 * justify_format() will not look at the first skip characters of
679 * paragraph. skip should be at most strlen(paragraph->data). The
680 * character at paragraph[skip + 1] must not be blank. */
681void justify_format(filestruct *paragraph, size_t skip)
682{
683 char *end, *new_end, *new_paragraph_data;
684 size_t shift = 0;
685#ifndef NANO_SMALL
686 size_t mark_shift = 0;
687#endif
688
689 /* These four asserts are assumptions about the input data. */
690 assert(paragraph != NULL);
691 assert(paragraph->data != NULL);
692 assert(skip < strlen(paragraph->data));
693 assert(!is_blank_mbchar(paragraph->data + skip));
694
695 end = paragraph->data + skip;
696 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
697 strncpy(new_paragraph_data, paragraph->data, skip);
698 new_end = new_paragraph_data + skip;
699
700 while (*end != '\0') {
701 int end_len;
702
703 /* If this character is blank, make sure that it's a space with
704 * no blanks after it. */
705 if (is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000706 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000707
708 *new_end = ' ';
709 new_end++;
710 end += end_len;
711
712 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000713 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000714
715 end += end_len;
716 shift += end_len;
717
718#ifndef NANO_SMALL
719 /* Keep track of the change in the current line. */
720 if (openfile->mark_set && openfile->mark_begin ==
721 paragraph && openfile->mark_begin_x >= end -
722 paragraph->data)
723 mark_shift += end_len;
724#endif
725 }
726 /* If this character is punctuation optionally followed by a
727 * bracket and then followed by blanks, make sure there are no
728 * more than two blanks after it, and make sure that the blanks
729 * are spaces. */
730 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000731 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000732
733 while (end_len > 0) {
734 *new_end = *end;
735 new_end++;
736 end++;
737 end_len--;
738 }
739
740 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000741 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000742
743 while (end_len > 0) {
744 *new_end = *end;
745 new_end++;
746 end++;
747 end_len--;
748 }
749 }
750
751 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000752 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000753
754 *new_end = ' ';
755 new_end++;
756 end += end_len;
757 }
758
759 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000760 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000761
762 *new_end = ' ';
763 new_end++;
764 end += end_len;
765 }
766
767 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000768 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000769
770 end += end_len;
771 shift += end_len;
772
773#ifndef NANO_SMALL
774 /* Keep track of the change in the current line. */
775 if (openfile->mark_set && openfile->mark_begin ==
776 paragraph && openfile->mark_begin_x >= end -
777 paragraph->data)
778 mark_shift += end_len;
779#endif
780 }
781 /* If this character is neither blank nor punctuation, leave it
782 * alone. */
783 } else {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000784 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000785
786 while (end_len > 0) {
787 *new_end = *end;
788 new_end++;
789 end++;
790 end_len--;
791 }
792 }
793 }
794
795 assert(*end == '\0');
796
797 *new_end = *end;
798
799 /* Make sure that there are no spaces at the end of the line. */
800 while (new_end > new_paragraph_data + skip &&
801 *(new_end - 1) == ' ') {
802 new_end--;
803 shift++;
804 }
805
806 if (shift > 0) {
807 openfile->totsize -= shift;
808 null_at(&new_paragraph_data, new_end - new_paragraph_data);
809 free(paragraph->data);
810 paragraph->data = new_paragraph_data;
811
812#ifndef NANO_SMALL
813 /* Adjust the mark coordinates to compensate for the change in
814 * the current line. */
815 if (openfile->mark_set && openfile->mark_begin == paragraph) {
816 openfile->mark_begin_x -= mark_shift;
817 if (openfile->mark_begin_x > new_end - new_paragraph_data)
818 openfile->mark_begin_x = new_end - new_paragraph_data;
819 }
820#endif
821 } else
822 free(new_paragraph_data);
823}
824
825/* The "quote part" of a line is the largest initial substring matching
826 * the quote string. This function returns the length of the quote part
827 * of the given line.
828 *
829 * Note that if !HAVE_REGEX_H then we match concatenated copies of
830 * quotestr. */
831size_t quote_length(const char *line)
832{
833#ifdef HAVE_REGEX_H
834 regmatch_t matches;
835 int rc = regexec(&quotereg, line, 1, &matches, 0);
836
837 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
838 return 0;
839 /* matches.rm_so should be 0, since the quote string should start
840 * with the caret ^. */
841 return matches.rm_eo;
842#else /* !HAVE_REGEX_H */
843 size_t qdepth = 0;
844
845 /* Compute quote depth level. */
846 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
847 qdepth += quotelen;
848 return qdepth;
849#endif /* !HAVE_REGEX_H */
850}
851
852/* a_line and b_line are lines of text. The quotation part of a_line is
853 * the first a_quote characters. Check that the quotation part of
854 * b_line is the same. */
855bool quotes_match(const char *a_line, size_t a_quote, const char
856 *b_line)
857{
858 /* Here is the assumption about a_quote. */
859 assert(a_quote == quote_length(a_line));
860
861 return (a_quote == quote_length(b_line) &&
862 strncmp(a_line, b_line, a_quote) == 0);
863}
864
865/* We assume a_line and b_line have no quote part. Then, we return
866 * whether b_line could follow a_line in a paragraph. */
867bool indents_match(const char *a_line, size_t a_indent, const char
868 *b_line, size_t b_indent)
869{
870 assert(a_indent == indent_length(a_line));
871 assert(b_indent == indent_length(b_line));
872
873 return (b_indent <= a_indent &&
874 strncmp(a_line, b_line, b_indent) == 0);
875}
876
877/* Is foo the beginning of a paragraph?
878 *
879 * A line of text consists of a "quote part", followed by an
880 * "indentation part", followed by text. The functions quote_length()
881 * and indent_length() calculate these parts.
882 *
883 * A line is "part of a paragraph" if it has a part not in the quote
884 * part or the indentation.
885 *
886 * A line is "the beginning of a paragraph" if it is part of a
887 * paragraph and
888 * 1) it is the top line of the file, or
889 * 2) the line above it is not part of a paragraph, or
890 * 3) the line above it does not have precisely the same quote
891 * part, or
892 * 4) the indentation of this line is not an initial substring of
893 * the indentation of the previous line, or
894 * 5) this line has no quote part and some indentation, and
895 * autoindent isn't turned on.
896 * The reason for number 5) is that if autoindent isn't turned on,
897 * then an indented line is expected to start a paragraph, as in
898 * books. Thus, nano can justify an indented paragraph only if
899 * autoindent is turned on. */
900bool begpar(const filestruct *const foo)
901{
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +0000902 size_t quote_len, indent_len, temp_id_len;
903
904 if (foo == NULL)
905 return FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000906
907 /* Case 1). */
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +0000908 if (foo == openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000909 return TRUE;
910
911 quote_len = quote_length(foo->data);
912 indent_len = indent_length(foo->data + quote_len);
913
914 /* Not part of a paragraph. */
915 if (foo->data[quote_len + indent_len] == '\0')
916 return FALSE;
917
918 /* Case 3). */
919 if (!quotes_match(foo->data, quote_len, foo->prev->data))
920 return TRUE;
921
922 temp_id_len = indent_length(foo->prev->data + quote_len);
923
924 /* Case 2) or 5) or 4). */
925 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
926 (quote_len == 0 && indent_len > 0
927#ifndef NANO_SMALL
928 && !ISSET(AUTOINDENT)
929#endif
930 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
931 foo->data + quote_len, indent_len))
932 return TRUE;
933
934 return FALSE;
935}
936
937/* Is foo inside a paragraph? */
938bool inpar(const filestruct *const foo)
939{
940 size_t quote_len;
941
942 if (foo == NULL)
943 return FALSE;
944
945 quote_len = quote_length(foo->data);
946
947 return foo->data[quote_len + indent_length(foo->data +
948 quote_len)] != '\0';
949}
950
951/* Put the next par_len lines, starting with first_line, into the
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +0000952 * justify buffer, leaving copies of those lines in place. Assume that
953 * par_len is greater than zero, and that there are enough lines after
954 * first_line. Return the new copy of first_line. */
955filestruct *backup_lines(filestruct *first_line, size_t par_len)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000956{
957 filestruct *top = first_line;
958 /* The top of the paragraph we're backing up. */
959 filestruct *bot = first_line;
960 /* The bottom of the paragraph we're backing up. */
961 size_t i;
962 /* Generic loop variable. */
963 size_t current_x_save = openfile->current_x;
964 ssize_t fl_lineno_save = first_line->lineno;
965 ssize_t edittop_lineno_save = openfile->edittop->lineno;
966 ssize_t current_lineno_save = openfile->current->lineno;
967#ifndef NANO_SMALL
968 bool old_mark_set = openfile->mark_set;
969 ssize_t mb_lineno_save = 0;
970 size_t mark_begin_x_save = 0;
971
972 if (old_mark_set) {
973 mb_lineno_save = openfile->mark_begin->lineno;
974 mark_begin_x_save = openfile->mark_begin_x;
975 }
976#endif
977
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +0000978 assert(par_len > 0 && openfile->current->lineno + par_len <=
979 filebot->lineno + 1);
980
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +0000981 /* Move bot down par_len lines to the line after the last line of
982 * the paragraph, if there is one. */
983 for (i = par_len; i > 0 && bot != openfile->filebot; i--)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000984 bot = bot->next;
985
986 /* Move the paragraph from the current buffer's filestruct to the
987 * justify buffer. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +0000988 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
989 strlen(bot->data));
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000990
991 /* Copy the paragraph back to the current buffer's filestruct from
992 * the justify buffer. */
993 copy_from_filestruct(jusbuffer, jusbottom);
994
995 /* Move upward from the last line of the paragraph to the first
996 * line, putting first_line, edittop, current, and mark_begin at the
997 * same lines in the copied paragraph that they had in the original
998 * paragraph. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +0000999 if (openfile->current != openfile->filebot)
1000 top = openfile->current->prev;
1001 else
1002 top = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001003 for (i = par_len; i > 0; i--) {
1004 if (top->lineno == fl_lineno_save)
1005 first_line = top;
1006 if (top->lineno == edittop_lineno_save)
1007 openfile->edittop = top;
1008 if (top->lineno == current_lineno_save)
1009 openfile->current = top;
1010#ifndef NANO_SMALL
1011 if (old_mark_set && top->lineno == mb_lineno_save) {
1012 openfile->mark_begin = top;
1013 openfile->mark_begin_x = mark_begin_x_save;
1014 }
1015#endif
1016 top = top->prev;
1017 }
1018
1019 /* Put current_x at the same place in the copied paragraph that it
1020 * had in the original paragraph. */
1021 openfile->current_x = current_x_save;
1022
1023 set_modified();
1024
1025 return first_line;
1026}
1027
1028/* Find the beginning of the current paragraph if we're in one, or the
1029 * beginning of the next paragraph if we're not. Afterwards, save the
1030 * quote length and paragraph length in *quote and *par. Return TRUE if
1031 * we found a paragraph, or FALSE if there was an error or we didn't
1032 * find a paragraph.
1033 *
1034 * See the comment at begpar() for more about when a line is the
1035 * beginning of a paragraph. */
1036bool find_paragraph(size_t *const quote, size_t *const par)
1037{
1038 size_t quote_len;
1039 /* Length of the initial quotation of the paragraph we search
1040 * for. */
1041 size_t par_len;
1042 /* Number of lines in the paragraph we search for. */
1043 filestruct *current_save;
1044 /* The line at the beginning of the paragraph we search for. */
1045 ssize_t current_y_save;
1046 /* The y-coordinate at the beginning of the paragraph we search
1047 * for. */
1048
1049#ifdef HAVE_REGEX_H
1050 if (quoterc != 0) {
1051 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1052 return FALSE;
1053 }
1054#endif
1055
1056 assert(openfile->current != NULL);
1057
1058 /* Move back to the beginning of the current line. */
1059 openfile->current_x = 0;
1060 openfile->placewewant = 0;
1061
1062 /* 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
1064 * after the last line of the next paragraph. If we end up on the
1065 * same line, or the line before that isn't in a paragraph, it means
1066 * that there aren't any paragraphs left, so get out. Otherwise,
1067 * move back to the last line of the paragraph. If the current line
1068 * is in a paragraph and it isn't the first line of that paragraph,
1069 * move back to the first line. */
1070 if (!inpar(openfile->current)) {
1071 current_save = openfile->current;
1072
1073 do_para_end(FALSE);
1074 if (openfile->current == current_save ||
1075 !inpar(openfile->current->prev))
1076 return FALSE;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00001077 if (openfile->current != openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001078 openfile->current = openfile->current->prev;
1079 }
1080 if (!begpar(openfile->current))
1081 do_para_begin(FALSE);
1082
1083 /* Now current is the first line of the paragraph. Set quote_len to
1084 * the quotation length of that line, and set par_len to the number
1085 * of lines in this paragraph. */
1086 quote_len = quote_length(openfile->current->data);
1087 current_save = openfile->current;
1088 current_y_save = openfile->current_y;
1089 do_para_end(FALSE);
1090 par_len = openfile->current->lineno - current_save->lineno;
1091 openfile->current = current_save;
1092 openfile->current_y = current_y_save;
1093
1094 /* Save the values of quote_len and par_len. */
1095 assert(quote != NULL && par != NULL);
1096
1097 *quote = quote_len;
1098 *par = par_len;
1099
1100 return TRUE;
1101}
1102
1103/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1104 * the current paragraph. */
1105void do_justify(bool full_justify)
1106{
1107 filestruct *first_par_line = NULL;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001108 /* Will be the first line of the justified paragraph. For
1109 * restoring after unjustify. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001110 filestruct *last_par_line;
1111 /* Will be the line containing the newline after the last line
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001112 * of the justified paragraph. Also for restoring after
1113 * unjustify. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001114
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001115 /* We save these variables to be restored if the user
1116 * unjustifies. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001117 size_t current_x_save = openfile->current_x;
1118 size_t pww_save = openfile->placewewant;
1119 ssize_t current_y_save = openfile->current_y;
1120 bool modified_save = openfile->modified;
1121 size_t totsize_save = openfile->totsize;
1122 filestruct *edittop_save = openfile->edittop;
1123 filestruct *current_save = openfile->current;
1124#ifndef NANO_SMALL
1125 filestruct *mark_begin_save = openfile->mark_begin;
1126 size_t mark_begin_x_save = openfile->mark_begin_x;
1127#endif
1128 int kbinput;
1129 bool meta_key, func_key, s_or_t, ran_func, finished;
1130
1131 /* If we're justifying the entire file, start at the beginning. */
1132 if (full_justify)
1133 openfile->current = openfile->fileage;
1134
1135 last_par_line = openfile->current;
1136
1137 while (TRUE) {
1138 size_t i;
1139 /* Generic loop variable. */
1140 size_t quote_len;
1141 /* Length of the initial quotation of the paragraph we
1142 * justify. */
1143 size_t indent_len;
1144 /* Length of the initial indentation of the paragraph we
1145 * justify. */
1146 size_t par_len;
1147 /* Number of lines in the paragraph we justify. */
1148 ssize_t break_pos;
1149 /* Where we will break lines. */
1150 char *indent_string;
1151 /* The first indentation that doesn't match the initial
1152 * indentation of the paragraph we justify. This is put at
1153 * the beginning of every line broken off the first
1154 * justified line of the paragraph. (Note that this works
1155 * because a paragraph can only contain two indentations at
1156 * most: the initial one, and a different one starting on a
1157 * line after the first. See the comment at begpar() for
1158 * more about when a line is part of a paragraph.) */
1159
1160 /* Find the first line of the paragraph to be justified. That
1161 * is the start of this paragraph if we're in one, or the start
1162 * of the next otherwise. Save the quote length and paragraph
1163 * length (number of lines). Don't refresh the screen yet,
1164 * since we'll do that after we justify.
1165 *
1166 * If the search failed, we do one of two things. If we're
1167 * justifying the whole file, we've found at least one
1168 * paragraph, and the search didn't leave us on the last line of
1169 * the file, it means that we should justify all the way to the
1170 * last line of the file, so set the last line of the text to be
1171 * justified to the last line of the file and break out of the
1172 * loop. Otherwise, it means that there are no paragraph(s) to
1173 * justify, so refresh the screen and get out. */
1174 if (!find_paragraph(&quote_len, &par_len)) {
1175 if (full_justify && first_par_line != NULL &&
1176 first_par_line != openfile->filebot) {
1177 last_par_line = openfile->filebot;
1178 break;
1179 } else {
1180 edit_refresh();
1181 return;
1182 }
1183 }
1184
1185 /* If we haven't already done it, copy the original paragraph(s)
1186 * to the justify buffer. */
1187 if (first_par_line == NULL)
1188 first_par_line = backup_lines(openfile->current,
1189 full_justify ? openfile->filebot->lineno -
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001190 openfile->current->lineno : par_len);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001191
1192 /* Initialize indent_string to a blank string. */
1193 indent_string = mallocstrcpy(NULL, "");
1194
1195 /* Find the first indentation in the paragraph that doesn't
1196 * match the indentation of the first line, and save it in
1197 * indent_string. If all the indentations are the same, save
1198 * the indentation of the first line in indent_string. */
1199 {
1200 const filestruct *indent_line = openfile->current;
1201 bool past_first_line = FALSE;
1202
1203 for (i = 0; i < par_len; i++) {
1204 indent_len = quote_len +
1205 indent_length(indent_line->data + quote_len);
1206
1207 if (indent_len != strlen(indent_string)) {
1208 indent_string = mallocstrncpy(indent_string,
1209 indent_line->data, indent_len + 1);
1210 indent_string[indent_len] = '\0';
1211
1212 if (past_first_line)
1213 break;
1214 }
1215
1216 if (indent_line == openfile->current)
1217 past_first_line = TRUE;
1218
1219 indent_line = indent_line->next;
1220 }
1221 }
1222
1223 /* Now tack all the lines of the paragraph together, skipping
1224 * the quoting and indentation on all lines after the first. */
1225 for (i = 0; i < par_len - 1; i++) {
1226 filestruct *next_line = openfile->current->next;
1227 size_t line_len = strlen(openfile->current->data);
1228 size_t next_line_len =
1229 strlen(openfile->current->next->data);
1230
1231 indent_len = quote_len +
1232 indent_length(openfile->current->next->data +
1233 quote_len);
1234
1235 next_line_len -= indent_len;
1236 openfile->totsize -= indent_len;
1237
1238 /* We're just about to tack the next line onto this one. If
1239 * this line isn't empty, make sure it ends in a space. */
1240 if (line_len > 0 &&
1241 openfile->current->data[line_len - 1] != ' ') {
1242 line_len++;
1243 openfile->current->data =
1244 charealloc(openfile->current->data,
1245 line_len + 1);
1246 openfile->current->data[line_len - 1] = ' ';
1247 openfile->current->data[line_len] = '\0';
1248 openfile->totsize++;
1249 }
1250
1251 openfile->current->data =
1252 charealloc(openfile->current->data, line_len +
1253 next_line_len + 1);
1254 strcat(openfile->current->data, next_line->data +
1255 indent_len);
1256
1257 /* Don't destroy edittop! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00001258 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001259 openfile->edittop = openfile->current;
1260
1261#ifndef NANO_SMALL
1262 /* Adjust the mark coordinates to compensate for the change
1263 * in the next line. */
1264 if (openfile->mark_set && openfile->mark_begin ==
1265 next_line) {
1266 openfile->mark_begin = openfile->current;
1267 openfile->mark_begin_x += line_len - indent_len;
1268 }
1269#endif
1270
1271 unlink_node(next_line);
1272 delete_node(next_line);
1273
1274 /* If we've removed the next line, we need to go through
1275 * this line again. */
1276 i--;
1277
1278 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001279 openfile->totsize--;
1280 }
1281
1282 /* Call justify_format() on the paragraph, which will remove
1283 * excess spaces from it and change all blank characters to
1284 * spaces. */
1285 justify_format(openfile->current, quote_len +
1286 indent_length(openfile->current->data + quote_len));
1287
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001288 while (par_len > 0 && strlenpt(openfile->current->data) >
1289 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001290 size_t line_len = strlen(openfile->current->data);
1291
1292 indent_len = strlen(indent_string);
1293
1294 /* If this line is too long, try to wrap it to the next line
1295 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001296 break_pos = break_line(openfile->current->data + indent_len,
1297 fill - strnlenpt(openfile->current->data, indent_len),
1298 FALSE);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001299
1300 /* We can't break the line, or don't need to, so get out. */
1301 if (break_pos == -1 || break_pos + indent_len == line_len)
1302 break;
1303
1304 /* Move forward to the character after the indentation and
1305 * just after the space. */
1306 break_pos += indent_len + 1;
1307
1308 assert(break_pos <= line_len);
1309
1310 /* Make a new line, and copy the text after where we're
1311 * going to break this line to the beginning of the new
1312 * line. */
1313 splice_node(openfile->current,
1314 make_new_node(openfile->current),
1315 openfile->current->next);
1316
1317 /* If this paragraph is non-quoted, and autoindent isn't
1318 * turned on, set the indentation length to zero so that the
1319 * indentation is treated as part of the line. */
1320 if (quote_len == 0
1321#ifndef NANO_SMALL
1322 && !ISSET(AUTOINDENT)
1323#endif
1324 )
1325 indent_len = 0;
1326
1327 /* Copy the text after where we're going to break the
1328 * current line to the next line. */
1329 openfile->current->next->data = charalloc(indent_len + 1 +
1330 line_len - break_pos);
1331 strncpy(openfile->current->next->data, indent_string,
1332 indent_len);
1333 strcpy(openfile->current->next->data + indent_len,
1334 openfile->current->data + break_pos);
1335
1336 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001337 openfile->totsize += indent_len + 1;
1338
1339#ifndef NANO_SMALL
1340 /* Adjust the mark coordinates to compensate for the change
1341 * in the current line. */
1342 if (openfile->mark_set && openfile->mark_begin ==
1343 openfile->current && openfile->mark_begin_x >
1344 break_pos) {
1345 openfile->mark_begin = openfile->current->next;
1346 openfile->mark_begin_x -= break_pos - indent_len;
1347 }
1348#endif
1349
1350 /* Break the current line. */
1351 null_at(&openfile->current->data, break_pos);
1352
1353 /* Go to the next line. */
1354 par_len--;
1355 openfile->current_y++;
1356 openfile->current = openfile->current->next;
1357 }
1358
1359 /* We're done breaking lines, so we don't need indent_string
1360 * anymore. */
1361 free(indent_string);
1362
1363 /* Go to the next line, the line after the last line of the
1364 * paragraph. */
1365 openfile->current_y++;
1366 openfile->current = openfile->current->next;
1367
1368 /* We've just justified a paragraph. If we're not justifying the
1369 * entire file, break out of the loop. Otherwise, continue the
1370 * loop so that we justify all the paragraphs in the file. */
1371 if (!full_justify)
1372 break;
1373 }
1374
1375 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001376 * up. totsize, and current_y have been maintained above. Set
1377 * last_par_line to the new end of the paragraph, update fileage,
1378 * and renumber, since edit_refresh() needs the line numbers to be
1379 * right (but only do the last two if we actually justified
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001380 * something). */
1381 last_par_line = openfile->current;
1382 if (first_par_line != NULL) {
1383 if (first_par_line->prev == NULL)
1384 openfile->fileage = first_par_line;
1385 renumber(first_par_line);
1386 }
1387
1388 edit_refresh();
1389
1390 statusbar(_("Can now UnJustify!"));
1391
1392 /* If constant cursor position display is on, make sure the current
1393 * cursor position will be properly displayed on the statusbar. */
1394 if (ISSET(CONST_UPDATE))
1395 do_cursorpos(TRUE);
1396
1397 /* Display the shortcut list with UnJustify. */
1398 shortcut_init(TRUE);
1399 display_main_list();
1400
1401 /* Now get a keystroke and see if it's unjustify. If not, put back
1402 * the keystroke and return. */
1403 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
1404 &finished, FALSE);
1405
1406 if (!meta_key && !func_key && s_or_t &&
1407 kbinput == NANO_UNJUSTIFY_KEY) {
1408 /* Restore the justify we just did (ungrateful user!). */
1409 openfile->current = current_save;
1410 openfile->current_x = current_x_save;
1411 openfile->placewewant = pww_save;
1412 openfile->current_y = current_y_save;
1413 openfile->edittop = edittop_save;
1414
1415 /* Splice the justify buffer back into the file, but only if we
1416 * actually justified something. */
1417 if (first_par_line != NULL) {
1418 filestruct *top_save;
1419
1420 /* Partition the filestruct so that it contains only the
1421 * text of the justified paragraph. */
1422 filepart = partition_filestruct(first_par_line, 0,
1423 last_par_line, 0);
1424
1425 /* Remove the text of the justified paragraph, and
1426 * put the text in the justify buffer in its place. */
1427 free_filestruct(openfile->fileage);
1428 openfile->fileage = jusbuffer;
1429 openfile->filebot = jusbottom;
1430
1431 top_save = openfile->fileage;
1432
1433 /* Unpartition the filestruct so that it contains all the
1434 * text again. Note that the justified paragraph has been
1435 * replaced with the unjustified paragraph. */
1436 unpartition_filestruct(&filepart);
1437
1438 /* Renumber starting with the beginning line of the old
1439 * partition. */
1440 renumber(top_save);
1441
1442 /* Restore variables from before the justify. */
1443 openfile->totsize = totsize_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001444#ifndef NANO_SMALL
1445 if (openfile->mark_set) {
1446 openfile->mark_begin = mark_begin_save;
1447 openfile->mark_begin_x = mark_begin_x_save;
1448 }
1449#endif
1450 openfile->modified = modified_save;
1451
1452 /* Clear the justify buffer. */
1453 jusbuffer = NULL;
1454
1455 if (!openfile->modified)
1456 titlebar(NULL);
1457 edit_refresh();
1458 }
1459 } else {
1460 unget_kbinput(kbinput, meta_key, func_key);
1461
1462 /* Blow away the text in the justify buffer. */
1463 free_filestruct(jusbuffer);
1464 jusbuffer = NULL;
1465 }
1466
1467 blank_statusbar();
1468
1469 /* Display the shortcut list with UnCut. */
1470 shortcut_init(FALSE);
1471 display_main_list();
1472}
1473
1474void do_justify_void(void)
1475{
1476 do_justify(FALSE);
1477}
1478
1479void do_full_justify(void)
1480{
1481 do_justify(TRUE);
1482}
1483#endif /* !DISABLE_JUSTIFY */
1484
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001485#ifndef DISABLE_SPELLER
1486/* A word is misspelled in the file. Let the user replace it. We
1487 * return FALSE if the user cancels. */
1488bool do_int_spell_fix(const char *word)
1489{
1490 char *save_search, *save_replace;
1491 size_t match_len, current_x_save = openfile->current_x;
1492 size_t pww_save = openfile->placewewant;
1493 filestruct *edittop_save = openfile->edittop;
1494 filestruct *current_save = openfile->current;
1495 /* Save where we are. */
1496 bool canceled = FALSE;
1497 /* The return value. */
1498 bool case_sens_set = ISSET(CASE_SENSITIVE);
1499#ifndef NANO_SMALL
1500 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
1501#endif
1502#ifdef HAVE_REGEX_H
1503 bool regexp_set = ISSET(USE_REGEXP);
1504#endif
1505#ifndef NANO_SMALL
1506 bool old_mark_set = openfile->mark_set;
1507 bool added_magicline = FALSE;
1508 /* Whether we added a magicline after filebot. */
1509 bool right_side_up = FALSE;
1510 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1511 * FALSE if (current, current_x) is. */
1512 filestruct *top, *bot;
1513 size_t top_x, bot_x;
1514#endif
1515
1516 /* Make sure spell-check is case sensitive. */
1517 SET(CASE_SENSITIVE);
1518
1519#ifndef NANO_SMALL
1520 /* Make sure spell-check goes forward only. */
1521 UNSET(BACKWARDS_SEARCH);
1522#endif
1523#ifdef HAVE_REGEX_H
1524 /* Make sure spell-check doesn't use regular expressions. */
1525 UNSET(USE_REGEXP);
1526#endif
1527
1528 /* Save the current search/replace strings. */
1529 search_init_globals();
1530 save_search = last_search;
1531 save_replace = last_replace;
1532
1533 /* Set the search/replace strings to the misspelled word. */
1534 last_search = mallocstrcpy(NULL, word);
1535 last_replace = mallocstrcpy(NULL, word);
1536
1537#ifndef NANO_SMALL
1538 if (old_mark_set) {
1539 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001540 * contains only the marked text; if the NO_NEWLINES flag isn't
1541 * set, keep track of whether the text will have a magicline
1542 * added when we're done correcting misspelled words; and
1543 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001544 mark_order((const filestruct **)&top, &top_x,
1545 (const filestruct **)&bot, &bot_x, &right_side_up);
1546 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001547 if (!ISSET(NO_NEWLINES))
1548 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001549 openfile->mark_set = FALSE;
1550 }
1551#endif
1552
1553 /* Start from the top of the file. */
1554 openfile->edittop = openfile->fileage;
1555 openfile->current = openfile->fileage;
1556 openfile->current_x = (size_t)-1;
1557 openfile->placewewant = 0;
1558
1559 /* Find the first whole-word occurrence of word. */
1560 findnextstr_wrap_reset();
1561 while (findnextstr(TRUE, TRUE, FALSE, openfile->fileage, 0, word,
1562 &match_len)) {
1563 if (is_whole_word(openfile->current_x, openfile->current->data,
1564 word)) {
1565 size_t xpt = xplustabs();
1566 char *exp_word = display_string(openfile->current->data,
1567 xpt, strnlenpt(openfile->current->data,
1568 openfile->current_x + match_len) - xpt, FALSE);
1569
1570 edit_refresh();
1571
1572 do_replace_highlight(TRUE, exp_word);
1573
1574 /* Allow all instances of the word to be corrected. */
David Lawrence Ramseye19449e2005-11-07 21:45:44 +00001575 canceled = (do_prompt(FALSE, spell_list, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001576#ifndef NANO_SMALL
1577 NULL,
1578#endif
1579 _("Edit a replacement")) == -1);
1580
1581 do_replace_highlight(FALSE, exp_word);
1582
1583 free(exp_word);
1584
1585 if (!canceled && strcmp(word, answer) != 0) {
1586 openfile->current_x--;
1587 do_replace_loop(word, openfile->current,
1588 &openfile->current_x, TRUE, &canceled);
1589 }
1590
1591 break;
1592 }
1593 }
1594
1595#ifndef NANO_SMALL
1596 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001597 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
1598 * added a magicline, remove it now. */
1599 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001600 remove_magicline();
1601
1602 /* Put the beginning and the end of the mark at the beginning
1603 * and the end of the spell-checked text. */
1604 if (openfile->fileage == openfile->filebot)
1605 bot_x += top_x;
1606 if (right_side_up) {
1607 openfile->mark_begin_x = top_x;
1608 current_x_save = bot_x;
1609 } else {
1610 current_x_save = top_x;
1611 openfile->mark_begin_x = bot_x;
1612 }
1613
1614 /* Unpartition the filestruct so that it contains all the text
1615 * again, and turn the mark back on. */
1616 unpartition_filestruct(&filepart);
1617 openfile->mark_set = TRUE;
1618 }
1619#endif
1620
1621 /* Restore the search/replace strings. */
1622 free(last_search);
1623 last_search = save_search;
1624 free(last_replace);
1625 last_replace = save_replace;
1626
1627 /* Restore where we were. */
1628 openfile->edittop = edittop_save;
1629 openfile->current = current_save;
1630 openfile->current_x = current_x_save;
1631 openfile->placewewant = pww_save;
1632
1633 /* Restore case sensitivity setting. */
1634 if (!case_sens_set)
1635 UNSET(CASE_SENSITIVE);
1636
1637#ifndef NANO_SMALL
1638 /* Restore search/replace direction. */
1639 if (backwards_search_set)
1640 SET(BACKWARDS_SEARCH);
1641#endif
1642#ifdef HAVE_REGEX_H
1643 /* Restore regular expression usage setting. */
1644 if (regexp_set)
1645 SET(USE_REGEXP);
1646#endif
1647
1648 return !canceled;
1649}
1650
1651/* Integrated spell checking using the spell program, filtered through
1652 * the sort and uniq programs. Return NULL for normal termination,
1653 * and the error string otherwise. */
1654const char *do_int_speller(const char *tempfile_name)
1655{
1656 char *read_buff, *read_buff_ptr, *read_buff_word;
1657 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
1658 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
1659 pid_t pid_spell, pid_sort, pid_uniq;
1660 int spell_status, sort_status, uniq_status;
1661
1662 /* Create all three pipes up front. */
1663 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
1664 pipe(uniq_fd) == -1)
1665 return _("Could not create pipe");
1666
1667 statusbar(_("Creating misspelled word list, please wait..."));
1668
1669 /* A new process to run spell in. */
1670 if ((pid_spell = fork()) == 0) {
1671 /* Child continues (i.e, future spell process). */
1672 close(spell_fd[0]);
1673
1674 /* Replace the standard input with the temp file. */
1675 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1676 goto close_pipes_and_exit;
1677
1678 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1679 goto close_pipes_and_exit;
1680
1681 close(tempfile_fd);
1682
1683 /* Send spell's standard output to the pipe. */
1684 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1685 goto close_pipes_and_exit;
1686
1687 close(spell_fd[1]);
1688
1689 /* Start the spell program; we are using PATH. */
1690 execlp("spell", "spell", NULL);
1691
1692 /* This should not be reached if spell is found. */
1693 exit(1);
1694 }
1695
1696 /* Parent continues here. */
1697 close(spell_fd[1]);
1698
1699 /* A new process to run sort in. */
1700 if ((pid_sort = fork()) == 0) {
1701 /* Child continues (i.e, future spell process). Replace the
1702 * standard input with the standard output of the old pipe. */
1703 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1704 goto close_pipes_and_exit;
1705
1706 close(spell_fd[0]);
1707
1708 /* Send sort's standard output to the new pipe. */
1709 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1710 goto close_pipes_and_exit;
1711
1712 close(sort_fd[1]);
1713
1714 /* Start the sort program. Use -f to remove mixed case. If
1715 * this isn't portable, let me know. */
1716 execlp("sort", "sort", "-f", NULL);
1717
1718 /* This should not be reached if sort is found. */
1719 exit(1);
1720 }
1721
1722 close(spell_fd[0]);
1723 close(sort_fd[1]);
1724
1725 /* A new process to run uniq in. */
1726 if ((pid_uniq = fork()) == 0) {
1727 /* Child continues (i.e, future uniq process). Replace the
1728 * standard input with the standard output of the old pipe. */
1729 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
1730 goto close_pipes_and_exit;
1731
1732 close(sort_fd[0]);
1733
1734 /* Send uniq's standard output to the new pipe. */
1735 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1736 goto close_pipes_and_exit;
1737
1738 close(uniq_fd[1]);
1739
1740 /* Start the uniq program; we are using PATH. */
1741 execlp("uniq", "uniq", NULL);
1742
1743 /* This should not be reached if uniq is found. */
1744 exit(1);
1745 }
1746
1747 close(sort_fd[0]);
1748 close(uniq_fd[1]);
1749
1750 /* The child process was not forked successfully. */
1751 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
1752 close(uniq_fd[0]);
1753 return _("Could not fork");
1754 }
1755
1756 /* Get the system pipe buffer size. */
1757 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
1758 close(uniq_fd[0]);
1759 return _("Could not get size of pipe buffer");
1760 }
1761
1762 /* Read in the returned spelling errors. */
1763 read_buff_read = 0;
1764 read_buff_size = pipe_buff_size + 1;
1765 read_buff = read_buff_ptr = charalloc(read_buff_size);
1766
1767 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
1768 pipe_buff_size)) > 0) {
1769 read_buff_read += bytesread;
1770 read_buff_size += pipe_buff_size;
1771 read_buff = read_buff_ptr = charealloc(read_buff,
1772 read_buff_size);
1773 read_buff_ptr += read_buff_read;
1774 }
1775
1776 *read_buff_ptr = '\0';
1777 close(uniq_fd[0]);
1778
1779 /* Process the spelling errors. */
1780 read_buff_word = read_buff_ptr = read_buff;
1781
1782 while (*read_buff_ptr != '\0') {
1783 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
1784 *read_buff_ptr = '\0';
1785 if (read_buff_word != read_buff_ptr) {
1786 if (!do_int_spell_fix(read_buff_word)) {
1787 read_buff_word = read_buff_ptr;
1788 break;
1789 }
1790 }
1791 read_buff_word = read_buff_ptr + 1;
1792 }
1793 read_buff_ptr++;
1794 }
1795
1796 /* Special case: the last word doesn't end with '\r' or '\n'. */
1797 if (read_buff_word != read_buff_ptr)
1798 do_int_spell_fix(read_buff_word);
1799
1800 free(read_buff);
1801 replace_abort();
1802 edit_refresh();
1803
1804 /* Process the end of the spell process. */
1805 waitpid(pid_spell, &spell_status, 0);
1806 waitpid(pid_sort, &sort_status, 0);
1807 waitpid(pid_uniq, &uniq_status, 0);
1808
1809 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
1810 return _("Error invoking \"spell\"");
1811
1812 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
1813 return _("Error invoking \"sort -f\"");
1814
1815 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
1816 return _("Error invoking \"uniq\"");
1817
1818 /* Otherwise... */
1819 return NULL;
1820
1821 close_pipes_and_exit:
1822 /* Don't leak any handles. */
1823 close(tempfile_fd);
1824 close(spell_fd[0]);
1825 close(spell_fd[1]);
1826 close(sort_fd[0]);
1827 close(sort_fd[1]);
1828 close(uniq_fd[0]);
1829 close(uniq_fd[1]);
1830 exit(1);
1831}
1832
1833/* External spell checking. Return value: NULL for normal termination,
1834 * otherwise the error string. */
1835const char *do_alt_speller(char *tempfile_name)
1836{
1837 int alt_spell_status;
1838 size_t current_x_save = openfile->current_x;
1839 size_t pww_save = openfile->placewewant;
1840 ssize_t current_y_save = openfile->current_y;
1841 ssize_t lineno_save = openfile->current->lineno;
1842 pid_t pid_spell;
1843 char *ptr;
1844 static int arglen = 3;
1845 static char **spellargs = NULL;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001846#ifndef NANO_SMALL
1847 bool old_mark_set = openfile->mark_set;
1848 bool added_magicline = FALSE;
1849 /* Whether we added a magicline after filebot. */
1850 bool right_side_up = FALSE;
1851 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1852 * FALSE if (current, current_x) is. */
1853 filestruct *top, *bot;
1854 size_t top_x, bot_x;
1855 ssize_t mb_lineno_save = 0;
1856 /* We're going to close the current file, and open the output of
1857 * the alternate spell command. The line that mark_begin points
1858 * to will be freed, so we save the line number and restore it
1859 * afterwards. */
1860 size_t totsize_save = openfile->totsize;
1861 /* Our saved value of totsize, used when we spell-check a marked
1862 * selection. */
1863
1864 if (old_mark_set) {
1865 /* If the mark is on, save the number of the line it starts on,
1866 * and then turn the mark off. */
1867 mb_lineno_save = openfile->mark_begin->lineno;
1868 openfile->mark_set = FALSE;
1869 }
1870#endif
1871
1872 endwin();
1873
1874 /* Set up an argument list to pass execvp(). */
1875 if (spellargs == NULL) {
1876 spellargs = (char **)nmalloc(arglen * sizeof(char *));
1877
1878 spellargs[0] = strtok(alt_speller, " ");
1879 while ((ptr = strtok(NULL, " ")) != NULL) {
1880 arglen++;
1881 spellargs = (char **)nrealloc(spellargs, arglen *
1882 sizeof(char *));
1883 spellargs[arglen - 3] = ptr;
1884 }
1885 spellargs[arglen - 1] = NULL;
1886 }
1887 spellargs[arglen - 2] = tempfile_name;
1888
1889 /* Start a new process for the alternate speller. */
1890 if ((pid_spell = fork()) == 0) {
1891 /* Start alternate spell program; we are using PATH. */
1892 execvp(spellargs[0], spellargs);
1893
1894 /* Should not be reached, if alternate speller is found!!! */
1895 exit(1);
1896 }
1897
1898 /* If we couldn't fork, get out. */
1899 if (pid_spell < 0)
1900 return _("Could not fork");
1901
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001902#ifndef NANO_SMALL
1903 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001904 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001905 allow_pending_sigwinch(FALSE);
1906#endif
1907
1908 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001909 wait(&alt_spell_status);
1910
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00001911 /* Reenter curses mode. */
1912 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001913
1914 /* Restore the terminal to its previous state. */
1915 terminal_init();
1916
1917 /* Turn the cursor back on for sure. */
1918 curs_set(1);
1919
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001920 /* The screen might have been resized. If it has, reinitialize all
1921 * the windows based on the new screen dimensions. */
1922 window_init();
1923
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001924 if (!WIFEXITED(alt_spell_status) ||
1925 WEXITSTATUS(alt_spell_status) != 0) {
1926 char *altspell_error;
1927 char *invoke_error = _("Error invoking \"%s\"");
1928
1929#ifndef NANO_SMALL
1930 /* Turn the mark back on if it was on before. */
1931 openfile->mark_set = old_mark_set;
1932#endif
1933
1934 altspell_error =
1935 charalloc(strlen(invoke_error) +
1936 strlen(alt_speller) + 1);
1937 sprintf(altspell_error, invoke_error, alt_speller);
1938 return altspell_error;
1939 }
1940
1941#ifndef NANO_SMALL
1942 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001943 /* If the mark is on, partition the filestruct so that it
1944 * contains only the marked text; if the NO_NEWLINES flag isn't
1945 * set, keep track of whether the text will have a magicline
1946 * added when we're done correcting misspelled words; and
1947 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001948 mark_order((const filestruct **)&top, &top_x,
1949 (const filestruct **)&bot, &bot_x, &right_side_up);
1950 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001951 if (!ISSET(NO_NEWLINES))
1952 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001953
1954 /* Get the number of characters in the marked text, and subtract
1955 * it from the saved value of totsize. */
1956 totsize_save -= get_totsize(top, bot);
1957 }
1958#endif
1959
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00001960 /* Replace the text of the current buffer with the spell-checked
1961 * text. */
1962 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001963
1964#ifndef NANO_SMALL
1965 if (old_mark_set) {
1966 filestruct *top_save = openfile->fileage;
1967
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001968 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
1969 * added a magicline, remove it now. */
1970 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001971 remove_magicline();
1972
1973 /* Put the beginning and the end of the mark at the beginning
1974 * and the end of the spell-checked text. */
1975 if (openfile->fileage == openfile->filebot)
1976 bot_x += top_x;
1977 if (right_side_up) {
1978 openfile->mark_begin_x = top_x;
1979 current_x_save = bot_x;
1980 } else {
1981 current_x_save = top_x;
1982 openfile->mark_begin_x = bot_x;
1983 }
1984
1985 /* Unpartition the filestruct so that it contains all the text
1986 * again. Note that we've replaced the marked text originally
1987 * in the partition with the spell-checked marked text in the
1988 * temp file. */
1989 unpartition_filestruct(&filepart);
1990
1991 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001992 * partition. Also add the number of characters in the
1993 * spell-checked marked text to the saved value of totsize, and
1994 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001995 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001996 totsize_save += openfile->totsize;
1997 openfile->totsize = totsize_save;
1998
1999 /* Assign mark_begin to the line where the mark began before. */
2000 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2001 current_y_save, 0);
2002 openfile->mark_begin = openfile->current;
2003
2004 /* Assign mark_begin_x to the location in mark_begin where the
2005 * mark began before, adjusted for any shortening of the
2006 * line. */
2007 openfile->mark_begin_x = openfile->current_x;
2008
2009 /* Turn the mark back on. */
2010 openfile->mark_set = TRUE;
2011 }
2012#endif
2013
2014 /* Go back to the old position, and mark the file as modified. */
2015 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2016 set_modified();
2017
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002018#ifndef NANO_SMALL
2019 /* Handle a pending SIGWINCH again. */
2020 allow_pending_sigwinch(TRUE);
2021#endif
2022
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002023 return NULL;
2024}
2025
2026void do_spell(void)
2027{
2028 int i;
2029 FILE *temp_file;
2030 char *temp = safe_tempfile(&temp_file);
2031 const char *spell_msg;
2032
2033 if (temp == NULL) {
2034 statusbar(_("Could not create temp file: %s"), strerror(errno));
2035 return;
2036 }
2037
2038#ifndef NANO_SMALL
2039 if (openfile->mark_set)
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002040 i = write_marked_file(temp, temp_file, TRUE, OVERWRITE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002041 else
2042#endif
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002043 i = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002044
2045 if (i == -1) {
2046 statusbar(_("Error writing temp file: %s"), strerror(errno));
2047 free(temp);
2048 return;
2049 }
2050
2051 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2052 do_int_speller(temp);
2053 unlink(temp);
2054 free(temp);
2055
2056 /* If the spell-checker printed any error messages onscreen, make
2057 * sure that they're cleared off. */
2058 total_refresh();
2059
2060 if (spell_msg != NULL) {
2061 if (errno == 0)
2062 /* Don't display an error message of "Success". */
2063 statusbar(_("Spell checking failed: %s"), spell_msg);
2064 else
2065 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2066 strerror(errno));
2067 } else
2068 statusbar(_("Finished checking spelling"));
2069}
2070#endif /* !DISABLE_SPELLER */
2071
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002072#ifndef NANO_SMALL
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002073/* Our own version of "wc". Note that its character counts are in
2074 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002075void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002076{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002077 size_t words = 0, chars = 0;
2078 ssize_t lines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002079 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002080 size_t pww_save = openfile->placewewant;
2081 filestruct *current_save = openfile->current;
2082 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002083 filestruct *top, *bot;
2084 size_t top_x, bot_x;
2085
2086 if (old_mark_set) {
2087 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002088 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002089 mark_order((const filestruct **)&top, &top_x,
2090 (const filestruct **)&bot, &bot_x, NULL);
2091 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002092 openfile->mark_set = FALSE;
2093 }
2094
2095 /* Start at the top of the file. */
2096 openfile->current = openfile->fileage;
2097 openfile->current_x = 0;
2098 openfile->placewewant = 0;
2099
2100 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002101 * part of a word, as "wc -w" does), without updating the screen,
2102 * until we reach the end of the file, incrementing the total word
2103 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002104 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002105 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002106 if (do_next_word(TRUE, FALSE))
2107 words++;
2108 }
2109
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002110 /* Get the total line and character counts, as "wc -l" and "wc -c"
2111 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002112 if (old_mark_set) {
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002113 lines = openfile->filebot->lineno -
2114 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002115 chars = get_totsize(openfile->fileage, openfile->filebot);
2116
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002117 /* Unpartition the filestruct so that it contains all the text
2118 * again, and turn the mark back on. */
2119 unpartition_filestruct(&filepart);
2120 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002121 } else {
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002122 lines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002123 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002124 }
2125
2126 /* Restore where we were. */
2127 openfile->current = current_save;
2128 openfile->current_x = current_x_save;
2129 openfile->placewewant = pww_save;
2130
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002131 /* Display the total word, line, and character counts on the
2132 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002133 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
David Lawrence Ramsey815fb0a2005-08-05 19:38:11 +00002134 _("In Selection: ") : "", (unsigned long)words, (long)lines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002135 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002136}
2137#endif /* !NANO_SMALL */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002138
2139void do_verbatim_input(void)
2140{
2141 int *kbinput;
2142 size_t kbinput_len, i;
2143 char *output;
2144
2145 statusbar(_("Verbatim Input"));
2146
2147 /* If constant cursor position display is on, make sure the current
2148 * cursor position will be properly displayed on the statusbar. */
2149 if (ISSET(CONST_UPDATE))
2150 do_cursorpos(TRUE);
2151
2152 /* Read in all the verbatim characters. */
2153 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2154
2155 /* Display all the verbatim characters at once, not filtering out
2156 * control characters. */
2157 output = charalloc(kbinput_len + 1);
2158
2159 for (i = 0; i < kbinput_len; i++)
2160 output[i] = (char)kbinput[i];
2161 output[i] = '\0';
2162
2163 do_output(output, kbinput_len, TRUE);
2164
2165 free(output);
2166}