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