1 /* Markers: examining, setting and deleting.
2 Copyright (C) 1985, 1997-1998, 2001-2012 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 3 of the License, or
9 (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
23 #include "character.h"
26 /* Record one cached position found recently by
27 buf_charpos_to_bytepos or buf_bytepos_to_charpos. */
29 static ptrdiff_t cached_charpos
;
30 static ptrdiff_t cached_bytepos
;
31 static struct buffer
*cached_buffer
;
32 static int cached_modiff
;
34 #ifdef ENABLE_CHECKING
36 extern int count_markers (struct buffer
*) EXTERNALLY_VISIBLE
;
39 byte_char_debug_check (struct buffer
*b
, ptrdiff_t charpos
, ptrdiff_t bytepos
)
43 if (bytepos
> BUF_GPT_BYTE (b
))
45 nchars
= multibyte_chars_in_text (BUF_BEG_ADDR (b
),
46 BUF_GPT_BYTE (b
) - BUF_BEG_BYTE (b
));
47 nchars
+= multibyte_chars_in_text (BUF_GAP_END_ADDR (b
),
48 bytepos
- BUF_GPT_BYTE (b
));
51 nchars
= multibyte_chars_in_text (BUF_BEG_ADDR (b
),
52 bytepos
- BUF_BEG_BYTE (b
));
54 if (charpos
- 1 != nchars
)
58 #else /* not ENABLE_CHECKING */
60 #define byte_char_debug_check(b,charpos,bytepos) do { } while (0)
62 #endif /* ENABLE_CHECKING */
65 clear_charpos_cache (struct buffer
*b
)
67 if (cached_buffer
== b
)
71 /* Converting between character positions and byte positions. */
73 /* There are several places in the buffer where we know
74 the correspondence: BEG, BEGV, PT, GPT, ZV and Z,
75 and everywhere there is a marker. So we find the one of these places
76 that is closest to the specified position, and scan from there. */
78 /* charpos_to_bytepos returns the byte position corresponding to CHARPOS. */
80 /* This macro is a subroutine of charpos_to_bytepos.
81 Note that it is desirable that BYTEPOS is not evaluated
82 except when we really want its value. */
84 #define CONSIDER(CHARPOS, BYTEPOS) \
86 ptrdiff_t this_charpos = (CHARPOS); \
89 if (this_charpos == charpos) \
91 ptrdiff_t value = (BYTEPOS); \
93 byte_char_debug_check (b, charpos, value); \
96 else if (this_charpos > charpos) \
98 if (this_charpos < best_above) \
100 best_above = this_charpos; \
101 best_above_byte = (BYTEPOS); \
105 else if (this_charpos > best_below) \
107 best_below = this_charpos; \
108 best_below_byte = (BYTEPOS); \
114 if (best_above - best_below == best_above_byte - best_below_byte) \
116 ptrdiff_t value = best_below_byte + (charpos - best_below); \
118 byte_char_debug_check (b, charpos, value); \
125 charpos_to_bytepos (ptrdiff_t charpos
)
127 return buf_charpos_to_bytepos (current_buffer
, charpos
);
131 buf_charpos_to_bytepos (struct buffer
*b
, ptrdiff_t charpos
)
133 struct Lisp_Marker
*tail
;
134 ptrdiff_t best_above
, best_above_byte
;
135 ptrdiff_t best_below
, best_below_byte
;
137 if (charpos
< BUF_BEG (b
) || charpos
> BUF_Z (b
))
140 best_above
= BUF_Z (b
);
141 best_above_byte
= BUF_Z_BYTE (b
);
143 /* If this buffer has as many characters as bytes,
144 each character must be one byte.
145 This takes care of the case where enable-multibyte-characters is nil. */
146 if (best_above
== best_above_byte
)
150 best_below_byte
= BEG_BYTE
;
152 /* We find in best_above and best_above_byte
153 the closest known point above CHARPOS,
154 and in best_below and best_below_byte
155 the closest known point below CHARPOS,
157 If at any point we can tell that the space between those
158 two best approximations is all single-byte,
159 we interpolate the result immediately. */
161 CONSIDER (BUF_PT (b
), BUF_PT_BYTE (b
));
162 CONSIDER (BUF_GPT (b
), BUF_GPT_BYTE (b
));
163 CONSIDER (BUF_BEGV (b
), BUF_BEGV_BYTE (b
));
164 CONSIDER (BUF_ZV (b
), BUF_ZV_BYTE (b
));
166 if (b
== cached_buffer
&& BUF_MODIFF (b
) == cached_modiff
)
167 CONSIDER (cached_charpos
, cached_bytepos
);
169 for (tail
= BUF_MARKERS (b
); tail
; tail
= tail
->next
)
171 CONSIDER (tail
->charpos
, tail
->bytepos
);
173 /* If we are down to a range of 50 chars,
174 don't bother checking any other markers;
175 scan the intervening chars directly now. */
176 if (best_above
- best_below
< 50)
180 /* We get here if we did not exactly hit one of the known places.
181 We have one known above and one known below.
182 Scan, counting characters, from whichever one is closer. */
184 if (charpos
- best_below
< best_above
- charpos
)
186 int record
= charpos
- best_below
> 5000;
188 while (best_below
!= charpos
)
191 BUF_INC_POS (b
, best_below_byte
);
194 /* If this position is quite far from the nearest known position,
195 cache the correspondence by creating a marker here.
196 It will last until the next GC. */
198 build_marker (b
, best_below
, best_below_byte
);
200 byte_char_debug_check (b
, charpos
, best_below_byte
);
203 cached_modiff
= BUF_MODIFF (b
);
204 cached_charpos
= best_below
;
205 cached_bytepos
= best_below_byte
;
207 return best_below_byte
;
211 int record
= best_above
- charpos
> 5000;
213 while (best_above
!= charpos
)
216 BUF_DEC_POS (b
, best_above_byte
);
219 /* If this position is quite far from the nearest known position,
220 cache the correspondence by creating a marker here.
221 It will last until the next GC. */
223 build_marker (b
, best_above
, best_above_byte
);
225 byte_char_debug_check (b
, charpos
, best_above_byte
);
228 cached_modiff
= BUF_MODIFF (b
);
229 cached_charpos
= best_above
;
230 cached_bytepos
= best_above_byte
;
232 return best_above_byte
;
238 /* Used for debugging: recompute the bytepos corresponding to CHARPOS
239 in the simplest, most reliable way. */
241 extern ptrdiff_t verify_bytepos (ptrdiff_t charpos
) EXTERNALLY_VISIBLE
;
243 verify_bytepos (ptrdiff_t charpos
)
246 ptrdiff_t below_byte
= 1;
248 while (below
!= charpos
)
251 BUF_INC_POS (current_buffer
, below_byte
);
257 /* buf_bytepos_to_charpos returns the char position corresponding to
260 /* This macro is a subroutine of buf_bytepos_to_charpos.
261 It is used when BYTEPOS is actually the byte position. */
263 #define CONSIDER(BYTEPOS, CHARPOS) \
265 ptrdiff_t this_bytepos = (BYTEPOS); \
268 if (this_bytepos == bytepos) \
270 ptrdiff_t value = (CHARPOS); \
272 byte_char_debug_check (b, value, bytepos); \
275 else if (this_bytepos > bytepos) \
277 if (this_bytepos < best_above_byte) \
279 best_above = (CHARPOS); \
280 best_above_byte = this_bytepos; \
284 else if (this_bytepos > best_below_byte) \
286 best_below = (CHARPOS); \
287 best_below_byte = this_bytepos; \
293 if (best_above - best_below == best_above_byte - best_below_byte) \
295 ptrdiff_t value = best_below + (bytepos - best_below_byte); \
297 byte_char_debug_check (b, value, bytepos); \
304 buf_bytepos_to_charpos (struct buffer
*b
, ptrdiff_t bytepos
)
306 struct Lisp_Marker
*tail
;
307 ptrdiff_t best_above
, best_above_byte
;
308 ptrdiff_t best_below
, best_below_byte
;
310 if (bytepos
< BUF_BEG_BYTE (b
) || bytepos
> BUF_Z_BYTE (b
))
313 best_above
= BUF_Z (b
);
314 best_above_byte
= BUF_Z_BYTE (b
);
316 /* If this buffer has as many characters as bytes,
317 each character must be one byte.
318 This takes care of the case where enable-multibyte-characters is nil. */
319 if (best_above
== best_above_byte
)
323 best_below_byte
= BEG_BYTE
;
325 CONSIDER (BUF_PT_BYTE (b
), BUF_PT (b
));
326 CONSIDER (BUF_GPT_BYTE (b
), BUF_GPT (b
));
327 CONSIDER (BUF_BEGV_BYTE (b
), BUF_BEGV (b
));
328 CONSIDER (BUF_ZV_BYTE (b
), BUF_ZV (b
));
330 if (b
== cached_buffer
&& BUF_MODIFF (b
) == cached_modiff
)
331 CONSIDER (cached_bytepos
, cached_charpos
);
333 for (tail
= BUF_MARKERS (b
); tail
; tail
= tail
->next
)
335 CONSIDER (tail
->bytepos
, tail
->charpos
);
337 /* If we are down to a range of 50 chars,
338 don't bother checking any other markers;
339 scan the intervening chars directly now. */
340 if (best_above
- best_below
< 50)
344 /* We get here if we did not exactly hit one of the known places.
345 We have one known above and one known below.
346 Scan, counting characters, from whichever one is closer. */
348 if (bytepos
- best_below_byte
< best_above_byte
- bytepos
)
350 int record
= bytepos
- best_below_byte
> 5000;
352 while (best_below_byte
< bytepos
)
355 BUF_INC_POS (b
, best_below_byte
);
358 /* If this position is quite far from the nearest known position,
359 cache the correspondence by creating a marker here.
360 It will last until the next GC.
361 But don't do it if BUF_MARKERS is nil;
362 that is a signal from Fset_buffer_multibyte. */
363 if (record
&& BUF_MARKERS (b
))
364 build_marker (b
, best_below
, best_below_byte
);
366 byte_char_debug_check (b
, best_below
, bytepos
);
369 cached_modiff
= BUF_MODIFF (b
);
370 cached_charpos
= best_below
;
371 cached_bytepos
= best_below_byte
;
377 int record
= best_above_byte
- bytepos
> 5000;
379 while (best_above_byte
> bytepos
)
382 BUF_DEC_POS (b
, best_above_byte
);
385 /* If this position is quite far from the nearest known position,
386 cache the correspondence by creating a marker here.
387 It will last until the next GC.
388 But don't do it if BUF_MARKERS is nil;
389 that is a signal from Fset_buffer_multibyte. */
390 if (record
&& BUF_MARKERS (b
))
391 build_marker (b
, best_above
, best_above_byte
);
393 byte_char_debug_check (b
, best_above
, bytepos
);
396 cached_modiff
= BUF_MODIFF (b
);
397 cached_charpos
= best_above
;
398 cached_bytepos
= best_above_byte
;
406 /* Operations on markers. */
408 DEFUN ("marker-buffer", Fmarker_buffer
, Smarker_buffer
, 1, 1, 0,
409 doc
: /* Return the buffer that MARKER points into, or nil if none.
410 Returns nil if MARKER points into a dead buffer. */)
411 (register Lisp_Object marker
)
413 register Lisp_Object buf
;
414 CHECK_MARKER (marker
);
415 if (XMARKER (marker
)->buffer
)
417 XSETBUFFER (buf
, XMARKER (marker
)->buffer
);
418 /* If the buffer is dead, we're in trouble: the buffer pointer here
419 does not preserve the buffer from being GC'd (it's weak), so
420 markers have to be unlinked from their buffer as soon as the buffer
422 eassert (!NILP (BVAR (XBUFFER (buf
), name
)));
428 DEFUN ("marker-position", Fmarker_position
, Smarker_position
, 1, 1, 0,
429 doc
: /* Return the position MARKER points at, as a character number.
430 Returns nil if MARKER points nowhere. */)
433 CHECK_MARKER (marker
);
434 if (XMARKER (marker
)->buffer
)
435 return make_number (XMARKER (marker
)->charpos
);
440 /* Change M so it points to B at CHARPOS and BYTEPOS. */
443 attach_marker (struct Lisp_Marker
*m
, struct buffer
*b
,
444 ptrdiff_t charpos
, ptrdiff_t bytepos
)
446 /* Every character is at least one byte. */
447 eassert (charpos
<= bytepos
);
449 m
->charpos
= charpos
;
450 m
->bytepos
= bytepos
;
456 m
->next
= BUF_MARKERS (b
);
461 DEFUN ("set-marker", Fset_marker
, Sset_marker
, 2, 3, 0,
462 doc
: /* Position MARKER before character number POSITION in BUFFER.
463 BUFFER defaults to the current buffer.
464 If POSITION is nil, makes marker point nowhere.
465 Then it no longer slows down editing in any buffer.
467 (Lisp_Object marker
, Lisp_Object position
, Lisp_Object buffer
)
469 register ptrdiff_t charpos
;
470 register ptrdiff_t bytepos
;
471 register struct buffer
*b
;
472 register struct Lisp_Marker
*m
;
474 CHECK_MARKER (marker
);
475 m
= XMARKER (marker
);
477 /* If position is nil or a marker that points nowhere,
478 make this marker point nowhere. */
480 || (MARKERP (position
) && !XMARKER (position
)->buffer
))
490 CHECK_BUFFER (buffer
);
491 b
= XBUFFER (buffer
);
492 /* If buffer is dead, set marker to point nowhere. */
493 if (EQ (BVAR (b
, name
), Qnil
))
500 /* Optimize the special case where we are copying the position
501 of an existing marker, and MARKER is already in the same buffer. */
502 if (MARKERP (position
) && b
== XMARKER (position
)->buffer
505 m
->bytepos
= XMARKER (position
)->bytepos
;
506 m
->charpos
= XMARKER (position
)->charpos
;
510 CHECK_NUMBER_COERCE_MARKER (position
);
511 charpos
= clip_to_bounds (BUF_BEG (b
), XINT (position
), BUF_Z (b
));
512 bytepos
= buf_charpos_to_bytepos (b
, charpos
);
514 attach_marker (m
, b
, charpos
, bytepos
);
518 /* This version of Fset_marker won't let the position
519 be outside the visible part. */
522 set_marker_restricted (Lisp_Object marker
, Lisp_Object pos
, Lisp_Object buffer
)
524 register ptrdiff_t charpos
;
525 register ptrdiff_t bytepos
;
526 register struct buffer
*b
;
527 register struct Lisp_Marker
*m
;
529 CHECK_MARKER (marker
);
530 m
= XMARKER (marker
);
532 /* If position is nil or a marker that points nowhere,
533 make this marker point nowhere. */
535 || (MARKERP (pos
) && !XMARKER (pos
)->buffer
))
545 CHECK_BUFFER (buffer
);
546 b
= XBUFFER (buffer
);
547 /* If buffer is dead, set marker to point nowhere. */
548 if (EQ (BVAR (b
, name
), Qnil
))
555 /* Optimize the special case where we are copying the position
556 of an existing marker, and MARKER is already in the same buffer. */
557 if (MARKERP (pos
) && b
== XMARKER (pos
)->buffer
560 m
->bytepos
= XMARKER (pos
)->bytepos
;
561 m
->charpos
= XMARKER (pos
)->charpos
;
565 CHECK_NUMBER_COERCE_MARKER (pos
);
566 charpos
= clip_to_bounds (BUF_BEGV (b
), XINT (pos
), BUF_ZV (b
));
567 bytepos
= buf_charpos_to_bytepos (b
, charpos
);
569 attach_marker (m
, b
, charpos
, bytepos
);
573 /* Set the position of MARKER, specifying both the
574 character position and the corresponding byte position. */
577 set_marker_both (Lisp_Object marker
, Lisp_Object buffer
, ptrdiff_t charpos
, ptrdiff_t bytepos
)
579 register struct buffer
*b
;
580 register struct Lisp_Marker
*m
;
582 CHECK_MARKER (marker
);
583 m
= XMARKER (marker
);
589 CHECK_BUFFER (buffer
);
590 b
= XBUFFER (buffer
);
591 /* If buffer is dead, set marker to point nowhere. */
592 if (EQ (BVAR (b
, name
), Qnil
))
599 /* In a single-byte buffer, the two positions must be equal. */
600 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
601 && charpos
!= bytepos
)
604 attach_marker (m
, b
, charpos
, bytepos
);
608 /* This version of set_marker_both won't let the position
609 be outside the visible part. */
612 set_marker_restricted_both (Lisp_Object marker
, Lisp_Object buffer
, ptrdiff_t charpos
, ptrdiff_t bytepos
)
614 register struct buffer
*b
;
615 register struct Lisp_Marker
*m
;
617 CHECK_MARKER (marker
);
618 m
= XMARKER (marker
);
624 CHECK_BUFFER (buffer
);
625 b
= XBUFFER (buffer
);
626 /* If buffer is dead, set marker to point nowhere. */
627 if (EQ (BVAR (b
, name
), Qnil
))
634 charpos
= clip_to_bounds (BUF_BEGV (b
), charpos
, BUF_ZV (b
));
635 bytepos
= clip_to_bounds (BUF_BEGV_BYTE (b
), bytepos
, BUF_ZV_BYTE (b
));
637 /* In a single-byte buffer, the two positions must be equal. */
638 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
639 && charpos
!= bytepos
)
642 attach_marker (m
, b
, charpos
, bytepos
);
646 /* Remove MARKER from the chain of whatever buffer it is in,
647 leaving it points to nowhere. This is called during garbage
648 collection, so we must be careful to ignore and preserve
649 mark bits, including those in chain fields of markers. */
652 unchain_marker (register struct Lisp_Marker
*marker
)
654 register struct buffer
*b
= marker
->buffer
;
658 register struct Lisp_Marker
*tail
, **prev
;
660 /* No dead buffers here. */
661 eassert (!NILP (BVAR (b
, name
)));
663 marker
->buffer
= NULL
;
664 prev
= &BUF_MARKERS (b
);
666 for (tail
= BUF_MARKERS (b
); tail
; prev
= &tail
->next
, tail
= *prev
)
669 if (*prev
== BUF_MARKERS (b
))
671 /* Deleting first marker from the buffer's chain. Crash
672 if new first marker in chain does not say it belongs
673 to the same buffer, or at least that they have the same
675 if (tail
->next
&& b
->text
!= tail
->next
->buffer
->text
)
679 /* We have removed the marker from the chain;
680 no need to scan the rest of the chain. */
684 /* Error if marker was not in it's chain. */
685 eassert (tail
!= NULL
);
689 /* Return the char position of marker MARKER, as a C integer. */
692 marker_position (Lisp_Object marker
)
694 register struct Lisp_Marker
*m
= XMARKER (marker
);
695 register struct buffer
*buf
= m
->buffer
;
698 error ("Marker does not point anywhere");
700 eassert (BUF_BEG (buf
) <= m
->charpos
&& m
->charpos
<= BUF_Z (buf
));
705 /* Return the byte position of marker MARKER, as a C integer. */
708 marker_byte_position (Lisp_Object marker
)
710 register struct Lisp_Marker
*m
= XMARKER (marker
);
711 register struct buffer
*buf
= m
->buffer
;
714 error ("Marker does not point anywhere");
716 eassert (BUF_BEG_BYTE (buf
) <= m
->bytepos
&& m
->bytepos
<= BUF_Z_BYTE (buf
));
721 DEFUN ("copy-marker", Fcopy_marker
, Scopy_marker
, 0, 2, 0,
722 doc
: /* Return a new marker pointing at the same place as MARKER.
723 If argument is a number, makes a new marker pointing
724 at that position in the current buffer.
725 If MARKER is not specified, the new marker does not point anywhere.
726 The optional argument TYPE specifies the insertion type of the new marker;
727 see `marker-insertion-type'. */)
728 (register Lisp_Object marker
, Lisp_Object type
)
730 register Lisp_Object
new;
733 CHECK_TYPE (INTEGERP (marker
) || MARKERP (marker
), Qinteger_or_marker_p
, marker
);
735 new = Fmake_marker ();
736 Fset_marker (new, marker
,
737 (MARKERP (marker
) ? Fmarker_buffer (marker
) : Qnil
));
738 XMARKER (new)->insertion_type
= !NILP (type
);
742 DEFUN ("marker-insertion-type", Fmarker_insertion_type
,
743 Smarker_insertion_type
, 1, 1, 0,
744 doc
: /* Return insertion type of MARKER: t if it stays after inserted text.
745 The value nil means the marker stays before text inserted there. */)
746 (register Lisp_Object marker
)
748 CHECK_MARKER (marker
);
749 return XMARKER (marker
)->insertion_type
? Qt
: Qnil
;
752 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type
,
753 Sset_marker_insertion_type
, 2, 2, 0,
754 doc
: /* Set the insertion-type of MARKER to TYPE.
755 If TYPE is t, it means the marker advances when you insert text at it.
756 If TYPE is nil, it means the marker stays behind when you insert text at it. */)
757 (Lisp_Object marker
, Lisp_Object type
)
759 CHECK_MARKER (marker
);
761 XMARKER (marker
)->insertion_type
= ! NILP (type
);
765 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at
, Sbuffer_has_markers_at
,
767 doc
: /* Return t if there are markers pointing at POSITION in the current buffer. */)
768 (Lisp_Object position
)
770 register struct Lisp_Marker
*tail
;
771 register ptrdiff_t charpos
;
773 charpos
= clip_to_bounds (BEG
, XINT (position
), Z
);
775 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
776 if (tail
->charpos
== charpos
)
782 #ifdef ENABLE_CHECKING
784 /* For debugging -- count the markers in buffer BUF. */
787 count_markers (struct buffer
*buf
)
790 struct Lisp_Marker
*tail
;
792 for (tail
= BUF_MARKERS (buf
); tail
; tail
= tail
->next
)
798 #endif /* ENABLE_CHECKING */
801 syms_of_marker (void)
803 defsubr (&Smarker_position
);
804 defsubr (&Smarker_buffer
);
805 defsubr (&Sset_marker
);
806 defsubr (&Scopy_marker
);
807 defsubr (&Smarker_insertion_type
);
808 defsubr (&Sset_marker_insertion_type
);
809 defsubr (&Sbuffer_has_markers_at
);