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