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