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