2000-06-06 05:53:49 +00:00
|
|
|
/**************************************************************************
|
2016-08-29 15:10:49 +00:00
|
|
|
* cut.c -- This file is part of GNU nano. *
|
2000-06-06 05:53:49 +00:00
|
|
|
* *
|
2020-01-15 10:42:38 +00:00
|
|
|
* Copyright (C) 1999-2011, 2013-2020 Free Software Foundation, Inc. *
|
2016-08-29 13:14:18 +00:00
|
|
|
* Copyright (C) 2014 Mark Majeres *
|
2019-03-10 15:29:57 +00:00
|
|
|
* Copyright (C) 2016, 2018, 2019 Benno Schulenberg *
|
2016-08-29 13:14:18 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
2000-06-06 05:53:49 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano 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. *
|
2000-06-06 05:53:49 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2016-08-29 15:10:49 +00:00
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
2000-06-06 05:53:49 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2001-04-28 18:03:52 +00:00
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-01-01 16:29:08 +00:00
|
|
|
/* Delete the character under the cursor. */
|
|
|
|
void do_deletion(undo_type action)
|
|
|
|
{
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
size_t old_amount = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
|
|
|
|
/* When in the middle of a line, delete the current character. */
|
|
|
|
if (openfile->current->data[openfile->current_x] != '\0') {
|
2019-06-09 18:02:58 +00:00
|
|
|
int charlen = char_length(openfile->current->data + openfile->current_x);
|
|
|
|
size_t line_len = strlen(openfile->current->data + openfile->current_x);
|
2019-01-01 16:29:08 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* If the type of action changed or the cursor moved to a different
|
|
|
|
* line, create a new undo item, otherwise update the existing item. */
|
|
|
|
if (action != openfile->last_action ||
|
2020-02-25 15:47:50 +00:00
|
|
|
openfile->current->lineno != openfile->current_undo->head_lineno)
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(action, NULL);
|
2019-01-01 16:29:08 +00:00
|
|
|
else
|
|
|
|
update_undo(action);
|
|
|
|
|
|
|
|
if (ISSET(SOFTWRAP))
|
|
|
|
old_amount = number_of_chunks_in(openfile->current);
|
|
|
|
#endif
|
|
|
|
/* Move the remainder of the line "in", over the current character. */
|
2019-09-18 13:20:08 +00:00
|
|
|
memmove(&openfile->current->data[openfile->current_x],
|
2019-06-09 15:07:02 +00:00
|
|
|
&openfile->current->data[openfile->current_x + charlen],
|
|
|
|
line_len - charlen + 1);
|
2019-01-01 16:29:08 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* Adjust the mark if it is after the cursor on the current line. */
|
|
|
|
if (openfile->mark == openfile->current &&
|
|
|
|
openfile->mark_x > openfile->current_x)
|
2019-06-09 15:07:02 +00:00
|
|
|
openfile->mark_x -= charlen;
|
2019-01-01 16:29:08 +00:00
|
|
|
#endif
|
|
|
|
/* Otherwise, when not at end of buffer, join this line with the next. */
|
|
|
|
} else if (openfile->current != openfile->filebot) {
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *joining = openfile->current->next;
|
2019-01-01 16:29:08 +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 &&
|
2019-04-07 06:47:29 +00:00
|
|
|
!ISSET(NO_NEWLINES)) {
|
2019-01-01 16:29:08 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (action == BACK)
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(BACK, NULL);
|
2019-01-01 16:29:08 +00:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(action, NULL);
|
2019-01-01 16:29:08 +00:00
|
|
|
#endif
|
|
|
|
/* Add the contents of the next line to those of the current one. */
|
|
|
|
openfile->current->data = charealloc(openfile->current->data,
|
|
|
|
strlen(openfile->current->data) + strlen(joining->data) + 1);
|
|
|
|
strcat(openfile->current->data, joining->data);
|
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* Adjust the mark if it was on the line that was "eaten". */
|
|
|
|
if (openfile->mark == joining) {
|
|
|
|
openfile->mark = openfile->current;
|
|
|
|
openfile->mark_x += openfile->current_x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
unlink_node(joining);
|
2019-04-30 08:27:10 +00:00
|
|
|
renumber_from(openfile->current);
|
2019-01-01 16:29:08 +00:00
|
|
|
|
|
|
|
/* Two lines were joined, so we need to refresh the screen. */
|
|
|
|
refresh_needed = TRUE;
|
|
|
|
} else
|
|
|
|
/* We're at the end-of-file: nothing to do. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Adjust the file size, and remember it for a possible redo. */
|
|
|
|
openfile->totsize--;
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
openfile->current_undo->newsize = openfile->totsize;
|
|
|
|
|
|
|
|
/* If the number of screen rows that a softwrapped line occupies
|
|
|
|
* has changed, we need a full refresh. */
|
|
|
|
if (ISSET(SOFTWRAP) && refresh_needed == FALSE &&
|
|
|
|
number_of_chunks_in(openfile->current) != old_amount)
|
|
|
|
refresh_needed = TRUE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
set_modified();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the character under the cursor. */
|
|
|
|
void do_delete(void)
|
|
|
|
{
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (openfile->mark && ISSET(LET_THEM_ZAP))
|
|
|
|
zap_text();
|
|
|
|
else
|
|
|
|
#endif
|
2020-02-17 10:29:13 +00:00
|
|
|
do_deletion(DEL);
|
2019-01-01 16:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Backspace over one character. That is, move the cursor left one
|
|
|
|
* character, and then delete the character under the cursor. */
|
|
|
|
void do_backspace(void)
|
|
|
|
{
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (openfile->mark && ISSET(LET_THEM_ZAP))
|
|
|
|
zap_text();
|
|
|
|
else
|
|
|
|
#endif
|
2020-02-17 10:29:13 +00:00
|
|
|
if (openfile->current_x > 0 || openfile->current != openfile->filetop) {
|
2019-01-01 16:29:08 +00:00
|
|
|
do_left();
|
|
|
|
do_deletion(BACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 12:27:22 +00:00
|
|
|
/* Return FALSE when a cut command would not actually cut anything: when
|
|
|
|
* on an empty line at EOF, or when the mark covers zero characters, or
|
|
|
|
* (when test_cliff is TRUE) when the magic line would be cut. */
|
|
|
|
bool is_cuttable(bool test_cliff)
|
|
|
|
{
|
|
|
|
size_t from = (test_cliff) ? openfile->current_x : 0;
|
|
|
|
|
|
|
|
if ((openfile->current->next == NULL && openfile->current->data[from] == '\0'
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
&& openfile->mark == NULL) ||
|
|
|
|
(openfile->mark == openfile->current &&
|
|
|
|
openfile->mark_x == openfile->current_x) ||
|
|
|
|
(from > 0 && !ISSET(NO_NEWLINES) &&
|
|
|
|
openfile->current->data[from] == '\0' &&
|
|
|
|
openfile->current->next == openfile->filebot
|
|
|
|
#endif
|
|
|
|
)) {
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
statusbar(_("Nothing was cut"));
|
|
|
|
openfile->mark = NULL;
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
} else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-01-01 16:29:08 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* Delete text from the cursor until the first start of a word to
|
2019-02-24 16:27:22 +00:00
|
|
|
* the left, or to the right when forward is TRUE. */
|
|
|
|
void chop_word(bool forward)
|
2019-01-01 16:29:08 +00:00
|
|
|
{
|
|
|
|
/* Remember the current cursor position. */
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *is_current = openfile->current;
|
2019-01-01 16:29:08 +00:00
|
|
|
size_t is_current_x = openfile->current_x;
|
2019-04-28 17:21:40 +00:00
|
|
|
/* Remember where the cutbuffer is, then make it seem blank. */
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *is_cutbuffer = cutbuffer;
|
2019-04-28 17:21:40 +00:00
|
|
|
|
2019-01-01 16:29:08 +00:00
|
|
|
cutbuffer = NULL;
|
|
|
|
|
|
|
|
/* Move the cursor to a word start, to the left or to the right.
|
|
|
|
* If that word is on another line and the cursor was not already
|
|
|
|
* on the edge of the original line, then put the cursor on that
|
|
|
|
* edge instead, so that lines will not be joined unexpectedly. */
|
2019-02-24 16:27:22 +00:00
|
|
|
if (!forward) {
|
2019-01-01 16:29:08 +00:00
|
|
|
do_prev_word(ISSET(WORD_BOUNDS));
|
|
|
|
if (openfile->current != is_current) {
|
|
|
|
if (is_current_x > 0) {
|
|
|
|
openfile->current = is_current;
|
|
|
|
openfile->current_x = 0;
|
|
|
|
} else
|
|
|
|
openfile->current_x = strlen(openfile->current->data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_next_word(FALSE, ISSET(WORD_BOUNDS));
|
|
|
|
if (openfile->current != is_current &&
|
|
|
|
is_current->data[is_current_x] != '\0') {
|
|
|
|
openfile->current = is_current;
|
|
|
|
openfile->current_x = strlen(is_current->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the mark at the start of that word. */
|
|
|
|
openfile->mark = openfile->current;
|
|
|
|
openfile->mark_x = openfile->current_x;
|
|
|
|
|
|
|
|
/* 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. */
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(CUT, NULL);
|
2020-03-30 14:44:55 +00:00
|
|
|
do_snip(TRUE, FALSE, FALSE);
|
2019-08-01 11:24:56 +00:00
|
|
|
update_undo(CUT);
|
2019-01-01 16:29:08 +00:00
|
|
|
|
|
|
|
/* Discard the cut word and restore the cutbuffer. */
|
2019-03-21 16:18:50 +00:00
|
|
|
free_lines(cutbuffer);
|
2019-01-01 16:29:08 +00:00
|
|
|
cutbuffer = is_cutbuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a word leftward. */
|
2019-02-24 16:27:22 +00:00
|
|
|
void chop_previous_word(void)
|
2019-01-01 16:29:08 +00:00
|
|
|
{
|
2019-10-07 15:18:45 +00:00
|
|
|
if (openfile->current->prev == NULL && openfile->current_x == 0)
|
|
|
|
statusbar(_("Nothing was cut"));
|
|
|
|
else
|
|
|
|
chop_word(BACKWARD);
|
2019-01-01 16:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a word rightward. */
|
2019-02-24 16:27:22 +00:00
|
|
|
void chop_next_word(void)
|
2019-01-01 16:29:08 +00:00
|
|
|
{
|
2019-04-30 07:50:21 +00:00
|
|
|
openfile->mark = NULL;
|
|
|
|
|
2020-02-26 11:10:28 +00:00
|
|
|
if (is_cuttable(TRUE))
|
2019-02-24 16:27:22 +00:00
|
|
|
chop_word(FORWARD);
|
2019-01-01 16:29:08 +00:00
|
|
|
}
|
|
|
|
#endif /* !NANO_TINY */
|
|
|
|
|
2020-01-08 15:04:49 +00:00
|
|
|
/* Move all text between (top, top_x) and (bot, bot_x) from the current buffer
|
|
|
|
* into the cutbuffer. */
|
2020-01-08 15:13:04 +00:00
|
|
|
void extract_segment(linestruct *top, size_t top_x, linestruct *bot, size_t bot_x)
|
2020-01-08 15:04:49 +00:00
|
|
|
{
|
2020-01-08 15:55:45 +00:00
|
|
|
bool edittop_inside = (openfile->edittop->lineno >= top->lineno &&
|
|
|
|
openfile->edittop->lineno <= bot->lineno);
|
2020-01-08 15:04:49 +00:00
|
|
|
#ifndef NANO_TINY
|
2020-01-08 15:55:45 +00:00
|
|
|
bool mark_inside = (openfile->mark &&
|
|
|
|
openfile->mark->lineno >= top->lineno &&
|
|
|
|
openfile->mark->lineno <= bot->lineno &&
|
|
|
|
(openfile->mark != top || openfile->mark_x >= top_x) &&
|
|
|
|
(openfile->mark != bot || openfile->mark_x <= bot_x));
|
|
|
|
bool same_line = (openfile->mark == top);
|
2020-01-08 15:04:49 +00:00
|
|
|
|
|
|
|
if (top == bot && top_x == bot_x)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2020-01-08 15:55:45 +00:00
|
|
|
/* Reduce the buffer to cover just the text that needs to be extracted. */
|
2020-01-08 15:04:49 +00:00
|
|
|
partition_buffer(top, top_x, bot, bot_x);
|
|
|
|
|
2020-01-08 15:55:45 +00:00
|
|
|
/* Subtract the number of characters in that text from the file size. */
|
2020-01-08 15:04:49 +00:00
|
|
|
openfile->totsize -= get_totsize(top, bot);
|
|
|
|
|
2020-01-08 15:55:45 +00:00
|
|
|
/* If the cutbuffer is currently empty, just move all the text directly
|
|
|
|
* into it; otherwise, append the text to what is already there. */
|
2020-01-08 15:04:49 +00:00
|
|
|
if (cutbuffer == NULL) {
|
|
|
|
cutbuffer = openfile->filetop;
|
|
|
|
cutbottom = openfile->filebot;
|
|
|
|
} else {
|
|
|
|
/* Tack the data of the first line of the text onto the data of
|
|
|
|
* the last line in the given buffer. */
|
|
|
|
cutbottom->data = charealloc(cutbottom->data,
|
|
|
|
strlen(cutbottom->data) +
|
|
|
|
strlen(openfile->filetop->data) + 1);
|
|
|
|
strcat(cutbottom->data, openfile->filetop->data);
|
|
|
|
|
|
|
|
/* Attach the second line of the text (if any) to the last line
|
|
|
|
* of the buffer, then remove the now superfluous first line. */
|
|
|
|
cutbottom->next = openfile->filetop->next;
|
|
|
|
delete_node(openfile->filetop);
|
|
|
|
|
|
|
|
/* If there is a second line, make the reverse attachment too and
|
|
|
|
* update the buffer pointer to point at the end of the text. */
|
|
|
|
if (cutbottom->next != NULL) {
|
|
|
|
cutbottom->next->prev = cutbottom;
|
|
|
|
cutbottom = openfile->filebot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Since the text has now been saved, remove it from the file buffer. */
|
|
|
|
openfile->filetop = make_new_node(NULL);
|
|
|
|
openfile->filetop->data = copy_of("");
|
|
|
|
openfile->filebot = openfile->filetop;
|
|
|
|
|
|
|
|
/* Set the cursor at the point where the text was removed. */
|
|
|
|
openfile->current = openfile->filetop;
|
|
|
|
openfile->current_x = top_x;
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* If the mark was inside the partition, put it where the cursor now is. */
|
|
|
|
if (mark_inside) {
|
|
|
|
openfile->mark = openfile->current;
|
|
|
|
openfile->mark_x = openfile->current_x;
|
|
|
|
} else if (same_line)
|
|
|
|
/* Update the pointer to this partially cut line. */
|
|
|
|
openfile->mark = openfile->current;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Glue the texts before and after the extraction together. */
|
|
|
|
unpartition_buffer();
|
|
|
|
|
|
|
|
renumber_from(openfile->current);
|
|
|
|
|
|
|
|
/* If the top of the edit window was inside the old partition, put
|
|
|
|
* it in range of current. */
|
|
|
|
if (edittop_inside) {
|
|
|
|
adjust_viewport(STATIONARY);
|
|
|
|
refresh_needed = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the text doesn't end with a newline, and it should, add one. */
|
|
|
|
if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
|
|
|
|
new_magicline();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Meld the buffer that starts at topline into the current file buffer
|
|
|
|
* at the current cursor position. */
|
|
|
|
void ingraft_buffer(linestruct *topline)
|
|
|
|
{
|
2020-04-01 09:25:41 +00:00
|
|
|
size_t length = strlen(openfile->current->data);
|
|
|
|
size_t extralen = strlen(topline->data);
|
|
|
|
size_t xpos = openfile->current_x;
|
|
|
|
char *tailtext = copy_of(openfile->current->data + xpos);
|
|
|
|
linestruct *line = openfile->current;
|
2020-01-08 15:04:49 +00:00
|
|
|
#ifndef NANO_TINY
|
2020-04-01 09:25:41 +00:00
|
|
|
bool placed_after = (openfile->mark == line && !mark_is_before_cursor());
|
2020-01-08 15:04:49 +00:00
|
|
|
#endif
|
2020-04-01 09:25:41 +00:00
|
|
|
linestruct *botline = topline;
|
2020-01-08 15:04:49 +00:00
|
|
|
|
2020-04-01 09:25:41 +00:00
|
|
|
while (botline->next != NULL)
|
|
|
|
botline = botline->next;
|
2020-01-08 15:04:49 +00:00
|
|
|
|
2020-04-01 09:25:41 +00:00
|
|
|
/* Add the size of the text to be grafted to the buffer size. */
|
|
|
|
openfile->totsize += get_totsize(topline, botline);
|
2020-01-08 15:04:49 +00:00
|
|
|
|
2020-04-01 09:25:41 +00:00
|
|
|
if (topline != botline)
|
|
|
|
length = xpos;
|
2020-01-08 15:04:49 +00:00
|
|
|
|
2020-04-01 09:25:41 +00:00
|
|
|
if (extralen > 0) {
|
|
|
|
/* Insert the text of topline at the current cursor position. */
|
|
|
|
line->data = charealloc(line->data, length + extralen + 1);
|
|
|
|
memmove(line->data + xpos + extralen, line->data + xpos, length - xpos + 1);
|
|
|
|
strncpy(line->data + xpos, topline->data, extralen);
|
2020-01-08 15:04:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 09:25:41 +00:00
|
|
|
if (topline != botline) {
|
|
|
|
/* When inserting at end-of-buffer, update the relevant pointer. */
|
|
|
|
if (line->next == NULL)
|
|
|
|
openfile->filebot = botline;
|
|
|
|
|
|
|
|
line->data[xpos + extralen] = '\0';
|
|
|
|
|
|
|
|
/* Hook the grafted lines in after the current one. */
|
|
|
|
botline->next = openfile->current->next;
|
|
|
|
if (botline->next)
|
|
|
|
botline->next->prev = botline;
|
|
|
|
openfile->current->next = topline->next;
|
|
|
|
topline->next->prev = openfile->current;
|
|
|
|
|
|
|
|
/* Add the text after the cursor position at the end of botline. */
|
|
|
|
length = strlen(botline->data);
|
|
|
|
extralen = strlen(tailtext);
|
|
|
|
botline->data = charealloc(botline->data, length + extralen + 1);
|
|
|
|
strcpy(botline->data + length, tailtext);
|
|
|
|
|
|
|
|
/* Put the cursor at the end of the grafted text. */
|
|
|
|
openfile->current = botline;
|
|
|
|
openfile->current_x = length;
|
|
|
|
} else
|
|
|
|
openfile->current_x += extralen;
|
2020-01-08 15:04:49 +00:00
|
|
|
|
2020-04-01 09:25:41 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* When needed, update the mark's pointer and position. */
|
|
|
|
if (placed_after && topline != botline) {
|
|
|
|
openfile->mark = botline;
|
|
|
|
openfile->mark_x += length - xpos;
|
|
|
|
} else if (placed_after)
|
|
|
|
openfile->mark_x += extralen;
|
|
|
|
#endif
|
2020-01-08 15:04:49 +00:00
|
|
|
|
2020-04-01 09:25:41 +00:00
|
|
|
delete_node(topline);
|
|
|
|
free(tailtext);
|
2020-01-08 15:04:49 +00:00
|
|
|
|
2020-04-01 09:25:41 +00:00
|
|
|
renumber_from(line);
|
2020-01-08 15:04:49 +00:00
|
|
|
|
|
|
|
/* If the text doesn't end with a newline, and it should, add one. */
|
|
|
|
if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
|
|
|
|
new_magicline();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Meld a copy of the given buffer into the current file buffer. */
|
|
|
|
void copy_from_buffer(linestruct *somebuffer)
|
|
|
|
{
|
|
|
|
linestruct *the_copy = copy_buffer(somebuffer);
|
|
|
|
|
|
|
|
ingraft_buffer(the_copy);
|
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-02 08:14:08 +00:00
|
|
|
/* Move all marked text from the current buffer into the cutbuffer. */
|
2020-03-29 11:14:17 +00:00
|
|
|
void cut_marked_region(void)
|
2019-05-02 08:14:08 +00:00
|
|
|
{
|
|
|
|
linestruct *top, *bot;
|
|
|
|
size_t top_x, bot_x;
|
|
|
|
|
2020-03-29 18:17:32 +00:00
|
|
|
get_region(&top, &top_x, &bot, &bot_x);
|
2019-05-02 08:14:08 +00:00
|
|
|
|
2020-01-08 15:13:04 +00:00
|
|
|
extract_segment(top, top_x, bot, bot_x);
|
2019-05-02 08:14:08 +00:00
|
|
|
|
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
}
|
2020-03-30 17:05:57 +00:00
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2020-03-30 11:51:15 +00:00
|
|
|
/* Move text from the current buffer into the cutbuffer.
|
2019-05-02 08:01:42 +00:00
|
|
|
* If until_eof is TRUE, move all text from the current cursor
|
2018-10-24 02:25:22 +00:00
|
|
|
* position to the end of the file into the cutbuffer. If append
|
|
|
|
* is TRUE (when zapping), always append the cut to the cutbuffer. */
|
2020-03-30 14:44:55 +00:00
|
|
|
void do_snip(bool marked, bool until_eof, bool append)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2020-03-30 17:10:30 +00:00
|
|
|
linestruct *line = openfile->current;
|
|
|
|
|
2020-04-03 15:12:28 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
keep_cutbuffer &= (openfile->last_action != COPY);
|
|
|
|
#endif
|
|
|
|
|
2018-06-02 15:57:19 +00:00
|
|
|
/* If cuts were not continuous, or when cutting a region, clear the slate. */
|
2020-04-03 15:12:28 +00:00
|
|
|
if ((marked || until_eof || !keep_cutbuffer) && !append) {
|
2019-03-21 16:18:50 +00:00
|
|
|
free_lines(cutbuffer);
|
2017-12-29 18:27:33 +00:00
|
|
|
cutbuffer = NULL;
|
|
|
|
}
|
2000-08-07 14:58:26 +00:00
|
|
|
|
2006-04-25 02:23:28 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-05-02 08:14:08 +00:00
|
|
|
/* Now move the relevant piece of text into the cutbuffer. */
|
|
|
|
if (until_eof)
|
2020-03-30 17:05:57 +00:00
|
|
|
extract_segment(openfile->current, openfile->current_x,
|
|
|
|
openfile->filebot, strlen(openfile->filebot->data));
|
2019-05-02 08:14:08 +00:00
|
|
|
else if (openfile->mark) {
|
2020-03-29 11:14:17 +00:00
|
|
|
cut_marked_region();
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->mark = NULL;
|
2020-03-30 17:05:57 +00:00
|
|
|
} else if (ISSET(CUT_FROM_CURSOR)) {
|
|
|
|
/* When not at the end of a line, move the rest of this line into
|
|
|
|
* the cutbuffer. Otherwise, when not at the end of the buffer,
|
|
|
|
* move just the "line separator" into the cutbuffer. */
|
|
|
|
if (openfile->current_x < strlen(openfile->current->data))
|
2020-03-30 17:10:30 +00:00
|
|
|
extract_segment(line, openfile->current_x, line, strlen(line->data));
|
2020-03-30 17:05:57 +00:00
|
|
|
else if (openfile->current != openfile->filebot) {
|
2020-03-30 17:10:30 +00:00
|
|
|
extract_segment(line, openfile->current_x, line->next, 0);
|
2020-03-30 17:05:57 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
}
|
|
|
|
} else
|
2005-03-26 22:49:46 +00:00
|
|
|
#endif
|
2020-03-30 17:05:57 +00:00
|
|
|
{
|
|
|
|
/* When not at end-of-buffer, move one full line into the cutbuffer;
|
|
|
|
* otherwise, move all text until end-of-line into the cutbuffer. */
|
|
|
|
if (openfile->current != openfile->filebot)
|
2020-03-30 17:10:30 +00:00
|
|
|
extract_segment(line, 0, line->next, 0);
|
2020-03-30 17:05:57 +00:00
|
|
|
else
|
2020-03-30 17:10:30 +00:00
|
|
|
extract_segment(line, 0, line, strlen(line->data));
|
2020-03-30 17:05:57 +00:00
|
|
|
|
|
|
|
openfile->placewewant = 0;
|
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2020-03-30 17:10:30 +00:00
|
|
|
/* After a line operation, future ones should add to the cutbuffer. */
|
|
|
|
keep_cutbuffer = !marked && !until_eof;
|
|
|
|
|
2020-03-30 11:51:15 +00:00
|
|
|
set_modified();
|
2017-12-29 18:27:33 +00:00
|
|
|
refresh_needed = TRUE;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 15:38:34 +00:00
|
|
|
/* Move text from the current buffer into the cutbuffer. */
|
2019-05-03 17:51:15 +00:00
|
|
|
void cut_text(void)
|
2006-04-25 02:23:28 +00:00
|
|
|
{
|
2019-01-06 10:56:42 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-01-06 12:12:44 +00:00
|
|
|
if (!is_cuttable(ISSET(CUT_FROM_CURSOR) && openfile->mark == NULL))
|
2018-12-30 11:21:33 +00:00
|
|
|
return;
|
|
|
|
|
2018-09-26 14:42:23 +00:00
|
|
|
/* Only add a new undo item when the current item is not a CUT or when
|
|
|
|
* the current cut is not contiguous with the previous cutting. */
|
2019-05-03 15:36:09 +00:00
|
|
|
if (openfile->last_action != CUT || !keep_cutbuffer) {
|
|
|
|
keep_cutbuffer = FALSE;
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(CUT, NULL);
|
2019-05-03 15:36:09 +00:00
|
|
|
}
|
2019-05-03 10:41:49 +00:00
|
|
|
|
2020-03-30 14:44:55 +00:00
|
|
|
do_snip(openfile->mark != NULL, FALSE, FALSE);
|
2019-05-03 10:41:49 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
update_undo(CUT);
|
2018-07-10 13:31:04 +00:00
|
|
|
#else
|
2019-01-06 12:12:44 +00:00
|
|
|
if (is_cuttable(FALSE))
|
2020-03-30 14:44:55 +00:00
|
|
|
do_snip(FALSE, FALSE, FALSE);
|
2008-08-02 14:39:42 +00:00
|
|
|
#endif
|
2019-08-01 11:24:56 +00:00
|
|
|
wipe_statusbar();
|
2006-04-25 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2020-03-30 17:20:18 +00:00
|
|
|
/* Cut from the current cursor position to the end of the file. */
|
|
|
|
void cut_till_eof(void)
|
|
|
|
{
|
|
|
|
if (openfile->current->data[openfile->current_x] == '\0' &&
|
|
|
|
(openfile->current->next == NULL ||
|
|
|
|
(!ISSET(NO_NEWLINES) && openfile->current_x > 0 &&
|
|
|
|
openfile->current->next == openfile->filebot))) {
|
|
|
|
statusbar(_("Nothing was cut"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_undo(CUT_TO_EOF, NULL);
|
|
|
|
do_snip(FALSE, TRUE, FALSE);
|
|
|
|
update_undo(CUT_TO_EOF);
|
|
|
|
wipe_statusbar();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Erase text (current line or marked region), sending it into oblivion. */
|
|
|
|
void zap_text(void)
|
|
|
|
{
|
|
|
|
/* Remember the current cutbuffer so it can be restored after the zap. */
|
|
|
|
linestruct *was_cutbuffer = cutbuffer;
|
|
|
|
|
|
|
|
if (!is_cuttable(ISSET(CUT_FROM_CURSOR) && openfile->mark == NULL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Add a new undo item only when the current item is not a ZAP or when
|
|
|
|
* the current zap is not contiguous with the previous zapping. */
|
|
|
|
if (openfile->last_action != ZAP || !keep_cutbuffer)
|
|
|
|
add_undo(ZAP, NULL);
|
|
|
|
|
|
|
|
/* Use the cutbuffer from the ZAP undo item, so the cut can be undone. */
|
|
|
|
cutbuffer = openfile->current_undo->cutbuffer;
|
|
|
|
|
|
|
|
do_snip(openfile->mark != NULL, FALSE, TRUE);
|
|
|
|
|
|
|
|
update_undo(ZAP);
|
|
|
|
wipe_statusbar();
|
|
|
|
|
|
|
|
cutbuffer = was_cutbuffer;
|
|
|
|
}
|
|
|
|
|
2020-03-30 11:51:15 +00:00
|
|
|
/* Make a copy of the marked region, putting it in the cutbuffer. */
|
|
|
|
void copy_marked_region(void)
|
|
|
|
{
|
|
|
|
linestruct *topline, *botline, *afterline;
|
|
|
|
char *was_datastart, saved_byte;
|
|
|
|
size_t top_x, bot_x;
|
|
|
|
|
|
|
|
get_region(&topline, &top_x, &botline, &bot_x);
|
|
|
|
|
|
|
|
openfile->last_action = OTHER;
|
|
|
|
keep_cutbuffer = FALSE;
|
|
|
|
openfile->mark = NULL;
|
|
|
|
refresh_needed = TRUE;
|
|
|
|
|
2020-03-31 17:40:23 +00:00
|
|
|
if (topline == botline && top_x == bot_x) {
|
|
|
|
statusbar(_("Copied nothing"));
|
2020-03-30 11:51:15 +00:00
|
|
|
return;
|
2020-03-31 17:40:23 +00:00
|
|
|
}
|
2020-03-30 11:51:15 +00:00
|
|
|
|
|
|
|
/* Make the area that was marked look like a separate buffer. */
|
|
|
|
afterline = botline->next;
|
|
|
|
botline->next = NULL;
|
|
|
|
saved_byte = botline->data[bot_x];
|
|
|
|
botline->data[bot_x] = '\0';
|
|
|
|
was_datastart = topline->data;
|
|
|
|
topline->data += top_x;
|
|
|
|
|
|
|
|
cutbuffer = copy_buffer(topline);
|
|
|
|
|
|
|
|
/* Restore the proper state of the buffer. */
|
|
|
|
topline->data = was_datastart;
|
|
|
|
botline->data[bot_x] = saved_byte;
|
|
|
|
botline->next = afterline;
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:38:47 +00:00
|
|
|
/* Copy text from the current buffer into the cutbuffer. The text is either
|
|
|
|
* the marked region, the whole line, the text from cursor to end-of-line,
|
|
|
|
* just the line break, or nothing, depending on mode and cursor position. */
|
2019-05-03 17:47:08 +00:00
|
|
|
void copy_text(void)
|
2006-04-25 02:23:28 +00:00
|
|
|
{
|
2020-03-30 11:51:15 +00:00
|
|
|
bool at_eol = (openfile->current->data[openfile->current_x] == '\0');
|
2020-03-31 17:26:39 +00:00
|
|
|
bool sans_newline = (ISSET(NO_NEWLINES) && openfile->current->next == NULL);
|
2020-03-31 17:21:38 +00:00
|
|
|
size_t from_x = (ISSET(CUT_FROM_CURSOR)) ? openfile->current_x : 0;
|
2020-04-08 09:04:35 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2020-03-30 11:51:15 +00:00
|
|
|
linestruct *addition;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2020-03-30 11:51:15 +00:00
|
|
|
if (openfile->mark || openfile->last_action != COPY || !keep_cutbuffer) {
|
|
|
|
free_lines(cutbuffer);
|
|
|
|
cutbuffer = NULL;
|
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2020-04-03 14:34:27 +00:00
|
|
|
wipe_statusbar();
|
|
|
|
|
2020-03-30 11:51:15 +00:00
|
|
|
if (openfile->mark) {
|
|
|
|
copy_marked_region();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:25:02 +00:00
|
|
|
/* When at the very end of the buffer, there is nothing to do. */
|
2020-03-31 15:50:21 +00:00
|
|
|
if (openfile->current->next == NULL && at_eol && (ISSET(CUT_FROM_CURSOR) ||
|
|
|
|
openfile->current_x == 0 || cutbuffer)) {
|
|
|
|
statusbar(_("Copied nothing"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-30 11:51:15 +00:00
|
|
|
addition = make_new_node(NULL);
|
2020-03-31 17:21:38 +00:00
|
|
|
addition->data = copy_of(openfile->current->data + from_x);
|
2020-03-30 11:51:15 +00:00
|
|
|
|
2020-03-31 17:26:39 +00:00
|
|
|
if (ISSET(CUT_FROM_CURSOR))
|
|
|
|
sans_newline = !at_eol;
|
|
|
|
|
2020-04-08 09:25:02 +00:00
|
|
|
/* Create the cutbuffer OR add to it, depending on the mode, the position
|
2020-03-30 11:51:15 +00:00
|
|
|
* of the cursor, and whether or not the cutbuffer is currently empty. */
|
2020-03-31 17:38:47 +00:00
|
|
|
if (cutbuffer == NULL && sans_newline) {
|
|
|
|
cutbuffer = addition;
|
|
|
|
cutbottom = addition;
|
|
|
|
} else if (cutbuffer == NULL) {
|
|
|
|
cutbuffer = addition;
|
|
|
|
cutbottom = make_new_node(cutbuffer);
|
|
|
|
cutbottom->data = copy_of("");
|
|
|
|
cutbuffer->next = cutbottom;
|
|
|
|
} else if (sans_newline) {
|
|
|
|
addition->prev = cutbottom->prev;
|
|
|
|
addition->prev->next = addition;
|
|
|
|
delete_node(cutbottom);
|
|
|
|
cutbottom = addition;
|
|
|
|
} else if (ISSET(CUT_FROM_CURSOR)) {
|
|
|
|
addition->prev = cutbottom;
|
|
|
|
cutbottom->next = addition;
|
|
|
|
cutbottom = addition;
|
|
|
|
} else {
|
|
|
|
addition->prev = cutbottom->prev;
|
|
|
|
addition->prev->next = addition;
|
|
|
|
addition->next = cutbottom;
|
|
|
|
cutbottom->prev = addition;
|
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2020-04-08 09:25:02 +00:00
|
|
|
/* When needed and possible, move the cursor to the next line. */
|
2020-03-30 11:51:15 +00:00
|
|
|
if ((!ISSET(CUT_FROM_CURSOR) || at_eol) && openfile->current->next) {
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
openfile->current_x = 0;
|
2019-04-30 17:31:44 +00:00
|
|
|
} else
|
2020-03-30 11:51:15 +00:00
|
|
|
openfile->current_x = strlen(openfile->current->data);
|
2019-05-03 10:24:48 +00:00
|
|
|
|
2020-04-08 09:04:35 +00:00
|
|
|
edit_redraw(was_current, FLOWING);
|
2020-03-30 11:51:15 +00:00
|
|
|
|
|
|
|
openfile->last_action = COPY;
|
|
|
|
keep_cutbuffer = TRUE;
|
2006-04-25 02:23:28 +00:00
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2005-01-01 07:43:32 +00:00
|
|
|
|
2017-04-19 15:38:34 +00:00
|
|
|
/* Copy text from the cutbuffer into the current buffer. */
|
2019-05-03 17:47:08 +00:00
|
|
|
void paste_text(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
ssize_t was_lineno = openfile->current->lineno;
|
|
|
|
/* The line number where we started the paste. */
|
|
|
|
size_t was_leftedge = 0;
|
|
|
|
/* The leftedge where we started the paste. */
|
2016-04-12 08:24:57 +00:00
|
|
|
|
2018-12-31 12:17:14 +00:00
|
|
|
if (cutbuffer == NULL) {
|
2019-04-06 07:34:23 +00:00
|
|
|
statusbar(_("Cutbuffer is empty"));
|
2017-12-29 18:27:33 +00:00
|
|
|
return;
|
2018-12-31 12:17:14 +00:00
|
|
|
}
|
2004-11-06 20:33:43 +00:00
|
|
|
|
2008-09-16 21:35:19 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(PASTE, NULL);
|
2017-01-15 22:01:17 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(SOFTWRAP))
|
|
|
|
was_leftedge = leftedge_for(xplustabs(), openfile->current);
|
2008-09-16 21:35:19 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Add a copy of the text in the cutbuffer to the current buffer
|
|
|
|
* at the current cursor position. */
|
|
|
|
copy_from_buffer(cutbuffer);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2014-05-25 19:41:49 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
update_undo(PASTE);
|
2014-05-25 19:41:49 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we pasted less than a screenful, don't center the cursor. */
|
|
|
|
if (less_than_a_screenful(was_lineno, was_leftedge))
|
|
|
|
focusing = FALSE;
|
2016-04-12 08:24:57 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Set the desired x position to where the pasted text ends. */
|
|
|
|
openfile->placewewant = xplustabs();
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
set_modified();
|
2019-08-01 11:24:56 +00:00
|
|
|
wipe_statusbar();
|
2017-12-29 18:27:33 +00:00
|
|
|
refresh_needed = TRUE;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|