2000-08-06 21:13:45 +00:00
|
|
|
/* $Id$ */
|
2000-06-06 05:53:49 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* cut.c *
|
|
|
|
* *
|
2005-01-01 07:43:32 +00:00
|
|
|
* Copyright (C) 1999-2005 Chris Allegretta *
|
2000-06-06 05:53:49 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
2001-10-24 11:33:54 +00:00
|
|
|
* the Free Software Foundation; either version 2, or (at your option) *
|
2000-06-06 05:53:49 +00:00
|
|
|
* any later version. *
|
|
|
|
* *
|
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
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2004-11-17 23:17:05 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2001-04-28 18:03:52 +00:00
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "proto.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? */
|
2002-12-22 16:30:00 +00:00
|
|
|
static filestruct *cutbottom = NULL;
|
2005-11-09 00:08:29 +00:00
|
|
|
/* Pointer to the end 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
|
|
|
|
2005-11-03 21:08:39 +00:00
|
|
|
/* If we're not on the last line of the file, move all the text of the
|
2005-11-09 00:25:30 +00:00
|
|
|
* current line, plus the newline at the end, to the cutbuffer. If we
|
2005-11-09 02:23:54 +00:00
|
|
|
* are, move all of the text of the current line to the cutbuffer. In
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NANO_SMALL
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Move all currently marked text to the cutbuffer, and set the current
|
|
|
|
* place we want to where the text used to start. */
|
|
|
|
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
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* If we're not at the end of the current line, move all the text from
|
|
|
|
* the current cursor position to the end of the current line,
|
|
|
|
* not counting the newline at the end, to the cutbuffer. If we are,
|
2005-11-03 21:08:39 +00:00
|
|
|
* and we're not on the last line of the file, move the newline at the
|
|
|
|
* end to the cutbuffer, and set the current place we want to where the
|
|
|
|
* 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
|
|
|
|
* the end, to 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
|
|
|
|
* to the beginning of the next line, i.e, the newline at the
|
|
|
|
* end, to 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
|
|
|
}
|
|
|
|
}
|
2005-03-26 22:49:46 +00:00
|
|
|
#endif /* !NANO_SMALL */
|
2000-06-06 05:53:49 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Move text from the current filestruct into the cutbuffer. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_cut_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);
|
2001-06-21 15:07:40 +00:00
|
|
|
|
2004-09-30 22:07:21 +00:00
|
|
|
check_statusblank();
|
2000-06-06 05:53:49 +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
|
|
|
|
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
|
|
|
|
2000-06-06 05:53:49 +00:00
|
|
|
#ifndef NANO_SMALL
|
2005-07-08 20:09:16 +00:00
|
|
|
if (openfile->mark_set) {
|
2004-11-23 04:08:28 +00:00
|
|
|
/* If the mark is on, move the marked text to the cutbuffer and
|
|
|
|
* 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))
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Otherwise, if the CUT_TO_END flag is set, move all text up to
|
|
|
|
* the end of the line into the cutbuffer. */
|
|
|
|
cut_to_eol();
|
2002-07-19 01:08:59 +00:00
|
|
|
else
|
2005-03-26 22:49:46 +00:00
|
|
|
#endif
|
2004-11-23 04:08:28 +00:00
|
|
|
/* Otherwise, move the entire line into the cutbuffer. */
|
|
|
|
cut_line();
|
2000-06-06 05:53:49 +00:00
|
|
|
|
|
|
|
edit_refresh();
|
|
|
|
set_modified();
|
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
|
|
|
}
|
|
|
|
|
2005-01-01 07:43:32 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
/* Cut from the current cursor position to the end of the file. */
|
|
|
|
void do_cut_till_end(void)
|
|
|
|
{
|
2005-07-08 20:09:16 +00:00
|
|
|
assert(openfile->current != NULL && openfile->current->data != NULL);
|
2005-01-01 07:43:32 +00:00
|
|
|
|
|
|
|
check_statusblank();
|
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
move_to_filestruct(&cutbuffer, &cutbottom, openfile->current,
|
2005-11-03 21:08:39 +00:00
|
|
|
openfile->current_x, openfile->filebot,
|
|
|
|
strlen(openfile->filebot->data));
|
2005-01-01 07:43:32 +00:00
|
|
|
|
|
|
|
edit_refresh();
|
|
|
|
set_modified();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2005-07-08 20:09:16 +00:00
|
|
|
dump_filestruct(cutbuffer);
|
2005-01-01 07:43:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
2005-03-26 22:49:46 +00:00
|
|
|
#endif /* !NANO_SMALL */
|
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
|
|
|
|
2003-09-10 20:08:00 +00:00
|
|
|
#ifndef DISABLE_WRAPPING
|
2000-06-06 05:53:49 +00:00
|
|
|
wrap_reset();
|
2003-09-10 20:08:00 +00:00
|
|
|
#endif
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2004-11-23 04:08:28 +00:00
|
|
|
check_statusblank();
|
2000-12-10 05:44:02 +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
|
|
|
|
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
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
edit_refresh();
|
2004-11-23 04:08:28 +00:00
|
|
|
set_modified();
|
2000-06-06 05:53:49 +00:00
|
|
|
|
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
|
|
|
}
|