blob: 4d6d855bee9a05e1157ec68f16911a4dd8f75e36 [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 Ramsey4ed81342005-11-08 23:08:08 +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 Ramsey4ed81342005-11-08 23:08:08 +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{
902 size_t quote_len;
903 size_t indent_len;
904 size_t temp_id_len;
905
906 /* Case 1). */
907 if (foo->prev == NULL)
908 return TRUE;
909
910 quote_len = quote_length(foo->data);
911 indent_len = indent_length(foo->data + quote_len);
912
913 /* Not part of a paragraph. */
914 if (foo->data[quote_len + indent_len] == '\0')
915 return FALSE;
916
917 /* Case 3). */
918 if (!quotes_match(foo->data, quote_len, foo->prev->data))
919 return TRUE;
920
921 temp_id_len = indent_length(foo->prev->data + quote_len);
922
923 /* Case 2) or 5) or 4). */
924 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
925 (quote_len == 0 && indent_len > 0
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
946 return foo->data[quote_len + indent_length(foo->data +
947 quote_len)] != '\0';
948}
949
950/* Put the next par_len lines, starting with first_line, into the
951 * justify buffer, leaving copies of those lines in place. Assume there
952 * are enough lines after first_line. Return the new copy of
953 * first_line. */
954filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t
955 quote_len)
956{
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
978 /* Move bot down par_len lines to the newline after the last line of
979 * the paragraph. */
980 for (i = par_len; i > 0; i--)
981 bot = bot->next;
982
983 /* Move the paragraph from the current buffer's filestruct to the
984 * justify buffer. */
985 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot, 0);
986
987 /* Copy the paragraph back to the current buffer's filestruct from
988 * the justify buffer. */
989 copy_from_filestruct(jusbuffer, jusbottom);
990
991 /* Move upward from the last line of the paragraph to the first
992 * line, putting first_line, edittop, current, and mark_begin at the
993 * same lines in the copied paragraph that they had in the original
994 * paragraph. */
995 top = openfile->current->prev;
996 for (i = par_len; i > 0; i--) {
997 if (top->lineno == fl_lineno_save)
998 first_line = top;
999 if (top->lineno == edittop_lineno_save)
1000 openfile->edittop = top;
1001 if (top->lineno == current_lineno_save)
1002 openfile->current = top;
1003#ifndef NANO_SMALL
1004 if (old_mark_set && top->lineno == mb_lineno_save) {
1005 openfile->mark_begin = top;
1006 openfile->mark_begin_x = mark_begin_x_save;
1007 }
1008#endif
1009 top = top->prev;
1010 }
1011
1012 /* Put current_x at the same place in the copied paragraph that it
1013 * had in the original paragraph. */
1014 openfile->current_x = current_x_save;
1015
1016 set_modified();
1017
1018 return first_line;
1019}
1020
1021/* Find the beginning of the current paragraph if we're in one, or the
1022 * beginning of the next paragraph if we're not. Afterwards, save the
1023 * quote length and paragraph length in *quote and *par. Return TRUE if
1024 * we found a paragraph, or FALSE if there was an error or we didn't
1025 * find a paragraph.
1026 *
1027 * See the comment at begpar() for more about when a line is the
1028 * beginning of a paragraph. */
1029bool find_paragraph(size_t *const quote, size_t *const par)
1030{
1031 size_t quote_len;
1032 /* Length of the initial quotation of the paragraph we search
1033 * for. */
1034 size_t par_len;
1035 /* Number of lines in the paragraph we search for. */
1036 filestruct *current_save;
1037 /* The line at the beginning of the paragraph we search for. */
1038 ssize_t current_y_save;
1039 /* The y-coordinate at the beginning of the paragraph we search
1040 * for. */
1041
1042#ifdef HAVE_REGEX_H
1043 if (quoterc != 0) {
1044 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1045 return FALSE;
1046 }
1047#endif
1048
1049 assert(openfile->current != NULL);
1050
1051 /* Move back to the beginning of the current line. */
1052 openfile->current_x = 0;
1053 openfile->placewewant = 0;
1054
1055 /* Find the first line of the current or next paragraph. First, if
1056 * the current line isn't in a paragraph, move forward to the line
1057 * after the last line of the next paragraph. If we end up on the
1058 * same line, or the line before that isn't in a paragraph, it means
1059 * that there aren't any paragraphs left, so get out. Otherwise,
1060 * move back to the last line of the paragraph. If the current line
1061 * is in a paragraph and it isn't the first line of that paragraph,
1062 * move back to the first line. */
1063 if (!inpar(openfile->current)) {
1064 current_save = openfile->current;
1065
1066 do_para_end(FALSE);
1067 if (openfile->current == current_save ||
1068 !inpar(openfile->current->prev))
1069 return FALSE;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00001070 if (openfile->current != openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001071 openfile->current = openfile->current->prev;
1072 }
1073 if (!begpar(openfile->current))
1074 do_para_begin(FALSE);
1075
1076 /* Now current is the first line of the paragraph. Set quote_len to
1077 * the quotation length of that line, and set par_len to the number
1078 * of lines in this paragraph. */
1079 quote_len = quote_length(openfile->current->data);
1080 current_save = openfile->current;
1081 current_y_save = openfile->current_y;
1082 do_para_end(FALSE);
1083 par_len = openfile->current->lineno - current_save->lineno;
1084 openfile->current = current_save;
1085 openfile->current_y = current_y_save;
1086
1087 /* Save the values of quote_len and par_len. */
1088 assert(quote != NULL && par != NULL);
1089
1090 *quote = quote_len;
1091 *par = par_len;
1092
1093 return TRUE;
1094}
1095
1096/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1097 * the current paragraph. */
1098void do_justify(bool full_justify)
1099{
1100 filestruct *first_par_line = NULL;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001101 /* Will be the first line of the justified paragraph. For
1102 * restoring after unjustify. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001103 filestruct *last_par_line;
1104 /* Will be the line containing the newline after the last line
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001105 * of the justified paragraph. Also for restoring after
1106 * unjustify. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001107
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001108 /* We save these variables to be restored if the user
1109 * unjustifies. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001110 size_t current_x_save = openfile->current_x;
1111 size_t pww_save = openfile->placewewant;
1112 ssize_t current_y_save = openfile->current_y;
1113 bool modified_save = openfile->modified;
1114 size_t totsize_save = openfile->totsize;
1115 filestruct *edittop_save = openfile->edittop;
1116 filestruct *current_save = openfile->current;
1117#ifndef NANO_SMALL
1118 filestruct *mark_begin_save = openfile->mark_begin;
1119 size_t mark_begin_x_save = openfile->mark_begin_x;
1120#endif
1121 int kbinput;
1122 bool meta_key, func_key, s_or_t, ran_func, finished;
1123
1124 /* If we're justifying the entire file, start at the beginning. */
1125 if (full_justify)
1126 openfile->current = openfile->fileage;
1127
1128 last_par_line = openfile->current;
1129
1130 while (TRUE) {
1131 size_t i;
1132 /* Generic loop variable. */
1133 size_t quote_len;
1134 /* Length of the initial quotation of the paragraph we
1135 * justify. */
1136 size_t indent_len;
1137 /* Length of the initial indentation of the paragraph we
1138 * justify. */
1139 size_t par_len;
1140 /* Number of lines in the paragraph we justify. */
1141 ssize_t break_pos;
1142 /* Where we will break lines. */
1143 char *indent_string;
1144 /* The first indentation that doesn't match the initial
1145 * indentation of the paragraph we justify. This is put at
1146 * the beginning of every line broken off the first
1147 * justified line of the paragraph. (Note that this works
1148 * because a paragraph can only contain two indentations at
1149 * most: the initial one, and a different one starting on a
1150 * line after the first. See the comment at begpar() for
1151 * more about when a line is part of a paragraph.) */
1152
1153 /* Find the first line of the paragraph to be justified. That
1154 * is the start of this paragraph if we're in one, or the start
1155 * of the next otherwise. Save the quote length and paragraph
1156 * length (number of lines). Don't refresh the screen yet,
1157 * since we'll do that after we justify.
1158 *
1159 * If the search failed, we do one of two things. If we're
1160 * justifying the whole file, we've found at least one
1161 * paragraph, and the search didn't leave us on the last line of
1162 * the file, it means that we should justify all the way to the
1163 * last line of the file, so set the last line of the text to be
1164 * justified to the last line of the file and break out of the
1165 * loop. Otherwise, it means that there are no paragraph(s) to
1166 * justify, so refresh the screen and get out. */
1167 if (!find_paragraph(&quote_len, &par_len)) {
1168 if (full_justify && first_par_line != NULL &&
1169 first_par_line != openfile->filebot) {
1170 last_par_line = openfile->filebot;
1171 break;
1172 } else {
1173 edit_refresh();
1174 return;
1175 }
1176 }
1177
1178 /* If we haven't already done it, copy the original paragraph(s)
1179 * to the justify buffer. */
1180 if (first_par_line == NULL)
1181 first_par_line = backup_lines(openfile->current,
1182 full_justify ? openfile->filebot->lineno -
1183 openfile->current->lineno : par_len, quote_len);
1184
1185 /* Initialize indent_string to a blank string. */
1186 indent_string = mallocstrcpy(NULL, "");
1187
1188 /* Find the first indentation in the paragraph that doesn't
1189 * match the indentation of the first line, and save it in
1190 * indent_string. If all the indentations are the same, save
1191 * the indentation of the first line in indent_string. */
1192 {
1193 const filestruct *indent_line = openfile->current;
1194 bool past_first_line = FALSE;
1195
1196 for (i = 0; i < par_len; i++) {
1197 indent_len = quote_len +
1198 indent_length(indent_line->data + quote_len);
1199
1200 if (indent_len != strlen(indent_string)) {
1201 indent_string = mallocstrncpy(indent_string,
1202 indent_line->data, indent_len + 1);
1203 indent_string[indent_len] = '\0';
1204
1205 if (past_first_line)
1206 break;
1207 }
1208
1209 if (indent_line == openfile->current)
1210 past_first_line = TRUE;
1211
1212 indent_line = indent_line->next;
1213 }
1214 }
1215
1216 /* Now tack all the lines of the paragraph together, skipping
1217 * the quoting and indentation on all lines after the first. */
1218 for (i = 0; i < par_len - 1; i++) {
1219 filestruct *next_line = openfile->current->next;
1220 size_t line_len = strlen(openfile->current->data);
1221 size_t next_line_len =
1222 strlen(openfile->current->next->data);
1223
1224 indent_len = quote_len +
1225 indent_length(openfile->current->next->data +
1226 quote_len);
1227
1228 next_line_len -= indent_len;
1229 openfile->totsize -= indent_len;
1230
1231 /* We're just about to tack the next line onto this one. If
1232 * this line isn't empty, make sure it ends in a space. */
1233 if (line_len > 0 &&
1234 openfile->current->data[line_len - 1] != ' ') {
1235 line_len++;
1236 openfile->current->data =
1237 charealloc(openfile->current->data,
1238 line_len + 1);
1239 openfile->current->data[line_len - 1] = ' ';
1240 openfile->current->data[line_len] = '\0';
1241 openfile->totsize++;
1242 }
1243
1244 openfile->current->data =
1245 charealloc(openfile->current->data, line_len +
1246 next_line_len + 1);
1247 strcat(openfile->current->data, next_line->data +
1248 indent_len);
1249
1250 /* Don't destroy edittop! */
1251 if (openfile->edittop == next_line)
1252 openfile->edittop = openfile->current;
1253
1254#ifndef NANO_SMALL
1255 /* Adjust the mark coordinates to compensate for the change
1256 * in the next line. */
1257 if (openfile->mark_set && openfile->mark_begin ==
1258 next_line) {
1259 openfile->mark_begin = openfile->current;
1260 openfile->mark_begin_x += line_len - indent_len;
1261 }
1262#endif
1263
1264 unlink_node(next_line);
1265 delete_node(next_line);
1266
1267 /* If we've removed the next line, we need to go through
1268 * this line again. */
1269 i--;
1270
1271 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001272 openfile->totsize--;
1273 }
1274
1275 /* Call justify_format() on the paragraph, which will remove
1276 * excess spaces from it and change all blank characters to
1277 * spaces. */
1278 justify_format(openfile->current, quote_len +
1279 indent_length(openfile->current->data + quote_len));
1280
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001281 while (par_len > 0 && strlenpt(openfile->current->data) >
1282 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001283 size_t line_len = strlen(openfile->current->data);
1284
1285 indent_len = strlen(indent_string);
1286
1287 /* If this line is too long, try to wrap it to the next line
1288 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001289 break_pos = break_line(openfile->current->data + indent_len,
1290 fill - strnlenpt(openfile->current->data, indent_len),
1291 FALSE);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001292
1293 /* We can't break the line, or don't need to, so get out. */
1294 if (break_pos == -1 || break_pos + indent_len == line_len)
1295 break;
1296
1297 /* Move forward to the character after the indentation and
1298 * just after the space. */
1299 break_pos += indent_len + 1;
1300
1301 assert(break_pos <= line_len);
1302
1303 /* Make a new line, and copy the text after where we're
1304 * going to break this line to the beginning of the new
1305 * line. */
1306 splice_node(openfile->current,
1307 make_new_node(openfile->current),
1308 openfile->current->next);
1309
1310 /* If this paragraph is non-quoted, and autoindent isn't
1311 * turned on, set the indentation length to zero so that the
1312 * indentation is treated as part of the line. */
1313 if (quote_len == 0
1314#ifndef NANO_SMALL
1315 && !ISSET(AUTOINDENT)
1316#endif
1317 )
1318 indent_len = 0;
1319
1320 /* Copy the text after where we're going to break the
1321 * current line to the next line. */
1322 openfile->current->next->data = charalloc(indent_len + 1 +
1323 line_len - break_pos);
1324 strncpy(openfile->current->next->data, indent_string,
1325 indent_len);
1326 strcpy(openfile->current->next->data + indent_len,
1327 openfile->current->data + break_pos);
1328
1329 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001330 openfile->totsize += indent_len + 1;
1331
1332#ifndef NANO_SMALL
1333 /* Adjust the mark coordinates to compensate for the change
1334 * in the current line. */
1335 if (openfile->mark_set && openfile->mark_begin ==
1336 openfile->current && openfile->mark_begin_x >
1337 break_pos) {
1338 openfile->mark_begin = openfile->current->next;
1339 openfile->mark_begin_x -= break_pos - indent_len;
1340 }
1341#endif
1342
1343 /* Break the current line. */
1344 null_at(&openfile->current->data, break_pos);
1345
1346 /* Go to the next line. */
1347 par_len--;
1348 openfile->current_y++;
1349 openfile->current = openfile->current->next;
1350 }
1351
1352 /* We're done breaking lines, so we don't need indent_string
1353 * anymore. */
1354 free(indent_string);
1355
1356 /* Go to the next line, the line after the last line of the
1357 * paragraph. */
1358 openfile->current_y++;
1359 openfile->current = openfile->current->next;
1360
1361 /* We've just justified a paragraph. If we're not justifying the
1362 * entire file, break out of the loop. Otherwise, continue the
1363 * loop so that we justify all the paragraphs in the file. */
1364 if (!full_justify)
1365 break;
1366 }
1367
1368 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001369 * up. totsize, and current_y have been maintained above. Set
1370 * last_par_line to the new end of the paragraph, update fileage,
1371 * and renumber, since edit_refresh() needs the line numbers to be
1372 * right (but only do the last two if we actually justified
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001373 * something). */
1374 last_par_line = openfile->current;
1375 if (first_par_line != NULL) {
1376 if (first_par_line->prev == NULL)
1377 openfile->fileage = first_par_line;
1378 renumber(first_par_line);
1379 }
1380
1381 edit_refresh();
1382
1383 statusbar(_("Can now UnJustify!"));
1384
1385 /* If constant cursor position display is on, make sure the current
1386 * cursor position will be properly displayed on the statusbar. */
1387 if (ISSET(CONST_UPDATE))
1388 do_cursorpos(TRUE);
1389
1390 /* Display the shortcut list with UnJustify. */
1391 shortcut_init(TRUE);
1392 display_main_list();
1393
1394 /* Now get a keystroke and see if it's unjustify. If not, put back
1395 * the keystroke and return. */
1396 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
1397 &finished, FALSE);
1398
1399 if (!meta_key && !func_key && s_or_t &&
1400 kbinput == NANO_UNJUSTIFY_KEY) {
1401 /* Restore the justify we just did (ungrateful user!). */
1402 openfile->current = current_save;
1403 openfile->current_x = current_x_save;
1404 openfile->placewewant = pww_save;
1405 openfile->current_y = current_y_save;
1406 openfile->edittop = edittop_save;
1407
1408 /* Splice the justify buffer back into the file, but only if we
1409 * actually justified something. */
1410 if (first_par_line != NULL) {
1411 filestruct *top_save;
1412
1413 /* Partition the filestruct so that it contains only the
1414 * text of the justified paragraph. */
1415 filepart = partition_filestruct(first_par_line, 0,
1416 last_par_line, 0);
1417
1418 /* Remove the text of the justified paragraph, and
1419 * put the text in the justify buffer in its place. */
1420 free_filestruct(openfile->fileage);
1421 openfile->fileage = jusbuffer;
1422 openfile->filebot = jusbottom;
1423
1424 top_save = openfile->fileage;
1425
1426 /* Unpartition the filestruct so that it contains all the
1427 * text again. Note that the justified paragraph has been
1428 * replaced with the unjustified paragraph. */
1429 unpartition_filestruct(&filepart);
1430
1431 /* Renumber starting with the beginning line of the old
1432 * partition. */
1433 renumber(top_save);
1434
1435 /* Restore variables from before the justify. */
1436 openfile->totsize = totsize_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001437#ifndef NANO_SMALL
1438 if (openfile->mark_set) {
1439 openfile->mark_begin = mark_begin_save;
1440 openfile->mark_begin_x = mark_begin_x_save;
1441 }
1442#endif
1443 openfile->modified = modified_save;
1444
1445 /* Clear the justify buffer. */
1446 jusbuffer = NULL;
1447
1448 if (!openfile->modified)
1449 titlebar(NULL);
1450 edit_refresh();
1451 }
1452 } else {
1453 unget_kbinput(kbinput, meta_key, func_key);
1454
1455 /* Blow away the text in the justify buffer. */
1456 free_filestruct(jusbuffer);
1457 jusbuffer = NULL;
1458 }
1459
1460 blank_statusbar();
1461
1462 /* Display the shortcut list with UnCut. */
1463 shortcut_init(FALSE);
1464 display_main_list();
1465}
1466
1467void do_justify_void(void)
1468{
1469 do_justify(FALSE);
1470}
1471
1472void do_full_justify(void)
1473{
1474 do_justify(TRUE);
1475}
1476#endif /* !DISABLE_JUSTIFY */
1477
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001478#ifndef DISABLE_SPELLER
1479/* A word is misspelled in the file. Let the user replace it. We
1480 * return FALSE if the user cancels. */
1481bool do_int_spell_fix(const char *word)
1482{
1483 char *save_search, *save_replace;
1484 size_t match_len, current_x_save = openfile->current_x;
1485 size_t pww_save = openfile->placewewant;
1486 filestruct *edittop_save = openfile->edittop;
1487 filestruct *current_save = openfile->current;
1488 /* Save where we are. */
1489 bool canceled = FALSE;
1490 /* The return value. */
1491 bool case_sens_set = ISSET(CASE_SENSITIVE);
1492#ifndef NANO_SMALL
1493 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
1494#endif
1495#ifdef HAVE_REGEX_H
1496 bool regexp_set = ISSET(USE_REGEXP);
1497#endif
1498#ifndef NANO_SMALL
1499 bool old_mark_set = openfile->mark_set;
1500 bool added_magicline = FALSE;
1501 /* Whether we added a magicline after filebot. */
1502 bool right_side_up = FALSE;
1503 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1504 * FALSE if (current, current_x) is. */
1505 filestruct *top, *bot;
1506 size_t top_x, bot_x;
1507#endif
1508
1509 /* Make sure spell-check is case sensitive. */
1510 SET(CASE_SENSITIVE);
1511
1512#ifndef NANO_SMALL
1513 /* Make sure spell-check goes forward only. */
1514 UNSET(BACKWARDS_SEARCH);
1515#endif
1516#ifdef HAVE_REGEX_H
1517 /* Make sure spell-check doesn't use regular expressions. */
1518 UNSET(USE_REGEXP);
1519#endif
1520
1521 /* Save the current search/replace strings. */
1522 search_init_globals();
1523 save_search = last_search;
1524 save_replace = last_replace;
1525
1526 /* Set the search/replace strings to the misspelled word. */
1527 last_search = mallocstrcpy(NULL, word);
1528 last_replace = mallocstrcpy(NULL, word);
1529
1530#ifndef NANO_SMALL
1531 if (old_mark_set) {
1532 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001533 * contains only the marked text; if the NO_NEWLINES flag isn't
1534 * set, keep track of whether the text will have a magicline
1535 * added when we're done correcting misspelled words; and
1536 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001537 mark_order((const filestruct **)&top, &top_x,
1538 (const filestruct **)&bot, &bot_x, &right_side_up);
1539 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001540 if (!ISSET(NO_NEWLINES))
1541 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001542 openfile->mark_set = FALSE;
1543 }
1544#endif
1545
1546 /* Start from the top of the file. */
1547 openfile->edittop = openfile->fileage;
1548 openfile->current = openfile->fileage;
1549 openfile->current_x = (size_t)-1;
1550 openfile->placewewant = 0;
1551
1552 /* Find the first whole-word occurrence of word. */
1553 findnextstr_wrap_reset();
1554 while (findnextstr(TRUE, TRUE, FALSE, openfile->fileage, 0, word,
1555 &match_len)) {
1556 if (is_whole_word(openfile->current_x, openfile->current->data,
1557 word)) {
1558 size_t xpt = xplustabs();
1559 char *exp_word = display_string(openfile->current->data,
1560 xpt, strnlenpt(openfile->current->data,
1561 openfile->current_x + match_len) - xpt, FALSE);
1562
1563 edit_refresh();
1564
1565 do_replace_highlight(TRUE, exp_word);
1566
1567 /* Allow all instances of the word to be corrected. */
David Lawrence Ramseye19449e2005-11-07 21:45:44 +00001568 canceled = (do_prompt(FALSE, spell_list, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001569#ifndef NANO_SMALL
1570 NULL,
1571#endif
1572 _("Edit a replacement")) == -1);
1573
1574 do_replace_highlight(FALSE, exp_word);
1575
1576 free(exp_word);
1577
1578 if (!canceled && strcmp(word, answer) != 0) {
1579 openfile->current_x--;
1580 do_replace_loop(word, openfile->current,
1581 &openfile->current_x, TRUE, &canceled);
1582 }
1583
1584 break;
1585 }
1586 }
1587
1588#ifndef NANO_SMALL
1589 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001590 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
1591 * added a magicline, remove it now. */
1592 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001593 remove_magicline();
1594
1595 /* Put the beginning and the end of the mark at the beginning
1596 * and the end of the spell-checked text. */
1597 if (openfile->fileage == openfile->filebot)
1598 bot_x += top_x;
1599 if (right_side_up) {
1600 openfile->mark_begin_x = top_x;
1601 current_x_save = bot_x;
1602 } else {
1603 current_x_save = top_x;
1604 openfile->mark_begin_x = bot_x;
1605 }
1606
1607 /* Unpartition the filestruct so that it contains all the text
1608 * again, and turn the mark back on. */
1609 unpartition_filestruct(&filepart);
1610 openfile->mark_set = TRUE;
1611 }
1612#endif
1613
1614 /* Restore the search/replace strings. */
1615 free(last_search);
1616 last_search = save_search;
1617 free(last_replace);
1618 last_replace = save_replace;
1619
1620 /* Restore where we were. */
1621 openfile->edittop = edittop_save;
1622 openfile->current = current_save;
1623 openfile->current_x = current_x_save;
1624 openfile->placewewant = pww_save;
1625
1626 /* Restore case sensitivity setting. */
1627 if (!case_sens_set)
1628 UNSET(CASE_SENSITIVE);
1629
1630#ifndef NANO_SMALL
1631 /* Restore search/replace direction. */
1632 if (backwards_search_set)
1633 SET(BACKWARDS_SEARCH);
1634#endif
1635#ifdef HAVE_REGEX_H
1636 /* Restore regular expression usage setting. */
1637 if (regexp_set)
1638 SET(USE_REGEXP);
1639#endif
1640
1641 return !canceled;
1642}
1643
1644/* Integrated spell checking using the spell program, filtered through
1645 * the sort and uniq programs. Return NULL for normal termination,
1646 * and the error string otherwise. */
1647const char *do_int_speller(const char *tempfile_name)
1648{
1649 char *read_buff, *read_buff_ptr, *read_buff_word;
1650 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
1651 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
1652 pid_t pid_spell, pid_sort, pid_uniq;
1653 int spell_status, sort_status, uniq_status;
1654
1655 /* Create all three pipes up front. */
1656 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
1657 pipe(uniq_fd) == -1)
1658 return _("Could not create pipe");
1659
1660 statusbar(_("Creating misspelled word list, please wait..."));
1661
1662 /* A new process to run spell in. */
1663 if ((pid_spell = fork()) == 0) {
1664 /* Child continues (i.e, future spell process). */
1665 close(spell_fd[0]);
1666
1667 /* Replace the standard input with the temp file. */
1668 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1669 goto close_pipes_and_exit;
1670
1671 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1672 goto close_pipes_and_exit;
1673
1674 close(tempfile_fd);
1675
1676 /* Send spell's standard output to the pipe. */
1677 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1678 goto close_pipes_and_exit;
1679
1680 close(spell_fd[1]);
1681
1682 /* Start the spell program; we are using PATH. */
1683 execlp("spell", "spell", NULL);
1684
1685 /* This should not be reached if spell is found. */
1686 exit(1);
1687 }
1688
1689 /* Parent continues here. */
1690 close(spell_fd[1]);
1691
1692 /* A new process to run sort in. */
1693 if ((pid_sort = fork()) == 0) {
1694 /* Child continues (i.e, future spell process). Replace the
1695 * standard input with the standard output of the old pipe. */
1696 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1697 goto close_pipes_and_exit;
1698
1699 close(spell_fd[0]);
1700
1701 /* Send sort's standard output to the new pipe. */
1702 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1703 goto close_pipes_and_exit;
1704
1705 close(sort_fd[1]);
1706
1707 /* Start the sort program. Use -f to remove mixed case. If
1708 * this isn't portable, let me know. */
1709 execlp("sort", "sort", "-f", NULL);
1710
1711 /* This should not be reached if sort is found. */
1712 exit(1);
1713 }
1714
1715 close(spell_fd[0]);
1716 close(sort_fd[1]);
1717
1718 /* A new process to run uniq in. */
1719 if ((pid_uniq = fork()) == 0) {
1720 /* Child continues (i.e, future uniq process). Replace the
1721 * standard input with the standard output of the old pipe. */
1722 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
1723 goto close_pipes_and_exit;
1724
1725 close(sort_fd[0]);
1726
1727 /* Send uniq's standard output to the new pipe. */
1728 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1729 goto close_pipes_and_exit;
1730
1731 close(uniq_fd[1]);
1732
1733 /* Start the uniq program; we are using PATH. */
1734 execlp("uniq", "uniq", NULL);
1735
1736 /* This should not be reached if uniq is found. */
1737 exit(1);
1738 }
1739
1740 close(sort_fd[0]);
1741 close(uniq_fd[1]);
1742
1743 /* The child process was not forked successfully. */
1744 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
1745 close(uniq_fd[0]);
1746 return _("Could not fork");
1747 }
1748
1749 /* Get the system pipe buffer size. */
1750 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
1751 close(uniq_fd[0]);
1752 return _("Could not get size of pipe buffer");
1753 }
1754
1755 /* Read in the returned spelling errors. */
1756 read_buff_read = 0;
1757 read_buff_size = pipe_buff_size + 1;
1758 read_buff = read_buff_ptr = charalloc(read_buff_size);
1759
1760 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
1761 pipe_buff_size)) > 0) {
1762 read_buff_read += bytesread;
1763 read_buff_size += pipe_buff_size;
1764 read_buff = read_buff_ptr = charealloc(read_buff,
1765 read_buff_size);
1766 read_buff_ptr += read_buff_read;
1767 }
1768
1769 *read_buff_ptr = '\0';
1770 close(uniq_fd[0]);
1771
1772 /* Process the spelling errors. */
1773 read_buff_word = read_buff_ptr = read_buff;
1774
1775 while (*read_buff_ptr != '\0') {
1776 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
1777 *read_buff_ptr = '\0';
1778 if (read_buff_word != read_buff_ptr) {
1779 if (!do_int_spell_fix(read_buff_word)) {
1780 read_buff_word = read_buff_ptr;
1781 break;
1782 }
1783 }
1784 read_buff_word = read_buff_ptr + 1;
1785 }
1786 read_buff_ptr++;
1787 }
1788
1789 /* Special case: the last word doesn't end with '\r' or '\n'. */
1790 if (read_buff_word != read_buff_ptr)
1791 do_int_spell_fix(read_buff_word);
1792
1793 free(read_buff);
1794 replace_abort();
1795 edit_refresh();
1796
1797 /* Process the end of the spell process. */
1798 waitpid(pid_spell, &spell_status, 0);
1799 waitpid(pid_sort, &sort_status, 0);
1800 waitpid(pid_uniq, &uniq_status, 0);
1801
1802 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
1803 return _("Error invoking \"spell\"");
1804
1805 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
1806 return _("Error invoking \"sort -f\"");
1807
1808 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
1809 return _("Error invoking \"uniq\"");
1810
1811 /* Otherwise... */
1812 return NULL;
1813
1814 close_pipes_and_exit:
1815 /* Don't leak any handles. */
1816 close(tempfile_fd);
1817 close(spell_fd[0]);
1818 close(spell_fd[1]);
1819 close(sort_fd[0]);
1820 close(sort_fd[1]);
1821 close(uniq_fd[0]);
1822 close(uniq_fd[1]);
1823 exit(1);
1824}
1825
1826/* External spell checking. Return value: NULL for normal termination,
1827 * otherwise the error string. */
1828const char *do_alt_speller(char *tempfile_name)
1829{
1830 int alt_spell_status;
1831 size_t current_x_save = openfile->current_x;
1832 size_t pww_save = openfile->placewewant;
1833 ssize_t current_y_save = openfile->current_y;
1834 ssize_t lineno_save = openfile->current->lineno;
1835 pid_t pid_spell;
1836 char *ptr;
1837 static int arglen = 3;
1838 static char **spellargs = NULL;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001839#ifndef NANO_SMALL
1840 bool old_mark_set = openfile->mark_set;
1841 bool added_magicline = FALSE;
1842 /* Whether we added a magicline after filebot. */
1843 bool right_side_up = FALSE;
1844 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1845 * FALSE if (current, current_x) is. */
1846 filestruct *top, *bot;
1847 size_t top_x, bot_x;
1848 ssize_t mb_lineno_save = 0;
1849 /* We're going to close the current file, and open the output of
1850 * the alternate spell command. The line that mark_begin points
1851 * to will be freed, so we save the line number and restore it
1852 * afterwards. */
1853 size_t totsize_save = openfile->totsize;
1854 /* Our saved value of totsize, used when we spell-check a marked
1855 * selection. */
1856
1857 if (old_mark_set) {
1858 /* If the mark is on, save the number of the line it starts on,
1859 * and then turn the mark off. */
1860 mb_lineno_save = openfile->mark_begin->lineno;
1861 openfile->mark_set = FALSE;
1862 }
1863#endif
1864
1865 endwin();
1866
1867 /* Set up an argument list to pass execvp(). */
1868 if (spellargs == NULL) {
1869 spellargs = (char **)nmalloc(arglen * sizeof(char *));
1870
1871 spellargs[0] = strtok(alt_speller, " ");
1872 while ((ptr = strtok(NULL, " ")) != NULL) {
1873 arglen++;
1874 spellargs = (char **)nrealloc(spellargs, arglen *
1875 sizeof(char *));
1876 spellargs[arglen - 3] = ptr;
1877 }
1878 spellargs[arglen - 1] = NULL;
1879 }
1880 spellargs[arglen - 2] = tempfile_name;
1881
1882 /* Start a new process for the alternate speller. */
1883 if ((pid_spell = fork()) == 0) {
1884 /* Start alternate spell program; we are using PATH. */
1885 execvp(spellargs[0], spellargs);
1886
1887 /* Should not be reached, if alternate speller is found!!! */
1888 exit(1);
1889 }
1890
1891 /* If we couldn't fork, get out. */
1892 if (pid_spell < 0)
1893 return _("Could not fork");
1894
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001895#ifndef NANO_SMALL
1896 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001897 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001898 allow_pending_sigwinch(FALSE);
1899#endif
1900
1901 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001902 wait(&alt_spell_status);
1903
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00001904 /* Reenter curses mode. */
1905 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001906
1907 /* Restore the terminal to its previous state. */
1908 terminal_init();
1909
1910 /* Turn the cursor back on for sure. */
1911 curs_set(1);
1912
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001913 /* The screen might have been resized. If it has, reinitialize all
1914 * the windows based on the new screen dimensions. */
1915 window_init();
1916
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001917 if (!WIFEXITED(alt_spell_status) ||
1918 WEXITSTATUS(alt_spell_status) != 0) {
1919 char *altspell_error;
1920 char *invoke_error = _("Error invoking \"%s\"");
1921
1922#ifndef NANO_SMALL
1923 /* Turn the mark back on if it was on before. */
1924 openfile->mark_set = old_mark_set;
1925#endif
1926
1927 altspell_error =
1928 charalloc(strlen(invoke_error) +
1929 strlen(alt_speller) + 1);
1930 sprintf(altspell_error, invoke_error, alt_speller);
1931 return altspell_error;
1932 }
1933
1934#ifndef NANO_SMALL
1935 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001936 /* If the mark is on, partition the filestruct so that it
1937 * contains only the marked text; if the NO_NEWLINES flag isn't
1938 * set, keep track of whether the text will have a magicline
1939 * added when we're done correcting misspelled words; and
1940 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001941 mark_order((const filestruct **)&top, &top_x,
1942 (const filestruct **)&bot, &bot_x, &right_side_up);
1943 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001944 if (!ISSET(NO_NEWLINES))
1945 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001946
1947 /* Get the number of characters in the marked text, and subtract
1948 * it from the saved value of totsize. */
1949 totsize_save -= get_totsize(top, bot);
1950 }
1951#endif
1952
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00001953 /* Replace the text of the current buffer with the spell-checked
1954 * text. */
1955 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001956
1957#ifndef NANO_SMALL
1958 if (old_mark_set) {
1959 filestruct *top_save = openfile->fileage;
1960
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001961 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
1962 * added a magicline, remove it now. */
1963 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001964 remove_magicline();
1965
1966 /* Put the beginning and the end of the mark at the beginning
1967 * and the end of the spell-checked text. */
1968 if (openfile->fileage == openfile->filebot)
1969 bot_x += top_x;
1970 if (right_side_up) {
1971 openfile->mark_begin_x = top_x;
1972 current_x_save = bot_x;
1973 } else {
1974 current_x_save = top_x;
1975 openfile->mark_begin_x = bot_x;
1976 }
1977
1978 /* Unpartition the filestruct so that it contains all the text
1979 * again. Note that we've replaced the marked text originally
1980 * in the partition with the spell-checked marked text in the
1981 * temp file. */
1982 unpartition_filestruct(&filepart);
1983
1984 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001985 * partition. Also add the number of characters in the
1986 * spell-checked marked text to the saved value of totsize, and
1987 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001988 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001989 totsize_save += openfile->totsize;
1990 openfile->totsize = totsize_save;
1991
1992 /* Assign mark_begin to the line where the mark began before. */
1993 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
1994 current_y_save, 0);
1995 openfile->mark_begin = openfile->current;
1996
1997 /* Assign mark_begin_x to the location in mark_begin where the
1998 * mark began before, adjusted for any shortening of the
1999 * line. */
2000 openfile->mark_begin_x = openfile->current_x;
2001
2002 /* Turn the mark back on. */
2003 openfile->mark_set = TRUE;
2004 }
2005#endif
2006
2007 /* Go back to the old position, and mark the file as modified. */
2008 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2009 set_modified();
2010
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002011#ifndef NANO_SMALL
2012 /* Handle a pending SIGWINCH again. */
2013 allow_pending_sigwinch(TRUE);
2014#endif
2015
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002016 return NULL;
2017}
2018
2019void do_spell(void)
2020{
2021 int i;
2022 FILE *temp_file;
2023 char *temp = safe_tempfile(&temp_file);
2024 const char *spell_msg;
2025
2026 if (temp == NULL) {
2027 statusbar(_("Could not create temp file: %s"), strerror(errno));
2028 return;
2029 }
2030
2031#ifndef NANO_SMALL
2032 if (openfile->mark_set)
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002033 i = write_marked_file(temp, temp_file, TRUE, OVERWRITE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002034 else
2035#endif
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002036 i = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002037
2038 if (i == -1) {
2039 statusbar(_("Error writing temp file: %s"), strerror(errno));
2040 free(temp);
2041 return;
2042 }
2043
2044 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2045 do_int_speller(temp);
2046 unlink(temp);
2047 free(temp);
2048
2049 /* If the spell-checker printed any error messages onscreen, make
2050 * sure that they're cleared off. */
2051 total_refresh();
2052
2053 if (spell_msg != NULL) {
2054 if (errno == 0)
2055 /* Don't display an error message of "Success". */
2056 statusbar(_("Spell checking failed: %s"), spell_msg);
2057 else
2058 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2059 strerror(errno));
2060 } else
2061 statusbar(_("Finished checking spelling"));
2062}
2063#endif /* !DISABLE_SPELLER */
2064
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002065#ifndef NANO_SMALL
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002066/* Our own version of "wc". Note that its character counts are in
2067 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002068void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002069{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002070 size_t words = 0, chars = 0;
2071 ssize_t lines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002072 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002073 size_t pww_save = openfile->placewewant;
2074 filestruct *current_save = openfile->current;
2075 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002076 filestruct *top, *bot;
2077 size_t top_x, bot_x;
2078
2079 if (old_mark_set) {
2080 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002081 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002082 mark_order((const filestruct **)&top, &top_x,
2083 (const filestruct **)&bot, &bot_x, NULL);
2084 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002085 openfile->mark_set = FALSE;
2086 }
2087
2088 /* Start at the top of the file. */
2089 openfile->current = openfile->fileage;
2090 openfile->current_x = 0;
2091 openfile->placewewant = 0;
2092
2093 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002094 * part of a word, as "wc -w" does), without updating the screen,
2095 * until we reach the end of the file, incrementing the total word
2096 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002097 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002098 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002099 if (do_next_word(TRUE, FALSE))
2100 words++;
2101 }
2102
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002103 /* Get the total line and character counts, as "wc -l" and "wc -c"
2104 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002105 if (old_mark_set) {
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002106 lines = openfile->filebot->lineno -
2107 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002108 chars = get_totsize(openfile->fileage, openfile->filebot);
2109
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002110 /* Unpartition the filestruct so that it contains all the text
2111 * again, and turn the mark back on. */
2112 unpartition_filestruct(&filepart);
2113 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002114 } else {
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002115 lines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002116 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002117 }
2118
2119 /* Restore where we were. */
2120 openfile->current = current_save;
2121 openfile->current_x = current_x_save;
2122 openfile->placewewant = pww_save;
2123
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002124 /* Display the total word, line, and character counts on the
2125 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002126 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
David Lawrence Ramsey815fb0a2005-08-05 19:38:11 +00002127 _("In Selection: ") : "", (unsigned long)words, (long)lines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002128 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002129}
2130#endif /* !NANO_SMALL */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002131
2132void do_verbatim_input(void)
2133{
2134 int *kbinput;
2135 size_t kbinput_len, i;
2136 char *output;
2137
2138 statusbar(_("Verbatim Input"));
2139
2140 /* If constant cursor position display is on, make sure the current
2141 * cursor position will be properly displayed on the statusbar. */
2142 if (ISSET(CONST_UPDATE))
2143 do_cursorpos(TRUE);
2144
2145 /* Read in all the verbatim characters. */
2146 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2147
2148 /* Display all the verbatim characters at once, not filtering out
2149 * control characters. */
2150 output = charalloc(kbinput_len + 1);
2151
2152 for (i = 0; i < kbinput_len; i++)
2153 output[i] = (char)kbinput[i];
2154 output[i] = '\0';
2155
2156 do_output(output, kbinput_len, TRUE);
2157
2158 free(output);
2159}