*** empty log message ***
[emacs.git] / src / insdel.c
blob3383f4e56e285f6fb72c95946e30a28b5ced1a3e
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86,93,94,95,97,98, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "intervals.h"
26 #include "buffer.h"
27 #include "charset.h"
28 #include "window.h"
29 #include "blockinput.h"
30 #include "region-cache.h"
32 #ifndef NULL
33 #define NULL 0
34 #endif
36 #define min(x, y) ((x) < (y) ? (x) : (y))
37 #define max(x, y) ((x) > (y) ? (x) : (y))
39 static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
40 static void insert_from_buffer_1 ();
41 static void gap_left P_ ((int, int, int));
42 static void gap_right P_ ((int, int));
43 static void adjust_markers_gap_motion P_ ((int, int, int));
44 static void adjust_markers_for_insert P_ ((int, int, int, int, int));
45 void adjust_markers_for_delete P_ ((int, int, int, int));
46 static void adjust_markers_for_replace P_ ((int, int, int, int, int, int));
47 static void adjust_point P_ ((int, int));
49 Lisp_Object Fcombine_after_change_execute ();
51 /* Non-nil means don't call the after-change-functions right away,
52 just record an element in Vcombine_after_change_calls_list. */
53 Lisp_Object Vcombine_after_change_calls;
55 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
56 describing changes which happened while combine_after_change_calls
57 was nonzero. We use this to decide how to call them
58 once the deferral ends.
60 In each element.
61 BEG-UNCHANGED is the number of chars before the changed range.
62 END-UNCHANGED is the number of chars after the changed range,
63 and CHANGE-AMOUNT is the number of characters inserted by the change
64 (negative for a deletion). */
65 Lisp_Object combine_after_change_list;
67 /* Buffer which combine_after_change_list is about. */
68 Lisp_Object combine_after_change_buffer;
70 Lisp_Object Qinhibit_modification_hooks;
73 /* Check all markers in the current buffer, looking for something invalid. */
75 static int check_markers_debug_flag;
77 #define CHECK_MARKERS() \
78 if (check_markers_debug_flag) \
79 check_markers (); \
80 else
82 void
83 check_markers ()
85 register Lisp_Object tail;
86 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
88 tail = BUF_MARKERS (current_buffer);
90 while (! NILP (tail))
92 if (XMARKER (tail)->buffer->text != current_buffer->text)
93 abort ();
94 if (XMARKER (tail)->charpos > Z)
95 abort ();
96 if (XMARKER (tail)->bytepos > Z_BYTE)
97 abort ();
98 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail)->bytepos)))
99 abort ();
101 tail = XMARKER (tail)->chain;
105 /* Move gap to position CHARPOS.
106 Note that this can quit! */
108 void
109 move_gap (charpos)
110 int charpos;
112 move_gap_both (charpos, charpos_to_bytepos (charpos));
115 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
116 Note that this can quit! */
118 void
119 move_gap_both (charpos, bytepos)
120 int charpos, bytepos;
122 if (bytepos < GPT_BYTE)
123 gap_left (charpos, bytepos, 0);
124 else if (bytepos > GPT_BYTE)
125 gap_right (charpos, bytepos);
128 /* Move the gap to a position less than the current GPT.
129 BYTEPOS describes the new position as a byte position,
130 and CHARPOS is the corresponding char position.
131 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
133 static void
134 gap_left (charpos, bytepos, newgap)
135 register int charpos, bytepos;
136 int newgap;
138 register unsigned char *to, *from;
139 register int i;
140 int new_s1;
142 if (!newgap)
143 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
145 i = GPT_BYTE;
146 to = GAP_END_ADDR;
147 from = GPT_ADDR;
148 new_s1 = GPT_BYTE;
150 /* Now copy the characters. To move the gap down,
151 copy characters up. */
153 while (1)
155 /* I gets number of characters left to copy. */
156 i = new_s1 - bytepos;
157 if (i == 0)
158 break;
159 /* If a quit is requested, stop copying now.
160 Change BYTEPOS to be where we have actually moved the gap to. */
161 if (QUITP)
163 bytepos = new_s1;
164 charpos = BYTE_TO_CHAR (bytepos);
165 break;
167 /* Move at most 32000 chars before checking again for a quit. */
168 if (i > 32000)
169 i = 32000;
170 #ifdef GAP_USE_BCOPY
171 if (i >= 128
172 /* bcopy is safe if the two areas of memory do not overlap
173 or on systems where bcopy is always safe for moving upward. */
174 && (BCOPY_UPWARD_SAFE
175 || to - from >= 128))
177 /* If overlap is not safe, avoid it by not moving too many
178 characters at once. */
179 if (!BCOPY_UPWARD_SAFE && i > to - from)
180 i = to - from;
181 new_s1 -= i;
182 from -= i, to -= i;
183 bcopy (from, to, i);
185 else
186 #endif
188 new_s1 -= i;
189 while (--i >= 0)
190 *--to = *--from;
194 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
195 BYTEPOS is where the loop above stopped, which may be what was specified
196 or may be where a quit was detected. */
197 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
198 GPT_BYTE = bytepos;
199 GPT = charpos;
200 if (bytepos < charpos)
201 abort ();
202 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
203 QUIT;
206 /* Move the gap to a position greater than than the current GPT.
207 BYTEPOS describes the new position as a byte position,
208 and CHARPOS is the corresponding char position. */
210 static void
211 gap_right (charpos, bytepos)
212 register int charpos, bytepos;
214 register unsigned char *to, *from;
215 register int i;
216 int new_s1;
218 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
220 i = GPT_BYTE;
221 from = GAP_END_ADDR;
222 to = GPT_ADDR;
223 new_s1 = GPT_BYTE;
225 /* Now copy the characters. To move the gap up,
226 copy characters down. */
228 while (1)
230 /* I gets number of characters left to copy. */
231 i = bytepos - new_s1;
232 if (i == 0)
233 break;
234 /* If a quit is requested, stop copying now.
235 Change BYTEPOS to be where we have actually moved the gap to. */
236 if (QUITP)
238 bytepos = new_s1;
239 charpos = BYTE_TO_CHAR (bytepos);
240 break;
242 /* Move at most 32000 chars before checking again for a quit. */
243 if (i > 32000)
244 i = 32000;
245 #ifdef GAP_USE_BCOPY
246 if (i >= 128
247 /* bcopy is safe if the two areas of memory do not overlap
248 or on systems where bcopy is always safe for moving downward. */
249 && (BCOPY_DOWNWARD_SAFE
250 || from - to >= 128))
252 /* If overlap is not safe, avoid it by not moving too many
253 characters at once. */
254 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
255 i = from - to;
256 new_s1 += i;
257 bcopy (from, to, i);
258 from += i, to += i;
260 else
261 #endif
263 new_s1 += i;
264 while (--i >= 0)
265 *to++ = *from++;
269 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
270 - GAP_SIZE);
271 GPT = charpos;
272 GPT_BYTE = bytepos;
273 if (bytepos < charpos)
274 abort ();
275 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
276 QUIT;
279 /* Add AMOUNT to the byte position of every marker in the current buffer
280 whose current byte position is between FROM (exclusive) and TO (inclusive).
282 Also, any markers past the outside of that interval, in the direction
283 of adjustment, are first moved back to the near end of the interval
284 and then adjusted by AMOUNT.
286 When the latter adjustment is done, if AMOUNT is negative,
287 we record the adjustment for undo. (This case happens only for
288 deletion.)
290 The markers' character positions are not altered,
291 because gap motion does not affect character positions. */
293 int adjust_markers_test;
295 static void
296 adjust_markers_gap_motion (from, to, amount)
297 register int from, to, amount;
299 /* Now that a marker has a bytepos, not counting the gap,
300 nothing needs to be done here. */
301 #if 0
302 Lisp_Object marker;
303 register struct Lisp_Marker *m;
304 register int mpos;
306 marker = BUF_MARKERS (current_buffer);
308 while (!NILP (marker))
310 m = XMARKER (marker);
311 mpos = m->bytepos;
312 if (amount > 0)
314 if (mpos > to && mpos < to + amount)
316 if (adjust_markers_test)
317 abort ();
318 mpos = to + amount;
321 else
323 /* Here's the case where a marker is inside text being deleted.
324 AMOUNT can be negative for gap motion, too,
325 but then this range contains no markers. */
326 if (mpos > from + amount && mpos <= from)
328 if (adjust_markers_test)
329 abort ();
330 mpos = from + amount;
333 if (mpos > from && mpos <= to)
334 mpos += amount;
335 m->bufpos = mpos;
336 marker = m->chain;
338 #endif
341 /* Adjust all markers for a deletion
342 whose range in bytes is FROM_BYTE to TO_BYTE.
343 The range in charpos is FROM to TO.
345 This function assumes that the gap is adjacent to
346 or inside of the range being deleted. */
348 void
349 adjust_markers_for_delete (from, from_byte, to, to_byte)
350 register int from, from_byte, to, to_byte;
352 Lisp_Object marker;
353 register struct Lisp_Marker *m;
354 register int charpos;
356 marker = BUF_MARKERS (current_buffer);
358 while (!NILP (marker))
360 m = XMARKER (marker);
361 charpos = m->charpos;
363 if (charpos > Z)
364 abort ();
366 /* If the marker is after the deletion,
367 relocate by number of chars / bytes deleted. */
368 if (charpos > to)
370 m->charpos -= to - from;
371 m->bytepos -= to_byte - from_byte;
373 /* Here's the case where a marker is inside text being deleted. */
374 else if (charpos > from)
376 if (! m->insertion_type)
377 /* Normal markers will end up at the beginning of the
378 re-inserted text after undoing a deletion, and must be
379 adjusted to move them to the correct place. */
380 record_marker_adjustment (marker, from - charpos);
381 else if (charpos < to)
382 /* Before-insertion markers will automatically move forward
383 upon re-inserting the deleted text, so we have to arrange
384 for them to move backward to the correct position. */
385 record_marker_adjustment (marker, charpos - to);
387 m->charpos = from;
388 m->bytepos = from_byte;
390 /* Here's the case where a before-insertion marker is immediately
391 before the deleted region. */
392 else if (charpos == from && m->insertion_type)
394 /* Undoing the change uses normal insertion, which will
395 incorrectly make MARKER move forward, so we arrange for it
396 to then move backward to the correct place at the beginning
397 of the deleted region. */
398 record_marker_adjustment (marker, to - from);
401 marker = m->chain;
406 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
407 to TO / TO_BYTE. We have to relocate the charpos of every marker
408 that points after the insertion (but not their bytepos).
410 When a marker points at the insertion point,
411 we advance it if either its insertion-type is t
412 or BEFORE_MARKERS is true. */
414 static void
415 adjust_markers_for_insert (from, from_byte, to, to_byte, before_markers)
416 register int from, from_byte, to, to_byte;
417 int before_markers;
419 Lisp_Object marker;
420 int adjusted = 0;
421 int nchars = to - from;
422 int nbytes = to_byte - from_byte;
424 marker = BUF_MARKERS (current_buffer);
426 while (!NILP (marker))
428 register struct Lisp_Marker *m = XMARKER (marker);
430 /* In a single-byte buffer, a marker's two positions must be
431 equal. */
432 if (Z == Z_BYTE)
434 if (m->charpos != m->bytepos)
435 abort ();
438 if (m->bytepos == from_byte)
440 if (m->insertion_type || before_markers)
442 m->bytepos = to_byte;
443 m->charpos = to;
444 if (m->insertion_type)
445 adjusted = 1;
448 else if (m->bytepos > from_byte)
450 m->bytepos += nbytes;
451 m->charpos += nchars;
454 marker = m->chain;
457 /* Adjusting only markers whose insertion-type is t may result in
458 disordered overlays in the slot `overlays_before'. */
459 if (adjusted)
460 fix_overlays_before (current_buffer, from, to);
463 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
465 This is used only when the value of point changes due to an insert
466 or delete; it does not represent a conceptual change in point as a
467 marker. In particular, point is not crossing any interval
468 boundaries, so there's no need to use the usual SET_PT macro. In
469 fact it would be incorrect to do so, because either the old or the
470 new value of point is out of sync with the current set of
471 intervals. */
473 static void
474 adjust_point (nchars, nbytes)
475 int nchars, nbytes;
477 BUF_PT (current_buffer) += nchars;
478 BUF_PT_BYTE (current_buffer) += nbytes;
480 /* In a single-byte buffer, the two positions must be equal. */
481 if (ZV == ZV_BYTE
482 && PT != PT_BYTE)
483 abort ();
486 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
487 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
488 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
489 an insertion. */
491 static void
492 adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
493 new_chars, new_bytes)
494 int from, from_byte, old_chars, old_bytes, new_chars, new_bytes;
496 Lisp_Object marker = BUF_MARKERS (current_buffer);
497 int prev_to_byte = from_byte + old_bytes;
498 int diff_chars = new_chars - old_chars;
499 int diff_bytes = new_bytes - old_bytes;
501 while (!NILP (marker))
503 register struct Lisp_Marker *m = XMARKER (marker);
505 if (m->bytepos >= prev_to_byte)
507 m->charpos += diff_chars;
508 m->bytepos += diff_bytes;
510 else if (m->bytepos > from_byte)
512 m->charpos = from;
513 m->bytepos = from_byte;
516 marker = m->chain;
519 CHECK_MARKERS ();
523 /* Make the gap NBYTES_ADDED bytes longer. */
525 void
526 make_gap (nbytes_added)
527 int nbytes_added;
529 Lisp_Object tem;
530 int real_gap_loc;
531 int real_gap_loc_byte;
532 int old_gap_size;
534 /* If we have to get more space, get enough to last a while. */
535 nbytes_added += 2000;
537 /* Don't allow a buffer size that won't fit in an int
538 even if it will fit in a Lisp integer.
539 That won't work because so many places use `int'. */
541 if (Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added
542 >= ((unsigned) 1 << (min (BITS_PER_INT, VALBITS) - 1)))
543 error ("Buffer exceeds maximum size");
545 enlarge_buffer_text (current_buffer, nbytes_added);
547 /* Prevent quitting in move_gap. */
548 tem = Vinhibit_quit;
549 Vinhibit_quit = Qt;
551 real_gap_loc = GPT;
552 real_gap_loc_byte = GPT_BYTE;
553 old_gap_size = GAP_SIZE;
555 /* Call the newly allocated space a gap at the end of the whole space. */
556 GPT = Z + GAP_SIZE;
557 GPT_BYTE = Z_BYTE + GAP_SIZE;
558 GAP_SIZE = nbytes_added;
560 /* Move the new gap down to be consecutive with the end of the old one.
561 This adjusts the markers properly too. */
562 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
564 /* Now combine the two into one large gap. */
565 GAP_SIZE += old_gap_size;
566 GPT = real_gap_loc;
567 GPT_BYTE = real_gap_loc_byte;
569 /* Put an anchor. */
570 *(Z_ADDR) = 0;
572 Vinhibit_quit = tem;
575 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
576 FROM_MULTIBYTE says whether the incoming text is multibyte.
577 TO_MULTIBYTE says whether to store the text as multibyte.
578 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
580 Return the number of bytes stored at TO_ADDR. */
583 copy_text (from_addr, to_addr, nbytes,
584 from_multibyte, to_multibyte)
585 unsigned char *from_addr;
586 unsigned char *to_addr;
587 int nbytes;
588 int from_multibyte, to_multibyte;
590 if (from_multibyte == to_multibyte)
592 bcopy (from_addr, to_addr, nbytes);
593 return nbytes;
595 else if (from_multibyte)
597 int nchars = 0;
598 int bytes_left = nbytes;
599 Lisp_Object tbl = Qnil;
601 /* We set the variable tbl to the reverse table of
602 Vnonascii_translation_table in advance. */
603 if (CHAR_TABLE_P (Vnonascii_translation_table))
605 tbl = Fchar_table_extra_slot (Vnonascii_translation_table,
606 make_number (0));
607 if (!CHAR_TABLE_P (tbl))
608 tbl = Qnil;
611 /* Convert multibyte to single byte. */
612 while (bytes_left > 0)
614 int thislen, c;
615 c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
616 if (!SINGLE_BYTE_CHAR_P (c))
617 c = multibyte_char_to_unibyte (c, tbl);
618 *to_addr++ = c;
619 from_addr += thislen;
620 bytes_left -= thislen;
621 nchars++;
623 return nchars;
625 else
627 unsigned char *initial_to_addr = to_addr;
629 /* Convert single-byte to multibyte. */
630 while (nbytes > 0)
632 int c = *from_addr++;
634 if (c >= 0200)
636 c = unibyte_char_to_multibyte (c);
637 to_addr += CHAR_STRING (c, to_addr);
638 nbytes--;
640 else
641 /* Special case for speed. */
642 *to_addr++ = c, nbytes--;
644 return to_addr - initial_to_addr;
648 /* Return the number of bytes it would take
649 to convert some single-byte text to multibyte.
650 The single-byte text consists of NBYTES bytes at PTR. */
653 count_size_as_multibyte (ptr, nbytes)
654 unsigned char *ptr;
655 int nbytes;
657 int i;
658 int outgoing_nbytes = 0;
660 for (i = 0; i < nbytes; i++)
662 unsigned int c = *ptr++;
664 if (c < 0200)
665 outgoing_nbytes++;
666 else
668 c = unibyte_char_to_multibyte (c);
669 outgoing_nbytes += CHAR_BYTES (c);
673 return outgoing_nbytes;
676 /* Insert a string of specified length before point.
677 This function judges multibyteness based on
678 enable_multibyte_characters in the current buffer;
679 it never converts between single-byte and multibyte.
681 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
682 prepare_to_modify_buffer could relocate the text. */
684 void
685 insert (string, nbytes)
686 register unsigned char *string;
687 register int nbytes;
689 if (nbytes > 0)
691 int opoint = PT;
692 insert_1 (string, nbytes, 0, 1, 0);
693 signal_after_change (opoint, 0, PT - opoint);
694 update_compositions (opoint, PT, CHECK_BORDER);
698 /* Likewise, but inherit text properties from neighboring characters. */
700 void
701 insert_and_inherit (string, nbytes)
702 register unsigned char *string;
703 register int nbytes;
705 if (nbytes > 0)
707 int opoint = PT;
708 insert_1 (string, nbytes, 1, 1, 0);
709 signal_after_change (opoint, 0, PT - opoint);
710 update_compositions (opoint, PT, CHECK_BORDER);
714 /* Insert the character C before point. Do not inherit text properties. */
716 void
717 insert_char (c)
718 int c;
720 unsigned char str[MAX_MULTIBYTE_LENGTH];
721 int len;
723 if (! NILP (current_buffer->enable_multibyte_characters))
724 len = CHAR_STRING (c, str);
725 else
727 len = 1;
728 str[0] = c;
731 insert (str, len);
734 /* Insert the null-terminated string S before point. */
736 void
737 insert_string (s)
738 char *s;
740 insert (s, strlen (s));
743 /* Like `insert' except that all markers pointing at the place where
744 the insertion happens are adjusted to point after it.
745 Don't use this function to insert part of a Lisp string,
746 since gc could happen and relocate it. */
748 void
749 insert_before_markers (string, nbytes)
750 unsigned char *string;
751 register int nbytes;
753 if (nbytes > 0)
755 int opoint = PT;
757 insert_1 (string, nbytes, 0, 1, 1);
758 signal_after_change (opoint, 0, PT - opoint);
759 update_compositions (opoint, PT, CHECK_BORDER);
763 /* Likewise, but inherit text properties from neighboring characters. */
765 void
766 insert_before_markers_and_inherit (string, nbytes)
767 unsigned char *string;
768 register int nbytes;
770 if (nbytes > 0)
772 int opoint = PT;
774 insert_1 (string, nbytes, 1, 1, 1);
775 signal_after_change (opoint, 0, PT - opoint);
776 update_compositions (opoint, PT, CHECK_BORDER);
780 /* Subroutine used by the insert functions above. */
782 void
783 insert_1 (string, nbytes, inherit, prepare, before_markers)
784 register unsigned char *string;
785 register int nbytes;
786 int inherit, prepare, before_markers;
788 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
789 inherit, prepare, before_markers);
793 #ifdef BYTE_COMBINING_DEBUG
795 /* See if the bytes before POS/POS_BYTE combine with bytes
796 at the start of STRING to form a single character.
797 If so, return the number of bytes at the start of STRING
798 which combine in this way. Otherwise, return 0. */
801 count_combining_before (string, length, pos, pos_byte)
802 unsigned char *string;
803 int length;
804 int pos, pos_byte;
806 int len, combining_bytes;
807 unsigned char *p;
809 if (NILP (current_buffer->enable_multibyte_characters))
810 return 0;
812 /* At first, we can exclude the following cases:
813 (1) STRING[0] can't be a following byte of multibyte sequence.
814 (2) POS is the start of the current buffer.
815 (3) A character before POS is not a multibyte character. */
816 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
817 return 0;
818 if (pos_byte == BEG_BYTE) /* case (2) */
819 return 0;
820 len = 1;
821 p = BYTE_POS_ADDR (pos_byte - 1);
822 while (! CHAR_HEAD_P (*p)) p--, len++;
823 if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
824 return 0;
826 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
827 if (combining_bytes <= 0)
828 /* The character preceding POS is, complete and no room for
829 combining bytes (combining_bytes == 0), or an independent 8-bit
830 character (combining_bytes < 0). */
831 return 0;
833 /* We have a combination situation. Count the bytes at STRING that
834 may combine. */
835 p = string + 1;
836 while (!CHAR_HEAD_P (*p) && p < string + length)
837 p++;
839 return (combining_bytes < p - string ? combining_bytes : p - string);
842 /* See if the bytes after POS/POS_BYTE combine with bytes
843 at the end of STRING to form a single character.
844 If so, return the number of bytes after POS/POS_BYTE
845 which combine in this way. Otherwise, return 0. */
848 count_combining_after (string, length, pos, pos_byte)
849 unsigned char *string;
850 int length;
851 int pos, pos_byte;
853 int opos_byte = pos_byte;
854 int i;
855 int bytes;
856 unsigned char *bufp;
858 if (NILP (current_buffer->enable_multibyte_characters))
859 return 0;
861 /* At first, we can exclude the following cases:
862 (1) The last byte of STRING is an ASCII.
863 (2) POS is the last of the current buffer.
864 (3) A character at POS can't be a following byte of multibyte
865 character. */
866 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
867 return 0;
868 if (pos_byte == Z_BYTE) /* case (2) */
869 return 0;
870 bufp = BYTE_POS_ADDR (pos_byte);
871 if (CHAR_HEAD_P (*bufp)) /* case (3) */
872 return 0;
874 i = length - 1;
875 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
877 i--;
879 if (i < 0)
881 /* All characters in STRING are not character head. We must
882 check also preceding bytes at POS. We are sure that the gap
883 is at POS. */
884 unsigned char *p = BEG_ADDR;
885 i = pos_byte - 2;
886 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
887 i--;
888 if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
889 return 0;
891 bytes = BYTES_BY_CHAR_HEAD (p[i]);
892 return (bytes <= pos_byte - 1 - i + length
894 : bytes - (pos_byte - 1 - i + length));
896 if (!BASE_LEADING_CODE_P (string[i]))
897 return 0;
899 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
900 bufp++, pos_byte++;
901 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
903 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
906 #endif
909 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
910 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
911 are the same as in insert_1. */
913 void
914 insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
915 register unsigned char *string;
916 register int nchars, nbytes;
917 int inherit, prepare, before_markers;
919 if (nchars == 0)
920 return;
922 if (NILP (current_buffer->enable_multibyte_characters))
923 nchars = nbytes;
925 if (prepare)
926 /* Do this before moving and increasing the gap,
927 because the before-change hooks might move the gap
928 or make it smaller. */
929 prepare_to_modify_buffer (PT, PT, NULL);
931 if (PT != GPT)
932 move_gap_both (PT, PT_BYTE);
933 if (GAP_SIZE < nbytes)
934 make_gap (nbytes - GAP_SIZE);
936 #ifdef BYTE_COMBINING_DEBUG
937 if (count_combining_before (string, nbytes, PT, PT_BYTE)
938 || count_combining_after (string, nbytes, PT, PT_BYTE))
939 abort ();
940 #endif
942 /* Record deletion of the surrounding text that combines with
943 the insertion. This, together with recording the insertion,
944 will add up to the right stuff in the undo list. */
945 record_insert (PT, nchars);
946 MODIFF++;
948 bcopy (string, GPT_ADDR, nbytes);
950 GAP_SIZE -= nbytes;
951 GPT += nchars;
952 ZV += nchars;
953 Z += nchars;
954 GPT_BYTE += nbytes;
955 ZV_BYTE += nbytes;
956 Z_BYTE += nbytes;
957 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
959 if (GPT_BYTE < GPT)
960 abort ();
962 adjust_overlays_for_insert (PT, nchars);
963 adjust_markers_for_insert (PT, PT_BYTE,
964 PT + nchars, PT_BYTE + nbytes,
965 before_markers);
967 if (BUF_INTERVALS (current_buffer) != 0)
968 offset_intervals (current_buffer, PT, nchars);
970 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
971 set_text_properties (make_number (PT), make_number (PT + nchars),
972 Qnil, Qnil, Qnil);
974 adjust_point (nchars, nbytes);
976 CHECK_MARKERS ();
979 /* Insert the part of the text of STRING, a Lisp object assumed to be
980 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
981 starting at position POS / POS_BYTE. If the text of STRING has properties,
982 copy them into the buffer.
984 It does not work to use `insert' for this, because a GC could happen
985 before we bcopy the stuff into the buffer, and relocate the string
986 without insert noticing. */
988 void
989 insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
990 Lisp_Object string;
991 register int pos, pos_byte, length, length_byte;
992 int inherit;
994 int opoint = PT;
995 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
996 inherit, 0);
997 signal_after_change (opoint, 0, PT - opoint);
998 update_compositions (opoint, PT, CHECK_BORDER);
1001 /* Like `insert_from_string' except that all markers pointing
1002 at the place where the insertion happens are adjusted to point after it. */
1004 void
1005 insert_from_string_before_markers (string, pos, pos_byte,
1006 length, length_byte, inherit)
1007 Lisp_Object string;
1008 register int pos, pos_byte, length, length_byte;
1009 int inherit;
1011 int opoint = PT;
1012 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1013 inherit, 1);
1014 signal_after_change (opoint, 0, PT - opoint);
1015 update_compositions (opoint, PT, CHECK_BORDER);
1018 /* Subroutine of the insertion functions above. */
1020 static void
1021 insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
1022 inherit, before_markers)
1023 Lisp_Object string;
1024 register int pos, pos_byte, nchars, nbytes;
1025 int inherit, before_markers;
1027 struct gcpro gcpro1;
1028 int outgoing_nbytes = nbytes;
1029 INTERVAL intervals;
1031 /* Make OUTGOING_NBYTES describe the text
1032 as it will be inserted in this buffer. */
1034 if (NILP (current_buffer->enable_multibyte_characters))
1035 outgoing_nbytes = nchars;
1036 else if (! STRING_MULTIBYTE (string))
1037 outgoing_nbytes
1038 = count_size_as_multibyte (&XSTRING (string)->data[pos_byte],
1039 nbytes);
1041 GCPRO1 (string);
1042 /* Do this before moving and increasing the gap,
1043 because the before-change hooks might move the gap
1044 or make it smaller. */
1045 prepare_to_modify_buffer (PT, PT, NULL);
1047 if (PT != GPT)
1048 move_gap_both (PT, PT_BYTE);
1049 if (GAP_SIZE < outgoing_nbytes)
1050 make_gap (outgoing_nbytes - GAP_SIZE);
1051 UNGCPRO;
1053 /* Copy the string text into the buffer, perhaps converting
1054 between single-byte and multibyte. */
1055 copy_text (XSTRING (string)->data + pos_byte, GPT_ADDR, nbytes,
1056 STRING_MULTIBYTE (string),
1057 ! NILP (current_buffer->enable_multibyte_characters));
1059 #ifdef BYTE_COMBINING_DEBUG
1060 /* We have copied text into the gap, but we have not altered
1061 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1062 to these functions and get the same results as we would
1063 have got earlier on. Meanwhile, PT_ADDR does point to
1064 the text that has been stored by copy_text. */
1065 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1066 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1067 abort ();
1068 #endif
1070 record_insert (PT, nchars);
1071 MODIFF++;
1073 GAP_SIZE -= outgoing_nbytes;
1074 GPT += nchars;
1075 ZV += nchars;
1076 Z += nchars;
1077 GPT_BYTE += outgoing_nbytes;
1078 ZV_BYTE += outgoing_nbytes;
1079 Z_BYTE += outgoing_nbytes;
1080 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1082 if (GPT_BYTE < GPT)
1083 abort ();
1085 adjust_overlays_for_insert (PT, nchars);
1086 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1087 PT_BYTE + outgoing_nbytes,
1088 before_markers);
1090 offset_intervals (current_buffer, PT, nchars);
1092 intervals = XSTRING (string)->intervals;
1093 /* Get the intervals for the part of the string we are inserting. */
1094 if (nbytes < STRING_BYTES (XSTRING (string)))
1095 intervals = copy_intervals (intervals, pos, nchars);
1097 /* Insert those intervals. */
1098 graft_intervals_into_buffer (intervals, PT, nchars,
1099 current_buffer, inherit);
1101 adjust_point (nchars, outgoing_nbytes);
1104 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1105 current buffer. If the text in BUF has properties, they are absorbed
1106 into the current buffer.
1108 It does not work to use `insert' for this, because a malloc could happen
1109 and relocate BUF's text before the bcopy happens. */
1111 void
1112 insert_from_buffer (buf, charpos, nchars, inherit)
1113 struct buffer *buf;
1114 int charpos, nchars;
1115 int inherit;
1117 int opoint = PT;
1119 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1120 signal_after_change (opoint, 0, PT - opoint);
1121 update_compositions (opoint, PT, CHECK_BORDER);
1124 static void
1125 insert_from_buffer_1 (buf, from, nchars, inherit)
1126 struct buffer *buf;
1127 int from, nchars;
1128 int inherit;
1130 register Lisp_Object temp;
1131 int chunk, chunk_expanded;
1132 int from_byte = buf_charpos_to_bytepos (buf, from);
1133 int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1134 int incoming_nbytes = to_byte - from_byte;
1135 int outgoing_nbytes = incoming_nbytes;
1136 INTERVAL intervals;
1138 /* Make OUTGOING_NBYTES describe the text
1139 as it will be inserted in this buffer. */
1141 if (NILP (current_buffer->enable_multibyte_characters))
1142 outgoing_nbytes = nchars;
1143 else if (NILP (buf->enable_multibyte_characters))
1145 int outgoing_before_gap = 0;
1146 int outgoing_after_gap = 0;
1148 if (from < BUF_GPT (buf))
1150 chunk = BUF_GPT_BYTE (buf) - from_byte;
1151 if (chunk > incoming_nbytes)
1152 chunk = incoming_nbytes;
1153 outgoing_before_gap
1154 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1155 chunk);
1157 else
1158 chunk = 0;
1160 if (chunk < incoming_nbytes)
1161 outgoing_after_gap
1162 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1163 from_byte + chunk),
1164 incoming_nbytes - chunk);
1166 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1169 /* Make sure point-max won't overflow after this insertion. */
1170 XSETINT (temp, outgoing_nbytes + Z);
1171 if (outgoing_nbytes + Z != XINT (temp))
1172 error ("Maximum buffer size exceeded");
1174 /* Do this before moving and increasing the gap,
1175 because the before-change hooks might move the gap
1176 or make it smaller. */
1177 prepare_to_modify_buffer (PT, PT, NULL);
1179 if (PT != GPT)
1180 move_gap_both (PT, PT_BYTE);
1181 if (GAP_SIZE < outgoing_nbytes)
1182 make_gap (outgoing_nbytes - GAP_SIZE);
1184 if (from < BUF_GPT (buf))
1186 chunk = BUF_GPT_BYTE (buf) - from_byte;
1187 if (chunk > incoming_nbytes)
1188 chunk = incoming_nbytes;
1189 /* Record number of output bytes, so we know where
1190 to put the output from the second copy_text. */
1191 chunk_expanded
1192 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1193 GPT_ADDR, chunk,
1194 ! NILP (buf->enable_multibyte_characters),
1195 ! NILP (current_buffer->enable_multibyte_characters));
1197 else
1198 chunk_expanded = chunk = 0;
1200 if (chunk < incoming_nbytes)
1201 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1202 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1203 ! NILP (buf->enable_multibyte_characters),
1204 ! NILP (current_buffer->enable_multibyte_characters));
1206 #ifdef BYTE_COMBINING_DEBUG
1207 /* We have copied text into the gap, but we have not altered
1208 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1209 to these functions and get the same results as we would
1210 have got earlier on. Meanwhile, GPT_ADDR does point to
1211 the text that has been stored by copy_text. */
1212 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1213 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1214 abort ();
1215 #endif
1217 record_insert (PT, nchars);
1218 MODIFF++;
1220 GAP_SIZE -= outgoing_nbytes;
1221 GPT += nchars;
1222 ZV += nchars;
1223 Z += nchars;
1224 GPT_BYTE += outgoing_nbytes;
1225 ZV_BYTE += outgoing_nbytes;
1226 Z_BYTE += outgoing_nbytes;
1227 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1229 if (GPT_BYTE < GPT)
1230 abort ();
1232 adjust_overlays_for_insert (PT, nchars);
1233 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1234 PT_BYTE + outgoing_nbytes,
1237 if (BUF_INTERVALS (current_buffer) != 0)
1238 offset_intervals (current_buffer, PT, nchars);
1240 /* Get the intervals for the part of the string we are inserting. */
1241 intervals = BUF_INTERVALS (buf);
1242 if (outgoing_nbytes < BUF_Z_BYTE (buf) - BUF_BEG_BYTE (buf))
1244 if (buf == current_buffer && PT <= from)
1245 from += nchars;
1246 intervals = copy_intervals (intervals, from, nchars);
1249 /* Insert those intervals. */
1250 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1252 adjust_point (nchars, outgoing_nbytes);
1255 /* Record undo information and adjust markers and position keepers for
1256 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1257 chars (LEN_BYTE bytes) which resides in the gap just after
1258 GPT_ADDR.
1260 PREV_TEXT nil means the new text was just inserted. */
1262 void
1263 adjust_after_replace (from, from_byte, prev_text, len, len_byte)
1264 int from, from_byte, len, len_byte;
1265 Lisp_Object prev_text;
1267 int nchars_del = 0, nbytes_del = 0;
1269 #ifdef BYTE_COMBINING_DEBUG
1270 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1271 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1272 abort ();
1273 #endif
1275 if (STRINGP (prev_text))
1277 nchars_del = XSTRING (prev_text)->size;
1278 nbytes_del = STRING_BYTES (XSTRING (prev_text));
1281 /* Update various buffer positions for the new text. */
1282 GAP_SIZE -= len_byte;
1283 ZV += len; Z+= len;
1284 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1285 GPT += len; GPT_BYTE += len_byte;
1286 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1288 if (nchars_del > 0)
1289 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1290 len, len_byte);
1291 else
1292 adjust_markers_for_insert (from, from_byte,
1293 from + len, from_byte + len_byte, 0);
1295 if (! EQ (current_buffer->undo_list, Qt))
1297 if (nchars_del > 0)
1298 record_delete (from, prev_text);
1299 record_insert (from, len);
1302 if (len > nchars_del)
1303 adjust_overlays_for_insert (from, len - nchars_del);
1304 else if (len < nchars_del)
1305 adjust_overlays_for_delete (from, nchars_del - len);
1306 if (BUF_INTERVALS (current_buffer) != 0)
1308 offset_intervals (current_buffer, from, len - nchars_del);
1311 if (from < PT)
1312 adjust_point (len - nchars_del, len_byte - nbytes_del);
1314 /* As byte combining will decrease Z, we must check this again. */
1315 if (Z - GPT < END_UNCHANGED)
1316 END_UNCHANGED = Z - GPT;
1318 CHECK_MARKERS ();
1320 if (len == 0)
1321 evaporate_overlays (from);
1322 MODIFF++;
1325 /* Record undo information, adjust markers and position keepers for an
1326 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1327 text already exists in the current buffer but character length (TO
1328 - FROM) may be incorrect, the correct length is NEWLEN. */
1330 void
1331 adjust_after_insert (from, from_byte, to, to_byte, newlen)
1332 int from, from_byte, to, to_byte, newlen;
1334 int len = to - from, len_byte = to_byte - from_byte;
1336 if (GPT != to)
1337 move_gap_both (to, to_byte);
1338 GAP_SIZE += len_byte;
1339 GPT -= len; GPT_BYTE -= len_byte;
1340 ZV -= len; ZV_BYTE -= len_byte;
1341 Z -= len; Z_BYTE -= len_byte;
1342 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1345 /* Replace the text from character positions FROM to TO with NEW,
1346 If PREPARE is nonzero, call prepare_to_modify_buffer.
1347 If INHERIT, the newly inserted text should inherit text properties
1348 from the surrounding non-deleted text. */
1350 /* Note that this does not yet handle markers quite right.
1351 Also it needs to record a single undo-entry that does a replacement
1352 rather than a separate delete and insert.
1353 That way, undo will also handle markers properly.
1355 But if MARKERS is 0, don't relocate markers. */
1357 void
1358 replace_range (from, to, new, prepare, inherit, markers)
1359 Lisp_Object new;
1360 int from, to, prepare, inherit, markers;
1362 int inschars = XSTRING (new)->size;
1363 int insbytes = STRING_BYTES (XSTRING (new));
1364 int from_byte, to_byte;
1365 int nbytes_del, nchars_del;
1366 register Lisp_Object temp;
1367 struct gcpro gcpro1;
1368 INTERVAL intervals;
1369 int outgoing_insbytes = insbytes;
1370 Lisp_Object deletion;
1372 CHECK_MARKERS ();
1374 GCPRO1 (new);
1375 deletion = Qnil;
1377 if (prepare)
1379 int range_length = to - from;
1380 prepare_to_modify_buffer (from, to, &from);
1381 to = from + range_length;
1384 UNGCPRO;
1386 /* Make args be valid */
1387 if (from < BEGV)
1388 from = BEGV;
1389 if (to > ZV)
1390 to = ZV;
1392 from_byte = CHAR_TO_BYTE (from);
1393 to_byte = CHAR_TO_BYTE (to);
1395 nchars_del = to - from;
1396 nbytes_del = to_byte - from_byte;
1398 if (nbytes_del <= 0 && insbytes == 0)
1399 return;
1401 /* Make OUTGOING_INSBYTES describe the text
1402 as it will be inserted in this buffer. */
1404 if (NILP (current_buffer->enable_multibyte_characters))
1405 outgoing_insbytes = inschars;
1406 else if (! STRING_MULTIBYTE (new))
1407 outgoing_insbytes
1408 = count_size_as_multibyte (XSTRING (new)->data, insbytes);
1410 /* Make sure point-max won't overflow after this insertion. */
1411 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1412 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1413 error ("Maximum buffer size exceeded");
1415 GCPRO1 (new);
1417 /* Make sure the gap is somewhere in or next to what we are deleting. */
1418 if (from > GPT)
1419 gap_right (from, from_byte);
1420 if (to < GPT)
1421 gap_left (to, to_byte, 0);
1423 /* Even if we don't record for undo, we must keep the original text
1424 because we may have to recover it because of inappropriate byte
1425 combining. */
1426 if (! EQ (current_buffer->undo_list, Qt))
1427 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1429 if (markers)
1430 /* Relocate all markers pointing into the new, larger gap
1431 to point at the end of the text before the gap.
1432 Do this before recording the deletion,
1433 so that undo handles this after reinserting the text. */
1434 adjust_markers_for_delete (from, from_byte, to, to_byte);
1436 GAP_SIZE += nbytes_del;
1437 ZV -= nchars_del;
1438 Z -= nchars_del;
1439 ZV_BYTE -= nbytes_del;
1440 Z_BYTE -= nbytes_del;
1441 GPT = from;
1442 GPT_BYTE = from_byte;
1443 *(GPT_ADDR) = 0; /* Put an anchor. */
1445 if (GPT_BYTE < GPT)
1446 abort ();
1448 if (GPT - BEG < BEG_UNCHANGED)
1449 BEG_UNCHANGED = GPT - BEG;
1450 if (Z - GPT < END_UNCHANGED)
1451 END_UNCHANGED = Z - GPT;
1453 if (GAP_SIZE < insbytes)
1454 make_gap (insbytes - GAP_SIZE);
1456 /* Copy the string text into the buffer, perhaps converting
1457 between single-byte and multibyte. */
1458 copy_text (XSTRING (new)->data, GPT_ADDR, insbytes,
1459 STRING_MULTIBYTE (new),
1460 ! NILP (current_buffer->enable_multibyte_characters));
1462 #ifdef BYTE_COMBINING_DEBUG
1463 /* We have copied text into the gap, but we have not marked
1464 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1465 here, for both the previous text and the following text.
1466 Meanwhile, GPT_ADDR does point to
1467 the text that has been stored by copy_text. */
1468 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1469 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1470 abort ();
1471 #endif
1473 if (! EQ (current_buffer->undo_list, Qt))
1475 record_delete (from, deletion);
1476 record_insert (from, inschars);
1479 GAP_SIZE -= outgoing_insbytes;
1480 GPT += inschars;
1481 ZV += inschars;
1482 Z += inschars;
1483 GPT_BYTE += outgoing_insbytes;
1484 ZV_BYTE += outgoing_insbytes;
1485 Z_BYTE += outgoing_insbytes;
1486 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1488 if (GPT_BYTE < GPT)
1489 abort ();
1491 /* Adjust the overlay center as needed. This must be done after
1492 adjusting the markers that bound the overlays. */
1493 adjust_overlays_for_delete (from, nchars_del);
1494 adjust_overlays_for_insert (from, inschars);
1495 if (markers)
1496 adjust_markers_for_insert (from, from_byte,
1497 from + inschars, from_byte + outgoing_insbytes,
1500 offset_intervals (current_buffer, from, inschars - nchars_del);
1502 /* Get the intervals for the part of the string we are inserting--
1503 not including the combined-before bytes. */
1504 intervals = XSTRING (new)->intervals;
1505 /* Insert those intervals. */
1506 graft_intervals_into_buffer (intervals, from, inschars,
1507 current_buffer, inherit);
1509 /* Relocate point as if it were a marker. */
1510 if (from < PT)
1511 adjust_point ((from + inschars - (PT < to ? PT : to)),
1512 (from_byte + outgoing_insbytes
1513 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1515 if (outgoing_insbytes == 0)
1516 evaporate_overlays (from);
1518 CHECK_MARKERS ();
1520 MODIFF++;
1521 UNGCPRO;
1523 signal_after_change (from, nchars_del, GPT - from);
1524 update_compositions (from, GPT, CHECK_BORDER);
1527 /* Delete characters in current buffer
1528 from FROM up to (but not including) TO.
1529 If TO comes before FROM, we delete nothing. */
1531 void
1532 del_range (from, to)
1533 register int from, to;
1535 del_range_1 (from, to, 1, 0);
1538 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1539 RET_STRING says to return the deleted text. */
1541 Lisp_Object
1542 del_range_1 (from, to, prepare, ret_string)
1543 int from, to, prepare, ret_string;
1545 int from_byte, to_byte;
1546 Lisp_Object deletion;
1547 struct gcpro gcpro1;
1549 /* Make args be valid */
1550 if (from < BEGV)
1551 from = BEGV;
1552 if (to > ZV)
1553 to = ZV;
1555 if (to <= from)
1556 return Qnil;
1558 if (prepare)
1560 int range_length = to - from;
1561 prepare_to_modify_buffer (from, to, &from);
1562 to = min (ZV, from + range_length);
1565 from_byte = CHAR_TO_BYTE (from);
1566 to_byte = CHAR_TO_BYTE (to);
1568 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1569 GCPRO1(deletion);
1570 signal_after_change (from, to - from, 0);
1571 update_compositions (from, from, CHECK_HEAD);
1572 UNGCPRO;
1573 return deletion;
1576 /* Like del_range_1 but args are byte positions, not char positions. */
1578 void
1579 del_range_byte (from_byte, to_byte, prepare)
1580 int from_byte, to_byte, prepare;
1582 int from, to;
1584 /* Make args be valid */
1585 if (from_byte < BEGV_BYTE)
1586 from_byte = BEGV_BYTE;
1587 if (to_byte > ZV_BYTE)
1588 to_byte = ZV_BYTE;
1590 if (to_byte <= from_byte)
1591 return;
1593 from = BYTE_TO_CHAR (from_byte);
1594 to = BYTE_TO_CHAR (to_byte);
1596 if (prepare)
1598 int old_from = from, old_to = Z - to;
1599 int range_length = to - from;
1600 prepare_to_modify_buffer (from, to, &from);
1601 to = from + range_length;
1603 if (old_from != from)
1604 from_byte = CHAR_TO_BYTE (from);
1605 if (to > ZV)
1607 to = ZV;
1608 to_byte = ZV_BYTE;
1610 else if (old_to == Z - to)
1611 to_byte = CHAR_TO_BYTE (to);
1614 del_range_2 (from, from_byte, to, to_byte, 0);
1615 signal_after_change (from, to - from, 0);
1616 update_compositions (from, from, CHECK_HEAD);
1619 /* Like del_range_1, but positions are specified both as charpos
1620 and bytepos. */
1622 void
1623 del_range_both (from, from_byte, to, to_byte, prepare)
1624 int from, from_byte, to, to_byte, prepare;
1626 /* Make args be valid */
1627 if (from_byte < BEGV_BYTE)
1628 from_byte = BEGV_BYTE;
1629 if (to_byte > ZV_BYTE)
1630 to_byte = ZV_BYTE;
1632 if (to_byte <= from_byte)
1633 return;
1635 if (from < BEGV)
1636 from = BEGV;
1637 if (to > ZV)
1638 to = ZV;
1640 if (prepare)
1642 int old_from = from, old_to = Z - to;
1643 int range_length = to - from;
1644 prepare_to_modify_buffer (from, to, &from);
1645 to = from + range_length;
1647 if (old_from != from)
1648 from_byte = CHAR_TO_BYTE (from);
1649 if (to > ZV)
1651 to = ZV;
1652 to_byte = ZV_BYTE;
1654 else if (old_to == Z - to)
1655 to_byte = CHAR_TO_BYTE (to);
1658 del_range_2 (from, from_byte, to, to_byte, 0);
1659 signal_after_change (from, to - from, 0);
1660 update_compositions (from, from, CHECK_HEAD);
1663 /* Delete a range of text, specified both as character positions
1664 and byte positions. FROM and TO are character positions,
1665 while FROM_BYTE and TO_BYTE are byte positions.
1666 If RET_STRING is true, the deleted area is returned as a string. */
1668 Lisp_Object
1669 del_range_2 (from, from_byte, to, to_byte, ret_string)
1670 int from, from_byte, to, to_byte, ret_string;
1672 register int nbytes_del, nchars_del;
1673 Lisp_Object deletion;
1675 CHECK_MARKERS ();
1677 nchars_del = to - from;
1678 nbytes_del = to_byte - from_byte;
1680 /* Make sure the gap is somewhere in or next to what we are deleting. */
1681 if (from > GPT)
1682 gap_right (from, from_byte);
1683 if (to < GPT)
1684 gap_left (to, to_byte, 0);
1686 #ifdef BYTE_COMBINING_DEBUG
1687 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1688 Z_BYTE - to_byte, from, from_byte))
1689 abort ();
1690 #endif
1692 if (ret_string || ! EQ (current_buffer->undo_list, Qt))
1693 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1694 else
1695 deletion = Qnil;
1697 /* Relocate all markers pointing into the new, larger gap
1698 to point at the end of the text before the gap.
1699 Do this before recording the deletion,
1700 so that undo handles this after reinserting the text. */
1701 adjust_markers_for_delete (from, from_byte, to, to_byte);
1703 if (! EQ (current_buffer->undo_list, Qt))
1704 record_delete (from, deletion);
1705 MODIFF++;
1707 /* Relocate point as if it were a marker. */
1708 if (from < PT)
1709 adjust_point (from - (PT < to ? PT : to),
1710 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1712 offset_intervals (current_buffer, from, - nchars_del);
1714 /* Adjust the overlay center as needed. This must be done after
1715 adjusting the markers that bound the overlays. */
1716 adjust_overlays_for_delete (from, nchars_del);
1718 GAP_SIZE += nbytes_del;
1719 ZV_BYTE -= nbytes_del;
1720 Z_BYTE -= nbytes_del;
1721 ZV -= nchars_del;
1722 Z -= nchars_del;
1723 GPT = from;
1724 GPT_BYTE = from_byte;
1725 *(GPT_ADDR) = 0; /* Put an anchor. */
1727 if (GPT_BYTE < GPT)
1728 abort ();
1730 if (GPT - BEG < BEG_UNCHANGED)
1731 BEG_UNCHANGED = GPT - BEG;
1732 if (Z - GPT < END_UNCHANGED)
1733 END_UNCHANGED = Z - GPT;
1735 CHECK_MARKERS ();
1737 evaporate_overlays (from);
1739 return deletion;
1742 /* Call this if you're about to change the region of BUFFER from
1743 character positions START to END. This checks the read-only
1744 properties of the region, calls the necessary modification hooks,
1745 and warns the next redisplay that it should pay attention to that
1746 area. */
1748 void
1749 modify_region (buffer, start, end)
1750 struct buffer *buffer;
1751 int start, end;
1753 struct buffer *old_buffer = current_buffer;
1755 if (buffer != old_buffer)
1756 set_buffer_internal (buffer);
1758 prepare_to_modify_buffer (start, end, NULL);
1760 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
1762 if (MODIFF <= SAVE_MODIFF)
1763 record_first_change ();
1764 MODIFF++;
1766 buffer->point_before_scroll = Qnil;
1768 if (buffer != old_buffer)
1769 set_buffer_internal (old_buffer);
1772 /* Check that it is okay to modify the buffer between START and END,
1773 which are char positions.
1775 Run the before-change-function, if any. If intervals are in use,
1776 verify that the text to be modified is not read-only, and call
1777 any modification properties the text may have.
1779 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1780 by holding its value temporarily in a marker. */
1782 void
1783 prepare_to_modify_buffer (start, end, preserve_ptr)
1784 int start, end;
1785 int *preserve_ptr;
1787 if (!NILP (current_buffer->read_only))
1788 Fbarf_if_buffer_read_only ();
1790 /* Let redisplay consider other windows than selected_window
1791 if modifying another buffer. */
1792 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1793 ++windows_or_buffers_changed;
1795 if (BUF_INTERVALS (current_buffer) != 0)
1797 if (preserve_ptr)
1799 Lisp_Object preserve_marker;
1800 struct gcpro gcpro1;
1801 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1802 GCPRO1 (preserve_marker);
1803 verify_interval_modification (current_buffer, start, end);
1804 *preserve_ptr = marker_position (preserve_marker);
1805 unchain_marker (preserve_marker);
1806 UNGCPRO;
1808 else
1809 verify_interval_modification (current_buffer, start, end);
1812 #ifdef CLASH_DETECTION
1813 if (!NILP (current_buffer->file_truename)
1814 /* Make binding buffer-file-name to nil effective. */
1815 && !NILP (current_buffer->filename)
1816 && SAVE_MODIFF >= MODIFF)
1817 lock_file (current_buffer->file_truename);
1818 #else
1819 /* At least warn if this file has changed on disk since it was visited. */
1820 if (!NILP (current_buffer->filename)
1821 && SAVE_MODIFF >= MODIFF
1822 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1823 && !NILP (Ffile_exists_p (current_buffer->filename)))
1824 call1 (intern ("ask-user-about-supersession-threat"),
1825 current_buffer->filename);
1826 #endif /* not CLASH_DETECTION */
1828 signal_before_change (start, end, preserve_ptr);
1830 if (current_buffer->newline_cache)
1831 invalidate_region_cache (current_buffer,
1832 current_buffer->newline_cache,
1833 start - BEG, Z - end);
1834 if (current_buffer->width_run_cache)
1835 invalidate_region_cache (current_buffer,
1836 current_buffer->width_run_cache,
1837 start - BEG, Z - end);
1839 Vdeactivate_mark = Qt;
1842 /* These macros work with an argument named `preserve_ptr'
1843 and a local variable named `preserve_marker'. */
1845 #define PRESERVE_VALUE \
1846 if (preserve_ptr && NILP (preserve_marker)) \
1847 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1849 #define RESTORE_VALUE \
1850 if (! NILP (preserve_marker)) \
1852 *preserve_ptr = marker_position (preserve_marker); \
1853 unchain_marker (preserve_marker); \
1856 #define PRESERVE_START_END \
1857 if (NILP (start_marker)) \
1858 start_marker = Fcopy_marker (start, Qnil); \
1859 if (NILP (end_marker)) \
1860 end_marker = Fcopy_marker (end, Qnil);
1862 #define FETCH_START \
1863 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1865 #define FETCH_END \
1866 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1868 /* Signal a change to the buffer immediately before it happens.
1869 START_INT and END_INT are the bounds of the text to be changed.
1871 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1872 by holding its value temporarily in a marker. */
1874 void
1875 signal_before_change (start_int, end_int, preserve_ptr)
1876 int start_int, end_int;
1877 int *preserve_ptr;
1879 Lisp_Object start, end;
1880 Lisp_Object start_marker, end_marker;
1881 Lisp_Object preserve_marker;
1882 struct gcpro gcpro1, gcpro2, gcpro3;
1884 if (inhibit_modification_hooks)
1885 return;
1887 start = make_number (start_int);
1888 end = make_number (end_int);
1889 preserve_marker = Qnil;
1890 start_marker = Qnil;
1891 end_marker = Qnil;
1892 GCPRO3 (preserve_marker, start_marker, end_marker);
1894 /* If buffer is unmodified, run a special hook for that case. */
1895 if (SAVE_MODIFF >= MODIFF
1896 && !NILP (Vfirst_change_hook)
1897 && !NILP (Vrun_hooks))
1899 PRESERVE_VALUE;
1900 PRESERVE_START_END;
1901 call1 (Vrun_hooks, Qfirst_change_hook);
1904 /* Now run the before-change-functions if any. */
1905 if (!NILP (Vbefore_change_functions))
1907 Lisp_Object args[3];
1908 Lisp_Object before_change_functions;
1909 Lisp_Object after_change_functions;
1910 struct gcpro gcpro1, gcpro2;
1911 struct buffer *old = current_buffer;
1912 struct buffer *new;
1914 PRESERVE_VALUE;
1915 PRESERVE_START_END;
1917 /* "Bind" before-change-functions and after-change-functions
1918 to nil--but in a way that errors don't know about.
1919 That way, if there's an error in them, they will stay nil. */
1920 before_change_functions = Vbefore_change_functions;
1921 after_change_functions = Vafter_change_functions;
1922 Vbefore_change_functions = Qnil;
1923 Vafter_change_functions = Qnil;
1924 GCPRO2 (before_change_functions, after_change_functions);
1926 /* Actually run the hook functions. */
1927 args[0] = Qbefore_change_functions;
1928 args[1] = FETCH_START;
1929 args[2] = FETCH_END;
1930 run_hook_list_with_args (before_change_functions, 3, args);
1932 /* "Unbind" the variables we "bound" to nil. Beware a
1933 buffer-local hook which changes the buffer when run (e.g. W3). */
1934 if (old != current_buffer)
1936 new = current_buffer;
1937 set_buffer_internal (old);
1938 Vbefore_change_functions = before_change_functions;
1939 Vafter_change_functions = after_change_functions;
1940 set_buffer_internal (new);
1942 else
1944 Vbefore_change_functions = before_change_functions;
1945 Vafter_change_functions = after_change_functions;
1947 UNGCPRO;
1950 if (!NILP (current_buffer->overlays_before)
1951 || !NILP (current_buffer->overlays_after))
1953 PRESERVE_VALUE;
1954 report_overlay_modification (FETCH_START, FETCH_END, 0,
1955 FETCH_START, FETCH_END, Qnil);
1958 if (! NILP (start_marker))
1959 free_marker (start_marker);
1960 if (! NILP (end_marker))
1961 free_marker (end_marker);
1962 RESTORE_VALUE;
1963 UNGCPRO;
1966 /* Signal a change immediately after it happens.
1967 CHARPOS is the character position of the start of the changed text.
1968 LENDEL is the number of characters of the text before the change.
1969 (Not the whole buffer; just the part that was changed.)
1970 LENINS is the number of characters in that part of the text
1971 after the change. */
1973 void
1974 signal_after_change (charpos, lendel, lenins)
1975 int charpos, lendel, lenins;
1977 if (inhibit_modification_hooks)
1978 return;
1980 /* If we are deferring calls to the after-change functions
1981 and there are no before-change functions,
1982 just record the args that we were going to use. */
1983 if (! NILP (Vcombine_after_change_calls)
1984 && NILP (Vbefore_change_functions)
1985 && NILP (current_buffer->overlays_before)
1986 && NILP (current_buffer->overlays_after))
1988 Lisp_Object elt;
1990 if (!NILP (combine_after_change_list)
1991 && current_buffer != XBUFFER (combine_after_change_buffer))
1992 Fcombine_after_change_execute ();
1994 elt = Fcons (make_number (charpos - BEG),
1995 Fcons (make_number (Z - (charpos - lendel + lenins)),
1996 Fcons (make_number (lenins - lendel), Qnil)));
1997 combine_after_change_list
1998 = Fcons (elt, combine_after_change_list);
1999 combine_after_change_buffer = Fcurrent_buffer ();
2001 return;
2004 if (!NILP (combine_after_change_list))
2005 Fcombine_after_change_execute ();
2007 if (!NILP (Vafter_change_functions))
2009 Lisp_Object args[4];
2010 Lisp_Object before_change_functions;
2011 Lisp_Object after_change_functions;
2012 struct buffer *old = current_buffer;
2013 struct buffer *new;
2014 struct gcpro gcpro1, gcpro2;
2016 /* "Bind" before-change-functions and after-change-functions
2017 to nil--but in a way that errors don't know about.
2018 That way, if there's an error in them, they will stay nil. */
2019 before_change_functions = Vbefore_change_functions;
2020 after_change_functions = Vafter_change_functions;
2021 Vbefore_change_functions = Qnil;
2022 Vafter_change_functions = Qnil;
2023 GCPRO2 (before_change_functions, after_change_functions);
2025 /* Actually run the hook functions. */
2026 args[0] = Qafter_change_functions;
2027 XSETFASTINT (args[1], charpos);
2028 XSETFASTINT (args[2], charpos + lenins);
2029 XSETFASTINT (args[3], lendel);
2030 run_hook_list_with_args (after_change_functions,
2031 4, args);
2033 /* "Unbind" the variables we "bound" to nil. Beware a
2034 buffer-local hook which changes the buffer when run (e.g. W3). */
2035 if (old != current_buffer)
2037 new = current_buffer;
2038 set_buffer_internal (old);
2039 Vbefore_change_functions = before_change_functions;
2040 Vafter_change_functions = after_change_functions;
2041 set_buffer_internal (new);
2043 else
2045 Vbefore_change_functions = before_change_functions;
2046 Vafter_change_functions = after_change_functions;
2048 UNGCPRO;
2051 if (!NILP (current_buffer->overlays_before)
2052 || !NILP (current_buffer->overlays_after))
2053 report_overlay_modification (make_number (charpos),
2054 make_number (charpos + lenins),
2056 make_number (charpos),
2057 make_number (charpos + lenins),
2058 make_number (lendel));
2060 /* After an insertion, call the text properties
2061 insert-behind-hooks or insert-in-front-hooks. */
2062 if (lendel == 0)
2063 report_interval_modification (make_number (charpos),
2064 make_number (charpos + lenins));
2067 Lisp_Object
2068 Fcombine_after_change_execute_1 (val)
2069 Lisp_Object val;
2071 Vcombine_after_change_calls = val;
2072 return val;
2075 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2076 Scombine_after_change_execute, 0, 0, 0,
2077 "This function is for use internally in `combine-after-change-calls'.")
2080 int count = specpdl_ptr - specpdl;
2081 int beg, end, change;
2082 int begpos, endpos;
2083 Lisp_Object tail;
2085 if (NILP (combine_after_change_list))
2086 return Qnil;
2088 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2090 Fset_buffer (combine_after_change_buffer);
2092 /* # chars unchanged at beginning of buffer. */
2093 beg = Z - BEG;
2094 /* # chars unchanged at end of buffer. */
2095 end = beg;
2096 /* Total amount of insertion (negative for deletion). */
2097 change = 0;
2099 /* Scan the various individual changes,
2100 accumulating the range info in BEG, END and CHANGE. */
2101 for (tail = combine_after_change_list; CONSP (tail);
2102 tail = XCDR (tail))
2104 Lisp_Object elt;
2105 int thisbeg, thisend, thischange;
2107 /* Extract the info from the next element. */
2108 elt = XCAR (tail);
2109 if (! CONSP (elt))
2110 continue;
2111 thisbeg = XINT (XCAR (elt));
2113 elt = XCDR (elt);
2114 if (! CONSP (elt))
2115 continue;
2116 thisend = XINT (XCAR (elt));
2118 elt = XCDR (elt);
2119 if (! CONSP (elt))
2120 continue;
2121 thischange = XINT (XCAR (elt));
2123 /* Merge this range into the accumulated range. */
2124 change += thischange;
2125 if (thisbeg < beg)
2126 beg = thisbeg;
2127 if (thisend < end)
2128 end = thisend;
2131 /* Get the current start and end positions of the range
2132 that was changed. */
2133 begpos = BEG + beg;
2134 endpos = Z - end;
2136 /* We are about to handle these, so discard them. */
2137 combine_after_change_list = Qnil;
2139 /* Now run the after-change functions for real.
2140 Turn off the flag that defers them. */
2141 record_unwind_protect (Fcombine_after_change_execute_1,
2142 Vcombine_after_change_calls);
2143 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2144 update_compositions (begpos, endpos, CHECK_ALL);
2146 return unbind_to (count, Qnil);
2149 void
2150 syms_of_insdel ()
2152 staticpro (&combine_after_change_list);
2153 combine_after_change_list = Qnil;
2154 combine_after_change_buffer = Qnil;
2156 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2157 "Non-nil means enable debugging checks for invalid marker positions.");
2158 check_markers_debug_flag = 0;
2159 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2160 "Used internally by the `combine-after-change-calls' macro.");
2161 Vcombine_after_change_calls = Qnil;
2163 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2164 "Non-nil means don't run any of the hooks that respond to buffer changes.\n\
2165 This affects `before-change-functions' and `after-change-functions',\n\
2166 as well as hooks attached to text properties and overlays.");
2167 inhibit_modification_hooks = 0;
2168 Qinhibit_modification_hooks = intern ("inhibit-modification-hooks");
2169 staticpro (&Qinhibit_modification_hooks);
2171 defsubr (&Scombine_after_change_execute);