Merge from trunk.
[bpt/emacs.git] / src / marker.c
1 /* Markers: examining, setting and deleting.
2 Copyright (C) 1985, 1997-1998, 2001-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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 3 of the License, or
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19
20 #include <config.h>
21 #include <setjmp.h>
22 #include "lisp.h"
23 #include "buffer.h"
24 #include "character.h"
25
26 /* Record one cached position found recently by
27 buf_charpos_to_bytepos or buf_bytepos_to_charpos. */
28
29 static ptrdiff_t cached_charpos;
30 static ptrdiff_t cached_bytepos;
31 static struct buffer *cached_buffer;
32 static int cached_modiff;
33
34 static void byte_char_debug_check (struct buffer *, ptrdiff_t, ptrdiff_t);
35
36 void
37 clear_charpos_cache (struct buffer *b)
38 {
39 if (cached_buffer == b)
40 cached_buffer = 0;
41 }
42 \f
43 /* Converting between character positions and byte positions. */
44
45 /* There are several places in the buffer where we know
46 the correspondence: BEG, BEGV, PT, GPT, ZV and Z,
47 and everywhere there is a marker. So we find the one of these places
48 that is closest to the specified position, and scan from there. */
49
50 /* charpos_to_bytepos returns the byte position corresponding to CHARPOS. */
51
52 /* This macro is a subroutine of charpos_to_bytepos.
53 Note that it is desirable that BYTEPOS is not evaluated
54 except when we really want its value. */
55
56 #define CONSIDER(CHARPOS, BYTEPOS) \
57 { \
58 ptrdiff_t this_charpos = (CHARPOS); \
59 int changed = 0; \
60 \
61 if (this_charpos == charpos) \
62 { \
63 ptrdiff_t value = (BYTEPOS); \
64 if (byte_debug_flag) \
65 byte_char_debug_check (b, charpos, value); \
66 return value; \
67 } \
68 else if (this_charpos > charpos) \
69 { \
70 if (this_charpos < best_above) \
71 { \
72 best_above = this_charpos; \
73 best_above_byte = (BYTEPOS); \
74 changed = 1; \
75 } \
76 } \
77 else if (this_charpos > best_below) \
78 { \
79 best_below = this_charpos; \
80 best_below_byte = (BYTEPOS); \
81 changed = 1; \
82 } \
83 \
84 if (changed) \
85 { \
86 if (best_above - best_below == best_above_byte - best_below_byte) \
87 { \
88 ptrdiff_t value = best_below_byte + (charpos - best_below); \
89 if (byte_debug_flag) \
90 byte_char_debug_check (b, charpos, value); \
91 return value; \
92 } \
93 } \
94 }
95
96 static void
97 byte_char_debug_check (struct buffer *b, ptrdiff_t charpos, ptrdiff_t bytepos)
98 {
99 ptrdiff_t nchars = 0;
100
101 if (bytepos > BUF_GPT_BYTE (b))
102 {
103 nchars = multibyte_chars_in_text (BUF_BEG_ADDR (b),
104 BUF_GPT_BYTE (b) - BUF_BEG_BYTE (b));
105 nchars += multibyte_chars_in_text (BUF_GAP_END_ADDR (b),
106 bytepos - BUF_GPT_BYTE (b));
107 }
108 else
109 nchars = multibyte_chars_in_text (BUF_BEG_ADDR (b),
110 bytepos - BUF_BEG_BYTE (b));
111
112 if (charpos - 1 != nchars)
113 abort ();
114 }
115
116 ptrdiff_t
117 charpos_to_bytepos (ptrdiff_t charpos)
118 {
119 return buf_charpos_to_bytepos (current_buffer, charpos);
120 }
121
122 ptrdiff_t
123 buf_charpos_to_bytepos (struct buffer *b, ptrdiff_t charpos)
124 {
125 struct Lisp_Marker *tail;
126 ptrdiff_t best_above, best_above_byte;
127 ptrdiff_t best_below, best_below_byte;
128
129 if (charpos < BUF_BEG (b) || charpos > BUF_Z (b))
130 abort ();
131
132 best_above = BUF_Z (b);
133 best_above_byte = BUF_Z_BYTE (b);
134
135 /* If this buffer has as many characters as bytes,
136 each character must be one byte.
137 This takes care of the case where enable-multibyte-characters is nil. */
138 if (best_above == best_above_byte)
139 return charpos;
140
141 best_below = BEG;
142 best_below_byte = BEG_BYTE;
143
144 /* We find in best_above and best_above_byte
145 the closest known point above CHARPOS,
146 and in best_below and best_below_byte
147 the closest known point below CHARPOS,
148
149 If at any point we can tell that the space between those
150 two best approximations is all single-byte,
151 we interpolate the result immediately. */
152
153 CONSIDER (BUF_PT (b), BUF_PT_BYTE (b));
154 CONSIDER (BUF_GPT (b), BUF_GPT_BYTE (b));
155 CONSIDER (BUF_BEGV (b), BUF_BEGV_BYTE (b));
156 CONSIDER (BUF_ZV (b), BUF_ZV_BYTE (b));
157
158 if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff)
159 CONSIDER (cached_charpos, cached_bytepos);
160
161 for (tail = BUF_MARKERS (b); tail; tail = tail->next)
162 {
163 CONSIDER (tail->charpos, tail->bytepos);
164
165 /* If we are down to a range of 50 chars,
166 don't bother checking any other markers;
167 scan the intervening chars directly now. */
168 if (best_above - best_below < 50)
169 break;
170 }
171
172 /* We get here if we did not exactly hit one of the known places.
173 We have one known above and one known below.
174 Scan, counting characters, from whichever one is closer. */
175
176 if (charpos - best_below < best_above - charpos)
177 {
178 int record = charpos - best_below > 5000;
179
180 while (best_below != charpos)
181 {
182 best_below++;
183 BUF_INC_POS (b, best_below_byte);
184 }
185
186 /* If this position is quite far from the nearest known position,
187 cache the correspondence by creating a marker here.
188 It will last until the next GC. */
189 if (record)
190 {
191 Lisp_Object marker, buffer;
192 marker = Fmake_marker ();
193 XSETBUFFER (buffer, b);
194 set_marker_both (marker, buffer, best_below, best_below_byte);
195 }
196
197 if (byte_debug_flag)
198 byte_char_debug_check (b, charpos, best_below_byte);
199
200 cached_buffer = b;
201 cached_modiff = BUF_MODIFF (b);
202 cached_charpos = best_below;
203 cached_bytepos = best_below_byte;
204
205 return best_below_byte;
206 }
207 else
208 {
209 int record = best_above - charpos > 5000;
210
211 while (best_above != charpos)
212 {
213 best_above--;
214 BUF_DEC_POS (b, best_above_byte);
215 }
216
217 /* If this position is quite far from the nearest known position,
218 cache the correspondence by creating a marker here.
219 It will last until the next GC. */
220 if (record)
221 {
222 Lisp_Object marker, buffer;
223 marker = Fmake_marker ();
224 XSETBUFFER (buffer, b);
225 set_marker_both (marker, buffer, best_above, best_above_byte);
226 }
227
228 if (byte_debug_flag)
229 byte_char_debug_check (b, charpos, best_above_byte);
230
231 cached_buffer = b;
232 cached_modiff = BUF_MODIFF (b);
233 cached_charpos = best_above;
234 cached_bytepos = best_above_byte;
235
236 return best_above_byte;
237 }
238 }
239
240 #undef CONSIDER
241
242 /* Used for debugging: recompute the bytepos corresponding to CHARPOS
243 in the simplest, most reliable way. */
244
245 extern ptrdiff_t verify_bytepos (ptrdiff_t charpos) EXTERNALLY_VISIBLE;
246 ptrdiff_t
247 verify_bytepos (ptrdiff_t charpos)
248 {
249 ptrdiff_t below = 1;
250 ptrdiff_t below_byte = 1;
251
252 while (below != charpos)
253 {
254 below++;
255 BUF_INC_POS (current_buffer, below_byte);
256 }
257
258 return below_byte;
259 }
260 \f
261 /* buf_bytepos_to_charpos returns the char position corresponding to
262 BYTEPOS. */
263
264 /* This macro is a subroutine of buf_bytepos_to_charpos.
265 It is used when BYTEPOS is actually the byte position. */
266
267 #define CONSIDER(BYTEPOS, CHARPOS) \
268 { \
269 ptrdiff_t this_bytepos = (BYTEPOS); \
270 int changed = 0; \
271 \
272 if (this_bytepos == bytepos) \
273 { \
274 ptrdiff_t value = (CHARPOS); \
275 if (byte_debug_flag) \
276 byte_char_debug_check (b, value, bytepos); \
277 return value; \
278 } \
279 else if (this_bytepos > bytepos) \
280 { \
281 if (this_bytepos < best_above_byte) \
282 { \
283 best_above = (CHARPOS); \
284 best_above_byte = this_bytepos; \
285 changed = 1; \
286 } \
287 } \
288 else if (this_bytepos > best_below_byte) \
289 { \
290 best_below = (CHARPOS); \
291 best_below_byte = this_bytepos; \
292 changed = 1; \
293 } \
294 \
295 if (changed) \
296 { \
297 if (best_above - best_below == best_above_byte - best_below_byte) \
298 { \
299 ptrdiff_t value = best_below + (bytepos - best_below_byte); \
300 if (byte_debug_flag) \
301 byte_char_debug_check (b, value, bytepos); \
302 return value; \
303 } \
304 } \
305 }
306
307 ptrdiff_t
308 buf_bytepos_to_charpos (struct buffer *b, ptrdiff_t bytepos)
309 {
310 struct Lisp_Marker *tail;
311 ptrdiff_t best_above, best_above_byte;
312 ptrdiff_t best_below, best_below_byte;
313
314 if (bytepos < BUF_BEG_BYTE (b) || bytepos > BUF_Z_BYTE (b))
315 abort ();
316
317 best_above = BUF_Z (b);
318 best_above_byte = BUF_Z_BYTE (b);
319
320 /* If this buffer has as many characters as bytes,
321 each character must be one byte.
322 This takes care of the case where enable-multibyte-characters is nil. */
323 if (best_above == best_above_byte)
324 return bytepos;
325
326 best_below = BEG;
327 best_below_byte = BEG_BYTE;
328
329 CONSIDER (BUF_PT_BYTE (b), BUF_PT (b));
330 CONSIDER (BUF_GPT_BYTE (b), BUF_GPT (b));
331 CONSIDER (BUF_BEGV_BYTE (b), BUF_BEGV (b));
332 CONSIDER (BUF_ZV_BYTE (b), BUF_ZV (b));
333
334 if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff)
335 CONSIDER (cached_bytepos, cached_charpos);
336
337 for (tail = BUF_MARKERS (b); tail; tail = tail->next)
338 {
339 CONSIDER (tail->bytepos, tail->charpos);
340
341 /* If we are down to a range of 50 chars,
342 don't bother checking any other markers;
343 scan the intervening chars directly now. */
344 if (best_above - best_below < 50)
345 break;
346 }
347
348 /* We get here if we did not exactly hit one of the known places.
349 We have one known above and one known below.
350 Scan, counting characters, from whichever one is closer. */
351
352 if (bytepos - best_below_byte < best_above_byte - bytepos)
353 {
354 int record = bytepos - best_below_byte > 5000;
355
356 while (best_below_byte < bytepos)
357 {
358 best_below++;
359 BUF_INC_POS (b, best_below_byte);
360 }
361
362 /* If this position is quite far from the nearest known position,
363 cache the correspondence by creating a marker here.
364 It will last until the next GC.
365 But don't do it if BUF_MARKERS is nil;
366 that is a signal from Fset_buffer_multibyte. */
367 if (record && BUF_MARKERS (b))
368 {
369 Lisp_Object marker, buffer;
370 marker = Fmake_marker ();
371 XSETBUFFER (buffer, b);
372 set_marker_both (marker, buffer, best_below, best_below_byte);
373 }
374
375 if (byte_debug_flag)
376 byte_char_debug_check (b, best_below, bytepos);
377
378 cached_buffer = b;
379 cached_modiff = BUF_MODIFF (b);
380 cached_charpos = best_below;
381 cached_bytepos = best_below_byte;
382
383 return best_below;
384 }
385 else
386 {
387 int record = best_above_byte - bytepos > 5000;
388
389 while (best_above_byte > bytepos)
390 {
391 best_above--;
392 BUF_DEC_POS (b, best_above_byte);
393 }
394
395 /* If this position is quite far from the nearest known position,
396 cache the correspondence by creating a marker here.
397 It will last until the next GC.
398 But don't do it if BUF_MARKERS is nil;
399 that is a signal from Fset_buffer_multibyte. */
400 if (record && BUF_MARKERS (b))
401 {
402 Lisp_Object marker, buffer;
403 marker = Fmake_marker ();
404 XSETBUFFER (buffer, b);
405 set_marker_both (marker, buffer, best_above, best_above_byte);
406 }
407
408 if (byte_debug_flag)
409 byte_char_debug_check (b, best_above, bytepos);
410
411 cached_buffer = b;
412 cached_modiff = BUF_MODIFF (b);
413 cached_charpos = best_above;
414 cached_bytepos = best_above_byte;
415
416 return best_above;
417 }
418 }
419
420 #undef CONSIDER
421 \f
422 /* Operations on markers. */
423
424 DEFUN ("marker-buffer", Fmarker_buffer, Smarker_buffer, 1, 1, 0,
425 doc: /* Return the buffer that MARKER points into, or nil if none.
426 Returns nil if MARKER points into a dead buffer. */)
427 (register Lisp_Object marker)
428 {
429 register Lisp_Object buf;
430 CHECK_MARKER (marker);
431 if (XMARKER (marker)->buffer)
432 {
433 XSETBUFFER (buf, XMARKER (marker)->buffer);
434 /* If the buffer is dead, we're in trouble: the buffer pointer here
435 does not preserve the buffer from being GC'd (it's weak), so
436 markers have to be unlinked from their buffer as soon as the buffer
437 is killed. */
438 eassert (!NILP (BVAR (XBUFFER (buf), name)));
439 return buf;
440 }
441 return Qnil;
442 }
443
444 DEFUN ("marker-position", Fmarker_position, Smarker_position, 1, 1, 0,
445 doc: /* Return the position MARKER points at, as a character number.
446 Returns nil if MARKER points nowhere. */)
447 (Lisp_Object marker)
448 {
449 CHECK_MARKER (marker);
450 if (XMARKER (marker)->buffer)
451 return make_number (XMARKER (marker)->charpos);
452
453 return Qnil;
454 }
455 \f
456 DEFUN ("set-marker", Fset_marker, Sset_marker, 2, 3, 0,
457 doc: /* Position MARKER before character number POSITION in BUFFER.
458 BUFFER defaults to the current buffer.
459 If POSITION is nil, makes marker point nowhere.
460 Then it no longer slows down editing in any buffer.
461 Returns MARKER. */)
462 (Lisp_Object marker, Lisp_Object position, Lisp_Object buffer)
463 {
464 register ptrdiff_t charno;
465 register ptrdiff_t bytepos;
466 register struct buffer *b;
467 register struct Lisp_Marker *m;
468
469 CHECK_MARKER (marker);
470 m = XMARKER (marker);
471
472 /* If position is nil or a marker that points nowhere,
473 make this marker point nowhere. */
474 if (NILP (position)
475 || (MARKERP (position) && !XMARKER (position)->buffer))
476 {
477 unchain_marker (m);
478 return marker;
479 }
480
481 if (NILP (buffer))
482 b = current_buffer;
483 else
484 {
485 CHECK_BUFFER (buffer);
486 b = XBUFFER (buffer);
487 /* If buffer is dead, set marker to point nowhere. */
488 if (EQ (BVAR (b, name), Qnil))
489 {
490 unchain_marker (m);
491 return marker;
492 }
493 }
494
495 /* Optimize the special case where we are copying the position
496 of an existing marker, and MARKER is already in the same buffer. */
497 if (MARKERP (position) && b == XMARKER (position)->buffer
498 && b == m->buffer)
499 {
500 m->bytepos = XMARKER (position)->bytepos;
501 m->charpos = XMARKER (position)->charpos;
502 return marker;
503 }
504
505 CHECK_NUMBER_COERCE_MARKER (position);
506 charno = clip_to_bounds (BUF_BEG (b), XINT (position), BUF_Z (b));
507 bytepos = buf_charpos_to_bytepos (b, charno);
508
509 /* Every character is at least one byte. */
510 if (charno > bytepos)
511 abort ();
512
513 m->bytepos = bytepos;
514 m->charpos = charno;
515
516 if (m->buffer != b)
517 {
518 unchain_marker (m);
519 m->buffer = b;
520 m->next = BUF_MARKERS (b);
521 BUF_MARKERS (b) = m;
522 }
523
524 return marker;
525 }
526
527 /* This version of Fset_marker won't let the position
528 be outside the visible part. */
529
530 Lisp_Object
531 set_marker_restricted (Lisp_Object marker, Lisp_Object pos, Lisp_Object buffer)
532 {
533 register ptrdiff_t charno;
534 register ptrdiff_t bytepos;
535 register struct buffer *b;
536 register struct Lisp_Marker *m;
537
538 CHECK_MARKER (marker);
539 m = XMARKER (marker);
540
541 /* If position is nil or a marker that points nowhere,
542 make this marker point nowhere. */
543 if (NILP (pos)
544 || (MARKERP (pos) && !XMARKER (pos)->buffer))
545 {
546 unchain_marker (m);
547 return marker;
548 }
549
550 if (NILP (buffer))
551 b = current_buffer;
552 else
553 {
554 CHECK_BUFFER (buffer);
555 b = XBUFFER (buffer);
556 /* If buffer is dead, set marker to point nowhere. */
557 if (EQ (BVAR (b, name), Qnil))
558 {
559 unchain_marker (m);
560 return marker;
561 }
562 }
563
564 /* Optimize the special case where we are copying the position
565 of an existing marker, and MARKER is already in the same buffer. */
566 if (MARKERP (pos) && b == XMARKER (pos)->buffer
567 && b == m->buffer)
568 {
569 m->bytepos = XMARKER (pos)->bytepos;
570 m->charpos = XMARKER (pos)->charpos;
571 return marker;
572 }
573
574 CHECK_NUMBER_COERCE_MARKER (pos);
575 charno = clip_to_bounds (BUF_BEGV (b), XINT (pos), BUF_ZV (b));
576 bytepos = buf_charpos_to_bytepos (b, charno);
577
578 /* Every character is at least one byte. */
579 if (charno > bytepos)
580 abort ();
581
582 m->bytepos = bytepos;
583 m->charpos = charno;
584
585 if (m->buffer != b)
586 {
587 unchain_marker (m);
588 m->buffer = b;
589 m->next = BUF_MARKERS (b);
590 BUF_MARKERS (b) = m;
591 }
592
593 return marker;
594 }
595 \f
596 /* Set the position of MARKER, specifying both the
597 character position and the corresponding byte position. */
598
599 Lisp_Object
600 set_marker_both (Lisp_Object marker, Lisp_Object buffer, ptrdiff_t charpos, ptrdiff_t bytepos)
601 {
602 register struct buffer *b;
603 register struct Lisp_Marker *m;
604
605 CHECK_MARKER (marker);
606 m = XMARKER (marker);
607
608 if (NILP (buffer))
609 b = current_buffer;
610 else
611 {
612 CHECK_BUFFER (buffer);
613 b = XBUFFER (buffer);
614 /* If buffer is dead, set marker to point nowhere. */
615 if (EQ (BVAR (b, name), Qnil))
616 {
617 unchain_marker (m);
618 return marker;
619 }
620 }
621
622 /* In a single-byte buffer, the two positions must be equal. */
623 if (BUF_Z (b) == BUF_Z_BYTE (b)
624 && charpos != bytepos)
625 abort ();
626 /* Every character is at least one byte. */
627 if (charpos > bytepos)
628 abort ();
629
630 m->bytepos = bytepos;
631 m->charpos = charpos;
632
633 if (m->buffer != b)
634 {
635 unchain_marker (m);
636 m->buffer = b;
637 m->next = BUF_MARKERS (b);
638 BUF_MARKERS (b) = m;
639 }
640
641 return marker;
642 }
643
644 /* This version of set_marker_both won't let the position
645 be outside the visible part. */
646
647 Lisp_Object
648 set_marker_restricted_both (Lisp_Object marker, Lisp_Object buffer, ptrdiff_t charpos, ptrdiff_t bytepos)
649 {
650 register struct buffer *b;
651 register struct Lisp_Marker *m;
652
653 CHECK_MARKER (marker);
654 m = XMARKER (marker);
655
656 if (NILP (buffer))
657 b = current_buffer;
658 else
659 {
660 CHECK_BUFFER (buffer);
661 b = XBUFFER (buffer);
662 /* If buffer is dead, set marker to point nowhere. */
663 if (EQ (BVAR (b, name), Qnil))
664 {
665 unchain_marker (m);
666 return marker;
667 }
668 }
669
670 if (charpos < BUF_BEGV (b))
671 charpos = BUF_BEGV (b);
672 if (charpos > BUF_ZV (b))
673 charpos = BUF_ZV (b);
674 if (bytepos < BUF_BEGV_BYTE (b))
675 bytepos = BUF_BEGV_BYTE (b);
676 if (bytepos > BUF_ZV_BYTE (b))
677 bytepos = BUF_ZV_BYTE (b);
678
679 /* In a single-byte buffer, the two positions must be equal. */
680 if (BUF_Z (b) == BUF_Z_BYTE (b)
681 && charpos != bytepos)
682 abort ();
683 /* Every character is at least one byte. */
684 if (charpos > bytepos)
685 abort ();
686
687 m->bytepos = bytepos;
688 m->charpos = charpos;
689
690 if (m->buffer != b)
691 {
692 unchain_marker (m);
693 m->buffer = b;
694 m->next = BUF_MARKERS (b);
695 BUF_MARKERS (b) = m;
696 }
697
698 return marker;
699 }
700 \f
701 /* Remove MARKER from the chain of whatever buffer it is in.
702 Leave it "in no buffer".
703
704 This is called during garbage collection,
705 so we must be careful to ignore and preserve mark bits,
706 including those in chain fields of markers. */
707
708 void
709 unchain_marker (register struct Lisp_Marker *marker)
710 {
711 register struct Lisp_Marker *tail, *prev, *next;
712 register struct buffer *b;
713
714 b = marker->buffer;
715 if (b == 0)
716 return;
717
718 if (EQ (BVAR (b, name), Qnil))
719 abort ();
720
721 marker->buffer = 0;
722
723 tail = BUF_MARKERS (b);
724 prev = NULL;
725 while (tail)
726 {
727 next = tail->next;
728
729 if (marker == tail)
730 {
731 if (!prev)
732 {
733 BUF_MARKERS (b) = next;
734 /* Deleting first marker from the buffer's chain. Crash
735 if new first marker in chain does not say it belongs
736 to the same buffer, or at least that they have the same
737 base buffer. */
738 if (next && b->text != next->buffer->text)
739 abort ();
740 }
741 else
742 prev->next = next;
743 /* We have removed the marker from the chain;
744 no need to scan the rest of the chain. */
745 return;
746 }
747 else
748 prev = tail;
749 tail = next;
750 }
751
752 /* Marker was not in its chain. */
753 abort ();
754 }
755
756 /* Return the char position of marker MARKER, as a C integer. */
757
758 ptrdiff_t
759 marker_position (Lisp_Object marker)
760 {
761 register struct Lisp_Marker *m = XMARKER (marker);
762 register struct buffer *buf = m->buffer;
763
764 if (!buf)
765 error ("Marker does not point anywhere");
766
767 return m->charpos;
768 }
769
770 /* Return the byte position of marker MARKER, as a C integer. */
771
772 ptrdiff_t
773 marker_byte_position (Lisp_Object marker)
774 {
775 register struct Lisp_Marker *m = XMARKER (marker);
776 register struct buffer *buf = m->buffer;
777 register ptrdiff_t i = m->bytepos;
778
779 if (!buf)
780 error ("Marker does not point anywhere");
781
782 if (i < BUF_BEG_BYTE (buf) || i > BUF_Z_BYTE (buf))
783 abort ();
784
785 return i;
786 }
787 \f
788 DEFUN ("copy-marker", Fcopy_marker, Scopy_marker, 0, 2, 0,
789 doc: /* Return a new marker pointing at the same place as MARKER.
790 If argument is a number, makes a new marker pointing
791 at that position in the current buffer.
792 If MARKER is not specified, the new marker does not point anywhere.
793 The optional argument TYPE specifies the insertion type of the new marker;
794 see `marker-insertion-type'. */)
795 (register Lisp_Object marker, Lisp_Object type)
796 {
797 register Lisp_Object new;
798
799 if (!NILP (marker))
800 CHECK_TYPE (INTEGERP (marker) || MARKERP (marker), Qinteger_or_marker_p, marker);
801
802 new = Fmake_marker ();
803 Fset_marker (new, marker,
804 (MARKERP (marker) ? Fmarker_buffer (marker) : Qnil));
805 XMARKER (new)->insertion_type = !NILP (type);
806 return new;
807 }
808
809 DEFUN ("marker-insertion-type", Fmarker_insertion_type,
810 Smarker_insertion_type, 1, 1, 0,
811 doc: /* Return insertion type of MARKER: t if it stays after inserted text.
812 The value nil means the marker stays before text inserted there. */)
813 (register Lisp_Object marker)
814 {
815 CHECK_MARKER (marker);
816 return XMARKER (marker)->insertion_type ? Qt : Qnil;
817 }
818
819 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type,
820 Sset_marker_insertion_type, 2, 2, 0,
821 doc: /* Set the insertion-type of MARKER to TYPE.
822 If TYPE is t, it means the marker advances when you insert text at it.
823 If TYPE is nil, it means the marker stays behind when you insert text at it. */)
824 (Lisp_Object marker, Lisp_Object type)
825 {
826 CHECK_MARKER (marker);
827
828 XMARKER (marker)->insertion_type = ! NILP (type);
829 return type;
830 }
831
832 DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at, Sbuffer_has_markers_at,
833 1, 1, 0,
834 doc: /* Return t if there are markers pointing at POSITION in the current buffer. */)
835 (Lisp_Object position)
836 {
837 register struct Lisp_Marker *tail;
838 register ptrdiff_t charno;
839
840 charno = clip_to_bounds (BEG, XINT (position), Z);
841
842 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
843 if (tail->charpos == charno)
844 return Qt;
845
846 return Qnil;
847 }
848
849 /* For debugging -- count the markers in buffer BUF. */
850
851 extern int count_markers (struct buffer *) EXTERNALLY_VISIBLE;
852 int
853 count_markers (struct buffer *buf)
854 {
855 int total = 0;
856 struct Lisp_Marker *tail;
857
858 for (tail = BUF_MARKERS (buf); tail; tail = tail->next)
859 total++;
860
861 return total;
862 }
863 \f
864 void
865 syms_of_marker (void)
866 {
867 defsubr (&Smarker_position);
868 defsubr (&Smarker_buffer);
869 defsubr (&Sset_marker);
870 defsubr (&Scopy_marker);
871 defsubr (&Smarker_insertion_type);
872 defsubr (&Sset_marker_insertion_type);
873 defsubr (&Sbuffer_has_markers_at);
874
875 DEFVAR_BOOL ("byte-debug-flag", byte_debug_flag,
876 doc: /* Non-nil enables debugging checks in byte/char position conversions. */);
877 byte_debug_flag = 0;
878 }