blob: 59459c212495270ff58a49519e24d8b276d1e204 [file] [log] [blame]
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001/* $Id$ */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002/**************************************************************************
David Lawrence Ramsey4005fc62005-10-08 06:12:41 +00003 * text.c *
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00004 * *
Chris Allegretta8a07a962009-12-02 03:36:22 +00005 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
6 * 2008, 2009 Free Software Foundation, Inc. *
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 *
David Lawrence Ramseyd0035b42007-08-11 05:17:36 +00009 * the Free Software Foundation; either version 3, or (at your option) *
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000010 * 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 {
David Lawrence Ramsey7b0531a2006-07-31 01:30:31 +000058 statusbar(_("Mark Unset"));
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000059 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 Ramseyf1982f02006-12-10 17:57:09 +000066/* Delete the character under the cursor. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000067void do_delete(void)
68{
Chris Allegrettaa8bc4922009-12-12 22:21:20 +000069 size_t orig_lenpt = 0;
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000070
Chris Allegretta07fcc4c2008-07-10 20:13:04 +000071#ifndef NANO_TINY
Chris Allegretta14c86202008-08-03 04:48:05 +000072 update_undo(DEL);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +000073#endif
74
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000075 assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data));
76
77 openfile->placewewant = xplustabs();
78
79 if (openfile->current->data[openfile->current_x] != '\0') {
80 int char_buf_len = parse_mbchar(openfile->current->data +
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +000081 openfile->current_x, NULL, NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000082 size_t line_len = strlen(openfile->current->data +
83 openfile->current_x);
84
85 assert(openfile->current_x < strlen(openfile->current->data));
86
Chris Allegrettaa8bc4922009-12-12 22:21:20 +000087 if (ISSET(SOFTWRAP))
88 orig_lenpt = strlenpt(openfile->current->data);
89
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000090 /* Let's get dangerous. */
91 charmove(&openfile->current->data[openfile->current_x],
92 &openfile->current->data[openfile->current_x +
93 char_buf_len], line_len - char_buf_len + 1);
94
95 null_at(&openfile->current->data, openfile->current_x +
96 line_len - char_buf_len);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000097#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000098 if (openfile->mark_set && openfile->mark_begin ==
99 openfile->current && openfile->current_x <
100 openfile->mark_begin_x)
101 openfile->mark_begin_x -= char_buf_len;
102#endif
103 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000104 } else if (openfile->current != openfile->filebot) {
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000105 filestruct *foo = openfile->current->next;
106
107 assert(openfile->current_x == strlen(openfile->current->data));
108
109 /* If we're deleting at the end of a line, we need to call
110 * edit_refresh(). */
111 if (openfile->current->data[openfile->current_x] == '\0')
Chris Allegrettaa8bc4922009-12-12 22:21:20 +0000112 edit_refresh_needed = TRUE;
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000113
114 openfile->current->data = charealloc(openfile->current->data,
115 openfile->current_x + strlen(foo->data) + 1);
116 strcpy(openfile->current->data + openfile->current_x,
117 foo->data);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000118#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000119 if (openfile->mark_set && openfile->mark_begin ==
120 openfile->current->next) {
121 openfile->mark_begin = openfile->current;
122 openfile->mark_begin_x += openfile->current_x;
123 }
124#endif
125 if (openfile->filebot == foo)
126 openfile->filebot = openfile->current;
127
128 unlink_node(foo);
129 delete_node(foo);
130 renumber(openfile->current);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000131 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000132
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000133 /* If the NO_NEWLINES flag isn't set, and text has been added to
134 * the magicline as a result of deleting at the end of the line
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000135 * before filebot, add a new magicline. */
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000136 if (!ISSET(NO_NEWLINES) && openfile->current ==
137 openfile->filebot && openfile->current->data[0] != '\0')
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000138 new_magicline();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000139 } else
140 return;
141
Chris Allegrettaa8bc4922009-12-12 22:21:20 +0000142 if (ISSET(SOFTWRAP) && edit_refresh_needed == FALSE)
143 if (strlenpt(openfile->current->data) / COLS != orig_lenpt / COLS)
144 edit_refresh_needed = TRUE;
145
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000146 set_modified();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000147
Chris Allegrettaa8bc4922009-12-12 22:21:20 +0000148 if (edit_refresh_needed == FALSE)
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000149 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
David Lawrence Ramsey84d22e32006-11-08 13:05:50 +0000153 * character, and then delete the character under the cursor. */
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
Chris Allegretta5a018f02009-11-29 06:13:22 +0000187 do_output((char *) "\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 Ramseye44cd2d2007-01-11 22:54:55 +0000194/* Indent or unindent the current line (or, if the mark is on, all lines
195 * covered by the mark) len columns, depending on whether len is
196 * positive or negative. If the TABS_TO_SPACES flag is set, indent or
197 * unindent by len spaces. Otherwise, indent or unindent by (len /
198 * tabsize) tabs and (len % tabsize) spaces. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000199void 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. */
Chris Allegrettafd265af2009-02-06 03:41:02 +0000345 edit_refresh_needed = TRUE;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000346 }
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}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000362
Chris Allegrettab549f372008-09-16 21:35:19 +0000363/* undo a cut, or re-do an uncut */
364void undo_cut(undo *u)
365{
Chris Allegretta42726f72009-07-27 04:16:44 +0000366 /* If we cut the magicline may was well not crash :/ */
367 if (!u->cutbuffer)
368 return;
Chris Allegrettab549f372008-09-16 21:35:19 +0000369
Chris Allegretta42726f72009-07-27 04:16:44 +0000370 cutbuffer = copy_filestruct(u->cutbuffer);
Chris Allegrettab549f372008-09-16 21:35:19 +0000371
Chris Allegretta42726f72009-07-27 04:16:44 +0000372 /* Compute cutbottom for the uncut using out copy */
373 for (cutbottom = cutbuffer; cutbottom->next != NULL; cutbottom = cutbottom->next)
374 ;
Chris Allegrettab549f372008-09-16 21:35:19 +0000375
Chris Allegretta42726f72009-07-27 04:16:44 +0000376 /* Get to where we need to uncut from */
377 if (u->mark_set && u->mark_begin_lineno < u->lineno)
378 do_gotolinecolumn(u->mark_begin_lineno, u->mark_begin_x+1, FALSE, FALSE, FALSE, FALSE);
379 else
380 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
381
382 copy_from_filestruct(cutbuffer, cutbottom);
383 free_filestruct(cutbuffer);
384 cutbuffer = NULL;
Chris Allegrettab549f372008-09-16 21:35:19 +0000385
386}
387
388/* Re-do a cut, or undo an uncut */
389void redo_cut(undo *u) {
390 int i;
Chris Allegrettac81cf522008-10-14 04:34:56 +0000391 filestruct *t, *c;
Chris Allegrettab549f372008-09-16 21:35:19 +0000392
Chris Allegretta42726f72009-07-27 04:16:44 +0000393 /* If we cut the magicline may was well not crash :/ */
394 if (!u->cutbuffer)
395 return;
Chris Allegrettab549f372008-09-16 21:35:19 +0000396
Chris Allegretta42726f72009-07-27 04:16:44 +0000397 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
398 openfile->mark_set = u->mark_set;
399 if (cutbuffer)
400 free(cutbuffer);
401 cutbuffer = NULL;
402
403 /* Move ahead the same # lines we had if a marked cut */
404 if (u->mark_set) {
405 for (i = 1, t = openfile->fileage; i != u->mark_begin_lineno; i++)
406 t = t->next;
407 openfile->mark_begin = t;
408 } else if (!u->to_end) {
409 /* Here we have a regular old potentially multi-line ^K cut. We'll
410 need to trick nano into thinking it's a marked cut to cut more
411 than one line again */
412 for (c = u->cutbuffer, t = openfile->current; c->next != NULL && t->next != NULL; ) {
Chris Allegrettab549f372008-09-16 21:35:19 +0000413
414#ifdef DEBUG
Chris Allegretta6f083322009-11-11 06:00:33 +0000415 fprintf(stderr, "Advancing, lineno = %lu, data = \"%s\"\n", (unsigned long) t->lineno, t->data);
Chris Allegrettab549f372008-09-16 21:35:19 +0000416#endif
Chris Allegretta42726f72009-07-27 04:16:44 +0000417 c = c->next;
418 t = t->next;
419 }
420 openfile->mark_begin = t;
421 openfile->mark_begin_x = 0;
422 openfile->mark_set = TRUE;
423 }
Chris Allegrettab549f372008-09-16 21:35:19 +0000424
Chris Allegretta42726f72009-07-27 04:16:44 +0000425 openfile->mark_begin_x = u->mark_begin_x;
426 do_cut_text(FALSE, u->to_end, TRUE);
427 openfile->mark_set = FALSE;
428 openfile->mark_begin = NULL;
429 openfile->mark_begin_x = 0;
430 edit_refresh_needed = TRUE;
Chris Allegrettab549f372008-09-16 21:35:19 +0000431}
432
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000433/* Undo the last thing(s) we did */
434void do_undo(void)
435{
436 undo *u = openfile->current_undo;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000437 filestruct *f = openfile->current, *t;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000438 int len = 0;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000439 char *undidmsg, *data;
Chris Allegretta14c86202008-08-03 04:48:05 +0000440 filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000441
442 if (!u) {
443 statusbar(_("Nothing in undo buffer!"));
444 return;
445 }
446
447
448 if (u->lineno <= f->lineno)
449 for (; f->prev != NULL && f->lineno != u->lineno; f = f->prev)
450 ;
451 else
452 for (; f->next != NULL && f->lineno != u->lineno; f = f->next)
453 ;
454 if (f->lineno != u->lineno) {
Chris Allegretta77bf1b52008-08-21 04:21:06 +0000455 statusbar(_("Internal error: can't match line %d. Please save your work"), u->lineno);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000456 return;
457 }
458#ifdef DEBUG
459 fprintf(stderr, "data we're about to undo = \"%s\"\n", f->data);
460 fprintf(stderr, "Undo running for type %d\n", u->type);
461#endif
462
Chris Allegrettafa406942008-07-13 16:44:19 +0000463 openfile->current_x = u->begin;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000464 switch(u->type) {
465 case ADD:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000466 undidmsg = _("text add");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000467 len = strlen(f->data) - strlen(u->strdata) + 1;
468 data = charalloc(len);
469 strncpy(data, f->data, u->begin);
470 strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]);
471 free(f->data);
472 f->data = data;
473 break;
474 case DEL:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000475 undidmsg = _("text delete");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000476 len = strlen(f->data) + strlen(u->strdata) + 1;
477 data = charalloc(len);
478
479 strncpy(data, f->data, u->begin);
480 strcpy(&data[u->begin], u->strdata);
481 strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]);
482 free(f->data);
483 f->data = data;
Chris Allegretta637daa82011-02-07 14:45:56 +0000484 if (u->xflags == UNdel_backspace)
Chris Allegretta0b499d42008-07-14 07:18:22 +0000485 openfile->current_x += strlen(u->strdata);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000486 break;
Chris Allegrettac9f07992009-12-03 03:12:00 +0000487#ifndef DISABLE_WRAPPING
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000488 case SPLIT:
Chris Allegrettab843a512009-04-21 05:34:08 +0000489 undidmsg = _("line wrap");
Chris Allegretta5a018f02009-11-29 06:13:22 +0000490 f->data = (char *) nrealloc(f->data, strlen(f->data) + strlen(u->strdata) + 1);
Chris Allegrettadf543e72009-04-12 06:13:16 +0000491 strcpy(&f->data[strlen(f->data) - 1], u->strdata);
Chris Allegretta8b9fb362009-04-29 04:43:58 +0000492 if (u->strdata2 != NULL)
493 f->next->data = mallocstrcpy(f->next->data, u->strdata2);
494 else {
Chris Allegretta12ba5572009-03-26 01:01:48 +0000495 filestruct *foo = openfile->current->next;
496 unlink_node(foo);
497 delete_node(foo);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000498 }
Chris Allegretta0b499d42008-07-14 07:18:22 +0000499 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000500 break;
Chris Allegrettac9f07992009-12-03 03:12:00 +0000501#endif /* DISABLE_WRAPPING */
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000502 case UNSPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000503 undidmsg = _("line join");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000504 t = make_new_node(f);
505 t->data = mallocstrcpy(NULL, u->strdata);
506 data = mallocstrncpy(NULL, f->data, u->begin);
507 data[u->begin] = '\0';
508 free(f->data);
509 f->data = data;
510 splice_node(f, t, f->next);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000511 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000512 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000513 case CUT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000514 undidmsg = _("text cut");
Chris Allegrettab549f372008-09-16 21:35:19 +0000515 undo_cut(u);
516 break;
517 case UNCUT:
518 undidmsg = _("text uncut");
519 redo_cut(u);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000520 break;
Chris Allegrettab843a512009-04-21 05:34:08 +0000521 case ENTER:
522 undidmsg = _("line break");
523 if (f->next) {
Chris Allegrettaad37e672009-07-12 03:36:58 +0000524 filestruct *foo = f->next;
Chris Allegretta5a018f02009-11-29 06:13:22 +0000525 f->data = (char *) nrealloc(f->data, strlen(f->data) + strlen(f->next->data) + 1);
Chris Allegrettab843a512009-04-21 05:34:08 +0000526 strcat(f->data, f->next->data);
Chris Allegrettab843a512009-04-21 05:34:08 +0000527 unlink_node(foo);
528 delete_node(foo);
529 }
530 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000531 case INSERT:
532 undidmsg = _("text insert");
533 cutbuffer = NULL;
534 cutbottom = NULL;
535 /* When we updated mark_begin_lineno in update_undo, it was effectively how many line
536 were inserted due to being partitioned before read_file was called. So we
537 add its value here */
538 openfile->mark_begin = fsfromline(u->lineno + u->mark_begin_lineno - 1);
539 openfile->mark_begin_x = 0;
540 openfile->mark_set = TRUE;
541 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
542 cut_marked();
543 u->cutbuffer = cutbuffer;
544 u->cutbottom = cutbottom;
545 cutbuffer = oldcutbuffer;
546 cutbottom = oldcutbottom;
547 openfile->mark_set = FALSE;
548 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000549 case REPLACE:
550 undidmsg = _("text replace");
551 data = u->strdata;
552 u->strdata = f->data;
553 f->data = data;
554 break;
Chris Allegrettab843a512009-04-21 05:34:08 +0000555
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000556 default:
Chris Allegretta77bf1b52008-08-21 04:21:06 +0000557 undidmsg = _("Internal error: unknown type. Please save your work");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000558 break;
559
560 }
Chris Allegrettab843a512009-04-21 05:34:08 +0000561 renumber(f);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000562 do_gotolinecolumn(u->lineno, u->begin, FALSE, FALSE, FALSE, TRUE);
563 statusbar(_("Undid action (%s)"), undidmsg);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000564 openfile->current_undo = openfile->current_undo->next;
Chris Allegretta6f681c12008-08-08 03:02:03 +0000565 openfile->last_action = OTHER;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000566}
567
568void do_redo(void)
569{
570 undo *u = openfile->undotop;
Chris Allegrettaad37e672009-07-12 03:36:58 +0000571 filestruct *f = openfile->current;
Chris Allegretta4e12cb82008-10-14 19:55:34 +0000572 int len = 0;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000573 char *undidmsg, *data;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000574
575 for (; u != NULL && u->next != openfile->current_undo; u = u->next)
576 ;
577 if (!u) {
578 statusbar(_("Nothing to re-do!"));
579 return;
580 }
581 if (u->next != openfile->current_undo) {
Chris Allegretta77bf1b52008-08-21 04:21:06 +0000582 statusbar(_("Internal error: Redo setup failed. Please save your work"));
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000583 return;
584 }
585
586 if (u->lineno <= f->lineno)
587 for (; f->prev != NULL && f->lineno != u->lineno; f = f->prev)
588 ;
589 else
590 for (; f->next != NULL && f->lineno != u->lineno; f = f->next)
591 ;
592 if (f->lineno != u->lineno) {
Chris Allegretta77bf1b52008-08-21 04:21:06 +0000593 statusbar(_("Internal error: can't match line %d. Please save your work"), u->lineno);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000594 return;
595 }
596#ifdef DEBUG
597 fprintf(stderr, "data we're about to redo = \"%s\"\n", f->data);
598 fprintf(stderr, "Redo running for type %d\n", u->type);
599#endif
600
601 switch(u->type) {
602 case ADD:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000603 undidmsg = _("text add");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000604 len = strlen(f->data) + strlen(u->strdata) + 1;
605 data = charalloc(len);
Chris Allegretta1f37c452008-08-01 04:11:57 +0000606 strncpy(data, f->data, u->begin);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000607 strcpy(&data[u->begin], u->strdata);
608 strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]);
609 free(f->data);
610 f->data = data;
611 break;
612 case DEL:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000613 undidmsg = _("text delete");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000614 len = strlen(f->data) + strlen(u->strdata) + 1;
615 data = charalloc(len);
616 strncpy(data, f->data, u->begin);
617 strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]);
618 free(f->data);
619 f->data = data;
620 break;
Chris Allegrettab843a512009-04-21 05:34:08 +0000621 case ENTER:
622 undidmsg = _("line break");
623 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
Chris Allegrettae061a0d2009-04-25 03:31:30 +0000624 do_enter(TRUE);
Chris Allegrettab843a512009-04-21 05:34:08 +0000625 break;
Chris Allegrettac9f07992009-12-03 03:12:00 +0000626#ifndef DISABLE_WRAPPING
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000627 case SPLIT:
Chris Allegrettab843a512009-04-21 05:34:08 +0000628 undidmsg = _("line wrap");
Chris Allegretta637daa82011-02-07 14:45:56 +0000629 if (u->xflags & UNsplit_madenew)
Chris Allegrettaa4c2b992009-04-29 22:34:27 +0000630 prepend_wrap = TRUE;
631 do_wrap(f, TRUE);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000632 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000633 break;
Chris Allegrettac9f07992009-12-03 03:12:00 +0000634#endif /* DISABLE_WRAPPING */
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000635 case UNSPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000636 undidmsg = _("line join");
Chris Allegretta0b499d42008-07-14 07:18:22 +0000637 len = strlen(f->data) + strlen(u->strdata + 1);
638 data = charalloc(len);
639 strcpy(data, f->data);
640 strcat(data, u->strdata);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000641 free(f->data);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000642 f->data = data;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000643 if (f->next != NULL) {
644 filestruct *tmp = f->next;
645 unlink_node(tmp);
646 delete_node(tmp);
647 }
Chris Allegretta0b499d42008-07-14 07:18:22 +0000648 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000649 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000650 case CUT:
Chris Allegrettab549f372008-09-16 21:35:19 +0000651 undidmsg = _("text cut");
652 redo_cut(u);
653 break;
654 case UNCUT:
655 undidmsg = _("text uncut");
656 undo_cut(u);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000657 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000658 case REPLACE:
659 undidmsg = _("text replace");
660 data = u->strdata;
661 u->strdata = f->data;
662 f->data = data;
663 break;
Chris Allegrettab549f372008-09-16 21:35:19 +0000664 case INSERT:
Chris Allegrettaea577872008-08-03 20:19:42 +0000665 undidmsg = _("text insert");
Chris Allegrettaea577872008-08-03 20:19:42 +0000666 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
Chris Allegrettab549f372008-09-16 21:35:19 +0000667 copy_from_filestruct(u->cutbuffer, u->cutbottom);
668 openfile->placewewant = xplustabs();
Chris Allegrettaea577872008-08-03 20:19:42 +0000669 break;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000670 default:
Chris Allegretta77bf1b52008-08-21 04:21:06 +0000671 undidmsg = _("Internal error: unknown type. Please save your work");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000672 break;
673
674 }
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000675 do_gotolinecolumn(u->lineno, u->begin, FALSE, FALSE, FALSE, TRUE);
676 statusbar(_("Redid action (%s)"), undidmsg);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000677
678 openfile->current_undo = u;
Chris Allegretta6f681c12008-08-08 03:02:03 +0000679 openfile->last_action = OTHER;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000680
681}
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000682#endif /* !NANO_TINY */
683
David Lawrence Ramseyb0e04c02005-12-08 07:24:54 +0000684/* Someone hits Enter *gasp!* */
Chris Allegrettae061a0d2009-04-25 03:31:30 +0000685void do_enter(bool undoing)
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000686{
687 filestruct *newnode = make_new_node(openfile->current);
688 size_t extra = 0;
689
Chris Allegrettadc7136a2008-08-21 04:24:25 +0000690 assert(openfile->current != NULL && openfile->current->data != NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000691
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000692#ifndef NANO_TINY
Chris Allegrettae061a0d2009-04-25 03:31:30 +0000693 if (!undoing)
694 add_undo(ENTER);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000695
696
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000697 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
698 if (ISSET(AUTOINDENT)) {
699 /* If we are breaking the line in the indentation, the new
700 * indentation should have only current_x characters, and
701 * current_x should not change. */
702 extra = indent_length(openfile->current->data);
703 if (extra > openfile->current_x)
704 extra = openfile->current_x;
705 }
706#endif
707 newnode->data = charalloc(strlen(openfile->current->data +
708 openfile->current_x) + extra + 1);
709 strcpy(&newnode->data[extra], openfile->current->data +
710 openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000711#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000712 if (ISSET(AUTOINDENT)) {
713 strncpy(newnode->data, openfile->current->data, extra);
714 openfile->totsize += mbstrlen(newnode->data);
715 }
716#endif
717 null_at(&openfile->current->data, openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000718#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000719 if (openfile->mark_set && openfile->current ==
720 openfile->mark_begin && openfile->current_x <
721 openfile->mark_begin_x) {
722 openfile->mark_begin = newnode;
723 openfile->mark_begin_x += extra - openfile->current_x;
724 }
725#endif
726 openfile->current_x = extra;
727
728 if (openfile->current == openfile->filebot)
729 openfile->filebot = newnode;
730 splice_node(openfile->current, newnode,
731 openfile->current->next);
732
733 renumber(openfile->current);
734 openfile->current = newnode;
735
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000736 openfile->totsize++;
737 set_modified();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000738
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000739 openfile->placewewant = xplustabs();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000740
Chris Allegrettafd265af2009-02-06 03:41:02 +0000741 edit_refresh_needed = TRUE;
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000742}
743
Chris Allegretta637daa82011-02-07 14:45:56 +0000744/* Need this again... */
745void do_enter_void(void) {
746 do_enter(FALSE);
747}
748
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000749#ifndef NANO_TINY
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000750/* Send a SIGKILL (unconditional kill) to the forked process in
751 * execute_command(). */
David Lawrence Ramsey8befda62005-12-06 19:39:56 +0000752RETSIGTYPE cancel_command(int signal)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000753{
754 if (kill(pid, SIGKILL) == -1)
755 nperror("kill");
756}
757
David Lawrence Ramsey0ed71712005-11-08 23:09:47 +0000758/* Execute command in a shell. Return TRUE on success. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000759bool execute_command(const char *command)
760{
761 int fd[2];
762 FILE *f;
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000763 char *shellenv;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000764 struct sigaction oldaction, newaction;
765 /* Original and temporary handlers for SIGINT. */
766 bool sig_failed = FALSE;
767 /* Did sigaction() fail without changing the signal handlers? */
768
769 /* Make our pipes. */
770 if (pipe(fd) == -1) {
771 statusbar(_("Could not pipe"));
772 return FALSE;
773 }
774
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000775 /* Check $SHELL for the shell to use. If it isn't set, use
David Lawrence Ramsey1932dfb2005-11-29 05:52:49 +0000776 * /bin/sh. Note that $SHELL should contain only a path, with no
777 * arguments. */
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000778 shellenv = getenv("SHELL");
779 if (shellenv == NULL)
Chris Allegretta5a018f02009-11-29 06:13:22 +0000780 shellenv = (char *) "/bin/sh";
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000781
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000782 /* Fork a child. */
783 if ((pid = fork()) == 0) {
784 close(fd[0]);
785 dup2(fd[1], fileno(stdout));
786 dup2(fd[1], fileno(stderr));
787
788 /* If execl() returns at all, there was an error. */
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000789 execl(shellenv, tail(shellenv), "-c", command, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000790 exit(0);
791 }
792
David Lawrence Ramseyc838a4c2006-04-26 18:33:50 +0000793 /* Continue as parent. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000794 close(fd[1]);
795
796 if (pid == -1) {
797 close(fd[0]);
798 statusbar(_("Could not fork"));
799 return FALSE;
800 }
801
802 /* Before we start reading the forked command's output, we set
803 * things up so that Ctrl-C will cancel the new process. */
804
805 /* Enable interpretation of the special control keys so that we get
806 * SIGINT when Ctrl-C is pressed. */
807 enable_signals();
808
809 if (sigaction(SIGINT, NULL, &newaction) == -1) {
810 sig_failed = TRUE;
811 nperror("sigaction");
812 } else {
813 newaction.sa_handler = cancel_command;
814 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
815 sig_failed = TRUE;
816 nperror("sigaction");
817 }
818 }
819
820 /* Note that now oldaction is the previous SIGINT signal handler,
821 * to be restored later. */
822
823 f = fdopen(fd[0], "rb");
824 if (f == NULL)
825 nperror("fdopen");
826
Chris Allegretta2c7b5062009-12-09 16:51:43 +0000827 read_file(f, 0, "stdin", TRUE, FALSE);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000828
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000829 if (wait(NULL) == -1)
830 nperror("wait");
831
832 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
833 nperror("sigaction");
834
David Lawrence Ramsey8b9c91b2007-12-18 01:28:53 +0000835 /* Restore the terminal to its previous state. In the process,
836 * disable interpretation of the special control keys so that we can
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000837 * use Ctrl-C for other things. */
David Lawrence Ramsey8b9c91b2007-12-18 01:28:53 +0000838 terminal_init();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000839
840 return TRUE;
841}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000842
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000843/* Add a new undo struct to the top of the current pile */
Chris Allegretta14c86202008-08-03 04:48:05 +0000844void add_undo(undo_type current_action)
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000845{
Chris Allegretta4e12cb82008-10-14 19:55:34 +0000846 undo *u;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000847 char *data;
Chris Allegretta14c86202008-08-03 04:48:05 +0000848 openfilestruct *fs = openfile;
Chris Allegretta5c1c1432008-10-04 11:10:11 +0000849 static undo *last_cutu = NULL; /* Last thing we cut to set up the undo for uncut */
Chris Allegretta12ba5572009-03-26 01:01:48 +0000850 ssize_t wrap_loc; /* For calculating split beginning */
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000851
Chris Allegrettaa48507d2009-08-14 03:18:29 +0000852 if (!ISSET(UNDOABLE))
Chris Allegrettaad37e672009-07-12 03:36:58 +0000853 return;
854
Chris Allegretta8f761122008-08-01 06:52:15 +0000855 /* Ugh, if we were called while cutting not-to-end, non-marked and on the same lineno,
856 we need to abort here */
857 u = fs->current_undo;
Chris Allegretta80ea9c52008-08-09 10:08:33 +0000858 if (current_action == CUT && u && u->type == CUT
859 && !u->mark_set && u->lineno == fs->current->lineno)
Chris Allegretta8f761122008-08-01 06:52:15 +0000860 return;
861
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000862 /* Blow away the old undo stack if we are starting from the middle */
Chris Allegretta91a18622008-08-01 03:50:20 +0000863 while (fs->undotop != NULL && fs->undotop != fs->current_undo) {
864 undo *u2 = fs->undotop;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000865 fs->undotop = fs->undotop->next;
Chris Allegretta91a18622008-08-01 03:50:20 +0000866 if (u2->strdata != NULL)
867 free(u2->strdata);
Chris Allegretta6f681c12008-08-08 03:02:03 +0000868 if (u2->cutbuffer)
Chris Allegrettaea577872008-08-03 20:19:42 +0000869 free_filestruct(u2->cutbuffer);
Chris Allegretta91a18622008-08-01 03:50:20 +0000870 free(u2);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000871 }
872
Chris Allegretta8f761122008-08-01 06:52:15 +0000873 /* Allocate and initialize a new undo type */
Chris Allegretta5a018f02009-11-29 06:13:22 +0000874 u = (undo *) nmalloc(sizeof(undo));
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000875 u->type = current_action;
876 u->lineno = fs->current->lineno;
877 u->begin = fs->current_x;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000878 u->next = fs->undotop;
879 fs->undotop = u;
880 fs->current_undo = u;
Chris Allegretta91a18622008-08-01 03:50:20 +0000881 u->strdata = NULL;
Chris Allegrettadf543e72009-04-12 06:13:16 +0000882 u->strdata2 = NULL;
Chris Allegretta91a18622008-08-01 03:50:20 +0000883 u->cutbuffer = NULL;
884 u->cutbottom = NULL;
Chris Allegrettab549f372008-09-16 21:35:19 +0000885 u->mark_set = 0;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000886 u->mark_begin_lineno = 0;
887 u->mark_begin_x = 0;
Chris Allegretta91a18622008-08-01 03:50:20 +0000888 u->xflags = 0;
Chris Allegrettac81cf522008-10-14 04:34:56 +0000889 u->to_end = FALSE;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000890
891 switch (u->type) {
892 /* We need to start copying data into the undo buffer or we wont be able
893 to restore it later */
894 case ADD:
895 data = charalloc(2);
896 data[0] = fs->current->data[fs->current_x];
897 data[1] = '\0';
898 u->strdata = data;
899 break;
900 case DEL:
901 if (u->begin != strlen(fs->current->data)) {
902 data = mallocstrncpy(NULL, &fs->current->data[u->begin], 2);
903 data[1] = '\0';
904 u->strdata = data;
905 break;
906 }
907 /* Else purposely fall into unsplit code */
908 current_action = u->type = UNSPLIT;
909 case UNSPLIT:
910 if (fs->current->next) {
911 data = mallocstrcpy(NULL, fs->current->next->data);
912 u->strdata = data;
913 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000914 break;
Chris Allegrettac9f07992009-12-03 03:12:00 +0000915#ifndef DISABLE_WRAPPING
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000916 case SPLIT:
Chris Allegretta12ba5572009-03-26 01:01:48 +0000917 wrap_loc = break_line(openfile->current->data, fill
918#ifndef DISABLE_HELP
919 , FALSE
920#endif
921 );
922 u->strdata = mallocstrcpy(NULL, &openfile->current->data[wrap_loc]);
Chris Allegrettadf543e72009-04-12 06:13:16 +0000923 /* Don't both saving the next line if we're not prepending as a new line
924 will be created */
925 if (prepend_wrap)
926 u->strdata2 = mallocstrcpy(NULL, fs->current->next->data);
Chris Allegretta8b9fb362009-04-29 04:43:58 +0000927 u->begin = wrap_loc;
Chris Allegretta12ba5572009-03-26 01:01:48 +0000928 break;
Chris Allegrettac9f07992009-12-03 03:12:00 +0000929#endif /* DISABLE_WRAPPING */
Chris Allegretta12ba5572009-03-26 01:01:48 +0000930 case INSERT:
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000931 case REPLACE:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000932 data = mallocstrcpy(NULL, fs->current->data);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000933 u->strdata = data;
934 break;
935 case CUT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000936 u->mark_set = openfile->mark_set;
937 if (u->mark_set) {
938 u->mark_begin_lineno = openfile->mark_begin->lineno;
939 u->mark_begin_x = openfile->mark_begin_x;
940 }
Chris Allegrettac84e7652008-10-14 01:14:12 +0000941 u->to_end = (ISSET(CUT_TO_END)) ? TRUE : FALSE;
Chris Allegretta5c1c1432008-10-04 11:10:11 +0000942 last_cutu = u;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000943 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000944 case UNCUT:
Chris Allegretta5c1c1432008-10-04 11:10:11 +0000945 if (!last_cutu)
946 statusbar(_("Internal error: can't setup uncut. Please save your work."));
947 else if (last_cutu->type == CUT) {
948 u->cutbuffer = last_cutu->cutbuffer;
949 u->cutbottom = last_cutu->cutbottom;
Chris Allegretta5c1c1432008-10-04 11:10:11 +0000950 }
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000951 break;
Chris Allegrettab843a512009-04-21 05:34:08 +0000952 case ENTER:
953 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000954 case OTHER:
Chris Allegretta77bf1b52008-08-21 04:21:06 +0000955 statusbar(_("Internal error: unknown type. Please save your work."));
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000956 break;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000957 }
958
959#ifdef DEBUG
Chris Allegretta6f083322009-11-11 06:00:33 +0000960 fprintf(stderr, "fs->current->data = \"%s\", current_x = %lu, u->begin = %d, type = %d\n",
961 fs->current->data, (unsigned long) fs->current_x, u->begin, current_action);
Chris Allegretta12ba5572009-03-26 01:01:48 +0000962 fprintf(stderr, "left add_undo...\n");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000963#endif
964 fs->last_action = current_action;
965}
966
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000967/* Update an undo item, or determine whether a new one
968 is really needed and bounce the data to add_undo
969 instead. The latter functionality just feels
970 gimmicky and may just be more hassle than
971 it's worth, so it should be axed if needed. */
Chris Allegretta14c86202008-08-03 04:48:05 +0000972void update_undo(undo_type action)
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000973{
974 undo *u;
975 char *data;
976 int len = 0;
Chris Allegretta14c86202008-08-03 04:48:05 +0000977 openfilestruct *fs = openfile;
Chris Allegretta91a18622008-08-01 03:50:20 +0000978
Chris Allegrettaa48507d2009-08-14 03:18:29 +0000979 if (!ISSET(UNDOABLE))
Chris Allegrettaad37e672009-07-12 03:36:58 +0000980 return;
981
Chris Allegretta91a18622008-08-01 03:50:20 +0000982#ifdef DEBUG
Chris Allegretta6f083322009-11-11 06:00:33 +0000983 fprintf(stderr, "action = %d, fs->last_action = %d, openfile->current->lineno = %lu",
984 action, fs->last_action, (unsigned long) openfile->current->lineno);
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000985 if (fs->current_undo)
Chris Allegretta6f083322009-11-11 06:00:33 +0000986 fprintf(stderr, "fs->current_undo->lineno = %lu\n", (unsigned long) fs->current_undo->lineno);
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000987 else
988 fprintf(stderr, "\n");
Chris Allegretta91a18622008-08-01 03:50:20 +0000989#endif
990
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000991 /* Change to an add if we're not using the same undo struct
992 that we should be using */
993 if (action != fs->last_action
Chris Allegrettaa4c2b992009-04-29 22:34:27 +0000994 || (action != CUT && action != INSERT && action != SPLIT
Chris Allegretta91a18622008-08-01 03:50:20 +0000995 && openfile->current->lineno != fs->current_undo->lineno)) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000996 add_undo(action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000997 return;
998 }
999
1000 assert(fs->undotop != NULL);
1001 u = fs->undotop;
1002
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001003 switch (u->type) {
1004 case ADD:
1005#ifdef DEBUG
Chris Allegretta6f083322009-11-11 06:00:33 +00001006 fprintf(stderr, "fs->current->data = \"%s\", current_x = %lu, u->begin = %d\n",
1007 fs->current->data, (unsigned long) fs->current_x, u->begin);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001008#endif
1009 len = strlen(u->strdata) + 2;
Chris Allegretta5a018f02009-11-29 06:13:22 +00001010 data = (char *) nrealloc((void *) u->strdata, len * sizeof(char *));
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001011 data[len-2] = fs->current->data[fs->current_x];
1012 data[len-1] = '\0';
1013 u->strdata = (char *) data;
1014#ifdef DEBUG
1015 fprintf(stderr, "current undo data now \"%s\"\n", u->strdata);
1016#endif
1017 break;
1018 case DEL:
1019 len = strlen(u->strdata) + 2;
1020 assert(len > 2);
1021 if (fs->current_x == u->begin) {
1022 /* They're deleting */
Chris Allegretta0b499d42008-07-14 07:18:22 +00001023 if (!u->xflags)
Chris Allegretta637daa82011-02-07 14:45:56 +00001024 u->xflags = UNdel_del;
1025 else if (u->xflags != UNdel_del) {
Chris Allegretta14c86202008-08-03 04:48:05 +00001026 add_undo(action);
Chris Allegretta0b499d42008-07-14 07:18:22 +00001027 return;
1028 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001029 data = charalloc(len);
1030 strcpy(data, u->strdata);
1031 data[len-2] = fs->current->data[fs->current_x];;
1032 data[len-1] = '\0';
1033 free(u->strdata);
1034 u->strdata = data;
1035 } else if (fs->current_x == u->begin - 1) {
1036 /* They're backspacing */
Chris Allegretta0b499d42008-07-14 07:18:22 +00001037 if (!u->xflags)
Chris Allegretta637daa82011-02-07 14:45:56 +00001038 u->xflags = UNdel_backspace;
1039 else if (u->xflags != UNdel_backspace) {
Chris Allegretta14c86202008-08-03 04:48:05 +00001040 add_undo(action);
Chris Allegretta0b499d42008-07-14 07:18:22 +00001041 return;
1042 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001043 data = charalloc(len);
1044 data[0] = fs->current->data[fs->current_x];
1045 strcpy(&data[1], u->strdata);
1046 free(u->strdata);
1047 u->strdata = data;
1048 u->begin--;
1049 } else {
1050 /* They deleted something else on the line */
Chris Allegretta14c86202008-08-03 04:48:05 +00001051 add_undo(DEL);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001052 return;
1053 }
1054#ifdef DEBUG
Chris Allegretta0b499d42008-07-14 07:18:22 +00001055 fprintf(stderr, "current undo data now \"%s\"\nu->begin = %d\n", u->strdata, u->begin);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001056#endif
1057 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +00001058 case CUT:
Chris Allegrettac84e7652008-10-14 01:14:12 +00001059 if (!cutbuffer)
1060 break;
Chris Allegretta8f761122008-08-01 06:52:15 +00001061 if (u->cutbuffer)
1062 free(u->cutbuffer);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +00001063 u->cutbuffer = copy_filestruct(cutbuffer);
Chris Allegrettac81cf522008-10-14 04:34:56 +00001064 /* Compute cutbottom for the uncut using out copy */
1065 for (u->cutbottom = u->cutbuffer; u->cutbottom->next != NULL; u->cutbottom = u->cutbottom->next)
1066 ;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +00001067 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +00001068 case REPLACE:
Chris Allegrettab549f372008-09-16 21:35:19 +00001069 case UNCUT:
Chris Allegretta14c86202008-08-03 04:48:05 +00001070 add_undo(action);
Chris Allegretta3c1131a2008-08-02 22:31:01 +00001071 break;
Chris Allegretta14c86202008-08-03 04:48:05 +00001072 case INSERT:
1073 u->mark_begin_lineno = openfile->current->lineno;
Chris Allegretta12ba5572009-03-26 01:01:48 +00001074 break;
Chris Allegrettac9f07992009-12-03 03:12:00 +00001075#ifndef DISABLE_WRAPPING
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001076 case SPLIT:
Chris Allegretta12ba5572009-03-26 01:01:48 +00001077 /* This will only be called if we made a completely new line,
1078 and as such we should note that so we can destroy it later */
Chris Allegretta637daa82011-02-07 14:45:56 +00001079 u->xflags = UNsplit_madenew;
Chris Allegretta12ba5572009-03-26 01:01:48 +00001080 break;
Chris Allegrettac9f07992009-12-03 03:12:00 +00001081#endif /* DISABLE_WRAPPING */
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001082 case UNSPLIT:
Chris Allegretta3c1131a2008-08-02 22:31:01 +00001083 /* These cases are handled by the earlier check for a new line and action */
Chris Allegrettaad37e672009-07-12 03:36:58 +00001084 case ENTER:
Chris Allegretta3c1131a2008-08-02 22:31:01 +00001085 case OTHER:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001086 break;
1087 }
1088
1089#ifdef DEBUG
1090 fprintf(stderr, "Done in udpate_undo (type was %d)\n", action);
1091#endif
1092 if (fs->last_action != action) {
1093#ifdef DEBUG
1094 fprintf(stderr, "Starting add_undo for new action as it does not match last_action\n");
1095#endif
Chris Allegretta14c86202008-08-03 04:48:05 +00001096 add_undo(action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001097 }
1098 fs->last_action = action;
1099}
1100
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001101#endif /* !NANO_TINY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001102
1103#ifndef DISABLE_WRAPPING
David Lawrence Ramseyef0d5a72006-05-22 02:08:49 +00001104/* Unset the prepend_wrap flag. We need to do this as soon as we do
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001105 * something other than type text. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001106void wrap_reset(void)
1107{
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001108 prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001109}
1110
1111/* We wrap the given line. Precondition: we assume the cursor has been
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001112 * moved forward since the last typed character. Return TRUE if we
1113 * wrapped, and FALSE otherwise. */
Chris Allegrettaa4c2b992009-04-29 22:34:27 +00001114bool do_wrap(filestruct *line, bool undoing)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001115{
1116 size_t line_len;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001117 /* The length of the line we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001118 ssize_t wrap_loc;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001119 /* The index of line->data where we wrap. */
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001120#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001121 const char *indent_string = NULL;
1122 /* Indentation to prepend to the new line. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001123 size_t indent_len = 0;
1124 /* The length of indent_string. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001125#endif
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001126 const char *after_break;
1127 /* The text after the wrap point. */
1128 size_t after_break_len;
1129 /* The length of after_break. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001130 bool prepending = FALSE;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001131 /* Do we prepend to the next line? */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001132 const char *next_line = NULL;
1133 /* The next line, minus indentation. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001134 size_t next_line_len = 0;
1135 /* The length of next_line. */
1136 char *new_line = NULL;
1137 /* The line we create. */
1138 size_t new_line_len = 0;
1139 /* The eventual length of new_line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001140
1141 /* There are three steps. First, we decide where to wrap. Then, we
1142 * create the new wrap line. Finally, we clean up. */
1143
1144 /* Step 1, finding where to wrap. We are going to add a new line
1145 * after a blank character. In this step, we call break_line() to
1146 * get the location of the last blank we can break the line at, and
David Lawrence Ramseyd4686b82006-06-11 19:14:14 +00001147 * set wrap_loc to the location of the character after it, so that
1148 * the blank is preserved at the end of the line.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001149 *
1150 * If there is no legal wrap point, or we reach the last character
1151 * of the line while trying to find one, we should return without
1152 * wrapping. Note that if autoindent is turned on, we don't break
1153 * at the end of it! */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001154 assert(line != NULL && line->data != NULL);
1155
1156 /* Save the length of the line. */
1157 line_len = strlen(line->data);
1158
1159 /* Find the last blank where we can break the line. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001160 wrap_loc = break_line(line->data, fill
1161#ifndef DISABLE_HELP
1162 , FALSE
1163#endif
1164 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001165
1166 /* If we couldn't break the line, or we've reached the end of it, we
1167 * don't wrap. */
1168 if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
1169 return FALSE;
1170
1171 /* Otherwise, move forward to the character just after the blank. */
1172 wrap_loc += move_mbright(line->data + wrap_loc, 0);
1173
1174 /* If we've reached the end of the line, we don't wrap. */
1175 if (line->data[wrap_loc] == '\0')
1176 return FALSE;
1177
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001178#ifndef NANO_TINY
Chris Allegrettaa4c2b992009-04-29 22:34:27 +00001179 if (!undoing)
1180 add_undo(SPLIT);
Chris Allegretta12ba5572009-03-26 01:01:48 +00001181
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001182 /* If autoindent is turned on, and we're on the character just after
1183 * the indentation, we don't wrap. */
1184 if (ISSET(AUTOINDENT)) {
1185 /* Get the indentation of this line. */
1186 indent_string = line->data;
1187 indent_len = indent_length(indent_string);
1188
1189 if (wrap_loc == indent_len)
1190 return FALSE;
1191 }
1192#endif
1193
1194 /* Step 2, making the new wrap line. It will consist of indentation
1195 * followed by the text after the wrap point, optionally followed by
1196 * a space (if the text after the wrap point doesn't end in a blank)
David Lawrence Ramsey03979d72006-06-11 19:15:59 +00001197 * and the text of the next line, if they can fit without wrapping,
1198 * the next line exists, and the prepend_wrap flag is set. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001199
1200 /* after_break is the text that will be wrapped to the next line. */
1201 after_break = line->data + wrap_loc;
1202 after_break_len = line_len - wrap_loc;
1203
1204 assert(strlen(after_break) == after_break_len);
1205
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001206 /* We prepend the wrapped text to the next line, if the prepend_wrap
1207 * flag is set, there is a next line, and prepending would not make
1208 * the line too long. */
1209 if (prepend_wrap && line != openfile->filebot) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001210 const char *end = after_break + move_mbleft(after_break,
1211 after_break_len);
1212
1213 /* If after_break doesn't end in a blank, make sure it ends in a
1214 * space. */
1215 if (!is_blank_mbchar(end)) {
1216 line_len++;
1217 line->data = charealloc(line->data, line_len + 1);
1218 line->data[line_len - 1] = ' ';
1219 line->data[line_len] = '\0';
1220 after_break = line->data + wrap_loc;
1221 after_break_len++;
1222 openfile->totsize++;
1223 }
1224
1225 next_line = line->next->data;
1226 next_line_len = strlen(next_line);
1227
1228 if (after_break_len + next_line_len <= fill) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001229 prepending = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001230 new_line_len += next_line_len;
1231 }
1232 }
1233
1234 /* new_line_len is now the length of the text that will be wrapped
1235 * to the next line, plus (if we're prepending to it) the length of
1236 * the text of the next line. */
1237 new_line_len += after_break_len;
1238
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001239#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001240 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001241 if (prepending) {
1242 /* If we're prepending, the indentation will come from the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001243 * next line. */
1244 indent_string = next_line;
1245 indent_len = indent_length(indent_string);
1246 next_line += indent_len;
1247 } else {
1248 /* Otherwise, it will come from this line, in which case
1249 * we should increase new_line_len to make room for it. */
1250 new_line_len += indent_len;
1251 openfile->totsize += mbstrnlen(indent_string, indent_len);
1252 }
1253 }
1254#endif
1255
1256 /* Now we allocate the new line and copy the text into it. */
1257 new_line = charalloc(new_line_len + 1);
1258 new_line[0] = '\0';
1259
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001260#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001261 if (ISSET(AUTOINDENT)) {
1262 /* Copy the indentation. */
1263 strncpy(new_line, indent_string, indent_len);
1264 new_line[indent_len] = '\0';
1265 new_line_len += indent_len;
1266 }
1267#endif
1268
1269 /* Copy all the text after the wrap point of the current line. */
1270 strcat(new_line, after_break);
1271
1272 /* Break the current line at the wrap point. */
1273 null_at(&line->data, wrap_loc);
1274
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001275 if (prepending) {
Chris Allegrettaa4c2b992009-04-29 22:34:27 +00001276 if (!undoing)
1277 update_undo(SPLIT);
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001278 /* If we're prepending, copy the text from the next line, minus
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001279 * the indentation that we already copied above. */
1280 strcat(new_line, next_line);
1281
1282 free(line->next->data);
1283 line->next->data = new_line;
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001284
1285 /* If the NO_NEWLINES flag isn't set, and text has been added to
1286 * the magicline, make a new magicline. */
1287 if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
1288 new_magicline();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001289 } else {
1290 /* Otherwise, make a new line and copy the text after where we
1291 * broke this line to the beginning of the new line. */
1292 splice_node(openfile->current, make_new_node(openfile->current),
1293 openfile->current->next);
1294
David Lawrence Ramsey219a8142005-11-22 22:08:01 +00001295 /* If the current line is the last line of the file, move the
1296 * last line of the file down to the next line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001297 if (openfile->filebot == openfile->current)
1298 openfile->filebot = openfile->current->next;
1299
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001300 openfile->current->next->data = new_line;
1301
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001302 openfile->totsize++;
1303 }
1304
1305 /* Step 3, clean up. Reposition the cursor and mark, and do some
1306 * other sundry things. */
1307
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001308 /* Set the prepend_wrap flag, so that later wraps of this line will
1309 * be prepended to the next line. */
1310 prepend_wrap = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001311
David Lawrence Ramsey2829aae2006-05-22 01:24:09 +00001312 /* Each line knows its number. We recalculate these if we inserted
1313 * a new line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001314 if (!prepending)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001315 renumber(line);
1316
1317 /* If the cursor was after the break point, we must move it. We
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001318 * also clear the prepend_wrap flag in this case. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001319 if (openfile->current_x > wrap_loc) {
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001320 prepend_wrap = FALSE;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001321
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001322 openfile->current = openfile->current->next;
1323 openfile->current_x -= wrap_loc
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001324#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001325 - indent_len
1326#endif
1327 ;
1328 openfile->placewewant = xplustabs();
1329 }
1330
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001331#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001332 /* If the mark was on this line after the wrap point, we move it
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001333 * down. If it was on the next line and we prepended to that line,
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001334 * we move it right. */
1335 if (openfile->mark_set) {
1336 if (openfile->mark_begin == line && openfile->mark_begin_x >
1337 wrap_loc) {
1338 openfile->mark_begin = line->next;
1339 openfile->mark_begin_x -= wrap_loc - indent_len + 1;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001340 } else if (prepending && openfile->mark_begin == line->next)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001341 openfile->mark_begin_x += after_break_len;
1342 }
1343#endif
1344
1345 return TRUE;
1346}
1347#endif /* !DISABLE_WRAPPING */
1348
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +00001349#if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001350/* We are trying to break a chunk off line. We find the last blank such
David Lawrence Ramseycd9a5f02005-09-20 06:12:54 +00001351 * that the display length to there is at most (goal + 1). If there is
1352 * no such blank, then we find the first blank. We then take the last
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001353 * blank in that group of blanks. The terminating '\0' counts as a
1354 * blank, as does a '\n' if newline is TRUE. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001355ssize_t break_line(const char *line, ssize_t goal
1356#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001357 , bool newln
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001358#endif
1359 )
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001360{
1361 ssize_t blank_loc = -1;
1362 /* Current tentative return value. Index of the last blank we
1363 * found with short enough display width. */
1364 ssize_t cur_loc = 0;
1365 /* Current index in line. */
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001366 size_t cur_pos = 0;
1367 /* Current column position in line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001368 int line_len;
1369
1370 assert(line != NULL);
1371
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001372 while (*line != '\0' && goal >= cur_pos) {
1373 line_len = parse_mbchar(line, NULL, &cur_pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001374
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001375 if (is_blank_mbchar(line)
1376#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001377 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001378#endif
1379 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001380 blank_loc = cur_loc;
1381
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001382#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001383 if (newln && *line == '\n')
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001384 break;
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001385#endif
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001386 }
1387
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001388 line += line_len;
1389 cur_loc += line_len;
1390 }
1391
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001392 if (goal >= cur_pos)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001393 /* In fact, the whole line displays shorter than goal. */
1394 return cur_loc;
1395
Chris Allegretta09b81242008-07-12 01:54:49 +00001396#ifndef DISABLE_HELP
1397 if (newln && blank_loc <= 0) {
1398 /* If blank was not found or was found only first character,
1399 * force line break. */
1400 cur_loc -= line_len;
1401 return cur_loc;
1402 }
1403#endif
1404
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001405 if (blank_loc == -1) {
1406 /* No blank was found that was short enough. */
1407 bool found_blank = FALSE;
David Lawrence Ramsey5ab12ca2005-09-20 17:52:52 +00001408 ssize_t found_blank_loc = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001409
1410 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001411 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001412
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001413 if (is_blank_mbchar(line)
1414#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001415 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001416#endif
1417 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001418 if (!found_blank)
1419 found_blank = TRUE;
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +00001420 found_blank_loc = cur_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001421 } else if (found_blank)
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +00001422 return found_blank_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001423
1424 line += line_len;
1425 cur_loc += line_len;
1426 }
1427
1428 return -1;
1429 }
1430
1431 /* Move to the last blank after blank_loc, if there is one. */
1432 line -= cur_loc;
1433 line += blank_loc;
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001434 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001435 line += line_len;
1436
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001437 while (*line != '\0' && (is_blank_mbchar(line)
1438#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001439 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001440#endif
1441 )) {
David Lawrence Ramseycd243f52006-05-19 23:27:16 +00001442#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001443 if (newln && *line == '\n')
David Lawrence Ramseycd243f52006-05-19 23:27:16 +00001444 break;
1445#endif
1446
David Lawrence Ramsey39bd1b32006-05-20 13:11:56 +00001447 line_len = parse_mbchar(line, NULL, NULL);
1448
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001449 line += line_len;
1450 blank_loc += line_len;
1451 }
1452
1453 return blank_loc;
1454}
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +00001455#endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001456
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001457#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001458/* The "indentation" of a line is the whitespace between the quote part
1459 * and the non-whitespace of the line. */
1460size_t indent_length(const char *line)
1461{
1462 size_t len = 0;
1463 char *blank_mb;
1464 int blank_mb_len;
1465
1466 assert(line != NULL);
1467
1468 blank_mb = charalloc(mb_cur_max());
1469
1470 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001471 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001472
1473 if (!is_blank_mbchar(blank_mb))
1474 break;
1475
1476 line += blank_mb_len;
1477 len += blank_mb_len;
1478 }
1479
1480 free(blank_mb);
1481
1482 return len;
1483}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001484#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001485
1486#ifndef DISABLE_JUSTIFY
1487/* justify_format() replaces blanks with spaces and multiple spaces by 1
1488 * (except it maintains up to 2 after a character in punct optionally
1489 * followed by a character in brackets, and removes all from the end).
1490 *
1491 * justify_format() might make paragraph->data shorter, and change the
1492 * actual pointer with null_at().
1493 *
1494 * justify_format() will not look at the first skip characters of
1495 * paragraph. skip should be at most strlen(paragraph->data). The
1496 * character at paragraph[skip + 1] must not be blank. */
1497void justify_format(filestruct *paragraph, size_t skip)
1498{
1499 char *end, *new_end, *new_paragraph_data;
1500 size_t shift = 0;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001501#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001502 size_t mark_shift = 0;
1503#endif
1504
1505 /* These four asserts are assumptions about the input data. */
1506 assert(paragraph != NULL);
1507 assert(paragraph->data != NULL);
1508 assert(skip < strlen(paragraph->data));
1509 assert(!is_blank_mbchar(paragraph->data + skip));
1510
1511 end = paragraph->data + skip;
1512 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
1513 strncpy(new_paragraph_data, paragraph->data, skip);
1514 new_end = new_paragraph_data + skip;
1515
1516 while (*end != '\0') {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001517 int end_len;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001518
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001519 /* If this character is blank, change it to a space if
1520 * necessary, and skip over all blanks after it. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001521 if (is_blank_mbchar(end)) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001522 end_len = parse_mbchar(end, NULL, NULL);
1523
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001524 *new_end = ' ';
1525 new_end++;
1526 end += end_len;
1527
1528 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001529 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001530
1531 end += end_len;
1532 shift += end_len;
1533
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001534#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001535 /* Keep track of the change in the current line. */
1536 if (openfile->mark_set && openfile->mark_begin ==
1537 paragraph && openfile->mark_begin_x >= end -
1538 paragraph->data)
1539 mark_shift += end_len;
1540#endif
1541 }
1542 /* If this character is punctuation optionally followed by a
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001543 * bracket and then followed by blanks, change no more than two
1544 * of the blanks to spaces if necessary, and skip over all
1545 * blanks after them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001546 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001547 end_len = parse_mbchar(end, NULL, NULL);
1548
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001549 while (end_len > 0) {
1550 *new_end = *end;
1551 new_end++;
1552 end++;
1553 end_len--;
1554 }
1555
1556 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001557 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001558
1559 while (end_len > 0) {
1560 *new_end = *end;
1561 new_end++;
1562 end++;
1563 end_len--;
1564 }
1565 }
1566
1567 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001568 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001569
1570 *new_end = ' ';
1571 new_end++;
1572 end += end_len;
1573 }
1574
1575 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001576 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001577
1578 *new_end = ' ';
1579 new_end++;
1580 end += end_len;
1581 }
1582
1583 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001584 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001585
1586 end += end_len;
1587 shift += end_len;
1588
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001589#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001590 /* Keep track of the change in the current line. */
1591 if (openfile->mark_set && openfile->mark_begin ==
1592 paragraph && openfile->mark_begin_x >= end -
1593 paragraph->data)
1594 mark_shift += end_len;
1595#endif
1596 }
1597 /* If this character is neither blank nor punctuation, leave it
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001598 * unchanged. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001599 } else {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001600 end_len = parse_mbchar(end, NULL, NULL);
1601
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001602 while (end_len > 0) {
1603 *new_end = *end;
1604 new_end++;
1605 end++;
1606 end_len--;
1607 }
1608 }
1609 }
1610
1611 assert(*end == '\0');
1612
1613 *new_end = *end;
1614
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001615 /* If there are spaces at the end of the line, remove them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001616 while (new_end > new_paragraph_data + skip &&
1617 *(new_end - 1) == ' ') {
1618 new_end--;
1619 shift++;
1620 }
1621
1622 if (shift > 0) {
1623 openfile->totsize -= shift;
1624 null_at(&new_paragraph_data, new_end - new_paragraph_data);
1625 free(paragraph->data);
1626 paragraph->data = new_paragraph_data;
1627
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001628#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001629 /* Adjust the mark coordinates to compensate for the change in
1630 * the current line. */
1631 if (openfile->mark_set && openfile->mark_begin == paragraph) {
1632 openfile->mark_begin_x -= mark_shift;
1633 if (openfile->mark_begin_x > new_end - new_paragraph_data)
1634 openfile->mark_begin_x = new_end - new_paragraph_data;
1635 }
1636#endif
1637 } else
1638 free(new_paragraph_data);
1639}
1640
1641/* The "quote part" of a line is the largest initial substring matching
1642 * the quote string. This function returns the length of the quote part
1643 * of the given line.
1644 *
1645 * Note that if !HAVE_REGEX_H then we match concatenated copies of
1646 * quotestr. */
1647size_t quote_length(const char *line)
1648{
1649#ifdef HAVE_REGEX_H
1650 regmatch_t matches;
1651 int rc = regexec(&quotereg, line, 1, &matches, 0);
1652
1653 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
1654 return 0;
1655 /* matches.rm_so should be 0, since the quote string should start
1656 * with the caret ^. */
1657 return matches.rm_eo;
1658#else /* !HAVE_REGEX_H */
1659 size_t qdepth = 0;
1660
1661 /* Compute quote depth level. */
1662 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
1663 qdepth += quotelen;
1664 return qdepth;
1665#endif /* !HAVE_REGEX_H */
1666}
1667
1668/* a_line and b_line are lines of text. The quotation part of a_line is
1669 * the first a_quote characters. Check that the quotation part of
1670 * b_line is the same. */
1671bool quotes_match(const char *a_line, size_t a_quote, const char
1672 *b_line)
1673{
1674 /* Here is the assumption about a_quote. */
1675 assert(a_quote == quote_length(a_line));
1676
1677 return (a_quote == quote_length(b_line) &&
1678 strncmp(a_line, b_line, a_quote) == 0);
1679}
1680
1681/* We assume a_line and b_line have no quote part. Then, we return
1682 * whether b_line could follow a_line in a paragraph. */
1683bool indents_match(const char *a_line, size_t a_indent, const char
1684 *b_line, size_t b_indent)
1685{
1686 assert(a_indent == indent_length(a_line));
1687 assert(b_indent == indent_length(b_line));
1688
1689 return (b_indent <= a_indent &&
1690 strncmp(a_line, b_line, b_indent) == 0);
1691}
1692
1693/* Is foo the beginning of a paragraph?
1694 *
1695 * A line of text consists of a "quote part", followed by an
1696 * "indentation part", followed by text. The functions quote_length()
1697 * and indent_length() calculate these parts.
1698 *
1699 * A line is "part of a paragraph" if it has a part not in the quote
1700 * part or the indentation.
1701 *
1702 * A line is "the beginning of a paragraph" if it is part of a
1703 * paragraph and
1704 * 1) it is the top line of the file, or
1705 * 2) the line above it is not part of a paragraph, or
1706 * 3) the line above it does not have precisely the same quote
1707 * part, or
1708 * 4) the indentation of this line is not an initial substring of
1709 * the indentation of the previous line, or
1710 * 5) this line has no quote part and some indentation, and
1711 * autoindent isn't turned on.
1712 * The reason for number 5) is that if autoindent isn't turned on,
1713 * then an indented line is expected to start a paragraph, as in
1714 * books. Thus, nano can justify an indented paragraph only if
1715 * autoindent is turned on. */
1716bool begpar(const filestruct *const foo)
1717{
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001718 size_t quote_len, indent_len, temp_id_len;
1719
1720 if (foo == NULL)
1721 return FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001722
1723 /* Case 1). */
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001724 if (foo == openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001725 return TRUE;
1726
1727 quote_len = quote_length(foo->data);
1728 indent_len = indent_length(foo->data + quote_len);
1729
1730 /* Not part of a paragraph. */
1731 if (foo->data[quote_len + indent_len] == '\0')
1732 return FALSE;
1733
1734 /* Case 3). */
1735 if (!quotes_match(foo->data, quote_len, foo->prev->data))
1736 return TRUE;
1737
1738 temp_id_len = indent_length(foo->prev->data + quote_len);
1739
1740 /* Case 2) or 5) or 4). */
1741 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
1742 (quote_len == 0 && indent_len > 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001743#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001744 && !ISSET(AUTOINDENT)
1745#endif
1746 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
1747 foo->data + quote_len, indent_len))
1748 return TRUE;
1749
1750 return FALSE;
1751}
1752
1753/* Is foo inside a paragraph? */
1754bool inpar(const filestruct *const foo)
1755{
1756 size_t quote_len;
1757
1758 if (foo == NULL)
1759 return FALSE;
1760
1761 quote_len = quote_length(foo->data);
1762
David Lawrence Ramsey21014032005-11-09 20:33:42 +00001763 return (foo->data[quote_len + indent_length(foo->data +
1764 quote_len)] != '\0');
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001765}
1766
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001767/* Move the next par_len lines, starting with first_line, into the
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001768 * justify buffer, leaving copies of those lines in place. Assume that
1769 * par_len is greater than zero, and that there are enough lines after
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001770 * first_line. */
1771void backup_lines(filestruct *first_line, size_t par_len)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001772{
1773 filestruct *top = first_line;
1774 /* The top of the paragraph we're backing up. */
1775 filestruct *bot = first_line;
1776 /* The bottom of the paragraph we're backing up. */
1777 size_t i;
1778 /* Generic loop variable. */
1779 size_t current_x_save = openfile->current_x;
1780 ssize_t fl_lineno_save = first_line->lineno;
1781 ssize_t edittop_lineno_save = openfile->edittop->lineno;
1782 ssize_t current_lineno_save = openfile->current->lineno;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001783#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001784 bool old_mark_set = openfile->mark_set;
1785 ssize_t mb_lineno_save = 0;
1786 size_t mark_begin_x_save = 0;
1787
1788 if (old_mark_set) {
1789 mb_lineno_save = openfile->mark_begin->lineno;
1790 mark_begin_x_save = openfile->mark_begin_x;
1791 }
1792#endif
1793
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001794 /* par_len will be one greater than the number of lines between
1795 * current and filebot if filebot is the last line in the
1796 * paragraph. */
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001797 assert(par_len > 0 && openfile->current->lineno + par_len <=
David Lawrence Ramsey24777c02005-12-01 05:49:08 +00001798 openfile->filebot->lineno + 1);
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001799
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001800 /* Move bot down par_len lines to the line after the last line of
1801 * the paragraph, if there is one. */
1802 for (i = par_len; i > 0 && bot != openfile->filebot; i--)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001803 bot = bot->next;
1804
1805 /* Move the paragraph from the current buffer's filestruct to the
1806 * justify buffer. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001807 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
David Lawrence Ramseyf0575cf2005-11-09 23:27:51 +00001808 (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001809
1810 /* Copy the paragraph back to the current buffer's filestruct from
1811 * the justify buffer. */
1812 copy_from_filestruct(jusbuffer, jusbottom);
1813
1814 /* Move upward from the last line of the paragraph to the first
1815 * line, putting first_line, edittop, current, and mark_begin at the
1816 * same lines in the copied paragraph that they had in the original
1817 * paragraph. */
David Lawrence Ramseyee43ea62007-04-22 15:04:05 +00001818 if (openfile->current != openfile->fileage) {
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001819 top = openfile->current->prev;
David Lawrence Ramseyee43ea62007-04-22 15:04:05 +00001820#ifndef NANO_TINY
1821 if (old_mark_set &&
1822 openfile->current->lineno == mb_lineno_save) {
1823 openfile->mark_begin = openfile->current;
1824 openfile->mark_begin_x = mark_begin_x_save;
1825 }
1826#endif
1827 } else
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001828 top = openfile->current;
David Lawrence Ramseye8d505b2005-11-10 03:40:45 +00001829 for (i = par_len; i > 0 && top != NULL; i--) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001830 if (top->lineno == fl_lineno_save)
1831 first_line = top;
1832 if (top->lineno == edittop_lineno_save)
1833 openfile->edittop = top;
1834 if (top->lineno == current_lineno_save)
1835 openfile->current = top;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001836#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001837 if (old_mark_set && top->lineno == mb_lineno_save) {
1838 openfile->mark_begin = top;
1839 openfile->mark_begin_x = mark_begin_x_save;
1840 }
1841#endif
1842 top = top->prev;
1843 }
1844
1845 /* Put current_x at the same place in the copied paragraph that it
1846 * had in the original paragraph. */
1847 openfile->current_x = current_x_save;
1848
1849 set_modified();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001850}
1851
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001852/* Find the beginning of the current paragraph if we're in one, or the
1853 * beginning of the next paragraph if we're not. Afterwards, save the
1854 * quote length and paragraph length in *quote and *par. Return TRUE if
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001855 * we found a paragraph, and FALSE if there was an error or we didn't
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001856 * find a paragraph.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001857 *
1858 * See the comment at begpar() for more about when a line is the
1859 * beginning of a paragraph. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001860bool find_paragraph(size_t *const quote, size_t *const par)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001861{
1862 size_t quote_len;
1863 /* Length of the initial quotation of the paragraph we search
1864 * for. */
1865 size_t par_len;
1866 /* Number of lines in the paragraph we search for. */
1867 filestruct *current_save;
1868 /* The line at the beginning of the paragraph we search for. */
1869 ssize_t current_y_save;
1870 /* The y-coordinate at the beginning of the paragraph we search
1871 * for. */
1872
1873#ifdef HAVE_REGEX_H
1874 if (quoterc != 0) {
1875 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1876 return FALSE;
1877 }
1878#endif
1879
1880 assert(openfile->current != NULL);
1881
David Lawrence Ramsey1be131a2005-11-11 03:55:52 +00001882 /* If we're at the end of the last line of the file, it means that
1883 * there aren't any paragraphs left, so get out. */
1884 if (openfile->current == openfile->filebot && openfile->current_x ==
1885 strlen(openfile->filebot->data))
1886 return FALSE;
1887
1888 /* If the current line isn't in a paragraph, move forward to the
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001889 * last line of the next paragraph, if any. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001890 if (!inpar(openfile->current)) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001891 do_para_end(FALSE);
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001892
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001893 /* If we end up past the beginning of the line, it means that
1894 * we're at the end of the last line of the file, and the line
1895 * isn't blank, in which case the last line of the file is the
1896 * last line of the next paragraph.
1897 *
1898 * Otherwise, if we end up on a line that's in a paragraph, it
1899 * means that we're on the line after the last line of the next
1900 * paragraph, in which case we should move back to the last line
1901 * of the next paragraph. */
1902 if (openfile->current_x == 0) {
1903 if (!inpar(openfile->current->prev))
1904 return FALSE;
1905 if (openfile->current != openfile->fileage)
1906 openfile->current = openfile->current->prev;
1907 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001908 }
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001909
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001910 /* If the current line isn't the first line of the paragraph, move
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001911 * back to the first line of the paragraph. */
1912 if (!begpar(openfile->current))
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001913 do_para_begin(FALSE);
1914
1915 /* Now current is the first line of the paragraph. Set quote_len to
1916 * the quotation length of that line, and set par_len to the number
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001917 * of lines in this paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001918 quote_len = quote_length(openfile->current->data);
1919 current_save = openfile->current;
1920 current_y_save = openfile->current_y;
1921 do_para_end(FALSE);
1922 par_len = openfile->current->lineno - current_save->lineno;
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001923
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001924 /* If we end up past the beginning of the line, it means that we're
1925 * at the end of the last line of the file, and the line isn't
1926 * blank, in which case the last line of the file is part of the
1927 * paragraph. */
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001928 if (openfile->current_x > 0)
1929 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001930 openfile->current = current_save;
1931 openfile->current_y = current_y_save;
1932
1933 /* Save the values of quote_len and par_len. */
1934 assert(quote != NULL && par != NULL);
1935
1936 *quote = quote_len;
1937 *par = par_len;
1938
1939 return TRUE;
1940}
1941
1942/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1943 * the current paragraph. */
1944void do_justify(bool full_justify)
1945{
1946 filestruct *first_par_line = NULL;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001947 /* Will be the first line of the justified paragraph(s), if any.
1948 * For restoring after unjustify. */
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001949 filestruct *last_par_line = NULL;
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001950 /* Will be the line after the last line of the justified
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001951 * paragraph(s), if any. Also for restoring after unjustify. */
David Lawrence Ramsey82b5deb2005-11-10 06:07:57 +00001952 bool filebot_inpar = FALSE;
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001953 /* Whether the text at filebot is part of the current
1954 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001955
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001956 /* We save these variables to be restored if the user
1957 * unjustifies. */
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001958 filestruct *edittop_save = openfile->edittop;
1959 filestruct *current_save = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001960 size_t current_x_save = openfile->current_x;
1961 size_t pww_save = openfile->placewewant;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001962 size_t totsize_save = openfile->totsize;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001963#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001964 filestruct *mark_begin_save = openfile->mark_begin;
1965 size_t mark_begin_x_save = openfile->mark_begin_x;
1966#endif
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001967 bool modified_save = openfile->modified;
1968
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001969 int kbinput;
1970 bool meta_key, func_key, s_or_t, ran_func, finished;
Chris Allegretta0018d8e2008-03-13 08:23:52 +00001971 const sc *s;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001972
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001973 /* Move to the beginning of the current line, so that justifying at
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001974 * the end of the last line of the file, if that line isn't blank,
1975 * will work the first time through. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001976 openfile->current_x = 0;
1977
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001978 /* If we're justifying the entire file, start at the beginning. */
1979 if (full_justify)
1980 openfile->current = openfile->fileage;
1981
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001982 while (TRUE) {
1983 size_t i;
1984 /* Generic loop variable. */
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001985 filestruct *curr_first_par_line;
1986 /* The first line of the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001987 size_t quote_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001988 /* Length of the initial quotation of the current
1989 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001990 size_t indent_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001991 /* Length of the initial indentation of the current
1992 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001993 size_t par_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001994 /* Number of lines in the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001995 ssize_t break_pos;
1996 /* Where we will break lines. */
1997 char *indent_string;
1998 /* The first indentation that doesn't match the initial
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001999 * indentation of the current paragraph. This is put at the
2000 * beginning of every line broken off the first justified
2001 * line of the paragraph. Note that this works because a
2002 * paragraph can only contain two indentations at most: the
2003 * initial one, and a different one starting on a line after
2004 * the first. See the comment at begpar() for more about
2005 * when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002006
2007 /* Find the first line of the paragraph to be justified. That
2008 * is the start of this paragraph if we're in one, or the start
2009 * of the next otherwise. Save the quote length and paragraph
2010 * length (number of lines). Don't refresh the screen yet,
2011 * since we'll do that after we justify.
2012 *
2013 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00002014 * justifying the whole file, and we've found at least one
2015 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002016 * last line of the file, so set the last line of the text to be
2017 * justified to the last line of the file and break out of the
2018 * loop. Otherwise, it means that there are no paragraph(s) to
2019 * justify, so refresh the screen and get out. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00002020 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00002021 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002022 last_par_line = openfile->filebot;
2023 break;
2024 } else {
Chris Allegrettafd265af2009-02-06 03:41:02 +00002025 edit_refresh_needed = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002026 return;
2027 }
2028 }
2029
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00002030 /* par_len will be one greater than the number of lines between
2031 * current and filebot if filebot is the last line in the
2032 * paragraph. Set filebot_inpar to TRUE if this is the case. */
2033 filebot_inpar = (openfile->current->lineno + par_len ==
2034 openfile->filebot->lineno + 1);
2035
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00002036 /* If we haven't already done it, move the original paragraph(s)
2037 * to the justify buffer, splice a copy of the original
2038 * paragraph(s) into the file in the same place, and set
2039 * first_par_line to the first line of the copy. */
2040 if (first_par_line == NULL) {
2041 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00002042 openfile->filebot->lineno - openfile->current->lineno +
2043 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00002044 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00002045 first_par_line = openfile->current;
2046 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002047
David Lawrence Ramseya6854682005-11-30 21:19:42 +00002048 /* Set curr_first_par_line to the first line of the current
2049 * paragraph. */
2050 curr_first_par_line = openfile->current;
2051
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002052 /* Initialize indent_string to a blank string. */
2053 indent_string = mallocstrcpy(NULL, "");
2054
2055 /* Find the first indentation in the paragraph that doesn't
David Lawrence Ramsey8602fd62006-05-28 18:43:21 +00002056 * match the indentation of the first line, and save it in
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002057 * indent_string. If all the indentations are the same, save
2058 * the indentation of the first line in indent_string. */
2059 {
2060 const filestruct *indent_line = openfile->current;
2061 bool past_first_line = FALSE;
2062
2063 for (i = 0; i < par_len; i++) {
2064 indent_len = quote_len +
2065 indent_length(indent_line->data + quote_len);
2066
2067 if (indent_len != strlen(indent_string)) {
2068 indent_string = mallocstrncpy(indent_string,
2069 indent_line->data, indent_len + 1);
2070 indent_string[indent_len] = '\0';
2071
2072 if (past_first_line)
2073 break;
2074 }
2075
2076 if (indent_line == openfile->current)
2077 past_first_line = TRUE;
2078
2079 indent_line = indent_line->next;
2080 }
2081 }
2082
2083 /* Now tack all the lines of the paragraph together, skipping
2084 * the quoting and indentation on all lines after the first. */
2085 for (i = 0; i < par_len - 1; i++) {
2086 filestruct *next_line = openfile->current->next;
2087 size_t line_len = strlen(openfile->current->data);
2088 size_t next_line_len =
2089 strlen(openfile->current->next->data);
2090
2091 indent_len = quote_len +
2092 indent_length(openfile->current->next->data +
2093 quote_len);
2094
2095 next_line_len -= indent_len;
2096 openfile->totsize -= indent_len;
2097
2098 /* We're just about to tack the next line onto this one. If
2099 * this line isn't empty, make sure it ends in a space. */
2100 if (line_len > 0 &&
2101 openfile->current->data[line_len - 1] != ' ') {
2102 line_len++;
2103 openfile->current->data =
2104 charealloc(openfile->current->data,
2105 line_len + 1);
2106 openfile->current->data[line_len - 1] = ' ';
2107 openfile->current->data[line_len] = '\0';
2108 openfile->totsize++;
2109 }
2110
2111 openfile->current->data =
2112 charealloc(openfile->current->data, line_len +
2113 next_line_len + 1);
2114 strcat(openfile->current->data, next_line->data +
2115 indent_len);
2116
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002117 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00002118 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002119 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002120 if (next_line == openfile->filebot)
2121 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002122
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002123#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002124 /* Adjust the mark coordinates to compensate for the change
2125 * in the next line. */
2126 if (openfile->mark_set && openfile->mark_begin ==
2127 next_line) {
2128 openfile->mark_begin = openfile->current;
2129 openfile->mark_begin_x += line_len - indent_len;
2130 }
2131#endif
2132
2133 unlink_node(next_line);
2134 delete_node(next_line);
2135
2136 /* If we've removed the next line, we need to go through
2137 * this line again. */
2138 i--;
2139
2140 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002141 openfile->totsize--;
2142 }
2143
2144 /* Call justify_format() on the paragraph, which will remove
2145 * excess spaces from it and change all blank characters to
2146 * spaces. */
2147 justify_format(openfile->current, quote_len +
2148 indent_length(openfile->current->data + quote_len));
2149
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002150 while (par_len > 0 && strlenpt(openfile->current->data) >
2151 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002152 size_t line_len = strlen(openfile->current->data);
2153
2154 indent_len = strlen(indent_string);
2155
2156 /* If this line is too long, try to wrap it to the next line
2157 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002158 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00002159 fill - strnlenpt(openfile->current->data, indent_len)
2160#ifndef DISABLE_HELP
2161 , FALSE
2162#endif
2163 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002164
2165 /* We can't break the line, or don't need to, so get out. */
2166 if (break_pos == -1 || break_pos + indent_len == line_len)
2167 break;
2168
2169 /* Move forward to the character after the indentation and
2170 * just after the space. */
2171 break_pos += indent_len + 1;
2172
2173 assert(break_pos <= line_len);
2174
2175 /* Make a new line, and copy the text after where we're
2176 * going to break this line to the beginning of the new
2177 * line. */
2178 splice_node(openfile->current,
2179 make_new_node(openfile->current),
2180 openfile->current->next);
2181
2182 /* If this paragraph is non-quoted, and autoindent isn't
2183 * turned on, set the indentation length to zero so that the
2184 * indentation is treated as part of the line. */
2185 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002186#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002187 && !ISSET(AUTOINDENT)
2188#endif
2189 )
2190 indent_len = 0;
2191
2192 /* Copy the text after where we're going to break the
2193 * current line to the next line. */
2194 openfile->current->next->data = charalloc(indent_len + 1 +
2195 line_len - break_pos);
2196 strncpy(openfile->current->next->data, indent_string,
2197 indent_len);
2198 strcpy(openfile->current->next->data + indent_len,
2199 openfile->current->data + break_pos);
2200
2201 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002202 openfile->totsize += indent_len + 1;
2203
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002204#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002205 /* Adjust the mark coordinates to compensate for the change
2206 * in the current line. */
2207 if (openfile->mark_set && openfile->mark_begin ==
2208 openfile->current && openfile->mark_begin_x >
2209 break_pos) {
2210 openfile->mark_begin = openfile->current->next;
2211 openfile->mark_begin_x -= break_pos - indent_len;
2212 }
2213#endif
2214
2215 /* Break the current line. */
2216 null_at(&openfile->current->data, break_pos);
2217
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002218 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00002219 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002220 if (openfile->filebot == openfile->current)
2221 openfile->filebot = openfile->filebot->next;
2222
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002223 /* Go to the next line. */
2224 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002225 openfile->current_y++;
2226 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002227 }
2228
2229 /* We're done breaking lines, so we don't need indent_string
2230 * anymore. */
2231 free(indent_string);
2232
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002233 /* Go to the next line, if possible. If there is no next line,
2234 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00002235 if (openfile->current != openfile->filebot) {
2236 openfile->current_y++;
2237 openfile->current = openfile->current->next;
2238 } else
2239 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002240
David Lawrence Ramseya6854682005-11-30 21:19:42 +00002241 /* Renumber the lines of the now-justified current paragraph,
2242 * since both find_paragraph() and edit_refresh() need the line
2243 * numbers to be right. */
2244 renumber(curr_first_par_line);
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002245
2246 /* We've just finished justifying the paragraph. If we're not
2247 * justifying the entire file, break out of the loop.
2248 * Otherwise, continue the loop so that we justify all the
2249 * paragraphs in the file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002250 if (!full_justify)
2251 break;
2252 }
2253
2254 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002255 * up. current_y and totsize have been maintained above. If we
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002256 * actually justified something, set last_par_line to the new end of
2257 * the paragraph. */
2258 if (first_par_line != NULL)
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002259 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002260
2261 edit_refresh();
2262
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002263#ifndef NANO_TINY
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002264 /* We're going to set jump_buf so that we return here after a
2265 * SIGWINCH instead of to main(). Indicate this. */
2266 jump_buf_main = FALSE;
2267
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002268 /* Return here after a SIGWINCH. */
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002269 sigsetjmp(jump_buf, 1);
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002270#endif
2271
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002272 statusbar(_("Can now UnJustify!"));
2273
2274 /* If constant cursor position display is on, make sure the current
2275 * cursor position will be properly displayed on the statusbar. */
2276 if (ISSET(CONST_UPDATE))
2277 do_cursorpos(TRUE);
2278
2279 /* Display the shortcut list with UnJustify. */
2280 shortcut_init(TRUE);
2281 display_main_list();
2282
2283 /* Now get a keystroke and see if it's unjustify. If not, put back
2284 * the keystroke and return. */
2285 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
2286 &finished, FALSE);
Chris Allegretta0018d8e2008-03-13 08:23:52 +00002287 s = get_shortcut(currmenu, &kbinput, &meta_key, &func_key);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002288
Chris Allegretta637daa82011-02-07 14:45:56 +00002289 if (s && s->scfunc == do_uncut_text) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002290 /* Splice the justify buffer back into the file, but only if we
2291 * actually justified something. */
2292 if (first_par_line != NULL) {
2293 filestruct *top_save;
2294
2295 /* Partition the filestruct so that it contains only the
2296 * text of the justified paragraph. */
2297 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00002298 last_par_line, filebot_inpar ?
2299 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002300
2301 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00002302 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002303 free_filestruct(openfile->fileage);
2304 openfile->fileage = jusbuffer;
2305 openfile->filebot = jusbottom;
2306
2307 top_save = openfile->fileage;
2308
2309 /* Unpartition the filestruct so that it contains all the
2310 * text again. Note that the justified paragraph has been
2311 * replaced with the unjustified paragraph. */
2312 unpartition_filestruct(&filepart);
2313
2314 /* Renumber starting with the beginning line of the old
2315 * partition. */
2316 renumber(top_save);
2317
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002318 /* Restore the justify we just did (ungrateful user!). */
2319 openfile->edittop = edittop_save;
2320 openfile->current = current_save;
2321 openfile->current_x = current_x_save;
2322 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002323 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002324#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002325 if (openfile->mark_set) {
2326 openfile->mark_begin = mark_begin_save;
2327 openfile->mark_begin_x = mark_begin_x_save;
2328 }
2329#endif
2330 openfile->modified = modified_save;
2331
2332 /* Clear the justify buffer. */
2333 jusbuffer = NULL;
2334
2335 if (!openfile->modified)
2336 titlebar(NULL);
Chris Allegrettafd265af2009-02-06 03:41:02 +00002337 edit_refresh_needed = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002338 }
2339 } else {
2340 unget_kbinput(kbinput, meta_key, func_key);
2341
2342 /* Blow away the text in the justify buffer. */
2343 free_filestruct(jusbuffer);
2344 jusbuffer = NULL;
2345 }
2346
2347 blank_statusbar();
2348
2349 /* Display the shortcut list with UnCut. */
2350 shortcut_init(FALSE);
2351 display_main_list();
2352}
2353
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002354/* Justify the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002355void do_justify_void(void)
2356{
2357 do_justify(FALSE);
2358}
2359
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002360/* Justify the entire file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002361void do_full_justify(void)
2362{
2363 do_justify(TRUE);
2364}
2365#endif /* !DISABLE_JUSTIFY */
2366
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002367#ifndef DISABLE_SPELLER
2368/* A word is misspelled in the file. Let the user replace it. We
2369 * return FALSE if the user cancels. */
2370bool do_int_spell_fix(const char *word)
2371{
2372 char *save_search, *save_replace;
2373 size_t match_len, current_x_save = openfile->current_x;
2374 size_t pww_save = openfile->placewewant;
Chris Allegretta10f868d2008-03-14 04:08:51 +00002375 bool meta_key = FALSE, func_key = FALSE;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002376 filestruct *edittop_save = openfile->edittop;
2377 filestruct *current_save = openfile->current;
2378 /* Save where we are. */
2379 bool canceled = FALSE;
2380 /* The return value. */
2381 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002382#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002383 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
2384#endif
2385#ifdef HAVE_REGEX_H
2386 bool regexp_set = ISSET(USE_REGEXP);
2387#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002388#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002389 bool old_mark_set = openfile->mark_set;
2390 bool added_magicline = FALSE;
2391 /* Whether we added a magicline after filebot. */
2392 bool right_side_up = FALSE;
2393 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2394 * FALSE if (current, current_x) is. */
2395 filestruct *top, *bot;
2396 size_t top_x, bot_x;
2397#endif
2398
2399 /* Make sure spell-check is case sensitive. */
2400 SET(CASE_SENSITIVE);
2401
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002402#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002403 /* Make sure spell-check goes forward only. */
2404 UNSET(BACKWARDS_SEARCH);
2405#endif
2406#ifdef HAVE_REGEX_H
2407 /* Make sure spell-check doesn't use regular expressions. */
2408 UNSET(USE_REGEXP);
2409#endif
2410
2411 /* Save the current search/replace strings. */
2412 search_init_globals();
2413 save_search = last_search;
2414 save_replace = last_replace;
2415
2416 /* Set the search/replace strings to the misspelled word. */
2417 last_search = mallocstrcpy(NULL, word);
2418 last_replace = mallocstrcpy(NULL, word);
2419
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002420#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002421 if (old_mark_set) {
2422 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002423 * contains only the marked text; if the NO_NEWLINES flag isn't
2424 * set, keep track of whether the text will have a magicline
2425 * added when we're done correcting misspelled words; and
2426 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002427 mark_order((const filestruct **)&top, &top_x,
2428 (const filestruct **)&bot, &bot_x, &right_side_up);
2429 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002430 if (!ISSET(NO_NEWLINES))
2431 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002432 openfile->mark_set = FALSE;
2433 }
2434#endif
2435
2436 /* Start from the top of the file. */
2437 openfile->edittop = openfile->fileage;
2438 openfile->current = openfile->fileage;
2439 openfile->current_x = (size_t)-1;
2440 openfile->placewewant = 0;
2441
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002442 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002443 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002444 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002445 &match_len)) {
2446 if (is_whole_word(openfile->current_x, openfile->current->data,
2447 word)) {
2448 size_t xpt = xplustabs();
2449 char *exp_word = display_string(openfile->current->data,
2450 xpt, strnlenpt(openfile->current->data,
2451 openfile->current_x + match_len) - xpt, FALSE);
2452
2453 edit_refresh();
2454
2455 do_replace_highlight(TRUE, exp_word);
2456
2457 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002458 canceled = (do_prompt(FALSE,
2459#ifndef DISABLE_TABCOMP
2460 TRUE,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002461#endif
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002462 MSPELL, word,
Chris Allegretta10f868d2008-03-14 04:08:51 +00002463 &meta_key, &func_key,
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002464#ifndef NANO_TINY
2465 NULL,
2466#endif
David Lawrence Ramsey68160072006-02-18 21:32:29 +00002467 edit_refresh, _("Edit a replacement")) == -1);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002468
2469 do_replace_highlight(FALSE, exp_word);
2470
2471 free(exp_word);
2472
2473 if (!canceled && strcmp(word, answer) != 0) {
2474 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002475 do_replace_loop(TRUE, &canceled, openfile->current,
2476 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002477 }
2478
2479 break;
2480 }
2481 }
2482
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002483#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002484 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002485 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2486 * added a magicline, remove it now. */
2487 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002488 remove_magicline();
2489
2490 /* Put the beginning and the end of the mark at the beginning
2491 * and the end of the spell-checked text. */
2492 if (openfile->fileage == openfile->filebot)
2493 bot_x += top_x;
2494 if (right_side_up) {
2495 openfile->mark_begin_x = top_x;
2496 current_x_save = bot_x;
2497 } else {
2498 current_x_save = top_x;
2499 openfile->mark_begin_x = bot_x;
2500 }
2501
2502 /* Unpartition the filestruct so that it contains all the text
2503 * again, and turn the mark back on. */
2504 unpartition_filestruct(&filepart);
2505 openfile->mark_set = TRUE;
2506 }
2507#endif
2508
2509 /* Restore the search/replace strings. */
2510 free(last_search);
2511 last_search = save_search;
2512 free(last_replace);
2513 last_replace = save_replace;
2514
2515 /* Restore where we were. */
2516 openfile->edittop = edittop_save;
2517 openfile->current = current_save;
2518 openfile->current_x = current_x_save;
2519 openfile->placewewant = pww_save;
2520
2521 /* Restore case sensitivity setting. */
2522 if (!case_sens_set)
2523 UNSET(CASE_SENSITIVE);
2524
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002525#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002526 /* Restore search/replace direction. */
2527 if (backwards_search_set)
2528 SET(BACKWARDS_SEARCH);
2529#endif
2530#ifdef HAVE_REGEX_H
2531 /* Restore regular expression usage setting. */
2532 if (regexp_set)
2533 SET(USE_REGEXP);
2534#endif
2535
2536 return !canceled;
2537}
2538
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002539/* Internal (integrated) spell checking using the spell program,
2540 * filtered through the sort and uniq programs. Return NULL for normal
2541 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002542const char *do_int_speller(const char *tempfile_name)
2543{
2544 char *read_buff, *read_buff_ptr, *read_buff_word;
2545 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
2546 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
2547 pid_t pid_spell, pid_sort, pid_uniq;
2548 int spell_status, sort_status, uniq_status;
2549
2550 /* Create all three pipes up front. */
2551 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
2552 pipe(uniq_fd) == -1)
2553 return _("Could not create pipe");
2554
2555 statusbar(_("Creating misspelled word list, please wait..."));
2556
2557 /* A new process to run spell in. */
2558 if ((pid_spell = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002559 /* Child continues (i.e. future spell process). */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002560 close(spell_fd[0]);
2561
2562 /* Replace the standard input with the temp file. */
2563 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
2564 goto close_pipes_and_exit;
2565
2566 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
2567 goto close_pipes_and_exit;
2568
2569 close(tempfile_fd);
2570
2571 /* Send spell's standard output to the pipe. */
2572 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2573 goto close_pipes_and_exit;
2574
2575 close(spell_fd[1]);
2576
David Lawrence Ramsey3239ff22005-11-29 20:01:06 +00002577 /* Start the spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002578 execlp("spell", "spell", NULL);
2579
2580 /* This should not be reached if spell is found. */
2581 exit(1);
2582 }
2583
2584 /* Parent continues here. */
2585 close(spell_fd[1]);
2586
2587 /* A new process to run sort in. */
2588 if ((pid_sort = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002589 /* Child continues (i.e. future spell process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002590 * standard input with the standard output of the old pipe. */
2591 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
2592 goto close_pipes_and_exit;
2593
2594 close(spell_fd[0]);
2595
2596 /* Send sort's standard output to the new pipe. */
2597 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2598 goto close_pipes_and_exit;
2599
2600 close(sort_fd[1]);
2601
2602 /* Start the sort program. Use -f to remove mixed case. If
2603 * this isn't portable, let me know. */
2604 execlp("sort", "sort", "-f", NULL);
2605
2606 /* This should not be reached if sort is found. */
2607 exit(1);
2608 }
2609
2610 close(spell_fd[0]);
2611 close(sort_fd[1]);
2612
2613 /* A new process to run uniq in. */
2614 if ((pid_uniq = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002615 /* Child continues (i.e. future uniq process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002616 * standard input with the standard output of the old pipe. */
2617 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
2618 goto close_pipes_and_exit;
2619
2620 close(sort_fd[0]);
2621
2622 /* Send uniq's standard output to the new pipe. */
2623 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2624 goto close_pipes_and_exit;
2625
2626 close(uniq_fd[1]);
2627
2628 /* Start the uniq program; we are using PATH. */
2629 execlp("uniq", "uniq", NULL);
2630
2631 /* This should not be reached if uniq is found. */
2632 exit(1);
2633 }
2634
2635 close(sort_fd[0]);
2636 close(uniq_fd[1]);
2637
2638 /* The child process was not forked successfully. */
2639 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
2640 close(uniq_fd[0]);
2641 return _("Could not fork");
2642 }
2643
2644 /* Get the system pipe buffer size. */
2645 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
2646 close(uniq_fd[0]);
2647 return _("Could not get size of pipe buffer");
2648 }
2649
2650 /* Read in the returned spelling errors. */
2651 read_buff_read = 0;
2652 read_buff_size = pipe_buff_size + 1;
2653 read_buff = read_buff_ptr = charalloc(read_buff_size);
2654
2655 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
2656 pipe_buff_size)) > 0) {
2657 read_buff_read += bytesread;
2658 read_buff_size += pipe_buff_size;
2659 read_buff = read_buff_ptr = charealloc(read_buff,
2660 read_buff_size);
2661 read_buff_ptr += read_buff_read;
2662 }
2663
2664 *read_buff_ptr = '\0';
2665 close(uniq_fd[0]);
2666
2667 /* Process the spelling errors. */
2668 read_buff_word = read_buff_ptr = read_buff;
2669
2670 while (*read_buff_ptr != '\0') {
2671 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
2672 *read_buff_ptr = '\0';
2673 if (read_buff_word != read_buff_ptr) {
2674 if (!do_int_spell_fix(read_buff_word)) {
2675 read_buff_word = read_buff_ptr;
2676 break;
2677 }
2678 }
2679 read_buff_word = read_buff_ptr + 1;
2680 }
2681 read_buff_ptr++;
2682 }
2683
2684 /* Special case: the last word doesn't end with '\r' or '\n'. */
2685 if (read_buff_word != read_buff_ptr)
2686 do_int_spell_fix(read_buff_word);
2687
2688 free(read_buff);
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002689 search_replace_abort();
Chris Allegrettafd265af2009-02-06 03:41:02 +00002690 edit_refresh_needed = TRUE;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002691
2692 /* Process the end of the spell process. */
2693 waitpid(pid_spell, &spell_status, 0);
2694 waitpid(pid_sort, &sort_status, 0);
2695 waitpid(pid_uniq, &uniq_status, 0);
2696
2697 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
2698 return _("Error invoking \"spell\"");
2699
2700 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
2701 return _("Error invoking \"sort -f\"");
2702
2703 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
2704 return _("Error invoking \"uniq\"");
2705
2706 /* Otherwise... */
2707 return NULL;
2708
2709 close_pipes_and_exit:
2710 /* Don't leak any handles. */
2711 close(tempfile_fd);
2712 close(spell_fd[0]);
2713 close(spell_fd[1]);
2714 close(sort_fd[0]);
2715 close(sort_fd[1]);
2716 close(uniq_fd[0]);
2717 close(uniq_fd[1]);
2718 exit(1);
2719}
2720
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002721/* External (alternate) spell checking. Return NULL for normal
2722 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002723const char *do_alt_speller(char *tempfile_name)
2724{
2725 int alt_spell_status;
2726 size_t current_x_save = openfile->current_x;
2727 size_t pww_save = openfile->placewewant;
2728 ssize_t current_y_save = openfile->current_y;
2729 ssize_t lineno_save = openfile->current->lineno;
2730 pid_t pid_spell;
2731 char *ptr;
2732 static int arglen = 3;
2733 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002734#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002735 bool old_mark_set = openfile->mark_set;
2736 bool added_magicline = FALSE;
2737 /* Whether we added a magicline after filebot. */
2738 bool right_side_up = FALSE;
2739 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2740 * FALSE if (current, current_x) is. */
2741 filestruct *top, *bot;
2742 size_t top_x, bot_x;
2743 ssize_t mb_lineno_save = 0;
2744 /* We're going to close the current file, and open the output of
2745 * the alternate spell command. The line that mark_begin points
2746 * to will be freed, so we save the line number and restore it
2747 * afterwards. */
2748 size_t totsize_save = openfile->totsize;
2749 /* Our saved value of totsize, used when we spell-check a marked
2750 * selection. */
2751
2752 if (old_mark_set) {
2753 /* If the mark is on, save the number of the line it starts on,
2754 * and then turn the mark off. */
2755 mb_lineno_save = openfile->mark_begin->lineno;
2756 openfile->mark_set = FALSE;
2757 }
2758#endif
2759
Chris Allegretta181c4a92010-04-14 03:14:40 +00002760 if (openfile->totsize == 0) {
2761 statusbar(_("Finished checking spelling"));
2762 return NULL;
2763 }
2764
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002765 endwin();
2766
2767 /* Set up an argument list to pass execvp(). */
2768 if (spellargs == NULL) {
2769 spellargs = (char **)nmalloc(arglen * sizeof(char *));
2770
2771 spellargs[0] = strtok(alt_speller, " ");
2772 while ((ptr = strtok(NULL, " ")) != NULL) {
2773 arglen++;
2774 spellargs = (char **)nrealloc(spellargs, arglen *
2775 sizeof(char *));
2776 spellargs[arglen - 3] = ptr;
2777 }
2778 spellargs[arglen - 1] = NULL;
2779 }
2780 spellargs[arglen - 2] = tempfile_name;
2781
2782 /* Start a new process for the alternate speller. */
2783 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey2e2112c2005-11-29 05:39:31 +00002784 /* Start alternate spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002785 execvp(spellargs[0], spellargs);
2786
2787 /* Should not be reached, if alternate speller is found!!! */
2788 exit(1);
2789 }
2790
2791 /* If we couldn't fork, get out. */
2792 if (pid_spell < 0)
2793 return _("Could not fork");
2794
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002795#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002796 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002797 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002798 allow_pending_sigwinch(FALSE);
2799#endif
2800
2801 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002802 wait(&alt_spell_status);
2803
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00002804 /* Reenter curses mode. */
2805 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002806
2807 /* Restore the terminal to its previous state. */
2808 terminal_init();
2809
2810 /* Turn the cursor back on for sure. */
2811 curs_set(1);
2812
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002813 /* The screen might have been resized. If it has, reinitialize all
2814 * the windows based on the new screen dimensions. */
2815 window_init();
2816
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002817 if (!WIFEXITED(alt_spell_status) ||
2818 WEXITSTATUS(alt_spell_status) != 0) {
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002819 char *alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002820 char *invoke_error = _("Error invoking \"%s\"");
2821
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002822#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002823 /* Turn the mark back on if it was on before. */
2824 openfile->mark_set = old_mark_set;
2825#endif
2826
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002827 alt_spell_error =
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002828 charalloc(strlen(invoke_error) +
2829 strlen(alt_speller) + 1);
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002830 sprintf(alt_spell_error, invoke_error, alt_speller);
2831 return alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002832 }
2833
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002834#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002835 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002836 /* If the mark is on, partition the filestruct so that it
2837 * contains only the marked text; if the NO_NEWLINES flag isn't
2838 * set, keep track of whether the text will have a magicline
2839 * added when we're done correcting misspelled words; and
2840 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002841 mark_order((const filestruct **)&top, &top_x,
2842 (const filestruct **)&bot, &bot_x, &right_side_up);
2843 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002844 if (!ISSET(NO_NEWLINES))
2845 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002846
2847 /* Get the number of characters in the marked text, and subtract
2848 * it from the saved value of totsize. */
2849 totsize_save -= get_totsize(top, bot);
2850 }
2851#endif
2852
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002853 /* Replace the text of the current buffer with the spell-checked
2854 * text. */
2855 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002856
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002857#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002858 if (old_mark_set) {
2859 filestruct *top_save = openfile->fileage;
2860
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002861 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2862 * added a magicline, remove it now. */
2863 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002864 remove_magicline();
2865
2866 /* Put the beginning and the end of the mark at the beginning
2867 * and the end of the spell-checked text. */
2868 if (openfile->fileage == openfile->filebot)
2869 bot_x += top_x;
2870 if (right_side_up) {
2871 openfile->mark_begin_x = top_x;
2872 current_x_save = bot_x;
2873 } else {
2874 current_x_save = top_x;
2875 openfile->mark_begin_x = bot_x;
2876 }
2877
2878 /* Unpartition the filestruct so that it contains all the text
2879 * again. Note that we've replaced the marked text originally
2880 * in the partition with the spell-checked marked text in the
2881 * temp file. */
2882 unpartition_filestruct(&filepart);
2883
2884 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002885 * partition. Also add the number of characters in the
2886 * spell-checked marked text to the saved value of totsize, and
2887 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002888 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002889 totsize_save += openfile->totsize;
2890 openfile->totsize = totsize_save;
2891
2892 /* Assign mark_begin to the line where the mark began before. */
2893 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2894 current_y_save, 0);
2895 openfile->mark_begin = openfile->current;
2896
2897 /* Assign mark_begin_x to the location in mark_begin where the
2898 * mark began before, adjusted for any shortening of the
2899 * line. */
2900 openfile->mark_begin_x = openfile->current_x;
2901
2902 /* Turn the mark back on. */
2903 openfile->mark_set = TRUE;
2904 }
2905#endif
2906
2907 /* Go back to the old position, and mark the file as modified. */
2908 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2909 set_modified();
2910
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002911#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002912 /* Handle a pending SIGWINCH again. */
2913 allow_pending_sigwinch(TRUE);
2914#endif
2915
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002916 return NULL;
2917}
2918
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002919/* Spell check the current file. If an alternate spell checker is
2920 * specified, use it. Otherwise, use the internal spell checker. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002921void do_spell(void)
2922{
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002923 bool status;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002924 FILE *temp_file;
2925 char *temp = safe_tempfile(&temp_file);
2926 const char *spell_msg;
2927
Chris Allegrettaab538642010-11-12 06:22:12 +00002928 if (ISSET(RESTRICTED)) {
2929 nano_disabled_msg();
2930 return;
2931 }
2932
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002933 if (temp == NULL) {
David Lawrence Ramseyf0e3ca62006-05-03 13:11:00 +00002934 statusbar(_("Error writing temp file: %s"), strerror(errno));
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002935 return;
2936 }
2937
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002938 status =
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002939#ifndef NANO_TINY
David Lawrence Ramsey0e1df432007-01-12 02:58:12 +00002940 openfile->mark_set ? write_marked_file(temp, temp_file, TRUE,
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002941 OVERWRITE) :
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002942#endif
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002943 write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002944
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002945 if (!status) {
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002946 statusbar(_("Error writing temp file: %s"), strerror(errno));
2947 free(temp);
2948 return;
2949 }
2950
2951 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2952 do_int_speller(temp);
2953 unlink(temp);
2954 free(temp);
2955
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002956 currmenu = MMAIN;
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002957
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002958 /* If the spell-checker printed any error messages onscreen, make
2959 * sure that they're cleared off. */
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002960 total_refresh();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002961
2962 if (spell_msg != NULL) {
2963 if (errno == 0)
2964 /* Don't display an error message of "Success". */
2965 statusbar(_("Spell checking failed: %s"), spell_msg);
2966 else
2967 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2968 strerror(errno));
2969 } else
2970 statusbar(_("Finished checking spelling"));
2971}
2972#endif /* !DISABLE_SPELLER */
2973
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002974#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002975/* Our own version of "wc". Note that its character counts are in
2976 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002977void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002978{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002979 size_t words = 0, chars = 0;
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002980 ssize_t nlines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002981 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002982 size_t pww_save = openfile->placewewant;
2983 filestruct *current_save = openfile->current;
2984 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002985 filestruct *top, *bot;
2986 size_t top_x, bot_x;
2987
2988 if (old_mark_set) {
2989 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002990 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002991 mark_order((const filestruct **)&top, &top_x,
2992 (const filestruct **)&bot, &bot_x, NULL);
2993 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002994 openfile->mark_set = FALSE;
2995 }
2996
2997 /* Start at the top of the file. */
2998 openfile->current = openfile->fileage;
2999 openfile->current_x = 0;
3000 openfile->placewewant = 0;
3001
3002 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003003 * part of a word, as "wc -w" does), without updating the screen,
3004 * until we reach the end of the file, incrementing the total word
3005 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003006 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00003007 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003008 if (do_next_word(TRUE, FALSE))
3009 words++;
3010 }
3011
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003012 /* Get the total line and character counts, as "wc -l" and "wc -c"
3013 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003014 if (old_mark_set) {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00003015 nlines = openfile->filebot->lineno -
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00003016 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003017 chars = get_totsize(openfile->fileage, openfile->filebot);
3018
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003019 /* Unpartition the filestruct so that it contains all the text
3020 * again, and turn the mark back on. */
3021 unpartition_filestruct(&filepart);
3022 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003023 } else {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00003024 nlines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003025 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003026 }
3027
3028 /* Restore where we were. */
3029 openfile->current = current_save;
3030 openfile->current_x = current_x_save;
3031 openfile->placewewant = pww_save;
3032
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003033 /* Display the total word, line, and character counts on the
3034 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00003035 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
Chris Allegretta8b6461f2008-05-31 23:09:40 +00003036 _("In Selection: ") : "", (unsigned long)words, (long)nlines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00003037 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003038}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00003039#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003040
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00003041/* Get verbatim input. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003042void do_verbatim_input(void)
3043{
3044 int *kbinput;
3045 size_t kbinput_len, i;
3046 char *output;
3047
David Lawrence Ramseyf451d6a2006-05-27 16:02:48 +00003048 /* TRANSLATORS: This is displayed when the next keystroke will be
3049 * inserted verbatim. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003050 statusbar(_("Verbatim Input"));
3051
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003052 /* Read in all the verbatim characters. */
3053 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
3054
David Lawrence Ramseya620e682006-05-27 18:19:03 +00003055 /* If constant cursor position display is on, make sure the current
3056 * cursor position will be properly displayed on the statusbar.
3057 * Otherwise, blank the statusbar. */
3058 if (ISSET(CONST_UPDATE))
3059 do_cursorpos(TRUE);
3060 else {
3061 blank_statusbar();
3062 wnoutrefresh(bottomwin);
3063 }
David Lawrence Ramsey6fb66892006-05-27 17:39:19 +00003064
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003065 /* Display all the verbatim characters at once, not filtering out
3066 * control characters. */
3067 output = charalloc(kbinput_len + 1);
3068
3069 for (i = 0; i < kbinput_len; i++)
3070 output[i] = (char)kbinput[i];
3071 output[i] = '\0';
3072
David Lawrence Ramseyad36bdc2006-12-02 17:22:21 +00003073 free(kbinput);
3074
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003075 do_output(output, kbinput_len, TRUE);
3076
3077 free(output);
3078}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00003079