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