]> code.delx.au - gnu-emacs/blob - src/insdel.c
Tweak for sgml-transformation-function
[gnu-emacs] / src / insdel.c
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2013 Free Software
3 Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include <config.h>
22
23 #include <intprops.h>
24
25 #include "lisp.h"
26 #include "intervals.h"
27 #include "character.h"
28 #include "buffer.h"
29 #include "window.h"
30 #include "blockinput.h"
31 #include "region-cache.h"
32
33 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
38
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
43
44 In each element.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list;
50
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer;
53
54 Lisp_Object Qinhibit_modification_hooks;
55
56 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
57
58 /* Also used in marker.c to enable expensive marker checks. */
59
60 #ifdef MARKER_DEBUG
61
62 static void
63 check_markers (void)
64 {
65 struct Lisp_Marker *tail;
66 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
67
68 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
69 {
70 if (tail->buffer->text != current_buffer->text)
71 emacs_abort ();
72 if (tail->charpos > Z)
73 emacs_abort ();
74 if (tail->bytepos > Z_BYTE)
75 emacs_abort ();
76 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
77 emacs_abort ();
78 }
79 }
80
81 #else /* not MARKER_DEBUG */
82
83 #define check_markers() do { } while (0)
84
85 #endif /* MARKER_DEBUG */
86
87 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
88 Note that this can quit! */
89
90 void
91 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
92 {
93 eassert (charpos == BYTE_TO_CHAR (bytepos)
94 && bytepos == CHAR_TO_BYTE (charpos));
95 if (bytepos < GPT_BYTE)
96 gap_left (charpos, bytepos, 0);
97 else if (bytepos > GPT_BYTE)
98 gap_right (charpos, bytepos);
99 }
100
101 /* Move the gap to a position less than the current GPT.
102 BYTEPOS describes the new position as a byte position,
103 and CHARPOS is the corresponding char position.
104 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
105
106 static void
107 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
108 {
109 unsigned char *to, *from;
110 ptrdiff_t i;
111 ptrdiff_t new_s1;
112
113 if (!newgap)
114 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
115
116 i = GPT_BYTE;
117 to = GAP_END_ADDR;
118 from = GPT_ADDR;
119 new_s1 = GPT_BYTE;
120
121 /* Now copy the characters. To move the gap down,
122 copy characters up. */
123
124 while (1)
125 {
126 /* I gets number of characters left to copy. */
127 i = new_s1 - bytepos;
128 if (i == 0)
129 break;
130 /* If a quit is requested, stop copying now.
131 Change BYTEPOS to be where we have actually moved the gap to. */
132 if (QUITP)
133 {
134 bytepos = new_s1;
135 charpos = BYTE_TO_CHAR (bytepos);
136 break;
137 }
138 /* Move at most 32000 chars before checking again for a quit. */
139 if (i > 32000)
140 i = 32000;
141 new_s1 -= i;
142 from -= i, to -= i;
143 memmove (to, from, i);
144 }
145
146 /* Adjust buffer data structure, to put the gap at BYTEPOS.
147 BYTEPOS is where the loop above stopped, which may be what
148 was specified or may be where a quit was detected. */
149 GPT_BYTE = bytepos;
150 GPT = charpos;
151 eassert (charpos <= bytepos);
152 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
153 QUIT;
154 }
155
156 /* Move the gap to a position greater than the current GPT.
157 BYTEPOS describes the new position as a byte position,
158 and CHARPOS is the corresponding char position. */
159
160 static void
161 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
162 {
163 register unsigned char *to, *from;
164 register ptrdiff_t i;
165 ptrdiff_t new_s1;
166
167 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
168
169 i = GPT_BYTE;
170 from = GAP_END_ADDR;
171 to = GPT_ADDR;
172 new_s1 = GPT_BYTE;
173
174 /* Now copy the characters. To move the gap up,
175 copy characters down. */
176
177 while (1)
178 {
179 /* I gets number of characters left to copy. */
180 i = bytepos - new_s1;
181 if (i == 0)
182 break;
183 /* If a quit is requested, stop copying now.
184 Change BYTEPOS to be where we have actually moved the gap to. */
185 if (QUITP)
186 {
187 bytepos = new_s1;
188 charpos = BYTE_TO_CHAR (bytepos);
189 break;
190 }
191 /* Move at most 32000 chars before checking again for a quit. */
192 if (i > 32000)
193 i = 32000;
194 new_s1 += i;
195 memmove (to, from, i);
196 from += i, to += i;
197 }
198
199 GPT = charpos;
200 GPT_BYTE = bytepos;
201 eassert (charpos <= bytepos);
202 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
203 QUIT;
204 }
205 \f
206 /* Adjust all markers for a deletion
207 whose range in bytes is FROM_BYTE to TO_BYTE.
208 The range in charpos is FROM to TO.
209
210 This function assumes that the gap is adjacent to
211 or inside of the range being deleted. */
212
213 void
214 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
215 ptrdiff_t to, ptrdiff_t to_byte)
216 {
217 Lisp_Object marker;
218 register struct Lisp_Marker *m;
219 register ptrdiff_t charpos;
220
221 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
222 {
223 charpos = m->charpos;
224 eassert (charpos <= Z);
225
226 /* If the marker is after the deletion,
227 relocate by number of chars / bytes deleted. */
228 if (charpos > to)
229 {
230 m->charpos -= to - from;
231 m->bytepos -= to_byte - from_byte;
232 }
233 /* Here's the case where a marker is inside text being deleted. */
234 else if (charpos > from)
235 {
236 if (! m->insertion_type)
237 { /* Normal markers will end up at the beginning of the
238 re-inserted text after undoing a deletion, and must be
239 adjusted to move them to the correct place. */
240 XSETMISC (marker, m);
241 record_marker_adjustment (marker, from - charpos);
242 }
243 else if (charpos < to)
244 { /* Before-insertion markers will automatically move forward
245 upon re-inserting the deleted text, so we have to arrange
246 for them to move backward to the correct position. */
247 XSETMISC (marker, m);
248 record_marker_adjustment (marker, to - charpos);
249 }
250 m->charpos = from;
251 m->bytepos = from_byte;
252 }
253 /* Here's the case where a before-insertion marker is immediately
254 before the deleted region. */
255 else if (charpos == from && m->insertion_type)
256 {
257 /* Undoing the change uses normal insertion, which will
258 incorrectly make MARKER move forward, so we arrange for it
259 to then move backward to the correct place at the beginning
260 of the deleted region. */
261 XSETMISC (marker, m);
262 record_marker_adjustment (marker, to - from);
263 }
264 }
265 }
266
267 \f
268 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
269 to TO / TO_BYTE. We have to relocate the charpos of every marker
270 that points after the insertion (but not their bytepos).
271
272 When a marker points at the insertion point,
273 we advance it if either its insertion-type is t
274 or BEFORE_MARKERS is true. */
275
276 static void
277 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
278 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
279 {
280 struct Lisp_Marker *m;
281 bool adjusted = 0;
282 ptrdiff_t nchars = to - from;
283 ptrdiff_t nbytes = to_byte - from_byte;
284
285 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
286 {
287 eassert (m->bytepos >= m->charpos
288 && m->bytepos - m->charpos <= Z_BYTE - Z);
289
290 if (m->bytepos == from_byte)
291 {
292 if (m->insertion_type || before_markers)
293 {
294 m->bytepos = to_byte;
295 m->charpos = to;
296 if (m->insertion_type)
297 adjusted = 1;
298 }
299 }
300 else if (m->bytepos > from_byte)
301 {
302 m->bytepos += nbytes;
303 m->charpos += nchars;
304 }
305 }
306
307 /* Adjusting only markers whose insertion-type is t may result in
308 - disordered start and end in overlays, and
309 - disordered overlays in the slot `overlays_before' of current_buffer. */
310 if (adjusted)
311 {
312 fix_start_end_in_overlays (from, to);
313 fix_overlays_before (current_buffer, from, to);
314 }
315 }
316
317 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
318
319 This is used only when the value of point changes due to an insert
320 or delete; it does not represent a conceptual change in point as a
321 marker. In particular, point is not crossing any interval
322 boundaries, so there's no need to use the usual SET_PT macro. In
323 fact it would be incorrect to do so, because either the old or the
324 new value of point is out of sync with the current set of
325 intervals. */
326
327 static void
328 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
329 {
330 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
331 /* In a single-byte buffer, the two positions must be equal. */
332 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
333 }
334 \f
335 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
336 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
337 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
338 an insertion. */
339
340 static void
341 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
342 ptrdiff_t old_chars, ptrdiff_t old_bytes,
343 ptrdiff_t new_chars, ptrdiff_t new_bytes)
344 {
345 register struct Lisp_Marker *m;
346 ptrdiff_t prev_to_byte = from_byte + old_bytes;
347 ptrdiff_t diff_chars = new_chars - old_chars;
348 ptrdiff_t diff_bytes = new_bytes - old_bytes;
349
350 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
351 {
352 if (m->bytepos >= prev_to_byte)
353 {
354 m->charpos += diff_chars;
355 m->bytepos += diff_bytes;
356 }
357 else if (m->bytepos > from_byte)
358 {
359 m->charpos = from;
360 m->bytepos = from_byte;
361 }
362 }
363
364 check_markers ();
365 }
366
367 \f
368 void
369 buffer_overflow (void)
370 {
371 error ("Maximum buffer size exceeded");
372 }
373
374 /* Make the gap NBYTES_ADDED bytes longer. */
375
376 static void
377 make_gap_larger (ptrdiff_t nbytes_added)
378 {
379 Lisp_Object tem;
380 ptrdiff_t real_gap_loc;
381 ptrdiff_t real_gap_loc_byte;
382 ptrdiff_t old_gap_size;
383 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
384
385 if (BUF_BYTES_MAX - current_size < nbytes_added)
386 buffer_overflow ();
387
388 /* If we have to get more space, get enough to last a while;
389 but do not exceed the maximum buffer size. */
390 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
391 BUF_BYTES_MAX - current_size);
392
393 enlarge_buffer_text (current_buffer, nbytes_added);
394
395 /* Prevent quitting in move_gap. */
396 tem = Vinhibit_quit;
397 Vinhibit_quit = Qt;
398
399 real_gap_loc = GPT;
400 real_gap_loc_byte = GPT_BYTE;
401 old_gap_size = GAP_SIZE;
402
403 /* Call the newly allocated space a gap at the end of the whole space. */
404 GPT = Z + GAP_SIZE;
405 GPT_BYTE = Z_BYTE + GAP_SIZE;
406 GAP_SIZE = nbytes_added;
407
408 /* Move the new gap down to be consecutive with the end of the old one. */
409 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
410
411 /* Now combine the two into one large gap. */
412 GAP_SIZE += old_gap_size;
413 GPT = real_gap_loc;
414 GPT_BYTE = real_gap_loc_byte;
415
416 /* Put an anchor. */
417 *(Z_ADDR) = 0;
418
419 Vinhibit_quit = tem;
420 }
421
422 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
423
424 /* Make the gap NBYTES_REMOVED bytes shorter. */
425
426 static void
427 make_gap_smaller (ptrdiff_t nbytes_removed)
428 {
429 Lisp_Object tem;
430 ptrdiff_t real_gap_loc;
431 ptrdiff_t real_gap_loc_byte;
432 ptrdiff_t real_Z;
433 ptrdiff_t real_Z_byte;
434 ptrdiff_t real_beg_unchanged;
435 ptrdiff_t new_gap_size;
436
437 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
438 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
439 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
440
441 /* Prevent quitting in move_gap. */
442 tem = Vinhibit_quit;
443 Vinhibit_quit = Qt;
444
445 real_gap_loc = GPT;
446 real_gap_loc_byte = GPT_BYTE;
447 new_gap_size = GAP_SIZE - nbytes_removed;
448 real_Z = Z;
449 real_Z_byte = Z_BYTE;
450 real_beg_unchanged = BEG_UNCHANGED;
451
452 /* Pretend that the last unwanted part of the gap is the entire gap,
453 and that the first desired part of the gap is part of the buffer
454 text. */
455 memset (GPT_ADDR, 0, new_gap_size);
456 GPT += new_gap_size;
457 GPT_BYTE += new_gap_size;
458 Z += new_gap_size;
459 Z_BYTE += new_gap_size;
460 GAP_SIZE = nbytes_removed;
461
462 /* Move the unwanted pretend gap to the end of the buffer. */
463 gap_right (Z, Z_BYTE);
464
465 enlarge_buffer_text (current_buffer, -nbytes_removed);
466
467 /* Now restore the desired gap. */
468 GAP_SIZE = new_gap_size;
469 GPT = real_gap_loc;
470 GPT_BYTE = real_gap_loc_byte;
471 Z = real_Z;
472 Z_BYTE = real_Z_byte;
473 BEG_UNCHANGED = real_beg_unchanged;
474
475 /* Put an anchor. */
476 *(Z_ADDR) = 0;
477
478 Vinhibit_quit = tem;
479 }
480
481 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
482
483 void
484 make_gap (ptrdiff_t nbytes_added)
485 {
486 if (nbytes_added >= 0)
487 make_gap_larger (nbytes_added);
488 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
489 else
490 make_gap_smaller (-nbytes_added);
491 #endif
492 }
493
494 /* Add NBYTES to B's gap. It's enough to temporarily
495 fake current_buffer and avoid real switch to B. */
496
497 void
498 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
499 {
500 struct buffer *oldb = current_buffer;
501
502 current_buffer = b;
503 make_gap (nbytes);
504 current_buffer = oldb;
505 }
506
507 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
508 FROM_MULTIBYTE says whether the incoming text is multibyte.
509 TO_MULTIBYTE says whether to store the text as multibyte.
510 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
511
512 Return the number of bytes stored at TO_ADDR. */
513
514 ptrdiff_t
515 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
516 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
517 {
518 if (from_multibyte == to_multibyte)
519 {
520 memcpy (to_addr, from_addr, nbytes);
521 return nbytes;
522 }
523 else if (from_multibyte)
524 {
525 ptrdiff_t nchars = 0;
526 ptrdiff_t bytes_left = nbytes;
527
528 while (bytes_left > 0)
529 {
530 int thislen, c;
531 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
532 if (! ASCII_CHAR_P (c))
533 c &= 0xFF;
534 *to_addr++ = c;
535 from_addr += thislen;
536 bytes_left -= thislen;
537 nchars++;
538 }
539 return nchars;
540 }
541 else
542 {
543 unsigned char *initial_to_addr = to_addr;
544
545 /* Convert single-byte to multibyte. */
546 while (nbytes > 0)
547 {
548 int c = *from_addr++;
549
550 if (!ASCII_CHAR_P (c))
551 {
552 c = BYTE8_TO_CHAR (c);
553 to_addr += CHAR_STRING (c, to_addr);
554 nbytes--;
555 }
556 else
557 /* Special case for speed. */
558 *to_addr++ = c, nbytes--;
559 }
560 return to_addr - initial_to_addr;
561 }
562 }
563 \f
564 /* Insert a string of specified length before point.
565 This function judges multibyteness based on
566 enable_multibyte_characters in the current buffer;
567 it never converts between single-byte and multibyte.
568
569 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
570 prepare_to_modify_buffer could relocate the text. */
571
572 void
573 insert (const char *string, ptrdiff_t nbytes)
574 {
575 if (nbytes > 0)
576 {
577 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
578 insert_1_both (string, len, nbytes, 0, 1, 0);
579 opoint = PT - len;
580 signal_after_change (opoint, 0, len);
581 update_compositions (opoint, PT, CHECK_BORDER);
582 }
583 }
584
585 /* Likewise, but inherit text properties from neighboring characters. */
586
587 void
588 insert_and_inherit (const char *string, ptrdiff_t nbytes)
589 {
590 if (nbytes > 0)
591 {
592 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
593 insert_1_both (string, len, nbytes, 1, 1, 0);
594 opoint = PT - len;
595 signal_after_change (opoint, 0, len);
596 update_compositions (opoint, PT, CHECK_BORDER);
597 }
598 }
599
600 /* Insert the character C before point. Do not inherit text properties. */
601
602 void
603 insert_char (int c)
604 {
605 unsigned char str[MAX_MULTIBYTE_LENGTH];
606 int len;
607
608 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
609 len = CHAR_STRING (c, str);
610 else
611 {
612 len = 1;
613 str[0] = c;
614 }
615
616 insert ((char *) str, len);
617 }
618
619 /* Insert the null-terminated string S before point. */
620
621 void
622 insert_string (const char *s)
623 {
624 insert (s, strlen (s));
625 }
626
627 /* Like `insert' except that all markers pointing at the place where
628 the insertion happens are adjusted to point after it.
629 Don't use this function to insert part of a Lisp string,
630 since gc could happen and relocate it. */
631
632 void
633 insert_before_markers (const char *string, ptrdiff_t nbytes)
634 {
635 if (nbytes > 0)
636 {
637 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
638 insert_1_both (string, len, nbytes, 0, 1, 1);
639 opoint = PT - len;
640 signal_after_change (opoint, 0, len);
641 update_compositions (opoint, PT, CHECK_BORDER);
642 }
643 }
644
645 /* Likewise, but inherit text properties from neighboring characters. */
646
647 void
648 insert_before_markers_and_inherit (const char *string,
649 ptrdiff_t nbytes)
650 {
651 if (nbytes > 0)
652 {
653 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
654 insert_1_both (string, len, nbytes, 1, 1, 1);
655 opoint = PT - len;
656 signal_after_change (opoint, 0, len);
657 update_compositions (opoint, PT, CHECK_BORDER);
658 }
659 }
660
661 #ifdef BYTE_COMBINING_DEBUG
662
663 /* See if the bytes before POS/POS_BYTE combine with bytes
664 at the start of STRING to form a single character.
665 If so, return the number of bytes at the start of STRING
666 which combine in this way. Otherwise, return 0. */
667
668 int
669 count_combining_before (const unsigned char *string, ptrdiff_t length,
670 ptrdiff_t pos, ptrdiff_t pos_byte)
671 {
672 int len, combining_bytes;
673 const unsigned char *p;
674
675 if (NILP (current_buffer->enable_multibyte_characters))
676 return 0;
677
678 /* At first, we can exclude the following cases:
679 (1) STRING[0] can't be a following byte of multibyte sequence.
680 (2) POS is the start of the current buffer.
681 (3) A character before POS is not a multibyte character. */
682 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
683 return 0;
684 if (pos_byte == BEG_BYTE) /* case (2) */
685 return 0;
686 len = 1;
687 p = BYTE_POS_ADDR (pos_byte - 1);
688 while (! CHAR_HEAD_P (*p)) p--, len++;
689 if (! LEADING_CODE_P (*p)) /* case (3) */
690 return 0;
691
692 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
693 if (combining_bytes <= 0)
694 /* The character preceding POS is, complete and no room for
695 combining bytes (combining_bytes == 0), or an independent 8-bit
696 character (combining_bytes < 0). */
697 return 0;
698
699 /* We have a combination situation. Count the bytes at STRING that
700 may combine. */
701 p = string + 1;
702 while (!CHAR_HEAD_P (*p) && p < string + length)
703 p++;
704
705 return (combining_bytes < p - string ? combining_bytes : p - string);
706 }
707
708 /* See if the bytes after POS/POS_BYTE combine with bytes
709 at the end of STRING to form a single character.
710 If so, return the number of bytes after POS/POS_BYTE
711 which combine in this way. Otherwise, return 0. */
712
713 int
714 count_combining_after (const unsigned char *string,
715 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
716 {
717 ptrdiff_t opos_byte = pos_byte;
718 ptrdiff_t i;
719 ptrdiff_t bytes;
720 unsigned char *bufp;
721
722 if (NILP (current_buffer->enable_multibyte_characters))
723 return 0;
724
725 /* At first, we can exclude the following cases:
726 (1) The last byte of STRING is an ASCII.
727 (2) POS is the last of the current buffer.
728 (3) A character at POS can't be a following byte of multibyte
729 character. */
730 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
731 return 0;
732 if (pos_byte == Z_BYTE) /* case (2) */
733 return 0;
734 bufp = BYTE_POS_ADDR (pos_byte);
735 if (CHAR_HEAD_P (*bufp)) /* case (3) */
736 return 0;
737
738 i = length - 1;
739 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
740 {
741 i--;
742 }
743 if (i < 0)
744 {
745 /* All characters in STRING are not character head. We must
746 check also preceding bytes at POS. We are sure that the gap
747 is at POS. */
748 unsigned char *p = BEG_ADDR;
749 i = pos_byte - 2;
750 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
751 i--;
752 if (i < 0 || !LEADING_CODE_P (p[i]))
753 return 0;
754
755 bytes = BYTES_BY_CHAR_HEAD (p[i]);
756 return (bytes <= pos_byte - 1 - i + length
757 ? 0
758 : bytes - (pos_byte - 1 - i + length));
759 }
760 if (!LEADING_CODE_P (string[i]))
761 return 0;
762
763 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
764 bufp++, pos_byte++;
765 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
766
767 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
768 }
769
770 #endif
771
772 \f
773 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
774 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
775 are the same as in insert_1. */
776
777 void
778 insert_1_both (const char *string,
779 ptrdiff_t nchars, ptrdiff_t nbytes,
780 bool inherit, bool prepare, bool before_markers)
781 {
782 if (nchars == 0)
783 return;
784
785 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
786 nchars = nbytes;
787
788 if (prepare)
789 /* Do this before moving and increasing the gap,
790 because the before-change hooks might move the gap
791 or make it smaller. */
792 prepare_to_modify_buffer (PT, PT, NULL);
793
794 if (PT != GPT)
795 move_gap_both (PT, PT_BYTE);
796 if (GAP_SIZE < nbytes)
797 make_gap (nbytes - GAP_SIZE);
798
799 #ifdef BYTE_COMBINING_DEBUG
800 if (count_combining_before (string, nbytes, PT, PT_BYTE)
801 || count_combining_after (string, nbytes, PT, PT_BYTE))
802 emacs_abort ();
803 #endif
804
805 /* Record deletion of the surrounding text that combines with
806 the insertion. This, together with recording the insertion,
807 will add up to the right stuff in the undo list. */
808 record_insert (PT, nchars);
809 MODIFF++;
810 CHARS_MODIFF = MODIFF;
811
812 memcpy (GPT_ADDR, string, nbytes);
813
814 GAP_SIZE -= nbytes;
815 GPT += nchars;
816 ZV += nchars;
817 Z += nchars;
818 GPT_BYTE += nbytes;
819 ZV_BYTE += nbytes;
820 Z_BYTE += nbytes;
821 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
822
823 eassert (GPT <= GPT_BYTE);
824
825 /* The insert may have been in the unchanged region, so check again. */
826 if (Z - GPT < END_UNCHANGED)
827 END_UNCHANGED = Z - GPT;
828
829 adjust_overlays_for_insert (PT, nchars);
830 adjust_markers_for_insert (PT, PT_BYTE,
831 PT + nchars, PT_BYTE + nbytes,
832 before_markers);
833
834 offset_intervals (current_buffer, PT, nchars);
835
836 if (!inherit && buffer_intervals (current_buffer))
837 set_text_properties (make_number (PT), make_number (PT + nchars),
838 Qnil, Qnil, Qnil);
839
840 adjust_point (nchars, nbytes);
841
842 check_markers ();
843 }
844 \f
845 /* Insert the part of the text of STRING, a Lisp object assumed to be
846 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
847 starting at position POS / POS_BYTE. If the text of STRING has properties,
848 copy them into the buffer.
849
850 It does not work to use `insert' for this, because a GC could happen
851 before we copy the stuff into the buffer, and relocate the string
852 without insert noticing. */
853
854 void
855 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
856 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
857 {
858 ptrdiff_t opoint = PT;
859
860 if (SCHARS (string) == 0)
861 return;
862
863 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
864 inherit, 0);
865 signal_after_change (opoint, 0, PT - opoint);
866 update_compositions (opoint, PT, CHECK_BORDER);
867 }
868
869 /* Like `insert_from_string' except that all markers pointing
870 at the place where the insertion happens are adjusted to point after it. */
871
872 void
873 insert_from_string_before_markers (Lisp_Object string,
874 ptrdiff_t pos, ptrdiff_t pos_byte,
875 ptrdiff_t length, ptrdiff_t length_byte,
876 bool inherit)
877 {
878 ptrdiff_t opoint = PT;
879
880 if (SCHARS (string) == 0)
881 return;
882
883 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
884 inherit, 1);
885 signal_after_change (opoint, 0, PT - opoint);
886 update_compositions (opoint, PT, CHECK_BORDER);
887 }
888
889 /* Subroutine of the insertion functions above. */
890
891 static void
892 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
893 ptrdiff_t nchars, ptrdiff_t nbytes,
894 bool inherit, bool before_markers)
895 {
896 struct gcpro gcpro1;
897 ptrdiff_t outgoing_nbytes = nbytes;
898 INTERVAL intervals;
899
900 /* Make OUTGOING_NBYTES describe the text
901 as it will be inserted in this buffer. */
902
903 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
904 outgoing_nbytes = nchars;
905 else if (! STRING_MULTIBYTE (string))
906 outgoing_nbytes
907 = count_size_as_multibyte (SDATA (string) + pos_byte,
908 nbytes);
909
910 GCPRO1 (string);
911 /* Do this before moving and increasing the gap,
912 because the before-change hooks might move the gap
913 or make it smaller. */
914 prepare_to_modify_buffer (PT, PT, NULL);
915
916 if (PT != GPT)
917 move_gap_both (PT, PT_BYTE);
918 if (GAP_SIZE < outgoing_nbytes)
919 make_gap (outgoing_nbytes - GAP_SIZE);
920 UNGCPRO;
921
922 /* Copy the string text into the buffer, perhaps converting
923 between single-byte and multibyte. */
924 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
925 STRING_MULTIBYTE (string),
926 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
927
928 #ifdef BYTE_COMBINING_DEBUG
929 /* We have copied text into the gap, but we have not altered
930 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
931 to these functions and get the same results as we would
932 have got earlier on. Meanwhile, PT_ADDR does point to
933 the text that has been stored by copy_text. */
934 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
935 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
936 emacs_abort ();
937 #endif
938
939 record_insert (PT, nchars);
940 MODIFF++;
941 CHARS_MODIFF = MODIFF;
942
943 GAP_SIZE -= outgoing_nbytes;
944 GPT += nchars;
945 ZV += nchars;
946 Z += nchars;
947 GPT_BYTE += outgoing_nbytes;
948 ZV_BYTE += outgoing_nbytes;
949 Z_BYTE += outgoing_nbytes;
950 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
951
952 eassert (GPT <= GPT_BYTE);
953
954 /* The insert may have been in the unchanged region, so check again. */
955 if (Z - GPT < END_UNCHANGED)
956 END_UNCHANGED = Z - GPT;
957
958 adjust_overlays_for_insert (PT, nchars);
959 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
960 PT_BYTE + outgoing_nbytes,
961 before_markers);
962
963 offset_intervals (current_buffer, PT, nchars);
964
965 intervals = string_intervals (string);
966 /* Get the intervals for the part of the string we are inserting. */
967 if (nbytes < SBYTES (string))
968 intervals = copy_intervals (intervals, pos, nchars);
969
970 /* Insert those intervals. */
971 graft_intervals_into_buffer (intervals, PT, nchars,
972 current_buffer, inherit);
973
974 adjust_point (nchars, outgoing_nbytes);
975
976 check_markers ();
977 }
978 \f
979 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
980 starting at GPT_ADDR. */
981
982 void
983 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes)
984 {
985 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
986 nchars = nbytes;
987
988 record_insert (GPT, nchars);
989 MODIFF++;
990
991 GAP_SIZE -= nbytes;
992 GPT += nchars;
993 ZV += nchars;
994 Z += nchars;
995 GPT_BYTE += nbytes;
996 ZV_BYTE += nbytes;
997 Z_BYTE += nbytes;
998 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
999
1000 eassert (GPT <= GPT_BYTE);
1001
1002 adjust_overlays_for_insert (GPT - nchars, nchars);
1003 adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
1004 GPT, GPT_BYTE, 0);
1005
1006 if (buffer_intervals (current_buffer))
1007 {
1008 offset_intervals (current_buffer, GPT - nchars, nchars);
1009 graft_intervals_into_buffer (NULL, GPT - nchars, nchars,
1010 current_buffer, 0);
1011 }
1012
1013 if (GPT - nchars < PT)
1014 adjust_point (nchars, nbytes);
1015
1016 check_markers ();
1017 }
1018 \f
1019 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1020 current buffer. If the text in BUF has properties, they are absorbed
1021 into the current buffer.
1022
1023 It does not work to use `insert' for this, because a malloc could happen
1024 and relocate BUF's text before the copy happens. */
1025
1026 void
1027 insert_from_buffer (struct buffer *buf,
1028 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1029 {
1030 ptrdiff_t opoint = PT;
1031
1032 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1033 signal_after_change (opoint, 0, PT - opoint);
1034 update_compositions (opoint, PT, CHECK_BORDER);
1035 }
1036
1037 static void
1038 insert_from_buffer_1 (struct buffer *buf,
1039 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1040 {
1041 ptrdiff_t chunk, chunk_expanded;
1042 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1043 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1044 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1045 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1046 INTERVAL intervals;
1047
1048 /* Make OUTGOING_NBYTES describe the text
1049 as it will be inserted in this buffer. */
1050
1051 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1052 outgoing_nbytes = nchars;
1053 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1054 {
1055 ptrdiff_t outgoing_before_gap = 0;
1056 ptrdiff_t outgoing_after_gap = 0;
1057
1058 if (from < BUF_GPT (buf))
1059 {
1060 chunk = BUF_GPT_BYTE (buf) - from_byte;
1061 if (chunk > incoming_nbytes)
1062 chunk = incoming_nbytes;
1063 outgoing_before_gap
1064 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1065 chunk);
1066 }
1067 else
1068 chunk = 0;
1069
1070 if (chunk < incoming_nbytes)
1071 outgoing_after_gap
1072 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1073 from_byte + chunk),
1074 incoming_nbytes - chunk);
1075
1076 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1077 }
1078
1079 /* Do this before moving and increasing the gap,
1080 because the before-change hooks might move the gap
1081 or make it smaller. */
1082 prepare_to_modify_buffer (PT, PT, NULL);
1083
1084 if (PT != GPT)
1085 move_gap_both (PT, PT_BYTE);
1086 if (GAP_SIZE < outgoing_nbytes)
1087 make_gap (outgoing_nbytes - GAP_SIZE);
1088
1089 if (from < BUF_GPT (buf))
1090 {
1091 chunk = BUF_GPT_BYTE (buf) - from_byte;
1092 if (chunk > incoming_nbytes)
1093 chunk = incoming_nbytes;
1094 /* Record number of output bytes, so we know where
1095 to put the output from the second copy_text. */
1096 chunk_expanded
1097 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1098 GPT_ADDR, chunk,
1099 ! NILP (BVAR (buf, enable_multibyte_characters)),
1100 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1101 }
1102 else
1103 chunk_expanded = chunk = 0;
1104
1105 if (chunk < incoming_nbytes)
1106 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1107 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1108 ! NILP (BVAR (buf, enable_multibyte_characters)),
1109 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1110
1111 #ifdef BYTE_COMBINING_DEBUG
1112 /* We have copied text into the gap, but we have not altered
1113 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1114 to these functions and get the same results as we would
1115 have got earlier on. Meanwhile, GPT_ADDR does point to
1116 the text that has been stored by copy_text. */
1117 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1118 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1119 emacs_abort ();
1120 #endif
1121
1122 record_insert (PT, nchars);
1123 MODIFF++;
1124 CHARS_MODIFF = MODIFF;
1125
1126 GAP_SIZE -= outgoing_nbytes;
1127 GPT += nchars;
1128 ZV += nchars;
1129 Z += nchars;
1130 GPT_BYTE += outgoing_nbytes;
1131 ZV_BYTE += outgoing_nbytes;
1132 Z_BYTE += outgoing_nbytes;
1133 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1134
1135 eassert (GPT <= GPT_BYTE);
1136
1137 /* The insert may have been in the unchanged region, so check again. */
1138 if (Z - GPT < END_UNCHANGED)
1139 END_UNCHANGED = Z - GPT;
1140
1141 adjust_overlays_for_insert (PT, nchars);
1142 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1143 PT_BYTE + outgoing_nbytes,
1144 0);
1145
1146 offset_intervals (current_buffer, PT, nchars);
1147
1148 /* Get the intervals for the part of the string we are inserting. */
1149 intervals = buffer_intervals (buf);
1150 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1151 {
1152 if (buf == current_buffer && PT <= from)
1153 from += nchars;
1154 intervals = copy_intervals (intervals, from, nchars);
1155 }
1156
1157 /* Insert those intervals. */
1158 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1159
1160 adjust_point (nchars, outgoing_nbytes);
1161 }
1162 \f
1163 /* Record undo information and adjust markers and position keepers for
1164 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1165 chars (LEN_BYTE bytes) which resides in the gap just after
1166 GPT_ADDR.
1167
1168 PREV_TEXT nil means the new text was just inserted. */
1169
1170 static void
1171 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1172 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1173 {
1174 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1175
1176 #ifdef BYTE_COMBINING_DEBUG
1177 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1178 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1179 emacs_abort ();
1180 #endif
1181
1182 if (STRINGP (prev_text))
1183 {
1184 nchars_del = SCHARS (prev_text);
1185 nbytes_del = SBYTES (prev_text);
1186 }
1187
1188 /* Update various buffer positions for the new text. */
1189 GAP_SIZE -= len_byte;
1190 ZV += len; Z+= len;
1191 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1192 GPT += len; GPT_BYTE += len_byte;
1193 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1194
1195 if (nchars_del > 0)
1196 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1197 len, len_byte);
1198 else
1199 adjust_markers_for_insert (from, from_byte,
1200 from + len, from_byte + len_byte, 0);
1201
1202 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1203 {
1204 if (nchars_del > 0)
1205 record_delete (from, prev_text);
1206 record_insert (from, len);
1207 }
1208
1209 if (len > nchars_del)
1210 adjust_overlays_for_insert (from, len - nchars_del);
1211 else if (len < nchars_del)
1212 adjust_overlays_for_delete (from, nchars_del - len);
1213
1214 offset_intervals (current_buffer, from, len - nchars_del);
1215
1216 if (from < PT)
1217 adjust_point (len - nchars_del, len_byte - nbytes_del);
1218
1219 /* As byte combining will decrease Z, we must check this again. */
1220 if (Z - GPT < END_UNCHANGED)
1221 END_UNCHANGED = Z - GPT;
1222
1223 check_markers ();
1224
1225 if (len == 0)
1226 evaporate_overlays (from);
1227 MODIFF++;
1228 CHARS_MODIFF = MODIFF;
1229 }
1230
1231 /* Record undo information, adjust markers and position keepers for an
1232 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1233 text already exists in the current buffer but character length (TO
1234 - FROM) may be incorrect, the correct length is NEWLEN. */
1235
1236 void
1237 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1238 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1239 {
1240 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1241
1242 if (GPT != to)
1243 move_gap_both (to, to_byte);
1244 GAP_SIZE += len_byte;
1245 GPT -= len; GPT_BYTE -= len_byte;
1246 ZV -= len; ZV_BYTE -= len_byte;
1247 Z -= len; Z_BYTE -= len_byte;
1248 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1249 }
1250 \f
1251 /* Replace the text from character positions FROM to TO with NEW,
1252 If PREPARE, call prepare_to_modify_buffer.
1253 If INHERIT, the newly inserted text should inherit text properties
1254 from the surrounding non-deleted text. */
1255
1256 /* Note that this does not yet handle markers quite right.
1257 Also it needs to record a single undo-entry that does a replacement
1258 rather than a separate delete and insert.
1259 That way, undo will also handle markers properly.
1260
1261 But if MARKERS is 0, don't relocate markers. */
1262
1263 void
1264 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1265 bool prepare, bool inherit, bool markers)
1266 {
1267 ptrdiff_t inschars = SCHARS (new);
1268 ptrdiff_t insbytes = SBYTES (new);
1269 ptrdiff_t from_byte, to_byte;
1270 ptrdiff_t nbytes_del, nchars_del;
1271 struct gcpro gcpro1;
1272 INTERVAL intervals;
1273 ptrdiff_t outgoing_insbytes = insbytes;
1274 Lisp_Object deletion;
1275
1276 check_markers ();
1277
1278 GCPRO1 (new);
1279 deletion = Qnil;
1280
1281 if (prepare)
1282 {
1283 ptrdiff_t range_length = to - from;
1284 prepare_to_modify_buffer (from, to, &from);
1285 to = from + range_length;
1286 }
1287
1288 UNGCPRO;
1289
1290 /* Make args be valid. */
1291 if (from < BEGV)
1292 from = BEGV;
1293 if (to > ZV)
1294 to = ZV;
1295
1296 from_byte = CHAR_TO_BYTE (from);
1297 to_byte = CHAR_TO_BYTE (to);
1298
1299 nchars_del = to - from;
1300 nbytes_del = to_byte - from_byte;
1301
1302 if (nbytes_del <= 0 && insbytes == 0)
1303 return;
1304
1305 /* Make OUTGOING_INSBYTES describe the text
1306 as it will be inserted in this buffer. */
1307
1308 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1309 outgoing_insbytes = inschars;
1310 else if (! STRING_MULTIBYTE (new))
1311 outgoing_insbytes
1312 = count_size_as_multibyte (SDATA (new), insbytes);
1313
1314 GCPRO1 (new);
1315
1316 /* Make sure the gap is somewhere in or next to what we are deleting. */
1317 if (from > GPT)
1318 gap_right (from, from_byte);
1319 if (to < GPT)
1320 gap_left (to, to_byte, 0);
1321
1322 /* Even if we don't record for undo, we must keep the original text
1323 because we may have to recover it because of inappropriate byte
1324 combining. */
1325 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1326 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1327
1328 GAP_SIZE += nbytes_del;
1329 ZV -= nchars_del;
1330 Z -= nchars_del;
1331 ZV_BYTE -= nbytes_del;
1332 Z_BYTE -= nbytes_del;
1333 GPT = from;
1334 GPT_BYTE = from_byte;
1335 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1336
1337 eassert (GPT <= GPT_BYTE);
1338
1339 if (GPT - BEG < BEG_UNCHANGED)
1340 BEG_UNCHANGED = GPT - BEG;
1341 if (Z - GPT < END_UNCHANGED)
1342 END_UNCHANGED = Z - GPT;
1343
1344 if (GAP_SIZE < outgoing_insbytes)
1345 make_gap (outgoing_insbytes - GAP_SIZE);
1346
1347 /* Copy the string text into the buffer, perhaps converting
1348 between single-byte and multibyte. */
1349 copy_text (SDATA (new), GPT_ADDR, insbytes,
1350 STRING_MULTIBYTE (new),
1351 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1352
1353 #ifdef BYTE_COMBINING_DEBUG
1354 /* We have copied text into the gap, but we have not marked
1355 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1356 here, for both the previous text and the following text.
1357 Meanwhile, GPT_ADDR does point to
1358 the text that has been stored by copy_text. */
1359 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1360 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1361 emacs_abort ();
1362 #endif
1363
1364 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1365 {
1366 /* Record the insertion first, so that when we undo,
1367 the deletion will be undone first. Thus, undo
1368 will insert before deleting, and thus will keep
1369 the markers before and after this text separate. */
1370 record_insert (from + SCHARS (deletion), inschars);
1371 record_delete (from, deletion);
1372 }
1373
1374 GAP_SIZE -= outgoing_insbytes;
1375 GPT += inschars;
1376 ZV += inschars;
1377 Z += inschars;
1378 GPT_BYTE += outgoing_insbytes;
1379 ZV_BYTE += outgoing_insbytes;
1380 Z_BYTE += outgoing_insbytes;
1381 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1382
1383 eassert (GPT <= GPT_BYTE);
1384
1385 /* Adjust markers for the deletion and the insertion. */
1386 if (markers)
1387 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1388 inschars, outgoing_insbytes);
1389
1390 /* Adjust the overlay center as needed. This must be done after
1391 adjusting the markers that bound the overlays. */
1392 adjust_overlays_for_delete (from, nchars_del);
1393 adjust_overlays_for_insert (from, inschars);
1394
1395 offset_intervals (current_buffer, from, inschars - nchars_del);
1396
1397 /* Get the intervals for the part of the string we are inserting--
1398 not including the combined-before bytes. */
1399 intervals = string_intervals (new);
1400 /* Insert those intervals. */
1401 graft_intervals_into_buffer (intervals, from, inschars,
1402 current_buffer, inherit);
1403
1404 /* Relocate point as if it were a marker. */
1405 if (from < PT)
1406 adjust_point ((from + inschars - (PT < to ? PT : to)),
1407 (from_byte + outgoing_insbytes
1408 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1409
1410 if (outgoing_insbytes == 0)
1411 evaporate_overlays (from);
1412
1413 check_markers ();
1414
1415 MODIFF++;
1416 CHARS_MODIFF = MODIFF;
1417 UNGCPRO;
1418
1419 signal_after_change (from, nchars_del, GPT - from);
1420 update_compositions (from, GPT, CHECK_BORDER);
1421 }
1422 \f
1423 /* Replace the text from character positions FROM to TO with
1424 the text in INS of length INSCHARS.
1425 Keep the text properties that applied to the old characters
1426 (extending them to all the new chars if there are more new chars).
1427
1428 Note that this does not yet handle markers quite right.
1429
1430 If MARKERS, relocate markers.
1431
1432 Unlike most functions at this level, never call
1433 prepare_to_modify_buffer and never call signal_after_change. */
1434
1435 void
1436 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1437 ptrdiff_t to, ptrdiff_t to_byte,
1438 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1439 bool markers)
1440 {
1441 ptrdiff_t nbytes_del, nchars_del;
1442
1443 check_markers ();
1444
1445 nchars_del = to - from;
1446 nbytes_del = to_byte - from_byte;
1447
1448 if (nbytes_del <= 0 && insbytes == 0)
1449 return;
1450
1451 /* Make sure the gap is somewhere in or next to what we are deleting. */
1452 if (from > GPT)
1453 gap_right (from, from_byte);
1454 if (to < GPT)
1455 gap_left (to, to_byte, 0);
1456
1457 GAP_SIZE += nbytes_del;
1458 ZV -= nchars_del;
1459 Z -= nchars_del;
1460 ZV_BYTE -= nbytes_del;
1461 Z_BYTE -= nbytes_del;
1462 GPT = from;
1463 GPT_BYTE = from_byte;
1464 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1465
1466 eassert (GPT <= GPT_BYTE);
1467
1468 if (GPT - BEG < BEG_UNCHANGED)
1469 BEG_UNCHANGED = GPT - BEG;
1470 if (Z - GPT < END_UNCHANGED)
1471 END_UNCHANGED = Z - GPT;
1472
1473 if (GAP_SIZE < insbytes)
1474 make_gap (insbytes - GAP_SIZE);
1475
1476 /* Copy the replacement text into the buffer. */
1477 memcpy (GPT_ADDR, ins, insbytes);
1478
1479 #ifdef BYTE_COMBINING_DEBUG
1480 /* We have copied text into the gap, but we have not marked
1481 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1482 here, for both the previous text and the following text.
1483 Meanwhile, GPT_ADDR does point to
1484 the text that has been stored by copy_text. */
1485 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1486 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1487 emacs_abort ();
1488 #endif
1489
1490 GAP_SIZE -= insbytes;
1491 GPT += inschars;
1492 ZV += inschars;
1493 Z += inschars;
1494 GPT_BYTE += insbytes;
1495 ZV_BYTE += insbytes;
1496 Z_BYTE += insbytes;
1497 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1498
1499 eassert (GPT <= GPT_BYTE);
1500
1501 /* Adjust markers for the deletion and the insertion. */
1502 if (markers
1503 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1504 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1505 inschars, insbytes);
1506
1507 /* Adjust the overlay center as needed. This must be done after
1508 adjusting the markers that bound the overlays. */
1509 if (nchars_del != inschars)
1510 {
1511 adjust_overlays_for_insert (from, inschars);
1512 adjust_overlays_for_delete (from + inschars, nchars_del);
1513 }
1514
1515 offset_intervals (current_buffer, from, inschars - nchars_del);
1516
1517 /* Relocate point as if it were a marker. */
1518 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1519 {
1520 if (PT < to)
1521 /* PT was within the deleted text. Move it to FROM. */
1522 adjust_point (from - PT, from_byte - PT_BYTE);
1523 else
1524 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1525 }
1526
1527 if (insbytes == 0)
1528 evaporate_overlays (from);
1529
1530 check_markers ();
1531
1532 MODIFF++;
1533 CHARS_MODIFF = MODIFF;
1534 }
1535 \f
1536 /* Delete characters in current buffer
1537 from FROM up to (but not including) TO.
1538 If TO comes before FROM, we delete nothing. */
1539
1540 void
1541 del_range (ptrdiff_t from, ptrdiff_t to)
1542 {
1543 del_range_1 (from, to, 1, 0);
1544 }
1545
1546 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1547 RET_STRING says to return the deleted text. */
1548
1549 Lisp_Object
1550 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1551 {
1552 ptrdiff_t from_byte, to_byte;
1553 Lisp_Object deletion;
1554 struct gcpro gcpro1;
1555
1556 /* Make args be valid */
1557 if (from < BEGV)
1558 from = BEGV;
1559 if (to > ZV)
1560 to = ZV;
1561
1562 if (to <= from)
1563 return Qnil;
1564
1565 if (prepare)
1566 {
1567 ptrdiff_t range_length = to - from;
1568 prepare_to_modify_buffer (from, to, &from);
1569 to = min (ZV, from + range_length);
1570 }
1571
1572 from_byte = CHAR_TO_BYTE (from);
1573 to_byte = CHAR_TO_BYTE (to);
1574
1575 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1576 GCPRO1 (deletion);
1577 signal_after_change (from, to - from, 0);
1578 update_compositions (from, from, CHECK_HEAD);
1579 UNGCPRO;
1580 return deletion;
1581 }
1582
1583 /* Like del_range_1 but args are byte positions, not char positions. */
1584
1585 void
1586 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1587 {
1588 ptrdiff_t from, to;
1589
1590 /* Make args be valid */
1591 if (from_byte < BEGV_BYTE)
1592 from_byte = BEGV_BYTE;
1593 if (to_byte > ZV_BYTE)
1594 to_byte = ZV_BYTE;
1595
1596 if (to_byte <= from_byte)
1597 return;
1598
1599 from = BYTE_TO_CHAR (from_byte);
1600 to = BYTE_TO_CHAR (to_byte);
1601
1602 if (prepare)
1603 {
1604 ptrdiff_t old_from = from, old_to = Z - to;
1605 ptrdiff_t range_length = to - from;
1606 prepare_to_modify_buffer (from, to, &from);
1607 to = from + range_length;
1608
1609 if (old_from != from)
1610 from_byte = CHAR_TO_BYTE (from);
1611 if (to > ZV)
1612 {
1613 to = ZV;
1614 to_byte = ZV_BYTE;
1615 }
1616 else if (old_to == Z - to)
1617 to_byte = CHAR_TO_BYTE (to);
1618 }
1619
1620 del_range_2 (from, from_byte, to, to_byte, 0);
1621 signal_after_change (from, to - from, 0);
1622 update_compositions (from, from, CHECK_HEAD);
1623 }
1624
1625 /* Like del_range_1, but positions are specified both as charpos
1626 and bytepos. */
1627
1628 void
1629 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1630 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1631 {
1632 /* Make args be valid */
1633 if (from_byte < BEGV_BYTE)
1634 from_byte = BEGV_BYTE;
1635 if (to_byte > ZV_BYTE)
1636 to_byte = ZV_BYTE;
1637
1638 if (to_byte <= from_byte)
1639 return;
1640
1641 if (from < BEGV)
1642 from = BEGV;
1643 if (to > ZV)
1644 to = ZV;
1645
1646 if (prepare)
1647 {
1648 ptrdiff_t old_from = from, old_to = Z - to;
1649 ptrdiff_t range_length = to - from;
1650 prepare_to_modify_buffer (from, to, &from);
1651 to = from + range_length;
1652
1653 if (old_from != from)
1654 from_byte = CHAR_TO_BYTE (from);
1655 if (to > ZV)
1656 {
1657 to = ZV;
1658 to_byte = ZV_BYTE;
1659 }
1660 else if (old_to == Z - to)
1661 to_byte = CHAR_TO_BYTE (to);
1662 }
1663
1664 del_range_2 (from, from_byte, to, to_byte, 0);
1665 signal_after_change (from, to - from, 0);
1666 update_compositions (from, from, CHECK_HEAD);
1667 }
1668
1669 /* Delete a range of text, specified both as character positions
1670 and byte positions. FROM and TO are character positions,
1671 while FROM_BYTE and TO_BYTE are byte positions.
1672 If RET_STRING, the deleted area is returned as a string. */
1673
1674 Lisp_Object
1675 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1676 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1677 {
1678 ptrdiff_t nbytes_del, nchars_del;
1679 Lisp_Object deletion;
1680
1681 check_markers ();
1682
1683 nchars_del = to - from;
1684 nbytes_del = to_byte - from_byte;
1685
1686 /* Make sure the gap is somewhere in or next to what we are deleting. */
1687 if (from > GPT)
1688 gap_right (from, from_byte);
1689 if (to < GPT)
1690 gap_left (to, to_byte, 0);
1691
1692 #ifdef BYTE_COMBINING_DEBUG
1693 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1694 Z_BYTE - to_byte, from, from_byte))
1695 emacs_abort ();
1696 #endif
1697
1698 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1699 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1700 else
1701 deletion = Qnil;
1702
1703 /* Relocate all markers pointing into the new, larger gap
1704 to point at the end of the text before the gap.
1705 Do this before recording the deletion,
1706 so that undo handles this after reinserting the text. */
1707 adjust_markers_for_delete (from, from_byte, to, to_byte);
1708
1709 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1710 record_delete (from, deletion);
1711 MODIFF++;
1712 CHARS_MODIFF = MODIFF;
1713
1714 /* Relocate point as if it were a marker. */
1715 if (from < PT)
1716 adjust_point (from - (PT < to ? PT : to),
1717 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1718
1719 offset_intervals (current_buffer, from, - nchars_del);
1720
1721 /* Adjust the overlay center as needed. This must be done after
1722 adjusting the markers that bound the overlays. */
1723 adjust_overlays_for_delete (from, nchars_del);
1724
1725 GAP_SIZE += nbytes_del;
1726 ZV_BYTE -= nbytes_del;
1727 Z_BYTE -= nbytes_del;
1728 ZV -= nchars_del;
1729 Z -= nchars_del;
1730 GPT = from;
1731 GPT_BYTE = from_byte;
1732 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1733 /* Put an anchor, unless called from decode_coding_object which
1734 needs to access the previous gap contents. */
1735 *(GPT_ADDR) = 0;
1736
1737 eassert (GPT <= GPT_BYTE);
1738
1739 if (GPT - BEG < BEG_UNCHANGED)
1740 BEG_UNCHANGED = GPT - BEG;
1741 if (Z - GPT < END_UNCHANGED)
1742 END_UNCHANGED = Z - GPT;
1743
1744 check_markers ();
1745
1746 evaporate_overlays (from);
1747
1748 return deletion;
1749 }
1750
1751 /* Call this if you're about to change the region of current buffer
1752 from character positions START to END. This checks the read-only
1753 properties of the region, calls the necessary modification hooks,
1754 and warns the next redisplay that it should pay attention to that
1755 area.
1756
1757 If PRESERVE_CHARS_MODIFF, do not update CHARS_MODIFF.
1758 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1759
1760 void
1761 modify_region_1 (ptrdiff_t start, ptrdiff_t end, bool preserve_chars_modiff)
1762 {
1763 prepare_to_modify_buffer (start, end, NULL);
1764
1765 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1766
1767 if (MODIFF <= SAVE_MODIFF)
1768 record_first_change ();
1769 MODIFF++;
1770 if (! preserve_chars_modiff)
1771 CHARS_MODIFF = MODIFF;
1772
1773 bset_point_before_scroll (current_buffer, Qnil);
1774 }
1775
1776 /* Check that it is okay to modify the buffer between START and END,
1777 which are char positions.
1778
1779 Run the before-change-function, if any. If intervals are in use,
1780 verify that the text to be modified is not read-only, and call
1781 any modification properties the text may have.
1782
1783 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1784 by holding its value temporarily in a marker. */
1785
1786 void
1787 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1788 ptrdiff_t *preserve_ptr)
1789 {
1790 struct buffer *base_buffer;
1791
1792 if (!NILP (BVAR (current_buffer, read_only)))
1793 Fbarf_if_buffer_read_only ();
1794
1795 /* If we're modifying the buffer other than shown in a selected window,
1796 let redisplay consider other windows if this buffer is visible. */
1797 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer
1798 && buffer_window_count (current_buffer))
1799 ++windows_or_buffers_changed;
1800
1801 if (buffer_intervals (current_buffer))
1802 {
1803 if (preserve_ptr)
1804 {
1805 Lisp_Object preserve_marker;
1806 struct gcpro gcpro1;
1807 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1808 GCPRO1 (preserve_marker);
1809 verify_interval_modification (current_buffer, start, end);
1810 *preserve_ptr = marker_position (preserve_marker);
1811 unchain_marker (XMARKER (preserve_marker));
1812 UNGCPRO;
1813 }
1814 else
1815 verify_interval_modification (current_buffer, start, end);
1816 }
1817
1818 /* For indirect buffers, use the base buffer to check clashes. */
1819 if (current_buffer->base_buffer != 0)
1820 base_buffer = current_buffer->base_buffer;
1821 else
1822 base_buffer = current_buffer;
1823
1824 #ifdef CLASH_DETECTION
1825 if (!NILP (BVAR (base_buffer, file_truename))
1826 /* Make binding buffer-file-name to nil effective. */
1827 && !NILP (BVAR (base_buffer, filename))
1828 && SAVE_MODIFF >= MODIFF)
1829 lock_file (BVAR (base_buffer, file_truename));
1830 #else
1831 /* At least warn if this file has changed on disk since it was visited. */
1832 if (!NILP (BVAR (base_buffer, filename))
1833 && SAVE_MODIFF >= MODIFF
1834 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1835 && !NILP (Ffile_exists_p (BVAR (base_buffer, filename))))
1836 call1 (intern ("ask-user-about-supersession-threat"),
1837 BVAR (base_buffer,filename));
1838 #endif /* not CLASH_DETECTION */
1839
1840 /* If `select-active-regions' is non-nil, save the region text. */
1841 if (!NILP (BVAR (current_buffer, mark_active))
1842 && !inhibit_modification_hooks
1843 && XMARKER (BVAR (current_buffer, mark))->buffer
1844 && NILP (Vsaved_region_selection)
1845 && (EQ (Vselect_active_regions, Qonly)
1846 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1847 : (!NILP (Vselect_active_regions)
1848 && !NILP (Vtransient_mark_mode))))
1849 {
1850 ptrdiff_t b = marker_position (BVAR (current_buffer, mark));
1851 ptrdiff_t e = PT;
1852 if (b < e)
1853 Vsaved_region_selection = make_buffer_string (b, e, 0);
1854 else if (b > e)
1855 Vsaved_region_selection = make_buffer_string (e, b, 0);
1856 }
1857
1858 signal_before_change (start, end, preserve_ptr);
1859
1860 if (current_buffer->newline_cache)
1861 invalidate_region_cache (current_buffer,
1862 current_buffer->newline_cache,
1863 start - BEG, Z - end);
1864 if (current_buffer->width_run_cache)
1865 invalidate_region_cache (current_buffer,
1866 current_buffer->width_run_cache,
1867 start - BEG, Z - end);
1868
1869 Vdeactivate_mark = Qt;
1870 }
1871 \f
1872 /* These macros work with an argument named `preserve_ptr'
1873 and a local variable named `preserve_marker'. */
1874
1875 #define PRESERVE_VALUE \
1876 if (preserve_ptr && NILP (preserve_marker)) \
1877 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1878
1879 #define RESTORE_VALUE \
1880 if (! NILP (preserve_marker)) \
1881 { \
1882 *preserve_ptr = marker_position (preserve_marker); \
1883 unchain_marker (XMARKER (preserve_marker)); \
1884 }
1885
1886 #define PRESERVE_START_END \
1887 if (NILP (start_marker)) \
1888 start_marker = Fcopy_marker (start, Qnil); \
1889 if (NILP (end_marker)) \
1890 end_marker = Fcopy_marker (end, Qnil);
1891
1892 #define FETCH_START \
1893 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1894
1895 #define FETCH_END \
1896 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1897
1898 /* Set a variable to nil if an error occurred.
1899 Don't change the variable if there was no error.
1900 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1901 VARIABLE is the variable to maybe set to nil.
1902 NO-ERROR-FLAG is nil if there was an error,
1903 anything else meaning no error (so this function does nothing). */
1904 static Lisp_Object
1905 reset_var_on_error (Lisp_Object val)
1906 {
1907 if (NILP (XCDR (val)))
1908 Fset (XCAR (val), Qnil);
1909 return Qnil;
1910 }
1911
1912 /* Signal a change to the buffer immediately before it happens.
1913 START_INT and END_INT are the bounds of the text to be changed.
1914
1915 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1916 by holding its value temporarily in a marker. */
1917
1918 static void
1919 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1920 ptrdiff_t *preserve_ptr)
1921 {
1922 Lisp_Object start, end;
1923 Lisp_Object start_marker, end_marker;
1924 Lisp_Object preserve_marker;
1925 struct gcpro gcpro1, gcpro2, gcpro3;
1926 ptrdiff_t count = SPECPDL_INDEX ();
1927
1928 if (inhibit_modification_hooks)
1929 return;
1930
1931 start = make_number (start_int);
1932 end = make_number (end_int);
1933 preserve_marker = Qnil;
1934 start_marker = Qnil;
1935 end_marker = Qnil;
1936 GCPRO3 (preserve_marker, start_marker, end_marker);
1937
1938 specbind (Qinhibit_modification_hooks, Qt);
1939
1940 /* If buffer is unmodified, run a special hook for that case. The
1941 check for Vfirst_change_hook is just a minor optimization. */
1942 if (SAVE_MODIFF >= MODIFF
1943 && !NILP (Vfirst_change_hook))
1944 {
1945 PRESERVE_VALUE;
1946 PRESERVE_START_END;
1947 Frun_hooks (1, &Qfirst_change_hook);
1948 }
1949
1950 /* Now run the before-change-functions if any. */
1951 if (!NILP (Vbefore_change_functions))
1952 {
1953 Lisp_Object args[3];
1954 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
1955
1956 PRESERVE_VALUE;
1957 PRESERVE_START_END;
1958
1959 /* Mark before-change-functions to be reset to nil in case of error. */
1960 record_unwind_protect (reset_var_on_error, rvoe_arg);
1961
1962 /* Actually run the hook functions. */
1963 args[0] = Qbefore_change_functions;
1964 args[1] = FETCH_START;
1965 args[2] = FETCH_END;
1966 Frun_hook_with_args (3, args);
1967
1968 /* There was no error: unarm the reset_on_error. */
1969 XSETCDR (rvoe_arg, Qt);
1970 }
1971
1972 if (buffer_has_overlays ())
1973 {
1974 PRESERVE_VALUE;
1975 report_overlay_modification (FETCH_START, FETCH_END, 0,
1976 FETCH_START, FETCH_END, Qnil);
1977 }
1978
1979 if (! NILP (start_marker))
1980 free_marker (start_marker);
1981 if (! NILP (end_marker))
1982 free_marker (end_marker);
1983 RESTORE_VALUE;
1984 UNGCPRO;
1985
1986 unbind_to (count, Qnil);
1987 }
1988
1989 /* Signal a change immediately after it happens.
1990 CHARPOS is the character position of the start of the changed text.
1991 LENDEL is the number of characters of the text before the change.
1992 (Not the whole buffer; just the part that was changed.)
1993 LENINS is the number of characters in that part of the text
1994 after the change. */
1995
1996 void
1997 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
1998 {
1999 ptrdiff_t count = SPECPDL_INDEX ();
2000 if (inhibit_modification_hooks)
2001 return;
2002
2003 /* If we are deferring calls to the after-change functions
2004 and there are no before-change functions,
2005 just record the args that we were going to use. */
2006 if (! NILP (Vcombine_after_change_calls)
2007 && NILP (Vbefore_change_functions)
2008 && !buffer_has_overlays ())
2009 {
2010 Lisp_Object elt;
2011
2012 if (!NILP (combine_after_change_list)
2013 && current_buffer != XBUFFER (combine_after_change_buffer))
2014 Fcombine_after_change_execute ();
2015
2016 elt = Fcons (make_number (charpos - BEG),
2017 Fcons (make_number (Z - (charpos - lendel + lenins)),
2018 Fcons (make_number (lenins - lendel), Qnil)));
2019 combine_after_change_list
2020 = Fcons (elt, combine_after_change_list);
2021 combine_after_change_buffer = Fcurrent_buffer ();
2022
2023 return;
2024 }
2025
2026 if (!NILP (combine_after_change_list))
2027 Fcombine_after_change_execute ();
2028
2029 specbind (Qinhibit_modification_hooks, Qt);
2030
2031 if (!NILP (Vafter_change_functions))
2032 {
2033 Lisp_Object args[4];
2034 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2035
2036 /* Mark after-change-functions to be reset to nil in case of error. */
2037 record_unwind_protect (reset_var_on_error, rvoe_arg);
2038
2039 /* Actually run the hook functions. */
2040 args[0] = Qafter_change_functions;
2041 XSETFASTINT (args[1], charpos);
2042 XSETFASTINT (args[2], charpos + lenins);
2043 XSETFASTINT (args[3], lendel);
2044 Frun_hook_with_args (4, args);
2045
2046 /* There was no error: unarm the reset_on_error. */
2047 XSETCDR (rvoe_arg, Qt);
2048 }
2049
2050 if (buffer_has_overlays ())
2051 report_overlay_modification (make_number (charpos),
2052 make_number (charpos + lenins),
2053 1,
2054 make_number (charpos),
2055 make_number (charpos + lenins),
2056 make_number (lendel));
2057
2058 /* After an insertion, call the text properties
2059 insert-behind-hooks or insert-in-front-hooks. */
2060 if (lendel == 0)
2061 report_interval_modification (make_number (charpos),
2062 make_number (charpos + lenins));
2063
2064 unbind_to (count, Qnil);
2065 }
2066
2067 static Lisp_Object
2068 Fcombine_after_change_execute_1 (Lisp_Object val)
2069 {
2070 Vcombine_after_change_calls = val;
2071 return val;
2072 }
2073
2074 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2075 Scombine_after_change_execute, 0, 0, 0,
2076 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2077 (void)
2078 {
2079 ptrdiff_t count = SPECPDL_INDEX ();
2080 ptrdiff_t beg, end, change;
2081 ptrdiff_t begpos, endpos;
2082 Lisp_Object tail;
2083
2084 if (NILP (combine_after_change_list))
2085 return Qnil;
2086
2087 /* It is rare for combine_after_change_buffer to be invalid, but
2088 possible. It can happen when combine-after-change-calls is
2089 non-nil, and insertion calls a file handler (e.g. through
2090 lock_file) which scribbles into a temp file -- cyd */
2091 if (!BUFFERP (combine_after_change_buffer)
2092 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2093 {
2094 combine_after_change_list = Qnil;
2095 return Qnil;
2096 }
2097
2098 record_unwind_current_buffer ();
2099
2100 Fset_buffer (combine_after_change_buffer);
2101
2102 /* # chars unchanged at beginning of buffer. */
2103 beg = Z - BEG;
2104 /* # chars unchanged at end of buffer. */
2105 end = beg;
2106 /* Total amount of insertion (negative for deletion). */
2107 change = 0;
2108
2109 /* Scan the various individual changes,
2110 accumulating the range info in BEG, END and CHANGE. */
2111 for (tail = combine_after_change_list; CONSP (tail);
2112 tail = XCDR (tail))
2113 {
2114 Lisp_Object elt;
2115 ptrdiff_t thisbeg, thisend, thischange;
2116
2117 /* Extract the info from the next element. */
2118 elt = XCAR (tail);
2119 if (! CONSP (elt))
2120 continue;
2121 thisbeg = XINT (XCAR (elt));
2122
2123 elt = XCDR (elt);
2124 if (! CONSP (elt))
2125 continue;
2126 thisend = XINT (XCAR (elt));
2127
2128 elt = XCDR (elt);
2129 if (! CONSP (elt))
2130 continue;
2131 thischange = XINT (XCAR (elt));
2132
2133 /* Merge this range into the accumulated range. */
2134 change += thischange;
2135 if (thisbeg < beg)
2136 beg = thisbeg;
2137 if (thisend < end)
2138 end = thisend;
2139 }
2140
2141 /* Get the current start and end positions of the range
2142 that was changed. */
2143 begpos = BEG + beg;
2144 endpos = Z - end;
2145
2146 /* We are about to handle these, so discard them. */
2147 combine_after_change_list = Qnil;
2148
2149 /* Now run the after-change functions for real.
2150 Turn off the flag that defers them. */
2151 record_unwind_protect (Fcombine_after_change_execute_1,
2152 Vcombine_after_change_calls);
2153 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2154 update_compositions (begpos, endpos, CHECK_ALL);
2155
2156 return unbind_to (count, Qnil);
2157 }
2158 \f
2159 void
2160 syms_of_insdel (void)
2161 {
2162 staticpro (&combine_after_change_list);
2163 staticpro (&combine_after_change_buffer);
2164 combine_after_change_list = Qnil;
2165 combine_after_change_buffer = Qnil;
2166
2167 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2168 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2169 Vcombine_after_change_calls = Qnil;
2170
2171 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2172 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2173 This affects `before-change-functions' and `after-change-functions',
2174 as well as hooks attached to text properties and overlays. */);
2175 inhibit_modification_hooks = 0;
2176 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2177
2178 defsubr (&Scombine_after_change_execute);
2179 }