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