2000-08-06 21:13:45 +00:00
|
|
|
/* $Id$ */
|
2000-06-06 05:53:49 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* cut.c *
|
|
|
|
* *
|
2007-10-11 05:01:32 +00:00
|
|
|
* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 *
|
|
|
|
* Free Software Foundation, Inc. *
|
2000-06-06 05:53:49 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
2007-08-11 05:17:36 +00:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option) *
|
2000-06-06 05:53:49 +00:00
|
|
|
* any later version. *
|
|
|
|
* *
|
2005-05-15 19:57:17 +00:00
|
|
|
* 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. *
|
2000-06-06 05:53:49 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
2005-05-15 19:57:17 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
|
|
|
|
* 02110-1301, USA. *
|
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>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2004-08-12 03:27:54 +00:00
|
|
|
static bool keep_cutbuffer = FALSE;
|
2004-05-29 15:36:58 +00:00
|
|
|
/* Should we keep the contents of the cutbuffer? */
|
2005-11-09 00:08:29 +00:00
|
|
|
/* Pointer to the end of the cutbuffer. */
|
2004-05-29 15:36:58 +00:00
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Indicate that we should no longer keep the contents of the
|
|
|
|
* cutbuffer. */
|
2004-05-29 15:36:58 +00:00
|
|
|
void cutbuffer_reset(void)
|
|
|
|
{
|
|
|
|
keep_cutbuffer = FALSE;
|
|
|
|
}
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2006-04-24 19:44:04 +00:00
|
|
|
/* If we aren't on the last line of the file, move all the text of the
|
2006-05-21 20:03:43 +00:00
|
|
|
* current line, plus the newline at the end, into the cutbuffer. If we
|
|
|
|
* are, move all of the text of the current line into the cutbuffer. In
|
2005-11-09 02:23:54 +00:00
|
|
|
* both cases, set the current place we want to the beginning of the
|
|
|
|
* current line. */
|
2004-11-23 04:08:28 +00:00
|
|
|
void cut_line(void)
|
2002-07-19 01:08:59 +00:00
|
|
|
{
|
2005-11-08 23:59:29 +00:00
|
|
|
if (openfile->current != openfile->filebot)
|
2005-07-08 20:09:16 +00:00
|
|
|
move_to_filestruct(&cutbuffer, &cutbottom, openfile->current, 0,
|
|
|
|
openfile->current->next, 0);
|
2005-11-09 02:13:07 +00:00
|
|
|
else
|
2005-11-08 23:59:29 +00:00
|
|
|
move_to_filestruct(&cutbuffer, &cutbottom, openfile->current, 0,
|
2005-11-09 02:13:07 +00:00
|
|
|
openfile->current, strlen(openfile->current->data));
|
2005-11-09 00:23:12 +00:00
|
|
|
openfile->placewewant = 0;
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2006-05-21 20:03:43 +00:00
|
|
|
/* Move all currently marked text into the cutbuffer, and set the
|
|
|
|
* current place we want to where the text used to start. */
|
2004-11-23 04:08:28 +00:00
|
|
|
void cut_marked(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2004-11-23 04:08:28 +00:00
|
|
|
filestruct *top, *bot;
|
|
|
|
size_t top_x, bot_x;
|
2001-06-21 23:58:47 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
mark_order((const filestruct **)&top, &top_x,
|
2004-11-23 04:08:28 +00:00
|
|
|
(const filestruct **)&bot, &bot_x, NULL);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
move_to_filestruct(&cutbuffer, &cutbottom, top, top_x, bot, bot_x);
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
2004-11-23 04:08:28 +00:00
|
|
|
}
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2006-04-24 19:44:04 +00:00
|
|
|
/* If we aren't at the end of the current line, move all the text from
|
|
|
|
* the current cursor position to the end of the current line, not
|
2006-05-21 20:03:43 +00:00
|
|
|
* counting the newline at the end, into the cutbuffer. If we are, and
|
2006-04-24 19:44:04 +00:00
|
|
|
* we're not on the last line of the file, move the newline at the end
|
2006-05-21 20:03:43 +00:00
|
|
|
* into the cutbuffer, and set the current place we want to where the
|
2005-11-03 21:08:39 +00:00
|
|
|
* newline used to be. */
|
2004-11-23 04:08:28 +00:00
|
|
|
void cut_to_eol(void)
|
|
|
|
{
|
2005-07-08 20:09:16 +00:00
|
|
|
size_t data_len = strlen(openfile->current->data);
|
2004-11-23 04:08:28 +00:00
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
assert(openfile->current_x <= data_len);
|
2004-11-23 04:08:28 +00:00
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
if (openfile->current_x < data_len)
|
2004-11-23 04:08:28 +00:00
|
|
|
/* If we're not at the end of the line, move all the text from
|
|
|
|
* the current position up to it, not counting the newline at
|
2006-05-21 20:03:43 +00:00
|
|
|
* the end, into the cutbuffer. */
|
2005-07-08 20:09:16 +00:00
|
|
|
move_to_filestruct(&cutbuffer, &cutbottom, openfile->current,
|
|
|
|
openfile->current_x, openfile->current, data_len);
|
2005-11-03 21:08:39 +00:00
|
|
|
else if (openfile->current != openfile->filebot) {
|
|
|
|
/* If we're at the end of the line, and it isn't the last line
|
|
|
|
* of the file, move all the text from the current position up
|
2006-07-28 17:06:27 +00:00
|
|
|
* to the beginning of the next line, i.e. the newline at the
|
2006-05-21 20:03:43 +00:00
|
|
|
* end, into the cutbuffer. */
|
2005-07-08 20:09:16 +00:00
|
|
|
move_to_filestruct(&cutbuffer, &cutbottom, openfile->current,
|
|
|
|
openfile->current_x, openfile->current->next, 0);
|
|
|
|
openfile->placewewant = xplustabs();
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-21 20:03:43 +00:00
|
|
|
|
|
|
|
/* Move all the text from the current cursor position to the end of the
|
|
|
|
* file into the cutbuffer. */
|
|
|
|
void cut_to_eof(void)
|
|
|
|
{
|
|
|
|
move_to_filestruct(&cutbuffer, &cutbottom, openfile->current,
|
|
|
|
openfile->current_x, openfile->filebot,
|
|
|
|
strlen(openfile->filebot->data));
|
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2006-04-25 02:23:28 +00:00
|
|
|
/* Move text from the current filestruct into the cutbuffer. If
|
2006-05-21 20:03:43 +00:00
|
|
|
* copy_text is TRUE, copy the text back into the filestruct afterward.
|
|
|
|
* If cut_till_end is TRUE, move all text from the current cursor
|
|
|
|
* position to the end of the file into the cutbuffer. */
|
2006-04-25 02:23:28 +00:00
|
|
|
void do_cut_text(
|
|
|
|
#ifndef NANO_TINY
|
2008-08-01 03:50:20 +00:00
|
|
|
bool copy_text, bool cut_till_end, bool undoing
|
2006-04-25 02:23:28 +00:00
|
|
|
#else
|
|
|
|
void
|
|
|
|
#endif
|
|
|
|
)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2006-04-25 02:23:28 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
filestruct *cb_save = NULL;
|
|
|
|
/* The current end of the cutbuffer, before we add text to
|
|
|
|
* it. */
|
2006-05-11 01:53:33 +00:00
|
|
|
size_t cb_save_len = 0;
|
|
|
|
/* The length of the string at the current end of the cutbuffer,
|
|
|
|
* before we add text to it. */
|
2006-04-26 19:03:50 +00:00
|
|
|
bool old_no_newlines = ISSET(NO_NEWLINES);
|
2006-04-25 02:23:28 +00:00
|
|
|
#endif
|
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
assert(openfile->current != NULL && openfile->current->data != NULL);
|
2001-06-21 15:07:40 +00:00
|
|
|
|
2004-11-23 17:59:32 +00:00
|
|
|
/* If keep_cutbuffer is FALSE and the cutbuffer isn't empty, blow
|
|
|
|
* away the text in the cutbuffer. */
|
|
|
|
if (!keep_cutbuffer && cutbuffer != NULL) {
|
2000-06-06 05:53:49 +00:00
|
|
|
free_filestruct(cutbuffer);
|
|
|
|
cutbuffer = NULL;
|
|
|
|
#ifdef DEBUG
|
2003-08-05 19:31:12 +00:00
|
|
|
fprintf(stderr, "Blew away cutbuffer =)\n");
|
2000-06-06 05:53:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
2000-08-07 14:58:26 +00:00
|
|
|
|
2006-04-25 02:23:28 +00:00
|
|
|
#ifndef NANO_TINY
|
2006-04-26 19:03:50 +00:00
|
|
|
if (copy_text) {
|
|
|
|
if (cutbuffer != NULL) {
|
|
|
|
/* If the cutbuffer isn't empty, save where it currently
|
2007-01-01 05:15:32 +00:00
|
|
|
* ends. This is where we'll add the new text. */
|
2006-04-26 19:03:50 +00:00
|
|
|
cb_save = cutbottom;
|
2006-05-11 11:13:54 +00:00
|
|
|
cb_save_len = strlen(cutbottom->data);
|
2006-04-26 19:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set NO_NEWLINES to TRUE, so that we don't disturb the last
|
|
|
|
* line of the file when moving text to the cutbuffer. */
|
|
|
|
SET(NO_NEWLINES);
|
2006-04-25 02:23:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Set keep_cutbuffer to TRUE, so that the text we're going to move
|
|
|
|
* into the cutbuffer will be added to the text already in the
|
|
|
|
* cutbuffer instead of replacing it. */
|
2004-05-29 15:36:58 +00:00
|
|
|
keep_cutbuffer = TRUE;
|
2003-04-15 01:15:09 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2008-07-31 04:24:04 +00:00
|
|
|
|
2006-05-21 20:03:43 +00:00
|
|
|
if (cut_till_end) {
|
|
|
|
/* If cut_till_end is TRUE, move all text up to the end of the
|
|
|
|
* file into the cutbuffer. */
|
|
|
|
cut_to_eof();
|
|
|
|
} else if (openfile->mark_set) {
|
2006-04-25 02:23:28 +00:00
|
|
|
/* If the mark is on, move the marked text to the cutbuffer, and
|
2004-11-23 04:08:28 +00:00
|
|
|
* turn the mark off. */
|
|
|
|
cut_marked();
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->mark_set = FALSE;
|
2005-03-26 22:49:46 +00:00
|
|
|
} else if (ISSET(CUT_TO_END))
|
2007-01-01 05:15:32 +00:00
|
|
|
/* If the CUT_TO_END flag is set, move all text up to the end of
|
|
|
|
* the line into the cutbuffer. */
|
2004-11-23 04:08:28 +00:00
|
|
|
cut_to_eol();
|
2002-07-19 01:08:59 +00:00
|
|
|
else
|
2005-03-26 22:49:46 +00:00
|
|
|
#endif
|
2007-01-01 05:15:32 +00:00
|
|
|
/* Move the entire line into the cutbuffer. */
|
2004-11-23 04:08:28 +00:00
|
|
|
cut_line();
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2006-04-25 02:23:28 +00:00
|
|
|
#ifndef NANO_TINY
|
2006-04-25 02:56:54 +00:00
|
|
|
if (copy_text) {
|
2006-04-25 02:23:28 +00:00
|
|
|
/* Copy the text in the cutbuffer, starting at its saved end if
|
|
|
|
* there is one, back into the filestruct. This effectively
|
|
|
|
* uncuts the text we just cut without marking the file as
|
|
|
|
* modified. */
|
2006-05-11 01:53:33 +00:00
|
|
|
if (cutbuffer != NULL) {
|
|
|
|
if (cb_save != NULL) {
|
|
|
|
cb_save->data += cb_save_len;
|
|
|
|
copy_from_filestruct(cb_save, cutbottom);
|
|
|
|
cb_save->data -= cb_save_len;
|
|
|
|
} else
|
|
|
|
copy_from_filestruct(cutbuffer, cutbottom);
|
2006-04-26 19:03:50 +00:00
|
|
|
|
2006-10-18 20:27:52 +00:00
|
|
|
/* Set the current place we want to where the text from the
|
|
|
|
* cutbuffer ends. */
|
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
}
|
2006-10-16 19:44:13 +00:00
|
|
|
|
2006-04-26 19:03:50 +00:00
|
|
|
/* Set NO_NEWLINES back to what it was before, since we're done
|
|
|
|
* disturbing the text. */
|
|
|
|
if (!old_no_newlines)
|
|
|
|
UNSET(NO_NEWLINES);
|
2008-08-01 03:50:20 +00:00
|
|
|
} else if (!undoing)
|
2008-08-03 04:48:05 +00:00
|
|
|
update_undo(CUT);
|
2006-04-25 02:23:28 +00:00
|
|
|
#endif
|
|
|
|
/* Leave the text in the cutbuffer, and mark the file as
|
|
|
|
* modified. */
|
|
|
|
set_modified();
|
|
|
|
|
|
|
|
/* Update the screen. */
|
2009-02-16 21:04:00 +00:00
|
|
|
edit_refresh_needed = TRUE;
|
2004-11-23 04:08:28 +00:00
|
|
|
|
2009-02-16 23:06:09 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
reset_multis(openfile->current, FALSE);
|
|
|
|
#endif
|
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
#ifdef DEBUG
|
2005-07-08 20:09:16 +00:00
|
|
|
dump_filestruct(cutbuffer);
|
2003-04-15 01:15:09 +00:00
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|
|
|
|
|
2006-04-25 02:23:28 +00:00
|
|
|
/* Move text from the current filestruct into the cutbuffer. */
|
|
|
|
void do_cut_text_void(void)
|
|
|
|
{
|
2008-08-02 14:39:42 +00:00
|
|
|
#ifndef NANO_TINY
|
2008-08-03 04:48:05 +00:00
|
|
|
add_undo(CUT);
|
2008-08-02 14:39:42 +00:00
|
|
|
#endif
|
2006-04-25 02:40:58 +00:00
|
|
|
do_cut_text(
|
|
|
|
#ifndef NANO_TINY
|
2008-08-01 03:50:20 +00:00
|
|
|
FALSE, FALSE, FALSE
|
2006-04-25 02:40:58 +00:00
|
|
|
#endif
|
|
|
|
);
|
2006-04-25 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2006-04-25 02:23:28 +00:00
|
|
|
/* Move text from the current filestruct into the cutbuffer, and copy it
|
|
|
|
* back into the filestruct afterward. */
|
|
|
|
void do_copy_text(void)
|
|
|
|
{
|
2008-08-01 03:50:20 +00:00
|
|
|
do_cut_text(TRUE, FALSE, FALSE);
|
2006-04-25 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
2005-01-01 07:43:32 +00:00
|
|
|
/* Cut from the current cursor position to the end of the file. */
|
|
|
|
void do_cut_till_end(void)
|
|
|
|
{
|
2008-08-02 14:39:42 +00:00
|
|
|
#ifndef NANO_TINY
|
2008-10-14 01:14:12 +00:00
|
|
|
add_undo(CUT);
|
2008-08-02 14:39:42 +00:00
|
|
|
#endif
|
2008-08-01 03:50:20 +00:00
|
|
|
do_cut_text(FALSE, TRUE, FALSE);
|
2005-01-01 07:43:32 +00:00
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2005-01-01 07:43:32 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Copy text from the cutbuffer into the current filestruct. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_uncut_text(void)
|
2000-06-06 05:53:49 +00:00
|
|
|
{
|
2005-07-08 20:09:16 +00:00
|
|
|
assert(openfile->current != NULL && openfile->current->data != NULL);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* If the cutbuffer is empty, get out. */
|
|
|
|
if (cutbuffer == NULL)
|
2004-07-02 14:31:03 +00:00
|
|
|
return;
|
2004-11-06 20:33:43 +00:00
|
|
|
|
2008-09-16 21:35:19 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
update_undo(UNCUT);
|
|
|
|
#endif
|
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Add a copy of the text in the cutbuffer to the current filestruct
|
2004-11-24 20:12:57 +00:00
|
|
|
* at the current cursor position. */
|
2004-11-23 04:08:28 +00:00
|
|
|
copy_from_filestruct(cutbuffer, cutbottom);
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Set the current place we want to where the text from the
|
|
|
|
* cutbuffer ends. */
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2006-06-18 15:26:51 +00:00
|
|
|
/* Mark the file as modified. */
|
2004-11-23 04:08:28 +00:00
|
|
|
set_modified();
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2006-06-18 15:26:51 +00:00
|
|
|
/* Update the screen. */
|
2009-02-16 21:04:00 +00:00
|
|
|
edit_refresh_needed = TRUE;
|
2006-06-18 15:26:51 +00:00
|
|
|
|
2009-02-16 23:06:09 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
reset_multis(openfile->current, FALSE);
|
|
|
|
#endif
|
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifdef DEBUG
|
2005-07-08 20:09:16 +00:00
|
|
|
dump_filestruct_reverse();
|
2002-07-19 01:08:59 +00:00
|
|
|
#endif
|
2000-06-06 05:53:49 +00:00
|
|
|
}
|