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