blob: 463fd6de4b28581f2bb1f6e199ef61503931588f [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 *
David Lawrence Ramsey33f2a082006-02-07 21:11:49 +00006 * Copyright (C) 2005-2006 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
David Lawrence Ramsey034b9942005-12-08 02:47:10 +000024#include "proto.h"
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000025
David Lawrence Ramseyee11c6a2005-11-02 19:42:02 +000026#include <stdio.h>
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000027#include <signal.h>
28#include <unistd.h>
29#include <string.h>
30#include <fcntl.h>
31#include <sys/wait.h>
32#include <errno.h>
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000033
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000034#ifndef NANO_TINY
David Lawrence Ramsey8779a172005-11-08 16:45:22 +000035static pid_t pid = -1;
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +000036 /* The PID of the forked process in execute_command(), for use
37 * with the cancel_command() signal handler. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000038#endif
39#ifndef DISABLE_WRAPPING
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +000040static bool prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000041 /* Should we prepend wrapped text to the next line? */
42#endif
43#ifndef DISABLE_JUSTIFY
44static filestruct *jusbottom = NULL;
David Lawrence Ramseyae4c3a62005-09-20 19:46:39 +000045 /* Pointer to the end of the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000046#endif
47
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000048#ifndef NANO_TINY
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +000049/* Toggle the mark. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000050void do_mark(void)
51{
52 openfile->mark_set = !openfile->mark_set;
53 if (openfile->mark_set) {
54 statusbar(_("Mark Set"));
55 openfile->mark_begin = openfile->current;
56 openfile->mark_begin_x = openfile->current_x;
57 } else {
58 statusbar(_("Mark UNset"));
59 openfile->mark_begin = NULL;
60 openfile->mark_begin_x = 0;
61 edit_refresh();
62 }
63}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000064#endif /* !NANO_TINY */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000065
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +000066/* Delete one character. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000067void do_delete(void)
68{
69 bool do_refresh = FALSE;
70 /* Do we have to call edit_refresh(), or can we get away with
David Lawrence Ramsey75e9dfe2006-05-18 17:50:08 +000071 * just update_line()? */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000072
73 assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data));
74
75 openfile->placewewant = xplustabs();
76
77 if (openfile->current->data[openfile->current_x] != '\0') {
78 int char_buf_len = parse_mbchar(openfile->current->data +
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +000079 openfile->current_x, NULL, NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000080 size_t line_len = strlen(openfile->current->data +
81 openfile->current_x);
82
83 assert(openfile->current_x < strlen(openfile->current->data));
84
85 /* Let's get dangerous. */
86 charmove(&openfile->current->data[openfile->current_x],
87 &openfile->current->data[openfile->current_x +
88 char_buf_len], line_len - char_buf_len + 1);
89
90 null_at(&openfile->current->data, openfile->current_x +
91 line_len - char_buf_len);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000092#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000093 if (openfile->mark_set && openfile->mark_begin ==
94 openfile->current && openfile->current_x <
95 openfile->mark_begin_x)
96 openfile->mark_begin_x -= char_buf_len;
97#endif
98 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +000099 } else if (openfile->current != openfile->filebot) {
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000100 filestruct *foo = openfile->current->next;
101
102 assert(openfile->current_x == strlen(openfile->current->data));
103
104 /* If we're deleting at the end of a line, we need to call
105 * edit_refresh(). */
106 if (openfile->current->data[openfile->current_x] == '\0')
107 do_refresh = TRUE;
108
109 openfile->current->data = charealloc(openfile->current->data,
110 openfile->current_x + strlen(foo->data) + 1);
111 strcpy(openfile->current->data + openfile->current_x,
112 foo->data);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000113#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000114 if (openfile->mark_set && openfile->mark_begin ==
115 openfile->current->next) {
116 openfile->mark_begin = openfile->current;
117 openfile->mark_begin_x += openfile->current_x;
118 }
119#endif
120 if (openfile->filebot == foo)
121 openfile->filebot = openfile->current;
122
123 unlink_node(foo);
124 delete_node(foo);
125 renumber(openfile->current);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000126 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000127
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000128 /* If the NO_NEWLINES flag isn't set, and text has been added to
129 * the magicline as a result of deleting at the end of the line
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000130 * before filebot, add a new magicline. */
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000131 if (!ISSET(NO_NEWLINES) && openfile->current ==
132 openfile->filebot && openfile->current->data[0] != '\0')
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000133 new_magicline();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000134 } else
135 return;
136
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000137 set_modified();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000138
139#ifdef ENABLE_COLOR
140 /* If color syntaxes are available and turned on, we need to call
141 * edit_refresh(). */
142 if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX))
143 do_refresh = TRUE;
144#endif
145
146 if (do_refresh)
147 edit_refresh();
148 else
149 update_line(openfile->current, openfile->current_x);
150}
151
David Lawrence Ramsey5b7b3e32005-12-31 21:22:54 +0000152/* Backspace over one character. That is, move the cursor left one
153 * character, and then delete the character there. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000154void do_backspace(void)
155{
156 if (openfile->current != openfile->fileage ||
157 openfile->current_x > 0) {
David Lawrence Ramsey1c3bfa92005-09-13 04:53:44 +0000158 do_left();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000159 do_delete();
160 }
161}
162
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000163/* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number
164 * of spaces that a tab would normally take up. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000165void 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 Ramseyf85001a2006-04-28 13:19:56 +0000193#ifndef NANO_TINY
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000194/* Indent or unindent the current line (or all lines covered by the mark
195 * if the mark is on) len columns, depending on whether len is positive
196 * or negative. If the TABS_TO_SPACES flag is set, indent/unindent by
197 * len spaces. Otherwise, indent/unindent by (len / tabsize) tabs and
198 * (len % tabsize) spaces. */
199void do_indent(ssize_t cols)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000200{
201 bool indent_changed = FALSE;
202 /* Whether any indenting or unindenting was done. */
203 bool unindent = FALSE;
204 /* Whether we're unindenting text. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000205 char *line_indent = NULL;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000206 /* The text added to each line in order to indent it. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000207 size_t line_indent_len = 0;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000208 /* The length of the text added to each line in order to indent
209 * it. */
210 filestruct *top, *bot, *f;
211 size_t top_x, bot_x;
212
213 assert(openfile->current != NULL && openfile->current->data != NULL);
214
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000215 /* If cols is zero, get out. */
216 if (cols == 0)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000217 return;
218
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000219 /* If cols is negative, make it positive and set unindent to
220 * TRUE. */
221 if (cols < 0) {
222 cols = -cols;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000223 unindent = TRUE;
224 /* Otherwise, we're indenting, in which case the file will always be
225 * modified, so set indent_changed to TRUE. */
226 } else
227 indent_changed = TRUE;
228
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000229 /* If the mark is on, use all lines covered by the mark. */
230 if (openfile->mark_set)
231 mark_order((const filestruct **)&top, &top_x,
232 (const filestruct **)&bot, &bot_x, NULL);
233 /* Otherwise, use the current line. */
234 else {
235 top = openfile->current;
236 bot = top;
237 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000238
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000239 if (!unindent) {
240 /* Set up the text we'll be using as indentation. */
241 line_indent = charalloc(cols + 1);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000242
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000243 if (ISSET(TABS_TO_SPACES)) {
244 /* Set the indentation to cols spaces. */
245 charset(line_indent, ' ', cols);
246 line_indent_len = cols;
247 } else {
248 /* Set the indentation to (cols / tabsize) tabs and (cols %
249 * tabsize) spaces. */
250 size_t num_tabs = cols / tabsize;
251 size_t num_spaces = cols % tabsize;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000252
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000253 charset(line_indent, '\t', num_tabs);
254 charset(line_indent + num_tabs, ' ', num_spaces);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000255
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000256 line_indent_len = num_tabs + num_spaces;
257 }
258
259 line_indent[line_indent_len] = '\0';
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000260 }
261
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000262 /* Go through each line of the text. */
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000263 for (f = top; f != bot->next; f = f->next) {
264 size_t line_len = strlen(f->data);
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000265 size_t indent_len = indent_length(f->data);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000266
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000267 if (!unindent) {
268 /* If we're indenting, add the characters in line_indent to
269 * the beginning of the non-whitespace text of this line. */
270 f->data = charealloc(f->data, line_len +
271 line_indent_len + 1);
272 charmove(&f->data[indent_len + line_indent_len],
273 &f->data[indent_len], line_len - indent_len + 1);
274 strncpy(f->data + indent_len, line_indent, line_indent_len);
275 openfile->totsize += line_indent_len;
276
277 /* Keep track of the change in the current line. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000278 if (openfile->mark_set && f == openfile->mark_begin &&
279 openfile->mark_begin_x >= indent_len)
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000280 openfile->mark_begin_x += line_indent_len;
281
282 if (f == openfile->current && openfile->current_x >=
283 indent_len)
284 openfile->current_x += line_indent_len;
285
286 /* If the NO_NEWLINES flag isn't set, and this is the
287 * magicline, add a new magicline. */
288 if (!ISSET(NO_NEWLINES) && f == openfile->filebot)
289 new_magicline();
290 } else {
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000291 size_t indent_col = strnlenpt(f->data, indent_len);
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000292 /* The length in columns of the indentation on this
293 * line. */
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000294
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000295 if (cols <= indent_col) {
296 size_t indent_new = actual_x(f->data, indent_col -
297 cols);
David Lawrence Ramsey5bb77272006-05-06 14:37:33 +0000298 /* The length of the indentation remaining on
299 * this line after we unindent. */
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000300 size_t indent_shift = indent_len - indent_new;
David Lawrence Ramsey5bb77272006-05-06 14:37:33 +0000301 /* The change in the indentation on this line
302 * after we unindent. */
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000303
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000304 /* If we're unindenting, and there's at least cols
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000305 * columns' worth of indentation at the beginning of the
306 * non-whitespace text of this line, remove it. */
307 charmove(&f->data[indent_new], &f->data[indent_len],
308 line_len - indent_shift - indent_new + 1);
309 null_at(&f->data, line_len - indent_shift + 1);
310 openfile->totsize -= indent_shift;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000311
David Lawrence Ramsey2e8fac62006-04-29 15:44:58 +0000312 /* Keep track of the change in the current line. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000313 if (openfile->mark_set && f == openfile->mark_begin &&
David Lawrence Ramseyeb4f90e2006-05-05 14:22:42 +0000314 openfile->mark_begin_x > indent_new) {
315 if (openfile->mark_begin_x <= indent_len)
316 openfile->mark_begin_x = indent_new;
317 else
318 openfile->mark_begin_x -= indent_shift;
319 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000320
David Lawrence Ramseyac37b042006-05-03 12:59:05 +0000321 if (f == openfile->current && openfile->current_x >
David Lawrence Ramseyeb4f90e2006-05-05 14:22:42 +0000322 indent_new) {
323 if (openfile->current_x <= indent_len)
324 openfile->current_x = indent_new;
325 else
326 openfile->current_x -= indent_shift;
327 }
David Lawrence Ramsey7194a612006-04-29 16:11:21 +0000328
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000329 /* We've unindented, so set indent_changed to TRUE. */
330 if (!indent_changed)
331 indent_changed = TRUE;
332 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000333 }
334 }
335
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000336 if (!unindent)
David Lawrence Ramsey25456862006-05-05 15:43:52 +0000337 /* Clean up. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000338 free(line_indent);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000339
340 if (indent_changed) {
341 /* Mark the file as modified. */
342 set_modified();
343
344 /* Update the screen. */
345 edit_refresh();
346 }
347}
348
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000349/* Indent the current line, or all lines covered by the mark if the mark
350 * is on, tabsize columns. */
351void do_indent_void(void)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000352{
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000353 do_indent(tabsize);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000354}
355
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000356/* Unindent the current line, or all lines covered by the mark if the
357 * mark is on, tabsize columns. */
358void do_unindent(void)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000359{
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000360 do_indent(-tabsize);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000361}
362#endif /* !NANO_TINY */
363
David Lawrence Ramseyb0e04c02005-12-08 07:24:54 +0000364/* Someone hits Enter *gasp!* */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000365void do_enter(void)
366{
367 filestruct *newnode = make_new_node(openfile->current);
368 size_t extra = 0;
369
370 assert(openfile->current != NULL && openfile->current->data != NULL);
371
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000372#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000373 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
374 if (ISSET(AUTOINDENT)) {
375 /* If we are breaking the line in the indentation, the new
376 * indentation should have only current_x characters, and
377 * current_x should not change. */
378 extra = indent_length(openfile->current->data);
379 if (extra > openfile->current_x)
380 extra = openfile->current_x;
381 }
382#endif
383 newnode->data = charalloc(strlen(openfile->current->data +
384 openfile->current_x) + extra + 1);
385 strcpy(&newnode->data[extra], openfile->current->data +
386 openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000387#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000388 if (ISSET(AUTOINDENT)) {
389 strncpy(newnode->data, openfile->current->data, extra);
390 openfile->totsize += mbstrlen(newnode->data);
391 }
392#endif
393 null_at(&openfile->current->data, openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000394#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000395 if (openfile->mark_set && openfile->current ==
396 openfile->mark_begin && openfile->current_x <
397 openfile->mark_begin_x) {
398 openfile->mark_begin = newnode;
399 openfile->mark_begin_x += extra - openfile->current_x;
400 }
401#endif
402 openfile->current_x = extra;
403
404 if (openfile->current == openfile->filebot)
405 openfile->filebot = newnode;
406 splice_node(openfile->current, newnode,
407 openfile->current->next);
408
409 renumber(openfile->current);
410 openfile->current = newnode;
411
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000412 openfile->totsize++;
413 set_modified();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000414
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000415 openfile->placewewant = xplustabs();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000416
417 edit_refresh();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000418}
419
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000420#ifndef NANO_TINY
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000421/* Send a SIGKILL (unconditional kill) to the forked process in
422 * execute_command(). */
David Lawrence Ramsey8befda62005-12-06 19:39:56 +0000423RETSIGTYPE cancel_command(int signal)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000424{
425 if (kill(pid, SIGKILL) == -1)
426 nperror("kill");
427}
428
David Lawrence Ramsey0ed71712005-11-08 23:09:47 +0000429/* Execute command in a shell. Return TRUE on success. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000430bool execute_command(const char *command)
431{
432 int fd[2];
433 FILE *f;
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000434 char *shellenv;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000435 struct sigaction oldaction, newaction;
436 /* Original and temporary handlers for SIGINT. */
437 bool sig_failed = FALSE;
438 /* Did sigaction() fail without changing the signal handlers? */
439
440 /* Make our pipes. */
441 if (pipe(fd) == -1) {
442 statusbar(_("Could not pipe"));
443 return FALSE;
444 }
445
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000446 /* Check $SHELL for the shell to use. If it isn't set, use
David Lawrence Ramsey1932dfb2005-11-29 05:52:49 +0000447 * /bin/sh. Note that $SHELL should contain only a path, with no
448 * arguments. */
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000449 shellenv = getenv("SHELL");
450 if (shellenv == NULL)
451 shellenv = "/bin/sh";
452
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000453 /* Fork a child. */
454 if ((pid = fork()) == 0) {
455 close(fd[0]);
456 dup2(fd[1], fileno(stdout));
457 dup2(fd[1], fileno(stderr));
458
459 /* If execl() returns at all, there was an error. */
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000460 execl(shellenv, tail(shellenv), "-c", command, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000461 exit(0);
462 }
463
David Lawrence Ramseyc838a4c2006-04-26 18:33:50 +0000464 /* Continue as parent. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000465 close(fd[1]);
466
467 if (pid == -1) {
468 close(fd[0]);
469 statusbar(_("Could not fork"));
470 return FALSE;
471 }
472
473 /* Before we start reading the forked command's output, we set
474 * things up so that Ctrl-C will cancel the new process. */
475
476 /* Enable interpretation of the special control keys so that we get
477 * SIGINT when Ctrl-C is pressed. */
478 enable_signals();
479
480 if (sigaction(SIGINT, NULL, &newaction) == -1) {
481 sig_failed = TRUE;
482 nperror("sigaction");
483 } else {
484 newaction.sa_handler = cancel_command;
485 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
486 sig_failed = TRUE;
487 nperror("sigaction");
488 }
489 }
490
491 /* Note that now oldaction is the previous SIGINT signal handler,
492 * to be restored later. */
493
494 f = fdopen(fd[0], "rb");
495 if (f == NULL)
496 nperror("fdopen");
497
498 read_file(f, "stdin");
499
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000500 if (wait(NULL) == -1)
501 nperror("wait");
502
503 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
504 nperror("sigaction");
505
506 /* Disable interpretation of the special control keys so that we can
507 * use Ctrl-C for other things. */
508 disable_signals();
509
510 return TRUE;
511}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000512#endif /* !NANO_TINY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000513
514#ifndef DISABLE_WRAPPING
David Lawrence Ramseyef0d5a72006-05-22 02:08:49 +0000515/* Unset the prepend_wrap flag. We need to do this as soon as we do
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000516 * something other than type text. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000517void wrap_reset(void)
518{
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000519 prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000520}
521
522/* We wrap the given line. Precondition: we assume the cursor has been
David Lawrence Ramsey139fa652006-05-22 01:26:24 +0000523 * moved forward since the last typed character. Return TRUE if we
524 * wrapped, and FALSE otherwise. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000525bool do_wrap(filestruct *line)
526{
527 size_t line_len;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000528 /* The length of the line we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000529 ssize_t wrap_loc;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000530 /* The index of line->data where we wrap. */
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000531#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000532 const char *indent_string = NULL;
533 /* Indentation to prepend to the new line. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000534 size_t indent_len = 0;
535 /* The length of indent_string. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000536#endif
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000537 const char *after_break;
538 /* The text after the wrap point. */
539 size_t after_break_len;
540 /* The length of after_break. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000541 bool prepending = FALSE;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000542 /* Do we prepend to the next line? */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000543 const char *next_line = NULL;
544 /* The next line, minus indentation. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000545 size_t next_line_len = 0;
546 /* The length of next_line. */
547 char *new_line = NULL;
548 /* The line we create. */
549 size_t new_line_len = 0;
550 /* The eventual length of new_line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000551
552 /* There are three steps. First, we decide where to wrap. Then, we
553 * create the new wrap line. Finally, we clean up. */
554
555 /* Step 1, finding where to wrap. We are going to add a new line
556 * after a blank character. In this step, we call break_line() to
557 * get the location of the last blank we can break the line at, and
David Lawrence Ramseyd4686b82006-06-11 19:14:14 +0000558 * set wrap_loc to the location of the character after it, so that
559 * the blank is preserved at the end of the line.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000560 *
561 * If there is no legal wrap point, or we reach the last character
562 * of the line while trying to find one, we should return without
563 * wrapping. Note that if autoindent is turned on, we don't break
564 * at the end of it! */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000565 assert(line != NULL && line->data != NULL);
566
567 /* Save the length of the line. */
568 line_len = strlen(line->data);
569
570 /* Find the last blank where we can break the line. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000571 wrap_loc = break_line(line->data, fill
572#ifndef DISABLE_HELP
573 , FALSE
574#endif
575 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000576
577 /* If we couldn't break the line, or we've reached the end of it, we
578 * don't wrap. */
579 if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
580 return FALSE;
581
582 /* Otherwise, move forward to the character just after the blank. */
583 wrap_loc += move_mbright(line->data + wrap_loc, 0);
584
585 /* If we've reached the end of the line, we don't wrap. */
586 if (line->data[wrap_loc] == '\0')
587 return FALSE;
588
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000589#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000590 /* If autoindent is turned on, and we're on the character just after
591 * the indentation, we don't wrap. */
592 if (ISSET(AUTOINDENT)) {
593 /* Get the indentation of this line. */
594 indent_string = line->data;
595 indent_len = indent_length(indent_string);
596
597 if (wrap_loc == indent_len)
598 return FALSE;
599 }
600#endif
601
602 /* Step 2, making the new wrap line. It will consist of indentation
603 * followed by the text after the wrap point, optionally followed by
604 * a space (if the text after the wrap point doesn't end in a blank)
David Lawrence Ramsey03979d72006-06-11 19:15:59 +0000605 * and the text of the next line, if they can fit without wrapping,
606 * the next line exists, and the prepend_wrap flag is set. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000607
608 /* after_break is the text that will be wrapped to the next line. */
609 after_break = line->data + wrap_loc;
610 after_break_len = line_len - wrap_loc;
611
612 assert(strlen(after_break) == after_break_len);
613
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000614 /* We prepend the wrapped text to the next line, if the prepend_wrap
615 * flag is set, there is a next line, and prepending would not make
616 * the line too long. */
617 if (prepend_wrap && line != openfile->filebot) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000618 const char *end = after_break + move_mbleft(after_break,
619 after_break_len);
620
621 /* If after_break doesn't end in a blank, make sure it ends in a
622 * space. */
623 if (!is_blank_mbchar(end)) {
624 line_len++;
625 line->data = charealloc(line->data, line_len + 1);
626 line->data[line_len - 1] = ' ';
627 line->data[line_len] = '\0';
628 after_break = line->data + wrap_loc;
629 after_break_len++;
630 openfile->totsize++;
631 }
632
633 next_line = line->next->data;
634 next_line_len = strlen(next_line);
635
636 if (after_break_len + next_line_len <= fill) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000637 prepending = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000638 new_line_len += next_line_len;
639 }
640 }
641
642 /* new_line_len is now the length of the text that will be wrapped
643 * to the next line, plus (if we're prepending to it) the length of
644 * the text of the next line. */
645 new_line_len += after_break_len;
646
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000647#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000648 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000649 if (prepending) {
650 /* If we're prepending, the indentation will come from the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000651 * next line. */
652 indent_string = next_line;
653 indent_len = indent_length(indent_string);
654 next_line += indent_len;
655 } else {
656 /* Otherwise, it will come from this line, in which case
657 * we should increase new_line_len to make room for it. */
658 new_line_len += indent_len;
659 openfile->totsize += mbstrnlen(indent_string, indent_len);
660 }
661 }
662#endif
663
664 /* Now we allocate the new line and copy the text into it. */
665 new_line = charalloc(new_line_len + 1);
666 new_line[0] = '\0';
667
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000668#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000669 if (ISSET(AUTOINDENT)) {
670 /* Copy the indentation. */
671 strncpy(new_line, indent_string, indent_len);
672 new_line[indent_len] = '\0';
673 new_line_len += indent_len;
674 }
675#endif
676
677 /* Copy all the text after the wrap point of the current line. */
678 strcat(new_line, after_break);
679
680 /* Break the current line at the wrap point. */
681 null_at(&line->data, wrap_loc);
682
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000683 if (prepending) {
684 /* If we're prepending, copy the text from the next line, minus
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000685 * the indentation that we already copied above. */
686 strcat(new_line, next_line);
687
688 free(line->next->data);
689 line->next->data = new_line;
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000690
691 /* If the NO_NEWLINES flag isn't set, and text has been added to
692 * the magicline, make a new magicline. */
693 if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
694 new_magicline();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000695 } else {
696 /* Otherwise, make a new line and copy the text after where we
697 * broke this line to the beginning of the new line. */
698 splice_node(openfile->current, make_new_node(openfile->current),
699 openfile->current->next);
700
David Lawrence Ramsey219a8142005-11-22 22:08:01 +0000701 /* If the current line is the last line of the file, move the
702 * last line of the file down to the next line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000703 if (openfile->filebot == openfile->current)
704 openfile->filebot = openfile->current->next;
705
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000706 openfile->current->next->data = new_line;
707
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000708 openfile->totsize++;
709 }
710
711 /* Step 3, clean up. Reposition the cursor and mark, and do some
712 * other sundry things. */
713
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000714 /* Set the prepend_wrap flag, so that later wraps of this line will
715 * be prepended to the next line. */
716 prepend_wrap = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000717
David Lawrence Ramsey2829aae2006-05-22 01:24:09 +0000718 /* Each line knows its number. We recalculate these if we inserted
719 * a new line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000720 if (!prepending)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000721 renumber(line);
722
723 /* If the cursor was after the break point, we must move it. We
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000724 * also clear the prepend_wrap flag in this case. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000725 if (openfile->current_x > wrap_loc) {
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000726 prepend_wrap = FALSE;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000727
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000728 openfile->current = openfile->current->next;
729 openfile->current_x -= wrap_loc
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000730#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000731 - indent_len
732#endif
733 ;
734 openfile->placewewant = xplustabs();
735 }
736
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000737#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000738 /* If the mark was on this line after the wrap point, we move it
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000739 * down. If it was on the next line and we prepended to that line,
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000740 * we move it right. */
741 if (openfile->mark_set) {
742 if (openfile->mark_begin == line && openfile->mark_begin_x >
743 wrap_loc) {
744 openfile->mark_begin = line->next;
745 openfile->mark_begin_x -= wrap_loc - indent_len + 1;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000746 } else if (prepending && openfile->mark_begin == line->next)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000747 openfile->mark_begin_x += after_break_len;
748 }
749#endif
750
751 return TRUE;
752}
753#endif /* !DISABLE_WRAPPING */
754
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +0000755#if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000756/* We are trying to break a chunk off line. We find the last blank such
David Lawrence Ramseycd9a5f02005-09-20 06:12:54 +0000757 * that the display length to there is at most (goal + 1). If there is
758 * no such blank, then we find the first blank. We then take the last
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000759 * blank in that group of blanks. The terminating '\0' counts as a
760 * blank, as does a '\n' if newline is TRUE. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000761ssize_t break_line(const char *line, ssize_t goal
762#ifndef DISABLE_HELP
763 , bool newline
764#endif
765 )
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000766{
767 ssize_t blank_loc = -1;
768 /* Current tentative return value. Index of the last blank we
769 * found with short enough display width. */
770 ssize_t cur_loc = 0;
771 /* Current index in line. */
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +0000772 size_t cur_pos = 0;
773 /* Current column position in line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000774 int line_len;
775
776 assert(line != NULL);
777
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +0000778 while (*line != '\0' && goal >= cur_pos) {
779 line_len = parse_mbchar(line, NULL, &cur_pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000780
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000781 if (is_blank_mbchar(line)
782#ifndef DISABLE_HELP
783 || (newline && *line == '\n')
784#endif
785 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000786 blank_loc = cur_loc;
787
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000788#ifndef DISABLE_HELP
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000789 if (newline && *line == '\n')
790 break;
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000791#endif
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000792 }
793
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000794 line += line_len;
795 cur_loc += line_len;
796 }
797
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +0000798 if (goal >= cur_pos)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000799 /* In fact, the whole line displays shorter than goal. */
800 return cur_loc;
801
802 if (blank_loc == -1) {
803 /* No blank was found that was short enough. */
804 bool found_blank = FALSE;
David Lawrence Ramsey5ab12ca2005-09-20 17:52:52 +0000805 ssize_t found_blank_loc = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000806
807 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000808 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000809
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000810 if (is_blank_mbchar(line)
811#ifndef DISABLE_HELP
812 || (newline && *line == '\n')
813#endif
814 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000815 if (!found_blank)
816 found_blank = TRUE;
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +0000817 found_blank_loc = cur_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000818 } else if (found_blank)
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +0000819 return found_blank_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000820
821 line += line_len;
822 cur_loc += line_len;
823 }
824
825 return -1;
826 }
827
828 /* Move to the last blank after blank_loc, if there is one. */
829 line -= cur_loc;
830 line += blank_loc;
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000831 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000832 line += line_len;
833
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000834 while (*line != '\0' && (is_blank_mbchar(line)
835#ifndef DISABLE_HELP
836 || (newline && *line == '\n')
837#endif
838 )) {
David Lawrence Ramseycd243f52006-05-19 23:27:16 +0000839#ifndef DISABLE_HELP
840 if (newline && *line == '\n')
841 break;
842#endif
843
David Lawrence Ramsey39bd1b32006-05-20 13:11:56 +0000844 line_len = parse_mbchar(line, NULL, NULL);
845
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000846 line += line_len;
847 blank_loc += line_len;
848 }
849
850 return blank_loc;
851}
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +0000852#endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000853
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000854#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000855/* The "indentation" of a line is the whitespace between the quote part
856 * and the non-whitespace of the line. */
857size_t indent_length(const char *line)
858{
859 size_t len = 0;
860 char *blank_mb;
861 int blank_mb_len;
862
863 assert(line != NULL);
864
865 blank_mb = charalloc(mb_cur_max());
866
867 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000868 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000869
870 if (!is_blank_mbchar(blank_mb))
871 break;
872
873 line += blank_mb_len;
874 len += blank_mb_len;
875 }
876
877 free(blank_mb);
878
879 return len;
880}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000881#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000882
883#ifndef DISABLE_JUSTIFY
884/* justify_format() replaces blanks with spaces and multiple spaces by 1
885 * (except it maintains up to 2 after a character in punct optionally
886 * followed by a character in brackets, and removes all from the end).
887 *
888 * justify_format() might make paragraph->data shorter, and change the
889 * actual pointer with null_at().
890 *
891 * justify_format() will not look at the first skip characters of
892 * paragraph. skip should be at most strlen(paragraph->data). The
893 * character at paragraph[skip + 1] must not be blank. */
894void justify_format(filestruct *paragraph, size_t skip)
895{
896 char *end, *new_end, *new_paragraph_data;
897 size_t shift = 0;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000898#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000899 size_t mark_shift = 0;
900#endif
901
902 /* These four asserts are assumptions about the input data. */
903 assert(paragraph != NULL);
904 assert(paragraph->data != NULL);
905 assert(skip < strlen(paragraph->data));
906 assert(!is_blank_mbchar(paragraph->data + skip));
907
908 end = paragraph->data + skip;
909 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
910 strncpy(new_paragraph_data, paragraph->data, skip);
911 new_end = new_paragraph_data + skip;
912
913 while (*end != '\0') {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000914 int end_len;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000915
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000916 /* If this character is blank, change it to a space if
917 * necessary, and skip over all blanks after it. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000918 if (is_blank_mbchar(end)) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000919 end_len = parse_mbchar(end, NULL, NULL);
920
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000921 *new_end = ' ';
922 new_end++;
923 end += end_len;
924
925 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000926 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000927
928 end += end_len;
929 shift += end_len;
930
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000931#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000932 /* Keep track of the change in the current line. */
933 if (openfile->mark_set && openfile->mark_begin ==
934 paragraph && openfile->mark_begin_x >= end -
935 paragraph->data)
936 mark_shift += end_len;
937#endif
938 }
939 /* If this character is punctuation optionally followed by a
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000940 * bracket and then followed by blanks, change no more than two
941 * of the blanks to spaces if necessary, and skip over all
942 * blanks after them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000943 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000944 end_len = parse_mbchar(end, NULL, NULL);
945
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000946 while (end_len > 0) {
947 *new_end = *end;
948 new_end++;
949 end++;
950 end_len--;
951 }
952
953 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000954 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000955
956 while (end_len > 0) {
957 *new_end = *end;
958 new_end++;
959 end++;
960 end_len--;
961 }
962 }
963
964 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000965 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000966
967 *new_end = ' ';
968 new_end++;
969 end += end_len;
970 }
971
972 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000973 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000974
975 *new_end = ' ';
976 new_end++;
977 end += end_len;
978 }
979
980 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000981 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000982
983 end += end_len;
984 shift += end_len;
985
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000986#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000987 /* Keep track of the change in the current line. */
988 if (openfile->mark_set && openfile->mark_begin ==
989 paragraph && openfile->mark_begin_x >= end -
990 paragraph->data)
991 mark_shift += end_len;
992#endif
993 }
994 /* If this character is neither blank nor punctuation, leave it
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000995 * unchanged. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000996 } else {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000997 end_len = parse_mbchar(end, NULL, NULL);
998
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000999 while (end_len > 0) {
1000 *new_end = *end;
1001 new_end++;
1002 end++;
1003 end_len--;
1004 }
1005 }
1006 }
1007
1008 assert(*end == '\0');
1009
1010 *new_end = *end;
1011
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001012 /* If there are spaces at the end of the line, remove them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001013 while (new_end > new_paragraph_data + skip &&
1014 *(new_end - 1) == ' ') {
1015 new_end--;
1016 shift++;
1017 }
1018
1019 if (shift > 0) {
1020 openfile->totsize -= shift;
1021 null_at(&new_paragraph_data, new_end - new_paragraph_data);
1022 free(paragraph->data);
1023 paragraph->data = new_paragraph_data;
1024
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001025#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001026 /* Adjust the mark coordinates to compensate for the change in
1027 * the current line. */
1028 if (openfile->mark_set && openfile->mark_begin == paragraph) {
1029 openfile->mark_begin_x -= mark_shift;
1030 if (openfile->mark_begin_x > new_end - new_paragraph_data)
1031 openfile->mark_begin_x = new_end - new_paragraph_data;
1032 }
1033#endif
1034 } else
1035 free(new_paragraph_data);
1036}
1037
1038/* The "quote part" of a line is the largest initial substring matching
1039 * the quote string. This function returns the length of the quote part
1040 * of the given line.
1041 *
1042 * Note that if !HAVE_REGEX_H then we match concatenated copies of
1043 * quotestr. */
1044size_t quote_length(const char *line)
1045{
1046#ifdef HAVE_REGEX_H
1047 regmatch_t matches;
1048 int rc = regexec(&quotereg, line, 1, &matches, 0);
1049
1050 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
1051 return 0;
1052 /* matches.rm_so should be 0, since the quote string should start
1053 * with the caret ^. */
1054 return matches.rm_eo;
1055#else /* !HAVE_REGEX_H */
1056 size_t qdepth = 0;
1057
1058 /* Compute quote depth level. */
1059 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
1060 qdepth += quotelen;
1061 return qdepth;
1062#endif /* !HAVE_REGEX_H */
1063}
1064
1065/* a_line and b_line are lines of text. The quotation part of a_line is
1066 * the first a_quote characters. Check that the quotation part of
1067 * b_line is the same. */
1068bool quotes_match(const char *a_line, size_t a_quote, const char
1069 *b_line)
1070{
1071 /* Here is the assumption about a_quote. */
1072 assert(a_quote == quote_length(a_line));
1073
1074 return (a_quote == quote_length(b_line) &&
1075 strncmp(a_line, b_line, a_quote) == 0);
1076}
1077
1078/* We assume a_line and b_line have no quote part. Then, we return
1079 * whether b_line could follow a_line in a paragraph. */
1080bool indents_match(const char *a_line, size_t a_indent, const char
1081 *b_line, size_t b_indent)
1082{
1083 assert(a_indent == indent_length(a_line));
1084 assert(b_indent == indent_length(b_line));
1085
1086 return (b_indent <= a_indent &&
1087 strncmp(a_line, b_line, b_indent) == 0);
1088}
1089
1090/* Is foo the beginning of a paragraph?
1091 *
1092 * A line of text consists of a "quote part", followed by an
1093 * "indentation part", followed by text. The functions quote_length()
1094 * and indent_length() calculate these parts.
1095 *
1096 * A line is "part of a paragraph" if it has a part not in the quote
1097 * part or the indentation.
1098 *
1099 * A line is "the beginning of a paragraph" if it is part of a
1100 * paragraph and
1101 * 1) it is the top line of the file, or
1102 * 2) the line above it is not part of a paragraph, or
1103 * 3) the line above it does not have precisely the same quote
1104 * part, or
1105 * 4) the indentation of this line is not an initial substring of
1106 * the indentation of the previous line, or
1107 * 5) this line has no quote part and some indentation, and
1108 * autoindent isn't turned on.
1109 * The reason for number 5) is that if autoindent isn't turned on,
1110 * then an indented line is expected to start a paragraph, as in
1111 * books. Thus, nano can justify an indented paragraph only if
1112 * autoindent is turned on. */
1113bool begpar(const filestruct *const foo)
1114{
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001115 size_t quote_len, indent_len, temp_id_len;
1116
1117 if (foo == NULL)
1118 return FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001119
1120 /* Case 1). */
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001121 if (foo == openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001122 return TRUE;
1123
1124 quote_len = quote_length(foo->data);
1125 indent_len = indent_length(foo->data + quote_len);
1126
1127 /* Not part of a paragraph. */
1128 if (foo->data[quote_len + indent_len] == '\0')
1129 return FALSE;
1130
1131 /* Case 3). */
1132 if (!quotes_match(foo->data, quote_len, foo->prev->data))
1133 return TRUE;
1134
1135 temp_id_len = indent_length(foo->prev->data + quote_len);
1136
1137 /* Case 2) or 5) or 4). */
1138 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
1139 (quote_len == 0 && indent_len > 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001140#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001141 && !ISSET(AUTOINDENT)
1142#endif
1143 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
1144 foo->data + quote_len, indent_len))
1145 return TRUE;
1146
1147 return FALSE;
1148}
1149
1150/* Is foo inside a paragraph? */
1151bool inpar(const filestruct *const foo)
1152{
1153 size_t quote_len;
1154
1155 if (foo == NULL)
1156 return FALSE;
1157
1158 quote_len = quote_length(foo->data);
1159
David Lawrence Ramsey21014032005-11-09 20:33:42 +00001160 return (foo->data[quote_len + indent_length(foo->data +
1161 quote_len)] != '\0');
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001162}
1163
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001164/* Move the next par_len lines, starting with first_line, into the
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001165 * justify buffer, leaving copies of those lines in place. Assume that
1166 * par_len is greater than zero, and that there are enough lines after
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001167 * first_line. */
1168void backup_lines(filestruct *first_line, size_t par_len)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001169{
1170 filestruct *top = first_line;
1171 /* The top of the paragraph we're backing up. */
1172 filestruct *bot = first_line;
1173 /* The bottom of the paragraph we're backing up. */
1174 size_t i;
1175 /* Generic loop variable. */
1176 size_t current_x_save = openfile->current_x;
1177 ssize_t fl_lineno_save = first_line->lineno;
1178 ssize_t edittop_lineno_save = openfile->edittop->lineno;
1179 ssize_t current_lineno_save = openfile->current->lineno;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001180#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001181 bool old_mark_set = openfile->mark_set;
1182 ssize_t mb_lineno_save = 0;
1183 size_t mark_begin_x_save = 0;
1184
1185 if (old_mark_set) {
1186 mb_lineno_save = openfile->mark_begin->lineno;
1187 mark_begin_x_save = openfile->mark_begin_x;
1188 }
1189#endif
1190
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001191 /* par_len will be one greater than the number of lines between
1192 * current and filebot if filebot is the last line in the
1193 * paragraph. */
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001194 assert(par_len > 0 && openfile->current->lineno + par_len <=
David Lawrence Ramsey24777c02005-12-01 05:49:08 +00001195 openfile->filebot->lineno + 1);
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001196
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001197 /* Move bot down par_len lines to the line after the last line of
1198 * the paragraph, if there is one. */
1199 for (i = par_len; i > 0 && bot != openfile->filebot; i--)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001200 bot = bot->next;
1201
1202 /* Move the paragraph from the current buffer's filestruct to the
1203 * justify buffer. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001204 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
David Lawrence Ramseyf0575cf2005-11-09 23:27:51 +00001205 (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001206
1207 /* Copy the paragraph back to the current buffer's filestruct from
1208 * the justify buffer. */
1209 copy_from_filestruct(jusbuffer, jusbottom);
1210
1211 /* Move upward from the last line of the paragraph to the first
1212 * line, putting first_line, edittop, current, and mark_begin at the
1213 * same lines in the copied paragraph that they had in the original
1214 * paragraph. */
David Lawrence Ramsey5d6f1272005-11-10 03:32:59 +00001215 if (openfile->current != openfile->fileage)
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001216 top = openfile->current->prev;
1217 else
1218 top = openfile->current;
David Lawrence Ramseye8d505b2005-11-10 03:40:45 +00001219 for (i = par_len; i > 0 && top != NULL; i--) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001220 if (top->lineno == fl_lineno_save)
1221 first_line = top;
1222 if (top->lineno == edittop_lineno_save)
1223 openfile->edittop = top;
1224 if (top->lineno == current_lineno_save)
1225 openfile->current = top;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001226#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001227 if (old_mark_set && top->lineno == mb_lineno_save) {
1228 openfile->mark_begin = top;
1229 openfile->mark_begin_x = mark_begin_x_save;
1230 }
1231#endif
1232 top = top->prev;
1233 }
1234
1235 /* Put current_x at the same place in the copied paragraph that it
1236 * had in the original paragraph. */
1237 openfile->current_x = current_x_save;
1238
1239 set_modified();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001240}
1241
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001242/* Find the beginning of the current paragraph if we're in one, or the
1243 * beginning of the next paragraph if we're not. Afterwards, save the
1244 * quote length and paragraph length in *quote and *par. Return TRUE if
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001245 * we found a paragraph, and FALSE if there was an error or we didn't
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001246 * find a paragraph.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001247 *
1248 * See the comment at begpar() for more about when a line is the
1249 * beginning of a paragraph. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001250bool find_paragraph(size_t *const quote, size_t *const par)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001251{
1252 size_t quote_len;
1253 /* Length of the initial quotation of the paragraph we search
1254 * for. */
1255 size_t par_len;
1256 /* Number of lines in the paragraph we search for. */
1257 filestruct *current_save;
1258 /* The line at the beginning of the paragraph we search for. */
1259 ssize_t current_y_save;
1260 /* The y-coordinate at the beginning of the paragraph we search
1261 * for. */
1262
1263#ifdef HAVE_REGEX_H
1264 if (quoterc != 0) {
1265 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1266 return FALSE;
1267 }
1268#endif
1269
1270 assert(openfile->current != NULL);
1271
David Lawrence Ramsey1be131a2005-11-11 03:55:52 +00001272 /* If we're at the end of the last line of the file, it means that
1273 * there aren't any paragraphs left, so get out. */
1274 if (openfile->current == openfile->filebot && openfile->current_x ==
1275 strlen(openfile->filebot->data))
1276 return FALSE;
1277
1278 /* If the current line isn't in a paragraph, move forward to the
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001279 * last line of the next paragraph, if any. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001280 if (!inpar(openfile->current)) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001281 do_para_end(FALSE);
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001282
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001283 /* If we end up past the beginning of the line, it means that
1284 * we're at the end of the last line of the file, and the line
1285 * isn't blank, in which case the last line of the file is the
1286 * last line of the next paragraph.
1287 *
1288 * Otherwise, if we end up on a line that's in a paragraph, it
1289 * means that we're on the line after the last line of the next
1290 * paragraph, in which case we should move back to the last line
1291 * of the next paragraph. */
1292 if (openfile->current_x == 0) {
1293 if (!inpar(openfile->current->prev))
1294 return FALSE;
1295 if (openfile->current != openfile->fileage)
1296 openfile->current = openfile->current->prev;
1297 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001298 }
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001299
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001300 /* If the current line isn't the first line of the paragraph, move
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001301 * back to the first line of the paragraph. */
1302 if (!begpar(openfile->current))
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001303 do_para_begin(FALSE);
1304
1305 /* Now current is the first line of the paragraph. Set quote_len to
1306 * the quotation length of that line, and set par_len to the number
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001307 * of lines in this paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001308 quote_len = quote_length(openfile->current->data);
1309 current_save = openfile->current;
1310 current_y_save = openfile->current_y;
1311 do_para_end(FALSE);
1312 par_len = openfile->current->lineno - current_save->lineno;
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001313
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001314 /* If we end up past the beginning of the line, it means that we're
1315 * at the end of the last line of the file, and the line isn't
1316 * blank, in which case the last line of the file is part of the
1317 * paragraph. */
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001318 if (openfile->current_x > 0)
1319 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001320 openfile->current = current_save;
1321 openfile->current_y = current_y_save;
1322
1323 /* Save the values of quote_len and par_len. */
1324 assert(quote != NULL && par != NULL);
1325
1326 *quote = quote_len;
1327 *par = par_len;
1328
1329 return TRUE;
1330}
1331
1332/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1333 * the current paragraph. */
1334void do_justify(bool full_justify)
1335{
1336 filestruct *first_par_line = NULL;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001337 /* Will be the first line of the justified paragraph(s), if any.
1338 * For restoring after unjustify. */
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001339 filestruct *last_par_line = NULL;
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001340 /* Will be the line after the last line of the justified
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001341 * paragraph(s), if any. Also for restoring after unjustify. */
David Lawrence Ramsey82b5deb2005-11-10 06:07:57 +00001342 bool filebot_inpar = FALSE;
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001343 /* Whether the text at filebot is part of the current
1344 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001345
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001346 /* We save these variables to be restored if the user
1347 * unjustifies. */
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001348 filestruct *edittop_save = openfile->edittop;
1349 filestruct *current_save = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001350 size_t current_x_save = openfile->current_x;
1351 size_t pww_save = openfile->placewewant;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001352 size_t totsize_save = openfile->totsize;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001353#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001354 filestruct *mark_begin_save = openfile->mark_begin;
1355 size_t mark_begin_x_save = openfile->mark_begin_x;
1356#endif
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001357 bool modified_save = openfile->modified;
1358
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001359 int kbinput;
1360 bool meta_key, func_key, s_or_t, ran_func, finished;
1361
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001362 /* Move to the beginning of the current line, so that justifying at
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001363 * the end of the last line of the file, if that line isn't blank,
1364 * will work the first time through. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001365 openfile->current_x = 0;
1366
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001367 /* If we're justifying the entire file, start at the beginning. */
1368 if (full_justify)
1369 openfile->current = openfile->fileage;
1370
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001371 while (TRUE) {
1372 size_t i;
1373 /* Generic loop variable. */
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001374 filestruct *curr_first_par_line;
1375 /* The first line of the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001376 size_t quote_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001377 /* Length of the initial quotation of the current
1378 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001379 size_t indent_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001380 /* Length of the initial indentation of the current
1381 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001382 size_t par_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001383 /* Number of lines in the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001384 ssize_t break_pos;
1385 /* Where we will break lines. */
1386 char *indent_string;
1387 /* The first indentation that doesn't match the initial
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001388 * indentation of the current paragraph. This is put at the
1389 * beginning of every line broken off the first justified
1390 * line of the paragraph. Note that this works because a
1391 * paragraph can only contain two indentations at most: the
1392 * initial one, and a different one starting on a line after
1393 * the first. See the comment at begpar() for more about
1394 * when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001395
1396 /* Find the first line of the paragraph to be justified. That
1397 * is the start of this paragraph if we're in one, or the start
1398 * of the next otherwise. Save the quote length and paragraph
1399 * length (number of lines). Don't refresh the screen yet,
1400 * since we'll do that after we justify.
1401 *
1402 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001403 * justifying the whole file, and we've found at least one
1404 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001405 * last line of the file, so set the last line of the text to be
1406 * justified to the last line of the file and break out of the
1407 * loop. Otherwise, it means that there are no paragraph(s) to
1408 * justify, so refresh the screen and get out. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001409 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001410 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001411 last_par_line = openfile->filebot;
1412 break;
1413 } else {
1414 edit_refresh();
1415 return;
1416 }
1417 }
1418
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001419 /* par_len will be one greater than the number of lines between
1420 * current and filebot if filebot is the last line in the
1421 * paragraph. Set filebot_inpar to TRUE if this is the case. */
1422 filebot_inpar = (openfile->current->lineno + par_len ==
1423 openfile->filebot->lineno + 1);
1424
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001425 /* If we haven't already done it, move the original paragraph(s)
1426 * to the justify buffer, splice a copy of the original
1427 * paragraph(s) into the file in the same place, and set
1428 * first_par_line to the first line of the copy. */
1429 if (first_par_line == NULL) {
1430 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00001431 openfile->filebot->lineno - openfile->current->lineno +
1432 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001433 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001434 first_par_line = openfile->current;
1435 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001436
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001437 /* Set curr_first_par_line to the first line of the current
1438 * paragraph. */
1439 curr_first_par_line = openfile->current;
1440
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001441 /* Initialize indent_string to a blank string. */
1442 indent_string = mallocstrcpy(NULL, "");
1443
1444 /* Find the first indentation in the paragraph that doesn't
David Lawrence Ramsey8602fd62006-05-28 18:43:21 +00001445 * match the indentation of the first line, and save it in
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001446 * indent_string. If all the indentations are the same, save
1447 * the indentation of the first line in indent_string. */
1448 {
1449 const filestruct *indent_line = openfile->current;
1450 bool past_first_line = FALSE;
1451
1452 for (i = 0; i < par_len; i++) {
1453 indent_len = quote_len +
1454 indent_length(indent_line->data + quote_len);
1455
1456 if (indent_len != strlen(indent_string)) {
1457 indent_string = mallocstrncpy(indent_string,
1458 indent_line->data, indent_len + 1);
1459 indent_string[indent_len] = '\0';
1460
1461 if (past_first_line)
1462 break;
1463 }
1464
1465 if (indent_line == openfile->current)
1466 past_first_line = TRUE;
1467
1468 indent_line = indent_line->next;
1469 }
1470 }
1471
1472 /* Now tack all the lines of the paragraph together, skipping
1473 * the quoting and indentation on all lines after the first. */
1474 for (i = 0; i < par_len - 1; i++) {
1475 filestruct *next_line = openfile->current->next;
1476 size_t line_len = strlen(openfile->current->data);
1477 size_t next_line_len =
1478 strlen(openfile->current->next->data);
1479
1480 indent_len = quote_len +
1481 indent_length(openfile->current->next->data +
1482 quote_len);
1483
1484 next_line_len -= indent_len;
1485 openfile->totsize -= indent_len;
1486
1487 /* We're just about to tack the next line onto this one. If
1488 * this line isn't empty, make sure it ends in a space. */
1489 if (line_len > 0 &&
1490 openfile->current->data[line_len - 1] != ' ') {
1491 line_len++;
1492 openfile->current->data =
1493 charealloc(openfile->current->data,
1494 line_len + 1);
1495 openfile->current->data[line_len - 1] = ' ';
1496 openfile->current->data[line_len] = '\0';
1497 openfile->totsize++;
1498 }
1499
1500 openfile->current->data =
1501 charealloc(openfile->current->data, line_len +
1502 next_line_len + 1);
1503 strcat(openfile->current->data, next_line->data +
1504 indent_len);
1505
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00001506 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00001507 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001508 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00001509 if (next_line == openfile->filebot)
1510 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001511
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001512#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001513 /* Adjust the mark coordinates to compensate for the change
1514 * in the next line. */
1515 if (openfile->mark_set && openfile->mark_begin ==
1516 next_line) {
1517 openfile->mark_begin = openfile->current;
1518 openfile->mark_begin_x += line_len - indent_len;
1519 }
1520#endif
1521
1522 unlink_node(next_line);
1523 delete_node(next_line);
1524
1525 /* If we've removed the next line, we need to go through
1526 * this line again. */
1527 i--;
1528
1529 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001530 openfile->totsize--;
1531 }
1532
1533 /* Call justify_format() on the paragraph, which will remove
1534 * excess spaces from it and change all blank characters to
1535 * spaces. */
1536 justify_format(openfile->current, quote_len +
1537 indent_length(openfile->current->data + quote_len));
1538
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001539 while (par_len > 0 && strlenpt(openfile->current->data) >
1540 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001541 size_t line_len = strlen(openfile->current->data);
1542
1543 indent_len = strlen(indent_string);
1544
1545 /* If this line is too long, try to wrap it to the next line
1546 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001547 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001548 fill - strnlenpt(openfile->current->data, indent_len)
1549#ifndef DISABLE_HELP
1550 , FALSE
1551#endif
1552 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001553
1554 /* We can't break the line, or don't need to, so get out. */
1555 if (break_pos == -1 || break_pos + indent_len == line_len)
1556 break;
1557
1558 /* Move forward to the character after the indentation and
1559 * just after the space. */
1560 break_pos += indent_len + 1;
1561
1562 assert(break_pos <= line_len);
1563
1564 /* Make a new line, and copy the text after where we're
1565 * going to break this line to the beginning of the new
1566 * line. */
1567 splice_node(openfile->current,
1568 make_new_node(openfile->current),
1569 openfile->current->next);
1570
1571 /* If this paragraph is non-quoted, and autoindent isn't
1572 * turned on, set the indentation length to zero so that the
1573 * indentation is treated as part of the line. */
1574 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001575#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001576 && !ISSET(AUTOINDENT)
1577#endif
1578 )
1579 indent_len = 0;
1580
1581 /* Copy the text after where we're going to break the
1582 * current line to the next line. */
1583 openfile->current->next->data = charalloc(indent_len + 1 +
1584 line_len - break_pos);
1585 strncpy(openfile->current->next->data, indent_string,
1586 indent_len);
1587 strcpy(openfile->current->next->data + indent_len,
1588 openfile->current->data + break_pos);
1589
1590 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001591 openfile->totsize += indent_len + 1;
1592
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001593#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001594 /* Adjust the mark coordinates to compensate for the change
1595 * in the current line. */
1596 if (openfile->mark_set && openfile->mark_begin ==
1597 openfile->current && openfile->mark_begin_x >
1598 break_pos) {
1599 openfile->mark_begin = openfile->current->next;
1600 openfile->mark_begin_x -= break_pos - indent_len;
1601 }
1602#endif
1603
1604 /* Break the current line. */
1605 null_at(&openfile->current->data, break_pos);
1606
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001607 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00001608 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001609 if (openfile->filebot == openfile->current)
1610 openfile->filebot = openfile->filebot->next;
1611
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001612 /* Go to the next line. */
1613 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001614 openfile->current_y++;
1615 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001616 }
1617
1618 /* We're done breaking lines, so we don't need indent_string
1619 * anymore. */
1620 free(indent_string);
1621
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001622 /* Go to the next line, if possible. If there is no next line,
1623 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001624 if (openfile->current != openfile->filebot) {
1625 openfile->current_y++;
1626 openfile->current = openfile->current->next;
1627 } else
1628 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001629
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001630 /* Renumber the lines of the now-justified current paragraph,
1631 * since both find_paragraph() and edit_refresh() need the line
1632 * numbers to be right. */
1633 renumber(curr_first_par_line);
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00001634
1635 /* We've just finished justifying the paragraph. If we're not
1636 * justifying the entire file, break out of the loop.
1637 * Otherwise, continue the loop so that we justify all the
1638 * paragraphs in the file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001639 if (!full_justify)
1640 break;
1641 }
1642
1643 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001644 * up. current_y and totsize have been maintained above. If we
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00001645 * actually justified something, set last_par_line to the new end of
1646 * the paragraph. */
1647 if (first_par_line != NULL)
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001648 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001649
1650 edit_refresh();
1651
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00001652#ifndef NANO_TINY
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00001653 /* We're going to set jump_buf so that we return here after a
1654 * SIGWINCH instead of to main(). Indicate this. */
1655 jump_buf_main = FALSE;
1656
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00001657 /* Return here after a SIGWINCH. */
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00001658 sigsetjmp(jump_buf, 1);
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00001659#endif
1660
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001661 statusbar(_("Can now UnJustify!"));
1662
1663 /* If constant cursor position display is on, make sure the current
1664 * cursor position will be properly displayed on the statusbar. */
1665 if (ISSET(CONST_UPDATE))
1666 do_cursorpos(TRUE);
1667
1668 /* Display the shortcut list with UnJustify. */
1669 shortcut_init(TRUE);
1670 display_main_list();
1671
1672 /* Now get a keystroke and see if it's unjustify. If not, put back
1673 * the keystroke and return. */
1674 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
1675 &finished, FALSE);
1676
David Lawrence Ramseyb22c80a2006-05-06 13:41:59 +00001677 if (s_or_t && kbinput == NANO_UNJUSTIFY_KEY) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001678 /* Splice the justify buffer back into the file, but only if we
1679 * actually justified something. */
1680 if (first_par_line != NULL) {
1681 filestruct *top_save;
1682
1683 /* Partition the filestruct so that it contains only the
1684 * text of the justified paragraph. */
1685 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00001686 last_par_line, filebot_inpar ?
1687 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001688
1689 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001690 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001691 free_filestruct(openfile->fileage);
1692 openfile->fileage = jusbuffer;
1693 openfile->filebot = jusbottom;
1694
1695 top_save = openfile->fileage;
1696
1697 /* Unpartition the filestruct so that it contains all the
1698 * text again. Note that the justified paragraph has been
1699 * replaced with the unjustified paragraph. */
1700 unpartition_filestruct(&filepart);
1701
1702 /* Renumber starting with the beginning line of the old
1703 * partition. */
1704 renumber(top_save);
1705
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001706 /* Restore the justify we just did (ungrateful user!). */
1707 openfile->edittop = edittop_save;
1708 openfile->current = current_save;
1709 openfile->current_x = current_x_save;
1710 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001711 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001712#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001713 if (openfile->mark_set) {
1714 openfile->mark_begin = mark_begin_save;
1715 openfile->mark_begin_x = mark_begin_x_save;
1716 }
1717#endif
1718 openfile->modified = modified_save;
1719
1720 /* Clear the justify buffer. */
1721 jusbuffer = NULL;
1722
1723 if (!openfile->modified)
1724 titlebar(NULL);
1725 edit_refresh();
1726 }
1727 } else {
1728 unget_kbinput(kbinput, meta_key, func_key);
1729
1730 /* Blow away the text in the justify buffer. */
1731 free_filestruct(jusbuffer);
1732 jusbuffer = NULL;
1733 }
1734
1735 blank_statusbar();
1736
1737 /* Display the shortcut list with UnCut. */
1738 shortcut_init(FALSE);
1739 display_main_list();
1740}
1741
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001742/* Justify the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001743void do_justify_void(void)
1744{
1745 do_justify(FALSE);
1746}
1747
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001748/* Justify the entire file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001749void do_full_justify(void)
1750{
1751 do_justify(TRUE);
1752}
1753#endif /* !DISABLE_JUSTIFY */
1754
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001755#ifndef DISABLE_SPELLER
1756/* A word is misspelled in the file. Let the user replace it. We
1757 * return FALSE if the user cancels. */
1758bool do_int_spell_fix(const char *word)
1759{
1760 char *save_search, *save_replace;
1761 size_t match_len, current_x_save = openfile->current_x;
1762 size_t pww_save = openfile->placewewant;
1763 filestruct *edittop_save = openfile->edittop;
1764 filestruct *current_save = openfile->current;
1765 /* Save where we are. */
1766 bool canceled = FALSE;
1767 /* The return value. */
1768 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001769#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001770 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
1771#endif
1772#ifdef HAVE_REGEX_H
1773 bool regexp_set = ISSET(USE_REGEXP);
1774#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001775#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001776 bool old_mark_set = openfile->mark_set;
1777 bool added_magicline = FALSE;
1778 /* Whether we added a magicline after filebot. */
1779 bool right_side_up = FALSE;
1780 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1781 * FALSE if (current, current_x) is. */
1782 filestruct *top, *bot;
1783 size_t top_x, bot_x;
1784#endif
1785
1786 /* Make sure spell-check is case sensitive. */
1787 SET(CASE_SENSITIVE);
1788
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001789#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001790 /* Make sure spell-check goes forward only. */
1791 UNSET(BACKWARDS_SEARCH);
1792#endif
1793#ifdef HAVE_REGEX_H
1794 /* Make sure spell-check doesn't use regular expressions. */
1795 UNSET(USE_REGEXP);
1796#endif
1797
1798 /* Save the current search/replace strings. */
1799 search_init_globals();
1800 save_search = last_search;
1801 save_replace = last_replace;
1802
1803 /* Set the search/replace strings to the misspelled word. */
1804 last_search = mallocstrcpy(NULL, word);
1805 last_replace = mallocstrcpy(NULL, word);
1806
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001807#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001808 if (old_mark_set) {
1809 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001810 * contains only the marked text; if the NO_NEWLINES flag isn't
1811 * set, keep track of whether the text will have a magicline
1812 * added when we're done correcting misspelled words; and
1813 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001814 mark_order((const filestruct **)&top, &top_x,
1815 (const filestruct **)&bot, &bot_x, &right_side_up);
1816 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001817 if (!ISSET(NO_NEWLINES))
1818 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001819 openfile->mark_set = FALSE;
1820 }
1821#endif
1822
1823 /* Start from the top of the file. */
1824 openfile->edittop = openfile->fileage;
1825 openfile->current = openfile->fileage;
1826 openfile->current_x = (size_t)-1;
1827 openfile->placewewant = 0;
1828
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001829 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001830 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001831 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001832 &match_len)) {
1833 if (is_whole_word(openfile->current_x, openfile->current->data,
1834 word)) {
1835 size_t xpt = xplustabs();
1836 char *exp_word = display_string(openfile->current->data,
1837 xpt, strnlenpt(openfile->current->data,
1838 openfile->current_x + match_len) - xpt, FALSE);
1839
1840 edit_refresh();
1841
1842 do_replace_highlight(TRUE, exp_word);
1843
1844 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00001845 canceled = (do_prompt(FALSE,
1846#ifndef DISABLE_TABCOMP
1847 TRUE,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001848#endif
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00001849 spell_list, word,
1850#ifndef NANO_TINY
1851 NULL,
1852#endif
David Lawrence Ramsey68160072006-02-18 21:32:29 +00001853 edit_refresh, _("Edit a replacement")) == -1);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001854
1855 do_replace_highlight(FALSE, exp_word);
1856
1857 free(exp_word);
1858
1859 if (!canceled && strcmp(word, answer) != 0) {
1860 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001861 do_replace_loop(TRUE, &canceled, openfile->current,
1862 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001863 }
1864
1865 break;
1866 }
1867 }
1868
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001869#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001870 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001871 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
1872 * added a magicline, remove it now. */
1873 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001874 remove_magicline();
1875
1876 /* Put the beginning and the end of the mark at the beginning
1877 * and the end of the spell-checked text. */
1878 if (openfile->fileage == openfile->filebot)
1879 bot_x += top_x;
1880 if (right_side_up) {
1881 openfile->mark_begin_x = top_x;
1882 current_x_save = bot_x;
1883 } else {
1884 current_x_save = top_x;
1885 openfile->mark_begin_x = bot_x;
1886 }
1887
1888 /* Unpartition the filestruct so that it contains all the text
1889 * again, and turn the mark back on. */
1890 unpartition_filestruct(&filepart);
1891 openfile->mark_set = TRUE;
1892 }
1893#endif
1894
1895 /* Restore the search/replace strings. */
1896 free(last_search);
1897 last_search = save_search;
1898 free(last_replace);
1899 last_replace = save_replace;
1900
1901 /* Restore where we were. */
1902 openfile->edittop = edittop_save;
1903 openfile->current = current_save;
1904 openfile->current_x = current_x_save;
1905 openfile->placewewant = pww_save;
1906
1907 /* Restore case sensitivity setting. */
1908 if (!case_sens_set)
1909 UNSET(CASE_SENSITIVE);
1910
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001911#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001912 /* Restore search/replace direction. */
1913 if (backwards_search_set)
1914 SET(BACKWARDS_SEARCH);
1915#endif
1916#ifdef HAVE_REGEX_H
1917 /* Restore regular expression usage setting. */
1918 if (regexp_set)
1919 SET(USE_REGEXP);
1920#endif
1921
1922 return !canceled;
1923}
1924
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001925/* Internal (integrated) spell checking using the spell program,
1926 * filtered through the sort and uniq programs. Return NULL for normal
1927 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001928const char *do_int_speller(const char *tempfile_name)
1929{
1930 char *read_buff, *read_buff_ptr, *read_buff_word;
1931 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
1932 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
1933 pid_t pid_spell, pid_sort, pid_uniq;
1934 int spell_status, sort_status, uniq_status;
1935
1936 /* Create all three pipes up front. */
1937 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
1938 pipe(uniq_fd) == -1)
1939 return _("Could not create pipe");
1940
1941 statusbar(_("Creating misspelled word list, please wait..."));
1942
1943 /* A new process to run spell in. */
1944 if ((pid_spell = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00001945 /* Child continues (i.e. future spell process). */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001946 close(spell_fd[0]);
1947
1948 /* Replace the standard input with the temp file. */
1949 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1950 goto close_pipes_and_exit;
1951
1952 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1953 goto close_pipes_and_exit;
1954
1955 close(tempfile_fd);
1956
1957 /* Send spell's standard output to the pipe. */
1958 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1959 goto close_pipes_and_exit;
1960
1961 close(spell_fd[1]);
1962
David Lawrence Ramsey3239ff22005-11-29 20:01:06 +00001963 /* Start the spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001964 execlp("spell", "spell", NULL);
1965
1966 /* This should not be reached if spell is found. */
1967 exit(1);
1968 }
1969
1970 /* Parent continues here. */
1971 close(spell_fd[1]);
1972
1973 /* A new process to run sort in. */
1974 if ((pid_sort = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00001975 /* Child continues (i.e. future spell process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001976 * standard input with the standard output of the old pipe. */
1977 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1978 goto close_pipes_and_exit;
1979
1980 close(spell_fd[0]);
1981
1982 /* Send sort's standard output to the new pipe. */
1983 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1984 goto close_pipes_and_exit;
1985
1986 close(sort_fd[1]);
1987
1988 /* Start the sort program. Use -f to remove mixed case. If
1989 * this isn't portable, let me know. */
1990 execlp("sort", "sort", "-f", NULL);
1991
1992 /* This should not be reached if sort is found. */
1993 exit(1);
1994 }
1995
1996 close(spell_fd[0]);
1997 close(sort_fd[1]);
1998
1999 /* A new process to run uniq in. */
2000 if ((pid_uniq = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002001 /* Child continues (i.e. future uniq process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002002 * standard input with the standard output of the old pipe. */
2003 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
2004 goto close_pipes_and_exit;
2005
2006 close(sort_fd[0]);
2007
2008 /* Send uniq's standard output to the new pipe. */
2009 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2010 goto close_pipes_and_exit;
2011
2012 close(uniq_fd[1]);
2013
2014 /* Start the uniq program; we are using PATH. */
2015 execlp("uniq", "uniq", NULL);
2016
2017 /* This should not be reached if uniq is found. */
2018 exit(1);
2019 }
2020
2021 close(sort_fd[0]);
2022 close(uniq_fd[1]);
2023
2024 /* The child process was not forked successfully. */
2025 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
2026 close(uniq_fd[0]);
2027 return _("Could not fork");
2028 }
2029
2030 /* Get the system pipe buffer size. */
2031 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
2032 close(uniq_fd[0]);
2033 return _("Could not get size of pipe buffer");
2034 }
2035
2036 /* Read in the returned spelling errors. */
2037 read_buff_read = 0;
2038 read_buff_size = pipe_buff_size + 1;
2039 read_buff = read_buff_ptr = charalloc(read_buff_size);
2040
2041 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
2042 pipe_buff_size)) > 0) {
2043 read_buff_read += bytesread;
2044 read_buff_size += pipe_buff_size;
2045 read_buff = read_buff_ptr = charealloc(read_buff,
2046 read_buff_size);
2047 read_buff_ptr += read_buff_read;
2048 }
2049
2050 *read_buff_ptr = '\0';
2051 close(uniq_fd[0]);
2052
2053 /* Process the spelling errors. */
2054 read_buff_word = read_buff_ptr = read_buff;
2055
2056 while (*read_buff_ptr != '\0') {
2057 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
2058 *read_buff_ptr = '\0';
2059 if (read_buff_word != read_buff_ptr) {
2060 if (!do_int_spell_fix(read_buff_word)) {
2061 read_buff_word = read_buff_ptr;
2062 break;
2063 }
2064 }
2065 read_buff_word = read_buff_ptr + 1;
2066 }
2067 read_buff_ptr++;
2068 }
2069
2070 /* Special case: the last word doesn't end with '\r' or '\n'. */
2071 if (read_buff_word != read_buff_ptr)
2072 do_int_spell_fix(read_buff_word);
2073
2074 free(read_buff);
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002075 search_replace_abort();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002076 edit_refresh();
2077
2078 /* Process the end of the spell process. */
2079 waitpid(pid_spell, &spell_status, 0);
2080 waitpid(pid_sort, &sort_status, 0);
2081 waitpid(pid_uniq, &uniq_status, 0);
2082
2083 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
2084 return _("Error invoking \"spell\"");
2085
2086 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
2087 return _("Error invoking \"sort -f\"");
2088
2089 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
2090 return _("Error invoking \"uniq\"");
2091
2092 /* Otherwise... */
2093 return NULL;
2094
2095 close_pipes_and_exit:
2096 /* Don't leak any handles. */
2097 close(tempfile_fd);
2098 close(spell_fd[0]);
2099 close(spell_fd[1]);
2100 close(sort_fd[0]);
2101 close(sort_fd[1]);
2102 close(uniq_fd[0]);
2103 close(uniq_fd[1]);
2104 exit(1);
2105}
2106
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002107/* External (alternate) spell checking. Return NULL for normal
2108 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002109const char *do_alt_speller(char *tempfile_name)
2110{
2111 int alt_spell_status;
2112 size_t current_x_save = openfile->current_x;
2113 size_t pww_save = openfile->placewewant;
2114 ssize_t current_y_save = openfile->current_y;
2115 ssize_t lineno_save = openfile->current->lineno;
2116 pid_t pid_spell;
2117 char *ptr;
2118 static int arglen = 3;
2119 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002120#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002121 bool old_mark_set = openfile->mark_set;
2122 bool added_magicline = FALSE;
2123 /* Whether we added a magicline after filebot. */
2124 bool right_side_up = FALSE;
2125 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2126 * FALSE if (current, current_x) is. */
2127 filestruct *top, *bot;
2128 size_t top_x, bot_x;
2129 ssize_t mb_lineno_save = 0;
2130 /* We're going to close the current file, and open the output of
2131 * the alternate spell command. The line that mark_begin points
2132 * to will be freed, so we save the line number and restore it
2133 * afterwards. */
2134 size_t totsize_save = openfile->totsize;
2135 /* Our saved value of totsize, used when we spell-check a marked
2136 * selection. */
2137
2138 if (old_mark_set) {
2139 /* If the mark is on, save the number of the line it starts on,
2140 * and then turn the mark off. */
2141 mb_lineno_save = openfile->mark_begin->lineno;
2142 openfile->mark_set = FALSE;
2143 }
2144#endif
2145
2146 endwin();
2147
2148 /* Set up an argument list to pass execvp(). */
2149 if (spellargs == NULL) {
2150 spellargs = (char **)nmalloc(arglen * sizeof(char *));
2151
2152 spellargs[0] = strtok(alt_speller, " ");
2153 while ((ptr = strtok(NULL, " ")) != NULL) {
2154 arglen++;
2155 spellargs = (char **)nrealloc(spellargs, arglen *
2156 sizeof(char *));
2157 spellargs[arglen - 3] = ptr;
2158 }
2159 spellargs[arglen - 1] = NULL;
2160 }
2161 spellargs[arglen - 2] = tempfile_name;
2162
2163 /* Start a new process for the alternate speller. */
2164 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey2e2112c2005-11-29 05:39:31 +00002165 /* Start alternate spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002166 execvp(spellargs[0], spellargs);
2167
2168 /* Should not be reached, if alternate speller is found!!! */
2169 exit(1);
2170 }
2171
2172 /* If we couldn't fork, get out. */
2173 if (pid_spell < 0)
2174 return _("Could not fork");
2175
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002176#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002177 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002178 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002179 allow_pending_sigwinch(FALSE);
2180#endif
2181
2182 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002183 wait(&alt_spell_status);
2184
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00002185 /* Reenter curses mode. */
2186 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002187
2188 /* Restore the terminal to its previous state. */
2189 terminal_init();
2190
2191 /* Turn the cursor back on for sure. */
2192 curs_set(1);
2193
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002194 /* The screen might have been resized. If it has, reinitialize all
2195 * the windows based on the new screen dimensions. */
2196 window_init();
2197
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002198 if (!WIFEXITED(alt_spell_status) ||
2199 WEXITSTATUS(alt_spell_status) != 0) {
2200 char *altspell_error;
2201 char *invoke_error = _("Error invoking \"%s\"");
2202
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002203#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002204 /* Turn the mark back on if it was on before. */
2205 openfile->mark_set = old_mark_set;
2206#endif
2207
2208 altspell_error =
2209 charalloc(strlen(invoke_error) +
2210 strlen(alt_speller) + 1);
2211 sprintf(altspell_error, invoke_error, alt_speller);
2212 return altspell_error;
2213 }
2214
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002215#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002216 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002217 /* If the mark is on, partition the filestruct so that it
2218 * contains only the marked text; if the NO_NEWLINES flag isn't
2219 * set, keep track of whether the text will have a magicline
2220 * added when we're done correcting misspelled words; and
2221 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002222 mark_order((const filestruct **)&top, &top_x,
2223 (const filestruct **)&bot, &bot_x, &right_side_up);
2224 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002225 if (!ISSET(NO_NEWLINES))
2226 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002227
2228 /* Get the number of characters in the marked text, and subtract
2229 * it from the saved value of totsize. */
2230 totsize_save -= get_totsize(top, bot);
2231 }
2232#endif
2233
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002234 /* Replace the text of the current buffer with the spell-checked
2235 * text. */
2236 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002237
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002238#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002239 if (old_mark_set) {
2240 filestruct *top_save = openfile->fileage;
2241
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002242 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2243 * added a magicline, remove it now. */
2244 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002245 remove_magicline();
2246
2247 /* Put the beginning and the end of the mark at the beginning
2248 * and the end of the spell-checked text. */
2249 if (openfile->fileage == openfile->filebot)
2250 bot_x += top_x;
2251 if (right_side_up) {
2252 openfile->mark_begin_x = top_x;
2253 current_x_save = bot_x;
2254 } else {
2255 current_x_save = top_x;
2256 openfile->mark_begin_x = bot_x;
2257 }
2258
2259 /* Unpartition the filestruct so that it contains all the text
2260 * again. Note that we've replaced the marked text originally
2261 * in the partition with the spell-checked marked text in the
2262 * temp file. */
2263 unpartition_filestruct(&filepart);
2264
2265 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002266 * partition. Also add the number of characters in the
2267 * spell-checked marked text to the saved value of totsize, and
2268 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002269 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002270 totsize_save += openfile->totsize;
2271 openfile->totsize = totsize_save;
2272
2273 /* Assign mark_begin to the line where the mark began before. */
2274 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2275 current_y_save, 0);
2276 openfile->mark_begin = openfile->current;
2277
2278 /* Assign mark_begin_x to the location in mark_begin where the
2279 * mark began before, adjusted for any shortening of the
2280 * line. */
2281 openfile->mark_begin_x = openfile->current_x;
2282
2283 /* Turn the mark back on. */
2284 openfile->mark_set = TRUE;
2285 }
2286#endif
2287
2288 /* Go back to the old position, and mark the file as modified. */
2289 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2290 set_modified();
2291
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002292#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002293 /* Handle a pending SIGWINCH again. */
2294 allow_pending_sigwinch(TRUE);
2295#endif
2296
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002297 return NULL;
2298}
2299
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002300/* Spell check the current file. If an alternate spell checker is
2301 * specified, use it. Otherwise, use the internal spell checker. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002302void do_spell(void)
2303{
2304 int i;
2305 FILE *temp_file;
2306 char *temp = safe_tempfile(&temp_file);
2307 const char *spell_msg;
2308
2309 if (temp == NULL) {
David Lawrence Ramseyf0e3ca62006-05-03 13:11:00 +00002310 statusbar(_("Error writing temp file: %s"), strerror(errno));
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002311 return;
2312 }
2313
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002314#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002315 if (openfile->mark_set)
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002316 i = write_marked_file(temp, temp_file, TRUE, OVERWRITE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002317 else
2318#endif
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002319 i = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002320
2321 if (i == -1) {
2322 statusbar(_("Error writing temp file: %s"), strerror(errno));
2323 free(temp);
2324 return;
2325 }
2326
2327 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2328 do_int_speller(temp);
2329 unlink(temp);
2330 free(temp);
2331
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002332 currshortcut = main_list;
2333
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002334 /* If the spell-checker printed any error messages onscreen, make
2335 * sure that they're cleared off. */
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002336 total_refresh();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002337
2338 if (spell_msg != NULL) {
2339 if (errno == 0)
2340 /* Don't display an error message of "Success". */
2341 statusbar(_("Spell checking failed: %s"), spell_msg);
2342 else
2343 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2344 strerror(errno));
2345 } else
2346 statusbar(_("Finished checking spelling"));
2347}
2348#endif /* !DISABLE_SPELLER */
2349
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002350#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002351/* Our own version of "wc". Note that its character counts are in
2352 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002353void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002354{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002355 size_t words = 0, chars = 0;
2356 ssize_t lines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002357 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002358 size_t pww_save = openfile->placewewant;
2359 filestruct *current_save = openfile->current;
2360 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002361 filestruct *top, *bot;
2362 size_t top_x, bot_x;
2363
2364 if (old_mark_set) {
2365 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002366 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002367 mark_order((const filestruct **)&top, &top_x,
2368 (const filestruct **)&bot, &bot_x, NULL);
2369 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002370 openfile->mark_set = FALSE;
2371 }
2372
2373 /* Start at the top of the file. */
2374 openfile->current = openfile->fileage;
2375 openfile->current_x = 0;
2376 openfile->placewewant = 0;
2377
2378 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002379 * part of a word, as "wc -w" does), without updating the screen,
2380 * until we reach the end of the file, incrementing the total word
2381 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002382 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002383 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002384 if (do_next_word(TRUE, FALSE))
2385 words++;
2386 }
2387
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002388 /* Get the total line and character counts, as "wc -l" and "wc -c"
2389 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002390 if (old_mark_set) {
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002391 lines = openfile->filebot->lineno -
2392 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002393 chars = get_totsize(openfile->fileage, openfile->filebot);
2394
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002395 /* Unpartition the filestruct so that it contains all the text
2396 * again, and turn the mark back on. */
2397 unpartition_filestruct(&filepart);
2398 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002399 } else {
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002400 lines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002401 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002402 }
2403
2404 /* Restore where we were. */
2405 openfile->current = current_save;
2406 openfile->current_x = current_x_save;
2407 openfile->placewewant = pww_save;
2408
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002409 /* Display the total word, line, and character counts on the
2410 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002411 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
David Lawrence Ramsey815fb0a2005-08-05 19:38:11 +00002412 _("In Selection: ") : "", (unsigned long)words, (long)lines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002413 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002414}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002415#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002416
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002417/* Get verbatim input. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002418void do_verbatim_input(void)
2419{
2420 int *kbinput;
2421 size_t kbinput_len, i;
2422 char *output;
2423
David Lawrence Ramseyf451d6a2006-05-27 16:02:48 +00002424 /* TRANSLATORS: This is displayed when the next keystroke will be
2425 * inserted verbatim. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002426 statusbar(_("Verbatim Input"));
2427
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002428 /* Read in all the verbatim characters. */
2429 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2430
David Lawrence Ramseya620e682006-05-27 18:19:03 +00002431 /* If constant cursor position display is on, make sure the current
2432 * cursor position will be properly displayed on the statusbar.
2433 * Otherwise, blank the statusbar. */
2434 if (ISSET(CONST_UPDATE))
2435 do_cursorpos(TRUE);
2436 else {
2437 blank_statusbar();
2438 wnoutrefresh(bottomwin);
2439 }
David Lawrence Ramsey6fb66892006-05-27 17:39:19 +00002440
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002441 /* Display all the verbatim characters at once, not filtering out
2442 * control characters. */
2443 output = charalloc(kbinput_len + 1);
2444
2445 for (i = 0; i < kbinput_len; i++)
2446 output[i] = (char)kbinput[i];
2447 output[i] = '\0';
2448
2449 do_output(output, kbinput_len, TRUE);
2450
2451 free(output);
2452}