-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete.c
284 lines (249 loc) · 4.48 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
/* Copyright (C) 1988-2017 Sean MacLennan */
#include "z.h"
/** @addtogroup zedit
* @{
*/
static struct buff *Killbuff;
void delinit(void)
{
Killbuff = bcreate();
}
void delfini(void)
{
bdelbuff(Killbuff);
}
unsigned int delpages(void)
{
unsigned int npage = 0;
for (struct page *page = Killbuff->firstp; page; page = page->nextp)
++npage;
return npage;
}
/* Was the last command a delete to kill buffer command? */
bool delcmd(void)
{
switch (Lfunc) {
case ZDELETE_TO_EOL:
case ZDELETE_LINE:
case ZDELETE_REGION:
case ZDELETE_WORD:
case ZDELETE_PREVIOUS_WORD:
case ZCOPY_REGION:
case ZCOPY_WORD:
case ZAPPEND_KILL:
return true;
default:
return false;
}
}
static void copytomrk(struct mark *tmark)
{
if (delcmd())
btoend(Killbuff);
else
bempty(Killbuff);
bcopyrgn(tmark, Killbuff);
}
void killtomrk(struct mark *tmark)
{
copytomrk(tmark);
bdeltomrk(tmark);
}
void Zappend_kill(void) {}
void Zdelete_char(void)
{
bdelete(Bbuff, Arg);
Arg = 0;
}
void Zdelete_previous_char(void)
{
bmove(Bbuff, -Arg);
bdelete(Bbuff, Arg);
Arg = 0;
}
void Zdelete_to_eol(void)
{
if (!bisend(Bbuff) && Buff() == NL)
bdelete(Bbuff, 1);
else {
bool atstart = bpeek(Bbuff) == NL;
struct mark *tmark = bcremark(Bbuff);
if (!tmark) {
tbell();
return;
}
toendline(Bbuff);
if (atstart)
bmove1(Bbuff); /* delete the NL */
killtomrk(tmark);
bdelmark(tmark);
}
}
void Zdelete_line(void)
{
struct mark *tmark = bcremark(Bbuff);
if (!tmark) {
tbell();
return;
}
tobegline(Bbuff);
bmrktopnt(Bbuff, tmark);
bcsearch(Bbuff, NL);
killtomrk(tmark);
bdelmark(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 mark *tmark; /* save must NOT be a pointer */
if (InPaw && First) {
bempty(Bbuff);
First = false;
}
#if !UNDO
/* This leaves the mark at the start of the yank and
* the point at the end.
*/
set_umark(NULL);
#endif
btoend(Killbuff);
tmark = bcremark(Killbuff);
if (!tmark) {
error("out of memory");
return;
}
btostart(Killbuff);
#if UNDO
undo_add(Bbuff, bcopyrgn(tmark, Bbuff));
#else
bcopyrgn(tmark, Bbuff);
#endif
bdelmark(tmark);
}
void Zdelete_word(void)
{
struct mark *tmark = bcremark(Bbuff);
if (!tmark) {
tbell();
return;
}
bmoveto(Bbuff, bisword, FORWARD);
bmovepast(Bbuff, bisword, FORWARD);
killtomrk(tmark);
bdelmark(tmark);
}
void Zdelete_previous_word(void)
{
struct mark *tmark = bcremark(Bbuff);
if (!tmark) {
tbell();
return;
}
Zprevious_word();
killtomrk(tmark);
bdelmark(tmark);
}
void Zcopy_word(void)
{
char word[STRMAX], *ptr;
if (InPaw) {
zswitchto(Buff_save);
getbword(word, STRMAX, bistoken);
zswitchto(Paw);
for (ptr = word; *ptr; ++ptr) {
Cmd = *ptr;
pinsert();
}
} else {
struct mark *tmark = bcremark(Bbuff); /* save current Point */
struct mark *start = bcremark(Bbuff);
if (tmark && start) {
/* find start of word */
bmoveto(Bbuff, bistoken, FORWARD);
bmovepast(Bbuff, bistoken, BACKWARD);
bmrktopnt(Bbuff, start);
/* move Point to end of word */
bmovepast(Bbuff, bistoken, FORWARD);
/* copy to Kill buffer */
copytomrk(start);
/* move Point back */
bpnttomrk(Bbuff, tmark);
} else
tbell();
bdelmark(tmark);
bdelmark(start);
}
Arg = 0;
}
void Zdelete_blanks(void)
{
struct mark *pmark = bcremark(Bbuff);
struct mark *tmark = bcremark(Bbuff);
if (!pmark || !tmark) {
tbell();
goto done;
}
if (Argp) {
struct regexp re;
Arg = 0;
if (re_compile(&re, "^[ \t]*\r?$", REG_EXTENDED)) {
error("Internal re error.");
goto done;
}
btostart(Bbuff);
while (re_step(Bbuff, &re, tmark)) {
bmove1(Bbuff); /* skip over the NL */
bdeltomrk(tmark);
}
re_free(&re);
goto done;
}
if (bcrsearch(Bbuff, NL)) {
bmove1(Bbuff);
bmrktopnt(Bbuff, tmark);
bmovepast(Bbuff, isspace, BACKWARD);
if (!bisstart(Bbuff))
bcsearch(Bbuff, NL);
if (bisbeforemrk(Bbuff, tmark))
bdeltomrk(tmark);
}
if (bcsearch(Bbuff, NL)) {
bmrktopnt(Bbuff, tmark);
bmovepast(Bbuff, isspace, FORWARD);
if (bcrsearch(Bbuff, NL))
bmove1(Bbuff);
if (bisaftermrk(Bbuff, tmark))
bdeltomrk(tmark);
}
done:
if (pmark)
bpnttomrk(Bbuff, pmark);
bdelmark(pmark);
bdelmark(tmark);
}
void Zjoin(void)
{
toendline(Bbuff);
bdelete(Bbuff, 1);
Ztrim_white_space();
binsert(Bbuff, ' ');
}
void Zempty_buffer(void)
{
if (ask("Empty buffer? ") != YES)
return;
bempty(Bbuff);
Bbuff->bmodf = true;
}
/* @} */