2000-08-06 21:13:45 +00:00
|
|
|
/* $Id$ */
|
2000-06-06 05:53:49 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* nano.c *
|
|
|
|
* *
|
2004-01-09 23:04:55 +00:00
|
|
|
* Copyright (C) 1999-2004 Chris Allegretta *
|
2000-06-06 05:53:49 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
2001-10-24 11:33:54 +00:00
|
|
|
* the Free Software Foundation; either version 2, or (at your option) *
|
2000-06-06 05:53:49 +00:00
|
|
|
* any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2004-11-17 23:17:05 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2001-04-28 18:03:52 +00:00
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/param.h>
|
2000-11-05 16:52:21 +00:00
|
|
|
#include <sys/wait.h>
|
2000-06-06 05:53:49 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <locale.h>
|
2000-06-07 03:56:54 +00:00
|
|
|
#include <assert.h>
|
2000-06-06 05:53:49 +00:00
|
|
|
#include "proto.h"
|
|
|
|
#include "nano.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_TERMIO_H
|
|
|
|
#include <termio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
2001-05-21 12:56:25 +00:00
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
2004-07-12 16:07:14 +00:00
|
|
|
static ssize_t fill = 0; /* Fill - where to wrap lines,
|
|
|
|
basically */
|
2001-05-21 12:56:25 +00:00
|
|
|
#endif
|
2003-09-10 20:08:00 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2004-08-05 22:10:22 +00:00
|
|
|
static bool same_line_wrap = FALSE; /* Whether wrapped text should
|
2004-06-22 15:38:47 +00:00
|
|
|
be prepended to the next
|
|
|
|
line */
|
2003-09-10 20:08:00 +00:00
|
|
|
#endif
|
2001-01-12 07:51:05 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
static struct termios oldterm; /* The user's original term settings */
|
2001-05-05 17:45:54 +00:00
|
|
|
static struct sigaction act; /* For all our fun signal handlers */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-06-22 15:38:47 +00:00
|
|
|
static sigjmp_buf jmpbuf; /* Used to return to mainloop after
|
|
|
|
SIGWINCH */
|
2004-08-05 15:16:19 +00:00
|
|
|
static int pid; /* The PID of the newly forked process
|
|
|
|
* in open_pipe(). It must be global
|
|
|
|
* because the signal handler needs
|
|
|
|
* it. */
|
2004-05-18 01:20:36 +00:00
|
|
|
#endif
|
2001-01-29 23:37:54 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
|
|
|
static filestruct *jusbottom = NULL;
|
|
|
|
/* Pointer to end of justify buffer. */
|
|
|
|
#endif
|
|
|
|
|
2004-05-25 19:41:11 +00:00
|
|
|
/* What we do when we're all set to exit. */
|
|
|
|
void finish(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2004-05-18 01:20:36 +00:00
|
|
|
if (!ISSET(NO_HELP))
|
|
|
|
blank_bottombars();
|
|
|
|
else
|
|
|
|
blank_statusbar();
|
2001-04-12 03:01:53 +00:00
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
wrefresh(bottomwin);
|
|
|
|
endwin();
|
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Restore the old terminal settings. */
|
2000-06-21 03:00:43 +00:00
|
|
|
tcsetattr(0, TCSANOW, &oldterm);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2003-03-11 03:50:40 +00:00
|
|
|
#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
|
|
|
|
if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG))
|
|
|
|
save_history();
|
|
|
|
#endif
|
|
|
|
|
2002-05-12 19:52:15 +00:00
|
|
|
#ifdef DEBUG
|
2002-02-27 04:14:16 +00:00
|
|
|
thanks_for_all_the_fish();
|
2002-05-12 19:52:15 +00:00
|
|
|
#endif
|
2002-02-27 04:14:16 +00:00
|
|
|
|
2004-05-25 19:41:11 +00:00
|
|
|
exit(0);
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-10 23:05:59 +00:00
|
|
|
/* Die (gracefully?). */
|
2002-07-19 01:08:59 +00:00
|
|
|
void die(const char *msg, ...)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2003-02-03 03:32:08 +00:00
|
|
|
endwin();
|
|
|
|
curses_ended = TRUE;
|
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Restore the old terminal settings. */
|
2001-07-11 02:08:33 +00:00
|
|
|
tcsetattr(0, TCSANOW, &oldterm);
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
va_start(ap, msg);
|
|
|
|
vfprintf(stderr, msg, ap);
|
|
|
|
va_end(ap);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2004-12-01 15:11:27 +00:00
|
|
|
/* Save the current file buffer if it's been modified. */
|
2002-01-02 15:12:21 +00:00
|
|
|
if (ISSET(MODIFIED))
|
|
|
|
die_save_file(filename);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2001-07-14 19:32:47 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2004-12-01 15:11:27 +00:00
|
|
|
/* Save all of the other modified file buffers, if any. */
|
2002-12-22 16:30:00 +00:00
|
|
|
if (open_files != NULL) {
|
2004-12-01 15:11:27 +00:00
|
|
|
openfilestruct *tmp = open_files;
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2004-12-01 15:11:27 +00:00
|
|
|
while (tmp != open_files->next) {
|
|
|
|
open_files = open_files->next;
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2004-12-01 15:11:27 +00:00
|
|
|
/* Save the current file buffer if it's been modified. */
|
|
|
|
if (open_files->flags & MODIFIED) {
|
|
|
|
/* Set fileage and filebot to match the current file
|
|
|
|
* buffer, and then write it to disk. */
|
2002-04-10 02:31:20 +00:00
|
|
|
fileage = open_files->fileage;
|
2002-05-11 03:04:44 +00:00
|
|
|
filebot = open_files->filebot;
|
2004-12-01 15:11:27 +00:00
|
|
|
die_save_file(open_files->filename);
|
2001-07-11 02:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-12-01 15:11:27 +00:00
|
|
|
/* Get out. */
|
|
|
|
exit(1);
|
2001-07-11 02:08:33 +00:00
|
|
|
}
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
void die_save_file(const char *die_filename)
|
2001-07-11 02:08:33 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
char *ret;
|
2004-08-05 22:10:22 +00:00
|
|
|
bool failed = TRUE;
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2004-05-29 01:20:17 +00:00
|
|
|
/* If we're using restricted mode, don't write any emergency backup
|
|
|
|
* files, since that would allow reading from or writing to files
|
|
|
|
* not specified on the command line. */
|
2004-04-30 04:49:02 +00:00
|
|
|
if (ISSET(RESTRICTED))
|
|
|
|
return;
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
/* If we can't save, we have REAL bad problems, but we might as well
|
|
|
|
TRY. */
|
|
|
|
if (die_filename[0] == '\0')
|
2004-08-10 23:05:59 +00:00
|
|
|
die_filename = "nano";
|
|
|
|
|
|
|
|
ret = get_next_filename(die_filename);
|
2002-07-19 01:08:59 +00:00
|
|
|
if (ret[0] != '\0')
|
2004-10-04 15:23:47 +00:00
|
|
|
failed = (write_file(ret, TRUE, FALSE, TRUE) == -1);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-08-05 22:10:22 +00:00
|
|
|
if (!failed)
|
2002-07-19 01:08:59 +00:00
|
|
|
fprintf(stderr, _("\nBuffer written to %s\n"), ret);
|
2000-12-02 04:36:50 +00:00
|
|
|
else
|
2004-09-05 21:40:31 +00:00
|
|
|
fprintf(stderr, _("\nBuffer not written to %s (too many backup files?)\n"), ret);
|
2002-02-22 04:30:50 +00:00
|
|
|
|
|
|
|
free(ret);
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2001-01-14 05:18:27 +00:00
|
|
|
/* Die with an error message that the screen was too small if, well, the
|
2002-07-19 01:08:59 +00:00
|
|
|
* screen is too small. */
|
2002-08-21 16:10:37 +00:00
|
|
|
void die_too_small(void)
|
2001-01-14 05:18:27 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
die(_("Window size is too small for nano...\n"));
|
2001-01-14 05:18:27 +00:00
|
|
|
}
|
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
void print_view_warning(void)
|
|
|
|
{
|
|
|
|
statusbar(_("Key illegal in VIEW mode"));
|
|
|
|
}
|
|
|
|
|
2004-01-09 23:04:55 +00:00
|
|
|
/* Initialize global variables -- no better way for now. If
|
2004-08-05 22:10:22 +00:00
|
|
|
* save_cutbuffer is TRUE, don't set cutbuffer to NULL. */
|
|
|
|
void global_init(bool save_cutbuffer)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
|
|
|
current_x = 0;
|
|
|
|
current_y = 0;
|
2001-01-14 05:18:27 +00:00
|
|
|
|
2003-02-10 02:43:48 +00:00
|
|
|
editwinrows = LINES - 5 + no_help();
|
|
|
|
if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS)
|
2001-01-14 05:18:27 +00:00
|
|
|
die_too_small();
|
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
fileage = NULL;
|
2001-09-27 02:46:53 +00:00
|
|
|
if (!save_cutbuffer)
|
|
|
|
cutbuffer = NULL;
|
2000-06-06 05:53:49 +00:00
|
|
|
current = NULL;
|
|
|
|
edittop = NULL;
|
|
|
|
totlines = 0;
|
2001-09-27 02:46:53 +00:00
|
|
|
totsize = 0;
|
2000-06-06 05:53:49 +00:00
|
|
|
placewewant = 0;
|
2001-01-14 05:18:27 +00:00
|
|
|
|
2001-05-21 12:56:25 +00:00
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
2002-07-19 01:08:59 +00:00
|
|
|
fill = wrap_at;
|
2002-07-24 01:02:26 +00:00
|
|
|
if (fill <= 0)
|
2002-07-19 01:08:59 +00:00
|
|
|
fill += COLS;
|
2003-02-10 02:43:48 +00:00
|
|
|
if (fill < 0)
|
|
|
|
fill = 0;
|
2001-05-21 12:56:25 +00:00
|
|
|
#endif
|
2001-01-14 05:18:27 +00:00
|
|
|
|
2001-05-17 11:35:43 +00:00
|
|
|
hblank = charalloc(COLS + 1);
|
2001-01-23 02:35:04 +00:00
|
|
|
memset(hblank, ' ', COLS);
|
2002-06-13 00:40:19 +00:00
|
|
|
hblank[COLS] = '\0';
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
void window_init(void)
|
|
|
|
{
|
2003-02-10 02:43:48 +00:00
|
|
|
editwinrows = LINES - 5 + no_help();
|
|
|
|
if (editwinrows < MIN_EDITOR_ROWS)
|
2002-09-06 20:35:28 +00:00
|
|
|
die_too_small();
|
|
|
|
|
2003-01-26 04:15:56 +00:00
|
|
|
if (topwin != NULL)
|
|
|
|
delwin(topwin);
|
2004-03-29 18:36:39 +00:00
|
|
|
if (edit != NULL)
|
|
|
|
delwin(edit);
|
2003-01-26 04:15:56 +00:00
|
|
|
if (bottomwin != NULL)
|
|
|
|
delwin(bottomwin);
|
|
|
|
|
2004-03-29 18:36:39 +00:00
|
|
|
/* Set up the windows. */
|
2002-09-06 20:35:28 +00:00
|
|
|
topwin = newwin(2, COLS, 0, 0);
|
2004-03-29 18:36:39 +00:00
|
|
|
edit = newwin(editwinrows, COLS, 2, 0);
|
2002-09-06 20:35:28 +00:00
|
|
|
bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0);
|
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Turn the keypad back on. */
|
2002-09-06 20:35:28 +00:00
|
|
|
keypad(edit, TRUE);
|
|
|
|
keypad(bottomwin, TRUE);
|
|
|
|
}
|
|
|
|
|
2003-10-03 20:26:25 +00:00
|
|
|
#ifndef DISABLE_MOUSE
|
2002-09-06 20:35:28 +00:00
|
|
|
void mouse_init(void)
|
|
|
|
{
|
|
|
|
if (ISSET(USE_MOUSE)) {
|
|
|
|
mousemask(BUTTON1_RELEASED, NULL);
|
|
|
|
mouseinterval(50);
|
|
|
|
} else
|
|
|
|
mousemask(0, NULL);
|
|
|
|
}
|
2002-12-22 16:30:00 +00:00
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_HELP
|
|
|
|
/* This function allocates help_text, and stores the help string in it.
|
|
|
|
* help_text should be NULL initially. */
|
|
|
|
void help_init(void)
|
|
|
|
{
|
2004-08-05 15:45:09 +00:00
|
|
|
size_t allocsize = 1; /* Space needed for help_text. */
|
|
|
|
const char *htx; /* Untranslated help message. */
|
|
|
|
char *ptr;
|
2004-03-19 21:46:34 +00:00
|
|
|
const shortcut *s;
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
const toggle *t;
|
|
|
|
#endif
|
|
|
|
|
2004-08-05 15:45:09 +00:00
|
|
|
/* First, set up the initial help text for the current function. */
|
2002-09-06 20:35:28 +00:00
|
|
|
if (currshortcut == whereis_list || currshortcut == replace_list
|
|
|
|
|| currshortcut == replace_list_2)
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = N_("Search Command Help Text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"Enter the words or characters you would like to search "
|
2004-10-18 01:51:43 +00:00
|
|
|
"for, then hit Enter. If there is a match for the text you "
|
2002-09-06 20:35:28 +00:00
|
|
|
"entered, the screen will be updated to the location of the "
|
2004-10-18 01:51:43 +00:00
|
|
|
"nearest match for the search string.\n\n The previous "
|
|
|
|
"search string will be shown in brackets after the search "
|
|
|
|
"prompt. Hitting Enter without entering any text will "
|
|
|
|
"perform the previous search. If you have selected text "
|
|
|
|
"with the mark and then search to replace, only matches in "
|
|
|
|
"the selected text will be replaced.\n\n The following "
|
|
|
|
"function keys are available in Search mode:\n\n");
|
2004-09-30 22:07:21 +00:00
|
|
|
else if (currshortcut == gotoline_list)
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = N_("Go To Line Help Text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"Enter the line number that you wish to go to and hit "
|
|
|
|
"Enter. If there are fewer lines of text than the "
|
|
|
|
"number you entered, you will be brought to the last line "
|
|
|
|
"of the file.\n\n The following function keys are "
|
|
|
|
"available in Go To Line mode:\n\n");
|
|
|
|
else if (currshortcut == insertfile_list)
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = N_("Insert File Help Text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"Type in the name of a file to be inserted into the current "
|
|
|
|
"file buffer at the current cursor location.\n\n "
|
|
|
|
"If you have compiled nano with multiple file buffer "
|
|
|
|
"support, and enable multiple buffers with the -F "
|
|
|
|
"or --multibuffer command line flags, the Meta-F toggle, or "
|
|
|
|
"a nanorc file, inserting a file will cause it to be "
|
|
|
|
"loaded into a separate buffer (use Meta-< and > to switch "
|
2004-11-07 21:30:55 +00:00
|
|
|
"between file buffers). If you need another blank buffer, "
|
|
|
|
"do not enter any filename, or type in a nonexistent "
|
|
|
|
"filename at the prompt and press Enter.\n\n The following "
|
|
|
|
"function keys are available in Insert File mode:\n\n");
|
2002-09-06 20:35:28 +00:00
|
|
|
else if (currshortcut == writefile_list)
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = N_("Write File Help Text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"Type the name that you wish to save the current file "
|
|
|
|
"as and hit Enter to save the file.\n\n If you have "
|
2004-10-18 01:51:43 +00:00
|
|
|
"selected text with the mark, you will be prompted to "
|
2002-09-06 20:35:28 +00:00
|
|
|
"save only the selected portion to a separate file. To "
|
|
|
|
"reduce the chance of overwriting the current file with "
|
|
|
|
"just a portion of it, the current filename is not the "
|
|
|
|
"default in this mode.\n\n The following function keys "
|
|
|
|
"are available in Write File mode:\n\n");
|
|
|
|
#ifndef DISABLE_BROWSER
|
|
|
|
else if (currshortcut == browser_list)
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = N_("File Browser Help Text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"The file browser is used to visually browse the "
|
|
|
|
"directory structure to select a file for reading "
|
|
|
|
"or writing. You may use the arrow keys or Page Up/"
|
|
|
|
"Down to browse through the files, and S or Enter to "
|
|
|
|
"choose the selected file or enter the selected "
|
|
|
|
"directory. To move up one level, select the directory "
|
|
|
|
"called \"..\" at the top of the file list.\n\n The "
|
|
|
|
"following function keys are available in the file "
|
|
|
|
"browser:\n\n");
|
|
|
|
else if (currshortcut == gotodir_list)
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = N_("Browser Go To Directory Help Text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"Enter the name of the directory you would like to "
|
|
|
|
"browse to.\n\n If tab completion has not been disabled, "
|
2004-10-18 01:51:43 +00:00
|
|
|
"you can use the Tab key to (attempt to) automatically "
|
2002-09-06 20:35:28 +00:00
|
|
|
"complete the directory name.\n\n The following function "
|
|
|
|
"keys are available in Browser Go To Directory mode:\n\n");
|
|
|
|
#endif
|
2003-02-05 02:51:19 +00:00
|
|
|
#ifndef DISABLE_SPELLER
|
2002-09-06 20:35:28 +00:00
|
|
|
else if (currshortcut == spell_list)
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = N_("Spell Check Help Text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"The spell checker checks the spelling of all text "
|
|
|
|
"in the current file. When an unknown word is "
|
|
|
|
"encountered, it is highlighted and a replacement can "
|
|
|
|
"be edited. It will then prompt to replace every "
|
|
|
|
"instance of the given misspelled word in the "
|
2004-10-18 01:51:43 +00:00
|
|
|
"current file, or, if you have selected text with the "
|
|
|
|
"mark, in the selected text.\n\n The following other "
|
|
|
|
"functions are available in Spell Check mode:\n\n");
|
2003-02-05 02:51:19 +00:00
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
else if (currshortcut == extcmd_list)
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = N_("External Command Help Text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"This menu allows you to insert the output of a command "
|
|
|
|
"run by the shell into the current buffer (or a new "
|
2004-11-07 21:30:55 +00:00
|
|
|
"buffer in multibuffer mode). If you need another blank "
|
|
|
|
"buffer, do not enter any command.\n\n The following keys "
|
|
|
|
"are available in this mode:\n\n");
|
2002-09-06 20:35:28 +00:00
|
|
|
#endif
|
2004-08-05 15:45:09 +00:00
|
|
|
else
|
|
|
|
/* Default to the main help list. */
|
|
|
|
htx = N_(" nano help text\n\n "
|
2002-09-06 20:35:28 +00:00
|
|
|
"The nano editor is designed to emulate the functionality and "
|
|
|
|
"ease-of-use of the UW Pico text editor. There are four main "
|
|
|
|
"sections of the editor: The top line shows the program "
|
|
|
|
"version, the current filename being edited, and whether "
|
|
|
|
"or not the file has been modified. Next is the main editor "
|
|
|
|
"window showing the file being edited. The status line is "
|
|
|
|
"the third line from the bottom and shows important messages. "
|
|
|
|
"The bottom two lines show the most commonly used shortcuts "
|
|
|
|
"in the editor.\n\n "
|
|
|
|
"The notation for shortcuts is as follows: Control-key "
|
2003-08-31 16:44:10 +00:00
|
|
|
"sequences are notated with a caret (^) symbol and can be "
|
|
|
|
"entered either by using the Control (Ctrl) key or pressing the "
|
|
|
|
"Esc key twice. Escape-key sequences are notated with the Meta "
|
|
|
|
"(M) symbol and can be entered using either the Esc, Alt or "
|
|
|
|
"Meta key depending on your keyboard setup. Also, pressing Esc "
|
|
|
|
"twice and then typing a three-digit number from 000 to 255 "
|
|
|
|
"will enter the character with the corresponding ASCII code. "
|
|
|
|
"The following keystrokes are available in the main editor "
|
|
|
|
"window. Alternative keys are shown in parentheses:\n\n");
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-08-05 15:45:09 +00:00
|
|
|
htx = _(htx);
|
|
|
|
|
|
|
|
allocsize += strlen(htx);
|
2002-09-06 20:35:28 +00:00
|
|
|
|
|
|
|
/* The space needed for the shortcut lists, at most COLS characters,
|
|
|
|
* plus '\n'. */
|
2004-11-11 21:50:01 +00:00
|
|
|
allocsize += (COLS < 21 ? 21 : COLS + 1) *
|
|
|
|
length_of_list(currshortcut);
|
2002-09-06 20:35:28 +00:00
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
2004-08-05 15:45:09 +00:00
|
|
|
/* If we're on the main list, we also count the toggle help text.
|
2004-08-05 22:10:22 +00:00
|
|
|
* Each line has "M-%c\t\t\t", which fills 24 columns, plus a space,
|
|
|
|
* plus translated text, plus '\n'. */
|
2003-02-10 02:32:58 +00:00
|
|
|
if (currshortcut == main_list) {
|
|
|
|
size_t endislen = strlen(_("enable/disable"));
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
for (t = toggles; t != NULL; t = t->next)
|
2003-02-10 02:32:58 +00:00
|
|
|
allocsize += 8 + strlen(t->desc) + endislen;
|
|
|
|
}
|
2004-11-11 21:50:01 +00:00
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
|
|
|
|
/* help_text has been freed and set to NULL unless the user resized
|
|
|
|
* while in the help screen. */
|
|
|
|
free(help_text);
|
|
|
|
|
2004-08-05 15:45:09 +00:00
|
|
|
/* Allocate space for the help text. */
|
2003-01-15 11:18:58 +00:00
|
|
|
help_text = charalloc(allocsize);
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-08-05 15:45:09 +00:00
|
|
|
/* Now add the text we want. */
|
|
|
|
strcpy(help_text, htx);
|
2002-09-06 20:35:28 +00:00
|
|
|
ptr = help_text + strlen(help_text);
|
|
|
|
|
2004-09-11 21:41:13 +00:00
|
|
|
/* Now add our shortcut info. Assume that each shortcut has, at the
|
|
|
|
* very least, an equivalent control key, an equivalent primary meta
|
|
|
|
* key sequence, or both. Also assume that the meta key values are
|
|
|
|
* not control characters. We can display a maximum of 3 shortcut
|
|
|
|
* entries. */
|
2002-09-06 20:35:28 +00:00
|
|
|
for (s = currshortcut; s != NULL; s = s->next) {
|
2004-09-11 21:41:13 +00:00
|
|
|
int entries = 0;
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-09-11 21:41:13 +00:00
|
|
|
/* Control key. */
|
2004-03-19 21:46:34 +00:00
|
|
|
if (s->ctrlval != NANO_NO_KEY) {
|
2004-09-11 21:41:13 +00:00
|
|
|
entries++;
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-03-19 21:46:34 +00:00
|
|
|
if (s->ctrlval == NANO_HISTORY_KEY)
|
2004-08-05 22:10:22 +00:00
|
|
|
ptr += sprintf(ptr, "%.7s", _("Up"));
|
2003-12-24 03:33:09 +00:00
|
|
|
else
|
2003-11-28 19:47:42 +00:00
|
|
|
#endif
|
2004-03-19 21:46:34 +00:00
|
|
|
if (s->ctrlval == NANO_CONTROL_SPACE)
|
2004-08-05 22:10:22 +00:00
|
|
|
ptr += sprintf(ptr, "^%.6s", _("Space"));
|
2004-03-19 21:46:34 +00:00
|
|
|
else if (s->ctrlval == NANO_CONTROL_8)
|
2003-11-28 19:47:42 +00:00
|
|
|
ptr += sprintf(ptr, "^?");
|
|
|
|
else
|
2004-03-19 21:46:34 +00:00
|
|
|
ptr += sprintf(ptr, "^%c", s->ctrlval + 64);
|
2004-09-11 21:41:13 +00:00
|
|
|
*(ptr++) = '\t';
|
2003-11-28 19:47:42 +00:00
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-09-11 21:41:13 +00:00
|
|
|
/* Function key. */
|
|
|
|
if (s->funcval != NANO_NO_KEY) {
|
|
|
|
entries++;
|
2004-11-01 22:35:26 +00:00
|
|
|
/* If this is the first entry, put it in the middle. */
|
|
|
|
if (entries == 1) {
|
|
|
|
entries++;
|
|
|
|
*(ptr++) = '\t';
|
|
|
|
}
|
2004-03-19 21:46:34 +00:00
|
|
|
ptr += sprintf(ptr, "(F%d)", s->funcval - KEY_F0);
|
2004-09-11 21:41:13 +00:00
|
|
|
*(ptr++) = '\t';
|
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-09-11 21:41:13 +00:00
|
|
|
/* Primary meta key sequence. */
|
|
|
|
if (s->metaval != NANO_NO_KEY) {
|
|
|
|
entries++;
|
|
|
|
/* If this is the last entry, put it at the end. */
|
|
|
|
if (entries == 2 && s->miscval == NANO_NO_KEY) {
|
|
|
|
entries++;
|
|
|
|
*(ptr++) = '\t';
|
|
|
|
}
|
|
|
|
/* If the primary meta key sequence is the first entry,
|
|
|
|
* don't put parentheses around it. */
|
|
|
|
if (entries == 1 && s->metaval == NANO_ALT_SPACE)
|
|
|
|
ptr += sprintf(ptr, "M-%.5s", _("Space"));
|
|
|
|
else
|
|
|
|
ptr += sprintf(ptr, entries == 1 ? "M-%c" : "(M-%c)",
|
|
|
|
toupper(s->metaval));
|
|
|
|
*(ptr++) = '\t';
|
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-09-11 21:41:13 +00:00
|
|
|
/* Miscellaneous meta key sequence. */
|
|
|
|
if (entries < 3 && s->miscval != NANO_NO_KEY) {
|
|
|
|
entries++;
|
|
|
|
/* If this is the last entry, put it at the end. */
|
|
|
|
if (entries == 2) {
|
|
|
|
entries++;
|
|
|
|
*(ptr++) = '\t';
|
|
|
|
}
|
2004-03-19 21:46:34 +00:00
|
|
|
ptr += sprintf(ptr, "(M-%c)", toupper(s->miscval));
|
2004-09-11 21:41:13 +00:00
|
|
|
*(ptr++) = '\t';
|
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-09-11 21:41:13 +00:00
|
|
|
/* Make sure all the help text starts at the same place. */
|
|
|
|
while (entries < 3) {
|
|
|
|
entries++;
|
|
|
|
*(ptr++) = '\t';
|
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
|
|
|
|
assert(s->help != NULL);
|
2003-02-10 02:32:58 +00:00
|
|
|
ptr += sprintf(ptr, "%.*s\n", COLS > 24 ? COLS - 24 : 0, s->help);
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
/* And the toggles... */
|
2004-03-19 21:46:34 +00:00
|
|
|
if (currshortcut == main_list) {
|
2002-09-06 20:35:28 +00:00
|
|
|
for (t = toggles; t != NULL; t = t->next) {
|
|
|
|
assert(t->desc != NULL);
|
2004-10-21 22:49:51 +00:00
|
|
|
ptr += sprintf(ptr, "M-%c\t\t\t%s %s\n", toupper(t->val),
|
|
|
|
t->desc, _("enable/disable"));
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2004-03-19 21:46:34 +00:00
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
#endif /* !NANO_SMALL */
|
|
|
|
|
|
|
|
/* If all went well, we didn't overwrite the allocated space for
|
2004-08-05 15:45:09 +00:00
|
|
|
* help_text. */
|
2003-01-15 11:18:58 +00:00
|
|
|
assert(strlen(help_text) < allocsize);
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create a new filestruct node. Note that we specifically do not set
|
|
|
|
* prevnode->next equal to the new line. */
|
|
|
|
filestruct *make_new_node(filestruct *prevnode)
|
|
|
|
{
|
|
|
|
filestruct *newnode = (filestruct *)nmalloc(sizeof(filestruct));
|
|
|
|
newnode->data = NULL;
|
|
|
|
newnode->prev = prevnode;
|
|
|
|
newnode->next = NULL;
|
2004-11-25 04:39:07 +00:00
|
|
|
newnode->lineno = (prevnode != NULL) ? prevnode->lineno + 1 : 1;
|
2002-09-06 20:35:28 +00:00
|
|
|
return newnode;
|
|
|
|
}
|
|
|
|
|
2004-11-25 04:39:07 +00:00
|
|
|
/* Make a copy of a filestruct node. */
|
2002-07-19 01:08:59 +00:00
|
|
|
filestruct *copy_node(const filestruct *src)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
filestruct *dst = (filestruct *)nmalloc(sizeof(filestruct));
|
|
|
|
assert(src != NULL);
|
2004-11-25 04:39:07 +00:00
|
|
|
dst->data = mallocstrcpy(NULL, src->data);
|
2000-06-06 05:53:49 +00:00
|
|
|
dst->next = src->next;
|
|
|
|
dst->prev = src->prev;
|
|
|
|
dst->lineno = src->lineno;
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Splice a node into an existing filestruct. */
|
2004-07-08 17:15:10 +00:00
|
|
|
void splice_node(filestruct *begin, filestruct *newnode, filestruct
|
|
|
|
*end)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2004-11-25 04:39:07 +00:00
|
|
|
assert(newnode != NULL && begin != NULL);
|
|
|
|
newnode->next = end;
|
|
|
|
newnode->prev = begin;
|
|
|
|
begin->next = newnode;
|
2002-09-06 20:35:28 +00:00
|
|
|
if (end != NULL)
|
|
|
|
end->prev = newnode;
|
|
|
|
}
|
|
|
|
|
2002-04-10 02:31:20 +00:00
|
|
|
/* Unlink a node from the rest of the filestruct. */
|
2002-07-19 01:08:59 +00:00
|
|
|
void unlink_node(const filestruct *fileptr)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
assert(fileptr != NULL);
|
2002-04-10 02:31:20 +00:00
|
|
|
if (fileptr->prev != NULL)
|
|
|
|
fileptr->prev->next = fileptr->next;
|
|
|
|
if (fileptr->next != NULL)
|
|
|
|
fileptr->next->prev = fileptr->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a node from the filestruct. */
|
2002-07-19 01:08:59 +00:00
|
|
|
void delete_node(filestruct *fileptr)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2004-11-25 04:39:07 +00:00
|
|
|
assert(fileptr != NULL && fileptr->data != NULL);
|
|
|
|
if (fileptr->data != NULL)
|
|
|
|
free(fileptr->data);
|
|
|
|
free(fileptr);
|
2002-04-10 02:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Okay, now let's duplicate a whole struct! */
|
2002-07-19 01:08:59 +00:00
|
|
|
filestruct *copy_filestruct(const filestruct *src)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
filestruct *head; /* copy of src, top of the copied list */
|
|
|
|
filestruct *prev; /* temp that traverses the list */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
assert(src != NULL);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
prev = copy_node(src);
|
|
|
|
prev->prev = NULL;
|
|
|
|
head = prev;
|
|
|
|
src = src->next;
|
|
|
|
while (src != NULL) {
|
|
|
|
prev->next = copy_node(src);
|
|
|
|
prev->next->prev = prev;
|
|
|
|
prev = prev->next;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
src = src->next;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
prev->next = NULL;
|
2000-06-06 05:53:49 +00:00
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2002-04-10 02:31:20 +00:00
|
|
|
/* Frees a filestruct. */
|
2002-07-19 01:08:59 +00:00
|
|
|
void free_filestruct(filestruct *src)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2004-11-25 04:39:07 +00:00
|
|
|
assert(src != NULL);
|
|
|
|
|
|
|
|
while (src->next != NULL) {
|
|
|
|
src = src->next;
|
|
|
|
delete_node(src->prev);
|
2002-04-10 02:31:20 +00:00
|
|
|
}
|
2004-11-25 04:39:07 +00:00
|
|
|
delete_node(src);
|
2002-04-10 02:31:20 +00:00
|
|
|
}
|
|
|
|
|
2004-11-03 22:03:41 +00:00
|
|
|
/* Partition a filestruct so it begins at (top, top_x) and ends at (bot,
|
|
|
|
* bot_x). */
|
|
|
|
partition *partition_filestruct(filestruct *top, size_t top_x,
|
|
|
|
filestruct *bot, size_t bot_x)
|
|
|
|
{
|
|
|
|
partition *p;
|
2004-11-08 03:22:23 +00:00
|
|
|
assert(top != NULL && bot != NULL && fileage != NULL && filebot != NULL);
|
2004-11-03 22:03:41 +00:00
|
|
|
|
|
|
|
/* Initialize the partition. */
|
|
|
|
p = (partition *)nmalloc(sizeof(partition));
|
|
|
|
|
2004-11-04 16:45:48 +00:00
|
|
|
/* If the top and bottom of the partition are different from the top
|
|
|
|
* and bottom of the filestruct, save the latter and then set them
|
|
|
|
* to top and bot. */
|
|
|
|
if (top != fileage) {
|
|
|
|
p->fileage = fileage;
|
|
|
|
fileage = top;
|
|
|
|
} else
|
|
|
|
p->fileage = NULL;
|
|
|
|
if (bot != filebot) {
|
|
|
|
p->filebot = filebot;
|
|
|
|
filebot = bot;
|
|
|
|
} else
|
|
|
|
p->filebot = NULL;
|
2004-11-03 22:03:41 +00:00
|
|
|
|
|
|
|
/* Save the line above the top of the partition, detach the top of
|
|
|
|
* the partition from it, and save the text before top_x in
|
|
|
|
* top_data. */
|
|
|
|
p->top_prev = top->prev;
|
|
|
|
top->prev = NULL;
|
|
|
|
p->top_data = mallocstrncpy(NULL, top->data, top_x + 1);
|
|
|
|
p->top_data[top_x] = '\0';
|
|
|
|
|
|
|
|
/* Save the line below the bottom of the partition, detach the
|
|
|
|
* bottom of the partition from it, and save the text after bot_x in
|
|
|
|
* bot_data. */
|
|
|
|
p->bot_next = bot->next;
|
|
|
|
bot->next = NULL;
|
|
|
|
p->bot_data = mallocstrcpy(NULL, bot->data + bot_x);
|
|
|
|
|
|
|
|
/* Remove all text after bot_x at the bottom of the partition. */
|
|
|
|
null_at(&bot->data, bot_x);
|
|
|
|
|
|
|
|
/* Remove all text before top_x at the top of the partition. */
|
|
|
|
charmove(top->data, top->data + top_x, strlen(top->data) -
|
|
|
|
top_x + 1);
|
|
|
|
align(&top->data);
|
|
|
|
|
|
|
|
/* Return the partition. */
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unpartition a filestruct so it begins at (fileage, 0) and ends at
|
|
|
|
* (filebot, strlen(filebot)) again. */
|
2004-11-22 00:16:23 +00:00
|
|
|
void unpartition_filestruct(partition **p)
|
2004-11-03 22:03:41 +00:00
|
|
|
{
|
|
|
|
char *tmp;
|
2004-11-08 03:19:10 +00:00
|
|
|
assert(p != NULL && fileage != NULL && filebot != NULL);
|
2004-11-03 22:03:41 +00:00
|
|
|
|
|
|
|
/* Reattach the line above the top of the partition, and restore the
|
|
|
|
* text before top_x from top_data. Free top_data when we're done
|
|
|
|
* with it. */
|
|
|
|
tmp = mallocstrcpy(NULL, fileage->data);
|
2004-11-22 00:16:23 +00:00
|
|
|
fileage->prev = (*p)->top_prev;
|
2004-11-04 15:31:43 +00:00
|
|
|
if (fileage->prev != NULL)
|
|
|
|
fileage->prev->next = fileage;
|
2004-11-22 00:16:23 +00:00
|
|
|
fileage->data = charealloc(fileage->data, strlen((*p)->top_data) +
|
2004-11-03 22:03:41 +00:00
|
|
|
strlen(fileage->data) + 1);
|
2004-11-22 00:16:23 +00:00
|
|
|
strcpy(fileage->data, (*p)->top_data);
|
|
|
|
free((*p)->top_data);
|
2004-11-03 22:03:41 +00:00
|
|
|
strcat(fileage->data, tmp);
|
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
/* Reattach the line below the bottom of the partition, and restore
|
|
|
|
* the text after bot_x from bot_data. Free bot_data when we're
|
|
|
|
* done with it. */
|
2004-11-22 00:16:23 +00:00
|
|
|
filebot->next = (*p)->bot_next;
|
2004-11-04 15:31:43 +00:00
|
|
|
if (filebot->next != NULL)
|
|
|
|
filebot->next->prev = filebot;
|
2004-11-03 22:03:41 +00:00
|
|
|
filebot->data = charealloc(filebot->data, strlen(filebot->data) +
|
2004-11-22 00:16:23 +00:00
|
|
|
strlen((*p)->bot_data) + 1);
|
|
|
|
strcat(filebot->data, (*p)->bot_data);
|
|
|
|
free((*p)->bot_data);
|
2004-11-03 22:03:41 +00:00
|
|
|
|
2004-11-04 16:45:48 +00:00
|
|
|
/* Restore the top and bottom of the filestruct, if they were
|
|
|
|
* different from the top and bottom of the partition. */
|
2004-11-22 00:16:23 +00:00
|
|
|
if ((*p)->fileage != NULL)
|
|
|
|
fileage = (*p)->fileage;
|
|
|
|
if ((*p)->filebot != NULL)
|
|
|
|
filebot = (*p)->filebot;
|
2004-11-03 22:03:41 +00:00
|
|
|
|
|
|
|
/* Uninitialize the partition. */
|
2004-11-22 00:16:23 +00:00
|
|
|
free(*p);
|
|
|
|
*p = NULL;
|
2004-11-03 22:03:41 +00:00
|
|
|
}
|
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Move all the text between (top, top_x) and (bot, bot_x) in the
|
|
|
|
* current filestruct to a filestruct beginning with file_top and ending
|
|
|
|
* with file_bot. If no text is between (top, top_x) and (bot, bot_x),
|
|
|
|
* don't do anything. */
|
|
|
|
void move_to_filestruct(filestruct **file_top, filestruct **file_bot,
|
|
|
|
filestruct *top, size_t top_x, filestruct *bot, size_t bot_x)
|
|
|
|
{
|
|
|
|
filestruct *top_save;
|
|
|
|
long part_totsize;
|
|
|
|
bool at_edittop;
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
bool mark_inside = FALSE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(file_top != NULL && file_bot != NULL && top != NULL && bot != NULL);
|
|
|
|
|
|
|
|
/* If (top, top_x)-(bot, bot_x) doesn't cover any text, get out. */
|
|
|
|
if (top == bot && top_x == bot_x)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Partition the filestruct so that it contains only the text from
|
|
|
|
* (top, top_x) to (bot, bot_x), keep track of whether the top of
|
|
|
|
* the partition is the top of the edit window, and keep track of
|
|
|
|
* whether the mark begins inside the partition. */
|
|
|
|
filepart = partition_filestruct(top, top_x, bot, bot_x);
|
|
|
|
at_edittop = (fileage == edittop);
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (ISSET(MARK_ISSET))
|
|
|
|
mark_inside = (mark_beginbuf->lineno >= fileage->lineno &&
|
|
|
|
mark_beginbuf->lineno <= filebot->lineno &&
|
|
|
|
(mark_beginbuf != fileage || mark_beginx >= top_x) &&
|
|
|
|
(mark_beginbuf != filebot || mark_beginx <= bot_x));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get the number of characters in the text, and subtract it from
|
|
|
|
* totsize. */
|
|
|
|
get_totals(top, bot, NULL, &part_totsize);
|
|
|
|
totsize -= part_totsize;
|
|
|
|
|
|
|
|
if (*file_top == NULL) {
|
|
|
|
/* If file_top is empty, just move all the text directly into
|
|
|
|
* it. This is equivalent to tacking the text in top onto the
|
|
|
|
* (lack of) text at the end of file_top. */
|
|
|
|
*file_top = fileage;
|
|
|
|
*file_bot = filebot;
|
|
|
|
} else {
|
|
|
|
/* Otherwise, tack the text in top onto the text at the end of
|
|
|
|
* file_bot. */
|
|
|
|
(*file_bot)->data = charealloc((*file_bot)->data,
|
|
|
|
strlen((*file_bot)->data) + strlen(fileage->data) + 1);
|
|
|
|
strcat((*file_bot)->data, fileage->data);
|
|
|
|
|
|
|
|
/* Attach the line after top to the line after file_bot. Then,
|
|
|
|
* if there's more than one line after top, move file_bot down
|
|
|
|
* to bot. */
|
|
|
|
(*file_bot)->next = fileage->next;
|
|
|
|
if ((*file_bot)->next != NULL) {
|
|
|
|
(*file_bot)->next->prev = *file_bot;
|
|
|
|
*file_bot = filebot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Since the text has now been saved, remove it from the filestruct.
|
|
|
|
* If the top of the partition was the top of the edit window, set
|
|
|
|
* edittop to where the text used to start. If the mark began
|
|
|
|
* inside the partition, set the beginning of the mark to where the
|
|
|
|
* text used to start. */
|
|
|
|
fileage = (filestruct *)nmalloc(sizeof(filestruct));
|
|
|
|
fileage->data = mallocstrcpy(NULL, "");
|
|
|
|
filebot = fileage;
|
|
|
|
if (at_edittop)
|
|
|
|
edittop = fileage;
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (mark_inside) {
|
|
|
|
mark_beginbuf = fileage;
|
|
|
|
mark_beginx = top_x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Restore the current line and cursor position. */
|
|
|
|
current = fileage;
|
|
|
|
current_x = top_x;
|
|
|
|
|
|
|
|
top_save = fileage;
|
|
|
|
|
|
|
|
/* Unpartition the filestruct so that it contains all the text
|
|
|
|
* again, minus the saved text. */
|
|
|
|
unpartition_filestruct(&filepart);
|
|
|
|
|
|
|
|
/* Renumber starting with the beginning line of the old
|
|
|
|
* partition. */
|
|
|
|
renumber(top_save);
|
|
|
|
|
|
|
|
if (filebot->data[0] != '\0')
|
|
|
|
new_magicline();
|
|
|
|
|
|
|
|
/* Set totlines to the new number of lines in the file. */
|
|
|
|
totlines = filebot->lineno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy all the text from the filestruct beginning with file_top and
|
|
|
|
* ending with file_bot to the current filestruct at the current cursor
|
|
|
|
* position. */
|
|
|
|
void copy_from_filestruct(filestruct *file_top, filestruct *file_bot)
|
|
|
|
{
|
|
|
|
filestruct *top_save;
|
|
|
|
int part_totlines;
|
|
|
|
long part_totsize;
|
|
|
|
bool at_edittop;
|
|
|
|
|
|
|
|
assert(file_top != NULL && file_bot != NULL);
|
|
|
|
|
|
|
|
/* Partition the filestruct so that it contains no text, and keep
|
|
|
|
* track of whether the top of the partition is the top of the edit
|
|
|
|
* window. */
|
|
|
|
filepart = partition_filestruct(current, current_x, current,
|
|
|
|
current_x);
|
|
|
|
at_edittop = (fileage == edittop);
|
|
|
|
|
|
|
|
/* Put the top and bottom of the filestruct at copies of file_top
|
|
|
|
* and file_bot. */
|
|
|
|
fileage = copy_filestruct(file_top);
|
|
|
|
filebot = fileage;
|
|
|
|
while (filebot->next != NULL)
|
|
|
|
filebot = filebot->next;
|
|
|
|
|
|
|
|
/* Restore the current line and cursor position. */
|
|
|
|
current = filebot;
|
|
|
|
current_x = strlen(filebot->data);
|
|
|
|
if (fileage == filebot)
|
|
|
|
current_x += strlen(filepart->top_data);
|
|
|
|
|
|
|
|
/* Get the number of lines and the number of characters in the saved
|
|
|
|
* text, and add the latter to totsize. */
|
|
|
|
get_totals(fileage, filebot, &part_totlines, &part_totsize);
|
|
|
|
totsize += part_totsize;
|
|
|
|
|
|
|
|
/* If the top of the partition was the top of the edit window, set
|
|
|
|
* edittop to where the saved text now starts, and update the
|
|
|
|
* current y-coordinate to account for the number of lines it
|
|
|
|
* has, less one since the first line will be tacked onto the
|
|
|
|
* current line. */
|
|
|
|
if (at_edittop)
|
|
|
|
edittop = fileage;
|
|
|
|
current_y += part_totlines - 1;
|
|
|
|
|
|
|
|
top_save = fileage;
|
|
|
|
|
|
|
|
/* Unpartition the filestruct so that it contains all the text
|
|
|
|
* again, minus the saved text. */
|
|
|
|
unpartition_filestruct(&filepart);
|
|
|
|
|
|
|
|
/* Renumber starting with the beginning line of the old
|
|
|
|
* partition. */
|
|
|
|
renumber(top_save);
|
|
|
|
|
|
|
|
if (filebot->data[0] != '\0')
|
|
|
|
new_magicline();
|
|
|
|
|
|
|
|
/* Set totlines to the new number of lines in the file. */
|
|
|
|
totlines = filebot->lineno;
|
|
|
|
}
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
void renumber_all(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
|
|
|
filestruct *temp;
|
2002-03-06 03:30:40 +00:00
|
|
|
int i = 1;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
assert(fileage == NULL || fileage != fileage->next);
|
|
|
|
for (temp = fileage; temp != NULL; temp = temp->next)
|
2000-06-06 05:53:49 +00:00
|
|
|
temp->lineno = i++;
|
|
|
|
}
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
void renumber(filestruct *fileptr)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
if (fileptr == NULL || fileptr->prev == NULL || fileptr == fileage)
|
2000-06-06 05:53:49 +00:00
|
|
|
renumber_all();
|
2002-07-19 01:08:59 +00:00
|
|
|
else {
|
|
|
|
int lineno = fileptr->prev->lineno;
|
2001-10-22 23:22:19 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
assert(fileptr != fileptr->next);
|
|
|
|
for (; fileptr != NULL; fileptr = fileptr->next)
|
|
|
|
fileptr->lineno = ++lineno;
|
|
|
|
}
|
2000-07-28 01:41:29 +00:00
|
|
|
}
|
|
|
|
|
2004-08-05 15:16:19 +00:00
|
|
|
/* Print one usage string to the screen. This cuts down on duplicate
|
|
|
|
* strings to translate and leaves out the parts that shouldn't be
|
2002-07-19 01:08:59 +00:00
|
|
|
* translatable (the flag names). */
|
2004-07-08 17:15:10 +00:00
|
|
|
void print1opt(const char *shortflag, const char *longflag, const char
|
|
|
|
*desc)
|
2002-03-09 18:51:58 +00:00
|
|
|
{
|
|
|
|
printf(" %s\t", shortflag);
|
|
|
|
if (strlen(shortflag) < 8)
|
|
|
|
printf("\t");
|
|
|
|
|
|
|
|
#ifdef HAVE_GETOPT_LONG
|
|
|
|
printf("%s\t", longflag);
|
|
|
|
if (strlen(longflag) < 8)
|
|
|
|
printf("\t\t");
|
|
|
|
else if (strlen(longflag) < 16)
|
|
|
|
printf("\t");
|
|
|
|
#endif
|
|
|
|
|
2004-08-05 15:45:09 +00:00
|
|
|
printf("%s\n", _(desc));
|
2002-03-09 18:51:58 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 16:10:37 +00:00
|
|
|
void usage(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_GETOPT_LONG
|
2002-07-19 01:08:59 +00:00
|
|
|
printf(_("Usage: nano [+LINE] [GNU long option] [option] [file]\n\n"));
|
|
|
|
printf(_("Option\t\tLong option\t\tMeaning\n"));
|
2000-06-06 05:53:49 +00:00
|
|
|
#else
|
2002-07-19 01:08:59 +00:00
|
|
|
printf(_("Usage: nano [+LINE] [option] [file]\n\n"));
|
|
|
|
printf(_("Option\t\tMeaning\n"));
|
2004-08-07 22:00:02 +00:00
|
|
|
#endif
|
2002-03-09 18:51:58 +00:00
|
|
|
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-h, -?", "--help", N_("Show this message"));
|
|
|
|
print1opt(_("+LINE"), "", N_("Start at line number LINE"));
|
2001-09-22 00:42:10 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-A", "--smarthome", N_("Enable smart home key"));
|
|
|
|
print1opt("-B", "--backup", N_("Backup existing files on save"));
|
|
|
|
print1opt(_("-E [dir]"), _("--backupdir=[dir]"), N_("Directory for writing backup files"));
|
2001-09-22 00:42:10 +00:00
|
|
|
#endif
|
2001-07-14 19:32:47 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-F", "--multibuffer", N_("Enable multiple file buffers"));
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_NANORC
|
2003-09-06 21:44:37 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-H", "--historylog", N_("Log & read search/replace string history"));
|
2003-09-06 21:44:37 +00:00
|
|
|
#endif
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-I", "--ignorercfiles", N_("Don't look at nanorc files"));
|
2001-09-22 04:20:25 +00:00
|
|
|
#endif
|
|
|
|
#ifndef NANO_SMALL
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-N", "--noconvert", N_("Don't convert files from DOS/Mac format"));
|
2002-03-03 22:36:36 +00:00
|
|
|
#endif
|
2004-12-04 17:41:52 +00:00
|
|
|
#ifdef NANO_WIDE
|
|
|
|
print1opt("-O", "--noutf8", N_("Don't convert files from UTF-8 format"));
|
|
|
|
#endif
|
2002-03-03 22:36:36 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt(_("-Q [str]"), _("--quotestr=[str]"), N_("Quoting string, default \"> \""));
|
2001-07-14 19:32:47 +00:00
|
|
|
#endif
|
2002-06-28 22:45:14 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-R", "--regexp", N_("Do regular expression searches"));
|
2002-06-28 22:45:14 +00:00
|
|
|
#endif
|
2001-09-22 19:02:04 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-S", "--smooth", N_("Smooth scrolling"));
|
2001-09-22 19:02:04 +00:00
|
|
|
#endif
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt(_("-T [#cols]"), _("--tabsize=[#cols]"), N_("Set width of a tab in cols to #cols"));
|
|
|
|
print1opt("-V", "--version", N_("Print version information and exit"));
|
2002-05-04 04:23:30 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt(_("-Y [str]"), _("--syntax [str]"), N_("Syntax definition to use"));
|
2002-05-04 04:23:30 +00:00
|
|
|
#endif
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-Z", "--restricted", N_("Restricted mode"));
|
|
|
|
print1opt("-c", "--const", N_("Constantly show cursor position"));
|
2000-07-12 18:14:51 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-d", "--rebinddelete", N_("Fix Backspace/Delete confusion problem"));
|
|
|
|
print1opt("-i", "--autoindent", N_("Automatically indent new lines"));
|
|
|
|
print1opt("-k", "--cut", N_("Cut from cursor to end of line"));
|
2000-07-12 18:14:51 +00:00
|
|
|
#endif
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-l", "--nofollow", N_("Don't follow symbolic links, overwrite"));
|
2003-10-03 20:26:25 +00:00
|
|
|
#ifndef DISABLE_MOUSE
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-m", "--mouse", N_("Enable mouse"));
|
2000-06-06 05:53:49 +00:00
|
|
|
#endif
|
2001-09-19 03:19:43 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt(_("-o [dir]"), _("--operatingdir=[dir]"), N_("Set operating directory"));
|
2000-06-06 05:53:49 +00:00
|
|
|
#endif
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-p", "--preserve", N_("Preserve XON (^Q) and XOFF (^S) keys"));
|
2001-05-21 12:56:25 +00:00
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt(_("-r [#cols]"), _("--fill=[#cols]"), N_("Set fill cols to (wrap lines at) #cols"));
|
2001-05-21 12:56:25 +00:00
|
|
|
#endif
|
2001-01-12 07:51:05 +00:00
|
|
|
#ifndef DISABLE_SPELLER
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt(_("-s [prog]"), _("--speller=[prog]"), N_("Enable alternate speller"));
|
2001-01-12 07:51:05 +00:00
|
|
|
#endif
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-t", "--tempfile", N_("Auto save on exit, don't prompt"));
|
|
|
|
print1opt("-v", "--view", N_("View (read only) mode"));
|
2001-04-02 05:36:08 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-w", "--nowrap", N_("Don't wrap long lines"));
|
2000-06-06 05:53:49 +00:00
|
|
|
#endif
|
2004-08-05 15:45:09 +00:00
|
|
|
print1opt("-x", "--nohelp", N_("Don't show help window"));
|
|
|
|
print1opt("-z", "--suspend", N_("Enable suspend"));
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2004-08-05 15:16:19 +00:00
|
|
|
/* This is a special case. */
|
2002-07-19 01:08:59 +00:00
|
|
|
printf(" %s\t\t\t%s\n","-a, -b, -e, -f, -g, -j", _("(ignored, for Pico compatibility)"));
|
2002-03-09 18:51:58 +00:00
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2002-08-21 16:10:37 +00:00
|
|
|
void version(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2001-02-14 14:28:27 +00:00
|
|
|
printf(_(" GNU nano version %s (compiled %s, %s)\n"),
|
2000-06-06 05:53:49 +00:00
|
|
|
VERSION, __TIME__, __DATE__);
|
2000-10-26 01:44:42 +00:00
|
|
|
printf(_
|
2002-11-04 16:05:42 +00:00
|
|
|
(" Email: nano@nano-editor.org Web: http://www.nano-editor.org/"));
|
2000-11-24 20:45:14 +00:00
|
|
|
printf(_("\n Compiled options:"));
|
2000-12-01 18:46:01 +00:00
|
|
|
|
2003-01-17 03:39:41 +00:00
|
|
|
#ifndef ENABLE_NLS
|
|
|
|
printf(" --disable-nls");
|
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" --enable-debug");
|
|
|
|
#endif
|
2000-11-24 20:45:14 +00:00
|
|
|
#ifdef NANO_EXTRA
|
|
|
|
printf(" --enable-extra");
|
2002-01-17 12:40:33 +00:00
|
|
|
#endif
|
2001-01-12 07:51:05 +00:00
|
|
|
#ifdef NANO_SMALL
|
|
|
|
printf(" --enable-tiny");
|
|
|
|
#else
|
2001-04-12 03:01:53 +00:00
|
|
|
#ifdef DISABLE_BROWSER
|
2001-01-05 05:41:07 +00:00
|
|
|
printf(" --disable-browser");
|
2001-04-12 03:01:53 +00:00
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifdef DISABLE_HELP
|
|
|
|
printf(" --disable-help");
|
2001-04-12 03:01:53 +00:00
|
|
|
#endif
|
|
|
|
#ifdef DISABLE_JUSTIFY
|
2000-12-01 18:46:01 +00:00
|
|
|
printf(" --disable-justify");
|
2001-04-12 03:01:53 +00:00
|
|
|
#endif
|
2003-10-03 20:26:25 +00:00
|
|
|
#ifdef DISABLE_MOUSE
|
2001-04-12 14:51:48 +00:00
|
|
|
printf(" --disable-mouse");
|
2000-12-18 05:36:51 +00:00
|
|
|
#endif
|
2001-09-19 03:19:43 +00:00
|
|
|
#ifdef DISABLE_OPERATINGDIR
|
|
|
|
printf(" --disable-operatingdir");
|
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifdef DISABLE_SPELLER
|
|
|
|
printf(" --disable-speller");
|
|
|
|
#endif
|
|
|
|
#ifdef DISABLE_TABCOMP
|
|
|
|
printf(" --disable-tabcomp");
|
|
|
|
#endif
|
|
|
|
#endif /* NANO_SMALL */
|
|
|
|
#ifdef DISABLE_WRAPPING
|
|
|
|
printf(" --disable-wrapping");
|
|
|
|
#endif
|
2002-10-25 16:08:53 +00:00
|
|
|
#ifdef DISABLE_ROOTWRAP
|
|
|
|
printf(" --disable-wrapping-as-root");
|
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
printf(" --enable-color");
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
|
|
|
printf(" --enable-multibuffer");
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_NANORC
|
|
|
|
printf(" --enable-nanorc");
|
|
|
|
#endif
|
2000-11-24 20:45:14 +00:00
|
|
|
#ifdef USE_SLANG
|
|
|
|
printf(" --with-slang");
|
|
|
|
#endif
|
|
|
|
printf("\n");
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int no_help(void)
|
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
return ISSET(NO_HELP) ? 2 : 0;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void nano_disabled_msg(void)
|
2000-12-01 18:46:01 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
statusbar(_("Sorry, support for this function has been disabled"));
|
2000-12-01 18:46:01 +00:00
|
|
|
}
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifndef NANO_SMALL
|
2002-09-06 20:35:28 +00:00
|
|
|
RETSIGTYPE cancel_fork(int signal)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2002-12-22 16:30:00 +00:00
|
|
|
if (kill(pid, SIGKILL) == -1)
|
|
|
|
nperror("kill");
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-05 22:10:22 +00:00
|
|
|
/* Return TRUE on success. */
|
|
|
|
bool open_pipe(const char *command)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2002-09-06 20:35:28 +00:00
|
|
|
int fd[2];
|
|
|
|
FILE *f;
|
|
|
|
struct sigaction oldaction, newaction;
|
2004-08-05 15:16:19 +00:00
|
|
|
/* Original and temporary handlers for
|
|
|
|
* SIGINT. */
|
2004-08-05 22:10:22 +00:00
|
|
|
bool sig_failed = FALSE;
|
|
|
|
/* sig_failed means that sigaction() failed without changing the
|
|
|
|
* signal handlers.
|
2002-11-04 16:05:42 +00:00
|
|
|
*
|
2004-08-05 22:10:22 +00:00
|
|
|
* We use this variable since it is important to put things back
|
|
|
|
* when we finish, even if we get errors. */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Make our pipes. */
|
2000-12-01 23:49:48 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
if (pipe(fd) == -1) {
|
|
|
|
statusbar(_("Could not pipe"));
|
2004-08-05 22:10:22 +00:00
|
|
|
return FALSE;
|
2000-12-01 23:49:48 +00:00
|
|
|
}
|
2001-10-14 19:05:10 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Fork a child. */
|
2001-10-14 19:05:10 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
if ((pid = fork()) == 0) {
|
|
|
|
close(fd[0]);
|
|
|
|
dup2(fd[1], fileno(stdout));
|
|
|
|
dup2(fd[1], fileno(stderr));
|
|
|
|
/* If execl() returns at all, there was an error. */
|
2004-08-05 22:10:22 +00:00
|
|
|
|
2002-11-04 16:05:42 +00:00
|
|
|
execl("/bin/sh", "sh", "-c", command, 0);
|
2002-09-06 20:35:28 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2001-10-14 19:05:10 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Else continue as parent. */
|
|
|
|
|
|
|
|
close(fd[1]);
|
|
|
|
|
|
|
|
if (pid == -1) {
|
|
|
|
close(fd[0]);
|
|
|
|
statusbar(_("Could not fork"));
|
2004-08-05 22:10:22 +00:00
|
|
|
return FALSE;
|
2001-10-14 19:05:10 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Before we start reading the forked command's output, we set
|
|
|
|
* things up so that ^C will cancel the new process. */
|
2004-05-18 01:20:36 +00:00
|
|
|
|
2004-05-19 16:04:27 +00:00
|
|
|
/* Enable interpretation of the special control keys so that we get
|
|
|
|
* SIGINT when Ctrl-C is pressed. */
|
2004-05-18 01:20:36 +00:00
|
|
|
enable_signals();
|
|
|
|
|
2002-11-04 16:05:42 +00:00
|
|
|
if (sigaction(SIGINT, NULL, &newaction) == -1) {
|
2004-08-05 22:10:22 +00:00
|
|
|
sig_failed = TRUE;
|
2002-09-06 20:35:28 +00:00
|
|
|
nperror("sigaction");
|
2002-07-19 01:08:59 +00:00
|
|
|
} else {
|
2002-09-06 20:35:28 +00:00
|
|
|
newaction.sa_handler = cancel_fork;
|
2002-11-04 16:05:42 +00:00
|
|
|
if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
|
2004-08-05 22:10:22 +00:00
|
|
|
sig_failed = TRUE;
|
2002-09-06 20:35:28 +00:00
|
|
|
nperror("sigaction");
|
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Note that now oldaction is the previous SIGINT signal handler,
|
|
|
|
* to be restored later. */
|
2001-10-14 19:05:10 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
f = fdopen(fd[0], "rb");
|
2002-12-22 16:30:00 +00:00
|
|
|
if (f == NULL)
|
2004-08-05 22:10:22 +00:00
|
|
|
nperror("fdopen");
|
2004-08-07 21:27:37 +00:00
|
|
|
|
2004-09-05 21:40:31 +00:00
|
|
|
read_file(f, "stdin");
|
2004-08-05 15:16:19 +00:00
|
|
|
/* If multibuffer mode is on, we could be here in view mode. If so,
|
|
|
|
* don't set the modification flag. */
|
2002-09-06 20:35:28 +00:00
|
|
|
if (!ISSET(VIEW_MODE))
|
|
|
|
set_modified();
|
|
|
|
|
|
|
|
if (wait(NULL) == -1)
|
|
|
|
nperror("wait");
|
|
|
|
|
2004-08-05 22:10:22 +00:00
|
|
|
if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
|
2002-09-06 20:35:28 +00:00
|
|
|
nperror("sigaction");
|
|
|
|
|
2004-05-19 16:04:27 +00:00
|
|
|
/* Disable interpretation of the special control keys so that we can
|
|
|
|
* use Ctrl-C for other things. */
|
2004-05-18 01:20:36 +00:00
|
|
|
disable_signals();
|
|
|
|
|
2004-08-05 22:10:22 +00:00
|
|
|
return TRUE;
|
2002-05-12 19:52:15 +00:00
|
|
|
}
|
2004-03-05 19:54:58 +00:00
|
|
|
#endif /* !NANO_SMALL */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_verbatim_input(void)
|
2003-11-28 19:47:42 +00:00
|
|
|
{
|
2004-12-04 17:41:52 +00:00
|
|
|
int *kbinput; /* Used to hold verbatim input. */
|
|
|
|
size_t kbinput_len; /* Length of verbatim input. */
|
2003-11-28 19:47:42 +00:00
|
|
|
|
|
|
|
statusbar(_("Verbatim input"));
|
2004-05-28 17:23:33 +00:00
|
|
|
|
2004-12-04 17:41:52 +00:00
|
|
|
/* Read in all the verbatim characters. */
|
|
|
|
kbinput = get_verbatim_kbinput(edit, &kbinput_len);
|
2003-11-28 19:47:42 +00:00
|
|
|
|
2004-12-04 17:41:52 +00:00
|
|
|
/* Display all the verbatim characters at once. */
|
|
|
|
do_output(kbinput, kbinput_len);
|
2003-11-28 19:47:42 +00:00
|
|
|
|
2004-12-04 17:41:52 +00:00
|
|
|
free(kbinput);
|
2003-11-28 19:47:42 +00:00
|
|
|
}
|
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_backspace(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2003-09-07 23:57:24 +00:00
|
|
|
if (current != fileage || current_x > 0) {
|
2004-05-28 20:44:09 +00:00
|
|
|
do_left(FALSE);
|
2003-09-07 23:57:24 +00:00
|
|
|
do_delete();
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_delete(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2004-08-05 22:10:22 +00:00
|
|
|
bool do_refresh = FALSE;
|
2004-05-28 20:44:09 +00:00
|
|
|
/* Do we have to call edit_refresh(), or can we get away with
|
|
|
|
* update_line()? */
|
|
|
|
|
2004-11-27 21:10:11 +00:00
|
|
|
assert(current != NULL && current->data != NULL &&
|
|
|
|
current_x <= strlen(current->data));
|
2002-07-29 23:46:38 +00:00
|
|
|
|
2004-04-19 02:44:13 +00:00
|
|
|
placewewant = xplustabs();
|
2001-11-29 03:43:08 +00:00
|
|
|
|
2004-04-19 02:44:13 +00:00
|
|
|
if (current->data[current_x] != '\0') {
|
|
|
|
size_t linelen = strlen(current->data + current_x);
|
2001-11-29 03:43:08 +00:00
|
|
|
|
2004-04-19 02:44:13 +00:00
|
|
|
assert(current_x < strlen(current->data));
|
2002-07-20 13:57:41 +00:00
|
|
|
|
2004-04-19 02:44:13 +00:00
|
|
|
/* Let's get dangerous. */
|
2003-09-16 02:04:00 +00:00
|
|
|
charmove(¤t->data[current_x], ¤t->data[current_x + 1],
|
2004-04-19 02:44:13 +00:00
|
|
|
linelen);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-04-19 02:44:13 +00:00
|
|
|
null_at(¤t->data, linelen + current_x - 1);
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (current_x < mark_beginx && mark_beginbuf == current)
|
|
|
|
mark_beginx--;
|
2002-07-29 23:46:38 +00:00
|
|
|
#endif
|
2004-04-19 02:44:13 +00:00
|
|
|
} else if (current != filebot && (current->next != filebot ||
|
|
|
|
current->data[0] == '\0')) {
|
2002-05-12 19:52:15 +00:00
|
|
|
/* We can delete the line before filebot only if it is blank: it
|
2004-05-24 05:05:07 +00:00
|
|
|
* becomes the new magicline then. */
|
2004-04-19 02:44:13 +00:00
|
|
|
filestruct *foo = current->next;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-04-19 02:44:13 +00:00
|
|
|
assert(current_x == strlen(current->data));
|
2004-05-28 20:44:09 +00:00
|
|
|
|
|
|
|
/* If we're deleting at the end of a line, we need to call
|
|
|
|
* edit_refresh(). */
|
|
|
|
if (current->data[current_x] == '\0')
|
|
|
|
do_refresh = TRUE;
|
|
|
|
|
2004-04-19 02:44:13 +00:00
|
|
|
current->data = charealloc(current->data, current_x +
|
|
|
|
strlen(foo->data) + 1);
|
|
|
|
strcpy(current->data + current_x, foo->data);
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (mark_beginbuf == current->next) {
|
|
|
|
mark_beginx += current_x;
|
|
|
|
mark_beginbuf = current;
|
|
|
|
}
|
|
|
|
#endif
|
2004-03-19 02:15:42 +00:00
|
|
|
if (filebot == foo)
|
2000-06-06 05:53:49 +00:00
|
|
|
filebot = current;
|
|
|
|
|
|
|
|
unlink_node(foo);
|
|
|
|
delete_node(foo);
|
|
|
|
renumber(current);
|
|
|
|
totlines--;
|
2004-08-07 21:27:37 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2004-04-19 02:44:13 +00:00
|
|
|
wrap_reset();
|
2004-08-07 21:27:37 +00:00
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
} else
|
2004-07-02 14:31:03 +00:00
|
|
|
return;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
|
|
|
totsize--;
|
|
|
|
set_modified();
|
2004-05-28 20:44:09 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
/* If color syntaxes are turned on, we need to call
|
|
|
|
* edit_refresh(). */
|
|
|
|
if (ISSET(COLOR_SYNTAX))
|
|
|
|
do_refresh = TRUE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (do_refresh)
|
|
|
|
edit_refresh();
|
|
|
|
else
|
|
|
|
update_line(current, current_x);
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_tab(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2004-12-04 17:41:52 +00:00
|
|
|
int kbinput = '\t';
|
|
|
|
do_output(&kbinput, 1);
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Someone hits return *gasp!* */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_enter(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2004-05-27 20:09:52 +00:00
|
|
|
filestruct *newnode = make_new_node(current);
|
|
|
|
size_t extra = 0;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
assert(current != NULL && current->data != NULL);
|
2002-01-21 20:32:22 +00:00
|
|
|
|
2002-06-13 00:40:19 +00:00
|
|
|
#ifndef NANO_SMALL
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Do auto-indenting, like the neolithic Turbo Pascal editor. */
|
|
|
|
if (ISSET(AUTOINDENT)) {
|
2004-05-27 20:09:52 +00:00
|
|
|
/* If we are breaking the line in the indentation, the new
|
|
|
|
* indentation should have only current_x characters, and
|
|
|
|
* current_x should not change. */
|
|
|
|
extra = indent_length(current->data);
|
|
|
|
if (extra > current_x)
|
2002-09-06 20:35:28 +00:00
|
|
|
extra = current_x;
|
|
|
|
totsize += extra;
|
2004-05-27 20:09:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
newnode->data = charalloc(strlen(current->data + current_x) +
|
|
|
|
extra + 1);
|
|
|
|
strcpy(&newnode->data[extra], current->data + current_x);
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (ISSET(AUTOINDENT))
|
2002-09-06 20:35:28 +00:00
|
|
|
strncpy(newnode->data, current->data, extra);
|
|
|
|
#endif
|
2004-05-27 20:09:52 +00:00
|
|
|
null_at(¤t->data, current_x);
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (current == mark_beginbuf && current_x < mark_beginx) {
|
|
|
|
mark_beginbuf = newnode;
|
|
|
|
mark_beginx += extra - current_x;
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2004-05-27 20:09:52 +00:00
|
|
|
#endif
|
|
|
|
current_x = extra;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2004-05-27 20:09:52 +00:00
|
|
|
if (current == filebot)
|
2002-09-06 20:35:28 +00:00
|
|
|
filebot = newnode;
|
|
|
|
splice_node(current, newnode, current->next);
|
2002-01-21 20:32:22 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
totsize++;
|
|
|
|
renumber(current);
|
|
|
|
current = newnode;
|
2004-05-27 20:09:52 +00:00
|
|
|
|
|
|
|
edit_refresh();
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
totlines++;
|
|
|
|
set_modified();
|
|
|
|
placewewant = xplustabs();
|
2000-11-05 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_next_word(void)
|
2000-11-05 16:52:21 +00:00
|
|
|
{
|
2004-07-28 20:46:25 +00:00
|
|
|
size_t old_pww = placewewant;
|
2004-10-18 01:51:43 +00:00
|
|
|
const filestruct *old_current = current;
|
2002-09-06 20:35:28 +00:00
|
|
|
assert(current != NULL && current->data != NULL);
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Skip letters in this word first. */
|
|
|
|
while (current->data[current_x] != '\0' &&
|
2004-10-21 22:49:51 +00:00
|
|
|
isalnum(current->data[current_x]))
|
2002-09-06 20:35:28 +00:00
|
|
|
current_x++;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
for (; current != NULL; current = current->next) {
|
|
|
|
while (current->data[current_x] != '\0' &&
|
2004-10-21 22:49:51 +00:00
|
|
|
!isalnum(current->data[current_x]))
|
2002-09-06 20:35:28 +00:00
|
|
|
current_x++;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
if (current->data[current_x] != '\0')
|
|
|
|
break;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
current_x = 0;
|
|
|
|
}
|
|
|
|
if (current == NULL)
|
|
|
|
current = filebot;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
placewewant = xplustabs();
|
2000-11-10 18:15:43 +00:00
|
|
|
|
2004-10-18 01:51:43 +00:00
|
|
|
/* Update the screen. */
|
|
|
|
edit_redraw(old_current, old_pww);
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* The same thing for backwards. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_prev_word(void)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2004-07-28 20:46:25 +00:00
|
|
|
size_t old_pww = placewewant;
|
2004-10-18 01:51:43 +00:00
|
|
|
const filestruct *old_current = current;
|
2002-12-22 16:30:00 +00:00
|
|
|
assert(current != NULL && current->data != NULL);
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2004-10-30 01:03:15 +00:00
|
|
|
current_x++;
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Skip letters in this word first. */
|
2004-10-30 01:03:15 +00:00
|
|
|
while (current_x > 0 && isalnum(current->data[current_x - 1]))
|
2002-09-06 20:35:28 +00:00
|
|
|
current_x--;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
for (; current != NULL; current = current->prev) {
|
2004-10-30 01:03:15 +00:00
|
|
|
while (current_x > 0 && !isalnum(current->data[current_x - 1]))
|
2002-09-06 20:35:28 +00:00
|
|
|
current_x--;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2004-10-30 01:03:15 +00:00
|
|
|
if (current_x > 0)
|
2002-09-06 20:35:28 +00:00
|
|
|
break;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
if (current->prev != NULL)
|
2004-10-30 01:03:15 +00:00
|
|
|
current_x = strlen(current->prev->data) + 1;
|
2000-11-05 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
2004-10-30 01:03:15 +00:00
|
|
|
current_x--;
|
|
|
|
|
2004-05-24 02:35:02 +00:00
|
|
|
if (current == NULL) {
|
2002-09-06 20:35:28 +00:00
|
|
|
current = fileage;
|
|
|
|
current_x = 0;
|
2004-05-24 02:35:02 +00:00
|
|
|
} else {
|
2004-10-21 22:49:51 +00:00
|
|
|
while (current_x > 0 && isalnum(current->data[current_x - 1]))
|
2004-05-24 02:35:02 +00:00
|
|
|
current_x--;
|
2002-03-25 03:26:27 +00:00
|
|
|
}
|
2000-11-10 18:15:43 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
placewewant = xplustabs();
|
2000-11-10 18:15:43 +00:00
|
|
|
|
2004-10-18 01:51:43 +00:00
|
|
|
/* Update the screen. */
|
|
|
|
edit_redraw(old_current, old_pww);
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2000-11-10 18:15:43 +00:00
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_mark(void)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2003-09-28 21:26:49 +00:00
|
|
|
TOGGLE(MARK_ISSET);
|
|
|
|
if (ISSET(MARK_ISSET)) {
|
2002-09-06 20:35:28 +00:00
|
|
|
statusbar(_("Mark Set"));
|
|
|
|
mark_beginbuf = current;
|
|
|
|
mark_beginx = current_x;
|
|
|
|
} else {
|
|
|
|
statusbar(_("Mark UNset"));
|
|
|
|
edit_refresh();
|
|
|
|
}
|
|
|
|
}
|
2004-04-30 04:49:02 +00:00
|
|
|
#endif /* !NANO_SMALL */
|
2000-11-10 18:15:43 +00:00
|
|
|
|
2003-09-10 20:08:00 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2002-09-06 20:35:28 +00:00
|
|
|
void wrap_reset(void)
|
|
|
|
{
|
2004-06-22 15:38:47 +00:00
|
|
|
same_line_wrap = FALSE;
|
2000-11-05 16:52:21 +00:00
|
|
|
}
|
2003-09-10 20:08:00 +00:00
|
|
|
#endif
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2004-05-31 14:58:59 +00:00
|
|
|
/* We wrap the given line. Precondition: we assume the cursor has been
|
|
|
|
* moved forward since the last typed character. Return value: whether
|
|
|
|
* we wrapped. */
|
2004-08-05 22:10:22 +00:00
|
|
|
bool do_wrap(filestruct *inptr)
|
2000-11-05 16:52:21 +00:00
|
|
|
{
|
2004-05-31 14:58:59 +00:00
|
|
|
size_t len = strlen(inptr->data);
|
|
|
|
/* Length of the line we wrap. */
|
|
|
|
size_t i = 0;
|
|
|
|
/* Generic loop variable. */
|
2004-11-23 03:42:43 +00:00
|
|
|
ssize_t wrap_loc = -1;
|
2004-05-31 14:58:59 +00:00
|
|
|
/* Index of inptr->data where we wrap. */
|
2004-11-23 03:42:43 +00:00
|
|
|
ssize_t word_back = -1;
|
2002-06-13 00:40:19 +00:00
|
|
|
#ifndef NANO_SMALL
|
2002-09-06 20:35:28 +00:00
|
|
|
const char *indentation = NULL;
|
2004-05-31 14:58:59 +00:00
|
|
|
/* Indentation to prepend to the new line. */
|
2004-07-17 19:49:12 +00:00
|
|
|
size_t indent_len = 0; /* strlen(indentation) */
|
2002-06-13 00:40:19 +00:00
|
|
|
#endif
|
2004-05-31 14:58:59 +00:00
|
|
|
const char *after_break; /* Text after the wrap point. */
|
|
|
|
size_t after_break_len; /* strlen(after_break) */
|
2004-08-05 22:10:22 +00:00
|
|
|
bool wrapping = FALSE; /* Do we prepend to the next line? */
|
2002-09-06 20:35:28 +00:00
|
|
|
const char *wrap_line = NULL;
|
2004-07-17 19:49:12 +00:00
|
|
|
/* The next line, minus indentation. */
|
2004-05-31 14:58:59 +00:00
|
|
|
size_t wrap_line_len = 0; /* strlen(wrap_line) */
|
|
|
|
char *newline = NULL; /* The line we create. */
|
|
|
|
size_t new_line_len = 0; /* Eventual length of newline. */
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* There are three steps. First, we decide where to wrap. Then, we
|
|
|
|
* create the new wrap line. Finally, we clean up. */
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2004-07-17 19:49:12 +00:00
|
|
|
/* Step 1, finding where to wrap. We are going to add a new line
|
2004-05-29 16:47:52 +00:00
|
|
|
* after a whitespace character. In this step, we set wrap_loc as the
|
2002-09-06 20:35:28 +00:00
|
|
|
* location of this replacement.
|
|
|
|
*
|
2004-11-23 03:42:43 +00:00
|
|
|
* Where should we break the line? We need the last legal wrap point
|
2002-09-06 20:35:28 +00:00
|
|
|
* such that the last word before it ended at or before fill. If there
|
|
|
|
* is no such point, we settle for the first legal wrap point.
|
|
|
|
*
|
2004-11-23 03:42:43 +00:00
|
|
|
* A legal wrap point is a whitespace character that is not followed by
|
|
|
|
* whitespace.
|
2002-09-06 20:35:28 +00:00
|
|
|
*
|
|
|
|
* If there is no legal wrap point or we found the last character of the
|
|
|
|
* line, we should return without wrapping.
|
|
|
|
*
|
|
|
|
* Note that the initial indentation does not count as a legal wrap
|
|
|
|
* point if we are going to auto-indent!
|
|
|
|
*
|
2004-05-29 17:05:52 +00:00
|
|
|
* Note that the code below could be optimized, by not calling
|
|
|
|
* strnlenpt() so often. */
|
2001-01-27 19:25:00 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (ISSET(AUTOINDENT))
|
|
|
|
i = indent_length(inptr->data);
|
|
|
|
#endif
|
|
|
|
wrap_line = inptr->data + i;
|
2003-11-05 22:04:08 +00:00
|
|
|
for (; i < len; i++, wrap_line++) {
|
2004-05-31 14:58:59 +00:00
|
|
|
/* Record where the last word ended. */
|
2004-05-13 17:19:54 +00:00
|
|
|
if (!isblank(*wrap_line))
|
2002-09-06 20:35:28 +00:00
|
|
|
word_back = i;
|
2004-11-23 03:42:43 +00:00
|
|
|
/* If we have found a legal wrap point and the current word
|
2004-05-31 14:58:59 +00:00
|
|
|
* extends too far, then we stop. */
|
2004-11-27 21:10:11 +00:00
|
|
|
if (wrap_loc != -1 &&
|
|
|
|
strnlenpt(inptr->data, word_back + 1) > fill)
|
2002-09-06 20:35:28 +00:00
|
|
|
break;
|
2004-11-23 03:42:43 +00:00
|
|
|
/* We record the latest legal wrap point. */
|
2004-05-13 17:19:54 +00:00
|
|
|
if (word_back != i && !isblank(wrap_line[1]))
|
2002-09-06 20:35:28 +00:00
|
|
|
wrap_loc = i;
|
2001-01-27 19:25:00 +00:00
|
|
|
}
|
2004-05-31 14:58:59 +00:00
|
|
|
if (i == len)
|
|
|
|
return FALSE;
|
2000-11-10 18:15:43 +00:00
|
|
|
|
2004-05-31 14:58:59 +00:00
|
|
|
/* Step 2, making the new wrap line. It will consist of indentation
|
|
|
|
* + after_break + " " + wrap_line (although indentation and
|
|
|
|
* wrap_line are conditional on flags and #defines). */
|
2000-11-10 18:15:43 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* after_break is the text that will be moved to the next line. */
|
|
|
|
after_break = inptr->data + wrap_loc + 1;
|
|
|
|
after_break_len = len - wrap_loc - 1;
|
|
|
|
assert(after_break_len == strlen(after_break));
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* new_line_len will later be increased by the lengths of indentation
|
|
|
|
* and wrap_line. */
|
|
|
|
new_line_len = after_break_len;
|
2000-11-10 18:15:43 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* We prepend the wrapped text to the next line, if the flag is set,
|
|
|
|
* and there is a next line, and prepending would not make the line
|
|
|
|
* too long. */
|
2003-09-10 20:08:00 +00:00
|
|
|
if (same_line_wrap && inptr->next) {
|
2002-09-06 20:35:28 +00:00
|
|
|
wrap_line = inptr->next->data;
|
|
|
|
wrap_line_len = strlen(wrap_line);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-05-31 14:58:59 +00:00
|
|
|
/* +1 for the space between after_break and wrap_line. */
|
2002-09-06 20:35:28 +00:00
|
|
|
if ((new_line_len + 1 + wrap_line_len) <= fill) {
|
2004-05-31 14:58:59 +00:00
|
|
|
wrapping = TRUE;
|
2002-09-06 20:35:28 +00:00
|
|
|
new_line_len += (1 + wrap_line_len);
|
|
|
|
}
|
|
|
|
}
|
2001-04-20 01:59:55 +00:00
|
|
|
|
2002-06-13 00:40:19 +00:00
|
|
|
#ifndef NANO_SMALL
|
2002-09-06 20:35:28 +00:00
|
|
|
if (ISSET(AUTOINDENT)) {
|
2002-09-21 02:19:45 +00:00
|
|
|
/* Indentation comes from the next line if wrapping, else from
|
|
|
|
* this line. */
|
2002-09-06 20:35:28 +00:00
|
|
|
indentation = (wrapping ? wrap_line : inptr->data);
|
|
|
|
indent_len = indent_length(indentation);
|
|
|
|
if (wrapping)
|
2002-09-21 02:19:45 +00:00
|
|
|
/* The wrap_line text should not duplicate indentation.
|
|
|
|
* Note in this case we need not increase new_line_len. */
|
2002-09-06 20:35:28 +00:00
|
|
|
wrap_line += indent_len;
|
|
|
|
else
|
|
|
|
new_line_len += indent_len;
|
2002-08-21 16:10:37 +00:00
|
|
|
}
|
2002-06-13 00:40:19 +00:00
|
|
|
#endif
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Now we allocate the new line and copy into it. */
|
|
|
|
newline = charalloc(new_line_len + 1); /* +1 for \0 */
|
2004-06-22 15:38:47 +00:00
|
|
|
new_line_len = 0;
|
2002-09-06 20:35:28 +00:00
|
|
|
*newline = '\0';
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (ISSET(AUTOINDENT)) {
|
|
|
|
strncpy(newline, indentation, indent_len);
|
|
|
|
newline[indent_len] = '\0';
|
2004-05-31 14:58:59 +00:00
|
|
|
new_line_len = indent_len;
|
2000-12-02 04:36:50 +00:00
|
|
|
}
|
2001-09-19 03:19:43 +00:00
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
strcat(newline, after_break);
|
2004-05-31 14:58:59 +00:00
|
|
|
new_line_len += after_break_len;
|
|
|
|
/* We end the old line after wrap_loc. Note that this does not eat
|
|
|
|
* the space. */
|
2002-09-19 23:19:34 +00:00
|
|
|
null_at(&inptr->data, wrap_loc + 1);
|
|
|
|
totsize++;
|
2002-09-06 20:35:28 +00:00
|
|
|
if (wrapping) {
|
2002-09-19 23:19:34 +00:00
|
|
|
/* In this case, totsize increases by 1 since we add a space
|
2002-09-21 02:19:45 +00:00
|
|
|
* between after_break and wrap_line. If the line already ends
|
2002-09-21 15:41:33 +00:00
|
|
|
* in a tab or a space, we don't add a space and decrement
|
|
|
|
* totsize to account for that. */
|
2004-05-31 14:58:59 +00:00
|
|
|
if (!isblank(newline[new_line_len - 1]))
|
2002-09-21 02:19:45 +00:00
|
|
|
strcat(newline, " ");
|
|
|
|
else
|
|
|
|
totsize--;
|
2002-09-06 20:35:28 +00:00
|
|
|
strcat(newline, wrap_line);
|
|
|
|
free(inptr->next->data);
|
|
|
|
inptr->next->data = newline;
|
|
|
|
} else {
|
|
|
|
filestruct *temp = (filestruct *)nmalloc(sizeof(filestruct));
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2004-05-31 14:58:59 +00:00
|
|
|
/* In this case, the file size changes by +1 for the new line,
|
|
|
|
* and +indent_len for the new indentation. */
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
totsize += indent_len;
|
|
|
|
#endif
|
|
|
|
totlines++;
|
|
|
|
temp->data = newline;
|
|
|
|
temp->prev = inptr;
|
|
|
|
temp->next = inptr->next;
|
|
|
|
temp->prev->next = temp;
|
2002-12-22 16:30:00 +00:00
|
|
|
/* If temp->next is NULL, then temp is the last line of the
|
|
|
|
* file, so we must set filebot. */
|
|
|
|
if (temp->next != NULL)
|
2002-09-06 20:35:28 +00:00
|
|
|
temp->next->prev = temp;
|
|
|
|
else
|
|
|
|
filebot = temp;
|
|
|
|
}
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2004-05-31 14:58:59 +00:00
|
|
|
/* Step 3, clean up. Here we reposition the cursor and mark, and do
|
|
|
|
* some other sundry things. */
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2004-07-17 19:49:12 +00:00
|
|
|
/* Later wraps of this line will be prepended to the next line. */
|
2004-06-22 15:38:47 +00:00
|
|
|
same_line_wrap = TRUE;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Each line knows its line number. We recalculate these if we
|
|
|
|
* inserted a new line. */
|
|
|
|
if (!wrapping)
|
|
|
|
renumber(inptr);
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* If the cursor was after the break point, we must move it. */
|
|
|
|
if (current_x > wrap_loc) {
|
|
|
|
current = current->next;
|
|
|
|
current_x -=
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
-indent_len +
|
2000-07-03 03:10:14 +00:00
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
wrap_loc + 1;
|
|
|
|
wrap_reset();
|
|
|
|
placewewant = xplustabs();
|
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-03-29 16:31:29 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-07-17 19:49:12 +00:00
|
|
|
/* If the mark was on this line after the wrap point, we move it
|
|
|
|
* down. If it was on the next line and we wrapped, we move it
|
2002-09-06 20:35:28 +00:00
|
|
|
* right. */
|
|
|
|
if (mark_beginbuf == inptr && mark_beginx > wrap_loc) {
|
|
|
|
mark_beginbuf = inptr->next;
|
|
|
|
mark_beginx -= wrap_loc - indent_len + 1;
|
|
|
|
} else if (wrapping && mark_beginbuf == inptr->next)
|
|
|
|
mark_beginx += after_break_len;
|
|
|
|
#endif /* !NANO_SMALL */
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2004-05-31 14:58:59 +00:00
|
|
|
return TRUE;
|
2002-03-29 16:31:29 +00:00
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
#endif /* !DISABLE_WRAPPING */
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef DISABLE_SPELLER
|
2003-08-31 16:44:10 +00:00
|
|
|
/* A word is misspelled in the file. Let the user replace it. We
|
2004-08-05 22:10:22 +00:00
|
|
|
* return FALSE if the user cancels. */
|
|
|
|
bool do_int_spell_fix(const char *word)
|
2002-03-29 16:31:29 +00:00
|
|
|
{
|
2004-10-26 21:14:56 +00:00
|
|
|
char *save_search, *save_replace;
|
2004-10-21 16:25:44 +00:00
|
|
|
size_t current_x_save = current_x, pww_save = placewewant;
|
2004-10-26 21:14:56 +00:00
|
|
|
filestruct *edittop_save = edittop, *current_save = current;
|
2003-01-26 19:57:44 +00:00
|
|
|
/* Save where we are. */
|
2004-10-26 20:58:30 +00:00
|
|
|
bool canceled = FALSE;
|
2003-01-26 19:57:44 +00:00
|
|
|
/* The return value. */
|
2004-08-05 22:10:22 +00:00
|
|
|
bool case_sens_set = ISSET(CASE_SENSITIVE);
|
2004-10-11 13:55:33 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-10-09 16:59:43 +00:00
|
|
|
bool reverse_search_set = ISSET(REVERSE_SEARCH);
|
|
|
|
#endif
|
2004-10-09 17:15:46 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2004-10-09 16:59:43 +00:00
|
|
|
bool regexp_set = ISSET(USE_REGEXP);
|
|
|
|
#endif
|
2004-10-15 16:35:34 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
bool old_mark_set = ISSET(MARK_ISSET);
|
2004-11-06 00:18:27 +00:00
|
|
|
bool added_magicline = FALSE;
|
|
|
|
/* Whether we added a magicline after filebot. */
|
2004-11-05 23:03:03 +00:00
|
|
|
bool right_side_up = FALSE;
|
|
|
|
/* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark,
|
|
|
|
* FALSE if (current, current_x) is. */
|
2004-11-03 22:03:41 +00:00
|
|
|
filestruct *top, *bot;
|
2004-11-05 23:55:58 +00:00
|
|
|
size_t top_x, bot_x;
|
2004-10-15 16:35:34 +00:00
|
|
|
#endif
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2004-10-09 16:59:43 +00:00
|
|
|
/* Make sure spell-check is case sensitive. */
|
2002-12-10 00:53:21 +00:00
|
|
|
SET(CASE_SENSITIVE);
|
2004-10-09 16:59:43 +00:00
|
|
|
|
2004-10-11 13:55:33 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-10-09 16:59:43 +00:00
|
|
|
/* Make sure spell-check goes forward only. */
|
|
|
|
UNSET(REVERSE_SEARCH);
|
2002-09-06 20:35:28 +00:00
|
|
|
#endif
|
2004-10-09 17:15:46 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2004-10-09 16:59:43 +00:00
|
|
|
/* Make sure spell-check doesn't use regular expressions. */
|
|
|
|
UNSET(USE_REGEXP);
|
|
|
|
#endif
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2004-02-24 20:41:39 +00:00
|
|
|
/* Save the current search/replace strings. */
|
2002-09-06 20:35:28 +00:00
|
|
|
search_init_globals();
|
|
|
|
save_search = last_search;
|
|
|
|
save_replace = last_replace;
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2004-11-17 20:47:14 +00:00
|
|
|
/* Set the search/replace strings to the misspelled word. */
|
2002-09-06 20:35:28 +00:00
|
|
|
last_search = mallocstrcpy(NULL, word);
|
|
|
|
last_replace = mallocstrcpy(NULL, word);
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2004-11-03 22:03:41 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (old_mark_set) {
|
2004-11-04 16:45:48 +00:00
|
|
|
/* If the mark is on, partition the filestruct so that it
|
2004-11-06 00:18:27 +00:00
|
|
|
* contains only the marked text, keep track of whether the text
|
|
|
|
* will have a magicline added when we're done correcting
|
|
|
|
* misspelled words, and turn the mark off. */
|
2004-11-03 22:03:41 +00:00
|
|
|
mark_order((const filestruct **)&top, &top_x,
|
2004-11-05 23:03:03 +00:00
|
|
|
(const filestruct **)&bot, &bot_x, &right_side_up);
|
2004-11-03 22:03:41 +00:00
|
|
|
filepart = partition_filestruct(top, top_x, bot, bot_x);
|
2004-11-06 00:18:27 +00:00
|
|
|
added_magicline = (filebot->data[0] != '\0');
|
2004-11-03 22:03:41 +00:00
|
|
|
UNSET(MARK_ISSET);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-02-24 20:41:39 +00:00
|
|
|
/* Start from the top of the file. */
|
2004-10-18 02:06:53 +00:00
|
|
|
edittop = fileage;
|
2002-09-06 20:35:28 +00:00
|
|
|
current = fileage;
|
2004-10-30 01:03:15 +00:00
|
|
|
current_x = (size_t)-1;
|
2004-10-21 16:25:44 +00:00
|
|
|
placewewant = 0;
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2004-02-24 20:41:39 +00:00
|
|
|
/* Find the first whole-word occurrence of word. */
|
2004-10-31 13:20:30 +00:00
|
|
|
findnextstr_wrap_reset();
|
2004-10-27 02:21:01 +00:00
|
|
|
while (findnextstr(TRUE, TRUE, FALSE, fileage, 0, word, NULL)) {
|
2003-01-26 19:57:44 +00:00
|
|
|
if (is_whole_word(current_x, current->data, word)) {
|
|
|
|
edit_refresh();
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
do_replace_highlight(TRUE, word);
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2004-10-15 16:25:56 +00:00
|
|
|
/* Allow all instances of the word to be corrected. */
|
2004-10-26 20:58:30 +00:00
|
|
|
canceled = (statusq(FALSE, spell_list, word,
|
2003-01-13 01:35:15 +00:00
|
|
|
#ifndef NANO_SMALL
|
2003-01-26 19:57:44 +00:00
|
|
|
NULL,
|
2003-01-13 01:35:15 +00:00
|
|
|
#endif
|
2004-10-26 20:58:30 +00:00
|
|
|
_("Edit a replacement")) == -1);
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
do_replace_highlight(FALSE, word);
|
2002-03-29 16:31:29 +00:00
|
|
|
|
2004-10-26 20:58:30 +00:00
|
|
|
if (!canceled && strcmp(word, answer) != 0) {
|
2003-01-26 19:57:44 +00:00
|
|
|
current_x--;
|
2004-10-26 20:58:30 +00:00
|
|
|
do_replace_loop(word, current, ¤t_x, TRUE,
|
|
|
|
&canceled);
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2003-01-26 19:57:44 +00:00
|
|
|
|
|
|
|
break;
|
2001-07-11 02:08:33 +00:00
|
|
|
}
|
2004-10-18 22:19:22 +00:00
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-11-05 23:55:58 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (old_mark_set) {
|
2004-11-15 18:44:30 +00:00
|
|
|
/* If the mark was on and we added a magicline, remove it
|
|
|
|
* now. */
|
2004-11-06 00:18:27 +00:00
|
|
|
if (added_magicline)
|
|
|
|
remove_magicline();
|
2004-11-05 23:55:58 +00:00
|
|
|
|
2004-11-06 02:37:25 +00:00
|
|
|
/* Put the beginning and the end of the mark at the beginning
|
|
|
|
* and the end of the spell-checked text. */
|
|
|
|
if (fileage == filebot)
|
2004-11-17 20:47:14 +00:00
|
|
|
bot_x += top_x;
|
2004-11-06 02:37:25 +00:00
|
|
|
if (right_side_up) {
|
2004-11-17 20:47:14 +00:00
|
|
|
mark_beginx = top_x;
|
|
|
|
current_x_save = bot_x;
|
2004-11-06 02:37:25 +00:00
|
|
|
} else {
|
2004-11-17 20:47:14 +00:00
|
|
|
current_x_save = top_x;
|
|
|
|
mark_beginx = bot_x;
|
2004-11-06 02:37:25 +00:00
|
|
|
}
|
2004-11-06 00:18:27 +00:00
|
|
|
|
2004-11-15 18:44:30 +00:00
|
|
|
/* Unpartition the filestruct so that it contains all the text
|
|
|
|
* again, and turn the mark back on. */
|
2004-11-22 00:16:23 +00:00
|
|
|
unpartition_filestruct(&filepart);
|
2004-11-06 00:18:27 +00:00
|
|
|
SET(MARK_ISSET);
|
2004-11-05 23:55:58 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-11-05 23:03:03 +00:00
|
|
|
|
2004-02-24 20:41:39 +00:00
|
|
|
/* Restore the search/replace strings. */
|
|
|
|
free(last_search);
|
|
|
|
last_search = save_search;
|
|
|
|
free(last_replace);
|
|
|
|
last_replace = save_replace;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-02-24 20:41:39 +00:00
|
|
|
/* Restore where we were. */
|
2004-10-18 02:06:53 +00:00
|
|
|
edittop = edittop_save;
|
2003-01-26 19:57:44 +00:00
|
|
|
current = current_save;
|
|
|
|
current_x = current_x_save;
|
2004-10-21 16:25:44 +00:00
|
|
|
placewewant = pww_save;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-10-09 16:59:43 +00:00
|
|
|
/* Restore case sensitivity setting. */
|
2002-12-10 00:53:21 +00:00
|
|
|
if (!case_sens_set)
|
|
|
|
UNSET(CASE_SENSITIVE);
|
|
|
|
|
2004-10-11 13:55:33 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-10-09 16:59:43 +00:00
|
|
|
/* Restore search/replace direction. */
|
|
|
|
if (reverse_search_set)
|
|
|
|
SET(REVERSE_SEARCH);
|
2001-07-11 02:08:33 +00:00
|
|
|
#endif
|
2004-10-09 17:15:46 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2004-10-09 16:59:43 +00:00
|
|
|
/* Restore regular expression usage setting. */
|
|
|
|
if (regexp_set)
|
|
|
|
SET(USE_REGEXP);
|
|
|
|
#endif
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2004-10-26 20:58:30 +00:00
|
|
|
return !canceled;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
/* Integrated spell checking using 'spell' program. Return value: NULL
|
|
|
|
* for normal termination, otherwise the error string. */
|
2004-08-05 22:10:22 +00:00
|
|
|
const char *do_int_speller(const char *tempfile_name)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2002-09-06 20:35:28 +00:00
|
|
|
char *read_buff, *read_buff_ptr, *read_buff_word;
|
|
|
|
size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
|
2003-01-12 23:54:05 +00:00
|
|
|
int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
|
2002-12-09 00:58:51 +00:00
|
|
|
pid_t pid_spell, pid_sort, pid_uniq;
|
|
|
|
int spell_status, sort_status, uniq_status;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Create all three pipes up front. */
|
2003-01-12 23:54:05 +00:00
|
|
|
if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || pipe(uniq_fd) == -1)
|
|
|
|
return _("Could not create pipe");
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-12-10 00:16:27 +00:00
|
|
|
statusbar(_("Creating misspelled word list, please wait..."));
|
2001-04-12 03:01:53 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* A new process to run spell in. */
|
2002-09-06 20:35:28 +00:00
|
|
|
if ((pid_spell = fork()) == 0) {
|
2001-04-12 03:01:53 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Child continues (i.e, future spell process). */
|
2001-09-27 21:07:39 +00:00
|
|
|
|
2002-12-09 00:58:51 +00:00
|
|
|
close(spell_fd[0]);
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Replace the standard input with the temp file. */
|
2003-01-12 23:54:05 +00:00
|
|
|
if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
|
|
|
|
goto close_pipes_and_exit;
|
|
|
|
|
|
|
|
if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
close(tempfile_fd);
|
2001-04-12 03:01:53 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Send spell's standard output to the pipe. */
|
2003-01-12 23:54:05 +00:00
|
|
|
if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-12-09 00:58:51 +00:00
|
|
|
close(spell_fd[1]);
|
2001-04-12 03:01:53 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Start spell program; we are using PATH. */
|
2002-09-06 20:35:28 +00:00
|
|
|
execlp("spell", "spell", NULL);
|
2001-04-12 03:01:53 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Should not be reached, if spell is found. */
|
2002-09-06 20:35:28 +00:00
|
|
|
exit(1);
|
2001-04-12 03:01:53 +00:00
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Parent continues here. */
|
2002-12-09 00:58:51 +00:00
|
|
|
close(spell_fd[1]);
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* A new process to run sort in. */
|
2002-12-09 00:58:51 +00:00
|
|
|
if ((pid_sort = fork()) == 0) {
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Child continues (i.e, future spell process). Replace the
|
|
|
|
* standard input with the standard output of the old pipe. */
|
2003-01-12 23:54:05 +00:00
|
|
|
if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
|
|
|
|
2002-12-09 00:58:51 +00:00
|
|
|
close(spell_fd[0]);
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Send sort's standard output to the new pipe. */
|
2003-01-12 23:54:05 +00:00
|
|
|
if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
2002-12-09 00:58:51 +00:00
|
|
|
|
|
|
|
close(sort_fd[1]);
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Start sort program. Use -f to remove mixed case without
|
|
|
|
* having to have ANOTHER pipe for tr. If this isn't portable,
|
|
|
|
* let me know. */
|
2002-12-09 00:58:51 +00:00
|
|
|
execlp("sort", "sort", "-f", NULL);
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Should not be reached, if sort is found. */
|
2002-12-09 00:58:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-01-16 03:11:23 +00:00
|
|
|
close(spell_fd[0]);
|
2002-12-09 00:58:51 +00:00
|
|
|
close(sort_fd[1]);
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* A new process to run uniq in. */
|
2002-12-09 00:58:51 +00:00
|
|
|
if ((pid_uniq = fork()) == 0) {
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Child continues (i.e, future uniq process). Replace the
|
|
|
|
* standard input with the standard output of the old pipe. */
|
2003-01-12 23:54:05 +00:00
|
|
|
if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
|
|
|
|
2002-12-09 00:58:51 +00:00
|
|
|
close(sort_fd[0]);
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Send uniq's standard output to the new pipe. */
|
2003-01-12 23:54:05 +00:00
|
|
|
if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
2002-12-09 00:58:51 +00:00
|
|
|
|
|
|
|
close(uniq_fd[1]);
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Start uniq program; we are using PATH. */
|
2002-12-09 00:58:51 +00:00
|
|
|
execlp("uniq", "uniq", NULL);
|
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Should not be reached, if uniq is found. */
|
2002-12-09 00:58:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-01-16 03:11:23 +00:00
|
|
|
close(sort_fd[0]);
|
2002-12-09 00:58:51 +00:00
|
|
|
close(uniq_fd[1]);
|
2001-06-28 16:52:52 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Child process was not forked successfully. */
|
2002-12-09 00:58:51 +00:00
|
|
|
if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
|
|
|
|
close(uniq_fd[0]);
|
2002-12-16 04:25:53 +00:00
|
|
|
return _("Could not fork");
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2000-09-04 03:20:38 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Get system pipe buffer size. */
|
2002-12-09 00:58:51 +00:00
|
|
|
if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
|
|
|
|
close(uniq_fd[0]);
|
2002-12-16 04:25:53 +00:00
|
|
|
return _("Could not get size of pipe buffer");
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Read in the returned spelling errors. */
|
2002-09-06 20:35:28 +00:00
|
|
|
read_buff_read = 0;
|
|
|
|
read_buff_size = pipe_buff_size + 1;
|
|
|
|
read_buff = read_buff_ptr = charalloc(read_buff_size);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-12-09 00:58:51 +00:00
|
|
|
while ((bytesread = read(uniq_fd[0], read_buff_ptr, pipe_buff_size)) > 0) {
|
2002-09-06 20:35:28 +00:00
|
|
|
read_buff_read += bytesread;
|
|
|
|
read_buff_size += pipe_buff_size;
|
2003-06-14 20:41:34 +00:00
|
|
|
read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size);
|
2002-09-06 20:35:28 +00:00
|
|
|
read_buff_ptr += read_buff_read;
|
2002-12-09 00:58:51 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
*read_buff_ptr = (char)NULL;
|
2002-12-09 00:58:51 +00:00
|
|
|
close(uniq_fd[0]);
|
2001-01-14 05:18:27 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Process the spelling errors. */
|
2002-09-06 20:35:28 +00:00
|
|
|
read_buff_word = read_buff_ptr = read_buff;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
while (*read_buff_ptr != '\0') {
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
if ((*read_buff_ptr == '\n') || (*read_buff_ptr == '\r')) {
|
2002-12-22 16:30:00 +00:00
|
|
|
*read_buff_ptr = (char)NULL;
|
2002-09-06 20:35:28 +00:00
|
|
|
if (read_buff_word != read_buff_ptr) {
|
|
|
|
if (!do_int_spell_fix(read_buff_word)) {
|
|
|
|
read_buff_word = read_buff_ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
read_buff_word = read_buff_ptr + 1;
|
|
|
|
}
|
|
|
|
read_buff_ptr++;
|
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Special case where last word doesn't end with \n or \r. */
|
2002-09-06 20:35:28 +00:00
|
|
|
if (read_buff_word != read_buff_ptr)
|
|
|
|
do_int_spell_fix(read_buff_word);
|
2000-06-19 05:45:52 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
free(read_buff);
|
|
|
|
replace_abort();
|
2003-01-26 19:57:44 +00:00
|
|
|
edit_refresh();
|
2001-01-29 23:37:54 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Process end of spell process. */
|
2002-12-16 04:25:53 +00:00
|
|
|
waitpid(pid_spell, &spell_status, 0);
|
|
|
|
waitpid(pid_sort, &sort_status, 0);
|
|
|
|
waitpid(pid_uniq, &uniq_status, 0);
|
2002-12-09 00:58:51 +00:00
|
|
|
|
2002-12-16 04:25:53 +00:00
|
|
|
if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
|
|
|
|
return _("Error invoking \"spell\"");
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2002-12-16 04:25:53 +00:00
|
|
|
if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
|
|
|
|
return _("Error invoking \"sort -f\"");
|
|
|
|
|
|
|
|
if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
|
|
|
|
return _("Error invoking \"uniq\"");
|
|
|
|
|
|
|
|
/* Otherwise... */
|
|
|
|
return NULL;
|
2003-01-12 23:54:05 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
close_pipes_and_exit:
|
2003-01-12 23:54:05 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Don't leak any handles. */
|
2003-01-12 23:54:05 +00:00
|
|
|
close(tempfile_fd);
|
|
|
|
close(spell_fd[0]);
|
|
|
|
close(spell_fd[1]);
|
|
|
|
close(sort_fd[0]);
|
|
|
|
close(sort_fd[1]);
|
|
|
|
close(uniq_fd[0]);
|
|
|
|
close(uniq_fd[1]);
|
|
|
|
exit(1);
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
/* External spell checking. Return value: NULL for normal termination,
|
|
|
|
* otherwise the error string. */
|
2004-05-23 21:11:14 +00:00
|
|
|
const char *do_alt_speller(char *tempfile_name)
|
2000-09-01 13:32:47 +00:00
|
|
|
{
|
2004-11-05 23:03:03 +00:00
|
|
|
int alt_spell_status, lineno_save = current->lineno;
|
|
|
|
size_t current_x_save = current_x, pww_save = placewewant;
|
|
|
|
int current_y_save = current_y;
|
2002-09-06 20:35:28 +00:00
|
|
|
pid_t pid_spell;
|
|
|
|
char *ptr;
|
|
|
|
static int arglen = 3;
|
2004-09-05 21:40:31 +00:00
|
|
|
static char **spellargs = NULL;
|
|
|
|
FILE *f;
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-09-05 21:40:31 +00:00
|
|
|
bool old_mark_set = ISSET(MARK_ISSET);
|
2004-11-03 22:03:41 +00:00
|
|
|
bool added_magicline = FALSE;
|
|
|
|
/* Whether we added a magicline after filebot. */
|
2004-11-05 23:03:03 +00:00
|
|
|
bool right_side_up = FALSE;
|
|
|
|
/* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark,
|
|
|
|
* FALSE if (current, current_x) is. */
|
2004-11-17 20:47:14 +00:00
|
|
|
filestruct *top, *bot;
|
|
|
|
size_t top_x, bot_x;
|
2004-11-05 23:03:03 +00:00
|
|
|
int mbb_lineno_save = 0;
|
2002-09-06 20:35:28 +00:00
|
|
|
/* We're going to close the current file, and open the output of
|
2004-01-23 19:34:03 +00:00
|
|
|
* the alternate spell command. The line that mark_beginbuf
|
|
|
|
* points to will be freed, so we save the line number and
|
|
|
|
* restore afterwards. */
|
2004-11-03 22:03:41 +00:00
|
|
|
long old_totsize = totsize;
|
|
|
|
/* Our saved value of totsize, used when we spell-check a marked
|
|
|
|
* selection. */
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-09-05 21:40:31 +00:00
|
|
|
if (old_mark_set) {
|
2004-11-03 22:03:41 +00:00
|
|
|
/* If the mark is on, save the number of the line it starts on,
|
|
|
|
* and then turn the mark off. */
|
2004-11-05 23:03:03 +00:00
|
|
|
mbb_lineno_save = mark_beginbuf->lineno;
|
2002-09-06 20:35:28 +00:00
|
|
|
UNSET(MARK_ISSET);
|
|
|
|
}
|
2001-06-30 04:09:09 +00:00
|
|
|
#endif
|
2000-09-01 13:32:47 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
endwin();
|
2000-09-01 13:32:47 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Set up an argument list to pass execvp(). */
|
2002-09-06 20:35:28 +00:00
|
|
|
if (spellargs == NULL) {
|
2003-06-14 20:41:34 +00:00
|
|
|
spellargs = (char **)nmalloc(arglen * sizeof(char *));
|
2001-06-28 16:52:52 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
spellargs[0] = strtok(alt_speller, " ");
|
|
|
|
while ((ptr = strtok(NULL, " ")) != NULL) {
|
|
|
|
arglen++;
|
2003-06-14 20:41:34 +00:00
|
|
|
spellargs = (char **)nrealloc(spellargs, arglen * sizeof(char *));
|
2002-09-06 20:35:28 +00:00
|
|
|
spellargs[arglen - 3] = ptr;
|
|
|
|
}
|
|
|
|
spellargs[arglen - 1] = NULL;
|
|
|
|
}
|
|
|
|
spellargs[arglen - 2] = tempfile_name;
|
2001-12-17 04:34:23 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Start a new process for the alternate speller. */
|
2002-09-06 20:35:28 +00:00
|
|
|
if ((pid_spell = fork()) == 0) {
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Start alternate spell program; we are using PATH. */
|
2002-09-06 20:35:28 +00:00
|
|
|
execvp(spellargs[0], spellargs);
|
2001-12-17 04:34:23 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Should not be reached, if alternate speller is found!!! */
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-12-17 04:34:23 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
/* Could not fork?? */
|
|
|
|
if (pid_spell < 0)
|
2002-12-16 04:25:53 +00:00
|
|
|
return _("Could not fork");
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Wait for alternate speller to complete. */
|
2002-09-06 20:35:28 +00:00
|
|
|
wait(&alt_spell_status);
|
2004-01-23 19:34:03 +00:00
|
|
|
|
2002-12-16 04:25:53 +00:00
|
|
|
if (!WIFEXITED(alt_spell_status) || WEXITSTATUS(alt_spell_status) != 0) {
|
|
|
|
char *altspell_error = NULL;
|
|
|
|
char *invoke_error = _("Could not invoke \"%s\"");
|
|
|
|
int msglen = strlen(invoke_error) + strlen(alt_speller) + 2;
|
|
|
|
|
|
|
|
altspell_error = charalloc(msglen);
|
|
|
|
snprintf(altspell_error, msglen, invoke_error, alt_speller);
|
|
|
|
return altspell_error;
|
|
|
|
}
|
2002-09-06 20:35:28 +00:00
|
|
|
|
|
|
|
refresh();
|
2004-01-23 19:34:03 +00:00
|
|
|
|
2004-10-16 04:56:34 +00:00
|
|
|
/* Restore the terminal to its previous state. */
|
|
|
|
terminal_init();
|
|
|
|
|
2004-08-05 22:10:22 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-09-05 21:40:31 +00:00
|
|
|
if (old_mark_set) {
|
2004-11-03 22:03:41 +00:00
|
|
|
long part_totsize;
|
|
|
|
|
|
|
|
/* If the mark was on, partition the filestruct so that it
|
|
|
|
* contains only the marked text, and keep track of whether the
|
|
|
|
* temp file (which should contain the spell-checked marked
|
|
|
|
* text) will have a magicline added when it's reloaded. */
|
|
|
|
mark_order((const filestruct **)&top, &top_x,
|
2004-11-05 23:03:03 +00:00
|
|
|
(const filestruct **)&bot, &bot_x, &right_side_up);
|
2004-11-03 22:03:41 +00:00
|
|
|
filepart = partition_filestruct(top, top_x, bot, bot_x);
|
|
|
|
added_magicline = (filebot->data[0] != '\0');
|
|
|
|
|
2004-11-22 16:04:18 +00:00
|
|
|
/* Get the number of characters in the marked text, and subtract
|
|
|
|
* it from the saved value of totsize. Note that we don't need
|
|
|
|
* to save totlines. */
|
|
|
|
get_totals(top, bot, NULL, &part_totsize);
|
2004-11-03 22:03:41 +00:00
|
|
|
old_totsize -= part_totsize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Reinitialize the filestruct. */
|
|
|
|
free_filestruct(fileage);
|
|
|
|
global_init(TRUE);
|
|
|
|
|
|
|
|
/* Reload the temp file. Do what load_buffer() would do, except for
|
|
|
|
* making a new buffer for the temp file if multibuffer support is
|
|
|
|
* available. */
|
|
|
|
open_file(tempfile_name, FALSE, &f);
|
|
|
|
read_file(f, tempfile_name);
|
|
|
|
current = fileage;
|
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (old_mark_set) {
|
2004-11-05 15:03:12 +00:00
|
|
|
filestruct *top_save = fileage;
|
2004-11-03 22:03:41 +00:00
|
|
|
|
2004-11-15 18:44:30 +00:00
|
|
|
/* If the mark was on and we added a magicline, remove it
|
|
|
|
* now. */
|
2004-11-03 22:03:41 +00:00
|
|
|
if (added_magicline)
|
|
|
|
remove_magicline();
|
|
|
|
|
2004-11-06 02:37:25 +00:00
|
|
|
/* Put the beginning and the end of the mark at the beginning
|
|
|
|
* and the end of the spell-checked text. */
|
|
|
|
if (fileage == filebot)
|
2004-11-17 20:47:14 +00:00
|
|
|
bot_x += top_x;
|
2004-11-06 02:37:25 +00:00
|
|
|
if (right_side_up) {
|
2004-11-17 20:47:14 +00:00
|
|
|
mark_beginx = top_x;
|
|
|
|
current_x_save = bot_x;
|
2004-11-06 02:37:25 +00:00
|
|
|
} else {
|
2004-11-17 20:47:14 +00:00
|
|
|
current_x_save = top_x;
|
|
|
|
mark_beginx = bot_x;
|
2004-11-06 02:37:25 +00:00
|
|
|
}
|
2004-11-05 23:03:03 +00:00
|
|
|
|
2004-11-15 18:44:30 +00:00
|
|
|
/* Unpartition the filestruct so that it contains all the text
|
|
|
|
* again. Note that we've replaced the marked text originally
|
|
|
|
* in the partition with the spell-checked marked text in the
|
|
|
|
* temp file. */
|
2004-11-22 00:16:23 +00:00
|
|
|
unpartition_filestruct(&filepart);
|
2004-11-03 22:03:41 +00:00
|
|
|
|
|
|
|
/* Renumber starting with the beginning line of the old
|
2004-11-22 16:04:18 +00:00
|
|
|
* partition. Also set totlines to the new number of lines in
|
|
|
|
* the file, add the number of characters in the spell-checked
|
|
|
|
* marked text to the saved value of totsize, and then make that
|
|
|
|
* saved value the actual value. */
|
2004-11-05 15:03:12 +00:00
|
|
|
renumber(top_save);
|
2004-11-22 16:04:18 +00:00
|
|
|
totlines = filebot->lineno;
|
2004-11-03 22:03:41 +00:00
|
|
|
old_totsize += totsize;
|
|
|
|
totsize = old_totsize;
|
|
|
|
|
|
|
|
/* Assign mark_beginbuf to the line where the mark began
|
|
|
|
* before. */
|
2004-11-05 23:03:03 +00:00
|
|
|
do_gotopos(mbb_lineno_save, mark_beginx, current_y_save, 0);
|
2002-09-06 20:35:28 +00:00
|
|
|
mark_beginbuf = current;
|
2004-11-03 22:03:41 +00:00
|
|
|
|
|
|
|
/* Assign mark_beginx to the location in mark_beginbuf where the
|
|
|
|
* mark began before, adjusted for any shortening of the
|
|
|
|
* line. */
|
2002-09-06 20:35:28 +00:00
|
|
|
mark_beginx = current_x;
|
2004-09-05 21:40:31 +00:00
|
|
|
|
2004-11-03 22:03:41 +00:00
|
|
|
/* Turn the mark back on. */
|
|
|
|
SET(MARK_ISSET);
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
2001-12-17 04:34:23 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Go back to the old position, mark the file as modified, and make
|
|
|
|
* sure that the titlebar is refreshed. */
|
2004-11-05 23:03:03 +00:00
|
|
|
do_gotopos(lineno_save, current_x_save, current_y_save, pww_save);
|
2002-09-06 20:35:28 +00:00
|
|
|
set_modified();
|
|
|
|
clearok(topwin, FALSE);
|
|
|
|
titlebar(NULL);
|
2001-06-30 04:09:09 +00:00
|
|
|
|
2002-12-16 04:25:53 +00:00
|
|
|
return NULL;
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2001-06-30 04:09:09 +00:00
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_spell(void)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2004-01-23 19:34:03 +00:00
|
|
|
int i;
|
2004-05-23 21:11:14 +00:00
|
|
|
char *temp = safe_tempnam(0, "nano.");
|
|
|
|
const char *spell_msg;
|
2001-06-28 16:52:52 +00:00
|
|
|
|
2004-05-23 21:11:14 +00:00
|
|
|
if (temp == NULL) {
|
|
|
|
statusbar(_("Could not create temp file: %s"), strerror(errno));
|
2004-07-02 14:31:03 +00:00
|
|
|
return;
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2000-09-01 13:32:47 +00:00
|
|
|
|
2004-01-23 19:34:03 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (ISSET(MARK_ISSET))
|
2004-05-28 00:15:28 +00:00
|
|
|
i = write_marked(temp, TRUE, FALSE);
|
2004-01-23 19:34:03 +00:00
|
|
|
else
|
|
|
|
#endif
|
2004-05-28 00:15:28 +00:00
|
|
|
i = write_file(temp, TRUE, FALSE, FALSE);
|
2004-01-23 19:34:03 +00:00
|
|
|
|
|
|
|
if (i == -1) {
|
2004-08-12 02:52:14 +00:00
|
|
|
statusbar(_("Error writing temp file: %s"), strerror(errno));
|
2002-09-06 20:35:28 +00:00
|
|
|
free(temp);
|
2004-07-02 14:31:03 +00:00
|
|
|
return;
|
2000-09-04 03:20:38 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2004-01-23 19:34:03 +00:00
|
|
|
/* Update the current open_files entry before spell-checking, in
|
|
|
|
* case any problems occur. */
|
2004-07-02 14:31:03 +00:00
|
|
|
add_open_file(TRUE);
|
2001-12-17 04:34:23 +00:00
|
|
|
#endif
|
2000-09-13 14:03:27 +00:00
|
|
|
|
2004-05-23 21:11:14 +00:00
|
|
|
spell_msg = alt_speller != NULL ? do_alt_speller(temp) :
|
|
|
|
do_int_speller(temp);
|
2004-01-23 19:34:03 +00:00
|
|
|
unlink(temp);
|
2003-02-12 02:52:04 +00:00
|
|
|
free(temp);
|
2001-01-04 02:33:52 +00:00
|
|
|
|
2004-08-05 22:10:22 +00:00
|
|
|
if (spell_msg != NULL)
|
2004-05-23 21:11:14 +00:00
|
|
|
statusbar(_("Spell checking failed: %s: %s"), spell_msg,
|
|
|
|
strerror(errno));
|
2004-08-05 22:10:22 +00:00
|
|
|
else
|
2004-05-23 21:11:14 +00:00
|
|
|
statusbar(_("Finished checking spelling"));
|
2000-09-01 13:32:47 +00:00
|
|
|
}
|
2004-04-30 04:49:02 +00:00
|
|
|
#endif /* !DISABLE_SPELLER */
|
2000-09-01 13:32:47 +00:00
|
|
|
|
2004-03-30 04:17:10 +00:00
|
|
|
#if !defined(NANO_SMALL) || !defined(DISABLE_JUSTIFY)
|
2004-05-29 17:05:52 +00:00
|
|
|
/* The "indentation" of a line is the whitespace between the quote part
|
|
|
|
* and the non-whitespace of the line. */
|
2002-09-06 20:35:28 +00:00
|
|
|
size_t indent_length(const char *line)
|
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
size_t len = 0;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
assert(line != NULL);
|
2004-05-13 17:19:54 +00:00
|
|
|
while (isblank(*line)) {
|
2002-07-19 01:08:59 +00:00
|
|
|
line++;
|
|
|
|
len++;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
return len;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
2004-03-30 04:17:10 +00:00
|
|
|
#endif /* !NANO_SMALL || !DISABLE_JUSTIFY */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
2004-05-13 22:23:58 +00:00
|
|
|
/* justify_format() replaces Tab by Space and multiple spaces by 1
|
2004-09-11 21:28:36 +00:00
|
|
|
* (except it maintains 2 after a non-repeated character in punct
|
|
|
|
* followed by a character in brackets). Note that the terminating \0
|
|
|
|
* counts as a space.
|
2002-07-19 01:08:59 +00:00
|
|
|
*
|
2004-05-17 20:32:51 +00:00
|
|
|
* justify_format() might make line->data shorter, and change the actual
|
|
|
|
* pointer with null_at().
|
2002-07-19 01:08:59 +00:00
|
|
|
*
|
2002-11-04 16:05:42 +00:00
|
|
|
* justify_format() will not look at the first skip characters of line.
|
2004-05-13 22:23:58 +00:00
|
|
|
* skip should be at most strlen(line->data). The character at
|
|
|
|
* line[skip + 1] must not be whitespace. */
|
2004-05-17 20:32:51 +00:00
|
|
|
void justify_format(filestruct *line, size_t skip)
|
2002-08-21 16:10:37 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
char *back, *front;
|
|
|
|
|
|
|
|
/* These four asserts are assumptions about the input data. */
|
|
|
|
assert(line != NULL);
|
|
|
|
assert(line->data != NULL);
|
2003-03-11 03:50:40 +00:00
|
|
|
assert(skip < strlen(line->data));
|
2004-05-13 17:19:54 +00:00
|
|
|
assert(!isblank(line->data[skip]));
|
2002-07-19 01:08:59 +00:00
|
|
|
|
|
|
|
back = line->data + skip;
|
2003-02-19 22:27:53 +00:00
|
|
|
for (front = back; ; front++) {
|
2004-08-05 22:10:22 +00:00
|
|
|
bool remove_space = FALSE;
|
2002-10-17 02:19:31 +00:00
|
|
|
/* Do we want to remove this space? */
|
|
|
|
|
2004-05-17 20:32:51 +00:00
|
|
|
if (*front == '\t')
|
2002-07-19 01:08:59 +00:00
|
|
|
*front = ' ';
|
2004-05-17 20:32:51 +00:00
|
|
|
|
2004-05-13 23:19:01 +00:00
|
|
|
/* These tests are safe since line->data + skip is not a
|
|
|
|
* space. */
|
2003-02-19 22:27:53 +00:00
|
|
|
if ((*front == '\0' || *front == ' ') && *(front - 1) == ' ') {
|
2004-09-11 21:28:36 +00:00
|
|
|
const char *bob = back - 2;
|
2002-10-17 02:19:31 +00:00
|
|
|
|
2004-05-13 17:28:03 +00:00
|
|
|
remove_space = TRUE;
|
2004-09-11 21:28:36 +00:00
|
|
|
for (; bob >= line->data + skip; bob--) {
|
2002-10-17 02:19:31 +00:00
|
|
|
if (strchr(punct, *bob) != NULL) {
|
2004-09-11 21:28:36 +00:00
|
|
|
/* If this character is in punct, don't remove the
|
|
|
|
* space unless this character and the character
|
|
|
|
* before it are the same. */
|
|
|
|
remove_space = (bob > line->data + skip &&
|
|
|
|
*bob == *(bob - 1));
|
2002-10-17 02:19:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strchr(brackets, *bob) == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove_space) {
|
2002-07-19 01:08:59 +00:00
|
|
|
/* Now *front is a space we want to remove. We do that by
|
2002-10-17 02:19:31 +00:00
|
|
|
* simply failing to assign it to *back. */
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (mark_beginbuf == line && back - line->data < mark_beginx)
|
|
|
|
mark_beginx--;
|
|
|
|
#endif
|
2003-02-19 22:27:53 +00:00
|
|
|
if (*front == '\0')
|
|
|
|
*(back - 1) = '\0';
|
2002-07-19 01:08:59 +00:00
|
|
|
} else {
|
|
|
|
*back = *front;
|
|
|
|
back++;
|
|
|
|
}
|
2003-02-19 22:27:53 +00:00
|
|
|
if (*front == '\0')
|
|
|
|
break;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2003-02-19 22:27:53 +00:00
|
|
|
back--;
|
2003-03-11 03:50:40 +00:00
|
|
|
assert(*back == '\0' && *front == '\0');
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
/* Now back is the new end of line->data. */
|
|
|
|
if (back != front) {
|
2003-03-11 03:50:40 +00:00
|
|
|
totsize -= front - back;
|
2002-07-19 01:08:59 +00:00
|
|
|
null_at(&line->data, back - line->data);
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (mark_beginbuf == line && back - line->data < mark_beginx)
|
|
|
|
mark_beginx = back - line->data;
|
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
/* The "quote part" of a line is the largest initial substring matching
|
|
|
|
* the quote string. This function returns the length of the quote part
|
|
|
|
* of the given line.
|
|
|
|
*
|
|
|
|
* Note that if !HAVE_REGEX_H then we match concatenated copies of
|
|
|
|
* quotestr. */
|
2004-07-30 03:54:34 +00:00
|
|
|
size_t quote_length(const char *line)
|
2002-08-21 16:10:37 +00:00
|
|
|
{
|
2004-07-30 03:54:34 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2002-07-19 01:08:59 +00:00
|
|
|
regmatch_t matches;
|
2004-07-30 03:54:34 +00:00
|
|
|
int rc = regexec("ereg, line, 1, &matches, 0);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
|
2002-07-19 01:08:59 +00:00
|
|
|
return 0;
|
2004-07-30 03:54:34 +00:00
|
|
|
/* matches.rm_so should be 0, since the quote string should start
|
|
|
|
* with the caret ^. */
|
2002-07-19 01:08:59 +00:00
|
|
|
return matches.rm_eo;
|
|
|
|
#else /* !HAVE_REGEX_H */
|
|
|
|
size_t qdepth = 0;
|
2002-03-03 22:36:36 +00:00
|
|
|
|
2004-07-17 19:49:12 +00:00
|
|
|
/* Compute quote depth level. */
|
2004-07-31 14:10:23 +00:00
|
|
|
while (strncmp(line + qdepth, quotestr, quotelen) == 0)
|
2004-07-30 03:54:34 +00:00
|
|
|
qdepth += quotelen;
|
2002-07-19 01:08:59 +00:00
|
|
|
return qdepth;
|
|
|
|
#endif /* !HAVE_REGEX_H */
|
2004-07-30 03:54:34 +00:00
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
/* a_line and b_line are lines of text. The quotation part of a_line is
|
|
|
|
* the first a_quote characters. Check that the quotation part of
|
|
|
|
* b_line is the same. */
|
2004-08-05 22:10:22 +00:00
|
|
|
bool quotes_match(const char *a_line, size_t a_quote, const char
|
|
|
|
*b_line)
|
2002-08-21 16:10:37 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
/* Here is the assumption about a_quote: */
|
2004-07-30 03:54:34 +00:00
|
|
|
assert(a_quote == quote_length(a_line));
|
|
|
|
return a_quote == quote_length(b_line) &&
|
2004-07-31 14:10:23 +00:00
|
|
|
strncmp(a_line, b_line, a_quote) == 0;
|
2002-07-19 01:08:59 +00:00
|
|
|
}
|
2002-03-03 22:36:36 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
/* We assume a_line and b_line have no quote part. Then, we return
|
|
|
|
* whether b_line could follow a_line in a paragraph. */
|
2004-08-05 22:10:22 +00:00
|
|
|
bool indents_match(const char *a_line, size_t a_indent, const char
|
2004-05-14 01:17:25 +00:00
|
|
|
*b_line, size_t b_indent)
|
2002-08-21 16:10:37 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
assert(a_indent == indent_length(a_line));
|
|
|
|
assert(b_indent == indent_length(b_line));
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-07-31 14:10:23 +00:00
|
|
|
return b_indent <= a_indent &&
|
|
|
|
strncmp(a_line, b_line, b_indent) == 0;
|
2002-07-19 01:08:59 +00:00
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
/* Is foo the beginning of a paragraph?
|
|
|
|
*
|
|
|
|
* A line of text consists of a "quote part", followed by an
|
|
|
|
* "indentation part", followed by text. The functions quote_length()
|
|
|
|
* and indent_length() calculate these parts.
|
|
|
|
*
|
|
|
|
* A line is "part of a paragraph" if it has a part not in the quote
|
|
|
|
* part or the indentation.
|
|
|
|
*
|
|
|
|
* A line is "the beginning of a paragraph" if it is part of a
|
|
|
|
* paragraph and
|
|
|
|
* 1) it is the top line of the file, or
|
|
|
|
* 2) the line above it is not part of a paragraph, or
|
|
|
|
* 3) the line above it does not have precisely the same quote
|
|
|
|
* part, or
|
|
|
|
* 4) the indentation of this line is not an initial substring of
|
|
|
|
* the indentation of the previous line, or
|
|
|
|
* 5) this line has no quote part and some indentation, and
|
|
|
|
* AUTOINDENT is not set.
|
|
|
|
* The reason for number 5) is that if AUTOINDENT is not set, then an
|
|
|
|
* indented line is expected to start a paragraph, like in books.
|
|
|
|
* Thus, nano can justify an indented paragraph only if AUTOINDENT is
|
|
|
|
* turned on. */
|
|
|
|
bool begpar(const filestruct *const foo)
|
|
|
|
{
|
|
|
|
size_t quote_len;
|
|
|
|
size_t indent_len;
|
|
|
|
size_t temp_id_len;
|
|
|
|
|
|
|
|
/* Case 1). */
|
|
|
|
if (foo->prev == NULL)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
quote_len = quote_length(foo->data);
|
|
|
|
indent_len = indent_length(foo->data + quote_len);
|
|
|
|
|
|
|
|
/* Not part of a paragraph. */
|
|
|
|
if (foo->data[quote_len + indent_len] == '\0')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Case 3). */
|
|
|
|
if (!quotes_match(foo->data, quote_len, foo->prev->data))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
temp_id_len = indent_length(foo->prev->data + quote_len);
|
|
|
|
|
|
|
|
/* Case 2) or 5) or 4). */
|
|
|
|
if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
|
|
|
|
(quote_len == 0 && indent_len > 0
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
&& !ISSET(AUTOINDENT)
|
|
|
|
#endif
|
|
|
|
) || !indents_match(foo->prev->data + quote_len, temp_id_len,
|
|
|
|
foo->data + quote_len, indent_len))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We find the last beginning-of-paragraph line before the current
|
|
|
|
* line. */
|
|
|
|
void do_para_begin(void)
|
|
|
|
{
|
|
|
|
const filestruct *old_current = current;
|
|
|
|
const size_t old_pww = placewewant;
|
|
|
|
|
|
|
|
current_x = 0;
|
|
|
|
placewewant = 0;
|
|
|
|
|
|
|
|
if (current->prev != NULL) {
|
|
|
|
do {
|
|
|
|
current = current->prev;
|
2004-08-27 17:02:05 +00:00
|
|
|
current_y--;
|
2004-07-30 03:54:34 +00:00
|
|
|
} while (!begpar(current));
|
|
|
|
}
|
|
|
|
|
|
|
|
edit_redraw(old_current, old_pww);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool inpar(const char *str)
|
|
|
|
{
|
|
|
|
size_t quote_len = quote_length(str);
|
|
|
|
|
|
|
|
return str[quote_len + indent_length(str + quote_len)] != '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A line is the last line of a paragraph if it is in a paragraph, and
|
|
|
|
* the next line isn't, or is the beginning of a paragraph. We move
|
|
|
|
* down to the end of a paragraph, then one line farther. */
|
|
|
|
void do_para_end(void)
|
|
|
|
{
|
|
|
|
const filestruct *const old_current = current;
|
|
|
|
const size_t old_pww = placewewant;
|
|
|
|
|
|
|
|
current_x = 0;
|
|
|
|
placewewant = 0;
|
|
|
|
|
|
|
|
while (current->next != NULL && !inpar(current->data))
|
|
|
|
current = current->next;
|
|
|
|
|
|
|
|
while (current->next != NULL && inpar(current->next->data) &&
|
2004-08-27 17:02:05 +00:00
|
|
|
!begpar(current->next)) {
|
2004-07-30 03:54:34 +00:00
|
|
|
current = current->next;
|
2004-08-27 17:02:05 +00:00
|
|
|
current_y++;
|
|
|
|
}
|
2004-07-30 03:54:34 +00:00
|
|
|
|
|
|
|
if (current->next != NULL)
|
|
|
|
current = current->next;
|
|
|
|
|
|
|
|
edit_redraw(old_current, old_pww);
|
|
|
|
}
|
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Put the next par_len lines, starting with first_line, into the
|
|
|
|
* justify buffer, leaving copies of those lines in place. Assume there
|
|
|
|
* are enough lines after first_line. Return the new copy of
|
|
|
|
* first_line. */
|
2004-05-14 01:17:25 +00:00
|
|
|
filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t
|
|
|
|
quote_len)
|
2002-08-21 16:10:37 +00:00
|
|
|
{
|
2004-11-23 04:08:28 +00:00
|
|
|
filestruct *top = first_line;
|
|
|
|
/* The top of the paragraph we're backing up. */
|
|
|
|
filestruct *bot = first_line;
|
|
|
|
/* The bottom of the paragraph we're backing up. */
|
|
|
|
size_t i;
|
|
|
|
/* Generic loop variable. */
|
|
|
|
size_t current_x_save = current_x;
|
|
|
|
int fl_lineno_save = first_line->lineno;
|
|
|
|
int edittop_lineno_save = edittop->lineno;
|
|
|
|
int current_lineno_save = current->lineno;
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
bool old_mark_set = ISSET(MARK_ISSET);
|
|
|
|
int mbb_lineno_save = 0;
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
if (old_mark_set)
|
|
|
|
mbb_lineno_save = mark_beginbuf->lineno;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Move bot down par_len lines to the newline after the last line of
|
|
|
|
* the paragraph. */
|
|
|
|
for (i = par_len; i > 0; i--)
|
|
|
|
bot = bot->next;
|
|
|
|
|
|
|
|
/* Move the paragraph from the main filestruct to the justify
|
|
|
|
* buffer. */
|
|
|
|
move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot, 0);
|
|
|
|
|
|
|
|
/* Copy the paragraph from the justify buffer to the main
|
|
|
|
* filestruct. */
|
|
|
|
copy_from_filestruct(jusbuffer, jusbottom);
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Move upward from the last line of the paragraph to the first
|
|
|
|
* line, putting first_line, edittop, current, and mark_beginbuf at
|
|
|
|
* the same lines in the copied paragraph that they had in the
|
|
|
|
* original paragraph. */
|
|
|
|
top = current->prev;
|
|
|
|
for (i = par_len; i > 0; i--) {
|
|
|
|
if (top->lineno == fl_lineno_save)
|
|
|
|
first_line = top;
|
|
|
|
if (top->lineno == edittop_lineno_save)
|
|
|
|
edittop = top;
|
|
|
|
if (top->lineno == current_lineno_save)
|
|
|
|
current = top;
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-11-23 04:08:28 +00:00
|
|
|
if (old_mark_set && top->lineno == mbb_lineno_save)
|
|
|
|
mark_beginbuf = top;
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
2004-11-23 04:08:28 +00:00
|
|
|
top = top->prev;
|
2002-07-19 01:08:59 +00:00
|
|
|
}
|
2004-11-23 04:08:28 +00:00
|
|
|
|
|
|
|
/* Put current_x at the same place in the copied paragraph that it
|
|
|
|
* had in the original paragraph. */
|
|
|
|
current_x = current_x_save;
|
|
|
|
|
|
|
|
set_modified();
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
return first_line;
|
|
|
|
}
|
|
|
|
|
2003-02-03 03:22:02 +00:00
|
|
|
/* Is it possible to break line at or before goal? */
|
2004-09-18 22:02:21 +00:00
|
|
|
bool breakable(const char *line, ssize_t goal)
|
2003-02-03 03:22:02 +00:00
|
|
|
{
|
2003-11-05 22:04:08 +00:00
|
|
|
for (; *line != '\0' && goal >= 0; line++) {
|
2004-05-13 17:19:54 +00:00
|
|
|
if (isblank(*line))
|
2003-02-03 03:22:02 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2004-08-05 22:10:22 +00:00
|
|
|
if (is_cntrl_char(*line))
|
2003-02-03 03:22:02 +00:00
|
|
|
goal -= 2;
|
|
|
|
else
|
|
|
|
goal -= 1;
|
|
|
|
}
|
2003-02-12 03:21:45 +00:00
|
|
|
/* If goal is not negative, the whole line (one word) was short
|
|
|
|
* enough. */
|
|
|
|
return goal >= 0;
|
2003-02-03 03:22:02 +00:00
|
|
|
}
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
/* We are trying to break a chunk off line. We find the last space such
|
2004-07-30 03:54:34 +00:00
|
|
|
* that the display length to there is at most goal + 1. If there is no
|
2004-08-05 22:10:22 +00:00
|
|
|
* such space, and force is TRUE, then we find the first space. Anyway,
|
|
|
|
* we then take the last space in that group of spaces. The terminating
|
|
|
|
* '\0' counts as a space. */
|
2004-12-02 17:37:09 +00:00
|
|
|
ssize_t break_line(const char *line, ssize_t goal, bool force)
|
2002-08-21 16:10:37 +00:00
|
|
|
{
|
2004-09-18 22:02:21 +00:00
|
|
|
ssize_t space_loc = -1;
|
2002-07-19 01:08:59 +00:00
|
|
|
/* Current tentative return value. Index of the last space we
|
|
|
|
* found with short enough display width. */
|
2004-09-18 22:02:21 +00:00
|
|
|
ssize_t cur_loc = 0;
|
2004-05-13 17:28:03 +00:00
|
|
|
/* Current index in line. */
|
2002-07-19 01:08:59 +00:00
|
|
|
|
|
|
|
assert(line != NULL);
|
2003-11-05 22:04:08 +00:00
|
|
|
for (; *line != '\0' && goal >= 0; line++, cur_loc++) {
|
2002-07-19 01:08:59 +00:00
|
|
|
if (*line == ' ')
|
|
|
|
space_loc = cur_loc;
|
|
|
|
assert(*line != '\t');
|
|
|
|
|
2002-07-20 13:57:41 +00:00
|
|
|
if (is_cntrl_char(*line))
|
2002-07-19 01:08:59 +00:00
|
|
|
goal -= 2;
|
|
|
|
else
|
|
|
|
goal--;
|
|
|
|
}
|
|
|
|
if (goal >= 0)
|
|
|
|
/* In fact, the whole line displays shorter than goal. */
|
|
|
|
return cur_loc;
|
|
|
|
if (space_loc == -1) {
|
|
|
|
/* No space found short enough. */
|
|
|
|
if (force)
|
2003-11-05 22:04:08 +00:00
|
|
|
for (; *line != '\0'; line++, cur_loc++)
|
2003-02-19 22:27:53 +00:00
|
|
|
if (*line == ' ' && *(line + 1) != ' ' && *(line + 1) != '\0')
|
2002-07-19 01:08:59 +00:00
|
|
|
return cur_loc;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Perhaps the character after space_loc is a space. But because
|
2002-11-04 16:05:42 +00:00
|
|
|
* of justify_format(), there can be only two adjacent. */
|
2002-07-19 01:08:59 +00:00
|
|
|
if (*(line - cur_loc + space_loc + 1) == ' ' ||
|
|
|
|
*(line - cur_loc + space_loc + 1) == '\0')
|
|
|
|
space_loc++;
|
|
|
|
return space_loc;
|
|
|
|
}
|
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
/* Find the beginning of the current paragraph if we're in one, or the
|
|
|
|
* beginning of the next paragraph if we're not. Afterwards, save the
|
|
|
|
* quote length and paragraph length in *quote and *par. Return FALSE
|
|
|
|
* if we found a paragraph, or TRUE if there was an error or we didn't
|
|
|
|
* find a paragraph.
|
2002-07-19 01:08:59 +00:00
|
|
|
*
|
2004-07-30 03:54:34 +00:00
|
|
|
* See the comment at begpar() for more about when a line is the
|
|
|
|
* beginning of a paragraph. */
|
|
|
|
bool do_para_search(size_t *const quote, size_t *const par)
|
2004-02-27 18:54:04 +00:00
|
|
|
{
|
2002-07-19 01:08:59 +00:00
|
|
|
size_t quote_len;
|
2003-11-05 22:04:08 +00:00
|
|
|
/* Length of the initial quotation of the paragraph we
|
2004-02-27 18:54:04 +00:00
|
|
|
* search. */
|
2002-07-19 01:08:59 +00:00
|
|
|
size_t par_len;
|
|
|
|
/* Number of lines in that paragraph. */
|
2004-05-28 20:44:09 +00:00
|
|
|
size_t indent_len;
|
|
|
|
/* Generic indentation length. */
|
|
|
|
filestruct *line;
|
|
|
|
/* Generic line of text. */
|
2003-09-04 20:25:29 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2004-07-30 03:54:34 +00:00
|
|
|
if (quoterc != 0) {
|
|
|
|
statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
|
|
|
|
return TRUE;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
/* Here is an assumption that is always true anyway. */
|
|
|
|
assert(current != NULL);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2003-09-04 20:25:29 +00:00
|
|
|
current_x = 0;
|
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
quote_len = quote_length(current->data);
|
2002-07-19 01:08:59 +00:00
|
|
|
indent_len = indent_length(current->data + quote_len);
|
2001-01-21 23:23:48 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
/* Here we find the first line of the paragraph to search. If the
|
|
|
|
* current line is in a paragraph, then we move back to the first
|
|
|
|
* line of the paragraph. Otherwise, we move to the first line that
|
|
|
|
* is in a paragraph. */
|
2002-07-19 01:08:59 +00:00
|
|
|
if (current->data[quote_len + indent_len] != '\0') {
|
|
|
|
/* This line is part of a paragraph. So we must search back to
|
2003-11-05 22:04:08 +00:00
|
|
|
* the first line of this paragraph. First we check items 1)
|
|
|
|
* and 3) above. */
|
2004-07-30 03:54:34 +00:00
|
|
|
while (current->prev != NULL && quotes_match(current->data,
|
|
|
|
quote_len, current->prev->data)) {
|
2002-09-13 18:14:04 +00:00
|
|
|
size_t temp_id_len =
|
2004-08-12 21:43:00 +00:00
|
|
|
indent_length(current->prev->data + quote_len);
|
2002-09-13 18:14:04 +00:00
|
|
|
/* The indentation length of the previous line. */
|
2002-08-21 16:10:37 +00:00
|
|
|
|
2002-09-13 18:14:04 +00:00
|
|
|
/* Is this line the beginning of a paragraph, according to
|
2004-02-27 18:54:04 +00:00
|
|
|
* items 2), 5), or 4) above? If so, stop. */
|
2002-09-13 18:14:04 +00:00
|
|
|
if (current->prev->data[quote_len + temp_id_len] == '\0' ||
|
2004-05-17 16:11:18 +00:00
|
|
|
(quote_len == 0 && indent_len > 0
|
2002-09-13 18:14:04 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-05-17 16:11:18 +00:00
|
|
|
&& !ISSET(AUTOINDENT)
|
2002-09-13 18:14:04 +00:00
|
|
|
#endif
|
2004-05-17 16:11:18 +00:00
|
|
|
) || !indents_match(current->prev->data + quote_len,
|
|
|
|
temp_id_len, current->data + quote_len, indent_len))
|
2002-09-13 18:14:04 +00:00
|
|
|
break;
|
|
|
|
indent_len = temp_id_len;
|
|
|
|
current = current->prev;
|
|
|
|
current_y--;
|
2002-04-06 05:02:14 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
} else {
|
2002-07-29 23:46:38 +00:00
|
|
|
/* This line is not part of a paragraph. Move down until we get
|
|
|
|
* to a non "blank" line. */
|
2002-07-19 01:08:59 +00:00
|
|
|
do {
|
2004-02-27 18:54:04 +00:00
|
|
|
/* There is no next paragraph, so nothing to move to. */
|
2002-08-21 16:10:37 +00:00
|
|
|
if (current->next == NULL) {
|
|
|
|
placewewant = 0;
|
2004-07-30 03:54:34 +00:00
|
|
|
return TRUE;
|
2002-08-21 16:10:37 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
current = current->next;
|
2002-08-21 16:10:37 +00:00
|
|
|
current_y++;
|
2004-07-30 03:54:34 +00:00
|
|
|
quote_len = quote_length(current->data);
|
2002-07-19 01:08:59 +00:00
|
|
|
indent_len = indent_length(current->data + quote_len);
|
|
|
|
} while (current->data[quote_len + indent_len] == '\0');
|
|
|
|
}
|
2004-02-27 18:54:04 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
/* Now current is the first line of the paragraph, and quote_len is
|
|
|
|
* the quotation length of that line. */
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2004-05-17 16:11:18 +00:00
|
|
|
/* Next step, compute par_len, the number of lines in this
|
|
|
|
* paragraph. */
|
2002-07-19 01:08:59 +00:00
|
|
|
line = current;
|
|
|
|
par_len = 1;
|
|
|
|
indent_len = indent_length(line->data + quote_len);
|
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
while (line->next != NULL &&
|
|
|
|
quotes_match(current->data, quote_len, line->next->data)) {
|
|
|
|
size_t temp_id_len = indent_length(line->next->data + quote_len);
|
2004-06-27 00:54:08 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
if (!indents_match(line->data + quote_len, indent_len,
|
2004-05-17 16:11:18 +00:00
|
|
|
line->next->data + quote_len, temp_id_len) ||
|
2002-07-19 01:08:59 +00:00
|
|
|
line->next->data[quote_len + temp_id_len] == '\0' ||
|
|
|
|
(quote_len == 0 && temp_id_len > 0
|
|
|
|
#ifndef NANO_SMALL
|
2004-06-27 00:54:08 +00:00
|
|
|
&& !ISSET(AUTOINDENT)
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
|
|
|
))
|
2004-07-30 03:54:34 +00:00
|
|
|
break;
|
|
|
|
indent_len = temp_id_len;
|
|
|
|
line = line->next;
|
|
|
|
par_len++;
|
2004-02-27 18:54:04 +00:00
|
|
|
}
|
|
|
|
|
2004-05-17 16:11:18 +00:00
|
|
|
/* Now par_len is the number of lines in this paragraph. We should
|
|
|
|
* never call quotes_match() or quote_length() again. */
|
2004-02-27 18:54:04 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
/* Save the values of quote_len and par_len. */
|
|
|
|
assert(quote != NULL && par != NULL);
|
|
|
|
*quote = quote_len;
|
|
|
|
*par = par_len;
|
2004-02-27 18:54:04 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
return FALSE;
|
2004-02-27 18:54:04 +00:00
|
|
|
}
|
|
|
|
|
2004-05-13 17:28:03 +00:00
|
|
|
/* If full_justify is TRUE, justify the entire file. Otherwise, justify
|
|
|
|
* the current paragraph. */
|
2004-08-05 22:10:22 +00:00
|
|
|
void do_justify(bool full_justify)
|
2004-02-27 18:54:04 +00:00
|
|
|
{
|
2004-05-17 20:32:51 +00:00
|
|
|
filestruct *first_par_line = NULL;
|
|
|
|
/* Will be the first line of the resulting justified paragraph.
|
2004-11-23 04:08:28 +00:00
|
|
|
* For restoring after unjustify. */
|
2004-05-17 20:38:00 +00:00
|
|
|
filestruct *last_par_line;
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Will be the line containing the newline after the last line
|
|
|
|
* of the result. Also for restoring after unjustify. */
|
2004-07-30 03:54:34 +00:00
|
|
|
bool allow_respacing;
|
2004-07-03 14:15:58 +00:00
|
|
|
/* Whether we should change the spacing at the end of a line
|
2004-07-05 14:27:29 +00:00
|
|
|
* after justifying it. This should be TRUE whenever we move
|
2004-07-03 14:15:58 +00:00
|
|
|
* to the next line after justifying the current line. */
|
2004-02-27 18:54:04 +00:00
|
|
|
|
|
|
|
/* We save these global variables to be restored if the user
|
2004-10-29 15:48:40 +00:00
|
|
|
* unjustifies. Note that we don't need to save totlines. */
|
2004-10-30 01:03:15 +00:00
|
|
|
size_t current_x_save = current_x;
|
|
|
|
int current_y_save = current_y;
|
2004-10-29 15:48:40 +00:00
|
|
|
long flags_save = flags, totsize_save = totsize;
|
|
|
|
filestruct *edittop_save = edittop, *current_save = current;
|
2004-02-27 18:54:04 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
filestruct *mark_beginbuf_save = mark_beginbuf;
|
2004-10-30 01:16:08 +00:00
|
|
|
size_t mark_beginx_save = mark_beginx;
|
2004-02-27 18:54:04 +00:00
|
|
|
#endif
|
2004-07-01 18:59:52 +00:00
|
|
|
int kbinput;
|
2004-12-04 17:41:52 +00:00
|
|
|
bool meta_key, func_key, s_or_t;
|
2004-02-27 18:54:04 +00:00
|
|
|
|
2004-05-17 20:32:51 +00:00
|
|
|
/* If we're justifying the entire file, start at the beginning. */
|
|
|
|
if (full_justify)
|
2004-05-13 17:28:03 +00:00
|
|
|
current = fileage;
|
2004-05-17 20:32:51 +00:00
|
|
|
|
|
|
|
last_par_line = current;
|
2004-05-13 17:28:03 +00:00
|
|
|
|
|
|
|
while (TRUE) {
|
2004-07-30 03:54:34 +00:00
|
|
|
size_t quote_len;
|
|
|
|
/* Length of the initial quotation of the paragraph we
|
|
|
|
* justify. */
|
|
|
|
size_t par_len;
|
|
|
|
/* Number of lines in that paragraph. */
|
|
|
|
|
|
|
|
/* Find the first line of the paragraph to be justified. That
|
|
|
|
* is the start of this paragraph if we're in one, or the start
|
|
|
|
* of the next otherwise. Save the quote length and paragraph
|
2004-12-04 17:36:14 +00:00
|
|
|
* length (number of lines). Don't refresh the screen yet,
|
|
|
|
* since we'll do that after we justify. If the search
|
|
|
|
* failed, we're justifying the whole file, and the search
|
|
|
|
* didn't leave us on the last line of the file, set the last
|
|
|
|
* line of the text to be justified to the last line of the file
|
|
|
|
* and break out of the loop. Otherwise, refresh the screen and
|
2004-07-30 03:54:34 +00:00
|
|
|
* get out. */
|
|
|
|
if (do_para_search("e_len, &par_len)) {
|
2004-12-04 17:36:14 +00:00
|
|
|
if (full_justify && current != filebot) {
|
2004-11-23 04:08:28 +00:00
|
|
|
last_par_line = filebot;
|
2004-05-13 17:28:03 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
edit_refresh();
|
2004-07-02 14:31:03 +00:00
|
|
|
return;
|
2004-05-13 17:28:03 +00:00
|
|
|
}
|
|
|
|
}
|
2003-09-04 20:25:29 +00:00
|
|
|
|
2004-05-13 17:28:03 +00:00
|
|
|
/* Next step, we loop through the lines of this paragraph,
|
|
|
|
* justifying each one individually. */
|
|
|
|
for (; par_len > 0; current_y++, par_len--) {
|
2004-07-30 03:54:34 +00:00
|
|
|
size_t indent_len; /* Generic indentation length. */
|
2004-05-13 17:28:03 +00:00
|
|
|
size_t line_len;
|
|
|
|
size_t display_len;
|
|
|
|
/* The width of current in screen columns. */
|
2004-09-18 22:02:21 +00:00
|
|
|
ssize_t break_pos;
|
2004-05-13 17:28:03 +00:00
|
|
|
/* Where we will break the line. */
|
|
|
|
|
2004-07-03 14:15:58 +00:00
|
|
|
/* We'll be moving to the next line after justifying the
|
|
|
|
* current line in almost all cases, so allow changing the
|
|
|
|
* spacing at the ends of justified lines by default. */
|
|
|
|
allow_respacing = TRUE;
|
|
|
|
|
2004-05-23 21:11:14 +00:00
|
|
|
indent_len = quote_len + indent_length(current->data +
|
|
|
|
quote_len);
|
2004-05-13 17:28:03 +00:00
|
|
|
|
2004-06-25 01:52:10 +00:00
|
|
|
/* If we haven't already done it, copy the original
|
2004-11-23 04:08:28 +00:00
|
|
|
* paragraph to the justify buffer. */
|
2004-05-17 20:32:51 +00:00
|
|
|
if (first_par_line == NULL)
|
|
|
|
first_par_line = backup_lines(current, full_justify ?
|
2004-11-17 16:59:34 +00:00
|
|
|
filebot->lineno - current->lineno : par_len,
|
|
|
|
quote_len);
|
2004-05-13 17:28:03 +00:00
|
|
|
|
2004-06-25 01:52:10 +00:00
|
|
|
/* Now we call justify_format() on the current line of the
|
|
|
|
* paragraph, which will remove excess spaces from it and
|
|
|
|
* change tabs to spaces. */
|
|
|
|
justify_format(current, quote_len +
|
|
|
|
indent_length(current->data + quote_len));
|
|
|
|
|
2004-05-13 17:28:03 +00:00
|
|
|
line_len = strlen(current->data);
|
|
|
|
display_len = strlenpt(current->data);
|
|
|
|
|
|
|
|
if (display_len > fill) {
|
|
|
|
/* The line is too long. Try to wrap it to the next. */
|
|
|
|
break_pos = break_line(current->data + indent_len,
|
2004-11-23 22:30:32 +00:00
|
|
|
fill - strnlenpt(current->data, indent_len),
|
|
|
|
TRUE);
|
|
|
|
if (break_pos == -1 || break_pos + indent_len ==
|
|
|
|
line_len)
|
2004-05-13 17:28:03 +00:00
|
|
|
/* We can't break the line, or don't need to, so
|
|
|
|
* just go on to the next. */
|
|
|
|
goto continue_loc;
|
|
|
|
break_pos += indent_len;
|
|
|
|
assert(break_pos < line_len);
|
|
|
|
if (par_len == 1) {
|
|
|
|
/* There is no next line in this paragraph. We make
|
|
|
|
* a new line and copy text after break_pos into
|
|
|
|
* it. */
|
2004-11-23 22:30:32 +00:00
|
|
|
splice_node(current, make_new_node(current),
|
|
|
|
current->next);
|
2004-05-13 17:28:03 +00:00
|
|
|
/* In a non-quoted paragraph, we copy the indent
|
|
|
|
* only if AUTOINDENT is turned on. */
|
2004-05-17 16:11:18 +00:00
|
|
|
if (quote_len == 0
|
2003-02-12 03:21:45 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-05-17 16:11:18 +00:00
|
|
|
&& !ISSET(AUTOINDENT)
|
2004-05-13 17:28:03 +00:00
|
|
|
#endif
|
2004-05-17 16:11:18 +00:00
|
|
|
)
|
2004-05-13 17:28:03 +00:00
|
|
|
indent_len = 0;
|
2004-11-23 22:30:32 +00:00
|
|
|
current->next->data = charalloc(indent_len +
|
|
|
|
line_len - break_pos);
|
|
|
|
strncpy(current->next->data, current->data,
|
|
|
|
indent_len);
|
2004-05-13 17:28:03 +00:00
|
|
|
strcpy(current->next->data + indent_len,
|
2002-07-19 01:08:59 +00:00
|
|
|
current->data + break_pos + 1);
|
2004-05-13 17:28:03 +00:00
|
|
|
assert(strlen(current->next->data) ==
|
2002-07-19 01:08:59 +00:00
|
|
|
indent_len + line_len - break_pos - 1);
|
2004-05-13 17:28:03 +00:00
|
|
|
totlines++;
|
|
|
|
totsize += indent_len;
|
|
|
|
par_len++;
|
|
|
|
} else {
|
|
|
|
size_t next_line_len = strlen(current->next->data);
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2004-05-13 17:28:03 +00:00
|
|
|
indent_len = quote_len +
|
2002-07-19 01:08:59 +00:00
|
|
|
indent_length(current->next->data + quote_len);
|
2004-11-23 22:30:32 +00:00
|
|
|
current->next->data =
|
|
|
|
charealloc(current->next->data, next_line_len +
|
|
|
|
line_len - break_pos + 1);
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2004-11-23 22:30:32 +00:00
|
|
|
charmove(current->next->data + indent_len +
|
|
|
|
line_len - break_pos, current->next->data +
|
|
|
|
indent_len, next_line_len - indent_len + 1);
|
2004-05-13 17:28:03 +00:00
|
|
|
strcpy(current->next->data + indent_len,
|
2002-07-19 01:08:59 +00:00
|
|
|
current->data + break_pos + 1);
|
2004-06-24 22:39:24 +00:00
|
|
|
current->next->data[indent_len + line_len -
|
|
|
|
break_pos - 1] = ' ';
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-05-13 17:28:03 +00:00
|
|
|
if (mark_beginbuf == current->next) {
|
|
|
|
if (mark_beginx < indent_len)
|
|
|
|
mark_beginx = indent_len;
|
|
|
|
mark_beginx += line_len - break_pos;
|
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
2004-05-13 17:28:03 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-11-27 21:10:11 +00:00
|
|
|
if (mark_beginbuf == current &&
|
|
|
|
mark_beginx > break_pos) {
|
2004-05-13 17:28:03 +00:00
|
|
|
mark_beginbuf = current->next;
|
|
|
|
mark_beginx -= break_pos + 1 - indent_len;
|
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
2004-05-13 17:28:03 +00:00
|
|
|
null_at(¤t->data, break_pos);
|
2004-07-03 14:15:58 +00:00
|
|
|
|
|
|
|
/* Go to the next line. */
|
2004-05-13 17:28:03 +00:00
|
|
|
current = current->next;
|
|
|
|
} else if (display_len < fill && par_len > 1) {
|
|
|
|
size_t next_line_len;
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2004-05-13 17:28:03 +00:00
|
|
|
indent_len = quote_len +
|
2002-07-19 01:08:59 +00:00
|
|
|
indent_length(current->next->data + quote_len);
|
2004-05-13 17:28:03 +00:00
|
|
|
/* If we can't pull a word from the next line up to this
|
|
|
|
* one, just go on. */
|
|
|
|
if (!breakable(current->next->data + indent_len,
|
|
|
|
fill - display_len - 1))
|
|
|
|
goto continue_loc;
|
|
|
|
|
|
|
|
break_pos = break_line(current->next->data + indent_len,
|
|
|
|
fill - display_len - 1, FALSE);
|
|
|
|
assert(break_pos != -1);
|
|
|
|
|
|
|
|
current->data = charealloc(current->data,
|
|
|
|
line_len + break_pos + 2);
|
|
|
|
current->data[line_len] = ' ';
|
|
|
|
strncpy(current->data + line_len + 1,
|
2002-07-19 01:08:59 +00:00
|
|
|
current->next->data + indent_len, break_pos);
|
2004-05-13 17:28:03 +00:00
|
|
|
current->data[line_len + break_pos + 1] = '\0';
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-05-13 17:28:03 +00:00
|
|
|
if (mark_beginbuf == current->next) {
|
|
|
|
if (mark_beginx < indent_len + break_pos) {
|
2004-05-23 21:11:14 +00:00
|
|
|
mark_beginbuf = current;
|
2004-05-13 17:28:03 +00:00
|
|
|
if (mark_beginx <= indent_len)
|
|
|
|
mark_beginx = line_len + 1;
|
|
|
|
else
|
2004-05-24 05:52:35 +00:00
|
|
|
mark_beginx = line_len + 1 + mark_beginx -
|
|
|
|
indent_len;
|
2004-05-13 17:28:03 +00:00
|
|
|
} else
|
|
|
|
mark_beginx -= break_pos + 1;
|
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
2004-05-13 17:28:03 +00:00
|
|
|
next_line_len = strlen(current->next->data);
|
|
|
|
if (indent_len + break_pos == next_line_len) {
|
|
|
|
filestruct *line = current->next;
|
|
|
|
|
|
|
|
/* Don't destroy edittop! */
|
|
|
|
if (line == edittop)
|
|
|
|
edittop = current;
|
|
|
|
|
|
|
|
unlink_node(line);
|
|
|
|
delete_node(line);
|
|
|
|
totlines--;
|
|
|
|
totsize -= indent_len;
|
|
|
|
current_y--;
|
2004-07-03 05:23:19 +00:00
|
|
|
|
2004-07-03 14:15:58 +00:00
|
|
|
/* Don't go to the next line. Accordingly, don't
|
|
|
|
* allow changing the spacing at the end of the
|
2004-07-03 14:34:03 +00:00
|
|
|
* previous justified line, so that we don't end up
|
|
|
|
* doing it more than once on the same line. */
|
2004-07-03 14:15:58 +00:00
|
|
|
allow_respacing = FALSE;
|
2004-05-13 17:28:03 +00:00
|
|
|
} else {
|
|
|
|
charmove(current->next->data + indent_len,
|
2002-07-19 01:08:59 +00:00
|
|
|
current->next->data + indent_len + break_pos + 1,
|
|
|
|
next_line_len - break_pos - indent_len);
|
2004-11-23 22:30:32 +00:00
|
|
|
null_at(¤t->next->data, next_line_len -
|
|
|
|
break_pos);
|
2004-07-03 05:23:19 +00:00
|
|
|
|
|
|
|
/* Go to the next line. */
|
2004-05-13 17:28:03 +00:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
} else
|
2002-08-21 16:10:37 +00:00
|
|
|
continue_loc:
|
2004-05-17 20:32:51 +00:00
|
|
|
/* Go to the next line. */
|
2004-05-13 17:28:03 +00:00
|
|
|
current = current->next;
|
2003-04-15 01:15:09 +00:00
|
|
|
|
2004-07-30 03:54:34 +00:00
|
|
|
/* We've moved to the next line after justifying the
|
2004-07-03 14:15:58 +00:00
|
|
|
* current line. If the justified line was not the last
|
|
|
|
* line of the paragraph, add a space to the end of it to
|
|
|
|
* replace the one removed or left out by justify_format().
|
|
|
|
* If it was the last line of the paragraph, and
|
|
|
|
* justify_format() left a space on the end of it, remove
|
|
|
|
* the space. */
|
|
|
|
if (allow_respacing) {
|
2004-05-17 20:32:51 +00:00
|
|
|
size_t prev_line_len = strlen(current->prev->data);
|
2004-06-05 22:09:56 +00:00
|
|
|
|
|
|
|
if (par_len > 1) {
|
|
|
|
current->prev->data = charealloc(current->prev->data,
|
2004-05-17 20:32:51 +00:00
|
|
|
prev_line_len + 2);
|
2004-06-05 22:09:56 +00:00
|
|
|
current->prev->data[prev_line_len] = ' ';
|
|
|
|
current->prev->data[prev_line_len + 1] = '\0';
|
|
|
|
totsize++;
|
|
|
|
} else if (par_len == 1 &&
|
|
|
|
current->prev->data[prev_line_len - 1] == ' ') {
|
2004-11-23 22:30:32 +00:00
|
|
|
current->prev->data =
|
|
|
|
charealloc(current->prev->data, prev_line_len);
|
2004-06-05 22:09:56 +00:00
|
|
|
current->prev->data[prev_line_len - 1] = '\0';
|
|
|
|
totsize--;
|
|
|
|
}
|
2004-05-17 20:32:51 +00:00
|
|
|
}
|
2004-05-13 17:28:03 +00:00
|
|
|
}
|
|
|
|
|
2004-05-17 20:32:51 +00:00
|
|
|
/* We've just justified a paragraph. If we're not justifying the
|
|
|
|
* entire file, break out of the loop. Otherwise, continue the
|
|
|
|
* loop so that we justify all the paragraphs in the file. */
|
2004-05-13 17:28:03 +00:00
|
|
|
if (!full_justify)
|
|
|
|
break;
|
|
|
|
|
|
|
|
} /* while (TRUE) */
|
2000-06-20 02:50:33 +00:00
|
|
|
|
2004-05-17 20:32:51 +00:00
|
|
|
/* We are now done justifying the paragraph or the file, so clean
|
|
|
|
* up. totlines, totsize, and current_y have been maintained above.
|
|
|
|
* Set last_par_line to the new end of the paragraph, update
|
2004-07-23 12:30:40 +00:00
|
|
|
* fileage, and renumber() since edit_refresh() needs the line
|
|
|
|
* numbers to be right (but only do the last two if we actually
|
|
|
|
* justified something). */
|
2004-11-23 04:08:28 +00:00
|
|
|
last_par_line = current;
|
2004-07-23 12:30:40 +00:00
|
|
|
if (first_par_line != NULL) {
|
|
|
|
if (first_par_line->prev == NULL)
|
|
|
|
fileage = first_par_line;
|
|
|
|
renumber(first_par_line);
|
|
|
|
}
|
2004-05-17 20:32:51 +00:00
|
|
|
|
2004-05-17 16:11:18 +00:00
|
|
|
edit_refresh();
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2000-11-27 00:23:41 +00:00
|
|
|
statusbar(_("Can now UnJustify!"));
|
2004-09-05 21:40:31 +00:00
|
|
|
|
2004-05-13 17:28:03 +00:00
|
|
|
/* Display the shortcut list with UnJustify. */
|
2004-07-01 18:59:52 +00:00
|
|
|
shortcut_init(TRUE);
|
2000-11-27 22:58:23 +00:00
|
|
|
display_main_list();
|
2000-11-27 00:23:41 +00:00
|
|
|
|
2004-11-28 04:52:57 +00:00
|
|
|
/* Now get a keystroke and see if it's unjustify. If not, put back
|
|
|
|
* the keystroke and return. */
|
2004-12-04 17:41:52 +00:00
|
|
|
kbinput = do_input(&meta_key, &func_key, &s_or_t, FALSE);
|
2004-07-01 18:59:52 +00:00
|
|
|
|
2004-12-04 17:41:52 +00:00
|
|
|
if (!meta_key && !func_key && s_or_t &&
|
|
|
|
kbinput == NANO_UNJUSTIFY_KEY) {
|
2004-08-25 15:39:10 +00:00
|
|
|
/* Restore the justify we just did (ungrateful user!). */
|
2002-07-19 01:08:59 +00:00
|
|
|
current = current_save;
|
|
|
|
current_x = current_x_save;
|
|
|
|
current_y = current_y_save;
|
|
|
|
edittop = edittop_save;
|
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Splice the justify buffer back into the file, but only if we
|
2004-07-23 12:30:40 +00:00
|
|
|
* actually justified something. */
|
|
|
|
if (first_par_line != NULL) {
|
2004-11-23 04:08:28 +00:00
|
|
|
filestruct *bot_save;
|
2004-07-23 12:30:40 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Partition the filestruct so that it contains only the
|
|
|
|
* text of the justified paragraph. */
|
|
|
|
filepart = partition_filestruct(first_par_line, 0,
|
|
|
|
last_par_line, 0);
|
2004-05-17 20:32:51 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Remove the text of the justified paragraph, and
|
|
|
|
* put the text in the justify buffer in its place. */
|
|
|
|
free_filestruct(fileage);
|
|
|
|
fileage = jusbuffer;
|
|
|
|
filebot = jusbottom;
|
|
|
|
|
|
|
|
bot_save = filebot;
|
|
|
|
|
|
|
|
/* Unpartition the filestruct so that it contains all the
|
|
|
|
* text again. Note that the justified paragraph has been
|
|
|
|
* replaced with the unjustified paragraph. */
|
|
|
|
unpartition_filestruct(&filepart);
|
|
|
|
|
|
|
|
/* Renumber starting with the ending line of the old
|
|
|
|
* partition. */
|
|
|
|
if (bot_save->next != NULL)
|
|
|
|
renumber(bot_save->next);
|
|
|
|
|
|
|
|
/* Restore global variables from before the justify. */
|
|
|
|
totsize = totsize_save;
|
|
|
|
totlines = filebot->lineno;
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-11-23 04:08:28 +00:00
|
|
|
mark_beginbuf = mark_beginbuf_save;
|
|
|
|
mark_beginx = mark_beginx_save;
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
2004-11-23 04:08:28 +00:00
|
|
|
flags = flags_save;
|
|
|
|
|
|
|
|
/* Clear the justify buffer. */
|
|
|
|
jusbuffer = NULL;
|
|
|
|
|
|
|
|
if (!ISSET(MODIFIED))
|
|
|
|
titlebar(NULL);
|
|
|
|
edit_refresh();
|
|
|
|
}
|
2004-08-25 15:39:10 +00:00
|
|
|
} else {
|
2004-12-04 17:41:52 +00:00
|
|
|
unget_kbinput(kbinput, meta_key, func_key);
|
2004-11-23 21:40:26 +00:00
|
|
|
|
2004-11-28 03:53:01 +00:00
|
|
|
/* Blow away the text in the justify buffer. */
|
2004-11-23 04:08:28 +00:00
|
|
|
free_filestruct(jusbuffer);
|
|
|
|
jusbuffer = NULL;
|
2000-11-27 00:23:41 +00:00
|
|
|
}
|
2004-07-01 18:59:52 +00:00
|
|
|
|
2004-05-23 21:11:14 +00:00
|
|
|
blank_statusbar();
|
2004-09-05 21:40:31 +00:00
|
|
|
|
2004-05-13 17:28:03 +00:00
|
|
|
/* Display the shortcut list with UnCut. */
|
2004-07-01 18:59:52 +00:00
|
|
|
shortcut_init(FALSE);
|
2000-11-27 22:59:40 +00:00
|
|
|
display_main_list();
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
2004-05-13 17:28:03 +00:00
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_justify_void(void)
|
2004-05-13 17:28:03 +00:00
|
|
|
{
|
2004-07-02 14:31:03 +00:00
|
|
|
do_justify(FALSE);
|
2004-05-13 17:28:03 +00:00
|
|
|
}
|
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_full_justify(void)
|
2004-05-13 17:28:03 +00:00
|
|
|
{
|
2004-07-02 14:31:03 +00:00
|
|
|
do_justify(TRUE);
|
2004-05-13 17:28:03 +00:00
|
|
|
}
|
2004-04-30 04:49:02 +00:00
|
|
|
#endif /* !DISABLE_JUSTIFY */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_exit(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2002-09-06 20:35:28 +00:00
|
|
|
int i;
|
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
if (!ISSET(MODIFIED))
|
|
|
|
i = 0; /* Pretend the user chose not to save. */
|
2004-07-03 03:09:12 +00:00
|
|
|
else if (ISSET(TEMP_FILE))
|
2002-09-06 20:35:28 +00:00
|
|
|
i = 1;
|
2003-12-24 03:33:09 +00:00
|
|
|
else
|
2004-07-02 14:31:03 +00:00
|
|
|
i = do_yesno(FALSE,
|
|
|
|
_("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES) ? "));
|
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
dump_buffer(fileage);
|
2001-10-22 03:15:31 +00:00
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-07-02 14:31:03 +00:00
|
|
|
if (i == 0 || (i == 1 && do_writeout(TRUE) > 0)) {
|
2002-09-06 20:35:28 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2004-12-01 15:11:27 +00:00
|
|
|
/* Exit only if there are no more open file buffers. */
|
2004-10-03 19:18:48 +00:00
|
|
|
if (!close_open_file())
|
2002-09-06 20:35:28 +00:00
|
|
|
#endif
|
2004-05-25 19:41:11 +00:00
|
|
|
finish();
|
2004-07-02 14:31:03 +00:00
|
|
|
} else if (i != 1)
|
2002-09-06 20:35:28 +00:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
|
|
|
|
display_main_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
void signal_init(void)
|
|
|
|
{
|
2004-04-07 00:44:35 +00:00
|
|
|
/* Trap SIGINT and SIGQUIT because we want them to do useful
|
|
|
|
* things. */
|
2002-09-06 20:35:28 +00:00
|
|
|
memset(&act, 0, sizeof(struct sigaction));
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGINT, &act, NULL);
|
2004-01-26 20:56:20 +00:00
|
|
|
sigaction(SIGQUIT, &act, NULL);
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-04-07 00:44:35 +00:00
|
|
|
/* Trap SIGHUP and SIGTERM because we want to write the file out. */
|
2002-10-17 02:19:31 +00:00
|
|
|
act.sa_handler = handle_hupterm;
|
2002-09-06 20:35:28 +00:00
|
|
|
sigaction(SIGHUP, &act, NULL);
|
2002-10-17 02:19:31 +00:00
|
|
|
sigaction(SIGTERM, &act, NULL);
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2002-03-21 05:07:28 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-04-07 00:44:35 +00:00
|
|
|
/* Trap SIGWINCH because we want to handle window resizes. */
|
2002-09-06 20:35:28 +00:00
|
|
|
act.sa_handler = handle_sigwinch;
|
|
|
|
sigaction(SIGWINCH, &act, NULL);
|
2004-02-16 20:32:40 +00:00
|
|
|
allow_pending_sigwinch(FALSE);
|
2002-03-21 05:07:28 +00:00
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Trap normal suspend (^Z) so we can handle it ourselves. */
|
2002-09-06 20:35:28 +00:00
|
|
|
if (!ISSET(SUSPEND)) {
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGTSTP, &act, NULL);
|
|
|
|
} else {
|
2004-04-07 00:44:35 +00:00
|
|
|
/* Block all other signals in the suspend and continue handlers.
|
|
|
|
* If we don't do this, other stuff interrupts them! */
|
2002-09-06 20:35:28 +00:00
|
|
|
sigfillset(&act.sa_mask);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
act.sa_handler = do_suspend;
|
|
|
|
sigaction(SIGTSTP, &act, NULL);
|
2002-05-12 19:52:15 +00:00
|
|
|
|
2002-09-06 20:35:28 +00:00
|
|
|
act.sa_handler = do_cont;
|
|
|
|
sigaction(SIGCONT, &act, NULL);
|
|
|
|
}
|
|
|
|
}
|
2000-09-01 13:32:47 +00:00
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Handler for SIGHUP (hangup) and SIGTERM (terminate). */
|
2002-10-17 02:19:31 +00:00
|
|
|
RETSIGTYPE handle_hupterm(int signal)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2003-02-03 03:32:08 +00:00
|
|
|
die(_("Received SIGHUP or SIGTERM\n"));
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
2002-06-21 03:20:06 +00:00
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Handler for SIGTSTP (suspend). */
|
2002-09-06 20:35:28 +00:00
|
|
|
RETSIGTYPE do_suspend(int signal)
|
|
|
|
{
|
|
|
|
endwin();
|
2003-02-12 23:54:34 +00:00
|
|
|
printf("\n\n\n\n\n%s\n", _("Use \"fg\" to return to nano"));
|
2002-09-06 20:35:28 +00:00
|
|
|
fflush(stdout);
|
2000-09-01 13:32:47 +00:00
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Restore the old terminal settings. */
|
2002-09-06 20:35:28 +00:00
|
|
|
tcsetattr(0, TCSANOW, &oldterm);
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2003-09-29 07:21:11 +00:00
|
|
|
/* Trap SIGHUP and SIGTERM so we can properly deal with them while
|
2004-05-18 01:20:36 +00:00
|
|
|
* suspended. */
|
2003-09-29 07:21:11 +00:00
|
|
|
act.sa_handler = handle_hupterm;
|
|
|
|
sigaction(SIGHUP, &act, NULL);
|
|
|
|
sigaction(SIGTERM, &act, NULL);
|
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Do what mutt does: send ourselves a SIGSTOP. */
|
2002-09-06 20:35:28 +00:00
|
|
|
kill(0, SIGSTOP);
|
|
|
|
}
|
2002-06-21 03:20:06 +00:00
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Handler for SIGCONT (continue after suspend). */
|
2002-09-06 20:35:28 +00:00
|
|
|
RETSIGTYPE do_cont(int signal)
|
|
|
|
{
|
2002-04-23 10:56:06 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Perhaps the user resized the window while we slept. Handle it
|
|
|
|
* and update the screen in the process. */
|
2002-09-06 20:35:28 +00:00
|
|
|
handle_sigwinch(0);
|
2004-04-07 00:44:35 +00:00
|
|
|
#else
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Just update the screen. */
|
|
|
|
doupdate();
|
2002-09-06 20:35:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
void handle_sigwinch(int s)
|
|
|
|
{
|
|
|
|
const char *tty = ttyname(0);
|
|
|
|
int fd;
|
|
|
|
int result = 0;
|
|
|
|
struct winsize win;
|
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
if (tty == NULL)
|
2002-09-06 20:35:28 +00:00
|
|
|
return;
|
|
|
|
fd = open(tty, O_RDWR);
|
|
|
|
if (fd == -1)
|
|
|
|
return;
|
|
|
|
result = ioctl(fd, TIOCGWINSZ, &win);
|
|
|
|
close(fd);
|
|
|
|
if (result == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Could check whether the COLS or LINES changed, and return
|
|
|
|
* otherwise. EXCEPT, that COLS and LINES are ncurses global
|
|
|
|
* variables, and in some cases ncurses has already updated them.
|
|
|
|
* But not in all cases, argh. */
|
|
|
|
COLS = win.ws_col;
|
|
|
|
LINES = win.ws_row;
|
2003-02-10 02:43:48 +00:00
|
|
|
editwinrows = LINES - 5 + no_help();
|
|
|
|
if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS)
|
2002-09-06 20:35:28 +00:00
|
|
|
die_too_small();
|
|
|
|
|
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
|
|
|
fill = wrap_at;
|
|
|
|
if (fill <= 0)
|
|
|
|
fill += COLS;
|
2003-02-10 02:43:48 +00:00
|
|
|
if (fill < 0)
|
|
|
|
fill = 0;
|
2002-09-06 20:35:28 +00:00
|
|
|
#endif
|
|
|
|
|
2003-06-14 20:41:34 +00:00
|
|
|
hblank = charealloc(hblank, COLS + 1);
|
2002-09-06 20:35:28 +00:00
|
|
|
memset(hblank, ' ', COLS);
|
|
|
|
hblank[COLS] = '\0';
|
|
|
|
|
2004-11-03 22:03:41 +00:00
|
|
|
/* If we've partitioned the filestruct, unpartition it now. */
|
|
|
|
if (filepart != NULL)
|
2004-11-22 00:16:23 +00:00
|
|
|
unpartition_filestruct(&filepart);
|
2004-11-03 22:03:41 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
2004-11-23 04:17:19 +00:00
|
|
|
/* If the justify buffer isn't empty, blow away the text in it and
|
|
|
|
* display the shortcut list with UnCut. */
|
2004-11-23 04:08:28 +00:00
|
|
|
if (jusbuffer != NULL) {
|
|
|
|
free_filestruct(jusbuffer);
|
|
|
|
jusbuffer = NULL;
|
|
|
|
shortcut_init(FALSE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
#ifdef USE_SLANG
|
|
|
|
/* Slang curses emulation brain damage, part 1: If we just do what
|
|
|
|
* curses does here, it'll only work properly if the resize made the
|
|
|
|
* window smaller. Do what mutt does: Leave and immediately reenter
|
|
|
|
* Slang screen management mode. */
|
|
|
|
SLsmg_reset_smg();
|
|
|
|
SLsmg_init_smg();
|
|
|
|
#else
|
|
|
|
/* Do the equivalent of what Minimum Profit does: Leave and
|
|
|
|
* immediately reenter curses mode. */
|
|
|
|
endwin();
|
|
|
|
refresh();
|
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
|
2004-07-27 16:46:35 +00:00
|
|
|
/* Restore the terminal to its previous state. */
|
|
|
|
terminal_init();
|
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Do the equivalent of what both mutt and Minimum Profit do:
|
|
|
|
* Reinitialize all the windows based on the new screen
|
|
|
|
* dimensions. */
|
|
|
|
window_init();
|
|
|
|
|
2004-11-12 00:09:20 +00:00
|
|
|
/* Redraw the contents of the windows that need it. */
|
2002-09-06 20:35:28 +00:00
|
|
|
blank_statusbar();
|
2004-05-18 01:20:36 +00:00
|
|
|
display_main_list();
|
2002-09-06 20:35:28 +00:00
|
|
|
total_refresh();
|
|
|
|
|
2004-01-30 04:20:28 +00:00
|
|
|
/* Turn cursor back on for sure. */
|
2002-09-06 20:35:28 +00:00
|
|
|
curs_set(1);
|
|
|
|
|
2004-05-28 17:23:33 +00:00
|
|
|
/* Reset all the input routines that rely on character sequences. */
|
|
|
|
reset_kbinput();
|
|
|
|
|
2004-01-30 04:20:28 +00:00
|
|
|
/* Jump back to the main loop. */
|
2002-09-06 20:35:28 +00:00
|
|
|
siglongjmp(jmpbuf, 1);
|
|
|
|
}
|
2004-02-16 20:32:40 +00:00
|
|
|
|
2004-08-05 22:10:22 +00:00
|
|
|
void allow_pending_sigwinch(bool allow)
|
2004-02-16 20:32:40 +00:00
|
|
|
{
|
|
|
|
sigset_t winch;
|
|
|
|
sigemptyset(&winch);
|
|
|
|
sigaddset(&winch, SIGWINCH);
|
|
|
|
if (allow)
|
|
|
|
sigprocmask(SIG_UNBLOCK, &winch, NULL);
|
|
|
|
else
|
|
|
|
sigprocmask(SIG_BLOCK, &winch, NULL);
|
|
|
|
}
|
2002-04-23 10:56:06 +00:00
|
|
|
#endif /* !NANO_SMALL */
|
2002-06-21 03:20:06 +00:00
|
|
|
|
2002-04-23 10:56:06 +00:00
|
|
|
#ifndef NANO_SMALL
|
2002-08-21 16:10:37 +00:00
|
|
|
void do_toggle(const toggle *which)
|
2000-09-01 13:32:47 +00:00
|
|
|
{
|
2004-08-05 22:10:22 +00:00
|
|
|
bool enabled;
|
2000-09-02 18:44:21 +00:00
|
|
|
|
2002-02-15 19:17:02 +00:00
|
|
|
TOGGLE(which->flag);
|
2000-09-12 23:02:49 +00:00
|
|
|
|
2002-02-15 19:17:02 +00:00
|
|
|
switch (which->val) {
|
2004-11-27 16:54:00 +00:00
|
|
|
case TOGGLE_SUSPEND_KEY:
|
|
|
|
signal_init();
|
|
|
|
break;
|
2003-10-03 20:26:25 +00:00
|
|
|
#ifndef DISABLE_MOUSE
|
2004-11-27 16:54:00 +00:00
|
|
|
case TOGGLE_MOUSE_KEY:
|
|
|
|
mouse_init();
|
|
|
|
break;
|
2002-12-22 16:30:00 +00:00
|
|
|
#endif
|
2004-11-27 16:54:00 +00:00
|
|
|
case TOGGLE_NOHELP_KEY:
|
|
|
|
blank_statusbar();
|
|
|
|
blank_bottombars();
|
|
|
|
wrefresh(bottomwin);
|
|
|
|
window_init();
|
|
|
|
edit_refresh();
|
|
|
|
display_main_list();
|
|
|
|
break;
|
2002-12-22 16:30:00 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2004-11-27 16:54:00 +00:00
|
|
|
case TOGGLE_SYNTAX_KEY:
|
|
|
|
edit_refresh();
|
|
|
|
break;
|
2004-05-29 16:25:30 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_NANORC
|
2004-11-27 16:54:00 +00:00
|
|
|
case TOGGLE_WHITESPACE_KEY:
|
2004-12-05 04:18:26 +00:00
|
|
|
titlebar(NULL);
|
2004-11-27 16:54:00 +00:00
|
|
|
edit_refresh();
|
|
|
|
break;
|
2002-12-22 16:30:00 +00:00
|
|
|
#endif
|
2000-09-01 13:32:47 +00:00
|
|
|
}
|
2000-09-12 23:02:49 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
/* We are assuming here that shortcut_init() above didn't free and
|
|
|
|
* reallocate the toggles. */
|
|
|
|
enabled = ISSET(which->flag);
|
|
|
|
if (which->val == TOGGLE_NOHELP_KEY || which->val == TOGGLE_WRAP_KEY)
|
|
|
|
enabled = !enabled;
|
|
|
|
statusbar("%s %s", which->desc,
|
|
|
|
enabled ? _("enabled") : _("disabled"));
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
2002-04-23 10:56:06 +00:00
|
|
|
#endif /* !NANO_SMALL */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-09-22 22:45:08 +00:00
|
|
|
void disable_extended_input(void)
|
|
|
|
{
|
|
|
|
struct termios term;
|
|
|
|
|
|
|
|
tcgetattr(0, &term);
|
|
|
|
term.c_lflag &= ~IEXTEN;
|
|
|
|
tcsetattr(0, TCSANOW, &term);
|
|
|
|
}
|
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
void disable_signals(void)
|
|
|
|
{
|
|
|
|
struct termios term;
|
|
|
|
|
|
|
|
tcgetattr(0, &term);
|
|
|
|
term.c_lflag &= ~ISIG;
|
|
|
|
tcsetattr(0, TCSANOW, &term);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
void enable_signals(void)
|
|
|
|
{
|
|
|
|
struct termios term;
|
|
|
|
|
|
|
|
tcgetattr(0, &term);
|
|
|
|
term.c_lflag |= ISIG;
|
|
|
|
tcsetattr(0, TCSANOW, &term);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void disable_flow_control(void)
|
|
|
|
{
|
|
|
|
struct termios term;
|
|
|
|
|
|
|
|
tcgetattr(0, &term);
|
|
|
|
term.c_iflag &= ~(IXON|IXOFF);
|
|
|
|
tcsetattr(0, TCSANOW, &term);
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable_flow_control(void)
|
|
|
|
{
|
|
|
|
struct termios term;
|
|
|
|
|
|
|
|
tcgetattr(0, &term);
|
|
|
|
term.c_iflag |= (IXON|IXOFF);
|
|
|
|
tcsetattr(0, TCSANOW, &term);
|
|
|
|
}
|
|
|
|
|
2004-07-27 16:46:35 +00:00
|
|
|
/* Set up the terminal state. Put the terminal in cbreak mode (read one
|
|
|
|
* character at a time and interpret the special control keys), disable
|
|
|
|
* translation of carriage return (^M) into newline (^J) so that we can
|
|
|
|
* tell the difference between the Enter key and Ctrl-J, and disable
|
2004-09-22 22:45:08 +00:00
|
|
|
* echoing of characters as they're typed. Finally, disable extended
|
|
|
|
* input processing, disable interpretation of the special control keys,
|
|
|
|
* and if we're not in preserve mode, disable interpretation of the flow
|
|
|
|
* control characters too. */
|
2004-07-27 16:46:35 +00:00
|
|
|
void terminal_init(void)
|
|
|
|
{
|
|
|
|
cbreak();
|
|
|
|
nonl();
|
|
|
|
noecho();
|
2004-09-22 22:45:08 +00:00
|
|
|
disable_extended_input();
|
2004-07-27 16:46:35 +00:00
|
|
|
disable_signals();
|
|
|
|
if (!ISSET(PRESERVE))
|
|
|
|
disable_flow_control();
|
|
|
|
}
|
|
|
|
|
2004-12-04 17:41:52 +00:00
|
|
|
int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
|
|
|
|
allow_funcs)
|
|
|
|
{
|
|
|
|
int input;
|
|
|
|
/* The character we read in. */
|
|
|
|
static int *kbinput = NULL;
|
|
|
|
/* The input buffer. */
|
|
|
|
static size_t kbinput_len = 0;
|
|
|
|
/* The length of the input buffer. */
|
|
|
|
const shortcut *s;
|
|
|
|
bool have_shortcut;
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
const toggle *t;
|
|
|
|
bool have_toggle;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*s_or_t = FALSE;
|
|
|
|
|
|
|
|
/* Read in a character. */
|
|
|
|
input = get_kbinput(edit, meta_key, func_key);
|
|
|
|
|
|
|
|
#ifndef DISABLE_MOUSE
|
|
|
|
/* If we got a mouse click and it was on a shortcut, read in the
|
|
|
|
* shortcut character. */
|
|
|
|
if (allow_funcs && func_key && input == KEY_MOUSE) {
|
|
|
|
if (do_mouse())
|
|
|
|
input = get_kbinput(edit, meta_key, func_key);
|
|
|
|
else
|
|
|
|
input = ERR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check for a shortcut in the main list. */
|
|
|
|
s = get_shortcut(main_list, &input, meta_key, func_key);
|
|
|
|
|
|
|
|
/* If we got a shortcut from the main list, or a "universal"
|
|
|
|
* edit window shortcut, set have_shortcut to TRUE. */
|
|
|
|
have_shortcut = (s != NULL || input == NANO_XON_KEY ||
|
|
|
|
input == NANO_XOFF_KEY || input == NANO_SUSPEND_KEY);
|
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
/* Check for a toggle in the main list. */
|
|
|
|
t = get_toggle(input, *meta_key);
|
|
|
|
|
|
|
|
/* If we got a toggle from the main list, set have_toggle to
|
|
|
|
* TRUE. */
|
|
|
|
have_toggle = (t != NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set s_or_t to TRUE if we got a shortcut or toggle. */
|
|
|
|
*s_or_t = (have_shortcut
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
|| have_toggle
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
if (allow_funcs) {
|
|
|
|
/* If we got a character, and it isn't a shortcut, toggle, or
|
|
|
|
* control character, it's a normal text character. Display the
|
|
|
|
* warning if we're in view mode, or add the character to the
|
|
|
|
* input buffer if we're not. */
|
|
|
|
if (input != ERR && *s_or_t == FALSE && !is_cntrl_char(input)) {
|
|
|
|
if (ISSET(VIEW_MODE))
|
|
|
|
print_view_warning();
|
|
|
|
else {
|
|
|
|
kbinput_len++;
|
|
|
|
kbinput = (int *)nrealloc(kbinput, kbinput_len *
|
|
|
|
sizeof(int));
|
|
|
|
kbinput[kbinput_len - 1] = input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we got a shortcut or toggle, or if there aren't any other
|
|
|
|
* characters waiting after the one we read in, we need to
|
|
|
|
* display all the characters in the input buffer if it isn't
|
|
|
|
* empty. Note that it should be empty if we're in view
|
|
|
|
* mode. */
|
|
|
|
if (*s_or_t == TRUE || get_buffer_len() == 0) {
|
|
|
|
if (kbinput != NULL) {
|
|
|
|
/* Display all the characters in the input buffer at
|
|
|
|
* once. */
|
|
|
|
do_output(kbinput, kbinput_len);
|
|
|
|
|
|
|
|
/* Empty the input buffer. */
|
|
|
|
kbinput_len = 0;
|
|
|
|
free(kbinput);
|
|
|
|
kbinput = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_shortcut) {
|
|
|
|
switch (input) {
|
|
|
|
/* Handle the "universal" edit window shortcuts. */
|
|
|
|
case NANO_XON_KEY:
|
|
|
|
statusbar(_("XON ignored, mumble mumble."));
|
|
|
|
break;
|
|
|
|
case NANO_XOFF_KEY:
|
|
|
|
statusbar(_("XOFF ignored, mumble mumble."));
|
|
|
|
break;
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
case NANO_SUSPEND_KEY:
|
|
|
|
if (ISSET(SUSPEND))
|
|
|
|
do_suspend(0);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
/* Handle the normal edit window shortcuts. */
|
|
|
|
default:
|
|
|
|
/* Blow away the text in the cutbuffer if we aren't
|
|
|
|
* cutting text. */
|
|
|
|
if (s->func != do_cut_text)
|
|
|
|
cutbuffer_reset();
|
|
|
|
|
|
|
|
/* Run the function associated with this shortcut,
|
|
|
|
* if there is one. */
|
|
|
|
if (s->func != NULL) {
|
|
|
|
if (ISSET(VIEW_MODE) && !s->viewok)
|
|
|
|
print_view_warning();
|
|
|
|
else
|
|
|
|
s->func();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
else if (have_toggle) {
|
|
|
|
/* Blow away the text in the cutbuffer, since we aren't
|
|
|
|
* cutting text. */
|
|
|
|
cutbuffer_reset();
|
|
|
|
/* Toggle the flag associated with this shortcut. */
|
|
|
|
if (allow_funcs)
|
|
|
|
do_toggle(t);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
/* Blow away the text in the cutbuffer, since we aren't
|
|
|
|
* cutting text. */
|
|
|
|
cutbuffer_reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef DISABLE_MOUSE
|
|
|
|
bool do_mouse(void)
|
|
|
|
{
|
|
|
|
int mouse_x, mouse_y;
|
|
|
|
bool retval;
|
|
|
|
|
|
|
|
retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
|
|
|
|
|
|
|
|
if (!retval) {
|
|
|
|
/* We can click in the edit window to move the cursor. */
|
|
|
|
if (wenclose(edit, mouse_y, mouse_x)) {
|
|
|
|
bool sameline;
|
|
|
|
/* Did they click on the line with the cursor? If they
|
|
|
|
* clicked on the cursor, we set the mark. */
|
|
|
|
size_t xcur;
|
|
|
|
/* The character they clicked on. */
|
|
|
|
|
|
|
|
/* Subtract out the size of topwin. Perhaps we need a
|
|
|
|
* constant somewhere? */
|
|
|
|
mouse_y -= 2;
|
|
|
|
|
|
|
|
sameline = (mouse_y == current_y);
|
|
|
|
|
|
|
|
/* Move to where the click occurred. */
|
|
|
|
for (; current_y < mouse_y && current->next != NULL; current_y++)
|
|
|
|
current = current->next;
|
|
|
|
for (; current_y > mouse_y && current->prev != NULL; current_y--)
|
|
|
|
current = current->prev;
|
|
|
|
|
|
|
|
xcur = actual_x(current->data, get_page_start(xplustabs()) +
|
|
|
|
mouse_x);
|
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
/* Clicking where the cursor is toggles the mark, as does
|
|
|
|
* clicking beyond the line length with the cursor at the
|
|
|
|
* end of the line. */
|
|
|
|
if (sameline && xcur == current_x) {
|
|
|
|
if (ISSET(VIEW_MODE)) {
|
|
|
|
print_view_warning();
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
do_mark();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
current_x = xcur;
|
|
|
|
placewewant = xplustabs();
|
|
|
|
edit_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* FIXME: If we clicked on a location in the statusbar, the cursor
|
|
|
|
* should move to the location we clicked on. This functionality
|
|
|
|
* should be in do_statusbar_mouse() when it's written. */
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
#endif /* !DISABLE_MOUSE */
|
|
|
|
|
2004-12-07 18:05:38 +00:00
|
|
|
/* The user typed kbinput_len wide characters. Add them to the edit
|
|
|
|
* buffer as multibyte characters. */
|
2004-12-04 17:41:52 +00:00
|
|
|
void do_output(int *kbinput, size_t kbinput_len)
|
|
|
|
{
|
|
|
|
size_t i, current_len = strlen(current->data);
|
|
|
|
bool old_constupdate = ISSET(CONSTUPDATE);
|
|
|
|
bool do_refresh = FALSE;
|
|
|
|
/* Do we have to call edit_refresh(), or can we get away with
|
|
|
|
* update_line()? */
|
|
|
|
|
2004-12-07 22:24:03 +00:00
|
|
|
char key[
|
2004-12-04 17:41:52 +00:00
|
|
|
#ifdef NANO_WIDE
|
2004-12-07 22:24:03 +00:00
|
|
|
MB_LEN_MAX
|
2004-12-04 17:41:52 +00:00
|
|
|
#else
|
|
|
|
1
|
|
|
|
#endif
|
2004-12-07 22:24:03 +00:00
|
|
|
]; /* The current multibyte character we have. */
|
2004-12-07 21:23:39 +00:00
|
|
|
int key_len; /* The length of the current multibyte
|
|
|
|
* character. */
|
2004-12-04 17:41:52 +00:00
|
|
|
|
|
|
|
assert(current != NULL && current->data != NULL);
|
|
|
|
|
2004-12-05 04:18:26 +00:00
|
|
|
/* Turn off constant cursor position display. */
|
|
|
|
UNSET(CONSTUPDATE);
|
2004-12-04 17:41:52 +00:00
|
|
|
|
|
|
|
for (i = 0; i < kbinput_len; i++) {
|
|
|
|
#ifdef NANO_WIDE
|
|
|
|
/* Change the wide character to its multibyte value. If it's
|
|
|
|
* invalid, go on to the next character. */
|
|
|
|
if (!ISSET(NO_UTF8)) {
|
|
|
|
key_len = wctomb(key, kbinput[i]);
|
|
|
|
|
|
|
|
if (key_len == -1)
|
|
|
|
continue;
|
2004-12-06 04:14:42 +00:00
|
|
|
/* Interpret the character as a single-byte sequence. */
|
2004-12-04 17:41:52 +00:00
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
key_len = 1;
|
2004-12-07 05:02:21 +00:00
|
|
|
key[0] = (unsigned char)kbinput[i];
|
2004-12-04 17:41:52 +00:00
|
|
|
#ifdef NANO_WIDE
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Null to newline, if needed. */
|
|
|
|
if (key[0] == '\0' && key_len == 1)
|
|
|
|
key[0] = '\n';
|
|
|
|
/* Newline to Enter, if needed. */
|
|
|
|
else if (key[0] == '\n' && key_len == 1) {
|
|
|
|
do_enter();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When a character is inserted on the current magicline, it
|
|
|
|
* means we need a new one! */
|
|
|
|
if (filebot == current)
|
|
|
|
new_magicline();
|
|
|
|
|
|
|
|
/* More dangerousness fun =) */
|
|
|
|
current->data = charealloc(current->data,
|
|
|
|
current_len + key_len + 1);
|
|
|
|
assert(current_x <= current_len);
|
|
|
|
charmove(¤t->data[current_x + key_len],
|
|
|
|
¤t->data[current_x],
|
|
|
|
current_len - current_x + key_len);
|
|
|
|
charcpy(¤t->data[current_x], key, key_len);
|
|
|
|
current_len += key_len;
|
|
|
|
/* FIXME: Should totsize be the number of single-byte characters
|
|
|
|
* or the number of multibyte characters? Assume for former for
|
|
|
|
* now. */
|
|
|
|
totsize += key_len;
|
|
|
|
set_modified();
|
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
|
|
|
/* Note that current_x has not yet been incremented. */
|
|
|
|
if (current == mark_beginbuf && current_x < mark_beginx)
|
|
|
|
mark_beginx += key_len;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
2004-12-07 21:23:39 +00:00
|
|
|
/* FIXME: The movement functions should take multibyte
|
|
|
|
* characters into account. */
|
2004-12-04 17:41:52 +00:00
|
|
|
int j;
|
|
|
|
for (j = 0; j < key_len; j++)
|
|
|
|
do_right(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef DISABLE_WRAPPING
|
|
|
|
/* If we're wrapping text, we need to call edit_refresh(). */
|
|
|
|
if (!ISSET(NO_WRAP) && (key[0] != '\t' || key_len != 1)) {
|
|
|
|
bool do_refresh_save = do_refresh;
|
|
|
|
|
|
|
|
do_refresh = do_wrap(current);
|
|
|
|
|
|
|
|
/* If we needed to call edit_refresh() before this, we'll
|
|
|
|
* still need to after this. */
|
|
|
|
if (do_refresh_save)
|
|
|
|
do_refresh = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
/* If color syntaxes are turned on, we need to call
|
|
|
|
* edit_refresh(). */
|
|
|
|
if (ISSET(COLOR_SYNTAX))
|
|
|
|
do_refresh = TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-12-05 04:18:26 +00:00
|
|
|
/* Turn constant cursor position display back on if it was on
|
|
|
|
* before. */
|
2004-12-04 17:41:52 +00:00
|
|
|
if (old_constupdate)
|
|
|
|
SET(CONSTUPDATE);
|
|
|
|
|
|
|
|
if (do_refresh)
|
|
|
|
edit_refresh();
|
|
|
|
else
|
|
|
|
update_line(current, current_x);
|
|
|
|
}
|
|
|
|
|
2004-08-17 05:23:38 +00:00
|
|
|
int main(int argc, char **argv)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
|
|
|
int optchr;
|
2004-09-05 21:40:31 +00:00
|
|
|
int startline = 0;
|
|
|
|
/* Line to try and start at. */
|
2004-08-12 19:48:21 +00:00
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
2004-09-05 21:40:31 +00:00
|
|
|
bool fill_flag_used = FALSE;
|
|
|
|
/* Was the fill option used? */
|
2004-08-12 19:48:21 +00:00
|
|
|
#endif
|
2004-09-05 21:40:31 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
|
|
|
bool old_multibuffer;
|
|
|
|
/* The old value of the multibuffer option, restored after we
|
|
|
|
* load all files on the command line. */
|
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
#ifdef HAVE_GETOPT_LONG
|
2002-12-22 16:30:00 +00:00
|
|
|
const struct option long_options[] = {
|
2002-06-28 22:45:14 +00:00
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
|
|
|
{"multibuffer", 0, 0, 'F'},
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_NANORC
|
2003-09-06 21:44:37 +00:00
|
|
|
#ifndef NANO_SMALL
|
2003-01-16 23:44:46 +00:00
|
|
|
{"historylog", 0, 0, 'H'},
|
2003-09-06 21:44:37 +00:00
|
|
|
#endif
|
2002-07-19 01:08:59 +00:00
|
|
|
{"ignorercfiles", 0, 0, 'I'},
|
2002-06-28 22:45:14 +00:00
|
|
|
#endif
|
2004-12-04 17:41:52 +00:00
|
|
|
#ifdef NANO_WIDE
|
|
|
|
{"noutf8", 0, 0, 'O'},
|
|
|
|
#endif
|
2002-06-28 22:45:14 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
|
|
|
{"quotestr", 1, 0, 'Q'},
|
|
|
|
#endif
|
2000-09-06 13:39:17 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2000-10-26 01:44:42 +00:00
|
|
|
{"regexp", 0, 0, 'R'},
|
2000-07-07 02:35:34 +00:00
|
|
|
#endif
|
2002-06-28 22:45:14 +00:00
|
|
|
{"tabsize", 1, 0, 'T'},
|
2000-06-06 05:53:49 +00:00
|
|
|
{"version", 0, 0, 'V'},
|
2002-06-28 22:45:14 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
{"syntax", 1, 0, 'Y'},
|
2001-05-21 12:56:25 +00:00
|
|
|
#endif
|
2002-06-28 22:45:14 +00:00
|
|
|
{"const", 0, 0, 'c'},
|
2003-08-17 02:48:43 +00:00
|
|
|
{"rebinddelete", 0, 0, 'd'},
|
2002-06-28 22:45:14 +00:00
|
|
|
{"nofollow", 0, 0, 'l'},
|
2003-10-03 20:26:25 +00:00
|
|
|
#ifndef DISABLE_MOUSE
|
2000-06-06 05:53:49 +00:00
|
|
|
{"mouse", 0, 0, 'm'},
|
2002-12-22 16:30:00 +00:00
|
|
|
#endif
|
2001-09-19 03:19:43 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
|
|
|
{"operatingdir", 1, 0, 'o'},
|
|
|
|
#endif
|
2003-01-05 23:35:44 +00:00
|
|
|
{"preserve", 0, 0, 'p'},
|
2002-06-28 22:45:14 +00:00
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
|
|
|
{"fill", 1, 0, 'r'},
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SPELLER
|
|
|
|
{"speller", 1, 0, 's'},
|
2001-07-11 02:08:33 +00:00
|
|
|
#endif
|
2002-06-28 22:45:14 +00:00
|
|
|
{"tempfile", 0, 0, 't'},
|
|
|
|
{"view", 0, 0, 'v'},
|
2002-10-17 02:19:31 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2002-06-28 22:45:14 +00:00
|
|
|
{"nowrap", 0, 0, 'w'},
|
2002-10-17 02:19:31 +00:00
|
|
|
#endif
|
2002-06-28 22:45:14 +00:00
|
|
|
{"nohelp", 0, 0, 'x'},
|
|
|
|
{"suspend", 0, 0, 'z'},
|
2001-09-22 19:02:04 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-05-22 20:15:20 +00:00
|
|
|
{"smarthome", 0, 0, 'A'},
|
2002-06-28 22:45:14 +00:00
|
|
|
{"backup", 0, 0, 'B'},
|
2004-02-28 16:24:31 +00:00
|
|
|
{"backupdir", 1, 0, 'E'},
|
2002-06-28 22:45:14 +00:00
|
|
|
{"noconvert", 0, 0, 'N'},
|
2001-09-22 19:02:04 +00:00
|
|
|
{"smooth", 0, 0, 'S'},
|
2004-04-30 04:49:02 +00:00
|
|
|
{"restricted", 0, 0, 'Z'},
|
2002-06-28 22:45:14 +00:00
|
|
|
{"autoindent", 0, 0, 'i'},
|
|
|
|
{"cut", 0, 0, 'k'},
|
2002-05-04 04:23:30 +00:00
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
2004-07-27 15:46:58 +00:00
|
|
|
#ifdef ENABLE_NLS
|
2000-06-06 05:53:49 +00:00
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
#endif
|
|
|
|
|
2003-01-13 01:35:15 +00:00
|
|
|
#if !defined(ENABLE_NANORC) && defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING)
|
2004-09-05 21:40:31 +00:00
|
|
|
/* If we don't have rcfile support, we're root, and
|
|
|
|
* --disable-wrapping-as-root is used, turn wrapping off. */
|
2004-03-04 06:33:52 +00:00
|
|
|
if (geteuid() == NANO_ROOT_UID)
|
2002-10-25 16:08:53 +00:00
|
|
|
SET(NO_WRAP);
|
|
|
|
#endif
|
2001-04-18 04:28:54 +00:00
|
|
|
|
2004-08-07 22:00:02 +00:00
|
|
|
while ((optchr =
|
2000-06-06 05:53:49 +00:00
|
|
|
#ifdef HAVE_GETOPT_LONG
|
2004-12-04 17:41:52 +00:00
|
|
|
getopt_long(argc, argv, "h?ABE:FHINOQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz", long_options, NULL)
|
2000-06-06 05:53:49 +00:00
|
|
|
#else
|
2004-12-04 17:41:52 +00:00
|
|
|
getopt(argc, argv, "h?ABE:FHINOQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz")
|
2000-06-06 05:53:49 +00:00
|
|
|
#endif
|
2004-08-07 22:00:02 +00:00
|
|
|
) != -1) {
|
2000-06-06 05:53:49 +00:00
|
|
|
|
|
|
|
switch (optchr) {
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'a':
|
|
|
|
case 'b':
|
|
|
|
case 'e':
|
|
|
|
case 'f':
|
|
|
|
case 'g':
|
|
|
|
case 'j':
|
|
|
|
/* Pico compatibility flags. */
|
|
|
|
break;
|
2001-09-22 00:42:10 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'A':
|
|
|
|
SET(SMART_HOME);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
SET(BACKUP_FILE);
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
backup_dir = mallocstrcpy(backup_dir, optarg);
|
|
|
|
break;
|
2001-09-22 00:42:10 +00:00
|
|
|
#endif
|
2001-07-14 19:32:47 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'F':
|
|
|
|
SET(MULTIBUFFER);
|
|
|
|
break;
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_NANORC
|
2003-09-06 21:44:37 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'H':
|
|
|
|
SET(HISTORYLOG);
|
|
|
|
break;
|
2003-09-06 21:44:37 +00:00
|
|
|
#endif
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'I':
|
|
|
|
SET(NO_RCFILE);
|
|
|
|
break;
|
2001-07-11 02:08:33 +00:00
|
|
|
#endif
|
2001-09-22 04:20:25 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'N':
|
|
|
|
SET(NO_CONVERT);
|
|
|
|
break;
|
2002-03-03 22:36:36 +00:00
|
|
|
#endif
|
2004-12-04 17:41:52 +00:00
|
|
|
case 'O':
|
|
|
|
SET(NO_UTF8);
|
|
|
|
break;
|
2002-03-03 22:36:36 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'Q':
|
|
|
|
quotestr = mallocstrcpy(quotestr, optarg);
|
|
|
|
break;
|
2002-02-15 19:17:02 +00:00
|
|
|
#endif
|
2000-09-06 13:39:17 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'R':
|
|
|
|
SET(USE_REGEXP);
|
|
|
|
break;
|
2001-09-22 19:02:04 +00:00
|
|
|
#endif
|
|
|
|
#ifndef NANO_SMALL
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'S':
|
|
|
|
SET(SMOOTHSCROLL);
|
|
|
|
break;
|
2000-07-07 02:35:34 +00:00
|
|
|
#endif
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'T':
|
|
|
|
if (!parse_num(optarg, &tabsize) || tabsize <= 0) {
|
2004-08-11 05:13:08 +00:00
|
|
|
fprintf(stderr, _("Requested tab size %s invalid"), optarg);
|
|
|
|
fprintf(stderr, "\n");
|
2004-08-07 22:00:02 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
version();
|
|
|
|
exit(0);
|
2002-05-04 04:23:30 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'Y':
|
|
|
|
syntaxstr = mallocstrcpy(syntaxstr, optarg);
|
|
|
|
break;
|
2002-05-04 04:23:30 +00:00
|
|
|
#endif
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'Z':
|
|
|
|
SET(RESTRICTED);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
SET(CONSTUPDATE);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
SET(REBIND_DELETE);
|
|
|
|
break;
|
2001-09-17 13:48:00 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'i':
|
|
|
|
SET(AUTOINDENT);
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
SET(CUT_TO_END);
|
|
|
|
break;
|
2000-07-12 18:14:51 +00:00
|
|
|
#endif
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'l':
|
|
|
|
SET(NOFOLLOW_SYMLINKS);
|
|
|
|
break;
|
2003-10-03 20:26:25 +00:00
|
|
|
#ifndef DISABLE_MOUSE
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'm':
|
|
|
|
SET(USE_MOUSE);
|
|
|
|
break;
|
2002-12-22 16:30:00 +00:00
|
|
|
#endif
|
2001-09-19 03:19:43 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'o':
|
|
|
|
operating_dir = mallocstrcpy(operating_dir, optarg);
|
|
|
|
break;
|
2001-09-19 03:19:43 +00:00
|
|
|
#endif
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'p':
|
|
|
|
SET(PRESERVE);
|
|
|
|
break;
|
2001-05-21 12:56:25 +00:00
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'r':
|
|
|
|
if (!parse_num(optarg, &wrap_at)) {
|
2004-08-11 05:13:08 +00:00
|
|
|
fprintf(stderr, _("Requested fill size %s invalid"), optarg);
|
|
|
|
fprintf(stderr, "\n");
|
2004-08-07 22:00:02 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fill_flag_used = TRUE;
|
|
|
|
break;
|
2001-05-21 12:56:25 +00:00
|
|
|
#endif
|
2001-01-12 07:51:05 +00:00
|
|
|
#ifndef DISABLE_SPELLER
|
2004-08-07 22:00:02 +00:00
|
|
|
case 's':
|
|
|
|
alt_speller = mallocstrcpy(alt_speller, optarg);
|
|
|
|
break;
|
2001-01-12 07:51:05 +00:00
|
|
|
#endif
|
2004-08-07 22:00:02 +00:00
|
|
|
case 't':
|
|
|
|
SET(TEMP_FILE);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
SET(VIEW_MODE);
|
|
|
|
break;
|
2003-01-02 16:32:20 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'w':
|
|
|
|
SET(NO_WRAP);
|
|
|
|
break;
|
2002-12-22 16:30:00 +00:00
|
|
|
#endif
|
2004-08-07 22:00:02 +00:00
|
|
|
case 'x':
|
|
|
|
SET(NO_HELP);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
SET(SUSPEND);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-29 01:20:17 +00:00
|
|
|
/* If the executable filename starts with 'r', we use restricted
|
|
|
|
* mode. */
|
2004-04-30 04:49:02 +00:00
|
|
|
if (*(tail(argv[0])) == 'r')
|
|
|
|
SET(RESTRICTED);
|
|
|
|
|
2004-05-29 01:20:17 +00:00
|
|
|
/* If we're using restricted mode, disable suspending, backups, and
|
|
|
|
* reading rcfiles, since they all would allow reading from or
|
|
|
|
* writing to files not specified on the command line. */
|
2004-04-30 04:49:02 +00:00
|
|
|
if (ISSET(RESTRICTED)) {
|
|
|
|
UNSET(SUSPEND);
|
|
|
|
UNSET(BACKUP_FILE);
|
|
|
|
SET(NO_RCFILE);
|
|
|
|
}
|
|
|
|
|
2003-01-13 01:35:15 +00:00
|
|
|
/* We've read through the command line options. Now back up the flags
|
2004-09-05 21:40:31 +00:00
|
|
|
* and values that are set, and read the rcfile(s). If the values
|
|
|
|
* haven't changed afterward, restore the backed-up values. */
|
2003-01-13 01:35:15 +00:00
|
|
|
#ifdef ENABLE_NANORC
|
|
|
|
if (!ISSET(NO_RCFILE)) {
|
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
|
|
|
char *operating_dir_cpy = operating_dir;
|
|
|
|
#endif
|
2004-08-12 20:06:20 +00:00
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
2004-07-12 16:07:14 +00:00
|
|
|
ssize_t wrap_at_cpy = wrap_at;
|
2003-01-13 01:35:15 +00:00
|
|
|
#endif
|
2004-02-28 16:24:31 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
char *backup_dir_cpy = backup_dir;
|
|
|
|
#endif
|
2003-01-13 01:35:15 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
|
|
|
char *quotestr_cpy = quotestr;
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SPELLER
|
|
|
|
char *alt_speller_cpy = alt_speller;
|
|
|
|
#endif
|
2004-07-12 16:07:14 +00:00
|
|
|
ssize_t tabsize_cpy = tabsize;
|
2003-01-13 01:35:15 +00:00
|
|
|
long flags_cpy = flags;
|
|
|
|
|
2003-02-05 02:39:34 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2003-01-13 01:35:15 +00:00
|
|
|
operating_dir = NULL;
|
2003-02-05 02:39:34 +00:00
|
|
|
#endif
|
2004-02-28 16:24:31 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
backup_dir = NULL;
|
|
|
|
#endif
|
2003-02-05 02:39:34 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
2003-01-13 01:35:15 +00:00
|
|
|
quotestr = NULL;
|
2003-02-05 02:39:34 +00:00
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SPELLER
|
2003-01-13 01:35:15 +00:00
|
|
|
alt_speller = NULL;
|
2003-02-05 02:39:34 +00:00
|
|
|
#endif
|
2003-01-13 01:35:15 +00:00
|
|
|
|
|
|
|
do_rcfile();
|
|
|
|
|
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
|
|
|
if (operating_dir_cpy != NULL) {
|
|
|
|
free(operating_dir);
|
|
|
|
operating_dir = operating_dir_cpy;
|
|
|
|
}
|
|
|
|
#endif
|
2004-08-12 19:48:21 +00:00
|
|
|
#ifndef DISABLE_WRAPJUSTIFY
|
2003-01-13 01:35:15 +00:00
|
|
|
if (fill_flag_used)
|
|
|
|
wrap_at = wrap_at_cpy;
|
|
|
|
#endif
|
2004-02-28 16:24:31 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
if (backup_dir_cpy != NULL) {
|
|
|
|
free(backup_dir);
|
|
|
|
backup_dir = backup_dir_cpy;
|
|
|
|
}
|
|
|
|
#endif
|
2003-01-13 01:35:15 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
|
|
|
if (quotestr_cpy != NULL) {
|
|
|
|
free(quotestr);
|
|
|
|
quotestr = quotestr_cpy;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SPELLER
|
|
|
|
if (alt_speller_cpy != NULL) {
|
|
|
|
free(alt_speller);
|
|
|
|
alt_speller = alt_speller_cpy;
|
|
|
|
}
|
|
|
|
#endif
|
2004-07-18 18:13:54 +00:00
|
|
|
if (tabsize_cpy != -1)
|
2003-01-13 01:35:15 +00:00
|
|
|
tabsize = tabsize_cpy;
|
|
|
|
flags |= flags_cpy;
|
|
|
|
}
|
|
|
|
#if defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING)
|
2004-03-04 06:33:52 +00:00
|
|
|
else if (geteuid() == NANO_ROOT_UID)
|
2003-01-13 01:35:15 +00:00
|
|
|
SET(NO_WRAP);
|
|
|
|
#endif
|
|
|
|
#endif /* ENABLE_NANORC */
|
|
|
|
|
2003-03-11 03:50:40 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
history_init();
|
|
|
|
#ifdef ENABLE_NANORC
|
|
|
|
if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG))
|
|
|
|
load_history();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2004-02-28 16:24:31 +00:00
|
|
|
#ifndef NANO_SMALL
|
2004-05-28 23:45:25 +00:00
|
|
|
/* Set up the backup directory (unless we're using restricted mode,
|
2004-05-29 01:20:17 +00:00
|
|
|
* in which case backups are disabled, since they would allow
|
|
|
|
* reading from or writing to files not specified on the command
|
|
|
|
* line). This entails making sure it exists and is a directory, so
|
|
|
|
* that backup files will be saved there. */
|
2004-05-28 23:45:25 +00:00
|
|
|
if (!ISSET(RESTRICTED))
|
|
|
|
init_backup_dir();
|
2004-02-28 16:24:31 +00:00
|
|
|
#endif
|
|
|
|
|
2002-09-13 18:14:04 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2003-01-13 01:35:15 +00:00
|
|
|
/* Set up the operating directory. This entails chdir()ing there,
|
2004-05-28 23:45:25 +00:00
|
|
|
* so that file reads and writes will be based there. */
|
2002-09-13 18:14:04 +00:00
|
|
|
init_operating_dir();
|
|
|
|
#endif
|
|
|
|
|
2003-01-13 01:35:15 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
2004-05-29 16:38:57 +00:00
|
|
|
if (punct == NULL)
|
2004-06-12 21:03:14 +00:00
|
|
|
punct = mallocstrcpy(punct, ".?!");
|
2004-05-29 16:38:57 +00:00
|
|
|
|
|
|
|
if (brackets == NULL)
|
2004-06-12 21:03:14 +00:00
|
|
|
brackets = mallocstrcpy(brackets, "'\")}]>");
|
2004-05-29 16:38:57 +00:00
|
|
|
|
2003-01-13 01:35:15 +00:00
|
|
|
if (quotestr == NULL)
|
2004-06-12 21:03:14 +00:00
|
|
|
quotestr = mallocstrcpy(NULL,
|
2003-01-13 01:35:15 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
2004-06-12 21:03:14 +00:00
|
|
|
"^([ \t]*[|>:}#])+"
|
2003-01-13 01:35:15 +00:00
|
|
|
#else
|
2004-06-12 21:03:14 +00:00
|
|
|
"> "
|
2003-01-13 01:35:15 +00:00
|
|
|
#endif
|
2004-06-12 21:03:14 +00:00
|
|
|
);
|
2004-07-30 03:54:34 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
quoterc = regcomp("ereg, quotestr, REG_EXTENDED);
|
|
|
|
|
|
|
|
if (quoterc == 0) {
|
|
|
|
/* We no longer need quotestr, just quotereg. */
|
|
|
|
free(quotestr);
|
|
|
|
quotestr = NULL;
|
|
|
|
} else {
|
|
|
|
size_t size = regerror(quoterc, "ereg, NULL, 0);
|
|
|
|
|
|
|
|
quoteerr = charalloc(size);
|
|
|
|
regerror(quoterc, "ereg, quoteerr, size);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
quotelen = strlen(quotestr);
|
|
|
|
#endif /* !HAVE_REGEX_H */
|
2003-01-13 01:35:15 +00:00
|
|
|
#endif /* !DISABLE_JUSTIFY */
|
2004-02-28 16:24:31 +00:00
|
|
|
|
2004-05-28 22:42:41 +00:00
|
|
|
#ifndef DISABLE_SPELLER
|
|
|
|
/* If we don't have an alternative spell checker after reading the
|
2004-05-29 01:20:17 +00:00
|
|
|
* command line and/or rcfile(s), check $SPELL for one, as Pico
|
2004-05-28 23:45:25 +00:00
|
|
|
* does (unless we're using restricted mode, in which case spell
|
2004-05-29 01:20:17 +00:00
|
|
|
* checking is disabled, since it would allow reading from or
|
|
|
|
* writing to files not specified on the command line). */
|
2004-05-28 23:45:25 +00:00
|
|
|
if (!ISSET(RESTRICTED) && alt_speller == NULL) {
|
2004-05-28 22:42:41 +00:00
|
|
|
char *spellenv = getenv("SPELL");
|
|
|
|
if (spellenv != NULL)
|
|
|
|
alt_speller = mallocstrcpy(NULL, spellenv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-29 16:25:30 +00:00
|
|
|
#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
|
2004-09-05 21:40:31 +00:00
|
|
|
/* If whitespace wasn't specified, set its default value. */
|
2004-05-29 16:25:30 +00:00
|
|
|
if (whitespace == NULL)
|
|
|
|
whitespace = mallocstrcpy(NULL, " ");
|
|
|
|
#endif
|
|
|
|
|
2004-09-05 21:40:31 +00:00
|
|
|
/* If tabsize wasn't specified, set its default value. */
|
2003-01-13 01:35:15 +00:00
|
|
|
if (tabsize == -1)
|
2004-09-05 21:40:31 +00:00
|
|
|
tabsize = WIDTH_OF_TAB;
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
/* Back up the old terminal settings so that they can be restored. */
|
2002-03-29 16:00:59 +00:00
|
|
|
tcgetattr(0, &oldterm);
|
2002-03-29 15:38:17 +00:00
|
|
|
|
2004-09-05 21:40:31 +00:00
|
|
|
/* Curses initialization stuff: Start curses and set up the
|
|
|
|
* terminal state. */
|
2000-06-06 05:53:49 +00:00
|
|
|
initscr();
|
2004-07-27 16:46:35 +00:00
|
|
|
terminal_init();
|
2004-01-12 03:28:06 +00:00
|
|
|
|
2004-02-16 20:32:40 +00:00
|
|
|
/* Set up the global variables and the shortcuts. */
|
2004-07-01 18:59:52 +00:00
|
|
|
global_init(FALSE);
|
|
|
|
shortcut_init(FALSE);
|
2004-02-16 20:32:40 +00:00
|
|
|
|
|
|
|
/* Set up the signal handlers. */
|
2002-03-29 16:00:59 +00:00
|
|
|
signal_init();
|
2000-06-06 05:53:49 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2003-08-05 19:31:12 +00:00
|
|
|
fprintf(stderr, "Main: set up windows\n");
|
2000-06-06 05:53:49 +00:00
|
|
|
#endif
|
|
|
|
|
2001-11-29 02:42:27 +00:00
|
|
|
window_init();
|
2003-10-03 20:26:25 +00:00
|
|
|
#ifndef DISABLE_MOUSE
|
2001-11-29 02:42:27 +00:00
|
|
|
mouse_init();
|
2002-12-22 16:30:00 +00:00
|
|
|
#endif
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
#ifdef DEBUG
|
2003-08-05 19:31:12 +00:00
|
|
|
fprintf(stderr, "Main: open file\n");
|
2000-06-06 05:53:49 +00:00
|
|
|
#endif
|
2004-09-05 21:40:31 +00:00
|
|
|
|
2004-10-22 20:25:56 +00:00
|
|
|
/* If there's a +LINE flag here, it is the first non-option
|
|
|
|
* argument, and it is followed by at least one other argument, the
|
|
|
|
* filename it applies to. */
|
|
|
|
if (0 < optind && optind < argc - 1 && argv[optind][0] == '+') {
|
|
|
|
startline = atoi(&argv[optind][1]);
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
2003-01-13 01:35:15 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2004-09-05 21:40:31 +00:00
|
|
|
old_multibuffer = ISSET(MULTIBUFFER);
|
|
|
|
SET(MULTIBUFFER);
|
|
|
|
|
|
|
|
/* Read all the files after the first one on the command line into
|
|
|
|
* new buffers. */
|
|
|
|
{
|
2004-10-22 20:25:56 +00:00
|
|
|
int i = optind + 1, iline = 0;
|
|
|
|
for (; i < argc; i++) {
|
|
|
|
/* If there's a +LINE flag here, it is followed by at least
|
|
|
|
* one other argument, the filename it applies to. */
|
|
|
|
if (i < argc - 1 && argv[i][0] == '+' && iline == 0) {
|
|
|
|
iline = atoi(&argv[i][1]);
|
|
|
|
} else {
|
|
|
|
load_buffer(argv[i]);
|
|
|
|
if (iline > 0) {
|
|
|
|
do_gotoline(iline, FALSE);
|
|
|
|
iline = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-05 21:40:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Read the first file on the command line into either the current
|
|
|
|
* buffer or a new buffer, depending on whether multibuffer mode is
|
|
|
|
* enabled. */
|
|
|
|
if (optind < argc)
|
|
|
|
load_buffer(argv[optind]);
|
|
|
|
|
|
|
|
/* We didn't open any files if all the command line arguments were
|
|
|
|
* invalid files like directories or if there were no command line
|
|
|
|
* arguments given. In this case, we have to load a blank buffer.
|
|
|
|
* Also, we unset view mode to allow editing. */
|
|
|
|
if (filename == NULL) {
|
|
|
|
filename = mallocstrcpy(NULL, "");
|
|
|
|
new_file();
|
|
|
|
UNSET(VIEW_MODE);
|
|
|
|
|
|
|
|
/* Add this new entry to the open_files structure if we have
|
|
|
|
* multibuffer support, or to the main filestruct if we don't. */
|
|
|
|
load_file();
|
2003-01-13 01:35:15 +00:00
|
|
|
}
|
2004-09-05 21:40:31 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
|
|
|
if (!old_multibuffer)
|
|
|
|
UNSET(MULTIBUFFER);
|
2003-01-13 01:35:15 +00:00
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-09-05 21:40:31 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Main: top and bottom win\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
titlebar(NULL);
|
|
|
|
display_main_list();
|
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
if (startline > 0)
|
2004-07-12 16:07:14 +00:00
|
|
|
do_gotoline(startline, FALSE);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-05-18 01:20:36 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
/* Return here after a SIGWINCH. */
|
2001-04-12 03:01:53 +00:00
|
|
|
sigsetjmp(jmpbuf, 1);
|
2004-05-18 01:20:36 +00:00
|
|
|
#endif
|
2001-01-29 23:37:54 +00:00
|
|
|
|
2000-07-08 14:23:32 +00:00
|
|
|
edit_refresh();
|
|
|
|
|
2004-03-15 20:26:30 +00:00
|
|
|
while (TRUE) {
|
2004-12-04 17:41:52 +00:00
|
|
|
bool meta_key;
|
|
|
|
/* Whether we got a meta key sequence. */
|
|
|
|
bool func_key;
|
|
|
|
/* Whether we got a function key. */
|
|
|
|
bool s_or_t;
|
|
|
|
/* Whether we got a shortcut or toggle. */
|
|
|
|
|
|
|
|
/* Make sure the cursor is in the edit window. */
|
2004-07-13 17:09:24 +00:00
|
|
|
reset_cursor();
|
2004-12-04 17:41:52 +00:00
|
|
|
|
|
|
|
/* If constant cursor position display is on, display the cursor
|
|
|
|
* position. */
|
2003-01-28 01:16:47 +00:00
|
|
|
if (ISSET(CONSTUPDATE))
|
2004-07-01 19:41:09 +00:00
|
|
|
do_cursorpos(TRUE);
|
2003-01-28 01:16:47 +00:00
|
|
|
|
2003-10-03 20:26:25 +00:00
|
|
|
#if !defined(DISABLE_BROWSER) || !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
|
2001-04-12 03:01:53 +00:00
|
|
|
currshortcut = main_list;
|
2001-05-21 12:56:25 +00:00
|
|
|
#endif
|
2001-04-12 03:01:53 +00:00
|
|
|
|
2004-12-04 17:41:52 +00:00
|
|
|
/* Read in and interpret characters. */
|
|
|
|
do_input(&meta_key, &func_key, &s_or_t, TRUE);
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
2004-03-19 21:57:56 +00:00
|
|
|
assert(FALSE);
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|