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
;
375 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
376 to TO / TO_BYTE. We have to relocate the charpos of every marker
377 that points after the insertion (but not their bytepos).
379 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
380 that combine into one character with the text before the insertion.
381 COMBINED_AFTER_BYTES is the number of bytes after the insertion
382 that combine into one character with the last inserted bytes.
384 When a marker points at the insertion point,
385 we advance it if either its insertion-type is t
386 or BEFORE_MARKERS is true. */
389 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
,
390 combined_before_bytes
, combined_after_bytes
,
392 register int from
, from_byte
, to
, to_byte
;
393 int combined_before_bytes
, combined_after_bytes
, before_markers
;
397 int nchars
= to
- from
;
398 int nbytes
= to_byte
- from_byte
;
400 marker
= BUF_MARKERS (current_buffer
);
402 while (!NILP (marker
))
404 register struct Lisp_Marker
*m
= XMARKER (marker
);
406 /* In a single-byte buffer, a marker's two positions must be equal.
407 (If this insertion is going to combine characters, Z will
408 become different from Z_BYTE, but they might be the same now.
409 If so, the two OLD positions of the marker should be equal.) */
412 if (m
->charpos
!= m
->bytepos
)
416 if (m
->bytepos
== from_byte
)
418 if (m
->insertion_type
|| before_markers
)
420 m
->bytepos
+= nbytes
+ combined_after_bytes
;
421 m
->charpos
+= nchars
+ !!combined_after_bytes
;
422 /* Point the marker before the combined character,
423 so that undoing the insertion puts it back where it was. */
424 if (combined_after_bytes
)
425 DEC_BOTH (m
->charpos
, m
->bytepos
);
426 if (m
->insertion_type
)
429 else if (combined_before_bytes
)
431 /* This marker doesn't "need relocation",
432 but don't leave it pointing in the middle of a character.
433 Point the marker after the combined character,
434 so that undoing the insertion puts it back where it was. */
436 /* Here we depend on the fact that the gap is after
437 all of the combining bytes that we are going to skip over. */
438 DEC_BOTH (m
->charpos
, m
->bytepos
);
439 INC_BOTH (m
->charpos
, m
->bytepos
);
442 /* If a marker was pointing into the combining bytes
443 after the insertion, don't leave it there
444 in the middle of a character. */
445 else if (combined_after_bytes
&& m
->bytepos
>= from_byte
446 && m
->bytepos
< from_byte
+ combined_after_bytes
)
448 /* Put it after the combining bytes. */
449 m
->bytepos
= to_byte
+ combined_after_bytes
;
451 /* Now move it back before the combined character,
452 so that undoing the insertion will put it where it was. */
453 DEC_BOTH (m
->charpos
, m
->bytepos
);
455 else if (m
->bytepos
> from_byte
)
457 m
->bytepos
+= nbytes
;
458 m
->charpos
+= nchars
;
464 /* Adjusting only markers whose insertion-type is t may result in
465 disordered overlays in the slot `overlays_before'. */
467 fix_overlays_before (current_buffer
, from
, to
);
470 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
472 This is used only when the value of point changes due to an insert
473 or delete; it does not represent a conceptual change in point as a
474 marker. In particular, point is not crossing any interval
475 boundaries, so there's no need to use the usual SET_PT macro. In
476 fact it would be incorrect to do so, because either the old or the
477 new value of point is out of sync with the current set of
481 adjust_point (nchars
, nbytes
)
484 BUF_PT (current_buffer
) += nchars
;
485 BUF_PT_BYTE (current_buffer
) += nbytes
;
487 /* In a single-byte buffer, the two positions must be equal. */
493 /* Make the gap NBYTES_ADDED bytes longer. */
496 make_gap (nbytes_added
)
499 unsigned char *result
;
502 int real_gap_loc_byte
;
505 /* If we have to get more space, get enough to last a while. */
506 nbytes_added
+= 2000;
508 /* Don't allow a buffer size that won't fit in an int
509 even if it will fit in a Lisp integer.
510 That won't work because so many places use `int'. */
512 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
513 >= ((unsigned) 1 << (min (BITS_PER_INT
, VALBITS
) - 1)))
514 error ("Buffer exceeds maximum size");
517 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
518 result
= BUFFER_REALLOC (BEG_ADDR
, (Z_BYTE
- BEG_BYTE
519 + GAP_SIZE
+ nbytes_added
+ 1));
527 /* We can't unblock until the new address is properly stored. */
531 /* Prevent quitting in move_gap. */
536 real_gap_loc_byte
= GPT_BYTE
;
537 old_gap_size
= GAP_SIZE
;
539 /* Call the newly allocated space a gap at the end of the whole space. */
541 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
542 GAP_SIZE
= nbytes_added
;
544 /* Move the new gap down to be consecutive with the end of the old one.
545 This adjusts the markers properly too. */
546 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
548 /* Now combine the two into one large gap. */
549 GAP_SIZE
+= old_gap_size
;
551 GPT_BYTE
= real_gap_loc_byte
;
559 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
560 FROM_MULTIBYTE says whether the incoming text is multibyte.
561 TO_MULTIBYTE says whether to store the text as multibyte.
562 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
564 Return the number of bytes stored at TO_ADDR. */
567 copy_text (from_addr
, to_addr
, nbytes
,
568 from_multibyte
, to_multibyte
)
569 unsigned char *from_addr
;
570 unsigned char *to_addr
;
572 int from_multibyte
, to_multibyte
;
574 if (from_multibyte
== to_multibyte
)
576 bcopy (from_addr
, to_addr
, nbytes
);
579 else if (from_multibyte
)
582 int bytes_left
= nbytes
;
584 /* Convert multibyte to single byte. */
585 while (bytes_left
> 0)
588 c
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
589 *to_addr
++ = SINGLE_BYTE_CHAR_P (c
) ? c
: (c
& 0177) + 0200;
590 from_addr
+= thislen
;
598 unsigned char *initial_to_addr
= to_addr
;
600 /* Convert single-byte to multibyte. */
603 int c
= *from_addr
++;
604 unsigned char workbuf
[4], *str
;
607 if (c
>= 0240 && c
< 0400)
609 c
= unibyte_char_to_multibyte (c
);
610 len
= CHAR_STRING (c
, workbuf
, str
);
611 bcopy (str
, to_addr
, len
);
616 /* Special case for speed. */
617 *to_addr
++ = c
, nbytes
--;
619 return to_addr
- initial_to_addr
;
623 /* Return the number of bytes it would take
624 to convert some single-byte text to multibyte.
625 The single-byte text consists of NBYTES bytes at PTR. */
628 count_size_as_multibyte (ptr
, nbytes
)
633 int outgoing_nbytes
= 0;
635 for (i
= 0; i
< nbytes
; i
++)
637 unsigned int c
= *ptr
++;
643 c
= unibyte_char_to_multibyte (c
);
644 outgoing_nbytes
+= XINT (Fchar_bytes (make_number (c
)));
648 return outgoing_nbytes
;
651 /* Insert a string of specified length before point.
652 This function judges multibyteness based on
653 enable_multibyte_characters in the current buffer;
654 it never converts between single-byte and multibyte.
656 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
657 prepare_to_modify_buffer could relocate the text. */
660 insert (string
, nbytes
)
661 register unsigned char *string
;
667 insert_1 (string
, nbytes
, 0, 1, 0);
668 signal_after_change (opoint
, 0, PT
- opoint
);
672 /* Likewise, but inherit text properties from neighboring characters. */
675 insert_and_inherit (string
, nbytes
)
676 register unsigned char *string
;
682 insert_1 (string
, nbytes
, 1, 1, 0);
683 signal_after_change (opoint
, 0, PT
- opoint
);
687 /* Insert the character C before point. Do not inherit text properties. */
693 unsigned char workbuf
[4], *str
;
696 if (! NILP (current_buffer
->enable_multibyte_characters
))
697 len
= CHAR_STRING (c
, workbuf
, str
);
708 /* Insert the null-terminated string S before point. */
714 insert (s
, strlen (s
));
717 /* Like `insert' except that all markers pointing at the place where
718 the insertion happens are adjusted to point after it.
719 Don't use this function to insert part of a Lisp string,
720 since gc could happen and relocate it. */
723 insert_before_markers (string
, nbytes
)
724 unsigned char *string
;
731 insert_1 (string
, nbytes
, 0, 1, 1);
732 signal_after_change (opoint
, 0, PT
- opoint
);
736 /* Likewise, but inherit text properties from neighboring characters. */
739 insert_before_markers_and_inherit (string
, nbytes
)
740 unsigned char *string
;
747 insert_1 (string
, nbytes
, 1, 1, 1);
748 signal_after_change (opoint
, 0, PT
- opoint
);
752 /* Subroutine used by the insert functions above. */
755 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
756 register unsigned char *string
;
758 int inherit
, prepare
, before_markers
;
760 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
761 inherit
, prepare
, before_markers
);
764 /* See if the bytes before POS/POS_BYTE combine with bytes
765 at the start of STRING to form a single character.
766 If so, return the number of bytes at the start of STRING
767 which combine in this way. Otherwise, return 0. */
770 count_combining_before (string
, length
, pos
, pos_byte
)
771 unsigned char *string
;
775 int opos
= pos
, opos_byte
= pos_byte
;
777 unsigned char *p
= string
;
779 if (NILP (current_buffer
->enable_multibyte_characters
))
781 if (length
== 0 || CHAR_HEAD_P (*string
))
785 c
= FETCH_BYTE (pos_byte
- 1);
786 if (ASCII_BYTE_P (c
))
788 DEC_BOTH (pos
, pos_byte
);
789 c
= FETCH_BYTE (pos_byte
);
790 if (! BASE_LEADING_CODE_P (c
))
793 /* We have a combination situation.
794 Count the bytes at STRING that will combine. */
795 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
801 /* See if the bytes after POS/POS_BYTE combine with bytes
802 at the end of STRING to form a single character.
803 If so, return the number of bytes after POS/POS_BYTE
804 which combine in this way. Otherwise, return 0. */
807 count_combining_after (string
, length
, pos
, pos_byte
)
808 unsigned char *string
;
812 int opos
= pos
, opos_byte
= pos_byte
;
816 if (NILP (current_buffer
->enable_multibyte_characters
))
818 if (length
== 0 || ASCII_BYTE_P (string
[length
- 1]))
821 while (i
> 0 && ! CHAR_HEAD_P (string
[i
]))
825 if (! BASE_LEADING_CODE_P (string
[i
]))
830 c
= FETCH_BYTE (pos_byte
);
833 while (pos_byte
< ZV_BYTE
)
835 c
= FETCH_BYTE (pos_byte
);
841 return pos_byte
- opos_byte
;
844 /* Combine NBYTES stray trailing-codes, which were formerly separate
845 characters, with the preceding character. These bytes
846 are located after position POS / POS_BYTE, and the preceding character
847 is located just before that position. */
850 combine_bytes (pos
, pos_byte
, nbytes
)
851 int pos
, pos_byte
, nbytes
;
853 /* Adjust all markers. */
854 adjust_markers_for_delete (pos
, pos_byte
, pos
+ nbytes
, pos_byte
);
856 adjust_overlays_for_delete (pos
, nbytes
);
859 BUF_PT (current_buffer
) -= nbytes
;
867 if (BUF_INTERVALS (current_buffer
) != 0)
868 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
869 offset_intervals (current_buffer
, pos
, - nbytes
);
872 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
873 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
874 are the same as in insert_1. */
877 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
878 register unsigned char *string
;
879 register int nchars
, nbytes
;
880 int inherit
, prepare
, before_markers
;
882 register Lisp_Object temp
;
883 int combined_before_bytes
, combined_after_bytes
;
885 if (NILP (current_buffer
->enable_multibyte_characters
))
889 move_gap_both (PT
, PT_BYTE
);
890 if (GAP_SIZE
< nbytes
)
891 make_gap (nbytes
- GAP_SIZE
);
894 prepare_to_modify_buffer (PT
, PT
, NULL
);
896 combined_before_bytes
897 = count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
899 = count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
901 /* Record deletion of the surrounding text that combines with
902 the insertion. This, together with recording the insertion,
903 will add up to the right stuff in the undo list.
905 But there is no need to actually delete the combining bytes
906 from the buffer and reinsert them. */
908 if (combined_after_bytes
)
909 record_delete (PT
, combined_after_bytes
);
911 if (combined_before_bytes
)
912 record_delete (PT
- 1, 1);
914 record_insert (PT
- !!combined_before_bytes
, nchars
);
917 bcopy (string
, GPT_ADDR
, nbytes
);
920 /* When we have combining at the end of the insertion,
921 this is the character position before the combined character. */
928 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
930 if (combined_after_bytes
)
931 move_gap_both (GPT
+ combined_after_bytes
,
932 GPT_BYTE
+ combined_after_bytes
);
937 adjust_overlays_for_insert (PT
, nchars
);
938 adjust_markers_for_insert (PT
, PT_BYTE
,
939 PT
+ nchars
, PT_BYTE
+ nbytes
,
940 combined_before_bytes
, combined_after_bytes
,
943 #ifdef USE_TEXT_PROPERTIES
944 if (BUF_INTERVALS (current_buffer
) != 0)
945 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
946 offset_intervals (current_buffer
, PT
, nchars
);
948 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
949 Fset_text_properties (make_number (PT
), make_number (PT
+ nchars
),
954 int pos
= PT
, pos_byte
= PT_BYTE
;
956 adjust_point (nchars
+ combined_after_bytes
,
957 nbytes
+ combined_after_bytes
);
959 if (combined_after_bytes
)
960 combine_bytes (pos
+ nchars
, pos_byte
+ nbytes
, combined_after_bytes
);
962 if (combined_before_bytes
)
963 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
967 /* Insert the part of the text of STRING, a Lisp object assumed to be
968 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
969 starting at position POS / POS_BYTE. If the text of STRING has properties,
970 copy them into the buffer.
972 It does not work to use `insert' for this, because a GC could happen
973 before we bcopy the stuff into the buffer, and relocate the string
974 without insert noticing. */
977 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
979 register int pos
, pos_byte
, length
, length_byte
;
985 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
987 signal_after_change (opoint
, 0, PT
- opoint
);
991 /* Like `insert_from_string' except that all markers pointing
992 at the place where the insertion happens are adjusted to point after it. */
995 insert_from_string_before_markers (string
, pos
, pos_byte
,
996 length
, length_byte
, inherit
)
998 register int pos
, pos_byte
, length
, length_byte
;
1004 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1006 signal_after_change (opoint
, 0, PT
- opoint
);
1010 /* Subroutine of the insertion functions above. */
1013 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1014 inherit
, before_markers
)
1016 register int pos
, pos_byte
, nchars
, nbytes
;
1017 int inherit
, before_markers
;
1019 register Lisp_Object temp
;
1020 struct gcpro gcpro1
;
1021 int outgoing_nbytes
= nbytes
;
1022 int combined_before_bytes
, combined_after_bytes
;
1023 int adjusted_nchars
;
1026 /* Make OUTGOING_NBYTES describe the text
1027 as it will be inserted in this buffer. */
1029 if (NILP (current_buffer
->enable_multibyte_characters
))
1030 outgoing_nbytes
= nchars
;
1031 else if (nchars
== nbytes
)
1033 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1036 /* Make sure point-max won't overflow after this insertion. */
1037 XSETINT (temp
, outgoing_nbytes
+ Z
);
1038 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1039 error ("Maximum buffer size exceeded");
1042 prepare_to_modify_buffer (PT
, PT
, NULL
);
1045 move_gap_both (PT
, PT_BYTE
);
1046 if (GAP_SIZE
< nbytes
)
1047 make_gap (outgoing_nbytes
- GAP_SIZE
);
1050 /* Copy the string text into the buffer, perhaps converting
1051 between single-byte and multibyte. */
1052 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1053 /* If these are equal, it is a single-byte string.
1054 Its chars are either ASCII, in which case copy_text
1055 won't change it, or single-byte non-ASCII chars,
1056 that need to be changed. */
1058 ! NILP (current_buffer
->enable_multibyte_characters
));
1060 /* We have copied text into the gap, but we have not altered
1061 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1062 to these functions and get the same results as we would
1063 have got earlier on. Meanwhile, PT_ADDR does point to
1064 the text that has been stored by copy_text. */
1066 combined_before_bytes
1067 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1068 combined_after_bytes
1069 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1071 /* Record deletion of the surrounding text that combines with
1072 the insertion. This, together with recording the insertion,
1073 will add up to the right stuff in the undo list.
1075 But there is no need to actually delete the combining bytes
1076 from the buffer and reinsert them. */
1078 if (combined_after_bytes
)
1079 record_delete (PT
, combined_after_bytes
);
1081 if (combined_before_bytes
)
1082 record_delete (PT
- 1, 1);
1084 record_insert (PT
- !!combined_before_bytes
, nchars
);
1087 GAP_SIZE
-= outgoing_nbytes
;
1091 GPT_BYTE
+= outgoing_nbytes
;
1092 ZV_BYTE
+= outgoing_nbytes
;
1093 Z_BYTE
+= outgoing_nbytes
;
1094 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1096 if (combined_after_bytes
)
1097 move_gap_both (GPT
+ combined_after_bytes
,
1098 GPT_BYTE
+ combined_after_bytes
);
1103 adjust_overlays_for_insert (PT
, nchars
);
1104 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1105 PT_BYTE
+ outgoing_nbytes
,
1106 combined_before_bytes
, combined_after_bytes
,
1109 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1110 offset_intervals (current_buffer
, PT
, nchars
);
1112 intervals
= XSTRING (string
)->intervals
;
1113 /* Get the intervals for the part of the string we are inserting--
1114 not including the combined-before bytes. */
1115 if (nbytes
< XSTRING (string
)->size_byte
)
1116 intervals
= copy_intervals (intervals
, pos
, nchars
);
1118 /* Insert those intervals. */
1119 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1120 current_buffer
, inherit
);
1123 int pos
= PT
, pos_byte
= PT_BYTE
;
1125 adjust_point (nchars
+ combined_after_bytes
,
1126 outgoing_nbytes
+ combined_after_bytes
);
1128 if (combined_after_bytes
)
1129 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1130 combined_after_bytes
);
1132 if (combined_before_bytes
)
1133 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1137 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1138 current buffer. If the text in BUF has properties, they are absorbed
1139 into the current buffer.
1141 It does not work to use `insert' for this, because a malloc could happen
1142 and relocate BUF's text before the bcopy happens. */
1145 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1147 int charpos
, nchars
;
1154 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1155 signal_after_change (opoint
, 0, PT
- opoint
);
1160 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1165 register Lisp_Object temp
;
1167 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1168 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1169 int incoming_nbytes
= to_byte
- from_byte
;
1170 int outgoing_nbytes
= incoming_nbytes
;
1171 int combined_before_bytes
, combined_after_bytes
;
1172 int adjusted_nchars
;
1175 /* Make OUTGOING_NBYTES describe the text
1176 as it will be inserted in this buffer. */
1178 if (NILP (current_buffer
->enable_multibyte_characters
))
1179 outgoing_nbytes
= nchars
;
1180 else if (NILP (buf
->enable_multibyte_characters
))
1182 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1185 /* Make sure point-max won't overflow after this insertion. */
1186 XSETINT (temp
, outgoing_nbytes
+ Z
);
1187 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1188 error ("Maximum buffer size exceeded");
1190 prepare_to_modify_buffer (PT
, PT
, NULL
);
1193 move_gap_both (PT
, PT_BYTE
);
1194 if (GAP_SIZE
< outgoing_nbytes
)
1195 make_gap (outgoing_nbytes
- GAP_SIZE
);
1197 if (from
< BUF_GPT (buf
))
1199 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1200 if (chunk
> incoming_nbytes
)
1201 chunk
= incoming_nbytes
;
1202 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1204 ! NILP (buf
->enable_multibyte_characters
),
1205 ! NILP (current_buffer
->enable_multibyte_characters
));
1209 if (chunk
< incoming_nbytes
)
1210 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1211 GPT_ADDR
+ chunk
, incoming_nbytes
- chunk
,
1212 ! NILP (buf
->enable_multibyte_characters
),
1213 ! NILP (current_buffer
->enable_multibyte_characters
));
1215 /* We have copied text into the gap, but we have not altered
1216 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1217 to these functions and get the same results as we would
1218 have got earlier on. Meanwhile, GPT_ADDR does point to
1219 the text that has been stored by copy_text. */
1220 combined_before_bytes
1221 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1222 combined_after_bytes
1223 = count_combining_after (GPT_ADDR
, outgoing_nbytes
,
1226 /* Record deletion of the surrounding text that combines with
1227 the insertion. This, together with recording the insertion,
1228 will add up to the right stuff in the undo list.
1230 But there is no need to actually delete the combining bytes
1231 from the buffer and reinsert them. */
1233 if (combined_after_bytes
)
1234 record_delete (PT
, combined_after_bytes
);
1236 if (combined_before_bytes
)
1237 record_delete (PT
- 1, 1);
1239 record_insert (PT
- !!combined_before_bytes
, nchars
);
1242 GAP_SIZE
-= outgoing_nbytes
;
1246 GPT_BYTE
+= outgoing_nbytes
;
1247 ZV_BYTE
+= outgoing_nbytes
;
1248 Z_BYTE
+= outgoing_nbytes
;
1249 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1251 if (combined_after_bytes
)
1252 move_gap_both (GPT
+ combined_after_bytes
,
1253 GPT_BYTE
+ combined_after_bytes
);
1258 adjust_overlays_for_insert (PT
, nchars
);
1259 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1260 PT_BYTE
+ outgoing_nbytes
,
1261 combined_before_bytes
, combined_after_bytes
, 0);
1263 #ifdef USE_TEXT_PROPERTIES
1264 if (BUF_INTERVALS (current_buffer
) != 0)
1265 offset_intervals (current_buffer
, PT
, nchars
);
1268 /* Get the intervals for the part of the string we are inserting--
1269 not including the combined-before bytes. */
1270 intervals
= BUF_INTERVALS (buf
);
1271 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1272 intervals
= copy_intervals (intervals
, from
, nchars
);
1274 /* Insert those intervals. */
1275 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1278 int pos
= PT
, pos_byte
= PT_BYTE
;
1280 adjust_point (nchars
+ combined_after_bytes
,
1281 outgoing_nbytes
+ combined_after_bytes
);
1283 if (combined_after_bytes
)
1284 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1285 combined_after_bytes
);
1287 if (combined_before_bytes
)
1288 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1292 /* This function should be called after moving gap to FROM and before
1293 altering text between FROM and TO. This adjusts various position
1294 keepers and markers as if the text is deleted. Don't forget to
1295 call adjust_after_replace after you actually alter the text. */
1298 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1299 int from
, from_byte
, to
, to_byte
;
1301 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1302 record_delete (from
, to
- from
);
1305 /* This function should be called after altering the text between FROM
1306 and TO to a new text of LEN chars (LEN_BYTE bytes).
1307 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES are the number
1308 of bytes before (resp. after) the change which combine with
1309 the beginning or end of the replacement text to form one character. */
1312 adjust_after_replace (from
, from_byte
, to
, to_byte
, len
, len_byte
,
1313 combined_before_bytes
, combined_after_bytes
)
1314 int from
, from_byte
, to
, to_byte
, len
, len_byte
;
1315 int combined_before_bytes
, combined_after_bytes
;
1317 int adjusted_nchars
= len
- combined_before_bytes
- combined_after_bytes
;
1318 record_insert (from
- !!combined_before_bytes
, len
);
1320 adjust_point (len
- (to
- from
) + combined_after_bytes
,
1321 len_byte
- (to_byte
- from_byte
) + combined_after_bytes
);
1322 #ifdef USE_TEXT_PROPERTIES
1323 offset_intervals (current_buffer
, PT
, adjusted_nchars
- (to
- from
));
1325 adjust_overlays_for_delete (from
, to
- from
);
1326 adjust_overlays_for_insert (from
, adjusted_nchars
);
1327 adjust_markers_for_insert (from
, from_byte
,
1328 from
+ adjusted_nchars
, from_byte
+ len_byte
,
1329 combined_before_bytes
, combined_after_bytes
, 0);
1331 evaporate_overlays (from
);
1335 /* Replace the text from character positions FROM to TO with NEW,
1336 If PREPARE is nonzero, call prepare_to_modify_buffer.
1337 If INHERIT, the newly inserted text should inherit text properties
1338 from the surrounding non-deleted text. */
1340 /* Note that this does not yet handle markers quite right.
1341 Also it needs to record a single undo-entry that does a replacement
1342 rather than a separate delete and insert.
1343 That way, undo will also handle markers properly. */
1346 replace_range (from
, to
, new, prepare
, inherit
)
1348 int from
, to
, prepare
, inherit
;
1350 int inschars
= XSTRING (new)->size
;
1351 int insbytes
= XSTRING (new)->size_byte
;
1352 int from_byte
, to_byte
;
1353 int nbytes_del
, nchars_del
;
1354 register Lisp_Object temp
;
1355 struct gcpro gcpro1
;
1356 int combined_before_bytes
, combined_after_bytes
;
1357 int adjusted_inschars
;
1359 int outgoing_insbytes
= insbytes
;
1365 int range_length
= to
- from
;
1366 prepare_to_modify_buffer (from
, to
, &from
);
1367 to
= from
+ range_length
;
1372 /* Make args be valid */
1378 from_byte
= CHAR_TO_BYTE (from
);
1379 to_byte
= CHAR_TO_BYTE (to
);
1381 nchars_del
= to
- from
;
1382 nbytes_del
= to_byte
- from_byte
;
1384 if (nbytes_del
<= 0 && insbytes
== 0)
1387 /* Make OUTGOING_INSBYTES describe the text
1388 as it will be inserted in this buffer. */
1390 if (NILP (current_buffer
->enable_multibyte_characters
))
1391 outgoing_insbytes
= inschars
;
1392 else if (inschars
== insbytes
)
1394 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1396 /* Make sure point-max won't overflow after this insertion. */
1397 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1398 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1399 error ("Maximum buffer size exceeded");
1403 /* Make sure the gap is somewhere in or next to what we are deleting. */
1405 gap_right (from
, from_byte
);
1407 gap_left (to
, to_byte
, 0);
1409 /* Relocate all markers pointing into the new, larger gap
1410 to point at the end of the text before the gap.
1411 Do this before recording the deletion,
1412 so that undo handles this after reinserting the text. */
1413 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1415 record_delete (from
, nchars_del
);
1417 GAP_SIZE
+= nbytes_del
;
1420 ZV_BYTE
-= nbytes_del
;
1421 Z_BYTE
-= nbytes_del
;
1423 GPT_BYTE
= from_byte
;
1424 *(GPT_ADDR
) = 0; /* Put an anchor. */
1429 if (GPT
- BEG
< beg_unchanged
)
1430 beg_unchanged
= GPT
- BEG
;
1431 if (Z
- GPT
< end_unchanged
)
1432 end_unchanged
= Z
- GPT
;
1434 if (GAP_SIZE
< insbytes
)
1435 make_gap (insbytes
- GAP_SIZE
);
1437 /* Copy the string text into the buffer, perhaps converting
1438 between single-byte and multibyte. */
1439 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1440 /* If these are equal, it is a single-byte string.
1441 Its chars are either ASCII, in which case copy_text
1442 won't change it, or single-byte non-ASCII chars,
1443 that need to be changed. */
1444 inschars
!= insbytes
,
1445 ! NILP (current_buffer
->enable_multibyte_characters
));
1447 /* We have copied text into the gap, but we have not altered
1448 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1449 to these functions and get the same results as we would
1450 have got earlier on. Meanwhile, GPT_ADDR does point to
1451 the text that has been stored by copy_text. */
1453 combined_before_bytes
1454 = count_combining_before (GPT_ADDR
, outgoing_insbytes
, PT
, PT_BYTE
);
1455 combined_after_bytes
1456 = count_combining_after (GPT_ADDR
, outgoing_insbytes
, PT
, PT_BYTE
);
1458 /* Record deletion of the surrounding text that combines with
1459 the insertion. This, together with recording the insertion,
1460 will add up to the right stuff in the undo list.
1462 But there is no need to actually delete the combining bytes
1463 from the buffer and reinsert them. */
1465 if (combined_after_bytes
)
1466 record_delete (PT
, combined_after_bytes
);
1468 if (combined_before_bytes
)
1469 record_delete (PT
- 1, 1);
1471 record_insert (from
, inschars
);
1473 GAP_SIZE
-= outgoing_insbytes
;
1477 GPT_BYTE
+= outgoing_insbytes
;
1478 ZV_BYTE
+= outgoing_insbytes
;
1479 Z_BYTE
+= outgoing_insbytes
;
1480 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1482 if (combined_after_bytes
)
1483 move_gap_both (GPT
+ combined_after_bytes
,
1484 GPT_BYTE
+ combined_after_bytes
);
1489 /* Adjust the overlay center as needed. This must be done after
1490 adjusting the markers that bound the overlays. */
1491 adjust_overlays_for_delete (from
, nchars_del
);
1492 adjust_overlays_for_insert (from
, inschars
);
1493 adjust_markers_for_insert (from
, from_byte
,
1494 from
+ inschars
, from_byte
+ outgoing_insbytes
,
1495 combined_before_bytes
, combined_after_bytes
, 0);
1497 #ifdef USE_TEXT_PROPERTIES
1498 offset_intervals (current_buffer
, PT
, inschars
- nchars_del
);
1500 /* Get the intervals for the part of the string we are inserting--
1501 not including the combined-before bytes. */
1502 intervals
= XSTRING (new)->intervals
;
1503 /* Insert those intervals. */
1504 graft_intervals_into_buffer (intervals
, from
, inschars
,
1505 current_buffer
, inherit
);
1508 /* Relocate point as if it were a marker. */
1510 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)
1511 + combined_after_bytes
),
1512 (from_byte
+ outgoing_insbytes
1513 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)
1514 + combined_after_bytes
));
1516 if (combined_after_bytes
)
1517 combine_bytes (from
+ inschars
, from_byte
+ outgoing_insbytes
,
1518 combined_after_bytes
);
1520 if (combined_before_bytes
)
1521 combine_bytes (from
, from_byte
, combined_before_bytes
);
1523 if (outgoing_insbytes
== 0)
1524 evaporate_overlays (from
);
1529 signal_after_change (from
, nchars_del
, PT
- from
);
1532 /* Delete characters in current buffer
1533 from FROM up to (but not including) TO.
1534 If TO comes before FROM, we delete nothing. */
1537 del_range (from
, to
)
1538 register int from
, to
;
1540 del_range_1 (from
, to
, 1);
1543 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
1546 del_range_1 (from
, to
, prepare
)
1547 int from
, to
, prepare
;
1549 int from_byte
, to_byte
;
1551 /* Make args be valid */
1562 int range_length
= to
- from
;
1563 prepare_to_modify_buffer (from
, to
, &from
);
1564 to
= from
+ range_length
;
1567 from_byte
= CHAR_TO_BYTE (from
);
1568 to_byte
= CHAR_TO_BYTE (to
);
1570 del_range_2 (from
, from_byte
, to
, to_byte
);
1573 /* Like del_range_1 but args are byte positions, not char positions. */
1576 del_range_byte (from_byte
, to_byte
, prepare
)
1577 int from_byte
, to_byte
, prepare
;
1581 /* Make args be valid */
1582 if (from_byte
< BEGV_BYTE
)
1583 from_byte
= BEGV_BYTE
;
1584 if (to_byte
> ZV_BYTE
)
1587 if (to_byte
<= from_byte
)
1590 from
= BYTE_TO_CHAR (from_byte
);
1591 to
= BYTE_TO_CHAR (to_byte
);
1595 int old_from
= from
, old_to
= Z
- to
;
1596 int range_length
= to
- from
;
1597 prepare_to_modify_buffer (from
, to
, &from
);
1598 to
= from
+ range_length
;
1600 if (old_from
!= from
)
1601 from_byte
= CHAR_TO_BYTE (from
);
1602 if (old_to
== Z
- to
)
1603 to_byte
= CHAR_TO_BYTE (to
);
1606 del_range_2 (from
, from_byte
, to
, to_byte
);
1609 /* Like del_range_1, but positions are specified both as charpos
1613 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
1614 int from
, from_byte
, to
, to_byte
, prepare
;
1616 /* Make args be valid */
1617 if (from_byte
< BEGV_BYTE
)
1618 from_byte
= BEGV_BYTE
;
1619 if (to_byte
> ZV_BYTE
)
1622 if (to_byte
<= from_byte
)
1632 int old_from
= from
, old_to
= Z
- to
;
1633 int range_length
= to
- from
;
1634 prepare_to_modify_buffer (from
, to
, &from
);
1635 to
= from
+ range_length
;
1637 if (old_from
!= from
)
1638 from_byte
= CHAR_TO_BYTE (from
);
1639 if (old_to
== Z
- to
)
1640 to_byte
= CHAR_TO_BYTE (to
);
1643 del_range_2 (from
, from_byte
, to
, to_byte
);
1646 /* Delete a range of text, specified both as character positions
1647 and byte positions. FROM and TO are character positions,
1648 while FROM_BYTE and TO_BYTE are byte positions. */
1651 del_range_2 (from
, from_byte
, to
, to_byte
)
1652 int from
, from_byte
, to
, to_byte
;
1654 register int nbytes_del
, nchars_del
;
1655 int combined_after_bytes
;
1657 nchars_del
= to
- from
;
1658 nbytes_del
= to_byte
- from_byte
;
1660 /* Make sure the gap is somewhere in or next to what we are deleting. */
1662 gap_right (from
, from_byte
);
1664 gap_left (to
, to_byte
, 0);
1666 /* Relocate all markers pointing into the new, larger gap
1667 to point at the end of the text before the gap.
1668 Do this before recording the deletion,
1669 so that undo handles this after reinserting the text. */
1670 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1672 record_delete (from
, nchars_del
);
1675 /* Relocate point as if it were a marker. */
1677 adjust_point (from
- (PT
< to
? PT
: to
),
1678 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1680 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1681 offset_intervals (current_buffer
, from
, - nchars_del
);
1683 /* Adjust the overlay center as needed. This must be done after
1684 adjusting the markers that bound the overlays. */
1685 adjust_overlays_for_delete (from_byte
, nchars_del
);
1687 GAP_SIZE
+= nbytes_del
;
1688 ZV_BYTE
-= nbytes_del
;
1689 Z_BYTE
-= nbytes_del
;
1693 GPT_BYTE
= from_byte
;
1694 *(GPT_ADDR
) = 0; /* Put an anchor. */
1699 if (GPT
- BEG
< beg_unchanged
)
1700 beg_unchanged
= GPT
- BEG
;
1701 if (Z
- GPT
< end_unchanged
)
1702 end_unchanged
= Z
- GPT
;
1704 combined_after_bytes
1705 = count_combining_before (GAP_END_ADDR
, ZV_BYTE
- GPT_BYTE
, PT
, PT_BYTE
);
1707 if (combined_after_bytes
)
1708 move_gap_both (GPT
+ combined_after_bytes
,
1709 GPT_BYTE
+ combined_after_bytes
);
1711 if (combined_after_bytes
)
1712 combine_bytes (PT
, PT_BYTE
, combined_after_bytes
);
1714 evaporate_overlays (from
);
1715 signal_after_change (from
, nchars_del
, 0);
1718 /* Call this if you're about to change the region of BUFFER from
1719 character positions START to END. This checks the read-only
1720 properties of the region, calls the necessary modification hooks,
1721 and warns the next redisplay that it should pay attention to that
1725 modify_region (buffer
, start
, end
)
1726 struct buffer
*buffer
;
1729 struct buffer
*old_buffer
= current_buffer
;
1731 if (buffer
!= old_buffer
)
1732 set_buffer_internal (buffer
);
1734 prepare_to_modify_buffer (start
, end
, NULL
);
1736 if (start
- 1 < beg_unchanged
1737 || (unchanged_modified
== MODIFF
1738 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
1739 beg_unchanged
= start
- 1;
1740 if (Z
- end
< end_unchanged
1741 || (unchanged_modified
== MODIFF
1742 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
1743 end_unchanged
= Z
- end
;
1745 if (MODIFF
<= SAVE_MODIFF
)
1746 record_first_change ();
1749 buffer
->point_before_scroll
= Qnil
;
1751 if (buffer
!= old_buffer
)
1752 set_buffer_internal (old_buffer
);
1755 /* Check that it is okay to modify the buffer between START and END,
1756 which are char positions.
1758 Run the before-change-function, if any. If intervals are in use,
1759 verify that the text to be modified is not read-only, and call
1760 any modification properties the text may have.
1762 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1763 by holding its value temporarily in a marker. */
1766 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
1770 if (!NILP (current_buffer
->read_only
))
1771 Fbarf_if_buffer_read_only ();
1773 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1774 if (BUF_INTERVALS (current_buffer
) != 0)
1778 Lisp_Object preserve_marker
;
1779 struct gcpro gcpro1
;
1780 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1781 GCPRO1 (preserve_marker
);
1782 verify_interval_modification (current_buffer
, start
, end
);
1783 *preserve_ptr
= marker_position (preserve_marker
);
1784 unchain_marker (preserve_marker
);
1788 verify_interval_modification (current_buffer
, start
, end
);
1791 #ifdef CLASH_DETECTION
1792 if (!NILP (current_buffer
->file_truename
)
1793 /* Make binding buffer-file-name to nil effective. */
1794 && !NILP (current_buffer
->filename
)
1795 && SAVE_MODIFF
>= MODIFF
)
1796 lock_file (current_buffer
->file_truename
);
1798 /* At least warn if this file has changed on disk since it was visited. */
1799 if (!NILP (current_buffer
->filename
)
1800 && SAVE_MODIFF
>= MODIFF
1801 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1802 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
1803 call1 (intern ("ask-user-about-supersession-threat"),
1804 current_buffer
->filename
);
1805 #endif /* not CLASH_DETECTION */
1807 signal_before_change (start
, end
, preserve_ptr
);
1809 if (current_buffer
->newline_cache
)
1810 invalidate_region_cache (current_buffer
,
1811 current_buffer
->newline_cache
,
1812 start
- BEG
, Z
- end
);
1813 if (current_buffer
->width_run_cache
)
1814 invalidate_region_cache (current_buffer
,
1815 current_buffer
->width_run_cache
,
1816 start
- BEG
, Z
- end
);
1818 Vdeactivate_mark
= Qt
;
1821 /* These macros work with an argument named `preserve_ptr'
1822 and a local variable named `preserve_marker'. */
1824 #define PRESERVE_VALUE \
1825 if (preserve_ptr && NILP (preserve_marker)) \
1826 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1828 #define RESTORE_VALUE \
1829 if (! NILP (preserve_marker)) \
1831 *preserve_ptr = marker_position (preserve_marker); \
1832 unchain_marker (preserve_marker); \
1835 #define PRESERVE_START_END \
1836 if (NILP (start_marker)) \
1837 start_marker = Fcopy_marker (start, Qnil); \
1838 if (NILP (end_marker)) \
1839 end_marker = Fcopy_marker (end, Qnil);
1841 #define FETCH_START \
1842 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1845 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1847 /* Signal a change to the buffer immediately before it happens.
1848 START_INT and END_INT are the bounds of the text to be changed.
1850 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1851 by holding its value temporarily in a marker. */
1854 signal_before_change (start_int
, end_int
, preserve_ptr
)
1855 int start_int
, end_int
;
1858 Lisp_Object start
, end
;
1859 Lisp_Object start_marker
, end_marker
;
1860 Lisp_Object preserve_marker
;
1861 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1863 start
= make_number (start_int
);
1864 end
= make_number (end_int
);
1865 preserve_marker
= Qnil
;
1866 start_marker
= Qnil
;
1868 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
1870 /* If buffer is unmodified, run a special hook for that case. */
1871 if (SAVE_MODIFF
>= MODIFF
1872 && !NILP (Vfirst_change_hook
)
1873 && !NILP (Vrun_hooks
))
1877 call1 (Vrun_hooks
, Qfirst_change_hook
);
1880 /* Run the before-change-function if any.
1881 We don't bother "binding" this variable to nil
1882 because it is obsolete anyway and new code should not use it. */
1883 if (!NILP (Vbefore_change_function
))
1887 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
1890 /* Now run the before-change-functions if any. */
1891 if (!NILP (Vbefore_change_functions
))
1893 Lisp_Object args
[3];
1894 Lisp_Object before_change_functions
;
1895 Lisp_Object after_change_functions
;
1896 struct gcpro gcpro1
, gcpro2
;
1901 /* "Bind" before-change-functions and after-change-functions
1902 to nil--but in a way that errors don't know about.
1903 That way, if there's an error in them, they will stay nil. */
1904 before_change_functions
= Vbefore_change_functions
;
1905 after_change_functions
= Vafter_change_functions
;
1906 Vbefore_change_functions
= Qnil
;
1907 Vafter_change_functions
= Qnil
;
1908 GCPRO2 (before_change_functions
, after_change_functions
);
1910 /* Actually run the hook functions. */
1911 args
[0] = Qbefore_change_functions
;
1912 args
[1] = FETCH_START
;
1913 args
[2] = FETCH_END
;
1914 run_hook_list_with_args (before_change_functions
, 3, args
);
1916 /* "Unbind" the variables we "bound" to nil. */
1917 Vbefore_change_functions
= before_change_functions
;
1918 Vafter_change_functions
= after_change_functions
;
1922 if (!NILP (current_buffer
->overlays_before
)
1923 || !NILP (current_buffer
->overlays_after
))
1926 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
1927 FETCH_START
, FETCH_END
, Qnil
);
1930 if (! NILP (start_marker
))
1931 free_marker (start_marker
);
1932 if (! NILP (end_marker
))
1933 free_marker (end_marker
);
1938 /* Signal a change immediately after it happens.
1939 CHARPOS is the character position of the start of the changed text.
1940 LENDEL is the number of characters of the text before the change.
1941 (Not the whole buffer; just the part that was changed.)
1942 LENINS is the number of characters in that part of the text
1943 after the change. */
1946 signal_after_change (charpos
, lendel
, lenins
)
1947 int charpos
, lendel
, lenins
;
1949 /* If we are deferring calls to the after-change functions
1950 and there are no before-change functions,
1951 just record the args that we were going to use. */
1952 if (! NILP (Vcombine_after_change_calls
)
1953 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
1954 && NILP (current_buffer
->overlays_before
)
1955 && NILP (current_buffer
->overlays_after
))
1959 if (!NILP (combine_after_change_list
)
1960 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
1961 Fcombine_after_change_execute ();
1963 elt
= Fcons (make_number (charpos
- BEG
),
1964 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
1965 Fcons (make_number (lenins
- lendel
), Qnil
)));
1966 combine_after_change_list
1967 = Fcons (elt
, combine_after_change_list
);
1968 combine_after_change_buffer
= Fcurrent_buffer ();
1973 if (!NILP (combine_after_change_list
))
1974 Fcombine_after_change_execute ();
1976 /* Run the after-change-function if any.
1977 We don't bother "binding" this variable to nil
1978 because it is obsolete anyway and new code should not use it. */
1979 if (!NILP (Vafter_change_function
))
1980 call3 (Vafter_change_function
,
1981 make_number (charpos
), make_number (charpos
+ lenins
),
1982 make_number (lendel
));
1984 if (!NILP (Vafter_change_functions
))
1986 Lisp_Object args
[4];
1987 Lisp_Object before_change_functions
;
1988 Lisp_Object after_change_functions
;
1989 struct gcpro gcpro1
, gcpro2
;
1991 /* "Bind" before-change-functions and after-change-functions
1992 to nil--but in a way that errors don't know about.
1993 That way, if there's an error in them, they will stay nil. */
1994 before_change_functions
= Vbefore_change_functions
;
1995 after_change_functions
= Vafter_change_functions
;
1996 Vbefore_change_functions
= Qnil
;
1997 Vafter_change_functions
= Qnil
;
1998 GCPRO2 (before_change_functions
, after_change_functions
);
2000 /* Actually run the hook functions. */
2001 args
[0] = Qafter_change_functions
;
2002 XSETFASTINT (args
[1], charpos
);
2003 XSETFASTINT (args
[2], charpos
+ lenins
);
2004 XSETFASTINT (args
[3], lendel
);
2005 run_hook_list_with_args (after_change_functions
,
2008 /* "Unbind" the variables we "bound" to nil. */
2009 Vbefore_change_functions
= before_change_functions
;
2010 Vafter_change_functions
= after_change_functions
;
2014 if (!NILP (current_buffer
->overlays_before
)
2015 || !NILP (current_buffer
->overlays_after
))
2016 report_overlay_modification (make_number (charpos
),
2017 make_number (charpos
+ lenins
),
2019 make_number (charpos
),
2020 make_number (charpos
+ lenins
),
2021 make_number (lendel
));
2023 /* After an insertion, call the text properties
2024 insert-behind-hooks or insert-in-front-hooks. */
2026 report_interval_modification (charpos
, charpos
+ lenins
);
2030 Fcombine_after_change_execute_1 (val
)
2033 Vcombine_after_change_calls
= val
;
2037 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2038 Scombine_after_change_execute
, 0, 0, 0,
2039 "This function is for use internally in `combine-after-change-calls'.")
2042 register Lisp_Object val
;
2043 int count
= specpdl_ptr
- specpdl
;
2044 int beg
, end
, change
;
2048 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2050 Fset_buffer (combine_after_change_buffer
);
2052 /* # chars unchanged at beginning of buffer. */
2054 /* # chars unchanged at end of buffer. */
2056 /* Total amount of insertion (negative for deletion). */
2059 /* Scan the various individual changes,
2060 accumulating the range info in BEG, END and CHANGE. */
2061 for (tail
= combine_after_change_list
; CONSP (tail
);
2062 tail
= XCONS (tail
)->cdr
)
2065 int thisbeg
, thisend
, thischange
;
2067 /* Extract the info from the next element. */
2068 elt
= XCONS (tail
)->car
;
2071 thisbeg
= XINT (XCONS (elt
)->car
);
2073 elt
= XCONS (elt
)->cdr
;
2076 thisend
= XINT (XCONS (elt
)->car
);
2078 elt
= XCONS (elt
)->cdr
;
2081 thischange
= XINT (XCONS (elt
)->car
);
2083 /* Merge this range into the accumulated range. */
2084 change
+= thischange
;
2091 /* Get the current start and end positions of the range
2092 that was changed. */
2096 /* We are about to handle these, so discard them. */
2097 combine_after_change_list
= Qnil
;
2099 /* Now run the after-change functions for real.
2100 Turn off the flag that defers them. */
2101 record_unwind_protect (Fcombine_after_change_execute_1
,
2102 Vcombine_after_change_calls
);
2103 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2105 return unbind_to (count
, val
);
2110 staticpro (&combine_after_change_list
);
2111 combine_after_change_list
= Qnil
;
2113 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2114 "Used internally by the `combine-after-change-calls' macro.");
2115 Vcombine_after_change_calls
= Qnil
;
2117 defsubr (&Scombine_after_change_execute
);