(f90-indent-to, f90-indent-line-no)
[emacs.git] / src / marker.c
blobb91609ca084fbede024222b1ed333a99139a89e6
1 /* Markers: examining, setting and deleting.
2 Copyright (C) 1985, 1997, 1998 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 2, or (at your option)
9 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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "charset.h"
27 /* Record one cached position found recently by
28 buf_charpos_to_bytepos or buf_bytepos_to_charpos. */
30 static int cached_charpos;
31 static int cached_bytepos;
32 static struct buffer *cached_buffer;
33 static int cached_modiff;
35 static void byte_char_debug_check P_ ((struct buffer *, int, int));
37 /* Nonzero means enable debugging checks on byte/char correspondences. */
39 static int byte_debug_flag;
41 void
42 clear_charpos_cache (b)
43 struct buffer *b;
45 if (cached_buffer == b)
46 cached_buffer = 0;
49 /* Converting between character positions and byte positions. */
51 /* There are several places in the buffer where we know
52 the correspondence: BEG, BEGV, PT, GPT, ZV and Z,
53 and everywhere there is a marker. So we find the one of these places
54 that is closest to the specified position, and scan from there. */
56 /* charpos_to_bytepos returns the byte position corresponding to CHARPOS. */
58 /* This macro is a subroutine of charpos_to_bytepos.
59 Note that it is desirable that BYTEPOS is not evaluated
60 except when we really want its value. */
62 #define CONSIDER(CHARPOS, BYTEPOS) \
63 { \
64 int this_charpos = (CHARPOS); \
65 int changed = 0; \
67 if (this_charpos == charpos) \
68 { \
69 int value = (BYTEPOS); \
70 if (byte_debug_flag) \
71 byte_char_debug_check (b, charpos, value); \
72 return value; \
73 } \
74 else if (this_charpos > charpos) \
75 { \
76 if (this_charpos < best_above) \
77 { \
78 best_above = this_charpos; \
79 best_above_byte = (BYTEPOS); \
80 changed = 1; \
81 } \
82 } \
83 else if (this_charpos > best_below) \
84 { \
85 best_below = this_charpos; \
86 best_below_byte = (BYTEPOS); \
87 changed = 1; \
88 } \
90 if (changed) \
91 { \
92 if (best_above - best_below == best_above_byte - best_below_byte) \
93 { \
94 int value = best_below_byte + (charpos - best_below); \
95 if (byte_debug_flag) \
96 byte_char_debug_check (b, charpos, value); \
97 return value; \
98 } \
99 } \
102 static void
103 byte_char_debug_check (b, charpos, bytepos)
104 struct buffer *b;
105 int charpos, bytepos;
107 int nchars = 0;
109 if (bytepos > BUF_GPT_BYTE (b))
111 nchars = multibyte_chars_in_text (BUF_BEG_ADDR (b),
112 BUF_GPT_BYTE (b) - BUF_BEG_BYTE (b));
113 nchars += multibyte_chars_in_text (BUF_GAP_END_ADDR (b),
114 bytepos - BUF_GPT_BYTE (b));
116 else
117 nchars = multibyte_chars_in_text (BUF_BEG_ADDR (b),
118 bytepos - BUF_BEG_BYTE (b));
120 if (charpos - 1 != nchars)
121 abort ();
125 charpos_to_bytepos (charpos)
126 int charpos;
128 return buf_charpos_to_bytepos (current_buffer, charpos);
132 buf_charpos_to_bytepos (b, charpos)
133 struct buffer *b;
134 int charpos;
136 Lisp_Object tail;
137 int best_above, best_above_byte;
138 int best_below, best_below_byte;
140 if (charpos < BUF_BEG (b) || charpos > BUF_Z (b))
141 abort ();
143 best_above = BUF_Z (b);
144 best_above_byte = BUF_Z_BYTE (b);
146 /* If this buffer has as many characters as bytes,
147 each character must be one byte.
148 This takes care of the case where enable-multibyte-characters is nil. */
149 if (best_above == best_above_byte)
150 return charpos;
152 best_below = BEG;
153 best_below_byte = BEG_BYTE;
155 /* We find in best_above and best_above_byte
156 the closest known point above CHARPOS,
157 and in best_below and best_below_byte
158 the closest known point below CHARPOS,
160 If at any point we can tell that the space between those
161 two best approximations is all single-byte,
162 we interpolate the result immediately. */
164 CONSIDER (BUF_PT (b), BUF_PT_BYTE (b));
165 CONSIDER (BUF_GPT (b), BUF_GPT_BYTE (b));
166 CONSIDER (BUF_BEGV (b), BUF_BEGV_BYTE (b));
167 CONSIDER (BUF_ZV (b), BUF_ZV_BYTE (b));
169 if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff)
170 CONSIDER (cached_charpos, cached_bytepos);
172 tail = BUF_MARKERS (b);
173 while (! NILP (tail))
175 CONSIDER (XMARKER (tail)->charpos, XMARKER (tail)->bytepos);
177 /* If we are down to a range of 50 chars,
178 don't bother checking any other markers;
179 scan the intervening chars directly now. */
180 if (best_above - best_below < 50)
181 break;
183 tail = XMARKER (tail)->chain;
186 /* We get here if we did not exactly hit one of the known places.
187 We have one known above and one known below.
188 Scan, counting characters, from whichever one is closer. */
190 if (charpos - best_below < best_above - charpos)
192 int record = charpos - best_below > 5000;
194 while (best_below != charpos)
196 best_below++;
197 BUF_INC_POS (b, best_below_byte);
200 /* If this position is quite far from the nearest known position,
201 cache the correspondence by creating a marker here.
202 It will last until the next GC. */
203 if (record)
205 Lisp_Object marker, buffer;
206 marker = Fmake_marker ();
207 XSETBUFFER (buffer, b);
208 set_marker_both (marker, buffer, best_below, best_below_byte);
211 if (byte_debug_flag)
212 byte_char_debug_check (b, charpos, best_below_byte);
214 cached_buffer = b;
215 cached_modiff = BUF_MODIFF (b);
216 cached_charpos = best_below;
217 cached_bytepos = best_below_byte;
219 return best_below_byte;
221 else
223 int record = best_above - charpos > 5000;
225 while (best_above != charpos)
227 best_above--;
228 BUF_DEC_POS (b, best_above_byte);
231 /* If this position is quite far from the nearest known position,
232 cache the correspondence by creating a marker here.
233 It will last until the next GC. */
234 if (record)
236 Lisp_Object marker, buffer;
237 marker = Fmake_marker ();
238 XSETBUFFER (buffer, b);
239 set_marker_both (marker, buffer, best_above, best_above_byte);
242 if (byte_debug_flag)
243 byte_char_debug_check (b, charpos, best_above_byte);
245 cached_buffer = b;
246 cached_modiff = BUF_MODIFF (b);
247 cached_charpos = best_above;
248 cached_bytepos = best_above_byte;
250 return best_above_byte;
254 #undef CONSIDER
256 /* Used for debugging: recompute the bytepos corresponding to CHARPOS
257 in the simplest, most reliable way. */
260 verify_bytepos (charpos)
262 int below = 1;
263 int below_byte = 1;
265 while (below != charpos)
267 below++;
268 BUF_INC_POS (current_buffer, below_byte);
271 return below_byte;
274 /* bytepos_to_charpos returns the char position corresponding to BYTEPOS. */
276 /* This macro is a subroutine of bytepos_to_charpos.
277 It is used when BYTEPOS is actually the byte position. */
279 #define CONSIDER(BYTEPOS, CHARPOS) \
281 int this_bytepos = (BYTEPOS); \
282 int changed = 0; \
284 if (this_bytepos == bytepos) \
286 int value = (CHARPOS); \
287 if (byte_debug_flag) \
288 byte_char_debug_check (b, value, bytepos); \
289 return value; \
291 else if (this_bytepos > bytepos) \
293 if (this_bytepos < best_above_byte) \
295 best_above = (CHARPOS); \
296 best_above_byte = this_bytepos; \
297 changed = 1; \
300 else if (this_bytepos > best_below_byte) \
302 best_below = (CHARPOS); \
303 best_below_byte = this_bytepos; \
304 changed = 1; \
307 if (changed) \
309 if (best_above - best_below == best_above_byte - best_below_byte) \
311 int value = best_below + (bytepos - best_below_byte); \
312 if (byte_debug_flag) \
313 byte_char_debug_check (b, value, bytepos); \
314 return value; \
320 bytepos_to_charpos (bytepos)
321 int bytepos;
323 return buf_bytepos_to_charpos (current_buffer, bytepos);
327 buf_bytepos_to_charpos (b, bytepos)
328 struct buffer *b;
329 int bytepos;
331 Lisp_Object tail;
332 int best_above, best_above_byte;
333 int best_below, best_below_byte;
335 if (bytepos < BUF_BEG_BYTE (b) || bytepos > BUF_Z_BYTE (b))
336 abort ();
338 best_above = BUF_Z (b);
339 best_above_byte = BUF_Z_BYTE (b);
341 /* If this buffer has as many characters as bytes,
342 each character must be one byte.
343 This takes care of the case where enable-multibyte-characters is nil. */
344 if (best_above == best_above_byte)
345 return bytepos;
347 best_below = BEG;
348 best_below_byte = BEG_BYTE;
350 CONSIDER (BUF_PT_BYTE (b), BUF_PT (b));
351 CONSIDER (BUF_GPT_BYTE (b), BUF_GPT (b));
352 CONSIDER (BUF_BEGV_BYTE (b), BUF_BEGV (b));
353 CONSIDER (BUF_ZV_BYTE (b), BUF_ZV (b));
355 if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff)
356 CONSIDER (cached_bytepos, cached_charpos);
358 tail = BUF_MARKERS (b);
359 while (! NILP (tail))
361 CONSIDER (XMARKER (tail)->bytepos, XMARKER (tail)->charpos);
363 /* If we are down to a range of 50 chars,
364 don't bother checking any other markers;
365 scan the intervening chars directly now. */
366 if (best_above - best_below < 50)
367 break;
369 tail = XMARKER (tail)->chain;
372 /* We get here if we did not exactly hit one of the known places.
373 We have one known above and one known below.
374 Scan, counting characters, from whichever one is closer. */
376 if (bytepos - best_below_byte < best_above_byte - bytepos)
378 int record = bytepos - best_below_byte > 5000;
380 while (best_below_byte < bytepos)
382 best_below++;
383 BUF_INC_POS (b, best_below_byte);
386 /* If this position is quite far from the nearest known position,
387 cache the correspondence by creating a marker here.
388 It will last until the next GC.
389 But don't do it if BUF_MARKERS is nil;
390 that is a signal from Fset_buffer_multibyte. */
391 if (record && ! NILP (BUF_MARKERS (b)))
393 Lisp_Object marker, buffer;
394 marker = Fmake_marker ();
395 XSETBUFFER (buffer, b);
396 set_marker_both (marker, buffer, best_below, best_below_byte);
399 if (byte_debug_flag)
400 byte_char_debug_check (b, best_below, bytepos);
402 cached_buffer = b;
403 cached_modiff = BUF_MODIFF (b);
404 cached_charpos = best_below;
405 cached_bytepos = best_below_byte;
407 return best_below;
409 else
411 int record = best_above_byte - bytepos > 5000;
413 while (best_above_byte > bytepos)
415 best_above--;
416 BUF_DEC_POS (b, best_above_byte);
419 /* If this position is quite far from the nearest known position,
420 cache the correspondence by creating a marker here.
421 It will last until the next GC.
422 But don't do it if BUF_MARKERS is nil;
423 that is a signal from Fset_buffer_multibyte. */
424 if (record && ! NILP (BUF_MARKERS (b)))
426 Lisp_Object marker, buffer;
427 marker = Fmake_marker ();
428 XSETBUFFER (buffer, b);
429 set_marker_both (marker, buffer, best_above, best_above_byte);
432 if (byte_debug_flag)
433 byte_char_debug_check (b, best_above, bytepos);
435 cached_buffer = b;
436 cached_modiff = BUF_MODIFF (b);
437 cached_charpos = best_above;
438 cached_bytepos = best_above_byte;
440 return best_above;
444 #undef CONSIDER
446 /* Operations on markers. */
448 DEFUN ("marker-buffer", Fmarker_buffer, Smarker_buffer, 1, 1, 0,
449 doc: /* Return the buffer that MARKER points into, or nil if none.
450 Returns nil if MARKER points into a dead buffer. */)
451 (marker)
452 register Lisp_Object marker;
454 register Lisp_Object buf;
455 CHECK_MARKER (marker);
456 if (XMARKER (marker)->buffer)
458 XSETBUFFER (buf, XMARKER (marker)->buffer);
459 /* Return marker's buffer only if it is not dead. */
460 if (!NILP (XBUFFER (buf)->name))
461 return buf;
463 return Qnil;
466 DEFUN ("marker-position", Fmarker_position, Smarker_position, 1, 1, 0,
467 doc: /* Return the position MARKER points at, as a character number. */)
468 (marker)
469 Lisp_Object marker;
471 CHECK_MARKER (marker);
472 if (XMARKER (marker)->buffer)
473 return make_number (XMARKER (marker)->charpos);
475 return Qnil;
478 DEFUN ("set-marker", Fset_marker, Sset_marker, 2, 3, 0,
479 doc: /* Position MARKER before character number POSITION in BUFFER.
480 BUFFER defaults to the current buffer.
481 If POSITION is nil, makes marker point nowhere.
482 Then it no longer slows down editing in any buffer.
483 Returns MARKER. */)
484 (marker, position, buffer)
485 Lisp_Object marker, position, buffer;
487 register int charno, bytepos;
488 register struct buffer *b;
489 register struct Lisp_Marker *m;
491 CHECK_MARKER (marker);
492 /* If position is nil or a marker that points nowhere,
493 make this marker point nowhere. */
494 if (NILP (position)
495 || (MARKERP (position) && !XMARKER (position)->buffer))
497 unchain_marker (marker);
498 return marker;
501 if (NILP (buffer))
502 b = current_buffer;
503 else
505 CHECK_BUFFER (buffer);
506 b = XBUFFER (buffer);
507 /* If buffer is dead, set marker to point nowhere. */
508 if (EQ (b->name, Qnil))
510 unchain_marker (marker);
511 return marker;
515 m = XMARKER (marker);
517 /* Optimize the special case where we are copying the position
518 of an existing marker, and MARKER is already in the same buffer. */
519 if (MARKERP (position) && b == XMARKER (position)->buffer
520 && b == m->buffer)
522 m->bytepos = XMARKER (position)->bytepos;
523 m->charpos = XMARKER (position)->charpos;
524 return marker;
527 CHECK_NUMBER_COERCE_MARKER (position);
529 charno = XINT (position);
531 if (charno < BUF_BEG (b))
532 charno = BUF_BEG (b);
533 if (charno > BUF_Z (b))
534 charno = BUF_Z (b);
536 bytepos = buf_charpos_to_bytepos (b, charno);
538 /* Every character is at least one byte. */
539 if (charno > bytepos)
540 abort ();
542 m->bytepos = bytepos;
543 m->charpos = charno;
545 if (m->buffer != b)
547 unchain_marker (marker);
548 m->buffer = b;
549 m->chain = BUF_MARKERS (b);
550 BUF_MARKERS (b) = marker;
553 return marker;
556 /* This version of Fset_marker won't let the position
557 be outside the visible part. */
559 Lisp_Object
560 set_marker_restricted (marker, pos, buffer)
561 Lisp_Object marker, pos, buffer;
563 register int charno, bytepos;
564 register struct buffer *b;
565 register struct Lisp_Marker *m;
567 CHECK_MARKER (marker);
568 /* If position is nil or a marker that points nowhere,
569 make this marker point nowhere. */
570 if (NILP (pos)
571 || (MARKERP (pos) && !XMARKER (pos)->buffer))
573 unchain_marker (marker);
574 return marker;
577 if (NILP (buffer))
578 b = current_buffer;
579 else
581 CHECK_BUFFER (buffer);
582 b = XBUFFER (buffer);
583 /* If buffer is dead, set marker to point nowhere. */
584 if (EQ (b->name, Qnil))
586 unchain_marker (marker);
587 return marker;
591 m = XMARKER (marker);
593 /* Optimize the special case where we are copying the position
594 of an existing marker, and MARKER is already in the same buffer. */
595 if (MARKERP (pos) && b == XMARKER (pos)->buffer
596 && b == m->buffer)
598 m->bytepos = XMARKER (pos)->bytepos;
599 m->charpos = XMARKER (pos)->charpos;
600 return marker;
603 CHECK_NUMBER_COERCE_MARKER (pos);
605 charno = XINT (pos);
607 if (charno < BUF_BEGV (b))
608 charno = BUF_BEGV (b);
609 if (charno > BUF_ZV (b))
610 charno = BUF_ZV (b);
612 bytepos = buf_charpos_to_bytepos (b, charno);
614 /* Every character is at least one byte. */
615 if (charno > bytepos)
616 abort ();
618 m->bytepos = bytepos;
619 m->charpos = charno;
621 if (m->buffer != b)
623 unchain_marker (marker);
624 m->buffer = b;
625 m->chain = BUF_MARKERS (b);
626 BUF_MARKERS (b) = marker;
629 return marker;
632 /* Set the position of MARKER, specifying both the
633 character position and the corresponding byte position. */
635 Lisp_Object
636 set_marker_both (marker, buffer, charpos, bytepos)
637 Lisp_Object marker, buffer;
638 int charpos, bytepos;
640 register struct buffer *b;
641 register struct Lisp_Marker *m;
643 CHECK_MARKER (marker);
645 if (NILP (buffer))
646 b = current_buffer;
647 else
649 CHECK_BUFFER (buffer);
650 b = XBUFFER (buffer);
651 /* If buffer is dead, set marker to point nowhere. */
652 if (EQ (b->name, Qnil))
654 unchain_marker (marker);
655 return marker;
659 m = XMARKER (marker);
661 /* In a single-byte buffer, the two positions must be equal. */
662 if (BUF_Z (b) == BUF_Z_BYTE (b)
663 && charpos != bytepos)
664 abort ();
665 /* Every character is at least one byte. */
666 if (charpos > bytepos)
667 abort ();
669 m->bytepos = bytepos;
670 m->charpos = charpos;
672 if (m->buffer != b)
674 unchain_marker (marker);
675 m->buffer = b;
676 m->chain = BUF_MARKERS (b);
677 BUF_MARKERS (b) = marker;
680 return marker;
683 /* This version of set_marker_both won't let the position
684 be outside the visible part. */
686 Lisp_Object
687 set_marker_restricted_both (marker, buffer, charpos, bytepos)
688 Lisp_Object marker, buffer;
689 int charpos, bytepos;
691 register struct buffer *b;
692 register struct Lisp_Marker *m;
694 CHECK_MARKER (marker);
696 if (NILP (buffer))
697 b = current_buffer;
698 else
700 CHECK_BUFFER (buffer);
701 b = XBUFFER (buffer);
702 /* If buffer is dead, set marker to point nowhere. */
703 if (EQ (b->name, Qnil))
705 unchain_marker (marker);
706 return marker;
710 m = XMARKER (marker);
712 if (charpos < BUF_BEGV (b))
713 charpos = BUF_BEGV (b);
714 if (charpos > BUF_ZV (b))
715 charpos = BUF_ZV (b);
716 if (bytepos < BUF_BEGV_BYTE (b))
717 bytepos = BUF_BEGV_BYTE (b);
718 if (bytepos > BUF_ZV_BYTE (b))
719 bytepos = BUF_ZV_BYTE (b);
721 /* In a single-byte buffer, the two positions must be equal. */
722 if (BUF_Z (b) == BUF_Z_BYTE (b)
723 && charpos != bytepos)
724 abort ();
725 /* Every character is at least one byte. */
726 if (charpos > bytepos)
727 abort ();
729 m->bytepos = bytepos;
730 m->charpos = charpos;
732 if (m->buffer != b)
734 unchain_marker (marker);
735 m->buffer = b;
736 m->chain = BUF_MARKERS (b);
737 BUF_MARKERS (b) = marker;
740 return marker;
743 /* Remove MARKER from the chain of whatever buffer it is in.
744 Leave it "in no buffer".
746 This is called during garbage collection,
747 so we must be careful to ignore and preserve mark bits,
748 including those in chain fields of markers. */
750 void
751 unchain_marker (marker)
752 register Lisp_Object marker;
754 register Lisp_Object tail, prev, next;
755 register EMACS_INT omark;
756 register struct buffer *b;
758 b = XMARKER (marker)->buffer;
759 if (b == 0)
760 return;
762 if (EQ (b->name, Qnil))
763 abort ();
765 XMARKER (marker)->buffer = 0;
767 tail = BUF_MARKERS (b);
768 prev = Qnil;
769 while (! GC_NILP (tail))
771 next = XMARKER (tail)->chain;
772 XUNMARK (next);
774 if (XMARKER (marker) == XMARKER (tail))
776 if (NILP (prev))
778 BUF_MARKERS (b) = next;
779 /* Deleting first marker from the buffer's chain. Crash
780 if new first marker in chain does not say it belongs
781 to the same buffer, or at least that they have the same
782 base buffer. */
783 if (!NILP (next) && b->text != XMARKER (next)->buffer->text)
784 abort ();
786 else
788 omark = XMARKBIT (XMARKER (prev)->chain);
789 XMARKER (prev)->chain = next;
790 XSETMARKBIT (XMARKER (prev)->chain, omark);
792 /* We have removed the marker from the chain;
793 no need to scan the rest of the chain. */
794 return;
796 else
797 prev = tail;
798 tail = next;
801 /* Marker was not in its chain. */
802 abort ();
805 /* Return the char position of marker MARKER, as a C integer. */
808 marker_position (marker)
809 Lisp_Object marker;
811 register struct Lisp_Marker *m = XMARKER (marker);
812 register struct buffer *buf = m->buffer;
814 if (!buf)
815 error ("Marker does not point anywhere");
817 return m->charpos;
820 /* Return the byte position of marker MARKER, as a C integer. */
823 marker_byte_position (marker)
824 Lisp_Object marker;
826 register struct Lisp_Marker *m = XMARKER (marker);
827 register struct buffer *buf = m->buffer;
828 register int i = m->bytepos;
830 if (!buf)
831 error ("Marker does not point anywhere");
833 if (i < BUF_BEG_BYTE (buf) || i > BUF_Z_BYTE (buf))
834 abort ();
836 return i;
839 DEFUN ("copy-marker", Fcopy_marker, Scopy_marker, 1, 2, 0,
840 doc: /* Return a new marker pointing at the same place as MARKER.
841 If argument is a number, makes a new marker pointing
842 at that position in the current buffer.
843 The optional argument TYPE specifies the insertion type of the new marker;
844 see `marker-insertion-type'. */)
845 (marker, type)
846 register Lisp_Object marker, type;
848 register Lisp_Object new;
850 if (! (INTEGERP (marker) || MARKERP (marker)))
851 marker = wrong_type_argument (Qinteger_or_marker_p, marker);
853 new = Fmake_marker ();
854 Fset_marker (new, marker,
855 (MARKERP (marker) ? Fmarker_buffer (marker) : Qnil));
856 XMARKER (new)->insertion_type = !NILP (type);
857 return new;
860 DEFUN ("marker-insertion-type", Fmarker_insertion_type,
861 Smarker_insertion_type, 1, 1, 0,
862 doc: /* Return insertion type of MARKER: t if it stays after inserted text.
863 nil means the marker stays before text inserted there. */)
864 (marker)
865 register Lisp_Object marker;
867 CHECK_MARKER (marker);
868 return XMARKER (marker)->insertion_type ? Qt : Qnil;
871 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type,
872 Sset_marker_insertion_type, 2, 2, 0,
873 doc: /* Set the insertion-type of MARKER to TYPE.
874 If TYPE is t, it means the marker advances when you insert text at it.
875 If TYPE is nil, it means the marker stays behind when you insert text at it. */)
876 (marker, type)
877 Lisp_Object marker, type;
879 CHECK_MARKER (marker);
881 XMARKER (marker)->insertion_type = ! NILP (type);
882 return type;
885 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at, Sbuffer_has_markers_at,
886 1, 1, 0,
887 doc: /* Return t if there are markers pointing at POSITION in the current buffer. */)
888 (position)
889 Lisp_Object position;
891 register Lisp_Object tail;
892 register int charno;
894 charno = XINT (position);
896 if (charno < BEG)
897 charno = BEG;
898 if (charno > Z)
899 charno = Z;
901 for (tail = BUF_MARKERS (current_buffer);
902 !NILP (tail);
903 tail = XMARKER (tail)->chain)
904 if (XMARKER (tail)->charpos == charno)
905 return Qt;
907 return Qnil;
910 /* For debugging -- count the markers in buffer BUF. */
913 count_markers (buf)
914 struct buffer *buf;
916 int total = 0;
917 Lisp_Object tail;
919 for (tail = BUF_MARKERS (buf);
920 !NILP (tail);
921 tail = XMARKER (tail)->chain)
922 total++;
924 return total;
927 void
928 syms_of_marker ()
930 defsubr (&Smarker_position);
931 defsubr (&Smarker_buffer);
932 defsubr (&Sset_marker);
933 defsubr (&Scopy_marker);
934 defsubr (&Smarker_insertion_type);
935 defsubr (&Sset_marker_insertion_type);
936 defsubr (&Sbuffer_has_markers_at);
938 DEFVAR_BOOL ("byte-debug-flag", &byte_debug_flag,
939 doc: /* Non-nil enables debugging checks in byte/char position conversions. */);
940 byte_debug_flag = 0;