; doc/emacs/misc.texi (Network Security): Fix typo.
[emacs.git] / src / marker.c
blob2d5b05cc2b8e6a3c15a3fd3cc36a9a9ecfe2fd9b
1 /* Markers: examining, setting and deleting.
2 Copyright (C) 1985, 1997-1998, 2001-2018 Free Software Foundation,
3 Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include "lisp.h"
24 #include "character.h"
25 #include "buffer.h"
27 /* Record one cached position found recently by
28 buf_charpos_to_bytepos or buf_bytepos_to_charpos. */
30 static ptrdiff_t cached_charpos;
31 static ptrdiff_t cached_bytepos;
32 static struct buffer *cached_buffer;
33 static EMACS_INT cached_modiff;
35 /* Juanma Barranquero <lekktu@gmail.com> reported ~3x increased
36 bootstrap time when byte_char_debug_check is enabled; so this
37 is never turned on by --enable-checking configure option. */
39 #ifdef MARKER_DEBUG
41 extern int count_markers (struct buffer *) EXTERNALLY_VISIBLE;
42 extern ptrdiff_t verify_bytepos (ptrdiff_t charpos) EXTERNALLY_VISIBLE;
44 static void
45 byte_char_debug_check (struct buffer *b, ptrdiff_t charpos, ptrdiff_t bytepos)
47 ptrdiff_t nchars;
49 if (NILP (BVAR (b, enable_multibyte_characters)))
50 return;
52 if (bytepos > BUF_GPT_BYTE (b))
53 nchars
54 = multibyte_chars_in_text (BUF_BEG_ADDR (b),
55 BUF_GPT_BYTE (b) - BUF_BEG_BYTE (b))
56 + multibyte_chars_in_text (BUF_GAP_END_ADDR (b),
57 bytepos - BUF_GPT_BYTE (b));
58 else
59 nchars = multibyte_chars_in_text (BUF_BEG_ADDR (b),
60 bytepos - BUF_BEG_BYTE (b));
62 if (charpos - 1 != nchars)
63 emacs_abort ();
66 #else /* not MARKER_DEBUG */
68 #define byte_char_debug_check(b, charpos, bytepos) do { } while (0)
70 #endif /* MARKER_DEBUG */
72 void
73 clear_charpos_cache (struct buffer *b)
75 if (cached_buffer == b)
76 cached_buffer = 0;
79 /* Converting between character positions and byte positions. */
81 /* There are several places in the buffer where we know
82 the correspondence: BEG, BEGV, PT, GPT, ZV and Z,
83 and everywhere there is a marker. So we find the one of these places
84 that is closest to the specified position, and scan from there. */
86 /* This macro is a subroutine of buf_charpos_to_bytepos.
87 Note that it is desirable that BYTEPOS is not evaluated
88 except when we really want its value. */
90 #define CONSIDER(CHARPOS, BYTEPOS) \
91 { \
92 ptrdiff_t this_charpos = (CHARPOS); \
93 bool changed = false; \
95 if (this_charpos == charpos) \
96 { \
97 ptrdiff_t value = (BYTEPOS); \
99 byte_char_debug_check (b, charpos, value); \
100 return value; \
102 else if (this_charpos > charpos) \
104 if (this_charpos < best_above) \
106 best_above = this_charpos; \
107 best_above_byte = (BYTEPOS); \
108 changed = true; \
111 else if (this_charpos > best_below) \
113 best_below = this_charpos; \
114 best_below_byte = (BYTEPOS); \
115 changed = true; \
118 if (changed) \
120 if (best_above - best_below == best_above_byte - best_below_byte) \
122 ptrdiff_t value = best_below_byte + (charpos - best_below); \
124 byte_char_debug_check (b, charpos, value); \
125 return value; \
130 static void
131 CHECK_MARKER (Lisp_Object x)
133 CHECK_TYPE (MARKERP (x), Qmarkerp, x);
136 /* When converting bytes from/to chars, we look through the list of
137 markers to try and find a good starting point (since markers keep
138 track of both bytepos and charpos at the same time).
139 But if there are many markers, it can take too much time to find a "good"
140 marker from which to start. Worse yet: if it takes a long time and we end
141 up finding a nearby markers, we won't add a new marker to cache this
142 result, so next time around we'll have to go through this same long list
143 to (re)find this best marker. So the further down the list of
144 markers we go, the less demanding we are w.r.t what is a good marker.
146 The previous code used INITIAL=50 and INCREMENT=0 and this lead to
147 really poor performance when there are many markers.
148 I haven't tried to tweak INITIAL, but experiments on my trusty Thinkpad
149 T61 using various artificial test cases seem to suggest that INCREMENT=50
150 might be "the best compromise": it significantly improved the
151 worst case and it was rarely slower and never by much.
153 The asymptotic behavior is still poor, tho, so in largish buffers with many
154 overlays (e.g. 300KB and 30K overlays), it can still be a bottleneck. */
155 #define BYTECHAR_DISTANCE_INITIAL 50
156 #define BYTECHAR_DISTANCE_INCREMENT 50
158 /* Return the byte position corresponding to CHARPOS in B. */
160 ptrdiff_t
161 buf_charpos_to_bytepos (struct buffer *b, ptrdiff_t charpos)
163 struct Lisp_Marker *tail;
164 ptrdiff_t best_above, best_above_byte;
165 ptrdiff_t best_below, best_below_byte;
166 ptrdiff_t distance = BYTECHAR_DISTANCE_INITIAL;
168 eassert (BUF_BEG (b) <= charpos && charpos <= BUF_Z (b));
170 best_above = BUF_Z (b);
171 best_above_byte = BUF_Z_BYTE (b);
173 /* If this buffer has as many characters as bytes,
174 each character must be one byte.
175 This takes care of the case where enable-multibyte-characters is nil. */
176 if (best_above == best_above_byte)
177 return charpos;
179 best_below = BEG;
180 best_below_byte = BEG_BYTE;
182 /* We find in best_above and best_above_byte
183 the closest known point above CHARPOS,
184 and in best_below and best_below_byte
185 the closest known point below CHARPOS,
187 If at any point we can tell that the space between those
188 two best approximations is all single-byte,
189 we interpolate the result immediately. */
191 CONSIDER (BUF_PT (b), BUF_PT_BYTE (b));
192 CONSIDER (BUF_GPT (b), BUF_GPT_BYTE (b));
193 CONSIDER (BUF_BEGV (b), BUF_BEGV_BYTE (b));
194 CONSIDER (BUF_ZV (b), BUF_ZV_BYTE (b));
196 if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff)
197 CONSIDER (cached_charpos, cached_bytepos);
199 for (tail = BUF_MARKERS (b); tail; tail = tail->next)
201 CONSIDER (tail->charpos, tail->bytepos);
203 /* If we are down to a range of 50 chars,
204 don't bother checking any other markers;
205 scan the intervening chars directly now. */
206 if (best_above - charpos < distance
207 || charpos - best_below < distance)
208 break;
209 else
210 distance += BYTECHAR_DISTANCE_INCREMENT;
213 /* We get here if we did not exactly hit one of the known places.
214 We have one known above and one known below.
215 Scan, counting characters, from whichever one is closer. */
217 if (charpos - best_below < best_above - charpos)
219 bool record = charpos - best_below > 5000;
221 while (best_below != charpos)
223 best_below++;
224 BUF_INC_POS (b, best_below_byte);
227 /* If this position is quite far from the nearest known position,
228 cache the correspondence by creating a marker here.
229 It will last until the next GC. */
230 if (record)
231 build_marker (b, best_below, best_below_byte);
233 byte_char_debug_check (b, best_below, best_below_byte);
235 cached_buffer = b;
236 cached_modiff = BUF_MODIFF (b);
237 cached_charpos = best_below;
238 cached_bytepos = best_below_byte;
240 return best_below_byte;
242 else
244 bool record = best_above - charpos > 5000;
246 while (best_above != charpos)
248 best_above--;
249 BUF_DEC_POS (b, best_above_byte);
252 /* If this position is quite far from the nearest known position,
253 cache the correspondence by creating a marker here.
254 It will last until the next GC. */
255 if (record)
256 build_marker (b, best_above, best_above_byte);
258 byte_char_debug_check (b, best_above, best_above_byte);
260 cached_buffer = b;
261 cached_modiff = BUF_MODIFF (b);
262 cached_charpos = best_above;
263 cached_bytepos = best_above_byte;
265 return best_above_byte;
269 #undef CONSIDER
271 /* This macro is a subroutine of buf_bytepos_to_charpos.
272 It is used when BYTEPOS is actually the byte position. */
274 #define CONSIDER(BYTEPOS, CHARPOS) \
276 ptrdiff_t this_bytepos = (BYTEPOS); \
277 int changed = false; \
279 if (this_bytepos == bytepos) \
281 ptrdiff_t value = (CHARPOS); \
283 byte_char_debug_check (b, value, bytepos); \
284 return value; \
286 else if (this_bytepos > bytepos) \
288 if (this_bytepos < best_above_byte) \
290 best_above = (CHARPOS); \
291 best_above_byte = this_bytepos; \
292 changed = true; \
295 else if (this_bytepos > best_below_byte) \
297 best_below = (CHARPOS); \
298 best_below_byte = this_bytepos; \
299 changed = true; \
302 if (changed) \
304 if (best_above - best_below == best_above_byte - best_below_byte) \
306 ptrdiff_t value = best_below + (bytepos - best_below_byte); \
308 byte_char_debug_check (b, value, bytepos); \
309 return value; \
314 /* Return the character position corresponding to BYTEPOS in B. */
316 ptrdiff_t
317 buf_bytepos_to_charpos (struct buffer *b, ptrdiff_t bytepos)
319 struct Lisp_Marker *tail;
320 ptrdiff_t best_above, best_above_byte;
321 ptrdiff_t best_below, best_below_byte;
322 ptrdiff_t distance = BYTECHAR_DISTANCE_INITIAL;
324 eassert (BUF_BEG_BYTE (b) <= bytepos && bytepos <= BUF_Z_BYTE (b));
326 best_above = BUF_Z (b);
327 best_above_byte = BUF_Z_BYTE (b);
329 /* If this buffer has as many characters as bytes,
330 each character must be one byte.
331 This takes care of the case where enable-multibyte-characters is nil. */
332 if (best_above == best_above_byte)
333 return bytepos;
335 best_below = BEG;
336 best_below_byte = BEG_BYTE;
338 CONSIDER (BUF_PT_BYTE (b), BUF_PT (b));
339 CONSIDER (BUF_GPT_BYTE (b), BUF_GPT (b));
340 CONSIDER (BUF_BEGV_BYTE (b), BUF_BEGV (b));
341 CONSIDER (BUF_ZV_BYTE (b), BUF_ZV (b));
343 if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff)
344 CONSIDER (cached_bytepos, cached_charpos);
346 for (tail = BUF_MARKERS (b); tail; tail = tail->next)
348 CONSIDER (tail->bytepos, tail->charpos);
350 /* If we are down to a range of 50 chars,
351 don't bother checking any other markers;
352 scan the intervening chars directly now. */
353 if (best_above - bytepos < distance
354 || bytepos - best_below < distance)
355 break;
356 else
357 distance += BYTECHAR_DISTANCE_INCREMENT;
360 /* We get here if we did not exactly hit one of the known places.
361 We have one known above and one known below.
362 Scan, counting characters, from whichever one is closer. */
364 if (bytepos - best_below_byte < best_above_byte - bytepos)
366 bool record = bytepos - best_below_byte > 5000;
368 while (best_below_byte < bytepos)
370 best_below++;
371 BUF_INC_POS (b, best_below_byte);
374 /* If this position is quite far from the nearest known position,
375 cache the correspondence by creating a marker here.
376 It will last until the next GC.
377 But don't do it if BUF_MARKERS is nil;
378 that is a signal from Fset_buffer_multibyte. */
379 if (record && BUF_MARKERS (b))
380 build_marker (b, best_below, best_below_byte);
382 byte_char_debug_check (b, best_below, best_below_byte);
384 cached_buffer = b;
385 cached_modiff = BUF_MODIFF (b);
386 cached_charpos = best_below;
387 cached_bytepos = best_below_byte;
389 return best_below;
391 else
393 bool record = best_above_byte - bytepos > 5000;
395 while (best_above_byte > bytepos)
397 best_above--;
398 BUF_DEC_POS (b, best_above_byte);
401 /* If this position is quite far from the nearest known position,
402 cache the correspondence by creating a marker here.
403 It will last until the next GC.
404 But don't do it if BUF_MARKERS is nil;
405 that is a signal from Fset_buffer_multibyte. */
406 if (record && BUF_MARKERS (b))
407 build_marker (b, best_above, best_above_byte);
409 byte_char_debug_check (b, best_above, best_above_byte);
411 cached_buffer = b;
412 cached_modiff = BUF_MODIFF (b);
413 cached_charpos = best_above;
414 cached_bytepos = best_above_byte;
416 return best_above;
420 #undef CONSIDER
422 /* Operations on markers. */
424 DEFUN ("marker-buffer", Fmarker_buffer, Smarker_buffer, 1, 1, 0,
425 doc: /* Return the buffer that MARKER points into, or nil if none.
426 Returns nil if MARKER points into a dead buffer. */)
427 (register Lisp_Object marker)
429 register Lisp_Object buf;
430 CHECK_MARKER (marker);
431 if (XMARKER (marker)->buffer)
433 XSETBUFFER (buf, XMARKER (marker)->buffer);
434 /* If the buffer is dead, we're in trouble: the buffer pointer here
435 does not preserve the buffer from being GC'd (it's weak), so
436 markers have to be unlinked from their buffer as soon as the buffer
437 is killed. */
438 eassert (BUFFER_LIVE_P (XBUFFER (buf)));
439 return buf;
441 return Qnil;
444 DEFUN ("marker-position", Fmarker_position, Smarker_position, 1, 1, 0,
445 doc: /* Return the position of MARKER, or nil if it points nowhere. */)
446 (Lisp_Object marker)
448 CHECK_MARKER (marker);
449 if (XMARKER (marker)->buffer)
450 return make_number (XMARKER (marker)->charpos);
452 return Qnil;
455 /* Change M so it points to B at CHARPOS and BYTEPOS. */
457 static void
458 attach_marker (struct Lisp_Marker *m, struct buffer *b,
459 ptrdiff_t charpos, ptrdiff_t bytepos)
461 /* In a single-byte buffer, two positions must be equal.
462 Otherwise, every character is at least one byte. */
463 if (BUF_Z (b) == BUF_Z_BYTE (b))
464 eassert (charpos == bytepos);
465 else
466 eassert (charpos <= bytepos);
468 m->charpos = charpos;
469 m->bytepos = bytepos;
471 if (m->buffer != b)
473 unchain_marker (m);
474 m->buffer = b;
475 m->next = BUF_MARKERS (b);
476 BUF_MARKERS (b) = m;
480 /* If BUFFER is nil, return current buffer pointer. Next, check
481 whether BUFFER is a buffer object and return buffer pointer
482 corresponding to BUFFER if BUFFER is live, or NULL otherwise. */
484 static struct buffer *
485 live_buffer (Lisp_Object buffer)
487 struct buffer *b = decode_buffer (buffer);
488 return BUFFER_LIVE_P (b) ? b : NULL;
491 /* Internal function to set MARKER in BUFFER at POSITION. Non-zero
492 RESTRICTED means limit the POSITION by the visible part of BUFFER. */
494 static Lisp_Object
495 set_marker_internal (Lisp_Object marker, Lisp_Object position,
496 Lisp_Object buffer, bool restricted)
498 struct Lisp_Marker *m;
499 struct buffer *b = live_buffer (buffer);
501 CHECK_MARKER (marker);
502 m = XMARKER (marker);
504 /* Set MARKER to point nowhere if BUFFER is dead, or
505 POSITION is nil or a marker points to nowhere. */
506 if (NILP (position)
507 || (MARKERP (position) && !XMARKER (position)->buffer)
508 || !b)
509 unchain_marker (m);
511 /* Optimize the special case where we are copying the position of
512 an existing marker, and MARKER is already in the same buffer. */
513 else if (MARKERP (position) && b == XMARKER (position)->buffer
514 && b == m->buffer)
516 m->bytepos = XMARKER (position)->bytepos;
517 m->charpos = XMARKER (position)->charpos;
520 else
522 register ptrdiff_t charpos, bytepos;
524 /* Do not use CHECK_NUMBER_COERCE_MARKER because we
525 don't want to call buf_charpos_to_bytepos if POSITION
526 is a marker and so we know the bytepos already. */
527 if (INTEGERP (position))
528 charpos = XINT (position), bytepos = -1;
529 else if (MARKERP (position))
531 charpos = XMARKER (position)->charpos;
532 bytepos = XMARKER (position)->bytepos;
534 else
535 wrong_type_argument (Qinteger_or_marker_p, position);
537 charpos = clip_to_bounds
538 (restricted ? BUF_BEGV (b) : BUF_BEG (b), charpos,
539 restricted ? BUF_ZV (b) : BUF_Z (b));
540 /* Don't believe BYTEPOS if it comes from a different buffer,
541 since that buffer might have a very different correspondence
542 between character and byte positions. */
543 if (bytepos == -1
544 || !(MARKERP (position) && XMARKER (position)->buffer == b))
545 bytepos = buf_charpos_to_bytepos (b, charpos);
546 else
547 bytepos = clip_to_bounds
548 (restricted ? BUF_BEGV_BYTE (b) : BUF_BEG_BYTE (b),
549 bytepos, restricted ? BUF_ZV_BYTE (b) : BUF_Z_BYTE (b));
551 attach_marker (m, b, charpos, bytepos);
553 return marker;
556 DEFUN ("set-marker", Fset_marker, Sset_marker, 2, 3, 0,
557 doc: /* Position MARKER before character number POSITION in BUFFER.
558 If BUFFER is omitted or nil, it defaults to the current buffer. If
559 POSITION is nil, makes marker point nowhere so it no longer slows down
560 editing in any buffer. Returns MARKER. */)
561 (Lisp_Object marker, Lisp_Object position, Lisp_Object buffer)
563 return set_marker_internal (marker, position, buffer, false);
566 /* Like the above, but won't let the position be outside the visible part. */
568 Lisp_Object
569 set_marker_restricted (Lisp_Object marker, Lisp_Object position,
570 Lisp_Object buffer)
572 return set_marker_internal (marker, position, buffer, true);
575 /* Set the position of MARKER, specifying both the
576 character position and the corresponding byte position. */
578 Lisp_Object
579 set_marker_both (Lisp_Object marker, Lisp_Object buffer,
580 ptrdiff_t charpos, ptrdiff_t bytepos)
582 register struct Lisp_Marker *m;
583 register struct buffer *b = live_buffer (buffer);
585 CHECK_MARKER (marker);
586 m = XMARKER (marker);
588 if (b)
589 attach_marker (m, b, charpos, bytepos);
590 else
591 unchain_marker (m);
592 return marker;
595 /* Like the above, but won't let the position be outside the visible part. */
597 Lisp_Object
598 set_marker_restricted_both (Lisp_Object marker, Lisp_Object buffer,
599 ptrdiff_t charpos, ptrdiff_t bytepos)
601 register struct Lisp_Marker *m;
602 register struct buffer *b = live_buffer (buffer);
604 CHECK_MARKER (marker);
605 m = XMARKER (marker);
607 if (b)
609 attach_marker
610 (m, b,
611 clip_to_bounds (BUF_BEGV (b), charpos, BUF_ZV (b)),
612 clip_to_bounds (BUF_BEGV_BYTE (b), bytepos, BUF_ZV_BYTE (b)));
614 else
615 unchain_marker (m);
616 return marker;
619 /* Detach a marker so that it no longer points anywhere and no longer
620 slows down editing. Do not free the marker, though, as a change
621 function could have inserted it into an undo list (Bug#30931). */
622 void
623 detach_marker (Lisp_Object marker)
625 Fset_marker (marker, Qnil, Qnil);
628 /* Remove MARKER from the chain of whatever buffer it is in,
629 leaving it points to nowhere. This is called during garbage
630 collection, so we must be careful to ignore and preserve
631 mark bits, including those in chain fields of markers. */
633 void
634 unchain_marker (register struct Lisp_Marker *marker)
636 register struct buffer *b = marker->buffer;
638 if (b)
640 register struct Lisp_Marker *tail, **prev;
642 /* No dead buffers here. */
643 eassert (BUFFER_LIVE_P (b));
645 marker->buffer = NULL;
646 prev = &BUF_MARKERS (b);
648 for (tail = BUF_MARKERS (b); tail; prev = &tail->next, tail = *prev)
649 if (marker == tail)
651 if (*prev == BUF_MARKERS (b))
653 /* Deleting first marker from the buffer's chain. Crash
654 if new first marker in chain does not say it belongs
655 to the same buffer, or at least that they have the same
656 base buffer. */
657 if (tail->next && b->text != tail->next->buffer->text)
658 emacs_abort ();
660 *prev = tail->next;
661 /* We have removed the marker from the chain;
662 no need to scan the rest of the chain. */
663 break;
666 /* Error if marker was not in it's chain. */
667 eassert (tail != NULL);
671 /* Return the char position of marker MARKER, as a C integer. */
673 ptrdiff_t
674 marker_position (Lisp_Object marker)
676 register struct Lisp_Marker *m = XMARKER (marker);
677 register struct buffer *buf = m->buffer;
679 if (!buf)
680 error ("Marker does not point anywhere");
682 eassert (BUF_BEG (buf) <= m->charpos && m->charpos <= BUF_Z (buf));
684 return m->charpos;
687 /* Return the byte position of marker MARKER, as a C integer. */
689 ptrdiff_t
690 marker_byte_position (Lisp_Object marker)
692 register struct Lisp_Marker *m = XMARKER (marker);
693 register struct buffer *buf = m->buffer;
695 if (!buf)
696 error ("Marker does not point anywhere");
698 eassert (BUF_BEG_BYTE (buf) <= m->bytepos && m->bytepos <= BUF_Z_BYTE (buf));
700 return m->bytepos;
703 DEFUN ("copy-marker", Fcopy_marker, Scopy_marker, 0, 2, 0,
704 doc: /* Return a new marker pointing at the same place as MARKER.
705 If argument is a number, makes a new marker pointing
706 at that position in the current buffer.
707 If MARKER is not specified, the new marker does not point anywhere.
708 The optional argument TYPE specifies the insertion type of the new marker;
709 see `marker-insertion-type'. */)
710 (register Lisp_Object marker, Lisp_Object type)
712 register Lisp_Object new;
714 if (!NILP (marker))
715 CHECK_TYPE (INTEGERP (marker) || MARKERP (marker), Qinteger_or_marker_p, marker);
717 new = Fmake_marker ();
718 Fset_marker (new, marker,
719 (MARKERP (marker) ? Fmarker_buffer (marker) : Qnil));
720 XMARKER (new)->insertion_type = !NILP (type);
721 return new;
724 DEFUN ("marker-insertion-type", Fmarker_insertion_type,
725 Smarker_insertion_type, 1, 1, 0,
726 doc: /* Return insertion type of MARKER: t if it stays after inserted text.
727 The value nil means the marker stays before text inserted there. */)
728 (register Lisp_Object marker)
730 CHECK_MARKER (marker);
731 return XMARKER (marker)->insertion_type ? Qt : Qnil;
734 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type,
735 Sset_marker_insertion_type, 2, 2, 0,
736 doc: /* Set the insertion-type of MARKER to TYPE.
737 If TYPE is t, it means the marker advances when you insert text at it.
738 If TYPE is nil, it means the marker stays behind when you insert text at it. */)
739 (Lisp_Object marker, Lisp_Object type)
741 CHECK_MARKER (marker);
743 XMARKER (marker)->insertion_type = ! NILP (type);
744 return type;
747 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at, Sbuffer_has_markers_at,
748 1, 1, 0,
749 doc: /* Return t if there are markers pointing at POSITION in the current buffer. */)
750 (Lisp_Object position)
752 register struct Lisp_Marker *tail;
753 register ptrdiff_t charpos;
755 charpos = clip_to_bounds (BEG, XINT (position), Z);
757 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
758 if (tail->charpos == charpos)
759 return Qt;
761 return Qnil;
764 #ifdef MARKER_DEBUG
766 /* For debugging -- count the markers in buffer BUF. */
769 count_markers (struct buffer *buf)
771 int total = 0;
772 struct Lisp_Marker *tail;
774 for (tail = BUF_MARKERS (buf); tail; tail = tail->next)
775 total++;
777 return total;
780 /* For debugging -- recompute the bytepos corresponding
781 to CHARPOS in the simplest, most reliable way. */
783 ptrdiff_t
784 verify_bytepos (ptrdiff_t charpos)
786 ptrdiff_t below = BEG;
787 ptrdiff_t below_byte = BEG_BYTE;
789 while (below != charpos)
791 below++;
792 BUF_INC_POS (current_buffer, below_byte);
795 return below_byte;
798 #endif /* MARKER_DEBUG */
800 void
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);