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 clear_charpos_cache (b
)
38 if (cached_buffer
== b
)
42 /* Converting between character positions and byte positions. */
44 /* There are several places in the buffer where we know
45 the corrspondence: BEG, BEGV, PT, GPT, ZV and Z,
46 and everywhere there is a marker. So we find the one of these places
47 that is closest to the specified position, and scan from there. */
49 /* charpos_to_bytepos returns the byte position corresponding to CHARPOS. */
51 /* This macro is a subroutine of charpos_to_bytepos.
52 Note that it is desirable that BYTEPOS is not evaluated
53 except when we really want its value. */
55 #define CONSIDER(CHARPOS, BYTEPOS) \
57 int this_charpos = (CHARPOS); \
60 if (this_charpos == charpos) \
62 else if (this_charpos > charpos) \
64 if (this_charpos < best_above) \
66 best_above = this_charpos; \
67 best_above_byte = (BYTEPOS); \
71 else if (this_charpos > best_below) \
73 best_below = this_charpos; \
74 best_below_byte = (BYTEPOS); \
80 if (best_above - best_below == best_above_byte - best_below_byte) \
81 return best_below_byte + (charpos - best_below); \
86 charpos_to_bytepos (charpos
)
89 return buf_charpos_to_bytepos (current_buffer
, charpos
);
93 buf_charpos_to_bytepos (b
, charpos
)
98 int gapend_byte
= BUF_GPT_BYTE (b
) + BUF_GAP_SIZE (b
);
99 int best_above
, best_above_byte
;
100 int best_below
, best_below_byte
;
102 if (charpos
< BUF_BEG (b
) || charpos
> BUF_Z (b
))
105 best_above
= BUF_Z (b
);
106 best_above_byte
= BUF_Z_BYTE (b
);
108 /* If this buffer has as many characters as bytes,
109 each character must be one byte.
110 This takes care of the case where enable-multibyte-characters is nil. */
111 if (best_above
== best_above_byte
)
117 /* We find in best_above and best_above_byte
118 the closest known point above CHARPOS,
119 and in best_below and best_below_byte
120 the closest known point below CHARPOS,
122 If at any point we can tell that the space between those
123 two best approximations is all single-byte,
124 we interpolate the result immediately. */
126 CONSIDER (BUF_PT (b
), BUF_PT_BYTE (b
));
127 CONSIDER (BUF_GPT (b
), BUF_GPT_BYTE (b
));
128 CONSIDER (BUF_BEGV (b
), BUF_BEGV_BYTE (b
));
129 CONSIDER (BUF_ZV (b
), BUF_ZV_BYTE (b
));
131 if (b
== cached_buffer
&& BUF_MODIFF (b
) == cached_modiff
)
132 CONSIDER (cached_charpos
, cached_bytepos
);
134 tail
= BUF_MARKERS (b
);
135 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
137 CONSIDER (XMARKER (tail
)->charpos
, XMARKER (tail
)->bytepos
);
139 /* If we are down to a range of 50 chars,
140 don't bother checking any other markers;
141 scan the intervening chars directly now. */
142 if (best_above
- best_below
< 50)
145 tail
= XMARKER (tail
)->chain
;
148 /* We get here if we did not exactly hit one of the known places.
149 We have one known above and one known below.
150 Scan, counting characters, from whichever one is closer. */
152 if (charpos
- best_below
< best_above
- charpos
)
154 int record
= charpos
- best_below
> 5000;
156 while (best_below
!= charpos
)
159 BUF_INC_POS (b
, best_below_byte
);
162 /* If this position is quite far from the nearest known position,
163 cache the correspondence by creating a marker here.
164 It will last until the next GC. */
168 marker
= Fmake_marker ();
169 set_marker_both (marker
, Qnil
, best_below
, best_below_byte
);
173 cached_modiff
= BUF_MODIFF (b
);
174 cached_charpos
= best_below
;
175 cached_bytepos
= best_below_byte
;
177 return best_below_byte
;
181 int record
= best_above
- charpos
> 5000;
183 while (best_above
!= charpos
)
186 BUF_DEC_POS (b
, best_above_byte
);
189 /* If this position is quite far from the nearest known position,
190 cache the correspondence by creating a marker here.
191 It will last until the next GC. */
195 marker
= Fmake_marker ();
196 set_marker_both (marker
, Qnil
, best_above
, best_above_byte
);
200 cached_modiff
= BUF_MODIFF (b
);
201 cached_charpos
= best_above
;
202 cached_bytepos
= best_above_byte
;
204 return best_above_byte
;
210 /* bytepos_to_charpos returns the char position corresponding to BYTEPOS. */
212 /* This macro is a subroutine of bytepos_to_charpos.
213 It is used when BYTEPOS is actually the byte position. */
215 #define CONSIDER(BYTEPOS, CHARPOS) \
217 int this_bytepos = (BYTEPOS); \
220 if (this_bytepos == bytepos) \
222 else if (this_bytepos > bytepos) \
224 if (this_bytepos < best_above_byte) \
226 best_above = (CHARPOS); \
227 best_above_byte = this_bytepos; \
231 else if (this_bytepos > best_below_byte) \
233 best_below = (CHARPOS); \
234 best_below_byte = this_bytepos; \
240 if (best_above - best_below == best_above_byte - best_below_byte) \
241 return best_below + (bytepos - best_below_byte); \
246 bytepos_to_charpos (bytepos
)
249 return buf_bytepos_to_charpos (current_buffer
, bytepos
);
253 buf_bytepos_to_charpos (b
, bytepos
)
258 int best_above
, best_above_byte
;
259 int best_below
, best_below_byte
;
261 if (bytepos
< BUF_BEG_BYTE (b
) || bytepos
> BUF_Z_BYTE (b
))
264 best_above
= BUF_Z (b
);
265 best_above_byte
= BUF_Z_BYTE (b
);
267 /* If this buffer has as many characters as bytes,
268 each character must be one byte.
269 This takes care of the case where enable-multibyte-characters is nil. */
270 if (best_above
== best_above_byte
)
276 CONSIDER (BUF_PT_BYTE (b
), BUF_PT (b
));
277 CONSIDER (BUF_GPT_BYTE (b
), BUF_GPT (b
));
278 CONSIDER (BUF_BEGV_BYTE (b
), BUF_BEGV (b
));
279 CONSIDER (BUF_ZV_BYTE (b
), BUF_ZV (b
));
281 if (b
== cached_buffer
&& BUF_MODIFF (b
) == cached_modiff
)
282 CONSIDER (cached_bytepos
, cached_charpos
);
284 tail
= BUF_MARKERS (b
);
285 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
287 CONSIDER (XMARKER (tail
)->bytepos
, XMARKER (tail
)->charpos
);
289 /* If we are down to a range of 50 chars,
290 don't bother checking any other markers;
291 scan the intervening chars directly now. */
292 if (best_above
- best_below
< 50)
295 tail
= XMARKER (tail
)->chain
;
298 /* We get here if we did not exactly hit one of the known places.
299 We have one known above and one known below.
300 Scan, counting characters, from whichever one is closer. */
302 if (bytepos
- best_below_byte
< best_above_byte
- bytepos
)
304 int record
= best_above_byte
- bytepos
> 5000;
306 while (best_below_byte
< bytepos
)
309 BUF_INC_POS (b
, best_below_byte
);
312 /* If this position is quite far from the nearest known position,
313 cache the correspondence by creating a marker here.
314 It will last until the next GC. */
318 marker
= Fmake_marker ();
319 set_marker_both (marker
, Qnil
, best_below
, best_below_byte
);
323 cached_modiff
= BUF_MODIFF (b
);
324 cached_charpos
= best_below
;
325 cached_bytepos
= best_below_byte
;
331 int record
= best_above_byte
- bytepos
> 5000;
333 while (best_above_byte
> bytepos
)
336 BUF_DEC_POS (b
, best_above_byte
);
339 /* If this position is quite far from the nearest known position,
340 cache the correspondence by creating a marker here.
341 It will last until the next GC. */
345 marker
= Fmake_marker ();
346 set_marker_both (marker
, Qnil
, best_above
, best_above_byte
);
350 cached_modiff
= BUF_MODIFF (b
);
351 cached_charpos
= best_above
;
352 cached_bytepos
= best_above_byte
;
360 /* Operations on markers. */
362 DEFUN ("marker-buffer", Fmarker_buffer
, Smarker_buffer
, 1, 1, 0,
363 "Return the buffer that MARKER points into, or nil if none.\n\
364 Returns nil if MARKER points into a dead buffer.")
366 register Lisp_Object marker
;
368 register Lisp_Object buf
;
369 CHECK_MARKER (marker
, 0);
370 if (XMARKER (marker
)->buffer
)
372 XSETBUFFER (buf
, XMARKER (marker
)->buffer
);
373 /* Return marker's buffer only if it is not dead. */
374 if (!NILP (XBUFFER (buf
)->name
))
380 DEFUN ("marker-position", Fmarker_position
, Smarker_position
, 1, 1, 0,
381 "Return the position MARKER points at, as a character number.")
385 register Lisp_Object pos
;
387 register struct buffer
*buf
;
389 CHECK_MARKER (marker
, 0);
390 if (XMARKER (marker
)->buffer
)
391 return make_number (XMARKER (marker
)->charpos
);
396 DEFUN ("set-marker", Fset_marker
, Sset_marker
, 2, 3, 0,
397 "Position MARKER before character number POSITION in BUFFER.\n\
398 BUFFER defaults to the current buffer.\n\
399 If POSITION is nil, makes marker point nowhere.\n\
400 Then it no longer slows down editing in any buffer.\n\
402 (marker
, position
, buffer
)
403 Lisp_Object marker
, position
, buffer
;
405 register int charno
, bytepos
;
406 register struct buffer
*b
;
407 register struct Lisp_Marker
*m
;
409 CHECK_MARKER (marker
, 0);
410 /* If position is nil or a marker that points nowhere,
411 make this marker point nowhere. */
413 || (MARKERP (position
) && !XMARKER (position
)->buffer
))
415 unchain_marker (marker
);
423 CHECK_BUFFER (buffer
, 1);
424 b
= XBUFFER (buffer
);
425 /* If buffer is dead, set marker to point nowhere. */
426 if (EQ (b
->name
, Qnil
))
428 unchain_marker (marker
);
433 m
= XMARKER (marker
);
435 /* Optimize the special case where we are copying the position
436 of an existing marker, and MARKER is already in the same buffer. */
437 if (MARKERP (position
) && b
== XMARKER (position
)->buffer
440 m
->bytepos
= XMARKER (position
)->bytepos
;
441 m
->charpos
= XMARKER (position
)->charpos
;
445 CHECK_NUMBER_COERCE_MARKER (position
, 1);
447 charno
= XINT (position
);
449 if (charno
< BUF_BEG (b
))
450 charno
= BUF_BEG (b
);
451 if (charno
> BUF_Z (b
))
454 bytepos
= buf_charpos_to_bytepos (b
, charno
);
456 /* Every character is at least one byte. */
457 if (charno
> bytepos
)
460 m
->bytepos
= bytepos
;
465 unchain_marker (marker
);
467 m
->chain
= BUF_MARKERS (b
);
468 BUF_MARKERS (b
) = marker
;
474 /* This version of Fset_marker won't let the position
475 be outside the visible part. */
478 set_marker_restricted (marker
, pos
, buffer
)
479 Lisp_Object marker
, pos
, buffer
;
481 register int charno
, bytepos
;
482 register struct buffer
*b
;
483 register struct Lisp_Marker
*m
;
485 CHECK_MARKER (marker
, 0);
486 /* If position is nil or a marker that points nowhere,
487 make this marker point nowhere. */
489 || (MARKERP (pos
) && !XMARKER (pos
)->buffer
))
491 unchain_marker (marker
);
499 CHECK_BUFFER (buffer
, 1);
500 b
= XBUFFER (buffer
);
501 /* If buffer is dead, set marker to point nowhere. */
502 if (EQ (b
->name
, Qnil
))
504 unchain_marker (marker
);
509 m
= XMARKER (marker
);
511 /* Optimize the special case where we are copying the position
512 of an existing marker, and MARKER is already in the same buffer. */
513 if (MARKERP (pos
) && b
== XMARKER (pos
)->buffer
516 m
->bytepos
= XMARKER (pos
)->bytepos
;
517 m
->charpos
= XMARKER (pos
)->charpos
;
521 CHECK_NUMBER_COERCE_MARKER (pos
, 1);
525 if (charno
< BUF_BEGV (b
))
526 charno
= BUF_BEGV (b
);
527 if (charno
> BUF_ZV (b
))
530 bytepos
= buf_charpos_to_bytepos (b
, charno
);
532 /* Every character is at least one byte. */
533 if (charno
> bytepos
)
536 m
->bytepos
= bytepos
;
541 unchain_marker (marker
);
543 m
->chain
= BUF_MARKERS (b
);
544 BUF_MARKERS (b
) = marker
;
550 /* Set the position of MARKER, specifying both the
551 character position and the corresponding byte position. */
554 set_marker_both (marker
, buffer
, charpos
, bytepos
)
555 Lisp_Object marker
, buffer
;
556 int charpos
, bytepos
;
558 register struct buffer
*b
;
559 register struct Lisp_Marker
*m
;
561 CHECK_MARKER (marker
, 0);
562 /* If position is nil or a marker that points nowhere,
563 make this marker point nowhere. */
565 || (MARKERP (charpos
) && !XMARKER (charpos
)->buffer
))
567 unchain_marker (marker
);
571 CHECK_NUMBER_COERCE_MARKER (charpos
, 1);
576 CHECK_BUFFER (buffer
, 1);
577 b
= XBUFFER (buffer
);
578 /* If buffer is dead, set marker to point nowhere. */
579 if (EQ (b
->name
, Qnil
))
581 unchain_marker (marker
);
586 m
= XMARKER (marker
);
588 /* In a single-byte buffer, the two positions must be equal. */
589 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
590 && charpos
!= bytepos
)
592 /* Every character is at least one byte. */
593 if (charpos
> bytepos
)
596 m
->bytepos
= bytepos
;
597 m
->charpos
= charpos
;
601 unchain_marker (marker
);
603 m
->chain
= BUF_MARKERS (b
);
604 BUF_MARKERS (b
) = marker
;
610 /* This version of set_marker_both won't let the position
611 be outside the visible part. */
614 set_marker_restricted_both (marker
, buffer
, charpos
, bytepos
)
615 Lisp_Object marker
, buffer
;
616 int charpos
, bytepos
;
618 register struct buffer
*b
;
619 register struct Lisp_Marker
*m
;
621 CHECK_MARKER (marker
, 0);
627 CHECK_BUFFER (buffer
, 1);
628 b
= XBUFFER (buffer
);
629 /* If buffer is dead, set marker to point nowhere. */
630 if (EQ (b
->name
, Qnil
))
632 unchain_marker (marker
);
637 m
= XMARKER (marker
);
639 if (charpos
< BUF_BEGV (b
))
640 charpos
= BUF_BEGV (b
);
641 if (charpos
> BUF_ZV (b
))
642 charpos
= BUF_ZV (b
);
643 if (bytepos
< BUF_BEGV_BYTE (b
))
644 bytepos
= BUF_BEGV_BYTE (b
);
645 if (bytepos
> BUF_ZV_BYTE (b
))
646 bytepos
= BUF_ZV_BYTE (b
);
648 /* In a single-byte buffer, the two positions must be equal. */
649 if (BUF_Z (b
) == BUF_Z_BYTE (b
)
650 && charpos
!= bytepos
)
652 /* Every character is at least one byte. */
653 if (charpos
> bytepos
)
656 m
->bytepos
= bytepos
;
657 m
->charpos
= charpos
;
661 unchain_marker (marker
);
663 m
->chain
= BUF_MARKERS (b
);
664 BUF_MARKERS (b
) = marker
;
670 /* This is called during garbage collection,
671 so we must be careful to ignore and preserve mark bits,
672 including those in chain fields of markers. */
675 unchain_marker (marker
)
676 register Lisp_Object marker
;
678 register Lisp_Object tail
, prev
, next
;
679 register EMACS_INT omark
;
680 register struct buffer
*b
;
682 b
= XMARKER (marker
)->buffer
;
686 if (EQ (b
->name
, Qnil
))
689 tail
= BUF_MARKERS (b
);
691 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
693 next
= XMARKER (tail
)->chain
;
696 if (XMARKER (marker
) == XMARKER (tail
))
700 BUF_MARKERS (b
) = next
;
701 /* Deleting first marker from the buffer's chain. Crash
702 if new first marker in chain does not say it belongs
703 to the same buffer, or at least that they have the same
705 if (!NILP (next
) && b
->text
!= XMARKER (next
)->buffer
->text
)
710 omark
= XMARKBIT (XMARKER (prev
)->chain
);
711 XMARKER (prev
)->chain
= next
;
712 XSETMARKBIT (XMARKER (prev
)->chain
, omark
);
720 XMARKER (marker
)->buffer
= 0;
723 /* Return the char position of marker MARKER, as a C integer. */
726 marker_position (marker
)
729 register struct Lisp_Marker
*m
= XMARKER (marker
);
730 register struct buffer
*buf
= m
->buffer
;
733 error ("Marker does not point anywhere");
738 /* Return the byte position of marker MARKER, as a C integer. */
741 marker_byte_position (marker
)
744 register struct Lisp_Marker
*m
= XMARKER (marker
);
745 register struct buffer
*buf
= m
->buffer
;
746 register int i
= m
->bytepos
;
749 error ("Marker does not point anywhere");
751 if (i
< BUF_BEG_BYTE (buf
) || i
> BUF_Z_BYTE (buf
))
757 DEFUN ("copy-marker", Fcopy_marker
, Scopy_marker
, 1, 2, 0,
758 "Return a new marker pointing at the same place as MARKER.\n\
759 If argument is a number, makes a new marker pointing\n\
760 at that position in the current buffer.\n\
761 The optional argument TYPE specifies the insertion type of the new marker;\n\
762 see `marker-insertion-type'.")
764 register Lisp_Object marker
, type
;
766 register Lisp_Object
new;
768 if (INTEGERP (marker
) || MARKERP (marker
))
770 new = Fmake_marker ();
771 Fset_marker (new, marker
,
772 (MARKERP (marker
) ? Fmarker_buffer (marker
) : Qnil
));
773 XMARKER (new)->insertion_type
= !NILP (type
);
777 marker
= wrong_type_argument (Qinteger_or_marker_p
, marker
);
780 DEFUN ("marker-insertion-type", Fmarker_insertion_type
,
781 Smarker_insertion_type
, 1, 1, 0,
782 "Return insertion type of MARKER: t if it stays after inserted text.\n\
783 nil means the marker stays before text inserted there.")
785 register Lisp_Object marker
;
787 register Lisp_Object buf
;
788 CHECK_MARKER (marker
, 0);
789 return XMARKER (marker
)->insertion_type
? Qt
: Qnil
;
792 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type
,
793 Sset_marker_insertion_type
, 2, 2, 0,
794 "Set the insertion-type of MARKER to TYPE.\n\
795 If TYPE is t, it means the marker advances when you insert text at it.\n\
796 If TYPE is nil, it means the marker stays behind when you insert text at it.")
798 Lisp_Object marker
, type
;
800 CHECK_MARKER (marker
, 0);
802 XMARKER (marker
)->insertion_type
= ! NILP (type
);
806 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at
, Sbuffer_has_markers_at
,
808 "Return t if there are markers pointing at POSITION in the current buffer.")
810 Lisp_Object position
;
812 register Lisp_Object tail
;
815 charno
= XINT (position
);
822 for (tail
= BUF_MARKERS (current_buffer
);
823 XSYMBOL (tail
) != XSYMBOL (Qnil
);
824 tail
= XMARKER (tail
)->chain
)
825 if (XMARKER (tail
)->charpos
== charno
)
834 defsubr (&Smarker_position
);
835 defsubr (&Smarker_buffer
);
836 defsubr (&Sset_marker
);
837 defsubr (&Scopy_marker
);
838 defsubr (&Smarker_insertion_type
);
839 defsubr (&Sset_marker_insertion_type
);
840 defsubr (&Sbuffer_has_markers_at
);