Drop FRAME_PTR typedef.
[bpt/emacs.git] / src / insdel.c
CommitLineData
b45433b3 1/* Buffer insertion/deletion and gap motion for GNU Emacs.
ab422c4d
PE
2 Copyright (C) 1985-1986, 1993-1995, 1997-2013 Free Software
3 Foundation, Inc.
b45433b3
JB
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
b45433b3 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
b45433b3
JB
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
b45433b3
JB
19
20
18160b98 21#include <config.h>
06d6db33
PE
22
23#include <intprops.h>
24
b45433b3 25#include "lisp.h"
679194a6 26#include "intervals.h"
811f7370 27#include "character.h"
e5560ff7 28#include "buffer.h"
b45433b3 29#include "window.h"
d014bf88 30#include "blockinput.h"
dfcf069d 31#include "region-cache.h"
b45433b3 32
a08d4ba7
PE
33static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37static void gap_right (ptrdiff_t, ptrdiff_t);
395ec62e 38
fb2e7d14
RS
39/* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
a08d4ba7 41 was non-nil. We use this to decide how to call them
fb2e7d14
RS
42 once the deferral ends.
43
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). */
77382fcc 49static Lisp_Object combine_after_change_list;
fb2e7d14
RS
50
51/* Buffer which combine_after_change_list is about. */
77382fcc 52static Lisp_Object combine_after_change_buffer;
27ea0af2
GM
53
54Lisp_Object Qinhibit_modification_hooks;
4889fc82 55
d311d28c 56static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
e2688e4a
DA
57
58/* Also used in marker.c to enable expensive marker checks. */
59
60#ifdef MARKER_DEBUG
60ea6052 61
85876d07 62static void
971de7fb 63check_markers (void)
60ea6052 64{
a08d4ba7
PE
65 struct Lisp_Marker *tail;
66 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
60ea6052 67
dab0b04d 68 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
60ea6052 69 {
dab0b04d 70 if (tail->buffer->text != current_buffer->text)
1088b922 71 emacs_abort ();
dab0b04d 72 if (tail->charpos > Z)
1088b922 73 emacs_abort ();
dab0b04d 74 if (tail->bytepos > Z_BYTE)
1088b922 75 emacs_abort ();
dab0b04d 76 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
1088b922 77 emacs_abort ();
60ea6052
RS
78 }
79}
e2688e4a
DA
80
81#else /* not MARKER_DEBUG */
82
83#define check_markers() do { } while (0)
84
85#endif /* MARKER_DEBUG */
86
3be11131
RS
87/* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
88 Note that this can quit! */
89
90void
d311d28c 91move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
3be11131 92{
6020559a
DA
93 eassert (charpos == BYTE_TO_CHAR (bytepos)
94 && bytepos == CHAR_TO_BYTE (charpos));
3be11131
RS
95 if (bytepos < GPT_BYTE)
96 gap_left (charpos, bytepos, 0);
97 else if (bytepos > GPT_BYTE)
98 gap_right (charpos, bytepos);
99}
100
101/* Move the gap to a position less than the current GPT.
102 BYTEPOS describes the new position as a byte position,
103 and CHARPOS is the corresponding char position.
a08d4ba7 104 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
b45433b3 105
2bcaed71 106static void
a08d4ba7 107gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
b45433b3 108{
a08d4ba7
PE
109 unsigned char *to, *from;
110 ptrdiff_t i;
d311d28c 111 ptrdiff_t new_s1;
b45433b3 112
b45433b3 113 if (!newgap)
a50df55b 114 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
b45433b3 115
3be11131 116 i = GPT_BYTE;
b45433b3
JB
117 to = GAP_END_ADDR;
118 from = GPT_ADDR;
3be11131 119 new_s1 = GPT_BYTE;
b45433b3
JB
120
121 /* Now copy the characters. To move the gap down,
122 copy characters up. */
123
124 while (1)
125 {
126 /* I gets number of characters left to copy. */
3be11131 127 i = new_s1 - bytepos;
b45433b3
JB
128 if (i == 0)
129 break;
130 /* If a quit is requested, stop copying now.
3be11131 131 Change BYTEPOS to be where we have actually moved the gap to. */
b45433b3
JB
132 if (QUITP)
133 {
3be11131
RS
134 bytepos = new_s1;
135 charpos = BYTE_TO_CHAR (bytepos);
b45433b3
JB
136 break;
137 }
138 /* Move at most 32000 chars before checking again for a quit. */
139 if (i > 32000)
140 i = 32000;
72af86bd
AS
141 new_s1 -= i;
142 from -= i, to -= i;
143 memmove (to, from, i);
b45433b3
JB
144 }
145
f868cd8a
JB
146 /* Adjust buffer data structure, to put the gap at BYTEPOS.
147 BYTEPOS is where the loop above stopped, which may be what
148 was specified or may be where a quit was detected. */
3be11131
RS
149 GPT_BYTE = bytepos;
150 GPT = charpos;
e2688e4a 151 eassert (charpos <= bytepos);
469ff680 152 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
b45433b3
JB
153 QUIT;
154}
155
2b34df4e 156/* Move the gap to a position greater than the current GPT.
3be11131
RS
157 BYTEPOS describes the new position as a byte position,
158 and CHARPOS is the corresponding char position. */
159
2bcaed71 160static void
d311d28c 161gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
b45433b3
JB
162{
163 register unsigned char *to, *from;
d311d28c
PE
164 register ptrdiff_t i;
165 ptrdiff_t new_s1;
b45433b3 166
a50df55b 167 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
b45433b3 168
3be11131 169 i = GPT_BYTE;
b45433b3
JB
170 from = GAP_END_ADDR;
171 to = GPT_ADDR;
3be11131 172 new_s1 = GPT_BYTE;
b45433b3
JB
173
174 /* Now copy the characters. To move the gap up,
175 copy characters down. */
176
177 while (1)
178 {
179 /* I gets number of characters left to copy. */
3be11131 180 i = bytepos - new_s1;
b45433b3
JB
181 if (i == 0)
182 break;
183 /* If a quit is requested, stop copying now.
3be11131 184 Change BYTEPOS to be where we have actually moved the gap to. */
b45433b3
JB
185 if (QUITP)
186 {
3be11131
RS
187 bytepos = new_s1;
188 charpos = BYTE_TO_CHAR (bytepos);
b45433b3
JB
189 break;
190 }
191 /* Move at most 32000 chars before checking again for a quit. */
192 if (i > 32000)
193 i = 32000;
72af86bd
AS
194 new_s1 += i;
195 memmove (to, from, i);
196 from += i, to += i;
b45433b3
JB
197 }
198
3be11131
RS
199 GPT = charpos;
200 GPT_BYTE = bytepos;
e2688e4a 201 eassert (charpos <= bytepos);
469ff680 202 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
b45433b3
JB
203 QUIT;
204}
2b083808 205\f
3be11131
RS
206/* Adjust all markers for a deletion
207 whose range in bytes is FROM_BYTE to TO_BYTE.
208 The range in charpos is FROM to TO.
209
210 This function assumes that the gap is adjacent to
211 or inside of the range being deleted. */
beecb55b 212
809f3d51 213void
d311d28c
PE
214adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
215 ptrdiff_t to, ptrdiff_t to_byte)
3be11131
RS
216{
217 Lisp_Object marker;
218 register struct Lisp_Marker *m;
d311d28c 219 register ptrdiff_t charpos;
3be11131 220
dab0b04d 221 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
3be11131 222 {
3be11131 223 charpos = m->charpos;
e2688e4a 224 eassert (charpos <= Z);
3be11131
RS
225
226 /* If the marker is after the deletion,
80f6e77c 227 relocate by number of chars / bytes deleted. */
3be11131 228 if (charpos > to)
80f6e77c
RS
229 {
230 m->charpos -= to - from;
231 m->bytepos -= to_byte - from_byte;
232 }
80f6e77c 233 /* Here's the case where a marker is inside text being deleted. */
3be11131
RS
234 else if (charpos > from)
235 {
00de2987 236 if (! m->insertion_type)
dab0b04d 237 { /* Normal markers will end up at the beginning of the
00de2987 238 re-inserted text after undoing a deletion, and must be
177c0ea7 239 adjusted to move them to the correct place. */
dab0b04d 240 XSETMISC (marker, m);
28259cac 241 record_marker_adjustment (marker, from - charpos);
dab0b04d 242 }
00de2987 243 else if (charpos < to)
dab0b04d 244 { /* Before-insertion markers will automatically move forward
00de2987
MB
245 upon re-inserting the deleted text, so we have to arrange
246 for them to move backward to the correct position. */
dab0b04d 247 XSETMISC (marker, m);
28259cac 248 record_marker_adjustment (marker, to - charpos);
dab0b04d 249 }
3be11131 250 m->charpos = from;
80f6e77c 251 m->bytepos = from_byte;
3be11131 252 }
00de2987
MB
253 /* Here's the case where a before-insertion marker is immediately
254 before the deleted region. */
255 else if (charpos == from && m->insertion_type)
256 {
257 /* Undoing the change uses normal insertion, which will
258 incorrectly make MARKER move forward, so we arrange for it
259 to then move backward to the correct place at the beginning
260 of the deleted region. */
dab0b04d 261 XSETMISC (marker, m);
00de2987
MB
262 record_marker_adjustment (marker, to - from);
263 }
3be11131
RS
264 }
265}
23b20a70 266
2b083808 267\f
432f78d2
RS
268/* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
269 to TO / TO_BYTE. We have to relocate the charpos of every marker
270 that points after the insertion (but not their bytepos).
3be11131 271
3be11131
RS
272 When a marker points at the insertion point,
273 we advance it if either its insertion-type is t
274 or BEFORE_MARKERS is true. */
275
276static void
d311d28c 277adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
a08d4ba7 278 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
beecb55b 279{
dab0b04d 280 struct Lisp_Marker *m;
a08d4ba7 281 bool adjusted = 0;
d311d28c
PE
282 ptrdiff_t nchars = to - from;
283 ptrdiff_t nbytes = to_byte - from_byte;
beecb55b 284
dab0b04d 285 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
beecb55b 286 {
86a56ef3
SM
287 eassert (m->bytepos >= m->charpos
288 && m->bytepos - m->charpos <= Z_BYTE - Z);
1f90a790 289
432f78d2 290 if (m->bytepos == from_byte)
469ff680 291 {
432f78d2
RS
292 if (m->insertion_type || before_markers)
293 {
b16afa45
KH
294 m->bytepos = to_byte;
295 m->charpos = to;
432f78d2
RS
296 if (m->insertion_type)
297 adjusted = 1;
298 }
ce97a2d7 299 }
80f6e77c
RS
300 else if (m->bytepos > from_byte)
301 {
302 m->bytepos += nbytes;
b16afa45 303 m->charpos += nchars;
80f6e77c 304 }
beecb55b 305 }
3be11131
RS
306
307 /* Adjusting only markers whose insertion-type is t may result in
409f2919 308 - disordered start and end in overlays, and
6b61353c 309 - disordered overlays in the slot `overlays_before' of current_buffer. */
469ff680 310 if (adjusted)
6b61353c 311 {
5e617bc2 312 fix_start_end_in_overlays (from, to);
6b61353c
KH
313 fix_overlays_before (current_buffer, from, to);
314 }
beecb55b
RS
315}
316
3be11131
RS
317/* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
318
319 This is used only when the value of point changes due to an insert
320 or delete; it does not represent a conceptual change in point as a
321 marker. In particular, point is not crossing any interval
322 boundaries, so there's no need to use the usual SET_PT macro. In
323 fact it would be incorrect to do so, because either the old or the
324 new value of point is out of sync with the current set of
325 intervals. */
326
a27a38d8 327static void
d311d28c 328adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
a27a38d8 329{
cffc6f3b 330 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
3be11131 331 /* In a single-byte buffer, the two positions must be equal. */
86a56ef3 332 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
a27a38d8 333}
b45433b3 334\f
652838b5
KH
335/* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
336 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
adee4f91
KH
337 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
338 an insertion. */
652838b5
KH
339
340static void
d311d28c
PE
341adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
342 ptrdiff_t old_chars, ptrdiff_t old_bytes,
343 ptrdiff_t new_chars, ptrdiff_t new_bytes)
652838b5 344{
dab0b04d 345 register struct Lisp_Marker *m;
d311d28c
PE
346 ptrdiff_t prev_to_byte = from_byte + old_bytes;
347 ptrdiff_t diff_chars = new_chars - old_chars;
348 ptrdiff_t diff_bytes = new_bytes - old_bytes;
652838b5 349
dab0b04d 350 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
652838b5 351 {
adee4f91 352 if (m->bytepos >= prev_to_byte)
652838b5 353 {
adee4f91
KH
354 m->charpos += diff_chars;
355 m->bytepos += diff_bytes;
652838b5 356 }
adee4f91 357 else if (m->bytepos > from_byte)
652838b5 358 {
652838b5 359 m->charpos = from;
b16afa45 360 m->bytepos = from_byte;
652838b5 361 }
652838b5 362 }
3019692c 363
e2688e4a 364 check_markers ();
652838b5
KH
365}
366
367\f
99561444
PE
368void
369buffer_overflow (void)
370{
371 error ("Maximum buffer size exceeded");
372}
373
3be11131 374/* Make the gap NBYTES_ADDED bytes longer. */
b45433b3 375
85876d07 376static void
d311d28c 377make_gap_larger (ptrdiff_t nbytes_added)
b45433b3 378{
b45433b3 379 Lisp_Object tem;
d311d28c
PE
380 ptrdiff_t real_gap_loc;
381 ptrdiff_t real_gap_loc_byte;
382 ptrdiff_t old_gap_size;
383 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
b45433b3 384
1c8e352f
PE
385 if (BUF_BYTES_MAX - current_size < nbytes_added)
386 buffer_overflow ();
b45433b3 387
1c8e352f
PE
388 /* If we have to get more space, get enough to last a while;
389 but do not exceed the maximum buffer size. */
eefd7278 390 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
1c8e352f 391 BUF_BYTES_MAX - current_size);
94056516 392
71a7bfa7 393 enlarge_buffer_text (current_buffer, nbytes_added);
b45433b3
JB
394
395 /* Prevent quitting in move_gap. */
396 tem = Vinhibit_quit;
397 Vinhibit_quit = Qt;
398
399 real_gap_loc = GPT;
3be11131 400 real_gap_loc_byte = GPT_BYTE;
b45433b3
JB
401 old_gap_size = GAP_SIZE;
402
403 /* Call the newly allocated space a gap at the end of the whole space. */
404 GPT = Z + GAP_SIZE;
7d92db58 405 GPT_BYTE = Z_BYTE + GAP_SIZE;
3be11131 406 GAP_SIZE = nbytes_added;
b45433b3 407
2eae9ccc 408 /* Move the new gap down to be consecutive with the end of the old one. */
3be11131 409 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
b45433b3
JB
410
411 /* Now combine the two into one large gap. */
412 GAP_SIZE += old_gap_size;
413 GPT = real_gap_loc;
3be11131 414 GPT_BYTE = real_gap_loc_byte;
b45433b3 415
469ff680
KH
416 /* Put an anchor. */
417 *(Z_ADDR) = 0;
418
b45433b3
JB
419 Vinhibit_quit = tem;
420}
8af5b8e7 421
b58c5c4a 422#if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
8af5b8e7 423
7273faa1 424/* Make the gap NBYTES_REMOVED bytes shorter. */
8af5b8e7 425
85876d07 426static void
d311d28c 427make_gap_smaller (ptrdiff_t nbytes_removed)
8af5b8e7
AI
428{
429 Lisp_Object tem;
d311d28c
PE
430 ptrdiff_t real_gap_loc;
431 ptrdiff_t real_gap_loc_byte;
432 ptrdiff_t real_Z;
433 ptrdiff_t real_Z_byte;
434 ptrdiff_t real_beg_unchanged;
435 ptrdiff_t new_gap_size;
8af5b8e7 436
eefd7278
DA
437 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
438 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
439 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
8af5b8e7
AI
440
441 /* Prevent quitting in move_gap. */
442 tem = Vinhibit_quit;
443 Vinhibit_quit = Qt;
444
445 real_gap_loc = GPT;
446 real_gap_loc_byte = GPT_BYTE;
73df3b72 447 new_gap_size = GAP_SIZE - nbytes_removed;
8af5b8e7
AI
448 real_Z = Z;
449 real_Z_byte = Z_BYTE;
73df3b72 450 real_beg_unchanged = BEG_UNCHANGED;
8af5b8e7
AI
451
452 /* Pretend that the last unwanted part of the gap is the entire gap,
453 and that the first desired part of the gap is part of the buffer
454 text. */
72af86bd 455 memset (GPT_ADDR, 0, new_gap_size);
73df3b72
KS
456 GPT += new_gap_size;
457 GPT_BYTE += new_gap_size;
458 Z += new_gap_size;
459 Z_BYTE += new_gap_size;
8af5b8e7
AI
460 GAP_SIZE = nbytes_removed;
461
2eae9ccc 462 /* Move the unwanted pretend gap to the end of the buffer. */
8af5b8e7
AI
463 gap_right (Z, Z_BYTE);
464
465 enlarge_buffer_text (current_buffer, -nbytes_removed);
466
467 /* Now restore the desired gap. */
73df3b72 468 GAP_SIZE = new_gap_size;
8af5b8e7
AI
469 GPT = real_gap_loc;
470 GPT_BYTE = real_gap_loc_byte;
471 Z = real_Z;
472 Z_BYTE = real_Z_byte;
73df3b72 473 BEG_UNCHANGED = real_beg_unchanged;
8af5b8e7
AI
474
475 /* Put an anchor. */
476 *(Z_ADDR) = 0;
477
478 Vinhibit_quit = tem;
479}
480
b58c5c4a
PE
481#endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
482
8af5b8e7 483void
d311d28c 484make_gap (ptrdiff_t nbytes_added)
8af5b8e7
AI
485{
486 if (nbytes_added >= 0)
487 make_gap_larger (nbytes_added);
d77fbc16 488#if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
8af5b8e7
AI
489 else
490 make_gap_smaller (-nbytes_added);
491#endif
492}
eefd7278
DA
493
494/* Add NBYTES to B's gap. It's enough to temporarily
495 fake current_buffer and avoid real switch to B. */
496
497void
498make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
499{
500 struct buffer *oldb = current_buffer;
501
502 current_buffer = b;
503 make_gap (nbytes);
504 current_buffer = oldb;
505}
506
2b083808
RS
507/* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
508 FROM_MULTIBYTE says whether the incoming text is multibyte.
509 TO_MULTIBYTE says whether to store the text as multibyte.
510 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
511
512 Return the number of bytes stored at TO_ADDR. */
513
d311d28c 514ptrdiff_t
ae19ba7c 515copy_text (const unsigned char *from_addr, unsigned char *to_addr,
a08d4ba7 516 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
2b083808
RS
517{
518 if (from_multibyte == to_multibyte)
519 {
72af86bd 520 memcpy (to_addr, from_addr, nbytes);
2b083808
RS
521 return nbytes;
522 }
523 else if (from_multibyte)
524 {
d311d28c
PE
525 ptrdiff_t nchars = 0;
526 ptrdiff_t bytes_left = nbytes;
f44cbcd8 527
2b083808
RS
528 while (bytes_left > 0)
529 {
b16afa45 530 int thislen, c;
62a6e103 531 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
1ede3eb6
KH
532 if (! ASCII_CHAR_P (c))
533 c &= 0xFF;
f44cbcd8 534 *to_addr++ = c;
2b083808 535 from_addr += thislen;
7e79b8e0 536 bytes_left -= thislen;
2b083808
RS
537 nchars++;
538 }
539 return nchars;
540 }
541 else
542 {
543 unsigned char *initial_to_addr = to_addr;
544
545 /* Convert single-byte to multibyte. */
546 while (nbytes > 0)
547 {
548 int c = *from_addr++;
2b083808 549
4c0354d7 550 if (!ASCII_CHAR_P (c))
2b083808 551 {
4c0354d7 552 c = BYTE8_TO_CHAR (c);
0ef71121 553 to_addr += CHAR_STRING (c, to_addr);
2b083808
RS
554 nbytes--;
555 }
556 else
557 /* Special case for speed. */
558 *to_addr++ = c, nbytes--;
559 }
560 return to_addr - initial_to_addr;
561 }
562}
2b083808 563\f
b45433b3 564/* Insert a string of specified length before point.
2b083808
RS
565 This function judges multibyteness based on
566 enable_multibyte_characters in the current buffer;
567 it never converts between single-byte and multibyte.
568
ef29f213
KH
569 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
570 prepare_to_modify_buffer could relocate the text. */
b45433b3 571
c660b094 572void
d311d28c 573insert (const char *string, ptrdiff_t nbytes)
b45433b3 574{
3be11131 575 if (nbytes > 0)
395ec62e 576 {
d311d28c 577 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
bab3eee1
EZ
578 insert_1_both (string, len, nbytes, 0, 1, 0);
579 opoint = PT - len;
580 signal_after_change (opoint, 0, len);
0ef71121 581 update_compositions (opoint, PT, CHECK_BORDER);
cd11ef31
RS
582 }
583}
584
2b083808
RS
585/* Likewise, but inherit text properties from neighboring characters. */
586
c660b094 587void
d311d28c 588insert_and_inherit (const char *string, ptrdiff_t nbytes)
cd11ef31 589{
3be11131 590 if (nbytes > 0)
cd11ef31 591 {
d311d28c 592 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
bab3eee1
EZ
593 insert_1_both (string, len, nbytes, 1, 1, 0);
594 opoint = PT - len;
595 signal_after_change (opoint, 0, len);
0ef71121 596 update_compositions (opoint, PT, CHECK_BORDER);
395ec62e
KH
597 }
598}
b45433b3 599
2b083808 600/* Insert the character C before point. Do not inherit text properties. */
3be11131 601
c660b094 602void
ae19ba7c 603insert_char (int c)
3be11131 604{
0ef71121 605 unsigned char str[MAX_MULTIBYTE_LENGTH];
2b083808
RS
606 int len;
607
4b4deea2 608 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
0ef71121 609 len = CHAR_STRING (c, str);
2b083808
RS
610 else
611 {
612 len = 1;
0ef71121 613 str[0] = c;
2b083808 614 }
3be11131 615
b68864e5 616 insert ((char *) str, len);
3be11131
RS
617}
618
2b083808 619/* Insert the null-terminated string S before point. */
3be11131
RS
620
621void
ae19ba7c 622insert_string (const char *s)
3be11131
RS
623{
624 insert (s, strlen (s));
625}
626
627/* Like `insert' except that all markers pointing at the place where
628 the insertion happens are adjusted to point after it.
629 Don't use this function to insert part of a Lisp string,
630 since gc could happen and relocate it. */
631
632void
d311d28c 633insert_before_markers (const char *string, ptrdiff_t nbytes)
3be11131
RS
634{
635 if (nbytes > 0)
636 {
d311d28c 637 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
bab3eee1
EZ
638 insert_1_both (string, len, nbytes, 0, 1, 1);
639 opoint = PT - len;
640 signal_after_change (opoint, 0, len);
0ef71121 641 update_compositions (opoint, PT, CHECK_BORDER);
3be11131
RS
642 }
643}
644
2b083808
RS
645/* Likewise, but inherit text properties from neighboring characters. */
646
3be11131 647void
b68864e5 648insert_before_markers_and_inherit (const char *string,
d311d28c 649 ptrdiff_t nbytes)
3be11131
RS
650{
651 if (nbytes > 0)
652 {
d311d28c 653 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
bab3eee1
EZ
654 insert_1_both (string, len, nbytes, 1, 1, 1);
655 opoint = PT - len;
656 signal_after_change (opoint, 0, len);
0ef71121 657 update_compositions (opoint, PT, CHECK_BORDER);
3be11131
RS
658 }
659}
1f90a790 660
b16afa45
KH
661#ifdef BYTE_COMBINING_DEBUG
662
432f78d2
RS
663/* See if the bytes before POS/POS_BYTE combine with bytes
664 at the start of STRING to form a single character.
ce97a2d7 665 If so, return the number of bytes at the start of STRING
432f78d2 666 which combine in this way. Otherwise, return 0. */
b45433b3 667
432f78d2 668int
d311d28c
PE
669count_combining_before (const unsigned char *string, ptrdiff_t length,
670 ptrdiff_t pos, ptrdiff_t pos_byte)
432f78d2 671{
cae184f2 672 int len, combining_bytes;
641a26d2 673 const unsigned char *p;
b45433b3 674
432f78d2
RS
675 if (NILP (current_buffer->enable_multibyte_characters))
676 return 0;
cae184f2
KH
677
678 /* At first, we can exclude the following cases:
679 (1) STRING[0] can't be a following byte of multibyte sequence.
680 (2) POS is the start of the current buffer.
681 (3) A character before POS is not a multibyte character. */
682 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
432f78d2 683 return 0;
cae184f2 684 if (pos_byte == BEG_BYTE) /* case (2) */
432f78d2 685 return 0;
cae184f2
KH
686 len = 1;
687 p = BYTE_POS_ADDR (pos_byte - 1);
688 while (! CHAR_HEAD_P (*p)) p--, len++;
409f2919 689 if (! LEADING_CODE_P (*p)) /* case (3) */
432f78d2 690 return 0;
cae184f2 691
cae184f2
KH
692 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
693 if (combining_bytes <= 0)
694 /* The character preceding POS is, complete and no room for
695 combining bytes (combining_bytes == 0), or an independent 8-bit
696 character (combining_bytes < 0). */
432f78d2 697 return 0;
ce97a2d7 698
cae184f2
KH
699 /* We have a combination situation. Count the bytes at STRING that
700 may combine. */
701 p = string + 1;
ce97a2d7
RS
702 while (!CHAR_HEAD_P (*p) && p < string + length)
703 p++;
704
cae184f2 705 return (combining_bytes < p - string ? combining_bytes : p - string);
432f78d2 706}
b45433b3 707
432f78d2
RS
708/* See if the bytes after POS/POS_BYTE combine with bytes
709 at the end of STRING to form a single character.
710 If so, return the number of bytes after POS/POS_BYTE
711 which combine in this way. Otherwise, return 0. */
679194a6 712
432f78d2 713int
ae19ba7c 714count_combining_after (const unsigned char *string,
d311d28c 715 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
432f78d2 716{
d311d28c
PE
717 ptrdiff_t opos_byte = pos_byte;
718 ptrdiff_t i;
719 ptrdiff_t bytes;
cae184f2 720 unsigned char *bufp;
3be11131 721
432f78d2
RS
722 if (NILP (current_buffer->enable_multibyte_characters))
723 return 0;
cae184f2
KH
724
725 /* At first, we can exclude the following cases:
726 (1) The last byte of STRING is an ASCII.
727 (2) POS is the last of the current buffer.
728 (3) A character at POS can't be a following byte of multibyte
729 character. */
730 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
731 return 0;
732 if (pos_byte == Z_BYTE) /* case (2) */
432f78d2 733 return 0;
cae184f2
KH
734 bufp = BYTE_POS_ADDR (pos_byte);
735 if (CHAR_HEAD_P (*bufp)) /* case (3) */
736 return 0;
737
432f78d2 738 i = length - 1;
b57a7b0b 739 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
432f78d2
RS
740 {
741 i--;
742 }
b57a7b0b
KH
743 if (i < 0)
744 {
cae184f2
KH
745 /* All characters in STRING are not character head. We must
746 check also preceding bytes at POS. We are sure that the gap
747 is at POS. */
748 unsigned char *p = BEG_ADDR;
b57a7b0b 749 i = pos_byte - 2;
cae184f2 750 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
b57a7b0b 751 i--;
409f2919 752 if (i < 0 || !LEADING_CODE_P (p[i]))
b57a7b0b 753 return 0;
0ef71121 754
cae184f2
KH
755 bytes = BYTES_BY_CHAR_HEAD (p[i]);
756 return (bytes <= pos_byte - 1 - i + length
757 ? 0
758 : bytes - (pos_byte - 1 - i + length));
b57a7b0b 759 }
409f2919 760 if (!LEADING_CODE_P (string[i]))
432f78d2
RS
761 return 0;
762
cae184f2
KH
763 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
764 bufp++, pos_byte++;
765 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
cd11ef31 766
cae184f2 767 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
b45433b3 768}
2b083808 769
b16afa45 770#endif
9f3ede3c 771
1f90a790 772\f
2b083808 773/* Insert a sequence of NCHARS chars which occupy NBYTES bytes
04d360e7
EZ
774 starting at STRING. INHERIT non-zero means inherit the text
775 properties from neighboring characters; zero means inserted text
776 will have no text properties. PREPARE non-zero means call
777 prepare_to_modify_buffer, which checks that the region is not
778 read-only, and calls before-change-function and any modification
779 properties the text may have. BEFORE_MARKERS non-zero means adjust
780 all markers that point at the insertion place to point after it. */
2b083808
RS
781
782void
b68864e5 783insert_1_both (const char *string,
d311d28c 784 ptrdiff_t nchars, ptrdiff_t nbytes,
a08d4ba7 785 bool inherit, bool prepare, bool before_markers)
2b083808 786{
da80a8d5
GM
787 if (nchars == 0)
788 return;
177c0ea7 789
4b4deea2 790 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
57404188
KH
791 nchars = nbytes;
792
35d63725
RS
793 if (prepare)
794 /* Do this before moving and increasing the gap,
795 because the before-change hooks might move the gap
796 or make it smaller. */
797 prepare_to_modify_buffer (PT, PT, NULL);
798
2b083808
RS
799 if (PT != GPT)
800 move_gap_both (PT, PT_BYTE);
801 if (GAP_SIZE < nbytes)
802 make_gap (nbytes - GAP_SIZE);
803
b16afa45
KH
804#ifdef BYTE_COMBINING_DEBUG
805 if (count_combining_before (string, nbytes, PT, PT_BYTE)
806 || count_combining_after (string, nbytes, PT, PT_BYTE))
1088b922 807 emacs_abort ();
b16afa45 808#endif
432f78d2
RS
809
810 /* Record deletion of the surrounding text that combines with
811 the insertion. This, together with recording the insertion,
b16afa45
KH
812 will add up to the right stuff in the undo list. */
813 record_insert (PT, nchars);
2b083808 814 MODIFF++;
3e145152 815 CHARS_MODIFF = MODIFF;
2b083808 816
72af86bd 817 memcpy (GPT_ADDR, string, nbytes);
2b083808 818
2b083808 819 GAP_SIZE -= nbytes;
1f90a790
RS
820 GPT += nchars;
821 ZV += nchars;
822 Z += nchars;
2b083808
RS
823 GPT_BYTE += nbytes;
824 ZV_BYTE += nbytes;
825 Z_BYTE += nbytes;
826 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1f90a790 827
e2688e4a 828 eassert (GPT <= GPT_BYTE);
1f90a790 829
9dde4e0c
KS
830 /* The insert may have been in the unchanged region, so check again. */
831 if (Z - GPT < END_UNCHANGED)
832 END_UNCHANGED = Z - GPT;
833
1f90a790 834 adjust_overlays_for_insert (PT, nchars);
432f78d2 835 adjust_markers_for_insert (PT, PT_BYTE,
1f90a790 836 PT + nchars, PT_BYTE + nbytes,
2b083808 837 before_markers);
ce97a2d7 838
2f294edf 839 offset_intervals (current_buffer, PT, nchars);
1f90a790 840
0c94c8d6 841 if (!inherit && buffer_intervals (current_buffer))
f2cab2ea
GM
842 set_text_properties (make_number (PT), make_number (PT + nchars),
843 Qnil, Qnil, Qnil);
ce97a2d7 844
b16afa45 845 adjust_point (nchars, nbytes);
828f1ed3 846
e2688e4a 847 check_markers ();
2b083808 848}
3be11131 849\f
679194a6 850/* Insert the part of the text of STRING, a Lisp object assumed to be
2b083808
RS
851 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
852 starting at position POS / POS_BYTE. If the text of STRING has properties,
853 copy them into the buffer.
679194a6
JA
854
855 It does not work to use `insert' for this, because a GC could happen
72af86bd 856 before we copy the stuff into the buffer, and relocate the string
7e1ea612 857 without insert noticing. */
679194a6 858
c660b094 859void
d311d28c 860insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
a08d4ba7 861 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
395ec62e 862{
d311d28c 863 ptrdiff_t opoint = PT;
d8bf4256
RS
864
865 if (SCHARS (string) == 0)
866 return;
867
62b82678
RS
868 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
869 inherit, 0);
870 signal_after_change (opoint, 0, PT - opoint);
0ef71121 871 update_compositions (opoint, PT, CHECK_BORDER);
395ec62e
KH
872}
873
2b083808
RS
874/* Like `insert_from_string' except that all markers pointing
875 at the place where the insertion happens are adjusted to point after it. */
3be11131
RS
876
877void
ae19ba7c 878insert_from_string_before_markers (Lisp_Object string,
d311d28c
PE
879 ptrdiff_t pos, ptrdiff_t pos_byte,
880 ptrdiff_t length, ptrdiff_t length_byte,
a08d4ba7 881 bool inherit)
3be11131 882{
d311d28c 883 ptrdiff_t opoint = PT;
d8bf4256
RS
884
885 if (SCHARS (string) == 0)
886 return;
887
62b82678
RS
888 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
889 inherit, 1);
890 signal_after_change (opoint, 0, PT - opoint);
0ef71121 891 update_compositions (opoint, PT, CHECK_BORDER);
3be11131
RS
892}
893
894/* Subroutine of the insertion functions above. */
895
896static void
d311d28c
PE
897insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
898 ptrdiff_t nchars, ptrdiff_t nbytes,
a08d4ba7 899 bool inherit, bool before_markers)
b45433b3 900{
b45433b3 901 struct gcpro gcpro1;
d311d28c 902 ptrdiff_t outgoing_nbytes = nbytes;
ce97a2d7 903 INTERVAL intervals;
2b083808
RS
904
905 /* Make OUTGOING_NBYTES describe the text
906 as it will be inserted in this buffer. */
907
4b4deea2 908 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
2b083808 909 outgoing_nbytes = nchars;
2a1d8be0 910 else if (! STRING_MULTIBYTE (string))
2b083808 911 outgoing_nbytes
f7e233a8 912 = count_size_as_multibyte (SDATA (string) + pos_byte,
2b083808 913 nbytes);
b45433b3 914
b45433b3 915 GCPRO1 (string);
35d63725
RS
916 /* Do this before moving and increasing the gap,
917 because the before-change hooks might move the gap
918 or make it smaller. */
d206af14 919 prepare_to_modify_buffer (PT, PT, NULL);
b45433b3 920
2bcaed71 921 if (PT != GPT)
3be11131 922 move_gap_both (PT, PT_BYTE);
534b9840 923 if (GAP_SIZE < outgoing_nbytes)
2b083808 924 make_gap (outgoing_nbytes - GAP_SIZE);
b45433b3
JB
925 UNGCPRO;
926
2b083808
RS
927 /* Copy the string text into the buffer, perhaps converting
928 between single-byte and multibyte. */
d5db4077 929 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
2a1d8be0 930 STRING_MULTIBYTE (string),
4b4deea2 931 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
b45433b3 932
b16afa45 933#ifdef BYTE_COMBINING_DEBUG
432f78d2
RS
934 /* We have copied text into the gap, but we have not altered
935 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
936 to these functions and get the same results as we would
937 have got earlier on. Meanwhile, PT_ADDR does point to
938 the text that has been stored by copy_text. */
b16afa45
KH
939 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
940 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1088b922 941 emacs_abort ();
b16afa45 942#endif
432f78d2 943
b16afa45 944 record_insert (PT, nchars);
432f78d2 945 MODIFF++;
3e145152 946 CHARS_MODIFF = MODIFF;
432f78d2 947
7792090e 948 GAP_SIZE -= outgoing_nbytes;
1f90a790
RS
949 GPT += nchars;
950 ZV += nchars;
951 Z += nchars;
2b083808
RS
952 GPT_BYTE += outgoing_nbytes;
953 ZV_BYTE += outgoing_nbytes;
954 Z_BYTE += outgoing_nbytes;
469ff680 955 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
3be11131 956
e2688e4a 957 eassert (GPT <= GPT_BYTE);
679194a6 958
9dde4e0c
KS
959 /* The insert may have been in the unchanged region, so check again. */
960 if (Z - GPT < END_UNCHANGED)
961 END_UNCHANGED = Z - GPT;
962
1f90a790
RS
963 adjust_overlays_for_insert (PT, nchars);
964 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
965 PT_BYTE + outgoing_nbytes,
1f90a790 966 before_markers);
ce97a2d7 967
1f90a790
RS
968 offset_intervals (current_buffer, PT, nchars);
969
0c94c8d6 970 intervals = string_intervals (string);
b16afa45 971 /* Get the intervals for the part of the string we are inserting. */
d5db4077 972 if (nbytes < SBYTES (string))
1f90a790 973 intervals = copy_intervals (intervals, pos, nchars);
177c0ea7 974
ce97a2d7 975 /* Insert those intervals. */
1f90a790 976 graft_intervals_into_buffer (intervals, PT, nchars,
9391e591 977 current_buffer, inherit);
ce97a2d7 978
b16afa45 979 adjust_point (nchars, outgoing_nbytes);
8f924df7 980
e2688e4a 981 check_markers ();
8f924df7
KH
982}
983\f
984/* Insert a sequence of NCHARS chars which occupy NBYTES bytes
8a44e6d1
KH
985 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
986 GPT_ADDR (if not text_at_gap_tail). */
8f924df7
KH
987
988void
8a44e6d1 989insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
8f924df7 990{
8bc369d4
KH
991 int ins_charpos = GPT;
992 int ins_bytepos = GPT_BYTE;
993
4b4deea2 994 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
8f924df7
KH
995 nchars = nbytes;
996
997 record_insert (GPT, nchars);
998 MODIFF++;
999
1000 GAP_SIZE -= nbytes;
8a44e6d1
KH
1001 if (! text_at_gap_tail)
1002 {
1003 GPT += nchars;
1004 GPT_BYTE += nbytes;
1005 }
8f924df7
KH
1006 ZV += nchars;
1007 Z += nchars;
8f924df7
KH
1008 ZV_BYTE += nbytes;
1009 Z_BYTE += nbytes;
1010 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1011
e2688e4a 1012 eassert (GPT <= GPT_BYTE);
8f924df7 1013
8bc369d4
KH
1014 adjust_overlays_for_insert (ins_charpos, nchars);
1015 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1016 ins_charpos + nchars, ins_bytepos + nbytes, 0);
8f924df7 1017
0c94c8d6 1018 if (buffer_intervals (current_buffer))
c669988b 1019 {
8bc369d4
KH
1020 offset_intervals (current_buffer, ins_charpos, nchars);
1021 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
c669988b
KH
1022 current_buffer, 0);
1023 }
8f924df7 1024
8bc369d4 1025 if (ins_charpos < PT)
8f924df7
KH
1026 adjust_point (nchars, nbytes);
1027
e2688e4a 1028 check_markers ();
b45433b3 1029}
3be11131
RS
1030\f
1031/* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
ef29f213
KH
1032 current buffer. If the text in BUF has properties, they are absorbed
1033 into the current buffer.
1034
1035 It does not work to use `insert' for this, because a malloc could happen
72af86bd 1036 and relocate BUF's text before the copy happens. */
ef29f213
KH
1037
1038void
ae19ba7c 1039insert_from_buffer (struct buffer *buf,
a08d4ba7 1040 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
ef29f213 1041{
d311d28c 1042 ptrdiff_t opoint = PT;
3be11131 1043
62b82678
RS
1044 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1045 signal_after_change (opoint, 0, PT - opoint);
0ef71121 1046 update_compositions (opoint, PT, CHECK_BORDER);
ef29f213
KH
1047}
1048
1049static void
ae19ba7c 1050insert_from_buffer_1 (struct buffer *buf,
a08d4ba7 1051 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
ef29f213 1052{
d311d28c
PE
1053 ptrdiff_t chunk, chunk_expanded;
1054 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1055 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1056 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1057 ptrdiff_t outgoing_nbytes = incoming_nbytes;
ce97a2d7 1058 INTERVAL intervals;
2b083808
RS
1059
1060 /* Make OUTGOING_NBYTES describe the text
1061 as it will be inserted in this buffer. */
1062
4b4deea2 1063 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
2b083808 1064 outgoing_nbytes = nchars;
4b4deea2 1065 else if (NILP (BVAR (buf, enable_multibyte_characters)))
4468c4f1 1066 {
d311d28c
PE
1067 ptrdiff_t outgoing_before_gap = 0;
1068 ptrdiff_t outgoing_after_gap = 0;
ef29f213 1069
4468c4f1
KH
1070 if (from < BUF_GPT (buf))
1071 {
1072 chunk = BUF_GPT_BYTE (buf) - from_byte;
1073 if (chunk > incoming_nbytes)
1074 chunk = incoming_nbytes;
1075 outgoing_before_gap
1076 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1077 chunk);
1078 }
1079 else
1080 chunk = 0;
1081
1082 if (chunk < incoming_nbytes)
1083 outgoing_after_gap
177c0ea7 1084 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
4468c4f1
KH
1085 from_byte + chunk),
1086 incoming_nbytes - chunk);
1087
1088 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1089 }
177c0ea7 1090
35d63725
RS
1091 /* Do this before moving and increasing the gap,
1092 because the before-change hooks might move the gap
1093 or make it smaller. */
d206af14 1094 prepare_to_modify_buffer (PT, PT, NULL);
ef29f213
KH
1095
1096 if (PT != GPT)
3be11131 1097 move_gap_both (PT, PT_BYTE);
2b083808
RS
1098 if (GAP_SIZE < outgoing_nbytes)
1099 make_gap (outgoing_nbytes - GAP_SIZE);
ef29f213 1100
3be11131 1101 if (from < BUF_GPT (buf))
ef29f213 1102 {
3be11131 1103 chunk = BUF_GPT_BYTE (buf) - from_byte;
2b083808
RS
1104 if (chunk > incoming_nbytes)
1105 chunk = incoming_nbytes;
4468c4f1
KH
1106 /* Record number of output bytes, so we know where
1107 to put the output from the second copy_text. */
1108 chunk_expanded
1109 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1110 GPT_ADDR, chunk,
4b4deea2
TT
1111 ! NILP (BVAR (buf, enable_multibyte_characters)),
1112 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
ef29f213
KH
1113 }
1114 else
4468c4f1
KH
1115 chunk_expanded = chunk = 0;
1116
2b083808
RS
1117 if (chunk < incoming_nbytes)
1118 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
4468c4f1 1119 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
4b4deea2
TT
1120 ! NILP (BVAR (buf, enable_multibyte_characters)),
1121 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
ef29f213 1122
b16afa45 1123#ifdef BYTE_COMBINING_DEBUG
432f78d2
RS
1124 /* We have copied text into the gap, but we have not altered
1125 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1126 to these functions and get the same results as we would
1f90a790 1127 have got earlier on. Meanwhile, GPT_ADDR does point to
432f78d2 1128 the text that has been stored by copy_text. */
b16afa45
KH
1129 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1130 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1088b922 1131 emacs_abort ();
b16afa45 1132#endif
432f78d2 1133
b16afa45 1134 record_insert (PT, nchars);
432f78d2 1135 MODIFF++;
3e145152 1136 CHARS_MODIFF = MODIFF;
432f78d2 1137
2b083808 1138 GAP_SIZE -= outgoing_nbytes;
1f90a790
RS
1139 GPT += nchars;
1140 ZV += nchars;
1141 Z += nchars;
2b083808
RS
1142 GPT_BYTE += outgoing_nbytes;
1143 ZV_BYTE += outgoing_nbytes;
1144 Z_BYTE += outgoing_nbytes;
469ff680 1145 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1f90a790 1146
e2688e4a 1147 eassert (GPT <= GPT_BYTE);
1f90a790 1148
9dde4e0c
KS
1149 /* The insert may have been in the unchanged region, so check again. */
1150 if (Z - GPT < END_UNCHANGED)
1151 END_UNCHANGED = Z - GPT;
1152
1f90a790
RS
1153 adjust_overlays_for_insert (PT, nchars);
1154 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
432f78d2 1155 PT_BYTE + outgoing_nbytes,
b16afa45 1156 0);
ce97a2d7 1157
2f294edf 1158 offset_intervals (current_buffer, PT, nchars);
ce97a2d7 1159
b16afa45 1160 /* Get the intervals for the part of the string we are inserting. */
0c94c8d6 1161 intervals = buffer_intervals (buf);
f1a6b216 1162 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
06d8227a
GM
1163 {
1164 if (buf == current_buffer && PT <= from)
1165 from += nchars;
1166 intervals = copy_intervals (intervals, from, nchars);
1167 }
177c0ea7 1168
ce97a2d7 1169 /* Insert those intervals. */
1f90a790 1170 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
ce97a2d7 1171
b16afa45 1172 adjust_point (nchars, outgoing_nbytes);
b45433b3
JB
1173}
1174\f
652838b5
KH
1175/* Record undo information and adjust markers and position keepers for
1176 a replacement of a text PREV_TEXT at FROM to a new text of LEN
8a44e6d1
KH
1177 chars (LEN_BYTE bytes) which resides in the gap just after
1178 GPT_ADDR.
652838b5
KH
1179
1180 PREV_TEXT nil means the new text was just inserted. */
2d9eea44 1181
8a44e6d1 1182static void
d311d28c 1183adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
8a44e6d1 1184 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1e9c7b7d 1185{
d311d28c 1186 ptrdiff_t nchars_del = 0, nbytes_del = 0;
61415a25 1187
b16afa45
KH
1188#ifdef BYTE_COMBINING_DEBUG
1189 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1190 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1088b922 1191 emacs_abort ();
b16afa45
KH
1192#endif
1193
828f1ed3
KH
1194 if (STRINGP (prev_text))
1195 {
d5db4077
KR
1196 nchars_del = SCHARS (prev_text);
1197 nbytes_del = SBYTES (prev_text);
828f1ed3
KH
1198 }
1199
61415a25
KH
1200 /* Update various buffer positions for the new text. */
1201 GAP_SIZE -= len_byte;
1202 ZV += len; Z+= len;
1203 ZV_BYTE += len_byte; Z_BYTE += len_byte;
8a44e6d1
KH
1204 GPT += len; GPT_BYTE += len_byte;
1205 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
61415a25 1206
adee4f91
KH
1207 if (nchars_del > 0)
1208 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1209 len, len_byte);
1210 else
1211 adjust_markers_for_insert (from, from_byte,
1212 from + len, from_byte + len_byte, 0);
b16afa45 1213
af1f7e06
DA
1214 if (nchars_del > 0)
1215 record_delete (from, prev_text);
1216 record_insert (from, len);
652838b5
KH
1217
1218 if (len > nchars_del)
1219 adjust_overlays_for_insert (from, len - nchars_del);
1220 else if (len < nchars_del)
1221 adjust_overlays_for_delete (from, nchars_del - len);
8707c1e5 1222
2f294edf 1223 offset_intervals (current_buffer, from, len - nchars_del);
61415a25 1224
b16afa45
KH
1225 if (from < PT)
1226 adjust_point (len - nchars_del, len_byte - nbytes_del);
61415a25 1227
9f3ede3c 1228 /* As byte combining will decrease Z, we must check this again. */
a50df55b
GM
1229 if (Z - GPT < END_UNCHANGED)
1230 END_UNCHANGED = Z - GPT;
9f3ede3c 1231
e2688e4a 1232 check_markers ();
60ea6052 1233
1e9c7b7d 1234 if (len == 0)
b58e3ca1
RS
1235 evaporate_overlays (from);
1236 MODIFF++;
3e145152 1237 CHARS_MODIFF = MODIFF;
b58e3ca1
RS
1238}
1239
652838b5
KH
1240/* Record undo information, adjust markers and position keepers for an
1241 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1242 text already exists in the current buffer but character length (TO
1243 - FROM) may be incorrect, the correct length is NEWLEN. */
1244
1245void
d311d28c
PE
1246adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1247 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
652838b5 1248{
d311d28c 1249 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
652838b5
KH
1250
1251 if (GPT != to)
1252 move_gap_both (to, to_byte);
1253 GAP_SIZE += len_byte;
1254 GPT -= len; GPT_BYTE -= len_byte;
1255 ZV -= len; ZV_BYTE -= len_byte;
1256 Z -= len; Z_BYTE -= len_byte;
8a44e6d1 1257 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
652838b5 1258}
085db7de 1259\f
3be11131 1260/* Replace the text from character positions FROM to TO with NEW,
a08d4ba7 1261 If PREPARE, call prepare_to_modify_buffer.
c5ca4d3a
RS
1262 If INHERIT, the newly inserted text should inherit text properties
1263 from the surrounding non-deleted text. */
1264
1265/* Note that this does not yet handle markers quite right.
1266 Also it needs to record a single undo-entry that does a replacement
1267 rather than a separate delete and insert.
60aa777a
RS
1268 That way, undo will also handle markers properly.
1269
1270 But if MARKERS is 0, don't relocate markers. */
c5ca4d3a
RS
1271
1272void
d311d28c 1273replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
a08d4ba7 1274 bool prepare, bool inherit, bool markers)
c5ca4d3a 1275{
d311d28c
PE
1276 ptrdiff_t inschars = SCHARS (new);
1277 ptrdiff_t insbytes = SBYTES (new);
1278 ptrdiff_t from_byte, to_byte;
1279 ptrdiff_t nbytes_del, nchars_del;
c5ca4d3a 1280 struct gcpro gcpro1;
ce97a2d7 1281 INTERVAL intervals;
d311d28c 1282 ptrdiff_t outgoing_insbytes = insbytes;
23b20a70 1283 Lisp_Object deletion;
c5ca4d3a 1284
e2688e4a 1285 check_markers ();
60ea6052 1286
c5ca4d3a 1287 GCPRO1 (new);
6bbd7a29 1288 deletion = Qnil;
c5ca4d3a
RS
1289
1290 if (prepare)
1291 {
d311d28c 1292 ptrdiff_t range_length = to - from;
c5ca4d3a
RS
1293 prepare_to_modify_buffer (from, to, &from);
1294 to = from + range_length;
1295 }
1296
3be11131
RS
1297 UNGCPRO;
1298
b50a28de 1299 /* Make args be valid. */
c5ca4d3a
RS
1300 if (from < BEGV)
1301 from = BEGV;
1302 if (to > ZV)
1303 to = ZV;
1304
3be11131
RS
1305 from_byte = CHAR_TO_BYTE (from);
1306 to_byte = CHAR_TO_BYTE (to);
c5ca4d3a 1307
3be11131
RS
1308 nchars_del = to - from;
1309 nbytes_del = to_byte - from_byte;
1310
1311 if (nbytes_del <= 0 && insbytes == 0)
1312 return;
c5ca4d3a 1313
1f90a790
RS
1314 /* Make OUTGOING_INSBYTES describe the text
1315 as it will be inserted in this buffer. */
1316
4b4deea2 1317 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1f90a790 1318 outgoing_insbytes = inschars;
2a1d8be0 1319 else if (! STRING_MULTIBYTE (new))
1f90a790 1320 outgoing_insbytes
d5db4077 1321 = count_size_as_multibyte (SDATA (new), insbytes);
1f90a790 1322
c5ca4d3a
RS
1323 GCPRO1 (new);
1324
1325 /* Make sure the gap is somewhere in or next to what we are deleting. */
1326 if (from > GPT)
3be11131 1327 gap_right (from, from_byte);
c5ca4d3a 1328 if (to < GPT)
3be11131 1329 gap_left (to, to_byte, 0);
c5ca4d3a 1330
039af53a
KH
1331 /* Even if we don't record for undo, we must keep the original text
1332 because we may have to recover it because of inappropriate byte
1333 combining. */
4b4deea2 1334 if (! EQ (BVAR (current_buffer, undo_list), Qt))
b16afa45 1335 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
c5ca4d3a 1336
3be11131
RS
1337 GAP_SIZE += nbytes_del;
1338 ZV -= nchars_del;
1339 Z -= nchars_del;
1340 ZV_BYTE -= nbytes_del;
1341 Z_BYTE -= nbytes_del;
c5ca4d3a 1342 GPT = from;
3be11131 1343 GPT_BYTE = from_byte;
017e09ac 1344 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
c5ca4d3a 1345
e2688e4a 1346 eassert (GPT <= GPT_BYTE);
3be11131 1347
a50df55b
GM
1348 if (GPT - BEG < BEG_UNCHANGED)
1349 BEG_UNCHANGED = GPT - BEG;
1350 if (Z - GPT < END_UNCHANGED)
1351 END_UNCHANGED = Z - GPT;
c5ca4d3a 1352
599a9e4f
PE
1353 if (GAP_SIZE < outgoing_insbytes)
1354 make_gap (outgoing_insbytes - GAP_SIZE);
c5ca4d3a 1355
1f90a790
RS
1356 /* Copy the string text into the buffer, perhaps converting
1357 between single-byte and multibyte. */
d5db4077 1358 copy_text (SDATA (new), GPT_ADDR, insbytes,
2a1d8be0 1359 STRING_MULTIBYTE (new),
4b4deea2 1360 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1f90a790 1361
b16afa45 1362#ifdef BYTE_COMBINING_DEBUG
255c7dae
RS
1363 /* We have copied text into the gap, but we have not marked
1364 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1365 here, for both the previous text and the following text.
1366 Meanwhile, GPT_ADDR does point to
432f78d2 1367 the text that has been stored by copy_text. */
b16afa45
KH
1368 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1369 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1088b922 1370 emacs_abort ();
b16afa45 1371#endif
432f78d2 1372
af1f7e06
DA
1373 /* Record the insertion first, so that when we undo,
1374 the deletion will be undone first. Thus, undo
1375 will insert before deleting, and thus will keep
1376 the markers before and after this text separate. */
3521bd09
DA
1377 if (!NILP (deletion))
1378 {
1379 record_insert (from + SCHARS (deletion), inschars);
1380 record_delete (from, deletion);
1381 }
c5ca4d3a 1382
1f90a790
RS
1383 GAP_SIZE -= outgoing_insbytes;
1384 GPT += inschars;
1385 ZV += inschars;
1386 Z += inschars;
1387 GPT_BYTE += outgoing_insbytes;
1388 ZV_BYTE += outgoing_insbytes;
1389 Z_BYTE += outgoing_insbytes;
c5ca4d3a
RS
1390 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1391
e2688e4a 1392 eassert (GPT <= GPT_BYTE);
3be11131 1393
048a0fbb 1394 /* Adjust markers for the deletion and the insertion. */
60aa777a 1395 if (markers)
048a0fbb
RS
1396 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1397 inschars, outgoing_insbytes);
c5ca4d3a 1398
2f294edf
DA
1399 /* Adjust the overlay center as needed. This must be done after
1400 adjusting the markers that bound the overlays. */
1401 adjust_overlays_for_delete (from, nchars_del);
1402 adjust_overlays_for_insert (from, inschars);
1403
255c7dae 1404 offset_intervals (current_buffer, from, inschars - nchars_del);
ce97a2d7
RS
1405
1406 /* Get the intervals for the part of the string we are inserting--
1407 not including the combined-before bytes. */
0c94c8d6 1408 intervals = string_intervals (new);
ce97a2d7 1409 /* Insert those intervals. */
1f90a790 1410 graft_intervals_into_buffer (intervals, from, inschars,
ce97a2d7 1411 current_buffer, inherit);
c5ca4d3a 1412
1f90a790
RS
1413 /* Relocate point as if it were a marker. */
1414 if (from < PT)
8bedbe9d 1415 adjust_point ((from + inschars - (PT < to ? PT : to)),
1f90a790 1416 (from_byte + outgoing_insbytes
8bedbe9d 1417 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
c5ca4d3a 1418
1f90a790
RS
1419 if (outgoing_insbytes == 0)
1420 evaporate_overlays (from);
93b882e8 1421
e2688e4a 1422 check_markers ();
60ea6052 1423
c5ca4d3a 1424 MODIFF++;
3e145152 1425 CHARS_MODIFF = MODIFF;
c5ca4d3a
RS
1426 UNGCPRO;
1427
255c7dae 1428 signal_after_change (from, nchars_del, GPT - from);
0ef71121 1429 update_compositions (from, GPT, CHECK_BORDER);
c5ca4d3a
RS
1430}
1431\f
085db7de
RS
1432/* Replace the text from character positions FROM to TO with
1433 the text in INS of length INSCHARS.
1434 Keep the text properties that applied to the old characters
1435 (extending them to all the new chars if there are more new chars).
1436
1437 Note that this does not yet handle markers quite right.
1438
a08d4ba7 1439 If MARKERS, relocate markers.
085db7de
RS
1440
1441 Unlike most functions at this level, never call
1442 prepare_to_modify_buffer and never call signal_after_change. */
1443
1444void
d311d28c
PE
1445replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1446 ptrdiff_t to, ptrdiff_t to_byte,
1447 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
a08d4ba7 1448 bool markers)
085db7de 1449{
d311d28c 1450 ptrdiff_t nbytes_del, nchars_del;
085db7de 1451
e2688e4a 1452 check_markers ();
085db7de
RS
1453
1454 nchars_del = to - from;
1455 nbytes_del = to_byte - from_byte;
1456
1457 if (nbytes_del <= 0 && insbytes == 0)
1458 return;
1459
085db7de
RS
1460 /* Make sure the gap is somewhere in or next to what we are deleting. */
1461 if (from > GPT)
1462 gap_right (from, from_byte);
1463 if (to < GPT)
1464 gap_left (to, to_byte, 0);
1465
1466 GAP_SIZE += nbytes_del;
1467 ZV -= nchars_del;
1468 Z -= nchars_del;
1469 ZV_BYTE -= nbytes_del;
1470 Z_BYTE -= nbytes_del;
1471 GPT = from;
1472 GPT_BYTE = from_byte;
1473 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1474
e2688e4a 1475 eassert (GPT <= GPT_BYTE);
085db7de
RS
1476
1477 if (GPT - BEG < BEG_UNCHANGED)
1478 BEG_UNCHANGED = GPT - BEG;
1479 if (Z - GPT < END_UNCHANGED)
1480 END_UNCHANGED = Z - GPT;
1481
1482 if (GAP_SIZE < insbytes)
1483 make_gap (insbytes - GAP_SIZE);
1484
1485 /* Copy the replacement text into the buffer. */
72af86bd 1486 memcpy (GPT_ADDR, ins, insbytes);
085db7de
RS
1487
1488#ifdef BYTE_COMBINING_DEBUG
1489 /* We have copied text into the gap, but we have not marked
1490 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1491 here, for both the previous text and the following text.
1492 Meanwhile, GPT_ADDR does point to
1493 the text that has been stored by copy_text. */
1494 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1495 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1088b922 1496 emacs_abort ();
085db7de
RS
1497#endif
1498
1499 GAP_SIZE -= insbytes;
1500 GPT += inschars;
1501 ZV += inschars;
1502 Z += inschars;
1503 GPT_BYTE += insbytes;
1504 ZV_BYTE += insbytes;
1505 Z_BYTE += insbytes;
1506 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1507
e2688e4a 1508 eassert (GPT <= GPT_BYTE);
085db7de 1509
2f294edf
DA
1510 /* Adjust markers for the deletion and the insertion. */
1511 if (markers
1512 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1513 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1514 inschars, insbytes);
1515
085db7de
RS
1516 /* Adjust the overlay center as needed. This must be done after
1517 adjusting the markers that bound the overlays. */
1518 if (nchars_del != inschars)
1519 {
1520 adjust_overlays_for_insert (from, inschars);
1521 adjust_overlays_for_delete (from + inschars, nchars_del);
1522 }
1523
085db7de
RS
1524 offset_intervals (current_buffer, from, inschars - nchars_del);
1525
1526 /* Relocate point as if it were a marker. */
7077904e
KH
1527 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1528 {
1529 if (PT < to)
1530 /* PT was within the deleted text. Move it to FROM. */
1531 adjust_point (from - PT, from_byte - PT_BYTE);
1532 else
1533 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1534 }
085db7de
RS
1535
1536 if (insbytes == 0)
1537 evaporate_overlays (from);
1538
e2688e4a 1539 check_markers ();
085db7de
RS
1540
1541 MODIFF++;
3e145152 1542 CHARS_MODIFF = MODIFF;
085db7de
RS
1543}
1544\f
b45433b3 1545/* Delete characters in current buffer
3be11131
RS
1546 from FROM up to (but not including) TO.
1547 If TO comes before FROM, we delete nothing. */
b45433b3 1548
c660b094 1549void
d311d28c 1550del_range (ptrdiff_t from, ptrdiff_t to)
47c64747 1551{
7dae4502 1552 del_range_1 (from, to, 1, 0);
47c64747
RS
1553}
1554
7dae4502
SM
1555/* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1556 RET_STRING says to return the deleted text. */
47c64747 1557
7dae4502 1558Lisp_Object
a08d4ba7 1559del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
b45433b3 1560{
d311d28c 1561 ptrdiff_t from_byte, to_byte;
7dae4502
SM
1562 Lisp_Object deletion;
1563 struct gcpro gcpro1;
3be11131
RS
1564
1565 /* Make args be valid */
1566 if (from < BEGV)
1567 from = BEGV;
1568 if (to > ZV)
1569 to = ZV;
1570
1571 if (to <= from)
7dae4502 1572 return Qnil;
3be11131
RS
1573
1574 if (prepare)
1575 {
d311d28c 1576 ptrdiff_t range_length = to - from;
3be11131 1577 prepare_to_modify_buffer (from, to, &from);
0a411995 1578 to = min (ZV, from + range_length);
3be11131
RS
1579 }
1580
1581 from_byte = CHAR_TO_BYTE (from);
1582 to_byte = CHAR_TO_BYTE (to);
1583
7dae4502 1584 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
5e617bc2 1585 GCPRO1 (deletion);
9c36993a 1586 signal_after_change (from, to - from, 0);
233cc02d 1587 update_compositions (from, from, CHECK_HEAD);
7dae4502
SM
1588 UNGCPRO;
1589 return deletion;
3be11131
RS
1590}
1591
1592/* Like del_range_1 but args are byte positions, not char positions. */
1593
1594void
a08d4ba7 1595del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
3be11131 1596{
d311d28c 1597 ptrdiff_t from, to;
3be11131
RS
1598
1599 /* Make args be valid */
1600 if (from_byte < BEGV_BYTE)
1601 from_byte = BEGV_BYTE;
1602 if (to_byte > ZV_BYTE)
1603 to_byte = ZV_BYTE;
1604
1605 if (to_byte <= from_byte)
1606 return;
1607
1608 from = BYTE_TO_CHAR (from_byte);
1609 to = BYTE_TO_CHAR (to_byte);
b45433b3 1610
d206af14
RS
1611 if (prepare)
1612 {
d311d28c
PE
1613 ptrdiff_t old_from = from, old_to = Z - to;
1614 ptrdiff_t range_length = to - from;
d206af14
RS
1615 prepare_to_modify_buffer (from, to, &from);
1616 to = from + range_length;
3be11131
RS
1617
1618 if (old_from != from)
1619 from_byte = CHAR_TO_BYTE (from);
0a411995
GM
1620 if (to > ZV)
1621 {
1622 to = ZV;
1623 to_byte = ZV_BYTE;
1624 }
1625 else if (old_to == Z - to)
3be11131 1626 to_byte = CHAR_TO_BYTE (to);
d206af14
RS
1627 }
1628
7dae4502 1629 del_range_2 (from, from_byte, to, to_byte, 0);
9c36993a 1630 signal_after_change (from, to - from, 0);
0ef71121 1631 update_compositions (from, from, CHECK_HEAD);
3be11131
RS
1632}
1633
1634/* Like del_range_1, but positions are specified both as charpos
1635 and bytepos. */
1636
1637void
d311d28c 1638del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
a08d4ba7 1639 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
3be11131 1640{
b45433b3 1641 /* Make args be valid */
3be11131
RS
1642 if (from_byte < BEGV_BYTE)
1643 from_byte = BEGV_BYTE;
1644 if (to_byte > ZV_BYTE)
1645 to_byte = ZV_BYTE;
1646
1647 if (to_byte <= from_byte)
1648 return;
1649
b45433b3
JB
1650 if (from < BEGV)
1651 from = BEGV;
1652 if (to > ZV)
1653 to = ZV;
1654
3be11131
RS
1655 if (prepare)
1656 {
d311d28c
PE
1657 ptrdiff_t old_from = from, old_to = Z - to;
1658 ptrdiff_t range_length = to - from;
3be11131
RS
1659 prepare_to_modify_buffer (from, to, &from);
1660 to = from + range_length;
1661
1662 if (old_from != from)
1663 from_byte = CHAR_TO_BYTE (from);
0a411995
GM
1664 if (to > ZV)
1665 {
1666 to = ZV;
1667 to_byte = ZV_BYTE;
1668 }
1669 else if (old_to == Z - to)
3be11131
RS
1670 to_byte = CHAR_TO_BYTE (to);
1671 }
1672
7dae4502 1673 del_range_2 (from, from_byte, to, to_byte, 0);
9c36993a 1674 signal_after_change (from, to - from, 0);
0ef71121 1675 update_compositions (from, from, CHECK_HEAD);
3be11131
RS
1676}
1677
1678/* Delete a range of text, specified both as character positions
1679 and byte positions. FROM and TO are character positions,
7dae4502 1680 while FROM_BYTE and TO_BYTE are byte positions.
a08d4ba7 1681 If RET_STRING, the deleted area is returned as a string. */
3be11131 1682
7dae4502 1683Lisp_Object
d311d28c 1684del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
a08d4ba7 1685 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
3be11131 1686{
a08d4ba7 1687 ptrdiff_t nbytes_del, nchars_del;
628cea90 1688 Lisp_Object deletion;
3be11131 1689
e2688e4a 1690 check_markers ();
60ea6052 1691
3be11131
RS
1692 nchars_del = to - from;
1693 nbytes_del = to_byte - from_byte;
b45433b3
JB
1694
1695 /* Make sure the gap is somewhere in or next to what we are deleting. */
1696 if (from > GPT)
3be11131 1697 gap_right (from, from_byte);
b45433b3 1698 if (to < GPT)
3be11131 1699 gap_left (to, to_byte, 0);
b45433b3 1700
b16afa45
KH
1701#ifdef BYTE_COMBINING_DEBUG
1702 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1703 Z_BYTE - to_byte, from, from_byte))
1088b922 1704 emacs_abort ();
b16afa45 1705#endif
628cea90 1706
4b4deea2 1707 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
b16afa45 1708 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
7dae4502
SM
1709 else
1710 deletion = Qnil;
1711
8948d317
RS
1712 /* Relocate all markers pointing into the new, larger gap
1713 to point at the end of the text before the gap.
3be11131
RS
1714 Do this before recording the deletion,
1715 so that undo handles this after reinserting the text. */
1716 adjust_markers_for_delete (from, from_byte, to, to_byte);
b16afa45 1717
af1f7e06 1718 record_delete (from, deletion);
be09561e 1719 MODIFF++;
3e145152 1720 CHARS_MODIFF = MODIFF;
be09561e 1721
b45433b3 1722 /* Relocate point as if it were a marker. */
2bcaed71 1723 if (from < PT)
3be11131
RS
1724 adjust_point (from - (PT < to ? PT : to),
1725 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
b45433b3 1726
3be11131 1727 offset_intervals (current_buffer, from, - nchars_del);
16032db6 1728
adde4858 1729 /* Adjust the overlay center as needed. This must be done after
a7f38d28 1730 adjusting the markers that bound the overlays. */
e3a87305 1731 adjust_overlays_for_delete (from, nchars_del);
adde4858 1732
3be11131
RS
1733 GAP_SIZE += nbytes_del;
1734 ZV_BYTE -= nbytes_del;
1735 Z_BYTE -= nbytes_del;
1736 ZV -= nchars_del;
1737 Z -= nchars_del;
b45433b3 1738 GPT = from;
3be11131 1739 GPT_BYTE = from_byte;
b3b58c01
AS
1740 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1741 /* Put an anchor, unless called from decode_coding_object which
1742 needs to access the previous gap contents. */
1743 *(GPT_ADDR) = 0;
e3a87305 1744
e2688e4a 1745 eassert (GPT <= GPT_BYTE);
3be11131 1746
a50df55b
GM
1747 if (GPT - BEG < BEG_UNCHANGED)
1748 BEG_UNCHANGED = GPT - BEG;
1749 if (Z - GPT < END_UNCHANGED)
1750 END_UNCHANGED = Z - GPT;
b45433b3 1751
e2688e4a 1752 check_markers ();
60ea6052 1753
d386034e 1754 evaporate_overlays (from);
7dae4502
SM
1755
1756 return deletion;
b45433b3 1757}
20edc1c9
DA
1758
1759/* Call this if you're about to change the region of current buffer
1760 from character positions START to END. This checks the read-only
3be11131
RS
1761 properties of the region, calls the necessary modification hooks,
1762 and warns the next redisplay that it should pay attention to that
3e145152
CY
1763 area.
1764
a08d4ba7 1765 If PRESERVE_CHARS_MODIFF, do not update CHARS_MODIFF.
3e145152 1766 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
3be11131 1767
c660b094 1768void
20edc1c9 1769modify_region_1 (ptrdiff_t start, ptrdiff_t end, bool preserve_chars_modiff)
b45433b3 1770{
d206af14 1771 prepare_to_modify_buffer (start, end, NULL);
b45433b3 1772
20edc1c9 1773 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
83010cd6 1774
9fbf87cd 1775 if (MODIFF <= SAVE_MODIFF)
83010cd6 1776 record_first_change ();
b45433b3 1777 MODIFF++;
3e145152
CY
1778 if (! preserve_chars_modiff)
1779 CHARS_MODIFF = MODIFF;
04a759c8 1780
20edc1c9 1781 bset_point_before_scroll (current_buffer, Qnil);
b45433b3 1782}
20edc1c9 1783
3be11131
RS
1784/* Check that it is okay to modify the buffer between START and END,
1785 which are char positions.
1786
679194a6
JA
1787 Run the before-change-function, if any. If intervals are in use,
1788 verify that the text to be modified is not read-only, and call
d206af14
RS
1789 any modification properties the text may have.
1790
1791 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1792 by holding its value temporarily in a marker. */
b45433b3 1793
c660b094 1794void
d311d28c
PE
1795prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1796 ptrdiff_t *preserve_ptr)
b45433b3 1797{
234fb773
CY
1798 struct buffer *base_buffer;
1799
4b4deea2 1800 if (!NILP (BVAR (current_buffer, read_only)))
b45433b3
JB
1801 Fbarf_if_buffer_read_only ();
1802
c6afe371 1803 /* If we're modifying the buffer other than shown in a selected window,
5ad86e34 1804 let redisplay consider other windows if this buffer is visible. */
e74aeda8 1805 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer
5ad86e34 1806 && buffer_window_count (current_buffer))
2e9f55fd
GM
1807 ++windows_or_buffers_changed;
1808
0c94c8d6 1809 if (buffer_intervals (current_buffer))
d206af14
RS
1810 {
1811 if (preserve_ptr)
1812 {
1813 Lisp_Object preserve_marker;
1814 struct gcpro gcpro1;
1815 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1816 GCPRO1 (preserve_marker);
1817 verify_interval_modification (current_buffer, start, end);
1818 *preserve_ptr = marker_position (preserve_marker);
dab0b04d 1819 unchain_marker (XMARKER (preserve_marker));
d206af14
RS
1820 UNGCPRO;
1821 }
1822 else
1823 verify_interval_modification (current_buffer, start, end);
1824 }
b45433b3 1825
234fb773
CY
1826 /* For indirect buffers, use the base buffer to check clashes. */
1827 if (current_buffer->base_buffer != 0)
1828 base_buffer = current_buffer->base_buffer;
1829 else
1830 base_buffer = current_buffer;
1831
b45433b3 1832#ifdef CLASH_DETECTION
4b4deea2 1833 if (!NILP (BVAR (base_buffer, file_truename))
ab6c5c0c 1834 /* Make binding buffer-file-name to nil effective. */
4b4deea2 1835 && !NILP (BVAR (base_buffer, filename))
9fbf87cd 1836 && SAVE_MODIFF >= MODIFF)
4b4deea2 1837 lock_file (BVAR (base_buffer, file_truename));
b45433b3
JB
1838#else
1839 /* At least warn if this file has changed on disk since it was visited. */
4b4deea2 1840 if (!NILP (BVAR (base_buffer, filename))
9fbf87cd 1841 && SAVE_MODIFF >= MODIFF
d427b66a 1842 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
4b4deea2 1843 && !NILP (Ffile_exists_p (BVAR (base_buffer, filename))))
b45433b3 1844 call1 (intern ("ask-user-about-supersession-threat"),
4b4deea2 1845 BVAR (base_buffer,filename));
b45433b3
JB
1846#endif /* not CLASH_DETECTION */
1847
9852377f 1848 /* If `select-active-regions' is non-nil, save the region text. */
4b4deea2 1849 if (!NILP (BVAR (current_buffer, mark_active))
8b78d5e3 1850 && !inhibit_modification_hooks
4b4deea2 1851 && XMARKER (BVAR (current_buffer, mark))->buffer
7c23dd44
CY
1852 && NILP (Vsaved_region_selection)
1853 && (EQ (Vselect_active_regions, Qonly)
1854 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1855 : (!NILP (Vselect_active_regions)
1856 && !NILP (Vtransient_mark_mode))))
9852377f 1857 {
4c1acb95 1858 ptrdiff_t b = marker_position (BVAR (current_buffer, mark));
d311d28c 1859 ptrdiff_t e = PT;
746812d9
CY
1860 if (b < e)
1861 Vsaved_region_selection = make_buffer_string (b, e, 0);
1862 else if (b > e)
1863 Vsaved_region_selection = make_buffer_string (e, b, 0);
9852377f
CY
1864 }
1865
d206af14 1866 signal_before_change (start, end, preserve_ptr);
2f545eea 1867
56e1065e
JB
1868 if (current_buffer->newline_cache)
1869 invalidate_region_cache (current_buffer,
1870 current_buffer->newline_cache,
1871 start - BEG, Z - end);
1872 if (current_buffer->width_run_cache)
1873 invalidate_region_cache (current_buffer,
1874 current_buffer->width_run_cache,
1875 start - BEG, Z - end);
1876
2f545eea 1877 Vdeactivate_mark = Qt;
b45433b3
JB
1878}
1879\f
d206af14
RS
1880/* These macros work with an argument named `preserve_ptr'
1881 and a local variable named `preserve_marker'. */
1882
1883#define PRESERVE_VALUE \
1884 if (preserve_ptr && NILP (preserve_marker)) \
1885 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1886
1887#define RESTORE_VALUE \
1888 if (! NILP (preserve_marker)) \
1889 { \
1890 *preserve_ptr = marker_position (preserve_marker); \
dab0b04d 1891 unchain_marker (XMARKER (preserve_marker)); \
d206af14
RS
1892 }
1893
b86e0aaf
RS
1894#define PRESERVE_START_END \
1895 if (NILP (start_marker)) \
1896 start_marker = Fcopy_marker (start, Qnil); \
1897 if (NILP (end_marker)) \
1898 end_marker = Fcopy_marker (end, Qnil);
1899
1900#define FETCH_START \
1901 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1902
1903#define FETCH_END \
1904 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1905
4a181359 1906/* Set a variable to nil if an error occurred.
e6d3f090
SM
1907 Don't change the variable if there was no error.
1908 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1909 VARIABLE is the variable to maybe set to nil.
1910 NO-ERROR-FLAG is nil if there was an error,
1911 anything else meaning no error (so this function does nothing). */
27e498e6 1912struct rvoe_arg
4a181359 1913{
27e498e6
PE
1914 Lisp_Object *location;
1915 bool errorp;
1916};
1917
1918static void
1919reset_var_on_error (void *ptr)
1920{
1921 struct rvoe_arg *p = ptr;
1922 if (p->errorp)
1923 *p->location = Qnil;
4a181359
SM
1924}
1925
eb8c3be9 1926/* Signal a change to the buffer immediately before it happens.
d206af14
RS
1927 START_INT and END_INT are the bounds of the text to be changed.
1928
1929 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1930 by holding its value temporarily in a marker. */
b45433b3 1931
4889fc82 1932static void
d311d28c
PE
1933signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1934 ptrdiff_t *preserve_ptr)
b45433b3 1935{
fb4ee5cd 1936 Lisp_Object start, end;
b86e0aaf 1937 Lisp_Object start_marker, end_marker;
d206af14 1938 Lisp_Object preserve_marker;
b86e0aaf 1939 struct gcpro gcpro1, gcpro2, gcpro3;
d311d28c 1940 ptrdiff_t count = SPECPDL_INDEX ();
27e498e6 1941 struct rvoe_arg rvoe_arg;
fb4ee5cd 1942
fd16a4c6
KH
1943 if (inhibit_modification_hooks)
1944 return;
1945
fb4ee5cd
RS
1946 start = make_number (start_int);
1947 end = make_number (end_int);
d206af14 1948 preserve_marker = Qnil;
b86e0aaf
RS
1949 start_marker = Qnil;
1950 end_marker = Qnil;
1951 GCPRO3 (preserve_marker, start_marker, end_marker);
fb4ee5cd 1952
4a181359
SM
1953 specbind (Qinhibit_modification_hooks, Qt);
1954
dee091a3
JD
1955 /* If buffer is unmodified, run a special hook for that case. The
1956 check for Vfirst_change_hook is just a minor optimization. */
9fbf87cd 1957 if (SAVE_MODIFF >= MODIFF
dee091a3 1958 && !NILP (Vfirst_change_hook))
d206af14
RS
1959 {
1960 PRESERVE_VALUE;
b86e0aaf 1961 PRESERVE_START_END;
dee091a3 1962 Frun_hooks (1, &Qfirst_change_hook);
d206af14 1963 }
dbc4e1c1 1964
3d1e2d9c 1965 /* Now run the before-change-functions if any. */
e45fb8bf
RS
1966 if (!NILP (Vbefore_change_functions))
1967 {
3d1e2d9c 1968 Lisp_Object args[3];
27e498e6
PE
1969 rvoe_arg.location = &Vbefore_change_functions;
1970 rvoe_arg.errorp = 1;
3d1e2d9c 1971
d206af14 1972 PRESERVE_VALUE;
b86e0aaf 1973 PRESERVE_START_END;
d206af14 1974
4a181359 1975 /* Mark before-change-functions to be reset to nil in case of error. */
27e498e6 1976 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
3d1e2d9c
RS
1977
1978 /* Actually run the hook functions. */
1979 args[0] = Qbefore_change_functions;
b86e0aaf
RS
1980 args[1] = FETCH_START;
1981 args[2] = FETCH_END;
4a181359 1982 Frun_hook_with_args (3, args);
3d1e2d9c 1983
4a181359 1984 /* There was no error: unarm the reset_on_error. */
27e498e6 1985 rvoe_arg.errorp = 0;
e45fb8bf 1986 }
d07c0804 1987
4cb3e6b3 1988 if (buffer_has_overlays ())
d206af14
RS
1989 {
1990 PRESERVE_VALUE;
b86e0aaf
RS
1991 report_overlay_modification (FETCH_START, FETCH_END, 0,
1992 FETCH_START, FETCH_END, Qnil);
d206af14
RS
1993 }
1994
b86e0aaf
RS
1995 if (! NILP (start_marker))
1996 free_marker (start_marker);
1997 if (! NILP (end_marker))
1998 free_marker (end_marker);
d206af14
RS
1999 RESTORE_VALUE;
2000 UNGCPRO;
4a181359
SM
2001
2002 unbind_to (count, Qnil);
b45433b3
JB
2003}
2004
eb8c3be9 2005/* Signal a change immediately after it happens.
3be11131 2006 CHARPOS is the character position of the start of the changed text.
b45433b3
JB
2007 LENDEL is the number of characters of the text before the change.
2008 (Not the whole buffer; just the part that was changed.)
8b09e5d0
RS
2009 LENINS is the number of characters in that part of the text
2010 after the change. */
b45433b3 2011
c660b094 2012void
d311d28c 2013signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
b45433b3 2014{
d311d28c 2015 ptrdiff_t count = SPECPDL_INDEX ();
27e498e6
PE
2016 struct rvoe_arg rvoe_arg;
2017
fd16a4c6
KH
2018 if (inhibit_modification_hooks)
2019 return;
2020
fb2e7d14
RS
2021 /* If we are deferring calls to the after-change functions
2022 and there are no before-change functions,
2023 just record the args that we were going to use. */
2024 if (! NILP (Vcombine_after_change_calls)
1675d086 2025 && NILP (Vbefore_change_functions)
4cb3e6b3 2026 && !buffer_has_overlays ())
fb2e7d14
RS
2027 {
2028 Lisp_Object elt;
2029
2030 if (!NILP (combine_after_change_list)
2031 && current_buffer != XBUFFER (combine_after_change_buffer))
2032 Fcombine_after_change_execute ();
2033
3de717bd
DA
2034 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2035 lenins - lendel);
fb2e7d14
RS
2036 combine_after_change_list
2037 = Fcons (elt, combine_after_change_list);
2038 combine_after_change_buffer = Fcurrent_buffer ();
2039
2040 return;
2041 }
2042
177c0ea7 2043 if (!NILP (combine_after_change_list))
fb2e7d14
RS
2044 Fcombine_after_change_execute ();
2045
4a181359
SM
2046 specbind (Qinhibit_modification_hooks, Qt);
2047
e45fb8bf
RS
2048 if (!NILP (Vafter_change_functions))
2049 {
3d1e2d9c 2050 Lisp_Object args[4];
27e498e6
PE
2051 rvoe_arg.location = &Vafter_change_functions;
2052 rvoe_arg.errorp = 1;
4a181359
SM
2053
2054 /* Mark after-change-functions to be reset to nil in case of error. */
27e498e6 2055 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
3d1e2d9c
RS
2056
2057 /* Actually run the hook functions. */
2058 args[0] = Qafter_change_functions;
3be11131
RS
2059 XSETFASTINT (args[1], charpos);
2060 XSETFASTINT (args[2], charpos + lenins);
3d1e2d9c 2061 XSETFASTINT (args[3], lendel);
4a181359 2062 Frun_hook_with_args (4, args);
3d1e2d9c 2063
4a181359 2064 /* There was no error: unarm the reset_on_error. */
27e498e6 2065 rvoe_arg.errorp = 0;
e45fb8bf 2066 }
d07c0804 2067
4cb3e6b3 2068 if (buffer_has_overlays ())
3be11131
RS
2069 report_overlay_modification (make_number (charpos),
2070 make_number (charpos + lenins),
d07c0804 2071 1,
3be11131
RS
2072 make_number (charpos),
2073 make_number (charpos + lenins),
d07c0804 2074 make_number (lendel));
c5ca0786
RS
2075
2076 /* After an insertion, call the text properties
2077 insert-behind-hooks or insert-in-front-hooks. */
2078 if (lendel == 0)
d6b81c0f
AS
2079 report_interval_modification (make_number (charpos),
2080 make_number (charpos + lenins));
4a181359
SM
2081
2082 unbind_to (count, Qnil);
b45433b3 2083}
fb2e7d14 2084
27e498e6 2085static void
971de7fb 2086Fcombine_after_change_execute_1 (Lisp_Object val)
fb2e7d14
RS
2087{
2088 Vcombine_after_change_calls = val;
fb2e7d14
RS
2089}
2090
2091DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
335c5470 2092 Scombine_after_change_execute, 0, 0, 0,
9fc9a531 2093 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
5842a27b 2094 (void)
fb2e7d14 2095{
d311d28c
PE
2096 ptrdiff_t count = SPECPDL_INDEX ();
2097 ptrdiff_t beg, end, change;
2098 ptrdiff_t begpos, endpos;
fb2e7d14
RS
2099 Lisp_Object tail;
2100
101e446f
KH
2101 if (NILP (combine_after_change_list))
2102 return Qnil;
2103
c47a9ed1
CY
2104 /* It is rare for combine_after_change_buffer to be invalid, but
2105 possible. It can happen when combine-after-change-calls is
2106 non-nil, and insertion calls a file handler (e.g. through
2107 lock_file) which scribbles into a temp file -- cyd */
2108 if (!BUFFERP (combine_after_change_buffer)
e578f381 2109 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
c47a9ed1
CY
2110 {
2111 combine_after_change_list = Qnil;
2112 return Qnil;
2113 }
2114
66322887 2115 record_unwind_current_buffer ();
fb2e7d14
RS
2116
2117 Fset_buffer (combine_after_change_buffer);
2118
2119 /* # chars unchanged at beginning of buffer. */
2120 beg = Z - BEG;
2121 /* # chars unchanged at end of buffer. */
2122 end = beg;
2123 /* Total amount of insertion (negative for deletion). */
2124 change = 0;
2125
2126 /* Scan the various individual changes,
2127 accumulating the range info in BEG, END and CHANGE. */
2128 for (tail = combine_after_change_list; CONSP (tail);
03699b14 2129 tail = XCDR (tail))
fb2e7d14 2130 {
e688a080 2131 Lisp_Object elt;
d311d28c 2132 ptrdiff_t thisbeg, thisend, thischange;
fb2e7d14
RS
2133
2134 /* Extract the info from the next element. */
03699b14 2135 elt = XCAR (tail);
fb2e7d14
RS
2136 if (! CONSP (elt))
2137 continue;
03699b14 2138 thisbeg = XINT (XCAR (elt));
fb2e7d14 2139
03699b14 2140 elt = XCDR (elt);
fb2e7d14
RS
2141 if (! CONSP (elt))
2142 continue;
03699b14 2143 thisend = XINT (XCAR (elt));
fb2e7d14 2144
03699b14 2145 elt = XCDR (elt);
fb2e7d14
RS
2146 if (! CONSP (elt))
2147 continue;
03699b14 2148 thischange = XINT (XCAR (elt));
fb2e7d14
RS
2149
2150 /* Merge this range into the accumulated range. */
2151 change += thischange;
2152 if (thisbeg < beg)
2153 beg = thisbeg;
2154 if (thisend < end)
2155 end = thisend;
2156 }
2157
2158 /* Get the current start and end positions of the range
2159 that was changed. */
2160 begpos = BEG + beg;
2161 endpos = Z - end;
177c0ea7 2162
fb2e7d14
RS
2163 /* We are about to handle these, so discard them. */
2164 combine_after_change_list = Qnil;
2165
2166 /* Now run the after-change functions for real.
2167 Turn off the flag that defers them. */
2168 record_unwind_protect (Fcombine_after_change_execute_1,
2169 Vcombine_after_change_calls);
2170 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
0ef71121 2171 update_compositions (begpos, endpos, CHECK_ALL);
fb2e7d14 2172
101e446f 2173 return unbind_to (count, Qnil);
fb2e7d14
RS
2174}
2175\f
dfcf069d 2176void
971de7fb 2177syms_of_insdel (void)
fb2e7d14
RS
2178{
2179 staticpro (&combine_after_change_list);
bf0bf758 2180 staticpro (&combine_after_change_buffer);
fb2e7d14 2181 combine_after_change_list = Qnil;
101e446f 2182 combine_after_change_buffer = Qnil;
fb2e7d14 2183
29208e82 2184 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
9fc9a531 2185 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
fb2e7d14
RS
2186 Vcombine_after_change_calls = Qnil;
2187
29208e82 2188 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
335c5470
PJ
2189 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2190This affects `before-change-functions' and `after-change-functions',
2191as well as hooks attached to text properties and overlays. */);
7baf49cf 2192 inhibit_modification_hooks = 0;
cd3520a4 2193 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
7baf49cf 2194
fb2e7d14
RS
2195 defsubr (&Scombine_after_change_execute);
2196}