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