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 (!NILP (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) consisting
318 of LENGTH characters at position POS.
319 It does not work to use `insert' for this, becase a GC could happen
320 before we bcopy the stuff into the buffer, and relocate the string
321 without insert noticing. */
322 insert_from_string (string
, pos
, length
)
324 register int pos
, length
;
326 register Lisp_Object temp
;
332 /* Make sure point-max won't overflow after this insertion. */
333 XSET (temp
, Lisp_Int
, length
+ Z
);
334 if (length
+ Z
!= XINT (temp
))
335 error ("maximum buffer size exceeded");
338 prepare_to_modify_buffer (point
, point
);
342 if (GAP_SIZE
< length
)
343 make_gap (length
- GAP_SIZE
);
345 record_insert (point
, length
);
349 bcopy (XSTRING (string
)->data
, GPT_ADDR
, length
);
355 SET_PT (point
+ length
);
357 signal_after_change (point
-length
, 0, length
);
360 /* Insert the character C before point */
369 /* Insert the null-terminated string S before point */
375 insert (s
, strlen (s
));
378 /* Like `insert' except that all markers pointing at the place where
379 the insertion happens are adjusted to point after it.
380 Don't use this function to insert part of a Lisp string,
381 since gc could happen and relocate it. */
383 insert_before_markers (string
, length
)
384 unsigned char *string
;
387 register int opoint
= point
;
388 insert (string
, length
);
389 adjust_markers (opoint
- 1, opoint
, length
);
392 /* Insert part of a Lisp string, relocating markers after. */
394 insert_from_string_before_markers (string
, pos
, length
)
396 register int pos
, length
;
398 register int opoint
= point
;
399 insert_from_string (string
, pos
, length
);
400 adjust_markers (opoint
- 1, opoint
, length
);
403 /* Delete characters in current buffer
404 from FROM up to (but not including) TO. */
407 register int from
, to
;
411 /* Make args be valid */
417 if ((numdel
= to
- from
) <= 0)
420 /* Make sure the gap is somewhere in or next to what we are deleting. */
426 prepare_to_modify_buffer (from
, to
);
428 /* Relocate point as if it were a marker. */
434 SET_PT (point
- numdel
);
437 record_delete (from
, numdel
);
440 /* Relocate all markers pointing into the new, larger gap
441 to point at the end of the text before the gap. */
442 adjust_markers (to
+ GAP_SIZE
, to
+ GAP_SIZE
, - numdel
- GAP_SIZE
);
449 if (GPT
- BEG
< beg_unchanged
)
450 beg_unchanged
= GPT
- BEG
;
451 if (Z
- GPT
< end_unchanged
)
452 end_unchanged
= Z
- GPT
;
454 signal_after_change (from
, numdel
, 0);
457 modify_region (start
, end
)
460 prepare_to_modify_buffer (start
, end
);
462 if (start
- 1 < beg_unchanged
|| unchanged_modified
== MODIFF
)
463 beg_unchanged
= start
- 1;
464 if (Z
- end
< end_unchanged
465 || unchanged_modified
== MODIFF
)
466 end_unchanged
= Z
- end
;
470 /* Check that it is okay to modify the buffer between START and END.
471 Run the before-change-function, if any. */
473 prepare_to_modify_buffer (start
, end
)
474 Lisp_Object start
, end
;
476 if (!NILP (current_buffer
->read_only
))
477 Fbarf_if_buffer_read_only ();
479 if (check_protected_fields
)
480 Fregion_fields (start
, end
, Qnil
, Qt
);
482 #ifdef CLASH_DETECTION
483 if (!NILP (current_buffer
->filename
)
484 && current_buffer
->save_modified
>= MODIFF
)
485 lock_file (current_buffer
->filename
);
487 /* At least warn if this file has changed on disk since it was visited. */
488 if (!NILP (current_buffer
->filename
)
489 && current_buffer
->save_modified
>= MODIFF
490 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
491 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
492 call1 (intern ("ask-user-about-supersession-threat"),
493 current_buffer
->filename
);
494 #endif /* not CLASH_DETECTION */
496 signal_before_change (start
, end
);
500 before_change_function_restore (value
)
503 Vbefore_change_function
= value
;
507 after_change_function_restore (value
)
510 Vafter_change_function
= value
;
513 /* Signal a change to the buffer immediatly before it happens.
514 START and END are the bounds of the text to be changed,
517 signal_before_change (start
, end
)
518 Lisp_Object start
, end
;
520 /* If buffer is unmodified, run a special hook for that case. */
521 if (current_buffer
->save_modified
>= MODIFF
522 && !NILP (Vfirst_change_function
))
524 call0 (Vfirst_change_function
);
526 /* Now in any case run the before-change-function if any. */
527 if (!NILP (Vbefore_change_function
))
529 int count
= specpdl_ptr
- specpdl
;
530 Lisp_Object function
;
532 function
= Vbefore_change_function
;
533 record_unwind_protect (after_change_function_restore
,
534 Vafter_change_function
);
535 record_unwind_protect (before_change_function_restore
,
536 Vbefore_change_function
);
537 Vafter_change_function
= Qnil
;
538 Vbefore_change_function
= Qnil
;
540 call2 (function
, start
, end
);
541 unbind_to (count
, Qnil
);
545 /* Signal a change immediatly after it happens.
546 POS is the address of the start of the changed text.
547 LENDEL is the number of characters of the text before the change.
548 (Not the whole buffer; just the part that was changed.)
549 LENINS is the number of characters in the changed text. */
551 signal_after_change (pos
, lendel
, lenins
)
552 int pos
, lendel
, lenins
;
554 if (!NILP (Vafter_change_function
))
556 int count
= specpdl_ptr
- specpdl
;
557 Lisp_Object function
;
558 function
= Vafter_change_function
;
560 record_unwind_protect (after_change_function_restore
,
561 Vafter_change_function
);
562 record_unwind_protect (before_change_function_restore
,
563 Vbefore_change_function
);
564 Vafter_change_function
= Qnil
;
565 Vbefore_change_function
= Qnil
;
567 call3 (function
, make_number (pos
), make_number (pos
+ lenins
),
568 make_number (lendel
));
569 unbind_to (count
, Qnil
);