Document changes made in 2010-10-13T14:50:06Z!lekktu@gmail.com.
[emacs.git] / src / insdel.c
blobabe6f3505852d7754e8bda1ca60ecceff3187aa6
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <setjmp.h>
24 #include "lisp.h"
25 #include "intervals.h"
26 #include "buffer.h"
27 #include "character.h"
28 #include "window.h"
29 #include "blockinput.h"
30 #include "region-cache.h"
32 #ifndef NULL
33 #define NULL 0
34 #endif
36 static void insert_from_string_1 (Lisp_Object string,
37 EMACS_INT pos, EMACS_INT pos_byte,
38 EMACS_INT nchars, EMACS_INT nbytes,
39 int inherit, int before_markers);
40 static void insert_from_buffer_1 (struct buffer *buf,
41 EMACS_INT from, EMACS_INT nchars,
42 int inherit);
43 static void gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap);
44 static void gap_right (EMACS_INT charpos, EMACS_INT bytepos);
45 static void adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to,
46 EMACS_INT amount);
47 static void adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
48 EMACS_INT to, EMACS_INT to_byte,
49 int before_markers);
50 static void adjust_markers_for_replace (EMACS_INT, EMACS_INT, EMACS_INT,
51 EMACS_INT, EMACS_INT, EMACS_INT);
52 static void adjust_point (EMACS_INT nchars, EMACS_INT nbytes);
54 Lisp_Object Fcombine_after_change_execute (void);
56 /* Non-nil means don't call the after-change-functions right away,
57 just record an element in combine_after_change_list. */
58 Lisp_Object Vcombine_after_change_calls;
60 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
61 describing changes which happened while combine_after_change_calls
62 was nonzero. We use this to decide how to call them
63 once the deferral ends.
65 In each element.
66 BEG-UNCHANGED is the number of chars before the changed range.
67 END-UNCHANGED is the number of chars after the changed range,
68 and CHANGE-AMOUNT is the number of characters inserted by the change
69 (negative for a deletion). */
70 Lisp_Object combine_after_change_list;
72 /* Buffer which combine_after_change_list is about. */
73 Lisp_Object combine_after_change_buffer;
75 Lisp_Object Qinhibit_modification_hooks;
77 extern Lisp_Object Vselect_active_regions, Vsaved_region_selection, Qonly;
80 /* Check all markers in the current buffer, looking for something invalid. */
82 static int check_markers_debug_flag;
84 #define CHECK_MARKERS() \
85 if (check_markers_debug_flag) \
86 check_markers (); \
87 else
89 void
90 check_markers (void)
92 register struct Lisp_Marker *tail;
93 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
95 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
97 if (tail->buffer->text != current_buffer->text)
98 abort ();
99 if (tail->charpos > Z)
100 abort ();
101 if (tail->bytepos > Z_BYTE)
102 abort ();
103 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
104 abort ();
108 /* Move gap to position CHARPOS.
109 Note that this can quit! */
111 void
112 move_gap (EMACS_INT charpos)
114 move_gap_both (charpos, charpos_to_bytepos (charpos));
117 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
118 Note that this can quit! */
120 void
121 move_gap_both (EMACS_INT charpos, EMACS_INT bytepos)
123 if (bytepos < GPT_BYTE)
124 gap_left (charpos, bytepos, 0);
125 else if (bytepos > GPT_BYTE)
126 gap_right (charpos, bytepos);
129 /* Move the gap to a position less than the current GPT.
130 BYTEPOS describes the new position as a byte position,
131 and CHARPOS is the corresponding char position.
132 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
134 static void
135 gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap)
137 register unsigned char *to, *from;
138 register EMACS_INT i;
139 EMACS_INT new_s1;
141 if (!newgap)
142 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
144 i = GPT_BYTE;
145 to = GAP_END_ADDR;
146 from = GPT_ADDR;
147 new_s1 = GPT_BYTE;
149 /* Now copy the characters. To move the gap down,
150 copy characters up. */
152 while (1)
154 /* I gets number of characters left to copy. */
155 i = new_s1 - bytepos;
156 if (i == 0)
157 break;
158 /* If a quit is requested, stop copying now.
159 Change BYTEPOS to be where we have actually moved the gap to. */
160 if (QUITP)
162 bytepos = new_s1;
163 charpos = BYTE_TO_CHAR (bytepos);
164 break;
166 /* Move at most 32000 chars before checking again for a quit. */
167 if (i > 32000)
168 i = 32000;
169 new_s1 -= i;
170 from -= i, to -= i;
171 memmove (to, from, i);
174 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
175 BYTEPOS is where the loop above stopped, which may be what was specified
176 or may be where a quit was detected. */
177 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
178 GPT_BYTE = bytepos;
179 GPT = charpos;
180 if (bytepos < charpos)
181 abort ();
182 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
183 QUIT;
186 /* Move the gap to a position greater than the current GPT.
187 BYTEPOS describes the new position as a byte position,
188 and CHARPOS is the corresponding char position. */
190 static void
191 gap_right (EMACS_INT charpos, EMACS_INT bytepos)
193 register unsigned char *to, *from;
194 register EMACS_INT i;
195 EMACS_INT new_s1;
197 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
199 i = GPT_BYTE;
200 from = GAP_END_ADDR;
201 to = GPT_ADDR;
202 new_s1 = GPT_BYTE;
204 /* Now copy the characters. To move the gap up,
205 copy characters down. */
207 while (1)
209 /* I gets number of characters left to copy. */
210 i = bytepos - new_s1;
211 if (i == 0)
212 break;
213 /* If a quit is requested, stop copying now.
214 Change BYTEPOS to be where we have actually moved the gap to. */
215 if (QUITP)
217 bytepos = new_s1;
218 charpos = BYTE_TO_CHAR (bytepos);
219 break;
221 /* Move at most 32000 chars before checking again for a quit. */
222 if (i > 32000)
223 i = 32000;
224 new_s1 += i;
225 memmove (to, from, i);
226 from += i, to += i;
229 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
230 - GAP_SIZE);
231 GPT = charpos;
232 GPT_BYTE = bytepos;
233 if (bytepos < charpos)
234 abort ();
235 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
236 QUIT;
239 /* Add AMOUNT to the byte position of every marker in the current buffer
240 whose current byte position is between FROM (exclusive) and TO (inclusive).
242 Also, any markers past the outside of that interval, in the direction
243 of adjustment, are first moved back to the near end of the interval
244 and then adjusted by AMOUNT.
246 When the latter adjustment is done, if AMOUNT is negative,
247 we record the adjustment for undo. (This case happens only for
248 deletion.)
250 The markers' character positions are not altered,
251 because gap motion does not affect character positions. */
253 int adjust_markers_test;
255 static void
256 adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to, EMACS_INT amount)
258 /* Now that a marker has a bytepos, not counting the gap,
259 nothing needs to be done here. */
260 #if 0
261 Lisp_Object marker;
262 register struct Lisp_Marker *m;
263 register EMACS_INT mpos;
265 marker = BUF_MARKERS (current_buffer);
267 while (!NILP (marker))
269 m = XMARKER (marker);
270 mpos = m->bytepos;
271 if (amount > 0)
273 if (mpos > to && mpos < to + amount)
275 if (adjust_markers_test)
276 abort ();
277 mpos = to + amount;
280 else
282 /* Here's the case where a marker is inside text being deleted.
283 AMOUNT can be negative for gap motion, too,
284 but then this range contains no markers. */
285 if (mpos > from + amount && mpos <= from)
287 if (adjust_markers_test)
288 abort ();
289 mpos = from + amount;
292 if (mpos > from && mpos <= to)
293 mpos += amount;
294 m->bufpos = mpos;
295 marker = m->chain;
297 #endif
300 /* Adjust all markers for a deletion
301 whose range in bytes is FROM_BYTE to TO_BYTE.
302 The range in charpos is FROM to TO.
304 This function assumes that the gap is adjacent to
305 or inside of the range being deleted. */
307 void
308 adjust_markers_for_delete (EMACS_INT from, EMACS_INT from_byte,
309 EMACS_INT to, EMACS_INT to_byte)
311 Lisp_Object marker;
312 register struct Lisp_Marker *m;
313 register EMACS_INT charpos;
315 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
317 charpos = m->charpos;
319 if (charpos > Z)
320 abort ();
322 /* If the marker is after the deletion,
323 relocate by number of chars / bytes deleted. */
324 if (charpos > to)
326 m->charpos -= to - from;
327 m->bytepos -= to_byte - from_byte;
329 /* Here's the case where a marker is inside text being deleted. */
330 else if (charpos > from)
332 if (! m->insertion_type)
333 { /* Normal markers will end up at the beginning of the
334 re-inserted text after undoing a deletion, and must be
335 adjusted to move them to the correct place. */
336 XSETMISC (marker, m);
337 record_marker_adjustment (marker, from - charpos);
339 else if (charpos < to)
340 { /* Before-insertion markers will automatically move forward
341 upon re-inserting the deleted text, so we have to arrange
342 for them to move backward to the correct position. */
343 XSETMISC (marker, m);
344 record_marker_adjustment (marker, to - charpos);
346 m->charpos = from;
347 m->bytepos = from_byte;
349 /* Here's the case where a before-insertion marker is immediately
350 before the deleted region. */
351 else if (charpos == from && m->insertion_type)
353 /* Undoing the change uses normal insertion, which will
354 incorrectly make MARKER move forward, so we arrange for it
355 to then move backward to the correct place at the beginning
356 of the deleted region. */
357 XSETMISC (marker, m);
358 record_marker_adjustment (marker, to - from);
364 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
365 to TO / TO_BYTE. We have to relocate the charpos of every marker
366 that points after the insertion (but not their bytepos).
368 When a marker points at the insertion point,
369 we advance it if either its insertion-type is t
370 or BEFORE_MARKERS is true. */
372 static void
373 adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
374 EMACS_INT to, EMACS_INT to_byte, int before_markers)
376 struct Lisp_Marker *m;
377 int adjusted = 0;
378 EMACS_INT nchars = to - from;
379 EMACS_INT nbytes = to_byte - from_byte;
381 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
383 eassert (m->bytepos >= m->charpos
384 && m->bytepos - m->charpos <= Z_BYTE - Z);
386 if (m->bytepos == from_byte)
388 if (m->insertion_type || before_markers)
390 m->bytepos = to_byte;
391 m->charpos = to;
392 if (m->insertion_type)
393 adjusted = 1;
396 else if (m->bytepos > from_byte)
398 m->bytepos += nbytes;
399 m->charpos += nchars;
403 /* Adjusting only markers whose insertion-type is t may result in
404 - disordered start and end in overlays, and
405 - disordered overlays in the slot `overlays_before' of current_buffer. */
406 if (adjusted)
408 fix_start_end_in_overlays(from, to);
409 fix_overlays_before (current_buffer, from, to);
413 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
415 This is used only when the value of point changes due to an insert
416 or delete; it does not represent a conceptual change in point as a
417 marker. In particular, point is not crossing any interval
418 boundaries, so there's no need to use the usual SET_PT macro. In
419 fact it would be incorrect to do so, because either the old or the
420 new value of point is out of sync with the current set of
421 intervals. */
423 static void
424 adjust_point (EMACS_INT nchars, EMACS_INT nbytes)
426 BUF_PT (current_buffer) += nchars;
427 BUF_PT_BYTE (current_buffer) += nbytes;
429 /* In a single-byte buffer, the two positions must be equal. */
430 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
433 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
434 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
435 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
436 an insertion. */
438 static void
439 adjust_markers_for_replace (EMACS_INT from, EMACS_INT from_byte,
440 EMACS_INT old_chars, EMACS_INT old_bytes,
441 EMACS_INT new_chars, EMACS_INT new_bytes)
443 register struct Lisp_Marker *m;
444 EMACS_INT prev_to_byte = from_byte + old_bytes;
445 EMACS_INT diff_chars = new_chars - old_chars;
446 EMACS_INT diff_bytes = new_bytes - old_bytes;
448 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
450 if (m->bytepos >= prev_to_byte)
452 m->charpos += diff_chars;
453 m->bytepos += diff_bytes;
455 else if (m->bytepos > from_byte)
457 m->charpos = from;
458 m->bytepos = from_byte;
462 CHECK_MARKERS ();
466 /* Make the gap NBYTES_ADDED bytes longer. */
468 void
469 make_gap_larger (EMACS_INT nbytes_added)
471 Lisp_Object tem;
472 EMACS_INT real_gap_loc;
473 EMACS_INT real_gap_loc_byte;
474 EMACS_INT old_gap_size;
476 /* If we have to get more space, get enough to last a while. */
477 nbytes_added += 2000;
479 { EMACS_INT total_size = Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added;
480 if (total_size < 0
481 /* Don't allow a buffer size that won't fit in a Lisp integer. */
482 || total_size != XINT (make_number (total_size))
483 /* Don't allow a buffer size that won't fit in an int
484 even if it will fit in a Lisp integer.
485 That won't work because so many places still use `int'. */
486 || total_size != (EMACS_INT) (int) total_size)
487 error ("Buffer exceeds maximum size");
490 enlarge_buffer_text (current_buffer, nbytes_added);
492 /* Prevent quitting in move_gap. */
493 tem = Vinhibit_quit;
494 Vinhibit_quit = Qt;
496 real_gap_loc = GPT;
497 real_gap_loc_byte = GPT_BYTE;
498 old_gap_size = GAP_SIZE;
500 /* Call the newly allocated space a gap at the end of the whole space. */
501 GPT = Z + GAP_SIZE;
502 GPT_BYTE = Z_BYTE + GAP_SIZE;
503 GAP_SIZE = nbytes_added;
505 /* Move the new gap down to be consecutive with the end of the old one.
506 This adjusts the markers properly too. */
507 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
509 /* Now combine the two into one large gap. */
510 GAP_SIZE += old_gap_size;
511 GPT = real_gap_loc;
512 GPT_BYTE = real_gap_loc_byte;
514 /* Put an anchor. */
515 *(Z_ADDR) = 0;
517 Vinhibit_quit = tem;
521 /* Make the gap NBYTES_REMOVED bytes shorter. */
523 void
524 make_gap_smaller (EMACS_INT nbytes_removed)
526 Lisp_Object tem;
527 EMACS_INT real_gap_loc;
528 EMACS_INT real_gap_loc_byte;
529 EMACS_INT real_Z;
530 EMACS_INT real_Z_byte;
531 EMACS_INT real_beg_unchanged;
532 EMACS_INT new_gap_size;
534 /* Make sure the gap is at least 20 bytes. */
535 if (GAP_SIZE - nbytes_removed < 20)
536 nbytes_removed = GAP_SIZE - 20;
538 /* Prevent quitting in move_gap. */
539 tem = Vinhibit_quit;
540 Vinhibit_quit = Qt;
542 real_gap_loc = GPT;
543 real_gap_loc_byte = GPT_BYTE;
544 new_gap_size = GAP_SIZE - nbytes_removed;
545 real_Z = Z;
546 real_Z_byte = Z_BYTE;
547 real_beg_unchanged = BEG_UNCHANGED;
549 /* Pretend that the last unwanted part of the gap is the entire gap,
550 and that the first desired part of the gap is part of the buffer
551 text. */
552 memset (GPT_ADDR, 0, new_gap_size);
553 GPT += new_gap_size;
554 GPT_BYTE += new_gap_size;
555 Z += new_gap_size;
556 Z_BYTE += new_gap_size;
557 GAP_SIZE = nbytes_removed;
559 /* Move the unwanted pretend gap to the end of the buffer. This
560 adjusts the markers properly too. */
561 gap_right (Z, Z_BYTE);
563 enlarge_buffer_text (current_buffer, -nbytes_removed);
565 /* Now restore the desired gap. */
566 GAP_SIZE = new_gap_size;
567 GPT = real_gap_loc;
568 GPT_BYTE = real_gap_loc_byte;
569 Z = real_Z;
570 Z_BYTE = real_Z_byte;
571 BEG_UNCHANGED = real_beg_unchanged;
573 /* Put an anchor. */
574 *(Z_ADDR) = 0;
576 Vinhibit_quit = tem;
579 void
580 make_gap (EMACS_INT nbytes_added)
582 if (nbytes_added >= 0)
583 make_gap_larger (nbytes_added);
584 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
585 else
586 make_gap_smaller (-nbytes_added);
587 #endif
590 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
591 FROM_MULTIBYTE says whether the incoming text is multibyte.
592 TO_MULTIBYTE says whether to store the text as multibyte.
593 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
595 Return the number of bytes stored at TO_ADDR. */
597 EMACS_INT
598 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
599 EMACS_INT nbytes, int from_multibyte, int to_multibyte)
601 if (from_multibyte == to_multibyte)
603 memcpy (to_addr, from_addr, nbytes);
604 return nbytes;
606 else if (from_multibyte)
608 EMACS_INT nchars = 0;
609 EMACS_INT bytes_left = nbytes;
610 Lisp_Object tbl = Qnil;
612 while (bytes_left > 0)
614 int thislen, c;
615 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
616 if (! ASCII_CHAR_P (c))
617 c &= 0xFF;
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 (!ASCII_CHAR_P (c))
636 c = BYTE8_TO_CHAR (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. */
652 EMACS_INT
653 count_size_as_multibyte (const unsigned char *ptr, EMACS_INT nbytes)
655 EMACS_INT i;
656 EMACS_INT outgoing_nbytes = 0;
658 for (i = 0; i < nbytes; i++)
660 unsigned int c = *ptr++;
662 if (ASCII_CHAR_P (c))
663 outgoing_nbytes++;
664 else
666 c = BYTE8_TO_CHAR (c);
667 outgoing_nbytes += CHAR_BYTES (c);
671 return outgoing_nbytes;
674 /* Insert a string of specified length before point.
675 This function judges multibyteness based on
676 enable_multibyte_characters in the current buffer;
677 it never converts between single-byte and multibyte.
679 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
680 prepare_to_modify_buffer could relocate the text. */
682 void
683 insert (const unsigned char *string, EMACS_INT nbytes)
685 if (nbytes > 0)
687 EMACS_INT len = chars_in_text (string, nbytes), opoint;
688 insert_1_both (string, len, nbytes, 0, 1, 0);
689 opoint = PT - len;
690 signal_after_change (opoint, 0, len);
691 update_compositions (opoint, PT, CHECK_BORDER);
695 /* Likewise, but inherit text properties from neighboring characters. */
697 void
698 insert_and_inherit (const unsigned char *string, EMACS_INT nbytes)
700 if (nbytes > 0)
702 EMACS_INT len = chars_in_text (string, nbytes), opoint;
703 insert_1_both (string, len, nbytes, 1, 1, 0);
704 opoint = PT - len;
705 signal_after_change (opoint, 0, len);
706 update_compositions (opoint, PT, CHECK_BORDER);
710 /* Insert the character C before point. Do not inherit text properties. */
712 void
713 insert_char (int c)
715 unsigned char str[MAX_MULTIBYTE_LENGTH];
716 int len;
718 if (! NILP (current_buffer->enable_multibyte_characters))
719 len = CHAR_STRING (c, str);
720 else
722 len = 1;
723 str[0] = c;
726 insert (str, len);
729 /* Insert the null-terminated string S before point. */
731 void
732 insert_string (const char *s)
734 insert (s, strlen (s));
737 /* Like `insert' except that all markers pointing at the place where
738 the insertion happens are adjusted to point after it.
739 Don't use this function to insert part of a Lisp string,
740 since gc could happen and relocate it. */
742 void
743 insert_before_markers (const unsigned char *string, EMACS_INT nbytes)
745 if (nbytes > 0)
747 EMACS_INT len = chars_in_text (string, nbytes), opoint;
748 insert_1_both (string, len, nbytes, 0, 1, 1);
749 opoint = PT - len;
750 signal_after_change (opoint, 0, len);
751 update_compositions (opoint, PT, CHECK_BORDER);
755 /* Likewise, but inherit text properties from neighboring characters. */
757 void
758 insert_before_markers_and_inherit (const unsigned char *string,
759 EMACS_INT nbytes)
761 if (nbytes > 0)
763 EMACS_INT len = chars_in_text (string, nbytes), opoint;
764 insert_1_both (string, len, nbytes, 1, 1, 1);
765 opoint = PT - len;
766 signal_after_change (opoint, 0, len);
767 update_compositions (opoint, PT, CHECK_BORDER);
771 /* Subroutine used by the insert functions above. */
773 void
774 insert_1 (const unsigned char *string, EMACS_INT nbytes,
775 int inherit, int prepare, int before_markers)
777 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
778 inherit, prepare, before_markers);
782 #ifdef BYTE_COMBINING_DEBUG
784 /* See if the bytes before POS/POS_BYTE combine with bytes
785 at the start of STRING to form a single character.
786 If so, return the number of bytes at the start of STRING
787 which combine in this way. Otherwise, return 0. */
790 count_combining_before (const unsigned char *string, EMACS_INT length,
791 EMACS_INT pos, EMACS_INT pos_byte)
793 int len, combining_bytes;
794 const unsigned char *p;
796 if (NILP (current_buffer->enable_multibyte_characters))
797 return 0;
799 /* At first, we can exclude the following cases:
800 (1) STRING[0] can't be a following byte of multibyte sequence.
801 (2) POS is the start of the current buffer.
802 (3) A character before POS is not a multibyte character. */
803 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
804 return 0;
805 if (pos_byte == BEG_BYTE) /* case (2) */
806 return 0;
807 len = 1;
808 p = BYTE_POS_ADDR (pos_byte - 1);
809 while (! CHAR_HEAD_P (*p)) p--, len++;
810 if (! LEADING_CODE_P (*p)) /* case (3) */
811 return 0;
813 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
814 if (combining_bytes <= 0)
815 /* The character preceding POS is, complete and no room for
816 combining bytes (combining_bytes == 0), or an independent 8-bit
817 character (combining_bytes < 0). */
818 return 0;
820 /* We have a combination situation. Count the bytes at STRING that
821 may combine. */
822 p = string + 1;
823 while (!CHAR_HEAD_P (*p) && p < string + length)
824 p++;
826 return (combining_bytes < p - string ? combining_bytes : p - string);
829 /* See if the bytes after POS/POS_BYTE combine with bytes
830 at the end of STRING to form a single character.
831 If so, return the number of bytes after POS/POS_BYTE
832 which combine in this way. Otherwise, return 0. */
835 count_combining_after (const unsigned char *string,
836 EMACS_INT length, EMACS_INT pos, EMACS_INT pos_byte)
838 EMACS_INT opos_byte = pos_byte;
839 EMACS_INT i;
840 EMACS_INT bytes;
841 unsigned char *bufp;
843 if (NILP (current_buffer->enable_multibyte_characters))
844 return 0;
846 /* At first, we can exclude the following cases:
847 (1) The last byte of STRING is an ASCII.
848 (2) POS is the last of the current buffer.
849 (3) A character at POS can't be a following byte of multibyte
850 character. */
851 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
852 return 0;
853 if (pos_byte == Z_BYTE) /* case (2) */
854 return 0;
855 bufp = BYTE_POS_ADDR (pos_byte);
856 if (CHAR_HEAD_P (*bufp)) /* case (3) */
857 return 0;
859 i = length - 1;
860 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
862 i--;
864 if (i < 0)
866 /* All characters in STRING are not character head. We must
867 check also preceding bytes at POS. We are sure that the gap
868 is at POS. */
869 unsigned char *p = BEG_ADDR;
870 i = pos_byte - 2;
871 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
872 i--;
873 if (i < 0 || !LEADING_CODE_P (p[i]))
874 return 0;
876 bytes = BYTES_BY_CHAR_HEAD (p[i]);
877 return (bytes <= pos_byte - 1 - i + length
879 : bytes - (pos_byte - 1 - i + length));
881 if (!LEADING_CODE_P (string[i]))
882 return 0;
884 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
885 bufp++, pos_byte++;
886 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
888 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
891 #endif
894 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
895 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
896 are the same as in insert_1. */
898 void
899 insert_1_both (const unsigned char *string,
900 EMACS_INT nchars, EMACS_INT nbytes,
901 int inherit, int prepare, int before_markers)
903 if (nchars == 0)
904 return;
906 if (NILP (current_buffer->enable_multibyte_characters))
907 nchars = nbytes;
909 if (prepare)
910 /* Do this before moving and increasing the gap,
911 because the before-change hooks might move the gap
912 or make it smaller. */
913 prepare_to_modify_buffer (PT, PT, NULL);
915 if (PT != GPT)
916 move_gap_both (PT, PT_BYTE);
917 if (GAP_SIZE < nbytes)
918 make_gap (nbytes - GAP_SIZE);
920 #ifdef BYTE_COMBINING_DEBUG
921 if (count_combining_before (string, nbytes, PT, PT_BYTE)
922 || count_combining_after (string, nbytes, PT, PT_BYTE))
923 abort ();
924 #endif
926 /* Record deletion of the surrounding text that combines with
927 the insertion. This, together with recording the insertion,
928 will add up to the right stuff in the undo list. */
929 record_insert (PT, nchars);
930 MODIFF++;
931 CHARS_MODIFF = MODIFF;
933 memcpy (GPT_ADDR, string, nbytes);
935 GAP_SIZE -= nbytes;
936 GPT += nchars;
937 ZV += nchars;
938 Z += nchars;
939 GPT_BYTE += nbytes;
940 ZV_BYTE += nbytes;
941 Z_BYTE += nbytes;
942 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
944 if (GPT_BYTE < GPT)
945 abort ();
947 /* The insert may have been in the unchanged region, so check again. */
948 if (Z - GPT < END_UNCHANGED)
949 END_UNCHANGED = Z - GPT;
951 adjust_overlays_for_insert (PT, nchars);
952 adjust_markers_for_insert (PT, PT_BYTE,
953 PT + nchars, PT_BYTE + nbytes,
954 before_markers);
956 if (BUF_INTERVALS (current_buffer) != 0)
957 offset_intervals (current_buffer, PT, nchars);
959 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
960 set_text_properties (make_number (PT), make_number (PT + nchars),
961 Qnil, Qnil, Qnil);
963 adjust_point (nchars, nbytes);
965 CHECK_MARKERS ();
968 /* Insert the part of the text of STRING, a Lisp object assumed to be
969 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
970 starting at position POS / POS_BYTE. If the text of STRING has properties,
971 copy them into the buffer.
973 It does not work to use `insert' for this, because a GC could happen
974 before we copy the stuff into the buffer, and relocate the string
975 without insert noticing. */
977 void
978 insert_from_string (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
979 EMACS_INT length, EMACS_INT length_byte, int inherit)
981 EMACS_INT opoint = PT;
983 if (SCHARS (string) == 0)
984 return;
986 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
987 inherit, 0);
988 signal_after_change (opoint, 0, PT - opoint);
989 update_compositions (opoint, PT, CHECK_BORDER);
992 /* Like `insert_from_string' except that all markers pointing
993 at the place where the insertion happens are adjusted to point after it. */
995 void
996 insert_from_string_before_markers (Lisp_Object string,
997 EMACS_INT pos, EMACS_INT pos_byte,
998 EMACS_INT length, EMACS_INT length_byte,
999 int inherit)
1001 EMACS_INT opoint = PT;
1003 if (SCHARS (string) == 0)
1004 return;
1006 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1007 inherit, 1);
1008 signal_after_change (opoint, 0, PT - opoint);
1009 update_compositions (opoint, PT, CHECK_BORDER);
1012 /* Subroutine of the insertion functions above. */
1014 static void
1015 insert_from_string_1 (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
1016 EMACS_INT nchars, EMACS_INT nbytes,
1017 int inherit, int before_markers)
1019 struct gcpro gcpro1;
1020 EMACS_INT outgoing_nbytes = nbytes;
1021 INTERVAL intervals;
1023 /* Make OUTGOING_NBYTES describe the text
1024 as it will be inserted in this buffer. */
1026 if (NILP (current_buffer->enable_multibyte_characters))
1027 outgoing_nbytes = nchars;
1028 else if (! STRING_MULTIBYTE (string))
1029 outgoing_nbytes
1030 = count_size_as_multibyte (SDATA (string) + pos_byte,
1031 nbytes);
1033 GCPRO1 (string);
1034 /* Do this before moving and increasing the gap,
1035 because the before-change hooks might move the gap
1036 or make it smaller. */
1037 prepare_to_modify_buffer (PT, PT, NULL);
1039 if (PT != GPT)
1040 move_gap_both (PT, PT_BYTE);
1041 if (GAP_SIZE < outgoing_nbytes)
1042 make_gap (outgoing_nbytes - GAP_SIZE);
1043 UNGCPRO;
1045 /* Copy the string text into the buffer, perhaps converting
1046 between single-byte and multibyte. */
1047 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
1048 STRING_MULTIBYTE (string),
1049 ! NILP (current_buffer->enable_multibyte_characters));
1051 #ifdef BYTE_COMBINING_DEBUG
1052 /* We have copied text into the gap, but we have not altered
1053 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1054 to these functions and get the same results as we would
1055 have got earlier on. Meanwhile, PT_ADDR does point to
1056 the text that has been stored by copy_text. */
1057 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1058 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1059 abort ();
1060 #endif
1062 record_insert (PT, nchars);
1063 MODIFF++;
1064 CHARS_MODIFF = MODIFF;
1066 GAP_SIZE -= outgoing_nbytes;
1067 GPT += nchars;
1068 ZV += nchars;
1069 Z += nchars;
1070 GPT_BYTE += outgoing_nbytes;
1071 ZV_BYTE += outgoing_nbytes;
1072 Z_BYTE += outgoing_nbytes;
1073 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1075 if (GPT_BYTE < GPT)
1076 abort ();
1078 /* The insert may have been in the unchanged region, so check again. */
1079 if (Z - GPT < END_UNCHANGED)
1080 END_UNCHANGED = Z - GPT;
1082 adjust_overlays_for_insert (PT, nchars);
1083 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1084 PT_BYTE + outgoing_nbytes,
1085 before_markers);
1087 offset_intervals (current_buffer, PT, nchars);
1089 intervals = STRING_INTERVALS (string);
1090 /* Get the intervals for the part of the string we are inserting. */
1091 if (nbytes < SBYTES (string))
1092 intervals = copy_intervals (intervals, pos, nchars);
1094 /* Insert those intervals. */
1095 graft_intervals_into_buffer (intervals, PT, nchars,
1096 current_buffer, inherit);
1098 adjust_point (nchars, outgoing_nbytes);
1100 CHECK_MARKERS ();
1103 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1104 starting at GPT_ADDR. */
1106 void
1107 insert_from_gap (EMACS_INT nchars, EMACS_INT nbytes)
1109 if (NILP (current_buffer->enable_multibyte_characters))
1110 nchars = nbytes;
1112 record_insert (GPT, nchars);
1113 MODIFF++;
1115 GAP_SIZE -= nbytes;
1116 GPT += nchars;
1117 ZV += nchars;
1118 Z += nchars;
1119 GPT_BYTE += nbytes;
1120 ZV_BYTE += nbytes;
1121 Z_BYTE += nbytes;
1122 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1124 if (GPT_BYTE < GPT)
1125 abort ();
1127 adjust_overlays_for_insert (GPT - nchars, nchars);
1128 adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
1129 GPT, GPT_BYTE, 0);
1131 if (BUF_INTERVALS (current_buffer) != 0)
1133 offset_intervals (current_buffer, GPT - nchars, nchars);
1134 graft_intervals_into_buffer (NULL_INTERVAL, GPT - nchars, nchars,
1135 current_buffer, 0);
1138 if (GPT - nchars < PT)
1139 adjust_point (nchars, nbytes);
1141 CHECK_MARKERS ();
1144 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1145 current buffer. If the text in BUF has properties, they are absorbed
1146 into the current buffer.
1148 It does not work to use `insert' for this, because a malloc could happen
1149 and relocate BUF's text before the copy happens. */
1151 void
1152 insert_from_buffer (struct buffer *buf,
1153 EMACS_INT charpos, EMACS_INT nchars, int inherit)
1155 EMACS_INT opoint = PT;
1157 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1158 signal_after_change (opoint, 0, PT - opoint);
1159 update_compositions (opoint, PT, CHECK_BORDER);
1162 static void
1163 insert_from_buffer_1 (struct buffer *buf,
1164 EMACS_INT from, EMACS_INT nchars, int inherit)
1166 register Lisp_Object temp;
1167 EMACS_INT chunk, chunk_expanded;
1168 EMACS_INT from_byte = buf_charpos_to_bytepos (buf, from);
1169 EMACS_INT to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1170 EMACS_INT incoming_nbytes = to_byte - from_byte;
1171 EMACS_INT outgoing_nbytes = incoming_nbytes;
1172 INTERVAL intervals;
1174 /* Make OUTGOING_NBYTES describe the text
1175 as it will be inserted in this buffer. */
1177 if (NILP (current_buffer->enable_multibyte_characters))
1178 outgoing_nbytes = nchars;
1179 else if (NILP (buf->enable_multibyte_characters))
1181 EMACS_INT outgoing_before_gap = 0;
1182 EMACS_INT outgoing_after_gap = 0;
1184 if (from < BUF_GPT (buf))
1186 chunk = BUF_GPT_BYTE (buf) - from_byte;
1187 if (chunk > incoming_nbytes)
1188 chunk = incoming_nbytes;
1189 outgoing_before_gap
1190 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1191 chunk);
1193 else
1194 chunk = 0;
1196 if (chunk < incoming_nbytes)
1197 outgoing_after_gap
1198 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1199 from_byte + chunk),
1200 incoming_nbytes - chunk);
1202 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1205 /* Make sure point-max won't overflow after this insertion. */
1206 XSETINT (temp, outgoing_nbytes + Z);
1207 if (outgoing_nbytes + Z != XINT (temp))
1208 error ("Maximum buffer size exceeded");
1210 /* Do this before moving and increasing the gap,
1211 because the before-change hooks might move the gap
1212 or make it smaller. */
1213 prepare_to_modify_buffer (PT, PT, NULL);
1215 if (PT != GPT)
1216 move_gap_both (PT, PT_BYTE);
1217 if (GAP_SIZE < outgoing_nbytes)
1218 make_gap (outgoing_nbytes - GAP_SIZE);
1220 if (from < BUF_GPT (buf))
1222 chunk = BUF_GPT_BYTE (buf) - from_byte;
1223 if (chunk > incoming_nbytes)
1224 chunk = incoming_nbytes;
1225 /* Record number of output bytes, so we know where
1226 to put the output from the second copy_text. */
1227 chunk_expanded
1228 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1229 GPT_ADDR, chunk,
1230 ! NILP (buf->enable_multibyte_characters),
1231 ! NILP (current_buffer->enable_multibyte_characters));
1233 else
1234 chunk_expanded = chunk = 0;
1236 if (chunk < incoming_nbytes)
1237 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1238 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1239 ! NILP (buf->enable_multibyte_characters),
1240 ! NILP (current_buffer->enable_multibyte_characters));
1242 #ifdef BYTE_COMBINING_DEBUG
1243 /* We have copied text into the gap, but we have not altered
1244 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1245 to these functions and get the same results as we would
1246 have got earlier on. Meanwhile, GPT_ADDR does point to
1247 the text that has been stored by copy_text. */
1248 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1249 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1250 abort ();
1251 #endif
1253 record_insert (PT, nchars);
1254 MODIFF++;
1255 CHARS_MODIFF = MODIFF;
1257 GAP_SIZE -= outgoing_nbytes;
1258 GPT += nchars;
1259 ZV += nchars;
1260 Z += nchars;
1261 GPT_BYTE += outgoing_nbytes;
1262 ZV_BYTE += outgoing_nbytes;
1263 Z_BYTE += outgoing_nbytes;
1264 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1266 if (GPT_BYTE < GPT)
1267 abort ();
1269 /* The insert may have been in the unchanged region, so check again. */
1270 if (Z - GPT < END_UNCHANGED)
1271 END_UNCHANGED = Z - GPT;
1273 adjust_overlays_for_insert (PT, nchars);
1274 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1275 PT_BYTE + outgoing_nbytes,
1278 if (BUF_INTERVALS (current_buffer) != 0)
1279 offset_intervals (current_buffer, PT, nchars);
1281 /* Get the intervals for the part of the string we are inserting. */
1282 intervals = BUF_INTERVALS (buf);
1283 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1285 if (buf == current_buffer && PT <= from)
1286 from += nchars;
1287 intervals = copy_intervals (intervals, from, nchars);
1290 /* Insert those intervals. */
1291 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1293 adjust_point (nchars, outgoing_nbytes);
1296 /* Record undo information and adjust markers and position keepers for
1297 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1298 chars (LEN_BYTE bytes) which resides in the gap just after
1299 GPT_ADDR.
1301 PREV_TEXT nil means the new text was just inserted. */
1303 void
1304 adjust_after_replace (EMACS_INT from, EMACS_INT from_byte,
1305 Lisp_Object prev_text, EMACS_INT len, EMACS_INT len_byte)
1307 EMACS_INT nchars_del = 0, nbytes_del = 0;
1309 #ifdef BYTE_COMBINING_DEBUG
1310 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1311 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1312 abort ();
1313 #endif
1315 if (STRINGP (prev_text))
1317 nchars_del = SCHARS (prev_text);
1318 nbytes_del = SBYTES (prev_text);
1321 /* Update various buffer positions for the new text. */
1322 GAP_SIZE -= len_byte;
1323 ZV += len; Z+= len;
1324 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1325 GPT += len; GPT_BYTE += len_byte;
1326 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1328 if (nchars_del > 0)
1329 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1330 len, len_byte);
1331 else
1332 adjust_markers_for_insert (from, from_byte,
1333 from + len, from_byte + len_byte, 0);
1335 if (! EQ (current_buffer->undo_list, Qt))
1337 if (nchars_del > 0)
1338 record_delete (from, prev_text);
1339 record_insert (from, len);
1342 if (len > nchars_del)
1343 adjust_overlays_for_insert (from, len - nchars_del);
1344 else if (len < nchars_del)
1345 adjust_overlays_for_delete (from, nchars_del - len);
1346 if (BUF_INTERVALS (current_buffer) != 0)
1348 offset_intervals (current_buffer, from, len - nchars_del);
1351 if (from < PT)
1352 adjust_point (len - nchars_del, len_byte - nbytes_del);
1354 /* As byte combining will decrease Z, we must check this again. */
1355 if (Z - GPT < END_UNCHANGED)
1356 END_UNCHANGED = Z - GPT;
1358 CHECK_MARKERS ();
1360 if (len == 0)
1361 evaporate_overlays (from);
1362 MODIFF++;
1363 CHARS_MODIFF = MODIFF;
1366 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1367 This is for use when undo is not enabled in the current buffer. */
1369 void
1370 adjust_after_replace_noundo (EMACS_INT from, EMACS_INT from_byte,
1371 EMACS_INT nchars_del, EMACS_INT nbytes_del,
1372 EMACS_INT len, EMACS_INT len_byte)
1374 #ifdef BYTE_COMBINING_DEBUG
1375 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1376 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1377 abort ();
1378 #endif
1380 /* Update various buffer positions for the new text. */
1381 GAP_SIZE -= len_byte;
1382 ZV += len; Z+= len;
1383 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1384 GPT += len; GPT_BYTE += len_byte;
1385 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1387 if (nchars_del > 0)
1388 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1389 len, len_byte);
1390 else
1391 adjust_markers_for_insert (from, from_byte,
1392 from + len, from_byte + len_byte, 0);
1394 if (len > nchars_del)
1395 adjust_overlays_for_insert (from, len - nchars_del);
1396 else if (len < nchars_del)
1397 adjust_overlays_for_delete (from, nchars_del - len);
1398 if (BUF_INTERVALS (current_buffer) != 0)
1400 offset_intervals (current_buffer, from, len - nchars_del);
1403 if (from < PT)
1404 adjust_point (len - nchars_del, len_byte - nbytes_del);
1406 /* As byte combining will decrease Z, we must check this again. */
1407 if (Z - GPT < END_UNCHANGED)
1408 END_UNCHANGED = Z - GPT;
1410 CHECK_MARKERS ();
1412 if (len == 0)
1413 evaporate_overlays (from);
1414 MODIFF++;
1415 CHARS_MODIFF = MODIFF;
1418 /* Record undo information, adjust markers and position keepers for an
1419 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1420 text already exists in the current buffer but character length (TO
1421 - FROM) may be incorrect, the correct length is NEWLEN. */
1423 void
1424 adjust_after_insert (EMACS_INT from, EMACS_INT from_byte,
1425 EMACS_INT to, EMACS_INT to_byte, EMACS_INT newlen)
1427 EMACS_INT len = to - from, len_byte = to_byte - from_byte;
1429 if (GPT != to)
1430 move_gap_both (to, to_byte);
1431 GAP_SIZE += len_byte;
1432 GPT -= len; GPT_BYTE -= len_byte;
1433 ZV -= len; ZV_BYTE -= len_byte;
1434 Z -= len; Z_BYTE -= len_byte;
1435 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1438 /* Replace the text from character positions FROM to TO with NEW,
1439 If PREPARE is nonzero, call prepare_to_modify_buffer.
1440 If INHERIT, the newly inserted text should inherit text properties
1441 from the surrounding non-deleted text. */
1443 /* Note that this does not yet handle markers quite right.
1444 Also it needs to record a single undo-entry that does a replacement
1445 rather than a separate delete and insert.
1446 That way, undo will also handle markers properly.
1448 But if MARKERS is 0, don't relocate markers. */
1450 void
1451 replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
1452 int prepare, int inherit, int markers)
1454 EMACS_INT inschars = SCHARS (new);
1455 EMACS_INT insbytes = SBYTES (new);
1456 EMACS_INT from_byte, to_byte;
1457 EMACS_INT nbytes_del, nchars_del;
1458 register Lisp_Object temp;
1459 struct gcpro gcpro1;
1460 INTERVAL intervals;
1461 EMACS_INT outgoing_insbytes = insbytes;
1462 Lisp_Object deletion;
1464 CHECK_MARKERS ();
1466 GCPRO1 (new);
1467 deletion = Qnil;
1469 if (prepare)
1471 EMACS_INT range_length = to - from;
1472 prepare_to_modify_buffer (from, to, &from);
1473 to = from + range_length;
1476 UNGCPRO;
1478 /* Make args be valid */
1479 if (from < BEGV)
1480 from = BEGV;
1481 if (to > ZV)
1482 to = ZV;
1484 from_byte = CHAR_TO_BYTE (from);
1485 to_byte = CHAR_TO_BYTE (to);
1487 nchars_del = to - from;
1488 nbytes_del = to_byte - from_byte;
1490 if (nbytes_del <= 0 && insbytes == 0)
1491 return;
1493 /* Make OUTGOING_INSBYTES describe the text
1494 as it will be inserted in this buffer. */
1496 if (NILP (current_buffer->enable_multibyte_characters))
1497 outgoing_insbytes = inschars;
1498 else if (! STRING_MULTIBYTE (new))
1499 outgoing_insbytes
1500 = count_size_as_multibyte (SDATA (new), insbytes);
1502 /* Make sure point-max won't overflow after this insertion. */
1503 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1504 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1505 error ("Maximum buffer size exceeded");
1507 GCPRO1 (new);
1509 /* Make sure the gap is somewhere in or next to what we are deleting. */
1510 if (from > GPT)
1511 gap_right (from, from_byte);
1512 if (to < GPT)
1513 gap_left (to, to_byte, 0);
1515 /* Even if we don't record for undo, we must keep the original text
1516 because we may have to recover it because of inappropriate byte
1517 combining. */
1518 if (! EQ (current_buffer->undo_list, Qt))
1519 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1521 GAP_SIZE += nbytes_del;
1522 ZV -= nchars_del;
1523 Z -= nchars_del;
1524 ZV_BYTE -= nbytes_del;
1525 Z_BYTE -= nbytes_del;
1526 GPT = from;
1527 GPT_BYTE = from_byte;
1528 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1530 if (GPT_BYTE < GPT)
1531 abort ();
1533 if (GPT - BEG < BEG_UNCHANGED)
1534 BEG_UNCHANGED = GPT - BEG;
1535 if (Z - GPT < END_UNCHANGED)
1536 END_UNCHANGED = Z - GPT;
1538 if (GAP_SIZE < insbytes)
1539 make_gap (insbytes - GAP_SIZE);
1541 /* Copy the string text into the buffer, perhaps converting
1542 between single-byte and multibyte. */
1543 copy_text (SDATA (new), GPT_ADDR, insbytes,
1544 STRING_MULTIBYTE (new),
1545 ! NILP (current_buffer->enable_multibyte_characters));
1547 #ifdef BYTE_COMBINING_DEBUG
1548 /* We have copied text into the gap, but we have not marked
1549 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1550 here, for both the previous text and the following text.
1551 Meanwhile, GPT_ADDR does point to
1552 the text that has been stored by copy_text. */
1553 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1554 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1555 abort ();
1556 #endif
1558 if (! EQ (current_buffer->undo_list, Qt))
1560 /* Record the insertion first, so that when we undo,
1561 the deletion will be undone first. Thus, undo
1562 will insert before deleting, and thus will keep
1563 the markers before and after this text separate. */
1564 record_insert (from + SCHARS (deletion), inschars);
1565 record_delete (from, deletion);
1568 GAP_SIZE -= outgoing_insbytes;
1569 GPT += inschars;
1570 ZV += inschars;
1571 Z += inschars;
1572 GPT_BYTE += outgoing_insbytes;
1573 ZV_BYTE += outgoing_insbytes;
1574 Z_BYTE += outgoing_insbytes;
1575 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1577 if (GPT_BYTE < GPT)
1578 abort ();
1580 /* Adjust the overlay center as needed. This must be done after
1581 adjusting the markers that bound the overlays. */
1582 adjust_overlays_for_delete (from, nchars_del);
1583 adjust_overlays_for_insert (from, inschars);
1585 /* Adjust markers for the deletion and the insertion. */
1586 if (markers)
1587 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1588 inschars, outgoing_insbytes);
1590 offset_intervals (current_buffer, from, inschars - nchars_del);
1592 /* Get the intervals for the part of the string we are inserting--
1593 not including the combined-before bytes. */
1594 intervals = STRING_INTERVALS (new);
1595 /* Insert those intervals. */
1596 graft_intervals_into_buffer (intervals, from, inschars,
1597 current_buffer, inherit);
1599 /* Relocate point as if it were a marker. */
1600 if (from < PT)
1601 adjust_point ((from + inschars - (PT < to ? PT : to)),
1602 (from_byte + outgoing_insbytes
1603 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1605 if (outgoing_insbytes == 0)
1606 evaporate_overlays (from);
1608 CHECK_MARKERS ();
1610 MODIFF++;
1611 CHARS_MODIFF = MODIFF;
1612 UNGCPRO;
1614 signal_after_change (from, nchars_del, GPT - from);
1615 update_compositions (from, GPT, CHECK_BORDER);
1618 /* Replace the text from character positions FROM to TO with
1619 the text in INS of length INSCHARS.
1620 Keep the text properties that applied to the old characters
1621 (extending them to all the new chars if there are more new chars).
1623 Note that this does not yet handle markers quite right.
1625 If MARKERS is nonzero, relocate markers.
1627 Unlike most functions at this level, never call
1628 prepare_to_modify_buffer and never call signal_after_change. */
1630 void
1631 replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
1632 EMACS_INT to, EMACS_INT to_byte,
1633 const char *ins, EMACS_INT inschars, EMACS_INT insbytes,
1634 int markers)
1636 EMACS_INT nbytes_del, nchars_del;
1637 Lisp_Object temp;
1639 CHECK_MARKERS ();
1641 nchars_del = to - from;
1642 nbytes_del = to_byte - from_byte;
1644 if (nbytes_del <= 0 && insbytes == 0)
1645 return;
1647 /* Make sure point-max won't overflow after this insertion. */
1648 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1649 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1650 error ("Maximum buffer size exceeded");
1652 /* Make sure the gap is somewhere in or next to what we are deleting. */
1653 if (from > GPT)
1654 gap_right (from, from_byte);
1655 if (to < GPT)
1656 gap_left (to, to_byte, 0);
1658 GAP_SIZE += nbytes_del;
1659 ZV -= nchars_del;
1660 Z -= nchars_del;
1661 ZV_BYTE -= nbytes_del;
1662 Z_BYTE -= nbytes_del;
1663 GPT = from;
1664 GPT_BYTE = from_byte;
1665 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1667 if (GPT_BYTE < GPT)
1668 abort ();
1670 if (GPT - BEG < BEG_UNCHANGED)
1671 BEG_UNCHANGED = GPT - BEG;
1672 if (Z - GPT < END_UNCHANGED)
1673 END_UNCHANGED = Z - GPT;
1675 if (GAP_SIZE < insbytes)
1676 make_gap (insbytes - GAP_SIZE);
1678 /* Copy the replacement text into the buffer. */
1679 memcpy (GPT_ADDR, ins, insbytes);
1681 #ifdef BYTE_COMBINING_DEBUG
1682 /* We have copied text into the gap, but we have not marked
1683 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1684 here, for both the previous text and the following text.
1685 Meanwhile, GPT_ADDR does point to
1686 the text that has been stored by copy_text. */
1687 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1688 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1689 abort ();
1690 #endif
1692 GAP_SIZE -= insbytes;
1693 GPT += inschars;
1694 ZV += inschars;
1695 Z += inschars;
1696 GPT_BYTE += insbytes;
1697 ZV_BYTE += insbytes;
1698 Z_BYTE += insbytes;
1699 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1701 if (GPT_BYTE < GPT)
1702 abort ();
1704 /* Adjust the overlay center as needed. This must be done after
1705 adjusting the markers that bound the overlays. */
1706 if (nchars_del != inschars)
1708 adjust_overlays_for_insert (from, inschars);
1709 adjust_overlays_for_delete (from + inschars, nchars_del);
1712 /* Adjust markers for the deletion and the insertion. */
1713 if (markers
1714 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1715 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1716 inschars, insbytes);
1718 offset_intervals (current_buffer, from, inschars - nchars_del);
1720 /* Relocate point as if it were a marker. */
1721 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1723 if (PT < to)
1724 /* PT was within the deleted text. Move it to FROM. */
1725 adjust_point (from - PT, from_byte - PT_BYTE);
1726 else
1727 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1730 if (insbytes == 0)
1731 evaporate_overlays (from);
1733 CHECK_MARKERS ();
1735 MODIFF++;
1736 CHARS_MODIFF = MODIFF;
1739 /* Delete characters in current buffer
1740 from FROM up to (but not including) TO.
1741 If TO comes before FROM, we delete nothing. */
1743 void
1744 del_range (EMACS_INT from, EMACS_INT to)
1746 del_range_1 (from, to, 1, 0);
1749 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1750 RET_STRING says to return the deleted text. */
1752 Lisp_Object
1753 del_range_1 (EMACS_INT from, EMACS_INT to, int prepare, int ret_string)
1755 EMACS_INT from_byte, to_byte;
1756 Lisp_Object deletion;
1757 struct gcpro gcpro1;
1759 /* Make args be valid */
1760 if (from < BEGV)
1761 from = BEGV;
1762 if (to > ZV)
1763 to = ZV;
1765 if (to <= from)
1766 return Qnil;
1768 if (prepare)
1770 EMACS_INT range_length = to - from;
1771 prepare_to_modify_buffer (from, to, &from);
1772 to = min (ZV, from + range_length);
1775 from_byte = CHAR_TO_BYTE (from);
1776 to_byte = CHAR_TO_BYTE (to);
1778 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1779 GCPRO1(deletion);
1780 signal_after_change (from, to - from, 0);
1781 update_compositions (from, from, CHECK_HEAD);
1782 UNGCPRO;
1783 return deletion;
1786 /* Like del_range_1 but args are byte positions, not char positions. */
1788 void
1789 del_range_byte (EMACS_INT from_byte, EMACS_INT to_byte, int prepare)
1791 EMACS_INT from, to;
1793 /* Make args be valid */
1794 if (from_byte < BEGV_BYTE)
1795 from_byte = BEGV_BYTE;
1796 if (to_byte > ZV_BYTE)
1797 to_byte = ZV_BYTE;
1799 if (to_byte <= from_byte)
1800 return;
1802 from = BYTE_TO_CHAR (from_byte);
1803 to = BYTE_TO_CHAR (to_byte);
1805 if (prepare)
1807 EMACS_INT old_from = from, old_to = Z - to;
1808 EMACS_INT range_length = to - from;
1809 prepare_to_modify_buffer (from, to, &from);
1810 to = from + range_length;
1812 if (old_from != from)
1813 from_byte = CHAR_TO_BYTE (from);
1814 if (to > ZV)
1816 to = ZV;
1817 to_byte = ZV_BYTE;
1819 else if (old_to == Z - to)
1820 to_byte = CHAR_TO_BYTE (to);
1823 del_range_2 (from, from_byte, to, to_byte, 0);
1824 signal_after_change (from, to - from, 0);
1825 update_compositions (from, from, CHECK_HEAD);
1828 /* Like del_range_1, but positions are specified both as charpos
1829 and bytepos. */
1831 void
1832 del_range_both (EMACS_INT from, EMACS_INT from_byte,
1833 EMACS_INT to, EMACS_INT to_byte, int prepare)
1835 /* Make args be valid */
1836 if (from_byte < BEGV_BYTE)
1837 from_byte = BEGV_BYTE;
1838 if (to_byte > ZV_BYTE)
1839 to_byte = ZV_BYTE;
1841 if (to_byte <= from_byte)
1842 return;
1844 if (from < BEGV)
1845 from = BEGV;
1846 if (to > ZV)
1847 to = ZV;
1849 if (prepare)
1851 EMACS_INT old_from = from, old_to = Z - to;
1852 EMACS_INT range_length = to - from;
1853 prepare_to_modify_buffer (from, to, &from);
1854 to = from + range_length;
1856 if (old_from != from)
1857 from_byte = CHAR_TO_BYTE (from);
1858 if (to > ZV)
1860 to = ZV;
1861 to_byte = ZV_BYTE;
1863 else if (old_to == Z - to)
1864 to_byte = CHAR_TO_BYTE (to);
1867 del_range_2 (from, from_byte, to, to_byte, 0);
1868 signal_after_change (from, to - from, 0);
1869 update_compositions (from, from, CHECK_HEAD);
1872 /* Delete a range of text, specified both as character positions
1873 and byte positions. FROM and TO are character positions,
1874 while FROM_BYTE and TO_BYTE are byte positions.
1875 If RET_STRING is true, the deleted area is returned as a string. */
1877 Lisp_Object
1878 del_range_2 (EMACS_INT from, EMACS_INT from_byte,
1879 EMACS_INT to, EMACS_INT to_byte, int ret_string)
1881 register EMACS_INT nbytes_del, nchars_del;
1882 Lisp_Object deletion;
1884 CHECK_MARKERS ();
1886 nchars_del = to - from;
1887 nbytes_del = to_byte - from_byte;
1889 /* Make sure the gap is somewhere in or next to what we are deleting. */
1890 if (from > GPT)
1891 gap_right (from, from_byte);
1892 if (to < GPT)
1893 gap_left (to, to_byte, 0);
1895 #ifdef BYTE_COMBINING_DEBUG
1896 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1897 Z_BYTE - to_byte, from, from_byte))
1898 abort ();
1899 #endif
1901 if (ret_string || ! EQ (current_buffer->undo_list, Qt))
1902 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1903 else
1904 deletion = Qnil;
1906 /* Relocate all markers pointing into the new, larger gap
1907 to point at the end of the text before the gap.
1908 Do this before recording the deletion,
1909 so that undo handles this after reinserting the text. */
1910 adjust_markers_for_delete (from, from_byte, to, to_byte);
1912 if (! EQ (current_buffer->undo_list, Qt))
1913 record_delete (from, deletion);
1914 MODIFF++;
1915 CHARS_MODIFF = MODIFF;
1917 /* Relocate point as if it were a marker. */
1918 if (from < PT)
1919 adjust_point (from - (PT < to ? PT : to),
1920 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1922 offset_intervals (current_buffer, from, - nchars_del);
1924 /* Adjust the overlay center as needed. This must be done after
1925 adjusting the markers that bound the overlays. */
1926 adjust_overlays_for_delete (from, nchars_del);
1928 GAP_SIZE += nbytes_del;
1929 ZV_BYTE -= nbytes_del;
1930 Z_BYTE -= nbytes_del;
1931 ZV -= nchars_del;
1932 Z -= nchars_del;
1933 GPT = from;
1934 GPT_BYTE = from_byte;
1935 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1936 /* Put an anchor, unless called from decode_coding_object which
1937 needs to access the previous gap contents. */
1938 *(GPT_ADDR) = 0;
1940 if (GPT_BYTE < GPT)
1941 abort ();
1943 if (GPT - BEG < BEG_UNCHANGED)
1944 BEG_UNCHANGED = GPT - BEG;
1945 if (Z - GPT < END_UNCHANGED)
1946 END_UNCHANGED = Z - GPT;
1948 CHECK_MARKERS ();
1950 evaporate_overlays (from);
1952 return deletion;
1955 /* Call this if you're about to change the region of BUFFER from
1956 character positions START to END. This checks the read-only
1957 properties of the region, calls the necessary modification hooks,
1958 and warns the next redisplay that it should pay attention to that
1959 area.
1961 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
1962 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1964 void
1965 modify_region (struct buffer *buffer, EMACS_INT start, EMACS_INT end,
1966 int preserve_chars_modiff)
1968 struct buffer *old_buffer = current_buffer;
1970 if (buffer != old_buffer)
1971 set_buffer_internal (buffer);
1973 prepare_to_modify_buffer (start, end, NULL);
1975 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
1977 if (MODIFF <= SAVE_MODIFF)
1978 record_first_change ();
1979 MODIFF++;
1980 if (! preserve_chars_modiff)
1981 CHARS_MODIFF = MODIFF;
1983 buffer->point_before_scroll = Qnil;
1985 if (buffer != old_buffer)
1986 set_buffer_internal (old_buffer);
1989 /* Check that it is okay to modify the buffer between START and END,
1990 which are char positions.
1992 Run the before-change-function, if any. If intervals are in use,
1993 verify that the text to be modified is not read-only, and call
1994 any modification properties the text may have.
1996 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1997 by holding its value temporarily in a marker. */
1999 void
2000 prepare_to_modify_buffer (EMACS_INT start, EMACS_INT end,
2001 EMACS_INT *preserve_ptr)
2003 struct buffer *base_buffer;
2005 if (!NILP (current_buffer->read_only))
2006 Fbarf_if_buffer_read_only ();
2008 /* Let redisplay consider other windows than selected_window
2009 if modifying another buffer. */
2010 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2011 ++windows_or_buffers_changed;
2013 if (BUF_INTERVALS (current_buffer) != 0)
2015 if (preserve_ptr)
2017 Lisp_Object preserve_marker;
2018 struct gcpro gcpro1;
2019 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
2020 GCPRO1 (preserve_marker);
2021 verify_interval_modification (current_buffer, start, end);
2022 *preserve_ptr = marker_position (preserve_marker);
2023 unchain_marker (XMARKER (preserve_marker));
2024 UNGCPRO;
2026 else
2027 verify_interval_modification (current_buffer, start, end);
2030 /* For indirect buffers, use the base buffer to check clashes. */
2031 if (current_buffer->base_buffer != 0)
2032 base_buffer = current_buffer->base_buffer;
2033 else
2034 base_buffer = current_buffer;
2036 #ifdef CLASH_DETECTION
2037 if (!NILP (base_buffer->file_truename)
2038 /* Make binding buffer-file-name to nil effective. */
2039 && !NILP (base_buffer->filename)
2040 && SAVE_MODIFF >= MODIFF)
2041 lock_file (base_buffer->file_truename);
2042 #else
2043 /* At least warn if this file has changed on disk since it was visited. */
2044 if (!NILP (base_buffer->filename)
2045 && SAVE_MODIFF >= MODIFF
2046 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2047 && !NILP (Ffile_exists_p (base_buffer->filename)))
2048 call1 (intern ("ask-user-about-supersession-threat"),
2049 base_buffer->filename);
2050 #endif /* not CLASH_DETECTION */
2052 /* If `select-active-regions' is non-nil, save the region text. */
2053 if (!NILP (current_buffer->mark_active)
2054 && XMARKER (current_buffer->mark)->buffer
2055 && NILP (Vsaved_region_selection)
2056 && (EQ (Vselect_active_regions, Qonly)
2057 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
2058 : (!NILP (Vselect_active_regions)
2059 && !NILP (Vtransient_mark_mode))))
2061 EMACS_INT b = XMARKER (current_buffer->mark)->charpos;
2062 EMACS_INT e = PT;
2063 if (b < e)
2064 Vsaved_region_selection = make_buffer_string (b, e, 0);
2065 else if (b > e)
2066 Vsaved_region_selection = make_buffer_string (e, b, 0);
2069 signal_before_change (start, end, preserve_ptr);
2071 if (current_buffer->newline_cache)
2072 invalidate_region_cache (current_buffer,
2073 current_buffer->newline_cache,
2074 start - BEG, Z - end);
2075 if (current_buffer->width_run_cache)
2076 invalidate_region_cache (current_buffer,
2077 current_buffer->width_run_cache,
2078 start - BEG, Z - end);
2080 Vdeactivate_mark = Qt;
2083 /* These macros work with an argument named `preserve_ptr'
2084 and a local variable named `preserve_marker'. */
2086 #define PRESERVE_VALUE \
2087 if (preserve_ptr && NILP (preserve_marker)) \
2088 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2090 #define RESTORE_VALUE \
2091 if (! NILP (preserve_marker)) \
2093 *preserve_ptr = marker_position (preserve_marker); \
2094 unchain_marker (XMARKER (preserve_marker)); \
2097 #define PRESERVE_START_END \
2098 if (NILP (start_marker)) \
2099 start_marker = Fcopy_marker (start, Qnil); \
2100 if (NILP (end_marker)) \
2101 end_marker = Fcopy_marker (end, Qnil);
2103 #define FETCH_START \
2104 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2106 #define FETCH_END \
2107 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2109 /* Set a variable to nil if an error occurred.
2110 Don't change the variable if there was no error.
2111 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2112 VARIABLE is the variable to maybe set to nil.
2113 NO-ERROR-FLAG is nil if there was an error,
2114 anything else meaning no error (so this function does nothing). */
2115 Lisp_Object
2116 reset_var_on_error (Lisp_Object val)
2118 if (NILP (XCDR (val)))
2119 Fset (XCAR (val), Qnil);
2120 return Qnil;
2123 /* Signal a change to the buffer immediately before it happens.
2124 START_INT and END_INT are the bounds of the text to be changed.
2126 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2127 by holding its value temporarily in a marker. */
2129 void
2130 signal_before_change (EMACS_INT start_int, EMACS_INT end_int,
2131 EMACS_INT *preserve_ptr)
2133 Lisp_Object start, end;
2134 Lisp_Object start_marker, end_marker;
2135 Lisp_Object preserve_marker;
2136 struct gcpro gcpro1, gcpro2, gcpro3;
2137 int count = SPECPDL_INDEX ();
2139 if (inhibit_modification_hooks)
2140 return;
2142 start = make_number (start_int);
2143 end = make_number (end_int);
2144 preserve_marker = Qnil;
2145 start_marker = Qnil;
2146 end_marker = Qnil;
2147 GCPRO3 (preserve_marker, start_marker, end_marker);
2149 specbind (Qinhibit_modification_hooks, Qt);
2151 /* If buffer is unmodified, run a special hook for that case. */
2152 if (SAVE_MODIFF >= MODIFF
2153 && !NILP (Vfirst_change_hook)
2154 && !NILP (Vrun_hooks))
2156 PRESERVE_VALUE;
2157 PRESERVE_START_END;
2158 call1 (Vrun_hooks, Qfirst_change_hook);
2161 /* Now run the before-change-functions if any. */
2162 if (!NILP (Vbefore_change_functions))
2164 Lisp_Object args[3];
2165 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
2167 PRESERVE_VALUE;
2168 PRESERVE_START_END;
2170 /* Mark before-change-functions to be reset to nil in case of error. */
2171 record_unwind_protect (reset_var_on_error, rvoe_arg);
2173 /* Actually run the hook functions. */
2174 args[0] = Qbefore_change_functions;
2175 args[1] = FETCH_START;
2176 args[2] = FETCH_END;
2177 Frun_hook_with_args (3, args);
2179 /* There was no error: unarm the reset_on_error. */
2180 XSETCDR (rvoe_arg, Qt);
2183 if (current_buffer->overlays_before || current_buffer->overlays_after)
2185 PRESERVE_VALUE;
2186 report_overlay_modification (FETCH_START, FETCH_END, 0,
2187 FETCH_START, FETCH_END, Qnil);
2190 if (! NILP (start_marker))
2191 free_marker (start_marker);
2192 if (! NILP (end_marker))
2193 free_marker (end_marker);
2194 RESTORE_VALUE;
2195 UNGCPRO;
2197 unbind_to (count, Qnil);
2200 /* Signal a change immediately after it happens.
2201 CHARPOS is the character position of the start of the changed text.
2202 LENDEL is the number of characters of the text before the change.
2203 (Not the whole buffer; just the part that was changed.)
2204 LENINS is the number of characters in that part of the text
2205 after the change. */
2207 void
2208 signal_after_change (EMACS_INT charpos, EMACS_INT lendel, EMACS_INT lenins)
2210 int count = SPECPDL_INDEX ();
2211 if (inhibit_modification_hooks)
2212 return;
2214 /* If we are deferring calls to the after-change functions
2215 and there are no before-change functions,
2216 just record the args that we were going to use. */
2217 if (! NILP (Vcombine_after_change_calls)
2218 && NILP (Vbefore_change_functions)
2219 && !current_buffer->overlays_before
2220 && !current_buffer->overlays_after)
2222 Lisp_Object elt;
2224 if (!NILP (combine_after_change_list)
2225 && current_buffer != XBUFFER (combine_after_change_buffer))
2226 Fcombine_after_change_execute ();
2228 elt = Fcons (make_number (charpos - BEG),
2229 Fcons (make_number (Z - (charpos - lendel + lenins)),
2230 Fcons (make_number (lenins - lendel), Qnil)));
2231 combine_after_change_list
2232 = Fcons (elt, combine_after_change_list);
2233 combine_after_change_buffer = Fcurrent_buffer ();
2235 return;
2238 if (!NILP (combine_after_change_list))
2239 Fcombine_after_change_execute ();
2241 specbind (Qinhibit_modification_hooks, Qt);
2243 if (!NILP (Vafter_change_functions))
2245 Lisp_Object args[4];
2246 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2248 /* Mark after-change-functions to be reset to nil in case of error. */
2249 record_unwind_protect (reset_var_on_error, rvoe_arg);
2251 /* Actually run the hook functions. */
2252 args[0] = Qafter_change_functions;
2253 XSETFASTINT (args[1], charpos);
2254 XSETFASTINT (args[2], charpos + lenins);
2255 XSETFASTINT (args[3], lendel);
2256 Frun_hook_with_args (4, args);
2258 /* There was no error: unarm the reset_on_error. */
2259 XSETCDR (rvoe_arg, Qt);
2262 if (current_buffer->overlays_before || current_buffer->overlays_after)
2263 report_overlay_modification (make_number (charpos),
2264 make_number (charpos + lenins),
2266 make_number (charpos),
2267 make_number (charpos + lenins),
2268 make_number (lendel));
2270 /* After an insertion, call the text properties
2271 insert-behind-hooks or insert-in-front-hooks. */
2272 if (lendel == 0)
2273 report_interval_modification (make_number (charpos),
2274 make_number (charpos + lenins));
2276 unbind_to (count, Qnil);
2279 Lisp_Object
2280 Fcombine_after_change_execute_1 (Lisp_Object val)
2282 Vcombine_after_change_calls = val;
2283 return val;
2286 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2287 Scombine_after_change_execute, 0, 0, 0,
2288 doc: /* This function is for use internally in `combine-after-change-calls'. */)
2289 (void)
2291 int count = SPECPDL_INDEX ();
2292 EMACS_INT beg, end, change;
2293 EMACS_INT begpos, endpos;
2294 Lisp_Object tail;
2296 if (NILP (combine_after_change_list))
2297 return Qnil;
2299 /* It is rare for combine_after_change_buffer to be invalid, but
2300 possible. It can happen when combine-after-change-calls is
2301 non-nil, and insertion calls a file handler (e.g. through
2302 lock_file) which scribbles into a temp file -- cyd */
2303 if (!BUFFERP (combine_after_change_buffer)
2304 || NILP (XBUFFER (combine_after_change_buffer)->name))
2306 combine_after_change_list = Qnil;
2307 return Qnil;
2310 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2312 Fset_buffer (combine_after_change_buffer);
2314 /* # chars unchanged at beginning of buffer. */
2315 beg = Z - BEG;
2316 /* # chars unchanged at end of buffer. */
2317 end = beg;
2318 /* Total amount of insertion (negative for deletion). */
2319 change = 0;
2321 /* Scan the various individual changes,
2322 accumulating the range info in BEG, END and CHANGE. */
2323 for (tail = combine_after_change_list; CONSP (tail);
2324 tail = XCDR (tail))
2326 Lisp_Object elt;
2327 EMACS_INT thisbeg, thisend, thischange;
2329 /* Extract the info from the next element. */
2330 elt = XCAR (tail);
2331 if (! CONSP (elt))
2332 continue;
2333 thisbeg = XINT (XCAR (elt));
2335 elt = XCDR (elt);
2336 if (! CONSP (elt))
2337 continue;
2338 thisend = XINT (XCAR (elt));
2340 elt = XCDR (elt);
2341 if (! CONSP (elt))
2342 continue;
2343 thischange = XINT (XCAR (elt));
2345 /* Merge this range into the accumulated range. */
2346 change += thischange;
2347 if (thisbeg < beg)
2348 beg = thisbeg;
2349 if (thisend < end)
2350 end = thisend;
2353 /* Get the current start and end positions of the range
2354 that was changed. */
2355 begpos = BEG + beg;
2356 endpos = Z - end;
2358 /* We are about to handle these, so discard them. */
2359 combine_after_change_list = Qnil;
2361 /* Now run the after-change functions for real.
2362 Turn off the flag that defers them. */
2363 record_unwind_protect (Fcombine_after_change_execute_1,
2364 Vcombine_after_change_calls);
2365 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2366 update_compositions (begpos, endpos, CHECK_ALL);
2368 return unbind_to (count, Qnil);
2371 void
2372 syms_of_insdel (void)
2374 staticpro (&combine_after_change_list);
2375 staticpro (&combine_after_change_buffer);
2376 combine_after_change_list = Qnil;
2377 combine_after_change_buffer = Qnil;
2379 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2380 doc: /* Non-nil means enable debugging checks for invalid marker positions. */);
2381 check_markers_debug_flag = 0;
2382 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2383 doc: /* Used internally by the `combine-after-change-calls' macro. */);
2384 Vcombine_after_change_calls = Qnil;
2386 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2387 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2388 This affects `before-change-functions' and `after-change-functions',
2389 as well as hooks attached to text properties and overlays. */);
2390 inhibit_modification_hooks = 0;
2391 Qinhibit_modification_hooks = intern_c_string ("inhibit-modification-hooks");
2392 staticpro (&Qinhibit_modification_hooks);
2394 defsubr (&Scombine_after_change_execute);
2397 /* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
2398 (do not change this comment) */