blob: 5226a321250afee3f4897f3f463c9090005fbfd9 [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;
494
495}
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);
554 t->data = mallocstrcpy(NULL, u->strdata);
555 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;
641
642}
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000643#endif /* !NANO_TINY */
644
David Lawrence Ramseyb0e04c02005-12-08 07:24:54 +0000645/* Someone hits Enter *gasp!* */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000646void do_enter(void)
647{
648 filestruct *newnode = make_new_node(openfile->current);
649 size_t extra = 0;
650
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000651 assert(openfile->current != NULL && xopenfile->current->data != NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000652
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000653#ifndef NANO_TINY
Chris Allegretta14c86202008-08-03 04:48:05 +0000654 update_undo(SPLIT);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000655
656
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000657 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
658 if (ISSET(AUTOINDENT)) {
659 /* If we are breaking the line in the indentation, the new
660 * indentation should have only current_x characters, and
661 * current_x should not change. */
662 extra = indent_length(openfile->current->data);
663 if (extra > openfile->current_x)
664 extra = openfile->current_x;
665 }
666#endif
667 newnode->data = charalloc(strlen(openfile->current->data +
668 openfile->current_x) + extra + 1);
669 strcpy(&newnode->data[extra], openfile->current->data +
670 openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000671#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000672 if (ISSET(AUTOINDENT)) {
673 strncpy(newnode->data, openfile->current->data, extra);
674 openfile->totsize += mbstrlen(newnode->data);
675 }
676#endif
677 null_at(&openfile->current->data, openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000678#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000679 if (openfile->mark_set && openfile->current ==
680 openfile->mark_begin && openfile->current_x <
681 openfile->mark_begin_x) {
682 openfile->mark_begin = newnode;
683 openfile->mark_begin_x += extra - openfile->current_x;
684 }
685#endif
686 openfile->current_x = extra;
687
688 if (openfile->current == openfile->filebot)
689 openfile->filebot = newnode;
690 splice_node(openfile->current, newnode,
691 openfile->current->next);
692
693 renumber(openfile->current);
694 openfile->current = newnode;
695
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000696 openfile->totsize++;
697 set_modified();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000698
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000699 openfile->placewewant = xplustabs();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000700
701 edit_refresh();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000702}
703
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000704#ifndef NANO_TINY
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000705/* Send a SIGKILL (unconditional kill) to the forked process in
706 * execute_command(). */
David Lawrence Ramsey8befda62005-12-06 19:39:56 +0000707RETSIGTYPE cancel_command(int signal)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000708{
709 if (kill(pid, SIGKILL) == -1)
710 nperror("kill");
711}
712
David Lawrence Ramsey0ed71712005-11-08 23:09:47 +0000713/* Execute command in a shell. Return TRUE on success. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000714bool execute_command(const char *command)
715{
716 int fd[2];
717 FILE *f;
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000718 char *shellenv;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000719 struct sigaction oldaction, newaction;
720 /* Original and temporary handlers for SIGINT. */
721 bool sig_failed = FALSE;
722 /* Did sigaction() fail without changing the signal handlers? */
723
724 /* Make our pipes. */
725 if (pipe(fd) == -1) {
726 statusbar(_("Could not pipe"));
727 return FALSE;
728 }
729
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000730 /* Check $SHELL for the shell to use. If it isn't set, use
David Lawrence Ramsey1932dfb2005-11-29 05:52:49 +0000731 * /bin/sh. Note that $SHELL should contain only a path, with no
732 * arguments. */
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000733 shellenv = getenv("SHELL");
734 if (shellenv == NULL)
735 shellenv = "/bin/sh";
736
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000737 /* Fork a child. */
738 if ((pid = fork()) == 0) {
739 close(fd[0]);
740 dup2(fd[1], fileno(stdout));
741 dup2(fd[1], fileno(stderr));
742
743 /* If execl() returns at all, there was an error. */
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000744 execl(shellenv, tail(shellenv), "-c", command, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000745 exit(0);
746 }
747
David Lawrence Ramseyc838a4c2006-04-26 18:33:50 +0000748 /* Continue as parent. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000749 close(fd[1]);
750
751 if (pid == -1) {
752 close(fd[0]);
753 statusbar(_("Could not fork"));
754 return FALSE;
755 }
756
757 /* Before we start reading the forked command's output, we set
758 * things up so that Ctrl-C will cancel the new process. */
759
760 /* Enable interpretation of the special control keys so that we get
761 * SIGINT when Ctrl-C is pressed. */
762 enable_signals();
763
764 if (sigaction(SIGINT, NULL, &newaction) == -1) {
765 sig_failed = TRUE;
766 nperror("sigaction");
767 } else {
768 newaction.sa_handler = cancel_command;
769 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
770 sig_failed = TRUE;
771 nperror("sigaction");
772 }
773 }
774
775 /* Note that now oldaction is the previous SIGINT signal handler,
776 * to be restored later. */
777
778 f = fdopen(fd[0], "rb");
779 if (f == NULL)
780 nperror("fdopen");
781
Chris Allegretta14c86202008-08-03 04:48:05 +0000782 read_file(f, "stdin", TRUE);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000783
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000784 if (wait(NULL) == -1)
785 nperror("wait");
786
787 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
788 nperror("sigaction");
789
David Lawrence Ramsey8b9c91b2007-12-18 01:28:53 +0000790 /* Restore the terminal to its previous state. In the process,
791 * disable interpretation of the special control keys so that we can
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000792 * use Ctrl-C for other things. */
David Lawrence Ramsey8b9c91b2007-12-18 01:28:53 +0000793 terminal_init();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000794
795 return TRUE;
796}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000797
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000798/* Add a new undo struct to the top of the current pile */
Chris Allegretta14c86202008-08-03 04:48:05 +0000799void add_undo(undo_type current_action)
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000800{
Chris Allegretta8f761122008-08-01 06:52:15 +0000801 undo *u;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000802 char *data;
Chris Allegretta14c86202008-08-03 04:48:05 +0000803 openfilestruct *fs = openfile;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000804
Chris Allegretta8f761122008-08-01 06:52:15 +0000805 /* Ugh, if we were called while cutting not-to-end, non-marked and on the same lineno,
806 we need to abort here */
807 u = fs->current_undo;
808 if (u && u->type == CUT && !u->mark_set && u->lineno == fs->current->lineno)
809 return;
810
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000811 /* Blow away the old undo stack if we are starting from the middle */
Chris Allegretta91a18622008-08-01 03:50:20 +0000812 while (fs->undotop != NULL && fs->undotop != fs->current_undo) {
813 undo *u2 = fs->undotop;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000814 fs->undotop = fs->undotop->next;
Chris Allegretta91a18622008-08-01 03:50:20 +0000815 if (u2->strdata != NULL)
816 free(u2->strdata);
Chris Allegrettaea577872008-08-03 20:19:42 +0000817 if (u2->cutbuffer);
818 free_filestruct(u2->cutbuffer);
Chris Allegretta91a18622008-08-01 03:50:20 +0000819 free(u2);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000820 }
821
Chris Allegretta8f761122008-08-01 06:52:15 +0000822 /* Allocate and initialize a new undo type */
823 u = nmalloc(sizeof(undo));
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000824 u->type = current_action;
825 u->lineno = fs->current->lineno;
826 u->begin = fs->current_x;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000827 u->next = fs->undotop;
828 fs->undotop = u;
829 fs->current_undo = u;
Chris Allegretta91a18622008-08-01 03:50:20 +0000830 u->strdata = NULL;
831 u->cutbuffer = NULL;
832 u->cutbottom = NULL;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000833 u->mark_begin_lineno = 0;
834 u->mark_begin_x = 0;
835 u->linescut = 0;
Chris Allegretta91a18622008-08-01 03:50:20 +0000836 u->xflags = 0;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000837
838 switch (u->type) {
839 /* We need to start copying data into the undo buffer or we wont be able
840 to restore it later */
841 case ADD:
842 data = charalloc(2);
843 data[0] = fs->current->data[fs->current_x];
844 data[1] = '\0';
845 u->strdata = data;
846 break;
847 case DEL:
848 if (u->begin != strlen(fs->current->data)) {
849 data = mallocstrncpy(NULL, &fs->current->data[u->begin], 2);
850 data[1] = '\0';
851 u->strdata = data;
852 break;
853 }
854 /* Else purposely fall into unsplit code */
855 current_action = u->type = UNSPLIT;
856 case UNSPLIT:
857 if (fs->current->next) {
858 data = mallocstrcpy(NULL, fs->current->next->data);
859 u->strdata = data;
860 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000861 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000862 case INSERT:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000863 case SPLIT:
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000864 case REPLACE:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000865 data = mallocstrcpy(NULL, fs->current->data);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000866 u->strdata = data;
867 break;
868 case CUT:
869 case CUTTOEND:
870 u->mark_set = openfile->mark_set;
871 if (u->mark_set) {
872 u->mark_begin_lineno = openfile->mark_begin->lineno;
873 u->mark_begin_x = openfile->mark_begin_x;
874 }
875 u->to_end = (current_action == CUTTOEND);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000876 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000877 case UNCUT:
878 break;
879 case OTHER:
880 statusbar(_("OOPS. Tried to add unknown thing to undo struct, I'd save your work"));
881 break;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000882 }
883
884#ifdef DEBUG
885 fprintf(stderr, "fs->current->data = \"%s\", current_x = %d, u->begin = %d, type = %d\n",
886 fs->current->data, fs->current_x, u->begin, current_action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000887 fprintf(stderr, "left update_add...\n");
888#endif
889 fs->last_action = current_action;
890}
891
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000892/* Update an undo item, or determine whether a new one
893 is really needed and bounce the data to add_undo
894 instead. The latter functionality just feels
895 gimmicky and may just be more hassle than
896 it's worth, so it should be axed if needed. */
Chris Allegretta14c86202008-08-03 04:48:05 +0000897void update_undo(undo_type action)
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000898{
899 undo *u;
900 char *data;
901 int len = 0;
Chris Allegretta14c86202008-08-03 04:48:05 +0000902 openfilestruct *fs = openfile;
Chris Allegretta91a18622008-08-01 03:50:20 +0000903
904#ifdef DEBUG
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000905 fprintf(stderr, "action = %d, fs->last_action = %d, openfile->current->lineno = %d",
906 action, fs->last_action, openfile->current->lineno);
907 if (fs->current_undo)
908 fprintf(stderr, "fs->current_undo->lineno = %d\n", fs->current_undo->lineno);
909 else
910 fprintf(stderr, "\n");
Chris Allegretta91a18622008-08-01 03:50:20 +0000911#endif
912
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000913 /* Change to an add if we're not using the same undo struct
914 that we should be using */
915 if (action != fs->last_action
Chris Allegretta14c86202008-08-03 04:48:05 +0000916 || (action != CUT && action != CUTTOEND && action != INSERT
Chris Allegretta91a18622008-08-01 03:50:20 +0000917 && openfile->current->lineno != fs->current_undo->lineno)) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000918 add_undo(action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000919 return;
920 }
921
922 assert(fs->undotop != NULL);
923 u = fs->undotop;
924
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000925 switch (u->type) {
926 case ADD:
927#ifdef DEBUG
928 fprintf(stderr, "fs->current->data = \"%s\", current_x = %d, u->begin = %d\n",
929 fs->current->data, fs->current_x, u->begin);
930#endif
931 len = strlen(u->strdata) + 2;
932 data = nrealloc((void *) u->strdata, len * sizeof(char *));
933 data[len-2] = fs->current->data[fs->current_x];
934 data[len-1] = '\0';
935 u->strdata = (char *) data;
936#ifdef DEBUG
937 fprintf(stderr, "current undo data now \"%s\"\n", u->strdata);
938#endif
939 break;
940 case DEL:
941 len = strlen(u->strdata) + 2;
942 assert(len > 2);
943 if (fs->current_x == u->begin) {
944 /* They're deleting */
Chris Allegretta0b499d42008-07-14 07:18:22 +0000945 if (!u->xflags)
946 u->xflags = UNDO_DEL_DEL;
947 else if (u->xflags != UNDO_DEL_DEL) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000948 add_undo(action);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000949 return;
950 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000951 data = charalloc(len);
952 strcpy(data, u->strdata);
953 data[len-2] = fs->current->data[fs->current_x];;
954 data[len-1] = '\0';
955 free(u->strdata);
956 u->strdata = data;
957 } else if (fs->current_x == u->begin - 1) {
958 /* They're backspacing */
Chris Allegretta0b499d42008-07-14 07:18:22 +0000959 if (!u->xflags)
960 u->xflags = UNDO_DEL_BACKSPACE;
961 else if (u->xflags != UNDO_DEL_BACKSPACE) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000962 add_undo(action);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000963 return;
964 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000965 data = charalloc(len);
966 data[0] = fs->current->data[fs->current_x];
967 strcpy(&data[1], u->strdata);
968 free(u->strdata);
969 u->strdata = data;
970 u->begin--;
971 } else {
972 /* They deleted something else on the line */
Chris Allegretta14c86202008-08-03 04:48:05 +0000973 add_undo(DEL);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000974 return;
975 }
976#ifdef DEBUG
Chris Allegretta0b499d42008-07-14 07:18:22 +0000977 fprintf(stderr, "current undo data now \"%s\"\nu->begin = %d\n", u->strdata, u->begin);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000978#endif
979 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000980 case CUT:
981 case CUTTOEND:
982 case UNCUT:
Chris Allegretta8f761122008-08-01 06:52:15 +0000983 if (u->cutbuffer)
984 free(u->cutbuffer);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000985 u->cutbuffer = copy_filestruct(cutbuffer);
986 u->cutbottom = cutbottom;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000987 u->linescut++;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000988 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000989 case REPLACE:
Chris Allegretta14c86202008-08-03 04:48:05 +0000990 add_undo(action);
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000991 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000992 case INSERT:
993 u->mark_begin_lineno = openfile->current->lineno;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000994 case SPLIT:
995 case UNSPLIT:
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000996 /* These cases are handled by the earlier check for a new line and action */
997 case OTHER:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000998 break;
999 }
1000
1001#ifdef DEBUG
1002 fprintf(stderr, "Done in udpate_undo (type was %d)\n", action);
1003#endif
1004 if (fs->last_action != action) {
1005#ifdef DEBUG
1006 fprintf(stderr, "Starting add_undo for new action as it does not match last_action\n");
1007#endif
Chris Allegretta14c86202008-08-03 04:48:05 +00001008 add_undo(action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001009 }
1010 fs->last_action = action;
1011}
1012
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001013#endif /* !NANO_TINY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001014
1015#ifndef DISABLE_WRAPPING
David Lawrence Ramseyef0d5a72006-05-22 02:08:49 +00001016/* Unset the prepend_wrap flag. We need to do this as soon as we do
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001017 * something other than type text. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001018void wrap_reset(void)
1019{
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001020 prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001021}
1022
1023/* We wrap the given line. Precondition: we assume the cursor has been
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001024 * moved forward since the last typed character. Return TRUE if we
1025 * wrapped, and FALSE otherwise. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001026bool do_wrap(filestruct *line)
1027{
1028 size_t line_len;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001029 /* The length of the line we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001030 ssize_t wrap_loc;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001031 /* The index of line->data where we wrap. */
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001032#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001033 const char *indent_string = NULL;
1034 /* Indentation to prepend to the new line. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001035 size_t indent_len = 0;
1036 /* The length of indent_string. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001037#endif
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001038 const char *after_break;
1039 /* The text after the wrap point. */
1040 size_t after_break_len;
1041 /* The length of after_break. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001042 bool prepending = FALSE;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001043 /* Do we prepend to the next line? */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001044 const char *next_line = NULL;
1045 /* The next line, minus indentation. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001046 size_t next_line_len = 0;
1047 /* The length of next_line. */
1048 char *new_line = NULL;
1049 /* The line we create. */
1050 size_t new_line_len = 0;
1051 /* The eventual length of new_line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001052
1053 /* There are three steps. First, we decide where to wrap. Then, we
1054 * create the new wrap line. Finally, we clean up. */
1055
1056 /* Step 1, finding where to wrap. We are going to add a new line
1057 * after a blank character. In this step, we call break_line() to
1058 * get the location of the last blank we can break the line at, and
David Lawrence Ramseyd4686b82006-06-11 19:14:14 +00001059 * set wrap_loc to the location of the character after it, so that
1060 * the blank is preserved at the end of the line.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001061 *
1062 * If there is no legal wrap point, or we reach the last character
1063 * of the line while trying to find one, we should return without
1064 * wrapping. Note that if autoindent is turned on, we don't break
1065 * at the end of it! */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001066 assert(line != NULL && line->data != NULL);
1067
1068 /* Save the length of the line. */
1069 line_len = strlen(line->data);
1070
1071 /* Find the last blank where we can break the line. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001072 wrap_loc = break_line(line->data, fill
1073#ifndef DISABLE_HELP
1074 , FALSE
1075#endif
1076 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001077
1078 /* If we couldn't break the line, or we've reached the end of it, we
1079 * don't wrap. */
1080 if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
1081 return FALSE;
1082
1083 /* Otherwise, move forward to the character just after the blank. */
1084 wrap_loc += move_mbright(line->data + wrap_loc, 0);
1085
1086 /* If we've reached the end of the line, we don't wrap. */
1087 if (line->data[wrap_loc] == '\0')
1088 return FALSE;
1089
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001090#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001091 /* If autoindent is turned on, and we're on the character just after
1092 * the indentation, we don't wrap. */
1093 if (ISSET(AUTOINDENT)) {
1094 /* Get the indentation of this line. */
1095 indent_string = line->data;
1096 indent_len = indent_length(indent_string);
1097
1098 if (wrap_loc == indent_len)
1099 return FALSE;
1100 }
1101#endif
1102
1103 /* Step 2, making the new wrap line. It will consist of indentation
1104 * followed by the text after the wrap point, optionally followed by
1105 * a space (if the text after the wrap point doesn't end in a blank)
David Lawrence Ramsey03979d72006-06-11 19:15:59 +00001106 * and the text of the next line, if they can fit without wrapping,
1107 * the next line exists, and the prepend_wrap flag is set. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001108
1109 /* after_break is the text that will be wrapped to the next line. */
1110 after_break = line->data + wrap_loc;
1111 after_break_len = line_len - wrap_loc;
1112
1113 assert(strlen(after_break) == after_break_len);
1114
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001115 /* We prepend the wrapped text to the next line, if the prepend_wrap
1116 * flag is set, there is a next line, and prepending would not make
1117 * the line too long. */
1118 if (prepend_wrap && line != openfile->filebot) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001119 const char *end = after_break + move_mbleft(after_break,
1120 after_break_len);
1121
1122 /* If after_break doesn't end in a blank, make sure it ends in a
1123 * space. */
1124 if (!is_blank_mbchar(end)) {
1125 line_len++;
1126 line->data = charealloc(line->data, line_len + 1);
1127 line->data[line_len - 1] = ' ';
1128 line->data[line_len] = '\0';
1129 after_break = line->data + wrap_loc;
1130 after_break_len++;
1131 openfile->totsize++;
1132 }
1133
1134 next_line = line->next->data;
1135 next_line_len = strlen(next_line);
1136
1137 if (after_break_len + next_line_len <= fill) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001138 prepending = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001139 new_line_len += next_line_len;
1140 }
1141 }
1142
1143 /* new_line_len is now the length of the text that will be wrapped
1144 * to the next line, plus (if we're prepending to it) the length of
1145 * the text of the next line. */
1146 new_line_len += after_break_len;
1147
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001148#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001149 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001150 if (prepending) {
1151 /* If we're prepending, the indentation will come from the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001152 * next line. */
1153 indent_string = next_line;
1154 indent_len = indent_length(indent_string);
1155 next_line += indent_len;
1156 } else {
1157 /* Otherwise, it will come from this line, in which case
1158 * we should increase new_line_len to make room for it. */
1159 new_line_len += indent_len;
1160 openfile->totsize += mbstrnlen(indent_string, indent_len);
1161 }
1162 }
1163#endif
1164
1165 /* Now we allocate the new line and copy the text into it. */
1166 new_line = charalloc(new_line_len + 1);
1167 new_line[0] = '\0';
1168
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001169#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001170 if (ISSET(AUTOINDENT)) {
1171 /* Copy the indentation. */
1172 strncpy(new_line, indent_string, indent_len);
1173 new_line[indent_len] = '\0';
1174 new_line_len += indent_len;
1175 }
1176#endif
1177
1178 /* Copy all the text after the wrap point of the current line. */
1179 strcat(new_line, after_break);
1180
1181 /* Break the current line at the wrap point. */
1182 null_at(&line->data, wrap_loc);
1183
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001184 if (prepending) {
1185 /* If we're prepending, copy the text from the next line, minus
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001186 * the indentation that we already copied above. */
1187 strcat(new_line, next_line);
1188
1189 free(line->next->data);
1190 line->next->data = new_line;
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001191
1192 /* If the NO_NEWLINES flag isn't set, and text has been added to
1193 * the magicline, make a new magicline. */
1194 if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
1195 new_magicline();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001196 } else {
1197 /* Otherwise, make a new line and copy the text after where we
1198 * broke this line to the beginning of the new line. */
1199 splice_node(openfile->current, make_new_node(openfile->current),
1200 openfile->current->next);
1201
David Lawrence Ramsey219a8142005-11-22 22:08:01 +00001202 /* If the current line is the last line of the file, move the
1203 * last line of the file down to the next line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001204 if (openfile->filebot == openfile->current)
1205 openfile->filebot = openfile->current->next;
1206
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001207 openfile->current->next->data = new_line;
1208
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001209 openfile->totsize++;
1210 }
1211
1212 /* Step 3, clean up. Reposition the cursor and mark, and do some
1213 * other sundry things. */
1214
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001215 /* Set the prepend_wrap flag, so that later wraps of this line will
1216 * be prepended to the next line. */
1217 prepend_wrap = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001218
David Lawrence Ramsey2829aae2006-05-22 01:24:09 +00001219 /* Each line knows its number. We recalculate these if we inserted
1220 * a new line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001221 if (!prepending)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001222 renumber(line);
1223
1224 /* If the cursor was after the break point, we must move it. We
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001225 * also clear the prepend_wrap flag in this case. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001226 if (openfile->current_x > wrap_loc) {
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001227 prepend_wrap = FALSE;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001228
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001229 openfile->current = openfile->current->next;
1230 openfile->current_x -= wrap_loc
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001231#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001232 - indent_len
1233#endif
1234 ;
1235 openfile->placewewant = xplustabs();
1236 }
1237
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001238#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001239 /* If the mark was on this line after the wrap point, we move it
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001240 * down. If it was on the next line and we prepended to that line,
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001241 * we move it right. */
1242 if (openfile->mark_set) {
1243 if (openfile->mark_begin == line && openfile->mark_begin_x >
1244 wrap_loc) {
1245 openfile->mark_begin = line->next;
1246 openfile->mark_begin_x -= wrap_loc - indent_len + 1;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001247 } else if (prepending && openfile->mark_begin == line->next)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001248 openfile->mark_begin_x += after_break_len;
1249 }
1250#endif
1251
1252 return TRUE;
1253}
1254#endif /* !DISABLE_WRAPPING */
1255
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +00001256#if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001257/* We are trying to break a chunk off line. We find the last blank such
David Lawrence Ramseycd9a5f02005-09-20 06:12:54 +00001258 * that the display length to there is at most (goal + 1). If there is
1259 * no such blank, then we find the first blank. We then take the last
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001260 * blank in that group of blanks. The terminating '\0' counts as a
1261 * blank, as does a '\n' if newline is TRUE. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001262ssize_t break_line(const char *line, ssize_t goal
1263#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001264 , bool newln
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001265#endif
1266 )
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001267{
1268 ssize_t blank_loc = -1;
1269 /* Current tentative return value. Index of the last blank we
1270 * found with short enough display width. */
1271 ssize_t cur_loc = 0;
1272 /* Current index in line. */
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001273 size_t cur_pos = 0;
1274 /* Current column position in line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001275 int line_len;
1276
1277 assert(line != NULL);
1278
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001279 while (*line != '\0' && goal >= cur_pos) {
1280 line_len = parse_mbchar(line, NULL, &cur_pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001281
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001282 if (is_blank_mbchar(line)
1283#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001284 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001285#endif
1286 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001287 blank_loc = cur_loc;
1288
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001289#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001290 if (newln && *line == '\n')
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001291 break;
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001292#endif
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001293 }
1294
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001295 line += line_len;
1296 cur_loc += line_len;
1297 }
1298
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001299 if (goal >= cur_pos)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001300 /* In fact, the whole line displays shorter than goal. */
1301 return cur_loc;
1302
Chris Allegretta09b81242008-07-12 01:54:49 +00001303#ifndef DISABLE_HELP
1304 if (newln && blank_loc <= 0) {
1305 /* If blank was not found or was found only first character,
1306 * force line break. */
1307 cur_loc -= line_len;
1308 return cur_loc;
1309 }
1310#endif
1311
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001312 if (blank_loc == -1) {
1313 /* No blank was found that was short enough. */
1314 bool found_blank = FALSE;
David Lawrence Ramsey5ab12ca2005-09-20 17:52:52 +00001315 ssize_t found_blank_loc = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001316
1317 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001318 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001319
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001320 if (is_blank_mbchar(line)
1321#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001322 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001323#endif
1324 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001325 if (!found_blank)
1326 found_blank = TRUE;
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +00001327 found_blank_loc = cur_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001328 } else if (found_blank)
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +00001329 return found_blank_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001330
1331 line += line_len;
1332 cur_loc += line_len;
1333 }
1334
1335 return -1;
1336 }
1337
1338 /* Move to the last blank after blank_loc, if there is one. */
1339 line -= cur_loc;
1340 line += blank_loc;
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001341 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001342 line += line_len;
1343
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001344 while (*line != '\0' && (is_blank_mbchar(line)
1345#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001346 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001347#endif
1348 )) {
David Lawrence Ramseycd243f52006-05-19 23:27:16 +00001349#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001350 if (newln && *line == '\n')
David Lawrence Ramseycd243f52006-05-19 23:27:16 +00001351 break;
1352#endif
1353
David Lawrence Ramsey39bd1b32006-05-20 13:11:56 +00001354 line_len = parse_mbchar(line, NULL, NULL);
1355
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001356 line += line_len;
1357 blank_loc += line_len;
1358 }
1359
1360 return blank_loc;
1361}
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +00001362#endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001363
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001364#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001365/* The "indentation" of a line is the whitespace between the quote part
1366 * and the non-whitespace of the line. */
1367size_t indent_length(const char *line)
1368{
1369 size_t len = 0;
1370 char *blank_mb;
1371 int blank_mb_len;
1372
1373 assert(line != NULL);
1374
1375 blank_mb = charalloc(mb_cur_max());
1376
1377 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001378 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001379
1380 if (!is_blank_mbchar(blank_mb))
1381 break;
1382
1383 line += blank_mb_len;
1384 len += blank_mb_len;
1385 }
1386
1387 free(blank_mb);
1388
1389 return len;
1390}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001391#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001392
1393#ifndef DISABLE_JUSTIFY
1394/* justify_format() replaces blanks with spaces and multiple spaces by 1
1395 * (except it maintains up to 2 after a character in punct optionally
1396 * followed by a character in brackets, and removes all from the end).
1397 *
1398 * justify_format() might make paragraph->data shorter, and change the
1399 * actual pointer with null_at().
1400 *
1401 * justify_format() will not look at the first skip characters of
1402 * paragraph. skip should be at most strlen(paragraph->data). The
1403 * character at paragraph[skip + 1] must not be blank. */
1404void justify_format(filestruct *paragraph, size_t skip)
1405{
1406 char *end, *new_end, *new_paragraph_data;
1407 size_t shift = 0;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001408#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001409 size_t mark_shift = 0;
1410#endif
1411
1412 /* These four asserts are assumptions about the input data. */
1413 assert(paragraph != NULL);
1414 assert(paragraph->data != NULL);
1415 assert(skip < strlen(paragraph->data));
1416 assert(!is_blank_mbchar(paragraph->data + skip));
1417
1418 end = paragraph->data + skip;
1419 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
1420 strncpy(new_paragraph_data, paragraph->data, skip);
1421 new_end = new_paragraph_data + skip;
1422
1423 while (*end != '\0') {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001424 int end_len;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001425
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001426 /* If this character is blank, change it to a space if
1427 * necessary, and skip over all blanks after it. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001428 if (is_blank_mbchar(end)) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001429 end_len = parse_mbchar(end, NULL, NULL);
1430
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001431 *new_end = ' ';
1432 new_end++;
1433 end += end_len;
1434
1435 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001436 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001437
1438 end += end_len;
1439 shift += end_len;
1440
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001441#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001442 /* Keep track of the change in the current line. */
1443 if (openfile->mark_set && openfile->mark_begin ==
1444 paragraph && openfile->mark_begin_x >= end -
1445 paragraph->data)
1446 mark_shift += end_len;
1447#endif
1448 }
1449 /* If this character is punctuation optionally followed by a
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001450 * bracket and then followed by blanks, change no more than two
1451 * of the blanks to spaces if necessary, and skip over all
1452 * blanks after them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001453 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001454 end_len = parse_mbchar(end, NULL, NULL);
1455
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001456 while (end_len > 0) {
1457 *new_end = *end;
1458 new_end++;
1459 end++;
1460 end_len--;
1461 }
1462
1463 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001464 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001465
1466 while (end_len > 0) {
1467 *new_end = *end;
1468 new_end++;
1469 end++;
1470 end_len--;
1471 }
1472 }
1473
1474 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001475 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001476
1477 *new_end = ' ';
1478 new_end++;
1479 end += end_len;
1480 }
1481
1482 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001483 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001484
1485 *new_end = ' ';
1486 new_end++;
1487 end += end_len;
1488 }
1489
1490 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001491 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001492
1493 end += end_len;
1494 shift += end_len;
1495
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001496#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001497 /* Keep track of the change in the current line. */
1498 if (openfile->mark_set && openfile->mark_begin ==
1499 paragraph && openfile->mark_begin_x >= end -
1500 paragraph->data)
1501 mark_shift += end_len;
1502#endif
1503 }
1504 /* If this character is neither blank nor punctuation, leave it
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001505 * unchanged. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001506 } else {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001507 end_len = parse_mbchar(end, NULL, NULL);
1508
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001509 while (end_len > 0) {
1510 *new_end = *end;
1511 new_end++;
1512 end++;
1513 end_len--;
1514 }
1515 }
1516 }
1517
1518 assert(*end == '\0');
1519
1520 *new_end = *end;
1521
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001522 /* If there are spaces at the end of the line, remove them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001523 while (new_end > new_paragraph_data + skip &&
1524 *(new_end - 1) == ' ') {
1525 new_end--;
1526 shift++;
1527 }
1528
1529 if (shift > 0) {
1530 openfile->totsize -= shift;
1531 null_at(&new_paragraph_data, new_end - new_paragraph_data);
1532 free(paragraph->data);
1533 paragraph->data = new_paragraph_data;
1534
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001535#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001536 /* Adjust the mark coordinates to compensate for the change in
1537 * the current line. */
1538 if (openfile->mark_set && openfile->mark_begin == paragraph) {
1539 openfile->mark_begin_x -= mark_shift;
1540 if (openfile->mark_begin_x > new_end - new_paragraph_data)
1541 openfile->mark_begin_x = new_end - new_paragraph_data;
1542 }
1543#endif
1544 } else
1545 free(new_paragraph_data);
1546}
1547
1548/* The "quote part" of a line is the largest initial substring matching
1549 * the quote string. This function returns the length of the quote part
1550 * of the given line.
1551 *
1552 * Note that if !HAVE_REGEX_H then we match concatenated copies of
1553 * quotestr. */
1554size_t quote_length(const char *line)
1555{
1556#ifdef HAVE_REGEX_H
1557 regmatch_t matches;
1558 int rc = regexec(&quotereg, line, 1, &matches, 0);
1559
1560 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
1561 return 0;
1562 /* matches.rm_so should be 0, since the quote string should start
1563 * with the caret ^. */
1564 return matches.rm_eo;
1565#else /* !HAVE_REGEX_H */
1566 size_t qdepth = 0;
1567
1568 /* Compute quote depth level. */
1569 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
1570 qdepth += quotelen;
1571 return qdepth;
1572#endif /* !HAVE_REGEX_H */
1573}
1574
1575/* a_line and b_line are lines of text. The quotation part of a_line is
1576 * the first a_quote characters. Check that the quotation part of
1577 * b_line is the same. */
1578bool quotes_match(const char *a_line, size_t a_quote, const char
1579 *b_line)
1580{
1581 /* Here is the assumption about a_quote. */
1582 assert(a_quote == quote_length(a_line));
1583
1584 return (a_quote == quote_length(b_line) &&
1585 strncmp(a_line, b_line, a_quote) == 0);
1586}
1587
1588/* We assume a_line and b_line have no quote part. Then, we return
1589 * whether b_line could follow a_line in a paragraph. */
1590bool indents_match(const char *a_line, size_t a_indent, const char
1591 *b_line, size_t b_indent)
1592{
1593 assert(a_indent == indent_length(a_line));
1594 assert(b_indent == indent_length(b_line));
1595
1596 return (b_indent <= a_indent &&
1597 strncmp(a_line, b_line, b_indent) == 0);
1598}
1599
1600/* Is foo the beginning of a paragraph?
1601 *
1602 * A line of text consists of a "quote part", followed by an
1603 * "indentation part", followed by text. The functions quote_length()
1604 * and indent_length() calculate these parts.
1605 *
1606 * A line is "part of a paragraph" if it has a part not in the quote
1607 * part or the indentation.
1608 *
1609 * A line is "the beginning of a paragraph" if it is part of a
1610 * paragraph and
1611 * 1) it is the top line of the file, or
1612 * 2) the line above it is not part of a paragraph, or
1613 * 3) the line above it does not have precisely the same quote
1614 * part, or
1615 * 4) the indentation of this line is not an initial substring of
1616 * the indentation of the previous line, or
1617 * 5) this line has no quote part and some indentation, and
1618 * autoindent isn't turned on.
1619 * The reason for number 5) is that if autoindent isn't turned on,
1620 * then an indented line is expected to start a paragraph, as in
1621 * books. Thus, nano can justify an indented paragraph only if
1622 * autoindent is turned on. */
1623bool begpar(const filestruct *const foo)
1624{
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001625 size_t quote_len, indent_len, temp_id_len;
1626
1627 if (foo == NULL)
1628 return FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001629
1630 /* Case 1). */
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001631 if (foo == openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001632 return TRUE;
1633
1634 quote_len = quote_length(foo->data);
1635 indent_len = indent_length(foo->data + quote_len);
1636
1637 /* Not part of a paragraph. */
1638 if (foo->data[quote_len + indent_len] == '\0')
1639 return FALSE;
1640
1641 /* Case 3). */
1642 if (!quotes_match(foo->data, quote_len, foo->prev->data))
1643 return TRUE;
1644
1645 temp_id_len = indent_length(foo->prev->data + quote_len);
1646
1647 /* Case 2) or 5) or 4). */
1648 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
1649 (quote_len == 0 && indent_len > 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001650#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001651 && !ISSET(AUTOINDENT)
1652#endif
1653 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
1654 foo->data + quote_len, indent_len))
1655 return TRUE;
1656
1657 return FALSE;
1658}
1659
1660/* Is foo inside a paragraph? */
1661bool inpar(const filestruct *const foo)
1662{
1663 size_t quote_len;
1664
1665 if (foo == NULL)
1666 return FALSE;
1667
1668 quote_len = quote_length(foo->data);
1669
David Lawrence Ramsey21014032005-11-09 20:33:42 +00001670 return (foo->data[quote_len + indent_length(foo->data +
1671 quote_len)] != '\0');
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001672}
1673
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001674/* Move the next par_len lines, starting with first_line, into the
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001675 * justify buffer, leaving copies of those lines in place. Assume that
1676 * par_len is greater than zero, and that there are enough lines after
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001677 * first_line. */
1678void backup_lines(filestruct *first_line, size_t par_len)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001679{
1680 filestruct *top = first_line;
1681 /* The top of the paragraph we're backing up. */
1682 filestruct *bot = first_line;
1683 /* The bottom of the paragraph we're backing up. */
1684 size_t i;
1685 /* Generic loop variable. */
1686 size_t current_x_save = openfile->current_x;
1687 ssize_t fl_lineno_save = first_line->lineno;
1688 ssize_t edittop_lineno_save = openfile->edittop->lineno;
1689 ssize_t current_lineno_save = openfile->current->lineno;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001690#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001691 bool old_mark_set = openfile->mark_set;
1692 ssize_t mb_lineno_save = 0;
1693 size_t mark_begin_x_save = 0;
1694
1695 if (old_mark_set) {
1696 mb_lineno_save = openfile->mark_begin->lineno;
1697 mark_begin_x_save = openfile->mark_begin_x;
1698 }
1699#endif
1700
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001701 /* par_len will be one greater than the number of lines between
1702 * current and filebot if filebot is the last line in the
1703 * paragraph. */
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001704 assert(par_len > 0 && openfile->current->lineno + par_len <=
David Lawrence Ramsey24777c02005-12-01 05:49:08 +00001705 openfile->filebot->lineno + 1);
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001706
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001707 /* Move bot down par_len lines to the line after the last line of
1708 * the paragraph, if there is one. */
1709 for (i = par_len; i > 0 && bot != openfile->filebot; i--)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001710 bot = bot->next;
1711
1712 /* Move the paragraph from the current buffer's filestruct to the
1713 * justify buffer. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001714 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
David Lawrence Ramseyf0575cf2005-11-09 23:27:51 +00001715 (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001716
1717 /* Copy the paragraph back to the current buffer's filestruct from
1718 * the justify buffer. */
1719 copy_from_filestruct(jusbuffer, jusbottom);
1720
1721 /* Move upward from the last line of the paragraph to the first
1722 * line, putting first_line, edittop, current, and mark_begin at the
1723 * same lines in the copied paragraph that they had in the original
1724 * paragraph. */
David Lawrence Ramseyee43ea62007-04-22 15:04:05 +00001725 if (openfile->current != openfile->fileage) {
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001726 top = openfile->current->prev;
David Lawrence Ramseyee43ea62007-04-22 15:04:05 +00001727#ifndef NANO_TINY
1728 if (old_mark_set &&
1729 openfile->current->lineno == mb_lineno_save) {
1730 openfile->mark_begin = openfile->current;
1731 openfile->mark_begin_x = mark_begin_x_save;
1732 }
1733#endif
1734 } else
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001735 top = openfile->current;
David Lawrence Ramseye8d505b2005-11-10 03:40:45 +00001736 for (i = par_len; i > 0 && top != NULL; i--) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001737 if (top->lineno == fl_lineno_save)
1738 first_line = top;
1739 if (top->lineno == edittop_lineno_save)
1740 openfile->edittop = top;
1741 if (top->lineno == current_lineno_save)
1742 openfile->current = top;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001743#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001744 if (old_mark_set && top->lineno == mb_lineno_save) {
1745 openfile->mark_begin = top;
1746 openfile->mark_begin_x = mark_begin_x_save;
1747 }
1748#endif
1749 top = top->prev;
1750 }
1751
1752 /* Put current_x at the same place in the copied paragraph that it
1753 * had in the original paragraph. */
1754 openfile->current_x = current_x_save;
1755
1756 set_modified();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001757}
1758
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001759/* Find the beginning of the current paragraph if we're in one, or the
1760 * beginning of the next paragraph if we're not. Afterwards, save the
1761 * quote length and paragraph length in *quote and *par. Return TRUE if
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001762 * we found a paragraph, and FALSE if there was an error or we didn't
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001763 * find a paragraph.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001764 *
1765 * See the comment at begpar() for more about when a line is the
1766 * beginning of a paragraph. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001767bool find_paragraph(size_t *const quote, size_t *const par)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001768{
1769 size_t quote_len;
1770 /* Length of the initial quotation of the paragraph we search
1771 * for. */
1772 size_t par_len;
1773 /* Number of lines in the paragraph we search for. */
1774 filestruct *current_save;
1775 /* The line at the beginning of the paragraph we search for. */
1776 ssize_t current_y_save;
1777 /* The y-coordinate at the beginning of the paragraph we search
1778 * for. */
1779
1780#ifdef HAVE_REGEX_H
1781 if (quoterc != 0) {
1782 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1783 return FALSE;
1784 }
1785#endif
1786
1787 assert(openfile->current != NULL);
1788
David Lawrence Ramsey1be131a2005-11-11 03:55:52 +00001789 /* If we're at the end of the last line of the file, it means that
1790 * there aren't any paragraphs left, so get out. */
1791 if (openfile->current == openfile->filebot && openfile->current_x ==
1792 strlen(openfile->filebot->data))
1793 return FALSE;
1794
1795 /* If the current line isn't in a paragraph, move forward to the
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001796 * last line of the next paragraph, if any. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001797 if (!inpar(openfile->current)) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001798 do_para_end(FALSE);
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001799
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001800 /* If we end up past the beginning of the line, it means that
1801 * we're at the end of the last line of the file, and the line
1802 * isn't blank, in which case the last line of the file is the
1803 * last line of the next paragraph.
1804 *
1805 * Otherwise, if we end up on a line that's in a paragraph, it
1806 * means that we're on the line after the last line of the next
1807 * paragraph, in which case we should move back to the last line
1808 * of the next paragraph. */
1809 if (openfile->current_x == 0) {
1810 if (!inpar(openfile->current->prev))
1811 return FALSE;
1812 if (openfile->current != openfile->fileage)
1813 openfile->current = openfile->current->prev;
1814 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001815 }
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001816
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001817 /* If the current line isn't the first line of the paragraph, move
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001818 * back to the first line of the paragraph. */
1819 if (!begpar(openfile->current))
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001820 do_para_begin(FALSE);
1821
1822 /* Now current is the first line of the paragraph. Set quote_len to
1823 * the quotation length of that line, and set par_len to the number
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001824 * of lines in this paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001825 quote_len = quote_length(openfile->current->data);
1826 current_save = openfile->current;
1827 current_y_save = openfile->current_y;
1828 do_para_end(FALSE);
1829 par_len = openfile->current->lineno - current_save->lineno;
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001830
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001831 /* If we end up past the beginning of the line, it means that we're
1832 * at the end of the last line of the file, and the line isn't
1833 * blank, in which case the last line of the file is part of the
1834 * paragraph. */
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001835 if (openfile->current_x > 0)
1836 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001837 openfile->current = current_save;
1838 openfile->current_y = current_y_save;
1839
1840 /* Save the values of quote_len and par_len. */
1841 assert(quote != NULL && par != NULL);
1842
1843 *quote = quote_len;
1844 *par = par_len;
1845
1846 return TRUE;
1847}
1848
1849/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1850 * the current paragraph. */
1851void do_justify(bool full_justify)
1852{
1853 filestruct *first_par_line = NULL;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001854 /* Will be the first line of the justified paragraph(s), if any.
1855 * For restoring after unjustify. */
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001856 filestruct *last_par_line = NULL;
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001857 /* Will be the line after the last line of the justified
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001858 * paragraph(s), if any. Also for restoring after unjustify. */
David Lawrence Ramsey82b5deb2005-11-10 06:07:57 +00001859 bool filebot_inpar = FALSE;
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001860 /* Whether the text at filebot is part of the current
1861 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001862
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001863 /* We save these variables to be restored if the user
1864 * unjustifies. */
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001865 filestruct *edittop_save = openfile->edittop;
1866 filestruct *current_save = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001867 size_t current_x_save = openfile->current_x;
1868 size_t pww_save = openfile->placewewant;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001869 size_t totsize_save = openfile->totsize;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001870#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001871 filestruct *mark_begin_save = openfile->mark_begin;
1872 size_t mark_begin_x_save = openfile->mark_begin_x;
1873#endif
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001874 bool modified_save = openfile->modified;
1875
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001876 int kbinput;
1877 bool meta_key, func_key, s_or_t, ran_func, finished;
Chris Allegretta0018d8e2008-03-13 08:23:52 +00001878 const sc *s;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001879
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001880 /* Move to the beginning of the current line, so that justifying at
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001881 * the end of the last line of the file, if that line isn't blank,
1882 * will work the first time through. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001883 openfile->current_x = 0;
1884
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001885 /* If we're justifying the entire file, start at the beginning. */
1886 if (full_justify)
1887 openfile->current = openfile->fileage;
1888
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001889 while (TRUE) {
1890 size_t i;
1891 /* Generic loop variable. */
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001892 filestruct *curr_first_par_line;
1893 /* The first line of the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001894 size_t quote_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001895 /* Length of the initial quotation of the current
1896 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001897 size_t indent_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001898 /* Length of the initial indentation of the current
1899 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001900 size_t par_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001901 /* Number of lines in the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001902 ssize_t break_pos;
1903 /* Where we will break lines. */
1904 char *indent_string;
1905 /* The first indentation that doesn't match the initial
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001906 * indentation of the current paragraph. This is put at the
1907 * beginning of every line broken off the first justified
1908 * line of the paragraph. Note that this works because a
1909 * paragraph can only contain two indentations at most: the
1910 * initial one, and a different one starting on a line after
1911 * the first. See the comment at begpar() for more about
1912 * when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001913
1914 /* Find the first line of the paragraph to be justified. That
1915 * is the start of this paragraph if we're in one, or the start
1916 * of the next otherwise. Save the quote length and paragraph
1917 * length (number of lines). Don't refresh the screen yet,
1918 * since we'll do that after we justify.
1919 *
1920 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001921 * justifying the whole file, and we've found at least one
1922 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001923 * last line of the file, so set the last line of the text to be
1924 * justified to the last line of the file and break out of the
1925 * loop. Otherwise, it means that there are no paragraph(s) to
1926 * justify, so refresh the screen and get out. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001927 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001928 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001929 last_par_line = openfile->filebot;
1930 break;
1931 } else {
1932 edit_refresh();
1933 return;
1934 }
1935 }
1936
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001937 /* par_len will be one greater than the number of lines between
1938 * current and filebot if filebot is the last line in the
1939 * paragraph. Set filebot_inpar to TRUE if this is the case. */
1940 filebot_inpar = (openfile->current->lineno + par_len ==
1941 openfile->filebot->lineno + 1);
1942
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001943 /* If we haven't already done it, move the original paragraph(s)
1944 * to the justify buffer, splice a copy of the original
1945 * paragraph(s) into the file in the same place, and set
1946 * first_par_line to the first line of the copy. */
1947 if (first_par_line == NULL) {
1948 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00001949 openfile->filebot->lineno - openfile->current->lineno +
1950 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001951 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001952 first_par_line = openfile->current;
1953 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001954
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001955 /* Set curr_first_par_line to the first line of the current
1956 * paragraph. */
1957 curr_first_par_line = openfile->current;
1958
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001959 /* Initialize indent_string to a blank string. */
1960 indent_string = mallocstrcpy(NULL, "");
1961
1962 /* Find the first indentation in the paragraph that doesn't
David Lawrence Ramsey8602fd62006-05-28 18:43:21 +00001963 * match the indentation of the first line, and save it in
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001964 * indent_string. If all the indentations are the same, save
1965 * the indentation of the first line in indent_string. */
1966 {
1967 const filestruct *indent_line = openfile->current;
1968 bool past_first_line = FALSE;
1969
1970 for (i = 0; i < par_len; i++) {
1971 indent_len = quote_len +
1972 indent_length(indent_line->data + quote_len);
1973
1974 if (indent_len != strlen(indent_string)) {
1975 indent_string = mallocstrncpy(indent_string,
1976 indent_line->data, indent_len + 1);
1977 indent_string[indent_len] = '\0';
1978
1979 if (past_first_line)
1980 break;
1981 }
1982
1983 if (indent_line == openfile->current)
1984 past_first_line = TRUE;
1985
1986 indent_line = indent_line->next;
1987 }
1988 }
1989
1990 /* Now tack all the lines of the paragraph together, skipping
1991 * the quoting and indentation on all lines after the first. */
1992 for (i = 0; i < par_len - 1; i++) {
1993 filestruct *next_line = openfile->current->next;
1994 size_t line_len = strlen(openfile->current->data);
1995 size_t next_line_len =
1996 strlen(openfile->current->next->data);
1997
1998 indent_len = quote_len +
1999 indent_length(openfile->current->next->data +
2000 quote_len);
2001
2002 next_line_len -= indent_len;
2003 openfile->totsize -= indent_len;
2004
2005 /* We're just about to tack the next line onto this one. If
2006 * this line isn't empty, make sure it ends in a space. */
2007 if (line_len > 0 &&
2008 openfile->current->data[line_len - 1] != ' ') {
2009 line_len++;
2010 openfile->current->data =
2011 charealloc(openfile->current->data,
2012 line_len + 1);
2013 openfile->current->data[line_len - 1] = ' ';
2014 openfile->current->data[line_len] = '\0';
2015 openfile->totsize++;
2016 }
2017
2018 openfile->current->data =
2019 charealloc(openfile->current->data, line_len +
2020 next_line_len + 1);
2021 strcat(openfile->current->data, next_line->data +
2022 indent_len);
2023
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002024 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00002025 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002026 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002027 if (next_line == openfile->filebot)
2028 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002029
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002030#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002031 /* Adjust the mark coordinates to compensate for the change
2032 * in the next line. */
2033 if (openfile->mark_set && openfile->mark_begin ==
2034 next_line) {
2035 openfile->mark_begin = openfile->current;
2036 openfile->mark_begin_x += line_len - indent_len;
2037 }
2038#endif
2039
2040 unlink_node(next_line);
2041 delete_node(next_line);
2042
2043 /* If we've removed the next line, we need to go through
2044 * this line again. */
2045 i--;
2046
2047 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002048 openfile->totsize--;
2049 }
2050
2051 /* Call justify_format() on the paragraph, which will remove
2052 * excess spaces from it and change all blank characters to
2053 * spaces. */
2054 justify_format(openfile->current, quote_len +
2055 indent_length(openfile->current->data + quote_len));
2056
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002057 while (par_len > 0 && strlenpt(openfile->current->data) >
2058 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002059 size_t line_len = strlen(openfile->current->data);
2060
2061 indent_len = strlen(indent_string);
2062
2063 /* If this line is too long, try to wrap it to the next line
2064 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002065 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00002066 fill - strnlenpt(openfile->current->data, indent_len)
2067#ifndef DISABLE_HELP
2068 , FALSE
2069#endif
2070 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002071
2072 /* We can't break the line, or don't need to, so get out. */
2073 if (break_pos == -1 || break_pos + indent_len == line_len)
2074 break;
2075
2076 /* Move forward to the character after the indentation and
2077 * just after the space. */
2078 break_pos += indent_len + 1;
2079
2080 assert(break_pos <= line_len);
2081
2082 /* Make a new line, and copy the text after where we're
2083 * going to break this line to the beginning of the new
2084 * line. */
2085 splice_node(openfile->current,
2086 make_new_node(openfile->current),
2087 openfile->current->next);
2088
2089 /* If this paragraph is non-quoted, and autoindent isn't
2090 * turned on, set the indentation length to zero so that the
2091 * indentation is treated as part of the line. */
2092 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002093#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002094 && !ISSET(AUTOINDENT)
2095#endif
2096 )
2097 indent_len = 0;
2098
2099 /* Copy the text after where we're going to break the
2100 * current line to the next line. */
2101 openfile->current->next->data = charalloc(indent_len + 1 +
2102 line_len - break_pos);
2103 strncpy(openfile->current->next->data, indent_string,
2104 indent_len);
2105 strcpy(openfile->current->next->data + indent_len,
2106 openfile->current->data + break_pos);
2107
2108 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002109 openfile->totsize += indent_len + 1;
2110
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002111#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002112 /* Adjust the mark coordinates to compensate for the change
2113 * in the current line. */
2114 if (openfile->mark_set && openfile->mark_begin ==
2115 openfile->current && openfile->mark_begin_x >
2116 break_pos) {
2117 openfile->mark_begin = openfile->current->next;
2118 openfile->mark_begin_x -= break_pos - indent_len;
2119 }
2120#endif
2121
2122 /* Break the current line. */
2123 null_at(&openfile->current->data, break_pos);
2124
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002125 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00002126 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002127 if (openfile->filebot == openfile->current)
2128 openfile->filebot = openfile->filebot->next;
2129
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002130 /* Go to the next line. */
2131 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002132 openfile->current_y++;
2133 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002134 }
2135
2136 /* We're done breaking lines, so we don't need indent_string
2137 * anymore. */
2138 free(indent_string);
2139
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002140 /* Go to the next line, if possible. If there is no next line,
2141 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00002142 if (openfile->current != openfile->filebot) {
2143 openfile->current_y++;
2144 openfile->current = openfile->current->next;
2145 } else
2146 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002147
David Lawrence Ramseya6854682005-11-30 21:19:42 +00002148 /* Renumber the lines of the now-justified current paragraph,
2149 * since both find_paragraph() and edit_refresh() need the line
2150 * numbers to be right. */
2151 renumber(curr_first_par_line);
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002152
2153 /* We've just finished justifying the paragraph. If we're not
2154 * justifying the entire file, break out of the loop.
2155 * Otherwise, continue the loop so that we justify all the
2156 * paragraphs in the file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002157 if (!full_justify)
2158 break;
2159 }
2160
2161 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002162 * up. current_y and totsize have been maintained above. If we
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002163 * actually justified something, set last_par_line to the new end of
2164 * the paragraph. */
2165 if (first_par_line != NULL)
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002166 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002167
2168 edit_refresh();
2169
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002170#ifndef NANO_TINY
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002171 /* We're going to set jump_buf so that we return here after a
2172 * SIGWINCH instead of to main(). Indicate this. */
2173 jump_buf_main = FALSE;
2174
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002175 /* Return here after a SIGWINCH. */
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002176 sigsetjmp(jump_buf, 1);
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002177#endif
2178
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002179 statusbar(_("Can now UnJustify!"));
2180
2181 /* If constant cursor position display is on, make sure the current
2182 * cursor position will be properly displayed on the statusbar. */
2183 if (ISSET(CONST_UPDATE))
2184 do_cursorpos(TRUE);
2185
2186 /* Display the shortcut list with UnJustify. */
2187 shortcut_init(TRUE);
2188 display_main_list();
2189
2190 /* Now get a keystroke and see if it's unjustify. If not, put back
2191 * the keystroke and return. */
2192 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
2193 &finished, FALSE);
Chris Allegretta0018d8e2008-03-13 08:23:52 +00002194 s = get_shortcut(currmenu, &kbinput, &meta_key, &func_key);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002195
Chris Allegretta0018d8e2008-03-13 08:23:52 +00002196 if (s && s->scfunc == do_uncut_text) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002197 /* Splice the justify buffer back into the file, but only if we
2198 * actually justified something. */
2199 if (first_par_line != NULL) {
2200 filestruct *top_save;
2201
2202 /* Partition the filestruct so that it contains only the
2203 * text of the justified paragraph. */
2204 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00002205 last_par_line, filebot_inpar ?
2206 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002207
2208 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00002209 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002210 free_filestruct(openfile->fileage);
2211 openfile->fileage = jusbuffer;
2212 openfile->filebot = jusbottom;
2213
2214 top_save = openfile->fileage;
2215
2216 /* Unpartition the filestruct so that it contains all the
2217 * text again. Note that the justified paragraph has been
2218 * replaced with the unjustified paragraph. */
2219 unpartition_filestruct(&filepart);
2220
2221 /* Renumber starting with the beginning line of the old
2222 * partition. */
2223 renumber(top_save);
2224
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002225 /* Restore the justify we just did (ungrateful user!). */
2226 openfile->edittop = edittop_save;
2227 openfile->current = current_save;
2228 openfile->current_x = current_x_save;
2229 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002230 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002231#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002232 if (openfile->mark_set) {
2233 openfile->mark_begin = mark_begin_save;
2234 openfile->mark_begin_x = mark_begin_x_save;
2235 }
2236#endif
2237 openfile->modified = modified_save;
2238
2239 /* Clear the justify buffer. */
2240 jusbuffer = NULL;
2241
2242 if (!openfile->modified)
2243 titlebar(NULL);
2244 edit_refresh();
2245 }
2246 } else {
2247 unget_kbinput(kbinput, meta_key, func_key);
2248
2249 /* Blow away the text in the justify buffer. */
2250 free_filestruct(jusbuffer);
2251 jusbuffer = NULL;
2252 }
2253
2254 blank_statusbar();
2255
2256 /* Display the shortcut list with UnCut. */
2257 shortcut_init(FALSE);
2258 display_main_list();
2259}
2260
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002261/* Justify the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002262void do_justify_void(void)
2263{
2264 do_justify(FALSE);
2265}
2266
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002267/* Justify the entire file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002268void do_full_justify(void)
2269{
2270 do_justify(TRUE);
2271}
2272#endif /* !DISABLE_JUSTIFY */
2273
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002274#ifndef DISABLE_SPELLER
2275/* A word is misspelled in the file. Let the user replace it. We
2276 * return FALSE if the user cancels. */
2277bool do_int_spell_fix(const char *word)
2278{
2279 char *save_search, *save_replace;
2280 size_t match_len, current_x_save = openfile->current_x;
2281 size_t pww_save = openfile->placewewant;
Chris Allegretta10f868d2008-03-14 04:08:51 +00002282 bool meta_key = FALSE, func_key = FALSE;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002283 filestruct *edittop_save = openfile->edittop;
2284 filestruct *current_save = openfile->current;
2285 /* Save where we are. */
2286 bool canceled = FALSE;
2287 /* The return value. */
2288 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002289#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002290 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
2291#endif
2292#ifdef HAVE_REGEX_H
2293 bool regexp_set = ISSET(USE_REGEXP);
2294#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002295#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002296 bool old_mark_set = openfile->mark_set;
2297 bool added_magicline = FALSE;
2298 /* Whether we added a magicline after filebot. */
2299 bool right_side_up = FALSE;
2300 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2301 * FALSE if (current, current_x) is. */
2302 filestruct *top, *bot;
2303 size_t top_x, bot_x;
2304#endif
2305
2306 /* Make sure spell-check is case sensitive. */
2307 SET(CASE_SENSITIVE);
2308
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002309#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002310 /* Make sure spell-check goes forward only. */
2311 UNSET(BACKWARDS_SEARCH);
2312#endif
2313#ifdef HAVE_REGEX_H
2314 /* Make sure spell-check doesn't use regular expressions. */
2315 UNSET(USE_REGEXP);
2316#endif
2317
2318 /* Save the current search/replace strings. */
2319 search_init_globals();
2320 save_search = last_search;
2321 save_replace = last_replace;
2322
2323 /* Set the search/replace strings to the misspelled word. */
2324 last_search = mallocstrcpy(NULL, word);
2325 last_replace = mallocstrcpy(NULL, word);
2326
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002327#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002328 if (old_mark_set) {
2329 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002330 * contains only the marked text; if the NO_NEWLINES flag isn't
2331 * set, keep track of whether the text will have a magicline
2332 * added when we're done correcting misspelled words; and
2333 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002334 mark_order((const filestruct **)&top, &top_x,
2335 (const filestruct **)&bot, &bot_x, &right_side_up);
2336 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002337 if (!ISSET(NO_NEWLINES))
2338 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002339 openfile->mark_set = FALSE;
2340 }
2341#endif
2342
2343 /* Start from the top of the file. */
2344 openfile->edittop = openfile->fileage;
2345 openfile->current = openfile->fileage;
2346 openfile->current_x = (size_t)-1;
2347 openfile->placewewant = 0;
2348
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002349 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002350 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002351 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002352 &match_len)) {
2353 if (is_whole_word(openfile->current_x, openfile->current->data,
2354 word)) {
2355 size_t xpt = xplustabs();
2356 char *exp_word = display_string(openfile->current->data,
2357 xpt, strnlenpt(openfile->current->data,
2358 openfile->current_x + match_len) - xpt, FALSE);
2359
2360 edit_refresh();
2361
2362 do_replace_highlight(TRUE, exp_word);
2363
2364 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002365 canceled = (do_prompt(FALSE,
2366#ifndef DISABLE_TABCOMP
2367 TRUE,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002368#endif
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002369 MSPELL, word,
Chris Allegretta10f868d2008-03-14 04:08:51 +00002370 &meta_key, &func_key,
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002371#ifndef NANO_TINY
2372 NULL,
2373#endif
David Lawrence Ramsey68160072006-02-18 21:32:29 +00002374 edit_refresh, _("Edit a replacement")) == -1);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002375
2376 do_replace_highlight(FALSE, exp_word);
2377
2378 free(exp_word);
2379
2380 if (!canceled && strcmp(word, answer) != 0) {
2381 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002382 do_replace_loop(TRUE, &canceled, openfile->current,
2383 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002384 }
2385
2386 break;
2387 }
2388 }
2389
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002390#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002391 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002392 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2393 * added a magicline, remove it now. */
2394 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002395 remove_magicline();
2396
2397 /* Put the beginning and the end of the mark at the beginning
2398 * and the end of the spell-checked text. */
2399 if (openfile->fileage == openfile->filebot)
2400 bot_x += top_x;
2401 if (right_side_up) {
2402 openfile->mark_begin_x = top_x;
2403 current_x_save = bot_x;
2404 } else {
2405 current_x_save = top_x;
2406 openfile->mark_begin_x = bot_x;
2407 }
2408
2409 /* Unpartition the filestruct so that it contains all the text
2410 * again, and turn the mark back on. */
2411 unpartition_filestruct(&filepart);
2412 openfile->mark_set = TRUE;
2413 }
2414#endif
2415
2416 /* Restore the search/replace strings. */
2417 free(last_search);
2418 last_search = save_search;
2419 free(last_replace);
2420 last_replace = save_replace;
2421
2422 /* Restore where we were. */
2423 openfile->edittop = edittop_save;
2424 openfile->current = current_save;
2425 openfile->current_x = current_x_save;
2426 openfile->placewewant = pww_save;
2427
2428 /* Restore case sensitivity setting. */
2429 if (!case_sens_set)
2430 UNSET(CASE_SENSITIVE);
2431
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002432#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002433 /* Restore search/replace direction. */
2434 if (backwards_search_set)
2435 SET(BACKWARDS_SEARCH);
2436#endif
2437#ifdef HAVE_REGEX_H
2438 /* Restore regular expression usage setting. */
2439 if (regexp_set)
2440 SET(USE_REGEXP);
2441#endif
2442
2443 return !canceled;
2444}
2445
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002446/* Internal (integrated) spell checking using the spell program,
2447 * filtered through the sort and uniq programs. Return NULL for normal
2448 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002449const char *do_int_speller(const char *tempfile_name)
2450{
2451 char *read_buff, *read_buff_ptr, *read_buff_word;
2452 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
2453 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
2454 pid_t pid_spell, pid_sort, pid_uniq;
2455 int spell_status, sort_status, uniq_status;
2456
2457 /* Create all three pipes up front. */
2458 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
2459 pipe(uniq_fd) == -1)
2460 return _("Could not create pipe");
2461
2462 statusbar(_("Creating misspelled word list, please wait..."));
2463
2464 /* A new process to run spell in. */
2465 if ((pid_spell = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002466 /* Child continues (i.e. future spell process). */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002467 close(spell_fd[0]);
2468
2469 /* Replace the standard input with the temp file. */
2470 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
2471 goto close_pipes_and_exit;
2472
2473 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
2474 goto close_pipes_and_exit;
2475
2476 close(tempfile_fd);
2477
2478 /* Send spell's standard output to the pipe. */
2479 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2480 goto close_pipes_and_exit;
2481
2482 close(spell_fd[1]);
2483
David Lawrence Ramsey3239ff22005-11-29 20:01:06 +00002484 /* Start the spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002485 execlp("spell", "spell", NULL);
2486
2487 /* This should not be reached if spell is found. */
2488 exit(1);
2489 }
2490
2491 /* Parent continues here. */
2492 close(spell_fd[1]);
2493
2494 /* A new process to run sort in. */
2495 if ((pid_sort = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002496 /* Child continues (i.e. future spell process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002497 * standard input with the standard output of the old pipe. */
2498 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
2499 goto close_pipes_and_exit;
2500
2501 close(spell_fd[0]);
2502
2503 /* Send sort's standard output to the new pipe. */
2504 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2505 goto close_pipes_and_exit;
2506
2507 close(sort_fd[1]);
2508
2509 /* Start the sort program. Use -f to remove mixed case. If
2510 * this isn't portable, let me know. */
2511 execlp("sort", "sort", "-f", NULL);
2512
2513 /* This should not be reached if sort is found. */
2514 exit(1);
2515 }
2516
2517 close(spell_fd[0]);
2518 close(sort_fd[1]);
2519
2520 /* A new process to run uniq in. */
2521 if ((pid_uniq = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002522 /* Child continues (i.e. future uniq process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002523 * standard input with the standard output of the old pipe. */
2524 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
2525 goto close_pipes_and_exit;
2526
2527 close(sort_fd[0]);
2528
2529 /* Send uniq's standard output to the new pipe. */
2530 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2531 goto close_pipes_and_exit;
2532
2533 close(uniq_fd[1]);
2534
2535 /* Start the uniq program; we are using PATH. */
2536 execlp("uniq", "uniq", NULL);
2537
2538 /* This should not be reached if uniq is found. */
2539 exit(1);
2540 }
2541
2542 close(sort_fd[0]);
2543 close(uniq_fd[1]);
2544
2545 /* The child process was not forked successfully. */
2546 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
2547 close(uniq_fd[0]);
2548 return _("Could not fork");
2549 }
2550
2551 /* Get the system pipe buffer size. */
2552 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
2553 close(uniq_fd[0]);
2554 return _("Could not get size of pipe buffer");
2555 }
2556
2557 /* Read in the returned spelling errors. */
2558 read_buff_read = 0;
2559 read_buff_size = pipe_buff_size + 1;
2560 read_buff = read_buff_ptr = charalloc(read_buff_size);
2561
2562 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
2563 pipe_buff_size)) > 0) {
2564 read_buff_read += bytesread;
2565 read_buff_size += pipe_buff_size;
2566 read_buff = read_buff_ptr = charealloc(read_buff,
2567 read_buff_size);
2568 read_buff_ptr += read_buff_read;
2569 }
2570
2571 *read_buff_ptr = '\0';
2572 close(uniq_fd[0]);
2573
2574 /* Process the spelling errors. */
2575 read_buff_word = read_buff_ptr = read_buff;
2576
2577 while (*read_buff_ptr != '\0') {
2578 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
2579 *read_buff_ptr = '\0';
2580 if (read_buff_word != read_buff_ptr) {
2581 if (!do_int_spell_fix(read_buff_word)) {
2582 read_buff_word = read_buff_ptr;
2583 break;
2584 }
2585 }
2586 read_buff_word = read_buff_ptr + 1;
2587 }
2588 read_buff_ptr++;
2589 }
2590
2591 /* Special case: the last word doesn't end with '\r' or '\n'. */
2592 if (read_buff_word != read_buff_ptr)
2593 do_int_spell_fix(read_buff_word);
2594
2595 free(read_buff);
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002596 search_replace_abort();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002597 edit_refresh();
2598
2599 /* Process the end of the spell process. */
2600 waitpid(pid_spell, &spell_status, 0);
2601 waitpid(pid_sort, &sort_status, 0);
2602 waitpid(pid_uniq, &uniq_status, 0);
2603
2604 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
2605 return _("Error invoking \"spell\"");
2606
2607 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
2608 return _("Error invoking \"sort -f\"");
2609
2610 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
2611 return _("Error invoking \"uniq\"");
2612
2613 /* Otherwise... */
2614 return NULL;
2615
2616 close_pipes_and_exit:
2617 /* Don't leak any handles. */
2618 close(tempfile_fd);
2619 close(spell_fd[0]);
2620 close(spell_fd[1]);
2621 close(sort_fd[0]);
2622 close(sort_fd[1]);
2623 close(uniq_fd[0]);
2624 close(uniq_fd[1]);
2625 exit(1);
2626}
2627
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002628/* External (alternate) spell checking. Return NULL for normal
2629 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002630const char *do_alt_speller(char *tempfile_name)
2631{
2632 int alt_spell_status;
2633 size_t current_x_save = openfile->current_x;
2634 size_t pww_save = openfile->placewewant;
2635 ssize_t current_y_save = openfile->current_y;
2636 ssize_t lineno_save = openfile->current->lineno;
2637 pid_t pid_spell;
2638 char *ptr;
2639 static int arglen = 3;
2640 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002641#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002642 bool old_mark_set = openfile->mark_set;
2643 bool added_magicline = FALSE;
2644 /* Whether we added a magicline after filebot. */
2645 bool right_side_up = FALSE;
2646 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2647 * FALSE if (current, current_x) is. */
2648 filestruct *top, *bot;
2649 size_t top_x, bot_x;
2650 ssize_t mb_lineno_save = 0;
2651 /* We're going to close the current file, and open the output of
2652 * the alternate spell command. The line that mark_begin points
2653 * to will be freed, so we save the line number and restore it
2654 * afterwards. */
2655 size_t totsize_save = openfile->totsize;
2656 /* Our saved value of totsize, used when we spell-check a marked
2657 * selection. */
2658
2659 if (old_mark_set) {
2660 /* If the mark is on, save the number of the line it starts on,
2661 * and then turn the mark off. */
2662 mb_lineno_save = openfile->mark_begin->lineno;
2663 openfile->mark_set = FALSE;
2664 }
2665#endif
2666
2667 endwin();
2668
2669 /* Set up an argument list to pass execvp(). */
2670 if (spellargs == NULL) {
2671 spellargs = (char **)nmalloc(arglen * sizeof(char *));
2672
2673 spellargs[0] = strtok(alt_speller, " ");
2674 while ((ptr = strtok(NULL, " ")) != NULL) {
2675 arglen++;
2676 spellargs = (char **)nrealloc(spellargs, arglen *
2677 sizeof(char *));
2678 spellargs[arglen - 3] = ptr;
2679 }
2680 spellargs[arglen - 1] = NULL;
2681 }
2682 spellargs[arglen - 2] = tempfile_name;
2683
2684 /* Start a new process for the alternate speller. */
2685 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey2e2112c2005-11-29 05:39:31 +00002686 /* Start alternate spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002687 execvp(spellargs[0], spellargs);
2688
2689 /* Should not be reached, if alternate speller is found!!! */
2690 exit(1);
2691 }
2692
2693 /* If we couldn't fork, get out. */
2694 if (pid_spell < 0)
2695 return _("Could not fork");
2696
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002697#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002698 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002699 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002700 allow_pending_sigwinch(FALSE);
2701#endif
2702
2703 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002704 wait(&alt_spell_status);
2705
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00002706 /* Reenter curses mode. */
2707 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002708
2709 /* Restore the terminal to its previous state. */
2710 terminal_init();
2711
2712 /* Turn the cursor back on for sure. */
2713 curs_set(1);
2714
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002715 /* The screen might have been resized. If it has, reinitialize all
2716 * the windows based on the new screen dimensions. */
2717 window_init();
2718
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002719 if (!WIFEXITED(alt_spell_status) ||
2720 WEXITSTATUS(alt_spell_status) != 0) {
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002721 char *alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002722 char *invoke_error = _("Error invoking \"%s\"");
2723
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002724#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002725 /* Turn the mark back on if it was on before. */
2726 openfile->mark_set = old_mark_set;
2727#endif
2728
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002729 alt_spell_error =
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002730 charalloc(strlen(invoke_error) +
2731 strlen(alt_speller) + 1);
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002732 sprintf(alt_spell_error, invoke_error, alt_speller);
2733 return alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002734 }
2735
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002736#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002737 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002738 /* If the mark is on, partition the filestruct so that it
2739 * contains only the marked text; if the NO_NEWLINES flag isn't
2740 * set, keep track of whether the text will have a magicline
2741 * added when we're done correcting misspelled words; and
2742 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002743 mark_order((const filestruct **)&top, &top_x,
2744 (const filestruct **)&bot, &bot_x, &right_side_up);
2745 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002746 if (!ISSET(NO_NEWLINES))
2747 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002748
2749 /* Get the number of characters in the marked text, and subtract
2750 * it from the saved value of totsize. */
2751 totsize_save -= get_totsize(top, bot);
2752 }
2753#endif
2754
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002755 /* Replace the text of the current buffer with the spell-checked
2756 * text. */
2757 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002758
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002759#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002760 if (old_mark_set) {
2761 filestruct *top_save = openfile->fileage;
2762
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002763 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2764 * added a magicline, remove it now. */
2765 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002766 remove_magicline();
2767
2768 /* Put the beginning and the end of the mark at the beginning
2769 * and the end of the spell-checked text. */
2770 if (openfile->fileage == openfile->filebot)
2771 bot_x += top_x;
2772 if (right_side_up) {
2773 openfile->mark_begin_x = top_x;
2774 current_x_save = bot_x;
2775 } else {
2776 current_x_save = top_x;
2777 openfile->mark_begin_x = bot_x;
2778 }
2779
2780 /* Unpartition the filestruct so that it contains all the text
2781 * again. Note that we've replaced the marked text originally
2782 * in the partition with the spell-checked marked text in the
2783 * temp file. */
2784 unpartition_filestruct(&filepart);
2785
2786 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002787 * partition. Also add the number of characters in the
2788 * spell-checked marked text to the saved value of totsize, and
2789 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002790 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002791 totsize_save += openfile->totsize;
2792 openfile->totsize = totsize_save;
2793
2794 /* Assign mark_begin to the line where the mark began before. */
2795 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2796 current_y_save, 0);
2797 openfile->mark_begin = openfile->current;
2798
2799 /* Assign mark_begin_x to the location in mark_begin where the
2800 * mark began before, adjusted for any shortening of the
2801 * line. */
2802 openfile->mark_begin_x = openfile->current_x;
2803
2804 /* Turn the mark back on. */
2805 openfile->mark_set = TRUE;
2806 }
2807#endif
2808
2809 /* Go back to the old position, and mark the file as modified. */
2810 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2811 set_modified();
2812
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002813#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002814 /* Handle a pending SIGWINCH again. */
2815 allow_pending_sigwinch(TRUE);
2816#endif
2817
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002818 return NULL;
2819}
2820
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002821/* Spell check the current file. If an alternate spell checker is
2822 * specified, use it. Otherwise, use the internal spell checker. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002823void do_spell(void)
2824{
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002825 bool status;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002826 FILE *temp_file;
2827 char *temp = safe_tempfile(&temp_file);
2828 const char *spell_msg;
2829
2830 if (temp == NULL) {
David Lawrence Ramseyf0e3ca62006-05-03 13:11:00 +00002831 statusbar(_("Error writing temp file: %s"), strerror(errno));
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002832 return;
2833 }
2834
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002835 status =
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002836#ifndef NANO_TINY
David Lawrence Ramsey0e1df432007-01-12 02:58:12 +00002837 openfile->mark_set ? write_marked_file(temp, temp_file, TRUE,
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002838 OVERWRITE) :
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002839#endif
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002840 write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002841
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002842 if (!status) {
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002843 statusbar(_("Error writing temp file: %s"), strerror(errno));
2844 free(temp);
2845 return;
2846 }
2847
2848 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2849 do_int_speller(temp);
2850 unlink(temp);
2851 free(temp);
2852
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002853 currmenu = MMAIN;
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002854
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002855 /* If the spell-checker printed any error messages onscreen, make
2856 * sure that they're cleared off. */
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002857 total_refresh();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002858
2859 if (spell_msg != NULL) {
2860 if (errno == 0)
2861 /* Don't display an error message of "Success". */
2862 statusbar(_("Spell checking failed: %s"), spell_msg);
2863 else
2864 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2865 strerror(errno));
2866 } else
2867 statusbar(_("Finished checking spelling"));
2868}
2869#endif /* !DISABLE_SPELLER */
2870
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002871#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002872/* Our own version of "wc". Note that its character counts are in
2873 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002874void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002875{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002876 size_t words = 0, chars = 0;
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002877 ssize_t nlines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002878 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002879 size_t pww_save = openfile->placewewant;
2880 filestruct *current_save = openfile->current;
2881 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002882 filestruct *top, *bot;
2883 size_t top_x, bot_x;
2884
2885 if (old_mark_set) {
2886 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002887 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002888 mark_order((const filestruct **)&top, &top_x,
2889 (const filestruct **)&bot, &bot_x, NULL);
2890 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002891 openfile->mark_set = FALSE;
2892 }
2893
2894 /* Start at the top of the file. */
2895 openfile->current = openfile->fileage;
2896 openfile->current_x = 0;
2897 openfile->placewewant = 0;
2898
2899 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002900 * part of a word, as "wc -w" does), without updating the screen,
2901 * until we reach the end of the file, incrementing the total word
2902 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002903 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002904 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002905 if (do_next_word(TRUE, FALSE))
2906 words++;
2907 }
2908
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002909 /* Get the total line and character counts, as "wc -l" and "wc -c"
2910 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002911 if (old_mark_set) {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002912 nlines = openfile->filebot->lineno -
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002913 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002914 chars = get_totsize(openfile->fileage, openfile->filebot);
2915
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002916 /* Unpartition the filestruct so that it contains all the text
2917 * again, and turn the mark back on. */
2918 unpartition_filestruct(&filepart);
2919 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002920 } else {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002921 nlines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002922 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002923 }
2924
2925 /* Restore where we were. */
2926 openfile->current = current_save;
2927 openfile->current_x = current_x_save;
2928 openfile->placewewant = pww_save;
2929
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002930 /* Display the total word, line, and character counts on the
2931 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002932 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002933 _("In Selection: ") : "", (unsigned long)words, (long)nlines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002934 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002935}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002936#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002937
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002938/* Get verbatim input. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002939void do_verbatim_input(void)
2940{
2941 int *kbinput;
2942 size_t kbinput_len, i;
2943 char *output;
2944
David Lawrence Ramseyf451d6a2006-05-27 16:02:48 +00002945 /* TRANSLATORS: This is displayed when the next keystroke will be
2946 * inserted verbatim. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002947 statusbar(_("Verbatim Input"));
2948
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002949 /* Read in all the verbatim characters. */
2950 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2951
David Lawrence Ramseya620e682006-05-27 18:19:03 +00002952 /* If constant cursor position display is on, make sure the current
2953 * cursor position will be properly displayed on the statusbar.
2954 * Otherwise, blank the statusbar. */
2955 if (ISSET(CONST_UPDATE))
2956 do_cursorpos(TRUE);
2957 else {
2958 blank_statusbar();
2959 wnoutrefresh(bottomwin);
2960 }
David Lawrence Ramsey6fb66892006-05-27 17:39:19 +00002961
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002962 /* Display all the verbatim characters at once, not filtering out
2963 * control characters. */
2964 output = charalloc(kbinput_len + 1);
2965
2966 for (i = 0; i < kbinput_len; i++)
2967 output[i] = (char)kbinput[i];
2968 output[i] = '\0';
2969
David Lawrence Ramseyad36bdc2006-12-02 17:22:21 +00002970 free(kbinput);
2971
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002972 do_output(output, kbinput_len, TRUE);
2973
2974 free(output);
2975}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00002976