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