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