428
|
1 /* Markers: examining, setting and killing.
|
|
2 Copyright (C) 1985, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
|
800
|
3 Copyright (C) 2002 Ben Wing.
|
428
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: FSF 19.30. */
|
|
23
|
|
24 /* This file has been Mule-ized. */
|
|
25
|
|
26 /* Note that markers are currently kept in an unordered list.
|
|
27 This means that marker operations may be inefficient if
|
|
28 there are a bunch of markers in the buffer. This probably
|
|
29 won't have a significant impact on redisplay (which uses
|
|
30 markers), but if it does, it wouldn't be too hard to change
|
|
31 to an ordered gap array. (Just copy the code from extents.c.)
|
|
32 */
|
|
33
|
|
34 #include <config.h>
|
|
35 #include "lisp.h"
|
|
36
|
|
37 #include "buffer.h"
|
|
38
|
|
39 static Lisp_Object
|
|
40 mark_marker (Lisp_Object obj)
|
|
41 {
|
440
|
42 Lisp_Marker *marker = XMARKER (obj);
|
428
|
43 Lisp_Object buf;
|
|
44 /* DO NOT mark through the marker's chain.
|
|
45 The buffer's markers chain does not preserve markers from gc;
|
|
46 Instead, markers are removed from the chain when they are freed
|
|
47 by gc.
|
|
48 */
|
|
49 if (!marker->buffer)
|
|
50 return (Qnil);
|
|
51
|
793
|
52 buf = wrap_buffer (marker->buffer);
|
428
|
53 return (buf);
|
|
54 }
|
|
55
|
|
56 static void
|
|
57 print_marker (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
58 {
|
440
|
59 Lisp_Marker *marker = XMARKER (obj);
|
428
|
60
|
|
61 if (print_readably)
|
563
|
62 printing_unreadable_object ("#<marker 0x%lx>", (long) marker);
|
428
|
63
|
|
64 write_c_string (GETTEXT ("#<marker "), printcharfun);
|
|
65 if (!marker->buffer)
|
|
66 write_c_string (GETTEXT ("in no buffer"), printcharfun);
|
|
67 else
|
|
68 {
|
800
|
69 write_fmt_string (printcharfun, "at %ld in ", (long) marker_position (obj));
|
428
|
70 print_internal (marker->buffer->name, printcharfun, 0);
|
|
71 }
|
800
|
72 write_fmt_string (printcharfun, " 0x%lx>", (long) marker);
|
428
|
73 }
|
|
74
|
|
75 static int
|
|
76 marker_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
77 {
|
440
|
78 Lisp_Marker *marker1 = XMARKER (obj1);
|
|
79 Lisp_Marker *marker2 = XMARKER (obj2);
|
428
|
80
|
|
81 return ((marker1->buffer == marker2->buffer) &&
|
665
|
82 (marker1->membpos == marker2->membpos ||
|
428
|
83 /* All markers pointing nowhere are equal */
|
|
84 !marker1->buffer));
|
|
85 }
|
|
86
|
|
87 static unsigned long
|
|
88 marker_hash (Lisp_Object obj, int depth)
|
|
89 {
|
|
90 unsigned long hash = (unsigned long) XMARKER (obj)->buffer;
|
|
91 if (hash)
|
665
|
92 hash = HASH2 (hash, XMARKER (obj)->membpos);
|
428
|
93 return hash;
|
|
94 }
|
|
95
|
|
96 static const struct lrecord_description marker_description[] = {
|
440
|
97 { XD_LISP_OBJECT, offsetof (Lisp_Marker, next) },
|
|
98 { XD_LISP_OBJECT, offsetof (Lisp_Marker, prev) },
|
|
99 { XD_LISP_OBJECT, offsetof (Lisp_Marker, buffer) },
|
428
|
100 { XD_END }
|
|
101 };
|
|
102
|
|
103 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("marker", marker,
|
|
104 mark_marker, print_marker, 0,
|
|
105 marker_equal, marker_hash, marker_description,
|
440
|
106 Lisp_Marker);
|
428
|
107
|
|
108 /* Operations on markers. */
|
|
109
|
|
110 DEFUN ("marker-buffer", Fmarker_buffer, 1, 1, 0, /*
|
|
111 Return the buffer that MARKER points into, or nil if none.
|
|
112 Return nil if MARKER points into a dead buffer or doesn't point anywhere.
|
|
113 */
|
|
114 (marker))
|
|
115 {
|
|
116 struct buffer *buf;
|
|
117 CHECK_MARKER (marker);
|
|
118 /* Return marker's buffer only if it is not dead. */
|
|
119 if ((buf = XMARKER (marker)->buffer) && BUFFER_LIVE_P (buf))
|
|
120 {
|
793
|
121 return wrap_buffer (buf);
|
428
|
122 }
|
|
123 return Qnil;
|
|
124 }
|
|
125
|
|
126 DEFUN ("marker-position", Fmarker_position, 1, 1, 0, /*
|
|
127 Return the position MARKER points at, as a character number.
|
|
128 Return `nil' if marker doesn't point anywhere.
|
|
129 */
|
|
130 (marker))
|
|
131 {
|
|
132 CHECK_MARKER (marker);
|
|
133 return XMARKER (marker)->buffer ? make_int (marker_position (marker)) : Qnil;
|
|
134 }
|
|
135
|
|
136 #if 0 /* useful debugging function */
|
|
137
|
|
138 static void
|
|
139 check_marker_circularities (struct buffer *buf)
|
|
140 {
|
440
|
141 Lisp_Marker *tortoise, *hare;
|
428
|
142
|
|
143 tortoise = BUF_MARKERS (buf);
|
|
144 hare = tortoise;
|
|
145
|
|
146 if (!tortoise)
|
|
147 return;
|
|
148
|
|
149 while (1)
|
|
150 {
|
|
151 assert (hare->buffer == buf);
|
|
152 hare = hare->next;
|
|
153 if (!hare)
|
|
154 return;
|
|
155 assert (hare->buffer == buf);
|
|
156 hare = hare->next;
|
|
157 if (!hare)
|
|
158 return;
|
|
159 tortoise = tortoise->next;
|
|
160 assert (tortoise != hare);
|
|
161 }
|
|
162 }
|
|
163
|
|
164 #endif
|
|
165
|
|
166 static Lisp_Object
|
444
|
167 set_marker_internal (Lisp_Object marker, Lisp_Object position,
|
|
168 Lisp_Object buffer, int restricted_p)
|
428
|
169 {
|
665
|
170 Charbpos charno;
|
428
|
171 struct buffer *b;
|
440
|
172 Lisp_Marker *m;
|
428
|
173 int point_p;
|
|
174
|
|
175 CHECK_MARKER (marker);
|
|
176
|
|
177 point_p = POINT_MARKER_P (marker);
|
|
178
|
|
179 /* If position is nil or a marker that points nowhere,
|
|
180 make this marker point nowhere. */
|
444
|
181 if (NILP (position) ||
|
|
182 (MARKERP (position) && !XMARKER (position)->buffer))
|
428
|
183 {
|
|
184 if (point_p)
|
563
|
185 invalid_operation ("Can't make point-marker point nowhere",
|
|
186 marker);
|
428
|
187 if (XMARKER (marker)->buffer)
|
|
188 unchain_marker (marker);
|
|
189 return marker;
|
|
190 }
|
|
191
|
444
|
192 CHECK_INT_COERCE_MARKER (position);
|
428
|
193 if (NILP (buffer))
|
|
194 b = current_buffer;
|
|
195 else
|
|
196 {
|
|
197 CHECK_BUFFER (buffer);
|
|
198 b = XBUFFER (buffer);
|
|
199 /* If buffer is dead, set marker to point nowhere. */
|
|
200 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
|
|
201 {
|
|
202 if (point_p)
|
563
|
203 invalid_operation
|
428
|
204 ("Can't move point-marker in a killed buffer", marker);
|
|
205 if (XMARKER (marker)->buffer)
|
|
206 unchain_marker (marker);
|
|
207 return marker;
|
|
208 }
|
|
209 }
|
|
210
|
444
|
211 charno = XINT (position);
|
428
|
212 m = XMARKER (marker);
|
|
213
|
|
214 if (restricted_p)
|
|
215 {
|
|
216 if (charno < BUF_BEGV (b)) charno = BUF_BEGV (b);
|
|
217 if (charno > BUF_ZV (b)) charno = BUF_ZV (b);
|
|
218 }
|
|
219 else
|
|
220 {
|
|
221 if (charno < BUF_BEG (b)) charno = BUF_BEG (b);
|
|
222 if (charno > BUF_Z (b)) charno = BUF_Z (b);
|
|
223 }
|
|
224
|
|
225 if (point_p)
|
|
226 {
|
|
227 #ifndef moving_point_by_moving_its_marker_is_a_bug
|
|
228 BUF_SET_PT (b, charno); /* this will move the marker */
|
|
229 #else /* It's not a feature, so it must be a bug */
|
563
|
230 invalid_operation ("DEBUG: attempt to move point via point-marker",
|
|
231 marker);
|
428
|
232 #endif
|
|
233 }
|
|
234 else
|
|
235 {
|
665
|
236 m->membpos = charbpos_to_membpos (b, charno);
|
428
|
237 }
|
|
238
|
|
239 if (m->buffer != b)
|
|
240 {
|
|
241 if (point_p)
|
563
|
242 invalid_operation ("Can't change buffer of point-marker", marker);
|
428
|
243 if (m->buffer != 0)
|
|
244 unchain_marker (marker);
|
|
245 m->buffer = b;
|
|
246 marker_next (m) = BUF_MARKERS (b);
|
|
247 marker_prev (m) = 0;
|
|
248 if (BUF_MARKERS (b))
|
|
249 marker_prev (BUF_MARKERS (b)) = m;
|
|
250 BUF_MARKERS (b) = m;
|
|
251 }
|
|
252
|
|
253 return marker;
|
|
254 }
|
|
255
|
|
256
|
|
257 DEFUN ("set-marker", Fset_marker, 2, 3, 0, /*
|
444
|
258 Move MARKER to position POSITION in BUFFER.
|
|
259 POSITION can be a marker, an integer or nil. If POSITION is an
|
|
260 integer, make MARKER point before the POSITIONth character in BUFFER.
|
|
261 If POSITION is nil, makes MARKER point nowhere. Then it no longer
|
|
262 slows down editing in any buffer. If POSITION is less than 1, move
|
|
263 MARKER to the beginning of BUFFER. If POSITION is greater than the
|
|
264 size of BUFFER, move MARKER to the end of BUFFER.
|
428
|
265 BUFFER defaults to the current buffer.
|
444
|
266 If this marker was returned by (point-marker t), then changing its
|
|
267 position moves point. You cannot change its buffer or make it point
|
|
268 nowhere.
|
|
269 The return value is MARKER.
|
428
|
270 */
|
444
|
271 (marker, position, buffer))
|
428
|
272 {
|
444
|
273 return set_marker_internal (marker, position, buffer, 0);
|
428
|
274 }
|
|
275
|
|
276
|
|
277 /* This version of Fset_marker won't let the position
|
|
278 be outside the visible part. */
|
|
279 Lisp_Object
|
444
|
280 set_marker_restricted (Lisp_Object marker, Lisp_Object position,
|
|
281 Lisp_Object buffer)
|
428
|
282 {
|
444
|
283 return set_marker_internal (marker, position, buffer, 1);
|
428
|
284 }
|
|
285
|
|
286
|
|
287 /* This is called during garbage collection,
|
|
288 so we must be careful to ignore and preserve mark bits,
|
|
289 including those in chain fields of markers. */
|
|
290
|
|
291 void
|
|
292 unchain_marker (Lisp_Object m)
|
|
293 {
|
440
|
294 Lisp_Marker *marker = XMARKER (m);
|
428
|
295 struct buffer *b = marker->buffer;
|
|
296
|
|
297 if (b == 0)
|
|
298 return;
|
|
299
|
800
|
300 #ifdef ERROR_CHECK_STRUCTURES
|
428
|
301 assert (BUFFER_LIVE_P (b));
|
|
302 #endif
|
|
303
|
|
304 if (marker_next (marker))
|
|
305 marker_prev (marker_next (marker)) = marker_prev (marker);
|
|
306 if (marker_prev (marker))
|
|
307 marker_next (marker_prev (marker)) = marker_next (marker);
|
|
308 else
|
|
309 BUF_MARKERS (b) = marker_next (marker);
|
|
310
|
800
|
311 #ifdef ERROR_CHECK_STRUCTURES
|
428
|
312 assert (marker != XMARKER (b->point_marker));
|
|
313 #endif
|
|
314
|
|
315 marker->buffer = 0;
|
|
316 }
|
|
317
|
665
|
318 Bytebpos
|
428
|
319 bi_marker_position (Lisp_Object marker)
|
|
320 {
|
440
|
321 Lisp_Marker *m = XMARKER (marker);
|
428
|
322 struct buffer *buf = m->buffer;
|
665
|
323 Bytebpos pos;
|
428
|
324
|
|
325 if (!buf)
|
563
|
326 invalid_argument ("Marker does not point anywhere", Qunbound);
|
428
|
327
|
|
328 /* FSF claims that marker indices could end up denormalized, i.e.
|
|
329 in the gap. This is way bogus if it ever happens, and means
|
|
330 something fucked up elsewhere. Since I've overhauled all this
|
|
331 shit, I don't think this can happen. In any case, the following
|
|
332 macro has an assert() in it that will catch these denormalized
|
|
333 positions. */
|
665
|
334 pos = membpos_to_bytebpos (buf, m->membpos);
|
428
|
335
|
800
|
336 #ifdef ERROR_CHECK_TEXT
|
428
|
337 if (pos < BI_BUF_BEG (buf) || pos > BI_BUF_Z (buf))
|
|
338 abort ();
|
|
339 #endif
|
|
340
|
|
341 return pos;
|
|
342 }
|
|
343
|
665
|
344 Charbpos
|
428
|
345 marker_position (Lisp_Object marker)
|
|
346 {
|
|
347 struct buffer *buf = XMARKER (marker)->buffer;
|
|
348
|
|
349 if (!buf)
|
563
|
350 invalid_argument ("Marker does not point anywhere", Qunbound);
|
428
|
351
|
665
|
352 return bytebpos_to_charbpos (buf, bi_marker_position (marker));
|
428
|
353 }
|
|
354
|
|
355 void
|
665
|
356 set_bi_marker_position (Lisp_Object marker, Bytebpos pos)
|
428
|
357 {
|
440
|
358 Lisp_Marker *m = XMARKER (marker);
|
428
|
359 struct buffer *buf = m->buffer;
|
|
360
|
|
361 if (!buf)
|
563
|
362 invalid_argument ("Marker does not point anywhere", Qunbound);
|
428
|
363
|
800
|
364 #ifdef ERROR_CHECK_TEXT
|
428
|
365 if (pos < BI_BUF_BEG (buf) || pos > BI_BUF_Z (buf))
|
|
366 abort ();
|
|
367 #endif
|
|
368
|
665
|
369 m->membpos = bytebpos_to_membpos (buf, pos);
|
428
|
370 }
|
|
371
|
|
372 void
|
665
|
373 set_marker_position (Lisp_Object marker, Charbpos pos)
|
428
|
374 {
|
|
375 struct buffer *buf = XMARKER (marker)->buffer;
|
|
376
|
|
377 if (!buf)
|
563
|
378 invalid_argument ("Marker does not point anywhere", Qunbound);
|
428
|
379
|
665
|
380 set_bi_marker_position (marker, charbpos_to_bytebpos (buf, pos));
|
428
|
381 }
|
|
382
|
|
383 static Lisp_Object
|
|
384 copy_marker_1 (Lisp_Object marker, Lisp_Object type, int noseeum)
|
|
385 {
|
|
386 REGISTER Lisp_Object new;
|
|
387
|
|
388 while (1)
|
|
389 {
|
|
390 if (INTP (marker) || MARKERP (marker))
|
|
391 {
|
|
392 if (noseeum)
|
|
393 new = noseeum_make_marker ();
|
|
394 else
|
|
395 new = Fmake_marker ();
|
|
396 Fset_marker (new, marker,
|
|
397 (MARKERP (marker) ? Fmarker_buffer (marker) : Qnil));
|
|
398 XMARKER (new)->insertion_type = !NILP (type);
|
|
399 return new;
|
|
400 }
|
|
401 else
|
|
402 marker = wrong_type_argument (Qinteger_or_marker_p, marker);
|
|
403 }
|
|
404
|
|
405 RETURN_NOT_REACHED (Qnil) /* not reached */
|
|
406 }
|
|
407
|
|
408 DEFUN ("copy-marker", Fcopy_marker, 1, 2, 0, /*
|
444
|
409 Return a new marker pointing at the same place as MARKER-OR-INTEGER.
|
|
410 If MARKER-OR-INTEGER is an integer, return a new marker pointing
|
428
|
411 at that position in the current buffer.
|
444
|
412 Optional argument MARKER-TYPE specifies the insertion type of the new
|
|
413 marker; see `marker-insertion-type'.
|
428
|
414 */
|
444
|
415 (marker_or_integer, marker_type))
|
428
|
416 {
|
444
|
417 return copy_marker_1 (marker_or_integer, marker_type, 0);
|
428
|
418 }
|
|
419
|
|
420 Lisp_Object
|
444
|
421 noseeum_copy_marker (Lisp_Object marker, Lisp_Object marker_type)
|
428
|
422 {
|
444
|
423 return copy_marker_1 (marker, marker_type, 1);
|
428
|
424 }
|
|
425
|
|
426 DEFUN ("marker-insertion-type", Fmarker_insertion_type, 1, 1, 0, /*
|
|
427 Return insertion type of MARKER: t if it stays after inserted text.
|
|
428 nil means the marker stays before text inserted there.
|
|
429 */
|
|
430 (marker))
|
|
431 {
|
|
432 CHECK_MARKER (marker);
|
|
433 return XMARKER (marker)->insertion_type ? Qt : Qnil;
|
|
434 }
|
|
435
|
|
436 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type, 2, 2, 0, /*
|
|
437 Set the insertion-type of MARKER to TYPE.
|
|
438 If TYPE is t, it means the marker advances when you insert text at it.
|
|
439 If TYPE is nil, it means the marker stays behind when you insert text at it.
|
|
440 */
|
|
441 (marker, type))
|
|
442 {
|
|
443 CHECK_MARKER (marker);
|
|
444
|
|
445 XMARKER (marker)->insertion_type = ! NILP (type);
|
|
446 return type;
|
|
447 }
|
|
448
|
|
449 /* #### What is the possible use of this? It looks quite useless to
|
|
450 me, because there is no way to find *which* markers are positioned
|
|
451 at POSITION. Additional bogosity bonus: (buffer-has-markers-at
|
|
452 (point)) will always return t because of the `point-marker'. The
|
|
453 same goes for the position of mark. Bletch!
|
|
454
|
|
455 Someone should discuss this with Stallman, but I don't have the
|
|
456 stomach. In fact, this function sucks so badly that I'm disabling
|
|
457 it by default (although I've debugged it). If you want to use it,
|
|
458 use extents instead. --hniksic */
|
|
459 #if 0
|
|
460 xxDEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at, 1, 1, 0, /*
|
|
461 Return t if there are markers pointing at POSITION in the current buffer.
|
|
462 */
|
|
463 (position))
|
|
464 {
|
440
|
465 Lisp_Marker *marker;
|
665
|
466 Membpos pos;
|
428
|
467
|
665
|
468 /* A small optimization trick: convert POS to membpos now, rather
|
|
469 than converting every marker's memory index to charbpos. */
|
|
470 pos = bytebpos_to_membpos (current_buffer,
|
428
|
471 get_buffer_pos_byte (current_buffer, position,
|
|
472 GB_COERCE_RANGE));
|
|
473
|
|
474 for (marker = BUF_MARKERS (current_buffer);
|
|
475 marker;
|
|
476 marker = marker_next (marker))
|
|
477 {
|
665
|
478 /* We use marker->membpos, so we don't have to go through the
|
428
|
479 unwieldy operation of creating a Lisp_Object for
|
|
480 marker_position() every time around. */
|
665
|
481 if (marker->membpos == pos)
|
428
|
482 return Qt;
|
|
483 }
|
|
484
|
|
485 return Qnil;
|
|
486 }
|
|
487 #endif /* 0 */
|
|
488
|
|
489 #ifdef MEMORY_USAGE_STATS
|
|
490
|
|
491 int
|
|
492 compute_buffer_marker_usage (struct buffer *b, struct overhead_stats *ovstats)
|
|
493 {
|
440
|
494 Lisp_Marker *m;
|
428
|
495 int total = 0;
|
|
496 int overhead;
|
|
497
|
|
498 for (m = BUF_MARKERS (b); m; m = m->next)
|
440
|
499 total += sizeof (Lisp_Marker);
|
428
|
500 ovstats->was_requested += total;
|
|
501 overhead = fixed_type_block_overhead (total);
|
|
502 /* #### claiming this is all malloc overhead is not really right,
|
|
503 but it has to go somewhere. */
|
|
504 ovstats->malloc_overhead += overhead;
|
|
505 return total + overhead;
|
|
506 }
|
|
507
|
|
508 #endif /* MEMORY_USAGE_STATS */
|
|
509
|
|
510
|
|
511 void
|
|
512 syms_of_marker (void)
|
|
513 {
|
442
|
514 INIT_LRECORD_IMPLEMENTATION (marker);
|
|
515
|
428
|
516 DEFSUBR (Fmarker_position);
|
|
517 DEFSUBR (Fmarker_buffer);
|
|
518 DEFSUBR (Fset_marker);
|
|
519 DEFSUBR (Fcopy_marker);
|
|
520 DEFSUBR (Fmarker_insertion_type);
|
|
521 DEFSUBR (Fset_marker_insertion_type);
|
|
522 #if 0 /* FSFmacs crock */
|
|
523 DEFSUBR (Fbuffer_has_markers_at);
|
|
524 #endif
|
|
525 }
|
|
526
|
|
527 void
|
|
528 init_buffer_markers (struct buffer *b)
|
|
529 {
|
793
|
530 Lisp_Object buf = wrap_buffer (b);
|
428
|
531
|
|
532 b->mark = Fmake_marker ();
|
|
533 BUF_MARKERS (b) = 0;
|
|
534 b->point_marker = Fmake_marker ();
|
|
535 Fset_marker (b->point_marker,
|
|
536 /* For indirect buffers, point is already set. */
|
|
537 b->base_buffer ? make_int (BUF_PT (b)) : make_int (1),
|
|
538 buf);
|
|
539 }
|
|
540
|
|
541 void
|
|
542 uninit_buffer_markers (struct buffer *b)
|
|
543 {
|
|
544 /* Unchain all markers of this buffer
|
|
545 and leave them pointing nowhere. */
|
440
|
546 REGISTER Lisp_Marker *m, *next;
|
428
|
547 for (m = BUF_MARKERS (b); m; m = next)
|
|
548 {
|
|
549 m->buffer = 0;
|
|
550 next = marker_next (m);
|
|
551 marker_next (m) = 0;
|
|
552 marker_prev (m) = 0;
|
|
553 }
|
|
554 BUF_MARKERS (b) = 0;
|
|
555 }
|