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
;
40 clear_charpos_cache (b
)
43 if (cached_buffer
== b
)
47 /* Converting between character positions and byte positions. */
49 /* There are several places in the buffer where we know
50 the corrspondence: BEG, BEGV, PT, GPT, ZV and Z,
51 and everywhere there is a marker. So we find the one of these places
52 that is closest to the specified position, and scan from there. */
54 /* charpos_to_bytepos returns the byte position corresponding to CHARPOS. */
56 /* This macro is a subroutine of charpos_to_bytepos.
57 Note that it is desirable that BYTEPOS is not evaluated
58 except when we really want its value. */
60 #define CONSIDER(CHARPOS, BYTEPOS) \
62 int this_charpos = (CHARPOS); \
65 if (this_charpos == charpos) \
67 int value = (BYTEPOS); \
68 if (byte_debug_flag) \
69 byte_char_debug_check (b, charpos, value); \
72 else if (this_charpos > charpos) \
74 if (this_charpos < best_above) \
76 best_above = this_charpos; \
77 best_above_byte = (BYTEPOS); \
81 else if (this_charpos > best_below) \
83 best_below = this_charpos; \
84 best_below_byte = (BYTEPOS); \
90 if (best_above - best_below == best_above_byte - best_below_byte) \
92 int value = best_below_byte + (charpos - best_below); \
93 if (byte_debug_flag) \
94 byte_char_debug_check (b, charpos, value); \
101 byte_char_debug_check (b
, charpos
, bytepos
)
103 int charpos
, bytepos
;
107 if (bytepos
> BUF_GPT_BYTE (b
))
109 nchars
= multibyte_chars_in_text (BUF_BEG_ADDR (b
),
110 BUF_GPT_BYTE (b
) - BUF_BEG_BYTE (b
));
111 nchars
+= multibyte_chars_in_text (BUF_GAP_END_ADDR (b
),
112 bytepos
- BUF_GPT_BYTE (b
));
115 nchars
= multibyte_chars_in_text (BUF_BEG_ADDR (b
),
116 bytepos
- BUF_BEG_BYTE (b
));
118 if (charpos
- 1 != nchars
)
123 charpos_to_bytepos (charpos
)
126 return buf_charpos_to_bytepos (current_buffer
, charpos
);
130 buf_charpos_to_bytepos (b
, charpos
)
135 int best_above
, best_above_byte
;
136 int best_below
, best_below_byte
;
138 if (charpos
< BUF_BEG (b
) || charpos
> BUF_Z (b
))
141 best_above
= BUF_Z (b
);
142 best_above_byte
= BUF_Z_BYTE (b
);
144 /* If this buffer has as many characters as bytes,
145 each character must be one byte.
146 This takes care of the case where enable-multibyte-characters is nil. */
147 if (best_above
== best_above_byte
)
153 /* We find in best_above and best_above_byte
154 the closest known point above CHARPOS,
155 and in best_below and best_below_byte
156 the closest known point below CHARPOS,
158 If at any point we can tell that the space between those
159 two best approximations is all single-byte,
160 we interpolate the result immediately. */
162 CONSIDER (BUF_PT (b
), BUF_PT_BYTE (b
));
163 CONSIDER (BUF_GPT (b
), BUF_GPT_BYTE (b
));
164 CONSIDER (BUF_BEGV (b
), BUF_BEGV_BYTE (b
));
165 CONSIDER (BUF_ZV (b
), BUF_ZV_BYTE (b
));
167 if (b
== cached_buffer
&& BUF_MODIFF (b
) == cached_modiff
)
168 CONSIDER (cached_charpos
, cached_bytepos
);
170 tail
= BUF_MARKERS (b
);
171 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
173 CONSIDER (XMARKER (tail
)->charpos
, XMARKER (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)
181 tail
= XMARKER (tail
)->chain
;
184 /* We get here if we did not exactly hit one of the known places.
185 We have one known above and one known below.
186 Scan, counting characters, from whichever one is closer. */
188 if (charpos
- best_below
< best_above
- charpos
)
190 int record
= charpos
- best_below
> 5000;
192 while (best_below
!= charpos
)
195 BUF_INC_POS (b
, best_below_byte
);
198 /* If this position is quite far from the nearest known position,
199 cache the correspondence by creating a marker here.
200 It will last until the next GC. */
203 Lisp_Object marker
, buffer
;
204 marker
= Fmake_marker ();
205 XSETBUFFER (buffer
, b
);
206 set_marker_both (marker
, buffer
, best_below
, best_below_byte
);
210 byte_char_debug_check (b
, charpos
, best_below_byte
);
213 cached_modiff
= BUF_MODIFF (b
);
214 cached_charpos
= best_below
;
215 cached_bytepos
= best_below_byte
;
217 return best_below_byte
;
221 int record
= best_above
- charpos
> 5000;
223 while (best_above
!= charpos
)
226 BUF_DEC_POS (b
, best_above_byte
);
229 /* If this position is quite far from the nearest known position,
230 cache the correspondence by creating a marker here.
231 It will last until the next GC. */
234 Lisp_Object marker
, buffer
;
235 marker
= Fmake_marker ();
236 XSETBUFFER (buffer
, b
);
237 set_marker_both (marker
, buffer
, best_above
, best_above_byte
);
241 byte_char_debug_check (b
, charpos
, best_above_byte
);
244 cached_modiff
= BUF_MODIFF (b
);
245 cached_charpos
= best_above
;
246 cached_bytepos
= best_above_byte
;
248 return best_above_byte
;
254 /* bytepos_to_charpos returns the char position corresponding to BYTEPOS. */
256 /* This macro is a subroutine of bytepos_to_charpos.
257 It is used when BYTEPOS is actually the byte position. */
259 #define CONSIDER(BYTEPOS, CHARPOS) \
261 int this_bytepos = (BYTEPOS); \
264 if (this_bytepos == bytepos) \
266 int value = (CHARPOS); \
267 if (byte_debug_flag) \
268 byte_char_debug_check (b, value, bytepos); \
271 else if (this_bytepos > bytepos) \
273 if (this_bytepos < best_above_byte) \
275 best_above = (CHARPOS); \
276 best_above_byte = this_bytepos; \
280 else if (this_bytepos > best_below_byte) \
282 best_below = (CHARPOS); \
283 best_below_byte = this_bytepos; \
289 if (best_above - best_below == best_above_byte - best_below_byte) \
291 int value = best_below + (bytepos - best_below_byte); \
292 if (byte_debug_flag) \
293 byte_char_debug_check (b, value, bytepos); \
300 bytepos_to_charpos (bytepos
)
303 return buf_bytepos_to_charpos (current_buffer
, bytepos
);
307 buf_bytepos_to_charpos (b
, bytepos
)
312 int best_above
, best_above_byte
;
313 int best_below
, best_below_byte
;
315 if (bytepos
< BUF_BEG_BYTE (b
) || bytepos
> BUF_Z_BYTE (b
))
318 best_above
= BUF_Z (b
);
319 best_above_byte
= BUF_Z_BYTE (b
);
321 /* If this buffer has as many characters as bytes,
322 each character must be one byte.
323 This takes care of the case where enable-multibyte-characters is nil. */
324 if (best_above
== best_above_byte
)
330 CONSIDER (BUF_PT_BYTE (b
), BUF_PT (b
));
331 CONSIDER (BUF_GPT_BYTE (b
), BUF_GPT (b
));
332 CONSIDER (BUF_BEGV_BYTE (b
), BUF_BEGV (b
));
333 CONSIDER (BUF_ZV_BYTE (b
), BUF_ZV (b
));
335 if (b
== cached_buffer
&& BUF_MODIFF (b
) == cached_modiff
)
336 CONSIDER (cached_bytepos
, cached_charpos
);
338 tail
= BUF_MARKERS (b
);
339 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
341 CONSIDER (XMARKER (tail
)->bytepos
, XMARKER (tail
)->charpos
);
343 /* If we are down to a range of 50 chars,
344 don't bother checking any other markers;
345 scan the intervening chars directly now. */
346 if (best_above
- best_below
< 50)
349 tail
= XMARKER (tail
)->chain
;
352 /* We get here if we did not exactly hit one of the known places.
353 We have one known above and one known below.
354 Scan, counting characters, from whichever one is closer. */
356 if (bytepos
- best_below_byte
< best_above_byte
- bytepos
)
358 int record
= bytepos
- best_below_byte
> 5000;
360 while (best_below_byte
< bytepos
)
363 BUF_INC_POS (b
, best_below_byte
);
366 /* If this position is quite far from the nearest known position,
367 cache the correspondence by creating a marker here.
368 It will last until the next GC.
369 But don't do it if BUF_MARKERS is nil;
370 that is a signal from Fset_buffer_multibyte. */
371 if (record
&& ! NILP (BUF_MARKERS (b
)))
373 Lisp_Object marker
, buffer
;
374 marker
= Fmake_marker ();
375 XSETBUFFER (buffer
, b
);
376 set_marker_both (marker
, buffer
, best_below
, best_below_byte
);
380 byte_char_debug_check (b
, best_below
, bytepos
);
383 cached_modiff
= BUF_MODIFF (b
);
384 cached_charpos
= best_below
;
385 cached_bytepos
= best_below_byte
;
391 int record
= best_above_byte
- bytepos
> 5000;
393 while (best_above_byte
> bytepos
)
396 BUF_DEC_POS (b
, best_above_byte
);
399 /* If this position is quite far from the nearest known position,
400 cache the correspondence by creating a marker here.
401 It will last until the next GC.
402 But don't do it if BUF_MARKERS is nil;
403 that is a signal from Fset_buffer_multibyte. */
404 if (record
&& ! NILP (BUF_MARKERS (b
)))
406 Lisp_Object marker
, buffer
;
407 marker
= Fmake_marker ();
408 XSETBUFFER (buffer
, b
);
409 set_marker_both (marker
, buffer
, best_above
, best_above_byte
);
413 byte_char_debug_check (b
, best_above
, bytepos
);
416 cached_modiff
= BUF_MODIFF (b
);
417 cached_charpos
= best_above
;
418 cached_bytepos
= best_above_byte
;
426 /* Operations on markers. */
428 DEFUN ("marker-buffer", Fmarker_buffer
, Smarker_buffer
, 1, 1, 0,
429 "Return the buffer that MARKER points into, or nil if none.\n\
430 Returns nil if MARKER points into a dead buffer.")
432 register Lisp_Object marker
;
434 register Lisp_Object buf
;
435 CHECK_MARKER (marker
, 0);
436 if (XMARKER (marker
)->buffer
)
438 XSETBUFFER (buf
, XMARKER (marker
)->buffer
);
439 /* Return marker's buffer only if it is not dead. */
440 if (!NILP (XBUFFER (buf
)->name
))
446 DEFUN ("marker-position", Fmarker_position
, Smarker_position
, 1, 1, 0,
447 "Return the position MARKER points at, as a character number.")
451 register Lisp_Object pos
;
453 register struct buffer
*buf
;
455 CHECK_MARKER (marker
, 0);
456 if (XMARKER (marker
)->buffer
)
457 return make_number (XMARKER (marker
)->charpos
);
462 DEFUN ("set-marker", Fset_marker
, Sset_marker
, 2, 3, 0,
463 "Position MARKER before character number POSITION in BUFFER.\n\
464 BUFFER defaults to the current buffer.\n\
465 If POSITION is nil, makes marker point nowhere.\n\
466 Then it no longer slows down editing in any buffer.\n\
468 (marker
, position
, buffer
)
469 Lisp_Object marker
, position
, buffer
;
471 register int charno
, bytepos
;
472 register struct buffer
*b
;
473 register struct Lisp_Marker
*m
;
475 CHECK_MARKER (marker
, 0);
476 /* If position is nil or a marker that points nowhere,
477 make this marker point nowhere. */
479 || (MARKERP (position
) && !XMARKER (position
)->buffer
))
481 unchain_marker (marker
);
489 CHECK_BUFFER (buffer
, 1);
490 b
= XBUFFER (buffer
);
491 /* If buffer is dead, set marker to point nowhere. */
492 if (EQ (b
->name
, Qnil
))
494 unchain_marker (marker
);
499 m
= XMARKER (marker
);
501 /* Optimize the special case where we are copying the position
502 of an existing marker, and MARKER is already in the same buffer. */
503 if (MARKERP (position
) && b
== XMARKER (position
)->buffer
506 m
->bytepos
= XMARKER (position
)->bytepos
;
507 m
->charpos
= XMARKER (position
)->charpos
;
511 CHECK_NUMBER_COERCE_MARKER (position
, 1);
513 charno
= XINT (position
);
515 if (charno
< BUF_BEG (b
))
516 charno
= BUF_BEG (b
);
517 if (charno
> BUF_Z (b
))
520 bytepos
= buf_charpos_to_bytepos (b
, charno
);
522 /* Every character is at least one byte. */
523 if (charno
> bytepos
)
526 m
->bytepos
= bytepos
;
531 unchain_marker (marker
);
533 m
->chain
= BUF_MARKERS (b
);
534 BUF_MARKERS (b
) = marker
;
540 /* This version of Fset_marker won't let the position
541 be outside the visible part. */
544 set_marker_restricted (marker
, pos
, buffer
)
545 Lisp_Object marker
, pos
, buffer
;
547 register int charno
, bytepos
;
548 register struct buffer
*b
;
549 register struct Lisp_Marker
*m
;
551 CHECK_MARKER (marker
, 0);
552 /* If position is nil or a marker that points nowhere,
553 make this marker point nowhere. */
555 || (MARKERP (pos
) && !XMARKER (pos
)->buffer
))
557 unchain_marker (marker
);
565 CHECK_BUFFER (buffer
, 1);
566 b
= XBUFFER (buffer
);
567 /* If buffer is dead, set marker to point nowhere. */
568 if (EQ (b
->name
, Qnil
))
570 unchain_marker (marker
);
575 m
= XMARKER (marker
);
577 /* Optimize the special case where we are copying the position
578 of an existing marker, and MARKER is already in the same buffer. */
579 if (MARKERP (pos
) && b
== XMARKER (pos
)->buffer
582 m
->bytepos
= XMARKER (pos
)->bytepos
;
583 m
->charpos
= XMARKER (pos
)->charpos
;
587 CHECK_NUMBER_COERCE_MARKER (pos
, 1);
591 if (charno
< BUF_BEGV (b
))
592 charno
= BUF_BEGV (b
);
593 if (charno
> BUF_ZV (b
))
596 bytepos
= buf_charpos_to_bytepos (b
, charno
);
598 /* Every character is at least one byte. */
599 if (charno
> bytepos
)
602 m
->bytepos
= bytepos
;
607 unchain_marker (marker
);
609 m
->chain
= BUF_MARKERS (b
);
610 BUF_MARKERS (b
) = marker
;
616 /* Set the position of MARKER, specifying both the
617 character position and the corresponding byte position. */
620 set_marker_both (marker
, buffer
, charpos
, bytepos
)
621 Lisp_Object marker
, buffer
;
622 int charpos
, bytepos
;
624 register struct buffer
*b
;
625 register struct Lisp_Marker
*m
;
627 CHECK_MARKER (marker
, 0);
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 XMARKER (marker
)->buffer
= 0;
748 tail
= BUF_MARKERS (b
);
750 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
752 next
= XMARKER (tail
)->chain
;
755 if (XMARKER (marker
) == XMARKER (tail
))
759 BUF_MARKERS (b
) = next
;
760 /* Deleting first marker from the buffer's chain. Crash
761 if new first marker in chain does not say it belongs
762 to the same buffer, or at least that they have the same
764 if (!NILP (next
) && b
->text
!= XMARKER (next
)->buffer
->text
)
769 omark
= XMARKBIT (XMARKER (prev
)->chain
);
770 XMARKER (prev
)->chain
= next
;
771 XSETMARKBIT (XMARKER (prev
)->chain
, omark
);
773 /* We have removed the marker from the chain;
774 no need to scan the rest of the chain. */
782 /* Marker was not in its chain. */
786 /* Return the char position of marker MARKER, as a C integer. */
789 marker_position (marker
)
792 register struct Lisp_Marker
*m
= XMARKER (marker
);
793 register struct buffer
*buf
= m
->buffer
;
796 error ("Marker does not point anywhere");
801 /* Return the byte position of marker MARKER, as a C integer. */
804 marker_byte_position (marker
)
807 register struct Lisp_Marker
*m
= XMARKER (marker
);
808 register struct buffer
*buf
= m
->buffer
;
809 register int i
= m
->bytepos
;
812 error ("Marker does not point anywhere");
814 if (i
< BUF_BEG_BYTE (buf
) || i
> BUF_Z_BYTE (buf
))
820 DEFUN ("copy-marker", Fcopy_marker
, Scopy_marker
, 1, 2, 0,
821 "Return a new marker pointing at the same place as MARKER.\n\
822 If argument is a number, makes a new marker pointing\n\
823 at that position in the current buffer.\n\
824 The optional argument TYPE specifies the insertion type of the new marker;\n\
825 see `marker-insertion-type'.")
827 register Lisp_Object marker
, type
;
829 register Lisp_Object
new;
831 if (! (INTEGERP (marker
) || MARKERP (marker
)))
832 marker
= wrong_type_argument (Qinteger_or_marker_p
, marker
);
834 new = Fmake_marker ();
835 Fset_marker (new, marker
,
836 (MARKERP (marker
) ? Fmarker_buffer (marker
) : Qnil
));
837 XMARKER (new)->insertion_type
= !NILP (type
);
841 DEFUN ("marker-insertion-type", Fmarker_insertion_type
,
842 Smarker_insertion_type
, 1, 1, 0,
843 "Return insertion type of MARKER: t if it stays after inserted text.\n\
844 nil means the marker stays before text inserted there.")
846 register Lisp_Object marker
;
848 register Lisp_Object buf
;
849 CHECK_MARKER (marker
, 0);
850 return XMARKER (marker
)->insertion_type
? Qt
: Qnil
;
853 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type
,
854 Sset_marker_insertion_type
, 2, 2, 0,
855 "Set the insertion-type of MARKER to TYPE.\n\
856 If TYPE is t, it means the marker advances when you insert text at it.\n\
857 If TYPE is nil, it means the marker stays behind when you insert text at it.")
859 Lisp_Object marker
, type
;
861 CHECK_MARKER (marker
, 0);
863 XMARKER (marker
)->insertion_type
= ! NILP (type
);
867 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at
, Sbuffer_has_markers_at
,
869 "Return t if there are markers pointing at POSITION in the current buffer.")
871 Lisp_Object position
;
873 register Lisp_Object tail
;
876 charno
= XINT (position
);
883 for (tail
= BUF_MARKERS (current_buffer
);
885 tail
= XMARKER (tail
)->chain
)
886 if (XMARKER (tail
)->charpos
== charno
)
895 defsubr (&Smarker_position
);
896 defsubr (&Smarker_buffer
);
897 defsubr (&Sset_marker
);
898 defsubr (&Scopy_marker
);
899 defsubr (&Smarker_insertion_type
);
900 defsubr (&Sset_marker_insertion_type
);
901 defsubr (&Sbuffer_has_markers_at
);
903 DEFVAR_BOOL ("byte-debug-flag", &byte_debug_flag
,
904 "Non-nil enables debugging checks in byte/char position conversions.");