2005-07-24 19:57:51 +00:00
|
|
|
/**************************************************************************
|
2005-10-08 06:12:41 +00:00
|
|
|
* text.c *
|
2005-07-24 19:57:51 +00:00
|
|
|
* *
|
2009-12-02 03:36:22 +00:00
|
|
|
* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
|
2014-04-30 20:18:26 +00:00
|
|
|
* 2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc. *
|
2005-07-24 19:57:51 +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 *
|
2007-08-11 05:17:36 +00:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option) *
|
2005-07-24 19:57:51 +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., 51 Franklin St, Fifth Floor, Boston, MA *
|
|
|
|
* 02110-1301, USA. *
|
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-02 19:42:02 +00:00
|
|
|
#include <stdio.h>
|
2005-07-24 19:57:51 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-11-08 16:45:22 +00:00
|
|
|
static pid_t pid = -1;
|
2005-12-08 07:09:08 +00:00
|
|
|
/* The PID of the forked process in execute_command(), for use
|
|
|
|
* with the cancel_command() signal handler. */
|
2005-07-24 19:57:51 +00:00
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_WRAPPING
|
2005-11-25 13:48:09 +00:00
|
|
|
static bool prepend_wrap = FALSE;
|
2005-07-24 19:57:51 +00:00
|
|
|
/* Should we prepend wrapped text to the next line? */
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_JUSTIFY
|
|
|
|
static filestruct *jusbottom = NULL;
|
2005-09-20 19:46:39 +00:00
|
|
|
/* Pointer to the end of the justify buffer. */
|
2005-07-24 19:57:51 +00:00
|
|
|
#endif
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Toggle the mark. */
|
2005-07-25 02:41:59 +00:00
|
|
|
void do_mark(void)
|
|
|
|
{
|
|
|
|
openfile->mark_set = !openfile->mark_set;
|
|
|
|
if (openfile->mark_set) {
|
|
|
|
statusbar(_("Mark Set"));
|
|
|
|
openfile->mark_begin = openfile->current;
|
|
|
|
openfile->mark_begin_x = openfile->current_x;
|
|
|
|
} else {
|
2006-07-31 01:30:31 +00:00
|
|
|
statusbar(_("Mark Unset"));
|
2005-07-25 02:41:59 +00:00
|
|
|
openfile->mark_begin = NULL;
|
|
|
|
openfile->mark_begin_x = 0;
|
|
|
|
edit_refresh();
|
|
|
|
}
|
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2005-07-25 02:41:59 +00:00
|
|
|
|
2015-04-17 09:24:17 +00:00
|
|
|
#if !defined(DISABLE_COLOR) || !defined(DISABLE_SPELLER)
|
|
|
|
/* Return an error message containing the given name. */
|
|
|
|
char *invocation_error(const char *name)
|
|
|
|
{
|
|
|
|
char *message, *invoke_error = _("Error invoking \"%s\"");
|
|
|
|
|
|
|
|
message = charalloc(strlen(invoke_error) + strlen(name) + 1);
|
|
|
|
sprintf(message, invoke_error, name);
|
|
|
|
return message;
|
|
|
|
}
|
2015-06-28 14:12:25 +00:00
|
|
|
#endif
|
2015-04-17 09:24:17 +00:00
|
|
|
|
2006-12-10 17:57:09 +00:00
|
|
|
/* Delete the character under the cursor. */
|
2014-06-08 19:02:12 +00:00
|
|
|
void do_deletion(undo_type action)
|
2005-07-25 02:41:59 +00:00
|
|
|
{
|
2014-06-09 20:26:54 +00:00
|
|
|
#ifndef NANO_TINY
|
2009-12-12 22:21:20 +00:00
|
|
|
size_t orig_lenpt = 0;
|
2014-06-09 20:26:54 +00:00
|
|
|
#endif
|
2005-07-25 02:41:59 +00:00
|
|
|
|
2015-06-28 14:12:25 +00:00
|
|
|
assert(openfile->current != NULL && openfile->current->data != NULL &&
|
|
|
|
openfile->current_x <= strlen(openfile->current->data));
|
2005-07-25 02:41:59 +00:00
|
|
|
|
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
|
|
|
|
if (openfile->current->data[openfile->current_x] != '\0') {
|
2016-06-06 18:29:53 +00:00
|
|
|
/* We're in the middle of a line: delete the current character. */
|
2005-07-25 02:41:59 +00:00
|
|
|
int char_buf_len = parse_mbchar(openfile->current->data +
|
2016-06-06 18:29:53 +00:00
|
|
|
openfile->current_x, NULL, NULL);
|
2005-07-25 02:41:59 +00:00
|
|
|
size_t line_len = strlen(openfile->current->data +
|
2016-06-06 18:29:53 +00:00
|
|
|
openfile->current_x);
|
2005-07-25 02:41:59 +00:00
|
|
|
|
|
|
|
assert(openfile->current_x < strlen(openfile->current->data));
|
|
|
|
|
2014-05-29 18:50:13 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-06-08 19:02:12 +00:00
|
|
|
update_undo(action);
|
2014-06-09 20:26:54 +00:00
|
|
|
|
2009-12-12 22:21:20 +00:00
|
|
|
if (ISSET(SOFTWRAP))
|
|
|
|
orig_lenpt = strlenpt(openfile->current->data);
|
2014-06-09 20:26:54 +00:00
|
|
|
#endif
|
2009-12-12 22:21:20 +00:00
|
|
|
|
2016-06-06 18:29:53 +00:00
|
|
|
/* Move the remainder of the line "in", over the current character. */
|
2005-07-25 02:41:59 +00:00
|
|
|
charmove(&openfile->current->data[openfile->current_x],
|
2016-06-06 18:29:53 +00:00
|
|
|
&openfile->current->data[openfile->current_x + char_buf_len],
|
|
|
|
line_len - char_buf_len + 1);
|
2005-07-25 02:41:59 +00:00
|
|
|
null_at(&openfile->current->data, openfile->current_x +
|
|
|
|
line_len - char_buf_len);
|
2016-06-06 18:29:53 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-06-06 18:29:53 +00:00
|
|
|
/* Adjust the mark if it is after the cursor on the current line. */
|
2015-06-28 14:12:25 +00:00
|
|
|
if (openfile->mark_set && openfile->mark_begin == openfile->current &&
|
2016-06-06 18:29:53 +00:00
|
|
|
openfile->mark_begin_x > openfile->current_x)
|
2005-07-25 02:41:59 +00:00
|
|
|
openfile->mark_begin_x -= char_buf_len;
|
|
|
|
#endif
|
2016-06-06 18:29:53 +00:00
|
|
|
/* Adjust the file size. */
|
2005-07-25 02:41:59 +00:00
|
|
|
openfile->totsize--;
|
2005-11-03 21:08:39 +00:00
|
|
|
} else if (openfile->current != openfile->filebot) {
|
2016-06-06 18:29:53 +00:00
|
|
|
/* We're at the end of a line and not at the end of the file: join
|
|
|
|
* this line with the next. */
|
|
|
|
filestruct *joining = openfile->current->next;
|
2005-07-25 02:41:59 +00:00
|
|
|
|
|
|
|
assert(openfile->current_x == strlen(openfile->current->data));
|
|
|
|
|
2016-06-06 18:29:53 +00:00
|
|
|
/* If there is a magic line, and we're before it: don't eat it. */
|
|
|
|
if (joining == openfile->filebot && openfile->current_x != 0 &&
|
|
|
|
!ISSET(NO_NEWLINES)) {
|
2015-07-06 19:08:13 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (action == BACK)
|
|
|
|
add_undo(BACK);
|
|
|
|
#endif
|
2015-07-06 18:48:15 +00:00
|
|
|
return;
|
2015-07-06 19:08:13 +00:00
|
|
|
}
|
2015-07-06 18:48:15 +00:00
|
|
|
|
2014-05-29 18:50:13 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-06-08 19:02:12 +00:00
|
|
|
add_undo(action);
|
2014-05-29 18:50:13 +00:00
|
|
|
#endif
|
2016-06-06 18:29:53 +00:00
|
|
|
/* Add the contents of the next line to those of the current one. */
|
2005-07-25 02:41:59 +00:00
|
|
|
openfile->current->data = charealloc(openfile->current->data,
|
2016-06-06 18:29:53 +00:00
|
|
|
strlen(openfile->current->data) + strlen(joining->data) + 1);
|
|
|
|
strcat(openfile->current->data, joining->data);
|
2015-12-08 19:09:14 +00:00
|
|
|
|
2016-06-06 17:57:51 +00:00
|
|
|
/* Adjust the file size. */
|
|
|
|
openfile->totsize--;
|
2016-06-04 09:35:11 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-06-06 17:57:51 +00:00
|
|
|
/* Remember the new file size for a possible redo. */
|
|
|
|
openfile->current_undo->newsize = openfile->totsize;
|
|
|
|
|
2016-06-06 18:29:53 +00:00
|
|
|
/* Adjust the mark if it was on the line that was "eaten". */
|
|
|
|
if (openfile->mark_set && openfile->mark_begin == joining) {
|
2005-07-25 02:41:59 +00:00
|
|
|
openfile->mark_begin = openfile->current;
|
|
|
|
openfile->mark_begin_x += openfile->current_x;
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-06 18:29:53 +00:00
|
|
|
unlink_node(joining);
|
2005-07-25 02:41:59 +00:00
|
|
|
renumber(openfile->current);
|
2015-06-20 09:42:26 +00:00
|
|
|
|
2015-06-28 14:04:03 +00:00
|
|
|
/* Two lines were joined, so we need to refresh the screen. */
|
2016-04-25 19:14:18 +00:00
|
|
|
refresh_needed = TRUE;
|
2005-07-25 02:41:59 +00:00
|
|
|
} else
|
2016-06-06 18:29:53 +00:00
|
|
|
/* We're at the end-of-file: nothing to do. */
|
2005-07-25 02:41:59 +00:00
|
|
|
return;
|
|
|
|
|
2014-06-09 20:26:54 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-04-25 19:14:18 +00:00
|
|
|
if (ISSET(SOFTWRAP) && refresh_needed == FALSE)
|
2009-12-12 22:21:20 +00:00
|
|
|
if (strlenpt(openfile->current->data) / COLS != orig_lenpt / COLS)
|
2016-04-25 19:14:18 +00:00
|
|
|
refresh_needed = TRUE;
|
2014-06-09 20:26:54 +00:00
|
|
|
#endif
|
2009-12-12 22:21:20 +00:00
|
|
|
|
2005-11-09 00:08:29 +00:00
|
|
|
set_modified();
|
2005-07-25 02:41:59 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 14:12:25 +00:00
|
|
|
/* Delete the character under the cursor. */
|
2014-06-08 19:02:12 +00:00
|
|
|
void do_delete(void)
|
|
|
|
{
|
|
|
|
do_deletion(DEL);
|
|
|
|
}
|
|
|
|
|
2005-12-31 21:22:54 +00:00
|
|
|
/* Backspace over one character. That is, move the cursor left one
|
2006-11-08 13:05:50 +00:00
|
|
|
* character, and then delete the character under the cursor. */
|
2005-07-25 02:41:59 +00:00
|
|
|
void do_backspace(void)
|
|
|
|
{
|
2015-06-28 14:12:25 +00:00
|
|
|
if (openfile->current != openfile->fileage || openfile->current_x > 0) {
|
2005-09-13 04:53:44 +00:00
|
|
|
do_left();
|
2014-06-08 19:02:12 +00:00
|
|
|
do_deletion(BACK);
|
2005-07-25 02:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 11:52:26 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* Delete text from the cursor until the first start of a word to
|
|
|
|
* the right, or to the left when backward is true. */
|
|
|
|
void do_cutword(bool backward)
|
|
|
|
{
|
|
|
|
/* Remember the current cursor position. */
|
|
|
|
filestruct *is_current = openfile->current;
|
|
|
|
size_t is_current_x = openfile->current_x;
|
|
|
|
|
2016-03-20 11:10:31 +00:00
|
|
|
/* Remember where the cutbuffer is and then make it seem blank. */
|
|
|
|
filestruct *is_cutbuffer = cutbuffer;
|
|
|
|
filestruct *is_cutbottom = cutbottom;
|
|
|
|
cutbuffer = NULL;
|
|
|
|
cutbottom = NULL;
|
|
|
|
|
2015-07-31 11:52:26 +00:00
|
|
|
/* Move the cursor to a word start, to the left or to the right. */
|
|
|
|
if (backward)
|
|
|
|
do_prev_word(ISSET(WORD_BOUNDS), FALSE);
|
|
|
|
else
|
|
|
|
do_next_word(ISSET(WORD_BOUNDS), FALSE);
|
|
|
|
|
|
|
|
/* Set the mark at the start of that word. */
|
|
|
|
openfile->mark_begin = openfile->current;
|
|
|
|
openfile->mark_begin_x = openfile->current_x;
|
|
|
|
openfile->mark_set = TRUE;
|
|
|
|
|
|
|
|
/* Put the cursor back where it was, so an undo will put it there too. */
|
|
|
|
openfile->current = is_current;
|
|
|
|
openfile->current_x = is_current_x;
|
|
|
|
|
|
|
|
/* Now kill the marked region and a word is gone. */
|
|
|
|
do_cut_text_void();
|
2016-03-20 11:10:31 +00:00
|
|
|
|
|
|
|
/* Discard the cut word and restore the cutbuffer. */
|
|
|
|
free_filestruct(cutbuffer);
|
|
|
|
cutbuffer = is_cutbuffer;
|
|
|
|
cutbottom = is_cutbottom;
|
2015-07-31 11:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a word leftward. */
|
|
|
|
void do_cut_prev_word(void)
|
|
|
|
{
|
2016-05-30 07:09:36 +00:00
|
|
|
do_cutword(TRUE);
|
2015-07-31 11:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a word rightward. */
|
|
|
|
void do_cut_next_word(void)
|
|
|
|
{
|
2016-05-30 07:09:36 +00:00
|
|
|
do_cutword(FALSE);
|
2015-07-31 11:52:26 +00:00
|
|
|
}
|
|
|
|
#endif /* !NANO_TINY */
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number
|
|
|
|
* of spaces that a tab would normally take up. */
|
2005-07-25 02:41:59 +00:00
|
|
|
void do_tab(void)
|
|
|
|
{
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:41:59 +00:00
|
|
|
if (ISSET(TABS_TO_SPACES)) {
|
|
|
|
char *output;
|
2005-10-07 15:57:48 +00:00
|
|
|
size_t output_len = 0, new_pww = xplustabs();
|
2005-07-25 02:41:59 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
new_pww++;
|
|
|
|
output_len++;
|
|
|
|
} while (new_pww % tabsize != 0);
|
|
|
|
|
|
|
|
output = charalloc(output_len + 1);
|
|
|
|
|
|
|
|
charset(output, ' ', output_len);
|
|
|
|
output[output_len] = '\0';
|
|
|
|
|
|
|
|
do_output(output, output_len, TRUE);
|
|
|
|
|
|
|
|
free(output);
|
2015-06-28 14:12:25 +00:00
|
|
|
} else
|
2005-07-25 02:41:59 +00:00
|
|
|
#endif
|
2009-11-29 06:13:22 +00:00
|
|
|
do_output((char *) "\t", 1, TRUE);
|
2005-07-25 02:41:59 +00:00
|
|
|
}
|
|
|
|
|
2006-04-28 13:19:56 +00:00
|
|
|
#ifndef NANO_TINY
|
2007-01-11 22:54:55 +00:00
|
|
|
/* Indent or unindent the current line (or, if the mark is on, all lines
|
|
|
|
* covered by the mark) len columns, depending on whether len is
|
|
|
|
* positive or negative. If the TABS_TO_SPACES flag is set, indent or
|
|
|
|
* unindent by len spaces. Otherwise, indent or unindent by (len /
|
|
|
|
* tabsize) tabs and (len % tabsize) spaces. */
|
2006-07-05 18:42:22 +00:00
|
|
|
void do_indent(ssize_t cols)
|
2006-04-28 13:19:56 +00:00
|
|
|
{
|
|
|
|
bool indent_changed = FALSE;
|
|
|
|
/* Whether any indenting or unindenting was done. */
|
|
|
|
bool unindent = FALSE;
|
|
|
|
/* Whether we're unindenting text. */
|
2006-05-05 15:41:43 +00:00
|
|
|
char *line_indent = NULL;
|
2006-04-28 13:19:56 +00:00
|
|
|
/* The text added to each line in order to indent it. */
|
2006-05-05 15:41:43 +00:00
|
|
|
size_t line_indent_len = 0;
|
2006-04-28 13:19:56 +00:00
|
|
|
/* The length of the text added to each line in order to indent
|
|
|
|
* it. */
|
|
|
|
filestruct *top, *bot, *f;
|
|
|
|
size_t top_x, bot_x;
|
|
|
|
|
|
|
|
assert(openfile->current != NULL && openfile->current->data != NULL);
|
|
|
|
|
2006-05-01 17:14:25 +00:00
|
|
|
/* If cols is zero, get out. */
|
|
|
|
if (cols == 0)
|
2006-04-28 13:19:56 +00:00
|
|
|
return;
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* If cols is negative, make it positive and set unindent to TRUE. */
|
2006-05-01 17:14:25 +00:00
|
|
|
if (cols < 0) {
|
|
|
|
cols = -cols;
|
2006-04-28 13:19:56 +00:00
|
|
|
unindent = TRUE;
|
|
|
|
/* Otherwise, we're indenting, in which case the file will always be
|
|
|
|
* modified, so set indent_changed to TRUE. */
|
|
|
|
} else
|
|
|
|
indent_changed = TRUE;
|
|
|
|
|
2006-07-05 18:42:22 +00:00
|
|
|
/* If the mark is on, use all lines covered by the mark. */
|
|
|
|
if (openfile->mark_set)
|
|
|
|
mark_order((const filestruct **)&top, &top_x,
|
|
|
|
(const filestruct **)&bot, &bot_x, NULL);
|
|
|
|
/* Otherwise, use the current line. */
|
|
|
|
else {
|
|
|
|
top = openfile->current;
|
|
|
|
bot = top;
|
|
|
|
}
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-05-05 15:41:43 +00:00
|
|
|
if (!unindent) {
|
|
|
|
/* Set up the text we'll be using as indentation. */
|
|
|
|
line_indent = charalloc(cols + 1);
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-05-05 15:41:43 +00:00
|
|
|
if (ISSET(TABS_TO_SPACES)) {
|
|
|
|
/* Set the indentation to cols spaces. */
|
|
|
|
charset(line_indent, ' ', cols);
|
|
|
|
line_indent_len = cols;
|
|
|
|
} else {
|
|
|
|
/* Set the indentation to (cols / tabsize) tabs and (cols %
|
|
|
|
* tabsize) spaces. */
|
|
|
|
size_t num_tabs = cols / tabsize;
|
|
|
|
size_t num_spaces = cols % tabsize;
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-05-05 15:41:43 +00:00
|
|
|
charset(line_indent, '\t', num_tabs);
|
|
|
|
charset(line_indent + num_tabs, ' ', num_spaces);
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-05-05 15:41:43 +00:00
|
|
|
line_indent_len = num_tabs + num_spaces;
|
|
|
|
}
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-05-05 15:41:43 +00:00
|
|
|
line_indent[line_indent_len] = '\0';
|
|
|
|
}
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-07-05 18:42:22 +00:00
|
|
|
/* Go through each line of the text. */
|
2006-04-28 13:19:56 +00:00
|
|
|
for (f = top; f != bot->next; f = f->next) {
|
|
|
|
size_t line_len = strlen(f->data);
|
2006-05-01 16:48:12 +00:00
|
|
|
size_t indent_len = indent_length(f->data);
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-05-05 15:41:43 +00:00
|
|
|
if (!unindent) {
|
|
|
|
/* If we're indenting, add the characters in line_indent to
|
|
|
|
* the beginning of the non-whitespace text of this line. */
|
2015-06-28 14:12:25 +00:00
|
|
|
f->data = charealloc(f->data, line_len + line_indent_len + 1);
|
2006-05-05 15:41:43 +00:00
|
|
|
charmove(&f->data[indent_len + line_indent_len],
|
|
|
|
&f->data[indent_len], line_len - indent_len + 1);
|
|
|
|
strncpy(f->data + indent_len, line_indent, line_indent_len);
|
|
|
|
openfile->totsize += line_indent_len;
|
|
|
|
|
|
|
|
/* Keep track of the change in the current line. */
|
2006-07-05 18:42:22 +00:00
|
|
|
if (openfile->mark_set && f == openfile->mark_begin &&
|
2015-06-28 14:12:25 +00:00
|
|
|
openfile->mark_begin_x >= indent_len)
|
2006-05-05 15:41:43 +00:00
|
|
|
openfile->mark_begin_x += line_indent_len;
|
|
|
|
|
2015-06-28 14:12:25 +00:00
|
|
|
if (f == openfile->current && openfile->current_x >= indent_len)
|
2006-05-05 15:41:43 +00:00
|
|
|
openfile->current_x += line_indent_len;
|
|
|
|
|
|
|
|
/* If the NO_NEWLINES flag isn't set, and this is the
|
|
|
|
* magicline, add a new magicline. */
|
|
|
|
if (!ISSET(NO_NEWLINES) && f == openfile->filebot)
|
|
|
|
new_magicline();
|
|
|
|
} else {
|
2006-05-01 16:48:12 +00:00
|
|
|
size_t indent_col = strnlenpt(f->data, indent_len);
|
2015-06-28 14:12:25 +00:00
|
|
|
/* The length in columns of the indentation on this line. */
|
2006-05-01 16:48:12 +00:00
|
|
|
|
2006-05-01 17:14:25 +00:00
|
|
|
if (cols <= indent_col) {
|
2015-06-28 14:12:25 +00:00
|
|
|
size_t indent_new = actual_x(f->data, indent_col - cols);
|
2006-05-06 14:37:33 +00:00
|
|
|
/* The length of the indentation remaining on
|
|
|
|
* this line after we unindent. */
|
2006-05-01 16:48:12 +00:00
|
|
|
size_t indent_shift = indent_len - indent_new;
|
2006-05-06 14:37:33 +00:00
|
|
|
/* The change in the indentation on this line
|
|
|
|
* after we unindent. */
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-05-01 17:14:25 +00:00
|
|
|
/* If we're unindenting, and there's at least cols
|
2006-05-01 16:48:12 +00:00
|
|
|
* columns' worth of indentation at the beginning of the
|
|
|
|
* non-whitespace text of this line, remove it. */
|
|
|
|
charmove(&f->data[indent_new], &f->data[indent_len],
|
|
|
|
line_len - indent_shift - indent_new + 1);
|
|
|
|
null_at(&f->data, line_len - indent_shift + 1);
|
|
|
|
openfile->totsize -= indent_shift;
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2006-04-29 15:44:58 +00:00
|
|
|
/* Keep track of the change in the current line. */
|
2006-07-05 18:42:22 +00:00
|
|
|
if (openfile->mark_set && f == openfile->mark_begin &&
|
2006-05-05 14:22:42 +00:00
|
|
|
openfile->mark_begin_x > indent_new) {
|
|
|
|
if (openfile->mark_begin_x <= indent_len)
|
|
|
|
openfile->mark_begin_x = indent_new;
|
|
|
|
else
|
|
|
|
openfile->mark_begin_x -= indent_shift;
|
|
|
|
}
|
2006-04-28 13:19:56 +00:00
|
|
|
|
2015-06-28 14:12:25 +00:00
|
|
|
if (f == openfile->current &&
|
|
|
|
openfile->current_x > indent_new) {
|
2006-05-05 14:22:42 +00:00
|
|
|
if (openfile->current_x <= indent_len)
|
|
|
|
openfile->current_x = indent_new;
|
|
|
|
else
|
|
|
|
openfile->current_x -= indent_shift;
|
|
|
|
}
|
2006-04-29 16:11:21 +00:00
|
|
|
|
2015-06-28 14:12:25 +00:00
|
|
|
/* We've unindented, so the indentation changed. */
|
|
|
|
indent_changed = TRUE;
|
2006-04-28 13:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-05 15:41:43 +00:00
|
|
|
if (!unindent)
|
2006-05-05 15:43:52 +00:00
|
|
|
/* Clean up. */
|
2006-05-05 15:41:43 +00:00
|
|
|
free(line_indent);
|
2006-04-28 13:19:56 +00:00
|
|
|
|
|
|
|
if (indent_changed) {
|
2015-12-03 09:17:06 +00:00
|
|
|
/* Throw away the undo stack, to prevent making mistakes when
|
|
|
|
* the user tries to undo something in the reindented text. */
|
2016-01-15 16:44:50 +00:00
|
|
|
discard_until(NULL, openfile);
|
2015-12-03 09:17:06 +00:00
|
|
|
openfile->current_undo = NULL;
|
|
|
|
|
2006-04-28 13:19:56 +00:00
|
|
|
/* Mark the file as modified. */
|
|
|
|
set_modified();
|
|
|
|
|
|
|
|
/* Update the screen. */
|
2016-04-25 19:14:18 +00:00
|
|
|
refresh_needed = TRUE;
|
2006-04-28 13:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-05 18:42:22 +00:00
|
|
|
/* Indent the current line, or all lines covered by the mark if the mark
|
|
|
|
* is on, tabsize columns. */
|
|
|
|
void do_indent_void(void)
|
2006-04-28 13:19:56 +00:00
|
|
|
{
|
2006-07-05 18:42:22 +00:00
|
|
|
do_indent(tabsize);
|
2006-04-28 13:19:56 +00:00
|
|
|
}
|
|
|
|
|
2006-07-05 18:42:22 +00:00
|
|
|
/* Unindent the current line, or all lines covered by the mark if the
|
|
|
|
* mark is on, tabsize columns. */
|
|
|
|
void do_unindent(void)
|
2006-04-28 13:19:56 +00:00
|
|
|
{
|
2006-07-05 18:42:22 +00:00
|
|
|
do_indent(-tabsize);
|
2006-04-28 13:19:56 +00:00
|
|
|
}
|
2016-05-25 20:13:50 +00:00
|
|
|
#endif /* !NANO_TINY */
|
|
|
|
|
|
|
|
#ifdef ENABLE_COMMENT
|
|
|
|
/* Test whether the string is empty or consists of only blanks. */
|
|
|
|
bool white_string(const char *s)
|
|
|
|
{
|
|
|
|
while (*s != '\0' && (is_blank_mbchar(s) || *s == '\r'))
|
|
|
|
s += move_mbright(s, 0);
|
|
|
|
|
|
|
|
return !*s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Comment or uncomment the current line or the marked lines. */
|
|
|
|
void do_comment()
|
|
|
|
{
|
|
|
|
const char *comment_seq = "#";
|
|
|
|
undo_type action = UNCOMMENT;
|
|
|
|
filestruct *top, *bot, *f;
|
2016-06-01 07:36:05 +00:00
|
|
|
size_t top_x, bot_x;
|
2016-05-25 20:13:50 +00:00
|
|
|
bool empty, all_empty = TRUE;
|
|
|
|
|
|
|
|
assert(openfile->current != NULL && openfile->current->data != NULL);
|
|
|
|
|
|
|
|
#ifndef DISABLE_COLOR
|
|
|
|
if (openfile->syntax && openfile->syntax->comment)
|
|
|
|
comment_seq = openfile->syntax->comment;
|
|
|
|
|
|
|
|
/* Does the syntax not allow comments? */
|
|
|
|
if (strlen(comment_seq) == 0) {
|
|
|
|
statusbar(_("Commenting is not supported for this file type"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Determine which lines to work on. */
|
|
|
|
if (openfile->mark_set)
|
|
|
|
mark_order((const filestruct **) &top, &top_x,
|
|
|
|
(const filestruct **) &bot, &bot_x, NULL);
|
|
|
|
else {
|
|
|
|
top = openfile->current;
|
|
|
|
bot = top;
|
|
|
|
}
|
|
|
|
|
2016-06-01 07:36:05 +00:00
|
|
|
/* If only the magic line is selected, don't do anything. */
|
|
|
|
if (top == bot && bot == openfile->filebot && !ISSET(NO_NEWLINES)) {
|
|
|
|
statusbar(_("Cannot comment past end of file"));
|
|
|
|
return;
|
|
|
|
}
|
2016-05-25 20:13:50 +00:00
|
|
|
|
|
|
|
/* Figure out whether to comment or uncomment the selected line or lines. */
|
|
|
|
for (f = top; f != bot->next; f = f->next) {
|
|
|
|
empty = white_string(f->data);
|
|
|
|
|
|
|
|
/* If this line is not blank and not commented, we comment all. */
|
|
|
|
if (!empty && !comment_line(PREFLIGHT, f, comment_seq)) {
|
|
|
|
action = COMMENT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
all_empty = all_empty && empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If all selected lines are blank, we comment them. */
|
|
|
|
action = all_empty ? COMMENT : action;
|
|
|
|
|
2016-06-01 07:36:05 +00:00
|
|
|
add_undo(action);
|
|
|
|
|
|
|
|
/* Store the comment sequence used for the operation, because it could
|
|
|
|
* change when the file name changes; we need to know what it was. */
|
|
|
|
openfile->current_undo->strdata = mallocstrcpy(NULL, comment_seq);
|
|
|
|
|
2016-05-25 20:13:50 +00:00
|
|
|
/* Process the selected line or lines. */
|
|
|
|
for (f = top; f != bot->next; f = f->next) {
|
2016-06-01 07:36:05 +00:00
|
|
|
/* Comment/uncomment a line, and add undo data when line changed. */
|
|
|
|
if (comment_line(action, f, comment_seq))
|
2016-05-25 20:13:50 +00:00
|
|
|
update_comment_undo(f->lineno);
|
|
|
|
}
|
|
|
|
|
2016-06-01 07:36:05 +00:00
|
|
|
set_modified();
|
|
|
|
refresh_needed = TRUE;
|
2016-05-25 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test whether the given line can be uncommented, or add or remove a comment,
|
|
|
|
* depending on action. Return TRUE if the line is uncommentable, or when
|
|
|
|
* anything was added or removed; FALSE otherwise. */
|
|
|
|
bool comment_line(undo_type action, filestruct *f, const char *comment_seq)
|
|
|
|
{
|
|
|
|
size_t comment_seq_len = strlen(comment_seq);
|
|
|
|
const char *post_seq = strchr(comment_seq, '|');
|
|
|
|
/* The postfix, if this is a bracketing type comment sequence. */
|
|
|
|
size_t pre_len = post_seq ? post_seq++ - comment_seq : comment_seq_len;
|
|
|
|
/* Length of prefix. */
|
|
|
|
size_t post_len = post_seq ? comment_seq_len - pre_len - 1 : 0;
|
|
|
|
/* Length of postfix. */
|
|
|
|
size_t line_len = strlen(f->data);
|
|
|
|
|
|
|
|
if (!ISSET(NO_NEWLINES) && f == openfile->filebot)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (action == COMMENT) {
|
|
|
|
/* Make room for the comment sequence(s), move the text right and
|
|
|
|
* copy them in. */
|
|
|
|
f->data = charealloc(f->data, line_len + pre_len + post_len + 1);
|
|
|
|
charmove(&f->data[pre_len], f->data, line_len);
|
|
|
|
charmove(f->data, comment_seq, pre_len);
|
|
|
|
if (post_len)
|
|
|
|
charmove(&f->data[pre_len + line_len], post_seq, post_len);
|
|
|
|
f->data[pre_len + line_len + post_len] = '\0';
|
|
|
|
|
|
|
|
openfile->totsize += pre_len + post_len;
|
|
|
|
|
|
|
|
/* If needed, adjust the position of the mark and of the cursor. */
|
|
|
|
if (openfile->mark_set && f == openfile->mark_begin)
|
|
|
|
openfile->mark_begin_x += pre_len;
|
|
|
|
if (f == openfile->current) {
|
|
|
|
openfile->current_x += pre_len;
|
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the line is commented, report it as uncommentable, or uncomment it. */
|
|
|
|
if (strncmp(f->data, comment_seq, pre_len) == 0 && (post_len == 0 ||
|
|
|
|
strcmp(&f->data[line_len - post_len], post_seq) == 0)) {
|
2008-07-10 20:13:04 +00:00
|
|
|
|
2016-05-25 20:13:50 +00:00
|
|
|
if (action == PREFLIGHT)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* Erase the comment prefix by moving the non-comment part. */
|
|
|
|
charmove(f->data, &f->data[pre_len], line_len - pre_len);
|
|
|
|
/* Truncate the postfix if there was one. */
|
|
|
|
f->data[line_len - pre_len - post_len] = '\0';
|
|
|
|
|
|
|
|
openfile->totsize -= pre_len + post_len;
|
|
|
|
|
|
|
|
/* If needed, adjust the position of the mark and then the cursor. */
|
|
|
|
if (openfile->mark_set && f == openfile->mark_begin) {
|
|
|
|
if (openfile->mark_begin_x < pre_len)
|
|
|
|
openfile->mark_begin_x = 0;
|
|
|
|
else
|
|
|
|
openfile->mark_begin_x -= pre_len;
|
|
|
|
}
|
|
|
|
if (f == openfile->current) {
|
|
|
|
if (openfile->current_x < pre_len)
|
|
|
|
openfile->current_x = 0;
|
|
|
|
else
|
|
|
|
openfile->current_x -= pre_len;
|
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform an undo or redo for a comment or uncomment action. */
|
|
|
|
void handle_comment_action(undo *u, bool undoing, bool add_comment)
|
|
|
|
{
|
|
|
|
undo_group *group = u->grouping;
|
|
|
|
|
|
|
|
/* When redoing, reposition the cursor and let the commenter adjust it. */
|
|
|
|
if (!undoing)
|
|
|
|
goto_line_posx(u->lineno, u->begin);
|
|
|
|
|
|
|
|
while (group) {
|
|
|
|
filestruct *f = fsfromline(group->top_line);
|
|
|
|
|
|
|
|
while (f && f->lineno <= group->bottom_line) {
|
|
|
|
comment_line(undoing ^ add_comment ?
|
|
|
|
COMMENT : UNCOMMENT, f, u->strdata);
|
|
|
|
f = f->next;
|
|
|
|
}
|
|
|
|
group = group->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When undoing, reposition the cursor to the recorded location. */
|
|
|
|
if (undoing)
|
|
|
|
goto_line_posx(u->lineno, u->begin);
|
|
|
|
|
|
|
|
refresh_needed = TRUE;
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_COMMENT */
|
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2014-05-15 20:00:46 +00:00
|
|
|
#define redo_paste undo_cut
|
|
|
|
#define undo_paste redo_cut
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Undo a cut, or redo an uncut. */
|
2008-09-16 21:35:19 +00:00
|
|
|
void undo_cut(undo *u)
|
|
|
|
{
|
2014-04-04 20:45:28 +00:00
|
|
|
/* If we cut the magicline, we may as well not crash. :/ */
|
2009-07-27 04:16:44 +00:00
|
|
|
if (!u->cutbuffer)
|
|
|
|
return;
|
2008-09-16 21:35:19 +00:00
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Get to where we need to uncut from. */
|
2015-06-27 09:27:19 +00:00
|
|
|
if (u->xflags == WAS_WHOLE_LINE)
|
2014-05-25 19:41:49 +00:00
|
|
|
goto_line_posx(u->mark_begin_lineno, 0);
|
|
|
|
else
|
|
|
|
goto_line_posx(u->mark_begin_lineno, u->mark_begin_x);
|
2008-09-16 21:35:19 +00:00
|
|
|
|
2014-06-22 11:03:49 +00:00
|
|
|
copy_from_filestruct(u->cutbuffer);
|
2014-05-25 19:41:49 +00:00
|
|
|
|
2015-06-27 09:27:19 +00:00
|
|
|
if (u->xflags != WAS_MARKED_FORWARD && u->type != PASTE)
|
2014-05-25 19:41:49 +00:00
|
|
|
goto_line_posx(u->mark_begin_lineno, u->mark_begin_x);
|
2008-09-16 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Redo a cut, or undo an uncut. */
|
2014-04-08 18:38:45 +00:00
|
|
|
void redo_cut(undo *u)
|
|
|
|
{
|
2014-04-04 20:45:28 +00:00
|
|
|
/* If we cut the magicline, we may as well not crash. :/ */
|
2009-07-27 04:16:44 +00:00
|
|
|
if (!u->cutbuffer)
|
|
|
|
return;
|
|
|
|
|
2014-07-02 20:52:27 +00:00
|
|
|
filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom;
|
2015-12-01 11:51:10 +00:00
|
|
|
cutbuffer = cutbottom = NULL;
|
2014-07-02 20:52:27 +00:00
|
|
|
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
|
|
|
|
2014-07-02 20:29:57 +00:00
|
|
|
openfile->mark_set = TRUE;
|
2014-05-15 20:00:46 +00:00
|
|
|
openfile->mark_begin = fsfromline(u->mark_begin_lineno);
|
2015-06-27 09:27:19 +00:00
|
|
|
openfile->mark_begin_x = (u->xflags == WAS_WHOLE_LINE) ? 0 : u->mark_begin_x;
|
2014-06-22 11:03:49 +00:00
|
|
|
|
2015-11-02 13:46:40 +00:00
|
|
|
do_cut_text(FALSE, FALSE);
|
2014-06-22 11:03:49 +00:00
|
|
|
|
2016-02-18 19:58:18 +00:00
|
|
|
free_filestruct(cutbuffer);
|
2014-07-02 20:52:27 +00:00
|
|
|
cutbuffer = oldcutbuffer;
|
|
|
|
cutbottom = oldcutbottom;
|
2008-09-16 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Undo the last thing(s) we did. */
|
2008-07-10 20:13:04 +00:00
|
|
|
void do_undo(void)
|
|
|
|
{
|
|
|
|
undo *u = openfile->current_undo;
|
2016-05-08 08:51:40 +00:00
|
|
|
filestruct *f, *t = NULL;
|
2014-07-11 19:14:25 +00:00
|
|
|
char *data, *undidmsg = NULL;
|
2008-07-10 20:13:04 +00:00
|
|
|
|
|
|
|
if (!u) {
|
|
|
|
statusbar(_("Nothing in undo buffer!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-08 08:51:40 +00:00
|
|
|
f = fsfromline(u->mark_begin_lineno);
|
|
|
|
if (!f)
|
2008-07-10 20:13:04 +00:00
|
|
|
return;
|
2016-05-08 08:51:40 +00:00
|
|
|
|
2008-07-10 20:13:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-06-17 15:17:09 +00:00
|
|
|
fprintf(stderr, " >> Undoing a type %d...\n", u->type);
|
|
|
|
fprintf(stderr, " >> Data we're about to undo = \"%s\"\n", f->data);
|
2008-07-10 20:13:04 +00:00
|
|
|
#endif
|
|
|
|
|
2008-07-13 16:44:19 +00:00
|
|
|
openfile->current_x = u->begin;
|
2014-04-08 18:38:45 +00:00
|
|
|
switch (u->type) {
|
2008-07-10 20:13:04 +00:00
|
|
|
case ADD:
|
2016-02-05 12:27:54 +00:00
|
|
|
/* TRANSLATORS: Eight of the next nine strings describe actions
|
|
|
|
* that are undone or redone. It are all nouns, not verbs. */
|
2008-07-31 04:24:04 +00:00
|
|
|
undidmsg = _("text add");
|
2015-11-22 16:09:15 +00:00
|
|
|
data = charalloc(strlen(f->data) - strlen(u->strdata) + 1);
|
2014-07-16 08:53:16 +00:00
|
|
|
strncpy(data, f->data, u->begin);
|
2008-07-10 20:13:04 +00:00
|
|
|
strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]);
|
|
|
|
free(f->data);
|
|
|
|
f->data = data;
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2014-06-08 19:02:12 +00:00
|
|
|
case BACK:
|
2008-07-10 20:13:04 +00:00
|
|
|
case DEL:
|
2008-07-31 04:24:04 +00:00
|
|
|
undidmsg = _("text delete");
|
2015-11-22 16:09:15 +00:00
|
|
|
data = charalloc(strlen(f->data) + strlen(u->strdata) + 1);
|
2008-07-10 20:13:04 +00:00
|
|
|
strncpy(data, f->data, u->begin);
|
|
|
|
strcpy(&data[u->begin], u->strdata);
|
|
|
|
strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]);
|
|
|
|
free(f->data);
|
|
|
|
f->data = data;
|
2014-05-25 19:41:49 +00:00
|
|
|
goto_line_posx(u->mark_begin_lineno, u->mark_begin_x);
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2009-12-03 03:12:00 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2014-06-09 10:01:54 +00:00
|
|
|
case SPLIT_END:
|
|
|
|
goto_line_posx(u->lineno, u->begin);
|
|
|
|
openfile->current_undo = openfile->current_undo->next;
|
|
|
|
openfile->last_action = OTHER;
|
|
|
|
while (openfile->current_undo->type != SPLIT_BEGIN)
|
|
|
|
do_undo();
|
|
|
|
u = openfile->current_undo;
|
|
|
|
f = openfile->current;
|
2014-07-11 19:14:25 +00:00
|
|
|
case SPLIT_BEGIN:
|
|
|
|
undidmsg = _("text add");
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2015-06-28 14:12:25 +00:00
|
|
|
#endif
|
2014-06-18 20:11:52 +00:00
|
|
|
case JOIN:
|
2008-07-31 04:24:04 +00:00
|
|
|
undidmsg = _("line join");
|
2015-06-17 10:41:57 +00:00
|
|
|
/* When the join was done by a Backspace at the tail of the file,
|
2015-07-06 18:03:14 +00:00
|
|
|
* and the nonewlines flag isn't set, do not re-add a newline that
|
|
|
|
* wasn't actually deleted; just position the cursor. */
|
2015-12-04 21:11:10 +00:00
|
|
|
if (u->xflags == WAS_FINAL_BACKSPACE && !ISSET(NO_NEWLINES)) {
|
2015-07-06 18:03:14 +00:00
|
|
|
goto_line_posx(openfile->filebot->lineno, 0);
|
|
|
|
break;
|
|
|
|
}
|
2015-07-06 19:17:27 +00:00
|
|
|
t = make_new_node(f);
|
|
|
|
t->data = mallocstrcpy(NULL, u->strdata);
|
|
|
|
data = mallocstrncpy(NULL, f->data, u->mark_begin_x + 1);
|
|
|
|
data[u->mark_begin_x] = '\0';
|
|
|
|
free(f->data);
|
|
|
|
f->data = data;
|
2015-11-24 13:24:01 +00:00
|
|
|
splice_node(f, t);
|
2014-05-25 19:41:49 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2014-05-15 20:00:46 +00:00
|
|
|
case CUT_EOF:
|
2008-07-31 04:24:04 +00:00
|
|
|
case CUT:
|
|
|
|
undidmsg = _("text cut");
|
2014-07-16 08:53:16 +00:00
|
|
|
undo_cut(u);
|
2014-07-16 08:46:42 +00:00
|
|
|
f = fsfromline(u->lineno);
|
2008-09-16 21:35:19 +00:00
|
|
|
break;
|
2014-05-15 20:00:46 +00:00
|
|
|
case PASTE:
|
2008-09-16 21:35:19 +00:00
|
|
|
undidmsg = _("text uncut");
|
2014-05-15 20:00:46 +00:00
|
|
|
undo_paste(u);
|
2016-06-14 14:01:52 +00:00
|
|
|
f = fsfromline(u->mark_begin_lineno);
|
2008-07-31 04:24:04 +00:00
|
|
|
break;
|
2009-04-21 05:34:08 +00:00
|
|
|
case ENTER:
|
2015-11-25 09:49:27 +00:00
|
|
|
if (f->next == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Internal error: line is missing. "
|
|
|
|
"Please save your work."));
|
2015-11-25 09:49:27 +00:00
|
|
|
break;
|
2009-04-21 05:34:08 +00:00
|
|
|
}
|
2015-11-25 09:49:27 +00:00
|
|
|
undidmsg = _("line break");
|
2015-11-25 09:27:25 +00:00
|
|
|
f->data = charealloc(f->data, strlen(f->data) +
|
|
|
|
strlen(&f->next->data[u->mark_begin_x]) + 1);
|
|
|
|
strcat(f->data, &f->next->data[u->mark_begin_x]);
|
2015-11-25 09:49:27 +00:00
|
|
|
unlink_node(f->next);
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2009-04-21 05:34:08 +00:00
|
|
|
break;
|
2016-05-25 20:13:50 +00:00
|
|
|
#ifdef ENABLE_COMMENT
|
|
|
|
case COMMENT:
|
|
|
|
handle_comment_action(u, TRUE, TRUE);
|
|
|
|
undidmsg = _("comment");
|
|
|
|
break;
|
|
|
|
case UNCOMMENT:
|
|
|
|
handle_comment_action(u, TRUE, FALSE);
|
|
|
|
undidmsg = _("uncomment");
|
|
|
|
break;
|
|
|
|
#endif
|
2008-08-03 04:48:05 +00:00
|
|
|
case INSERT:
|
|
|
|
undidmsg = _("text insert");
|
2014-06-22 11:03:49 +00:00
|
|
|
filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom;
|
2008-08-03 04:48:05 +00:00
|
|
|
cutbuffer = NULL;
|
|
|
|
cutbottom = NULL;
|
2015-11-22 16:08:28 +00:00
|
|
|
/* Instead of a line number, u->mark_begin_lineno contains the number
|
|
|
|
* of lines of the inserted segment, because the file was partitioned
|
|
|
|
* when update_undo() was called; so, calculate the end-line number. */
|
2008-08-03 04:48:05 +00:00
|
|
|
openfile->mark_begin = fsfromline(u->lineno + u->mark_begin_lineno - 1);
|
2015-11-12 19:01:57 +00:00
|
|
|
openfile->mark_begin_x = u->mark_begin_x;
|
2008-08-03 04:48:05 +00:00
|
|
|
openfile->mark_set = TRUE;
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2008-08-03 04:48:05 +00:00
|
|
|
cut_marked();
|
2016-02-18 19:58:18 +00:00
|
|
|
free_filestruct(u->cutbuffer);
|
2008-08-03 04:48:05 +00:00
|
|
|
u->cutbuffer = cutbuffer;
|
|
|
|
u->cutbottom = cutbottom;
|
|
|
|
cutbuffer = oldcutbuffer;
|
|
|
|
cutbottom = oldcutbottom;
|
|
|
|
openfile->mark_set = FALSE;
|
|
|
|
break;
|
2008-08-02 22:31:01 +00:00
|
|
|
case REPLACE:
|
|
|
|
undidmsg = _("text replace");
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2008-08-02 22:31:01 +00:00
|
|
|
data = u->strdata;
|
|
|
|
u->strdata = f->data;
|
|
|
|
f->data = data;
|
|
|
|
break;
|
2008-07-10 20:13:04 +00:00
|
|
|
default:
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Internal error: unknown type. "
|
|
|
|
"Please save your work."));
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-04-14 09:57:06 +00:00
|
|
|
|
2014-07-11 19:14:25 +00:00
|
|
|
if (undidmsg)
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Undid action (%s)"), undidmsg);
|
2014-06-17 15:37:34 +00:00
|
|
|
|
|
|
|
renumber(f);
|
2008-07-10 20:13:04 +00:00
|
|
|
openfile->current_undo = openfile->current_undo->next;
|
2008-08-08 03:02:03 +00:00
|
|
|
openfile->last_action = OTHER;
|
2016-06-07 11:04:51 +00:00
|
|
|
openfile->mark_set = FALSE;
|
2014-06-17 15:50:34 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
2015-11-30 16:21:51 +00:00
|
|
|
openfile->totsize = u->wassize;
|
2014-05-15 20:00:46 +00:00
|
|
|
set_modified();
|
2008-07-10 20:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Redo the last thing(s) we undid. */
|
2008-07-10 20:13:04 +00:00
|
|
|
void do_redo(void)
|
|
|
|
{
|
2016-05-08 08:51:40 +00:00
|
|
|
filestruct *f;
|
2014-07-11 19:14:25 +00:00
|
|
|
char *data, *redidmsg = NULL;
|
2015-10-28 20:49:16 +00:00
|
|
|
undo *u = openfile->undotop;
|
2008-07-10 20:13:04 +00:00
|
|
|
|
2015-10-29 17:02:13 +00:00
|
|
|
if (u == NULL || u == openfile->current_undo) {
|
|
|
|
statusbar(_("Nothing to re-do!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-28 20:49:16 +00:00
|
|
|
/* Get the previous undo item. */
|
2015-10-29 09:04:30 +00:00
|
|
|
while (u != NULL && u->next != openfile->current_undo)
|
2015-10-28 20:49:16 +00:00
|
|
|
u = u->next;
|
|
|
|
|
2015-10-29 17:02:13 +00:00
|
|
|
if (u->next != openfile->current_undo) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Internal error: cannot set up redo. "
|
|
|
|
"Please save your work."));
|
2008-07-10 20:13:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-08 08:51:40 +00:00
|
|
|
f = fsfromline(u->type == INSERT ? 1 : u->mark_begin_lineno);
|
|
|
|
if (!f)
|
2008-07-10 20:13:04 +00:00
|
|
|
return;
|
2016-05-08 08:51:40 +00:00
|
|
|
|
2008-07-10 20:13:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-04 21:11:10 +00:00
|
|
|
fprintf(stderr, " >> Redo running for type %d\n", u->type);
|
|
|
|
fprintf(stderr, " >> Data we're about to redo = \"%s\"\n", f->data);
|
2008-07-10 20:13:04 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-08 18:38:45 +00:00
|
|
|
switch (u->type) {
|
2008-07-10 20:13:04 +00:00
|
|
|
case ADD:
|
2014-06-22 21:26:56 +00:00
|
|
|
redidmsg = _("text add");
|
2015-11-22 16:09:15 +00:00
|
|
|
data = charalloc(strlen(f->data) + strlen(u->strdata) + 1);
|
2008-08-01 04:11:57 +00:00
|
|
|
strncpy(data, f->data, u->begin);
|
2008-07-10 20:13:04 +00:00
|
|
|
strcpy(&data[u->begin], u->strdata);
|
|
|
|
strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]);
|
|
|
|
free(f->data);
|
|
|
|
f->data = data;
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->mark_begin_lineno, u->mark_begin_x);
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2014-06-08 19:02:12 +00:00
|
|
|
case BACK:
|
2008-07-10 20:13:04 +00:00
|
|
|
case DEL:
|
2014-06-22 21:26:56 +00:00
|
|
|
redidmsg = _("text delete");
|
2015-11-22 16:09:15 +00:00
|
|
|
data = charalloc(strlen(f->data) + strlen(u->strdata) + 1);
|
2014-07-16 08:53:16 +00:00
|
|
|
strncpy(data, f->data, u->begin);
|
2008-07-10 20:13:04 +00:00
|
|
|
strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]);
|
|
|
|
free(f->data);
|
|
|
|
f->data = data;
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2009-04-21 05:34:08 +00:00
|
|
|
case ENTER:
|
2014-06-22 21:26:56 +00:00
|
|
|
redidmsg = _("line break");
|
2015-11-11 18:51:39 +00:00
|
|
|
filestruct *shoveline = make_new_node(f);
|
|
|
|
shoveline->data = mallocstrcpy(NULL, u->strdata);
|
|
|
|
data = mallocstrncpy(NULL, f->data, u->begin + 1);
|
|
|
|
data[u->begin] = '\0';
|
|
|
|
free(f->data);
|
|
|
|
f->data = data;
|
2015-11-24 13:24:01 +00:00
|
|
|
splice_node(f, shoveline);
|
2015-11-11 18:51:39 +00:00
|
|
|
renumber(shoveline);
|
|
|
|
goto_line_posx(u->lineno + 1, u->mark_begin_x);
|
2009-04-21 05:34:08 +00:00
|
|
|
break;
|
2009-12-03 03:12:00 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2014-06-09 10:01:54 +00:00
|
|
|
case SPLIT_BEGIN:
|
|
|
|
goto_line_posx(u->lineno, u->begin);
|
|
|
|
openfile->current_undo = u;
|
|
|
|
openfile->last_action = OTHER;
|
|
|
|
while (openfile->current_undo->type != SPLIT_END)
|
|
|
|
do_redo();
|
|
|
|
u = openfile->current_undo;
|
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2014-07-11 19:14:25 +00:00
|
|
|
case SPLIT_END:
|
|
|
|
redidmsg = _("text add");
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2015-06-28 14:12:25 +00:00
|
|
|
#endif
|
2014-06-18 20:11:52 +00:00
|
|
|
case JOIN:
|
2015-11-26 09:31:33 +00:00
|
|
|
if (f->next == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Internal error: line is missing. "
|
|
|
|
"Please save your work."));
|
2015-11-26 09:31:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-06-22 21:26:56 +00:00
|
|
|
redidmsg = _("line join");
|
2015-11-26 08:45:22 +00:00
|
|
|
/* When the join was done by a Backspace at the tail of the file,
|
|
|
|
* and the nonewlines flag isn't set, do not join anything, as
|
|
|
|
* nothing was actually deleted; just position the cursor. */
|
|
|
|
if (u->xflags == WAS_FINAL_BACKSPACE && !ISSET(NO_NEWLINES)) {
|
|
|
|
goto_line_posx(u->mark_begin_lineno, u->mark_begin_x);
|
|
|
|
break;
|
|
|
|
}
|
2015-11-22 16:09:15 +00:00
|
|
|
f->data = charealloc(f->data, strlen(f->data) + strlen(u->strdata) + 1);
|
2014-05-15 20:00:46 +00:00
|
|
|
strcat(f->data, u->strdata);
|
2015-11-26 09:31:33 +00:00
|
|
|
unlink_node(f->next);
|
2008-07-14 07:18:22 +00:00
|
|
|
renumber(f);
|
2014-06-08 19:02:12 +00:00
|
|
|
goto_line_posx(u->mark_begin_lineno, u->mark_begin_x);
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2014-05-15 20:00:46 +00:00
|
|
|
case CUT_EOF:
|
2008-07-31 04:24:04 +00:00
|
|
|
case CUT:
|
2014-06-22 21:26:56 +00:00
|
|
|
redidmsg = _("text cut");
|
2008-09-16 21:35:19 +00:00
|
|
|
redo_cut(u);
|
|
|
|
break;
|
2014-05-15 20:00:46 +00:00
|
|
|
case PASTE:
|
2014-06-22 21:26:56 +00:00
|
|
|
redidmsg = _("text uncut");
|
2014-05-15 20:00:46 +00:00
|
|
|
redo_paste(u);
|
2008-07-31 04:24:04 +00:00
|
|
|
break;
|
2008-08-02 22:31:01 +00:00
|
|
|
case REPLACE:
|
2014-06-22 21:26:56 +00:00
|
|
|
redidmsg = _("text replace");
|
2008-08-02 22:31:01 +00:00
|
|
|
data = u->strdata;
|
|
|
|
u->strdata = f->data;
|
|
|
|
f->data = data;
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2008-08-02 22:31:01 +00:00
|
|
|
break;
|
2008-09-16 21:35:19 +00:00
|
|
|
case INSERT:
|
2014-06-22 21:26:56 +00:00
|
|
|
redidmsg = _("text insert");
|
2014-05-15 20:00:46 +00:00
|
|
|
goto_line_posx(u->lineno, u->begin);
|
2014-04-14 09:14:39 +00:00
|
|
|
copy_from_filestruct(u->cutbuffer);
|
2014-07-02 20:52:27 +00:00
|
|
|
free_filestruct(u->cutbuffer);
|
|
|
|
u->cutbuffer = NULL;
|
2008-08-03 20:19:42 +00:00
|
|
|
break;
|
2016-05-25 20:13:50 +00:00
|
|
|
#ifdef ENABLE_COMMENT
|
|
|
|
case COMMENT:
|
|
|
|
handle_comment_action(u, FALSE, TRUE);
|
|
|
|
redidmsg = _("comment");
|
|
|
|
break;
|
|
|
|
case UNCOMMENT:
|
|
|
|
handle_comment_action(u, FALSE, FALSE);
|
|
|
|
redidmsg = _("uncomment");
|
|
|
|
break;
|
|
|
|
#endif
|
2008-07-10 20:13:04 +00:00
|
|
|
default:
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Internal error: unknown type. "
|
|
|
|
"Please save your work."));
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-04-14 09:57:06 +00:00
|
|
|
|
2014-07-11 19:14:25 +00:00
|
|
|
if (redidmsg)
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Redid action (%s)"), redidmsg);
|
2008-07-10 20:13:04 +00:00
|
|
|
|
|
|
|
openfile->current_undo = u;
|
2008-08-08 03:02:03 +00:00
|
|
|
openfile->last_action = OTHER;
|
2016-06-07 11:04:51 +00:00
|
|
|
openfile->mark_set = FALSE;
|
2014-06-17 15:50:34 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
2015-11-30 16:21:51 +00:00
|
|
|
openfile->totsize = u->newsize;
|
2014-05-15 20:00:46 +00:00
|
|
|
set_modified();
|
2008-07-10 20:13:04 +00:00
|
|
|
}
|
2006-04-28 13:19:56 +00:00
|
|
|
#endif /* !NANO_TINY */
|
|
|
|
|
2005-12-08 07:24:54 +00:00
|
|
|
/* Someone hits Enter *gasp!* */
|
2015-11-11 19:04:31 +00:00
|
|
|
void do_enter()
|
2005-07-25 02:41:59 +00:00
|
|
|
{
|
|
|
|
filestruct *newnode = make_new_node(openfile->current);
|
|
|
|
size_t extra = 0;
|
|
|
|
|
2008-08-21 04:24:25 +00:00
|
|
|
assert(openfile->current != NULL && openfile->current->data != NULL);
|
2005-07-25 02:41:59 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2015-11-11 19:15:36 +00:00
|
|
|
add_undo(ENTER);
|
|
|
|
|
2005-07-25 02:41:59 +00:00
|
|
|
/* Do auto-indenting, like the neolithic Turbo Pascal editor. */
|
|
|
|
if (ISSET(AUTOINDENT)) {
|
|
|
|
/* 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(openfile->current->data);
|
|
|
|
if (extra > openfile->current_x)
|
|
|
|
extra = openfile->current_x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
newnode->data = charalloc(strlen(openfile->current->data +
|
|
|
|
openfile->current_x) + extra + 1);
|
|
|
|
strcpy(&newnode->data[extra], openfile->current->data +
|
|
|
|
openfile->current_x);
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:41:59 +00:00
|
|
|
if (ISSET(AUTOINDENT)) {
|
|
|
|
strncpy(newnode->data, openfile->current->data, extra);
|
2011-05-10 05:43:08 +00:00
|
|
|
openfile->totsize += extra;
|
2005-07-25 02:41:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
null_at(&openfile->current->data, openfile->current_x);
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2015-06-28 14:12:25 +00:00
|
|
|
if (openfile->mark_set && openfile->current == openfile->mark_begin &&
|
|
|
|
openfile->current_x < openfile->mark_begin_x) {
|
2005-07-25 02:41:59 +00:00
|
|
|
openfile->mark_begin = newnode;
|
|
|
|
openfile->mark_begin_x += extra - openfile->current_x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
openfile->current_x = extra;
|
|
|
|
|
2015-11-24 13:24:01 +00:00
|
|
|
splice_node(openfile->current, newnode);
|
2005-07-25 02:41:59 +00:00
|
|
|
openfile->current = newnode;
|
2015-11-22 16:08:28 +00:00
|
|
|
renumber(newnode);
|
2005-07-25 02:41:59 +00:00
|
|
|
|
|
|
|
openfile->totsize++;
|
|
|
|
set_modified();
|
2005-09-13 04:45:46 +00:00
|
|
|
|
2005-07-25 02:41:59 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
2005-09-13 04:45:46 +00:00
|
|
|
|
2015-11-11 19:15:36 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
update_undo(ENTER);
|
|
|
|
#endif
|
|
|
|
|
2016-04-25 19:14:18 +00:00
|
|
|
refresh_needed = TRUE;
|
2005-07-25 02:41:59 +00:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Send a SIGKILL (unconditional kill) to the forked process in
|
|
|
|
* execute_command(). */
|
2005-12-06 19:39:56 +00:00
|
|
|
RETSIGTYPE cancel_command(int signal)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
|
|
|
if (kill(pid, SIGKILL) == -1)
|
|
|
|
nperror("kill");
|
|
|
|
}
|
|
|
|
|
2005-11-08 23:09:47 +00:00
|
|
|
/* Execute command in a shell. Return TRUE on success. */
|
2005-07-24 19:57:51 +00:00
|
|
|
bool execute_command(const char *command)
|
|
|
|
{
|
|
|
|
int fd[2];
|
|
|
|
FILE *f;
|
2005-11-29 05:48:06 +00:00
|
|
|
char *shellenv;
|
2005-07-24 19:57:51 +00:00
|
|
|
struct sigaction oldaction, newaction;
|
|
|
|
/* Original and temporary handlers for SIGINT. */
|
|
|
|
bool sig_failed = FALSE;
|
|
|
|
/* Did sigaction() fail without changing the signal handlers? */
|
|
|
|
|
|
|
|
/* Make our pipes. */
|
|
|
|
if (pipe(fd) == -1) {
|
2014-02-28 11:49:12 +00:00
|
|
|
statusbar(_("Could not create pipe"));
|
2005-07-24 19:57:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Check $SHELL for the shell to use. If it isn't set, use /bin/sh.
|
|
|
|
* Note that $SHELL should contain only a path, with no arguments. */
|
2005-11-29 05:48:06 +00:00
|
|
|
shellenv = getenv("SHELL");
|
|
|
|
if (shellenv == NULL)
|
2009-11-29 06:13:22 +00:00
|
|
|
shellenv = (char *) "/bin/sh";
|
2005-11-29 05:48:06 +00:00
|
|
|
|
2005-07-24 19:57:51 +00:00
|
|
|
/* Fork a child. */
|
|
|
|
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. */
|
2005-11-29 05:21:06 +00:00
|
|
|
execl(shellenv, tail(shellenv), "-c", command, NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2006-04-26 18:33:50 +00:00
|
|
|
/* Continue as parent. */
|
2005-07-24 19:57:51 +00:00
|
|
|
close(fd[1]);
|
|
|
|
|
|
|
|
if (pid == -1) {
|
|
|
|
close(fd[0]);
|
|
|
|
statusbar(_("Could not fork"));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Before we start reading the forked command's output, we set
|
|
|
|
* things up so that Ctrl-C will cancel the new process. */
|
|
|
|
|
|
|
|
/* Enable interpretation of the special control keys so that we get
|
|
|
|
* SIGINT when Ctrl-C is pressed. */
|
|
|
|
enable_signals();
|
|
|
|
|
|
|
|
if (sigaction(SIGINT, NULL, &newaction) == -1) {
|
|
|
|
sig_failed = TRUE;
|
|
|
|
nperror("sigaction");
|
|
|
|
} else {
|
|
|
|
newaction.sa_handler = cancel_command;
|
|
|
|
if (sigaction(SIGINT, &newaction, &oldaction) == -1) {
|
|
|
|
sig_failed = TRUE;
|
|
|
|
nperror("sigaction");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that now oldaction is the previous SIGINT signal handler,
|
|
|
|
* to be restored later. */
|
|
|
|
|
|
|
|
f = fdopen(fd[0], "rb");
|
|
|
|
if (f == NULL)
|
|
|
|
nperror("fdopen");
|
|
|
|
|
2009-12-09 16:51:43 +00:00
|
|
|
read_file(f, 0, "stdin", TRUE, FALSE);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
|
|
|
if (wait(NULL) == -1)
|
|
|
|
nperror("wait");
|
|
|
|
|
|
|
|
if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1)
|
|
|
|
nperror("sigaction");
|
|
|
|
|
2007-12-18 01:28:53 +00:00
|
|
|
/* Restore the terminal to its previous state. In the process,
|
|
|
|
* disable interpretation of the special control keys so that we can
|
2005-07-24 19:57:51 +00:00
|
|
|
* use Ctrl-C for other things. */
|
2007-12-18 01:28:53 +00:00
|
|
|
terminal_init();
|
2005-07-24 19:57:51 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2008-07-10 20:13:04 +00:00
|
|
|
|
2016-01-15 16:44:50 +00:00
|
|
|
/* Discard undo items that are newer than the given one, or all if NULL. */
|
|
|
|
void discard_until(const undo *thisitem, openfilestruct *thefile)
|
2015-12-03 08:50:34 +00:00
|
|
|
{
|
2016-01-15 16:44:50 +00:00
|
|
|
undo *dropit = thefile->undotop;
|
2016-05-25 20:13:50 +00:00
|
|
|
undo_group *group;
|
2015-12-03 08:50:34 +00:00
|
|
|
|
2016-01-15 16:44:50 +00:00
|
|
|
while (dropit != NULL && dropit != thisitem) {
|
|
|
|
thefile->undotop = dropit->next;
|
2015-12-03 08:50:34 +00:00
|
|
|
free(dropit->strdata);
|
2016-02-18 19:58:18 +00:00
|
|
|
free_filestruct(dropit->cutbuffer);
|
2016-05-25 20:13:50 +00:00
|
|
|
#ifdef ENABLE_COMMENT
|
|
|
|
group = dropit->grouping;
|
|
|
|
while (group != NULL) {
|
|
|
|
undo_group *next = group->next;
|
|
|
|
free(group);
|
|
|
|
group = next;
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-03 08:50:34 +00:00
|
|
|
free(dropit);
|
2016-01-15 16:44:50 +00:00
|
|
|
dropit = thefile->undotop;
|
2015-12-03 08:50:34 +00:00
|
|
|
}
|
2016-02-10 08:49:23 +00:00
|
|
|
|
|
|
|
/* Prevent a chain of editing actions from continuing. */
|
|
|
|
thefile->last_action = OTHER;
|
2015-12-03 08:50:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Add a new undo struct to the top of the current pile. */
|
2015-06-17 11:18:20 +00:00
|
|
|
void add_undo(undo_type action)
|
2008-07-10 20:13:04 +00:00
|
|
|
{
|
2015-11-12 19:50:33 +00:00
|
|
|
undo *u = openfile->current_undo;
|
2015-06-17 10:59:16 +00:00
|
|
|
/* The thing we did previously. */
|
2008-07-10 20:13:04 +00:00
|
|
|
|
2015-06-17 10:59:16 +00:00
|
|
|
/* When doing contiguous adds or contiguous cuts -- which means: with
|
|
|
|
* no cursor movement in between -- don't add a new undo item. */
|
2015-11-12 19:50:33 +00:00
|
|
|
if (u && u->mark_begin_lineno == openfile->current->lineno && action == openfile->last_action &&
|
|
|
|
((action == ADD && u->type == ADD && u->mark_begin_x == openfile->current_x) ||
|
2016-06-07 09:52:57 +00:00
|
|
|
(action == CUT && u->type == CUT && u->xflags < MARK_WAS_SET && keeping_cutbuffer())))
|
2008-08-01 06:52:15 +00:00
|
|
|
return;
|
|
|
|
|
2015-11-22 16:08:28 +00:00
|
|
|
/* Blow away newer undo items if we add somewhere in the middle. */
|
2016-01-15 16:44:50 +00:00
|
|
|
discard_until(u, openfile);
|
2008-07-10 20:13:04 +00:00
|
|
|
|
2015-06-17 15:17:09 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, " >> Adding an undo...\n");
|
|
|
|
#endif
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Allocate and initialize a new undo type. */
|
2009-11-29 06:13:22 +00:00
|
|
|
u = (undo *) nmalloc(sizeof(undo));
|
2015-06-17 11:18:20 +00:00
|
|
|
u->type = action;
|
2014-06-09 10:35:44 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2014-06-09 10:01:54 +00:00
|
|
|
if (u->type == SPLIT_BEGIN) {
|
|
|
|
/* Some action, most likely an ADD, was performed that invoked
|
|
|
|
* do_wrap(). Rearrange the undo order so that this previous
|
|
|
|
* action is after the SPLIT_BEGIN undo. */
|
2015-11-12 19:50:33 +00:00
|
|
|
u->next = openfile->undotop->next;
|
|
|
|
openfile->undotop->next = u;
|
2014-06-09 10:35:44 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2015-11-12 19:50:33 +00:00
|
|
|
u->next = openfile->undotop;
|
|
|
|
openfile->undotop = u;
|
|
|
|
openfile->current_undo = u;
|
2014-06-09 10:01:54 +00:00
|
|
|
}
|
2008-08-01 03:50:20 +00:00
|
|
|
u->strdata = NULL;
|
|
|
|
u->cutbuffer = NULL;
|
2014-05-16 11:03:04 +00:00
|
|
|
u->cutbottom = NULL;
|
2015-11-22 16:08:28 +00:00
|
|
|
u->lineno = openfile->current->lineno;
|
|
|
|
u->begin = openfile->current_x;
|
2015-11-12 19:50:33 +00:00
|
|
|
u->mark_begin_lineno = openfile->current->lineno;
|
|
|
|
u->mark_begin_x = openfile->current_x;
|
2015-11-30 16:21:51 +00:00
|
|
|
u->wassize = openfile->totsize;
|
2008-08-01 03:50:20 +00:00
|
|
|
u->xflags = 0;
|
2016-05-25 20:13:50 +00:00
|
|
|
u->grouping = NULL;
|
2008-07-10 20:13:04 +00:00
|
|
|
|
|
|
|
switch (u->type) {
|
2014-04-04 20:45:28 +00:00
|
|
|
/* We need to start copying data into the undo buffer
|
|
|
|
* or we won't be able to restore it later. */
|
2008-07-10 20:13:04 +00:00
|
|
|
case ADD:
|
2015-11-30 16:21:51 +00:00
|
|
|
u->wassize--;
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2014-06-08 19:02:12 +00:00
|
|
|
case BACK:
|
2015-06-27 09:17:36 +00:00
|
|
|
/* If the next line is the magic line, don't ever undo this
|
|
|
|
* backspace, as it won't actually have deleted anything. */
|
2015-11-12 19:50:33 +00:00
|
|
|
if (openfile->current->next == openfile->filebot && openfile->current->data[0] != '\0')
|
2015-06-27 09:27:19 +00:00
|
|
|
u->xflags = WAS_FINAL_BACKSPACE;
|
2008-07-10 20:13:04 +00:00
|
|
|
case DEL:
|
2015-11-12 19:50:33 +00:00
|
|
|
if (u->begin != strlen(openfile->current->data)) {
|
2014-05-25 19:41:49 +00:00
|
|
|
char *char_buf = charalloc(mb_cur_max() + 1);
|
2015-11-12 19:50:33 +00:00
|
|
|
int char_buf_len = parse_mbchar(&openfile->current->data[u->begin], char_buf, NULL);
|
2014-06-04 16:30:11 +00:00
|
|
|
null_at(&char_buf, char_buf_len);
|
|
|
|
u->strdata = char_buf;
|
2014-06-08 19:02:12 +00:00
|
|
|
if (u->type == BACK)
|
|
|
|
u->mark_begin_x += char_buf_len;
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-06-18 20:11:52 +00:00
|
|
|
/* Else purposely fall into the line-joining code. */
|
|
|
|
case JOIN:
|
2015-11-12 19:50:33 +00:00
|
|
|
if (openfile->current->next) {
|
2014-06-08 19:02:12 +00:00
|
|
|
if (u->type == BACK) {
|
2015-11-12 19:50:33 +00:00
|
|
|
u->lineno = openfile->current->next->lineno;
|
2014-06-08 19:02:12 +00:00
|
|
|
u->begin = 0;
|
|
|
|
}
|
2015-11-12 19:50:33 +00:00
|
|
|
u->strdata = mallocstrcpy(NULL, openfile->current->next->data);
|
2008-07-10 20:13:04 +00:00
|
|
|
}
|
2015-06-17 11:18:20 +00:00
|
|
|
action = u->type = JOIN;
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2009-12-03 03:12:00 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2014-06-09 10:01:54 +00:00
|
|
|
case SPLIT_BEGIN:
|
2015-11-12 19:50:33 +00:00
|
|
|
action = openfile->undotop->type;
|
2014-06-09 10:01:54 +00:00
|
|
|
break;
|
|
|
|
case SPLIT_END:
|
2009-03-26 01:01:48 +00:00
|
|
|
break;
|
2015-06-28 14:12:25 +00:00
|
|
|
#endif
|
2009-03-26 01:01:48 +00:00
|
|
|
case INSERT:
|
2014-07-02 20:52:27 +00:00
|
|
|
break;
|
2008-08-02 22:31:01 +00:00
|
|
|
case REPLACE:
|
2015-11-12 19:50:33 +00:00
|
|
|
u->strdata = mallocstrcpy(NULL, openfile->current->data);
|
2008-07-31 04:24:04 +00:00
|
|
|
break;
|
2014-05-15 20:00:46 +00:00
|
|
|
case CUT_EOF:
|
2014-06-21 19:32:17 +00:00
|
|
|
cutbuffer_reset();
|
|
|
|
break;
|
2008-07-31 04:24:04 +00:00
|
|
|
case CUT:
|
2014-06-18 19:04:35 +00:00
|
|
|
cutbuffer_reset();
|
2016-06-07 09:52:57 +00:00
|
|
|
if (openfile->mark_set) {
|
2008-07-31 04:24:04 +00:00
|
|
|
u->mark_begin_lineno = openfile->mark_begin->lineno;
|
|
|
|
u->mark_begin_x = openfile->mark_begin_x;
|
2016-06-07 09:52:57 +00:00
|
|
|
u->xflags = MARK_WAS_SET;
|
2015-11-22 16:08:28 +00:00
|
|
|
} else if (!ISSET(CUT_TO_END)) {
|
2014-05-15 20:00:46 +00:00
|
|
|
/* The entire line is being cut regardless of the cursor position. */
|
|
|
|
u->begin = 0;
|
2015-06-27 09:27:19 +00:00
|
|
|
u->xflags = WAS_WHOLE_LINE;
|
2014-05-15 20:00:46 +00:00
|
|
|
}
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2014-05-15 20:00:46 +00:00
|
|
|
case PASTE:
|
2015-11-22 16:07:23 +00:00
|
|
|
u->cutbuffer = copy_filestruct(cutbuffer);
|
|
|
|
u->lineno += cutbottom->lineno - cutbuffer->lineno;
|
2008-08-02 22:31:01 +00:00
|
|
|
break;
|
2009-04-21 05:34:08 +00:00
|
|
|
case ENTER:
|
|
|
|
break;
|
2016-05-25 20:13:50 +00:00
|
|
|
#ifdef ENABLE_COMMENT
|
|
|
|
case COMMENT:
|
|
|
|
case UNCOMMENT:
|
|
|
|
break;
|
|
|
|
#endif
|
2015-11-25 10:11:54 +00:00
|
|
|
default:
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Internal error: unknown type. "
|
|
|
|
"Please save your work."));
|
2008-08-02 22:31:01 +00:00
|
|
|
break;
|
2008-07-10 20:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2015-11-12 19:50:33 +00:00
|
|
|
fprintf(stderr, " >> openfile->current->data = \"%s\", current_x = %lu, u->begin = %lu, type = %d\n",
|
|
|
|
openfile->current->data, (unsigned long)openfile->current_x, (unsigned long)u->begin, action);
|
2008-07-10 20:13:04 +00:00
|
|
|
#endif
|
2015-11-12 19:50:33 +00:00
|
|
|
openfile->last_action = action;
|
2008-07-10 20:13:04 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 20:13:50 +00:00
|
|
|
#ifdef ENABLE_COMMENT
|
|
|
|
/* Update a comment undo item. This should be called once for each line
|
|
|
|
* affected by the comment/uncomment feature. */
|
|
|
|
void update_comment_undo(ssize_t lineno)
|
|
|
|
{
|
|
|
|
undo *u = openfile->current_undo;
|
|
|
|
|
|
|
|
/* If there already is a group and the current line is contiguous with it,
|
|
|
|
* extend the group; otherwise, create a new group. */
|
|
|
|
if (u->grouping && u->grouping->bottom_line + 1 == lineno)
|
|
|
|
u->grouping->bottom_line++;
|
|
|
|
else {
|
|
|
|
undo_group *born = (undo_group *)nmalloc(sizeof(undo_group));
|
|
|
|
|
|
|
|
born->next = u->grouping;
|
|
|
|
u->grouping = born;
|
|
|
|
born->top_line = lineno;
|
|
|
|
born->bottom_line = lineno;
|
|
|
|
}
|
2016-05-31 19:19:50 +00:00
|
|
|
|
|
|
|
/* Store the file size after the change, to be used when redoing. */
|
|
|
|
u->newsize = openfile->totsize;
|
2016-05-25 20:13:50 +00:00
|
|
|
}
|
|
|
|
#endif /* ENABLE_COMMENT */
|
|
|
|
|
2014-04-04 20:45:28 +00:00
|
|
|
/* Update an undo item, or determine whether a new one is really needed
|
|
|
|
* and bounce the data to add_undo instead. The latter functionality
|
|
|
|
* just feels gimmicky and may just be more hassle than it's worth,
|
|
|
|
* so it should be axed if needed. */
|
2008-08-03 04:48:05 +00:00
|
|
|
void update_undo(undo_type action)
|
2008-07-10 20:13:04 +00:00
|
|
|
{
|
|
|
|
undo *u;
|
2008-08-01 03:50:20 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2015-11-12 19:50:33 +00:00
|
|
|
fprintf(stderr, " >> Updating... action = %d, openfile->last_action = %d, openfile->current->lineno = %ld",
|
|
|
|
action, openfile->last_action, (long)openfile->current->lineno);
|
|
|
|
if (openfile->current_undo)
|
|
|
|
fprintf(stderr, ", openfile->current_undo->lineno = %ld\n", (long)openfile->current_undo->lineno);
|
2008-08-02 22:31:01 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "\n");
|
2008-08-01 03:50:20 +00:00
|
|
|
#endif
|
|
|
|
|
2008-07-31 04:24:04 +00:00
|
|
|
/* Change to an add if we're not using the same undo struct
|
2014-04-04 20:45:28 +00:00
|
|
|
* that we should be using. */
|
2015-11-22 16:08:28 +00:00
|
|
|
if (action != openfile->last_action ||
|
|
|
|
(action != ENTER && action != CUT && action != INSERT &&
|
|
|
|
openfile->current->lineno != openfile->current_undo->lineno)) {
|
2014-07-16 08:53:16 +00:00
|
|
|
add_undo(action);
|
2008-07-10 20:13:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:50:33 +00:00
|
|
|
assert(openfile->undotop != NULL);
|
|
|
|
u = openfile->undotop;
|
2008-07-10 20:13:04 +00:00
|
|
|
|
2015-11-30 16:21:51 +00:00
|
|
|
u->newsize = openfile->totsize;
|
|
|
|
|
2008-07-10 20:13:04 +00:00
|
|
|
switch (u->type) {
|
2014-05-25 19:41:49 +00:00
|
|
|
case ADD: {
|
2008-07-10 20:13:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-11-12 19:50:33 +00:00
|
|
|
fprintf(stderr, " >> openfile->current->data = \"%s\", current_x = %lu, u->begin = %lu\n",
|
|
|
|
openfile->current->data, (unsigned long)openfile->current_x, (unsigned long)u->begin);
|
2008-07-10 20:13:04 +00:00
|
|
|
#endif
|
2014-05-25 19:41:49 +00:00
|
|
|
char *char_buf = charalloc(mb_cur_max());
|
2015-11-12 19:50:33 +00:00
|
|
|
size_t char_buf_len = parse_mbchar(&openfile->current->data[u->mark_begin_x], char_buf, NULL);
|
2014-06-04 16:02:51 +00:00
|
|
|
u->strdata = addstrings(u->strdata, u->strdata ? strlen(u->strdata) : 0, char_buf, char_buf_len);
|
2008-07-10 20:13:04 +00:00
|
|
|
#ifdef DEBUG
|
2015-07-10 17:25:51 +00:00
|
|
|
fprintf(stderr, " >> current undo data is \"%s\"\n", u->strdata);
|
2008-07-10 20:13:04 +00:00
|
|
|
#endif
|
2015-11-12 19:50:33 +00:00
|
|
|
u->mark_begin_lineno = openfile->current->lineno;
|
|
|
|
u->mark_begin_x = openfile->current_x;
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
2014-05-25 19:41:49 +00:00
|
|
|
}
|
2014-06-08 19:02:12 +00:00
|
|
|
case BACK:
|
2014-05-25 19:41:49 +00:00
|
|
|
case DEL: {
|
|
|
|
char *char_buf = charalloc(mb_cur_max());
|
2015-11-12 19:50:33 +00:00
|
|
|
size_t char_buf_len = parse_mbchar(&openfile->current->data[openfile->current_x], char_buf, NULL);
|
|
|
|
if (openfile->current_x == u->begin) {
|
2014-04-04 20:45:28 +00:00
|
|
|
/* They're deleting. */
|
2014-05-25 19:41:49 +00:00
|
|
|
u->strdata = addstrings(u->strdata, strlen(u->strdata), char_buf, char_buf_len);
|
2015-11-12 19:50:33 +00:00
|
|
|
u->mark_begin_x = openfile->current_x;
|
|
|
|
} else if (openfile->current_x == u->begin - char_buf_len) {
|
2014-04-04 20:45:28 +00:00
|
|
|
/* They're backspacing. */
|
2014-05-25 19:41:49 +00:00
|
|
|
u->strdata = addstrings(char_buf, char_buf_len, u->strdata, strlen(u->strdata));
|
2015-11-12 19:50:33 +00:00
|
|
|
u->begin = openfile->current_x;
|
2008-07-10 20:13:04 +00:00
|
|
|
} else {
|
2014-04-04 20:45:28 +00:00
|
|
|
/* They deleted something else on the line. */
|
2014-05-25 19:41:49 +00:00
|
|
|
free(char_buf);
|
2014-06-08 19:02:12 +00:00
|
|
|
add_undo(u->type);
|
2008-07-10 20:13:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2015-06-17 15:17:09 +00:00
|
|
|
fprintf(stderr, " >> current undo data is \"%s\"\nu->begin = %lu\n", u->strdata, (unsigned long)u->begin);
|
2008-07-10 20:13:04 +00:00
|
|
|
#endif
|
|
|
|
break;
|
2014-05-25 19:41:49 +00:00
|
|
|
}
|
2014-05-15 20:00:46 +00:00
|
|
|
case CUT_EOF:
|
2008-07-31 04:24:04 +00:00
|
|
|
case CUT:
|
2008-10-14 01:14:12 +00:00
|
|
|
if (!cutbuffer)
|
|
|
|
break;
|
2016-02-18 19:58:18 +00:00
|
|
|
free_filestruct(u->cutbuffer);
|
2008-07-31 04:24:04 +00:00
|
|
|
u->cutbuffer = copy_filestruct(cutbuffer);
|
2016-06-07 09:52:57 +00:00
|
|
|
if (u->xflags == MARK_WAS_SET) {
|
2014-05-15 20:00:46 +00:00
|
|
|
/* If the "marking" operation was from right-->left or
|
|
|
|
* bottom-->top, then swap the mark points. */
|
|
|
|
if ((u->lineno == u->mark_begin_lineno && u->begin < u->mark_begin_x)
|
|
|
|
|| u->lineno < u->mark_begin_lineno) {
|
|
|
|
size_t x_loc = u->begin;
|
|
|
|
u->begin = u->mark_begin_x;
|
|
|
|
u->mark_begin_x = x_loc;
|
|
|
|
|
|
|
|
ssize_t line = u->lineno;
|
|
|
|
u->lineno = u->mark_begin_lineno;
|
|
|
|
u->mark_begin_lineno = line;
|
2014-07-02 20:29:57 +00:00
|
|
|
} else
|
2015-06-27 09:27:19 +00:00
|
|
|
u->xflags = WAS_MARKED_FORWARD;
|
2014-07-02 20:29:57 +00:00
|
|
|
} else {
|
2015-12-04 21:11:10 +00:00
|
|
|
/* Compute the end of the cut for the undo, using our copy. */
|
2014-05-15 20:00:46 +00:00
|
|
|
u->cutbottom = u->cutbuffer;
|
|
|
|
while (u->cutbottom->next != NULL)
|
|
|
|
u->cutbottom = u->cutbottom->next;
|
2015-12-04 21:11:10 +00:00
|
|
|
u->lineno = u->mark_begin_lineno + u->cutbottom->lineno -
|
|
|
|
u->cutbuffer->lineno;
|
2014-07-02 20:29:57 +00:00
|
|
|
if (ISSET(CUT_TO_END) || u->type == CUT_EOF) {
|
|
|
|
u->begin = strlen(u->cutbottom->data);
|
2015-06-28 14:12:25 +00:00
|
|
|
if (u->lineno == u->mark_begin_lineno)
|
|
|
|
u->begin += u->mark_begin_x;
|
2015-11-30 15:49:37 +00:00
|
|
|
} else if (openfile->current == openfile->filebot &&
|
|
|
|
ISSET(NO_NEWLINES))
|
|
|
|
u->begin = strlen(u->cutbottom->data);
|
2014-05-15 20:00:46 +00:00
|
|
|
}
|
2008-07-31 04:24:04 +00:00
|
|
|
break;
|
2008-08-02 22:31:01 +00:00
|
|
|
case REPLACE:
|
2014-05-15 20:00:46 +00:00
|
|
|
case PASTE:
|
2015-11-12 19:50:33 +00:00
|
|
|
u->begin = openfile->current_x;
|
2014-05-25 19:41:49 +00:00
|
|
|
u->lineno = openfile->current->lineno;
|
2008-08-02 22:31:01 +00:00
|
|
|
break;
|
2008-08-03 04:48:05 +00:00
|
|
|
case INSERT:
|
2015-11-22 16:08:28 +00:00
|
|
|
/* Store the number of lines (plus one) of the insertion. */
|
2008-08-03 04:48:05 +00:00
|
|
|
u->mark_begin_lineno = openfile->current->lineno;
|
2015-11-12 19:01:57 +00:00
|
|
|
/* When the insertion contains no newline, store the adjusted
|
|
|
|
* x position; otherwise, store the length of the last line. */
|
|
|
|
if (openfile->fileage == openfile->filebot)
|
|
|
|
u->mark_begin_x = openfile->current_x;
|
|
|
|
else
|
|
|
|
u->mark_begin_x = strlen(openfile->filebot->data);
|
2009-03-26 01:01:48 +00:00
|
|
|
break;
|
2014-05-25 19:41:49 +00:00
|
|
|
case ENTER:
|
2015-11-12 19:50:33 +00:00
|
|
|
u->strdata = mallocstrcpy(NULL, openfile->current->data);
|
|
|
|
u->mark_begin_x = openfile->current_x;
|
2014-05-25 19:41:49 +00:00
|
|
|
break;
|
2009-12-03 03:12:00 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2014-06-09 10:01:54 +00:00
|
|
|
case SPLIT_BEGIN:
|
|
|
|
case SPLIT_END:
|
2015-06-28 14:12:25 +00:00
|
|
|
#endif
|
2014-06-18 20:11:52 +00:00
|
|
|
case JOIN:
|
2014-04-04 20:45:28 +00:00
|
|
|
/* These cases are handled by the earlier check for a new line and action. */
|
2015-11-25 10:11:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Internal error: unknown type. "
|
|
|
|
"Please save your work."));
|
2008-07-10 20:13:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2015-06-17 15:17:09 +00:00
|
|
|
fprintf(stderr, " >> Done in update_undo (type was %d)\n", action);
|
2008-07-10 20:13:04 +00:00
|
|
|
#endif
|
2014-06-18 21:23:50 +00:00
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_WRAPPING
|
2006-05-22 02:08:49 +00:00
|
|
|
/* Unset the prepend_wrap flag. We need to do this as soon as we do
|
2005-12-08 07:09:08 +00:00
|
|
|
* something other than type text. */
|
2005-07-24 19:57:51 +00:00
|
|
|
void wrap_reset(void)
|
|
|
|
{
|
2005-11-25 13:48:09 +00:00
|
|
|
prepend_wrap = FALSE;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 10:01:54 +00:00
|
|
|
/* Try wrapping the given line. Return TRUE if wrapped, FALSE otherwise. */
|
|
|
|
bool do_wrap(filestruct *line)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
|
|
|
size_t line_len;
|
2005-07-25 22:54:16 +00:00
|
|
|
/* The length of the line we wrap. */
|
2005-07-24 19:57:51 +00:00
|
|
|
ssize_t wrap_loc;
|
2005-07-25 22:54:16 +00:00
|
|
|
/* The index of line->data where we wrap. */
|
|
|
|
const char *after_break;
|
|
|
|
/* The text after the wrap point. */
|
|
|
|
size_t after_break_len;
|
|
|
|
/* The length of after_break. */
|
2005-07-24 19:57:51 +00:00
|
|
|
const char *next_line = NULL;
|
|
|
|
/* The next line, minus indentation. */
|
2005-07-25 22:54:16 +00:00
|
|
|
size_t next_line_len = 0;
|
|
|
|
/* The length of next_line. */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
|
|
|
/* There are three steps. First, we decide where to wrap. Then, we
|
|
|
|
* create the new wrap line. Finally, we clean up. */
|
|
|
|
|
|
|
|
/* Step 1, finding where to wrap. We are going to add a new line
|
|
|
|
* after a blank character. In this step, we call break_line() to
|
|
|
|
* get the location of the last blank we can break the line at, and
|
2006-06-11 19:14:14 +00:00
|
|
|
* set wrap_loc to the location of the character after it, so that
|
|
|
|
* the blank is preserved at the end of the line.
|
2005-07-24 19:57:51 +00:00
|
|
|
*
|
|
|
|
* If there is no legal wrap point, or we reach the last character
|
|
|
|
* of the line while trying to find one, we should return without
|
|
|
|
* wrapping. Note that if autoindent is turned on, we don't break
|
|
|
|
* at the end of it! */
|
|
|
|
assert(line != NULL && line->data != NULL);
|
|
|
|
|
|
|
|
/* Save the length of the line. */
|
|
|
|
line_len = strlen(line->data);
|
|
|
|
|
|
|
|
/* Find the last blank where we can break the line. */
|
2005-11-22 21:13:36 +00:00
|
|
|
wrap_loc = break_line(line->data, fill
|
|
|
|
#ifndef DISABLE_HELP
|
|
|
|
, FALSE
|
|
|
|
#endif
|
|
|
|
);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
|
|
|
/* If we couldn't break the line, or we've reached the end of it, we
|
|
|
|
* don't wrap. */
|
|
|
|
if (wrap_loc == -1 || line->data[wrap_loc] == '\0')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Otherwise, move forward to the character just after the blank. */
|
|
|
|
wrap_loc += move_mbright(line->data + wrap_loc, 0);
|
|
|
|
|
|
|
|
/* If we've reached the end of the line, we don't wrap. */
|
|
|
|
if (line->data[wrap_loc] == '\0')
|
|
|
|
return FALSE;
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-24 19:57:51 +00:00
|
|
|
/* If autoindent is turned on, and we're on the character just after
|
|
|
|
* the indentation, we don't wrap. */
|
2015-11-02 11:24:22 +00:00
|
|
|
if (ISSET(AUTOINDENT) && wrap_loc == indent_length(line->data))
|
|
|
|
return FALSE;
|
2014-06-09 10:01:54 +00:00
|
|
|
|
|
|
|
add_undo(SPLIT_BEGIN);
|
2005-07-24 19:57:51 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-09 10:01:54 +00:00
|
|
|
size_t old_x = openfile->current_x;
|
|
|
|
filestruct * oldLine = openfile->current;
|
|
|
|
openfile->current = line;
|
|
|
|
|
2005-07-24 19:57:51 +00:00
|
|
|
/* Step 2, making the new wrap line. It will consist of indentation
|
|
|
|
* followed by the text after the wrap point, optionally followed by
|
|
|
|
* a space (if the text after the wrap point doesn't end in a blank)
|
2006-06-11 19:15:59 +00:00
|
|
|
* and the text of the next line, if they can fit without wrapping,
|
|
|
|
* the next line exists, and the prepend_wrap flag is set. */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
|
|
|
/* after_break is the text that will be wrapped to the next line. */
|
|
|
|
after_break = line->data + wrap_loc;
|
|
|
|
after_break_len = line_len - wrap_loc;
|
|
|
|
|
|
|
|
assert(strlen(after_break) == after_break_len);
|
|
|
|
|
2005-11-25 13:48:09 +00:00
|
|
|
/* We prepend the wrapped text to the next line, if the prepend_wrap
|
|
|
|
* flag is set, there is a next line, and prepending would not make
|
|
|
|
* the line too long. */
|
|
|
|
if (prepend_wrap && line != openfile->filebot) {
|
2005-07-24 19:57:51 +00:00
|
|
|
const char *end = after_break + move_mbleft(after_break,
|
|
|
|
after_break_len);
|
|
|
|
|
2014-06-09 10:01:54 +00:00
|
|
|
/* Go to the end of the line. */
|
|
|
|
openfile->current_x = line_len;
|
|
|
|
|
2005-07-24 19:57:51 +00:00
|
|
|
/* If after_break doesn't end in a blank, make sure it ends in a
|
|
|
|
* space. */
|
2016-02-25 21:04:45 +00:00
|
|
|
if (!is_blank_mbchar(end) && !ISSET(JUSTIFY_TRIM)) {
|
2014-06-20 16:13:54 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-06-09 10:01:54 +00:00
|
|
|
add_undo(ADD);
|
2014-06-20 16:13:54 +00:00
|
|
|
#endif
|
2005-07-24 19:57:51 +00:00
|
|
|
line_len++;
|
|
|
|
line->data = charealloc(line->data, line_len + 1);
|
|
|
|
line->data[line_len - 1] = ' ';
|
|
|
|
line->data[line_len] = '\0';
|
|
|
|
after_break = line->data + wrap_loc;
|
|
|
|
after_break_len++;
|
|
|
|
openfile->totsize++;
|
2014-06-09 10:01:54 +00:00
|
|
|
openfile->current_x++;
|
2014-06-20 16:13:54 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-06-09 10:01:54 +00:00
|
|
|
update_undo(ADD);
|
2014-06-20 16:13:54 +00:00
|
|
|
#endif
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next_line = line->next->data;
|
|
|
|
next_line_len = strlen(next_line);
|
|
|
|
|
|
|
|
if (after_break_len + next_line_len <= fill) {
|
2014-06-09 10:01:54 +00:00
|
|
|
/* Delete the LF to join the two lines. */
|
|
|
|
do_delete();
|
|
|
|
/* Delete any leading blanks from the joined-on line. */
|
|
|
|
while (is_blank_mbchar(&line->data[openfile->current_x]))
|
|
|
|
do_delete();
|
|
|
|
renumber(line);
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 10:01:54 +00:00
|
|
|
/* Go to the wrap location and split the line there. */
|
|
|
|
openfile->current_x = wrap_loc;
|
2015-11-11 19:04:31 +00:00
|
|
|
do_enter();
|
2005-11-25 13:48:09 +00:00
|
|
|
|
2014-06-09 10:01:54 +00:00
|
|
|
if (old_x < wrap_loc) {
|
|
|
|
openfile->current_x = old_x;
|
|
|
|
openfile->current = oldLine;
|
|
|
|
prepend_wrap = TRUE;
|
2005-07-24 19:57:51 +00:00
|
|
|
} else {
|
2014-06-09 10:01:54 +00:00
|
|
|
openfile->current_x += (old_x - wrap_loc);
|
2005-11-25 13:48:09 +00:00
|
|
|
prepend_wrap = FALSE;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 10:01:54 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-06-09 10:01:54 +00:00
|
|
|
add_undo(SPLIT_END);
|
2005-07-24 19:57:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif /* !DISABLE_WRAPPING */
|
|
|
|
|
2005-11-29 21:30:00 +00:00
|
|
|
#if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY)
|
2005-07-25 02:33:45 +00:00
|
|
|
/* We are trying to break a chunk off line. We find the last blank such
|
2005-09-20 06:12:54 +00:00
|
|
|
* that the display length to there is at most (goal + 1). If there is
|
|
|
|
* no such blank, then we find the first blank. We then take the last
|
2005-07-25 02:33:45 +00:00
|
|
|
* blank in that group of blanks. The terminating '\0' counts as a
|
2014-04-14 13:02:43 +00:00
|
|
|
* blank, as does a '\n' if newln is TRUE. */
|
2005-11-22 21:13:36 +00:00
|
|
|
ssize_t break_line(const char *line, ssize_t goal
|
|
|
|
#ifndef DISABLE_HELP
|
2008-05-31 23:09:40 +00:00
|
|
|
, bool newln
|
2005-11-22 21:13:36 +00:00
|
|
|
#endif
|
|
|
|
)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2005-07-25 02:33:45 +00:00
|
|
|
ssize_t blank_loc = -1;
|
|
|
|
/* Current tentative return value. Index of the last blank we
|
2014-04-14 09:57:06 +00:00
|
|
|
* found with short enough display width. */
|
2005-07-25 02:33:45 +00:00
|
|
|
ssize_t cur_loc = 0;
|
|
|
|
/* Current index in line. */
|
2006-05-18 17:28:16 +00:00
|
|
|
size_t cur_pos = 0;
|
|
|
|
/* Current column position in line. */
|
2014-04-14 13:02:43 +00:00
|
|
|
int char_len = 0;
|
|
|
|
/* Length of current character, in bytes. */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
assert(line != NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2006-05-18 17:28:16 +00:00
|
|
|
while (*line != '\0' && goal >= cur_pos) {
|
2014-04-14 13:02:43 +00:00
|
|
|
char_len = parse_mbchar(line, NULL, &cur_pos);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-22 21:13:36 +00:00
|
|
|
if (is_blank_mbchar(line)
|
|
|
|
#ifndef DISABLE_HELP
|
2008-05-31 23:09:40 +00:00
|
|
|
|| (newln && *line == '\n')
|
2005-11-22 21:13:36 +00:00
|
|
|
#endif
|
|
|
|
) {
|
2005-07-25 02:33:45 +00:00
|
|
|
blank_loc = cur_loc;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-22 21:13:36 +00:00
|
|
|
#ifndef DISABLE_HELP
|
2008-05-31 23:09:40 +00:00
|
|
|
if (newln && *line == '\n')
|
2005-07-25 02:33:45 +00:00
|
|
|
break;
|
2005-11-22 21:13:36 +00:00
|
|
|
#endif
|
2005-07-25 02:33:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-14 13:02:43 +00:00
|
|
|
line += char_len;
|
|
|
|
cur_loc += char_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 17:28:16 +00:00
|
|
|
if (goal >= cur_pos)
|
2005-07-25 02:33:45 +00:00
|
|
|
/* In fact, the whole line displays shorter than goal. */
|
|
|
|
return cur_loc;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2008-07-12 01:54:49 +00:00
|
|
|
#ifndef DISABLE_HELP
|
|
|
|
if (newln && blank_loc <= 0) {
|
2014-07-16 08:53:16 +00:00
|
|
|
/* If no blank was found, or was found only as the first
|
|
|
|
* character, force a line break. */
|
|
|
|
cur_loc -= char_len;
|
|
|
|
return cur_loc;
|
2008-07-12 01:54:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (blank_loc == -1) {
|
2014-04-14 13:02:43 +00:00
|
|
|
/* No blank was found within the goal width,
|
|
|
|
* so now try and find a blank beyond it. */
|
2005-07-25 02:33:45 +00:00
|
|
|
bool found_blank = FALSE;
|
2005-09-20 17:52:52 +00:00
|
|
|
ssize_t found_blank_loc = 0;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (*line != '\0') {
|
2014-04-14 13:02:43 +00:00
|
|
|
char_len = parse_mbchar(line, NULL, NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-22 21:13:36 +00:00
|
|
|
if (is_blank_mbchar(line)
|
|
|
|
#ifndef DISABLE_HELP
|
2008-05-31 23:09:40 +00:00
|
|
|
|| (newln && *line == '\n')
|
2005-11-22 21:13:36 +00:00
|
|
|
#endif
|
|
|
|
) {
|
2014-04-14 13:02:43 +00:00
|
|
|
found_blank = TRUE;
|
2005-09-20 16:36:08 +00:00
|
|
|
found_blank_loc = cur_loc;
|
2005-07-25 02:33:45 +00:00
|
|
|
} else if (found_blank)
|
2005-09-20 16:36:08 +00:00
|
|
|
return found_blank_loc;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2014-04-14 13:02:43 +00:00
|
|
|
line += char_len;
|
|
|
|
cur_loc += char_len;
|
2005-07-25 02:33:45 +00:00
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Move to the last blank after blank_loc, if there is one. */
|
|
|
|
line -= cur_loc;
|
|
|
|
line += blank_loc;
|
2014-04-14 13:02:43 +00:00
|
|
|
char_len = parse_mbchar(line, NULL, NULL);
|
|
|
|
line += char_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2014-06-18 21:23:50 +00:00
|
|
|
while (*line != '\0' && is_blank_mbchar(line)) {
|
2014-04-14 13:02:43 +00:00
|
|
|
char_len = parse_mbchar(line, NULL, NULL);
|
2006-05-20 13:11:56 +00:00
|
|
|
|
2014-04-14 13:02:43 +00:00
|
|
|
line += char_len;
|
|
|
|
blank_loc += char_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
return blank_loc;
|
|
|
|
}
|
2005-11-29 21:30:00 +00:00
|
|
|
#endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
|
2005-07-25 02:33:45 +00:00
|
|
|
/* The "indentation" of a line is the whitespace between the quote part
|
|
|
|
* and the non-whitespace of the line. */
|
|
|
|
size_t indent_length(const char *line)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
char *blank_mb;
|
|
|
|
int blank_mb_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
assert(line != NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
blank_mb = charalloc(mb_cur_max());
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (*line != '\0') {
|
2005-07-26 06:13:45 +00:00
|
|
|
blank_mb_len = parse_mbchar(line, blank_mb, NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (!is_blank_mbchar(blank_mb))
|
|
|
|
break;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
line += blank_mb_len;
|
|
|
|
len += blank_mb_len;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
free(blank_mb);
|
|
|
|
|
|
|
|
return len;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
#ifndef DISABLE_JUSTIFY
|
|
|
|
/* justify_format() replaces blanks with spaces and multiple spaces by 1
|
|
|
|
* (except it maintains up to 2 after a character in punct optionally
|
|
|
|
* followed by a character in brackets, and removes all from the end).
|
|
|
|
*
|
|
|
|
* justify_format() might make paragraph->data shorter, and change the
|
|
|
|
* actual pointer with null_at().
|
|
|
|
*
|
|
|
|
* justify_format() will not look at the first skip characters of
|
|
|
|
* paragraph. skip should be at most strlen(paragraph->data). The
|
|
|
|
* character at paragraph[skip + 1] must not be blank. */
|
|
|
|
void justify_format(filestruct *paragraph, size_t skip)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2005-07-25 02:33:45 +00:00
|
|
|
char *end, *new_end, *new_paragraph_data;
|
|
|
|
size_t shift = 0;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
size_t mark_shift = 0;
|
|
|
|
#endif
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* These four asserts are assumptions about the input data. */
|
|
|
|
assert(paragraph != NULL);
|
|
|
|
assert(paragraph->data != NULL);
|
|
|
|
assert(skip < strlen(paragraph->data));
|
|
|
|
assert(!is_blank_mbchar(paragraph->data + skip));
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
end = paragraph->data + skip;
|
|
|
|
new_paragraph_data = charalloc(strlen(paragraph->data) + 1);
|
|
|
|
strncpy(new_paragraph_data, paragraph->data, skip);
|
|
|
|
new_end = new_paragraph_data + skip;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (*end != '\0') {
|
2005-12-31 21:08:10 +00:00
|
|
|
int end_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-12-31 21:08:10 +00:00
|
|
|
/* If this character is blank, change it to a space if
|
|
|
|
* necessary, and skip over all blanks after it. */
|
2005-07-25 02:33:45 +00:00
|
|
|
if (is_blank_mbchar(end)) {
|
2005-12-31 21:08:10 +00:00
|
|
|
end_len = parse_mbchar(end, NULL, NULL);
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
*new_end = ' ';
|
|
|
|
new_end++;
|
|
|
|
end += end_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (*end != '\0' && is_blank_mbchar(end)) {
|
2005-07-26 06:13:45 +00:00
|
|
|
end_len = parse_mbchar(end, NULL, NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
end += end_len;
|
|
|
|
shift += end_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Keep track of the change in the current line. */
|
|
|
|
if (openfile->mark_set && openfile->mark_begin ==
|
|
|
|
paragraph && openfile->mark_begin_x >= end -
|
|
|
|
paragraph->data)
|
|
|
|
mark_shift += end_len;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* If this character is punctuation optionally followed by a
|
2005-12-31 21:08:10 +00:00
|
|
|
* bracket and then followed by blanks, change no more than two
|
|
|
|
* of the blanks to spaces if necessary, and skip over all
|
|
|
|
* blanks after them. */
|
2005-07-25 02:33:45 +00:00
|
|
|
} else if (mbstrchr(punct, end) != NULL) {
|
2005-12-31 21:08:10 +00:00
|
|
|
end_len = parse_mbchar(end, NULL, NULL);
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (end_len > 0) {
|
|
|
|
*new_end = *end;
|
|
|
|
new_end++;
|
|
|
|
end++;
|
|
|
|
end_len--;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (*end != '\0' && mbstrchr(brackets, end) != NULL) {
|
2005-07-26 06:13:45 +00:00
|
|
|
end_len = parse_mbchar(end, NULL, NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (end_len > 0) {
|
|
|
|
*new_end = *end;
|
|
|
|
new_end++;
|
|
|
|
end++;
|
|
|
|
end_len--;
|
|
|
|
}
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (*end != '\0' && is_blank_mbchar(end)) {
|
2005-07-26 06:13:45 +00:00
|
|
|
end_len = parse_mbchar(end, NULL, NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
*new_end = ' ';
|
|
|
|
new_end++;
|
|
|
|
end += end_len;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (*end != '\0' && is_blank_mbchar(end)) {
|
2005-07-26 06:13:45 +00:00
|
|
|
end_len = parse_mbchar(end, NULL, NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
*new_end = ' ';
|
|
|
|
new_end++;
|
|
|
|
end += end_len;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (*end != '\0' && is_blank_mbchar(end)) {
|
2005-07-26 06:13:45 +00:00
|
|
|
end_len = parse_mbchar(end, NULL, NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
end += end_len;
|
|
|
|
shift += end_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Keep track of the change in the current line. */
|
|
|
|
if (openfile->mark_set && openfile->mark_begin ==
|
|
|
|
paragraph && openfile->mark_begin_x >= end -
|
|
|
|
paragraph->data)
|
|
|
|
mark_shift += end_len;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* If this character is neither blank nor punctuation, leave it
|
2005-12-31 21:08:10 +00:00
|
|
|
* unchanged. */
|
2005-07-25 02:33:45 +00:00
|
|
|
} else {
|
2005-12-31 21:08:10 +00:00
|
|
|
end_len = parse_mbchar(end, NULL, NULL);
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (end_len > 0) {
|
|
|
|
*new_end = *end;
|
|
|
|
new_end++;
|
|
|
|
end++;
|
|
|
|
end_len--;
|
|
|
|
}
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
assert(*end == '\0');
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
*new_end = *end;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-12-31 21:08:10 +00:00
|
|
|
/* If there are spaces at the end of the line, remove them. */
|
2005-07-25 02:33:45 +00:00
|
|
|
while (new_end > new_paragraph_data + skip &&
|
|
|
|
*(new_end - 1) == ' ') {
|
|
|
|
new_end--;
|
|
|
|
shift++;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (shift > 0) {
|
|
|
|
openfile->totsize -= shift;
|
|
|
|
null_at(&new_paragraph_data, new_end - new_paragraph_data);
|
|
|
|
free(paragraph->data);
|
|
|
|
paragraph->data = new_paragraph_data;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Adjust the mark coordinates to compensate for the change in
|
|
|
|
* the current line. */
|
|
|
|
if (openfile->mark_set && openfile->mark_begin == paragraph) {
|
|
|
|
openfile->mark_begin_x -= mark_shift;
|
|
|
|
if (openfile->mark_begin_x > new_end - new_paragraph_data)
|
|
|
|
openfile->mark_begin_x = new_end - new_paragraph_data;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-07-25 02:33:45 +00:00
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
free(new_paragraph_data);
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +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. */
|
|
|
|
size_t quote_length(const char *line)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
regmatch_t matches;
|
|
|
|
int rc = regexec("ereg, line, 1, &matches, 0);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1)
|
|
|
|
return 0;
|
|
|
|
/* matches.rm_so should be 0, since the quote string should start
|
|
|
|
* with the caret ^. */
|
|
|
|
return matches.rm_eo;
|
|
|
|
#else /* !HAVE_REGEX_H */
|
|
|
|
size_t qdepth = 0;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Compute quote depth level. */
|
|
|
|
while (strncmp(line + qdepth, quotestr, quotelen) == 0)
|
|
|
|
qdepth += quotelen;
|
|
|
|
return qdepth;
|
|
|
|
#endif /* !HAVE_REGEX_H */
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +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. */
|
|
|
|
bool quotes_match(const char *a_line, size_t a_quote, const char
|
|
|
|
*b_line)
|
|
|
|
{
|
|
|
|
/* Here is the assumption about a_quote. */
|
|
|
|
assert(a_quote == quote_length(a_line));
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
return (a_quote == quote_length(b_line) &&
|
|
|
|
strncmp(a_line, b_line, a_quote) == 0);
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +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. */
|
|
|
|
bool indents_match(const char *a_line, size_t a_indent, const char
|
|
|
|
*b_line, size_t b_indent)
|
|
|
|
{
|
|
|
|
assert(a_indent == indent_length(a_line));
|
|
|
|
assert(b_indent == indent_length(b_line));
|
|
|
|
|
|
|
|
return (b_indent <= a_indent &&
|
|
|
|
strncmp(a_line, b_line, b_indent) == 0);
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-25 02:33:45 +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 isn't turned on.
|
|
|
|
* The reason for number 5) is that if autoindent isn't turned on,
|
|
|
|
* then an indented line is expected to start a paragraph, as in
|
|
|
|
* books. Thus, nano can justify an indented paragraph only if
|
|
|
|
* autoindent is turned on. */
|
|
|
|
bool begpar(const filestruct *const foo)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2005-11-09 18:26:44 +00:00
|
|
|
size_t quote_len, indent_len, temp_id_len;
|
|
|
|
|
|
|
|
if (foo == NULL)
|
|
|
|
return FALSE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Case 1). */
|
2005-11-09 18:26:44 +00:00
|
|
|
if (foo == openfile->fileage)
|
2005-07-25 02:33:45 +00:00
|
|
|
return TRUE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
quote_len = quote_length(foo->data);
|
|
|
|
indent_len = indent_length(foo->data + quote_len);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Not part of a paragraph. */
|
|
|
|
if (foo->data[quote_len + indent_len] == '\0')
|
|
|
|
return FALSE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Case 3). */
|
|
|
|
if (!quotes_match(foo->data, quote_len, foo->prev->data))
|
|
|
|
return TRUE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
temp_id_len = indent_length(foo->prev->data + quote_len);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Case 2) or 5) or 4). */
|
|
|
|
if (foo->prev->data[quote_len + temp_id_len] == '\0' ||
|
|
|
|
(quote_len == 0 && indent_len > 0
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
&& !ISSET(AUTOINDENT)
|
|
|
|
#endif
|
|
|
|
) || !indents_match(foo->prev->data + quote_len, temp_id_len,
|
|
|
|
foo->data + quote_len, indent_len))
|
|
|
|
return TRUE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Is foo inside a paragraph? */
|
|
|
|
bool inpar(const filestruct *const foo)
|
|
|
|
{
|
|
|
|
size_t quote_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (foo == NULL)
|
|
|
|
return FALSE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
quote_len = quote_length(foo->data);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-09 20:33:42 +00:00
|
|
|
return (foo->data[quote_len + indent_length(foo->data +
|
|
|
|
quote_len)] != '\0');
|
2005-07-25 02:33:45 +00:00
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-09 23:06:44 +00:00
|
|
|
/* Move the next par_len lines, starting with first_line, into the
|
2005-11-09 18:49:16 +00:00
|
|
|
* justify buffer, leaving copies of those lines in place. Assume that
|
|
|
|
* par_len is greater than zero, and that there are enough lines after
|
2005-11-10 21:20:32 +00:00
|
|
|
* first_line. */
|
|
|
|
void backup_lines(filestruct *first_line, size_t par_len)
|
2005-07-25 02:33:45 +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 = openfile->current_x;
|
|
|
|
ssize_t fl_lineno_save = first_line->lineno;
|
|
|
|
ssize_t edittop_lineno_save = openfile->edittop->lineno;
|
|
|
|
ssize_t current_lineno_save = openfile->current->lineno;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
bool old_mark_set = openfile->mark_set;
|
|
|
|
ssize_t mb_lineno_save = 0;
|
|
|
|
size_t mark_begin_x_save = 0;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
|
|
|
if (old_mark_set) {
|
2005-07-25 02:33:45 +00:00
|
|
|
mb_lineno_save = openfile->mark_begin->lineno;
|
|
|
|
mark_begin_x_save = openfile->mark_begin_x;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-11-10 06:01:41 +00:00
|
|
|
/* par_len will be one greater than the number of lines between
|
|
|
|
* current and filebot if filebot is the last line in the
|
|
|
|
* paragraph. */
|
2005-11-09 18:49:16 +00:00
|
|
|
assert(par_len > 0 && openfile->current->lineno + par_len <=
|
2005-12-01 05:49:08 +00:00
|
|
|
openfile->filebot->lineno + 1);
|
2005-11-09 18:49:16 +00:00
|
|
|
|
2005-11-09 18:58:04 +00:00
|
|
|
/* Move bot down par_len lines to the line after the last line of
|
|
|
|
* the paragraph, if there is one. */
|
|
|
|
for (i = par_len; i > 0 && bot != openfile->filebot; i--)
|
2005-07-25 02:33:45 +00:00
|
|
|
bot = bot->next;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Move the paragraph from the current buffer's filestruct to the
|
|
|
|
* justify buffer. */
|
2005-11-09 18:58:04 +00:00
|
|
|
move_to_filestruct(&jusbuffer, &jusbottom, top, 0, bot,
|
2005-11-09 23:27:51 +00:00
|
|
|
(i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Copy the paragraph back to the current buffer's filestruct from
|
|
|
|
* the justify buffer. */
|
2014-04-14 09:14:39 +00:00
|
|
|
copy_from_filestruct(jusbuffer);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Move upward from the last line of the paragraph to the first
|
|
|
|
* line, putting first_line, edittop, current, and mark_begin at the
|
|
|
|
* same lines in the copied paragraph that they had in the original
|
|
|
|
* paragraph. */
|
2007-04-22 15:04:05 +00:00
|
|
|
if (openfile->current != openfile->fileage) {
|
2005-11-09 18:58:04 +00:00
|
|
|
top = openfile->current->prev;
|
2007-04-22 15:04:05 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (old_mark_set &&
|
|
|
|
openfile->current->lineno == mb_lineno_save) {
|
|
|
|
openfile->mark_begin = openfile->current;
|
|
|
|
openfile->mark_begin_x = mark_begin_x_save;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else
|
2005-11-09 18:58:04 +00:00
|
|
|
top = openfile->current;
|
2005-11-10 03:40:45 +00:00
|
|
|
for (i = par_len; i > 0 && top != NULL; i--) {
|
2005-07-25 02:33:45 +00:00
|
|
|
if (top->lineno == fl_lineno_save)
|
|
|
|
first_line = top;
|
|
|
|
if (top->lineno == edittop_lineno_save)
|
|
|
|
openfile->edittop = top;
|
|
|
|
if (top->lineno == current_lineno_save)
|
|
|
|
openfile->current = top;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
if (old_mark_set && top->lineno == mb_lineno_save) {
|
|
|
|
openfile->mark_begin = top;
|
|
|
|
openfile->mark_begin_x = mark_begin_x_save;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-07-25 02:33:45 +00:00
|
|
|
#endif
|
|
|
|
top = top->prev;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Put current_x at the same place in the copied paragraph that it
|
|
|
|
* had in the original paragraph. */
|
|
|
|
openfile->current_x = current_x_save;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
set_modified();
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-29 18:34:45 +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 TRUE if
|
2006-05-22 01:26:24 +00:00
|
|
|
* we found a paragraph, and FALSE if there was an error or we didn't
|
2005-11-29 18:34:45 +00:00
|
|
|
* find a paragraph.
|
2005-07-25 02:33:45 +00:00
|
|
|
*
|
|
|
|
* See the comment at begpar() for more about when a line is the
|
|
|
|
* beginning of a paragraph. */
|
2005-11-29 18:34:45 +00:00
|
|
|
bool find_paragraph(size_t *const quote, size_t *const par)
|
2005-07-25 02:33:45 +00:00
|
|
|
{
|
|
|
|
size_t quote_len;
|
|
|
|
/* Length of the initial quotation of the paragraph we search
|
|
|
|
* for. */
|
|
|
|
size_t par_len;
|
|
|
|
/* Number of lines in the paragraph we search for. */
|
|
|
|
filestruct *current_save;
|
|
|
|
/* The line at the beginning of the paragraph we search for. */
|
|
|
|
ssize_t current_y_save;
|
|
|
|
/* The y-coordinate at the beginning of the paragraph we search
|
|
|
|
* for. */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
if (quoterc != 0) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Bad quote string %s: %s"), quotestr, quoteerr);
|
2005-07-25 02:33:45 +00:00
|
|
|
return FALSE;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
assert(openfile->current != NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-11 03:55:52 +00:00
|
|
|
/* If we're at the end of the last line of the file, it means that
|
|
|
|
* there aren't any paragraphs left, so get out. */
|
|
|
|
if (openfile->current == openfile->filebot && openfile->current_x ==
|
|
|
|
strlen(openfile->filebot->data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* If the current line isn't in a paragraph, move forward to the
|
2005-11-11 05:13:28 +00:00
|
|
|
* last line of the next paragraph, if any. */
|
2005-07-25 02:33:45 +00:00
|
|
|
if (!inpar(openfile->current)) {
|
|
|
|
do_para_end(FALSE);
|
2005-11-29 18:25:53 +00:00
|
|
|
|
2005-11-11 05:13:28 +00:00
|
|
|
/* If we end up past the beginning of the line, it means that
|
|
|
|
* we're at the end of the last line of the file, and the line
|
|
|
|
* isn't blank, in which case the last line of the file is the
|
|
|
|
* last line of the next paragraph.
|
|
|
|
*
|
|
|
|
* Otherwise, if we end up on a line that's in a paragraph, it
|
|
|
|
* means that we're on the line after the last line of the next
|
|
|
|
* paragraph, in which case we should move back to the last line
|
|
|
|
* of the next paragraph. */
|
|
|
|
if (openfile->current_x == 0) {
|
|
|
|
if (!inpar(openfile->current->prev))
|
|
|
|
return FALSE;
|
|
|
|
if (openfile->current != openfile->fileage)
|
|
|
|
openfile->current = openfile->current->prev;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-11-29 18:25:53 +00:00
|
|
|
|
2005-11-11 05:13:28 +00:00
|
|
|
/* If the current line isn't the first line of the paragraph, move
|
2005-11-29 18:34:45 +00:00
|
|
|
* back to the first line of the paragraph. */
|
|
|
|
if (!begpar(openfile->current))
|
2005-07-25 02:33:45 +00:00
|
|
|
do_para_begin(FALSE);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Now current is the first line of the paragraph. Set quote_len to
|
|
|
|
* the quotation length of that line, and set par_len to the number
|
2005-11-11 05:13:28 +00:00
|
|
|
* of lines in this paragraph. */
|
2005-07-25 02:33:45 +00:00
|
|
|
quote_len = quote_length(openfile->current->data);
|
|
|
|
current_save = openfile->current;
|
|
|
|
current_y_save = openfile->current_y;
|
|
|
|
do_para_end(FALSE);
|
|
|
|
par_len = openfile->current->lineno - current_save->lineno;
|
2005-11-29 18:25:53 +00:00
|
|
|
|
2005-11-11 05:13:28 +00:00
|
|
|
/* If we end up past the beginning of the line, it means that we're
|
|
|
|
* at the end of the last line of the file, and the line isn't
|
|
|
|
* blank, in which case the last line of the file is part of the
|
|
|
|
* paragraph. */
|
2005-11-11 04:14:33 +00:00
|
|
|
if (openfile->current_x > 0)
|
|
|
|
par_len++;
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->current = current_save;
|
|
|
|
openfile->current_y = current_y_save;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Save the values of quote_len and par_len. */
|
|
|
|
assert(quote != NULL && par != NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
*quote = quote_len;
|
|
|
|
*par = par_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
return TRUE;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* If full_justify is TRUE, justify the entire file. Otherwise, justify
|
|
|
|
* the current paragraph. */
|
|
|
|
void do_justify(bool full_justify)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2005-07-25 02:33:45 +00:00
|
|
|
filestruct *first_par_line = NULL;
|
2005-11-30 21:19:42 +00:00
|
|
|
/* Will be the first line of the justified paragraph(s), if any.
|
|
|
|
* For restoring after unjustify. */
|
2005-11-10 19:28:27 +00:00
|
|
|
filestruct *last_par_line = NULL;
|
2005-11-09 19:06:01 +00:00
|
|
|
/* Will be the line after the last line of the justified
|
2005-11-30 21:19:42 +00:00
|
|
|
* paragraph(s), if any. Also for restoring after unjustify. */
|
2005-11-10 06:07:57 +00:00
|
|
|
bool filebot_inpar = FALSE;
|
2005-11-10 06:01:41 +00:00
|
|
|
/* Whether the text at filebot is part of the current
|
|
|
|
* paragraph. */
|
2014-08-29 20:03:58 +00:00
|
|
|
int kbinput;
|
|
|
|
/* The first keystroke after a justification. */
|
|
|
|
functionptrtype func;
|
|
|
|
/* The function associated with that keystroke. */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 21:23:11 +00:00
|
|
|
/* We save these variables to be restored if the user
|
|
|
|
* unjustifies. */
|
2005-11-10 19:56:26 +00:00
|
|
|
filestruct *edittop_save = openfile->edittop;
|
|
|
|
filestruct *current_save = openfile->current;
|
2005-07-25 02:33:45 +00:00
|
|
|
size_t current_x_save = openfile->current_x;
|
|
|
|
size_t totsize_save = openfile->totsize;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
filestruct *mark_begin_save = openfile->mark_begin;
|
|
|
|
size_t mark_begin_x_save = openfile->mark_begin_x;
|
|
|
|
#endif
|
2005-11-10 19:56:26 +00:00
|
|
|
bool modified_save = openfile->modified;
|
|
|
|
|
2005-11-09 19:06:01 +00:00
|
|
|
/* Move to the beginning of the current line, so that justifying at
|
2005-11-11 04:14:33 +00:00
|
|
|
* the end of the last line of the file, if that line isn't blank,
|
|
|
|
* will work the first time through. */
|
2005-11-09 19:06:01 +00:00
|
|
|
openfile->current_x = 0;
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* If we're justifying the entire file, start at the beginning. */
|
|
|
|
if (full_justify)
|
|
|
|
openfile->current = openfile->fileage;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (TRUE) {
|
|
|
|
size_t i;
|
|
|
|
/* Generic loop variable. */
|
2005-11-30 21:19:42 +00:00
|
|
|
filestruct *curr_first_par_line;
|
|
|
|
/* The first line of the current paragraph. */
|
2005-07-25 02:33:45 +00:00
|
|
|
size_t quote_len;
|
2005-11-30 21:19:42 +00:00
|
|
|
/* Length of the initial quotation of the current
|
|
|
|
* paragraph. */
|
2005-07-25 02:33:45 +00:00
|
|
|
size_t indent_len;
|
2005-11-30 21:19:42 +00:00
|
|
|
/* Length of the initial indentation of the current
|
|
|
|
* paragraph. */
|
2005-07-25 02:33:45 +00:00
|
|
|
size_t par_len;
|
2005-11-30 21:19:42 +00:00
|
|
|
/* Number of lines in the current paragraph. */
|
2005-07-25 02:33:45 +00:00
|
|
|
ssize_t break_pos;
|
|
|
|
/* Where we will break lines. */
|
|
|
|
char *indent_string;
|
|
|
|
/* The first indentation that doesn't match the initial
|
2005-11-30 21:19:42 +00:00
|
|
|
* indentation of the current paragraph. This is put at the
|
|
|
|
* beginning of every line broken off the first justified
|
|
|
|
* line of the paragraph. Note that this works because a
|
|
|
|
* paragraph can only contain two indentations at most: the
|
|
|
|
* initial one, and a different one starting on a line after
|
|
|
|
* the first. See the comment at begpar() for more about
|
|
|
|
* when a line is part of a paragraph. */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* 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
|
|
|
|
* length (number of lines). Don't refresh the screen yet,
|
|
|
|
* since we'll do that after we justify.
|
|
|
|
*
|
|
|
|
* If the search failed, we do one of two things. If we're
|
2005-11-11 03:17:44 +00:00
|
|
|
* justifying the whole file, and we've found at least one
|
|
|
|
* paragraph, it means that we should justify all the way to the
|
2005-07-25 02:33:45 +00:00
|
|
|
* last line of the file, so set the last line of the text to be
|
|
|
|
* justified to the last line of the file and break out of the
|
|
|
|
* loop. Otherwise, it means that there are no paragraph(s) to
|
|
|
|
* justify, so refresh the screen and get out. */
|
2005-11-29 18:34:45 +00:00
|
|
|
if (!find_paragraph("e_len, &par_len)) {
|
2005-11-11 03:17:44 +00:00
|
|
|
if (full_justify && first_par_line != NULL) {
|
2005-07-25 02:33:45 +00:00
|
|
|
last_par_line = openfile->filebot;
|
2005-07-24 19:57:51 +00:00
|
|
|
break;
|
2005-07-25 02:33:45 +00:00
|
|
|
} else {
|
2016-04-25 19:14:18 +00:00
|
|
|
refresh_needed = TRUE;
|
2005-07-25 02:33:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-11-10 06:01:41 +00:00
|
|
|
/* par_len will be one greater than the number of lines between
|
|
|
|
* current and filebot if filebot is the last line in the
|
|
|
|
* paragraph. Set filebot_inpar to TRUE if this is the case. */
|
|
|
|
filebot_inpar = (openfile->current->lineno + par_len ==
|
|
|
|
openfile->filebot->lineno + 1);
|
|
|
|
|
2005-11-10 21:20:32 +00:00
|
|
|
/* If we haven't already done it, move the original paragraph(s)
|
|
|
|
* to the justify buffer, splice a copy of the original
|
|
|
|
* paragraph(s) into the file in the same place, and set
|
|
|
|
* first_par_line to the first line of the copy. */
|
|
|
|
if (first_par_line == NULL) {
|
|
|
|
backup_lines(openfile->current, full_justify ?
|
2005-11-10 21:57:56 +00:00
|
|
|
openfile->filebot->lineno - openfile->current->lineno +
|
|
|
|
((openfile->filebot->data[0] != '\0') ? 1 : 0) :
|
2005-11-09 23:06:44 +00:00
|
|
|
par_len);
|
2005-11-10 21:20:32 +00:00
|
|
|
first_par_line = openfile->current;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-30 21:19:42 +00:00
|
|
|
/* Set curr_first_par_line to the first line of the current
|
|
|
|
* paragraph. */
|
|
|
|
curr_first_par_line = openfile->current;
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Initialize indent_string to a blank string. */
|
|
|
|
indent_string = mallocstrcpy(NULL, "");
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Find the first indentation in the paragraph that doesn't
|
2006-05-28 18:43:21 +00:00
|
|
|
* match the indentation of the first line, and save it in
|
2005-07-25 02:33:45 +00:00
|
|
|
* indent_string. If all the indentations are the same, save
|
|
|
|
* the indentation of the first line in indent_string. */
|
|
|
|
{
|
|
|
|
const filestruct *indent_line = openfile->current;
|
|
|
|
bool past_first_line = FALSE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
for (i = 0; i < par_len; i++) {
|
|
|
|
indent_len = quote_len +
|
|
|
|
indent_length(indent_line->data + quote_len);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (indent_len != strlen(indent_string)) {
|
|
|
|
indent_string = mallocstrncpy(indent_string,
|
|
|
|
indent_line->data, indent_len + 1);
|
|
|
|
indent_string[indent_len] = '\0';
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (past_first_line)
|
|
|
|
break;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (indent_line == openfile->current)
|
|
|
|
past_first_line = TRUE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
indent_line = indent_line->next;
|
|
|
|
}
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Now tack all the lines of the paragraph together, skipping
|
|
|
|
* the quoting and indentation on all lines after the first. */
|
|
|
|
for (i = 0; i < par_len - 1; i++) {
|
|
|
|
filestruct *next_line = openfile->current->next;
|
|
|
|
size_t line_len = strlen(openfile->current->data);
|
2015-12-08 19:09:14 +00:00
|
|
|
size_t next_line_len = strlen(openfile->current->next->data);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
indent_len = quote_len +
|
2015-12-08 19:09:14 +00:00
|
|
|
indent_length(openfile->current->next->data + quote_len);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
next_line_len -= indent_len;
|
|
|
|
openfile->totsize -= indent_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* We're just about to tack the next line onto this one. If
|
|
|
|
* this line isn't empty, make sure it ends in a space. */
|
|
|
|
if (line_len > 0 &&
|
2015-12-08 19:09:14 +00:00
|
|
|
openfile->current->data[line_len - 1] != ' ') {
|
2005-07-25 02:33:45 +00:00
|
|
|
line_len++;
|
|
|
|
openfile->current->data =
|
2015-12-08 19:09:14 +00:00
|
|
|
charealloc(openfile->current->data, line_len + 1);
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->current->data[line_len - 1] = ' ';
|
|
|
|
openfile->current->data[line_len] = '\0';
|
|
|
|
openfile->totsize++;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-12-08 19:09:14 +00:00
|
|
|
openfile->current->data = charealloc(openfile->current->data,
|
|
|
|
line_len + next_line_len + 1);
|
|
|
|
strcat(openfile->current->data, next_line->data + indent_len);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2015-12-08 19:09:14 +00:00
|
|
|
/* If needed, adjust the coordinates of the mark. */
|
|
|
|
if (openfile->mark_set &&
|
|
|
|
openfile->mark_begin == next_line) {
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->mark_begin = openfile->current;
|
|
|
|
openfile->mark_begin_x += line_len - indent_len;
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-08 19:09:14 +00:00
|
|
|
/* Don't destroy edittop! */
|
|
|
|
if (next_line == openfile->edittop)
|
|
|
|
openfile->edittop = openfile->current;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
unlink_node(next_line);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* If we've removed the next line, we need to go through
|
|
|
|
* this line again. */
|
|
|
|
i--;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
par_len--;
|
|
|
|
openfile->totsize--;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Call justify_format() on the paragraph, which will remove
|
|
|
|
* excess spaces from it and change all blank characters to
|
|
|
|
* spaces. */
|
|
|
|
justify_format(openfile->current, quote_len +
|
|
|
|
indent_length(openfile->current->data + quote_len));
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-12-08 19:09:14 +00:00
|
|
|
while (par_len > 0 && strlenpt(openfile->current->data) > fill) {
|
2005-07-25 02:33:45 +00:00
|
|
|
size_t line_len = strlen(openfile->current->data);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
indent_len = strlen(indent_string);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* If this line is too long, try to wrap it to the next line
|
|
|
|
* to make it short enough. */
|
2005-07-25 22:54:16 +00:00
|
|
|
break_pos = break_line(openfile->current->data + indent_len,
|
2005-11-22 21:13:36 +00:00
|
|
|
fill - strnlenpt(openfile->current->data, indent_len)
|
|
|
|
#ifndef DISABLE_HELP
|
|
|
|
, FALSE
|
|
|
|
#endif
|
|
|
|
);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* We can't break the line, or don't need to, so get out. */
|
|
|
|
if (break_pos == -1 || break_pos + indent_len == line_len)
|
|
|
|
break;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Move forward to the character after the indentation and
|
|
|
|
* just after the space. */
|
|
|
|
break_pos += indent_len + 1;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
assert(break_pos <= line_len);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* If this paragraph is non-quoted, and autoindent isn't
|
|
|
|
* turned on, set the indentation length to zero so that the
|
|
|
|
* indentation is treated as part of the line. */
|
|
|
|
if (quote_len == 0
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
&& !ISSET(AUTOINDENT)
|
2005-07-24 19:57:51 +00:00
|
|
|
#endif
|
2005-07-25 02:33:45 +00:00
|
|
|
)
|
|
|
|
indent_len = 0;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-12-08 19:09:14 +00:00
|
|
|
/* Insert a new line after the current one. */
|
|
|
|
splice_node(openfile->current, make_new_node(openfile->current));
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Copy the text after where we're going to break the
|
|
|
|
* current line to the next line. */
|
|
|
|
openfile->current->next->data = charalloc(indent_len + 1 +
|
|
|
|
line_len - break_pos);
|
|
|
|
strncpy(openfile->current->next->data, indent_string,
|
|
|
|
indent_len);
|
|
|
|
strcpy(openfile->current->next->data + indent_len,
|
|
|
|
openfile->current->data + break_pos);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
par_len++;
|
|
|
|
openfile->totsize += indent_len + 1;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Adjust the mark coordinates to compensate for the change
|
|
|
|
* in the current line. */
|
2015-12-08 19:09:14 +00:00
|
|
|
if (openfile->mark_set &&
|
|
|
|
openfile->mark_begin == openfile->current &&
|
|
|
|
openfile->mark_begin_x > break_pos) {
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->mark_begin = openfile->current->next;
|
|
|
|
openfile->mark_begin_x -= break_pos - indent_len;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-07-25 02:33:45 +00:00
|
|
|
#endif
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Break the current line. */
|
2016-05-17 09:33:21 +00:00
|
|
|
if (ISSET(JUSTIFY_TRIM)) {
|
|
|
|
while (break_pos > 0 &&
|
|
|
|
is_blank_mbchar(&openfile->current->data[break_pos - 1])) {
|
2016-02-24 04:46:44 +00:00
|
|
|
break_pos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
null_at(&openfile->current->data, break_pos);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Go to the next line. */
|
|
|
|
par_len--;
|
2005-11-09 20:17:12 +00:00
|
|
|
openfile->current_y++;
|
|
|
|
openfile->current = openfile->current->next;
|
2005-07-25 02:33:45 +00:00
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* We're done breaking lines, so we don't need indent_string
|
|
|
|
* anymore. */
|
|
|
|
free(indent_string);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-09 20:17:12 +00:00
|
|
|
/* Go to the next line, if possible. If there is no next line,
|
|
|
|
* move to the end of the current line. */
|
2005-11-09 19:06:01 +00:00
|
|
|
if (openfile->current != openfile->filebot) {
|
|
|
|
openfile->current_y++;
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
} else
|
|
|
|
openfile->current_x = strlen(openfile->current->data);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-30 21:19:42 +00:00
|
|
|
/* Renumber the lines of the now-justified current paragraph,
|
|
|
|
* since both find_paragraph() and edit_refresh() need the line
|
|
|
|
* numbers to be right. */
|
|
|
|
renumber(curr_first_par_line);
|
2005-11-29 19:00:09 +00:00
|
|
|
|
|
|
|
/* We've just finished justifying the 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. */
|
2005-07-25 02:33:45 +00:00
|
|
|
if (!full_justify)
|
|
|
|
break;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* We are now done justifying the paragraph or the file, so clean
|
2005-11-10 19:28:27 +00:00
|
|
|
* up. current_y and totsize have been maintained above. If we
|
2005-11-29 19:00:09 +00:00
|
|
|
* actually justified something, set last_par_line to the new end of
|
|
|
|
* the paragraph. */
|
|
|
|
if (first_par_line != NULL)
|
2005-11-10 19:28:27 +00:00
|
|
|
last_par_line = openfile->current;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
edit_refresh();
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Display the shortcut list with UnJustify. */
|
2014-04-07 09:02:22 +00:00
|
|
|
uncutfunc->desc = unjust_tag;
|
2005-07-25 02:33:45 +00:00
|
|
|
display_main_list();
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Now get a keystroke and see if it's unjustify. If not, put back
|
|
|
|
* the keystroke and return. */
|
2015-05-28 13:02:29 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
do {
|
|
|
|
#endif
|
|
|
|
statusbar(_("Can now UnJustify!"));
|
2016-03-29 14:09:17 +00:00
|
|
|
reset_cursor();
|
2016-02-22 17:15:28 +00:00
|
|
|
curs_set(1);
|
2015-05-28 13:02:29 +00:00
|
|
|
kbinput = do_input(FALSE);
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
} while (kbinput == KEY_WINCH);
|
|
|
|
#endif
|
|
|
|
|
2016-06-16 18:40:09 +00:00
|
|
|
/* If needed, unset the cursor-position suppression flag, so the cursor
|
|
|
|
* position /will/ be displayed upon a return to the main loop. */
|
|
|
|
if (ISSET(CONST_UPDATE))
|
|
|
|
do_cursorpos(TRUE);
|
|
|
|
|
2014-08-29 20:03:58 +00:00
|
|
|
func = func_from_key(&kbinput);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-12-05 10:16:26 +00:00
|
|
|
if (func == do_uncut_text
|
|
|
|
#ifndef NANO_TINY
|
2016-05-30 07:09:36 +00:00
|
|
|
|| func == do_undo
|
2015-12-05 10:16:26 +00:00
|
|
|
#endif
|
|
|
|
) {
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Splice the justify buffer back into the file, but only if we
|
|
|
|
* actually justified something. */
|
|
|
|
if (first_par_line != NULL) {
|
|
|
|
filestruct *top_save;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Partition the filestruct so that it contains only the
|
|
|
|
* text of the justified paragraph. */
|
|
|
|
filepart = partition_filestruct(first_par_line, 0,
|
2005-11-18 20:21:48 +00:00
|
|
|
last_par_line, filebot_inpar ?
|
|
|
|
strlen(last_par_line->data) : 0);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Remove the text of the justified paragraph, and
|
2005-11-09 23:06:44 +00:00
|
|
|
* replace it with the text in the justify buffer. */
|
2005-07-25 02:33:45 +00:00
|
|
|
free_filestruct(openfile->fileage);
|
|
|
|
openfile->fileage = jusbuffer;
|
|
|
|
openfile->filebot = jusbottom;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
top_save = openfile->fileage;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* 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);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-03-27 13:46:50 +00:00
|
|
|
/* Renumber, starting with the beginning line of the old
|
|
|
|
* partition. */
|
2005-07-25 02:33:45 +00:00
|
|
|
renumber(top_save);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-10 19:28:27 +00:00
|
|
|
/* Restore the justify we just did (ungrateful user!). */
|
|
|
|
openfile->edittop = edittop_save;
|
|
|
|
openfile->current = current_save;
|
|
|
|
openfile->current_x = current_x_save;
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->totsize = totsize_save;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
if (openfile->mark_set) {
|
|
|
|
openfile->mark_begin = mark_begin_save;
|
|
|
|
openfile->mark_begin_x = mark_begin_x_save;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
openfile->modified = modified_save;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Clear the justify buffer. */
|
|
|
|
jusbuffer = NULL;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (!openfile->modified)
|
|
|
|
titlebar(NULL);
|
2016-04-25 19:14:18 +00:00
|
|
|
refresh_needed = TRUE;
|
2005-07-25 02:33:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-12-03 09:03:45 +00:00
|
|
|
/* Put the keystroke back into the queue. */
|
2015-12-02 19:11:03 +00:00
|
|
|
unget_kbinput(kbinput, meta_key, func_key);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-04-10 19:16:19 +00:00
|
|
|
/* Set the desired screen column (always zero, except at EOF). */
|
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
|
2015-12-05 10:16:26 +00:00
|
|
|
#ifndef NANO_TINY
|
2015-12-03 09:03:45 +00:00
|
|
|
/* Throw away the entire undo stack, to prevent a crash when
|
|
|
|
* the user tries to undo something in the justified text. */
|
2016-01-15 16:44:50 +00:00
|
|
|
discard_until(NULL, openfile);
|
2015-12-03 09:03:45 +00:00
|
|
|
openfile->current_undo = NULL;
|
2015-12-05 10:16:26 +00:00
|
|
|
#endif
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Blow away the text in the justify buffer. */
|
|
|
|
free_filestruct(jusbuffer);
|
|
|
|
jusbuffer = NULL;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
blank_statusbar();
|
|
|
|
|
|
|
|
/* Display the shortcut list with UnCut. */
|
2014-04-07 09:02:22 +00:00
|
|
|
uncutfunc->desc = uncut_tag;
|
2005-07-25 02:33:45 +00:00
|
|
|
display_main_list();
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Justify the current paragraph. */
|
2005-07-25 02:33:45 +00:00
|
|
|
void do_justify_void(void)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2005-07-25 02:33:45 +00:00
|
|
|
do_justify(FALSE);
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Justify the entire file. */
|
2005-07-25 02:33:45 +00:00
|
|
|
void do_full_justify(void)
|
|
|
|
{
|
|
|
|
do_justify(TRUE);
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-07-25 02:33:45 +00:00
|
|
|
#endif /* !DISABLE_JUSTIFY */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
#ifndef DISABLE_SPELLER
|
|
|
|
/* A word is misspelled in the file. Let the user replace it. We
|
|
|
|
* return FALSE if the user cancels. */
|
|
|
|
bool do_int_spell_fix(const char *word)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2016-03-31 11:14:25 +00:00
|
|
|
char *save_search, *exp_word;
|
2016-04-04 15:53:26 +00:00
|
|
|
size_t current_x_save = openfile->current_x;
|
2005-07-25 02:33:45 +00:00
|
|
|
filestruct *edittop_save = openfile->edittop;
|
|
|
|
filestruct *current_save = openfile->current;
|
|
|
|
/* Save where we are. */
|
2016-04-04 15:43:31 +00:00
|
|
|
bool proceed = FALSE;
|
2016-03-31 11:27:16 +00:00
|
|
|
/* The return value of this function. */
|
2016-03-31 11:14:25 +00:00
|
|
|
bool result;
|
|
|
|
/* The return value of searching for a misspelled word. */
|
2015-04-21 17:37:59 +00:00
|
|
|
unsigned stash[sizeof(flags) / sizeof(flags[0])];
|
|
|
|
/* A storage place for the current flag settings. */
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-24 19:57:51 +00:00
|
|
|
bool old_mark_set = openfile->mark_set;
|
2005-07-25 02:33:45 +00:00
|
|
|
bool right_side_up = FALSE;
|
|
|
|
/* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
|
|
|
|
* FALSE if (current, current_x) is. */
|
|
|
|
filestruct *top, *bot;
|
|
|
|
size_t top_x, bot_x;
|
|
|
|
#endif
|
|
|
|
|
2015-04-21 17:37:59 +00:00
|
|
|
/* Save the settings of the global flags. */
|
|
|
|
memcpy(stash, flags, sizeof(flags));
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Make sure spell-check is case sensitive. */
|
|
|
|
SET(CASE_SENSITIVE);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Make sure spell-check goes forward only. */
|
|
|
|
UNSET(BACKWARDS_SEARCH);
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
/* Make sure spell-check doesn't use regular expressions. */
|
|
|
|
UNSET(USE_REGEXP);
|
|
|
|
#endif
|
|
|
|
|
2016-03-28 19:14:33 +00:00
|
|
|
/* Save the current search string, then set it to the misspelled word. */
|
2005-07-25 02:33:45 +00:00
|
|
|
save_search = last_search;
|
|
|
|
last_search = mallocstrcpy(NULL, word);
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-05-04 15:49:37 +00:00
|
|
|
/* If the mark is on, start at the beginning of the marked region. */
|
2005-07-24 19:57:51 +00:00
|
|
|
if (old_mark_set) {
|
2005-07-25 02:33:45 +00:00
|
|
|
mark_order((const filestruct **)&top, &top_x,
|
|
|
|
(const filestruct **)&bot, &bot_x, &right_side_up);
|
2016-05-04 15:49:37 +00:00
|
|
|
/* If the region is marked normally, swap the end points, so that
|
|
|
|
* (current, current_x) (where searching starts) is at the top. */
|
|
|
|
if (right_side_up) {
|
|
|
|
openfile->current = top;
|
|
|
|
openfile->current_x = (size_t)(top_x - 1);
|
|
|
|
openfile->mark_begin = bot;
|
|
|
|
openfile->mark_begin_x = bot_x;
|
2016-05-14 09:53:52 +00:00
|
|
|
} else
|
|
|
|
openfile->current_x = (size_t)(openfile->current_x - 1);
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->mark_set = FALSE;
|
2016-05-04 15:49:37 +00:00
|
|
|
} else
|
2005-07-24 19:57:51 +00:00
|
|
|
#endif
|
2016-05-04 15:49:37 +00:00
|
|
|
/* Otherwise, start from the top of the file. */
|
|
|
|
{
|
|
|
|
openfile->edittop = openfile->fileage;
|
|
|
|
openfile->current = openfile->fileage;
|
|
|
|
openfile->current_x = (size_t)-1;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-31 11:14:25 +00:00
|
|
|
/* Find the first whole occurrence of word. */
|
2016-05-01 11:38:54 +00:00
|
|
|
result = findnextstr(TRUE, NULL, 0, word, NULL);
|
2016-03-31 11:14:25 +00:00
|
|
|
|
|
|
|
/* The word must exist; if not, something is wrong. */
|
|
|
|
if (result == 0)
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, "Internal error: "
|
|
|
|
"speller listed unfindable word: %s", word);
|
2016-03-31 11:14:25 +00:00
|
|
|
else if (result == 1) {
|
|
|
|
exp_word = display_string(openfile->current->data, xplustabs(),
|
2016-04-04 15:53:26 +00:00
|
|
|
strlenpt(word), FALSE);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-31 11:27:16 +00:00
|
|
|
edit_refresh();
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-31 11:27:16 +00:00
|
|
|
spotlight(TRUE, exp_word);
|
2005-07-25 02:33:45 +00:00
|
|
|
|
2016-03-31 11:14:25 +00:00
|
|
|
/* Let the user supply a correctly spelled alternative. */
|
2016-03-31 11:27:16 +00:00
|
|
|
proceed = (do_prompt(FALSE,
|
2006-02-07 21:11:05 +00:00
|
|
|
#ifndef DISABLE_TABCOMP
|
2016-03-31 11:27:16 +00:00
|
|
|
TRUE,
|
2006-02-07 21:11:05 +00:00
|
|
|
#endif
|
2016-03-31 11:27:16 +00:00
|
|
|
MSPELL, word,
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
2016-03-31 11:27:16 +00:00
|
|
|
NULL,
|
2005-07-24 19:57:51 +00:00
|
|
|
#endif
|
2016-03-31 11:27:16 +00:00
|
|
|
edit_refresh, _("Edit a replacement")) != -1);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-31 11:27:16 +00:00
|
|
|
spotlight(FALSE, exp_word);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-31 11:27:16 +00:00
|
|
|
free(exp_word);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-31 11:14:25 +00:00
|
|
|
/* If a replacement was given, go through all occurrences. */
|
2016-03-31 11:27:16 +00:00
|
|
|
if (proceed && strcmp(word, answer) != 0) {
|
|
|
|
openfile->current_x--;
|
2016-05-04 15:49:37 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* Replacements should happen only in the marked region. */
|
|
|
|
openfile->mark_set = old_mark_set;
|
|
|
|
#endif
|
2016-05-03 09:17:45 +00:00
|
|
|
do_replace_loop(TRUE, current_save, ¤t_x_save, word);
|
2016-05-04 17:31:59 +00:00
|
|
|
|
|
|
|
/* TRANSLATORS: Shown after fixing misspellings in one word. */
|
2016-05-18 08:25:16 +00:00
|
|
|
statusbar(_("Next word..."));
|
2016-05-04 17:31:59 +00:00
|
|
|
napms(400);
|
2016-03-31 11:27:16 +00:00
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
if (old_mark_set) {
|
2016-05-04 15:49:37 +00:00
|
|
|
/* Restore the (compensated) end points of the marked region. */
|
2005-07-25 02:33:45 +00:00
|
|
|
if (right_side_up) {
|
2016-05-04 15:49:37 +00:00
|
|
|
openfile->current = openfile->mark_begin;
|
|
|
|
openfile->current_x = openfile->mark_begin_x;
|
|
|
|
openfile->mark_begin = top;
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->mark_begin_x = top_x;
|
|
|
|
} else {
|
2016-05-04 15:49:37 +00:00
|
|
|
openfile->current = top;
|
|
|
|
openfile->current_x = top_x;
|
2005-07-25 02:33:45 +00:00
|
|
|
}
|
|
|
|
openfile->mark_set = TRUE;
|
2016-05-04 15:49:37 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Restore the (compensated) cursor position. */
|
|
|
|
openfile->current = current_save;
|
|
|
|
openfile->current_x = current_x_save;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 19:14:33 +00:00
|
|
|
/* Restore the string that was last searched for. */
|
2005-07-25 02:33:45 +00:00
|
|
|
free(last_search);
|
|
|
|
last_search = save_search;
|
|
|
|
|
2016-05-04 15:49:37 +00:00
|
|
|
/* Restore the viewport to where it was. */
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->edittop = edittop_save;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-04-21 17:37:59 +00:00
|
|
|
/* Restore the settings of the global flags. */
|
|
|
|
memcpy(flags, stash, sizeof(flags));
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-31 11:27:16 +00:00
|
|
|
return proceed;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Internal (integrated) spell checking using the spell program,
|
|
|
|
* filtered through the sort and uniq programs. Return NULL for normal
|
|
|
|
* termination, and the error string otherwise. */
|
2005-07-25 02:33:45 +00:00
|
|
|
const char *do_int_speller(const char *tempfile_name)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2005-07-25 02:33:45 +00:00
|
|
|
char *read_buff, *read_buff_ptr, *read_buff_word;
|
|
|
|
size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
|
|
|
|
int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
|
|
|
|
pid_t pid_spell, pid_sort, pid_uniq;
|
|
|
|
int spell_status, sort_status, uniq_status;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Create all three pipes up front. */
|
2016-03-30 12:30:14 +00:00
|
|
|
if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || pipe(uniq_fd) == -1)
|
2005-07-25 02:33:45 +00:00
|
|
|
return _("Could not create pipe");
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
statusbar(_("Creating misspelled word list, please wait..."));
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* A new process to run spell in. */
|
|
|
|
if ((pid_spell = fork()) == 0) {
|
2006-07-28 17:06:27 +00:00
|
|
|
/* Child continues (i.e. future spell process). */
|
2005-07-25 02:33:45 +00:00
|
|
|
close(spell_fd[0]);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Replace the standard input with the temp file. */
|
|
|
|
if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1)
|
|
|
|
goto close_pipes_and_exit;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
close(tempfile_fd);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Send spell's standard output to the pipe. */
|
|
|
|
if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
close(spell_fd[1]);
|
|
|
|
|
2005-11-29 20:01:06 +00:00
|
|
|
/* Start the spell program; we are using $PATH. */
|
2005-07-25 02:33:45 +00:00
|
|
|
execlp("spell", "spell", NULL);
|
|
|
|
|
|
|
|
/* This should not be reached if spell is found. */
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent continues here. */
|
|
|
|
close(spell_fd[1]);
|
|
|
|
|
|
|
|
/* A new process to run sort in. */
|
|
|
|
if ((pid_sort = fork()) == 0) {
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Child continues (i.e. future sort process). Replace the
|
2005-07-25 02:33:45 +00:00
|
|
|
* standard input with the standard output of the old pipe. */
|
|
|
|
if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
|
|
|
|
|
|
|
close(spell_fd[0]);
|
|
|
|
|
|
|
|
/* Send sort's standard output to the new pipe. */
|
|
|
|
if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
|
|
|
|
|
|
|
close(sort_fd[1]);
|
|
|
|
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Start the sort program. Use -f to ignore case. */
|
2005-07-25 02:33:45 +00:00
|
|
|
execlp("sort", "sort", "-f", NULL);
|
|
|
|
|
|
|
|
/* This should not be reached if sort is found. */
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
close(spell_fd[0]);
|
|
|
|
close(sort_fd[1]);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* A new process to run uniq in. */
|
|
|
|
if ((pid_uniq = fork()) == 0) {
|
2006-07-28 17:06:27 +00:00
|
|
|
/* Child continues (i.e. future uniq process). Replace the
|
2005-07-25 02:33:45 +00:00
|
|
|
* standard input with the standard output of the old pipe. */
|
|
|
|
if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
close(sort_fd[0]);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Send uniq's standard output to the new pipe. */
|
|
|
|
if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
|
|
|
goto close_pipes_and_exit;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
close(uniq_fd[1]);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Start the uniq program; we are using PATH. */
|
|
|
|
execlp("uniq", "uniq", NULL);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* This should not be reached if uniq is found. */
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
close(sort_fd[0]);
|
|
|
|
close(uniq_fd[1]);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* The child process was not forked successfully. */
|
|
|
|
if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) {
|
|
|
|
close(uniq_fd[0]);
|
|
|
|
return _("Could not fork");
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Get the system pipe buffer size. */
|
|
|
|
if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) {
|
|
|
|
close(uniq_fd[0]);
|
|
|
|
return _("Could not get size of pipe buffer");
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Read in the returned spelling errors. */
|
|
|
|
read_buff_read = 0;
|
|
|
|
read_buff_size = pipe_buff_size + 1;
|
|
|
|
read_buff = read_buff_ptr = charalloc(read_buff_size);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-30 12:30:14 +00:00
|
|
|
while ((bytesread = read(uniq_fd[0], read_buff_ptr, pipe_buff_size)) > 0) {
|
2005-07-25 02:33:45 +00:00
|
|
|
read_buff_read += bytesread;
|
|
|
|
read_buff_size += pipe_buff_size;
|
2016-03-30 12:30:14 +00:00
|
|
|
read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size);
|
2005-07-25 02:33:45 +00:00
|
|
|
read_buff_ptr += read_buff_read;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
*read_buff_ptr = '\0';
|
|
|
|
close(uniq_fd[0]);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Process the spelling errors. */
|
|
|
|
read_buff_word = read_buff_ptr = read_buff;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
while (*read_buff_ptr != '\0') {
|
|
|
|
if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
|
|
|
|
*read_buff_ptr = '\0';
|
|
|
|
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;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-07-25 02:33:45 +00:00
|
|
|
read_buff_ptr++;
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Special case: the last word doesn't end with '\r' or '\n'. */
|
|
|
|
if (read_buff_word != read_buff_ptr)
|
|
|
|
do_int_spell_fix(read_buff_word);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
free(read_buff);
|
2005-12-08 07:09:08 +00:00
|
|
|
search_replace_abort();
|
2016-04-25 19:14:18 +00:00
|
|
|
refresh_needed = TRUE;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Process the end of the three processes. */
|
2005-07-25 02:33:45 +00:00
|
|
|
waitpid(pid_spell, &spell_status, 0);
|
|
|
|
waitpid(pid_sort, &sort_status, 0);
|
|
|
|
waitpid(pid_uniq, &uniq_status, 0);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
|
|
|
|
return _("Error invoking \"spell\"");
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2014-04-15 15:02:43 +00:00
|
|
|
if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status))
|
2005-07-25 02:33:45 +00:00
|
|
|
return _("Error invoking \"sort -f\"");
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status))
|
|
|
|
return _("Error invoking \"uniq\"");
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-03-30 12:30:14 +00:00
|
|
|
/* When all went okay. */
|
2005-07-25 02:33:45 +00:00
|
|
|
return NULL;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
close_pipes_and_exit:
|
|
|
|
/* Don't leak any handles. */
|
|
|
|
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);
|
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* External (alternate) spell checking. Return NULL for normal
|
|
|
|
* termination, and the error string otherwise. */
|
2005-07-25 02:33:45 +00:00
|
|
|
const char *do_alt_speller(char *tempfile_name)
|
|
|
|
{
|
|
|
|
int alt_spell_status;
|
|
|
|
size_t current_x_save = openfile->current_x;
|
|
|
|
ssize_t current_y_save = openfile->current_y;
|
|
|
|
ssize_t lineno_save = openfile->current->lineno;
|
2015-03-17 20:10:59 +00:00
|
|
|
struct stat spellfileinfo;
|
2015-03-23 04:32:45 +00:00
|
|
|
time_t timestamp;
|
2005-07-25 02:33:45 +00:00
|
|
|
pid_t pid_spell;
|
|
|
|
char *ptr;
|
|
|
|
static int arglen = 3;
|
|
|
|
static char **spellargs = NULL;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
bool old_mark_set = openfile->mark_set;
|
2015-07-13 05:32:17 +00:00
|
|
|
bool added_magicline = FALSE;
|
|
|
|
/* Whether we added a magicline after filebot. */
|
2005-07-25 02:33:45 +00:00
|
|
|
filestruct *top, *bot;
|
|
|
|
size_t top_x, bot_x;
|
2015-03-27 20:16:36 +00:00
|
|
|
bool right_side_up = FALSE;
|
2005-07-25 02:33:45 +00:00
|
|
|
ssize_t mb_lineno_save = 0;
|
|
|
|
/* We're going to close the current file, and open the output of
|
|
|
|
* the alternate spell command. The line that mark_begin points
|
|
|
|
* to will be freed, so we save the line number and restore it
|
|
|
|
* afterwards. */
|
2015-07-17 21:11:32 +00:00
|
|
|
size_t size_of_surrounding = 0;
|
2015-07-12 19:27:26 +00:00
|
|
|
/* The size of the text outside of a marked region. */
|
2005-07-24 19:57:51 +00:00
|
|
|
#endif
|
|
|
|
|
2015-03-23 17:01:25 +00:00
|
|
|
/* Get the timestamp and the size of the temporary file. */
|
2015-03-17 20:10:59 +00:00
|
|
|
stat(tempfile_name, &spellfileinfo);
|
|
|
|
timestamp = spellfileinfo.st_mtime;
|
|
|
|
|
2015-03-23 17:01:25 +00:00
|
|
|
/* If the number of bytes to check is zero, get out. */
|
|
|
|
if (spellfileinfo.st_size == 0)
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-23 17:12:49 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (old_mark_set) {
|
|
|
|
/* If the mark is on, save the number of the line it starts on,
|
|
|
|
* and then turn the mark off. */
|
|
|
|
mb_lineno_save = openfile->mark_begin->lineno;
|
|
|
|
openfile->mark_set = FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
endwin();
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Set up an argument list to pass to execvp(). */
|
2005-07-25 02:33:45 +00:00
|
|
|
if (spellargs == NULL) {
|
|
|
|
spellargs = (char **)nmalloc(arglen * sizeof(char *));
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
spellargs[0] = strtok(alt_speller, " ");
|
|
|
|
while ((ptr = strtok(NULL, " ")) != NULL) {
|
|
|
|
arglen++;
|
|
|
|
spellargs = (char **)nrealloc(spellargs, arglen *
|
|
|
|
sizeof(char *));
|
|
|
|
spellargs[arglen - 3] = ptr;
|
|
|
|
}
|
|
|
|
spellargs[arglen - 1] = NULL;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-07-25 02:33:45 +00:00
|
|
|
spellargs[arglen - 2] = tempfile_name;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Start a new process for the alternate speller. */
|
|
|
|
if ((pid_spell = fork()) == 0) {
|
2005-11-29 05:39:31 +00:00
|
|
|
/* Start alternate spell program; we are using $PATH. */
|
2005-07-25 02:33:45 +00:00
|
|
|
execvp(spellargs[0], spellargs);
|
|
|
|
|
|
|
|
/* Should not be reached, if alternate speller is found!!! */
|
|
|
|
exit(1);
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* If we couldn't fork, get out. */
|
|
|
|
if (pid_spell < 0)
|
|
|
|
return _("Could not fork");
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-01-04 10:37:11 +00:00
|
|
|
/* Block SIGWINCHes so the spell checker doesn't get any. */
|
|
|
|
allow_sigwinch(FALSE);
|
2005-07-26 00:06:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Wait for the alternate spell checker to finish. */
|
2005-07-25 02:33:45 +00:00
|
|
|
wait(&alt_spell_status);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-08-14 20:08:49 +00:00
|
|
|
/* Reenter curses mode. */
|
|
|
|
doupdate();
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Restore the terminal to its previous state. */
|
|
|
|
terminal_init();
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-04-17 09:24:17 +00:00
|
|
|
if (!WIFEXITED(alt_spell_status) || WEXITSTATUS(alt_spell_status) != 0) {
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
/* Turn the mark back on if it was on before. */
|
|
|
|
openfile->mark_set = old_mark_set;
|
|
|
|
#endif
|
2015-04-17 09:24:17 +00:00
|
|
|
return invocation_error(alt_speller);
|
2005-07-25 02:33:45 +00:00
|
|
|
}
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
if (old_mark_set) {
|
2015-07-18 10:32:01 +00:00
|
|
|
/* Trim the filestruct so that it contains only the marked text. */
|
2005-07-25 02:33:45 +00:00
|
|
|
mark_order((const filestruct **)&top, &top_x,
|
2015-03-27 20:16:36 +00:00
|
|
|
(const filestruct **)&bot, &bot_x, &right_side_up);
|
2005-07-25 02:33:45 +00:00
|
|
|
filepart = partition_filestruct(top, top_x, bot, bot_x);
|
2015-07-18 10:32:01 +00:00
|
|
|
|
|
|
|
/* Foresay whether a magicline will be added when the
|
|
|
|
* spell-checked text is read back in. */
|
2015-07-13 05:32:17 +00:00
|
|
|
if (!ISSET(NO_NEWLINES))
|
|
|
|
added_magicline = (openfile->filebot->data[0] != '\0');
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-07-12 19:27:26 +00:00
|
|
|
/* Compute the size of the text outside of the marked region. */
|
|
|
|
size_of_surrounding = openfile->totsize - get_totsize(top, bot);
|
2005-07-25 02:33:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2016-05-30 07:09:36 +00:00
|
|
|
/* Replace the text of the current buffer with the spell-checked text. */
|
2005-11-08 19:15:58 +00:00
|
|
|
replace_buffer(tempfile_name);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-25 02:33:45 +00:00
|
|
|
if (old_mark_set) {
|
|
|
|
filestruct *top_save = openfile->fileage;
|
2015-03-27 20:16:36 +00:00
|
|
|
/* Adjust the end point of the marked region for any change in
|
2016-05-30 07:09:36 +00:00
|
|
|
* length of the region's last line. */
|
2015-03-27 20:16:36 +00:00
|
|
|
if (right_side_up)
|
|
|
|
current_x_save = strlen(openfile->filebot->data);
|
|
|
|
else
|
|
|
|
openfile->mark_begin_x = strlen(openfile->filebot->data);
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-07-13 17:57:58 +00:00
|
|
|
/* If a magicline was added, remove it again. */
|
|
|
|
if (added_magicline)
|
2015-07-13 05:32:17 +00:00
|
|
|
remove_magicline();
|
|
|
|
|
2005-07-25 02:33:45 +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. */
|
|
|
|
unpartition_filestruct(&filepart);
|
|
|
|
|
2015-07-12 19:27:26 +00:00
|
|
|
/* Renumber, starting with the beginning line of the old partition. */
|
2005-07-25 02:33:45 +00:00
|
|
|
renumber(top_save);
|
2015-07-12 19:27:26 +00:00
|
|
|
|
|
|
|
/* Add back the size of the text surrounding the marked region. */
|
|
|
|
openfile->totsize += size_of_surrounding;
|
2005-07-25 02:33:45 +00:00
|
|
|
|
2015-11-22 16:08:28 +00:00
|
|
|
/* Restore the position of the mark, and turn it back on. */
|
2015-03-27 20:16:36 +00:00
|
|
|
openfile->mark_begin = fsfromline(mb_lineno_save);
|
2005-07-25 02:33:45 +00:00
|
|
|
openfile->mark_set = TRUE;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2015-06-28 14:12:25 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2015-03-17 20:10:59 +00:00
|
|
|
/* Go back to the old position. */
|
2015-03-21 21:02:13 +00:00
|
|
|
goto_line_posx(lineno_save, current_x_save);
|
|
|
|
openfile->current_y = current_y_save;
|
2016-04-07 11:58:51 +00:00
|
|
|
edit_update(STATIONARY);
|
2015-03-17 20:10:59 +00:00
|
|
|
|
|
|
|
/* Stat the temporary file again, and mark the buffer as modified only
|
|
|
|
* if this file was changed since it was written. */
|
|
|
|
stat(tempfile_name, &spellfileinfo);
|
|
|
|
if (spellfileinfo.st_mtime != timestamp)
|
|
|
|
set_modified();
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-01-04 10:37:11 +00:00
|
|
|
/* Unblock SIGWINCHes again. */
|
|
|
|
allow_sigwinch(TRUE);
|
2005-07-26 01:17:16 +00:00
|
|
|
#endif
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
return NULL;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Spell check the current file. If an alternate spell checker is
|
|
|
|
* specified, use it. Otherwise, use the internal spell checker. */
|
2005-07-25 02:33:45 +00:00
|
|
|
void do_spell(void)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2007-01-11 22:46:22 +00:00
|
|
|
bool status;
|
2005-07-25 02:33:45 +00:00
|
|
|
FILE *temp_file;
|
2016-01-02 16:01:04 +00:00
|
|
|
char *temp;
|
2005-07-25 02:33:45 +00:00
|
|
|
const char *spell_msg;
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2010-11-12 06:22:12 +00:00
|
|
|
if (ISSET(RESTRICTED)) {
|
2015-07-30 18:10:16 +00:00
|
|
|
show_restricted_warning();
|
2010-11-12 06:22:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-02 16:01:04 +00:00
|
|
|
temp = safe_tempfile(&temp_file);
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
if (temp == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Error writing temp file: %s"), strerror(errno));
|
2005-07-25 02:33:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-05-08 11:03:47 +00:00
|
|
|
if (openfile->mark_set)
|
|
|
|
status = write_marked_file(temp, temp_file, TRUE, OVERWRITE);
|
|
|
|
else
|
2005-07-25 02:33:45 +00:00
|
|
|
#endif
|
2016-05-08 11:03:47 +00:00
|
|
|
status = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
|
2005-07-25 02:33:45 +00:00
|
|
|
|
2007-01-11 22:46:22 +00:00
|
|
|
if (!status) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Error writing temp file: %s"), strerror(errno));
|
2005-07-25 02:33:45 +00:00
|
|
|
free(temp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-12 11:50:58 +00:00
|
|
|
blank_bottombars();
|
|
|
|
statusbar(_("Invoking spell checker, please wait"));
|
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) :
|
2016-05-08 11:03:47 +00:00
|
|
|
do_int_speller(temp);
|
2005-07-25 02:33:45 +00:00
|
|
|
unlink(temp);
|
|
|
|
free(temp);
|
|
|
|
|
2008-03-05 07:34:01 +00:00
|
|
|
currmenu = MMAIN;
|
2006-06-09 17:09:51 +00:00
|
|
|
|
2005-07-25 02:33:45 +00:00
|
|
|
/* If the spell-checker printed any error messages onscreen, make
|
|
|
|
* sure that they're cleared off. */
|
2006-06-09 17:09:51 +00:00
|
|
|
total_refresh();
|
2005-07-25 02:33:45 +00:00
|
|
|
|
|
|
|
if (spell_msg != NULL) {
|
|
|
|
if (errno == 0)
|
|
|
|
/* Don't display an error message of "Success". */
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Spell checking failed: %s"), spell_msg);
|
2005-07-25 02:33:45 +00:00
|
|
|
else
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Spell checking failed: %s: %s"), spell_msg,
|
2016-05-08 11:03:47 +00:00
|
|
|
strerror(errno));
|
2005-07-25 02:33:45 +00:00
|
|
|
} else
|
|
|
|
statusbar(_("Finished checking spelling"));
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-07-25 02:33:45 +00:00
|
|
|
#endif /* !DISABLE_SPELLER */
|
2005-07-24 19:57:51 +00:00
|
|
|
|
2014-04-04 11:59:03 +00:00
|
|
|
#ifndef DISABLE_COLOR
|
2016-01-04 10:05:52 +00:00
|
|
|
/* Run a linting program on the current buffer. Return NULL for normal
|
2014-02-24 10:18:15 +00:00
|
|
|
* termination, and the error string otherwise. */
|
|
|
|
void do_linter(void)
|
|
|
|
{
|
|
|
|
char *read_buff, *read_buff_ptr, *read_buff_word, *ptr;
|
|
|
|
size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread;
|
|
|
|
size_t parsesuccess = 0;
|
2016-02-11 17:25:37 +00:00
|
|
|
int lint_status, lint_fd[2];
|
2014-02-24 10:18:15 +00:00
|
|
|
pid_t pid_lint;
|
|
|
|
static int arglen = 3;
|
|
|
|
static char **lintargs = NULL;
|
2016-02-11 17:25:37 +00:00
|
|
|
char *lintcopy, *convendptr = NULL;
|
2014-02-24 10:18:15 +00:00
|
|
|
lintstruct *lints = NULL, *tmplint = NULL, *curlint = NULL;
|
|
|
|
|
2015-07-29 17:36:39 +00:00
|
|
|
if (ISSET(RESTRICTED)) {
|
2015-07-30 18:10:16 +00:00
|
|
|
show_restricted_warning();
|
|
|
|
return;
|
2015-07-29 17:36:39 +00:00
|
|
|
}
|
|
|
|
|
2014-02-24 10:18:15 +00:00
|
|
|
if (!openfile->syntax || !openfile->syntax->linter) {
|
2014-05-13 08:12:52 +00:00
|
|
|
statusbar(_("No linter defined for this type of file!"));
|
2014-02-24 10:18:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (openfile->modified) {
|
2014-07-16 08:53:16 +00:00
|
|
|
int i = do_yesno_prompt(FALSE, _("Save modified buffer before linting?"));
|
2016-02-11 17:25:37 +00:00
|
|
|
|
2014-05-11 03:09:00 +00:00
|
|
|
if (i == -1) {
|
|
|
|
statusbar(_("Cancelled"));
|
2016-01-04 10:05:52 +00:00
|
|
|
goto exit_from_lint;
|
2016-02-11 17:25:37 +00:00
|
|
|
} else if (i == 1 && (do_writeout(FALSE) != TRUE))
|
|
|
|
goto exit_from_lint;
|
2014-02-24 10:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lintcopy = mallocstrcpy(NULL, openfile->syntax->linter);
|
2016-02-11 17:25:37 +00:00
|
|
|
/* Create a pipe up front. */
|
2014-02-24 10:18:15 +00:00
|
|
|
if (pipe(lint_fd) == -1) {
|
|
|
|
statusbar(_("Could not create pipe"));
|
2016-01-04 10:05:52 +00:00
|
|
|
goto exit_from_lint;
|
2014-02-24 10:18:15 +00:00
|
|
|
}
|
|
|
|
|
2014-05-11 03:09:00 +00:00
|
|
|
blank_bottombars();
|
2014-02-24 10:18:15 +00:00
|
|
|
statusbar(_("Invoking linter, please wait"));
|
|
|
|
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Set up an argument list to pass to execvp(). */
|
2014-02-24 10:18:15 +00:00
|
|
|
if (lintargs == NULL) {
|
|
|
|
lintargs = (char **)nmalloc(arglen * sizeof(char *));
|
|
|
|
|
|
|
|
lintargs[0] = strtok(lintcopy, " ");
|
|
|
|
while ((ptr = strtok(NULL, " ")) != NULL) {
|
|
|
|
arglen++;
|
2016-02-11 17:25:37 +00:00
|
|
|
lintargs = (char **)nrealloc(lintargs, arglen * sizeof(char *));
|
2014-02-24 10:18:15 +00:00
|
|
|
lintargs[arglen - 3] = ptr;
|
|
|
|
}
|
|
|
|
lintargs[arglen - 1] = NULL;
|
|
|
|
}
|
|
|
|
lintargs[arglen - 2] = openfile->filename;
|
|
|
|
|
2016-02-11 17:25:37 +00:00
|
|
|
/* Start a new process to run the linter in. */
|
2014-02-24 10:18:15 +00:00
|
|
|
if ((pid_lint = fork()) == 0) {
|
|
|
|
|
2016-02-11 17:25:37 +00:00
|
|
|
/* Child continues here (i.e. the future linting process). */
|
2014-02-24 10:18:15 +00:00
|
|
|
close(lint_fd[0]);
|
|
|
|
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Send the linter's standard output + err to the pipe. */
|
2014-07-16 08:53:16 +00:00
|
|
|
if (dup2(lint_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
2015-04-17 09:24:17 +00:00
|
|
|
exit(9);
|
2014-07-16 08:53:16 +00:00
|
|
|
if (dup2(lint_fd[1], STDERR_FILENO) != STDERR_FILENO)
|
2015-04-17 09:24:17 +00:00
|
|
|
exit(9);
|
2014-02-24 10:18:15 +00:00
|
|
|
|
2014-07-16 08:53:16 +00:00
|
|
|
close(lint_fd[1]);
|
2014-02-24 10:18:15 +00:00
|
|
|
|
|
|
|
/* Start the linter program; we are using $PATH. */
|
|
|
|
execvp(lintargs[0], lintargs);
|
|
|
|
|
2015-04-17 09:24:17 +00:00
|
|
|
/* This is only reached when the linter is not found. */
|
|
|
|
exit(9);
|
2014-02-24 10:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent continues here. */
|
|
|
|
close(lint_fd[1]);
|
|
|
|
|
2016-02-11 17:25:37 +00:00
|
|
|
/* If the child process was not forked successfully... */
|
2014-02-24 10:18:15 +00:00
|
|
|
if (pid_lint < 0) {
|
|
|
|
close(lint_fd[0]);
|
|
|
|
statusbar(_("Could not fork"));
|
2016-01-04 10:05:52 +00:00
|
|
|
goto exit_from_lint;
|
2014-02-24 10:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the system pipe buffer size. */
|
|
|
|
if ((pipe_buff_size = fpathconf(lint_fd[0], _PC_PIPE_BUF)) < 1) {
|
|
|
|
close(lint_fd[0]);
|
2014-03-17 21:36:37 +00:00
|
|
|
statusbar(_("Could not get size of pipe buffer"));
|
2016-01-04 10:05:52 +00:00
|
|
|
goto exit_from_lint;
|
2014-02-24 10:18:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Read in the returned syntax errors. */
|
2014-02-24 10:18:15 +00:00
|
|
|
read_buff_read = 0;
|
|
|
|
read_buff_size = pipe_buff_size + 1;
|
|
|
|
read_buff = read_buff_ptr = charalloc(read_buff_size);
|
|
|
|
|
2016-03-30 12:30:14 +00:00
|
|
|
while ((bytesread = read(lint_fd[0], read_buff_ptr, pipe_buff_size)) > 0) {
|
2014-02-24 10:18:15 +00:00
|
|
|
#ifdef DEBUG
|
2014-06-09 15:08:59 +00:00
|
|
|
fprintf(stderr, "text.c:do_linter:%ld bytes (%s)\n", (long)bytesread, read_buff_ptr);
|
2014-02-24 10:18:15 +00:00
|
|
|
#endif
|
|
|
|
read_buff_read += bytesread;
|
|
|
|
read_buff_size += pipe_buff_size;
|
2016-02-11 17:25:37 +00:00
|
|
|
read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size);
|
2014-02-24 10:18:15 +00:00
|
|
|
read_buff_ptr += read_buff_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
*read_buff_ptr = '\0';
|
|
|
|
close(lint_fd[0]);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "text.c:do_lint:Raw output: %s\n", read_buff);
|
|
|
|
#endif
|
|
|
|
|
2016-02-11 17:25:37 +00:00
|
|
|
/* Process the linter output. */
|
2014-02-24 10:18:15 +00:00
|
|
|
read_buff_word = read_buff_ptr = read_buff;
|
|
|
|
|
|
|
|
while (*read_buff_ptr != '\0') {
|
|
|
|
if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) {
|
|
|
|
*read_buff_ptr = '\0';
|
|
|
|
if (read_buff_word != read_buff_ptr) {
|
|
|
|
char *filename = NULL, *linestr = NULL, *maybecol = NULL;
|
|
|
|
char *message = mallocstrcpy(NULL, read_buff_word);
|
|
|
|
|
|
|
|
/* At the moment we're assuming the following formats:
|
2014-04-08 18:38:45 +00:00
|
|
|
*
|
|
|
|
* filenameorcategory:line:column:message (e.g. splint)
|
|
|
|
* filenameorcategory:line:message (e.g. pyflakes)
|
|
|
|
* filenameorcategory:line,col:message (e.g. pylint)
|
|
|
|
*
|
|
|
|
* This could be turned into some scanf() based parser,
|
|
|
|
* but ugh. */
|
2014-02-24 10:18:15 +00:00
|
|
|
if ((filename = strtok(read_buff_word, ":")) != NULL) {
|
|
|
|
if ((linestr = strtok(NULL, ":")) != NULL) {
|
|
|
|
if ((maybecol = strtok(NULL, ":")) != NULL) {
|
|
|
|
ssize_t tmplineno = 0, tmpcolno = 0;
|
|
|
|
char *tmplinecol;
|
|
|
|
|
|
|
|
tmplineno = strtol(linestr, NULL, 10);
|
|
|
|
if (tmplineno <= 0) {
|
|
|
|
read_buff_ptr++;
|
|
|
|
free(message);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:04 +00:00
|
|
|
tmpcolno = strtol(maybecol, &convendptr, 10);
|
2014-02-24 10:18:15 +00:00
|
|
|
if (*convendptr != '\0') {
|
2014-04-08 18:38:45 +00:00
|
|
|
/* Previous field might still be
|
|
|
|
* line,col format. */
|
2014-02-24 10:18:15 +00:00
|
|
|
strtok(linestr, ",");
|
|
|
|
if ((tmplinecol = strtok(NULL, ",")) != NULL)
|
|
|
|
tmpcolno = strtol(tmplinecol, NULL, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-06-09 15:08:59 +00:00
|
|
|
fprintf(stderr, "text.c:do_lint:Successful parse! %ld:%ld:%s\n", (long)tmplineno, (long)tmpcolno, message);
|
2014-02-24 10:18:15 +00:00
|
|
|
#endif
|
2014-04-08 18:38:45 +00:00
|
|
|
/* Nice. We have a lint message we can use. */
|
2014-02-24 10:18:15 +00:00
|
|
|
parsesuccess++;
|
|
|
|
tmplint = curlint;
|
|
|
|
curlint = nmalloc(sizeof(lintstruct));
|
|
|
|
curlint->next = NULL;
|
|
|
|
curlint->prev = tmplint;
|
|
|
|
if (curlint->prev != NULL)
|
|
|
|
curlint->prev->next = curlint;
|
|
|
|
curlint->msg = mallocstrcpy(NULL, message);
|
|
|
|
curlint->lineno = tmplineno;
|
|
|
|
curlint->colno = tmpcolno;
|
|
|
|
curlint->filename = mallocstrcpy(NULL, filename);
|
|
|
|
|
|
|
|
if (lints == NULL)
|
|
|
|
lints = curlint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
free(message);
|
|
|
|
}
|
|
|
|
read_buff_word = read_buff_ptr + 1;
|
|
|
|
}
|
|
|
|
read_buff_ptr++;
|
|
|
|
}
|
|
|
|
|
2015-04-17 09:24:17 +00:00
|
|
|
/* Process the end of the linting process. */
|
2014-02-24 10:18:15 +00:00
|
|
|
waitpid(pid_lint, &lint_status, 0);
|
|
|
|
|
2015-04-17 09:24:17 +00:00
|
|
|
if (!WIFEXITED(lint_status) || WEXITSTATUS(lint_status) > 2) {
|
|
|
|
statusbar(invocation_error(openfile->syntax->linter));
|
2016-01-04 10:05:52 +00:00
|
|
|
goto exit_from_lint;
|
2015-04-17 09:24:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-24 10:18:15 +00:00
|
|
|
free(read_buff);
|
|
|
|
|
|
|
|
if (parsesuccess == 0) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Got 0 parsable lines from command: %s"),
|
|
|
|
openfile->syntax->linter);
|
2016-01-04 10:05:52 +00:00
|
|
|
goto exit_from_lint;
|
2014-02-24 10:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bottombars(MLINTER);
|
|
|
|
tmplint = NULL;
|
|
|
|
curlint = lints;
|
2016-02-11 17:25:37 +00:00
|
|
|
|
2014-07-27 20:16:28 +00:00
|
|
|
while (TRUE) {
|
2014-02-24 10:18:15 +00:00
|
|
|
int kbinput;
|
2014-07-27 20:16:28 +00:00
|
|
|
functionptrtype func;
|
2014-02-24 10:18:15 +00:00
|
|
|
|
|
|
|
if (tmplint != curlint) {
|
2014-04-03 21:06:30 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-02-24 10:18:15 +00:00
|
|
|
struct stat lintfileinfo;
|
|
|
|
|
2016-02-11 17:25:37 +00:00
|
|
|
new_lint_loop:
|
2014-02-24 10:18:15 +00:00
|
|
|
if (stat(curlint->filename, &lintfileinfo) != -1) {
|
|
|
|
if (openfile->current_stat->st_ino != lintfileinfo.st_ino) {
|
|
|
|
openfilestruct *tmpof = openfile;
|
|
|
|
while (tmpof != openfile->next) {
|
|
|
|
if (tmpof->current_stat->st_ino == lintfileinfo.st_ino)
|
|
|
|
break;
|
|
|
|
tmpof = tmpof->next;
|
|
|
|
}
|
|
|
|
if (tmpof->current_stat->st_ino != lintfileinfo.st_ino) {
|
|
|
|
char *msg = charalloc(1024 + strlen(curlint->filename));
|
|
|
|
int i;
|
|
|
|
|
2016-02-13 19:41:12 +00:00
|
|
|
sprintf(msg, _("This message is for unopened file %s,"
|
|
|
|
" open it in a new buffer?"),
|
2014-02-24 10:18:15 +00:00
|
|
|
curlint->filename);
|
2016-02-13 19:41:12 +00:00
|
|
|
/* Show a cursor after the question. */
|
|
|
|
curs_set(1);
|
2014-02-24 10:18:15 +00:00
|
|
|
i = do_yesno_prompt(FALSE, msg);
|
|
|
|
free(msg);
|
2014-05-11 03:09:00 +00:00
|
|
|
if (i == -1) {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
goto free_lints_and_return;
|
|
|
|
} else if (i == 1) {
|
2014-02-24 10:18:15 +00:00
|
|
|
SET(MULTIBUFFER);
|
|
|
|
open_buffer(curlint->filename, FALSE);
|
|
|
|
} else {
|
|
|
|
char *dontwantfile = curlint->filename;
|
|
|
|
|
|
|
|
while (curlint != NULL && !strcmp(curlint->filename, dontwantfile))
|
|
|
|
curlint = curlint->next;
|
|
|
|
if (curlint == NULL) {
|
2016-01-04 11:10:07 +00:00
|
|
|
statusbar(_("No more errors in unopened files, cancelling"));
|
2014-02-24 10:18:15 +00:00
|
|
|
break;
|
|
|
|
} else
|
|
|
|
goto new_lint_loop;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
openfile = tmpof;
|
|
|
|
}
|
|
|
|
}
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2016-02-22 19:49:45 +00:00
|
|
|
do_gotolinecolumn(curlint->lineno, curlint->colno, FALSE, FALSE);
|
2014-02-24 10:18:15 +00:00
|
|
|
titlebar(NULL);
|
|
|
|
edit_refresh();
|
|
|
|
statusbar(curlint->msg);
|
|
|
|
bottombars(MLINTER);
|
|
|
|
}
|
|
|
|
|
2016-02-13 19:41:12 +00:00
|
|
|
/* Show the cursor to indicate the affected line. */
|
2016-02-22 18:34:04 +00:00
|
|
|
reset_cursor();
|
2016-02-13 19:41:12 +00:00
|
|
|
curs_set(1);
|
|
|
|
|
2014-06-30 18:04:33 +00:00
|
|
|
kbinput = get_kbinput(bottomwin);
|
2015-05-28 13:02:29 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (kbinput == KEY_WINCH)
|
|
|
|
continue;
|
|
|
|
#endif
|
2014-07-27 20:16:28 +00:00
|
|
|
func = func_from_key(&kbinput);
|
2014-02-24 10:18:15 +00:00
|
|
|
tmplint = curlint;
|
|
|
|
|
2014-07-27 20:16:28 +00:00
|
|
|
if (func == do_cancel)
|
2014-02-24 10:18:15 +00:00
|
|
|
break;
|
2014-07-27 20:16:28 +00:00
|
|
|
else if (func == do_help_void) {
|
2014-02-24 10:18:15 +00:00
|
|
|
tmplint = NULL;
|
|
|
|
do_help_void();
|
2014-07-27 20:16:28 +00:00
|
|
|
} else if (func == do_page_down) {
|
2014-02-24 10:18:15 +00:00
|
|
|
if (curlint->next != NULL)
|
2014-07-16 08:53:16 +00:00
|
|
|
curlint = curlint->next;
|
2014-07-27 20:16:28 +00:00
|
|
|
else
|
2014-07-16 08:53:16 +00:00
|
|
|
statusbar(_("At last message"));
|
2014-07-27 20:16:28 +00:00
|
|
|
} else if (func == do_page_up) {
|
2014-02-24 10:18:15 +00:00
|
|
|
if (curlint->prev != NULL)
|
|
|
|
curlint = curlint->prev;
|
2014-07-27 20:16:28 +00:00
|
|
|
else
|
2014-07-16 08:53:16 +00:00
|
|
|
statusbar(_("At first message"));
|
2014-02-24 10:18:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 17:25:37 +00:00
|
|
|
|
2014-05-11 03:09:00 +00:00
|
|
|
blank_statusbar();
|
2016-02-11 17:25:37 +00:00
|
|
|
|
2014-05-26 07:53:20 +00:00
|
|
|
#ifndef NANO_TINY
|
2015-06-28 14:12:25 +00:00
|
|
|
free_lints_and_return:
|
2014-05-26 07:53:20 +00:00
|
|
|
#endif
|
2016-02-11 08:50:11 +00:00
|
|
|
for (curlint = lints; curlint != NULL;) {
|
|
|
|
tmplint = curlint;
|
|
|
|
curlint = curlint->next;
|
2014-02-24 10:18:15 +00:00
|
|
|
free(tmplint->msg);
|
|
|
|
free(tmplint->filename);
|
|
|
|
free(tmplint);
|
|
|
|
}
|
2016-02-11 17:25:37 +00:00
|
|
|
|
2016-01-04 10:05:52 +00:00
|
|
|
exit_from_lint:
|
|
|
|
display_main_list();
|
2014-02-24 10:18:15 +00:00
|
|
|
}
|
2015-01-03 07:24:17 +00:00
|
|
|
|
2015-03-27 16:55:49 +00:00
|
|
|
#ifndef DISABLE_SPELLER
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Run a formatter for the current syntax. This expects the formatter
|
|
|
|
* to be non-interactive and operate on a file in-place, which we'll
|
|
|
|
* pass it on the command line. */
|
2015-01-03 07:24:17 +00:00
|
|
|
void do_formatter(void)
|
|
|
|
{
|
|
|
|
bool status;
|
|
|
|
FILE *temp_file;
|
|
|
|
int format_status;
|
2016-02-11 17:25:37 +00:00
|
|
|
ssize_t lineno_save = openfile->current->lineno;
|
|
|
|
ssize_t current_y_save = openfile->current_y;
|
2015-01-03 07:24:17 +00:00
|
|
|
size_t current_x_save = openfile->current_x;
|
|
|
|
size_t pww_save = openfile->placewewant;
|
|
|
|
pid_t pid_format;
|
|
|
|
static int arglen = 3;
|
|
|
|
static char **formatargs = NULL;
|
2016-02-11 17:25:37 +00:00
|
|
|
char *temp, *ptr, *finalstatus = NULL;
|
2015-01-03 07:24:17 +00:00
|
|
|
|
2016-01-02 16:01:04 +00:00
|
|
|
if (openfile->totsize == 0) {
|
|
|
|
statusbar(_("Finished"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = safe_tempfile(&temp_file);
|
|
|
|
|
2015-01-03 07:24:17 +00:00
|
|
|
if (temp == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Error writing temp file: %s"), strerror(errno));
|
2015-01-03 07:24:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-14 20:17:21 +00:00
|
|
|
/* We're not supporting partial formatting, oi vey. */
|
2015-01-03 07:24:17 +00:00
|
|
|
openfile->mark_set = FALSE;
|
|
|
|
status = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE);
|
|
|
|
|
|
|
|
if (!status) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Error writing temp file: %s"), strerror(errno));
|
2015-01-03 07:24:17 +00:00
|
|
|
free(temp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
blank_bottombars();
|
|
|
|
statusbar(_("Invoking formatter, please wait"));
|
|
|
|
|
2015-04-08 19:57:31 +00:00
|
|
|
/* Set up an argument list to pass to execvp(). */
|
2015-01-03 07:24:17 +00:00
|
|
|
if (formatargs == NULL) {
|
|
|
|
formatargs = (char **)nmalloc(arglen * sizeof(char *));
|
|
|
|
|
|
|
|
formatargs[0] = strtok(openfile->syntax->formatter, " ");
|
|
|
|
while ((ptr = strtok(NULL, " ")) != NULL) {
|
|
|
|
arglen++;
|
2016-02-11 17:25:37 +00:00
|
|
|
formatargs = (char **)nrealloc(formatargs, arglen * sizeof(char *));
|
2015-01-03 07:24:17 +00:00
|
|
|
formatargs[arglen - 3] = ptr;
|
|
|
|
}
|
|
|
|
formatargs[arglen - 1] = NULL;
|
|
|
|
}
|
|
|
|
formatargs[arglen - 2] = temp;
|
|
|
|
|
|
|
|
/* Start a new process for the formatter. */
|
|
|
|
if ((pid_format = fork()) == 0) {
|
2015-03-14 20:17:21 +00:00
|
|
|
/* Start the formatting program; we are using $PATH. */
|
2015-01-03 07:24:17 +00:00
|
|
|
execvp(formatargs[0], formatargs);
|
|
|
|
|
2015-03-14 20:17:21 +00:00
|
|
|
/* Should not be reached, if the formatter is found! */
|
2015-01-03 07:24:17 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we couldn't fork, get out. */
|
|
|
|
if (pid_format < 0) {
|
|
|
|
statusbar(_("Could not fork"));
|
2016-01-02 16:01:04 +00:00
|
|
|
unlink(temp);
|
|
|
|
free(temp);
|
2015-01-03 07:24:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2016-01-04 10:37:11 +00:00
|
|
|
/* Block SIGWINCHes so the formatter doesn't get any. */
|
|
|
|
allow_sigwinch(FALSE);
|
2015-01-03 07:24:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Wait for the formatter to finish. */
|
|
|
|
wait(&format_status);
|
|
|
|
|
2015-04-17 09:24:17 +00:00
|
|
|
if (!WIFEXITED(format_status) || WEXITSTATUS(format_status) != 0)
|
|
|
|
finalstatus = invocation_error(openfile->syntax->formatter);
|
|
|
|
else {
|
2015-03-14 20:17:21 +00:00
|
|
|
/* Replace the text of the current buffer with the formatted text. */
|
2015-01-03 07:24:17 +00:00
|
|
|
replace_buffer(temp);
|
|
|
|
|
2015-12-31 16:44:32 +00:00
|
|
|
/* Restore the cursor position, and mark the file as modified. */
|
|
|
|
goto_line_posx(lineno_save, current_x_save);
|
|
|
|
openfile->current_y = current_y_save;
|
|
|
|
openfile->placewewant = pww_save;
|
2016-04-07 11:58:51 +00:00
|
|
|
edit_update(STATIONARY);
|
2015-12-31 16:44:32 +00:00
|
|
|
|
2015-01-03 07:24:17 +00:00
|
|
|
set_modified();
|
|
|
|
|
|
|
|
finalstatus = _("Finished formatting");
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink(temp);
|
|
|
|
free(temp);
|
|
|
|
|
2015-07-28 19:39:34 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-01-04 10:37:11 +00:00
|
|
|
/* Unblock SIGWINCHes again. */
|
|
|
|
allow_sigwinch(TRUE);
|
2015-07-28 19:39:34 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-11 16:57:52 +00:00
|
|
|
statusbar(finalstatus);
|
|
|
|
|
|
|
|
/* If there were error messages, allow the user some time to read them. */
|
|
|
|
if (WIFEXITED(format_status) && WEXITSTATUS(format_status) == 2)
|
|
|
|
sleep(4);
|
|
|
|
|
2016-02-11 17:25:37 +00:00
|
|
|
/* If there were any messages, clear them off. */
|
2015-01-03 07:24:17 +00:00
|
|
|
total_refresh();
|
|
|
|
}
|
2015-03-27 16:55:49 +00:00
|
|
|
#endif /* !DISABLE_SPELLER */
|
2014-04-04 11:59:03 +00:00
|
|
|
#endif /* !DISABLE_COLOR */
|
2014-02-24 10:18:15 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-08-10 22:51:49 +00:00
|
|
|
/* Our own version of "wc". Note that its character counts are in
|
|
|
|
* multibyte characters instead of single-byte characters. */
|
2005-07-25 04:21:46 +00:00
|
|
|
void do_wordlinechar_count(void)
|
2005-07-24 19:57:51 +00:00
|
|
|
{
|
2005-07-25 21:23:11 +00:00
|
|
|
size_t words = 0, chars = 0;
|
2008-05-31 23:09:40 +00:00
|
|
|
ssize_t nlines = 0;
|
2005-07-25 03:47:08 +00:00
|
|
|
size_t current_x_save = openfile->current_x;
|
2005-07-24 19:57:51 +00:00
|
|
|
size_t pww_save = openfile->placewewant;
|
|
|
|
filestruct *current_save = openfile->current;
|
|
|
|
bool old_mark_set = openfile->mark_set;
|
|
|
|
filestruct *top, *bot;
|
|
|
|
size_t top_x, bot_x;
|
|
|
|
|
2015-07-18 10:32:01 +00:00
|
|
|
/* If the mark is on, partition the filestruct so that it
|
|
|
|
* contains only the marked text, and turn the mark off. */
|
2005-07-24 19:57:51 +00:00
|
|
|
if (old_mark_set) {
|
|
|
|
mark_order((const filestruct **)&top, &top_x,
|
|
|
|
(const filestruct **)&bot, &bot_x, NULL);
|
|
|
|
filepart = partition_filestruct(top, top_x, bot, bot_x);
|
|
|
|
openfile->mark_set = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start at the top of the file. */
|
|
|
|
openfile->current = openfile->fileage;
|
|
|
|
openfile->current_x = 0;
|
|
|
|
openfile->placewewant = 0;
|
|
|
|
|
|
|
|
/* Keep moving to the next word (counting punctuation characters as
|
2005-07-25 03:47:08 +00:00
|
|
|
* part of a word, as "wc -w" does), without updating the screen,
|
|
|
|
* until we reach the end of the file, incrementing the total word
|
|
|
|
* count whenever we're on a word just before moving. */
|
2005-07-24 19:57:51 +00:00
|
|
|
while (openfile->current != openfile->filebot ||
|
2005-11-03 21:08:39 +00:00
|
|
|
openfile->current->data[openfile->current_x] != '\0') {
|
2005-07-24 19:57:51 +00:00
|
|
|
if (do_next_word(TRUE, FALSE))
|
|
|
|
words++;
|
|
|
|
}
|
|
|
|
|
2005-07-25 03:47:08 +00:00
|
|
|
/* Get the total line and character counts, as "wc -l" and "wc -c"
|
|
|
|
* do, but get the latter in multibyte characters. */
|
2005-07-24 19:57:51 +00:00
|
|
|
if (old_mark_set) {
|
2015-07-18 10:32:01 +00:00
|
|
|
nlines = openfile->filebot->lineno - openfile->fileage->lineno + 1;
|
2005-07-25 03:47:08 +00:00
|
|
|
chars = get_totsize(openfile->fileage, openfile->filebot);
|
|
|
|
|
2005-07-24 19:57:51 +00:00
|
|
|
/* Unpartition the filestruct so that it contains all the text
|
|
|
|
* again, and turn the mark back on. */
|
|
|
|
unpartition_filestruct(&filepart);
|
|
|
|
openfile->mark_set = TRUE;
|
2005-07-25 03:47:08 +00:00
|
|
|
} else {
|
2008-05-31 23:09:40 +00:00
|
|
|
nlines = openfile->filebot->lineno;
|
2005-07-25 03:47:08 +00:00
|
|
|
chars = openfile->totsize;
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore where we were. */
|
|
|
|
openfile->current = current_save;
|
|
|
|
openfile->current_x = current_x_save;
|
|
|
|
openfile->placewewant = pww_save;
|
|
|
|
|
2015-07-18 10:32:01 +00:00
|
|
|
/* Display the total word, line, and character counts on the statusbar. */
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ?
|
|
|
|
_("In Selection: ") : "", (unsigned long)words, (long)nlines,
|
|
|
|
(unsigned long)chars);
|
2005-07-24 19:57:51 +00:00
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2005-11-07 06:06:05 +00:00
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Get verbatim input. */
|
2005-11-07 06:06:05 +00:00
|
|
|
void do_verbatim_input(void)
|
|
|
|
{
|
|
|
|
int *kbinput;
|
|
|
|
size_t kbinput_len, i;
|
|
|
|
char *output;
|
|
|
|
|
2006-05-27 16:02:48 +00:00
|
|
|
/* TRANSLATORS: This is displayed when the next keystroke will be
|
|
|
|
* inserted verbatim. */
|
2005-11-07 06:06:05 +00:00
|
|
|
statusbar(_("Verbatim Input"));
|
2016-03-29 14:09:17 +00:00
|
|
|
reset_cursor();
|
|
|
|
curs_set(1);
|
2005-11-07 06:06:05 +00:00
|
|
|
|
|
|
|
/* Read in all the verbatim characters. */
|
|
|
|
kbinput = get_verbatim_kbinput(edit, &kbinput_len);
|
|
|
|
|
2006-05-27 18:19:03 +00:00
|
|
|
/* If constant cursor position display is on, make sure the current
|
|
|
|
* cursor position will be properly displayed on the statusbar.
|
|
|
|
* Otherwise, blank the statusbar. */
|
|
|
|
if (ISSET(CONST_UPDATE))
|
|
|
|
do_cursorpos(TRUE);
|
|
|
|
else {
|
|
|
|
blank_statusbar();
|
|
|
|
wnoutrefresh(bottomwin);
|
|
|
|
}
|
2006-05-27 17:39:19 +00:00
|
|
|
|
2005-11-07 06:06:05 +00:00
|
|
|
/* Display all the verbatim characters at once, not filtering out
|
|
|
|
* control characters. */
|
|
|
|
output = charalloc(kbinput_len + 1);
|
|
|
|
|
|
|
|
for (i = 0; i < kbinput_len; i++)
|
|
|
|
output[i] = (char)kbinput[i];
|
|
|
|
output[i] = '\0';
|
|
|
|
|
2006-12-02 17:22:21 +00:00
|
|
|
free(kbinput);
|
|
|
|
|
2005-11-07 06:06:05 +00:00
|
|
|
do_output(output, kbinput_len, TRUE);
|
|
|
|
|
|
|
|
free(output);
|
|
|
|
}
|