cut.c 13.1 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-2002 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
27
28
29
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "proto.h"
#include "nano.h"

30
#ifdef ENABLE_NLS
Chris Allegretta's avatar
Chris Allegretta committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif

static int marked_cut;		/* Is the cutbuffer from a mark */
static filestruct *cutbottom = NULL;	/* Pointer to end of cutbuffer */

void add_to_cutbuffer(filestruct * inptr)
{
#ifdef DEBUG
    fprintf(stderr, _("add_to_cutbuffer called with inptr->data = %s\n"),
	    inptr->data);
#endif

47
    totsize -= strlen(inptr->data);
Chris Allegretta's avatar
Chris Allegretta committed
48
49
50
51
52
53
54
55
56
57
58
59
60
    if (cutbuffer == NULL) {
	cutbuffer = inptr;
	inptr->prev = NULL;
    } else {
	cutbottom->next = inptr;
	inptr->prev = cutbottom;
    }

    inptr->next = NULL;
    cutbottom = inptr;
}

#ifndef NANO_SMALL
61
62
/* Cut a marked segment instead of a whole line.  Only called from
   do_cut_text().
63
64
65
   destructive is whether to actually modify the file structure, if not then
   just copy the buffer into cutbuffer and don't pull it from the file */

Chris Allegretta's avatar
Chris Allegretta committed
66
void cut_marked_segment(filestruct * top, int top_x, filestruct * bot,
67
			int bot_x, int destructive)
Chris Allegretta's avatar
Chris Allegretta committed
68
{
69
    filestruct *tmp, *next, *botcopy;
Chris Allegretta's avatar
Chris Allegretta committed
70
    char *tmpstr;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    int newsize;

    /* Special case for cutting part of one line */
    if (top == bot) {
        int swap;

	tmp = copy_node(top);
	newsize = abs(bot_x - top_x) + 1;
	tmpstr = charalloc(newsize + 1);

	/* Make top_x always be before bot_x */
	if (top_x > bot_x) {
	    swap = top_x;
	    top_x = bot_x;
	    bot_x = swap;
	}

	strncpy(tmpstr, &top->data[top_x], newsize);

	if (destructive) {
	    memmove(&top->data[top_x], &top->data[bot_x],
		strlen(&top->data[bot_x]) + 1);
	    align(&top->data);
	    current_x = top_x;
	    update_cursor();
	}
	tmpstr[newsize - 1] = 0;
	tmp->data = tmpstr;
	add_to_cutbuffer(tmp);
	dump_buffer(cutbuffer);

	return;
    }
Chris Allegretta's avatar
Chris Allegretta committed
104
105
106

    /* Set up the beginning of the cutbuffer */
    tmp = copy_node(top);
107
    tmpstr = charalloc(strlen(&top->data[top_x]) + 1);
Chris Allegretta's avatar
Chris Allegretta committed
108
109
110
111
112
    strcpy(tmpstr, &top->data[top_x]);
    free(tmp->data);
    tmp->data = tmpstr;

    /* Chop off the end of the first line */
113
    tmpstr = charalloc(top_x + 1);
Chris Allegretta's avatar
Chris Allegretta committed
114
    strncpy(tmpstr, top->data, top_x);
115
116
117
118
119

    if (destructive) {
	free(top->data);
	top->data = tmpstr;
    }
Chris Allegretta's avatar
Chris Allegretta committed
120
121
122

    do {
	next = tmp->next;
123
124
125
126
127
128
129
130
	if (destructive)
	    add_to_cutbuffer(tmp);
	else {
	    filestruct *tmpcopy = NULL;
	    
	    tmpcopy = copy_node(tmp);
	    add_to_cutbuffer(tmpcopy);
	}
Chris Allegretta's avatar
Chris Allegretta committed
131
	totlines--;
Chris Allegretta's avatar
Chris Allegretta committed
132
	totsize--;		/* newline (add_to_cutbuffer doesn't count newlines) */
Chris Allegretta's avatar
Chris Allegretta committed
133
134
135
136
137
138
139
140
	tmp = next;
    }
    while (next != bot && next != NULL);

    dump_buffer(cutbuffer);
    if (next == NULL)
	return;

141
142
    /* Now, paste bot[bot_x] into top[top_x] */
    if (destructive) {
Chris Allegretta's avatar
Chris Allegretta committed
143

144
	tmpstr = charalloc(top_x + strlen(&bot->data[bot_x]) + 1);
145
146
147
148
	strncpy(tmpstr, top->data, top_x);
	strcpy(&tmpstr[top_x], &bot->data[bot_x]);
	free(top->data);
	top->data = tmpstr;
Chris Allegretta's avatar
Chris Allegretta committed
149

150
151
152
153
	/* We explicitly don't decrement totlines here because we don't snarf
	 * up a newline when we're grabbing the last line of the mark.  For
 	 * the same reason, we don't do an extra totsize decrement. */
    }
154
155

    /* I honestly do not know why this is needed.  After many hours of
156
157
158
	using gdb on an OpenBSD box, I can honestly say something is 
 	screwed somewhere.  Not doing this causes update_line to annihilate
	the last line copied into the cutbuffer when the mark is set ?!?!? */
159
    botcopy = copy_node(bot);
160
    null_at(&botcopy->data, bot_x);
161
    next = botcopy->next;
162
163
    add_to_cutbuffer(botcopy);

Chris Allegretta's avatar
Chris Allegretta committed
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    if (destructive) {
	free(bot);

	top->next = next;
 	if (next != NULL)
	    next->prev = top;

	dump_buffer(cutbuffer);
	renumber(top);
	current = top;
 	current_x = top_x;

 	/* If we're hitting the end of the buffer, we should clean that up. */
	if (bot == filebot) {
	    if (next != NULL) {
		filebot = next;
	    } else {
		filebot = top;
183
184
		if (top_x > 0)
		    new_magicline();
185
	    }
Chris Allegretta's avatar
Chris Allegretta committed
186
	}
187
188
	if (top->lineno < edittop->lineno)
	    edit_update(top, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
189
    }
190

Chris Allegretta's avatar
Chris Allegretta committed
191
192
193
194
195
196
197
}
#endif

int do_cut_text(void)
{
    filestruct *tmp, *fileptr = current;
#ifndef NANO_SMALL
198
    int dontupdate = 0;
199
    int cuttingtoend = 0;
Chris Allegretta's avatar
Chris Allegretta committed
200
201
#endif

202

203
    check_statblank();
204
    if (fileptr == NULL || fileptr->data == NULL)
Chris Allegretta's avatar
Chris Allegretta committed
205
206
207
208
	return 0;

    tmp = fileptr->next;

209
    if (!ISSET(KEEP_CUTBUFFER)) {
Chris Allegretta's avatar
Chris Allegretta committed
210
211
	free_filestruct(cutbuffer);
	cutbuffer = NULL;
212

213
	marked_cut = 0;
Chris Allegretta's avatar
Chris Allegretta committed
214
215
216
217
#ifdef DEBUG
	fprintf(stderr, _("Blew away cutbuffer =)\n"));
#endif
    }
218
219

    /* Must let cutbuffer get blown away first before we do this... */
220
    if (fileptr == filebot && !ISSET(MARK_ISSET))
221
222
	return 0;

Chris Allegretta's avatar
Chris Allegretta committed
223
#ifndef NANO_SMALL
Chris Allegretta's avatar
Chris Allegretta committed
224
    if (ISSET(CUT_TO_END) && !ISSET(MARK_ISSET)) {
225
	if (current_x == strlen(current->data)) {
226

227
    	/* If the line is empty and we didn't just cut a non-blank
Chris Allegretta's avatar
Chris Allegretta committed
228
		line, create a dummy line and add it to the cutbuffer */
229
	    if (marked_cut != 1 && current->next != filebot) {
230

231
232
		filestruct *junk;

233
234
		junk = NULL;
		junk = make_new_node(current);
235
	        junk->data = charalloc(1);
236
		junk->data[0] = 0;
237

238
		add_to_cutbuffer(junk);
239
240
		dump_buffer(cutbuffer);

241
	    }
242

243
	    do_delete();
244
	    SET(KEEP_CUTBUFFER);
Chris Allegretta's avatar
Chris Allegretta committed
245
	    marked_cut = 2;
246
	    return 1;
247
	} else {
248
249
250
251
252
253
254
	    SET(MARK_ISSET);
	    SET(KEEP_CUTBUFFER);

	    mark_beginx = strlen(current->data);
	    mark_beginbuf = current;
	    cuttingtoend = 1;
	}
255
    }
256

Chris Allegretta's avatar
Chris Allegretta committed
257
    if (ISSET(MARK_ISSET)) {
258
	if (current->lineno <= mark_beginbuf->lineno) {
259
260
261
262
263
264
265
	    /* Don't do_update and move the screen position if the marked
		area lies entirely within the screen buffer */
	    if (current->lineno == mark_beginbuf->lineno
		|| (current->lineno >= edittop->lineno
		&& mark_beginbuf->lineno <= editbot->lineno))
		dontupdate = 1;

Chris Allegretta's avatar
Chris Allegretta committed
266
	    cut_marked_segment(current, current_x, mark_beginbuf,
267
			       mark_beginx, 1);
268
	}
269
270
271
272
273
274
275
	else {
	    /* Same as above, easier logic since we know it's a multi-line
		cut and mark_beginbuf is before current */
	    if (mark_beginbuf->lineno >= edittop->lineno
		&& current->lineno <= editbot->lineno)
		dontupdate = 1;

Chris Allegretta's avatar
Chris Allegretta committed
276
	    cut_marked_segment(mark_beginbuf, mark_beginx, current,
277
			       current_x, 1);
278
279
	}

Chris Allegretta's avatar
Chris Allegretta committed
280
281
282

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

Chris Allegretta's avatar
Chris Allegretta committed
284
285
	marked_cut = 1;
	set_modified();
286
287
	if (dontupdate || cuttingtoend) {
	    fix_editbot();
288
	    edit_refresh();
289
	} else
290
	    edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
291

Chris Allegretta's avatar
Chris Allegretta committed
292
293
294
295
296
297
298
299
300
301
302
	return 1;
#else
    if (0) {
#endif
    } else if (fileptr == fileage) {
	/* we're cutting the first line */
	if (fileptr->next != NULL) {
	    fileptr = fileptr->next;
	    tmp = fileptr;
	    fileage = fileptr;
	    add_to_cutbuffer(fileptr->prev);
303
	    totsize--;		/* get the newline */
Chris Allegretta's avatar
Chris Allegretta committed
304
305
306
	    totlines--;
	    fileptr->prev = NULL;
	    current = fileptr;
307
	    edit_update(fileage, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
308
309
310
	} else {
	    add_to_cutbuffer(fileptr);
	    fileage = make_new_node(NULL);
311
	    fileage->data = charalloc(1);
312
	    fileage->data[0] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
313
314
315
316
317
318
319
320
321
322
	    current = fileage;
	}
    } else {
	if (fileptr->prev != NULL)
	    fileptr->prev->next = fileptr->next;

	if (fileptr->next != NULL) {
	    (fileptr->next)->prev = fileptr->prev;
	    current = fileptr->next;
	    totlines--;
323
324
325
326
	    totsize--;		/* get the newline */
	}
	/* No longer an else here, because we never get here anymore...
	   No need to cut the magic line, as it's empty */
Chris Allegretta's avatar
Chris Allegretta committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
	add_to_cutbuffer(fileptr);
    }

    if (fileptr == edittop)
	edittop = current;

    edit_refresh();

    dump_buffer(cutbuffer);
    reset_cursor();

    set_modified();
    marked_cut = 0;
    current_x = 0;
    placewewant = 0;
    update_cursor();
    renumber(tmp);
    SET(KEEP_CUTBUFFER);
    return 1;
}

int do_uncut_text(void)
{
350
    filestruct *tmp = current, *fileptr = current, *newbuf, *newend;
Chris Allegretta's avatar
Chris Allegretta committed
351
352
#ifndef NANO_SMALL
    char *tmpstr, *tmpstr2;
353
    filestruct *hold = current;
Chris Allegretta's avatar
Chris Allegretta committed
354
355
356
357
#endif
    int i;

    wrap_reset();
358
    check_statblank();
Chris Allegretta's avatar
Chris Allegretta committed
359
360
361
362
363
364
365
366
367
368
369
    if (cutbuffer == NULL || fileptr == NULL)
	return 0;		/* AIEEEEEEEEEEEE */

    newbuf = copy_filestruct(cutbuffer);
    for (newend = newbuf; newend->next != NULL && newend != NULL;
	 newend = newend->next) {
	totlines++;
    }

    /* Hook newbuf into fileptr */
#ifndef NANO_SMALL
Chris Allegretta's avatar
Chris Allegretta committed
370
    if (marked_cut) {
Chris Allegretta's avatar
Chris Allegretta committed
371
372
373
	/* If there's only one line in the cutbuffer */
	if (cutbuffer->next == NULL) {
	    tmpstr =
374
		charalloc(strlen(current->data) + strlen(cutbuffer->data) +
Chris Allegretta's avatar
Chris Allegretta committed
375
376
377
378
379
380
381
382
			1);
	    strncpy(tmpstr, current->data, current_x);
	    strcpy(&tmpstr[current_x], cutbuffer->data);
	    strcat(tmpstr, &current->data[current_x]);
	    free(current->data);
	    current->data = tmpstr;
	    current_x += strlen(cutbuffer->data);
	    totsize += strlen(cutbuffer->data);
383
384
	    if (strlen(cutbuffer->data) == 0)
		totlines++;
Chris Allegretta's avatar
Chris Allegretta committed
385
386
387
388
	    /* If we've uncut a line, make sure there's a magicline after
	       it */
	    if (current->next == NULL)
		new_magicline();
Chris Allegretta's avatar
Chris Allegretta committed
389
390
391
392
393
394

	    placewewant = xplustabs();
	    update_cursor();
	} else {		/* yuck -- no kidding! */
	    tmp = current->next;
	    /* New beginning */
395
	    tmpstr = charalloc(current_x + strlen(newbuf->data) + 1);
Chris Allegretta's avatar
Chris Allegretta committed
396
397
398
399
400
	    strncpy(tmpstr, current->data, current_x);
	    strcpy(&tmpstr[current_x], newbuf->data);
	    totsize += strlen(newbuf->data) + strlen(newend->data) + 1;

	    /* New end */
401
	    tmpstr2 = charalloc(strlen(newend->data) +
Chris Allegretta's avatar
Chris Allegretta committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
			      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;

	    /* If tmp isn't null, we're in the middle: update the
420
	     * prev pointer.  If it IS null, we're at the end; update
Chris Allegretta's avatar
Chris Allegretta committed
421
422
423
424
	     * the filebot pointer */

	    if (tmp != NULL)
		tmp->prev = newend;
Chris Allegretta's avatar
Chris Allegretta committed
425
426
427
428
	    else {
		/* Fix the editbot pointer too */
		if (editbot == filebot)
		    editbot = newend;
Chris Allegretta's avatar
Chris Allegretta committed
429
		filebot = newend;
430
		new_magicline();
Chris Allegretta's avatar
Chris Allegretta committed
431
	    }
Chris Allegretta's avatar
Chris Allegretta committed
432
433
434
435
436
437
438
439

	    /* Now why don't we update the totsize also */
	    for (tmp = current->next; tmp != newend; tmp = tmp->next)
		totsize += strlen(tmp->data) + 1;

	    i = editbot->lineno;

	    current = newend;
440
	    if (i < newend->lineno) {
441
		edit_update(current, CENTER);
442
443
444
445
	    }
	    else {
		edit_refresh();
	    }
Chris Allegretta's avatar
Chris Allegretta committed
446
447
	}

448
449
450
451
452
	/* If marked cut == 2, that means that we're doing a cut to end
	   and we don't want anything else on the line, so we have to
	   screw up all the work we just did and separate the line.  There
	   must be a better way to do this, but not at 1AM on a work night. */

453
	if (marked_cut == 2) {
454
	    tmp = make_new_node(current);
455
	    tmp->data = charalloc(strlen(&current->data[current_x]) + 1);
456
	    strcpy(tmp->data, &current->data[current_x]);
457
	    splice_node(current, tmp, current->next);
458
	    null_at(&current->data, current_x);
459
460
461
	    current = current->next;
	    current_x = 0;
	    placewewant = 0;
462
463
464
465

	    /* Extra line added, update stuff */
	    totlines++;
	    totsize++;
466
	}
Chris Allegretta's avatar
Chris Allegretta committed
467
468
469
	/* Renumber from BEFORE where we pasted ;) */
	renumber(hold);

Chris Allegretta's avatar
Chris Allegretta committed
470
471
472
473
	dump_buffer(fileage);
	dump_buffer(cutbuffer);
	set_modified();
	edit_refresh();
474
	UNSET(KEEP_CUTBUFFER);
Chris Allegretta's avatar
Chris Allegretta committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
	return 0;
#else
    if (0) {
#endif
    } else if (fileptr != fileage) {
	tmp = fileptr->prev;
	tmp->next = newbuf;
	newbuf->prev = tmp;
	totlines++;		/* Unmarked uncuts don't split lines */
    } else {
	fileage = newbuf;
	totlines++;		/* Unmarked uncuts don't split lines */
    }

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

    /* Connect the end of the buffer to the filestruct */
    newend->next = fileptr;
    fileptr->prev = newend;

    /* recalculate size *sigh* */
    for (tmp = newbuf; tmp != fileptr; tmp = tmp->next)
	totsize += strlen(tmp->data) + 1;

    i = editbot->lineno;
    renumber(newbuf);
503
    if (i < newend->lineno) {
504
	edit_update(fileptr, CENTER);
505
506
507
508
    }
    else {
	edit_refresh();
    }
Chris Allegretta's avatar
Chris Allegretta committed
509
510
511
512
513
514
515
516

    dump_buffer_reverse(fileptr);

    set_modified();
    UNSET(KEEP_CUTBUFFER);
    edit_refresh();
    return 1;
}