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)
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. */
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 /* Nonzero means enable debugging checks on byte/char correspondences. */
37 static int byte_debug_flag
;
39 clear_charpos_cache (b
)
42 if (cached_buffer
== b
)
46 /* Converting between character positions and byte positions. */
48 /* There are several places in the buffer where we know
49 the corrspondence: BEG, BEGV, PT, GPT, ZV and Z,
50 and everywhere there is a marker. So we find the one of these places
51 that is closest to the specified position, and scan from there. */
53 /* charpos_to_bytepos returns the byte position corresponding to CHARPOS. */
55 /* This macro is a subroutine of charpos_to_bytepos.
56 Note that it is desirable that BYTEPOS is not evaluated
57 except when we really want its value. */
59 #define CONSIDER(CHARPOS, BYTEPOS) \
61 int this_charpos = (CHARPOS); \
64 if (this_charpos == charpos) \
66 int value = (BYTEPOS); \
67 if (byte_debug_flag) \
68 byte_char_debug_check (b, charpos, value); \
71 else if (this_charpos > charpos) \
73 if (this_charpos < best_above) \
75 best_above = this_charpos; \
76 best_above_byte = (BYTEPOS); \
80 else if (this_charpos > best_below) \
82 best_below = this_charpos; \
83 best_below_byte = (BYTEPOS); \
89 if (best_above - best_below == best_above_byte - best_below_byte) \
91 int value = best_below_byte + (charpos - best_below); \
92 if (byte_debug_flag) \
93 byte_char_debug_check (b, charpos, value); \
100 byte_char_debug_check (b
, charpos
, bytepos
)
102 int charpos
, bytepos
;
106 if (bytepos
> BUF_GPT_BYTE (b
))
108 nchars
= multibyte_chars_in_text (BUF_BEG_ADDR (b
),
109 BUF_GPT_BYTE (b
) - BUF_BEG_BYTE (b
));
110 nchars
+= multibyte_chars_in_text (BUF_GAP_END_ADDR (b
),
111 bytepos
- BUF_GPT_BYTE (b
));
114 nchars
= multibyte_chars_in_text (BUF_BEG_ADDR (b
),
115 bytepos
- BUF_BEG_BYTE (b
));
117 if (charpos
- 1 != nchars
)
122 charpos_to_bytepos (charpos
)
125 return buf_charpos_to_bytepos (current_buffer
, charpos
);
129 buf_charpos_to_bytepos (b
, charpos
)
134 int best_above
, best_above_byte
;
135 int 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
)
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 tail
= BUF_MARKERS (b
);
170 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
172 CONSIDER (XMARKER (tail
)->charpos
, XMARKER (tail
)->bytepos
);
174 /* If we are down to a range of 50 chars,
175 don't bother checking any other markers;
176 scan the intervening chars directly now. */
177 if (best_above
- best_below
< 50)
180 tail
= XMARKER (tail
)->chain
;
183 /* We get here if we did not exactly hit one of the known places.
184 We have one known above and one known below.
185 Scan, counting characters, from whichever one is closer. */
187 if (charpos
- best_below
< best_above
- charpos
)
189 int record
= charpos
- best_below
> 5000;
191 while (best_below
!= charpos
)
194 BUF_INC_POS (b
, best_below_byte
);
197 /* If this position is quite far from the nearest known position,
198 cache the correspondence by creating a marker here.
199 It will last until the next GC. */
203 marker
= Fmake_marker ();
204 set_marker_both (marker
, Qnil
, best_below
, best_below_byte
);
208 byte_char_debug_check (b
, charpos
, best_below_byte
);
211 cached_modiff
= BUF_MODIFF (b
);
212 cached_charpos
= best_below
;
213 cached_bytepos
= best_below_byte
;
215 return best_below_byte
;
219 int record
= best_above
- charpos
> 5000;
221 while (best_above
!= charpos
)
224 BUF_DEC_POS (b
, best_above_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. */
233 marker
= Fmake_marker ();
234 set_marker_both (marker
, Qnil
, best_above
, best_above_byte
);
238 byte_char_debug_check (b
, charpos
, best_above_byte
);
241 cached_modiff
= BUF_MODIFF (b
);
242 cached_charpos
= best_above
;
243 cached_bytepos
= best_above_byte
;
245 return best_above_byte
;
251 /* bytepos_to_charpos returns the char position corresponding to BYTEPOS. */
253 /* This macro is a subroutine of bytepos_to_charpos.
254 It is used when BYTEPOS is actually the byte position. */
256 #define CONSIDER(BYTEPOS, CHARPOS) \
258 int this_bytepos = (BYTEPOS); \
261 if (this_bytepos == bytepos) \
263 int value = (CHARPOS); \
264 if (byte_debug_flag) \
265 byte_char_debug_check (b, value, bytepos); \
268 else if (this_bytepos > bytepos) \
270 if (this_bytepos < best_above_byte) \
272 best_above = (CHARPOS); \
273 best_above_byte = this_bytepos; \
277 else if (this_bytepos > best_below_byte) \
279 best_below = (CHARPOS); \
280 best_below_byte = this_bytepos; \
286 if (best_above - best_below == best_above_byte - best_below_byte) \
288 int value = best_below + (bytepos - best_below_byte); \
289 if (byte_debug_flag) \
290 byte_char_debug_check (b, value, bytepos); \
297 bytepos_to_charpos (bytepos
)
300 return buf_bytepos_to_charpos (current_buffer
, bytepos
);
304 buf_bytepos_to_charpos (b
, bytepos
)
309 int best_above
, best_above_byte
;
310 int 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
)
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 tail
= BUF_MARKERS (b
);
336 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
338 CONSIDER (XMARKER (tail
)->bytepos
, XMARKER (tail
)->charpos
);
340 /* If we are down to a range of 50 chars,
341 don't bother checking any other markers;
342 scan the intervening chars directly now. */
343 if (best_above
- best_below
< 50)
346 tail
= XMARKER (tail
)->chain
;
349 /* We get here if we did not exactly hit one of the known places.
350 We have one known above and one known below.
351 Scan, counting characters, from whichever one is closer. */
353 if (bytepos
- best_below_byte
< best_above_byte
- bytepos
)
355 int record
= best_above_byte
- bytepos
> 5000;
357 while (best_below_byte
< bytepos
)
360 BUF_INC_POS (b
, best_below_byte
);
363 /* If this position is quite far from the nearest known position,
364 cache the correspondence by creating a marker here.
365 It will last until the next GC. */
369 marker
= Fmake_marker ();
370 set_marker_both (marker
, Qnil
, best_below
, best_below_byte
);
374 byte_char_debug_check (b
, best_below
, bytepos
);
377 cached_modiff
= BUF_MODIFF (b
);
378 cached_charpos
= best_below
;
379 cached_bytepos
= best_below_byte
;
385 int record
= best_above_byte
- bytepos
> 5000;
387 while (best_above_byte
> bytepos
)
390 BUF_DEC_POS (b
, best_above_byte
);
393 /* If this position is quite far from the nearest known position,
394 cache the correspondence by creating a marker here.
395 It will last until the next GC. */
399 marker
= Fmake_marker ();
400 set_marker_both (marker
, Qnil
, best_above
, best_above_byte
);
404 byte_char_debug_check (b
, best_above
, bytepos
);
407 cached_modiff
= BUF_MODIFF (b
);
408 cached_charpos
= best_above
;
409 cached_bytepos
= best_above_byte
;
417 /* Operations on markers. */
419 DEFUN ("marker-buffer", Fmarker_buffer
, Smarker_buffer
, 1, 1, 0,
420 "Return the buffer that MARKER points into, or nil if none.\n\
421 Returns nil if MARKER points into a dead buffer.")
423 register Lisp_Object marker
;
425 register Lisp_Object buf
;
426 CHECK_MARKER (marker
, 0);
427 if (XMARKER (marker
)->buffer
)
429 XSETBUFFER (buf
, XMARKER (marker
)->buffer
);
430 /* Return marker's buffer only if it is not dead. */
431 if (!NILP (XBUFFER (buf
)->name
))
437 DEFUN ("marker-position", Fmarker_position
, Smarker_position
, 1, 1, 0,
438 "Return the position MARKER points at, as a character number.")
442 register Lisp_Object pos
;
444 register struct buffer
*buf
;
446 CHECK_MARKER (marker
, 0);
447 if (XMARKER (marker
)->buffer
)
448 return make_number (XMARKER (marker
)->charpos
);
453 DEFUN ("set-marker", Fset_marker
, Sset_marker
, 2, 3, 0,
454 "Position MARKER before character number POSITION in BUFFER.\n\
455 BUFFER defaults to the current buffer.\n\
456 If POSITION is nil, makes marker point nowhere.\n\
457 Then it no longer slows down editing in any buffer.\n\
459 (marker
, position
, buffer
)
460 Lisp_Object marker
, position
, buffer
;
462 register int charno
, bytepos
;
463 register struct buffer
*b
;
464 register struct Lisp_Marker
*m
;
466 CHECK_MARKER (marker
, 0);
467 /* If position is nil or a marker that points nowhere,
468 make this marker point nowhere. */
470 || (MARKERP (position
) && !XMARKER (position
)->buffer
))
472 unchain_marker (marker
);
480 CHECK_BUFFER (buffer
, 1);
481 b
= XBUFFER (buffer
);
482 /* If buffer is dead, set marker to point nowhere. */
483 if (EQ (b
->name
, Qnil
))
485 unchain_marker (marker
);
490 m
= XMARKER (marker
);
492 /* Optimize the special case where we are copying the position
493 of an existing marker, and MARKER is already in the same buffer. */
494 if (MARKERP (position
) && b
== XMARKER (position
)->buffer
497 m
->bytepos
= XMARKER (position
)->bytepos
;
498 m
->charpos
= XMARKER (position
)->charpos
;
502 CHECK_NUMBER_COERCE_MARKER (position
, 1);
504 charno
= XINT (position
);
506 if (charno
< BUF_BEG (b
))
507 charno
= BUF_BEG (b
);
508 if (charno
> BUF_Z (b
))
511 bytepos
= buf_charpos_to_bytepos (b
, charno
);
513 /* Every character is at least one byte. */
514 if (charno
> bytepos
)
517 m
->bytepos
= bytepos
;
522 unchain_marker (marker
);
524 m
->chain
= BUF_MARKERS (b
);
525 BUF_MARKERS (b
) = marker
;
531 /* This version of Fset_marker won't let the position
532 be outside the visible part. */
535 set_marker_restricted (marker
, pos
, buffer
)
536 Lisp_Object marker
, pos
, buffer
;
538 register int charno
, bytepos
;
539 register struct buffer
*b
;
540 register struct Lisp_Marker
*m
;
542 CHECK_MARKER (marker
, 0);
543 /* If position is nil or a marker that points nowhere,
544 make this marker point nowhere. */
546 || (MARKERP (pos
) && !XMARKER (pos
)->buffer
))
548 unchain_marker (marker
);
556 CHECK_BUFFER (buffer
, 1);
557 b
= XBUFFER (buffer
);
558 /* If buffer is dead, set marker to point nowhere. */
559 if (EQ (b
->name
, Qnil
))
561 unchain_marker (marker
);
566 m
= XMARKER (marker
);
568 /* Optimize the special case where we are copying the position
569 of an existing marker, and MARKER is already in the same buffer. */
570 if (MARKERP (pos
) && b
== XMARKER (pos
)->buffer
573 m
->bytepos
= XMARKER (pos
)->bytepos
;
574 m
->charpos
= XMARKER (pos
)->charpos
;
578 CHECK_NUMBER_COERCE_MARKER (pos
, 1);
582 if (charno
< BUF_BEGV (b
))
583 charno
= BUF_BEGV (b
);
584 if (charno
> BUF_ZV (b
))
587 bytepos
= buf_charpos_to_bytepos (b
, charno
);
589 /* Every character is at least one byte. */
590 if (charno
> bytepos
)
593 m
->bytepos
= bytepos
;
598 unchain_marker (marker
);
600 m
->chain
= BUF_MARKERS (b
);
601 BUF_MARKERS (b
) = marker
;
607 /* Set the position of MARKER, specifying both the
608 character position and the corresponding byte position. */
611 set_marker_both (marker
, buffer
, charpos
, bytepos
)
612 Lisp_Object marker
, buffer
;
613 int charpos
, bytepos
;
615 register struct buffer
*b
;
616 register struct Lisp_Marker
*m
;
618 CHECK_MARKER (marker
, 0);
619 /* If position is nil or a marker that points nowhere,
620 make this marker point nowhere. */
622 || (MARKERP (charpos
) && !XMARKER (charpos
)->buffer
))
624 unchain_marker (marker
);
628 CHECK_NUMBER_COERCE_MARKER (charpos
, 1);
633 CHECK_BUFFER (buffer
, 1);
634 b
= XBUFFER (buffer
);
635 /* If buffer is dead, set marker to point nowhere. */
636 if (EQ (b
->name
, Qnil
))
638 unchain_marker (marker
);
643 m
= XMARKER (marker
);
645 /* In a single-byte buffer, the two positions must be equal. */
646 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
647 && charpos
!= bytepos
)
649 /* Every character is at least one byte. */
650 if (charpos
> bytepos
)
653 m
->bytepos
= bytepos
;
654 m
->charpos
= charpos
;
658 unchain_marker (marker
);
660 m
->chain
= BUF_MARKERS (b
);
661 BUF_MARKERS (b
) = marker
;
667 /* This version of set_marker_both won't let the position
668 be outside the visible part. */
671 set_marker_restricted_both (marker
, buffer
, charpos
, bytepos
)
672 Lisp_Object marker
, buffer
;
673 int charpos
, bytepos
;
675 register struct buffer
*b
;
676 register struct Lisp_Marker
*m
;
678 CHECK_MARKER (marker
, 0);
684 CHECK_BUFFER (buffer
, 1);
685 b
= XBUFFER (buffer
);
686 /* If buffer is dead, set marker to point nowhere. */
687 if (EQ (b
->name
, Qnil
))
689 unchain_marker (marker
);
694 m
= XMARKER (marker
);
696 if (charpos
< BUF_BEGV (b
))
697 charpos
= BUF_BEGV (b
);
698 if (charpos
> BUF_ZV (b
))
699 charpos
= BUF_ZV (b
);
700 if (bytepos
< BUF_BEGV_BYTE (b
))
701 bytepos
= BUF_BEGV_BYTE (b
);
702 if (bytepos
> BUF_ZV_BYTE (b
))
703 bytepos
= BUF_ZV_BYTE (b
);
705 /* In a single-byte buffer, the two positions must be equal. */
706 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
707 && charpos
!= bytepos
)
709 /* Every character is at least one byte. */
710 if (charpos
> bytepos
)
713 m
->bytepos
= bytepos
;
714 m
->charpos
= charpos
;
718 unchain_marker (marker
);
720 m
->chain
= BUF_MARKERS (b
);
721 BUF_MARKERS (b
) = marker
;
727 /* This is called during garbage collection,
728 so we must be careful to ignore and preserve mark bits,
729 including those in chain fields of markers. */
732 unchain_marker (marker
)
733 register Lisp_Object marker
;
735 register Lisp_Object tail
, prev
, next
;
736 register EMACS_INT omark
;
737 register struct buffer
*b
;
739 b
= XMARKER (marker
)->buffer
;
743 if (EQ (b
->name
, Qnil
))
746 tail
= BUF_MARKERS (b
);
748 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
750 next
= XMARKER (tail
)->chain
;
753 if (XMARKER (marker
) == XMARKER (tail
))
757 BUF_MARKERS (b
) = next
;
758 /* Deleting first marker from the buffer's chain. Crash
759 if new first marker in chain does not say it belongs
760 to the same buffer, or at least that they have the same
762 if (!NILP (next
) && b
->text
!= XMARKER (next
)->buffer
->text
)
767 omark
= XMARKBIT (XMARKER (prev
)->chain
);
768 XMARKER (prev
)->chain
= next
;
769 XSETMARKBIT (XMARKER (prev
)->chain
, omark
);
777 XMARKER (marker
)->buffer
= 0;
780 /* Return the char position of marker MARKER, as a C integer. */
783 marker_position (marker
)
786 register struct Lisp_Marker
*m
= XMARKER (marker
);
787 register struct buffer
*buf
= m
->buffer
;
790 error ("Marker does not point anywhere");
795 /* Return the byte position of marker MARKER, as a C integer. */
798 marker_byte_position (marker
)
801 register struct Lisp_Marker
*m
= XMARKER (marker
);
802 register struct buffer
*buf
= m
->buffer
;
803 register int i
= m
->bytepos
;
806 error ("Marker does not point anywhere");
808 if (i
< BUF_BEG_BYTE (buf
) || i
> BUF_Z_BYTE (buf
))
814 DEFUN ("copy-marker", Fcopy_marker
, Scopy_marker
, 1, 2, 0,
815 "Return a new marker pointing at the same place as MARKER.\n\
816 If argument is a number, makes a new marker pointing\n\
817 at that position in the current buffer.\n\
818 The optional argument TYPE specifies the insertion type of the new marker;\n\
819 see `marker-insertion-type'.")
821 register Lisp_Object marker
, type
;
823 register Lisp_Object
new;
825 if (INTEGERP (marker
) || MARKERP (marker
))
827 new = Fmake_marker ();
828 Fset_marker (new, marker
,
829 (MARKERP (marker
) ? Fmarker_buffer (marker
) : Qnil
));
830 XMARKER (new)->insertion_type
= !NILP (type
);
834 marker
= wrong_type_argument (Qinteger_or_marker_p
, marker
);
837 DEFUN ("marker-insertion-type", Fmarker_insertion_type
,
838 Smarker_insertion_type
, 1, 1, 0,
839 "Return insertion type of MARKER: t if it stays after inserted text.\n\
840 nil means the marker stays before text inserted there.")
842 register Lisp_Object marker
;
844 register Lisp_Object buf
;
845 CHECK_MARKER (marker
, 0);
846 return XMARKER (marker
)->insertion_type
? Qt
: Qnil
;
849 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type
,
850 Sset_marker_insertion_type
, 2, 2, 0,
851 "Set the insertion-type of MARKER to TYPE.\n\
852 If TYPE is t, it means the marker advances when you insert text at it.\n\
853 If TYPE is nil, it means the marker stays behind when you insert text at it.")
855 Lisp_Object marker
, type
;
857 CHECK_MARKER (marker
, 0);
859 XMARKER (marker
)->insertion_type
= ! NILP (type
);
863 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at
, Sbuffer_has_markers_at
,
865 "Return t if there are markers pointing at POSITION in the current buffer.")
867 Lisp_Object position
;
869 register Lisp_Object tail
;
872 charno
= XINT (position
);
879 for (tail
= BUF_MARKERS (current_buffer
);
881 tail
= XMARKER (tail
)->chain
)
882 if (XMARKER (tail
)->charpos
== charno
)
891 defsubr (&Smarker_position
);
892 defsubr (&Smarker_buffer
);
893 defsubr (&Sset_marker
);
894 defsubr (&Scopy_marker
);
895 defsubr (&Smarker_insertion_type
);
896 defsubr (&Sset_marker_insertion_type
);
897 defsubr (&Sbuffer_has_markers_at
);
899 DEFVAR_BOOL ("byte-debug-flag", &byte_debug_flag
,
900 "Non-nil enables debugging checks in byte/char position conversions.");