1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86, 93, 94, 95, 97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "intervals.h"
28 #include "blockinput.h"
29 #include "region-cache.h"
35 #define min(x, y) ((x) < (y) ? (x) : (y))
36 #define max(x, y) ((x) > (y) ? (x) : (y))
38 static void insert_from_string_1
P_ ((Lisp_Object
, int, int, int, int, int, int));
39 static void insert_from_buffer_1 ();
40 static void gap_left
P_ ((int, int, int));
41 static void gap_right
P_ ((int, int));
42 static void adjust_markers_gap_motion
P_ ((int, int, int));
43 static void adjust_markers_for_insert
P_ ((int, int, int, int, int, int, int));
44 static void adjust_markers_for_delete
P_ ((int, int, int, int));
45 static void adjust_markers_for_record_delete
P_ ((int, int, int, int));
46 static void adjust_point
P_ ((int, int));
48 Lisp_Object
Fcombine_after_change_execute ();
50 /* Non-nil means don't call the after-change-functions right away,
51 just record an element in Vcombine_after_change_calls_list. */
52 Lisp_Object Vcombine_after_change_calls
;
54 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
55 describing changes which happened while combine_after_change_calls
56 was nonzero. We use this to decide how to call them
57 once the deferral ends.
60 BEG-UNCHANGED is the number of chars before the changed range.
61 END-UNCHANGED is the number of chars after the changed range,
62 and CHANGE-AMOUNT is the number of characters inserted by the change
63 (negative for a deletion). */
64 Lisp_Object combine_after_change_list
;
66 /* Buffer which combine_after_change_list is about. */
67 Lisp_Object combine_after_change_buffer
;
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) \
81 register Lisp_Object tail
, prev
, next
;
82 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
84 tail
= BUF_MARKERS (current_buffer
);
86 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
88 if (XMARKER (tail
)->buffer
->text
!= current_buffer
->text
)
90 if (XMARKER (tail
)->charpos
> Z
)
92 if (XMARKER (tail
)->bytepos
> Z_BYTE
)
94 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail
)->bytepos
)))
97 tail
= XMARKER (tail
)->chain
;
101 /* Move gap to position CHARPOS.
102 Note that this can quit! */
108 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
111 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
112 Note that this can quit! */
115 move_gap_both (charpos
, bytepos
)
116 int charpos
, bytepos
;
118 if (bytepos
< GPT_BYTE
)
119 gap_left (charpos
, bytepos
, 0);
120 else if (bytepos
> GPT_BYTE
)
121 gap_right (charpos
, bytepos
);
124 /* Move the gap to a position less than the current GPT.
125 BYTEPOS describes the new position as a byte position,
126 and CHARPOS is the corresponding char position.
127 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
130 gap_left (charpos
, bytepos
, newgap
)
131 register int charpos
, bytepos
;
134 register unsigned char *to
, *from
;
140 if (unchanged_modified
== MODIFF
141 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
143 beg_unchanged
= charpos
- BEG
;
144 end_unchanged
= Z
- charpos
;
148 if (Z
- GPT
< end_unchanged
)
149 end_unchanged
= Z
- GPT
;
150 if (charpos
< beg_unchanged
)
151 beg_unchanged
= charpos
- BEG
;
160 /* Now copy the characters. To move the gap down,
161 copy characters up. */
165 /* I gets number of characters left to copy. */
166 i
= new_s1
- bytepos
;
169 /* If a quit is requested, stop copying now.
170 Change BYTEPOS to be where we have actually moved the gap to. */
174 charpos
= BYTE_TO_CHAR (bytepos
);
177 /* Move at most 32000 chars before checking again for a quit. */
182 /* bcopy is safe if the two areas of memory do not overlap
183 or on systems where bcopy is always safe for moving upward. */
184 && (BCOPY_UPWARD_SAFE
185 || to
- from
>= 128))
187 /* If overlap is not safe, avoid it by not moving too many
188 characters at once. */
189 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
204 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
205 BYTEPOS is where the loop above stopped, which may be what was specified
206 or may be where a quit was detected. */
207 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
210 if (bytepos
< charpos
)
212 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
216 /* Move the gap to a position greater than than the current GPT.
217 BYTEPOS describes the new position as a byte position,
218 and CHARPOS is the corresponding char position. */
221 gap_right (charpos
, bytepos
)
222 register int charpos
, bytepos
;
224 register unsigned char *to
, *from
;
228 if (unchanged_modified
== MODIFF
229 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
231 beg_unchanged
= charpos
- BEG
;
232 end_unchanged
= Z
- charpos
;
236 if (Z
- charpos
- 1 < end_unchanged
)
237 end_unchanged
= Z
- charpos
;
238 if (GPT
- BEG
< beg_unchanged
)
239 beg_unchanged
= GPT
- BEG
;
247 /* Now copy the characters. To move the gap up,
248 copy characters down. */
252 /* I gets number of characters left to copy. */
253 i
= bytepos
- new_s1
;
256 /* If a quit is requested, stop copying now.
257 Change BYTEPOS to be where we have actually moved the gap to. */
261 charpos
= BYTE_TO_CHAR (bytepos
);
264 /* Move at most 32000 chars before checking again for a quit. */
269 /* bcopy is safe if the two areas of memory do not overlap
270 or on systems where bcopy is always safe for moving downward. */
271 && (BCOPY_DOWNWARD_SAFE
272 || from
- to
>= 128))
274 /* If overlap is not safe, avoid it by not moving too many
275 characters at once. */
276 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
291 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
295 if (bytepos
< charpos
)
297 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
301 /* Add AMOUNT to the byte position of every marker in the current buffer
302 whose current byte position is between FROM (exclusive) and TO (inclusive).
304 Also, any markers past the outside of that interval, in the direction
305 of adjustment, are first moved back to the near end of the interval
306 and then adjusted by AMOUNT.
308 When the latter adjustment is done, if AMOUNT is negative,
309 we record the adjustment for undo. (This case happens only for
312 The markers' character positions are not altered,
313 because gap motion does not affect character positions. */
315 int adjust_markers_test
;
318 adjust_markers_gap_motion (from
, to
, amount
)
319 register int from
, to
, amount
;
321 /* Now that a marker has a bytepos, not counting the gap,
322 nothing needs to be done here. */
325 register struct Lisp_Marker
*m
;
328 marker
= BUF_MARKERS (current_buffer
);
330 while (!NILP (marker
))
332 m
= XMARKER (marker
);
336 if (mpos
> to
&& mpos
< to
+ amount
)
338 if (adjust_markers_test
)
345 /* Here's the case where a marker is inside text being deleted.
346 AMOUNT can be negative for gap motion, too,
347 but then this range contains no markers. */
348 if (mpos
> from
+ amount
&& mpos
<= from
)
350 if (adjust_markers_test
)
352 mpos
= from
+ amount
;
355 if (mpos
> from
&& mpos
<= to
)
363 /* Adjust all markers for a deletion
364 whose range in bytes is FROM_BYTE to TO_BYTE.
365 The range in charpos is FROM to TO.
367 This function assumes that the gap is adjacent to
368 or inside of the range being deleted. */
371 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
372 register int from
, from_byte
, to
, to_byte
;
375 register struct Lisp_Marker
*m
;
376 register int charpos
;
378 marker
= BUF_MARKERS (current_buffer
);
380 while (!NILP (marker
))
382 m
= XMARKER (marker
);
383 charpos
= m
->charpos
;
388 /* If the marker is after the deletion,
389 relocate by number of chars / bytes deleted. */
392 m
->charpos
-= to
- from
;
393 m
->bytepos
-= to_byte
- from_byte
;
396 /* Here's the case where a marker is inside text being deleted. */
397 else if (charpos
> from
)
399 record_marker_adjustment (marker
, from
- charpos
);
401 m
->bytepos
= from_byte
;
409 /* Adjust all markers for calling record_delete for combining bytes.
410 whose range in bytes is FROM_BYTE to TO_BYTE.
411 The range in charpos is FROM to TO. */
414 adjust_markers_for_record_delete (from
, from_byte
, to
, to_byte
)
415 register int from
, from_byte
, to
, to_byte
;
418 register struct Lisp_Marker
*m
;
419 register int charpos
;
421 marker
= BUF_MARKERS (current_buffer
);
423 while (!NILP (marker
))
425 m
= XMARKER (marker
);
426 charpos
= m
->charpos
;
428 /* If the marker is after the deletion,
429 relocate by number of chars / bytes deleted. */
432 /* Here's the case where a marker is inside text being deleted. */
433 else if (charpos
> from
)
434 record_marker_adjustment (marker
, from
- charpos
);
440 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
441 to TO / TO_BYTE. We have to relocate the charpos of every marker
442 that points after the insertion (but not their bytepos).
444 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
445 that combine into one character with the text before the insertion.
446 COMBINED_AFTER_BYTES is the number of bytes after the insertion
447 that combine into one character with the last inserted bytes.
449 When a marker points at the insertion point,
450 we advance it if either its insertion-type is t
451 or BEFORE_MARKERS is true. */
454 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
,
455 combined_before_bytes
, combined_after_bytes
,
457 register int from
, from_byte
, to
, to_byte
;
458 int combined_before_bytes
, combined_after_bytes
, before_markers
;
462 int nchars
= to
- from
;
463 int nbytes
= to_byte
- from_byte
;
465 marker
= BUF_MARKERS (current_buffer
);
467 while (!NILP (marker
))
469 register struct Lisp_Marker
*m
= XMARKER (marker
);
471 /* In a single-byte buffer, a marker's two positions must be equal.
472 (If this insertion is going to combine characters, Z will
473 become different from Z_BYTE, but they might be the same now.
474 If so, the two OLD positions of the marker should be equal.) */
477 if (m
->charpos
!= m
->bytepos
)
481 if (m
->bytepos
== from_byte
)
483 if (m
->insertion_type
|| before_markers
)
485 m
->bytepos
= to_byte
+ combined_after_bytes
;
486 m
->charpos
= to
- combined_before_bytes
;
487 /* Point the marker before the combined character,
488 so that undoing the insertion puts it back where it was. */
489 if (combined_after_bytes
)
490 DEC_BOTH (m
->charpos
, m
->bytepos
);
491 if (m
->insertion_type
)
494 else if (combined_before_bytes
)
496 /* This marker doesn't "need relocation",
497 but don't leave it pointing in the middle of a character.
498 Point the marker after the combined character,
499 so that undoing the insertion puts it back where it was. */
500 m
->bytepos
+= combined_before_bytes
;
501 if (combined_before_bytes
== nbytes
)
502 /* All new bytes plus combined_after_bytes (if any)
504 m
->bytepos
+= combined_after_bytes
;
507 /* If a marker was pointing into the combining bytes
508 after the insertion, don't leave it there
509 in the middle of a character. */
510 else if (combined_after_bytes
&& m
->bytepos
>= from_byte
511 && m
->bytepos
< from_byte
+ combined_after_bytes
)
513 /* Put it after the combining bytes. */
514 m
->bytepos
= to_byte
+ combined_after_bytes
;
515 m
->charpos
= to
- combined_before_bytes
;
516 /* Now move it back before the combined character,
517 so that undoing the insertion will put it where it was. */
518 DEC_BOTH (m
->charpos
, m
->bytepos
);
520 else if (m
->bytepos
> from_byte
)
522 m
->bytepos
+= nbytes
;
523 m
->charpos
+= nchars
- combined_after_bytes
- combined_before_bytes
;
529 /* Adjusting only markers whose insertion-type is t may result in
530 disordered overlays in the slot `overlays_before'. */
532 fix_overlays_before (current_buffer
, from
, to
);
535 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
537 This is used only when the value of point changes due to an insert
538 or delete; it does not represent a conceptual change in point as a
539 marker. In particular, point is not crossing any interval
540 boundaries, so there's no need to use the usual SET_PT macro. In
541 fact it would be incorrect to do so, because either the old or the
542 new value of point is out of sync with the current set of
546 adjust_point (nchars
, nbytes
)
549 BUF_PT (current_buffer
) += nchars
;
550 BUF_PT_BYTE (current_buffer
) += nbytes
;
552 /* In a single-byte buffer, the two positions must be equal. */
558 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
559 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
562 See the comment of adjust_markers_for_insert for the args
563 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES. */
566 adjust_markers_for_replace (from
, from_byte
, old_chars
, old_bytes
,
567 new_chars
, new_bytes
,
568 combined_before_bytes
, combined_after_bytes
)
569 int from
, from_byte
, old_chars
, old_bytes
, new_chars
, new_bytes
;
570 int combined_before_bytes
, combined_after_bytes
;
572 Lisp_Object marker
= BUF_MARKERS (current_buffer
);
573 int prev_to_byte
= from_byte
+ old_bytes
;
575 = (new_chars
- combined_before_bytes
) - (old_chars
+ combined_after_bytes
);
576 int diff_bytes
= new_bytes
- old_bytes
;
578 while (!NILP (marker
))
580 register struct Lisp_Marker
*m
= XMARKER (marker
);
582 if (m
->bytepos
>= prev_to_byte
584 /* If this is an insertion (replacing 0 chars),
585 reject the case of a marker that is at the
586 insertion point and should stay before the insertion. */
587 || m
->bytepos
> from_byte
|| m
->insertion_type
))
589 if (m
->bytepos
< prev_to_byte
+ combined_after_bytes
)
591 /* Put it after the combining bytes. */
592 m
->bytepos
= from_byte
+ new_bytes
+ combined_after_bytes
;
593 m
->charpos
= from
+ new_chars
- combined_before_bytes
;
597 m
->charpos
+= diff_chars
;
598 m
->bytepos
+= diff_bytes
;
601 else if (m
->bytepos
>= from_byte
)
604 m
->bytepos
= from_byte
+ combined_before_bytes
;
605 /* If all new bytes are combined in addition to that there
606 are after combining bytes, we must set byte position of
607 the marker after the after combining bytes. */
608 if (combined_before_bytes
== new_bytes
)
609 m
->bytepos
+= combined_after_bytes
;
619 /* Make the gap NBYTES_ADDED bytes longer. */
622 make_gap (nbytes_added
)
625 unsigned char *result
;
628 int real_gap_loc_byte
;
631 /* If we have to get more space, get enough to last a while. */
632 nbytes_added
+= 2000;
634 /* Don't allow a buffer size that won't fit in an int
635 even if it will fit in a Lisp integer.
636 That won't work because so many places use `int'. */
638 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
639 >= ((unsigned) 1 << (min (BITS_PER_INT
, VALBITS
) - 1)))
640 error ("Buffer exceeds maximum size");
643 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
644 result
= BUFFER_REALLOC (BEG_ADDR
, (Z_BYTE
- BEG_BYTE
645 + GAP_SIZE
+ nbytes_added
+ 1));
653 /* We can't unblock until the new address is properly stored. */
657 /* Prevent quitting in move_gap. */
662 real_gap_loc_byte
= GPT_BYTE
;
663 old_gap_size
= GAP_SIZE
;
665 /* Call the newly allocated space a gap at the end of the whole space. */
667 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
668 GAP_SIZE
= nbytes_added
;
670 /* Move the new gap down to be consecutive with the end of the old one.
671 This adjusts the markers properly too. */
672 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
674 /* Now combine the two into one large gap. */
675 GAP_SIZE
+= old_gap_size
;
677 GPT_BYTE
= real_gap_loc_byte
;
685 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
686 FROM_MULTIBYTE says whether the incoming text is multibyte.
687 TO_MULTIBYTE says whether to store the text as multibyte.
688 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
690 Return the number of bytes stored at TO_ADDR. */
693 copy_text (from_addr
, to_addr
, nbytes
,
694 from_multibyte
, to_multibyte
)
695 unsigned char *from_addr
;
696 unsigned char *to_addr
;
698 int from_multibyte
, to_multibyte
;
700 if (from_multibyte
== to_multibyte
)
702 bcopy (from_addr
, to_addr
, nbytes
);
705 else if (from_multibyte
)
708 int bytes_left
= nbytes
;
709 Lisp_Object tbl
= Qnil
, temp
;
711 /* We set the variable tbl to the reverse table of
712 Vnonascii_translation_table in advance. */
713 if (CHAR_TABLE_P (Vnonascii_translation_table
))
715 tbl
= Fchar_table_extra_slot (Vnonascii_translation_table
,
717 if (!CHAR_TABLE_P (tbl
))
721 /* Convert multibyte to single byte. */
722 while (bytes_left
> 0)
724 int thislen
, c
, c_save
;
725 c
= c_save
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
726 if (!SINGLE_BYTE_CHAR_P (c
))
727 c
= multibyte_char_to_unibyte (c
, tbl
);
729 from_addr
+= thislen
;
730 bytes_left
-= thislen
;
737 unsigned char *initial_to_addr
= to_addr
;
739 /* Convert single-byte to multibyte. */
742 int c
= *from_addr
++;
743 unsigned char workbuf
[4], *str
;
748 || (c
>= 0200 && !NILP (Vnonascii_translation_table
))))
750 c
= unibyte_char_to_multibyte (c
);
751 len
= CHAR_STRING (c
, workbuf
, str
);
752 bcopy (str
, to_addr
, len
);
757 /* Special case for speed. */
758 *to_addr
++ = c
, nbytes
--;
760 return to_addr
- initial_to_addr
;
764 /* Return the number of bytes it would take
765 to convert some single-byte text to multibyte.
766 The single-byte text consists of NBYTES bytes at PTR. */
769 count_size_as_multibyte (ptr
, nbytes
)
774 int outgoing_nbytes
= 0;
776 for (i
= 0; i
< nbytes
; i
++)
778 unsigned int c
= *ptr
++;
780 if (c
< 0200 || (c
< 0240 && NILP (Vnonascii_translation_table
)))
784 c
= unibyte_char_to_multibyte (c
);
785 outgoing_nbytes
+= CHAR_BYTES (c
);
789 return outgoing_nbytes
;
792 /* Insert a string of specified length before point.
793 This function judges multibyteness based on
794 enable_multibyte_characters in the current buffer;
795 it never converts between single-byte and multibyte.
797 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
798 prepare_to_modify_buffer could relocate the text. */
801 insert (string
, nbytes
)
802 register unsigned char *string
;
808 insert_1 (string
, nbytes
, 0, 1, 0);
809 signal_after_change (opoint
, 0, PT
- opoint
);
813 /* Likewise, but inherit text properties from neighboring characters. */
816 insert_and_inherit (string
, nbytes
)
817 register unsigned char *string
;
823 insert_1 (string
, nbytes
, 1, 1, 0);
824 signal_after_change (opoint
, 0, PT
- opoint
);
828 /* Insert the character C before point. Do not inherit text properties. */
834 unsigned char workbuf
[4], *str
;
837 if (! NILP (current_buffer
->enable_multibyte_characters
))
838 len
= CHAR_STRING (c
, workbuf
, str
);
849 /* Insert the null-terminated string S before point. */
855 insert (s
, strlen (s
));
858 /* Like `insert' except that all markers pointing at the place where
859 the insertion happens are adjusted to point after it.
860 Don't use this function to insert part of a Lisp string,
861 since gc could happen and relocate it. */
864 insert_before_markers (string
, nbytes
)
865 unsigned char *string
;
872 insert_1 (string
, nbytes
, 0, 1, 1);
873 signal_after_change (opoint
, 0, PT
- opoint
);
877 /* Likewise, but inherit text properties from neighboring characters. */
880 insert_before_markers_and_inherit (string
, nbytes
)
881 unsigned char *string
;
888 insert_1 (string
, nbytes
, 1, 1, 1);
889 signal_after_change (opoint
, 0, PT
- opoint
);
893 /* Subroutine used by the insert functions above. */
896 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
897 register unsigned char *string
;
899 int inherit
, prepare
, before_markers
;
901 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
902 inherit
, prepare
, before_markers
);
905 /* See if the bytes before POS/POS_BYTE combine with bytes
906 at the start of STRING to form a single character.
907 If so, return the number of bytes at the start of STRING
908 which combine in this way. Otherwise, return 0. */
911 count_combining_before (string
, length
, pos
, pos_byte
)
912 unsigned char *string
;
916 int opos
= pos
, opos_byte
= pos_byte
;
918 unsigned char *p
= string
;
920 if (NILP (current_buffer
->enable_multibyte_characters
))
922 if (length
== 0 || CHAR_HEAD_P (*string
))
926 c
= FETCH_BYTE (pos_byte
- 1);
927 if (ASCII_BYTE_P (c
))
929 DEC_BOTH (pos
, pos_byte
);
930 c
= FETCH_BYTE (pos_byte
);
931 if (! BASE_LEADING_CODE_P (c
))
934 /* We have a combination situation.
935 Count the bytes at STRING that will combine. */
936 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
942 /* See if the bytes after POS/POS_BYTE combine with bytes
943 at the end of STRING to form a single character.
944 If so, return the number of bytes after POS/POS_BYTE
945 which combine in this way. Otherwise, return 0. */
948 count_combining_after (string
, length
, pos
, pos_byte
)
949 unsigned char *string
;
953 int opos
= pos
, opos_byte
= pos_byte
;
957 if (NILP (current_buffer
->enable_multibyte_characters
))
959 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1]))
962 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
968 /* All characters in `string' are not character head.
969 We must check also preceding bytes at POS.
970 We are sure that the gap is at POS. */
973 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
975 if (i
< 0 || !BASE_LEADING_CODE_P (string
[i
]))
978 else if (!BASE_LEADING_CODE_P (string
[i
]))
983 c
= FETCH_BYTE (pos_byte
);
986 while (pos_byte
< Z_BYTE
)
988 c
= FETCH_BYTE (pos_byte
);
994 return pos_byte
- opos_byte
;
997 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
998 following the position POS/POS_BYTE to the character preceding POS.
999 If TARGET is after POS+NBYTES, we only have to adjust the character
1000 position TARGET, else, if TARGET is after POS, we have to adjust
1001 both the character position TARGET and the byte position
1002 TARGET_BYTE, else we don't have to do any adjustment. */
1004 #define ADJUST_CHAR_POS(target, target_byte) \
1006 if (target > pos + nbytes) \
1008 else if (target >= pos) \
1011 target_byte = pos_byte + nbytes; \
1015 /* Combine NBYTES stray trailing-codes, which were formerly separate
1016 characters, with the preceding character. These bytes
1017 are located after position POS / POS_BYTE, and the preceding character
1018 is located just before that position.
1020 This function does not adjust markers for byte combining. That
1021 should be done in advance by the functions
1022 adjust_markers_for_insert or adjust_markers_for_replace. */
1025 combine_bytes (pos
, pos_byte
, nbytes
)
1026 int pos
, pos_byte
, nbytes
;
1028 adjust_overlays_for_delete (pos
, nbytes
);
1030 ADJUST_CHAR_POS (BUF_PT (current_buffer
), BUF_PT_BYTE (current_buffer
));
1031 ADJUST_CHAR_POS (GPT
, GPT_BYTE
);
1032 ADJUST_CHAR_POS (Z
, Z_BYTE
);
1033 ADJUST_CHAR_POS (ZV
, ZV_BYTE
);
1035 if (BUF_INTERVALS (current_buffer
) != 0)
1036 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1037 offset_intervals (current_buffer
, pos
, - nbytes
);
1041 byte_combining_error ()
1043 error ("Byte combining across boundary of accessible buffer text inhibitted");
1046 /* If we are going to combine bytes at POS which is at a narrowed
1047 region boundary, signal an error. */
1048 #define CHECK_BYTE_COMBINING_FOR_INSERT(pos) \
1050 if (combined_before_bytes && pos == BEGV \
1051 || combined_after_bytes && pos == ZV) \
1052 byte_combining_error (); \
1056 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1057 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
1058 are the same as in insert_1. */
1061 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
1062 register unsigned char *string
;
1063 register int nchars
, nbytes
;
1064 int inherit
, prepare
, before_markers
;
1066 register Lisp_Object temp
;
1067 int combined_before_bytes
, combined_after_bytes
;
1069 if (NILP (current_buffer
->enable_multibyte_characters
))
1073 /* Do this before moving and increasing the gap,
1074 because the before-change hooks might move the gap
1075 or make it smaller. */
1076 prepare_to_modify_buffer (PT
, PT
, NULL
);
1079 move_gap_both (PT
, PT_BYTE
);
1080 if (GAP_SIZE
< nbytes
)
1081 make_gap (nbytes
- GAP_SIZE
);
1083 combined_before_bytes
1084 = count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
1085 combined_after_bytes
1086 = count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
1087 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1089 /* Record deletion of the surrounding text that combines with
1090 the insertion. This, together with recording the insertion,
1091 will add up to the right stuff in the undo list.
1093 But there is no need to actually delete the combining bytes
1094 from the buffer and reinsert them. */
1096 if (combined_after_bytes
)
1098 Lisp_Object deletion
;
1101 if (! EQ (current_buffer
->undo_list
, Qt
))
1102 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1103 PT
+ combined_after_bytes
,
1104 PT_BYTE
+ combined_after_bytes
, 1);
1106 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1107 PT
+ combined_after_bytes
,
1108 PT_BYTE
+ combined_after_bytes
);
1109 if (! EQ (current_buffer
->undo_list
, Qt
))
1110 record_delete (PT
, deletion
);
1113 if (combined_before_bytes
)
1115 Lisp_Object deletion
;
1118 if (! EQ (current_buffer
->undo_list
, Qt
))
1119 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1121 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1123 if (! EQ (current_buffer
->undo_list
, Qt
))
1124 record_delete (PT
- 1, deletion
);
1127 record_insert (PT
- !!combined_before_bytes
,
1128 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1131 bcopy (string
, GPT_ADDR
, nbytes
);
1134 /* When we have combining at the end of the insertion,
1135 this is the character position before the combined character. */
1142 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1144 if (combined_after_bytes
)
1145 move_gap_both (GPT
+ combined_after_bytes
,
1146 GPT_BYTE
+ combined_after_bytes
);
1151 adjust_overlays_for_insert (PT
, nchars
);
1152 adjust_markers_for_insert (PT
, PT_BYTE
,
1153 PT
+ nchars
, PT_BYTE
+ nbytes
,
1154 combined_before_bytes
, combined_after_bytes
,
1157 #ifdef USE_TEXT_PROPERTIES
1158 if (BUF_INTERVALS (current_buffer
) != 0)
1159 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1160 offset_intervals (current_buffer
, PT
, nchars
);
1162 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1163 Fset_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1168 int pos
= PT
, pos_byte
= PT_BYTE
;
1170 adjust_point (nchars
+ combined_after_bytes
,
1171 nbytes
+ combined_after_bytes
);
1173 if (combined_after_bytes
)
1174 combine_bytes (pos
+ nchars
, pos_byte
+ nbytes
, combined_after_bytes
);
1176 if (combined_before_bytes
)
1177 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1183 /* Insert the part of the text of STRING, a Lisp object assumed to be
1184 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1185 starting at position POS / POS_BYTE. If the text of STRING has properties,
1186 copy them into the buffer.
1188 It does not work to use `insert' for this, because a GC could happen
1189 before we bcopy the stuff into the buffer, and relocate the string
1190 without insert noticing. */
1193 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1195 register int pos
, pos_byte
, length
, length_byte
;
1199 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1201 signal_after_change (opoint
, 0, PT
- opoint
);
1204 /* Like `insert_from_string' except that all markers pointing
1205 at the place where the insertion happens are adjusted to point after it. */
1208 insert_from_string_before_markers (string
, pos
, pos_byte
,
1209 length
, length_byte
, inherit
)
1211 register int pos
, pos_byte
, length
, length_byte
;
1215 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1217 signal_after_change (opoint
, 0, PT
- opoint
);
1220 /* Subroutine of the insertion functions above. */
1223 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1224 inherit
, before_markers
)
1226 register int pos
, pos_byte
, nchars
, nbytes
;
1227 int inherit
, before_markers
;
1229 register Lisp_Object temp
;
1230 struct gcpro gcpro1
;
1231 int outgoing_nbytes
= nbytes
;
1232 int combined_before_bytes
, combined_after_bytes
;
1233 int adjusted_nchars
;
1236 /* Make OUTGOING_NBYTES describe the text
1237 as it will be inserted in this buffer. */
1239 if (NILP (current_buffer
->enable_multibyte_characters
))
1240 outgoing_nbytes
= nchars
;
1241 else if (! STRING_MULTIBYTE (string
))
1243 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1247 /* Do this before moving and increasing the gap,
1248 because the before-change hooks might move the gap
1249 or make it smaller. */
1250 prepare_to_modify_buffer (PT
, PT
, NULL
);
1253 move_gap_both (PT
, PT_BYTE
);
1254 if (GAP_SIZE
< outgoing_nbytes
)
1255 make_gap (outgoing_nbytes
- GAP_SIZE
);
1258 /* Copy the string text into the buffer, perhaps converting
1259 between single-byte and multibyte. */
1260 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1261 STRING_MULTIBYTE (string
),
1262 ! NILP (current_buffer
->enable_multibyte_characters
));
1264 /* We have copied text into the gap, but we have not altered
1265 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1266 to these functions and get the same results as we would
1267 have got earlier on. Meanwhile, PT_ADDR does point to
1268 the text that has been stored by copy_text. */
1270 combined_before_bytes
1271 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1272 combined_after_bytes
1273 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1275 unsigned char save
= *(GPT_ADDR
);
1277 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1281 /* Record deletion of the surrounding text that combines with
1282 the insertion. This, together with recording the insertion,
1283 will add up to the right stuff in the undo list.
1285 But there is no need to actually delete the combining bytes
1286 from the buffer and reinsert them. */
1288 if (combined_after_bytes
)
1290 Lisp_Object deletion
;
1293 if (! EQ (current_buffer
->undo_list
, Qt
))
1294 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1295 PT
+ combined_after_bytes
,
1296 PT_BYTE
+ combined_after_bytes
, 1);
1298 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1299 PT
+ combined_after_bytes
,
1300 PT_BYTE
+ combined_after_bytes
);
1301 if (! EQ (current_buffer
->undo_list
, Qt
))
1302 record_delete (PT
, deletion
);
1305 if (combined_before_bytes
)
1307 Lisp_Object deletion
;
1310 if (! EQ (current_buffer
->undo_list
, Qt
))
1311 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1313 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1315 if (! EQ (current_buffer
->undo_list
, Qt
))
1316 record_delete (PT
- 1, deletion
);
1319 record_insert (PT
- !!combined_before_bytes
,
1320 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1323 GAP_SIZE
-= outgoing_nbytes
;
1327 GPT_BYTE
+= outgoing_nbytes
;
1328 ZV_BYTE
+= outgoing_nbytes
;
1329 Z_BYTE
+= outgoing_nbytes
;
1330 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1332 if (combined_after_bytes
)
1333 move_gap_both (GPT
+ combined_after_bytes
,
1334 GPT_BYTE
+ combined_after_bytes
);
1339 adjust_overlays_for_insert (PT
, nchars
);
1340 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1341 PT_BYTE
+ outgoing_nbytes
,
1342 combined_before_bytes
, combined_after_bytes
,
1345 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1346 offset_intervals (current_buffer
, PT
, nchars
);
1348 intervals
= XSTRING (string
)->intervals
;
1349 /* Get the intervals for the part of the string we are inserting--
1350 not including the combined-before bytes. */
1351 if (nbytes
< STRING_BYTES (XSTRING (string
)))
1352 intervals
= copy_intervals (intervals
, pos
, nchars
);
1354 /* Insert those intervals. */
1355 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1356 current_buffer
, inherit
);
1359 int pos
= PT
, pos_byte
= PT_BYTE
;
1361 adjust_point (nchars
+ combined_after_bytes
,
1362 outgoing_nbytes
+ combined_after_bytes
);
1364 if (combined_after_bytes
)
1365 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1366 combined_after_bytes
);
1368 if (combined_before_bytes
)
1369 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1373 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1374 current buffer. If the text in BUF has properties, they are absorbed
1375 into the current buffer.
1377 It does not work to use `insert' for this, because a malloc could happen
1378 and relocate BUF's text before the bcopy happens. */
1381 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1383 int charpos
, nchars
;
1388 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1389 signal_after_change (opoint
, 0, PT
- opoint
);
1393 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1398 register Lisp_Object temp
;
1399 int chunk
, chunk_expanded
;
1400 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1401 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1402 int incoming_nbytes
= to_byte
- from_byte
;
1403 int outgoing_nbytes
= incoming_nbytes
;
1404 int combined_before_bytes
, combined_after_bytes
;
1405 int adjusted_nchars
;
1408 /* Make OUTGOING_NBYTES describe the text
1409 as it will be inserted in this buffer. */
1411 if (NILP (current_buffer
->enable_multibyte_characters
))
1412 outgoing_nbytes
= nchars
;
1413 else if (NILP (buf
->enable_multibyte_characters
))
1415 int outgoing_before_gap
= 0;
1416 int outgoing_after_gap
= 0;
1418 if (from
< BUF_GPT (buf
))
1420 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1421 if (chunk
> incoming_nbytes
)
1422 chunk
= incoming_nbytes
;
1424 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1430 if (chunk
< incoming_nbytes
)
1432 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1434 incoming_nbytes
- chunk
);
1436 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1439 /* Make sure point-max won't overflow after this insertion. */
1440 XSETINT (temp
, outgoing_nbytes
+ Z
);
1441 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1442 error ("Maximum buffer size exceeded");
1444 /* Do this before moving and increasing the gap,
1445 because the before-change hooks might move the gap
1446 or make it smaller. */
1447 prepare_to_modify_buffer (PT
, PT
, NULL
);
1450 move_gap_both (PT
, PT_BYTE
);
1451 if (GAP_SIZE
< outgoing_nbytes
)
1452 make_gap (outgoing_nbytes
- GAP_SIZE
);
1454 if (from
< BUF_GPT (buf
))
1456 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1457 if (chunk
> incoming_nbytes
)
1458 chunk
= incoming_nbytes
;
1459 /* Record number of output bytes, so we know where
1460 to put the output from the second copy_text. */
1462 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1464 ! NILP (buf
->enable_multibyte_characters
),
1465 ! NILP (current_buffer
->enable_multibyte_characters
));
1468 chunk_expanded
= chunk
= 0;
1470 if (chunk
< incoming_nbytes
)
1471 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1472 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1473 ! NILP (buf
->enable_multibyte_characters
),
1474 ! NILP (current_buffer
->enable_multibyte_characters
));
1476 /* We have copied text into the gap, but we have not altered
1477 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1478 to these functions and get the same results as we would
1479 have got earlier on. Meanwhile, GPT_ADDR does point to
1480 the text that has been stored by copy_text. */
1481 combined_before_bytes
1482 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1483 combined_after_bytes
1484 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1486 unsigned char save
= *(GPT_ADDR
);
1488 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1492 /* Record deletion of the surrounding text that combines with
1493 the insertion. This, together with recording the insertion,
1494 will add up to the right stuff in the undo list.
1496 But there is no need to actually delete the combining bytes
1497 from the buffer and reinsert them. */
1499 if (combined_after_bytes
)
1501 Lisp_Object deletion
;
1504 if (! EQ (current_buffer
->undo_list
, Qt
))
1505 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1506 PT
+ combined_after_bytes
,
1507 PT_BYTE
+ combined_after_bytes
, 1);
1509 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1510 PT
+ combined_after_bytes
,
1511 PT_BYTE
+ combined_after_bytes
);
1512 if (! EQ (current_buffer
->undo_list
, Qt
))
1513 record_delete (PT
, deletion
);
1516 if (combined_before_bytes
)
1518 Lisp_Object deletion
;
1521 if (! EQ (current_buffer
->undo_list
, Qt
))
1522 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1524 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1526 if (! EQ (current_buffer
->undo_list
, Qt
))
1527 record_delete (PT
- 1, deletion
);
1530 record_insert (PT
- !!combined_before_bytes
,
1531 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1534 GAP_SIZE
-= outgoing_nbytes
;
1538 GPT_BYTE
+= outgoing_nbytes
;
1539 ZV_BYTE
+= outgoing_nbytes
;
1540 Z_BYTE
+= outgoing_nbytes
;
1541 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1543 if (combined_after_bytes
)
1544 move_gap_both (GPT
+ combined_after_bytes
,
1545 GPT_BYTE
+ combined_after_bytes
);
1550 adjust_overlays_for_insert (PT
, nchars
);
1551 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1552 PT_BYTE
+ outgoing_nbytes
,
1553 combined_before_bytes
, combined_after_bytes
, 0);
1555 #ifdef USE_TEXT_PROPERTIES
1556 if (BUF_INTERVALS (current_buffer
) != 0)
1557 offset_intervals (current_buffer
, PT
, nchars
);
1560 /* Get the intervals for the part of the string we are inserting--
1561 not including the combined-before bytes. */
1562 intervals
= BUF_INTERVALS (buf
);
1563 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1564 intervals
= copy_intervals (intervals
, from
, nchars
);
1566 /* Insert those intervals. */
1567 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1570 int pos
= PT
, pos_byte
= PT_BYTE
;
1572 adjust_point (nchars
+ combined_after_bytes
,
1573 outgoing_nbytes
+ combined_after_bytes
);
1575 if (combined_after_bytes
)
1576 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1577 combined_after_bytes
);
1579 if (combined_before_bytes
)
1580 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1584 /* This function should be called after moving gap to FROM and before
1585 altering text between FROM and TO. This adjusts various position
1586 keepers and markers as if the text is deleted. Don't forget to
1587 call adjust_after_replace after you actually alter the text. */
1590 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1591 int from
, from_byte
, to
, to_byte
;
1593 Lisp_Object deletion
;
1595 if (! EQ (current_buffer
->undo_list
, Qt
))
1596 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1600 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1602 if (! EQ (current_buffer
->undo_list
, Qt
))
1603 record_delete (from
, deletion
);
1605 adjust_overlays_for_delete (from
, to
- from
);
1608 /* Record undo information and adjust markers and position keepers for
1609 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1610 chars (LEN_BYTE bytes) which resides in the gap just after
1613 PREV_TEXT nil means the new text was just inserted. */
1616 adjust_after_replace (from
, from_byte
, prev_text
, len
, len_byte
)
1617 int from
, from_byte
, len
, len_byte
;
1618 Lisp_Object prev_text
;
1620 int combined_before_bytes
1621 = count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
);
1622 int combined_after_bytes
1623 = count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
);
1624 /* This flag tells if we combine some bytes with a character before
1625 FROM. This happens even if combined_before_bytes is zero. */
1626 int combine_before
= (combined_before_bytes
1627 || (len
== 0 && combined_after_bytes
));
1629 int nchars_del
= 0, nbytes_del
= 0;
1631 if (STRINGP (prev_text
))
1633 nchars_del
= XSTRING (prev_text
)->size
;
1634 nbytes_del
= STRING_BYTES (XSTRING (prev_text
));
1637 if (combine_before
&& from
== BEGV
1638 || combined_after_bytes
&& from
== ZV
)
1640 /* We can't combine bytes nor signal an error here. So, let's
1641 pretend that the new text is just a single space. */
1643 combined_before_bytes
= combined_after_bytes
= 0;
1647 if (combined_after_bytes
)
1649 Lisp_Object deletion
;
1652 if (! EQ (current_buffer
->undo_list
, Qt
))
1653 deletion
= make_buffer_string_both (from
, from_byte
,
1654 from
+ combined_after_bytes
,
1655 from_byte
+ combined_after_bytes
,
1658 adjust_markers_for_record_delete (from
, from_byte
,
1659 from
+ combined_after_bytes
,
1660 from_byte
+ combined_after_bytes
);
1662 if (! EQ (current_buffer
->undo_list
, Qt
))
1663 record_delete (from
+ nchars_del
, deletion
);
1666 if (combined_before_bytes
1667 || len_byte
== 0 && combined_after_bytes
> 0)
1669 Lisp_Object deletion
;
1672 if (! EQ (current_buffer
->undo_list
, Qt
))
1673 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1674 from
, from_byte
, 1);
1675 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1677 if (! EQ (current_buffer
->undo_list
, Qt
))
1678 record_delete (from
- 1, deletion
);
1681 /* Update various buffer positions for the new text. */
1682 GAP_SIZE
-= len_byte
;
1684 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1685 GPT
+= len
; GPT_BYTE
+= len_byte
;
1686 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1688 /* The gap should be at character boundary. */
1689 if (combined_after_bytes
)
1690 move_gap_both (GPT
+ combined_after_bytes
,
1691 GPT_BYTE
+ combined_after_bytes
);
1693 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1695 combined_before_bytes
, combined_after_bytes
);
1696 if (! EQ (current_buffer
->undo_list
, Qt
))
1699 record_delete (from
- combine_before
, prev_text
);
1701 record_insert (from
- 1, len
- combined_before_bytes
+ 1);
1703 record_insert (from
, len
);
1706 if (len
> nchars_del
)
1707 adjust_overlays_for_insert (from
, len
- nchars_del
);
1708 else if (len
< nchars_del
)
1709 adjust_overlays_for_delete (from
, nchars_del
- len
);
1710 #ifdef USE_TEXT_PROPERTIES
1711 if (BUF_INTERVALS (current_buffer
) != 0)
1713 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1718 int pos
= PT
, pos_byte
= PT_BYTE
;
1721 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1723 if (combined_after_bytes
)
1725 if (combined_before_bytes
== len_byte
)
1726 /* This is the case that all new bytes are combined. */
1727 combined_before_bytes
+= combined_after_bytes
;
1729 combine_bytes (from
+ len
, from_byte
+ len_byte
,
1730 combined_after_bytes
);
1732 if (combined_before_bytes
)
1733 combine_bytes (from
, from_byte
, combined_before_bytes
);
1736 /* As byte combining will decrease Z, we must check this again. */
1737 if (Z
- GPT
< end_unchanged
)
1738 end_unchanged
= Z
- GPT
;
1743 evaporate_overlays (from
);
1747 /* Record undo information, adjust markers and position keepers for an
1748 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1749 text already exists in the current buffer but character length (TO
1750 - FROM) may be incorrect, the correct length is NEWLEN. */
1753 adjust_after_insert (from
, from_byte
, to
, to_byte
, newlen
)
1754 int from
, from_byte
, to
, to_byte
, newlen
;
1756 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
1759 move_gap_both (to
, to_byte
);
1760 GAP_SIZE
+= len_byte
;
1761 GPT
-= len
; GPT_BYTE
-= len_byte
;
1762 ZV
-= len
; ZV_BYTE
-= len_byte
;
1763 Z
-= len
; Z_BYTE
-= len_byte
;
1764 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1767 /* Replace the text from character positions FROM to TO with NEW,
1768 If PREPARE is nonzero, call prepare_to_modify_buffer.
1769 If INHERIT, the newly inserted text should inherit text properties
1770 from the surrounding non-deleted text. */
1772 /* Note that this does not yet handle markers quite right.
1773 Also it needs to record a single undo-entry that does a replacement
1774 rather than a separate delete and insert.
1775 That way, undo will also handle markers properly.
1777 But if MARKERS is 0, don't relocate markers. */
1780 replace_range (from
, to
, new, prepare
, inherit
, markers
)
1782 int from
, to
, prepare
, inherit
, markers
;
1784 int inschars
= XSTRING (new)->size
;
1785 int insbytes
= STRING_BYTES (XSTRING (new));
1786 int from_byte
, to_byte
;
1787 int nbytes_del
, nchars_del
;
1788 register Lisp_Object temp
;
1789 struct gcpro gcpro1
;
1790 int combined_before_bytes
, combined_after_bytes
;
1791 int adjusted_inschars
;
1793 int outgoing_insbytes
= insbytes
;
1794 Lisp_Object deletion
;
1802 int range_length
= to
- from
;
1803 prepare_to_modify_buffer (from
, to
, &from
);
1804 to
= from
+ range_length
;
1809 /* Make args be valid */
1815 from_byte
= CHAR_TO_BYTE (from
);
1816 to_byte
= CHAR_TO_BYTE (to
);
1818 nchars_del
= to
- from
;
1819 nbytes_del
= to_byte
- from_byte
;
1821 if (nbytes_del
<= 0 && insbytes
== 0)
1824 /* Make OUTGOING_INSBYTES describe the text
1825 as it will be inserted in this buffer. */
1827 if (NILP (current_buffer
->enable_multibyte_characters
))
1828 outgoing_insbytes
= inschars
;
1829 else if (! STRING_MULTIBYTE (new))
1831 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1833 /* Make sure point-max won't overflow after this insertion. */
1834 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1835 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1836 error ("Maximum buffer size exceeded");
1840 /* Make sure the gap is somewhere in or next to what we are deleting. */
1842 gap_right (from
, from_byte
);
1844 gap_left (to
, to_byte
, 0);
1846 /* Even if we don't record for undo, we must keep the original text
1847 because we may have to recover it because of inappropriate byte
1849 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1852 /* Relocate all markers pointing into the new, larger gap
1853 to point at the end of the text before the gap.
1854 Do this before recording the deletion,
1855 so that undo handles this after reinserting the text. */
1856 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1858 GAP_SIZE
+= nbytes_del
;
1861 ZV_BYTE
-= nbytes_del
;
1862 Z_BYTE
-= nbytes_del
;
1864 GPT_BYTE
= from_byte
;
1865 *(GPT_ADDR
) = 0; /* Put an anchor. */
1870 if (GPT
- BEG
< beg_unchanged
)
1871 beg_unchanged
= GPT
- BEG
;
1872 if (Z
- GPT
< end_unchanged
)
1873 end_unchanged
= Z
- GPT
;
1875 if (GAP_SIZE
< insbytes
)
1876 make_gap (insbytes
- GAP_SIZE
);
1878 /* Copy the string text into the buffer, perhaps converting
1879 between single-byte and multibyte. */
1880 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1881 STRING_MULTIBYTE (new),
1882 ! NILP (current_buffer
->enable_multibyte_characters
));
1884 /* We have copied text into the gap, but we have not marked
1885 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1886 here, for both the previous text and the following text.
1887 Meanwhile, GPT_ADDR does point to
1888 the text that has been stored by copy_text. */
1890 combined_before_bytes
1891 = count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1892 combined_after_bytes
1893 = count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1895 if (combined_before_bytes
&& from
== BEGV
1896 || combined_after_bytes
&& from
== ZV
)
1898 /* Bytes are being combined across the region boundary. We
1899 should avoid it. We recover the original contents before
1900 signaling an error. */
1901 bcopy (XSTRING (deletion
)->data
, GPT_ADDR
, nbytes_del
);
1902 GAP_SIZE
-= nbytes_del
;
1905 ZV_BYTE
+= nbytes_del
;
1906 Z_BYTE
+= nbytes_del
;
1907 GPT
= from
+ nchars_del
;
1908 GPT_BYTE
= from_byte
+ nbytes_del
;
1909 *(GPT_ADDR
) = 0; /* Put an anchor. */
1911 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
, 0, 0, 0);
1913 byte_combining_error ();
1917 /* Record deletion of the surrounding text that combines with
1918 the insertion. This, together with recording the insertion,
1919 will add up to the right stuff in the undo list.
1921 But there is no need to actually delete the combining bytes
1922 from the buffer and reinsert them. */
1924 if (combined_after_bytes
)
1926 Lisp_Object deletion
;
1929 if (! EQ (current_buffer
->undo_list
, Qt
))
1930 deletion
= make_buffer_string_both (from
, from_byte
,
1931 from
+ combined_after_bytes
,
1932 from_byte
+ combined_after_bytes
,
1935 adjust_markers_for_record_delete (from
, from_byte
,
1936 from
+ combined_after_bytes
,
1937 from_byte
+ combined_after_bytes
);
1938 if (! EQ (current_buffer
->undo_list
, Qt
))
1939 record_delete (from
+ nchars_del
, deletion
);
1942 if (combined_before_bytes
)
1944 Lisp_Object deletion
;
1947 if (! EQ (current_buffer
->undo_list
, Qt
))
1948 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1949 from
, from_byte
, 1);
1950 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1952 if (! EQ (current_buffer
->undo_list
, Qt
))
1953 record_delete (from
- 1, deletion
);
1956 if (! EQ (current_buffer
->undo_list
, Qt
))
1958 record_delete (from
- !!combined_before_bytes
, deletion
);
1959 record_insert (from
- !!combined_before_bytes
,
1960 (inschars
- combined_before_bytes
1961 + !!combined_before_bytes
));
1964 GAP_SIZE
-= outgoing_insbytes
;
1968 GPT_BYTE
+= outgoing_insbytes
;
1969 ZV_BYTE
+= outgoing_insbytes
;
1970 Z_BYTE
+= outgoing_insbytes
;
1971 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1973 if (combined_after_bytes
)
1974 move_gap_both (GPT
+ combined_after_bytes
,
1975 GPT_BYTE
+ combined_after_bytes
);
1980 /* Adjust the overlay center as needed. This must be done after
1981 adjusting the markers that bound the overlays. */
1982 adjust_overlays_for_delete (from
, nchars_del
);
1983 adjust_overlays_for_insert (from
, inschars
);
1985 adjust_markers_for_insert (from
, from_byte
,
1986 from
+ inschars
, from_byte
+ outgoing_insbytes
,
1987 combined_before_bytes
, combined_after_bytes
, 0);
1989 #ifdef USE_TEXT_PROPERTIES
1990 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1992 /* Get the intervals for the part of the string we are inserting--
1993 not including the combined-before bytes. */
1994 intervals
= XSTRING (new)->intervals
;
1995 /* Insert those intervals. */
1996 graft_intervals_into_buffer (intervals
, from
, inschars
,
1997 current_buffer
, inherit
);
2000 /* Relocate point as if it were a marker. */
2002 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
2003 (from_byte
+ outgoing_insbytes
2004 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
2006 if (combined_after_bytes
)
2008 if (combined_before_bytes
== outgoing_insbytes
)
2009 /* This is the case that all new bytes are combined. */
2010 combined_before_bytes
+= combined_after_bytes
;
2012 combine_bytes (from
+ inschars
, from_byte
+ outgoing_insbytes
,
2013 combined_after_bytes
);
2015 if (combined_before_bytes
)
2016 combine_bytes (from
, from_byte
, combined_before_bytes
);
2018 /* As byte combining will decrease Z, we must check this again. */
2019 if (Z
- GPT
< end_unchanged
)
2020 end_unchanged
= Z
- GPT
;
2022 if (outgoing_insbytes
== 0)
2023 evaporate_overlays (from
);
2030 signal_after_change (from
, nchars_del
, GPT
- from
);
2033 /* Delete characters in current buffer
2034 from FROM up to (but not including) TO.
2035 If TO comes before FROM, we delete nothing. */
2038 del_range (from
, to
)
2039 register int from
, to
;
2041 del_range_1 (from
, to
, 1);
2044 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2047 del_range_1 (from
, to
, prepare
)
2048 int from
, to
, prepare
;
2050 int from_byte
, to_byte
;
2052 #if !NO_PROMPT_IN_BUFFER
2053 if (INTEGERP (current_buffer
->minibuffer_prompt_length
))
2055 /* Don't delete part of a mini-buffer prompt. */
2056 int len
= XFASTINT (current_buffer
->minibuffer_prompt_length
);
2057 from
= max (from
, len
);
2059 #endif /* !NO_PROMPT_IN_BUFFER */
2061 /* Make args be valid */
2072 int range_length
= to
- from
;
2073 prepare_to_modify_buffer (from
, to
, &from
);
2074 to
= from
+ range_length
;
2077 from_byte
= CHAR_TO_BYTE (from
);
2078 to_byte
= CHAR_TO_BYTE (to
);
2080 del_range_2 (from
, from_byte
, to
, to_byte
);
2081 signal_after_change (from
, to
- from
, 0);
2084 /* Like del_range_1 but args are byte positions, not char positions. */
2087 del_range_byte (from_byte
, to_byte
, prepare
)
2088 int from_byte
, to_byte
, prepare
;
2092 /* Make args be valid */
2093 if (from_byte
< BEGV_BYTE
)
2094 from_byte
= BEGV_BYTE
;
2095 if (to_byte
> ZV_BYTE
)
2098 if (to_byte
<= from_byte
)
2101 from
= BYTE_TO_CHAR (from_byte
);
2102 to
= BYTE_TO_CHAR (to_byte
);
2106 int old_from
= from
, old_to
= Z
- to
;
2107 int range_length
= to
- from
;
2108 prepare_to_modify_buffer (from
, to
, &from
);
2109 to
= from
+ range_length
;
2111 if (old_from
!= from
)
2112 from_byte
= CHAR_TO_BYTE (from
);
2113 if (old_to
== Z
- to
)
2114 to_byte
= CHAR_TO_BYTE (to
);
2117 del_range_2 (from
, from_byte
, to
, to_byte
);
2118 signal_after_change (from
, to
- from
, 0);
2121 /* Like del_range_1, but positions are specified both as charpos
2125 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
2126 int from
, from_byte
, to
, to_byte
, prepare
;
2128 /* Make args be valid */
2129 if (from_byte
< BEGV_BYTE
)
2130 from_byte
= BEGV_BYTE
;
2131 if (to_byte
> ZV_BYTE
)
2134 if (to_byte
<= from_byte
)
2144 int old_from
= from
, old_to
= Z
- to
;
2145 int range_length
= to
- from
;
2146 prepare_to_modify_buffer (from
, to
, &from
);
2147 to
= from
+ range_length
;
2149 if (old_from
!= from
)
2150 from_byte
= CHAR_TO_BYTE (from
);
2151 if (old_to
== Z
- to
)
2152 to_byte
= CHAR_TO_BYTE (to
);
2155 del_range_2 (from
, from_byte
, to
, to_byte
);
2156 signal_after_change (from
, to
- from
, 0);
2159 /* Delete a range of text, specified both as character positions
2160 and byte positions. FROM and TO are character positions,
2161 while FROM_BYTE and TO_BYTE are byte positions. */
2164 del_range_2 (from
, from_byte
, to
, to_byte
)
2165 int from
, from_byte
, to
, to_byte
;
2167 register int nbytes_del
, nchars_del
;
2168 int combined_after_bytes
;
2169 Lisp_Object deletion
;
2174 nchars_del
= to
- from
;
2175 nbytes_del
= to_byte
- from_byte
;
2177 /* Make sure the gap is somewhere in or next to what we are deleting. */
2179 gap_right (from
, from_byte
);
2181 gap_left (to
, to_byte
, 0);
2183 combined_after_bytes
2184 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
2185 Z_BYTE
- to_byte
, from
, from_byte
);
2186 if (combined_after_bytes
)
2188 if (from
== BEGV
|| to
== ZV
)
2189 byte_combining_error ();
2190 from_byte_1
= from_byte
;
2191 DEC_POS (from_byte_1
);
2194 from_byte_1
= from_byte
;
2196 if (! EQ (current_buffer
->undo_list
, Qt
))
2198 = make_buffer_string_both (from
- !!combined_after_bytes
,
2200 to
+ combined_after_bytes
,
2201 to_byte
+ combined_after_bytes
, 1);
2202 if (combined_after_bytes
)
2203 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
2204 the gap. We must move the gap again to a proper place. */
2205 move_gap_both (from
, from_byte
);
2207 /* Relocate all markers pointing into the new, larger gap
2208 to point at the end of the text before the gap.
2209 Do this before recording the deletion,
2210 so that undo handles this after reinserting the text. */
2211 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
2212 if (combined_after_bytes
)
2214 /* Adjust markers for the phony deletion
2215 that we are about to call record_undo for. */
2217 /* Here we delete the markers that formerly
2218 pointed at TO ... TO + COMBINED_AFTER_BYTES.
2219 But because of the call to adjust_markers_for_delete, above,
2220 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
2221 adjust_markers_for_record_delete (from
, from_byte
,
2222 from
+ combined_after_bytes
,
2223 from_byte
+ combined_after_bytes
);
2225 adjust_markers_for_record_delete (from
- 1, from_byte_1
,
2228 if (! EQ (current_buffer
->undo_list
, Qt
))
2229 record_delete (from
- !!combined_after_bytes
, deletion
);
2232 /* Relocate point as if it were a marker. */
2234 adjust_point (from
- (PT
< to
? PT
: to
),
2235 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
2237 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2238 offset_intervals (current_buffer
, from
, - nchars_del
);
2240 /* Adjust the overlay center as needed. This must be done after
2241 adjusting the markers that bound the overlays. */
2242 adjust_overlays_for_delete (from
, nchars_del
);
2244 GAP_SIZE
+= nbytes_del
;
2245 ZV_BYTE
-= nbytes_del
;
2246 Z_BYTE
-= nbytes_del
;
2250 GPT_BYTE
= from_byte
;
2252 if (combined_after_bytes
)
2253 move_gap_both (GPT
+ combined_after_bytes
,
2254 GPT_BYTE
+ combined_after_bytes
);
2256 *(GPT_ADDR
) = 0; /* Put an anchor. */
2261 if (GPT
- BEG
< beg_unchanged
)
2262 beg_unchanged
= GPT
- BEG
;
2263 if (Z
- GPT
< end_unchanged
)
2264 end_unchanged
= Z
- GPT
;
2266 if (combined_after_bytes
)
2268 /* Adjust markers for byte combining. As we have already
2269 adjuted markers without concerning byte combining, here we
2270 must concern only byte combining. */
2271 adjust_markers_for_replace (from
, from_byte
, 0, 0, 0, 0,
2272 0, combined_after_bytes
);
2273 combine_bytes (from
, from_byte
, combined_after_bytes
);
2275 record_insert (GPT
- 1, 1);
2277 if (Z
- GPT
< end_unchanged
)
2278 end_unchanged
= Z
- GPT
;
2283 evaporate_overlays (from
);
2286 /* Call this if you're about to change the region of BUFFER from
2287 character positions START to END. This checks the read-only
2288 properties of the region, calls the necessary modification hooks,
2289 and warns the next redisplay that it should pay attention to that
2293 modify_region (buffer
, start
, end
)
2294 struct buffer
*buffer
;
2297 struct buffer
*old_buffer
= current_buffer
;
2299 if (buffer
!= old_buffer
)
2300 set_buffer_internal (buffer
);
2302 prepare_to_modify_buffer (start
, end
, NULL
);
2304 if (start
- 1 < beg_unchanged
2305 || (unchanged_modified
== MODIFF
2306 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
2307 beg_unchanged
= start
- 1;
2308 if (Z
- end
< end_unchanged
2309 || (unchanged_modified
== MODIFF
2310 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
2311 end_unchanged
= Z
- end
;
2313 if (MODIFF
<= SAVE_MODIFF
)
2314 record_first_change ();
2317 buffer
->point_before_scroll
= Qnil
;
2319 if (buffer
!= old_buffer
)
2320 set_buffer_internal (old_buffer
);
2323 /* Check that it is okay to modify the buffer between START and END,
2324 which are char positions.
2326 Run the before-change-function, if any. If intervals are in use,
2327 verify that the text to be modified is not read-only, and call
2328 any modification properties the text may have.
2330 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2331 by holding its value temporarily in a marker. */
2334 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
2338 if (!NILP (current_buffer
->read_only
))
2339 Fbarf_if_buffer_read_only ();
2341 /* Let redisplay consider other windows than selected_window
2342 if modifying another buffer. */
2343 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
2344 ++windows_or_buffers_changed
;
2346 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2347 if (BUF_INTERVALS (current_buffer
) != 0)
2351 Lisp_Object preserve_marker
;
2352 struct gcpro gcpro1
;
2353 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2354 GCPRO1 (preserve_marker
);
2355 verify_interval_modification (current_buffer
, start
, end
);
2356 *preserve_ptr
= marker_position (preserve_marker
);
2357 unchain_marker (preserve_marker
);
2361 verify_interval_modification (current_buffer
, start
, end
);
2364 #ifdef CLASH_DETECTION
2365 if (!NILP (current_buffer
->file_truename
)
2366 /* Make binding buffer-file-name to nil effective. */
2367 && !NILP (current_buffer
->filename
)
2368 && SAVE_MODIFF
>= MODIFF
)
2369 lock_file (current_buffer
->file_truename
);
2371 /* At least warn if this file has changed on disk since it was visited. */
2372 if (!NILP (current_buffer
->filename
)
2373 && SAVE_MODIFF
>= MODIFF
2374 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2375 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
2376 call1 (intern ("ask-user-about-supersession-threat"),
2377 current_buffer
->filename
);
2378 #endif /* not CLASH_DETECTION */
2380 signal_before_change (start
, end
, preserve_ptr
);
2382 if (current_buffer
->newline_cache
)
2383 invalidate_region_cache (current_buffer
,
2384 current_buffer
->newline_cache
,
2385 start
- BEG
, Z
- end
);
2386 if (current_buffer
->width_run_cache
)
2387 invalidate_region_cache (current_buffer
,
2388 current_buffer
->width_run_cache
,
2389 start
- BEG
, Z
- end
);
2391 Vdeactivate_mark
= Qt
;
2394 /* These macros work with an argument named `preserve_ptr'
2395 and a local variable named `preserve_marker'. */
2397 #define PRESERVE_VALUE \
2398 if (preserve_ptr && NILP (preserve_marker)) \
2399 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2401 #define RESTORE_VALUE \
2402 if (! NILP (preserve_marker)) \
2404 *preserve_ptr = marker_position (preserve_marker); \
2405 unchain_marker (preserve_marker); \
2408 #define PRESERVE_START_END \
2409 if (NILP (start_marker)) \
2410 start_marker = Fcopy_marker (start, Qnil); \
2411 if (NILP (end_marker)) \
2412 end_marker = Fcopy_marker (end, Qnil);
2414 #define FETCH_START \
2415 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2418 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2420 /* Signal a change to the buffer immediately before it happens.
2421 START_INT and END_INT are the bounds of the text to be changed.
2423 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2424 by holding its value temporarily in a marker. */
2427 signal_before_change (start_int
, end_int
, preserve_ptr
)
2428 int start_int
, end_int
;
2431 Lisp_Object start
, end
;
2432 Lisp_Object start_marker
, end_marker
;
2433 Lisp_Object preserve_marker
;
2434 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2436 if (inhibit_modification_hooks
)
2439 start
= make_number (start_int
);
2440 end
= make_number (end_int
);
2441 preserve_marker
= Qnil
;
2442 start_marker
= Qnil
;
2444 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2446 /* If buffer is unmodified, run a special hook for that case. */
2447 if (SAVE_MODIFF
>= MODIFF
2448 && !NILP (Vfirst_change_hook
)
2449 && !NILP (Vrun_hooks
))
2453 call1 (Vrun_hooks
, Qfirst_change_hook
);
2456 /* Run the before-change-function if any.
2457 We don't bother "binding" this variable to nil
2458 because it is obsolete anyway and new code should not use it. */
2459 if (!NILP (Vbefore_change_function
))
2463 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
2466 /* Now run the before-change-functions if any. */
2467 if (!NILP (Vbefore_change_functions
))
2469 Lisp_Object args
[3];
2470 Lisp_Object before_change_functions
;
2471 Lisp_Object after_change_functions
;
2472 struct gcpro gcpro1
, gcpro2
;
2477 /* "Bind" before-change-functions and after-change-functions
2478 to nil--but in a way that errors don't know about.
2479 That way, if there's an error in them, they will stay nil. */
2480 before_change_functions
= Vbefore_change_functions
;
2481 after_change_functions
= Vafter_change_functions
;
2482 Vbefore_change_functions
= Qnil
;
2483 Vafter_change_functions
= Qnil
;
2484 GCPRO2 (before_change_functions
, after_change_functions
);
2486 /* Actually run the hook functions. */
2487 args
[0] = Qbefore_change_functions
;
2488 args
[1] = FETCH_START
;
2489 args
[2] = FETCH_END
;
2490 run_hook_list_with_args (before_change_functions
, 3, args
);
2492 /* "Unbind" the variables we "bound" to nil. */
2493 Vbefore_change_functions
= before_change_functions
;
2494 Vafter_change_functions
= after_change_functions
;
2498 if (!NILP (current_buffer
->overlays_before
)
2499 || !NILP (current_buffer
->overlays_after
))
2502 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2503 FETCH_START
, FETCH_END
, Qnil
);
2506 if (! NILP (start_marker
))
2507 free_marker (start_marker
);
2508 if (! NILP (end_marker
))
2509 free_marker (end_marker
);
2514 /* Signal a change immediately after it happens.
2515 CHARPOS is the character position of the start of the changed text.
2516 LENDEL is the number of characters of the text before the change.
2517 (Not the whole buffer; just the part that was changed.)
2518 LENINS is the number of characters in that part of the text
2519 after the change. */
2522 signal_after_change (charpos
, lendel
, lenins
)
2523 int charpos
, lendel
, lenins
;
2525 if (inhibit_modification_hooks
)
2528 /* If we are deferring calls to the after-change functions
2529 and there are no before-change functions,
2530 just record the args that we were going to use. */
2531 if (! NILP (Vcombine_after_change_calls
)
2532 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
2533 && NILP (current_buffer
->overlays_before
)
2534 && NILP (current_buffer
->overlays_after
))
2538 if (!NILP (combine_after_change_list
)
2539 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2540 Fcombine_after_change_execute ();
2542 elt
= Fcons (make_number (charpos
- BEG
),
2543 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2544 Fcons (make_number (lenins
- lendel
), Qnil
)));
2545 combine_after_change_list
2546 = Fcons (elt
, combine_after_change_list
);
2547 combine_after_change_buffer
= Fcurrent_buffer ();
2552 if (!NILP (combine_after_change_list
))
2553 Fcombine_after_change_execute ();
2555 /* Run the after-change-function if any.
2556 We don't bother "binding" this variable to nil
2557 because it is obsolete anyway and new code should not use it. */
2558 if (!NILP (Vafter_change_function
))
2559 call3 (Vafter_change_function
,
2560 make_number (charpos
), make_number (charpos
+ lenins
),
2561 make_number (lendel
));
2563 if (!NILP (Vafter_change_functions
))
2565 Lisp_Object args
[4];
2566 Lisp_Object before_change_functions
;
2567 Lisp_Object after_change_functions
;
2568 struct gcpro gcpro1
, gcpro2
;
2570 /* "Bind" before-change-functions and after-change-functions
2571 to nil--but in a way that errors don't know about.
2572 That way, if there's an error in them, they will stay nil. */
2573 before_change_functions
= Vbefore_change_functions
;
2574 after_change_functions
= Vafter_change_functions
;
2575 Vbefore_change_functions
= Qnil
;
2576 Vafter_change_functions
= Qnil
;
2577 GCPRO2 (before_change_functions
, after_change_functions
);
2579 /* Actually run the hook functions. */
2580 args
[0] = Qafter_change_functions
;
2581 XSETFASTINT (args
[1], charpos
);
2582 XSETFASTINT (args
[2], charpos
+ lenins
);
2583 XSETFASTINT (args
[3], lendel
);
2584 run_hook_list_with_args (after_change_functions
,
2587 /* "Unbind" the variables we "bound" to nil. */
2588 Vbefore_change_functions
= before_change_functions
;
2589 Vafter_change_functions
= after_change_functions
;
2593 if (!NILP (current_buffer
->overlays_before
)
2594 || !NILP (current_buffer
->overlays_after
))
2595 report_overlay_modification (make_number (charpos
),
2596 make_number (charpos
+ lenins
),
2598 make_number (charpos
),
2599 make_number (charpos
+ lenins
),
2600 make_number (lendel
));
2602 /* After an insertion, call the text properties
2603 insert-behind-hooks or insert-in-front-hooks. */
2605 report_interval_modification (make_number (charpos
),
2606 make_number (charpos
+ lenins
));
2610 Fcombine_after_change_execute_1 (val
)
2613 Vcombine_after_change_calls
= val
;
2617 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2618 Scombine_after_change_execute
, 0, 0, 0,
2619 "This function is for use internally in `combine-after-change-calls'.")
2622 int count
= specpdl_ptr
- specpdl
;
2623 int beg
, end
, change
;
2627 if (NILP (combine_after_change_list
))
2630 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2632 Fset_buffer (combine_after_change_buffer
);
2634 /* # chars unchanged at beginning of buffer. */
2636 /* # chars unchanged at end of buffer. */
2638 /* Total amount of insertion (negative for deletion). */
2641 /* Scan the various individual changes,
2642 accumulating the range info in BEG, END and CHANGE. */
2643 for (tail
= combine_after_change_list
; CONSP (tail
);
2644 tail
= XCONS (tail
)->cdr
)
2647 int thisbeg
, thisend
, thischange
;
2649 /* Extract the info from the next element. */
2650 elt
= XCONS (tail
)->car
;
2653 thisbeg
= XINT (XCONS (elt
)->car
);
2655 elt
= XCONS (elt
)->cdr
;
2658 thisend
= XINT (XCONS (elt
)->car
);
2660 elt
= XCONS (elt
)->cdr
;
2663 thischange
= XINT (XCONS (elt
)->car
);
2665 /* Merge this range into the accumulated range. */
2666 change
+= thischange
;
2673 /* Get the current start and end positions of the range
2674 that was changed. */
2678 /* We are about to handle these, so discard them. */
2679 combine_after_change_list
= Qnil
;
2681 /* Now run the after-change functions for real.
2682 Turn off the flag that defers them. */
2683 record_unwind_protect (Fcombine_after_change_execute_1
,
2684 Vcombine_after_change_calls
);
2685 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2687 return unbind_to (count
, Qnil
);
2693 staticpro (&combine_after_change_list
);
2694 combine_after_change_list
= Qnil
;
2695 combine_after_change_buffer
= Qnil
;
2697 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2698 "Non-nil means enable debugging checks for invalid marker positions.");
2699 check_markers_debug_flag
= 0;
2700 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2701 "Used internally by the `combine-after-change-calls' macro.");
2702 Vcombine_after_change_calls
= Qnil
;
2704 defsubr (&Scombine_after_change_execute
);