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