blob: 57e619c24e2b53008db49fef0594ae6a13d8206f [file] [log] [blame]
Chris Allegretta2ad1de02012-12-30 19:20:10 +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) {
Benno Schulenberge9ee0782014-02-23 10:12:50 +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:
Benno Schulenberge9ee0782014-02-23 10:12:50 +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) {
Benno Schulenberge9ee0782014-02-23 10:12:50 +0000582 statusbar(_("Internal error: cannot set up redo. 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) {
Benno Schulenberge9ee0782014-02-23 10:12:50 +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:
Benno Schulenberge9ee0782014-02-23 10:12:50 +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);
Chris Allegrettadaeab052011-05-10 05:43:08 +0000714 openfile->totsize += extra;
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000715 }
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) {
Benno Schulenberg46fccb22014-02-28 11:49:12 +0000771 statusbar(_("Could not create pipe"));
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000772 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 Allegretta2ad1de02012-12-30 19:20:10 +0000858 if (current_action == CUT && u && u->type == CUT
Chris Allegretta80ea9c52008-08-09 10:08:33 +0000859 && !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)
Benno Schulenberge9ee0782014-02-23 10:12:50 +0000946 statusbar(_("Internal error: cannot set up uncut. Please save your work."));
Chris Allegretta5c1c1432008-10-04 11:10:11 +0000947 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
Chris Allegretta67acf2c2013-01-20 07:51:34 +00001982 allow_pending_sigwinch(FALSE);
1983
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001984 while (TRUE) {
1985 size_t i;
1986 /* Generic loop variable. */
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001987 filestruct *curr_first_par_line;
1988 /* The first line of the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001989 size_t quote_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001990 /* Length of the initial quotation of the current
1991 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001992 size_t indent_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001993 /* Length of the initial indentation of the current
1994 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001995 size_t par_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001996 /* Number of lines in the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001997 ssize_t break_pos;
1998 /* Where we will break lines. */
1999 char *indent_string;
2000 /* The first indentation that doesn't match the initial
David Lawrence Ramseya6854682005-11-30 21:19:42 +00002001 * indentation of the current paragraph. This is put at the
2002 * beginning of every line broken off the first justified
2003 * line of the paragraph. Note that this works because a
2004 * paragraph can only contain two indentations at most: the
2005 * initial one, and a different one starting on a line after
2006 * the first. See the comment at begpar() for more about
2007 * when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002008
2009 /* Find the first line of the paragraph to be justified. That
2010 * is the start of this paragraph if we're in one, or the start
2011 * of the next otherwise. Save the quote length and paragraph
2012 * length (number of lines). Don't refresh the screen yet,
2013 * since we'll do that after we justify.
2014 *
2015 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00002016 * justifying the whole file, and we've found at least one
2017 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002018 * last line of the file, so set the last line of the text to be
2019 * justified to the last line of the file and break out of the
2020 * loop. Otherwise, it means that there are no paragraph(s) to
2021 * justify, so refresh the screen and get out. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00002022 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00002023 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002024 last_par_line = openfile->filebot;
2025 break;
2026 } else {
Chris Allegrettafd265af2009-02-06 03:41:02 +00002027 edit_refresh_needed = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002028 return;
2029 }
2030 }
2031
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00002032 /* par_len will be one greater than the number of lines between
2033 * current and filebot if filebot is the last line in the
2034 * paragraph. Set filebot_inpar to TRUE if this is the case. */
2035 filebot_inpar = (openfile->current->lineno + par_len ==
2036 openfile->filebot->lineno + 1);
2037
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00002038 /* If we haven't already done it, move the original paragraph(s)
2039 * to the justify buffer, splice a copy of the original
2040 * paragraph(s) into the file in the same place, and set
2041 * first_par_line to the first line of the copy. */
2042 if (first_par_line == NULL) {
2043 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00002044 openfile->filebot->lineno - openfile->current->lineno +
2045 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00002046 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00002047 first_par_line = openfile->current;
2048 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002049
David Lawrence Ramseya6854682005-11-30 21:19:42 +00002050 /* Set curr_first_par_line to the first line of the current
2051 * paragraph. */
2052 curr_first_par_line = openfile->current;
2053
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002054 /* Initialize indent_string to a blank string. */
2055 indent_string = mallocstrcpy(NULL, "");
2056
2057 /* Find the first indentation in the paragraph that doesn't
David Lawrence Ramsey8602fd62006-05-28 18:43:21 +00002058 * match the indentation of the first line, and save it in
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002059 * indent_string. If all the indentations are the same, save
2060 * the indentation of the first line in indent_string. */
2061 {
2062 const filestruct *indent_line = openfile->current;
2063 bool past_first_line = FALSE;
2064
2065 for (i = 0; i < par_len; i++) {
2066 indent_len = quote_len +
2067 indent_length(indent_line->data + quote_len);
2068
2069 if (indent_len != strlen(indent_string)) {
2070 indent_string = mallocstrncpy(indent_string,
2071 indent_line->data, indent_len + 1);
2072 indent_string[indent_len] = '\0';
2073
2074 if (past_first_line)
2075 break;
2076 }
2077
2078 if (indent_line == openfile->current)
2079 past_first_line = TRUE;
2080
2081 indent_line = indent_line->next;
2082 }
2083 }
2084
2085 /* Now tack all the lines of the paragraph together, skipping
2086 * the quoting and indentation on all lines after the first. */
2087 for (i = 0; i < par_len - 1; i++) {
2088 filestruct *next_line = openfile->current->next;
2089 size_t line_len = strlen(openfile->current->data);
2090 size_t next_line_len =
2091 strlen(openfile->current->next->data);
2092
2093 indent_len = quote_len +
2094 indent_length(openfile->current->next->data +
2095 quote_len);
2096
2097 next_line_len -= indent_len;
2098 openfile->totsize -= indent_len;
2099
2100 /* We're just about to tack the next line onto this one. If
2101 * this line isn't empty, make sure it ends in a space. */
2102 if (line_len > 0 &&
2103 openfile->current->data[line_len - 1] != ' ') {
2104 line_len++;
2105 openfile->current->data =
2106 charealloc(openfile->current->data,
2107 line_len + 1);
2108 openfile->current->data[line_len - 1] = ' ';
2109 openfile->current->data[line_len] = '\0';
2110 openfile->totsize++;
2111 }
2112
2113 openfile->current->data =
2114 charealloc(openfile->current->data, line_len +
2115 next_line_len + 1);
2116 strcat(openfile->current->data, next_line->data +
2117 indent_len);
2118
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002119 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00002120 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002121 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002122 if (next_line == openfile->filebot)
2123 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002124
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002125#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002126 /* Adjust the mark coordinates to compensate for the change
2127 * in the next line. */
2128 if (openfile->mark_set && openfile->mark_begin ==
2129 next_line) {
2130 openfile->mark_begin = openfile->current;
2131 openfile->mark_begin_x += line_len - indent_len;
2132 }
2133#endif
2134
2135 unlink_node(next_line);
2136 delete_node(next_line);
2137
2138 /* If we've removed the next line, we need to go through
2139 * this line again. */
2140 i--;
2141
2142 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002143 openfile->totsize--;
2144 }
2145
2146 /* Call justify_format() on the paragraph, which will remove
2147 * excess spaces from it and change all blank characters to
2148 * spaces. */
2149 justify_format(openfile->current, quote_len +
2150 indent_length(openfile->current->data + quote_len));
2151
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002152 while (par_len > 0 && strlenpt(openfile->current->data) >
2153 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002154 size_t line_len = strlen(openfile->current->data);
2155
2156 indent_len = strlen(indent_string);
2157
2158 /* If this line is too long, try to wrap it to the next line
2159 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002160 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00002161 fill - strnlenpt(openfile->current->data, indent_len)
2162#ifndef DISABLE_HELP
2163 , FALSE
2164#endif
2165 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002166
2167 /* We can't break the line, or don't need to, so get out. */
2168 if (break_pos == -1 || break_pos + indent_len == line_len)
2169 break;
2170
2171 /* Move forward to the character after the indentation and
2172 * just after the space. */
2173 break_pos += indent_len + 1;
2174
2175 assert(break_pos <= line_len);
2176
2177 /* Make a new line, and copy the text after where we're
2178 * going to break this line to the beginning of the new
2179 * line. */
2180 splice_node(openfile->current,
2181 make_new_node(openfile->current),
2182 openfile->current->next);
2183
2184 /* If this paragraph is non-quoted, and autoindent isn't
2185 * turned on, set the indentation length to zero so that the
2186 * indentation is treated as part of the line. */
2187 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002188#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002189 && !ISSET(AUTOINDENT)
2190#endif
2191 )
2192 indent_len = 0;
2193
2194 /* Copy the text after where we're going to break the
2195 * current line to the next line. */
2196 openfile->current->next->data = charalloc(indent_len + 1 +
2197 line_len - break_pos);
2198 strncpy(openfile->current->next->data, indent_string,
2199 indent_len);
2200 strcpy(openfile->current->next->data + indent_len,
2201 openfile->current->data + break_pos);
2202
2203 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002204 openfile->totsize += indent_len + 1;
2205
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002206#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002207 /* Adjust the mark coordinates to compensate for the change
2208 * in the current line. */
2209 if (openfile->mark_set && openfile->mark_begin ==
2210 openfile->current && openfile->mark_begin_x >
2211 break_pos) {
2212 openfile->mark_begin = openfile->current->next;
2213 openfile->mark_begin_x -= break_pos - indent_len;
2214 }
2215#endif
2216
2217 /* Break the current line. */
2218 null_at(&openfile->current->data, break_pos);
2219
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002220 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00002221 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002222 if (openfile->filebot == openfile->current)
2223 openfile->filebot = openfile->filebot->next;
2224
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002225 /* Go to the next line. */
2226 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002227 openfile->current_y++;
2228 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002229 }
2230
2231 /* We're done breaking lines, so we don't need indent_string
2232 * anymore. */
2233 free(indent_string);
2234
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002235 /* Go to the next line, if possible. If there is no next line,
2236 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00002237 if (openfile->current != openfile->filebot) {
2238 openfile->current_y++;
2239 openfile->current = openfile->current->next;
2240 } else
2241 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002242
David Lawrence Ramseya6854682005-11-30 21:19:42 +00002243 /* Renumber the lines of the now-justified current paragraph,
2244 * since both find_paragraph() and edit_refresh() need the line
2245 * numbers to be right. */
2246 renumber(curr_first_par_line);
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002247
2248 /* We've just finished justifying the paragraph. If we're not
2249 * justifying the entire file, break out of the loop.
2250 * Otherwise, continue the loop so that we justify all the
2251 * paragraphs in the file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002252 if (!full_justify)
2253 break;
2254 }
2255
2256 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002257 * up. current_y and totsize have been maintained above. If we
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002258 * actually justified something, set last_par_line to the new end of
2259 * the paragraph. */
2260 if (first_par_line != NULL)
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002261 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002262
2263 edit_refresh();
2264
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002265#ifndef NANO_TINY
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002266 /* We're going to set jump_buf so that we return here after a
2267 * SIGWINCH instead of to main(). Indicate this. */
2268 jump_buf_main = FALSE;
2269
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002270 /* Return here after a SIGWINCH. */
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002271 sigsetjmp(jump_buf, 1);
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002272#endif
2273
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002274 statusbar(_("Can now UnJustify!"));
2275
2276 /* If constant cursor position display is on, make sure the current
2277 * cursor position will be properly displayed on the statusbar. */
2278 if (ISSET(CONST_UPDATE))
2279 do_cursorpos(TRUE);
2280
2281 /* Display the shortcut list with UnJustify. */
2282 shortcut_init(TRUE);
2283 display_main_list();
2284
2285 /* Now get a keystroke and see if it's unjustify. If not, put back
2286 * the keystroke and return. */
2287 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
2288 &finished, FALSE);
Chris Allegretta0018d8e2008-03-13 08:23:52 +00002289 s = get_shortcut(currmenu, &kbinput, &meta_key, &func_key);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002290
Chris Allegretta637daa82011-02-07 14:45:56 +00002291 if (s && s->scfunc == do_uncut_text) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002292 /* Splice the justify buffer back into the file, but only if we
2293 * actually justified something. */
2294 if (first_par_line != NULL) {
2295 filestruct *top_save;
2296
2297 /* Partition the filestruct so that it contains only the
2298 * text of the justified paragraph. */
2299 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00002300 last_par_line, filebot_inpar ?
2301 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002302
2303 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00002304 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002305 free_filestruct(openfile->fileage);
2306 openfile->fileage = jusbuffer;
2307 openfile->filebot = jusbottom;
2308
2309 top_save = openfile->fileage;
2310
2311 /* Unpartition the filestruct so that it contains all the
2312 * text again. Note that the justified paragraph has been
2313 * replaced with the unjustified paragraph. */
2314 unpartition_filestruct(&filepart);
2315
2316 /* Renumber starting with the beginning line of the old
2317 * partition. */
2318 renumber(top_save);
2319
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002320 /* Restore the justify we just did (ungrateful user!). */
2321 openfile->edittop = edittop_save;
2322 openfile->current = current_save;
2323 openfile->current_x = current_x_save;
2324 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002325 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002326#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002327 if (openfile->mark_set) {
2328 openfile->mark_begin = mark_begin_save;
2329 openfile->mark_begin_x = mark_begin_x_save;
2330 }
2331#endif
2332 openfile->modified = modified_save;
2333
2334 /* Clear the justify buffer. */
2335 jusbuffer = NULL;
2336
2337 if (!openfile->modified)
2338 titlebar(NULL);
Chris Allegrettafd265af2009-02-06 03:41:02 +00002339 edit_refresh_needed = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002340 }
2341 } else {
2342 unget_kbinput(kbinput, meta_key, func_key);
2343
2344 /* Blow away the text in the justify buffer. */
2345 free_filestruct(jusbuffer);
2346 jusbuffer = NULL;
2347 }
2348
2349 blank_statusbar();
2350
2351 /* Display the shortcut list with UnCut. */
2352 shortcut_init(FALSE);
2353 display_main_list();
Chris Allegretta67acf2c2013-01-20 07:51:34 +00002354
2355 allow_pending_sigwinch(TRUE);
2356
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002357}
2358
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002359/* Justify the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002360void do_justify_void(void)
2361{
2362 do_justify(FALSE);
2363}
2364
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002365/* Justify the entire file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002366void do_full_justify(void)
2367{
2368 do_justify(TRUE);
2369}
2370#endif /* !DISABLE_JUSTIFY */
2371
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002372#ifndef DISABLE_SPELLER
2373/* A word is misspelled in the file. Let the user replace it. We
2374 * return FALSE if the user cancels. */
2375bool do_int_spell_fix(const char *word)
2376{
2377 char *save_search, *save_replace;
2378 size_t match_len, current_x_save = openfile->current_x;
2379 size_t pww_save = openfile->placewewant;
Chris Allegretta10f868d2008-03-14 04:08:51 +00002380 bool meta_key = FALSE, func_key = FALSE;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002381 filestruct *edittop_save = openfile->edittop;
2382 filestruct *current_save = openfile->current;
2383 /* Save where we are. */
2384 bool canceled = FALSE;
2385 /* The return value. */
2386 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002387#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002388 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
2389#endif
2390#ifdef HAVE_REGEX_H
2391 bool regexp_set = ISSET(USE_REGEXP);
2392#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002393#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002394 bool old_mark_set = openfile->mark_set;
2395 bool added_magicline = FALSE;
2396 /* Whether we added a magicline after filebot. */
2397 bool right_side_up = FALSE;
2398 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2399 * FALSE if (current, current_x) is. */
2400 filestruct *top, *bot;
2401 size_t top_x, bot_x;
2402#endif
2403
2404 /* Make sure spell-check is case sensitive. */
2405 SET(CASE_SENSITIVE);
2406
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002407#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002408 /* Make sure spell-check goes forward only. */
2409 UNSET(BACKWARDS_SEARCH);
2410#endif
2411#ifdef HAVE_REGEX_H
2412 /* Make sure spell-check doesn't use regular expressions. */
2413 UNSET(USE_REGEXP);
2414#endif
2415
2416 /* Save the current search/replace strings. */
2417 search_init_globals();
2418 save_search = last_search;
2419 save_replace = last_replace;
2420
2421 /* Set the search/replace strings to the misspelled word. */
2422 last_search = mallocstrcpy(NULL, word);
2423 last_replace = mallocstrcpy(NULL, word);
2424
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002425#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002426 if (old_mark_set) {
2427 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002428 * contains only the marked text; if the NO_NEWLINES flag isn't
2429 * set, keep track of whether the text will have a magicline
2430 * added when we're done correcting misspelled words; and
2431 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002432 mark_order((const filestruct **)&top, &top_x,
2433 (const filestruct **)&bot, &bot_x, &right_side_up);
2434 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002435 if (!ISSET(NO_NEWLINES))
2436 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002437 openfile->mark_set = FALSE;
2438 }
2439#endif
2440
2441 /* Start from the top of the file. */
2442 openfile->edittop = openfile->fileage;
2443 openfile->current = openfile->fileage;
2444 openfile->current_x = (size_t)-1;
2445 openfile->placewewant = 0;
2446
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002447 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002448 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002449 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002450 &match_len)) {
2451 if (is_whole_word(openfile->current_x, openfile->current->data,
2452 word)) {
2453 size_t xpt = xplustabs();
2454 char *exp_word = display_string(openfile->current->data,
2455 xpt, strnlenpt(openfile->current->data,
2456 openfile->current_x + match_len) - xpt, FALSE);
2457
2458 edit_refresh();
2459
2460 do_replace_highlight(TRUE, exp_word);
2461
2462 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002463 canceled = (do_prompt(FALSE,
2464#ifndef DISABLE_TABCOMP
2465 TRUE,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002466#endif
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002467 MSPELL, word,
Chris Allegretta10f868d2008-03-14 04:08:51 +00002468 &meta_key, &func_key,
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002469#ifndef NANO_TINY
2470 NULL,
2471#endif
David Lawrence Ramsey68160072006-02-18 21:32:29 +00002472 edit_refresh, _("Edit a replacement")) == -1);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002473
2474 do_replace_highlight(FALSE, exp_word);
2475
2476 free(exp_word);
2477
2478 if (!canceled && strcmp(word, answer) != 0) {
2479 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002480 do_replace_loop(TRUE, &canceled, openfile->current,
2481 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002482 }
2483
2484 break;
2485 }
2486 }
2487
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002488#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002489 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002490 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2491 * added a magicline, remove it now. */
2492 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002493 remove_magicline();
2494
2495 /* Put the beginning and the end of the mark at the beginning
2496 * and the end of the spell-checked text. */
2497 if (openfile->fileage == openfile->filebot)
2498 bot_x += top_x;
2499 if (right_side_up) {
2500 openfile->mark_begin_x = top_x;
2501 current_x_save = bot_x;
2502 } else {
2503 current_x_save = top_x;
2504 openfile->mark_begin_x = bot_x;
2505 }
2506
2507 /* Unpartition the filestruct so that it contains all the text
2508 * again, and turn the mark back on. */
2509 unpartition_filestruct(&filepart);
2510 openfile->mark_set = TRUE;
2511 }
2512#endif
2513
2514 /* Restore the search/replace strings. */
2515 free(last_search);
2516 last_search = save_search;
2517 free(last_replace);
2518 last_replace = save_replace;
2519
2520 /* Restore where we were. */
2521 openfile->edittop = edittop_save;
2522 openfile->current = current_save;
2523 openfile->current_x = current_x_save;
2524 openfile->placewewant = pww_save;
2525
2526 /* Restore case sensitivity setting. */
2527 if (!case_sens_set)
2528 UNSET(CASE_SENSITIVE);
2529
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002530#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002531 /* Restore search/replace direction. */
2532 if (backwards_search_set)
2533 SET(BACKWARDS_SEARCH);
2534#endif
2535#ifdef HAVE_REGEX_H
2536 /* Restore regular expression usage setting. */
2537 if (regexp_set)
2538 SET(USE_REGEXP);
2539#endif
2540
2541 return !canceled;
2542}
2543
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002544/* Internal (integrated) spell checking using the spell program,
2545 * filtered through the sort and uniq programs. Return NULL for normal
2546 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002547const char *do_int_speller(const char *tempfile_name)
2548{
2549 char *read_buff, *read_buff_ptr, *read_buff_word;
2550 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
2551 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
2552 pid_t pid_spell, pid_sort, pid_uniq;
2553 int spell_status, sort_status, uniq_status;
2554
2555 /* Create all three pipes up front. */
2556 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
2557 pipe(uniq_fd) == -1)
2558 return _("Could not create pipe");
2559
2560 statusbar(_("Creating misspelled word list, please wait..."));
2561
2562 /* A new process to run spell in. */
2563 if ((pid_spell = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002564 /* Child continues (i.e. future spell process). */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002565 close(spell_fd[0]);
2566
2567 /* Replace the standard input with the temp file. */
2568 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
2569 goto close_pipes_and_exit;
2570
2571 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
2572 goto close_pipes_and_exit;
2573
2574 close(tempfile_fd);
2575
2576 /* Send spell's standard output to the pipe. */
2577 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2578 goto close_pipes_and_exit;
2579
2580 close(spell_fd[1]);
2581
David Lawrence Ramsey3239ff22005-11-29 20:01:06 +00002582 /* Start the spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002583 execlp("spell", "spell", NULL);
2584
2585 /* This should not be reached if spell is found. */
2586 exit(1);
2587 }
2588
2589 /* Parent continues here. */
2590 close(spell_fd[1]);
2591
2592 /* A new process to run sort in. */
2593 if ((pid_sort = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002594 /* Child continues (i.e. future spell process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002595 * standard input with the standard output of the old pipe. */
2596 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
2597 goto close_pipes_and_exit;
2598
2599 close(spell_fd[0]);
2600
2601 /* Send sort's standard output to the new pipe. */
2602 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2603 goto close_pipes_and_exit;
2604
2605 close(sort_fd[1]);
2606
2607 /* Start the sort program. Use -f to remove mixed case. If
2608 * this isn't portable, let me know. */
2609 execlp("sort", "sort", "-f", NULL);
2610
2611 /* This should not be reached if sort is found. */
2612 exit(1);
2613 }
2614
2615 close(spell_fd[0]);
2616 close(sort_fd[1]);
2617
2618 /* A new process to run uniq in. */
2619 if ((pid_uniq = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002620 /* Child continues (i.e. future uniq process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002621 * standard input with the standard output of the old pipe. */
2622 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
2623 goto close_pipes_and_exit;
2624
2625 close(sort_fd[0]);
2626
2627 /* Send uniq's standard output to the new pipe. */
2628 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2629 goto close_pipes_and_exit;
2630
2631 close(uniq_fd[1]);
2632
2633 /* Start the uniq program; we are using PATH. */
2634 execlp("uniq", "uniq", NULL);
2635
2636 /* This should not be reached if uniq is found. */
2637 exit(1);
2638 }
2639
2640 close(sort_fd[0]);
2641 close(uniq_fd[1]);
2642
2643 /* The child process was not forked successfully. */
2644 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
2645 close(uniq_fd[0]);
2646 return _("Could not fork");
2647 }
2648
2649 /* Get the system pipe buffer size. */
2650 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
2651 close(uniq_fd[0]);
2652 return _("Could not get size of pipe buffer");
2653 }
2654
2655 /* Read in the returned spelling errors. */
2656 read_buff_read = 0;
2657 read_buff_size = pipe_buff_size + 1;
2658 read_buff = read_buff_ptr = charalloc(read_buff_size);
2659
2660 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
2661 pipe_buff_size)) > 0) {
2662 read_buff_read += bytesread;
2663 read_buff_size += pipe_buff_size;
2664 read_buff = read_buff_ptr = charealloc(read_buff,
2665 read_buff_size);
2666 read_buff_ptr += read_buff_read;
2667 }
2668
2669 *read_buff_ptr = '\0';
2670 close(uniq_fd[0]);
2671
2672 /* Process the spelling errors. */
2673 read_buff_word = read_buff_ptr = read_buff;
2674
2675 while (*read_buff_ptr != '\0') {
2676 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
2677 *read_buff_ptr = '\0';
2678 if (read_buff_word != read_buff_ptr) {
2679 if (!do_int_spell_fix(read_buff_word)) {
2680 read_buff_word = read_buff_ptr;
2681 break;
2682 }
2683 }
2684 read_buff_word = read_buff_ptr + 1;
2685 }
2686 read_buff_ptr++;
2687 }
2688
2689 /* Special case: the last word doesn't end with '\r' or '\n'. */
2690 if (read_buff_word != read_buff_ptr)
2691 do_int_spell_fix(read_buff_word);
2692
2693 free(read_buff);
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002694 search_replace_abort();
Chris Allegrettafd265af2009-02-06 03:41:02 +00002695 edit_refresh_needed = TRUE;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002696
2697 /* Process the end of the spell process. */
2698 waitpid(pid_spell, &spell_status, 0);
2699 waitpid(pid_sort, &sort_status, 0);
2700 waitpid(pid_uniq, &uniq_status, 0);
2701
2702 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
2703 return _("Error invoking \"spell\"");
2704
2705 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
2706 return _("Error invoking \"sort -f\"");
2707
2708 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
2709 return _("Error invoking \"uniq\"");
2710
2711 /* Otherwise... */
2712 return NULL;
2713
2714 close_pipes_and_exit:
2715 /* Don't leak any handles. */
2716 close(tempfile_fd);
2717 close(spell_fd[0]);
2718 close(spell_fd[1]);
2719 close(sort_fd[0]);
2720 close(sort_fd[1]);
2721 close(uniq_fd[0]);
2722 close(uniq_fd[1]);
2723 exit(1);
2724}
2725
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002726/* External (alternate) spell checking. Return NULL for normal
2727 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002728const char *do_alt_speller(char *tempfile_name)
2729{
2730 int alt_spell_status;
2731 size_t current_x_save = openfile->current_x;
2732 size_t pww_save = openfile->placewewant;
2733 ssize_t current_y_save = openfile->current_y;
2734 ssize_t lineno_save = openfile->current->lineno;
2735 pid_t pid_spell;
2736 char *ptr;
2737 static int arglen = 3;
2738 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002739#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002740 bool old_mark_set = openfile->mark_set;
2741 bool added_magicline = FALSE;
2742 /* Whether we added a magicline after filebot. */
2743 bool right_side_up = FALSE;
2744 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2745 * FALSE if (current, current_x) is. */
2746 filestruct *top, *bot;
2747 size_t top_x, bot_x;
2748 ssize_t mb_lineno_save = 0;
2749 /* We're going to close the current file, and open the output of
2750 * the alternate spell command. The line that mark_begin points
2751 * to will be freed, so we save the line number and restore it
2752 * afterwards. */
2753 size_t totsize_save = openfile->totsize;
2754 /* Our saved value of totsize, used when we spell-check a marked
2755 * selection. */
2756
2757 if (old_mark_set) {
2758 /* If the mark is on, save the number of the line it starts on,
2759 * and then turn the mark off. */
2760 mb_lineno_save = openfile->mark_begin->lineno;
2761 openfile->mark_set = FALSE;
2762 }
2763#endif
2764
Chris Allegretta181c4a92010-04-14 03:14:40 +00002765 if (openfile->totsize == 0) {
2766 statusbar(_("Finished checking spelling"));
2767 return NULL;
2768 }
2769
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002770 endwin();
2771
2772 /* Set up an argument list to pass execvp(). */
2773 if (spellargs == NULL) {
2774 spellargs = (char **)nmalloc(arglen * sizeof(char *));
2775
2776 spellargs[0] = strtok(alt_speller, " ");
2777 while ((ptr = strtok(NULL, " ")) != NULL) {
2778 arglen++;
2779 spellargs = (char **)nrealloc(spellargs, arglen *
2780 sizeof(char *));
2781 spellargs[arglen - 3] = ptr;
2782 }
2783 spellargs[arglen - 1] = NULL;
2784 }
2785 spellargs[arglen - 2] = tempfile_name;
2786
2787 /* Start a new process for the alternate speller. */
2788 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey2e2112c2005-11-29 05:39:31 +00002789 /* Start alternate spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002790 execvp(spellargs[0], spellargs);
2791
2792 /* Should not be reached, if alternate speller is found!!! */
2793 exit(1);
2794 }
2795
2796 /* If we couldn't fork, get out. */
2797 if (pid_spell < 0)
2798 return _("Could not fork");
2799
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002800#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002801 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002802 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002803 allow_pending_sigwinch(FALSE);
2804#endif
2805
2806 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002807 wait(&alt_spell_status);
2808
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00002809 /* Reenter curses mode. */
2810 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002811
2812 /* Restore the terminal to its previous state. */
2813 terminal_init();
2814
2815 /* Turn the cursor back on for sure. */
2816 curs_set(1);
2817
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002818 /* The screen might have been resized. If it has, reinitialize all
2819 * the windows based on the new screen dimensions. */
2820 window_init();
2821
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002822 if (!WIFEXITED(alt_spell_status) ||
2823 WEXITSTATUS(alt_spell_status) != 0) {
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002824 char *alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002825 char *invoke_error = _("Error invoking \"%s\"");
2826
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002827#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002828 /* Turn the mark back on if it was on before. */
2829 openfile->mark_set = old_mark_set;
2830#endif
2831
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002832 alt_spell_error =
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002833 charalloc(strlen(invoke_error) +
2834 strlen(alt_speller) + 1);
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002835 sprintf(alt_spell_error, invoke_error, alt_speller);
2836 return alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002837 }
2838
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002839#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002840 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002841 /* If the mark is on, partition the filestruct so that it
2842 * contains only the marked text; if the NO_NEWLINES flag isn't
2843 * set, keep track of whether the text will have a magicline
2844 * added when we're done correcting misspelled words; and
2845 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002846 mark_order((const filestruct **)&top, &top_x,
2847 (const filestruct **)&bot, &bot_x, &right_side_up);
2848 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002849 if (!ISSET(NO_NEWLINES))
2850 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002851
2852 /* Get the number of characters in the marked text, and subtract
2853 * it from the saved value of totsize. */
2854 totsize_save -= get_totsize(top, bot);
2855 }
2856#endif
2857
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002858 /* Replace the text of the current buffer with the spell-checked
2859 * text. */
2860 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002861
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002862#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002863 if (old_mark_set) {
2864 filestruct *top_save = openfile->fileage;
2865
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002866 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2867 * added a magicline, remove it now. */
2868 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002869 remove_magicline();
2870
2871 /* Put the beginning and the end of the mark at the beginning
2872 * and the end of the spell-checked text. */
2873 if (openfile->fileage == openfile->filebot)
2874 bot_x += top_x;
2875 if (right_side_up) {
2876 openfile->mark_begin_x = top_x;
2877 current_x_save = bot_x;
2878 } else {
2879 current_x_save = top_x;
2880 openfile->mark_begin_x = bot_x;
2881 }
2882
2883 /* Unpartition the filestruct so that it contains all the text
2884 * again. Note that we've replaced the marked text originally
2885 * in the partition with the spell-checked marked text in the
2886 * temp file. */
2887 unpartition_filestruct(&filepart);
2888
2889 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002890 * partition. Also add the number of characters in the
2891 * spell-checked marked text to the saved value of totsize, and
2892 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002893 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002894 totsize_save += openfile->totsize;
2895 openfile->totsize = totsize_save;
2896
2897 /* Assign mark_begin to the line where the mark began before. */
2898 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2899 current_y_save, 0);
2900 openfile->mark_begin = openfile->current;
2901
2902 /* Assign mark_begin_x to the location in mark_begin where the
2903 * mark began before, adjusted for any shortening of the
2904 * line. */
2905 openfile->mark_begin_x = openfile->current_x;
2906
2907 /* Turn the mark back on. */
2908 openfile->mark_set = TRUE;
2909 }
2910#endif
2911
2912 /* Go back to the old position, and mark the file as modified. */
2913 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2914 set_modified();
2915
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002916#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002917 /* Handle a pending SIGWINCH again. */
2918 allow_pending_sigwinch(TRUE);
2919#endif
2920
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002921 return NULL;
2922}
2923
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002924/* Spell check the current file. If an alternate spell checker is
2925 * specified, use it. Otherwise, use the internal spell checker. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002926void do_spell(void)
2927{
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002928 bool status;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002929 FILE *temp_file;
2930 char *temp = safe_tempfile(&temp_file);
2931 const char *spell_msg;
2932
Chris Allegrettaab538642010-11-12 06:22:12 +00002933 if (ISSET(RESTRICTED)) {
2934 nano_disabled_msg();
2935 return;
2936 }
2937
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002938 if (temp == NULL) {
David Lawrence Ramseyf0e3ca62006-05-03 13:11:00 +00002939 statusbar(_("Error writing temp file: %s"), strerror(errno));
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002940 return;
2941 }
2942
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002943 status =
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002944#ifndef NANO_TINY
David Lawrence Ramsey0e1df432007-01-12 02:58:12 +00002945 openfile->mark_set ? write_marked_file(temp, temp_file, TRUE,
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002946 OVERWRITE) :
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002947#endif
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002948 write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002949
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002950 if (!status) {
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002951 statusbar(_("Error writing temp file: %s"), strerror(errno));
2952 free(temp);
2953 return;
2954 }
2955
2956 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2957 do_int_speller(temp);
2958 unlink(temp);
2959 free(temp);
2960
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002961 currmenu = MMAIN;
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002962
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002963 /* If the spell-checker printed any error messages onscreen, make
2964 * sure that they're cleared off. */
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002965 total_refresh();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002966
2967 if (spell_msg != NULL) {
2968 if (errno == 0)
2969 /* Don't display an error message of "Success". */
2970 statusbar(_("Spell checking failed: %s"), spell_msg);
2971 else
2972 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2973 strerror(errno));
2974 } else
2975 statusbar(_("Finished checking spelling"));
2976}
2977#endif /* !DISABLE_SPELLER */
2978
Chris Allegretta5575bfa2014-02-24 10:18:15 +00002979#ifdef ENABLE_COLOR
2980/* Run linter. Based on alt-speller code. Return NULL for normal
2981 * termination, and the error string otherwise. */
2982void do_linter(void)
2983{
2984 char *read_buff, *read_buff_ptr, *read_buff_word, *ptr;
2985 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
2986 size_t parsesuccess = 0;
Chris Allegretta3a5f55e2014-03-04 03:55:43 +00002987 int lint_fd[2];
Chris Allegretta5575bfa2014-02-24 10:18:15 +00002988 pid_t pid_lint;
2989 int lint_status;
2990 static int arglen = 3;
2991 static char **lintargs = NULL;
Chris Allegretta5575bfa2014-02-24 10:18:15 +00002992 char *lintcopy;
2993 char *convendptr = NULL;
2994 const sc *s;
2995 lintstruct *lints = NULL, *tmplint = NULL, *curlint = NULL;
2996
2997 if (!openfile->syntax || !openfile->syntax->linter) {
2998 statusbar(_("No linter defined for this file!"));
2999 return;
3000 }
3001
3002 if (ISSET(RESTRICTED)) {
3003 nano_disabled_msg();
3004 return;
3005 }
3006
3007 if (openfile->modified) {
3008 int i = do_yesno_prompt(FALSE,
3009 _("Save modified buffer before linting?"));
3010
3011 if (i == 1) {
3012 if (do_writeout(FALSE) != TRUE) {
3013 return;
3014 }
3015 }
3016 }
3017
3018 lintcopy = mallocstrcpy(NULL, openfile->syntax->linter);
3019 /* Create pipe up front. */
3020 if (pipe(lint_fd) == -1) {
3021 statusbar(_("Could not create pipe"));
3022 return;
3023 }
3024
3025 statusbar(_("Invoking linter, please wait"));
3026
3027 /* Set up an argument list to pass execvp(). */
3028 if (lintargs == NULL) {
3029 lintargs = (char **)nmalloc(arglen * sizeof(char *));
3030
3031 lintargs[0] = strtok(lintcopy, " ");
3032 while ((ptr = strtok(NULL, " ")) != NULL) {
3033 arglen++;
3034 lintargs = (char **)nrealloc(lintargs, arglen *
3035 sizeof(char *));
3036 lintargs[arglen - 3] = ptr;
3037 }
3038 lintargs[arglen - 1] = NULL;
3039 }
3040 lintargs[arglen - 2] = openfile->filename;
3041
3042 /* A new process to run linter. */
3043 if ((pid_lint = fork()) == 0) {
3044
3045 /* Child continues (i.e. future spell process). */
3046 close(lint_fd[0]);
3047
3048 /* Send spell's standard output/err to the pipe. */
3049 if (dup2(lint_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
3050 exit(1);
3051 if (dup2(lint_fd[1], STDERR_FILENO) != STDERR_FILENO)
3052 exit(1);
3053
3054 close(lint_fd[1]);
3055
3056 /* Start the linter program; we are using $PATH. */
3057 execvp(lintargs[0], lintargs);
3058
3059 /* This should not be reached if linter is found. */
3060 exit(1);
3061 }
3062
3063 /* Parent continues here. */
3064 close(lint_fd[1]);
3065
3066 /* The child process was not forked successfully. */
3067 if (pid_lint < 0) {
3068 close(lint_fd[0]);
3069 statusbar(_("Could not fork"));
3070 return;
3071 }
3072
3073 /* Get the system pipe buffer size. */
3074 if ((pipe_buff_size = fpathconf(lint_fd[0], _PC_PIPE_BUF)) < 1) {
3075 close(lint_fd[0]);
3076 statusbar(_("Could not get size of pipe buffer"));
3077 return;
3078 }
3079
3080 /* Read in the returned spelling errors. */
3081 read_buff_read = 0;
3082 read_buff_size = pipe_buff_size + 1;
3083 read_buff = read_buff_ptr = charalloc(read_buff_size);
3084
3085 while ((bytesread = read(lint_fd[0], read_buff_ptr,
3086 pipe_buff_size)) > 0) {
3087#ifdef DEBUG
3088 fprintf(stderr, "text.c:do_linter:%d bytes (%s)\n", bytesread, read_buff_ptr);
3089#endif
3090 read_buff_read += bytesread;
3091 read_buff_size += pipe_buff_size;
3092 read_buff = read_buff_ptr = charealloc(read_buff,
3093 read_buff_size);
3094 read_buff_ptr += read_buff_read;
3095 }
3096
3097 *read_buff_ptr = '\0';
3098 close(lint_fd[0]);
3099
3100#ifdef DEBUG
3101 fprintf(stderr, "text.c:do_lint:Raw output: %s\n", read_buff);
3102#endif
3103
3104 /* Process output. */
3105 read_buff_word = read_buff_ptr = read_buff;
3106
3107 while (*read_buff_ptr != '\0') {
3108 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
3109 *read_buff_ptr = '\0';
3110 if (read_buff_word != read_buff_ptr) {
3111 char *filename = NULL, *linestr = NULL, *maybecol = NULL;
3112 char *message = mallocstrcpy(NULL, read_buff_word);
3113
3114 /* At the moment we're assuming the following formats:
3115 filenameorcategory:line:column:message (e.g. splint)
3116 filenameorcategory:line:message (e.g. pyflakes)
3117 filenameorcategory:line,col:message (e.g. pylint)
3118 This could be turnes into some scanf() based parser but ugh.
3119 */
3120 if ((filename = strtok(read_buff_word, ":")) != NULL) {
3121 if ((linestr = strtok(NULL, ":")) != NULL) {
3122 if ((maybecol = strtok(NULL, ":")) != NULL) {
3123 ssize_t tmplineno = 0, tmpcolno = 0;
3124 char *tmplinecol;
3125
3126 tmplineno = strtol(linestr, NULL, 10);
3127 if (tmplineno <= 0) {
3128 read_buff_ptr++;
3129 free(message);
3130 continue;
3131 }
3132
3133 tmpcolno = strtol(maybecol, &convendptr, 10);
3134 if (*convendptr != '\0') {
3135
3136 /* Prev field might still be line,col format */
3137 strtok(linestr, ",");
3138 if ((tmplinecol = strtok(NULL, ",")) != NULL)
3139 tmpcolno = strtol(tmplinecol, NULL, 10);
3140 }
3141
3142#ifdef DEBUG
3143 fprintf(stderr, "text.c:do_lint:Successful parse! %d:%d:%s\n", tmplineno, tmpcolno, message);
3144#endif
3145 /* Nice we have a lint message we can use */
3146 parsesuccess++;
3147 tmplint = curlint;
3148 curlint = nmalloc(sizeof(lintstruct));
3149 curlint->next = NULL;
3150 curlint->prev = tmplint;
3151 if (curlint->prev != NULL)
3152 curlint->prev->next = curlint;
3153 curlint->msg = mallocstrcpy(NULL, message);
3154 curlint->lineno = tmplineno;
3155 curlint->colno = tmpcolno;
3156 curlint->filename = mallocstrcpy(NULL, filename);
3157
3158 if (lints == NULL)
3159 lints = curlint;
3160 }
3161 }
3162 } else
3163 free(message);
3164 }
3165 read_buff_word = read_buff_ptr + 1;
3166 }
3167 read_buff_ptr++;
3168 }
3169
3170 /* Process the end of the lint process. */
3171 waitpid(pid_lint, &lint_status, 0);
3172
3173 free(read_buff);
3174
3175 if (parsesuccess == 0) {
3176 statusbar(_("Got 0 parsable lines from command: %s"), openfile->syntax->linter);
3177 return;
3178 }
3179
3180 currmenu = MLINTER;
3181 bottombars(MLINTER);
3182 tmplint = NULL;
3183 curlint = lints;
3184 while (1) {
3185 ssize_t tmpcol = 1;
3186 int kbinput;
3187 bool meta_key, func_key;
Chris Allegretta5575bfa2014-02-24 10:18:15 +00003188
3189 if (curlint->colno > 0)
3190 tmpcol = curlint->colno;
3191
3192 if (tmplint != curlint) {
3193 struct stat lintfileinfo;
3194
3195#ifndef NANO_TINY
3196 new_lint_loop:
3197 if (stat(curlint->filename, &lintfileinfo) != -1) {
3198 if (openfile->current_stat->st_ino != lintfileinfo.st_ino) {
3199 openfilestruct *tmpof = openfile;
3200 while (tmpof != openfile->next) {
3201 if (tmpof->current_stat->st_ino == lintfileinfo.st_ino)
3202 break;
3203 tmpof = tmpof->next;
3204 }
3205 if (tmpof->current_stat->st_ino != lintfileinfo.st_ino) {
3206 char *msg = charalloc(1024 + strlen(curlint->filename));
3207 int i;
3208
3209 sprintf(msg, _("This message is for unopened file %s, open it in a new buffer?"),
3210 curlint->filename);
3211 i = do_yesno_prompt(FALSE, msg);
3212 free(msg);
3213 if (i == 1) {
3214 SET(MULTIBUFFER);
3215 open_buffer(curlint->filename, FALSE);
3216 } else {
3217 char *dontwantfile = curlint->filename;
3218
3219 while (curlint != NULL && !strcmp(curlint->filename, dontwantfile))
3220 curlint = curlint->next;
3221 if (curlint == NULL) {
3222 statusbar("No more errors in un-opened filed, cancelling");
3223 break;
3224 } else
3225 goto new_lint_loop;
3226 }
3227 } else
3228 openfile = tmpof;
3229 }
3230 }
Benno Schulenberge4c34c32014-03-17 14:15:57 +00003231#endif /* !NANO_TINY */
Chris Allegretta5575bfa2014-02-24 10:18:15 +00003232 do_gotolinecolumn(curlint->lineno, tmpcol, FALSE, FALSE, FALSE, FALSE);
3233 titlebar(NULL);
3234 edit_refresh();
3235 statusbar(curlint->msg);
3236 bottombars(MLINTER);
3237 }
3238
3239 kbinput = get_kbinput(bottomwin, &meta_key, &func_key);
3240 s = get_shortcut(currmenu, &kbinput, &meta_key, &func_key);
3241 tmplint = curlint;
3242
3243 if (!s)
3244 continue;
3245 else if (s->scfunc == do_cancel)
3246 break;
3247 else if (s->scfunc == do_help_void) {
3248 tmplint = NULL;
3249 do_help_void();
3250 } else if (s->scfunc == do_page_down) {
3251 if (curlint->next != NULL)
3252 curlint = curlint->next;
3253 else {
3254 statusbar(_("At last message"));
3255 continue;
3256 }
3257 } else if (s->scfunc == do_page_up) {
3258 if (curlint->prev != NULL)
3259 curlint = curlint->prev;
3260 else {
3261 statusbar(_("At first message"));
3262 continue;
3263 }
3264 }
3265 }
3266 for (tmplint = lints; tmplint != NULL; tmplint = tmplint->next) {
3267 free(tmplint->msg);
3268 free(tmplint->filename);
3269 free(tmplint);
3270 }
3271 blank_statusbar();
3272 currmenu = MMAIN;
3273 display_main_list();
3274}
3275#endif /* ENABLE_COLOR */
3276
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00003277#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00003278/* Our own version of "wc". Note that its character counts are in
3279 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00003280void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003281{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00003282 size_t words = 0, chars = 0;
Chris Allegretta8b6461f2008-05-31 23:09:40 +00003283 ssize_t nlines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003284 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003285 size_t pww_save = openfile->placewewant;
3286 filestruct *current_save = openfile->current;
3287 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003288 filestruct *top, *bot;
3289 size_t top_x, bot_x;
3290
3291 if (old_mark_set) {
3292 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00003293 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003294 mark_order((const filestruct **)&top, &top_x,
3295 (const filestruct **)&bot, &bot_x, NULL);
3296 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003297 openfile->mark_set = FALSE;
3298 }
3299
3300 /* Start at the top of the file. */
3301 openfile->current = openfile->fileage;
3302 openfile->current_x = 0;
3303 openfile->placewewant = 0;
3304
3305 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003306 * part of a word, as "wc -w" does), without updating the screen,
3307 * until we reach the end of the file, incrementing the total word
3308 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003309 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00003310 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003311 if (do_next_word(TRUE, FALSE))
3312 words++;
3313 }
3314
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003315 /* Get the total line and character counts, as "wc -l" and "wc -c"
3316 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003317 if (old_mark_set) {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00003318 nlines = openfile->filebot->lineno -
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00003319 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003320 chars = get_totsize(openfile->fileage, openfile->filebot);
3321
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003322 /* Unpartition the filestruct so that it contains all the text
3323 * again, and turn the mark back on. */
3324 unpartition_filestruct(&filepart);
3325 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003326 } else {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00003327 nlines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003328 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003329 }
3330
3331 /* Restore where we were. */
3332 openfile->current = current_save;
3333 openfile->current_x = current_x_save;
3334 openfile->placewewant = pww_save;
3335
David Lawrence Ramsey72936852005-07-25 03:47:08 +00003336 /* Display the total word, line, and character counts on the
3337 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00003338 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
Chris Allegretta8b6461f2008-05-31 23:09:40 +00003339 _("In Selection: ") : "", (unsigned long)words, (long)nlines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00003340 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00003341}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00003342#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003343
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00003344/* Get verbatim input. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003345void do_verbatim_input(void)
3346{
3347 int *kbinput;
3348 size_t kbinput_len, i;
3349 char *output;
3350
David Lawrence Ramseyf451d6a2006-05-27 16:02:48 +00003351 /* TRANSLATORS: This is displayed when the next keystroke will be
3352 * inserted verbatim. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003353 statusbar(_("Verbatim Input"));
3354
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003355 /* Read in all the verbatim characters. */
3356 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
3357
David Lawrence Ramseya620e682006-05-27 18:19:03 +00003358 /* If constant cursor position display is on, make sure the current
3359 * cursor position will be properly displayed on the statusbar.
3360 * Otherwise, blank the statusbar. */
3361 if (ISSET(CONST_UPDATE))
3362 do_cursorpos(TRUE);
3363 else {
3364 blank_statusbar();
3365 wnoutrefresh(bottomwin);
3366 }
David Lawrence Ramsey6fb66892006-05-27 17:39:19 +00003367
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003368 /* Display all the verbatim characters at once, not filtering out
3369 * control characters. */
3370 output = charalloc(kbinput_len + 1);
3371
3372 for (i = 0; i < kbinput_len; i++)
3373 output[i] = (char)kbinput[i];
3374 output[i] = '\0';
3375
David Lawrence Ramseyad36bdc2006-12-02 17:22:21 +00003376 free(kbinput);
3377
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00003378 do_output(output, kbinput_len, TRUE);
3379
3380 free(output);
3381}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00003382