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