cut.c 13.2 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
/**************************************************************************
 *   cut.c                                                                *
 *                                                                        *
5
 *   Copyright (C) 1999-2004 Chris Allegretta                             *
Chris Allegretta's avatar
Chris Allegretta committed
6
7
 *   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 *
8
 *   the Free Software Foundation; either version 2, or (at your option)  *
Chris Allegretta's avatar
Chris Allegretta committed
9
10
11
12
13
14
15
16
17
18
19
20
21
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.            *
 *                                                                        *
 **************************************************************************/

22
23
#include "config.h"

Chris Allegretta's avatar
Chris Allegretta committed
24
25
26
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Chris Allegretta's avatar
Chris Allegretta committed
27
#include <assert.h>
Chris Allegretta's avatar
Chris Allegretta committed
28
29
30
#include "proto.h"
#include "nano.h"

31
static bool keep_cutbuffer = FALSE;
32
33
34
35
	/* Should we keep the contents of the cutbuffer? */
static int marked_cut;
	/* Is the cutbuffer from a mark?  0 means whole-line cut, 1
	 * means mark, and 2 means cut-from-cursor. */
Chris Allegretta's avatar
Chris Allegretta committed
36
#ifndef NANO_SMALL
37
static bool concatenate_cut;
38
	/* Should we add this cut string to the end of the last one? */
Chris Allegretta's avatar
Chris Allegretta committed
39
#endif
40
static filestruct *cutbottom = NULL;
41
42
43
44
45
46
	/* Pointer to end of cutbuffer. */

void cutbuffer_reset(void)
{
    keep_cutbuffer = FALSE;
}
Chris Allegretta's avatar
Chris Allegretta committed
47

Chris Allegretta's avatar
Chris Allegretta committed
48
49
50
51
52
filestruct *get_cutbottom(void)
{
    return cutbottom;
}

53
void add_to_cutbuffer(filestruct *inptr, bool allow_concat)
Chris Allegretta's avatar
Chris Allegretta committed
54
55
{
#ifdef DEBUG
56
    fprintf(stderr, "add_to_cutbuffer(): inptr->data = %s\n", inptr->data);
Chris Allegretta's avatar
Chris Allegretta committed
57
58
#endif

59
    if (cutbuffer == NULL)
Chris Allegretta's avatar
Chris Allegretta committed
60
	cutbuffer = inptr;
Chris Allegretta's avatar
Chris Allegretta committed
61
#ifndef NANO_SMALL
62
    else if (allow_concat && concatenate_cut) {
Chris Allegretta's avatar
Chris Allegretta committed
63
	/* Just tack the text in inptr onto the text in cutbottom,
64
	 * unless allow_concat is FALSE. */
Chris Allegretta's avatar
Chris Allegretta committed
65
66
67
68
	cutbottom->data = charealloc(cutbottom->data,
		strlen(cutbottom->data) + strlen(inptr->data) + 1);
	strcat(cutbottom->data, inptr->data);
	return;
69
    }
Chris Allegretta's avatar
Chris Allegretta committed
70
#endif
71
    else {
Chris Allegretta's avatar
Chris Allegretta committed
72
73
74
75
	cutbottom->next = inptr;
	inptr->prev = cutbottom;
    }
    cutbottom = inptr;
76
    cutbottom->next = NULL;
Chris Allegretta's avatar
Chris Allegretta committed
77
78
79
}

#ifndef NANO_SMALL
Chris Allegretta's avatar
Chris Allegretta committed
80
/* Cut a marked segment instead of a whole line.
81
 *
Chris Allegretta's avatar
Chris Allegretta committed
82
83
84
85
 * The first cut character is top->data[top_x].  Unless top == bot, the
 * last cut line has length bot_x.  That is, if bot_x > 0 then we cut to
 * bot->data[bot_x - 1].
 *
86
87
88
89
 * We maintain totsize, totlines, filebot, the magicline, and line
 * numbers.  Also, we set current and current_x so the cursor will be on
 * the first character after what was cut.  We do not do any screen
 * updates.
Chris Allegretta's avatar
Chris Allegretta committed
90
 *
91
 * Note cutbuffer might not be NULL if cut to end is used. */
92
void cut_marked_segment(void)
Chris Allegretta's avatar
Chris Allegretta committed
93
{
94
95
96
97
98
    filestruct *top;
    filestruct *bot;
    filestruct *tmp;
    size_t top_x;
    size_t bot_x;
Chris Allegretta's avatar
Chris Allegretta committed
99
    size_t newsize;
100

101
102
    /* If the mark doesn't cover any text, get out. */
    if (current == mark_beginbuf && current_x == mark_beginx)
Chris Allegretta's avatar
Chris Allegretta committed
103
	return;
104
    assert(current != NULL && mark_beginbuf != NULL);
105

106
107
108
109
    /* Set up the top and bottom lines and coordinates of the marked
     * text. */
    mark_order((const filestruct **)&top, &top_x,
		(const filestruct **)&bot, &bot_x);
110

111
112
    /* Make the first cut line manually.  Move the cut part of the top
     * line into tmp, and set newsize to that partial line's length. */
Chris Allegretta's avatar
Chris Allegretta committed
113
    tmp = copy_node(top);
Chris Allegretta's avatar
Chris Allegretta committed
114
    newsize = (top == bot ? bot_x - top_x : strlen(top->data + top_x));
115
    charmove(tmp->data, tmp->data + top_x, newsize);
Chris Allegretta's avatar
Chris Allegretta committed
116
117
    null_at(&tmp->data, newsize);

118
119
120
121
122
123
124
125
126
    /* Add the contents of tmp to the cutbuffer.  Note that cutbuffer
     * might be non-NULL if we have cut to end enabled. */
    if (cutbuffer == NULL) {
	cutbuffer = tmp;
	cutbottom = tmp;
    } else {
	cutbottom->next = tmp;
	tmp->prev = cutbottom;
	cutbottom = tmp;
Chris Allegretta's avatar
Chris Allegretta committed
127
128
    }

129
130
131
132
133
134
135
136
137
138
139
    /* And make the top remainder line manually too.  Update current_x
     * and totlines to account for all the cut text, and update totsize
     * to account for the length of the cut part of the first line. */
    current_x = top_x;
    totsize -= newsize;
    totlines -= bot->lineno - top->lineno;

    /* Now set newsize to be the length of the top remainder line plus
     * the bottom remainder line, plus one for the null terminator. */
    newsize = top_x + strlen(bot->data + bot_x) + 1;

Chris Allegretta's avatar
Chris Allegretta committed
140
    if (top == bot) {
141
142
143
144
145
146
147
	/* In this case, we're only cutting one line or part of one
	 * line, so the remainder line is shorter.  This means that we
	 * must move text from the end forward first. */
	charmove(top->data + top_x, bot->data + bot_x, newsize - top_x);
	top->data = charealloc(top->data, newsize);

	cutbottom->next = NULL;
Chris Allegretta's avatar
Chris Allegretta committed
148
149
150
#ifdef DEBUG
	dump_buffer(cutbuffer);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
151
	return;
152
    }
153

154
155
    /* Update totsize to account for the cut part of the last line. */
    totsize -= bot_x + 1;
156

157
158
159
160
161
    /* Here, the top remainder line might get longer (if the bottom
     * remainder line is added to the end of it), so we realloc() it
     * first. */
    top->data = charealloc(top->data, newsize);
    charmove(top->data + top_x, bot->data + bot_x, newsize - top_x);
Chris Allegretta's avatar
Chris Allegretta committed
162

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    assert(cutbottom != NULL && cutbottom->next != NULL);
    /* We're cutting multiple lines, so in particular the next line is
     * cut too. */
    cutbottom->next->prev = cutbottom;

    /* Update totsize to account for all the complete lines that have
     * been cut.  After this, totsize is fully up to date. */
    for (tmp = top->next; tmp != bot; tmp = tmp->next)
	totsize -= strlen(tmp->data) + 1;

    /* Make the last cut line manually. */
    null_at(&bot->data, bot_x);

    /* Move the rest of the cut text (other than the cut part of the top
     * line) from the buffer to the end of the cutbuffer, and fix the
     * edit buffer to account for the cut text. */
    top->next = bot->next;
    cutbottom = bot;
    cutbottom->next = NULL;
    if (top->next != NULL)
	top->next->prev = top;
    renumber(top);
    current = top;

    /* If the bottom line of the cut was the magicline, set filebot
     * properly, and add a new magicline if the top remainder line
     * (which is now the new bottom line) is non-blank. */
    if (bot == filebot) {
	filebot = top;
	assert(bot_x == 0);
	if (top_x > 0)
	    new_magicline();
Chris Allegretta's avatar
Chris Allegretta committed
195
    }
Chris Allegretta's avatar
Chris Allegretta committed
196
197
198
#ifdef DEBUG
    dump_buffer(cutbuffer);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
199
200
201
}
#endif

202
void do_cut_text(void)
Chris Allegretta's avatar
Chris Allegretta committed
203
{
Chris Allegretta's avatar
Chris Allegretta committed
204
    filestruct *fileptr;
Chris Allegretta's avatar
Chris Allegretta committed
205

Chris Allegretta's avatar
Chris Allegretta committed
206
    assert(current != NULL && current->data != NULL);
207

208
    check_statblank();
Chris Allegretta's avatar
Chris Allegretta committed
209

210
    if (!keep_cutbuffer) {
Chris Allegretta's avatar
Chris Allegretta committed
211
212
	free_filestruct(cutbuffer);
	cutbuffer = NULL;
213
	marked_cut = 0;
Chris Allegretta's avatar
Chris Allegretta committed
214
#ifndef NANO_SMALL
215
	concatenate_cut = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
216
#endif
Chris Allegretta's avatar
Chris Allegretta committed
217
#ifdef DEBUG
218
	fprintf(stderr, "Blew away cutbuffer =)\n");
Chris Allegretta's avatar
Chris Allegretta committed
219
220
#endif
    }
221

222
    /* You can't cut the magicline except with the mark.  But trying
223
     * does clear the cutbuffer if keep_cutbuffer is FALSE. */
Chris Allegretta's avatar
Chris Allegretta committed
224
225
226
227
228
    if (current == filebot
#ifndef NANO_SMALL
			&& !ISSET(MARK_ISSET)
#endif
						)
229
	return;
230

231
    keep_cutbuffer = TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
232

Chris Allegretta's avatar
Chris Allegretta committed
233
#ifndef NANO_SMALL
Chris Allegretta's avatar
Chris Allegretta committed
234
    if (ISSET(CUT_TO_END) && !ISSET(MARK_ISSET)) {
Chris Allegretta's avatar
Chris Allegretta committed
235
	assert(current_x >= 0 && current_x <= strlen(current->data));
236

Chris Allegretta's avatar
Chris Allegretta committed
237
238
	if (current->data[current_x] == '\0') {
	    /* If the line is empty and we didn't just cut a non-blank
239
240
	     * line, create a dummy blank line and add it to the
	     * cutbuffer. */
241
	    if (marked_cut != 1 && current->next != filebot) {
Chris Allegretta's avatar
Chris Allegretta committed
242
		filestruct *junk = make_new_node(current);
243

244
		junk->data = charalloc(1);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
245
		junk->data[0] = '\0';
246
		add_to_cutbuffer(junk, TRUE);
Chris Allegretta's avatar
Chris Allegretta committed
247
#ifdef DEBUG
248
		dump_buffer(cutbuffer);
Chris Allegretta's avatar
Chris Allegretta committed
249
#endif
250
	    }
251

252
	    do_delete();
Chris Allegretta's avatar
Chris Allegretta committed
253
	    marked_cut = 2;
254
	    return;
255
	} else {
256
257
258
259
260
	    SET(MARK_ISSET);

	    mark_beginx = strlen(current->data);
	    mark_beginbuf = current;
	}
261
    }
262

Chris Allegretta's avatar
Chris Allegretta committed
263
    if (ISSET(MARK_ISSET)) {
264
	cut_marked_segment();
Chris Allegretta's avatar
Chris Allegretta committed
265
266
267

	placewewant = xplustabs();
	UNSET(MARK_ISSET);
268

Chris Allegretta's avatar
Chris Allegretta committed
269
	/* If we just did a marked cut of part of a line, we should add
270
271
	 * the first line of any cut done immediately afterward to the
	 * end of this cut, as Pico does. */
Chris Allegretta's avatar
Chris Allegretta committed
272
	if (current == mark_beginbuf && current_x < strlen(current->data))
273
	    concatenate_cut = TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
274
	marked_cut = 1;
275
	edit_refresh();
276
	set_modified();
277
	return;
Chris Allegretta's avatar
Chris Allegretta committed
278
    }
Chris Allegretta's avatar
Chris Allegretta committed
279
280
281
282
283
284
285
#endif /* !NANO_SMALL */

    totlines--;
    totsize -= strlen(current->data) + 1;
    fileptr = current;
    current = current->next;
    current->prev = fileptr->prev;
286
    add_to_cutbuffer(fileptr, TRUE);
Chris Allegretta's avatar
Chris Allegretta committed
287
288
289
#ifdef DEBUG
    dump_buffer(cutbuffer);
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
290

Chris Allegretta's avatar
Chris Allegretta committed
291
292
293
294
    if (fileptr == fileage)
	fileage = current;
    else
	current->prev->next = current;
Chris Allegretta's avatar
Chris Allegretta committed
295
296
297
298

    if (fileptr == edittop)
	edittop = current;

Chris Allegretta's avatar
Chris Allegretta committed
299
    renumber(current);
300
    current_x = 0;
Chris Allegretta's avatar
Chris Allegretta committed
301
302
303
    edit_refresh();
    set_modified();
    marked_cut = 0;
Chris Allegretta's avatar
Chris Allegretta committed
304
#ifndef NANO_SMALL
305
    concatenate_cut = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
306
#endif
Chris Allegretta's avatar
Chris Allegretta committed
307
308
}

309
void do_uncut_text(void)
Chris Allegretta's avatar
Chris Allegretta committed
310
{
311
312
313
    filestruct *tmp = current;
    filestruct *newbuf = NULL;
    filestruct *newend = NULL;
Chris Allegretta's avatar
Chris Allegretta committed
314

315
#ifndef DISABLE_WRAPPING
Chris Allegretta's avatar
Chris Allegretta committed
316
    wrap_reset();
317
#endif
318
    check_statblank();
319
    if (cutbuffer == NULL || current == NULL)
320
	return;			/* AIEEEEEEEEEEEE */
Chris Allegretta's avatar
Chris Allegretta committed
321

Chris Allegretta's avatar
Chris Allegretta committed
322
    /* If we're uncutting a previously non-marked block, uncut to end if
323
324
325
     * we're not at the beginning of the line.  If we are at the
     * beginning of the line, set placewewant to 0.  Pico does both of
     * these. */
Chris Allegretta's avatar
Chris Allegretta committed
326
327
328
329
330
331
332
    if (marked_cut == 0) {
	if (current_x != 0)
	    marked_cut = 2;
	else
	    placewewant = 0;
    }

Chris Allegretta's avatar
Chris Allegretta committed
333
    /* If we're going to uncut on the magicline, always make a new
334
     * magicline in advance, as Pico does. */
Chris Allegretta's avatar
Chris Allegretta committed
335
336
337
    if (current->next == NULL)
	new_magicline();

338
    if (marked_cut == 0 || cutbuffer->next != NULL) {
Chris Allegretta's avatar
Chris Allegretta committed
339
340
341
342
	newbuf = copy_filestruct(cutbuffer);
	for (newend = newbuf; newend->next != NULL && newend != NULL;
		newend = newend->next)
	    totlines++;
Chris Allegretta's avatar
Chris Allegretta committed
343
344
    }

345
    /* Hook newbuf in at current. */
346
    if (marked_cut != 0) {
347
	filestruct *hold = current;
Chris Allegretta's avatar
Chris Allegretta committed
348

349
	/* If there's only one line in the cutbuffer... */
Chris Allegretta's avatar
Chris Allegretta committed
350
	if (cutbuffer->next == NULL) {
Chris Allegretta's avatar
Chris Allegretta committed
351
352
353
	    size_t buf_len = strlen(cutbuffer->data);
	    size_t cur_len = strlen(current->data);

David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
354
	    current->data = charealloc(current->data, cur_len + buf_len + 1);
355
	    charmove(current->data + current_x + buf_len,
Chris Allegretta's avatar
Chris Allegretta committed
356
357
			current->data + current_x, cur_len - current_x + 1);
	    strncpy(current->data + current_x, cutbuffer->data, buf_len);
358
		/* Use strncpy() to not copy the null terminator. */
Chris Allegretta's avatar
Chris Allegretta committed
359
360
361

	    current_x += buf_len;
	    totsize += buf_len;
Chris Allegretta's avatar
Chris Allegretta committed
362
363

	    placewewant = xplustabs();
364
365
366
	} else {		/* Yuck -- no kidding! */
	    char *tmpstr, *tmpstr2;

Chris Allegretta's avatar
Chris Allegretta committed
367
	    tmp = current->next;
368
369

	    /* New beginning. */
370
	    tmpstr = charalloc(current_x + strlen(newbuf->data) + 1);
Chris Allegretta's avatar
Chris Allegretta committed
371
372
373
374
	    strncpy(tmpstr, current->data, current_x);
	    strcpy(&tmpstr[current_x], newbuf->data);
	    totsize += strlen(newbuf->data) + strlen(newend->data) + 1;

375
	    /* New end. */
376
	    tmpstr2 = charalloc(strlen(newend->data) +
Chris Allegretta's avatar
Chris Allegretta committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
			      strlen(&current->data[current_x]) + 1);
	    strcpy(tmpstr2, newend->data);
	    strcat(tmpstr2, &current->data[current_x]);

	    free(current->data);
	    current->data = tmpstr;
	    current->next = newbuf->next;
	    newbuf->next->prev = current;
	    delete_node(newbuf);

	    current_x = strlen(newend->data);
	    placewewant = xplustabs();
	    free(newend->data);
	    newend->data = tmpstr2;

	    newend->next = tmp;

394
395
396
	    /* If tmp isn't NULL, we're in the middle: update the
	     * prev pointer.  If it IS NULL, we're at the end; update
	     * the filebot pointer. */
Chris Allegretta's avatar
Chris Allegretta committed
397
398
	    if (tmp != NULL)
		tmp->prev = newend;
Chris Allegretta's avatar
Chris Allegretta committed
399
	    else {
Chris Allegretta's avatar
Chris Allegretta committed
400
		filebot = newend;
401
		new_magicline();
Chris Allegretta's avatar
Chris Allegretta committed
402
	    }
Chris Allegretta's avatar
Chris Allegretta committed
403

404
	    /* Now why don't we update the totsize also? */
Chris Allegretta's avatar
Chris Allegretta committed
405
406
407
408
409
410
	    for (tmp = current->next; tmp != newend; tmp = tmp->next)
		totsize += strlen(tmp->data) + 1;

	    current = newend;
	}

411
	/* If marked cut == 2, it means that we're doing a cut to end
412
	 * and we don't want anything else on the line, so we have to
413
	 * screw up all the work we just did and separate the line. */
414
	if (marked_cut == 2) {
415
	    tmp = make_new_node(current);
416
	    tmp->data = mallocstrcpy(NULL, current->data + current_x);
417
	    splice_node(current, tmp, current->next);
418
	    null_at(&current->data, current_x);
419
420
421
	    current = current->next;
	    current_x = 0;
	    placewewant = 0;
422

423
	    /* Extra line added; update stuff. */
424
425
	    totlines++;
	    totsize++;
426
	}
Chris Allegretta's avatar
Chris Allegretta committed
427
428
429
	/* Renumber from BEFORE where we pasted ;) */
	renumber(hold);

Chris Allegretta's avatar
Chris Allegretta committed
430
#ifdef DEBUG
Chris Allegretta's avatar
Chris Allegretta committed
431
432
	dump_buffer(fileage);
	dump_buffer(cutbuffer);
Chris Allegretta's avatar
Chris Allegretta committed
433
#endif
Chris Allegretta's avatar
Chris Allegretta committed
434
	set_modified();
435
	edit_refresh();
436
	return;
Chris Allegretta's avatar
Chris Allegretta committed
437
438
    }

439
440
    if (current != fileage) {
	tmp = current->prev;
Chris Allegretta's avatar
Chris Allegretta committed
441
442
	tmp->next = newbuf;
	newbuf->prev = tmp;
Chris Allegretta's avatar
Chris Allegretta committed
443
    } else
Chris Allegretta's avatar
Chris Allegretta committed
444
	fileage = newbuf;
445
    totlines++;		/* Unmarked uncuts don't split lines. */
Chris Allegretta's avatar
Chris Allegretta committed
446
447
448
449
450

    /* This is so uncutting at the top of the buffer will work => */
    if (current_y == 0)
	edittop = newbuf;

451
452
453
    /* Connect the end of the buffer to the filestruct. */
    newend->next = current;
    current->prev = newend;
Chris Allegretta's avatar
Chris Allegretta committed
454

Chris Allegretta's avatar
Chris Allegretta committed
455
    /* Recalculate size *sigh* */
456
    for (tmp = newbuf; tmp != current; tmp = tmp->next)
Chris Allegretta's avatar
Chris Allegretta committed
457
458
459
	totsize += strlen(tmp->data) + 1;

    renumber(newbuf);
460
    edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
461

Chris Allegretta's avatar
Chris Allegretta committed
462
463
464
#ifdef DEBUG
    dump_buffer_reverse();
#endif
Chris Allegretta's avatar
Chris Allegretta committed
465
466
467

    set_modified();
}