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