blob: 64c0d8e37ccc8c7edc02604c8a83cdf90132bb72 [file] [log] [blame]
Chris Allegretta11b00112000-08-06 21:13:45 +00001/* $Id$ */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002/**************************************************************************
3 * nano.c *
4 * *
David Lawrence Ramseyf28f50e2004-01-09 23:04:55 +00005 * Copyright (C) 1999-2004 Chris Allegretta *
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00006 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
Chris Allegretta3a24f3f2001-10-24 11:33:54 +00008 * the Free Software Foundation; either version 2, or (at your option) *
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00009 * any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 * *
20 **************************************************************************/
21
Jordi Mallach55381aa2004-11-17 23:17:05 +000022#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
Chris Allegretta6efda542001-04-28 18:03:52 +000025
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000026#include <stdio.h>
27#include <stdlib.h>
28#include <stdarg.h>
29#include <signal.h>
30#include <unistd.h>
31#include <string.h>
32#include <fcntl.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000033#include <sys/ioctl.h>
34#include <sys/param.h>
Chris Allegretta27eb13f2000-11-05 16:52:21 +000035#include <sys/wait.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000036#include <errno.h>
37#include <ctype.h>
38#include <locale.h>
Adam Rogoyski77f36de2000-06-07 03:56:54 +000039#include <assert.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000040#include "proto.h"
41#include "nano.h"
42
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000043#ifdef HAVE_TERMIOS_H
44#include <termios.h>
45#endif
46
47#ifdef HAVE_TERMIO_H
48#include <termio.h>
49#endif
50
51#ifdef HAVE_GETOPT_H
52#include <getopt.h>
53#endif
54
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000055#ifndef NANO_SMALL
56#include <setjmp.h>
57#endif
58
Chris Allegretta6fe61492001-05-21 12:56:25 +000059#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramsey49c3f242004-07-12 16:07:14 +000060static ssize_t fill = 0; /* Fill - where to wrap lines,
61 basically */
Chris Allegretta6fe61492001-05-21 12:56:25 +000062#endif
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +000063#ifndef DISABLE_WRAPPING
David Lawrence Ramseyce62e822004-08-05 22:10:22 +000064static bool same_line_wrap = FALSE; /* Whether wrapped text should
David Lawrence Ramsey85529b32004-06-22 15:38:47 +000065 be prepended to the next
66 line */
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +000067#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +000068
Chris Allegretta6df90f52002-07-19 01:08:59 +000069static struct termios oldterm; /* The user's original term settings */
Chris Allegretta88520c92001-05-05 17:45:54 +000070static struct sigaction act; /* For all our fun signal handlers */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000071
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000072#ifndef NANO_SMALL
David Lawrence Ramsey85529b32004-06-22 15:38:47 +000073static sigjmp_buf jmpbuf; /* Used to return to mainloop after
74 SIGWINCH */
David Lawrence Ramsey22fac782004-08-05 15:16:19 +000075static int pid; /* The PID of the newly forked process
76 * in open_pipe(). It must be global
77 * because the signal handler needs
78 * it. */
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000079#endif
Chris Allegretta08020882001-01-29 23:37:54 +000080
David Lawrence Ramsey93c84052004-11-23 04:08:28 +000081#ifndef DISABLE_JUSTIFY
82static filestruct *jusbottom = NULL;
83 /* Pointer to end of justify buffer. */
84#endif
85
David Lawrence Ramseyda141062004-05-25 19:41:11 +000086/* What we do when we're all set to exit. */
87void finish(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000088{
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000089 if (!ISSET(NO_HELP))
90 blank_bottombars();
91 else
92 blank_statusbar();
Chris Allegretta6b58acd2001-04-12 03:01:53 +000093
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000094 wrefresh(bottomwin);
95 endwin();
96
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000097 /* Restore the old terminal settings. */
Chris Allegretta4da1fc62000-06-21 03:00:43 +000098 tcsetattr(0, TCSANOW, &oldterm);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000099
Chris Allegrettad8451932003-03-11 03:50:40 +0000100#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
101 if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG))
102 save_history();
103#endif
104
Chris Allegretta6232d662002-05-12 19:52:15 +0000105#ifdef DEBUG
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000106 thanks_for_all_the_fish();
Chris Allegretta6232d662002-05-12 19:52:15 +0000107#endif
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000108
David Lawrence Ramseyda141062004-05-25 19:41:11 +0000109 exit(0);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000110}
111
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000112/* Die (gracefully?). */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000113void die(const char *msg, ...)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000114{
115 va_list ap;
116
Chris Allegrettaa0d89972003-02-03 03:32:08 +0000117 endwin();
118 curses_ended = TRUE;
119
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +0000120 /* Restore the old terminal settings. */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000121 tcsetattr(0, TCSANOW, &oldterm);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000122
Chris Allegretta6df90f52002-07-19 01:08:59 +0000123 va_start(ap, msg);
124 vfprintf(stderr, msg, ap);
125 va_end(ap);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000126
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000127 /* Save the current file buffer if it's been modified. */
Chris Allegretta32da4562002-01-02 15:12:21 +0000128 if (ISSET(MODIFIED))
129 die_save_file(filename);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000130
Chris Allegretta355fbe52001-07-14 19:32:47 +0000131#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000132 /* Save all of the other modified file buffers, if any. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000133 if (open_files != NULL) {
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000134 openfilestruct *tmp = open_files;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000135
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000136 while (tmp != open_files->next) {
137 open_files = open_files->next;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000138
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000139 /* Save the current file buffer if it's been modified. */
140 if (open_files->flags & MODIFIED) {
141 /* Set fileage and filebot to match the current file
142 * buffer, and then write it to disk. */
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000143 fileage = open_files->fileage;
Chris Allegretta4dc03d52002-05-11 03:04:44 +0000144 filebot = open_files->filebot;
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000145 die_save_file(open_files->filename);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000146 }
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000147 }
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000148 }
149#endif
150
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000151 /* Get out. */
152 exit(1);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000153}
154
Chris Allegretta6df90f52002-07-19 01:08:59 +0000155void die_save_file(const char *die_filename)
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000156{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000157 char *ret;
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000158 bool failed = TRUE;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000159
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +0000160 /* If we're using restricted mode, don't write any emergency backup
161 * files, since that would allow reading from or writing to files
162 * not specified on the command line. */
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +0000163 if (ISSET(RESTRICTED))
164 return;
165
Chris Allegretta6df90f52002-07-19 01:08:59 +0000166 /* If we can't save, we have REAL bad problems, but we might as well
167 TRY. */
168 if (die_filename[0] == '\0')
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000169 die_filename = "nano";
Chris Allegretta6df90f52002-07-19 01:08:59 +0000170
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000171 ret = get_next_filename(die_filename);
Chris Allegretta6df90f52002-07-19 01:08:59 +0000172 if (ret[0] != '\0')
David Lawrence Ramsey951d7142004-10-04 15:23:47 +0000173 failed = (write_file(ret, TRUE, FALSE, TRUE) == -1);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000174
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000175 if (!failed)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000176 fprintf(stderr, _("\nBuffer written to %s\n"), ret);
Chris Allegretta3dbb2782000-12-02 04:36:50 +0000177 else
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000178 fprintf(stderr, _("\nBuffer not written to %s (too many backup files?)\n"), ret);
Chris Allegretta48b06702002-02-22 04:30:50 +0000179
180 free(ret);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000181}
182
Chris Allegrettae61e8302001-01-14 05:18:27 +0000183/* Die with an error message that the screen was too small if, well, the
Chris Allegretta6df90f52002-07-19 01:08:59 +0000184 * screen is too small. */
David Lawrence Ramsey0341b582002-08-21 16:10:37 +0000185void die_too_small(void)
Chris Allegrettae61e8302001-01-14 05:18:27 +0000186{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000187 die(_("Window size is too small for nano...\n"));
Chris Allegrettae61e8302001-01-14 05:18:27 +0000188}
189
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000190void print_view_warning(void)
191{
192 statusbar(_("Key illegal in VIEW mode"));
193}
194
David Lawrence Ramseyf28f50e2004-01-09 23:04:55 +0000195/* Initialize global variables -- no better way for now. If
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000196 * save_cutbuffer is TRUE, don't set cutbuffer to NULL. */
197void global_init(bool save_cutbuffer)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000198{
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000199 current_x = 0;
200 current_y = 0;
Chris Allegrettae61e8302001-01-14 05:18:27 +0000201
Chris Allegrettaf8f2d582003-02-10 02:43:48 +0000202 editwinrows = LINES - 5 + no_help();
203 if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS)
Chris Allegrettae61e8302001-01-14 05:18:27 +0000204 die_too_small();
205
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000206 fileage = NULL;
Chris Allegretta56214c62001-09-27 02:46:53 +0000207 if (!save_cutbuffer)
208 cutbuffer = NULL;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000209 current = NULL;
210 edittop = NULL;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000211 totlines = 0;
Chris Allegretta56214c62001-09-27 02:46:53 +0000212 totsize = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000213 placewewant = 0;
Chris Allegrettae61e8302001-01-14 05:18:27 +0000214
Chris Allegretta6fe61492001-05-21 12:56:25 +0000215#ifndef DISABLE_WRAPJUSTIFY
Chris Allegretta6df90f52002-07-19 01:08:59 +0000216 fill = wrap_at;
Chris Allegrettadffa2072002-07-24 01:02:26 +0000217 if (fill <= 0)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000218 fill += COLS;
Chris Allegrettaf8f2d582003-02-10 02:43:48 +0000219 if (fill < 0)
220 fill = 0;
Chris Allegretta6fe61492001-05-21 12:56:25 +0000221#endif
Chris Allegrettae61e8302001-01-14 05:18:27 +0000222
Chris Allegretta88b09152001-05-17 11:35:43 +0000223 hblank = charalloc(COLS + 1);
Chris Allegretta0a06e072001-01-23 02:35:04 +0000224 memset(hblank, ' ', COLS);
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000225 hblank[COLS] = '\0';
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000226}
227
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000228void window_init(void)
229{
Chris Allegrettaf8f2d582003-02-10 02:43:48 +0000230 editwinrows = LINES - 5 + no_help();
231 if (editwinrows < MIN_EDITOR_ROWS)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000232 die_too_small();
233
Chris Allegretta1a128af2003-01-26 04:15:56 +0000234 if (topwin != NULL)
235 delwin(topwin);
David Lawrence Ramseyce991bb2004-03-29 18:36:39 +0000236 if (edit != NULL)
237 delwin(edit);
Chris Allegretta1a128af2003-01-26 04:15:56 +0000238 if (bottomwin != NULL)
239 delwin(bottomwin);
240
David Lawrence Ramseyce991bb2004-03-29 18:36:39 +0000241 /* Set up the windows. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000242 topwin = newwin(2, COLS, 0, 0);
David Lawrence Ramseyce991bb2004-03-29 18:36:39 +0000243 edit = newwin(editwinrows, COLS, 2, 0);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000244 bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0);
245
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +0000246 /* Turn the keypad back on. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000247 keypad(edit, TRUE);
248 keypad(bottomwin, TRUE);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000249}
250
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +0000251#ifndef DISABLE_MOUSE
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000252void mouse_init(void)
253{
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000254 if (ISSET(USE_MOUSE)) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000255 mousemask(BUTTON1_RELEASED, NULL);
256 mouseinterval(50);
257 } else
258 mousemask(0, NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000259}
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000260#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000261
262#ifndef DISABLE_HELP
263/* This function allocates help_text, and stores the help string in it.
264 * help_text should be NULL initially. */
265void help_init(void)
266{
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000267 size_t allocsize = 1; /* Space needed for help_text. */
268 const char *htx; /* Untranslated help message. */
269 char *ptr;
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000270 const shortcut *s;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000271#ifndef NANO_SMALL
272 const toggle *t;
273#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000274
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000275 /* First, set up the initial help text for the current function. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000276 if (currshortcut == whereis_list || currshortcut == replace_list
277 || currshortcut == replace_list_2)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000278 htx = N_("Search Command Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000279 "Enter the words or characters you would like to search "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000280 "for, then hit Enter. If there is a match for the text you "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000281 "entered, the screen will be updated to the location of the "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000282 "nearest match for the search string.\n\n The previous "
283 "search string will be shown in brackets after the search "
284 "prompt. Hitting Enter without entering any text will "
285 "perform the previous search. If you have selected text "
286 "with the mark and then search to replace, only matches in "
287 "the selected text will be replaced.\n\n The following "
288 "function keys are available in Search mode:\n\n");
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +0000289 else if (currshortcut == gotoline_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000290 htx = N_("Go To Line Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000291 "Enter the line number that you wish to go to and hit "
292 "Enter. If there are fewer lines of text than the "
293 "number you entered, you will be brought to the last line "
294 "of the file.\n\n The following function keys are "
295 "available in Go To Line mode:\n\n");
296 else if (currshortcut == insertfile_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000297 htx = N_("Insert File Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000298 "Type in the name of a file to be inserted into the current "
299 "file buffer at the current cursor location.\n\n "
300 "If you have compiled nano with multiple file buffer "
301 "support, and enable multiple buffers with the -F "
302 "or --multibuffer command line flags, the Meta-F toggle, or "
303 "a nanorc file, inserting a file will cause it to be "
304 "loaded into a separate buffer (use Meta-< and > to switch "
David Lawrence Ramseyfdd3bec2004-11-07 21:30:55 +0000305 "between file buffers). If you need another blank buffer, "
306 "do not enter any filename, or type in a nonexistent "
307 "filename at the prompt and press Enter.\n\n The following "
308 "function keys are available in Insert File mode:\n\n");
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000309 else if (currshortcut == writefile_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000310 htx = N_("Write File Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000311 "Type the name that you wish to save the current file "
312 "as and hit Enter to save the file.\n\n If you have "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000313 "selected text with the mark, you will be prompted to "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000314 "save only the selected portion to a separate file. To "
315 "reduce the chance of overwriting the current file with "
316 "just a portion of it, the current filename is not the "
317 "default in this mode.\n\n The following function keys "
318 "are available in Write File mode:\n\n");
319#ifndef DISABLE_BROWSER
320 else if (currshortcut == browser_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000321 htx = N_("File Browser Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000322 "The file browser is used to visually browse the "
323 "directory structure to select a file for reading "
324 "or writing. You may use the arrow keys or Page Up/"
325 "Down to browse through the files, and S or Enter to "
326 "choose the selected file or enter the selected "
327 "directory. To move up one level, select the directory "
328 "called \"..\" at the top of the file list.\n\n The "
329 "following function keys are available in the file "
330 "browser:\n\n");
331 else if (currshortcut == gotodir_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000332 htx = N_("Browser Go To Directory Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000333 "Enter the name of the directory you would like to "
334 "browse to.\n\n If tab completion has not been disabled, "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000335 "you can use the Tab key to (attempt to) automatically "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000336 "complete the directory name.\n\n The following function "
337 "keys are available in Browser Go To Directory mode:\n\n");
338#endif
Chris Allegretta201f1d92003-02-05 02:51:19 +0000339#ifndef DISABLE_SPELLER
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000340 else if (currshortcut == spell_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000341 htx = N_("Spell Check Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000342 "The spell checker checks the spelling of all text "
343 "in the current file. When an unknown word is "
344 "encountered, it is highlighted and a replacement can "
345 "be edited. It will then prompt to replace every "
346 "instance of the given misspelled word in the "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000347 "current file, or, if you have selected text with the "
348 "mark, in the selected text.\n\n The following other "
349 "functions are available in Spell Check mode:\n\n");
Chris Allegretta201f1d92003-02-05 02:51:19 +0000350#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000351#ifndef NANO_SMALL
352 else if (currshortcut == extcmd_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000353 htx = N_("External Command Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000354 "This menu allows you to insert the output of a command "
355 "run by the shell into the current buffer (or a new "
David Lawrence Ramseyfdd3bec2004-11-07 21:30:55 +0000356 "buffer in multibuffer mode). If you need another blank "
357 "buffer, do not enter any command.\n\n The following keys "
358 "are available in this mode:\n\n");
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000359#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000360 else
361 /* Default to the main help list. */
362 htx = N_(" nano help text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000363 "The nano editor is designed to emulate the functionality and "
364 "ease-of-use of the UW Pico text editor. There are four main "
365 "sections of the editor: The top line shows the program "
366 "version, the current filename being edited, and whether "
367 "or not the file has been modified. Next is the main editor "
368 "window showing the file being edited. The status line is "
369 "the third line from the bottom and shows important messages. "
370 "The bottom two lines show the most commonly used shortcuts "
371 "in the editor.\n\n "
372 "The notation for shortcuts is as follows: Control-key "
David Lawrence Ramseyaaad3af2003-08-31 16:44:10 +0000373 "sequences are notated with a caret (^) symbol and can be "
374 "entered either by using the Control (Ctrl) key or pressing the "
375 "Esc key twice. Escape-key sequences are notated with the Meta "
376 "(M) symbol and can be entered using either the Esc, Alt or "
377 "Meta key depending on your keyboard setup. Also, pressing Esc "
378 "twice and then typing a three-digit number from 000 to 255 "
379 "will enter the character with the corresponding ASCII code. "
380 "The following keystrokes are available in the main editor "
381 "window. Alternative keys are shown in parentheses:\n\n");
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000382
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000383 htx = _(htx);
384
385 allocsize += strlen(htx);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000386
387 /* The space needed for the shortcut lists, at most COLS characters,
388 * plus '\n'. */
David Lawrence Ramsey1f1ebb82004-11-11 21:50:01 +0000389 allocsize += (COLS < 21 ? 21 : COLS + 1) *
390 length_of_list(currshortcut);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000391
392#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000393 /* If we're on the main list, we also count the toggle help text.
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000394 * Each line has "M-%c\t\t\t", which fills 24 columns, plus a space,
395 * plus translated text, plus '\n'. */
Chris Allegretta3a784062003-02-10 02:32:58 +0000396 if (currshortcut == main_list) {
397 size_t endislen = strlen(_("enable/disable"));
398
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000399 for (t = toggles; t != NULL; t = t->next)
Chris Allegretta3a784062003-02-10 02:32:58 +0000400 allocsize += 8 + strlen(t->desc) + endislen;
401 }
David Lawrence Ramsey1f1ebb82004-11-11 21:50:01 +0000402#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000403
404 /* help_text has been freed and set to NULL unless the user resized
405 * while in the help screen. */
406 free(help_text);
407
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000408 /* Allocate space for the help text. */
Chris Allegretta908f7702003-01-15 11:18:58 +0000409 help_text = charalloc(allocsize);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000410
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000411 /* Now add the text we want. */
412 strcpy(help_text, htx);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000413 ptr = help_text + strlen(help_text);
414
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000415 /* Now add our shortcut info. Assume that each shortcut has, at the
416 * very least, an equivalent control key, an equivalent primary meta
417 * key sequence, or both. Also assume that the meta key values are
418 * not control characters. We can display a maximum of 3 shortcut
419 * entries. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000420 for (s = currshortcut; s != NULL; s = s->next) {
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000421 int entries = 0;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000422
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000423 /* Control key. */
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000424 if (s->ctrlval != NANO_NO_KEY) {
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000425 entries++;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000426#ifndef NANO_SMALL
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000427 if (s->ctrlval == NANO_HISTORY_KEY)
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000428 ptr += sprintf(ptr, "%.7s", _("Up"));
David Lawrence Ramseyf4276942003-12-24 03:33:09 +0000429 else
David Lawrence Ramseya593f532003-11-28 19:47:42 +0000430#endif
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000431 if (s->ctrlval == NANO_CONTROL_SPACE)
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000432 ptr += sprintf(ptr, "^%.6s", _("Space"));
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000433 else if (s->ctrlval == NANO_CONTROL_8)
David Lawrence Ramseya593f532003-11-28 19:47:42 +0000434 ptr += sprintf(ptr, "^?");
435 else
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000436 ptr += sprintf(ptr, "^%c", s->ctrlval + 64);
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000437 *(ptr++) = '\t';
David Lawrence Ramseya593f532003-11-28 19:47:42 +0000438 }
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000439
440 /* Function key. */
441 if (s->funcval != NANO_NO_KEY) {
442 entries++;
David Lawrence Ramsey2e83a502004-11-01 22:35:26 +0000443 /* If this is the first entry, put it in the middle. */
444 if (entries == 1) {
445 entries++;
446 *(ptr++) = '\t';
447 }
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000448 ptr += sprintf(ptr, "(F%d)", s->funcval - KEY_F0);
449 *(ptr++) = '\t';
450 }
451
452 /* Primary meta key sequence. */
453 if (s->metaval != NANO_NO_KEY) {
454 entries++;
455 /* If this is the last entry, put it at the end. */
456 if (entries == 2 && s->miscval == NANO_NO_KEY) {
457 entries++;
458 *(ptr++) = '\t';
459 }
460 /* If the primary meta key sequence is the first entry,
461 * don't put parentheses around it. */
462 if (entries == 1 && s->metaval == NANO_ALT_SPACE)
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000463 ptr += sprintf(ptr, "M-%.5s", _("Space"));
David Lawrence Ramseyf4276942003-12-24 03:33:09 +0000464 else
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000465 ptr += sprintf(ptr, entries == 1 ? "M-%c" : "(M-%c)",
466 toupper(s->metaval));
467 *(ptr++) = '\t';
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000468 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000469
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000470 /* Miscellaneous meta key sequence. */
471 if (entries < 3 && s->miscval != NANO_NO_KEY) {
472 entries++;
473 /* If this is the last entry, put it at the end. */
474 if (entries == 2) {
475 entries++;
476 *(ptr++) = '\t';
477 }
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000478 ptr += sprintf(ptr, "(M-%c)", toupper(s->miscval));
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000479 *(ptr++) = '\t';
480 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000481
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000482 /* Make sure all the help text starts at the same place. */
483 while (entries < 3) {
484 entries++;
485 *(ptr++) = '\t';
486 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000487
488 assert(s->help != NULL);
Chris Allegretta3a784062003-02-10 02:32:58 +0000489 ptr += sprintf(ptr, "%.*s\n", COLS > 24 ? COLS - 24 : 0, s->help);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000490 }
491
492#ifndef NANO_SMALL
493 /* And the toggles... */
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000494 if (currshortcut == main_list) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000495 for (t = toggles; t != NULL; t = t->next) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000496 assert(t->desc != NULL);
David Lawrence Ramseyd9fb3e62004-10-21 22:49:51 +0000497 ptr += sprintf(ptr, "M-%c\t\t\t%s %s\n", toupper(t->val),
498 t->desc, _("enable/disable"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000499 }
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000500 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000501#endif /* !NANO_SMALL */
502
503 /* If all went well, we didn't overwrite the allocated space for
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000504 * help_text. */
Chris Allegretta908f7702003-01-15 11:18:58 +0000505 assert(strlen(help_text) < allocsize);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000506}
507#endif
508
509/* Create a new filestruct node. Note that we specifically do not set
510 * prevnode->next equal to the new line. */
511filestruct *make_new_node(filestruct *prevnode)
512{
513 filestruct *newnode = (filestruct *)nmalloc(sizeof(filestruct));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000514 newnode->data = NULL;
515 newnode->prev = prevnode;
516 newnode->next = NULL;
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000517 newnode->lineno = (prevnode != NULL) ? prevnode->lineno + 1 : 1;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000518 return newnode;
519}
520
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000521/* Make a copy of a filestruct node. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000522filestruct *copy_node(const filestruct *src)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000523{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000524 filestruct *dst = (filestruct *)nmalloc(sizeof(filestruct));
Chris Allegretta6df90f52002-07-19 01:08:59 +0000525 assert(src != NULL);
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000526 dst->data = mallocstrcpy(NULL, src->data);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000527 dst->next = src->next;
528 dst->prev = src->prev;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000529 dst->lineno = src->lineno;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000530 return dst;
531}
532
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000533/* Splice a node into an existing filestruct. */
David Lawrence Ramseyf7080372004-07-08 17:15:10 +0000534void splice_node(filestruct *begin, filestruct *newnode, filestruct
535 *end)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000536{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000537 assert(newnode != NULL && begin != NULL);
538 newnode->next = end;
539 newnode->prev = begin;
540 begin->next = newnode;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000541 if (end != NULL)
542 end->prev = newnode;
543}
544
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000545/* Unlink a node from the rest of the filestruct. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000546void unlink_node(const filestruct *fileptr)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000547{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000548 assert(fileptr != NULL);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000549 if (fileptr->prev != NULL)
550 fileptr->prev->next = fileptr->next;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000551 if (fileptr->next != NULL)
552 fileptr->next->prev = fileptr->prev;
553}
554
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000555/* Delete a node from the filestruct. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000556void delete_node(filestruct *fileptr)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000557{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000558 assert(fileptr != NULL && fileptr->data != NULL);
559 if (fileptr->data != NULL)
560 free(fileptr->data);
561 free(fileptr);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000562}
563
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000564/* Okay, now let's duplicate a whole struct! */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000565filestruct *copy_filestruct(const filestruct *src)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000566{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000567 filestruct *head; /* copy of src, top of the copied list */
568 filestruct *prev; /* temp that traverses the list */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000569
Chris Allegretta6df90f52002-07-19 01:08:59 +0000570 assert(src != NULL);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000571
Chris Allegretta6df90f52002-07-19 01:08:59 +0000572 prev = copy_node(src);
573 prev->prev = NULL;
574 head = prev;
575 src = src->next;
576 while (src != NULL) {
577 prev->next = copy_node(src);
578 prev->next->prev = prev;
579 prev = prev->next;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000580
Chris Allegretta6df90f52002-07-19 01:08:59 +0000581 src = src->next;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000582 }
583
Chris Allegretta6df90f52002-07-19 01:08:59 +0000584 prev->next = NULL;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000585 return head;
586}
587
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000588/* Frees a filestruct. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000589void free_filestruct(filestruct *src)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000590{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000591 assert(src != NULL);
592
593 while (src->next != NULL) {
594 src = src->next;
595 delete_node(src->prev);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000596 }
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000597 delete_node(src);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000598}
599
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000600/* Partition a filestruct so it begins at (top, top_x) and ends at (bot,
601 * bot_x). */
602partition *partition_filestruct(filestruct *top, size_t top_x,
603 filestruct *bot, size_t bot_x)
604{
605 partition *p;
David Lawrence Ramsey6299b0d2004-11-08 03:22:23 +0000606 assert(top != NULL && bot != NULL && fileage != NULL && filebot != NULL);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000607
608 /* Initialize the partition. */
609 p = (partition *)nmalloc(sizeof(partition));
610
David Lawrence Ramseyf978f042004-11-04 16:45:48 +0000611 /* If the top and bottom of the partition are different from the top
612 * and bottom of the filestruct, save the latter and then set them
613 * to top and bot. */
614 if (top != fileage) {
615 p->fileage = fileage;
616 fileage = top;
617 } else
618 p->fileage = NULL;
619 if (bot != filebot) {
620 p->filebot = filebot;
621 filebot = bot;
622 } else
623 p->filebot = NULL;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000624
625 /* Save the line above the top of the partition, detach the top of
626 * the partition from it, and save the text before top_x in
627 * top_data. */
628 p->top_prev = top->prev;
629 top->prev = NULL;
630 p->top_data = mallocstrncpy(NULL, top->data, top_x + 1);
631 p->top_data[top_x] = '\0';
632
633 /* Save the line below the bottom of the partition, detach the
634 * bottom of the partition from it, and save the text after bot_x in
635 * bot_data. */
636 p->bot_next = bot->next;
637 bot->next = NULL;
638 p->bot_data = mallocstrcpy(NULL, bot->data + bot_x);
639
640 /* Remove all text after bot_x at the bottom of the partition. */
641 null_at(&bot->data, bot_x);
642
643 /* Remove all text before top_x at the top of the partition. */
644 charmove(top->data, top->data + top_x, strlen(top->data) -
645 top_x + 1);
646 align(&top->data);
647
648 /* Return the partition. */
649 return p;
650}
651
652/* Unpartition a filestruct so it begins at (fileage, 0) and ends at
653 * (filebot, strlen(filebot)) again. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000654void unpartition_filestruct(partition **p)
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000655{
656 char *tmp;
David Lawrence Ramsey2c315402004-11-08 03:19:10 +0000657 assert(p != NULL && fileage != NULL && filebot != NULL);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000658
659 /* Reattach the line above the top of the partition, and restore the
660 * text before top_x from top_data. Free top_data when we're done
661 * with it. */
662 tmp = mallocstrcpy(NULL, fileage->data);
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000663 fileage->prev = (*p)->top_prev;
David Lawrence Ramsey8cbd4cb2004-11-04 15:31:43 +0000664 if (fileage->prev != NULL)
665 fileage->prev->next = fileage;
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000666 fileage->data = charealloc(fileage->data, strlen((*p)->top_data) +
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000667 strlen(fileage->data) + 1);
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000668 strcpy(fileage->data, (*p)->top_data);
669 free((*p)->top_data);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000670 strcat(fileage->data, tmp);
671 free(tmp);
672
673 /* Reattach the line below the bottom of the partition, and restore
674 * the text after bot_x from bot_data. Free bot_data when we're
675 * done with it. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000676 filebot->next = (*p)->bot_next;
David Lawrence Ramsey8cbd4cb2004-11-04 15:31:43 +0000677 if (filebot->next != NULL)
678 filebot->next->prev = filebot;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000679 filebot->data = charealloc(filebot->data, strlen(filebot->data) +
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000680 strlen((*p)->bot_data) + 1);
681 strcat(filebot->data, (*p)->bot_data);
682 free((*p)->bot_data);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000683
David Lawrence Ramseyf978f042004-11-04 16:45:48 +0000684 /* Restore the top and bottom of the filestruct, if they were
685 * different from the top and bottom of the partition. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000686 if ((*p)->fileage != NULL)
687 fileage = (*p)->fileage;
688 if ((*p)->filebot != NULL)
689 filebot = (*p)->filebot;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000690
691 /* Uninitialize the partition. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000692 free(*p);
693 *p = NULL;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000694}
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000695
David Lawrence Ramsey93c84052004-11-23 04:08:28 +0000696/* Move all the text between (top, top_x) and (bot, bot_x) in the
697 * current filestruct to a filestruct beginning with file_top and ending
698 * with file_bot. If no text is between (top, top_x) and (bot, bot_x),
699 * don't do anything. */
700void move_to_filestruct(filestruct **file_top, filestruct **file_bot,
701 filestruct *top, size_t top_x, filestruct *bot, size_t bot_x)
702{
703 filestruct *top_save;
704 long part_totsize;
705 bool at_edittop;
706#ifndef NANO_SMALL
707 bool mark_inside = FALSE;
708#endif
709
710 assert(file_top != NULL && file_bot != NULL && top != NULL && bot != NULL);
711
712 /* If (top, top_x)-(bot, bot_x) doesn't cover any text, get out. */
713 if (top == bot && top_x == bot_x)
714 return;
715
716 /* Partition the filestruct so that it contains only the text from
717 * (top, top_x) to (bot, bot_x), keep track of whether the top of
718 * the partition is the top of the edit window, and keep track of
719 * whether the mark begins inside the partition. */
720 filepart = partition_filestruct(top, top_x, bot, bot_x);
721 at_edittop = (fileage == edittop);
722#ifndef NANO_SMALL
723 if (ISSET(MARK_ISSET))
724 mark_inside = (mark_beginbuf->lineno >= fileage->lineno &&
725 mark_beginbuf->lineno <= filebot->lineno &&
726 (mark_beginbuf != fileage || mark_beginx >= top_x) &&
727 (mark_beginbuf != filebot || mark_beginx <= bot_x));
728#endif
729
730 /* Get the number of characters in the text, and subtract it from
731 * totsize. */
732 get_totals(top, bot, NULL, &part_totsize);
733 totsize -= part_totsize;
734
735 if (*file_top == NULL) {
736 /* If file_top is empty, just move all the text directly into
737 * it. This is equivalent to tacking the text in top onto the
738 * (lack of) text at the end of file_top. */
739 *file_top = fileage;
740 *file_bot = filebot;
741 } else {
742 /* Otherwise, tack the text in top onto the text at the end of
743 * file_bot. */
744 (*file_bot)->data = charealloc((*file_bot)->data,
745 strlen((*file_bot)->data) + strlen(fileage->data) + 1);
746 strcat((*file_bot)->data, fileage->data);
747
748 /* Attach the line after top to the line after file_bot. Then,
749 * if there's more than one line after top, move file_bot down
750 * to bot. */
751 (*file_bot)->next = fileage->next;
752 if ((*file_bot)->next != NULL) {
753 (*file_bot)->next->prev = *file_bot;
754 *file_bot = filebot;
755 }
756 }
757
758 /* Since the text has now been saved, remove it from the filestruct.
759 * If the top of the partition was the top of the edit window, set
760 * edittop to where the text used to start. If the mark began
761 * inside the partition, set the beginning of the mark to where the
762 * text used to start. */
763 fileage = (filestruct *)nmalloc(sizeof(filestruct));
764 fileage->data = mallocstrcpy(NULL, "");
765 filebot = fileage;
766 if (at_edittop)
767 edittop = fileage;
768#ifndef NANO_SMALL
769 if (mark_inside) {
770 mark_beginbuf = fileage;
771 mark_beginx = top_x;
772 }
773#endif
774
775 /* Restore the current line and cursor position. */
776 current = fileage;
777 current_x = top_x;
778
779 top_save = fileage;
780
781 /* Unpartition the filestruct so that it contains all the text
782 * again, minus the saved text. */
783 unpartition_filestruct(&filepart);
784
785 /* Renumber starting with the beginning line of the old
786 * partition. */
787 renumber(top_save);
788
789 if (filebot->data[0] != '\0')
790 new_magicline();
791
792 /* Set totlines to the new number of lines in the file. */
793 totlines = filebot->lineno;
794}
795
796/* Copy all the text from the filestruct beginning with file_top and
797 * ending with file_bot to the current filestruct at the current cursor
798 * position. */
799void copy_from_filestruct(filestruct *file_top, filestruct *file_bot)
800{
801 filestruct *top_save;
802 int part_totlines;
803 long part_totsize;
804 bool at_edittop;
805
806 assert(file_top != NULL && file_bot != NULL);
807
808 /* Partition the filestruct so that it contains no text, and keep
809 * track of whether the top of the partition is the top of the edit
810 * window. */
811 filepart = partition_filestruct(current, current_x, current,
812 current_x);
813 at_edittop = (fileage == edittop);
814
815 /* Put the top and bottom of the filestruct at copies of file_top
816 * and file_bot. */
817 fileage = copy_filestruct(file_top);
818 filebot = fileage;
819 while (filebot->next != NULL)
820 filebot = filebot->next;
821
822 /* Restore the current line and cursor position. */
823 current = filebot;
824 current_x = strlen(filebot->data);
825 if (fileage == filebot)
826 current_x += strlen(filepart->top_data);
827
828 /* Get the number of lines and the number of characters in the saved
829 * text, and add the latter to totsize. */
830 get_totals(fileage, filebot, &part_totlines, &part_totsize);
831 totsize += part_totsize;
832
833 /* If the top of the partition was the top of the edit window, set
834 * edittop to where the saved text now starts, and update the
835 * current y-coordinate to account for the number of lines it
836 * has, less one since the first line will be tacked onto the
837 * current line. */
838 if (at_edittop)
839 edittop = fileage;
840 current_y += part_totlines - 1;
841
842 top_save = fileage;
843
844 /* Unpartition the filestruct so that it contains all the text
845 * again, minus the saved text. */
846 unpartition_filestruct(&filepart);
847
848 /* Renumber starting with the beginning line of the old
849 * partition. */
850 renumber(top_save);
851
852 if (filebot->data[0] != '\0')
853 new_magicline();
854
855 /* Set totlines to the new number of lines in the file. */
856 totlines = filebot->lineno;
857}
858
Chris Allegretta6df90f52002-07-19 01:08:59 +0000859void renumber_all(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000860{
861 filestruct *temp;
Chris Allegrettabef12972002-03-06 03:30:40 +0000862 int i = 1;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000863
Chris Allegretta6df90f52002-07-19 01:08:59 +0000864 assert(fileage == NULL || fileage != fileage->next);
865 for (temp = fileage; temp != NULL; temp = temp->next)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000866 temp->lineno = i++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000867}
868
Chris Allegretta6df90f52002-07-19 01:08:59 +0000869void renumber(filestruct *fileptr)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000870{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000871 if (fileptr == NULL || fileptr->prev == NULL || fileptr == fileage)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000872 renumber_all();
Chris Allegretta6df90f52002-07-19 01:08:59 +0000873 else {
874 int lineno = fileptr->prev->lineno;
875
876 assert(fileptr != fileptr->next);
877 for (; fileptr != NULL; fileptr = fileptr->next)
878 fileptr->lineno = ++lineno;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000879 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000880}
881
David Lawrence Ramsey22fac782004-08-05 15:16:19 +0000882/* Print one usage string to the screen. This cuts down on duplicate
883 * strings to translate and leaves out the parts that shouldn't be
Chris Allegretta6df90f52002-07-19 01:08:59 +0000884 * translatable (the flag names). */
David Lawrence Ramseyf7080372004-07-08 17:15:10 +0000885void print1opt(const char *shortflag, const char *longflag, const char
886 *desc)
Chris Allegretta3fc5d572002-03-09 18:51:58 +0000887{
888 printf(" %s\t", shortflag);
889 if (strlen(shortflag) < 8)
890 printf("\t");
891
892#ifdef HAVE_GETOPT_LONG
893 printf("%s\t", longflag);
894 if (strlen(longflag) < 8)
895 printf("\t\t");
896 else if (strlen(longflag) < 16)
897 printf("\t");
898#endif
899
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000900 printf("%s\n", _(desc));
Chris Allegretta3fc5d572002-03-09 18:51:58 +0000901}
902
David Lawrence Ramsey0341b582002-08-21 16:10:37 +0000903void usage(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000904{
905#ifdef HAVE_GETOPT_LONG
Chris Allegretta6df90f52002-07-19 01:08:59 +0000906 printf(_("Usage: nano [+LINE] [GNU long option] [option] [file]\n\n"));
907 printf(_("Option\t\tLong option\t\tMeaning\n"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000908#else
Chris Allegretta6df90f52002-07-19 01:08:59 +0000909 printf(_("Usage: nano [+LINE] [option] [file]\n\n"));
910 printf(_("Option\t\tMeaning\n"));
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +0000911#endif
Chris Allegretta3fc5d572002-03-09 18:51:58 +0000912
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000913 print1opt("-h, -?", "--help", N_("Show this message"));
914 print1opt(_("+LINE"), "", N_("Start at line number LINE"));
Chris Allegretta7004c282001-09-22 00:42:10 +0000915#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000916 print1opt("-A", "--smarthome", N_("Enable smart home key"));
917 print1opt("-B", "--backup", N_("Backup existing files on save"));
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000918 print1opt(_("-E [dir]"), _("--backupdir=[dir]"), N_("Directory for writing backup files"));
Chris Allegretta7004c282001-09-22 00:42:10 +0000919#endif
Chris Allegretta355fbe52001-07-14 19:32:47 +0000920#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000921 print1opt("-F", "--multibuffer", N_("Enable multiple file buffers"));
Chris Allegretta355fbe52001-07-14 19:32:47 +0000922#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +0000923#ifdef ENABLE_NANORC
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +0000924#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000925 print1opt("-H", "--historylog", N_("Log & read search/replace string history"));
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +0000926#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000927 print1opt("-I", "--ignorercfiles", N_("Don't look at nanorc files"));
Chris Allegretta6df90f52002-07-19 01:08:59 +0000928#endif
Chris Allegretta8fa1e282001-09-22 04:20:25 +0000929#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000930 print1opt("-N", "--noconvert", N_("Don't convert files from DOS/Mac format"));
Chris Allegretta8fa1e282001-09-22 04:20:25 +0000931#endif
David Lawrence Ramsey74835712004-12-04 17:41:52 +0000932#ifdef NANO_WIDE
933 print1opt("-O", "--noutf8", N_("Don't convert files from UTF-8 format"));
934#endif
Chris Allegrettae4f940d2002-03-03 22:36:36 +0000935#ifndef DISABLE_JUSTIFY
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000936 print1opt(_("-Q [str]"), _("--quotestr=[str]"), N_("Quoting string, default \"> \""));
Chris Allegrettae4f940d2002-03-03 22:36:36 +0000937#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +0000938#ifdef HAVE_REGEX_H
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000939 print1opt("-R", "--regexp", N_("Do regular expression searches"));
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +0000940#endif
Chris Allegretta3e3ae942001-09-22 19:02:04 +0000941#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000942 print1opt("-S", "--smooth", N_("Smooth scrolling"));
Chris Allegretta3e3ae942001-09-22 19:02:04 +0000943#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000944 print1opt(_("-T [#cols]"), _("--tabsize=[#cols]"), N_("Set width of a tab in cols to #cols"));
945 print1opt("-V", "--version", N_("Print version information and exit"));
Chris Allegretta09900ff2002-05-04 04:23:30 +0000946#ifdef ENABLE_COLOR
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000947 print1opt(_("-Y [str]"), _("--syntax [str]"), N_("Syntax definition to use"));
Chris Allegretta09900ff2002-05-04 04:23:30 +0000948#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000949 print1opt("-Z", "--restricted", N_("Restricted mode"));
950 print1opt("-c", "--const", N_("Constantly show cursor position"));
Chris Allegrettad19e9912000-07-12 18:14:51 +0000951#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000952 print1opt("-d", "--rebinddelete", N_("Fix Backspace/Delete confusion problem"));
953 print1opt("-i", "--autoindent", N_("Automatically indent new lines"));
954 print1opt("-k", "--cut", N_("Cut from cursor to end of line"));
Chris Allegrettad19e9912000-07-12 18:14:51 +0000955#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000956 print1opt("-l", "--nofollow", N_("Don't follow symbolic links, overwrite"));
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +0000957#ifndef DISABLE_MOUSE
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000958 print1opt("-m", "--mouse", N_("Enable mouse"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000959#endif
Chris Allegrettae1f14522001-09-19 03:19:43 +0000960#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000961 print1opt(_("-o [dir]"), _("--operatingdir=[dir]"), N_("Set operating directory"));
Chris Allegrettae1f14522001-09-19 03:19:43 +0000962#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000963 print1opt("-p", "--preserve", N_("Preserve XON (^Q) and XOFF (^S) keys"));
Chris Allegretta6fe61492001-05-21 12:56:25 +0000964#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000965 print1opt(_("-r [#cols]"), _("--fill=[#cols]"), N_("Set fill cols to (wrap lines at) #cols"));
Chris Allegretta6fe61492001-05-21 12:56:25 +0000966#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000967#ifndef DISABLE_SPELLER
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000968 print1opt(_("-s [prog]"), _("--speller=[prog]"), N_("Enable alternate speller"));
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000969#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000970 print1opt("-t", "--tempfile", N_("Auto save on exit, don't prompt"));
971 print1opt("-v", "--view", N_("View (read only) mode"));
Chris Allegrettacef7fbb2001-04-02 05:36:08 +0000972#ifndef DISABLE_WRAPPING
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000973 print1opt("-w", "--nowrap", N_("Don't wrap long lines"));
Chris Allegrettacef7fbb2001-04-02 05:36:08 +0000974#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000975 print1opt("-x", "--nohelp", N_("Don't show help window"));
976 print1opt("-z", "--suspend", N_("Enable suspend"));
Chris Allegretta6df90f52002-07-19 01:08:59 +0000977
David Lawrence Ramsey22fac782004-08-05 15:16:19 +0000978 /* This is a special case. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000979 printf(" %s\t\t\t%s\n","-a, -b, -e, -f, -g, -j", _("(ignored, for Pico compatibility)"));
Chris Allegretta3fc5d572002-03-09 18:51:58 +0000980
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000981 exit(0);
982}
983
David Lawrence Ramsey0341b582002-08-21 16:10:37 +0000984void version(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000985{
Chris Allegrettac46dd812001-02-14 14:28:27 +0000986 printf(_(" GNU nano version %s (compiled %s, %s)\n"),
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000987 VERSION, __TIME__, __DATE__);
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000988 printf(_
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +0000989 (" Email: nano@nano-editor.org Web: http://www.nano-editor.org/"));
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000990 printf(_("\n Compiled options:"));
Chris Allegrettaff269f82000-12-01 18:46:01 +0000991
Chris Allegrettae6600372003-01-17 03:39:41 +0000992#ifndef ENABLE_NLS
993 printf(" --disable-nls");
994#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000995#ifdef DEBUG
996 printf(" --enable-debug");
997#endif
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000998#ifdef NANO_EXTRA
999 printf(" --enable-extra");
Jordi Mallach5285ca92002-01-17 12:40:33 +00001000#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001001#ifdef NANO_SMALL
1002 printf(" --enable-tiny");
1003#else
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001004#ifdef DISABLE_BROWSER
Chris Allegretta6636dc32001-01-05 05:41:07 +00001005 printf(" --disable-browser");
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001006#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001007#ifdef DISABLE_HELP
1008 printf(" --disable-help");
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001009#endif
1010#ifdef DISABLE_JUSTIFY
Chris Allegrettaff269f82000-12-01 18:46:01 +00001011 printf(" --disable-justify");
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001012#endif
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00001013#ifdef DISABLE_MOUSE
Chris Allegretta84de5522001-04-12 14:51:48 +00001014 printf(" --disable-mouse");
Chris Allegrettab7d00ef2000-12-18 05:36:51 +00001015#endif
Chris Allegrettae1f14522001-09-19 03:19:43 +00001016#ifdef DISABLE_OPERATINGDIR
1017 printf(" --disable-operatingdir");
1018#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001019#ifdef DISABLE_SPELLER
1020 printf(" --disable-speller");
1021#endif
1022#ifdef DISABLE_TABCOMP
1023 printf(" --disable-tabcomp");
1024#endif
Chris Allegretta84de5522001-04-12 14:51:48 +00001025#endif /* NANO_SMALL */
Chris Allegrettacef7fbb2001-04-02 05:36:08 +00001026#ifdef DISABLE_WRAPPING
1027 printf(" --disable-wrapping");
1028#endif
David Lawrence Ramseydc60b722002-10-25 16:08:53 +00001029#ifdef DISABLE_ROOTWRAP
1030 printf(" --disable-wrapping-as-root");
1031#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001032#ifdef ENABLE_COLOR
1033 printf(" --enable-color");
1034#endif
1035#ifdef ENABLE_MULTIBUFFER
1036 printf(" --enable-multibuffer");
1037#endif
1038#ifdef ENABLE_NANORC
1039 printf(" --enable-nanorc");
1040#endif
Chris Allegretta8a0de3b2000-11-24 20:45:14 +00001041#ifdef USE_SLANG
1042 printf(" --with-slang");
1043#endif
1044 printf("\n");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001045}
1046
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001047int no_help(void)
1048{
Chris Allegretta6df90f52002-07-19 01:08:59 +00001049 return ISSET(NO_HELP) ? 2 : 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001050}
1051
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001052void nano_disabled_msg(void)
Chris Allegrettaff269f82000-12-01 18:46:01 +00001053{
Chris Allegretta6df90f52002-07-19 01:08:59 +00001054 statusbar(_("Sorry, support for this function has been disabled"));
Chris Allegrettaff269f82000-12-01 18:46:01 +00001055}
1056
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001057#ifndef NANO_SMALL
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001058RETSIGTYPE cancel_fork(int signal)
1059{
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001060 if (kill(pid, SIGKILL) == -1)
1061 nperror("kill");
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001062}
1063
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001064/* Return TRUE on success. */
1065bool open_pipe(const char *command)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001066{
1067 int fd[2];
1068 FILE *f;
1069 struct sigaction oldaction, newaction;
David Lawrence Ramsey22fac782004-08-05 15:16:19 +00001070 /* Original and temporary handlers for
1071 * SIGINT. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001072 bool sig_failed = FALSE;
1073 /* sig_failed means that sigaction() failed without changing the
1074 * signal handlers.
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00001075 *
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001076 * We use this variable since it is important to put things back
1077 * when we finish, even if we get errors. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001078
1079 /* Make our pipes. */
1080
1081 if (pipe(fd) == -1) {
1082 statusbar(_("Could not pipe"));
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001083 return FALSE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001084 }
1085
1086 /* Fork a child. */
1087
1088 if ((pid = fork()) == 0) {
1089 close(fd[0]);
1090 dup2(fd[1], fileno(stdout));
1091 dup2(fd[1], fileno(stderr));
1092 /* If execl() returns at all, there was an error. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001093
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00001094 execl("/bin/sh", "sh", "-c", command, 0);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001095 exit(0);
1096 }
1097
1098 /* Else continue as parent. */
1099
1100 close(fd[1]);
1101
1102 if (pid == -1) {
1103 close(fd[0]);
1104 statusbar(_("Could not fork"));
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001105 return FALSE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001106 }
1107
1108 /* Before we start reading the forked command's output, we set
1109 * things up so that ^C will cancel the new process. */
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00001110
David Lawrence Ramseye608f942004-05-19 16:04:27 +00001111 /* Enable interpretation of the special control keys so that we get
1112 * SIGINT when Ctrl-C is pressed. */
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00001113 enable_signals();
1114
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00001115 if (sigaction(SIGINT, NULL, &newaction) == -1) {
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001116 sig_failed = TRUE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001117 nperror("sigaction");
1118 } else {
1119 newaction.sa_handler = cancel_fork;
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00001120 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001121 sig_failed = TRUE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001122 nperror("sigaction");
1123 }
1124 }
1125 /* Note that now oldaction is the previous SIGINT signal handler,
1126 * to be restored later. */
1127
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001128 f = fdopen(fd[0], "rb");
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001129 if (f == NULL)
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001130 nperror("fdopen");
David Lawrence Ramsey00d77982004-08-07 21:27:37 +00001131
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00001132 read_file(f, "stdin");
David Lawrence Ramsey22fac782004-08-05 15:16:19 +00001133 /* If multibuffer mode is on, we could be here in view mode. If so,
1134 * don't set the modification flag. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001135 if (!ISSET(VIEW_MODE))
1136 set_modified();
1137
1138 if (wait(NULL) == -1)
1139 nperror("wait");
1140
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001141 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001142 nperror("sigaction");
1143
David Lawrence Ramseye608f942004-05-19 16:04:27 +00001144 /* Disable interpretation of the special control keys so that we can
1145 * use Ctrl-C for other things. */
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00001146 disable_signals();
1147
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001148 return TRUE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001149}
David Lawrence Ramseya9cd41c2004-03-05 19:54:58 +00001150#endif /* !NANO_SMALL */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001151
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001152void do_verbatim_input(void)
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001153{
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001154 int *kbinput; /* Used to hold verbatim input. */
1155 size_t kbinput_len; /* Length of verbatim input. */
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001156
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001157 statusbar(_("Verbatim input"));
David Lawrence Ramsey48ae9862004-05-28 17:23:33 +00001158
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001159 /* Read in all the verbatim characters. */
1160 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001161
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001162 /* Display all the verbatim characters at once. */
1163 do_output(kbinput, kbinput_len);
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001164
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001165 free(kbinput);
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001166}
1167
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001168void do_backspace(void)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001169{
David Lawrence Ramseyd91ab6e2003-09-07 23:57:24 +00001170 if (current != fileage || current_x > 0) {
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00001171 do_left(FALSE);
David Lawrence Ramseyd91ab6e2003-09-07 23:57:24 +00001172 do_delete();
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001173 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001174}
1175
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001176void do_delete(void)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001177{
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001178 bool do_refresh = FALSE;
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00001179 /* Do we have to call edit_refresh(), or can we get away with
1180 * update_line()? */
1181
David Lawrence Ramsey40a6c8c2004-11-27 21:10:11 +00001182 assert(current != NULL && current->data != NULL &&
1183 current_x <= strlen(current->data));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001184
1185 placewewant = xplustabs();
1186
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001187 if (current->data[current_x] != '\0') {
1188 size_t linelen = strlen(current->data + current_x);
1189
1190 assert(current_x < strlen(current->data));
1191
1192 /* Let's get dangerous. */
David Lawrence Ramsey9eff7462003-09-16 02:04:00 +00001193 charmove(&current->data[current_x], &current->data[current_x + 1],
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001194 linelen);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001195
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001196 null_at(&current->data, linelen + current_x - 1);
1197#ifndef NANO_SMALL
1198 if (current_x < mark_beginx && mark_beginbuf == current)
1199 mark_beginx--;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001200#endif
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001201 } else if (current != filebot && (current->next != filebot ||
1202 current->data[0] == '\0')) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001203 /* We can delete the line before filebot only if it is blank: it
David Lawrence Ramsey32d19ce2004-05-24 05:05:07 +00001204 * becomes the new magicline then. */
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001205 filestruct *foo = current->next;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001206
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001207 assert(current_x == strlen(current->data));
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00001208
1209 /* If we're deleting at the end of a line, we need to call
1210 * edit_refresh(). */
1211 if (current->data[current_x] == '\0')
1212 do_refresh = TRUE;
1213
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001214 current->data = charealloc(current->data, current_x +
1215 strlen(foo->data) + 1);
1216 strcpy(current->data + current_x, foo->data);
1217#ifndef NANO_SMALL
1218 if (mark_beginbuf == current->next) {
1219 mark_beginx += current_x;
1220 mark_beginbuf = current;
1221 }
1222#endif
David Lawrence Ramseyb9775152004-03-19 02:15:42 +00001223 if (filebot == foo)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001224 filebot = current;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001225
1226 unlink_node(foo);
1227 delete_node(foo);
1228 renumber(current);
1229 totlines--;
David Lawrence Ramsey00d77982004-08-07 21:27:37 +00001230#ifndef DISABLE_WRAPPING
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001231 wrap_reset();
David Lawrence Ramsey00d77982004-08-07 21:27:37 +00001232#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001233 } else
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001234 return;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001235
1236 totsize--;
1237 set_modified();
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00001238
1239#ifdef ENABLE_COLOR
1240 /* If color syntaxes are turned on, we need to call
1241 * edit_refresh(). */
1242 if (ISSET(COLOR_SYNTAX))
1243 do_refresh = TRUE;
1244#endif
1245
1246 if (do_refresh)
1247 edit_refresh();
1248 else
1249 update_line(current, current_x);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001250}
1251
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001252void do_tab(void)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001253{
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001254 int kbinput = '\t';
1255 do_output(&kbinput, 1);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001256}
1257
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001258/* Someone hits return *gasp!* */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001259void do_enter(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001260{
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001261 filestruct *newnode = make_new_node(current);
1262 size_t extra = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001263
Chris Allegretta6df90f52002-07-19 01:08:59 +00001264 assert(current != NULL && current->data != NULL);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001265
Chris Allegrettaff989832001-09-17 13:48:00 +00001266#ifndef NANO_SMALL
Chris Allegretta6df90f52002-07-19 01:08:59 +00001267 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001268 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001269 /* If we are breaking the line in the indentation, the new
1270 * indentation should have only current_x characters, and
1271 * current_x should not change. */
1272 extra = indent_length(current->data);
1273 if (extra > current_x)
Chris Allegrettadab017e2002-04-23 10:56:06 +00001274 extra = current_x;
Chris Allegrettadab017e2002-04-23 10:56:06 +00001275 totsize += extra;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001276 }
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001277#endif
1278 newnode->data = charalloc(strlen(current->data + current_x) +
1279 extra + 1);
1280 strcpy(&newnode->data[extra], current->data + current_x);
1281#ifndef NANO_SMALL
1282 if (ISSET(AUTOINDENT))
1283 strncpy(newnode->data, current->data, extra);
1284#endif
1285 null_at(&current->data, current_x);
1286#ifndef NANO_SMALL
1287 if (current == mark_beginbuf && current_x < mark_beginx) {
1288 mark_beginbuf = newnode;
1289 mark_beginx += extra - current_x;
1290 }
1291#endif
1292 current_x = extra;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001293
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001294 if (current == filebot)
Chris Allegrettae3167732001-03-18 16:59:34 +00001295 filebot = newnode;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001296 splice_node(current, newnode, current->next);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001297
1298 totsize++;
1299 renumber(current);
Chris Allegrettae3167732001-03-18 16:59:34 +00001300 current = newnode;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001301
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001302 edit_refresh();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001303
1304 totlines++;
1305 set_modified();
Chris Allegrettab0ae3932000-06-15 23:39:14 +00001306 placewewant = xplustabs();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001307}
1308
Chris Allegrettaad3f4782001-10-02 03:54:13 +00001309#ifndef NANO_SMALL
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001310void do_next_word(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001311{
David Lawrence Ramsey86e851b2004-07-28 20:46:25 +00001312 size_t old_pww = placewewant;
David Lawrence Ramsey381d4832004-10-18 01:51:43 +00001313 const filestruct *old_current = current;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001314 assert(current != NULL && current->data != NULL);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001315
Chris Allegretta6df90f52002-07-19 01:08:59 +00001316 /* Skip letters in this word first. */
1317 while (current->data[current_x] != '\0' &&
David Lawrence Ramseyd9fb3e62004-10-21 22:49:51 +00001318 isalnum(current->data[current_x]))
Chris Allegretta6df90f52002-07-19 01:08:59 +00001319 current_x++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001320
Chris Allegretta6df90f52002-07-19 01:08:59 +00001321 for (; current != NULL; current = current->next) {
1322 while (current->data[current_x] != '\0' &&
David Lawrence Ramseyd9fb3e62004-10-21 22:49:51 +00001323 !isalnum(current->data[current_x]))
Chris Allegretta6df90f52002-07-19 01:08:59 +00001324 current_x++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001325
Chris Allegretta6df90f52002-07-19 01:08:59 +00001326 if (current->data[current_x] != '\0')
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001327 break;
1328
Chris Allegretta6df90f52002-07-19 01:08:59 +00001329 current_x = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001330 }
Chris Allegretta6df90f52002-07-19 01:08:59 +00001331 if (current == NULL)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001332 current = filebot;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001333
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001334 placewewant = xplustabs();
Chris Allegretta9e2934f2000-12-01 23:49:48 +00001335
David Lawrence Ramsey381d4832004-10-18 01:51:43 +00001336 /* Update the screen. */
1337 edit_redraw(old_current, old_pww);
Chris Allegretta6232d662002-05-12 19:52:15 +00001338}
1339
Chris Allegretta6df90f52002-07-19 01:08:59 +00001340/* The same thing for backwards. */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001341void do_prev_word(void)
Chris Allegretta76e291b2001-10-14 19:05:10 +00001342{
David Lawrence Ramsey86e851b2004-07-28 20:46:25 +00001343 size_t old_pww = placewewant;
David Lawrence Ramsey381d4832004-10-18 01:51:43 +00001344 const filestruct *old_current = current;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001345 assert(current != NULL && current->data != NULL);
Chris Allegretta76e291b2001-10-14 19:05:10 +00001346
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001347 current_x++;
1348
Chris Allegretta6df90f52002-07-19 01:08:59 +00001349 /* Skip letters in this word first. */
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001350 while (current_x > 0 && isalnum(current->data[current_x - 1]))
Chris Allegretta6df90f52002-07-19 01:08:59 +00001351 current_x--;
Chris Allegretta76e291b2001-10-14 19:05:10 +00001352
Chris Allegretta6df90f52002-07-19 01:08:59 +00001353 for (; current != NULL; current = current->prev) {
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001354 while (current_x > 0 && !isalnum(current->data[current_x - 1]))
Chris Allegretta6df90f52002-07-19 01:08:59 +00001355 current_x--;
Chris Allegretta76e291b2001-10-14 19:05:10 +00001356
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001357 if (current_x > 0)
Chris Allegretta6df90f52002-07-19 01:08:59 +00001358 break;
Chris Allegretta76e291b2001-10-14 19:05:10 +00001359
Chris Allegretta6df90f52002-07-19 01:08:59 +00001360 if (current->prev != NULL)
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001361 current_x = strlen(current->prev->data) + 1;
Chris Allegretta76e291b2001-10-14 19:05:10 +00001362 }
Chris Allegretta76e291b2001-10-14 19:05:10 +00001363
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001364 current_x--;
1365
David Lawrence Ramsey1b525e92004-05-24 02:35:02 +00001366 if (current == NULL) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00001367 current = fileage;
1368 current_x = 0;
David Lawrence Ramsey1b525e92004-05-24 02:35:02 +00001369 } else {
David Lawrence Ramseyd9fb3e62004-10-21 22:49:51 +00001370 while (current_x > 0 && isalnum(current->data[current_x - 1]))
David Lawrence Ramsey1b525e92004-05-24 02:35:02 +00001371 current_x--;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001372 }
1373
Chris Allegretta76e291b2001-10-14 19:05:10 +00001374 placewewant = xplustabs();
1375
David Lawrence Ramsey381d4832004-10-18 01:51:43 +00001376 /* Update the screen. */
1377 edit_redraw(old_current, old_pww);
Chris Allegretta6232d662002-05-12 19:52:15 +00001378}
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001379
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001380void do_mark(void)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001381{
David Lawrence Ramseyf03c78b2003-09-28 21:26:49 +00001382 TOGGLE(MARK_ISSET);
1383 if (ISSET(MARK_ISSET)) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001384 statusbar(_("Mark Set"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001385 mark_beginbuf = current;
1386 mark_beginx = current_x;
1387 } else {
1388 statusbar(_("Mark UNset"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001389 edit_refresh();
1390 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001391}
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00001392#endif /* !NANO_SMALL */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001393
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +00001394#ifndef DISABLE_WRAPPING
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001395void wrap_reset(void)
1396{
David Lawrence Ramsey85529b32004-06-22 15:38:47 +00001397 same_line_wrap = FALSE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001398}
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +00001399#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001400
Chris Allegrettacef7fbb2001-04-02 05:36:08 +00001401#ifndef DISABLE_WRAPPING
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001402/* We wrap the given line. Precondition: we assume the cursor has been
1403 * moved forward since the last typed character. Return value: whether
1404 * we wrapped. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001405bool do_wrap(filestruct *inptr)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001406{
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001407 size_t len = strlen(inptr->data);
1408 /* Length of the line we wrap. */
1409 size_t i = 0;
1410 /* Generic loop variable. */
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001411 ssize_t wrap_loc = -1;
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001412 /* Index of inptr->data where we wrap. */
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001413 ssize_t word_back = -1;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001414#ifndef NANO_SMALL
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001415 const char *indentation = NULL;
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001416 /* Indentation to prepend to the new line. */
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001417 size_t indent_len = 0; /* strlen(indentation) */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001418#endif
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001419 const char *after_break; /* Text after the wrap point. */
1420 size_t after_break_len; /* strlen(after_break) */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001421 bool wrapping = FALSE; /* Do we prepend to the next line? */
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001422 const char *wrap_line = NULL;
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001423 /* The next line, minus indentation. */
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001424 size_t wrap_line_len = 0; /* strlen(wrap_line) */
1425 char *newline = NULL; /* The line we create. */
1426 size_t new_line_len = 0; /* Eventual length of newline. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001427
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001428/* There are three steps. First, we decide where to wrap. Then, we
1429 * create the new wrap line. Finally, we clean up. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001430
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001431/* Step 1, finding where to wrap. We are going to add a new line
David Lawrence Ramsey89bb9372004-05-29 16:47:52 +00001432 * after a whitespace character. In this step, we set wrap_loc as the
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001433 * location of this replacement.
1434 *
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001435 * Where should we break the line? We need the last legal wrap point
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001436 * such that the last word before it ended at or before fill. If there
1437 * is no such point, we settle for the first legal wrap point.
1438 *
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001439 * A legal wrap point is a whitespace character that is not followed by
1440 * whitespace.
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001441 *
1442 * If there is no legal wrap point or we found the last character of the
1443 * line, we should return without wrapping.
1444 *
1445 * Note that the initial indentation does not count as a legal wrap
1446 * point if we are going to auto-indent!
1447 *
David Lawrence Ramsey8d911992004-05-29 17:05:52 +00001448 * Note that the code below could be optimized, by not calling
1449 * strnlenpt() so often. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001450
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001451#ifndef NANO_SMALL
Chris Allegretta6df90f52002-07-19 01:08:59 +00001452 if (ISSET(AUTOINDENT))
1453 i = indent_length(inptr->data);
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001454#endif
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001455 wrap_line = inptr->data + i;
David Lawrence Ramsey4e254102003-11-05 22:04:08 +00001456 for (; i < len; i++, wrap_line++) {
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001457 /* Record where the last word ended. */
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00001458 if (!isblank(*wrap_line))
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001459 word_back = i;
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001460 /* If we have found a legal wrap point and the current word
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001461 * extends too far, then we stop. */
David Lawrence Ramsey40a6c8c2004-11-27 21:10:11 +00001462 if (wrap_loc != -1 &&
1463 strnlenpt(inptr->data, word_back + 1) > fill)
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001464 break;
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001465 /* We record the latest legal wrap point. */
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00001466 if (word_back != i && !isblank(wrap_line[1]))
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001467 wrap_loc = i;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001468 }
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001469 if (i == len)
1470 return FALSE;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001471
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001472 /* Step 2, making the new wrap line. It will consist of indentation
1473 * + after_break + " " + wrap_line (although indentation and
1474 * wrap_line are conditional on flags and #defines). */
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001475
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001476 /* after_break is the text that will be moved to the next line. */
1477 after_break = inptr->data + wrap_loc + 1;
1478 after_break_len = len - wrap_loc - 1;
1479 assert(after_break_len == strlen(after_break));
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001480
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001481 /* new_line_len will later be increased by the lengths of indentation
1482 * and wrap_line. */
1483 new_line_len = after_break_len;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001484
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001485 /* We prepend the wrapped text to the next line, if the flag is set,
1486 * and there is a next line, and prepending would not make the line
1487 * too long. */
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +00001488 if (same_line_wrap && inptr->next) {
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001489 wrap_line = inptr->next->data;
1490 wrap_line_len = strlen(wrap_line);
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001491
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001492 /* +1 for the space between after_break and wrap_line. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001493 if ((new_line_len + 1 + wrap_line_len) <= fill) {
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001494 wrapping = TRUE;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001495 new_line_len += (1 + wrap_line_len);
1496 }
1497 }
Chris Allegretta56214c62001-09-27 02:46:53 +00001498
Chris Allegrettaff989832001-09-17 13:48:00 +00001499#ifndef NANO_SMALL
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001500 if (ISSET(AUTOINDENT)) {
Chris Allegretta81dea022002-09-21 02:19:45 +00001501 /* Indentation comes from the next line if wrapping, else from
1502 * this line. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001503 indentation = (wrapping ? wrap_line : inptr->data);
Chris Allegretta6df90f52002-07-19 01:08:59 +00001504 indent_len = indent_length(indentation);
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001505 if (wrapping)
Chris Allegretta81dea022002-09-21 02:19:45 +00001506 /* The wrap_line text should not duplicate indentation.
1507 * Note in this case we need not increase new_line_len. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001508 wrap_line += indent_len;
1509 else
1510 new_line_len += indent_len;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001511 }
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001512#endif
Adam Rogoyski1e9183f2001-03-13 18:36:03 +00001513
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001514 /* Now we allocate the new line and copy into it. */
1515 newline = charalloc(new_line_len + 1); /* +1 for \0 */
David Lawrence Ramsey85529b32004-06-22 15:38:47 +00001516 new_line_len = 0;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001517 *newline = '\0';
1518
1519#ifndef NANO_SMALL
Chris Allegretta6df90f52002-07-19 01:08:59 +00001520 if (ISSET(AUTOINDENT)) {
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001521 strncpy(newline, indentation, indent_len);
Chris Allegretta6df90f52002-07-19 01:08:59 +00001522 newline[indent_len] = '\0';
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001523 new_line_len = indent_len;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001524 }
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001525#endif
1526 strcat(newline, after_break);
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001527 new_line_len += after_break_len;
1528 /* We end the old line after wrap_loc. Note that this does not eat
1529 * the space. */
Chris Allegretta67ca2aa2002-09-19 23:19:34 +00001530 null_at(&inptr->data, wrap_loc + 1);
1531 totsize++;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001532 if (wrapping) {
Chris Allegretta67ca2aa2002-09-19 23:19:34 +00001533 /* In this case, totsize increases by 1 since we add a space
Chris Allegretta81dea022002-09-21 02:19:45 +00001534 * between after_break and wrap_line. If the line already ends
Chris Allegretta43000922002-09-21 15:41:33 +00001535 * in a tab or a space, we don't add a space and decrement
1536 * totsize to account for that. */
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001537 if (!isblank(newline[new_line_len - 1]))
Chris Allegretta81dea022002-09-21 02:19:45 +00001538 strcat(newline, " ");
1539 else
1540 totsize--;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001541 strcat(newline, wrap_line);
1542 free(inptr->next->data);
1543 inptr->next->data = newline;
1544 } else {
1545 filestruct *temp = (filestruct *)nmalloc(sizeof(filestruct));
Chris Allegretta6df90f52002-07-19 01:08:59 +00001546
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001547 /* In this case, the file size changes by +1 for the new line,
1548 * and +indent_len for the new indentation. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001549#ifndef NANO_SMALL
1550 totsize += indent_len;
1551#endif
1552 totlines++;
1553 temp->data = newline;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001554 temp->prev = inptr;
1555 temp->next = inptr->next;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001556 temp->prev->next = temp;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001557 /* If temp->next is NULL, then temp is the last line of the
1558 * file, so we must set filebot. */
1559 if (temp->next != NULL)
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001560 temp->next->prev = temp;
1561 else
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001562 filebot = temp;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001563 }
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001564
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001565 /* Step 3, clean up. Here we reposition the cursor and mark, and do
1566 * some other sundry things. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001567
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001568 /* Later wraps of this line will be prepended to the next line. */
David Lawrence Ramsey85529b32004-06-22 15:38:47 +00001569 same_line_wrap = TRUE;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001570
1571 /* Each line knows its line number. We recalculate these if we
1572 * inserted a new line. */
1573 if (!wrapping)
1574 renumber(inptr);
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001575
Chris Allegretta6df90f52002-07-19 01:08:59 +00001576 /* If the cursor was after the break point, we must move it. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001577 if (current_x > wrap_loc) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00001578 current = current->next;
1579 current_x -=
Chris Allegrettaff989832001-09-17 13:48:00 +00001580#ifndef NANO_SMALL
Chris Allegretta6df90f52002-07-19 01:08:59 +00001581 -indent_len +
Chris Allegrettaff989832001-09-17 13:48:00 +00001582#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00001583 wrap_loc + 1;
1584 wrap_reset();
1585 placewewant = xplustabs();
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001586 }
1587
Chris Allegretta6df90f52002-07-19 01:08:59 +00001588#ifndef NANO_SMALL
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001589 /* If the mark was on this line after the wrap point, we move it
1590 * down. If it was on the next line and we wrapped, we move it
Chris Allegretta6df90f52002-07-19 01:08:59 +00001591 * right. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001592 if (mark_beginbuf == inptr && mark_beginx > wrap_loc) {
1593 mark_beginbuf = inptr->next;
Chris Allegrettadffa2072002-07-24 01:02:26 +00001594 mark_beginx -= wrap_loc - indent_len + 1;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001595 } else if (wrapping && mark_beginbuf == inptr->next)
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001596 mark_beginx += after_break_len;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001597#endif /* !NANO_SMALL */
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001598
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001599 return TRUE;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001600}
Chris Allegretta6df90f52002-07-19 01:08:59 +00001601#endif /* !DISABLE_WRAPPING */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001602
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001603#ifndef DISABLE_SPELLER
David Lawrence Ramseyaaad3af2003-08-31 16:44:10 +00001604/* A word is misspelled in the file. Let the user replace it. We
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001605 * return FALSE if the user cancels. */
1606bool do_int_spell_fix(const char *word)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001607{
David Lawrence Ramseyd4ca9f22004-10-26 21:14:56 +00001608 char *save_search, *save_replace;
David Lawrence Ramsey2eb74592004-10-21 16:25:44 +00001609 size_t current_x_save = current_x, pww_save = placewewant;
David Lawrence Ramseyd4ca9f22004-10-26 21:14:56 +00001610 filestruct *edittop_save = edittop, *current_save = current;
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001611 /* Save where we are. */
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001612 bool canceled = FALSE;
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001613 /* The return value. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001614 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramsey49d5c1b2004-10-11 13:55:33 +00001615#ifndef NANO_SMALL
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001616 bool reverse_search_set = ISSET(REVERSE_SEARCH);
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001617#endif
David Lawrence Ramseyb112fe62004-10-09 17:15:46 +00001618#ifdef HAVE_REGEX_H
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001619 bool regexp_set = ISSET(USE_REGEXP);
1620#endif
David Lawrence Ramsey9cf7b282004-10-15 16:35:34 +00001621#ifndef NANO_SMALL
1622 bool old_mark_set = ISSET(MARK_ISSET);
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001623 bool added_magicline = FALSE;
1624 /* Whether we added a magicline after filebot. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001625 bool right_side_up = FALSE;
1626 /* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark,
1627 * FALSE if (current, current_x) is. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001628 filestruct *top, *bot;
David Lawrence Ramsey46c604a2004-11-05 23:55:58 +00001629 size_t top_x, bot_x;
David Lawrence Ramsey9cf7b282004-10-15 16:35:34 +00001630#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001631
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001632 /* Make sure spell-check is case sensitive. */
Chris Allegretta1d8fa1f2002-12-10 00:53:21 +00001633 SET(CASE_SENSITIVE);
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001634
David Lawrence Ramsey49d5c1b2004-10-11 13:55:33 +00001635#ifndef NANO_SMALL
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001636 /* Make sure spell-check goes forward only. */
1637 UNSET(REVERSE_SEARCH);
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001638#endif
David Lawrence Ramseyb112fe62004-10-09 17:15:46 +00001639#ifdef HAVE_REGEX_H
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001640 /* Make sure spell-check doesn't use regular expressions. */
1641 UNSET(USE_REGEXP);
1642#endif
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001643
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001644 /* Save the current search/replace strings. */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001645 search_init_globals();
Chris Allegretta6df90f52002-07-19 01:08:59 +00001646 save_search = last_search;
1647 save_replace = last_replace;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001648
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001649 /* Set the search/replace strings to the misspelled word. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00001650 last_search = mallocstrcpy(NULL, word);
1651 last_replace = mallocstrcpy(NULL, word);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001652
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001653#ifndef NANO_SMALL
1654 if (old_mark_set) {
David Lawrence Ramseyf978f042004-11-04 16:45:48 +00001655 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001656 * contains only the marked text, keep track of whether the text
1657 * will have a magicline added when we're done correcting
1658 * misspelled words, and turn the mark off. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001659 mark_order((const filestruct **)&top, &top_x,
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001660 (const filestruct **)&bot, &bot_x, &right_side_up);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001661 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001662 added_magicline = (filebot->data[0] != '\0');
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001663 UNSET(MARK_ISSET);
1664 }
1665#endif
1666
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001667 /* Start from the top of the file. */
David Lawrence Ramsey2cc2a572004-10-18 02:06:53 +00001668 edittop = fileage;
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001669 current = fileage;
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001670 current_x = (size_t)-1;
David Lawrence Ramsey2eb74592004-10-21 16:25:44 +00001671 placewewant = 0;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001672
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001673 /* Find the first whole-word occurrence of word. */
David Lawrence Ramseye5e88fd2004-10-31 13:20:30 +00001674 findnextstr_wrap_reset();
David Lawrence Ramsey77b284a2004-10-27 02:21:01 +00001675 while (findnextstr(TRUE, TRUE, FALSE, fileage, 0, word, NULL)) {
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001676 if (is_whole_word(current_x, current->data, word)) {
1677 edit_refresh();
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001678
Chris Allegretta6df90f52002-07-19 01:08:59 +00001679 do_replace_highlight(TRUE, word);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001680
David Lawrence Ramsey4b7e3c32004-10-15 16:25:56 +00001681 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001682 canceled = (statusq(FALSE, spell_list, word,
Chris Allegretta7662c862003-01-13 01:35:15 +00001683#ifndef NANO_SMALL
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001684 NULL,
Chris Allegretta7662c862003-01-13 01:35:15 +00001685#endif
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001686 _("Edit a replacement")) == -1);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001687
Chris Allegretta6df90f52002-07-19 01:08:59 +00001688 do_replace_highlight(FALSE, word);
Chris Allegretta1bc0c7e2002-01-08 15:00:24 +00001689
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001690 if (!canceled && strcmp(word, answer) != 0) {
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001691 current_x--;
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001692 do_replace_loop(word, current, &current_x, TRUE,
1693 &canceled);
Chris Allegretta1bc0c7e2002-01-08 15:00:24 +00001694 }
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001695
1696 break;
Chris Allegretta80838272001-12-02 06:03:22 +00001697 }
David Lawrence Ramsey53752e82004-10-18 22:19:22 +00001698 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001699
David Lawrence Ramsey46c604a2004-11-05 23:55:58 +00001700#ifndef NANO_SMALL
1701 if (old_mark_set) {
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +00001702 /* If the mark was on and we added a magicline, remove it
1703 * now. */
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001704 if (added_magicline)
1705 remove_magicline();
David Lawrence Ramsey46c604a2004-11-05 23:55:58 +00001706
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001707 /* Put the beginning and the end of the mark at the beginning
1708 * and the end of the spell-checked text. */
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001709 if (fileage == filebot)
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001710 bot_x += top_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001711 if (right_side_up) {
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001712 mark_beginx = top_x;
1713 current_x_save = bot_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001714 } else {
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001715 current_x_save = top_x;
1716 mark_beginx = bot_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001717 }
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001718
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +00001719 /* Unpartition the filestruct so that it contains all the text
1720 * again, and turn the mark back on. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +00001721 unpartition_filestruct(&filepart);
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001722 SET(MARK_ISSET);
David Lawrence Ramsey46c604a2004-11-05 23:55:58 +00001723 }
1724#endif
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001725
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001726 /* Restore the search/replace strings. */
1727 free(last_search);
1728 last_search = save_search;
1729 free(last_replace);
1730 last_replace = save_replace;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001731
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001732 /* Restore where we were. */
David Lawrence Ramsey2cc2a572004-10-18 02:06:53 +00001733 edittop = edittop_save;
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001734 current = current_save;
1735 current_x = current_x_save;
David Lawrence Ramsey2eb74592004-10-21 16:25:44 +00001736 placewewant = pww_save;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001737
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001738 /* Restore case sensitivity setting. */
1739 if (!case_sens_set)
1740 UNSET(CASE_SENSITIVE);
1741
David Lawrence Ramsey49d5c1b2004-10-11 13:55:33 +00001742#ifndef NANO_SMALL
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001743 /* Restore search/replace direction. */
Chris Allegretta23b74b22002-01-21 20:32:22 +00001744 if (reverse_search_set)
1745 SET(REVERSE_SEARCH);
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001746#endif
David Lawrence Ramseyb112fe62004-10-09 17:15:46 +00001747#ifdef HAVE_REGEX_H
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001748 /* Restore regular expression usage setting. */
1749 if (regexp_set)
1750 SET(USE_REGEXP);
1751#endif
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001752
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001753 return !canceled;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001754}
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001755
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001756/* Integrated spell checking using 'spell' program. Return value: NULL
1757 * for normal termination, otherwise the error string. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001758const char *do_int_speller(const char *tempfile_name)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001759{
Chris Allegretta271e9722000-11-10 18:15:43 +00001760 char *read_buff, *read_buff_ptr, *read_buff_word;
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001761 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001762 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001763 pid_t pid_spell, pid_sort, pid_uniq;
1764 int spell_status, sort_status, uniq_status;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001765
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001766 /* Create all three pipes up front. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001767 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || pipe(uniq_fd) == -1)
1768 return _("Could not create pipe");
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001769
Chris Allegretta2a7a9a22002-12-10 00:16:27 +00001770 statusbar(_("Creating misspelled word list, please wait..."));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001771
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001772 /* A new process to run spell in. */
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001773 if ((pid_spell = fork()) == 0) {
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001774
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001775 /* Child continues (i.e, future spell process). */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001776
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001777 close(spell_fd[0]);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001778
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001779 /* Replace the standard input with the temp file. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001780 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1781 goto close_pipes_and_exit;
1782
1783 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1784 goto close_pipes_and_exit;
1785
Chris Allegretta271e9722000-11-10 18:15:43 +00001786 close(tempfile_fd);
1787
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001788 /* Send spell's standard output to the pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001789 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1790 goto close_pipes_and_exit;
Chris Allegretta271e9722000-11-10 18:15:43 +00001791
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001792 close(spell_fd[1]);
Chris Allegretta271e9722000-11-10 18:15:43 +00001793
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001794 /* Start spell program; we are using PATH. */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001795 execlp("spell", "spell", NULL);
1796
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001797 /* Should not be reached, if spell is found. */
Chris Allegretta271e9722000-11-10 18:15:43 +00001798 exit(1);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001799 }
1800
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001801 /* Parent continues here. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001802 close(spell_fd[1]);
1803
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001804 /* A new process to run sort in. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001805 if ((pid_sort = fork()) == 0) {
1806
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001807 /* Child continues (i.e, future spell process). Replace the
1808 * standard input with the standard output of the old pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001809 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1810 goto close_pipes_and_exit;
1811
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001812 close(spell_fd[0]);
1813
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001814 /* Send sort's standard output to the new pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001815 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1816 goto close_pipes_and_exit;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001817
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001818 close(sort_fd[1]);
1819
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001820 /* Start sort program. Use -f to remove mixed case without
1821 * having to have ANOTHER pipe for tr. If this isn't portable,
1822 * let me know. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001823 execlp("sort", "sort", "-f", NULL);
1824
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001825 /* Should not be reached, if sort is found. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001826 exit(1);
1827 }
1828
Chris Allegretta2d5fc3a2003-01-16 03:11:23 +00001829 close(spell_fd[0]);
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001830 close(sort_fd[1]);
1831
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001832 /* A new process to run uniq in. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001833 if ((pid_uniq = fork()) == 0) {
1834
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001835 /* Child continues (i.e, future uniq process). Replace the
1836 * standard input with the standard output of the old pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001837 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
1838 goto close_pipes_and_exit;
1839
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001840 close(sort_fd[0]);
1841
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001842 /* Send uniq's standard output to the new pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001843 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1844 goto close_pipes_and_exit;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001845
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001846 close(uniq_fd[1]);
1847
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001848 /* Start uniq program; we are using PATH. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001849 execlp("uniq", "uniq", NULL);
1850
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001851 /* Should not be reached, if uniq is found. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001852 exit(1);
1853 }
1854
Chris Allegretta2d5fc3a2003-01-16 03:11:23 +00001855 close(sort_fd[0]);
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001856 close(uniq_fd[1]);
Chris Allegretta271e9722000-11-10 18:15:43 +00001857
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001858 /* Child process was not forked successfully. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001859 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
1860 close(uniq_fd[0]);
Chris Allegretta334a9402002-12-16 04:25:53 +00001861 return _("Could not fork");
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001862 }
1863
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001864 /* Get system pipe buffer size. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001865 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
1866 close(uniq_fd[0]);
Chris Allegretta334a9402002-12-16 04:25:53 +00001867 return _("Could not get size of pipe buffer");
Chris Allegretta271e9722000-11-10 18:15:43 +00001868 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001869
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001870 /* Read in the returned spelling errors. */
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001871 read_buff_read = 0;
1872 read_buff_size = pipe_buff_size + 1;
1873 read_buff = read_buff_ptr = charalloc(read_buff_size);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001874
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001875 while ((bytesread = read(uniq_fd[0], read_buff_ptr, pipe_buff_size)) > 0) {
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001876 read_buff_read += bytesread;
1877 read_buff_size += pipe_buff_size;
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00001878 read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size);
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001879 read_buff_ptr += read_buff_read;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001880
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001881 }
Chris Allegretta271e9722000-11-10 18:15:43 +00001882
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001883 *read_buff_ptr = (char)NULL;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001884 close(uniq_fd[0]);
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001885
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001886 /* Process the spelling errors. */
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001887 read_buff_word = read_buff_ptr = read_buff;
1888
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001889 while (*read_buff_ptr != '\0') {
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001890
1891 if ((*read_buff_ptr == '\n') || (*read_buff_ptr == '\r')) {
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001892 *read_buff_ptr = (char)NULL;
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001893 if (read_buff_word != read_buff_ptr) {
1894 if (!do_int_spell_fix(read_buff_word)) {
1895 read_buff_word = read_buff_ptr;
1896 break;
1897 }
1898 }
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001899 read_buff_word = read_buff_ptr + 1;
1900 }
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001901 read_buff_ptr++;
1902 }
1903
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001904 /* Special case where last word doesn't end with \n or \r. */
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001905 if (read_buff_word != read_buff_ptr)
1906 do_int_spell_fix(read_buff_word);
1907
Chris Allegretta271e9722000-11-10 18:15:43 +00001908 free(read_buff);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001909 replace_abort();
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001910 edit_refresh();
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001911
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001912 /* Process end of spell process. */
Chris Allegretta334a9402002-12-16 04:25:53 +00001913 waitpid(pid_spell, &spell_status, 0);
1914 waitpid(pid_sort, &sort_status, 0);
1915 waitpid(pid_uniq, &uniq_status, 0);
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001916
Chris Allegretta334a9402002-12-16 04:25:53 +00001917 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
1918 return _("Error invoking \"spell\"");
Chris Allegretta271e9722000-11-10 18:15:43 +00001919
Chris Allegretta334a9402002-12-16 04:25:53 +00001920 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
1921 return _("Error invoking \"sort -f\"");
1922
1923 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
1924 return _("Error invoking \"uniq\"");
1925
1926 /* Otherwise... */
1927 return NULL;
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001928
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001929 close_pipes_and_exit:
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001930
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001931 /* Don't leak any handles. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001932 close(tempfile_fd);
1933 close(spell_fd[0]);
1934 close(spell_fd[1]);
1935 close(sort_fd[0]);
1936 close(sort_fd[1]);
1937 close(uniq_fd[0]);
1938 close(uniq_fd[1]);
1939 exit(1);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001940}
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001941
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001942/* External spell checking. Return value: NULL for normal termination,
1943 * otherwise the error string. */
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00001944const char *do_alt_speller(char *tempfile_name)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001945{
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001946 int alt_spell_status, lineno_save = current->lineno;
1947 size_t current_x_save = current_x, pww_save = placewewant;
1948 int current_y_save = current_y;
Chris Allegretta271e9722000-11-10 18:15:43 +00001949 pid_t pid_spell;
Chris Allegretta169ee842001-01-26 01:57:32 +00001950 char *ptr;
1951 static int arglen = 3;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00001952 static char **spellargs = NULL;
1953 FILE *f;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001954#ifndef NANO_SMALL
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00001955 bool old_mark_set = ISSET(MARK_ISSET);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001956 bool added_magicline = FALSE;
1957 /* Whether we added a magicline after filebot. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001958 bool right_side_up = FALSE;
1959 /* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark,
1960 * FALSE if (current, current_x) is. */
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001961 filestruct *top, *bot;
1962 size_t top_x, bot_x;
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001963 int mbb_lineno_save = 0;
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001964 /* We're going to close the current file, and open the output of
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001965 * the alternate spell command. The line that mark_beginbuf
1966 * points to will be freed, so we save the line number and
1967 * restore afterwards. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001968 long old_totsize = totsize;
1969 /* Our saved value of totsize, used when we spell-check a marked
1970 * selection. */
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001971
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00001972 if (old_mark_set) {
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001973 /* If the mark is on, save the number of the line it starts on,
1974 * and then turn the mark off. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001975 mbb_lineno_save = mark_beginbuf->lineno;
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001976 UNSET(MARK_ISSET);
1977 }
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001978#endif
1979
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001980 endwin();
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001981
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001982 /* Set up an argument list to pass execvp(). */
Chris Allegrettae434b452001-01-27 19:25:00 +00001983 if (spellargs == NULL) {
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00001984 spellargs = (char **)nmalloc(arglen * sizeof(char *));
Chris Allegretta271e9722000-11-10 18:15:43 +00001985
Chris Allegrettae434b452001-01-27 19:25:00 +00001986 spellargs[0] = strtok(alt_speller, " ");
1987 while ((ptr = strtok(NULL, " ")) != NULL) {
1988 arglen++;
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00001989 spellargs = (char **)nrealloc(spellargs, arglen * sizeof(char *));
Chris Allegrettae434b452001-01-27 19:25:00 +00001990 spellargs[arglen - 3] = ptr;
Chris Allegretta169ee842001-01-26 01:57:32 +00001991 }
Chris Allegrettae434b452001-01-27 19:25:00 +00001992 spellargs[arglen - 1] = NULL;
1993 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001994 spellargs[arglen - 2] = tempfile_name;
Chris Allegrettae434b452001-01-27 19:25:00 +00001995
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001996 /* Start a new process for the alternate speller. */
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001997 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001998 /* Start alternate spell program; we are using PATH. */
Chris Allegretta169ee842001-01-26 01:57:32 +00001999 execvp(spellargs[0], spellargs);
Chris Allegretta271e9722000-11-10 18:15:43 +00002000
2001 /* Should not be reached, if alternate speller is found!!! */
Chris Allegretta271e9722000-11-10 18:15:43 +00002002 exit(1);
2003 }
2004
2005 /* Could not fork?? */
Chris Allegretta271e9722000-11-10 18:15:43 +00002006 if (pid_spell < 0)
Chris Allegretta334a9402002-12-16 04:25:53 +00002007 return _("Could not fork");
Chris Allegretta271e9722000-11-10 18:15:43 +00002008
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002009 /* Wait for alternate speller to complete. */
Chris Allegretta271e9722000-11-10 18:15:43 +00002010 wait(&alt_spell_status);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002011
Chris Allegretta334a9402002-12-16 04:25:53 +00002012 if (!WIFEXITED(alt_spell_status) || WEXITSTATUS(alt_spell_status) != 0) {
2013 char *altspell_error = NULL;
2014 char *invoke_error = _("Could not invoke \"%s\"");
2015 int msglen = strlen(invoke_error) + strlen(alt_speller) + 2;
2016
2017 altspell_error = charalloc(msglen);
2018 snprintf(altspell_error, msglen, invoke_error, alt_speller);
2019 return altspell_error;
2020 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002021
Chris Allegretta8f6c0692000-07-19 01:16:18 +00002022 refresh();
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002023
David Lawrence Ramsey439fbe32004-10-16 04:56:34 +00002024 /* Restore the terminal to its previous state. */
2025 terminal_init();
2026
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002027#ifndef NANO_SMALL
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00002028 if (old_mark_set) {
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002029 long part_totsize;
2030
2031 /* If the mark was on, partition the filestruct so that it
2032 * contains only the marked text, and keep track of whether the
2033 * temp file (which should contain the spell-checked marked
2034 * text) will have a magicline added when it's reloaded. */
2035 mark_order((const filestruct **)&top, &top_x,
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00002036 (const filestruct **)&bot, &bot_x, &right_side_up);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002037 filepart = partition_filestruct(top, top_x, bot, bot_x);
2038 added_magicline = (filebot->data[0] != '\0');
2039
David Lawrence Ramsey3f358642004-11-22 16:04:18 +00002040 /* Get the number of characters in the marked text, and subtract
2041 * it from the saved value of totsize. Note that we don't need
2042 * to save totlines. */
2043 get_totals(top, bot, NULL, &part_totsize);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002044 old_totsize -= part_totsize;
2045 }
2046#endif
2047
2048 /* Reinitialize the filestruct. */
2049 free_filestruct(fileage);
2050 global_init(TRUE);
2051
2052 /* Reload the temp file. Do what load_buffer() would do, except for
2053 * making a new buffer for the temp file if multibuffer support is
2054 * available. */
2055 open_file(tempfile_name, FALSE, &f);
2056 read_file(f, tempfile_name);
2057 current = fileage;
2058
2059#ifndef NANO_SMALL
2060 if (old_mark_set) {
David Lawrence Ramsey15398372004-11-05 15:03:12 +00002061 filestruct *top_save = fileage;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002062
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +00002063 /* If the mark was on and we added a magicline, remove it
2064 * now. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002065 if (added_magicline)
2066 remove_magicline();
2067
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002068 /* Put the beginning and the end of the mark at the beginning
2069 * and the end of the spell-checked text. */
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002070 if (fileage == filebot)
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00002071 bot_x += top_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002072 if (right_side_up) {
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00002073 mark_beginx = top_x;
2074 current_x_save = bot_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002075 } else {
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00002076 current_x_save = top_x;
2077 mark_beginx = bot_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002078 }
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00002079
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +00002080 /* Unpartition the filestruct so that it contains all the text
2081 * again. Note that we've replaced the marked text originally
2082 * in the partition with the spell-checked marked text in the
2083 * temp file. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +00002084 unpartition_filestruct(&filepart);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002085
2086 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey3f358642004-11-22 16:04:18 +00002087 * partition. Also set totlines to the new number of lines in
2088 * the file, add the number of characters in the spell-checked
2089 * marked text to the saved value of totsize, and then make that
2090 * saved value the actual value. */
David Lawrence Ramsey15398372004-11-05 15:03:12 +00002091 renumber(top_save);
David Lawrence Ramsey3f358642004-11-22 16:04:18 +00002092 totlines = filebot->lineno;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002093 old_totsize += totsize;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002094 totsize = old_totsize;
2095
2096 /* Assign mark_beginbuf to the line where the mark began
2097 * before. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00002098 do_gotopos(mbb_lineno_save, mark_beginx, current_y_save, 0);
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002099 mark_beginbuf = current;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00002100
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002101 /* Assign mark_beginx to the location in mark_beginbuf where the
2102 * mark began before, adjusted for any shortening of the
2103 * line. */
2104 mark_beginx = current_x;
2105
2106 /* Turn the mark back on. */
2107 SET(MARK_ISSET);
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002108 }
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00002109#endif
2110
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002111 /* Go back to the old position, mark the file as modified, and make
2112 * sure that the titlebar is refreshed. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00002113 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002114 set_modified();
Chris Allegrettae1f14522001-09-19 03:19:43 +00002115 clearok(topwin, FALSE);
2116 titlebar(NULL);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00002117
Chris Allegretta334a9402002-12-16 04:25:53 +00002118 return NULL;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002119}
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002120
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002121void do_spell(void)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002122{
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002123 int i;
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002124 char *temp = safe_tempnam(0, "nano.");
2125 const char *spell_msg;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002126
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002127 if (temp == NULL) {
2128 statusbar(_("Could not create temp file: %s"), strerror(errno));
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002129 return;
Chris Allegretta271e9722000-11-10 18:15:43 +00002130 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002131
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002132#ifndef NANO_SMALL
2133 if (ISSET(MARK_ISSET))
David Lawrence Ramsey2f0d03b2004-05-28 00:15:28 +00002134 i = write_marked(temp, TRUE, FALSE);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002135 else
2136#endif
David Lawrence Ramsey2f0d03b2004-05-28 00:15:28 +00002137 i = write_file(temp, TRUE, FALSE, FALSE);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002138
2139 if (i == -1) {
David Lawrence Ramsey95e39e52004-08-12 02:52:14 +00002140 statusbar(_("Error writing temp file: %s"), strerror(errno));
Chris Allegrettabef12972002-03-06 03:30:40 +00002141 free(temp);
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002142 return;
Chris Allegretta3dbb2782000-12-02 04:36:50 +00002143 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002144
Chris Allegrettae1f14522001-09-19 03:19:43 +00002145#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002146 /* Update the current open_files entry before spell-checking, in
2147 * case any problems occur. */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002148 add_open_file(TRUE);
Chris Allegrettae1f14522001-09-19 03:19:43 +00002149#endif
2150
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002151 spell_msg = alt_speller != NULL ? do_alt_speller(temp) :
2152 do_int_speller(temp);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002153 unlink(temp);
Chris Allegrettafdcb9e92003-02-12 02:52:04 +00002154 free(temp);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002155
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002156 if (spell_msg != NULL)
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002157 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2158 strerror(errno));
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002159 else
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002160 statusbar(_("Finished checking spelling"));
Chris Allegretta67105eb2000-07-03 03:18:32 +00002161}
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00002162#endif /* !DISABLE_SPELLER */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002163
David Lawrence Ramsey44e7f822004-03-30 04:17:10 +00002164#if !defined(NANO_SMALL) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey8d911992004-05-29 17:05:52 +00002165/* The "indentation" of a line is the whitespace between the quote part
2166 * and the non-whitespace of the line. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002167size_t indent_length(const char *line)
2168{
Chris Allegretta6df90f52002-07-19 01:08:59 +00002169 size_t len = 0;
2170
2171 assert(line != NULL);
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00002172 while (isblank(*line)) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00002173 line++;
2174 len++;
2175 }
2176 return len;
2177}
David Lawrence Ramsey44e7f822004-03-30 04:17:10 +00002178#endif /* !NANO_SMALL || !DISABLE_JUSTIFY */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002179
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002180#ifndef DISABLE_JUSTIFY
David Lawrence Ramseyf613ca72004-05-13 22:23:58 +00002181/* justify_format() replaces Tab by Space and multiple spaces by 1
David Lawrence Ramseyfd73c462004-09-11 21:28:36 +00002182 * (except it maintains 2 after a non-repeated character in punct
2183 * followed by a character in brackets). Note that the terminating \0
2184 * counts as a space.
Chris Allegretta6df90f52002-07-19 01:08:59 +00002185 *
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002186 * justify_format() might make line->data shorter, and change the actual
2187 * pointer with null_at().
Chris Allegretta6df90f52002-07-19 01:08:59 +00002188 *
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00002189 * justify_format() will not look at the first skip characters of line.
David Lawrence Ramseyf613ca72004-05-13 22:23:58 +00002190 * skip should be at most strlen(line->data). The character at
2191 * line[skip + 1] must not be whitespace. */
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002192void justify_format(filestruct *line, size_t skip)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002193{
Chris Allegretta6df90f52002-07-19 01:08:59 +00002194 char *back, *front;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002195
Chris Allegretta6df90f52002-07-19 01:08:59 +00002196 /* These four asserts are assumptions about the input data. */
2197 assert(line != NULL);
2198 assert(line->data != NULL);
Chris Allegrettad8451932003-03-11 03:50:40 +00002199 assert(skip < strlen(line->data));
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00002200 assert(!isblank(line->data[skip]));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002201
Chris Allegretta6df90f52002-07-19 01:08:59 +00002202 back = line->data + skip;
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002203 for (front = back; ; front++) {
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002204 bool remove_space = FALSE;
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002205 /* Do we want to remove this space? */
2206
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002207 if (*front == '\t')
Chris Allegretta6df90f52002-07-19 01:08:59 +00002208 *front = ' ';
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002209
David Lawrence Ramsey021960d2004-05-13 23:19:01 +00002210 /* These tests are safe since line->data + skip is not a
2211 * space. */
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002212 if ((*front == '\0' || *front == ' ') && *(front - 1) == ' ') {
David Lawrence Ramseyfd73c462004-09-11 21:28:36 +00002213 const char *bob = back - 2;
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002214
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002215 remove_space = TRUE;
David Lawrence Ramseyfd73c462004-09-11 21:28:36 +00002216 for (; bob >= line->data + skip; bob--) {
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002217 if (strchr(punct, *bob) != NULL) {
David Lawrence Ramseyfd73c462004-09-11 21:28:36 +00002218 /* If this character is in punct, don't remove the
2219 * space unless this character and the character
2220 * before it are the same. */
2221 remove_space = (bob > line->data + skip &&
2222 *bob == *(bob - 1));
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002223 break;
2224 }
2225 if (strchr(brackets, *bob) == NULL)
2226 break;
2227 }
2228 }
2229
2230 if (remove_space) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00002231 /* Now *front is a space we want to remove. We do that by
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002232 * simply failing to assign it to *back. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002233#ifndef NANO_SMALL
2234 if (mark_beginbuf == line && back - line->data < mark_beginx)
2235 mark_beginx--;
2236#endif
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002237 if (*front == '\0')
2238 *(back - 1) = '\0';
Chris Allegretta6df90f52002-07-19 01:08:59 +00002239 } else {
2240 *back = *front;
2241 back++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002242 }
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002243 if (*front == '\0')
2244 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002245 }
Chris Allegretta6df90f52002-07-19 01:08:59 +00002246
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002247 back--;
Chris Allegrettad8451932003-03-11 03:50:40 +00002248 assert(*back == '\0' && *front == '\0');
Chris Allegretta6df90f52002-07-19 01:08:59 +00002249
Chris Allegretta6df90f52002-07-19 01:08:59 +00002250 /* Now back is the new end of line->data. */
2251 if (back != front) {
Chris Allegrettad8451932003-03-11 03:50:40 +00002252 totsize -= front - back;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002253 null_at(&line->data, back - line->data);
2254#ifndef NANO_SMALL
2255 if (mark_beginbuf == line && back - line->data < mark_beginx)
2256 mark_beginx = back - line->data;
2257#endif
2258 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002259}
Chris Allegretta6df90f52002-07-19 01:08:59 +00002260
2261/* The "quote part" of a line is the largest initial substring matching
2262 * the quote string. This function returns the length of the quote part
2263 * of the given line.
2264 *
2265 * Note that if !HAVE_REGEX_H then we match concatenated copies of
2266 * quotestr. */
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002267size_t quote_length(const char *line)
2268{
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002269#ifdef HAVE_REGEX_H
2270 regmatch_t matches;
2271 int rc = regexec(&quotereg, line, 1, &matches, 0);
2272
2273 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
2274 return 0;
2275 /* matches.rm_so should be 0, since the quote string should start
2276 * with the caret ^. */
2277 return matches.rm_eo;
2278#else /* !HAVE_REGEX_H */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002279 size_t qdepth = 0;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002280
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00002281 /* Compute quote depth level. */
David Lawrence Ramseyb8c479a2004-07-31 14:10:23 +00002282 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002283 qdepth += quotelen;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002284 return qdepth;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002285#endif /* !HAVE_REGEX_H */
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002286}
Chris Allegretta6df90f52002-07-19 01:08:59 +00002287
Chris Allegretta6df90f52002-07-19 01:08:59 +00002288/* a_line and b_line are lines of text. The quotation part of a_line is
2289 * the first a_quote characters. Check that the quotation part of
2290 * b_line is the same. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002291bool quotes_match(const char *a_line, size_t a_quote, const char
2292 *b_line)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002293{
Chris Allegretta6df90f52002-07-19 01:08:59 +00002294 /* Here is the assumption about a_quote: */
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002295 assert(a_quote == quote_length(a_line));
2296 return a_quote == quote_length(b_line) &&
David Lawrence Ramseyb8c479a2004-07-31 14:10:23 +00002297 strncmp(a_line, b_line, a_quote) == 0;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002298}
2299
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002300/* We assume a_line and b_line have no quote part. Then, we return
2301 * whether b_line could follow a_line in a paragraph. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002302bool indents_match(const char *a_line, size_t a_indent, const char
David Lawrence Ramseyd4693cb2004-05-14 01:17:25 +00002303 *b_line, size_t b_indent)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002304{
Chris Allegretta6df90f52002-07-19 01:08:59 +00002305 assert(a_indent == indent_length(a_line));
2306 assert(b_indent == indent_length(b_line));
2307
David Lawrence Ramseyb8c479a2004-07-31 14:10:23 +00002308 return b_indent <= a_indent &&
2309 strncmp(a_line, b_line, b_indent) == 0;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002310}
2311
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002312/* Is foo the beginning of a paragraph?
2313 *
2314 * A line of text consists of a "quote part", followed by an
2315 * "indentation part", followed by text. The functions quote_length()
2316 * and indent_length() calculate these parts.
2317 *
2318 * A line is "part of a paragraph" if it has a part not in the quote
2319 * part or the indentation.
2320 *
2321 * A line is "the beginning of a paragraph" if it is part of a
2322 * paragraph and
2323 * 1) it is the top line of the file, or
2324 * 2) the line above it is not part of a paragraph, or
2325 * 3) the line above it does not have precisely the same quote
2326 * part, or
2327 * 4) the indentation of this line is not an initial substring of
2328 * the indentation of the previous line, or
2329 * 5) this line has no quote part and some indentation, and
2330 * AUTOINDENT is not set.
2331 * The reason for number 5) is that if AUTOINDENT is not set, then an
2332 * indented line is expected to start a paragraph, like in books.
2333 * Thus, nano can justify an indented paragraph only if AUTOINDENT is
2334 * turned on. */
2335bool begpar(const filestruct *const foo)
2336{
2337 size_t quote_len;
2338 size_t indent_len;
2339 size_t temp_id_len;
2340
2341 /* Case 1). */
2342 if (foo->prev == NULL)
2343 return TRUE;
2344
2345 quote_len = quote_length(foo->data);
2346 indent_len = indent_length(foo->data + quote_len);
2347
2348 /* Not part of a paragraph. */
2349 if (foo->data[quote_len + indent_len] == '\0')
2350 return FALSE;
2351
2352 /* Case 3). */
2353 if (!quotes_match(foo->data, quote_len, foo->prev->data))
2354 return TRUE;
2355
2356 temp_id_len = indent_length(foo->prev->data + quote_len);
2357
2358 /* Case 2) or 5) or 4). */
2359 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
2360 (quote_len == 0 && indent_len > 0
2361#ifndef NANO_SMALL
2362 && !ISSET(AUTOINDENT)
2363#endif
2364 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
2365 foo->data + quote_len, indent_len))
2366 return TRUE;
2367
2368 return FALSE;
2369}
2370
2371/* We find the last beginning-of-paragraph line before the current
2372 * line. */
2373void do_para_begin(void)
2374{
2375 const filestruct *old_current = current;
2376 const size_t old_pww = placewewant;
2377
2378 current_x = 0;
2379 placewewant = 0;
2380
2381 if (current->prev != NULL) {
2382 do {
2383 current = current->prev;
David Lawrence Ramsey50c7f2d2004-08-27 17:02:05 +00002384 current_y--;
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002385 } while (!begpar(current));
2386 }
2387
2388 edit_redraw(old_current, old_pww);
2389}
2390
2391bool inpar(const char *str)
2392{
2393 size_t quote_len = quote_length(str);
2394
2395 return str[quote_len + indent_length(str + quote_len)] != '\0';
2396}
2397
2398/* A line is the last line of a paragraph if it is in a paragraph, and
2399 * the next line isn't, or is the beginning of a paragraph. We move
2400 * down to the end of a paragraph, then one line farther. */
2401void do_para_end(void)
2402{
2403 const filestruct *const old_current = current;
2404 const size_t old_pww = placewewant;
2405
2406 current_x = 0;
2407 placewewant = 0;
2408
2409 while (current->next != NULL && !inpar(current->data))
2410 current = current->next;
2411
2412 while (current->next != NULL && inpar(current->next->data) &&
David Lawrence Ramsey50c7f2d2004-08-27 17:02:05 +00002413 !begpar(current->next)) {
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002414 current = current->next;
David Lawrence Ramsey50c7f2d2004-08-27 17:02:05 +00002415 current_y++;
2416 }
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002417
2418 if (current->next != NULL)
2419 current = current->next;
2420
2421 edit_redraw(old_current, old_pww);
2422}
2423
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002424/* Put the next par_len lines, starting with first_line, into the
2425 * justify buffer, leaving copies of those lines in place. Assume there
2426 * are enough lines after first_line. Return the new copy of
2427 * first_line. */
David Lawrence Ramseyd4693cb2004-05-14 01:17:25 +00002428filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t
2429 quote_len)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002430{
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002431 filestruct *top = first_line;
2432 /* The top of the paragraph we're backing up. */
2433 filestruct *bot = first_line;
2434 /* The bottom of the paragraph we're backing up. */
2435 size_t i;
2436 /* Generic loop variable. */
2437 size_t current_x_save = current_x;
2438 int fl_lineno_save = first_line->lineno;
2439 int edittop_lineno_save = edittop->lineno;
2440 int current_lineno_save = current->lineno;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002441#ifndef NANO_SMALL
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002442 bool old_mark_set = ISSET(MARK_ISSET);
2443 int mbb_lineno_save = 0;
2444
2445 if (old_mark_set)
2446 mbb_lineno_save = mark_beginbuf->lineno;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002447#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00002448
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002449 /* Move bot down par_len lines to the newline after the last line of
2450 * the paragraph. */
2451 for (i = par_len; i > 0; i--)
2452 bot = bot->next;
2453
2454 /* Move the paragraph from the main filestruct to the justify
2455 * buffer. */
2456 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot, 0);
2457
2458 /* Copy the paragraph from the justify buffer to the main
2459 * filestruct. */
2460 copy_from_filestruct(jusbuffer, jusbottom);
2461
2462 /* Move upward from the last line of the paragraph to the first
2463 * line, putting first_line, edittop, current, and mark_beginbuf at
2464 * the same lines in the copied paragraph that they had in the
2465 * original paragraph. */
2466 top = current->prev;
2467 for (i = par_len; i > 0; i--) {
2468 if (top->lineno == fl_lineno_save)
2469 first_line = top;
2470 if (top->lineno == edittop_lineno_save)
2471 edittop = top;
2472 if (top->lineno == current_lineno_save)
2473 current = top;
2474#ifndef NANO_SMALL
2475 if (old_mark_set && top->lineno == mbb_lineno_save)
2476 mark_beginbuf = top;
2477#endif
2478 top = top->prev;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002479 }
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002480
2481 /* Put current_x at the same place in the copied paragraph that it
2482 * had in the original paragraph. */
2483 current_x = current_x_save;
2484
2485 set_modified();
2486
Chris Allegretta6df90f52002-07-19 01:08:59 +00002487 return first_line;
2488}
2489
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002490/* Is it possible to break line at or before goal? */
David Lawrence Ramseyd08348b2004-09-18 22:02:21 +00002491bool breakable(const char *line, ssize_t goal)
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002492{
David Lawrence Ramsey4e254102003-11-05 22:04:08 +00002493 for (; *line != '\0' && goal >= 0; line++) {
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00002494 if (isblank(*line))
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002495 return TRUE;
2496
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002497 if (is_cntrl_char(*line))
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002498 goal -= 2;
2499 else
2500 goal -= 1;
2501 }
Chris Allegretta428f6202003-02-12 03:21:45 +00002502 /* If goal is not negative, the whole line (one word) was short
2503 * enough. */
2504 return goal >= 0;
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002505}
2506
Chris Allegretta6df90f52002-07-19 01:08:59 +00002507/* We are trying to break a chunk off line. We find the last space such
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002508 * that the display length to there is at most goal + 1. If there is no
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002509 * such space, and force is TRUE, then we find the first space. Anyway,
2510 * we then take the last space in that group of spaces. The terminating
2511 * '\0' counts as a space. */
David Lawrence Ramseyfc54d6e2004-12-02 17:37:09 +00002512ssize_t break_line(const char *line, ssize_t goal, bool force)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002513{
David Lawrence Ramseyd08348b2004-09-18 22:02:21 +00002514 ssize_t space_loc = -1;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002515 /* Current tentative return value. Index of the last space we
2516 * found with short enough display width. */
David Lawrence Ramseyd08348b2004-09-18 22:02:21 +00002517 ssize_t cur_loc = 0;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002518 /* Current index in line. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002519
2520 assert(line != NULL);
David Lawrence Ramsey4e254102003-11-05 22:04:08 +00002521 for (; *line != '\0' && goal >= 0; line++, cur_loc++) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00002522 if (*line == ' ')
2523 space_loc = cur_loc;
2524 assert(*line != '\t');
2525
Chris Allegrettacf287c82002-07-20 13:57:41 +00002526 if (is_cntrl_char(*line))
Chris Allegretta6df90f52002-07-19 01:08:59 +00002527 goal -= 2;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002528 else
2529 goal--;
2530 }
2531 if (goal >= 0)
2532 /* In fact, the whole line displays shorter than goal. */
2533 return cur_loc;
2534 if (space_loc == -1) {
2535 /* No space found short enough. */
2536 if (force)
David Lawrence Ramsey4e254102003-11-05 22:04:08 +00002537 for (; *line != '\0'; line++, cur_loc++)
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002538 if (*line == ' ' && *(line + 1) != ' ' && *(line + 1) != '\0')
Chris Allegretta6df90f52002-07-19 01:08:59 +00002539 return cur_loc;
2540 return -1;
2541 }
2542 /* Perhaps the character after space_loc is a space. But because
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00002543 * of justify_format(), there can be only two adjacent. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002544 if (*(line - cur_loc + space_loc + 1) == ' ' ||
2545 *(line - cur_loc + space_loc + 1) == '\0')
2546 space_loc++;
2547 return space_loc;
2548}
Chris Allegretta6df90f52002-07-19 01:08:59 +00002549
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002550/* Find the beginning of the current paragraph if we're in one, or the
2551 * beginning of the next paragraph if we're not. Afterwards, save the
2552 * quote length and paragraph length in *quote and *par. Return FALSE
2553 * if we found a paragraph, or TRUE if there was an error or we didn't
2554 * find a paragraph.
David Lawrence Ramsey8faf3052003-09-04 20:25:29 +00002555 *
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002556 * See the comment at begpar() for more about when a line is the
2557 * beginning of a paragraph. */
2558bool do_para_search(size_t *const quote, size_t *const par)
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002559{
2560 size_t quote_len;
2561 /* Length of the initial quotation of the paragraph we
2562 * search. */
2563 size_t par_len;
2564 /* Number of lines in that paragraph. */
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00002565 size_t indent_len;
2566 /* Generic indentation length. */
2567 filestruct *line;
2568 /* Generic line of text. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002569
2570#ifdef HAVE_REGEX_H
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002571 if (quoterc != 0) {
2572 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
2573 return TRUE;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002574 }
2575#endif
2576
2577 /* Here is an assumption that is always true anyway. */
2578 assert(current != NULL);
2579
2580 current_x = 0;
2581
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002582 quote_len = quote_length(current->data);
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002583 indent_len = indent_length(current->data + quote_len);
2584
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002585 /* Here we find the first line of the paragraph to search. If the
2586 * current line is in a paragraph, then we move back to the first
2587 * line of the paragraph. Otherwise, we move to the first line that
2588 * is in a paragraph. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002589 if (current->data[quote_len + indent_len] != '\0') {
2590 /* This line is part of a paragraph. So we must search back to
2591 * the first line of this paragraph. First we check items 1)
2592 * and 3) above. */
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002593 while (current->prev != NULL && quotes_match(current->data,
2594 quote_len, current->prev->data)) {
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002595 size_t temp_id_len =
David Lawrence Ramseybb50b302004-08-12 21:43:00 +00002596 indent_length(current->prev->data + quote_len);
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002597 /* The indentation length of the previous line. */
2598
2599 /* Is this line the beginning of a paragraph, according to
2600 * items 2), 5), or 4) above? If so, stop. */
2601 if (current->prev->data[quote_len + temp_id_len] == '\0' ||
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002602 (quote_len == 0 && indent_len > 0
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002603#ifndef NANO_SMALL
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002604 && !ISSET(AUTOINDENT)
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002605#endif
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002606 ) || !indents_match(current->prev->data + quote_len,
2607 temp_id_len, current->data + quote_len, indent_len))
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002608 break;
2609 indent_len = temp_id_len;
2610 current = current->prev;
2611 current_y--;
2612 }
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002613 } else {
2614 /* This line is not part of a paragraph. Move down until we get
2615 * to a non "blank" line. */
2616 do {
2617 /* There is no next paragraph, so nothing to move to. */
2618 if (current->next == NULL) {
2619 placewewant = 0;
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002620 return TRUE;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002621 }
2622 current = current->next;
2623 current_y++;
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002624 quote_len = quote_length(current->data);
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002625 indent_len = indent_length(current->data + quote_len);
2626 } while (current->data[quote_len + indent_len] == '\0');
2627 }
2628
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002629 /* Now current is the first line of the paragraph, and quote_len is
2630 * the quotation length of that line. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002631
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002632 /* Next step, compute par_len, the number of lines in this
2633 * paragraph. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002634 line = current;
2635 par_len = 1;
2636 indent_len = indent_length(line->data + quote_len);
2637
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002638 while (line->next != NULL &&
2639 quotes_match(current->data, quote_len, line->next->data)) {
2640 size_t temp_id_len = indent_length(line->next->data + quote_len);
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002641
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002642 if (!indents_match(line->data + quote_len, indent_len,
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002643 line->next->data + quote_len, temp_id_len) ||
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002644 line->next->data[quote_len + temp_id_len] == '\0' ||
2645 (quote_len == 0 && temp_id_len > 0
2646#ifndef NANO_SMALL
David Lawrence Ramseydbde9d72004-06-27 00:54:08 +00002647 && !ISSET(AUTOINDENT)
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002648#endif
2649 ))
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002650 break;
2651 indent_len = temp_id_len;
2652 line = line->next;
2653 par_len++;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002654 }
2655
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002656 /* Now par_len is the number of lines in this paragraph. We should
2657 * never call quotes_match() or quote_length() again. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002658
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002659 /* Save the values of quote_len and par_len. */
2660 assert(quote != NULL && par != NULL);
2661 *quote = quote_len;
2662 *par = par_len;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002663
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002664 return FALSE;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002665}
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002666
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002667/* If full_justify is TRUE, justify the entire file. Otherwise, justify
2668 * the current paragraph. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002669void do_justify(bool full_justify)
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002670{
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002671 filestruct *first_par_line = NULL;
2672 /* Will be the first line of the resulting justified paragraph.
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002673 * For restoring after unjustify. */
David Lawrence Ramsey36e363f2004-05-17 20:38:00 +00002674 filestruct *last_par_line;
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002675 /* Will be the line containing the newline after the last line
2676 * of the result. Also for restoring after unjustify. */
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002677 bool allow_respacing;
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002678 /* Whether we should change the spacing at the end of a line
David Lawrence Ramseyf7b5d932004-07-05 14:27:29 +00002679 * after justifying it. This should be TRUE whenever we move
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002680 * to the next line after justifying the current line. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002681
2682 /* We save these global variables to be restored if the user
David Lawrence Ramsey59f5e042004-10-29 15:48:40 +00002683 * unjustifies. Note that we don't need to save totlines. */
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00002684 size_t current_x_save = current_x;
2685 int current_y_save = current_y;
David Lawrence Ramsey59f5e042004-10-29 15:48:40 +00002686 long flags_save = flags, totsize_save = totsize;
2687 filestruct *edittop_save = edittop, *current_save = current;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002688#ifndef NANO_SMALL
2689 filestruct *mark_beginbuf_save = mark_beginbuf;
David Lawrence Ramsey687776b2004-10-30 01:16:08 +00002690 size_t mark_beginx_save = mark_beginx;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002691#endif
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00002692 int kbinput;
David Lawrence Ramsey74835712004-12-04 17:41:52 +00002693 bool meta_key, func_key, s_or_t;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002694
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002695 /* If we're justifying the entire file, start at the beginning. */
2696 if (full_justify)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002697 current = fileage;
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002698
2699 last_par_line = current;
David Lawrence Ramsey8faf3052003-09-04 20:25:29 +00002700
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002701 while (TRUE) {
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002702 size_t quote_len;
2703 /* Length of the initial quotation of the paragraph we
2704 * justify. */
2705 size_t par_len;
2706 /* Number of lines in that paragraph. */
2707
2708 /* Find the first line of the paragraph to be justified. That
2709 * is the start of this paragraph if we're in one, or the start
2710 * of the next otherwise. Save the quote length and paragraph
David Lawrence Ramsey68ebb612004-12-04 17:36:14 +00002711 * length (number of lines). Don't refresh the screen yet,
2712 * since we'll do that after we justify. If the search
2713 * failed, we're justifying the whole file, and the search
2714 * didn't leave us on the last line of the file, set the last
2715 * line of the text to be justified to the last line of the file
2716 * and break out of the loop. Otherwise, refresh the screen and
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002717 * get out. */
2718 if (do_para_search(&quote_len, &par_len)) {
David Lawrence Ramsey68ebb612004-12-04 17:36:14 +00002719 if (full_justify && current != filebot) {
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002720 last_par_line = filebot;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002721 break;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002722 } else {
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002723 edit_refresh();
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002724 return;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002725 }
2726 }
2727
2728 /* Next step, we loop through the lines of this paragraph,
2729 * justifying each one individually. */
2730 for (; par_len > 0; current_y++, par_len--) {
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002731 size_t indent_len; /* Generic indentation length. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002732 size_t line_len;
2733 size_t display_len;
2734 /* The width of current in screen columns. */
David Lawrence Ramseyd08348b2004-09-18 22:02:21 +00002735 ssize_t break_pos;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002736 /* Where we will break the line. */
2737
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002738 /* We'll be moving to the next line after justifying the
2739 * current line in almost all cases, so allow changing the
2740 * spacing at the ends of justified lines by default. */
2741 allow_respacing = TRUE;
2742
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002743 indent_len = quote_len + indent_length(current->data +
2744 quote_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002745
David Lawrence Ramsey91bc83a2004-06-25 01:52:10 +00002746 /* If we haven't already done it, copy the original
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002747 * paragraph to the justify buffer. */
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002748 if (first_par_line == NULL)
2749 first_par_line = backup_lines(current, full_justify ?
David Lawrence Ramsey6b769e52004-11-17 16:59:34 +00002750 filebot->lineno - current->lineno : par_len,
2751 quote_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002752
David Lawrence Ramsey91bc83a2004-06-25 01:52:10 +00002753 /* Now we call justify_format() on the current line of the
2754 * paragraph, which will remove excess spaces from it and
2755 * change tabs to spaces. */
2756 justify_format(current, quote_len +
2757 indent_length(current->data + quote_len));
2758
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002759 line_len = strlen(current->data);
2760 display_len = strlenpt(current->data);
2761
2762 if (display_len > fill) {
2763 /* The line is too long. Try to wrap it to the next. */
2764 break_pos = break_line(current->data + indent_len,
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002765 fill - strnlenpt(current->data, indent_len),
2766 TRUE);
2767 if (break_pos == -1 || break_pos + indent_len ==
2768 line_len)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002769 /* We can't break the line, or don't need to, so
2770 * just go on to the next. */
2771 goto continue_loc;
2772 break_pos += indent_len;
2773 assert(break_pos < line_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002774 if (par_len == 1) {
2775 /* There is no next line in this paragraph. We make
2776 * a new line and copy text after break_pos into
2777 * it. */
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002778 splice_node(current, make_new_node(current),
2779 current->next);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002780 /* In a non-quoted paragraph, we copy the indent
2781 * only if AUTOINDENT is turned on. */
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002782 if (quote_len == 0
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002783#ifndef NANO_SMALL
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002784 && !ISSET(AUTOINDENT)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002785#endif
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002786 )
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002787 indent_len = 0;
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002788 current->next->data = charalloc(indent_len +
2789 line_len - break_pos);
2790 strncpy(current->next->data, current->data,
2791 indent_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002792 strcpy(current->next->data + indent_len,
2793 current->data + break_pos + 1);
2794 assert(strlen(current->next->data) ==
2795 indent_len + line_len - break_pos - 1);
2796 totlines++;
2797 totsize += indent_len;
2798 par_len++;
2799 } else {
2800 size_t next_line_len = strlen(current->next->data);
2801
2802 indent_len = quote_len +
2803 indent_length(current->next->data + quote_len);
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002804 current->next->data =
2805 charealloc(current->next->data, next_line_len +
2806 line_len - break_pos + 1);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002807
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002808 charmove(current->next->data + indent_len +
2809 line_len - break_pos, current->next->data +
2810 indent_len, next_line_len - indent_len + 1);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002811 strcpy(current->next->data + indent_len,
2812 current->data + break_pos + 1);
David Lawrence Ramsey537a8802004-06-24 22:39:24 +00002813 current->next->data[indent_len + line_len -
2814 break_pos - 1] = ' ';
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002815#ifndef NANO_SMALL
2816 if (mark_beginbuf == current->next) {
2817 if (mark_beginx < indent_len)
2818 mark_beginx = indent_len;
2819 mark_beginx += line_len - break_pos;
2820 }
2821#endif
2822 }
2823#ifndef NANO_SMALL
David Lawrence Ramsey40a6c8c2004-11-27 21:10:11 +00002824 if (mark_beginbuf == current &&
2825 mark_beginx > break_pos) {
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002826 mark_beginbuf = current->next;
2827 mark_beginx -= break_pos + 1 - indent_len;
2828 }
2829#endif
2830 null_at(&current->data, break_pos);
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002831
2832 /* Go to the next line. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002833 current = current->next;
2834 } else if (display_len < fill && par_len > 1) {
2835 size_t next_line_len;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002836
2837 indent_len = quote_len +
2838 indent_length(current->next->data + quote_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002839 /* If we can't pull a word from the next line up to this
2840 * one, just go on. */
2841 if (!breakable(current->next->data + indent_len,
2842 fill - display_len - 1))
2843 goto continue_loc;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002844
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002845 break_pos = break_line(current->next->data + indent_len,
2846 fill - display_len - 1, FALSE);
2847 assert(break_pos != -1);
2848
2849 current->data = charealloc(current->data,
2850 line_len + break_pos + 2);
2851 current->data[line_len] = ' ';
2852 strncpy(current->data + line_len + 1,
2853 current->next->data + indent_len, break_pos);
2854 current->data[line_len + break_pos + 1] = '\0';
Chris Allegretta6df90f52002-07-19 01:08:59 +00002855#ifndef NANO_SMALL
2856 if (mark_beginbuf == current->next) {
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002857 if (mark_beginx < indent_len + break_pos) {
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002858 mark_beginbuf = current;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002859 if (mark_beginx <= indent_len)
2860 mark_beginx = line_len + 1;
2861 else
David Lawrence Ramsey2e3aeae2004-05-24 05:52:35 +00002862 mark_beginx = line_len + 1 + mark_beginx -
2863 indent_len;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002864 } else
2865 mark_beginx -= break_pos + 1;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002866 }
2867#endif
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002868 next_line_len = strlen(current->next->data);
2869 if (indent_len + break_pos == next_line_len) {
2870 filestruct *line = current->next;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002871
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002872 /* Don't destroy edittop! */
2873 if (line == edittop)
2874 edittop = current;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002875
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002876 unlink_node(line);
2877 delete_node(line);
2878 totlines--;
2879 totsize -= indent_len;
2880 current_y--;
David Lawrence Ramsey01a6bf42004-07-03 05:23:19 +00002881
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002882 /* Don't go to the next line. Accordingly, don't
2883 * allow changing the spacing at the end of the
David Lawrence Ramsey309fbcb2004-07-03 14:34:03 +00002884 * previous justified line, so that we don't end up
2885 * doing it more than once on the same line. */
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002886 allow_respacing = FALSE;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002887 } else {
2888 charmove(current->next->data + indent_len,
Chris Allegretta6df90f52002-07-19 01:08:59 +00002889 current->next->data + indent_len + break_pos + 1,
2890 next_line_len - break_pos - indent_len);
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002891 null_at(&current->next->data, next_line_len -
2892 break_pos);
David Lawrence Ramsey01a6bf42004-07-03 05:23:19 +00002893
2894 /* Go to the next line. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002895 current = current->next;
2896 }
2897 } else
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002898 continue_loc:
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002899 /* Go to the next line. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002900 current = current->next;
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002901
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002902 /* We've moved to the next line after justifying the
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002903 * current line. If the justified line was not the last
2904 * line of the paragraph, add a space to the end of it to
2905 * replace the one removed or left out by justify_format().
2906 * If it was the last line of the paragraph, and
2907 * justify_format() left a space on the end of it, remove
2908 * the space. */
2909 if (allow_respacing) {
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002910 size_t prev_line_len = strlen(current->prev->data);
David Lawrence Ramseya9a6ce02004-06-05 22:09:56 +00002911
2912 if (par_len > 1) {
2913 current->prev->data = charealloc(current->prev->data,
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002914 prev_line_len + 2);
David Lawrence Ramseya9a6ce02004-06-05 22:09:56 +00002915 current->prev->data[prev_line_len] = ' ';
2916 current->prev->data[prev_line_len + 1] = '\0';
2917 totsize++;
2918 } else if (par_len == 1 &&
2919 current->prev->data[prev_line_len - 1] == ' ') {
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002920 current->prev->data =
2921 charealloc(current->prev->data, prev_line_len);
David Lawrence Ramseya9a6ce02004-06-05 22:09:56 +00002922 current->prev->data[prev_line_len - 1] = '\0';
2923 totsize--;
2924 }
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002925 }
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002926 }
Chris Allegrettae1e0fd62003-04-15 01:15:09 +00002927
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002928 /* We've just justified a paragraph. If we're not justifying the
2929 * entire file, break out of the loop. Otherwise, continue the
2930 * loop so that we justify all the paragraphs in the file. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002931 if (!full_justify)
2932 break;
2933
2934 } /* while (TRUE) */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002935
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002936 /* We are now done justifying the paragraph or the file, so clean
2937 * up. totlines, totsize, and current_y have been maintained above.
2938 * Set last_par_line to the new end of the paragraph, update
David Lawrence Ramsey8037fe02004-07-23 12:30:40 +00002939 * fileage, and renumber() since edit_refresh() needs the line
2940 * numbers to be right (but only do the last two if we actually
2941 * justified something). */
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002942 last_par_line = current;
David Lawrence Ramsey8037fe02004-07-23 12:30:40 +00002943 if (first_par_line != NULL) {
2944 if (first_par_line->prev == NULL)
2945 fileage = first_par_line;
2946 renumber(first_par_line);
2947 }
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002948
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002949 edit_refresh();
Chris Allegretta6df90f52002-07-19 01:08:59 +00002950
Chris Allegretta9149e612000-11-27 00:23:41 +00002951 statusbar(_("Can now UnJustify!"));
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00002952
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002953 /* Display the shortcut list with UnJustify. */
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00002954 shortcut_init(TRUE);
Chris Allegretta07798352000-11-27 22:58:23 +00002955 display_main_list();
Chris Allegretta9149e612000-11-27 00:23:41 +00002956
David Lawrence Ramsey63e73cb2004-11-28 04:52:57 +00002957 /* Now get a keystroke and see if it's unjustify. If not, put back
2958 * the keystroke and return. */
David Lawrence Ramsey74835712004-12-04 17:41:52 +00002959 kbinput = do_input(&meta_key, &func_key, &s_or_t, FALSE);
Chris Allegretta5f071802001-05-06 02:34:31 +00002960
David Lawrence Ramsey74835712004-12-04 17:41:52 +00002961 if (!meta_key && !func_key && s_or_t &&
2962 kbinput == NANO_UNJUSTIFY_KEY) {
David Lawrence Ramseya0b5ba22004-08-25 15:39:10 +00002963 /* Restore the justify we just did (ungrateful user!). */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002964 current = current_save;
2965 current_x = current_x_save;
2966 current_y = current_y_save;
2967 edittop = edittop_save;
Chris Allegrettad022eac2000-11-27 02:50:49 +00002968
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002969 /* Splice the justify buffer back into the file, but only if we
David Lawrence Ramsey8037fe02004-07-23 12:30:40 +00002970 * actually justified something. */
2971 if (first_par_line != NULL) {
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002972 filestruct *bot_save;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002973
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002974 /* Partition the filestruct so that it contains only the
2975 * text of the justified paragraph. */
2976 filepart = partition_filestruct(first_par_line, 0,
2977 last_par_line, 0);
Chris Allegretta6df90f52002-07-19 01:08:59 +00002978
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002979 /* Remove the text of the justified paragraph, and
2980 * put the text in the justify buffer in its place. */
2981 free_filestruct(fileage);
2982 fileage = jusbuffer;
2983 filebot = jusbottom;
2984
2985 bot_save = filebot;
2986
2987 /* Unpartition the filestruct so that it contains all the
2988 * text again. Note that the justified paragraph has been
2989 * replaced with the unjustified paragraph. */
2990 unpartition_filestruct(&filepart);
2991
2992 /* Renumber starting with the ending line of the old
2993 * partition. */
2994 if (bot_save->next != NULL)
2995 renumber(bot_save->next);
2996
2997 /* Restore global variables from before the justify. */
2998 totsize = totsize_save;
2999 totlines = filebot->lineno;
Chris Allegretta6df90f52002-07-19 01:08:59 +00003000#ifndef NANO_SMALL
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003001 mark_beginbuf = mark_beginbuf_save;
3002 mark_beginx = mark_beginx_save;
Chris Allegretta6df90f52002-07-19 01:08:59 +00003003#endif
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003004 flags = flags_save;
3005
3006 /* Clear the justify buffer. */
3007 jusbuffer = NULL;
3008
3009 if (!ISSET(MODIFIED))
3010 titlebar(NULL);
3011 edit_refresh();
3012 }
David Lawrence Ramseya0b5ba22004-08-25 15:39:10 +00003013 } else {
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003014 unget_kbinput(kbinput, meta_key, func_key);
David Lawrence Ramseyd994ad52004-11-23 21:40:26 +00003015
David Lawrence Ramsey53d3db42004-11-28 03:53:01 +00003016 /* Blow away the text in the justify buffer. */
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003017 free_filestruct(jusbuffer);
3018 jusbuffer = NULL;
Chris Allegretta9149e612000-11-27 00:23:41 +00003019 }
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00003020
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00003021 blank_statusbar();
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003022
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003023 /* Display the shortcut list with UnCut. */
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00003024 shortcut_init(FALSE);
Chris Allegretta4a9c8582000-11-27 22:59:40 +00003025 display_main_list();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003026}
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003027
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003028void do_justify_void(void)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003029{
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003030 do_justify(FALSE);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003031}
3032
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003033void do_full_justify(void)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003034{
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003035 do_justify(TRUE);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003036}
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00003037#endif /* !DISABLE_JUSTIFY */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003038
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003039void do_exit(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003040{
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003041 int i;
Chris Allegretta13fd44b2002-01-02 13:59:11 +00003042
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003043 if (!ISSET(MODIFIED))
3044 i = 0; /* Pretend the user chose not to save. */
David Lawrence Ramseyedab0cc2004-07-03 03:09:12 +00003045 else if (ISSET(TEMP_FILE))
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003046 i = 1;
David Lawrence Ramseyf4276942003-12-24 03:33:09 +00003047 else
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003048 i = do_yesno(FALSE,
3049 _("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES) ? "));
3050
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003051#ifdef DEBUG
3052 dump_buffer(fileage);
3053#endif
3054
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003055 if (i == 0 || (i == 1 && do_writeout(TRUE) > 0)) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003056#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +00003057 /* Exit only if there are no more open file buffers. */
David Lawrence Ramsey3e189a82004-10-03 19:18:48 +00003058 if (!close_open_file())
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003059#endif
David Lawrence Ramseyda141062004-05-25 19:41:11 +00003060 finish();
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003061 } else if (i != 1)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003062 statusbar(_("Cancelled"));
3063
3064 display_main_list();
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003065}
3066
3067void signal_init(void)
3068{
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003069 /* Trap SIGINT and SIGQUIT because we want them to do useful
3070 * things. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003071 memset(&act, 0, sizeof(struct sigaction));
3072 act.sa_handler = SIG_IGN;
3073 sigaction(SIGINT, &act, NULL);
David Lawrence Ramsey2897d2b2004-01-26 20:56:20 +00003074 sigaction(SIGQUIT, &act, NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003075
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003076 /* Trap SIGHUP and SIGTERM because we want to write the file out. */
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003077 act.sa_handler = handle_hupterm;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003078 sigaction(SIGHUP, &act, NULL);
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003079 sigaction(SIGTERM, &act, NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003080
3081#ifndef NANO_SMALL
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003082 /* Trap SIGWINCH because we want to handle window resizes. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003083 act.sa_handler = handle_sigwinch;
3084 sigaction(SIGWINCH, &act, NULL);
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00003085 allow_pending_sigwinch(FALSE);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003086#endif
3087
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003088 /* Trap normal suspend (^Z) so we can handle it ourselves. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003089 if (!ISSET(SUSPEND)) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003090 act.sa_handler = SIG_IGN;
3091 sigaction(SIGTSTP, &act, NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003092 } else {
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003093 /* Block all other signals in the suspend and continue handlers.
3094 * If we don't do this, other stuff interrupts them! */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003095 sigfillset(&act.sa_mask);
3096
3097 act.sa_handler = do_suspend;
3098 sigaction(SIGTSTP, &act, NULL);
3099
3100 act.sa_handler = do_cont;
3101 sigaction(SIGCONT, &act, NULL);
3102 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003103}
3104
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003105/* Handler for SIGHUP (hangup) and SIGTERM (terminate). */
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003106RETSIGTYPE handle_hupterm(int signal)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003107{
Chris Allegrettaa0d89972003-02-03 03:32:08 +00003108 die(_("Received SIGHUP or SIGTERM\n"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003109}
3110
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003111/* Handler for SIGTSTP (suspend). */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003112RETSIGTYPE do_suspend(int signal)
3113{
3114 endwin();
Chris Allegretta76417082003-02-12 23:54:34 +00003115 printf("\n\n\n\n\n%s\n", _("Use \"fg\" to return to nano"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003116 fflush(stdout);
3117
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003118 /* Restore the old terminal settings. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003119 tcsetattr(0, TCSANOW, &oldterm);
3120
David Lawrence Ramsey99bede32003-09-29 07:21:11 +00003121 /* Trap SIGHUP and SIGTERM so we can properly deal with them while
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003122 * suspended. */
David Lawrence Ramsey99bede32003-09-29 07:21:11 +00003123 act.sa_handler = handle_hupterm;
3124 sigaction(SIGHUP, &act, NULL);
3125 sigaction(SIGTERM, &act, NULL);
3126
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003127 /* Do what mutt does: send ourselves a SIGSTOP. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003128 kill(0, SIGSTOP);
3129}
3130
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003131/* Handler for SIGCONT (continue after suspend). */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003132RETSIGTYPE do_cont(int signal)
3133{
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003134#ifndef NANO_SMALL
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003135 /* Perhaps the user resized the window while we slept. Handle it
3136 * and update the screen in the process. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003137 handle_sigwinch(0);
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003138#else
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003139 /* Just update the screen. */
3140 doupdate();
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003141#endif
3142}
3143
3144#ifndef NANO_SMALL
3145void handle_sigwinch(int s)
3146{
3147 const char *tty = ttyname(0);
3148 int fd;
3149 int result = 0;
3150 struct winsize win;
3151
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003152 if (tty == NULL)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003153 return;
3154 fd = open(tty, O_RDWR);
3155 if (fd == -1)
3156 return;
3157 result = ioctl(fd, TIOCGWINSZ, &win);
3158 close(fd);
3159 if (result == -1)
3160 return;
3161
3162 /* Could check whether the COLS or LINES changed, and return
3163 * otherwise. EXCEPT, that COLS and LINES are ncurses global
3164 * variables, and in some cases ncurses has already updated them.
3165 * But not in all cases, argh. */
3166 COLS = win.ws_col;
3167 LINES = win.ws_row;
Chris Allegrettaf8f2d582003-02-10 02:43:48 +00003168 editwinrows = LINES - 5 + no_help();
3169 if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003170 die_too_small();
3171
3172#ifndef DISABLE_WRAPJUSTIFY
3173 fill = wrap_at;
3174 if (fill <= 0)
3175 fill += COLS;
Chris Allegrettaf8f2d582003-02-10 02:43:48 +00003176 if (fill < 0)
3177 fill = 0;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003178#endif
3179
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00003180 hblank = charealloc(hblank, COLS + 1);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003181 memset(hblank, ' ', COLS);
3182 hblank[COLS] = '\0';
3183
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00003184 /* If we've partitioned the filestruct, unpartition it now. */
3185 if (filepart != NULL)
David Lawrence Ramsey74d87072004-11-22 00:16:23 +00003186 unpartition_filestruct(&filepart);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00003187
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003188#ifndef DISABLE_JUSTIFY
David Lawrence Ramseybc3b9262004-11-23 04:17:19 +00003189 /* If the justify buffer isn't empty, blow away the text in it and
3190 * display the shortcut list with UnCut. */
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003191 if (jusbuffer != NULL) {
3192 free_filestruct(jusbuffer);
3193 jusbuffer = NULL;
3194 shortcut_init(FALSE);
3195 }
3196#endif
3197
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003198#ifdef USE_SLANG
3199 /* Slang curses emulation brain damage, part 1: If we just do what
3200 * curses does here, it'll only work properly if the resize made the
3201 * window smaller. Do what mutt does: Leave and immediately reenter
3202 * Slang screen management mode. */
3203 SLsmg_reset_smg();
3204 SLsmg_init_smg();
3205#else
3206 /* Do the equivalent of what Minimum Profit does: Leave and
3207 * immediately reenter curses mode. */
3208 endwin();
3209 refresh();
3210#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003211
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00003212 /* Restore the terminal to its previous state. */
3213 terminal_init();
3214
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003215 /* Do the equivalent of what both mutt and Minimum Profit do:
3216 * Reinitialize all the windows based on the new screen
3217 * dimensions. */
3218 window_init();
3219
David Lawrence Ramsey907725f2004-11-12 00:09:20 +00003220 /* Redraw the contents of the windows that need it. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003221 blank_statusbar();
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003222 display_main_list();
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003223 total_refresh();
3224
David Lawrence Ramsey273d2ce2004-01-30 04:20:28 +00003225 /* Turn cursor back on for sure. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003226 curs_set(1);
3227
David Lawrence Ramsey48ae9862004-05-28 17:23:33 +00003228 /* Reset all the input routines that rely on character sequences. */
3229 reset_kbinput();
3230
David Lawrence Ramsey273d2ce2004-01-30 04:20:28 +00003231 /* Jump back to the main loop. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003232 siglongjmp(jmpbuf, 1);
3233}
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00003234
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00003235void allow_pending_sigwinch(bool allow)
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00003236{
3237 sigset_t winch;
3238 sigemptyset(&winch);
3239 sigaddset(&winch, SIGWINCH);
3240 if (allow)
3241 sigprocmask(SIG_UNBLOCK, &winch, NULL);
3242 else
3243 sigprocmask(SIG_BLOCK, &winch, NULL);
3244}
Chris Allegrettadab017e2002-04-23 10:56:06 +00003245#endif /* !NANO_SMALL */
David Lawrence Ramseyc5967552002-06-21 03:20:06 +00003246
Chris Allegrettadab017e2002-04-23 10:56:06 +00003247#ifndef NANO_SMALL
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00003248void do_toggle(const toggle *which)
Chris Allegretta756f2202000-09-01 13:32:47 +00003249{
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00003250 bool enabled;
Chris Allegrettaf0f63a82000-09-02 18:44:21 +00003251
Chris Allegrettaa8c22572002-02-15 19:17:02 +00003252 TOGGLE(which->flag);
Chris Allegretta2a42af12000-09-12 23:02:49 +00003253
Chris Allegrettaa8c22572002-02-15 19:17:02 +00003254 switch (which->val) {
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003255 case TOGGLE_SUSPEND_KEY:
3256 signal_init();
3257 break;
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00003258#ifndef DISABLE_MOUSE
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003259 case TOGGLE_MOUSE_KEY:
3260 mouse_init();
3261 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003262#endif
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003263 case TOGGLE_NOHELP_KEY:
3264 blank_statusbar();
3265 blank_bottombars();
3266 wrefresh(bottomwin);
3267 window_init();
3268 edit_refresh();
3269 display_main_list();
3270 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003271#ifdef ENABLE_COLOR
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003272 case TOGGLE_SYNTAX_KEY:
3273 edit_refresh();
3274 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003275#endif
David Lawrence Ramsey483ea322004-05-29 16:25:30 +00003276#ifdef ENABLE_NANORC
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003277 case TOGGLE_WHITESPACE_KEY:
3278 edit_refresh();
3279 break;
David Lawrence Ramsey483ea322004-05-29 16:25:30 +00003280#endif
Chris Allegretta756f2202000-09-01 13:32:47 +00003281 }
Chris Allegretta2a42af12000-09-12 23:02:49 +00003282
Chris Allegretta6df90f52002-07-19 01:08:59 +00003283 /* We are assuming here that shortcut_init() above didn't free and
3284 * reallocate the toggles. */
3285 enabled = ISSET(which->flag);
3286 if (which->val == TOGGLE_NOHELP_KEY || which->val == TOGGLE_WRAP_KEY)
3287 enabled = !enabled;
3288 statusbar("%s %s", which->desc,
3289 enabled ? _("enabled") : _("disabled"));
Chris Allegretta756f2202000-09-01 13:32:47 +00003290}
Chris Allegrettadab017e2002-04-23 10:56:06 +00003291#endif /* !NANO_SMALL */
Chris Allegretta756f2202000-09-01 13:32:47 +00003292
David Lawrence Ramsey013344c2004-09-22 22:45:08 +00003293void disable_extended_input(void)
3294{
3295 struct termios term;
3296
3297 tcgetattr(0, &term);
3298 term.c_lflag &= ~IEXTEN;
3299 tcsetattr(0, TCSANOW, &term);
3300}
3301
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003302void disable_signals(void)
3303{
3304 struct termios term;
3305
3306 tcgetattr(0, &term);
3307 term.c_lflag &= ~ISIG;
3308 tcsetattr(0, TCSANOW, &term);
3309}
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003310
3311#ifndef NANO_SMALL
3312void enable_signals(void)
3313{
3314 struct termios term;
3315
3316 tcgetattr(0, &term);
3317 term.c_lflag |= ISIG;
3318 tcsetattr(0, TCSANOW, &term);
3319}
3320#endif
3321
3322void disable_flow_control(void)
3323{
3324 struct termios term;
3325
3326 tcgetattr(0, &term);
3327 term.c_iflag &= ~(IXON|IXOFF);
3328 tcsetattr(0, TCSANOW, &term);
3329}
3330
3331void enable_flow_control(void)
3332{
3333 struct termios term;
3334
3335 tcgetattr(0, &term);
3336 term.c_iflag |= (IXON|IXOFF);
3337 tcsetattr(0, TCSANOW, &term);
3338}
3339
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00003340/* Set up the terminal state. Put the terminal in cbreak mode (read one
3341 * character at a time and interpret the special control keys), disable
3342 * translation of carriage return (^M) into newline (^J) so that we can
3343 * tell the difference between the Enter key and Ctrl-J, and disable
David Lawrence Ramsey013344c2004-09-22 22:45:08 +00003344 * echoing of characters as they're typed. Finally, disable extended
3345 * input processing, disable interpretation of the special control keys,
3346 * and if we're not in preserve mode, disable interpretation of the flow
3347 * control characters too. */
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00003348void terminal_init(void)
3349{
3350 cbreak();
3351 nonl();
3352 noecho();
David Lawrence Ramsey013344c2004-09-22 22:45:08 +00003353 disable_extended_input();
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00003354 disable_signals();
3355 if (!ISSET(PRESERVE))
3356 disable_flow_control();
3357}
3358
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003359int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
3360 allow_funcs)
3361{
3362 int input;
3363 /* The character we read in. */
3364 static int *kbinput = NULL;
3365 /* The input buffer. */
3366 static size_t kbinput_len = 0;
3367 /* The length of the input buffer. */
3368 const shortcut *s;
3369 bool have_shortcut;
3370#ifndef NANO_SMALL
3371 const toggle *t;
3372 bool have_toggle;
3373#endif
3374
3375 *s_or_t = FALSE;
3376
3377 /* Read in a character. */
3378 input = get_kbinput(edit, meta_key, func_key);
3379
3380#ifndef DISABLE_MOUSE
3381 /* If we got a mouse click and it was on a shortcut, read in the
3382 * shortcut character. */
3383 if (allow_funcs && func_key && input == KEY_MOUSE) {
3384 if (do_mouse())
3385 input = get_kbinput(edit, meta_key, func_key);
3386 else
3387 input = ERR;
3388 }
3389#endif
3390
3391 /* Check for a shortcut in the main list. */
3392 s = get_shortcut(main_list, &input, meta_key, func_key);
3393
3394 /* If we got a shortcut from the main list, or a "universal"
3395 * edit window shortcut, set have_shortcut to TRUE. */
3396 have_shortcut = (s != NULL || input == NANO_XON_KEY ||
3397 input == NANO_XOFF_KEY || input == NANO_SUSPEND_KEY);
3398
3399#ifndef NANO_SMALL
3400 /* Check for a toggle in the main list. */
3401 t = get_toggle(input, *meta_key);
3402
3403 /* If we got a toggle from the main list, set have_toggle to
3404 * TRUE. */
3405 have_toggle = (t != NULL);
3406#endif
3407
3408 /* Set s_or_t to TRUE if we got a shortcut or toggle. */
3409 *s_or_t = (have_shortcut
3410#ifndef NANO_SMALL
3411 || have_toggle
3412#endif
3413 );
3414
3415 if (allow_funcs) {
3416 /* If we got a character, and it isn't a shortcut, toggle, or
3417 * control character, it's a normal text character. Display the
3418 * warning if we're in view mode, or add the character to the
3419 * input buffer if we're not. */
3420 if (input != ERR && *s_or_t == FALSE && !is_cntrl_char(input)) {
3421 if (ISSET(VIEW_MODE))
3422 print_view_warning();
3423 else {
3424 kbinput_len++;
3425 kbinput = (int *)nrealloc(kbinput, kbinput_len *
3426 sizeof(int));
3427 kbinput[kbinput_len - 1] = input;
3428 }
3429 }
3430
3431 /* If we got a shortcut or toggle, or if there aren't any other
3432 * characters waiting after the one we read in, we need to
3433 * display all the characters in the input buffer if it isn't
3434 * empty. Note that it should be empty if we're in view
3435 * mode. */
3436 if (*s_or_t == TRUE || get_buffer_len() == 0) {
3437 if (kbinput != NULL) {
3438 /* Display all the characters in the input buffer at
3439 * once. */
3440 do_output(kbinput, kbinput_len);
3441
3442 /* Empty the input buffer. */
3443 kbinput_len = 0;
3444 free(kbinput);
3445 kbinput = NULL;
3446 }
3447 }
3448
3449 if (have_shortcut) {
3450 switch (input) {
3451 /* Handle the "universal" edit window shortcuts. */
3452 case NANO_XON_KEY:
3453 statusbar(_("XON ignored, mumble mumble."));
3454 break;
3455 case NANO_XOFF_KEY:
3456 statusbar(_("XOFF ignored, mumble mumble."));
3457 break;
3458#ifndef NANO_SMALL
3459 case NANO_SUSPEND_KEY:
3460 if (ISSET(SUSPEND))
3461 do_suspend(0);
3462 break;
3463#endif
3464 /* Handle the normal edit window shortcuts. */
3465 default:
3466 /* Blow away the text in the cutbuffer if we aren't
3467 * cutting text. */
3468 if (s->func != do_cut_text)
3469 cutbuffer_reset();
3470
3471 /* Run the function associated with this shortcut,
3472 * if there is one. */
3473 if (s->func != NULL) {
3474 if (ISSET(VIEW_MODE) && !s->viewok)
3475 print_view_warning();
3476 else
3477 s->func();
3478 }
3479 break;
3480 }
3481 }
3482#ifndef NANO_SMALL
3483 else if (have_toggle) {
3484 /* Blow away the text in the cutbuffer, since we aren't
3485 * cutting text. */
3486 cutbuffer_reset();
3487 /* Toggle the flag associated with this shortcut. */
3488 if (allow_funcs)
3489 do_toggle(t);
3490 }
3491#endif
3492 else
3493 /* Blow away the text in the cutbuffer, since we aren't
3494 * cutting text. */
3495 cutbuffer_reset();
3496 }
3497
3498 return input;
3499}
3500
3501#ifndef DISABLE_MOUSE
3502bool do_mouse(void)
3503{
3504 int mouse_x, mouse_y;
3505 bool retval;
3506
3507 retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
3508
3509 if (!retval) {
3510 /* We can click in the edit window to move the cursor. */
3511 if (wenclose(edit, mouse_y, mouse_x)) {
3512 bool sameline;
3513 /* Did they click on the line with the cursor? If they
3514 * clicked on the cursor, we set the mark. */
3515 size_t xcur;
3516 /* The character they clicked on. */
3517
3518 /* Subtract out the size of topwin. Perhaps we need a
3519 * constant somewhere? */
3520 mouse_y -= 2;
3521
3522 sameline = (mouse_y == current_y);
3523
3524 /* Move to where the click occurred. */
3525 for (; current_y < mouse_y && current->next != NULL; current_y++)
3526 current = current->next;
3527 for (; current_y > mouse_y && current->prev != NULL; current_y--)
3528 current = current->prev;
3529
3530 xcur = actual_x(current->data, get_page_start(xplustabs()) +
3531 mouse_x);
3532
3533#ifndef NANO_SMALL
3534 /* Clicking where the cursor is toggles the mark, as does
3535 * clicking beyond the line length with the cursor at the
3536 * end of the line. */
3537 if (sameline && xcur == current_x) {
3538 if (ISSET(VIEW_MODE)) {
3539 print_view_warning();
3540 return retval;
3541 }
3542 do_mark();
3543 }
3544#endif
3545
3546 current_x = xcur;
3547 placewewant = xplustabs();
3548 edit_refresh();
3549 }
3550 }
3551 /* FIXME: If we clicked on a location in the statusbar, the cursor
3552 * should move to the location we clicked on. This functionality
3553 * should be in do_statusbar_mouse() when it's written. */
3554
3555 return retval;
3556}
3557#endif /* !DISABLE_MOUSE */
3558
3559/* The user typed kbinput_len characters. Add them to the edit
3560 * buffer. */
3561void do_output(int *kbinput, size_t kbinput_len)
3562{
3563 size_t i, current_len = strlen(current->data);
3564 bool old_constupdate = ISSET(CONSTUPDATE);
3565 bool do_refresh = FALSE;
3566 /* Do we have to call edit_refresh(), or can we get away with
3567 * update_line()? */
3568
3569 char key[
3570#ifdef NANO_WIDE
3571 MB_LEN_MAX
3572#else
3573 1
3574#endif
3575 ]; /* The current character we have. */
3576 int key_len; /* The length of the current character. */
3577
3578 assert(current != NULL && current->data != NULL);
3579
3580 /* Turn off constant cursor position display if it's on. */
3581 if (old_constupdate)
3582 UNSET(CONSTUPDATE);
3583
3584 for (i = 0; i < kbinput_len; i++) {
3585#ifdef NANO_WIDE
3586 /* Change the wide character to its multibyte value. If it's
3587 * invalid, go on to the next character. */
3588 if (!ISSET(NO_UTF8)) {
3589 key_len = wctomb(key, kbinput[i]);
3590
3591 if (key_len == -1)
3592 continue;
3593 } else {
3594#endif
3595 /* Interpret the character as a single-byte sequence. */
3596 key_len = 1;
3597 key[0] = (char)kbinput[i];
3598#ifdef NANO_WIDE
3599 }
3600#endif
3601
3602 /* Null to newline, if needed. */
3603 if (key[0] == '\0' && key_len == 1)
3604 key[0] = '\n';
3605 /* Newline to Enter, if needed. */
3606 else if (key[0] == '\n' && key_len == 1) {
3607 do_enter();
3608 continue;
3609 }
3610
3611 /* When a character is inserted on the current magicline, it
3612 * means we need a new one! */
3613 if (filebot == current)
3614 new_magicline();
3615
3616 /* More dangerousness fun =) */
3617 current->data = charealloc(current->data,
3618 current_len + key_len + 1);
3619 assert(current_x <= current_len);
3620 charmove(&current->data[current_x + key_len],
3621 &current->data[current_x],
3622 current_len - current_x + key_len);
3623 charcpy(&current->data[current_x], key, key_len);
3624 current_len += key_len;
3625 /* FIXME: Should totsize be the number of single-byte characters
3626 * or the number of multibyte characters? Assume for former for
3627 * now. */
3628 totsize += key_len;
3629 set_modified();
3630
3631#ifndef NANO_SMALL
3632 /* Note that current_x has not yet been incremented. */
3633 if (current == mark_beginbuf && current_x < mark_beginx)
3634 mark_beginx += key_len;
3635#endif
3636
3637 {
3638 int j;
3639 for (j = 0; j < key_len; j++)
3640 do_right(FALSE);
3641 }
3642
3643#ifndef DISABLE_WRAPPING
3644 /* If we're wrapping text, we need to call edit_refresh(). */
3645 if (!ISSET(NO_WRAP) && (key[0] != '\t' || key_len != 1)) {
3646 bool do_refresh_save = do_refresh;
3647
3648 do_refresh = do_wrap(current);
3649
3650 /* If we needed to call edit_refresh() before this, we'll
3651 * still need to after this. */
3652 if (do_refresh_save)
3653 do_refresh = TRUE;
3654 }
3655#endif
3656
3657#ifdef ENABLE_COLOR
3658 /* If color syntaxes are turned on, we need to call
3659 * edit_refresh(). */
3660 if (ISSET(COLOR_SYNTAX))
3661 do_refresh = TRUE;
3662#endif
3663 }
3664
3665 /* Turn constant cursor position display back on if it was on. */
3666 if (old_constupdate)
3667 SET(CONSTUPDATE);
3668
3669 if (do_refresh)
3670 edit_refresh();
3671 else
3672 update_line(current, current_x);
3673}
3674
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00003675int main(int argc, char **argv)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003676{
3677 int optchr;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003678 int startline = 0;
3679 /* Line to try and start at. */
David Lawrence Ramseydf13e3b2004-08-12 19:48:21 +00003680#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003681 bool fill_flag_used = FALSE;
3682 /* Was the fill option used? */
David Lawrence Ramseydf13e3b2004-08-12 19:48:21 +00003683#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003684#ifdef ENABLE_MULTIBUFFER
3685 bool old_multibuffer;
3686 /* The old value of the multibuffer option, restored after we
3687 * load all files on the command line. */
3688#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003689#ifdef HAVE_GETOPT_LONG
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003690 const struct option long_options[] = {
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003691 {"help", 0, 0, 'h'},
3692#ifdef ENABLE_MULTIBUFFER
3693 {"multibuffer", 0, 0, 'F'},
3694#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00003695#ifdef ENABLE_NANORC
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00003696#ifndef NANO_SMALL
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00003697 {"historylog", 0, 0, 'H'},
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00003698#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00003699 {"ignorercfiles", 0, 0, 'I'},
3700#endif
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003701#ifdef NANO_WIDE
3702 {"noutf8", 0, 0, 'O'},
3703#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003704#ifndef DISABLE_JUSTIFY
3705 {"quotestr", 1, 0, 'Q'},
3706#endif
Chris Allegretta805c26d2000-09-06 13:39:17 +00003707#ifdef HAVE_REGEX_H
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00003708 {"regexp", 0, 0, 'R'},
Chris Allegretta47805612000-07-07 02:35:34 +00003709#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003710 {"tabsize", 1, 0, 'T'},
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003711 {"version", 0, 0, 'V'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003712#ifdef ENABLE_COLOR
3713 {"syntax", 1, 0, 'Y'},
3714#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003715 {"const", 0, 0, 'c'},
David Lawrence Ramsey4d7c2602003-08-17 02:48:43 +00003716 {"rebinddelete", 0, 0, 'd'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003717 {"nofollow", 0, 0, 'l'},
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00003718#ifndef DISABLE_MOUSE
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003719 {"mouse", 0, 0, 'm'},
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003720#endif
Chris Allegrettae1f14522001-09-19 03:19:43 +00003721#ifndef DISABLE_OPERATINGDIR
3722 {"operatingdir", 1, 0, 'o'},
3723#endif
Chris Allegretta6cd143d2003-01-05 23:35:44 +00003724 {"preserve", 0, 0, 'p'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003725#ifndef DISABLE_WRAPJUSTIFY
3726 {"fill", 1, 0, 'r'},
Chris Allegretta2d7893d2001-07-11 02:08:33 +00003727#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003728#ifndef DISABLE_SPELLER
3729 {"speller", 1, 0, 's'},
3730#endif
3731 {"tempfile", 0, 0, 't'},
3732 {"view", 0, 0, 'v'},
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003733#ifndef DISABLE_WRAPPING
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003734 {"nowrap", 0, 0, 'w'},
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003735#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003736 {"nohelp", 0, 0, 'x'},
3737 {"suspend", 0, 0, 'z'},
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003738#ifndef NANO_SMALL
David Lawrence Ramseyc7acf692004-05-22 20:15:20 +00003739 {"smarthome", 0, 0, 'A'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003740 {"backup", 0, 0, 'B'},
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00003741 {"backupdir", 1, 0, 'E'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003742 {"noconvert", 0, 0, 'N'},
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003743 {"smooth", 0, 0, 'S'},
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00003744 {"restricted", 0, 0, 'Z'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003745 {"autoindent", 0, 0, 'i'},
3746 {"cut", 0, 0, 'k'},
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003747#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003748 {0, 0, 0, 0}
3749 };
3750#endif
3751
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003752 setlocale(LC_ALL, "");
David Lawrence Ramseyad1fd0d2004-07-27 15:46:58 +00003753#ifdef ENABLE_NLS
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003754 bindtextdomain(PACKAGE, LOCALEDIR);
3755 textdomain(PACKAGE);
3756#endif
3757
Chris Allegretta7662c862003-01-13 01:35:15 +00003758#if !defined(ENABLE_NANORC) && defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING)
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003759 /* If we don't have rcfile support, we're root, and
3760 * --disable-wrapping-as-root is used, turn wrapping off. */
David Lawrence Ramseya619ae62004-03-04 06:33:52 +00003761 if (geteuid() == NANO_ROOT_UID)
David Lawrence Ramseydc60b722002-10-25 16:08:53 +00003762 SET(NO_WRAP);
3763#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00003764
Chris Allegretta6df90f52002-07-19 01:08:59 +00003765 while ((optchr =
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003766#ifdef HAVE_GETOPT_LONG
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003767 getopt_long(argc, argv, "h?ABE:FHINOQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz", long_options, NULL)
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003768#else
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003769 getopt(argc, argv, "h?ABE:FHINOQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz")
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003770#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003771 ) != -1) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003772
3773 switch (optchr) {
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003774 case 'a':
3775 case 'b':
3776 case 'e':
3777 case 'f':
3778 case 'g':
3779 case 'j':
3780 /* Pico compatibility flags. */
3781 break;
Chris Allegretta7004c282001-09-22 00:42:10 +00003782#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003783 case 'A':
3784 SET(SMART_HOME);
3785 break;
3786 case 'B':
3787 SET(BACKUP_FILE);
3788 break;
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003789 case 'E':
3790 backup_dir = mallocstrcpy(backup_dir, optarg);
3791 break;
Chris Allegretta7004c282001-09-22 00:42:10 +00003792#endif
Chris Allegretta355fbe52001-07-14 19:32:47 +00003793#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003794 case 'F':
3795 SET(MULTIBUFFER);
3796 break;
Chris Allegretta2d7893d2001-07-11 02:08:33 +00003797#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00003798#ifdef ENABLE_NANORC
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00003799#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003800 case 'H':
3801 SET(HISTORYLOG);
3802 break;
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00003803#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003804 case 'I':
3805 SET(NO_RCFILE);
3806 break;
Chris Allegretta6df90f52002-07-19 01:08:59 +00003807#endif
Chris Allegretta8fa1e282001-09-22 04:20:25 +00003808#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003809 case 'N':
3810 SET(NO_CONVERT);
3811 break;
Chris Allegrettaa8c22572002-02-15 19:17:02 +00003812#endif
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003813 case 'O':
3814 SET(NO_UTF8);
3815 break;
Chris Allegrettae4f940d2002-03-03 22:36:36 +00003816#ifndef DISABLE_JUSTIFY
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003817 case 'Q':
3818 quotestr = mallocstrcpy(quotestr, optarg);
3819 break;
Chris Allegrettae4f940d2002-03-03 22:36:36 +00003820#endif
Chris Allegretta805c26d2000-09-06 13:39:17 +00003821#ifdef HAVE_REGEX_H
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003822 case 'R':
3823 SET(USE_REGEXP);
3824 break;
Chris Allegretta47805612000-07-07 02:35:34 +00003825#endif
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003826#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003827 case 'S':
3828 SET(SMOOTHSCROLL);
3829 break;
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003830#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003831 case 'T':
3832 if (!parse_num(optarg, &tabsize) || tabsize <= 0) {
David Lawrence Ramsey6420d442004-08-11 05:13:08 +00003833 fprintf(stderr, _("Requested tab size %s invalid"), optarg);
3834 fprintf(stderr, "\n");
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003835 exit(1);
3836 }
3837 break;
3838 case 'V':
3839 version();
3840 exit(0);
Chris Allegretta09900ff2002-05-04 04:23:30 +00003841#ifdef ENABLE_COLOR
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003842 case 'Y':
3843 syntaxstr = mallocstrcpy(syntaxstr, optarg);
3844 break;
Chris Allegretta09900ff2002-05-04 04:23:30 +00003845#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003846 case 'Z':
3847 SET(RESTRICTED);
3848 break;
3849 case 'c':
3850 SET(CONSTUPDATE);
3851 break;
3852 case 'd':
3853 SET(REBIND_DELETE);
3854 break;
Chris Allegrettaff989832001-09-17 13:48:00 +00003855#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003856 case 'i':
3857 SET(AUTOINDENT);
3858 break;
3859 case 'k':
3860 SET(CUT_TO_END);
3861 break;
Chris Allegrettad19e9912000-07-12 18:14:51 +00003862#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003863 case 'l':
3864 SET(NOFOLLOW_SYMLINKS);
3865 break;
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00003866#ifndef DISABLE_MOUSE
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003867 case 'm':
3868 SET(USE_MOUSE);
3869 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003870#endif
Chris Allegrettae1f14522001-09-19 03:19:43 +00003871#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003872 case 'o':
3873 operating_dir = mallocstrcpy(operating_dir, optarg);
3874 break;
Chris Allegrettae1f14522001-09-19 03:19:43 +00003875#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003876 case 'p':
3877 SET(PRESERVE);
3878 break;
Chris Allegretta6fe61492001-05-21 12:56:25 +00003879#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003880 case 'r':
3881 if (!parse_num(optarg, &wrap_at)) {
David Lawrence Ramsey6420d442004-08-11 05:13:08 +00003882 fprintf(stderr, _("Requested fill size %s invalid"), optarg);
3883 fprintf(stderr, "\n");
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003884 exit(1);
3885 }
3886 fill_flag_used = TRUE;
3887 break;
Chris Allegretta6fe61492001-05-21 12:56:25 +00003888#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +00003889#ifndef DISABLE_SPELLER
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003890 case 's':
3891 alt_speller = mallocstrcpy(alt_speller, optarg);
3892 break;
Rocco Corsiaf5c3022001-01-12 07:51:05 +00003893#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003894 case 't':
3895 SET(TEMP_FILE);
3896 break;
3897 case 'v':
3898 SET(VIEW_MODE);
3899 break;
David Lawrence Ramsey95e0cf52003-01-02 16:32:20 +00003900#ifndef DISABLE_WRAPPING
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003901 case 'w':
3902 SET(NO_WRAP);
3903 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003904#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003905 case 'x':
3906 SET(NO_HELP);
3907 break;
3908 case 'z':
3909 SET(SUSPEND);
3910 break;
3911 default:
3912 usage();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003913 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003914 }
3915
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00003916 /* If the executable filename starts with 'r', we use restricted
3917 * mode. */
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00003918 if (*(tail(argv[0])) == 'r')
3919 SET(RESTRICTED);
3920
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00003921 /* If we're using restricted mode, disable suspending, backups, and
3922 * reading rcfiles, since they all would allow reading from or
3923 * writing to files not specified on the command line. */
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00003924 if (ISSET(RESTRICTED)) {
3925 UNSET(SUSPEND);
3926 UNSET(BACKUP_FILE);
3927 SET(NO_RCFILE);
3928 }
3929
Chris Allegretta7662c862003-01-13 01:35:15 +00003930/* We've read through the command line options. Now back up the flags
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003931 * and values that are set, and read the rcfile(s). If the values
3932 * haven't changed afterward, restore the backed-up values. */
Chris Allegretta7662c862003-01-13 01:35:15 +00003933#ifdef ENABLE_NANORC
3934 if (!ISSET(NO_RCFILE)) {
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00003935#ifndef DISABLE_OPERATINGDIR
Chris Allegretta7662c862003-01-13 01:35:15 +00003936 char *operating_dir_cpy = operating_dir;
3937#endif
David Lawrence Ramsey7e8dd192004-08-12 20:06:20 +00003938#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramsey49c3f242004-07-12 16:07:14 +00003939 ssize_t wrap_at_cpy = wrap_at;
Chris Allegretta7662c862003-01-13 01:35:15 +00003940#endif
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00003941#ifndef NANO_SMALL
3942 char *backup_dir_cpy = backup_dir;
3943#endif
Chris Allegretta7662c862003-01-13 01:35:15 +00003944#ifndef DISABLE_JUSTIFY
3945 char *quotestr_cpy = quotestr;
3946#endif
3947#ifndef DISABLE_SPELLER
3948 char *alt_speller_cpy = alt_speller;
3949#endif
David Lawrence Ramsey49c3f242004-07-12 16:07:14 +00003950 ssize_t tabsize_cpy = tabsize;
Chris Allegretta7662c862003-01-13 01:35:15 +00003951 long flags_cpy = flags;
3952
Chris Allegretta5ec68622003-02-05 02:39:34 +00003953#ifndef DISABLE_OPERATINGDIR
Chris Allegretta7662c862003-01-13 01:35:15 +00003954 operating_dir = NULL;
Chris Allegretta5ec68622003-02-05 02:39:34 +00003955#endif
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00003956#ifndef NANO_SMALL
3957 backup_dir = NULL;
3958#endif
Chris Allegretta5ec68622003-02-05 02:39:34 +00003959#ifndef DISABLE_JUSTIFY
Chris Allegretta7662c862003-01-13 01:35:15 +00003960 quotestr = NULL;
Chris Allegretta5ec68622003-02-05 02:39:34 +00003961#endif
3962#ifndef DISABLE_SPELLER
Chris Allegretta7662c862003-01-13 01:35:15 +00003963 alt_speller = NULL;
Chris Allegretta5ec68622003-02-05 02:39:34 +00003964#endif
Chris Allegretta7662c862003-01-13 01:35:15 +00003965
3966 do_rcfile();
3967
3968#ifndef DISABLE_OPERATINGDIR
3969 if (operating_dir_cpy != NULL) {
3970 free(operating_dir);
3971 operating_dir = operating_dir_cpy;
3972 }
3973#endif
David Lawrence Ramseydf13e3b2004-08-12 19:48:21 +00003974#ifndef DISABLE_WRAPJUSTIFY
Chris Allegretta7662c862003-01-13 01:35:15 +00003975 if (fill_flag_used)
3976 wrap_at = wrap_at_cpy;
3977#endif
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00003978#ifndef NANO_SMALL
3979 if (backup_dir_cpy != NULL) {
3980 free(backup_dir);
3981 backup_dir = backup_dir_cpy;
3982 }
3983#endif
Chris Allegretta7662c862003-01-13 01:35:15 +00003984#ifndef DISABLE_JUSTIFY
3985 if (quotestr_cpy != NULL) {
3986 free(quotestr);
3987 quotestr = quotestr_cpy;
3988 }
3989#endif
3990#ifndef DISABLE_SPELLER
3991 if (alt_speller_cpy != NULL) {
3992 free(alt_speller);
3993 alt_speller = alt_speller_cpy;
3994 }
3995#endif
David Lawrence Ramsey04419b92004-07-18 18:13:54 +00003996 if (tabsize_cpy != -1)
Chris Allegretta7662c862003-01-13 01:35:15 +00003997 tabsize = tabsize_cpy;
3998 flags |= flags_cpy;
3999 }
4000#if defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING)
David Lawrence Ramseya619ae62004-03-04 06:33:52 +00004001 else if (geteuid() == NANO_ROOT_UID)
Chris Allegretta7662c862003-01-13 01:35:15 +00004002 SET(NO_WRAP);
4003#endif
4004#endif /* ENABLE_NANORC */
4005
Chris Allegrettad8451932003-03-11 03:50:40 +00004006#ifndef NANO_SMALL
4007 history_init();
4008#ifdef ENABLE_NANORC
4009 if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG))
4010 load_history();
4011#endif
4012#endif
4013
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00004014#ifndef NANO_SMALL
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004015 /* Set up the backup directory (unless we're using restricted mode,
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00004016 * in which case backups are disabled, since they would allow
4017 * reading from or writing to files not specified on the command
4018 * line). This entails making sure it exists and is a directory, so
4019 * that backup files will be saved there. */
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004020 if (!ISSET(RESTRICTED))
4021 init_backup_dir();
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00004022#endif
4023
Chris Allegretta7662c862003-01-13 01:35:15 +00004024#ifndef DISABLE_OPERATINGDIR
4025 /* Set up the operating directory. This entails chdir()ing there,
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004026 * so that file reads and writes will be based there. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00004027 init_operating_dir();
4028#endif
4029
Chris Allegretta7662c862003-01-13 01:35:15 +00004030#ifndef DISABLE_JUSTIFY
David Lawrence Ramsey2c62b072004-05-29 16:38:57 +00004031 if (punct == NULL)
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004032 punct = mallocstrcpy(punct, ".?!");
David Lawrence Ramsey2c62b072004-05-29 16:38:57 +00004033
4034 if (brackets == NULL)
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004035 brackets = mallocstrcpy(brackets, "'\")}]>");
David Lawrence Ramsey2c62b072004-05-29 16:38:57 +00004036
Chris Allegretta7662c862003-01-13 01:35:15 +00004037 if (quotestr == NULL)
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004038 quotestr = mallocstrcpy(NULL,
Chris Allegretta7662c862003-01-13 01:35:15 +00004039#ifdef HAVE_REGEX_H
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004040 "^([ \t]*[|>:}#])+"
Chris Allegretta7662c862003-01-13 01:35:15 +00004041#else
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004042 "> "
Chris Allegretta7662c862003-01-13 01:35:15 +00004043#endif
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004044 );
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00004045#ifdef HAVE_REGEX_H
4046 quoterc = regcomp(&quotereg, quotestr, REG_EXTENDED);
4047
4048 if (quoterc == 0) {
4049 /* We no longer need quotestr, just quotereg. */
4050 free(quotestr);
4051 quotestr = NULL;
4052 } else {
4053 size_t size = regerror(quoterc, &quotereg, NULL, 0);
4054
4055 quoteerr = charalloc(size);
4056 regerror(quoterc, &quotereg, quoteerr, size);
4057 }
4058#else
4059 quotelen = strlen(quotestr);
4060#endif /* !HAVE_REGEX_H */
Chris Allegretta7662c862003-01-13 01:35:15 +00004061#endif /* !DISABLE_JUSTIFY */
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00004062
David Lawrence Ramsey3aedb362004-05-28 22:42:41 +00004063#ifndef DISABLE_SPELLER
4064 /* If we don't have an alternative spell checker after reading the
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00004065 * command line and/or rcfile(s), check $SPELL for one, as Pico
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004066 * does (unless we're using restricted mode, in which case spell
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00004067 * checking is disabled, since it would allow reading from or
4068 * writing to files not specified on the command line). */
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004069 if (!ISSET(RESTRICTED) && alt_speller == NULL) {
David Lawrence Ramsey3aedb362004-05-28 22:42:41 +00004070 char *spellenv = getenv("SPELL");
4071 if (spellenv != NULL)
4072 alt_speller = mallocstrcpy(NULL, spellenv);
4073 }
4074#endif
4075
David Lawrence Ramsey483ea322004-05-29 16:25:30 +00004076#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004077 /* If whitespace wasn't specified, set its default value. */
David Lawrence Ramsey483ea322004-05-29 16:25:30 +00004078 if (whitespace == NULL)
4079 whitespace = mallocstrcpy(NULL, " ");
4080#endif
4081
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004082 /* If tabsize wasn't specified, set its default value. */
Chris Allegretta7662c862003-01-13 01:35:15 +00004083 if (tabsize == -1)
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004084 tabsize = WIDTH_OF_TAB;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00004085
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00004086 /* Back up the old terminal settings so that they can be restored. */
Chris Allegretta9b4055c2002-03-29 16:00:59 +00004087 tcgetattr(0, &oldterm);
Chris Allegretta6ca01b12002-03-29 15:38:17 +00004088
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004089 /* Curses initialization stuff: Start curses and set up the
4090 * terminal state. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004091 initscr();
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00004092 terminal_init();
David Lawrence Ramseyc53a92d2004-01-12 03:28:06 +00004093
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00004094 /* Set up the global variables and the shortcuts. */
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00004095 global_init(FALSE);
4096 shortcut_init(FALSE);
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00004097
4098 /* Set up the signal handlers. */
Chris Allegretta9b4055c2002-03-29 16:00:59 +00004099 signal_init();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004100
4101#ifdef DEBUG
Jordi Mallachf9390af2003-08-05 19:31:12 +00004102 fprintf(stderr, "Main: set up windows\n");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004103#endif
4104
Chris Allegretta2a42af12000-09-12 23:02:49 +00004105 window_init();
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00004106#ifndef DISABLE_MOUSE
Chris Allegretta756f2202000-09-01 13:32:47 +00004107 mouse_init();
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00004108#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004109
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004110#ifdef DEBUG
Jordi Mallachf9390af2003-08-05 19:31:12 +00004111 fprintf(stderr, "Main: open file\n");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004112#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004113
David Lawrence Ramseybf1346f2004-10-22 20:25:56 +00004114 /* If there's a +LINE flag here, it is the first non-option
4115 * argument, and it is followed by at least one other argument, the
4116 * filename it applies to. */
4117 if (0 < optind && optind < argc - 1 && argv[optind][0] == '+') {
4118 startline = atoi(&argv[optind][1]);
4119 optind++;
4120 }
4121
Chris Allegretta7662c862003-01-13 01:35:15 +00004122#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004123 old_multibuffer = ISSET(MULTIBUFFER);
4124 SET(MULTIBUFFER);
4125
4126 /* Read all the files after the first one on the command line into
4127 * new buffers. */
4128 {
David Lawrence Ramseybf1346f2004-10-22 20:25:56 +00004129 int i = optind + 1, iline = 0;
4130 for (; i < argc; i++) {
4131 /* If there's a +LINE flag here, it is followed by at least
4132 * one other argument, the filename it applies to. */
4133 if (i < argc - 1 && argv[i][0] == '+' && iline == 0) {
4134 iline = atoi(&argv[i][1]);
4135 } else {
4136 load_buffer(argv[i]);
4137 if (iline > 0) {
4138 do_gotoline(iline, FALSE);
4139 iline = 0;
4140 }
4141 }
4142 }
Chris Allegretta7662c862003-01-13 01:35:15 +00004143 }
4144#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004145
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004146 /* Read the first file on the command line into either the current
4147 * buffer or a new buffer, depending on whether multibuffer mode is
4148 * enabled. */
4149 if (optind < argc)
4150 load_buffer(argv[optind]);
4151
4152 /* We didn't open any files if all the command line arguments were
4153 * invalid files like directories or if there were no command line
4154 * arguments given. In this case, we have to load a blank buffer.
4155 * Also, we unset view mode to allow editing. */
4156 if (filename == NULL) {
4157 filename = mallocstrcpy(NULL, "");
4158 new_file();
4159 UNSET(VIEW_MODE);
4160
4161 /* Add this new entry to the open_files structure if we have
4162 * multibuffer support, or to the main filestruct if we don't. */
4163 load_file();
4164 }
4165
4166#ifdef ENABLE_MULTIBUFFER
4167 if (!old_multibuffer)
4168 UNSET(MULTIBUFFER);
4169#endif
4170
4171#ifdef DEBUG
4172 fprintf(stderr, "Main: top and bottom win\n");
4173#endif
4174
4175 titlebar(NULL);
4176 display_main_list();
4177
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004178 if (startline > 0)
David Lawrence Ramsey49c3f242004-07-12 16:07:14 +00004179 do_gotoline(startline, FALSE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004180
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00004181#ifndef NANO_SMALL
4182 /* Return here after a SIGWINCH. */
Chris Allegretta6b58acd2001-04-12 03:01:53 +00004183 sigsetjmp(jmpbuf, 1);
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00004184#endif
Chris Allegretta08020882001-01-29 23:37:54 +00004185
Robert Siemborski6967eec2000-07-08 14:23:32 +00004186 edit_refresh();
Robert Siemborski6967eec2000-07-08 14:23:32 +00004187
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00004188 while (TRUE) {
David Lawrence Ramsey74835712004-12-04 17:41:52 +00004189 bool meta_key;
4190 /* Whether we got a meta key sequence. */
4191 bool func_key;
4192 /* Whether we got a function key. */
4193 bool s_or_t;
4194 /* Whether we got a shortcut or toggle. */
4195
4196 /* Make sure the cursor is in the edit window. */
David Lawrence Ramseyaea4dab2004-07-13 17:09:24 +00004197 reset_cursor();
David Lawrence Ramsey74835712004-12-04 17:41:52 +00004198
4199 /* If constant cursor position display is on, display the cursor
4200 * position. */
Chris Allegrettad26ab912003-01-28 01:16:47 +00004201 if (ISSET(CONSTUPDATE))
David Lawrence Ramsey74912c62004-07-01 19:41:09 +00004202 do_cursorpos(TRUE);
Chris Allegrettad26ab912003-01-28 01:16:47 +00004203
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00004204#if !defined(DISABLE_BROWSER) || !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
Chris Allegretta6b58acd2001-04-12 03:01:53 +00004205 currshortcut = main_list;
Chris Allegretta6fe61492001-05-21 12:56:25 +00004206#endif
Chris Allegretta6b58acd2001-04-12 03:01:53 +00004207
David Lawrence Ramsey74835712004-12-04 17:41:52 +00004208 /* Read in and interpret characters. */
4209 do_input(&meta_key, &func_key, &s_or_t, TRUE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004210 }
David Lawrence Ramseyfd1768a2004-03-19 21:57:56 +00004211 assert(FALSE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004212}