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