cut.c 11.2 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**************************************************************************
 *   cut.c                                                                *
 *                                                                        *
 *   Copyright (C) 1999 Chris Allegretta                                  *
 *   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 *
 *   the Free Software Foundation; either version 1, or (at your option)  *
 *   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.            *
 *                                                                        *
 **************************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
#include "proto.h"
#include "nano.h"

#ifndef NANO_SMALL
#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)
{
    filestruct *tmp;

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

48
    totsize -= strlen(inptr->data);
Chris Allegretta's avatar
Chris Allegretta committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    tmp = cutbuffer;
    if (cutbuffer == NULL) {
	cutbuffer = inptr;
	inptr->prev = NULL;
    } else {
	cutbottom->next = inptr;
	inptr->prev = cutbottom;
    }

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

#ifndef NANO_SMALL
void cut_marked_segment(filestruct * top, int top_x, filestruct * bot,
			int bot_x)
{
    filestruct *tmp, *next;
    char *tmpstr;

    /* Set up the beginning of the cutbuffer */
    tmp = copy_node(top);
    tmpstr = nmalloc(strlen(&top->data[top_x]) + 1);
    strcpy(tmpstr, &top->data[top_x]);
    free(tmp->data);
    tmp->data = tmpstr;

    /* Chop off the end of the first line */
    tmpstr = nmalloc(top_x + 1);
    strncpy(tmpstr, top->data, top_x);
    free(top->data);
    top->data = tmpstr;

    do {
	next = tmp->next;
	add_to_cutbuffer(tmp);
	totlines--;
Chris Allegretta's avatar
Chris Allegretta committed
86
	totsize--;		/* newline (add_to_cutbuffer doesn't count newlines) */
Chris Allegretta's avatar
Chris Allegretta committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	tmp = next;
    }
    while (next != bot && next != NULL);

    dump_buffer(cutbuffer);
    if (next == NULL)
	return;
    /* Now, paste bot[bot_x] into top[top_x] */
    tmpstr = nmalloc(strlen(top->data) + strlen(&bot->data[bot_x]));
    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
101
    null_at(bot->data, bot_x);
Chris Allegretta's avatar
Chris Allegretta committed
102
103
104
    next = bot->next;

    /* We explicitly don't decrement totlines here because we don't snarf
105
106
     * 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 */
Chris Allegretta's avatar
Chris Allegretta committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

    add_to_cutbuffer(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;
	}
    }
126
    if (top->lineno < edittop->lineno)
127
	edit_update(top, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
128
129
130
131
132
133
134
135
}
#endif

int do_cut_text(void)
{
    filestruct *tmp, *fileptr = current;
#ifndef NANO_SMALL
    char *tmpstr;
136
    int newsize, cuttingtoend = 0;
Chris Allegretta's avatar
Chris Allegretta committed
137
138
#endif

139
    check_statblank();
140
    if (fileptr == NULL || fileptr->data == NULL)
Chris Allegretta's avatar
Chris Allegretta committed
141
142
143
144
	return 0;

    tmp = fileptr->next;

145
    if (!ISSET(KEEP_CUTBUFFER)) {
Chris Allegretta's avatar
Chris Allegretta committed
146
147
	free_filestruct(cutbuffer);
	cutbuffer = NULL;
148

Chris Allegretta's avatar
Chris Allegretta committed
149
150
151
152
#ifdef DEBUG
	fprintf(stderr, _("Blew away cutbuffer =)\n"));
#endif
    }
153
154

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

Chris Allegretta's avatar
Chris Allegretta committed
158
#ifndef NANO_SMALL
Chris Allegretta's avatar
Chris Allegretta committed
159
    if (ISSET(CUT_TO_END) && !ISSET(MARK_ISSET)) {
160
	if (current_x == strlen(current->data)) {
161
	    do_delete();
162
	    SET(KEEP_CUTBUFFER);
Chris Allegretta's avatar
Chris Allegretta committed
163
	    marked_cut = 2;
164
	    return 1;
165
	} else {
166
167
168
169
170
171
172
	    SET(MARK_ISSET);
	    SET(KEEP_CUTBUFFER);

	    mark_beginx = strlen(current->data);
	    mark_beginbuf = current;
	    cuttingtoend = 1;
	}
173
    }
Chris Allegretta's avatar
Chris Allegretta committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    if (ISSET(MARK_ISSET)) {
	if (current->lineno == mark_beginbuf->lineno) {
	    tmp = copy_node(current);
	    newsize = abs(strlen(&current->data[mark_beginx]) -
			  strlen(&current->data[current_x]));

	    tmpstr = nmalloc(newsize);
	    if (current_x < mark_beginx) {
		strncpy(tmpstr, &current->data[current_x], newsize);
		memmove(&current->data[current_x],
			&current->data[mark_beginx],
			strlen(&current->data[mark_beginx] - newsize));
	    } else {
		strncpy(tmpstr, &current->data[mark_beginx], newsize);
		memmove(&current->data[mark_beginx],
			&current->data[current_x],
			strlen(&current->data[current_x] - newsize));
		current_x = mark_beginx;
		update_cursor();
	    }
	    tmpstr[newsize] = 0;
	    tmp->data = tmpstr;
	    add_to_cutbuffer(tmp);
	    dump_buffer(cutbuffer);
	    align(&current->data);
	} else if (current->lineno < mark_beginbuf->lineno)
	    cut_marked_segment(current, current_x, mark_beginbuf,
			       mark_beginx);
	else
	    cut_marked_segment(mark_beginbuf, mark_beginx, current,
			       current_x);

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

Chris Allegretta's avatar
Chris Allegretta committed
209
210
	marked_cut = 1;
	set_modified();
211
212
213
	if (cuttingtoend)
	    edit_refresh();
	else
214
	    edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
215

Chris Allegretta's avatar
Chris Allegretta committed
216
217
218
219
220
221
222
223
224
225
226
	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);
227
	    totsize--;		/* get the newline */
Chris Allegretta's avatar
Chris Allegretta committed
228
229
230
	    totlines--;
	    fileptr->prev = NULL;
	    current = fileptr;
231
	    edit_update(fileage, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
232
233
234
235
	} else {
	    add_to_cutbuffer(fileptr);
	    fileage = make_new_node(NULL);
	    fileage->data = nmalloc(1);
236
	    fileage->data[0] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
237
238
239
240
241
242
243
244
245
246
	    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--;
247
248
249
250
	    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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
	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)
{
274
    filestruct *tmp = current, *fileptr = current, *newbuf, *newend;
Chris Allegretta's avatar
Chris Allegretta committed
275
276
#ifndef NANO_SMALL
    char *tmpstr, *tmpstr2;
277
    filestruct *hold = current;
Chris Allegretta's avatar
Chris Allegretta committed
278
279
280
281
#endif
    int i;

    wrap_reset();
282
    check_statblank();
Chris Allegretta's avatar
Chris Allegretta committed
283
284
285
286
287
288
289
290
291
292
293
    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
294
    if (marked_cut) {
Chris Allegretta's avatar
Chris Allegretta committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
	/* If there's only one line in the cutbuffer */
	if (cutbuffer->next == NULL) {
	    tmpstr =
		nmalloc(strlen(current->data) + strlen(cutbuffer->data) +
			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);

	    placewewant = xplustabs();
	    update_cursor();
	} else {		/* yuck -- no kidding! */
	    tmp = current->next;
	    /* New beginning */
	    tmpstr = nmalloc(current_x + strlen(newbuf->data) + 1);
	    strncpy(tmpstr, current->data, current_x);
	    strcpy(&tmpstr[current_x], newbuf->data);
	    totsize += strlen(newbuf->data) + strlen(newend->data) + 1;

	    /* New end */
	    tmpstr2 = nmalloc(strlen(newend->data) +
			      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
	     * prev pointer.  If it IS null, we're at the end, update
	     * the filebot pointer */

	    if (tmp != NULL)
		tmp->prev = newend;
Chris Allegretta's avatar
Chris Allegretta committed
343
344
345
346
	    else {
		/* Fix the editbot pointer too */
		if (editbot == filebot)
		    editbot = newend;
Chris Allegretta's avatar
Chris Allegretta committed
347
		filebot = newend;
348
		new_magicline();
Chris Allegretta's avatar
Chris Allegretta committed
349
	    }
Chris Allegretta's avatar
Chris Allegretta committed
350
351
352
353
354
355
356
357
358

	    /* 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;
	    if (i <= newend->lineno)
359
		edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
360
361
	}

362
363
364
365
366
367
368
	/* 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. */

	if (marked_cut == 2 && current_x != strlen(current->data)) {
	    tmp = make_new_node(current);
369
	    tmp->data = nmalloc(strlen(&current->data[current_x]) + 1);
370
	    strcpy(tmp->data, &current->data[current_x]);
371
	    splice_node(current, tmp, current->next);
Chris Allegretta's avatar
Chris Allegretta committed
372
	    null_at(current->data, current_x);
373
374
375
	    current = current->next;
	    current_x = 0;
	    placewewant = 0;
376
	}
Chris Allegretta's avatar
Chris Allegretta committed
377
378
379
	/* Renumber from BEFORE where we pasted ;) */
	renumber(hold);

Chris Allegretta's avatar
Chris Allegretta committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
	dump_buffer(fileage);
	dump_buffer(cutbuffer);
	set_modified();
	edit_refresh();
	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);
412
    if (i < newend->lineno)
413
	edit_update(fileptr, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
414
415
416
417
418
419
420
421

    dump_buffer_reverse(fileptr);

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