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