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