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. */
|
826
|
63 #define BYTE_BUF_GPT(buf) ((buf)->text->gpt + 0)
|
|
64 #define BUF_GPT_ADDR(buf) (BUF_BEG_ADDR (buf) + BYTE_BUF_GPT (buf) - 1)
|
428
|
65
|
|
66 /* Set gap location. */
|
826
|
67 #define SET_BYTE_BUF_GPT(buf, value) do { (buf)->text->gpt = (value); } while (0)
|
428
|
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) \
|
826
|
98 (*(BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + BYTE_BUF_Z (buf) - 1) = 0)
|
428
|
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 {
|
826
|
140 assert (bytpos >= BYTE_BUF_BEGV (buf) && bytpos <= BYTE_BUF_ZV (buf));
|
|
141 if (bytpos == BYTE_BUF_PT (buf))
|
428
|
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);
|
826
|
264 new_s1 = BYTE_BUF_GPT (buf);
|
428
|
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 {
|
826
|
306 adjust_markers (mbuf, pos, BYTE_BUF_GPT (mbuf), BUF_GAP_SIZE (mbuf));
|
428
|
307 }
|
|
308 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
309 {
|
826
|
310 adjust_extents (wrap_buffer (mbuf), pos, BYTE_BUF_GPT (mbuf),
|
428
|
311 BUF_GAP_SIZE (mbuf));
|
|
312 }
|
826
|
313 SET_BYTE_BUF_GPT (buf, pos);
|
428
|
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);
|
826
|
335 new_s1 = BYTE_BUF_GPT (buf);
|
428
|
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 {
|
826
|
376 adjust_markers (mbuf, BYTE_BUF_GPT (mbuf) + gsize, pos + gsize, - gsize);
|
428
|
377 }
|
|
378 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
379 {
|
826
|
380 adjust_extents (wrap_buffer (mbuf), BYTE_BUF_GPT (mbuf) + gsize,
|
428
|
381 pos + gsize, - gsize);
|
|
382 }
|
826
|
383 SET_BYTE_BUF_GPT (buf, pos);
|
428
|
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 }
|
826
|
392 if (pos == BYTE_BUF_Z (buf))
|
428
|
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 ();
|
826
|
412 if (pos < BYTE_BUF_GPT (buf))
|
428
|
413 gap_left (buf, pos);
|
826
|
414 else if (pos > BYTE_BUF_GPT (buf))
|
428
|
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
|
826
|
437 real_gap_loc = BYTE_BUF_GPT (buf);
|
428
|
438 old_gap_size = BUF_GAP_SIZE (buf);
|
|
439
|
|
440 /* Pretend the end gap is the gap */
|
826
|
441 SET_BYTE_BUF_GPT (buf, BYTE_BUF_Z (buf) + BUF_GAP_SIZE (buf));
|
428
|
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);
|
826
|
450 SET_BYTE_BUF_GPT (buf, real_gap_loc);
|
428
|
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. */
|
826
|
473 increment += 2000 + ((BYTE_BUF_Z (buf) - BYTE_BUF_BEG (buf)) / 8);
|
|
474 /* Make sure the gap is always aligned properly in case we're using a
|
|
475 16-bit or 32-bit fixed-width format. (Other sizes should already be
|
|
476 aligned in such a case.) */
|
|
477 increment = MAX_ALIGN_SIZE (increment);
|
428
|
478
|
|
479 if (increment > BUF_END_GAP_SIZE (buf))
|
|
480 {
|
|
481 /* Don't allow a buffer size that won't fit in an int
|
|
482 even if it will fit in a Lisp integer.
|
|
483 That won't work because so many places use `int'. */
|
|
484
|
|
485 if (BUF_Z (buf) - BUF_BEG (buf) + BUF_GAP_SIZE (buf) + increment
|
|
486 > EMACS_INT_MAX)
|
563
|
487 out_of_memory ("Maximum buffer size exceeded", Qunbound);
|
428
|
488
|
|
489 result = BUFFER_REALLOC (buf->text->beg,
|
826
|
490 BYTE_BUF_Z (buf) - BYTE_BUF_BEG (buf) +
|
428
|
491 BUF_GAP_SIZE (buf) + increment +
|
|
492 BUF_END_SENTINEL_SIZE);
|
|
493 if (result == 0)
|
|
494 memory_full ();
|
|
495
|
|
496 SET_BUF_BEG_ADDR (buf, result);
|
|
497 }
|
|
498 else
|
|
499 increment = BUF_END_GAP_SIZE (buf);
|
|
500
|
|
501 /* Prevent quitting in move_gap. */
|
|
502 tem = Vinhibit_quit;
|
|
503 Vinhibit_quit = Qt;
|
|
504
|
826
|
505 real_gap_loc = BYTE_BUF_GPT (buf);
|
428
|
506 old_gap_size = BUF_GAP_SIZE (buf);
|
|
507
|
|
508 /* Call the newly allocated space a gap at the end of the whole space. */
|
826
|
509 SET_BYTE_BUF_GPT (buf, BYTE_BUF_Z (buf) + BUF_GAP_SIZE (buf));
|
428
|
510 SET_BUF_GAP_SIZE (buf, increment);
|
|
511
|
|
512 SET_BUF_END_GAP_SIZE (buf, 0);
|
|
513
|
|
514 /* Move the new gap down to be consecutive with the end of the old one.
|
|
515 This adjusts the markers properly too. */
|
|
516 gap_left (buf, real_gap_loc + old_gap_size);
|
|
517
|
|
518 /* Now combine the two into one large gap. */
|
|
519 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
|
826
|
520 SET_BYTE_BUF_GPT (buf, real_gap_loc);
|
428
|
521 SET_GAP_SENTINEL (buf);
|
|
522
|
|
523 /* We changed the total size of the buffer (including gap),
|
|
524 so we need to fix up the end sentinel. */
|
|
525 SET_END_SENTINEL (buf);
|
|
526
|
|
527 Vinhibit_quit = tem;
|
|
528 }
|
|
529
|
|
530
|
|
531 /************************************************************************/
|
|
532 /* Before/after-change processing */
|
|
533 /************************************************************************/
|
|
534
|
|
535 /* Those magic changes ... */
|
|
536
|
|
537 static void
|
665
|
538 buffer_signal_changed_region (struct buffer *buf, Charbpos start,
|
|
539 Charbpos end)
|
428
|
540 {
|
|
541 /* The changed region is recorded as the number of unchanged
|
|
542 characters from the beginning and from the end of the
|
|
543 buffer. This obviates much of the need of shifting the
|
|
544 region around to compensate for insertions and deletions.
|
|
545 */
|
|
546 if (buf->changes->begin_unchanged < 0 ||
|
|
547 buf->changes->begin_unchanged > start - BUF_BEG (buf))
|
|
548 buf->changes->begin_unchanged = start - BUF_BEG (buf);
|
|
549 if (buf->changes->end_unchanged < 0 ||
|
|
550 buf->changes->end_unchanged > BUF_Z (buf) - end)
|
|
551 buf->changes->end_unchanged = BUF_Z (buf) - end;
|
|
552 }
|
|
553
|
|
554 void
|
665
|
555 buffer_extent_signal_changed_region (struct buffer *buf, Charbpos start,
|
|
556 Charbpos end)
|
428
|
557 {
|
|
558 if (buf->changes->begin_extent_unchanged < 0 ||
|
|
559 buf->changes->begin_extent_unchanged > start - BUF_BEG (buf))
|
|
560 buf->changes->begin_extent_unchanged = start - BUF_BEG (buf);
|
|
561 if (buf->changes->end_extent_unchanged < 0 ||
|
|
562 buf->changes->end_extent_unchanged > BUF_Z (buf) - end)
|
|
563 buf->changes->end_extent_unchanged = BUF_Z (buf) - end;
|
|
564 }
|
|
565
|
|
566 void
|
|
567 buffer_reset_changes (struct buffer *buf)
|
|
568 {
|
|
569 buf->changes->begin_unchanged = -1;
|
|
570 buf->changes->end_unchanged = -1;
|
|
571 buf->changes->begin_extent_unchanged = -1;
|
|
572 buf->changes->end_extent_unchanged = -1;
|
|
573 buf->changes->newline_was_deleted = 0;
|
|
574 }
|
|
575
|
|
576 static void
|
665
|
577 signal_after_change (struct buffer *buf, Charbpos start, Charbpos orig_end,
|
|
578 Charbpos new_end);
|
428
|
579
|
|
580
|
|
581 /* Call the after-change-functions according to the changes made so far
|
|
582 and treat all further changes as single until the outermost
|
|
583 multiple change exits. This is called when the outermost multiple
|
|
584 change exits and when someone is trying to make a change that violates
|
|
585 the constraints specified in begin_multiple_change(), typically
|
|
586 when nested multiple-change sessions occur. (There are smarter ways of
|
|
587 dealing with nested multiple changes, but these rarely occur so there's
|
|
588 probably no point in it.) */
|
|
589
|
|
590 /* #### This needs to keep track of what actually changed and only
|
|
591 call the after-change functions on that region. */
|
|
592
|
|
593 static void
|
|
594 cancel_multiple_change (struct buffer *buf)
|
|
595 {
|
|
596 /* This function can GC */
|
|
597 /* Call the after-change-functions except when they've already been
|
|
598 called or when there were no changes made to the buffer at all. */
|
|
599 if (buf->text->changes->mc_begin != 0 &&
|
|
600 buf->text->changes->mc_begin_signaled)
|
|
601 {
|
665
|
602 Charbpos real_mc_begin = buf->text->changes->mc_begin;
|
428
|
603 buf->text->changes->mc_begin = 0;
|
|
604
|
|
605 signal_after_change (buf, real_mc_begin, buf->text->changes->mc_orig_end,
|
|
606 buf->text->changes->mc_new_end);
|
|
607 }
|
|
608 else
|
|
609 {
|
|
610 buf->text->changes->mc_begin = 0;
|
|
611 }
|
|
612 }
|
|
613
|
|
614 /* this is an unwind_protect, to ensure that the after-change-functions
|
|
615 get called even in a non-local exit. */
|
|
616
|
|
617 static Lisp_Object
|
|
618 multiple_change_finish_up (Lisp_Object buffer)
|
|
619 {
|
|
620 struct buffer *buf = XBUFFER (buffer);
|
|
621
|
|
622 /* #### I don't know whether or not it should even be possible to
|
|
623 get here with a dead buffer (though given how it is called I can
|
|
624 see how it might be). In any case, there isn't time before 19.14
|
|
625 to find out. */
|
|
626 if (!BUFFER_LIVE_P (buf))
|
|
627 return Qnil;
|
|
628
|
|
629 /* This function can GC */
|
|
630 buf->text->changes->in_multiple_change = 0; /* do this first so that
|
|
631 errors in the after-change
|
|
632 functions don't mess things
|
|
633 up. */
|
|
634 cancel_multiple_change (buf);
|
|
635 return Qnil;
|
|
636 }
|
|
637
|
|
638 /* Call this function when you're about to make a number of buffer changes
|
|
639 that should be considered a single change. (e.g. `replace-match' calls
|
|
640 this.) You need to specify the START and END of the region that is
|
|
641 going to be changed so that the before-change-functions are called
|
|
642 with the correct arguments. The after-change region is calculated
|
|
643 automatically, however, and if changes somehow or other happen outside
|
|
644 of the specified region, that will also be handled correctly.
|
|
645
|
|
646 begin_multiple_change() returns a number (actually a specpdl depth)
|
438
|
647 that you must pass to end_multiple_change() when you are done.
|
|
648
|
|
649 FSF Emacs 20 implements a similar feature, accessible from Lisp
|
|
650 through a `combine-after-change-calls' special form, which is
|
|
651 essentially equivalent to this function. We should consider
|
|
652 whether we want to introduce a similar Lisp form. */
|
428
|
653
|
|
654 int
|
665
|
655 begin_multiple_change (struct buffer *buf, Charbpos start, Charbpos end)
|
428
|
656 {
|
|
657 /* This function can GC */
|
|
658 int count = -1;
|
|
659 if (buf->text->changes->in_multiple_change)
|
|
660 {
|
|
661 if (buf->text->changes->mc_begin != 0 &&
|
|
662 (start < buf->text->changes->mc_begin ||
|
|
663 end > buf->text->changes->mc_new_end))
|
|
664 cancel_multiple_change (buf);
|
|
665 }
|
|
666 else
|
|
667 {
|
|
668 Lisp_Object buffer;
|
|
669
|
|
670 buf->text->changes->mc_begin = start;
|
|
671 buf->text->changes->mc_orig_end = buf->text->changes->mc_new_end = end;
|
|
672 buf->text->changes->mc_begin_signaled = 0;
|
|
673 count = specpdl_depth ();
|
793
|
674 buffer = wrap_buffer (buf);
|
428
|
675 record_unwind_protect (multiple_change_finish_up, buffer);
|
|
676 }
|
|
677 buf->text->changes->in_multiple_change++;
|
|
678 /* We don't call before-change-functions until signal_before_change()
|
|
679 is called, in case there is a read-only or other error. */
|
|
680 return count;
|
|
681 }
|
|
682
|
|
683 void
|
|
684 end_multiple_change (struct buffer *buf, int count)
|
|
685 {
|
|
686 assert (buf->text->changes->in_multiple_change > 0);
|
|
687 buf->text->changes->in_multiple_change--;
|
|
688 if (!buf->text->changes->in_multiple_change)
|
771
|
689 unbind_to (count);
|
428
|
690 }
|
|
691
|
|
692 static int inside_change_hook;
|
|
693
|
|
694 static Lisp_Object
|
|
695 change_function_restore (Lisp_Object buffer)
|
|
696 {
|
|
697 /* We should first reset the variable and then change the buffer,
|
|
698 because Fset_buffer() can throw. */
|
|
699 inside_change_hook = 0;
|
438
|
700 if (XBUFFER (buffer) != current_buffer)
|
|
701 Fset_buffer (buffer);
|
428
|
702 return Qnil;
|
|
703 }
|
|
704
|
|
705 static int in_first_change;
|
|
706
|
|
707 static Lisp_Object
|
|
708 first_change_hook_restore (Lisp_Object buffer)
|
|
709 {
|
|
710 in_first_change = 0;
|
|
711 Fset_buffer (buffer);
|
|
712 return Qnil;
|
|
713 }
|
|
714
|
|
715 /* Signal an initial modification to the buffer. */
|
|
716
|
|
717 static void
|
|
718 signal_first_change (struct buffer *buf)
|
|
719 {
|
|
720 /* This function can GC */
|
793
|
721 Lisp_Object buffer = wrap_buffer (current_buffer);
|
|
722
|
428
|
723
|
|
724 if (!in_first_change)
|
|
725 {
|
|
726 if (!NILP (symbol_value_in_buffer (Qfirst_change_hook, buffer)))
|
|
727 {
|
|
728 int speccount = specpdl_depth ();
|
|
729 record_unwind_protect (first_change_hook_restore, buffer);
|
|
730 set_buffer_internal (buf);
|
|
731 in_first_change = 1;
|
|
732 run_hook (Qfirst_change_hook);
|
771
|
733 unbind_to (speccount);
|
428
|
734 }
|
|
735 }
|
|
736 }
|
|
737
|
|
738 /* Signal a change to the buffer immediately before it happens.
|
|
739 START and END are the bounds of the text to be changed. */
|
|
740
|
|
741 static void
|
665
|
742 signal_before_change (struct buffer *buf, Charbpos start, Charbpos end)
|
428
|
743 {
|
|
744 /* This function can GC */
|
|
745 struct buffer *mbuf;
|
|
746 Lisp_Object bufcons;
|
|
747
|
|
748 if (!inside_change_hook)
|
|
749 {
|
|
750 Lisp_Object buffer;
|
438
|
751 int speccount;
|
428
|
752
|
|
753 /* Are we in a multiple-change session? */
|
|
754 if (buf->text->changes->in_multiple_change &&
|
|
755 buf->text->changes->mc_begin != 0)
|
|
756 {
|
|
757 /* If we're violating the constraints of the session,
|
|
758 call the after-change-functions as necessary for the
|
|
759 changes already made and treat further changes as
|
|
760 single. */
|
|
761 if (start < buf->text->changes->mc_begin ||
|
|
762 end > buf->text->changes->mc_new_end)
|
|
763 cancel_multiple_change (buf);
|
|
764 /* Do nothing if this is not the first change in the session. */
|
|
765 else if (buf->text->changes->mc_begin_signaled)
|
|
766 return;
|
|
767 else
|
|
768 {
|
|
769 /* First time through; call the before-change-functions
|
|
770 specifying the entire region to be changed. (Note that
|
|
771 we didn't call before-change-functions in
|
|
772 begin_multiple_change() because the buffer might be
|
|
773 read-only, etc.) */
|
|
774 start = buf->text->changes->mc_begin;
|
|
775 end = buf->text->changes->mc_new_end;
|
|
776 }
|
|
777 }
|
|
778
|
|
779 /* If buffer is unmodified, run a special hook for that case. */
|
|
780 if (BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
|
|
781 {
|
|
782 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
783 {
|
|
784 signal_first_change (mbuf);
|
|
785 }
|
|
786 }
|
|
787
|
|
788 /* Now in any case run the before-change-functions if any. */
|
438
|
789 speccount = specpdl_depth ();
|
|
790 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
|
|
791 inside_change_hook = 1;
|
428
|
792
|
|
793 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
794 {
|
793
|
795 buffer = wrap_buffer (mbuf);
|
428
|
796 if (!NILP (symbol_value_in_buffer (Qbefore_change_functions, buffer))
|
|
797 /* Obsolete, for compatibility */
|
|
798 || !NILP (symbol_value_in_buffer (Qbefore_change_function, buffer)))
|
|
799 {
|
|
800 set_buffer_internal (buf);
|
|
801 va_run_hook_with_args (Qbefore_change_functions, 2,
|
|
802 make_int (start), make_int (end));
|
|
803 /* Obsolete, for compatibility */
|
|
804 va_run_hook_with_args (Qbefore_change_function, 2,
|
|
805 make_int (start), make_int (end));
|
|
806 }
|
|
807 }
|
|
808
|
438
|
809 /* Make sure endpoints remain valid. before-change-functions
|
|
810 might have modified the buffer. */
|
|
811 if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
|
|
812 if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
|
|
813 if (end < BUF_BEGV (buf)) end = BUF_BEGV (buf);
|
|
814 if (end > BUF_ZV (buf)) end = BUF_ZV (buf);
|
|
815
|
428
|
816 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
817 {
|
826
|
818 report_extent_modification (wrap_buffer (mbuf), start, end, 0);
|
428
|
819 }
|
771
|
820 unbind_to (speccount);
|
428
|
821
|
|
822 /* Only now do we indicate that the before-change-functions have
|
|
823 been called, in case some function throws out. */
|
|
824 buf->text->changes->mc_begin_signaled = 1;
|
|
825 }
|
|
826 }
|
|
827
|
|
828 /* Signal a change immediately after it happens.
|
665
|
829 START is the charbpos of the start of the changed text.
|
|
830 ORIG_END is the charbpos of the end of the before-changed text.
|
|
831 NEW_END is the charbpos of the end of the after-changed text.
|
428
|
832 */
|
|
833
|
|
834 static void
|
665
|
835 signal_after_change (struct buffer *buf, Charbpos start, Charbpos orig_end,
|
|
836 Charbpos new_end)
|
428
|
837 {
|
|
838 /* This function can GC */
|
|
839 struct buffer *mbuf;
|
|
840 Lisp_Object bufcons;
|
|
841
|
|
842 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
843 {
|
|
844 /* always do this. */
|
|
845 buffer_signal_changed_region (mbuf, start, new_end);
|
|
846 }
|
826
|
847 #ifdef USE_C_FONT_LOCK
|
428
|
848 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
849 {
|
|
850 /* #### This seems inefficient. Wouldn't it be better to just
|
|
851 keep one cache per base buffer? */
|
|
852 font_lock_maybe_update_syntactic_caches (mbuf, start, orig_end, new_end);
|
|
853 }
|
826
|
854 #endif /* USE_C_FONT_LOCK */
|
428
|
855
|
|
856 if (!inside_change_hook)
|
|
857 {
|
|
858 Lisp_Object buffer;
|
438
|
859 int speccount;
|
428
|
860
|
|
861 if (buf->text->changes->in_multiple_change &&
|
|
862 buf->text->changes->mc_begin != 0)
|
|
863 {
|
|
864 assert (start >= buf->text->changes->mc_begin &&
|
|
865 start <= buf->text->changes->mc_new_end);
|
|
866 assert (orig_end >= buf->text->changes->mc_begin &&
|
|
867 orig_end <= buf->text->changes->mc_new_end);
|
|
868 buf->text->changes->mc_new_end += new_end - orig_end;
|
|
869 return; /* after-change-functions signalled when all changes done */
|
|
870 }
|
|
871
|
438
|
872 speccount = specpdl_depth ();
|
|
873 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
|
|
874 inside_change_hook = 1;
|
428
|
875 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
876 {
|
793
|
877 buffer = wrap_buffer (mbuf);
|
428
|
878
|
|
879 if (!NILP (symbol_value_in_buffer (Qafter_change_functions, buffer))
|
|
880 /* Obsolete, for compatibility */
|
|
881 || !NILP (symbol_value_in_buffer (Qafter_change_function, buffer)))
|
|
882 {
|
|
883 set_buffer_internal (buf);
|
|
884 /* The actual after-change functions take slightly
|
|
885 different arguments than what we were passed. */
|
|
886 va_run_hook_with_args (Qafter_change_functions, 3,
|
|
887 make_int (start), make_int (new_end),
|
|
888 make_int (orig_end - start));
|
|
889 /* Obsolete, for compatibility */
|
|
890 va_run_hook_with_args (Qafter_change_function, 3,
|
|
891 make_int (start), make_int (new_end),
|
|
892 make_int (orig_end - start));
|
|
893 }
|
|
894 }
|
|
895
|
438
|
896 /* Make sure endpoints remain valid. after-change-functions
|
|
897 might have modified the buffer. */
|
|
898 if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
|
|
899 if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
|
|
900 if (new_end < BUF_BEGV (buf)) new_end = BUF_BEGV (buf);
|
|
901 if (new_end > BUF_ZV (buf)) new_end = BUF_ZV (buf);
|
|
902 if (orig_end < BUF_BEGV (buf)) orig_end = BUF_BEGV (buf);
|
|
903 if (orig_end > BUF_ZV (buf)) orig_end = BUF_ZV (buf);
|
|
904
|
428
|
905 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
906 {
|
793
|
907 buffer = wrap_buffer (mbuf);
|
438
|
908 report_extent_modification (buffer, start, new_end, 1);
|
428
|
909 }
|
771
|
910 unbind_to (speccount); /* sets inside_change_hook back to 0 */
|
428
|
911 }
|
|
912 }
|
|
913
|
|
914 /* Call this if you're about to change the region of BUFFER from START
|
|
915 to END. This checks the read-only properties of the region, calls
|
|
916 the necessary modification hooks, and warns the next redisplay that
|
|
917 it should pay attention to that area. */
|
|
918
|
|
919 static void
|
665
|
920 prepare_to_modify_buffer (struct buffer *buf, Charbpos start, Charbpos end,
|
428
|
921 int lockit)
|
|
922 {
|
|
923 /* This function can GC */
|
|
924 /* dmoore - This function can also kill the buffer buf, the current
|
|
925 buffer, and do anything it pleases. So if you call it, be
|
|
926 careful. */
|
|
927 struct buffer *mbuf;
|
|
928 Lisp_Object buffer, bufcons;
|
|
929 struct gcpro gcpro1;
|
|
930
|
|
931 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
932 {
|
|
933 barf_if_buffer_read_only (mbuf, start, end);
|
|
934 }
|
|
935
|
|
936 /* if this is the first modification, see about locking the buffer's
|
|
937 file */
|
793
|
938 buffer = wrap_buffer (buf);
|
428
|
939 GCPRO1 (buffer);
|
|
940 if (!NILP (buf->filename) && lockit &&
|
|
941 BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
|
|
942 {
|
758
|
943 #ifdef CLASH_DETECTION
|
|
944 if (!NILP (buf->file_truename))
|
|
945 /* Make binding buffer-file-name to nil effective. */
|
|
946 lock_file (buf->file_truename);
|
|
947 #else
|
428
|
948 /* At least warn if this file has changed on disk since it was visited.*/
|
|
949 if (NILP (Fverify_visited_file_modtime (buffer))
|
|
950 && !NILP (Ffile_exists_p (buf->filename)))
|
|
951 call1_in_buffer (buf, intern ("ask-user-about-supersession-threat"),
|
|
952 buf->filename);
|
|
953 #endif /* not CLASH_DETECTION */
|
|
954 }
|
|
955 UNGCPRO;
|
|
956
|
|
957 /* #### dmoore - is this reasonable in case of buf being killed above? */
|
|
958 if (!BUFFER_LIVE_P (buf))
|
|
959 return;
|
|
960
|
|
961 signal_before_change (buf, start, end);
|
|
962
|
|
963 #ifdef REGION_CACHE_NEEDS_WORK
|
|
964 if (buf->newline_cache)
|
|
965 invalidate_region_cache (buf,
|
|
966 buf->newline_cache,
|
|
967 start - BUF_BEG (buf), BUF_Z (buf) - end);
|
|
968 if (buf->width_run_cache)
|
|
969 invalidate_region_cache (buf,
|
|
970 buf->width_run_cache,
|
|
971 start - BUF_BEG (buf), BUF_Z (buf) - end);
|
|
972 #endif
|
|
973
|
|
974 #if 0 /* FSFmacs */
|
|
975 Vdeactivate_mark = Qt;
|
|
976 #endif
|
|
977
|
|
978 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
979 {
|
|
980 mbuf->point_before_scroll = Qnil;
|
|
981 }
|
|
982 }
|
|
983
|
|
984
|
|
985 /************************************************************************/
|
|
986 /* Insertion of strings */
|
|
987 /************************************************************************/
|
|
988
|
|
989 void
|
665
|
990 fixup_internal_substring (const Intbyte *nonreloc, Lisp_Object reloc,
|
428
|
991 Bytecount offset, Bytecount *len)
|
|
992 {
|
|
993 assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc)));
|
|
994
|
|
995 if (*len < 0)
|
|
996 {
|
|
997 if (nonreloc)
|
442
|
998 *len = strlen ((const char *) nonreloc) - offset;
|
428
|
999 else
|
|
1000 *len = XSTRING_LENGTH (reloc) - offset;
|
|
1001 }
|
800
|
1002 #ifdef ERROR_CHECK_TEXT
|
428
|
1003 assert (*len >= 0);
|
|
1004 if (STRINGP (reloc))
|
|
1005 {
|
|
1006 assert (offset >= 0 && offset <= XSTRING_LENGTH (reloc));
|
|
1007 assert (offset + *len <= XSTRING_LENGTH (reloc));
|
|
1008 }
|
|
1009 #endif
|
|
1010 }
|
|
1011
|
665
|
1012 /* Insert a string into BUF at Charbpos POS. The string data comes
|
428
|
1013 from one of two sources: constant, non-relocatable data (specified
|
|
1014 in NONRELOC), or a Lisp string object (specified in RELOC), which
|
|
1015 is relocatable and may have extent data that needs to be copied
|
|
1016 into the buffer. OFFSET and LENGTH specify the substring of the
|
|
1017 data that is actually to be inserted. As a special case, if POS
|
|
1018 is -1, insert the string at point and move point to the end of the
|
|
1019 string.
|
|
1020
|
|
1021 Normally, markers at the insertion point end up before the
|
|
1022 inserted string. If INSDEL_BEFORE_MARKERS is set in flags, however,
|
|
1023 they end up after the string.
|
|
1024
|
|
1025 INSDEL_NO_LOCKING is kludgy and is used when insert-file-contents is
|
|
1026 visiting a new file; it inhibits the locking checks normally done
|
|
1027 before modifying a buffer. Similar checks were already done
|
|
1028 in the higher-level Lisp functions calling insert-file-contents. */
|
|
1029
|
|
1030 Charcount
|
665
|
1031 buffer_insert_string_1 (struct buffer *buf, Charbpos pos,
|
|
1032 const Intbyte *nonreloc, Lisp_Object reloc,
|
428
|
1033 Bytecount offset, Bytecount length,
|
|
1034 int flags)
|
|
1035 {
|
|
1036 /* This function can GC */
|
|
1037 struct gcpro gcpro1;
|
826
|
1038 Bytebpos bytepos;
|
|
1039 Bytecount length_in_buffer;
|
428
|
1040 Charcount cclen;
|
|
1041 int move_point = 0;
|
|
1042 struct buffer *mbuf;
|
|
1043 Lisp_Object bufcons;
|
|
1044
|
|
1045 /* Defensive steps just in case a buffer gets deleted and a calling
|
|
1046 function doesn't notice it. */
|
|
1047 if (!BUFFER_LIVE_P (buf))
|
|
1048 return 0;
|
|
1049
|
|
1050 fixup_internal_substring (nonreloc, reloc, offset, &length);
|
|
1051
|
|
1052 if (pos == -1)
|
|
1053 {
|
|
1054 pos = BUF_PT (buf);
|
|
1055 move_point = 1;
|
|
1056 }
|
|
1057
|
|
1058 #ifdef I18N3
|
|
1059 /* #### See the comment in print_internal(). If this buffer is marked
|
|
1060 as translatable, then Fgettext() should be called on obj if it
|
|
1061 is a string. */
|
|
1062 #endif
|
|
1063
|
|
1064 /* Make sure that point-max won't exceed the size of an emacs int. */
|
|
1065 if ((length + BUF_Z (buf)) > EMACS_INT_MAX)
|
563
|
1066 out_of_memory ("Maximum buffer size exceeded", Qunbound);
|
428
|
1067
|
|
1068 /* theoretically not necessary -- caller should GCPRO.
|
|
1069 #### buffer_insert_from_buffer_1() doesn't! */
|
|
1070 GCPRO1 (reloc);
|
|
1071
|
|
1072 prepare_to_modify_buffer (buf, pos, pos, !(flags & INSDEL_NO_LOCKING));
|
|
1073
|
|
1074 /* Defensive steps in case the before-change-functions fuck around */
|
|
1075 if (!BUFFER_LIVE_P (buf))
|
|
1076 {
|
|
1077 UNGCPRO;
|
|
1078 /* Bad bad pre-change function. */
|
|
1079 return 0;
|
|
1080 }
|
|
1081
|
|
1082 /* Make args be valid again. prepare_to_modify_buffer() might have
|
|
1083 modified the buffer. */
|
|
1084 if (pos < BUF_BEGV (buf))
|
|
1085 pos = BUF_BEGV (buf);
|
|
1086 if (pos > BUF_ZV (buf))
|
|
1087 pos = BUF_ZV (buf);
|
|
1088
|
826
|
1089 bytepos = charbpos_to_bytebpos (buf, pos);
|
771
|
1090
|
428
|
1091 /* string may have been relocated up to this point */
|
|
1092 if (STRINGP (reloc))
|
771
|
1093 {
|
793
|
1094 cclen = string_offset_byte_to_char_len (reloc, offset, length);
|
771
|
1095 nonreloc = XSTRING_DATA (reloc);
|
|
1096 }
|
|
1097 else
|
|
1098 cclen = bytecount_to_charcount (nonreloc + offset, length);
|
826
|
1099 /* &&#### Here we check if the text can't fit into the format of the buffer,
|
|
1100 and if so convert it to another format (either default or 32-bit-fixed,
|
|
1101 according to some flag; if no flag, use default). */
|
|
1102
|
|
1103 length_in_buffer = copy_text_between_formats (nonreloc + offset, length,
|
|
1104 FORMAT_DEFAULT,
|
|
1105 STRINGP (reloc) ? reloc : Qnil,
|
|
1106 NULL, 0,
|
|
1107 BUF_FORMAT (buf),
|
|
1108 wrap_buffer (buf),
|
|
1109 NULL);
|
428
|
1110
|
826
|
1111 if (bytepos != BYTE_BUF_GPT (buf))
|
428
|
1112 /* #### if debug-on-quit is invoked and the user changes the
|
|
1113 buffer, bad things can happen. This is a rampant problem
|
|
1114 in Emacs. */
|
826
|
1115 move_gap (buf, bytepos); /* may QUIT */
|
|
1116 if (! GAP_CAN_HOLD_SIZE_P (buf, length_in_buffer))
|
428
|
1117 {
|
826
|
1118 if (BUF_END_GAP_SIZE (buf) >= length_in_buffer)
|
428
|
1119 merge_gap_with_end_gap (buf);
|
|
1120 else
|
826
|
1121 make_gap (buf, length_in_buffer - BUF_GAP_SIZE (buf));
|
428
|
1122 }
|
|
1123
|
826
|
1124 /* At this point, no more QUITting or processing of Lisp code. Buffer is
|
|
1125 in a consistent state. Following code puts buffer in an inconsistent
|
|
1126 state and can be considered a "critical section". */
|
|
1127
|
428
|
1128 insert_invalidate_line_number_cache (buf, pos, nonreloc + offset, length);
|
|
1129
|
|
1130 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1131 {
|
|
1132 record_insert (mbuf, pos, cclen);
|
|
1133 }
|
|
1134
|
|
1135 BUF_MODIFF (buf)++;
|
|
1136 MARK_BUFFERS_CHANGED;
|
|
1137
|
826
|
1138 /* string may have been relocated up to this point #### if string is
|
|
1139 modified during quit processing, bad things can happen. */
|
428
|
1140 if (STRINGP (reloc))
|
|
1141 nonreloc = XSTRING_DATA (reloc);
|
|
1142
|
826
|
1143 copy_text_between_formats (nonreloc + offset, length, FORMAT_DEFAULT,
|
|
1144 STRINGP (reloc) ? reloc : Qnil,
|
|
1145 BUF_GPT_ADDR (buf), length_in_buffer,
|
|
1146 BUF_FORMAT (buf), wrap_buffer (buf), NULL);
|
|
1147
|
|
1148 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) - length_in_buffer);
|
|
1149 SET_BYTE_BUF_GPT (buf, BYTE_BUF_GPT (buf) + length_in_buffer);
|
428
|
1150 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1151 {
|
826
|
1152 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) + cclen,
|
|
1153 BYTE_BUF_ZV (mbuf) + length_in_buffer);
|
428
|
1154 }
|
826
|
1155 SET_BOTH_BUF_Z (buf, BUF_Z (buf) + cclen, BYTE_BUF_Z (buf) + length_in_buffer);
|
428
|
1156 SET_GAP_SENTINEL (buf);
|
771
|
1157
|
|
1158
|
428
|
1159 #ifdef MULE
|
826
|
1160 buffer_mule_signal_inserted_region (buf, pos, length_in_buffer, cclen);
|
|
1161 /* Update our count of ASCII, 8-bit and 16-bit chars and the
|
|
1162 entirely-one-byte flag */
|
|
1163 {
|
|
1164 const Intbyte *ptr = nonreloc + offset;
|
|
1165 const Intbyte *ptrend = ptr + length;
|
|
1166
|
|
1167 while (ptr < ptrend)
|
|
1168 {
|
|
1169 Emchar ch = charptr_emchar (ptr);
|
|
1170 if (emchar_ascii_p (ch))
|
|
1171 buf->text->num_ascii_chars++;
|
|
1172 if (emchar_8_bit_fixed_p (ch, wrap_buffer (buf)))
|
|
1173 buf->text->num_8_bit_fixed_chars++;
|
|
1174 if (emchar_16_bit_fixed_p (ch, wrap_buffer (buf)))
|
|
1175 buf->text->num_16_bit_fixed_chars++;
|
|
1176 INC_CHARPTR (ptr);
|
|
1177 }
|
|
1178
|
|
1179 buf->text->entirely_one_byte_p =
|
|
1180 (BUF_FORMAT (buf) == FORMAT_8_BIT_FIXED ||
|
|
1181 (BUF_FORMAT (buf) == FORMAT_DEFAULT && BUF_Z (buf) == BYTE_BUF_Z (buf)));
|
|
1182 }
|
428
|
1183 #endif
|
|
1184
|
|
1185 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1186 {
|
826
|
1187 process_extents_for_insertion (wrap_buffer (mbuf), bytepos,
|
|
1188 length_in_buffer);
|
428
|
1189 }
|
|
1190
|
|
1191 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1192 {
|
826
|
1193 /* We know the gap is at BYTEPOS so the cast is OK. */
|
|
1194 adjust_markers_for_insert (mbuf, (Membpos) bytepos, length_in_buffer);
|
428
|
1195 }
|
|
1196
|
|
1197 /* Point logically doesn't move, but may need to be adjusted because
|
|
1198 it's a byte index. point-marker doesn't change because it's a
|
|
1199 memory index. */
|
|
1200 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1201 {
|
826
|
1202 if (BYTE_BUF_PT (mbuf) > bytepos)
|
428
|
1203 JUST_SET_POINT (mbuf, BUF_PT (mbuf) + cclen,
|
826
|
1204 BYTE_BUF_PT (mbuf) + length_in_buffer);
|
428
|
1205 }
|
|
1206
|
|
1207 /* Well, point might move. */
|
|
1208 if (move_point)
|
826
|
1209 BYTE_BUF_SET_PT (buf, bytepos + length_in_buffer);
|
428
|
1210
|
|
1211 if (STRINGP (reloc))
|
|
1212 {
|
|
1213 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1214 {
|
826
|
1215 splice_in_string_extents (reloc, mbuf, bytepos, length, offset);
|
428
|
1216 }
|
|
1217 }
|
|
1218
|
|
1219 if (flags & INSDEL_BEFORE_MARKERS)
|
|
1220 {
|
|
1221 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1222 {
|
826
|
1223 /* bytepos - 1 is correct because the FROM argument is exclusive.
|
665
|
1224 I formerly used DEC_BYTEBPOS() but that caused problems at the
|
428
|
1225 beginning of the buffer. */
|
826
|
1226 adjust_markers (mbuf, bytepos - 1, bytepos, length_in_buffer);
|
428
|
1227 }
|
|
1228 }
|
|
1229
|
826
|
1230 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1231 {
|
|
1232 signal_syntax_table_extent_adjust (mbuf);
|
|
1233 }
|
|
1234
|
428
|
1235 signal_after_change (buf, pos, pos, pos + cclen);
|
|
1236
|
|
1237 UNGCPRO;
|
|
1238
|
|
1239 return cclen;
|
|
1240 }
|
|
1241
|
|
1242
|
|
1243 /* The following functions are interfaces onto the above function,
|
|
1244 for inserting particular sorts of data. In all the functions,
|
|
1245 BUF and POS specify the buffer and location where the insertion is
|
|
1246 to take place. (If POS is -1, text is inserted at point and point
|
|
1247 moves forward past the text.) FLAGS is as above. */
|
|
1248
|
|
1249 Charcount
|
665
|
1250 buffer_insert_raw_string_1 (struct buffer *buf, Charbpos pos,
|
|
1251 const Intbyte *nonreloc, Bytecount length,
|
428
|
1252 int flags)
|
|
1253 {
|
|
1254 /* This function can GC */
|
|
1255 return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length,
|
|
1256 flags);
|
|
1257 }
|
|
1258
|
|
1259 Charcount
|
665
|
1260 buffer_insert_lisp_string_1 (struct buffer *buf, Charbpos pos, Lisp_Object str,
|
428
|
1261 int flags)
|
|
1262 {
|
|
1263 /* This function can GC */
|
|
1264 return buffer_insert_string_1 (buf, pos, 0, str, 0,
|
|
1265 XSTRING_LENGTH (str),
|
|
1266 flags);
|
|
1267 }
|
|
1268
|
|
1269 /* Insert the null-terminated string S (in external format). */
|
|
1270
|
|
1271 Charcount
|
665
|
1272 buffer_insert_c_string_1 (struct buffer *buf, Charbpos pos, const char *s,
|
428
|
1273 int flags)
|
|
1274 {
|
|
1275 /* This function can GC */
|
442
|
1276 const char *translated = GETTEXT (s);
|
665
|
1277 return buffer_insert_string_1 (buf, pos, (const Intbyte *) translated, Qnil,
|
428
|
1278 0, strlen (translated), flags);
|
|
1279 }
|
|
1280
|
|
1281 Charcount
|
665
|
1282 buffer_insert_emacs_char_1 (struct buffer *buf, Charbpos pos, Emchar ch,
|
428
|
1283 int flags)
|
|
1284 {
|
|
1285 /* This function can GC */
|
665
|
1286 Intbyte str[MAX_EMCHAR_LEN];
|
428
|
1287 Bytecount len = set_charptr_emchar (str, ch);
|
|
1288 return buffer_insert_string_1 (buf, pos, str, Qnil, 0, len, flags);
|
|
1289 }
|
|
1290
|
|
1291 Charcount
|
665
|
1292 buffer_insert_c_char_1 (struct buffer *buf, Charbpos pos, char c,
|
428
|
1293 int flags)
|
|
1294 {
|
|
1295 /* This function can GC */
|
|
1296 return buffer_insert_emacs_char_1 (buf, pos, (Emchar) (unsigned char) c,
|
|
1297 flags);
|
|
1298 }
|
|
1299
|
|
1300 Charcount
|
665
|
1301 buffer_insert_from_buffer_1 (struct buffer *buf, Charbpos pos,
|
|
1302 struct buffer *buf2, Charbpos pos2,
|
428
|
1303 Charcount length, int flags)
|
|
1304 {
|
|
1305 /* This function can GC */
|
|
1306 Lisp_Object str = make_string_from_buffer (buf2, pos2, length);
|
|
1307 return buffer_insert_string_1 (buf, pos, 0, str, 0,
|
|
1308 XSTRING_LENGTH (str), flags);
|
|
1309 }
|
|
1310
|
|
1311
|
|
1312 /************************************************************************/
|
|
1313 /* Deletion of ranges */
|
|
1314 /************************************************************************/
|
|
1315
|
|
1316 /* Delete characters in buffer from FROM up to (but not including) TO. */
|
|
1317
|
|
1318 void
|
665
|
1319 buffer_delete_range (struct buffer *buf, Charbpos from, Charbpos to, int flags)
|
428
|
1320 {
|
|
1321 /* This function can GC */
|
|
1322 Charcount numdel;
|
826
|
1323 Bytebpos byte_from, byte_to;
|
|
1324 Bytecount byte_numdel;
|
428
|
1325 EMACS_INT shortage;
|
|
1326 struct buffer *mbuf;
|
|
1327 Lisp_Object bufcons;
|
826
|
1328 int do_move_gap = 0;
|
428
|
1329
|
|
1330 /* Defensive steps just in case a buffer gets deleted and a calling
|
|
1331 function doesn't notice it. */
|
|
1332 if (!BUFFER_LIVE_P (buf))
|
|
1333 return;
|
|
1334
|
|
1335 /* Make args be valid */
|
|
1336 if (from < BUF_BEGV (buf))
|
|
1337 from = BUF_BEGV (buf);
|
|
1338 if (to > BUF_ZV (buf))
|
|
1339 to = BUF_ZV (buf);
|
|
1340 if ((numdel = to - from) <= 0)
|
|
1341 return;
|
|
1342
|
|
1343 prepare_to_modify_buffer (buf, from, to, !(flags & INSDEL_NO_LOCKING));
|
|
1344
|
|
1345 /* Defensive steps in case the before-change-functions fuck around */
|
|
1346 if (!BUFFER_LIVE_P (buf))
|
|
1347 /* Bad bad pre-change function. */
|
|
1348 return;
|
|
1349
|
|
1350 /* Make args be valid again. prepare_to_modify_buffer() might have
|
|
1351 modified the buffer. */
|
|
1352 if (from < BUF_BEGV (buf))
|
|
1353 from = BUF_BEGV (buf);
|
|
1354 if (to > BUF_ZV (buf))
|
|
1355 to = BUF_ZV (buf);
|
|
1356 if ((numdel = to - from) <= 0)
|
|
1357 return;
|
|
1358
|
826
|
1359 byte_from = charbpos_to_bytebpos (buf, from);
|
|
1360 byte_to = charbpos_to_bytebpos (buf, to);
|
|
1361 byte_numdel = byte_to - byte_from;
|
|
1362
|
|
1363 if (to == BUF_Z (buf) &&
|
|
1364 byte_from > BYTE_BUF_GPT (buf))
|
|
1365 /* avoid moving the gap just to delete from the bottom. */
|
|
1366 do_move_gap = 0;
|
|
1367 else
|
|
1368 {
|
|
1369 /* Make sure the gap is somewhere in or next to what we are deleting. */
|
|
1370 /* NOTE: Can QUIT! */
|
|
1371 if (byte_to < BYTE_BUF_GPT (buf))
|
|
1372 gap_left (buf, byte_to);
|
|
1373 if (byte_from > BYTE_BUF_GPT (buf))
|
|
1374 gap_right (buf, byte_from);
|
|
1375 do_move_gap = 1;
|
|
1376 }
|
|
1377
|
|
1378 /* At this point, no more QUITting or processing of Lisp code. Buffer is
|
|
1379 in a consistent state. Following code puts buffer in an inconsistent
|
|
1380 state and can be considered a "critical section". */
|
|
1381
|
|
1382 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1383 {
|
|
1384 record_delete (mbuf, from, numdel);
|
|
1385 }
|
|
1386 BUF_MODIFF (buf)++;
|
|
1387 MARK_BUFFERS_CHANGED;
|
|
1388
|
|
1389 /* We used to do the following before the gap move. But that might QUIT,
|
|
1390 and (as a result of this) the gap code always leaves the buffer in
|
|
1391 a consistent state. Therefore, it's totally safe to do these operations
|
|
1392 now, and just as well not before, as we're making state changes
|
|
1393 related to the deletion. */
|
|
1394
|
428
|
1395 /* Redisplay needs to know if a newline was in the deleted region.
|
|
1396 If we've already marked the changed region as having a deleted
|
|
1397 newline there is no use in performing the check. */
|
|
1398 if (!buf->changes->newline_was_deleted)
|
|
1399 {
|
|
1400 scan_buffer (buf, '\n', from, to, 1, &shortage, 1);
|
|
1401 if (!shortage)
|
|
1402 {
|
|
1403 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1404 {
|
|
1405 mbuf->changes->newline_was_deleted = 1;
|
|
1406 }
|
|
1407 }
|
|
1408 }
|
|
1409
|
|
1410 delete_invalidate_line_number_cache (buf, from, to);
|
|
1411
|
826
|
1412 #ifdef MULE
|
|
1413 /* Update our count of ASCII, 8-bit and 16-bit chars and the
|
|
1414 entirely-one-byte flag */
|
|
1415 {
|
|
1416 Bytebpos i;
|
428
|
1417
|
826
|
1418 for (i = byte_from; i < byte_to; i = next_bytebpos (buf, i))
|
|
1419 {
|
|
1420 Emchar ch = BYTE_BUF_FETCH_CHAR (buf, i);
|
|
1421 if (emchar_ascii_p (ch))
|
|
1422 buf->text->num_ascii_chars--;
|
|
1423 if (emchar_8_bit_fixed_p (ch, wrap_buffer (buf)))
|
|
1424 buf->text->num_8_bit_fixed_chars--;
|
|
1425 if (emchar_16_bit_fixed_p (ch, wrap_buffer (buf)))
|
|
1426 buf->text->num_16_bit_fixed_chars--;
|
|
1427 }
|
|
1428 }
|
|
1429 #endif /* MULE */
|
428
|
1430
|
826
|
1431 /* #### Point used to be modified here, but this causes problems
|
|
1432 with MULE, as point is used to calculate bytebpos's, and if the
|
|
1433 offset in byte_numdel causes point to move to a non first-byte
|
|
1434 location, causing some other function to throw an assertion
|
|
1435 in ASSERT_VALID_BYTEBPOS. I've moved the code to right after
|
|
1436 the other movements and adjustments, but before the gap is
|
|
1437 moved. -- jh 970813 */
|
428
|
1438
|
826
|
1439 /* Detach any extents that are completely within the range [FROM, TO],
|
|
1440 if the extents are detachable.
|
|
1441
|
|
1442 This must come AFTER record_delete(), so that the appropriate extents
|
|
1443 will be present to be recorded, and BEFORE the gap size is increased,
|
|
1444 as otherwise we will be confused about where the extents end. */
|
|
1445 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1446 {
|
|
1447 process_extents_for_deletion (wrap_buffer (mbuf), byte_from, byte_to, 0);
|
428
|
1448 }
|
|
1449
|
826
|
1450 /* Relocate all markers pointing into the new, larger gap to
|
|
1451 point at the end of the text before the gap. */
|
|
1452 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1453 {
|
|
1454 adjust_markers (mbuf,
|
|
1455 (byte_to + BUF_GAP_SIZE (mbuf)),
|
|
1456 (byte_to + BUF_GAP_SIZE (mbuf)),
|
|
1457 (- byte_numdel -
|
|
1458 (do_move_gap ? BUF_GAP_SIZE (mbuf) : 0)));
|
|
1459 }
|
|
1460
|
|
1461 /* Relocate any extent endpoints just like markers. */
|
|
1462 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1463 {
|
|
1464 adjust_extents_for_deletion (wrap_buffer (mbuf), byte_from, byte_to,
|
|
1465 BUF_GAP_SIZE (mbuf),
|
|
1466 byte_numdel,
|
|
1467 do_move_gap ? BUF_GAP_SIZE (mbuf) : 0);
|
|
1468 }
|
|
1469
|
|
1470 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1471 {
|
|
1472 /* Relocate point as if it were a marker. */
|
|
1473 if (byte_from < BYTE_BUF_PT (mbuf))
|
|
1474 {
|
|
1475 if (BYTE_BUF_PT (mbuf) < byte_to)
|
|
1476 JUST_SET_POINT (mbuf, from, byte_from);
|
|
1477 else
|
|
1478 JUST_SET_POINT (mbuf, BUF_PT (mbuf) - numdel,
|
|
1479 BYTE_BUF_PT (mbuf) - byte_numdel);
|
|
1480 }
|
|
1481 }
|
|
1482
|
|
1483 if (do_move_gap)
|
|
1484 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + byte_numdel);
|
|
1485 else
|
|
1486 SET_BUF_END_GAP_SIZE (buf, BUF_END_GAP_SIZE (buf) + byte_numdel);
|
|
1487 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1488 {
|
|
1489 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) - numdel,
|
|
1490 BYTE_BUF_ZV (mbuf) - byte_numdel);
|
|
1491 }
|
|
1492 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BYTE_BUF_Z (buf) - byte_numdel);
|
|
1493 if (do_move_gap)
|
|
1494 SET_BYTE_BUF_GPT (buf, byte_from);
|
|
1495 SET_GAP_SENTINEL (buf);
|
|
1496
|
428
|
1497 #ifdef MULE
|
826
|
1498 buffer_mule_signal_deleted_region (buf, from, to, byte_from, byte_to);
|
|
1499 buf->text->entirely_one_byte_p =
|
|
1500 (BUF_FORMAT (buf) == FORMAT_8_BIT_FIXED ||
|
|
1501 (BUF_FORMAT (buf) == FORMAT_DEFAULT && BUF_Z (buf) == BYTE_BUF_Z (buf)));
|
428
|
1502 #endif
|
|
1503
|
|
1504 #ifdef ERROR_CHECK_EXTENTS
|
|
1505 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1506 {
|
771
|
1507 sledgehammer_extent_check (wrap_buffer (mbuf));
|
428
|
1508 }
|
|
1509 #endif
|
|
1510
|
826
|
1511 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1512 {
|
|
1513 signal_syntax_table_extent_adjust (mbuf);
|
|
1514 }
|
|
1515
|
|
1516 /* &&#### Here we consider converting the buffer from default to
|
|
1517 8-bit-fixed if is entirely 8-bit-fixed chars and has been that way for
|
|
1518 a long time, e.g. 20 minutes. And if the buffer just switched to all
|
|
1519 8-bit-fixed chars, start the timer. */
|
428
|
1520 signal_after_change (buf, from, to, from);
|
|
1521 }
|
|
1522
|
|
1523
|
|
1524 /************************************************************************/
|
|
1525 /* Replacement of characters */
|
|
1526 /************************************************************************/
|
|
1527
|
|
1528 /* Replace the character at POS in buffer B with CH. */
|
|
1529
|
|
1530 void
|
665
|
1531 buffer_replace_char (struct buffer *buf, Charbpos pos, Emchar ch,
|
428
|
1532 int not_real_change, int force_lock_check)
|
|
1533 {
|
|
1534 /* This function can GC */
|
665
|
1535 Intbyte newstr[MAX_EMCHAR_LEN];
|
826
|
1536 Bytecount newlen;
|
|
1537 Emchar oldch;
|
428
|
1538
|
|
1539 /* Defensive steps just in case a buffer gets deleted and a calling
|
|
1540 function doesn't notice it. */
|
|
1541 if (!BUFFER_LIVE_P (buf))
|
|
1542 return;
|
|
1543
|
826
|
1544 newlen = set_charptr_emchar_fmt (newstr, ch, BUF_FORMAT (buf),
|
|
1545 wrap_buffer (buf));
|
|
1546 oldch = BUF_FETCH_CHAR (buf, pos);
|
|
1547 if (emchar_fits_in_format (ch, BUF_FORMAT (buf), wrap_buffer (buf)) &&
|
|
1548 newlen == emchar_len_fmt (oldch, BUF_FORMAT (buf)))
|
428
|
1549 {
|
|
1550 struct buffer *mbuf;
|
|
1551 Lisp_Object bufcons;
|
|
1552
|
|
1553 /* then we can just replace the text. */
|
|
1554 prepare_to_modify_buffer (buf, pos, pos + 1,
|
|
1555 !not_real_change || force_lock_check);
|
|
1556 /* Defensive steps in case the before-change-functions fuck around */
|
|
1557 if (!BUFFER_LIVE_P (buf))
|
|
1558 /* Bad bad pre-change function. */
|
|
1559 return;
|
|
1560
|
|
1561 /* Make args be valid again. prepare_to_modify_buffer() might have
|
|
1562 modified the buffer. */
|
|
1563 if (pos < BUF_BEGV (buf))
|
|
1564 pos = BUF_BEGV (buf);
|
|
1565 if (pos >= BUF_ZV (buf))
|
|
1566 pos = BUF_ZV (buf) - 1;
|
|
1567 if (pos < BUF_BEGV (buf))
|
|
1568 /* no more characters in buffer! */
|
|
1569 return;
|
|
1570
|
|
1571 if (BUF_FETCH_CHAR (buf, pos) == '\n')
|
|
1572 {
|
|
1573 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1574 {
|
|
1575 mbuf->changes->newline_was_deleted = 1;
|
|
1576 }
|
|
1577 }
|
|
1578 MARK_BUFFERS_CHANGED;
|
|
1579 if (!not_real_change)
|
|
1580 {
|
|
1581 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
|
|
1582 {
|
|
1583 record_change (mbuf, pos, 1);
|
|
1584 }
|
|
1585 BUF_MODIFF (buf)++;
|
|
1586 }
|
826
|
1587
|
|
1588 #ifdef MULE
|
|
1589 if (emchar_ascii_p (oldch))
|
|
1590 buf->text->num_ascii_chars--;
|
|
1591 if (emchar_8_bit_fixed_p (oldch, wrap_buffer (buf)))
|
|
1592 buf->text->num_8_bit_fixed_chars--;
|
|
1593 if (emchar_16_bit_fixed_p (oldch, wrap_buffer (buf)))
|
|
1594 buf->text->num_16_bit_fixed_chars--;
|
|
1595 if (emchar_ascii_p (ch))
|
|
1596 buf->text->num_ascii_chars++;
|
|
1597 if (emchar_8_bit_fixed_p (ch, wrap_buffer (buf)))
|
|
1598 buf->text->num_8_bit_fixed_chars++;
|
|
1599 if (emchar_16_bit_fixed_p (ch, wrap_buffer (buf)))
|
|
1600 buf->text->num_16_bit_fixed_chars++;
|
|
1601 #endif /* MULE */
|
|
1602
|
428
|
1603 memcpy (BUF_BYTE_ADDRESS (buf, pos), newstr, newlen);
|
|
1604
|
|
1605 signal_after_change (buf, pos, pos + 1, pos + 1);
|
|
1606
|
|
1607 /* We do not have to adjust the Mule data; we just replaced a
|
|
1608 character with another of the same number of bytes. */
|
|
1609 }
|
|
1610 else
|
|
1611 {
|
|
1612 /*
|
|
1613 * Must implement as deletion followed by insertion.
|
|
1614 *
|
|
1615 * Make a note to move point forward later in the one situation
|
|
1616 * where it is needed, a delete/insert one position behind
|
|
1617 * point. Point will drift backward by one position and stay
|
|
1618 * there otherwise.
|
|
1619 */
|
|
1620 int movepoint = (pos == BUF_PT (buf) - 1);
|
|
1621
|
|
1622 buffer_delete_range (buf, pos, pos + 1, 0);
|
|
1623 /* Defensive steps in case the before-change-functions fuck around */
|
|
1624 if (!BUFFER_LIVE_P (buf))
|
|
1625 /* Bad bad pre-change function. */
|
|
1626 return;
|
|
1627
|
|
1628 /* Make args be valid again. prepare_to_modify_buffer() might have
|
|
1629 modified the buffer. */
|
|
1630 if (pos < BUF_BEGV (buf))
|
|
1631 pos = BUF_BEGV (buf);
|
|
1632 if (pos >= BUF_ZV (buf))
|
|
1633 pos = BUF_ZV (buf) - 1;
|
|
1634 if (pos < BUF_BEGV (buf))
|
|
1635 /* no more characters in buffer! */
|
|
1636 return;
|
|
1637 /*
|
|
1638 * -1 as the pos argument means to move point forward with the
|
|
1639 * insertion, which we must do if the deletion moved point
|
|
1640 * backward so that it now equals the insertion point.
|
|
1641 */
|
|
1642 buffer_insert_string_1 (buf, (movepoint ? -1 : pos),
|
|
1643 newstr, Qnil, 0, newlen, 0);
|
|
1644 }
|
|
1645 }
|
|
1646
|
|
1647
|
|
1648 /************************************************************************/
|
|
1649 /* Other functions */
|
|
1650 /************************************************************************/
|
|
1651
|
|
1652 /* Make a string from a buffer. This needs to take into account the gap,
|
|
1653 and add any necessary extents from the buffer. */
|
|
1654
|
|
1655 static Lisp_Object
|
665
|
1656 make_string_from_buffer_1 (struct buffer *buf, Charbpos pos, Charcount length,
|
428
|
1657 int no_extents)
|
|
1658 {
|
|
1659 /* This function can GC */
|
826
|
1660 Bytebpos bytepos = charbpos_to_bytebpos (buf, pos);
|
|
1661 Bytecount bytelen = charbpos_to_bytebpos (buf, pos + length) - bytepos;
|
|
1662 Bytecount needed = copy_buffer_text_out (buf, bytepos, bytelen, NULL, 0,
|
|
1663 FORMAT_DEFAULT, Qnil, NULL);
|
|
1664 Lisp_Object val = make_uninit_string (needed);
|
428
|
1665
|
|
1666 struct gcpro gcpro1;
|
|
1667 GCPRO1 (val);
|
|
1668
|
|
1669 if (!no_extents)
|
826
|
1670 add_string_extents (val, buf, bytepos, bytelen);
|
|
1671 copy_buffer_text_out (buf, bytepos, bytelen, XSTRING_DATA (val), needed,
|
|
1672 FORMAT_DEFAULT, Qnil, NULL);
|
771
|
1673 init_string_ascii_begin (val);
|
|
1674 sledgehammer_check_ascii_begin (val);
|
|
1675
|
428
|
1676 UNGCPRO;
|
|
1677 return val;
|
|
1678 }
|
|
1679
|
|
1680 Lisp_Object
|
665
|
1681 make_string_from_buffer (struct buffer *buf, Charbpos pos, Charcount length)
|
428
|
1682 {
|
|
1683 return make_string_from_buffer_1 (buf, pos, length, 0);
|
|
1684 }
|
|
1685
|
|
1686 Lisp_Object
|
665
|
1687 make_string_from_buffer_no_extents (struct buffer *buf, Charbpos pos,
|
428
|
1688 Charcount length)
|
|
1689 {
|
|
1690 return make_string_from_buffer_1 (buf, pos, length, 1);
|
|
1691 }
|
|
1692
|
|
1693 void
|
665
|
1694 barf_if_buffer_read_only (struct buffer *buf, Charbpos from, Charbpos to)
|
428
|
1695 {
|
|
1696 Lisp_Object buffer;
|
|
1697 Lisp_Object iro;
|
|
1698
|
793
|
1699 buffer = wrap_buffer (buf);
|
428
|
1700 back:
|
|
1701 iro = (buf == current_buffer ? Vinhibit_read_only :
|
|
1702 symbol_value_in_buffer (Qinhibit_read_only, buffer));
|
|
1703 if (!LISTP (iro))
|
|
1704 return;
|
|
1705 if (NILP (iro) && !NILP (buf->read_only))
|
|
1706 {
|
|
1707 Fsignal (Qbuffer_read_only, (list1 (buffer)));
|
|
1708 goto back;
|
|
1709 }
|
|
1710 if (from > 0)
|
|
1711 {
|
|
1712 if (to < 0)
|
|
1713 to = from;
|
|
1714 verify_extent_modification (buffer,
|
665
|
1715 charbpos_to_bytebpos (buf, from),
|
|
1716 charbpos_to_bytebpos (buf, to),
|
428
|
1717 iro);
|
|
1718 }
|
|
1719 }
|
|
1720
|
|
1721
|
|
1722 /************************************************************************/
|
|
1723 /* initialization */
|
|
1724 /************************************************************************/
|
|
1725
|
|
1726 void
|
|
1727 reinit_vars_of_insdel (void)
|
|
1728 {
|
|
1729 inside_change_hook = 0;
|
|
1730 in_first_change = 0;
|
|
1731 }
|
|
1732
|
|
1733 void
|
|
1734 vars_of_insdel (void)
|
|
1735 {
|
|
1736 reinit_vars_of_insdel ();
|
|
1737 }
|
|
1738
|
|
1739 void
|
|
1740 init_buffer_text (struct buffer *b)
|
|
1741 {
|
|
1742 if (!b->base_buffer)
|
|
1743 {
|
|
1744 SET_BUF_GAP_SIZE (b, 20);
|
|
1745 BUFFER_ALLOC (b->text->beg, BUF_GAP_SIZE (b) + BUF_END_SENTINEL_SIZE);
|
|
1746 if (! BUF_BEG_ADDR (b))
|
|
1747 memory_full ();
|
|
1748
|
|
1749 SET_BUF_END_GAP_SIZE (b, 0);
|
826
|
1750 SET_BYTE_BUF_GPT (b, 1);
|
428
|
1751 SET_BOTH_BUF_Z (b, 1, 1);
|
|
1752 SET_GAP_SENTINEL (b);
|
|
1753 SET_END_SENTINEL (b);
|
|
1754 #ifdef MULE
|
|
1755 {
|
|
1756 int i;
|
|
1757
|
|
1758 b->text->mule_bufmin = b->text->mule_bufmax = 1;
|
|
1759 b->text->mule_bytmin = b->text->mule_bytmax = 1;
|
826
|
1760 b->text->entirely_one_byte_p = 1;
|
428
|
1761
|
|
1762 for (i = 0; i < 16; i++)
|
|
1763 {
|
665
|
1764 b->text->mule_charbpos_cache[i] = 1;
|
|
1765 b->text->mule_bytebpos_cache[i] = 1;
|
428
|
1766 }
|
|
1767 }
|
826
|
1768 /* &&#### Set to FORMAT_8_BIT_FIXED when that code is working */
|
|
1769 BUF_FORMAT (b) = FORMAT_DEFAULT;
|
428
|
1770 #endif /* MULE */
|
|
1771 b->text->line_number_cache = Qnil;
|
|
1772
|
|
1773 BUF_MODIFF (b) = 1;
|
|
1774 BUF_SAVE_MODIFF (b) = 1;
|
|
1775
|
|
1776 JUST_SET_POINT (b, 1, 1);
|
|
1777 SET_BOTH_BUF_BEGV (b, 1, 1);
|
|
1778 SET_BOTH_BUF_ZV (b, 1, 1);
|
|
1779
|
|
1780 b->text->changes = xnew_and_zero (struct buffer_text_change_data);
|
|
1781 }
|
|
1782 else
|
|
1783 {
|
826
|
1784 JUST_SET_POINT (b, BUF_PT (b->base_buffer), BYTE_BUF_PT (b->base_buffer));
|
428
|
1785 SET_BOTH_BUF_BEGV (b, BUF_BEGV (b->base_buffer),
|
826
|
1786 BYTE_BUF_BEGV (b->base_buffer));
|
428
|
1787 SET_BOTH_BUF_ZV (b, BUF_ZV (b->base_buffer),
|
826
|
1788 BYTE_BUF_ZV (b->base_buffer));
|
428
|
1789 }
|
|
1790
|
|
1791 b->changes = xnew_and_zero (struct each_buffer_change_data);
|
|
1792 BUF_FACECHANGE (b) = 1;
|
|
1793
|
|
1794 #ifdef REGION_CACHE_NEEDS_WORK
|
|
1795 b->newline_cache = 0;
|
|
1796 b->width_run_cache = 0;
|
|
1797 b->width_table = Qnil;
|
|
1798 #endif
|
|
1799 }
|
|
1800
|
|
1801 void
|
|
1802 uninit_buffer_text (struct buffer *b)
|
|
1803 {
|
|
1804 if (!b->base_buffer)
|
|
1805 {
|
|
1806 BUFFER_FREE (b->text->beg);
|
|
1807 xfree (b->text->changes);
|
|
1808 }
|
|
1809 xfree (b->changes);
|
|
1810
|
|
1811 #ifdef REGION_CACHE_NEEDS_WORK
|
|
1812 if (b->newline_cache)
|
|
1813 {
|
|
1814 free_region_cache (b->newline_cache);
|
|
1815 b->newline_cache = 0;
|
|
1816 }
|
|
1817 if (b->width_run_cache)
|
|
1818 {
|
|
1819 free_region_cache (b->width_run_cache);
|
|
1820 b->width_run_cache = 0;
|
|
1821 }
|
|
1822 b->width_table = Qnil;
|
|
1823 #endif
|
|
1824 }
|