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