-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete.c
312 lines (273 loc) · 5.69 KB
/
delete.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* delete.c - Zedit delete commands
* Copyright (C) 1988-2013 Sean MacLennan
*
* 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 2, 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 project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "z.h"
static struct buff *Killbuff;
static void delfini(void)
{
_bdelbuff(Killbuff);
_bdelbuff(Paw);
}
void delinit(void)
{
Killbuff = _bcreate();
Paw = _bcreate();
atexit(delfini);
}
static void copytomrk(struct mark *tmark)
{
struct buff *save = Curbuff;
bswitchto(Killbuff);
if (delcmd())
btoend();
else
bempty(Curbuff);
bswitchto(save);
bcopyrgn(tmark, Killbuff);
}
/* Copy from Point to tmark to tbuff. Returns number of bytes
* copied. Caller must handle undo. */
int bcopyrgn(struct mark *tmark, struct buff *tbuff)
{
struct buff *sbuff;
struct mark *ltmrk, *btmrk;
bool flip;
int srclen, dstlen;
Byte *spnt;
int copied = 0;
if (tbuff == Curbuff)
return 0;
flip = bisaftermrk(tmark);
if (flip)
bswappnt(tmark);
if (!(ltmrk = _bcremrk(Curbuff)))
return 0;
sbuff = Curbuff;
while (bisbeforemrk(tmark)) {
if (Curpage == tmark->mpage)
srclen = tmark->moffset - Curchar;
else
srclen = Curpage->plen - Curchar;
spnt = Curcptr;
bswitchto(tbuff);
dstlen = PSIZE - Curpage->plen;
if (dstlen == 0) {
if (pagesplit(Curbuff, HALFP))
dstlen = PSIZE - Curpage->plen;
else {
bswitchto(sbuff);
break;
}
}
if (srclen < dstlen)
dstlen = srclen;
/* Make a gap */
memmove(Curcptr + dstlen, Curcptr, Curpage->plen - Curchar);
/* and fill it in */
memmove(Curcptr, spnt, dstlen);
Curpage->plen += dstlen;
copied += dstlen;
foreach_pagemark(Curbuff, btmrk, Curpage)
if (btmrk->moffset > Curchar)
btmrk->moffset += dstlen;
makeoffset(Curbuff, Curchar + dstlen);
vsetmod();
Curbuff->bmodf = true;
bswitchto(sbuff);
bmove(dstlen);
}
bpnttomrk(ltmrk);
unmark(ltmrk);
if (flip)
bswappnt(tmark);
return copied;
}
/* Delete from the point to the Mark. */
void bdeltomrk(struct mark *tmark)
{
if (bisaftermrk(tmark))
bswappnt(tmark);
while (bisbeforemrk(tmark))
if (Curpage == tmark->mpage)
bdelete(tmark->moffset - Curchar);
else
bdelete(Curpage->plen - Curchar);
}
void killtomrk(struct mark *tmark)
{
copytomrk(tmark);
bdeltomrk(tmark);
}
void Zappend_kill(void) {}
void Zdelete_char(void)
{
bdelete(Arg);
Arg = 0;
}
void Zdelete_previous_char(void)
{
bmove(-Arg);
bdelete(Arg);
Arg = 0;
}
void Zdelete_to_eol(void)
{
if (!bisend() && Buff() == NL)
bdelete(1);
else {
bool atstart = _bpeek(Curbuff) == NL;
struct mark *tmark = zcreatemrk();
toendline(Curbuff);
if (atstart)
bmove1(); /* delete the NL */
killtomrk(tmark);
unmark(tmark);
}
}
void Zdelete_line(void)
{
struct mark *tmark;
tobegline(Curbuff);
tmark = zcreatemrk();
bcsearch(Curbuff, NL);
killtomrk(tmark);
unmark(tmark);
}
void Zdelete_region(void)
{
NEED_UMARK;
killtomrk(UMARK);
CLEAR_UMARK;
}
void Zcopy_region(void)
{
NEED_UMARK;
copytomrk(UMARK);
CLEAR_UMARK;
}
void Zyank(void)
{
struct buff *tbuff;
struct mark *tmark, save; /* save must NOT be a pointer */
if (InPaw && First) {
bempty(Curbuff);
First = false;
}
mrktomrk(&save, Send);
tbuff = Curbuff;
#if !UNDO
/* This leaves the mark at the start of the yank and
* the point at the end. */
set_umark(NULL);
#endif
bswitchto(Killbuff);
btoend();
tmark = zcreatemrk();
btostart();
#if UNDO
undo_add(bcopyrgn(tmark, tbuff));
#else
bcopyrgn(tmark, tbuff);
#endif
unmark(tmark);
bswitchto(tbuff);
if (bisaftermrk(&save))
reframe();
}
void Zdelete_word(void)
{
struct mark *tmark;
tmark = zcreatemrk();
moveto(bisword, FORWARD);
movepast(bisword, FORWARD);
killtomrk(tmark);
unmark(tmark);
}
void Zdelete_previous_word(void)
{
struct mark *tmark;
tmark = zcreatemrk();
Zprevious_word();
killtomrk(tmark);
unmark(tmark);
}
void Zcopy_word(void)
{
char word[STRMAX], *ptr;
struct mark *tmark, *start;
if (InPaw) {
bswitchto(Buff_save);
getbword(word, STRMAX, bistoken);
bswitchto(Paw);
for (ptr = word; *ptr; ++ptr) {
Cmd = *ptr;
pinsert();
}
} else {
tmark = zcreatemrk(); /* save current Point */
moveto(bistoken, FORWARD); /* find start of word */
movepast(bistoken, BACKWARD);
start = zcreatemrk();
movepast(bistoken, FORWARD); /* move Point to end of word */
copytomrk(start); /* copy to Kill buffer */
bpnttomrk(tmark); /* move Point back */
unmark(tmark);
unmark(start);
}
Arg = 0;
}
void Zdelete_blanks(void)
{
struct mark *tmark, *pmark;
pmark = zcreatemrk();
if (bcrsearch(Curbuff, NL)) {
bmove1();
tmark = zcreatemrk();
movepast(bisspace, BACKWARD);
if (!bisstart())
bcsearch(Curbuff, NL);
if (bisbeforemrk(tmark))
bdeltomrk(tmark);
unmark(tmark);
}
if (bcsearch(Curbuff, NL)) {
tmark = zcreatemrk();
movepast(bisspace, FORWARD);
if (bcrsearch(Curbuff, NL))
bmove1();
if (bisaftermrk(tmark))
bdeltomrk(tmark);
unmark(tmark);
}
bpnttomrk(pmark);
unmark(pmark);
}
void Zjoin(void)
{
toendline(Curbuff);
bdelete(1);
Ztrim_white_space();
binsert(' ');
}
void Zempty_buffer(void)
{
if (ask("Empty buffer? ") != YES)
return;
bempty(Curbuff);
Curbuff->bmodf = true;
}