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
= best_above_byte
- bytepos
> 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. */
371 Lisp_Object marker
, buffer
;
372 marker
= Fmake_marker ();
373 XSETBUFFER (buffer
, b
);
374 set_marker_both (marker
, buffer
, best_below
, best_below_byte
);
378 byte_char_debug_check (b
, best_below
, bytepos
);
381 cached_modiff
= BUF_MODIFF (b
);
382 cached_charpos
= best_below
;
383 cached_bytepos
= best_below_byte
;
389 int record
= best_above_byte
- bytepos
> 5000;
391 while (best_above_byte
> bytepos
)
394 BUF_DEC_POS (b
, best_above_byte
);
397 /* If this position is quite far from the nearest known position,
398 cache the correspondence by creating a marker here.
399 It will last until the next GC. */
402 Lisp_Object marker
, buffer
;
403 marker
= Fmake_marker ();
404 XSETBUFFER (buffer
, b
);
405 set_marker_both (marker
, buffer
, best_above
, best_above_byte
);
409 byte_char_debug_check (b
, best_above
, bytepos
);
412 cached_modiff
= BUF_MODIFF (b
);
413 cached_charpos
= best_above
;
414 cached_bytepos
= best_above_byte
;
422 /* Operations on markers. */
424 DEFUN ("marker-buffer", Fmarker_buffer
, Smarker_buffer
, 1, 1, 0,
425 "Return the buffer that MARKER points into, or nil if none.\n\
426 Returns nil if MARKER points into a dead buffer.")
428 register Lisp_Object marker
;
430 register Lisp_Object buf
;
431 CHECK_MARKER (marker
, 0);
432 if (XMARKER (marker
)->buffer
)
434 XSETBUFFER (buf
, XMARKER (marker
)->buffer
);
435 /* Return marker's buffer only if it is not dead. */
436 if (!NILP (XBUFFER (buf
)->name
))
442 DEFUN ("marker-position", Fmarker_position
, Smarker_position
, 1, 1, 0,
443 "Return the position MARKER points at, as a character number.")
447 register Lisp_Object pos
;
449 register struct buffer
*buf
;
451 CHECK_MARKER (marker
, 0);
452 if (XMARKER (marker
)->buffer
)
453 return make_number (XMARKER (marker
)->charpos
);
458 DEFUN ("set-marker", Fset_marker
, Sset_marker
, 2, 3, 0,
459 "Position MARKER before character number POSITION in BUFFER.\n\
460 BUFFER defaults to the current buffer.\n\
461 If POSITION is nil, makes marker point nowhere.\n\
462 Then it no longer slows down editing in any buffer.\n\
464 (marker
, position
, buffer
)
465 Lisp_Object marker
, position
, buffer
;
467 register int charno
, bytepos
;
468 register struct buffer
*b
;
469 register struct Lisp_Marker
*m
;
471 CHECK_MARKER (marker
, 0);
472 /* If position is nil or a marker that points nowhere,
473 make this marker point nowhere. */
475 || (MARKERP (position
) && !XMARKER (position
)->buffer
))
477 unchain_marker (marker
);
485 CHECK_BUFFER (buffer
, 1);
486 b
= XBUFFER (buffer
);
487 /* If buffer is dead, set marker to point nowhere. */
488 if (EQ (b
->name
, Qnil
))
490 unchain_marker (marker
);
495 m
= XMARKER (marker
);
497 /* Optimize the special case where we are copying the position
498 of an existing marker, and MARKER is already in the same buffer. */
499 if (MARKERP (position
) && b
== XMARKER (position
)->buffer
502 m
->bytepos
= XMARKER (position
)->bytepos
;
503 m
->charpos
= XMARKER (position
)->charpos
;
507 CHECK_NUMBER_COERCE_MARKER (position
, 1);
509 charno
= XINT (position
);
511 if (charno
< BUF_BEG (b
))
512 charno
= BUF_BEG (b
);
513 if (charno
> BUF_Z (b
))
516 bytepos
= buf_charpos_to_bytepos (b
, charno
);
518 /* Every character is at least one byte. */
519 if (charno
> bytepos
)
522 m
->bytepos
= bytepos
;
527 unchain_marker (marker
);
529 m
->chain
= BUF_MARKERS (b
);
530 BUF_MARKERS (b
) = marker
;
536 /* This version of Fset_marker won't let the position
537 be outside the visible part. */
540 set_marker_restricted (marker
, pos
, buffer
)
541 Lisp_Object marker
, pos
, buffer
;
543 register int charno
, bytepos
;
544 register struct buffer
*b
;
545 register struct Lisp_Marker
*m
;
547 CHECK_MARKER (marker
, 0);
548 /* If position is nil or a marker that points nowhere,
549 make this marker point nowhere. */
551 || (MARKERP (pos
) && !XMARKER (pos
)->buffer
))
553 unchain_marker (marker
);
561 CHECK_BUFFER (buffer
, 1);
562 b
= XBUFFER (buffer
);
563 /* If buffer is dead, set marker to point nowhere. */
564 if (EQ (b
->name
, Qnil
))
566 unchain_marker (marker
);
571 m
= XMARKER (marker
);
573 /* Optimize the special case where we are copying the position
574 of an existing marker, and MARKER is already in the same buffer. */
575 if (MARKERP (pos
) && b
== XMARKER (pos
)->buffer
578 m
->bytepos
= XMARKER (pos
)->bytepos
;
579 m
->charpos
= XMARKER (pos
)->charpos
;
583 CHECK_NUMBER_COERCE_MARKER (pos
, 1);
587 if (charno
< BUF_BEGV (b
))
588 charno
= BUF_BEGV (b
);
589 if (charno
> BUF_ZV (b
))
592 bytepos
= buf_charpos_to_bytepos (b
, charno
);
594 /* Every character is at least one byte. */
595 if (charno
> bytepos
)
598 m
->bytepos
= bytepos
;
603 unchain_marker (marker
);
605 m
->chain
= BUF_MARKERS (b
);
606 BUF_MARKERS (b
) = marker
;
612 /* Set the position of MARKER, specifying both the
613 character position and the corresponding byte position. */
616 set_marker_both (marker
, buffer
, charpos
, bytepos
)
617 Lisp_Object marker
, buffer
;
618 int charpos
, bytepos
;
620 register struct buffer
*b
;
621 register struct Lisp_Marker
*m
;
623 CHECK_MARKER (marker
, 0);
629 CHECK_BUFFER (buffer
, 1);
630 b
= XBUFFER (buffer
);
631 /* If buffer is dead, set marker to point nowhere. */
632 if (EQ (b
->name
, Qnil
))
634 unchain_marker (marker
);
639 m
= XMARKER (marker
);
641 /* In a single-byte buffer, the two positions must be equal. */
642 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
643 && charpos
!= bytepos
)
645 /* Every character is at least one byte. */
646 if (charpos
> bytepos
)
649 m
->bytepos
= bytepos
;
650 m
->charpos
= charpos
;
654 unchain_marker (marker
);
656 m
->chain
= BUF_MARKERS (b
);
657 BUF_MARKERS (b
) = marker
;
663 /* This version of set_marker_both won't let the position
664 be outside the visible part. */
667 set_marker_restricted_both (marker
, buffer
, charpos
, bytepos
)
668 Lisp_Object marker
, buffer
;
669 int charpos
, bytepos
;
671 register struct buffer
*b
;
672 register struct Lisp_Marker
*m
;
674 CHECK_MARKER (marker
, 0);
680 CHECK_BUFFER (buffer
, 1);
681 b
= XBUFFER (buffer
);
682 /* If buffer is dead, set marker to point nowhere. */
683 if (EQ (b
->name
, Qnil
))
685 unchain_marker (marker
);
690 m
= XMARKER (marker
);
692 if (charpos
< BUF_BEGV (b
))
693 charpos
= BUF_BEGV (b
);
694 if (charpos
> BUF_ZV (b
))
695 charpos
= BUF_ZV (b
);
696 if (bytepos
< BUF_BEGV_BYTE (b
))
697 bytepos
= BUF_BEGV_BYTE (b
);
698 if (bytepos
> BUF_ZV_BYTE (b
))
699 bytepos
= BUF_ZV_BYTE (b
);
701 /* In a single-byte buffer, the two positions must be equal. */
702 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
703 && charpos
!= bytepos
)
705 /* Every character is at least one byte. */
706 if (charpos
> bytepos
)
709 m
->bytepos
= bytepos
;
710 m
->charpos
= charpos
;
714 unchain_marker (marker
);
716 m
->chain
= BUF_MARKERS (b
);
717 BUF_MARKERS (b
) = marker
;
723 /* This is called during garbage collection,
724 so we must be careful to ignore and preserve mark bits,
725 including those in chain fields of markers. */
728 unchain_marker (marker
)
729 register Lisp_Object marker
;
731 register Lisp_Object tail
, prev
, next
;
732 register EMACS_INT omark
;
733 register struct buffer
*b
;
735 b
= XMARKER (marker
)->buffer
;
739 if (EQ (b
->name
, Qnil
))
742 tail
= BUF_MARKERS (b
);
744 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
746 next
= XMARKER (tail
)->chain
;
749 if (XMARKER (marker
) == XMARKER (tail
))
753 BUF_MARKERS (b
) = next
;
754 /* Deleting first marker from the buffer's chain. Crash
755 if new first marker in chain does not say it belongs
756 to the same buffer, or at least that they have the same
758 if (!NILP (next
) && b
->text
!= XMARKER (next
)->buffer
->text
)
763 omark
= XMARKBIT (XMARKER (prev
)->chain
);
764 XMARKER (prev
)->chain
= next
;
765 XSETMARKBIT (XMARKER (prev
)->chain
, omark
);
773 XMARKER (marker
)->buffer
= 0;
776 /* Return the char position of marker MARKER, as a C integer. */
779 marker_position (marker
)
782 register struct Lisp_Marker
*m
= XMARKER (marker
);
783 register struct buffer
*buf
= m
->buffer
;
786 error ("Marker does not point anywhere");
791 /* Return the byte position of marker MARKER, as a C integer. */
794 marker_byte_position (marker
)
797 register struct Lisp_Marker
*m
= XMARKER (marker
);
798 register struct buffer
*buf
= m
->buffer
;
799 register int i
= m
->bytepos
;
802 error ("Marker does not point anywhere");
804 if (i
< BUF_BEG_BYTE (buf
) || i
> BUF_Z_BYTE (buf
))
810 DEFUN ("copy-marker", Fcopy_marker
, Scopy_marker
, 1, 2, 0,
811 "Return a new marker pointing at the same place as MARKER.\n\
812 If argument is a number, makes a new marker pointing\n\
813 at that position in the current buffer.\n\
814 The optional argument TYPE specifies the insertion type of the new marker;\n\
815 see `marker-insertion-type'.")
817 register Lisp_Object marker
, type
;
819 register Lisp_Object
new;
821 if (INTEGERP (marker
) || MARKERP (marker
))
823 new = Fmake_marker ();
824 Fset_marker (new, marker
,
825 (MARKERP (marker
) ? Fmarker_buffer (marker
) : Qnil
));
826 XMARKER (new)->insertion_type
= !NILP (type
);
830 marker
= wrong_type_argument (Qinteger_or_marker_p
, marker
);
833 DEFUN ("marker-insertion-type", Fmarker_insertion_type
,
834 Smarker_insertion_type
, 1, 1, 0,
835 "Return insertion type of MARKER: t if it stays after inserted text.\n\
836 nil means the marker stays before text inserted there.")
838 register Lisp_Object marker
;
840 register Lisp_Object buf
;
841 CHECK_MARKER (marker
, 0);
842 return XMARKER (marker
)->insertion_type
? Qt
: Qnil
;
845 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type
,
846 Sset_marker_insertion_type
, 2, 2, 0,
847 "Set the insertion-type of MARKER to TYPE.\n\
848 If TYPE is t, it means the marker advances when you insert text at it.\n\
849 If TYPE is nil, it means the marker stays behind when you insert text at it.")
851 Lisp_Object marker
, type
;
853 CHECK_MARKER (marker
, 0);
855 XMARKER (marker
)->insertion_type
= ! NILP (type
);
859 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at
, Sbuffer_has_markers_at
,
861 "Return t if there are markers pointing at POSITION in the current buffer.")
863 Lisp_Object position
;
865 register Lisp_Object tail
;
868 charno
= XINT (position
);
875 for (tail
= BUF_MARKERS (current_buffer
);
877 tail
= XMARKER (tail
)->chain
)
878 if (XMARKER (tail
)->charpos
== charno
)
887 defsubr (&Smarker_position
);
888 defsubr (&Smarker_buffer
);
889 defsubr (&Sset_marker
);
890 defsubr (&Scopy_marker
);
891 defsubr (&Smarker_insertion_type
);
892 defsubr (&Sset_marker_insertion_type
);
893 defsubr (&Sbuffer_has_markers_at
);
895 DEFVAR_BOOL ("byte-debug-flag", &byte_debug_flag
,
896 "Non-nil enables debugging checks in byte/char position conversions.");