]> code.delx.au - gnu-emacs/blob - src/insdel.c
Include-file cleanup for src directory
[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 "composite.h"
27 #include "intervals.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "window.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 ptrdiff_t outgoing_nbytes = nbytes;
896 INTERVAL intervals;
897
898 /* Make OUTGOING_NBYTES describe the text
899 as it will be inserted in this buffer. */
900
901 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
902 outgoing_nbytes = nchars;
903 else if (! STRING_MULTIBYTE (string))
904 outgoing_nbytes
905 = count_size_as_multibyte (SDATA (string) + pos_byte,
906 nbytes);
907
908 /* Do this before moving and increasing the gap,
909 because the before-change hooks might move the gap
910 or make it smaller. */
911 prepare_to_modify_buffer (PT, PT, NULL);
912
913 if (PT != GPT)
914 move_gap_both (PT, PT_BYTE);
915 if (GAP_SIZE < outgoing_nbytes)
916 make_gap (outgoing_nbytes - GAP_SIZE);
917
918 /* Copy the string text into the buffer, perhaps converting
919 between single-byte and multibyte. */
920 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
921 STRING_MULTIBYTE (string),
922 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
923
924 #ifdef BYTE_COMBINING_DEBUG
925 /* We have copied text into the gap, but we have not altered
926 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
927 to these functions and get the same results as we would
928 have got earlier on. Meanwhile, PT_ADDR does point to
929 the text that has been stored by copy_text. */
930 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
931 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
932 emacs_abort ();
933 #endif
934
935 record_insert (PT, nchars);
936 MODIFF++;
937 CHARS_MODIFF = MODIFF;
938
939 GAP_SIZE -= outgoing_nbytes;
940 GPT += nchars;
941 ZV += nchars;
942 Z += nchars;
943 GPT_BYTE += outgoing_nbytes;
944 ZV_BYTE += outgoing_nbytes;
945 Z_BYTE += outgoing_nbytes;
946 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
947
948 eassert (GPT <= GPT_BYTE);
949
950 /* The insert may have been in the unchanged region, so check again. */
951 if (Z - GPT < END_UNCHANGED)
952 END_UNCHANGED = Z - GPT;
953
954 adjust_overlays_for_insert (PT, nchars);
955 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
956 PT_BYTE + outgoing_nbytes,
957 before_markers);
958
959 offset_intervals (current_buffer, PT, nchars);
960
961 intervals = string_intervals (string);
962 /* Get the intervals for the part of the string we are inserting. */
963 if (nbytes < SBYTES (string))
964 intervals = copy_intervals (intervals, pos, nchars);
965
966 /* Insert those intervals. */
967 graft_intervals_into_buffer (intervals, PT, nchars,
968 current_buffer, inherit);
969
970 adjust_point (nchars, outgoing_nbytes);
971
972 check_markers ();
973 }
974 \f
975 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
976 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
977 GPT_ADDR (if not text_at_gap_tail). */
978
979 void
980 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
981 {
982 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
983
984 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
985 nchars = nbytes;
986
987 /* No need to call prepare_to_modify_buffer, since this is called
988 from places that replace some region with a different text, so
989 prepare_to_modify_buffer was already called by the deletion part
990 of this dance. */
991 invalidate_buffer_caches (current_buffer, GPT, GPT);
992 record_insert (GPT, nchars);
993 MODIFF++;
994
995 GAP_SIZE -= nbytes;
996 if (! text_at_gap_tail)
997 {
998 GPT += nchars;
999 GPT_BYTE += nbytes;
1000 }
1001 ZV += nchars;
1002 Z += nchars;
1003 ZV_BYTE += nbytes;
1004 Z_BYTE += nbytes;
1005 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1006
1007 eassert (GPT <= GPT_BYTE);
1008
1009 adjust_overlays_for_insert (ins_charpos, nchars);
1010 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1011 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1012
1013 if (buffer_intervals (current_buffer))
1014 {
1015 offset_intervals (current_buffer, ins_charpos, nchars);
1016 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1017 current_buffer, 0);
1018 }
1019
1020 if (ins_charpos < PT)
1021 adjust_point (nchars, nbytes);
1022
1023 check_markers ();
1024 }
1025 \f
1026 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1027 current buffer. If the text in BUF has properties, they are absorbed
1028 into the current buffer.
1029
1030 It does not work to use `insert' for this, because a malloc could happen
1031 and relocate BUF's text before the copy happens. */
1032
1033 void
1034 insert_from_buffer (struct buffer *buf,
1035 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1036 {
1037 ptrdiff_t opoint = PT;
1038
1039 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1040 signal_after_change (opoint, 0, PT - opoint);
1041 update_compositions (opoint, PT, CHECK_BORDER);
1042 }
1043
1044 static void
1045 insert_from_buffer_1 (struct buffer *buf,
1046 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1047 {
1048 ptrdiff_t chunk, chunk_expanded;
1049 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1050 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1051 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1052 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1053 INTERVAL intervals;
1054
1055 if (nchars == 0)
1056 return;
1057
1058 /* Make OUTGOING_NBYTES describe the text
1059 as it will be inserted in this buffer. */
1060
1061 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1062 outgoing_nbytes = nchars;
1063 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1064 {
1065 ptrdiff_t outgoing_before_gap = 0;
1066 ptrdiff_t outgoing_after_gap = 0;
1067
1068 if (from < BUF_GPT (buf))
1069 {
1070 chunk = BUF_GPT_BYTE (buf) - from_byte;
1071 if (chunk > incoming_nbytes)
1072 chunk = incoming_nbytes;
1073 outgoing_before_gap
1074 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1075 chunk);
1076 }
1077 else
1078 chunk = 0;
1079
1080 if (chunk < incoming_nbytes)
1081 outgoing_after_gap
1082 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1083 from_byte + chunk),
1084 incoming_nbytes - chunk);
1085
1086 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1087 }
1088
1089 /* Do this before moving and increasing the gap,
1090 because the before-change hooks might move the gap
1091 or make it smaller. */
1092 prepare_to_modify_buffer (PT, PT, NULL);
1093
1094 if (PT != GPT)
1095 move_gap_both (PT, PT_BYTE);
1096 if (GAP_SIZE < outgoing_nbytes)
1097 make_gap (outgoing_nbytes - GAP_SIZE);
1098
1099 if (from < BUF_GPT (buf))
1100 {
1101 chunk = BUF_GPT_BYTE (buf) - from_byte;
1102 if (chunk > incoming_nbytes)
1103 chunk = incoming_nbytes;
1104 /* Record number of output bytes, so we know where
1105 to put the output from the second copy_text. */
1106 chunk_expanded
1107 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1108 GPT_ADDR, chunk,
1109 ! NILP (BVAR (buf, enable_multibyte_characters)),
1110 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1111 }
1112 else
1113 chunk_expanded = chunk = 0;
1114
1115 if (chunk < incoming_nbytes)
1116 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1117 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1118 ! NILP (BVAR (buf, enable_multibyte_characters)),
1119 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1120
1121 #ifdef BYTE_COMBINING_DEBUG
1122 /* We have copied text into the gap, but we have not altered
1123 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1124 to these functions and get the same results as we would
1125 have got earlier on. Meanwhile, GPT_ADDR does point to
1126 the text that has been stored by copy_text. */
1127 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1128 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1129 emacs_abort ();
1130 #endif
1131
1132 record_insert (PT, nchars);
1133 MODIFF++;
1134 CHARS_MODIFF = MODIFF;
1135
1136 GAP_SIZE -= outgoing_nbytes;
1137 GPT += nchars;
1138 ZV += nchars;
1139 Z += nchars;
1140 GPT_BYTE += outgoing_nbytes;
1141 ZV_BYTE += outgoing_nbytes;
1142 Z_BYTE += outgoing_nbytes;
1143 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1144
1145 eassert (GPT <= GPT_BYTE);
1146
1147 /* The insert may have been in the unchanged region, so check again. */
1148 if (Z - GPT < END_UNCHANGED)
1149 END_UNCHANGED = Z - GPT;
1150
1151 adjust_overlays_for_insert (PT, nchars);
1152 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1153 PT_BYTE + outgoing_nbytes,
1154 0);
1155
1156 offset_intervals (current_buffer, PT, nchars);
1157
1158 /* Get the intervals for the part of the string we are inserting. */
1159 intervals = buffer_intervals (buf);
1160 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1161 {
1162 if (buf == current_buffer && PT <= from)
1163 from += nchars;
1164 intervals = copy_intervals (intervals, from, nchars);
1165 }
1166
1167 /* Insert those intervals. */
1168 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1169
1170 adjust_point (nchars, outgoing_nbytes);
1171 }
1172 \f
1173 /* Record undo information and adjust markers and position keepers for
1174 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1175 chars (LEN_BYTE bytes) which resides in the gap just after
1176 GPT_ADDR.
1177
1178 PREV_TEXT nil means the new text was just inserted. */
1179
1180 static void
1181 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1182 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1183 {
1184 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1185
1186 #ifdef BYTE_COMBINING_DEBUG
1187 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1188 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1189 emacs_abort ();
1190 #endif
1191
1192 if (STRINGP (prev_text))
1193 {
1194 nchars_del = SCHARS (prev_text);
1195 nbytes_del = SBYTES (prev_text);
1196 }
1197
1198 /* Update various buffer positions for the new text. */
1199 GAP_SIZE -= len_byte;
1200 ZV += len; Z += len;
1201 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1202 GPT += len; GPT_BYTE += len_byte;
1203 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1204
1205 if (nchars_del > 0)
1206 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1207 len, len_byte);
1208 else
1209 adjust_markers_for_insert (from, from_byte,
1210 from + len, from_byte + len_byte, 0);
1211
1212 if (nchars_del > 0)
1213 record_delete (from, prev_text, false);
1214 record_insert (from, len);
1215
1216 if (len > nchars_del)
1217 adjust_overlays_for_insert (from, len - nchars_del);
1218 else if (len < nchars_del)
1219 adjust_overlays_for_delete (from, nchars_del - len);
1220
1221 offset_intervals (current_buffer, from, len - nchars_del);
1222
1223 if (from < PT)
1224 adjust_point (len - nchars_del, len_byte - nbytes_del);
1225
1226 /* As byte combining will decrease Z, we must check this again. */
1227 if (Z - GPT < END_UNCHANGED)
1228 END_UNCHANGED = Z - GPT;
1229
1230 check_markers ();
1231
1232 if (len == 0)
1233 evaporate_overlays (from);
1234 MODIFF++;
1235 CHARS_MODIFF = MODIFF;
1236 }
1237
1238 /* Record undo information, adjust markers and position keepers for an
1239 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1240 text already exists in the current buffer but character length (TO
1241 - FROM) may be incorrect, the correct length is NEWLEN. */
1242
1243 void
1244 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1245 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1246 {
1247 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1248
1249 if (GPT != to)
1250 move_gap_both (to, to_byte);
1251 GAP_SIZE += len_byte;
1252 GPT -= len; GPT_BYTE -= len_byte;
1253 ZV -= len; ZV_BYTE -= len_byte;
1254 Z -= len; Z_BYTE -= len_byte;
1255 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1256 }
1257 \f
1258 /* Replace the text from character positions FROM to TO with NEW,
1259 If PREPARE, call prepare_to_modify_buffer.
1260 If INHERIT, the newly inserted text should inherit text properties
1261 from the surrounding non-deleted text. */
1262
1263 /* Note that this does not yet handle markers quite right.
1264 Also it needs to record a single undo-entry that does a replacement
1265 rather than a separate delete and insert.
1266 That way, undo will also handle markers properly.
1267
1268 But if MARKERS is 0, don't relocate markers. */
1269
1270 void
1271 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1272 bool prepare, bool inherit, bool markers)
1273 {
1274 ptrdiff_t inschars = SCHARS (new);
1275 ptrdiff_t insbytes = SBYTES (new);
1276 ptrdiff_t from_byte, to_byte;
1277 ptrdiff_t nbytes_del, nchars_del;
1278 INTERVAL intervals;
1279 ptrdiff_t outgoing_insbytes = insbytes;
1280 Lisp_Object deletion;
1281
1282 check_markers ();
1283
1284 deletion = Qnil;
1285
1286 if (prepare)
1287 {
1288 ptrdiff_t range_length = to - from;
1289 prepare_to_modify_buffer (from, to, &from);
1290 to = from + range_length;
1291 }
1292
1293 /* Make args be valid. */
1294 if (from < BEGV)
1295 from = BEGV;
1296 if (to > ZV)
1297 to = ZV;
1298
1299 from_byte = CHAR_TO_BYTE (from);
1300 to_byte = CHAR_TO_BYTE (to);
1301
1302 nchars_del = to - from;
1303 nbytes_del = to_byte - from_byte;
1304
1305 if (nbytes_del <= 0 && insbytes == 0)
1306 return;
1307
1308 /* Make OUTGOING_INSBYTES describe the text
1309 as it will be inserted in this buffer. */
1310
1311 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1312 outgoing_insbytes = inschars;
1313 else if (! STRING_MULTIBYTE (new))
1314 outgoing_insbytes
1315 = count_size_as_multibyte (SDATA (new), insbytes);
1316
1317 /* Make sure the gap is somewhere in or next to what we are deleting. */
1318 if (from > GPT)
1319 gap_right (from, from_byte);
1320 if (to < GPT)
1321 gap_left (to, to_byte, 0);
1322
1323 /* Even if we don't record for undo, we must keep the original text
1324 because we may have to recover it because of inappropriate byte
1325 combining. */
1326 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1327 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1328
1329 GAP_SIZE += nbytes_del;
1330 ZV -= nchars_del;
1331 Z -= nchars_del;
1332 ZV_BYTE -= nbytes_del;
1333 Z_BYTE -= nbytes_del;
1334 GPT = from;
1335 GPT_BYTE = from_byte;
1336 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1337
1338 eassert (GPT <= GPT_BYTE);
1339
1340 if (GPT - BEG < BEG_UNCHANGED)
1341 BEG_UNCHANGED = GPT - BEG;
1342 if (Z - GPT < END_UNCHANGED)
1343 END_UNCHANGED = Z - GPT;
1344
1345 if (GAP_SIZE < outgoing_insbytes)
1346 make_gap (outgoing_insbytes - GAP_SIZE);
1347
1348 /* Copy the string text into the buffer, perhaps converting
1349 between single-byte and multibyte. */
1350 copy_text (SDATA (new), GPT_ADDR, insbytes,
1351 STRING_MULTIBYTE (new),
1352 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1353
1354 #ifdef BYTE_COMBINING_DEBUG
1355 /* We have copied text into the gap, but we have not marked
1356 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1357 here, for both the previous text and the following text.
1358 Meanwhile, GPT_ADDR does point to
1359 the text that has been stored by copy_text. */
1360 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1361 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1362 emacs_abort ();
1363 #endif
1364
1365 /* Record the insertion first, so that when we undo,
1366 the deletion will be undone first. Thus, undo
1367 will insert before deleting, and thus will keep
1368 the markers before and after this text separate. */
1369 if (!NILP (deletion))
1370 {
1371 record_insert (from + SCHARS (deletion), inschars);
1372 record_delete (from, deletion, false);
1373 }
1374
1375 GAP_SIZE -= outgoing_insbytes;
1376 GPT += inschars;
1377 ZV += inschars;
1378 Z += inschars;
1379 GPT_BYTE += outgoing_insbytes;
1380 ZV_BYTE += outgoing_insbytes;
1381 Z_BYTE += outgoing_insbytes;
1382 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1383
1384 eassert (GPT <= GPT_BYTE);
1385
1386 /* Adjust markers for the deletion and the insertion. */
1387 if (markers)
1388 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1389 inschars, outgoing_insbytes);
1390
1391 /* Adjust the overlay center as needed. This must be done after
1392 adjusting the markers that bound the overlays. */
1393 adjust_overlays_for_delete (from, nchars_del);
1394 adjust_overlays_for_insert (from, inschars);
1395
1396 offset_intervals (current_buffer, from, inschars - nchars_del);
1397
1398 /* Get the intervals for the part of the string we are inserting--
1399 not including the combined-before bytes. */
1400 intervals = string_intervals (new);
1401 /* Insert those intervals. */
1402 graft_intervals_into_buffer (intervals, from, inschars,
1403 current_buffer, inherit);
1404
1405 /* Relocate point as if it were a marker. */
1406 if (from < PT)
1407 adjust_point ((from + inschars - (PT < to ? PT : to)),
1408 (from_byte + outgoing_insbytes
1409 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1410
1411 if (outgoing_insbytes == 0)
1412 evaporate_overlays (from);
1413
1414 check_markers ();
1415
1416 MODIFF++;
1417 CHARS_MODIFF = MODIFF;
1418
1419 signal_after_change (from, nchars_del, GPT - from);
1420 update_compositions (from, GPT, CHECK_BORDER);
1421 }
1422 \f
1423 /* Replace the text from character positions FROM to TO with
1424 the text in INS of length INSCHARS.
1425 Keep the text properties that applied to the old characters
1426 (extending them to all the new chars if there are more new chars).
1427
1428 Note that this does not yet handle markers quite right.
1429
1430 If MARKERS, relocate markers.
1431
1432 Unlike most functions at this level, never call
1433 prepare_to_modify_buffer and never call signal_after_change. */
1434
1435 void
1436 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1437 ptrdiff_t to, ptrdiff_t to_byte,
1438 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1439 bool markers)
1440 {
1441 ptrdiff_t nbytes_del, nchars_del;
1442
1443 check_markers ();
1444
1445 nchars_del = to - from;
1446 nbytes_del = to_byte - from_byte;
1447
1448 if (nbytes_del <= 0 && insbytes == 0)
1449 return;
1450
1451 /* Make sure the gap is somewhere in or next to what we are deleting. */
1452 if (from > GPT)
1453 gap_right (from, from_byte);
1454 if (to < GPT)
1455 gap_left (to, to_byte, 0);
1456
1457 GAP_SIZE += nbytes_del;
1458 ZV -= nchars_del;
1459 Z -= nchars_del;
1460 ZV_BYTE -= nbytes_del;
1461 Z_BYTE -= nbytes_del;
1462 GPT = from;
1463 GPT_BYTE = from_byte;
1464 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1465
1466 eassert (GPT <= GPT_BYTE);
1467
1468 if (GPT - BEG < BEG_UNCHANGED)
1469 BEG_UNCHANGED = GPT - BEG;
1470 if (Z - GPT < END_UNCHANGED)
1471 END_UNCHANGED = Z - GPT;
1472
1473 if (GAP_SIZE < insbytes)
1474 make_gap (insbytes - GAP_SIZE);
1475
1476 /* Copy the replacement text into the buffer. */
1477 memcpy (GPT_ADDR, ins, insbytes);
1478
1479 #ifdef BYTE_COMBINING_DEBUG
1480 /* We have copied text into the gap, but we have not marked
1481 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1482 here, for both the previous text and the following text.
1483 Meanwhile, GPT_ADDR does point to
1484 the text that has been stored by copy_text. */
1485 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1486 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1487 emacs_abort ();
1488 #endif
1489
1490 GAP_SIZE -= insbytes;
1491 GPT += inschars;
1492 ZV += inschars;
1493 Z += inschars;
1494 GPT_BYTE += insbytes;
1495 ZV_BYTE += insbytes;
1496 Z_BYTE += insbytes;
1497 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1498
1499 eassert (GPT <= GPT_BYTE);
1500
1501 /* Adjust markers for the deletion and the insertion. */
1502 if (markers
1503 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1504 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1505 inschars, insbytes);
1506
1507 /* Adjust the overlay center as needed. This must be done after
1508 adjusting the markers that bound the overlays. */
1509 if (nchars_del != inschars)
1510 {
1511 adjust_overlays_for_insert (from, inschars);
1512 adjust_overlays_for_delete (from + inschars, nchars_del);
1513 }
1514
1515 offset_intervals (current_buffer, from, inschars - nchars_del);
1516
1517 /* Relocate point as if it were a marker. */
1518 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1519 {
1520 if (PT < to)
1521 /* PT was within the deleted text. Move it to FROM. */
1522 adjust_point (from - PT, from_byte - PT_BYTE);
1523 else
1524 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1525 }
1526
1527 if (insbytes == 0)
1528 evaporate_overlays (from);
1529
1530 check_markers ();
1531
1532 MODIFF++;
1533 CHARS_MODIFF = MODIFF;
1534 }
1535 \f
1536 /* Delete characters in current buffer
1537 from FROM up to (but not including) TO.
1538 If TO comes before FROM, we delete nothing. */
1539
1540 void
1541 del_range (ptrdiff_t from, ptrdiff_t to)
1542 {
1543 del_range_1 (from, to, 1, 0);
1544 }
1545
1546 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1547 RET_STRING says to return the deleted text. */
1548
1549 Lisp_Object
1550 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1551 {
1552 ptrdiff_t from_byte, to_byte;
1553 Lisp_Object deletion;
1554
1555 /* Make args be valid */
1556 if (from < BEGV)
1557 from = BEGV;
1558 if (to > ZV)
1559 to = ZV;
1560
1561 if (to <= from)
1562 return Qnil;
1563
1564 if (prepare)
1565 {
1566 ptrdiff_t range_length = to - from;
1567 prepare_to_modify_buffer (from, to, &from);
1568 to = min (ZV, from + range_length);
1569 }
1570
1571 from_byte = CHAR_TO_BYTE (from);
1572 to_byte = CHAR_TO_BYTE (to);
1573
1574 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1575 signal_after_change (from, to - from, 0);
1576 update_compositions (from, from, CHECK_HEAD);
1577 return deletion;
1578 }
1579
1580 /* Like del_range_1 but args are byte positions, not char positions. */
1581
1582 void
1583 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1584 {
1585 ptrdiff_t from, to;
1586
1587 /* Make args be valid. */
1588 if (from_byte < BEGV_BYTE)
1589 from_byte = BEGV_BYTE;
1590 if (to_byte > ZV_BYTE)
1591 to_byte = ZV_BYTE;
1592
1593 if (to_byte <= from_byte)
1594 return;
1595
1596 from = BYTE_TO_CHAR (from_byte);
1597 to = BYTE_TO_CHAR (to_byte);
1598
1599 if (prepare)
1600 {
1601 ptrdiff_t old_from = from, old_to = Z - to;
1602 ptrdiff_t range_length = to - from;
1603 prepare_to_modify_buffer (from, to, &from);
1604 to = from + range_length;
1605
1606 if (old_from != from)
1607 from_byte = CHAR_TO_BYTE (from);
1608 if (to > ZV)
1609 {
1610 to = ZV;
1611 to_byte = ZV_BYTE;
1612 }
1613 else if (old_to == Z - to)
1614 to_byte = CHAR_TO_BYTE (to);
1615 }
1616
1617 del_range_2 (from, from_byte, to, to_byte, 0);
1618 signal_after_change (from, to - from, 0);
1619 update_compositions (from, from, CHECK_HEAD);
1620 }
1621
1622 /* Like del_range_1, but positions are specified both as charpos
1623 and bytepos. */
1624
1625 void
1626 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1627 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1628 {
1629 /* Make args be valid */
1630 if (from_byte < BEGV_BYTE)
1631 from_byte = BEGV_BYTE;
1632 if (to_byte > ZV_BYTE)
1633 to_byte = ZV_BYTE;
1634
1635 if (to_byte <= from_byte)
1636 return;
1637
1638 if (from < BEGV)
1639 from = BEGV;
1640 if (to > ZV)
1641 to = ZV;
1642
1643 if (prepare)
1644 {
1645 ptrdiff_t old_from = from, old_to = Z - to;
1646 ptrdiff_t range_length = to - from;
1647 prepare_to_modify_buffer (from, to, &from);
1648 to = from + range_length;
1649
1650 if (old_from != from)
1651 from_byte = CHAR_TO_BYTE (from);
1652 if (to > ZV)
1653 {
1654 to = ZV;
1655 to_byte = ZV_BYTE;
1656 }
1657 else if (old_to == Z - to)
1658 to_byte = CHAR_TO_BYTE (to);
1659 }
1660
1661 del_range_2 (from, from_byte, to, to_byte, 0);
1662 signal_after_change (from, to - from, 0);
1663 update_compositions (from, from, CHECK_HEAD);
1664 }
1665
1666 /* Delete a range of text, specified both as character positions
1667 and byte positions. FROM and TO are character positions,
1668 while FROM_BYTE and TO_BYTE are byte positions.
1669 If RET_STRING, the deleted area is returned as a string. */
1670
1671 Lisp_Object
1672 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1673 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1674 {
1675 ptrdiff_t nbytes_del, nchars_del;
1676 Lisp_Object deletion;
1677
1678 check_markers ();
1679
1680 nchars_del = to - from;
1681 nbytes_del = to_byte - from_byte;
1682
1683 /* Make sure the gap is somewhere in or next to what we are deleting. */
1684 if (from > GPT)
1685 gap_right (from, from_byte);
1686 if (to < GPT)
1687 gap_left (to, to_byte, 0);
1688
1689 #ifdef BYTE_COMBINING_DEBUG
1690 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1691 Z_BYTE - to_byte, from, from_byte))
1692 emacs_abort ();
1693 #endif
1694
1695 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1696 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1697 else
1698 deletion = Qnil;
1699
1700 /* Record marker adjustments, and text deletion into undo
1701 history. */
1702 record_delete (from, deletion, true);
1703
1704 /* Relocate all markers pointing into the new, larger gap to point
1705 at the end of the text before the gap. */
1706 adjust_markers_for_delete (from, from_byte, to, to_byte);
1707
1708 MODIFF++;
1709 CHARS_MODIFF = MODIFF;
1710
1711 /* Relocate point as if it were a marker. */
1712 if (from < PT)
1713 adjust_point (from - (PT < to ? PT : to),
1714 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1715
1716 offset_intervals (current_buffer, from, - nchars_del);
1717
1718 /* Adjust the overlay center as needed. This must be done after
1719 adjusting the markers that bound the overlays. */
1720 adjust_overlays_for_delete (from, nchars_del);
1721
1722 GAP_SIZE += nbytes_del;
1723 ZV_BYTE -= nbytes_del;
1724 Z_BYTE -= nbytes_del;
1725 ZV -= nchars_del;
1726 Z -= nchars_del;
1727 GPT = from;
1728 GPT_BYTE = from_byte;
1729 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1730 /* Put an anchor, unless called from decode_coding_object which
1731 needs to access the previous gap contents. */
1732 *(GPT_ADDR) = 0;
1733
1734 eassert (GPT <= GPT_BYTE);
1735
1736 if (GPT - BEG < BEG_UNCHANGED)
1737 BEG_UNCHANGED = GPT - BEG;
1738 if (Z - GPT < END_UNCHANGED)
1739 END_UNCHANGED = Z - GPT;
1740
1741 check_markers ();
1742
1743 evaporate_overlays (from);
1744
1745 return deletion;
1746 }
1747
1748 /* Call this if you're about to change the text of current buffer
1749 from character positions START to END. This checks the read-only
1750 properties of the region, calls the necessary modification hooks,
1751 and warns the next redisplay that it should pay attention to that
1752 area. */
1753
1754 void
1755 modify_text (ptrdiff_t start, ptrdiff_t end)
1756 {
1757 prepare_to_modify_buffer (start, end, NULL);
1758
1759 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1760 if (MODIFF <= SAVE_MODIFF)
1761 record_first_change ();
1762 MODIFF++;
1763 CHARS_MODIFF = MODIFF;
1764
1765 bset_point_before_scroll (current_buffer, Qnil);
1766 }
1767
1768 /* Check that it is okay to modify the buffer between START and END,
1769 which are char positions.
1770
1771 Run the before-change-function, if any. If intervals are in use,
1772 verify that the text to be modified is not read-only, and call
1773 any modification properties the text may have.
1774
1775 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1776 by holding its value temporarily in a marker. */
1777
1778 void
1779 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1780 ptrdiff_t *preserve_ptr)
1781 {
1782 struct buffer *base_buffer;
1783 Lisp_Object temp;
1784
1785 XSETFASTINT (temp, start);
1786 if (!NILP (BVAR (current_buffer, read_only)))
1787 Fbarf_if_buffer_read_only (temp);
1788
1789 bset_redisplay (current_buffer);
1790
1791 if (buffer_intervals (current_buffer))
1792 {
1793 if (preserve_ptr)
1794 {
1795 Lisp_Object preserve_marker;
1796 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1797 verify_interval_modification (current_buffer, start, end);
1798 *preserve_ptr = marker_position (preserve_marker);
1799 unchain_marker (XMARKER (preserve_marker));
1800 }
1801 else
1802 verify_interval_modification (current_buffer, start, end);
1803 }
1804
1805 /* For indirect buffers, use the base buffer to check clashes. */
1806 if (current_buffer->base_buffer != 0)
1807 base_buffer = current_buffer->base_buffer;
1808 else
1809 base_buffer = current_buffer;
1810
1811 if (inhibit_modification_hooks)
1812 return;
1813
1814 if (!NILP (BVAR (base_buffer, file_truename))
1815 /* Make binding buffer-file-name to nil effective. */
1816 && !NILP (BVAR (base_buffer, filename))
1817 && SAVE_MODIFF >= MODIFF)
1818 lock_file (BVAR (base_buffer, file_truename));
1819
1820 /* If `select-active-regions' is non-nil, save the region text. */
1821 /* FIXME: Move this to Elisp (via before-change-functions). */
1822 if (!NILP (BVAR (current_buffer, mark_active))
1823 && XMARKER (BVAR (current_buffer, mark))->buffer
1824 && NILP (Vsaved_region_selection)
1825 && (EQ (Vselect_active_regions, Qonly)
1826 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1827 : (!NILP (Vselect_active_regions)
1828 && !NILP (Vtransient_mark_mode))))
1829 Vsaved_region_selection
1830 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1831
1832 signal_before_change (start, end, preserve_ptr);
1833 Fset (Qdeactivate_mark, Qt);
1834 }
1835
1836 /* Like above, but called when we know that the buffer text
1837 will be modified and region caches should be invalidated. */
1838
1839 void
1840 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1841 ptrdiff_t *preserve_ptr)
1842 {
1843 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1844 invalidate_buffer_caches (current_buffer, start, end);
1845 }
1846
1847 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1848 region between buffer positions START and END. */
1849 void
1850 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1851 {
1852 /* Indirect buffers usually have their caches set to NULL, but we
1853 need to consider the caches of their base buffer. */
1854 if (buf->base_buffer)
1855 buf = buf->base_buffer;
1856 /* The bidi_paragraph_cache must be invalidated first, because doing
1857 so might need to use the newline_cache (via find_newline_no_quit,
1858 see below). */
1859 if (buf->bidi_paragraph_cache)
1860 {
1861 if (start != end
1862 && start > BUF_BEG (buf))
1863 {
1864 /* If we are deleting or replacing characters, we could
1865 create a paragraph start, because all of the characters
1866 from START to the beginning of START's line are
1867 whitespace. Therefore, we must extend the region to be
1868 invalidated up to the newline before START. */
1869 ptrdiff_t line_beg = start;
1870 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
1871
1872 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
1873 {
1874 struct buffer *old = current_buffer;
1875
1876 set_buffer_internal (buf);
1877
1878 line_beg = find_newline_no_quit (start, start_byte, -1,
1879 &start_byte);
1880 set_buffer_internal (old);
1881 }
1882 start = line_beg - (line_beg > BUF_BEG (buf));
1883 }
1884 invalidate_region_cache (buf,
1885 buf->bidi_paragraph_cache,
1886 start - BUF_BEG (buf), BUF_Z (buf) - end);
1887 }
1888 if (buf->newline_cache)
1889 invalidate_region_cache (buf,
1890 buf->newline_cache,
1891 start - BUF_BEG (buf), BUF_Z (buf) - end);
1892 if (buf->width_run_cache)
1893 invalidate_region_cache (buf,
1894 buf->width_run_cache,
1895 start - BUF_BEG (buf), BUF_Z (buf) - end);
1896 }
1897
1898 /* These macros work with an argument named `preserve_ptr'
1899 and a local variable named `preserve_marker'. */
1900
1901 #define PRESERVE_VALUE \
1902 if (preserve_ptr && NILP (preserve_marker)) \
1903 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1904
1905 #define RESTORE_VALUE \
1906 if (! NILP (preserve_marker)) \
1907 { \
1908 *preserve_ptr = marker_position (preserve_marker); \
1909 unchain_marker (XMARKER (preserve_marker)); \
1910 }
1911
1912 #define PRESERVE_START_END \
1913 if (NILP (start_marker)) \
1914 start_marker = Fcopy_marker (start, Qnil); \
1915 if (NILP (end_marker)) \
1916 end_marker = Fcopy_marker (end, Qnil);
1917
1918 #define FETCH_START \
1919 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1920
1921 #define FETCH_END \
1922 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1923
1924 /* Set a variable to nil if an error occurred.
1925 Don't change the variable if there was no error.
1926 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1927 VARIABLE is the variable to maybe set to nil.
1928 NO-ERROR-FLAG is nil if there was an error,
1929 anything else meaning no error (so this function does nothing). */
1930 struct rvoe_arg
1931 {
1932 Lisp_Object *location;
1933 bool errorp;
1934 };
1935
1936 static void
1937 reset_var_on_error (void *ptr)
1938 {
1939 struct rvoe_arg *p = ptr;
1940 if (p->errorp)
1941 *p->location = Qnil;
1942 }
1943
1944 /* Signal a change to the buffer immediately before it happens.
1945 START_INT and END_INT are the bounds of the text to be changed.
1946
1947 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1948 by holding its value temporarily in a marker. */
1949
1950 static void
1951 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1952 ptrdiff_t *preserve_ptr)
1953 {
1954 Lisp_Object start, end;
1955 Lisp_Object start_marker, end_marker;
1956 Lisp_Object preserve_marker;
1957 ptrdiff_t count = SPECPDL_INDEX ();
1958 struct rvoe_arg rvoe_arg;
1959
1960 start = make_number (start_int);
1961 end = make_number (end_int);
1962 preserve_marker = Qnil;
1963 start_marker = Qnil;
1964 end_marker = Qnil;
1965
1966 specbind (Qinhibit_modification_hooks, Qt);
1967
1968 /* If buffer is unmodified, run a special hook for that case. The
1969 check for Vfirst_change_hook is just a minor optimization. */
1970 if (SAVE_MODIFF >= MODIFF
1971 && !NILP (Vfirst_change_hook))
1972 {
1973 PRESERVE_VALUE;
1974 PRESERVE_START_END;
1975 run_hook (Qfirst_change_hook);
1976 }
1977
1978 /* Now run the before-change-functions if any. */
1979 if (!NILP (Vbefore_change_functions))
1980 {
1981 rvoe_arg.location = &Vbefore_change_functions;
1982 rvoe_arg.errorp = 1;
1983
1984 PRESERVE_VALUE;
1985 PRESERVE_START_END;
1986
1987 /* Mark before-change-functions to be reset to nil in case of error. */
1988 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
1989
1990 /* Actually run the hook functions. */
1991 CALLN (Frun_hook_with_args, Qbefore_change_functions,
1992 FETCH_START, FETCH_END);
1993
1994 /* There was no error: unarm the reset_on_error. */
1995 rvoe_arg.errorp = 0;
1996 }
1997
1998 if (buffer_has_overlays ())
1999 {
2000 PRESERVE_VALUE;
2001 report_overlay_modification (FETCH_START, FETCH_END, 0,
2002 FETCH_START, FETCH_END, Qnil);
2003 }
2004
2005 if (! NILP (start_marker))
2006 free_marker (start_marker);
2007 if (! NILP (end_marker))
2008 free_marker (end_marker);
2009 RESTORE_VALUE;
2010
2011 unbind_to (count, Qnil);
2012 }
2013
2014 /* Signal a change immediately after it happens.
2015 CHARPOS is the character position of the start of the changed text.
2016 LENDEL is the number of characters of the text before the change.
2017 (Not the whole buffer; just the part that was changed.)
2018 LENINS is the number of characters in that part of the text
2019 after the change. */
2020
2021 void
2022 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2023 {
2024 ptrdiff_t count = SPECPDL_INDEX ();
2025 struct rvoe_arg rvoe_arg;
2026
2027 if (inhibit_modification_hooks)
2028 return;
2029
2030 /* If we are deferring calls to the after-change functions
2031 and there are no before-change functions,
2032 just record the args that we were going to use. */
2033 if (! NILP (Vcombine_after_change_calls)
2034 && NILP (Vbefore_change_functions)
2035 && !buffer_has_overlays ())
2036 {
2037 Lisp_Object elt;
2038
2039 if (!NILP (combine_after_change_list)
2040 && current_buffer != XBUFFER (combine_after_change_buffer))
2041 Fcombine_after_change_execute ();
2042
2043 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2044 lenins - lendel);
2045 combine_after_change_list
2046 = Fcons (elt, combine_after_change_list);
2047 combine_after_change_buffer = Fcurrent_buffer ();
2048
2049 return;
2050 }
2051
2052 if (!NILP (combine_after_change_list))
2053 Fcombine_after_change_execute ();
2054
2055 specbind (Qinhibit_modification_hooks, Qt);
2056
2057 if (!NILP (Vafter_change_functions))
2058 {
2059 rvoe_arg.location = &Vafter_change_functions;
2060 rvoe_arg.errorp = 1;
2061
2062 /* Mark after-change-functions to be reset to nil in case of error. */
2063 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2064
2065 /* Actually run the hook functions. */
2066 CALLN (Frun_hook_with_args, Qafter_change_functions,
2067 make_number (charpos), make_number (charpos + lenins),
2068 make_number (lendel));
2069
2070 /* There was no error: unarm the reset_on_error. */
2071 rvoe_arg.errorp = 0;
2072 }
2073
2074 if (buffer_has_overlays ())
2075 report_overlay_modification (make_number (charpos),
2076 make_number (charpos + lenins),
2077 1,
2078 make_number (charpos),
2079 make_number (charpos + lenins),
2080 make_number (lendel));
2081
2082 /* After an insertion, call the text properties
2083 insert-behind-hooks or insert-in-front-hooks. */
2084 if (lendel == 0)
2085 report_interval_modification (make_number (charpos),
2086 make_number (charpos + lenins));
2087
2088 unbind_to (count, Qnil);
2089 }
2090
2091 static void
2092 Fcombine_after_change_execute_1 (Lisp_Object val)
2093 {
2094 Vcombine_after_change_calls = val;
2095 }
2096
2097 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2098 Scombine_after_change_execute, 0, 0, 0,
2099 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2100 (void)
2101 {
2102 ptrdiff_t count = SPECPDL_INDEX ();
2103 ptrdiff_t beg, end, change;
2104 ptrdiff_t begpos, endpos;
2105 Lisp_Object tail;
2106
2107 if (NILP (combine_after_change_list))
2108 return Qnil;
2109
2110 /* It is rare for combine_after_change_buffer to be invalid, but
2111 possible. It can happen when combine-after-change-calls is
2112 non-nil, and insertion calls a file handler (e.g. through
2113 lock_file) which scribbles into a temp file -- cyd */
2114 if (!BUFFERP (combine_after_change_buffer)
2115 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2116 {
2117 combine_after_change_list = Qnil;
2118 return Qnil;
2119 }
2120
2121 record_unwind_current_buffer ();
2122
2123 Fset_buffer (combine_after_change_buffer);
2124
2125 /* # chars unchanged at beginning of buffer. */
2126 beg = Z - BEG;
2127 /* # chars unchanged at end of buffer. */
2128 end = beg;
2129 /* Total amount of insertion (negative for deletion). */
2130 change = 0;
2131
2132 /* Scan the various individual changes,
2133 accumulating the range info in BEG, END and CHANGE. */
2134 for (tail = combine_after_change_list; CONSP (tail);
2135 tail = XCDR (tail))
2136 {
2137 Lisp_Object elt;
2138 ptrdiff_t thisbeg, thisend, thischange;
2139
2140 /* Extract the info from the next element. */
2141 elt = XCAR (tail);
2142 if (! CONSP (elt))
2143 continue;
2144 thisbeg = XINT (XCAR (elt));
2145
2146 elt = XCDR (elt);
2147 if (! CONSP (elt))
2148 continue;
2149 thisend = XINT (XCAR (elt));
2150
2151 elt = XCDR (elt);
2152 if (! CONSP (elt))
2153 continue;
2154 thischange = XINT (XCAR (elt));
2155
2156 /* Merge this range into the accumulated range. */
2157 change += thischange;
2158 if (thisbeg < beg)
2159 beg = thisbeg;
2160 if (thisend < end)
2161 end = thisend;
2162 }
2163
2164 /* Get the current start and end positions of the range
2165 that was changed. */
2166 begpos = BEG + beg;
2167 endpos = Z - end;
2168
2169 /* We are about to handle these, so discard them. */
2170 combine_after_change_list = Qnil;
2171
2172 /* Now run the after-change functions for real.
2173 Turn off the flag that defers them. */
2174 record_unwind_protect (Fcombine_after_change_execute_1,
2175 Vcombine_after_change_calls);
2176 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2177 update_compositions (begpos, endpos, CHECK_ALL);
2178
2179 return unbind_to (count, Qnil);
2180 }
2181 \f
2182 void
2183 syms_of_insdel (void)
2184 {
2185 staticpro (&combine_after_change_list);
2186 staticpro (&combine_after_change_buffer);
2187 combine_after_change_list = Qnil;
2188 combine_after_change_buffer = Qnil;
2189
2190 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2191 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2192 Vcombine_after_change_calls = Qnil;
2193
2194 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2195 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2196 This affects `before-change-functions' and `after-change-functions',
2197 as well as hooks attached to text properties and overlays. */);
2198 inhibit_modification_hooks = 0;
2199 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2200
2201 DEFSYM (Qregion_extract_function, "region-extract-function");
2202
2203 defsubr (&Scombine_after_change_execute);
2204 }