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