1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 1986 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* Nonzero means don't allow protected fields to be modified. */
28 extern int check_protected_fields
;
30 /* Move gap to position `pos'.
31 Note that this can quit! */
42 /* Move the gap to POS, which is less than the current GPT.
43 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
45 gap_left (pos
, newgap
)
49 register unsigned char *to
, *from
;
57 if (unchanged_modified
== MODIFF
)
60 end_unchanged
= Z
- pos
- 1;
64 if (Z
- GPT
< end_unchanged
)
65 end_unchanged
= Z
- GPT
;
66 if (pos
< beg_unchanged
)
76 /* Now copy the characters. To move the gap down,
77 copy characters up. */
81 /* I gets number of characters left to copy. */
85 /* If a quit is requested, stop copying now.
86 Change POS to be where we have actually moved the gap to. */
92 /* Move at most 32000 chars before checking again for a quit. */
97 /* bcopy is safe if the two areas of memory do not overlap
98 or on systems where bcopy is always safe for moving upward. */
100 || to
- from
>= 128))
102 /* If overlap is not safe, avoid it by not moving too many
103 characters at once. */
104 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
119 /* Adjust markers, and buffer data structure, to put the gap at POS.
120 POS is where the loop above stopped, which may be what was specified
121 or may be where a quit was detected. */
122 adjust_markers (pos
+ 1, GPT
, GAP_SIZE
);
130 register unsigned char *to
, *from
;
136 if (unchanged_modified
== MODIFF
)
139 end_unchanged
= Z
- pos
- 1;
143 if (Z
- pos
- 1 < end_unchanged
)
144 end_unchanged
= Z
- pos
- 1;
145 if (GPT
- BEG
< beg_unchanged
)
146 beg_unchanged
= GPT
- BEG
;
154 /* Now copy the characters. To move the gap up,
155 copy characters down. */
159 /* I gets number of characters left to copy. */
163 /* If a quit is requested, stop copying now.
164 Change POS to be where we have actually moved the gap to. */
170 /* Move at most 32000 chars before checking again for a quit. */
175 /* bcopy is safe if the two areas of memory do not overlap
176 or on systems where bcopy is always safe for moving downward. */
177 && (BCOPY_DOWNWARD_SAFE
178 || from
- to
>= 128))
180 /* If overlap is not safe, avoid it by not moving too many
181 characters at once. */
182 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
197 adjust_markers (GPT
+ GAP_SIZE
, pos
+ 1 + GAP_SIZE
, - GAP_SIZE
);
202 /* Add `amount' to the position of every marker in the current buffer
203 whose current position is between `from' (exclusive) and `to' (inclusive).
204 Also, any markers past the outside of that interval, in the direction
205 of adjustment, are first moved back to the near end of the interval
206 and then adjusted by `amount'. */
208 adjust_markers (from
, to
, amount
)
209 register int from
, to
, amount
;
212 register struct Lisp_Marker
*m
;
215 marker
= current_buffer
->markers
;
217 while (!NULL (marker
))
219 m
= XMARKER (marker
);
223 if (mpos
> to
&& mpos
< to
+ amount
)
228 if (mpos
> from
+ amount
&& mpos
<= from
)
229 mpos
= from
+ amount
;
231 if (mpos
> from
&& mpos
<= to
)
238 /* Make the gap INCREMENT characters longer. */
243 unsigned char *result
;
248 /* If we have to get more space, get enough to last a while. */
251 result
= BUFFER_REALLOC (BEG_ADDR
, (Z
- BEG
+ GAP_SIZE
+ increment
));
256 /* Prevent quitting in move_gap. */
261 old_gap_size
= GAP_SIZE
;
263 /* Call the newly allocated space a gap at the end of the whole space. */
265 GAP_SIZE
= increment
;
267 /* Move the new gap down to be consecutive with the end of the old one.
268 This adjusts the markers properly too. */
269 gap_left (real_gap_loc
+ old_gap_size
, 1);
271 /* Now combine the two into one large gap. */
272 GAP_SIZE
+= old_gap_size
;
278 /* Insert a string of specified length before point.
279 DO NOT use this for the contents of a Lisp string!
280 prepare_to_modify_buffer could relocate the string. */
282 insert (string
, length
)
283 register unsigned char *string
;
286 register Lisp_Object temp
;
291 /* Make sure point-max won't overflow after this insertion. */
292 XSET (temp
, Lisp_Int
, length
+ Z
);
293 if (length
+ Z
!= XINT (temp
))
294 error ("maximum buffer size exceeded");
296 prepare_to_modify_buffer (point
, point
);
300 if (GAP_SIZE
< length
)
301 make_gap (length
- GAP_SIZE
);
303 record_insert (point
, length
);
306 bcopy (string
, GPT_ADDR
, length
);
312 SET_PT (point
+ length
);
314 signal_after_change (point
-length
, 0, length
);
317 /* Function to insert part of the text of a string (STRING)
318 consisting of LENGTH characters at position POS.
319 It does not work to use `insert' for this. */
321 insert_from_string (string
, pos
, length
)
323 register int pos
, length
;
325 register Lisp_Object temp
;
331 /* Make sure point-max won't overflow after this insertion. */
332 XSET (temp
, Lisp_Int
, length
+ Z
);
333 if (length
+ Z
!= XINT (temp
))
334 error ("maximum buffer size exceeded");
337 prepare_to_modify_buffer (point
, point
);
341 if (GAP_SIZE
< length
)
342 make_gap (length
- GAP_SIZE
);
344 record_insert (point
, length
);
348 bcopy (XSTRING (string
)->data
, GPT_ADDR
, length
);
356 signal_after_change (point
-length
, 0, length
);
359 /* Insert the character C before point */
368 /* Insert the null-terminated string S before point */
374 insert (s
, strlen (s
));
377 /* Like `insert' except that all markers pointing at the place where
378 the insertion happens are adjusted to point after it.
379 Don't use this function to insert part of a Lisp string,
380 since gc could happen and relocate it. */
382 insert_before_markers (string
, length
)
383 unsigned char *string
;
386 register int opoint
= point
;
387 insert (string
, length
);
388 adjust_markers (opoint
- 1, opoint
, length
);
391 /* Insert part of a Lisp string, relocating markers after. */
393 insert_from_string_before_markers (string
, pos
, length
)
395 register int pos
, length
;
397 register int opoint
= point
;
398 insert_from_string (string
, pos
, length
);
399 adjust_markers (opoint
- 1, opoint
, length
);
402 /* Delete characters in current buffer
403 from FROM up to (but not including) TO. */
406 register int from
, to
;
410 /* Make args be valid */
416 if ((numdel
= to
- from
) <= 0)
419 /* Make sure the gap is somewhere in or next to what we are deleting. */
425 prepare_to_modify_buffer (from
, to
);
427 /* Relocate point as if it were a marker. */
433 SET_PT (point
- numdel
);
436 record_delete (from
, numdel
);
439 /* Relocate all markers pointing into the new, larger gap
440 to point at the end of the text before the gap. */
441 adjust_markers (to
+ GAP_SIZE
, to
+ GAP_SIZE
, - numdel
- GAP_SIZE
);
448 if (GPT
- BEG
< beg_unchanged
)
449 beg_unchanged
= GPT
- BEG
;
450 if (Z
- GPT
< end_unchanged
)
451 end_unchanged
= Z
- GPT
;
453 signal_after_change (from
, numdel
, 0);
456 modify_region (start
, end
)
459 prepare_to_modify_buffer (start
, end
);
461 if (start
- 1 < beg_unchanged
|| unchanged_modified
== MODIFF
)
462 beg_unchanged
= start
- 1;
463 if (Z
- end
< end_unchanged
464 || unchanged_modified
== MODIFF
)
465 end_unchanged
= Z
- end
;
469 /* Check that it is okay to modify the buffer between START and END.
470 Run the before-change-function, if any. */
472 prepare_to_modify_buffer (start
, end
)
473 Lisp_Object start
, end
;
475 if (!NULL (current_buffer
->read_only
))
476 Fbarf_if_buffer_read_only ();
478 if (check_protected_fields
)
479 Fregion_fields (start
, end
, Qnil
, Qt
);
481 #ifdef CLASH_DETECTION
482 if (!NULL (current_buffer
->filename
)
483 && current_buffer
->save_modified
>= MODIFF
)
484 lock_file (current_buffer
->filename
);
486 /* At least warn if this file has changed on disk since it was visited. */
487 if (!NULL (current_buffer
->filename
)
488 && current_buffer
->save_modified
>= MODIFF
489 && NULL (Fverify_visited_file_modtime (Fcurrent_buffer ()))
490 && !NULL (Ffile_exists_p (current_buffer
->filename
)))
491 call1 (intern ("ask-user-about-supersession-threat"),
492 current_buffer
->filename
);
493 #endif /* not CLASH_DETECTION */
495 signal_before_change (start
, end
);
499 before_change_function_restore (value
)
502 Vbefore_change_function
= value
;
506 after_change_function_restore (value
)
509 Vafter_change_function
= value
;
512 /* Signal a change to the buffer immediatly before it happens.
513 START and END are the bounds of the text to be changed,
516 signal_before_change (start
, end
)
517 Lisp_Object start
, end
;
519 /* If buffer is unmodified, run a special hook for that case. */
520 if (current_buffer
->save_modified
>= MODIFF
521 && !NULL (Vfirst_change_function
))
523 call0 (Vfirst_change_function
);
525 /* Now in any case run the before-change-function if any. */
526 if (!NULL (Vbefore_change_function
))
528 int count
= specpdl_ptr
- specpdl
;
529 Lisp_Object function
;
531 function
= Vbefore_change_function
;
532 record_unwind_protect (after_change_function_restore
,
533 Vafter_change_function
);
534 record_unwind_protect (before_change_function_restore
,
535 Vbefore_change_function
);
536 Vafter_change_function
= Qnil
;
537 Vbefore_change_function
= Qnil
;
539 call2 (function
, start
, end
);
540 unbind_to (count
, Qnil
);
544 /* Signal a change immediatly after it happens.
545 POS is the address of the start of the changed text.
546 LENDEL is the number of characters of the text before the change.
547 (Not the whole buffer; just the part that was changed.)
548 LENINS is the number of characters in the changed text. */
550 signal_after_change (pos
, lendel
, lenins
)
551 int pos
, lendel
, lenins
;
553 if (!NULL (Vafter_change_function
))
555 int count
= specpdl_ptr
- specpdl
;
556 Lisp_Object function
;
557 function
= Vafter_change_function
;
559 record_unwind_protect (after_change_function_restore
,
560 Vafter_change_function
);
561 record_unwind_protect (before_change_function_restore
,
562 Vbefore_change_function
);
563 Vafter_change_function
= Qnil
;
564 Vbefore_change_function
= Qnil
;
566 call3 (function
, make_number (pos
), make_number (pos
+ lenins
),
567 make_number (lendel
));
568 unbind_to (count
, Qnil
);