When opening new eww buffers, use buffer names based on the host name
[emacs.git] / src / insdel.c
blobb93606ced855ddb94047ad5b25399fc4c36e75bb
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2017 Free Software
3 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 3 of the License, or (at
10 your option) 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. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <intprops.h>
25 #include "lisp.h"
26 #include "composite.h"
27 #include "intervals.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "window.h"
31 #include "region-cache.h"
33 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
44 In each element.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list;
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer;
54 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
56 /* Also used in marker.c to enable expensive marker checks. */
58 #ifdef MARKER_DEBUG
60 static void
61 check_markers (void)
63 struct Lisp_Marker *tail;
64 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
66 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
68 if (tail->buffer->text != current_buffer->text)
69 emacs_abort ();
70 if (tail->charpos > Z)
71 emacs_abort ();
72 if (tail->bytepos > Z_BYTE)
73 emacs_abort ();
74 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
75 emacs_abort ();
79 #else /* not MARKER_DEBUG */
81 #define check_markers() do { } while (0)
83 #endif /* MARKER_DEBUG */
85 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
86 Note that this can quit! */
88 void
89 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
91 eassert (charpos == BYTE_TO_CHAR (bytepos)
92 && bytepos == CHAR_TO_BYTE (charpos));
93 if (bytepos < GPT_BYTE)
94 gap_left (charpos, bytepos, 0);
95 else if (bytepos > GPT_BYTE)
96 gap_right (charpos, bytepos);
99 /* Move the gap to a position less than the current GPT.
100 BYTEPOS describes the new position as a byte position,
101 and CHARPOS is the corresponding char position.
102 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
104 static void
105 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
107 unsigned char *to, *from;
108 ptrdiff_t i;
109 ptrdiff_t new_s1;
111 if (!newgap)
112 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
114 i = GPT_BYTE;
115 to = GAP_END_ADDR;
116 from = GPT_ADDR;
117 new_s1 = GPT_BYTE;
119 /* Now copy the characters. To move the gap down,
120 copy characters up. */
122 while (1)
124 /* I gets number of characters left to copy. */
125 i = new_s1 - bytepos;
126 if (i == 0)
127 break;
128 /* If a quit is requested, stop copying now.
129 Change BYTEPOS to be where we have actually moved the gap to.
130 Note that this cannot happen when we are called to make the
131 gap larger or smaller, since make_gap_larger and
132 make_gap_smaller prevent QUIT by setting inhibit-quit. */
133 if (QUITP)
135 bytepos = new_s1;
136 charpos = BYTE_TO_CHAR (bytepos);
137 break;
139 /* Move at most 32000 chars before checking again for a quit. */
140 if (i > 32000)
141 i = 32000;
142 new_s1 -= i;
143 from -= i, to -= i;
144 memmove (to, from, i);
147 /* Adjust buffer data structure, to put the gap at BYTEPOS.
148 BYTEPOS is where the loop above stopped, which may be what
149 was specified or may be where a quit was detected. */
150 GPT_BYTE = bytepos;
151 GPT = charpos;
152 eassert (charpos <= bytepos);
153 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
154 QUIT;
157 /* Move the gap to a position greater than the current GPT.
158 BYTEPOS describes the new position as a byte position,
159 and CHARPOS is the corresponding char position. */
161 static void
162 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
164 register unsigned char *to, *from;
165 register ptrdiff_t i;
166 ptrdiff_t new_s1;
168 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
170 i = GPT_BYTE;
171 from = GAP_END_ADDR;
172 to = GPT_ADDR;
173 new_s1 = GPT_BYTE;
175 /* Now copy the characters. To move the gap up,
176 copy characters down. */
178 while (1)
180 /* I gets number of characters left to copy. */
181 i = bytepos - new_s1;
182 if (i == 0)
183 break;
184 /* If a quit is requested, stop copying now.
185 Change BYTEPOS to be where we have actually moved the gap to.
186 Note that this cannot happen when we are called to make the
187 gap larger or smaller, since make_gap_larger and
188 make_gap_smaller prevent QUIT by setting inhibit-quit. */
189 if (QUITP)
191 bytepos = new_s1;
192 charpos = BYTE_TO_CHAR (bytepos);
193 break;
195 /* Move at most 32000 chars before checking again for a quit. */
196 if (i > 32000)
197 i = 32000;
198 new_s1 += i;
199 memmove (to, from, i);
200 from += i, to += i;
203 GPT = charpos;
204 GPT_BYTE = bytepos;
205 eassert (charpos <= bytepos);
206 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
207 QUIT;
210 /* If the selected window's old pointm is adjacent or covered by the
211 region from FROM to TO, unsuspend auto hscroll in that window. */
213 static void
214 adjust_suspend_auto_hscroll (ptrdiff_t from, ptrdiff_t to)
216 if (WINDOWP (selected_window))
218 struct window *w = XWINDOW (selected_window);
220 if (BUFFERP (w->contents)
221 && XBUFFER (w->contents) == current_buffer
222 && XMARKER (w->old_pointm)->charpos >= from
223 && XMARKER (w->old_pointm)->charpos <= to)
224 w->suspend_auto_hscroll = 0;
229 /* Adjust all markers for a deletion
230 whose range in bytes is FROM_BYTE to TO_BYTE.
231 The range in charpos is FROM to TO.
233 This function assumes that the gap is adjacent to
234 or inside of the range being deleted. */
236 void
237 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
238 ptrdiff_t to, ptrdiff_t to_byte)
240 struct Lisp_Marker *m;
241 ptrdiff_t charpos;
243 adjust_suspend_auto_hscroll (from, to);
244 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
246 charpos = m->charpos;
247 eassert (charpos <= Z);
249 /* If the marker is after the deletion,
250 relocate by number of chars / bytes deleted. */
251 if (charpos > to)
253 m->charpos -= to - from;
254 m->bytepos -= to_byte - from_byte;
256 /* Here's the case where a marker is inside text being deleted. */
257 else if (charpos > from)
259 m->charpos = from;
260 m->bytepos = from_byte;
266 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
267 to TO / TO_BYTE. We have to relocate the charpos of every marker
268 that points after the insertion (but not their bytepos).
270 When a marker points at the insertion point,
271 we advance it if either its insertion-type is t
272 or BEFORE_MARKERS is true. */
274 static void
275 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
276 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
278 struct Lisp_Marker *m;
279 bool adjusted = 0;
280 ptrdiff_t nchars = to - from;
281 ptrdiff_t nbytes = to_byte - from_byte;
283 adjust_suspend_auto_hscroll (from, to);
284 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
286 eassert (m->bytepos >= m->charpos
287 && m->bytepos - m->charpos <= Z_BYTE - Z);
289 if (m->bytepos == from_byte)
291 if (m->insertion_type || before_markers)
293 m->bytepos = to_byte;
294 m->charpos = to;
295 if (m->insertion_type)
296 adjusted = 1;
299 else if (m->bytepos > from_byte)
301 m->bytepos += nbytes;
302 m->charpos += nchars;
306 /* Adjusting only markers whose insertion-type is t may result in
307 - disordered start and end in overlays, and
308 - disordered overlays in the slot `overlays_before' of current_buffer. */
309 if (adjusted)
311 fix_start_end_in_overlays (from, to);
312 fix_overlays_before (current_buffer, from, to);
316 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
318 This is used only when the value of point changes due to an insert
319 or delete; it does not represent a conceptual change in point as a
320 marker. In particular, point is not crossing any interval
321 boundaries, so there's no need to use the usual SET_PT macro. In
322 fact it would be incorrect to do so, because either the old or the
323 new value of point is out of sync with the current set of
324 intervals. */
326 static void
327 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
329 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
330 /* In a single-byte buffer, the two positions must be equal. */
331 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
334 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
335 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
336 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
337 an insertion. */
339 static void
340 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
341 ptrdiff_t old_chars, ptrdiff_t old_bytes,
342 ptrdiff_t new_chars, ptrdiff_t new_bytes)
344 register struct Lisp_Marker *m;
345 ptrdiff_t prev_to_byte = from_byte + old_bytes;
346 ptrdiff_t diff_chars = new_chars - old_chars;
347 ptrdiff_t diff_bytes = new_bytes - old_bytes;
349 adjust_suspend_auto_hscroll (from, from + old_chars);
350 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
352 if (m->bytepos >= prev_to_byte)
354 m->charpos += diff_chars;
355 m->bytepos += diff_bytes;
357 else if (m->bytepos > from_byte)
359 m->charpos = from;
360 m->bytepos = from_byte;
364 check_markers ();
367 /* Starting at POS (BYTEPOS), find the byte position corresponding to
368 ENDPOS, which could be either before or after POS. */
369 static ptrdiff_t
370 count_bytes (ptrdiff_t pos, ptrdiff_t bytepos, ptrdiff_t endpos)
372 eassert (BEG_BYTE <= bytepos && bytepos <= Z_BYTE
373 && BEG <= endpos && endpos <= Z);
375 if (pos <= endpos)
376 for ( ; pos < endpos; pos++)
377 INC_POS (bytepos);
378 else
379 for ( ; pos > endpos; pos--)
380 DEC_POS (bytepos);
382 return bytepos;
385 /* Adjust byte positions of markers when their character positions
386 didn't change. This is used in several places that replace text,
387 but keep the character positions of the markers unchanged -- the
388 byte positions could still change due to different numbers of bytes
389 in the new text.
391 FROM (FROM_BYTE) and TO (TO_BYTE) specify the region of text where
392 changes have been done. TO_Z, if non-zero, means all the markers
393 whose positions are after TO should also be adjusted. */
394 void
395 adjust_markers_bytepos (ptrdiff_t from, ptrdiff_t from_byte,
396 ptrdiff_t to, ptrdiff_t to_byte, int to_z)
398 register struct Lisp_Marker *m;
399 ptrdiff_t beg = from, begbyte = from_byte;
401 adjust_suspend_auto_hscroll (from, to);
403 if (Z == Z_BYTE || (!to_z && to == to_byte))
405 /* Make sure each affected marker's bytepos is equal to
406 its charpos. */
407 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
409 if (m->bytepos > from_byte
410 && (to_z || m->bytepos <= to_byte))
411 m->bytepos = m->charpos;
414 else
416 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
418 /* Recompute each affected marker's bytepos. */
419 if (m->bytepos > from_byte
420 && (to_z || m->bytepos <= to_byte))
422 if (m->charpos < beg
423 && beg - m->charpos > m->charpos - from)
425 beg = from;
426 begbyte = from_byte;
428 m->bytepos = count_bytes (beg, begbyte, m->charpos);
429 beg = m->charpos;
430 begbyte = m->bytepos;
435 /* Make sure cached charpos/bytepos is invalid. */
436 clear_charpos_cache (current_buffer);
440 void
441 buffer_overflow (void)
443 error ("Maximum buffer size exceeded");
446 /* Make the gap NBYTES_ADDED bytes longer. */
448 static void
449 make_gap_larger (ptrdiff_t nbytes_added)
451 Lisp_Object tem;
452 ptrdiff_t real_gap_loc;
453 ptrdiff_t real_gap_loc_byte;
454 ptrdiff_t old_gap_size;
455 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
457 if (BUF_BYTES_MAX - current_size < nbytes_added)
458 buffer_overflow ();
460 /* If we have to get more space, get enough to last a while;
461 but do not exceed the maximum buffer size. */
462 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
463 BUF_BYTES_MAX - current_size);
465 enlarge_buffer_text (current_buffer, nbytes_added);
467 /* Prevent quitting in gap_left. We cannot allow a QUIT there,
468 because that would leave the buffer text in an inconsistent
469 state, with 2 gap holes instead of just one. */
470 tem = Vinhibit_quit;
471 Vinhibit_quit = Qt;
473 real_gap_loc = GPT;
474 real_gap_loc_byte = GPT_BYTE;
475 old_gap_size = GAP_SIZE;
477 /* Call the newly allocated space a gap at the end of the whole space. */
478 GPT = Z + GAP_SIZE;
479 GPT_BYTE = Z_BYTE + GAP_SIZE;
480 GAP_SIZE = nbytes_added;
482 /* Move the new gap down to be consecutive with the end of the old one. */
483 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
485 /* Now combine the two into one large gap. */
486 GAP_SIZE += old_gap_size;
487 GPT = real_gap_loc;
488 GPT_BYTE = real_gap_loc_byte;
490 /* Put an anchor. */
491 *(Z_ADDR) = 0;
493 Vinhibit_quit = tem;
496 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
498 /* Make the gap NBYTES_REMOVED bytes shorter. */
500 static void
501 make_gap_smaller (ptrdiff_t nbytes_removed)
503 Lisp_Object tem;
504 ptrdiff_t real_gap_loc;
505 ptrdiff_t real_gap_loc_byte;
506 ptrdiff_t real_Z;
507 ptrdiff_t real_Z_byte;
508 ptrdiff_t real_beg_unchanged;
509 ptrdiff_t new_gap_size;
511 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
512 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
513 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
515 /* Prevent quitting in gap_right. We cannot allow a QUIT there,
516 because that would leave the buffer text in an inconsistent
517 state, with 2 gap holes instead of just one. */
518 tem = Vinhibit_quit;
519 Vinhibit_quit = Qt;
521 real_gap_loc = GPT;
522 real_gap_loc_byte = GPT_BYTE;
523 new_gap_size = GAP_SIZE - nbytes_removed;
524 real_Z = Z;
525 real_Z_byte = Z_BYTE;
526 real_beg_unchanged = BEG_UNCHANGED;
528 /* Pretend that the last unwanted part of the gap is the entire gap,
529 and that the first desired part of the gap is part of the buffer
530 text. */
531 memset (GPT_ADDR, 0, new_gap_size);
532 GPT += new_gap_size;
533 GPT_BYTE += new_gap_size;
534 Z += new_gap_size;
535 Z_BYTE += new_gap_size;
536 GAP_SIZE = nbytes_removed;
538 /* Move the unwanted pretend gap to the end of the buffer. */
539 gap_right (Z, Z_BYTE);
541 enlarge_buffer_text (current_buffer, -nbytes_removed);
543 /* Now restore the desired gap. */
544 GAP_SIZE = new_gap_size;
545 GPT = real_gap_loc;
546 GPT_BYTE = real_gap_loc_byte;
547 Z = real_Z;
548 Z_BYTE = real_Z_byte;
549 BEG_UNCHANGED = real_beg_unchanged;
551 /* Put an anchor. */
552 *(Z_ADDR) = 0;
554 Vinhibit_quit = tem;
557 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
559 void
560 make_gap (ptrdiff_t nbytes_added)
562 if (nbytes_added >= 0)
563 make_gap_larger (nbytes_added);
564 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
565 else
566 make_gap_smaller (-nbytes_added);
567 #endif
570 /* Add NBYTES to B's gap. It's enough to temporarily
571 fake current_buffer and avoid real switch to B. */
573 void
574 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
576 struct buffer *oldb = current_buffer;
578 current_buffer = b;
579 make_gap (nbytes);
580 current_buffer = oldb;
583 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
584 FROM_MULTIBYTE says whether the incoming text is multibyte.
585 TO_MULTIBYTE says whether to store the text as multibyte.
586 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
588 Return the number of bytes stored at TO_ADDR. */
590 ptrdiff_t
591 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
592 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
594 if (from_multibyte == to_multibyte)
596 memcpy (to_addr, from_addr, nbytes);
597 return nbytes;
599 else if (from_multibyte)
601 ptrdiff_t nchars = 0;
602 ptrdiff_t bytes_left = nbytes;
604 while (bytes_left > 0)
606 int thislen, c;
607 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
608 if (! ASCII_CHAR_P (c))
609 c &= 0xFF;
610 *to_addr++ = c;
611 from_addr += thislen;
612 bytes_left -= thislen;
613 nchars++;
615 return nchars;
617 else
619 unsigned char *initial_to_addr = to_addr;
621 /* Convert single-byte to multibyte. */
622 while (nbytes > 0)
624 int c = *from_addr++;
626 if (!ASCII_CHAR_P (c))
628 c = BYTE8_TO_CHAR (c);
629 to_addr += CHAR_STRING (c, to_addr);
630 nbytes--;
632 else
633 /* Special case for speed. */
634 *to_addr++ = c, nbytes--;
636 return to_addr - initial_to_addr;
640 /* Insert a string of specified length before point.
641 This function judges multibyteness based on
642 enable_multibyte_characters in the current buffer;
643 it never converts between single-byte and multibyte.
645 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
646 prepare_to_modify_buffer could relocate the text. */
648 void
649 insert (const char *string, ptrdiff_t nbytes)
651 if (nbytes > 0)
653 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
654 insert_1_both (string, len, nbytes, 0, 1, 0);
655 opoint = PT - len;
656 signal_after_change (opoint, 0, len);
657 update_compositions (opoint, PT, CHECK_BORDER);
661 /* Likewise, but inherit text properties from neighboring characters. */
663 void
664 insert_and_inherit (const char *string, ptrdiff_t nbytes)
666 if (nbytes > 0)
668 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
669 insert_1_both (string, len, nbytes, 1, 1, 0);
670 opoint = PT - len;
671 signal_after_change (opoint, 0, len);
672 update_compositions (opoint, PT, CHECK_BORDER);
676 /* Insert the character C before point. Do not inherit text properties. */
678 void
679 insert_char (int c)
681 unsigned char str[MAX_MULTIBYTE_LENGTH];
682 int len;
684 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
685 len = CHAR_STRING (c, str);
686 else
688 len = 1;
689 str[0] = c;
692 insert ((char *) str, len);
695 /* Insert the null-terminated string S before point. */
697 void
698 insert_string (const char *s)
700 insert (s, strlen (s));
703 /* Like `insert' except that all markers pointing at the place where
704 the insertion happens are adjusted to point after it.
705 Don't use this function to insert part of a Lisp string,
706 since gc could happen and relocate it. */
708 void
709 insert_before_markers (const char *string, ptrdiff_t nbytes)
711 if (nbytes > 0)
713 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
714 insert_1_both (string, len, nbytes, 0, 1, 1);
715 opoint = PT - len;
716 signal_after_change (opoint, 0, len);
717 update_compositions (opoint, PT, CHECK_BORDER);
721 /* Likewise, but inherit text properties from neighboring characters. */
723 void
724 insert_before_markers_and_inherit (const char *string,
725 ptrdiff_t nbytes)
727 if (nbytes > 0)
729 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
730 insert_1_both (string, len, nbytes, 1, 1, 1);
731 opoint = PT - len;
732 signal_after_change (opoint, 0, len);
733 update_compositions (opoint, PT, CHECK_BORDER);
737 #ifdef BYTE_COMBINING_DEBUG
739 /* See if the bytes before POS/POS_BYTE combine with bytes
740 at the start of STRING to form a single character.
741 If so, return the number of bytes at the start of STRING
742 which combine in this way. Otherwise, return 0. */
745 count_combining_before (const unsigned char *string, ptrdiff_t length,
746 ptrdiff_t pos, ptrdiff_t pos_byte)
748 int len, combining_bytes;
749 const unsigned char *p;
751 if (NILP (current_buffer->enable_multibyte_characters))
752 return 0;
754 /* At first, we can exclude the following cases:
755 (1) STRING[0] can't be a following byte of multibyte sequence.
756 (2) POS is the start of the current buffer.
757 (3) A character before POS is not a multibyte character. */
758 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
759 return 0;
760 if (pos_byte == BEG_BYTE) /* case (2) */
761 return 0;
762 len = 1;
763 p = BYTE_POS_ADDR (pos_byte - 1);
764 while (! CHAR_HEAD_P (*p)) p--, len++;
765 if (! LEADING_CODE_P (*p)) /* case (3) */
766 return 0;
768 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
769 if (combining_bytes <= 0)
770 /* The character preceding POS is, complete and no room for
771 combining bytes (combining_bytes == 0), or an independent 8-bit
772 character (combining_bytes < 0). */
773 return 0;
775 /* We have a combination situation. Count the bytes at STRING that
776 may combine. */
777 p = string + 1;
778 while (!CHAR_HEAD_P (*p) && p < string + length)
779 p++;
781 return (combining_bytes < p - string ? combining_bytes : p - string);
784 /* See if the bytes after POS/POS_BYTE combine with bytes
785 at the end of STRING to form a single character.
786 If so, return the number of bytes after POS/POS_BYTE
787 which combine in this way. Otherwise, return 0. */
790 count_combining_after (const unsigned char *string,
791 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
793 ptrdiff_t opos_byte = pos_byte;
794 ptrdiff_t i;
795 ptrdiff_t bytes;
796 unsigned char *bufp;
798 if (NILP (current_buffer->enable_multibyte_characters))
799 return 0;
801 /* At first, we can exclude the following cases:
802 (1) The last byte of STRING is an ASCII.
803 (2) POS is the last of the current buffer.
804 (3) A character at POS can't be a following byte of multibyte
805 character. */
806 if (length > 0 && ASCII_CHAR_P (string[length - 1])) /* case (1) */
807 return 0;
808 if (pos_byte == Z_BYTE) /* case (2) */
809 return 0;
810 bufp = BYTE_POS_ADDR (pos_byte);
811 if (CHAR_HEAD_P (*bufp)) /* case (3) */
812 return 0;
814 i = length - 1;
815 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
817 i--;
819 if (i < 0)
821 /* All characters in STRING are not character head. We must
822 check also preceding bytes at POS. We are sure that the gap
823 is at POS. */
824 unsigned char *p = BEG_ADDR;
825 i = pos_byte - 2;
826 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
827 i--;
828 if (i < 0 || !LEADING_CODE_P (p[i]))
829 return 0;
831 bytes = BYTES_BY_CHAR_HEAD (p[i]);
832 return (bytes <= pos_byte - 1 - i + length
834 : bytes - (pos_byte - 1 - i + length));
836 if (!LEADING_CODE_P (string[i]))
837 return 0;
839 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
840 bufp++, pos_byte++;
841 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
843 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
846 #endif
849 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
850 starting at STRING. INHERIT non-zero means inherit the text
851 properties from neighboring characters; zero means inserted text
852 will have no text properties. PREPARE non-zero means call
853 prepare_to_modify_buffer, which checks that the region is not
854 read-only, and calls before-change-function and any modification
855 properties the text may have. BEFORE_MARKERS non-zero means adjust
856 all markers that point at the insertion place to point after it. */
858 void
859 insert_1_both (const char *string,
860 ptrdiff_t nchars, ptrdiff_t nbytes,
861 bool inherit, bool prepare, bool before_markers)
863 if (nchars == 0)
864 return;
866 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
867 nchars = nbytes;
869 if (prepare)
870 /* Do this before moving and increasing the gap,
871 because the before-change hooks might move the gap
872 or make it smaller. */
873 prepare_to_modify_buffer (PT, PT, NULL);
875 if (PT != GPT)
876 move_gap_both (PT, PT_BYTE);
877 if (GAP_SIZE < nbytes)
878 make_gap (nbytes - GAP_SIZE);
880 #ifdef BYTE_COMBINING_DEBUG
881 if (count_combining_before (string, nbytes, PT, PT_BYTE)
882 || count_combining_after (string, nbytes, PT, PT_BYTE))
883 emacs_abort ();
884 #endif
886 /* Record deletion of the surrounding text that combines with
887 the insertion. This, together with recording the insertion,
888 will add up to the right stuff in the undo list. */
889 record_insert (PT, nchars);
890 MODIFF++;
891 CHARS_MODIFF = MODIFF;
893 memcpy (GPT_ADDR, string, nbytes);
895 GAP_SIZE -= nbytes;
896 GPT += nchars;
897 ZV += nchars;
898 Z += nchars;
899 GPT_BYTE += nbytes;
900 ZV_BYTE += nbytes;
901 Z_BYTE += nbytes;
902 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
904 eassert (GPT <= GPT_BYTE);
906 /* The insert may have been in the unchanged region, so check again. */
907 if (Z - GPT < END_UNCHANGED)
908 END_UNCHANGED = Z - GPT;
910 adjust_overlays_for_insert (PT, nchars);
911 adjust_markers_for_insert (PT, PT_BYTE,
912 PT + nchars, PT_BYTE + nbytes,
913 before_markers);
915 offset_intervals (current_buffer, PT, nchars);
917 if (!inherit && buffer_intervals (current_buffer))
918 set_text_properties (make_number (PT), make_number (PT + nchars),
919 Qnil, Qnil, Qnil);
921 adjust_point (nchars, nbytes);
923 check_markers ();
926 /* Insert the part of the text of STRING, a Lisp object assumed to be
927 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
928 starting at position POS / POS_BYTE. If the text of STRING has properties,
929 copy them into the buffer.
931 It does not work to use `insert' for this, because a GC could happen
932 before we copy the stuff into the buffer, and relocate the string
933 without insert noticing. */
935 void
936 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
937 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
939 ptrdiff_t opoint = PT;
941 if (SCHARS (string) == 0)
942 return;
944 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
945 inherit, 0);
946 signal_after_change (opoint, 0, PT - opoint);
947 update_compositions (opoint, PT, CHECK_BORDER);
950 /* Like `insert_from_string' except that all markers pointing
951 at the place where the insertion happens are adjusted to point after it. */
953 void
954 insert_from_string_before_markers (Lisp_Object string,
955 ptrdiff_t pos, ptrdiff_t pos_byte,
956 ptrdiff_t length, ptrdiff_t length_byte,
957 bool inherit)
959 ptrdiff_t opoint = PT;
961 if (SCHARS (string) == 0)
962 return;
964 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
965 inherit, 1);
966 signal_after_change (opoint, 0, PT - opoint);
967 update_compositions (opoint, PT, CHECK_BORDER);
970 /* Subroutine of the insertion functions above. */
972 static void
973 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
974 ptrdiff_t nchars, ptrdiff_t nbytes,
975 bool inherit, bool before_markers)
977 ptrdiff_t outgoing_nbytes = nbytes;
978 INTERVAL intervals;
980 /* Make OUTGOING_NBYTES describe the text
981 as it will be inserted in this buffer. */
983 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
984 outgoing_nbytes = nchars;
985 else if (! STRING_MULTIBYTE (string))
986 outgoing_nbytes
987 = count_size_as_multibyte (SDATA (string) + pos_byte,
988 nbytes);
990 /* Do this before moving and increasing the gap,
991 because the before-change hooks might move the gap
992 or make it smaller. */
993 prepare_to_modify_buffer (PT, PT, NULL);
995 if (PT != GPT)
996 move_gap_both (PT, PT_BYTE);
997 if (GAP_SIZE < outgoing_nbytes)
998 make_gap (outgoing_nbytes - GAP_SIZE);
1000 /* Copy the string text into the buffer, perhaps converting
1001 between single-byte and multibyte. */
1002 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
1003 STRING_MULTIBYTE (string),
1004 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1006 #ifdef BYTE_COMBINING_DEBUG
1007 /* We have copied text into the gap, but we have not altered
1008 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1009 to these functions and get the same results as we would
1010 have got earlier on. Meanwhile, PT_ADDR does point to
1011 the text that has been stored by copy_text. */
1012 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1013 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1014 emacs_abort ();
1015 #endif
1017 record_insert (PT, nchars);
1018 MODIFF++;
1019 CHARS_MODIFF = MODIFF;
1021 GAP_SIZE -= outgoing_nbytes;
1022 GPT += nchars;
1023 ZV += nchars;
1024 Z += nchars;
1025 GPT_BYTE += outgoing_nbytes;
1026 ZV_BYTE += outgoing_nbytes;
1027 Z_BYTE += outgoing_nbytes;
1028 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1030 eassert (GPT <= GPT_BYTE);
1032 /* The insert may have been in the unchanged region, so check again. */
1033 if (Z - GPT < END_UNCHANGED)
1034 END_UNCHANGED = Z - GPT;
1036 adjust_overlays_for_insert (PT, nchars);
1037 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1038 PT_BYTE + outgoing_nbytes,
1039 before_markers);
1041 offset_intervals (current_buffer, PT, nchars);
1043 intervals = string_intervals (string);
1044 /* Get the intervals for the part of the string we are inserting. */
1045 if (nbytes < SBYTES (string))
1046 intervals = copy_intervals (intervals, pos, nchars);
1048 /* Insert those intervals. */
1049 graft_intervals_into_buffer (intervals, PT, nchars,
1050 current_buffer, inherit);
1052 adjust_point (nchars, outgoing_nbytes);
1054 check_markers ();
1057 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1058 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
1059 GPT_ADDR (if not text_at_gap_tail). */
1061 void
1062 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
1064 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
1066 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1067 nchars = nbytes;
1069 /* No need to call prepare_to_modify_buffer, since this is called
1070 from places that replace some region with a different text, so
1071 prepare_to_modify_buffer was already called by the deletion part
1072 of this dance. */
1073 invalidate_buffer_caches (current_buffer, GPT, GPT);
1074 record_insert (GPT, nchars);
1075 MODIFF++;
1077 GAP_SIZE -= nbytes;
1078 if (! text_at_gap_tail)
1080 GPT += nchars;
1081 GPT_BYTE += nbytes;
1083 ZV += nchars;
1084 Z += nchars;
1085 ZV_BYTE += nbytes;
1086 Z_BYTE += nbytes;
1087 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1089 eassert (GPT <= GPT_BYTE);
1091 adjust_overlays_for_insert (ins_charpos, nchars);
1092 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1093 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1095 if (buffer_intervals (current_buffer))
1097 offset_intervals (current_buffer, ins_charpos, nchars);
1098 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1099 current_buffer, 0);
1102 if (ins_charpos < PT)
1103 adjust_point (nchars, nbytes);
1105 check_markers ();
1108 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1109 current buffer. If the text in BUF has properties, they are absorbed
1110 into the current buffer.
1112 It does not work to use `insert' for this, because a malloc could happen
1113 and relocate BUF's text before the copy happens. */
1115 void
1116 insert_from_buffer (struct buffer *buf,
1117 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1119 ptrdiff_t opoint = PT;
1121 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1122 signal_after_change (opoint, 0, PT - opoint);
1123 update_compositions (opoint, PT, CHECK_BORDER);
1126 static void
1127 insert_from_buffer_1 (struct buffer *buf,
1128 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1130 ptrdiff_t chunk, chunk_expanded;
1131 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1132 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1133 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1134 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1135 INTERVAL intervals;
1137 if (nchars == 0)
1138 return;
1140 /* Make OUTGOING_NBYTES describe the text
1141 as it will be inserted in this buffer. */
1143 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1144 outgoing_nbytes = nchars;
1145 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1147 ptrdiff_t outgoing_before_gap = 0;
1148 ptrdiff_t outgoing_after_gap = 0;
1150 if (from < BUF_GPT (buf))
1152 chunk = BUF_GPT_BYTE (buf) - from_byte;
1153 if (chunk > incoming_nbytes)
1154 chunk = incoming_nbytes;
1155 outgoing_before_gap
1156 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1157 chunk);
1159 else
1160 chunk = 0;
1162 if (chunk < incoming_nbytes)
1163 outgoing_after_gap
1164 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1165 from_byte + chunk),
1166 incoming_nbytes - chunk);
1168 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1171 /* Do this before moving and increasing the gap,
1172 because the before-change hooks might move the gap
1173 or make it smaller. */
1174 prepare_to_modify_buffer (PT, PT, NULL);
1176 if (PT != GPT)
1177 move_gap_both (PT, PT_BYTE);
1178 if (GAP_SIZE < outgoing_nbytes)
1179 make_gap (outgoing_nbytes - GAP_SIZE);
1181 if (from < BUF_GPT (buf))
1183 chunk = BUF_GPT_BYTE (buf) - from_byte;
1184 if (chunk > incoming_nbytes)
1185 chunk = incoming_nbytes;
1186 /* Record number of output bytes, so we know where
1187 to put the output from the second copy_text. */
1188 chunk_expanded
1189 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1190 GPT_ADDR, chunk,
1191 ! NILP (BVAR (buf, enable_multibyte_characters)),
1192 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1194 else
1195 chunk_expanded = chunk = 0;
1197 if (chunk < incoming_nbytes)
1198 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1199 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1200 ! NILP (BVAR (buf, enable_multibyte_characters)),
1201 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1203 #ifdef BYTE_COMBINING_DEBUG
1204 /* We have copied text into the gap, but we have not altered
1205 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1206 to these functions and get the same results as we would
1207 have got earlier on. Meanwhile, GPT_ADDR does point to
1208 the text that has been stored by copy_text. */
1209 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1210 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1211 emacs_abort ();
1212 #endif
1214 record_insert (PT, nchars);
1215 MODIFF++;
1216 CHARS_MODIFF = MODIFF;
1218 GAP_SIZE -= outgoing_nbytes;
1219 GPT += nchars;
1220 ZV += nchars;
1221 Z += nchars;
1222 GPT_BYTE += outgoing_nbytes;
1223 ZV_BYTE += outgoing_nbytes;
1224 Z_BYTE += outgoing_nbytes;
1225 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1227 eassert (GPT <= GPT_BYTE);
1229 /* The insert may have been in the unchanged region, so check again. */
1230 if (Z - GPT < END_UNCHANGED)
1231 END_UNCHANGED = Z - GPT;
1233 adjust_overlays_for_insert (PT, nchars);
1234 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1235 PT_BYTE + outgoing_nbytes,
1238 offset_intervals (current_buffer, PT, nchars);
1240 /* Get the intervals for the part of the string we are inserting. */
1241 intervals = buffer_intervals (buf);
1242 if (nchars < BUF_Z (buf) - BUF_BEG (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 static void
1263 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1264 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1266 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1268 #ifdef BYTE_COMBINING_DEBUG
1269 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1270 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1271 emacs_abort ();
1272 #endif
1274 if (STRINGP (prev_text))
1276 nchars_del = SCHARS (prev_text);
1277 nbytes_del = SBYTES (prev_text);
1280 /* Update various buffer positions for the new text. */
1281 GAP_SIZE -= len_byte;
1282 ZV += len; Z += len;
1283 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1284 GPT += len; GPT_BYTE += len_byte;
1285 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1287 if (nchars_del > 0)
1288 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1289 len, len_byte);
1290 else
1291 adjust_markers_for_insert (from, from_byte,
1292 from + len, from_byte + len_byte, 0);
1294 if (nchars_del > 0)
1295 record_delete (from, prev_text, false);
1296 record_insert (from, len);
1298 if (len > nchars_del)
1299 adjust_overlays_for_insert (from, len - nchars_del);
1300 else if (len < nchars_del)
1301 adjust_overlays_for_delete (from, nchars_del - len);
1303 offset_intervals (current_buffer, from, len - nchars_del);
1305 if (from < PT)
1306 adjust_point (len - nchars_del, len_byte - nbytes_del);
1308 /* As byte combining will decrease Z, we must check this again. */
1309 if (Z - GPT < END_UNCHANGED)
1310 END_UNCHANGED = Z - GPT;
1312 check_markers ();
1314 if (len == 0)
1315 evaporate_overlays (from);
1316 MODIFF++;
1317 CHARS_MODIFF = MODIFF;
1320 /* Record undo information, adjust markers and position keepers for an
1321 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1322 text already exists in the current buffer but character length (TO
1323 - FROM) may be incorrect, the correct length is NEWLEN. */
1325 void
1326 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1327 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1329 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1331 if (GPT != to)
1332 move_gap_both (to, to_byte);
1333 GAP_SIZE += len_byte;
1334 GPT -= len; GPT_BYTE -= len_byte;
1335 ZV -= len; ZV_BYTE -= len_byte;
1336 Z -= len; Z_BYTE -= len_byte;
1337 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1340 /* Replace the text from character positions FROM to TO with NEW,
1341 If PREPARE, call prepare_to_modify_buffer.
1342 If INHERIT, the newly inserted text should inherit text properties
1343 from the surrounding non-deleted text.
1344 If ADJUST_MATCH_DATA, then adjust the match data before calling
1345 signal_after_change. */
1347 /* Note that this does not yet handle markers quite right.
1348 Also it needs to record a single undo-entry that does a replacement
1349 rather than a separate delete and insert.
1350 That way, undo will also handle markers properly.
1352 But if MARKERS is 0, don't relocate markers. */
1354 void
1355 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1356 bool prepare, bool inherit, bool markers,
1357 bool adjust_match_data)
1359 ptrdiff_t inschars = SCHARS (new);
1360 ptrdiff_t insbytes = SBYTES (new);
1361 ptrdiff_t from_byte, to_byte;
1362 ptrdiff_t nbytes_del, nchars_del;
1363 INTERVAL intervals;
1364 ptrdiff_t outgoing_insbytes = insbytes;
1365 Lisp_Object deletion;
1367 check_markers ();
1369 deletion = Qnil;
1371 if (prepare)
1373 ptrdiff_t range_length = to - from;
1374 prepare_to_modify_buffer (from, to, &from);
1375 to = from + range_length;
1378 /* Make args be valid. */
1379 if (from < BEGV)
1380 from = BEGV;
1381 if (to > ZV)
1382 to = ZV;
1384 from_byte = CHAR_TO_BYTE (from);
1385 to_byte = CHAR_TO_BYTE (to);
1387 nchars_del = to - from;
1388 nbytes_del = to_byte - from_byte;
1390 if (nbytes_del <= 0 && insbytes == 0)
1391 return;
1393 /* Make OUTGOING_INSBYTES describe the text
1394 as it will be inserted in this buffer. */
1396 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1397 outgoing_insbytes = inschars;
1398 else if (! STRING_MULTIBYTE (new))
1399 outgoing_insbytes
1400 = count_size_as_multibyte (SDATA (new), insbytes);
1402 /* Make sure the gap is somewhere in or next to what we are deleting. */
1403 if (from > GPT)
1404 gap_right (from, from_byte);
1405 if (to < GPT)
1406 gap_left (to, to_byte, 0);
1408 /* Even if we don't record for undo, we must keep the original text
1409 because we may have to recover it because of inappropriate byte
1410 combining. */
1411 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1412 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1414 GAP_SIZE += nbytes_del;
1415 ZV -= nchars_del;
1416 Z -= nchars_del;
1417 ZV_BYTE -= nbytes_del;
1418 Z_BYTE -= nbytes_del;
1419 GPT = from;
1420 GPT_BYTE = from_byte;
1421 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1423 eassert (GPT <= GPT_BYTE);
1425 if (GPT - BEG < BEG_UNCHANGED)
1426 BEG_UNCHANGED = GPT - BEG;
1427 if (Z - GPT < END_UNCHANGED)
1428 END_UNCHANGED = Z - GPT;
1430 if (GAP_SIZE < outgoing_insbytes)
1431 make_gap (outgoing_insbytes - GAP_SIZE);
1433 /* Copy the string text into the buffer, perhaps converting
1434 between single-byte and multibyte. */
1435 copy_text (SDATA (new), GPT_ADDR, insbytes,
1436 STRING_MULTIBYTE (new),
1437 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1439 #ifdef BYTE_COMBINING_DEBUG
1440 /* We have copied text into the gap, but we have not marked
1441 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1442 here, for both the previous text and the following text.
1443 Meanwhile, GPT_ADDR does point to
1444 the text that has been stored by copy_text. */
1445 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1446 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1447 emacs_abort ();
1448 #endif
1450 /* Record the insertion first, so that when we undo,
1451 the deletion will be undone first. Thus, undo
1452 will insert before deleting, and thus will keep
1453 the markers before and after this text separate. */
1454 if (!NILP (deletion))
1456 record_insert (from + SCHARS (deletion), inschars);
1457 record_delete (from, deletion, false);
1460 GAP_SIZE -= outgoing_insbytes;
1461 GPT += inschars;
1462 ZV += inschars;
1463 Z += inschars;
1464 GPT_BYTE += outgoing_insbytes;
1465 ZV_BYTE += outgoing_insbytes;
1466 Z_BYTE += outgoing_insbytes;
1467 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1469 eassert (GPT <= GPT_BYTE);
1471 /* Adjust markers for the deletion and the insertion. */
1472 if (markers)
1473 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1474 inschars, outgoing_insbytes);
1475 else
1477 /* The character positions of the markers remain intact, but we
1478 still need to update their byte positions, because the
1479 deleted and the inserted text might have multibyte sequences
1480 which make the original byte positions of the markers
1481 invalid. */
1482 adjust_markers_bytepos (from, from_byte, from + inschars,
1483 from_byte + outgoing_insbytes, 1);
1486 /* Adjust the overlay center as needed. This must be done after
1487 adjusting the markers that bound the overlays. */
1488 adjust_overlays_for_delete (from, nchars_del);
1489 adjust_overlays_for_insert (from, inschars);
1491 offset_intervals (current_buffer, from, inschars - nchars_del);
1493 /* Get the intervals for the part of the string we are inserting--
1494 not including the combined-before bytes. */
1495 intervals = string_intervals (new);
1496 /* Insert those intervals. */
1497 graft_intervals_into_buffer (intervals, from, inschars,
1498 current_buffer, inherit);
1500 /* Relocate point as if it were a marker. */
1501 if (from < PT)
1502 adjust_point ((from + inschars - (PT < to ? PT : to)),
1503 (from_byte + outgoing_insbytes
1504 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1506 if (outgoing_insbytes == 0)
1507 evaporate_overlays (from);
1509 check_markers ();
1511 MODIFF++;
1512 CHARS_MODIFF = MODIFF;
1514 if (adjust_match_data)
1515 update_search_regs (from, to, from + SCHARS (new));
1517 signal_after_change (from, nchars_del, GPT - from);
1518 update_compositions (from, GPT, CHECK_BORDER);
1521 /* Replace the text from character positions FROM to TO with
1522 the text in INS of length INSCHARS.
1523 Keep the text properties that applied to the old characters
1524 (extending them to all the new chars if there are more new chars).
1526 Note that this does not yet handle markers quite right.
1528 If MARKERS, relocate markers.
1530 Unlike most functions at this level, never call
1531 prepare_to_modify_buffer and never call signal_after_change. */
1533 void
1534 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1535 ptrdiff_t to, ptrdiff_t to_byte,
1536 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1537 bool markers)
1539 ptrdiff_t nbytes_del, nchars_del;
1541 check_markers ();
1543 nchars_del = to - from;
1544 nbytes_del = to_byte - from_byte;
1546 if (nbytes_del <= 0 && insbytes == 0)
1547 return;
1549 /* Make sure the gap is somewhere in or next to what we are deleting. */
1550 if (from > GPT)
1551 gap_right (from, from_byte);
1552 if (to < GPT)
1553 gap_left (to, to_byte, 0);
1555 GAP_SIZE += nbytes_del;
1556 ZV -= nchars_del;
1557 Z -= nchars_del;
1558 ZV_BYTE -= nbytes_del;
1559 Z_BYTE -= nbytes_del;
1560 GPT = from;
1561 GPT_BYTE = from_byte;
1562 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1564 eassert (GPT <= GPT_BYTE);
1566 if (GPT - BEG < BEG_UNCHANGED)
1567 BEG_UNCHANGED = GPT - BEG;
1568 if (Z - GPT < END_UNCHANGED)
1569 END_UNCHANGED = Z - GPT;
1571 if (GAP_SIZE < insbytes)
1572 make_gap (insbytes - GAP_SIZE);
1574 /* Copy the replacement text into the buffer. */
1575 memcpy (GPT_ADDR, ins, insbytes);
1577 #ifdef BYTE_COMBINING_DEBUG
1578 /* We have copied text into the gap, but we have not marked
1579 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1580 here, for both the previous text and the following text.
1581 Meanwhile, GPT_ADDR does point to
1582 the text that has been stored by copy_text. */
1583 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1584 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1585 emacs_abort ();
1586 #endif
1588 GAP_SIZE -= insbytes;
1589 GPT += inschars;
1590 ZV += inschars;
1591 Z += inschars;
1592 GPT_BYTE += insbytes;
1593 ZV_BYTE += insbytes;
1594 Z_BYTE += insbytes;
1595 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1597 eassert (GPT <= GPT_BYTE);
1599 /* Adjust markers for the deletion and the insertion. */
1600 if (! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1602 if (markers)
1603 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1604 inschars, insbytes);
1605 else
1607 /* The character positions of the markers remain intact, but
1608 we still need to update their byte positions, because the
1609 deleted and the inserted text might have multibyte
1610 sequences which make the original byte positions of the
1611 markers invalid. */
1612 adjust_markers_bytepos (from, from_byte, from + inschars,
1613 from_byte + insbytes, 1);
1617 /* Adjust the overlay center as needed. This must be done after
1618 adjusting the markers that bound the overlays. */
1619 if (nchars_del != inschars)
1621 adjust_overlays_for_insert (from, inschars);
1622 adjust_overlays_for_delete (from + inschars, nchars_del);
1625 offset_intervals (current_buffer, from, inschars - nchars_del);
1627 /* Relocate point as if it were a marker. */
1628 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1630 if (PT < to)
1631 /* PT was within the deleted text. Move it to FROM. */
1632 adjust_point (from - PT, from_byte - PT_BYTE);
1633 else
1634 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1637 if (insbytes == 0)
1638 evaporate_overlays (from);
1640 check_markers ();
1642 MODIFF++;
1643 CHARS_MODIFF = MODIFF;
1646 /* Delete characters in current buffer
1647 from FROM up to (but not including) TO.
1648 If TO comes before FROM, we delete nothing. */
1650 void
1651 del_range (ptrdiff_t from, ptrdiff_t to)
1653 del_range_1 (from, to, 1, 0);
1656 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1657 RET_STRING says to return the deleted text. */
1659 Lisp_Object
1660 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1662 ptrdiff_t from_byte, to_byte;
1663 Lisp_Object deletion;
1665 /* Make args be valid */
1666 if (from < BEGV)
1667 from = BEGV;
1668 if (to > ZV)
1669 to = ZV;
1671 if (to <= from)
1672 return Qnil;
1674 if (prepare)
1676 ptrdiff_t range_length = to - from;
1677 prepare_to_modify_buffer (from, to, &from);
1678 to = min (ZV, from + range_length);
1681 from_byte = CHAR_TO_BYTE (from);
1682 to_byte = CHAR_TO_BYTE (to);
1684 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1685 signal_after_change (from, to - from, 0);
1686 update_compositions (from, from, CHECK_HEAD);
1687 return deletion;
1690 /* Like del_range_1 but args are byte positions, not char positions. */
1692 void
1693 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte)
1695 ptrdiff_t from, to;
1697 /* Make args be valid. */
1698 if (from_byte < BEGV_BYTE)
1699 from_byte = BEGV_BYTE;
1700 if (to_byte > ZV_BYTE)
1701 to_byte = ZV_BYTE;
1703 if (to_byte <= from_byte)
1704 return;
1706 from = BYTE_TO_CHAR (from_byte);
1707 to = BYTE_TO_CHAR (to_byte);
1710 ptrdiff_t old_from = from, old_to = Z - to;
1711 ptrdiff_t range_length = to - from;
1712 prepare_to_modify_buffer (from, to, &from);
1713 to = from + range_length;
1715 if (old_from != from)
1716 from_byte = CHAR_TO_BYTE (from);
1717 if (to > ZV)
1719 to = ZV;
1720 to_byte = ZV_BYTE;
1722 else if (old_to == Z - to)
1723 to_byte = CHAR_TO_BYTE (to);
1726 del_range_2 (from, from_byte, to, to_byte, 0);
1727 signal_after_change (from, to - from, 0);
1728 update_compositions (from, from, CHECK_HEAD);
1731 /* Like del_range_1, but positions are specified both as charpos
1732 and bytepos. */
1734 void
1735 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1736 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1738 /* Make args be valid */
1739 if (from_byte < BEGV_BYTE)
1740 from_byte = BEGV_BYTE;
1741 if (to_byte > ZV_BYTE)
1742 to_byte = ZV_BYTE;
1744 if (to_byte <= from_byte)
1745 return;
1747 if (from < BEGV)
1748 from = BEGV;
1749 if (to > ZV)
1750 to = ZV;
1752 if (prepare)
1754 ptrdiff_t old_from = from, old_to = Z - to;
1755 ptrdiff_t range_length = to - from;
1756 prepare_to_modify_buffer (from, to, &from);
1757 to = from + range_length;
1759 if (old_from != from)
1760 from_byte = CHAR_TO_BYTE (from);
1761 if (to > ZV)
1763 to = ZV;
1764 to_byte = ZV_BYTE;
1766 else if (old_to == Z - to)
1767 to_byte = CHAR_TO_BYTE (to);
1770 del_range_2 (from, from_byte, to, to_byte, 0);
1771 signal_after_change (from, to - from, 0);
1772 update_compositions (from, from, CHECK_HEAD);
1775 /* Delete a range of text, specified both as character positions
1776 and byte positions. FROM and TO are character positions,
1777 while FROM_BYTE and TO_BYTE are byte positions.
1778 If RET_STRING, the deleted area is returned as a string. */
1780 Lisp_Object
1781 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1782 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1784 ptrdiff_t nbytes_del, nchars_del;
1785 Lisp_Object deletion;
1787 check_markers ();
1789 nchars_del = to - from;
1790 nbytes_del = to_byte - from_byte;
1792 /* Make sure the gap is somewhere in or next to what we are deleting. */
1793 if (from > GPT)
1794 gap_right (from, from_byte);
1795 if (to < GPT)
1796 gap_left (to, to_byte, 0);
1798 #ifdef BYTE_COMBINING_DEBUG
1799 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1800 Z_BYTE - to_byte, from, from_byte))
1801 emacs_abort ();
1802 #endif
1804 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1805 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1806 else
1807 deletion = Qnil;
1809 /* Record marker adjustments, and text deletion into undo
1810 history. */
1811 record_delete (from, deletion, true);
1813 /* Relocate all markers pointing into the new, larger gap to point
1814 at the end of the text before the gap. */
1815 adjust_markers_for_delete (from, from_byte, to, to_byte);
1817 MODIFF++;
1818 CHARS_MODIFF = MODIFF;
1820 /* Relocate point as if it were a marker. */
1821 if (from < PT)
1822 adjust_point (from - (PT < to ? PT : to),
1823 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1825 offset_intervals (current_buffer, from, - nchars_del);
1827 /* Adjust the overlay center as needed. This must be done after
1828 adjusting the markers that bound the overlays. */
1829 adjust_overlays_for_delete (from, nchars_del);
1831 GAP_SIZE += nbytes_del;
1832 ZV_BYTE -= nbytes_del;
1833 Z_BYTE -= nbytes_del;
1834 ZV -= nchars_del;
1835 Z -= nchars_del;
1836 GPT = from;
1837 GPT_BYTE = from_byte;
1838 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1839 /* Put an anchor, unless called from decode_coding_object which
1840 needs to access the previous gap contents. */
1841 *(GPT_ADDR) = 0;
1843 eassert (GPT <= GPT_BYTE);
1845 if (GPT - BEG < BEG_UNCHANGED)
1846 BEG_UNCHANGED = GPT - BEG;
1847 if (Z - GPT < END_UNCHANGED)
1848 END_UNCHANGED = Z - GPT;
1850 check_markers ();
1852 evaporate_overlays (from);
1854 return deletion;
1857 /* Call this if you're about to change the text of current buffer
1858 from character positions START to END. This checks the read-only
1859 properties of the region, calls the necessary modification hooks,
1860 and warns the next redisplay that it should pay attention to that
1861 area. */
1863 void
1864 modify_text (ptrdiff_t start, ptrdiff_t end)
1866 prepare_to_modify_buffer (start, end, NULL);
1868 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1869 if (MODIFF <= SAVE_MODIFF)
1870 record_first_change ();
1871 MODIFF++;
1872 CHARS_MODIFF = MODIFF;
1874 bset_point_before_scroll (current_buffer, Qnil);
1877 /* Signal that we are about to make a change that may result in new
1878 undo information.
1880 static void
1881 run_undoable_change (void)
1883 if (EQ (BVAR (current_buffer, undo_list), Qt))
1884 return;
1886 call0 (Qundo_auto__undoable_change);
1889 /* Check that it is okay to modify the buffer between START and END,
1890 which are char positions.
1892 Run the before-change-function, if any. If intervals are in use,
1893 verify that the text to be modified is not read-only, and call
1894 any modification properties the text may have.
1896 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1897 by holding its value temporarily in a marker.
1899 This function runs Lisp, which means it can GC, which means it can
1900 compact buffers, including the current buffer being worked on here.
1901 So don't you dare calling this function while manipulating the gap,
1902 or during some other similar "critical section". */
1904 void
1905 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1906 ptrdiff_t *preserve_ptr)
1908 struct buffer *base_buffer;
1909 Lisp_Object temp;
1911 XSETFASTINT (temp, start);
1912 if (!NILP (BVAR (current_buffer, read_only)))
1913 Fbarf_if_buffer_read_only (temp);
1915 run_undoable_change();
1917 bset_redisplay (current_buffer);
1919 if (buffer_intervals (current_buffer))
1921 if (preserve_ptr)
1923 Lisp_Object preserve_marker;
1924 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1925 verify_interval_modification (current_buffer, start, end);
1926 *preserve_ptr = marker_position (preserve_marker);
1927 unchain_marker (XMARKER (preserve_marker));
1929 else
1930 verify_interval_modification (current_buffer, start, end);
1933 /* For indirect buffers, use the base buffer to check clashes. */
1934 if (current_buffer->base_buffer != 0)
1935 base_buffer = current_buffer->base_buffer;
1936 else
1937 base_buffer = current_buffer;
1939 if (inhibit_modification_hooks)
1940 return;
1942 if (!NILP (BVAR (base_buffer, file_truename))
1943 /* Make binding buffer-file-name to nil effective. */
1944 && !NILP (BVAR (base_buffer, filename))
1945 && SAVE_MODIFF >= MODIFF)
1946 lock_file (BVAR (base_buffer, file_truename));
1948 /* If `select-active-regions' is non-nil, save the region text. */
1949 /* FIXME: Move this to Elisp (via before-change-functions). */
1950 if (!NILP (BVAR (current_buffer, mark_active))
1951 && XMARKER (BVAR (current_buffer, mark))->buffer
1952 && NILP (Vsaved_region_selection)
1953 && (EQ (Vselect_active_regions, Qonly)
1954 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1955 : (!NILP (Vselect_active_regions)
1956 && !NILP (Vtransient_mark_mode))))
1957 Vsaved_region_selection
1958 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1960 signal_before_change (start, end, preserve_ptr);
1961 Fset (Qdeactivate_mark, Qt);
1964 /* Like above, but called when we know that the buffer text
1965 will be modified and region caches should be invalidated. */
1967 void
1968 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1969 ptrdiff_t *preserve_ptr)
1971 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1972 invalidate_buffer_caches (current_buffer, start, end);
1975 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1976 region between buffer positions START and END. */
1977 void
1978 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1980 /* Indirect buffers usually have their caches set to NULL, but we
1981 need to consider the caches of their base buffer. */
1982 if (buf->base_buffer)
1983 buf = buf->base_buffer;
1984 /* The bidi_paragraph_cache must be invalidated first, because doing
1985 so might need to use the newline_cache (via find_newline_no_quit,
1986 see below). */
1987 if (buf->bidi_paragraph_cache)
1989 if (start != end
1990 && start > BUF_BEG (buf))
1992 /* If we are deleting or replacing characters, we could
1993 create a paragraph start, because all of the characters
1994 from START to the beginning of START's line are
1995 whitespace. Therefore, we must extend the region to be
1996 invalidated up to the newline before START. */
1997 ptrdiff_t line_beg = start;
1998 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
2000 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
2002 struct buffer *old = current_buffer;
2004 set_buffer_internal (buf);
2006 line_beg = find_newline_no_quit (start, start_byte, -1,
2007 &start_byte);
2008 set_buffer_internal (old);
2010 start = line_beg - (line_beg > BUF_BEG (buf));
2012 invalidate_region_cache (buf,
2013 buf->bidi_paragraph_cache,
2014 start - BUF_BEG (buf), BUF_Z (buf) - end);
2016 if (buf->newline_cache)
2017 invalidate_region_cache (buf,
2018 buf->newline_cache,
2019 start - BUF_BEG (buf), BUF_Z (buf) - end);
2020 if (buf->width_run_cache)
2021 invalidate_region_cache (buf,
2022 buf->width_run_cache,
2023 start - BUF_BEG (buf), BUF_Z (buf) - end);
2026 /* These macros work with an argument named `preserve_ptr'
2027 and a local variable named `preserve_marker'. */
2029 #define PRESERVE_VALUE \
2030 if (preserve_ptr && NILP (preserve_marker)) \
2031 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2033 #define RESTORE_VALUE \
2034 if (! NILP (preserve_marker)) \
2036 *preserve_ptr = marker_position (preserve_marker); \
2037 unchain_marker (XMARKER (preserve_marker)); \
2040 #define PRESERVE_START_END \
2041 if (NILP (start_marker)) \
2042 start_marker = Fcopy_marker (start, Qnil); \
2043 if (NILP (end_marker)) \
2044 end_marker = Fcopy_marker (end, Qnil);
2046 #define FETCH_START \
2047 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2049 #define FETCH_END \
2050 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2052 /* Set a variable to nil if an error occurred.
2053 Don't change the variable if there was no error.
2054 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2055 VARIABLE is the variable to maybe set to nil.
2056 NO-ERROR-FLAG is nil if there was an error,
2057 anything else meaning no error (so this function does nothing). */
2058 struct rvoe_arg
2060 Lisp_Object *location;
2061 bool errorp;
2064 static void
2065 reset_var_on_error (void *ptr)
2067 struct rvoe_arg *p = ptr;
2068 if (p->errorp)
2069 *p->location = Qnil;
2072 /* Signal a change to the buffer immediately before it happens.
2073 START_INT and END_INT are the bounds of the text to be changed.
2075 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2076 by holding its value temporarily in a marker. */
2078 static void
2079 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
2080 ptrdiff_t *preserve_ptr)
2082 Lisp_Object start, end;
2083 Lisp_Object start_marker, end_marker;
2084 Lisp_Object preserve_marker;
2085 ptrdiff_t count = SPECPDL_INDEX ();
2086 struct rvoe_arg rvoe_arg;
2088 start = make_number (start_int);
2089 end = make_number (end_int);
2090 preserve_marker = Qnil;
2091 start_marker = Qnil;
2092 end_marker = Qnil;
2094 specbind (Qinhibit_modification_hooks, Qt);
2096 /* If buffer is unmodified, run a special hook for that case. The
2097 check for Vfirst_change_hook is just a minor optimization. */
2098 if (SAVE_MODIFF >= MODIFF
2099 && !NILP (Vfirst_change_hook))
2101 PRESERVE_VALUE;
2102 PRESERVE_START_END;
2103 run_hook (Qfirst_change_hook);
2106 /* Now run the before-change-functions if any. */
2107 if (!NILP (Vbefore_change_functions))
2109 rvoe_arg.location = &Vbefore_change_functions;
2110 rvoe_arg.errorp = 1;
2112 PRESERVE_VALUE;
2113 PRESERVE_START_END;
2115 /* Mark before-change-functions to be reset to nil in case of error. */
2116 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2118 /* Actually run the hook functions. */
2119 CALLN (Frun_hook_with_args, Qbefore_change_functions,
2120 FETCH_START, FETCH_END);
2122 /* There was no error: unarm the reset_on_error. */
2123 rvoe_arg.errorp = 0;
2126 if (buffer_has_overlays ())
2128 PRESERVE_VALUE;
2129 report_overlay_modification (FETCH_START, FETCH_END, 0,
2130 FETCH_START, FETCH_END, Qnil);
2133 if (! NILP (start_marker))
2134 free_marker (start_marker);
2135 if (! NILP (end_marker))
2136 free_marker (end_marker);
2137 RESTORE_VALUE;
2139 unbind_to (count, Qnil);
2142 /* Signal a change immediately after it happens.
2143 CHARPOS is the character position of the start of the changed text.
2144 LENDEL is the number of characters of the text before the change.
2145 (Not the whole buffer; just the part that was changed.)
2146 LENINS is the number of characters in that part of the text
2147 after the change. */
2149 void
2150 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2152 ptrdiff_t count = SPECPDL_INDEX ();
2153 struct rvoe_arg rvoe_arg;
2155 if (inhibit_modification_hooks)
2156 return;
2158 /* If we are deferring calls to the after-change functions
2159 and there are no before-change functions,
2160 just record the args that we were going to use. */
2161 if (! NILP (Vcombine_after_change_calls)
2162 && NILP (Vbefore_change_functions)
2163 && !buffer_has_overlays ())
2165 Lisp_Object elt;
2167 if (!NILP (combine_after_change_list)
2168 && current_buffer != XBUFFER (combine_after_change_buffer))
2169 Fcombine_after_change_execute ();
2171 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2172 lenins - lendel);
2173 combine_after_change_list
2174 = Fcons (elt, combine_after_change_list);
2175 combine_after_change_buffer = Fcurrent_buffer ();
2177 return;
2180 if (!NILP (combine_after_change_list))
2181 Fcombine_after_change_execute ();
2183 specbind (Qinhibit_modification_hooks, Qt);
2185 if (!NILP (Vafter_change_functions))
2187 rvoe_arg.location = &Vafter_change_functions;
2188 rvoe_arg.errorp = 1;
2190 /* Mark after-change-functions to be reset to nil in case of error. */
2191 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2193 /* Actually run the hook functions. */
2194 CALLN (Frun_hook_with_args, Qafter_change_functions,
2195 make_number (charpos), make_number (charpos + lenins),
2196 make_number (lendel));
2198 /* There was no error: unarm the reset_on_error. */
2199 rvoe_arg.errorp = 0;
2202 if (buffer_has_overlays ())
2203 report_overlay_modification (make_number (charpos),
2204 make_number (charpos + lenins),
2206 make_number (charpos),
2207 make_number (charpos + lenins),
2208 make_number (lendel));
2210 /* After an insertion, call the text properties
2211 insert-behind-hooks or insert-in-front-hooks. */
2212 if (lendel == 0)
2213 report_interval_modification (make_number (charpos),
2214 make_number (charpos + lenins));
2216 unbind_to (count, Qnil);
2219 static void
2220 Fcombine_after_change_execute_1 (Lisp_Object val)
2222 Vcombine_after_change_calls = val;
2225 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2226 Scombine_after_change_execute, 0, 0, 0,
2227 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2228 (void)
2230 ptrdiff_t count = SPECPDL_INDEX ();
2231 ptrdiff_t beg, end, change;
2232 ptrdiff_t begpos, endpos;
2233 Lisp_Object tail;
2235 if (NILP (combine_after_change_list))
2236 return Qnil;
2238 /* It is rare for combine_after_change_buffer to be invalid, but
2239 possible. It can happen when combine-after-change-calls is
2240 non-nil, and insertion calls a file handler (e.g. through
2241 lock_file) which scribbles into a temp file -- cyd */
2242 if (!BUFFERP (combine_after_change_buffer)
2243 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2245 combine_after_change_list = Qnil;
2246 return Qnil;
2249 record_unwind_current_buffer ();
2251 Fset_buffer (combine_after_change_buffer);
2253 /* # chars unchanged at beginning of buffer. */
2254 beg = Z - BEG;
2255 /* # chars unchanged at end of buffer. */
2256 end = beg;
2257 /* Total amount of insertion (negative for deletion). */
2258 change = 0;
2260 /* Scan the various individual changes,
2261 accumulating the range info in BEG, END and CHANGE. */
2262 for (tail = combine_after_change_list; CONSP (tail);
2263 tail = XCDR (tail))
2265 Lisp_Object elt;
2266 ptrdiff_t thisbeg, thisend, thischange;
2268 /* Extract the info from the next element. */
2269 elt = XCAR (tail);
2270 if (! CONSP (elt))
2271 continue;
2272 thisbeg = XINT (XCAR (elt));
2274 elt = XCDR (elt);
2275 if (! CONSP (elt))
2276 continue;
2277 thisend = XINT (XCAR (elt));
2279 elt = XCDR (elt);
2280 if (! CONSP (elt))
2281 continue;
2282 thischange = XINT (XCAR (elt));
2284 /* Merge this range into the accumulated range. */
2285 change += thischange;
2286 if (thisbeg < beg)
2287 beg = thisbeg;
2288 if (thisend < end)
2289 end = thisend;
2292 /* Get the current start and end positions of the range
2293 that was changed. */
2294 begpos = BEG + beg;
2295 endpos = Z - end;
2297 /* We are about to handle these, so discard them. */
2298 combine_after_change_list = Qnil;
2300 /* Now run the after-change functions for real.
2301 Turn off the flag that defers them. */
2302 record_unwind_protect (Fcombine_after_change_execute_1,
2303 Vcombine_after_change_calls);
2304 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2305 update_compositions (begpos, endpos, CHECK_ALL);
2307 return unbind_to (count, Qnil);
2310 void
2311 syms_of_insdel (void)
2313 staticpro (&combine_after_change_list);
2314 staticpro (&combine_after_change_buffer);
2315 combine_after_change_list = Qnil;
2316 combine_after_change_buffer = Qnil;
2318 DEFSYM (Qundo_auto__undoable_change, "undo-auto--undoable-change");
2320 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2321 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2322 Vcombine_after_change_calls = Qnil;
2324 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2325 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2326 This affects `before-change-functions' and `after-change-functions',
2327 as well as hooks attached to text properties and overlays. */);
2328 inhibit_modification_hooks = 0;
2329 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2331 DEFSYM (Qregion_extract_function, "region-extract-function");
2333 defsubr (&Scombine_after_change_execute);