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