1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
26 #include "intervals.h"
30 #include "blockinput.h"
31 #include "region-cache.h"
37 static void insert_from_string_1
P_ ((Lisp_Object
, int, int, int, int, int, int));
38 static void insert_from_buffer_1 ();
39 static void gap_left
P_ ((int, int, int));
40 static void gap_right
P_ ((int, int));
41 static void adjust_markers_gap_motion
P_ ((int, int, int));
42 static void adjust_markers_for_insert
P_ ((int, int, int, int, int));
43 void adjust_markers_for_delete
P_ ((int, int, int, int));
44 static void adjust_markers_for_replace
P_ ((int, int, int, int, int, int));
45 static void adjust_point
P_ ((int, int));
47 Lisp_Object
Fcombine_after_change_execute ();
49 /* Non-nil means don't call the after-change-functions right away,
50 just record an element in Vcombine_after_change_calls_list. */
51 Lisp_Object Vcombine_after_change_calls
;
53 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
54 describing changes which happened while combine_after_change_calls
55 was nonzero. We use this to decide how to call them
56 once the deferral ends.
59 BEG-UNCHANGED is the number of chars before the changed range.
60 END-UNCHANGED is the number of chars after the changed range,
61 and CHANGE-AMOUNT is the number of characters inserted by the change
62 (negative for a deletion). */
63 Lisp_Object combine_after_change_list
;
65 /* Buffer which combine_after_change_list is about. */
66 Lisp_Object combine_after_change_buffer
;
68 Lisp_Object Qinhibit_modification_hooks
;
71 /* Check all markers in the current buffer, looking for something invalid. */
73 static int check_markers_debug_flag
;
75 #define CHECK_MARKERS() \
76 if (check_markers_debug_flag) \
83 register struct Lisp_Marker
*tail
;
84 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
86 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
88 if (tail
->buffer
->text
!= current_buffer
->text
)
90 if (tail
->charpos
> Z
)
92 if (tail
->bytepos
> Z_BYTE
)
94 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (tail
->bytepos
)))
99 /* Move gap to position CHARPOS.
100 Note that this can quit! */
106 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
109 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
110 Note that this can quit! */
113 move_gap_both (charpos
, bytepos
)
114 int charpos
, bytepos
;
116 if (bytepos
< GPT_BYTE
)
117 gap_left (charpos
, bytepos
, 0);
118 else if (bytepos
> GPT_BYTE
)
119 gap_right (charpos
, bytepos
);
122 /* Move the gap to a position less than the current GPT.
123 BYTEPOS describes the new position as a byte position,
124 and CHARPOS is the corresponding char position.
125 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
128 gap_left (charpos
, bytepos
, newgap
)
129 register int charpos
, bytepos
;
132 register unsigned char *to
, *from
;
137 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
144 /* Now copy the characters. To move the gap down,
145 copy characters up. */
149 /* I gets number of characters left to copy. */
150 i
= new_s1
- bytepos
;
153 /* If a quit is requested, stop copying now.
154 Change BYTEPOS to be where we have actually moved the gap to. */
158 charpos
= BYTE_TO_CHAR (bytepos
);
161 /* Move at most 32000 chars before checking again for a quit. */
166 /* bcopy is safe if the two areas of memory do not overlap
167 or on systems where bcopy is always safe for moving upward. */
168 && (BCOPY_UPWARD_SAFE
169 || to
- from
>= 128))
171 /* If overlap is not safe, avoid it by not moving too many
172 characters at once. */
173 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
188 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
189 BYTEPOS is where the loop above stopped, which may be what was specified
190 or may be where a quit was detected. */
191 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
194 if (bytepos
< charpos
)
196 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
200 /* Move the gap to a position greater than than the current GPT.
201 BYTEPOS describes the new position as a byte position,
202 and CHARPOS is the corresponding char position. */
205 gap_right (charpos
, bytepos
)
206 register int charpos
, bytepos
;
208 register unsigned char *to
, *from
;
212 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
219 /* Now copy the characters. To move the gap up,
220 copy characters down. */
224 /* I gets number of characters left to copy. */
225 i
= bytepos
- new_s1
;
228 /* If a quit is requested, stop copying now.
229 Change BYTEPOS to be where we have actually moved the gap to. */
233 charpos
= BYTE_TO_CHAR (bytepos
);
236 /* Move at most 32000 chars before checking again for a quit. */
241 /* bcopy is safe if the two areas of memory do not overlap
242 or on systems where bcopy is always safe for moving downward. */
243 && (BCOPY_DOWNWARD_SAFE
244 || from
- to
>= 128))
246 /* If overlap is not safe, avoid it by not moving too many
247 characters at once. */
248 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
263 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
267 if (bytepos
< charpos
)
269 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
273 /* Add AMOUNT to the byte position of every marker in the current buffer
274 whose current byte position is between FROM (exclusive) and TO (inclusive).
276 Also, any markers past the outside of that interval, in the direction
277 of adjustment, are first moved back to the near end of the interval
278 and then adjusted by AMOUNT.
280 When the latter adjustment is done, if AMOUNT is negative,
281 we record the adjustment for undo. (This case happens only for
284 The markers' character positions are not altered,
285 because gap motion does not affect character positions. */
287 int adjust_markers_test
;
290 adjust_markers_gap_motion (from
, to
, amount
)
291 register int from
, to
, amount
;
293 /* Now that a marker has a bytepos, not counting the gap,
294 nothing needs to be done here. */
297 register struct Lisp_Marker
*m
;
300 marker
= BUF_MARKERS (current_buffer
);
302 while (!NILP (marker
))
304 m
= XMARKER (marker
);
308 if (mpos
> to
&& mpos
< to
+ amount
)
310 if (adjust_markers_test
)
317 /* Here's the case where a marker is inside text being deleted.
318 AMOUNT can be negative for gap motion, too,
319 but then this range contains no markers. */
320 if (mpos
> from
+ amount
&& mpos
<= from
)
322 if (adjust_markers_test
)
324 mpos
= from
+ amount
;
327 if (mpos
> from
&& mpos
<= to
)
335 /* Adjust all markers for a deletion
336 whose range in bytes is FROM_BYTE to TO_BYTE.
337 The range in charpos is FROM to TO.
339 This function assumes that the gap is adjacent to
340 or inside of the range being deleted. */
343 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
344 register int from
, from_byte
, to
, to_byte
;
347 register struct Lisp_Marker
*m
;
348 register int charpos
;
350 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
352 charpos
= m
->charpos
;
357 /* If the marker is after the deletion,
358 relocate by number of chars / bytes deleted. */
361 m
->charpos
-= to
- from
;
362 m
->bytepos
-= to_byte
- from_byte
;
364 /* Here's the case where a marker is inside text being deleted. */
365 else if (charpos
> from
)
367 if (! m
->insertion_type
)
368 { /* Normal markers will end up at the beginning of the
369 re-inserted text after undoing a deletion, and must be
370 adjusted to move them to the correct place. */
371 XSETMISC (marker
, m
);
372 record_marker_adjustment (marker
, from
- charpos
);
374 else if (charpos
< to
)
375 { /* Before-insertion markers will automatically move forward
376 upon re-inserting the deleted text, so we have to arrange
377 for them to move backward to the correct position. */
378 XSETMISC (marker
, m
);
379 record_marker_adjustment (marker
, charpos
- to
);
382 m
->bytepos
= from_byte
;
384 /* Here's the case where a before-insertion marker is immediately
385 before the deleted region. */
386 else if (charpos
== from
&& m
->insertion_type
)
388 /* Undoing the change uses normal insertion, which will
389 incorrectly make MARKER move forward, so we arrange for it
390 to then move backward to the correct place at the beginning
391 of the deleted region. */
392 XSETMISC (marker
, m
);
393 record_marker_adjustment (marker
, to
- from
);
399 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
400 to TO / TO_BYTE. We have to relocate the charpos of every marker
401 that points after the insertion (but not their bytepos).
403 When a marker points at the insertion point,
404 we advance it if either its insertion-type is t
405 or BEFORE_MARKERS is true. */
408 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
, before_markers
)
409 register int from
, from_byte
, to
, to_byte
;
412 struct Lisp_Marker
*m
;
414 int nchars
= to
- from
;
415 int nbytes
= to_byte
- from_byte
;
417 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
419 eassert (m
->bytepos
>= m
->charpos
420 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
422 if (m
->bytepos
== from_byte
)
424 if (m
->insertion_type
|| before_markers
)
426 m
->bytepos
= to_byte
;
428 if (m
->insertion_type
)
432 else if (m
->bytepos
> from_byte
)
434 m
->bytepos
+= nbytes
;
435 m
->charpos
+= nchars
;
439 /* Adjusting only markers whose insertion-type is t may result in
440 - disordered start and end in overlays, and
441 - disordered overlays in the slot `overlays_before' of current_buffer. */
444 fix_start_end_in_overlays(from
, to
);
445 fix_overlays_before (current_buffer
, from
, to
);
449 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
451 This is used only when the value of point changes due to an insert
452 or delete; it does not represent a conceptual change in point as a
453 marker. In particular, point is not crossing any interval
454 boundaries, so there's no need to use the usual SET_PT macro. In
455 fact it would be incorrect to do so, because either the old or the
456 new value of point is out of sync with the current set of
460 adjust_point (nchars
, nbytes
)
463 BUF_PT (current_buffer
) += nchars
;
464 BUF_PT_BYTE (current_buffer
) += nbytes
;
466 /* In a single-byte buffer, the two positions must be equal. */
467 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
470 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
471 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
472 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
476 adjust_markers_for_replace (from
, from_byte
, old_chars
, old_bytes
,
477 new_chars
, new_bytes
)
478 int from
, from_byte
, old_chars
, old_bytes
, new_chars
, new_bytes
;
480 register struct Lisp_Marker
*m
;
481 int prev_to_byte
= from_byte
+ old_bytes
;
482 int diff_chars
= new_chars
- old_chars
;
483 int diff_bytes
= new_bytes
- old_bytes
;
485 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
487 if (m
->bytepos
>= prev_to_byte
)
489 m
->charpos
+= diff_chars
;
490 m
->bytepos
+= diff_bytes
;
492 else if (m
->bytepos
> from_byte
)
495 m
->bytepos
= from_byte
;
503 /* Make the gap NBYTES_ADDED bytes longer. */
506 make_gap_larger (nbytes_added
)
511 int real_gap_loc_byte
;
514 /* If we have to get more space, get enough to last a while. */
515 nbytes_added
+= 2000;
517 /* Don't allow a buffer size that won't fit in an int
518 even if it will fit in a Lisp integer.
519 That won't work because so many places use `int'.
521 Make sure we don't introduce overflows in the calculation. */
523 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
524 >= (((EMACS_INT
) 1 << (min (VALBITS
, BITS_PER_INT
) - 1)) - 1
526 error ("Buffer exceeds maximum size");
528 enlarge_buffer_text (current_buffer
, nbytes_added
);
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
;
559 /* Make the gap NBYTES_REMOVED bytes shorter. */
562 make_gap_smaller (nbytes_removed
)
567 int real_gap_loc_byte
;
570 int real_beg_unchanged
;
573 /* Make sure the gap is at least 20 bytes. */
574 if (GAP_SIZE
- nbytes_removed
< 20)
575 nbytes_removed
= GAP_SIZE
- 20;
577 /* Prevent quitting in move_gap. */
582 real_gap_loc_byte
= GPT_BYTE
;
583 new_gap_size
= GAP_SIZE
- nbytes_removed
;
585 real_Z_byte
= Z_BYTE
;
586 real_beg_unchanged
= BEG_UNCHANGED
;
588 /* Pretend that the last unwanted part of the gap is the entire gap,
589 and that the first desired part of the gap is part of the buffer
591 bzero (GPT_ADDR
, new_gap_size
);
593 GPT_BYTE
+= new_gap_size
;
595 Z_BYTE
+= new_gap_size
;
596 GAP_SIZE
= nbytes_removed
;
598 /* Move the unwanted pretend gap to the end of the buffer. This
599 adjusts the markers properly too. */
600 gap_right (Z
, Z_BYTE
);
602 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
604 /* Now restore the desired gap. */
605 GAP_SIZE
= new_gap_size
;
607 GPT_BYTE
= real_gap_loc_byte
;
609 Z_BYTE
= real_Z_byte
;
610 BEG_UNCHANGED
= real_beg_unchanged
;
619 make_gap (nbytes_added
)
622 if (nbytes_added
>= 0)
623 make_gap_larger (nbytes_added
);
624 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
626 make_gap_smaller (-nbytes_added
);
630 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
631 FROM_MULTIBYTE says whether the incoming text is multibyte.
632 TO_MULTIBYTE says whether to store the text as multibyte.
633 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
635 Return the number of bytes stored at TO_ADDR. */
638 copy_text (from_addr
, to_addr
, nbytes
,
639 from_multibyte
, to_multibyte
)
640 const unsigned char *from_addr
;
641 unsigned char *to_addr
;
643 int from_multibyte
, to_multibyte
;
645 if (from_multibyte
== to_multibyte
)
647 bcopy (from_addr
, to_addr
, nbytes
);
650 else if (from_multibyte
)
653 int bytes_left
= nbytes
;
654 Lisp_Object tbl
= Qnil
;
656 /* We set the variable tbl to the reverse table of
657 Vnonascii_translation_table in advance. */
658 if (CHAR_TABLE_P (Vnonascii_translation_table
))
660 tbl
= Fchar_table_extra_slot (Vnonascii_translation_table
,
662 if (!CHAR_TABLE_P (tbl
))
666 /* Convert multibyte to single byte. */
667 while (bytes_left
> 0)
670 c
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
671 if (!SINGLE_BYTE_CHAR_P (c
))
672 c
= multibyte_char_to_unibyte (c
, tbl
);
674 from_addr
+= thislen
;
675 bytes_left
-= thislen
;
682 unsigned char *initial_to_addr
= to_addr
;
684 /* Convert single-byte to multibyte. */
687 int c
= *from_addr
++;
691 c
= unibyte_char_to_multibyte (c
);
692 to_addr
+= CHAR_STRING (c
, to_addr
);
696 /* Special case for speed. */
697 *to_addr
++ = c
, nbytes
--;
699 return to_addr
- initial_to_addr
;
703 /* Return the number of bytes it would take
704 to convert some single-byte text to multibyte.
705 The single-byte text consists of NBYTES bytes at PTR. */
708 count_size_as_multibyte (ptr
, nbytes
)
709 const unsigned char *ptr
;
713 int outgoing_nbytes
= 0;
715 for (i
= 0; i
< nbytes
; i
++)
717 unsigned int c
= *ptr
++;
723 c
= unibyte_char_to_multibyte (c
);
724 outgoing_nbytes
+= CHAR_BYTES (c
);
728 return outgoing_nbytes
;
731 /* Insert a string of specified length before point.
732 This function judges multibyteness based on
733 enable_multibyte_characters in the current buffer;
734 it never converts between single-byte and multibyte.
736 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
737 prepare_to_modify_buffer could relocate the text. */
740 insert (string
, nbytes
)
741 register const unsigned char *string
;
746 int len
= chars_in_text (string
, nbytes
), opoint
;
747 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
749 signal_after_change (opoint
, 0, len
);
750 update_compositions (opoint
, PT
, CHECK_BORDER
);
754 /* Likewise, but inherit text properties from neighboring characters. */
757 insert_and_inherit (string
, nbytes
)
758 register const unsigned char *string
;
763 int len
= chars_in_text (string
, nbytes
), opoint
;
764 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
766 signal_after_change (opoint
, 0, len
);
767 update_compositions (opoint
, PT
, CHECK_BORDER
);
771 /* Insert the character C before point. Do not inherit text properties. */
777 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
780 if (! NILP (current_buffer
->enable_multibyte_characters
))
781 len
= CHAR_STRING (c
, str
);
791 /* Insert the null-terminated string S before point. */
797 insert (s
, strlen (s
));
800 /* Like `insert' except that all markers pointing at the place where
801 the insertion happens are adjusted to point after it.
802 Don't use this function to insert part of a Lisp string,
803 since gc could happen and relocate it. */
806 insert_before_markers (string
, nbytes
)
807 const unsigned char *string
;
812 int len
= chars_in_text (string
, nbytes
), opoint
;
813 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
815 signal_after_change (opoint
, 0, len
);
816 update_compositions (opoint
, PT
, CHECK_BORDER
);
820 /* Likewise, but inherit text properties from neighboring characters. */
823 insert_before_markers_and_inherit (string
, nbytes
)
824 const unsigned char *string
;
829 int len
= chars_in_text (string
, nbytes
), opoint
;
830 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
832 signal_after_change (opoint
, 0, len
);
833 update_compositions (opoint
, PT
, CHECK_BORDER
);
837 /* Subroutine used by the insert functions above. */
840 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
841 register const unsigned char *string
;
843 int inherit
, prepare
, before_markers
;
845 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
846 inherit
, prepare
, before_markers
);
850 #ifdef BYTE_COMBINING_DEBUG
852 /* See if the bytes before POS/POS_BYTE combine with bytes
853 at the start of STRING to form a single character.
854 If so, return the number of bytes at the start of STRING
855 which combine in this way. Otherwise, return 0. */
858 count_combining_before (string
, length
, pos
, pos_byte
)
859 const unsigned char *string
;
863 int len
, combining_bytes
;
864 const unsigned char *p
;
866 if (NILP (current_buffer
->enable_multibyte_characters
))
869 /* At first, we can exclude the following cases:
870 (1) STRING[0] can't be a following byte of multibyte sequence.
871 (2) POS is the start of the current buffer.
872 (3) A character before POS is not a multibyte character. */
873 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
875 if (pos_byte
== BEG_BYTE
) /* case (2) */
878 p
= BYTE_POS_ADDR (pos_byte
- 1);
879 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
880 if (! BASE_LEADING_CODE_P (*p
)) /* case (3) */
883 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
884 if (combining_bytes
<= 0)
885 /* The character preceding POS is, complete and no room for
886 combining bytes (combining_bytes == 0), or an independent 8-bit
887 character (combining_bytes < 0). */
890 /* We have a combination situation. Count the bytes at STRING that
893 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
896 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
899 /* See if the bytes after POS/POS_BYTE combine with bytes
900 at the end of STRING to form a single character.
901 If so, return the number of bytes after POS/POS_BYTE
902 which combine in this way. Otherwise, return 0. */
905 count_combining_after (string
, length
, pos
, pos_byte
)
906 const unsigned char *string
;
910 int opos_byte
= pos_byte
;
915 if (NILP (current_buffer
->enable_multibyte_characters
))
918 /* At first, we can exclude the following cases:
919 (1) The last byte of STRING is an ASCII.
920 (2) POS is the last of the current buffer.
921 (3) A character at POS can't be a following byte of multibyte
923 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
925 if (pos_byte
== Z_BYTE
) /* case (2) */
927 bufp
= BYTE_POS_ADDR (pos_byte
);
928 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
932 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
938 /* All characters in STRING are not character head. We must
939 check also preceding bytes at POS. We are sure that the gap
941 unsigned char *p
= BEG_ADDR
;
943 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
945 if (i
< 0 || !BASE_LEADING_CODE_P (p
[i
]))
948 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
949 return (bytes
<= pos_byte
- 1 - i
+ length
951 : bytes
- (pos_byte
- 1 - i
+ length
));
953 if (!BASE_LEADING_CODE_P (string
[i
]))
956 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
958 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
960 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
966 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
967 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
968 are the same as in insert_1. */
971 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
972 register const unsigned char *string
;
973 register int nchars
, nbytes
;
974 int inherit
, prepare
, before_markers
;
979 if (NILP (current_buffer
->enable_multibyte_characters
))
983 /* Do this before moving and increasing the gap,
984 because the before-change hooks might move the gap
985 or make it smaller. */
986 prepare_to_modify_buffer (PT
, PT
, NULL
);
989 move_gap_both (PT
, PT_BYTE
);
990 if (GAP_SIZE
< nbytes
)
991 make_gap (nbytes
- GAP_SIZE
);
993 #ifdef BYTE_COMBINING_DEBUG
994 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
995 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
999 /* Record deletion of the surrounding text that combines with
1000 the insertion. This, together with recording the insertion,
1001 will add up to the right stuff in the undo list. */
1002 record_insert (PT
, nchars
);
1004 CHARS_MODIFF
= MODIFF
;
1006 bcopy (string
, GPT_ADDR
, nbytes
);
1015 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1020 /* The insert may have been in the unchanged region, so check again. */
1021 if (Z
- GPT
< END_UNCHANGED
)
1022 END_UNCHANGED
= Z
- GPT
;
1024 adjust_overlays_for_insert (PT
, nchars
);
1025 adjust_markers_for_insert (PT
, PT_BYTE
,
1026 PT
+ nchars
, PT_BYTE
+ nbytes
,
1029 if (BUF_INTERVALS (current_buffer
) != 0)
1030 offset_intervals (current_buffer
, PT
, nchars
);
1032 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1033 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1036 adjust_point (nchars
, nbytes
);
1041 /* Insert the part of the text of STRING, a Lisp object assumed to be
1042 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1043 starting at position POS / POS_BYTE. If the text of STRING has properties,
1044 copy them into the buffer.
1046 It does not work to use `insert' for this, because a GC could happen
1047 before we bcopy the stuff into the buffer, and relocate the string
1048 without insert noticing. */
1051 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1053 register int pos
, pos_byte
, length
, length_byte
;
1058 if (SCHARS (string
) == 0)
1061 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1063 signal_after_change (opoint
, 0, PT
- opoint
);
1064 update_compositions (opoint
, PT
, CHECK_BORDER
);
1067 /* Like `insert_from_string' except that all markers pointing
1068 at the place where the insertion happens are adjusted to point after it. */
1071 insert_from_string_before_markers (string
, pos
, pos_byte
,
1072 length
, length_byte
, inherit
)
1074 register int pos
, pos_byte
, length
, length_byte
;
1079 if (SCHARS (string
) == 0)
1082 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1084 signal_after_change (opoint
, 0, PT
- opoint
);
1085 update_compositions (opoint
, PT
, CHECK_BORDER
);
1088 /* Subroutine of the insertion functions above. */
1091 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1092 inherit
, before_markers
)
1094 register int pos
, pos_byte
, nchars
, nbytes
;
1095 int inherit
, before_markers
;
1097 struct gcpro gcpro1
;
1098 int outgoing_nbytes
= nbytes
;
1101 /* Make OUTGOING_NBYTES describe the text
1102 as it will be inserted in this buffer. */
1104 if (NILP (current_buffer
->enable_multibyte_characters
))
1105 outgoing_nbytes
= nchars
;
1106 else if (! STRING_MULTIBYTE (string
))
1108 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
1112 /* Do this before moving and increasing the gap,
1113 because the before-change hooks might move the gap
1114 or make it smaller. */
1115 prepare_to_modify_buffer (PT
, PT
, NULL
);
1118 move_gap_both (PT
, PT_BYTE
);
1119 if (GAP_SIZE
< outgoing_nbytes
)
1120 make_gap (outgoing_nbytes
- GAP_SIZE
);
1123 /* Copy the string text into the buffer, perhaps converting
1124 between single-byte and multibyte. */
1125 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
1126 STRING_MULTIBYTE (string
),
1127 ! NILP (current_buffer
->enable_multibyte_characters
));
1129 #ifdef BYTE_COMBINING_DEBUG
1130 /* We have copied text into the gap, but we have not altered
1131 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1132 to these functions and get the same results as we would
1133 have got earlier on. Meanwhile, PT_ADDR does point to
1134 the text that has been stored by copy_text. */
1135 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1136 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1140 record_insert (PT
, nchars
);
1142 CHARS_MODIFF
= MODIFF
;
1144 GAP_SIZE
-= outgoing_nbytes
;
1148 GPT_BYTE
+= outgoing_nbytes
;
1149 ZV_BYTE
+= outgoing_nbytes
;
1150 Z_BYTE
+= outgoing_nbytes
;
1151 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1156 /* The insert may have been in the unchanged region, so check again. */
1157 if (Z
- GPT
< END_UNCHANGED
)
1158 END_UNCHANGED
= Z
- GPT
;
1160 adjust_overlays_for_insert (PT
, nchars
);
1161 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1162 PT_BYTE
+ outgoing_nbytes
,
1165 offset_intervals (current_buffer
, PT
, nchars
);
1167 intervals
= STRING_INTERVALS (string
);
1168 /* Get the intervals for the part of the string we are inserting. */
1169 if (nbytes
< SBYTES (string
))
1170 intervals
= copy_intervals (intervals
, pos
, nchars
);
1172 /* Insert those intervals. */
1173 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1174 current_buffer
, inherit
);
1176 adjust_point (nchars
, outgoing_nbytes
);
1179 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1180 current buffer. If the text in BUF has properties, they are absorbed
1181 into the current buffer.
1183 It does not work to use `insert' for this, because a malloc could happen
1184 and relocate BUF's text before the bcopy happens. */
1187 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1189 int charpos
, nchars
;
1194 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1195 signal_after_change (opoint
, 0, PT
- opoint
);
1196 update_compositions (opoint
, PT
, CHECK_BORDER
);
1200 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1205 register Lisp_Object temp
;
1206 int chunk
, chunk_expanded
;
1207 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1208 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1209 int incoming_nbytes
= to_byte
- from_byte
;
1210 int outgoing_nbytes
= incoming_nbytes
;
1213 /* Make OUTGOING_NBYTES describe the text
1214 as it will be inserted in this buffer. */
1216 if (NILP (current_buffer
->enable_multibyte_characters
))
1217 outgoing_nbytes
= nchars
;
1218 else if (NILP (buf
->enable_multibyte_characters
))
1220 int outgoing_before_gap
= 0;
1221 int outgoing_after_gap
= 0;
1223 if (from
< BUF_GPT (buf
))
1225 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1226 if (chunk
> incoming_nbytes
)
1227 chunk
= incoming_nbytes
;
1229 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1235 if (chunk
< incoming_nbytes
)
1237 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1239 incoming_nbytes
- chunk
);
1241 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1244 /* Make sure point-max won't overflow after this insertion. */
1245 XSETINT (temp
, outgoing_nbytes
+ Z
);
1246 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1247 error ("Maximum buffer size exceeded");
1249 /* Do this before moving and increasing the gap,
1250 because the before-change hooks might move the gap
1251 or make it smaller. */
1252 prepare_to_modify_buffer (PT
, PT
, NULL
);
1255 move_gap_both (PT
, PT_BYTE
);
1256 if (GAP_SIZE
< outgoing_nbytes
)
1257 make_gap (outgoing_nbytes
- GAP_SIZE
);
1259 if (from
< BUF_GPT (buf
))
1261 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1262 if (chunk
> incoming_nbytes
)
1263 chunk
= incoming_nbytes
;
1264 /* Record number of output bytes, so we know where
1265 to put the output from the second copy_text. */
1267 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1269 ! NILP (buf
->enable_multibyte_characters
),
1270 ! NILP (current_buffer
->enable_multibyte_characters
));
1273 chunk_expanded
= chunk
= 0;
1275 if (chunk
< incoming_nbytes
)
1276 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1277 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1278 ! NILP (buf
->enable_multibyte_characters
),
1279 ! NILP (current_buffer
->enable_multibyte_characters
));
1281 #ifdef BYTE_COMBINING_DEBUG
1282 /* We have copied text into the gap, but we have not altered
1283 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1284 to these functions and get the same results as we would
1285 have got earlier on. Meanwhile, GPT_ADDR does point to
1286 the text that has been stored by copy_text. */
1287 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1288 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1292 record_insert (PT
, nchars
);
1294 CHARS_MODIFF
= MODIFF
;
1296 GAP_SIZE
-= outgoing_nbytes
;
1300 GPT_BYTE
+= outgoing_nbytes
;
1301 ZV_BYTE
+= outgoing_nbytes
;
1302 Z_BYTE
+= outgoing_nbytes
;
1303 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1308 /* The insert may have been in the unchanged region, so check again. */
1309 if (Z
- GPT
< END_UNCHANGED
)
1310 END_UNCHANGED
= Z
- GPT
;
1312 adjust_overlays_for_insert (PT
, nchars
);
1313 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1314 PT_BYTE
+ outgoing_nbytes
,
1317 if (BUF_INTERVALS (current_buffer
) != 0)
1318 offset_intervals (current_buffer
, PT
, nchars
);
1320 /* Get the intervals for the part of the string we are inserting. */
1321 intervals
= BUF_INTERVALS (buf
);
1322 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1324 if (buf
== current_buffer
&& PT
<= from
)
1326 intervals
= copy_intervals (intervals
, from
, nchars
);
1329 /* Insert those intervals. */
1330 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1332 adjust_point (nchars
, outgoing_nbytes
);
1335 /* Record undo information and adjust markers and position keepers for
1336 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1337 chars (LEN_BYTE bytes) which resides in the gap just after
1340 PREV_TEXT nil means the new text was just inserted. */
1343 adjust_after_replace (from
, from_byte
, prev_text
, len
, len_byte
)
1344 int from
, from_byte
, len
, len_byte
;
1345 Lisp_Object prev_text
;
1347 int nchars_del
= 0, nbytes_del
= 0;
1349 #ifdef BYTE_COMBINING_DEBUG
1350 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1351 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1355 if (STRINGP (prev_text
))
1357 nchars_del
= SCHARS (prev_text
);
1358 nbytes_del
= SBYTES (prev_text
);
1361 /* Update various buffer positions for the new text. */
1362 GAP_SIZE
-= len_byte
;
1364 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1365 GPT
+= len
; GPT_BYTE
+= len_byte
;
1366 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1369 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1372 adjust_markers_for_insert (from
, from_byte
,
1373 from
+ len
, from_byte
+ len_byte
, 0);
1375 if (! EQ (current_buffer
->undo_list
, Qt
))
1378 record_delete (from
, prev_text
);
1379 record_insert (from
, len
);
1382 if (len
> nchars_del
)
1383 adjust_overlays_for_insert (from
, len
- nchars_del
);
1384 else if (len
< nchars_del
)
1385 adjust_overlays_for_delete (from
, nchars_del
- len
);
1386 if (BUF_INTERVALS (current_buffer
) != 0)
1388 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1392 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1394 /* As byte combining will decrease Z, we must check this again. */
1395 if (Z
- GPT
< END_UNCHANGED
)
1396 END_UNCHANGED
= Z
- GPT
;
1401 evaporate_overlays (from
);
1403 CHARS_MODIFF
= MODIFF
;
1406 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1407 This is for use when undo is not enabled in the current buffer. */
1410 adjust_after_replace_noundo (from
, from_byte
, nchars_del
, nbytes_del
, len
, len_byte
)
1411 int from
, from_byte
, nchars_del
, nbytes_del
, len
, len_byte
;
1413 #ifdef BYTE_COMBINING_DEBUG
1414 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1415 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1419 /* Update various buffer positions for the new text. */
1420 GAP_SIZE
-= len_byte
;
1422 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1423 GPT
+= len
; GPT_BYTE
+= len_byte
;
1424 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1427 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1430 adjust_markers_for_insert (from
, from_byte
,
1431 from
+ len
, from_byte
+ len_byte
, 0);
1433 if (len
> nchars_del
)
1434 adjust_overlays_for_insert (from
, len
- nchars_del
);
1435 else if (len
< nchars_del
)
1436 adjust_overlays_for_delete (from
, nchars_del
- len
);
1437 if (BUF_INTERVALS (current_buffer
) != 0)
1439 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1443 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1445 /* As byte combining will decrease Z, we must check this again. */
1446 if (Z
- GPT
< END_UNCHANGED
)
1447 END_UNCHANGED
= Z
- GPT
;
1452 evaporate_overlays (from
);
1454 CHARS_MODIFF
= MODIFF
;
1457 /* Record undo information, adjust markers and position keepers for an
1458 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1459 text already exists in the current buffer but character length (TO
1460 - FROM) may be incorrect, the correct length is NEWLEN. */
1463 adjust_after_insert (from
, from_byte
, to
, to_byte
, newlen
)
1464 int from
, from_byte
, to
, to_byte
, newlen
;
1466 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
1469 move_gap_both (to
, to_byte
);
1470 GAP_SIZE
+= len_byte
;
1471 GPT
-= len
; GPT_BYTE
-= len_byte
;
1472 ZV
-= len
; ZV_BYTE
-= len_byte
;
1473 Z
-= len
; Z_BYTE
-= len_byte
;
1474 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1477 /* Replace the text from character positions FROM to TO with NEW,
1478 If PREPARE is nonzero, call prepare_to_modify_buffer.
1479 If INHERIT, the newly inserted text should inherit text properties
1480 from the surrounding non-deleted text. */
1482 /* Note that this does not yet handle markers quite right.
1483 Also it needs to record a single undo-entry that does a replacement
1484 rather than a separate delete and insert.
1485 That way, undo will also handle markers properly.
1487 But if MARKERS is 0, don't relocate markers. */
1490 replace_range (from
, to
, new, prepare
, inherit
, markers
)
1492 int from
, to
, prepare
, inherit
, markers
;
1494 int inschars
= SCHARS (new);
1495 int insbytes
= SBYTES (new);
1496 int from_byte
, to_byte
;
1497 int nbytes_del
, nchars_del
;
1498 register Lisp_Object temp
;
1499 struct gcpro gcpro1
;
1501 int outgoing_insbytes
= insbytes
;
1502 Lisp_Object deletion
;
1511 int range_length
= to
- from
;
1512 prepare_to_modify_buffer (from
, to
, &from
);
1513 to
= from
+ range_length
;
1518 /* Make args be valid */
1524 from_byte
= CHAR_TO_BYTE (from
);
1525 to_byte
= CHAR_TO_BYTE (to
);
1527 nchars_del
= to
- from
;
1528 nbytes_del
= to_byte
- from_byte
;
1530 if (nbytes_del
<= 0 && insbytes
== 0)
1533 /* Make OUTGOING_INSBYTES describe the text
1534 as it will be inserted in this buffer. */
1536 if (NILP (current_buffer
->enable_multibyte_characters
))
1537 outgoing_insbytes
= inschars
;
1538 else if (! STRING_MULTIBYTE (new))
1540 = count_size_as_multibyte (SDATA (new), insbytes
);
1542 /* Make sure point-max won't overflow after this insertion. */
1543 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1544 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1545 error ("Maximum buffer size exceeded");
1549 /* Make sure the gap is somewhere in or next to what we are deleting. */
1551 gap_right (from
, from_byte
);
1553 gap_left (to
, to_byte
, 0);
1555 /* Even if we don't record for undo, we must keep the original text
1556 because we may have to recover it because of inappropriate byte
1558 if (! EQ (current_buffer
->undo_list
, Qt
))
1559 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1561 GAP_SIZE
+= nbytes_del
;
1564 ZV_BYTE
-= nbytes_del
;
1565 Z_BYTE
-= nbytes_del
;
1567 GPT_BYTE
= from_byte
;
1568 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1573 if (GPT
- BEG
< BEG_UNCHANGED
)
1574 BEG_UNCHANGED
= GPT
- BEG
;
1575 if (Z
- GPT
< END_UNCHANGED
)
1576 END_UNCHANGED
= Z
- GPT
;
1578 if (GAP_SIZE
< insbytes
)
1579 make_gap (insbytes
- GAP_SIZE
);
1581 /* Copy the string text into the buffer, perhaps converting
1582 between single-byte and multibyte. */
1583 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1584 STRING_MULTIBYTE (new),
1585 ! NILP (current_buffer
->enable_multibyte_characters
));
1587 #ifdef BYTE_COMBINING_DEBUG
1588 /* We have copied text into the gap, but we have not marked
1589 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1590 here, for both the previous text and the following text.
1591 Meanwhile, GPT_ADDR does point to
1592 the text that has been stored by copy_text. */
1593 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1594 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1598 if (! EQ (current_buffer
->undo_list
, Qt
))
1600 /* Record the insertion first, so that when we undo,
1601 the deletion will be undone first. Thus, undo
1602 will insert before deleting, and thus will keep
1603 the markers before and after this text separate. */
1604 record_insert (from
+ SCHARS (deletion
), inschars
);
1605 record_delete (from
, deletion
);
1608 GAP_SIZE
-= outgoing_insbytes
;
1612 GPT_BYTE
+= outgoing_insbytes
;
1613 ZV_BYTE
+= outgoing_insbytes
;
1614 Z_BYTE
+= outgoing_insbytes
;
1615 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1620 /* Adjust the overlay center as needed. This must be done after
1621 adjusting the markers that bound the overlays. */
1622 adjust_overlays_for_delete (from
, nchars_del
);
1623 adjust_overlays_for_insert (from
, inschars
);
1625 /* Adjust markers for the deletion and the insertion. */
1627 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1628 inschars
, outgoing_insbytes
);
1630 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1632 /* Get the intervals for the part of the string we are inserting--
1633 not including the combined-before bytes. */
1634 intervals
= STRING_INTERVALS (new);
1635 /* Insert those intervals. */
1636 graft_intervals_into_buffer (intervals
, from
, inschars
,
1637 current_buffer
, inherit
);
1639 /* Relocate point as if it were a marker. */
1641 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1642 (from_byte
+ outgoing_insbytes
1643 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1645 if (outgoing_insbytes
== 0)
1646 evaporate_overlays (from
);
1651 CHARS_MODIFF
= MODIFF
;
1654 signal_after_change (from
, nchars_del
, GPT
- from
);
1655 update_compositions (from
, GPT
, CHECK_BORDER
);
1658 /* Replace the text from character positions FROM to TO with
1659 the text in INS of length INSCHARS.
1660 Keep the text properties that applied to the old characters
1661 (extending them to all the new chars if there are more new chars).
1663 Note that this does not yet handle markers quite right.
1665 If MARKERS is nonzero, relocate markers.
1667 Unlike most functions at this level, never call
1668 prepare_to_modify_buffer and never call signal_after_change. */
1671 replace_range_2 (from
, from_byte
, to
, to_byte
, ins
, inschars
, insbytes
, markers
)
1672 int from
, from_byte
, to
, to_byte
;
1674 int inschars
, insbytes
, markers
;
1676 int nbytes_del
, nchars_del
;
1681 nchars_del
= to
- from
;
1682 nbytes_del
= to_byte
- from_byte
;
1684 if (nbytes_del
<= 0 && insbytes
== 0)
1687 /* Make sure point-max won't overflow after this insertion. */
1688 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1689 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1690 error ("Maximum buffer size exceeded");
1692 /* Make sure the gap is somewhere in or next to what we are deleting. */
1694 gap_right (from
, from_byte
);
1696 gap_left (to
, to_byte
, 0);
1698 GAP_SIZE
+= nbytes_del
;
1701 ZV_BYTE
-= nbytes_del
;
1702 Z_BYTE
-= nbytes_del
;
1704 GPT_BYTE
= from_byte
;
1705 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1710 if (GPT
- BEG
< BEG_UNCHANGED
)
1711 BEG_UNCHANGED
= GPT
- BEG
;
1712 if (Z
- GPT
< END_UNCHANGED
)
1713 END_UNCHANGED
= Z
- GPT
;
1715 if (GAP_SIZE
< insbytes
)
1716 make_gap (insbytes
- GAP_SIZE
);
1718 /* Copy the replacement text into the buffer. */
1719 bcopy (ins
, GPT_ADDR
, insbytes
);
1721 #ifdef BYTE_COMBINING_DEBUG
1722 /* We have copied text into the gap, but we have not marked
1723 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1724 here, for both the previous text and the following text.
1725 Meanwhile, GPT_ADDR does point to
1726 the text that has been stored by copy_text. */
1727 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1728 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1732 GAP_SIZE
-= insbytes
;
1736 GPT_BYTE
+= insbytes
;
1737 ZV_BYTE
+= insbytes
;
1739 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1744 /* Adjust the overlay center as needed. This must be done after
1745 adjusting the markers that bound the overlays. */
1746 if (nchars_del
!= inschars
)
1748 adjust_overlays_for_insert (from
, inschars
);
1749 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1752 /* Adjust markers for the deletion and the insertion. */
1754 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1755 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1756 inschars
, insbytes
);
1758 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1760 /* Relocate point as if it were a marker. */
1761 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1764 /* PT was within the deleted text. Move it to FROM. */
1765 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1767 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1771 evaporate_overlays (from
);
1776 CHARS_MODIFF
= MODIFF
;
1779 /* Delete characters in current buffer
1780 from FROM up to (but not including) TO.
1781 If TO comes before FROM, we delete nothing. */
1784 del_range (from
, to
)
1785 register int from
, to
;
1787 del_range_1 (from
, to
, 1, 0);
1790 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1791 RET_STRING says to return the deleted text. */
1794 del_range_1 (from
, to
, prepare
, ret_string
)
1795 int from
, to
, prepare
, ret_string
;
1797 int from_byte
, to_byte
;
1798 Lisp_Object deletion
;
1799 struct gcpro gcpro1
;
1801 /* Make args be valid */
1812 int range_length
= to
- from
;
1813 prepare_to_modify_buffer (from
, to
, &from
);
1814 to
= min (ZV
, from
+ range_length
);
1817 from_byte
= CHAR_TO_BYTE (from
);
1818 to_byte
= CHAR_TO_BYTE (to
);
1820 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1822 signal_after_change (from
, to
- from
, 0);
1823 update_compositions (from
, from
, CHECK_HEAD
);
1828 /* Like del_range_1 but args are byte positions, not char positions. */
1831 del_range_byte (from_byte
, to_byte
, prepare
)
1832 int from_byte
, to_byte
, prepare
;
1836 /* Make args be valid */
1837 if (from_byte
< BEGV_BYTE
)
1838 from_byte
= BEGV_BYTE
;
1839 if (to_byte
> ZV_BYTE
)
1842 if (to_byte
<= from_byte
)
1845 from
= BYTE_TO_CHAR (from_byte
);
1846 to
= BYTE_TO_CHAR (to_byte
);
1850 int old_from
= from
, old_to
= Z
- to
;
1851 int range_length
= to
- from
;
1852 prepare_to_modify_buffer (from
, to
, &from
);
1853 to
= from
+ range_length
;
1855 if (old_from
!= from
)
1856 from_byte
= CHAR_TO_BYTE (from
);
1862 else if (old_to
== Z
- to
)
1863 to_byte
= CHAR_TO_BYTE (to
);
1866 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1867 signal_after_change (from
, to
- from
, 0);
1868 update_compositions (from
, from
, CHECK_HEAD
);
1871 /* Like del_range_1, but positions are specified both as charpos
1875 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
1876 int from
, from_byte
, to
, to_byte
, prepare
;
1878 /* Make args be valid */
1879 if (from_byte
< BEGV_BYTE
)
1880 from_byte
= BEGV_BYTE
;
1881 if (to_byte
> ZV_BYTE
)
1884 if (to_byte
<= from_byte
)
1894 int old_from
= from
, old_to
= Z
- to
;
1895 int range_length
= to
- from
;
1896 prepare_to_modify_buffer (from
, to
, &from
);
1897 to
= from
+ range_length
;
1899 if (old_from
!= from
)
1900 from_byte
= CHAR_TO_BYTE (from
);
1906 else if (old_to
== Z
- to
)
1907 to_byte
= CHAR_TO_BYTE (to
);
1910 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1911 signal_after_change (from
, to
- from
, 0);
1912 update_compositions (from
, from
, CHECK_HEAD
);
1915 /* Delete a range of text, specified both as character positions
1916 and byte positions. FROM and TO are character positions,
1917 while FROM_BYTE and TO_BYTE are byte positions.
1918 If RET_STRING is true, the deleted area is returned as a string. */
1921 del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
)
1922 int from
, from_byte
, to
, to_byte
, ret_string
;
1924 register int nbytes_del
, nchars_del
;
1925 Lisp_Object deletion
;
1929 nchars_del
= to
- from
;
1930 nbytes_del
= to_byte
- from_byte
;
1932 /* Make sure the gap is somewhere in or next to what we are deleting. */
1934 gap_right (from
, from_byte
);
1936 gap_left (to
, to_byte
, 0);
1938 #ifdef BYTE_COMBINING_DEBUG
1939 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1940 Z_BYTE
- to_byte
, from
, from_byte
))
1944 if (ret_string
|| ! EQ (current_buffer
->undo_list
, Qt
))
1945 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1949 /* Relocate all markers pointing into the new, larger gap
1950 to point at the end of the text before the gap.
1951 Do this before recording the deletion,
1952 so that undo handles this after reinserting the text. */
1953 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1955 if (! EQ (current_buffer
->undo_list
, Qt
))
1956 record_delete (from
, deletion
);
1958 CHARS_MODIFF
= MODIFF
;
1960 /* Relocate point as if it were a marker. */
1962 adjust_point (from
- (PT
< to
? PT
: to
),
1963 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1965 offset_intervals (current_buffer
, from
, - nchars_del
);
1967 /* Adjust the overlay center as needed. This must be done after
1968 adjusting the markers that bound the overlays. */
1969 adjust_overlays_for_delete (from
, nchars_del
);
1971 GAP_SIZE
+= nbytes_del
;
1972 ZV_BYTE
-= nbytes_del
;
1973 Z_BYTE
-= nbytes_del
;
1977 GPT_BYTE
= from_byte
;
1978 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1983 if (GPT
- BEG
< BEG_UNCHANGED
)
1984 BEG_UNCHANGED
= GPT
- BEG
;
1985 if (Z
- GPT
< END_UNCHANGED
)
1986 END_UNCHANGED
= Z
- GPT
;
1990 evaporate_overlays (from
);
1995 /* Call this if you're about to change the region of BUFFER from
1996 character positions START to END. This checks the read-only
1997 properties of the region, calls the necessary modification hooks,
1998 and warns the next redisplay that it should pay attention to that
2001 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
2002 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
2005 modify_region (buffer
, start
, end
, preserve_chars_modiff
)
2006 struct buffer
*buffer
;
2007 int start
, end
, preserve_chars_modiff
;
2009 struct buffer
*old_buffer
= current_buffer
;
2011 if (buffer
!= old_buffer
)
2012 set_buffer_internal (buffer
);
2014 prepare_to_modify_buffer (start
, end
, NULL
);
2016 BUF_COMPUTE_UNCHANGED (buffer
, start
- 1, end
);
2018 if (MODIFF
<= SAVE_MODIFF
)
2019 record_first_change ();
2021 if (! preserve_chars_modiff
)
2022 CHARS_MODIFF
= MODIFF
;
2024 buffer
->point_before_scroll
= Qnil
;
2026 if (buffer
!= old_buffer
)
2027 set_buffer_internal (old_buffer
);
2030 /* Check that it is okay to modify the buffer between START and END,
2031 which are char positions.
2033 Run the before-change-function, if any. If intervals are in use,
2034 verify that the text to be modified is not read-only, and call
2035 any modification properties the text may have.
2037 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2038 by holding its value temporarily in a marker. */
2041 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
2045 struct buffer
*base_buffer
;
2047 if (!NILP (current_buffer
->read_only
))
2048 Fbarf_if_buffer_read_only ();
2050 /* Let redisplay consider other windows than selected_window
2051 if modifying another buffer. */
2052 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
2053 ++windows_or_buffers_changed
;
2055 if (BUF_INTERVALS (current_buffer
) != 0)
2059 Lisp_Object preserve_marker
;
2060 struct gcpro gcpro1
;
2061 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2062 GCPRO1 (preserve_marker
);
2063 verify_interval_modification (current_buffer
, start
, end
);
2064 *preserve_ptr
= marker_position (preserve_marker
);
2065 unchain_marker (XMARKER (preserve_marker
));
2069 verify_interval_modification (current_buffer
, start
, end
);
2072 /* For indirect buffers, use the base buffer to check clashes. */
2073 if (current_buffer
->base_buffer
!= 0)
2074 base_buffer
= current_buffer
->base_buffer
;
2076 base_buffer
= current_buffer
;
2078 #ifdef CLASH_DETECTION
2079 if (!NILP (base_buffer
->file_truename
)
2080 /* Make binding buffer-file-name to nil effective. */
2081 && !NILP (base_buffer
->filename
)
2082 && SAVE_MODIFF
>= MODIFF
)
2083 lock_file (base_buffer
->file_truename
);
2085 /* At least warn if this file has changed on disk since it was visited. */
2086 if (!NILP (base_buffer
->filename
)
2087 && SAVE_MODIFF
>= MODIFF
2088 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2089 && !NILP (Ffile_exists_p (base_buffer
->filename
)))
2090 call1 (intern ("ask-user-about-supersession-threat"),
2091 base_buffer
->filename
);
2092 #endif /* not CLASH_DETECTION */
2094 signal_before_change (start
, end
, preserve_ptr
);
2096 if (current_buffer
->newline_cache
)
2097 invalidate_region_cache (current_buffer
,
2098 current_buffer
->newline_cache
,
2099 start
- BEG
, Z
- end
);
2100 if (current_buffer
->width_run_cache
)
2101 invalidate_region_cache (current_buffer
,
2102 current_buffer
->width_run_cache
,
2103 start
- BEG
, Z
- end
);
2105 Vdeactivate_mark
= Qt
;
2108 /* These macros work with an argument named `preserve_ptr'
2109 and a local variable named `preserve_marker'. */
2111 #define PRESERVE_VALUE \
2112 if (preserve_ptr && NILP (preserve_marker)) \
2113 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2115 #define RESTORE_VALUE \
2116 if (! NILP (preserve_marker)) \
2118 *preserve_ptr = marker_position (preserve_marker); \
2119 unchain_marker (XMARKER (preserve_marker)); \
2122 #define PRESERVE_START_END \
2123 if (NILP (start_marker)) \
2124 start_marker = Fcopy_marker (start, Qnil); \
2125 if (NILP (end_marker)) \
2126 end_marker = Fcopy_marker (end, Qnil);
2128 #define FETCH_START \
2129 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2132 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2134 /* Set a variable to nil if an error occurred.
2135 Don't change the variable if there was no error.
2136 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2137 VARIABLE is the variable to maybe set to nil.
2138 NO-ERROR-FLAG is nil if there was an error,
2139 anything else meaning no error (so this function does nothing). */
2141 reset_var_on_error (val
)
2144 if (NILP (XCDR (val
)))
2145 Fset (XCAR (val
), Qnil
);
2149 /* Signal a change to the buffer immediately before it happens.
2150 START_INT and END_INT are the bounds of the text to be changed.
2152 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2153 by holding its value temporarily in a marker. */
2156 signal_before_change (start_int
, end_int
, preserve_ptr
)
2157 int start_int
, end_int
;
2160 Lisp_Object start
, end
;
2161 Lisp_Object start_marker
, end_marker
;
2162 Lisp_Object preserve_marker
;
2163 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2164 int count
= SPECPDL_INDEX ();
2166 if (inhibit_modification_hooks
)
2169 start
= make_number (start_int
);
2170 end
= make_number (end_int
);
2171 preserve_marker
= Qnil
;
2172 start_marker
= Qnil
;
2174 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2176 specbind (Qinhibit_modification_hooks
, Qt
);
2178 /* If buffer is unmodified, run a special hook for that case. */
2179 if (SAVE_MODIFF
>= MODIFF
2180 && !NILP (Vfirst_change_hook
)
2181 && !NILP (Vrun_hooks
))
2185 call1 (Vrun_hooks
, Qfirst_change_hook
);
2188 /* Now run the before-change-functions if any. */
2189 if (!NILP (Vbefore_change_functions
))
2191 Lisp_Object args
[3];
2192 Lisp_Object rvoe_arg
= Fcons (Qbefore_change_functions
, Qnil
);
2197 /* Mark before-change-functions to be reset to nil in case of error. */
2198 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2200 /* Actually run the hook functions. */
2201 args
[0] = Qbefore_change_functions
;
2202 args
[1] = FETCH_START
;
2203 args
[2] = FETCH_END
;
2204 Frun_hook_with_args (3, args
);
2206 /* There was no error: unarm the reset_on_error. */
2207 XSETCDR (rvoe_arg
, Qt
);
2210 if (current_buffer
->overlays_before
|| current_buffer
->overlays_after
)
2213 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2214 FETCH_START
, FETCH_END
, Qnil
);
2217 if (! NILP (start_marker
))
2218 free_marker (start_marker
);
2219 if (! NILP (end_marker
))
2220 free_marker (end_marker
);
2224 unbind_to (count
, Qnil
);
2227 /* Signal a change immediately after it happens.
2228 CHARPOS is the character position of the start of the changed text.
2229 LENDEL is the number of characters of the text before the change.
2230 (Not the whole buffer; just the part that was changed.)
2231 LENINS is the number of characters in that part of the text
2232 after the change. */
2235 signal_after_change (charpos
, lendel
, lenins
)
2236 int charpos
, lendel
, lenins
;
2238 int count
= SPECPDL_INDEX ();
2239 if (inhibit_modification_hooks
)
2242 /* If we are deferring calls to the after-change functions
2243 and there are no before-change functions,
2244 just record the args that we were going to use. */
2245 if (! NILP (Vcombine_after_change_calls
)
2246 && NILP (Vbefore_change_functions
)
2247 && !current_buffer
->overlays_before
2248 && !current_buffer
->overlays_after
)
2252 if (!NILP (combine_after_change_list
)
2253 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2254 Fcombine_after_change_execute ();
2256 elt
= Fcons (make_number (charpos
- BEG
),
2257 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2258 Fcons (make_number (lenins
- lendel
), Qnil
)));
2259 combine_after_change_list
2260 = Fcons (elt
, combine_after_change_list
);
2261 combine_after_change_buffer
= Fcurrent_buffer ();
2266 if (!NILP (combine_after_change_list
))
2267 Fcombine_after_change_execute ();
2269 specbind (Qinhibit_modification_hooks
, Qt
);
2271 if (!NILP (Vafter_change_functions
))
2273 Lisp_Object args
[4];
2274 Lisp_Object rvoe_arg
= Fcons (Qafter_change_functions
, Qnil
);
2276 /* Mark after-change-functions to be reset to nil in case of error. */
2277 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2279 /* Actually run the hook functions. */
2280 args
[0] = Qafter_change_functions
;
2281 XSETFASTINT (args
[1], charpos
);
2282 XSETFASTINT (args
[2], charpos
+ lenins
);
2283 XSETFASTINT (args
[3], lendel
);
2284 Frun_hook_with_args (4, args
);
2286 /* There was no error: unarm the reset_on_error. */
2287 XSETCDR (rvoe_arg
, Qt
);
2290 if (current_buffer
->overlays_before
|| current_buffer
->overlays_after
)
2291 report_overlay_modification (make_number (charpos
),
2292 make_number (charpos
+ lenins
),
2294 make_number (charpos
),
2295 make_number (charpos
+ lenins
),
2296 make_number (lendel
));
2298 /* After an insertion, call the text properties
2299 insert-behind-hooks or insert-in-front-hooks. */
2301 report_interval_modification (make_number (charpos
),
2302 make_number (charpos
+ lenins
));
2304 unbind_to (count
, Qnil
);
2308 Fcombine_after_change_execute_1 (val
)
2311 Vcombine_after_change_calls
= val
;
2315 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2316 Scombine_after_change_execute
, 0, 0, 0,
2317 doc
: /* This function is for use internally in `combine-after-change-calls'. */)
2320 int count
= SPECPDL_INDEX ();
2321 int beg
, end
, change
;
2325 if (NILP (combine_after_change_list
))
2328 /* It is rare for combine_after_change_buffer to be invalid, but
2329 possible. It can happen when combine-after-change-calls is
2330 non-nil, and insertion calls a file handler (e.g. through
2331 lock_file) which scribbles into a temp file -- cyd */
2332 if (!BUFFERP (combine_after_change_buffer
)
2333 || NILP (XBUFFER (combine_after_change_buffer
)->name
))
2335 combine_after_change_list
= Qnil
;
2339 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2341 Fset_buffer (combine_after_change_buffer
);
2343 /* # chars unchanged at beginning of buffer. */
2345 /* # chars unchanged at end of buffer. */
2347 /* Total amount of insertion (negative for deletion). */
2350 /* Scan the various individual changes,
2351 accumulating the range info in BEG, END and CHANGE. */
2352 for (tail
= combine_after_change_list
; CONSP (tail
);
2356 int thisbeg
, thisend
, thischange
;
2358 /* Extract the info from the next element. */
2362 thisbeg
= XINT (XCAR (elt
));
2367 thisend
= XINT (XCAR (elt
));
2372 thischange
= XINT (XCAR (elt
));
2374 /* Merge this range into the accumulated range. */
2375 change
+= thischange
;
2382 /* Get the current start and end positions of the range
2383 that was changed. */
2387 /* We are about to handle these, so discard them. */
2388 combine_after_change_list
= Qnil
;
2390 /* Now run the after-change functions for real.
2391 Turn off the flag that defers them. */
2392 record_unwind_protect (Fcombine_after_change_execute_1
,
2393 Vcombine_after_change_calls
);
2394 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2395 update_compositions (begpos
, endpos
, CHECK_ALL
);
2397 return unbind_to (count
, Qnil
);
2403 staticpro (&combine_after_change_list
);
2404 staticpro (&combine_after_change_buffer
);
2405 combine_after_change_list
= Qnil
;
2406 combine_after_change_buffer
= Qnil
;
2408 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2409 doc
: /* Non-nil means enable debugging checks for invalid marker positions. */);
2410 check_markers_debug_flag
= 0;
2411 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2412 doc
: /* Used internally by the `combine-after-change-calls' macro. */);
2413 Vcombine_after_change_calls
= Qnil
;
2415 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks
,
2416 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2417 This affects `before-change-functions' and `after-change-functions',
2418 as well as hooks attached to text properties and overlays. */);
2419 inhibit_modification_hooks
= 0;
2420 Qinhibit_modification_hooks
= intern ("inhibit-modification-hooks");
2421 staticpro (&Qinhibit_modification_hooks
);
2423 defsubr (&Scombine_after_change_execute
);
2426 /* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
2427 (do not change this comment) */