(browse-url-text-xterm): Unquote browse-url-text-browser.
[emacs.git] / src / insdel.c
blob2919c183ab7fdef12329a8a7d2dab3d486443199
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include <config.h>
25 #include "lisp.h"
26 #include "intervals.h"
27 #include "buffer.h"
28 #include "character.h"
29 #include "window.h"
30 #include "blockinput.h"
31 #include "region-cache.h"
33 #ifndef NULL
34 #define NULL 0
35 #endif
37 static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
38 static void insert_from_buffer_1 ();
39 static void gap_left P_ ((int, int, int));
40 static void gap_right P_ ((int, int));
41 static void adjust_markers_gap_motion P_ ((int, int, int));
42 static void adjust_markers_for_insert P_ ((int, int, int, int, int));
43 void adjust_markers_for_delete P_ ((int, int, int, int));
44 static void adjust_markers_for_replace P_ ((int, int, int, int, int, int));
45 static void adjust_point P_ ((int, int));
47 Lisp_Object Fcombine_after_change_execute ();
49 /* Non-nil means don't call the after-change-functions right away,
50 just record an element in Vcombine_after_change_calls_list. */
51 Lisp_Object Vcombine_after_change_calls;
53 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
54 describing changes which happened while combine_after_change_calls
55 was nonzero. We use this to decide how to call them
56 once the deferral ends.
58 In each element.
59 BEG-UNCHANGED is the number of chars before the changed range.
60 END-UNCHANGED is the number of chars after the changed range,
61 and CHANGE-AMOUNT is the number of characters inserted by the change
62 (negative for a deletion). */
63 Lisp_Object combine_after_change_list;
65 /* Buffer which combine_after_change_list is about. */
66 Lisp_Object combine_after_change_buffer;
68 Lisp_Object Qinhibit_modification_hooks;
71 /* Check all markers in the current buffer, looking for something invalid. */
73 static int check_markers_debug_flag;
75 #define CHECK_MARKERS() \
76 if (check_markers_debug_flag) \
77 check_markers (); \
78 else
80 void
81 check_markers ()
83 register struct Lisp_Marker *tail;
84 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
86 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
88 if (tail->buffer->text != current_buffer->text)
89 abort ();
90 if (tail->charpos > Z)
91 abort ();
92 if (tail->bytepos > Z_BYTE)
93 abort ();
94 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
95 abort ();
99 /* Move gap to position CHARPOS.
100 Note that this can quit! */
102 void
103 move_gap (charpos)
104 int charpos;
106 move_gap_both (charpos, charpos_to_bytepos (charpos));
109 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
110 Note that this can quit! */
112 void
113 move_gap_both (charpos, bytepos)
114 int charpos, bytepos;
116 if (bytepos < GPT_BYTE)
117 gap_left (charpos, bytepos, 0);
118 else if (bytepos > GPT_BYTE)
119 gap_right (charpos, bytepos);
122 /* Move the gap to a position less than the current GPT.
123 BYTEPOS describes the new position as a byte position,
124 and CHARPOS is the corresponding char position.
125 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
127 static void
128 gap_left (charpos, bytepos, newgap)
129 register int charpos, bytepos;
130 int newgap;
132 register unsigned char *to, *from;
133 register int i;
134 int new_s1;
136 if (!newgap)
137 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
139 i = GPT_BYTE;
140 to = GAP_END_ADDR;
141 from = GPT_ADDR;
142 new_s1 = GPT_BYTE;
144 /* Now copy the characters. To move the gap down,
145 copy characters up. */
147 while (1)
149 /* I gets number of characters left to copy. */
150 i = new_s1 - bytepos;
151 if (i == 0)
152 break;
153 /* If a quit is requested, stop copying now.
154 Change BYTEPOS to be where we have actually moved the gap to. */
155 if (QUITP)
157 bytepos = new_s1;
158 charpos = BYTE_TO_CHAR (bytepos);
159 break;
161 /* Move at most 32000 chars before checking again for a quit. */
162 if (i > 32000)
163 i = 32000;
164 #ifdef GAP_USE_BCOPY
165 if (i >= 128
166 /* bcopy is safe if the two areas of memory do not overlap
167 or on systems where bcopy is always safe for moving upward. */
168 && (BCOPY_UPWARD_SAFE
169 || to - from >= 128))
171 /* If overlap is not safe, avoid it by not moving too many
172 characters at once. */
173 if (!BCOPY_UPWARD_SAFE && i > to - from)
174 i = to - from;
175 new_s1 -= i;
176 from -= i, to -= i;
177 bcopy (from, to, i);
179 else
180 #endif
182 new_s1 -= i;
183 while (--i >= 0)
184 *--to = *--from;
188 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
189 BYTEPOS is where the loop above stopped, which may be what was specified
190 or may be where a quit was detected. */
191 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
192 GPT_BYTE = bytepos;
193 GPT = charpos;
194 if (bytepos < charpos)
195 abort ();
196 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
197 QUIT;
200 /* Move the gap to a position greater than than the current GPT.
201 BYTEPOS describes the new position as a byte position,
202 and CHARPOS is the corresponding char position. */
204 static void
205 gap_right (charpos, bytepos)
206 register int charpos, bytepos;
208 register unsigned char *to, *from;
209 register int i;
210 int new_s1;
212 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
214 i = GPT_BYTE;
215 from = GAP_END_ADDR;
216 to = GPT_ADDR;
217 new_s1 = GPT_BYTE;
219 /* Now copy the characters. To move the gap up,
220 copy characters down. */
222 while (1)
224 /* I gets number of characters left to copy. */
225 i = bytepos - new_s1;
226 if (i == 0)
227 break;
228 /* If a quit is requested, stop copying now.
229 Change BYTEPOS to be where we have actually moved the gap to. */
230 if (QUITP)
232 bytepos = new_s1;
233 charpos = BYTE_TO_CHAR (bytepos);
234 break;
236 /* Move at most 32000 chars before checking again for a quit. */
237 if (i > 32000)
238 i = 32000;
239 #ifdef GAP_USE_BCOPY
240 if (i >= 128
241 /* bcopy is safe if the two areas of memory do not overlap
242 or on systems where bcopy is always safe for moving downward. */
243 && (BCOPY_DOWNWARD_SAFE
244 || from - to >= 128))
246 /* If overlap is not safe, avoid it by not moving too many
247 characters at once. */
248 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
249 i = from - to;
250 new_s1 += i;
251 bcopy (from, to, i);
252 from += i, to += i;
254 else
255 #endif
257 new_s1 += i;
258 while (--i >= 0)
259 *to++ = *from++;
263 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
264 - GAP_SIZE);
265 GPT = charpos;
266 GPT_BYTE = bytepos;
267 if (bytepos < charpos)
268 abort ();
269 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
270 QUIT;
273 /* Add AMOUNT to the byte position of every marker in the current buffer
274 whose current byte position is between FROM (exclusive) and TO (inclusive).
276 Also, any markers past the outside of that interval, in the direction
277 of adjustment, are first moved back to the near end of the interval
278 and then adjusted by AMOUNT.
280 When the latter adjustment is done, if AMOUNT is negative,
281 we record the adjustment for undo. (This case happens only for
282 deletion.)
284 The markers' character positions are not altered,
285 because gap motion does not affect character positions. */
287 int adjust_markers_test;
289 static void
290 adjust_markers_gap_motion (from, to, amount)
291 register int from, to, amount;
293 /* Now that a marker has a bytepos, not counting the gap,
294 nothing needs to be done here. */
295 #if 0
296 Lisp_Object marker;
297 register struct Lisp_Marker *m;
298 register int mpos;
300 marker = BUF_MARKERS (current_buffer);
302 while (!NILP (marker))
304 m = XMARKER (marker);
305 mpos = m->bytepos;
306 if (amount > 0)
308 if (mpos > to && mpos < to + amount)
310 if (adjust_markers_test)
311 abort ();
312 mpos = to + amount;
315 else
317 /* Here's the case where a marker is inside text being deleted.
318 AMOUNT can be negative for gap motion, too,
319 but then this range contains no markers. */
320 if (mpos > from + amount && mpos <= from)
322 if (adjust_markers_test)
323 abort ();
324 mpos = from + amount;
327 if (mpos > from && mpos <= to)
328 mpos += amount;
329 m->bufpos = mpos;
330 marker = m->chain;
332 #endif
335 /* Adjust all markers for a deletion
336 whose range in bytes is FROM_BYTE to TO_BYTE.
337 The range in charpos is FROM to TO.
339 This function assumes that the gap is adjacent to
340 or inside of the range being deleted. */
342 void
343 adjust_markers_for_delete (from, from_byte, to, to_byte)
344 register int from, from_byte, to, to_byte;
346 Lisp_Object marker;
347 register struct Lisp_Marker *m;
348 register int charpos;
350 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
352 charpos = m->charpos;
354 if (charpos > Z)
355 abort ();
357 /* If the marker is after the deletion,
358 relocate by number of chars / bytes deleted. */
359 if (charpos > to)
361 m->charpos -= to - from;
362 m->bytepos -= to_byte - from_byte;
364 /* Here's the case where a marker is inside text being deleted. */
365 else if (charpos > from)
367 if (! m->insertion_type)
368 { /* Normal markers will end up at the beginning of the
369 re-inserted text after undoing a deletion, and must be
370 adjusted to move them to the correct place. */
371 XSETMISC (marker, m);
372 record_marker_adjustment (marker, from - charpos);
374 else if (charpos < to)
375 { /* Before-insertion markers will automatically move forward
376 upon re-inserting the deleted text, so we have to arrange
377 for them to move backward to the correct position. */
378 XSETMISC (marker, m);
379 record_marker_adjustment (marker, charpos - to);
381 m->charpos = from;
382 m->bytepos = from_byte;
384 /* Here's the case where a before-insertion marker is immediately
385 before the deleted region. */
386 else if (charpos == from && m->insertion_type)
388 /* Undoing the change uses normal insertion, which will
389 incorrectly make MARKER move forward, so we arrange for it
390 to then move backward to the correct place at the beginning
391 of the deleted region. */
392 XSETMISC (marker, m);
393 record_marker_adjustment (marker, to - from);
399 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
400 to TO / TO_BYTE. We have to relocate the charpos of every marker
401 that points after the insertion (but not their bytepos).
403 When a marker points at the insertion point,
404 we advance it if either its insertion-type is t
405 or BEFORE_MARKERS is true. */
407 static void
408 adjust_markers_for_insert (from, from_byte, to, to_byte, before_markers)
409 register int from, from_byte, to, to_byte;
410 int before_markers;
412 struct Lisp_Marker *m;
413 int adjusted = 0;
414 int nchars = to - from;
415 int nbytes = to_byte - from_byte;
417 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
419 eassert (m->bytepos >= m->charpos
420 && m->bytepos - m->charpos <= Z_BYTE - Z);
422 if (m->bytepos == from_byte)
424 if (m->insertion_type || before_markers)
426 m->bytepos = to_byte;
427 m->charpos = to;
428 if (m->insertion_type)
429 adjusted = 1;
432 else if (m->bytepos > from_byte)
434 m->bytepos += nbytes;
435 m->charpos += nchars;
439 /* Adjusting only markers whose insertion-type is t may result in
440 - disordered start and end in overlays, and
441 - disordered overlays in the slot `overlays_before' of current_buffer. */
442 if (adjusted)
444 fix_start_end_in_overlays(from, to);
445 fix_overlays_before (current_buffer, from, to);
449 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
451 This is used only when the value of point changes due to an insert
452 or delete; it does not represent a conceptual change in point as a
453 marker. In particular, point is not crossing any interval
454 boundaries, so there's no need to use the usual SET_PT macro. In
455 fact it would be incorrect to do so, because either the old or the
456 new value of point is out of sync with the current set of
457 intervals. */
459 static void
460 adjust_point (nchars, nbytes)
461 int nchars, nbytes;
463 BUF_PT (current_buffer) += nchars;
464 BUF_PT_BYTE (current_buffer) += nbytes;
466 /* In a single-byte buffer, the two positions must be equal. */
467 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
470 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
471 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
472 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
473 an insertion. */
475 static void
476 adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
477 new_chars, new_bytes)
478 int from, from_byte, old_chars, old_bytes, new_chars, new_bytes;
480 register struct Lisp_Marker *m;
481 int prev_to_byte = from_byte + old_bytes;
482 int diff_chars = new_chars - old_chars;
483 int diff_bytes = new_bytes - old_bytes;
485 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
487 if (m->bytepos >= prev_to_byte)
489 m->charpos += diff_chars;
490 m->bytepos += diff_bytes;
492 else if (m->bytepos > from_byte)
494 m->charpos = from;
495 m->bytepos = from_byte;
499 CHECK_MARKERS ();
503 /* Make the gap NBYTES_ADDED bytes longer. */
505 void
506 make_gap_larger (nbytes_added)
507 int nbytes_added;
509 Lisp_Object tem;
510 int real_gap_loc;
511 int real_gap_loc_byte;
512 int old_gap_size;
514 /* If we have to get more space, get enough to last a while. */
515 nbytes_added += 2000;
517 /* Don't allow a buffer size that won't fit in an int
518 even if it will fit in a Lisp integer.
519 That won't work because so many places use `int'.
521 Make sure we don't introduce overflows in the calculation. */
523 if (Z_BYTE - BEG_BYTE + GAP_SIZE
524 >= (((EMACS_INT) 1 << (min (VALBITS, BITS_PER_INT) - 1)) - 1
525 - nbytes_added))
526 error ("Buffer exceeds maximum size");
528 enlarge_buffer_text (current_buffer, nbytes_added);
530 /* Prevent quitting in move_gap. */
531 tem = Vinhibit_quit;
532 Vinhibit_quit = Qt;
534 real_gap_loc = GPT;
535 real_gap_loc_byte = GPT_BYTE;
536 old_gap_size = GAP_SIZE;
538 /* Call the newly allocated space a gap at the end of the whole space. */
539 GPT = Z + GAP_SIZE;
540 GPT_BYTE = Z_BYTE + GAP_SIZE;
541 GAP_SIZE = nbytes_added;
543 /* Move the new gap down to be consecutive with the end of the old one.
544 This adjusts the markers properly too. */
545 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
547 /* Now combine the two into one large gap. */
548 GAP_SIZE += old_gap_size;
549 GPT = real_gap_loc;
550 GPT_BYTE = real_gap_loc_byte;
552 /* Put an anchor. */
553 *(Z_ADDR) = 0;
555 Vinhibit_quit = tem;
559 /* Make the gap NBYTES_REMOVED bytes shorter. */
561 void
562 make_gap_smaller (nbytes_removed)
563 int nbytes_removed;
565 Lisp_Object tem;
566 int real_gap_loc;
567 int real_gap_loc_byte;
568 int real_Z;
569 int real_Z_byte;
570 int real_beg_unchanged;
571 int new_gap_size;
573 /* Make sure the gap is at least 20 bytes. */
574 if (GAP_SIZE - nbytes_removed < 20)
575 nbytes_removed = GAP_SIZE - 20;
577 /* Prevent quitting in move_gap. */
578 tem = Vinhibit_quit;
579 Vinhibit_quit = Qt;
581 real_gap_loc = GPT;
582 real_gap_loc_byte = GPT_BYTE;
583 new_gap_size = GAP_SIZE - nbytes_removed;
584 real_Z = Z;
585 real_Z_byte = Z_BYTE;
586 real_beg_unchanged = BEG_UNCHANGED;
588 /* Pretend that the last unwanted part of the gap is the entire gap,
589 and that the first desired part of the gap is part of the buffer
590 text. */
591 bzero (GPT_ADDR, new_gap_size);
592 GPT += new_gap_size;
593 GPT_BYTE += new_gap_size;
594 Z += new_gap_size;
595 Z_BYTE += new_gap_size;
596 GAP_SIZE = nbytes_removed;
598 /* Move the unwanted pretend gap to the end of the buffer. This
599 adjusts the markers properly too. */
600 gap_right (Z, Z_BYTE);
602 enlarge_buffer_text (current_buffer, -nbytes_removed);
604 /* Now restore the desired gap. */
605 GAP_SIZE = new_gap_size;
606 GPT = real_gap_loc;
607 GPT_BYTE = real_gap_loc_byte;
608 Z = real_Z;
609 Z_BYTE = real_Z_byte;
610 BEG_UNCHANGED = real_beg_unchanged;
612 /* Put an anchor. */
613 *(Z_ADDR) = 0;
615 Vinhibit_quit = tem;
618 void
619 make_gap (nbytes_added)
620 int nbytes_added;
622 if (nbytes_added >= 0)
623 make_gap_larger (nbytes_added);
624 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
625 else
626 make_gap_smaller (-nbytes_added);
627 #endif
630 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
631 FROM_MULTIBYTE says whether the incoming text is multibyte.
632 TO_MULTIBYTE says whether to store the text as multibyte.
633 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
635 Return the number of bytes stored at TO_ADDR. */
638 copy_text (from_addr, to_addr, nbytes,
639 from_multibyte, to_multibyte)
640 const unsigned char *from_addr;
641 unsigned char *to_addr;
642 int nbytes;
643 int from_multibyte, to_multibyte;
645 if (from_multibyte == to_multibyte)
647 bcopy (from_addr, to_addr, nbytes);
648 return nbytes;
650 else if (from_multibyte)
652 int nchars = 0;
653 int bytes_left = nbytes;
654 Lisp_Object tbl = Qnil;
656 while (bytes_left > 0)
658 int thislen, c;
659 c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
660 if (!ASCII_CHAR_P (c))
661 c = multibyte_char_to_unibyte (c, tbl);
662 *to_addr++ = c;
663 from_addr += thislen;
664 bytes_left -= thislen;
665 nchars++;
667 return nchars;
669 else
671 unsigned char *initial_to_addr = to_addr;
673 /* Convert single-byte to multibyte. */
674 while (nbytes > 0)
676 int c = *from_addr++;
678 if (c >= 0200)
680 c = unibyte_char_to_multibyte (c);
681 to_addr += CHAR_STRING (c, to_addr);
682 nbytes--;
684 else
685 /* Special case for speed. */
686 *to_addr++ = c, nbytes--;
688 return to_addr - initial_to_addr;
692 /* Return the number of bytes it would take
693 to convert some single-byte text to multibyte.
694 The single-byte text consists of NBYTES bytes at PTR. */
697 count_size_as_multibyte (ptr, nbytes)
698 const unsigned char *ptr;
699 int nbytes;
701 int i;
702 int outgoing_nbytes = 0;
704 for (i = 0; i < nbytes; i++)
706 unsigned int c = *ptr++;
708 if (c < 0200)
709 outgoing_nbytes++;
710 else
712 c = unibyte_char_to_multibyte (c);
713 outgoing_nbytes += CHAR_BYTES (c);
717 return outgoing_nbytes;
720 /* Insert a string of specified length before point.
721 This function judges multibyteness based on
722 enable_multibyte_characters in the current buffer;
723 it never converts between single-byte and multibyte.
725 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
726 prepare_to_modify_buffer could relocate the text. */
728 void
729 insert (string, nbytes)
730 register const unsigned char *string;
731 register int nbytes;
733 if (nbytes > 0)
735 int len = chars_in_text (string, nbytes), opoint;
736 insert_1_both (string, len, nbytes, 0, 1, 0);
737 opoint = PT - len;
738 signal_after_change (opoint, 0, len);
739 update_compositions (opoint, PT, CHECK_BORDER);
743 /* Likewise, but inherit text properties from neighboring characters. */
745 void
746 insert_and_inherit (string, nbytes)
747 register const unsigned char *string;
748 register int nbytes;
750 if (nbytes > 0)
752 int len = chars_in_text (string, nbytes), opoint;
753 insert_1_both (string, len, nbytes, 1, 1, 0);
754 opoint = PT - len;
755 signal_after_change (opoint, 0, len);
756 update_compositions (opoint, PT, CHECK_BORDER);
760 /* Insert the character C before point. Do not inherit text properties. */
762 void
763 insert_char (c)
764 int c;
766 unsigned char str[MAX_MULTIBYTE_LENGTH];
767 int len;
769 if (! NILP (current_buffer->enable_multibyte_characters))
770 len = CHAR_STRING (c, str);
771 else
773 len = 1;
774 str[0] = c;
777 insert (str, len);
780 /* Insert the null-terminated string S before point. */
782 void
783 insert_string (s)
784 const char *s;
786 insert (s, strlen (s));
789 /* Like `insert' except that all markers pointing at the place where
790 the insertion happens are adjusted to point after it.
791 Don't use this function to insert part of a Lisp string,
792 since gc could happen and relocate it. */
794 void
795 insert_before_markers (string, nbytes)
796 const unsigned char *string;
797 register int nbytes;
799 if (nbytes > 0)
801 int len = chars_in_text (string, nbytes), opoint;
802 insert_1_both (string, len, nbytes, 0, 1, 1);
803 opoint = PT - len;
804 signal_after_change (opoint, 0, len);
805 update_compositions (opoint, PT, CHECK_BORDER);
809 /* Likewise, but inherit text properties from neighboring characters. */
811 void
812 insert_before_markers_and_inherit (string, nbytes)
813 const unsigned char *string;
814 register int nbytes;
816 if (nbytes > 0)
818 int len = chars_in_text (string, nbytes), opoint;
819 insert_1_both (string, len, nbytes, 1, 1, 1);
820 opoint = PT - len;
821 signal_after_change (opoint, 0, len);
822 update_compositions (opoint, PT, CHECK_BORDER);
826 /* Subroutine used by the insert functions above. */
828 void
829 insert_1 (string, nbytes, inherit, prepare, before_markers)
830 register const unsigned char *string;
831 register int nbytes;
832 int inherit, prepare, before_markers;
834 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
835 inherit, prepare, before_markers);
839 #ifdef BYTE_COMBINING_DEBUG
841 /* See if the bytes before POS/POS_BYTE combine with bytes
842 at the start of STRING to form a single character.
843 If so, return the number of bytes at the start of STRING
844 which combine in this way. Otherwise, return 0. */
847 count_combining_before (string, length, pos, pos_byte)
848 const unsigned char *string;
849 int length;
850 int pos, pos_byte;
852 int len, combining_bytes;
853 const unsigned char *p;
855 if (NILP (current_buffer->enable_multibyte_characters))
856 return 0;
858 /* At first, we can exclude the following cases:
859 (1) STRING[0] can't be a following byte of multibyte sequence.
860 (2) POS is the start of the current buffer.
861 (3) A character before POS is not a multibyte character. */
862 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
863 return 0;
864 if (pos_byte == BEG_BYTE) /* case (2) */
865 return 0;
866 len = 1;
867 p = BYTE_POS_ADDR (pos_byte - 1);
868 while (! CHAR_HEAD_P (*p)) p--, len++;
869 if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
870 return 0;
872 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
873 if (combining_bytes <= 0)
874 /* The character preceding POS is, complete and no room for
875 combining bytes (combining_bytes == 0), or an independent 8-bit
876 character (combining_bytes < 0). */
877 return 0;
879 /* We have a combination situation. Count the bytes at STRING that
880 may combine. */
881 p = string + 1;
882 while (!CHAR_HEAD_P (*p) && p < string + length)
883 p++;
885 return (combining_bytes < p - string ? combining_bytes : p - string);
888 /* See if the bytes after POS/POS_BYTE combine with bytes
889 at the end of STRING to form a single character.
890 If so, return the number of bytes after POS/POS_BYTE
891 which combine in this way. Otherwise, return 0. */
894 count_combining_after (string, length, pos, pos_byte)
895 const unsigned char *string;
896 int length;
897 int pos, pos_byte;
899 int opos_byte = pos_byte;
900 int i;
901 int bytes;
902 unsigned char *bufp;
904 if (NILP (current_buffer->enable_multibyte_characters))
905 return 0;
907 /* At first, we can exclude the following cases:
908 (1) The last byte of STRING is an ASCII.
909 (2) POS is the last of the current buffer.
910 (3) A character at POS can't be a following byte of multibyte
911 character. */
912 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
913 return 0;
914 if (pos_byte == Z_BYTE) /* case (2) */
915 return 0;
916 bufp = BYTE_POS_ADDR (pos_byte);
917 if (CHAR_HEAD_P (*bufp)) /* case (3) */
918 return 0;
920 i = length - 1;
921 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
923 i--;
925 if (i < 0)
927 /* All characters in STRING are not character head. We must
928 check also preceding bytes at POS. We are sure that the gap
929 is at POS. */
930 unsigned char *p = BEG_ADDR;
931 i = pos_byte - 2;
932 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
933 i--;
934 if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
935 return 0;
937 bytes = BYTES_BY_CHAR_HEAD (p[i]);
938 return (bytes <= pos_byte - 1 - i + length
940 : bytes - (pos_byte - 1 - i + length));
942 if (!BASE_LEADING_CODE_P (string[i]))
943 return 0;
945 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
946 bufp++, pos_byte++;
947 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
949 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
952 #endif
955 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
956 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
957 are the same as in insert_1. */
959 void
960 insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
961 register const unsigned char *string;
962 register int nchars, nbytes;
963 int inherit, prepare, before_markers;
965 if (nchars == 0)
966 return;
968 if (NILP (current_buffer->enable_multibyte_characters))
969 nchars = nbytes;
971 if (prepare)
972 /* Do this before moving and increasing the gap,
973 because the before-change hooks might move the gap
974 or make it smaller. */
975 prepare_to_modify_buffer (PT, PT, NULL);
977 if (PT != GPT)
978 move_gap_both (PT, PT_BYTE);
979 if (GAP_SIZE < nbytes)
980 make_gap (nbytes - GAP_SIZE);
982 #ifdef BYTE_COMBINING_DEBUG
983 if (count_combining_before (string, nbytes, PT, PT_BYTE)
984 || count_combining_after (string, nbytes, PT, PT_BYTE))
985 abort ();
986 #endif
988 /* Record deletion of the surrounding text that combines with
989 the insertion. This, together with recording the insertion,
990 will add up to the right stuff in the undo list. */
991 record_insert (PT, nchars);
992 MODIFF++;
993 CHARS_MODIFF = MODIFF;
995 bcopy (string, GPT_ADDR, nbytes);
997 GAP_SIZE -= nbytes;
998 GPT += nchars;
999 ZV += nchars;
1000 Z += nchars;
1001 GPT_BYTE += nbytes;
1002 ZV_BYTE += nbytes;
1003 Z_BYTE += nbytes;
1004 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1006 if (GPT_BYTE < GPT)
1007 abort ();
1009 /* The insert may have been in the unchanged region, so check again. */
1010 if (Z - GPT < END_UNCHANGED)
1011 END_UNCHANGED = Z - GPT;
1013 adjust_overlays_for_insert (PT, nchars);
1014 adjust_markers_for_insert (PT, PT_BYTE,
1015 PT + nchars, PT_BYTE + nbytes,
1016 before_markers);
1018 if (BUF_INTERVALS (current_buffer) != 0)
1019 offset_intervals (current_buffer, PT, nchars);
1021 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
1022 set_text_properties (make_number (PT), make_number (PT + nchars),
1023 Qnil, Qnil, Qnil);
1025 adjust_point (nchars, nbytes);
1027 CHECK_MARKERS ();
1030 /* Insert the part of the text of STRING, a Lisp object assumed to be
1031 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1032 starting at position POS / POS_BYTE. If the text of STRING has properties,
1033 copy them into the buffer.
1035 It does not work to use `insert' for this, because a GC could happen
1036 before we bcopy the stuff into the buffer, and relocate the string
1037 without insert noticing. */
1039 void
1040 insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
1041 Lisp_Object string;
1042 register int pos, pos_byte, length, length_byte;
1043 int inherit;
1045 int opoint = PT;
1047 if (SCHARS (string) == 0)
1048 return;
1050 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1051 inherit, 0);
1052 signal_after_change (opoint, 0, PT - opoint);
1053 update_compositions (opoint, PT, CHECK_BORDER);
1056 /* Like `insert_from_string' except that all markers pointing
1057 at the place where the insertion happens are adjusted to point after it. */
1059 void
1060 insert_from_string_before_markers (string, pos, pos_byte,
1061 length, length_byte, inherit)
1062 Lisp_Object string;
1063 register int pos, pos_byte, length, length_byte;
1064 int inherit;
1066 int opoint = PT;
1068 if (SCHARS (string) == 0)
1069 return;
1071 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1072 inherit, 1);
1073 signal_after_change (opoint, 0, PT - opoint);
1074 update_compositions (opoint, PT, CHECK_BORDER);
1077 /* Subroutine of the insertion functions above. */
1079 static void
1080 insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
1081 inherit, before_markers)
1082 Lisp_Object string;
1083 register int pos, pos_byte, nchars, nbytes;
1084 int inherit, before_markers;
1086 struct gcpro gcpro1;
1087 int outgoing_nbytes = nbytes;
1088 INTERVAL intervals;
1090 /* Make OUTGOING_NBYTES describe the text
1091 as it will be inserted in this buffer. */
1093 if (NILP (current_buffer->enable_multibyte_characters))
1094 outgoing_nbytes = nchars;
1095 else if (! STRING_MULTIBYTE (string))
1096 outgoing_nbytes
1097 = count_size_as_multibyte (SDATA (string) + pos_byte,
1098 nbytes);
1100 GCPRO1 (string);
1101 /* Do this before moving and increasing the gap,
1102 because the before-change hooks might move the gap
1103 or make it smaller. */
1104 prepare_to_modify_buffer (PT, PT, NULL);
1106 if (PT != GPT)
1107 move_gap_both (PT, PT_BYTE);
1108 if (GAP_SIZE < outgoing_nbytes)
1109 make_gap (outgoing_nbytes - GAP_SIZE);
1110 UNGCPRO;
1112 /* Copy the string text into the buffer, perhaps converting
1113 between single-byte and multibyte. */
1114 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
1115 STRING_MULTIBYTE (string),
1116 ! NILP (current_buffer->enable_multibyte_characters));
1118 #ifdef BYTE_COMBINING_DEBUG
1119 /* We have copied text into the gap, but we have not altered
1120 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1121 to these functions and get the same results as we would
1122 have got earlier on. Meanwhile, PT_ADDR does point to
1123 the text that has been stored by copy_text. */
1124 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1125 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1126 abort ();
1127 #endif
1129 record_insert (PT, nchars);
1130 MODIFF++;
1131 CHARS_MODIFF = MODIFF;
1133 GAP_SIZE -= outgoing_nbytes;
1134 GPT += nchars;
1135 ZV += nchars;
1136 Z += nchars;
1137 GPT_BYTE += outgoing_nbytes;
1138 ZV_BYTE += outgoing_nbytes;
1139 Z_BYTE += outgoing_nbytes;
1140 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1142 if (GPT_BYTE < GPT)
1143 abort ();
1145 /* The insert may have been in the unchanged region, so check again. */
1146 if (Z - GPT < END_UNCHANGED)
1147 END_UNCHANGED = Z - GPT;
1149 adjust_overlays_for_insert (PT, nchars);
1150 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1151 PT_BYTE + outgoing_nbytes,
1152 before_markers);
1154 offset_intervals (current_buffer, PT, nchars);
1156 intervals = STRING_INTERVALS (string);
1157 /* Get the intervals for the part of the string we are inserting. */
1158 if (nbytes < SBYTES (string))
1159 intervals = copy_intervals (intervals, pos, nchars);
1161 /* Insert those intervals. */
1162 graft_intervals_into_buffer (intervals, PT, nchars,
1163 current_buffer, inherit);
1165 adjust_point (nchars, outgoing_nbytes);
1167 CHECK_MARKERS ();
1170 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1171 starting at GPT_ADDR. */
1173 void
1174 insert_from_gap (nchars, nbytes)
1175 register EMACS_INT nchars, nbytes;
1177 if (NILP (current_buffer->enable_multibyte_characters))
1178 nchars = nbytes;
1180 record_insert (GPT, nchars);
1181 MODIFF++;
1183 GAP_SIZE -= nbytes;
1184 GPT += nchars;
1185 ZV += nchars;
1186 Z += nchars;
1187 GPT_BYTE += nbytes;
1188 ZV_BYTE += nbytes;
1189 Z_BYTE += nbytes;
1190 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1192 if (GPT_BYTE < GPT)
1193 abort ();
1195 adjust_overlays_for_insert (GPT - nchars, nchars);
1196 adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
1197 GPT, GPT_BYTE, 0);
1199 if (BUF_INTERVALS (current_buffer) != 0)
1201 offset_intervals (current_buffer, GPT - nchars, nchars);
1202 graft_intervals_into_buffer (NULL_INTERVAL, GPT - nchars, nchars,
1203 current_buffer, 0);
1206 if (GPT - nchars < PT)
1207 adjust_point (nchars, nbytes);
1209 CHECK_MARKERS ();
1212 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1213 current buffer. If the text in BUF has properties, they are absorbed
1214 into the current buffer.
1216 It does not work to use `insert' for this, because a malloc could happen
1217 and relocate BUF's text before the bcopy happens. */
1219 void
1220 insert_from_buffer (buf, charpos, nchars, inherit)
1221 struct buffer *buf;
1222 int charpos, nchars;
1223 int inherit;
1225 int opoint = PT;
1227 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1228 signal_after_change (opoint, 0, PT - opoint);
1229 update_compositions (opoint, PT, CHECK_BORDER);
1232 static void
1233 insert_from_buffer_1 (buf, from, nchars, inherit)
1234 struct buffer *buf;
1235 int from, nchars;
1236 int inherit;
1238 register Lisp_Object temp;
1239 int chunk, chunk_expanded;
1240 int from_byte = buf_charpos_to_bytepos (buf, from);
1241 int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1242 int incoming_nbytes = to_byte - from_byte;
1243 int outgoing_nbytes = incoming_nbytes;
1244 INTERVAL intervals;
1246 /* Make OUTGOING_NBYTES describe the text
1247 as it will be inserted in this buffer. */
1249 if (NILP (current_buffer->enable_multibyte_characters))
1250 outgoing_nbytes = nchars;
1251 else if (NILP (buf->enable_multibyte_characters))
1253 int outgoing_before_gap = 0;
1254 int outgoing_after_gap = 0;
1256 if (from < BUF_GPT (buf))
1258 chunk = BUF_GPT_BYTE (buf) - from_byte;
1259 if (chunk > incoming_nbytes)
1260 chunk = incoming_nbytes;
1261 outgoing_before_gap
1262 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1263 chunk);
1265 else
1266 chunk = 0;
1268 if (chunk < incoming_nbytes)
1269 outgoing_after_gap
1270 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1271 from_byte + chunk),
1272 incoming_nbytes - chunk);
1274 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1277 /* Make sure point-max won't overflow after this insertion. */
1278 XSETINT (temp, outgoing_nbytes + Z);
1279 if (outgoing_nbytes + Z != XINT (temp))
1280 error ("Maximum buffer size exceeded");
1282 /* Do this before moving and increasing the gap,
1283 because the before-change hooks might move the gap
1284 or make it smaller. */
1285 prepare_to_modify_buffer (PT, PT, NULL);
1287 if (PT != GPT)
1288 move_gap_both (PT, PT_BYTE);
1289 if (GAP_SIZE < outgoing_nbytes)
1290 make_gap (outgoing_nbytes - GAP_SIZE);
1292 if (from < BUF_GPT (buf))
1294 chunk = BUF_GPT_BYTE (buf) - from_byte;
1295 if (chunk > incoming_nbytes)
1296 chunk = incoming_nbytes;
1297 /* Record number of output bytes, so we know where
1298 to put the output from the second copy_text. */
1299 chunk_expanded
1300 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1301 GPT_ADDR, chunk,
1302 ! NILP (buf->enable_multibyte_characters),
1303 ! NILP (current_buffer->enable_multibyte_characters));
1305 else
1306 chunk_expanded = chunk = 0;
1308 if (chunk < incoming_nbytes)
1309 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1310 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1311 ! NILP (buf->enable_multibyte_characters),
1312 ! NILP (current_buffer->enable_multibyte_characters));
1314 #ifdef BYTE_COMBINING_DEBUG
1315 /* We have copied text into the gap, but we have not altered
1316 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1317 to these functions and get the same results as we would
1318 have got earlier on. Meanwhile, GPT_ADDR does point to
1319 the text that has been stored by copy_text. */
1320 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1321 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1322 abort ();
1323 #endif
1325 record_insert (PT, nchars);
1326 MODIFF++;
1327 CHARS_MODIFF = MODIFF;
1329 GAP_SIZE -= outgoing_nbytes;
1330 GPT += nchars;
1331 ZV += nchars;
1332 Z += nchars;
1333 GPT_BYTE += outgoing_nbytes;
1334 ZV_BYTE += outgoing_nbytes;
1335 Z_BYTE += outgoing_nbytes;
1336 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1338 if (GPT_BYTE < GPT)
1339 abort ();
1341 /* The insert may have been in the unchanged region, so check again. */
1342 if (Z - GPT < END_UNCHANGED)
1343 END_UNCHANGED = Z - GPT;
1345 adjust_overlays_for_insert (PT, nchars);
1346 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1347 PT_BYTE + outgoing_nbytes,
1350 if (BUF_INTERVALS (current_buffer) != 0)
1351 offset_intervals (current_buffer, PT, nchars);
1353 /* Get the intervals for the part of the string we are inserting. */
1354 intervals = BUF_INTERVALS (buf);
1355 if (outgoing_nbytes < BUF_Z_BYTE (buf) - BUF_BEG_BYTE (buf))
1357 if (buf == current_buffer && PT <= from)
1358 from += nchars;
1359 intervals = copy_intervals (intervals, from, nchars);
1362 /* Insert those intervals. */
1363 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1365 adjust_point (nchars, outgoing_nbytes);
1368 /* Record undo information and adjust markers and position keepers for
1369 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1370 chars (LEN_BYTE bytes) which resides in the gap just after
1371 GPT_ADDR.
1373 PREV_TEXT nil means the new text was just inserted. */
1375 void
1376 adjust_after_replace (from, from_byte, prev_text, len, len_byte)
1377 int from, from_byte, len, len_byte;
1378 Lisp_Object prev_text;
1380 int nchars_del = 0, nbytes_del = 0;
1382 #ifdef BYTE_COMBINING_DEBUG
1383 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1384 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1385 abort ();
1386 #endif
1388 if (STRINGP (prev_text))
1390 nchars_del = SCHARS (prev_text);
1391 nbytes_del = SBYTES (prev_text);
1394 /* Update various buffer positions for the new text. */
1395 GAP_SIZE -= len_byte;
1396 ZV += len; Z+= len;
1397 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1398 GPT += len; GPT_BYTE += len_byte;
1399 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1401 if (nchars_del > 0)
1402 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1403 len, len_byte);
1404 else
1405 adjust_markers_for_insert (from, from_byte,
1406 from + len, from_byte + len_byte, 0);
1408 if (! EQ (current_buffer->undo_list, Qt))
1410 if (nchars_del > 0)
1411 record_delete (from, prev_text);
1412 record_insert (from, len);
1415 if (len > nchars_del)
1416 adjust_overlays_for_insert (from, len - nchars_del);
1417 else if (len < nchars_del)
1418 adjust_overlays_for_delete (from, nchars_del - len);
1419 if (BUF_INTERVALS (current_buffer) != 0)
1421 offset_intervals (current_buffer, from, len - nchars_del);
1424 if (from < PT)
1425 adjust_point (len - nchars_del, len_byte - nbytes_del);
1427 /* As byte combining will decrease Z, we must check this again. */
1428 if (Z - GPT < END_UNCHANGED)
1429 END_UNCHANGED = Z - GPT;
1431 CHECK_MARKERS ();
1433 if (len == 0)
1434 evaporate_overlays (from);
1435 MODIFF++;
1436 CHARS_MODIFF = MODIFF;
1439 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1440 This is for use when undo is not enabled in the current buffer. */
1442 void
1443 adjust_after_replace_noundo (from, from_byte, nchars_del, nbytes_del, len, len_byte)
1444 int from, from_byte, nchars_del, nbytes_del, len, len_byte;
1446 #ifdef BYTE_COMBINING_DEBUG
1447 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1448 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1449 abort ();
1450 #endif
1452 /* Update various buffer positions for the new text. */
1453 GAP_SIZE -= len_byte;
1454 ZV += len; Z+= len;
1455 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1456 GPT += len; GPT_BYTE += len_byte;
1457 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1459 if (nchars_del > 0)
1460 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1461 len, len_byte);
1462 else
1463 adjust_markers_for_insert (from, from_byte,
1464 from + len, from_byte + len_byte, 0);
1466 if (len > nchars_del)
1467 adjust_overlays_for_insert (from, len - nchars_del);
1468 else if (len < nchars_del)
1469 adjust_overlays_for_delete (from, nchars_del - len);
1470 if (BUF_INTERVALS (current_buffer) != 0)
1472 offset_intervals (current_buffer, from, len - nchars_del);
1475 if (from < PT)
1476 adjust_point (len - nchars_del, len_byte - nbytes_del);
1478 /* As byte combining will decrease Z, we must check this again. */
1479 if (Z - GPT < END_UNCHANGED)
1480 END_UNCHANGED = Z - GPT;
1482 CHECK_MARKERS ();
1484 if (len == 0)
1485 evaporate_overlays (from);
1486 MODIFF++;
1487 CHARS_MODIFF = MODIFF;
1490 /* Record undo information, adjust markers and position keepers for an
1491 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1492 text already exists in the current buffer but character length (TO
1493 - FROM) may be incorrect, the correct length is NEWLEN. */
1495 void
1496 adjust_after_insert (from, from_byte, to, to_byte, newlen)
1497 int from, from_byte, to, to_byte, newlen;
1499 int len = to - from, len_byte = to_byte - from_byte;
1501 if (GPT != to)
1502 move_gap_both (to, to_byte);
1503 GAP_SIZE += len_byte;
1504 GPT -= len; GPT_BYTE -= len_byte;
1505 ZV -= len; ZV_BYTE -= len_byte;
1506 Z -= len; Z_BYTE -= len_byte;
1507 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1510 /* Replace the text from character positions FROM to TO with NEW,
1511 If PREPARE is nonzero, call prepare_to_modify_buffer.
1512 If INHERIT, the newly inserted text should inherit text properties
1513 from the surrounding non-deleted text. */
1515 /* Note that this does not yet handle markers quite right.
1516 Also it needs to record a single undo-entry that does a replacement
1517 rather than a separate delete and insert.
1518 That way, undo will also handle markers properly.
1520 But if MARKERS is 0, don't relocate markers. */
1522 void
1523 replace_range (from, to, new, prepare, inherit, markers)
1524 Lisp_Object new;
1525 int from, to, prepare, inherit, markers;
1527 int inschars = SCHARS (new);
1528 int insbytes = SBYTES (new);
1529 int from_byte, to_byte;
1530 int nbytes_del, nchars_del;
1531 register Lisp_Object temp;
1532 struct gcpro gcpro1;
1533 INTERVAL intervals;
1534 int outgoing_insbytes = insbytes;
1535 Lisp_Object deletion;
1537 CHECK_MARKERS ();
1539 GCPRO1 (new);
1540 deletion = Qnil;
1542 if (prepare)
1544 int range_length = to - from;
1545 prepare_to_modify_buffer (from, to, &from);
1546 to = from + range_length;
1549 UNGCPRO;
1551 /* Make args be valid */
1552 if (from < BEGV)
1553 from = BEGV;
1554 if (to > ZV)
1555 to = ZV;
1557 from_byte = CHAR_TO_BYTE (from);
1558 to_byte = CHAR_TO_BYTE (to);
1560 nchars_del = to - from;
1561 nbytes_del = to_byte - from_byte;
1563 if (nbytes_del <= 0 && insbytes == 0)
1564 return;
1566 /* Make OUTGOING_INSBYTES describe the text
1567 as it will be inserted in this buffer. */
1569 if (NILP (current_buffer->enable_multibyte_characters))
1570 outgoing_insbytes = inschars;
1571 else if (! STRING_MULTIBYTE (new))
1572 outgoing_insbytes
1573 = count_size_as_multibyte (SDATA (new), insbytes);
1575 /* Make sure point-max won't overflow after this insertion. */
1576 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1577 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1578 error ("Maximum buffer size exceeded");
1580 GCPRO1 (new);
1582 /* Make sure the gap is somewhere in or next to what we are deleting. */
1583 if (from > GPT)
1584 gap_right (from, from_byte);
1585 if (to < GPT)
1586 gap_left (to, to_byte, 0);
1588 /* Even if we don't record for undo, we must keep the original text
1589 because we may have to recover it because of inappropriate byte
1590 combining. */
1591 if (! EQ (current_buffer->undo_list, Qt))
1592 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1594 GAP_SIZE += nbytes_del;
1595 ZV -= nchars_del;
1596 Z -= nchars_del;
1597 ZV_BYTE -= nbytes_del;
1598 Z_BYTE -= nbytes_del;
1599 GPT = from;
1600 GPT_BYTE = from_byte;
1601 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1603 if (GPT_BYTE < GPT)
1604 abort ();
1606 if (GPT - BEG < BEG_UNCHANGED)
1607 BEG_UNCHANGED = GPT - BEG;
1608 if (Z - GPT < END_UNCHANGED)
1609 END_UNCHANGED = Z - GPT;
1611 if (GAP_SIZE < insbytes)
1612 make_gap (insbytes - GAP_SIZE);
1614 /* Copy the string text into the buffer, perhaps converting
1615 between single-byte and multibyte. */
1616 copy_text (SDATA (new), GPT_ADDR, insbytes,
1617 STRING_MULTIBYTE (new),
1618 ! NILP (current_buffer->enable_multibyte_characters));
1620 #ifdef BYTE_COMBINING_DEBUG
1621 /* We have copied text into the gap, but we have not marked
1622 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1623 here, for both the previous text and the following text.
1624 Meanwhile, GPT_ADDR does point to
1625 the text that has been stored by copy_text. */
1626 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1627 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1628 abort ();
1629 #endif
1631 if (! EQ (current_buffer->undo_list, Qt))
1633 /* Record the insertion first, so that when we undo,
1634 the deletion will be undone first. Thus, undo
1635 will insert before deleting, and thus will keep
1636 the markers before and after this text separate. */
1637 record_insert (from + SCHARS (deletion), inschars);
1638 record_delete (from, deletion);
1641 GAP_SIZE -= outgoing_insbytes;
1642 GPT += inschars;
1643 ZV += inschars;
1644 Z += inschars;
1645 GPT_BYTE += outgoing_insbytes;
1646 ZV_BYTE += outgoing_insbytes;
1647 Z_BYTE += outgoing_insbytes;
1648 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1650 if (GPT_BYTE < GPT)
1651 abort ();
1653 /* Adjust the overlay center as needed. This must be done after
1654 adjusting the markers that bound the overlays. */
1655 adjust_overlays_for_delete (from, nchars_del);
1656 adjust_overlays_for_insert (from, inschars);
1658 /* Adjust markers for the deletion and the insertion. */
1659 if (markers)
1660 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1661 inschars, outgoing_insbytes);
1663 offset_intervals (current_buffer, from, inschars - nchars_del);
1665 /* Get the intervals for the part of the string we are inserting--
1666 not including the combined-before bytes. */
1667 intervals = STRING_INTERVALS (new);
1668 /* Insert those intervals. */
1669 graft_intervals_into_buffer (intervals, from, inschars,
1670 current_buffer, inherit);
1672 /* Relocate point as if it were a marker. */
1673 if (from < PT)
1674 adjust_point ((from + inschars - (PT < to ? PT : to)),
1675 (from_byte + outgoing_insbytes
1676 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1678 if (outgoing_insbytes == 0)
1679 evaporate_overlays (from);
1681 CHECK_MARKERS ();
1683 MODIFF++;
1684 CHARS_MODIFF = MODIFF;
1685 UNGCPRO;
1687 signal_after_change (from, nchars_del, GPT - from);
1688 update_compositions (from, GPT, CHECK_BORDER);
1691 /* Replace the text from character positions FROM to TO with
1692 the text in INS of length INSCHARS.
1693 Keep the text properties that applied to the old characters
1694 (extending them to all the new chars if there are more new chars).
1696 Note that this does not yet handle markers quite right.
1698 If MARKERS is nonzero, relocate markers.
1700 Unlike most functions at this level, never call
1701 prepare_to_modify_buffer and never call signal_after_change. */
1703 void
1704 replace_range_2 (from, from_byte, to, to_byte, ins, inschars, insbytes, markers)
1705 int from, from_byte, to, to_byte;
1706 char *ins;
1707 int inschars, insbytes, markers;
1709 int nbytes_del, nchars_del;
1710 Lisp_Object temp;
1712 CHECK_MARKERS ();
1714 nchars_del = to - from;
1715 nbytes_del = to_byte - from_byte;
1717 if (nbytes_del <= 0 && insbytes == 0)
1718 return;
1720 /* Make sure point-max won't overflow after this insertion. */
1721 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1722 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1723 error ("Maximum buffer size exceeded");
1725 /* Make sure the gap is somewhere in or next to what we are deleting. */
1726 if (from > GPT)
1727 gap_right (from, from_byte);
1728 if (to < GPT)
1729 gap_left (to, to_byte, 0);
1731 GAP_SIZE += nbytes_del;
1732 ZV -= nchars_del;
1733 Z -= nchars_del;
1734 ZV_BYTE -= nbytes_del;
1735 Z_BYTE -= nbytes_del;
1736 GPT = from;
1737 GPT_BYTE = from_byte;
1738 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1740 if (GPT_BYTE < GPT)
1741 abort ();
1743 if (GPT - BEG < BEG_UNCHANGED)
1744 BEG_UNCHANGED = GPT - BEG;
1745 if (Z - GPT < END_UNCHANGED)
1746 END_UNCHANGED = Z - GPT;
1748 if (GAP_SIZE < insbytes)
1749 make_gap (insbytes - GAP_SIZE);
1751 /* Copy the replacement text into the buffer. */
1752 bcopy (ins, GPT_ADDR, insbytes);
1754 #ifdef BYTE_COMBINING_DEBUG
1755 /* We have copied text into the gap, but we have not marked
1756 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1757 here, for both the previous text and the following text.
1758 Meanwhile, GPT_ADDR does point to
1759 the text that has been stored by copy_text. */
1760 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1761 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1762 abort ();
1763 #endif
1765 GAP_SIZE -= insbytes;
1766 GPT += inschars;
1767 ZV += inschars;
1768 Z += inschars;
1769 GPT_BYTE += insbytes;
1770 ZV_BYTE += insbytes;
1771 Z_BYTE += insbytes;
1772 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1774 if (GPT_BYTE < GPT)
1775 abort ();
1777 /* Adjust the overlay center as needed. This must be done after
1778 adjusting the markers that bound the overlays. */
1779 if (nchars_del != inschars)
1781 adjust_overlays_for_insert (from, inschars);
1782 adjust_overlays_for_delete (from + inschars, nchars_del);
1785 /* Adjust markers for the deletion and the insertion. */
1786 if (markers
1787 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1788 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1789 inschars, insbytes);
1791 offset_intervals (current_buffer, from, inschars - nchars_del);
1793 /* Relocate point as if it were a marker. */
1794 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1796 if (PT < to)
1797 /* PT was within the deleted text. Move it to FROM. */
1798 adjust_point (from - PT, from_byte - PT_BYTE);
1799 else
1800 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1803 if (insbytes == 0)
1804 evaporate_overlays (from);
1806 CHECK_MARKERS ();
1808 MODIFF++;
1809 CHARS_MODIFF = MODIFF;
1812 /* Delete characters in current buffer
1813 from FROM up to (but not including) TO.
1814 If TO comes before FROM, we delete nothing. */
1816 void
1817 del_range (from, to)
1818 register int from, to;
1820 del_range_1 (from, to, 1, 0);
1823 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1824 RET_STRING says to return the deleted text. */
1826 Lisp_Object
1827 del_range_1 (from, to, prepare, ret_string)
1828 int from, to, prepare, ret_string;
1830 int from_byte, to_byte;
1831 Lisp_Object deletion;
1832 struct gcpro gcpro1;
1834 /* Make args be valid */
1835 if (from < BEGV)
1836 from = BEGV;
1837 if (to > ZV)
1838 to = ZV;
1840 if (to <= from)
1841 return Qnil;
1843 if (prepare)
1845 int range_length = to - from;
1846 prepare_to_modify_buffer (from, to, &from);
1847 to = min (ZV, from + range_length);
1850 from_byte = CHAR_TO_BYTE (from);
1851 to_byte = CHAR_TO_BYTE (to);
1853 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1854 GCPRO1(deletion);
1855 signal_after_change (from, to - from, 0);
1856 update_compositions (from, from, CHECK_HEAD);
1857 UNGCPRO;
1858 return deletion;
1861 /* Like del_range_1 but args are byte positions, not char positions. */
1863 void
1864 del_range_byte (from_byte, to_byte, prepare)
1865 int from_byte, to_byte, prepare;
1867 int from, to;
1869 /* Make args be valid */
1870 if (from_byte < BEGV_BYTE)
1871 from_byte = BEGV_BYTE;
1872 if (to_byte > ZV_BYTE)
1873 to_byte = ZV_BYTE;
1875 if (to_byte <= from_byte)
1876 return;
1878 from = BYTE_TO_CHAR (from_byte);
1879 to = BYTE_TO_CHAR (to_byte);
1881 if (prepare)
1883 int old_from = from, old_to = Z - to;
1884 int range_length = to - from;
1885 prepare_to_modify_buffer (from, to, &from);
1886 to = from + range_length;
1888 if (old_from != from)
1889 from_byte = CHAR_TO_BYTE (from);
1890 if (to > ZV)
1892 to = ZV;
1893 to_byte = ZV_BYTE;
1895 else if (old_to == Z - to)
1896 to_byte = CHAR_TO_BYTE (to);
1899 del_range_2 (from, from_byte, to, to_byte, 0);
1900 signal_after_change (from, to - from, 0);
1901 update_compositions (from, from, CHECK_HEAD);
1904 /* Like del_range_1, but positions are specified both as charpos
1905 and bytepos. */
1907 void
1908 del_range_both (from, from_byte, to, to_byte, prepare)
1909 int from, from_byte, to, to_byte, prepare;
1911 /* Make args be valid */
1912 if (from_byte < BEGV_BYTE)
1913 from_byte = BEGV_BYTE;
1914 if (to_byte > ZV_BYTE)
1915 to_byte = ZV_BYTE;
1917 if (to_byte <= from_byte)
1918 return;
1920 if (from < BEGV)
1921 from = BEGV;
1922 if (to > ZV)
1923 to = ZV;
1925 if (prepare)
1927 int old_from = from, old_to = Z - to;
1928 int range_length = to - from;
1929 prepare_to_modify_buffer (from, to, &from);
1930 to = from + range_length;
1932 if (old_from != from)
1933 from_byte = CHAR_TO_BYTE (from);
1934 if (to > ZV)
1936 to = ZV;
1937 to_byte = ZV_BYTE;
1939 else if (old_to == Z - to)
1940 to_byte = CHAR_TO_BYTE (to);
1943 del_range_2 (from, from_byte, to, to_byte, 0);
1944 signal_after_change (from, to - from, 0);
1945 update_compositions (from, from, CHECK_HEAD);
1948 /* Delete a range of text, specified both as character positions
1949 and byte positions. FROM and TO are character positions,
1950 while FROM_BYTE and TO_BYTE are byte positions.
1951 If RET_STRING is true, the deleted area is returned as a string. */
1953 Lisp_Object
1954 del_range_2 (from, from_byte, to, to_byte, ret_string)
1955 int from, from_byte, to, to_byte, ret_string;
1957 register int nbytes_del, nchars_del;
1958 Lisp_Object deletion;
1960 CHECK_MARKERS ();
1962 nchars_del = to - from;
1963 nbytes_del = to_byte - from_byte;
1965 /* Make sure the gap is somewhere in or next to what we are deleting. */
1966 if (from > GPT)
1967 gap_right (from, from_byte);
1968 if (to < GPT)
1969 gap_left (to, to_byte, 0);
1971 #ifdef BYTE_COMBINING_DEBUG
1972 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1973 Z_BYTE - to_byte, from, from_byte))
1974 abort ();
1975 #endif
1977 if (ret_string || ! EQ (current_buffer->undo_list, Qt))
1978 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1979 else
1980 deletion = Qnil;
1982 /* Relocate all markers pointing into the new, larger gap
1983 to point at the end of the text before the gap.
1984 Do this before recording the deletion,
1985 so that undo handles this after reinserting the text. */
1986 adjust_markers_for_delete (from, from_byte, to, to_byte);
1988 if (! EQ (current_buffer->undo_list, Qt))
1989 record_delete (from, deletion);
1990 MODIFF++;
1991 CHARS_MODIFF = MODIFF;
1993 /* Relocate point as if it were a marker. */
1994 if (from < PT)
1995 adjust_point (from - (PT < to ? PT : to),
1996 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1998 offset_intervals (current_buffer, from, - nchars_del);
2000 /* Adjust the overlay center as needed. This must be done after
2001 adjusting the markers that bound the overlays. */
2002 adjust_overlays_for_delete (from, nchars_del);
2004 GAP_SIZE += nbytes_del;
2005 ZV_BYTE -= nbytes_del;
2006 Z_BYTE -= nbytes_del;
2007 ZV -= nchars_del;
2008 Z -= nchars_del;
2009 GPT = from;
2010 GPT_BYTE = from_byte;
2011 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
2013 if (GPT_BYTE < GPT)
2014 abort ();
2016 if (GPT - BEG < BEG_UNCHANGED)
2017 BEG_UNCHANGED = GPT - BEG;
2018 if (Z - GPT < END_UNCHANGED)
2019 END_UNCHANGED = Z - GPT;
2021 CHECK_MARKERS ();
2023 evaporate_overlays (from);
2025 return deletion;
2028 /* Call this if you're about to change the region of BUFFER from
2029 character positions START to END. This checks the read-only
2030 properties of the region, calls the necessary modification hooks,
2031 and warns the next redisplay that it should pay attention to that
2032 area.
2034 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
2035 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
2037 void
2038 modify_region (buffer, start, end, preserve_chars_modiff)
2039 struct buffer *buffer;
2040 int start, end, preserve_chars_modiff;
2042 struct buffer *old_buffer = current_buffer;
2044 if (buffer != old_buffer)
2045 set_buffer_internal (buffer);
2047 prepare_to_modify_buffer (start, end, NULL);
2049 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
2051 if (MODIFF <= SAVE_MODIFF)
2052 record_first_change ();
2053 MODIFF++;
2054 if (! preserve_chars_modiff)
2055 CHARS_MODIFF = MODIFF;
2057 buffer->point_before_scroll = Qnil;
2059 if (buffer != old_buffer)
2060 set_buffer_internal (old_buffer);
2063 /* Check that it is okay to modify the buffer between START and END,
2064 which are char positions.
2066 Run the before-change-function, if any. If intervals are in use,
2067 verify that the text to be modified is not read-only, and call
2068 any modification properties the text may have.
2070 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2071 by holding its value temporarily in a marker. */
2073 void
2074 prepare_to_modify_buffer (start, end, preserve_ptr)
2075 int start, end;
2076 int *preserve_ptr;
2078 struct buffer *base_buffer;
2080 if (!NILP (current_buffer->read_only))
2081 Fbarf_if_buffer_read_only ();
2083 /* Let redisplay consider other windows than selected_window
2084 if modifying another buffer. */
2085 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2086 ++windows_or_buffers_changed;
2088 if (BUF_INTERVALS (current_buffer) != 0)
2090 if (preserve_ptr)
2092 Lisp_Object preserve_marker;
2093 struct gcpro gcpro1;
2094 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
2095 GCPRO1 (preserve_marker);
2096 verify_interval_modification (current_buffer, start, end);
2097 *preserve_ptr = marker_position (preserve_marker);
2098 unchain_marker (XMARKER (preserve_marker));
2099 UNGCPRO;
2101 else
2102 verify_interval_modification (current_buffer, start, end);
2105 /* For indirect buffers, use the base buffer to check clashes. */
2106 if (current_buffer->base_buffer != 0)
2107 base_buffer = current_buffer->base_buffer;
2108 else
2109 base_buffer = current_buffer;
2111 #ifdef CLASH_DETECTION
2112 if (!NILP (base_buffer->file_truename)
2113 /* Make binding buffer-file-name to nil effective. */
2114 && !NILP (base_buffer->filename)
2115 && SAVE_MODIFF >= MODIFF)
2116 lock_file (base_buffer->file_truename);
2117 #else
2118 /* At least warn if this file has changed on disk since it was visited. */
2119 if (!NILP (base_buffer->filename)
2120 && SAVE_MODIFF >= MODIFF
2121 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2122 && !NILP (Ffile_exists_p (base_buffer->filename)))
2123 call1 (intern ("ask-user-about-supersession-threat"),
2124 base_buffer->filename);
2125 #endif /* not CLASH_DETECTION */
2127 signal_before_change (start, end, preserve_ptr);
2129 if (current_buffer->newline_cache)
2130 invalidate_region_cache (current_buffer,
2131 current_buffer->newline_cache,
2132 start - BEG, Z - end);
2133 if (current_buffer->width_run_cache)
2134 invalidate_region_cache (current_buffer,
2135 current_buffer->width_run_cache,
2136 start - BEG, Z - end);
2138 Vdeactivate_mark = Qt;
2141 /* These macros work with an argument named `preserve_ptr'
2142 and a local variable named `preserve_marker'. */
2144 #define PRESERVE_VALUE \
2145 if (preserve_ptr && NILP (preserve_marker)) \
2146 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2148 #define RESTORE_VALUE \
2149 if (! NILP (preserve_marker)) \
2151 *preserve_ptr = marker_position (preserve_marker); \
2152 unchain_marker (XMARKER (preserve_marker)); \
2155 #define PRESERVE_START_END \
2156 if (NILP (start_marker)) \
2157 start_marker = Fcopy_marker (start, Qnil); \
2158 if (NILP (end_marker)) \
2159 end_marker = Fcopy_marker (end, Qnil);
2161 #define FETCH_START \
2162 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2164 #define FETCH_END \
2165 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2167 /* Set a variable to nil if an error occurred.
2168 Don't change the variable if there was no error.
2169 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2170 VARIABLE is the variable to maybe set to nil.
2171 NO-ERROR-FLAG is nil if there was an error,
2172 anything else meaning no error (so this function does nothing). */
2173 Lisp_Object
2174 reset_var_on_error (val)
2175 Lisp_Object val;
2177 if (NILP (XCDR (val)))
2178 Fset (XCAR (val), Qnil);
2179 return Qnil;
2182 /* Signal a change to the buffer immediately before it happens.
2183 START_INT and END_INT are the bounds of the text to be changed.
2185 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2186 by holding its value temporarily in a marker. */
2188 void
2189 signal_before_change (start_int, end_int, preserve_ptr)
2190 int start_int, end_int;
2191 int *preserve_ptr;
2193 Lisp_Object start, end;
2194 Lisp_Object start_marker, end_marker;
2195 Lisp_Object preserve_marker;
2196 struct gcpro gcpro1, gcpro2, gcpro3;
2197 int count = SPECPDL_INDEX ();
2199 if (inhibit_modification_hooks)
2200 return;
2202 start = make_number (start_int);
2203 end = make_number (end_int);
2204 preserve_marker = Qnil;
2205 start_marker = Qnil;
2206 end_marker = Qnil;
2207 GCPRO3 (preserve_marker, start_marker, end_marker);
2209 specbind (Qinhibit_modification_hooks, Qt);
2211 /* If buffer is unmodified, run a special hook for that case. */
2212 if (SAVE_MODIFF >= MODIFF
2213 && !NILP (Vfirst_change_hook)
2214 && !NILP (Vrun_hooks))
2216 PRESERVE_VALUE;
2217 PRESERVE_START_END;
2218 call1 (Vrun_hooks, Qfirst_change_hook);
2221 /* Now run the before-change-functions if any. */
2222 if (!NILP (Vbefore_change_functions))
2224 Lisp_Object args[3];
2225 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
2227 PRESERVE_VALUE;
2228 PRESERVE_START_END;
2230 /* Mark before-change-functions to be reset to nil in case of error. */
2231 record_unwind_protect (reset_var_on_error, rvoe_arg);
2233 /* Actually run the hook functions. */
2234 args[0] = Qbefore_change_functions;
2235 args[1] = FETCH_START;
2236 args[2] = FETCH_END;
2237 Frun_hook_with_args (3, args);
2239 /* There was no error: unarm the reset_on_error. */
2240 XSETCDR (rvoe_arg, Qt);
2243 if (current_buffer->overlays_before || current_buffer->overlays_after)
2245 PRESERVE_VALUE;
2246 report_overlay_modification (FETCH_START, FETCH_END, 0,
2247 FETCH_START, FETCH_END, Qnil);
2250 if (! NILP (start_marker))
2251 free_marker (start_marker);
2252 if (! NILP (end_marker))
2253 free_marker (end_marker);
2254 RESTORE_VALUE;
2255 UNGCPRO;
2257 unbind_to (count, Qnil);
2260 /* Signal a change immediately after it happens.
2261 CHARPOS is the character position of the start of the changed text.
2262 LENDEL is the number of characters of the text before the change.
2263 (Not the whole buffer; just the part that was changed.)
2264 LENINS is the number of characters in that part of the text
2265 after the change. */
2267 void
2268 signal_after_change (charpos, lendel, lenins)
2269 int charpos, lendel, lenins;
2271 int count = SPECPDL_INDEX ();
2272 if (inhibit_modification_hooks)
2273 return;
2275 /* If we are deferring calls to the after-change functions
2276 and there are no before-change functions,
2277 just record the args that we were going to use. */
2278 if (! NILP (Vcombine_after_change_calls)
2279 && NILP (Vbefore_change_functions)
2280 && !current_buffer->overlays_before
2281 && !current_buffer->overlays_after)
2283 Lisp_Object elt;
2285 if (!NILP (combine_after_change_list)
2286 && current_buffer != XBUFFER (combine_after_change_buffer))
2287 Fcombine_after_change_execute ();
2289 elt = Fcons (make_number (charpos - BEG),
2290 Fcons (make_number (Z - (charpos - lendel + lenins)),
2291 Fcons (make_number (lenins - lendel), Qnil)));
2292 combine_after_change_list
2293 = Fcons (elt, combine_after_change_list);
2294 combine_after_change_buffer = Fcurrent_buffer ();
2296 return;
2299 if (!NILP (combine_after_change_list))
2300 Fcombine_after_change_execute ();
2302 specbind (Qinhibit_modification_hooks, Qt);
2304 if (!NILP (Vafter_change_functions))
2306 Lisp_Object args[4];
2307 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2309 /* Mark after-change-functions to be reset to nil in case of error. */
2310 record_unwind_protect (reset_var_on_error, rvoe_arg);
2312 /* Actually run the hook functions. */
2313 args[0] = Qafter_change_functions;
2314 XSETFASTINT (args[1], charpos);
2315 XSETFASTINT (args[2], charpos + lenins);
2316 XSETFASTINT (args[3], lendel);
2317 Frun_hook_with_args (4, args);
2319 /* There was no error: unarm the reset_on_error. */
2320 XSETCDR (rvoe_arg, Qt);
2323 if (current_buffer->overlays_before || current_buffer->overlays_after)
2324 report_overlay_modification (make_number (charpos),
2325 make_number (charpos + lenins),
2327 make_number (charpos),
2328 make_number (charpos + lenins),
2329 make_number (lendel));
2331 /* After an insertion, call the text properties
2332 insert-behind-hooks or insert-in-front-hooks. */
2333 if (lendel == 0)
2334 report_interval_modification (make_number (charpos),
2335 make_number (charpos + lenins));
2337 unbind_to (count, Qnil);
2340 Lisp_Object
2341 Fcombine_after_change_execute_1 (val)
2342 Lisp_Object val;
2344 Vcombine_after_change_calls = val;
2345 return val;
2348 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2349 Scombine_after_change_execute, 0, 0, 0,
2350 doc: /* This function is for use internally in `combine-after-change-calls'. */)
2353 int count = SPECPDL_INDEX ();
2354 int beg, end, change;
2355 int begpos, endpos;
2356 Lisp_Object tail;
2358 if (NILP (combine_after_change_list))
2359 return Qnil;
2361 /* It is rare for combine_after_change_buffer to be invalid, but
2362 possible. It can happen when combine-after-change-calls is
2363 non-nil, and insertion calls a file handler (e.g. through
2364 lock_file) which scribbles into a temp file -- cyd */
2365 if (!BUFFERP (combine_after_change_buffer)
2366 || NILP (XBUFFER (combine_after_change_buffer)->name))
2368 combine_after_change_list = Qnil;
2369 return Qnil;
2372 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2374 Fset_buffer (combine_after_change_buffer);
2376 /* # chars unchanged at beginning of buffer. */
2377 beg = Z - BEG;
2378 /* # chars unchanged at end of buffer. */
2379 end = beg;
2380 /* Total amount of insertion (negative for deletion). */
2381 change = 0;
2383 /* Scan the various individual changes,
2384 accumulating the range info in BEG, END and CHANGE. */
2385 for (tail = combine_after_change_list; CONSP (tail);
2386 tail = XCDR (tail))
2388 Lisp_Object elt;
2389 int thisbeg, thisend, thischange;
2391 /* Extract the info from the next element. */
2392 elt = XCAR (tail);
2393 if (! CONSP (elt))
2394 continue;
2395 thisbeg = XINT (XCAR (elt));
2397 elt = XCDR (elt);
2398 if (! CONSP (elt))
2399 continue;
2400 thisend = XINT (XCAR (elt));
2402 elt = XCDR (elt);
2403 if (! CONSP (elt))
2404 continue;
2405 thischange = XINT (XCAR (elt));
2407 /* Merge this range into the accumulated range. */
2408 change += thischange;
2409 if (thisbeg < beg)
2410 beg = thisbeg;
2411 if (thisend < end)
2412 end = thisend;
2415 /* Get the current start and end positions of the range
2416 that was changed. */
2417 begpos = BEG + beg;
2418 endpos = Z - end;
2420 /* We are about to handle these, so discard them. */
2421 combine_after_change_list = Qnil;
2423 /* Now run the after-change functions for real.
2424 Turn off the flag that defers them. */
2425 record_unwind_protect (Fcombine_after_change_execute_1,
2426 Vcombine_after_change_calls);
2427 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2428 update_compositions (begpos, endpos, CHECK_ALL);
2430 return unbind_to (count, Qnil);
2433 void
2434 syms_of_insdel ()
2436 staticpro (&combine_after_change_list);
2437 staticpro (&combine_after_change_buffer);
2438 combine_after_change_list = Qnil;
2439 combine_after_change_buffer = Qnil;
2441 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2442 doc: /* Non-nil means enable debugging checks for invalid marker positions. */);
2443 check_markers_debug_flag = 0;
2444 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2445 doc: /* Used internally by the `combine-after-change-calls' macro. */);
2446 Vcombine_after_change_calls = Qnil;
2448 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2449 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2450 This affects `before-change-functions' and `after-change-functions',
2451 as well as hooks attached to text properties and overlays. */);
2452 inhibit_modification_hooks = 0;
2453 Qinhibit_modification_hooks = intern ("inhibit-modification-hooks");
2454 staticpro (&Qinhibit_modification_hooks);
2456 defsubr (&Scombine_after_change_execute);
2459 /* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
2460 (do not change this comment) */