428
|
1 /* Buffer insertion/deletion and gap motion for XEmacs.
|
|
2 Copyright (C) 1985, 1986, 1991, 1992, 1993, 1994, 1995
|
|
3 Free Software Foundation, Inc.
|
|
4 Copyright (C) 1995 Sun Microsystems, Inc.
|
793
|
5 Copyright (C) 2001, 2002 Ben Wing.
|
428
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
|
24 /* Synched up with: Mule 2.0, FSF 19.30. Diverges significantly. */
|
|
25
|
|
26 /* This file has been Mule-ized. */
|
|
27
|
|
28 /* Overhauled by Ben Wing, December 1994, for Mule implementation. */
|
|
29
|
|
30 #include <config.h>
|
|
31 #include "lisp.h"
|
|
32
|
|
33 #include "buffer.h"
|
|
34 #include "device.h"
|
|
35 #include "frame.h"
|
|
36 #include "extents.h"
|
|
37 #include "insdel.h"
|
|
38 #include "lstream.h"
|
|
39 #include "redisplay.h"
|
|
40 #include "line-number.h"
|
|
41
|
|
42 /* Various macros modelled along the lines of those in buffer.h.
|
|
43 Purposefully omitted from buffer.h because files other than this
|
|
44 one should not be using them. */
|
|
45
|
|
46 /* Address of beginning of buffer. This is an lvalue because
|
|
47 BUFFER_ALLOC needs it to be. */
|
|
48 #define BUF_BEG_ADDR(buf) ((buf)->text->beg)
|
|
49
|
|
50 /* Set the address of beginning of buffer. */
|
|
51 #define SET_BUF_BEG_ADDR(buf, addr) do { (buf)->text->beg = (addr); } while (0)
|
|
52
|
|
53 /* Gap size. */
|
|
54 #define BUF_GAP_SIZE(buf) ((buf)->text->gap_size + 0)
|
|
55 #define BUF_END_GAP_SIZE(buf) ((buf)->text->end_gap_size + 0)
|
|
56 /* Set gap size. */
|
|
57 #define SET_BUF_GAP_SIZE(buf, value) \
|
|
58 do { (buf)->text->gap_size = (value); } while (0)
|
|
59 #define SET_BUF_END_GAP_SIZE(buf, value) \
|
|
60 do { (buf)->text->end_gap_size = (value); } while (0)
|
|
61
|
|
62 /* Gap location. */
|
|
63 #define BI_BUF_GPT(buf) ((buf)->text->gpt + 0)
|
|
64 #define BUF_GPT_ADDR(buf) (BUF_BEG_ADDR (buf) + BI_BUF_GPT (buf) - 1)
|
|
65
|
|
66 /* Set gap location. */
|
|
67 #define SET_BI_BUF_GPT(buf, value) do { (buf)->text->gpt = (value); } while (0)
|
|
68
|
|
69 /* Set end of buffer. */
|
|
70 #define SET_BOTH_BUF_Z(buf, val, bival) \
|
|
71 do \
|
|
72 { \
|
|
73 (buf)->text->z = (bival); \
|
|
74 (buf)->text->bufz = (val); \
|
|
75 } while (0)
|
|
76
|
|
77 /* Under Mule, we maintain two sentinels in the buffer: one at the
|
|
78 beginning of the gap, and one at the end of the buffer. This
|
|
79 allows us to move forward, examining bytes looking for the
|
|
80 end of a character, and not worry about running off the end.
|
|
81 We do not need corresponding sentinels when moving backwards
|
|
82 because we do not have to look past the beginning of a character
|
|
83 to find the beginning of the character.
|
|
84
|
|
85 Every time we change the beginning of the gap, we have to
|
|
86 call SET_GAP_SENTINEL().
|
|
87
|
|
88 Every time we change the total size (characters plus gap)
|
|
89 of the buffer, we have to call SET_END_SENTINEL().
|
|
90 */
|
|
91
|
|
92
|
|
93 #ifdef MULE
|
|
94 # define GAP_CAN_HOLD_SIZE_P(buf, len) (BUF_GAP_SIZE (buf) >= (len) + 1)
|
|
95 # define SET_GAP_SENTINEL(buf) (*BUF_GPT_ADDR (buf) = 0)
|
|
96 # define BUF_END_SENTINEL_SIZE 1
|
|
97 # define SET_END_SENTINEL(buf) \
|
|
98 (*(BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + BI_BUF_Z (buf) - 1) = 0)
|
|
99 #else
|
|
100 # define GAP_CAN_HOLD_SIZE_P(buf, len) (BUF_GAP_SIZE (buf) >= (len))
|
|
101 # define SET_GAP_SENTINEL(buf)
|
|
102 # define BUF_END_SENTINEL_SIZE 0
|
|
103 # define SET_END_SENTINEL(buf)
|
|
104 #endif
|
|
105
|
|
106
|
|
107 /************************************************************************/
|
|
108 /* point and marker adjustment */
|
|
109 /************************************************************************/
|
|
110
|
|
111 /* just_set_point() is the only place `PT' is an lvalue in all of emacs.
|
|
112 This function is called from set_buffer_point(), which is the function
|
|
113 that the SET_PT and BUF_SET_PT macros expand into, and from the
|
|
114 routines below that insert and delete text. (This is in cases where
|
|
115 the point marker logically doesn't move but PT (being a byte index)
|
|
116 needs to get adjusted.) */
|
|
117
|
|
118 /* Set point to a specified value. This is used only when the value
|
|
119 of point changes due to an insert or delete; it does not represent
|
|
120 a conceptual change in point as a marker. In particular, point is
|
|
121 not crossing any interval boundaries, so there's no need to use the
|
|
122 usual SET_PT macro. In fact it would be incorrect to do so, because
|
|
123 either the old or the new value of point is out of synch with the
|
|
124 current set of intervals. */
|
|
125
|
|
126 /* This gets called more than enough to make the function call
|
|
127 overhead a significant factor so we've turned it into a macro. */
|
665
|
128 #define JUST_SET_POINT(buf, charbpos, ind) \
|
428
|
129 do \
|
|
130 { \
|
665
|
131 buf->bufpt = (charbpos); \
|
428
|
132 buf->pt = (ind); \
|
|
133 } while (0)
|
|
134
|
|
135 /* Set a buffer's point. */
|
|
136
|
|
137 void
|
665
|
138 set_buffer_point (struct buffer *buf, Charbpos charbpos, Bytebpos bytpos)
|
428
|
139 {
|
|
140 assert (bytpos >= BI_BUF_BEGV (buf) && bytpos <= BI_BUF_ZV (buf));
|
|
141 if (bytpos == BI_BUF_PT (buf))
|
|
142 return;
|
665
|
143 JUST_SET_POINT (buf, charbpos, bytpos);
|
428
|
144 MARK_POINT_CHANGED;
|
|
145 assert (MARKERP (buf->point_marker));
|
665
|
146 XMARKER (buf->point_marker)->membpos =
|
|
147 bytebpos_to_membpos (buf, bytpos);
|
428
|
148
|
|
149 /* FSF makes sure that PT is not being set within invisible text.
|
|
150 However, this is the wrong place for that check. The check
|
|
151 should happen only at the next redisplay. */
|
|
152
|
|
153 /* Some old coder said:
|
|
154
|
|
155 "If there were to be hooks which were run when point entered/left an
|
|
156 extent, this would be the place to put them.
|
|
157
|
|
158 However, it's probably the case that such hooks should be implemented
|
|
159 using a post-command-hook instead, to avoid running the hooks as a
|
|
160 result of intermediate motion inside of save-excursions, for example."
|
|
161
|
|
162 I definitely agree with this. PT gets moved all over the place
|
|
163 and it would be a Bad Thing for any hooks to get called, both for
|
|
164 the reason above and because many callers are not prepared for
|
|
165 a GC within this function. --ben
|
|
166 */
|
|
167 }
|
|
168
|
|
169 /* Do the correct marker-like adjustment on MPOS (see below). FROM, TO,
|
|
170 and AMOUNT are as in adjust_markers(). If MPOS doesn't need to be
|
|
171 adjusted, nothing will happen. */
|
665
|
172 Membpos
|
|
173 do_marker_adjustment (Membpos mpos, Membpos from,
|
|
174 Membpos to, Bytecount amount)
|
428
|
175 {
|
|
176 if (amount > 0)
|
|
177 {
|
|
178 if (mpos > to && mpos < to + amount)
|
|
179 mpos = to + amount;
|
|
180 }
|
|
181 else
|
|
182 {
|
|
183 if (mpos > from + amount && mpos <= from)
|
|
184 mpos = from + amount;
|
|
185 }
|
|
186 if (mpos > from && mpos <= to)
|
|
187 mpos += amount;
|
|
188 return mpos;
|
|
189 }
|
|
190
|
|
191 /* Do the following:
|
|
192
|
|
193 (1) Add `amount' to the position of every marker in the current buffer
|
|
194 whose current position is between `from' (exclusive) and `to' (inclusive).
|
|
195
|
|
196 (2) Also, any markers past the outside of that interval, in the direction
|
|
197 of adjustment, are first moved back to the near end of the interval
|
|
198 and then adjusted by `amount'.
|
|
199
|
|
200 This function is called in two different cases: when a region of
|
|
201 characters adjacent to the gap is moved, causing the gap to shift
|
|
202 to the other side of the region (in this case, `from' and `to'
|
|
203 point to the old position of the region and there should be no
|
|
204 markers affected by (2) because they would be inside the gap),
|
|
205 or when a region of characters adjacent to the gap is wiped out,
|
|
206 causing the gap to increase to include the region (in this case,
|
|
207 `from' and `to' are the same, both pointing to the boundary
|
|
208 between the gap and the deleted region, and there are no markers
|
|
209 affected by (1)).
|
|
210
|
|
211 The reason for the use of exclusive and inclusive is that markers at
|
|
212 the gap always sit at the beginning, not at the end.
|
|
213 */
|
|
214
|
|
215 static void
|
665
|
216 adjust_markers (struct buffer *buf, Membpos from, Membpos to,
|
428
|
217 Bytecount amount)
|
|
218 {
|
440
|
219 Lisp_Marker *m;
|
428
|
220
|
|
221 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
|
665
|
222 m->membpos = do_marker_adjustment (m->membpos, from, to, amount);
|
428
|
223 }
|
|
224
|
|
225 /* Adjust markers whose insertion-type is t
|
|
226 for an insertion of AMOUNT characters at POS. */
|
|
227
|
|
228 static void
|
665
|
229 adjust_markers_for_insert (struct buffer *buf, Membpos ind, Bytecount amount)
|
428
|
230 {
|
440
|
231 Lisp_Marker *m;
|
428
|
232
|
|
233 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
|
|
234 {
|
665
|
235 if (m->insertion_type && m->membpos == ind)
|
|
236 m->membpos += amount;
|
428
|
237 }
|
|
238 }
|
|
239
|
|
240
|
|
241 /************************************************************************/
|
|
242 /* Routines for dealing with the gap */
|
|
243 /************************************************************************/
|
|
244
|
|
245 /* maximum amount of memory moved in a single chunk. Increasing this
|
|
246 value improves gap-motion efficiency but decreases QUIT responsiveness
|
|
247 time. Was 32000 but today's processors are faster and files are
|
|
248 bigger. --ben */
|
|
249 #define GAP_MOVE_CHUNK 300000
|
|
250
|
|
251 /* Move the gap to POS, which is less than the current GPT. */
|
|
252
|
|
253 static void
|
665
|
254 gap_left (struct buffer *buf, Bytebpos pos)
|
428
|
255 {
|
665
|
256 Intbyte *to, *from;
|
428
|
257 Bytecount i;
|
665
|
258 Bytebpos new_s1;
|
428
|
259 struct buffer *mbuf;
|
|
260 Lisp_Object bufcons;
|
|
261
|
|
262 from = BUF_GPT_ADDR (buf);
|
|
263 to = from + BUF_GAP_SIZE (buf);
|
|
264 new_s1 = BI_BUF_GPT (buf);
|
|
265
|
|
266 /* Now copy the characters. To move the gap down,
|
|
267 copy characters up. */
|
|
268
|
|
269 while (1)
|
|
270 {
|
|
271 /* I gets number of characters left to copy. */
|
|
272 i = new_s1 - pos;
|
|
273 if (i == 0)
|
|
274 break;
|
|
275 /* If a quit is requested, stop copying now.
|
|
276 Change POS to be where we have actually moved the gap to. */
|
|
277 if (QUITP)
|
|
278 {
|
|
279 pos = new_s1;
|
|
280 break;
|
|
281 }
|
|
282 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
|
|
283 if (i > GAP_MOVE_CHUNK)
|
|
284 i = GAP_MOVE_CHUNK;
|
440
|
285
|
|
286 if (i >= 128)
|
428
|
287 {
|
|
288 new_s1 -= i;
|
440
|
289 from -= i;
|
|
290 to -= i;
|
428
|
291 memmove (to, from, i);
|
|
292 }
|
|
293 else
|
|
294 {
|
|
295 new_s1 -= i;
|
|
296 while (--i >= 0)
|
|
297 *--to = *--from;
|
|
298 }
|
|
299 }
|
|
300
|
|
301 /* Adjust markers, and buffer data structure, to put the gap at POS.
|
|
302 POS is where the loop above stopped, which may be what was specified
|
|
303 or may be where a quit was detected. */
|
|
304 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
305 {
|
|
306 adjust_markers (mbuf, pos, BI_BUF_GPT (mbuf), BUF_GAP_SIZE (mbuf));
|
|
307 }
|
|
308 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
309 {
|
771
|
310 adjust_extents (wrap_buffer (mbuf), pos, BI_BUF_GPT (mbuf),
|
428
|
311 BUF_GAP_SIZE (mbuf));
|
|
312 }
|
|
313 SET_BI_BUF_GPT (buf, pos);
|
|
314 SET_GAP_SENTINEL (buf);
|
|
315 #ifdef ERROR_CHECK_EXTENTS
|
|
316 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
317 {
|
771
|
318 sledgehammer_extent_check (wrap_buffer (mbuf));
|
428
|
319 }
|
|
320 #endif
|
|
321 QUIT;
|
|
322 }
|
|
323
|
|
324 static void
|
665
|
325 gap_right (struct buffer *buf, Bytebpos pos)
|
428
|
326 {
|
665
|
327 Intbyte *to, *from;
|
428
|
328 Bytecount i;
|
665
|
329 Bytebpos new_s1;
|
428
|
330 struct buffer *mbuf;
|
|
331 Lisp_Object bufcons;
|
|
332
|
|
333 to = BUF_GPT_ADDR (buf);
|
|
334 from = to + BUF_GAP_SIZE (buf);
|
|
335 new_s1 = BI_BUF_GPT (buf);
|
|
336
|
|
337 /* Now copy the characters. To move the gap up,
|
|
338 copy characters down. */
|
|
339
|
|
340 while (1)
|
|
341 {
|
|
342 /* I gets number of characters left to copy. */
|
|
343 i = pos - new_s1;
|
|
344 if (i == 0)
|
|
345 break;
|
|
346 /* If a quit is requested, stop copying now.
|
|
347 Change POS to be where we have actually moved the gap to. */
|
|
348 if (QUITP)
|
|
349 {
|
|
350 pos = new_s1;
|
|
351 break;
|
|
352 }
|
|
353 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
|
|
354 if (i > GAP_MOVE_CHUNK)
|
|
355 i = GAP_MOVE_CHUNK;
|
440
|
356
|
|
357 if (i >= 128)
|
428
|
358 {
|
|
359 new_s1 += i;
|
|
360 memmove (to, from, i);
|
440
|
361 from += i;
|
|
362 to += i;
|
428
|
363 }
|
|
364 else
|
|
365 {
|
|
366 new_s1 += i;
|
|
367 while (--i >= 0)
|
|
368 *to++ = *from++;
|
|
369 }
|
|
370 }
|
|
371
|
|
372 {
|
|
373 int gsize = BUF_GAP_SIZE (buf);
|
|
374 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
375 {
|
|
376 adjust_markers (mbuf, BI_BUF_GPT (mbuf) + gsize, pos + gsize, - gsize);
|
|
377 }
|
|
378 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
379 {
|
771
|
380 adjust_extents (wrap_buffer (mbuf), BI_BUF_GPT (mbuf) + gsize,
|
428
|
381 pos + gsize, - gsize);
|
|
382 }
|
|
383 SET_BI_BUF_GPT (buf, pos);
|
|
384 SET_GAP_SENTINEL (buf);
|
|
385 #ifdef ERROR_CHECK_EXTENTS
|
|
386 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
387 {
|
771
|
388 sledgehammer_extent_check (wrap_buffer (mbuf));
|
428
|
389 }
|
|
390 #endif
|
|
391 }
|
|
392 if (pos == BI_BUF_Z (buf))
|
|
393 {
|
|
394 /* merge gap with end gap */
|
|
395
|
|
396 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + BUF_END_GAP_SIZE (buf));
|
|
397 SET_BUF_END_GAP_SIZE (buf, 0);
|
|
398 SET_END_SENTINEL (buf);
|
|
399 }
|
|
400
|
|
401 QUIT;
|
|
402 }
|
|
403
|
|
404 /* Move gap to position `pos'.
|
|
405 Note that this can quit! */
|
|
406
|
|
407 static void
|
665
|
408 move_gap (struct buffer *buf, Bytebpos pos)
|
428
|
409 {
|
|
410 if (! BUF_BEG_ADDR (buf))
|
|
411 abort ();
|
|
412 if (pos < BI_BUF_GPT (buf))
|
|
413 gap_left (buf, pos);
|
|
414 else if (pos > BI_BUF_GPT (buf))
|
|
415 gap_right (buf, pos);
|
|
416 }
|
|
417
|
|
418 /* Merge the end gap into the gap */
|
|
419
|
|
420 static void
|
|
421 merge_gap_with_end_gap (struct buffer *buf)
|
|
422 {
|
|
423 Lisp_Object tem;
|
665
|
424 Bytebpos real_gap_loc;
|
428
|
425 Bytecount old_gap_size;
|
|
426 Bytecount increment;
|
|
427
|
|
428 increment = BUF_END_GAP_SIZE (buf);
|
|
429 SET_BUF_END_GAP_SIZE (buf, 0);
|
|
430
|
|
431 if (increment > 0)
|
|
432 {
|
|
433 /* Prevent quitting in move_gap. */
|
|
434 tem = Vinhibit_quit;
|
|
435 Vinhibit_quit = Qt;
|
|
436
|
|
437 real_gap_loc = BI_BUF_GPT (buf);
|
|
438 old_gap_size = BUF_GAP_SIZE (buf);
|
|
439
|
|
440 /* Pretend the end gap is the gap */
|
|
441 SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf));
|
|
442 SET_BUF_GAP_SIZE (buf, increment);
|
|
443
|
|
444 /* Move the new gap down to be consecutive with the end of the old one.
|
|
445 This adjusts the markers properly too. */
|
|
446 gap_left (buf, real_gap_loc + old_gap_size);
|
|
447
|
|
448 /* Now combine the two into one large gap. */
|
|
449 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
|
|
450 SET_BI_BUF_GPT (buf, real_gap_loc);
|
|
451 SET_GAP_SENTINEL (buf);
|
|
452
|
|
453 /* We changed the total size of the buffer (including gap),
|
|
454 so we need to fix up the end sentinel. */
|
|
455 SET_END_SENTINEL (buf);
|
|
456
|
|
457 Vinhibit_quit = tem;
|
|
458 }
|
|
459 }
|
|
460
|
|
461 /* Make the gap INCREMENT bytes longer. */
|
|
462
|
|
463 static void
|
|
464 make_gap (struct buffer *buf, Bytecount increment)
|
|
465 {
|
665
|
466 Intbyte *result;
|
428
|
467 Lisp_Object tem;
|
665
|
468 Bytebpos real_gap_loc;
|
428
|
469 Bytecount old_gap_size;
|
|
470
|
|
471 /* If we have to get more space, get enough to last a while. We use
|
|
472 a geometric progression that saves on realloc space. */
|
|
473 increment += 2000 + ((BI_BUF_Z (buf) - BI_BUF_BEG (buf)) / 8);
|
|
474
|
|
475 if (increment > BUF_END_GAP_SIZE (buf))
|
|
476 {
|
|
477 /* Don't allow a buffer size that won't fit in an int
|
|
478 even if it will fit in a Lisp integer.
|
|
479 That won't work because so many places use `int'. */
|
|
480
|
|
481 if (BUF_Z (buf) - BUF_BEG (buf) + BUF_GAP_SIZE (buf) + increment
|
|
482 > EMACS_INT_MAX)
|
563
|
483 out_of_memory ("Maximum buffer size exceeded", Qunbound);
|
428
|
484
|
|
485 result = BUFFER_REALLOC (buf->text->beg,
|
|
486 BI_BUF_Z (buf) - BI_BUF_BEG (buf) +
|
|
487 BUF_GAP_SIZE (buf) + increment +
|
|
488 BUF_END_SENTINEL_SIZE);
|
|
489 if (result == 0)
|
|
490 memory_full ();
|
|
491
|
|
492 SET_BUF_BEG_ADDR (buf, result);
|
|
493 }
|
|
494 else
|
|
495 increment = BUF_END_GAP_SIZE (buf);
|
|
496
|
|
497 /* Prevent quitting in move_gap. */
|
|
498 tem = Vinhibit_quit;
|
|
499 Vinhibit_quit = Qt;
|
|
500
|
|
501 real_gap_loc = BI_BUF_GPT (buf);
|
|
502 old_gap_size = BUF_GAP_SIZE (buf);
|
|
503
|
|
504 /* Call the newly allocated space a gap at the end of the whole space. */
|
|
505 SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf));
|
|
506 SET_BUF_GAP_SIZE (buf, increment);
|
|
507
|
|
508 SET_BUF_END_GAP_SIZE (buf, 0);
|
|
509
|
|
510 /* Move the new gap down to be consecutive with the end of the old one.
|
|
511 This adjusts the markers properly too. */
|
|
512 gap_left (buf, real_gap_loc + old_gap_size);
|
|
513
|
|
514 /* Now combine the two into one large gap. */
|
|
515 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
|
|
516 SET_BI_BUF_GPT (buf, real_gap_loc);
|
|
517 SET_GAP_SENTINEL (buf);
|
|
518
|
|
519 /* We changed the total size of the buffer (including gap),
|
|
520 so we need to fix up the end sentinel. */
|
|
521 SET_END_SENTINEL (buf);
|
|
522
|
|
523 Vinhibit_quit = tem;
|
|
524 }
|
|
525
|
|
526
|
|
527 /************************************************************************/
|
|
528 /* Before/after-change processing */
|
|
529 /************************************************************************/
|
|
530
|
|
531 /* Those magic changes ... */
|
|
532
|
|
533 static void
|
665
|
534 buffer_signal_changed_region (struct buffer *buf, Charbpos start,
|
|
535 Charbpos end)
|
428
|
536 {
|
|
537 /* The changed region is recorded as the number of unchanged
|
|
538 characters from the beginning and from the end of the
|
|
539 buffer. This obviates much of the need of shifting the
|
|
540 region around to compensate for insertions and deletions.
|
|
541 */
|
|
542 if (buf->changes->begin_unchanged < 0 ||
|
|
543 buf->changes->begin_unchanged > start - BUF_BEG (buf))
|
|
544 buf->changes->begin_unchanged = start - BUF_BEG (buf);
|
|
545 if (buf->changes->end_unchanged < 0 ||
|
|
546 buf->changes->end_unchanged > BUF_Z (buf) - end)
|
|
547 buf->changes->end_unchanged = BUF_Z (buf) - end;
|
|
548 }
|
|
549
|
|
550 void
|
665
|
551 buffer_extent_signal_changed_region (struct buffer *buf, Charbpos start,
|
|
552 Charbpos end)
|
428
|
553 {
|
|
554 if (buf->changes->begin_extent_unchanged < 0 ||
|
|
555 buf->changes->begin_extent_unchanged > start - BUF_BEG (buf))
|
|
556 buf->changes->begin_extent_unchanged = start - BUF_BEG (buf);
|
|
557 if (buf->changes->end_extent_unchanged < 0 ||
|
|
558 buf->changes->end_extent_unchanged > BUF_Z (buf) - end)
|
|
559 buf->changes->end_extent_unchanged = BUF_Z (buf) - end;
|
|
560 }
|
|
561
|
|
562 void
|
|
563 buffer_reset_changes (struct buffer *buf)
|
|
564 {
|
|
565 buf->changes->begin_unchanged = -1;
|
|
566 buf->changes->end_unchanged = -1;
|
|
567 buf->changes->begin_extent_unchanged = -1;
|
|
568 buf->changes->end_extent_unchanged = -1;
|
|
569 buf->changes->newline_was_deleted = 0;
|
|
570 }
|
|
571
|
|
572 static void
|
665
|
573 signal_after_change (struct buffer *buf, Charbpos start, Charbpos orig_end,
|
|
574 Charbpos new_end);
|
428
|
575
|
|
576
|
|
577 /* Call the after-change-functions according to the changes made so far
|
|
578 and treat all further changes as single until the outermost
|
|
579 multiple change exits. This is called when the outermost multiple
|
|
580 change exits and when someone is trying to make a change that violates
|
|
581 the constraints specified in begin_multiple_change(), typically
|
|
582 when nested multiple-change sessions occur. (There are smarter ways of
|
|
583 dealing with nested multiple changes, but these rarely occur so there's
|
|
584 probably no point in it.) */
|
|
585
|
|
586 /* #### This needs to keep track of what actually changed and only
|
|
587 call the after-change functions on that region. */
|
|
588
|
|
589 static void
|
|
590 cancel_multiple_change (struct buffer *buf)
|
|
591 {
|
|
592 /* This function can GC */
|
|
593 /* Call the after-change-functions except when they've already been
|
|
594 called or when there were no changes made to the buffer at all. */
|
|
595 if (buf->text->changes->mc_begin != 0 &&
|
|
596 buf->text->changes->mc_begin_signaled)
|
|
597 {
|
665
|
598 Charbpos real_mc_begin = buf->text->changes->mc_begin;
|
428
|
599 buf->text->changes->mc_begin = 0;
|
|
600
|
|
601 signal_after_change (buf, real_mc_begin, buf->text->changes->mc_orig_end,
|
|
602 buf->text->changes->mc_new_end);
|
|
603 }
|
|
604 else
|
|
605 {
|
|
606 buf->text->changes->mc_begin = 0;
|
|
607 }
|
|
608 }
|
|
609
|
|
610 /* this is an unwind_protect, to ensure that the after-change-functions
|
|
611 get called even in a non-local exit. */
|
|
612
|
|
613 static Lisp_Object
|
|
614 multiple_change_finish_up (Lisp_Object buffer)
|
|
615 {
|
|
616 struct buffer *buf = XBUFFER (buffer);
|
|
617
|
|
618 /* #### I don't know whether or not it should even be possible to
|
|
619 get here with a dead buffer (though given how it is called I can
|
|
620 see how it might be). In any case, there isn't time before 19.14
|
|
621 to find out. */
|
|
622 if (!BUFFER_LIVE_P (buf))
|
|
623 return Qnil;
|
|
624
|
|
625 /* This function can GC */
|
|
626 buf->text->changes->in_multiple_change = 0; /* do this first so that
|
|
627 errors in the after-change
|
|
628 functions don't mess things
|
|
629 up. */
|
|
630 cancel_multiple_change (buf);
|
|
631 return Qnil;
|
|
632 }
|
|
633
|
|
634 /* Call this function when you're about to make a number of buffer changes
|
|
635 that should be considered a single change. (e.g. `replace-match' calls
|
|
636 this.) You need to specify the START and END of the region that is
|
|
637 going to be changed so that the before-change-functions are called
|
|
638 with the correct arguments. The after-change region is calculated
|
|
639 automatically, however, and if changes somehow or other happen outside
|
|
640 of the specified region, that will also be handled correctly.
|
|
641
|
|
642 begin_multiple_change() returns a number (actually a specpdl depth)
|
438
|
643 that you must pass to end_multiple_change() when you are done.
|
|
644
|
|
645 FSF Emacs 20 implements a similar feature, accessible from Lisp
|
|
646 through a `combine-after-change-calls' special form, which is
|
|
647 essentially equivalent to this function. We should consider
|
|
648 whether we want to introduce a similar Lisp form. */
|
428
|
649
|
|
650 int
|
665
|
651 begin_multiple_change (struct buffer *buf, Charbpos start, Charbpos end)
|
428
|
652 {
|
|
653 /* This function can GC */
|
|
654 int count = -1;
|
|
655 if (buf->text->changes->in_multiple_change)
|
|
656 {
|
|
657 if (buf->text->changes->mc_begin != 0 &&
|
|
658 (start < buf->text->changes->mc_begin ||
|
|
659 end > buf->text->changes->mc_new_end))
|
|
660 cancel_multiple_change (buf);
|
|
661 }
|
|
662 else
|
|
663 {
|
|
664 Lisp_Object buffer;
|
|
665
|
|
666 buf->text->changes->mc_begin = start;
|
|
667 buf->text->changes->mc_orig_end = buf->text->changes->mc_new_end = end;
|
|
668 buf->text->changes->mc_begin_signaled = 0;
|
|
669 count = specpdl_depth ();
|
793
|
670 buffer = wrap_buffer (buf);
|
428
|
671 record_unwind_protect (multiple_change_finish_up, buffer);
|
|
672 }
|
|
673 buf->text->changes->in_multiple_change++;
|
|
674 /* We don't call before-change-functions until signal_before_change()
|
|
675 is called, in case there is a read-only or other error. */
|
|
676 return count;
|
|
677 }
|
|
678
|
|
679 void
|
|
680 end_multiple_change (struct buffer *buf, int count)
|
|
681 {
|
|
682 assert (buf->text->changes->in_multiple_change > 0);
|
|
683 buf->text->changes->in_multiple_change--;
|
|
684 if (!buf->text->changes->in_multiple_change)
|
771
|
685 unbind_to (count);
|
428
|
686 }
|
|
687
|
|
688 static int inside_change_hook;
|
|
689
|
|
690 static Lisp_Object
|
|
691 change_function_restore (Lisp_Object buffer)
|
|
692 {
|
|
693 /* We should first reset the variable and then change the buffer,
|
|
694 because Fset_buffer() can throw. */
|
|
695 inside_change_hook = 0;
|
438
|
696 if (XBUFFER (buffer) != current_buffer)
|
|
697 Fset_buffer (buffer);
|
428
|
698 return Qnil;
|
|
699 }
|
|
700
|
|
701 static int in_first_change;
|
|
702
|
|
703 static Lisp_Object
|
|
704 first_change_hook_restore (Lisp_Object buffer)
|
|
705 {
|
|
706 in_first_change = 0;
|
|
707 Fset_buffer (buffer);
|
|
708 return Qnil;
|
|
709 }
|
|
710
|
|
711 /* Signal an initial modification to the buffer. */
|
|
712
|
|
713 static void
|
|
714 signal_first_change (struct buffer *buf)
|
|
715 {
|
|
716 /* This function can GC */
|
793
|
717 Lisp_Object buffer = wrap_buffer (current_buffer);
|
|
718
|
428
|
719
|
|
720 if (!in_first_change)
|
|
721 {
|
|
722 if (!NILP (symbol_value_in_buffer (Qfirst_change_hook, buffer)))
|
|
723 {
|
|
724 int speccount = specpdl_depth ();
|
|
725 record_unwind_protect (first_change_hook_restore, buffer);
|
|
726 set_buffer_internal (buf);
|
|
727 in_first_change = 1;
|
|
728 run_hook (Qfirst_change_hook);
|
771
|
729 unbind_to (speccount);
|
428
|
730 }
|
|
731 }
|
|
732 }
|
|
733
|
|
734 /* Signal a change to the buffer immediately before it happens.
|
|
735 START and END are the bounds of the text to be changed. */
|
|
736
|
|
737 static void
|
665
|
738 signal_before_change (struct buffer *buf, Charbpos start, Charbpos end)
|
428
|
739 {
|
|
740 /* This function can GC */
|
|
741 struct buffer *mbuf;
|
|
742 Lisp_Object bufcons;
|
|
743
|
|
744 if (!inside_change_hook)
|
|
745 {
|
|
746 Lisp_Object buffer;
|
438
|
747 int speccount;
|
428
|
748
|
|
749 /* Are we in a multiple-change session? */
|
|
750 if (buf->text->changes->in_multiple_change &&
|
|
751 buf->text->changes->mc_begin != 0)
|
|
752 {
|
|
753 /* If we're violating the constraints of the session,
|
|
754 call the after-change-functions as necessary for the
|
|
755 changes already made and treat further changes as
|
|
756 single. */
|
|
757 if (start < buf->text->changes->mc_begin ||
|
|
758 end > buf->text->changes->mc_new_end)
|
|
759 cancel_multiple_change (buf);
|
|
760 /* Do nothing if this is not the first change in the session. */
|
|
761 else if (buf->text->changes->mc_begin_signaled)
|
|
762 return;
|
|
763 else
|
|
764 {
|
|
765 /* First time through; call the before-change-functions
|
|
766 specifying the entire region to be changed. (Note that
|
|
767 we didn't call before-change-functions in
|
|
768 begin_multiple_change() because the buffer might be
|
|
769 read-only, etc.) */
|
|
770 start = buf->text->changes->mc_begin;
|
|
771 end = buf->text->changes->mc_new_end;
|
|
772 }
|
|
773 }
|
|
774
|
|
775 /* If buffer is unmodified, run a special hook for that case. */
|
|
776 if (BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
|
|
777 {
|
|
778 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
779 {
|
|
780 signal_first_change (mbuf);
|
|
781 }
|
|
782 }
|
|
783
|
|
784 /* Now in any case run the before-change-functions if any. */
|
438
|
785 speccount = specpdl_depth ();
|
|
786 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
|
|
787 inside_change_hook = 1;
|
428
|
788
|
|
789 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
790 {
|
793
|
791 buffer = wrap_buffer (mbuf);
|
428
|
792 if (!NILP (symbol_value_in_buffer (Qbefore_change_functions, buffer))
|
|
793 /* Obsolete, for compatibility */
|
|
794 || !NILP (symbol_value_in_buffer (Qbefore_change_function, buffer)))
|
|
795 {
|
|
796 set_buffer_internal (buf);
|
|
797 va_run_hook_with_args (Qbefore_change_functions, 2,
|
|
798 make_int (start), make_int (end));
|
|
799 /* Obsolete, for compatibility */
|
|
800 va_run_hook_with_args (Qbefore_change_function, 2,
|
|
801 make_int (start), make_int (end));
|
|
802 }
|
|
803 }
|
|
804
|
438
|
805 /* Make sure endpoints remain valid. before-change-functions
|
|
806 might have modified the buffer. */
|
|
807 if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
|
|
808 if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
|
|
809 if (end < BUF_BEGV (buf)) end = BUF_BEGV (buf);
|
|
810 if (end > BUF_ZV (buf)) end = BUF_ZV (buf);
|
|
811
|
428
|
812 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
813 {
|
793
|
814 buffer = wrap_buffer (mbuf);
|
438
|
815 report_extent_modification (buffer, start, end, 0);
|
428
|
816 }
|
771
|
817 unbind_to (speccount);
|
428
|
818
|
|
819 /* Only now do we indicate that the before-change-functions have
|
|
820 been called, in case some function throws out. */
|
|
821 buf->text->changes->mc_begin_signaled = 1;
|
|
822 }
|
|
823 }
|
|
824
|
|
825 /* Signal a change immediately after it happens.
|
665
|
826 START is the charbpos of the start of the changed text.
|
|
827 ORIG_END is the charbpos of the end of the before-changed text.
|
|
828 NEW_END is the charbpos of the end of the after-changed text.
|
428
|
829 */
|
|
830
|
|
831 static void
|
665
|
832 signal_after_change (struct buffer *buf, Charbpos start, Charbpos orig_end,
|
|
833 Charbpos new_end)
|
428
|
834 {
|
|
835 /* This function can GC */
|
|
836 struct buffer *mbuf;
|
|
837 Lisp_Object bufcons;
|
|
838
|
|
839 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
840 {
|
|
841 /* always do this. */
|
|
842 buffer_signal_changed_region (mbuf, start, new_end);
|
|
843 }
|
|
844 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
845 {
|
|
846 /* #### This seems inefficient. Wouldn't it be better to just
|
|
847 keep one cache per base buffer? */
|
|
848 font_lock_maybe_update_syntactic_caches (mbuf, start, orig_end, new_end);
|
|
849 }
|
|
850
|
|
851 if (!inside_change_hook)
|
|
852 {
|
|
853 Lisp_Object buffer;
|
438
|
854 int speccount;
|
428
|
855
|
|
856 if (buf->text->changes->in_multiple_change &&
|
|
857 buf->text->changes->mc_begin != 0)
|
|
858 {
|
|
859 assert (start >= buf->text->changes->mc_begin &&
|
|
860 start <= buf->text->changes->mc_new_end);
|
|
861 assert (orig_end >= buf->text->changes->mc_begin &&
|
|
862 orig_end <= buf->text->changes->mc_new_end);
|
|
863 buf->text->changes->mc_new_end += new_end - orig_end;
|
|
864 return; /* after-change-functions signalled when all changes done */
|
|
865 }
|
|
866
|
438
|
867 speccount = specpdl_depth ();
|
|
868 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
|
|
869 inside_change_hook = 1;
|
428
|
870 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
871 {
|
793
|
872 buffer = wrap_buffer (mbuf);
|
428
|
873
|
|
874 if (!NILP (symbol_value_in_buffer (Qafter_change_functions, buffer))
|
|
875 /* Obsolete, for compatibility */
|
|
876 || !NILP (symbol_value_in_buffer (Qafter_change_function, buffer)))
|
|
877 {
|
|
878 set_buffer_internal (buf);
|
|
879 /* The actual after-change functions take slightly
|
|
880 different arguments than what we were passed. */
|
|
881 va_run_hook_with_args (Qafter_change_functions, 3,
|
|
882 make_int (start), make_int (new_end),
|
|
883 make_int (orig_end - start));
|
|
884 /* Obsolete, for compatibility */
|
|
885 va_run_hook_with_args (Qafter_change_function, 3,
|
|
886 make_int (start), make_int (new_end),
|
|
887 make_int (orig_end - start));
|
|
888 }
|
|
889 }
|
|
890
|
438
|
891 /* Make sure endpoints remain valid. after-change-functions
|
|
892 might have modified the buffer. */
|
|
893 if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
|
|
894 if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
|
|
895 if (new_end < BUF_BEGV (buf)) new_end = BUF_BEGV (buf);
|
|
896 if (new_end > BUF_ZV (buf)) new_end = BUF_ZV (buf);
|
|
897 if (orig_end < BUF_BEGV (buf)) orig_end = BUF_BEGV (buf);
|
|
898 if (orig_end > BUF_ZV (buf)) orig_end = BUF_ZV (buf);
|
|
899
|
428
|
900 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
901 {
|
793
|
902 buffer = wrap_buffer (mbuf);
|
438
|
903 report_extent_modification (buffer, start, new_end, 1);
|
428
|
904 }
|
771
|
905 unbind_to (speccount); /* sets inside_change_hook back to 0 */
|
428
|
906 }
|
|
907 }
|
|
908
|
|
909 /* Call this if you're about to change the region of BUFFER from START
|
|
910 to END. This checks the read-only properties of the region, calls
|
|
911 the necessary modification hooks, and warns the next redisplay that
|
|
912 it should pay attention to that area. */
|
|
913
|
|
914 static void
|
665
|
915 prepare_to_modify_buffer (struct buffer *buf, Charbpos start, Charbpos end,
|
428
|
916 int lockit)
|
|
917 {
|
|
918 /* This function can GC */
|
|
919 /* dmoore - This function can also kill the buffer buf, the current
|
|
920 buffer, and do anything it pleases. So if you call it, be
|
|
921 careful. */
|
|
922 struct buffer *mbuf;
|
|
923 Lisp_Object buffer, bufcons;
|
|
924 struct gcpro gcpro1;
|
|
925
|
|
926 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
927 {
|
|
928 barf_if_buffer_read_only (mbuf, start, end);
|
|
929 }
|
|
930
|
|
931 /* if this is the first modification, see about locking the buffer's
|
|
932 file */
|
793
|
933 buffer = wrap_buffer (buf);
|
428
|
934 GCPRO1 (buffer);
|
|
935 if (!NILP (buf->filename) && lockit &&
|
|
936 BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
|
|
937 {
|
758
|
938 #ifdef CLASH_DETECTION
|
|
939 if (!NILP (buf->file_truename))
|
|
940 /* Make binding buffer-file-name to nil effective. */
|
|
941 lock_file (buf->file_truename);
|
|
942 #else
|
428
|
943 /* At least warn if this file has changed on disk since it was visited.*/
|
|
944 if (NILP (Fverify_visited_file_modtime (buffer))
|
|
945 && !NILP (Ffile_exists_p (buf->filename)))
|
|
946 call1_in_buffer (buf, intern ("ask-user-about-supersession-threat"),
|
|
947 buf->filename);
|
|
948 #endif /* not CLASH_DETECTION */
|
|
949 }
|
|
950 UNGCPRO;
|
|
951
|
|
952 /* #### dmoore - is this reasonable in case of buf being killed above? */
|
|
953 if (!BUFFER_LIVE_P (buf))
|
|
954 return;
|
|
955
|
|
956 signal_before_change (buf, start, end);
|
|
957
|
|
958 #ifdef REGION_CACHE_NEEDS_WORK
|
|
959 if (buf->newline_cache)
|
|
960 invalidate_region_cache (buf,
|
|
961 buf->newline_cache,
|
|
962 start - BUF_BEG (buf), BUF_Z (buf) - end);
|
|
963 if (buf->width_run_cache)
|
|
964 invalidate_region_cache (buf,
|
|
965 buf->width_run_cache,
|
|
966 start - BUF_BEG (buf), BUF_Z (buf) - end);
|
|
967 #endif
|
|
968
|
|
969 #if 0 /* FSFmacs */
|
|
970 Vdeactivate_mark = Qt;
|
|
971 #endif
|
|
972
|
|
973 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
974 {
|
|
975 mbuf->point_before_scroll = Qnil;
|
|
976 }
|
|
977 }
|
|
978
|
|
979
|
|
980 /************************************************************************/
|
|
981 /* Insertion of strings */
|
|
982 /************************************************************************/
|
|
983
|
|
984 void
|
665
|
985 fixup_internal_substring (const Intbyte *nonreloc, Lisp_Object reloc,
|
428
|
986 Bytecount offset, Bytecount *len)
|
|
987 {
|
|
988 assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc)));
|
|
989
|
|
990 if (*len < 0)
|
|
991 {
|
|
992 if (nonreloc)
|
442
|
993 *len = strlen ((const char *) nonreloc) - offset;
|
428
|
994 else
|
|
995 *len = XSTRING_LENGTH (reloc) - offset;
|
|
996 }
|
800
|
997 #ifdef ERROR_CHECK_TEXT
|
428
|
998 assert (*len >= 0);
|
|
999 if (STRINGP (reloc))
|
|
1000 {
|
|
1001 assert (offset >= 0 && offset <= XSTRING_LENGTH (reloc));
|
|
1002 assert (offset + *len <= XSTRING_LENGTH (reloc));
|
|
1003 }
|
|
1004 #endif
|
|
1005 }
|
|
1006
|
665
|
1007 /* Insert a string into BUF at Charbpos POS. The string data comes
|
428
|
1008 from one of two sources: constant, non-relocatable data (specified
|
|
1009 in NONRELOC), or a Lisp string object (specified in RELOC), which
|
|
1010 is relocatable and may have extent data that needs to be copied
|
|
1011 into the buffer. OFFSET and LENGTH specify the substring of the
|
|
1012 data that is actually to be inserted. As a special case, if POS
|
|
1013 is -1, insert the string at point and move point to the end of the
|
|
1014 string.
|
|
1015
|
|
1016 Normally, markers at the insertion point end up before the
|
|
1017 inserted string. If INSDEL_BEFORE_MARKERS is set in flags, however,
|
|
1018 they end up after the string.
|
|
1019
|
|
1020 INSDEL_NO_LOCKING is kludgy and is used when insert-file-contents is
|
|
1021 visiting a new file; it inhibits the locking checks normally done
|
|
1022 before modifying a buffer. Similar checks were already done
|
|
1023 in the higher-level Lisp functions calling insert-file-contents. */
|
|
1024
|
|
1025 Charcount
|
665
|
1026 buffer_insert_string_1 (struct buffer *buf, Charbpos pos,
|
|
1027 const Intbyte *nonreloc, Lisp_Object reloc,
|
428
|
1028 Bytecount offset, Bytecount length,
|
|
1029 int flags)
|
|
1030 {
|
|
1031 /* This function can GC */
|
|
1032 struct gcpro gcpro1;
|
665
|
1033 Bytebpos ind;
|
428
|
1034 Charcount cclen;
|
|
1035 int move_point = 0;
|
|
1036 struct buffer *mbuf;
|
|
1037 Lisp_Object bufcons;
|
|
1038
|
|
1039 /* Defensive steps just in case a buffer gets deleted and a calling
|
|
1040 function doesn't notice it. */
|
|
1041 if (!BUFFER_LIVE_P (buf))
|
|
1042 return 0;
|
|
1043
|
|
1044 fixup_internal_substring (nonreloc, reloc, offset, &length);
|
|
1045
|
|
1046 if (pos == -1)
|
|
1047 {
|
|
1048 pos = BUF_PT (buf);
|
|
1049 move_point = 1;
|
|
1050 }
|
|
1051
|
|
1052 #ifdef I18N3
|
|
1053 /* #### See the comment in print_internal(). If this buffer is marked
|
|
1054 as translatable, then Fgettext() should be called on obj if it
|
|
1055 is a string. */
|
|
1056 #endif
|
|
1057
|
|
1058 /* Make sure that point-max won't exceed the size of an emacs int. */
|
|
1059 if ((length + BUF_Z (buf)) > EMACS_INT_MAX)
|
563
|
1060 out_of_memory ("Maximum buffer size exceeded", Qunbound);
|
428
|
1061
|
|
1062 /* theoretically not necessary -- caller should GCPRO.
|
|
1063 #### buffer_insert_from_buffer_1() doesn't! */
|
|
1064 GCPRO1 (reloc);
|
|
1065
|
|
1066 prepare_to_modify_buffer (buf, pos, pos, !(flags & INSDEL_NO_LOCKING));
|
|
1067
|
|
1068 /* Defensive steps in case the before-change-functions fuck around */
|
|
1069 if (!BUFFER_LIVE_P (buf))
|
|
1070 {
|
|
1071 UNGCPRO;
|
|
1072 /* Bad bad pre-change function. */
|
|
1073 return 0;
|
|
1074 }
|
|
1075
|
|
1076 /* Make args be valid again. prepare_to_modify_buffer() might have
|
|
1077 modified the buffer. */
|
|
1078 if (pos < BUF_BEGV (buf))
|
|
1079 pos = BUF_BEGV (buf);
|
|
1080 if (pos > BUF_ZV (buf))
|
|
1081 pos = BUF_ZV (buf);
|
|
1082
|
771
|
1083 ind = charbpos_to_bytebpos (buf, pos);
|
|
1084
|
428
|
1085 /* string may have been relocated up to this point */
|
|
1086 if (STRINGP (reloc))
|
771
|
1087 {
|
793
|
1088 cclen = string_offset_byte_to_char_len (reloc, offset, length);
|
771
|
1089 nonreloc = XSTRING_DATA (reloc);
|
|
1090 }
|
|
1091 else
|
|
1092 cclen = bytecount_to_charcount (nonreloc + offset, length);
|
428
|
1093
|
|
1094 if (ind != BI_BUF_GPT (buf))
|
|
1095 /* #### if debug-on-quit is invoked and the user changes the
|
|
1096 buffer, bad things can happen. This is a rampant problem
|
|
1097 in Emacs. */
|
|
1098 move_gap (buf, ind); /* may QUIT */
|
|
1099 if (! GAP_CAN_HOLD_SIZE_P (buf, length))
|
|
1100 {
|
|
1101 if (BUF_END_GAP_SIZE (buf) >= length)
|
|
1102 merge_gap_with_end_gap (buf);
|
|
1103 else
|
|
1104 make_gap (buf, length - BUF_GAP_SIZE (buf));
|
|
1105 }
|
|
1106
|
|
1107 insert_invalidate_line_number_cache (buf, pos, nonreloc + offset, length);
|
|
1108
|
|
1109 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1110 {
|
|
1111 record_insert (mbuf, pos, cclen);
|
|
1112 }
|
|
1113
|
|
1114 BUF_MODIFF (buf)++;
|
|
1115 MARK_BUFFERS_CHANGED;
|
|
1116
|
|
1117 /* string may have been relocated up to this point */
|
|
1118 if (STRINGP (reloc))
|
|
1119 nonreloc = XSTRING_DATA (reloc);
|
|
1120
|
|
1121 memcpy (BUF_GPT_ADDR (buf), nonreloc + offset, length);
|
|
1122
|
|
1123 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) - length);
|
|
1124 SET_BI_BUF_GPT (buf, BI_BUF_GPT (buf) + length);
|
|
1125 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1126 {
|
|
1127 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) + cclen, BI_BUF_ZV (mbuf) + length);
|
|
1128 }
|
|
1129 SET_BOTH_BUF_Z (buf, BUF_Z (buf) + cclen, BI_BUF_Z (buf) + length);
|
|
1130 SET_GAP_SENTINEL (buf);
|
771
|
1131
|
|
1132
|
428
|
1133 #ifdef MULE
|
|
1134 buffer_mule_signal_inserted_region (buf, pos, length, cclen);
|
|
1135 #endif
|
|
1136
|
|
1137 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1138 {
|
771
|
1139 process_extents_for_insertion (wrap_buffer (mbuf), ind, length);
|
428
|
1140 }
|
|
1141
|
|
1142 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1143 {
|
|
1144 /* We know the gap is at IND so the cast is OK. */
|
665
|
1145 adjust_markers_for_insert (mbuf, (Membpos) ind, length);
|
428
|
1146 }
|
|
1147
|
|
1148 /* Point logically doesn't move, but may need to be adjusted because
|
|
1149 it's a byte index. point-marker doesn't change because it's a
|
|
1150 memory index. */
|
|
1151 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1152 {
|
|
1153 if (BI_BUF_PT (mbuf) > ind)
|
|
1154 JUST_SET_POINT (mbuf, BUF_PT (mbuf) + cclen,
|
|
1155 BI_BUF_PT (mbuf) + length);
|
|
1156 }
|
|
1157
|
|
1158 /* Well, point might move. */
|
|
1159 if (move_point)
|
|
1160 BI_BUF_SET_PT (buf, ind + length);
|
|
1161
|
|
1162 if (STRINGP (reloc))
|
|
1163 {
|
|
1164 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1165 {
|
|
1166 splice_in_string_extents (reloc, mbuf, ind, length, offset);
|
|
1167 }
|
|
1168 }
|
|
1169
|
|
1170 if (flags & INSDEL_BEFORE_MARKERS)
|
|
1171 {
|
|
1172 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1173 {
|
|
1174 /* ind - 1 is correct because the FROM argument is exclusive.
|
665
|
1175 I formerly used DEC_BYTEBPOS() but that caused problems at the
|
428
|
1176 beginning of the buffer. */
|
|
1177 adjust_markers (mbuf, ind - 1, ind, length);
|
|
1178 }
|
|
1179 }
|
|
1180
|
|
1181 signal_after_change (buf, pos, pos, pos + cclen);
|
|
1182
|
|
1183 UNGCPRO;
|
|
1184
|
|
1185 return cclen;
|
|
1186 }
|
|
1187
|
|
1188
|
|
1189 /* The following functions are interfaces onto the above function,
|
|
1190 for inserting particular sorts of data. In all the functions,
|
|
1191 BUF and POS specify the buffer and location where the insertion is
|
|
1192 to take place. (If POS is -1, text is inserted at point and point
|
|
1193 moves forward past the text.) FLAGS is as above. */
|
|
1194
|
|
1195 Charcount
|
665
|
1196 buffer_insert_raw_string_1 (struct buffer *buf, Charbpos pos,
|
|
1197 const Intbyte *nonreloc, Bytecount length,
|
428
|
1198 int flags)
|
|
1199 {
|
|
1200 /* This function can GC */
|
|
1201 return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length,
|
|
1202 flags);
|
|
1203 }
|
|
1204
|
|
1205 Charcount
|
665
|
1206 buffer_insert_lisp_string_1 (struct buffer *buf, Charbpos pos, Lisp_Object str,
|
428
|
1207 int flags)
|
|
1208 {
|
|
1209 /* This function can GC */
|
|
1210 return buffer_insert_string_1 (buf, pos, 0, str, 0,
|
|
1211 XSTRING_LENGTH (str),
|
|
1212 flags);
|
|
1213 }
|
|
1214
|
|
1215 /* Insert the null-terminated string S (in external format). */
|
|
1216
|
|
1217 Charcount
|
665
|
1218 buffer_insert_c_string_1 (struct buffer *buf, Charbpos pos, const char *s,
|
428
|
1219 int flags)
|
|
1220 {
|
|
1221 /* This function can GC */
|
442
|
1222 const char *translated = GETTEXT (s);
|
665
|
1223 return buffer_insert_string_1 (buf, pos, (const Intbyte *) translated, Qnil,
|
428
|
1224 0, strlen (translated), flags);
|
|
1225 }
|
|
1226
|
|
1227 Charcount
|
665
|
1228 buffer_insert_emacs_char_1 (struct buffer *buf, Charbpos pos, Emchar ch,
|
428
|
1229 int flags)
|
|
1230 {
|
|
1231 /* This function can GC */
|
665
|
1232 Intbyte str[MAX_EMCHAR_LEN];
|
428
|
1233 Bytecount len = set_charptr_emchar (str, ch);
|
|
1234 return buffer_insert_string_1 (buf, pos, str, Qnil, 0, len, flags);
|
|
1235 }
|
|
1236
|
|
1237 Charcount
|
665
|
1238 buffer_insert_c_char_1 (struct buffer *buf, Charbpos pos, char c,
|
428
|
1239 int flags)
|
|
1240 {
|
|
1241 /* This function can GC */
|
|
1242 return buffer_insert_emacs_char_1 (buf, pos, (Emchar) (unsigned char) c,
|
|
1243 flags);
|
|
1244 }
|
|
1245
|
|
1246 Charcount
|
665
|
1247 buffer_insert_from_buffer_1 (struct buffer *buf, Charbpos pos,
|
|
1248 struct buffer *buf2, Charbpos pos2,
|
428
|
1249 Charcount length, int flags)
|
|
1250 {
|
|
1251 /* This function can GC */
|
|
1252 Lisp_Object str = make_string_from_buffer (buf2, pos2, length);
|
|
1253 return buffer_insert_string_1 (buf, pos, 0, str, 0,
|
|
1254 XSTRING_LENGTH (str), flags);
|
|
1255 }
|
|
1256
|
|
1257
|
|
1258 /************************************************************************/
|
|
1259 /* Deletion of ranges */
|
|
1260 /************************************************************************/
|
|
1261
|
|
1262 /* Delete characters in buffer from FROM up to (but not including) TO. */
|
|
1263
|
|
1264 void
|
665
|
1265 buffer_delete_range (struct buffer *buf, Charbpos from, Charbpos to, int flags)
|
428
|
1266 {
|
|
1267 /* This function can GC */
|
|
1268 Charcount numdel;
|
665
|
1269 Bytebpos bi_from, bi_to;
|
428
|
1270 Bytecount bc_numdel;
|
|
1271 EMACS_INT shortage;
|
|
1272 struct buffer *mbuf;
|
|
1273 Lisp_Object bufcons;
|
|
1274
|
|
1275 /* Defensive steps just in case a buffer gets deleted and a calling
|
|
1276 function doesn't notice it. */
|
|
1277 if (!BUFFER_LIVE_P (buf))
|
|
1278 return;
|
|
1279
|
|
1280 /* Make args be valid */
|
|
1281 if (from < BUF_BEGV (buf))
|
|
1282 from = BUF_BEGV (buf);
|
|
1283 if (to > BUF_ZV (buf))
|
|
1284 to = BUF_ZV (buf);
|
|
1285 if ((numdel = to - from) <= 0)
|
|
1286 return;
|
|
1287
|
|
1288 prepare_to_modify_buffer (buf, from, to, !(flags & INSDEL_NO_LOCKING));
|
|
1289
|
|
1290 /* Defensive steps in case the before-change-functions fuck around */
|
|
1291 if (!BUFFER_LIVE_P (buf))
|
|
1292 /* Bad bad pre-change function. */
|
|
1293 return;
|
|
1294
|
|
1295 /* Make args be valid again. prepare_to_modify_buffer() might have
|
|
1296 modified the buffer. */
|
|
1297 if (from < BUF_BEGV (buf))
|
|
1298 from = BUF_BEGV (buf);
|
|
1299 if (to > BUF_ZV (buf))
|
|
1300 to = BUF_ZV (buf);
|
|
1301 if ((numdel = to - from) <= 0)
|
|
1302 return;
|
|
1303
|
|
1304 /* Redisplay needs to know if a newline was in the deleted region.
|
|
1305 If we've already marked the changed region as having a deleted
|
|
1306 newline there is no use in performing the check. */
|
|
1307 if (!buf->changes->newline_was_deleted)
|
|
1308 {
|
|
1309 scan_buffer (buf, '\n', from, to, 1, &shortage, 1);
|
|
1310 if (!shortage)
|
|
1311 {
|
|
1312 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1313 {
|
|
1314 mbuf->changes->newline_was_deleted = 1;
|
|
1315 }
|
|
1316 }
|
|
1317 }
|
|
1318
|
665
|
1319 bi_from = charbpos_to_bytebpos (buf, from);
|
|
1320 bi_to = charbpos_to_bytebpos (buf, to);
|
428
|
1321 bc_numdel = bi_to - bi_from;
|
|
1322
|
|
1323 delete_invalidate_line_number_cache (buf, from, to);
|
|
1324
|
|
1325 if (to == BUF_Z (buf) &&
|
|
1326 bi_from > BI_BUF_GPT (buf))
|
|
1327 {
|
|
1328 /* avoid moving the gap just to delete from the bottom. */
|
|
1329
|
|
1330 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1331 {
|
|
1332 record_delete (mbuf, from, numdel);
|
|
1333 }
|
|
1334 BUF_MODIFF (buf)++;
|
|
1335 MARK_BUFFERS_CHANGED;
|
|
1336
|
|
1337 /* #### Point used to be modified here, but this causes problems
|
665
|
1338 with MULE, as point is used to calculate bytebposs, and if the
|
428
|
1339 offset in bc_numdel causes point to move to a non first-byte
|
|
1340 location, causing some other function to throw an assertion
|
665
|
1341 in ASSERT_VALID_BYTEBPOS. I've moved the code to right after
|
428
|
1342 the other movements and adjustments, but before the gap is
|
|
1343 moved. -- jh 970813 */
|
|
1344
|
|
1345 /* Detach any extents that are completely within the range [FROM, TO],
|
|
1346 if the extents are detachable.
|
|
1347
|
|
1348 This must come AFTER record_delete(), so that the appropriate
|
|
1349 extents will be present to be recorded, and BEFORE the gap
|
|
1350 size is increased, as otherwise we will be confused about
|
|
1351 where the extents end. */
|
|
1352 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1353 {
|
771
|
1354 process_extents_for_deletion (wrap_buffer (mbuf), bi_from, bi_to, 0);
|
428
|
1355 }
|
|
1356
|
|
1357 /* Relocate all markers pointing into the new, larger gap to
|
|
1358 point at the end of the text before the gap. */
|
|
1359 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1360 {
|
|
1361 adjust_markers (mbuf,
|
|
1362 (bi_to + BUF_GAP_SIZE (mbuf)),
|
|
1363 (bi_to + BUF_GAP_SIZE (mbuf)),
|
|
1364 (- bc_numdel));
|
|
1365 }
|
|
1366
|
|
1367 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1368 {
|
|
1369 /* Relocate any extent endpoints just like markers. */
|
771
|
1370 adjust_extents_for_deletion (wrap_buffer (mbuf), bi_from, bi_to,
|
428
|
1371 BUF_GAP_SIZE (mbuf), bc_numdel, 0);
|
|
1372 }
|
|
1373
|
|
1374 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1375 {
|
|
1376 /* Relocate point as if it were a marker. */
|
|
1377 if (bi_from < BI_BUF_PT (mbuf))
|
|
1378 {
|
|
1379 if (BI_BUF_PT (mbuf) < bi_to)
|
|
1380 JUST_SET_POINT (mbuf, from, bi_from);
|
|
1381 else
|
|
1382 JUST_SET_POINT (mbuf, BUF_PT (mbuf) - numdel,
|
|
1383 BI_BUF_PT (mbuf) - bc_numdel);
|
|
1384 }
|
|
1385 }
|
|
1386
|
|
1387 SET_BUF_END_GAP_SIZE (buf, BUF_END_GAP_SIZE (buf) + bc_numdel);
|
|
1388
|
|
1389 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1390 {
|
|
1391 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) - numdel,
|
|
1392 BI_BUF_ZV (mbuf) - bc_numdel);
|
|
1393 }
|
|
1394 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel);
|
|
1395 SET_GAP_SENTINEL (buf);
|
|
1396 }
|
|
1397 else
|
|
1398 {
|
|
1399 /* Make sure the gap is somewhere in or next to what we are deleting. */
|
|
1400 if (bi_to < BI_BUF_GPT (buf))
|
|
1401 gap_left (buf, bi_to);
|
|
1402 if (bi_from > BI_BUF_GPT (buf))
|
|
1403 gap_right (buf, bi_from);
|
|
1404
|
|
1405 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1406 {
|
|
1407 record_delete (mbuf, from, numdel);
|
|
1408 }
|
|
1409 BUF_MODIFF (buf)++;
|
|
1410 MARK_BUFFERS_CHANGED;
|
|
1411
|
|
1412 /* #### Point used to be modified here, but this causes problems
|
665
|
1413 with MULE, as point is used to calculate bytebposs, and if the
|
428
|
1414 offset in bc_numdel causes point to move to a non first-byte
|
|
1415 location, causing some other function to throw an assertion
|
665
|
1416 in ASSERT_VALID_BYTEBPOS. I've moved the code to right after
|
428
|
1417 the other movements and adjustments, but before the gap is
|
|
1418 moved. -- jh 970813 */
|
|
1419
|
|
1420 /* Detach any extents that are completely within the range [FROM, TO],
|
|
1421 if the extents are detachable.
|
|
1422
|
|
1423 This must come AFTER record_delete(), so that the appropriate extents
|
|
1424 will be present to be recorded, and BEFORE the gap size is increased,
|
|
1425 as otherwise we will be confused about where the extents end. */
|
|
1426 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1427 {
|
771
|
1428 process_extents_for_deletion (wrap_buffer (mbuf), bi_from, bi_to, 0);
|
428
|
1429 }
|
|
1430
|
|
1431 /* Relocate all markers pointing into the new, larger gap to
|
|
1432 point at the end of the text before the gap. */
|
|
1433 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1434 {
|
|
1435 adjust_markers (mbuf,
|
|
1436 (bi_to + BUF_GAP_SIZE (mbuf)),
|
|
1437 (bi_to + BUF_GAP_SIZE (mbuf)),
|
|
1438 (- bc_numdel - BUF_GAP_SIZE (mbuf)));
|
|
1439 }
|
|
1440
|
|
1441 /* Relocate any extent endpoints just like markers. */
|
|
1442 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1443 {
|
771
|
1444 adjust_extents_for_deletion (wrap_buffer (mbuf), bi_from, bi_to,
|
428
|
1445 BUF_GAP_SIZE (mbuf),
|
|
1446 bc_numdel, BUF_GAP_SIZE (mbuf));
|
|
1447 }
|
|
1448
|
|
1449 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1450 {
|
|
1451 /* Relocate point as if it were a marker. */
|
|
1452 if (bi_from < BI_BUF_PT (mbuf))
|
|
1453 {
|
|
1454 if (BI_BUF_PT (mbuf) < bi_to)
|
|
1455 JUST_SET_POINT (mbuf, from, bi_from);
|
|
1456 else
|
|
1457 JUST_SET_POINT (mbuf, BUF_PT (mbuf) - numdel,
|
|
1458 BI_BUF_PT (mbuf) - bc_numdel);
|
|
1459 }
|
|
1460 }
|
|
1461
|
|
1462 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + bc_numdel);
|
|
1463 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1464 {
|
|
1465 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) - numdel,
|
|
1466 BI_BUF_ZV (mbuf) - bc_numdel);
|
|
1467 }
|
|
1468 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel);
|
|
1469 SET_BI_BUF_GPT (buf, bi_from);
|
|
1470 SET_GAP_SENTINEL (buf);
|
|
1471 }
|
|
1472
|
|
1473 #ifdef MULE
|
|
1474 buffer_mule_signal_deleted_region (buf, from, to, bi_from, bi_to);
|
|
1475 #endif
|
|
1476
|
|
1477 #ifdef ERROR_CHECK_EXTENTS
|
|
1478 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1479 {
|
771
|
1480 sledgehammer_extent_check (wrap_buffer (mbuf));
|
428
|
1481 }
|
|
1482 #endif
|
|
1483
|
|
1484 signal_after_change (buf, from, to, from);
|
|
1485 }
|
|
1486
|
|
1487
|
|
1488 /************************************************************************/
|
|
1489 /* Replacement of characters */
|
|
1490 /************************************************************************/
|
|
1491
|
|
1492 /* Replace the character at POS in buffer B with CH. */
|
|
1493
|
|
1494 void
|
665
|
1495 buffer_replace_char (struct buffer *buf, Charbpos pos, Emchar ch,
|
428
|
1496 int not_real_change, int force_lock_check)
|
|
1497 {
|
|
1498 /* This function can GC */
|
665
|
1499 Intbyte curstr[MAX_EMCHAR_LEN];
|
|
1500 Intbyte newstr[MAX_EMCHAR_LEN];
|
428
|
1501 Bytecount curlen, newlen;
|
|
1502
|
|
1503 /* Defensive steps just in case a buffer gets deleted and a calling
|
|
1504 function doesn't notice it. */
|
|
1505 if (!BUFFER_LIVE_P (buf))
|
|
1506 return;
|
|
1507
|
|
1508 curlen = BUF_CHARPTR_COPY_CHAR (buf, pos, curstr);
|
|
1509 newlen = set_charptr_emchar (newstr, ch);
|
|
1510
|
|
1511 if (curlen == newlen)
|
|
1512 {
|
|
1513 struct buffer *mbuf;
|
|
1514 Lisp_Object bufcons;
|
|
1515
|
|
1516 /* then we can just replace the text. */
|
|
1517 prepare_to_modify_buffer (buf, pos, pos + 1,
|
|
1518 !not_real_change || force_lock_check);
|
|
1519 /* Defensive steps in case the before-change-functions fuck around */
|
|
1520 if (!BUFFER_LIVE_P (buf))
|
|
1521 /* Bad bad pre-change function. */
|
|
1522 return;
|
|
1523
|
|
1524 /* Make args be valid again. prepare_to_modify_buffer() might have
|
|
1525 modified the buffer. */
|
|
1526 if (pos < BUF_BEGV (buf))
|
|
1527 pos = BUF_BEGV (buf);
|
|
1528 if (pos >= BUF_ZV (buf))
|
|
1529 pos = BUF_ZV (buf) - 1;
|
|
1530 if (pos < BUF_BEGV (buf))
|
|
1531 /* no more characters in buffer! */
|
|
1532 return;
|
|
1533
|
|
1534 if (BUF_FETCH_CHAR (buf, pos) == '\n')
|
|
1535 {
|
|
1536 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1537 {
|
|
1538 mbuf->changes->newline_was_deleted = 1;
|
|
1539 }
|
|
1540 }
|
|
1541 MARK_BUFFERS_CHANGED;
|
|
1542 if (!not_real_change)
|
|
1543 {
|
|
1544 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1545 {
|
|
1546 record_change (mbuf, pos, 1);
|
|
1547 }
|
|
1548 BUF_MODIFF (buf)++;
|
|
1549 }
|
|
1550 memcpy (BUF_BYTE_ADDRESS (buf, pos), newstr, newlen);
|
|
1551
|
|
1552 signal_after_change (buf, pos, pos + 1, pos + 1);
|
|
1553
|
|
1554 /* We do not have to adjust the Mule data; we just replaced a
|
|
1555 character with another of the same number of bytes. */
|
|
1556 }
|
|
1557 else
|
|
1558 {
|
|
1559 /*
|
|
1560 * Must implement as deletion followed by insertion.
|
|
1561 *
|
|
1562 * Make a note to move point forward later in the one situation
|
|
1563 * where it is needed, a delete/insert one position behind
|
|
1564 * point. Point will drift backward by one position and stay
|
|
1565 * there otherwise.
|
|
1566 */
|
|
1567 int movepoint = (pos == BUF_PT (buf) - 1);
|
|
1568
|
|
1569 buffer_delete_range (buf, pos, pos + 1, 0);
|
|
1570 /* Defensive steps in case the before-change-functions fuck around */
|
|
1571 if (!BUFFER_LIVE_P (buf))
|
|
1572 /* Bad bad pre-change function. */
|
|
1573 return;
|
|
1574
|
|
1575 /* Make args be valid again. prepare_to_modify_buffer() might have
|
|
1576 modified the buffer. */
|
|
1577 if (pos < BUF_BEGV (buf))
|
|
1578 pos = BUF_BEGV (buf);
|
|
1579 if (pos >= BUF_ZV (buf))
|
|
1580 pos = BUF_ZV (buf) - 1;
|
|
1581 if (pos < BUF_BEGV (buf))
|
|
1582 /* no more characters in buffer! */
|
|
1583 return;
|
|
1584 /*
|
|
1585 * -1 as the pos argument means to move point forward with the
|
|
1586 * insertion, which we must do if the deletion moved point
|
|
1587 * backward so that it now equals the insertion point.
|
|
1588 */
|
|
1589 buffer_insert_string_1 (buf, (movepoint ? -1 : pos),
|
|
1590 newstr, Qnil, 0, newlen, 0);
|
|
1591 }
|
|
1592 }
|
|
1593
|
|
1594
|
|
1595 /************************************************************************/
|
|
1596 /* Other functions */
|
|
1597 /************************************************************************/
|
|
1598
|
|
1599 /* Make a string from a buffer. This needs to take into account the gap,
|
|
1600 and add any necessary extents from the buffer. */
|
|
1601
|
|
1602 static Lisp_Object
|
665
|
1603 make_string_from_buffer_1 (struct buffer *buf, Charbpos pos, Charcount length,
|
428
|
1604 int no_extents)
|
|
1605 {
|
|
1606 /* This function can GC */
|
665
|
1607 Bytebpos bi_ind = charbpos_to_bytebpos (buf, pos);
|
|
1608 Bytecount bi_len = charbpos_to_bytebpos (buf, pos + length) - bi_ind;
|
428
|
1609 Lisp_Object val = make_uninit_string (bi_len);
|
|
1610
|
|
1611 struct gcpro gcpro1;
|
|
1612 GCPRO1 (val);
|
|
1613
|
|
1614 if (!no_extents)
|
|
1615 add_string_extents (val, buf, bi_ind, bi_len);
|
|
1616
|
|
1617 {
|
|
1618 Bytecount len1 = BI_BUF_GPT (buf) - bi_ind;
|
665
|
1619 Intbyte *start1 = BI_BUF_BYTE_ADDRESS (buf, bi_ind);
|
|
1620 Intbyte *dest = XSTRING_DATA (val);
|
428
|
1621
|
|
1622 if (len1 < 0)
|
|
1623 {
|
|
1624 /* Completely after gap */
|
|
1625 memcpy (dest, start1, bi_len);
|
|
1626 }
|
|
1627 else if (bi_len <= len1)
|
|
1628 {
|
|
1629 /* Completely before gap */
|
|
1630 memcpy (dest, start1, bi_len);
|
|
1631 }
|
|
1632 else
|
|
1633 {
|
|
1634 /* Spans gap */
|
665
|
1635 Bytebpos pos2 = bi_ind + len1;
|
|
1636 Intbyte *start2 = BI_BUF_BYTE_ADDRESS (buf, pos2);
|
428
|
1637
|
|
1638 memcpy (dest, start1, len1);
|
|
1639 memcpy (dest + len1, start2, bi_len - len1);
|
|
1640 }
|
|
1641 }
|
|
1642
|
771
|
1643 init_string_ascii_begin (val);
|
|
1644 sledgehammer_check_ascii_begin (val);
|
|
1645
|
428
|
1646 UNGCPRO;
|
|
1647 return val;
|
|
1648 }
|
|
1649
|
|
1650 Lisp_Object
|
665
|
1651 make_string_from_buffer (struct buffer *buf, Charbpos pos, Charcount length)
|
428
|
1652 {
|
|
1653 return make_string_from_buffer_1 (buf, pos, length, 0);
|
|
1654 }
|
|
1655
|
|
1656 Lisp_Object
|
665
|
1657 make_string_from_buffer_no_extents (struct buffer *buf, Charbpos pos,
|
428
|
1658 Charcount length)
|
|
1659 {
|
|
1660 return make_string_from_buffer_1 (buf, pos, length, 1);
|
|
1661 }
|
|
1662
|
|
1663 void
|
665
|
1664 barf_if_buffer_read_only (struct buffer *buf, Charbpos from, Charbpos to)
|
428
|
1665 {
|
|
1666 Lisp_Object buffer;
|
|
1667 Lisp_Object iro;
|
|
1668
|
793
|
1669 buffer = wrap_buffer (buf);
|
428
|
1670 back:
|
|
1671 iro = (buf == current_buffer ? Vinhibit_read_only :
|
|
1672 symbol_value_in_buffer (Qinhibit_read_only, buffer));
|
|
1673 if (!LISTP (iro))
|
|
1674 return;
|
|
1675 if (NILP (iro) && !NILP (buf->read_only))
|
|
1676 {
|
|
1677 Fsignal (Qbuffer_read_only, (list1 (buffer)));
|
|
1678 goto back;
|
|
1679 }
|
|
1680 if (from > 0)
|
|
1681 {
|
|
1682 if (to < 0)
|
|
1683 to = from;
|
|
1684 verify_extent_modification (buffer,
|
665
|
1685 charbpos_to_bytebpos (buf, from),
|
|
1686 charbpos_to_bytebpos (buf, to),
|
428
|
1687 iro);
|
|
1688 }
|
|
1689 }
|
|
1690
|
|
1691
|
|
1692 /************************************************************************/
|
|
1693 /* initialization */
|
|
1694 /************************************************************************/
|
|
1695
|
|
1696 void
|
|
1697 reinit_vars_of_insdel (void)
|
|
1698 {
|
|
1699 inside_change_hook = 0;
|
|
1700 in_first_change = 0;
|
|
1701 }
|
|
1702
|
|
1703 void
|
|
1704 vars_of_insdel (void)
|
|
1705 {
|
|
1706 reinit_vars_of_insdel ();
|
|
1707 }
|
|
1708
|
|
1709 void
|
|
1710 init_buffer_text (struct buffer *b)
|
|
1711 {
|
|
1712 if (!b->base_buffer)
|
|
1713 {
|
|
1714 SET_BUF_GAP_SIZE (b, 20);
|
|
1715 BUFFER_ALLOC (b->text->beg, BUF_GAP_SIZE (b) + BUF_END_SENTINEL_SIZE);
|
|
1716 if (! BUF_BEG_ADDR (b))
|
|
1717 memory_full ();
|
|
1718
|
|
1719 SET_BUF_END_GAP_SIZE (b, 0);
|
|
1720 SET_BI_BUF_GPT (b, 1);
|
|
1721 SET_BOTH_BUF_Z (b, 1, 1);
|
|
1722 SET_GAP_SENTINEL (b);
|
|
1723 SET_END_SENTINEL (b);
|
|
1724 #ifdef MULE
|
|
1725 {
|
|
1726 int i;
|
|
1727
|
|
1728 b->text->mule_bufmin = b->text->mule_bufmax = 1;
|
|
1729 b->text->mule_bytmin = b->text->mule_bytmax = 1;
|
|
1730 b->text->mule_shifter = 0;
|
|
1731 b->text->mule_three_p = 0;
|
771
|
1732 b->text->entirely_ascii_p = 1;
|
428
|
1733
|
|
1734 for (i = 0; i < 16; i++)
|
|
1735 {
|
665
|
1736 b->text->mule_charbpos_cache[i] = 1;
|
|
1737 b->text->mule_bytebpos_cache[i] = 1;
|
428
|
1738 }
|
|
1739 }
|
|
1740 #endif /* MULE */
|
|
1741 b->text->line_number_cache = Qnil;
|
|
1742
|
|
1743 BUF_MODIFF (b) = 1;
|
|
1744 BUF_SAVE_MODIFF (b) = 1;
|
|
1745
|
|
1746 JUST_SET_POINT (b, 1, 1);
|
|
1747 SET_BOTH_BUF_BEGV (b, 1, 1);
|
|
1748 SET_BOTH_BUF_ZV (b, 1, 1);
|
|
1749
|
|
1750 b->text->changes = xnew_and_zero (struct buffer_text_change_data);
|
|
1751 }
|
|
1752 else
|
|
1753 {
|
|
1754 JUST_SET_POINT (b, BUF_PT (b->base_buffer), BI_BUF_PT (b->base_buffer));
|
|
1755 SET_BOTH_BUF_BEGV (b, BUF_BEGV (b->base_buffer),
|
|
1756 BI_BUF_BEGV (b->base_buffer));
|
|
1757 SET_BOTH_BUF_ZV (b, BUF_ZV (b->base_buffer),
|
|
1758 BI_BUF_ZV (b->base_buffer));
|
|
1759 }
|
|
1760
|
|
1761 b->changes = xnew_and_zero (struct each_buffer_change_data);
|
|
1762 BUF_FACECHANGE (b) = 1;
|
|
1763
|
|
1764 #ifdef REGION_CACHE_NEEDS_WORK
|
|
1765 b->newline_cache = 0;
|
|
1766 b->width_run_cache = 0;
|
|
1767 b->width_table = Qnil;
|
|
1768 #endif
|
|
1769 }
|
|
1770
|
|
1771 void
|
|
1772 uninit_buffer_text (struct buffer *b)
|
|
1773 {
|
|
1774 if (!b->base_buffer)
|
|
1775 {
|
|
1776 BUFFER_FREE (b->text->beg);
|
|
1777 xfree (b->text->changes);
|
|
1778 }
|
|
1779 xfree (b->changes);
|
|
1780
|
|
1781 #ifdef REGION_CACHE_NEEDS_WORK
|
|
1782 if (b->newline_cache)
|
|
1783 {
|
|
1784 free_region_cache (b->newline_cache);
|
|
1785 b->newline_cache = 0;
|
|
1786 }
|
|
1787 if (b->width_run_cache)
|
|
1788 {
|
|
1789 free_region_cache (b->width_run_cache);
|
|
1790 b->width_run_cache = 0;
|
|
1791 }
|
|
1792 b->width_table = Qnil;
|
|
1793 #endif
|
|
1794 }
|