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))
37 static void insert_from_string_1
P_ ((Lisp_Object
, int, int, int, int, int, int));
38 static void insert_from_buffer_1 ();
39 static void gap_left
P_ ((int, int, int));
40 static void gap_right
P_ ((int, int));
41 static void adjust_markers_gap_motion
P_ ((int, int, int));
42 static void adjust_markers_for_insert
P_ ((int, int, int, int, int, int, int));
43 static void adjust_markers_for_delete
P_ ((int, int, int, int));
44 static void adjust_markers_for_record_delete
P_ ((int, int, int, int));
45 static void adjust_point
P_ ((int, int));
47 Lisp_Object
Fcombine_after_change_execute ();
49 /* Non-nil means don't call the after-change-functions right away,
50 just record an element in Vcombine_after_change_calls_list. */
51 Lisp_Object Vcombine_after_change_calls
;
53 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
54 describing changes which happened while combine_after_change_calls
55 was nonzero. We use this to decide how to call them
56 once the deferral ends.
59 BEG-UNCHANGED is the number of chars before the changed range.
60 END-UNCHANGED is the number of chars after the changed range,
61 and CHANGE-AMOUNT is the number of characters inserted by the change
62 (negative for a deletion). */
63 Lisp_Object combine_after_change_list
;
65 /* Buffer which combine_after_change_list is about. */
66 Lisp_Object combine_after_change_buffer
;
68 /* Check all markers in the current buffer, looking for something invalid. */
70 static int check_markers_debug_flag
;
72 #define CHECK_MARKERS() \
73 if (check_markers_debug_flag) \
80 register Lisp_Object tail
, prev
, next
;
81 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
83 tail
= BUF_MARKERS (current_buffer
);
85 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
87 if (XMARKER (tail
)->buffer
->text
!= current_buffer
->text
)
89 if (XMARKER (tail
)->charpos
> Z
)
91 if (XMARKER (tail
)->bytepos
> Z_BYTE
)
93 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail
)->bytepos
)))
96 tail
= XMARKER (tail
)->chain
;
100 /* Move gap to position CHARPOS.
101 Note that this can quit! */
107 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
110 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
111 Note that this can quit! */
114 move_gap_both (charpos
, bytepos
)
115 int charpos
, bytepos
;
117 if (bytepos
< GPT_BYTE
)
118 gap_left (charpos
, bytepos
, 0);
119 else if (bytepos
> GPT_BYTE
)
120 gap_right (charpos
, bytepos
);
123 /* Move the gap to a position less than the current GPT.
124 BYTEPOS describes the new position as a byte position,
125 and CHARPOS is the corresponding char position.
126 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
129 gap_left (charpos
, bytepos
, newgap
)
130 register int charpos
, bytepos
;
133 register unsigned char *to
, *from
;
139 if (unchanged_modified
== MODIFF
140 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
142 beg_unchanged
= charpos
- BEG
;
143 end_unchanged
= Z
- charpos
;
147 if (Z
- GPT
< end_unchanged
)
148 end_unchanged
= Z
- GPT
;
149 if (charpos
< beg_unchanged
)
150 beg_unchanged
= charpos
- BEG
;
159 /* Now copy the characters. To move the gap down,
160 copy characters up. */
164 /* I gets number of characters left to copy. */
165 i
= new_s1
- bytepos
;
168 /* If a quit is requested, stop copying now.
169 Change BYTEPOS to be where we have actually moved the gap to. */
173 charpos
= BYTE_TO_CHAR (bytepos
);
176 /* Move at most 32000 chars before checking again for a quit. */
181 /* bcopy is safe if the two areas of memory do not overlap
182 or on systems where bcopy is always safe for moving upward. */
183 && (BCOPY_UPWARD_SAFE
184 || to
- from
>= 128))
186 /* If overlap is not safe, avoid it by not moving too many
187 characters at once. */
188 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
203 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
204 BYTEPOS is where the loop above stopped, which may be what was specified
205 or may be where a quit was detected. */
206 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
209 if (bytepos
< charpos
)
211 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
215 /* Move the gap to a position greater than than the current GPT.
216 BYTEPOS describes the new position as a byte position,
217 and CHARPOS is the corresponding char position. */
220 gap_right (charpos
, bytepos
)
221 register int charpos
, bytepos
;
223 register unsigned char *to
, *from
;
227 if (unchanged_modified
== MODIFF
228 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
230 beg_unchanged
= charpos
- BEG
;
231 end_unchanged
= Z
- charpos
;
235 if (Z
- charpos
- 1 < end_unchanged
)
236 end_unchanged
= Z
- charpos
;
237 if (GPT
- BEG
< beg_unchanged
)
238 beg_unchanged
= GPT
- BEG
;
246 /* Now copy the characters. To move the gap up,
247 copy characters down. */
251 /* I gets number of characters left to copy. */
252 i
= bytepos
- new_s1
;
255 /* If a quit is requested, stop copying now.
256 Change BYTEPOS to be where we have actually moved the gap to. */
260 charpos
= BYTE_TO_CHAR (bytepos
);
263 /* Move at most 32000 chars before checking again for a quit. */
268 /* bcopy is safe if the two areas of memory do not overlap
269 or on systems where bcopy is always safe for moving downward. */
270 && (BCOPY_DOWNWARD_SAFE
271 || from
- to
>= 128))
273 /* If overlap is not safe, avoid it by not moving too many
274 characters at once. */
275 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
290 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
294 if (bytepos
< charpos
)
296 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
300 /* Add AMOUNT to the byte position of every marker in the current buffer
301 whose current byte position is between FROM (exclusive) and TO (inclusive).
303 Also, any markers past the outside of that interval, in the direction
304 of adjustment, are first moved back to the near end of the interval
305 and then adjusted by AMOUNT.
307 When the latter adjustment is done, if AMOUNT is negative,
308 we record the adjustment for undo. (This case happens only for
311 The markers' character positions are not altered,
312 because gap motion does not affect character positions. */
314 int adjust_markers_test
;
317 adjust_markers_gap_motion (from
, to
, amount
)
318 register int from
, to
, amount
;
320 /* Now that a marker has a bytepos, not counting the gap,
321 nothing needs to be done here. */
324 register struct Lisp_Marker
*m
;
327 marker
= BUF_MARKERS (current_buffer
);
329 while (!NILP (marker
))
331 m
= XMARKER (marker
);
335 if (mpos
> to
&& mpos
< to
+ amount
)
337 if (adjust_markers_test
)
344 /* Here's the case where a marker is inside text being deleted.
345 AMOUNT can be negative for gap motion, too,
346 but then this range contains no markers. */
347 if (mpos
> from
+ amount
&& mpos
<= from
)
349 if (adjust_markers_test
)
351 mpos
= from
+ amount
;
354 if (mpos
> from
&& mpos
<= to
)
362 /* Adjust all markers for a deletion
363 whose range in bytes is FROM_BYTE to TO_BYTE.
364 The range in charpos is FROM to TO.
366 This function assumes that the gap is adjacent to
367 or inside of the range being deleted. */
370 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
371 register int from
, from_byte
, to
, to_byte
;
374 register struct Lisp_Marker
*m
;
375 register int charpos
;
377 marker
= BUF_MARKERS (current_buffer
);
379 while (!NILP (marker
))
381 m
= XMARKER (marker
);
382 charpos
= m
->charpos
;
387 /* If the marker is after the deletion,
388 relocate by number of chars / bytes deleted. */
391 m
->charpos
-= to
- from
;
392 m
->bytepos
-= to_byte
- from_byte
;
395 /* Here's the case where a marker is inside text being deleted. */
396 else if (charpos
> from
)
398 record_marker_adjustment (marker
, from
- charpos
);
400 m
->bytepos
= from_byte
;
408 /* Adjust all markers for calling record_delete for combining bytes.
409 whose range in bytes is FROM_BYTE to TO_BYTE.
410 The range in charpos is FROM to TO. */
413 adjust_markers_for_record_delete (from
, from_byte
, to
, to_byte
)
414 register int from
, from_byte
, to
, to_byte
;
417 register struct Lisp_Marker
*m
;
418 register int charpos
;
420 marker
= BUF_MARKERS (current_buffer
);
422 while (!NILP (marker
))
424 m
= XMARKER (marker
);
425 charpos
= m
->charpos
;
427 /* If the marker is after the deletion,
428 relocate by number of chars / bytes deleted. */
431 /* Here's the case where a marker is inside text being deleted. */
432 else if (charpos
> from
)
433 record_marker_adjustment (marker
, from
- charpos
);
439 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
440 to TO / TO_BYTE. We have to relocate the charpos of every marker
441 that points after the insertion (but not their bytepos).
443 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
444 that combine into one character with the text before the insertion.
445 COMBINED_AFTER_BYTES is the number of bytes after the insertion
446 that combine into one character with the last inserted bytes.
448 When a marker points at the insertion point,
449 we advance it if either its insertion-type is t
450 or BEFORE_MARKERS is true. */
453 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
,
454 combined_before_bytes
, combined_after_bytes
,
456 register int from
, from_byte
, to
, to_byte
;
457 int combined_before_bytes
, combined_after_bytes
, before_markers
;
461 int nchars
= to
- from
;
462 int nbytes
= to_byte
- from_byte
;
464 marker
= BUF_MARKERS (current_buffer
);
466 while (!NILP (marker
))
468 register struct Lisp_Marker
*m
= XMARKER (marker
);
470 /* In a single-byte buffer, a marker's two positions must be equal.
471 (If this insertion is going to combine characters, Z will
472 become different from Z_BYTE, but they might be the same now.
473 If so, the two OLD positions of the marker should be equal.) */
476 if (m
->charpos
!= m
->bytepos
)
480 if (m
->bytepos
== from_byte
)
482 if (m
->insertion_type
|| before_markers
)
484 m
->bytepos
= to_byte
+ combined_after_bytes
;
485 m
->charpos
= to
- combined_before_bytes
;
486 /* Point the marker before the combined character,
487 so that undoing the insertion puts it back where it was. */
488 if (combined_after_bytes
)
489 DEC_BOTH (m
->charpos
, m
->bytepos
);
490 if (m
->insertion_type
)
493 else if (combined_before_bytes
)
495 /* This marker doesn't "need relocation",
496 but don't leave it pointing in the middle of a character.
497 Point the marker after the combined character,
498 so that undoing the insertion puts it back where it was. */
499 m
->bytepos
+= combined_before_bytes
;
500 if (combined_before_bytes
== nbytes
)
501 /* All new bytes plus combined_after_bytes (if any)
503 m
->bytepos
+= combined_after_bytes
;
506 /* If a marker was pointing into the combining bytes
507 after the insertion, don't leave it there
508 in the middle of a character. */
509 else if (combined_after_bytes
&& m
->bytepos
>= from_byte
510 && m
->bytepos
< from_byte
+ combined_after_bytes
)
512 /* Put it after the combining bytes. */
513 m
->bytepos
= to_byte
+ combined_after_bytes
;
514 m
->charpos
= to
- combined_before_bytes
;
515 /* Now move it back before the combined character,
516 so that undoing the insertion will put it where it was. */
517 DEC_BOTH (m
->charpos
, m
->bytepos
);
519 else if (m
->bytepos
> from_byte
)
521 m
->bytepos
+= nbytes
;
522 m
->charpos
+= nchars
- combined_after_bytes
- combined_before_bytes
;
528 /* Adjusting only markers whose insertion-type is t may result in
529 disordered overlays in the slot `overlays_before'. */
531 fix_overlays_before (current_buffer
, from
, to
);
534 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
536 This is used only when the value of point changes due to an insert
537 or delete; it does not represent a conceptual change in point as a
538 marker. In particular, point is not crossing any interval
539 boundaries, so there's no need to use the usual SET_PT macro. In
540 fact it would be incorrect to do so, because either the old or the
541 new value of point is out of sync with the current set of
545 adjust_point (nchars
, nbytes
)
548 BUF_PT (current_buffer
) += nchars
;
549 BUF_PT_BYTE (current_buffer
) += nbytes
;
551 /* In a single-byte buffer, the two positions must be equal. */
557 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
558 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
561 See the comment of adjust_markers_for_insert for the args
562 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES. */
565 adjust_markers_for_replace (from
, from_byte
, old_chars
, old_bytes
,
566 new_chars
, new_bytes
,
567 combined_before_bytes
, combined_after_bytes
)
568 int from
, from_byte
, old_chars
, old_bytes
, new_chars
, new_bytes
;
569 int combined_before_bytes
, combined_after_bytes
;
571 Lisp_Object marker
= BUF_MARKERS (current_buffer
);
572 int prev_to_byte
= from_byte
+ old_bytes
;
574 = (new_chars
- combined_before_bytes
) - (old_chars
+ combined_after_bytes
);
575 int diff_bytes
= new_bytes
- old_bytes
;
577 while (!NILP (marker
))
579 register struct Lisp_Marker
*m
= XMARKER (marker
);
581 if (m
->bytepos
>= prev_to_byte
)
583 if (m
->bytepos
< prev_to_byte
+ combined_after_bytes
)
585 /* Put it after the combining bytes. */
586 m
->bytepos
= from_byte
+ new_bytes
+ combined_after_bytes
;
587 m
->charpos
= from
+ new_chars
- combined_before_bytes
;
591 m
->charpos
+= diff_chars
;
592 m
->bytepos
+= diff_bytes
;
595 else if (m
->bytepos
>= from_byte
)
598 m
->bytepos
= from_byte
+ combined_before_bytes
;
599 /* If all new bytes are combined in addition to that there
600 are after combining bytes, we must set byte position of
601 the marker after the after combining bytes. */
602 if (combined_before_bytes
== new_bytes
)
603 m
->bytepos
+= combined_after_bytes
;
613 /* Make the gap NBYTES_ADDED bytes longer. */
616 make_gap (nbytes_added
)
619 unsigned char *result
;
622 int real_gap_loc_byte
;
625 /* If we have to get more space, get enough to last a while. */
626 nbytes_added
+= 2000;
628 /* Don't allow a buffer size that won't fit in an int
629 even if it will fit in a Lisp integer.
630 That won't work because so many places use `int'. */
632 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
633 >= ((unsigned) 1 << (min (BITS_PER_INT
, VALBITS
) - 1)))
634 error ("Buffer exceeds maximum size");
637 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
638 result
= BUFFER_REALLOC (BEG_ADDR
, (Z_BYTE
- BEG_BYTE
639 + GAP_SIZE
+ nbytes_added
+ 1));
647 /* We can't unblock until the new address is properly stored. */
651 /* Prevent quitting in move_gap. */
656 real_gap_loc_byte
= GPT_BYTE
;
657 old_gap_size
= GAP_SIZE
;
659 /* Call the newly allocated space a gap at the end of the whole space. */
661 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
662 GAP_SIZE
= nbytes_added
;
664 /* Move the new gap down to be consecutive with the end of the old one.
665 This adjusts the markers properly too. */
666 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
668 /* Now combine the two into one large gap. */
669 GAP_SIZE
+= old_gap_size
;
671 GPT_BYTE
= real_gap_loc_byte
;
679 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
680 FROM_MULTIBYTE says whether the incoming text is multibyte.
681 TO_MULTIBYTE says whether to store the text as multibyte.
682 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
684 Return the number of bytes stored at TO_ADDR. */
687 copy_text (from_addr
, to_addr
, nbytes
,
688 from_multibyte
, to_multibyte
)
689 unsigned char *from_addr
;
690 unsigned char *to_addr
;
692 int from_multibyte
, to_multibyte
;
694 if (from_multibyte
== to_multibyte
)
696 bcopy (from_addr
, to_addr
, nbytes
);
699 else if (from_multibyte
)
702 int bytes_left
= nbytes
;
703 Lisp_Object tbl
= Qnil
, temp
;
705 /* We set the variable tbl to the reverse table of
706 Vnonascii_translation_table in advance. */
707 if (CHAR_TABLE_P (Vnonascii_translation_table
))
709 tbl
= Fchar_table_extra_slot (Vnonascii_translation_table
,
711 if (!CHAR_TABLE_P (tbl
))
715 /* Convert multibyte to single byte. */
716 while (bytes_left
> 0)
718 int thislen
, c
, c_save
;
719 c
= c_save
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
720 if (!SINGLE_BYTE_CHAR_P (c
))
721 c
= multibyte_char_to_unibyte (c
, tbl
);
723 from_addr
+= thislen
;
724 bytes_left
-= thislen
;
731 unsigned char *initial_to_addr
= to_addr
;
733 /* Convert single-byte to multibyte. */
736 int c
= *from_addr
++;
737 unsigned char workbuf
[4], *str
;
740 if ((c
>= 0240 || !NILP (Vnonascii_translation_table
)) && c
< 0400)
742 c
= unibyte_char_to_multibyte (c
);
743 len
= CHAR_STRING (c
, workbuf
, str
);
744 bcopy (str
, to_addr
, len
);
749 /* Special case for speed. */
750 *to_addr
++ = c
, nbytes
--;
752 return to_addr
- initial_to_addr
;
756 /* Return the number of bytes it would take
757 to convert some single-byte text to multibyte.
758 The single-byte text consists of NBYTES bytes at PTR. */
761 count_size_as_multibyte (ptr
, nbytes
)
766 int outgoing_nbytes
= 0;
768 for (i
= 0; i
< nbytes
; i
++)
770 unsigned int c
= *ptr
++;
772 if (c
< 0240 && NILP (Vnonascii_translation_table
))
776 c
= unibyte_char_to_multibyte (c
);
777 outgoing_nbytes
+= CHAR_BYTES (c
);
781 return outgoing_nbytes
;
784 /* Insert a string of specified length before point.
785 This function judges multibyteness based on
786 enable_multibyte_characters in the current buffer;
787 it never converts between single-byte and multibyte.
789 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
790 prepare_to_modify_buffer could relocate the text. */
793 insert (string
, nbytes
)
794 register unsigned char *string
;
800 insert_1 (string
, nbytes
, 0, 1, 0);
801 signal_after_change (opoint
, 0, PT
- opoint
);
805 /* Likewise, but inherit text properties from neighboring characters. */
808 insert_and_inherit (string
, nbytes
)
809 register unsigned char *string
;
815 insert_1 (string
, nbytes
, 1, 1, 0);
816 signal_after_change (opoint
, 0, PT
- opoint
);
820 /* Insert the character C before point. Do not inherit text properties. */
826 unsigned char workbuf
[4], *str
;
829 if (! NILP (current_buffer
->enable_multibyte_characters
))
830 len
= CHAR_STRING (c
, workbuf
, str
);
841 /* Insert the null-terminated string S before point. */
847 insert (s
, strlen (s
));
850 /* Like `insert' except that all markers pointing at the place where
851 the insertion happens are adjusted to point after it.
852 Don't use this function to insert part of a Lisp string,
853 since gc could happen and relocate it. */
856 insert_before_markers (string
, nbytes
)
857 unsigned char *string
;
864 insert_1 (string
, nbytes
, 0, 1, 1);
865 signal_after_change (opoint
, 0, PT
- opoint
);
869 /* Likewise, but inherit text properties from neighboring characters. */
872 insert_before_markers_and_inherit (string
, nbytes
)
873 unsigned char *string
;
880 insert_1 (string
, nbytes
, 1, 1, 1);
881 signal_after_change (opoint
, 0, PT
- opoint
);
885 /* Subroutine used by the insert functions above. */
888 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
889 register unsigned char *string
;
891 int inherit
, prepare
, before_markers
;
893 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
894 inherit
, prepare
, before_markers
);
897 /* See if the bytes before POS/POS_BYTE combine with bytes
898 at the start of STRING to form a single character.
899 If so, return the number of bytes at the start of STRING
900 which combine in this way. Otherwise, return 0. */
903 count_combining_before (string
, length
, pos
, pos_byte
)
904 unsigned char *string
;
908 int opos
= pos
, opos_byte
= pos_byte
;
910 unsigned char *p
= string
;
912 if (NILP (current_buffer
->enable_multibyte_characters
))
914 if (length
== 0 || CHAR_HEAD_P (*string
))
918 c
= FETCH_BYTE (pos_byte
- 1);
919 if (ASCII_BYTE_P (c
))
921 DEC_BOTH (pos
, pos_byte
);
922 c
= FETCH_BYTE (pos_byte
);
923 if (! BASE_LEADING_CODE_P (c
))
926 /* We have a combination situation.
927 Count the bytes at STRING that will combine. */
928 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
934 /* See if the bytes after POS/POS_BYTE combine with bytes
935 at the end of STRING to form a single character.
936 If so, return the number of bytes after POS/POS_BYTE
937 which combine in this way. Otherwise, return 0. */
940 count_combining_after (string
, length
, pos
, pos_byte
)
941 unsigned char *string
;
945 int opos
= pos
, opos_byte
= pos_byte
;
949 if (NILP (current_buffer
->enable_multibyte_characters
))
951 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1]))
954 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
960 /* All characters in `string' are not character head.
961 We must check also preceding bytes at POS.
962 We are sure that the gap is at POS. */
965 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
967 if (i
< 0 || !BASE_LEADING_CODE_P (string
[i
]))
970 else if (!BASE_LEADING_CODE_P (string
[i
]))
975 c
= FETCH_BYTE (pos_byte
);
978 while (pos_byte
< Z_BYTE
)
980 c
= FETCH_BYTE (pos_byte
);
986 return pos_byte
- opos_byte
;
989 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
990 following the position POS/POS_BYTE to the character preceding POS.
991 If TARGET is after POS+NBYTES, we only have to adjust the character
992 position TARGET, else, if TARGET is after POS, we have to adjust
993 both the character position TARGET and the byte position
994 TARGET_BYTE, else we don't have to do any adjustment. */
996 #define ADJUST_CHAR_POS(target, target_byte) \
998 if (target > pos + nbytes) \
1000 else if (target >= pos) \
1003 target_byte = pos_byte + nbytes; \
1007 /* Combine NBYTES stray trailing-codes, which were formerly separate
1008 characters, with the preceding character. These bytes
1009 are located after position POS / POS_BYTE, and the preceding character
1010 is located just before that position.
1012 This function does not adjust markers for byte combining. That
1013 should be done in advance by the functions
1014 adjust_markers_for_insert or adjust_markers_for_replace. */
1017 combine_bytes (pos
, pos_byte
, nbytes
)
1018 int pos
, pos_byte
, nbytes
;
1020 adjust_overlays_for_delete (pos
, nbytes
);
1022 ADJUST_CHAR_POS (BUF_PT (current_buffer
), BUF_PT_BYTE (current_buffer
));
1023 ADJUST_CHAR_POS (GPT
, GPT_BYTE
);
1024 ADJUST_CHAR_POS (Z
, Z_BYTE
);
1025 ADJUST_CHAR_POS (ZV
, ZV_BYTE
);
1027 if (BUF_INTERVALS (current_buffer
) != 0)
1028 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1029 offset_intervals (current_buffer
, pos
, - nbytes
);
1033 byte_combining_error ()
1035 error ("Byte combining across boundary of accessible buffer text inhibitted");
1038 /* If we are going to combine bytes at POS which is at a narrowed
1039 region boundary, signal an error. */
1040 #define CHECK_BYTE_COMBINING_FOR_INSERT(pos) \
1042 if (combined_before_bytes && pos == BEGV \
1043 || combined_after_bytes && pos == ZV) \
1044 byte_combining_error (); \
1048 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1049 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
1050 are the same as in insert_1. */
1053 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
1054 register unsigned char *string
;
1055 register int nchars
, nbytes
;
1056 int inherit
, prepare
, before_markers
;
1058 register Lisp_Object temp
;
1059 int combined_before_bytes
, combined_after_bytes
;
1061 if (NILP (current_buffer
->enable_multibyte_characters
))
1065 /* Do this before moving and increasing the gap,
1066 because the before-change hooks might move the gap
1067 or make it smaller. */
1068 prepare_to_modify_buffer (PT
, PT
, NULL
);
1071 move_gap_both (PT
, PT_BYTE
);
1072 if (GAP_SIZE
< nbytes
)
1073 make_gap (nbytes
- GAP_SIZE
);
1075 combined_before_bytes
1076 = count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
1077 combined_after_bytes
1078 = count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
1079 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1081 /* Record deletion of the surrounding text that combines with
1082 the insertion. This, together with recording the insertion,
1083 will add up to the right stuff in the undo list.
1085 But there is no need to actually delete the combining bytes
1086 from the buffer and reinsert them. */
1088 if (combined_after_bytes
)
1090 Lisp_Object deletion
;
1093 if (! EQ (current_buffer
->undo_list
, Qt
))
1094 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1095 PT
+ combined_after_bytes
,
1096 PT_BYTE
+ combined_after_bytes
, 1);
1098 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1099 PT
+ combined_after_bytes
,
1100 PT_BYTE
+ combined_after_bytes
);
1101 if (! EQ (current_buffer
->undo_list
, Qt
))
1102 record_delete (PT
, deletion
);
1105 if (combined_before_bytes
)
1107 Lisp_Object deletion
;
1110 if (! EQ (current_buffer
->undo_list
, Qt
))
1111 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1113 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1115 if (! EQ (current_buffer
->undo_list
, Qt
))
1116 record_delete (PT
- 1, deletion
);
1119 record_insert (PT
- !!combined_before_bytes
,
1120 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1123 bcopy (string
, GPT_ADDR
, nbytes
);
1126 /* When we have combining at the end of the insertion,
1127 this is the character position before the combined character. */
1134 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1136 if (combined_after_bytes
)
1137 move_gap_both (GPT
+ combined_after_bytes
,
1138 GPT_BYTE
+ combined_after_bytes
);
1143 adjust_overlays_for_insert (PT
, nchars
);
1144 adjust_markers_for_insert (PT
, PT_BYTE
,
1145 PT
+ nchars
, PT_BYTE
+ nbytes
,
1146 combined_before_bytes
, combined_after_bytes
,
1149 #ifdef USE_TEXT_PROPERTIES
1150 if (BUF_INTERVALS (current_buffer
) != 0)
1151 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1152 offset_intervals (current_buffer
, PT
, nchars
);
1154 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1155 Fset_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1160 int pos
= PT
, pos_byte
= PT_BYTE
;
1162 adjust_point (nchars
+ combined_after_bytes
,
1163 nbytes
+ combined_after_bytes
);
1165 if (combined_after_bytes
)
1166 combine_bytes (pos
+ nchars
, pos_byte
+ nbytes
, combined_after_bytes
);
1168 if (combined_before_bytes
)
1169 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1175 /* Insert the part of the text of STRING, a Lisp object assumed to be
1176 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1177 starting at position POS / POS_BYTE. If the text of STRING has properties,
1178 copy them into the buffer.
1180 It does not work to use `insert' for this, because a GC could happen
1181 before we bcopy the stuff into the buffer, and relocate the string
1182 without insert noticing. */
1185 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1187 register int pos
, pos_byte
, length
, length_byte
;
1191 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1193 signal_after_change (opoint
, 0, PT
- opoint
);
1196 /* Like `insert_from_string' except that all markers pointing
1197 at the place where the insertion happens are adjusted to point after it. */
1200 insert_from_string_before_markers (string
, pos
, pos_byte
,
1201 length
, length_byte
, inherit
)
1203 register int pos
, pos_byte
, length
, length_byte
;
1207 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1209 signal_after_change (opoint
, 0, PT
- opoint
);
1212 /* Subroutine of the insertion functions above. */
1215 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1216 inherit
, before_markers
)
1218 register int pos
, pos_byte
, nchars
, nbytes
;
1219 int inherit
, before_markers
;
1221 register Lisp_Object temp
;
1222 struct gcpro gcpro1
;
1223 int outgoing_nbytes
= nbytes
;
1224 int combined_before_bytes
, combined_after_bytes
;
1225 int adjusted_nchars
;
1228 /* Make OUTGOING_NBYTES describe the text
1229 as it will be inserted in this buffer. */
1231 if (NILP (current_buffer
->enable_multibyte_characters
))
1232 outgoing_nbytes
= nchars
;
1233 else if (! STRING_MULTIBYTE (string
))
1235 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1239 /* Do this before moving and increasing the gap,
1240 because the before-change hooks might move the gap
1241 or make it smaller. */
1242 prepare_to_modify_buffer (PT
, PT
, NULL
);
1245 move_gap_both (PT
, PT_BYTE
);
1246 if (GAP_SIZE
< nbytes
)
1247 make_gap (outgoing_nbytes
- GAP_SIZE
);
1250 /* Copy the string text into the buffer, perhaps converting
1251 between single-byte and multibyte. */
1252 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1253 STRING_MULTIBYTE (string
),
1254 ! NILP (current_buffer
->enable_multibyte_characters
));
1256 /* We have copied text into the gap, but we have not altered
1257 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1258 to these functions and get the same results as we would
1259 have got earlier on. Meanwhile, PT_ADDR does point to
1260 the text that has been stored by copy_text. */
1262 combined_before_bytes
1263 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1264 combined_after_bytes
1265 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1267 unsigned char save
= *(GPT_ADDR
);
1269 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1273 /* Record deletion of the surrounding text that combines with
1274 the insertion. This, together with recording the insertion,
1275 will add up to the right stuff in the undo list.
1277 But there is no need to actually delete the combining bytes
1278 from the buffer and reinsert them. */
1280 if (combined_after_bytes
)
1282 Lisp_Object deletion
;
1285 if (! EQ (current_buffer
->undo_list
, Qt
))
1286 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1287 PT
+ combined_after_bytes
,
1288 PT_BYTE
+ combined_after_bytes
, 1);
1290 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1291 PT
+ combined_after_bytes
,
1292 PT_BYTE
+ combined_after_bytes
);
1293 if (! EQ (current_buffer
->undo_list
, Qt
))
1294 record_delete (PT
, deletion
);
1297 if (combined_before_bytes
)
1299 Lisp_Object deletion
;
1302 if (! EQ (current_buffer
->undo_list
, Qt
))
1303 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1305 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1307 if (! EQ (current_buffer
->undo_list
, Qt
))
1308 record_delete (PT
- 1, deletion
);
1311 record_insert (PT
- !!combined_before_bytes
,
1312 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1315 GAP_SIZE
-= outgoing_nbytes
;
1319 GPT_BYTE
+= outgoing_nbytes
;
1320 ZV_BYTE
+= outgoing_nbytes
;
1321 Z_BYTE
+= outgoing_nbytes
;
1322 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1324 if (combined_after_bytes
)
1325 move_gap_both (GPT
+ combined_after_bytes
,
1326 GPT_BYTE
+ combined_after_bytes
);
1331 adjust_overlays_for_insert (PT
, nchars
);
1332 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1333 PT_BYTE
+ outgoing_nbytes
,
1334 combined_before_bytes
, combined_after_bytes
,
1337 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1338 offset_intervals (current_buffer
, PT
, nchars
);
1340 intervals
= XSTRING (string
)->intervals
;
1341 /* Get the intervals for the part of the string we are inserting--
1342 not including the combined-before bytes. */
1343 if (nbytes
< STRING_BYTES (XSTRING (string
)))
1344 intervals
= copy_intervals (intervals
, pos
, nchars
);
1346 /* Insert those intervals. */
1347 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1348 current_buffer
, inherit
);
1351 int pos
= PT
, pos_byte
= PT_BYTE
;
1353 adjust_point (nchars
+ combined_after_bytes
,
1354 outgoing_nbytes
+ combined_after_bytes
);
1356 if (combined_after_bytes
)
1357 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1358 combined_after_bytes
);
1360 if (combined_before_bytes
)
1361 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1365 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1366 current buffer. If the text in BUF has properties, they are absorbed
1367 into the current buffer.
1369 It does not work to use `insert' for this, because a malloc could happen
1370 and relocate BUF's text before the bcopy happens. */
1373 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1375 int charpos
, nchars
;
1380 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1381 signal_after_change (opoint
, 0, PT
- opoint
);
1385 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1390 register Lisp_Object temp
;
1391 int chunk
, chunk_expanded
;
1392 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1393 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1394 int incoming_nbytes
= to_byte
- from_byte
;
1395 int outgoing_nbytes
= incoming_nbytes
;
1396 int combined_before_bytes
, combined_after_bytes
;
1397 int adjusted_nchars
;
1400 /* Make OUTGOING_NBYTES describe the text
1401 as it will be inserted in this buffer. */
1403 if (NILP (current_buffer
->enable_multibyte_characters
))
1404 outgoing_nbytes
= nchars
;
1405 else if (NILP (buf
->enable_multibyte_characters
))
1407 int outgoing_before_gap
= 0;
1408 int outgoing_after_gap
= 0;
1410 if (from
< BUF_GPT (buf
))
1412 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1413 if (chunk
> incoming_nbytes
)
1414 chunk
= incoming_nbytes
;
1416 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1422 if (chunk
< incoming_nbytes
)
1424 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1426 incoming_nbytes
- chunk
);
1428 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1431 /* Make sure point-max won't overflow after this insertion. */
1432 XSETINT (temp
, outgoing_nbytes
+ Z
);
1433 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1434 error ("Maximum buffer size exceeded");
1436 /* Do this before moving and increasing the gap,
1437 because the before-change hooks might move the gap
1438 or make it smaller. */
1439 prepare_to_modify_buffer (PT
, PT
, NULL
);
1442 move_gap_both (PT
, PT_BYTE
);
1443 if (GAP_SIZE
< outgoing_nbytes
)
1444 make_gap (outgoing_nbytes
- GAP_SIZE
);
1446 if (from
< BUF_GPT (buf
))
1448 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1449 if (chunk
> incoming_nbytes
)
1450 chunk
= incoming_nbytes
;
1451 /* Record number of output bytes, so we know where
1452 to put the output from the second copy_text. */
1454 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1456 ! NILP (buf
->enable_multibyte_characters
),
1457 ! NILP (current_buffer
->enable_multibyte_characters
));
1460 chunk_expanded
= chunk
= 0;
1462 if (chunk
< incoming_nbytes
)
1463 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1464 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1465 ! NILP (buf
->enable_multibyte_characters
),
1466 ! NILP (current_buffer
->enable_multibyte_characters
));
1468 /* We have copied text into the gap, but we have not altered
1469 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1470 to these functions and get the same results as we would
1471 have got earlier on. Meanwhile, GPT_ADDR does point to
1472 the text that has been stored by copy_text. */
1473 combined_before_bytes
1474 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1475 combined_after_bytes
1476 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1478 unsigned char save
= *(GPT_ADDR
);
1480 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1484 /* Record deletion of the surrounding text that combines with
1485 the insertion. This, together with recording the insertion,
1486 will add up to the right stuff in the undo list.
1488 But there is no need to actually delete the combining bytes
1489 from the buffer and reinsert them. */
1491 if (combined_after_bytes
)
1493 Lisp_Object deletion
;
1496 if (! EQ (current_buffer
->undo_list
, Qt
))
1497 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1498 PT
+ combined_after_bytes
,
1499 PT_BYTE
+ combined_after_bytes
, 1);
1501 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1502 PT
+ combined_after_bytes
,
1503 PT_BYTE
+ combined_after_bytes
);
1504 if (! EQ (current_buffer
->undo_list
, Qt
))
1505 record_delete (PT
, deletion
);
1508 if (combined_before_bytes
)
1510 Lisp_Object deletion
;
1513 if (! EQ (current_buffer
->undo_list
, Qt
))
1514 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1516 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1518 if (! EQ (current_buffer
->undo_list
, Qt
))
1519 record_delete (PT
- 1, deletion
);
1522 record_insert (PT
- !!combined_before_bytes
,
1523 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1526 GAP_SIZE
-= outgoing_nbytes
;
1530 GPT_BYTE
+= outgoing_nbytes
;
1531 ZV_BYTE
+= outgoing_nbytes
;
1532 Z_BYTE
+= outgoing_nbytes
;
1533 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1535 if (combined_after_bytes
)
1536 move_gap_both (GPT
+ combined_after_bytes
,
1537 GPT_BYTE
+ combined_after_bytes
);
1542 adjust_overlays_for_insert (PT
, nchars
);
1543 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1544 PT_BYTE
+ outgoing_nbytes
,
1545 combined_before_bytes
, combined_after_bytes
, 0);
1547 #ifdef USE_TEXT_PROPERTIES
1548 if (BUF_INTERVALS (current_buffer
) != 0)
1549 offset_intervals (current_buffer
, PT
, nchars
);
1552 /* Get the intervals for the part of the string we are inserting--
1553 not including the combined-before bytes. */
1554 intervals
= BUF_INTERVALS (buf
);
1555 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1556 intervals
= copy_intervals (intervals
, from
, nchars
);
1558 /* Insert those intervals. */
1559 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1562 int pos
= PT
, pos_byte
= PT_BYTE
;
1564 adjust_point (nchars
+ combined_after_bytes
,
1565 outgoing_nbytes
+ combined_after_bytes
);
1567 if (combined_after_bytes
)
1568 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1569 combined_after_bytes
);
1571 if (combined_before_bytes
)
1572 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1576 /* This function should be called after moving gap to FROM and before
1577 altering text between FROM and TO. This adjusts various position
1578 keepers and markers as if the text is deleted. Don't forget to
1579 call adjust_after_replace after you actually alter the text. */
1582 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1583 int from
, from_byte
, to
, to_byte
;
1585 Lisp_Object deletion
;
1587 if (! EQ (current_buffer
->undo_list
, Qt
))
1588 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1592 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1594 if (! EQ (current_buffer
->undo_list
, Qt
))
1595 record_delete (from
, deletion
);
1597 adjust_overlays_for_delete (from
, to
- from
);
1600 /* Record undo information and adjust markers and position keepers for
1601 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1602 chars (LEN_BYTE bytes) which resides in the gap just after
1605 PREV_TEXT nil means the new text was just inserted. */
1608 adjust_after_replace (from
, from_byte
, prev_text
, len
, len_byte
)
1609 int from
, from_byte
, len
, len_byte
;
1610 Lisp_Object prev_text
;
1612 int combined_before_bytes
1613 = count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
);
1614 int combined_after_bytes
1615 = count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
);
1616 /* This flag tells if we combine some bytes with a character before
1617 FROM. This happens even if combined_before_bytes is zero. */
1618 int combine_before
= (combined_before_bytes
1619 || (len
== 0 && combined_after_bytes
));
1621 int nchars_del
= 0, nbytes_del
= 0;
1623 if (STRINGP (prev_text
))
1625 nchars_del
= XSTRING (prev_text
)->size
;
1626 nbytes_del
= STRING_BYTES (XSTRING (prev_text
));
1629 if (combine_before
&& from
== BEGV
1630 || combined_after_bytes
&& from
== ZV
)
1632 /* We can't combine bytes nor signal an error here. So, let's
1633 pretend that the new text is just a single space. */
1635 combined_before_bytes
= combined_after_bytes
= 0;
1639 if (combined_after_bytes
)
1641 Lisp_Object deletion
;
1644 if (! EQ (current_buffer
->undo_list
, Qt
))
1645 deletion
= make_buffer_string_both (from
, from_byte
,
1646 from
+ combined_after_bytes
,
1647 from_byte
+ combined_after_bytes
,
1650 adjust_markers_for_record_delete (from
, from_byte
,
1651 from
+ combined_after_bytes
,
1652 from_byte
+ combined_after_bytes
);
1654 if (! EQ (current_buffer
->undo_list
, Qt
))
1655 record_delete (from
+ nchars_del
, deletion
);
1658 if (combined_before_bytes
1659 || len_byte
== 0 && combined_after_bytes
> 0)
1661 Lisp_Object deletion
;
1664 if (! EQ (current_buffer
->undo_list
, Qt
))
1665 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1666 from
, from_byte
, 1);
1667 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1669 if (! EQ (current_buffer
->undo_list
, Qt
))
1670 record_delete (from
- 1, deletion
);
1673 /* Update various buffer positions for the new text. */
1674 GAP_SIZE
-= len_byte
;
1676 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1677 GPT
+= len
; GPT_BYTE
+= len_byte
;
1678 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1680 /* The gap should be at character boundary. */
1681 if (combined_after_bytes
)
1682 move_gap_both (GPT
+ combined_after_bytes
,
1683 GPT_BYTE
+ combined_after_bytes
);
1685 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1687 combined_before_bytes
, combined_after_bytes
);
1688 if (! EQ (current_buffer
->undo_list
, Qt
))
1691 record_delete (from
- combine_before
, prev_text
);
1693 record_insert (from
- 1, len
- combined_before_bytes
+ 1);
1695 record_insert (from
, len
);
1698 if (len
> nchars_del
)
1699 adjust_overlays_for_insert (from
, len
- nchars_del
);
1700 else if (len
< nchars_del
)
1701 adjust_overlays_for_delete (from
, nchars_del
- len
);
1702 #ifdef USE_TEXT_PROPERTIES
1703 if (BUF_INTERVALS (current_buffer
) != 0)
1705 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1710 int pos
= PT
, pos_byte
= PT_BYTE
;
1713 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1715 if (combined_after_bytes
)
1717 if (combined_before_bytes
== len_byte
)
1718 /* This is the case that all new bytes are combined. */
1719 combined_before_bytes
+= combined_after_bytes
;
1721 combine_bytes (from
+ len
, from_byte
+ len_byte
,
1722 combined_after_bytes
);
1724 if (combined_before_bytes
)
1725 combine_bytes (from
, from_byte
, combined_before_bytes
);
1728 /* As byte combining will decrease Z, we must check this again. */
1729 if (Z
- GPT
< end_unchanged
)
1730 end_unchanged
= Z
- GPT
;
1735 evaporate_overlays (from
);
1739 /* Record undo information, adjust markers and position keepers for an
1740 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1741 text already exists in the current buffer but character length (TO
1742 - FROM) may be incorrect, the correct length is NEWLEN. */
1745 adjust_after_insert (from
, from_byte
, to
, to_byte
, newlen
)
1746 int from
, from_byte
, to
, to_byte
, newlen
;
1748 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
1751 move_gap_both (to
, to_byte
);
1752 GAP_SIZE
+= len_byte
;
1753 GPT
-= len
; GPT_BYTE
-= len_byte
;
1754 ZV
-= len
; ZV_BYTE
-= len_byte
;
1755 Z
-= len
; Z_BYTE
-= len_byte
;
1756 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1759 /* Replace the text from character positions FROM to TO with NEW,
1760 If PREPARE is nonzero, call prepare_to_modify_buffer.
1761 If INHERIT, the newly inserted text should inherit text properties
1762 from the surrounding non-deleted text. */
1764 /* Note that this does not yet handle markers quite right.
1765 Also it needs to record a single undo-entry that does a replacement
1766 rather than a separate delete and insert.
1767 That way, undo will also handle markers properly.
1769 But if MARKERS is 0, don't relocate markers. */
1772 replace_range (from
, to
, new, prepare
, inherit
, markers
)
1774 int from
, to
, prepare
, inherit
, markers
;
1776 int inschars
= XSTRING (new)->size
;
1777 int insbytes
= STRING_BYTES (XSTRING (new));
1778 int from_byte
, to_byte
;
1779 int nbytes_del
, nchars_del
;
1780 register Lisp_Object temp
;
1781 struct gcpro gcpro1
;
1782 int combined_before_bytes
, combined_after_bytes
;
1783 int adjusted_inschars
;
1785 int outgoing_insbytes
= insbytes
;
1786 Lisp_Object deletion
;
1794 int range_length
= to
- from
;
1795 prepare_to_modify_buffer (from
, to
, &from
);
1796 to
= from
+ range_length
;
1801 /* Make args be valid */
1807 from_byte
= CHAR_TO_BYTE (from
);
1808 to_byte
= CHAR_TO_BYTE (to
);
1810 nchars_del
= to
- from
;
1811 nbytes_del
= to_byte
- from_byte
;
1813 if (nbytes_del
<= 0 && insbytes
== 0)
1816 /* Make OUTGOING_INSBYTES describe the text
1817 as it will be inserted in this buffer. */
1819 if (NILP (current_buffer
->enable_multibyte_characters
))
1820 outgoing_insbytes
= inschars
;
1821 else if (! STRING_MULTIBYTE (new))
1823 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1825 /* Make sure point-max won't overflow after this insertion. */
1826 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1827 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1828 error ("Maximum buffer size exceeded");
1832 /* Make sure the gap is somewhere in or next to what we are deleting. */
1834 gap_right (from
, from_byte
);
1836 gap_left (to
, to_byte
, 0);
1838 /* Even if we don't record for undo, we must keep the original text
1839 because we may have to recover it because of inappropriate byte
1841 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1844 /* Relocate all markers pointing into the new, larger gap
1845 to point at the end of the text before the gap.
1846 Do this before recording the deletion,
1847 so that undo handles this after reinserting the text. */
1848 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1850 GAP_SIZE
+= nbytes_del
;
1853 ZV_BYTE
-= nbytes_del
;
1854 Z_BYTE
-= nbytes_del
;
1856 GPT_BYTE
= from_byte
;
1857 *(GPT_ADDR
) = 0; /* Put an anchor. */
1862 if (GPT
- BEG
< beg_unchanged
)
1863 beg_unchanged
= GPT
- BEG
;
1864 if (Z
- GPT
< end_unchanged
)
1865 end_unchanged
= Z
- GPT
;
1867 if (GAP_SIZE
< insbytes
)
1868 make_gap (insbytes
- GAP_SIZE
);
1870 /* Copy the string text into the buffer, perhaps converting
1871 between single-byte and multibyte. */
1872 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1873 STRING_MULTIBYTE (new),
1874 ! NILP (current_buffer
->enable_multibyte_characters
));
1876 /* We have copied text into the gap, but we have not marked
1877 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1878 here, for both the previous text and the following text.
1879 Meanwhile, GPT_ADDR does point to
1880 the text that has been stored by copy_text. */
1882 combined_before_bytes
1883 = count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1884 combined_after_bytes
1885 = count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1887 if (combined_before_bytes
&& from
== BEGV
1888 || combined_after_bytes
&& from
== ZV
)
1890 /* Bytes are being combined across the region boundary. We
1891 should avoid it. We recover the original contents before
1892 signaling an error. */
1893 bcopy (XSTRING (deletion
)->data
, GPT_ADDR
, nbytes_del
);
1894 GAP_SIZE
-= nbytes_del
;
1897 ZV_BYTE
+= nbytes_del
;
1898 Z_BYTE
+= nbytes_del
;
1899 GPT
= from
+ nchars_del
;
1900 GPT_BYTE
= from_byte
+ nbytes_del
;
1901 *(GPT_ADDR
) = 0; /* Put an anchor. */
1903 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
, 0, 0, 0);
1905 byte_combining_error ();
1909 /* Record deletion of the surrounding text that combines with
1910 the insertion. This, together with recording the insertion,
1911 will add up to the right stuff in the undo list.
1913 But there is no need to actually delete the combining bytes
1914 from the buffer and reinsert them. */
1916 if (combined_after_bytes
)
1918 Lisp_Object deletion
;
1921 if (! EQ (current_buffer
->undo_list
, Qt
))
1922 deletion
= make_buffer_string_both (from
, from_byte
,
1923 from
+ combined_after_bytes
,
1924 from_byte
+ combined_after_bytes
,
1927 adjust_markers_for_record_delete (from
, from_byte
,
1928 from
+ combined_after_bytes
,
1929 from_byte
+ combined_after_bytes
);
1930 if (! EQ (current_buffer
->undo_list
, Qt
))
1931 record_delete (from
+ nchars_del
, deletion
);
1934 if (combined_before_bytes
)
1936 Lisp_Object deletion
;
1939 if (! EQ (current_buffer
->undo_list
, Qt
))
1940 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1941 from
, from_byte
, 1);
1942 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1944 if (! EQ (current_buffer
->undo_list
, Qt
))
1945 record_delete (from
- 1, deletion
);
1948 if (! EQ (current_buffer
->undo_list
, Qt
))
1950 record_delete (from
- !!combined_before_bytes
, deletion
);
1951 record_insert (from
- !!combined_before_bytes
,
1952 (inschars
- combined_before_bytes
1953 + !!combined_before_bytes
));
1956 GAP_SIZE
-= outgoing_insbytes
;
1960 GPT_BYTE
+= outgoing_insbytes
;
1961 ZV_BYTE
+= outgoing_insbytes
;
1962 Z_BYTE
+= outgoing_insbytes
;
1963 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1965 if (combined_after_bytes
)
1966 move_gap_both (GPT
+ combined_after_bytes
,
1967 GPT_BYTE
+ combined_after_bytes
);
1972 /* Adjust the overlay center as needed. This must be done after
1973 adjusting the markers that bound the overlays. */
1974 adjust_overlays_for_delete (from
, nchars_del
);
1975 adjust_overlays_for_insert (from
, inschars
);
1977 adjust_markers_for_insert (from
, from_byte
,
1978 from
+ inschars
, from_byte
+ outgoing_insbytes
,
1979 combined_before_bytes
, combined_after_bytes
, 0);
1981 #ifdef USE_TEXT_PROPERTIES
1982 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1984 /* Get the intervals for the part of the string we are inserting--
1985 not including the combined-before bytes. */
1986 intervals
= XSTRING (new)->intervals
;
1987 /* Insert those intervals. */
1988 graft_intervals_into_buffer (intervals
, from
, inschars
,
1989 current_buffer
, inherit
);
1992 /* Relocate point as if it were a marker. */
1994 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1995 (from_byte
+ outgoing_insbytes
1996 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1998 if (combined_after_bytes
)
2000 if (combined_before_bytes
== outgoing_insbytes
)
2001 /* This is the case that all new bytes are combined. */
2002 combined_before_bytes
+= combined_after_bytes
;
2004 combine_bytes (from
+ inschars
, from_byte
+ outgoing_insbytes
,
2005 combined_after_bytes
);
2007 if (combined_before_bytes
)
2008 combine_bytes (from
, from_byte
, combined_before_bytes
);
2010 /* As byte combining will decrease Z, we must check this again. */
2011 if (Z
- GPT
< end_unchanged
)
2012 end_unchanged
= Z
- GPT
;
2014 if (outgoing_insbytes
== 0)
2015 evaporate_overlays (from
);
2022 signal_after_change (from
, nchars_del
, GPT
- from
);
2025 /* Delete characters in current buffer
2026 from FROM up to (but not including) TO.
2027 If TO comes before FROM, we delete nothing. */
2030 del_range (from
, to
)
2031 register int from
, to
;
2033 del_range_1 (from
, to
, 1);
2036 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2039 del_range_1 (from
, to
, prepare
)
2040 int from
, to
, prepare
;
2042 int from_byte
, to_byte
;
2044 /* Make args be valid */
2055 int range_length
= to
- from
;
2056 prepare_to_modify_buffer (from
, to
, &from
);
2057 to
= from
+ range_length
;
2060 from_byte
= CHAR_TO_BYTE (from
);
2061 to_byte
= CHAR_TO_BYTE (to
);
2063 del_range_2 (from
, from_byte
, to
, to_byte
);
2066 /* Like del_range_1 but args are byte positions, not char positions. */
2069 del_range_byte (from_byte
, to_byte
, prepare
)
2070 int from_byte
, to_byte
, prepare
;
2074 /* Make args be valid */
2075 if (from_byte
< BEGV_BYTE
)
2076 from_byte
= BEGV_BYTE
;
2077 if (to_byte
> ZV_BYTE
)
2080 if (to_byte
<= from_byte
)
2083 from
= BYTE_TO_CHAR (from_byte
);
2084 to
= BYTE_TO_CHAR (to_byte
);
2088 int old_from
= from
, old_to
= Z
- to
;
2089 int range_length
= to
- from
;
2090 prepare_to_modify_buffer (from
, to
, &from
);
2091 to
= from
+ range_length
;
2093 if (old_from
!= from
)
2094 from_byte
= CHAR_TO_BYTE (from
);
2095 if (old_to
== Z
- to
)
2096 to_byte
= CHAR_TO_BYTE (to
);
2099 del_range_2 (from
, from_byte
, to
, to_byte
);
2102 /* Like del_range_1, but positions are specified both as charpos
2106 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
2107 int from
, from_byte
, to
, to_byte
, prepare
;
2109 /* Make args be valid */
2110 if (from_byte
< BEGV_BYTE
)
2111 from_byte
= BEGV_BYTE
;
2112 if (to_byte
> ZV_BYTE
)
2115 if (to_byte
<= from_byte
)
2125 int old_from
= from
, old_to
= Z
- to
;
2126 int range_length
= to
- from
;
2127 prepare_to_modify_buffer (from
, to
, &from
);
2128 to
= from
+ range_length
;
2130 if (old_from
!= from
)
2131 from_byte
= CHAR_TO_BYTE (from
);
2132 if (old_to
== Z
- to
)
2133 to_byte
= CHAR_TO_BYTE (to
);
2136 del_range_2 (from
, from_byte
, to
, to_byte
);
2139 /* Delete a range of text, specified both as character positions
2140 and byte positions. FROM and TO are character positions,
2141 while FROM_BYTE and TO_BYTE are byte positions. */
2144 del_range_2 (from
, from_byte
, to
, to_byte
)
2145 int from
, from_byte
, to
, to_byte
;
2147 register int nbytes_del
, nchars_del
;
2148 int combined_after_bytes
;
2149 Lisp_Object deletion
;
2154 nchars_del
= to
- from
;
2155 nbytes_del
= to_byte
- from_byte
;
2157 /* Make sure the gap is somewhere in or next to what we are deleting. */
2159 gap_right (from
, from_byte
);
2161 gap_left (to
, to_byte
, 0);
2163 combined_after_bytes
2164 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
2165 Z_BYTE
- to_byte
, from
, from_byte
);
2166 if (combined_after_bytes
)
2168 if (from
== BEGV
|| to
== ZV
)
2169 byte_combining_error ();
2170 from_byte_1
= from_byte
;
2171 DEC_POS (from_byte_1
);
2174 from_byte_1
= from_byte
;
2176 if (! EQ (current_buffer
->undo_list
, Qt
))
2178 = make_buffer_string_both (from
- !!combined_after_bytes
,
2180 to
+ combined_after_bytes
,
2181 to_byte
+ combined_after_bytes
, 1);
2182 if (combined_after_bytes
)
2183 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
2184 the gap. We must move the gap again to a proper place. */
2185 move_gap_both (from
, from_byte
);
2187 /* Relocate all markers pointing into the new, larger gap
2188 to point at the end of the text before the gap.
2189 Do this before recording the deletion,
2190 so that undo handles this after reinserting the text. */
2191 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
2192 if (combined_after_bytes
)
2194 /* Adjust markers for the phony deletion
2195 that we are about to call record_undo for. */
2197 /* Here we delete the markers that formerly
2198 pointed at TO ... TO + COMBINED_AFTER_BYTES.
2199 But because of the call to adjust_markers_for_delete, above,
2200 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
2201 adjust_markers_for_record_delete (from
, from_byte
,
2202 from
+ combined_after_bytes
,
2203 from_byte
+ combined_after_bytes
);
2205 adjust_markers_for_record_delete (from
- 1, from_byte_1
,
2208 if (! EQ (current_buffer
->undo_list
, Qt
))
2209 record_delete (from
- !!combined_after_bytes
, deletion
);
2212 /* Relocate point as if it were a marker. */
2214 adjust_point (from
- (PT
< to
? PT
: to
),
2215 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
2217 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2218 offset_intervals (current_buffer
, from
, - nchars_del
);
2220 /* Adjust the overlay center as needed. This must be done after
2221 adjusting the markers that bound the overlays. */
2222 adjust_overlays_for_delete (from
, nchars_del
);
2224 GAP_SIZE
+= nbytes_del
;
2225 ZV_BYTE
-= nbytes_del
;
2226 Z_BYTE
-= nbytes_del
;
2230 GPT_BYTE
= from_byte
;
2232 if (combined_after_bytes
)
2233 move_gap_both (GPT
+ combined_after_bytes
,
2234 GPT_BYTE
+ combined_after_bytes
);
2236 *(GPT_ADDR
) = 0; /* Put an anchor. */
2241 if (GPT
- BEG
< beg_unchanged
)
2242 beg_unchanged
= GPT
- BEG
;
2243 if (Z
- GPT
< end_unchanged
)
2244 end_unchanged
= Z
- GPT
;
2246 if (combined_after_bytes
)
2248 /* Adjust markers for byte combining. As we have already
2249 adjuted markers without concerning byte combining, here we
2250 must concern only byte combining. */
2251 adjust_markers_for_replace (from
, from_byte
, 0, 0, 0, 0,
2252 0, combined_after_bytes
);
2253 combine_bytes (from
, from_byte
, combined_after_bytes
);
2255 record_insert (GPT
- 1, 1);
2257 if (Z
- GPT
< end_unchanged
)
2258 end_unchanged
= Z
- GPT
;
2263 evaporate_overlays (from
);
2264 signal_after_change (from
, nchars_del
, 0);
2267 /* Call this if you're about to change the region of BUFFER from
2268 character positions START to END. This checks the read-only
2269 properties of the region, calls the necessary modification hooks,
2270 and warns the next redisplay that it should pay attention to that
2274 modify_region (buffer
, start
, end
)
2275 struct buffer
*buffer
;
2278 struct buffer
*old_buffer
= current_buffer
;
2280 if (buffer
!= old_buffer
)
2281 set_buffer_internal (buffer
);
2283 prepare_to_modify_buffer (start
, end
, NULL
);
2285 if (start
- 1 < beg_unchanged
2286 || (unchanged_modified
== MODIFF
2287 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
2288 beg_unchanged
= start
- 1;
2289 if (Z
- end
< end_unchanged
2290 || (unchanged_modified
== MODIFF
2291 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
2292 end_unchanged
= Z
- end
;
2294 if (MODIFF
<= SAVE_MODIFF
)
2295 record_first_change ();
2298 buffer
->point_before_scroll
= Qnil
;
2300 if (buffer
!= old_buffer
)
2301 set_buffer_internal (old_buffer
);
2304 /* Check that it is okay to modify the buffer between START and END,
2305 which are char positions.
2307 Run the before-change-function, if any. If intervals are in use,
2308 verify that the text to be modified is not read-only, and call
2309 any modification properties the text may have.
2311 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2312 by holding its value temporarily in a marker. */
2315 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
2319 if (!NILP (current_buffer
->read_only
))
2320 Fbarf_if_buffer_read_only ();
2322 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2323 if (BUF_INTERVALS (current_buffer
) != 0)
2327 Lisp_Object preserve_marker
;
2328 struct gcpro gcpro1
;
2329 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2330 GCPRO1 (preserve_marker
);
2331 verify_interval_modification (current_buffer
, start
, end
);
2332 *preserve_ptr
= marker_position (preserve_marker
);
2333 unchain_marker (preserve_marker
);
2337 verify_interval_modification (current_buffer
, start
, end
);
2340 #ifdef CLASH_DETECTION
2341 if (!NILP (current_buffer
->file_truename
)
2342 /* Make binding buffer-file-name to nil effective. */
2343 && !NILP (current_buffer
->filename
)
2344 && SAVE_MODIFF
>= MODIFF
)
2345 lock_file (current_buffer
->file_truename
);
2347 /* At least warn if this file has changed on disk since it was visited. */
2348 if (!NILP (current_buffer
->filename
)
2349 && SAVE_MODIFF
>= MODIFF
2350 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2351 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
2352 call1 (intern ("ask-user-about-supersession-threat"),
2353 current_buffer
->filename
);
2354 #endif /* not CLASH_DETECTION */
2356 signal_before_change (start
, end
, preserve_ptr
);
2358 if (current_buffer
->newline_cache
)
2359 invalidate_region_cache (current_buffer
,
2360 current_buffer
->newline_cache
,
2361 start
- BEG
, Z
- end
);
2362 if (current_buffer
->width_run_cache
)
2363 invalidate_region_cache (current_buffer
,
2364 current_buffer
->width_run_cache
,
2365 start
- BEG
, Z
- end
);
2367 Vdeactivate_mark
= Qt
;
2370 /* These macros work with an argument named `preserve_ptr'
2371 and a local variable named `preserve_marker'. */
2373 #define PRESERVE_VALUE \
2374 if (preserve_ptr && NILP (preserve_marker)) \
2375 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2377 #define RESTORE_VALUE \
2378 if (! NILP (preserve_marker)) \
2380 *preserve_ptr = marker_position (preserve_marker); \
2381 unchain_marker (preserve_marker); \
2384 #define PRESERVE_START_END \
2385 if (NILP (start_marker)) \
2386 start_marker = Fcopy_marker (start, Qnil); \
2387 if (NILP (end_marker)) \
2388 end_marker = Fcopy_marker (end, Qnil);
2390 #define FETCH_START \
2391 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2394 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2396 /* Signal a change to the buffer immediately before it happens.
2397 START_INT and END_INT are the bounds of the text to be changed.
2399 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2400 by holding its value temporarily in a marker. */
2403 signal_before_change (start_int
, end_int
, preserve_ptr
)
2404 int start_int
, end_int
;
2407 Lisp_Object start
, end
;
2408 Lisp_Object start_marker
, end_marker
;
2409 Lisp_Object preserve_marker
;
2410 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2412 if (inhibit_modification_hooks
)
2415 start
= make_number (start_int
);
2416 end
= make_number (end_int
);
2417 preserve_marker
= Qnil
;
2418 start_marker
= Qnil
;
2420 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2422 /* If buffer is unmodified, run a special hook for that case. */
2423 if (SAVE_MODIFF
>= MODIFF
2424 && !NILP (Vfirst_change_hook
)
2425 && !NILP (Vrun_hooks
))
2429 call1 (Vrun_hooks
, Qfirst_change_hook
);
2432 /* Run the before-change-function if any.
2433 We don't bother "binding" this variable to nil
2434 because it is obsolete anyway and new code should not use it. */
2435 if (!NILP (Vbefore_change_function
))
2439 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
2442 /* Now run the before-change-functions if any. */
2443 if (!NILP (Vbefore_change_functions
))
2445 Lisp_Object args
[3];
2446 Lisp_Object before_change_functions
;
2447 Lisp_Object after_change_functions
;
2448 struct gcpro gcpro1
, gcpro2
;
2453 /* "Bind" before-change-functions and after-change-functions
2454 to nil--but in a way that errors don't know about.
2455 That way, if there's an error in them, they will stay nil. */
2456 before_change_functions
= Vbefore_change_functions
;
2457 after_change_functions
= Vafter_change_functions
;
2458 Vbefore_change_functions
= Qnil
;
2459 Vafter_change_functions
= Qnil
;
2460 GCPRO2 (before_change_functions
, after_change_functions
);
2462 /* Actually run the hook functions. */
2463 args
[0] = Qbefore_change_functions
;
2464 args
[1] = FETCH_START
;
2465 args
[2] = FETCH_END
;
2466 run_hook_list_with_args (before_change_functions
, 3, args
);
2468 /* "Unbind" the variables we "bound" to nil. */
2469 Vbefore_change_functions
= before_change_functions
;
2470 Vafter_change_functions
= after_change_functions
;
2474 if (!NILP (current_buffer
->overlays_before
)
2475 || !NILP (current_buffer
->overlays_after
))
2478 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2479 FETCH_START
, FETCH_END
, Qnil
);
2482 if (! NILP (start_marker
))
2483 free_marker (start_marker
);
2484 if (! NILP (end_marker
))
2485 free_marker (end_marker
);
2490 /* Signal a change immediately after it happens.
2491 CHARPOS is the character position of the start of the changed text.
2492 LENDEL is the number of characters of the text before the change.
2493 (Not the whole buffer; just the part that was changed.)
2494 LENINS is the number of characters in that part of the text
2495 after the change. */
2498 signal_after_change (charpos
, lendel
, lenins
)
2499 int charpos
, lendel
, lenins
;
2501 if (inhibit_modification_hooks
)
2504 /* If we are deferring calls to the after-change functions
2505 and there are no before-change functions,
2506 just record the args that we were going to use. */
2507 if (! NILP (Vcombine_after_change_calls
)
2508 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
2509 && NILP (current_buffer
->overlays_before
)
2510 && NILP (current_buffer
->overlays_after
))
2514 if (!NILP (combine_after_change_list
)
2515 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2516 Fcombine_after_change_execute ();
2518 elt
= Fcons (make_number (charpos
- BEG
),
2519 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2520 Fcons (make_number (lenins
- lendel
), Qnil
)));
2521 combine_after_change_list
2522 = Fcons (elt
, combine_after_change_list
);
2523 combine_after_change_buffer
= Fcurrent_buffer ();
2528 if (!NILP (combine_after_change_list
))
2529 Fcombine_after_change_execute ();
2531 /* Run the after-change-function if any.
2532 We don't bother "binding" this variable to nil
2533 because it is obsolete anyway and new code should not use it. */
2534 if (!NILP (Vafter_change_function
))
2535 call3 (Vafter_change_function
,
2536 make_number (charpos
), make_number (charpos
+ lenins
),
2537 make_number (lendel
));
2539 if (!NILP (Vafter_change_functions
))
2541 Lisp_Object args
[4];
2542 Lisp_Object before_change_functions
;
2543 Lisp_Object after_change_functions
;
2544 struct gcpro gcpro1
, gcpro2
;
2546 /* "Bind" before-change-functions and after-change-functions
2547 to nil--but in a way that errors don't know about.
2548 That way, if there's an error in them, they will stay nil. */
2549 before_change_functions
= Vbefore_change_functions
;
2550 after_change_functions
= Vafter_change_functions
;
2551 Vbefore_change_functions
= Qnil
;
2552 Vafter_change_functions
= Qnil
;
2553 GCPRO2 (before_change_functions
, after_change_functions
);
2555 /* Actually run the hook functions. */
2556 args
[0] = Qafter_change_functions
;
2557 XSETFASTINT (args
[1], charpos
);
2558 XSETFASTINT (args
[2], charpos
+ lenins
);
2559 XSETFASTINT (args
[3], lendel
);
2560 run_hook_list_with_args (after_change_functions
,
2563 /* "Unbind" the variables we "bound" to nil. */
2564 Vbefore_change_functions
= before_change_functions
;
2565 Vafter_change_functions
= after_change_functions
;
2569 if (!NILP (current_buffer
->overlays_before
)
2570 || !NILP (current_buffer
->overlays_after
))
2571 report_overlay_modification (make_number (charpos
),
2572 make_number (charpos
+ lenins
),
2574 make_number (charpos
),
2575 make_number (charpos
+ lenins
),
2576 make_number (lendel
));
2578 /* After an insertion, call the text properties
2579 insert-behind-hooks or insert-in-front-hooks. */
2581 report_interval_modification (make_number (charpos
),
2582 make_number (charpos
+ lenins
));
2586 Fcombine_after_change_execute_1 (val
)
2589 Vcombine_after_change_calls
= val
;
2593 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2594 Scombine_after_change_execute
, 0, 0, 0,
2595 "This function is for use internally in `combine-after-change-calls'.")
2598 int count
= specpdl_ptr
- specpdl
;
2599 int beg
, end
, change
;
2603 if (NILP (combine_after_change_list
))
2606 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2608 Fset_buffer (combine_after_change_buffer
);
2610 /* # chars unchanged at beginning of buffer. */
2612 /* # chars unchanged at end of buffer. */
2614 /* Total amount of insertion (negative for deletion). */
2617 /* Scan the various individual changes,
2618 accumulating the range info in BEG, END and CHANGE. */
2619 for (tail
= combine_after_change_list
; CONSP (tail
);
2620 tail
= XCONS (tail
)->cdr
)
2623 int thisbeg
, thisend
, thischange
;
2625 /* Extract the info from the next element. */
2626 elt
= XCONS (tail
)->car
;
2629 thisbeg
= XINT (XCONS (elt
)->car
);
2631 elt
= XCONS (elt
)->cdr
;
2634 thisend
= XINT (XCONS (elt
)->car
);
2636 elt
= XCONS (elt
)->cdr
;
2639 thischange
= XINT (XCONS (elt
)->car
);
2641 /* Merge this range into the accumulated range. */
2642 change
+= thischange
;
2649 /* Get the current start and end positions of the range
2650 that was changed. */
2654 /* We are about to handle these, so discard them. */
2655 combine_after_change_list
= Qnil
;
2657 /* Now run the after-change functions for real.
2658 Turn off the flag that defers them. */
2659 record_unwind_protect (Fcombine_after_change_execute_1
,
2660 Vcombine_after_change_calls
);
2661 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2663 return unbind_to (count
, Qnil
);
2669 staticpro (&combine_after_change_list
);
2670 combine_after_change_list
= Qnil
;
2671 combine_after_change_buffer
= Qnil
;
2673 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2674 "Non-nil means enable debugging checks for invalid marker positions.");
2675 check_markers_debug_flag
= 0;
2676 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2677 "Used internally by the `combine-after-change-calls' macro.");
2678 Vcombine_after_change_calls
= Qnil
;
2680 defsubr (&Scombine_after_change_execute
);