blob: eeca2c80c68feec5c279e55eed13433e04c68756 [file] [log] [blame]
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001/* $Id$ */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002/**************************************************************************
David Lawrence Ramsey4005fc62005-10-08 06:12:41 +00003 * text.c *
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00004 * *
David Lawrence Ramseyd8a1d372007-10-11 05:01:32 +00005 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 *
6 * Free Software Foundation, Inc. *
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00007 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
David Lawrence Ramseyd0035b42007-08-11 05:17:36 +00009 * the Free Software Foundation; either version 3, or (at your option) *
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000010 * any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
20 * 02110-1301, USA. *
21 * *
22 **************************************************************************/
23
David Lawrence Ramsey034b9942005-12-08 02:47:10 +000024#include "proto.h"
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000025
David Lawrence Ramseyee11c6a2005-11-02 19:42:02 +000026#include <stdio.h>
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000027#include <signal.h>
28#include <unistd.h>
29#include <string.h>
30#include <fcntl.h>
31#include <sys/wait.h>
32#include <errno.h>
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000033
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000034#ifndef NANO_TINY
David Lawrence Ramsey8779a172005-11-08 16:45:22 +000035static pid_t pid = -1;
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +000036 /* The PID of the forked process in execute_command(), for use
37 * with the cancel_command() signal handler. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000038#endif
39#ifndef DISABLE_WRAPPING
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +000040static bool prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000041 /* Should we prepend wrapped text to the next line? */
42#endif
43#ifndef DISABLE_JUSTIFY
44static filestruct *jusbottom = NULL;
David Lawrence Ramseyae4c3a62005-09-20 19:46:39 +000045 /* Pointer to the end of the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +000046#endif
47
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000048#ifndef NANO_TINY
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +000049/* Toggle the mark. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000050void do_mark(void)
51{
52 openfile->mark_set = !openfile->mark_set;
53 if (openfile->mark_set) {
54 statusbar(_("Mark Set"));
55 openfile->mark_begin = openfile->current;
56 openfile->mark_begin_x = openfile->current_x;
57 } else {
David Lawrence Ramsey7b0531a2006-07-31 01:30:31 +000058 statusbar(_("Mark Unset"));
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000059 openfile->mark_begin = NULL;
60 openfile->mark_begin_x = 0;
61 edit_refresh();
62 }
63}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000064#endif /* !NANO_TINY */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000065
David Lawrence Ramseyf1982f02006-12-10 17:57:09 +000066/* Delete the character under the cursor. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000067void do_delete(void)
68{
69 bool do_refresh = FALSE;
70 /* Do we have to call edit_refresh(), or can we get away with
David Lawrence Ramsey75e9dfe2006-05-18 17:50:08 +000071 * just update_line()? */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000072
Chris Allegretta07fcc4c2008-07-10 20:13:04 +000073#ifndef NANO_TINY
Chris Allegretta14c86202008-08-03 04:48:05 +000074 update_undo(DEL);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +000075#endif
76
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000077 assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data));
78
79 openfile->placewewant = xplustabs();
80
81 if (openfile->current->data[openfile->current_x] != '\0') {
82 int char_buf_len = parse_mbchar(openfile->current->data +
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +000083 openfile->current_x, NULL, NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000084 size_t line_len = strlen(openfile->current->data +
85 openfile->current_x);
86
87 assert(openfile->current_x < strlen(openfile->current->data));
88
89 /* Let's get dangerous. */
90 charmove(&openfile->current->data[openfile->current_x],
91 &openfile->current->data[openfile->current_x +
92 char_buf_len], line_len - char_buf_len + 1);
93
94 null_at(&openfile->current->data, openfile->current_x +
95 line_len - char_buf_len);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000096#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000097 if (openfile->mark_set && openfile->mark_begin ==
98 openfile->current && openfile->current_x <
99 openfile->mark_begin_x)
100 openfile->mark_begin_x -= char_buf_len;
101#endif
102 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000103 } else if (openfile->current != openfile->filebot) {
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000104 filestruct *foo = openfile->current->next;
105
106 assert(openfile->current_x == strlen(openfile->current->data));
107
108 /* If we're deleting at the end of a line, we need to call
109 * edit_refresh(). */
110 if (openfile->current->data[openfile->current_x] == '\0')
111 do_refresh = TRUE;
112
113 openfile->current->data = charealloc(openfile->current->data,
114 openfile->current_x + strlen(foo->data) + 1);
115 strcpy(openfile->current->data + openfile->current_x,
116 foo->data);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000117#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000118 if (openfile->mark_set && openfile->mark_begin ==
119 openfile->current->next) {
120 openfile->mark_begin = openfile->current;
121 openfile->mark_begin_x += openfile->current_x;
122 }
123#endif
124 if (openfile->filebot == foo)
125 openfile->filebot = openfile->current;
126
127 unlink_node(foo);
128 delete_node(foo);
129 renumber(openfile->current);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000130 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000131
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000132 /* If the NO_NEWLINES flag isn't set, and text has been added to
133 * the magicline as a result of deleting at the end of the line
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000134 * before filebot, add a new magicline. */
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000135 if (!ISSET(NO_NEWLINES) && openfile->current ==
136 openfile->filebot && openfile->current->data[0] != '\0')
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000137 new_magicline();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000138 } else
139 return;
140
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000141 set_modified();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000142
143#ifdef ENABLE_COLOR
144 /* If color syntaxes are available and turned on, we need to call
145 * edit_refresh(). */
146 if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX))
147 do_refresh = TRUE;
148#endif
149
150 if (do_refresh)
151 edit_refresh();
152 else
153 update_line(openfile->current, openfile->current_x);
154}
155
David Lawrence Ramsey5b7b3e32005-12-31 21:22:54 +0000156/* Backspace over one character. That is, move the cursor left one
David Lawrence Ramsey84d22e32006-11-08 13:05:50 +0000157 * character, and then delete the character under the cursor. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000158void do_backspace(void)
159{
160 if (openfile->current != openfile->fileage ||
161 openfile->current_x > 0) {
David Lawrence Ramsey1c3bfa92005-09-13 04:53:44 +0000162 do_left();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000163 do_delete();
164 }
165}
166
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000167/* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number
168 * of spaces that a tab would normally take up. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000169void do_tab(void)
170{
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000171#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000172 if (ISSET(TABS_TO_SPACES)) {
173 char *output;
David Lawrence Ramsey90b07fc2005-10-07 15:57:48 +0000174 size_t output_len = 0, new_pww = xplustabs();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000175
176 do {
177 new_pww++;
178 output_len++;
179 } while (new_pww % tabsize != 0);
180
181 output = charalloc(output_len + 1);
182
183 charset(output, ' ', output_len);
184 output[output_len] = '\0';
185
186 do_output(output, output_len, TRUE);
187
188 free(output);
189 } else {
190#endif
191 do_output("\t", 1, TRUE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000192#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000193 }
194#endif
195}
196
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000197#ifndef NANO_TINY
David Lawrence Ramseye44cd2d2007-01-11 22:54:55 +0000198/* Indent or unindent the current line (or, if the mark is on, all lines
199 * covered by the mark) len columns, depending on whether len is
200 * positive or negative. If the TABS_TO_SPACES flag is set, indent or
201 * unindent by len spaces. Otherwise, indent or unindent by (len /
202 * tabsize) tabs and (len % tabsize) spaces. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000203void do_indent(ssize_t cols)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000204{
205 bool indent_changed = FALSE;
206 /* Whether any indenting or unindenting was done. */
207 bool unindent = FALSE;
208 /* Whether we're unindenting text. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000209 char *line_indent = NULL;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000210 /* The text added to each line in order to indent it. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000211 size_t line_indent_len = 0;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000212 /* The length of the text added to each line in order to indent
213 * it. */
214 filestruct *top, *bot, *f;
215 size_t top_x, bot_x;
216
217 assert(openfile->current != NULL && openfile->current->data != NULL);
218
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000219 /* If cols is zero, get out. */
220 if (cols == 0)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000221 return;
222
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000223 /* If cols is negative, make it positive and set unindent to
224 * TRUE. */
225 if (cols < 0) {
226 cols = -cols;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000227 unindent = TRUE;
228 /* Otherwise, we're indenting, in which case the file will always be
229 * modified, so set indent_changed to TRUE. */
230 } else
231 indent_changed = TRUE;
232
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000233 /* If the mark is on, use all lines covered by the mark. */
234 if (openfile->mark_set)
235 mark_order((const filestruct **)&top, &top_x,
236 (const filestruct **)&bot, &bot_x, NULL);
237 /* Otherwise, use the current line. */
238 else {
239 top = openfile->current;
240 bot = top;
241 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000242
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000243 if (!unindent) {
244 /* Set up the text we'll be using as indentation. */
245 line_indent = charalloc(cols + 1);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000246
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000247 if (ISSET(TABS_TO_SPACES)) {
248 /* Set the indentation to cols spaces. */
249 charset(line_indent, ' ', cols);
250 line_indent_len = cols;
251 } else {
252 /* Set the indentation to (cols / tabsize) tabs and (cols %
253 * tabsize) spaces. */
254 size_t num_tabs = cols / tabsize;
255 size_t num_spaces = cols % tabsize;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000256
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000257 charset(line_indent, '\t', num_tabs);
258 charset(line_indent + num_tabs, ' ', num_spaces);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000259
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000260 line_indent_len = num_tabs + num_spaces;
261 }
262
263 line_indent[line_indent_len] = '\0';
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000264 }
265
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000266 /* Go through each line of the text. */
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000267 for (f = top; f != bot->next; f = f->next) {
268 size_t line_len = strlen(f->data);
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000269 size_t indent_len = indent_length(f->data);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000270
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000271 if (!unindent) {
272 /* If we're indenting, add the characters in line_indent to
273 * the beginning of the non-whitespace text of this line. */
274 f->data = charealloc(f->data, line_len +
275 line_indent_len + 1);
276 charmove(&f->data[indent_len + line_indent_len],
277 &f->data[indent_len], line_len - indent_len + 1);
278 strncpy(f->data + indent_len, line_indent, line_indent_len);
279 openfile->totsize += line_indent_len;
280
281 /* Keep track of the change in the current line. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000282 if (openfile->mark_set && f == openfile->mark_begin &&
283 openfile->mark_begin_x >= indent_len)
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000284 openfile->mark_begin_x += line_indent_len;
285
286 if (f == openfile->current && openfile->current_x >=
287 indent_len)
288 openfile->current_x += line_indent_len;
289
290 /* If the NO_NEWLINES flag isn't set, and this is the
291 * magicline, add a new magicline. */
292 if (!ISSET(NO_NEWLINES) && f == openfile->filebot)
293 new_magicline();
294 } else {
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000295 size_t indent_col = strnlenpt(f->data, indent_len);
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000296 /* The length in columns of the indentation on this
297 * line. */
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000298
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000299 if (cols <= indent_col) {
300 size_t indent_new = actual_x(f->data, indent_col -
301 cols);
David Lawrence Ramsey5bb77272006-05-06 14:37:33 +0000302 /* The length of the indentation remaining on
303 * this line after we unindent. */
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000304 size_t indent_shift = indent_len - indent_new;
David Lawrence Ramsey5bb77272006-05-06 14:37:33 +0000305 /* The change in the indentation on this line
306 * after we unindent. */
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000307
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000308 /* If we're unindenting, and there's at least cols
David Lawrence Ramsey2ca3fc92006-05-01 16:48:12 +0000309 * columns' worth of indentation at the beginning of the
310 * non-whitespace text of this line, remove it. */
311 charmove(&f->data[indent_new], &f->data[indent_len],
312 line_len - indent_shift - indent_new + 1);
313 null_at(&f->data, line_len - indent_shift + 1);
314 openfile->totsize -= indent_shift;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000315
David Lawrence Ramsey2e8fac62006-04-29 15:44:58 +0000316 /* Keep track of the change in the current line. */
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000317 if (openfile->mark_set && f == openfile->mark_begin &&
David Lawrence Ramseyeb4f90e2006-05-05 14:22:42 +0000318 openfile->mark_begin_x > indent_new) {
319 if (openfile->mark_begin_x <= indent_len)
320 openfile->mark_begin_x = indent_new;
321 else
322 openfile->mark_begin_x -= indent_shift;
323 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000324
David Lawrence Ramseyac37b042006-05-03 12:59:05 +0000325 if (f == openfile->current && openfile->current_x >
David Lawrence Ramseyeb4f90e2006-05-05 14:22:42 +0000326 indent_new) {
327 if (openfile->current_x <= indent_len)
328 openfile->current_x = indent_new;
329 else
330 openfile->current_x -= indent_shift;
331 }
David Lawrence Ramsey7194a612006-04-29 16:11:21 +0000332
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000333 /* We've unindented, so set indent_changed to TRUE. */
334 if (!indent_changed)
335 indent_changed = TRUE;
336 }
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000337 }
338 }
339
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000340 if (!unindent)
David Lawrence Ramsey25456862006-05-05 15:43:52 +0000341 /* Clean up. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000342 free(line_indent);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000343
344 if (indent_changed) {
345 /* Mark the file as modified. */
346 set_modified();
347
348 /* Update the screen. */
349 edit_refresh();
350 }
351}
352
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000353/* Indent the current line, or all lines covered by the mark if the mark
354 * is on, tabsize columns. */
355void do_indent_void(void)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000356{
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000357 do_indent(tabsize);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000358}
359
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000360/* Unindent the current line, or all lines covered by the mark if the
361 * mark is on, tabsize columns. */
362void do_unindent(void)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000363{
David Lawrence Ramseyaee00d42006-07-05 18:42:22 +0000364 do_indent(-tabsize);
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000365}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000366
367/* Undo the last thing(s) we did */
368void do_undo(void)
369{
370 undo *u = openfile->current_undo;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000371 filestruct *f = openfile->current, *t;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000372 int len = 0;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000373 char *undidmsg, *data;
Chris Allegretta14c86202008-08-03 04:48:05 +0000374 filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000375
376 if (!u) {
377 statusbar(_("Nothing in undo buffer!"));
378 return;
379 }
380
381
382 if (u->lineno <= f->lineno)
383 for (; f->prev != NULL && f->lineno != u->lineno; f = f->prev)
384 ;
385 else
386 for (; f->next != NULL && f->lineno != u->lineno; f = f->next)
387 ;
388 if (f->lineno != u->lineno) {
389 statusbar(_("Couldnt match current undo line"));
390 return;
391 }
392#ifdef DEBUG
393 fprintf(stderr, "data we're about to undo = \"%s\"\n", f->data);
394 fprintf(stderr, "Undo running for type %d\n", u->type);
395#endif
396
Chris Allegrettafa406942008-07-13 16:44:19 +0000397 openfile->current_x = u->begin;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000398 switch(u->type) {
399 case ADD:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000400 undidmsg = _("text add");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000401 len = strlen(f->data) - strlen(u->strdata) + 1;
402 data = charalloc(len);
403 strncpy(data, f->data, u->begin);
404 strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]);
405 free(f->data);
406 f->data = data;
407 break;
408 case DEL:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000409 undidmsg = _("text delete");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000410 len = strlen(f->data) + strlen(u->strdata) + 1;
411 data = charalloc(len);
412
413 strncpy(data, f->data, u->begin);
414 strcpy(&data[u->begin], u->strdata);
415 strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]);
416 free(f->data);
417 f->data = data;
Chris Allegretta0b499d42008-07-14 07:18:22 +0000418 if (u->xflags == UNDO_DEL_BACKSPACE)
419 openfile->current_x += strlen(u->strdata);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000420 break;
421 case SPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000422 undidmsg = _("line split");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000423 free(f->data);
424 f->data = mallocstrcpy(NULL, u->strdata);
425 if (f->next != NULL) {
426 filestruct *tmp = f->next;
427 unlink_node(tmp);
428 delete_node(tmp);
429 }
Chris Allegretta0b499d42008-07-14 07:18:22 +0000430 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000431 break;
432 case UNSPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000433 undidmsg = _("line join");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000434 t = make_new_node(f);
435 t->data = mallocstrcpy(NULL, u->strdata);
436 data = mallocstrncpy(NULL, f->data, u->begin);
437 data[u->begin] = '\0';
438 free(f->data);
439 f->data = data;
440 splice_node(f, t, f->next);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000441 renumber(f);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000442 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000443 case CUT:
444 case CUTTOEND:
445 undidmsg = _("text cut");
446 cutbuffer = copy_filestruct(u->cutbuffer);
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000447
448 /* Compute cutbottom for the uncut using out copy */
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000449 for (cutbottom = cutbuffer; cutbottom->next != NULL; cutbottom = cutbottom->next)
450 ;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000451
452 /* Get to where we need to uncut from */
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000453 if (u->mark_set && u->mark_begin_lineno < u->lineno)
454 do_gotolinecolumn(u->mark_begin_lineno, u->mark_begin_x+1, FALSE, FALSE, FALSE, FALSE);
455 else
456 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000457
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000458 do_uncut_text();
459 free_filestruct(cutbuffer);
Chris Allegretta8f761122008-08-01 06:52:15 +0000460 cutbuffer = NULL;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000461 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000462 case INSERT:
463 undidmsg = _("text insert");
464 cutbuffer = NULL;
465 cutbottom = NULL;
466 /* When we updated mark_begin_lineno in update_undo, it was effectively how many line
467 were inserted due to being partitioned before read_file was called. So we
468 add its value here */
469 openfile->mark_begin = fsfromline(u->lineno + u->mark_begin_lineno - 1);
470 openfile->mark_begin_x = 0;
471 openfile->mark_set = TRUE;
472 do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
473 cut_marked();
474 u->cutbuffer = cutbuffer;
475 u->cutbottom = cutbottom;
476 cutbuffer = oldcutbuffer;
477 cutbottom = oldcutbottom;
478 openfile->mark_set = FALSE;
479 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000480 case REPLACE:
481 undidmsg = _("text replace");
482 data = u->strdata;
483 u->strdata = f->data;
484 f->data = data;
485 break;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000486 default:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000487 undidmsg = _("wtf?");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000488 break;
489
490 }
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000491 do_gotolinecolumn(u->lineno, u->begin, FALSE, FALSE, FALSE, TRUE);
492 statusbar(_("Undid action (%s)"), undidmsg);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000493 openfile->current_undo = openfile->current_undo->next;
Chris Allegretta6f681c12008-08-08 03:02:03 +0000494 openfile->last_action = OTHER;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000495}
496
497void do_redo(void)
498{
499 undo *u = openfile->undotop;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000500 filestruct *f = openfile->current, *t;
Chris Allegrettaea577872008-08-03 20:19:42 +0000501 filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000502 int len = 0, i;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000503 char *undidmsg, *data;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000504
505 for (; u != NULL && u->next != openfile->current_undo; u = u->next)
506 ;
507 if (!u) {
508 statusbar(_("Nothing to re-do!"));
509 return;
510 }
511 if (u->next != openfile->current_undo) {
512 statusbar(_("Can't find previous undo to re-do, argh"));
513 return;
514 }
515
516 if (u->lineno <= f->lineno)
517 for (; f->prev != NULL && f->lineno != u->lineno; f = f->prev)
518 ;
519 else
520 for (; f->next != NULL && f->lineno != u->lineno; f = f->next)
521 ;
522 if (f->lineno != u->lineno) {
523 statusbar(_("Couldnt match current undo line"));
524 return;
525 }
526#ifdef DEBUG
527 fprintf(stderr, "data we're about to redo = \"%s\"\n", f->data);
528 fprintf(stderr, "Redo running for type %d\n", u->type);
529#endif
530
531 switch(u->type) {
532 case ADD:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000533 undidmsg = _("text add");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000534 len = strlen(f->data) + strlen(u->strdata) + 1;
535 data = charalloc(len);
Chris Allegretta1f37c452008-08-01 04:11:57 +0000536 strncpy(data, f->data, u->begin);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000537 strcpy(&data[u->begin], u->strdata);
538 strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]);
539 free(f->data);
540 f->data = data;
541 break;
542 case DEL:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000543 undidmsg = _("text delete");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000544 len = strlen(f->data) + strlen(u->strdata) + 1;
545 data = charalloc(len);
546 strncpy(data, f->data, u->begin);
547 strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]);
548 free(f->data);
549 f->data = data;
550 break;
551 case SPLIT:
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000552 undidmsg = _("line split");
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000553 t = make_new_node(f);
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;
Chris Allegretta6f681c12008-08-08 03:02:03 +0000641 openfile->last_action = OTHER;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000642
643}
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000644#endif /* !NANO_TINY */
645
David Lawrence Ramseyb0e04c02005-12-08 07:24:54 +0000646/* Someone hits Enter *gasp!* */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000647void do_enter(void)
648{
649 filestruct *newnode = make_new_node(openfile->current);
650 size_t extra = 0;
651
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000652 assert(openfile->current != NULL && xopenfile->current->data != NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000653
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000654#ifndef NANO_TINY
Chris Allegretta14c86202008-08-03 04:48:05 +0000655 update_undo(SPLIT);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000656
657
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000658 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
659 if (ISSET(AUTOINDENT)) {
660 /* If we are breaking the line in the indentation, the new
661 * indentation should have only current_x characters, and
662 * current_x should not change. */
663 extra = indent_length(openfile->current->data);
664 if (extra > openfile->current_x)
665 extra = openfile->current_x;
666 }
667#endif
668 newnode->data = charalloc(strlen(openfile->current->data +
669 openfile->current_x) + extra + 1);
670 strcpy(&newnode->data[extra], openfile->current->data +
671 openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000672#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000673 if (ISSET(AUTOINDENT)) {
674 strncpy(newnode->data, openfile->current->data, extra);
675 openfile->totsize += mbstrlen(newnode->data);
676 }
677#endif
678 null_at(&openfile->current->data, openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000679#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000680 if (openfile->mark_set && openfile->current ==
681 openfile->mark_begin && openfile->current_x <
682 openfile->mark_begin_x) {
683 openfile->mark_begin = newnode;
684 openfile->mark_begin_x += extra - openfile->current_x;
685 }
686#endif
687 openfile->current_x = extra;
688
689 if (openfile->current == openfile->filebot)
690 openfile->filebot = newnode;
691 splice_node(openfile->current, newnode,
692 openfile->current->next);
693
694 renumber(openfile->current);
695 openfile->current = newnode;
696
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000697 openfile->totsize++;
698 set_modified();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000699
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000700 openfile->placewewant = xplustabs();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000701
702 edit_refresh();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000703}
704
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000705#ifndef NANO_TINY
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000706/* Send a SIGKILL (unconditional kill) to the forked process in
707 * execute_command(). */
David Lawrence Ramsey8befda62005-12-06 19:39:56 +0000708RETSIGTYPE cancel_command(int signal)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000709{
710 if (kill(pid, SIGKILL) == -1)
711 nperror("kill");
712}
713
David Lawrence Ramsey0ed71712005-11-08 23:09:47 +0000714/* Execute command in a shell. Return TRUE on success. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000715bool execute_command(const char *command)
716{
717 int fd[2];
718 FILE *f;
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000719 char *shellenv;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000720 struct sigaction oldaction, newaction;
721 /* Original and temporary handlers for SIGINT. */
722 bool sig_failed = FALSE;
723 /* Did sigaction() fail without changing the signal handlers? */
724
725 /* Make our pipes. */
726 if (pipe(fd) == -1) {
727 statusbar(_("Could not pipe"));
728 return FALSE;
729 }
730
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000731 /* Check $SHELL for the shell to use. If it isn't set, use
David Lawrence Ramsey1932dfb2005-11-29 05:52:49 +0000732 * /bin/sh. Note that $SHELL should contain only a path, with no
733 * arguments. */
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000734 shellenv = getenv("SHELL");
735 if (shellenv == NULL)
736 shellenv = "/bin/sh";
737
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000738 /* Fork a child. */
739 if ((pid = fork()) == 0) {
740 close(fd[0]);
741 dup2(fd[1], fileno(stdout));
742 dup2(fd[1], fileno(stderr));
743
744 /* If execl() returns at all, there was an error. */
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000745 execl(shellenv, tail(shellenv), "-c", command, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000746 exit(0);
747 }
748
David Lawrence Ramseyc838a4c2006-04-26 18:33:50 +0000749 /* Continue as parent. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000750 close(fd[1]);
751
752 if (pid == -1) {
753 close(fd[0]);
754 statusbar(_("Could not fork"));
755 return FALSE;
756 }
757
758 /* Before we start reading the forked command's output, we set
759 * things up so that Ctrl-C will cancel the new process. */
760
761 /* Enable interpretation of the special control keys so that we get
762 * SIGINT when Ctrl-C is pressed. */
763 enable_signals();
764
765 if (sigaction(SIGINT, NULL, &newaction) == -1) {
766 sig_failed = TRUE;
767 nperror("sigaction");
768 } else {
769 newaction.sa_handler = cancel_command;
770 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
771 sig_failed = TRUE;
772 nperror("sigaction");
773 }
774 }
775
776 /* Note that now oldaction is the previous SIGINT signal handler,
777 * to be restored later. */
778
779 f = fdopen(fd[0], "rb");
780 if (f == NULL)
781 nperror("fdopen");
782
Chris Allegretta14c86202008-08-03 04:48:05 +0000783 read_file(f, "stdin", TRUE);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000784
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000785 if (wait(NULL) == -1)
786 nperror("wait");
787
788 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
789 nperror("sigaction");
790
David Lawrence Ramsey8b9c91b2007-12-18 01:28:53 +0000791 /* Restore the terminal to its previous state. In the process,
792 * disable interpretation of the special control keys so that we can
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000793 * use Ctrl-C for other things. */
David Lawrence Ramsey8b9c91b2007-12-18 01:28:53 +0000794 terminal_init();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000795
796 return TRUE;
797}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000798
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000799/* Add a new undo struct to the top of the current pile */
Chris Allegretta14c86202008-08-03 04:48:05 +0000800void add_undo(undo_type current_action)
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000801{
Chris Allegretta8f761122008-08-01 06:52:15 +0000802 undo *u;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000803 char *data;
Chris Allegretta14c86202008-08-03 04:48:05 +0000804 openfilestruct *fs = openfile;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000805
Chris Allegretta8f761122008-08-01 06:52:15 +0000806 /* Ugh, if we were called while cutting not-to-end, non-marked and on the same lineno,
807 we need to abort here */
808 u = fs->current_undo;
809 if (u && u->type == CUT && !u->mark_set && u->lineno == fs->current->lineno)
810 return;
811
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000812 /* Blow away the old undo stack if we are starting from the middle */
Chris Allegretta91a18622008-08-01 03:50:20 +0000813 while (fs->undotop != NULL && fs->undotop != fs->current_undo) {
814 undo *u2 = fs->undotop;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000815 fs->undotop = fs->undotop->next;
Chris Allegretta91a18622008-08-01 03:50:20 +0000816 if (u2->strdata != NULL)
817 free(u2->strdata);
Chris Allegretta6f681c12008-08-08 03:02:03 +0000818 if (u2->cutbuffer)
Chris Allegrettaea577872008-08-03 20:19:42 +0000819 free_filestruct(u2->cutbuffer);
Chris Allegretta91a18622008-08-01 03:50:20 +0000820 free(u2);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000821 }
822
Chris Allegretta8f761122008-08-01 06:52:15 +0000823 /* Allocate and initialize a new undo type */
824 u = nmalloc(sizeof(undo));
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000825 u->type = current_action;
826 u->lineno = fs->current->lineno;
827 u->begin = fs->current_x;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000828 u->next = fs->undotop;
829 fs->undotop = u;
830 fs->current_undo = u;
Chris Allegretta91a18622008-08-01 03:50:20 +0000831 u->strdata = NULL;
832 u->cutbuffer = NULL;
833 u->cutbottom = NULL;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000834 u->mark_begin_lineno = 0;
835 u->mark_begin_x = 0;
836 u->linescut = 0;
Chris Allegretta91a18622008-08-01 03:50:20 +0000837 u->xflags = 0;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000838
839 switch (u->type) {
840 /* We need to start copying data into the undo buffer or we wont be able
841 to restore it later */
842 case ADD:
843 data = charalloc(2);
844 data[0] = fs->current->data[fs->current_x];
845 data[1] = '\0';
846 u->strdata = data;
847 break;
848 case DEL:
849 if (u->begin != strlen(fs->current->data)) {
850 data = mallocstrncpy(NULL, &fs->current->data[u->begin], 2);
851 data[1] = '\0';
852 u->strdata = data;
853 break;
854 }
855 /* Else purposely fall into unsplit code */
856 current_action = u->type = UNSPLIT;
857 case UNSPLIT:
858 if (fs->current->next) {
859 data = mallocstrcpy(NULL, fs->current->next->data);
860 u->strdata = data;
861 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000862 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000863 case INSERT:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000864 case SPLIT:
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000865 case REPLACE:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000866 data = mallocstrcpy(NULL, fs->current->data);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000867 u->strdata = data;
868 break;
869 case CUT:
870 case CUTTOEND:
871 u->mark_set = openfile->mark_set;
872 if (u->mark_set) {
873 u->mark_begin_lineno = openfile->mark_begin->lineno;
874 u->mark_begin_x = openfile->mark_begin_x;
875 }
876 u->to_end = (current_action == CUTTOEND);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000877 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000878 case UNCUT:
879 break;
880 case OTHER:
881 statusbar(_("OOPS. Tried to add unknown thing to undo struct, I'd save your work"));
882 break;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000883 }
884
885#ifdef DEBUG
886 fprintf(stderr, "fs->current->data = \"%s\", current_x = %d, u->begin = %d, type = %d\n",
887 fs->current->data, fs->current_x, u->begin, current_action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000888 fprintf(stderr, "left update_add...\n");
889#endif
890 fs->last_action = current_action;
891}
892
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000893/* Update an undo item, or determine whether a new one
894 is really needed and bounce the data to add_undo
895 instead. The latter functionality just feels
896 gimmicky and may just be more hassle than
897 it's worth, so it should be axed if needed. */
Chris Allegretta14c86202008-08-03 04:48:05 +0000898void update_undo(undo_type action)
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000899{
900 undo *u;
901 char *data;
902 int len = 0;
Chris Allegretta14c86202008-08-03 04:48:05 +0000903 openfilestruct *fs = openfile;
Chris Allegretta91a18622008-08-01 03:50:20 +0000904
905#ifdef DEBUG
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000906 fprintf(stderr, "action = %d, fs->last_action = %d, openfile->current->lineno = %d",
907 action, fs->last_action, openfile->current->lineno);
908 if (fs->current_undo)
909 fprintf(stderr, "fs->current_undo->lineno = %d\n", fs->current_undo->lineno);
910 else
911 fprintf(stderr, "\n");
Chris Allegretta91a18622008-08-01 03:50:20 +0000912#endif
913
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000914 /* Change to an add if we're not using the same undo struct
915 that we should be using */
916 if (action != fs->last_action
Chris Allegretta14c86202008-08-03 04:48:05 +0000917 || (action != CUT && action != CUTTOEND && action != INSERT
Chris Allegretta91a18622008-08-01 03:50:20 +0000918 && openfile->current->lineno != fs->current_undo->lineno)) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000919 add_undo(action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000920 return;
921 }
922
923 assert(fs->undotop != NULL);
924 u = fs->undotop;
925
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000926 switch (u->type) {
927 case ADD:
928#ifdef DEBUG
929 fprintf(stderr, "fs->current->data = \"%s\", current_x = %d, u->begin = %d\n",
930 fs->current->data, fs->current_x, u->begin);
931#endif
932 len = strlen(u->strdata) + 2;
933 data = nrealloc((void *) u->strdata, len * sizeof(char *));
934 data[len-2] = fs->current->data[fs->current_x];
935 data[len-1] = '\0';
936 u->strdata = (char *) data;
937#ifdef DEBUG
938 fprintf(stderr, "current undo data now \"%s\"\n", u->strdata);
939#endif
940 break;
941 case DEL:
942 len = strlen(u->strdata) + 2;
943 assert(len > 2);
944 if (fs->current_x == u->begin) {
945 /* They're deleting */
Chris Allegretta0b499d42008-07-14 07:18:22 +0000946 if (!u->xflags)
947 u->xflags = UNDO_DEL_DEL;
948 else if (u->xflags != UNDO_DEL_DEL) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000949 add_undo(action);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000950 return;
951 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000952 data = charalloc(len);
953 strcpy(data, u->strdata);
954 data[len-2] = fs->current->data[fs->current_x];;
955 data[len-1] = '\0';
956 free(u->strdata);
957 u->strdata = data;
958 } else if (fs->current_x == u->begin - 1) {
959 /* They're backspacing */
Chris Allegretta0b499d42008-07-14 07:18:22 +0000960 if (!u->xflags)
961 u->xflags = UNDO_DEL_BACKSPACE;
962 else if (u->xflags != UNDO_DEL_BACKSPACE) {
Chris Allegretta14c86202008-08-03 04:48:05 +0000963 add_undo(action);
Chris Allegretta0b499d42008-07-14 07:18:22 +0000964 return;
965 }
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000966 data = charalloc(len);
967 data[0] = fs->current->data[fs->current_x];
968 strcpy(&data[1], u->strdata);
969 free(u->strdata);
970 u->strdata = data;
971 u->begin--;
972 } else {
973 /* They deleted something else on the line */
Chris Allegretta14c86202008-08-03 04:48:05 +0000974 add_undo(DEL);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000975 return;
976 }
977#ifdef DEBUG
Chris Allegretta0b499d42008-07-14 07:18:22 +0000978 fprintf(stderr, "current undo data now \"%s\"\nu->begin = %d\n", u->strdata, u->begin);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000979#endif
980 break;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000981 case CUT:
982 case CUTTOEND:
983 case UNCUT:
Chris Allegretta8f761122008-08-01 06:52:15 +0000984 if (u->cutbuffer)
985 free(u->cutbuffer);
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000986 u->cutbuffer = copy_filestruct(cutbuffer);
987 u->cutbottom = cutbottom;
Chris Allegrettad31ddb72008-08-01 07:29:06 +0000988 u->linescut++;
Chris Allegretta12dc8ca2008-07-31 04:24:04 +0000989 break;
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000990 case REPLACE:
Chris Allegretta14c86202008-08-03 04:48:05 +0000991 add_undo(action);
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000992 break;
Chris Allegretta14c86202008-08-03 04:48:05 +0000993 case INSERT:
994 u->mark_begin_lineno = openfile->current->lineno;
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000995 case SPLIT:
996 case UNSPLIT:
Chris Allegretta3c1131a2008-08-02 22:31:01 +0000997 /* These cases are handled by the earlier check for a new line and action */
998 case OTHER:
Chris Allegretta07fcc4c2008-07-10 20:13:04 +0000999 break;
1000 }
1001
1002#ifdef DEBUG
1003 fprintf(stderr, "Done in udpate_undo (type was %d)\n", action);
1004#endif
1005 if (fs->last_action != action) {
1006#ifdef DEBUG
1007 fprintf(stderr, "Starting add_undo for new action as it does not match last_action\n");
1008#endif
Chris Allegretta14c86202008-08-03 04:48:05 +00001009 add_undo(action);
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00001010 }
1011 fs->last_action = action;
1012}
1013
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001014#endif /* !NANO_TINY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001015
1016#ifndef DISABLE_WRAPPING
David Lawrence Ramseyef0d5a72006-05-22 02:08:49 +00001017/* Unset the prepend_wrap flag. We need to do this as soon as we do
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001018 * something other than type text. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001019void wrap_reset(void)
1020{
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001021 prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001022}
1023
1024/* We wrap the given line. Precondition: we assume the cursor has been
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001025 * moved forward since the last typed character. Return TRUE if we
1026 * wrapped, and FALSE otherwise. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001027bool do_wrap(filestruct *line)
1028{
1029 size_t line_len;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001030 /* The length of the line we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001031 ssize_t wrap_loc;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001032 /* The index of line->data where we wrap. */
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001033#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001034 const char *indent_string = NULL;
1035 /* Indentation to prepend to the new line. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001036 size_t indent_len = 0;
1037 /* The length of indent_string. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001038#endif
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001039 const char *after_break;
1040 /* The text after the wrap point. */
1041 size_t after_break_len;
1042 /* The length of after_break. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001043 bool prepending = FALSE;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001044 /* Do we prepend to the next line? */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001045 const char *next_line = NULL;
1046 /* The next line, minus indentation. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001047 size_t next_line_len = 0;
1048 /* The length of next_line. */
1049 char *new_line = NULL;
1050 /* The line we create. */
1051 size_t new_line_len = 0;
1052 /* The eventual length of new_line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001053
1054 /* There are three steps. First, we decide where to wrap. Then, we
1055 * create the new wrap line. Finally, we clean up. */
1056
1057 /* Step 1, finding where to wrap. We are going to add a new line
1058 * after a blank character. In this step, we call break_line() to
1059 * get the location of the last blank we can break the line at, and
David Lawrence Ramseyd4686b82006-06-11 19:14:14 +00001060 * set wrap_loc to the location of the character after it, so that
1061 * the blank is preserved at the end of the line.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001062 *
1063 * If there is no legal wrap point, or we reach the last character
1064 * of the line while trying to find one, we should return without
1065 * wrapping. Note that if autoindent is turned on, we don't break
1066 * at the end of it! */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001067 assert(line != NULL && line->data != NULL);
1068
1069 /* Save the length of the line. */
1070 line_len = strlen(line->data);
1071
1072 /* Find the last blank where we can break the line. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001073 wrap_loc = break_line(line->data, fill
1074#ifndef DISABLE_HELP
1075 , FALSE
1076#endif
1077 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001078
1079 /* If we couldn't break the line, or we've reached the end of it, we
1080 * don't wrap. */
1081 if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
1082 return FALSE;
1083
1084 /* Otherwise, move forward to the character just after the blank. */
1085 wrap_loc += move_mbright(line->data + wrap_loc, 0);
1086
1087 /* If we've reached the end of the line, we don't wrap. */
1088 if (line->data[wrap_loc] == '\0')
1089 return FALSE;
1090
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001091#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001092 /* If autoindent is turned on, and we're on the character just after
1093 * the indentation, we don't wrap. */
1094 if (ISSET(AUTOINDENT)) {
1095 /* Get the indentation of this line. */
1096 indent_string = line->data;
1097 indent_len = indent_length(indent_string);
1098
1099 if (wrap_loc == indent_len)
1100 return FALSE;
1101 }
1102#endif
1103
1104 /* Step 2, making the new wrap line. It will consist of indentation
1105 * followed by the text after the wrap point, optionally followed by
1106 * a space (if the text after the wrap point doesn't end in a blank)
David Lawrence Ramsey03979d72006-06-11 19:15:59 +00001107 * and the text of the next line, if they can fit without wrapping,
1108 * the next line exists, and the prepend_wrap flag is set. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001109
1110 /* after_break is the text that will be wrapped to the next line. */
1111 after_break = line->data + wrap_loc;
1112 after_break_len = line_len - wrap_loc;
1113
1114 assert(strlen(after_break) == after_break_len);
1115
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001116 /* We prepend the wrapped text to the next line, if the prepend_wrap
1117 * flag is set, there is a next line, and prepending would not make
1118 * the line too long. */
1119 if (prepend_wrap && line != openfile->filebot) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001120 const char *end = after_break + move_mbleft(after_break,
1121 after_break_len);
1122
1123 /* If after_break doesn't end in a blank, make sure it ends in a
1124 * space. */
1125 if (!is_blank_mbchar(end)) {
1126 line_len++;
1127 line->data = charealloc(line->data, line_len + 1);
1128 line->data[line_len - 1] = ' ';
1129 line->data[line_len] = '\0';
1130 after_break = line->data + wrap_loc;
1131 after_break_len++;
1132 openfile->totsize++;
1133 }
1134
1135 next_line = line->next->data;
1136 next_line_len = strlen(next_line);
1137
1138 if (after_break_len + next_line_len <= fill) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001139 prepending = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001140 new_line_len += next_line_len;
1141 }
1142 }
1143
1144 /* new_line_len is now the length of the text that will be wrapped
1145 * to the next line, plus (if we're prepending to it) the length of
1146 * the text of the next line. */
1147 new_line_len += after_break_len;
1148
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001149#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001150 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001151 if (prepending) {
1152 /* If we're prepending, the indentation will come from the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001153 * next line. */
1154 indent_string = next_line;
1155 indent_len = indent_length(indent_string);
1156 next_line += indent_len;
1157 } else {
1158 /* Otherwise, it will come from this line, in which case
1159 * we should increase new_line_len to make room for it. */
1160 new_line_len += indent_len;
1161 openfile->totsize += mbstrnlen(indent_string, indent_len);
1162 }
1163 }
1164#endif
1165
1166 /* Now we allocate the new line and copy the text into it. */
1167 new_line = charalloc(new_line_len + 1);
1168 new_line[0] = '\0';
1169
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001170#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001171 if (ISSET(AUTOINDENT)) {
1172 /* Copy the indentation. */
1173 strncpy(new_line, indent_string, indent_len);
1174 new_line[indent_len] = '\0';
1175 new_line_len += indent_len;
1176 }
1177#endif
1178
1179 /* Copy all the text after the wrap point of the current line. */
1180 strcat(new_line, after_break);
1181
1182 /* Break the current line at the wrap point. */
1183 null_at(&line->data, wrap_loc);
1184
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001185 if (prepending) {
1186 /* If we're prepending, copy the text from the next line, minus
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001187 * the indentation that we already copied above. */
1188 strcat(new_line, next_line);
1189
1190 free(line->next->data);
1191 line->next->data = new_line;
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001192
1193 /* If the NO_NEWLINES flag isn't set, and text has been added to
1194 * the magicline, make a new magicline. */
1195 if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
1196 new_magicline();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001197 } else {
1198 /* Otherwise, make a new line and copy the text after where we
1199 * broke this line to the beginning of the new line. */
1200 splice_node(openfile->current, make_new_node(openfile->current),
1201 openfile->current->next);
1202
David Lawrence Ramsey219a8142005-11-22 22:08:01 +00001203 /* If the current line is the last line of the file, move the
1204 * last line of the file down to the next line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001205 if (openfile->filebot == openfile->current)
1206 openfile->filebot = openfile->current->next;
1207
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001208 openfile->current->next->data = new_line;
1209
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001210 openfile->totsize++;
1211 }
1212
1213 /* Step 3, clean up. Reposition the cursor and mark, and do some
1214 * other sundry things. */
1215
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001216 /* Set the prepend_wrap flag, so that later wraps of this line will
1217 * be prepended to the next line. */
1218 prepend_wrap = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001219
David Lawrence Ramsey2829aae2006-05-22 01:24:09 +00001220 /* Each line knows its number. We recalculate these if we inserted
1221 * a new line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001222 if (!prepending)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001223 renumber(line);
1224
1225 /* If the cursor was after the break point, we must move it. We
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001226 * also clear the prepend_wrap flag in this case. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001227 if (openfile->current_x > wrap_loc) {
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +00001228 prepend_wrap = FALSE;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001229
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001230 openfile->current = openfile->current->next;
1231 openfile->current_x -= wrap_loc
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001232#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001233 - indent_len
1234#endif
1235 ;
1236 openfile->placewewant = xplustabs();
1237 }
1238
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001239#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001240 /* If the mark was on this line after the wrap point, we move it
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001241 * down. If it was on the next line and we prepended to that line,
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001242 * we move it right. */
1243 if (openfile->mark_set) {
1244 if (openfile->mark_begin == line && openfile->mark_begin_x >
1245 wrap_loc) {
1246 openfile->mark_begin = line->next;
1247 openfile->mark_begin_x -= wrap_loc - indent_len + 1;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +00001248 } else if (prepending && openfile->mark_begin == line->next)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001249 openfile->mark_begin_x += after_break_len;
1250 }
1251#endif
1252
1253 return TRUE;
1254}
1255#endif /* !DISABLE_WRAPPING */
1256
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +00001257#if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001258/* We are trying to break a chunk off line. We find the last blank such
David Lawrence Ramseycd9a5f02005-09-20 06:12:54 +00001259 * that the display length to there is at most (goal + 1). If there is
1260 * no such blank, then we find the first blank. We then take the last
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001261 * blank in that group of blanks. The terminating '\0' counts as a
1262 * blank, as does a '\n' if newline is TRUE. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001263ssize_t break_line(const char *line, ssize_t goal
1264#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001265 , bool newln
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001266#endif
1267 )
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001268{
1269 ssize_t blank_loc = -1;
1270 /* Current tentative return value. Index of the last blank we
1271 * found with short enough display width. */
1272 ssize_t cur_loc = 0;
1273 /* Current index in line. */
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001274 size_t cur_pos = 0;
1275 /* Current column position in line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001276 int line_len;
1277
1278 assert(line != NULL);
1279
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001280 while (*line != '\0' && goal >= cur_pos) {
1281 line_len = parse_mbchar(line, NULL, &cur_pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001282
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001283 if (is_blank_mbchar(line)
1284#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001285 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001286#endif
1287 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001288 blank_loc = cur_loc;
1289
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001290#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001291 if (newln && *line == '\n')
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001292 break;
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001293#endif
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001294 }
1295
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001296 line += line_len;
1297 cur_loc += line_len;
1298 }
1299
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +00001300 if (goal >= cur_pos)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001301 /* In fact, the whole line displays shorter than goal. */
1302 return cur_loc;
1303
Chris Allegretta09b81242008-07-12 01:54:49 +00001304#ifndef DISABLE_HELP
1305 if (newln && blank_loc <= 0) {
1306 /* If blank was not found or was found only first character,
1307 * force line break. */
1308 cur_loc -= line_len;
1309 return cur_loc;
1310 }
1311#endif
1312
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001313 if (blank_loc == -1) {
1314 /* No blank was found that was short enough. */
1315 bool found_blank = FALSE;
David Lawrence Ramsey5ab12ca2005-09-20 17:52:52 +00001316 ssize_t found_blank_loc = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001317
1318 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001319 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001320
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001321 if (is_blank_mbchar(line)
1322#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001323 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001324#endif
1325 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001326 if (!found_blank)
1327 found_blank = TRUE;
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +00001328 found_blank_loc = cur_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001329 } else if (found_blank)
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +00001330 return found_blank_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001331
1332 line += line_len;
1333 cur_loc += line_len;
1334 }
1335
1336 return -1;
1337 }
1338
1339 /* Move to the last blank after blank_loc, if there is one. */
1340 line -= cur_loc;
1341 line += blank_loc;
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001342 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001343 line += line_len;
1344
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001345 while (*line != '\0' && (is_blank_mbchar(line)
1346#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001347 || (newln && *line == '\n')
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001348#endif
1349 )) {
David Lawrence Ramseycd243f52006-05-19 23:27:16 +00001350#ifndef DISABLE_HELP
Chris Allegretta8b6461f2008-05-31 23:09:40 +00001351 if (newln && *line == '\n')
David Lawrence Ramseycd243f52006-05-19 23:27:16 +00001352 break;
1353#endif
1354
David Lawrence Ramsey39bd1b32006-05-20 13:11:56 +00001355 line_len = parse_mbchar(line, NULL, NULL);
1356
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001357 line += line_len;
1358 blank_loc += line_len;
1359 }
1360
1361 return blank_loc;
1362}
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +00001363#endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001364
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001365#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001366/* The "indentation" of a line is the whitespace between the quote part
1367 * and the non-whitespace of the line. */
1368size_t indent_length(const char *line)
1369{
1370 size_t len = 0;
1371 char *blank_mb;
1372 int blank_mb_len;
1373
1374 assert(line != NULL);
1375
1376 blank_mb = charalloc(mb_cur_max());
1377
1378 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001379 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001380
1381 if (!is_blank_mbchar(blank_mb))
1382 break;
1383
1384 line += blank_mb_len;
1385 len += blank_mb_len;
1386 }
1387
1388 free(blank_mb);
1389
1390 return len;
1391}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001392#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001393
1394#ifndef DISABLE_JUSTIFY
1395/* justify_format() replaces blanks with spaces and multiple spaces by 1
1396 * (except it maintains up to 2 after a character in punct optionally
1397 * followed by a character in brackets, and removes all from the end).
1398 *
1399 * justify_format() might make paragraph->data shorter, and change the
1400 * actual pointer with null_at().
1401 *
1402 * justify_format() will not look at the first skip characters of
1403 * paragraph. skip should be at most strlen(paragraph->data). The
1404 * character at paragraph[skip + 1] must not be blank. */
1405void justify_format(filestruct *paragraph, size_t skip)
1406{
1407 char *end, *new_end, *new_paragraph_data;
1408 size_t shift = 0;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001409#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001410 size_t mark_shift = 0;
1411#endif
1412
1413 /* These four asserts are assumptions about the input data. */
1414 assert(paragraph != NULL);
1415 assert(paragraph->data != NULL);
1416 assert(skip < strlen(paragraph->data));
1417 assert(!is_blank_mbchar(paragraph->data + skip));
1418
1419 end = paragraph->data + skip;
1420 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
1421 strncpy(new_paragraph_data, paragraph->data, skip);
1422 new_end = new_paragraph_data + skip;
1423
1424 while (*end != '\0') {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001425 int end_len;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001426
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001427 /* If this character is blank, change it to a space if
1428 * necessary, and skip over all blanks after it. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001429 if (is_blank_mbchar(end)) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001430 end_len = parse_mbchar(end, NULL, NULL);
1431
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001432 *new_end = ' ';
1433 new_end++;
1434 end += end_len;
1435
1436 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001437 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001438
1439 end += end_len;
1440 shift += end_len;
1441
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001442#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001443 /* Keep track of the change in the current line. */
1444 if (openfile->mark_set && openfile->mark_begin ==
1445 paragraph && openfile->mark_begin_x >= end -
1446 paragraph->data)
1447 mark_shift += end_len;
1448#endif
1449 }
1450 /* If this character is punctuation optionally followed by a
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001451 * bracket and then followed by blanks, change no more than two
1452 * of the blanks to spaces if necessary, and skip over all
1453 * blanks after them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001454 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001455 end_len = parse_mbchar(end, NULL, NULL);
1456
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001457 while (end_len > 0) {
1458 *new_end = *end;
1459 new_end++;
1460 end++;
1461 end_len--;
1462 }
1463
1464 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001465 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001466
1467 while (end_len > 0) {
1468 *new_end = *end;
1469 new_end++;
1470 end++;
1471 end_len--;
1472 }
1473 }
1474
1475 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001476 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001477
1478 *new_end = ' ';
1479 new_end++;
1480 end += end_len;
1481 }
1482
1483 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001484 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001485
1486 *new_end = ' ';
1487 new_end++;
1488 end += end_len;
1489 }
1490
1491 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +00001492 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001493
1494 end += end_len;
1495 shift += end_len;
1496
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001497#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001498 /* Keep track of the change in the current line. */
1499 if (openfile->mark_set && openfile->mark_begin ==
1500 paragraph && openfile->mark_begin_x >= end -
1501 paragraph->data)
1502 mark_shift += end_len;
1503#endif
1504 }
1505 /* If this character is neither blank nor punctuation, leave it
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001506 * unchanged. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001507 } else {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001508 end_len = parse_mbchar(end, NULL, NULL);
1509
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001510 while (end_len > 0) {
1511 *new_end = *end;
1512 new_end++;
1513 end++;
1514 end_len--;
1515 }
1516 }
1517 }
1518
1519 assert(*end == '\0');
1520
1521 *new_end = *end;
1522
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001523 /* If there are spaces at the end of the line, remove them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001524 while (new_end > new_paragraph_data + skip &&
1525 *(new_end - 1) == ' ') {
1526 new_end--;
1527 shift++;
1528 }
1529
1530 if (shift > 0) {
1531 openfile->totsize -= shift;
1532 null_at(&new_paragraph_data, new_end - new_paragraph_data);
1533 free(paragraph->data);
1534 paragraph->data = new_paragraph_data;
1535
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001536#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001537 /* Adjust the mark coordinates to compensate for the change in
1538 * the current line. */
1539 if (openfile->mark_set && openfile->mark_begin == paragraph) {
1540 openfile->mark_begin_x -= mark_shift;
1541 if (openfile->mark_begin_x > new_end - new_paragraph_data)
1542 openfile->mark_begin_x = new_end - new_paragraph_data;
1543 }
1544#endif
1545 } else
1546 free(new_paragraph_data);
1547}
1548
1549/* The "quote part" of a line is the largest initial substring matching
1550 * the quote string. This function returns the length of the quote part
1551 * of the given line.
1552 *
1553 * Note that if !HAVE_REGEX_H then we match concatenated copies of
1554 * quotestr. */
1555size_t quote_length(const char *line)
1556{
1557#ifdef HAVE_REGEX_H
1558 regmatch_t matches;
1559 int rc = regexec(&quotereg, line, 1, &matches, 0);
1560
1561 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
1562 return 0;
1563 /* matches.rm_so should be 0, since the quote string should start
1564 * with the caret ^. */
1565 return matches.rm_eo;
1566#else /* !HAVE_REGEX_H */
1567 size_t qdepth = 0;
1568
1569 /* Compute quote depth level. */
1570 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
1571 qdepth += quotelen;
1572 return qdepth;
1573#endif /* !HAVE_REGEX_H */
1574}
1575
1576/* a_line and b_line are lines of text. The quotation part of a_line is
1577 * the first a_quote characters. Check that the quotation part of
1578 * b_line is the same. */
1579bool quotes_match(const char *a_line, size_t a_quote, const char
1580 *b_line)
1581{
1582 /* Here is the assumption about a_quote. */
1583 assert(a_quote == quote_length(a_line));
1584
1585 return (a_quote == quote_length(b_line) &&
1586 strncmp(a_line, b_line, a_quote) == 0);
1587}
1588
1589/* We assume a_line and b_line have no quote part. Then, we return
1590 * whether b_line could follow a_line in a paragraph. */
1591bool indents_match(const char *a_line, size_t a_indent, const char
1592 *b_line, size_t b_indent)
1593{
1594 assert(a_indent == indent_length(a_line));
1595 assert(b_indent == indent_length(b_line));
1596
1597 return (b_indent <= a_indent &&
1598 strncmp(a_line, b_line, b_indent) == 0);
1599}
1600
1601/* Is foo the beginning of a paragraph?
1602 *
1603 * A line of text consists of a "quote part", followed by an
1604 * "indentation part", followed by text. The functions quote_length()
1605 * and indent_length() calculate these parts.
1606 *
1607 * A line is "part of a paragraph" if it has a part not in the quote
1608 * part or the indentation.
1609 *
1610 * A line is "the beginning of a paragraph" if it is part of a
1611 * paragraph and
1612 * 1) it is the top line of the file, or
1613 * 2) the line above it is not part of a paragraph, or
1614 * 3) the line above it does not have precisely the same quote
1615 * part, or
1616 * 4) the indentation of this line is not an initial substring of
1617 * the indentation of the previous line, or
1618 * 5) this line has no quote part and some indentation, and
1619 * autoindent isn't turned on.
1620 * The reason for number 5) is that if autoindent isn't turned on,
1621 * then an indented line is expected to start a paragraph, as in
1622 * books. Thus, nano can justify an indented paragraph only if
1623 * autoindent is turned on. */
1624bool begpar(const filestruct *const foo)
1625{
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001626 size_t quote_len, indent_len, temp_id_len;
1627
1628 if (foo == NULL)
1629 return FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001630
1631 /* Case 1). */
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001632 if (foo == openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001633 return TRUE;
1634
1635 quote_len = quote_length(foo->data);
1636 indent_len = indent_length(foo->data + quote_len);
1637
1638 /* Not part of a paragraph. */
1639 if (foo->data[quote_len + indent_len] == '\0')
1640 return FALSE;
1641
1642 /* Case 3). */
1643 if (!quotes_match(foo->data, quote_len, foo->prev->data))
1644 return TRUE;
1645
1646 temp_id_len = indent_length(foo->prev->data + quote_len);
1647
1648 /* Case 2) or 5) or 4). */
1649 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
1650 (quote_len == 0 && indent_len > 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001651#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001652 && !ISSET(AUTOINDENT)
1653#endif
1654 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
1655 foo->data + quote_len, indent_len))
1656 return TRUE;
1657
1658 return FALSE;
1659}
1660
1661/* Is foo inside a paragraph? */
1662bool inpar(const filestruct *const foo)
1663{
1664 size_t quote_len;
1665
1666 if (foo == NULL)
1667 return FALSE;
1668
1669 quote_len = quote_length(foo->data);
1670
David Lawrence Ramsey21014032005-11-09 20:33:42 +00001671 return (foo->data[quote_len + indent_length(foo->data +
1672 quote_len)] != '\0');
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001673}
1674
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001675/* Move the next par_len lines, starting with first_line, into the
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001676 * justify buffer, leaving copies of those lines in place. Assume that
1677 * par_len is greater than zero, and that there are enough lines after
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001678 * first_line. */
1679void backup_lines(filestruct *first_line, size_t par_len)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001680{
1681 filestruct *top = first_line;
1682 /* The top of the paragraph we're backing up. */
1683 filestruct *bot = first_line;
1684 /* The bottom of the paragraph we're backing up. */
1685 size_t i;
1686 /* Generic loop variable. */
1687 size_t current_x_save = openfile->current_x;
1688 ssize_t fl_lineno_save = first_line->lineno;
1689 ssize_t edittop_lineno_save = openfile->edittop->lineno;
1690 ssize_t current_lineno_save = openfile->current->lineno;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001691#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001692 bool old_mark_set = openfile->mark_set;
1693 ssize_t mb_lineno_save = 0;
1694 size_t mark_begin_x_save = 0;
1695
1696 if (old_mark_set) {
1697 mb_lineno_save = openfile->mark_begin->lineno;
1698 mark_begin_x_save = openfile->mark_begin_x;
1699 }
1700#endif
1701
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001702 /* par_len will be one greater than the number of lines between
1703 * current and filebot if filebot is the last line in the
1704 * paragraph. */
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001705 assert(par_len > 0 && openfile->current->lineno + par_len <=
David Lawrence Ramsey24777c02005-12-01 05:49:08 +00001706 openfile->filebot->lineno + 1);
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001707
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001708 /* Move bot down par_len lines to the line after the last line of
1709 * the paragraph, if there is one. */
1710 for (i = par_len; i > 0 && bot != openfile->filebot; i--)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001711 bot = bot->next;
1712
1713 /* Move the paragraph from the current buffer's filestruct to the
1714 * justify buffer. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001715 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
David Lawrence Ramseyf0575cf2005-11-09 23:27:51 +00001716 (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001717
1718 /* Copy the paragraph back to the current buffer's filestruct from
1719 * the justify buffer. */
1720 copy_from_filestruct(jusbuffer, jusbottom);
1721
1722 /* Move upward from the last line of the paragraph to the first
1723 * line, putting first_line, edittop, current, and mark_begin at the
1724 * same lines in the copied paragraph that they had in the original
1725 * paragraph. */
David Lawrence Ramseyee43ea62007-04-22 15:04:05 +00001726 if (openfile->current != openfile->fileage) {
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001727 top = openfile->current->prev;
David Lawrence Ramseyee43ea62007-04-22 15:04:05 +00001728#ifndef NANO_TINY
1729 if (old_mark_set &&
1730 openfile->current->lineno == mb_lineno_save) {
1731 openfile->mark_begin = openfile->current;
1732 openfile->mark_begin_x = mark_begin_x_save;
1733 }
1734#endif
1735 } else
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001736 top = openfile->current;
David Lawrence Ramseye8d505b2005-11-10 03:40:45 +00001737 for (i = par_len; i > 0 && top != NULL; i--) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001738 if (top->lineno == fl_lineno_save)
1739 first_line = top;
1740 if (top->lineno == edittop_lineno_save)
1741 openfile->edittop = top;
1742 if (top->lineno == current_lineno_save)
1743 openfile->current = top;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001744#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001745 if (old_mark_set && top->lineno == mb_lineno_save) {
1746 openfile->mark_begin = top;
1747 openfile->mark_begin_x = mark_begin_x_save;
1748 }
1749#endif
1750 top = top->prev;
1751 }
1752
1753 /* Put current_x at the same place in the copied paragraph that it
1754 * had in the original paragraph. */
1755 openfile->current_x = current_x_save;
1756
1757 set_modified();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001758}
1759
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001760/* Find the beginning of the current paragraph if we're in one, or the
1761 * beginning of the next paragraph if we're not. Afterwards, save the
1762 * quote length and paragraph length in *quote and *par. Return TRUE if
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001763 * we found a paragraph, and FALSE if there was an error or we didn't
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001764 * find a paragraph.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001765 *
1766 * See the comment at begpar() for more about when a line is the
1767 * beginning of a paragraph. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001768bool find_paragraph(size_t *const quote, size_t *const par)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001769{
1770 size_t quote_len;
1771 /* Length of the initial quotation of the paragraph we search
1772 * for. */
1773 size_t par_len;
1774 /* Number of lines in the paragraph we search for. */
1775 filestruct *current_save;
1776 /* The line at the beginning of the paragraph we search for. */
1777 ssize_t current_y_save;
1778 /* The y-coordinate at the beginning of the paragraph we search
1779 * for. */
1780
1781#ifdef HAVE_REGEX_H
1782 if (quoterc != 0) {
1783 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1784 return FALSE;
1785 }
1786#endif
1787
1788 assert(openfile->current != NULL);
1789
David Lawrence Ramsey1be131a2005-11-11 03:55:52 +00001790 /* If we're at the end of the last line of the file, it means that
1791 * there aren't any paragraphs left, so get out. */
1792 if (openfile->current == openfile->filebot && openfile->current_x ==
1793 strlen(openfile->filebot->data))
1794 return FALSE;
1795
1796 /* If the current line isn't in a paragraph, move forward to the
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001797 * last line of the next paragraph, if any. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001798 if (!inpar(openfile->current)) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001799 do_para_end(FALSE);
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001800
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001801 /* If we end up past the beginning of the line, it means that
1802 * we're at the end of the last line of the file, and the line
1803 * isn't blank, in which case the last line of the file is the
1804 * last line of the next paragraph.
1805 *
1806 * Otherwise, if we end up on a line that's in a paragraph, it
1807 * means that we're on the line after the last line of the next
1808 * paragraph, in which case we should move back to the last line
1809 * of the next paragraph. */
1810 if (openfile->current_x == 0) {
1811 if (!inpar(openfile->current->prev))
1812 return FALSE;
1813 if (openfile->current != openfile->fileage)
1814 openfile->current = openfile->current->prev;
1815 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001816 }
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001817
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001818 /* If the current line isn't the first line of the paragraph, move
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001819 * back to the first line of the paragraph. */
1820 if (!begpar(openfile->current))
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001821 do_para_begin(FALSE);
1822
1823 /* Now current is the first line of the paragraph. Set quote_len to
1824 * the quotation length of that line, and set par_len to the number
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001825 * of lines in this paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001826 quote_len = quote_length(openfile->current->data);
1827 current_save = openfile->current;
1828 current_y_save = openfile->current_y;
1829 do_para_end(FALSE);
1830 par_len = openfile->current->lineno - current_save->lineno;
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001831
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001832 /* If we end up past the beginning of the line, it means that we're
1833 * at the end of the last line of the file, and the line isn't
1834 * blank, in which case the last line of the file is part of the
1835 * paragraph. */
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001836 if (openfile->current_x > 0)
1837 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001838 openfile->current = current_save;
1839 openfile->current_y = current_y_save;
1840
1841 /* Save the values of quote_len and par_len. */
1842 assert(quote != NULL && par != NULL);
1843
1844 *quote = quote_len;
1845 *par = par_len;
1846
1847 return TRUE;
1848}
1849
1850/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1851 * the current paragraph. */
1852void do_justify(bool full_justify)
1853{
1854 filestruct *first_par_line = NULL;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001855 /* Will be the first line of the justified paragraph(s), if any.
1856 * For restoring after unjustify. */
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001857 filestruct *last_par_line = NULL;
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001858 /* Will be the line after the last line of the justified
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001859 * paragraph(s), if any. Also for restoring after unjustify. */
David Lawrence Ramsey82b5deb2005-11-10 06:07:57 +00001860 bool filebot_inpar = FALSE;
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001861 /* Whether the text at filebot is part of the current
1862 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001863
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001864 /* We save these variables to be restored if the user
1865 * unjustifies. */
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001866 filestruct *edittop_save = openfile->edittop;
1867 filestruct *current_save = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001868 size_t current_x_save = openfile->current_x;
1869 size_t pww_save = openfile->placewewant;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001870 size_t totsize_save = openfile->totsize;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001871#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001872 filestruct *mark_begin_save = openfile->mark_begin;
1873 size_t mark_begin_x_save = openfile->mark_begin_x;
1874#endif
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001875 bool modified_save = openfile->modified;
1876
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001877 int kbinput;
1878 bool meta_key, func_key, s_or_t, ran_func, finished;
Chris Allegretta0018d8e2008-03-13 08:23:52 +00001879 const sc *s;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001880
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001881 /* Move to the beginning of the current line, so that justifying at
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001882 * the end of the last line of the file, if that line isn't blank,
1883 * will work the first time through. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001884 openfile->current_x = 0;
1885
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001886 /* If we're justifying the entire file, start at the beginning. */
1887 if (full_justify)
1888 openfile->current = openfile->fileage;
1889
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001890 while (TRUE) {
1891 size_t i;
1892 /* Generic loop variable. */
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001893 filestruct *curr_first_par_line;
1894 /* The first line of the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001895 size_t quote_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001896 /* Length of the initial quotation of the current
1897 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001898 size_t indent_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001899 /* Length of the initial indentation of the current
1900 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001901 size_t par_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001902 /* Number of lines in the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001903 ssize_t break_pos;
1904 /* Where we will break lines. */
1905 char *indent_string;
1906 /* The first indentation that doesn't match the initial
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001907 * indentation of the current paragraph. This is put at the
1908 * beginning of every line broken off the first justified
1909 * line of the paragraph. Note that this works because a
1910 * paragraph can only contain two indentations at most: the
1911 * initial one, and a different one starting on a line after
1912 * the first. See the comment at begpar() for more about
1913 * when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001914
1915 /* Find the first line of the paragraph to be justified. That
1916 * is the start of this paragraph if we're in one, or the start
1917 * of the next otherwise. Save the quote length and paragraph
1918 * length (number of lines). Don't refresh the screen yet,
1919 * since we'll do that after we justify.
1920 *
1921 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001922 * justifying the whole file, and we've found at least one
1923 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001924 * last line of the file, so set the last line of the text to be
1925 * justified to the last line of the file and break out of the
1926 * loop. Otherwise, it means that there are no paragraph(s) to
1927 * justify, so refresh the screen and get out. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001928 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001929 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001930 last_par_line = openfile->filebot;
1931 break;
1932 } else {
1933 edit_refresh();
1934 return;
1935 }
1936 }
1937
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001938 /* par_len will be one greater than the number of lines between
1939 * current and filebot if filebot is the last line in the
1940 * paragraph. Set filebot_inpar to TRUE if this is the case. */
1941 filebot_inpar = (openfile->current->lineno + par_len ==
1942 openfile->filebot->lineno + 1);
1943
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001944 /* If we haven't already done it, move the original paragraph(s)
1945 * to the justify buffer, splice a copy of the original
1946 * paragraph(s) into the file in the same place, and set
1947 * first_par_line to the first line of the copy. */
1948 if (first_par_line == NULL) {
1949 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00001950 openfile->filebot->lineno - openfile->current->lineno +
1951 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001952 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001953 first_par_line = openfile->current;
1954 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001955
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001956 /* Set curr_first_par_line to the first line of the current
1957 * paragraph. */
1958 curr_first_par_line = openfile->current;
1959
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001960 /* Initialize indent_string to a blank string. */
1961 indent_string = mallocstrcpy(NULL, "");
1962
1963 /* Find the first indentation in the paragraph that doesn't
David Lawrence Ramsey8602fd62006-05-28 18:43:21 +00001964 * match the indentation of the first line, and save it in
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001965 * indent_string. If all the indentations are the same, save
1966 * the indentation of the first line in indent_string. */
1967 {
1968 const filestruct *indent_line = openfile->current;
1969 bool past_first_line = FALSE;
1970
1971 for (i = 0; i < par_len; i++) {
1972 indent_len = quote_len +
1973 indent_length(indent_line->data + quote_len);
1974
1975 if (indent_len != strlen(indent_string)) {
1976 indent_string = mallocstrncpy(indent_string,
1977 indent_line->data, indent_len + 1);
1978 indent_string[indent_len] = '\0';
1979
1980 if (past_first_line)
1981 break;
1982 }
1983
1984 if (indent_line == openfile->current)
1985 past_first_line = TRUE;
1986
1987 indent_line = indent_line->next;
1988 }
1989 }
1990
1991 /* Now tack all the lines of the paragraph together, skipping
1992 * the quoting and indentation on all lines after the first. */
1993 for (i = 0; i < par_len - 1; i++) {
1994 filestruct *next_line = openfile->current->next;
1995 size_t line_len = strlen(openfile->current->data);
1996 size_t next_line_len =
1997 strlen(openfile->current->next->data);
1998
1999 indent_len = quote_len +
2000 indent_length(openfile->current->next->data +
2001 quote_len);
2002
2003 next_line_len -= indent_len;
2004 openfile->totsize -= indent_len;
2005
2006 /* We're just about to tack the next line onto this one. If
2007 * this line isn't empty, make sure it ends in a space. */
2008 if (line_len > 0 &&
2009 openfile->current->data[line_len - 1] != ' ') {
2010 line_len++;
2011 openfile->current->data =
2012 charealloc(openfile->current->data,
2013 line_len + 1);
2014 openfile->current->data[line_len - 1] = ' ';
2015 openfile->current->data[line_len] = '\0';
2016 openfile->totsize++;
2017 }
2018
2019 openfile->current->data =
2020 charealloc(openfile->current->data, line_len +
2021 next_line_len + 1);
2022 strcat(openfile->current->data, next_line->data +
2023 indent_len);
2024
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002025 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00002026 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002027 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00002028 if (next_line == openfile->filebot)
2029 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002030
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002031#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002032 /* Adjust the mark coordinates to compensate for the change
2033 * in the next line. */
2034 if (openfile->mark_set && openfile->mark_begin ==
2035 next_line) {
2036 openfile->mark_begin = openfile->current;
2037 openfile->mark_begin_x += line_len - indent_len;
2038 }
2039#endif
2040
2041 unlink_node(next_line);
2042 delete_node(next_line);
2043
2044 /* If we've removed the next line, we need to go through
2045 * this line again. */
2046 i--;
2047
2048 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002049 openfile->totsize--;
2050 }
2051
2052 /* Call justify_format() on the paragraph, which will remove
2053 * excess spaces from it and change all blank characters to
2054 * spaces. */
2055 justify_format(openfile->current, quote_len +
2056 indent_length(openfile->current->data + quote_len));
2057
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002058 while (par_len > 0 && strlenpt(openfile->current->data) >
2059 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002060 size_t line_len = strlen(openfile->current->data);
2061
2062 indent_len = strlen(indent_string);
2063
2064 /* If this line is too long, try to wrap it to the next line
2065 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00002066 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00002067 fill - strnlenpt(openfile->current->data, indent_len)
2068#ifndef DISABLE_HELP
2069 , FALSE
2070#endif
2071 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002072
2073 /* We can't break the line, or don't need to, so get out. */
2074 if (break_pos == -1 || break_pos + indent_len == line_len)
2075 break;
2076
2077 /* Move forward to the character after the indentation and
2078 * just after the space. */
2079 break_pos += indent_len + 1;
2080
2081 assert(break_pos <= line_len);
2082
2083 /* Make a new line, and copy the text after where we're
2084 * going to break this line to the beginning of the new
2085 * line. */
2086 splice_node(openfile->current,
2087 make_new_node(openfile->current),
2088 openfile->current->next);
2089
2090 /* If this paragraph is non-quoted, and autoindent isn't
2091 * turned on, set the indentation length to zero so that the
2092 * indentation is treated as part of the line. */
2093 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002094#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002095 && !ISSET(AUTOINDENT)
2096#endif
2097 )
2098 indent_len = 0;
2099
2100 /* Copy the text after where we're going to break the
2101 * current line to the next line. */
2102 openfile->current->next->data = charalloc(indent_len + 1 +
2103 line_len - break_pos);
2104 strncpy(openfile->current->next->data, indent_string,
2105 indent_len);
2106 strcpy(openfile->current->next->data + indent_len,
2107 openfile->current->data + break_pos);
2108
2109 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002110 openfile->totsize += indent_len + 1;
2111
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002112#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002113 /* Adjust the mark coordinates to compensate for the change
2114 * in the current line. */
2115 if (openfile->mark_set && openfile->mark_begin ==
2116 openfile->current && openfile->mark_begin_x >
2117 break_pos) {
2118 openfile->mark_begin = openfile->current->next;
2119 openfile->mark_begin_x -= break_pos - indent_len;
2120 }
2121#endif
2122
2123 /* Break the current line. */
2124 null_at(&openfile->current->data, break_pos);
2125
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002126 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00002127 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002128 if (openfile->filebot == openfile->current)
2129 openfile->filebot = openfile->filebot->next;
2130
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002131 /* Go to the next line. */
2132 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002133 openfile->current_y++;
2134 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002135 }
2136
2137 /* We're done breaking lines, so we don't need indent_string
2138 * anymore. */
2139 free(indent_string);
2140
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00002141 /* Go to the next line, if possible. If there is no next line,
2142 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00002143 if (openfile->current != openfile->filebot) {
2144 openfile->current_y++;
2145 openfile->current = openfile->current->next;
2146 } else
2147 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002148
David Lawrence Ramseya6854682005-11-30 21:19:42 +00002149 /* Renumber the lines of the now-justified current paragraph,
2150 * since both find_paragraph() and edit_refresh() need the line
2151 * numbers to be right. */
2152 renumber(curr_first_par_line);
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002153
2154 /* We've just finished justifying the paragraph. If we're not
2155 * justifying the entire file, break out of the loop.
2156 * Otherwise, continue the loop so that we justify all the
2157 * paragraphs in the file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002158 if (!full_justify)
2159 break;
2160 }
2161
2162 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002163 * up. current_y and totsize have been maintained above. If we
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00002164 * actually justified something, set last_par_line to the new end of
2165 * the paragraph. */
2166 if (first_par_line != NULL)
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002167 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002168
2169 edit_refresh();
2170
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002171#ifndef NANO_TINY
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002172 /* We're going to set jump_buf so that we return here after a
2173 * SIGWINCH instead of to main(). Indicate this. */
2174 jump_buf_main = FALSE;
2175
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002176 /* Return here after a SIGWINCH. */
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00002177 sigsetjmp(jump_buf, 1);
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00002178#endif
2179
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002180 statusbar(_("Can now UnJustify!"));
2181
2182 /* If constant cursor position display is on, make sure the current
2183 * cursor position will be properly displayed on the statusbar. */
2184 if (ISSET(CONST_UPDATE))
2185 do_cursorpos(TRUE);
2186
2187 /* Display the shortcut list with UnJustify. */
2188 shortcut_init(TRUE);
2189 display_main_list();
2190
2191 /* Now get a keystroke and see if it's unjustify. If not, put back
2192 * the keystroke and return. */
2193 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
2194 &finished, FALSE);
Chris Allegretta0018d8e2008-03-13 08:23:52 +00002195 s = get_shortcut(currmenu, &kbinput, &meta_key, &func_key);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002196
Chris Allegretta0018d8e2008-03-13 08:23:52 +00002197 if (s && s->scfunc == do_uncut_text) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002198 /* Splice the justify buffer back into the file, but only if we
2199 * actually justified something. */
2200 if (first_par_line != NULL) {
2201 filestruct *top_save;
2202
2203 /* Partition the filestruct so that it contains only the
2204 * text of the justified paragraph. */
2205 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00002206 last_par_line, filebot_inpar ?
2207 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002208
2209 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00002210 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002211 free_filestruct(openfile->fileage);
2212 openfile->fileage = jusbuffer;
2213 openfile->filebot = jusbottom;
2214
2215 top_save = openfile->fileage;
2216
2217 /* Unpartition the filestruct so that it contains all the
2218 * text again. Note that the justified paragraph has been
2219 * replaced with the unjustified paragraph. */
2220 unpartition_filestruct(&filepart);
2221
2222 /* Renumber starting with the beginning line of the old
2223 * partition. */
2224 renumber(top_save);
2225
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00002226 /* Restore the justify we just did (ungrateful user!). */
2227 openfile->edittop = edittop_save;
2228 openfile->current = current_save;
2229 openfile->current_x = current_x_save;
2230 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002231 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002232#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002233 if (openfile->mark_set) {
2234 openfile->mark_begin = mark_begin_save;
2235 openfile->mark_begin_x = mark_begin_x_save;
2236 }
2237#endif
2238 openfile->modified = modified_save;
2239
2240 /* Clear the justify buffer. */
2241 jusbuffer = NULL;
2242
2243 if (!openfile->modified)
2244 titlebar(NULL);
2245 edit_refresh();
2246 }
2247 } else {
2248 unget_kbinput(kbinput, meta_key, func_key);
2249
2250 /* Blow away the text in the justify buffer. */
2251 free_filestruct(jusbuffer);
2252 jusbuffer = NULL;
2253 }
2254
2255 blank_statusbar();
2256
2257 /* Display the shortcut list with UnCut. */
2258 shortcut_init(FALSE);
2259 display_main_list();
2260}
2261
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002262/* Justify the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002263void do_justify_void(void)
2264{
2265 do_justify(FALSE);
2266}
2267
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002268/* Justify the entire file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002269void do_full_justify(void)
2270{
2271 do_justify(TRUE);
2272}
2273#endif /* !DISABLE_JUSTIFY */
2274
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002275#ifndef DISABLE_SPELLER
2276/* A word is misspelled in the file. Let the user replace it. We
2277 * return FALSE if the user cancels. */
2278bool do_int_spell_fix(const char *word)
2279{
2280 char *save_search, *save_replace;
2281 size_t match_len, current_x_save = openfile->current_x;
2282 size_t pww_save = openfile->placewewant;
Chris Allegretta10f868d2008-03-14 04:08:51 +00002283 bool meta_key = FALSE, func_key = FALSE;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002284 filestruct *edittop_save = openfile->edittop;
2285 filestruct *current_save = openfile->current;
2286 /* Save where we are. */
2287 bool canceled = FALSE;
2288 /* The return value. */
2289 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002290#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002291 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
2292#endif
2293#ifdef HAVE_REGEX_H
2294 bool regexp_set = ISSET(USE_REGEXP);
2295#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002296#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002297 bool old_mark_set = openfile->mark_set;
2298 bool added_magicline = FALSE;
2299 /* Whether we added a magicline after filebot. */
2300 bool right_side_up = FALSE;
2301 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2302 * FALSE if (current, current_x) is. */
2303 filestruct *top, *bot;
2304 size_t top_x, bot_x;
2305#endif
2306
2307 /* Make sure spell-check is case sensitive. */
2308 SET(CASE_SENSITIVE);
2309
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002310#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002311 /* Make sure spell-check goes forward only. */
2312 UNSET(BACKWARDS_SEARCH);
2313#endif
2314#ifdef HAVE_REGEX_H
2315 /* Make sure spell-check doesn't use regular expressions. */
2316 UNSET(USE_REGEXP);
2317#endif
2318
2319 /* Save the current search/replace strings. */
2320 search_init_globals();
2321 save_search = last_search;
2322 save_replace = last_replace;
2323
2324 /* Set the search/replace strings to the misspelled word. */
2325 last_search = mallocstrcpy(NULL, word);
2326 last_replace = mallocstrcpy(NULL, word);
2327
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002328#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002329 if (old_mark_set) {
2330 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002331 * contains only the marked text; if the NO_NEWLINES flag isn't
2332 * set, keep track of whether the text will have a magicline
2333 * added when we're done correcting misspelled words; and
2334 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002335 mark_order((const filestruct **)&top, &top_x,
2336 (const filestruct **)&bot, &bot_x, &right_side_up);
2337 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002338 if (!ISSET(NO_NEWLINES))
2339 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002340 openfile->mark_set = FALSE;
2341 }
2342#endif
2343
2344 /* Start from the top of the file. */
2345 openfile->edittop = openfile->fileage;
2346 openfile->current = openfile->fileage;
2347 openfile->current_x = (size_t)-1;
2348 openfile->placewewant = 0;
2349
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002350 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002351 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002352 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002353 &match_len)) {
2354 if (is_whole_word(openfile->current_x, openfile->current->data,
2355 word)) {
2356 size_t xpt = xplustabs();
2357 char *exp_word = display_string(openfile->current->data,
2358 xpt, strnlenpt(openfile->current->data,
2359 openfile->current_x + match_len) - xpt, FALSE);
2360
2361 edit_refresh();
2362
2363 do_replace_highlight(TRUE, exp_word);
2364
2365 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002366 canceled = (do_prompt(FALSE,
2367#ifndef DISABLE_TABCOMP
2368 TRUE,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002369#endif
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002370 MSPELL, word,
Chris Allegretta10f868d2008-03-14 04:08:51 +00002371 &meta_key, &func_key,
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00002372#ifndef NANO_TINY
2373 NULL,
2374#endif
David Lawrence Ramsey68160072006-02-18 21:32:29 +00002375 edit_refresh, _("Edit a replacement")) == -1);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002376
2377 do_replace_highlight(FALSE, exp_word);
2378
2379 free(exp_word);
2380
2381 if (!canceled && strcmp(word, answer) != 0) {
2382 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00002383 do_replace_loop(TRUE, &canceled, openfile->current,
2384 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002385 }
2386
2387 break;
2388 }
2389 }
2390
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002391#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002392 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002393 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2394 * added a magicline, remove it now. */
2395 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002396 remove_magicline();
2397
2398 /* Put the beginning and the end of the mark at the beginning
2399 * and the end of the spell-checked text. */
2400 if (openfile->fileage == openfile->filebot)
2401 bot_x += top_x;
2402 if (right_side_up) {
2403 openfile->mark_begin_x = top_x;
2404 current_x_save = bot_x;
2405 } else {
2406 current_x_save = top_x;
2407 openfile->mark_begin_x = bot_x;
2408 }
2409
2410 /* Unpartition the filestruct so that it contains all the text
2411 * again, and turn the mark back on. */
2412 unpartition_filestruct(&filepart);
2413 openfile->mark_set = TRUE;
2414 }
2415#endif
2416
2417 /* Restore the search/replace strings. */
2418 free(last_search);
2419 last_search = save_search;
2420 free(last_replace);
2421 last_replace = save_replace;
2422
2423 /* Restore where we were. */
2424 openfile->edittop = edittop_save;
2425 openfile->current = current_save;
2426 openfile->current_x = current_x_save;
2427 openfile->placewewant = pww_save;
2428
2429 /* Restore case sensitivity setting. */
2430 if (!case_sens_set)
2431 UNSET(CASE_SENSITIVE);
2432
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002433#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002434 /* Restore search/replace direction. */
2435 if (backwards_search_set)
2436 SET(BACKWARDS_SEARCH);
2437#endif
2438#ifdef HAVE_REGEX_H
2439 /* Restore regular expression usage setting. */
2440 if (regexp_set)
2441 SET(USE_REGEXP);
2442#endif
2443
2444 return !canceled;
2445}
2446
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002447/* Internal (integrated) spell checking using the spell program,
2448 * filtered through the sort and uniq programs. Return NULL for normal
2449 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002450const char *do_int_speller(const char *tempfile_name)
2451{
2452 char *read_buff, *read_buff_ptr, *read_buff_word;
2453 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
2454 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
2455 pid_t pid_spell, pid_sort, pid_uniq;
2456 int spell_status, sort_status, uniq_status;
2457
2458 /* Create all three pipes up front. */
2459 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
2460 pipe(uniq_fd) == -1)
2461 return _("Could not create pipe");
2462
2463 statusbar(_("Creating misspelled word list, please wait..."));
2464
2465 /* A new process to run spell in. */
2466 if ((pid_spell = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002467 /* Child continues (i.e. future spell process). */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002468 close(spell_fd[0]);
2469
2470 /* Replace the standard input with the temp file. */
2471 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
2472 goto close_pipes_and_exit;
2473
2474 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
2475 goto close_pipes_and_exit;
2476
2477 close(tempfile_fd);
2478
2479 /* Send spell's standard output to the pipe. */
2480 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2481 goto close_pipes_and_exit;
2482
2483 close(spell_fd[1]);
2484
David Lawrence Ramsey3239ff22005-11-29 20:01:06 +00002485 /* Start the spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002486 execlp("spell", "spell", NULL);
2487
2488 /* This should not be reached if spell is found. */
2489 exit(1);
2490 }
2491
2492 /* Parent continues here. */
2493 close(spell_fd[1]);
2494
2495 /* A new process to run sort in. */
2496 if ((pid_sort = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002497 /* Child continues (i.e. future spell process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002498 * standard input with the standard output of the old pipe. */
2499 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
2500 goto close_pipes_and_exit;
2501
2502 close(spell_fd[0]);
2503
2504 /* Send sort's standard output to the new pipe. */
2505 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2506 goto close_pipes_and_exit;
2507
2508 close(sort_fd[1]);
2509
2510 /* Start the sort program. Use -f to remove mixed case. If
2511 * this isn't portable, let me know. */
2512 execlp("sort", "sort", "-f", NULL);
2513
2514 /* This should not be reached if sort is found. */
2515 exit(1);
2516 }
2517
2518 close(spell_fd[0]);
2519 close(sort_fd[1]);
2520
2521 /* A new process to run uniq in. */
2522 if ((pid_uniq = fork()) == 0) {
David Lawrence Ramseyb159f942006-07-28 17:06:27 +00002523 /* Child continues (i.e. future uniq process). Replace the
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002524 * standard input with the standard output of the old pipe. */
2525 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
2526 goto close_pipes_and_exit;
2527
2528 close(sort_fd[0]);
2529
2530 /* Send uniq's standard output to the new pipe. */
2531 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2532 goto close_pipes_and_exit;
2533
2534 close(uniq_fd[1]);
2535
2536 /* Start the uniq program; we are using PATH. */
2537 execlp("uniq", "uniq", NULL);
2538
2539 /* This should not be reached if uniq is found. */
2540 exit(1);
2541 }
2542
2543 close(sort_fd[0]);
2544 close(uniq_fd[1]);
2545
2546 /* The child process was not forked successfully. */
2547 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
2548 close(uniq_fd[0]);
2549 return _("Could not fork");
2550 }
2551
2552 /* Get the system pipe buffer size. */
2553 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
2554 close(uniq_fd[0]);
2555 return _("Could not get size of pipe buffer");
2556 }
2557
2558 /* Read in the returned spelling errors. */
2559 read_buff_read = 0;
2560 read_buff_size = pipe_buff_size + 1;
2561 read_buff = read_buff_ptr = charalloc(read_buff_size);
2562
2563 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
2564 pipe_buff_size)) > 0) {
2565 read_buff_read += bytesread;
2566 read_buff_size += pipe_buff_size;
2567 read_buff = read_buff_ptr = charealloc(read_buff,
2568 read_buff_size);
2569 read_buff_ptr += read_buff_read;
2570 }
2571
2572 *read_buff_ptr = '\0';
2573 close(uniq_fd[0]);
2574
2575 /* Process the spelling errors. */
2576 read_buff_word = read_buff_ptr = read_buff;
2577
2578 while (*read_buff_ptr != '\0') {
2579 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
2580 *read_buff_ptr = '\0';
2581 if (read_buff_word != read_buff_ptr) {
2582 if (!do_int_spell_fix(read_buff_word)) {
2583 read_buff_word = read_buff_ptr;
2584 break;
2585 }
2586 }
2587 read_buff_word = read_buff_ptr + 1;
2588 }
2589 read_buff_ptr++;
2590 }
2591
2592 /* Special case: the last word doesn't end with '\r' or '\n'. */
2593 if (read_buff_word != read_buff_ptr)
2594 do_int_spell_fix(read_buff_word);
2595
2596 free(read_buff);
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002597 search_replace_abort();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002598 edit_refresh();
2599
2600 /* Process the end of the spell process. */
2601 waitpid(pid_spell, &spell_status, 0);
2602 waitpid(pid_sort, &sort_status, 0);
2603 waitpid(pid_uniq, &uniq_status, 0);
2604
2605 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
2606 return _("Error invoking \"spell\"");
2607
2608 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
2609 return _("Error invoking \"sort -f\"");
2610
2611 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
2612 return _("Error invoking \"uniq\"");
2613
2614 /* Otherwise... */
2615 return NULL;
2616
2617 close_pipes_and_exit:
2618 /* Don't leak any handles. */
2619 close(tempfile_fd);
2620 close(spell_fd[0]);
2621 close(spell_fd[1]);
2622 close(sort_fd[0]);
2623 close(sort_fd[1]);
2624 close(uniq_fd[0]);
2625 close(uniq_fd[1]);
2626 exit(1);
2627}
2628
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002629/* External (alternate) spell checking. Return NULL for normal
2630 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002631const char *do_alt_speller(char *tempfile_name)
2632{
2633 int alt_spell_status;
2634 size_t current_x_save = openfile->current_x;
2635 size_t pww_save = openfile->placewewant;
2636 ssize_t current_y_save = openfile->current_y;
2637 ssize_t lineno_save = openfile->current->lineno;
2638 pid_t pid_spell;
2639 char *ptr;
2640 static int arglen = 3;
2641 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002642#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002643 bool old_mark_set = openfile->mark_set;
2644 bool added_magicline = FALSE;
2645 /* Whether we added a magicline after filebot. */
2646 bool right_side_up = FALSE;
2647 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2648 * FALSE if (current, current_x) is. */
2649 filestruct *top, *bot;
2650 size_t top_x, bot_x;
2651 ssize_t mb_lineno_save = 0;
2652 /* We're going to close the current file, and open the output of
2653 * the alternate spell command. The line that mark_begin points
2654 * to will be freed, so we save the line number and restore it
2655 * afterwards. */
2656 size_t totsize_save = openfile->totsize;
2657 /* Our saved value of totsize, used when we spell-check a marked
2658 * selection. */
2659
2660 if (old_mark_set) {
2661 /* If the mark is on, save the number of the line it starts on,
2662 * and then turn the mark off. */
2663 mb_lineno_save = openfile->mark_begin->lineno;
2664 openfile->mark_set = FALSE;
2665 }
2666#endif
2667
2668 endwin();
2669
2670 /* Set up an argument list to pass execvp(). */
2671 if (spellargs == NULL) {
2672 spellargs = (char **)nmalloc(arglen * sizeof(char *));
2673
2674 spellargs[0] = strtok(alt_speller, " ");
2675 while ((ptr = strtok(NULL, " ")) != NULL) {
2676 arglen++;
2677 spellargs = (char **)nrealloc(spellargs, arglen *
2678 sizeof(char *));
2679 spellargs[arglen - 3] = ptr;
2680 }
2681 spellargs[arglen - 1] = NULL;
2682 }
2683 spellargs[arglen - 2] = tempfile_name;
2684
2685 /* Start a new process for the alternate speller. */
2686 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey2e2112c2005-11-29 05:39:31 +00002687 /* Start alternate spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002688 execvp(spellargs[0], spellargs);
2689
2690 /* Should not be reached, if alternate speller is found!!! */
2691 exit(1);
2692 }
2693
2694 /* If we couldn't fork, get out. */
2695 if (pid_spell < 0)
2696 return _("Could not fork");
2697
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002698#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002699 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002700 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002701 allow_pending_sigwinch(FALSE);
2702#endif
2703
2704 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002705 wait(&alt_spell_status);
2706
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00002707 /* Reenter curses mode. */
2708 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002709
2710 /* Restore the terminal to its previous state. */
2711 terminal_init();
2712
2713 /* Turn the cursor back on for sure. */
2714 curs_set(1);
2715
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002716 /* The screen might have been resized. If it has, reinitialize all
2717 * the windows based on the new screen dimensions. */
2718 window_init();
2719
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002720 if (!WIFEXITED(alt_spell_status) ||
2721 WEXITSTATUS(alt_spell_status) != 0) {
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002722 char *alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002723 char *invoke_error = _("Error invoking \"%s\"");
2724
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002725#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002726 /* Turn the mark back on if it was on before. */
2727 openfile->mark_set = old_mark_set;
2728#endif
2729
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002730 alt_spell_error =
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002731 charalloc(strlen(invoke_error) +
2732 strlen(alt_speller) + 1);
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002733 sprintf(alt_spell_error, invoke_error, alt_speller);
2734 return alt_spell_error;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002735 }
2736
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002737#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002738 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002739 /* If the mark is on, partition the filestruct so that it
2740 * contains only the marked text; if the NO_NEWLINES flag isn't
2741 * set, keep track of whether the text will have a magicline
2742 * added when we're done correcting misspelled words; and
2743 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002744 mark_order((const filestruct **)&top, &top_x,
2745 (const filestruct **)&bot, &bot_x, &right_side_up);
2746 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002747 if (!ISSET(NO_NEWLINES))
2748 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002749
2750 /* Get the number of characters in the marked text, and subtract
2751 * it from the saved value of totsize. */
2752 totsize_save -= get_totsize(top, bot);
2753 }
2754#endif
2755
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002756 /* Replace the text of the current buffer with the spell-checked
2757 * text. */
2758 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002759
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002760#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002761 if (old_mark_set) {
2762 filestruct *top_save = openfile->fileage;
2763
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002764 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2765 * added a magicline, remove it now. */
2766 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002767 remove_magicline();
2768
2769 /* Put the beginning and the end of the mark at the beginning
2770 * and the end of the spell-checked text. */
2771 if (openfile->fileage == openfile->filebot)
2772 bot_x += top_x;
2773 if (right_side_up) {
2774 openfile->mark_begin_x = top_x;
2775 current_x_save = bot_x;
2776 } else {
2777 current_x_save = top_x;
2778 openfile->mark_begin_x = bot_x;
2779 }
2780
2781 /* Unpartition the filestruct so that it contains all the text
2782 * again. Note that we've replaced the marked text originally
2783 * in the partition with the spell-checked marked text in the
2784 * temp file. */
2785 unpartition_filestruct(&filepart);
2786
2787 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002788 * partition. Also add the number of characters in the
2789 * spell-checked marked text to the saved value of totsize, and
2790 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002791 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002792 totsize_save += openfile->totsize;
2793 openfile->totsize = totsize_save;
2794
2795 /* Assign mark_begin to the line where the mark began before. */
2796 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2797 current_y_save, 0);
2798 openfile->mark_begin = openfile->current;
2799
2800 /* Assign mark_begin_x to the location in mark_begin where the
2801 * mark began before, adjusted for any shortening of the
2802 * line. */
2803 openfile->mark_begin_x = openfile->current_x;
2804
2805 /* Turn the mark back on. */
2806 openfile->mark_set = TRUE;
2807 }
2808#endif
2809
2810 /* Go back to the old position, and mark the file as modified. */
2811 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2812 set_modified();
2813
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002814#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002815 /* Handle a pending SIGWINCH again. */
2816 allow_pending_sigwinch(TRUE);
2817#endif
2818
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002819 return NULL;
2820}
2821
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002822/* Spell check the current file. If an alternate spell checker is
2823 * specified, use it. Otherwise, use the internal spell checker. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002824void do_spell(void)
2825{
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002826 bool status;
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002827 FILE *temp_file;
2828 char *temp = safe_tempfile(&temp_file);
2829 const char *spell_msg;
2830
2831 if (temp == NULL) {
David Lawrence Ramseyf0e3ca62006-05-03 13:11:00 +00002832 statusbar(_("Error writing temp file: %s"), strerror(errno));
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002833 return;
2834 }
2835
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002836 status =
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002837#ifndef NANO_TINY
David Lawrence Ramsey0e1df432007-01-12 02:58:12 +00002838 openfile->mark_set ? write_marked_file(temp, temp_file, TRUE,
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002839 OVERWRITE) :
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002840#endif
David Lawrence Ramseyb6c4dbf2006-11-25 22:38:17 +00002841 write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002842
David Lawrence Ramsey9e7b2d52007-01-11 22:46:22 +00002843 if (!status) {
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002844 statusbar(_("Error writing temp file: %s"), strerror(errno));
2845 free(temp);
2846 return;
2847 }
2848
2849 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2850 do_int_speller(temp);
2851 unlink(temp);
2852 free(temp);
2853
Chris Allegretta79a33bb2008-03-05 07:34:01 +00002854 currmenu = MMAIN;
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002855
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002856 /* If the spell-checker printed any error messages onscreen, make
2857 * sure that they're cleared off. */
David Lawrence Ramseyf32e1dd2006-06-09 17:09:51 +00002858 total_refresh();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002859
2860 if (spell_msg != NULL) {
2861 if (errno == 0)
2862 /* Don't display an error message of "Success". */
2863 statusbar(_("Spell checking failed: %s"), spell_msg);
2864 else
2865 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2866 strerror(errno));
2867 } else
2868 statusbar(_("Finished checking spelling"));
2869}
2870#endif /* !DISABLE_SPELLER */
2871
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002872#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002873/* Our own version of "wc". Note that its character counts are in
2874 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002875void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002876{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002877 size_t words = 0, chars = 0;
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002878 ssize_t nlines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002879 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002880 size_t pww_save = openfile->placewewant;
2881 filestruct *current_save = openfile->current;
2882 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002883 filestruct *top, *bot;
2884 size_t top_x, bot_x;
2885
2886 if (old_mark_set) {
2887 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002888 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002889 mark_order((const filestruct **)&top, &top_x,
2890 (const filestruct **)&bot, &bot_x, NULL);
2891 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002892 openfile->mark_set = FALSE;
2893 }
2894
2895 /* Start at the top of the file. */
2896 openfile->current = openfile->fileage;
2897 openfile->current_x = 0;
2898 openfile->placewewant = 0;
2899
2900 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002901 * part of a word, as "wc -w" does), without updating the screen,
2902 * until we reach the end of the file, incrementing the total word
2903 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002904 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002905 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002906 if (do_next_word(TRUE, FALSE))
2907 words++;
2908 }
2909
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002910 /* Get the total line and character counts, as "wc -l" and "wc -c"
2911 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002912 if (old_mark_set) {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002913 nlines = openfile->filebot->lineno -
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002914 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002915 chars = get_totsize(openfile->fileage, openfile->filebot);
2916
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002917 /* Unpartition the filestruct so that it contains all the text
2918 * again, and turn the mark back on. */
2919 unpartition_filestruct(&filepart);
2920 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002921 } else {
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002922 nlines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002923 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002924 }
2925
2926 /* Restore where we were. */
2927 openfile->current = current_save;
2928 openfile->current_x = current_x_save;
2929 openfile->placewewant = pww_save;
2930
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002931 /* Display the total word, line, and character counts on the
2932 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002933 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
Chris Allegretta8b6461f2008-05-31 23:09:40 +00002934 _("In Selection: ") : "", (unsigned long)words, (long)nlines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002935 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002936}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002937#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002938
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002939/* Get verbatim input. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002940void do_verbatim_input(void)
2941{
2942 int *kbinput;
2943 size_t kbinput_len, i;
2944 char *output;
2945
David Lawrence Ramseyf451d6a2006-05-27 16:02:48 +00002946 /* TRANSLATORS: This is displayed when the next keystroke will be
2947 * inserted verbatim. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002948 statusbar(_("Verbatim Input"));
2949
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002950 /* Read in all the verbatim characters. */
2951 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2952
David Lawrence Ramseya620e682006-05-27 18:19:03 +00002953 /* If constant cursor position display is on, make sure the current
2954 * cursor position will be properly displayed on the statusbar.
2955 * Otherwise, blank the statusbar. */
2956 if (ISSET(CONST_UPDATE))
2957 do_cursorpos(TRUE);
2958 else {
2959 blank_statusbar();
2960 wnoutrefresh(bottomwin);
2961 }
David Lawrence Ramsey6fb66892006-05-27 17:39:19 +00002962
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002963 /* Display all the verbatim characters at once, not filtering out
2964 * control characters. */
2965 output = charalloc(kbinput_len + 1);
2966
2967 for (i = 0; i < kbinput_len; i++)
2968 output[i] = (char)kbinput[i];
2969 output[i] = '\0';
2970
David Lawrence Ramseyad36bdc2006-12-02 17:22:21 +00002971 free(kbinput);
2972
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002973 do_output(output, kbinput_len, TRUE);
2974
2975 free(output);
2976}
Chris Allegretta07fcc4c2008-07-10 20:13:04 +00002977