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