blob: ad09855cb028c57abf8f412de7784162d68bdfd6 [file] [log] [blame]
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001/* $Id$ */
2/**************************************************************************
David Lawrence Ramsey4005fc62005-10-08 06:12:41 +00003 * text.c *
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00004 * *
David Lawrence Ramsey315eb322005-11-28 19:35:29 +00005 * Copyright (C) 1999-2004 Chris Allegretta *
David Lawrence Ramsey33f2a082006-02-07 21:11:49 +00006 * Copyright (C) 2005-2006 David Lawrence Ramsey *
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 *
9 * the Free Software Foundation; either version 2, or (at your option) *
10 * 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 {
58 statusbar(_("Mark UNset"));
59 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 Ramsey6d6a36c2005-12-08 07:09:08 +000066/* Delete one character. */
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
73 assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data));
74
75 openfile->placewewant = xplustabs();
76
77 if (openfile->current->data[openfile->current_x] != '\0') {
78 int char_buf_len = parse_mbchar(openfile->current->data +
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +000079 openfile->current_x, NULL, NULL);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000080 size_t line_len = strlen(openfile->current->data +
81 openfile->current_x);
82
83 assert(openfile->current_x < strlen(openfile->current->data));
84
85 /* Let's get dangerous. */
86 charmove(&openfile->current->data[openfile->current_x],
87 &openfile->current->data[openfile->current_x +
88 char_buf_len], line_len - char_buf_len + 1);
89
90 null_at(&openfile->current->data, openfile->current_x +
91 line_len - char_buf_len);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +000092#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +000093 if (openfile->mark_set && openfile->mark_begin ==
94 openfile->current && openfile->current_x <
95 openfile->mark_begin_x)
96 openfile->mark_begin_x -= char_buf_len;
97#endif
98 openfile->totsize--;
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +000099 } else if (openfile->current != openfile->filebot) {
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000100 filestruct *foo = openfile->current->next;
101
102 assert(openfile->current_x == strlen(openfile->current->data));
103
104 /* If we're deleting at the end of a line, we need to call
105 * edit_refresh(). */
106 if (openfile->current->data[openfile->current_x] == '\0')
107 do_refresh = TRUE;
108
109 openfile->current->data = charealloc(openfile->current->data,
110 openfile->current_x + strlen(foo->data) + 1);
111 strcpy(openfile->current->data + openfile->current_x,
112 foo->data);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000113#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000114 if (openfile->mark_set && openfile->mark_begin ==
115 openfile->current->next) {
116 openfile->mark_begin = openfile->current;
117 openfile->mark_begin_x += openfile->current_x;
118 }
119#endif
120 if (openfile->filebot == foo)
121 openfile->filebot = openfile->current;
122
123 unlink_node(foo);
124 delete_node(foo);
125 renumber(openfile->current);
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000126 openfile->totsize--;
127#ifndef DISABLE_WRAPPING
128 wrap_reset();
129#endif
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000130
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000131 /* If the NO_NEWLINES flag isn't set, and text has been added to
132 * the magicline as a result of deleting at the end of the line
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000133 * before filebot, add a new magicline. */
David Lawrence Ramseya0168ca2005-11-05 17:35:44 +0000134 if (!ISSET(NO_NEWLINES) && openfile->current ==
135 openfile->filebot && openfile->current->data[0] != '\0')
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +0000136 new_magicline();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000137 } else
138 return;
139
David Lawrence Ramsey0f6236f2005-11-09 00:08:29 +0000140 set_modified();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000141
142#ifdef ENABLE_COLOR
143 /* If color syntaxes are available and turned on, we need to call
144 * edit_refresh(). */
145 if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX))
146 do_refresh = TRUE;
147#endif
148
149 if (do_refresh)
150 edit_refresh();
151 else
152 update_line(openfile->current, openfile->current_x);
153}
154
David Lawrence Ramsey5b7b3e32005-12-31 21:22:54 +0000155/* Backspace over one character. That is, move the cursor left one
156 * character, and then delete the character there. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000157void do_backspace(void)
158{
159 if (openfile->current != openfile->fileage ||
160 openfile->current_x > 0) {
David Lawrence Ramsey1c3bfa92005-09-13 04:53:44 +0000161 do_left();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000162 do_delete();
163 }
164}
165
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000166/* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number
167 * of spaces that a tab would normally take up. */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000168void do_tab(void)
169{
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000170#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000171 if (ISSET(TABS_TO_SPACES)) {
172 char *output;
David Lawrence Ramsey90b07fc2005-10-07 15:57:48 +0000173 size_t output_len = 0, new_pww = xplustabs();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000174
175 do {
176 new_pww++;
177 output_len++;
178 } while (new_pww % tabsize != 0);
179
180 output = charalloc(output_len + 1);
181
182 charset(output, ' ', output_len);
183 output[output_len] = '\0';
184
185 do_output(output, output_len, TRUE);
186
187 free(output);
188 } else {
189#endif
190 do_output("\t", 1, TRUE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000191#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000192 }
193#endif
194}
195
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000196#ifndef NANO_TINY
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000197/* Indent or unindent all lines covered by the mark len columns,
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000198 * depending on whether len is positive or negative. If the
199 * TABS_TO_SPACES flag is set, indent/unindent by len spaces.
200 * Otherwise, indent/unindent by (len / tabsize) tabs and (len %
201 * tabsize) spaces. */
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000202void do_indent_marked(ssize_t cols)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000203{
204 bool indent_changed = FALSE;
205 /* Whether any indenting or unindenting was done. */
206 bool unindent = FALSE;
207 /* Whether we're unindenting text. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000208 char *line_indent = NULL;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000209 /* The text added to each line in order to indent it. */
David Lawrence Ramsey80669c32006-05-05 15:41:43 +0000210 size_t line_indent_len = 0;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000211 /* The length of the text added to each line in order to indent
212 * it. */
213 filestruct *top, *bot, *f;
214 size_t top_x, bot_x;
215
216 assert(openfile->current != NULL && openfile->current->data != NULL);
217
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000218 /* If the mark isn't on, indicate it on the statusbar and get
219 * out. */
220 if (!openfile->mark_set) {
221 statusbar(_("No lines selected, nothing to do!"));
222 return;
223 }
224
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000225 /* If cols is zero, get out. */
226 if (cols == 0)
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000227 return;
228
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000229 /* If cols is negative, make it positive and set unindent to
230 * TRUE. */
231 if (cols < 0) {
232 cols = -cols;
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000233 unindent = TRUE;
234 /* Otherwise, we're indenting, in which case the file will always be
235 * modified, so set indent_changed to TRUE. */
236 } else
237 indent_changed = TRUE;
238
239 /* Get the coordinates of the marked text. */
240 mark_order((const filestruct **)&top, &top_x,
241 (const filestruct **)&bot, &bot_x, NULL);
242
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 Ramseyf85001a2006-04-28 13:19:56 +0000266 /* Go through each line of the marked text. */
267 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. */
282 if (f == openfile->mark_begin && openfile->mark_begin_x >=
283 indent_len)
284 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 Ramsey2e8fac62006-04-29 15:44:58 +0000317 if (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 Ramseyaf9052d2006-05-01 17:14:25 +0000353/* Indent all lines covered by the mark tabsize columns. */
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000354void do_indent_marked_void(void)
355{
356 do_indent_marked(tabsize);
357}
358
David Lawrence Ramseyaf9052d2006-05-01 17:14:25 +0000359/* Unindent all lines covered by the mark tabsize columns. */
David Lawrence Ramseyf85001a2006-04-28 13:19:56 +0000360void do_unindent_marked_void(void)
361{
362 do_indent_marked(-tabsize);
363}
364#endif /* !NANO_TINY */
365
David Lawrence Ramseyb0e04c02005-12-08 07:24:54 +0000366/* Someone hits Enter *gasp!* */
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000367void do_enter(void)
368{
369 filestruct *newnode = make_new_node(openfile->current);
370 size_t extra = 0;
371
372 assert(openfile->current != NULL && openfile->current->data != NULL);
373
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000374#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000375 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
376 if (ISSET(AUTOINDENT)) {
377 /* If we are breaking the line in the indentation, the new
378 * indentation should have only current_x characters, and
379 * current_x should not change. */
380 extra = indent_length(openfile->current->data);
381 if (extra > openfile->current_x)
382 extra = openfile->current_x;
383 }
384#endif
385 newnode->data = charalloc(strlen(openfile->current->data +
386 openfile->current_x) + extra + 1);
387 strcpy(&newnode->data[extra], openfile->current->data +
388 openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000389#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000390 if (ISSET(AUTOINDENT)) {
391 strncpy(newnode->data, openfile->current->data, extra);
392 openfile->totsize += mbstrlen(newnode->data);
393 }
394#endif
395 null_at(&openfile->current->data, openfile->current_x);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000396#ifndef NANO_TINY
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000397 if (openfile->mark_set && openfile->current ==
398 openfile->mark_begin && openfile->current_x <
399 openfile->mark_begin_x) {
400 openfile->mark_begin = newnode;
401 openfile->mark_begin_x += extra - openfile->current_x;
402 }
403#endif
404 openfile->current_x = extra;
405
406 if (openfile->current == openfile->filebot)
407 openfile->filebot = newnode;
408 splice_node(openfile->current, newnode,
409 openfile->current->next);
410
411 renumber(openfile->current);
412 openfile->current = newnode;
413
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000414 openfile->totsize++;
415 set_modified();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000416
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000417 openfile->placewewant = xplustabs();
David Lawrence Ramseyfeb89db2005-09-13 04:45:46 +0000418
419 edit_refresh();
David Lawrence Ramsey7ea09e52005-07-25 02:41:59 +0000420}
421
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000422#ifndef NANO_TINY
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000423/* Send a SIGKILL (unconditional kill) to the forked process in
424 * execute_command(). */
David Lawrence Ramsey8befda62005-12-06 19:39:56 +0000425RETSIGTYPE cancel_command(int signal)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000426{
427 if (kill(pid, SIGKILL) == -1)
428 nperror("kill");
429}
430
David Lawrence Ramsey0ed71712005-11-08 23:09:47 +0000431/* Execute command in a shell. Return TRUE on success. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000432bool execute_command(const char *command)
433{
434 int fd[2];
435 FILE *f;
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000436 char *shellenv;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000437 struct sigaction oldaction, newaction;
438 /* Original and temporary handlers for SIGINT. */
439 bool sig_failed = FALSE;
440 /* Did sigaction() fail without changing the signal handlers? */
441
442 /* Make our pipes. */
443 if (pipe(fd) == -1) {
444 statusbar(_("Could not pipe"));
445 return FALSE;
446 }
447
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000448 /* Check $SHELL for the shell to use. If it isn't set, use
David Lawrence Ramsey1932dfb2005-11-29 05:52:49 +0000449 * /bin/sh. Note that $SHELL should contain only a path, with no
450 * arguments. */
David Lawrence Ramseyeae85712005-11-29 05:48:06 +0000451 shellenv = getenv("SHELL");
452 if (shellenv == NULL)
453 shellenv = "/bin/sh";
454
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000455 /* Fork a child. */
456 if ((pid = fork()) == 0) {
457 close(fd[0]);
458 dup2(fd[1], fileno(stdout));
459 dup2(fd[1], fileno(stderr));
460
461 /* If execl() returns at all, there was an error. */
David Lawrence Ramsey5da68ee2005-11-29 05:21:06 +0000462 execl(shellenv, tail(shellenv), "-c", command, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000463 exit(0);
464 }
465
David Lawrence Ramseyc838a4c2006-04-26 18:33:50 +0000466 /* Continue as parent. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000467 close(fd[1]);
468
469 if (pid == -1) {
470 close(fd[0]);
471 statusbar(_("Could not fork"));
472 return FALSE;
473 }
474
475 /* Before we start reading the forked command's output, we set
476 * things up so that Ctrl-C will cancel the new process. */
477
478 /* Enable interpretation of the special control keys so that we get
479 * SIGINT when Ctrl-C is pressed. */
480 enable_signals();
481
482 if (sigaction(SIGINT, NULL, &newaction) == -1) {
483 sig_failed = TRUE;
484 nperror("sigaction");
485 } else {
486 newaction.sa_handler = cancel_command;
487 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
488 sig_failed = TRUE;
489 nperror("sigaction");
490 }
491 }
492
493 /* Note that now oldaction is the previous SIGINT signal handler,
494 * to be restored later. */
495
496 f = fdopen(fd[0], "rb");
497 if (f == NULL)
498 nperror("fdopen");
499
500 read_file(f, "stdin");
501
502 /* If multibuffer mode is on, we could be here in view mode. If so,
503 * don't set the modification flag. */
504 if (!ISSET(VIEW_MODE))
505 set_modified();
506
507 if (wait(NULL) == -1)
508 nperror("wait");
509
510 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
511 nperror("sigaction");
512
513 /* Disable interpretation of the special control keys so that we can
514 * use Ctrl-C for other things. */
515 disable_signals();
516
517 return TRUE;
518}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000519#endif /* !NANO_TINY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000520
521#ifndef DISABLE_WRAPPING
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +0000522/* Clear the prepend_wrap flag. We need to do this as soon as we do
523 * something other than type text. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000524void wrap_reset(void)
525{
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000526 prepend_wrap = FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000527}
528
529/* We wrap the given line. Precondition: we assume the cursor has been
David Lawrence Ramsey139fa652006-05-22 01:26:24 +0000530 * moved forward since the last typed character. Return TRUE if we
531 * wrapped, and FALSE otherwise. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000532bool do_wrap(filestruct *line)
533{
534 size_t line_len;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000535 /* The length of the line we wrap. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000536 ssize_t wrap_loc;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000537 /* The index of line->data where we wrap. */
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000538#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000539 const char *indent_string = NULL;
540 /* Indentation to prepend to the new line. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000541 size_t indent_len = 0;
542 /* The length of indent_string. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000543#endif
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000544 const char *after_break;
545 /* The text after the wrap point. */
546 size_t after_break_len;
547 /* The length of after_break. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000548 bool prepending = FALSE;
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000549 /* Do we prepend to the next line? */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000550 const char *next_line = NULL;
551 /* The next line, minus indentation. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +0000552 size_t next_line_len = 0;
553 /* The length of next_line. */
554 char *new_line = NULL;
555 /* The line we create. */
556 size_t new_line_len = 0;
557 /* The eventual length of new_line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000558
559 /* There are three steps. First, we decide where to wrap. Then, we
560 * create the new wrap line. Finally, we clean up. */
561
562 /* Step 1, finding where to wrap. We are going to add a new line
563 * after a blank character. In this step, we call break_line() to
564 * get the location of the last blank we can break the line at, and
565 * and set wrap_loc to the location of the character after it, so
566 * that the blank is preserved at the end of the line.
567 *
568 * If there is no legal wrap point, or we reach the last character
569 * of the line while trying to find one, we should return without
570 * wrapping. Note that if autoindent is turned on, we don't break
571 * at the end of it! */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000572 assert(line != NULL && line->data != NULL);
573
574 /* Save the length of the line. */
575 line_len = strlen(line->data);
576
577 /* Find the last blank where we can break the line. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000578 wrap_loc = break_line(line->data, fill
579#ifndef DISABLE_HELP
580 , FALSE
581#endif
582 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000583
584 /* If we couldn't break the line, or we've reached the end of it, we
585 * don't wrap. */
586 if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
587 return FALSE;
588
589 /* Otherwise, move forward to the character just after the blank. */
590 wrap_loc += move_mbright(line->data + wrap_loc, 0);
591
592 /* If we've reached the end of the line, we don't wrap. */
593 if (line->data[wrap_loc] == '\0')
594 return FALSE;
595
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000596#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000597 /* If autoindent is turned on, and we're on the character just after
598 * the indentation, we don't wrap. */
599 if (ISSET(AUTOINDENT)) {
600 /* Get the indentation of this line. */
601 indent_string = line->data;
602 indent_len = indent_length(indent_string);
603
604 if (wrap_loc == indent_len)
605 return FALSE;
606 }
607#endif
608
609 /* Step 2, making the new wrap line. It will consist of indentation
610 * followed by the text after the wrap point, optionally followed by
611 * a space (if the text after the wrap point doesn't end in a blank)
612 * and the text of the next line, if they can fit without
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000613 * wrapping, the next line exists, and the prepend_wrap flag is
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000614 * set. */
615
616 /* after_break is the text that will be wrapped to the next line. */
617 after_break = line->data + wrap_loc;
618 after_break_len = line_len - wrap_loc;
619
620 assert(strlen(after_break) == after_break_len);
621
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000622 /* We prepend the wrapped text to the next line, if the prepend_wrap
623 * flag is set, there is a next line, and prepending would not make
624 * the line too long. */
625 if (prepend_wrap && line != openfile->filebot) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000626 const char *end = after_break + move_mbleft(after_break,
627 after_break_len);
628
629 /* If after_break doesn't end in a blank, make sure it ends in a
630 * space. */
631 if (!is_blank_mbchar(end)) {
632 line_len++;
633 line->data = charealloc(line->data, line_len + 1);
634 line->data[line_len - 1] = ' ';
635 line->data[line_len] = '\0';
636 after_break = line->data + wrap_loc;
637 after_break_len++;
638 openfile->totsize++;
639 }
640
641 next_line = line->next->data;
642 next_line_len = strlen(next_line);
643
644 if (after_break_len + next_line_len <= fill) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000645 prepending = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000646 new_line_len += next_line_len;
647 }
648 }
649
650 /* new_line_len is now the length of the text that will be wrapped
651 * to the next line, plus (if we're prepending to it) the length of
652 * the text of the next line. */
653 new_line_len += after_break_len;
654
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000655#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000656 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000657 if (prepending) {
658 /* If we're prepending, the indentation will come from the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000659 * next line. */
660 indent_string = next_line;
661 indent_len = indent_length(indent_string);
662 next_line += indent_len;
663 } else {
664 /* Otherwise, it will come from this line, in which case
665 * we should increase new_line_len to make room for it. */
666 new_line_len += indent_len;
667 openfile->totsize += mbstrnlen(indent_string, indent_len);
668 }
669 }
670#endif
671
672 /* Now we allocate the new line and copy the text into it. */
673 new_line = charalloc(new_line_len + 1);
674 new_line[0] = '\0';
675
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000676#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000677 if (ISSET(AUTOINDENT)) {
678 /* Copy the indentation. */
679 strncpy(new_line, indent_string, indent_len);
680 new_line[indent_len] = '\0';
681 new_line_len += indent_len;
682 }
683#endif
684
685 /* Copy all the text after the wrap point of the current line. */
686 strcat(new_line, after_break);
687
688 /* Break the current line at the wrap point. */
689 null_at(&line->data, wrap_loc);
690
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000691 if (prepending) {
692 /* If we're prepending, copy the text from the next line, minus
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000693 * the indentation that we already copied above. */
694 strcat(new_line, next_line);
695
696 free(line->next->data);
697 line->next->data = new_line;
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000698
699 /* If the NO_NEWLINES flag isn't set, and text has been added to
700 * the magicline, make a new magicline. */
701 if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
702 new_magicline();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000703 } else {
704 /* Otherwise, make a new line and copy the text after where we
705 * broke this line to the beginning of the new line. */
706 splice_node(openfile->current, make_new_node(openfile->current),
707 openfile->current->next);
708
David Lawrence Ramsey219a8142005-11-22 22:08:01 +0000709 /* If the current line is the last line of the file, move the
710 * last line of the file down to the next line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000711 if (openfile->filebot == openfile->current)
712 openfile->filebot = openfile->current->next;
713
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000714 openfile->current->next->data = new_line;
715
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000716 openfile->totsize++;
717 }
718
719 /* Step 3, clean up. Reposition the cursor and mark, and do some
720 * other sundry things. */
721
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000722 /* Set the prepend_wrap flag, so that later wraps of this line will
723 * be prepended to the next line. */
724 prepend_wrap = TRUE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000725
David Lawrence Ramsey2829aae2006-05-22 01:24:09 +0000726 /* Each line knows its number. We recalculate these if we inserted
727 * a new line. */
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000728 if (!prepending)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000729 renumber(line);
730
731 /* If the cursor was after the break point, we must move it. We
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000732 * also clear the prepend_wrap flag in this case. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000733 if (openfile->current_x > wrap_loc) {
David Lawrence Ramseyb4e5c022005-11-25 13:48:09 +0000734 prepend_wrap = FALSE;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000735
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000736 openfile->current = openfile->current->next;
737 openfile->current_x -= wrap_loc
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000738#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000739 - indent_len
740#endif
741 ;
742 openfile->placewewant = xplustabs();
743 }
744
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000745#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000746 /* If the mark was on this line after the wrap point, we move it
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000747 * down. If it was on the next line and we prepended to that line,
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000748 * we move it right. */
749 if (openfile->mark_set) {
750 if (openfile->mark_begin == line && openfile->mark_begin_x >
751 wrap_loc) {
752 openfile->mark_begin = line->next;
753 openfile->mark_begin_x -= wrap_loc - indent_len + 1;
David Lawrence Ramsey615f4c72005-11-22 21:48:24 +0000754 } else if (prepending && openfile->mark_begin == line->next)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000755 openfile->mark_begin_x += after_break_len;
756 }
757#endif
758
759 return TRUE;
760}
761#endif /* !DISABLE_WRAPPING */
762
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +0000763#if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000764/* We are trying to break a chunk off line. We find the last blank such
David Lawrence Ramseycd9a5f02005-09-20 06:12:54 +0000765 * that the display length to there is at most (goal + 1). If there is
766 * no such blank, then we find the first blank. We then take the last
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000767 * blank in that group of blanks. The terminating '\0' counts as a
768 * blank, as does a '\n' if newline is TRUE. */
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000769ssize_t break_line(const char *line, ssize_t goal
770#ifndef DISABLE_HELP
771 , bool newline
772#endif
773 )
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000774{
775 ssize_t blank_loc = -1;
776 /* Current tentative return value. Index of the last blank we
777 * found with short enough display width. */
778 ssize_t cur_loc = 0;
779 /* Current index in line. */
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +0000780 size_t cur_pos = 0;
781 /* Current column position in line. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000782 int line_len;
783
784 assert(line != NULL);
785
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +0000786 while (*line != '\0' && goal >= cur_pos) {
787 line_len = parse_mbchar(line, NULL, &cur_pos);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000788
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000789 if (is_blank_mbchar(line)
790#ifndef DISABLE_HELP
791 || (newline && *line == '\n')
792#endif
793 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000794 blank_loc = cur_loc;
795
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000796#ifndef DISABLE_HELP
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000797 if (newline && *line == '\n')
798 break;
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000799#endif
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000800 }
801
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000802 line += line_len;
803 cur_loc += line_len;
804 }
805
David Lawrence Ramsey2d3d1e92006-05-18 17:28:16 +0000806 if (goal >= cur_pos)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000807 /* In fact, the whole line displays shorter than goal. */
808 return cur_loc;
809
810 if (blank_loc == -1) {
811 /* No blank was found that was short enough. */
812 bool found_blank = FALSE;
David Lawrence Ramsey5ab12ca2005-09-20 17:52:52 +0000813 ssize_t found_blank_loc = 0;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000814
815 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000816 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000817
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000818 if (is_blank_mbchar(line)
819#ifndef DISABLE_HELP
820 || (newline && *line == '\n')
821#endif
822 ) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000823 if (!found_blank)
824 found_blank = TRUE;
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +0000825 found_blank_loc = cur_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000826 } else if (found_blank)
David Lawrence Ramseybdc1b9b2005-09-20 16:36:08 +0000827 return found_blank_loc;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000828
829 line += line_len;
830 cur_loc += line_len;
831 }
832
833 return -1;
834 }
835
836 /* Move to the last blank after blank_loc, if there is one. */
837 line -= cur_loc;
838 line += blank_loc;
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000839 line_len = parse_mbchar(line, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000840 line += line_len;
841
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +0000842 while (*line != '\0' && (is_blank_mbchar(line)
843#ifndef DISABLE_HELP
844 || (newline && *line == '\n')
845#endif
846 )) {
David Lawrence Ramseycd243f52006-05-19 23:27:16 +0000847#ifndef DISABLE_HELP
848 if (newline && *line == '\n')
849 break;
850#endif
851
David Lawrence Ramsey39bd1b32006-05-20 13:11:56 +0000852 line_len = parse_mbchar(line, NULL, NULL);
853
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000854 line += line_len;
855 blank_loc += line_len;
856 }
857
858 return blank_loc;
859}
David Lawrence Ramseyc7c04bb2005-11-29 21:30:00 +0000860#endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000861
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000862#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000863/* The "indentation" of a line is the whitespace between the quote part
864 * and the non-whitespace of the line. */
865size_t indent_length(const char *line)
866{
867 size_t len = 0;
868 char *blank_mb;
869 int blank_mb_len;
870
871 assert(line != NULL);
872
873 blank_mb = charalloc(mb_cur_max());
874
875 while (*line != '\0') {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000876 blank_mb_len = parse_mbchar(line, blank_mb, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000877
878 if (!is_blank_mbchar(blank_mb))
879 break;
880
881 line += blank_mb_len;
882 len += blank_mb_len;
883 }
884
885 free(blank_mb);
886
887 return len;
888}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000889#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000890
891#ifndef DISABLE_JUSTIFY
892/* justify_format() replaces blanks with spaces and multiple spaces by 1
893 * (except it maintains up to 2 after a character in punct optionally
894 * followed by a character in brackets, and removes all from the end).
895 *
896 * justify_format() might make paragraph->data shorter, and change the
897 * actual pointer with null_at().
898 *
899 * justify_format() will not look at the first skip characters of
900 * paragraph. skip should be at most strlen(paragraph->data). The
901 * character at paragraph[skip + 1] must not be blank. */
902void justify_format(filestruct *paragraph, size_t skip)
903{
904 char *end, *new_end, *new_paragraph_data;
905 size_t shift = 0;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000906#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000907 size_t mark_shift = 0;
908#endif
909
910 /* These four asserts are assumptions about the input data. */
911 assert(paragraph != NULL);
912 assert(paragraph->data != NULL);
913 assert(skip < strlen(paragraph->data));
914 assert(!is_blank_mbchar(paragraph->data + skip));
915
916 end = paragraph->data + skip;
917 new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
918 strncpy(new_paragraph_data, paragraph->data, skip);
919 new_end = new_paragraph_data + skip;
920
921 while (*end != '\0') {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000922 int end_len;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000923
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000924 /* If this character is blank, change it to a space if
925 * necessary, and skip over all blanks after it. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000926 if (is_blank_mbchar(end)) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000927 end_len = parse_mbchar(end, NULL, NULL);
928
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000929 *new_end = ' ';
930 new_end++;
931 end += end_len;
932
933 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000934 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000935
936 end += end_len;
937 shift += end_len;
938
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000939#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000940 /* Keep track of the change in the current line. */
941 if (openfile->mark_set && openfile->mark_begin ==
942 paragraph && openfile->mark_begin_x >= end -
943 paragraph->data)
944 mark_shift += end_len;
945#endif
946 }
947 /* If this character is punctuation optionally followed by a
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000948 * bracket and then followed by blanks, change no more than two
949 * of the blanks to spaces if necessary, and skip over all
950 * blanks after them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000951 } else if (mbstrchr(punct, end) != NULL) {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +0000952 end_len = parse_mbchar(end, NULL, NULL);
953
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000954 while (end_len > 0) {
955 *new_end = *end;
956 new_end++;
957 end++;
958 end_len--;
959 }
960
961 if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000962 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000963
964 while (end_len > 0) {
965 *new_end = *end;
966 new_end++;
967 end++;
968 end_len--;
969 }
970 }
971
972 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000973 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000974
975 *new_end = ' ';
976 new_end++;
977 end += end_len;
978 }
979
980 if (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000981 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000982
983 *new_end = ' ';
984 new_end++;
985 end += end_len;
986 }
987
988 while (*end != '\0' && is_blank_mbchar(end)) {
David Lawrence Ramsey96452cb2005-07-26 06:13:45 +0000989 end_len = parse_mbchar(end, NULL, NULL);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000990
991 end += end_len;
992 shift += end_len;
993
David Lawrence Ramseyebe34252005-11-15 03:17:35 +0000994#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +0000995 /* Keep track of the change in the current line. */
996 if (openfile->mark_set && openfile->mark_begin ==
997 paragraph && openfile->mark_begin_x >= end -
998 paragraph->data)
999 mark_shift += end_len;
1000#endif
1001 }
1002 /* If this character is neither blank nor punctuation, leave it
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001003 * unchanged. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001004 } else {
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001005 end_len = parse_mbchar(end, NULL, NULL);
1006
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001007 while (end_len > 0) {
1008 *new_end = *end;
1009 new_end++;
1010 end++;
1011 end_len--;
1012 }
1013 }
1014 }
1015
1016 assert(*end == '\0');
1017
1018 *new_end = *end;
1019
David Lawrence Ramsey30bdadd2005-12-31 21:08:10 +00001020 /* If there are spaces at the end of the line, remove them. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001021 while (new_end > new_paragraph_data + skip &&
1022 *(new_end - 1) == ' ') {
1023 new_end--;
1024 shift++;
1025 }
1026
1027 if (shift > 0) {
1028 openfile->totsize -= shift;
1029 null_at(&new_paragraph_data, new_end - new_paragraph_data);
1030 free(paragraph->data);
1031 paragraph->data = new_paragraph_data;
1032
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001033#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001034 /* Adjust the mark coordinates to compensate for the change in
1035 * the current line. */
1036 if (openfile->mark_set && openfile->mark_begin == paragraph) {
1037 openfile->mark_begin_x -= mark_shift;
1038 if (openfile->mark_begin_x > new_end - new_paragraph_data)
1039 openfile->mark_begin_x = new_end - new_paragraph_data;
1040 }
1041#endif
1042 } else
1043 free(new_paragraph_data);
1044}
1045
1046/* The "quote part" of a line is the largest initial substring matching
1047 * the quote string. This function returns the length of the quote part
1048 * of the given line.
1049 *
1050 * Note that if !HAVE_REGEX_H then we match concatenated copies of
1051 * quotestr. */
1052size_t quote_length(const char *line)
1053{
1054#ifdef HAVE_REGEX_H
1055 regmatch_t matches;
1056 int rc = regexec(&quotereg, line, 1, &matches, 0);
1057
1058 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
1059 return 0;
1060 /* matches.rm_so should be 0, since the quote string should start
1061 * with the caret ^. */
1062 return matches.rm_eo;
1063#else /* !HAVE_REGEX_H */
1064 size_t qdepth = 0;
1065
1066 /* Compute quote depth level. */
1067 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
1068 qdepth += quotelen;
1069 return qdepth;
1070#endif /* !HAVE_REGEX_H */
1071}
1072
1073/* a_line and b_line are lines of text. The quotation part of a_line is
1074 * the first a_quote characters. Check that the quotation part of
1075 * b_line is the same. */
1076bool quotes_match(const char *a_line, size_t a_quote, const char
1077 *b_line)
1078{
1079 /* Here is the assumption about a_quote. */
1080 assert(a_quote == quote_length(a_line));
1081
1082 return (a_quote == quote_length(b_line) &&
1083 strncmp(a_line, b_line, a_quote) == 0);
1084}
1085
1086/* We assume a_line and b_line have no quote part. Then, we return
1087 * whether b_line could follow a_line in a paragraph. */
1088bool indents_match(const char *a_line, size_t a_indent, const char
1089 *b_line, size_t b_indent)
1090{
1091 assert(a_indent == indent_length(a_line));
1092 assert(b_indent == indent_length(b_line));
1093
1094 return (b_indent <= a_indent &&
1095 strncmp(a_line, b_line, b_indent) == 0);
1096}
1097
1098/* Is foo the beginning of a paragraph?
1099 *
1100 * A line of text consists of a "quote part", followed by an
1101 * "indentation part", followed by text. The functions quote_length()
1102 * and indent_length() calculate these parts.
1103 *
1104 * A line is "part of a paragraph" if it has a part not in the quote
1105 * part or the indentation.
1106 *
1107 * A line is "the beginning of a paragraph" if it is part of a
1108 * paragraph and
1109 * 1) it is the top line of the file, or
1110 * 2) the line above it is not part of a paragraph, or
1111 * 3) the line above it does not have precisely the same quote
1112 * part, or
1113 * 4) the indentation of this line is not an initial substring of
1114 * the indentation of the previous line, or
1115 * 5) this line has no quote part and some indentation, and
1116 * autoindent isn't turned on.
1117 * The reason for number 5) is that if autoindent isn't turned on,
1118 * then an indented line is expected to start a paragraph, as in
1119 * books. Thus, nano can justify an indented paragraph only if
1120 * autoindent is turned on. */
1121bool begpar(const filestruct *const foo)
1122{
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001123 size_t quote_len, indent_len, temp_id_len;
1124
1125 if (foo == NULL)
1126 return FALSE;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001127
1128 /* Case 1). */
David Lawrence Ramsey0083bd22005-11-09 18:26:44 +00001129 if (foo == openfile->fileage)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001130 return TRUE;
1131
1132 quote_len = quote_length(foo->data);
1133 indent_len = indent_length(foo->data + quote_len);
1134
1135 /* Not part of a paragraph. */
1136 if (foo->data[quote_len + indent_len] == '\0')
1137 return FALSE;
1138
1139 /* Case 3). */
1140 if (!quotes_match(foo->data, quote_len, foo->prev->data))
1141 return TRUE;
1142
1143 temp_id_len = indent_length(foo->prev->data + quote_len);
1144
1145 /* Case 2) or 5) or 4). */
1146 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
1147 (quote_len == 0 && indent_len > 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001148#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001149 && !ISSET(AUTOINDENT)
1150#endif
1151 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
1152 foo->data + quote_len, indent_len))
1153 return TRUE;
1154
1155 return FALSE;
1156}
1157
1158/* Is foo inside a paragraph? */
1159bool inpar(const filestruct *const foo)
1160{
1161 size_t quote_len;
1162
1163 if (foo == NULL)
1164 return FALSE;
1165
1166 quote_len = quote_length(foo->data);
1167
David Lawrence Ramsey21014032005-11-09 20:33:42 +00001168 return (foo->data[quote_len + indent_length(foo->data +
1169 quote_len)] != '\0');
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001170}
1171
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001172/* Move the next par_len lines, starting with first_line, into the
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001173 * justify buffer, leaving copies of those lines in place. Assume that
1174 * par_len is greater than zero, and that there are enough lines after
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001175 * first_line. */
1176void backup_lines(filestruct *first_line, size_t par_len)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001177{
1178 filestruct *top = first_line;
1179 /* The top of the paragraph we're backing up. */
1180 filestruct *bot = first_line;
1181 /* The bottom of the paragraph we're backing up. */
1182 size_t i;
1183 /* Generic loop variable. */
1184 size_t current_x_save = openfile->current_x;
1185 ssize_t fl_lineno_save = first_line->lineno;
1186 ssize_t edittop_lineno_save = openfile->edittop->lineno;
1187 ssize_t current_lineno_save = openfile->current->lineno;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001188#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001189 bool old_mark_set = openfile->mark_set;
1190 ssize_t mb_lineno_save = 0;
1191 size_t mark_begin_x_save = 0;
1192
1193 if (old_mark_set) {
1194 mb_lineno_save = openfile->mark_begin->lineno;
1195 mark_begin_x_save = openfile->mark_begin_x;
1196 }
1197#endif
1198
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001199 /* par_len will be one greater than the number of lines between
1200 * current and filebot if filebot is the last line in the
1201 * paragraph. */
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001202 assert(par_len > 0 && openfile->current->lineno + par_len <=
David Lawrence Ramsey24777c02005-12-01 05:49:08 +00001203 openfile->filebot->lineno + 1);
David Lawrence Ramsey8bd960b2005-11-09 18:49:16 +00001204
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001205 /* Move bot down par_len lines to the line after the last line of
1206 * the paragraph, if there is one. */
1207 for (i = par_len; i > 0 && bot != openfile->filebot; i--)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001208 bot = bot->next;
1209
1210 /* Move the paragraph from the current buffer's filestruct to the
1211 * justify buffer. */
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001212 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
David Lawrence Ramseyf0575cf2005-11-09 23:27:51 +00001213 (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001214
1215 /* Copy the paragraph back to the current buffer's filestruct from
1216 * the justify buffer. */
1217 copy_from_filestruct(jusbuffer, jusbottom);
1218
1219 /* Move upward from the last line of the paragraph to the first
1220 * line, putting first_line, edittop, current, and mark_begin at the
1221 * same lines in the copied paragraph that they had in the original
1222 * paragraph. */
David Lawrence Ramsey5d6f1272005-11-10 03:32:59 +00001223 if (openfile->current != openfile->fileage)
David Lawrence Ramsey5c33e882005-11-09 18:58:04 +00001224 top = openfile->current->prev;
1225 else
1226 top = openfile->current;
David Lawrence Ramseye8d505b2005-11-10 03:40:45 +00001227 for (i = par_len; i > 0 && top != NULL; i--) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001228 if (top->lineno == fl_lineno_save)
1229 first_line = top;
1230 if (top->lineno == edittop_lineno_save)
1231 openfile->edittop = top;
1232 if (top->lineno == current_lineno_save)
1233 openfile->current = top;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001234#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001235 if (old_mark_set && top->lineno == mb_lineno_save) {
1236 openfile->mark_begin = top;
1237 openfile->mark_begin_x = mark_begin_x_save;
1238 }
1239#endif
1240 top = top->prev;
1241 }
1242
1243 /* Put current_x at the same place in the copied paragraph that it
1244 * had in the original paragraph. */
1245 openfile->current_x = current_x_save;
1246
1247 set_modified();
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001248}
1249
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001250/* Find the beginning of the current paragraph if we're in one, or the
1251 * beginning of the next paragraph if we're not. Afterwards, save the
1252 * quote length and paragraph length in *quote and *par. Return TRUE if
David Lawrence Ramsey139fa652006-05-22 01:26:24 +00001253 * we found a paragraph, and FALSE if there was an error or we didn't
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001254 * find a paragraph.
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001255 *
1256 * See the comment at begpar() for more about when a line is the
1257 * beginning of a paragraph. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001258bool find_paragraph(size_t *const quote, size_t *const par)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001259{
1260 size_t quote_len;
1261 /* Length of the initial quotation of the paragraph we search
1262 * for. */
1263 size_t par_len;
1264 /* Number of lines in the paragraph we search for. */
1265 filestruct *current_save;
1266 /* The line at the beginning of the paragraph we search for. */
1267 ssize_t current_y_save;
1268 /* The y-coordinate at the beginning of the paragraph we search
1269 * for. */
1270
1271#ifdef HAVE_REGEX_H
1272 if (quoterc != 0) {
1273 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
1274 return FALSE;
1275 }
1276#endif
1277
1278 assert(openfile->current != NULL);
1279
David Lawrence Ramsey1be131a2005-11-11 03:55:52 +00001280 /* If we're at the end of the last line of the file, it means that
1281 * there aren't any paragraphs left, so get out. */
1282 if (openfile->current == openfile->filebot && openfile->current_x ==
1283 strlen(openfile->filebot->data))
1284 return FALSE;
1285
1286 /* If the current line isn't in a paragraph, move forward to the
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001287 * last line of the next paragraph, if any. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001288 if (!inpar(openfile->current)) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001289 do_para_end(FALSE);
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001290
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001291 /* If we end up past the beginning of the line, it means that
1292 * we're at the end of the last line of the file, and the line
1293 * isn't blank, in which case the last line of the file is the
1294 * last line of the next paragraph.
1295 *
1296 * Otherwise, if we end up on a line that's in a paragraph, it
1297 * means that we're on the line after the last line of the next
1298 * paragraph, in which case we should move back to the last line
1299 * of the next paragraph. */
1300 if (openfile->current_x == 0) {
1301 if (!inpar(openfile->current->prev))
1302 return FALSE;
1303 if (openfile->current != openfile->fileage)
1304 openfile->current = openfile->current->prev;
1305 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001306 }
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001307
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001308 /* If the current line isn't the first line of the paragraph, move
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001309 * back to the first line of the paragraph. */
1310 if (!begpar(openfile->current))
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001311 do_para_begin(FALSE);
1312
1313 /* Now current is the first line of the paragraph. Set quote_len to
1314 * the quotation length of that line, and set par_len to the number
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001315 * of lines in this paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001316 quote_len = quote_length(openfile->current->data);
1317 current_save = openfile->current;
1318 current_y_save = openfile->current_y;
1319 do_para_end(FALSE);
1320 par_len = openfile->current->lineno - current_save->lineno;
David Lawrence Ramsey9a065c02005-11-29 18:25:53 +00001321
David Lawrence Ramseyd82dae02005-11-11 05:13:28 +00001322 /* If we end up past the beginning of the line, it means that we're
1323 * at the end of the last line of the file, and the line isn't
1324 * blank, in which case the last line of the file is part of the
1325 * paragraph. */
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001326 if (openfile->current_x > 0)
1327 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001328 openfile->current = current_save;
1329 openfile->current_y = current_y_save;
1330
1331 /* Save the values of quote_len and par_len. */
1332 assert(quote != NULL && par != NULL);
1333
1334 *quote = quote_len;
1335 *par = par_len;
1336
1337 return TRUE;
1338}
1339
1340/* If full_justify is TRUE, justify the entire file. Otherwise, justify
1341 * the current paragraph. */
1342void do_justify(bool full_justify)
1343{
1344 filestruct *first_par_line = NULL;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001345 /* Will be the first line of the justified paragraph(s), if any.
1346 * For restoring after unjustify. */
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001347 filestruct *last_par_line = NULL;
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001348 /* Will be the line after the last line of the justified
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001349 * paragraph(s), if any. Also for restoring after unjustify. */
David Lawrence Ramsey82b5deb2005-11-10 06:07:57 +00001350 bool filebot_inpar = FALSE;
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001351 /* Whether the text at filebot is part of the current
1352 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001353
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00001354 /* We save these variables to be restored if the user
1355 * unjustifies. */
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001356 filestruct *edittop_save = openfile->edittop;
1357 filestruct *current_save = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001358 size_t current_x_save = openfile->current_x;
1359 size_t pww_save = openfile->placewewant;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001360 size_t totsize_save = openfile->totsize;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001361#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001362 filestruct *mark_begin_save = openfile->mark_begin;
1363 size_t mark_begin_x_save = openfile->mark_begin_x;
1364#endif
David Lawrence Ramsey52161ee2005-11-10 19:56:26 +00001365 bool modified_save = openfile->modified;
1366
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001367 int kbinput;
1368 bool meta_key, func_key, s_or_t, ran_func, finished;
1369
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001370 /* Move to the beginning of the current line, so that justifying at
David Lawrence Ramseybdff6652005-11-11 04:14:33 +00001371 * the end of the last line of the file, if that line isn't blank,
1372 * will work the first time through. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001373 openfile->current_x = 0;
1374
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001375 /* If we're justifying the entire file, start at the beginning. */
1376 if (full_justify)
1377 openfile->current = openfile->fileage;
1378
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001379 while (TRUE) {
1380 size_t i;
1381 /* Generic loop variable. */
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001382 filestruct *curr_first_par_line;
1383 /* The first line of the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001384 size_t quote_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001385 /* Length of the initial quotation of the current
1386 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001387 size_t indent_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001388 /* Length of the initial indentation of the current
1389 * paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001390 size_t par_len;
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001391 /* Number of lines in the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001392 ssize_t break_pos;
1393 /* Where we will break lines. */
1394 char *indent_string;
1395 /* The first indentation that doesn't match the initial
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001396 * indentation of the current paragraph. This is put at the
1397 * beginning of every line broken off the first justified
1398 * line of the paragraph. Note that this works because a
1399 * paragraph can only contain two indentations at most: the
1400 * initial one, and a different one starting on a line after
1401 * the first. See the comment at begpar() for more about
1402 * when a line is part of a paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001403
1404 /* Find the first line of the paragraph to be justified. That
1405 * is the start of this paragraph if we're in one, or the start
1406 * of the next otherwise. Save the quote length and paragraph
1407 * length (number of lines). Don't refresh the screen yet,
1408 * since we'll do that after we justify.
1409 *
1410 * If the search failed, we do one of two things. If we're
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001411 * justifying the whole file, and we've found at least one
1412 * paragraph, it means that we should justify all the way to the
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001413 * last line of the file, so set the last line of the text to be
1414 * justified to the last line of the file and break out of the
1415 * loop. Otherwise, it means that there are no paragraph(s) to
1416 * justify, so refresh the screen and get out. */
David Lawrence Ramsey79383be2005-11-29 18:34:45 +00001417 if (!find_paragraph(&quote_len, &par_len)) {
David Lawrence Ramsey8b203d62005-11-11 03:17:44 +00001418 if (full_justify && first_par_line != NULL) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001419 last_par_line = openfile->filebot;
1420 break;
1421 } else {
1422 edit_refresh();
1423 return;
1424 }
1425 }
1426
David Lawrence Ramseyb2d1c5f2005-11-10 06:01:41 +00001427 /* par_len will be one greater than the number of lines between
1428 * current and filebot if filebot is the last line in the
1429 * paragraph. Set filebot_inpar to TRUE if this is the case. */
1430 filebot_inpar = (openfile->current->lineno + par_len ==
1431 openfile->filebot->lineno + 1);
1432
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001433 /* If we haven't already done it, move the original paragraph(s)
1434 * to the justify buffer, splice a copy of the original
1435 * paragraph(s) into the file in the same place, and set
1436 * first_par_line to the first line of the copy. */
1437 if (first_par_line == NULL) {
1438 backup_lines(openfile->current, full_justify ?
David Lawrence Ramsey53f641f2005-11-10 21:57:56 +00001439 openfile->filebot->lineno - openfile->current->lineno +
1440 ((openfile->filebot->data[0] != '\0') ? 1 : 0) :
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001441 par_len);
David Lawrence Ramseycd8f7352005-11-10 21:20:32 +00001442 first_par_line = openfile->current;
1443 }
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001444
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001445 /* Set curr_first_par_line to the first line of the current
1446 * paragraph. */
1447 curr_first_par_line = openfile->current;
1448
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001449 /* Initialize indent_string to a blank string. */
1450 indent_string = mallocstrcpy(NULL, "");
1451
1452 /* Find the first indentation in the paragraph that doesn't
1453 * match the indentation of the first line, and save it in
1454 * indent_string. If all the indentations are the same, save
1455 * the indentation of the first line in indent_string. */
1456 {
1457 const filestruct *indent_line = openfile->current;
1458 bool past_first_line = FALSE;
1459
1460 for (i = 0; i < par_len; i++) {
1461 indent_len = quote_len +
1462 indent_length(indent_line->data + quote_len);
1463
1464 if (indent_len != strlen(indent_string)) {
1465 indent_string = mallocstrncpy(indent_string,
1466 indent_line->data, indent_len + 1);
1467 indent_string[indent_len] = '\0';
1468
1469 if (past_first_line)
1470 break;
1471 }
1472
1473 if (indent_line == openfile->current)
1474 past_first_line = TRUE;
1475
1476 indent_line = indent_line->next;
1477 }
1478 }
1479
1480 /* Now tack all the lines of the paragraph together, skipping
1481 * the quoting and indentation on all lines after the first. */
1482 for (i = 0; i < par_len - 1; i++) {
1483 filestruct *next_line = openfile->current->next;
1484 size_t line_len = strlen(openfile->current->data);
1485 size_t next_line_len =
1486 strlen(openfile->current->next->data);
1487
1488 indent_len = quote_len +
1489 indent_length(openfile->current->next->data +
1490 quote_len);
1491
1492 next_line_len -= indent_len;
1493 openfile->totsize -= indent_len;
1494
1495 /* We're just about to tack the next line onto this one. If
1496 * this line isn't empty, make sure it ends in a space. */
1497 if (line_len > 0 &&
1498 openfile->current->data[line_len - 1] != ' ') {
1499 line_len++;
1500 openfile->current->data =
1501 charealloc(openfile->current->data,
1502 line_len + 1);
1503 openfile->current->data[line_len - 1] = ' ';
1504 openfile->current->data[line_len] = '\0';
1505 openfile->totsize++;
1506 }
1507
1508 openfile->current->data =
1509 charealloc(openfile->current->data, line_len +
1510 next_line_len + 1);
1511 strcat(openfile->current->data, next_line->data +
1512 indent_len);
1513
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00001514 /* Don't destroy edittop or filebot! */
David Lawrence Ramsey32bd29e2005-11-09 03:44:23 +00001515 if (next_line == openfile->edittop)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001516 openfile->edittop = openfile->current;
David Lawrence Ramsey9bedc4b2005-11-09 19:51:48 +00001517 if (next_line == openfile->filebot)
1518 openfile->filebot = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001519
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001520#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001521 /* Adjust the mark coordinates to compensate for the change
1522 * in the next line. */
1523 if (openfile->mark_set && openfile->mark_begin ==
1524 next_line) {
1525 openfile->mark_begin = openfile->current;
1526 openfile->mark_begin_x += line_len - indent_len;
1527 }
1528#endif
1529
1530 unlink_node(next_line);
1531 delete_node(next_line);
1532
1533 /* If we've removed the next line, we need to go through
1534 * this line again. */
1535 i--;
1536
1537 par_len--;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001538 openfile->totsize--;
1539 }
1540
1541 /* Call justify_format() on the paragraph, which will remove
1542 * excess spaces from it and change all blank characters to
1543 * spaces. */
1544 justify_format(openfile->current, quote_len +
1545 indent_length(openfile->current->data + quote_len));
1546
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001547 while (par_len > 0 && strlenpt(openfile->current->data) >
1548 fill) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001549 size_t line_len = strlen(openfile->current->data);
1550
1551 indent_len = strlen(indent_string);
1552
1553 /* If this line is too long, try to wrap it to the next line
1554 * to make it short enough. */
David Lawrence Ramsey3f12ada2005-07-25 22:54:16 +00001555 break_pos = break_line(openfile->current->data + indent_len,
David Lawrence Ramseyb9b2fd52005-11-22 21:13:36 +00001556 fill - strnlenpt(openfile->current->data, indent_len)
1557#ifndef DISABLE_HELP
1558 , FALSE
1559#endif
1560 );
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001561
1562 /* We can't break the line, or don't need to, so get out. */
1563 if (break_pos == -1 || break_pos + indent_len == line_len)
1564 break;
1565
1566 /* Move forward to the character after the indentation and
1567 * just after the space. */
1568 break_pos += indent_len + 1;
1569
1570 assert(break_pos <= line_len);
1571
1572 /* Make a new line, and copy the text after where we're
1573 * going to break this line to the beginning of the new
1574 * line. */
1575 splice_node(openfile->current,
1576 make_new_node(openfile->current),
1577 openfile->current->next);
1578
1579 /* If this paragraph is non-quoted, and autoindent isn't
1580 * turned on, set the indentation length to zero so that the
1581 * indentation is treated as part of the line. */
1582 if (quote_len == 0
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001583#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001584 && !ISSET(AUTOINDENT)
1585#endif
1586 )
1587 indent_len = 0;
1588
1589 /* Copy the text after where we're going to break the
1590 * current line to the next line. */
1591 openfile->current->next->data = charalloc(indent_len + 1 +
1592 line_len - break_pos);
1593 strncpy(openfile->current->next->data, indent_string,
1594 indent_len);
1595 strcpy(openfile->current->next->data + indent_len,
1596 openfile->current->data + break_pos);
1597
1598 par_len++;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001599 openfile->totsize += indent_len + 1;
1600
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001601#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001602 /* Adjust the mark coordinates to compensate for the change
1603 * in the current line. */
1604 if (openfile->mark_set && openfile->mark_begin ==
1605 openfile->current && openfile->mark_begin_x >
1606 break_pos) {
1607 openfile->mark_begin = openfile->current->next;
1608 openfile->mark_begin_x -= break_pos - indent_len;
1609 }
1610#endif
1611
1612 /* Break the current line. */
1613 null_at(&openfile->current->data, break_pos);
1614
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001615 /* If the current line is the last line of the file, move
David Lawrence Ramseyb885c9c2005-11-10 05:20:25 +00001616 * the last line of the file down to the next line. */
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001617 if (openfile->filebot == openfile->current)
1618 openfile->filebot = openfile->filebot->next;
1619
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001620 /* Go to the next line. */
1621 par_len--;
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001622 openfile->current_y++;
1623 openfile->current = openfile->current->next;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001624 }
1625
1626 /* We're done breaking lines, so we don't need indent_string
1627 * anymore. */
1628 free(indent_string);
1629
David Lawrence Ramsey5455b6a2005-11-09 20:17:12 +00001630 /* Go to the next line, if possible. If there is no next line,
1631 * move to the end of the current line. */
David Lawrence Ramsey2c5d0ec2005-11-09 19:06:01 +00001632 if (openfile->current != openfile->filebot) {
1633 openfile->current_y++;
1634 openfile->current = openfile->current->next;
1635 } else
1636 openfile->current_x = strlen(openfile->current->data);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001637
David Lawrence Ramseya6854682005-11-30 21:19:42 +00001638 /* Renumber the lines of the now-justified current paragraph,
1639 * since both find_paragraph() and edit_refresh() need the line
1640 * numbers to be right. */
1641 renumber(curr_first_par_line);
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00001642
1643 /* We've just finished justifying the paragraph. If we're not
1644 * justifying the entire file, break out of the loop.
1645 * Otherwise, continue the loop so that we justify all the
1646 * paragraphs in the file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001647 if (!full_justify)
1648 break;
1649 }
1650
1651 /* We are now done justifying the paragraph or the file, so clean
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001652 * up. current_y and totsize have been maintained above. If we
David Lawrence Ramseyad1b64c2005-11-29 19:00:09 +00001653 * actually justified something, set last_par_line to the new end of
1654 * the paragraph. */
1655 if (first_par_line != NULL)
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001656 last_par_line = openfile->current;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001657
1658 edit_refresh();
1659
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00001660#ifndef NANO_TINY
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00001661 /* We're going to set jump_buf so that we return here after a
1662 * SIGWINCH instead of to main(). Indicate this. */
1663 jump_buf_main = FALSE;
1664
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00001665 /* Return here after a SIGWINCH. */
David Lawrence Ramsey1c5af642006-05-10 15:15:06 +00001666 sigsetjmp(jump_buf, 1);
David Lawrence Ramseyfc0f8f82006-05-10 13:41:53 +00001667#endif
1668
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001669 statusbar(_("Can now UnJustify!"));
1670
1671 /* If constant cursor position display is on, make sure the current
1672 * cursor position will be properly displayed on the statusbar. */
1673 if (ISSET(CONST_UPDATE))
1674 do_cursorpos(TRUE);
1675
1676 /* Display the shortcut list with UnJustify. */
1677 shortcut_init(TRUE);
1678 display_main_list();
1679
1680 /* Now get a keystroke and see if it's unjustify. If not, put back
1681 * the keystroke and return. */
1682 kbinput = do_input(&meta_key, &func_key, &s_or_t, &ran_func,
1683 &finished, FALSE);
1684
David Lawrence Ramseyb22c80a2006-05-06 13:41:59 +00001685 if (s_or_t && kbinput == NANO_UNJUSTIFY_KEY) {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001686 /* Splice the justify buffer back into the file, but only if we
1687 * actually justified something. */
1688 if (first_par_line != NULL) {
1689 filestruct *top_save;
1690
1691 /* Partition the filestruct so that it contains only the
1692 * text of the justified paragraph. */
1693 filepart = partition_filestruct(first_par_line, 0,
David Lawrence Ramseyccd1b7b2005-11-18 20:21:48 +00001694 last_par_line, filebot_inpar ?
1695 strlen(last_par_line->data) : 0);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001696
1697 /* Remove the text of the justified paragraph, and
David Lawrence Ramseyaf5a9992005-11-09 23:06:44 +00001698 * replace it with the text in the justify buffer. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001699 free_filestruct(openfile->fileage);
1700 openfile->fileage = jusbuffer;
1701 openfile->filebot = jusbottom;
1702
1703 top_save = openfile->fileage;
1704
1705 /* Unpartition the filestruct so that it contains all the
1706 * text again. Note that the justified paragraph has been
1707 * replaced with the unjustified paragraph. */
1708 unpartition_filestruct(&filepart);
1709
1710 /* Renumber starting with the beginning line of the old
1711 * partition. */
1712 renumber(top_save);
1713
David Lawrence Ramsey874ec8f2005-11-10 19:28:27 +00001714 /* Restore the justify we just did (ungrateful user!). */
1715 openfile->edittop = edittop_save;
1716 openfile->current = current_save;
1717 openfile->current_x = current_x_save;
1718 openfile->placewewant = pww_save;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001719 openfile->totsize = totsize_save;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001720#ifndef NANO_TINY
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001721 if (openfile->mark_set) {
1722 openfile->mark_begin = mark_begin_save;
1723 openfile->mark_begin_x = mark_begin_x_save;
1724 }
1725#endif
1726 openfile->modified = modified_save;
1727
1728 /* Clear the justify buffer. */
1729 jusbuffer = NULL;
1730
1731 if (!openfile->modified)
1732 titlebar(NULL);
1733 edit_refresh();
1734 }
1735 } else {
1736 unget_kbinput(kbinput, meta_key, func_key);
1737
1738 /* Blow away the text in the justify buffer. */
1739 free_filestruct(jusbuffer);
1740 jusbuffer = NULL;
1741 }
1742
1743 blank_statusbar();
1744
1745 /* Display the shortcut list with UnCut. */
1746 shortcut_init(FALSE);
1747 display_main_list();
1748}
1749
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001750/* Justify the current paragraph. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001751void do_justify_void(void)
1752{
1753 do_justify(FALSE);
1754}
1755
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001756/* Justify the entire file. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00001757void do_full_justify(void)
1758{
1759 do_justify(TRUE);
1760}
1761#endif /* !DISABLE_JUSTIFY */
1762
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001763#ifndef DISABLE_SPELLER
1764/* A word is misspelled in the file. Let the user replace it. We
1765 * return FALSE if the user cancels. */
1766bool do_int_spell_fix(const char *word)
1767{
1768 char *save_search, *save_replace;
1769 size_t match_len, current_x_save = openfile->current_x;
1770 size_t pww_save = openfile->placewewant;
1771 filestruct *edittop_save = openfile->edittop;
1772 filestruct *current_save = openfile->current;
1773 /* Save where we are. */
1774 bool canceled = FALSE;
1775 /* The return value. */
1776 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001777#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001778 bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
1779#endif
1780#ifdef HAVE_REGEX_H
1781 bool regexp_set = ISSET(USE_REGEXP);
1782#endif
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001783#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001784 bool old_mark_set = openfile->mark_set;
1785 bool added_magicline = FALSE;
1786 /* Whether we added a magicline after filebot. */
1787 bool right_side_up = FALSE;
1788 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
1789 * FALSE if (current, current_x) is. */
1790 filestruct *top, *bot;
1791 size_t top_x, bot_x;
1792#endif
1793
1794 /* Make sure spell-check is case sensitive. */
1795 SET(CASE_SENSITIVE);
1796
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001797#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001798 /* Make sure spell-check goes forward only. */
1799 UNSET(BACKWARDS_SEARCH);
1800#endif
1801#ifdef HAVE_REGEX_H
1802 /* Make sure spell-check doesn't use regular expressions. */
1803 UNSET(USE_REGEXP);
1804#endif
1805
1806 /* Save the current search/replace strings. */
1807 search_init_globals();
1808 save_search = last_search;
1809 save_replace = last_replace;
1810
1811 /* Set the search/replace strings to the misspelled word. */
1812 last_search = mallocstrcpy(NULL, word);
1813 last_replace = mallocstrcpy(NULL, word);
1814
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001815#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001816 if (old_mark_set) {
1817 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001818 * contains only the marked text; if the NO_NEWLINES flag isn't
1819 * set, keep track of whether the text will have a magicline
1820 * added when we're done correcting misspelled words; and
1821 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001822 mark_order((const filestruct **)&top, &top_x,
1823 (const filestruct **)&bot, &bot_x, &right_side_up);
1824 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001825 if (!ISSET(NO_NEWLINES))
1826 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001827 openfile->mark_set = FALSE;
1828 }
1829#endif
1830
1831 /* Start from the top of the file. */
1832 openfile->edittop = openfile->fileage;
1833 openfile->current = openfile->fileage;
1834 openfile->current_x = (size_t)-1;
1835 openfile->placewewant = 0;
1836
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001837 /* Find the first whole occurrence of word. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001838 findnextstr_wrap_reset();
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001839 while (findnextstr(TRUE, FALSE, openfile->fileage, 0, word,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001840 &match_len)) {
1841 if (is_whole_word(openfile->current_x, openfile->current->data,
1842 word)) {
1843 size_t xpt = xplustabs();
1844 char *exp_word = display_string(openfile->current->data,
1845 xpt, strnlenpt(openfile->current->data,
1846 openfile->current_x + match_len) - xpt, FALSE);
1847
1848 edit_refresh();
1849
1850 do_replace_highlight(TRUE, exp_word);
1851
1852 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00001853 canceled = (do_prompt(FALSE,
1854#ifndef DISABLE_TABCOMP
1855 TRUE,
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001856#endif
David Lawrence Ramsey9d8c2842006-02-07 21:11:05 +00001857 spell_list, word,
1858#ifndef NANO_TINY
1859 NULL,
1860#endif
David Lawrence Ramsey68160072006-02-18 21:32:29 +00001861 edit_refresh, _("Edit a replacement")) == -1);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001862
1863 do_replace_highlight(FALSE, exp_word);
1864
1865 free(exp_word);
1866
1867 if (!canceled && strcmp(word, answer) != 0) {
1868 openfile->current_x--;
David Lawrence Ramseyc5c52302005-11-15 23:45:29 +00001869 do_replace_loop(TRUE, &canceled, openfile->current,
1870 &openfile->current_x, word);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001871 }
1872
1873 break;
1874 }
1875 }
1876
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001877#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001878 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00001879 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
1880 * added a magicline, remove it now. */
1881 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001882 remove_magicline();
1883
1884 /* Put the beginning and the end of the mark at the beginning
1885 * and the end of the spell-checked text. */
1886 if (openfile->fileage == openfile->filebot)
1887 bot_x += top_x;
1888 if (right_side_up) {
1889 openfile->mark_begin_x = top_x;
1890 current_x_save = bot_x;
1891 } else {
1892 current_x_save = top_x;
1893 openfile->mark_begin_x = bot_x;
1894 }
1895
1896 /* Unpartition the filestruct so that it contains all the text
1897 * again, and turn the mark back on. */
1898 unpartition_filestruct(&filepart);
1899 openfile->mark_set = TRUE;
1900 }
1901#endif
1902
1903 /* Restore the search/replace strings. */
1904 free(last_search);
1905 last_search = save_search;
1906 free(last_replace);
1907 last_replace = save_replace;
1908
1909 /* Restore where we were. */
1910 openfile->edittop = edittop_save;
1911 openfile->current = current_save;
1912 openfile->current_x = current_x_save;
1913 openfile->placewewant = pww_save;
1914
1915 /* Restore case sensitivity setting. */
1916 if (!case_sens_set)
1917 UNSET(CASE_SENSITIVE);
1918
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00001919#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001920 /* Restore search/replace direction. */
1921 if (backwards_search_set)
1922 SET(BACKWARDS_SEARCH);
1923#endif
1924#ifdef HAVE_REGEX_H
1925 /* Restore regular expression usage setting. */
1926 if (regexp_set)
1927 SET(USE_REGEXP);
1928#endif
1929
1930 return !canceled;
1931}
1932
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00001933/* Internal (integrated) spell checking using the spell program,
1934 * filtered through the sort and uniq programs. Return NULL for normal
1935 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001936const char *do_int_speller(const char *tempfile_name)
1937{
1938 char *read_buff, *read_buff_ptr, *read_buff_word;
1939 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
1940 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
1941 pid_t pid_spell, pid_sort, pid_uniq;
1942 int spell_status, sort_status, uniq_status;
1943
1944 /* Create all three pipes up front. */
1945 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 ||
1946 pipe(uniq_fd) == -1)
1947 return _("Could not create pipe");
1948
1949 statusbar(_("Creating misspelled word list, please wait..."));
1950
1951 /* A new process to run spell in. */
1952 if ((pid_spell = fork()) == 0) {
1953 /* Child continues (i.e, future spell process). */
1954 close(spell_fd[0]);
1955
1956 /* Replace the standard input with the temp file. */
1957 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1958 goto close_pipes_and_exit;
1959
1960 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1961 goto close_pipes_and_exit;
1962
1963 close(tempfile_fd);
1964
1965 /* Send spell's standard output to the pipe. */
1966 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1967 goto close_pipes_and_exit;
1968
1969 close(spell_fd[1]);
1970
David Lawrence Ramsey3239ff22005-11-29 20:01:06 +00001971 /* Start the spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00001972 execlp("spell", "spell", NULL);
1973
1974 /* This should not be reached if spell is found. */
1975 exit(1);
1976 }
1977
1978 /* Parent continues here. */
1979 close(spell_fd[1]);
1980
1981 /* A new process to run sort in. */
1982 if ((pid_sort = fork()) == 0) {
1983 /* Child continues (i.e, future spell process). Replace the
1984 * standard input with the standard output of the old pipe. */
1985 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1986 goto close_pipes_and_exit;
1987
1988 close(spell_fd[0]);
1989
1990 /* Send sort's standard output to the new pipe. */
1991 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1992 goto close_pipes_and_exit;
1993
1994 close(sort_fd[1]);
1995
1996 /* Start the sort program. Use -f to remove mixed case. If
1997 * this isn't portable, let me know. */
1998 execlp("sort", "sort", "-f", NULL);
1999
2000 /* This should not be reached if sort is found. */
2001 exit(1);
2002 }
2003
2004 close(spell_fd[0]);
2005 close(sort_fd[1]);
2006
2007 /* A new process to run uniq in. */
2008 if ((pid_uniq = fork()) == 0) {
2009 /* Child continues (i.e, future uniq process). Replace the
2010 * standard input with the standard output of the old pipe. */
2011 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
2012 goto close_pipes_and_exit;
2013
2014 close(sort_fd[0]);
2015
2016 /* Send uniq's standard output to the new pipe. */
2017 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
2018 goto close_pipes_and_exit;
2019
2020 close(uniq_fd[1]);
2021
2022 /* Start the uniq program; we are using PATH. */
2023 execlp("uniq", "uniq", NULL);
2024
2025 /* This should not be reached if uniq is found. */
2026 exit(1);
2027 }
2028
2029 close(sort_fd[0]);
2030 close(uniq_fd[1]);
2031
2032 /* The child process was not forked successfully. */
2033 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
2034 close(uniq_fd[0]);
2035 return _("Could not fork");
2036 }
2037
2038 /* Get the system pipe buffer size. */
2039 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
2040 close(uniq_fd[0]);
2041 return _("Could not get size of pipe buffer");
2042 }
2043
2044 /* Read in the returned spelling errors. */
2045 read_buff_read = 0;
2046 read_buff_size = pipe_buff_size + 1;
2047 read_buff = read_buff_ptr = charalloc(read_buff_size);
2048
2049 while ((bytesread = read(uniq_fd[0], read_buff_ptr,
2050 pipe_buff_size)) > 0) {
2051 read_buff_read += bytesread;
2052 read_buff_size += pipe_buff_size;
2053 read_buff = read_buff_ptr = charealloc(read_buff,
2054 read_buff_size);
2055 read_buff_ptr += read_buff_read;
2056 }
2057
2058 *read_buff_ptr = '\0';
2059 close(uniq_fd[0]);
2060
2061 /* Process the spelling errors. */
2062 read_buff_word = read_buff_ptr = read_buff;
2063
2064 while (*read_buff_ptr != '\0') {
2065 if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
2066 *read_buff_ptr = '\0';
2067 if (read_buff_word != read_buff_ptr) {
2068 if (!do_int_spell_fix(read_buff_word)) {
2069 read_buff_word = read_buff_ptr;
2070 break;
2071 }
2072 }
2073 read_buff_word = read_buff_ptr + 1;
2074 }
2075 read_buff_ptr++;
2076 }
2077
2078 /* Special case: the last word doesn't end with '\r' or '\n'. */
2079 if (read_buff_word != read_buff_ptr)
2080 do_int_spell_fix(read_buff_word);
2081
2082 free(read_buff);
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002083 search_replace_abort();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002084 edit_refresh();
2085
2086 /* Process the end of the spell process. */
2087 waitpid(pid_spell, &spell_status, 0);
2088 waitpid(pid_sort, &sort_status, 0);
2089 waitpid(pid_uniq, &uniq_status, 0);
2090
2091 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
2092 return _("Error invoking \"spell\"");
2093
2094 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
2095 return _("Error invoking \"sort -f\"");
2096
2097 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
2098 return _("Error invoking \"uniq\"");
2099
2100 /* Otherwise... */
2101 return NULL;
2102
2103 close_pipes_and_exit:
2104 /* Don't leak any handles. */
2105 close(tempfile_fd);
2106 close(spell_fd[0]);
2107 close(spell_fd[1]);
2108 close(sort_fd[0]);
2109 close(sort_fd[1]);
2110 close(uniq_fd[0]);
2111 close(uniq_fd[1]);
2112 exit(1);
2113}
2114
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002115/* External (alternate) spell checking. Return NULL for normal
2116 * termination, and the error string otherwise. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002117const char *do_alt_speller(char *tempfile_name)
2118{
2119 int alt_spell_status;
2120 size_t current_x_save = openfile->current_x;
2121 size_t pww_save = openfile->placewewant;
2122 ssize_t current_y_save = openfile->current_y;
2123 ssize_t lineno_save = openfile->current->lineno;
2124 pid_t pid_spell;
2125 char *ptr;
2126 static int arglen = 3;
2127 static char **spellargs = NULL;
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002128#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002129 bool old_mark_set = openfile->mark_set;
2130 bool added_magicline = FALSE;
2131 /* Whether we added a magicline after filebot. */
2132 bool right_side_up = FALSE;
2133 /* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
2134 * FALSE if (current, current_x) is. */
2135 filestruct *top, *bot;
2136 size_t top_x, bot_x;
2137 ssize_t mb_lineno_save = 0;
2138 /* We're going to close the current file, and open the output of
2139 * the alternate spell command. The line that mark_begin points
2140 * to will be freed, so we save the line number and restore it
2141 * afterwards. */
2142 size_t totsize_save = openfile->totsize;
2143 /* Our saved value of totsize, used when we spell-check a marked
2144 * selection. */
2145
2146 if (old_mark_set) {
2147 /* If the mark is on, save the number of the line it starts on,
2148 * and then turn the mark off. */
2149 mb_lineno_save = openfile->mark_begin->lineno;
2150 openfile->mark_set = FALSE;
2151 }
2152#endif
2153
2154 endwin();
2155
2156 /* Set up an argument list to pass execvp(). */
2157 if (spellargs == NULL) {
2158 spellargs = (char **)nmalloc(arglen * sizeof(char *));
2159
2160 spellargs[0] = strtok(alt_speller, " ");
2161 while ((ptr = strtok(NULL, " ")) != NULL) {
2162 arglen++;
2163 spellargs = (char **)nrealloc(spellargs, arglen *
2164 sizeof(char *));
2165 spellargs[arglen - 3] = ptr;
2166 }
2167 spellargs[arglen - 1] = NULL;
2168 }
2169 spellargs[arglen - 2] = tempfile_name;
2170
2171 /* Start a new process for the alternate speller. */
2172 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey2e2112c2005-11-29 05:39:31 +00002173 /* Start alternate spell program; we are using $PATH. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002174 execvp(spellargs[0], spellargs);
2175
2176 /* Should not be reached, if alternate speller is found!!! */
2177 exit(1);
2178 }
2179
2180 /* If we couldn't fork, get out. */
2181 if (pid_spell < 0)
2182 return _("Could not fork");
2183
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002184#ifndef NANO_TINY
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002185 /* Don't handle a pending SIGWINCH until the alternate spell checker
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002186 * is finished and we've loaded the spell-checked file back in. */
David Lawrence Ramseyb18482e2005-07-26 00:06:34 +00002187 allow_pending_sigwinch(FALSE);
2188#endif
2189
2190 /* Wait for the alternate spell checker to finish. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002191 wait(&alt_spell_status);
2192
David Lawrence Ramsey84fdb902005-08-14 20:08:49 +00002193 /* Reenter curses mode. */
2194 doupdate();
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002195
2196 /* Restore the terminal to its previous state. */
2197 terminal_init();
2198
2199 /* Turn the cursor back on for sure. */
2200 curs_set(1);
2201
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002202 /* The screen might have been resized. If it has, reinitialize all
2203 * the windows based on the new screen dimensions. */
2204 window_init();
2205
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002206 if (!WIFEXITED(alt_spell_status) ||
2207 WEXITSTATUS(alt_spell_status) != 0) {
2208 char *altspell_error;
2209 char *invoke_error = _("Error invoking \"%s\"");
2210
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002211#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002212 /* Turn the mark back on if it was on before. */
2213 openfile->mark_set = old_mark_set;
2214#endif
2215
2216 altspell_error =
2217 charalloc(strlen(invoke_error) +
2218 strlen(alt_speller) + 1);
2219 sprintf(altspell_error, invoke_error, alt_speller);
2220 return altspell_error;
2221 }
2222
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002223#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002224 if (old_mark_set) {
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002225 /* If the mark is on, partition the filestruct so that it
2226 * contains only the marked text; if the NO_NEWLINES flag isn't
2227 * set, keep track of whether the text will have a magicline
2228 * added when we're done correcting misspelled words; and
2229 * turn the mark off. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002230 mark_order((const filestruct **)&top, &top_x,
2231 (const filestruct **)&bot, &bot_x, &right_side_up);
2232 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002233 if (!ISSET(NO_NEWLINES))
2234 added_magicline = (openfile->filebot->data[0] != '\0');
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002235
2236 /* Get the number of characters in the marked text, and subtract
2237 * it from the saved value of totsize. */
2238 totsize_save -= get_totsize(top, bot);
2239 }
2240#endif
2241
David Lawrence Ramsey9c984e82005-11-08 19:15:58 +00002242 /* Replace the text of the current buffer with the spell-checked
2243 * text. */
2244 replace_buffer(tempfile_name);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002245
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002246#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002247 if (old_mark_set) {
2248 filestruct *top_save = openfile->fileage;
2249
David Lawrence Ramsey1e0e2352005-11-08 18:34:12 +00002250 /* If the mark was on, the NO_NEWLINES flag isn't set, and we
2251 * added a magicline, remove it now. */
2252 if (!ISSET(NO_NEWLINES) && added_magicline)
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002253 remove_magicline();
2254
2255 /* Put the beginning and the end of the mark at the beginning
2256 * and the end of the spell-checked text. */
2257 if (openfile->fileage == openfile->filebot)
2258 bot_x += top_x;
2259 if (right_side_up) {
2260 openfile->mark_begin_x = top_x;
2261 current_x_save = bot_x;
2262 } else {
2263 current_x_save = top_x;
2264 openfile->mark_begin_x = bot_x;
2265 }
2266
2267 /* Unpartition the filestruct so that it contains all the text
2268 * again. Note that we've replaced the marked text originally
2269 * in the partition with the spell-checked marked text in the
2270 * temp file. */
2271 unpartition_filestruct(&filepart);
2272
2273 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002274 * partition. Also add the number of characters in the
2275 * spell-checked marked text to the saved value of totsize, and
2276 * then make that saved value the actual value. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002277 renumber(top_save);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002278 totsize_save += openfile->totsize;
2279 openfile->totsize = totsize_save;
2280
2281 /* Assign mark_begin to the line where the mark began before. */
2282 do_gotopos(mb_lineno_save, openfile->mark_begin_x,
2283 current_y_save, 0);
2284 openfile->mark_begin = openfile->current;
2285
2286 /* Assign mark_begin_x to the location in mark_begin where the
2287 * mark began before, adjusted for any shortening of the
2288 * line. */
2289 openfile->mark_begin_x = openfile->current_x;
2290
2291 /* Turn the mark back on. */
2292 openfile->mark_set = TRUE;
2293 }
2294#endif
2295
2296 /* Go back to the old position, and mark the file as modified. */
2297 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
2298 set_modified();
2299
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002300#ifndef NANO_TINY
David Lawrence Ramsey3fe08ac2005-07-26 01:17:16 +00002301 /* Handle a pending SIGWINCH again. */
2302 allow_pending_sigwinch(TRUE);
2303#endif
2304
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002305 return NULL;
2306}
2307
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002308/* Spell check the current file. If an alternate spell checker is
2309 * specified, use it. Otherwise, use the internal spell checker. */
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002310void do_spell(void)
2311{
2312 int i;
2313 FILE *temp_file;
2314 char *temp = safe_tempfile(&temp_file);
2315 const char *spell_msg;
2316
2317 if (temp == NULL) {
David Lawrence Ramseyf0e3ca62006-05-03 13:11:00 +00002318 statusbar(_("Error writing temp file: %s"), strerror(errno));
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002319 return;
2320 }
2321
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002322#ifndef NANO_TINY
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002323 if (openfile->mark_set)
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002324 i = write_marked_file(temp, temp_file, TRUE, OVERWRITE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002325 else
2326#endif
David Lawrence Ramseye014fbd2005-08-29 19:11:26 +00002327 i = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
David Lawrence Ramseycc8185f2005-07-25 02:33:45 +00002328
2329 if (i == -1) {
2330 statusbar(_("Error writing temp file: %s"), strerror(errno));
2331 free(temp);
2332 return;
2333 }
2334
2335 spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
2336 do_int_speller(temp);
2337 unlink(temp);
2338 free(temp);
2339
2340 /* If the spell-checker printed any error messages onscreen, make
2341 * sure that they're cleared off. */
2342 total_refresh();
2343
2344 if (spell_msg != NULL) {
2345 if (errno == 0)
2346 /* Don't display an error message of "Success". */
2347 statusbar(_("Spell checking failed: %s"), spell_msg);
2348 else
2349 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2350 strerror(errno));
2351 } else
2352 statusbar(_("Finished checking spelling"));
2353}
2354#endif /* !DISABLE_SPELLER */
2355
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002356#ifndef NANO_TINY
David Lawrence Ramseyd7f0fe92005-08-10 22:51:49 +00002357/* Our own version of "wc". Note that its character counts are in
2358 * multibyte characters instead of single-byte characters. */
David Lawrence Ramsey8e942342005-07-25 04:21:46 +00002359void do_wordlinechar_count(void)
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002360{
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002361 size_t words = 0, chars = 0;
2362 ssize_t lines = 0;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002363 size_t current_x_save = openfile->current_x;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002364 size_t pww_save = openfile->placewewant;
2365 filestruct *current_save = openfile->current;
2366 bool old_mark_set = openfile->mark_set;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002367 filestruct *top, *bot;
2368 size_t top_x, bot_x;
2369
2370 if (old_mark_set) {
2371 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002372 * contains only the marked text, and turn the mark off. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002373 mark_order((const filestruct **)&top, &top_x,
2374 (const filestruct **)&bot, &bot_x, NULL);
2375 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002376 openfile->mark_set = FALSE;
2377 }
2378
2379 /* Start at the top of the file. */
2380 openfile->current = openfile->fileage;
2381 openfile->current_x = 0;
2382 openfile->placewewant = 0;
2383
2384 /* Keep moving to the next word (counting punctuation characters as
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002385 * part of a word, as "wc -w" does), without updating the screen,
2386 * until we reach the end of the file, incrementing the total word
2387 * count whenever we're on a word just before moving. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002388 while (openfile->current != openfile->filebot ||
David Lawrence Ramsey2ffdea42005-11-03 21:08:39 +00002389 openfile->current->data[openfile->current_x] != '\0') {
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002390 if (do_next_word(TRUE, FALSE))
2391 words++;
2392 }
2393
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002394 /* Get the total line and character counts, as "wc -l" and "wc -c"
2395 * do, but get the latter in multibyte characters. */
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002396 if (old_mark_set) {
David Lawrence Ramsey78a81b22005-07-25 18:59:24 +00002397 lines = openfile->filebot->lineno -
2398 openfile->fileage->lineno + 1;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002399 chars = get_totsize(openfile->fileage, openfile->filebot);
2400
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002401 /* Unpartition the filestruct so that it contains all the text
2402 * again, and turn the mark back on. */
2403 unpartition_filestruct(&filepart);
2404 openfile->mark_set = TRUE;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002405 } else {
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002406 lines = openfile->filebot->lineno;
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002407 chars = openfile->totsize;
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002408 }
2409
2410 /* Restore where we were. */
2411 openfile->current = current_save;
2412 openfile->current_x = current_x_save;
2413 openfile->placewewant = pww_save;
2414
David Lawrence Ramsey72936852005-07-25 03:47:08 +00002415 /* Display the total word, line, and character counts on the
2416 * statusbar. */
David Lawrence Ramsey7b71f572005-10-06 20:46:11 +00002417 statusbar(_("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
David Lawrence Ramsey815fb0a2005-08-05 19:38:11 +00002418 _("In Selection: ") : "", (unsigned long)words, (long)lines,
David Lawrence Ramsey520a90c2005-07-25 21:23:11 +00002419 (unsigned long)chars);
David Lawrence Ramsey691698a2005-07-24 19:57:51 +00002420}
David Lawrence Ramseyebe34252005-11-15 03:17:35 +00002421#endif /* !NANO_TINY */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002422
David Lawrence Ramsey6d6a36c2005-12-08 07:09:08 +00002423/* Get verbatim input. */
David Lawrence Ramsey37ddfa92005-11-07 06:06:05 +00002424void do_verbatim_input(void)
2425{
2426 int *kbinput;
2427 size_t kbinput_len, i;
2428 char *output;
2429
2430 statusbar(_("Verbatim Input"));
2431
2432 /* If constant cursor position display is on, make sure the current
2433 * cursor position will be properly displayed on the statusbar. */
2434 if (ISSET(CONST_UPDATE))
2435 do_cursorpos(TRUE);
2436
2437 /* Read in all the verbatim characters. */
2438 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
2439
2440 /* Display all the verbatim characters at once, not filtering out
2441 * control characters. */
2442 output = charalloc(kbinput_len + 1);
2443
2444 for (i = 0; i < kbinput_len; i++)
2445 output[i] = (char)kbinput[i];
2446 output[i] = '\0';
2447
2448 do_output(output, kbinput_len, TRUE);
2449
2450 free(output);
2451}