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"
34 #define min(x, y) ((x) < (y) ? (x) : (y))
36 static void insert_from_string_1
P_ ((Lisp_Object
, int, int, int, int, int, int));
37 static void insert_from_buffer_1 ();
38 static void gap_left
P_ ((int, int, int));
39 static void gap_right
P_ ((int, int));
40 static void adjust_markers_gap_motion
P_ ((int, int, int));
41 static void adjust_markers_for_insert
P_ ((int, int, int, int, int, int, int));
42 static void adjust_markers_for_delete
P_ ((int, int, int, int));
43 static void adjust_point
P_ ((int, int));
45 Lisp_Object
Fcombine_after_change_execute ();
47 /* Non-nil means don't call the after-change-functions right away,
48 just record an element in Vcombine_after_change_calls_list. */
49 Lisp_Object Vcombine_after_change_calls
;
51 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
52 describing changes which happened while combine_after_change_calls
53 was nonzero. We use this to decide how to call them
54 once the deferral ends.
57 BEG-UNCHANGED is the number of chars before the changed range.
58 END-UNCHANGED is the number of chars after the changed range,
59 and CHANGE-AMOUNT is the number of characters inserted by the change
60 (negative for a deletion). */
61 Lisp_Object combine_after_change_list
;
63 /* Buffer which combine_after_change_list is about. */
64 Lisp_Object combine_after_change_buffer
;
66 /* Move gap to position CHARPOS.
67 Note that this can quit! */
73 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
76 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
77 Note that this can quit! */
80 move_gap_both (charpos
, bytepos
)
83 if (bytepos
< GPT_BYTE
)
84 gap_left (charpos
, bytepos
, 0);
85 else if (bytepos
> GPT_BYTE
)
86 gap_right (charpos
, bytepos
);
89 /* Move the gap to a position less than the current GPT.
90 BYTEPOS describes the new position as a byte position,
91 and CHARPOS is the corresponding char position.
92 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
95 gap_left (charpos
, bytepos
, newgap
)
96 register int charpos
, bytepos
;
99 register unsigned char *to
, *from
;
105 if (unchanged_modified
== MODIFF
106 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
108 beg_unchanged
= charpos
- BEG
;
109 end_unchanged
= Z
- charpos
;
113 if (Z
- GPT
< end_unchanged
)
114 end_unchanged
= Z
- GPT
;
115 if (charpos
< beg_unchanged
)
116 beg_unchanged
= charpos
- BEG
;
125 /* Now copy the characters. To move the gap down,
126 copy characters up. */
130 /* I gets number of characters left to copy. */
131 i
= new_s1
- bytepos
;
134 /* If a quit is requested, stop copying now.
135 Change BYTEPOS to be where we have actually moved the gap to. */
139 charpos
= BYTE_TO_CHAR (bytepos
);
142 /* Move at most 32000 chars before checking again for a quit. */
147 /* bcopy is safe if the two areas of memory do not overlap
148 or on systems where bcopy is always safe for moving upward. */
149 && (BCOPY_UPWARD_SAFE
150 || to
- from
>= 128))
152 /* If overlap is not safe, avoid it by not moving too many
153 characters at once. */
154 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
169 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
170 BYTEPOS is where the loop above stopped, which may be what was specified
171 or may be where a quit was detected. */
172 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
175 if (bytepos
< charpos
)
177 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
181 /* Move the gap to a position greater than than the current GPT.
182 BYTEPOS describes the new position as a byte position,
183 and CHARPOS is the corresponding char position. */
186 gap_right (charpos
, bytepos
)
187 register int charpos
, bytepos
;
189 register unsigned char *to
, *from
;
193 if (unchanged_modified
== MODIFF
194 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
196 beg_unchanged
= charpos
- BEG
;
197 end_unchanged
= Z
- charpos
;
201 if (Z
- charpos
- 1 < end_unchanged
)
202 end_unchanged
= Z
- charpos
;
203 if (GPT
- BEG
< beg_unchanged
)
204 beg_unchanged
= GPT
- BEG
;
212 /* Now copy the characters. To move the gap up,
213 copy characters down. */
217 /* I gets number of characters left to copy. */
218 i
= bytepos
- new_s1
;
221 /* If a quit is requested, stop copying now.
222 Change BYTEPOS to be where we have actually moved the gap to. */
226 charpos
= BYTE_TO_CHAR (bytepos
);
229 /* Move at most 32000 chars before checking again for a quit. */
234 /* bcopy is safe if the two areas of memory do not overlap
235 or on systems where bcopy is always safe for moving downward. */
236 && (BCOPY_DOWNWARD_SAFE
237 || from
- to
>= 128))
239 /* If overlap is not safe, avoid it by not moving too many
240 characters at once. */
241 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
256 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
260 if (bytepos
< charpos
)
262 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
266 /* Add AMOUNT to the byte position of every marker in the current buffer
267 whose current byte position is between FROM (exclusive) and TO (inclusive).
269 Also, any markers past the outside of that interval, in the direction
270 of adjustment, are first moved back to the near end of the interval
271 and then adjusted by AMOUNT.
273 When the latter adjustment is done, if AMOUNT is negative,
274 we record the adjustment for undo. (This case happens only for
277 The markers' character positions are not altered,
278 because gap motion does not affect character positions. */
280 int adjust_markers_test
;
283 adjust_markers_gap_motion (from
, to
, amount
)
284 register int from
, to
, amount
;
286 /* Now that a marker has a bytepos, not counting the gap,
287 nothing needs to be done here. */
290 register struct Lisp_Marker
*m
;
293 marker
= BUF_MARKERS (current_buffer
);
295 while (!NILP (marker
))
297 m
= XMARKER (marker
);
301 if (mpos
> to
&& mpos
< to
+ amount
)
303 if (adjust_markers_test
)
310 /* Here's the case where a marker is inside text being deleted.
311 AMOUNT can be negative for gap motion, too,
312 but then this range contains no markers. */
313 if (mpos
> from
+ amount
&& mpos
<= from
)
315 if (adjust_markers_test
)
317 mpos
= from
+ amount
;
320 if (mpos
> from
&& mpos
<= to
)
328 /* Adjust all markers for a deletion
329 whose range in bytes is FROM_BYTE to TO_BYTE.
330 The range in charpos is FROM to TO.
332 This function assumes that the gap is adjacent to
333 or inside of the range being deleted. */
336 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
337 register int from
, from_byte
, to
, to_byte
;
340 register struct Lisp_Marker
*m
;
341 register int charpos
;
342 /* This is what GAP_SIZE will be when this deletion is finished. */
343 int coming_gap_size
= GAP_SIZE
+ to_byte
- from_byte
;
345 marker
= BUF_MARKERS (current_buffer
);
347 while (!NILP (marker
))
349 m
= XMARKER (marker
);
350 charpos
= m
->charpos
;
355 /* If the marker is after the deletion,
356 relocate by number of chars / bytes deleted. */
359 m
->charpos
-= to
- from
;
360 m
->bytepos
-= to_byte
- from_byte
;
363 /* Here's the case where a marker is inside text being deleted. */
364 else if (charpos
> from
)
366 record_marker_adjustment (marker
, from
- charpos
);
368 m
->bytepos
= from_byte
;
371 /* In a single-byte buffer, a marker's two positions must be equal. */
374 register int i
= m
->bytepos
;
377 /* We use FROM_BYTE here instead of GPT_BYTE
378 because FROM_BYTE is where the gap will be after the deletion. */
379 if (i
> from_byte
+ coming_gap_size
)
380 i
-= coming_gap_size
;
381 else if (i
> from_byte
)
393 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
394 to TO / TO_BYTE. We have to relocate the charpos of every marker
395 that points after the insertion (but not their bytepos).
397 COMBINED_BEFORE_BYTES is the number of bytes before the insertion
398 that combines into one character with the first inserted bytes.
399 COMBINED_AFTER_BYTES is the number of bytes after the insertion
400 that combines into one character with the last inserted bytes.
402 When a marker points at the insertion point,
403 we advance it if either its insertion-type is t
404 or BEFORE_MARKERS is true. */
407 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
,
408 combined_before_bytes
, combined_after_bytes
,
410 register int from
, from_byte
, to
, to_byte
;
411 int combined_before_bytes
, combined_after_bytes
, before_markers
;
415 int nchars
= to
- from
;
416 int nbytes
= to_byte
- from_byte
;
418 marker
= BUF_MARKERS (current_buffer
);
420 while (!NILP (marker
))
422 register struct Lisp_Marker
*m
= XMARKER (marker
);
423 if (m
->bytepos
== from_byte
)
425 if (m
->insertion_type
|| before_markers
)
427 m
->bytepos
+= nbytes
+ combined_after_bytes
;
428 m
->charpos
+= nchars
+ !!combined_after_bytes
;
429 /* Point the marker before the combined character,
430 so that undoing the insertion puts it back where it was. */
431 if (combined_after_bytes
)
432 DEC_BOTH (m
->charpos
, m
->bytepos
);
433 if (m
->insertion_type
)
436 else if (combined_before_bytes
)
438 /* This marker doesn't "need relocation",
439 but don't leave it pointing in the middle of a character.
440 Point the marker after the combined character,
441 so that undoing the insertion puts it back where it was. */
442 m
->bytepos
-= combined_before_bytes
;
444 INC_BOTH (m
->charpos
, m
->bytepos
);
447 else if (m
->bytepos
> from_byte
)
449 m
->bytepos
+= nbytes
;
450 m
->charpos
+= nchars
;
453 /* In a single-byte buffer, a marker's two positions must be equal. */
456 if (m
->charpos
!= m
->bytepos
)
463 /* Adjusting only markers whose insertion-type is t may result in
464 disordered overlays in the slot `overlays_before'. */
466 fix_overlays_before (current_buffer
, from
, to
);
469 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
471 This is used only when the value of point changes due to an insert
472 or delete; it does not represent a conceptual change in point as a
473 marker. In particular, point is not crossing any interval
474 boundaries, so there's no need to use the usual SET_PT macro. In
475 fact it would be incorrect to do so, because either the old or the
476 new value of point is out of sync with the current set of
480 adjust_point (nchars
, nbytes
)
483 BUF_PT (current_buffer
) += nchars
;
484 BUF_PT_BYTE (current_buffer
) += nbytes
;
486 /* In a single-byte buffer, the two positions must be equal. */
492 /* Make the gap NBYTES_ADDED bytes longer. */
495 make_gap (nbytes_added
)
498 unsigned char *result
;
501 int real_gap_loc_byte
;
504 /* If we have to get more space, get enough to last a while. */
505 nbytes_added
+= 2000;
507 /* Don't allow a buffer size that won't fit in an int
508 even if it will fit in a Lisp integer.
509 That won't work because so many places use `int'. */
511 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
512 >= ((unsigned) 1 << (min (BITS_PER_INT
, VALBITS
) - 1)))
513 error ("Buffer exceeds maximum size");
516 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
517 result
= BUFFER_REALLOC (BEG_ADDR
, (Z_BYTE
- BEG_BYTE
518 + GAP_SIZE
+ nbytes_added
+ 1));
526 /* We can't unblock until the new address is properly stored. */
530 /* Prevent quitting in move_gap. */
535 real_gap_loc_byte
= GPT_BYTE
;
536 old_gap_size
= GAP_SIZE
;
538 /* Call the newly allocated space a gap at the end of the whole space. */
540 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
541 GAP_SIZE
= nbytes_added
;
543 /* Move the new gap down to be consecutive with the end of the old one.
544 This adjusts the markers properly too. */
545 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
547 /* Now combine the two into one large gap. */
548 GAP_SIZE
+= old_gap_size
;
550 GPT_BYTE
= real_gap_loc_byte
;
558 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
559 FROM_MULTIBYTE says whether the incoming text is multibyte.
560 TO_MULTIBYTE says whether to store the text as multibyte.
561 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
563 Return the number of bytes stored at TO_ADDR. */
566 copy_text (from_addr
, to_addr
, nbytes
,
567 from_multibyte
, to_multibyte
)
568 unsigned char *from_addr
;
569 unsigned char *to_addr
;
571 int from_multibyte
, to_multibyte
;
573 if (from_multibyte
== to_multibyte
)
575 bcopy (from_addr
, to_addr
, nbytes
);
578 else if (from_multibyte
)
581 int bytes_left
= nbytes
;
583 /* Convert multibyte to single byte. */
584 while (bytes_left
> 0)
587 c
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
588 *to_addr
++ = SINGLE_BYTE_CHAR_P (c
) ? c
: (c
& 0177) + 0200;
589 from_addr
+= thislen
;
597 unsigned char *initial_to_addr
= to_addr
;
599 /* Convert single-byte to multibyte. */
602 int c
= *from_addr
++;
603 unsigned char workbuf
[4], *str
;
606 if (c
>= 0240 && c
< 0400)
608 c
= unibyte_char_to_multibyte (c
);
609 len
= CHAR_STRING (c
, workbuf
, str
);
610 bcopy (str
, to_addr
, len
);
615 /* Special case for speed. */
616 *to_addr
++ = c
, nbytes
--;
618 return to_addr
- initial_to_addr
;
622 /* Return the number of bytes it would take
623 to convert some single-byte text to multibyte.
624 The single-byte text consists of NBYTES bytes at PTR. */
627 count_size_as_multibyte (ptr
, nbytes
)
632 int outgoing_nbytes
= 0;
634 for (i
= 0; i
< nbytes
; i
++)
636 unsigned int c
= *ptr
++;
642 c
= unibyte_char_to_multibyte (c
);
643 outgoing_nbytes
+= XINT (Fchar_bytes (make_number (c
)));
647 return outgoing_nbytes
;
650 /* Insert a string of specified length before point.
651 This function judges multibyteness based on
652 enable_multibyte_characters in the current buffer;
653 it never converts between single-byte and multibyte.
655 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
656 prepare_to_modify_buffer could relocate the text. */
659 insert (string
, nbytes
)
660 register unsigned char *string
;
666 insert_1 (string
, nbytes
, 0, 1, 0);
667 signal_after_change (opoint
, 0, PT
- opoint
);
671 /* Likewise, but inherit text properties from neighboring characters. */
674 insert_and_inherit (string
, nbytes
)
675 register unsigned char *string
;
681 insert_1 (string
, nbytes
, 1, 1, 0);
682 signal_after_change (opoint
, 0, PT
- opoint
);
686 /* Insert the character C before point. Do not inherit text properties. */
692 unsigned char workbuf
[4], *str
;
695 if (! NILP (current_buffer
->enable_multibyte_characters
))
696 len
= CHAR_STRING (c
, workbuf
, str
);
707 /* Insert the null-terminated string S before point. */
713 insert (s
, strlen (s
));
716 /* Like `insert' except that all markers pointing at the place where
717 the insertion happens are adjusted to point after it.
718 Don't use this function to insert part of a Lisp string,
719 since gc could happen and relocate it. */
722 insert_before_markers (string
, nbytes
)
723 unsigned char *string
;
730 insert_1 (string
, nbytes
, 0, 1, 1);
731 signal_after_change (opoint
, 0, PT
- opoint
);
735 /* Likewise, but inherit text properties from neighboring characters. */
738 insert_before_markers_and_inherit (string
, nbytes
)
739 unsigned char *string
;
746 insert_1 (string
, nbytes
, 1, 1, 1);
747 signal_after_change (opoint
, 0, PT
- opoint
);
751 /* Subroutine used by the insert functions above. */
754 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
755 register unsigned char *string
;
757 int inherit
, prepare
, before_markers
;
759 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
760 inherit
, prepare
, before_markers
);
763 /* See if the bytes before POS/POS_BYTE combine with bytes
764 at the start of STRING to form a single character.
765 If so, return the number of bytes before POS/POS_BYTE
766 which combine in this way. Otherwise, return 0. */
769 count_combining_before (string
, length
, pos
, pos_byte
)
770 unsigned char *string
;
774 int opos
= pos
, opos_byte
= pos_byte
;
777 if (NILP (current_buffer
->enable_multibyte_characters
))
779 if (length
== 0 || CHAR_HEAD_P (*string
))
783 c
= FETCH_BYTE (pos_byte
- 1);
784 if (ASCII_BYTE_P (c
))
786 DEC_BOTH (pos
, pos_byte
);
787 c
= FETCH_BYTE (pos_byte
);
788 if (! BASE_LEADING_CODE_P (c
))
790 return opos_byte
- pos_byte
;
793 /* See if the bytes after POS/POS_BYTE combine with bytes
794 at the end of STRING to form a single character.
795 If so, return the number of bytes after POS/POS_BYTE
796 which combine in this way. Otherwise, return 0. */
799 count_combining_after (string
, length
, pos
, pos_byte
)
800 unsigned char *string
;
804 int opos
= pos
, opos_byte
= pos_byte
;
808 if (NILP (current_buffer
->enable_multibyte_characters
))
810 if (length
== 0 || ASCII_BYTE_P (string
[length
- 1]))
813 while (i
> 0 && ! CHAR_HEAD_P (string
[i
]))
817 if (! BASE_LEADING_CODE_P (string
[i
]))
822 c
= FETCH_BYTE (pos_byte
);
825 while (pos_byte
< ZV_BYTE
)
827 c
= FETCH_BYTE (pos_byte
);
833 return pos_byte
- opos_byte
;
836 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
837 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
838 are the same as in insert_1. */
841 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
842 register unsigned char *string
;
843 register int nchars
, nbytes
;
844 int inherit
, prepare
, before_markers
;
846 register Lisp_Object temp
;
847 int combined_before_bytes
, combined_after_bytes
;
850 if (NILP (current_buffer
->enable_multibyte_characters
))
854 move_gap_both (PT
, PT_BYTE
);
855 if (GAP_SIZE
< nbytes
)
856 make_gap (nbytes
- GAP_SIZE
);
858 combined_before_bytes
= count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
859 combined_after_bytes
= count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
861 /* This is the net amount that Z will increase from this insertion. */
862 /* The combined bytes before all count as one character, because
863 they start with a leading code, but the combined bytes after
864 count as separate characters, because they are all trailing codes. */
866 adjusted_nchars
= nchars
- !!combined_before_bytes
- combined_after_bytes
;
869 prepare_to_modify_buffer (PT
- !!combined_before_bytes
,
870 PT
+ combined_after_bytes
,
873 /* Record deletion of the surrounding text that combines with
874 the insertion. This, together with recording the insertion,
875 will add up to the right stuff in the undo list.
877 But there is no need to actually delete the combining bytes
878 from the buffer and reinsert them. */
880 if (combined_after_bytes
)
881 record_delete (PT
, combined_after_bytes
);
883 if (combined_before_bytes
)
884 record_delete (PT
- 1, 1);
886 record_insert (PT
- !!combined_before_bytes
, nchars
);
889 bcopy (string
, GPT_ADDR
, nbytes
);
891 #ifdef USE_TEXT_PROPERTIES
892 if (BUF_INTERVALS (current_buffer
) != 0)
893 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
894 offset_intervals (current_buffer
, PT
, adjusted_nchars
);
898 GPT
+= nchars
- !! combined_before_bytes
- !!combined_after_bytes
;
899 ZV
+= adjusted_nchars
;
900 Z
+= adjusted_nchars
;
904 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
905 adjust_overlays_for_insert (PT
, adjusted_nchars
);
906 adjust_markers_for_insert (PT
, PT_BYTE
,
907 PT
+ adjusted_nchars
, PT_BYTE
+ nbytes
,
908 combined_before_bytes
, combined_after_bytes
,
910 adjust_point (adjusted_nchars
+ combined_after_bytes
,
911 nbytes
+ combined_after_bytes
);
913 if (combined_after_bytes
)
914 move_gap_both (GPT
+ 1, GPT_BYTE
+ combined_after_bytes
);
919 #ifdef USE_TEXT_PROPERTIES
920 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
921 Fset_text_properties (make_number (PT
- adjusted_nchars
), make_number (PT
),
926 /* Insert the part of the text of STRING, a Lisp object assumed to be
927 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
928 starting at position POS / POS_BYTE. If the text of STRING has properties,
929 copy them into the buffer.
931 It does not work to use `insert' for this, because a GC could happen
932 before we bcopy the stuff into the buffer, and relocate the string
933 without insert noticing. */
936 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
938 register int pos
, pos_byte
, length
, length_byte
;
944 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
946 signal_after_change (opoint
, 0, PT
- opoint
);
950 /* Like `insert_from_string' except that all markers pointing
951 at the place where the insertion happens are adjusted to point after it. */
954 insert_from_string_before_markers (string
, pos
, pos_byte
,
955 length
, length_byte
, inherit
)
957 register int pos
, pos_byte
, length
, length_byte
;
963 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
965 signal_after_change (opoint
, 0, PT
- opoint
);
969 /* Subroutine of the insertion functions above. */
972 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
973 inherit
, before_markers
)
975 register int pos
, pos_byte
, nchars
, nbytes
;
976 int inherit
, before_markers
;
978 register Lisp_Object temp
;
980 int outgoing_nbytes
= nbytes
;
981 int combined_before_bytes
, combined_after_bytes
;
984 /* Make OUTGOING_NBYTES describe the text
985 as it will be inserted in this buffer. */
987 if (NILP (current_buffer
->enable_multibyte_characters
))
988 outgoing_nbytes
= nchars
;
989 else if (nchars
== nbytes
)
991 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
994 /* Make sure point-max won't overflow after this insertion. */
995 XSETINT (temp
, outgoing_nbytes
+ Z
);
996 if (outgoing_nbytes
+ Z
!= XINT (temp
))
997 error ("Maximum buffer size exceeded");
1000 prepare_to_modify_buffer (PT
, PT
, NULL
);
1003 move_gap_both (PT
, PT_BYTE
);
1004 if (GAP_SIZE
< nbytes
)
1005 make_gap (outgoing_nbytes
- GAP_SIZE
);
1008 /* Copy the string text into the buffer, perhaps converting
1009 between single-byte and multibyte. */
1010 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1011 /* If these are equal, it is a single-byte string.
1012 Its chars are either ASCII, in which case copy_text
1013 won't change it, or single-byte non-ASCII chars,
1014 that need to be changed. */
1016 ! NILP (current_buffer
->enable_multibyte_characters
));
1018 /* We have copied text into the gap, but we have not altered
1019 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1020 to these functions and get the same results as we would
1021 have got earlier on. Meanwhile, PT_ADDR does point to
1022 the text that has been stored by copy_text. */
1024 combined_before_bytes
1025 = count_combining_before (XSTRING (string
)->data
+ pos_byte
, nbytes
,
1027 combined_after_bytes
1028 = count_combining_after (XSTRING (string
)->data
+ pos_byte
, nbytes
,
1031 /* This is the net amount that Z will increase from this insertion. */
1032 adjusted_nchars
= nchars
- !!combined_before_bytes
- combined_after_bytes
;
1034 /* Record deletion of the surrounding text that combines with
1035 the insertion. This, together with recording the insertion,
1036 will add up to the right stuff in the undo list.
1038 But there is no need to actually delete the combining bytes
1039 from the buffer and reinsert them. */
1041 if (combined_after_bytes
)
1042 record_delete (PT
, combined_after_bytes
);
1044 if (combined_before_bytes
)
1045 record_delete (PT
- 1, 1);
1047 record_insert (PT
- !!combined_before_bytes
, nchars
);
1050 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1051 offset_intervals (current_buffer
, PT
, adjusted_nchars
);
1053 GAP_SIZE
-= outgoing_nbytes
;
1054 GPT
+= nchars
- !!combined_before_bytes
- !!combined_after_bytes
;
1055 ZV
+= adjusted_nchars
;
1056 Z
+= adjusted_nchars
;
1057 GPT_BYTE
+= outgoing_nbytes
;
1058 ZV_BYTE
+= outgoing_nbytes
;
1059 Z_BYTE
+= outgoing_nbytes
;
1060 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1061 adjust_overlays_for_insert (PT
, adjusted_nchars
);
1062 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ adjusted_nchars
,
1063 PT_BYTE
+ outgoing_nbytes
,
1064 combined_before_bytes
, combined_after_bytes
,
1067 if (combined_after_bytes
)
1068 move_gap_both (GPT
+ 1, GPT_BYTE
+ combined_after_bytes
);
1073 graft_intervals_into_buffer (XSTRING (string
)->intervals
, PT
, nchars
,
1074 current_buffer
, inherit
);
1075 adjust_point (adjusted_nchars
+ combined_after_bytes
,
1076 outgoing_nbytes
+ combined_after_bytes
);
1079 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1080 current buffer. If the text in BUF has properties, they are absorbed
1081 into the current buffer.
1083 It does not work to use `insert' for this, because a malloc could happen
1084 and relocate BUF's text before the bcopy happens. */
1087 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1089 int charpos
, nchars
;
1096 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1097 signal_after_change (opoint
, 0, PT
- opoint
);
1102 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1107 register Lisp_Object temp
;
1109 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1110 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1111 int incoming_nbytes
= to_byte
- from_byte
;
1112 int outgoing_nbytes
= incoming_nbytes
;
1113 int combined_before_bytes
, combined_after_bytes
;
1114 int adjusted_nchars
;
1116 /* Make OUTGOING_NBYTES describe the text
1117 as it will be inserted in this buffer. */
1119 if (NILP (current_buffer
->enable_multibyte_characters
))
1120 outgoing_nbytes
= nchars
;
1121 else if (NILP (buf
->enable_multibyte_characters
))
1123 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1126 /* Make sure point-max won't overflow after this insertion. */
1127 XSETINT (temp
, outgoing_nbytes
+ Z
);
1128 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1129 error ("Maximum buffer size exceeded");
1131 prepare_to_modify_buffer (PT
, PT
, NULL
);
1134 move_gap_both (PT
, PT_BYTE
);
1135 if (GAP_SIZE
< outgoing_nbytes
)
1136 make_gap (outgoing_nbytes
- GAP_SIZE
);
1138 if (from
< BUF_GPT (buf
))
1140 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1141 if (chunk
> incoming_nbytes
)
1142 chunk
= incoming_nbytes
;
1143 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1145 ! NILP (buf
->enable_multibyte_characters
),
1146 ! NILP (current_buffer
->enable_multibyte_characters
));
1150 if (chunk
< incoming_nbytes
)
1151 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1152 GPT_ADDR
+ chunk
, incoming_nbytes
- chunk
,
1153 ! NILP (buf
->enable_multibyte_characters
),
1154 ! NILP (current_buffer
->enable_multibyte_characters
));
1156 /* We have copied text into the gap, but we have not altered
1157 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1158 to these functions and get the same results as we would
1159 have got earlier on. Meanwhile, PT_ADDR does point to
1160 the text that has been stored by copy_text. */
1161 combined_before_bytes
1162 = count_combining_before (PT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1163 combined_after_bytes
1164 = count_combining_after (PT_ADDR
, outgoing_nbytes
,
1167 /* This is the net amount that Z will increase from this insertion. */
1168 adjusted_nchars
= nchars
- !!combined_before_bytes
- combined_after_bytes
;
1170 /* Record deletion of the surrounding text that combines with
1171 the insertion. This, together with recording the insertion,
1172 will add up to the right stuff in the undo list.
1174 But there is no need to actually delete the combining bytes
1175 from the buffer and reinsert them. */
1177 if (combined_after_bytes
)
1178 record_delete (PT
, combined_after_bytes
);
1180 if (combined_before_bytes
)
1181 record_delete (PT
- 1, 1);
1183 record_insert (PT
- !!combined_before_bytes
, nchars
);
1186 #ifdef USE_TEXT_PROPERTIES
1187 if (BUF_INTERVALS (current_buffer
) != 0)
1188 offset_intervals (current_buffer
, PT
, adjusted_nchars
);
1191 GAP_SIZE
-= outgoing_nbytes
;
1192 GPT
+= nchars
- !!combined_before_bytes
- !!combined_after_bytes
;
1193 ZV
+= adjusted_nchars
;
1194 Z
+= adjusted_nchars
;
1195 GPT_BYTE
+= outgoing_nbytes
;
1196 ZV_BYTE
+= outgoing_nbytes
;
1197 Z_BYTE
+= outgoing_nbytes
;
1198 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1199 adjust_overlays_for_insert (PT
, adjusted_nchars
);
1200 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ adjusted_nchars
,
1201 PT_BYTE
+ outgoing_nbytes
,
1202 combined_before_bytes
, combined_after_bytes
, 0);
1203 adjust_point (adjusted_nchars
+ combined_after_bytes
,
1204 outgoing_nbytes
+ combined_after_bytes
);
1206 if (combined_after_bytes
)
1207 move_gap_both (GPT
+ 1, GPT_BYTE
+ combined_after_bytes
);
1212 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1213 graft_intervals_into_buffer (copy_intervals (BUF_INTERVALS (buf
),
1215 PT
- adjusted_nchars
, adjusted_nchars
,
1216 current_buffer
, inherit
);
1219 /* This function should be called after moving gap to FROM and before
1220 altering text between FROM and TO. This adjusts various position
1221 keepers and markers as if the text is deleted. Don't forget to
1222 call adjust_after_replace after you actually alter the text. */
1225 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1226 int from
, from_byte
, to
, to_byte
;
1228 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1229 record_delete (from
, to
- from
);
1232 /* This function should be called after altering the text between FROM
1233 and TO to a new text of LEN chars (LEN_BYTE bytes).
1234 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES are the number
1235 of bytes before (resp. after) the change which combine with
1236 the beginning or end of the replacement text to form one character. */
1239 adjust_after_replace (from
, from_byte
, to
, to_byte
, len
, len_byte
,
1240 combined_before_bytes
, combined_after_bytes
)
1241 int from
, from_byte
, to
, to_byte
, len
, len_byte
;
1242 int combined_before_bytes
, combined_after_bytes
;
1244 int adjusted_nchars
= len
- !!combined_before_bytes
- combined_after_bytes
;
1245 record_insert (from
- !!combined_before_bytes
, len
);
1247 adjust_point (len
- (to
- from
) + combined_after_bytes
,
1248 len_byte
- (to_byte
- from_byte
) + combined_after_bytes
);
1249 #ifdef USE_TEXT_PROPERTIES
1250 offset_intervals (current_buffer
, PT
, adjusted_nchars
- (to
- from
));
1252 adjust_overlays_for_delete (from
, to
- from
);
1253 adjust_overlays_for_insert (from
, adjusted_nchars
);
1254 adjust_markers_for_insert (from
, from_byte
,
1255 from
+ adjusted_nchars
, from_byte
+ len_byte
,
1256 combined_before_bytes
, combined_after_bytes
, 0);
1258 evaporate_overlays (from
);
1262 /* Replace the text from character positions FROM to TO with NEW,
1263 If PREPARE is nonzero, call prepare_to_modify_buffer.
1264 If INHERIT, the newly inserted text should inherit text properties
1265 from the surrounding non-deleted text. */
1267 /* Note that this does not yet handle markers quite right.
1268 Also it needs to record a single undo-entry that does a replacement
1269 rather than a separate delete and insert.
1270 That way, undo will also handle markers properly. */
1273 replace_range (from
, to
, new, prepare
, inherit
)
1275 int from
, to
, prepare
, inherit
;
1277 int inschars
= XSTRING (new)->size
;
1278 int insbytes
= XSTRING (new)->size_byte
;
1279 int from_byte
, to_byte
;
1280 int nbytes_del
, nchars_del
;
1281 register Lisp_Object temp
;
1282 struct gcpro gcpro1
;
1283 int combined_before_bytes
, combined_after_bytes
;
1284 int adjusted_inschars
;
1290 int range_length
= to
- from
;
1291 prepare_to_modify_buffer (from
, to
, &from
);
1292 to
= from
+ range_length
;
1297 /* Make args be valid */
1303 from_byte
= CHAR_TO_BYTE (from
);
1304 to_byte
= CHAR_TO_BYTE (to
);
1306 nchars_del
= to
- from
;
1307 nbytes_del
= to_byte
- from_byte
;
1309 if (nbytes_del
<= 0 && insbytes
== 0)
1312 /* Make sure point-max won't overflow after this insertion. */
1313 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1314 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1315 error ("Maximum buffer size exceeded");
1319 /* Make sure the gap is somewhere in or next to what we are deleting. */
1321 gap_right (from
, from_byte
);
1323 gap_left (to
, to_byte
, 0);
1325 /* Relocate all markers pointing into the new, larger gap
1326 to point at the end of the text before the gap.
1327 Do this before recording the deletion,
1328 so that undo handles this after reinserting the text. */
1329 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1331 record_delete (from
, nchars_del
);
1333 GAP_SIZE
+= nbytes_del
;
1336 ZV_BYTE
-= nbytes_del
;
1337 Z_BYTE
-= nbytes_del
;
1339 GPT_BYTE
= from_byte
;
1340 *(GPT_ADDR
) = 0; /* Put an anchor. */
1345 if (GPT
- BEG
< beg_unchanged
)
1346 beg_unchanged
= GPT
- BEG
;
1347 if (Z
- GPT
< end_unchanged
)
1348 end_unchanged
= Z
- GPT
;
1350 if (GAP_SIZE
< insbytes
)
1351 make_gap (insbytes
- GAP_SIZE
);
1353 /* We have copied text into the gap, but we have not altered
1354 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1355 to these functions and get the same results as we would
1356 have got earlier on. Meanwhile, PT_ADDR does point to
1357 the text that has been stored by copy_text. */
1359 combined_before_bytes
1360 = count_combining_before (XSTRING (new)->data
, insbytes
, PT
, PT_BYTE
);
1361 combined_after_bytes
1362 = count_combining_after (XSTRING (new)->data
, insbytes
, PT
, PT_BYTE
);
1364 /* This is the net amount that Z will increase from this insertion. */
1366 = inschars
- !!combined_before_bytes
- combined_after_bytes
;
1368 /* Record deletion of the surrounding text that combines with
1369 the insertion. This, together with recording the insertion,
1370 will add up to the right stuff in the undo list.
1372 But there is no need to actually delete the combining bytes
1373 from the buffer and reinsert them. */
1375 if (combined_after_bytes
)
1376 record_delete (PT
, combined_after_bytes
);
1378 if (combined_before_bytes
)
1379 record_delete (PT
- 1, 1);
1381 record_insert (from
, inschars
);
1383 bcopy (XSTRING (new)->data
, GPT_ADDR
, insbytes
);
1385 /* Relocate point as if it were a marker. */
1387 adjust_point ((from
+ adjusted_inschars
- (PT
< to
? PT
: to
)
1388 + combined_after_bytes
),
1389 (from_byte
+ insbytes
1390 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)
1391 + combined_after_bytes
));
1393 #ifdef USE_TEXT_PROPERTIES
1394 offset_intervals (current_buffer
, PT
, adjusted_inschars
- nchars_del
);
1397 GAP_SIZE
-= insbytes
;
1398 GPT
+= inschars
- !!combined_before_bytes
- !!combined_after_bytes
;
1399 ZV
+= adjusted_inschars
;
1400 Z
+= adjusted_inschars
;
1401 GPT_BYTE
+= insbytes
;
1402 ZV_BYTE
+= insbytes
;
1403 ZV_BYTE
+= insbytes
;
1404 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1409 /* Adjust the overlay center as needed. This must be done after
1410 adjusting the markers that bound the overlays. */
1411 adjust_overlays_for_delete (from
, nchars_del
);
1412 adjust_overlays_for_insert (from
, adjusted_inschars
);
1413 adjust_markers_for_insert (from
, from_byte
, from
+ adjusted_inschars
,
1414 from_byte
+ insbytes
,
1415 combined_before_bytes
, combined_after_bytes
, 0);
1417 #ifdef USE_TEXT_PROPERTIES
1418 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1419 graft_intervals_into_buffer (XSTRING (new)->intervals
, from
,
1420 inschars
, current_buffer
, inherit
);
1424 evaporate_overlays (from
);
1426 if (combined_after_bytes
)
1427 move_gap_both (GPT
+ 1, GPT_BYTE
+ combined_after_bytes
);
1432 signal_after_change (from
, nchars_del
, adjusted_inschars
);
1435 /* Delete characters in current buffer
1436 from FROM up to (but not including) TO.
1437 If TO comes before FROM, we delete nothing. */
1440 del_range (from
, to
)
1441 register int from
, to
;
1443 del_range_1 (from
, to
, 1);
1446 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
1449 del_range_1 (from
, to
, prepare
)
1450 int from
, to
, prepare
;
1452 int from_byte
, to_byte
;
1454 /* Make args be valid */
1465 int range_length
= to
- from
;
1466 prepare_to_modify_buffer (from
, to
, &from
);
1467 to
= from
+ range_length
;
1470 from_byte
= CHAR_TO_BYTE (from
);
1471 to_byte
= CHAR_TO_BYTE (to
);
1473 del_range_2 (from
, from_byte
, to
, to_byte
);
1476 /* Like del_range_1 but args are byte positions, not char positions. */
1479 del_range_byte (from_byte
, to_byte
, prepare
)
1480 int from_byte
, to_byte
, prepare
;
1484 /* Make args be valid */
1485 if (from_byte
< BEGV_BYTE
)
1486 from_byte
= BEGV_BYTE
;
1487 if (to_byte
> ZV_BYTE
)
1490 if (to_byte
<= from_byte
)
1493 from
= BYTE_TO_CHAR (from_byte
);
1494 to
= BYTE_TO_CHAR (to_byte
);
1498 int old_from
= from
, old_to
= Z
- to
;
1499 int range_length
= to
- from
;
1500 prepare_to_modify_buffer (from
, to
, &from
);
1501 to
= from
+ range_length
;
1503 if (old_from
!= from
)
1504 from_byte
= CHAR_TO_BYTE (from
);
1505 if (old_to
== Z
- to
)
1506 to_byte
= CHAR_TO_BYTE (to
);
1509 del_range_2 (from
, from_byte
, to
, to_byte
);
1512 /* Like del_range_1, but positions are specified both as charpos
1516 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
1517 int from
, from_byte
, to
, to_byte
, prepare
;
1519 /* Make args be valid */
1520 if (from_byte
< BEGV_BYTE
)
1521 from_byte
= BEGV_BYTE
;
1522 if (to_byte
> ZV_BYTE
)
1525 if (to_byte
<= from_byte
)
1535 int old_from
= from
, old_to
= Z
- to
;
1536 int range_length
= to
- from
;
1537 prepare_to_modify_buffer (from
, to
, &from
);
1538 to
= from
+ range_length
;
1540 if (old_from
!= from
)
1541 from_byte
= CHAR_TO_BYTE (from
);
1542 if (old_to
== Z
- to
)
1543 to_byte
= CHAR_TO_BYTE (to
);
1546 del_range_2 (from
, from_byte
, to
, to_byte
);
1549 /* Delete a range of text, specified both as character positions
1550 and byte positions. FROM and TO are character positions,
1551 while FROM_BYTE and TO_BYTE are byte positions. */
1554 del_range_2 (from
, from_byte
, to
, to_byte
)
1555 int from
, from_byte
, to
, to_byte
;
1557 register int nbytes_del
, nchars_del
;
1559 nchars_del
= to
- from
;
1560 nbytes_del
= to_byte
- from_byte
;
1562 /* Make sure the gap is somewhere in or next to what we are deleting. */
1564 gap_right (from
, from_byte
);
1566 gap_left (to
, to_byte
, 0);
1568 /* Relocate all markers pointing into the new, larger gap
1569 to point at the end of the text before the gap.
1570 Do this before recording the deletion,
1571 so that undo handles this after reinserting the text. */
1572 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1574 record_delete (from
, nchars_del
);
1577 /* Relocate point as if it were a marker. */
1579 adjust_point (from
- (PT
< to
? PT
: to
),
1580 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1582 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1583 offset_intervals (current_buffer
, from
, - nchars_del
);
1585 /* Adjust the overlay center as needed. This must be done after
1586 adjusting the markers that bound the overlays. */
1587 adjust_overlays_for_delete (from_byte
, nchars_del
);
1589 GAP_SIZE
+= nbytes_del
;
1590 ZV_BYTE
-= nbytes_del
;
1591 Z_BYTE
-= nbytes_del
;
1595 GPT_BYTE
= from_byte
;
1596 *(GPT_ADDR
) = 0; /* Put an anchor. */
1601 if (GPT
- BEG
< beg_unchanged
)
1602 beg_unchanged
= GPT
- BEG
;
1603 if (Z
- GPT
< end_unchanged
)
1604 end_unchanged
= Z
- GPT
;
1606 evaporate_overlays (from
);
1607 signal_after_change (from
, nchars_del
, 0);
1610 /* Call this if you're about to change the region of BUFFER from
1611 character positions START to END. This checks the read-only
1612 properties of the region, calls the necessary modification hooks,
1613 and warns the next redisplay that it should pay attention to that
1617 modify_region (buffer
, start
, end
)
1618 struct buffer
*buffer
;
1621 struct buffer
*old_buffer
= current_buffer
;
1623 if (buffer
!= old_buffer
)
1624 set_buffer_internal (buffer
);
1626 prepare_to_modify_buffer (start
, end
, NULL
);
1628 if (start
- 1 < beg_unchanged
1629 || (unchanged_modified
== MODIFF
1630 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
1631 beg_unchanged
= start
- 1;
1632 if (Z
- end
< end_unchanged
1633 || (unchanged_modified
== MODIFF
1634 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
1635 end_unchanged
= Z
- end
;
1637 if (MODIFF
<= SAVE_MODIFF
)
1638 record_first_change ();
1641 buffer
->point_before_scroll
= Qnil
;
1643 if (buffer
!= old_buffer
)
1644 set_buffer_internal (old_buffer
);
1647 /* Check that it is okay to modify the buffer between START and END,
1648 which are char positions.
1650 Run the before-change-function, if any. If intervals are in use,
1651 verify that the text to be modified is not read-only, and call
1652 any modification properties the text may have.
1654 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1655 by holding its value temporarily in a marker. */
1658 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
1662 if (!NILP (current_buffer
->read_only
))
1663 Fbarf_if_buffer_read_only ();
1665 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1666 if (BUF_INTERVALS (current_buffer
) != 0)
1670 Lisp_Object preserve_marker
;
1671 struct gcpro gcpro1
;
1672 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1673 GCPRO1 (preserve_marker
);
1674 verify_interval_modification (current_buffer
, start
, end
);
1675 *preserve_ptr
= marker_position (preserve_marker
);
1676 unchain_marker (preserve_marker
);
1680 verify_interval_modification (current_buffer
, start
, end
);
1683 #ifdef CLASH_DETECTION
1684 if (!NILP (current_buffer
->file_truename
)
1685 /* Make binding buffer-file-name to nil effective. */
1686 && !NILP (current_buffer
->filename
)
1687 && SAVE_MODIFF
>= MODIFF
)
1688 lock_file (current_buffer
->file_truename
);
1690 /* At least warn if this file has changed on disk since it was visited. */
1691 if (!NILP (current_buffer
->filename
)
1692 && SAVE_MODIFF
>= MODIFF
1693 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1694 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
1695 call1 (intern ("ask-user-about-supersession-threat"),
1696 current_buffer
->filename
);
1697 #endif /* not CLASH_DETECTION */
1699 signal_before_change (start
, end
, preserve_ptr
);
1701 if (current_buffer
->newline_cache
)
1702 invalidate_region_cache (current_buffer
,
1703 current_buffer
->newline_cache
,
1704 start
- BEG
, Z
- end
);
1705 if (current_buffer
->width_run_cache
)
1706 invalidate_region_cache (current_buffer
,
1707 current_buffer
->width_run_cache
,
1708 start
- BEG
, Z
- end
);
1710 Vdeactivate_mark
= Qt
;
1713 /* These macros work with an argument named `preserve_ptr'
1714 and a local variable named `preserve_marker'. */
1716 #define PRESERVE_VALUE \
1717 if (preserve_ptr && NILP (preserve_marker)) \
1718 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1720 #define RESTORE_VALUE \
1721 if (! NILP (preserve_marker)) \
1723 *preserve_ptr = marker_position (preserve_marker); \
1724 unchain_marker (preserve_marker); \
1727 #define PRESERVE_START_END \
1728 if (NILP (start_marker)) \
1729 start_marker = Fcopy_marker (start, Qnil); \
1730 if (NILP (end_marker)) \
1731 end_marker = Fcopy_marker (end, Qnil);
1733 #define FETCH_START \
1734 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1737 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1739 /* Signal a change to the buffer immediately before it happens.
1740 START_INT and END_INT are the bounds of the text to be changed.
1742 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1743 by holding its value temporarily in a marker. */
1746 signal_before_change (start_int
, end_int
, preserve_ptr
)
1747 int start_int
, end_int
;
1750 Lisp_Object start
, end
;
1751 Lisp_Object start_marker
, end_marker
;
1752 Lisp_Object preserve_marker
;
1753 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1755 start
= make_number (start_int
);
1756 end
= make_number (end_int
);
1757 preserve_marker
= Qnil
;
1758 start_marker
= Qnil
;
1760 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
1762 /* If buffer is unmodified, run a special hook for that case. */
1763 if (SAVE_MODIFF
>= MODIFF
1764 && !NILP (Vfirst_change_hook
)
1765 && !NILP (Vrun_hooks
))
1769 call1 (Vrun_hooks
, Qfirst_change_hook
);
1772 /* Run the before-change-function if any.
1773 We don't bother "binding" this variable to nil
1774 because it is obsolete anyway and new code should not use it. */
1775 if (!NILP (Vbefore_change_function
))
1779 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
1782 /* Now run the before-change-functions if any. */
1783 if (!NILP (Vbefore_change_functions
))
1785 Lisp_Object args
[3];
1786 Lisp_Object before_change_functions
;
1787 Lisp_Object after_change_functions
;
1788 struct gcpro gcpro1
, gcpro2
;
1793 /* "Bind" before-change-functions and after-change-functions
1794 to nil--but in a way that errors don't know about.
1795 That way, if there's an error in them, they will stay nil. */
1796 before_change_functions
= Vbefore_change_functions
;
1797 after_change_functions
= Vafter_change_functions
;
1798 Vbefore_change_functions
= Qnil
;
1799 Vafter_change_functions
= Qnil
;
1800 GCPRO2 (before_change_functions
, after_change_functions
);
1802 /* Actually run the hook functions. */
1803 args
[0] = Qbefore_change_functions
;
1804 args
[1] = FETCH_START
;
1805 args
[2] = FETCH_END
;
1806 run_hook_list_with_args (before_change_functions
, 3, args
);
1808 /* "Unbind" the variables we "bound" to nil. */
1809 Vbefore_change_functions
= before_change_functions
;
1810 Vafter_change_functions
= after_change_functions
;
1814 if (!NILP (current_buffer
->overlays_before
)
1815 || !NILP (current_buffer
->overlays_after
))
1818 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
1819 FETCH_START
, FETCH_END
, Qnil
);
1822 if (! NILP (start_marker
))
1823 free_marker (start_marker
);
1824 if (! NILP (end_marker
))
1825 free_marker (end_marker
);
1830 /* Signal a change immediately after it happens.
1831 CHARPOS is the character position of the start of the changed text.
1832 LENDEL is the number of characters of the text before the change.
1833 (Not the whole buffer; just the part that was changed.)
1834 LENINS is the number of characters in that part of the text
1835 after the change. */
1838 signal_after_change (charpos
, lendel
, lenins
)
1839 int charpos
, lendel
, lenins
;
1841 /* If we are deferring calls to the after-change functions
1842 and there are no before-change functions,
1843 just record the args that we were going to use. */
1844 if (! NILP (Vcombine_after_change_calls
)
1845 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
1846 && NILP (current_buffer
->overlays_before
)
1847 && NILP (current_buffer
->overlays_after
))
1851 if (!NILP (combine_after_change_list
)
1852 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
1853 Fcombine_after_change_execute ();
1855 elt
= Fcons (make_number (charpos
- BEG
),
1856 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
1857 Fcons (make_number (lenins
- lendel
), Qnil
)));
1858 combine_after_change_list
1859 = Fcons (elt
, combine_after_change_list
);
1860 combine_after_change_buffer
= Fcurrent_buffer ();
1865 if (!NILP (combine_after_change_list
))
1866 Fcombine_after_change_execute ();
1868 /* Run the after-change-function if any.
1869 We don't bother "binding" this variable to nil
1870 because it is obsolete anyway and new code should not use it. */
1871 if (!NILP (Vafter_change_function
))
1872 call3 (Vafter_change_function
,
1873 make_number (charpos
), make_number (charpos
+ lenins
),
1874 make_number (lendel
));
1876 if (!NILP (Vafter_change_functions
))
1878 Lisp_Object args
[4];
1879 Lisp_Object before_change_functions
;
1880 Lisp_Object after_change_functions
;
1881 struct gcpro gcpro1
, gcpro2
;
1883 /* "Bind" before-change-functions and after-change-functions
1884 to nil--but in a way that errors don't know about.
1885 That way, if there's an error in them, they will stay nil. */
1886 before_change_functions
= Vbefore_change_functions
;
1887 after_change_functions
= Vafter_change_functions
;
1888 Vbefore_change_functions
= Qnil
;
1889 Vafter_change_functions
= Qnil
;
1890 GCPRO2 (before_change_functions
, after_change_functions
);
1892 /* Actually run the hook functions. */
1893 args
[0] = Qafter_change_functions
;
1894 XSETFASTINT (args
[1], charpos
);
1895 XSETFASTINT (args
[2], charpos
+ lenins
);
1896 XSETFASTINT (args
[3], lendel
);
1897 run_hook_list_with_args (after_change_functions
,
1900 /* "Unbind" the variables we "bound" to nil. */
1901 Vbefore_change_functions
= before_change_functions
;
1902 Vafter_change_functions
= after_change_functions
;
1906 if (!NILP (current_buffer
->overlays_before
)
1907 || !NILP (current_buffer
->overlays_after
))
1908 report_overlay_modification (make_number (charpos
),
1909 make_number (charpos
+ lenins
),
1911 make_number (charpos
),
1912 make_number (charpos
+ lenins
),
1913 make_number (lendel
));
1915 /* After an insertion, call the text properties
1916 insert-behind-hooks or insert-in-front-hooks. */
1918 report_interval_modification (charpos
, charpos
+ lenins
);
1922 Fcombine_after_change_execute_1 (val
)
1925 Vcombine_after_change_calls
= val
;
1929 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
1930 Scombine_after_change_execute
, 0, 0, 0,
1931 "This function is for use internally in `combine-after-change-calls'.")
1934 register Lisp_Object val
;
1935 int count
= specpdl_ptr
- specpdl
;
1936 int beg
, end
, change
;
1940 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
1942 Fset_buffer (combine_after_change_buffer
);
1944 /* # chars unchanged at beginning of buffer. */
1946 /* # chars unchanged at end of buffer. */
1948 /* Total amount of insertion (negative for deletion). */
1951 /* Scan the various individual changes,
1952 accumulating the range info in BEG, END and CHANGE. */
1953 for (tail
= combine_after_change_list
; CONSP (tail
);
1954 tail
= XCONS (tail
)->cdr
)
1957 int thisbeg
, thisend
, thischange
;
1959 /* Extract the info from the next element. */
1960 elt
= XCONS (tail
)->car
;
1963 thisbeg
= XINT (XCONS (elt
)->car
);
1965 elt
= XCONS (elt
)->cdr
;
1968 thisend
= XINT (XCONS (elt
)->car
);
1970 elt
= XCONS (elt
)->cdr
;
1973 thischange
= XINT (XCONS (elt
)->car
);
1975 /* Merge this range into the accumulated range. */
1976 change
+= thischange
;
1983 /* Get the current start and end positions of the range
1984 that was changed. */
1988 /* We are about to handle these, so discard them. */
1989 combine_after_change_list
= Qnil
;
1991 /* Now run the after-change functions for real.
1992 Turn off the flag that defers them. */
1993 record_unwind_protect (Fcombine_after_change_execute_1
,
1994 Vcombine_after_change_calls
);
1995 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
1997 return unbind_to (count
, val
);
2002 staticpro (&combine_after_change_list
);
2003 combine_after_change_list
= Qnil
;
2005 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2006 "Used internally by the `combine-after-change-calls' macro.");
2007 Vcombine_after_change_calls
= Qnil
;
2009 defsubr (&Scombine_after_change_execute
);