Debian Codegraph @

delete.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 /* Routines to perform all kinds of deletion. */
9
10 #include "jove.h"
11 #include "jctype.h"
12 #include "disp.h"
13 #include "delete.h"
14 #include "insert.h"
15 #include "marks.h"
16 #include "move.h"
17
18 /* Assumes that either line1 or line2 is actually the current line, so it can
19 * put its result into linebuf.
20 */
21 private void
22 patchup(LinePtr line1, int char1, LinePtr line2, int char2)
23 {
24 if (line1 != line2)
25 ChkWindows(line1, line2);
26 DotTo(line1, char1);
27 modify();
28 linecopy(linebuf, curchar, lcontents(line2) + char2);
29
30 /* The following is a redisplay optimization. */
31 if (line1 != line2 && (char1 == 0 && char2 == 0))
32 line1->l_dline = line2->l_dline;
33
34 DFixMarks(line1, char1, line2, char2);
35 makedirty(curline);
36 }
37
38 /* Deletes the region by unlinking the lines in the middle,
39 * and patching things up. The unlinked lines are still in
40 * order.
41 */
42 LinePtr
43 reg_delete(LinePtr line1, int char1, LinePtr line2, int char2)
44 {
45 LinePtr retline;
46
47 if ((line1 == line2 && char1 == char2) || line2 == NULL) {
48 complain((char *)NULL);
49 /* NOTREACHED */
50 }
51 (void) fixorder(&line1, &char1, &line2, &char2);
52
53 retline = nbufline(); /* New buffer line */
54
55 (void) ltobuf(line1, genbuf);
56 if (line1 == line2)
57 genbuf[char2] = '\0';
58
59 retline->l_prev = NULL;
60 retline->l_dline = jputline(&genbuf[char1]);
61 patchup(line1, char1, line2, char2);
62
63 if (line1 == line2)
64 retline->l_next = NULL;
65 else {
66 retline->l_next = line1->l_next;
67 (void) ltobuf(line2, genbuf);
68 genbuf[char2] = '\0';
69 line2->l_dline = jputline(genbuf);
70 /* Shorten this line */
71 }
72
73 if (line1 != line2) {
74 line1->l_next = line2->l_next;
75 if (line1->l_next)
76 line1->l_next->l_prev = line1;
77 else
78 curbuf->b_last = line1;
79 line2->l_next = NULL;
80 }
81
82 return retline;
83 }
84
85 private void
86 lremove(LinePtr line1, LinePtr line2)
87 {
88 LinePtr next = line1->l_next;
89
90 if (line1 == line2)
91 return;
92
93 line1->l_next = line2->l_next;
94 if (line1->l_next)
95 line1->l_next->l_prev = line1;
96 else
97 curbuf->b_last = line1;
98 lfreereg(next, line2); /* Put region at end of free line list. */
99 }
100
101 /* delete character forward */
102
103 void
104 DelNChar(void)
105 {
106 del_char(FORWARD, arg_value(), YES);
107 }
108
109 /* Delete character backward */
110
111 void
112 DelPChar(void)
113 {
114 if (MinorMode(OverWrite) && !eolp()) {
115 /* Overwrite with spaces.
116 * Some care is exercised to overwrite tabs reasonably,
117 * but control characters displayed as two are not handled.
118 */
119 int rightcol = calc_pos(linebuf, curchar);
120 int charcount = jmin(arg_value(), curchar);
121 int colcount = rightcol - calc_pos(linebuf, curchar-charcount);
122
123 b_char(charcount);
124 overwrite(' ', colcount);
125 b_char(colcount);
126 } else {
127 del_char(BACKWARD, arg_value(), YES);
128 }
129 }
130
131 /* Delete some characters. If deleting forward then call for_char
132 * to the final position otherwise call back_char. Then delete the
133 * region between the two with patchup().
134 */
135 void
136 del_char(int dir, int num, jbool OK_kill)
137 {
138 Bufpos before,
139 after;
140 jbool killp = (OK_kill && (abs(num) > 1));
141
142 DOTsave(&before);
143 if (dir == FORWARD)
144 f_char(num);
145 else
146 b_char(num);
147 if (before.p_line == curline && before.p_char == curchar) {
148 complain((char *)NULL);
149 /* NOTREACHED */
150 }
151 if (killp)
152 reg_kill(before.p_line, before.p_char, YES);
153 else {
154 DOTsave(&after);
155 (void) fixorder(&before.p_line, &before.p_char, &after.p_line, &after.p_char);
156 patchup(before.p_line, before.p_char, after.p_line, after.p_char);
157 lremove(before.p_line, after.p_line);
158 }
159 }
160
161 /* The kill ring.
162 * Newest entry is at killptr; second newest is at killptr-1, etc.
163 * All empty slots are at the end of the array.
164 */
165
166 LinePtr killbuf[NUMKILLS];
167 int killptr = 0; /* index of newest entry (if any) */
168
169 void
170 DelKillRing(void) /* delete newest entry */
171 {
172 int i;
173
174 lfreelist(killbuf[killptr]); /* free entry */
175
176 /* move space to end */
177 for (i = killptr; i != NUMKILLS-1; i++)
178 killbuf[i] = killbuf[i+1];
179 killbuf[i] = NULL;
180
181 /* make killptr index predecessor (if any) */
182 killptr = (killptr + NUMKILLS - 1) % NUMKILLS;
183 while (killbuf[killptr] == NULL && killptr != 0)
184 killptr -= 1;
185 }
186
187 private void
188 AddKillRing(LinePtr text) /* add a new entry. */
189 {
190 if (killbuf[killptr] != NULL) {
191 killptr = (killptr +1) % NUMKILLS;
192 if (killbuf[NUMKILLS-1] == NULL) {
193 /* there is space: move one slot here */
194 int i;
195
196 for (i = NUMKILLS-1; i != killptr; i--)
197 killbuf[i] = killbuf[i-1];
198 } else {
199 /* no free slots: delete oldest element */
200 lfreelist(killbuf[killptr]);
201 }
202 }
203 killbuf[killptr] = text;
204 }
205
206 /* This kills a region between point, and line1/char1 and puts it on
207 * the kill-ring. If the last command was one of the kill commands,
208 * the region is appended (prepended if backwards) to the last entry.
209 */
210 void
211 reg_kill(LinePtr line2, int char2, jbool dot_moved)
212 {
213 LinePtr nl,
214 line1 = curline;
215 int char1 = curchar;
216 jbool backwards;
217
218 backwards = !fixorder(&line1, &char1, &line2, &char2);
219 /* This is a kludge! But it possible for commands that don't
220 * know which direction they are deleting in (e.g., delete
221 * previous word could have been called with a negative argument
222 * in which case, it wouldn't know that it really deleted
223 * forward.
224 */
225 if (!dot_moved)
226 backwards = !backwards;
227
228 DotTo(line1, char1);
229
230 nl = reg_delete(line1, char1, line2, char2);
231
232 if (last_cmd != KILLCMD) {
233 AddKillRing(nl);
234 } else {
235 LinePtr lastln = lastline(nl);
236
237 if (backwards) {
238 (void) DoYank(nl, 0, lastln, length(lastln), killbuf[killptr], 0, (Buffer *)NULL);
239 } else {
240 LinePtr olastln = lastline(killbuf[killptr]);
241
242 (void) DoYank(nl, 0, lastln, length(lastln), olastln, length(olastln), (Buffer *)NULL);
243 }
244 }
245 this_cmd = KILLCMD;
246 }
247
248 void
249 DelReg(void)
250 {
251 Mark *mp = CurMark();
252
253 reg_kill(mp->m_line, mp->m_char, NO);
254 }
255
256 /* get a new line buffer and add it to the kill ring */
257 LinePtr
258 new_kill(void)
259 {
260 LinePtr nl = nbufline();
261
262 AddKillRing(nl);
263 SavLine(nl, NullStr);
264 nl->l_next = nl->l_prev = NULL;
265 return nl;
266 }
267
268 /* Save a region. A pretend kill. */
269
270 void
271 CopyRegion(void)
272 {
273 LinePtr nl;
274 Mark *mp;
275 int status;
276
277 mp = CurMark();
278 if (mp->m_line == curline && mp->m_char == curchar) {
279 complain((char *)NULL);
280 /* NOTREACHED */
281 }
282
283 nl = new_kill();
284
285 status = inorder(mp->m_line, mp->m_char, curline, curchar);
286 if (status == -1)
287 return;
288
289 if (status)
290 (void) DoYank(mp->m_line, mp->m_char, curline, curchar,
291 nl, 0, (Buffer *)NULL);
292 else
293 (void) DoYank(curline, curchar, mp->m_line, mp->m_char,
294 nl, 0, (Buffer *)NULL);
295 }
296
297 void
298 DelWtSpace(void)
299 {
300 char *ep = &linebuf[curchar],
301 *sp = &linebuf[curchar];
302
303 while (jiswhite(*ep))
304 ep += 1;
305 while (sp > linebuf && jiswhite(sp[-1]))
306 sp -= 1;
307 if (sp != ep) {
308 curchar = sp - linebuf;
309 DFixMarks(curline, curchar, curline, curchar + (int)(ep - sp));
310 /* Shift the remaining characters left in the buffer to close the gap.
311 * strcpy(sp, ep) won't do because the destination overlaps the source.
312 */
313 do {} while ((*sp++ = *ep++) != '\0');
314 makedirty(curline);
315 modify();
316 }
317 }
318
319 void
320 DelBlnkLines(void)
321 {
322 Mark *dot;
323 jbool all;
324
325 if (!blnkp(&linebuf[curchar]))
326 return;
327
328 dot = MakeMark(curline, curchar);
329 all = !blnkp(linebuf);
330 while (blnkp(linebuf) && curline->l_prev)
331 SetLine(curline->l_prev);
332 all |= firstp(curline);
333 Eol();
334 DelWtSpace();
335 line_move(FORWARD, 1, NO);
336 while (blnkp(linebuf) && !eobp()) {
337 DelWtSpace();
338 del_char(FORWARD, 1, NO);
339 }
340 if (!all && !eobp())
341 open_lines(1);
342 ToMark(dot);
343 DelMark(dot);
344 }
345
346 private void
347 dword(jbool forward)
348 {
349 Bufpos savedot;
350
351 DOTsave(&savedot);
352 if (forward)
353 ForWord();
354 else
355 BackWord();
356 reg_kill(savedot.p_line, savedot.p_char, YES);
357 }
358
359 void
360 DelNWord(void)
361 {
362 dword(YES);
363 }
364
365 void
366 DelPWord(void)
367 {
368 dword(NO);
369 }