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