File: modify.c

package info (click to toggle)
levee 0.6-1.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 272 kB
  • ctags: 543
  • sloc: ansic: 4,457; makefile: 67
file content (186 lines) | stat: -rw-r--r-- 4,408 bytes parent folder | download
1
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
48
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
/*
 * LEVEE, or Captain Video;  A vi clone
 *
 * Copyright (c) 1982-1997 David L Parsons
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by David L Parsons (orc@pell.chi.il.us).  My name may not be used
 * to endorse or promote products derived from this software without
 * specific prior written permission.  THIS SOFTWARE IS PROVIDED
 * AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 */
#include <unistd.h>
#include <ctype.h>

#include "levee.h"
#include "extern.h"
#include "grep.h"

/* modification commands that can be accessed by either editcore || execmode */

/* put stuff into the yank buffer */

boolean doyank(int low, int high)
{
    HANDLE f;
    int sz;
    
    yank.size = high - low;
    moveleft(&core[low], yank.stuff, min(yank.size, SBUFSIZE));
    if (yank.size > SBUFSIZE) {
	if ((f = OPEN_NEW(yankbuf)) >= 0) {
	    low += SBUFSIZE;
	    sz = WRITE_TEXT(f, core + low, high - low);
	    CLOSE_FILE(f);
	    if (sz == high - low)
		return TRUE;
	}
	yank.size = -1;
	return FALSE;
    }
    return TRUE;
}

boolean deletion(int low, int high)
{
    if (doyank(low, high))		/* fill yank buffer */
	return delete_to_undo(&undo, low, high - low);
    return FALSE;
}

/* move stuff from the yank buffer into core */
boolean putback(int start, int *newend)
{
    int siz, st;
    HANDLE f;
    
    if (yank.size+bufmax < SIZE && yank.size > 0) {
	*newend = start + yank.size;
	if (start < bufmax)
	    moveright(&core[start], &core[start+yank.size], bufmax-start);
	moveleft(yank.stuff, &core[start], min(SBUFSIZE, yank.size));
	if (yank.size > SBUFSIZE) {
	    siz = yank.size - SBUFSIZE;
	    if ((f=OPEN_OLD(yankbuf)) >= 0) {
		st = READ_TEXT(f, &core[start+SBUFSIZE], siz);
		CLOSE_FILE(f);
		if (st == siz)
		    goto succeed;
	    }
	    moveleft(&core[start+yank.size], &core[start], bufmax-start);
	    *newend = -1;
	    return FALSE;
	}
    succeed:
	insert_to_undo(&undo, start, yank.size);
	return TRUE;
    }
    return FALSE;
}

#define DSIZE 1024

/* makedest: make the replacement string for an regular expression */
int makedest(char *str, int start, int ssize, int size)
{
    char *fr = dst;
    char *to = str;
    int as, asize, c;

    while (*fr && size >= 0) {
	if (*fr == AMPERSAND) {	/* & copies in the pattern that we matched */
	    if ((size -= ssize) < 0)
		return -1;
	    moveleft(&core[start],to,ssize);
	    to += ssize;
	    fr++;
	}
	else if (*fr == ESCAPE) {	/* \1 .. \9 do arguments */
	    c = fr[1];
	    fr += 2;
	    if (c >= '1' && c <= '9') {
		if ((as = RE_start[c-'1']) < 0)
		    continue;
		asize = RE_size [c-'1'];
		if ((size -= asize) < 0)
		    return -1;
		moveleft(&core[as],to,asize);
		to += asize;
	    }
	    else
		*to++ = c;
	}
	else {
	    *to++ = *fr++;
	    --size;
	}
    }
    return to-str;
}

int chop(int start, int *endd, boolean visual, boolean *query)
{
    int i,retval;
    char c;
/*>>>>
    boolean ok;
  <<<<*/
    char dest[DSIZE];
    int len, dlen;
    
    retval = -1;
    /*dlen = strlen(dst);*/
restart:
    count = 1;
    i = findfwd(pattern, start, *endd);
    if (i != ERR) {
	if (*query) {
	    /*>>>> don't delete -- keep for future use
	    if (visual) {
		levee_mvcur(yp,setX(i));puts("?");
	    }
	    else {
	    <<<<*/
		println();
		writeline(-1,-1,bseekeol(i));
		println();
		levee_mvcur(-1,setX(i));
		prints("^?");
	    /*>>>>
	    }
	    <<<<*/
	    do
		c = tolower(readchar());
	    while (c!='y'&&c!='n'&&c!='q'&&c!='a');
	    if (c == 'n') {
		start = i+1;
		goto restart;
	    }
	    else if (c == 'q')
		return retval;
	    else if (c == 'a')
		*query = FALSE;
	}
	len = lastp-i;
	dlen = makedest(dest, i, len, DSIZE);
	if (dlen >= 0 && bufmax-(int)(len+dlen) < SIZE
		      && delete_to_undo(&undo, i, len)) {
	    modified = TRUE;
	    if (dlen > 0) {
		moveright(&core[i], &core[i+dlen], bufmax-i);
		insert_to_undo(&undo,i,dlen);
		moveleft(dest,&core[i],dlen);
	    }
	    *endd += (dlen-len);
	    retval = i+dlen;
	}
    }
    return retval;
}