0
|
1 /* Markers: examining, setting and killing.
|
|
2 Copyright (C) 1985, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.30. */
|
|
22
|
|
23 /* This file has been Mule-ized. */
|
|
24
|
|
25 /* Note that markers are currently kept in an unordered list.
|
|
26 This means that marker operations may be inefficient if
|
|
27 there are a bunch of markers in the buffer. This probably
|
|
28 won't have a significant impact on redisplay (which uses
|
|
29 markers), but if it does, it wouldn't be too hard to change
|
|
30 to an ordered gap array. (Just copy the code from extents.c.)
|
|
31 */
|
|
32
|
|
33 #include <config.h>
|
|
34 #include "lisp.h"
|
|
35
|
|
36 #include "buffer.h"
|
|
37
|
|
38 static Lisp_Object mark_marker (Lisp_Object, void (*) (Lisp_Object));
|
|
39 static void print_marker (Lisp_Object, Lisp_Object, int);
|
|
40 static int marker_equal (Lisp_Object, Lisp_Object, int);
|
|
41 static unsigned long marker_hash (Lisp_Object obj, int depth);
|
|
42 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("marker", marker,
|
|
43 mark_marker, print_marker, 0,
|
|
44 marker_equal, marker_hash,
|
|
45 struct Lisp_Marker);
|
|
46
|
|
47 static Lisp_Object
|
|
48 mark_marker (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
49 {
|
|
50 struct Lisp_Marker *marker = XMARKER (obj);
|
|
51 Lisp_Object buf;
|
|
52 /* DO NOT mark through the marker's chain.
|
|
53 The buffer's markers chain does not preserve markers from gc;
|
|
54 Instead, markers are removed from the chain when they are freed
|
|
55 by gc.
|
|
56 */
|
|
57 if (!marker->buffer)
|
|
58 return (Qnil);
|
|
59
|
|
60 XSETBUFFER (buf, marker->buffer);
|
|
61 return (buf);
|
|
62 }
|
|
63
|
|
64 static void
|
|
65 print_marker (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
66 {
|
|
67 if (print_readably)
|
|
68 error ("printing unreadable object #<marker>");
|
|
69
|
|
70 write_c_string (GETTEXT ("#<marker "), printcharfun);
|
|
71 if (!(XMARKER (obj)->buffer))
|
|
72 write_c_string (GETTEXT ("in no buffer"), printcharfun);
|
|
73 else
|
|
74 {
|
|
75 char buf[200];
|
|
76 sprintf (buf, "at %d", marker_position (obj));
|
|
77 write_c_string (buf, printcharfun);
|
|
78 write_c_string (" in ", printcharfun);
|
|
79 print_internal (XMARKER (obj)->buffer->name, printcharfun, 0);
|
|
80 }
|
|
81 write_c_string (">", printcharfun);
|
|
82 }
|
|
83
|
|
84 static int
|
|
85 marker_equal (Lisp_Object o1, Lisp_Object o2, int depth)
|
|
86 {
|
|
87 struct buffer *b1 = XMARKER (o1)->buffer;
|
|
88 if (b1 != XMARKER (o2)->buffer)
|
|
89 return (0);
|
|
90 else if (!b1)
|
|
91 /* All markers pointing nowhere are equal */
|
|
92 return (1);
|
|
93 else
|
|
94 return ((XMARKER (o1)->memind == XMARKER (o2)->memind));
|
|
95 }
|
|
96
|
|
97 static unsigned long
|
|
98 marker_hash (Lisp_Object obj, int depth)
|
|
99 {
|
|
100 unsigned long hash = (unsigned long) XMARKER (obj)->buffer;
|
|
101 if (hash)
|
|
102 hash = HASH2 (hash, XMARKER (obj)->memind);
|
|
103 return hash;
|
|
104 }
|
|
105
|
|
106
|
|
107 /* Operations on markers. */
|
|
108
|
20
|
109 DEFUN ("marker-buffer", Fmarker_buffer, 1, 1, 0, /*
|
0
|
110 Return the buffer that MARKER points into, or nil if none.
|
|
111 Returns nil if MARKER points into a dead buffer.
|
20
|
112 */
|
|
113 (marker))
|
0
|
114 {
|
|
115 Lisp_Object buf;
|
|
116 CHECK_MARKER (marker);
|
|
117 if (XMARKER (marker)->buffer)
|
|
118 {
|
|
119 XSETBUFFER (buf, XMARKER (marker)->buffer);
|
|
120 /* Return marker's buffer only if it is not dead. */
|
|
121 if (BUFFER_LIVE_P (XBUFFER (buf)))
|
|
122 return buf;
|
|
123 }
|
|
124 return Qnil;
|
|
125 }
|
|
126
|
20
|
127 DEFUN ("marker-position", Fmarker_position, 1, 1, 0, /*
|
0
|
128 Return the position MARKER points at, as a character number.
|
|
129 Returns `nil' if marker doesn't point anywhere.
|
20
|
130 */
|
|
131 (marker))
|
0
|
132 {
|
|
133 CHECK_MARKER (marker);
|
|
134 if (XMARKER (marker)->buffer)
|
|
135 {
|
|
136 return (make_int (marker_position (marker)));
|
|
137 }
|
|
138 return Qnil;
|
|
139 }
|
|
140
|
|
141 #if 0 /* useful debugging function */
|
|
142
|
|
143 static void
|
|
144 check_marker_circularities (struct buffer *buf)
|
|
145 {
|
|
146 struct Lisp_Marker *tortoise, *hare;
|
|
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
|
|
172 set_marker_internal (Lisp_Object marker, Lisp_Object pos, Lisp_Object buffer,
|
|
173 int restricted_p)
|
|
174 {
|
|
175 Bufpos charno;
|
|
176 struct buffer *b;
|
|
177 struct Lisp_Marker *m;
|
|
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. */
|
|
186 if (NILP (pos) ||
|
|
187 (MARKERP (pos) && !XMARKER (pos)->buffer))
|
|
188 {
|
|
189 if (point_p)
|
|
190 signal_simple_error ("can't make point-marker point nowhere",
|
|
191 marker);
|
|
192 if (XMARKER (marker)->buffer)
|
|
193 unchain_marker (marker);
|
|
194 return marker;
|
|
195 }
|
|
196
|
|
197 CHECK_INT_COERCE_MARKER (pos);
|
|
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)
|
|
208 signal_simple_error
|
|
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
|
|
216 charno = XINT (pos);
|
|
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 */
|
|
235 signal_simple_error ("DEBUG: attempt to move point via point-marker",
|
|
236 marker);
|
|
237 #endif
|
|
238 }
|
|
239 else
|
|
240 {
|
|
241 m->memind = bufpos_to_memind (b, charno);
|
|
242 }
|
|
243
|
|
244 if (m->buffer != b)
|
|
245 {
|
|
246 if (point_p)
|
|
247 signal_simple_error ("can't change buffer of point-marker", marker);
|
|
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
|
20
|
262 DEFUN ("set-marker", Fset_marker, 2, 3, 0, /*
|
0
|
263 Position MARKER before character number NUMBER in BUFFER.
|
|
264 BUFFER defaults to the current buffer.
|
|
265 If NUMBER is nil, makes marker point nowhere.
|
|
266 Then it no longer slows down editing in any buffer.
|
|
267 If this marker was returned by (point-marker t), then changing its position
|
|
268 moves point. You cannot change its buffer or make it point nowhere.
|
|
269 Returns MARKER.
|
20
|
270 */
|
|
271 (marker, number, buffer))
|
0
|
272 {
|
|
273 return set_marker_internal (marker, number, buffer, 0);
|
|
274 }
|
|
275
|
|
276
|
|
277 /* This version of Fset_marker won't let the position
|
|
278 be outside the visible part. */
|
|
279 Lisp_Object
|
|
280 set_marker_restricted (Lisp_Object marker, Lisp_Object pos, Lisp_Object buffer)
|
|
281 {
|
|
282 return set_marker_internal (marker, pos, buffer, 1);
|
|
283 }
|
|
284
|
|
285
|
|
286 /* This is called during garbage collection,
|
|
287 so we must be careful to ignore and preserve mark bits,
|
|
288 including those in chain fields of markers. */
|
|
289
|
|
290 void
|
|
291 unchain_marker (Lisp_Object m)
|
|
292 {
|
|
293 struct Lisp_Marker *marker = XMARKER (m);
|
|
294 struct buffer *b = marker->buffer;
|
|
295
|
|
296 if (b == 0)
|
|
297 return;
|
|
298
|
|
299 assert (BUFFER_LIVE_P (b));
|
|
300
|
|
301 if (marker_next (marker))
|
|
302 marker_prev (marker_next (marker)) = marker_prev (marker);
|
|
303 if (marker_prev (marker))
|
|
304 marker_next (marker_prev (marker)) = marker_next (marker);
|
|
305 else
|
|
306 BUF_MARKERS (b) = marker_next (marker);
|
|
307
|
|
308 assert (marker != XMARKER (b->point_marker));
|
|
309
|
|
310 marker->buffer = 0;
|
|
311 }
|
|
312
|
|
313 Bytind
|
|
314 bi_marker_position (Lisp_Object marker)
|
|
315 {
|
|
316 struct Lisp_Marker *m = XMARKER (marker);
|
|
317 struct buffer *buf = m->buffer;
|
|
318 Bytind pos;
|
|
319
|
|
320 if (!buf)
|
|
321 error ("Marker does not point anywhere");
|
|
322
|
|
323 /* FSF claims that marker indices could end up denormalized, i.e.
|
|
324 in the gap. This is way bogus if it ever happens, and means
|
|
325 something fucked up elsewhere. Since I've overhauled all this
|
|
326 shit, I don't think this can happen. In any case, the following
|
|
327 macro has an assert() in it that will catch these denormalized
|
|
328 positions. */
|
|
329 pos = memind_to_bytind (buf, m->memind);
|
|
330
|
|
331 if (pos < BI_BUF_BEG (buf) || pos > BI_BUF_Z (buf))
|
|
332 abort ();
|
|
333
|
|
334 return pos;
|
|
335 }
|
|
336
|
|
337 Bufpos
|
|
338 marker_position (Lisp_Object marker)
|
|
339 {
|
|
340 struct buffer *buf = XMARKER (marker)->buffer;
|
|
341
|
|
342 if (!buf)
|
|
343 error ("Marker does not point anywhere");
|
|
344
|
|
345 return bytind_to_bufpos (buf, bi_marker_position (marker));
|
|
346 }
|
|
347
|
|
348 void
|
|
349 set_bi_marker_position (Lisp_Object marker, Bytind pos)
|
|
350 {
|
|
351 struct Lisp_Marker *m = XMARKER (marker);
|
|
352 struct buffer *buf = m->buffer;
|
|
353
|
|
354 if (!buf)
|
|
355 error ("Marker does not point anywhere");
|
|
356
|
|
357 if (pos < BI_BUF_BEG (buf) || pos > BI_BUF_Z (buf))
|
|
358 abort ();
|
|
359
|
|
360 m->memind = bytind_to_memind (buf, pos);
|
|
361 }
|
|
362
|
|
363 void
|
|
364 set_marker_position (Lisp_Object marker, Bufpos pos)
|
|
365 {
|
|
366 struct buffer *buf = XMARKER (marker)->buffer;
|
|
367
|
|
368 if (!buf)
|
|
369 error ("Marker does not point anywhere");
|
|
370
|
|
371 set_bi_marker_position (marker, bufpos_to_bytind (buf, pos));
|
|
372 }
|
|
373
|
|
374 static Lisp_Object
|
|
375 copy_marker_1 (Lisp_Object marker, Lisp_Object type, int noseeum)
|
|
376 {
|
|
377 REGISTER Lisp_Object new;
|
|
378
|
|
379 while (1)
|
|
380 {
|
|
381 if (INTP (marker) || MARKERP (marker))
|
|
382 {
|
|
383 if (noseeum)
|
|
384 new = noseeum_make_marker ();
|
|
385 else
|
|
386 new = Fmake_marker ();
|
|
387 Fset_marker (new, marker,
|
|
388 (MARKERP (marker) ? Fmarker_buffer (marker) : Qnil));
|
|
389 XMARKER (new)->insertion_type = !NILP (type);
|
|
390 return new;
|
|
391 }
|
|
392 else
|
|
393 marker = wrong_type_argument (Qinteger_or_marker_p, marker);
|
|
394 }
|
|
395
|
|
396 RETURN_NOT_REACHED (Qnil) /* not reached */
|
|
397 }
|
|
398
|
20
|
399 DEFUN ("copy-marker", Fcopy_marker, 1, 2, 0, /*
|
0
|
400 Return a new marker pointing at the same place as MARKER.
|
|
401 If argument is a number, makes a new marker pointing
|
|
402 at that position in the current buffer.
|
|
403 The optional argument TYPE specifies the insertion type of the new marker;
|
|
404 see `marker-insertion-type'.
|
20
|
405 */
|
|
406 (marker, type))
|
0
|
407 {
|
|
408 return copy_marker_1 (marker, type, 0);
|
|
409 }
|
|
410
|
|
411 Lisp_Object
|
|
412 noseeum_copy_marker (Lisp_Object marker, Lisp_Object type)
|
|
413 {
|
|
414 return copy_marker_1 (marker, type, 1);
|
|
415 }
|
|
416
|
20
|
417 DEFUN ("marker-insertion-type", Fmarker_insertion_type, 1, 1, 0, /*
|
0
|
418 Return insertion type of MARKER: t if it stays after inserted text.
|
|
419 nil means the marker stays before text inserted there.
|
20
|
420 */
|
|
421 (marker))
|
0
|
422 {
|
|
423 CHECK_MARKER (marker);
|
|
424 return XMARKER (marker)->insertion_type ? Qt : Qnil;
|
|
425 }
|
|
426
|
20
|
427 DEFUN ("set-marker-insertion-type", Fset_marker_insertion_type, 2, 2, 0, /*
|
0
|
428 Set the insertion-type of MARKER to TYPE.
|
|
429 If TYPE is t, it means the marker advances when you insert text at it.
|
|
430 If TYPE is nil, it means the marker stays behind when you insert text at it.
|
20
|
431 */
|
|
432 (marker, type))
|
0
|
433 {
|
|
434 CHECK_MARKER (marker);
|
|
435
|
|
436 XMARKER (marker)->insertion_type = ! NILP (type);
|
|
437 return type;
|
|
438 }
|
|
439
|
|
440 #ifdef MEMORY_USAGE_STATS
|
|
441
|
|
442 int
|
|
443 compute_buffer_marker_usage (struct buffer *b, struct overhead_stats *ovstats)
|
|
444 {
|
|
445 struct Lisp_Marker *m;
|
|
446 int total = 0;
|
|
447 int overhead;
|
|
448
|
|
449 for (m = BUF_MARKERS (b); m; m = m->next)
|
|
450 total += sizeof (struct Lisp_Marker);
|
|
451 ovstats->was_requested += total;
|
|
452 overhead = fixed_type_block_overhead (total);
|
|
453 /* #### claiming this is all malloc overhead is not really right,
|
|
454 but it has to go somewhere. */
|
|
455 ovstats->malloc_overhead += overhead;
|
|
456 return total + overhead;
|
|
457 }
|
|
458
|
|
459 #endif /* MEMORY_USAGE_STATS */
|
|
460
|
|
461
|
|
462 void
|
|
463 syms_of_marker (void)
|
|
464 {
|
20
|
465 DEFSUBR (Fmarker_position);
|
|
466 DEFSUBR (Fmarker_buffer);
|
|
467 DEFSUBR (Fset_marker);
|
|
468 DEFSUBR (Fcopy_marker);
|
|
469 DEFSUBR (Fmarker_insertion_type);
|
|
470 DEFSUBR (Fset_marker_insertion_type);
|
0
|
471 }
|
|
472
|
|
473 void init_buffer_markers (struct buffer *b);
|
|
474 void
|
|
475 init_buffer_markers (struct buffer *b)
|
|
476 {
|
|
477 Lisp_Object buf = Qnil;
|
|
478
|
|
479 XSETBUFFER (buf, b);
|
|
480 b->mark = Fmake_marker ();
|
|
481 BUF_MARKERS (b) = 0;
|
|
482 b->point_marker = Fmake_marker ();
|
|
483 Fset_marker (b->point_marker, make_int (1), buf);
|
|
484 }
|
|
485
|
|
486 void uninit_buffer_markers (struct buffer *b);
|
|
487 void
|
|
488 uninit_buffer_markers (struct buffer *b)
|
|
489 {
|
|
490 /* Unchain all markers of this buffer
|
|
491 and leave them pointing nowhere. */
|
|
492 REGISTER struct Lisp_Marker *m, *next;
|
|
493 for (m = BUF_MARKERS (b); m; m = next)
|
|
494 {
|
|
495 m->buffer = 0;
|
|
496 next = marker_next (m);
|
|
497 marker_next (m) = 0;
|
|
498 marker_prev (m) = 0;
|
|
499 }
|
|
500 BUF_MARKERS (b) = 0;
|
|
501 }
|