(display-completion-list): Re-state the obsolescence of `common-substring'.
[emacs.git] / src / insdel.c
bloba41f03b1ba474379ac316856f6be2a704b35aa20
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 of the License, or
11 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "intervals.h"
25 #include "buffer.h"
26 #include "character.h"
27 #include "window.h"
28 #include "blockinput.h"
29 #include "region-cache.h"
31 #ifndef NULL
32 #define NULL 0
33 #endif
35 static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
36 static void insert_from_buffer_1 ();
37 static void gap_left P_ ((int, int, int));
38 static void gap_right P_ ((int, int));
39 static void adjust_markers_gap_motion P_ ((int, int, int));
40 static void adjust_markers_for_insert P_ ((int, int, int, int, int));
41 void adjust_markers_for_delete P_ ((int, int, int, int));
42 static void adjust_markers_for_replace P_ ((int, int, int, int, int, int));
43 static void adjust_point P_ ((int, int));
45 Lisp_Object Fcombine_after_change_execute ();
47 /* Non-nil means don't call the after-change-functions right away,
48 just record an element in Vcombine_after_change_calls_list. */
49 Lisp_Object Vcombine_after_change_calls;
51 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
52 describing changes which happened while combine_after_change_calls
53 was nonzero. We use this to decide how to call them
54 once the deferral ends.
56 In each element.
57 BEG-UNCHANGED is the number of chars before the changed range.
58 END-UNCHANGED is the number of chars after the changed range,
59 and CHANGE-AMOUNT is the number of characters inserted by the change
60 (negative for a deletion). */
61 Lisp_Object combine_after_change_list;
63 /* Buffer which combine_after_change_list is about. */
64 Lisp_Object combine_after_change_buffer;
66 Lisp_Object Qinhibit_modification_hooks;
69 /* Check all markers in the current buffer, looking for something invalid. */
71 static int check_markers_debug_flag;
73 #define CHECK_MARKERS() \
74 if (check_markers_debug_flag) \
75 check_markers (); \
76 else
78 void
79 check_markers ()
81 register struct Lisp_Marker *tail;
82 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
84 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
86 if (tail->buffer->text != current_buffer->text)
87 abort ();
88 if (tail->charpos > Z)
89 abort ();
90 if (tail->bytepos > Z_BYTE)
91 abort ();
92 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
93 abort ();
97 /* Move gap to position CHARPOS.
98 Note that this can quit! */
100 void
101 move_gap (charpos)
102 int charpos;
104 move_gap_both (charpos, charpos_to_bytepos (charpos));
107 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
108 Note that this can quit! */
110 void
111 move_gap_both (charpos, bytepos)
112 int charpos, bytepos;
114 if (bytepos < GPT_BYTE)
115 gap_left (charpos, bytepos, 0);
116 else if (bytepos > GPT_BYTE)
117 gap_right (charpos, bytepos);
120 /* Move the gap to a position less than the current GPT.
121 BYTEPOS describes the new position as a byte position,
122 and CHARPOS is the corresponding char position.
123 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
125 static void
126 gap_left (charpos, bytepos, newgap)
127 register int charpos, bytepos;
128 int newgap;
130 register unsigned char *to, *from;
131 register int i;
132 int new_s1;
134 if (!newgap)
135 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
137 i = GPT_BYTE;
138 to = GAP_END_ADDR;
139 from = GPT_ADDR;
140 new_s1 = GPT_BYTE;
142 /* Now copy the characters. To move the gap down,
143 copy characters up. */
145 while (1)
147 /* I gets number of characters left to copy. */
148 i = new_s1 - bytepos;
149 if (i == 0)
150 break;
151 /* If a quit is requested, stop copying now.
152 Change BYTEPOS to be where we have actually moved the gap to. */
153 if (QUITP)
155 bytepos = new_s1;
156 charpos = BYTE_TO_CHAR (bytepos);
157 break;
159 /* Move at most 32000 chars before checking again for a quit. */
160 if (i > 32000)
161 i = 32000;
162 #ifdef GAP_USE_BCOPY
163 if (i >= 128
164 /* bcopy is safe if the two areas of memory do not overlap
165 or on systems where bcopy is always safe for moving upward. */
166 && (BCOPY_UPWARD_SAFE
167 || to - from >= 128))
169 /* If overlap is not safe, avoid it by not moving too many
170 characters at once. */
171 if (!BCOPY_UPWARD_SAFE && i > to - from)
172 i = to - from;
173 new_s1 -= i;
174 from -= i, to -= i;
175 bcopy (from, to, i);
177 else
178 #endif
180 new_s1 -= i;
181 while (--i >= 0)
182 *--to = *--from;
186 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
187 BYTEPOS is where the loop above stopped, which may be what was specified
188 or may be where a quit was detected. */
189 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
190 GPT_BYTE = bytepos;
191 GPT = charpos;
192 if (bytepos < charpos)
193 abort ();
194 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
195 QUIT;
198 /* Move the gap to a position greater than than the current GPT.
199 BYTEPOS describes the new position as a byte position,
200 and CHARPOS is the corresponding char position. */
202 static void
203 gap_right (charpos, bytepos)
204 register int charpos, bytepos;
206 register unsigned char *to, *from;
207 register int i;
208 int new_s1;
210 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
212 i = GPT_BYTE;
213 from = GAP_END_ADDR;
214 to = GPT_ADDR;
215 new_s1 = GPT_BYTE;
217 /* Now copy the characters. To move the gap up,
218 copy characters down. */
220 while (1)
222 /* I gets number of characters left to copy. */
223 i = bytepos - new_s1;
224 if (i == 0)
225 break;
226 /* If a quit is requested, stop copying now.
227 Change BYTEPOS to be where we have actually moved the gap to. */
228 if (QUITP)
230 bytepos = new_s1;
231 charpos = BYTE_TO_CHAR (bytepos);
232 break;
234 /* Move at most 32000 chars before checking again for a quit. */
235 if (i > 32000)
236 i = 32000;
237 #ifdef GAP_USE_BCOPY
238 if (i >= 128
239 /* bcopy is safe if the two areas of memory do not overlap
240 or on systems where bcopy is always safe for moving downward. */
241 && (BCOPY_DOWNWARD_SAFE
242 || from - to >= 128))
244 /* If overlap is not safe, avoid it by not moving too many
245 characters at once. */
246 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
247 i = from - to;
248 new_s1 += i;
249 bcopy (from, to, i);
250 from += i, to += i;
252 else
253 #endif
255 new_s1 += i;
256 while (--i >= 0)
257 *to++ = *from++;
261 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
262 - GAP_SIZE);
263 GPT = charpos;
264 GPT_BYTE = bytepos;
265 if (bytepos < charpos)
266 abort ();
267 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
268 QUIT;
271 /* Add AMOUNT to the byte position of every marker in the current buffer
272 whose current byte position is between FROM (exclusive) and TO (inclusive).
274 Also, any markers past the outside of that interval, in the direction
275 of adjustment, are first moved back to the near end of the interval
276 and then adjusted by AMOUNT.
278 When the latter adjustment is done, if AMOUNT is negative,
279 we record the adjustment for undo. (This case happens only for
280 deletion.)
282 The markers' character positions are not altered,
283 because gap motion does not affect character positions. */
285 int adjust_markers_test;
287 static void
288 adjust_markers_gap_motion (from, to, amount)
289 register int from, to, amount;
291 /* Now that a marker has a bytepos, not counting the gap,
292 nothing needs to be done here. */
293 #if 0
294 Lisp_Object marker;
295 register struct Lisp_Marker *m;
296 register int mpos;
298 marker = BUF_MARKERS (current_buffer);
300 while (!NILP (marker))
302 m = XMARKER (marker);
303 mpos = m->bytepos;
304 if (amount > 0)
306 if (mpos > to && mpos < to + amount)
308 if (adjust_markers_test)
309 abort ();
310 mpos = to + amount;
313 else
315 /* Here's the case where a marker is inside text being deleted.
316 AMOUNT can be negative for gap motion, too,
317 but then this range contains no markers. */
318 if (mpos > from + amount && mpos <= from)
320 if (adjust_markers_test)
321 abort ();
322 mpos = from + amount;
325 if (mpos > from && mpos <= to)
326 mpos += amount;
327 m->bufpos = mpos;
328 marker = m->chain;
330 #endif
333 /* Adjust all markers for a deletion
334 whose range in bytes is FROM_BYTE to TO_BYTE.
335 The range in charpos is FROM to TO.
337 This function assumes that the gap is adjacent to
338 or inside of the range being deleted. */
340 void
341 adjust_markers_for_delete (from, from_byte, to, to_byte)
342 register int from, from_byte, to, to_byte;
344 Lisp_Object marker;
345 register struct Lisp_Marker *m;
346 register int charpos;
348 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
350 charpos = m->charpos;
352 if (charpos > Z)
353 abort ();
355 /* If the marker is after the deletion,
356 relocate by number of chars / bytes deleted. */
357 if (charpos > to)
359 m->charpos -= to - from;
360 m->bytepos -= to_byte - from_byte;
362 /* Here's the case where a marker is inside text being deleted. */
363 else if (charpos > from)
365 if (! m->insertion_type)
366 { /* Normal markers will end up at the beginning of the
367 re-inserted text after undoing a deletion, and must be
368 adjusted to move them to the correct place. */
369 XSETMISC (marker, m);
370 record_marker_adjustment (marker, from - charpos);
372 else if (charpos < to)
373 { /* Before-insertion markers will automatically move forward
374 upon re-inserting the deleted text, so we have to arrange
375 for them to move backward to the correct position. */
376 XSETMISC (marker, m);
377 record_marker_adjustment (marker, charpos - to);
379 m->charpos = from;
380 m->bytepos = from_byte;
382 /* Here's the case where a before-insertion marker is immediately
383 before the deleted region. */
384 else if (charpos == from && m->insertion_type)
386 /* Undoing the change uses normal insertion, which will
387 incorrectly make MARKER move forward, so we arrange for it
388 to then move backward to the correct place at the beginning
389 of the deleted region. */
390 XSETMISC (marker, m);
391 record_marker_adjustment (marker, to - from);
397 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
398 to TO / TO_BYTE. We have to relocate the charpos of every marker
399 that points after the insertion (but not their bytepos).
401 When a marker points at the insertion point,
402 we advance it if either its insertion-type is t
403 or BEFORE_MARKERS is true. */
405 static void
406 adjust_markers_for_insert (from, from_byte, to, to_byte, before_markers)
407 register int from, from_byte, to, to_byte;
408 int before_markers;
410 struct Lisp_Marker *m;
411 int adjusted = 0;
412 int nchars = to - from;
413 int nbytes = to_byte - from_byte;
415 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
417 eassert (m->bytepos >= m->charpos
418 && m->bytepos - m->charpos <= Z_BYTE - Z);
420 if (m->bytepos == from_byte)
422 if (m->insertion_type || before_markers)
424 m->bytepos = to_byte;
425 m->charpos = to;
426 if (m->insertion_type)
427 adjusted = 1;
430 else if (m->bytepos > from_byte)
432 m->bytepos += nbytes;
433 m->charpos += nchars;
437 /* Adjusting only markers whose insertion-type is t may result in
438 - disordered start and end in overlays, and
439 - disordered overlays in the slot `overlays_before' of current_buffer. */
440 if (adjusted)
442 fix_start_end_in_overlays(from, to);
443 fix_overlays_before (current_buffer, from, to);
447 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
449 This is used only when the value of point changes due to an insert
450 or delete; it does not represent a conceptual change in point as a
451 marker. In particular, point is not crossing any interval
452 boundaries, so there's no need to use the usual SET_PT macro. In
453 fact it would be incorrect to do so, because either the old or the
454 new value of point is out of sync with the current set of
455 intervals. */
457 static void
458 adjust_point (nchars, nbytes)
459 int nchars, nbytes;
461 BUF_PT (current_buffer) += nchars;
462 BUF_PT_BYTE (current_buffer) += nbytes;
464 /* In a single-byte buffer, the two positions must be equal. */
465 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
468 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
469 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
470 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
471 an insertion. */
473 static void
474 adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
475 new_chars, new_bytes)
476 int from, from_byte, old_chars, old_bytes, new_chars, new_bytes;
478 register struct Lisp_Marker *m;
479 int prev_to_byte = from_byte + old_bytes;
480 int diff_chars = new_chars - old_chars;
481 int diff_bytes = new_bytes - old_bytes;
483 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
485 if (m->bytepos >= prev_to_byte)
487 m->charpos += diff_chars;
488 m->bytepos += diff_bytes;
490 else if (m->bytepos > from_byte)
492 m->charpos = from;
493 m->bytepos = from_byte;
497 CHECK_MARKERS ();
501 /* Make the gap NBYTES_ADDED bytes longer. */
503 void
504 make_gap_larger (nbytes_added)
505 int nbytes_added;
507 Lisp_Object tem;
508 int real_gap_loc;
509 int real_gap_loc_byte;
510 int old_gap_size;
512 /* If we have to get more space, get enough to last a while. */
513 nbytes_added += 2000;
515 /* Don't allow a buffer size that won't fit in an int
516 even if it will fit in a Lisp integer.
517 That won't work because so many places use `int'.
519 Make sure we don't introduce overflows in the calculation. */
521 if (Z_BYTE - BEG_BYTE + GAP_SIZE
522 >= (((EMACS_INT) 1 << (min (VALBITS, BITS_PER_INT) - 1)) - 1
523 - nbytes_added))
524 error ("Buffer exceeds maximum size");
526 enlarge_buffer_text (current_buffer, nbytes_added);
528 /* Prevent quitting in move_gap. */
529 tem = Vinhibit_quit;
530 Vinhibit_quit = Qt;
532 real_gap_loc = GPT;
533 real_gap_loc_byte = GPT_BYTE;
534 old_gap_size = GAP_SIZE;
536 /* Call the newly allocated space a gap at the end of the whole space. */
537 GPT = Z + GAP_SIZE;
538 GPT_BYTE = Z_BYTE + GAP_SIZE;
539 GAP_SIZE = nbytes_added;
541 /* Move the new gap down to be consecutive with the end of the old one.
542 This adjusts the markers properly too. */
543 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
545 /* Now combine the two into one large gap. */
546 GAP_SIZE += old_gap_size;
547 GPT = real_gap_loc;
548 GPT_BYTE = real_gap_loc_byte;
550 /* Put an anchor. */
551 *(Z_ADDR) = 0;
553 Vinhibit_quit = tem;
557 /* Make the gap NBYTES_REMOVED bytes shorter. */
559 void
560 make_gap_smaller (nbytes_removed)
561 int nbytes_removed;
563 Lisp_Object tem;
564 int real_gap_loc;
565 int real_gap_loc_byte;
566 int real_Z;
567 int real_Z_byte;
568 int real_beg_unchanged;
569 int new_gap_size;
571 /* Make sure the gap is at least 20 bytes. */
572 if (GAP_SIZE - nbytes_removed < 20)
573 nbytes_removed = GAP_SIZE - 20;
575 /* Prevent quitting in move_gap. */
576 tem = Vinhibit_quit;
577 Vinhibit_quit = Qt;
579 real_gap_loc = GPT;
580 real_gap_loc_byte = GPT_BYTE;
581 new_gap_size = GAP_SIZE - nbytes_removed;
582 real_Z = Z;
583 real_Z_byte = Z_BYTE;
584 real_beg_unchanged = BEG_UNCHANGED;
586 /* Pretend that the last unwanted part of the gap is the entire gap,
587 and that the first desired part of the gap is part of the buffer
588 text. */
589 bzero (GPT_ADDR, new_gap_size);
590 GPT += new_gap_size;
591 GPT_BYTE += new_gap_size;
592 Z += new_gap_size;
593 Z_BYTE += new_gap_size;
594 GAP_SIZE = nbytes_removed;
596 /* Move the unwanted pretend gap to the end of the buffer. This
597 adjusts the markers properly too. */
598 gap_right (Z, Z_BYTE);
600 enlarge_buffer_text (current_buffer, -nbytes_removed);
602 /* Now restore the desired gap. */
603 GAP_SIZE = new_gap_size;
604 GPT = real_gap_loc;
605 GPT_BYTE = real_gap_loc_byte;
606 Z = real_Z;
607 Z_BYTE = real_Z_byte;
608 BEG_UNCHANGED = real_beg_unchanged;
610 /* Put an anchor. */
611 *(Z_ADDR) = 0;
613 Vinhibit_quit = tem;
616 void
617 make_gap (nbytes_added)
618 int nbytes_added;
620 if (nbytes_added >= 0)
621 make_gap_larger (nbytes_added);
622 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
623 else
624 make_gap_smaller (-nbytes_added);
625 #endif
628 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
629 FROM_MULTIBYTE says whether the incoming text is multibyte.
630 TO_MULTIBYTE says whether to store the text as multibyte.
631 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
633 Return the number of bytes stored at TO_ADDR. */
636 copy_text (from_addr, to_addr, nbytes,
637 from_multibyte, to_multibyte)
638 const unsigned char *from_addr;
639 unsigned char *to_addr;
640 int nbytes;
641 int from_multibyte, to_multibyte;
643 if (from_multibyte == to_multibyte)
645 bcopy (from_addr, to_addr, nbytes);
646 return nbytes;
648 else if (from_multibyte)
650 int nchars = 0;
651 int bytes_left = nbytes;
652 Lisp_Object tbl = Qnil;
654 while (bytes_left > 0)
656 int thislen, c;
657 c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
658 if (!ASCII_CHAR_P (c))
659 c = multibyte_char_to_unibyte (c, tbl);
660 *to_addr++ = c;
661 from_addr += thislen;
662 bytes_left -= thislen;
663 nchars++;
665 return nchars;
667 else
669 unsigned char *initial_to_addr = to_addr;
671 /* Convert single-byte to multibyte. */
672 while (nbytes > 0)
674 int c = *from_addr++;
676 if (c >= 0200)
678 c = unibyte_char_to_multibyte (c);
679 to_addr += CHAR_STRING (c, to_addr);
680 nbytes--;
682 else
683 /* Special case for speed. */
684 *to_addr++ = c, nbytes--;
686 return to_addr - initial_to_addr;
690 /* Return the number of bytes it would take
691 to convert some single-byte text to multibyte.
692 The single-byte text consists of NBYTES bytes at PTR. */
695 count_size_as_multibyte (ptr, nbytes)
696 const unsigned char *ptr;
697 int nbytes;
699 int i;
700 int outgoing_nbytes = 0;
702 for (i = 0; i < nbytes; i++)
704 unsigned int c = *ptr++;
706 if (c < 0200)
707 outgoing_nbytes++;
708 else
710 c = unibyte_char_to_multibyte (c);
711 outgoing_nbytes += CHAR_BYTES (c);
715 return outgoing_nbytes;
718 /* Insert a string of specified length before point.
719 This function judges multibyteness based on
720 enable_multibyte_characters in the current buffer;
721 it never converts between single-byte and multibyte.
723 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
724 prepare_to_modify_buffer could relocate the text. */
726 void
727 insert (string, nbytes)
728 register const unsigned char *string;
729 register int nbytes;
731 if (nbytes > 0)
733 int len = chars_in_text (string, nbytes), opoint;
734 insert_1_both (string, len, nbytes, 0, 1, 0);
735 opoint = PT - len;
736 signal_after_change (opoint, 0, len);
737 update_compositions (opoint, PT, CHECK_BORDER);
741 /* Likewise, but inherit text properties from neighboring characters. */
743 void
744 insert_and_inherit (string, nbytes)
745 register const unsigned char *string;
746 register int nbytes;
748 if (nbytes > 0)
750 int len = chars_in_text (string, nbytes), opoint;
751 insert_1_both (string, len, nbytes, 1, 1, 0);
752 opoint = PT - len;
753 signal_after_change (opoint, 0, len);
754 update_compositions (opoint, PT, CHECK_BORDER);
758 /* Insert the character C before point. Do not inherit text properties. */
760 void
761 insert_char (c)
762 int c;
764 unsigned char str[MAX_MULTIBYTE_LENGTH];
765 int len;
767 if (! NILP (current_buffer->enable_multibyte_characters))
768 len = CHAR_STRING (c, str);
769 else
771 len = 1;
772 str[0] = c;
775 insert (str, len);
778 /* Insert the null-terminated string S before point. */
780 void
781 insert_string (s)
782 const char *s;
784 insert (s, strlen (s));
787 /* Like `insert' except that all markers pointing at the place where
788 the insertion happens are adjusted to point after it.
789 Don't use this function to insert part of a Lisp string,
790 since gc could happen and relocate it. */
792 void
793 insert_before_markers (string, nbytes)
794 const unsigned char *string;
795 register int nbytes;
797 if (nbytes > 0)
799 int len = chars_in_text (string, nbytes), opoint;
800 insert_1_both (string, len, nbytes, 0, 1, 1);
801 opoint = PT - len;
802 signal_after_change (opoint, 0, len);
803 update_compositions (opoint, PT, CHECK_BORDER);
807 /* Likewise, but inherit text properties from neighboring characters. */
809 void
810 insert_before_markers_and_inherit (string, nbytes)
811 const unsigned char *string;
812 register int nbytes;
814 if (nbytes > 0)
816 int len = chars_in_text (string, nbytes), opoint;
817 insert_1_both (string, len, nbytes, 1, 1, 1);
818 opoint = PT - len;
819 signal_after_change (opoint, 0, len);
820 update_compositions (opoint, PT, CHECK_BORDER);
824 /* Subroutine used by the insert functions above. */
826 void
827 insert_1 (string, nbytes, inherit, prepare, before_markers)
828 register const unsigned char *string;
829 register int nbytes;
830 int inherit, prepare, before_markers;
832 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
833 inherit, prepare, before_markers);
837 #ifdef BYTE_COMBINING_DEBUG
839 /* See if the bytes before POS/POS_BYTE combine with bytes
840 at the start of STRING to form a single character.
841 If so, return the number of bytes at the start of STRING
842 which combine in this way. Otherwise, return 0. */
845 count_combining_before (string, length, pos, pos_byte)
846 const unsigned char *string;
847 int length;
848 int pos, pos_byte;
850 int len, combining_bytes;
851 const unsigned char *p;
853 if (NILP (current_buffer->enable_multibyte_characters))
854 return 0;
856 /* At first, we can exclude the following cases:
857 (1) STRING[0] can't be a following byte of multibyte sequence.
858 (2) POS is the start of the current buffer.
859 (3) A character before POS is not a multibyte character. */
860 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
861 return 0;
862 if (pos_byte == BEG_BYTE) /* case (2) */
863 return 0;
864 len = 1;
865 p = BYTE_POS_ADDR (pos_byte - 1);
866 while (! CHAR_HEAD_P (*p)) p--, len++;
867 if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
868 return 0;
870 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
871 if (combining_bytes <= 0)
872 /* The character preceding POS is, complete and no room for
873 combining bytes (combining_bytes == 0), or an independent 8-bit
874 character (combining_bytes < 0). */
875 return 0;
877 /* We have a combination situation. Count the bytes at STRING that
878 may combine. */
879 p = string + 1;
880 while (!CHAR_HEAD_P (*p) && p < string + length)
881 p++;
883 return (combining_bytes < p - string ? combining_bytes : p - string);
886 /* See if the bytes after POS/POS_BYTE combine with bytes
887 at the end of STRING to form a single character.
888 If so, return the number of bytes after POS/POS_BYTE
889 which combine in this way. Otherwise, return 0. */
892 count_combining_after (string, length, pos, pos_byte)
893 const unsigned char *string;
894 int length;
895 int pos, pos_byte;
897 int opos_byte = pos_byte;
898 int i;
899 int bytes;
900 unsigned char *bufp;
902 if (NILP (current_buffer->enable_multibyte_characters))
903 return 0;
905 /* At first, we can exclude the following cases:
906 (1) The last byte of STRING is an ASCII.
907 (2) POS is the last of the current buffer.
908 (3) A character at POS can't be a following byte of multibyte
909 character. */
910 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
911 return 0;
912 if (pos_byte == Z_BYTE) /* case (2) */
913 return 0;
914 bufp = BYTE_POS_ADDR (pos_byte);
915 if (CHAR_HEAD_P (*bufp)) /* case (3) */
916 return 0;
918 i = length - 1;
919 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
921 i--;
923 if (i < 0)
925 /* All characters in STRING are not character head. We must
926 check also preceding bytes at POS. We are sure that the gap
927 is at POS. */
928 unsigned char *p = BEG_ADDR;
929 i = pos_byte - 2;
930 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
931 i--;
932 if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
933 return 0;
935 bytes = BYTES_BY_CHAR_HEAD (p[i]);
936 return (bytes <= pos_byte - 1 - i + length
938 : bytes - (pos_byte - 1 - i + length));
940 if (!BASE_LEADING_CODE_P (string[i]))
941 return 0;
943 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
944 bufp++, pos_byte++;
945 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
947 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
950 #endif
953 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
954 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
955 are the same as in insert_1. */
957 void
958 insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
959 register const unsigned char *string;
960 register int nchars, nbytes;
961 int inherit, prepare, before_markers;
963 if (nchars == 0)
964 return;
966 if (NILP (current_buffer->enable_multibyte_characters))
967 nchars = nbytes;
969 if (prepare)
970 /* Do this before moving and increasing the gap,
971 because the before-change hooks might move the gap
972 or make it smaller. */
973 prepare_to_modify_buffer (PT, PT, NULL);
975 if (PT != GPT)
976 move_gap_both (PT, PT_BYTE);
977 if (GAP_SIZE < nbytes)
978 make_gap (nbytes - GAP_SIZE);
980 #ifdef BYTE_COMBINING_DEBUG
981 if (count_combining_before (string, nbytes, PT, PT_BYTE)
982 || count_combining_after (string, nbytes, PT, PT_BYTE))
983 abort ();
984 #endif
986 /* Record deletion of the surrounding text that combines with
987 the insertion. This, together with recording the insertion,
988 will add up to the right stuff in the undo list. */
989 record_insert (PT, nchars);
990 MODIFF++;
991 CHARS_MODIFF = MODIFF;
993 bcopy (string, GPT_ADDR, nbytes);
995 GAP_SIZE -= nbytes;
996 GPT += nchars;
997 ZV += nchars;
998 Z += nchars;
999 GPT_BYTE += nbytes;
1000 ZV_BYTE += nbytes;
1001 Z_BYTE += nbytes;
1002 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1004 if (GPT_BYTE < GPT)
1005 abort ();
1007 /* The insert may have been in the unchanged region, so check again. */
1008 if (Z - GPT < END_UNCHANGED)
1009 END_UNCHANGED = Z - GPT;
1011 adjust_overlays_for_insert (PT, nchars);
1012 adjust_markers_for_insert (PT, PT_BYTE,
1013 PT + nchars, PT_BYTE + nbytes,
1014 before_markers);
1016 if (BUF_INTERVALS (current_buffer) != 0)
1017 offset_intervals (current_buffer, PT, nchars);
1019 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
1020 set_text_properties (make_number (PT), make_number (PT + nchars),
1021 Qnil, Qnil, Qnil);
1023 adjust_point (nchars, nbytes);
1025 CHECK_MARKERS ();
1028 /* Insert the part of the text of STRING, a Lisp object assumed to be
1029 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1030 starting at position POS / POS_BYTE. If the text of STRING has properties,
1031 copy them into the buffer.
1033 It does not work to use `insert' for this, because a GC could happen
1034 before we bcopy the stuff into the buffer, and relocate the string
1035 without insert noticing. */
1037 void
1038 insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
1039 Lisp_Object string;
1040 register int pos, pos_byte, length, length_byte;
1041 int inherit;
1043 int opoint = PT;
1045 if (SCHARS (string) == 0)
1046 return;
1048 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1049 inherit, 0);
1050 signal_after_change (opoint, 0, PT - opoint);
1051 update_compositions (opoint, PT, CHECK_BORDER);
1054 /* Like `insert_from_string' except that all markers pointing
1055 at the place where the insertion happens are adjusted to point after it. */
1057 void
1058 insert_from_string_before_markers (string, pos, pos_byte,
1059 length, length_byte, inherit)
1060 Lisp_Object string;
1061 register int pos, pos_byte, length, length_byte;
1062 int inherit;
1064 int opoint = PT;
1066 if (SCHARS (string) == 0)
1067 return;
1069 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1070 inherit, 1);
1071 signal_after_change (opoint, 0, PT - opoint);
1072 update_compositions (opoint, PT, CHECK_BORDER);
1075 /* Subroutine of the insertion functions above. */
1077 static void
1078 insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
1079 inherit, before_markers)
1080 Lisp_Object string;
1081 register int pos, pos_byte, nchars, nbytes;
1082 int inherit, before_markers;
1084 struct gcpro gcpro1;
1085 int outgoing_nbytes = nbytes;
1086 INTERVAL intervals;
1088 /* Make OUTGOING_NBYTES describe the text
1089 as it will be inserted in this buffer. */
1091 if (NILP (current_buffer->enable_multibyte_characters))
1092 outgoing_nbytes = nchars;
1093 else if (! STRING_MULTIBYTE (string))
1094 outgoing_nbytes
1095 = count_size_as_multibyte (SDATA (string) + pos_byte,
1096 nbytes);
1098 GCPRO1 (string);
1099 /* Do this before moving and increasing the gap,
1100 because the before-change hooks might move the gap
1101 or make it smaller. */
1102 prepare_to_modify_buffer (PT, PT, NULL);
1104 if (PT != GPT)
1105 move_gap_both (PT, PT_BYTE);
1106 if (GAP_SIZE < outgoing_nbytes)
1107 make_gap (outgoing_nbytes - GAP_SIZE);
1108 UNGCPRO;
1110 /* Copy the string text into the buffer, perhaps converting
1111 between single-byte and multibyte. */
1112 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
1113 STRING_MULTIBYTE (string),
1114 ! NILP (current_buffer->enable_multibyte_characters));
1116 #ifdef BYTE_COMBINING_DEBUG
1117 /* We have copied text into the gap, but we have not altered
1118 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1119 to these functions and get the same results as we would
1120 have got earlier on. Meanwhile, PT_ADDR does point to
1121 the text that has been stored by copy_text. */
1122 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1123 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1124 abort ();
1125 #endif
1127 record_insert (PT, nchars);
1128 MODIFF++;
1129 CHARS_MODIFF = MODIFF;
1131 GAP_SIZE -= outgoing_nbytes;
1132 GPT += nchars;
1133 ZV += nchars;
1134 Z += nchars;
1135 GPT_BYTE += outgoing_nbytes;
1136 ZV_BYTE += outgoing_nbytes;
1137 Z_BYTE += outgoing_nbytes;
1138 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1140 if (GPT_BYTE < GPT)
1141 abort ();
1143 /* The insert may have been in the unchanged region, so check again. */
1144 if (Z - GPT < END_UNCHANGED)
1145 END_UNCHANGED = Z - GPT;
1147 adjust_overlays_for_insert (PT, nchars);
1148 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1149 PT_BYTE + outgoing_nbytes,
1150 before_markers);
1152 offset_intervals (current_buffer, PT, nchars);
1154 intervals = STRING_INTERVALS (string);
1155 /* Get the intervals for the part of the string we are inserting. */
1156 if (nbytes < SBYTES (string))
1157 intervals = copy_intervals (intervals, pos, nchars);
1159 /* Insert those intervals. */
1160 graft_intervals_into_buffer (intervals, PT, nchars,
1161 current_buffer, inherit);
1163 adjust_point (nchars, outgoing_nbytes);
1165 CHECK_MARKERS ();
1168 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1169 starting at GPT_ADDR. */
1171 void
1172 insert_from_gap (nchars, nbytes)
1173 register EMACS_INT nchars, nbytes;
1175 if (NILP (current_buffer->enable_multibyte_characters))
1176 nchars = nbytes;
1178 record_insert (GPT, nchars);
1179 MODIFF++;
1181 GAP_SIZE -= nbytes;
1182 GPT += nchars;
1183 ZV += nchars;
1184 Z += nchars;
1185 GPT_BYTE += nbytes;
1186 ZV_BYTE += nbytes;
1187 Z_BYTE += nbytes;
1188 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1190 if (GPT_BYTE < GPT)
1191 abort ();
1193 adjust_overlays_for_insert (GPT - nchars, nchars);
1194 adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
1195 GPT, GPT_BYTE, 0);
1197 if (BUF_INTERVALS (current_buffer) != 0)
1199 offset_intervals (current_buffer, GPT - nchars, nchars);
1200 graft_intervals_into_buffer (NULL_INTERVAL, GPT - nchars, nchars,
1201 current_buffer, 0);
1204 if (GPT - nchars < PT)
1205 adjust_point (nchars, nbytes);
1207 CHECK_MARKERS ();
1210 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1211 current buffer. If the text in BUF has properties, they are absorbed
1212 into the current buffer.
1214 It does not work to use `insert' for this, because a malloc could happen
1215 and relocate BUF's text before the bcopy happens. */
1217 void
1218 insert_from_buffer (buf, charpos, nchars, inherit)
1219 struct buffer *buf;
1220 int charpos, nchars;
1221 int inherit;
1223 int opoint = PT;
1225 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1226 signal_after_change (opoint, 0, PT - opoint);
1227 update_compositions (opoint, PT, CHECK_BORDER);
1230 static void
1231 insert_from_buffer_1 (buf, from, nchars, inherit)
1232 struct buffer *buf;
1233 int from, nchars;
1234 int inherit;
1236 register Lisp_Object temp;
1237 int chunk, chunk_expanded;
1238 int from_byte = buf_charpos_to_bytepos (buf, from);
1239 int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1240 int incoming_nbytes = to_byte - from_byte;
1241 int outgoing_nbytes = incoming_nbytes;
1242 INTERVAL intervals;
1244 /* Make OUTGOING_NBYTES describe the text
1245 as it will be inserted in this buffer. */
1247 if (NILP (current_buffer->enable_multibyte_characters))
1248 outgoing_nbytes = nchars;
1249 else if (NILP (buf->enable_multibyte_characters))
1251 int outgoing_before_gap = 0;
1252 int outgoing_after_gap = 0;
1254 if (from < BUF_GPT (buf))
1256 chunk = BUF_GPT_BYTE (buf) - from_byte;
1257 if (chunk > incoming_nbytes)
1258 chunk = incoming_nbytes;
1259 outgoing_before_gap
1260 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1261 chunk);
1263 else
1264 chunk = 0;
1266 if (chunk < incoming_nbytes)
1267 outgoing_after_gap
1268 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1269 from_byte + chunk),
1270 incoming_nbytes - chunk);
1272 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1275 /* Make sure point-max won't overflow after this insertion. */
1276 XSETINT (temp, outgoing_nbytes + Z);
1277 if (outgoing_nbytes + Z != XINT (temp))
1278 error ("Maximum buffer size exceeded");
1280 /* Do this before moving and increasing the gap,
1281 because the before-change hooks might move the gap
1282 or make it smaller. */
1283 prepare_to_modify_buffer (PT, PT, NULL);
1285 if (PT != GPT)
1286 move_gap_both (PT, PT_BYTE);
1287 if (GAP_SIZE < outgoing_nbytes)
1288 make_gap (outgoing_nbytes - GAP_SIZE);
1290 if (from < BUF_GPT (buf))
1292 chunk = BUF_GPT_BYTE (buf) - from_byte;
1293 if (chunk > incoming_nbytes)
1294 chunk = incoming_nbytes;
1295 /* Record number of output bytes, so we know where
1296 to put the output from the second copy_text. */
1297 chunk_expanded
1298 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1299 GPT_ADDR, chunk,
1300 ! NILP (buf->enable_multibyte_characters),
1301 ! NILP (current_buffer->enable_multibyte_characters));
1303 else
1304 chunk_expanded = chunk = 0;
1306 if (chunk < incoming_nbytes)
1307 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1308 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1309 ! NILP (buf->enable_multibyte_characters),
1310 ! NILP (current_buffer->enable_multibyte_characters));
1312 #ifdef BYTE_COMBINING_DEBUG
1313 /* We have copied text into the gap, but we have not altered
1314 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1315 to these functions and get the same results as we would
1316 have got earlier on. Meanwhile, GPT_ADDR does point to
1317 the text that has been stored by copy_text. */
1318 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1319 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1320 abort ();
1321 #endif
1323 record_insert (PT, nchars);
1324 MODIFF++;
1325 CHARS_MODIFF = MODIFF;
1327 GAP_SIZE -= outgoing_nbytes;
1328 GPT += nchars;
1329 ZV += nchars;
1330 Z += nchars;
1331 GPT_BYTE += outgoing_nbytes;
1332 ZV_BYTE += outgoing_nbytes;
1333 Z_BYTE += outgoing_nbytes;
1334 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1336 if (GPT_BYTE < GPT)
1337 abort ();
1339 /* The insert may have been in the unchanged region, so check again. */
1340 if (Z - GPT < END_UNCHANGED)
1341 END_UNCHANGED = Z - GPT;
1343 adjust_overlays_for_insert (PT, nchars);
1344 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1345 PT_BYTE + outgoing_nbytes,
1348 if (BUF_INTERVALS (current_buffer) != 0)
1349 offset_intervals (current_buffer, PT, nchars);
1351 /* Get the intervals for the part of the string we are inserting. */
1352 intervals = BUF_INTERVALS (buf);
1353 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1355 if (buf == current_buffer && PT <= from)
1356 from += nchars;
1357 intervals = copy_intervals (intervals, from, nchars);
1360 /* Insert those intervals. */
1361 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1363 adjust_point (nchars, outgoing_nbytes);
1366 /* Record undo information and adjust markers and position keepers for
1367 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1368 chars (LEN_BYTE bytes) which resides in the gap just after
1369 GPT_ADDR.
1371 PREV_TEXT nil means the new text was just inserted. */
1373 void
1374 adjust_after_replace (from, from_byte, prev_text, len, len_byte)
1375 int from, from_byte, len, len_byte;
1376 Lisp_Object prev_text;
1378 int nchars_del = 0, nbytes_del = 0;
1380 #ifdef BYTE_COMBINING_DEBUG
1381 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1382 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1383 abort ();
1384 #endif
1386 if (STRINGP (prev_text))
1388 nchars_del = SCHARS (prev_text);
1389 nbytes_del = SBYTES (prev_text);
1392 /* Update various buffer positions for the new text. */
1393 GAP_SIZE -= len_byte;
1394 ZV += len; Z+= len;
1395 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1396 GPT += len; GPT_BYTE += len_byte;
1397 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1399 if (nchars_del > 0)
1400 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1401 len, len_byte);
1402 else
1403 adjust_markers_for_insert (from, from_byte,
1404 from + len, from_byte + len_byte, 0);
1406 if (! EQ (current_buffer->undo_list, Qt))
1408 if (nchars_del > 0)
1409 record_delete (from, prev_text);
1410 record_insert (from, len);
1413 if (len > nchars_del)
1414 adjust_overlays_for_insert (from, len - nchars_del);
1415 else if (len < nchars_del)
1416 adjust_overlays_for_delete (from, nchars_del - len);
1417 if (BUF_INTERVALS (current_buffer) != 0)
1419 offset_intervals (current_buffer, from, len - nchars_del);
1422 if (from < PT)
1423 adjust_point (len - nchars_del, len_byte - nbytes_del);
1425 /* As byte combining will decrease Z, we must check this again. */
1426 if (Z - GPT < END_UNCHANGED)
1427 END_UNCHANGED = Z - GPT;
1429 CHECK_MARKERS ();
1431 if (len == 0)
1432 evaporate_overlays (from);
1433 MODIFF++;
1434 CHARS_MODIFF = MODIFF;
1437 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1438 This is for use when undo is not enabled in the current buffer. */
1440 void
1441 adjust_after_replace_noundo (from, from_byte, nchars_del, nbytes_del, len, len_byte)
1442 int from, from_byte, nchars_del, nbytes_del, len, len_byte;
1444 #ifdef BYTE_COMBINING_DEBUG
1445 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1446 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1447 abort ();
1448 #endif
1450 /* Update various buffer positions for the new text. */
1451 GAP_SIZE -= len_byte;
1452 ZV += len; Z+= len;
1453 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1454 GPT += len; GPT_BYTE += len_byte;
1455 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1457 if (nchars_del > 0)
1458 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1459 len, len_byte);
1460 else
1461 adjust_markers_for_insert (from, from_byte,
1462 from + len, from_byte + len_byte, 0);
1464 if (len > nchars_del)
1465 adjust_overlays_for_insert (from, len - nchars_del);
1466 else if (len < nchars_del)
1467 adjust_overlays_for_delete (from, nchars_del - len);
1468 if (BUF_INTERVALS (current_buffer) != 0)
1470 offset_intervals (current_buffer, from, len - nchars_del);
1473 if (from < PT)
1474 adjust_point (len - nchars_del, len_byte - nbytes_del);
1476 /* As byte combining will decrease Z, we must check this again. */
1477 if (Z - GPT < END_UNCHANGED)
1478 END_UNCHANGED = Z - GPT;
1480 CHECK_MARKERS ();
1482 if (len == 0)
1483 evaporate_overlays (from);
1484 MODIFF++;
1485 CHARS_MODIFF = MODIFF;
1488 /* Record undo information, adjust markers and position keepers for an
1489 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1490 text already exists in the current buffer but character length (TO
1491 - FROM) may be incorrect, the correct length is NEWLEN. */
1493 void
1494 adjust_after_insert (from, from_byte, to, to_byte, newlen)
1495 int from, from_byte, to, to_byte, newlen;
1497 int len = to - from, len_byte = to_byte - from_byte;
1499 if (GPT != to)
1500 move_gap_both (to, to_byte);
1501 GAP_SIZE += len_byte;
1502 GPT -= len; GPT_BYTE -= len_byte;
1503 ZV -= len; ZV_BYTE -= len_byte;
1504 Z -= len; Z_BYTE -= len_byte;
1505 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1508 /* Replace the text from character positions FROM to TO with NEW,
1509 If PREPARE is nonzero, call prepare_to_modify_buffer.
1510 If INHERIT, the newly inserted text should inherit text properties
1511 from the surrounding non-deleted text. */
1513 /* Note that this does not yet handle markers quite right.
1514 Also it needs to record a single undo-entry that does a replacement
1515 rather than a separate delete and insert.
1516 That way, undo will also handle markers properly.
1518 But if MARKERS is 0, don't relocate markers. */
1520 void
1521 replace_range (from, to, new, prepare, inherit, markers)
1522 Lisp_Object new;
1523 int from, to, prepare, inherit, markers;
1525 int inschars = SCHARS (new);
1526 int insbytes = SBYTES (new);
1527 int from_byte, to_byte;
1528 int nbytes_del, nchars_del;
1529 register Lisp_Object temp;
1530 struct gcpro gcpro1;
1531 INTERVAL intervals;
1532 int outgoing_insbytes = insbytes;
1533 Lisp_Object deletion;
1535 CHECK_MARKERS ();
1537 GCPRO1 (new);
1538 deletion = Qnil;
1540 if (prepare)
1542 int range_length = to - from;
1543 prepare_to_modify_buffer (from, to, &from);
1544 to = from + range_length;
1547 UNGCPRO;
1549 /* Make args be valid */
1550 if (from < BEGV)
1551 from = BEGV;
1552 if (to > ZV)
1553 to = ZV;
1555 from_byte = CHAR_TO_BYTE (from);
1556 to_byte = CHAR_TO_BYTE (to);
1558 nchars_del = to - from;
1559 nbytes_del = to_byte - from_byte;
1561 if (nbytes_del <= 0 && insbytes == 0)
1562 return;
1564 /* Make OUTGOING_INSBYTES describe the text
1565 as it will be inserted in this buffer. */
1567 if (NILP (current_buffer->enable_multibyte_characters))
1568 outgoing_insbytes = inschars;
1569 else if (! STRING_MULTIBYTE (new))
1570 outgoing_insbytes
1571 = count_size_as_multibyte (SDATA (new), insbytes);
1573 /* Make sure point-max won't overflow after this insertion. */
1574 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1575 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1576 error ("Maximum buffer size exceeded");
1578 GCPRO1 (new);
1580 /* Make sure the gap is somewhere in or next to what we are deleting. */
1581 if (from > GPT)
1582 gap_right (from, from_byte);
1583 if (to < GPT)
1584 gap_left (to, to_byte, 0);
1586 /* Even if we don't record for undo, we must keep the original text
1587 because we may have to recover it because of inappropriate byte
1588 combining. */
1589 if (! EQ (current_buffer->undo_list, Qt))
1590 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1592 GAP_SIZE += nbytes_del;
1593 ZV -= nchars_del;
1594 Z -= nchars_del;
1595 ZV_BYTE -= nbytes_del;
1596 Z_BYTE -= nbytes_del;
1597 GPT = from;
1598 GPT_BYTE = from_byte;
1599 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1601 if (GPT_BYTE < GPT)
1602 abort ();
1604 if (GPT - BEG < BEG_UNCHANGED)
1605 BEG_UNCHANGED = GPT - BEG;
1606 if (Z - GPT < END_UNCHANGED)
1607 END_UNCHANGED = Z - GPT;
1609 if (GAP_SIZE < insbytes)
1610 make_gap (insbytes - GAP_SIZE);
1612 /* Copy the string text into the buffer, perhaps converting
1613 between single-byte and multibyte. */
1614 copy_text (SDATA (new), GPT_ADDR, insbytes,
1615 STRING_MULTIBYTE (new),
1616 ! NILP (current_buffer->enable_multibyte_characters));
1618 #ifdef BYTE_COMBINING_DEBUG
1619 /* We have copied text into the gap, but we have not marked
1620 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1621 here, for both the previous text and the following text.
1622 Meanwhile, GPT_ADDR does point to
1623 the text that has been stored by copy_text. */
1624 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1625 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1626 abort ();
1627 #endif
1629 if (! EQ (current_buffer->undo_list, Qt))
1631 /* Record the insertion first, so that when we undo,
1632 the deletion will be undone first. Thus, undo
1633 will insert before deleting, and thus will keep
1634 the markers before and after this text separate. */
1635 record_insert (from + SCHARS (deletion), inschars);
1636 record_delete (from, deletion);
1639 GAP_SIZE -= outgoing_insbytes;
1640 GPT += inschars;
1641 ZV += inschars;
1642 Z += inschars;
1643 GPT_BYTE += outgoing_insbytes;
1644 ZV_BYTE += outgoing_insbytes;
1645 Z_BYTE += outgoing_insbytes;
1646 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1648 if (GPT_BYTE < GPT)
1649 abort ();
1651 /* Adjust the overlay center as needed. This must be done after
1652 adjusting the markers that bound the overlays. */
1653 adjust_overlays_for_delete (from, nchars_del);
1654 adjust_overlays_for_insert (from, inschars);
1656 /* Adjust markers for the deletion and the insertion. */
1657 if (markers)
1658 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1659 inschars, outgoing_insbytes);
1661 offset_intervals (current_buffer, from, inschars - nchars_del);
1663 /* Get the intervals for the part of the string we are inserting--
1664 not including the combined-before bytes. */
1665 intervals = STRING_INTERVALS (new);
1666 /* Insert those intervals. */
1667 graft_intervals_into_buffer (intervals, from, inschars,
1668 current_buffer, inherit);
1670 /* Relocate point as if it were a marker. */
1671 if (from < PT)
1672 adjust_point ((from + inschars - (PT < to ? PT : to)),
1673 (from_byte + outgoing_insbytes
1674 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1676 if (outgoing_insbytes == 0)
1677 evaporate_overlays (from);
1679 CHECK_MARKERS ();
1681 MODIFF++;
1682 CHARS_MODIFF = MODIFF;
1683 UNGCPRO;
1685 signal_after_change (from, nchars_del, GPT - from);
1686 update_compositions (from, GPT, CHECK_BORDER);
1689 /* Replace the text from character positions FROM to TO with
1690 the text in INS of length INSCHARS.
1691 Keep the text properties that applied to the old characters
1692 (extending them to all the new chars if there are more new chars).
1694 Note that this does not yet handle markers quite right.
1696 If MARKERS is nonzero, relocate markers.
1698 Unlike most functions at this level, never call
1699 prepare_to_modify_buffer and never call signal_after_change. */
1701 void
1702 replace_range_2 (from, from_byte, to, to_byte, ins, inschars, insbytes, markers)
1703 int from, from_byte, to, to_byte;
1704 char *ins;
1705 int inschars, insbytes, markers;
1707 int nbytes_del, nchars_del;
1708 Lisp_Object temp;
1710 CHECK_MARKERS ();
1712 nchars_del = to - from;
1713 nbytes_del = to_byte - from_byte;
1715 if (nbytes_del <= 0 && insbytes == 0)
1716 return;
1718 /* Make sure point-max won't overflow after this insertion. */
1719 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1720 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1721 error ("Maximum buffer size exceeded");
1723 /* Make sure the gap is somewhere in or next to what we are deleting. */
1724 if (from > GPT)
1725 gap_right (from, from_byte);
1726 if (to < GPT)
1727 gap_left (to, to_byte, 0);
1729 GAP_SIZE += nbytes_del;
1730 ZV -= nchars_del;
1731 Z -= nchars_del;
1732 ZV_BYTE -= nbytes_del;
1733 Z_BYTE -= nbytes_del;
1734 GPT = from;
1735 GPT_BYTE = from_byte;
1736 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1738 if (GPT_BYTE < GPT)
1739 abort ();
1741 if (GPT - BEG < BEG_UNCHANGED)
1742 BEG_UNCHANGED = GPT - BEG;
1743 if (Z - GPT < END_UNCHANGED)
1744 END_UNCHANGED = Z - GPT;
1746 if (GAP_SIZE < insbytes)
1747 make_gap (insbytes - GAP_SIZE);
1749 /* Copy the replacement text into the buffer. */
1750 bcopy (ins, GPT_ADDR, insbytes);
1752 #ifdef BYTE_COMBINING_DEBUG
1753 /* We have copied text into the gap, but we have not marked
1754 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1755 here, for both the previous text and the following text.
1756 Meanwhile, GPT_ADDR does point to
1757 the text that has been stored by copy_text. */
1758 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1759 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1760 abort ();
1761 #endif
1763 GAP_SIZE -= insbytes;
1764 GPT += inschars;
1765 ZV += inschars;
1766 Z += inschars;
1767 GPT_BYTE += insbytes;
1768 ZV_BYTE += insbytes;
1769 Z_BYTE += insbytes;
1770 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1772 if (GPT_BYTE < GPT)
1773 abort ();
1775 /* Adjust the overlay center as needed. This must be done after
1776 adjusting the markers that bound the overlays. */
1777 if (nchars_del != inschars)
1779 adjust_overlays_for_insert (from, inschars);
1780 adjust_overlays_for_delete (from + inschars, nchars_del);
1783 /* Adjust markers for the deletion and the insertion. */
1784 if (markers
1785 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1786 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1787 inschars, insbytes);
1789 offset_intervals (current_buffer, from, inschars - nchars_del);
1791 /* Relocate point as if it were a marker. */
1792 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1794 if (PT < to)
1795 /* PT was within the deleted text. Move it to FROM. */
1796 adjust_point (from - PT, from_byte - PT_BYTE);
1797 else
1798 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1801 if (insbytes == 0)
1802 evaporate_overlays (from);
1804 CHECK_MARKERS ();
1806 MODIFF++;
1807 CHARS_MODIFF = MODIFF;
1810 /* Delete characters in current buffer
1811 from FROM up to (but not including) TO.
1812 If TO comes before FROM, we delete nothing. */
1814 void
1815 del_range (from, to)
1816 register int from, to;
1818 del_range_1 (from, to, 1, 0);
1821 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1822 RET_STRING says to return the deleted text. */
1824 Lisp_Object
1825 del_range_1 (from, to, prepare, ret_string)
1826 int from, to, prepare, ret_string;
1828 int from_byte, to_byte;
1829 Lisp_Object deletion;
1830 struct gcpro gcpro1;
1832 /* Make args be valid */
1833 if (from < BEGV)
1834 from = BEGV;
1835 if (to > ZV)
1836 to = ZV;
1838 if (to <= from)
1839 return Qnil;
1841 if (prepare)
1843 int range_length = to - from;
1844 prepare_to_modify_buffer (from, to, &from);
1845 to = min (ZV, from + range_length);
1848 from_byte = CHAR_TO_BYTE (from);
1849 to_byte = CHAR_TO_BYTE (to);
1851 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1852 GCPRO1(deletion);
1853 signal_after_change (from, to - from, 0);
1854 update_compositions (from, from, CHECK_HEAD);
1855 UNGCPRO;
1856 return deletion;
1859 /* Like del_range_1 but args are byte positions, not char positions. */
1861 void
1862 del_range_byte (from_byte, to_byte, prepare)
1863 int from_byte, to_byte, prepare;
1865 int from, to;
1867 /* Make args be valid */
1868 if (from_byte < BEGV_BYTE)
1869 from_byte = BEGV_BYTE;
1870 if (to_byte > ZV_BYTE)
1871 to_byte = ZV_BYTE;
1873 if (to_byte <= from_byte)
1874 return;
1876 from = BYTE_TO_CHAR (from_byte);
1877 to = BYTE_TO_CHAR (to_byte);
1879 if (prepare)
1881 int old_from = from, old_to = Z - to;
1882 int range_length = to - from;
1883 prepare_to_modify_buffer (from, to, &from);
1884 to = from + range_length;
1886 if (old_from != from)
1887 from_byte = CHAR_TO_BYTE (from);
1888 if (to > ZV)
1890 to = ZV;
1891 to_byte = ZV_BYTE;
1893 else if (old_to == Z - to)
1894 to_byte = CHAR_TO_BYTE (to);
1897 del_range_2 (from, from_byte, to, to_byte, 0);
1898 signal_after_change (from, to - from, 0);
1899 update_compositions (from, from, CHECK_HEAD);
1902 /* Like del_range_1, but positions are specified both as charpos
1903 and bytepos. */
1905 void
1906 del_range_both (from, from_byte, to, to_byte, prepare)
1907 int from, from_byte, to, to_byte, prepare;
1909 /* Make args be valid */
1910 if (from_byte < BEGV_BYTE)
1911 from_byte = BEGV_BYTE;
1912 if (to_byte > ZV_BYTE)
1913 to_byte = ZV_BYTE;
1915 if (to_byte <= from_byte)
1916 return;
1918 if (from < BEGV)
1919 from = BEGV;
1920 if (to > ZV)
1921 to = ZV;
1923 if (prepare)
1925 int old_from = from, old_to = Z - to;
1926 int range_length = to - from;
1927 prepare_to_modify_buffer (from, to, &from);
1928 to = from + range_length;
1930 if (old_from != from)
1931 from_byte = CHAR_TO_BYTE (from);
1932 if (to > ZV)
1934 to = ZV;
1935 to_byte = ZV_BYTE;
1937 else if (old_to == Z - to)
1938 to_byte = CHAR_TO_BYTE (to);
1941 del_range_2 (from, from_byte, to, to_byte, 0);
1942 signal_after_change (from, to - from, 0);
1943 update_compositions (from, from, CHECK_HEAD);
1946 /* Delete a range of text, specified both as character positions
1947 and byte positions. FROM and TO are character positions,
1948 while FROM_BYTE and TO_BYTE are byte positions.
1949 If RET_STRING is true, the deleted area is returned as a string. */
1951 Lisp_Object
1952 del_range_2 (from, from_byte, to, to_byte, ret_string)
1953 int from, from_byte, to, to_byte, ret_string;
1955 register int nbytes_del, nchars_del;
1956 Lisp_Object deletion;
1958 CHECK_MARKERS ();
1960 nchars_del = to - from;
1961 nbytes_del = to_byte - from_byte;
1963 /* Make sure the gap is somewhere in or next to what we are deleting. */
1964 if (from > GPT)
1965 gap_right (from, from_byte);
1966 if (to < GPT)
1967 gap_left (to, to_byte, 0);
1969 #ifdef BYTE_COMBINING_DEBUG
1970 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1971 Z_BYTE - to_byte, from, from_byte))
1972 abort ();
1973 #endif
1975 if (ret_string || ! EQ (current_buffer->undo_list, Qt))
1976 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1977 else
1978 deletion = Qnil;
1980 /* Relocate all markers pointing into the new, larger gap
1981 to point at the end of the text before the gap.
1982 Do this before recording the deletion,
1983 so that undo handles this after reinserting the text. */
1984 adjust_markers_for_delete (from, from_byte, to, to_byte);
1986 if (! EQ (current_buffer->undo_list, Qt))
1987 record_delete (from, deletion);
1988 MODIFF++;
1989 CHARS_MODIFF = MODIFF;
1991 /* Relocate point as if it were a marker. */
1992 if (from < PT)
1993 adjust_point (from - (PT < to ? PT : to),
1994 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1996 offset_intervals (current_buffer, from, - nchars_del);
1998 /* Adjust the overlay center as needed. This must be done after
1999 adjusting the markers that bound the overlays. */
2000 adjust_overlays_for_delete (from, nchars_del);
2002 GAP_SIZE += nbytes_del;
2003 ZV_BYTE -= nbytes_del;
2004 Z_BYTE -= nbytes_del;
2005 ZV -= nchars_del;
2006 Z -= nchars_del;
2007 GPT = from;
2008 GPT_BYTE = from_byte;
2009 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
2011 if (GPT_BYTE < GPT)
2012 abort ();
2014 if (GPT - BEG < BEG_UNCHANGED)
2015 BEG_UNCHANGED = GPT - BEG;
2016 if (Z - GPT < END_UNCHANGED)
2017 END_UNCHANGED = Z - GPT;
2019 CHECK_MARKERS ();
2021 evaporate_overlays (from);
2023 return deletion;
2026 /* Call this if you're about to change the region of BUFFER from
2027 character positions START to END. This checks the read-only
2028 properties of the region, calls the necessary modification hooks,
2029 and warns the next redisplay that it should pay attention to that
2030 area.
2032 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
2033 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
2035 void
2036 modify_region (buffer, start, end, preserve_chars_modiff)
2037 struct buffer *buffer;
2038 int start, end, preserve_chars_modiff;
2040 struct buffer *old_buffer = current_buffer;
2042 if (buffer != old_buffer)
2043 set_buffer_internal (buffer);
2045 prepare_to_modify_buffer (start, end, NULL);
2047 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
2049 if (MODIFF <= SAVE_MODIFF)
2050 record_first_change ();
2051 MODIFF++;
2052 if (! preserve_chars_modiff)
2053 CHARS_MODIFF = MODIFF;
2055 buffer->point_before_scroll = Qnil;
2057 if (buffer != old_buffer)
2058 set_buffer_internal (old_buffer);
2061 /* Check that it is okay to modify the buffer between START and END,
2062 which are char positions.
2064 Run the before-change-function, if any. If intervals are in use,
2065 verify that the text to be modified is not read-only, and call
2066 any modification properties the text may have.
2068 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2069 by holding its value temporarily in a marker. */
2071 void
2072 prepare_to_modify_buffer (start, end, preserve_ptr)
2073 int start, end;
2074 int *preserve_ptr;
2076 struct buffer *base_buffer;
2078 if (!NILP (current_buffer->read_only))
2079 Fbarf_if_buffer_read_only ();
2081 /* Let redisplay consider other windows than selected_window
2082 if modifying another buffer. */
2083 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2084 ++windows_or_buffers_changed;
2086 if (BUF_INTERVALS (current_buffer) != 0)
2088 if (preserve_ptr)
2090 Lisp_Object preserve_marker;
2091 struct gcpro gcpro1;
2092 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
2093 GCPRO1 (preserve_marker);
2094 verify_interval_modification (current_buffer, start, end);
2095 *preserve_ptr = marker_position (preserve_marker);
2096 unchain_marker (XMARKER (preserve_marker));
2097 UNGCPRO;
2099 else
2100 verify_interval_modification (current_buffer, start, end);
2103 /* For indirect buffers, use the base buffer to check clashes. */
2104 if (current_buffer->base_buffer != 0)
2105 base_buffer = current_buffer->base_buffer;
2106 else
2107 base_buffer = current_buffer;
2109 #ifdef CLASH_DETECTION
2110 if (!NILP (base_buffer->file_truename)
2111 /* Make binding buffer-file-name to nil effective. */
2112 && !NILP (base_buffer->filename)
2113 && SAVE_MODIFF >= MODIFF)
2114 lock_file (base_buffer->file_truename);
2115 #else
2116 /* At least warn if this file has changed on disk since it was visited. */
2117 if (!NILP (base_buffer->filename)
2118 && SAVE_MODIFF >= MODIFF
2119 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2120 && !NILP (Ffile_exists_p (base_buffer->filename)))
2121 call1 (intern ("ask-user-about-supersession-threat"),
2122 base_buffer->filename);
2123 #endif /* not CLASH_DETECTION */
2125 signal_before_change (start, end, preserve_ptr);
2127 if (current_buffer->newline_cache)
2128 invalidate_region_cache (current_buffer,
2129 current_buffer->newline_cache,
2130 start - BEG, Z - end);
2131 if (current_buffer->width_run_cache)
2132 invalidate_region_cache (current_buffer,
2133 current_buffer->width_run_cache,
2134 start - BEG, Z - end);
2136 Vdeactivate_mark = Qt;
2139 /* These macros work with an argument named `preserve_ptr'
2140 and a local variable named `preserve_marker'. */
2142 #define PRESERVE_VALUE \
2143 if (preserve_ptr && NILP (preserve_marker)) \
2144 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2146 #define RESTORE_VALUE \
2147 if (! NILP (preserve_marker)) \
2149 *preserve_ptr = marker_position (preserve_marker); \
2150 unchain_marker (XMARKER (preserve_marker)); \
2153 #define PRESERVE_START_END \
2154 if (NILP (start_marker)) \
2155 start_marker = Fcopy_marker (start, Qnil); \
2156 if (NILP (end_marker)) \
2157 end_marker = Fcopy_marker (end, Qnil);
2159 #define FETCH_START \
2160 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2162 #define FETCH_END \
2163 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2165 /* Set a variable to nil if an error occurred.
2166 Don't change the variable if there was no error.
2167 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2168 VARIABLE is the variable to maybe set to nil.
2169 NO-ERROR-FLAG is nil if there was an error,
2170 anything else meaning no error (so this function does nothing). */
2171 Lisp_Object
2172 reset_var_on_error (val)
2173 Lisp_Object val;
2175 if (NILP (XCDR (val)))
2176 Fset (XCAR (val), Qnil);
2177 return Qnil;
2180 /* Signal a change to the buffer immediately before it happens.
2181 START_INT and END_INT are the bounds of the text to be changed.
2183 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2184 by holding its value temporarily in a marker. */
2186 void
2187 signal_before_change (start_int, end_int, preserve_ptr)
2188 int start_int, end_int;
2189 int *preserve_ptr;
2191 Lisp_Object start, end;
2192 Lisp_Object start_marker, end_marker;
2193 Lisp_Object preserve_marker;
2194 struct gcpro gcpro1, gcpro2, gcpro3;
2195 int count = SPECPDL_INDEX ();
2197 if (inhibit_modification_hooks)
2198 return;
2200 start = make_number (start_int);
2201 end = make_number (end_int);
2202 preserve_marker = Qnil;
2203 start_marker = Qnil;
2204 end_marker = Qnil;
2205 GCPRO3 (preserve_marker, start_marker, end_marker);
2207 specbind (Qinhibit_modification_hooks, Qt);
2209 /* If buffer is unmodified, run a special hook for that case. */
2210 if (SAVE_MODIFF >= MODIFF
2211 && !NILP (Vfirst_change_hook)
2212 && !NILP (Vrun_hooks))
2214 PRESERVE_VALUE;
2215 PRESERVE_START_END;
2216 call1 (Vrun_hooks, Qfirst_change_hook);
2219 /* Now run the before-change-functions if any. */
2220 if (!NILP (Vbefore_change_functions))
2222 Lisp_Object args[3];
2223 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
2225 PRESERVE_VALUE;
2226 PRESERVE_START_END;
2228 /* Mark before-change-functions to be reset to nil in case of error. */
2229 record_unwind_protect (reset_var_on_error, rvoe_arg);
2231 /* Actually run the hook functions. */
2232 args[0] = Qbefore_change_functions;
2233 args[1] = FETCH_START;
2234 args[2] = FETCH_END;
2235 Frun_hook_with_args (3, args);
2237 /* There was no error: unarm the reset_on_error. */
2238 XSETCDR (rvoe_arg, Qt);
2241 if (current_buffer->overlays_before || current_buffer->overlays_after)
2243 PRESERVE_VALUE;
2244 report_overlay_modification (FETCH_START, FETCH_END, 0,
2245 FETCH_START, FETCH_END, Qnil);
2248 if (! NILP (start_marker))
2249 free_marker (start_marker);
2250 if (! NILP (end_marker))
2251 free_marker (end_marker);
2252 RESTORE_VALUE;
2253 UNGCPRO;
2255 unbind_to (count, Qnil);
2258 /* Signal a change immediately after it happens.
2259 CHARPOS is the character position of the start of the changed text.
2260 LENDEL is the number of characters of the text before the change.
2261 (Not the whole buffer; just the part that was changed.)
2262 LENINS is the number of characters in that part of the text
2263 after the change. */
2265 void
2266 signal_after_change (charpos, lendel, lenins)
2267 int charpos, lendel, lenins;
2269 int count = SPECPDL_INDEX ();
2270 if (inhibit_modification_hooks)
2271 return;
2273 /* If we are deferring calls to the after-change functions
2274 and there are no before-change functions,
2275 just record the args that we were going to use. */
2276 if (! NILP (Vcombine_after_change_calls)
2277 && NILP (Vbefore_change_functions)
2278 && !current_buffer->overlays_before
2279 && !current_buffer->overlays_after)
2281 Lisp_Object elt;
2283 if (!NILP (combine_after_change_list)
2284 && current_buffer != XBUFFER (combine_after_change_buffer))
2285 Fcombine_after_change_execute ();
2287 elt = Fcons (make_number (charpos - BEG),
2288 Fcons (make_number (Z - (charpos - lendel + lenins)),
2289 Fcons (make_number (lenins - lendel), Qnil)));
2290 combine_after_change_list
2291 = Fcons (elt, combine_after_change_list);
2292 combine_after_change_buffer = Fcurrent_buffer ();
2294 return;
2297 if (!NILP (combine_after_change_list))
2298 Fcombine_after_change_execute ();
2300 specbind (Qinhibit_modification_hooks, Qt);
2302 if (!NILP (Vafter_change_functions))
2304 Lisp_Object args[4];
2305 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2307 /* Mark after-change-functions to be reset to nil in case of error. */
2308 record_unwind_protect (reset_var_on_error, rvoe_arg);
2310 /* Actually run the hook functions. */
2311 args[0] = Qafter_change_functions;
2312 XSETFASTINT (args[1], charpos);
2313 XSETFASTINT (args[2], charpos + lenins);
2314 XSETFASTINT (args[3], lendel);
2315 Frun_hook_with_args (4, args);
2317 /* There was no error: unarm the reset_on_error. */
2318 XSETCDR (rvoe_arg, Qt);
2321 if (current_buffer->overlays_before || current_buffer->overlays_after)
2322 report_overlay_modification (make_number (charpos),
2323 make_number (charpos + lenins),
2325 make_number (charpos),
2326 make_number (charpos + lenins),
2327 make_number (lendel));
2329 /* After an insertion, call the text properties
2330 insert-behind-hooks or insert-in-front-hooks. */
2331 if (lendel == 0)
2332 report_interval_modification (make_number (charpos),
2333 make_number (charpos + lenins));
2335 unbind_to (count, Qnil);
2338 Lisp_Object
2339 Fcombine_after_change_execute_1 (val)
2340 Lisp_Object val;
2342 Vcombine_after_change_calls = val;
2343 return val;
2346 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2347 Scombine_after_change_execute, 0, 0, 0,
2348 doc: /* This function is for use internally in `combine-after-change-calls'. */)
2351 int count = SPECPDL_INDEX ();
2352 int beg, end, change;
2353 int begpos, endpos;
2354 Lisp_Object tail;
2356 if (NILP (combine_after_change_list))
2357 return Qnil;
2359 /* It is rare for combine_after_change_buffer to be invalid, but
2360 possible. It can happen when combine-after-change-calls is
2361 non-nil, and insertion calls a file handler (e.g. through
2362 lock_file) which scribbles into a temp file -- cyd */
2363 if (!BUFFERP (combine_after_change_buffer)
2364 || NILP (XBUFFER (combine_after_change_buffer)->name))
2366 combine_after_change_list = Qnil;
2367 return Qnil;
2370 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2372 Fset_buffer (combine_after_change_buffer);
2374 /* # chars unchanged at beginning of buffer. */
2375 beg = Z - BEG;
2376 /* # chars unchanged at end of buffer. */
2377 end = beg;
2378 /* Total amount of insertion (negative for deletion). */
2379 change = 0;
2381 /* Scan the various individual changes,
2382 accumulating the range info in BEG, END and CHANGE. */
2383 for (tail = combine_after_change_list; CONSP (tail);
2384 tail = XCDR (tail))
2386 Lisp_Object elt;
2387 int thisbeg, thisend, thischange;
2389 /* Extract the info from the next element. */
2390 elt = XCAR (tail);
2391 if (! CONSP (elt))
2392 continue;
2393 thisbeg = XINT (XCAR (elt));
2395 elt = XCDR (elt);
2396 if (! CONSP (elt))
2397 continue;
2398 thisend = XINT (XCAR (elt));
2400 elt = XCDR (elt);
2401 if (! CONSP (elt))
2402 continue;
2403 thischange = XINT (XCAR (elt));
2405 /* Merge this range into the accumulated range. */
2406 change += thischange;
2407 if (thisbeg < beg)
2408 beg = thisbeg;
2409 if (thisend < end)
2410 end = thisend;
2413 /* Get the current start and end positions of the range
2414 that was changed. */
2415 begpos = BEG + beg;
2416 endpos = Z - end;
2418 /* We are about to handle these, so discard them. */
2419 combine_after_change_list = Qnil;
2421 /* Now run the after-change functions for real.
2422 Turn off the flag that defers them. */
2423 record_unwind_protect (Fcombine_after_change_execute_1,
2424 Vcombine_after_change_calls);
2425 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2426 update_compositions (begpos, endpos, CHECK_ALL);
2428 return unbind_to (count, Qnil);
2431 void
2432 syms_of_insdel ()
2434 staticpro (&combine_after_change_list);
2435 staticpro (&combine_after_change_buffer);
2436 combine_after_change_list = Qnil;
2437 combine_after_change_buffer = Qnil;
2439 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2440 doc: /* Non-nil means enable debugging checks for invalid marker positions. */);
2441 check_markers_debug_flag = 0;
2442 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2443 doc: /* Used internally by the `combine-after-change-calls' macro. */);
2444 Vcombine_after_change_calls = Qnil;
2446 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2447 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2448 This affects `before-change-functions' and `after-change-functions',
2449 as well as hooks attached to text properties and overlays. */);
2450 inhibit_modification_hooks = 0;
2451 Qinhibit_modification_hooks = intern ("inhibit-modification-hooks");
2452 staticpro (&Qinhibit_modification_hooks);
2454 defsubr (&Scombine_after_change_execute);
2457 /* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
2458 (do not change this comment) */