Inline functions to examine and change buffer intervals.
[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 (buffer_get_intervals (current_buffer))
848 offset_intervals (current_buffer, PT, nchars);
849
850 if (!inherit && buffer_get_intervals (current_buffer))
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_get_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 (buffer_get_intervals (current_buffer))
1021 {
1022 offset_intervals (current_buffer, GPT - nchars, nchars);
1023 graft_intervals_into_buffer (NULL, 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 (buffer_get_intervals (current_buffer))
1161 offset_intervals (current_buffer, PT, nchars);
1162
1163 /* Get the intervals for the part of the string we are inserting. */
1164 intervals = buffer_get_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
1229 if (buffer_get_intervals (current_buffer))
1230 offset_intervals (current_buffer, from, len - nchars_del);
1231
1232 if (from < PT)
1233 adjust_point (len - nchars_del, len_byte - nbytes_del);
1234
1235 /* As byte combining will decrease Z, we must check this again. */
1236 if (Z - GPT < END_UNCHANGED)
1237 END_UNCHANGED = Z - GPT;
1238
1239 check_markers ();
1240
1241 if (len == 0)
1242 evaporate_overlays (from);
1243 MODIFF++;
1244 CHARS_MODIFF = MODIFF;
1245 }
1246
1247 /* Record undo information, adjust markers and position keepers for an
1248 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1249 text already exists in the current buffer but character length (TO
1250 - FROM) may be incorrect, the correct length is NEWLEN. */
1251
1252 void
1253 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1254 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1255 {
1256 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1257
1258 if (GPT != to)
1259 move_gap_both (to, to_byte);
1260 GAP_SIZE += len_byte;
1261 GPT -= len; GPT_BYTE -= len_byte;
1262 ZV -= len; ZV_BYTE -= len_byte;
1263 Z -= len; Z_BYTE -= len_byte;
1264 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1265 }
1266 \f
1267 /* Replace the text from character positions FROM to TO with NEW,
1268 If PREPARE is nonzero, call prepare_to_modify_buffer.
1269 If INHERIT, the newly inserted text should inherit text properties
1270 from the surrounding non-deleted text. */
1271
1272 /* Note that this does not yet handle markers quite right.
1273 Also it needs to record a single undo-entry that does a replacement
1274 rather than a separate delete and insert.
1275 That way, undo will also handle markers properly.
1276
1277 But if MARKERS is 0, don't relocate markers. */
1278
1279 void
1280 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1281 int prepare, int inherit, int markers)
1282 {
1283 ptrdiff_t inschars = SCHARS (new);
1284 ptrdiff_t insbytes = SBYTES (new);
1285 ptrdiff_t from_byte, to_byte;
1286 ptrdiff_t nbytes_del, nchars_del;
1287 struct gcpro gcpro1;
1288 INTERVAL intervals;
1289 ptrdiff_t outgoing_insbytes = insbytes;
1290 Lisp_Object deletion;
1291
1292 check_markers ();
1293
1294 GCPRO1 (new);
1295 deletion = Qnil;
1296
1297 if (prepare)
1298 {
1299 ptrdiff_t range_length = to - from;
1300 prepare_to_modify_buffer (from, to, &from);
1301 to = from + range_length;
1302 }
1303
1304 UNGCPRO;
1305
1306 /* Make args be valid. */
1307 if (from < BEGV)
1308 from = BEGV;
1309 if (to > ZV)
1310 to = ZV;
1311
1312 from_byte = CHAR_TO_BYTE (from);
1313 to_byte = CHAR_TO_BYTE (to);
1314
1315 nchars_del = to - from;
1316 nbytes_del = to_byte - from_byte;
1317
1318 if (nbytes_del <= 0 && insbytes == 0)
1319 return;
1320
1321 /* Make OUTGOING_INSBYTES describe the text
1322 as it will be inserted in this buffer. */
1323
1324 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1325 outgoing_insbytes = inschars;
1326 else if (! STRING_MULTIBYTE (new))
1327 outgoing_insbytes
1328 = count_size_as_multibyte (SDATA (new), insbytes);
1329
1330 GCPRO1 (new);
1331
1332 /* Make sure the gap is somewhere in or next to what we are deleting. */
1333 if (from > GPT)
1334 gap_right (from, from_byte);
1335 if (to < GPT)
1336 gap_left (to, to_byte, 0);
1337
1338 /* Even if we don't record for undo, we must keep the original text
1339 because we may have to recover it because of inappropriate byte
1340 combining. */
1341 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1342 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1343
1344 GAP_SIZE += nbytes_del;
1345 ZV -= nchars_del;
1346 Z -= nchars_del;
1347 ZV_BYTE -= nbytes_del;
1348 Z_BYTE -= nbytes_del;
1349 GPT = from;
1350 GPT_BYTE = from_byte;
1351 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1352
1353 eassert (GPT <= GPT_BYTE);
1354
1355 if (GPT - BEG < BEG_UNCHANGED)
1356 BEG_UNCHANGED = GPT - BEG;
1357 if (Z - GPT < END_UNCHANGED)
1358 END_UNCHANGED = Z - GPT;
1359
1360 if (GAP_SIZE < outgoing_insbytes)
1361 make_gap (outgoing_insbytes - GAP_SIZE);
1362
1363 /* Copy the string text into the buffer, perhaps converting
1364 between single-byte and multibyte. */
1365 copy_text (SDATA (new), GPT_ADDR, insbytes,
1366 STRING_MULTIBYTE (new),
1367 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1368
1369 #ifdef BYTE_COMBINING_DEBUG
1370 /* We have copied text into the gap, but we have not marked
1371 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1372 here, for both the previous text and the following text.
1373 Meanwhile, GPT_ADDR does point to
1374 the text that has been stored by copy_text. */
1375 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1376 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1377 abort ();
1378 #endif
1379
1380 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1381 {
1382 /* Record the insertion first, so that when we undo,
1383 the deletion will be undone first. Thus, undo
1384 will insert before deleting, and thus will keep
1385 the markers before and after this text separate. */
1386 record_insert (from + SCHARS (deletion), inschars);
1387 record_delete (from, deletion);
1388 }
1389
1390 GAP_SIZE -= outgoing_insbytes;
1391 GPT += inschars;
1392 ZV += inschars;
1393 Z += inschars;
1394 GPT_BYTE += outgoing_insbytes;
1395 ZV_BYTE += outgoing_insbytes;
1396 Z_BYTE += outgoing_insbytes;
1397 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1398
1399 eassert (GPT <= GPT_BYTE);
1400
1401 /* Adjust the overlay center as needed. This must be done after
1402 adjusting the markers that bound the overlays. */
1403 adjust_overlays_for_delete (from, nchars_del);
1404 adjust_overlays_for_insert (from, inschars);
1405
1406 /* Adjust markers for the deletion and the insertion. */
1407 if (markers)
1408 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1409 inschars, outgoing_insbytes);
1410
1411 offset_intervals (current_buffer, from, inschars - nchars_del);
1412
1413 /* Get the intervals for the part of the string we are inserting--
1414 not including the combined-before bytes. */
1415 intervals = string_get_intervals (new);
1416 /* Insert those intervals. */
1417 graft_intervals_into_buffer (intervals, from, inschars,
1418 current_buffer, inherit);
1419
1420 /* Relocate point as if it were a marker. */
1421 if (from < PT)
1422 adjust_point ((from + inschars - (PT < to ? PT : to)),
1423 (from_byte + outgoing_insbytes
1424 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1425
1426 if (outgoing_insbytes == 0)
1427 evaporate_overlays (from);
1428
1429 check_markers ();
1430
1431 MODIFF++;
1432 CHARS_MODIFF = MODIFF;
1433 UNGCPRO;
1434
1435 signal_after_change (from, nchars_del, GPT - from);
1436 update_compositions (from, GPT, CHECK_BORDER);
1437 }
1438 \f
1439 /* Replace the text from character positions FROM to TO with
1440 the text in INS of length INSCHARS.
1441 Keep the text properties that applied to the old characters
1442 (extending them to all the new chars if there are more new chars).
1443
1444 Note that this does not yet handle markers quite right.
1445
1446 If MARKERS is nonzero, relocate markers.
1447
1448 Unlike most functions at this level, never call
1449 prepare_to_modify_buffer and never call signal_after_change. */
1450
1451 void
1452 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1453 ptrdiff_t to, ptrdiff_t to_byte,
1454 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1455 int markers)
1456 {
1457 ptrdiff_t nbytes_del, nchars_del;
1458
1459 check_markers ();
1460
1461 nchars_del = to - from;
1462 nbytes_del = to_byte - from_byte;
1463
1464 if (nbytes_del <= 0 && insbytes == 0)
1465 return;
1466
1467 /* Make sure the gap is somewhere in or next to what we are deleting. */
1468 if (from > GPT)
1469 gap_right (from, from_byte);
1470 if (to < GPT)
1471 gap_left (to, to_byte, 0);
1472
1473 GAP_SIZE += nbytes_del;
1474 ZV -= nchars_del;
1475 Z -= nchars_del;
1476 ZV_BYTE -= nbytes_del;
1477 Z_BYTE -= nbytes_del;
1478 GPT = from;
1479 GPT_BYTE = from_byte;
1480 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1481
1482 eassert (GPT <= GPT_BYTE);
1483
1484 if (GPT - BEG < BEG_UNCHANGED)
1485 BEG_UNCHANGED = GPT - BEG;
1486 if (Z - GPT < END_UNCHANGED)
1487 END_UNCHANGED = Z - GPT;
1488
1489 if (GAP_SIZE < insbytes)
1490 make_gap (insbytes - GAP_SIZE);
1491
1492 /* Copy the replacement text into the buffer. */
1493 memcpy (GPT_ADDR, ins, insbytes);
1494
1495 #ifdef BYTE_COMBINING_DEBUG
1496 /* We have copied text into the gap, but we have not marked
1497 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1498 here, for both the previous text and the following text.
1499 Meanwhile, GPT_ADDR does point to
1500 the text that has been stored by copy_text. */
1501 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1502 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1503 abort ();
1504 #endif
1505
1506 GAP_SIZE -= insbytes;
1507 GPT += inschars;
1508 ZV += inschars;
1509 Z += inschars;
1510 GPT_BYTE += insbytes;
1511 ZV_BYTE += insbytes;
1512 Z_BYTE += insbytes;
1513 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1514
1515 eassert (GPT <= GPT_BYTE);
1516
1517 /* Adjust the overlay center as needed. This must be done after
1518 adjusting the markers that bound the overlays. */
1519 if (nchars_del != inschars)
1520 {
1521 adjust_overlays_for_insert (from, inschars);
1522 adjust_overlays_for_delete (from + inschars, nchars_del);
1523 }
1524
1525 /* Adjust markers for the deletion and the insertion. */
1526 if (markers
1527 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1528 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1529 inschars, insbytes);
1530
1531 offset_intervals (current_buffer, from, inschars - nchars_del);
1532
1533 /* Relocate point as if it were a marker. */
1534 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1535 {
1536 if (PT < to)
1537 /* PT was within the deleted text. Move it to FROM. */
1538 adjust_point (from - PT, from_byte - PT_BYTE);
1539 else
1540 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1541 }
1542
1543 if (insbytes == 0)
1544 evaporate_overlays (from);
1545
1546 check_markers ();
1547
1548 MODIFF++;
1549 CHARS_MODIFF = MODIFF;
1550 }
1551 \f
1552 /* Delete characters in current buffer
1553 from FROM up to (but not including) TO.
1554 If TO comes before FROM, we delete nothing. */
1555
1556 void
1557 del_range (ptrdiff_t from, ptrdiff_t to)
1558 {
1559 del_range_1 (from, to, 1, 0);
1560 }
1561
1562 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1563 RET_STRING says to return the deleted text. */
1564
1565 Lisp_Object
1566 del_range_1 (ptrdiff_t from, ptrdiff_t to, int prepare, int ret_string)
1567 {
1568 ptrdiff_t from_byte, to_byte;
1569 Lisp_Object deletion;
1570 struct gcpro gcpro1;
1571
1572 /* Make args be valid */
1573 if (from < BEGV)
1574 from = BEGV;
1575 if (to > ZV)
1576 to = ZV;
1577
1578 if (to <= from)
1579 return Qnil;
1580
1581 if (prepare)
1582 {
1583 ptrdiff_t range_length = to - from;
1584 prepare_to_modify_buffer (from, to, &from);
1585 to = min (ZV, from + range_length);
1586 }
1587
1588 from_byte = CHAR_TO_BYTE (from);
1589 to_byte = CHAR_TO_BYTE (to);
1590
1591 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1592 GCPRO1 (deletion);
1593 signal_after_change (from, to - from, 0);
1594 update_compositions (from, from, CHECK_HEAD);
1595 UNGCPRO;
1596 return deletion;
1597 }
1598
1599 /* Like del_range_1 but args are byte positions, not char positions. */
1600
1601 void
1602 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, int prepare)
1603 {
1604 ptrdiff_t from, to;
1605
1606 /* Make args be valid */
1607 if (from_byte < BEGV_BYTE)
1608 from_byte = BEGV_BYTE;
1609 if (to_byte > ZV_BYTE)
1610 to_byte = ZV_BYTE;
1611
1612 if (to_byte <= from_byte)
1613 return;
1614
1615 from = BYTE_TO_CHAR (from_byte);
1616 to = BYTE_TO_CHAR (to_byte);
1617
1618 if (prepare)
1619 {
1620 ptrdiff_t old_from = from, old_to = Z - to;
1621 ptrdiff_t range_length = to - from;
1622 prepare_to_modify_buffer (from, to, &from);
1623 to = from + range_length;
1624
1625 if (old_from != from)
1626 from_byte = CHAR_TO_BYTE (from);
1627 if (to > ZV)
1628 {
1629 to = ZV;
1630 to_byte = ZV_BYTE;
1631 }
1632 else if (old_to == Z - to)
1633 to_byte = CHAR_TO_BYTE (to);
1634 }
1635
1636 del_range_2 (from, from_byte, to, to_byte, 0);
1637 signal_after_change (from, to - from, 0);
1638 update_compositions (from, from, CHECK_HEAD);
1639 }
1640
1641 /* Like del_range_1, but positions are specified both as charpos
1642 and bytepos. */
1643
1644 void
1645 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1646 ptrdiff_t to, ptrdiff_t to_byte, int prepare)
1647 {
1648 /* Make args be valid */
1649 if (from_byte < BEGV_BYTE)
1650 from_byte = BEGV_BYTE;
1651 if (to_byte > ZV_BYTE)
1652 to_byte = ZV_BYTE;
1653
1654 if (to_byte <= from_byte)
1655 return;
1656
1657 if (from < BEGV)
1658 from = BEGV;
1659 if (to > ZV)
1660 to = ZV;
1661
1662 if (prepare)
1663 {
1664 ptrdiff_t old_from = from, old_to = Z - to;
1665 ptrdiff_t range_length = to - from;
1666 prepare_to_modify_buffer (from, to, &from);
1667 to = from + range_length;
1668
1669 if (old_from != from)
1670 from_byte = CHAR_TO_BYTE (from);
1671 if (to > ZV)
1672 {
1673 to = ZV;
1674 to_byte = ZV_BYTE;
1675 }
1676 else if (old_to == Z - to)
1677 to_byte = CHAR_TO_BYTE (to);
1678 }
1679
1680 del_range_2 (from, from_byte, to, to_byte, 0);
1681 signal_after_change (from, to - from, 0);
1682 update_compositions (from, from, CHECK_HEAD);
1683 }
1684
1685 /* Delete a range of text, specified both as character positions
1686 and byte positions. FROM and TO are character positions,
1687 while FROM_BYTE and TO_BYTE are byte positions.
1688 If RET_STRING is true, the deleted area is returned as a string. */
1689
1690 Lisp_Object
1691 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1692 ptrdiff_t to, ptrdiff_t to_byte, int ret_string)
1693 {
1694 register ptrdiff_t nbytes_del, nchars_del;
1695 Lisp_Object deletion;
1696
1697 check_markers ();
1698
1699 nchars_del = to - from;
1700 nbytes_del = to_byte - from_byte;
1701
1702 /* Make sure the gap is somewhere in or next to what we are deleting. */
1703 if (from > GPT)
1704 gap_right (from, from_byte);
1705 if (to < GPT)
1706 gap_left (to, to_byte, 0);
1707
1708 #ifdef BYTE_COMBINING_DEBUG
1709 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1710 Z_BYTE - to_byte, from, from_byte))
1711 abort ();
1712 #endif
1713
1714 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1715 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1716 else
1717 deletion = Qnil;
1718
1719 /* Relocate all markers pointing into the new, larger gap
1720 to point at the end of the text before the gap.
1721 Do this before recording the deletion,
1722 so that undo handles this after reinserting the text. */
1723 adjust_markers_for_delete (from, from_byte, to, to_byte);
1724
1725 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1726 record_delete (from, deletion);
1727 MODIFF++;
1728 CHARS_MODIFF = MODIFF;
1729
1730 /* Relocate point as if it were a marker. */
1731 if (from < PT)
1732 adjust_point (from - (PT < to ? PT : to),
1733 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1734
1735 offset_intervals (current_buffer, from, - nchars_del);
1736
1737 /* Adjust the overlay center as needed. This must be done after
1738 adjusting the markers that bound the overlays. */
1739 adjust_overlays_for_delete (from, nchars_del);
1740
1741 GAP_SIZE += nbytes_del;
1742 ZV_BYTE -= nbytes_del;
1743 Z_BYTE -= nbytes_del;
1744 ZV -= nchars_del;
1745 Z -= nchars_del;
1746 GPT = from;
1747 GPT_BYTE = from_byte;
1748 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1749 /* Put an anchor, unless called from decode_coding_object which
1750 needs to access the previous gap contents. */
1751 *(GPT_ADDR) = 0;
1752
1753 eassert (GPT <= GPT_BYTE);
1754
1755 if (GPT - BEG < BEG_UNCHANGED)
1756 BEG_UNCHANGED = GPT - BEG;
1757 if (Z - GPT < END_UNCHANGED)
1758 END_UNCHANGED = Z - GPT;
1759
1760 check_markers ();
1761
1762 evaporate_overlays (from);
1763
1764 return deletion;
1765 }
1766 \f
1767 /* Call this if you're about to change the region of BUFFER from
1768 character positions START to END. This checks the read-only
1769 properties of the region, calls the necessary modification hooks,
1770 and warns the next redisplay that it should pay attention to that
1771 area.
1772
1773 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
1774 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1775
1776 void
1777 modify_region (struct buffer *buffer, ptrdiff_t start, ptrdiff_t end,
1778 int preserve_chars_modiff)
1779 {
1780 struct buffer *old_buffer = current_buffer;
1781
1782 if (buffer != old_buffer)
1783 set_buffer_internal (buffer);
1784
1785 prepare_to_modify_buffer (start, end, NULL);
1786
1787 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
1788
1789 if (MODIFF <= SAVE_MODIFF)
1790 record_first_change ();
1791 MODIFF++;
1792 if (! preserve_chars_modiff)
1793 CHARS_MODIFF = MODIFF;
1794
1795 BVAR (buffer, point_before_scroll) = Qnil;
1796
1797 if (buffer != old_buffer)
1798 set_buffer_internal (old_buffer);
1799 }
1800 \f
1801 /* Check that it is okay to modify the buffer between START and END,
1802 which are char positions.
1803
1804 Run the before-change-function, if any. If intervals are in use,
1805 verify that the text to be modified is not read-only, and call
1806 any modification properties the text may have.
1807
1808 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1809 by holding its value temporarily in a marker. */
1810
1811 void
1812 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1813 ptrdiff_t *preserve_ptr)
1814 {
1815 struct buffer *base_buffer;
1816
1817 if (!NILP (BVAR (current_buffer, read_only)))
1818 Fbarf_if_buffer_read_only ();
1819
1820 /* Let redisplay consider other windows than selected_window
1821 if modifying another buffer. */
1822 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1823 ++windows_or_buffers_changed;
1824
1825 if (buffer_get_intervals (current_buffer))
1826 {
1827 if (preserve_ptr)
1828 {
1829 Lisp_Object preserve_marker;
1830 struct gcpro gcpro1;
1831 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1832 GCPRO1 (preserve_marker);
1833 verify_interval_modification (current_buffer, start, end);
1834 *preserve_ptr = marker_position (preserve_marker);
1835 unchain_marker (XMARKER (preserve_marker));
1836 UNGCPRO;
1837 }
1838 else
1839 verify_interval_modification (current_buffer, start, end);
1840 }
1841
1842 /* For indirect buffers, use the base buffer to check clashes. */
1843 if (current_buffer->base_buffer != 0)
1844 base_buffer = current_buffer->base_buffer;
1845 else
1846 base_buffer = current_buffer;
1847
1848 #ifdef CLASH_DETECTION
1849 if (!NILP (BVAR (base_buffer, file_truename))
1850 /* Make binding buffer-file-name to nil effective. */
1851 && !NILP (BVAR (base_buffer, filename))
1852 && SAVE_MODIFF >= MODIFF)
1853 lock_file (BVAR (base_buffer, file_truename));
1854 #else
1855 /* At least warn if this file has changed on disk since it was visited. */
1856 if (!NILP (BVAR (base_buffer, filename))
1857 && SAVE_MODIFF >= MODIFF
1858 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1859 && !NILP (Ffile_exists_p (BVAR (base_buffer, filename))))
1860 call1 (intern ("ask-user-about-supersession-threat"),
1861 BVAR (base_buffer,filename));
1862 #endif /* not CLASH_DETECTION */
1863
1864 /* If `select-active-regions' is non-nil, save the region text. */
1865 if (!NILP (BVAR (current_buffer, mark_active))
1866 && !inhibit_modification_hooks
1867 && XMARKER (BVAR (current_buffer, mark))->buffer
1868 && NILP (Vsaved_region_selection)
1869 && (EQ (Vselect_active_regions, Qonly)
1870 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1871 : (!NILP (Vselect_active_regions)
1872 && !NILP (Vtransient_mark_mode))))
1873 {
1874 ptrdiff_t b = XMARKER (BVAR (current_buffer, mark))->charpos;
1875 ptrdiff_t e = PT;
1876 if (b < e)
1877 Vsaved_region_selection = make_buffer_string (b, e, 0);
1878 else if (b > e)
1879 Vsaved_region_selection = make_buffer_string (e, b, 0);
1880 }
1881
1882 signal_before_change (start, end, preserve_ptr);
1883
1884 if (current_buffer->newline_cache)
1885 invalidate_region_cache (current_buffer,
1886 current_buffer->newline_cache,
1887 start - BEG, Z - end);
1888 if (current_buffer->width_run_cache)
1889 invalidate_region_cache (current_buffer,
1890 current_buffer->width_run_cache,
1891 start - BEG, Z - end);
1892
1893 Vdeactivate_mark = Qt;
1894 }
1895 \f
1896 /* These macros work with an argument named `preserve_ptr'
1897 and a local variable named `preserve_marker'. */
1898
1899 #define PRESERVE_VALUE \
1900 if (preserve_ptr && NILP (preserve_marker)) \
1901 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1902
1903 #define RESTORE_VALUE \
1904 if (! NILP (preserve_marker)) \
1905 { \
1906 *preserve_ptr = marker_position (preserve_marker); \
1907 unchain_marker (XMARKER (preserve_marker)); \
1908 }
1909
1910 #define PRESERVE_START_END \
1911 if (NILP (start_marker)) \
1912 start_marker = Fcopy_marker (start, Qnil); \
1913 if (NILP (end_marker)) \
1914 end_marker = Fcopy_marker (end, Qnil);
1915
1916 #define FETCH_START \
1917 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1918
1919 #define FETCH_END \
1920 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1921
1922 /* Set a variable to nil if an error occurred.
1923 Don't change the variable if there was no error.
1924 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1925 VARIABLE is the variable to maybe set to nil.
1926 NO-ERROR-FLAG is nil if there was an error,
1927 anything else meaning no error (so this function does nothing). */
1928 static Lisp_Object
1929 reset_var_on_error (Lisp_Object val)
1930 {
1931 if (NILP (XCDR (val)))
1932 Fset (XCAR (val), Qnil);
1933 return Qnil;
1934 }
1935
1936 /* Signal a change to the buffer immediately before it happens.
1937 START_INT and END_INT are the bounds of the text to be changed.
1938
1939 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1940 by holding its value temporarily in a marker. */
1941
1942 static void
1943 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1944 ptrdiff_t *preserve_ptr)
1945 {
1946 Lisp_Object start, end;
1947 Lisp_Object start_marker, end_marker;
1948 Lisp_Object preserve_marker;
1949 struct gcpro gcpro1, gcpro2, gcpro3;
1950 ptrdiff_t count = SPECPDL_INDEX ();
1951
1952 if (inhibit_modification_hooks)
1953 return;
1954
1955 start = make_number (start_int);
1956 end = make_number (end_int);
1957 preserve_marker = Qnil;
1958 start_marker = Qnil;
1959 end_marker = Qnil;
1960 GCPRO3 (preserve_marker, start_marker, end_marker);
1961
1962 specbind (Qinhibit_modification_hooks, Qt);
1963
1964 /* If buffer is unmodified, run a special hook for that case. The
1965 check for Vfirst_change_hook is just a minor optimization. */
1966 if (SAVE_MODIFF >= MODIFF
1967 && !NILP (Vfirst_change_hook))
1968 {
1969 PRESERVE_VALUE;
1970 PRESERVE_START_END;
1971 Frun_hooks (1, &Qfirst_change_hook);
1972 }
1973
1974 /* Now run the before-change-functions if any. */
1975 if (!NILP (Vbefore_change_functions))
1976 {
1977 Lisp_Object args[3];
1978 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
1979
1980 PRESERVE_VALUE;
1981 PRESERVE_START_END;
1982
1983 /* Mark before-change-functions to be reset to nil in case of error. */
1984 record_unwind_protect (reset_var_on_error, rvoe_arg);
1985
1986 /* Actually run the hook functions. */
1987 args[0] = Qbefore_change_functions;
1988 args[1] = FETCH_START;
1989 args[2] = FETCH_END;
1990 Frun_hook_with_args (3, args);
1991
1992 /* There was no error: unarm the reset_on_error. */
1993 XSETCDR (rvoe_arg, Qt);
1994 }
1995
1996 if (current_buffer->overlays_before || current_buffer->overlays_after)
1997 {
1998 PRESERVE_VALUE;
1999 report_overlay_modification (FETCH_START, FETCH_END, 0,
2000 FETCH_START, FETCH_END, Qnil);
2001 }
2002
2003 if (! NILP (start_marker))
2004 free_marker (start_marker);
2005 if (! NILP (end_marker))
2006 free_marker (end_marker);
2007 RESTORE_VALUE;
2008 UNGCPRO;
2009
2010 unbind_to (count, Qnil);
2011 }
2012
2013 /* Signal a change immediately after it happens.
2014 CHARPOS is the character position of the start of the changed text.
2015 LENDEL is the number of characters of the text before the change.
2016 (Not the whole buffer; just the part that was changed.)
2017 LENINS is the number of characters in that part of the text
2018 after the change. */
2019
2020 void
2021 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2022 {
2023 ptrdiff_t count = SPECPDL_INDEX ();
2024 if (inhibit_modification_hooks)
2025 return;
2026
2027 /* If we are deferring calls to the after-change functions
2028 and there are no before-change functions,
2029 just record the args that we were going to use. */
2030 if (! NILP (Vcombine_after_change_calls)
2031 && NILP (Vbefore_change_functions)
2032 && !current_buffer->overlays_before
2033 && !current_buffer->overlays_after)
2034 {
2035 Lisp_Object elt;
2036
2037 if (!NILP (combine_after_change_list)
2038 && current_buffer != XBUFFER (combine_after_change_buffer))
2039 Fcombine_after_change_execute ();
2040
2041 elt = Fcons (make_number (charpos - BEG),
2042 Fcons (make_number (Z - (charpos - lendel + lenins)),
2043 Fcons (make_number (lenins - lendel), Qnil)));
2044 combine_after_change_list
2045 = Fcons (elt, combine_after_change_list);
2046 combine_after_change_buffer = Fcurrent_buffer ();
2047
2048 return;
2049 }
2050
2051 if (!NILP (combine_after_change_list))
2052 Fcombine_after_change_execute ();
2053
2054 specbind (Qinhibit_modification_hooks, Qt);
2055
2056 if (!NILP (Vafter_change_functions))
2057 {
2058 Lisp_Object args[4];
2059 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2060
2061 /* Mark after-change-functions to be reset to nil in case of error. */
2062 record_unwind_protect (reset_var_on_error, rvoe_arg);
2063
2064 /* Actually run the hook functions. */
2065 args[0] = Qafter_change_functions;
2066 XSETFASTINT (args[1], charpos);
2067 XSETFASTINT (args[2], charpos + lenins);
2068 XSETFASTINT (args[3], lendel);
2069 Frun_hook_with_args (4, args);
2070
2071 /* There was no error: unarm the reset_on_error. */
2072 XSETCDR (rvoe_arg, Qt);
2073 }
2074
2075 if (current_buffer->overlays_before || current_buffer->overlays_after)
2076 report_overlay_modification (make_number (charpos),
2077 make_number (charpos + lenins),
2078 1,
2079 make_number (charpos),
2080 make_number (charpos + lenins),
2081 make_number (lendel));
2082
2083 /* After an insertion, call the text properties
2084 insert-behind-hooks or insert-in-front-hooks. */
2085 if (lendel == 0)
2086 report_interval_modification (make_number (charpos),
2087 make_number (charpos + lenins));
2088
2089 unbind_to (count, Qnil);
2090 }
2091
2092 static Lisp_Object
2093 Fcombine_after_change_execute_1 (Lisp_Object val)
2094 {
2095 Vcombine_after_change_calls = val;
2096 return val;
2097 }
2098
2099 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2100 Scombine_after_change_execute, 0, 0, 0,
2101 doc: /* This function is for use internally in `combine-after-change-calls'. */)
2102 (void)
2103 {
2104 ptrdiff_t count = SPECPDL_INDEX ();
2105 ptrdiff_t beg, end, change;
2106 ptrdiff_t begpos, endpos;
2107 Lisp_Object tail;
2108
2109 if (NILP (combine_after_change_list))
2110 return Qnil;
2111
2112 /* It is rare for combine_after_change_buffer to be invalid, but
2113 possible. It can happen when combine-after-change-calls is
2114 non-nil, and insertion calls a file handler (e.g. through
2115 lock_file) which scribbles into a temp file -- cyd */
2116 if (!BUFFERP (combine_after_change_buffer)
2117 || NILP (BVAR (XBUFFER (combine_after_change_buffer), name)))
2118 {
2119 combine_after_change_list = Qnil;
2120 return Qnil;
2121 }
2122
2123 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2124
2125 Fset_buffer (combine_after_change_buffer);
2126
2127 /* # chars unchanged at beginning of buffer. */
2128 beg = Z - BEG;
2129 /* # chars unchanged at end of buffer. */
2130 end = beg;
2131 /* Total amount of insertion (negative for deletion). */
2132 change = 0;
2133
2134 /* Scan the various individual changes,
2135 accumulating the range info in BEG, END and CHANGE. */
2136 for (tail = combine_after_change_list; CONSP (tail);
2137 tail = XCDR (tail))
2138 {
2139 Lisp_Object elt;
2140 ptrdiff_t thisbeg, thisend, thischange;
2141
2142 /* Extract the info from the next element. */
2143 elt = XCAR (tail);
2144 if (! CONSP (elt))
2145 continue;
2146 thisbeg = XINT (XCAR (elt));
2147
2148 elt = XCDR (elt);
2149 if (! CONSP (elt))
2150 continue;
2151 thisend = XINT (XCAR (elt));
2152
2153 elt = XCDR (elt);
2154 if (! CONSP (elt))
2155 continue;
2156 thischange = XINT (XCAR (elt));
2157
2158 /* Merge this range into the accumulated range. */
2159 change += thischange;
2160 if (thisbeg < beg)
2161 beg = thisbeg;
2162 if (thisend < end)
2163 end = thisend;
2164 }
2165
2166 /* Get the current start and end positions of the range
2167 that was changed. */
2168 begpos = BEG + beg;
2169 endpos = Z - end;
2170
2171 /* We are about to handle these, so discard them. */
2172 combine_after_change_list = Qnil;
2173
2174 /* Now run the after-change functions for real.
2175 Turn off the flag that defers them. */
2176 record_unwind_protect (Fcombine_after_change_execute_1,
2177 Vcombine_after_change_calls);
2178 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2179 update_compositions (begpos, endpos, CHECK_ALL);
2180
2181 return unbind_to (count, Qnil);
2182 }
2183 \f
2184 void
2185 syms_of_insdel (void)
2186 {
2187 staticpro (&combine_after_change_list);
2188 staticpro (&combine_after_change_buffer);
2189 combine_after_change_list = Qnil;
2190 combine_after_change_buffer = Qnil;
2191
2192 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2193 doc: /* Used internally by the `combine-after-change-calls' macro. */);
2194 Vcombine_after_change_calls = Qnil;
2195
2196 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2197 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2198 This affects `before-change-functions' and `after-change-functions',
2199 as well as hooks attached to text properties and overlays. */);
2200 inhibit_modification_hooks = 0;
2201 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2202
2203 defsubr (&Scombine_after_change_execute);
2204 }