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 (NILP (BVAR (b
, enable_multibyte_characters
)))
46 if (bytepos
> BUF_GPT_BYTE (b
))
48 = multibyte_chars_in_text (BUF_BEG_ADDR (b
),
49 BUF_GPT_BYTE (b
) - BUF_BEG_BYTE (b
))
50 + multibyte_chars_in_text (BUF_GAP_END_ADDR (b
),
51 bytepos
- BUF_GPT_BYTE (b
));
53 nchars
= multibyte_chars_in_text (BUF_BEG_ADDR (b
),
54 bytepos
- BUF_BEG_BYTE (b
));
56 if (charpos
- 1 != nchars
)
60 #else /* not ENABLE_CHECKING */
62 #define byte_char_debug_check(b,charpos,bytepos) do { } while (0)
64 #endif /* ENABLE_CHECKING */
67 clear_charpos_cache (struct buffer
*b
)
69 if (cached_buffer
== b
)
73 /* Converting between character positions and byte positions. */
75 /* There are several places in the buffer where we know
76 the correspondence: BEG, BEGV, PT, GPT, ZV and Z,
77 and everywhere there is a marker. So we find the one of these places
78 that is closest to the specified position, and scan from there. */
80 /* charpos_to_bytepos returns the byte position corresponding to CHARPOS. */
82 /* This macro is a subroutine of charpos_to_bytepos.
83 Note that it is desirable that BYTEPOS is not evaluated
84 except when we really want its value. */
86 #define CONSIDER(CHARPOS, BYTEPOS) \
88 ptrdiff_t this_charpos = (CHARPOS); \
91 if (this_charpos == charpos) \
93 ptrdiff_t value = (BYTEPOS); \
95 byte_char_debug_check (b, charpos, value); \
98 else if (this_charpos > charpos) \
100 if (this_charpos < best_above) \
102 best_above = this_charpos; \
103 best_above_byte = (BYTEPOS); \
107 else if (this_charpos > best_below) \
109 best_below = this_charpos; \
110 best_below_byte = (BYTEPOS); \
116 if (best_above - best_below == best_above_byte - best_below_byte) \
118 ptrdiff_t value = best_below_byte + (charpos - best_below); \
120 byte_char_debug_check (b, charpos, value); \
127 charpos_to_bytepos (ptrdiff_t charpos
)
129 return buf_charpos_to_bytepos (current_buffer
, charpos
);
133 buf_charpos_to_bytepos (struct buffer
*b
, ptrdiff_t charpos
)
135 struct Lisp_Marker
*tail
;
136 ptrdiff_t best_above
, best_above_byte
;
137 ptrdiff_t best_below
, best_below_byte
;
139 if (charpos
< BUF_BEG (b
) || charpos
> BUF_Z (b
))
142 best_above
= BUF_Z (b
);
143 best_above_byte
= BUF_Z_BYTE (b
);
145 /* If this buffer has as many characters as bytes,
146 each character must be one byte.
147 This takes care of the case where enable-multibyte-characters is nil. */
148 if (best_above
== best_above_byte
)
152 best_below_byte
= BEG_BYTE
;
154 /* We find in best_above and best_above_byte
155 the closest known point above CHARPOS,
156 and in best_below and best_below_byte
157 the closest known point below CHARPOS,
159 If at any point we can tell that the space between those
160 two best approximations is all single-byte,
161 we interpolate the result immediately. */
163 CONSIDER (BUF_PT (b
), BUF_PT_BYTE (b
));
164 CONSIDER (BUF_GPT (b
), BUF_GPT_BYTE (b
));
165 CONSIDER (BUF_BEGV (b
), BUF_BEGV_BYTE (b
));
166 CONSIDER (BUF_ZV (b
), BUF_ZV_BYTE (b
));
168 if (b
== cached_buffer
&& BUF_MODIFF (b
) == cached_modiff
)
169 CONSIDER (cached_charpos
, cached_bytepos
);
171 for (tail
= BUF_MARKERS (b
); tail
; tail
= tail
->next
)
173 CONSIDER (tail
->charpos
, tail
->bytepos
);
175 /* If we are down to a range of 50 chars,
176 don't bother checking any other markers;
177 scan the intervening chars directly now. */
178 if (best_above
- best_below
< 50)
182 /* We get here if we did not exactly hit one of the known places.
183 We have one known above and one known below.
184 Scan, counting characters, from whichever one is closer. */
186 if (charpos
- best_below
< best_above
- charpos
)
188 int record
= charpos
- best_below
> 5000;
190 while (best_below
!= charpos
)
193 BUF_INC_POS (b
, best_below_byte
);
196 /* If this position is quite far from the nearest known position,
197 cache the correspondence by creating a marker here.
198 It will last until the next GC. */
200 build_marker (b
, best_below
, best_below_byte
);
202 byte_char_debug_check (b
, best_below
, best_below_byte
);
205 cached_modiff
= BUF_MODIFF (b
);
206 cached_charpos
= best_below
;
207 cached_bytepos
= best_below_byte
;
209 return best_below_byte
;
213 int record
= best_above
- charpos
> 5000;
215 while (best_above
!= charpos
)
218 BUF_DEC_POS (b
, best_above_byte
);
221 /* If this position is quite far from the nearest known position,
222 cache the correspondence by creating a marker here.
223 It will last until the next GC. */
225 build_marker (b
, best_above
, best_above_byte
);
227 byte_char_debug_check (b
, best_above
, best_above_byte
);
230 cached_modiff
= BUF_MODIFF (b
);
231 cached_charpos
= best_above
;
232 cached_bytepos
= best_above_byte
;
234 return best_above_byte
;
240 /* Used for debugging: recompute the bytepos corresponding to CHARPOS
241 in the simplest, most reliable way. */
243 extern ptrdiff_t verify_bytepos (ptrdiff_t charpos
) EXTERNALLY_VISIBLE
;
245 verify_bytepos (ptrdiff_t charpos
)
248 ptrdiff_t below_byte
= 1;
250 while (below
!= charpos
)
253 BUF_INC_POS (current_buffer
, below_byte
);
259 /* buf_bytepos_to_charpos returns the char position corresponding to
262 /* This macro is a subroutine of buf_bytepos_to_charpos.
263 It is used when BYTEPOS is actually the byte position. */
265 #define CONSIDER(BYTEPOS, CHARPOS) \
267 ptrdiff_t this_bytepos = (BYTEPOS); \
270 if (this_bytepos == bytepos) \
272 ptrdiff_t value = (CHARPOS); \
274 byte_char_debug_check (b, value, bytepos); \
277 else if (this_bytepos > bytepos) \
279 if (this_bytepos < best_above_byte) \
281 best_above = (CHARPOS); \
282 best_above_byte = this_bytepos; \
286 else if (this_bytepos > best_below_byte) \
288 best_below = (CHARPOS); \
289 best_below_byte = this_bytepos; \
295 if (best_above - best_below == best_above_byte - best_below_byte) \
297 ptrdiff_t value = best_below + (bytepos - best_below_byte); \
299 byte_char_debug_check (b, value, bytepos); \
306 buf_bytepos_to_charpos (struct buffer
*b
, ptrdiff_t bytepos
)
308 struct Lisp_Marker
*tail
;
309 ptrdiff_t best_above
, best_above_byte
;
310 ptrdiff_t best_below
, best_below_byte
;
312 if (bytepos
< BUF_BEG_BYTE (b
) || bytepos
> BUF_Z_BYTE (b
))
315 best_above
= BUF_Z (b
);
316 best_above_byte
= BUF_Z_BYTE (b
);
318 /* If this buffer has as many characters as bytes,
319 each character must be one byte.
320 This takes care of the case where enable-multibyte-characters is nil. */
321 if (best_above
== best_above_byte
)
325 best_below_byte
= BEG_BYTE
;
327 CONSIDER (BUF_PT_BYTE (b
), BUF_PT (b
));
328 CONSIDER (BUF_GPT_BYTE (b
), BUF_GPT (b
));
329 CONSIDER (BUF_BEGV_BYTE (b
), BUF_BEGV (b
));
330 CONSIDER (BUF_ZV_BYTE (b
), BUF_ZV (b
));
332 if (b
== cached_buffer
&& BUF_MODIFF (b
) == cached_modiff
)
333 CONSIDER (cached_bytepos
, cached_charpos
);
335 for (tail
= BUF_MARKERS (b
); tail
; tail
= tail
->next
)
337 CONSIDER (tail
->bytepos
, tail
->charpos
);
339 /* If we are down to a range of 50 chars,
340 don't bother checking any other markers;
341 scan the intervening chars directly now. */
342 if (best_above
- best_below
< 50)
346 /* We get here if we did not exactly hit one of the known places.
347 We have one known above and one known below.
348 Scan, counting characters, from whichever one is closer. */
350 if (bytepos
- best_below_byte
< best_above_byte
- bytepos
)
352 int record
= bytepos
- best_below_byte
> 5000;
354 while (best_below_byte
< bytepos
)
357 BUF_INC_POS (b
, best_below_byte
);
360 /* If this position is quite far from the nearest known position,
361 cache the correspondence by creating a marker here.
362 It will last until the next GC.
363 But don't do it if BUF_MARKERS is nil;
364 that is a signal from Fset_buffer_multibyte. */
365 if (record
&& BUF_MARKERS (b
))
366 build_marker (b
, best_below
, best_below_byte
);
368 byte_char_debug_check (b
, best_below
, best_below_byte
);
371 cached_modiff
= BUF_MODIFF (b
);
372 cached_charpos
= best_below
;
373 cached_bytepos
= best_below_byte
;
379 int record
= best_above_byte
- bytepos
> 5000;
381 while (best_above_byte
> bytepos
)
384 BUF_DEC_POS (b
, best_above_byte
);
387 /* If this position is quite far from the nearest known position,
388 cache the correspondence by creating a marker here.
389 It will last until the next GC.
390 But don't do it if BUF_MARKERS is nil;
391 that is a signal from Fset_buffer_multibyte. */
392 if (record
&& BUF_MARKERS (b
))
393 build_marker (b
, best_above
, best_above_byte
);
395 byte_char_debug_check (b
, best_above
, best_above_byte
);
398 cached_modiff
= BUF_MODIFF (b
);
399 cached_charpos
= best_above
;
400 cached_bytepos
= best_above_byte
;
408 /* Operations on markers. */
410 DEFUN ("marker-buffer", Fmarker_buffer
, Smarker_buffer
, 1, 1, 0,
411 doc
: /* Return the buffer that MARKER points into, or nil if none.
412 Returns nil if MARKER points into a dead buffer. */)
413 (register Lisp_Object marker
)
415 register Lisp_Object buf
;
416 CHECK_MARKER (marker
);
417 if (XMARKER (marker
)->buffer
)
419 XSETBUFFER (buf
, XMARKER (marker
)->buffer
);
420 /* If the buffer is dead, we're in trouble: the buffer pointer here
421 does not preserve the buffer from being GC'd (it's weak), so
422 markers have to be unlinked from their buffer as soon as the buffer
424 eassert (!NILP (BVAR (XBUFFER (buf
), name
)));
430 DEFUN ("marker-position", Fmarker_position
, Smarker_position
, 1, 1, 0,
431 doc
: /* Return the position MARKER points at, as a character number.
432 Returns nil if MARKER points nowhere. */)
435 CHECK_MARKER (marker
);
436 if (XMARKER (marker
)->buffer
)
437 return make_number (XMARKER (marker
)->charpos
);
442 /* Change M so it points to B at CHARPOS and BYTEPOS. */
445 attach_marker (struct Lisp_Marker
*m
, struct buffer
*b
,
446 ptrdiff_t charpos
, ptrdiff_t bytepos
)
448 /* Every character is at least one byte. */
449 eassert (charpos
<= bytepos
);
451 m
->charpos
= charpos
;
452 m
->bytepos
= bytepos
;
458 m
->next
= BUF_MARKERS (b
);
463 DEFUN ("set-marker", Fset_marker
, Sset_marker
, 2, 3, 0,
464 doc
: /* Position MARKER before character number POSITION in BUFFER.
465 BUFFER defaults to the current buffer.
466 If POSITION is nil, makes marker point nowhere.
467 Then it no longer slows down editing in any buffer.
469 (Lisp_Object marker
, Lisp_Object position
, Lisp_Object buffer
)
471 register ptrdiff_t charpos
;
472 register ptrdiff_t bytepos
;
473 register struct buffer
*b
;
474 register struct Lisp_Marker
*m
;
476 CHECK_MARKER (marker
);
477 m
= XMARKER (marker
);
479 /* If position is nil or a marker that points nowhere,
480 make this marker point nowhere. */
482 || (MARKERP (position
) && !XMARKER (position
)->buffer
))
492 CHECK_BUFFER (buffer
);
493 b
= XBUFFER (buffer
);
494 /* If buffer is dead, set marker to point nowhere. */
495 if (EQ (BVAR (b
, name
), Qnil
))
502 /* Optimize the special case where we are copying the position
503 of an existing marker, and MARKER is already in the same buffer. */
504 if (MARKERP (position
) && b
== XMARKER (position
)->buffer
507 m
->bytepos
= XMARKER (position
)->bytepos
;
508 m
->charpos
= XMARKER (position
)->charpos
;
512 CHECK_NUMBER_COERCE_MARKER (position
);
513 charpos
= clip_to_bounds (BUF_BEG (b
), XINT (position
), BUF_Z (b
));
514 bytepos
= buf_charpos_to_bytepos (b
, charpos
);
516 attach_marker (m
, b
, charpos
, bytepos
);
520 /* This version of Fset_marker won't let the position
521 be outside the visible part. */
524 set_marker_restricted (Lisp_Object marker
, Lisp_Object pos
, Lisp_Object buffer
)
526 register ptrdiff_t charpos
;
527 register ptrdiff_t bytepos
;
528 register struct buffer
*b
;
529 register struct Lisp_Marker
*m
;
531 CHECK_MARKER (marker
);
532 m
= XMARKER (marker
);
534 /* If position is nil or a marker that points nowhere,
535 make this marker point nowhere. */
537 || (MARKERP (pos
) && !XMARKER (pos
)->buffer
))
547 CHECK_BUFFER (buffer
);
548 b
= XBUFFER (buffer
);
549 /* If buffer is dead, set marker to point nowhere. */
550 if (EQ (BVAR (b
, name
), Qnil
))
557 /* Optimize the special case where we are copying the position
558 of an existing marker, and MARKER is already in the same buffer. */
559 if (MARKERP (pos
) && b
== XMARKER (pos
)->buffer
562 m
->bytepos
= XMARKER (pos
)->bytepos
;
563 m
->charpos
= XMARKER (pos
)->charpos
;
567 CHECK_NUMBER_COERCE_MARKER (pos
);
568 charpos
= clip_to_bounds (BUF_BEGV (b
), XINT (pos
), BUF_ZV (b
));
569 bytepos
= buf_charpos_to_bytepos (b
, charpos
);
571 attach_marker (m
, b
, charpos
, bytepos
);
575 /* Set the position of MARKER, specifying both the
576 character position and the corresponding byte position. */
579 set_marker_both (Lisp_Object marker
, Lisp_Object buffer
, ptrdiff_t charpos
, ptrdiff_t bytepos
)
581 register struct buffer
*b
;
582 register struct Lisp_Marker
*m
;
584 CHECK_MARKER (marker
);
585 m
= XMARKER (marker
);
591 CHECK_BUFFER (buffer
);
592 b
= XBUFFER (buffer
);
593 /* If buffer is dead, set marker to point nowhere. */
594 if (EQ (BVAR (b
, name
), Qnil
))
601 /* In a single-byte buffer, the two positions must be equal. */
602 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
603 && charpos
!= bytepos
)
606 attach_marker (m
, b
, charpos
, bytepos
);
610 /* This version of set_marker_both won't let the position
611 be outside the visible part. */
614 set_marker_restricted_both (Lisp_Object marker
, Lisp_Object buffer
, ptrdiff_t charpos
, ptrdiff_t bytepos
)
616 register struct buffer
*b
;
617 register struct Lisp_Marker
*m
;
619 CHECK_MARKER (marker
);
620 m
= XMARKER (marker
);
626 CHECK_BUFFER (buffer
);
627 b
= XBUFFER (buffer
);
628 /* If buffer is dead, set marker to point nowhere. */
629 if (EQ (BVAR (b
, name
), Qnil
))
636 charpos
= clip_to_bounds (BUF_BEGV (b
), charpos
, BUF_ZV (b
));
637 bytepos
= clip_to_bounds (BUF_BEGV_BYTE (b
), bytepos
, BUF_ZV_BYTE (b
));
639 /* In a single-byte buffer, the two positions must be equal. */
640 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
641 && charpos
!= bytepos
)
644 attach_marker (m
, b
, charpos
, bytepos
);
648 /* Remove MARKER from the chain of whatever buffer it is in,
649 leaving it points to nowhere. This is called during garbage
650 collection, so we must be careful to ignore and preserve
651 mark bits, including those in chain fields of markers. */
654 unchain_marker (register struct Lisp_Marker
*marker
)
656 register struct buffer
*b
= marker
->buffer
;
660 register struct Lisp_Marker
*tail
, **prev
;
662 /* No dead buffers here. */
663 eassert (!NILP (BVAR (b
, name
)));
665 marker
->buffer
= NULL
;
666 prev
= &BUF_MARKERS (b
);
668 for (tail
= BUF_MARKERS (b
); tail
; prev
= &tail
->next
, tail
= *prev
)
671 if (*prev
== BUF_MARKERS (b
))
673 /* Deleting first marker from the buffer's chain. Crash
674 if new first marker in chain does not say it belongs
675 to the same buffer, or at least that they have the same
677 if (tail
->next
&& b
->text
!= tail
->next
->buffer
->text
)
681 /* We have removed the marker from the chain;
682 no need to scan the rest of the chain. */
686 /* Error if marker was not in it's chain. */
687 eassert (tail
!= NULL
);
691 /* Return the char position of marker MARKER, as a C integer. */
694 marker_position (Lisp_Object marker
)
696 register struct Lisp_Marker
*m
= XMARKER (marker
);
697 register struct buffer
*buf
= m
->buffer
;
700 error ("Marker does not point anywhere");
702 eassert (BUF_BEG (buf
) <= m
->charpos
&& m
->charpos
<= BUF_Z (buf
));
707 /* Return the byte position of marker MARKER, as a C integer. */
710 marker_byte_position (Lisp_Object marker
)
712 register struct Lisp_Marker
*m
= XMARKER (marker
);
713 register struct buffer
*buf
= m
->buffer
;
716 error ("Marker does not point anywhere");
718 eassert (BUF_BEG_BYTE (buf
) <= m
->bytepos
&& m
->bytepos
<= BUF_Z_BYTE (buf
));
723 DEFUN ("copy-marker", Fcopy_marker
, Scopy_marker
, 0, 2, 0,
724 doc
: /* Return a new marker pointing at the same place as MARKER.
725 If argument is a number, makes a new marker pointing
726 at that position in the current buffer.
727 If MARKER is not specified, the new marker does not point anywhere.
728 The optional argument TYPE specifies the insertion type of the new marker;
729 see `marker-insertion-type'. */)
730 (register Lisp_Object marker
, Lisp_Object type
)
732 register Lisp_Object
new;
735 CHECK_TYPE (INTEGERP (marker
) || MARKERP (marker
), Qinteger_or_marker_p
, marker
);
737 new = Fmake_marker ();
738 Fset_marker (new, marker
,
739 (MARKERP (marker
) ? Fmarker_buffer (marker
) : Qnil
));
740 XMARKER (new)->insertion_type
= !NILP (type
);
744 DEFUN ("marker-insertion-type", Fmarker_insertion_type
,
745 Smarker_insertion_type
, 1, 1, 0,
746 doc
: /* Return insertion type of MARKER: t if it stays after inserted text.
747 The value nil means the marker stays before text inserted there. */)
748 (register Lisp_Object marker
)
750 CHECK_MARKER (marker
);
751 return XMARKER (marker
)->insertion_type
? Qt
: Qnil
;
754 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type
,
755 Sset_marker_insertion_type
, 2, 2, 0,
756 doc
: /* Set the insertion-type of MARKER to TYPE.
757 If TYPE is t, it means the marker advances when you insert text at it.
758 If TYPE is nil, it means the marker stays behind when you insert text at it. */)
759 (Lisp_Object marker
, Lisp_Object type
)
761 CHECK_MARKER (marker
);
763 XMARKER (marker
)->insertion_type
= ! NILP (type
);
767 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at
, Sbuffer_has_markers_at
,
769 doc
: /* Return t if there are markers pointing at POSITION in the current buffer. */)
770 (Lisp_Object position
)
772 register struct Lisp_Marker
*tail
;
773 register ptrdiff_t charpos
;
775 charpos
= clip_to_bounds (BEG
, XINT (position
), Z
);
777 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
778 if (tail
->charpos
== charpos
)
784 #ifdef ENABLE_CHECKING
786 /* For debugging -- count the markers in buffer BUF. */
789 count_markers (struct buffer
*buf
)
792 struct Lisp_Marker
*tail
;
794 for (tail
= BUF_MARKERS (buf
); tail
; tail
= tail
->next
)
800 #endif /* ENABLE_CHECKING */
803 syms_of_marker (void)
805 defsubr (&Smarker_position
);
806 defsubr (&Smarker_buffer
);
807 defsubr (&Sset_marker
);
808 defsubr (&Scopy_marker
);
809 defsubr (&Smarker_insertion_type
);
810 defsubr (&Sset_marker_insertion_type
);
811 defsubr (&Sbuffer_has_markers_at
);