blob: 559ac14bfc9e09527460454707450824be463279 [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 Ramseyc13b7f02005-01-01 07:28:15 +00005 * Copyright (C) 1999-2005 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
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000047#ifdef HAVE_GETOPT_H
48#include <getopt.h>
49#endif
50
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000051#ifndef NANO_SMALL
52#include <setjmp.h>
53#endif
54
Chris Allegretta6fe61492001-05-21 12:56:25 +000055#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramsey49c3f242004-07-12 16:07:14 +000056static ssize_t fill = 0; /* Fill - where to wrap lines,
57 basically */
Chris Allegretta6fe61492001-05-21 12:56:25 +000058#endif
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +000059#ifndef DISABLE_WRAPPING
David Lawrence Ramseyce62e822004-08-05 22:10:22 +000060static bool same_line_wrap = FALSE; /* Whether wrapped text should
David Lawrence Ramsey85529b32004-06-22 15:38:47 +000061 be prepended to the next
62 line */
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +000063#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +000064
Chris Allegretta6df90f52002-07-19 01:08:59 +000065static struct termios oldterm; /* The user's original term settings */
Chris Allegretta88520c92001-05-05 17:45:54 +000066static struct sigaction act; /* For all our fun signal handlers */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000067
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000068#ifndef NANO_SMALL
David Lawrence Ramsey85529b32004-06-22 15:38:47 +000069static sigjmp_buf jmpbuf; /* Used to return to mainloop after
70 SIGWINCH */
David Lawrence Ramsey22fac782004-08-05 15:16:19 +000071static int pid; /* The PID of the newly forked process
72 * in open_pipe(). It must be global
73 * because the signal handler needs
74 * it. */
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000075#endif
Chris Allegretta08020882001-01-29 23:37:54 +000076
David Lawrence Ramsey93c84052004-11-23 04:08:28 +000077#ifndef DISABLE_JUSTIFY
78static filestruct *jusbottom = NULL;
79 /* Pointer to end of justify buffer. */
80#endif
81
David Lawrence Ramseyda141062004-05-25 19:41:11 +000082/* What we do when we're all set to exit. */
83void finish(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000084{
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000085 if (!ISSET(NO_HELP))
86 blank_bottombars();
87 else
88 blank_statusbar();
Chris Allegretta6b58acd2001-04-12 03:01:53 +000089
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000090 wrefresh(bottomwin);
91 endwin();
92
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +000093 /* Restore the old terminal settings. */
Chris Allegretta4da1fc62000-06-21 03:00:43 +000094 tcsetattr(0, TCSANOW, &oldterm);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000095
Chris Allegrettad8451932003-03-11 03:50:40 +000096#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
97 if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG))
98 save_history();
99#endif
100
Chris Allegretta6232d662002-05-12 19:52:15 +0000101#ifdef DEBUG
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000102 thanks_for_all_the_fish();
Chris Allegretta6232d662002-05-12 19:52:15 +0000103#endif
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000104
David Lawrence Ramseyda141062004-05-25 19:41:11 +0000105 exit(0);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000106}
107
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000108/* Die (gracefully?). */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000109void die(const char *msg, ...)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000110{
111 va_list ap;
112
Chris Allegrettaa0d89972003-02-03 03:32:08 +0000113 endwin();
114 curses_ended = TRUE;
115
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +0000116 /* Restore the old terminal settings. */
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000117 tcsetattr(0, TCSANOW, &oldterm);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000118
Chris Allegretta6df90f52002-07-19 01:08:59 +0000119 va_start(ap, msg);
120 vfprintf(stderr, msg, ap);
121 va_end(ap);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000122
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000123 /* Save the current file buffer if it's been modified. */
Chris Allegretta32da4562002-01-02 15:12:21 +0000124 if (ISSET(MODIFIED))
125 die_save_file(filename);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000126
Chris Allegretta355fbe52001-07-14 19:32:47 +0000127#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000128 /* Save all of the other modified file buffers, if any. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000129 if (open_files != NULL) {
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000130 openfilestruct *tmp = open_files;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000131
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000132 while (tmp != open_files->next) {
133 open_files = open_files->next;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000134
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000135 /* Save the current file buffer if it's been modified. */
136 if (open_files->flags & MODIFIED) {
137 /* Set fileage and filebot to match the current file
138 * buffer, and then write it to disk. */
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000139 fileage = open_files->fileage;
Chris Allegretta4dc03d52002-05-11 03:04:44 +0000140 filebot = open_files->filebot;
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000141 die_save_file(open_files->filename);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000142 }
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000143 }
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000144 }
145#endif
146
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000147 /* Get out. */
148 exit(1);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000149}
150
Chris Allegretta6df90f52002-07-19 01:08:59 +0000151void die_save_file(const char *die_filename)
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000152{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000153 char *ret;
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000154 bool failed = TRUE;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000155
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +0000156 /* If we're using restricted mode, don't write any emergency backup
157 * files, since that would allow reading from or writing to files
158 * not specified on the command line. */
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +0000159 if (ISSET(RESTRICTED))
160 return;
161
Chris Allegretta6df90f52002-07-19 01:08:59 +0000162 /* If we can't save, we have REAL bad problems, but we might as well
163 TRY. */
164 if (die_filename[0] == '\0')
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000165 die_filename = "nano";
Chris Allegretta6df90f52002-07-19 01:08:59 +0000166
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000167 ret = get_next_filename(die_filename);
Chris Allegretta6df90f52002-07-19 01:08:59 +0000168 if (ret[0] != '\0')
David Lawrence Ramsey951d7142004-10-04 15:23:47 +0000169 failed = (write_file(ret, TRUE, FALSE, TRUE) == -1);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000170
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000171 if (!failed)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000172 fprintf(stderr, _("\nBuffer written to %s\n"), ret);
Chris Allegretta3dbb2782000-12-02 04:36:50 +0000173 else
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000174 fprintf(stderr, _("\nBuffer not written to %s (too many backup files?)\n"), ret);
Chris Allegretta48b06702002-02-22 04:30:50 +0000175
176 free(ret);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000177}
178
Chris Allegrettae61e8302001-01-14 05:18:27 +0000179/* Die with an error message that the screen was too small if, well, the
Chris Allegretta6df90f52002-07-19 01:08:59 +0000180 * screen is too small. */
David Lawrence Ramsey0341b582002-08-21 16:10:37 +0000181void die_too_small(void)
Chris Allegrettae61e8302001-01-14 05:18:27 +0000182{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000183 die(_("Window size is too small for nano...\n"));
Chris Allegrettae61e8302001-01-14 05:18:27 +0000184}
185
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000186void print_view_warning(void)
187{
188 statusbar(_("Key illegal in VIEW mode"));
189}
190
David Lawrence Ramseyf28f50e2004-01-09 23:04:55 +0000191/* Initialize global variables -- no better way for now. If
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000192 * save_cutbuffer is TRUE, don't set cutbuffer to NULL. */
193void global_init(bool save_cutbuffer)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000194{
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000195 current_x = 0;
196 current_y = 0;
Chris Allegrettae61e8302001-01-14 05:18:27 +0000197
Chris Allegrettaf8f2d582003-02-10 02:43:48 +0000198 editwinrows = LINES - 5 + no_help();
199 if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS)
Chris Allegrettae61e8302001-01-14 05:18:27 +0000200 die_too_small();
201
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000202 fileage = NULL;
Chris Allegretta56214c62001-09-27 02:46:53 +0000203 if (!save_cutbuffer)
204 cutbuffer = NULL;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000205 current = NULL;
206 edittop = NULL;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000207 totlines = 0;
Chris Allegretta56214c62001-09-27 02:46:53 +0000208 totsize = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000209 placewewant = 0;
Chris Allegrettae61e8302001-01-14 05:18:27 +0000210
Chris Allegretta6fe61492001-05-21 12:56:25 +0000211#ifndef DISABLE_WRAPJUSTIFY
Chris Allegretta6df90f52002-07-19 01:08:59 +0000212 fill = wrap_at;
Chris Allegrettadffa2072002-07-24 01:02:26 +0000213 if (fill <= 0)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000214 fill += COLS;
Chris Allegrettaf8f2d582003-02-10 02:43:48 +0000215 if (fill < 0)
216 fill = 0;
Chris Allegretta6fe61492001-05-21 12:56:25 +0000217#endif
Chris Allegrettae61e8302001-01-14 05:18:27 +0000218
Chris Allegretta88b09152001-05-17 11:35:43 +0000219 hblank = charalloc(COLS + 1);
Chris Allegretta0a06e072001-01-23 02:35:04 +0000220 memset(hblank, ' ', COLS);
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000221 hblank[COLS] = '\0';
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000222}
223
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000224void window_init(void)
225{
Chris Allegrettaf8f2d582003-02-10 02:43:48 +0000226 editwinrows = LINES - 5 + no_help();
227 if (editwinrows < MIN_EDITOR_ROWS)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000228 die_too_small();
229
Chris Allegretta1a128af2003-01-26 04:15:56 +0000230 if (topwin != NULL)
231 delwin(topwin);
David Lawrence Ramseyce991bb2004-03-29 18:36:39 +0000232 if (edit != NULL)
233 delwin(edit);
Chris Allegretta1a128af2003-01-26 04:15:56 +0000234 if (bottomwin != NULL)
235 delwin(bottomwin);
236
David Lawrence Ramseyce991bb2004-03-29 18:36:39 +0000237 /* Set up the windows. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000238 topwin = newwin(2, COLS, 0, 0);
David Lawrence Ramseyce991bb2004-03-29 18:36:39 +0000239 edit = newwin(editwinrows, COLS, 2, 0);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000240 bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0);
241
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +0000242 /* Turn the keypad back on. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000243 keypad(edit, TRUE);
244 keypad(bottomwin, TRUE);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000245}
246
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +0000247#ifndef DISABLE_MOUSE
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000248void mouse_init(void)
249{
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000250 if (ISSET(USE_MOUSE)) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000251 mousemask(BUTTON1_RELEASED, NULL);
252 mouseinterval(50);
253 } else
254 mousemask(0, NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000255}
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000256#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000257
258#ifndef DISABLE_HELP
259/* This function allocates help_text, and stores the help string in it.
260 * help_text should be NULL initially. */
261void help_init(void)
262{
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000263 size_t allocsize = 1; /* Space needed for help_text. */
264 const char *htx; /* Untranslated help message. */
265 char *ptr;
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000266 const shortcut *s;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000267#ifndef NANO_SMALL
268 const toggle *t;
269#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000270
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000271 /* First, set up the initial help text for the current function. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000272 if (currshortcut == whereis_list || currshortcut == replace_list
273 || currshortcut == replace_list_2)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000274 htx = N_("Search Command Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000275 "Enter the words or characters you would like to search "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000276 "for, then hit Enter. If there is a match for the text you "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000277 "entered, the screen will be updated to the location of the "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000278 "nearest match for the search string.\n\n The previous "
279 "search string will be shown in brackets after the search "
280 "prompt. Hitting Enter without entering any text will "
281 "perform the previous search. If you have selected text "
282 "with the mark and then search to replace, only matches in "
283 "the selected text will be replaced.\n\n The following "
284 "function keys are available in Search mode:\n\n");
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +0000285 else if (currshortcut == gotoline_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000286 htx = N_("Go To Line Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000287 "Enter the line number that you wish to go to and hit "
288 "Enter. If there are fewer lines of text than the "
289 "number you entered, you will be brought to the last line "
290 "of the file.\n\n The following function keys are "
291 "available in Go To Line mode:\n\n");
292 else if (currshortcut == insertfile_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000293 htx = N_("Insert File Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000294 "Type in the name of a file to be inserted into the current "
295 "file buffer at the current cursor location.\n\n "
296 "If you have compiled nano with multiple file buffer "
297 "support, and enable multiple buffers with the -F "
298 "or --multibuffer command line flags, the Meta-F toggle, or "
299 "a nanorc file, inserting a file will cause it to be "
300 "loaded into a separate buffer (use Meta-< and > to switch "
David Lawrence Ramseyfdd3bec2004-11-07 21:30:55 +0000301 "between file buffers). If you need another blank buffer, "
302 "do not enter any filename, or type in a nonexistent "
303 "filename at the prompt and press Enter.\n\n The following "
304 "function keys are available in Insert File mode:\n\n");
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000305 else if (currshortcut == writefile_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000306 htx = N_("Write File Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000307 "Type the name that you wish to save the current file "
308 "as and hit Enter to save the file.\n\n If you have "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000309 "selected text with the mark, you will be prompted to "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000310 "save only the selected portion to a separate file. To "
311 "reduce the chance of overwriting the current file with "
312 "just a portion of it, the current filename is not the "
313 "default in this mode.\n\n The following function keys "
314 "are available in Write File mode:\n\n");
315#ifndef DISABLE_BROWSER
316 else if (currshortcut == browser_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000317 htx = N_("File Browser Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000318 "The file browser is used to visually browse the "
319 "directory structure to select a file for reading "
320 "or writing. You may use the arrow keys or Page Up/"
321 "Down to browse through the files, and S or Enter to "
322 "choose the selected file or enter the selected "
323 "directory. To move up one level, select the directory "
324 "called \"..\" at the top of the file list.\n\n The "
325 "following function keys are available in the file "
326 "browser:\n\n");
327 else if (currshortcut == gotodir_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000328 htx = N_("Browser Go To Directory Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000329 "Enter the name of the directory you would like to "
330 "browse to.\n\n If tab completion has not been disabled, "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000331 "you can use the Tab key to (attempt to) automatically "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000332 "complete the directory name.\n\n The following function "
333 "keys are available in Browser Go To Directory mode:\n\n");
334#endif
Chris Allegretta201f1d92003-02-05 02:51:19 +0000335#ifndef DISABLE_SPELLER
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000336 else if (currshortcut == spell_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000337 htx = N_("Spell Check Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000338 "The spell checker checks the spelling of all text "
339 "in the current file. When an unknown word is "
340 "encountered, it is highlighted and a replacement can "
341 "be edited. It will then prompt to replace every "
342 "instance of the given misspelled word in the "
David Lawrence Ramsey381d4832004-10-18 01:51:43 +0000343 "current file, or, if you have selected text with the "
344 "mark, in the selected text.\n\n The following other "
345 "functions are available in Spell Check mode:\n\n");
Chris Allegretta201f1d92003-02-05 02:51:19 +0000346#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000347#ifndef NANO_SMALL
348 else if (currshortcut == extcmd_list)
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000349 htx = N_("External Command Help Text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000350 "This menu allows you to insert the output of a command "
351 "run by the shell into the current buffer (or a new "
David Lawrence Ramseyfdd3bec2004-11-07 21:30:55 +0000352 "buffer in multibuffer mode). If you need another blank "
353 "buffer, do not enter any command.\n\n The following keys "
354 "are available in this mode:\n\n");
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000355#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000356 else
357 /* Default to the main help list. */
358 htx = N_(" nano help text\n\n "
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000359 "The nano editor is designed to emulate the functionality and "
360 "ease-of-use of the UW Pico text editor. There are four main "
361 "sections of the editor: The top line shows the program "
362 "version, the current filename being edited, and whether "
363 "or not the file has been modified. Next is the main editor "
364 "window showing the file being edited. The status line is "
365 "the third line from the bottom and shows important messages. "
366 "The bottom two lines show the most commonly used shortcuts "
367 "in the editor.\n\n "
368 "The notation for shortcuts is as follows: Control-key "
David Lawrence Ramseyaaad3af2003-08-31 16:44:10 +0000369 "sequences are notated with a caret (^) symbol and can be "
370 "entered either by using the Control (Ctrl) key or pressing the "
371 "Esc key twice. Escape-key sequences are notated with the Meta "
372 "(M) symbol and can be entered using either the Esc, Alt or "
373 "Meta key depending on your keyboard setup. Also, pressing Esc "
374 "twice and then typing a three-digit number from 000 to 255 "
375 "will enter the character with the corresponding ASCII code. "
376 "The following keystrokes are available in the main editor "
377 "window. Alternative keys are shown in parentheses:\n\n");
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000378
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000379 htx = _(htx);
380
381 allocsize += strlen(htx);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000382
383 /* The space needed for the shortcut lists, at most COLS characters,
384 * plus '\n'. */
David Lawrence Ramsey1f1ebb82004-11-11 21:50:01 +0000385 allocsize += (COLS < 21 ? 21 : COLS + 1) *
386 length_of_list(currshortcut);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000387
388#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000389 /* If we're on the main list, we also count the toggle help text.
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000390 * Each line has "M-%c\t\t\t", which fills 24 columns, plus a space,
391 * plus translated text, plus '\n'. */
Chris Allegretta3a784062003-02-10 02:32:58 +0000392 if (currshortcut == main_list) {
393 size_t endislen = strlen(_("enable/disable"));
394
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000395 for (t = toggles; t != NULL; t = t->next)
Chris Allegretta3a784062003-02-10 02:32:58 +0000396 allocsize += 8 + strlen(t->desc) + endislen;
397 }
David Lawrence Ramsey1f1ebb82004-11-11 21:50:01 +0000398#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000399
400 /* help_text has been freed and set to NULL unless the user resized
401 * while in the help screen. */
402 free(help_text);
403
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000404 /* Allocate space for the help text. */
Chris Allegretta908f7702003-01-15 11:18:58 +0000405 help_text = charalloc(allocsize);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000406
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000407 /* Now add the text we want. */
408 strcpy(help_text, htx);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000409 ptr = help_text + strlen(help_text);
410
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000411 /* Now add our shortcut info. Assume that each shortcut has, at the
412 * very least, an equivalent control key, an equivalent primary meta
413 * key sequence, or both. Also assume that the meta key values are
414 * not control characters. We can display a maximum of 3 shortcut
415 * entries. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000416 for (s = currshortcut; s != NULL; s = s->next) {
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000417 int entries = 0;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000418
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000419 /* Control key. */
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000420 if (s->ctrlval != NANO_NO_KEY) {
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000421 entries++;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000422#ifndef NANO_SMALL
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000423 if (s->ctrlval == NANO_HISTORY_KEY)
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000424 ptr += sprintf(ptr, "%.7s", _("Up"));
David Lawrence Ramseyf4276942003-12-24 03:33:09 +0000425 else
David Lawrence Ramseya593f532003-11-28 19:47:42 +0000426#endif
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000427 if (s->ctrlval == NANO_CONTROL_SPACE)
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000428 ptr += sprintf(ptr, "^%.6s", _("Space"));
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000429 else if (s->ctrlval == NANO_CONTROL_8)
David Lawrence Ramseya593f532003-11-28 19:47:42 +0000430 ptr += sprintf(ptr, "^?");
431 else
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000432 ptr += sprintf(ptr, "^%c", s->ctrlval + 64);
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000433 *(ptr++) = '\t';
David Lawrence Ramseya593f532003-11-28 19:47:42 +0000434 }
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000435
436 /* Function key. */
437 if (s->funcval != NANO_NO_KEY) {
438 entries++;
David Lawrence Ramsey2e83a502004-11-01 22:35:26 +0000439 /* If this is the first entry, put it in the middle. */
440 if (entries == 1) {
441 entries++;
442 *(ptr++) = '\t';
443 }
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000444 ptr += sprintf(ptr, "(F%d)", s->funcval - KEY_F0);
445 *(ptr++) = '\t';
446 }
447
448 /* Primary meta key sequence. */
449 if (s->metaval != NANO_NO_KEY) {
450 entries++;
451 /* If this is the last entry, put it at the end. */
452 if (entries == 2 && s->miscval == NANO_NO_KEY) {
453 entries++;
454 *(ptr++) = '\t';
455 }
456 /* If the primary meta key sequence is the first entry,
457 * don't put parentheses around it. */
458 if (entries == 1 && s->metaval == NANO_ALT_SPACE)
David Lawrence Ramseyce62e822004-08-05 22:10:22 +0000459 ptr += sprintf(ptr, "M-%.5s", _("Space"));
David Lawrence Ramseyf4276942003-12-24 03:33:09 +0000460 else
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000461 ptr += sprintf(ptr, entries == 1 ? "M-%c" : "(M-%c)",
462 toupper(s->metaval));
463 *(ptr++) = '\t';
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000464 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000465
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000466 /* Miscellaneous meta key sequence. */
467 if (entries < 3 && s->miscval != NANO_NO_KEY) {
468 entries++;
469 /* If this is the last entry, put it at the end. */
470 if (entries == 2) {
471 entries++;
472 *(ptr++) = '\t';
473 }
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000474 ptr += sprintf(ptr, "(M-%c)", toupper(s->miscval));
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000475 *(ptr++) = '\t';
476 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000477
David Lawrence Ramseydb6015c2004-09-11 21:41:13 +0000478 /* Make sure all the help text starts at the same place. */
479 while (entries < 3) {
480 entries++;
481 *(ptr++) = '\t';
482 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000483
484 assert(s->help != NULL);
Chris Allegretta3a784062003-02-10 02:32:58 +0000485 ptr += sprintf(ptr, "%.*s\n", COLS > 24 ? COLS - 24 : 0, s->help);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000486 }
487
488#ifndef NANO_SMALL
489 /* And the toggles... */
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000490 if (currshortcut == main_list) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000491 for (t = toggles; t != NULL; t = t->next) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000492 assert(t->desc != NULL);
David Lawrence Ramseyd9fb3e62004-10-21 22:49:51 +0000493 ptr += sprintf(ptr, "M-%c\t\t\t%s %s\n", toupper(t->val),
494 t->desc, _("enable/disable"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000495 }
David Lawrence Ramsey1576d532004-03-19 21:46:34 +0000496 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000497#endif /* !NANO_SMALL */
498
499 /* If all went well, we didn't overwrite the allocated space for
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000500 * help_text. */
Chris Allegretta908f7702003-01-15 11:18:58 +0000501 assert(strlen(help_text) < allocsize);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000502}
503#endif
504
505/* Create a new filestruct node. Note that we specifically do not set
506 * prevnode->next equal to the new line. */
507filestruct *make_new_node(filestruct *prevnode)
508{
509 filestruct *newnode = (filestruct *)nmalloc(sizeof(filestruct));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000510 newnode->data = NULL;
511 newnode->prev = prevnode;
512 newnode->next = NULL;
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000513 newnode->lineno = (prevnode != NULL) ? prevnode->lineno + 1 : 1;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000514 return newnode;
515}
516
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000517/* Make a copy of a filestruct node. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000518filestruct *copy_node(const filestruct *src)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000519{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000520 filestruct *dst = (filestruct *)nmalloc(sizeof(filestruct));
Chris Allegretta6df90f52002-07-19 01:08:59 +0000521 assert(src != NULL);
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000522 dst->data = mallocstrcpy(NULL, src->data);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000523 dst->next = src->next;
524 dst->prev = src->prev;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000525 dst->lineno = src->lineno;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000526 return dst;
527}
528
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000529/* Splice a node into an existing filestruct. */
David Lawrence Ramseyf7080372004-07-08 17:15:10 +0000530void splice_node(filestruct *begin, filestruct *newnode, filestruct
531 *end)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000532{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000533 assert(newnode != NULL && begin != NULL);
534 newnode->next = end;
535 newnode->prev = begin;
536 begin->next = newnode;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000537 if (end != NULL)
538 end->prev = newnode;
539}
540
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000541/* Unlink a node from the rest of the filestruct. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000542void unlink_node(const filestruct *fileptr)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000543{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000544 assert(fileptr != NULL);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000545 if (fileptr->prev != NULL)
546 fileptr->prev->next = fileptr->next;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000547 if (fileptr->next != NULL)
548 fileptr->next->prev = fileptr->prev;
549}
550
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000551/* Delete a node from the filestruct. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000552void delete_node(filestruct *fileptr)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000553{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000554 assert(fileptr != NULL && fileptr->data != NULL);
555 if (fileptr->data != NULL)
556 free(fileptr->data);
557 free(fileptr);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000558}
559
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000560/* Okay, now let's duplicate a whole struct! */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000561filestruct *copy_filestruct(const filestruct *src)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000562{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000563 filestruct *head; /* copy of src, top of the copied list */
564 filestruct *prev; /* temp that traverses the list */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000565
Chris Allegretta6df90f52002-07-19 01:08:59 +0000566 assert(src != NULL);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000567
Chris Allegretta6df90f52002-07-19 01:08:59 +0000568 prev = copy_node(src);
569 prev->prev = NULL;
570 head = prev;
571 src = src->next;
572 while (src != NULL) {
573 prev->next = copy_node(src);
574 prev->next->prev = prev;
575 prev = prev->next;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000576
Chris Allegretta6df90f52002-07-19 01:08:59 +0000577 src = src->next;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000578 }
579
Chris Allegretta6df90f52002-07-19 01:08:59 +0000580 prev->next = NULL;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000581 return head;
582}
583
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000584/* Frees a filestruct. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000585void free_filestruct(filestruct *src)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000586{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000587 assert(src != NULL);
588
589 while (src->next != NULL) {
590 src = src->next;
591 delete_node(src->prev);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000592 }
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000593 delete_node(src);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000594}
595
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000596/* Partition a filestruct so it begins at (top, top_x) and ends at (bot,
597 * bot_x). */
598partition *partition_filestruct(filestruct *top, size_t top_x,
599 filestruct *bot, size_t bot_x)
600{
601 partition *p;
David Lawrence Ramsey6299b0d2004-11-08 03:22:23 +0000602 assert(top != NULL && bot != NULL && fileage != NULL && filebot != NULL);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000603
604 /* Initialize the partition. */
605 p = (partition *)nmalloc(sizeof(partition));
606
David Lawrence Ramseyf978f042004-11-04 16:45:48 +0000607 /* If the top and bottom of the partition are different from the top
608 * and bottom of the filestruct, save the latter and then set them
609 * to top and bot. */
610 if (top != fileage) {
611 p->fileage = fileage;
612 fileage = top;
613 } else
614 p->fileage = NULL;
615 if (bot != filebot) {
616 p->filebot = filebot;
617 filebot = bot;
618 } else
619 p->filebot = NULL;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000620
621 /* Save the line above the top of the partition, detach the top of
622 * the partition from it, and save the text before top_x in
623 * top_data. */
624 p->top_prev = top->prev;
625 top->prev = NULL;
626 p->top_data = mallocstrncpy(NULL, top->data, top_x + 1);
627 p->top_data[top_x] = '\0';
628
629 /* Save the line below the bottom of the partition, detach the
630 * bottom of the partition from it, and save the text after bot_x in
631 * bot_data. */
632 p->bot_next = bot->next;
633 bot->next = NULL;
634 p->bot_data = mallocstrcpy(NULL, bot->data + bot_x);
635
636 /* Remove all text after bot_x at the bottom of the partition. */
637 null_at(&bot->data, bot_x);
638
639 /* Remove all text before top_x at the top of the partition. */
640 charmove(top->data, top->data + top_x, strlen(top->data) -
641 top_x + 1);
642 align(&top->data);
643
644 /* Return the partition. */
645 return p;
646}
647
648/* Unpartition a filestruct so it begins at (fileage, 0) and ends at
649 * (filebot, strlen(filebot)) again. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000650void unpartition_filestruct(partition **p)
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000651{
652 char *tmp;
David Lawrence Ramsey2c315402004-11-08 03:19:10 +0000653 assert(p != NULL && fileage != NULL && filebot != NULL);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000654
655 /* Reattach the line above the top of the partition, and restore the
656 * text before top_x from top_data. Free top_data when we're done
657 * with it. */
658 tmp = mallocstrcpy(NULL, fileage->data);
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000659 fileage->prev = (*p)->top_prev;
David Lawrence Ramsey8cbd4cb2004-11-04 15:31:43 +0000660 if (fileage->prev != NULL)
661 fileage->prev->next = fileage;
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000662 fileage->data = charealloc(fileage->data, strlen((*p)->top_data) +
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000663 strlen(fileage->data) + 1);
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000664 strcpy(fileage->data, (*p)->top_data);
665 free((*p)->top_data);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000666 strcat(fileage->data, tmp);
667 free(tmp);
668
669 /* Reattach the line below the bottom of the partition, and restore
670 * the text after bot_x from bot_data. Free bot_data when we're
671 * done with it. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000672 filebot->next = (*p)->bot_next;
David Lawrence Ramsey8cbd4cb2004-11-04 15:31:43 +0000673 if (filebot->next != NULL)
674 filebot->next->prev = filebot;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000675 filebot->data = charealloc(filebot->data, strlen(filebot->data) +
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000676 strlen((*p)->bot_data) + 1);
677 strcat(filebot->data, (*p)->bot_data);
678 free((*p)->bot_data);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000679
David Lawrence Ramseyf978f042004-11-04 16:45:48 +0000680 /* Restore the top and bottom of the filestruct, if they were
681 * different from the top and bottom of the partition. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000682 if ((*p)->fileage != NULL)
683 fileage = (*p)->fileage;
684 if ((*p)->filebot != NULL)
685 filebot = (*p)->filebot;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000686
687 /* Uninitialize the partition. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000688 free(*p);
689 *p = NULL;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000690}
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +0000691
David Lawrence Ramsey93c84052004-11-23 04:08:28 +0000692/* Move all the text between (top, top_x) and (bot, bot_x) in the
693 * current filestruct to a filestruct beginning with file_top and ending
694 * with file_bot. If no text is between (top, top_x) and (bot, bot_x),
695 * don't do anything. */
696void move_to_filestruct(filestruct **file_top, filestruct **file_bot,
697 filestruct *top, size_t top_x, filestruct *bot, size_t bot_x)
698{
699 filestruct *top_save;
700 long part_totsize;
701 bool at_edittop;
702#ifndef NANO_SMALL
703 bool mark_inside = FALSE;
704#endif
705
706 assert(file_top != NULL && file_bot != NULL && top != NULL && bot != NULL);
707
708 /* If (top, top_x)-(bot, bot_x) doesn't cover any text, get out. */
709 if (top == bot && top_x == bot_x)
710 return;
711
712 /* Partition the filestruct so that it contains only the text from
713 * (top, top_x) to (bot, bot_x), keep track of whether the top of
714 * the partition is the top of the edit window, and keep track of
715 * whether the mark begins inside the partition. */
716 filepart = partition_filestruct(top, top_x, bot, bot_x);
717 at_edittop = (fileage == edittop);
718#ifndef NANO_SMALL
719 if (ISSET(MARK_ISSET))
720 mark_inside = (mark_beginbuf->lineno >= fileage->lineno &&
721 mark_beginbuf->lineno <= filebot->lineno &&
722 (mark_beginbuf != fileage || mark_beginx >= top_x) &&
723 (mark_beginbuf != filebot || mark_beginx <= bot_x));
724#endif
725
726 /* Get the number of characters in the text, and subtract it from
727 * totsize. */
728 get_totals(top, bot, NULL, &part_totsize);
729 totsize -= part_totsize;
730
731 if (*file_top == NULL) {
732 /* If file_top is empty, just move all the text directly into
733 * it. This is equivalent to tacking the text in top onto the
734 * (lack of) text at the end of file_top. */
735 *file_top = fileage;
736 *file_bot = filebot;
737 } else {
738 /* Otherwise, tack the text in top onto the text at the end of
739 * file_bot. */
740 (*file_bot)->data = charealloc((*file_bot)->data,
741 strlen((*file_bot)->data) + strlen(fileage->data) + 1);
742 strcat((*file_bot)->data, fileage->data);
743
744 /* Attach the line after top to the line after file_bot. Then,
745 * if there's more than one line after top, move file_bot down
746 * to bot. */
747 (*file_bot)->next = fileage->next;
748 if ((*file_bot)->next != NULL) {
749 (*file_bot)->next->prev = *file_bot;
750 *file_bot = filebot;
751 }
752 }
753
754 /* Since the text has now been saved, remove it from the filestruct.
755 * If the top of the partition was the top of the edit window, set
756 * edittop to where the text used to start. If the mark began
757 * inside the partition, set the beginning of the mark to where the
758 * text used to start. */
759 fileage = (filestruct *)nmalloc(sizeof(filestruct));
760 fileage->data = mallocstrcpy(NULL, "");
761 filebot = fileage;
762 if (at_edittop)
763 edittop = fileage;
764#ifndef NANO_SMALL
765 if (mark_inside) {
766 mark_beginbuf = fileage;
767 mark_beginx = top_x;
768 }
769#endif
770
771 /* Restore the current line and cursor position. */
772 current = fileage;
773 current_x = top_x;
774
775 top_save = fileage;
776
777 /* Unpartition the filestruct so that it contains all the text
778 * again, minus the saved text. */
779 unpartition_filestruct(&filepart);
780
781 /* Renumber starting with the beginning line of the old
782 * partition. */
783 renumber(top_save);
784
785 if (filebot->data[0] != '\0')
786 new_magicline();
787
788 /* Set totlines to the new number of lines in the file. */
789 totlines = filebot->lineno;
790}
791
792/* Copy all the text from the filestruct beginning with file_top and
793 * ending with file_bot to the current filestruct at the current cursor
794 * position. */
795void copy_from_filestruct(filestruct *file_top, filestruct *file_bot)
796{
797 filestruct *top_save;
798 int part_totlines;
799 long part_totsize;
800 bool at_edittop;
801
802 assert(file_top != NULL && file_bot != NULL);
803
804 /* Partition the filestruct so that it contains no text, and keep
805 * track of whether the top of the partition is the top of the edit
806 * window. */
807 filepart = partition_filestruct(current, current_x, current,
808 current_x);
809 at_edittop = (fileage == edittop);
810
811 /* Put the top and bottom of the filestruct at copies of file_top
812 * and file_bot. */
813 fileage = copy_filestruct(file_top);
814 filebot = fileage;
815 while (filebot->next != NULL)
816 filebot = filebot->next;
817
818 /* Restore the current line and cursor position. */
819 current = filebot;
820 current_x = strlen(filebot->data);
821 if (fileage == filebot)
822 current_x += strlen(filepart->top_data);
823
824 /* Get the number of lines and the number of characters in the saved
825 * text, and add the latter to totsize. */
826 get_totals(fileage, filebot, &part_totlines, &part_totsize);
827 totsize += part_totsize;
828
829 /* If the top of the partition was the top of the edit window, set
830 * edittop to where the saved text now starts, and update the
831 * current y-coordinate to account for the number of lines it
832 * has, less one since the first line will be tacked onto the
833 * current line. */
834 if (at_edittop)
835 edittop = fileage;
836 current_y += part_totlines - 1;
837
838 top_save = fileage;
839
840 /* Unpartition the filestruct so that it contains all the text
841 * again, minus the saved text. */
842 unpartition_filestruct(&filepart);
843
844 /* Renumber starting with the beginning line of the old
845 * partition. */
846 renumber(top_save);
847
848 if (filebot->data[0] != '\0')
849 new_magicline();
850
851 /* Set totlines to the new number of lines in the file. */
852 totlines = filebot->lineno;
853}
854
Chris Allegretta6df90f52002-07-19 01:08:59 +0000855void renumber_all(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000856{
857 filestruct *temp;
Chris Allegrettabef12972002-03-06 03:30:40 +0000858 int i = 1;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000859
Chris Allegretta6df90f52002-07-19 01:08:59 +0000860 assert(fileage == NULL || fileage != fileage->next);
861 for (temp = fileage; temp != NULL; temp = temp->next)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000862 temp->lineno = i++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000863}
864
Chris Allegretta6df90f52002-07-19 01:08:59 +0000865void renumber(filestruct *fileptr)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000866{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000867 if (fileptr == NULL || fileptr->prev == NULL || fileptr == fileage)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000868 renumber_all();
Chris Allegretta6df90f52002-07-19 01:08:59 +0000869 else {
870 int lineno = fileptr->prev->lineno;
871
872 assert(fileptr != fileptr->next);
873 for (; fileptr != NULL; fileptr = fileptr->next)
874 fileptr->lineno = ++lineno;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000875 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000876}
877
David Lawrence Ramsey22fac782004-08-05 15:16:19 +0000878/* Print one usage string to the screen. This cuts down on duplicate
879 * strings to translate and leaves out the parts that shouldn't be
Chris Allegretta6df90f52002-07-19 01:08:59 +0000880 * translatable (the flag names). */
David Lawrence Ramseyf7080372004-07-08 17:15:10 +0000881void print1opt(const char *shortflag, const char *longflag, const char
882 *desc)
Chris Allegretta3fc5d572002-03-09 18:51:58 +0000883{
884 printf(" %s\t", shortflag);
885 if (strlen(shortflag) < 8)
886 printf("\t");
887
888#ifdef HAVE_GETOPT_LONG
889 printf("%s\t", longflag);
890 if (strlen(longflag) < 8)
891 printf("\t\t");
892 else if (strlen(longflag) < 16)
893 printf("\t");
894#endif
895
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000896 printf("%s\n", _(desc));
Chris Allegretta3fc5d572002-03-09 18:51:58 +0000897}
898
David Lawrence Ramsey0341b582002-08-21 16:10:37 +0000899void usage(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000900{
901#ifdef HAVE_GETOPT_LONG
Chris Allegretta6df90f52002-07-19 01:08:59 +0000902 printf(_("Usage: nano [+LINE] [GNU long option] [option] [file]\n\n"));
903 printf(_("Option\t\tLong option\t\tMeaning\n"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000904#else
Chris Allegretta6df90f52002-07-19 01:08:59 +0000905 printf(_("Usage: nano [+LINE] [option] [file]\n\n"));
906 printf(_("Option\t\tMeaning\n"));
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +0000907#endif
Chris Allegretta3fc5d572002-03-09 18:51:58 +0000908
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000909 print1opt("-h, -?", "--help", N_("Show this message"));
910 print1opt(_("+LINE"), "", N_("Start at line number LINE"));
Chris Allegretta7004c282001-09-22 00:42:10 +0000911#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000912 print1opt("-A", "--smarthome", N_("Enable smart home key"));
913 print1opt("-B", "--backup", N_("Backup existing files on save"));
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000914 print1opt(_("-E [dir]"), _("--backupdir=[dir]"), N_("Directory for writing backup files"));
Chris Allegretta7004c282001-09-22 00:42:10 +0000915#endif
Chris Allegretta355fbe52001-07-14 19:32:47 +0000916#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000917 print1opt("-F", "--multibuffer", N_("Enable multiple file buffers"));
Chris Allegretta355fbe52001-07-14 19:32:47 +0000918#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +0000919#ifdef ENABLE_NANORC
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +0000920#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000921 print1opt("-H", "--historylog", N_("Log & read search/replace string history"));
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +0000922#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000923 print1opt("-I", "--ignorercfiles", N_("Don't look at nanorc files"));
Chris Allegretta6df90f52002-07-19 01:08:59 +0000924#endif
Chris Allegretta8fa1e282001-09-22 04:20:25 +0000925#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000926 print1opt("-N", "--noconvert", N_("Don't convert files from DOS/Mac format"));
Chris Allegretta8fa1e282001-09-22 04:20:25 +0000927#endif
Chris Allegrettae4f940d2002-03-03 22:36:36 +0000928#ifndef DISABLE_JUSTIFY
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000929 print1opt(_("-Q [str]"), _("--quotestr=[str]"), N_("Quoting string, default \"> \""));
Chris Allegrettae4f940d2002-03-03 22:36:36 +0000930#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +0000931#ifdef HAVE_REGEX_H
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000932 print1opt("-R", "--regexp", N_("Do regular expression searches"));
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +0000933#endif
Chris Allegretta3e3ae942001-09-22 19:02:04 +0000934#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000935 print1opt("-S", "--smooth", N_("Smooth scrolling"));
Chris Allegretta3e3ae942001-09-22 19:02:04 +0000936#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000937 print1opt(_("-T [#cols]"), _("--tabsize=[#cols]"), N_("Set width of a tab in cols to #cols"));
938 print1opt("-V", "--version", N_("Print version information and exit"));
Chris Allegretta09900ff2002-05-04 04:23:30 +0000939#ifdef ENABLE_COLOR
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000940 print1opt(_("-Y [str]"), _("--syntax [str]"), N_("Syntax definition to use"));
Chris Allegretta09900ff2002-05-04 04:23:30 +0000941#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000942 print1opt("-Z", "--restricted", N_("Restricted mode"));
943 print1opt("-c", "--const", N_("Constantly show cursor position"));
Chris Allegrettad19e9912000-07-12 18:14:51 +0000944#ifndef NANO_SMALL
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000945 print1opt("-d", "--rebinddelete", N_("Fix Backspace/Delete confusion problem"));
946 print1opt("-i", "--autoindent", N_("Automatically indent new lines"));
947 print1opt("-k", "--cut", N_("Cut from cursor to end of line"));
Chris Allegrettad19e9912000-07-12 18:14:51 +0000948#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000949 print1opt("-l", "--nofollow", N_("Don't follow symbolic links, overwrite"));
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +0000950#ifndef DISABLE_MOUSE
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000951 print1opt("-m", "--mouse", N_("Enable mouse"));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000952#endif
Chris Allegrettae1f14522001-09-19 03:19:43 +0000953#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000954 print1opt(_("-o [dir]"), _("--operatingdir=[dir]"), N_("Set operating directory"));
Chris Allegrettae1f14522001-09-19 03:19:43 +0000955#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000956 print1opt("-p", "--preserve", N_("Preserve XON (^Q) and XOFF (^S) keys"));
Chris Allegretta6fe61492001-05-21 12:56:25 +0000957#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000958 print1opt(_("-r [#cols]"), _("--fill=[#cols]"), N_("Set fill cols to (wrap lines at) #cols"));
Chris Allegretta6fe61492001-05-21 12:56:25 +0000959#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000960#ifndef DISABLE_SPELLER
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000961 print1opt(_("-s [prog]"), _("--speller=[prog]"), N_("Enable alternate speller"));
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000962#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000963 print1opt("-t", "--tempfile", N_("Auto save on exit, don't prompt"));
964 print1opt("-v", "--view", N_("View (read only) mode"));
Chris Allegrettacef7fbb2001-04-02 05:36:08 +0000965#ifndef DISABLE_WRAPPING
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000966 print1opt("-w", "--nowrap", N_("Don't wrap long lines"));
Chris Allegrettacef7fbb2001-04-02 05:36:08 +0000967#endif
David Lawrence Ramsey76f63ff2004-08-05 15:45:09 +0000968 print1opt("-x", "--nohelp", N_("Don't show help window"));
969 print1opt("-z", "--suspend", N_("Enable suspend"));
Chris Allegretta6df90f52002-07-19 01:08:59 +0000970
David Lawrence Ramsey22fac782004-08-05 15:16:19 +0000971 /* This is a special case. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000972 printf(" %s\t\t\t%s\n","-a, -b, -e, -f, -g, -j", _("(ignored, for Pico compatibility)"));
Chris Allegretta3fc5d572002-03-09 18:51:58 +0000973
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000974 exit(0);
975}
976
David Lawrence Ramsey0341b582002-08-21 16:10:37 +0000977void version(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000978{
Chris Allegrettac46dd812001-02-14 14:28:27 +0000979 printf(_(" GNU nano version %s (compiled %s, %s)\n"),
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000980 VERSION, __TIME__, __DATE__);
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000981 printf(_
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +0000982 (" Email: nano@nano-editor.org Web: http://www.nano-editor.org/"));
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000983 printf(_("\n Compiled options:"));
Chris Allegrettaff269f82000-12-01 18:46:01 +0000984
Chris Allegrettae6600372003-01-17 03:39:41 +0000985#ifndef ENABLE_NLS
986 printf(" --disable-nls");
987#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000988#ifdef DEBUG
989 printf(" --enable-debug");
990#endif
Chris Allegretta8a0de3b2000-11-24 20:45:14 +0000991#ifdef NANO_EXTRA
992 printf(" --enable-extra");
Jordi Mallach5285ca92002-01-17 12:40:33 +0000993#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +0000994#ifdef NANO_SMALL
995 printf(" --enable-tiny");
996#else
Chris Allegretta6b58acd2001-04-12 03:01:53 +0000997#ifdef DISABLE_BROWSER
Chris Allegretta6636dc32001-01-05 05:41:07 +0000998 printf(" --disable-browser");
Chris Allegretta6b58acd2001-04-12 03:01:53 +0000999#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001000#ifdef DISABLE_HELP
1001 printf(" --disable-help");
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001002#endif
1003#ifdef DISABLE_JUSTIFY
Chris Allegrettaff269f82000-12-01 18:46:01 +00001004 printf(" --disable-justify");
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001005#endif
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00001006#ifdef DISABLE_MOUSE
Chris Allegretta84de5522001-04-12 14:51:48 +00001007 printf(" --disable-mouse");
Chris Allegrettab7d00ef2000-12-18 05:36:51 +00001008#endif
Chris Allegrettae1f14522001-09-19 03:19:43 +00001009#ifdef DISABLE_OPERATINGDIR
1010 printf(" --disable-operatingdir");
1011#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001012#ifdef DISABLE_SPELLER
1013 printf(" --disable-speller");
1014#endif
1015#ifdef DISABLE_TABCOMP
1016 printf(" --disable-tabcomp");
1017#endif
Chris Allegretta84de5522001-04-12 14:51:48 +00001018#endif /* NANO_SMALL */
Chris Allegrettacef7fbb2001-04-02 05:36:08 +00001019#ifdef DISABLE_WRAPPING
1020 printf(" --disable-wrapping");
1021#endif
David Lawrence Ramseydc60b722002-10-25 16:08:53 +00001022#ifdef DISABLE_ROOTWRAP
1023 printf(" --disable-wrapping-as-root");
1024#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001025#ifdef ENABLE_COLOR
1026 printf(" --enable-color");
1027#endif
1028#ifdef ENABLE_MULTIBUFFER
1029 printf(" --enable-multibuffer");
1030#endif
1031#ifdef ENABLE_NANORC
1032 printf(" --enable-nanorc");
1033#endif
Chris Allegretta8a0de3b2000-11-24 20:45:14 +00001034#ifdef USE_SLANG
1035 printf(" --with-slang");
1036#endif
1037 printf("\n");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001038}
1039
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001040int no_help(void)
1041{
Chris Allegretta6df90f52002-07-19 01:08:59 +00001042 return ISSET(NO_HELP) ? 2 : 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001043}
1044
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001045void nano_disabled_msg(void)
Chris Allegrettaff269f82000-12-01 18:46:01 +00001046{
Chris Allegretta6df90f52002-07-19 01:08:59 +00001047 statusbar(_("Sorry, support for this function has been disabled"));
Chris Allegrettaff269f82000-12-01 18:46:01 +00001048}
1049
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001050#ifndef NANO_SMALL
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001051RETSIGTYPE cancel_fork(int signal)
1052{
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001053 if (kill(pid, SIGKILL) == -1)
1054 nperror("kill");
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001055}
1056
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001057/* Return TRUE on success. */
1058bool open_pipe(const char *command)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001059{
1060 int fd[2];
1061 FILE *f;
1062 struct sigaction oldaction, newaction;
David Lawrence Ramsey22fac782004-08-05 15:16:19 +00001063 /* Original and temporary handlers for
1064 * SIGINT. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001065 bool sig_failed = FALSE;
1066 /* sig_failed means that sigaction() failed without changing the
1067 * signal handlers.
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00001068 *
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001069 * We use this variable since it is important to put things back
1070 * when we finish, even if we get errors. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001071
1072 /* Make our pipes. */
1073
1074 if (pipe(fd) == -1) {
1075 statusbar(_("Could not pipe"));
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001076 return FALSE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001077 }
1078
1079 /* Fork a child. */
1080
1081 if ((pid = fork()) == 0) {
1082 close(fd[0]);
1083 dup2(fd[1], fileno(stdout));
1084 dup2(fd[1], fileno(stderr));
1085 /* If execl() returns at all, there was an error. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001086
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00001087 execl("/bin/sh", "sh", "-c", command, 0);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001088 exit(0);
1089 }
1090
1091 /* Else continue as parent. */
1092
1093 close(fd[1]);
1094
1095 if (pid == -1) {
1096 close(fd[0]);
1097 statusbar(_("Could not fork"));
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001098 return FALSE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001099 }
1100
1101 /* Before we start reading the forked command's output, we set
1102 * things up so that ^C will cancel the new process. */
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00001103
David Lawrence Ramseye608f942004-05-19 16:04:27 +00001104 /* Enable interpretation of the special control keys so that we get
1105 * SIGINT when Ctrl-C is pressed. */
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00001106 enable_signals();
1107
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00001108 if (sigaction(SIGINT, NULL, &newaction) == -1) {
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001109 sig_failed = TRUE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001110 nperror("sigaction");
1111 } else {
1112 newaction.sa_handler = cancel_fork;
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00001113 if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001114 sig_failed = TRUE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001115 nperror("sigaction");
1116 }
1117 }
1118 /* Note that now oldaction is the previous SIGINT signal handler,
1119 * to be restored later. */
1120
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001121 f = fdopen(fd[0], "rb");
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001122 if (f == NULL)
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001123 nperror("fdopen");
David Lawrence Ramsey00d77982004-08-07 21:27:37 +00001124
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00001125 read_file(f, "stdin");
David Lawrence Ramsey22fac782004-08-05 15:16:19 +00001126 /* If multibuffer mode is on, we could be here in view mode. If so,
1127 * don't set the modification flag. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001128 if (!ISSET(VIEW_MODE))
1129 set_modified();
1130
1131 if (wait(NULL) == -1)
1132 nperror("wait");
1133
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001134 if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001135 nperror("sigaction");
1136
David Lawrence Ramseye608f942004-05-19 16:04:27 +00001137 /* Disable interpretation of the special control keys so that we can
1138 * use Ctrl-C for other things. */
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00001139 disable_signals();
1140
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001141 return TRUE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001142}
David Lawrence Ramseya9cd41c2004-03-05 19:54:58 +00001143#endif /* !NANO_SMALL */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001144
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001145void do_verbatim_input(void)
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001146{
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001147 int *kbinput; /* Used to hold verbatim input. */
1148 size_t kbinput_len; /* Length of verbatim input. */
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001149
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001150 statusbar(_("Verbatim input"));
David Lawrence Ramsey48ae9862004-05-28 17:23:33 +00001151
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001152 /* Read in all the verbatim characters. */
1153 kbinput = get_verbatim_kbinput(edit, &kbinput_len);
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001154
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001155 /* Display all the verbatim characters at once. */
1156 do_output(kbinput, kbinput_len);
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001157
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001158 free(kbinput);
David Lawrence Ramseya593f532003-11-28 19:47:42 +00001159}
1160
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001161void do_backspace(void)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001162{
David Lawrence Ramseyd91ab6e2003-09-07 23:57:24 +00001163 if (current != fileage || current_x > 0) {
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00001164 do_left(FALSE);
David Lawrence Ramseyd91ab6e2003-09-07 23:57:24 +00001165 do_delete();
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001166 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001167}
1168
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001169void do_delete(void)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001170{
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001171 bool do_refresh = FALSE;
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00001172 /* Do we have to call edit_refresh(), or can we get away with
1173 * update_line()? */
1174
David Lawrence Ramsey40a6c8c2004-11-27 21:10:11 +00001175 assert(current != NULL && current->data != NULL &&
1176 current_x <= strlen(current->data));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001177
1178 placewewant = xplustabs();
1179
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001180 if (current->data[current_x] != '\0') {
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00001181 int char_len = parse_char(current->data + current_x, NULL,
1182 NULL
1183#ifdef NANO_WIDE
1184 , NULL
1185#endif
1186 );
1187 size_t line_len = strlen(current->data + current_x);
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001188
1189 assert(current_x < strlen(current->data));
1190
1191 /* Let's get dangerous. */
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00001192 charmove(&current->data[current_x],
1193 &current->data[current_x + char_len],
1194 line_len - char_len + 1);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001195
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00001196 null_at(&current->data, current_x + line_len - char_len);
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001197#ifndef NANO_SMALL
1198 if (current_x < mark_beginx && mark_beginbuf == current)
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00001199 mark_beginx -= char_len;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001200#endif
David Lawrence Ramseyc8ade442004-12-28 22:36:13 +00001201 totsize -= char_len;
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001202 } else if (current != filebot && (current->next != filebot ||
1203 current->data[0] == '\0')) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001204 /* We can delete the line before filebot only if it is blank: it
David Lawrence Ramsey32d19ce2004-05-24 05:05:07 +00001205 * becomes the new magicline then. */
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001206 filestruct *foo = current->next;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001207
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001208 assert(current_x == strlen(current->data));
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00001209
1210 /* If we're deleting at the end of a line, we need to call
1211 * edit_refresh(). */
1212 if (current->data[current_x] == '\0')
1213 do_refresh = TRUE;
1214
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00001215 current->data = charealloc(current->data,
1216 current_x + strlen(foo->data) + 1);
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001217 strcpy(current->data + current_x, foo->data);
1218#ifndef NANO_SMALL
1219 if (mark_beginbuf == current->next) {
1220 mark_beginx += current_x;
1221 mark_beginbuf = current;
1222 }
1223#endif
David Lawrence Ramseyb9775152004-03-19 02:15:42 +00001224 if (filebot == foo)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001225 filebot = current;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001226
1227 unlink_node(foo);
1228 delete_node(foo);
1229 renumber(current);
1230 totlines--;
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00001231 totsize--;
David Lawrence Ramsey00d77982004-08-07 21:27:37 +00001232#ifndef DISABLE_WRAPPING
David Lawrence Ramsey604caf32004-04-19 02:44:13 +00001233 wrap_reset();
David Lawrence Ramsey00d77982004-08-07 21:27:37 +00001234#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001235 } else
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001236 return;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001237
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001238 set_modified();
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00001239
1240#ifdef ENABLE_COLOR
1241 /* If color syntaxes are turned on, we need to call
1242 * edit_refresh(). */
1243 if (ISSET(COLOR_SYNTAX))
1244 do_refresh = TRUE;
1245#endif
1246
1247 if (do_refresh)
1248 edit_refresh();
1249 else
1250 update_line(current, current_x);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001251}
1252
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001253void do_tab(void)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001254{
David Lawrence Ramsey74835712004-12-04 17:41:52 +00001255 int kbinput = '\t';
1256 do_output(&kbinput, 1);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001257}
1258
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001259/* Someone hits return *gasp!* */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001260void do_enter(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001261{
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001262 filestruct *newnode = make_new_node(current);
1263 size_t extra = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001264
Chris Allegretta6df90f52002-07-19 01:08:59 +00001265 assert(current != NULL && current->data != NULL);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001266
Chris Allegrettaff989832001-09-17 13:48:00 +00001267#ifndef NANO_SMALL
Chris Allegretta6df90f52002-07-19 01:08:59 +00001268 /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001269 if (ISSET(AUTOINDENT)) {
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001270 /* If we are breaking the line in the indentation, the new
1271 * indentation should have only current_x characters, and
1272 * current_x should not change. */
1273 extra = indent_length(current->data);
1274 if (extra > current_x)
Chris Allegrettadab017e2002-04-23 10:56:06 +00001275 extra = current_x;
Chris Allegrettadab017e2002-04-23 10:56:06 +00001276 totsize += extra;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001277 }
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001278#endif
1279 newnode->data = charalloc(strlen(current->data + current_x) +
1280 extra + 1);
1281 strcpy(&newnode->data[extra], current->data + current_x);
1282#ifndef NANO_SMALL
1283 if (ISSET(AUTOINDENT))
1284 strncpy(newnode->data, current->data, extra);
1285#endif
1286 null_at(&current->data, current_x);
1287#ifndef NANO_SMALL
1288 if (current == mark_beginbuf && current_x < mark_beginx) {
1289 mark_beginbuf = newnode;
1290 mark_beginx += extra - current_x;
1291 }
1292#endif
1293 current_x = extra;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001294
David Lawrence Ramsey228148b2004-05-27 20:09:52 +00001295 if (current == filebot)
Chris Allegrettae3167732001-03-18 16:59:34 +00001296 filebot = newnode;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001297 splice_node(current, newnode, current->next);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001298
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001299 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++;
David Lawrence Ramsey4d97a582004-12-29 16:42:48 +00001305 totsize++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001306 set_modified();
Chris Allegrettab0ae3932000-06-15 23:39:14 +00001307 placewewant = xplustabs();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001308}
1309
Chris Allegrettaad3f4782001-10-02 03:54:13 +00001310#ifndef NANO_SMALL
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001311void do_next_word(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001312{
David Lawrence Ramsey86e851b2004-07-28 20:46:25 +00001313 size_t old_pww = placewewant;
David Lawrence Ramsey381d4832004-10-18 01:51:43 +00001314 const filestruct *old_current = current;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001315 assert(current != NULL && current->data != NULL);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001316
Chris Allegretta6df90f52002-07-19 01:08:59 +00001317 /* Skip letters in this word first. */
1318 while (current->data[current_x] != '\0' &&
David Lawrence Ramseyd9fb3e62004-10-21 22:49:51 +00001319 isalnum(current->data[current_x]))
Chris Allegretta6df90f52002-07-19 01:08:59 +00001320 current_x++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001321
Chris Allegretta6df90f52002-07-19 01:08:59 +00001322 for (; current != NULL; current = current->next) {
1323 while (current->data[current_x] != '\0' &&
David Lawrence Ramseyd9fb3e62004-10-21 22:49:51 +00001324 !isalnum(current->data[current_x]))
Chris Allegretta6df90f52002-07-19 01:08:59 +00001325 current_x++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001326
Chris Allegretta6df90f52002-07-19 01:08:59 +00001327 if (current->data[current_x] != '\0')
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001328 break;
1329
Chris Allegretta6df90f52002-07-19 01:08:59 +00001330 current_x = 0;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001331 }
Chris Allegretta6df90f52002-07-19 01:08:59 +00001332 if (current == NULL)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001333 current = filebot;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001334
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001335 placewewant = xplustabs();
Chris Allegretta9e2934f2000-12-01 23:49:48 +00001336
David Lawrence Ramsey381d4832004-10-18 01:51:43 +00001337 /* Update the screen. */
1338 edit_redraw(old_current, old_pww);
Chris Allegretta6232d662002-05-12 19:52:15 +00001339}
1340
Chris Allegretta6df90f52002-07-19 01:08:59 +00001341/* The same thing for backwards. */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001342void do_prev_word(void)
Chris Allegretta76e291b2001-10-14 19:05:10 +00001343{
David Lawrence Ramsey86e851b2004-07-28 20:46:25 +00001344 size_t old_pww = placewewant;
David Lawrence Ramsey381d4832004-10-18 01:51:43 +00001345 const filestruct *old_current = current;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001346 assert(current != NULL && current->data != NULL);
Chris Allegretta76e291b2001-10-14 19:05:10 +00001347
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001348 current_x++;
1349
Chris Allegretta6df90f52002-07-19 01:08:59 +00001350 /* Skip letters in this word first. */
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001351 while (current_x > 0 && isalnum(current->data[current_x - 1]))
Chris Allegretta6df90f52002-07-19 01:08:59 +00001352 current_x--;
Chris Allegretta76e291b2001-10-14 19:05:10 +00001353
Chris Allegretta6df90f52002-07-19 01:08:59 +00001354 for (; current != NULL; current = current->prev) {
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001355 while (current_x > 0 && !isalnum(current->data[current_x - 1]))
Chris Allegretta6df90f52002-07-19 01:08:59 +00001356 current_x--;
Chris Allegretta76e291b2001-10-14 19:05:10 +00001357
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001358 if (current_x > 0)
Chris Allegretta6df90f52002-07-19 01:08:59 +00001359 break;
Chris Allegretta76e291b2001-10-14 19:05:10 +00001360
Chris Allegretta6df90f52002-07-19 01:08:59 +00001361 if (current->prev != NULL)
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001362 current_x = strlen(current->prev->data) + 1;
Chris Allegretta76e291b2001-10-14 19:05:10 +00001363 }
Chris Allegretta76e291b2001-10-14 19:05:10 +00001364
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001365 current_x--;
1366
David Lawrence Ramsey1b525e92004-05-24 02:35:02 +00001367 if (current == NULL) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00001368 current = fileage;
1369 current_x = 0;
David Lawrence Ramsey1b525e92004-05-24 02:35:02 +00001370 } else {
David Lawrence Ramseyd9fb3e62004-10-21 22:49:51 +00001371 while (current_x > 0 && isalnum(current->data[current_x - 1]))
David Lawrence Ramsey1b525e92004-05-24 02:35:02 +00001372 current_x--;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001373 }
1374
Chris Allegretta76e291b2001-10-14 19:05:10 +00001375 placewewant = xplustabs();
1376
David Lawrence Ramsey381d4832004-10-18 01:51:43 +00001377 /* Update the screen. */
1378 edit_redraw(old_current, old_pww);
Chris Allegretta6232d662002-05-12 19:52:15 +00001379}
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001380
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001381void do_mark(void)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001382{
David Lawrence Ramseyf03c78b2003-09-28 21:26:49 +00001383 TOGGLE(MARK_ISSET);
1384 if (ISSET(MARK_ISSET)) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001385 statusbar(_("Mark Set"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001386 mark_beginbuf = current;
1387 mark_beginx = current_x;
1388 } else {
1389 statusbar(_("Mark UNset"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001390 edit_refresh();
1391 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001392}
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00001393#endif /* !NANO_SMALL */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001394
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +00001395#ifndef DISABLE_WRAPPING
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001396void wrap_reset(void)
1397{
David Lawrence Ramsey85529b32004-06-22 15:38:47 +00001398 same_line_wrap = FALSE;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001399}
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +00001400#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001401
Chris Allegrettacef7fbb2001-04-02 05:36:08 +00001402#ifndef DISABLE_WRAPPING
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001403/* We wrap the given line. Precondition: we assume the cursor has been
1404 * moved forward since the last typed character. Return value: whether
1405 * we wrapped. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001406bool do_wrap(filestruct *inptr)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001407{
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001408 size_t len = strlen(inptr->data);
1409 /* Length of the line we wrap. */
1410 size_t i = 0;
1411 /* Generic loop variable. */
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001412 ssize_t wrap_loc = -1;
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001413 /* Index of inptr->data where we wrap. */
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001414 ssize_t word_back = -1;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001415#ifndef NANO_SMALL
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001416 const char *indentation = NULL;
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001417 /* Indentation to prepend to the new line. */
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001418 size_t indent_len = 0; /* strlen(indentation) */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001419#endif
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001420 const char *after_break; /* Text after the wrap point. */
1421 size_t after_break_len; /* strlen(after_break) */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001422 bool wrapping = FALSE; /* Do we prepend to the next line? */
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001423 const char *wrap_line = NULL;
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001424 /* The next line, minus indentation. */
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001425 size_t wrap_line_len = 0; /* strlen(wrap_line) */
1426 char *newline = NULL; /* The line we create. */
1427 size_t new_line_len = 0; /* Eventual length of newline. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001428
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001429/* There are three steps. First, we decide where to wrap. Then, we
1430 * create the new wrap line. Finally, we clean up. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001431
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001432/* Step 1, finding where to wrap. We are going to add a new line
David Lawrence Ramsey89bb9372004-05-29 16:47:52 +00001433 * after a whitespace character. In this step, we set wrap_loc as the
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001434 * location of this replacement.
1435 *
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001436 * Where should we break the line? We need the last legal wrap point
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001437 * such that the last word before it ended at or before fill. If there
1438 * is no such point, we settle for the first legal wrap point.
1439 *
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001440 * A legal wrap point is a whitespace character that is not followed by
1441 * whitespace.
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001442 *
1443 * If there is no legal wrap point or we found the last character of the
1444 * line, we should return without wrapping.
1445 *
1446 * Note that the initial indentation does not count as a legal wrap
1447 * point if we are going to auto-indent!
1448 *
David Lawrence Ramsey8d911992004-05-29 17:05:52 +00001449 * Note that the code below could be optimized, by not calling
1450 * strnlenpt() so often. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001451
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001452#ifndef NANO_SMALL
Chris Allegretta6df90f52002-07-19 01:08:59 +00001453 if (ISSET(AUTOINDENT))
1454 i = indent_length(inptr->data);
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001455#endif
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001456 wrap_line = inptr->data + i;
David Lawrence Ramsey4e254102003-11-05 22:04:08 +00001457 for (; i < len; i++, wrap_line++) {
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001458 /* Record where the last word ended. */
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00001459 if (!isblank(*wrap_line))
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001460 word_back = i;
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001461 /* If we have found a legal wrap point and the current word
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001462 * extends too far, then we stop. */
David Lawrence Ramsey40a6c8c2004-11-27 21:10:11 +00001463 if (wrap_loc != -1 &&
1464 strnlenpt(inptr->data, word_back + 1) > fill)
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001465 break;
David Lawrence Ramseyb5a7a5a2004-11-23 03:42:43 +00001466 /* We record the latest legal wrap point. */
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00001467 if (word_back != i && !isblank(wrap_line[1]))
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001468 wrap_loc = i;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001469 }
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001470 if (i == len)
1471 return FALSE;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001472
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001473 /* Step 2, making the new wrap line. It will consist of indentation
1474 * + after_break + " " + wrap_line (although indentation and
1475 * wrap_line are conditional on flags and #defines). */
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001476
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001477 /* after_break is the text that will be moved to the next line. */
1478 after_break = inptr->data + wrap_loc + 1;
1479 after_break_len = len - wrap_loc - 1;
David Lawrence Ramsey6a2f0682004-12-20 01:13:55 +00001480
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001481 assert(after_break_len == strlen(after_break));
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001482
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001483 /* new_line_len will later be increased by the lengths of indentation
1484 * and wrap_line. */
1485 new_line_len = after_break_len;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001486
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001487 /* We prepend the wrapped text to the next line, if the flag is set,
1488 * and there is a next line, and prepending would not make the line
1489 * too long. */
David Lawrence Ramseyc97acfb2003-09-10 20:08:00 +00001490 if (same_line_wrap && inptr->next) {
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001491 wrap_line = inptr->next->data;
1492 wrap_line_len = strlen(wrap_line);
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001493
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001494 /* +1 for the space between after_break and wrap_line. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001495 if ((new_line_len + 1 + wrap_line_len) <= fill) {
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001496 wrapping = TRUE;
David Lawrence Ramsey45641702004-12-18 20:29:42 +00001497 new_line_len += 1 + wrap_line_len;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001498 }
1499 }
Chris Allegretta56214c62001-09-27 02:46:53 +00001500
Chris Allegrettaff989832001-09-17 13:48:00 +00001501#ifndef NANO_SMALL
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001502 if (ISSET(AUTOINDENT)) {
Chris Allegretta81dea022002-09-21 02:19:45 +00001503 /* Indentation comes from the next line if wrapping, else from
1504 * this line. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001505 indentation = (wrapping ? wrap_line : inptr->data);
Chris Allegretta6df90f52002-07-19 01:08:59 +00001506 indent_len = indent_length(indentation);
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001507 if (wrapping)
Chris Allegretta81dea022002-09-21 02:19:45 +00001508 /* The wrap_line text should not duplicate indentation.
1509 * Note in this case we need not increase new_line_len. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001510 wrap_line += indent_len;
1511 else
1512 new_line_len += indent_len;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001513 }
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001514#endif
Adam Rogoyski1e9183f2001-03-13 18:36:03 +00001515
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001516 /* Now we allocate the new line and copy into it. */
1517 newline = charalloc(new_line_len + 1); /* +1 for \0 */
David Lawrence Ramsey85529b32004-06-22 15:38:47 +00001518 new_line_len = 0;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001519 *newline = '\0';
1520
1521#ifndef NANO_SMALL
Chris Allegretta6df90f52002-07-19 01:08:59 +00001522 if (ISSET(AUTOINDENT)) {
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001523 strncpy(newline, indentation, indent_len);
Chris Allegretta6df90f52002-07-19 01:08:59 +00001524 newline[indent_len] = '\0';
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001525 new_line_len = indent_len;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001526 }
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001527#endif
1528 strcat(newline, after_break);
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001529 new_line_len += after_break_len;
David Lawrence Ramsey6a2f0682004-12-20 01:13:55 +00001530
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001531 /* We end the old line after wrap_loc. Note that this does not eat
1532 * the space. */
Chris Allegretta67ca2aa2002-09-19 23:19:34 +00001533 null_at(&inptr->data, wrap_loc + 1);
1534 totsize++;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001535 if (wrapping) {
Chris Allegretta67ca2aa2002-09-19 23:19:34 +00001536 /* In this case, totsize increases by 1 since we add a space
Chris Allegretta81dea022002-09-21 02:19:45 +00001537 * between after_break and wrap_line. If the line already ends
Chris Allegretta43000922002-09-21 15:41:33 +00001538 * in a tab or a space, we don't add a space and decrement
1539 * totsize to account for that. */
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001540 if (!isblank(newline[new_line_len - 1]))
Chris Allegretta81dea022002-09-21 02:19:45 +00001541 strcat(newline, " ");
1542 else
1543 totsize--;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001544 strcat(newline, wrap_line);
1545 free(inptr->next->data);
1546 inptr->next->data = newline;
1547 } else {
1548 filestruct *temp = (filestruct *)nmalloc(sizeof(filestruct));
Chris Allegretta6df90f52002-07-19 01:08:59 +00001549
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001550 /* In this case, the file size changes by +1 for the new line,
1551 * and +indent_len for the new indentation. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001552#ifndef NANO_SMALL
1553 totsize += indent_len;
1554#endif
1555 totlines++;
1556 temp->data = newline;
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001557 temp->prev = inptr;
1558 temp->next = inptr->next;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001559 temp->prev->next = temp;
David Lawrence Ramsey6a2f0682004-12-20 01:13:55 +00001560
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001561 /* If temp->next is NULL, then temp is the last line of the
1562 * file, so we must set filebot. */
1563 if (temp->next != NULL)
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001564 temp->next->prev = temp;
1565 else
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001566 filebot = temp;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001567 }
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001568
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001569 /* Step 3, clean up. Here we reposition the cursor and mark, and do
1570 * some other sundry things. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001571
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001572 /* Later wraps of this line will be prepended to the next line. */
David Lawrence Ramsey85529b32004-06-22 15:38:47 +00001573 same_line_wrap = TRUE;
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001574
1575 /* Each line knows its line number. We recalculate these if we
1576 * inserted a new line. */
1577 if (!wrapping)
1578 renumber(inptr);
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001579
Chris Allegretta6df90f52002-07-19 01:08:59 +00001580 /* If the cursor was after the break point, we must move it. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001581 if (current_x > wrap_loc) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00001582 current = current->next;
1583 current_x -=
Chris Allegrettaff989832001-09-17 13:48:00 +00001584#ifndef NANO_SMALL
Chris Allegretta6df90f52002-07-19 01:08:59 +00001585 -indent_len +
Chris Allegrettaff989832001-09-17 13:48:00 +00001586#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00001587 wrap_loc + 1;
1588 wrap_reset();
1589 placewewant = xplustabs();
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001590 }
1591
Chris Allegretta6df90f52002-07-19 01:08:59 +00001592#ifndef NANO_SMALL
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00001593 /* If the mark was on this line after the wrap point, we move it
1594 * down. If it was on the next line and we wrapped, we move it
Chris Allegretta6df90f52002-07-19 01:08:59 +00001595 * right. */
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001596 if (mark_beginbuf == inptr && mark_beginx > wrap_loc) {
1597 mark_beginbuf = inptr->next;
Chris Allegrettadffa2072002-07-24 01:02:26 +00001598 mark_beginx -= wrap_loc - indent_len + 1;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001599 } else if (wrapping && mark_beginbuf == inptr->next)
Chris Allegretta7162e3d2002-04-06 05:02:14 +00001600 mark_beginx += after_break_len;
Chris Allegretta6df90f52002-07-19 01:08:59 +00001601#endif /* !NANO_SMALL */
Adam Rogoyski77f36de2000-06-07 03:56:54 +00001602
David Lawrence Ramsey9a527f52004-05-31 14:58:59 +00001603 return TRUE;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001604}
Chris Allegretta6df90f52002-07-19 01:08:59 +00001605#endif /* !DISABLE_WRAPPING */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001606
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001607#ifndef DISABLE_SPELLER
David Lawrence Ramseyaaad3af2003-08-31 16:44:10 +00001608/* A word is misspelled in the file. Let the user replace it. We
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001609 * return FALSE if the user cancels. */
1610bool do_int_spell_fix(const char *word)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001611{
David Lawrence Ramseyd4ca9f22004-10-26 21:14:56 +00001612 char *save_search, *save_replace;
David Lawrence Ramsey2eb74592004-10-21 16:25:44 +00001613 size_t current_x_save = current_x, pww_save = placewewant;
David Lawrence Ramseyd4ca9f22004-10-26 21:14:56 +00001614 filestruct *edittop_save = edittop, *current_save = current;
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001615 /* Save where we are. */
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001616 bool canceled = FALSE;
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001617 /* The return value. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001618 bool case_sens_set = ISSET(CASE_SENSITIVE);
David Lawrence Ramsey49d5c1b2004-10-11 13:55:33 +00001619#ifndef NANO_SMALL
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001620 bool reverse_search_set = ISSET(REVERSE_SEARCH);
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001621#endif
David Lawrence Ramseyb112fe62004-10-09 17:15:46 +00001622#ifdef HAVE_REGEX_H
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001623 bool regexp_set = ISSET(USE_REGEXP);
1624#endif
David Lawrence Ramsey9cf7b282004-10-15 16:35:34 +00001625#ifndef NANO_SMALL
1626 bool old_mark_set = ISSET(MARK_ISSET);
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001627 bool added_magicline = FALSE;
1628 /* Whether we added a magicline after filebot. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001629 bool right_side_up = FALSE;
1630 /* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark,
1631 * FALSE if (current, current_x) is. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001632 filestruct *top, *bot;
David Lawrence Ramsey46c604a2004-11-05 23:55:58 +00001633 size_t top_x, bot_x;
David Lawrence Ramsey9cf7b282004-10-15 16:35:34 +00001634#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001635
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001636 /* Make sure spell-check is case sensitive. */
Chris Allegretta1d8fa1f2002-12-10 00:53:21 +00001637 SET(CASE_SENSITIVE);
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001638
David Lawrence Ramsey49d5c1b2004-10-11 13:55:33 +00001639#ifndef NANO_SMALL
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001640 /* Make sure spell-check goes forward only. */
1641 UNSET(REVERSE_SEARCH);
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001642#endif
David Lawrence Ramseyb112fe62004-10-09 17:15:46 +00001643#ifdef HAVE_REGEX_H
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001644 /* Make sure spell-check doesn't use regular expressions. */
1645 UNSET(USE_REGEXP);
1646#endif
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001647
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001648 /* Save the current search/replace strings. */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001649 search_init_globals();
Chris Allegretta6df90f52002-07-19 01:08:59 +00001650 save_search = last_search;
1651 save_replace = last_replace;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001652
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001653 /* Set the search/replace strings to the misspelled word. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00001654 last_search = mallocstrcpy(NULL, word);
1655 last_replace = mallocstrcpy(NULL, word);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001656
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001657#ifndef NANO_SMALL
1658 if (old_mark_set) {
David Lawrence Ramseyf978f042004-11-04 16:45:48 +00001659 /* If the mark is on, partition the filestruct so that it
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001660 * contains only the marked text, keep track of whether the text
1661 * will have a magicline added when we're done correcting
1662 * misspelled words, and turn the mark off. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001663 mark_order((const filestruct **)&top, &top_x,
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001664 (const filestruct **)&bot, &bot_x, &right_side_up);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001665 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001666 added_magicline = (filebot->data[0] != '\0');
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001667 UNSET(MARK_ISSET);
1668 }
1669#endif
1670
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001671 /* Start from the top of the file. */
David Lawrence Ramsey2cc2a572004-10-18 02:06:53 +00001672 edittop = fileage;
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001673 current = fileage;
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00001674 current_x = (size_t)-1;
David Lawrence Ramsey2eb74592004-10-21 16:25:44 +00001675 placewewant = 0;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001676
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001677 /* Find the first whole-word occurrence of word. */
David Lawrence Ramseye5e88fd2004-10-31 13:20:30 +00001678 findnextstr_wrap_reset();
David Lawrence Ramsey77b284a2004-10-27 02:21:01 +00001679 while (findnextstr(TRUE, TRUE, FALSE, fileage, 0, word, NULL)) {
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001680 if (is_whole_word(current_x, current->data, word)) {
1681 edit_refresh();
Chris Allegrettad00e6df2000-11-29 04:33:26 +00001682
Chris Allegretta6df90f52002-07-19 01:08:59 +00001683 do_replace_highlight(TRUE, word);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001684
David Lawrence Ramsey4b7e3c32004-10-15 16:25:56 +00001685 /* Allow all instances of the word to be corrected. */
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001686 canceled = (statusq(FALSE, spell_list, word,
Chris Allegretta7662c862003-01-13 01:35:15 +00001687#ifndef NANO_SMALL
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001688 NULL,
Chris Allegretta7662c862003-01-13 01:35:15 +00001689#endif
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001690 _("Edit a replacement")) == -1);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001691
Chris Allegretta6df90f52002-07-19 01:08:59 +00001692 do_replace_highlight(FALSE, word);
Chris Allegretta1bc0c7e2002-01-08 15:00:24 +00001693
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001694 if (!canceled && strcmp(word, answer) != 0) {
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001695 current_x--;
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001696 do_replace_loop(word, current, &current_x, TRUE,
1697 &canceled);
Chris Allegretta1bc0c7e2002-01-08 15:00:24 +00001698 }
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001699
1700 break;
Chris Allegretta80838272001-12-02 06:03:22 +00001701 }
David Lawrence Ramsey53752e82004-10-18 22:19:22 +00001702 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001703
David Lawrence Ramsey46c604a2004-11-05 23:55:58 +00001704#ifndef NANO_SMALL
1705 if (old_mark_set) {
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +00001706 /* If the mark was on and we added a magicline, remove it
1707 * now. */
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001708 if (added_magicline)
1709 remove_magicline();
David Lawrence Ramsey46c604a2004-11-05 23:55:58 +00001710
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001711 /* Put the beginning and the end of the mark at the beginning
1712 * and the end of the spell-checked text. */
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001713 if (fileage == filebot)
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001714 bot_x += top_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001715 if (right_side_up) {
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001716 mark_beginx = top_x;
1717 current_x_save = bot_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001718 } else {
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001719 current_x_save = top_x;
1720 mark_beginx = bot_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00001721 }
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001722
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +00001723 /* Unpartition the filestruct so that it contains all the text
1724 * again, and turn the mark back on. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +00001725 unpartition_filestruct(&filepart);
David Lawrence Ramseycb093372004-11-06 00:18:27 +00001726 SET(MARK_ISSET);
David Lawrence Ramsey46c604a2004-11-05 23:55:58 +00001727 }
1728#endif
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001729
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001730 /* Restore the search/replace strings. */
1731 free(last_search);
1732 last_search = save_search;
1733 free(last_replace);
1734 last_replace = save_replace;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001735
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001736 /* Restore where we were. */
David Lawrence Ramsey2cc2a572004-10-18 02:06:53 +00001737 edittop = edittop_save;
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001738 current = current_save;
1739 current_x = current_x_save;
David Lawrence Ramsey2eb74592004-10-21 16:25:44 +00001740 placewewant = pww_save;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001741
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001742 /* Restore case sensitivity setting. */
1743 if (!case_sens_set)
1744 UNSET(CASE_SENSITIVE);
1745
David Lawrence Ramsey49d5c1b2004-10-11 13:55:33 +00001746#ifndef NANO_SMALL
David Lawrence Ramsey1044d742004-02-24 20:41:39 +00001747 /* Restore search/replace direction. */
Chris Allegretta23b74b22002-01-21 20:32:22 +00001748 if (reverse_search_set)
1749 SET(REVERSE_SEARCH);
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001750#endif
David Lawrence Ramseyb112fe62004-10-09 17:15:46 +00001751#ifdef HAVE_REGEX_H
David Lawrence Ramseyd93ee682004-10-09 16:59:43 +00001752 /* Restore regular expression usage setting. */
1753 if (regexp_set)
1754 SET(USE_REGEXP);
1755#endif
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001756
David Lawrence Ramsey9bf486f2004-10-26 20:58:30 +00001757 return !canceled;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001758}
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001759
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001760/* Integrated spell checking using 'spell' program. Return value: NULL
1761 * for normal termination, otherwise the error string. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00001762const char *do_int_speller(const char *tempfile_name)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001763{
Chris Allegretta271e9722000-11-10 18:15:43 +00001764 char *read_buff, *read_buff_ptr, *read_buff_word;
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001765 size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001766 int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001767 pid_t pid_spell, pid_sort, pid_uniq;
1768 int spell_status, sort_status, uniq_status;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001769
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001770 /* Create all three pipes up front. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001771 if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || pipe(uniq_fd) == -1)
1772 return _("Could not create pipe");
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001773
Chris Allegretta2a7a9a22002-12-10 00:16:27 +00001774 statusbar(_("Creating misspelled word list, please wait..."));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001775
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001776 /* A new process to run spell in. */
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001777 if ((pid_spell = fork()) == 0) {
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001778
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001779 /* Child continues (i.e, future spell process). */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001780
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001781 close(spell_fd[0]);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001782
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001783 /* Replace the standard input with the temp file. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001784 if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
1785 goto close_pipes_and_exit;
1786
1787 if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
1788 goto close_pipes_and_exit;
1789
Chris Allegretta271e9722000-11-10 18:15:43 +00001790 close(tempfile_fd);
1791
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001792 /* Send spell's standard output to the pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001793 if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1794 goto close_pipes_and_exit;
Chris Allegretta271e9722000-11-10 18:15:43 +00001795
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001796 close(spell_fd[1]);
Chris Allegretta271e9722000-11-10 18:15:43 +00001797
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001798 /* Start spell program; we are using PATH. */
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001799 execlp("spell", "spell", NULL);
1800
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001801 /* Should not be reached, if spell is found. */
Chris Allegretta271e9722000-11-10 18:15:43 +00001802 exit(1);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001803 }
1804
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001805 /* Parent continues here. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001806 close(spell_fd[1]);
1807
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001808 /* A new process to run sort in. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001809 if ((pid_sort = fork()) == 0) {
1810
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001811 /* Child continues (i.e, future spell process). Replace the
1812 * standard input with the standard output of the old pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001813 if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
1814 goto close_pipes_and_exit;
1815
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001816 close(spell_fd[0]);
1817
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001818 /* Send sort's standard output to the new pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001819 if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1820 goto close_pipes_and_exit;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001821
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001822 close(sort_fd[1]);
1823
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001824 /* Start sort program. Use -f to remove mixed case without
1825 * having to have ANOTHER pipe for tr. If this isn't portable,
1826 * let me know. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001827 execlp("sort", "sort", "-f", NULL);
1828
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001829 /* Should not be reached, if sort is found. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001830 exit(1);
1831 }
1832
Chris Allegretta2d5fc3a2003-01-16 03:11:23 +00001833 close(spell_fd[0]);
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001834 close(sort_fd[1]);
1835
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001836 /* A new process to run uniq in. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001837 if ((pid_uniq = fork()) == 0) {
1838
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001839 /* Child continues (i.e, future uniq process). Replace the
1840 * standard input with the standard output of the old pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001841 if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
1842 goto close_pipes_and_exit;
1843
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001844 close(sort_fd[0]);
1845
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001846 /* Send uniq's standard output to the new pipe. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001847 if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
1848 goto close_pipes_and_exit;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001849
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001850 close(uniq_fd[1]);
1851
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001852 /* Start uniq program; we are using PATH. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001853 execlp("uniq", "uniq", NULL);
1854
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001855 /* Should not be reached, if uniq is found. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001856 exit(1);
1857 }
1858
Chris Allegretta2d5fc3a2003-01-16 03:11:23 +00001859 close(sort_fd[0]);
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001860 close(uniq_fd[1]);
Chris Allegretta271e9722000-11-10 18:15:43 +00001861
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001862 /* Child process was not forked successfully. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001863 if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
1864 close(uniq_fd[0]);
Chris Allegretta334a9402002-12-16 04:25:53 +00001865 return _("Could not fork");
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001866 }
1867
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001868 /* Get system pipe buffer size. */
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001869 if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
1870 close(uniq_fd[0]);
Chris Allegretta334a9402002-12-16 04:25:53 +00001871 return _("Could not get size of pipe buffer");
Chris Allegretta271e9722000-11-10 18:15:43 +00001872 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001873
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001874 /* Read in the returned spelling errors. */
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001875 read_buff_read = 0;
1876 read_buff_size = pipe_buff_size + 1;
1877 read_buff = read_buff_ptr = charalloc(read_buff_size);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001878
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001879 while ((bytesread = read(uniq_fd[0], read_buff_ptr, pipe_buff_size)) > 0) {
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001880 read_buff_read += bytesread;
1881 read_buff_size += pipe_buff_size;
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00001882 read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size);
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001883 read_buff_ptr += read_buff_read;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001884
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001885 }
Chris Allegretta271e9722000-11-10 18:15:43 +00001886
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001887 *read_buff_ptr = (char)NULL;
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001888 close(uniq_fd[0]);
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001889
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001890 /* Process the spelling errors. */
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001891 read_buff_word = read_buff_ptr = read_buff;
1892
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001893 while (*read_buff_ptr != '\0') {
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001894
1895 if ((*read_buff_ptr == '\n') || (*read_buff_ptr == '\r')) {
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001896 *read_buff_ptr = (char)NULL;
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001897 if (read_buff_word != read_buff_ptr) {
1898 if (!do_int_spell_fix(read_buff_word)) {
1899 read_buff_word = read_buff_ptr;
1900 break;
1901 }
1902 }
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001903 read_buff_word = read_buff_ptr + 1;
1904 }
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001905 read_buff_ptr++;
1906 }
1907
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001908 /* Special case where last word doesn't end with \n or \r. */
Chris Allegrettaf21f3fc2002-03-25 03:26:27 +00001909 if (read_buff_word != read_buff_ptr)
1910 do_int_spell_fix(read_buff_word);
1911
Chris Allegretta271e9722000-11-10 18:15:43 +00001912 free(read_buff);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001913 replace_abort();
Chris Allegretta64fc78c2003-01-26 19:57:44 +00001914 edit_refresh();
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001915
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001916 /* Process end of spell process. */
Chris Allegretta334a9402002-12-16 04:25:53 +00001917 waitpid(pid_spell, &spell_status, 0);
1918 waitpid(pid_sort, &sort_status, 0);
1919 waitpid(pid_uniq, &uniq_status, 0);
Chris Allegretta5ad92ac2002-12-09 00:58:51 +00001920
Chris Allegretta334a9402002-12-16 04:25:53 +00001921 if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
1922 return _("Error invoking \"spell\"");
Chris Allegretta271e9722000-11-10 18:15:43 +00001923
Chris Allegretta334a9402002-12-16 04:25:53 +00001924 if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
1925 return _("Error invoking \"sort -f\"");
1926
1927 if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
1928 return _("Error invoking \"uniq\"");
1929
1930 /* Otherwise... */
1931 return NULL;
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001932
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001933 close_pipes_and_exit:
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001934 /* Don't leak any handles. */
Chris Allegretta3f1b6852003-01-12 23:54:05 +00001935 close(tempfile_fd);
1936 close(spell_fd[0]);
1937 close(spell_fd[1]);
1938 close(sort_fd[0]);
1939 close(sort_fd[1]);
1940 close(uniq_fd[0]);
1941 close(uniq_fd[1]);
1942 exit(1);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001943}
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001944
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001945/* External spell checking. Return value: NULL for normal termination,
1946 * otherwise the error string. */
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00001947const char *do_alt_speller(char *tempfile_name)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001948{
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001949 int alt_spell_status, lineno_save = current->lineno;
1950 size_t current_x_save = current_x, pww_save = placewewant;
1951 int current_y_save = current_y;
Chris Allegretta271e9722000-11-10 18:15:43 +00001952 pid_t pid_spell;
Chris Allegretta169ee842001-01-26 01:57:32 +00001953 char *ptr;
1954 static int arglen = 3;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00001955 static char **spellargs = NULL;
1956 FILE *f;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001957#ifndef NANO_SMALL
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00001958 bool old_mark_set = ISSET(MARK_ISSET);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001959 bool added_magicline = FALSE;
1960 /* Whether we added a magicline after filebot. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001961 bool right_side_up = FALSE;
1962 /* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark,
1963 * FALSE if (current, current_x) is. */
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00001964 filestruct *top, *bot;
1965 size_t top_x, bot_x;
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001966 int mbb_lineno_save = 0;
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001967 /* We're going to close the current file, and open the output of
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001968 * the alternate spell command. The line that mark_beginbuf
1969 * points to will be freed, so we save the line number and
1970 * restore afterwards. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001971 long old_totsize = totsize;
1972 /* Our saved value of totsize, used when we spell-check a marked
1973 * selection. */
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001974
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00001975 if (old_mark_set) {
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001976 /* If the mark is on, save the number of the line it starts on,
1977 * and then turn the mark off. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001978 mbb_lineno_save = mark_beginbuf->lineno;
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00001979 UNSET(MARK_ISSET);
1980 }
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001981#endif
1982
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001983 endwin();
Chris Allegretta27eb13f2000-11-05 16:52:21 +00001984
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001985 /* Set up an argument list to pass execvp(). */
Chris Allegrettae434b452001-01-27 19:25:00 +00001986 if (spellargs == NULL) {
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00001987 spellargs = (char **)nmalloc(arglen * sizeof(char *));
Chris Allegretta271e9722000-11-10 18:15:43 +00001988
Chris Allegrettae434b452001-01-27 19:25:00 +00001989 spellargs[0] = strtok(alt_speller, " ");
1990 while ((ptr = strtok(NULL, " ")) != NULL) {
1991 arglen++;
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00001992 spellargs = (char **)nrealloc(spellargs, arglen * sizeof(char *));
Chris Allegrettae434b452001-01-27 19:25:00 +00001993 spellargs[arglen - 3] = ptr;
Chris Allegretta169ee842001-01-26 01:57:32 +00001994 }
Chris Allegrettae434b452001-01-27 19:25:00 +00001995 spellargs[arglen - 1] = NULL;
1996 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001997 spellargs[arglen - 2] = tempfile_name;
Chris Allegrettae434b452001-01-27 19:25:00 +00001998
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001999 /* Start a new process for the alternate speller. */
Chris Allegretta6b58acd2001-04-12 03:01:53 +00002000 if ((pid_spell = fork()) == 0) {
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002001 /* Start alternate spell program; we are using PATH. */
Chris Allegretta169ee842001-01-26 01:57:32 +00002002 execvp(spellargs[0], spellargs);
Chris Allegretta271e9722000-11-10 18:15:43 +00002003
2004 /* Should not be reached, if alternate speller is found!!! */
Chris Allegretta271e9722000-11-10 18:15:43 +00002005 exit(1);
2006 }
2007
2008 /* Could not fork?? */
Chris Allegretta271e9722000-11-10 18:15:43 +00002009 if (pid_spell < 0)
Chris Allegretta334a9402002-12-16 04:25:53 +00002010 return _("Could not fork");
Chris Allegretta271e9722000-11-10 18:15:43 +00002011
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002012 /* Wait for alternate speller to complete. */
Chris Allegretta271e9722000-11-10 18:15:43 +00002013 wait(&alt_spell_status);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002014
David Lawrence Ramsey6a2f0682004-12-20 01:13:55 +00002015 if (!WIFEXITED(alt_spell_status) ||
2016 WEXITSTATUS(alt_spell_status) != 0) {
Chris Allegretta334a9402002-12-16 04:25:53 +00002017 char *altspell_error = NULL;
2018 char *invoke_error = _("Could not invoke \"%s\"");
David Lawrence Ramseyf3bea022004-12-20 01:18:49 +00002019 int msglen = strlen(invoke_error) + strlen(alt_speller) + 2;
Chris Allegretta334a9402002-12-16 04:25:53 +00002020
2021 altspell_error = charalloc(msglen);
2022 snprintf(altspell_error, msglen, invoke_error, alt_speller);
2023 return altspell_error;
2024 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002025
Chris Allegretta8f6c0692000-07-19 01:16:18 +00002026 refresh();
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002027
David Lawrence Ramsey439fbe32004-10-16 04:56:34 +00002028 /* Restore the terminal to its previous state. */
2029 terminal_init();
2030
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002031#ifndef NANO_SMALL
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00002032 if (old_mark_set) {
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002033 long part_totsize;
2034
2035 /* If the mark was on, partition the filestruct so that it
2036 * contains only the marked text, and keep track of whether the
2037 * temp file (which should contain the spell-checked marked
2038 * text) will have a magicline added when it's reloaded. */
2039 mark_order((const filestruct **)&top, &top_x,
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00002040 (const filestruct **)&bot, &bot_x, &right_side_up);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002041 filepart = partition_filestruct(top, top_x, bot, bot_x);
2042 added_magicline = (filebot->data[0] != '\0');
2043
David Lawrence Ramsey3f358642004-11-22 16:04:18 +00002044 /* Get the number of characters in the marked text, and subtract
2045 * it from the saved value of totsize. Note that we don't need
2046 * to save totlines. */
2047 get_totals(top, bot, NULL, &part_totsize);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002048 old_totsize -= part_totsize;
2049 }
2050#endif
2051
2052 /* Reinitialize the filestruct. */
2053 free_filestruct(fileage);
2054 global_init(TRUE);
2055
2056 /* Reload the temp file. Do what load_buffer() would do, except for
2057 * making a new buffer for the temp file if multibuffer support is
2058 * available. */
2059 open_file(tempfile_name, FALSE, &f);
2060 read_file(f, tempfile_name);
2061 current = fileage;
2062
2063#ifndef NANO_SMALL
2064 if (old_mark_set) {
David Lawrence Ramsey15398372004-11-05 15:03:12 +00002065 filestruct *top_save = fileage;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002066
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +00002067 /* If the mark was on and we added a magicline, remove it
2068 * now. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002069 if (added_magicline)
2070 remove_magicline();
2071
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002072 /* Put the beginning and the end of the mark at the beginning
2073 * and the end of the spell-checked text. */
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002074 if (fileage == filebot)
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00002075 bot_x += top_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002076 if (right_side_up) {
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00002077 mark_beginx = top_x;
2078 current_x_save = bot_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002079 } else {
David Lawrence Ramseyf6159042004-11-17 20:47:14 +00002080 current_x_save = top_x;
2081 mark_beginx = bot_x;
David Lawrence Ramseybde3e0e2004-11-06 02:37:25 +00002082 }
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00002083
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +00002084 /* Unpartition the filestruct so that it contains all the text
2085 * again. Note that we've replaced the marked text originally
2086 * in the partition with the spell-checked marked text in the
2087 * temp file. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +00002088 unpartition_filestruct(&filepart);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002089
2090 /* Renumber starting with the beginning line of the old
David Lawrence Ramsey3f358642004-11-22 16:04:18 +00002091 * partition. Also set totlines to the new number of lines in
2092 * the file, add the number of characters in the spell-checked
2093 * marked text to the saved value of totsize, and then make that
2094 * saved value the actual value. */
David Lawrence Ramsey15398372004-11-05 15:03:12 +00002095 renumber(top_save);
David Lawrence Ramsey3f358642004-11-22 16:04:18 +00002096 totlines = filebot->lineno;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002097 old_totsize += totsize;
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002098 totsize = old_totsize;
2099
2100 /* Assign mark_beginbuf to the line where the mark began
2101 * before. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00002102 do_gotopos(mbb_lineno_save, mark_beginx, current_y_save, 0);
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002103 mark_beginbuf = current;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00002104
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00002105 /* Assign mark_beginx to the location in mark_beginbuf where the
2106 * mark began before, adjusted for any shortening of the
2107 * line. */
2108 mark_beginx = current_x;
2109
2110 /* Turn the mark back on. */
2111 SET(MARK_ISSET);
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002112 }
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00002113#endif
2114
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002115 /* Go back to the old position, mark the file as modified, and make
2116 * sure that the titlebar is refreshed. */
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00002117 do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002118 set_modified();
Chris Allegrettae1f14522001-09-19 03:19:43 +00002119 clearok(topwin, FALSE);
2120 titlebar(NULL);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00002121
Chris Allegretta334a9402002-12-16 04:25:53 +00002122 return NULL;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002123}
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002124
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002125void do_spell(void)
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002126{
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002127 int i;
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002128 char *temp = safe_tempnam(0, "nano.");
2129 const char *spell_msg;
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002130
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002131 if (temp == NULL) {
2132 statusbar(_("Could not create temp file: %s"), strerror(errno));
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002133 return;
Chris Allegretta271e9722000-11-10 18:15:43 +00002134 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002135
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002136#ifndef NANO_SMALL
2137 if (ISSET(MARK_ISSET))
David Lawrence Ramsey2f0d03b2004-05-28 00:15:28 +00002138 i = write_marked(temp, TRUE, FALSE);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002139 else
2140#endif
David Lawrence Ramsey2f0d03b2004-05-28 00:15:28 +00002141 i = write_file(temp, TRUE, FALSE, FALSE);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002142
2143 if (i == -1) {
David Lawrence Ramsey95e39e52004-08-12 02:52:14 +00002144 statusbar(_("Error writing temp file: %s"), strerror(errno));
Chris Allegrettabef12972002-03-06 03:30:40 +00002145 free(temp);
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002146 return;
Chris Allegretta3dbb2782000-12-02 04:36:50 +00002147 }
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002148
Chris Allegrettae1f14522001-09-19 03:19:43 +00002149#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002150 /* Update the current open_files entry before spell-checking, in
2151 * case any problems occur. */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002152 add_open_file(TRUE);
Chris Allegrettae1f14522001-09-19 03:19:43 +00002153#endif
2154
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002155 spell_msg = alt_speller != NULL ? do_alt_speller(temp) :
2156 do_int_speller(temp);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00002157 unlink(temp);
Chris Allegrettafdcb9e92003-02-12 02:52:04 +00002158 free(temp);
Chris Allegretta27eb13f2000-11-05 16:52:21 +00002159
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002160 if (spell_msg != NULL)
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002161 statusbar(_("Spell checking failed: %s: %s"), spell_msg,
2162 strerror(errno));
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002163 else
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002164 statusbar(_("Finished checking spelling"));
Chris Allegretta67105eb2000-07-03 03:18:32 +00002165}
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00002166#endif /* !DISABLE_SPELLER */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002167
David Lawrence Ramsey44e7f822004-03-30 04:17:10 +00002168#if !defined(NANO_SMALL) || !defined(DISABLE_JUSTIFY)
David Lawrence Ramsey8d911992004-05-29 17:05:52 +00002169/* The "indentation" of a line is the whitespace between the quote part
2170 * and the non-whitespace of the line. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002171size_t indent_length(const char *line)
2172{
Chris Allegretta6df90f52002-07-19 01:08:59 +00002173 size_t len = 0;
2174
2175 assert(line != NULL);
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00002176 while (isblank(*line)) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00002177 line++;
2178 len++;
2179 }
2180 return len;
2181}
David Lawrence Ramsey44e7f822004-03-30 04:17:10 +00002182#endif /* !NANO_SMALL || !DISABLE_JUSTIFY */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002183
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002184#ifndef DISABLE_JUSTIFY
David Lawrence Ramseyf613ca72004-05-13 22:23:58 +00002185/* justify_format() replaces Tab by Space and multiple spaces by 1
David Lawrence Ramseyfd73c462004-09-11 21:28:36 +00002186 * (except it maintains 2 after a non-repeated character in punct
2187 * followed by a character in brackets). Note that the terminating \0
2188 * counts as a space.
Chris Allegretta6df90f52002-07-19 01:08:59 +00002189 *
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002190 * justify_format() might make line->data shorter, and change the actual
2191 * pointer with null_at().
Chris Allegretta6df90f52002-07-19 01:08:59 +00002192 *
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00002193 * justify_format() will not look at the first skip characters of line.
David Lawrence Ramseyf613ca72004-05-13 22:23:58 +00002194 * skip should be at most strlen(line->data). The character at
2195 * line[skip + 1] must not be whitespace. */
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002196void justify_format(filestruct *line, size_t skip)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002197{
Chris Allegretta6df90f52002-07-19 01:08:59 +00002198 char *back, *front;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002199
Chris Allegretta6df90f52002-07-19 01:08:59 +00002200 /* These four asserts are assumptions about the input data. */
2201 assert(line != NULL);
2202 assert(line->data != NULL);
Chris Allegrettad8451932003-03-11 03:50:40 +00002203 assert(skip < strlen(line->data));
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00002204 assert(!isblank(line->data[skip]));
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002205
Chris Allegretta6df90f52002-07-19 01:08:59 +00002206 back = line->data + skip;
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002207 for (front = back; ; front++) {
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002208 bool remove_space = FALSE;
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002209 /* Do we want to remove this space? */
2210
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002211 if (*front == '\t')
Chris Allegretta6df90f52002-07-19 01:08:59 +00002212 *front = ' ';
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002213
David Lawrence Ramsey021960d2004-05-13 23:19:01 +00002214 /* These tests are safe since line->data + skip is not a
2215 * space. */
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002216 if ((*front == '\0' || *front == ' ') && *(front - 1) == ' ') {
David Lawrence Ramseyfd73c462004-09-11 21:28:36 +00002217 const char *bob = back - 2;
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002218
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002219 remove_space = TRUE;
David Lawrence Ramseyfd73c462004-09-11 21:28:36 +00002220 for (; bob >= line->data + skip; bob--) {
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002221 if (strchr(punct, *bob) != NULL) {
David Lawrence Ramseyfd73c462004-09-11 21:28:36 +00002222 /* If this character is in punct, don't remove the
2223 * space unless this character and the character
2224 * before it are the same. */
2225 remove_space = (bob > line->data + skip &&
2226 *bob == *(bob - 1));
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002227 break;
2228 }
2229 if (strchr(brackets, *bob) == NULL)
2230 break;
2231 }
2232 }
2233
2234 if (remove_space) {
Chris Allegretta6df90f52002-07-19 01:08:59 +00002235 /* Now *front is a space we want to remove. We do that by
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00002236 * simply failing to assign it to *back. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002237#ifndef NANO_SMALL
2238 if (mark_beginbuf == line && back - line->data < mark_beginx)
2239 mark_beginx--;
2240#endif
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002241 if (*front == '\0')
2242 *(back - 1) = '\0';
Chris Allegretta6df90f52002-07-19 01:08:59 +00002243 } else {
2244 *back = *front;
2245 back++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002246 }
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002247 if (*front == '\0')
2248 break;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002249 }
Chris Allegretta6df90f52002-07-19 01:08:59 +00002250
Chris Allegrettaa7a78de2003-02-19 22:27:53 +00002251 back--;
Chris Allegrettad8451932003-03-11 03:50:40 +00002252 assert(*back == '\0' && *front == '\0');
Chris Allegretta6df90f52002-07-19 01:08:59 +00002253
Chris Allegretta6df90f52002-07-19 01:08:59 +00002254 /* Now back is the new end of line->data. */
2255 if (back != front) {
Chris Allegrettad8451932003-03-11 03:50:40 +00002256 totsize -= front - back;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002257 null_at(&line->data, back - line->data);
2258#ifndef NANO_SMALL
2259 if (mark_beginbuf == line && back - line->data < mark_beginx)
2260 mark_beginx = back - line->data;
2261#endif
2262 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002263}
Chris Allegretta6df90f52002-07-19 01:08:59 +00002264
2265/* The "quote part" of a line is the largest initial substring matching
2266 * the quote string. This function returns the length of the quote part
2267 * of the given line.
2268 *
2269 * Note that if !HAVE_REGEX_H then we match concatenated copies of
2270 * quotestr. */
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002271size_t quote_length(const char *line)
2272{
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002273#ifdef HAVE_REGEX_H
2274 regmatch_t matches;
2275 int rc = regexec(&quotereg, line, 1, &matches, 0);
2276
2277 if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
2278 return 0;
2279 /* matches.rm_so should be 0, since the quote string should start
2280 * with the caret ^. */
2281 return matches.rm_eo;
2282#else /* !HAVE_REGEX_H */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002283 size_t qdepth = 0;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002284
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00002285 /* Compute quote depth level. */
David Lawrence Ramseyb8c479a2004-07-31 14:10:23 +00002286 while (strncmp(line + qdepth, quotestr, quotelen) == 0)
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002287 qdepth += quotelen;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002288 return qdepth;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002289#endif /* !HAVE_REGEX_H */
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002290}
Chris Allegretta6df90f52002-07-19 01:08:59 +00002291
Chris Allegretta6df90f52002-07-19 01:08:59 +00002292/* a_line and b_line are lines of text. The quotation part of a_line is
2293 * the first a_quote characters. Check that the quotation part of
2294 * b_line is the same. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002295bool quotes_match(const char *a_line, size_t a_quote, const char
2296 *b_line)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002297{
David Lawrence Ramsey6a2f0682004-12-20 01:13:55 +00002298 /* Here is the assumption about a_quote. */
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002299 assert(a_quote == quote_length(a_line));
David Lawrence Ramsey6a2f0682004-12-20 01:13:55 +00002300
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002301 return a_quote == quote_length(b_line) &&
David Lawrence Ramseyb8c479a2004-07-31 14:10:23 +00002302 strncmp(a_line, b_line, a_quote) == 0;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002303}
2304
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002305/* We assume a_line and b_line have no quote part. Then, we return
2306 * whether b_line could follow a_line in a paragraph. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002307bool indents_match(const char *a_line, size_t a_indent, const char
David Lawrence Ramseyd4693cb2004-05-14 01:17:25 +00002308 *b_line, size_t b_indent)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002309{
Chris Allegretta6df90f52002-07-19 01:08:59 +00002310 assert(a_indent == indent_length(a_line));
2311 assert(b_indent == indent_length(b_line));
2312
David Lawrence Ramseyb8c479a2004-07-31 14:10:23 +00002313 return b_indent <= a_indent &&
2314 strncmp(a_line, b_line, b_indent) == 0;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002315}
2316
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002317/* Is foo the beginning of a paragraph?
2318 *
2319 * A line of text consists of a "quote part", followed by an
2320 * "indentation part", followed by text. The functions quote_length()
2321 * and indent_length() calculate these parts.
2322 *
2323 * A line is "part of a paragraph" if it has a part not in the quote
2324 * part or the indentation.
2325 *
2326 * A line is "the beginning of a paragraph" if it is part of a
2327 * paragraph and
2328 * 1) it is the top line of the file, or
2329 * 2) the line above it is not part of a paragraph, or
2330 * 3) the line above it does not have precisely the same quote
2331 * part, or
2332 * 4) the indentation of this line is not an initial substring of
2333 * the indentation of the previous line, or
2334 * 5) this line has no quote part and some indentation, and
2335 * AUTOINDENT is not set.
2336 * The reason for number 5) is that if AUTOINDENT is not set, then an
2337 * indented line is expected to start a paragraph, like in books.
2338 * Thus, nano can justify an indented paragraph only if AUTOINDENT is
2339 * turned on. */
2340bool begpar(const filestruct *const foo)
2341{
2342 size_t quote_len;
2343 size_t indent_len;
2344 size_t temp_id_len;
2345
2346 /* Case 1). */
2347 if (foo->prev == NULL)
2348 return TRUE;
2349
2350 quote_len = quote_length(foo->data);
2351 indent_len = indent_length(foo->data + quote_len);
2352
2353 /* Not part of a paragraph. */
2354 if (foo->data[quote_len + indent_len] == '\0')
2355 return FALSE;
2356
2357 /* Case 3). */
2358 if (!quotes_match(foo->data, quote_len, foo->prev->data))
2359 return TRUE;
2360
2361 temp_id_len = indent_length(foo->prev->data + quote_len);
2362
2363 /* Case 2) or 5) or 4). */
2364 if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
2365 (quote_len == 0 && indent_len > 0
2366#ifndef NANO_SMALL
2367 && !ISSET(AUTOINDENT)
2368#endif
2369 ) || !indents_match(foo->prev->data + quote_len, temp_id_len,
2370 foo->data + quote_len, indent_len))
2371 return TRUE;
2372
2373 return FALSE;
2374}
2375
2376/* We find the last beginning-of-paragraph line before the current
2377 * line. */
2378void do_para_begin(void)
2379{
2380 const filestruct *old_current = current;
2381 const size_t old_pww = placewewant;
2382
2383 current_x = 0;
2384 placewewant = 0;
2385
2386 if (current->prev != NULL) {
2387 do {
2388 current = current->prev;
David Lawrence Ramsey50c7f2d2004-08-27 17:02:05 +00002389 current_y--;
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002390 } while (!begpar(current));
2391 }
2392
2393 edit_redraw(old_current, old_pww);
2394}
2395
2396bool inpar(const char *str)
2397{
2398 size_t quote_len = quote_length(str);
2399
2400 return str[quote_len + indent_length(str + quote_len)] != '\0';
2401}
2402
2403/* A line is the last line of a paragraph if it is in a paragraph, and
2404 * the next line isn't, or is the beginning of a paragraph. We move
2405 * down to the end of a paragraph, then one line farther. */
2406void do_para_end(void)
2407{
2408 const filestruct *const old_current = current;
2409 const size_t old_pww = placewewant;
2410
2411 current_x = 0;
2412 placewewant = 0;
2413
2414 while (current->next != NULL && !inpar(current->data))
2415 current = current->next;
2416
2417 while (current->next != NULL && inpar(current->next->data) &&
David Lawrence Ramsey50c7f2d2004-08-27 17:02:05 +00002418 !begpar(current->next)) {
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002419 current = current->next;
David Lawrence Ramsey50c7f2d2004-08-27 17:02:05 +00002420 current_y++;
2421 }
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002422
2423 if (current->next != NULL)
2424 current = current->next;
2425
2426 edit_redraw(old_current, old_pww);
2427}
2428
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002429/* Put the next par_len lines, starting with first_line, into the
2430 * justify buffer, leaving copies of those lines in place. Assume there
2431 * are enough lines after first_line. Return the new copy of
2432 * first_line. */
David Lawrence Ramseyd4693cb2004-05-14 01:17:25 +00002433filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t
2434 quote_len)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002435{
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002436 filestruct *top = first_line;
2437 /* The top of the paragraph we're backing up. */
2438 filestruct *bot = first_line;
2439 /* The bottom of the paragraph we're backing up. */
2440 size_t i;
2441 /* Generic loop variable. */
2442 size_t current_x_save = current_x;
2443 int fl_lineno_save = first_line->lineno;
2444 int edittop_lineno_save = edittop->lineno;
2445 int current_lineno_save = current->lineno;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002446#ifndef NANO_SMALL
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002447 bool old_mark_set = ISSET(MARK_ISSET);
2448 int mbb_lineno_save = 0;
2449
2450 if (old_mark_set)
2451 mbb_lineno_save = mark_beginbuf->lineno;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002452#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00002453
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002454 /* Move bot down par_len lines to the newline after the last line of
2455 * the paragraph. */
2456 for (i = par_len; i > 0; i--)
2457 bot = bot->next;
2458
2459 /* Move the paragraph from the main filestruct to the justify
2460 * buffer. */
2461 move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot, 0);
2462
2463 /* Copy the paragraph from the justify buffer to the main
2464 * filestruct. */
2465 copy_from_filestruct(jusbuffer, jusbottom);
2466
2467 /* Move upward from the last line of the paragraph to the first
2468 * line, putting first_line, edittop, current, and mark_beginbuf at
2469 * the same lines in the copied paragraph that they had in the
2470 * original paragraph. */
2471 top = current->prev;
2472 for (i = par_len; i > 0; i--) {
2473 if (top->lineno == fl_lineno_save)
2474 first_line = top;
2475 if (top->lineno == edittop_lineno_save)
2476 edittop = top;
2477 if (top->lineno == current_lineno_save)
2478 current = top;
2479#ifndef NANO_SMALL
2480 if (old_mark_set && top->lineno == mbb_lineno_save)
2481 mark_beginbuf = top;
2482#endif
2483 top = top->prev;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002484 }
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002485
2486 /* Put current_x at the same place in the copied paragraph that it
2487 * had in the original paragraph. */
2488 current_x = current_x_save;
2489
2490 set_modified();
2491
Chris Allegretta6df90f52002-07-19 01:08:59 +00002492 return first_line;
2493}
2494
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002495/* Is it possible to break line at or before goal? */
David Lawrence Ramseyd08348b2004-09-18 22:02:21 +00002496bool breakable(const char *line, ssize_t goal)
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002497{
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002498 while (*line != '\0' && goal >= 0) {
2499 size_t pos = 0;
2500
David Lawrence Ramsey9830d752004-05-13 17:19:54 +00002501 if (isblank(*line))
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002502 return TRUE;
2503
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002504 line += parse_char(line, NULL, &pos
2505#ifdef NANO_WIDE
2506 , NULL
2507#endif
2508 );
2509
2510 goal -= pos;
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002511 }
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002512
Chris Allegretta428f6202003-02-12 03:21:45 +00002513 /* If goal is not negative, the whole line (one word) was short
2514 * enough. */
2515 return goal >= 0;
Chris Allegrettacff6e6f2003-02-03 03:22:02 +00002516}
2517
Chris Allegretta6df90f52002-07-19 01:08:59 +00002518/* We are trying to break a chunk off line. We find the last space such
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002519 * that the display length to there is at most goal + 1. If there is no
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002520 * such space, and force is TRUE, then we find the first space. Anyway,
2521 * we then take the last space in that group of spaces. The terminating
2522 * '\0' counts as a space. */
David Lawrence Ramseyfc54d6e2004-12-02 17:37:09 +00002523ssize_t break_line(const char *line, ssize_t goal, bool force)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002524{
David Lawrence Ramseyd08348b2004-09-18 22:02:21 +00002525 ssize_t space_loc = -1;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002526 /* Current tentative return value. Index of the last space we
2527 * found with short enough display width. */
David Lawrence Ramseyd08348b2004-09-18 22:02:21 +00002528 ssize_t cur_loc = 0;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002529 /* Current index in line. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002530
2531 assert(line != NULL);
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002532
2533 while (*line != '\0' && goal >= 0) {
2534 size_t pos = 0;
2535 int line_len;
2536
Chris Allegretta6df90f52002-07-19 01:08:59 +00002537 if (*line == ' ')
2538 space_loc = cur_loc;
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002539
Chris Allegretta6df90f52002-07-19 01:08:59 +00002540 assert(*line != '\t');
2541
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002542 line_len = parse_char(line, NULL, &pos
2543#ifdef NANO_WIDE
2544 , NULL
2545#endif
2546 );
2547
2548 goal -= pos;
2549 line += line_len;
2550 cur_loc += line_len;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002551 }
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002552
Chris Allegretta6df90f52002-07-19 01:08:59 +00002553 if (goal >= 0)
2554 /* In fact, the whole line displays shorter than goal. */
2555 return cur_loc;
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002556
Chris Allegretta6df90f52002-07-19 01:08:59 +00002557 if (space_loc == -1) {
2558 /* No space found short enough. */
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002559 if (force) {
2560 for (; *line != '\0'; line++, cur_loc++) {
2561 if (*line == ' ' && *(line + 1) != ' ' &&
2562 *(line + 1) != '\0')
Chris Allegretta6df90f52002-07-19 01:08:59 +00002563 return cur_loc;
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002564 }
2565 return -1;
2566 }
Chris Allegretta6df90f52002-07-19 01:08:59 +00002567 }
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002568
Chris Allegretta6df90f52002-07-19 01:08:59 +00002569 /* Perhaps the character after space_loc is a space. But because
David Lawrence Ramsey0084eaa2002-11-04 16:05:42 +00002570 * of justify_format(), there can be only two adjacent. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002571 if (*(line - cur_loc + space_loc + 1) == ' ' ||
2572 *(line - cur_loc + space_loc + 1) == '\0')
2573 space_loc++;
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00002574
Chris Allegretta6df90f52002-07-19 01:08:59 +00002575 return space_loc;
2576}
Chris Allegretta6df90f52002-07-19 01:08:59 +00002577
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002578/* Find the beginning of the current paragraph if we're in one, or the
2579 * beginning of the next paragraph if we're not. Afterwards, save the
2580 * quote length and paragraph length in *quote and *par. Return FALSE
2581 * if we found a paragraph, or TRUE if there was an error or we didn't
2582 * find a paragraph.
David Lawrence Ramsey8faf3052003-09-04 20:25:29 +00002583 *
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002584 * See the comment at begpar() for more about when a line is the
2585 * beginning of a paragraph. */
2586bool do_para_search(size_t *const quote, size_t *const par)
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002587{
2588 size_t quote_len;
2589 /* Length of the initial quotation of the paragraph we
2590 * search. */
2591 size_t par_len;
2592 /* Number of lines in that paragraph. */
David Lawrence Ramsey2ed225f2004-05-28 20:44:09 +00002593 size_t indent_len;
2594 /* Generic indentation length. */
2595 filestruct *line;
2596 /* Generic line of text. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002597
2598#ifdef HAVE_REGEX_H
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002599 if (quoterc != 0) {
2600 statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
2601 return TRUE;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002602 }
2603#endif
2604
2605 /* Here is an assumption that is always true anyway. */
2606 assert(current != NULL);
2607
2608 current_x = 0;
2609
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002610 quote_len = quote_length(current->data);
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002611 indent_len = indent_length(current->data + quote_len);
2612
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002613 /* Here we find the first line of the paragraph to search. If the
2614 * current line is in a paragraph, then we move back to the first
2615 * line of the paragraph. Otherwise, we move to the first line that
2616 * is in a paragraph. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002617 if (current->data[quote_len + indent_len] != '\0') {
2618 /* This line is part of a paragraph. So we must search back to
2619 * the first line of this paragraph. First we check items 1)
2620 * and 3) above. */
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002621 while (current->prev != NULL && quotes_match(current->data,
2622 quote_len, current->prev->data)) {
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002623 size_t temp_id_len =
David Lawrence Ramseybb50b302004-08-12 21:43:00 +00002624 indent_length(current->prev->data + quote_len);
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002625 /* The indentation length of the previous line. */
2626
2627 /* Is this line the beginning of a paragraph, according to
2628 * items 2), 5), or 4) above? If so, stop. */
2629 if (current->prev->data[quote_len + temp_id_len] == '\0' ||
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002630 (quote_len == 0 && indent_len > 0
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002631#ifndef NANO_SMALL
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002632 && !ISSET(AUTOINDENT)
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002633#endif
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002634 ) || !indents_match(current->prev->data + quote_len,
2635 temp_id_len, current->data + quote_len, indent_len))
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002636 break;
2637 indent_len = temp_id_len;
2638 current = current->prev;
2639 current_y--;
2640 }
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002641 } else {
2642 /* This line is not part of a paragraph. Move down until we get
2643 * to a non "blank" line. */
2644 do {
2645 /* There is no next paragraph, so nothing to move to. */
2646 if (current->next == NULL) {
2647 placewewant = 0;
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002648 return TRUE;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002649 }
2650 current = current->next;
2651 current_y++;
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002652 quote_len = quote_length(current->data);
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002653 indent_len = indent_length(current->data + quote_len);
2654 } while (current->data[quote_len + indent_len] == '\0');
2655 }
2656
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002657 /* Now current is the first line of the paragraph, and quote_len is
2658 * the quotation length of that line. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002659
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002660 /* Next step, compute par_len, the number of lines in this
2661 * paragraph. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002662 line = current;
2663 par_len = 1;
2664 indent_len = indent_length(line->data + quote_len);
2665
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002666 while (line->next != NULL &&
2667 quotes_match(current->data, quote_len, line->next->data)) {
2668 size_t temp_id_len = indent_length(line->next->data + quote_len);
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002669
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002670 if (!indents_match(line->data + quote_len, indent_len,
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002671 line->next->data + quote_len, temp_id_len) ||
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002672 line->next->data[quote_len + temp_id_len] == '\0' ||
2673 (quote_len == 0 && temp_id_len > 0
2674#ifndef NANO_SMALL
David Lawrence Ramseydbde9d72004-06-27 00:54:08 +00002675 && !ISSET(AUTOINDENT)
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002676#endif
2677 ))
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002678 break;
2679 indent_len = temp_id_len;
2680 line = line->next;
2681 par_len++;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002682 }
2683
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002684 /* Now par_len is the number of lines in this paragraph. We should
2685 * never call quotes_match() or quote_length() again. */
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002686
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002687 /* Save the values of quote_len and par_len. */
2688 assert(quote != NULL && par != NULL);
2689 *quote = quote_len;
2690 *par = par_len;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002691
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002692 return FALSE;
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002693}
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002694
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002695/* If full_justify is TRUE, justify the entire file. Otherwise, justify
2696 * the current paragraph. */
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00002697void do_justify(bool full_justify)
David Lawrence Ramsey281e0562004-02-27 18:54:04 +00002698{
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002699 filestruct *first_par_line = NULL;
2700 /* Will be the first line of the resulting justified paragraph.
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002701 * For restoring after unjustify. */
David Lawrence Ramsey36e363f2004-05-17 20:38:00 +00002702 filestruct *last_par_line;
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002703 /* Will be the line containing the newline after the last line
2704 * of the result. Also for restoring after unjustify. */
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002705 bool allow_respacing;
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002706 /* Whether we should change the spacing at the end of a line
David Lawrence Ramseyf7b5d932004-07-05 14:27:29 +00002707 * after justifying it. This should be TRUE whenever we move
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002708 * to the next line after justifying the current line. */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002709
2710 /* We save these global variables to be restored if the user
David Lawrence Ramsey59f5e042004-10-29 15:48:40 +00002711 * unjustifies. Note that we don't need to save totlines. */
David Lawrence Ramsey7a97e182004-10-30 01:03:15 +00002712 size_t current_x_save = current_x;
2713 int current_y_save = current_y;
David Lawrence Ramsey59f5e042004-10-29 15:48:40 +00002714 long flags_save = flags, totsize_save = totsize;
2715 filestruct *edittop_save = edittop, *current_save = current;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002716#ifndef NANO_SMALL
2717 filestruct *mark_beginbuf_save = mark_beginbuf;
David Lawrence Ramsey687776b2004-10-30 01:16:08 +00002718 size_t mark_beginx_save = mark_beginx;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002719#endif
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00002720 int kbinput;
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00002721 bool meta_key, func_key, s_or_t, finished;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002722
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002723 /* If we're justifying the entire file, start at the beginning. */
2724 if (full_justify)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002725 current = fileage;
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002726
2727 last_par_line = current;
David Lawrence Ramsey8faf3052003-09-04 20:25:29 +00002728
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002729 while (TRUE) {
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002730 size_t quote_len;
2731 /* Length of the initial quotation of the paragraph we
2732 * justify. */
2733 size_t par_len;
2734 /* Number of lines in that paragraph. */
2735
2736 /* Find the first line of the paragraph to be justified. That
2737 * is the start of this paragraph if we're in one, or the start
2738 * of the next otherwise. Save the quote length and paragraph
David Lawrence Ramsey68ebb612004-12-04 17:36:14 +00002739 * length (number of lines). Don't refresh the screen yet,
2740 * since we'll do that after we justify. If the search
2741 * failed, we're justifying the whole file, and the search
2742 * didn't leave us on the last line of the file, set the last
2743 * line of the text to be justified to the last line of the file
2744 * and break out of the loop. Otherwise, refresh the screen and
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002745 * get out. */
2746 if (do_para_search(&quote_len, &par_len)) {
David Lawrence Ramsey3e819142004-12-31 04:10:28 +00002747 if (full_justify && first_par_line != filebot) {
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002748 last_par_line = filebot;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002749 break;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002750 } else {
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002751 edit_refresh();
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00002752 return;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002753 }
2754 }
2755
2756 /* Next step, we loop through the lines of this paragraph,
2757 * justifying each one individually. */
2758 for (; par_len > 0; current_y++, par_len--) {
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002759 size_t indent_len; /* Generic indentation length. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002760 size_t line_len;
2761 size_t display_len;
2762 /* The width of current in screen columns. */
David Lawrence Ramseyd08348b2004-09-18 22:02:21 +00002763 ssize_t break_pos;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002764 /* Where we will break the line. */
2765
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002766 /* We'll be moving to the next line after justifying the
2767 * current line in almost all cases, so allow changing the
2768 * spacing at the ends of justified lines by default. */
2769 allow_respacing = TRUE;
2770
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002771 indent_len = quote_len + indent_length(current->data +
2772 quote_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002773
David Lawrence Ramsey91bc83a2004-06-25 01:52:10 +00002774 /* If we haven't already done it, copy the original
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002775 * paragraph to the justify buffer. */
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002776 if (first_par_line == NULL)
2777 first_par_line = backup_lines(current, full_justify ?
David Lawrence Ramsey6b769e52004-11-17 16:59:34 +00002778 filebot->lineno - current->lineno : par_len,
2779 quote_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002780
David Lawrence Ramsey91bc83a2004-06-25 01:52:10 +00002781 /* Now we call justify_format() on the current line of the
2782 * paragraph, which will remove excess spaces from it and
2783 * change tabs to spaces. */
2784 justify_format(current, quote_len +
2785 indent_length(current->data + quote_len));
2786
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002787 line_len = strlen(current->data);
2788 display_len = strlenpt(current->data);
2789
2790 if (display_len > fill) {
2791 /* The line is too long. Try to wrap it to the next. */
2792 break_pos = break_line(current->data + indent_len,
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002793 fill - strnlenpt(current->data, indent_len),
2794 TRUE);
David Lawrence Ramseyabc94232004-12-08 23:24:31 +00002795 if (break_pos == -1 ||
2796 break_pos + indent_len == line_len)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002797 /* We can't break the line, or don't need to, so
2798 * just go on to the next. */
2799 goto continue_loc;
2800 break_pos += indent_len;
2801 assert(break_pos < line_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002802 if (par_len == 1) {
2803 /* There is no next line in this paragraph. We make
2804 * a new line and copy text after break_pos into
2805 * it. */
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002806 splice_node(current, make_new_node(current),
2807 current->next);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002808 /* In a non-quoted paragraph, we copy the indent
2809 * only if AUTOINDENT is turned on. */
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002810 if (quote_len == 0
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002811#ifndef NANO_SMALL
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002812 && !ISSET(AUTOINDENT)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002813#endif
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002814 )
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002815 indent_len = 0;
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002816 current->next->data = charalloc(indent_len +
2817 line_len - break_pos);
2818 strncpy(current->next->data, current->data,
2819 indent_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002820 strcpy(current->next->data + indent_len,
2821 current->data + break_pos + 1);
2822 assert(strlen(current->next->data) ==
2823 indent_len + line_len - break_pos - 1);
2824 totlines++;
2825 totsize += indent_len;
2826 par_len++;
2827 } else {
2828 size_t next_line_len = strlen(current->next->data);
2829
2830 indent_len = quote_len +
2831 indent_length(current->next->data + quote_len);
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002832 current->next->data =
2833 charealloc(current->next->data, next_line_len +
2834 line_len - break_pos + 1);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002835
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002836 charmove(current->next->data + indent_len +
2837 line_len - break_pos, current->next->data +
2838 indent_len, next_line_len - indent_len + 1);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002839 strcpy(current->next->data + indent_len,
2840 current->data + break_pos + 1);
David Lawrence Ramsey537a8802004-06-24 22:39:24 +00002841 current->next->data[indent_len + line_len -
2842 break_pos - 1] = ' ';
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002843#ifndef NANO_SMALL
2844 if (mark_beginbuf == current->next) {
2845 if (mark_beginx < indent_len)
2846 mark_beginx = indent_len;
2847 mark_beginx += line_len - break_pos;
2848 }
2849#endif
2850 }
2851#ifndef NANO_SMALL
David Lawrence Ramsey40a6c8c2004-11-27 21:10:11 +00002852 if (mark_beginbuf == current &&
2853 mark_beginx > break_pos) {
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002854 mark_beginbuf = current->next;
2855 mark_beginx -= break_pos + 1 - indent_len;
2856 }
2857#endif
2858 null_at(&current->data, break_pos);
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002859
2860 /* Go to the next line. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002861 current = current->next;
2862 } else if (display_len < fill && par_len > 1) {
2863 size_t next_line_len;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002864
2865 indent_len = quote_len +
2866 indent_length(current->next->data + quote_len);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002867 /* If we can't pull a word from the next line up to this
2868 * one, just go on. */
2869 if (!breakable(current->next->data + indent_len,
2870 fill - display_len - 1))
2871 goto continue_loc;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002872
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002873 break_pos = break_line(current->next->data + indent_len,
2874 fill - display_len - 1, FALSE);
2875 assert(break_pos != -1);
2876
2877 current->data = charealloc(current->data,
2878 line_len + break_pos + 2);
2879 current->data[line_len] = ' ';
2880 strncpy(current->data + line_len + 1,
2881 current->next->data + indent_len, break_pos);
2882 current->data[line_len + break_pos + 1] = '\0';
Chris Allegretta6df90f52002-07-19 01:08:59 +00002883#ifndef NANO_SMALL
2884 if (mark_beginbuf == current->next) {
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002885 if (mark_beginx < indent_len + break_pos) {
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00002886 mark_beginbuf = current;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002887 if (mark_beginx <= indent_len)
2888 mark_beginx = line_len + 1;
2889 else
David Lawrence Ramsey2e3aeae2004-05-24 05:52:35 +00002890 mark_beginx = line_len + 1 + mark_beginx -
2891 indent_len;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002892 } else
2893 mark_beginx -= break_pos + 1;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002894 }
2895#endif
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002896 next_line_len = strlen(current->next->data);
2897 if (indent_len + break_pos == next_line_len) {
2898 filestruct *line = current->next;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002899
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002900 /* Don't destroy edittop! */
2901 if (line == edittop)
2902 edittop = current;
Chris Allegretta6df90f52002-07-19 01:08:59 +00002903
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002904 unlink_node(line);
2905 delete_node(line);
2906 totlines--;
2907 totsize -= indent_len;
2908 current_y--;
David Lawrence Ramsey01a6bf42004-07-03 05:23:19 +00002909
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002910 /* Don't go to the next line. Accordingly, don't
2911 * allow changing the spacing at the end of the
David Lawrence Ramsey309fbcb2004-07-03 14:34:03 +00002912 * previous justified line, so that we don't end up
2913 * doing it more than once on the same line. */
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002914 allow_respacing = FALSE;
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002915 } else {
2916 charmove(current->next->data + indent_len,
Chris Allegretta6df90f52002-07-19 01:08:59 +00002917 current->next->data + indent_len + break_pos + 1,
2918 next_line_len - break_pos - indent_len);
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002919 null_at(&current->next->data, next_line_len -
2920 break_pos);
David Lawrence Ramsey01a6bf42004-07-03 05:23:19 +00002921
2922 /* Go to the next line. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002923 current = current->next;
2924 }
2925 } else
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00002926 continue_loc:
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002927 /* Go to the next line. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002928 current = current->next;
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002929
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00002930 /* We've moved to the next line after justifying the
David Lawrence Ramsey46938642004-07-03 14:15:58 +00002931 * current line. If the justified line was not the last
2932 * line of the paragraph, add a space to the end of it to
2933 * replace the one removed or left out by justify_format().
2934 * If it was the last line of the paragraph, and
2935 * justify_format() left a space on the end of it, remove
2936 * the space. */
2937 if (allow_respacing) {
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002938 size_t prev_line_len = strlen(current->prev->data);
David Lawrence Ramseya9a6ce02004-06-05 22:09:56 +00002939
2940 if (par_len > 1) {
2941 current->prev->data = charealloc(current->prev->data,
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002942 prev_line_len + 2);
David Lawrence Ramseya9a6ce02004-06-05 22:09:56 +00002943 current->prev->data[prev_line_len] = ' ';
2944 current->prev->data[prev_line_len + 1] = '\0';
2945 totsize++;
2946 } else if (par_len == 1 &&
2947 current->prev->data[prev_line_len - 1] == ' ') {
David Lawrence Ramsey6e738ac2004-11-23 22:30:32 +00002948 current->prev->data =
2949 charealloc(current->prev->data, prev_line_len);
David Lawrence Ramseya9a6ce02004-06-05 22:09:56 +00002950 current->prev->data[prev_line_len - 1] = '\0';
2951 totsize--;
2952 }
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002953 }
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002954 }
Chris Allegrettae1e0fd62003-04-15 01:15:09 +00002955
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002956 /* We've just justified a paragraph. If we're not justifying the
2957 * entire file, break out of the loop. Otherwise, continue the
2958 * loop so that we justify all the paragraphs in the file. */
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002959 if (!full_justify)
2960 break;
2961
2962 } /* while (TRUE) */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002963
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002964 /* We are now done justifying the paragraph or the file, so clean
2965 * up. totlines, totsize, and current_y have been maintained above.
2966 * Set last_par_line to the new end of the paragraph, update
David Lawrence Ramsey8037fe02004-07-23 12:30:40 +00002967 * fileage, and renumber() since edit_refresh() needs the line
2968 * numbers to be right (but only do the last two if we actually
2969 * justified something). */
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002970 last_par_line = current;
David Lawrence Ramsey8037fe02004-07-23 12:30:40 +00002971 if (first_par_line != NULL) {
2972 if (first_par_line->prev == NULL)
2973 fileage = first_par_line;
2974 renumber(first_par_line);
2975 }
David Lawrence Ramsey1cadddd2004-05-17 20:32:51 +00002976
David Lawrence Ramsey7097d7b2004-05-17 16:11:18 +00002977 edit_refresh();
Chris Allegretta6df90f52002-07-19 01:08:59 +00002978
Chris Allegretta9149e612000-11-27 00:23:41 +00002979 statusbar(_("Can now UnJustify!"));
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00002980
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00002981 /* Display the shortcut list with UnJustify. */
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00002982 shortcut_init(TRUE);
Chris Allegretta07798352000-11-27 22:58:23 +00002983 display_main_list();
Chris Allegretta9149e612000-11-27 00:23:41 +00002984
David Lawrence Ramsey63e73cb2004-11-28 04:52:57 +00002985 /* Now get a keystroke and see if it's unjustify. If not, put back
2986 * the keystroke and return. */
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00002987 kbinput = do_input(&meta_key, &func_key, &s_or_t, &finished, FALSE);
Chris Allegretta5f071802001-05-06 02:34:31 +00002988
David Lawrence Ramsey74835712004-12-04 17:41:52 +00002989 if (!meta_key && !func_key && s_or_t &&
2990 kbinput == NANO_UNJUSTIFY_KEY) {
David Lawrence Ramseya0b5ba22004-08-25 15:39:10 +00002991 /* Restore the justify we just did (ungrateful user!). */
Chris Allegretta6df90f52002-07-19 01:08:59 +00002992 current = current_save;
2993 current_x = current_x_save;
2994 current_y = current_y_save;
2995 edittop = edittop_save;
Chris Allegrettad022eac2000-11-27 02:50:49 +00002996
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00002997 /* Splice the justify buffer back into the file, but only if we
David Lawrence Ramsey8037fe02004-07-23 12:30:40 +00002998 * actually justified something. */
2999 if (first_par_line != NULL) {
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003000 filestruct *bot_save;
Chris Allegretta6df90f52002-07-19 01:08:59 +00003001
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003002 /* Partition the filestruct so that it contains only the
3003 * text of the justified paragraph. */
3004 filepart = partition_filestruct(first_par_line, 0,
3005 last_par_line, 0);
Chris Allegretta6df90f52002-07-19 01:08:59 +00003006
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003007 /* Remove the text of the justified paragraph, and
3008 * put the text in the justify buffer in its place. */
3009 free_filestruct(fileage);
3010 fileage = jusbuffer;
3011 filebot = jusbottom;
3012
3013 bot_save = filebot;
3014
3015 /* Unpartition the filestruct so that it contains all the
3016 * text again. Note that the justified paragraph has been
3017 * replaced with the unjustified paragraph. */
3018 unpartition_filestruct(&filepart);
3019
3020 /* Renumber starting with the ending line of the old
3021 * partition. */
3022 if (bot_save->next != NULL)
3023 renumber(bot_save->next);
3024
3025 /* Restore global variables from before the justify. */
3026 totsize = totsize_save;
3027 totlines = filebot->lineno;
Chris Allegretta6df90f52002-07-19 01:08:59 +00003028#ifndef NANO_SMALL
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003029 mark_beginbuf = mark_beginbuf_save;
3030 mark_beginx = mark_beginx_save;
Chris Allegretta6df90f52002-07-19 01:08:59 +00003031#endif
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003032 flags = flags_save;
3033
3034 /* Clear the justify buffer. */
3035 jusbuffer = NULL;
3036
3037 if (!ISSET(MODIFIED))
3038 titlebar(NULL);
3039 edit_refresh();
3040 }
David Lawrence Ramseya0b5ba22004-08-25 15:39:10 +00003041 } else {
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003042 unget_kbinput(kbinput, meta_key, func_key);
David Lawrence Ramseyd994ad52004-11-23 21:40:26 +00003043
David Lawrence Ramsey53d3db42004-11-28 03:53:01 +00003044 /* Blow away the text in the justify buffer. */
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003045 free_filestruct(jusbuffer);
3046 jusbuffer = NULL;
Chris Allegretta9149e612000-11-27 00:23:41 +00003047 }
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00003048
David Lawrence Ramseyfa394042004-05-23 21:11:14 +00003049 blank_statusbar();
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003050
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003051 /* Display the shortcut list with UnCut. */
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00003052 shortcut_init(FALSE);
Chris Allegretta4a9c8582000-11-27 22:59:40 +00003053 display_main_list();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003054}
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003055
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003056void do_justify_void(void)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003057{
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003058 do_justify(FALSE);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003059}
3060
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003061void do_full_justify(void)
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003062{
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003063 do_justify(TRUE);
David Lawrence Ramsey8d3e7f32004-05-13 17:28:03 +00003064}
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00003065#endif /* !DISABLE_JUSTIFY */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003066
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003067void do_exit(void)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003068{
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003069 int i;
Chris Allegretta13fd44b2002-01-02 13:59:11 +00003070
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003071 if (!ISSET(MODIFIED))
3072 i = 0; /* Pretend the user chose not to save. */
David Lawrence Ramseyedab0cc2004-07-03 03:09:12 +00003073 else if (ISSET(TEMP_FILE))
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003074 i = 1;
David Lawrence Ramseyf4276942003-12-24 03:33:09 +00003075 else
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003076 i = do_yesno(FALSE,
3077 _("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES) ? "));
3078
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003079#ifdef DEBUG
3080 dump_buffer(fileage);
3081#endif
3082
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003083 if (i == 0 || (i == 1 && do_writeout(TRUE) > 0)) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003084#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +00003085 /* Exit only if there are no more open file buffers. */
David Lawrence Ramsey3e189a82004-10-03 19:18:48 +00003086 if (!close_open_file())
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003087#endif
David Lawrence Ramseyda141062004-05-25 19:41:11 +00003088 finish();
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00003089 } else if (i != 1)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003090 statusbar(_("Cancelled"));
3091
3092 display_main_list();
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003093}
3094
3095void signal_init(void)
3096{
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003097 /* Trap SIGINT and SIGQUIT because we want them to do useful
3098 * things. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003099 memset(&act, 0, sizeof(struct sigaction));
3100 act.sa_handler = SIG_IGN;
3101 sigaction(SIGINT, &act, NULL);
David Lawrence Ramsey2897d2b2004-01-26 20:56:20 +00003102 sigaction(SIGQUIT, &act, NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003103
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003104 /* Trap SIGHUP and SIGTERM because we want to write the file out. */
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003105 act.sa_handler = handle_hupterm;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003106 sigaction(SIGHUP, &act, NULL);
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003107 sigaction(SIGTERM, &act, NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003108
3109#ifndef NANO_SMALL
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003110 /* Trap SIGWINCH because we want to handle window resizes. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003111 act.sa_handler = handle_sigwinch;
3112 sigaction(SIGWINCH, &act, NULL);
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00003113 allow_pending_sigwinch(FALSE);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003114#endif
3115
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003116 /* Trap normal suspend (^Z) so we can handle it ourselves. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003117 if (!ISSET(SUSPEND)) {
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003118 act.sa_handler = SIG_IGN;
3119 sigaction(SIGTSTP, &act, NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003120 } else {
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003121 /* Block all other signals in the suspend and continue handlers.
3122 * If we don't do this, other stuff interrupts them! */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003123 sigfillset(&act.sa_mask);
3124
3125 act.sa_handler = do_suspend;
3126 sigaction(SIGTSTP, &act, NULL);
3127
3128 act.sa_handler = do_cont;
3129 sigaction(SIGCONT, &act, NULL);
3130 }
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003131}
3132
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003133/* Handler for SIGHUP (hangup) and SIGTERM (terminate). */
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003134RETSIGTYPE handle_hupterm(int signal)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003135{
Chris Allegrettaa0d89972003-02-03 03:32:08 +00003136 die(_("Received SIGHUP or SIGTERM\n"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003137}
3138
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003139/* Handler for SIGTSTP (suspend). */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003140RETSIGTYPE do_suspend(int signal)
3141{
3142 endwin();
Chris Allegretta76417082003-02-12 23:54:34 +00003143 printf("\n\n\n\n\n%s\n", _("Use \"fg\" to return to nano"));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003144 fflush(stdout);
3145
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003146 /* Restore the old terminal settings. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003147 tcsetattr(0, TCSANOW, &oldterm);
3148
David Lawrence Ramsey99bede32003-09-29 07:21:11 +00003149 /* Trap SIGHUP and SIGTERM so we can properly deal with them while
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003150 * suspended. */
David Lawrence Ramsey99bede32003-09-29 07:21:11 +00003151 act.sa_handler = handle_hupterm;
3152 sigaction(SIGHUP, &act, NULL);
3153 sigaction(SIGTERM, &act, NULL);
3154
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003155 /* Do what mutt does: send ourselves a SIGSTOP. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003156 kill(0, SIGSTOP);
3157}
3158
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003159/* Handler for SIGCONT (continue after suspend). */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003160RETSIGTYPE do_cont(int signal)
3161{
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003162#ifndef NANO_SMALL
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003163 /* Perhaps the user resized the window while we slept. Handle it
3164 * and update the screen in the process. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003165 handle_sigwinch(0);
David Lawrence Ramsey5520e852004-04-07 00:44:35 +00003166#else
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003167 /* Just update the screen. */
3168 doupdate();
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003169#endif
3170}
3171
3172#ifndef NANO_SMALL
3173void handle_sigwinch(int s)
3174{
3175 const char *tty = ttyname(0);
3176 int fd;
3177 int result = 0;
3178 struct winsize win;
3179
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003180 if (tty == NULL)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003181 return;
3182 fd = open(tty, O_RDWR);
3183 if (fd == -1)
3184 return;
3185 result = ioctl(fd, TIOCGWINSZ, &win);
3186 close(fd);
3187 if (result == -1)
3188 return;
3189
3190 /* Could check whether the COLS or LINES changed, and return
3191 * otherwise. EXCEPT, that COLS and LINES are ncurses global
3192 * variables, and in some cases ncurses has already updated them.
3193 * But not in all cases, argh. */
3194 COLS = win.ws_col;
3195 LINES = win.ws_row;
Chris Allegrettaf8f2d582003-02-10 02:43:48 +00003196 editwinrows = LINES - 5 + no_help();
3197 if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003198 die_too_small();
3199
3200#ifndef DISABLE_WRAPJUSTIFY
3201 fill = wrap_at;
3202 if (fill <= 0)
3203 fill += COLS;
Chris Allegrettaf8f2d582003-02-10 02:43:48 +00003204 if (fill < 0)
3205 fill = 0;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003206#endif
3207
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00003208 hblank = charealloc(hblank, COLS + 1);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003209 memset(hblank, ' ', COLS);
3210 hblank[COLS] = '\0';
3211
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00003212 /* If we've partitioned the filestruct, unpartition it now. */
3213 if (filepart != NULL)
David Lawrence Ramsey74d87072004-11-22 00:16:23 +00003214 unpartition_filestruct(&filepart);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00003215
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003216#ifndef DISABLE_JUSTIFY
David Lawrence Ramseybc3b9262004-11-23 04:17:19 +00003217 /* If the justify buffer isn't empty, blow away the text in it and
3218 * display the shortcut list with UnCut. */
David Lawrence Ramsey93c84052004-11-23 04:08:28 +00003219 if (jusbuffer != NULL) {
3220 free_filestruct(jusbuffer);
3221 jusbuffer = NULL;
3222 shortcut_init(FALSE);
3223 }
3224#endif
3225
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003226#ifdef USE_SLANG
3227 /* Slang curses emulation brain damage, part 1: If we just do what
3228 * curses does here, it'll only work properly if the resize made the
3229 * window smaller. Do what mutt does: Leave and immediately reenter
3230 * Slang screen management mode. */
3231 SLsmg_reset_smg();
3232 SLsmg_init_smg();
3233#else
3234 /* Do the equivalent of what Minimum Profit does: Leave and
3235 * immediately reenter curses mode. */
3236 endwin();
3237 refresh();
3238#endif
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003239
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00003240 /* Restore the terminal to its previous state. */
3241 terminal_init();
3242
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003243 /* Do the equivalent of what both mutt and Minimum Profit do:
3244 * Reinitialize all the windows based on the new screen
3245 * dimensions. */
3246 window_init();
3247
David Lawrence Ramsey907725f2004-11-12 00:09:20 +00003248 /* Redraw the contents of the windows that need it. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003249 blank_statusbar();
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003250 display_main_list();
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003251 total_refresh();
3252
David Lawrence Ramsey273d2ce2004-01-30 04:20:28 +00003253 /* Turn cursor back on for sure. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003254 curs_set(1);
3255
David Lawrence Ramsey48ae9862004-05-28 17:23:33 +00003256 /* Reset all the input routines that rely on character sequences. */
3257 reset_kbinput();
3258
David Lawrence Ramsey273d2ce2004-01-30 04:20:28 +00003259 /* Jump back to the main loop. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00003260 siglongjmp(jmpbuf, 1);
3261}
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00003262
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00003263void allow_pending_sigwinch(bool allow)
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00003264{
3265 sigset_t winch;
3266 sigemptyset(&winch);
3267 sigaddset(&winch, SIGWINCH);
3268 if (allow)
3269 sigprocmask(SIG_UNBLOCK, &winch, NULL);
3270 else
3271 sigprocmask(SIG_BLOCK, &winch, NULL);
3272}
Chris Allegrettadab017e2002-04-23 10:56:06 +00003273#endif /* !NANO_SMALL */
David Lawrence Ramseyc5967552002-06-21 03:20:06 +00003274
Chris Allegrettadab017e2002-04-23 10:56:06 +00003275#ifndef NANO_SMALL
David Lawrence Ramsey0341b582002-08-21 16:10:37 +00003276void do_toggle(const toggle *which)
Chris Allegretta756f2202000-09-01 13:32:47 +00003277{
David Lawrence Ramseyce62e822004-08-05 22:10:22 +00003278 bool enabled;
Chris Allegrettaf0f63a82000-09-02 18:44:21 +00003279
Chris Allegrettaa8c22572002-02-15 19:17:02 +00003280 TOGGLE(which->flag);
Chris Allegretta2a42af12000-09-12 23:02:49 +00003281
Chris Allegrettaa8c22572002-02-15 19:17:02 +00003282 switch (which->val) {
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003283 case TOGGLE_SUSPEND_KEY:
3284 signal_init();
3285 break;
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00003286#ifndef DISABLE_MOUSE
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003287 case TOGGLE_MOUSE_KEY:
3288 mouse_init();
3289 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003290#endif
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003291 case TOGGLE_NOHELP_KEY:
3292 blank_statusbar();
3293 blank_bottombars();
3294 wrefresh(bottomwin);
3295 window_init();
3296 edit_refresh();
3297 display_main_list();
3298 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003299#ifdef ENABLE_COLOR
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003300 case TOGGLE_SYNTAX_KEY:
3301 edit_refresh();
3302 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003303#endif
David Lawrence Ramsey483ea322004-05-29 16:25:30 +00003304#ifdef ENABLE_NANORC
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003305 case TOGGLE_WHITESPACE_KEY:
David Lawrence Ramsey846658e2004-12-05 04:18:26 +00003306 titlebar(NULL);
David Lawrence Ramsey62a217d2004-11-27 16:54:00 +00003307 edit_refresh();
3308 break;
David Lawrence Ramsey483ea322004-05-29 16:25:30 +00003309#endif
Chris Allegretta756f2202000-09-01 13:32:47 +00003310 }
Chris Allegretta2a42af12000-09-12 23:02:49 +00003311
Chris Allegretta6df90f52002-07-19 01:08:59 +00003312 /* We are assuming here that shortcut_init() above didn't free and
3313 * reallocate the toggles. */
3314 enabled = ISSET(which->flag);
3315 if (which->val == TOGGLE_NOHELP_KEY || which->val == TOGGLE_WRAP_KEY)
3316 enabled = !enabled;
3317 statusbar("%s %s", which->desc,
3318 enabled ? _("enabled") : _("disabled"));
Chris Allegretta756f2202000-09-01 13:32:47 +00003319}
Chris Allegrettadab017e2002-04-23 10:56:06 +00003320#endif /* !NANO_SMALL */
Chris Allegretta756f2202000-09-01 13:32:47 +00003321
David Lawrence Ramsey013344c2004-09-22 22:45:08 +00003322void disable_extended_input(void)
3323{
3324 struct termios term;
3325
3326 tcgetattr(0, &term);
3327 term.c_lflag &= ~IEXTEN;
3328 tcsetattr(0, TCSANOW, &term);
3329}
3330
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003331void disable_signals(void)
3332{
3333 struct termios term;
3334
3335 tcgetattr(0, &term);
3336 term.c_lflag &= ~ISIG;
3337 tcsetattr(0, TCSANOW, &term);
3338}
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00003339
3340#ifndef NANO_SMALL
3341void enable_signals(void)
3342{
3343 struct termios term;
3344
3345 tcgetattr(0, &term);
3346 term.c_lflag |= ISIG;
3347 tcsetattr(0, TCSANOW, &term);
3348}
3349#endif
3350
3351void disable_flow_control(void)
3352{
3353 struct termios term;
3354
3355 tcgetattr(0, &term);
3356 term.c_iflag &= ~(IXON|IXOFF);
3357 tcsetattr(0, TCSANOW, &term);
3358}
3359
3360void enable_flow_control(void)
3361{
3362 struct termios term;
3363
3364 tcgetattr(0, &term);
3365 term.c_iflag |= (IXON|IXOFF);
3366 tcsetattr(0, TCSANOW, &term);
3367}
3368
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00003369/* Set up the terminal state. Put the terminal in cbreak mode (read one
3370 * character at a time and interpret the special control keys), disable
3371 * translation of carriage return (^M) into newline (^J) so that we can
3372 * tell the difference between the Enter key and Ctrl-J, and disable
David Lawrence Ramsey013344c2004-09-22 22:45:08 +00003373 * echoing of characters as they're typed. Finally, disable extended
3374 * input processing, disable interpretation of the special control keys,
3375 * and if we're not in preserve mode, disable interpretation of the flow
3376 * control characters too. */
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00003377void terminal_init(void)
3378{
3379 cbreak();
3380 nonl();
3381 noecho();
David Lawrence Ramsey013344c2004-09-22 22:45:08 +00003382 disable_extended_input();
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00003383 disable_signals();
3384 if (!ISSET(PRESERVE))
3385 disable_flow_control();
3386}
3387
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003388int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00003389 *finished, bool allow_funcs)
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003390{
3391 int input;
3392 /* The character we read in. */
3393 static int *kbinput = NULL;
3394 /* The input buffer. */
3395 static size_t kbinput_len = 0;
3396 /* The length of the input buffer. */
3397 const shortcut *s;
3398 bool have_shortcut;
3399#ifndef NANO_SMALL
3400 const toggle *t;
3401 bool have_toggle;
3402#endif
3403
3404 *s_or_t = FALSE;
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00003405 *finished = FALSE;
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003406
3407 /* Read in a character. */
3408 input = get_kbinput(edit, meta_key, func_key);
3409
3410#ifndef DISABLE_MOUSE
3411 /* If we got a mouse click and it was on a shortcut, read in the
3412 * shortcut character. */
3413 if (allow_funcs && func_key && input == KEY_MOUSE) {
3414 if (do_mouse())
3415 input = get_kbinput(edit, meta_key, func_key);
3416 else
3417 input = ERR;
3418 }
3419#endif
3420
3421 /* Check for a shortcut in the main list. */
3422 s = get_shortcut(main_list, &input, meta_key, func_key);
3423
3424 /* If we got a shortcut from the main list, or a "universal"
3425 * edit window shortcut, set have_shortcut to TRUE. */
3426 have_shortcut = (s != NULL || input == NANO_XON_KEY ||
3427 input == NANO_XOFF_KEY || input == NANO_SUSPEND_KEY);
3428
3429#ifndef NANO_SMALL
3430 /* Check for a toggle in the main list. */
3431 t = get_toggle(input, *meta_key);
3432
3433 /* If we got a toggle from the main list, set have_toggle to
3434 * TRUE. */
3435 have_toggle = (t != NULL);
3436#endif
3437
3438 /* Set s_or_t to TRUE if we got a shortcut or toggle. */
3439 *s_or_t = (have_shortcut
3440#ifndef NANO_SMALL
3441 || have_toggle
3442#endif
3443 );
3444
3445 if (allow_funcs) {
3446 /* If we got a character, and it isn't a shortcut, toggle, or
3447 * control character, it's a normal text character. Display the
3448 * warning if we're in view mode, or add the character to the
3449 * input buffer if we're not. */
3450 if (input != ERR && *s_or_t == FALSE && !is_cntrl_char(input)) {
3451 if (ISSET(VIEW_MODE))
3452 print_view_warning();
3453 else {
3454 kbinput_len++;
3455 kbinput = (int *)nrealloc(kbinput, kbinput_len *
3456 sizeof(int));
3457 kbinput[kbinput_len - 1] = input;
3458 }
3459 }
3460
3461 /* If we got a shortcut or toggle, or if there aren't any other
3462 * characters waiting after the one we read in, we need to
3463 * display all the characters in the input buffer if it isn't
3464 * empty. Note that it should be empty if we're in view
3465 * mode. */
3466 if (*s_or_t == TRUE || get_buffer_len() == 0) {
3467 if (kbinput != NULL) {
3468 /* Display all the characters in the input buffer at
3469 * once. */
3470 do_output(kbinput, kbinput_len);
3471
3472 /* Empty the input buffer. */
3473 kbinput_len = 0;
3474 free(kbinput);
3475 kbinput = NULL;
3476 }
3477 }
3478
3479 if (have_shortcut) {
3480 switch (input) {
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00003481 /* Handle the "universal" statusbar prompt shortcuts,
3482 * setting ran_s_or_t to TRUE to indicate it. */
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003483 case NANO_XON_KEY:
3484 statusbar(_("XON ignored, mumble mumble."));
3485 break;
3486 case NANO_XOFF_KEY:
3487 statusbar(_("XOFF ignored, mumble mumble."));
3488 break;
3489#ifndef NANO_SMALL
3490 case NANO_SUSPEND_KEY:
3491 if (ISSET(SUSPEND))
3492 do_suspend(0);
3493 break;
3494#endif
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00003495 /* Handle the normal edit window shortcuts, setting
3496 * finished to TRUE to indicate that we're done after
3497 * running or trying to run their associated
3498 * functions. */
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003499 default:
3500 /* Blow away the text in the cutbuffer if we aren't
3501 * cutting text. */
3502 if (s->func != do_cut_text)
3503 cutbuffer_reset();
3504
David Lawrence Ramsey6a2f0682004-12-20 01:13:55 +00003505 if (s->func != NULL) {
3506 if (ISSET(VIEW_MODE) && !s->viewok)
3507 print_view_warning();
3508 else
3509 s->func();
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003510 }
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00003511 *finished = TRUE;
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003512 break;
3513 }
3514 }
3515#ifndef NANO_SMALL
3516 else if (have_toggle) {
3517 /* Blow away the text in the cutbuffer, since we aren't
3518 * cutting text. */
3519 cutbuffer_reset();
3520 /* Toggle the flag associated with this shortcut. */
3521 if (allow_funcs)
3522 do_toggle(t);
3523 }
3524#endif
3525 else
3526 /* Blow away the text in the cutbuffer, since we aren't
3527 * cutting text. */
3528 cutbuffer_reset();
3529 }
3530
3531 return input;
3532}
3533
3534#ifndef DISABLE_MOUSE
3535bool do_mouse(void)
3536{
3537 int mouse_x, mouse_y;
3538 bool retval;
3539
3540 retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
3541
3542 if (!retval) {
3543 /* We can click in the edit window to move the cursor. */
3544 if (wenclose(edit, mouse_y, mouse_x)) {
3545 bool sameline;
3546 /* Did they click on the line with the cursor? If they
3547 * clicked on the cursor, we set the mark. */
3548 size_t xcur;
3549 /* The character they clicked on. */
3550
3551 /* Subtract out the size of topwin. Perhaps we need a
3552 * constant somewhere? */
3553 mouse_y -= 2;
3554
3555 sameline = (mouse_y == current_y);
3556
3557 /* Move to where the click occurred. */
3558 for (; current_y < mouse_y && current->next != NULL; current_y++)
3559 current = current->next;
3560 for (; current_y > mouse_y && current->prev != NULL; current_y--)
3561 current = current->prev;
3562
3563 xcur = actual_x(current->data, get_page_start(xplustabs()) +
3564 mouse_x);
3565
3566#ifndef NANO_SMALL
3567 /* Clicking where the cursor is toggles the mark, as does
3568 * clicking beyond the line length with the cursor at the
3569 * end of the line. */
3570 if (sameline && xcur == current_x) {
3571 if (ISSET(VIEW_MODE)) {
3572 print_view_warning();
3573 return retval;
3574 }
3575 do_mark();
3576 }
3577#endif
3578
3579 current_x = xcur;
3580 placewewant = xplustabs();
3581 edit_refresh();
3582 }
3583 }
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003584
3585 return retval;
3586}
3587#endif /* !DISABLE_MOUSE */
3588
David Lawrence Ramseybed32a32004-12-07 18:05:38 +00003589/* The user typed kbinput_len wide characters. Add them to the edit
3590 * buffer as multibyte characters. */
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003591void do_output(int *kbinput, size_t kbinput_len)
3592{
3593 size_t i, current_len = strlen(current->data);
3594 bool old_constupdate = ISSET(CONSTUPDATE);
3595 bool do_refresh = FALSE;
3596 /* Do we have to call edit_refresh(), or can we get away with
3597 * update_line()? */
3598
David Lawrence Ramsey78ea5e42004-12-12 19:04:56 +00003599 char *key =
David Lawrence Ramsey415b5d92004-12-21 16:39:45 +00003600#ifdef NANO_WIDE
David Lawrence Ramseya35bdd12004-12-27 18:53:38 +00003601 !ISSET(NO_UTF8) ? charalloc(MB_CUR_MAX) :
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003602#endif
David Lawrence Ramseya35bdd12004-12-27 18:53:38 +00003603 charalloc(1);
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003604
3605 assert(current != NULL && current->data != NULL);
3606
David Lawrence Ramsey846658e2004-12-05 04:18:26 +00003607 /* Turn off constant cursor position display. */
3608 UNSET(CONSTUPDATE);
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003609
3610 for (i = 0; i < kbinput_len; i++) {
David Lawrence Ramsey78ea5e42004-12-12 19:04:56 +00003611 int key_len;
3612
David Lawrence Ramseyabc94232004-12-08 23:24:31 +00003613 /* Null to newline, if needed. */
3614 if (kbinput[i] == '\0')
3615 kbinput[i] = '\n';
3616 /* Newline to Enter, if needed. */
3617 else if (kbinput[i] == '\n') {
3618 do_enter();
3619 continue;
3620 }
3621
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003622#ifdef NANO_WIDE
3623 /* Change the wide character to its multibyte value. If it's
3624 * invalid, go on to the next character. */
3625 if (!ISSET(NO_UTF8)) {
David Lawrence Ramseyc2ac02f2004-12-09 03:05:45 +00003626 key_len = wctomb(key, (wchar_t)kbinput[i]);
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003627
3628 if (key_len == -1)
3629 continue;
David Lawrence Ramsey95a02242004-12-06 04:14:42 +00003630 /* Interpret the character as a single-byte sequence. */
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003631 } else {
3632#endif
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003633 key_len = 1;
David Lawrence Ramsey10820de2004-12-07 05:02:21 +00003634 key[0] = (unsigned char)kbinput[i];
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003635#ifdef NANO_WIDE
3636 }
3637#endif
3638
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003639 /* When a character is inserted on the current magicline, it
3640 * means we need a new one! */
3641 if (filebot == current)
3642 new_magicline();
3643
3644 /* More dangerousness fun =) */
3645 current->data = charealloc(current->data,
3646 current_len + key_len + 1);
David Lawrence Ramsey3e819142004-12-31 04:10:28 +00003647
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003648 assert(current_x <= current_len);
David Lawrence Ramsey3e819142004-12-31 04:10:28 +00003649
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003650 charmove(&current->data[current_x + key_len],
3651 &current->data[current_x],
3652 current_len - current_x + key_len);
3653 charcpy(&current->data[current_x], key, key_len);
3654 current_len += key_len;
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003655 totsize += key_len;
3656 set_modified();
3657
3658#ifndef NANO_SMALL
3659 /* Note that current_x has not yet been incremented. */
3660 if (current == mark_beginbuf && current_x < mark_beginx)
3661 mark_beginx += key_len;
3662#endif
3663
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00003664 do_right(FALSE);
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003665
3666#ifndef DISABLE_WRAPPING
3667 /* If we're wrapping text, we need to call edit_refresh(). */
David Lawrence Ramseye750fe62004-12-18 22:43:23 +00003668 if (!ISSET(NO_WRAP) && kbinput[i] != '\t') {
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003669 bool do_refresh_save = do_refresh;
3670
3671 do_refresh = do_wrap(current);
3672
3673 /* If we needed to call edit_refresh() before this, we'll
3674 * still need to after this. */
3675 if (do_refresh_save)
3676 do_refresh = TRUE;
3677 }
3678#endif
3679
3680#ifdef ENABLE_COLOR
3681 /* If color syntaxes are turned on, we need to call
3682 * edit_refresh(). */
3683 if (ISSET(COLOR_SYNTAX))
3684 do_refresh = TRUE;
3685#endif
3686 }
3687
David Lawrence Ramsey846658e2004-12-05 04:18:26 +00003688 /* Turn constant cursor position display back on if it was on
3689 * before. */
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003690 if (old_constupdate)
3691 SET(CONSTUPDATE);
3692
David Lawrence Ramsey78ea5e42004-12-12 19:04:56 +00003693 free(key);
3694
David Lawrence Ramsey74835712004-12-04 17:41:52 +00003695 if (do_refresh)
3696 edit_refresh();
3697 else
3698 update_line(current, current_x);
3699}
3700
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00003701int main(int argc, char **argv)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003702{
3703 int optchr;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003704 int startline = 0;
3705 /* Line to try and start at. */
David Lawrence Ramseydf13e3b2004-08-12 19:48:21 +00003706#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003707 bool fill_flag_used = FALSE;
3708 /* Was the fill option used? */
David Lawrence Ramseydf13e3b2004-08-12 19:48:21 +00003709#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003710#ifdef ENABLE_MULTIBUFFER
3711 bool old_multibuffer;
3712 /* The old value of the multibuffer option, restored after we
3713 * load all files on the command line. */
3714#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003715#ifdef HAVE_GETOPT_LONG
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003716 const struct option long_options[] = {
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003717 {"help", 0, 0, 'h'},
3718#ifdef ENABLE_MULTIBUFFER
3719 {"multibuffer", 0, 0, 'F'},
3720#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00003721#ifdef ENABLE_NANORC
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00003722#ifndef NANO_SMALL
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00003723 {"historylog", 0, 0, 'H'},
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00003724#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00003725 {"ignorercfiles", 0, 0, 'I'},
3726#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003727#ifndef DISABLE_JUSTIFY
3728 {"quotestr", 1, 0, 'Q'},
3729#endif
Chris Allegretta805c26d2000-09-06 13:39:17 +00003730#ifdef HAVE_REGEX_H
Chris Allegrettabd9e7c32000-10-26 01:44:42 +00003731 {"regexp", 0, 0, 'R'},
Chris Allegretta47805612000-07-07 02:35:34 +00003732#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003733 {"tabsize", 1, 0, 'T'},
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003734 {"version", 0, 0, 'V'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003735#ifdef ENABLE_COLOR
3736 {"syntax", 1, 0, 'Y'},
3737#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003738 {"const", 0, 0, 'c'},
David Lawrence Ramsey4d7c2602003-08-17 02:48:43 +00003739 {"rebinddelete", 0, 0, 'd'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003740 {"nofollow", 0, 0, 'l'},
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00003741#ifndef DISABLE_MOUSE
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003742 {"mouse", 0, 0, 'm'},
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003743#endif
Chris Allegrettae1f14522001-09-19 03:19:43 +00003744#ifndef DISABLE_OPERATINGDIR
3745 {"operatingdir", 1, 0, 'o'},
3746#endif
Chris Allegretta6cd143d2003-01-05 23:35:44 +00003747 {"preserve", 0, 0, 'p'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003748#ifndef DISABLE_WRAPJUSTIFY
3749 {"fill", 1, 0, 'r'},
Chris Allegretta2d7893d2001-07-11 02:08:33 +00003750#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003751#ifndef DISABLE_SPELLER
3752 {"speller", 1, 0, 's'},
3753#endif
3754 {"tempfile", 0, 0, 't'},
3755 {"view", 0, 0, 'v'},
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003756#ifndef DISABLE_WRAPPING
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003757 {"nowrap", 0, 0, 'w'},
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +00003758#endif
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003759 {"nohelp", 0, 0, 'x'},
3760 {"suspend", 0, 0, 'z'},
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003761#ifndef NANO_SMALL
David Lawrence Ramseyc7acf692004-05-22 20:15:20 +00003762 {"smarthome", 0, 0, 'A'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003763 {"backup", 0, 0, 'B'},
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00003764 {"backupdir", 1, 0, 'E'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003765 {"noconvert", 0, 0, 'N'},
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003766 {"smooth", 0, 0, 'S'},
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00003767 {"restricted", 0, 0, 'Z'},
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00003768 {"autoindent", 0, 0, 'i'},
3769 {"cut", 0, 0, 'k'},
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003770#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003771 {0, 0, 0, 0}
3772 };
3773#endif
3774
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00003775#ifdef NANO_WIDE
3776 {
3777 /* If the locale set doesn't exist, or it exists but doesn't
3778 * include the string "UTF-8", we shouldn't use UTF-8
3779 * support. */
3780 char *locale = setlocale(LC_ALL, "");
3781
3782 if (locale == NULL || (locale != NULL &&
3783 strstr(locale, "UTF-8") == NULL))
3784 SET(NO_UTF8);
3785 }
3786#else
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003787 setlocale(LC_ALL, "");
David Lawrence Ramseyfc693212004-12-23 17:43:27 +00003788#endif
3789
David Lawrence Ramseyad1fd0d2004-07-27 15:46:58 +00003790#ifdef ENABLE_NLS
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003791 bindtextdomain(PACKAGE, LOCALEDIR);
3792 textdomain(PACKAGE);
3793#endif
3794
Chris Allegretta7662c862003-01-13 01:35:15 +00003795#if !defined(ENABLE_NANORC) && defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING)
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003796 /* If we don't have rcfile support, we're root, and
3797 * --disable-wrapping-as-root is used, turn wrapping off. */
David Lawrence Ramseya619ae62004-03-04 06:33:52 +00003798 if (geteuid() == NANO_ROOT_UID)
David Lawrence Ramseydc60b722002-10-25 16:08:53 +00003799 SET(NO_WRAP);
3800#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00003801
Chris Allegretta6df90f52002-07-19 01:08:59 +00003802 while ((optchr =
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003803#ifdef HAVE_GETOPT_LONG
David Lawrence Ramsey9ec76e52004-12-23 19:55:57 +00003804 getopt_long(argc, argv, "h?ABE:FHINQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz", long_options, NULL)
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003805#else
David Lawrence Ramsey9ec76e52004-12-23 19:55:57 +00003806 getopt(argc, argv, "h?ABE:FHINQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz")
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003807#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003808 ) != -1) {
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003809
3810 switch (optchr) {
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003811 case 'a':
3812 case 'b':
3813 case 'e':
3814 case 'f':
3815 case 'g':
3816 case 'j':
3817 /* Pico compatibility flags. */
3818 break;
Chris Allegretta7004c282001-09-22 00:42:10 +00003819#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003820 case 'A':
3821 SET(SMART_HOME);
3822 break;
3823 case 'B':
3824 SET(BACKUP_FILE);
3825 break;
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003826 case 'E':
3827 backup_dir = mallocstrcpy(backup_dir, optarg);
3828 break;
Chris Allegretta7004c282001-09-22 00:42:10 +00003829#endif
Chris Allegretta355fbe52001-07-14 19:32:47 +00003830#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003831 case 'F':
3832 SET(MULTIBUFFER);
3833 break;
Chris Allegretta2d7893d2001-07-11 02:08:33 +00003834#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +00003835#ifdef ENABLE_NANORC
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00003836#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003837 case 'H':
3838 SET(HISTORYLOG);
3839 break;
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00003840#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003841 case 'I':
3842 SET(NO_RCFILE);
3843 break;
Chris Allegretta6df90f52002-07-19 01:08:59 +00003844#endif
Chris Allegretta8fa1e282001-09-22 04:20:25 +00003845#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003846 case 'N':
3847 SET(NO_CONVERT);
3848 break;
Chris Allegrettaa8c22572002-02-15 19:17:02 +00003849#endif
Chris Allegrettae4f940d2002-03-03 22:36:36 +00003850#ifndef DISABLE_JUSTIFY
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003851 case 'Q':
3852 quotestr = mallocstrcpy(quotestr, optarg);
3853 break;
Chris Allegrettae4f940d2002-03-03 22:36:36 +00003854#endif
Chris Allegretta805c26d2000-09-06 13:39:17 +00003855#ifdef HAVE_REGEX_H
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003856 case 'R':
3857 SET(USE_REGEXP);
3858 break;
Chris Allegretta47805612000-07-07 02:35:34 +00003859#endif
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003860#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003861 case 'S':
3862 SET(SMOOTHSCROLL);
3863 break;
Chris Allegretta3e3ae942001-09-22 19:02:04 +00003864#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003865 case 'T':
3866 if (!parse_num(optarg, &tabsize) || tabsize <= 0) {
David Lawrence Ramsey6420d442004-08-11 05:13:08 +00003867 fprintf(stderr, _("Requested tab size %s invalid"), optarg);
3868 fprintf(stderr, "\n");
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003869 exit(1);
3870 }
3871 break;
3872 case 'V':
3873 version();
3874 exit(0);
Chris Allegretta09900ff2002-05-04 04:23:30 +00003875#ifdef ENABLE_COLOR
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003876 case 'Y':
3877 syntaxstr = mallocstrcpy(syntaxstr, optarg);
3878 break;
Chris Allegretta09900ff2002-05-04 04:23:30 +00003879#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003880 case 'Z':
3881 SET(RESTRICTED);
3882 break;
3883 case 'c':
3884 SET(CONSTUPDATE);
3885 break;
3886 case 'd':
3887 SET(REBIND_DELETE);
3888 break;
Chris Allegrettaff989832001-09-17 13:48:00 +00003889#ifndef NANO_SMALL
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003890 case 'i':
3891 SET(AUTOINDENT);
3892 break;
3893 case 'k':
3894 SET(CUT_TO_END);
3895 break;
Chris Allegrettad19e9912000-07-12 18:14:51 +00003896#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003897 case 'l':
3898 SET(NOFOLLOW_SYMLINKS);
3899 break;
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00003900#ifndef DISABLE_MOUSE
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003901 case 'm':
3902 SET(USE_MOUSE);
3903 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003904#endif
Chris Allegrettae1f14522001-09-19 03:19:43 +00003905#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003906 case 'o':
3907 operating_dir = mallocstrcpy(operating_dir, optarg);
3908 break;
Chris Allegrettae1f14522001-09-19 03:19:43 +00003909#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003910 case 'p':
3911 SET(PRESERVE);
3912 break;
Chris Allegretta6fe61492001-05-21 12:56:25 +00003913#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003914 case 'r':
3915 if (!parse_num(optarg, &wrap_at)) {
David Lawrence Ramsey6420d442004-08-11 05:13:08 +00003916 fprintf(stderr, _("Requested fill size %s invalid"), optarg);
3917 fprintf(stderr, "\n");
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003918 exit(1);
3919 }
3920 fill_flag_used = TRUE;
3921 break;
Chris Allegretta6fe61492001-05-21 12:56:25 +00003922#endif
Rocco Corsiaf5c3022001-01-12 07:51:05 +00003923#ifndef DISABLE_SPELLER
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003924 case 's':
3925 alt_speller = mallocstrcpy(alt_speller, optarg);
3926 break;
Rocco Corsiaf5c3022001-01-12 07:51:05 +00003927#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003928 case 't':
3929 SET(TEMP_FILE);
3930 break;
3931 case 'v':
3932 SET(VIEW_MODE);
3933 break;
David Lawrence Ramsey95e0cf52003-01-02 16:32:20 +00003934#ifndef DISABLE_WRAPPING
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003935 case 'w':
3936 SET(NO_WRAP);
3937 break;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00003938#endif
David Lawrence Ramseyc7b6ac52004-08-07 22:00:02 +00003939 case 'x':
3940 SET(NO_HELP);
3941 break;
3942 case 'z':
3943 SET(SUSPEND);
3944 break;
3945 default:
3946 usage();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003947 }
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00003948 }
3949
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00003950 /* If the executable filename starts with 'r', we use restricted
3951 * mode. */
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00003952 if (*(tail(argv[0])) == 'r')
3953 SET(RESTRICTED);
3954
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00003955 /* If we're using restricted mode, disable suspending, backups, and
3956 * reading rcfiles, since they all would allow reading from or
3957 * writing to files not specified on the command line. */
David Lawrence Ramseyd893fa92004-04-30 04:49:02 +00003958 if (ISSET(RESTRICTED)) {
3959 UNSET(SUSPEND);
3960 UNSET(BACKUP_FILE);
3961 SET(NO_RCFILE);
3962 }
3963
Chris Allegretta7662c862003-01-13 01:35:15 +00003964/* We've read through the command line options. Now back up the flags
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00003965 * and values that are set, and read the rcfile(s). If the values
3966 * haven't changed afterward, restore the backed-up values. */
Chris Allegretta7662c862003-01-13 01:35:15 +00003967#ifdef ENABLE_NANORC
3968 if (!ISSET(NO_RCFILE)) {
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00003969#ifndef DISABLE_OPERATINGDIR
Chris Allegretta7662c862003-01-13 01:35:15 +00003970 char *operating_dir_cpy = operating_dir;
3971#endif
David Lawrence Ramsey7e8dd192004-08-12 20:06:20 +00003972#ifndef DISABLE_WRAPJUSTIFY
David Lawrence Ramsey49c3f242004-07-12 16:07:14 +00003973 ssize_t wrap_at_cpy = wrap_at;
Chris Allegretta7662c862003-01-13 01:35:15 +00003974#endif
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00003975#ifndef NANO_SMALL
3976 char *backup_dir_cpy = backup_dir;
3977#endif
Chris Allegretta7662c862003-01-13 01:35:15 +00003978#ifndef DISABLE_JUSTIFY
3979 char *quotestr_cpy = quotestr;
3980#endif
3981#ifndef DISABLE_SPELLER
3982 char *alt_speller_cpy = alt_speller;
3983#endif
David Lawrence Ramsey49c3f242004-07-12 16:07:14 +00003984 ssize_t tabsize_cpy = tabsize;
Chris Allegretta7662c862003-01-13 01:35:15 +00003985 long flags_cpy = flags;
3986
Chris Allegretta5ec68622003-02-05 02:39:34 +00003987#ifndef DISABLE_OPERATINGDIR
Chris Allegretta7662c862003-01-13 01:35:15 +00003988 operating_dir = NULL;
Chris Allegretta5ec68622003-02-05 02:39:34 +00003989#endif
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00003990#ifndef NANO_SMALL
3991 backup_dir = NULL;
3992#endif
Chris Allegretta5ec68622003-02-05 02:39:34 +00003993#ifndef DISABLE_JUSTIFY
Chris Allegretta7662c862003-01-13 01:35:15 +00003994 quotestr = NULL;
Chris Allegretta5ec68622003-02-05 02:39:34 +00003995#endif
3996#ifndef DISABLE_SPELLER
Chris Allegretta7662c862003-01-13 01:35:15 +00003997 alt_speller = NULL;
Chris Allegretta5ec68622003-02-05 02:39:34 +00003998#endif
Chris Allegretta7662c862003-01-13 01:35:15 +00003999
4000 do_rcfile();
4001
4002#ifndef DISABLE_OPERATINGDIR
4003 if (operating_dir_cpy != NULL) {
4004 free(operating_dir);
4005 operating_dir = operating_dir_cpy;
4006 }
4007#endif
David Lawrence Ramseydf13e3b2004-08-12 19:48:21 +00004008#ifndef DISABLE_WRAPJUSTIFY
Chris Allegretta7662c862003-01-13 01:35:15 +00004009 if (fill_flag_used)
4010 wrap_at = wrap_at_cpy;
4011#endif
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00004012#ifndef NANO_SMALL
4013 if (backup_dir_cpy != NULL) {
4014 free(backup_dir);
4015 backup_dir = backup_dir_cpy;
4016 }
4017#endif
Chris Allegretta7662c862003-01-13 01:35:15 +00004018#ifndef DISABLE_JUSTIFY
4019 if (quotestr_cpy != NULL) {
4020 free(quotestr);
4021 quotestr = quotestr_cpy;
4022 }
4023#endif
4024#ifndef DISABLE_SPELLER
4025 if (alt_speller_cpy != NULL) {
4026 free(alt_speller);
4027 alt_speller = alt_speller_cpy;
4028 }
4029#endif
David Lawrence Ramsey04419b92004-07-18 18:13:54 +00004030 if (tabsize_cpy != -1)
Chris Allegretta7662c862003-01-13 01:35:15 +00004031 tabsize = tabsize_cpy;
4032 flags |= flags_cpy;
4033 }
4034#if defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING)
David Lawrence Ramseya619ae62004-03-04 06:33:52 +00004035 else if (geteuid() == NANO_ROOT_UID)
Chris Allegretta7662c862003-01-13 01:35:15 +00004036 SET(NO_WRAP);
4037#endif
4038#endif /* ENABLE_NANORC */
4039
Chris Allegrettad8451932003-03-11 03:50:40 +00004040#ifndef NANO_SMALL
4041 history_init();
4042#ifdef ENABLE_NANORC
4043 if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG))
4044 load_history();
4045#endif
4046#endif
4047
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00004048#ifndef NANO_SMALL
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004049 /* Set up the backup directory (unless we're using restricted mode,
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00004050 * in which case backups are disabled, since they would allow
4051 * reading from or writing to files not specified on the command
4052 * line). This entails making sure it exists and is a directory, so
4053 * that backup files will be saved there. */
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004054 if (!ISSET(RESTRICTED))
4055 init_backup_dir();
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00004056#endif
4057
Chris Allegretta7662c862003-01-13 01:35:15 +00004058#ifndef DISABLE_OPERATINGDIR
4059 /* Set up the operating directory. This entails chdir()ing there,
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004060 * so that file reads and writes will be based there. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00004061 init_operating_dir();
4062#endif
4063
Chris Allegretta7662c862003-01-13 01:35:15 +00004064#ifndef DISABLE_JUSTIFY
David Lawrence Ramsey2c62b072004-05-29 16:38:57 +00004065 if (punct == NULL)
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004066 punct = mallocstrcpy(punct, ".?!");
David Lawrence Ramsey2c62b072004-05-29 16:38:57 +00004067
4068 if (brackets == NULL)
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004069 brackets = mallocstrcpy(brackets, "'\")}]>");
David Lawrence Ramsey2c62b072004-05-29 16:38:57 +00004070
Chris Allegretta7662c862003-01-13 01:35:15 +00004071 if (quotestr == NULL)
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004072 quotestr = mallocstrcpy(NULL,
Chris Allegretta7662c862003-01-13 01:35:15 +00004073#ifdef HAVE_REGEX_H
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004074 "^([ \t]*[|>:}#])+"
Chris Allegretta7662c862003-01-13 01:35:15 +00004075#else
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004076 "> "
Chris Allegretta7662c862003-01-13 01:35:15 +00004077#endif
David Lawrence Ramsey74af3a72004-06-12 21:03:14 +00004078 );
David Lawrence Ramsey819c7f02004-07-30 03:54:34 +00004079#ifdef HAVE_REGEX_H
4080 quoterc = regcomp(&quotereg, quotestr, REG_EXTENDED);
4081
4082 if (quoterc == 0) {
4083 /* We no longer need quotestr, just quotereg. */
4084 free(quotestr);
4085 quotestr = NULL;
4086 } else {
4087 size_t size = regerror(quoterc, &quotereg, NULL, 0);
4088
4089 quoteerr = charalloc(size);
4090 regerror(quoterc, &quotereg, quoteerr, size);
4091 }
4092#else
4093 quotelen = strlen(quotestr);
4094#endif /* !HAVE_REGEX_H */
Chris Allegretta7662c862003-01-13 01:35:15 +00004095#endif /* !DISABLE_JUSTIFY */
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00004096
David Lawrence Ramsey3aedb362004-05-28 22:42:41 +00004097#ifndef DISABLE_SPELLER
4098 /* If we don't have an alternative spell checker after reading the
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00004099 * command line and/or rcfile(s), check $SPELL for one, as Pico
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004100 * does (unless we're using restricted mode, in which case spell
David Lawrence Ramsey32e3b882004-05-29 01:20:17 +00004101 * checking is disabled, since it would allow reading from or
4102 * writing to files not specified on the command line). */
David Lawrence Ramseydd7cc722004-05-28 23:45:25 +00004103 if (!ISSET(RESTRICTED) && alt_speller == NULL) {
David Lawrence Ramsey3aedb362004-05-28 22:42:41 +00004104 char *spellenv = getenv("SPELL");
4105 if (spellenv != NULL)
4106 alt_speller = mallocstrcpy(NULL, spellenv);
4107 }
4108#endif
4109
David Lawrence Ramsey483ea322004-05-29 16:25:30 +00004110#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004111 /* If whitespace wasn't specified, set its default value. */
David Lawrence Ramsey483ea322004-05-29 16:25:30 +00004112 if (whitespace == NULL)
4113 whitespace = mallocstrcpy(NULL, " ");
4114#endif
4115
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004116 /* If tabsize wasn't specified, set its default value. */
Chris Allegretta7662c862003-01-13 01:35:15 +00004117 if (tabsize == -1)
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004118 tabsize = WIDTH_OF_TAB;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00004119
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00004120 /* Back up the old terminal settings so that they can be restored. */
Chris Allegretta9b4055c2002-03-29 16:00:59 +00004121 tcgetattr(0, &oldterm);
Chris Allegretta6ca01b12002-03-29 15:38:17 +00004122
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004123 /* Curses initialization stuff: Start curses and set up the
4124 * terminal state. */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004125 initscr();
David Lawrence Ramsey8aaf0302004-07-27 16:46:35 +00004126 terminal_init();
David Lawrence Ramseyc53a92d2004-01-12 03:28:06 +00004127
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00004128 /* Set up the global variables and the shortcuts. */
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +00004129 global_init(FALSE);
4130 shortcut_init(FALSE);
David Lawrence Ramsey369732f2004-02-16 20:32:40 +00004131
4132 /* Set up the signal handlers. */
Chris Allegretta9b4055c2002-03-29 16:00:59 +00004133 signal_init();
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004134
4135#ifdef DEBUG
Jordi Mallachf9390af2003-08-05 19:31:12 +00004136 fprintf(stderr, "Main: set up windows\n");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004137#endif
4138
Chris Allegretta2a42af12000-09-12 23:02:49 +00004139 window_init();
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00004140#ifndef DISABLE_MOUSE
Chris Allegretta756f2202000-09-01 13:32:47 +00004141 mouse_init();
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00004142#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004143
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004144#ifdef DEBUG
Jordi Mallachf9390af2003-08-05 19:31:12 +00004145 fprintf(stderr, "Main: open file\n");
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004146#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004147
David Lawrence Ramseybf1346f2004-10-22 20:25:56 +00004148 /* If there's a +LINE flag here, it is the first non-option
4149 * argument, and it is followed by at least one other argument, the
4150 * filename it applies to. */
4151 if (0 < optind && optind < argc - 1 && argv[optind][0] == '+') {
4152 startline = atoi(&argv[optind][1]);
4153 optind++;
4154 }
4155
Chris Allegretta7662c862003-01-13 01:35:15 +00004156#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004157 old_multibuffer = ISSET(MULTIBUFFER);
4158 SET(MULTIBUFFER);
4159
4160 /* Read all the files after the first one on the command line into
4161 * new buffers. */
4162 {
David Lawrence Ramseybf1346f2004-10-22 20:25:56 +00004163 int i = optind + 1, iline = 0;
4164 for (; i < argc; i++) {
4165 /* If there's a +LINE flag here, it is followed by at least
4166 * one other argument, the filename it applies to. */
4167 if (i < argc - 1 && argv[i][0] == '+' && iline == 0) {
4168 iline = atoi(&argv[i][1]);
4169 } else {
4170 load_buffer(argv[i]);
4171 if (iline > 0) {
4172 do_gotoline(iline, FALSE);
4173 iline = 0;
4174 }
4175 }
4176 }
Chris Allegretta7662c862003-01-13 01:35:15 +00004177 }
4178#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004179
David Lawrence Ramsey02517e02004-09-05 21:40:31 +00004180 /* Read the first file on the command line into either the current
4181 * buffer or a new buffer, depending on whether multibuffer mode is
4182 * enabled. */
4183 if (optind < argc)
4184 load_buffer(argv[optind]);
4185
4186 /* We didn't open any files if all the command line arguments were
4187 * invalid files like directories or if there were no command line
4188 * arguments given. In this case, we have to load a blank buffer.
4189 * Also, we unset view mode to allow editing. */
4190 if (filename == NULL) {
4191 filename = mallocstrcpy(NULL, "");
4192 new_file();
4193 UNSET(VIEW_MODE);
4194
4195 /* Add this new entry to the open_files structure if we have
4196 * multibuffer support, or to the main filestruct if we don't. */
4197 load_file();
4198 }
4199
4200#ifdef ENABLE_MULTIBUFFER
4201 if (!old_multibuffer)
4202 UNSET(MULTIBUFFER);
4203#endif
4204
4205#ifdef DEBUG
4206 fprintf(stderr, "Main: top and bottom win\n");
4207#endif
4208
4209 titlebar(NULL);
4210 display_main_list();
4211
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004212 if (startline > 0)
David Lawrence Ramsey49c3f242004-07-12 16:07:14 +00004213 do_gotoline(startline, FALSE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004214
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00004215#ifndef NANO_SMALL
4216 /* Return here after a SIGWINCH. */
Chris Allegretta6b58acd2001-04-12 03:01:53 +00004217 sigsetjmp(jmpbuf, 1);
David Lawrence Ramseyd7fd2002004-05-18 01:20:36 +00004218#endif
Chris Allegretta08020882001-01-29 23:37:54 +00004219
Robert Siemborski6967eec2000-07-08 14:23:32 +00004220 edit_refresh();
Robert Siemborski6967eec2000-07-08 14:23:32 +00004221
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00004222 while (TRUE) {
David Lawrence Ramsey74835712004-12-04 17:41:52 +00004223 bool meta_key;
4224 /* Whether we got a meta key sequence. */
4225 bool func_key;
4226 /* Whether we got a function key. */
4227 bool s_or_t;
4228 /* Whether we got a shortcut or toggle. */
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00004229 bool ran_s_or_t;
4230 /* Whether we ran a function associated with a
4231 * shortcut. */
David Lawrence Ramsey74835712004-12-04 17:41:52 +00004232
4233 /* Make sure the cursor is in the edit window. */
David Lawrence Ramseyaea4dab2004-07-13 17:09:24 +00004234 reset_cursor();
David Lawrence Ramsey74835712004-12-04 17:41:52 +00004235
4236 /* If constant cursor position display is on, display the cursor
4237 * position. */
Chris Allegrettad26ab912003-01-28 01:16:47 +00004238 if (ISSET(CONSTUPDATE))
David Lawrence Ramsey74912c62004-07-01 19:41:09 +00004239 do_cursorpos(TRUE);
Chris Allegrettad26ab912003-01-28 01:16:47 +00004240
Chris Allegretta6b58acd2001-04-12 03:01:53 +00004241 currshortcut = main_list;
Chris Allegretta6b58acd2001-04-12 03:01:53 +00004242
David Lawrence Ramsey74835712004-12-04 17:41:52 +00004243 /* Read in and interpret characters. */
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00004244 do_input(&meta_key, &func_key, &s_or_t, &ran_s_or_t, TRUE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004245 }
David Lawrence Ramseyfd1768a2004-03-19 21:57:56 +00004246 assert(FALSE);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00004247}