blob: 33effee8a661927202ee55ed0d9a0da6ff7802e2 [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;
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000261 char *shellenv;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000262 struct sigaction oldaction, newaction;
263 /* Original and temporary handlers for SIGINT. */
264 bool sig_failed = FALSE;
265 /* Did sigaction() fail without changing the signal handlers? */
266
267 /* Make our pipes. */
268 if (pipe(fd) == -1) {
269 statusbar(_("Could not pipe"));
270 return FALSE;
271 }
272
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000273 /* Check $SHELL for the shell to use. If it isn't set, use
David Lawrence Ramsey1932dfb2005-11-29 05:52:49 +0000274 * /bin/sh. Note that $SHELL should contain only a path, with no
275 * arguments. */
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000276 shellenv = getenv("SHELL");
277 if (shellenv == NULL)
278 shellenv = "/bin/sh";
279
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000280 /* Fork a child. */
281 if ((pid = fork()) == 0) {
282 close(fd[0]);
283 dup2(fd[1], fileno(stdout));
284 dup2(fd[1], fileno(stderr));
285
286 /* 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 Ramseyc7c04bb2005-11-29 21:30:00 +0000586#if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000587/* 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}
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +0000679#endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000680
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
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001069/* 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.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001074 *
1075 * See the comment at begpar() for more about when a line is the
1076 * beginning of a paragraph. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001077bool find_paragraph(size_t *const quote, size_t *const par)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001078{
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 Ramsey9a065c02005-11-29 18:25:53 +00001109
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001110 /* If we end up past the beginning of the line, it means that
1111 * we're at the end of the last line of the file, and the line
1112 * isn't blank, in which case the last line of the file is the
1113 * last line of the next paragraph.
1114 *
1115 * Otherwise, if we end up on a line that's in a paragraph, it
1116 * means that we're on the line after the last line of the next
1117 * paragraph, in which case we should move back to the last line
1118 * of the next paragraph. */
1119 if (openfile->current_x == 0) {
1120 if (!inpar(openfile->current->prev))
1121 return FALSE;
1122 if (openfile->current != openfile->fileage)
1123 openfile->current = openfile->current->prev;
1124 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001125 }
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001126
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001127 /* If the current line isn't the first line of the paragraph, move
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001128 * back to the first line of the paragraph. */
1129 if (!begpar(openfile->current))
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001130 do_para_begin(FALSE);
1131
1132 /* Now current is the first line of the paragraph. Set quote_len to
1133 * the quotation length of that line, and set par_len to the number
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001134 * of lines in this paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001135 quote_len = quote_length(openfile->current->data);
1136 current_save = openfile->current;
1137 current_y_save = openfile->current_y;
1138 do_para_end(FALSE);
1139 par_len = openfile->current->lineno - current_save->lineno;
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001140
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001141 /* If we end up past the beginning of the line, it means that we're
1142 * at the end of the last line of the file, and the line isn't
1143 * blank, in which case the last line of the file is part of the
1144 * paragraph. */
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001145 if (openfile->current_x > 0)
1146 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001147 openfile->current = current_save;
1148 openfile->current_y = current_y_save;
1149
1150 /* Save the values of quote_len and par_len. */
1151 assert(quote != NULL && par != NULL);
1152
1153 *quote = quote_len;
1154 *par = par_len;
1155
1156 return TRUE;
1157}
1158
1159/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1160 * the current paragraph. */
1161void do_justify(bool full_justify)
1162{
1163 filestruct *first_par_line = NULL;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001164 /* Will be the first line of the justified paragraph. For
1165 * restoring after unjustify. */
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001166 filestruct *last_par_line = NULL;
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001167 /* Will be the line after the last line of the justified
1168 * paragraph, if any. Also for restoring after unjustify. */
David Lawrence Ramsey82b5deb2005-11-10 06:07:57 +00001169 bool filebot_inpar = FALSE;
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001170 /* Whether the text at filebot is part of the current
1171 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001172
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001173 /* We save these variables to be restored if the user
1174 * unjustifies. */
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001175 filestruct *edittop_save = openfile->edittop;
1176 filestruct *current_save = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001177 size_t current_x_save = openfile->current_x;
1178 size_t pww_save = openfile->placewewant;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001179 size_t totsize_save = openfile->totsize;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001180#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001181 filestruct *mark_begin_save = openfile->mark_begin;
1182 size_t mark_begin_x_save = openfile->mark_begin_x;
1183#endif
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001184 bool modified_save = openfile->modified;
1185
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001186 int kbinput;
1187 bool meta_key, func_key, s_or_t, ran_func, finished;
1188
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001189 /* Move to the beginning of the current line, so that justifying at
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001190 * the end of the last line of the file, if that line isn't blank,
1191 * will work the first time through. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001192 openfile->current_x = 0;
1193
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001194 /* If we're justifying the entire file, start at the beginning. */
1195 if (full_justify)
1196 openfile->current = openfile->fileage;
1197
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001198 while (TRUE) {
1199 size_t i;
1200 /* Generic loop variable. */
1201 size_t quote_len;
1202 /* Length of the initial quotation of the paragraph we
1203 * justify. */
1204 size_t indent_len;
1205 /* Length of the initial indentation of the paragraph we
1206 * justify. */
1207 size_t par_len;
1208 /* Number of lines in the paragraph we justify. */
1209 ssize_t break_pos;
1210 /* Where we will break lines. */
1211 char *indent_string;
1212 /* The first indentation that doesn't match the initial
1213 * indentation of the paragraph we justify. This is put at
1214 * the beginning of every line broken off the first
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001215 * justified line of the paragraph. Note that this works
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001216 * because a paragraph can only contain two indentations at
1217 * most: the initial one, and a different one starting on a
1218 * line after the first. See the comment at begpar() for
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001219 * more about when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001220
1221 /* Find the first line of the paragraph to be justified. That
1222 * is the start of this paragraph if we're in one, or the start
1223 * of the next otherwise. Save the quote length and paragraph
1224 * length (number of lines). Don't refresh the screen yet,
1225 * since we'll do that after we justify.
1226 *
1227 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001228 * justifying the whole file, and we've found at least one
1229 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001230 * last line of the file, so set the last line of the text to be
1231 * justified to the last line of the file and break out of the
1232 * loop. Otherwise, it means that there are no paragraph(s) to
1233 * justify, so refresh the screen and get out. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001234 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001235 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001236 last_par_line = openfile->filebot;
1237 break;
1238 } else {
1239 edit_refresh();
1240 return;
1241 }
1242 }
1243
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001244 /* par_len will be one greater than the number of lines between
1245 * current and filebot if filebot is the last line in the
1246 * paragraph. Set filebot_inpar to TRUE if this is the case. */
1247 filebot_inpar = (openfile->current->lineno + par_len ==
1248 openfile->filebot->lineno + 1);
1249
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001250 /* If we haven't already done it, move the original paragraph(s)
1251 * to the justify buffer, splice a copy of the original
1252 * paragraph(s) into the file in the same place, and set
1253 * first_par_line to the first line of the copy. */
1254 if (first_par_line == NULL) {
1255 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00001256 openfile->filebot->lineno - openfile->current->lineno +
1257 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001258 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001259 first_par_line = openfile->current;
1260 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001261
1262 /* Initialize indent_string to a blank string. */
1263 indent_string = mallocstrcpy(NULL, "");
1264
1265 /* Find the first indentation in the paragraph that doesn't
1266 * match the indentation of the first line, and save it in
1267 * indent_string. If all the indentations are the same, save
1268 * the indentation of the first line in indent_string. */
1269 {
1270 const filestruct *indent_line = openfile->current;
1271 bool past_first_line = FALSE;
1272
1273 for (i = 0; i < par_len; i++) {
1274 indent_len = quote_len +
1275 indent_length(indent_line->data + quote_len);
1276
1277 if (indent_len != strlen(indent_string)) {
1278 indent_string = mallocstrncpy(indent_string,
1279 indent_line->data, indent_len + 1);
1280 indent_string[indent_len] = '\0';
1281
1282 if (past_first_line)
1283 break;
1284 }
1285
1286 if (indent_line == openfile->current)
1287 past_first_line = TRUE;
1288
1289 indent_line = indent_line->next;
1290 }
1291 }
1292
1293 /* Now tack all the lines of the paragraph together, skipping
1294 * the quoting and indentation on all lines after the first. */
1295 for (i = 0; i < par_len - 1; i++) {
1296 filestruct *next_line = openfile->current->next;
1297 size_t line_len = strlen(openfile->current->data);
1298 size_t next_line_len =
1299 strlen(openfile->current->next->data);
1300
1301 indent_len = quote_len +
1302 indent_length(openfile->current->next->data +
1303 quote_len);
1304
1305 next_line_len -= indent_len;
1306 openfile->totsize -= indent_len;
1307
1308 /* We're just about to tack the next line onto this one. If
1309 * this line isn't empty, make sure it ends in a space. */
1310 if (line_len > 0 &&
1311 openfile->current->data[line_len - 1] != ' ') {
1312 line_len++;
1313 openfile->current->data =
1314 charealloc(openfile->current->data,
1315 line_len + 1);
1316 openfile->current->data[line_len - 1] = ' ';
1317 openfile->current->data[line_len] = '\0';
1318 openfile->totsize++;
1319 }
1320
1321 openfile->current->data =
1322 charealloc(openfile->current->data, line_len +
1323 next_line_len + 1);
1324 strcat(openfile->current->data, next_line->data +
1325 indent_len);
1326
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00001327 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00001328 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001329 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00001330 if (next_line == openfile->filebot)
1331 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001332
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001333#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001334 /* Adjust the mark coordinates to compensate for the change
1335 * in the next line. */
1336 if (openfile->mark_set && openfile->mark_begin ==
1337 next_line) {
1338 openfile->mark_begin = openfile->current;
1339 openfile->mark_begin_x += line_len - indent_len;
1340 }
1341#endif
1342
1343 unlink_node(next_line);
1344 delete_node(next_line);
1345
1346 /* If we've removed the next line, we need to go through
1347 * this line again. */
1348 i--;
1349
1350 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001351 openfile->totsize--;
1352 }
1353
1354 /* Call justify_format() on the paragraph, which will remove
1355 * excess spaces from it and change all blank characters to
1356 * spaces. */
1357 justify_format(openfile->current, quote_len +
1358 indent_length(openfile->current->data + quote_len));
1359
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001360 while (par_len > 0 && strlenpt(openfile->current->data) >
1361 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001362 size_t line_len = strlen(openfile->current->data);
1363
1364 indent_len = strlen(indent_string);
1365
1366 /* If this line is too long, try to wrap it to the next line
1367 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001368 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001369 fill - strnlenpt(openfile->current->data, indent_len)
1370#ifndef DISABLE_HELP
1371 , FALSE
1372#endif
1373 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001374
1375 /* We can't break the line, or don't need to, so get out. */
1376 if (break_pos == -1 || break_pos + indent_len == line_len)
1377 break;
1378
1379 /* Move forward to the character after the indentation and
1380 * just after the space. */
1381 break_pos += indent_len + 1;
1382
1383 assert(break_pos <= line_len);
1384
1385 /* Make a new line, and copy the text after where we're
1386 * going to break this line to the beginning of the new
1387 * line. */
1388 splice_node(openfile->current,
1389 make_new_node(openfile->current),
1390 openfile->current->next);
1391
1392 /* If this paragraph is non-quoted, and autoindent isn't
1393 * turned on, set the indentation length to zero so that the
1394 * indentation is treated as part of the line. */
1395 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001396#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001397 && !ISSET(AUTOINDENT)
1398#endif
1399 )
1400 indent_len = 0;
1401
1402 /* Copy the text after where we're going to break the
1403 * current line to the next line. */
1404 openfile->current->next->data = charalloc(indent_len + 1 +
1405 line_len - break_pos);
1406 strncpy(openfile->current->next->data, indent_string,
1407 indent_len);
1408 strcpy(openfile->current->next->data + indent_len,
1409 openfile->current->data + break_pos);
1410
1411 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001412 openfile->totsize += indent_len + 1;
1413
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001414#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001415 /* Adjust the mark coordinates to compensate for the change
1416 * in the current line. */
1417 if (openfile->mark_set && openfile->mark_begin ==
1418 openfile->current && openfile->mark_begin_x >
1419 break_pos) {
1420 openfile->mark_begin = openfile->current->next;
1421 openfile->mark_begin_x -= break_pos - indent_len;
1422 }
1423#endif
1424
1425 /* Break the current line. */
1426 null_at(&openfile->current->data, break_pos);
1427
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001428 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00001429 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001430 if (openfile->filebot == openfile->current)
1431 openfile->filebot = openfile->filebot->next;
1432
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001433 /* Go to the next line. */
1434 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001435 openfile->current_y++;
1436 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001437 }
1438
1439 /* We're done breaking lines, so we don't need indent_string
1440 * anymore. */
1441 free(indent_string);
1442
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001443 /* Go to the next line, if possible. If there is no next line,
1444 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001445 if (openfile->current != openfile->filebot) {
1446 openfile->current_y++;
1447 openfile->current = openfile->current->next;
1448 } else
1449 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001450
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00001451 /* Renumber the lines of the now-justified paragraph, since both
1452 * find_paragraph() and edit_refresh() need the line numbers to
1453 * be right. */
1454 renumber(first_par_line);
1455
1456 /* We've just finished justifying the paragraph. If we're not
1457 * justifying the entire file, break out of the loop.
1458 * Otherwise, continue the loop so that we justify all the
1459 * paragraphs in the file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001460 if (!full_justify)
1461 break;
1462 }
1463
1464 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001465 * up. current_y and totsize have been maintained above. If we
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00001466 * actually justified something, set last_par_line to the new end of
1467 * the paragraph. */
1468 if (first_par_line != NULL)
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001469 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001470
1471 edit_refresh();
1472
1473 statusbar(_("Can now UnJustify!"));
1474
1475 /* If constant cursor position display is on, make sure the current
1476 * cursor position will be properly displayed on the statusbar. */
1477 if (ISSET(CONST_UPDATE))
1478 do_cursorpos(TRUE);
1479
1480 /* Display the shortcut list with UnJustify. */
1481 shortcut_init(TRUE);
1482 display_main_list();
1483
1484 /* Now get a keystroke and see if it's unjustify. If not, put back
1485 * the keystroke and return. */
1486 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
1487 &finished, FALSE);
1488
1489 if (!meta_key && !func_key && s_or_t &&
1490 kbinput == NANO_UNJUSTIFY_KEY) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001491 /* Splice the justify buffer back into the file, but only if we
1492 * actually justified something. */
1493 if (first_par_line != NULL) {
1494 filestruct *top_save;
1495
1496 /* Partition the filestruct so that it contains only the
1497 * text of the justified paragraph. */
1498 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00001499 last_par_line, filebot_inpar ?
1500 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001501
1502 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001503 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001504 free_filestruct(openfile->fileage);
1505 openfile->fileage = jusbuffer;
1506 openfile->filebot = jusbottom;
1507
1508 top_save = openfile->fileage;
1509
1510 /* Unpartition the filestruct so that it contains all the
1511 * text again. Note that the justified paragraph has been
1512 * replaced with the unjustified paragraph. */
1513 unpartition_filestruct(&filepart);
1514
1515 /* Renumber starting with the beginning line of the old
1516 * partition. */
1517 renumber(top_save);
1518
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001519 /* Restore the justify we just did (ungrateful user!). */
1520 openfile->edittop = edittop_save;
1521 openfile->current = current_save;
1522 openfile->current_x = current_x_save;
1523 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001524 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001525#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001526 if (openfile->mark_set) {
1527 openfile->mark_begin = mark_begin_save;
1528 openfile->mark_begin_x = mark_begin_x_save;
1529 }
1530#endif
1531 openfile->modified = modified_save;
1532
1533 /* Clear the justify buffer. */
1534 jusbuffer = NULL;
1535
1536 if (!openfile->modified)
1537 titlebar(NULL);
1538 edit_refresh();
1539 }
1540 } else {
1541 unget_kbinput(kbinput, meta_key, func_key);
1542
1543 /* Blow away the text in the justify buffer. */
1544 free_filestruct(jusbuffer);
1545 jusbuffer = NULL;
1546 }
1547
1548 blank_statusbar();
1549
1550 /* Display the shortcut list with UnCut. */
1551 shortcut_init(FALSE);
1552 display_main_list();
1553}
1554
1555void do_justify_void(void)
1556{
1557 do_justify(FALSE);
1558}
1559
1560void do_full_justify(void)
1561{
1562 do_justify(TRUE);
1563}
1564#endif /* !DISABLE_JUSTIFY */
1565
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001566#ifndef DISABLE_SPELLER
1567/* A word is misspelled in the file. Let the user replace it. We
1568 * return FALSE if the user cancels. */
1569bool do_int_spell_fix(const char *word)
1570{
1571 char *save_search, *save_replace;
1572 size_t match_len, current_x_save = openfile->current_x;
1573 size_t pww_save = openfile->placewewant;
1574 filestruct *edittop_save = openfile->edittop;
1575 filestruct *current_save = openfile->current;
1576 /* Save where we are. */
1577 bool canceled = FALSE;
1578 /* The return value. */
1579 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001580#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001581 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
1582#endif
1583#ifdef HAVE_REGEX_H
1584 bool regexp_set = ISSET(USE_REGEXP);
1585#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001586#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001587 bool old_mark_set = openfile->mark_set;
1588 bool added_magicline = FALSE;
1589 /* Whether we added a magicline after filebot. */
1590 bool right_side_up = FALSE;
1591 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1592 * FALSE if (current, current_x) is. */
1593 filestruct *top, *bot;
1594 size_t top_x, bot_x;
1595#endif
1596
1597 /* Make sure spell-check is case sensitive. */
1598 SET(CASE_SENSITIVE);
1599
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001600#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001601 /* Make sure spell-check goes forward only. */
1602 UNSET(BACKWARDS_SEARCH);
1603#endif
1604#ifdef HAVE_REGEX_H
1605 /* Make sure spell-check doesn't use regular expressions. */
1606 UNSET(USE_REGEXP);
1607#endif
1608
1609 /* Save the current search/replace strings. */
1610 search_init_globals();
1611 save_search = last_search;
1612 save_replace = last_replace;
1613
1614 /* Set the search/replace strings to the misspelled word. */
1615 last_search = mallocstrcpy(NULL, word);
1616 last_replace = mallocstrcpy(NULL, word);
1617
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001618#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001619 if (old_mark_set) {
1620 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001621 * contains only the marked text; if the NO_NEWLINES flag isn't
1622 * set, keep track of whether the text will have a magicline
1623 * added when we're done correcting misspelled words; and
1624 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001625 mark_order((const filestruct **)&top, &top_x,
1626 (const filestruct **)&bot, &bot_x, &right_side_up);
1627 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001628 if (!ISSET(NO_NEWLINES))
1629 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001630 openfile->mark_set = FALSE;
1631 }
1632#endif
1633
1634 /* Start from the top of the file. */
1635 openfile->edittop = openfile->fileage;
1636 openfile->current = openfile->fileage;
1637 openfile->current_x = (size_t)-1;
1638 openfile->placewewant = 0;
1639
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001640 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001641 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001642 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001643 &match_len)) {
1644 if (is_whole_word(openfile->current_x, openfile->current->data,
1645 word)) {
1646 size_t xpt = xplustabs();
1647 char *exp_word = display_string(openfile->current->data,
1648 xpt, strnlenpt(openfile->current->data,
1649 openfile->current_x + match_len) - xpt, FALSE);
1650
1651 edit_refresh();
1652
1653 do_replace_highlight(TRUE, exp_word);
1654
1655 /* Allow all instances of the word to be corrected. */
David Lawrence Ramseye19449e2005-11-07 21:45:44 +00001656 canceled = (do_prompt(FALSE, spell_list, word,
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001657#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001658 NULL,
1659#endif
1660 _("Edit a replacement")) == -1);
1661
1662 do_replace_highlight(FALSE, exp_word);
1663
1664 free(exp_word);
1665
1666 if (!canceled && strcmp(word, answer) != 0) {
1667 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001668 do_replace_loop(TRUE, &canceled, openfile->current,
1669 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001670 }
1671
1672 break;
1673 }
1674 }
1675
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001676#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001677 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001678 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
1679 * added a magicline, remove it now. */
1680 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001681 remove_magicline();
1682
1683 /* Put the beginning and the end of the mark at the beginning
1684 * and the end of the spell-checked text. */
1685 if (openfile->fileage == openfile->filebot)
1686 bot_x += top_x;
1687 if (right_side_up) {
1688 openfile->mark_begin_x = top_x;
1689 current_x_save = bot_x;
1690 } else {
1691 current_x_save = top_x;
1692 openfile->mark_begin_x = bot_x;
1693 }
1694
1695 /* Unpartition the filestruct so that it contains all the text
1696 * again, and turn the mark back on. */
1697 unpartition_filestruct(&filepart);
1698 openfile->mark_set = TRUE;
1699 }
1700#endif
1701
1702 /* Restore the search/replace strings. */
1703 free(last_search);
1704 last_search = save_search;
1705 free(last_replace);
1706 last_replace = save_replace;
1707
1708 /* Restore where we were. */
1709 openfile->edittop = edittop_save;
1710 openfile->current = current_save;
1711 openfile->current_x = current_x_save;
1712 openfile->placewewant = pww_save;
1713
1714 /* Restore case sensitivity setting. */
1715 if (!case_sens_set)
1716 UNSET(CASE_SENSITIVE);
1717
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001718#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001719 /* Restore search/replace direction. */
1720 if (backwards_search_set)
1721 SET(BACKWARDS_SEARCH);
1722#endif
1723#ifdef HAVE_REGEX_H
1724 /* Restore regular expression usage setting. */
1725 if (regexp_set)
1726 SET(USE_REGEXP);
1727#endif
1728
1729 return !canceled;
1730}
1731
1732/* Integrated spell checking using the spell program, filtered through
1733 * the sort and uniq programs. Return NULL for normal termination,
1734 * and the error string otherwise. */
1735const char *do_int_speller(const char *tempfile_name)
1736{
1737 char *read_buff, *read_buff_ptr, *read_buff_word;
1738 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
1739 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
1740 pid_t pid_spell, pid_sort, pid_uniq;
1741 int spell_status, sort_status, uniq_status;
1742
1743 /* Create all three pipes up front. */
1744 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
1745 pipe(uniq_fd) == -1)
1746 return _("Could not create pipe");
1747
1748 statusbar(_("Creating misspelled word list, please wait..."));
1749
1750 /* A new process to run spell in. */
1751 if ((pid_spell = fork()) == 0) {
1752 /* Child continues (i.e, future spell process). */
1753 close(spell_fd[0]);
1754
1755 /* Replace the standard input with the temp file. */
1756 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1757 goto close_pipes_and_exit;
1758
1759 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1760 goto close_pipes_and_exit;
1761
1762 close(tempfile_fd);
1763
1764 /* Send spell's standard output to the pipe. */
1765 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1766 goto close_pipes_and_exit;
1767
1768 close(spell_fd[1]);
1769
David Lawrence Ramsey3239ff22005-11-29 20:01:06 +00001770 /* Start the spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001771 execlp("spell", "spell", NULL);
1772
1773 /* This should not be reached if spell is found. */
1774 exit(1);
1775 }
1776
1777 /* Parent continues here. */
1778 close(spell_fd[1]);
1779
1780 /* A new process to run sort in. */
1781 if ((pid_sort = fork()) == 0) {
1782 /* Child continues (i.e, future spell process). Replace the
1783 * standard input with the standard output of the old pipe. */
1784 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1785 goto close_pipes_and_exit;
1786
1787 close(spell_fd[0]);
1788
1789 /* Send sort's standard output to the new pipe. */
1790 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1791 goto close_pipes_and_exit;
1792
1793 close(sort_fd[1]);
1794
1795 /* Start the sort program. Use -f to remove mixed case. If
1796 * this isn't portable, let me know. */
1797 execlp("sort", "sort", "-f", NULL);
1798
1799 /* This should not be reached if sort is found. */
1800 exit(1);
1801 }
1802
1803 close(spell_fd[0]);
1804 close(sort_fd[1]);
1805
1806 /* A new process to run uniq in. */
1807 if ((pid_uniq = fork()) == 0) {
1808 /* Child continues (i.e, future uniq process). Replace the
1809 * standard input with the standard output of the old pipe. */
1810 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
1811 goto close_pipes_and_exit;
1812
1813 close(sort_fd[0]);
1814
1815 /* Send uniq's standard output to the new pipe. */
1816 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1817 goto close_pipes_and_exit;
1818
1819 close(uniq_fd[1]);
1820
1821 /* Start the uniq program; we are using PATH. */
1822 execlp("uniq", "uniq", NULL);
1823
1824 /* This should not be reached if uniq is found. */
1825 exit(1);
1826 }
1827
1828 close(sort_fd[0]);
1829 close(uniq_fd[1]);
1830
1831 /* The child process was not forked successfully. */
1832 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
1833 close(uniq_fd[0]);
1834 return _("Could not fork");
1835 }
1836
1837 /* Get the system pipe buffer size. */
1838 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
1839 close(uniq_fd[0]);
1840 return _("Could not get size of pipe buffer");
1841 }
1842
1843 /* Read in the returned spelling errors. */
1844 read_buff_read = 0;
1845 read_buff_size = pipe_buff_size + 1;
1846 read_buff = read_buff_ptr = charalloc(read_buff_size);
1847
1848 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
1849 pipe_buff_size)) > 0) {
1850 read_buff_read += bytesread;
1851 read_buff_size += pipe_buff_size;
1852 read_buff = read_buff_ptr = charealloc(read_buff,
1853 read_buff_size);
1854 read_buff_ptr += read_buff_read;
1855 }
1856
1857 *read_buff_ptr = '\0';
1858 close(uniq_fd[0]);
1859
1860 /* Process the spelling errors. */
1861 read_buff_word = read_buff_ptr = read_buff;
1862
1863 while (*read_buff_ptr != '\0') {
1864 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
1865 *read_buff_ptr = '\0';
1866 if (read_buff_word != read_buff_ptr) {
1867 if (!do_int_spell_fix(read_buff_word)) {
1868 read_buff_word = read_buff_ptr;
1869 break;
1870 }
1871 }
1872 read_buff_word = read_buff_ptr + 1;
1873 }
1874 read_buff_ptr++;
1875 }
1876
1877 /* Special case: the last word doesn't end with '\r' or '\n'. */
1878 if (read_buff_word != read_buff_ptr)
1879 do_int_spell_fix(read_buff_word);
1880
1881 free(read_buff);
1882 replace_abort();
1883 edit_refresh();
1884
1885 /* Process the end of the spell process. */
1886 waitpid(pid_spell, &spell_status, 0);
1887 waitpid(pid_sort, &sort_status, 0);
1888 waitpid(pid_uniq, &uniq_status, 0);
1889
1890 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
1891 return _("Error invoking \"spell\"");
1892
1893 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
1894 return _("Error invoking \"sort -f\"");
1895
1896 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
1897 return _("Error invoking \"uniq\"");
1898
1899 /* Otherwise... */
1900 return NULL;
1901
1902 close_pipes_and_exit:
1903 /* Don't leak any handles. */
1904 close(tempfile_fd);
1905 close(spell_fd[0]);
1906 close(spell_fd[1]);
1907 close(sort_fd[0]);
1908 close(sort_fd[1]);
1909 close(uniq_fd[0]);
1910 close(uniq_fd[1]);
1911 exit(1);
1912}
1913
1914/* External spell checking. Return value: NULL for normal termination,
1915 * otherwise the error string. */
1916const char *do_alt_speller(char *tempfile_name)
1917{
1918 int alt_spell_status;
1919 size_t current_x_save = openfile->current_x;
1920 size_t pww_save = openfile->placewewant;
1921 ssize_t current_y_save = openfile->current_y;
1922 ssize_t lineno_save = openfile->current->lineno;
1923 pid_t pid_spell;
1924 char *ptr;
1925 static int arglen = 3;
1926 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001927#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001928 bool old_mark_set = openfile->mark_set;
1929 bool added_magicline = FALSE;
1930 /* Whether we added a magicline after filebot. */
1931 bool right_side_up = FALSE;
1932 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1933 * FALSE if (current, current_x) is. */
1934 filestruct *top, *bot;
1935 size_t top_x, bot_x;
1936 ssize_t mb_lineno_save = 0;
1937 /* We're going to close the current file, and open the output of
1938 * the alternate spell command. The line that mark_begin points
1939 * to will be freed, so we save the line number and restore it
1940 * afterwards. */
1941 size_t totsize_save = openfile->totsize;
1942 /* Our saved value of totsize, used when we spell-check a marked
1943 * selection. */
1944
1945 if (old_mark_set) {
1946 /* If the mark is on, save the number of the line it starts on,
1947 * and then turn the mark off. */
1948 mb_lineno_save = openfile->mark_begin->lineno;
1949 openfile->mark_set = FALSE;
1950 }
1951#endif
1952
1953 endwin();
1954
1955 /* Set up an argument list to pass execvp(). */
1956 if (spellargs == NULL) {
1957 spellargs = (char **)nmalloc(arglen * sizeof(char *));
1958
1959 spellargs[0] = strtok(alt_speller, " ");
1960 while ((ptr = strtok(NULL, " ")) != NULL) {
1961 arglen++;
1962 spellargs = (char **)nrealloc(spellargs, arglen *
1963 sizeof(char *));
1964 spellargs[arglen - 3] = ptr;
1965 }
1966 spellargs[arglen - 1] = NULL;
1967 }
1968 spellargs[arglen - 2] = tempfile_name;
1969
1970 /* Start a new process for the alternate speller. */
1971 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey2e2112c2005-11-29 05:39:31 +00001972 /* Start alternate spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001973 execvp(spellargs[0], spellargs);
1974
1975 /* Should not be reached, if alternate speller is found!!! */
1976 exit(1);
1977 }
1978
1979 /* If we couldn't fork, get out. */
1980 if (pid_spell < 0)
1981 return _("Could not fork");
1982
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001983#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001984 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00001985 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00001986 allow_pending_sigwinch(FALSE);
1987#endif
1988
1989 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001990 wait(&alt_spell_status);
1991
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00001992 /* Reenter curses mode. */
1993 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001994
1995 /* Restore the terminal to its previous state. */
1996 terminal_init();
1997
1998 /* Turn the cursor back on for sure. */
1999 curs_set(1);
2000
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002001 /* The screen might have been resized. If it has, reinitialize all
2002 * the windows based on the new screen dimensions. */
2003 window_init();
2004
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002005 if (!WIFEXITED(alt_spell_status) ||
2006 WEXITSTATUS(alt_spell_status) != 0) {
2007 char *altspell_error;
2008 char *invoke_error = _("Error invoking \"%s\"");
2009
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002010#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002011 /* Turn the mark back on if it was on before. */
2012 openfile->mark_set = old_mark_set;
2013#endif
2014
2015 altspell_error =
2016 charalloc(strlen(invoke_error) +
2017 strlen(alt_speller) + 1);
2018 sprintf(altspell_error, invoke_error, alt_speller);
2019 return altspell_error;
2020 }
2021
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002022#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002023 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002024 /* If the mark is on, partition the filestruct so that it
2025 * contains only the marked text; if the NO_NEWLINES flag isn't
2026 * set, keep track of whether the text will have a magicline
2027 * added when we're done correcting misspelled words; and
2028 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002029 mark_order((const filestruct **)&top, &top_x,
2030 (const filestruct **)&bot, &bot_x, &right_side_up);
2031 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002032 if (!ISSET(NO_NEWLINES))
2033 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002034
2035 /* Get the number of characters in the marked text, and subtract
2036 * it from the saved value of totsize. */
2037 totsize_save -= get_totsize(top, bot);
2038 }
2039#endif
2040
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002041 /* Replace the text of the current buffer with the spell-checked
2042 * text. */
2043 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002044
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002045#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002046 if (old_mark_set) {
2047 filestruct *top_save = openfile->fileage;
2048
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002049 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2050 * added a magicline, remove it now. */
2051 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002052 remove_magicline();
2053
2054 /* Put the beginning and the end of the mark at the beginning
2055 * and the end of the spell-checked text. */
2056 if (openfile->fileage == openfile->filebot)
2057 bot_x += top_x;
2058 if (right_side_up) {
2059 openfile->mark_begin_x = top_x;
2060 current_x_save = bot_x;
2061 } else {
2062 current_x_save = top_x;
2063 openfile->mark_begin_x = bot_x;
2064 }
2065
2066 /* Unpartition the filestruct so that it contains all the text
2067 * again. Note that we've replaced the marked text originally
2068 * in the partition with the spell-checked marked text in the
2069 * temp file. */
2070 unpartition_filestruct(&filepart);
2071
2072 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002073 * partition. Also add the number of characters in the
2074 * spell-checked marked text to the saved value of totsize, and
2075 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002076 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002077 totsize_save += openfile->totsize;
2078 openfile->totsize = totsize_save;
2079
2080 /* Assign mark_begin to the line where the mark began before. */
2081 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2082 current_y_save, 0);
2083 openfile->mark_begin = openfile->current;
2084
2085 /* Assign mark_begin_x to the location in mark_begin where the
2086 * mark began before, adjusted for any shortening of the
2087 * line. */
2088 openfile->mark_begin_x = openfile->current_x;
2089
2090 /* Turn the mark back on. */
2091 openfile->mark_set = TRUE;
2092 }
2093#endif
2094
2095 /* Go back to the old position, and mark the file as modified. */
2096 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2097 set_modified();
2098
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002099#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002100 /* Handle a pending SIGWINCH again. */
2101 allow_pending_sigwinch(TRUE);
2102#endif
2103
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002104 return NULL;
2105}
2106
2107void do_spell(void)
2108{
2109 int i;
2110 FILE *temp_file;
2111 char *temp = safe_tempfile(&temp_file);
2112 const char *spell_msg;
2113
2114 if (temp == NULL) {
2115 statusbar(_("Could not create temp file: %s"), strerror(errno));
2116 return;
2117 }
2118
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002119#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002120 if (openfile->mark_set)
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002121 i = write_marked_file(temp, temp_file, TRUE, OVERWRITE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002122 else
2123#endif
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002124 i = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002125
2126 if (i == -1) {
2127 statusbar(_("Error writing temp file: %s"), strerror(errno));
2128 free(temp);
2129 return;
2130 }
2131
2132 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2133 do_int_speller(temp);
2134 unlink(temp);
2135 free(temp);
2136
2137 /* If the spell-checker printed any error messages onscreen, make
2138 * sure that they're cleared off. */
2139 total_refresh();
2140
2141 if (spell_msg != NULL) {
2142 if (errno == 0)
2143 /* Don't display an error message of "Success". */
2144 statusbar(_("Spell checking failed: %s"), spell_msg);
2145 else
2146 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2147 strerror(errno));
2148 } else
2149 statusbar(_("Finished checking spelling"));
2150}
2151#endif /* !DISABLE_SPELLER */
2152
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002153#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002154/* Our own version of "wc". Note that its character counts are in
2155 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002156void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002157{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002158 size_t words = 0, chars = 0;
2159 ssize_t lines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002160 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002161 size_t pww_save = openfile->placewewant;
2162 filestruct *current_save = openfile->current;
2163 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002164 filestruct *top, *bot;
2165 size_t top_x, bot_x;
2166
2167 if (old_mark_set) {
2168 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002169 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002170 mark_order((const filestruct **)&top, &top_x,
2171 (const filestruct **)&bot, &bot_x, NULL);
2172 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002173 openfile->mark_set = FALSE;
2174 }
2175
2176 /* Start at the top of the file. */
2177 openfile->current = openfile->fileage;
2178 openfile->current_x = 0;
2179 openfile->placewewant = 0;
2180
2181 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002182 * part of a word, as "wc -w" does), without updating the screen,
2183 * until we reach the end of the file, incrementing the total word
2184 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002185 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002186 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002187 if (do_next_word(TRUE, FALSE))
2188 words++;
2189 }
2190
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002191 /* Get the total line and character counts, as "wc -l" and "wc -c"
2192 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002193 if (old_mark_set) {
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002194 lines = openfile->filebot->lineno -
2195 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002196 chars = get_totsize(openfile->fileage, openfile->filebot);
2197
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002198 /* Unpartition the filestruct so that it contains all the text
2199 * again, and turn the mark back on. */
2200 unpartition_filestruct(&filepart);
2201 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002202 } else {
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002203 lines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002204 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002205 }
2206
2207 /* Restore where we were. */
2208 openfile->current = current_save;
2209 openfile->current_x = current_x_save;
2210 openfile->placewewant = pww_save;
2211
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002212 /* Display the total word, line, and character counts on the
2213 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002214 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
David Lawrence Ramsey815fb0a2005-08-05 19:38:11 +00002215 _("In Selection: ") : "", (unsigned long)words, (long)lines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002216 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002217}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002218#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002219
2220void do_verbatim_input(void)
2221{
2222 int *kbinput;
2223 size_t kbinput_len, i;
2224 char *output;
2225
2226 statusbar(_("Verbatim Input"));
2227
2228 /* If constant cursor position display is on, make sure the current
2229 * cursor position will be properly displayed on the statusbar. */
2230 if (ISSET(CONST_UPDATE))
2231 do_cursorpos(TRUE);
2232
2233 /* Read in all the verbatim characters. */
2234 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2235
2236 /* Display all the verbatim characters at once, not filtering out
2237 * control characters. */
2238 output = charalloc(kbinput_len + 1);
2239
2240 for (i = 0; i < kbinput_len; i++)
2241 output[i] = (char)kbinput[i];
2242 output[i] = '\0';
2243
2244 do_output(output, kbinput_len, TRUE);
2245
2246 free(output);
2247}