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