blob: 3af8794810ad406d217cc499fad9c6566c1e37e3 [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 * *
David Lawrence Ramsey315eb322005-11-28 19:35:29 +00005 * Copyright (C) 1999-2004 Chris Allegretta *
6 * Copyright (C) 2005 David Lawrence Ramsey *
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00007 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2, or (at your option) *
10 * any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
20 * 02110-1301, USA. *
21 * *
22 **************************************************************************/
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
David Lawrence Ramseyee11c6a2005-11-02 19:42:02 +000028#include <stdio.h>
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000029#include <signal.h>
30#include <unistd.h>
31#include <string.h>
32#include <fcntl.h>
33#include <sys/wait.h>
34#include <errno.h>
35#include "proto.h"
36
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000037#ifndef NANO_TINY
David Lawrence Ramsey8779a172005-11-08 16:45:22 +000038static pid_t pid = -1;
39 /* The PID of the newly forked process in execute_command(), for
40 * use with the cancel_command() signal handler. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000041#endif
42#ifndef DISABLE_WRAPPING
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +000043static bool prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000044 /* Should we prepend wrapped text to the next line? */
45#endif
46#ifndef DISABLE_JUSTIFY
47static filestruct *jusbottom = NULL;
David Lawrence Ramseyae4c3a62005-09-20 19:46:39 +000048 /* Pointer to the end of the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000049#endif
50
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000051#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000052void do_mark(void)
53{
54 openfile->mark_set = !openfile->mark_set;
55 if (openfile->mark_set) {
56 statusbar(_("Mark Set"));
57 openfile->mark_begin = openfile->current;
58 openfile->mark_begin_x = openfile->current_x;
59 } else {
60 statusbar(_("Mark UNset"));
61 openfile->mark_begin = NULL;
62 openfile->mark_begin_x = 0;
63 edit_refresh();
64 }
65}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000066#endif /* !NANO_TINY */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000067
68void do_delete(void)
69{
70 bool do_refresh = FALSE;
71 /* Do we have to call edit_refresh(), or can we get away with
72 * update_line()? */
73
74 assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data));
75
76 openfile->placewewant = xplustabs();
77
78 if (openfile->current->data[openfile->current_x] != '\0') {
79 int char_buf_len = parse_mbchar(openfile->current->data +
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +000080 openfile->current_x, NULL, NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000081 size_t line_len = strlen(openfile->current->data +
82 openfile->current_x);
83
84 assert(openfile->current_x < strlen(openfile->current->data));
85
86 /* Let's get dangerous. */
87 charmove(&openfile->current->data[openfile->current_x],
88 &openfile->current->data[openfile->current_x +
89 char_buf_len], line_len - char_buf_len + 1);
90
91 null_at(&openfile->current->data, openfile->current_x +
92 line_len - char_buf_len);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000093#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000094 if (openfile->mark_set && openfile->mark_begin ==
95 openfile->current && openfile->current_x <
96 openfile->mark_begin_x)
97 openfile->mark_begin_x -= char_buf_len;
98#endif
99 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000100 } else if (openfile->current != openfile->filebot) {
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000101 filestruct *foo = openfile->current->next;
102
103 assert(openfile->current_x == strlen(openfile->current->data));
104
105 /* If we're deleting at the end of a line, we need to call
106 * edit_refresh(). */
107 if (openfile->current->data[openfile->current_x] == '\0')
108 do_refresh = TRUE;
109
110 openfile->current->data = charealloc(openfile->current->data,
111 openfile->current_x + strlen(foo->data) + 1);
112 strcpy(openfile->current->data + openfile->current_x,
113 foo->data);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000114#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000115 if (openfile->mark_set && openfile->mark_begin ==
116 openfile->current->next) {
117 openfile->mark_begin = openfile->current;
118 openfile->mark_begin_x += openfile->current_x;
119 }
120#endif
121 if (openfile->filebot == foo)
122 openfile->filebot = openfile->current;
123
124 unlink_node(foo);
125 delete_node(foo);
126 renumber(openfile->current);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000127 openfile->totsize--;
128#ifndef DISABLE_WRAPPING
129 wrap_reset();
130#endif
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000131
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000132 /* If the NO_NEWLINES flag isn't set, and text has been added to
133 * the magicline as a result of deleting at the end of the line
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000134 * before filebot, add a new magicline. */
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000135 if (!ISSET(NO_NEWLINES) && openfile->current ==
136 openfile->filebot && openfile->current->data[0] != '\0')
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000137 new_magicline();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000138 } else
139 return;
140
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000141 set_modified();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000142
143#ifdef ENABLE_COLOR
144 /* If color syntaxes are available and turned on, we need to call
145 * edit_refresh(). */
146 if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX))
147 do_refresh = TRUE;
148#endif
149
150 if (do_refresh)
151 edit_refresh();
152 else
153 update_line(openfile->current, openfile->current_x);
154}
155
156void do_backspace(void)
157{
158 if (openfile->current != openfile->fileage ||
159 openfile->current_x > 0) {
David Lawrence Ramsey1c3bfa92005-09-13 04:53:44 +0000160 do_left();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000161 do_delete();
162 }
163}
164
165void do_tab(void)
166{
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000167#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000168 if (ISSET(TABS_TO_SPACES)) {
169 char *output;
David Lawrence Ramsey90b07fc2005-10-07 15:57:48 +0000170 size_t output_len = 0, new_pww = xplustabs();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000171
172 do {
173 new_pww++;
174 output_len++;
175 } while (new_pww % tabsize != 0);
176
177 output = charalloc(output_len + 1);
178
179 charset(output, ' ', output_len);
180 output[output_len] = '\0';
181
182 do_output(output, output_len, TRUE);
183
184 free(output);
185 } else {
186#endif
187 do_output("\t", 1, TRUE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000188#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000189 }
190#endif
191}
192
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000193/* Someone hits Enter *gasp!* */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000194void do_enter(void)
195{
196 filestruct *newnode = make_new_node(openfile->current);
197 size_t extra = 0;
198
199 assert(openfile->current != NULL && openfile->current->data != NULL);
200
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000201#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000202 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
203 if (ISSET(AUTOINDENT)) {
204 /* If we are breaking the line in the indentation, the new
205 * indentation should have only current_x characters, and
206 * current_x should not change. */
207 extra = indent_length(openfile->current->data);
208 if (extra > openfile->current_x)
209 extra = openfile->current_x;
210 }
211#endif
212 newnode->data = charalloc(strlen(openfile->current->data +
213 openfile->current_x) + extra + 1);
214 strcpy(&newnode->data[extra], openfile->current->data +
215 openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000216#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000217 if (ISSET(AUTOINDENT)) {
218 strncpy(newnode->data, openfile->current->data, extra);
219 openfile->totsize += mbstrlen(newnode->data);
220 }
221#endif
222 null_at(&openfile->current->data, openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000223#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000224 if (openfile->mark_set && openfile->current ==
225 openfile->mark_begin && openfile->current_x <
226 openfile->mark_begin_x) {
227 openfile->mark_begin = newnode;
228 openfile->mark_begin_x += extra - openfile->current_x;
229 }
230#endif
231 openfile->current_x = extra;
232
233 if (openfile->current == openfile->filebot)
234 openfile->filebot = newnode;
235 splice_node(openfile->current, newnode,
236 openfile->current->next);
237
238 renumber(openfile->current);
239 openfile->current = newnode;
240
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000241 openfile->totsize++;
242 set_modified();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000243
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000244 openfile->placewewant = xplustabs();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000245
246 edit_refresh();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000247}
248
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000249#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000250void cancel_command(int signal)
251{
252 if (kill(pid, SIGKILL) == -1)
253 nperror("kill");
254}
255
David Lawrence Ramsey0ed71712005-11-08 23:09:47 +0000256/* Execute command in a shell. Return TRUE on success. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000257bool execute_command(const char *command)
258{
259 int fd[2];
260 FILE *f;
261 struct sigaction oldaction, newaction;
262 /* Original and temporary handlers for SIGINT. */
263 bool sig_failed = FALSE;
264 /* Did sigaction() fail without changing the signal handlers? */
265
266 /* Make our pipes. */
267 if (pipe(fd) == -1) {
268 statusbar(_("Could not pipe"));
269 return FALSE;
270 }
271
272 /* Fork a child. */
273 if ((pid = fork()) == 0) {
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000274 char *shellenv;
275
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000276 close(fd[0]);
277 dup2(fd[1], fileno(stdout));
278 dup2(fd[1], fileno(stderr));
279
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000280 /* Check $SHELL for the shell to use. If it isn't set, use
281 * /bin/sh. */
282 shellenv = getenv("SHELL");
283 if (shellenv == NULL)
284 shellenv = "/bin/sh";
285
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000286 /* If execl() returns at all, there was an error. */
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000287 execl(shellenv, tail(shellenv), "-c", command, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000288 exit(0);
289 }
290
291 /* Else continue as parent. */
292 close(fd[1]);
293
294 if (pid == -1) {
295 close(fd[0]);
296 statusbar(_("Could not fork"));
297 return FALSE;
298 }
299
300 /* Before we start reading the forked command's output, we set
301 * things up so that Ctrl-C will cancel the new process. */
302
303 /* Enable interpretation of the special control keys so that we get
304 * SIGINT when Ctrl-C is pressed. */
305 enable_signals();
306
307 if (sigaction(SIGINT, NULL, &newaction) == -1) {
308 sig_failed = TRUE;
309 nperror("sigaction");
310 } else {
311 newaction.sa_handler = cancel_command;
312 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
313 sig_failed = TRUE;
314 nperror("sigaction");
315 }
316 }
317
318 /* Note that now oldaction is the previous SIGINT signal handler,
319 * to be restored later. */
320
321 f = fdopen(fd[0], "rb");
322 if (f == NULL)
323 nperror("fdopen");
324
325 read_file(f, "stdin");
326
327 /* If multibuffer mode is on, we could be here in view mode. If so,
328 * don't set the modification flag. */
329 if (!ISSET(VIEW_MODE))
330 set_modified();
331
332 if (wait(NULL) == -1)
333 nperror("wait");
334
335 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
336 nperror("sigaction");
337
338 /* Disable interpretation of the special control keys so that we can
339 * use Ctrl-C for other things. */
340 disable_signals();
341
342 return TRUE;
343}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000344#endif /* !NANO_TINY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000345
346#ifndef DISABLE_WRAPPING
347void wrap_reset(void)
348{
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000349 prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000350}
351
352/* We wrap the given line. Precondition: we assume the cursor has been
353 * moved forward since the last typed character. Return value: whether
354 * we wrapped. */
355bool do_wrap(filestruct *line)
356{
357 size_t line_len;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000358 /* The length of the line we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000359 ssize_t wrap_loc;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000360 /* The index of line->data where we wrap. */
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000361#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000362 const char *indent_string = NULL;
363 /* Indentation to prepend to the new line. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000364 size_t indent_len = 0;
365 /* The length of indent_string. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000366#endif
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000367 const char *after_break;
368 /* The text after the wrap point. */
369 size_t after_break_len;
370 /* The length of after_break. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000371 bool prepending = FALSE;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000372 /* Do we prepend to the next line? */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000373 const char *next_line = NULL;
374 /* The next line, minus indentation. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000375 size_t next_line_len = 0;
376 /* The length of next_line. */
377 char *new_line = NULL;
378 /* The line we create. */
379 size_t new_line_len = 0;
380 /* The eventual length of new_line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000381
382 /* There are three steps. First, we decide where to wrap. Then, we
383 * create the new wrap line. Finally, we clean up. */
384
385 /* Step 1, finding where to wrap. We are going to add a new line
386 * after a blank character. In this step, we call break_line() to
387 * get the location of the last blank we can break the line at, and
388 * and set wrap_loc to the location of the character after it, so
389 * that the blank is preserved at the end of the line.
390 *
391 * If there is no legal wrap point, or we reach the last character
392 * of the line while trying to find one, we should return without
393 * wrapping. Note that if autoindent is turned on, we don't break
394 * at the end of it! */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000395 assert(line != NULL && line->data != NULL);
396
397 /* Save the length of the line. */
398 line_len = strlen(line->data);
399
400 /* Find the last blank where we can break the line. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000401 wrap_loc = break_line(line->data, fill
402#ifndef DISABLE_HELP
403 , FALSE
404#endif
405 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000406
407 /* If we couldn't break the line, or we've reached the end of it, we
408 * don't wrap. */
409 if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
410 return FALSE;
411
412 /* Otherwise, move forward to the character just after the blank. */
413 wrap_loc += move_mbright(line->data + wrap_loc, 0);
414
415 /* If we've reached the end of the line, we don't wrap. */
416 if (line->data[wrap_loc] == '\0')
417 return FALSE;
418
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000419#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000420 /* If autoindent is turned on, and we're on the character just after
421 * the indentation, we don't wrap. */
422 if (ISSET(AUTOINDENT)) {
423 /* Get the indentation of this line. */
424 indent_string = line->data;
425 indent_len = indent_length(indent_string);
426
427 if (wrap_loc == indent_len)
428 return FALSE;
429 }
430#endif
431
432 /* Step 2, making the new wrap line. It will consist of indentation
433 * followed by the text after the wrap point, optionally followed by
434 * a space (if the text after the wrap point doesn't end in a blank)
435 * and the text of the next line, if they can fit without
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000436 * wrapping, the next line exists, and the prepend_wrap flag is
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000437 * set. */
438
439 /* after_break is the text that will be wrapped to the next line. */
440 after_break = line->data + wrap_loc;
441 after_break_len = line_len - wrap_loc;
442
443 assert(strlen(after_break) == after_break_len);
444
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000445 /* We prepend the wrapped text to the next line, if the prepend_wrap
446 * flag is set, there is a next line, and prepending would not make
447 * the line too long. */
448 if (prepend_wrap && line != openfile->filebot) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000449 const char *end = after_break + move_mbleft(after_break,
450 after_break_len);
451
452 /* If after_break doesn't end in a blank, make sure it ends in a
453 * space. */
454 if (!is_blank_mbchar(end)) {
455 line_len++;
456 line->data = charealloc(line->data, line_len + 1);
457 line->data[line_len - 1] = ' ';
458 line->data[line_len] = '\0';
459 after_break = line->data + wrap_loc;
460 after_break_len++;
461 openfile->totsize++;
462 }
463
464 next_line = line->next->data;
465 next_line_len = strlen(next_line);
466
467 if (after_break_len + next_line_len <= fill) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000468 prepending = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000469 new_line_len += next_line_len;
470 }
471 }
472
473 /* new_line_len is now the length of the text that will be wrapped
474 * to the next line, plus (if we're prepending to it) the length of
475 * the text of the next line. */
476 new_line_len += after_break_len;
477
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000478#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000479 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000480 if (prepending) {
481 /* If we're prepending, the indentation will come from the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000482 * next line. */
483 indent_string = next_line;
484 indent_len = indent_length(indent_string);
485 next_line += indent_len;
486 } else {
487 /* Otherwise, it will come from this line, in which case
488 * we should increase new_line_len to make room for it. */
489 new_line_len += indent_len;
490 openfile->totsize += mbstrnlen(indent_string, indent_len);
491 }
492 }
493#endif
494
495 /* Now we allocate the new line and copy the text into it. */
496 new_line = charalloc(new_line_len + 1);
497 new_line[0] = '\0';
498
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000499#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000500 if (ISSET(AUTOINDENT)) {
501 /* Copy the indentation. */
502 strncpy(new_line, indent_string, indent_len);
503 new_line[indent_len] = '\0';
504 new_line_len += indent_len;
505 }
506#endif
507
508 /* Copy all the text after the wrap point of the current line. */
509 strcat(new_line, after_break);
510
511 /* Break the current line at the wrap point. */
512 null_at(&line->data, wrap_loc);
513
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000514 if (prepending) {
515 /* If we're prepending, copy the text from the next line, minus
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000516 * the indentation that we already copied above. */
517 strcat(new_line, next_line);
518
519 free(line->next->data);
520 line->next->data = new_line;
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000521
522 /* If the NO_NEWLINES flag isn't set, and text has been added to
523 * the magicline, make a new magicline. */
524 if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
525 new_magicline();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000526 } else {
527 /* Otherwise, make a new line and copy the text after where we
528 * broke this line to the beginning of the new line. */
529 splice_node(openfile->current, make_new_node(openfile->current),
530 openfile->current->next);
531
David Lawrence Ramsey219a8142005-11-22 22:08:01 +0000532 /* If the current line is the last line of the file, move the
533 * last line of the file down to the next line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000534 if (openfile->filebot == openfile->current)
535 openfile->filebot = openfile->current->next;
536
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000537 openfile->current->next->data = new_line;
538
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000539 openfile->totsize++;
540 }
541
542 /* Step 3, clean up. Reposition the cursor and mark, and do some
543 * other sundry things. */
544
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000545 /* Set the prepend_wrap flag, so that later wraps of this line will
546 * be prepended to the next line. */
547 prepend_wrap = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000548
549 /* Each line knows its line number. We recalculate these if we
550 * inserted a new line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000551 if (!prepending)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000552 renumber(line);
553
554 /* If the cursor was after the break point, we must move it. We
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000555 * also clear the prepend_wrap flag in this case. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000556 if (openfile->current_x > wrap_loc) {
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000557 prepend_wrap = FALSE;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000558
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000559 openfile->current = openfile->current->next;
560 openfile->current_x -= wrap_loc
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000561#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000562 - indent_len
563#endif
564 ;
565 openfile->placewewant = xplustabs();
566 }
567
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000568#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000569 /* If the mark was on this line after the wrap point, we move it
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000570 * down. If it was on the next line and we prepended to that line,
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000571 * we move it right. */
572 if (openfile->mark_set) {
573 if (openfile->mark_begin == line && openfile->mark_begin_x >
574 wrap_loc) {
575 openfile->mark_begin = line->next;
576 openfile->mark_begin_x -= wrap_loc - indent_len + 1;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000577 } else if (prepending && openfile->mark_begin == line->next)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000578 openfile->mark_begin_x += after_break_len;
579 }
580#endif
581
582 return TRUE;
583}
584#endif /* !DISABLE_WRAPPING */
585
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000586#if !defined(DISABLE_HELP) || !defined(DISABLE_JUSTIFY) || !defined(DISABLE_WRAPPING)
587/* We are trying to break a chunk off line. We find the last blank such
David Lawrence Ramseycd9a5f02005-09-20 06:12:54 +0000588 * that the display length to there is at most (goal + 1). If there is
589 * no such blank, then we find the first blank. We then take the last
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000590 * blank in that group of blanks. The terminating '\0' counts as a
591 * blank, as does a '\n' if newline is TRUE. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000592ssize_t break_line(const char *line, ssize_t goal
593#ifndef DISABLE_HELP
594 , bool newline
595#endif
596 )
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000597{
598 ssize_t blank_loc = -1;
599 /* Current tentative return value. Index of the last blank we
600 * found with short enough display width. */
601 ssize_t cur_loc = 0;
602 /* Current index in line. */
603 int line_len;
604
605 assert(line != NULL);
606
607 while (*line != '\0' && goal >= 0) {
David Lawrence Ramsey95ef3372005-09-20 19:44:19 +0000608 size_t pos = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000609
David Lawrence Ramseyca37ee62005-09-20 17:47:27 +0000610 line_len = parse_mbchar(line, NULL, &pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000611
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000612 if (is_blank_mbchar(line)
613#ifndef DISABLE_HELP
614 || (newline && *line == '\n')
615#endif
616 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000617 blank_loc = cur_loc;
618
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000619#ifndef DISABLE_HELP
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000620 if (newline && *line == '\n')
621 break;
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000622#endif
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000623 }
624
David Lawrence Ramsey95ef3372005-09-20 19:44:19 +0000625 goal -= pos;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000626 line += line_len;
627 cur_loc += line_len;
628 }
629
630 if (goal >= 0)
631 /* In fact, the whole line displays shorter than goal. */
632 return cur_loc;
633
634 if (blank_loc == -1) {
635 /* No blank was found that was short enough. */
636 bool found_blank = FALSE;
David Lawrence Ramsey5ab12ca2005-09-20 17:52:52 +0000637 ssize_t found_blank_loc = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000638
639 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000640 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000641
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000642 if (is_blank_mbchar(line)
643#ifndef DISABLE_HELP
644 || (newline && *line == '\n')
645#endif
646 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000647 if (!found_blank)
648 found_blank = TRUE;
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +0000649 found_blank_loc = cur_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000650 } else if (found_blank)
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +0000651 return found_blank_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000652
653 line += line_len;
654 cur_loc += line_len;
655 }
656
657 return -1;
658 }
659
660 /* Move to the last blank after blank_loc, if there is one. */
661 line -= cur_loc;
662 line += blank_loc;
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000663 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000664 line += line_len;
665
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000666 while (*line != '\0' && (is_blank_mbchar(line)
667#ifndef DISABLE_HELP
668 || (newline && *line == '\n')
669#endif
670 )) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000671 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000672
673 line += line_len;
674 blank_loc += line_len;
675 }
676
677 return blank_loc;
678}
679#endif /* !DISABLE_HELP || !DISABLE_JUSTIFY || !DISABLE_WRAPPING */
680
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000681#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000682/* The "indentation" of a line is the whitespace between the quote part
683 * and the non-whitespace of the line. */
684size_t indent_length(const char *line)
685{
686 size_t len = 0;
687 char *blank_mb;
688 int blank_mb_len;
689
690 assert(line != NULL);
691
692 blank_mb = charalloc(mb_cur_max());
693
694 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000695 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000696
697 if (!is_blank_mbchar(blank_mb))
698 break;
699
700 line += blank_mb_len;
701 len += blank_mb_len;
702 }
703
704 free(blank_mb);
705
706 return len;
707}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000708#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000709
710#ifndef DISABLE_JUSTIFY
711/* justify_format() replaces blanks with spaces and multiple spaces by 1
712 * (except it maintains up to 2 after a character in punct optionally
713 * followed by a character in brackets, and removes all from the end).
714 *
715 * justify_format() might make paragraph->data shorter, and change the
716 * actual pointer with null_at().
717 *
718 * justify_format() will not look at the first skip characters of
719 * paragraph. skip should be at most strlen(paragraph->data). The
720 * character at paragraph[skip + 1] must not be blank. */
721void justify_format(filestruct *paragraph, size_t skip)
722{
723 char *end, *new_end, *new_paragraph_data;
724 size_t shift = 0;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000725#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000726 size_t mark_shift = 0;
727#endif
728
729 /* These four asserts are assumptions about the input data. */
730 assert(paragraph != NULL);
731 assert(paragraph->data != NULL);
732 assert(skip < strlen(paragraph->data));
733 assert(!is_blank_mbchar(paragraph->data + skip));
734
735 end = paragraph->data + skip;
736 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
737 strncpy(new_paragraph_data, paragraph->data, skip);
738 new_end = new_paragraph_data + skip;
739
740 while (*end != '\0') {
741 int end_len;
742
743 /* If this character is blank, make sure that it's a space with
744 * no blanks after it. */
745 if (is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000746 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000747
748 *new_end = ' ';
749 new_end++;
750 end += end_len;
751
752 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000753 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000754
755 end += end_len;
756 shift += end_len;
757
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000758#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000759 /* Keep track of the change in the current line. */
760 if (openfile->mark_set && openfile->mark_begin ==
761 paragraph && openfile->mark_begin_x >= end -
762 paragraph->data)
763 mark_shift += end_len;
764#endif
765 }
766 /* If this character is punctuation optionally followed by a
767 * bracket and then followed by blanks, make sure there are no
768 * more than two blanks after it, and make sure that the blanks
769 * are spaces. */
770 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000771 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000772
773 while (end_len > 0) {
774 *new_end = *end;
775 new_end++;
776 end++;
777 end_len--;
778 }
779
780 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000781 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000782
783 while (end_len > 0) {
784 *new_end = *end;
785 new_end++;
786 end++;
787 end_len--;
788 }
789 }
790
791 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000792 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000793
794 *new_end = ' ';
795 new_end++;
796 end += end_len;
797 }
798
799 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000800 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000801
802 *new_end = ' ';
803 new_end++;
804 end += end_len;
805 }
806
807 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000808 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000809
810 end += end_len;
811 shift += end_len;
812
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000813#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000814 /* Keep track of the change in the current line. */
815 if (openfile->mark_set && openfile->mark_begin ==
816 paragraph && openfile->mark_begin_x >= end -
817 paragraph->data)
818 mark_shift += end_len;
819#endif
820 }
821 /* If this character is neither blank nor punctuation, leave it
822 * alone. */
823 } else {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000824 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000825
826 while (end_len > 0) {
827 *new_end = *end;
828 new_end++;
829 end++;
830 end_len--;
831 }
832 }
833 }
834
835 assert(*end == '\0');
836
837 *new_end = *end;
838
839 /* Make sure that there are no spaces at the end of the line. */
840 while (new_end > new_paragraph_data + skip &&
841 *(new_end - 1) == ' ') {
842 new_end--;
843 shift++;
844 }
845
846 if (shift > 0) {
847 openfile->totsize -= shift;
848 null_at(&new_paragraph_data, new_end - new_paragraph_data);
849 free(paragraph->data);
850 paragraph->data = new_paragraph_data;
851
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000852#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000853 /* Adjust the mark coordinates to compensate for the change in
854 * the current line. */
855 if (openfile->mark_set && openfile->mark_begin == paragraph) {
856 openfile->mark_begin_x -= mark_shift;
857 if (openfile->mark_begin_x > new_end - new_paragraph_data)
858 openfile->mark_begin_x = new_end - new_paragraph_data;
859 }
860#endif
861 } else
862 free(new_paragraph_data);
863}
864
865/* The "quote part" of a line is the largest initial substring matching
866 * the quote string. This function returns the length of the quote part
867 * of the given line.
868 *
869 * Note that if !HAVE_REGEX_H then we match concatenated copies of
870 * quotestr. */
871size_t quote_length(const char *line)
872{
873#ifdef HAVE_REGEX_H
874 regmatch_t matches;
875 int rc = regexec(&quotereg, line, 1, &matches, 0);
876
877 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
878 return 0;
879 /* matches.rm_so should be 0, since the quote string should start
880 * with the caret ^. */
881 return matches.rm_eo;
882#else /* !HAVE_REGEX_H */
883 size_t qdepth = 0;
884
885 /* Compute quote depth level. */
886 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
887 qdepth += quotelen;
888 return qdepth;
889#endif /* !HAVE_REGEX_H */
890}
891
892/* a_line and b_line are lines of text. The quotation part of a_line is
893 * the first a_quote characters. Check that the quotation part of
894 * b_line is the same. */
895bool quotes_match(const char *a_line, size_t a_quote, const char
896 *b_line)
897{
898 /* Here is the assumption about a_quote. */
899 assert(a_quote == quote_length(a_line));
900
901 return (a_quote == quote_length(b_line) &&
902 strncmp(a_line, b_line, a_quote) == 0);
903}
904
905/* We assume a_line and b_line have no quote part. Then, we return
906 * whether b_line could follow a_line in a paragraph. */
907bool indents_match(const char *a_line, size_t a_indent, const char
908 *b_line, size_t b_indent)
909{
910 assert(a_indent == indent_length(a_line));
911 assert(b_indent == indent_length(b_line));
912
913 return (b_indent <= a_indent &&
914 strncmp(a_line, b_line, b_indent) == 0);
915}
916
917/* Is foo the beginning of a paragraph?
918 *
919 * A line of text consists of a "quote part", followed by an
920 * "indentation part", followed by text. The functions quote_length()
921 * and indent_length() calculate these parts.
922 *
923 * A line is "part of a paragraph" if it has a part not in the quote
924 * part or the indentation.
925 *
926 * A line is "the beginning of a paragraph" if it is part of a
927 * paragraph and
928 * 1) it is the top line of the file, or
929 * 2) the line above it is not part of a paragraph, or
930 * 3) the line above it does not have precisely the same quote
931 * part, or
932 * 4) the indentation of this line is not an initial substring of
933 * the indentation of the previous line, or
934 * 5) this line has no quote part and some indentation, and
935 * autoindent isn't turned on.
936 * The reason for number 5) is that if autoindent isn't turned on,
937 * then an indented line is expected to start a paragraph, as in
938 * books. Thus, nano can justify an indented paragraph only if
939 * autoindent is turned on. */
940bool begpar(const filestruct *const foo)
941{
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +0000942 size_t quote_len, indent_len, temp_id_len;
943
944 if (foo == NULL)
945 return FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000946
947 /* Case 1). */
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +0000948 if (foo == openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000949 return TRUE;
950
951 quote_len = quote_length(foo->data);
952 indent_len = indent_length(foo->data + quote_len);
953
954 /* Not part of a paragraph. */
955 if (foo->data[quote_len + indent_len] == '\0')
956 return FALSE;
957
958 /* Case 3). */
959 if (!quotes_match(foo->data, quote_len, foo->prev->data))
960 return TRUE;
961
962 temp_id_len = indent_length(foo->prev->data + quote_len);
963
964 /* Case 2) or 5) or 4). */
965 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
966 (quote_len == 0 && indent_len > 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000967#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000968 && !ISSET(AUTOINDENT)
969#endif
970 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
971 foo->data + quote_len, indent_len))
972 return TRUE;
973
974 return FALSE;
975}
976
977/* Is foo inside a paragraph? */
978bool inpar(const filestruct *const foo)
979{
980 size_t quote_len;
981
982 if (foo == NULL)
983 return FALSE;
984
985 quote_len = quote_length(foo->data);
986
David Lawrence Ramsey21014032005-11-09 20:33:42 +0000987 return (foo->data[quote_len + indent_length(foo->data +
988 quote_len)] != '\0');
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000989}
990
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +0000991/* Move the next par_len lines, starting with first_line, into the
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +0000992 * justify buffer, leaving copies of those lines in place. Assume that
993 * par_len is greater than zero, and that there are enough lines after
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +0000994 * first_line. */
995void backup_lines(filestruct *first_line, size_t par_len)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000996{
997 filestruct *top = first_line;
998 /* The top of the paragraph we're backing up. */
999 filestruct *bot = first_line;
1000 /* The bottom of the paragraph we're backing up. */
1001 size_t i;
1002 /* Generic loop variable. */
1003 size_t current_x_save = openfile->current_x;
1004 ssize_t fl_lineno_save = first_line->lineno;
1005 ssize_t edittop_lineno_save = openfile->edittop->lineno;
1006 ssize_t current_lineno_save = openfile->current->lineno;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001007#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001008 bool old_mark_set = openfile->mark_set;
1009 ssize_t mb_lineno_save = 0;
1010 size_t mark_begin_x_save = 0;
1011
1012 if (old_mark_set) {
1013 mb_lineno_save = openfile->mark_begin->lineno;
1014 mark_begin_x_save = openfile->mark_begin_x;
1015 }
1016#endif
1017
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001018 /* par_len will be one greater than the number of lines between
1019 * current and filebot if filebot is the last line in the
1020 * paragraph. */
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001021 assert(par_len > 0 && openfile->current->lineno + par_len <=
1022 filebot->lineno + 1);
1023
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001024 /* Move bot down par_len lines to the line after the last line of
1025 * the paragraph, if there is one. */
1026 for (i = par_len; i > 0 && bot != openfile->filebot; i--)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001027 bot = bot->next;
1028
1029 /* Move the paragraph from the current buffer's filestruct to the
1030 * justify buffer. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001031 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
David Lawrence Ramseyf0575cf2005-11-09 23:27:51 +00001032 (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001033
1034 /* Copy the paragraph back to the current buffer's filestruct from
1035 * the justify buffer. */
1036 copy_from_filestruct(jusbuffer, jusbottom);
1037
1038 /* Move upward from the last line of the paragraph to the first
1039 * line, putting first_line, edittop, current, and mark_begin at the
1040 * same lines in the copied paragraph that they had in the original
1041 * paragraph. */
David Lawrence Ramsey5d6f1272005-11-10 03:32:59 +00001042 if (openfile->current != openfile->fileage)
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001043 top = openfile->current->prev;
1044 else
1045 top = openfile->current;
David Lawrence Ramseye8d505b2005-11-10 03:40:45 +00001046 for (i = par_len; i > 0 && top != NULL; i--) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001047 if (top->lineno == fl_lineno_save)
1048 first_line = top;
1049 if (top->lineno == edittop_lineno_save)
1050 openfile->edittop = top;
1051 if (top->lineno == current_lineno_save)
1052 openfile->current = top;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001053#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001054 if (old_mark_set && top->lineno == mb_lineno_save) {
1055 openfile->mark_begin = top;
1056 openfile->mark_begin_x = mark_begin_x_save;
1057 }
1058#endif
1059 top = top->prev;
1060 }
1061
1062 /* Put current_x at the same place in the copied paragraph that it
1063 * had in the original paragraph. */
1064 openfile->current_x = current_x_save;
1065
1066 set_modified();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001067}
1068
1069/* Find the beginning of the current paragraph if we're in one, or the
1070 * beginning of the next paragraph if we're not. Afterwards, save the
1071 * quote length and paragraph length in *quote and *par. Return TRUE if
1072 * we found a paragraph, or FALSE if there was an error or we didn't
1073 * find a paragraph.
1074 *
1075 * See the comment at begpar() for more about when a line is the
1076 * beginning of a paragraph. */
1077bool find_paragraph(size_t *const quote, size_t *const par)
1078{
1079 size_t quote_len;
1080 /* Length of the initial quotation of the paragraph we search
1081 * for. */
1082 size_t par_len;
1083 /* Number of lines in the paragraph we search for. */
1084 filestruct *current_save;
1085 /* The line at the beginning of the paragraph we search for. */
1086 ssize_t current_y_save;
1087 /* The y-coordinate at the beginning of the paragraph we search
1088 * for. */
1089
1090#ifdef HAVE_REGEX_H
1091 if (quoterc != 0) {
1092 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1093 return FALSE;
1094 }
1095#endif
1096
1097 assert(openfile->current != NULL);
1098
David Lawrence Ramsey1be131a2005-11-11 03:55:52 +00001099 /* If we're at the end of the last line of the file, it means that
1100 * there aren't any paragraphs left, so get out. */
1101 if (openfile->current == openfile->filebot && openfile->current_x ==
1102 strlen(openfile->filebot->data))
1103 return FALSE;
1104
1105 /* If the current line isn't in a paragraph, move forward to the
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001106 * last line of the next paragraph, if any. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001107 if (!inpar(openfile->current)) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001108 do_para_end(FALSE);
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001109 /* If we end up past the beginning of the line, it means that
1110 * we're at the end of the last line of the file, and the line
1111 * isn't blank, in which case the last line of the file is the
1112 * last line of the next paragraph.
1113 *
1114 * Otherwise, if we end up on a line that's in a paragraph, it
1115 * means that we're on the line after the last line of the next
1116 * paragraph, in which case we should move back to the last line
1117 * of the next paragraph. */
1118 if (openfile->current_x == 0) {
1119 if (!inpar(openfile->current->prev))
1120 return FALSE;
1121 if (openfile->current != openfile->fileage)
1122 openfile->current = openfile->current->prev;
1123 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001124 }
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001125 /* If the current line isn't the first line of the paragraph, move
1126 * back to the first line of the paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001127 if (!begpar(openfile->current))
1128 do_para_begin(FALSE);
1129
1130 /* Now current is the first line of the paragraph. Set quote_len to
1131 * the quotation length of that line, and set par_len to the number
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001132 * of lines in this paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001133 quote_len = quote_length(openfile->current->data);
1134 current_save = openfile->current;
1135 current_y_save = openfile->current_y;
1136 do_para_end(FALSE);
1137 par_len = openfile->current->lineno - current_save->lineno;
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001138 /* If we end up past the beginning of the line, it means that we're
1139 * at the end of the last line of the file, and the line isn't
1140 * blank, in which case the last line of the file is part of the
1141 * paragraph. */
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001142 if (openfile->current_x > 0)
1143 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001144 openfile->current = current_save;
1145 openfile->current_y = current_y_save;
1146
1147 /* Save the values of quote_len and par_len. */
1148 assert(quote != NULL && par != NULL);
1149
1150 *quote = quote_len;
1151 *par = par_len;
1152
1153 return TRUE;
1154}
1155
1156/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1157 * the current paragraph. */
1158void do_justify(bool full_justify)
1159{
1160 filestruct *first_par_line = NULL;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001161 /* Will be the first line of the justified paragraph. For
1162 * restoring after unjustify. */
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001163 filestruct *last_par_line = NULL;
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001164 /* Will be the line after the last line of the justified
1165 * paragraph, if any. Also for restoring after unjustify. */
David Lawrence Ramsey82b5deb2005-11-10 06:07:57 +00001166 bool filebot_inpar = FALSE;
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001167 /* Whether the text at filebot is part of the current
1168 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001169
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001170 /* We save these variables to be restored if the user
1171 * unjustifies. */
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001172 filestruct *edittop_save = openfile->edittop;
1173 filestruct *current_save = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001174 size_t current_x_save = openfile->current_x;
1175 size_t pww_save = openfile->placewewant;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001176 size_t totsize_save = openfile->totsize;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001177#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001178 filestruct *mark_begin_save = openfile->mark_begin;
1179 size_t mark_begin_x_save = openfile->mark_begin_x;
1180#endif
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001181 bool modified_save = openfile->modified;
1182
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001183 int kbinput;
1184 bool meta_key, func_key, s_or_t, ran_func, finished;
1185
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001186 /* Move to the beginning of the current line, so that justifying at
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001187 * the end of the last line of the file, if that line isn't blank,
1188 * will work the first time through. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001189 openfile->current_x = 0;
1190
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001191 /* If we're justifying the entire file, start at the beginning. */
1192 if (full_justify)
1193 openfile->current = openfile->fileage;
1194
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001195 while (TRUE) {
1196 size_t i;
1197 /* Generic loop variable. */
1198 size_t quote_len;
1199 /* Length of the initial quotation of the paragraph we
1200 * justify. */
1201 size_t indent_len;
1202 /* Length of the initial indentation of the paragraph we
1203 * justify. */
1204 size_t par_len;
1205 /* Number of lines in the paragraph we justify. */
1206 ssize_t break_pos;
1207 /* Where we will break lines. */
1208 char *indent_string;
1209 /* The first indentation that doesn't match the initial
1210 * indentation of the paragraph we justify. This is put at
1211 * the beginning of every line broken off the first
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001212 * justified line of the paragraph. Note that this works
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001213 * because a paragraph can only contain two indentations at
1214 * most: the initial one, and a different one starting on a
1215 * line after the first. See the comment at begpar() for
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001216 * more about when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001217
1218 /* Find the first line of the paragraph to be justified. That
1219 * is the start of this paragraph if we're in one, or the start
1220 * of the next otherwise. Save the quote length and paragraph
1221 * length (number of lines). Don't refresh the screen yet,
1222 * since we'll do that after we justify.
1223 *
1224 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001225 * justifying the whole file, and we've found at least one
1226 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001227 * last line of the file, so set the last line of the text to be
1228 * justified to the last line of the file and break out of the
1229 * loop. Otherwise, it means that there are no paragraph(s) to
1230 * justify, so refresh the screen and get out. */
1231 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001232 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001233 last_par_line = openfile->filebot;
1234 break;
1235 } else {
1236 edit_refresh();
1237 return;
1238 }
1239 }
1240
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001241 /* par_len will be one greater than the number of lines between
1242 * current and filebot if filebot is the last line in the
1243 * paragraph. Set filebot_inpar to TRUE if this is the case. */
1244 filebot_inpar = (openfile->current->lineno + par_len ==
1245 openfile->filebot->lineno + 1);
1246
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001247 /* If we haven't already done it, move the original paragraph(s)
1248 * to the justify buffer, splice a copy of the original
1249 * paragraph(s) into the file in the same place, and set
1250 * first_par_line to the first line of the copy. */
1251 if (first_par_line == NULL) {
1252 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00001253 openfile->filebot->lineno - openfile->current->lineno +
1254 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001255 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001256 first_par_line = openfile->current;
1257 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001258
1259 /* Initialize indent_string to a blank string. */
1260 indent_string = mallocstrcpy(NULL, "");
1261
1262 /* Find the first indentation in the paragraph that doesn't
1263 * match the indentation of the first line, and save it in
1264 * indent_string. If all the indentations are the same, save
1265 * the indentation of the first line in indent_string. */
1266 {
1267 const filestruct *indent_line = openfile->current;
1268 bool past_first_line = FALSE;
1269
1270 for (i = 0; i < par_len; i++) {
1271 indent_len = quote_len +
1272 indent_length(indent_line->data + quote_len);
1273
1274 if (indent_len != strlen(indent_string)) {
1275 indent_string = mallocstrncpy(indent_string,
1276 indent_line->data, indent_len + 1);
1277 indent_string[indent_len] = '\0';
1278
1279 if (past_first_line)
1280 break;
1281 }
1282
1283 if (indent_line == openfile->current)
1284 past_first_line = TRUE;
1285
1286 indent_line = indent_line->next;
1287 }
1288 }
1289
1290 /* Now tack all the lines of the paragraph together, skipping
1291 * the quoting and indentation on all lines after the first. */
1292 for (i = 0; i < par_len - 1; i++) {
1293 filestruct *next_line = openfile->current->next;
1294 size_t line_len = strlen(openfile->current->data);
1295 size_t next_line_len =
1296 strlen(openfile->current->next->data);
1297
1298 indent_len = quote_len +
1299 indent_length(openfile->current->next->data +
1300 quote_len);
1301
1302 next_line_len -= indent_len;
1303 openfile->totsize -= indent_len;
1304
1305 /* We're just about to tack the next line onto this one. If
1306 * this line isn't empty, make sure it ends in a space. */
1307 if (line_len > 0 &&
1308 openfile->current->data[line_len - 1] != ' ') {
1309 line_len++;
1310 openfile->current->data =
1311 charealloc(openfile->current->data,
1312 line_len + 1);
1313 openfile->current->data[line_len - 1] = ' ';
1314 openfile->current->data[line_len] = '\0';
1315 openfile->totsize++;
1316 }
1317
1318 openfile->current->data =
1319 charealloc(openfile->current->data, line_len +
1320 next_line_len + 1);
1321 strcat(openfile->current->data, next_line->data +
1322 indent_len);
1323
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00001324 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00001325 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001326 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00001327 if (next_line == openfile->filebot)
1328 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001329
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001330#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001331 /* Adjust the mark coordinates to compensate for the change
1332 * in the next line. */
1333 if (openfile->mark_set && openfile->mark_begin ==
1334 next_line) {
1335 openfile->mark_begin = openfile->current;
1336 openfile->mark_begin_x += line_len - indent_len;
1337 }
1338#endif
1339
1340 unlink_node(next_line);
1341 delete_node(next_line);
1342
1343 /* If we've removed the next line, we need to go through
1344 * this line again. */
1345 i--;
1346
1347 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001348 openfile->totsize--;
1349 }
1350
1351 /* Call justify_format() on the paragraph, which will remove
1352 * excess spaces from it and change all blank characters to
1353 * spaces. */
1354 justify_format(openfile->current, quote_len +
1355 indent_length(openfile->current->data + quote_len));
1356
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001357 while (par_len > 0 && strlenpt(openfile->current->data) >
1358 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001359 size_t line_len = strlen(openfile->current->data);
1360
1361 indent_len = strlen(indent_string);
1362
1363 /* If this line is too long, try to wrap it to the next line
1364 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001365 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001366 fill - strnlenpt(openfile->current->data, indent_len)
1367#ifndef DISABLE_HELP
1368 , FALSE
1369#endif
1370 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001371
1372 /* We can't break the line, or don't need to, so get out. */
1373 if (break_pos == -1 || break_pos + indent_len == line_len)
1374 break;
1375
1376 /* Move forward to the character after the indentation and
1377 * just after the space. */
1378 break_pos += indent_len + 1;
1379
1380 assert(break_pos <= line_len);
1381
1382 /* Make a new line, and copy the text after where we're
1383 * going to break this line to the beginning of the new
1384 * line. */
1385 splice_node(openfile->current,
1386 make_new_node(openfile->current),
1387 openfile->current->next);
1388
1389 /* If this paragraph is non-quoted, and autoindent isn't
1390 * turned on, set the indentation length to zero so that the
1391 * indentation is treated as part of the line. */
1392 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001393#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001394 && !ISSET(AUTOINDENT)
1395#endif
1396 )
1397 indent_len = 0;
1398
1399 /* Copy the text after where we're going to break the
1400 * current line to the next line. */
1401 openfile->current->next->data = charalloc(indent_len + 1 +
1402 line_len - break_pos);
1403 strncpy(openfile->current->next->data, indent_string,
1404 indent_len);
1405 strcpy(openfile->current->next->data + indent_len,
1406 openfile->current->data + break_pos);
1407
1408 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001409 openfile->totsize += indent_len + 1;
1410
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001411#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001412 /* Adjust the mark coordinates to compensate for the change
1413 * in the current line. */
1414 if (openfile->mark_set && openfile->mark_begin ==
1415 openfile->current && openfile->mark_begin_x >
1416 break_pos) {
1417 openfile->mark_begin = openfile->current->next;
1418 openfile->mark_begin_x -= break_pos - indent_len;
1419 }
1420#endif
1421
1422 /* Break the current line. */
1423 null_at(&openfile->current->data, break_pos);
1424
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001425 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00001426 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001427 if (openfile->filebot == openfile->current)
1428 openfile->filebot = openfile->filebot->next;
1429
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001430 /* Go to the next line. */
1431 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001432 openfile->current_y++;
1433 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001434 }
1435
1436 /* We're done breaking lines, so we don't need indent_string
1437 * anymore. */
1438 free(indent_string);
1439
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001440 /* Go to the next line, if possible. If there is no next line,
1441 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001442 if (openfile->current != openfile->filebot) {
1443 openfile->current_y++;
1444 openfile->current = openfile->current->next;
1445 } else
1446 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001447
1448 /* We've just justified a paragraph. If we're not justifying the
1449 * entire file, break out of the loop. Otherwise, continue the
1450 * loop so that we justify all the paragraphs in the file. */
1451 if (!full_justify)
1452 break;
1453 }
1454
1455 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001456 * up. current_y and totsize have been maintained above. If we
1457 * actually justified something, renumber, since edit_refresh()
1458 * needs the line numbers to be right, and set last_par_line to the
1459 * new end of the paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001460 if (first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001461 renumber(first_par_line);
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001462 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001463 }
1464
1465 edit_refresh();
1466
1467 statusbar(_("Can now UnJustify!"));
1468
1469 /* If constant cursor position display is on, make sure the current
1470 * cursor position will be properly displayed on the statusbar. */
1471 if (ISSET(CONST_UPDATE))
1472 do_cursorpos(TRUE);
1473
1474 /* Display the shortcut list with UnJustify. */
1475 shortcut_init(TRUE);
1476 display_main_list();
1477
1478 /* Now get a keystroke and see if it's unjustify. If not, put back
1479 * the keystroke and return. */
1480 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
1481 &finished, FALSE);
1482
1483 if (!meta_key && !func_key && s_or_t &&
1484 kbinput == NANO_UNJUSTIFY_KEY) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001485 /* Splice the justify buffer back into the file, but only if we
1486 * actually justified something. */
1487 if (first_par_line != NULL) {
1488 filestruct *top_save;
1489
1490 /* Partition the filestruct so that it contains only the
1491 * text of the justified paragraph. */
1492 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00001493 last_par_line, filebot_inpar ?
1494 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001495
1496 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001497 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001498 free_filestruct(openfile->fileage);
1499 openfile->fileage = jusbuffer;
1500 openfile->filebot = jusbottom;
1501
1502 top_save = openfile->fileage;
1503
1504 /* Unpartition the filestruct so that it contains all the
1505 * text again. Note that the justified paragraph has been
1506 * replaced with the unjustified paragraph. */
1507 unpartition_filestruct(&filepart);
1508
1509 /* Renumber starting with the beginning line of the old
1510 * partition. */
1511 renumber(top_save);
1512
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001513 /* Restore the justify we just did (ungrateful user!). */
1514 openfile->edittop = edittop_save;
1515 openfile->current = current_save;
1516 openfile->current_x = current_x_save;
1517 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001518 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001519#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001520 if (openfile->mark_set) {
1521 openfile->mark_begin = mark_begin_save;
1522 openfile->mark_begin_x = mark_begin_x_save;
1523 }
1524#endif
1525 openfile->modified = modified_save;
1526
1527 /* Clear the justify buffer. */
1528 jusbuffer = NULL;
1529
1530 if (!openfile->modified)
1531 titlebar(NULL);
1532 edit_refresh();
1533 }
1534 } else {
1535 unget_kbinput(kbinput, meta_key, func_key);
1536
1537 /* Blow away the text in the justify buffer. */
1538 free_filestruct(jusbuffer);
1539 jusbuffer = NULL;
1540 }
1541
1542 blank_statusbar();
1543
1544 /* Display the shortcut list with UnCut. */
1545 shortcut_init(FALSE);
1546 display_main_list();
1547}
1548
1549void do_justify_void(void)
1550{
1551 do_justify(FALSE);
1552}
1553
1554void do_full_justify(void)
1555{
1556 do_justify(TRUE);
1557}
1558#endif /* !DISABLE_JUSTIFY */
1559
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001560#ifndef DISABLE_SPELLER
1561/* A word is misspelled in the file. Let the user replace it. We
1562 * return FALSE if the user cancels. */
1563bool do_int_spell_fix(const char *word)
1564{
1565 char *save_search, *save_replace;
1566 size_t match_len, current_x_save = openfile->current_x;
1567 size_t pww_save = openfile->placewewant;
1568 filestruct *edittop_save = openfile->edittop;
1569 filestruct *current_save = openfile->current;
1570 /* Save where we are. */
1571 bool canceled = FALSE;
1572 /* The return value. */
1573 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001574#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001575 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
1576#endif
1577#ifdef HAVE_REGEX_H
1578 bool regexp_set = ISSET(USE_REGEXP);
1579#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001580#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001581 bool old_mark_set = openfile->mark_set;
1582 bool added_magicline = FALSE;
1583 /* Whether we added a magicline after filebot. */
1584 bool right_side_up = FALSE;
1585 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1586 * FALSE if (current, current_x) is. */
1587 filestruct *top, *bot;
1588 size_t top_x, bot_x;
1589#endif
1590
1591 /* Make sure spell-check is case sensitive. */
1592 SET(CASE_SENSITIVE);
1593
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001594#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001595 /* Make sure spell-check goes forward only. */
1596 UNSET(BACKWARDS_SEARCH);
1597#endif
1598#ifdef HAVE_REGEX_H
1599 /* Make sure spell-check doesn't use regular expressions. */
1600 UNSET(USE_REGEXP);
1601#endif
1602
1603 /* Save the current search/replace strings. */
1604 search_init_globals();
1605 save_search = last_search;
1606 save_replace = last_replace;
1607
1608 /* Set the search/replace strings to the misspelled word. */
1609 last_search = mallocstrcpy(NULL, word);
1610 last_replace = mallocstrcpy(NULL, word);
1611
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001612#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001613 if (old_mark_set) {
1614 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001615 * contains only the marked text; if the NO_NEWLINES flag isn't
1616 * set, keep track of whether the text will have a magicline
1617 * added when we're done correcting misspelled words; and
1618 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001619 mark_order((const filestruct **)&top, &top_x,
1620 (const filestruct **)&bot, &bot_x, &right_side_up);
1621 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001622 if (!ISSET(NO_NEWLINES))
1623 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001624 openfile->mark_set = FALSE;
1625 }
1626#endif
1627
1628 /* Start from the top of the file. */
1629 openfile->edittop = openfile->fileage;
1630 openfile->current = openfile->fileage;
1631 openfile->current_x = (size_t)-1;
1632 openfile->placewewant = 0;
1633
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001634 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001635 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001636 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001637 &match_len)) {
1638 if (is_whole_word(openfile->current_x, openfile->current->data,
1639 word)) {
1640 size_t xpt = xplustabs();
1641 char *exp_word = display_string(openfile->current->data,
1642 xpt, strnlenpt(openfile->current->data,
1643 openfile->current_x + match_len) - xpt, FALSE);
1644
1645 edit_refresh();
1646
1647 do_replace_highlight(TRUE, exp_word);
1648
1649 /* Allow all instances of the word to be corrected. */
David Lawrence Ramseye19449e2005-11-07 21:45:44 +00001650 canceled = (do_prompt(FALSE, spell_list, word,
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001651#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001652 NULL,
1653#endif
1654 _("Edit a replacement")) == -1);
1655
1656 do_replace_highlight(FALSE, exp_word);
1657
1658 free(exp_word);
1659
1660 if (!canceled && strcmp(word, answer) != 0) {
1661 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001662 do_replace_loop(TRUE, &canceled, openfile->current,
1663 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001664 }
1665
1666 break;
1667 }
1668 }
1669
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001670#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001671 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001672 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
1673 * added a magicline, remove it now. */
1674 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001675 remove_magicline();
1676
1677 /* Put the beginning and the end of the mark at the beginning
1678 * and the end of the spell-checked text. */
1679 if (openfile->fileage == openfile->filebot)
1680 bot_x += top_x;
1681 if (right_side_up) {
1682 openfile->mark_begin_x = top_x;
1683 current_x_save = bot_x;
1684 } else {
1685 current_x_save = top_x;
1686 openfile->mark_begin_x = bot_x;
1687 }
1688
1689 /* Unpartition the filestruct so that it contains all the text
1690 * again, and turn the mark back on. */
1691 unpartition_filestruct(&filepart);
1692 openfile->mark_set = TRUE;
1693 }
1694#endif
1695
1696 /* Restore the search/replace strings. */
1697 free(last_search);
1698 last_search = save_search;
1699 free(last_replace);
1700 last_replace = save_replace;
1701
1702 /* Restore where we were. */
1703 openfile->edittop = edittop_save;
1704 openfile->current = current_save;
1705 openfile->current_x = current_x_save;
1706 openfile->placewewant = pww_save;
1707
1708 /* Restore case sensitivity setting. */
1709 if (!case_sens_set)
1710 UNSET(CASE_SENSITIVE);
1711
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001712#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001713 /* Restore search/replace direction. */
1714 if (backwards_search_set)
1715 SET(BACKWARDS_SEARCH);
1716#endif
1717#ifdef HAVE_REGEX_H
1718 /* Restore regular expression usage setting. */
1719 if (regexp_set)
1720 SET(USE_REGEXP);
1721#endif
1722
1723 return !canceled;
1724}
1725
1726/* Integrated spell checking using the spell program, filtered through
1727 * the sort and uniq programs. Return NULL for normal termination,
1728 * and the error string otherwise. */
1729const char *do_int_speller(const char *tempfile_name)
1730{
1731 char *read_buff, *read_buff_ptr, *read_buff_word;
1732 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
1733 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
1734 pid_t pid_spell, pid_sort, pid_uniq;
1735 int spell_status, sort_status, uniq_status;
1736
1737 /* Create all three pipes up front. */
1738 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
1739 pipe(uniq_fd) == -1)
1740 return _("Could not create pipe");
1741
1742 statusbar(_("Creating misspelled word list, please wait..."));
1743
1744 /* A new process to run spell in. */
1745 if ((pid_spell = fork()) == 0) {
1746 /* Child continues (i.e, future spell process). */
1747 close(spell_fd[0]);
1748
1749 /* Replace the standard input with the temp file. */
1750 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1751 goto close_pipes_and_exit;
1752
1753 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1754 goto close_pipes_and_exit;
1755
1756 close(tempfile_fd);
1757
1758 /* Send spell's standard output to the pipe. */
1759 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1760 goto close_pipes_and_exit;
1761
1762 close(spell_fd[1]);
1763
1764 /* Start the spell program; we are using PATH. */
1765 execlp("spell", "spell", NULL);
1766
1767 /* This should not be reached if spell is found. */
1768 exit(1);
1769 }
1770
1771 /* Parent continues here. */
1772 close(spell_fd[1]);
1773
1774 /* A new process to run sort in. */
1775 if ((pid_sort = fork()) == 0) {
1776 /* Child continues (i.e, future spell process). Replace the
1777 * standard input with the standard output of the old pipe. */
1778 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1779 goto close_pipes_and_exit;
1780
1781 close(spell_fd[0]);
1782
1783 /* Send sort's standard output to the new pipe. */
1784 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1785 goto close_pipes_and_exit;
1786
1787 close(sort_fd[1]);
1788
1789 /* Start the sort program. Use -f to remove mixed case. If
1790 * this isn't portable, let me know. */
1791 execlp("sort", "sort", "-f", NULL);
1792
1793 /* This should not be reached if sort is found. */
1794 exit(1);
1795 }
1796
1797 close(spell_fd[0]);
1798 close(sort_fd[1]);
1799
1800 /* A new process to run uniq in. */
1801 if ((pid_uniq = fork()) == 0) {
1802 /* Child continues (i.e, future uniq process). Replace the
1803 * standard input with the standard output of the old pipe. */
1804 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
1805 goto close_pipes_and_exit;
1806
1807 close(sort_fd[0]);
1808
1809 /* Send uniq's standard output to the new pipe. */
1810 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1811 goto close_pipes_and_exit;
1812
1813 close(uniq_fd[1]);
1814
1815 /* Start the uniq program; we are using PATH. */
1816 execlp("uniq", "uniq", NULL);
1817
1818 /* This should not be reached if uniq is found. */
1819 exit(1);
1820 }
1821
1822 close(sort_fd[0]);
1823 close(uniq_fd[1]);
1824
1825 /* The child process was not forked successfully. */
1826 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
1827 close(uniq_fd[0]);
1828 return _("Could not fork");
1829 }
1830
1831 /* Get the system pipe buffer size. */
1832 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
1833 close(uniq_fd[0]);
1834 return _("Could not get size of pipe buffer");
1835 }
1836
1837 /* Read in the returned spelling errors. */
1838 read_buff_read = 0;
1839 read_buff_size = pipe_buff_size + 1;
1840 read_buff = read_buff_ptr = charalloc(read_buff_size);
1841
1842 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
1843 pipe_buff_size)) > 0) {
1844 read_buff_read += bytesread;
1845 read_buff_size += pipe_buff_size;
1846 read_buff = read_buff_ptr = charealloc(read_buff,
1847 read_buff_size);
1848 read_buff_ptr += read_buff_read;
1849 }
1850
1851 *read_buff_ptr = '\0';
1852 close(uniq_fd[0]);
1853
1854 /* Process the spelling errors. */
1855 read_buff_word = read_buff_ptr = read_buff;
1856
1857 while (*read_buff_ptr != '\0') {
1858 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
1859 *read_buff_ptr = '\0';
1860 if (read_buff_word != read_buff_ptr) {
1861 if (!do_int_spell_fix(read_buff_word)) {
1862 read_buff_word = read_buff_ptr;
1863 break;
1864 }
1865 }
1866 read_buff_word = read_buff_ptr + 1;
1867 }
1868 read_buff_ptr++;
1869 }
1870
1871 /* Special case: the last word doesn't end with '\r' or '\n'. */
1872 if (read_buff_word != read_buff_ptr)
1873 do_int_spell_fix(read_buff_word);
1874
1875 free(read_buff);
1876 replace_abort();
1877 edit_refresh();
1878
1879 /* Process the end of the spell process. */
1880 waitpid(pid_spell, &spell_status, 0);
1881 waitpid(pid_sort, &sort_status, 0);
1882 waitpid(pid_uniq, &uniq_status, 0);
1883
1884 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
1885 return _("Error invoking \"spell\"");
1886
1887 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
1888 return _("Error invoking \"sort -f\"");
1889
1890 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
1891 return _("Error invoking \"uniq\"");
1892
1893 /* Otherwise... */
1894 return NULL;
1895
1896 close_pipes_and_exit:
1897 /* Don't leak any handles. */
1898 close(tempfile_fd);
1899 close(spell_fd[0]);
1900 close(spell_fd[1]);
1901 close(sort_fd[0]);
1902 close(sort_fd[1]);
1903 close(uniq_fd[0]);
1904 close(uniq_fd[1]);
1905 exit(1);
1906}
1907
1908/* External spell checking. Return value: NULL for normal termination,
1909 * otherwise the error string. */
1910const char *do_alt_speller(char *tempfile_name)
1911{
1912 int alt_spell_status;
1913 size_t current_x_save = openfile->current_x;
1914 size_t pww_save = openfile->placewewant;
1915 ssize_t current_y_save = openfile->current_y;
1916 ssize_t lineno_save = openfile->current->lineno;
1917 pid_t pid_spell;
1918 char *ptr;
1919 static int arglen = 3;
1920 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001921#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001922 bool old_mark_set = openfile->mark_set;
1923 bool added_magicline = FALSE;
1924 /* Whether we added a magicline after filebot. */
1925 bool right_side_up = FALSE;
1926 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1927 * FALSE if (current, current_x) is. */
1928 filestruct *top, *bot;
1929 size_t top_x, bot_x;
1930 ssize_t mb_lineno_save = 0;
1931 /* We're going to close the current file, and open the output of
1932 * the alternate spell command. The line that mark_begin points
1933 * to will be freed, so we save the line number and restore it
1934 * afterwards. */
1935 size_t totsize_save = openfile->totsize;
1936 /* Our saved value of totsize, used when we spell-check a marked
1937 * selection. */
1938
1939 if (old_mark_set) {
1940 /* If the mark is on, save the number of the line it starts on,
1941 * and then turn the mark off. */
1942 mb_lineno_save = openfile->mark_begin->lineno;
1943 openfile->mark_set = FALSE;
1944 }
1945#endif
1946
1947 endwin();
1948
1949 /* Set up an argument list to pass execvp(). */
1950 if (spellargs == NULL) {
1951 spellargs = (char **)nmalloc(arglen * sizeof(char *));
1952
1953 spellargs[0] = strtok(alt_speller, " ");
1954 while ((ptr = strtok(NULL, " ")) != NULL) {
1955 arglen++;
1956 spellargs = (char **)nrealloc(spellargs, arglen *
1957 sizeof(char *));
1958 spellargs[arglen - 3] = ptr;
1959 }
1960 spellargs[arglen - 1] = NULL;
1961 }
1962 spellargs[arglen - 2] = tempfile_name;
1963
1964 /* Start a new process for the alternate speller. */
1965 if ((pid_spell = fork()) == 0) {
1966 /* Start alternate spell program; we are using PATH. */
1967 execvp(spellargs[0], spellargs);
1968
1969 /* Should not be reached, if alternate speller is found!!! */
1970 exit(1);
1971 }
1972
1973 /* If we couldn't fork, get out. */
1974 if (pid_spell < 0)
1975 return _("Could not fork");
1976
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001977#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001978 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001979 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001980 allow_pending_sigwinch(FALSE);
1981#endif
1982
1983 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001984 wait(&alt_spell_status);
1985
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00001986 /* Reenter curses mode. */
1987 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001988
1989 /* Restore the terminal to its previous state. */
1990 terminal_init();
1991
1992 /* Turn the cursor back on for sure. */
1993 curs_set(1);
1994
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001995 /* The screen might have been resized. If it has, reinitialize all
1996 * the windows based on the new screen dimensions. */
1997 window_init();
1998
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001999 if (!WIFEXITED(alt_spell_status) ||
2000 WEXITSTATUS(alt_spell_status) != 0) {
2001 char *altspell_error;
2002 char *invoke_error = _("Error invoking \"%s\"");
2003
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002004#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002005 /* Turn the mark back on if it was on before. */
2006 openfile->mark_set = old_mark_set;
2007#endif
2008
2009 altspell_error =
2010 charalloc(strlen(invoke_error) +
2011 strlen(alt_speller) + 1);
2012 sprintf(altspell_error, invoke_error, alt_speller);
2013 return altspell_error;
2014 }
2015
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002016#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002017 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002018 /* If the mark is on, partition the filestruct so that it
2019 * contains only the marked text; if the NO_NEWLINES flag isn't
2020 * set, keep track of whether the text will have a magicline
2021 * added when we're done correcting misspelled words; and
2022 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002023 mark_order((const filestruct **)&top, &top_x,
2024 (const filestruct **)&bot, &bot_x, &right_side_up);
2025 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002026 if (!ISSET(NO_NEWLINES))
2027 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002028
2029 /* Get the number of characters in the marked text, and subtract
2030 * it from the saved value of totsize. */
2031 totsize_save -= get_totsize(top, bot);
2032 }
2033#endif
2034
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002035 /* Replace the text of the current buffer with the spell-checked
2036 * text. */
2037 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002038
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002039#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002040 if (old_mark_set) {
2041 filestruct *top_save = openfile->fileage;
2042
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002043 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2044 * added a magicline, remove it now. */
2045 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002046 remove_magicline();
2047
2048 /* Put the beginning and the end of the mark at the beginning
2049 * and the end of the spell-checked text. */
2050 if (openfile->fileage == openfile->filebot)
2051 bot_x += top_x;
2052 if (right_side_up) {
2053 openfile->mark_begin_x = top_x;
2054 current_x_save = bot_x;
2055 } else {
2056 current_x_save = top_x;
2057 openfile->mark_begin_x = bot_x;
2058 }
2059
2060 /* Unpartition the filestruct so that it contains all the text
2061 * again. Note that we've replaced the marked text originally
2062 * in the partition with the spell-checked marked text in the
2063 * temp file. */
2064 unpartition_filestruct(&filepart);
2065
2066 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002067 * partition. Also add the number of characters in the
2068 * spell-checked marked text to the saved value of totsize, and
2069 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002070 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002071 totsize_save += openfile->totsize;
2072 openfile->totsize = totsize_save;
2073
2074 /* Assign mark_begin to the line where the mark began before. */
2075 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2076 current_y_save, 0);
2077 openfile->mark_begin = openfile->current;
2078
2079 /* Assign mark_begin_x to the location in mark_begin where the
2080 * mark began before, adjusted for any shortening of the
2081 * line. */
2082 openfile->mark_begin_x = openfile->current_x;
2083
2084 /* Turn the mark back on. */
2085 openfile->mark_set = TRUE;
2086 }
2087#endif
2088
2089 /* Go back to the old position, and mark the file as modified. */
2090 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2091 set_modified();
2092
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002093#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002094 /* Handle a pending SIGWINCH again. */
2095 allow_pending_sigwinch(TRUE);
2096#endif
2097
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002098 return NULL;
2099}
2100
2101void do_spell(void)
2102{
2103 int i;
2104 FILE *temp_file;
2105 char *temp = safe_tempfile(&temp_file);
2106 const char *spell_msg;
2107
2108 if (temp == NULL) {
2109 statusbar(_("Could not create temp file: %s"), strerror(errno));
2110 return;
2111 }
2112
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002113#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002114 if (openfile->mark_set)
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002115 i = write_marked_file(temp, temp_file, TRUE, OVERWRITE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002116 else
2117#endif
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002118 i = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002119
2120 if (i == -1) {
2121 statusbar(_("Error writing temp file: %s"), strerror(errno));
2122 free(temp);
2123 return;
2124 }
2125
2126 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2127 do_int_speller(temp);
2128 unlink(temp);
2129 free(temp);
2130
2131 /* If the spell-checker printed any error messages onscreen, make
2132 * sure that they're cleared off. */
2133 total_refresh();
2134
2135 if (spell_msg != NULL) {
2136 if (errno == 0)
2137 /* Don't display an error message of "Success". */
2138 statusbar(_("Spell checking failed: %s"), spell_msg);
2139 else
2140 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2141 strerror(errno));
2142 } else
2143 statusbar(_("Finished checking spelling"));
2144}
2145#endif /* !DISABLE_SPELLER */
2146
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002147#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002148/* Our own version of "wc". Note that its character counts are in
2149 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002150void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002151{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002152 size_t words = 0, chars = 0;
2153 ssize_t lines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002154 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002155 size_t pww_save = openfile->placewewant;
2156 filestruct *current_save = openfile->current;
2157 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002158 filestruct *top, *bot;
2159 size_t top_x, bot_x;
2160
2161 if (old_mark_set) {
2162 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002163 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002164 mark_order((const filestruct **)&top, &top_x,
2165 (const filestruct **)&bot, &bot_x, NULL);
2166 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002167 openfile->mark_set = FALSE;
2168 }
2169
2170 /* Start at the top of the file. */
2171 openfile->current = openfile->fileage;
2172 openfile->current_x = 0;
2173 openfile->placewewant = 0;
2174
2175 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002176 * part of a word, as "wc -w" does), without updating the screen,
2177 * until we reach the end of the file, incrementing the total word
2178 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002179 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002180 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002181 if (do_next_word(TRUE, FALSE))
2182 words++;
2183 }
2184
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002185 /* Get the total line and character counts, as "wc -l" and "wc -c"
2186 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002187 if (old_mark_set) {
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002188 lines = openfile->filebot->lineno -
2189 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002190 chars = get_totsize(openfile->fileage, openfile->filebot);
2191
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002192 /* Unpartition the filestruct so that it contains all the text
2193 * again, and turn the mark back on. */
2194 unpartition_filestruct(&filepart);
2195 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002196 } else {
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002197 lines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002198 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002199 }
2200
2201 /* Restore where we were. */
2202 openfile->current = current_save;
2203 openfile->current_x = current_x_save;
2204 openfile->placewewant = pww_save;
2205
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002206 /* Display the total word, line, and character counts on the
2207 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002208 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
David Lawrence Ramsey815fb0a2005-08-05 19:38:11 +00002209 _("In Selection: ") : "", (unsigned long)words, (long)lines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002210 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002211}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002212#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002213
2214void do_verbatim_input(void)
2215{
2216 int *kbinput;
2217 size_t kbinput_len, i;
2218 char *output;
2219
2220 statusbar(_("Verbatim Input"));
2221
2222 /* If constant cursor position display is on, make sure the current
2223 * cursor position will be properly displayed on the statusbar. */
2224 if (ISSET(CONST_UPDATE))
2225 do_cursorpos(TRUE);
2226
2227 /* Read in all the verbatim characters. */
2228 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2229
2230 /* Display all the verbatim characters at once, not filtering out
2231 * control characters. */
2232 output = charalloc(kbinput_len + 1);
2233
2234 for (i = 0; i < kbinput_len; i++)
2235 output[i] = (char)kbinput[i];
2236 output[i] = '\0';
2237
2238 do_output(output, kbinput_len, TRUE);
2239
2240 free(output);
2241}