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_markers_for_record_delete
P_ ((int, int, int, int));
44 static void adjust_point
P_ ((int, int));
46 Lisp_Object
Fcombine_after_change_execute ();
48 /* Non-nil means don't call the after-change-functions right away,
49 just record an element in Vcombine_after_change_calls_list. */
50 Lisp_Object Vcombine_after_change_calls
;
52 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
53 describing changes which happened while combine_after_change_calls
54 was nonzero. We use this to decide how to call them
55 once the deferral ends.
58 BEG-UNCHANGED is the number of chars before the changed range.
59 END-UNCHANGED is the number of chars after the changed range,
60 and CHANGE-AMOUNT is the number of characters inserted by the change
61 (negative for a deletion). */
62 Lisp_Object combine_after_change_list
;
64 /* Buffer which combine_after_change_list is about. */
65 Lisp_Object combine_after_change_buffer
;
67 /* Move gap to position CHARPOS.
68 Note that this can quit! */
74 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
77 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
78 Note that this can quit! */
81 move_gap_both (charpos
, bytepos
)
84 if (bytepos
< GPT_BYTE
)
85 gap_left (charpos
, bytepos
, 0);
86 else if (bytepos
> GPT_BYTE
)
87 gap_right (charpos
, bytepos
);
90 /* Move the gap to a position less than the current GPT.
91 BYTEPOS describes the new position as a byte position,
92 and CHARPOS is the corresponding char position.
93 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
96 gap_left (charpos
, bytepos
, newgap
)
97 register int charpos
, bytepos
;
100 register unsigned char *to
, *from
;
106 if (unchanged_modified
== MODIFF
107 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
109 beg_unchanged
= charpos
- BEG
;
110 end_unchanged
= Z
- charpos
;
114 if (Z
- GPT
< end_unchanged
)
115 end_unchanged
= Z
- GPT
;
116 if (charpos
< beg_unchanged
)
117 beg_unchanged
= charpos
- BEG
;
126 /* Now copy the characters. To move the gap down,
127 copy characters up. */
131 /* I gets number of characters left to copy. */
132 i
= new_s1
- bytepos
;
135 /* If a quit is requested, stop copying now.
136 Change BYTEPOS to be where we have actually moved the gap to. */
140 charpos
= BYTE_TO_CHAR (bytepos
);
143 /* Move at most 32000 chars before checking again for a quit. */
148 /* bcopy is safe if the two areas of memory do not overlap
149 or on systems where bcopy is always safe for moving upward. */
150 && (BCOPY_UPWARD_SAFE
151 || to
- from
>= 128))
153 /* If overlap is not safe, avoid it by not moving too many
154 characters at once. */
155 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
170 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
171 BYTEPOS is where the loop above stopped, which may be what was specified
172 or may be where a quit was detected. */
173 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
176 if (bytepos
< charpos
)
178 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
182 /* Move the gap to a position greater than than the current GPT.
183 BYTEPOS describes the new position as a byte position,
184 and CHARPOS is the corresponding char position. */
187 gap_right (charpos
, bytepos
)
188 register int charpos
, bytepos
;
190 register unsigned char *to
, *from
;
194 if (unchanged_modified
== MODIFF
195 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
197 beg_unchanged
= charpos
- BEG
;
198 end_unchanged
= Z
- charpos
;
202 if (Z
- charpos
- 1 < end_unchanged
)
203 end_unchanged
= Z
- charpos
;
204 if (GPT
- BEG
< beg_unchanged
)
205 beg_unchanged
= GPT
- BEG
;
213 /* Now copy the characters. To move the gap up,
214 copy characters down. */
218 /* I gets number of characters left to copy. */
219 i
= bytepos
- new_s1
;
222 /* If a quit is requested, stop copying now.
223 Change BYTEPOS to be where we have actually moved the gap to. */
227 charpos
= BYTE_TO_CHAR (bytepos
);
230 /* Move at most 32000 chars before checking again for a quit. */
235 /* bcopy is safe if the two areas of memory do not overlap
236 or on systems where bcopy is always safe for moving downward. */
237 && (BCOPY_DOWNWARD_SAFE
238 || from
- to
>= 128))
240 /* If overlap is not safe, avoid it by not moving too many
241 characters at once. */
242 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
257 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
261 if (bytepos
< charpos
)
263 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
267 /* Add AMOUNT to the byte position of every marker in the current buffer
268 whose current byte position is between FROM (exclusive) and TO (inclusive).
270 Also, any markers past the outside of that interval, in the direction
271 of adjustment, are first moved back to the near end of the interval
272 and then adjusted by AMOUNT.
274 When the latter adjustment is done, if AMOUNT is negative,
275 we record the adjustment for undo. (This case happens only for
278 The markers' character positions are not altered,
279 because gap motion does not affect character positions. */
281 int adjust_markers_test
;
284 adjust_markers_gap_motion (from
, to
, amount
)
285 register int from
, to
, amount
;
287 /* Now that a marker has a bytepos, not counting the gap,
288 nothing needs to be done here. */
291 register struct Lisp_Marker
*m
;
294 marker
= BUF_MARKERS (current_buffer
);
296 while (!NILP (marker
))
298 m
= XMARKER (marker
);
302 if (mpos
> to
&& mpos
< to
+ amount
)
304 if (adjust_markers_test
)
311 /* Here's the case where a marker is inside text being deleted.
312 AMOUNT can be negative for gap motion, too,
313 but then this range contains no markers. */
314 if (mpos
> from
+ amount
&& mpos
<= from
)
316 if (adjust_markers_test
)
318 mpos
= from
+ amount
;
321 if (mpos
> from
&& mpos
<= to
)
329 /* Adjust all markers for a deletion
330 whose range in bytes is FROM_BYTE to TO_BYTE.
331 The range in charpos is FROM to TO.
333 This function assumes that the gap is adjacent to
334 or inside of the range being deleted. */
337 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
338 register int from
, from_byte
, to
, to_byte
;
341 register struct Lisp_Marker
*m
;
342 register int charpos
;
344 marker
= BUF_MARKERS (current_buffer
);
346 while (!NILP (marker
))
348 m
= XMARKER (marker
);
349 charpos
= m
->charpos
;
354 /* If the marker is after the deletion,
355 relocate by number of chars / bytes deleted. */
358 m
->charpos
-= to
- from
;
359 m
->bytepos
-= to_byte
- from_byte
;
362 /* Here's the case where a marker is inside text being deleted. */
363 else if (charpos
> from
)
365 record_marker_adjustment (marker
, from
- charpos
);
367 m
->bytepos
= from_byte
;
374 /* Adjust all markers for calling record_delete for combining bytes.
375 whose range in bytes is FROM_BYTE to TO_BYTE.
376 The range in charpos is FROM to TO. */
379 adjust_markers_for_record_delete (from
, from_byte
, to
, to_byte
)
380 register int from
, from_byte
, to
, to_byte
;
383 register struct Lisp_Marker
*m
;
384 register int charpos
;
386 marker
= BUF_MARKERS (current_buffer
);
388 while (!NILP (marker
))
390 m
= XMARKER (marker
);
391 charpos
= m
->charpos
;
393 /* If the marker is after the deletion,
394 relocate by number of chars / bytes deleted. */
397 /* Here's the case where a marker is inside text being deleted. */
398 else if (charpos
> from
)
399 record_marker_adjustment (marker
, from
- charpos
);
405 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
406 to TO / TO_BYTE. We have to relocate the charpos of every marker
407 that points after the insertion (but not their bytepos).
409 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
410 that combine into one character with the text before the insertion.
411 COMBINED_AFTER_BYTES is the number of bytes after the insertion
412 that combine into one character with the last inserted bytes.
414 When a marker points at the insertion point,
415 we advance it if either its insertion-type is t
416 or BEFORE_MARKERS is true. */
419 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
,
420 combined_before_bytes
, combined_after_bytes
,
422 register int from
, from_byte
, to
, to_byte
;
423 int combined_before_bytes
, combined_after_bytes
, before_markers
;
427 int nchars
= to
- from
;
428 int nbytes
= to_byte
- from_byte
;
430 marker
= BUF_MARKERS (current_buffer
);
432 while (!NILP (marker
))
434 register struct Lisp_Marker
*m
= XMARKER (marker
);
436 /* In a single-byte buffer, a marker's two positions must be equal.
437 (If this insertion is going to combine characters, Z will
438 become different from Z_BYTE, but they might be the same now.
439 If so, the two OLD positions of the marker should be equal.) */
442 if (m
->charpos
!= m
->bytepos
)
446 if (m
->bytepos
== from_byte
)
448 if (m
->insertion_type
|| before_markers
)
450 m
->bytepos
+= nbytes
+ combined_after_bytes
;
451 m
->charpos
+= nchars
+ !!combined_after_bytes
;
452 /* Point the marker before the combined character,
453 so that undoing the insertion puts it back where it was. */
454 if (combined_after_bytes
)
455 DEC_BOTH (m
->charpos
, m
->bytepos
);
456 if (m
->insertion_type
)
459 else if (combined_before_bytes
)
461 /* This marker doesn't "need relocation",
462 but don't leave it pointing in the middle of a character.
463 Point the marker after the combined character,
464 so that undoing the insertion puts it back where it was. */
466 /* Here we depend on the fact that the gap is after
467 all of the combining bytes that we are going to skip over. */
468 DEC_BOTH (m
->charpos
, m
->bytepos
);
469 INC_BOTH (m
->charpos
, m
->bytepos
);
472 /* If a marker was pointing into the combining bytes
473 after the insertion, don't leave it there
474 in the middle of a character. */
475 else if (combined_after_bytes
&& m
->bytepos
>= from_byte
476 && m
->bytepos
< from_byte
+ combined_after_bytes
)
478 /* Put it after the combining bytes. */
479 m
->bytepos
= to_byte
+ combined_after_bytes
;
481 /* Now move it back before the combined character,
482 so that undoing the insertion will put it where it was. */
483 DEC_BOTH (m
->charpos
, m
->bytepos
);
485 else if (m
->bytepos
> from_byte
)
487 m
->bytepos
+= nbytes
;
488 m
->charpos
+= nchars
;
494 /* Adjusting only markers whose insertion-type is t may result in
495 disordered overlays in the slot `overlays_before'. */
497 fix_overlays_before (current_buffer
, from
, to
);
500 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
502 This is used only when the value of point changes due to an insert
503 or delete; it does not represent a conceptual change in point as a
504 marker. In particular, point is not crossing any interval
505 boundaries, so there's no need to use the usual SET_PT macro. In
506 fact it would be incorrect to do so, because either the old or the
507 new value of point is out of sync with the current set of
511 adjust_point (nchars
, nbytes
)
514 BUF_PT (current_buffer
) += nchars
;
515 BUF_PT_BYTE (current_buffer
) += nbytes
;
517 /* In a single-byte buffer, the two positions must be equal. */
523 /* Make the gap NBYTES_ADDED bytes longer. */
526 make_gap (nbytes_added
)
529 unsigned char *result
;
532 int real_gap_loc_byte
;
535 /* If we have to get more space, get enough to last a while. */
536 nbytes_added
+= 2000;
538 /* Don't allow a buffer size that won't fit in an int
539 even if it will fit in a Lisp integer.
540 That won't work because so many places use `int'. */
542 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
543 >= ((unsigned) 1 << (min (BITS_PER_INT
, VALBITS
) - 1)))
544 error ("Buffer exceeds maximum size");
547 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
548 result
= BUFFER_REALLOC (BEG_ADDR
, (Z_BYTE
- BEG_BYTE
549 + GAP_SIZE
+ nbytes_added
+ 1));
557 /* We can't unblock until the new address is properly stored. */
561 /* Prevent quitting in move_gap. */
566 real_gap_loc_byte
= GPT_BYTE
;
567 old_gap_size
= GAP_SIZE
;
569 /* Call the newly allocated space a gap at the end of the whole space. */
571 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
572 GAP_SIZE
= nbytes_added
;
574 /* Move the new gap down to be consecutive with the end of the old one.
575 This adjusts the markers properly too. */
576 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
578 /* Now combine the two into one large gap. */
579 GAP_SIZE
+= old_gap_size
;
581 GPT_BYTE
= real_gap_loc_byte
;
589 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
590 FROM_MULTIBYTE says whether the incoming text is multibyte.
591 TO_MULTIBYTE says whether to store the text as multibyte.
592 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
594 Return the number of bytes stored at TO_ADDR. */
597 copy_text (from_addr
, to_addr
, nbytes
,
598 from_multibyte
, to_multibyte
)
599 unsigned char *from_addr
;
600 unsigned char *to_addr
;
602 int from_multibyte
, to_multibyte
;
604 if (from_multibyte
== to_multibyte
)
606 bcopy (from_addr
, to_addr
, nbytes
);
609 else if (from_multibyte
)
612 int bytes_left
= nbytes
;
614 /* Convert multibyte to single byte. */
615 while (bytes_left
> 0)
618 c
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
619 *to_addr
++ = SINGLE_BYTE_CHAR_P (c
) ? c
: (c
& 0177) + 0200;
620 from_addr
+= thislen
;
628 unsigned char *initial_to_addr
= to_addr
;
630 /* Convert single-byte to multibyte. */
633 int c
= *from_addr
++;
634 unsigned char workbuf
[4], *str
;
637 if (c
>= 0240 && c
< 0400)
639 c
= unibyte_char_to_multibyte (c
);
640 len
= CHAR_STRING (c
, workbuf
, str
);
641 bcopy (str
, to_addr
, len
);
646 /* Special case for speed. */
647 *to_addr
++ = c
, nbytes
--;
649 return to_addr
- initial_to_addr
;
653 /* Return the number of bytes it would take
654 to convert some single-byte text to multibyte.
655 The single-byte text consists of NBYTES bytes at PTR. */
658 count_size_as_multibyte (ptr
, nbytes
)
663 int outgoing_nbytes
= 0;
665 for (i
= 0; i
< nbytes
; i
++)
667 unsigned int c
= *ptr
++;
673 c
= unibyte_char_to_multibyte (c
);
674 outgoing_nbytes
+= XINT (Fchar_bytes (make_number (c
)));
678 return outgoing_nbytes
;
681 /* Insert a string of specified length before point.
682 This function judges multibyteness based on
683 enable_multibyte_characters in the current buffer;
684 it never converts between single-byte and multibyte.
686 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
687 prepare_to_modify_buffer could relocate the text. */
690 insert (string
, nbytes
)
691 register unsigned char *string
;
697 insert_1 (string
, nbytes
, 0, 1, 0);
698 signal_after_change (opoint
, 0, PT
- opoint
);
702 /* Likewise, but inherit text properties from neighboring characters. */
705 insert_and_inherit (string
, nbytes
)
706 register unsigned char *string
;
712 insert_1 (string
, nbytes
, 1, 1, 0);
713 signal_after_change (opoint
, 0, PT
- opoint
);
717 /* Insert the character C before point. Do not inherit text properties. */
723 unsigned char workbuf
[4], *str
;
726 if (! NILP (current_buffer
->enable_multibyte_characters
))
727 len
= CHAR_STRING (c
, workbuf
, str
);
738 /* Insert the null-terminated string S before point. */
744 insert (s
, strlen (s
));
747 /* Like `insert' except that all markers pointing at the place where
748 the insertion happens are adjusted to point after it.
749 Don't use this function to insert part of a Lisp string,
750 since gc could happen and relocate it. */
753 insert_before_markers (string
, nbytes
)
754 unsigned char *string
;
761 insert_1 (string
, nbytes
, 0, 1, 1);
762 signal_after_change (opoint
, 0, PT
- opoint
);
766 /* Likewise, but inherit text properties from neighboring characters. */
769 insert_before_markers_and_inherit (string
, nbytes
)
770 unsigned char *string
;
777 insert_1 (string
, nbytes
, 1, 1, 1);
778 signal_after_change (opoint
, 0, PT
- opoint
);
782 /* Subroutine used by the insert functions above. */
785 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
786 register unsigned char *string
;
788 int inherit
, prepare
, before_markers
;
790 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
791 inherit
, prepare
, before_markers
);
794 /* See if the bytes before POS/POS_BYTE combine with bytes
795 at the start of STRING to form a single character.
796 If so, return the number of bytes at the start of STRING
797 which combine in this way. Otherwise, return 0. */
800 count_combining_before (string
, length
, pos
, pos_byte
)
801 unsigned char *string
;
805 int opos
= pos
, opos_byte
= pos_byte
;
807 unsigned char *p
= string
;
809 if (NILP (current_buffer
->enable_multibyte_characters
))
811 if (length
== 0 || CHAR_HEAD_P (*string
))
815 c
= FETCH_BYTE (pos_byte
- 1);
816 if (ASCII_BYTE_P (c
))
818 DEC_BOTH (pos
, pos_byte
);
819 c
= FETCH_BYTE (pos_byte
);
820 if (! BASE_LEADING_CODE_P (c
))
823 /* We have a combination situation.
824 Count the bytes at STRING that will combine. */
825 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
831 /* See if the bytes after POS/POS_BYTE combine with bytes
832 at the end of STRING to form a single character.
833 If so, return the number of bytes after POS/POS_BYTE
834 which combine in this way. Otherwise, return 0. */
837 count_combining_after (string
, length
, pos
, pos_byte
)
838 unsigned char *string
;
842 int opos
= pos
, opos_byte
= pos_byte
;
846 if (NILP (current_buffer
->enable_multibyte_characters
))
848 if (length
== 0 || ASCII_BYTE_P (string
[length
- 1]))
851 while (i
> 0 && ! CHAR_HEAD_P (string
[i
]))
855 if (! BASE_LEADING_CODE_P (string
[i
]))
860 c
= FETCH_BYTE (pos_byte
);
863 while (pos_byte
< ZV_BYTE
)
865 c
= FETCH_BYTE (pos_byte
);
871 return pos_byte
- opos_byte
;
874 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
875 following the position POS/POS_BYTE to the character preceding POS.
876 If TARGET is after POS+NBYTES, we only have to adjust the character
877 position TARGET, else, if TARGET is after POS, we have to adjust
878 both the character position TARGET and the byte position
879 TARGET_BYTE, else we don't have to do any adjustment. */
881 #define ADJUST_CHAR_POS(target, target_byte) \
883 if (target > pos + nbytes) \
885 else if (target >= pos) \
888 target_byte = pos_byte + nbytes; \
892 /* Combine NBYTES stray trailing-codes, which were formerly separate
893 characters, with the preceding character. These bytes
894 are located after position POS / POS_BYTE, and the preceding character
895 is located just before that position. */
898 combine_bytes (pos
, pos_byte
, nbytes
)
899 int pos
, pos_byte
, nbytes
;
901 /* Adjust all markers. */
902 adjust_markers_for_delete (pos
, pos_byte
, pos
+ nbytes
, pos_byte
);
904 adjust_overlays_for_delete (pos
, nbytes
);
906 ADJUST_CHAR_POS (BUF_PT (current_buffer
), BUF_PT_BYTE (current_buffer
));
907 ADJUST_CHAR_POS (GPT
, GPT_BYTE
);
908 ADJUST_CHAR_POS (Z
, Z_BYTE
);
909 ADJUST_CHAR_POS (ZV
, ZV_BYTE
);
911 if (BUF_INTERVALS (current_buffer
) != 0)
912 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
913 offset_intervals (current_buffer
, pos
, - nbytes
);
916 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
917 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
918 are the same as in insert_1. */
921 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
922 register unsigned char *string
;
923 register int nchars
, nbytes
;
924 int inherit
, prepare
, before_markers
;
926 register Lisp_Object temp
, deletion
;
927 int combined_before_bytes
, combined_after_bytes
;
929 if (NILP (current_buffer
->enable_multibyte_characters
))
933 move_gap_both (PT
, PT_BYTE
);
934 if (GAP_SIZE
< nbytes
)
935 make_gap (nbytes
- GAP_SIZE
);
938 prepare_to_modify_buffer (PT
, PT
, NULL
);
940 combined_before_bytes
941 = count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
943 = count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
945 /* Record deletion of the surrounding text that combines with
946 the insertion. This, together with recording the insertion,
947 will add up to the right stuff in the undo list.
949 But there is no need to actually delete the combining bytes
950 from the buffer and reinsert them. */
952 if (combined_after_bytes
)
954 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
955 PT
+ combined_after_bytes
,
956 PT_BYTE
+ combined_after_bytes
, 1);
958 adjust_markers_for_record_delete (PT
, PT_BYTE
,
959 PT
+ combined_after_bytes
,
960 PT_BYTE
+ combined_after_bytes
);
961 record_delete (PT
, deletion
);
964 if (combined_before_bytes
)
966 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
968 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
970 record_delete (PT
- 1, deletion
);
973 record_insert (PT
- !!combined_before_bytes
,
974 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
977 bcopy (string
, GPT_ADDR
, nbytes
);
980 /* When we have combining at the end of the insertion,
981 this is the character position before the combined character. */
988 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
990 if (combined_after_bytes
)
991 move_gap_both (GPT
+ combined_after_bytes
,
992 GPT_BYTE
+ combined_after_bytes
);
997 adjust_overlays_for_insert (PT
, nchars
);
998 adjust_markers_for_insert (PT
, PT_BYTE
,
999 PT
+ nchars
, PT_BYTE
+ nbytes
,
1000 combined_before_bytes
, combined_after_bytes
,
1003 #ifdef USE_TEXT_PROPERTIES
1004 if (BUF_INTERVALS (current_buffer
) != 0)
1005 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1006 offset_intervals (current_buffer
, PT
, nchars
);
1008 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1009 Fset_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1014 int pos
= PT
, pos_byte
= PT_BYTE
;
1016 adjust_point (nchars
+ combined_after_bytes
,
1017 nbytes
+ combined_after_bytes
);
1019 if (combined_after_bytes
)
1020 combine_bytes (pos
+ nchars
, pos_byte
+ nbytes
, combined_after_bytes
);
1022 if (combined_before_bytes
)
1023 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1027 /* Insert the part of the text of STRING, a Lisp object assumed to be
1028 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1029 starting at position POS / POS_BYTE. If the text of STRING has properties,
1030 copy them into the buffer.
1032 It does not work to use `insert' for this, because a GC could happen
1033 before we bcopy the stuff into the buffer, and relocate the string
1034 without insert noticing. */
1037 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1039 register int pos
, pos_byte
, length
, length_byte
;
1045 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1047 signal_after_change (opoint
, 0, PT
- opoint
);
1051 /* Like `insert_from_string' except that all markers pointing
1052 at the place where the insertion happens are adjusted to point after it. */
1055 insert_from_string_before_markers (string
, pos
, pos_byte
,
1056 length
, length_byte
, inherit
)
1058 register int pos
, pos_byte
, length
, length_byte
;
1064 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1066 signal_after_change (opoint
, 0, PT
- opoint
);
1070 /* Subroutine of the insertion functions above. */
1073 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1074 inherit
, before_markers
)
1076 register int pos
, pos_byte
, nchars
, nbytes
;
1077 int inherit
, before_markers
;
1079 register Lisp_Object temp
;
1080 struct gcpro gcpro1
;
1081 int outgoing_nbytes
= nbytes
;
1082 int combined_before_bytes
, combined_after_bytes
;
1083 int adjusted_nchars
;
1085 Lisp_Object deletion
;
1087 /* Make OUTGOING_NBYTES describe the text
1088 as it will be inserted in this buffer. */
1090 if (NILP (current_buffer
->enable_multibyte_characters
))
1091 outgoing_nbytes
= nchars
;
1092 else if (nchars
== nbytes
)
1094 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1097 /* Make sure point-max won't overflow after this insertion. */
1098 XSETINT (temp
, outgoing_nbytes
+ Z
);
1099 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1100 error ("Maximum buffer size exceeded");
1103 prepare_to_modify_buffer (PT
, PT
, NULL
);
1106 move_gap_both (PT
, PT_BYTE
);
1107 if (GAP_SIZE
< nbytes
)
1108 make_gap (outgoing_nbytes
- GAP_SIZE
);
1111 /* Copy the string text into the buffer, perhaps converting
1112 between single-byte and multibyte. */
1113 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1114 /* If these are equal, it is a single-byte string.
1115 Its chars are either ASCII, in which case copy_text
1116 won't change it, or single-byte non-ASCII chars,
1117 that need to be changed. */
1119 ! NILP (current_buffer
->enable_multibyte_characters
));
1121 /* We have copied text into the gap, but we have not altered
1122 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1123 to these functions and get the same results as we would
1124 have got earlier on. Meanwhile, PT_ADDR does point to
1125 the text that has been stored by copy_text. */
1127 combined_before_bytes
1128 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1129 combined_after_bytes
1130 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1132 /* Record deletion of the surrounding text that combines with
1133 the insertion. This, together with recording the insertion,
1134 will add up to the right stuff in the undo list.
1136 But there is no need to actually delete the combining bytes
1137 from the buffer and reinsert them. */
1139 if (combined_after_bytes
)
1141 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1142 PT
+ combined_after_bytes
,
1143 PT_BYTE
+ combined_after_bytes
, 1);
1145 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1146 PT
+ combined_after_bytes
,
1147 PT_BYTE
+ combined_after_bytes
);
1148 record_delete (PT
, deletion
);
1151 if (combined_before_bytes
)
1153 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1155 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1157 record_delete (PT
- 1, deletion
);
1160 record_insert (PT
- !!combined_before_bytes
,
1161 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1164 GAP_SIZE
-= outgoing_nbytes
;
1168 GPT_BYTE
+= outgoing_nbytes
;
1169 ZV_BYTE
+= outgoing_nbytes
;
1170 Z_BYTE
+= outgoing_nbytes
;
1171 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1173 if (combined_after_bytes
)
1174 move_gap_both (GPT
+ combined_after_bytes
,
1175 GPT_BYTE
+ combined_after_bytes
);
1180 adjust_overlays_for_insert (PT
, nchars
);
1181 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1182 PT_BYTE
+ outgoing_nbytes
,
1183 combined_before_bytes
, combined_after_bytes
,
1186 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1187 offset_intervals (current_buffer
, PT
, nchars
);
1189 intervals
= XSTRING (string
)->intervals
;
1190 /* Get the intervals for the part of the string we are inserting--
1191 not including the combined-before bytes. */
1192 if (nbytes
< STRING_BYTES (XSTRING (string
)))
1193 intervals
= copy_intervals (intervals
, pos
, nchars
);
1195 /* Insert those intervals. */
1196 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1197 current_buffer
, inherit
);
1200 int pos
= PT
, pos_byte
= PT_BYTE
;
1202 adjust_point (nchars
+ combined_after_bytes
,
1203 outgoing_nbytes
+ combined_after_bytes
);
1205 if (combined_after_bytes
)
1206 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1207 combined_after_bytes
);
1209 if (combined_before_bytes
)
1210 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1214 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1215 current buffer. If the text in BUF has properties, they are absorbed
1216 into the current buffer.
1218 It does not work to use `insert' for this, because a malloc could happen
1219 and relocate BUF's text before the bcopy happens. */
1222 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1224 int charpos
, nchars
;
1231 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1232 signal_after_change (opoint
, 0, PT
- opoint
);
1237 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1242 register Lisp_Object temp
, deletion
;
1244 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1245 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1246 int incoming_nbytes
= to_byte
- from_byte
;
1247 int outgoing_nbytes
= incoming_nbytes
;
1248 int combined_before_bytes
, combined_after_bytes
;
1249 int adjusted_nchars
;
1252 /* Make OUTGOING_NBYTES describe the text
1253 as it will be inserted in this buffer. */
1255 if (NILP (current_buffer
->enable_multibyte_characters
))
1256 outgoing_nbytes
= nchars
;
1257 else if (NILP (buf
->enable_multibyte_characters
))
1259 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1262 /* Make sure point-max won't overflow after this insertion. */
1263 XSETINT (temp
, outgoing_nbytes
+ Z
);
1264 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1265 error ("Maximum buffer size exceeded");
1267 prepare_to_modify_buffer (PT
, PT
, NULL
);
1270 move_gap_both (PT
, PT_BYTE
);
1271 if (GAP_SIZE
< outgoing_nbytes
)
1272 make_gap (outgoing_nbytes
- GAP_SIZE
);
1274 if (from
< BUF_GPT (buf
))
1276 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1277 if (chunk
> incoming_nbytes
)
1278 chunk
= incoming_nbytes
;
1279 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1281 ! NILP (buf
->enable_multibyte_characters
),
1282 ! NILP (current_buffer
->enable_multibyte_characters
));
1286 if (chunk
< incoming_nbytes
)
1287 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1288 GPT_ADDR
+ chunk
, incoming_nbytes
- chunk
,
1289 ! NILP (buf
->enable_multibyte_characters
),
1290 ! NILP (current_buffer
->enable_multibyte_characters
));
1292 /* We have copied text into the gap, but we have not altered
1293 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1294 to these functions and get the same results as we would
1295 have got earlier on. Meanwhile, GPT_ADDR does point to
1296 the text that has been stored by copy_text. */
1297 combined_before_bytes
1298 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1299 combined_after_bytes
1300 = count_combining_after (GPT_ADDR
, outgoing_nbytes
,
1303 /* Record deletion of the surrounding text that combines with
1304 the insertion. This, together with recording the insertion,
1305 will add up to the right stuff in the undo list.
1307 But there is no need to actually delete the combining bytes
1308 from the buffer and reinsert them. */
1310 if (combined_after_bytes
)
1312 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1313 PT
+ combined_after_bytes
,
1314 PT_BYTE
+ combined_after_bytes
, 1);
1316 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1317 PT
+ combined_after_bytes
,
1318 PT_BYTE
+ combined_after_bytes
);
1319 record_delete (PT
, deletion
);
1322 if (combined_before_bytes
)
1324 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1326 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1328 record_delete (PT
- 1, deletion
);
1331 record_insert (PT
- !!combined_before_bytes
,
1332 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1335 GAP_SIZE
-= outgoing_nbytes
;
1339 GPT_BYTE
+= outgoing_nbytes
;
1340 ZV_BYTE
+= outgoing_nbytes
;
1341 Z_BYTE
+= outgoing_nbytes
;
1342 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1344 if (combined_after_bytes
)
1345 move_gap_both (GPT
+ combined_after_bytes
,
1346 GPT_BYTE
+ combined_after_bytes
);
1351 adjust_overlays_for_insert (PT
, nchars
);
1352 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1353 PT_BYTE
+ outgoing_nbytes
,
1354 combined_before_bytes
, combined_after_bytes
, 0);
1356 #ifdef USE_TEXT_PROPERTIES
1357 if (BUF_INTERVALS (current_buffer
) != 0)
1358 offset_intervals (current_buffer
, PT
, nchars
);
1361 /* Get the intervals for the part of the string we are inserting--
1362 not including the combined-before bytes. */
1363 intervals
= BUF_INTERVALS (buf
);
1364 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1365 intervals
= copy_intervals (intervals
, from
, nchars
);
1367 /* Insert those intervals. */
1368 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1371 int pos
= PT
, pos_byte
= PT_BYTE
;
1373 adjust_point (nchars
+ combined_after_bytes
,
1374 outgoing_nbytes
+ combined_after_bytes
);
1376 if (combined_after_bytes
)
1377 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1378 combined_after_bytes
);
1380 if (combined_before_bytes
)
1381 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1385 /* This function should be called after moving gap to FROM and before
1386 altering text between FROM and TO. This adjusts various position
1387 keepers and markers as if the text is deleted. Don't forget to
1388 call adjust_after_replace after you actually alter the text. */
1391 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1392 int from
, from_byte
, to
, to_byte
;
1394 Lisp_Object deletion
;
1395 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1397 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1398 record_delete (from
, deletion
);
1399 adjust_overlays_for_delete (from
, to
- from
);
1402 /* This function should be called after altering the text between FROM
1403 and TO to a new text of LEN chars (LEN_BYTE bytes), but before
1404 making the text a buffer contents. It exists just after GPT_ADDR. */
1407 adjust_after_replace (from
, from_byte
, to
, to_byte
, len
, len_byte
, replace
)
1408 int from
, from_byte
, to
, to_byte
, len
, len_byte
, replace
;
1410 int combined_before_bytes
1411 = count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
);
1412 int combined_after_bytes
1413 = count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
);
1414 Lisp_Object deletion
;
1416 if (combined_after_bytes
)
1418 deletion
= make_buffer_string_both (from
, from_byte
,
1419 from
+ combined_after_bytes
,
1420 from_byte
+ combined_after_bytes
, 1);
1422 adjust_markers_for_record_delete (from
, from_byte
,
1423 from
+ combined_after_bytes
,
1424 from_byte
+ combined_after_bytes
);
1425 record_delete (from
, deletion
);
1428 if (combined_before_bytes
)
1430 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1431 from
, from_byte
, 1);
1432 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1434 record_delete (from
- 1, deletion
);
1437 /* Update various buffer positions for the new text. */
1438 GAP_SIZE
-= len_byte
;
1440 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1441 GPT
+= len
; GPT_BYTE
+= len_byte
;
1442 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1444 if (combined_after_bytes
)
1445 move_gap_both (GPT
+ combined_after_bytes
,
1446 GPT_BYTE
+ combined_after_bytes
);
1448 record_insert (from
- !!combined_before_bytes
,
1449 len
- combined_before_bytes
+ !!combined_before_bytes
);
1450 adjust_overlays_for_insert (from
, len
);
1451 adjust_markers_for_insert (from
, from_byte
,
1452 from
+ len
, from_byte
+ len_byte
,
1453 combined_before_bytes
, combined_after_bytes
, 0);
1454 #ifdef USE_TEXT_PROPERTIES
1455 if (BUF_INTERVALS (current_buffer
) != 0)
1456 /* REPLACE zero means that we have not yet adjusted the interval
1457 tree for the text between FROM and TO, thus, we must treat the
1458 new text as a newly inserted text, not as a replacement of
1460 offset_intervals (current_buffer
, from
, len
- (replace
? to
- from
: 0));
1464 int pos
= PT
, pos_byte
= PT_BYTE
;
1467 adjust_point (len
- (to
- from
) + combined_after_bytes
,
1468 len_byte
- (to_byte
- from_byte
) + combined_after_bytes
);
1469 else if (from
== PT
&& combined_before_bytes
)
1470 adjust_point (0, combined_before_bytes
);
1472 if (combined_after_bytes
)
1473 combine_bytes (from
+ len
, from_byte
+ len_byte
, combined_after_bytes
);
1475 if (combined_before_bytes
)
1476 combine_bytes (from
, from_byte
, combined_before_bytes
);
1480 evaporate_overlays (from
);
1484 /* Replace the text from character positions FROM to TO with NEW,
1485 If PREPARE is nonzero, call prepare_to_modify_buffer.
1486 If INHERIT, the newly inserted text should inherit text properties
1487 from the surrounding non-deleted text. */
1489 /* Note that this does not yet handle markers quite right.
1490 Also it needs to record a single undo-entry that does a replacement
1491 rather than a separate delete and insert.
1492 That way, undo will also handle markers properly. */
1495 replace_range (from
, to
, new, prepare
, inherit
, nomarkers
)
1497 int from
, to
, prepare
, inherit
, nomarkers
;
1499 int inschars
= XSTRING (new)->size
;
1500 int insbytes
= STRING_BYTES (XSTRING (new));
1501 int from_byte
, to_byte
;
1502 int nbytes_del
, nchars_del
;
1503 register Lisp_Object temp
;
1504 struct gcpro gcpro1
;
1505 int combined_before_bytes
, combined_after_bytes
;
1506 int adjusted_inschars
;
1508 int outgoing_insbytes
= insbytes
;
1509 Lisp_Object deletion
;
1515 int range_length
= to
- from
;
1516 prepare_to_modify_buffer (from
, to
, &from
);
1517 to
= from
+ range_length
;
1522 /* Make args be valid */
1528 from_byte
= CHAR_TO_BYTE (from
);
1529 to_byte
= CHAR_TO_BYTE (to
);
1531 nchars_del
= to
- from
;
1532 nbytes_del
= to_byte
- from_byte
;
1534 if (nbytes_del
<= 0 && insbytes
== 0)
1537 /* Make OUTGOING_INSBYTES describe the text
1538 as it will be inserted in this buffer. */
1540 if (NILP (current_buffer
->enable_multibyte_characters
))
1541 outgoing_insbytes
= inschars
;
1542 else if (inschars
== insbytes
)
1544 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1546 /* Make sure point-max won't overflow after this insertion. */
1547 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1548 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1549 error ("Maximum buffer size exceeded");
1553 /* Make sure the gap is somewhere in or next to what we are deleting. */
1555 gap_right (from
, from_byte
);
1557 gap_left (to
, to_byte
, 0);
1559 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1562 /* Relocate all markers pointing into the new, larger gap
1563 to point at the end of the text before the gap.
1564 Do this before recording the deletion,
1565 so that undo handles this after reinserting the text. */
1566 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1568 record_delete (from
, deletion
);
1570 GAP_SIZE
+= nbytes_del
;
1573 ZV_BYTE
-= nbytes_del
;
1574 Z_BYTE
-= nbytes_del
;
1576 GPT_BYTE
= from_byte
;
1577 *(GPT_ADDR
) = 0; /* Put an anchor. */
1582 if (GPT
- BEG
< beg_unchanged
)
1583 beg_unchanged
= GPT
- BEG
;
1584 if (Z
- GPT
< end_unchanged
)
1585 end_unchanged
= Z
- GPT
;
1587 if (GAP_SIZE
< insbytes
)
1588 make_gap (insbytes
- GAP_SIZE
);
1590 /* Copy the string text into the buffer, perhaps converting
1591 between single-byte and multibyte. */
1592 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1593 /* If these are equal, it is a single-byte string.
1594 Its chars are either ASCII, in which case copy_text
1595 won't change it, or single-byte non-ASCII chars,
1596 that need to be changed. */
1597 inschars
!= insbytes
,
1598 ! NILP (current_buffer
->enable_multibyte_characters
));
1600 /* We have copied text into the gap, but we have not altered
1601 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1602 to these functions and get the same results as we would
1603 have got earlier on. Meanwhile, GPT_ADDR does point to
1604 the text that has been stored by copy_text. */
1606 combined_before_bytes
1607 = count_combining_before (GPT_ADDR
, outgoing_insbytes
, PT
, PT_BYTE
);
1608 combined_after_bytes
1609 = count_combining_after (GPT_ADDR
, outgoing_insbytes
, PT
, PT_BYTE
);
1611 /* Record deletion of the surrounding text that combines with
1612 the insertion. This, together with recording the insertion,
1613 will add up to the right stuff in the undo list.
1615 But there is no need to actually delete the combining bytes
1616 from the buffer and reinsert them. */
1618 if (combined_after_bytes
)
1620 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1621 PT
+ combined_after_bytes
,
1622 PT_BYTE
+ combined_after_bytes
, 1);
1624 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1625 PT
+ combined_after_bytes
,
1626 PT_BYTE
+ combined_after_bytes
);
1627 record_delete (PT
, deletion
);
1630 if (combined_before_bytes
)
1632 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1634 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1636 record_delete (PT
- 1, deletion
);
1639 record_insert (PT
- !!combined_before_bytes
,
1640 inschars
- combined_before_bytes
+ !!combined_before_bytes
);
1642 GAP_SIZE
-= outgoing_insbytes
;
1646 GPT_BYTE
+= outgoing_insbytes
;
1647 ZV_BYTE
+= outgoing_insbytes
;
1648 Z_BYTE
+= outgoing_insbytes
;
1649 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1651 if (combined_after_bytes
)
1652 move_gap_both (GPT
+ combined_after_bytes
,
1653 GPT_BYTE
+ combined_after_bytes
);
1658 /* Adjust the overlay center as needed. This must be done after
1659 adjusting the markers that bound the overlays. */
1660 adjust_overlays_for_delete (from
, nchars_del
);
1661 adjust_overlays_for_insert (from
, inschars
);
1663 adjust_markers_for_insert (from
, from_byte
,
1664 from
+ inschars
, from_byte
+ outgoing_insbytes
,
1665 combined_before_bytes
, combined_after_bytes
, 0);
1667 #ifdef USE_TEXT_PROPERTIES
1668 offset_intervals (current_buffer
, PT
, inschars
- nchars_del
);
1670 /* Get the intervals for the part of the string we are inserting--
1671 not including the combined-before bytes. */
1672 intervals
= XSTRING (new)->intervals
;
1673 /* Insert those intervals. */
1674 graft_intervals_into_buffer (intervals
, from
, inschars
,
1675 current_buffer
, inherit
);
1678 /* Relocate point as if it were a marker. */
1680 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)
1681 + combined_after_bytes
),
1682 (from_byte
+ outgoing_insbytes
1683 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)
1684 + combined_after_bytes
));
1686 if (combined_after_bytes
)
1687 combine_bytes (from
+ inschars
, from_byte
+ outgoing_insbytes
,
1688 combined_after_bytes
);
1690 if (combined_before_bytes
)
1691 combine_bytes (from
, from_byte
, combined_before_bytes
);
1693 if (outgoing_insbytes
== 0)
1694 evaporate_overlays (from
);
1699 signal_after_change (from
, nchars_del
, PT
- from
);
1702 /* Delete characters in current buffer
1703 from FROM up to (but not including) TO.
1704 If TO comes before FROM, we delete nothing. */
1707 del_range (from
, to
)
1708 register int from
, to
;
1710 del_range_1 (from
, to
, 1);
1713 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
1716 del_range_1 (from
, to
, prepare
)
1717 int from
, to
, prepare
;
1719 int from_byte
, to_byte
;
1721 /* Make args be valid */
1732 int range_length
= to
- from
;
1733 prepare_to_modify_buffer (from
, to
, &from
);
1734 to
= from
+ range_length
;
1737 from_byte
= CHAR_TO_BYTE (from
);
1738 to_byte
= CHAR_TO_BYTE (to
);
1740 del_range_2 (from
, from_byte
, to
, to_byte
);
1743 /* Like del_range_1 but args are byte positions, not char positions. */
1746 del_range_byte (from_byte
, to_byte
, prepare
)
1747 int from_byte
, to_byte
, prepare
;
1751 /* Make args be valid */
1752 if (from_byte
< BEGV_BYTE
)
1753 from_byte
= BEGV_BYTE
;
1754 if (to_byte
> ZV_BYTE
)
1757 if (to_byte
<= from_byte
)
1760 from
= BYTE_TO_CHAR (from_byte
);
1761 to
= BYTE_TO_CHAR (to_byte
);
1765 int old_from
= from
, old_to
= Z
- to
;
1766 int range_length
= to
- from
;
1767 prepare_to_modify_buffer (from
, to
, &from
);
1768 to
= from
+ range_length
;
1770 if (old_from
!= from
)
1771 from_byte
= CHAR_TO_BYTE (from
);
1772 if (old_to
== Z
- to
)
1773 to_byte
= CHAR_TO_BYTE (to
);
1776 del_range_2 (from
, from_byte
, to
, to_byte
);
1779 /* Like del_range_1, but positions are specified both as charpos
1783 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
1784 int from
, from_byte
, to
, to_byte
, prepare
;
1786 /* Make args be valid */
1787 if (from_byte
< BEGV_BYTE
)
1788 from_byte
= BEGV_BYTE
;
1789 if (to_byte
> ZV_BYTE
)
1792 if (to_byte
<= from_byte
)
1802 int old_from
= from
, old_to
= Z
- to
;
1803 int range_length
= to
- from
;
1804 prepare_to_modify_buffer (from
, to
, &from
);
1805 to
= from
+ range_length
;
1807 if (old_from
!= from
)
1808 from_byte
= CHAR_TO_BYTE (from
);
1809 if (old_to
== Z
- to
)
1810 to_byte
= CHAR_TO_BYTE (to
);
1813 del_range_2 (from
, from_byte
, to
, to_byte
);
1816 /* Delete a range of text, specified both as character positions
1817 and byte positions. FROM and TO are character positions,
1818 while FROM_BYTE and TO_BYTE are byte positions. */
1821 del_range_2 (from
, from_byte
, to
, to_byte
)
1822 int from
, from_byte
, to
, to_byte
;
1824 register int nbytes_del
, nchars_del
;
1825 int combined_after_bytes
;
1826 Lisp_Object deletion
;
1829 nchars_del
= to
- from
;
1830 nbytes_del
= to_byte
- from_byte
;
1832 /* Make sure the gap is somewhere in or next to what we are deleting. */
1834 gap_right (from
, from_byte
);
1836 gap_left (to
, to_byte
, 0);
1838 combined_after_bytes
1839 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1840 ZV_BYTE
- to_byte
, from
, from_byte
);
1841 if (combined_after_bytes
)
1843 from_byte_1
= from_byte
;
1844 DEC_POS (from_byte_1
);
1847 from_byte_1
= from_byte
;
1850 = make_buffer_string_both (from
- !!combined_after_bytes
,
1852 to
+ combined_after_bytes
,
1853 to_byte
+ combined_after_bytes
, 1);
1855 /* Relocate all markers pointing into the new, larger gap
1856 to point at the end of the text before the gap.
1857 Do this before recording the deletion,
1858 so that undo handles this after reinserting the text. */
1859 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1860 if (combined_after_bytes
)
1862 /* Adjust markers for the phony deletion
1863 that we are about to call record_undo for. */
1865 /* Here we delete the markers that formerly
1866 pointed at TO ... TO + COMBINED_AFTER_BYTES.
1867 But because of the call to adjust_markers_for_delete, above,
1868 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
1869 adjust_markers_for_record_delete (from
, from_byte
,
1870 from
+ combined_after_bytes
,
1871 from_byte
+ combined_after_bytes
);
1873 adjust_markers_for_record_delete (from
- 1, from_byte_1
,
1876 record_delete (from
- !!combined_after_bytes
, deletion
);
1878 if (combined_after_bytes
)
1879 /* COMBINED_AFTER_BYTES nonzero means that the above record_delete
1880 moved the gap by calling Fbuffer_substring. We must move the
1881 gap again to a proper place. */
1882 move_gap_both (from
, from_byte
);
1885 /* Relocate point as if it were a marker. */
1887 adjust_point (from
- (PT
< to
? PT
: to
),
1888 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1890 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1891 offset_intervals (current_buffer
, from
, - nchars_del
);
1893 /* Adjust the overlay center as needed. This must be done after
1894 adjusting the markers that bound the overlays. */
1895 adjust_overlays_for_delete (from
, nchars_del
);
1897 GAP_SIZE
+= nbytes_del
;
1898 ZV_BYTE
-= nbytes_del
;
1899 Z_BYTE
-= nbytes_del
;
1903 GPT_BYTE
= from_byte
;
1905 if (combined_after_bytes
)
1906 move_gap_both (GPT
+ combined_after_bytes
,
1907 GPT_BYTE
+ combined_after_bytes
);
1909 *(GPT_ADDR
) = 0; /* Put an anchor. */
1914 if (GPT
- BEG
< beg_unchanged
)
1915 beg_unchanged
= GPT
- BEG
;
1916 if (Z
- GPT
< end_unchanged
)
1917 end_unchanged
= Z
- GPT
;
1919 if (combined_after_bytes
)
1921 combine_bytes (from
, from_byte
, combined_after_bytes
);
1923 record_insert (GPT
- 1, 1);
1926 evaporate_overlays (from
);
1927 signal_after_change (from
, nchars_del
, 0);
1930 /* Call this if you're about to change the region of BUFFER from
1931 character positions START to END. This checks the read-only
1932 properties of the region, calls the necessary modification hooks,
1933 and warns the next redisplay that it should pay attention to that
1937 modify_region (buffer
, start
, end
)
1938 struct buffer
*buffer
;
1941 struct buffer
*old_buffer
= current_buffer
;
1943 if (buffer
!= old_buffer
)
1944 set_buffer_internal (buffer
);
1946 prepare_to_modify_buffer (start
, end
, NULL
);
1948 if (start
- 1 < beg_unchanged
1949 || (unchanged_modified
== MODIFF
1950 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
1951 beg_unchanged
= start
- 1;
1952 if (Z
- end
< end_unchanged
1953 || (unchanged_modified
== MODIFF
1954 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
1955 end_unchanged
= Z
- end
;
1957 if (MODIFF
<= SAVE_MODIFF
)
1958 record_first_change ();
1961 buffer
->point_before_scroll
= Qnil
;
1963 if (buffer
!= old_buffer
)
1964 set_buffer_internal (old_buffer
);
1967 /* Check that it is okay to modify the buffer between START and END,
1968 which are char positions.
1970 Run the before-change-function, if any. If intervals are in use,
1971 verify that the text to be modified is not read-only, and call
1972 any modification properties the text may have.
1974 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1975 by holding its value temporarily in a marker. */
1978 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
1982 if (!NILP (current_buffer
->read_only
))
1983 Fbarf_if_buffer_read_only ();
1985 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1986 if (BUF_INTERVALS (current_buffer
) != 0)
1990 Lisp_Object preserve_marker
;
1991 struct gcpro gcpro1
;
1992 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1993 GCPRO1 (preserve_marker
);
1994 verify_interval_modification (current_buffer
, start
, end
);
1995 *preserve_ptr
= marker_position (preserve_marker
);
1996 unchain_marker (preserve_marker
);
2000 verify_interval_modification (current_buffer
, start
, end
);
2003 #ifdef CLASH_DETECTION
2004 if (!NILP (current_buffer
->file_truename
)
2005 /* Make binding buffer-file-name to nil effective. */
2006 && !NILP (current_buffer
->filename
)
2007 && SAVE_MODIFF
>= MODIFF
)
2008 lock_file (current_buffer
->file_truename
);
2010 /* At least warn if this file has changed on disk since it was visited. */
2011 if (!NILP (current_buffer
->filename
)
2012 && SAVE_MODIFF
>= MODIFF
2013 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2014 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
2015 call1 (intern ("ask-user-about-supersession-threat"),
2016 current_buffer
->filename
);
2017 #endif /* not CLASH_DETECTION */
2019 signal_before_change (start
, end
, preserve_ptr
);
2021 if (current_buffer
->newline_cache
)
2022 invalidate_region_cache (current_buffer
,
2023 current_buffer
->newline_cache
,
2024 start
- BEG
, Z
- end
);
2025 if (current_buffer
->width_run_cache
)
2026 invalidate_region_cache (current_buffer
,
2027 current_buffer
->width_run_cache
,
2028 start
- BEG
, Z
- end
);
2030 Vdeactivate_mark
= Qt
;
2033 /* These macros work with an argument named `preserve_ptr'
2034 and a local variable named `preserve_marker'. */
2036 #define PRESERVE_VALUE \
2037 if (preserve_ptr && NILP (preserve_marker)) \
2038 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2040 #define RESTORE_VALUE \
2041 if (! NILP (preserve_marker)) \
2043 *preserve_ptr = marker_position (preserve_marker); \
2044 unchain_marker (preserve_marker); \
2047 #define PRESERVE_START_END \
2048 if (NILP (start_marker)) \
2049 start_marker = Fcopy_marker (start, Qnil); \
2050 if (NILP (end_marker)) \
2051 end_marker = Fcopy_marker (end, Qnil);
2053 #define FETCH_START \
2054 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2057 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2059 /* Signal a change to the buffer immediately before it happens.
2060 START_INT and END_INT are the bounds of the text to be changed.
2062 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2063 by holding its value temporarily in a marker. */
2066 signal_before_change (start_int
, end_int
, preserve_ptr
)
2067 int start_int
, end_int
;
2070 Lisp_Object start
, end
;
2071 Lisp_Object start_marker
, end_marker
;
2072 Lisp_Object preserve_marker
;
2073 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2075 start
= make_number (start_int
);
2076 end
= make_number (end_int
);
2077 preserve_marker
= Qnil
;
2078 start_marker
= Qnil
;
2080 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2082 /* If buffer is unmodified, run a special hook for that case. */
2083 if (SAVE_MODIFF
>= MODIFF
2084 && !NILP (Vfirst_change_hook
)
2085 && !NILP (Vrun_hooks
))
2089 call1 (Vrun_hooks
, Qfirst_change_hook
);
2092 /* Run the before-change-function if any.
2093 We don't bother "binding" this variable to nil
2094 because it is obsolete anyway and new code should not use it. */
2095 if (!NILP (Vbefore_change_function
))
2099 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
2102 /* Now run the before-change-functions if any. */
2103 if (!NILP (Vbefore_change_functions
))
2105 Lisp_Object args
[3];
2106 Lisp_Object before_change_functions
;
2107 Lisp_Object after_change_functions
;
2108 struct gcpro gcpro1
, gcpro2
;
2113 /* "Bind" before-change-functions and after-change-functions
2114 to nil--but in a way that errors don't know about.
2115 That way, if there's an error in them, they will stay nil. */
2116 before_change_functions
= Vbefore_change_functions
;
2117 after_change_functions
= Vafter_change_functions
;
2118 Vbefore_change_functions
= Qnil
;
2119 Vafter_change_functions
= Qnil
;
2120 GCPRO2 (before_change_functions
, after_change_functions
);
2122 /* Actually run the hook functions. */
2123 args
[0] = Qbefore_change_functions
;
2124 args
[1] = FETCH_START
;
2125 args
[2] = FETCH_END
;
2126 run_hook_list_with_args (before_change_functions
, 3, args
);
2128 /* "Unbind" the variables we "bound" to nil. */
2129 Vbefore_change_functions
= before_change_functions
;
2130 Vafter_change_functions
= after_change_functions
;
2134 if (!NILP (current_buffer
->overlays_before
)
2135 || !NILP (current_buffer
->overlays_after
))
2138 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2139 FETCH_START
, FETCH_END
, Qnil
);
2142 if (! NILP (start_marker
))
2143 free_marker (start_marker
);
2144 if (! NILP (end_marker
))
2145 free_marker (end_marker
);
2150 /* Signal a change immediately after it happens.
2151 CHARPOS is the character position of the start of the changed text.
2152 LENDEL is the number of characters of the text before the change.
2153 (Not the whole buffer; just the part that was changed.)
2154 LENINS is the number of characters in that part of the text
2155 after the change. */
2158 signal_after_change (charpos
, lendel
, lenins
)
2159 int charpos
, lendel
, lenins
;
2161 /* If we are deferring calls to the after-change functions
2162 and there are no before-change functions,
2163 just record the args that we were going to use. */
2164 if (! NILP (Vcombine_after_change_calls
)
2165 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
2166 && NILP (current_buffer
->overlays_before
)
2167 && NILP (current_buffer
->overlays_after
))
2171 if (!NILP (combine_after_change_list
)
2172 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2173 Fcombine_after_change_execute ();
2175 elt
= Fcons (make_number (charpos
- BEG
),
2176 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2177 Fcons (make_number (lenins
- lendel
), Qnil
)));
2178 combine_after_change_list
2179 = Fcons (elt
, combine_after_change_list
);
2180 combine_after_change_buffer
= Fcurrent_buffer ();
2185 if (!NILP (combine_after_change_list
))
2186 Fcombine_after_change_execute ();
2188 /* Run the after-change-function if any.
2189 We don't bother "binding" this variable to nil
2190 because it is obsolete anyway and new code should not use it. */
2191 if (!NILP (Vafter_change_function
))
2192 call3 (Vafter_change_function
,
2193 make_number (charpos
), make_number (charpos
+ lenins
),
2194 make_number (lendel
));
2196 if (!NILP (Vafter_change_functions
))
2198 Lisp_Object args
[4];
2199 Lisp_Object before_change_functions
;
2200 Lisp_Object after_change_functions
;
2201 struct gcpro gcpro1
, gcpro2
;
2203 /* "Bind" before-change-functions and after-change-functions
2204 to nil--but in a way that errors don't know about.
2205 That way, if there's an error in them, they will stay nil. */
2206 before_change_functions
= Vbefore_change_functions
;
2207 after_change_functions
= Vafter_change_functions
;
2208 Vbefore_change_functions
= Qnil
;
2209 Vafter_change_functions
= Qnil
;
2210 GCPRO2 (before_change_functions
, after_change_functions
);
2212 /* Actually run the hook functions. */
2213 args
[0] = Qafter_change_functions
;
2214 XSETFASTINT (args
[1], charpos
);
2215 XSETFASTINT (args
[2], charpos
+ lenins
);
2216 XSETFASTINT (args
[3], lendel
);
2217 run_hook_list_with_args (after_change_functions
,
2220 /* "Unbind" the variables we "bound" to nil. */
2221 Vbefore_change_functions
= before_change_functions
;
2222 Vafter_change_functions
= after_change_functions
;
2226 if (!NILP (current_buffer
->overlays_before
)
2227 || !NILP (current_buffer
->overlays_after
))
2228 report_overlay_modification (make_number (charpos
),
2229 make_number (charpos
+ lenins
),
2231 make_number (charpos
),
2232 make_number (charpos
+ lenins
),
2233 make_number (lendel
));
2235 /* After an insertion, call the text properties
2236 insert-behind-hooks or insert-in-front-hooks. */
2238 report_interval_modification (charpos
, charpos
+ lenins
);
2242 Fcombine_after_change_execute_1 (val
)
2245 Vcombine_after_change_calls
= val
;
2249 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2250 Scombine_after_change_execute
, 0, 0, 0,
2251 "This function is for use internally in `combine-after-change-calls'.")
2254 register Lisp_Object val
;
2255 int count
= specpdl_ptr
- specpdl
;
2256 int beg
, end
, change
;
2260 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2262 Fset_buffer (combine_after_change_buffer
);
2264 /* # chars unchanged at beginning of buffer. */
2266 /* # chars unchanged at end of buffer. */
2268 /* Total amount of insertion (negative for deletion). */
2271 /* Scan the various individual changes,
2272 accumulating the range info in BEG, END and CHANGE. */
2273 for (tail
= combine_after_change_list
; CONSP (tail
);
2274 tail
= XCONS (tail
)->cdr
)
2277 int thisbeg
, thisend
, thischange
;
2279 /* Extract the info from the next element. */
2280 elt
= XCONS (tail
)->car
;
2283 thisbeg
= XINT (XCONS (elt
)->car
);
2285 elt
= XCONS (elt
)->cdr
;
2288 thisend
= XINT (XCONS (elt
)->car
);
2290 elt
= XCONS (elt
)->cdr
;
2293 thischange
= XINT (XCONS (elt
)->car
);
2295 /* Merge this range into the accumulated range. */
2296 change
+= thischange
;
2303 /* Get the current start and end positions of the range
2304 that was changed. */
2308 /* We are about to handle these, so discard them. */
2309 combine_after_change_list
= Qnil
;
2311 /* Now run the after-change functions for real.
2312 Turn off the flag that defers them. */
2313 record_unwind_protect (Fcombine_after_change_execute_1
,
2314 Vcombine_after_change_calls
);
2315 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2317 return unbind_to (count
, val
);
2322 staticpro (&combine_after_change_list
);
2323 combine_after_change_list
= Qnil
;
2325 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2326 "Used internally by the `combine-after-change-calls' macro.");
2327 Vcombine_after_change_calls
= Qnil
;
2329 defsubr (&Scombine_after_change_execute
);