blob: f4892c35d691220830780afba09685b4b7c6a2e6 [file] [log] [blame]
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001/* $Id$ */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002/**************************************************************************
David Lawrence Ramsey4005fc62005-10-08 06:12:41 +00003 * text.c *
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00004 * *
David Lawrence Ramseyd8a1d372007-10-11 05:01:32 +00005 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 *
6 * 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{
69 bool do_refresh = FALSE;
70 /* Do we have to call edit_refresh(), or can we get away with
David Lawrence Ramsey75e9dfe2006-05-18 17:50:08 +000071 * just update_line()? */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000072
Chris Allegretta07fcc4c2008-07-10 20:13:04 +000073#ifndef NANO_TINY
Chris Allegretta14c86202008-08-03 04:48:05 +000074 update_undo(DEL);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +000075#endif
76
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000077 assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data));
78
79 openfile->placewewant = xplustabs();
80
81 if (openfile->current->data[openfile->current_x] != '\0') {
82 int char_buf_len = parse_mbchar(openfile->current->data +
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +000083 openfile->current_x, NULL, NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000084 size_t line_len = strlen(openfile->current->data +
85 openfile->current_x);
86
87 assert(openfile->current_x < strlen(openfile->current->data));
88
89 /* Let's get dangerous. */
90 charmove(&openfile->current->data[openfile->current_x],
91 &openfile->current->data[openfile->current_x +
92 char_buf_len], line_len - char_buf_len + 1);
93
94 null_at(&openfile->current->data, openfile->current_x +
95 line_len - char_buf_len);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000096#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000097 if (openfile->mark_set && openfile->mark_begin ==
98 openfile->current && openfile->current_x <
99 openfile->mark_begin_x)
100 openfile->mark_begin_x -= char_buf_len;
101#endif
102 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000103 } else if (openfile->current != openfile->filebot) {
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000104 filestruct *foo = openfile->current->next;
105
106 assert(openfile->current_x == strlen(openfile->current->data));
107
108 /* If we're deleting at the end of a line, we need to call
109 * edit_refresh(). */
110 if (openfile->current->data[openfile->current_x] == '\0')
111 do_refresh = TRUE;
112
113 openfile->current->data = charealloc(openfile->current->data,
114 openfile->current_x + strlen(foo->data) + 1);
115 strcpy(openfile->current->data + openfile->current_x,
116 foo->data);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000117#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000118 if (openfile->mark_set && openfile->mark_begin ==
119 openfile->current->next) {
120 openfile->mark_begin = openfile->current;
121 openfile->mark_begin_x += openfile->current_x;
122 }
123#endif
124 if (openfile->filebot == foo)
125 openfile->filebot = openfile->current;
126
127 unlink_node(foo);
128 delete_node(foo);
129 renumber(openfile->current);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000130 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000131
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000132 /* If the NO_NEWLINES flag isn't set, and text has been added to
133 * the magicline as a result of deleting at the end of the line
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000134 * before filebot, add a new magicline. */
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000135 if (!ISSET(NO_NEWLINES) && openfile->current ==
136 openfile->filebot && openfile->current->data[0] != '\0')
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000137 new_magicline();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000138 } else
139 return;
140
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000141 set_modified();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000142
143#ifdef ENABLE_COLOR
144 /* If color syntaxes are available and turned on, we need to call
145 * edit_refresh(). */
146 if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX))
147 do_refresh = TRUE;
148#endif
149
150 if (do_refresh)
151 edit_refresh();
152 else
153 update_line(openfile->current, openfile->current_x);
154}
155
David Lawrence Ramsey5b7b3e32005-12-31 21:22:54 +0000156/* Backspace over one character. That is, move the cursor left one
David Lawrence Ramsey84d22e32006-11-08 13:05:50 +0000157 * character, and then delete the character under the cursor. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000158void do_backspace(void)
159{
160 if (openfile->current != openfile->fileage ||
161 openfile->current_x > 0) {
David Lawrence Ramsey1c3bfa92005-09-13 04:53:44 +0000162 do_left();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000163 do_delete();
164 }
165}
166
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000167/* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number
168 * of spaces that a tab would normally take up. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000169void do_tab(void)
170{
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000171#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000172 if (ISSET(TABS_TO_SPACES)) {
173 char *output;
David Lawrence Ramsey90b07fc2005-10-07 15:57:48 +0000174 size_t output_len = 0, new_pww = xplustabs();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000175
176 do {
177 new_pww++;
178 output_len++;
179 } while (new_pww % tabsize != 0);
180
181 output = charalloc(output_len + 1);
182
183 charset(output, ' ', output_len);
184 output[output_len] = '\0';
185
186 do_output(output, output_len, TRUE);
187
188 free(output);
189 } else {
190#endif
191 do_output("\t", 1, TRUE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000192#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000193 }
194#endif
195}
196
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000197#ifndef NANO_TINY
David Lawrence Ramseye44cd2d2007-01-11 22:54:55 +0000198/* Indent or unindent the current line (or, if the mark is on, all lines
199 * covered by the mark) len columns, depending on whether len is
200 * positive or negative. If the TABS_TO_SPACES flag is set, indent or
201 * unindent by len spaces. Otherwise, indent or unindent by (len /
202 * tabsize) tabs and (len % tabsize) spaces. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000203void do_indent(ssize_t cols)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000204{
205 bool indent_changed = FALSE;
206 /* Whether any indenting or unindenting was done. */
207 bool unindent = FALSE;
208 /* Whether we're unindenting text. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000209 char *line_indent = NULL;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000210 /* The text added to each line in order to indent it. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000211 size_t line_indent_len = 0;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000212 /* The length of the text added to each line in order to indent
213 * it. */
214 filestruct *top, *bot, *f;
215 size_t top_x, bot_x;
216
217 assert(openfile->current != NULL && openfile->current->data != NULL);
218
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000219 /* If cols is zero, get out. */
220 if (cols == 0)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000221 return;
222
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000223 /* If cols is negative, make it positive and set unindent to
224 * TRUE. */
225 if (cols < 0) {
226 cols = -cols;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000227 unindent = TRUE;
228 /* Otherwise, we're indenting, in which case the file will always be
229 * modified, so set indent_changed to TRUE. */
230 } else
231 indent_changed = TRUE;
232
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000233 /* If the mark is on, use all lines covered by the mark. */
234 if (openfile->mark_set)
235 mark_order((const filestruct **)&top, &top_x,
236 (const filestruct **)&bot, &bot_x, NULL);
237 /* Otherwise, use the current line. */
238 else {
239 top = openfile->current;
240 bot = top;
241 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000242
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000243 if (!unindent) {
244 /* Set up the text we'll be using as indentation. */
245 line_indent = charalloc(cols + 1);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000246
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000247 if (ISSET(TABS_TO_SPACES)) {
248 /* Set the indentation to cols spaces. */
249 charset(line_indent, ' ', cols);
250 line_indent_len = cols;
251 } else {
252 /* Set the indentation to (cols / tabsize) tabs and (cols %
253 * tabsize) spaces. */
254 size_t num_tabs = cols / tabsize;
255 size_t num_spaces = cols % tabsize;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000256
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000257 charset(line_indent, '\t', num_tabs);
258 charset(line_indent + num_tabs, ' ', num_spaces);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000259
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000260 line_indent_len = num_tabs + num_spaces;
261 }
262
263 line_indent[line_indent_len] = '\0';
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000264 }
265
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000266 /* Go through each line of the text. */
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000267 for (f = top; f != bot->next; f = f->next) {
268 size_t line_len = strlen(f->data);
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000269 size_t indent_len = indent_length(f->data);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000270
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000271 if (!unindent) {
272 /* If we're indenting, add the characters in line_indent to
273 * the beginning of the non-whitespace text of this line. */
274 f->data = charealloc(f->data, line_len +
275 line_indent_len + 1);
276 charmove(&f->data[indent_len + line_indent_len],
277 &f->data[indent_len], line_len - indent_len + 1);
278 strncpy(f->data + indent_len, line_indent, line_indent_len);
279 openfile->totsize += line_indent_len;
280
281 /* Keep track of the change in the current line. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000282 if (openfile->mark_set && f == openfile->mark_begin &&
283 openfile->mark_begin_x >= indent_len)
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000284 openfile->mark_begin_x += line_indent_len;
285
286 if (f == openfile->current && openfile->current_x >=
287 indent_len)
288 openfile->current_x += line_indent_len;
289
290 /* If the NO_NEWLINES flag isn't set, and this is the
291 * magicline, add a new magicline. */
292 if (!ISSET(NO_NEWLINES) && f == openfile->filebot)
293 new_magicline();
294 } else {
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000295 size_t indent_col = strnlenpt(f->data, indent_len);
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000296 /* The length in columns of the indentation on this
297 * line. */
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000298
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000299 if (cols <= indent_col) {
300 size_t indent_new = actual_x(f->data, indent_col -
301 cols);
David Lawrence Ramsey5bb77272006-05-06 14:37:33 +0000302 /* The length of the indentation remaining on
303 * this line after we unindent. */
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000304 size_t indent_shift = indent_len - indent_new;
David Lawrence Ramsey5bb77272006-05-06 14:37:33 +0000305 /* The change in the indentation on this line
306 * after we unindent. */
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000307
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000308 /* If we're unindenting, and there's at least cols
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000309 * columns' worth of indentation at the beginning of the
310 * non-whitespace text of this line, remove it. */
311 charmove(&f->data[indent_new], &f->data[indent_len],
312 line_len - indent_shift - indent_new + 1);
313 null_at(&f->data, line_len - indent_shift + 1);
314 openfile->totsize -= indent_shift;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000315
David Lawrence Ramsey2e8fac62006-04-29 15:44:58 +0000316 /* Keep track of the change in the current line. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000317 if (openfile->mark_set && f == openfile->mark_begin &&
David Lawrence Ramseyeb4f90e2006-05-05 14:22:42 +0000318 openfile->mark_begin_x > indent_new) {
319 if (openfile->mark_begin_x <= indent_len)
320 openfile->mark_begin_x = indent_new;
321 else
322 openfile->mark_begin_x -= indent_shift;
323 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000324
David Lawrence Ramseyac37b042006-05-03 12:59:05 +0000325 if (f == openfile->current && openfile->current_x >
David Lawrence Ramseyeb4f90e2006-05-05 14:22:42 +0000326 indent_new) {
327 if (openfile->current_x <= indent_len)
328 openfile->current_x = indent_new;
329 else
330 openfile->current_x -= indent_shift;
331 }
David Lawrence Ramsey7194a612006-04-29 16:11:21 +0000332
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000333 /* We've unindented, so set indent_changed to TRUE. */
334 if (!indent_changed)
335 indent_changed = TRUE;
336 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000337 }
338 }
339
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000340 if (!unindent)
David Lawrence Ramsey25456862006-05-05 15:43:52 +0000341 /* Clean up. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000342 free(line_indent);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000343
344 if (indent_changed) {
345 /* Mark the file as modified. */
346 set_modified();
347
348 /* Update the screen. */
349 edit_refresh();
350 }
351}
352
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000353/* Indent the current line, or all lines covered by the mark if the mark
354 * is on, tabsize columns. */
355void do_indent_void(void)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000356{
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000357 do_indent(tabsize);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000358}
359
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000360/* Unindent the current line, or all lines covered by the mark if the
361 * mark is on, tabsize columns. */
362void do_unindent(void)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000363{
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000364 do_indent(-tabsize);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000365}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000366
367/* Undo the last thing(s) we did */
368void do_undo(void)
369{
370 undo *u = openfile->current_undo;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000371 filestruct *f = openfile->current, *t;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000372 int len = 0;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000373 char *undidmsg, *data;
Chris Allegretta14c86202008-08-03 04:48:05 +0000374 filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000375
376 if (!u) {
377 statusbar(_("Nothing in undo buffer!"));
378 return;
379 }
380
381
382 if (u->lineno <= f->lineno)
383 for (; f->prev != NULL && f->lineno != u->lineno; f = f->prev)
384 ;
385 else
386 for (; f->next != NULL && f->lineno != u->lineno; f = f->next)
387 ;
388 if (f->lineno != u->lineno) {
389 statusbar(_("Couldnt match current undo line"));
390 return;
391 }
392#ifdef DEBUG
393 fprintf(stderr, "data we're about to undo = \"%s\"\n", f->data);
394 fprintf(stderr, "Undo running for type %d\n", u->type);
395#endif
396
Chris Allegrettafa406942008-07-13 16:44:19 +0000397 openfile->current_x = u->begin;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000398 switch(u->type) {
399 case ADD:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000400 undidmsg = _("text add");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000401 len = strlen(f->data) - strlen(u->strdata) + 1;
402 data = charalloc(len);
403 strncpy(data, f->data, u->begin);
404 strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]);
405 free(f->data);
406 f->data = data;
407 break;
408 case DEL:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000409 undidmsg = _("text delete");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000410 len = strlen(f->data) + strlen(u->strdata) + 1;
411 data = charalloc(len);
412
413 strncpy(data, f->data, u->begin);
414 strcpy(&data[u->begin], u->strdata);
415 strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]);
416 free(f->data);
417 f->data = data;
Chris Allegretta0b499d42008-07-14 07:18:22 +0000418 if (u->xflags == UNDO_DEL_BACKSPACE)
419 openfile->current_x += strlen(u->strdata);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000420 break;
421 case SPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000422 undidmsg = _("line split");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000423 free(f->data);
424 f->data = mallocstrcpy(NULL, u->strdata);
425 if (f->next != NULL) {
426 filestruct *tmp = f->next;
427 unlink_node(tmp);
428 delete_node(tmp);
429 }
Chris Allegretta0b499d42008-07-14 07:18:22 +0000430 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000431 break;
432 case UNSPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000433 undidmsg = _("line join");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000434 t = make_new_node(f);
435 t->data = mallocstrcpy(NULL, u->strdata);
436 data = mallocstrncpy(NULL, f->data, u->begin);
437 data[u->begin] = '\0';
438 free(f->data);
439 f->data = data;
440 splice_node(f, t, f->next);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000441 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000442 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000443 case CUT:
444 case CUTTOEND:
445 undidmsg = _("text cut");
446 cutbuffer = copy_filestruct(u->cutbuffer);
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000447
448 /* Compute cutbottom for the uncut using out copy */
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000449 for (cutbottom = cutbuffer; cutbottom->next != NULL; cutbottom = cutbottom->next)
450 ;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000451
452 /* Get to where we need to uncut from */
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000453 if (u->mark_set && u->mark_begin_lineno < u->lineno)
454 do_gotolinecolumn(u->mark_begin_lineno, u->mark_begin_x+1, FALSE, FALSE, FALSE, FALSE);
455 else
456 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000457
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000458 do_uncut_text();
459 free_filestruct(cutbuffer);
Chris Allegretta8f761122008-08-01 06:52:15 +0000460 cutbuffer = NULL;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000461 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000462 case INSERT:
463 undidmsg = _("text insert");
464 cutbuffer = NULL;
465 cutbottom = NULL;
466 /* When we updated mark_begin_lineno in update_undo, it was effectively how many line
467 were inserted due to being partitioned before read_file was called. So we
468 add its value here */
469 openfile->mark_begin = fsfromline(u->lineno + u->mark_begin_lineno - 1);
470 openfile->mark_begin_x = 0;
471 openfile->mark_set = TRUE;
472 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
473 cut_marked();
474 u->cutbuffer = cutbuffer;
475 u->cutbottom = cutbottom;
476 cutbuffer = oldcutbuffer;
477 cutbottom = oldcutbottom;
478 openfile->mark_set = FALSE;
479 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000480 case REPLACE:
481 undidmsg = _("text replace");
482 data = u->strdata;
483 u->strdata = f->data;
484 f->data = data;
485 break;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000486 default:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000487 undidmsg = _("wtf?");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000488 break;
489
490 }
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000491 do_gotolinecolumn(u->lineno, u->begin, FALSE, FALSE, FALSE, TRUE);
492 statusbar(_("Undid action (%s)"), undidmsg);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000493 openfile->current_undo = openfile->current_undo->next;
Chris Allegretta6f681c12008-08-08 03:02:03 +0000494 openfile->last_action = OTHER;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000495}
496
497void do_redo(void)
498{
499 undo *u = openfile->undotop;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000500 filestruct *f = openfile->current, *t;
Chris Allegrettaea577872008-08-03 20:19:42 +0000501 filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000502 int len = 0, i;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000503 char *undidmsg, *data;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000504
505 for (; u != NULL && u->next != openfile->current_undo; u = u->next)
506 ;
507 if (!u) {
508 statusbar(_("Nothing to re-do!"));
509 return;
510 }
511 if (u->next != openfile->current_undo) {
512 statusbar(_("Can't find previous undo to re-do, argh"));
513 return;
514 }
515
516 if (u->lineno <= f->lineno)
517 for (; f->prev != NULL && f->lineno != u->lineno; f = f->prev)
518 ;
519 else
520 for (; f->next != NULL && f->lineno != u->lineno; f = f->next)
521 ;
522 if (f->lineno != u->lineno) {
523 statusbar(_("Couldnt match current undo line"));
524 return;
525 }
526#ifdef DEBUG
527 fprintf(stderr, "data we're about to redo = \"%s\"\n", f->data);
528 fprintf(stderr, "Redo running for type %d\n", u->type);
529#endif
530
531 switch(u->type) {
532 case ADD:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000533 undidmsg = _("text add");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000534 len = strlen(f->data) + strlen(u->strdata) + 1;
535 data = charalloc(len);
Chris Allegretta1f37c452008-08-01 04:11:57 +0000536 strncpy(data, f->data, u->begin);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000537 strcpy(&data[u->begin], u->strdata);
538 strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]);
539 free(f->data);
540 f->data = data;
541 break;
542 case DEL:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000543 undidmsg = _("text delete");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000544 len = strlen(f->data) + strlen(u->strdata) + 1;
545 data = charalloc(len);
546 strncpy(data, f->data, u->begin);
547 strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]);
548 free(f->data);
549 f->data = data;
550 break;
551 case SPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000552 undidmsg = _("line split");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000553 t = make_new_node(f);
Chris Allegretta5c27d722008-08-09 09:28:07 +0000554 t->data = mallocstrcpy(NULL, &u->strdata[u->begin]);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000555 data = mallocstrncpy(NULL, f->data, u->begin);
556 data[u->begin] = '\0';
557 free(f->data);
558 f->data = data;
559 splice_node(f, t, f->next);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000560 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000561 break;
562 case UNSPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000563 undidmsg = _("line join");
Chris Allegretta0b499d42008-07-14 07:18:22 +0000564 len = strlen(f->data) + strlen(u->strdata + 1);
565 data = charalloc(len);
566 strcpy(data, f->data);
567 strcat(data, u->strdata);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000568 free(f->data);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000569 f->data = data;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000570 if (f->next != NULL) {
571 filestruct *tmp = f->next;
572 unlink_node(tmp);
573 delete_node(tmp);
574 }
Chris Allegretta0b499d42008-07-14 07:18:22 +0000575 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000576 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000577 case CUT:
578 case CUTTOEND:
579 undidmsg = _("line cut");
580 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
581 openfile->mark_set = u->mark_set;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000582 if (cutbuffer)
583 free(cutbuffer);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000584 cutbuffer = NULL;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000585
586 /* Move ahead the same # lines we had if a marked cut */
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000587 if (u->mark_set) {
588 for (i = 1, t = openfile->fileage; i != u->mark_begin_lineno; i++)
589 t = t->next;
590 openfile->mark_begin = t;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000591 } else if (!u->to_end) {
592 /* Here we have a regular old potentially multi-line ^K cut. We'll
593 need to trick nano into thinking it's a marked cut to cut more
594 than one line again */
595#ifdef DEBUG
596 fprintf(stderr, "Undoing multi-^K cut, u->linescut = %d\n", u->linescut);
597#endif
598 for (i = 0, t = openfile->current; i < u->linescut; i++) {
599
600#ifdef DEBUG
601 fprintf(stderr, "Advancing, lineno = %d, data = \"%s\"\n", t->lineno, t->data);
602#endif
603 t = t->next;
604 }
605 openfile->mark_begin = t;
606 openfile->mark_begin_x = 0;
607 openfile->mark_set = TRUE;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000608 }
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000609
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000610 openfile->mark_begin_x = u->mark_begin_x;
Chris Allegretta91a18622008-08-01 03:50:20 +0000611 do_cut_text(FALSE, u->to_end, TRUE);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000612 openfile->mark_set = FALSE;
613 openfile->mark_begin = NULL;
614 openfile->mark_begin_x = 0;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000615 edit_refresh();
616 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000617 case REPLACE:
618 undidmsg = _("text replace");
619 data = u->strdata;
620 u->strdata = f->data;
621 f->data = data;
622 break;
Chris Allegrettaea577872008-08-03 20:19:42 +0000623 case INSERT:
624 undidmsg = _("text insert");
625 cutbuffer = u->cutbuffer;
626 cutbottom = u->cutbottom;
627 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
628 do_uncut_text();
629 cutbuffer = oldcutbuffer;
630 cutbottom = oldcutbottom;
631 break;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000632 default:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000633 undidmsg = _("wtf?");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000634 break;
635
636 }
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000637 do_gotolinecolumn(u->lineno, u->begin, FALSE, FALSE, FALSE, TRUE);
638 statusbar(_("Redid action (%s)"), undidmsg);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000639
640 openfile->current_undo = u;
Chris Allegretta6f681c12008-08-08 03:02:03 +0000641 openfile->last_action = OTHER;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000642
643}
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000644#endif /* !NANO_TINY */
645
David Lawrence Ramseyb0e04c02005-12-08 07:24:54 +0000646/* Someone hits Enter *gasp!* */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000647void do_enter(void)
648{
649 filestruct *newnode = make_new_node(openfile->current);
650 size_t extra = 0;
651
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000652 assert(openfile->current != NULL && xopenfile->current->data != NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000653
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000654#ifndef NANO_TINY
Chris Allegretta14c86202008-08-03 04:48:05 +0000655 update_undo(SPLIT);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000656
657
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000658 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
659 if (ISSET(AUTOINDENT)) {
660 /* If we are breaking the line in the indentation, the new
661 * indentation should have only current_x characters, and
662 * current_x should not change. */
663 extra = indent_length(openfile->current->data);
664 if (extra > openfile->current_x)
665 extra = openfile->current_x;
666 }
667#endif
668 newnode->data = charalloc(strlen(openfile->current->data +
669 openfile->current_x) + extra + 1);
670 strcpy(&newnode->data[extra], openfile->current->data +
671 openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000672#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000673 if (ISSET(AUTOINDENT)) {
674 strncpy(newnode->data, openfile->current->data, extra);
675 openfile->totsize += mbstrlen(newnode->data);
676 }
677#endif
678 null_at(&openfile->current->data, openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000679#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000680 if (openfile->mark_set && openfile->current ==
681 openfile->mark_begin && openfile->current_x <
682 openfile->mark_begin_x) {
683 openfile->mark_begin = newnode;
684 openfile->mark_begin_x += extra - openfile->current_x;
685 }
686#endif
687 openfile->current_x = extra;
688
689 if (openfile->current == openfile->filebot)
690 openfile->filebot = newnode;
691 splice_node(openfile->current, newnode,
692 openfile->current->next);
693
694 renumber(openfile->current);
695 openfile->current = newnode;
696
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000697 openfile->totsize++;
698 set_modified();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000699
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000700 openfile->placewewant = xplustabs();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000701
702 edit_refresh();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000703}
704
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000705#ifndef NANO_TINY
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000706/* Send a SIGKILL (unconditional kill) to the forked process in
707 * execute_command(). */
David Lawrence Ramsey8befda62005-12-06 19:39:56 +0000708RETSIGTYPE cancel_command(int signal)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000709{
710 if (kill(pid, SIGKILL) == -1)
711 nperror("kill");
712}
713
David Lawrence Ramsey0ed71712005-11-08 23:09:47 +0000714/* Execute command in a shell. Return TRUE on success. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000715bool execute_command(const char *command)
716{
717 int fd[2];
718 FILE *f;
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000719 char *shellenv;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000720 struct sigaction oldaction, newaction;
721 /* Original and temporary handlers for SIGINT. */
722 bool sig_failed = FALSE;
723 /* Did sigaction() fail without changing the signal handlers? */
724
725 /* Make our pipes. */
726 if (pipe(fd) == -1) {
727 statusbar(_("Could not pipe"));
728 return FALSE;
729 }
730
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000731 /* Check $SHELL for the shell to use. If it isn't set, use
David Lawrence Ramsey1932dfb2005-11-29 05:52:49 +0000732 * /bin/sh. Note that $SHELL should contain only a path, with no
733 * arguments. */
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000734 shellenv = getenv("SHELL");
735 if (shellenv == NULL)
736 shellenv = "/bin/sh";
737
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000738 /* Fork a child. */
739 if ((pid = fork()) == 0) {
740 close(fd[0]);
741 dup2(fd[1], fileno(stdout));
742 dup2(fd[1], fileno(stderr));
743
744 /* If execl() returns at all, there was an error. */
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000745 execl(shellenv, tail(shellenv), "-c", command, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000746 exit(0);
747 }
748
David Lawrence Ramseyc838a4c2006-04-26 18:33:50 +0000749 /* Continue as parent. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000750 close(fd[1]);
751
752 if (pid == -1) {
753 close(fd[0]);
754 statusbar(_("Could not fork"));
755 return FALSE;
756 }
757
758 /* Before we start reading the forked command's output, we set
759 * things up so that Ctrl-C will cancel the new process. */
760
761 /* Enable interpretation of the special control keys so that we get
762 * SIGINT when Ctrl-C is pressed. */
763 enable_signals();
764
765 if (sigaction(SIGINT, NULL, &newaction) == -1) {
766 sig_failed = TRUE;
767 nperror("sigaction");
768 } else {
769 newaction.sa_handler = cancel_command;
770 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
771 sig_failed = TRUE;
772 nperror("sigaction");
773 }
774 }
775
776 /* Note that now oldaction is the previous SIGINT signal handler,
777 * to be restored later. */
778
779 f = fdopen(fd[0], "rb");
780 if (f == NULL)
781 nperror("fdopen");
782
Chris Allegretta14c86202008-08-03 04:48:05 +0000783 read_file(f, "stdin", TRUE);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000784
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000785 if (wait(NULL) == -1)
786 nperror("wait");
787
788 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
789 nperror("sigaction");
790
David Lawrence Ramsey8b9c91b2007-12-18 01:28:53 +0000791 /* Restore the terminal to its previous state. In the process,
792 * disable interpretation of the special control keys so that we can
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000793 * use Ctrl-C for other things. */
David Lawrence Ramsey8b9c91b2007-12-18 01:28:53 +0000794 terminal_init();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000795
796 return TRUE;
797}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000798
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000799/* Add a new undo struct to the top of the current pile */
Chris Allegretta14c86202008-08-03 04:48:05 +0000800void add_undo(undo_type current_action)
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000801{
Chris Allegretta8f761122008-08-01 06:52:15 +0000802 undo *u;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000803 char *data;
Chris Allegretta14c86202008-08-03 04:48:05 +0000804 openfilestruct *fs = openfile;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000805
Chris Allegretta8f761122008-08-01 06:52:15 +0000806 /* Ugh, if we were called while cutting not-to-end, non-marked and on the same lineno,
807 we need to abort here */
808 u = fs->current_undo;
Chris Allegretta80ea9c52008-08-09 10:08:33 +0000809 if (current_action == CUT && u && u->type == CUT
810 && !u->mark_set && u->lineno == fs->current->lineno)
Chris Allegretta8f761122008-08-01 06:52:15 +0000811 return;
812
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000813 /* Blow away the old undo stack if we are starting from the middle */
Chris Allegretta91a18622008-08-01 03:50:20 +0000814 while (fs->undotop != NULL && fs->undotop != fs->current_undo) {
815 undo *u2 = fs->undotop;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000816 fs->undotop = fs->undotop->next;
Chris Allegretta91a18622008-08-01 03:50:20 +0000817 if (u2->strdata != NULL)
818 free(u2->strdata);
Chris Allegretta6f681c12008-08-08 03:02:03 +0000819 if (u2->cutbuffer)
Chris Allegrettaea577872008-08-03 20:19:42 +0000820 free_filestruct(u2->cutbuffer);
Chris Allegretta91a18622008-08-01 03:50:20 +0000821 free(u2);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000822 }
823
Chris Allegretta8f761122008-08-01 06:52:15 +0000824 /* Allocate and initialize a new undo type */
825 u = nmalloc(sizeof(undo));
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000826 u->type = current_action;
827 u->lineno = fs->current->lineno;
828 u->begin = fs->current_x;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000829 u->next = fs->undotop;
830 fs->undotop = u;
831 fs->current_undo = u;
Chris Allegretta91a18622008-08-01 03:50:20 +0000832 u->strdata = NULL;
833 u->cutbuffer = NULL;
834 u->cutbottom = NULL;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000835 u->mark_begin_lineno = 0;
836 u->mark_begin_x = 0;
837 u->linescut = 0;
Chris Allegretta91a18622008-08-01 03:50:20 +0000838 u->xflags = 0;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000839
840 switch (u->type) {
841 /* We need to start copying data into the undo buffer or we wont be able
842 to restore it later */
843 case ADD:
844 data = charalloc(2);
845 data[0] = fs->current->data[fs->current_x];
846 data[1] = '\0';
847 u->strdata = data;
848 break;
849 case DEL:
850 if (u->begin != strlen(fs->current->data)) {
851 data = mallocstrncpy(NULL, &fs->current->data[u->begin], 2);
852 data[1] = '\0';
853 u->strdata = data;
854 break;
855 }
856 /* Else purposely fall into unsplit code */
857 current_action = u->type = UNSPLIT;
858 case UNSPLIT:
859 if (fs->current->next) {
860 data = mallocstrcpy(NULL, fs->current->next->data);
861 u->strdata = data;
862 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000863 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000864 case INSERT:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000865 case SPLIT:
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000866 case REPLACE:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000867 data = mallocstrcpy(NULL, fs->current->data);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000868 u->strdata = data;
869 break;
870 case CUT:
871 case CUTTOEND:
872 u->mark_set = openfile->mark_set;
873 if (u->mark_set) {
874 u->mark_begin_lineno = openfile->mark_begin->lineno;
875 u->mark_begin_x = openfile->mark_begin_x;
876 }
877 u->to_end = (current_action == CUTTOEND);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000878 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000879 case UNCUT:
880 break;
881 case OTHER:
882 statusbar(_("OOPS. Tried to add unknown thing to undo struct, I'd save your work"));
883 break;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000884 }
885
886#ifdef DEBUG
887 fprintf(stderr, "fs->current->data = \"%s\", current_x = %d, u->begin = %d, type = %d\n",
888 fs->current->data, fs->current_x, u->begin, current_action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000889 fprintf(stderr, "left update_add...\n");
890#endif
891 fs->last_action = current_action;
892}
893
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000894/* Update an undo item, or determine whether a new one
895 is really needed and bounce the data to add_undo
896 instead. The latter functionality just feels
897 gimmicky and may just be more hassle than
898 it's worth, so it should be axed if needed. */
Chris Allegretta14c86202008-08-03 04:48:05 +0000899void update_undo(undo_type action)
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000900{
901 undo *u;
902 char *data;
903 int len = 0;
Chris Allegretta14c86202008-08-03 04:48:05 +0000904 openfilestruct *fs = openfile;
Chris Allegretta91a18622008-08-01 03:50:20 +0000905
906#ifdef DEBUG
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000907 fprintf(stderr, "action = %d, fs->last_action = %d, openfile->current->lineno = %d",
908 action, fs->last_action, openfile->current->lineno);
909 if (fs->current_undo)
910 fprintf(stderr, "fs->current_undo->lineno = %d\n", fs->current_undo->lineno);
911 else
912 fprintf(stderr, "\n");
Chris Allegretta91a18622008-08-01 03:50:20 +0000913#endif
914
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000915 /* Change to an add if we're not using the same undo struct
916 that we should be using */
917 if (action != fs->last_action
Chris Allegretta14c86202008-08-03 04:48:05 +0000918 || (action != CUT && action != CUTTOEND && action != INSERT
Chris Allegretta91a18622008-08-01 03:50:20 +0000919 && openfile->current->lineno != fs->current_undo->lineno)) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000920 add_undo(action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000921 return;
922 }
923
924 assert(fs->undotop != NULL);
925 u = fs->undotop;
926
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000927 switch (u->type) {
928 case ADD:
929#ifdef DEBUG
930 fprintf(stderr, "fs->current->data = \"%s\", current_x = %d, u->begin = %d\n",
931 fs->current->data, fs->current_x, u->begin);
932#endif
933 len = strlen(u->strdata) + 2;
934 data = nrealloc((void *) u->strdata, len * sizeof(char *));
935 data[len-2] = fs->current->data[fs->current_x];
936 data[len-1] = '\0';
937 u->strdata = (char *) data;
938#ifdef DEBUG
939 fprintf(stderr, "current undo data now \"%s\"\n", u->strdata);
940#endif
941 break;
942 case DEL:
943 len = strlen(u->strdata) + 2;
944 assert(len > 2);
945 if (fs->current_x == u->begin) {
946 /* They're deleting */
Chris Allegretta0b499d42008-07-14 07:18:22 +0000947 if (!u->xflags)
948 u->xflags = UNDO_DEL_DEL;
949 else if (u->xflags != UNDO_DEL_DEL) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000950 add_undo(action);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000951 return;
952 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000953 data = charalloc(len);
954 strcpy(data, u->strdata);
955 data[len-2] = fs->current->data[fs->current_x];;
956 data[len-1] = '\0';
957 free(u->strdata);
958 u->strdata = data;
959 } else if (fs->current_x == u->begin - 1) {
960 /* They're backspacing */
Chris Allegretta0b499d42008-07-14 07:18:22 +0000961 if (!u->xflags)
962 u->xflags = UNDO_DEL_BACKSPACE;
963 else if (u->xflags != UNDO_DEL_BACKSPACE) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000964 add_undo(action);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000965 return;
966 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000967 data = charalloc(len);
968 data[0] = fs->current->data[fs->current_x];
969 strcpy(&data[1], u->strdata);
970 free(u->strdata);
971 u->strdata = data;
972 u->begin--;
973 } else {
974 /* They deleted something else on the line */
Chris Allegretta14c86202008-08-03 04:48:05 +0000975 add_undo(DEL);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000976 return;
977 }
978#ifdef DEBUG
Chris Allegretta0b499d42008-07-14 07:18:22 +0000979 fprintf(stderr, "current undo data now \"%s\"\nu->begin = %d\n", u->strdata, u->begin);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000980#endif
981 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000982 case CUT:
983 case CUTTOEND:
984 case UNCUT:
Chris Allegretta8f761122008-08-01 06:52:15 +0000985 if (u->cutbuffer)
986 free(u->cutbuffer);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000987 u->cutbuffer = copy_filestruct(cutbuffer);
988 u->cutbottom = cutbottom;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000989 u->linescut++;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000990 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000991 case REPLACE:
Chris Allegretta14c86202008-08-03 04:48:05 +0000992 add_undo(action);
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000993 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000994 case INSERT:
995 u->mark_begin_lineno = openfile->current->lineno;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000996 case SPLIT:
997 case UNSPLIT:
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000998 /* These cases are handled by the earlier check for a new line and action */
999 case OTHER:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001000 break;
1001 }
1002
1003#ifdef DEBUG
1004 fprintf(stderr, "Done in udpate_undo (type was %d)\n", action);
1005#endif
1006 if (fs->last_action != action) {
1007#ifdef DEBUG
1008 fprintf(stderr, "Starting add_undo for new action as it does not match last_action\n");
1009#endif
Chris Allegretta14c86202008-08-03 04:48:05 +00001010 add_undo(action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001011 }
1012 fs->last_action = action;
1013}
1014
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001015#endif /* !NANO_TINY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001016
1017#ifndef DISABLE_WRAPPING
David Lawrence Ramseyef0d5a72006-05-22 02:08:49 +00001018/* Unset the prepend_wrap flag. We need to do this as soon as we do
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001019 * something other than type text. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001020void wrap_reset(void)
1021{
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001022 prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001023}
1024
1025/* We wrap the given line. Precondition: we assume the cursor has been
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001026 * moved forward since the last typed character. Return TRUE if we
1027 * wrapped, and FALSE otherwise. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001028bool do_wrap(filestruct *line)
1029{
1030 size_t line_len;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001031 /* The length of the line we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001032 ssize_t wrap_loc;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001033 /* The index of line->data where we wrap. */
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001034#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001035 const char *indent_string = NULL;
1036 /* Indentation to prepend to the new line. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001037 size_t indent_len = 0;
1038 /* The length of indent_string. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001039#endif
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001040 const char *after_break;
1041 /* The text after the wrap point. */
1042 size_t after_break_len;
1043 /* The length of after_break. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001044 bool prepending = FALSE;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001045 /* Do we prepend to the next line? */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001046 const char *next_line = NULL;
1047 /* The next line, minus indentation. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001048 size_t next_line_len = 0;
1049 /* The length of next_line. */
1050 char *new_line = NULL;
1051 /* The line we create. */
1052 size_t new_line_len = 0;
1053 /* The eventual length of new_line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001054
1055 /* There are three steps. First, we decide where to wrap. Then, we
1056 * create the new wrap line. Finally, we clean up. */
1057
1058 /* Step 1, finding where to wrap. We are going to add a new line
1059 * after a blank character. In this step, we call break_line() to
1060 * get the location of the last blank we can break the line at, and
David Lawrence Ramseyd4686b82006-06-11 19:14:14 +00001061 * set wrap_loc to the location of the character after it, so that
1062 * the blank is preserved at the end of the line.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001063 *
1064 * If there is no legal wrap point, or we reach the last character
1065 * of the line while trying to find one, we should return without
1066 * wrapping. Note that if autoindent is turned on, we don't break
1067 * at the end of it! */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001068 assert(line != NULL && line->data != NULL);
1069
1070 /* Save the length of the line. */
1071 line_len = strlen(line->data);
1072
1073 /* Find the last blank where we can break the line. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001074 wrap_loc = break_line(line->data, fill
1075#ifndef DISABLE_HELP
1076 , FALSE
1077#endif
1078 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001079
1080 /* If we couldn't break the line, or we've reached the end of it, we
1081 * don't wrap. */
1082 if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
1083 return FALSE;
1084
1085 /* Otherwise, move forward to the character just after the blank. */
1086 wrap_loc += move_mbright(line->data + wrap_loc, 0);
1087
1088 /* If we've reached the end of the line, we don't wrap. */
1089 if (line->data[wrap_loc] == '\0')
1090 return FALSE;
1091
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001092#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001093 /* If autoindent is turned on, and we're on the character just after
1094 * the indentation, we don't wrap. */
1095 if (ISSET(AUTOINDENT)) {
1096 /* Get the indentation of this line. */
1097 indent_string = line->data;
1098 indent_len = indent_length(indent_string);
1099
1100 if (wrap_loc == indent_len)
1101 return FALSE;
1102 }
1103#endif
1104
1105 /* Step 2, making the new wrap line. It will consist of indentation
1106 * followed by the text after the wrap point, optionally followed by
1107 * a space (if the text after the wrap point doesn't end in a blank)
David Lawrence Ramsey03979d72006-06-11 19:15:59 +00001108 * and the text of the next line, if they can fit without wrapping,
1109 * the next line exists, and the prepend_wrap flag is set. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001110
1111 /* after_break is the text that will be wrapped to the next line. */
1112 after_break = line->data + wrap_loc;
1113 after_break_len = line_len - wrap_loc;
1114
1115 assert(strlen(after_break) == after_break_len);
1116
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001117 /* We prepend the wrapped text to the next line, if the prepend_wrap
1118 * flag is set, there is a next line, and prepending would not make
1119 * the line too long. */
1120 if (prepend_wrap && line != openfile->filebot) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001121 const char *end = after_break + move_mbleft(after_break,
1122 after_break_len);
1123
1124 /* If after_break doesn't end in a blank, make sure it ends in a
1125 * space. */
1126 if (!is_blank_mbchar(end)) {
1127 line_len++;
1128 line->data = charealloc(line->data, line_len + 1);
1129 line->data[line_len - 1] = ' ';
1130 line->data[line_len] = '\0';
1131 after_break = line->data + wrap_loc;
1132 after_break_len++;
1133 openfile->totsize++;
1134 }
1135
1136 next_line = line->next->data;
1137 next_line_len = strlen(next_line);
1138
1139 if (after_break_len + next_line_len <= fill) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001140 prepending = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001141 new_line_len += next_line_len;
1142 }
1143 }
1144
1145 /* new_line_len is now the length of the text that will be wrapped
1146 * to the next line, plus (if we're prepending to it) the length of
1147 * the text of the next line. */
1148 new_line_len += after_break_len;
1149
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001150#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001151 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001152 if (prepending) {
1153 /* If we're prepending, the indentation will come from the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001154 * next line. */
1155 indent_string = next_line;
1156 indent_len = indent_length(indent_string);
1157 next_line += indent_len;
1158 } else {
1159 /* Otherwise, it will come from this line, in which case
1160 * we should increase new_line_len to make room for it. */
1161 new_line_len += indent_len;
1162 openfile->totsize += mbstrnlen(indent_string, indent_len);
1163 }
1164 }
1165#endif
1166
1167 /* Now we allocate the new line and copy the text into it. */
1168 new_line = charalloc(new_line_len + 1);
1169 new_line[0] = '\0';
1170
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001171#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001172 if (ISSET(AUTOINDENT)) {
1173 /* Copy the indentation. */
1174 strncpy(new_line, indent_string, indent_len);
1175 new_line[indent_len] = '\0';
1176 new_line_len += indent_len;
1177 }
1178#endif
1179
1180 /* Copy all the text after the wrap point of the current line. */
1181 strcat(new_line, after_break);
1182
1183 /* Break the current line at the wrap point. */
1184 null_at(&line->data, wrap_loc);
1185
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001186 if (prepending) {
1187 /* If we're prepending, copy the text from the next line, minus
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001188 * the indentation that we already copied above. */
1189 strcat(new_line, next_line);
1190
1191 free(line->next->data);
1192 line->next->data = new_line;
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001193
1194 /* If the NO_NEWLINES flag isn't set, and text has been added to
1195 * the magicline, make a new magicline. */
1196 if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
1197 new_magicline();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001198 } else {
1199 /* Otherwise, make a new line and copy the text after where we
1200 * broke this line to the beginning of the new line. */
1201 splice_node(openfile->current, make_new_node(openfile->current),
1202 openfile->current->next);
1203
David Lawrence Ramsey219a8142005-11-22 22:08:01 +00001204 /* If the current line is the last line of the file, move the
1205 * last line of the file down to the next line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001206 if (openfile->filebot == openfile->current)
1207 openfile->filebot = openfile->current->next;
1208
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001209 openfile->current->next->data = new_line;
1210
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001211 openfile->totsize++;
1212 }
1213
1214 /* Step 3, clean up. Reposition the cursor and mark, and do some
1215 * other sundry things. */
1216
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001217 /* Set the prepend_wrap flag, so that later wraps of this line will
1218 * be prepended to the next line. */
1219 prepend_wrap = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001220
David Lawrence Ramsey2829aae2006-05-22 01:24:09 +00001221 /* Each line knows its number. We recalculate these if we inserted
1222 * a new line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001223 if (!prepending)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001224 renumber(line);
1225
1226 /* If the cursor was after the break point, we must move it. We
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001227 * also clear the prepend_wrap flag in this case. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001228 if (openfile->current_x > wrap_loc) {
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001229 prepend_wrap = FALSE;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001230
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001231 openfile->current = openfile->current->next;
1232 openfile->current_x -= wrap_loc
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001233#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001234 - indent_len
1235#endif
1236 ;
1237 openfile->placewewant = xplustabs();
1238 }
1239
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001240#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001241 /* If the mark was on this line after the wrap point, we move it
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001242 * down. If it was on the next line and we prepended to that line,
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001243 * we move it right. */
1244 if (openfile->mark_set) {
1245 if (openfile->mark_begin == line && openfile->mark_begin_x >
1246 wrap_loc) {
1247 openfile->mark_begin = line->next;
1248 openfile->mark_begin_x -= wrap_loc - indent_len + 1;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001249 } else if (prepending && openfile->mark_begin == line->next)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001250 openfile->mark_begin_x += after_break_len;
1251 }
1252#endif
1253
1254 return TRUE;
1255}
1256#endif /* !DISABLE_WRAPPING */
1257
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +00001258#if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001259/* We are trying to break a chunk off line. We find the last blank such
David Lawrence Ramseycd9a5f02005-09-20 06:12:54 +00001260 * that the display length to there is at most (goal + 1). If there is
1261 * no such blank, then we find the first blank. We then take the last
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001262 * blank in that group of blanks. The terminating '\0' counts as a
1263 * blank, as does a '\n' if newline is TRUE. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001264ssize_t break_line(const char *line, ssize_t goal
1265#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001266 , bool newln
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001267#endif
1268 )
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001269{
1270 ssize_t blank_loc = -1;
1271 /* Current tentative return value. Index of the last blank we
1272 * found with short enough display width. */
1273 ssize_t cur_loc = 0;
1274 /* Current index in line. */
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001275 size_t cur_pos = 0;
1276 /* Current column position in line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001277 int line_len;
1278
1279 assert(line != NULL);
1280
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001281 while (*line != '\0' && goal >= cur_pos) {
1282 line_len = parse_mbchar(line, NULL, &cur_pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001283
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001284 if (is_blank_mbchar(line)
1285#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001286 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001287#endif
1288 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001289 blank_loc = cur_loc;
1290
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001291#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001292 if (newln && *line == '\n')
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001293 break;
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001294#endif
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001295 }
1296
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001297 line += line_len;
1298 cur_loc += line_len;
1299 }
1300
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001301 if (goal >= cur_pos)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001302 /* In fact, the whole line displays shorter than goal. */
1303 return cur_loc;
1304
Chris Allegretta09b81242008-07-12 01:54:49 +00001305#ifndef DISABLE_HELP
1306 if (newln && blank_loc <= 0) {
1307 /* If blank was not found or was found only first character,
1308 * force line break. */
1309 cur_loc -= line_len;
1310 return cur_loc;
1311 }
1312#endif
1313
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001314 if (blank_loc == -1) {
1315 /* No blank was found that was short enough. */
1316 bool found_blank = FALSE;
David Lawrence Ramsey5ab12ca2005-09-20 17:52:52 +00001317 ssize_t found_blank_loc = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001318
1319 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001320 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001321
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001322 if (is_blank_mbchar(line)
1323#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001324 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001325#endif
1326 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001327 if (!found_blank)
1328 found_blank = TRUE;
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +00001329 found_blank_loc = cur_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001330 } else if (found_blank)
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +00001331 return found_blank_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001332
1333 line += line_len;
1334 cur_loc += line_len;
1335 }
1336
1337 return -1;
1338 }
1339
1340 /* Move to the last blank after blank_loc, if there is one. */
1341 line -= cur_loc;
1342 line += blank_loc;
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001343 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001344 line += line_len;
1345
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001346 while (*line != '\0' && (is_blank_mbchar(line)
1347#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001348 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001349#endif
1350 )) {
David Lawrence Ramseycd243f52006-05-19 23:27:16 +00001351#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001352 if (newln && *line == '\n')
David Lawrence Ramseycd243f52006-05-19 23:27:16 +00001353 break;
1354#endif
1355
David Lawrence Ramsey39bd1b32006-05-20 13:11:56 +00001356 line_len = parse_mbchar(line, NULL, NULL);
1357
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001358 line += line_len;
1359 blank_loc += line_len;
1360 }
1361
1362 return blank_loc;
1363}
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +00001364#endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001365
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001366#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001367/* The "indentation" of a line is the whitespace between the quote part
1368 * and the non-whitespace of the line. */
1369size_t indent_length(const char *line)
1370{
1371 size_t len = 0;
1372 char *blank_mb;
1373 int blank_mb_len;
1374
1375 assert(line != NULL);
1376
1377 blank_mb = charalloc(mb_cur_max());
1378
1379 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001380 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001381
1382 if (!is_blank_mbchar(blank_mb))
1383 break;
1384
1385 line += blank_mb_len;
1386 len += blank_mb_len;
1387 }
1388
1389 free(blank_mb);
1390
1391 return len;
1392}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001393#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001394
1395#ifndef DISABLE_JUSTIFY
1396/* justify_format() replaces blanks with spaces and multiple spaces by 1
1397 * (except it maintains up to 2 after a character in punct optionally
1398 * followed by a character in brackets, and removes all from the end).
1399 *
1400 * justify_format() might make paragraph->data shorter, and change the
1401 * actual pointer with null_at().
1402 *
1403 * justify_format() will not look at the first skip characters of
1404 * paragraph. skip should be at most strlen(paragraph->data). The
1405 * character at paragraph[skip + 1] must not be blank. */
1406void justify_format(filestruct *paragraph, size_t skip)
1407{
1408 char *end, *new_end, *new_paragraph_data;
1409 size_t shift = 0;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001410#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001411 size_t mark_shift = 0;
1412#endif
1413
1414 /* These four asserts are assumptions about the input data. */
1415 assert(paragraph != NULL);
1416 assert(paragraph->data != NULL);
1417 assert(skip < strlen(paragraph->data));
1418 assert(!is_blank_mbchar(paragraph->data + skip));
1419
1420 end = paragraph->data + skip;
1421 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
1422 strncpy(new_paragraph_data, paragraph->data, skip);
1423 new_end = new_paragraph_data + skip;
1424
1425 while (*end != '\0') {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001426 int end_len;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001427
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001428 /* If this character is blank, change it to a space if
1429 * necessary, and skip over all blanks after it. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001430 if (is_blank_mbchar(end)) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001431 end_len = parse_mbchar(end, NULL, NULL);
1432
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001433 *new_end = ' ';
1434 new_end++;
1435 end += end_len;
1436
1437 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001438 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001439
1440 end += end_len;
1441 shift += end_len;
1442
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001443#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001444 /* Keep track of the change in the current line. */
1445 if (openfile->mark_set && openfile->mark_begin ==
1446 paragraph && openfile->mark_begin_x >= end -
1447 paragraph->data)
1448 mark_shift += end_len;
1449#endif
1450 }
1451 /* If this character is punctuation optionally followed by a
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001452 * bracket and then followed by blanks, change no more than two
1453 * of the blanks to spaces if necessary, and skip over all
1454 * blanks after them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001455 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001456 end_len = parse_mbchar(end, NULL, NULL);
1457
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001458 while (end_len > 0) {
1459 *new_end = *end;
1460 new_end++;
1461 end++;
1462 end_len--;
1463 }
1464
1465 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001466 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001467
1468 while (end_len > 0) {
1469 *new_end = *end;
1470 new_end++;
1471 end++;
1472 end_len--;
1473 }
1474 }
1475
1476 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001477 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001478
1479 *new_end = ' ';
1480 new_end++;
1481 end += end_len;
1482 }
1483
1484 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001485 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001486
1487 *new_end = ' ';
1488 new_end++;
1489 end += end_len;
1490 }
1491
1492 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001493 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001494
1495 end += end_len;
1496 shift += end_len;
1497
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001498#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001499 /* Keep track of the change in the current line. */
1500 if (openfile->mark_set && openfile->mark_begin ==
1501 paragraph && openfile->mark_begin_x >= end -
1502 paragraph->data)
1503 mark_shift += end_len;
1504#endif
1505 }
1506 /* If this character is neither blank nor punctuation, leave it
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001507 * unchanged. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001508 } else {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001509 end_len = parse_mbchar(end, NULL, NULL);
1510
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001511 while (end_len > 0) {
1512 *new_end = *end;
1513 new_end++;
1514 end++;
1515 end_len--;
1516 }
1517 }
1518 }
1519
1520 assert(*end == '\0');
1521
1522 *new_end = *end;
1523
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001524 /* If there are spaces at the end of the line, remove them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001525 while (new_end > new_paragraph_data + skip &&
1526 *(new_end - 1) == ' ') {
1527 new_end--;
1528 shift++;
1529 }
1530
1531 if (shift > 0) {
1532 openfile->totsize -= shift;
1533 null_at(&new_paragraph_data, new_end - new_paragraph_data);
1534 free(paragraph->data);
1535 paragraph->data = new_paragraph_data;
1536
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001537#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001538 /* Adjust the mark coordinates to compensate for the change in
1539 * the current line. */
1540 if (openfile->mark_set && openfile->mark_begin == paragraph) {
1541 openfile->mark_begin_x -= mark_shift;
1542 if (openfile->mark_begin_x > new_end - new_paragraph_data)
1543 openfile->mark_begin_x = new_end - new_paragraph_data;
1544 }
1545#endif
1546 } else
1547 free(new_paragraph_data);
1548}
1549
1550/* The "quote part" of a line is the largest initial substring matching
1551 * the quote string. This function returns the length of the quote part
1552 * of the given line.
1553 *
1554 * Note that if !HAVE_REGEX_H then we match concatenated copies of
1555 * quotestr. */
1556size_t quote_length(const char *line)
1557{
1558#ifdef HAVE_REGEX_H
1559 regmatch_t matches;
1560 int rc = regexec(&quotereg, line, 1, &matches, 0);
1561
1562 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
1563 return 0;
1564 /* matches.rm_so should be 0, since the quote string should start
1565 * with the caret ^. */
1566 return matches.rm_eo;
1567#else /* !HAVE_REGEX_H */
1568 size_t qdepth = 0;
1569
1570 /* Compute quote depth level. */
1571 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
1572 qdepth += quotelen;
1573 return qdepth;
1574#endif /* !HAVE_REGEX_H */
1575}
1576
1577/* a_line and b_line are lines of text. The quotation part of a_line is
1578 * the first a_quote characters. Check that the quotation part of
1579 * b_line is the same. */
1580bool quotes_match(const char *a_line, size_t a_quote, const char
1581 *b_line)
1582{
1583 /* Here is the assumption about a_quote. */
1584 assert(a_quote == quote_length(a_line));
1585
1586 return (a_quote == quote_length(b_line) &&
1587 strncmp(a_line, b_line, a_quote) == 0);
1588}
1589
1590/* We assume a_line and b_line have no quote part. Then, we return
1591 * whether b_line could follow a_line in a paragraph. */
1592bool indents_match(const char *a_line, size_t a_indent, const char
1593 *b_line, size_t b_indent)
1594{
1595 assert(a_indent == indent_length(a_line));
1596 assert(b_indent == indent_length(b_line));
1597
1598 return (b_indent <= a_indent &&
1599 strncmp(a_line, b_line, b_indent) == 0);
1600}
1601
1602/* Is foo the beginning of a paragraph?
1603 *
1604 * A line of text consists of a "quote part", followed by an
1605 * "indentation part", followed by text. The functions quote_length()
1606 * and indent_length() calculate these parts.
1607 *
1608 * A line is "part of a paragraph" if it has a part not in the quote
1609 * part or the indentation.
1610 *
1611 * A line is "the beginning of a paragraph" if it is part of a
1612 * paragraph and
1613 * 1) it is the top line of the file, or
1614 * 2) the line above it is not part of a paragraph, or
1615 * 3) the line above it does not have precisely the same quote
1616 * part, or
1617 * 4) the indentation of this line is not an initial substring of
1618 * the indentation of the previous line, or
1619 * 5) this line has no quote part and some indentation, and
1620 * autoindent isn't turned on.
1621 * The reason for number 5) is that if autoindent isn't turned on,
1622 * then an indented line is expected to start a paragraph, as in
1623 * books. Thus, nano can justify an indented paragraph only if
1624 * autoindent is turned on. */
1625bool begpar(const filestruct *const foo)
1626{
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001627 size_t quote_len, indent_len, temp_id_len;
1628
1629 if (foo == NULL)
1630 return FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001631
1632 /* Case 1). */
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001633 if (foo == openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001634 return TRUE;
1635
1636 quote_len = quote_length(foo->data);
1637 indent_len = indent_length(foo->data + quote_len);
1638
1639 /* Not part of a paragraph. */
1640 if (foo->data[quote_len + indent_len] == '\0')
1641 return FALSE;
1642
1643 /* Case 3). */
1644 if (!quotes_match(foo->data, quote_len, foo->prev->data))
1645 return TRUE;
1646
1647 temp_id_len = indent_length(foo->prev->data + quote_len);
1648
1649 /* Case 2) or 5) or 4). */
1650 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
1651 (quote_len == 0 && indent_len > 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001652#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001653 && !ISSET(AUTOINDENT)
1654#endif
1655 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
1656 foo->data + quote_len, indent_len))
1657 return TRUE;
1658
1659 return FALSE;
1660}
1661
1662/* Is foo inside a paragraph? */
1663bool inpar(const filestruct *const foo)
1664{
1665 size_t quote_len;
1666
1667 if (foo == NULL)
1668 return FALSE;
1669
1670 quote_len = quote_length(foo->data);
1671
David Lawrence Ramsey21014032005-11-09 20:33:42 +00001672 return (foo->data[quote_len + indent_length(foo->data +
1673 quote_len)] != '\0');
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001674}
1675
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001676/* Move the next par_len lines, starting with first_line, into the
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001677 * justify buffer, leaving copies of those lines in place. Assume that
1678 * par_len is greater than zero, and that there are enough lines after
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001679 * first_line. */
1680void backup_lines(filestruct *first_line, size_t par_len)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001681{
1682 filestruct *top = first_line;
1683 /* The top of the paragraph we're backing up. */
1684 filestruct *bot = first_line;
1685 /* The bottom of the paragraph we're backing up. */
1686 size_t i;
1687 /* Generic loop variable. */
1688 size_t current_x_save = openfile->current_x;
1689 ssize_t fl_lineno_save = first_line->lineno;
1690 ssize_t edittop_lineno_save = openfile->edittop->lineno;
1691 ssize_t current_lineno_save = openfile->current->lineno;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001692#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001693 bool old_mark_set = openfile->mark_set;
1694 ssize_t mb_lineno_save = 0;
1695 size_t mark_begin_x_save = 0;
1696
1697 if (old_mark_set) {
1698 mb_lineno_save = openfile->mark_begin->lineno;
1699 mark_begin_x_save = openfile->mark_begin_x;
1700 }
1701#endif
1702
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001703 /* par_len will be one greater than the number of lines between
1704 * current and filebot if filebot is the last line in the
1705 * paragraph. */
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001706 assert(par_len > 0 && openfile->current->lineno + par_len <=
David Lawrence Ramsey24777c02005-12-01 05:49:08 +00001707 openfile->filebot->lineno + 1);
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001708
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001709 /* Move bot down par_len lines to the line after the last line of
1710 * the paragraph, if there is one. */
1711 for (i = par_len; i > 0 && bot != openfile->filebot; i--)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001712 bot = bot->next;
1713
1714 /* Move the paragraph from the current buffer's filestruct to the
1715 * justify buffer. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001716 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
David Lawrence Ramseyf0575cf2005-11-09 23:27:51 +00001717 (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001718
1719 /* Copy the paragraph back to the current buffer's filestruct from
1720 * the justify buffer. */
1721 copy_from_filestruct(jusbuffer, jusbottom);
1722
1723 /* Move upward from the last line of the paragraph to the first
1724 * line, putting first_line, edittop, current, and mark_begin at the
1725 * same lines in the copied paragraph that they had in the original
1726 * paragraph. */
David Lawrence Ramseyee43ea62007-04-22 15:04:05 +00001727 if (openfile->current != openfile->fileage) {
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001728 top = openfile->current->prev;
David Lawrence Ramseyee43ea62007-04-22 15:04:05 +00001729#ifndef NANO_TINY
1730 if (old_mark_set &&
1731 openfile->current->lineno == mb_lineno_save) {
1732 openfile->mark_begin = openfile->current;
1733 openfile->mark_begin_x = mark_begin_x_save;
1734 }
1735#endif
1736 } else
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001737 top = openfile->current;
David Lawrence Ramseye8d505b2005-11-10 03:40:45 +00001738 for (i = par_len; i > 0 && top != NULL; i--) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001739 if (top->lineno == fl_lineno_save)
1740 first_line = top;
1741 if (top->lineno == edittop_lineno_save)
1742 openfile->edittop = top;
1743 if (top->lineno == current_lineno_save)
1744 openfile->current = top;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001745#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001746 if (old_mark_set && top->lineno == mb_lineno_save) {
1747 openfile->mark_begin = top;
1748 openfile->mark_begin_x = mark_begin_x_save;
1749 }
1750#endif
1751 top = top->prev;
1752 }
1753
1754 /* Put current_x at the same place in the copied paragraph that it
1755 * had in the original paragraph. */
1756 openfile->current_x = current_x_save;
1757
1758 set_modified();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001759}
1760
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001761/* Find the beginning of the current paragraph if we're in one, or the
1762 * beginning of the next paragraph if we're not. Afterwards, save the
1763 * quote length and paragraph length in *quote and *par. Return TRUE if
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001764 * we found a paragraph, and FALSE if there was an error or we didn't
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001765 * find a paragraph.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001766 *
1767 * See the comment at begpar() for more about when a line is the
1768 * beginning of a paragraph. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001769bool find_paragraph(size_t *const quote, size_t *const par)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001770{
1771 size_t quote_len;
1772 /* Length of the initial quotation of the paragraph we search
1773 * for. */
1774 size_t par_len;
1775 /* Number of lines in the paragraph we search for. */
1776 filestruct *current_save;
1777 /* The line at the beginning of the paragraph we search for. */
1778 ssize_t current_y_save;
1779 /* The y-coordinate at the beginning of the paragraph we search
1780 * for. */
1781
1782#ifdef HAVE_REGEX_H
1783 if (quoterc != 0) {
1784 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1785 return FALSE;
1786 }
1787#endif
1788
1789 assert(openfile->current != NULL);
1790
David Lawrence Ramsey1be131a2005-11-11 03:55:52 +00001791 /* If we're at the end of the last line of the file, it means that
1792 * there aren't any paragraphs left, so get out. */
1793 if (openfile->current == openfile->filebot && openfile->current_x ==
1794 strlen(openfile->filebot->data))
1795 return FALSE;
1796
1797 /* If the current line isn't in a paragraph, move forward to the
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001798 * last line of the next paragraph, if any. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001799 if (!inpar(openfile->current)) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001800 do_para_end(FALSE);
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001801
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001802 /* If we end up past the beginning of the line, it means that
1803 * we're at the end of the last line of the file, and the line
1804 * isn't blank, in which case the last line of the file is the
1805 * last line of the next paragraph.
1806 *
1807 * Otherwise, if we end up on a line that's in a paragraph, it
1808 * means that we're on the line after the last line of the next
1809 * paragraph, in which case we should move back to the last line
1810 * of the next paragraph. */
1811 if (openfile->current_x == 0) {
1812 if (!inpar(openfile->current->prev))
1813 return FALSE;
1814 if (openfile->current != openfile->fileage)
1815 openfile->current = openfile->current->prev;
1816 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001817 }
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001818
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001819 /* If the current line isn't the first line of the paragraph, move
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001820 * back to the first line of the paragraph. */
1821 if (!begpar(openfile->current))
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001822 do_para_begin(FALSE);
1823
1824 /* Now current is the first line of the paragraph. Set quote_len to
1825 * the quotation length of that line, and set par_len to the number
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001826 * of lines in this paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001827 quote_len = quote_length(openfile->current->data);
1828 current_save = openfile->current;
1829 current_y_save = openfile->current_y;
1830 do_para_end(FALSE);
1831 par_len = openfile->current->lineno - current_save->lineno;
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001832
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001833 /* If we end up past the beginning of the line, it means that we're
1834 * at the end of the last line of the file, and the line isn't
1835 * blank, in which case the last line of the file is part of the
1836 * paragraph. */
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001837 if (openfile->current_x > 0)
1838 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001839 openfile->current = current_save;
1840 openfile->current_y = current_y_save;
1841
1842 /* Save the values of quote_len and par_len. */
1843 assert(quote != NULL && par != NULL);
1844
1845 *quote = quote_len;
1846 *par = par_len;
1847
1848 return TRUE;
1849}
1850
1851/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1852 * the current paragraph. */
1853void do_justify(bool full_justify)
1854{
1855 filestruct *first_par_line = NULL;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001856 /* Will be the first line of the justified paragraph(s), if any.
1857 * For restoring after unjustify. */
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001858 filestruct *last_par_line = NULL;
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001859 /* Will be the line after the last line of the justified
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001860 * paragraph(s), if any. Also for restoring after unjustify. */
David Lawrence Ramsey82b5deb2005-11-10 06:07:57 +00001861 bool filebot_inpar = FALSE;
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001862 /* Whether the text at filebot is part of the current
1863 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001864
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001865 /* We save these variables to be restored if the user
1866 * unjustifies. */
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001867 filestruct *edittop_save = openfile->edittop;
1868 filestruct *current_save = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001869 size_t current_x_save = openfile->current_x;
1870 size_t pww_save = openfile->placewewant;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001871 size_t totsize_save = openfile->totsize;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001872#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001873 filestruct *mark_begin_save = openfile->mark_begin;
1874 size_t mark_begin_x_save = openfile->mark_begin_x;
1875#endif
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001876 bool modified_save = openfile->modified;
1877
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001878 int kbinput;
1879 bool meta_key, func_key, s_or_t, ran_func, finished;
Chris Allegretta0018d8e2008-03-13 08:23:52 +00001880 const sc *s;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001881
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001882 /* Move to the beginning of the current line, so that justifying at
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001883 * the end of the last line of the file, if that line isn't blank,
1884 * will work the first time through. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001885 openfile->current_x = 0;
1886
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001887 /* If we're justifying the entire file, start at the beginning. */
1888 if (full_justify)
1889 openfile->current = openfile->fileage;
1890
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001891 while (TRUE) {
1892 size_t i;
1893 /* Generic loop variable. */
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001894 filestruct *curr_first_par_line;
1895 /* The first line of the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001896 size_t quote_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001897 /* Length of the initial quotation of the current
1898 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001899 size_t indent_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001900 /* Length of the initial indentation of the current
1901 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001902 size_t par_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001903 /* Number of lines in the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001904 ssize_t break_pos;
1905 /* Where we will break lines. */
1906 char *indent_string;
1907 /* The first indentation that doesn't match the initial
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001908 * indentation of the current paragraph. This is put at the
1909 * beginning of every line broken off the first justified
1910 * line of the paragraph. Note that this works because a
1911 * paragraph can only contain two indentations at most: the
1912 * initial one, and a different one starting on a line after
1913 * the first. See the comment at begpar() for more about
1914 * when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001915
1916 /* Find the first line of the paragraph to be justified. That
1917 * is the start of this paragraph if we're in one, or the start
1918 * of the next otherwise. Save the quote length and paragraph
1919 * length (number of lines). Don't refresh the screen yet,
1920 * since we'll do that after we justify.
1921 *
1922 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001923 * justifying the whole file, and we've found at least one
1924 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001925 * last line of the file, so set the last line of the text to be
1926 * justified to the last line of the file and break out of the
1927 * loop. Otherwise, it means that there are no paragraph(s) to
1928 * justify, so refresh the screen and get out. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001929 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001930 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001931 last_par_line = openfile->filebot;
1932 break;
1933 } else {
1934 edit_refresh();
1935 return;
1936 }
1937 }
1938
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001939 /* par_len will be one greater than the number of lines between
1940 * current and filebot if filebot is the last line in the
1941 * paragraph. Set filebot_inpar to TRUE if this is the case. */
1942 filebot_inpar = (openfile->current->lineno + par_len ==
1943 openfile->filebot->lineno + 1);
1944
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001945 /* If we haven't already done it, move the original paragraph(s)
1946 * to the justify buffer, splice a copy of the original
1947 * paragraph(s) into the file in the same place, and set
1948 * first_par_line to the first line of the copy. */
1949 if (first_par_line == NULL) {
1950 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00001951 openfile->filebot->lineno - openfile->current->lineno +
1952 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001953 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001954 first_par_line = openfile->current;
1955 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001956
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001957 /* Set curr_first_par_line to the first line of the current
1958 * paragraph. */
1959 curr_first_par_line = openfile->current;
1960
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001961 /* Initialize indent_string to a blank string. */
1962 indent_string = mallocstrcpy(NULL, "");
1963
1964 /* Find the first indentation in the paragraph that doesn't
David Lawrence Ramsey8602fd62006-05-28 18:43:21 +00001965 * match the indentation of the first line, and save it in
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001966 * indent_string. If all the indentations are the same, save
1967 * the indentation of the first line in indent_string. */
1968 {
1969 const filestruct *indent_line = openfile->current;
1970 bool past_first_line = FALSE;
1971
1972 for (i = 0; i < par_len; i++) {
1973 indent_len = quote_len +
1974 indent_length(indent_line->data + quote_len);
1975
1976 if (indent_len != strlen(indent_string)) {
1977 indent_string = mallocstrncpy(indent_string,
1978 indent_line->data, indent_len + 1);
1979 indent_string[indent_len] = '\0';
1980
1981 if (past_first_line)
1982 break;
1983 }
1984
1985 if (indent_line == openfile->current)
1986 past_first_line = TRUE;
1987
1988 indent_line = indent_line->next;
1989 }
1990 }
1991
1992 /* Now tack all the lines of the paragraph together, skipping
1993 * the quoting and indentation on all lines after the first. */
1994 for (i = 0; i < par_len - 1; i++) {
1995 filestruct *next_line = openfile->current->next;
1996 size_t line_len = strlen(openfile->current->data);
1997 size_t next_line_len =
1998 strlen(openfile->current->next->data);
1999
2000 indent_len = quote_len +
2001 indent_length(openfile->current->next->data +
2002 quote_len);
2003
2004 next_line_len -= indent_len;
2005 openfile->totsize -= indent_len;
2006
2007 /* We're just about to tack the next line onto this one. If
2008 * this line isn't empty, make sure it ends in a space. */
2009 if (line_len > 0 &&
2010 openfile->current->data[line_len - 1] != ' ') {
2011 line_len++;
2012 openfile->current->data =
2013 charealloc(openfile->current->data,
2014 line_len + 1);
2015 openfile->current->data[line_len - 1] = ' ';
2016 openfile->current->data[line_len] = '\0';
2017 openfile->totsize++;
2018 }
2019
2020 openfile->current->data =
2021 charealloc(openfile->current->data, line_len +
2022 next_line_len + 1);
2023 strcat(openfile->current->data, next_line->data +
2024 indent_len);
2025
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002026 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00002027 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002028 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002029 if (next_line == openfile->filebot)
2030 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002031
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002032#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002033 /* Adjust the mark coordinates to compensate for the change
2034 * in the next line. */
2035 if (openfile->mark_set && openfile->mark_begin ==
2036 next_line) {
2037 openfile->mark_begin = openfile->current;
2038 openfile->mark_begin_x += line_len - indent_len;
2039 }
2040#endif
2041
2042 unlink_node(next_line);
2043 delete_node(next_line);
2044
2045 /* If we've removed the next line, we need to go through
2046 * this line again. */
2047 i--;
2048
2049 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002050 openfile->totsize--;
2051 }
2052
2053 /* Call justify_format() on the paragraph, which will remove
2054 * excess spaces from it and change all blank characters to
2055 * spaces. */
2056 justify_format(openfile->current, quote_len +
2057 indent_length(openfile->current->data + quote_len));
2058
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002059 while (par_len > 0 && strlenpt(openfile->current->data) >
2060 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002061 size_t line_len = strlen(openfile->current->data);
2062
2063 indent_len = strlen(indent_string);
2064
2065 /* If this line is too long, try to wrap it to the next line
2066 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002067 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00002068 fill - strnlenpt(openfile->current->data, indent_len)
2069#ifndef DISABLE_HELP
2070 , FALSE
2071#endif
2072 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002073
2074 /* We can't break the line, or don't need to, so get out. */
2075 if (break_pos == -1 || break_pos + indent_len == line_len)
2076 break;
2077
2078 /* Move forward to the character after the indentation and
2079 * just after the space. */
2080 break_pos += indent_len + 1;
2081
2082 assert(break_pos <= line_len);
2083
2084 /* Make a new line, and copy the text after where we're
2085 * going to break this line to the beginning of the new
2086 * line. */
2087 splice_node(openfile->current,
2088 make_new_node(openfile->current),
2089 openfile->current->next);
2090
2091 /* If this paragraph is non-quoted, and autoindent isn't
2092 * turned on, set the indentation length to zero so that the
2093 * indentation is treated as part of the line. */
2094 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002095#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002096 && !ISSET(AUTOINDENT)
2097#endif
2098 )
2099 indent_len = 0;
2100
2101 /* Copy the text after where we're going to break the
2102 * current line to the next line. */
2103 openfile->current->next->data = charalloc(indent_len + 1 +
2104 line_len - break_pos);
2105 strncpy(openfile->current->next->data, indent_string,
2106 indent_len);
2107 strcpy(openfile->current->next->data + indent_len,
2108 openfile->current->data + break_pos);
2109
2110 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002111 openfile->totsize += indent_len + 1;
2112
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002113#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002114 /* Adjust the mark coordinates to compensate for the change
2115 * in the current line. */
2116 if (openfile->mark_set && openfile->mark_begin ==
2117 openfile->current && openfile->mark_begin_x >
2118 break_pos) {
2119 openfile->mark_begin = openfile->current->next;
2120 openfile->mark_begin_x -= break_pos - indent_len;
2121 }
2122#endif
2123
2124 /* Break the current line. */
2125 null_at(&openfile->current->data, break_pos);
2126
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002127 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00002128 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002129 if (openfile->filebot == openfile->current)
2130 openfile->filebot = openfile->filebot->next;
2131
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002132 /* Go to the next line. */
2133 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002134 openfile->current_y++;
2135 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002136 }
2137
2138 /* We're done breaking lines, so we don't need indent_string
2139 * anymore. */
2140 free(indent_string);
2141
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002142 /* Go to the next line, if possible. If there is no next line,
2143 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00002144 if (openfile->current != openfile->filebot) {
2145 openfile->current_y++;
2146 openfile->current = openfile->current->next;
2147 } else
2148 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002149
David Lawrence Ramseya6854682005-11-30 21:19:42 +00002150 /* Renumber the lines of the now-justified current paragraph,
2151 * since both find_paragraph() and edit_refresh() need the line
2152 * numbers to be right. */
2153 renumber(curr_first_par_line);
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002154
2155 /* We've just finished justifying the paragraph. If we're not
2156 * justifying the entire file, break out of the loop.
2157 * Otherwise, continue the loop so that we justify all the
2158 * paragraphs in the file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002159 if (!full_justify)
2160 break;
2161 }
2162
2163 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002164 * up. current_y and totsize have been maintained above. If we
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002165 * actually justified something, set last_par_line to the new end of
2166 * the paragraph. */
2167 if (first_par_line != NULL)
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002168 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002169
2170 edit_refresh();
2171
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002172#ifndef NANO_TINY
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002173 /* We're going to set jump_buf so that we return here after a
2174 * SIGWINCH instead of to main(). Indicate this. */
2175 jump_buf_main = FALSE;
2176
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002177 /* Return here after a SIGWINCH. */
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002178 sigsetjmp(jump_buf, 1);
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002179#endif
2180
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002181 statusbar(_("Can now UnJustify!"));
2182
2183 /* If constant cursor position display is on, make sure the current
2184 * cursor position will be properly displayed on the statusbar. */
2185 if (ISSET(CONST_UPDATE))
2186 do_cursorpos(TRUE);
2187
2188 /* Display the shortcut list with UnJustify. */
2189 shortcut_init(TRUE);
2190 display_main_list();
2191
2192 /* Now get a keystroke and see if it's unjustify. If not, put back
2193 * the keystroke and return. */
2194 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
2195 &finished, FALSE);
Chris Allegretta0018d8e2008-03-13 08:23:52 +00002196 s = get_shortcut(currmenu, &kbinput, &meta_key, &func_key);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002197
Chris Allegretta0018d8e2008-03-13 08:23:52 +00002198 if (s && s->scfunc == do_uncut_text) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002199 /* Splice the justify buffer back into the file, but only if we
2200 * actually justified something. */
2201 if (first_par_line != NULL) {
2202 filestruct *top_save;
2203
2204 /* Partition the filestruct so that it contains only the
2205 * text of the justified paragraph. */
2206 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00002207 last_par_line, filebot_inpar ?
2208 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002209
2210 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00002211 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002212 free_filestruct(openfile->fileage);
2213 openfile->fileage = jusbuffer;
2214 openfile->filebot = jusbottom;
2215
2216 top_save = openfile->fileage;
2217
2218 /* Unpartition the filestruct so that it contains all the
2219 * text again. Note that the justified paragraph has been
2220 * replaced with the unjustified paragraph. */
2221 unpartition_filestruct(&filepart);
2222
2223 /* Renumber starting with the beginning line of the old
2224 * partition. */
2225 renumber(top_save);
2226
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002227 /* Restore the justify we just did (ungrateful user!). */
2228 openfile->edittop = edittop_save;
2229 openfile->current = current_save;
2230 openfile->current_x = current_x_save;
2231 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002232 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002233#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002234 if (openfile->mark_set) {
2235 openfile->mark_begin = mark_begin_save;
2236 openfile->mark_begin_x = mark_begin_x_save;
2237 }
2238#endif
2239 openfile->modified = modified_save;
2240
2241 /* Clear the justify buffer. */
2242 jusbuffer = NULL;
2243
2244 if (!openfile->modified)
2245 titlebar(NULL);
2246 edit_refresh();
2247 }
2248 } else {
2249 unget_kbinput(kbinput, meta_key, func_key);
2250
2251 /* Blow away the text in the justify buffer. */
2252 free_filestruct(jusbuffer);
2253 jusbuffer = NULL;
2254 }
2255
2256 blank_statusbar();
2257
2258 /* Display the shortcut list with UnCut. */
2259 shortcut_init(FALSE);
2260 display_main_list();
2261}
2262
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002263/* Justify the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002264void do_justify_void(void)
2265{
2266 do_justify(FALSE);
2267}
2268
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002269/* Justify the entire file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002270void do_full_justify(void)
2271{
2272 do_justify(TRUE);
2273}
2274#endif /* !DISABLE_JUSTIFY */
2275
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002276#ifndef DISABLE_SPELLER
2277/* A word is misspelled in the file. Let the user replace it. We
2278 * return FALSE if the user cancels. */
2279bool do_int_spell_fix(const char *word)
2280{
2281 char *save_search, *save_replace;
2282 size_t match_len, current_x_save = openfile->current_x;
2283 size_t pww_save = openfile->placewewant;
Chris Allegretta10f868d2008-03-14 04:08:51 +00002284 bool meta_key = FALSE, func_key = FALSE;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002285 filestruct *edittop_save = openfile->edittop;
2286 filestruct *current_save = openfile->current;
2287 /* Save where we are. */
2288 bool canceled = FALSE;
2289 /* The return value. */
2290 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002291#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002292 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
2293#endif
2294#ifdef HAVE_REGEX_H
2295 bool regexp_set = ISSET(USE_REGEXP);
2296#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002297#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002298 bool old_mark_set = openfile->mark_set;
2299 bool added_magicline = FALSE;
2300 /* Whether we added a magicline after filebot. */
2301 bool right_side_up = FALSE;
2302 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2303 * FALSE if (current, current_x) is. */
2304 filestruct *top, *bot;
2305 size_t top_x, bot_x;
2306#endif
2307
2308 /* Make sure spell-check is case sensitive. */
2309 SET(CASE_SENSITIVE);
2310
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002311#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002312 /* Make sure spell-check goes forward only. */
2313 UNSET(BACKWARDS_SEARCH);
2314#endif
2315#ifdef HAVE_REGEX_H
2316 /* Make sure spell-check doesn't use regular expressions. */
2317 UNSET(USE_REGEXP);
2318#endif
2319
2320 /* Save the current search/replace strings. */
2321 search_init_globals();
2322 save_search = last_search;
2323 save_replace = last_replace;
2324
2325 /* Set the search/replace strings to the misspelled word. */
2326 last_search = mallocstrcpy(NULL, word);
2327 last_replace = mallocstrcpy(NULL, word);
2328
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002329#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002330 if (old_mark_set) {
2331 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002332 * contains only the marked text; if the NO_NEWLINES flag isn't
2333 * set, keep track of whether the text will have a magicline
2334 * added when we're done correcting misspelled words; and
2335 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002336 mark_order((const filestruct **)&top, &top_x,
2337 (const filestruct **)&bot, &bot_x, &right_side_up);
2338 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002339 if (!ISSET(NO_NEWLINES))
2340 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002341 openfile->mark_set = FALSE;
2342 }
2343#endif
2344
2345 /* Start from the top of the file. */
2346 openfile->edittop = openfile->fileage;
2347 openfile->current = openfile->fileage;
2348 openfile->current_x = (size_t)-1;
2349 openfile->placewewant = 0;
2350
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002351 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002352 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002353 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002354 &match_len)) {
2355 if (is_whole_word(openfile->current_x, openfile->current->data,
2356 word)) {
2357 size_t xpt = xplustabs();
2358 char *exp_word = display_string(openfile->current->data,
2359 xpt, strnlenpt(openfile->current->data,
2360 openfile->current_x + match_len) - xpt, FALSE);
2361
2362 edit_refresh();
2363
2364 do_replace_highlight(TRUE, exp_word);
2365
2366 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002367 canceled = (do_prompt(FALSE,
2368#ifndef DISABLE_TABCOMP
2369 TRUE,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002370#endif
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002371 MSPELL, word,
Chris Allegretta10f868d2008-03-14 04:08:51 +00002372 &meta_key, &func_key,
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002373#ifndef NANO_TINY
2374 NULL,
2375#endif
David Lawrence Ramsey68160072006-02-18 21:32:29 +00002376 edit_refresh, _("Edit a replacement")) == -1);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002377
2378 do_replace_highlight(FALSE, exp_word);
2379
2380 free(exp_word);
2381
2382 if (!canceled && strcmp(word, answer) != 0) {
2383 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002384 do_replace_loop(TRUE, &canceled, openfile->current,
2385 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002386 }
2387
2388 break;
2389 }
2390 }
2391
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002392#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002393 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002394 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2395 * added a magicline, remove it now. */
2396 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002397 remove_magicline();
2398
2399 /* Put the beginning and the end of the mark at the beginning
2400 * and the end of the spell-checked text. */
2401 if (openfile->fileage == openfile->filebot)
2402 bot_x += top_x;
2403 if (right_side_up) {
2404 openfile->mark_begin_x = top_x;
2405 current_x_save = bot_x;
2406 } else {
2407 current_x_save = top_x;
2408 openfile->mark_begin_x = bot_x;
2409 }
2410
2411 /* Unpartition the filestruct so that it contains all the text
2412 * again, and turn the mark back on. */
2413 unpartition_filestruct(&filepart);
2414 openfile->mark_set = TRUE;
2415 }
2416#endif
2417
2418 /* Restore the search/replace strings. */
2419 free(last_search);
2420 last_search = save_search;
2421 free(last_replace);
2422 last_replace = save_replace;
2423
2424 /* Restore where we were. */
2425 openfile->edittop = edittop_save;
2426 openfile->current = current_save;
2427 openfile->current_x = current_x_save;
2428 openfile->placewewant = pww_save;
2429
2430 /* Restore case sensitivity setting. */
2431 if (!case_sens_set)
2432 UNSET(CASE_SENSITIVE);
2433
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002434#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002435 /* Restore search/replace direction. */
2436 if (backwards_search_set)
2437 SET(BACKWARDS_SEARCH);
2438#endif
2439#ifdef HAVE_REGEX_H
2440 /* Restore regular expression usage setting. */
2441 if (regexp_set)
2442 SET(USE_REGEXP);
2443#endif
2444
2445 return !canceled;
2446}
2447
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002448/* Internal (integrated) spell checking using the spell program,
2449 * filtered through the sort and uniq programs. Return NULL for normal
2450 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002451const char *do_int_speller(const char *tempfile_name)
2452{
2453 char *read_buff, *read_buff_ptr, *read_buff_word;
2454 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
2455 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
2456 pid_t pid_spell, pid_sort, pid_uniq;
2457 int spell_status, sort_status, uniq_status;
2458
2459 /* Create all three pipes up front. */
2460 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
2461 pipe(uniq_fd) == -1)
2462 return _("Could not create pipe");
2463
2464 statusbar(_("Creating misspelled word list, please wait..."));
2465
2466 /* A new process to run spell in. */
2467 if ((pid_spell = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002468 /* Child continues (i.e. future spell process). */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002469 close(spell_fd[0]);
2470
2471 /* Replace the standard input with the temp file. */
2472 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
2473 goto close_pipes_and_exit;
2474
2475 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
2476 goto close_pipes_and_exit;
2477
2478 close(tempfile_fd);
2479
2480 /* Send spell's standard output to the pipe. */
2481 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2482 goto close_pipes_and_exit;
2483
2484 close(spell_fd[1]);
2485
David Lawrence Ramsey3239ff22005-11-29 20:01:06 +00002486 /* Start the spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002487 execlp("spell", "spell", NULL);
2488
2489 /* This should not be reached if spell is found. */
2490 exit(1);
2491 }
2492
2493 /* Parent continues here. */
2494 close(spell_fd[1]);
2495
2496 /* A new process to run sort in. */
2497 if ((pid_sort = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002498 /* Child continues (i.e. future spell process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002499 * standard input with the standard output of the old pipe. */
2500 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
2501 goto close_pipes_and_exit;
2502
2503 close(spell_fd[0]);
2504
2505 /* Send sort's standard output to the new pipe. */
2506 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2507 goto close_pipes_and_exit;
2508
2509 close(sort_fd[1]);
2510
2511 /* Start the sort program. Use -f to remove mixed case. If
2512 * this isn't portable, let me know. */
2513 execlp("sort", "sort", "-f", NULL);
2514
2515 /* This should not be reached if sort is found. */
2516 exit(1);
2517 }
2518
2519 close(spell_fd[0]);
2520 close(sort_fd[1]);
2521
2522 /* A new process to run uniq in. */
2523 if ((pid_uniq = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002524 /* Child continues (i.e. future uniq process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002525 * standard input with the standard output of the old pipe. */
2526 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
2527 goto close_pipes_and_exit;
2528
2529 close(sort_fd[0]);
2530
2531 /* Send uniq's standard output to the new pipe. */
2532 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2533 goto close_pipes_and_exit;
2534
2535 close(uniq_fd[1]);
2536
2537 /* Start the uniq program; we are using PATH. */
2538 execlp("uniq", "uniq", NULL);
2539
2540 /* This should not be reached if uniq is found. */
2541 exit(1);
2542 }
2543
2544 close(sort_fd[0]);
2545 close(uniq_fd[1]);
2546
2547 /* The child process was not forked successfully. */
2548 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
2549 close(uniq_fd[0]);
2550 return _("Could not fork");
2551 }
2552
2553 /* Get the system pipe buffer size. */
2554 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
2555 close(uniq_fd[0]);
2556 return _("Could not get size of pipe buffer");
2557 }
2558
2559 /* Read in the returned spelling errors. */
2560 read_buff_read = 0;
2561 read_buff_size = pipe_buff_size + 1;
2562 read_buff = read_buff_ptr = charalloc(read_buff_size);
2563
2564 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
2565 pipe_buff_size)) > 0) {
2566 read_buff_read += bytesread;
2567 read_buff_size += pipe_buff_size;
2568 read_buff = read_buff_ptr = charealloc(read_buff,
2569 read_buff_size);
2570 read_buff_ptr += read_buff_read;
2571 }
2572
2573 *read_buff_ptr = '\0';
2574 close(uniq_fd[0]);
2575
2576 /* Process the spelling errors. */
2577 read_buff_word = read_buff_ptr = read_buff;
2578
2579 while (*read_buff_ptr != '\0') {
2580 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
2581 *read_buff_ptr = '\0';
2582 if (read_buff_word != read_buff_ptr) {
2583 if (!do_int_spell_fix(read_buff_word)) {
2584 read_buff_word = read_buff_ptr;
2585 break;
2586 }
2587 }
2588 read_buff_word = read_buff_ptr + 1;
2589 }
2590 read_buff_ptr++;
2591 }
2592
2593 /* Special case: the last word doesn't end with '\r' or '\n'. */
2594 if (read_buff_word != read_buff_ptr)
2595 do_int_spell_fix(read_buff_word);
2596
2597 free(read_buff);
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002598 search_replace_abort();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002599 edit_refresh();
2600
2601 /* Process the end of the spell process. */
2602 waitpid(pid_spell, &spell_status, 0);
2603 waitpid(pid_sort, &sort_status, 0);
2604 waitpid(pid_uniq, &uniq_status, 0);
2605
2606 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
2607 return _("Error invoking \"spell\"");
2608
2609 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
2610 return _("Error invoking \"sort -f\"");
2611
2612 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
2613 return _("Error invoking \"uniq\"");
2614
2615 /* Otherwise... */
2616 return NULL;
2617
2618 close_pipes_and_exit:
2619 /* Don't leak any handles. */
2620 close(tempfile_fd);
2621 close(spell_fd[0]);
2622 close(spell_fd[1]);
2623 close(sort_fd[0]);
2624 close(sort_fd[1]);
2625 close(uniq_fd[0]);
2626 close(uniq_fd[1]);
2627 exit(1);
2628}
2629
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002630/* External (alternate) spell checking. Return NULL for normal
2631 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002632const char *do_alt_speller(char *tempfile_name)
2633{
2634 int alt_spell_status;
2635 size_t current_x_save = openfile->current_x;
2636 size_t pww_save = openfile->placewewant;
2637 ssize_t current_y_save = openfile->current_y;
2638 ssize_t lineno_save = openfile->current->lineno;
2639 pid_t pid_spell;
2640 char *ptr;
2641 static int arglen = 3;
2642 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002643#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002644 bool old_mark_set = openfile->mark_set;
2645 bool added_magicline = FALSE;
2646 /* Whether we added a magicline after filebot. */
2647 bool right_side_up = FALSE;
2648 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2649 * FALSE if (current, current_x) is. */
2650 filestruct *top, *bot;
2651 size_t top_x, bot_x;
2652 ssize_t mb_lineno_save = 0;
2653 /* We're going to close the current file, and open the output of
2654 * the alternate spell command. The line that mark_begin points
2655 * to will be freed, so we save the line number and restore it
2656 * afterwards. */
2657 size_t totsize_save = openfile->totsize;
2658 /* Our saved value of totsize, used when we spell-check a marked
2659 * selection. */
2660
2661 if (old_mark_set) {
2662 /* If the mark is on, save the number of the line it starts on,
2663 * and then turn the mark off. */
2664 mb_lineno_save = openfile->mark_begin->lineno;
2665 openfile->mark_set = FALSE;
2666 }
2667#endif
2668
2669 endwin();
2670
2671 /* Set up an argument list to pass execvp(). */
2672 if (spellargs == NULL) {
2673 spellargs = (char **)nmalloc(arglen * sizeof(char *));
2674
2675 spellargs[0] = strtok(alt_speller, " ");
2676 while ((ptr = strtok(NULL, " ")) != NULL) {
2677 arglen++;
2678 spellargs = (char **)nrealloc(spellargs, arglen *
2679 sizeof(char *));
2680 spellargs[arglen - 3] = ptr;
2681 }
2682 spellargs[arglen - 1] = NULL;
2683 }
2684 spellargs[arglen - 2] = tempfile_name;
2685
2686 /* Start a new process for the alternate speller. */
2687 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey2e2112c2005-11-29 05:39:31 +00002688 /* Start alternate spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002689 execvp(spellargs[0], spellargs);
2690
2691 /* Should not be reached, if alternate speller is found!!! */
2692 exit(1);
2693 }
2694
2695 /* If we couldn't fork, get out. */
2696 if (pid_spell < 0)
2697 return _("Could not fork");
2698
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002699#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002700 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002701 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002702 allow_pending_sigwinch(FALSE);
2703#endif
2704
2705 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002706 wait(&alt_spell_status);
2707
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00002708 /* Reenter curses mode. */
2709 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002710
2711 /* Restore the terminal to its previous state. */
2712 terminal_init();
2713
2714 /* Turn the cursor back on for sure. */
2715 curs_set(1);
2716
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002717 /* The screen might have been resized. If it has, reinitialize all
2718 * the windows based on the new screen dimensions. */
2719 window_init();
2720
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002721 if (!WIFEXITED(alt_spell_status) ||
2722 WEXITSTATUS(alt_spell_status) != 0) {
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002723 char *alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002724 char *invoke_error = _("Error invoking \"%s\"");
2725
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002726#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002727 /* Turn the mark back on if it was on before. */
2728 openfile->mark_set = old_mark_set;
2729#endif
2730
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002731 alt_spell_error =
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002732 charalloc(strlen(invoke_error) +
2733 strlen(alt_speller) + 1);
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002734 sprintf(alt_spell_error, invoke_error, alt_speller);
2735 return alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002736 }
2737
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002738#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002739 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002740 /* If the mark is on, partition the filestruct so that it
2741 * contains only the marked text; if the NO_NEWLINES flag isn't
2742 * set, keep track of whether the text will have a magicline
2743 * added when we're done correcting misspelled words; and
2744 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002745 mark_order((const filestruct **)&top, &top_x,
2746 (const filestruct **)&bot, &bot_x, &right_side_up);
2747 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002748 if (!ISSET(NO_NEWLINES))
2749 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002750
2751 /* Get the number of characters in the marked text, and subtract
2752 * it from the saved value of totsize. */
2753 totsize_save -= get_totsize(top, bot);
2754 }
2755#endif
2756
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002757 /* Replace the text of the current buffer with the spell-checked
2758 * text. */
2759 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002760
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002761#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002762 if (old_mark_set) {
2763 filestruct *top_save = openfile->fileage;
2764
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002765 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2766 * added a magicline, remove it now. */
2767 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002768 remove_magicline();
2769
2770 /* Put the beginning and the end of the mark at the beginning
2771 * and the end of the spell-checked text. */
2772 if (openfile->fileage == openfile->filebot)
2773 bot_x += top_x;
2774 if (right_side_up) {
2775 openfile->mark_begin_x = top_x;
2776 current_x_save = bot_x;
2777 } else {
2778 current_x_save = top_x;
2779 openfile->mark_begin_x = bot_x;
2780 }
2781
2782 /* Unpartition the filestruct so that it contains all the text
2783 * again. Note that we've replaced the marked text originally
2784 * in the partition with the spell-checked marked text in the
2785 * temp file. */
2786 unpartition_filestruct(&filepart);
2787
2788 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002789 * partition. Also add the number of characters in the
2790 * spell-checked marked text to the saved value of totsize, and
2791 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002792 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002793 totsize_save += openfile->totsize;
2794 openfile->totsize = totsize_save;
2795
2796 /* Assign mark_begin to the line where the mark began before. */
2797 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2798 current_y_save, 0);
2799 openfile->mark_begin = openfile->current;
2800
2801 /* Assign mark_begin_x to the location in mark_begin where the
2802 * mark began before, adjusted for any shortening of the
2803 * line. */
2804 openfile->mark_begin_x = openfile->current_x;
2805
2806 /* Turn the mark back on. */
2807 openfile->mark_set = TRUE;
2808 }
2809#endif
2810
2811 /* Go back to the old position, and mark the file as modified. */
2812 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2813 set_modified();
2814
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002815#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002816 /* Handle a pending SIGWINCH again. */
2817 allow_pending_sigwinch(TRUE);
2818#endif
2819
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002820 return NULL;
2821}
2822
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002823/* Spell check the current file. If an alternate spell checker is
2824 * specified, use it. Otherwise, use the internal spell checker. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002825void do_spell(void)
2826{
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002827 bool status;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002828 FILE *temp_file;
2829 char *temp = safe_tempfile(&temp_file);
2830 const char *spell_msg;
2831
2832 if (temp == NULL) {
David Lawrence Ramseyf0e3ca62006-05-03 13:11:00 +00002833 statusbar(_("Error writing temp file: %s"), strerror(errno));
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002834 return;
2835 }
2836
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002837 status =
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002838#ifndef NANO_TINY
David Lawrence Ramsey0e1df432007-01-12 02:58:12 +00002839 openfile->mark_set ? write_marked_file(temp, temp_file, TRUE,
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002840 OVERWRITE) :
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002841#endif
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002842 write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002843
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002844 if (!status) {
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002845 statusbar(_("Error writing temp file: %s"), strerror(errno));
2846 free(temp);
2847 return;
2848 }
2849
2850 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2851 do_int_speller(temp);
2852 unlink(temp);
2853 free(temp);
2854
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002855 currmenu = MMAIN;
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002856
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002857 /* If the spell-checker printed any error messages onscreen, make
2858 * sure that they're cleared off. */
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002859 total_refresh();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002860
2861 if (spell_msg != NULL) {
2862 if (errno == 0)
2863 /* Don't display an error message of "Success". */
2864 statusbar(_("Spell checking failed: %s"), spell_msg);
2865 else
2866 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2867 strerror(errno));
2868 } else
2869 statusbar(_("Finished checking spelling"));
2870}
2871#endif /* !DISABLE_SPELLER */
2872
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002873#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002874/* Our own version of "wc". Note that its character counts are in
2875 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002876void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002877{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002878 size_t words = 0, chars = 0;
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002879 ssize_t nlines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002880 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002881 size_t pww_save = openfile->placewewant;
2882 filestruct *current_save = openfile->current;
2883 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002884 filestruct *top, *bot;
2885 size_t top_x, bot_x;
2886
2887 if (old_mark_set) {
2888 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002889 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002890 mark_order((const filestruct **)&top, &top_x,
2891 (const filestruct **)&bot, &bot_x, NULL);
2892 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002893 openfile->mark_set = FALSE;
2894 }
2895
2896 /* Start at the top of the file. */
2897 openfile->current = openfile->fileage;
2898 openfile->current_x = 0;
2899 openfile->placewewant = 0;
2900
2901 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002902 * part of a word, as "wc -w" does), without updating the screen,
2903 * until we reach the end of the file, incrementing the total word
2904 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002905 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002906 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002907 if (do_next_word(TRUE, FALSE))
2908 words++;
2909 }
2910
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002911 /* Get the total line and character counts, as "wc -l" and "wc -c"
2912 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002913 if (old_mark_set) {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002914 nlines = openfile->filebot->lineno -
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002915 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002916 chars = get_totsize(openfile->fileage, openfile->filebot);
2917
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002918 /* Unpartition the filestruct so that it contains all the text
2919 * again, and turn the mark back on. */
2920 unpartition_filestruct(&filepart);
2921 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002922 } else {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002923 nlines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002924 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002925 }
2926
2927 /* Restore where we were. */
2928 openfile->current = current_save;
2929 openfile->current_x = current_x_save;
2930 openfile->placewewant = pww_save;
2931
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002932 /* Display the total word, line, and character counts on the
2933 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002934 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002935 _("In Selection: ") : "", (unsigned long)words, (long)nlines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002936 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002937}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002938#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002939
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002940/* Get verbatim input. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002941void do_verbatim_input(void)
2942{
2943 int *kbinput;
2944 size_t kbinput_len, i;
2945 char *output;
2946
David Lawrence Ramseyf451d6a2006-05-27 16:02:48 +00002947 /* TRANSLATORS: This is displayed when the next keystroke will be
2948 * inserted verbatim. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002949 statusbar(_("Verbatim Input"));
2950
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002951 /* Read in all the verbatim characters. */
2952 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2953
David Lawrence Ramseya620e682006-05-27 18:19:03 +00002954 /* If constant cursor position display is on, make sure the current
2955 * cursor position will be properly displayed on the statusbar.
2956 * Otherwise, blank the statusbar. */
2957 if (ISSET(CONST_UPDATE))
2958 do_cursorpos(TRUE);
2959 else {
2960 blank_statusbar();
2961 wnoutrefresh(bottomwin);
2962 }
David Lawrence Ramsey6fb66892006-05-27 17:39:19 +00002963
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002964 /* Display all the verbatim characters at once, not filtering out
2965 * control characters. */
2966 output = charalloc(kbinput_len + 1);
2967
2968 for (i = 0; i < kbinput_len; i++)
2969 output[i] = (char)kbinput[i];
2970 output[i] = '\0';
2971
David Lawrence Ramseyad36bdc2006-12-02 17:22:21 +00002972 free(kbinput);
2973
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002974 do_output(output, kbinput_len, TRUE);
2975
2976 free(output);
2977}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00002978