|
Packit |
1ac44c |
/**************************************************************************
|
|
Packit |
1ac44c |
* cut.c -- This file is part of GNU nano. *
|
|
Packit |
1ac44c |
* *
|
|
Packit |
1ac44c |
* Copyright (C) 1999-2011, 2013-2018 Free Software Foundation, Inc. *
|
|
Packit |
1ac44c |
* Copyright (C) 2014 Mark Majeres *
|
|
Packit |
1ac44c |
* Copyright (C) 2016 Benno Schulenberg *
|
|
Packit |
1ac44c |
* *
|
|
Packit |
1ac44c |
* GNU nano is free software: you can redistribute it and/or modify *
|
|
Packit |
1ac44c |
* it under the terms of the GNU General Public License as published *
|
|
Packit |
1ac44c |
* by the Free Software Foundation, either version 3 of the License, *
|
|
Packit |
1ac44c |
* or (at your option) any later version. *
|
|
Packit |
1ac44c |
* *
|
|
Packit |
1ac44c |
* GNU nano is distributed in the hope that it will be useful, *
|
|
Packit |
1ac44c |
* but WITHOUT ANY WARRANTY; without even the implied warranty *
|
|
Packit |
1ac44c |
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
Packit |
1ac44c |
* See the GNU General Public License for more details. *
|
|
Packit |
1ac44c |
* *
|
|
Packit |
1ac44c |
* You should have received a copy of the GNU General Public License *
|
|
Packit |
1ac44c |
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
|
Packit |
1ac44c |
* *
|
|
Packit |
1ac44c |
**************************************************************************/
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#include "proto.h"
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#include <string.h>
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
static bool keep_cutbuffer = FALSE;
|
|
Packit |
1ac44c |
/* Should we keep the contents of the cutbuffer? */
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Indicate that we should no longer keep the contents of the cutbuffer. */
|
|
Packit |
1ac44c |
void cutbuffer_reset(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
keep_cutbuffer = FALSE;
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Return the status of cutbuffer preservation. */
|
|
Packit |
1ac44c |
inline bool keeping_cutbuffer(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
return keep_cutbuffer;
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* If we aren't on the last line of the file, move all the text of the
|
|
Packit |
1ac44c |
* current line, plus the newline at the end, into the cutbuffer. If we
|
|
Packit |
1ac44c |
* are, move all of the text of the current line into the cutbuffer. In
|
|
Packit |
1ac44c |
* both cases, set the current place we want to the beginning of the
|
|
Packit |
1ac44c |
* current line. */
|
|
Packit |
1ac44c |
void cut_line(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
if (openfile->current != openfile->filebot)
|
|
Packit |
1ac44c |
extract_buffer(&cutbuffer, &cutbottom, openfile->current, 0,
|
|
Packit |
1ac44c |
openfile->current->next, 0);
|
|
Packit |
1ac44c |
else
|
|
Packit |
1ac44c |
extract_buffer(&cutbuffer, &cutbottom, openfile->current, 0,
|
|
Packit |
1ac44c |
openfile->current, strlen(openfile->current->data));
|
|
Packit |
1ac44c |
openfile->placewewant = 0;
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
/* Move all currently marked text into the cutbuffer, and set the
|
|
Packit |
1ac44c |
* current place we want to where the text used to start. */
|
|
Packit |
1ac44c |
void cut_marked(bool *right_side_up)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
filestruct *top, *bot;
|
|
Packit |
1ac44c |
size_t top_x, bot_x;
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
mark_order((const filestruct **)&top, &top_x,
|
|
Packit |
1ac44c |
(const filestruct **)&bot, &bot_x, right_side_up);
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
extract_buffer(&cutbuffer, &cutbottom, top, top_x, bot, bot_x);
|
|
Packit |
1ac44c |
openfile->placewewant = xplustabs();
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* If we aren't at the end of the current line, move all the text from
|
|
Packit |
1ac44c |
* the current cursor position to the end of the current line, not
|
|
Packit |
1ac44c |
* counting the newline at the end, into the cutbuffer. If we are, and
|
|
Packit |
1ac44c |
* we're not on the last line of the file, move the newline at the end
|
|
Packit |
1ac44c |
* into the cutbuffer, and set the current place we want to where the
|
|
Packit |
1ac44c |
* newline used to be. */
|
|
Packit |
1ac44c |
void cut_to_eol(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
size_t data_len = strlen(openfile->current->data);
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
if (openfile->current_x < data_len)
|
|
Packit |
1ac44c |
/* If we're not at the end of the line, move all the text from
|
|
Packit |
1ac44c |
* the current position up to it, not counting the newline at
|
|
Packit |
1ac44c |
* the end, into the cutbuffer. */
|
|
Packit |
1ac44c |
extract_buffer(&cutbuffer, &cutbottom, openfile->current,
|
|
Packit |
1ac44c |
openfile->current_x, openfile->current, data_len);
|
|
Packit |
1ac44c |
else if (openfile->current != openfile->filebot) {
|
|
Packit |
1ac44c |
/* If we're at the end of the line, and it isn't the last line
|
|
Packit |
1ac44c |
* of the file, move all the text from the current position up
|
|
Packit |
1ac44c |
* to the beginning of the next line, i.e. the newline at the
|
|
Packit |
1ac44c |
* end, into the cutbuffer. */
|
|
Packit |
1ac44c |
extract_buffer(&cutbuffer, &cutbottom, openfile->current,
|
|
Packit |
1ac44c |
openfile->current_x, openfile->current->next, 0);
|
|
Packit |
1ac44c |
openfile->placewewant = xplustabs();
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Move all the text from the current cursor position to the end of the
|
|
Packit |
1ac44c |
* file into the cutbuffer. */
|
|
Packit |
1ac44c |
void cut_to_eof(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
extract_buffer(&cutbuffer, &cutbottom,
|
|
Packit |
1ac44c |
openfile->current, openfile->current_x,
|
|
Packit |
1ac44c |
openfile->filebot, strlen(openfile->filebot->data));
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
#endif /* !NANO_TINY */
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Move text from the current buffer into the cutbuffer. If
|
|
Packit |
1ac44c |
* copy_text is TRUE, copy the text back into the buffer afterward.
|
|
Packit |
1ac44c |
* If cut_till_eof is TRUE, move all text from the current cursor
|
|
Packit |
1ac44c |
* position to the end of the file into the cutbuffer. */
|
|
Packit |
1ac44c |
void do_cut_text(bool copy_text, bool cut_till_eof)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
filestruct *cb_save = NULL;
|
|
Packit |
1ac44c |
/* The current end of the cutbuffer, before we add text to it. */
|
|
Packit |
1ac44c |
size_t cb_save_len = 0;
|
|
Packit |
1ac44c |
/* The length of the string at the current end of the cutbuffer,
|
|
Packit |
1ac44c |
* before we add text to it. */
|
|
Packit |
1ac44c |
bool old_no_newlines = ISSET(NO_NEWLINES);
|
|
Packit |
1ac44c |
bool right_side_up = TRUE;
|
|
Packit |
1ac44c |
/* There *is* no region, *or* it is marked forward. */
|
|
Packit |
1ac44c |
#endif
|
|
Packit |
1ac44c |
size_t was_totsize = openfile->totsize;
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* If a chain of cuts was broken, empty the cutbuffer. */
|
|
Packit |
1ac44c |
if (!keep_cutbuffer) {
|
|
Packit |
1ac44c |
free_filestruct(cutbuffer);
|
|
Packit |
1ac44c |
cutbuffer = NULL;
|
|
Packit |
1ac44c |
/* Indicate that future cuts should add to the cutbuffer. */
|
|
Packit |
1ac44c |
keep_cutbuffer = TRUE;
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
if (copy_text) {
|
|
Packit |
1ac44c |
/* If the cutbuffer isn't empty, remember where it currently ends. */
|
|
Packit |
1ac44c |
if (cutbuffer != NULL) {
|
|
Packit |
1ac44c |
cb_save = cutbottom;
|
|
Packit |
1ac44c |
cb_save_len = strlen(cutbottom->data);
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
/* Don't add a magicline when moving text to the cutbuffer. */
|
|
Packit |
1ac44c |
SET(NO_NEWLINES);
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
if (cut_till_eof) {
|
|
Packit |
1ac44c |
/* Move all text up to the end of the file into the cutbuffer. */
|
|
Packit |
1ac44c |
cut_to_eof();
|
|
Packit |
1ac44c |
} else if (openfile->mark) {
|
|
Packit |
1ac44c |
/* Move the marked text to the cutbuffer, and turn the mark off. */
|
|
Packit |
1ac44c |
cut_marked(&right_side_up);
|
|
Packit |
1ac44c |
openfile->mark = NULL;
|
|
Packit |
1ac44c |
} else if (ISSET(CUT_FROM_CURSOR))
|
|
Packit |
1ac44c |
/* Move all text up to the end of the line into the cutbuffer. */
|
|
Packit |
1ac44c |
cut_to_eol();
|
|
Packit |
1ac44c |
else
|
|
Packit |
1ac44c |
#endif
|
|
Packit |
1ac44c |
/* Move the entire line into the cutbuffer. */
|
|
Packit |
1ac44c |
cut_line();
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
if (copy_text) {
|
|
Packit |
1ac44c |
/* Copy the text that is in the cutbuffer (starting at its saved end,
|
|
Packit |
1ac44c |
* if there is one) back into the current buffer. This effectively
|
|
Packit |
1ac44c |
* uncuts the text we just cut. */
|
|
Packit |
1ac44c |
if (cutbuffer != NULL) {
|
|
Packit |
1ac44c |
if (cb_save != NULL) {
|
|
Packit |
1ac44c |
cb_save->data += cb_save_len;
|
|
Packit |
1ac44c |
copy_from_buffer(cb_save);
|
|
Packit |
1ac44c |
cb_save->data -= cb_save_len;
|
|
Packit |
1ac44c |
} else
|
|
Packit |
1ac44c |
copy_from_buffer(cutbuffer);
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* If the copied region was marked forward, put the new desired
|
|
Packit |
1ac44c |
* x position at its end; otherwise, leave it at its beginning. */
|
|
Packit |
1ac44c |
if (right_side_up)
|
|
Packit |
1ac44c |
openfile->placewewant = xplustabs();
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
/* Restore the magicline behavior now that we're done fiddling. */
|
|
Packit |
1ac44c |
if (!old_no_newlines)
|
|
Packit |
1ac44c |
UNSET(NO_NEWLINES);
|
|
Packit |
1ac44c |
} else
|
|
Packit |
1ac44c |
#endif /* !NANO_TINY */
|
|
Packit |
1ac44c |
/* Only set the modification flag if actually something was cut. */
|
|
Packit |
1ac44c |
if (openfile->totsize != was_totsize)
|
|
Packit |
1ac44c |
set_modified();
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
refresh_needed = TRUE;
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#ifdef DEBUG
|
|
Packit |
1ac44c |
dump_filestruct(cutbuffer);
|
|
Packit |
1ac44c |
#endif
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Move text from the current buffer into the cutbuffer. */
|
|
Packit |
1ac44c |
void do_cut_text_void(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
add_undo(CUT);
|
|
Packit |
1ac44c |
#endif
|
|
Packit |
1ac44c |
do_cut_text(FALSE, FALSE);
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
update_undo(CUT);
|
|
Packit |
1ac44c |
#endif
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
/* Move text from the current buffer into the cutbuffer, and copy it
|
|
Packit |
1ac44c |
* back into the buffer afterward. If the mark is set or the cursor
|
|
Packit |
1ac44c |
* was moved, blow away previous contents of the cutbuffer. */
|
|
Packit |
1ac44c |
void do_copy_text(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
static struct filestruct *next_contiguous_line = NULL;
|
|
Packit |
1ac44c |
bool mark_is_set = (openfile->mark != NULL);
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Remember the current viewport and cursor position. */
|
|
Packit |
1ac44c |
ssize_t is_edittop_lineno = openfile->edittop->lineno;
|
|
Packit |
1ac44c |
size_t is_firstcolumn = openfile->firstcolumn;
|
|
Packit |
1ac44c |
ssize_t is_current_lineno = openfile->current->lineno;
|
|
Packit |
1ac44c |
size_t is_current_x = openfile->current_x;
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
if (mark_is_set || openfile->current != next_contiguous_line)
|
|
Packit |
1ac44c |
cutbuffer_reset();
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
do_cut_text(TRUE, FALSE);
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* If the mark was set, blow away the cutbuffer on the next copy. */
|
|
Packit |
1ac44c |
next_contiguous_line = (mark_is_set ? NULL : openfile->current);
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* If the mark was set, restore the viewport and cursor position. */
|
|
Packit |
1ac44c |
if (mark_is_set) {
|
|
Packit |
1ac44c |
openfile->edittop = fsfromline(is_edittop_lineno);
|
|
Packit |
1ac44c |
openfile->firstcolumn = is_firstcolumn;
|
|
Packit |
1ac44c |
openfile->current = fsfromline(is_current_lineno);
|
|
Packit |
1ac44c |
openfile->current_x = is_current_x;
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Cut from the current cursor position to the end of the file. */
|
|
Packit |
1ac44c |
void do_cut_till_eof(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
add_undo(CUT_TO_EOF);
|
|
Packit |
1ac44c |
do_cut_text(FALSE, TRUE);
|
|
Packit |
1ac44c |
update_undo(CUT_TO_EOF);
|
|
Packit |
1ac44c |
}
|
|
Packit |
1ac44c |
#endif /* !NANO_TINY */
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Copy text from the cutbuffer into the current buffer. */
|
|
Packit |
1ac44c |
void do_uncut_text(void)
|
|
Packit |
1ac44c |
{
|
|
Packit |
1ac44c |
ssize_t was_lineno = openfile->current->lineno;
|
|
Packit |
1ac44c |
/* The line number where we started the paste. */
|
|
Packit |
1ac44c |
size_t was_leftedge = 0;
|
|
Packit |
1ac44c |
/* The leftedge where we started the paste. */
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* If the cutbuffer is empty, there is nothing to do. */
|
|
Packit |
1ac44c |
if (cutbuffer == NULL)
|
|
Packit |
1ac44c |
return;
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
add_undo(PASTE);
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
if (ISSET(SOFTWRAP))
|
|
Packit |
1ac44c |
was_leftedge = leftedge_for(xplustabs(), openfile->current);
|
|
Packit |
1ac44c |
#endif
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Add a copy of the text in the cutbuffer to the current buffer
|
|
Packit |
1ac44c |
* at the current cursor position. */
|
|
Packit |
1ac44c |
copy_from_buffer(cutbuffer);
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
#ifndef NANO_TINY
|
|
Packit |
1ac44c |
update_undo(PASTE);
|
|
Packit |
1ac44c |
#endif
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* If we pasted less than a screenful, don't center the cursor. */
|
|
Packit |
1ac44c |
if (less_than_a_screenful(was_lineno, was_leftedge))
|
|
Packit |
1ac44c |
focusing = FALSE;
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
/* Set the desired x position to where the pasted text ends. */
|
|
Packit |
1ac44c |
openfile->placewewant = xplustabs();
|
|
Packit |
1ac44c |
|
|
Packit |
1ac44c |
set_modified();
|
|
Packit |
1ac44c |
refresh_needed = TRUE;
|
|
Packit |
1ac44c |
}
|