0
|
1 /* Copyright (c) 1994, 1995 Free Software Foundation, Inc.
|
|
2 Copyright (c) 1995 Sun Microsystems, Inc.
|
|
3 Copyright (c) 1995, 1996 Ben Wing.
|
|
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: Not in FSF. */
|
|
23
|
|
24 /* This file has been Mule-ized. */
|
|
25
|
|
26 /* Written by Ben Wing <wing@666.com>.
|
|
27
|
|
28 [Originally written by some people at Lucid.
|
|
29 Hacked on by jwz.
|
|
30 Start/end-open stuff added by John Rose (john.rose@eng.sun.com).
|
|
31 Rewritten from scratch by Ben Wing, December 1994.] */
|
|
32
|
|
33 /* Commentary:
|
|
34
|
|
35 Extents are regions over a buffer, with a start and an end position
|
|
36 denoting the region of the buffer included in the extent. In
|
|
37 addition, either end can be closed or open, meaning that the endpoint
|
|
38 is or is not logically included in the extent. Insertion of a character
|
|
39 at a closed endpoint causes the character to go inside the extent;
|
|
40 insertion at an open endpoint causes the character to go outside.
|
|
41
|
|
42 Extent endpoints are stored using memory indices (see insdel.c),
|
|
43 to minimize the amount of adjusting that needs to be done when
|
|
44 characters are inserted or deleted.
|
|
45
|
|
46 (Formerly, extent endpoints at the gap could be either before or
|
|
47 after the gap, depending on the open/closedness of the endpoint.
|
|
48 The intent of this was to make it so that insertions would
|
|
49 automatically go inside or out of extents as necessary with no
|
|
50 further work needing to be done. It didn't work out that way,
|
|
51 however, and just ended up complexifying and buggifying all the
|
|
52 rest of the code.)
|
|
53
|
|
54 Extents are compared using memory indices. There are two orderings
|
|
55 for extents and both orders are kept current at all times. The normal
|
|
56 or "display" order is as follows:
|
|
57
|
|
58 Extent A is "less than" extent B, that is, earlier in the display order,
|
|
59 if: A-start < B-start,
|
|
60 or if: A-start = B-start, and A-end > B-end
|
|
61
|
|
62 So if two extents begin at the same position, the larger of them is the
|
|
63 earlier one in the display order (EXTENT_LESS is true).
|
|
64
|
|
65 For the e-order, the same thing holds: Extent A is "less than" extent B
|
|
66 in e-order, that is, later in the buffer,
|
|
67 if: A-end < B-end,
|
|
68 or if: A-end = B-end, and A-start > B-start
|
|
69
|
|
70 So if two extents end at the same position, the smaller of them is the
|
|
71 earlier one in the e-order (EXTENT_E_LESS is true).
|
|
72
|
|
73 The display order and the e-order are complementary orders: any
|
|
74 theorem about the display order also applies to the e-order if you
|
|
75 swap all occurrences of "display order" and "e-order", "less than"
|
|
76 and "greater than", and "extent start" and "extent end".
|
|
77
|
|
78 Extents can be zero-length, and will end up that way if their endpoints
|
|
79 are explicitly set that way or if their detachable property is nil
|
|
80 and all the text in the extent is deleted. (The exception is open-open
|
|
81 zero-length extents, which are barred from existing because there is
|
|
82 no sensible way to define their properties. Deletion of the text in
|
|
83 an open-open extent causes it to be converted into a closed-open
|
|
84 extent.) Zero-length extents are primarily used to represent
|
|
85 annotations, and behave as follows:
|
|
86
|
|
87 1) Insertion at the position of a zero-length extent expands the extent
|
|
88 if both endpoints are closed; goes after the extent if it is closed-open;
|
|
89 and goes before the extent if it is open-closed.
|
|
90
|
|
91 2) Deletion of a character on a side of a zero-length extent whose
|
|
92 corresponding endpoint is closed causes the extent to be detached if
|
|
93 it is detachable; if the extent is not detachable or the corresponding
|
|
94 endpoint is open, the extent remains in the buffer, moving as necessary.
|
|
95
|
|
96 Note that closed-open, non-detachable zero-length extents behave exactly
|
|
97 like markers and that open-closed, non-detachable zero-length extents
|
|
98 behave like the "point-type" marker in Mule.
|
|
99
|
|
100
|
|
101 #### The following information is wrong in places.
|
|
102
|
|
103 More about the different orders:
|
|
104 --------------------------------
|
|
105
|
|
106 The extents in a buffer are ordered by "display order" because that
|
|
107 is that order that the redisplay mechanism needs to process them in.
|
|
108 The e-order is an auxiliary ordering used to facilitate operations
|
|
109 over extents. The operations that can be performed on the ordered
|
|
110 list of extents in a buffer are
|
|
111
|
|
112 1) Locate where an extent would go if inserted into the list.
|
|
113 2) Insert an extent into the list.
|
|
114 3) Remove an extent from the list.
|
|
115 4) Map over all the extents that overlap a range.
|
|
116
|
|
117 (4) requires being able to determine the first and last extents
|
|
118 that overlap a range.
|
|
119
|
|
120 NOTE: "overlap" is used as follows:
|
|
121
|
|
122 -- two ranges overlap if they have at least one point in common.
|
|
123 Whether the endpoints are open or closed makes a difference here.
|
|
124 -- a point overlaps a range if the point is contained within the
|
|
125 range; this is equivalent to treating a point P as the range
|
|
126 [P, P].
|
|
127 -- In the case of an *extent* overlapping a point or range, the
|
|
128 extent is normally treated as having closed endpoints. This
|
|
129 applies consistently in the discussion of stacks of extents
|
|
130 and such below. Note that this definition of overlap is not
|
|
131 necessarily consistent with the extents that `map-extents'
|
|
132 maps over, since `map-extents' sometimes pays attention to
|
|
133 whether the endpoints of an extents are open or closed.
|
|
134 But for our purposes, it greatly simplifies things to treat
|
|
135 all extents as having closed endpoints.
|
|
136
|
|
137 First, define >, <, <=, etc. as applied to extents to mean
|
|
138 comparison according to the display order. Comparison between an
|
|
139 extent E and an index I means comparison between E and the range
|
|
140 [I, I].
|
|
141 Also define e>, e<, e<=, etc. to mean comparison according to the
|
|
142 e-order.
|
|
143 For any range R, define R(0) to be the starting index of the range
|
|
144 and R(1) to be the ending index of the range.
|
|
145 For any extent E, define E(next) to be the extent directly following
|
|
146 E, and E(prev) to be the extent directly preceding E. Assume
|
|
147 E(next) and E(prev) can be determined from E in constant time.
|
|
148 (This is because we store the extent list as a doubly linked
|
|
149 list.)
|
|
150 Similarly, define E(e-next) and E(e-prev) to be the extents
|
|
151 directly following and preceding E in the e-order.
|
|
152
|
|
153 Now:
|
|
154
|
|
155 Let R be a range.
|
|
156 Let F be the first extent overlapping R.
|
|
157 Let L be the last extent overlapping R.
|
|
158
|
|
159 Theorem 1: R(1) lies between L and L(next), i.e. L <= R(1) < L(next).
|
|
160
|
|
161 This follows easily from the definition of display order. The
|
|
162 basic reason that this theorem applies is that the display order
|
|
163 sorts by increasing starting index.
|
|
164
|
|
165 Therefore, we can determine L just by looking at where we would
|
|
166 insert R(1) into the list, and if we know F and are moving forward
|
|
167 over extents, we can easily determine when we've hit L by comparing
|
|
168 the extent we're at to R(1).
|
|
169
|
|
170 Theorem 2: F(e-prev) e< [1, R(0)] e<= F.
|
|
171
|
|
172 This is the analog of Theorem 1, and applies because the e-order
|
|
173 sorts by increasing ending index.
|
|
174
|
|
175 Therefore, F can be found in the same amount of time as operation (1),
|
|
176 i.e. the time that it takes to locate where an extent would go if
|
|
177 inserted into the e-order list.
|
|
178
|
|
179 If the lists were stored as balanced binary trees, then operation (1)
|
|
180 would take logarithmic time, which is usually quite fast. However,
|
|
181 currently they're stored as simple doubly-linked lists, and instead
|
|
182 we do some caching to try to speed things up.
|
|
183
|
|
184 Define a "stack of extents" (or "SOE") as the set of extents
|
|
185 (ordered in the display order) that overlap an index I, together with
|
|
186 the SOE's "previous" extent, which is an extent that precedes I in
|
|
187 the e-order. (Hopefully there will not be very many extents between
|
|
188 I and the previous extent.)
|
|
189
|
|
190 Now:
|
|
191
|
|
192 Let I be an index, let S be the stack of extents on I, let F be
|
|
193 the first extent in S, and let P be S's previous extent.
|
|
194
|
|
195 Theorem 3: The first extent in S is the first extent that overlaps
|
|
196 any range [I, J].
|
|
197
|
|
198 Proof: Any extent that overlaps [I, J] but does not include I must
|
|
199 have a start index > I, and thus be greater than any extent in S.
|
|
200
|
|
201 Therefore, finding the first extent that overlaps a range R is the
|
|
202 same as finding the first extent that overlaps R(0).
|
|
203
|
|
204 Theorem 4: Let I2 be an index such that I2 > I, and let F2 be the
|
|
205 first extent that overlaps I2. Then, either F2 is in S or F2 is
|
|
206 greater than any extent in S.
|
|
207
|
|
208 Proof: If F2 does not include I then its start index is greater
|
|
209 than I and thus it is greater than any extent in S, including F.
|
|
210 Otherwise, F2 includes I and thus is in S, and thus F2 >= F.
|
|
211
|
|
212 */
|
|
213
|
|
214 #include <config.h>
|
|
215 #include "lisp.h"
|
|
216
|
|
217 #include "buffer.h"
|
|
218 #include "debug.h"
|
|
219 #include "device.h"
|
|
220 #include "elhash.h"
|
|
221 #include "extents.h"
|
|
222 #include "faces.h"
|
|
223 #include "frame.h"
|
|
224 #include "glyphs.h"
|
|
225 #include "hash.h"
|
|
226 #include "insdel.h"
|
|
227 #include "opaque.h"
|
|
228 #include "process.h"
|
|
229 #include "redisplay.h"
|
|
230
|
|
231 /* ------------------------------- */
|
|
232 /* gap array */
|
|
233 /* ------------------------------- */
|
|
234
|
|
235 /* Note that this object is not extent-specific and should perhaps be
|
|
236 moved into another file. */
|
|
237
|
|
238 /* Holds a marker that moves as elements in the array are inserted and
|
|
239 deleted, similar to standard markers. */
|
|
240
|
|
241 typedef struct gap_array_marker
|
|
242 {
|
|
243 int pos;
|
|
244 struct gap_array_marker *next;
|
|
245 } Gap_Array_Marker;
|
|
246
|
|
247 /* Holds a "gap array", which is an array of elements with a gap located
|
|
248 in it. Insertions and deletions with a high degree of locality
|
|
249 are very fast, essentially in constant time. Array positions as
|
|
250 used and returned in the gap array functions are independent of
|
|
251 the gap. */
|
|
252
|
|
253 typedef struct gap_array
|
|
254 {
|
|
255 char *array;
|
|
256 int gap;
|
|
257 int gapsize;
|
|
258 int numels;
|
|
259 int elsize;
|
|
260 Gap_Array_Marker *markers;
|
|
261 } Gap_Array;
|
|
262
|
|
263 Gap_Array_Marker *gap_array_marker_freelist;
|
|
264
|
|
265 /* Convert a "memory position" (i.e. taking the gap into account) into
|
|
266 the address of the element at (i.e. after) that position. "Memory
|
|
267 positions" are only used internally and are of type Memind.
|
|
268 "Array positions" are used externally and are of type int. */
|
|
269 #define GAP_ARRAY_MEMEL_ADDR(ga, memel) ((ga)->array + (ga)->elsize*(memel))
|
|
270
|
|
271 /* Number of elements currently in a gap array */
|
|
272 #define GAP_ARRAY_NUM_ELS(ga) ((ga)->numels)
|
|
273
|
|
274 #define GAP_ARRAY_ARRAY_TO_MEMORY_POS(ga, pos) \
|
|
275 ((pos) <= (ga)->gap ? (pos) : (pos) + (ga)->gapsize)
|
|
276
|
|
277 #define GAP_ARRAY_MEMORY_TO_ARRAY_POS(ga, pos) \
|
|
278 ((pos) <= (ga)->gap ? (pos) : (pos) - (ga)->gapsize)
|
|
279
|
|
280 /* Convert an array position into the address of the element at
|
|
281 (i.e. after) that position. */
|
|
282 #define GAP_ARRAY_EL_ADDR(ga, pos) ((pos) < (ga)->gap ? \
|
|
283 GAP_ARRAY_MEMEL_ADDR(ga, pos) : \
|
|
284 GAP_ARRAY_MEMEL_ADDR(ga, (pos) + (ga)->gapsize))
|
|
285
|
|
286 /* ------------------------------- */
|
|
287 /* extent list */
|
|
288 /* ------------------------------- */
|
|
289
|
|
290 typedef struct extent_list_marker
|
|
291 {
|
|
292 Gap_Array_Marker *m;
|
|
293 int endp;
|
|
294 struct extent_list_marker *next;
|
|
295 } Extent_List_Marker;
|
|
296
|
|
297 typedef struct extent_list
|
|
298 {
|
|
299 Gap_Array *start;
|
|
300 Gap_Array *end;
|
|
301 Extent_List_Marker *markers;
|
|
302 } Extent_List;
|
|
303
|
|
304 Extent_List_Marker *extent_list_marker_freelist;
|
|
305
|
|
306 #define EXTENT_LESS_VALS(e,st,nd) ((extent_start (e) < (st)) || \
|
|
307 ((extent_start (e) == (st)) && \
|
|
308 (extent_end (e) > (nd))))
|
|
309
|
|
310 #define EXTENT_EQUAL_VALS(e,st,nd) ((extent_start (e) == (st)) && \
|
|
311 (extent_end (e) == (nd)))
|
|
312
|
|
313 #define EXTENT_LESS_EQUAL_VALS(e,st,nd) ((extent_start (e) < (st)) || \
|
|
314 ((extent_start (e) == (st)) && \
|
|
315 (extent_end (e) >= (nd))))
|
|
316
|
|
317 /* Is extent E1 less than extent E2 in the display order? */
|
|
318 #define EXTENT_LESS(e1,e2) \
|
|
319 EXTENT_LESS_VALS (e1, extent_start (e2), extent_end (e2))
|
|
320
|
|
321 /* Is extent E1 equal to extent E2? */
|
|
322 #define EXTENT_EQUAL(e1,e2) \
|
|
323 EXTENT_EQUAL_VALS (e1, extent_start (e2), extent_end (e2))
|
|
324
|
|
325 /* Is extent E1 less than or equal to extent E2 in the display order? */
|
|
326 #define EXTENT_LESS_EQUAL(e1,e2) \
|
|
327 EXTENT_LESS_EQUAL_VALS (e1, extent_start (e2), extent_end (e2))
|
|
328
|
|
329 #define EXTENT_E_LESS_VALS(e,st,nd) ((extent_end (e) < (nd)) || \
|
|
330 ((extent_end (e) == (nd)) && \
|
|
331 (extent_start (e) > (st))))
|
|
332
|
|
333 #define EXTENT_E_LESS_EQUAL_VALS(e,st,nd) ((extent_end (e) < (nd)) || \
|
|
334 ((extent_end (e) == (nd)) && \
|
|
335 (extent_start (e) >= (st))))
|
|
336
|
|
337 /* Is extent E1 less than extent E2 in the e-order? */
|
|
338 #define EXTENT_E_LESS(e1,e2) \
|
|
339 EXTENT_E_LESS_VALS(e1, extent_start (e2), extent_end (e2))
|
|
340
|
|
341 /* Is extent E1 less than or equal to extent E2 in the e-order? */
|
|
342 #define EXTENT_E_LESS_EQUAL(e1,e2) \
|
|
343 EXTENT_E_LESS_EQUAL_VALS (e1, extent_start (e2), extent_end (e2))
|
|
344
|
|
345 #define EXTENT_GAP_ARRAY_AT(ga, pos) (* (EXTENT *) GAP_ARRAY_EL_ADDR(ga, pos))
|
|
346
|
|
347 /* ------------------------------- */
|
|
348 /* auxiliary extent structure */
|
|
349 /* ------------------------------- */
|
|
350
|
|
351 struct extent_auxiliary extent_auxiliary_defaults;
|
|
352
|
|
353 MAC_DEFINE (EXTENT, MTancestor_extent)
|
|
354 MAC_DEFINE (EXTENT, MTaux_extent)
|
|
355 MAC_DEFINE (EXTENT, MTplist_extent)
|
|
356 MAC_DEFINE (EXTENT, MTensure_extent)
|
|
357 MAC_DEFINE (EXTENT, MTset_extent)
|
|
358
|
|
359 /* ------------------------------- */
|
|
360 /* buffer-extent primitives */
|
|
361 /* ------------------------------- */
|
|
362
|
|
363 typedef struct stack_of_extents
|
|
364 {
|
|
365 Extent_List *extents;
|
|
366 Memind pos; /* Position of stack of extents. EXTENTS is the list of
|
|
367 all extents that overlap this position. This position
|
|
368 can be -1 if the stack of extents is invalid (this
|
|
369 happens when a buffer is first created or a string's
|
|
370 stack of extents is created [a string's stack of extents
|
|
371 is nuked when a GC occurs, to conserve memory]). */
|
|
372 } Stack_Of_Extents;
|
|
373
|
|
374 /* ------------------------------- */
|
|
375 /* map-extents */
|
|
376 /* ------------------------------- */
|
|
377
|
|
378 typedef int Endpoint_Index;
|
|
379
|
|
380 #define memind_to_startind(x, start_open) \
|
|
381 ((Endpoint_Index) (((x) << 1) + !!(start_open)))
|
|
382 #define memind_to_endind(x, end_open) \
|
|
383 ((Endpoint_Index) (((x) << 1) - !!(end_open)))
|
|
384
|
|
385 /* Combination macros */
|
|
386 #define bytind_to_startind(buf, x, start_open) \
|
|
387 memind_to_startind (bytind_to_memind (buf, x), start_open)
|
|
388 #define bytind_to_endind(buf, x, end_open) \
|
|
389 memind_to_endind (bytind_to_memind (buf, x), end_open)
|
|
390
|
|
391 /* ------------------------------- */
|
|
392 /* buffer-or-string primitives */
|
|
393 /* ------------------------------- */
|
|
394
|
|
395 /* Similar for Bytinds and start/end indices. */
|
|
396
|
|
397 #define buffer_or_string_bytind_to_startind(obj, ind, start_open) \
|
|
398 memind_to_startind (buffer_or_string_bytind_to_memind (obj, ind), \
|
|
399 start_open)
|
|
400
|
|
401 #define buffer_or_string_bytind_to_endind(obj, ind, end_open) \
|
|
402 memind_to_endind (buffer_or_string_bytind_to_memind (obj, ind), \
|
|
403 end_open)
|
|
404
|
|
405 /* ------------------------------- */
|
|
406 /* Lisp-level functions */
|
|
407 /* ------------------------------- */
|
|
408
|
|
409 /* flags for decode_extent() */
|
|
410 #define DE_MUST_HAVE_BUFFER 1
|
|
411 #define DE_MUST_BE_ATTACHED 2
|
|
412
|
|
413 /* #### remove this crap */
|
|
414 #ifdef ENERGIZE
|
|
415 extern void restore_energize_extent_state (EXTENT extent);
|
|
416 #endif
|
|
417
|
|
418 Lisp_Object Vlast_highlighted_extent;
|
|
419 int mouse_highlight_priority;
|
|
420
|
|
421 Lisp_Object Qextentp;
|
|
422 Lisp_Object Qextent_live_p;
|
|
423
|
|
424 Lisp_Object Qend_closed;
|
|
425 Lisp_Object Qstart_open;
|
|
426 Lisp_Object Qall_extents_closed;
|
|
427 Lisp_Object Qall_extents_open;
|
|
428 Lisp_Object Qall_extents_closed_open;
|
|
429 Lisp_Object Qall_extents_open_closed;
|
|
430 Lisp_Object Qstart_in_region;
|
|
431 Lisp_Object Qend_in_region;
|
|
432 Lisp_Object Qstart_and_end_in_region;
|
|
433 Lisp_Object Qstart_or_end_in_region;
|
|
434 Lisp_Object Qnegate_in_region;
|
|
435
|
|
436 Lisp_Object Qdetached;
|
|
437 Lisp_Object Qdestroyed;
|
|
438 Lisp_Object Qbegin_glyph;
|
|
439 Lisp_Object Qend_glyph;
|
|
440 Lisp_Object Qstart_open;
|
|
441 Lisp_Object Qend_open;
|
|
442 Lisp_Object Qstart_closed;
|
|
443 Lisp_Object Qend_closed;
|
|
444 Lisp_Object Qread_only;
|
|
445 /* Qhighlight defined in general.c */
|
|
446 Lisp_Object Qunique;
|
|
447 Lisp_Object Qduplicable;
|
|
448 Lisp_Object Qdetachable;
|
|
449 Lisp_Object Qpriority;
|
|
450 Lisp_Object Qmouse_face;
|
|
451
|
|
452 Lisp_Object Qglyph_layout; /* This exists only for backwards compatibility. */
|
|
453 Lisp_Object Qbegin_glyph_layout, Qend_glyph_layout;
|
|
454 Lisp_Object Qoutside_margin;
|
|
455 Lisp_Object Qinside_margin;
|
|
456 Lisp_Object Qwhitespace;
|
|
457 /* Qtext defined in general.c */
|
|
458
|
|
459 /* partially used in redisplay */
|
|
460 Lisp_Object Qglyph_invisible;
|
|
461
|
|
462 Lisp_Object Qcopy_function;
|
|
463 Lisp_Object Qpaste_function;
|
|
464
|
|
465 /* The idea here is that if we're given a list of faces, we
|
|
466 need to "memoize" this so that two lists of faces that are `equal'
|
|
467 turn into the same object. When `set-extent-face' is called, we
|
|
468 "memoize" into a list of actual faces; when `extent-face' is called,
|
|
469 we do a reverse lookup to get the list of symbols. */
|
|
470
|
|
471 static Lisp_Object canonicalize_extent_property (Lisp_Object prop,
|
|
472 Lisp_Object value);
|
|
473 Lisp_Object Vextent_face_memoize_hash_table;
|
|
474 Lisp_Object Vextent_face_reverse_memoize_hash_table;
|
|
475 Lisp_Object Vextent_face_reusable_list;
|
|
476 /* FSFmacs bogosity */
|
|
477 Lisp_Object Vdefault_text_properties;
|
|
478
|
|
479
|
|
480 /************************************************************************/
|
|
481 /* Generalized gap array */
|
|
482 /************************************************************************/
|
|
483
|
|
484 /* This generalizes the "array with a gap" model used to store buffer
|
|
485 characters. This is based on the stuff in insdel.c and should
|
|
486 probably be merged with it. This is not extent-specific and should
|
|
487 perhaps be moved into a separate file. */
|
|
488
|
|
489 /* ------------------------------- */
|
|
490 /* internal functions */
|
|
491 /* ------------------------------- */
|
|
492
|
|
493 /* Adjust the gap array markers in the range (FROM, TO]. Parallel to
|
|
494 adjust_markers() in insdel.c. */
|
|
495
|
|
496 static void
|
|
497 gap_array_adjust_markers (Gap_Array *ga, Memind from,
|
|
498 Memind to, int amount)
|
|
499 {
|
|
500 Gap_Array_Marker *m;
|
|
501
|
|
502 for (m = ga->markers; m; m = m->next)
|
|
503 m->pos = do_marker_adjustment (m->pos, from, to, amount);
|
|
504 }
|
|
505
|
|
506 /* Move the gap to array position POS. Parallel to move_gap() in
|
|
507 insdel.c but somewhat simplified. */
|
|
508
|
|
509 static void
|
|
510 gap_array_move_gap (Gap_Array *ga, int pos)
|
|
511 {
|
|
512 int gap = ga->gap;
|
|
513 int gapsize = ga->gapsize;
|
|
514
|
|
515 assert (ga->array);
|
|
516 if (pos < gap)
|
|
517 {
|
|
518 memmove (GAP_ARRAY_MEMEL_ADDR (ga, pos + gapsize),
|
|
519 GAP_ARRAY_MEMEL_ADDR (ga, pos),
|
|
520 (gap - pos)*ga->elsize);
|
|
521 gap_array_adjust_markers (ga, (Memind) pos, (Memind) gap,
|
|
522 gapsize);
|
|
523 }
|
|
524 else if (pos > gap)
|
|
525 {
|
|
526 memmove (GAP_ARRAY_MEMEL_ADDR (ga, gap),
|
|
527 GAP_ARRAY_MEMEL_ADDR (ga, gap + gapsize),
|
|
528 (pos - gap)*ga->elsize);
|
|
529 gap_array_adjust_markers (ga, (Memind) (gap + gapsize),
|
|
530 (Memind) (pos + gapsize), - gapsize);
|
|
531 }
|
|
532 ga->gap = pos;
|
|
533 }
|
|
534
|
|
535 /* Make the gap INCREMENT characters longer. Parallel to make_gap() in
|
|
536 insdel.c. */
|
|
537
|
|
538 static void
|
|
539 gap_array_make_gap (Gap_Array *ga, int increment)
|
|
540 {
|
|
541 char *ptr = ga->array;
|
|
542 int real_gap_loc;
|
|
543 int old_gap_size;
|
|
544
|
|
545 /* If we have to get more space, get enough to last a while. We use
|
|
546 a geometric progession that saves on realloc space. */
|
|
547 increment += 100 + ga->numels / 8;
|
|
548
|
|
549 ptr = xrealloc (ptr,
|
|
550 (ga->numels + ga->gapsize + increment)*ga->elsize);
|
|
551 if (ptr == 0)
|
|
552 memory_full ();
|
|
553 ga->array = ptr;
|
|
554
|
|
555 real_gap_loc = ga->gap;
|
|
556 old_gap_size = ga->gapsize;
|
|
557
|
|
558 /* Call the newly allocated space a gap at the end of the whole space. */
|
|
559 ga->gap = ga->numels + ga->gapsize;
|
|
560 ga->gapsize = increment;
|
|
561
|
|
562 /* Move the new gap down to be consecutive with the end of the old one.
|
|
563 This adjusts the markers properly too. */
|
|
564 gap_array_move_gap (ga, real_gap_loc + old_gap_size);
|
|
565
|
|
566 /* Now combine the two into one large gap. */
|
|
567 ga->gapsize += old_gap_size;
|
|
568 ga->gap = real_gap_loc;
|
|
569 }
|
|
570
|
|
571 /* ------------------------------- */
|
|
572 /* external functions */
|
|
573 /* ------------------------------- */
|
|
574
|
|
575 /* Insert NUMELS elements (pointed to by ELPTR) into the specified
|
|
576 gap array at POS. */
|
|
577
|
|
578 static void
|
|
579 gap_array_insert_els (Gap_Array *ga, int pos, void *elptr, int numels)
|
|
580 {
|
|
581 assert (pos >= 0 && pos <= ga->numels);
|
|
582 if (ga->gapsize < numels)
|
|
583 gap_array_make_gap (ga, numels - ga->gapsize);
|
|
584 if (pos != ga->gap)
|
|
585 gap_array_move_gap (ga, pos);
|
|
586
|
|
587 memcpy (GAP_ARRAY_MEMEL_ADDR (ga, ga->gap), (char *) elptr,
|
|
588 numels*ga->elsize);
|
|
589 ga->gapsize -= numels;
|
|
590 ga->gap += numels;
|
|
591 ga->numels += numels;
|
|
592 /* This is the equivalent of insert-before-markers.
|
|
593
|
|
594 #### Should only happen if marker is "moves forward at insert" type.
|
|
595 */
|
|
596
|
|
597 gap_array_adjust_markers (ga, pos - 1, pos, numels);
|
|
598 }
|
|
599
|
|
600 /* Delete NUMELS elements from the specified gap array, starting at FROM. */
|
|
601
|
|
602 static void
|
|
603 gap_array_delete_els (Gap_Array *ga, int from, int numdel)
|
|
604 {
|
|
605 int to = from + numdel;
|
|
606 int gapsize = ga->gapsize;
|
|
607
|
|
608 assert (from >= 0);
|
|
609 assert (numdel >= 0);
|
|
610 assert (to <= ga->numels);
|
|
611
|
|
612 /* Make sure the gap is somewhere in or next to what we are deleting. */
|
|
613 if (to < ga->gap)
|
|
614 gap_array_move_gap (ga, to);
|
|
615 if (from > ga->gap)
|
|
616 gap_array_move_gap (ga, from);
|
|
617
|
|
618 /* Relocate all markers pointing into the new, larger gap
|
|
619 to point at the end of the text before the gap. */
|
|
620 gap_array_adjust_markers (ga, to + gapsize, to + gapsize,
|
|
621 - numdel - gapsize);
|
|
622
|
|
623 ga->gapsize += numdel;
|
|
624 ga->numels -= numdel;
|
|
625 ga->gap = from;
|
|
626 }
|
|
627
|
|
628 static Gap_Array_Marker *
|
|
629 gap_array_make_marker (Gap_Array *ga, int pos)
|
|
630 {
|
|
631 Gap_Array_Marker *m;
|
|
632
|
|
633 assert (pos >= 0 && pos <= ga->numels);
|
|
634 if (gap_array_marker_freelist)
|
|
635 {
|
|
636 m = gap_array_marker_freelist;
|
|
637 gap_array_marker_freelist = gap_array_marker_freelist->next;
|
|
638 }
|
|
639 else
|
|
640 m = (Gap_Array_Marker *) xmalloc (sizeof (*m));
|
|
641
|
|
642 m->pos = GAP_ARRAY_ARRAY_TO_MEMORY_POS (ga, pos);
|
|
643 m->next = ga->markers;
|
|
644 ga->markers = m;
|
|
645 return m;
|
|
646 }
|
|
647
|
|
648 static void
|
|
649 gap_array_delete_marker (Gap_Array *ga, Gap_Array_Marker *m)
|
|
650 {
|
|
651 Gap_Array_Marker *p, *prev;
|
|
652
|
|
653 for (prev = 0, p = ga->markers; p && p != m; prev = p, p = p->next)
|
|
654 ;
|
|
655 assert (p);
|
|
656 if (prev)
|
|
657 prev->next = p->next;
|
|
658 else
|
|
659 ga->markers = p->next;
|
|
660 m->next = gap_array_marker_freelist;
|
|
661 m->pos = 0xDEADBEEF; /* -559038737 as an int */
|
|
662 gap_array_marker_freelist = m;
|
|
663 }
|
|
664
|
|
665 static void
|
|
666 gap_array_delete_all_markers (Gap_Array *ga)
|
|
667 {
|
|
668 Gap_Array_Marker *p, *next;
|
|
669
|
|
670 for (p = ga->markers; p; p = next)
|
|
671 {
|
|
672 next = p->next;
|
|
673 p->next = gap_array_marker_freelist;
|
|
674 p->pos = 0xDEADBEEF; /* -559038737 as an int */
|
|
675 gap_array_marker_freelist = p;
|
|
676 }
|
|
677 }
|
|
678
|
|
679 static void
|
|
680 gap_array_move_marker (Gap_Array *ga, Gap_Array_Marker *m, int pos)
|
|
681 {
|
|
682 assert (pos >= 0 && pos <= ga->numels);
|
|
683 m->pos = GAP_ARRAY_ARRAY_TO_MEMORY_POS (ga, pos);
|
|
684 }
|
|
685
|
|
686 #define gap_array_marker_pos(ga, m) \
|
|
687 GAP_ARRAY_MEMORY_TO_ARRAY_POS (ga, (m)->pos)
|
|
688
|
|
689 static Gap_Array *
|
|
690 make_gap_array (int elsize)
|
|
691 {
|
|
692 Gap_Array *ga = (Gap_Array *) xmalloc (sizeof(*ga));
|
|
693 memset (ga, 0, sizeof(*ga));
|
|
694 ga->elsize = elsize;
|
|
695 return ga;
|
|
696 }
|
|
697
|
|
698 static void
|
|
699 free_gap_array (Gap_Array *ga)
|
|
700 {
|
|
701 if (ga->array)
|
|
702 xfree (ga->array);
|
|
703 gap_array_delete_all_markers (ga);
|
|
704 xfree (ga);
|
|
705 }
|
|
706
|
|
707
|
|
708 /************************************************************************/
|
|
709 /* Extent list primitives */
|
|
710 /************************************************************************/
|
|
711
|
|
712 /* A list of extents is maintained as a double gap array: one gap array
|
|
713 is ordered by start index (the "display order") and the other is
|
|
714 ordered by end index (the "e-order"). Note that positions in an
|
|
715 extent list should logically be conceived of as referring *to*
|
|
716 a particular extent (as is the norm in programs) rather than
|
|
717 sitting between two extents. Note also that callers of these
|
|
718 functions should not be aware of the fact that the extent list is
|
|
719 implemented as an array, except for the fact that positions are
|
|
720 integers (this should be generalized to handle integers and linked
|
|
721 list equally well).
|
|
722 */
|
|
723
|
|
724 /* Number of elements in an extent list */
|
|
725 #define extent_list_num_els(el) GAP_ARRAY_NUM_ELS(el->start)
|
|
726
|
|
727 /* Return the position at which EXTENT is located in the specified extent
|
|
728 list (in the display order if ENDP is 0, in the e-order otherwise).
|
|
729 If the extent is not found, the position where the extent would
|
|
730 be inserted is returned. If ENDP is 0, the insertion would go after
|
|
731 all other equal extents. If ENDP is not 0, the insertion would go
|
|
732 before all other equal extents. If FOUNDP is not 0, then whether
|
|
733 the extent was found will get written into it. */
|
|
734
|
|
735 static int
|
|
736 extent_list_locate (Extent_List *el, EXTENT extent, int endp, int *foundp)
|
|
737 {
|
|
738 Gap_Array *ga = endp ? el->end : el->start;
|
|
739 int left = 0, right = GAP_ARRAY_NUM_ELS (ga);
|
|
740 int oldfoundpos, foundpos;
|
|
741 int found;
|
|
742 EXTENT e;
|
|
743
|
|
744 while (left != right)
|
|
745 {
|
|
746 /* RIGHT might not point to a valid extent (i.e. it's at the end
|
|
747 of the list), so NEWPOS must round down. */
|
|
748 unsigned int newpos = (left + right) >> 1;
|
|
749 e = EXTENT_GAP_ARRAY_AT (ga, newpos);
|
|
750
|
|
751 if (endp ? EXTENT_E_LESS (e, extent) : EXTENT_LESS (e, extent))
|
|
752 left = newpos+1;
|
|
753 else
|
|
754 right = newpos;
|
|
755 }
|
|
756
|
|
757 /* Now we're at the beginning of all equal extents. */
|
|
758 found = 0;
|
|
759 oldfoundpos = foundpos = left;
|
|
760 while (foundpos < GAP_ARRAY_NUM_ELS (ga))
|
|
761 {
|
|
762 e = EXTENT_GAP_ARRAY_AT (ga, foundpos);
|
|
763 if (e == extent)
|
|
764 {
|
|
765 found = 1;
|
|
766 break;
|
|
767 }
|
|
768 if (!EXTENT_EQUAL (e, extent))
|
|
769 break;
|
|
770 foundpos++;
|
|
771 }
|
|
772 if (foundp)
|
|
773 *foundp = found;
|
|
774 if (found || !endp)
|
|
775 return foundpos;
|
|
776 else
|
|
777 return oldfoundpos;
|
|
778 }
|
|
779
|
|
780 /* Return the position of the first extent that begins at or after POS
|
|
781 (or ends at or after POS, if ENDP is not 0).
|
|
782
|
|
783 An out-of-range value for POS is allowed, and guarantees that the
|
|
784 position at the beginning or end of the extent list is returned. */
|
|
785
|
|
786 static int
|
|
787 extent_list_locate_from_pos (Extent_List *el, Memind pos, int endp)
|
|
788 {
|
|
789 struct extent fake_extent;
|
|
790 /*
|
|
791
|
|
792 Note that if we search for [POS, POS], then we get the following:
|
|
793
|
|
794 -- if ENDP is 0, then all extents whose start position is <= POS
|
|
795 lie before the returned position, and all extents whose start
|
|
796 position is > POS lie at or after the returned position.
|
|
797
|
|
798 -- if ENDP is not 0, then all extents whose end position is < POS
|
|
799 lie before the returned position, and all extents whose end
|
|
800 position is >= POS lie at or after the returned position.
|
|
801
|
|
802 */
|
|
803 set_extent_start (&fake_extent, endp ? pos : pos-1);
|
|
804 set_extent_end (&fake_extent, endp ? pos : pos-1);
|
|
805 return extent_list_locate (el, &fake_extent, endp, 0);
|
|
806 }
|
|
807
|
|
808 /* Return the extent at POS. */
|
|
809
|
|
810 static EXTENT
|
|
811 extent_list_at (Extent_List *el, Memind pos, int endp)
|
|
812 {
|
|
813 Gap_Array *ga = endp ? el->end : el->start;
|
|
814
|
|
815 assert (pos >= 0 && pos < GAP_ARRAY_NUM_ELS (ga));
|
|
816 return EXTENT_GAP_ARRAY_AT (ga, pos);
|
|
817 }
|
|
818
|
|
819 /* Insert an extent into an extent list. */
|
|
820
|
|
821 static void
|
|
822 extent_list_insert (Extent_List *el, EXTENT extent)
|
|
823 {
|
|
824 int pos, foundp;
|
|
825
|
|
826 pos = extent_list_locate (el, extent, 0, &foundp);
|
|
827 assert (!foundp);
|
|
828 gap_array_insert_els (el->start, pos, &extent, 1);
|
|
829 pos = extent_list_locate (el, extent, 1, &foundp);
|
|
830 assert (!foundp);
|
|
831 gap_array_insert_els (el->end, pos, &extent, 1);
|
|
832 }
|
|
833
|
|
834 /* Delete an extent from an extent list. */
|
|
835
|
|
836 static void
|
|
837 extent_list_delete (Extent_List *el, EXTENT extent)
|
|
838 {
|
|
839 int pos, foundp;
|
|
840
|
|
841 pos = extent_list_locate (el, extent, 0, &foundp);
|
|
842 assert (foundp);
|
|
843 gap_array_delete_els (el->start, pos, 1);
|
|
844 pos = extent_list_locate (el, extent, 1, &foundp);
|
|
845 assert (foundp);
|
|
846 gap_array_delete_els (el->end, pos, 1);
|
|
847 }
|
|
848
|
|
849 static void
|
|
850 extent_list_delete_all (Extent_List *el)
|
|
851 {
|
|
852 gap_array_delete_els (el->start, 0, GAP_ARRAY_NUM_ELS (el->start));
|
|
853 gap_array_delete_els (el->end, 0, GAP_ARRAY_NUM_ELS (el->end));
|
|
854 }
|
|
855
|
|
856 static Extent_List_Marker *
|
|
857 extent_list_make_marker (Extent_List *el, int pos, int endp)
|
|
858 {
|
|
859 Extent_List_Marker *m;
|
|
860
|
|
861 if (extent_list_marker_freelist)
|
|
862 {
|
|
863 m = extent_list_marker_freelist;
|
|
864 extent_list_marker_freelist = extent_list_marker_freelist->next;
|
|
865 }
|
|
866 else
|
|
867 m = (Extent_List_Marker *) xmalloc (sizeof (*m));
|
|
868
|
|
869 m->m = gap_array_make_marker (endp ? el->end : el->start, pos);
|
|
870 m->endp = endp;
|
|
871 m->next = el->markers;
|
|
872 el->markers = m;
|
|
873 return m;
|
|
874 }
|
|
875
|
|
876 #define extent_list_move_marker(el, mkr, pos) \
|
|
877 gap_array_move_marker((mkr)->endp ? (el)->end : (el)->start, (mkr)->m, pos)
|
|
878
|
|
879 static void
|
|
880 extent_list_delete_marker (Extent_List *el, Extent_List_Marker *m)
|
|
881 {
|
|
882 Extent_List_Marker *p, *prev;
|
|
883
|
|
884 for (prev = 0, p = el->markers; p && p != m; prev = p, p = p->next)
|
|
885 ;
|
|
886 assert (p);
|
|
887 if (prev)
|
|
888 prev->next = p->next;
|
|
889 else
|
|
890 el->markers = p->next;
|
|
891 m->next = extent_list_marker_freelist;
|
|
892 extent_list_marker_freelist = m;
|
|
893 gap_array_delete_marker (m->endp ? el->end : el->start, m->m);
|
|
894 }
|
|
895
|
|
896 #define extent_list_marker_pos(el, mkr) \
|
|
897 gap_array_marker_pos ((mkr)->endp ? (el)->end : (el)->start, (mkr)->m)
|
|
898
|
|
899 static Extent_List *
|
|
900 allocate_extent_list (void)
|
|
901 {
|
|
902 Extent_List *el = (Extent_List *) xmalloc (sizeof(*el));
|
|
903 el->start = make_gap_array (sizeof(EXTENT));
|
|
904 el->end = make_gap_array (sizeof(EXTENT));
|
|
905 el->markers = 0;
|
|
906 return el;
|
|
907 }
|
|
908
|
|
909 static void
|
|
910 free_extent_list (Extent_List *el)
|
|
911 {
|
|
912 free_gap_array (el->start);
|
|
913 free_gap_array (el->end);
|
|
914 xfree (el);
|
|
915 }
|
|
916
|
|
917
|
|
918 /************************************************************************/
|
|
919 /* Auxiliary extent structure */
|
|
920 /************************************************************************/
|
|
921
|
|
922 static Lisp_Object mark_extent_auxiliary (Lisp_Object obj,
|
|
923 void (*markobj) (Lisp_Object));
|
|
924 DEFINE_LRECORD_IMPLEMENTATION ("extent-auxiliary", extent_auxiliary,
|
|
925 mark_extent_auxiliary, internal_object_printer,
|
|
926 0, 0, 0, struct extent_auxiliary);
|
|
927
|
|
928 static Lisp_Object
|
|
929 mark_extent_auxiliary (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
930 {
|
|
931 struct extent_auxiliary *data =
|
|
932 (struct extent_auxiliary *) XEXTENT_AUXILIARY (obj);
|
|
933 ((markobj) (data->begin_glyph));
|
|
934 ((markobj) (data->end_glyph));
|
|
935 ((markobj) (data->invisible));
|
|
936 ((markobj) (data->children));
|
|
937 ((markobj) (data->read_only));
|
|
938 ((markobj) (data->mouse_face));
|
|
939 return (data->parent);
|
|
940 }
|
|
941
|
|
942 void
|
|
943 allocate_extent_auxiliary (EXTENT ext)
|
|
944 {
|
|
945 Lisp_Object extent_aux = Qnil;
|
|
946 struct extent_auxiliary *data =
|
|
947 alloc_lcrecord (sizeof (struct extent_auxiliary),
|
|
948 lrecord_extent_auxiliary);
|
|
949
|
|
950 copy_lcrecord (data, &extent_auxiliary_defaults);
|
|
951 XSETEXTENT_AUXILIARY (extent_aux, data);
|
|
952 ext->plist = Fcons (extent_aux, ext->plist);
|
|
953 ext->flags.has_aux = 1;
|
|
954 }
|
|
955
|
|
956
|
|
957 /************************************************************************/
|
|
958 /* Extent info structure */
|
|
959 /************************************************************************/
|
|
960
|
|
961 /* An extent-info structure consists of a list of the buffer or string's
|
|
962 extents and a "stack of extents" that lists all of the extents over
|
|
963 a particular position. The stack-of-extents info is used for
|
|
964 optimization purposes -- it basically caches some info that might
|
|
965 be expensive to compute. Certain otherwise hard computations are easy
|
|
966 given the stack of extents over a particular position, and if the
|
|
967 stack of extents over a nearby position is known (because it was
|
|
968 calculated at some prior point in time), it's easy to move the stack
|
|
969 of extents to the proper position.
|
|
970
|
|
971 Given that the stack of extents is an optimization, and given that
|
|
972 it requires memory, a string's stack of extents is wiped out each
|
|
973 time a garbage collection occurs. Therefore, any time you retrieve
|
|
974 the stack of extents, it might not be there. If you need it to
|
|
975 be there, use the _force version.
|
|
976
|
2
|
977 Similarly, a string may or may not have an extent_info structure.
|
0
|
978 (Generally it won't if there haven't been any extents added to the
|
|
979 string.) So use the _force version if you need the extent_info
|
|
980 structure to be there. */
|
|
981
|
|
982 static struct stack_of_extents *allocate_soe (void);
|
|
983 static void free_soe (struct stack_of_extents *soe);
|
|
984 static void soe_invalidate (Lisp_Object obj);
|
|
985
|
|
986 static Lisp_Object mark_extent_info (Lisp_Object obj,
|
|
987 void (*markobj) (Lisp_Object));
|
|
988 static void finalize_extent_info (void *header, int for_disksave);
|
|
989 DEFINE_LRECORD_IMPLEMENTATION ("extent-info", extent_info,
|
|
990 mark_extent_info, internal_object_printer,
|
|
991 finalize_extent_info, 0, 0,
|
|
992 struct extent_info);
|
|
993
|
|
994 static Lisp_Object
|
|
995 mark_extent_info (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
996 {
|
|
997 struct extent_info *data =
|
|
998 (struct extent_info *) XEXTENT_INFO (obj);
|
|
999 int i;
|
|
1000 Extent_List *list;
|
|
1001
|
|
1002 /* Vbuffer_defaults and Vbuffer_local_symbols are buffer-like
|
|
1003 objects that are created specially and never have their extent
|
|
1004 list initialized (or rather, it is set to zero in
|
|
1005 nuke_all_buffer_slots()). However, these objects get
|
|
1006 garbage-collected so we have to deal.
|
|
1007
|
|
1008 (Also the list can be zero when we're dealing with a destroyed
|
|
1009 buffer.) */
|
|
1010
|
|
1011 list = data->extents;
|
|
1012 if (list)
|
|
1013 {
|
|
1014 for (i = 0; i < extent_list_num_els (list); i++)
|
|
1015 {
|
|
1016 struct extent *extent = extent_list_at (list, i, 0);
|
|
1017 Lisp_Object exobj = Qnil;
|
|
1018
|
|
1019 XSETEXTENT (exobj, extent);
|
|
1020 ((markobj) (exobj));
|
|
1021 }
|
|
1022 }
|
|
1023
|
|
1024 return Qnil;
|
|
1025 }
|
|
1026
|
|
1027 static void
|
|
1028 finalize_extent_info (void *header, int for_disksave)
|
|
1029 {
|
|
1030 struct extent_info *data = (struct extent_info *) header;
|
|
1031
|
|
1032 if (for_disksave)
|
|
1033 return;
|
|
1034
|
|
1035 if (data->soe)
|
|
1036 {
|
|
1037 free_soe (data->soe);
|
|
1038 data->soe = 0;
|
|
1039 }
|
|
1040 if (data->extents)
|
|
1041 {
|
|
1042 free_extent_list (data->extents);
|
|
1043 data->extents = 0;
|
|
1044 }
|
|
1045 }
|
|
1046
|
|
1047 static Lisp_Object
|
|
1048 allocate_extent_info (void)
|
|
1049 {
|
|
1050 Lisp_Object extent_info = Qnil;
|
|
1051 struct extent_info *data =
|
|
1052 alloc_lcrecord (sizeof (struct extent_info),
|
|
1053 lrecord_extent_info);
|
|
1054
|
|
1055 XSETEXTENT_INFO (extent_info, data);
|
|
1056 data->extents = allocate_extent_list ();
|
|
1057 data->soe = 0;
|
|
1058 return extent_info;
|
|
1059 }
|
|
1060
|
|
1061 void
|
|
1062 flush_cached_extent_info (Lisp_Object extent_info)
|
|
1063 {
|
|
1064 struct extent_info *data = XEXTENT_INFO (extent_info);
|
|
1065
|
|
1066 if (data->soe)
|
|
1067 {
|
|
1068 free_soe (data->soe);
|
|
1069 data->soe = 0;
|
|
1070 }
|
|
1071 }
|
|
1072
|
|
1073
|
|
1074 /************************************************************************/
|
|
1075 /* Buffer/string extent primitives */
|
|
1076 /************************************************************************/
|
|
1077
|
|
1078 /* The functions in this section are the ONLY ones that should know
|
|
1079 about the internal implementation of the extent lists. Other functions
|
|
1080 should only know that there are two orderings on extents, the "display"
|
|
1081 order (sorted by start position, basically) and the e-order (sorted
|
|
1082 by end position, basically), and that certain operations are provided
|
|
1083 to manipulate the list. */
|
|
1084
|
|
1085 /* ------------------------------- */
|
|
1086 /* basic primitives */
|
|
1087 /* ------------------------------- */
|
|
1088
|
|
1089 static Lisp_Object
|
|
1090 decode_buffer_or_string (Lisp_Object object)
|
|
1091 {
|
|
1092 if (NILP (object))
|
|
1093 XSETBUFFER (object, current_buffer);
|
|
1094 else
|
|
1095 CHECK_LIVE_BUFFER_OR_STRING (object);
|
|
1096 return object;
|
|
1097 }
|
|
1098
|
|
1099 EXTENT
|
|
1100 extent_ancestor_1 (EXTENT e)
|
|
1101 {
|
|
1102 while (e->flags.has_parent)
|
|
1103 {
|
|
1104 /* There should be no circularities except in case of a logic
|
|
1105 error somewhere in the extent code */
|
|
1106 e = XEXTENT (XEXTENT_AUXILIARY (XCAR (e->plist))->parent);
|
|
1107 }
|
|
1108 return e;
|
|
1109 }
|
|
1110
|
|
1111 /* Given an extent object (string or buffer or nil), return its extent info. This may be
|
|
1112 0 for a string. */
|
|
1113
|
|
1114 static struct extent_info *
|
|
1115 buffer_or_string_extent_info (Lisp_Object object)
|
|
1116 {
|
|
1117 if (STRINGP (object))
|
|
1118 {
|
|
1119 Lisp_Object plist = XSTRING (object)->plist;
|
|
1120 if (!CONSP (plist) || !EXTENT_INFOP (XCAR (plist)))
|
|
1121 return 0;
|
|
1122 return XEXTENT_INFO (XCAR (plist));
|
|
1123 }
|
|
1124 else if (NILP (object))
|
|
1125 return 0;
|
|
1126 else
|
|
1127 return XEXTENT_INFO (XBUFFER (object)->extent_info);
|
|
1128 }
|
|
1129
|
|
1130 /* Given a string or buffer, return its extent list. This may be
|
|
1131 0 for a string. */
|
|
1132
|
|
1133 static Extent_List *
|
|
1134 buffer_or_string_extent_list (Lisp_Object object)
|
|
1135 {
|
|
1136 struct extent_info *info = buffer_or_string_extent_info (object);
|
|
1137
|
|
1138 if (!info)
|
|
1139 return 0;
|
|
1140 return info->extents;
|
|
1141 }
|
|
1142
|
|
1143 /* Given a string or buffer, return its extent info. If it's not there,
|
|
1144 create it. */
|
|
1145
|
|
1146 static struct extent_info *
|
|
1147 buffer_or_string_extent_info_force (Lisp_Object object)
|
|
1148 {
|
|
1149 struct extent_info *info = buffer_or_string_extent_info (object);
|
|
1150
|
|
1151 if (!info)
|
|
1152 {
|
|
1153 Lisp_Object extent_info;
|
|
1154
|
|
1155 assert (STRINGP (object)); /* should never happen for buffers --
|
|
1156 the only buffers without an extent
|
|
1157 info are those after finalization,
|
|
1158 destroyed buffers, or special
|
|
1159 Lisp-inaccessible buffer objects. */
|
|
1160 extent_info = allocate_extent_info ();
|
|
1161 XSTRING (object)->plist = Fcons (extent_info, XSTRING (object)->plist);
|
|
1162 return XEXTENT_INFO (extent_info);
|
|
1163 }
|
|
1164
|
|
1165 return info;
|
|
1166 }
|
|
1167
|
|
1168 /* Detach all the extents in OBJECT. Called from redisplay. */
|
|
1169
|
|
1170 void
|
|
1171 detach_all_extents (Lisp_Object object)
|
|
1172 {
|
|
1173 struct extent_info *data = buffer_or_string_extent_info (object);
|
|
1174
|
|
1175 if (data)
|
|
1176 {
|
|
1177 if (data->extents)
|
|
1178 {
|
|
1179 int i;
|
|
1180
|
|
1181 for (i = 0; i < extent_list_num_els (data->extents); i++)
|
|
1182 {
|
|
1183 EXTENT e = extent_list_at (data->extents, i, 0);
|
|
1184 /* No need to do detach_extent(). Just nuke the damn things,
|
|
1185 which results in the equivalent but faster. */
|
|
1186 set_extent_start (e, -1);
|
|
1187 set_extent_end (e, -1);
|
|
1188 }
|
|
1189 }
|
|
1190
|
|
1191 /* But we need to clear all the lists containing extents or
|
|
1192 havoc will result. */
|
|
1193 extent_list_delete_all (data->extents);
|
|
1194 soe_invalidate (object);
|
|
1195 }
|
|
1196 }
|
|
1197
|
|
1198
|
|
1199 void
|
|
1200 init_buffer_extents (struct buffer *b)
|
|
1201 {
|
|
1202 b->extent_info = allocate_extent_info ();
|
|
1203 }
|
|
1204
|
|
1205 void
|
|
1206 uninit_buffer_extents (struct buffer *b)
|
|
1207 {
|
|
1208 struct extent_info *data = XEXTENT_INFO (b->extent_info);
|
|
1209
|
|
1210 /* Don't destroy the extents here -- there may still be children
|
|
1211 extents pointing to the extents. */
|
|
1212 detach_all_extents (make_buffer (b));
|
|
1213 finalize_extent_info (data, 0);
|
|
1214 }
|
|
1215
|
|
1216 /* Retrieve the extent list that an extent is a member of; the
|
|
1217 return value will never be 0 except in destroyed buffers (in which
|
|
1218 case the only extents that can refer to this buffer are detached
|
|
1219 ones). */
|
|
1220
|
|
1221 #define extent_extent_list(e) buffer_or_string_extent_list (extent_object (e))
|
|
1222
|
|
1223 /* ------------------------------- */
|
|
1224 /* stack of extents */
|
|
1225 /* ------------------------------- */
|
|
1226
|
|
1227 #ifdef ERROR_CHECK_EXTENTS
|
|
1228
|
|
1229 void
|
|
1230 sledgehammer_extent_check (Lisp_Object object)
|
|
1231 {
|
|
1232 int i;
|
|
1233 int endp;
|
|
1234 Extent_List *el = buffer_or_string_extent_list (object);
|
|
1235 struct buffer *buf = 0;
|
|
1236
|
|
1237 if (!el)
|
|
1238 return;
|
|
1239
|
|
1240 if (BUFFERP (object))
|
|
1241 buf = XBUFFER (object);
|
|
1242
|
|
1243 for (endp = 0; endp < 2; endp++)
|
|
1244 for (i = 1; i < extent_list_num_els (el); i++)
|
|
1245 {
|
|
1246 EXTENT e1 = extent_list_at (el, i-1, endp);
|
|
1247 EXTENT e2 = extent_list_at (el, i, endp);
|
|
1248 if (buf)
|
|
1249 {
|
|
1250 assert (extent_start (e1) <= buf->text->gpt ||
|
|
1251 extent_start (e1) > buf->text->gpt + buf->text->gap_size);
|
|
1252 assert (extent_end (e1) <= buf->text->gpt ||
|
|
1253 extent_end (e1) > buf->text->gpt + buf->text->gap_size);
|
|
1254 }
|
|
1255 assert (extent_start (e1) <= extent_end (e1));
|
|
1256 assert (endp ? (EXTENT_E_LESS_EQUAL (e1, e2)) :
|
|
1257 (EXTENT_LESS_EQUAL (e1, e2)));
|
|
1258 }
|
|
1259 }
|
|
1260
|
|
1261 #endif
|
|
1262
|
|
1263 static Stack_Of_Extents *
|
|
1264 buffer_or_string_stack_of_extents (Lisp_Object object)
|
|
1265 {
|
|
1266 struct extent_info *info = buffer_or_string_extent_info (object);
|
|
1267 if (!info)
|
|
1268 return 0;
|
|
1269 return info->soe;
|
|
1270 }
|
|
1271
|
|
1272 static Stack_Of_Extents *
|
|
1273 buffer_or_string_stack_of_extents_force (Lisp_Object object)
|
|
1274 {
|
|
1275 struct extent_info *info = buffer_or_string_extent_info_force (object);
|
|
1276 if (!info->soe)
|
|
1277 info->soe = allocate_soe ();
|
|
1278 return info->soe;
|
|
1279 }
|
|
1280
|
|
1281 /* #define SOE_DEBUG */
|
|
1282
|
|
1283 #ifdef SOE_DEBUG
|
|
1284
|
|
1285 static char *print_extent_1 (char *buf, Lisp_Object extent);
|
|
1286
|
|
1287 static void
|
|
1288 print_extent_2 (EXTENT e)
|
|
1289 {
|
|
1290 Lisp_Object extent;
|
|
1291 char buf[200];
|
|
1292
|
|
1293 XSETEXTENT (extent, e);
|
|
1294 print_extent_1 (buf, extent);
|
|
1295 printf ("%s", buf);
|
|
1296 }
|
|
1297
|
|
1298 static void
|
|
1299 soe_dump (Lisp_Object obj)
|
|
1300 {
|
|
1301 int i;
|
|
1302 Stack_Of_Extents *soe = buffer_or_string_stack_of_extents (obj);
|
|
1303 Extent_List *sel;
|
|
1304 int endp;
|
|
1305
|
|
1306 if (!soe)
|
|
1307 {
|
|
1308 printf ("No SOE");
|
|
1309 return;
|
|
1310 }
|
|
1311 sel = soe->extents;
|
|
1312 printf ("SOE pos is %d (memind %d)\n",
|
|
1313 soe->pos < 0 ? soe->pos :
|
|
1314 buffer_or_string_memind_to_bytind (obj, soe->pos),
|
|
1315 soe->pos);
|
|
1316 for (endp = 0; endp < 2; endp++)
|
|
1317 {
|
|
1318 printf (endp ? "SOE end:" : "SOE start:");
|
|
1319 for (i = 0; i < extent_list_num_els (sel); i++)
|
|
1320 {
|
|
1321 EXTENT e = extent_list_at (sel, i, endp);
|
|
1322 printf ("\t");
|
|
1323 print_extent_2 (e);
|
|
1324 }
|
|
1325 printf ("\n");
|
|
1326 }
|
|
1327 printf ("\n");
|
|
1328 }
|
|
1329
|
|
1330 #endif
|
|
1331
|
|
1332 /* Insert EXTENT into OBJ's stack of extents, if necessary. */
|
|
1333
|
|
1334 static void
|
|
1335 soe_insert (Lisp_Object obj, EXTENT extent)
|
|
1336 {
|
|
1337 Stack_Of_Extents *soe = buffer_or_string_stack_of_extents (obj);
|
|
1338
|
|
1339 #ifdef SOE_DEBUG
|
|
1340 printf ("Inserting into SOE: ");
|
|
1341 print_extent_2 (extent);
|
|
1342 printf ("\n");
|
|
1343 #endif
|
|
1344 if (!soe || soe->pos < extent_start (extent) ||
|
|
1345 soe->pos > extent_end (extent))
|
|
1346 {
|
|
1347 #ifdef SOE_DEBUG
|
|
1348 printf ("(not needed)\n\n");
|
|
1349 #endif
|
|
1350 return;
|
|
1351 }
|
|
1352 extent_list_insert (soe->extents, extent);
|
|
1353 #ifdef SOE_DEBUG
|
|
1354 printf ("SOE afterwards is:\n");
|
|
1355 soe_dump (obj);
|
|
1356 #endif
|
|
1357 }
|
|
1358
|
|
1359 /* Delete EXTENT from OBJ's stack of extents, if necessary. */
|
|
1360
|
|
1361 static void
|
|
1362 soe_delete (Lisp_Object obj, EXTENT extent)
|
|
1363 {
|
|
1364 Stack_Of_Extents *soe = buffer_or_string_stack_of_extents (obj);
|
|
1365
|
|
1366 #ifdef SOE_DEBUG
|
|
1367 printf ("Deleting from SOE: ");
|
|
1368 print_extent_2 (extent);
|
|
1369 printf ("\n");
|
|
1370 #endif
|
|
1371 if (!soe || soe->pos < extent_start (extent) ||
|
|
1372 soe->pos > extent_end (extent))
|
|
1373 {
|
|
1374 #ifdef SOE_DEBUG
|
|
1375 printf ("(not needed)\n\n");
|
|
1376 #endif
|
|
1377 return;
|
|
1378 }
|
|
1379 extent_list_delete (soe->extents, extent);
|
|
1380 #ifdef SOE_DEBUG
|
|
1381 printf ("SOE afterwards is:\n");
|
|
1382 soe_dump (obj);
|
|
1383 #endif
|
|
1384 }
|
|
1385
|
|
1386 /* Move OBJ's stack of extents to lie over the specified position. */
|
|
1387
|
|
1388 static void
|
|
1389 soe_move (Lisp_Object obj, Memind pos)
|
|
1390 {
|
|
1391 Stack_Of_Extents *soe = buffer_or_string_stack_of_extents_force (obj);
|
|
1392 Extent_List *sel = soe->extents;
|
|
1393 int numsoe = extent_list_num_els (sel);
|
|
1394 Extent_List *bel = buffer_or_string_extent_list (obj);
|
|
1395 int direction;
|
|
1396 int endp;
|
|
1397
|
|
1398 #ifdef ERROR_CHECK_EXTENTS
|
|
1399 assert (bel);
|
|
1400 #endif
|
|
1401
|
|
1402 #ifdef SOE_DEBUG
|
|
1403 printf ("Moving SOE from %d (memind %d) to %d (memind %d)\n",
|
|
1404 soe->pos < 0 ? soe->pos :
|
|
1405 buffer_or_string_memind_to_bytind (obj, soe->pos), soe->pos,
|
|
1406 buffer_or_string_memind_to_bytind (obj, pos), pos);
|
|
1407 #endif
|
|
1408 if (soe->pos < pos)
|
|
1409 {
|
|
1410 direction = 1;
|
|
1411 endp = 0;
|
|
1412 }
|
|
1413 else if (soe->pos > pos)
|
|
1414 {
|
|
1415 direction = -1;
|
|
1416 endp = 1;
|
|
1417 }
|
|
1418 else
|
|
1419 {
|
|
1420 #ifdef SOE_DEBUG
|
|
1421 printf ("(not needed)\n\n");
|
|
1422 #endif
|
|
1423 return;
|
|
1424 }
|
|
1425
|
|
1426 /* For DIRECTION = 1: Any extent that overlaps POS is either in the
|
|
1427 SOE (if the extent starts at or before SOE->POS) or is greater
|
|
1428 (in the display order) than any extent in the SOE (if it starts
|
|
1429 after SOE->POS).
|
|
1430
|
|
1431 For DIRECTION = -1: Any extent that overlaps POS is either in the
|
|
1432 SOE (if the extent ends at or after SOE->POS) or is less (in the
|
|
1433 e-order) than any extent in the SOE (if it ends before SOE->POS).
|
|
1434
|
|
1435 We proceed in two stages:
|
|
1436
|
|
1437 1) delete all extents in the SOE that don't overlap POS.
|
|
1438 2) insert all extents into the SOE that start (or end, when
|
|
1439 DIRECTION = -1) in (SOE->POS, POS] and that overlap
|
|
1440 POS. (Don't include SOE->POS in the range because those
|
|
1441 extents would already be in the SOE.)
|
|
1442 */
|
|
1443
|
|
1444 /* STAGE 1. */
|
|
1445
|
|
1446 if (numsoe > 0)
|
|
1447 {
|
|
1448 /* Delete all extents in the SOE that don't overlap POS.
|
|
1449 This is all extents that end before (or start after,
|
|
1450 if DIRECTION = -1) POS.
|
|
1451 */
|
|
1452
|
|
1453 /* Deleting extents from the SOE is tricky because it changes
|
|
1454 the positions of extents. If we are deleting in the forward
|
|
1455 direction we have to call extent_list_at() on the same position
|
|
1456 over and over again because positions after the deleted element
|
|
1457 get shifted back by 1. To make life simplest, we delete forward
|
|
1458 irrespective of DIRECTION.
|
|
1459 */
|
|
1460 int start, end;
|
|
1461 int i;
|
|
1462
|
|
1463 if (direction > 0)
|
|
1464 {
|
|
1465 start = 0;
|
|
1466 end = extent_list_locate_from_pos (sel, pos, 1);
|
|
1467 }
|
|
1468 else
|
|
1469 {
|
|
1470 start = extent_list_locate_from_pos (sel, pos+1, 0);
|
|
1471 end = numsoe;
|
|
1472 }
|
|
1473
|
|
1474 for (i = start; i < end; i++)
|
|
1475 extent_list_delete (sel, extent_list_at (sel, start /* see above */,
|
|
1476 !endp));
|
|
1477 }
|
|
1478
|
|
1479 /* STAGE 2. */
|
|
1480
|
|
1481 {
|
|
1482 int start_pos;
|
|
1483
|
|
1484 if (direction < 0)
|
|
1485 start_pos = extent_list_locate_from_pos (bel, soe->pos, endp) - 1;
|
|
1486 else
|
|
1487 start_pos = extent_list_locate_from_pos (bel, soe->pos + 1, endp);
|
|
1488
|
|
1489 for (; start_pos >= 0 && start_pos < extent_list_num_els (bel);
|
|
1490 start_pos += direction)
|
|
1491 {
|
|
1492 EXTENT e = extent_list_at (bel, start_pos, endp);
|
|
1493 if ((direction > 0) ?
|
|
1494 (extent_start (e) > pos) :
|
|
1495 (extent_end (e) < pos))
|
|
1496 break; /* All further extents lie on the far side of POS
|
|
1497 and thus can't overlap. */
|
|
1498 if ((direction > 0) ?
|
|
1499 (extent_end (e) >= pos) :
|
|
1500 (extent_start (e) <= pos))
|
|
1501 extent_list_insert (sel, e);
|
|
1502 }
|
|
1503 }
|
|
1504
|
|
1505 soe->pos = pos;
|
|
1506 #ifdef SOE_DEBUG
|
|
1507 printf ("SOE afterwards is:\n");
|
|
1508 soe_dump (obj);
|
|
1509 #endif
|
|
1510 }
|
|
1511
|
|
1512 static void
|
|
1513 soe_invalidate (Lisp_Object obj)
|
|
1514 {
|
|
1515 Stack_Of_Extents *soe = buffer_or_string_stack_of_extents (obj);
|
|
1516
|
|
1517 if (soe)
|
|
1518 {
|
|
1519 extent_list_delete_all (soe->extents);
|
|
1520 soe->pos = -1;
|
|
1521 }
|
|
1522 }
|
|
1523
|
|
1524 static struct stack_of_extents *
|
|
1525 allocate_soe (void)
|
|
1526 {
|
|
1527 struct stack_of_extents *soe =
|
|
1528 malloc_type_and_zero (struct stack_of_extents);
|
|
1529 soe->extents = allocate_extent_list ();
|
|
1530 soe->pos = -1;
|
|
1531 return soe;
|
|
1532 }
|
|
1533
|
|
1534 static void
|
|
1535 free_soe (struct stack_of_extents *soe)
|
|
1536 {
|
|
1537 free_extent_list (soe->extents);
|
|
1538 xfree (soe);
|
|
1539 }
|
|
1540
|
|
1541 /* ------------------------------- */
|
|
1542 /* other primitives */
|
|
1543 /* ------------------------------- */
|
|
1544
|
|
1545 /* Return the start (endp == 0) or end (endp == 1) of an extent as
|
|
1546 a byte index. If you want the value as a memory index, use
|
|
1547 extent_endpoint(). If you want the value as a buffer position,
|
|
1548 use extent_endpoint_bufpos(). */
|
|
1549
|
|
1550 static Bytind
|
|
1551 extent_endpoint_bytind (EXTENT extent, int endp)
|
|
1552 {
|
|
1553 assert (EXTENT_LIVE_P (extent));
|
|
1554 assert (!extent_detached_p (extent));
|
|
1555 {
|
|
1556 Memind i = (endp) ? (extent_end (extent)) :
|
|
1557 (extent_start (extent));
|
|
1558 Lisp_Object obj = extent_object (extent);
|
|
1559 return buffer_or_string_memind_to_bytind (obj, i);
|
|
1560 }
|
|
1561 }
|
|
1562
|
|
1563 static Bufpos
|
|
1564 extent_endpoint_bufpos (EXTENT extent, int endp)
|
|
1565 {
|
|
1566 assert (EXTENT_LIVE_P (extent));
|
|
1567 assert (!extent_detached_p (extent));
|
|
1568 {
|
|
1569 Memind i = (endp) ? (extent_end (extent)) :
|
|
1570 (extent_start (extent));
|
|
1571 Lisp_Object obj = extent_object (extent);
|
|
1572 return buffer_or_string_memind_to_bufpos (obj, i);
|
|
1573 }
|
|
1574 }
|
|
1575
|
|
1576 /* A change to an extent occurred that will change the display, so
|
|
1577 notify redisplay. Maybe also recurse over all the extent's
|
|
1578 descendants. */
|
|
1579
|
|
1580 static void
|
|
1581 extent_changed_for_redisplay (EXTENT extent, int descendants_too)
|
|
1582 {
|
|
1583 Lisp_Object object;
|
|
1584 Lisp_Object rest;
|
|
1585
|
|
1586 /* we could easily encounter a detached extent while traversing the
|
|
1587 children, but we should never be able to encounter a dead extent. */
|
|
1588 assert (EXTENT_LIVE_P (extent));
|
|
1589
|
|
1590 if (descendants_too)
|
|
1591 {
|
|
1592 Lisp_Object children = extent_children (extent);
|
|
1593
|
|
1594 if (!NILP (children))
|
|
1595 {
|
|
1596 /* first mark all of the extent's children. We will lose big-time
|
|
1597 if there are any circularities here, so we sure as hell better
|
|
1598 ensure that there aren't. */
|
|
1599 LIST_LOOP (rest, XWEAK_LIST_LIST (children))
|
|
1600 extent_changed_for_redisplay (XEXTENT (XCAR (rest)), 1);
|
|
1601 }
|
|
1602 }
|
|
1603
|
|
1604 /* now mark the extent itself. */
|
|
1605
|
|
1606 object = extent_object (extent);
|
|
1607
|
|
1608 if (!BUFFERP (object) || extent_detached_p (extent))
|
|
1609 /* #### Can changes to string extents affect redisplay?
|
|
1610 I will have to think about this. What about string glyphs?
|
|
1611 Things in the modeline? etc. */
|
|
1612 /* #### changes to string extents can certainly affect redisplay
|
|
1613 if the extent is in some generated-modeline-string: when
|
|
1614 we change an extent in generated-modeline-string, this changes
|
|
1615 its parent, which is in `modeline-format', so we should
|
|
1616 force the modeline to be updated. But how to determine whether
|
|
1617 a string is a `generated-modeline-string'? Looping through
|
|
1618 all buffers is not very efficient. Should we add all
|
|
1619 `generated-modeline-string' strings to a hashtable?
|
|
1620 Maybe efficiency is not the greatest concern here and there's
|
|
1621 no big loss in looping over the buffers. */
|
|
1622 return;
|
|
1623
|
|
1624 {
|
|
1625 struct buffer *b;
|
|
1626 b = XBUFFER (object);
|
|
1627 BUF_FACECHANGE (b)++;
|
|
1628 MARK_EXTENTS_CHANGED;
|
|
1629 buffer_extent_signal_changed_region (b,
|
|
1630 extent_endpoint_bufpos (extent, 0),
|
|
1631 extent_endpoint_bufpos (extent, 1));
|
|
1632 }
|
|
1633 }
|
|
1634
|
|
1635 /* A change to an extent occurred that will might affect redisplay.
|
|
1636 This is called when properties such as the endpoints, the layout,
|
|
1637 or the priority changes. Redisplay will be affected only if
|
|
1638 the extent has any displayable attributes. */
|
|
1639
|
|
1640 static void
|
|
1641 extent_maybe_changed_for_redisplay (EXTENT extent, int descendants_too)
|
|
1642 {
|
|
1643 /* Retrieve the ancestor for efficiency */
|
|
1644 EXTENT anc = extent_ancestor (extent);
|
|
1645 if (!NILP (extent_face (anc)) || !NILP (extent_begin_glyph (anc)) ||
|
|
1646 !NILP (extent_end_glyph (anc)) || !NILP (extent_mouse_face (anc)) ||
|
|
1647 !NILP (extent_invisible (anc)))
|
|
1648 extent_changed_for_redisplay (extent, descendants_too);
|
|
1649 }
|
|
1650
|
|
1651 static EXTENT
|
|
1652 make_extent_detached (Lisp_Object object)
|
|
1653 {
|
|
1654 EXTENT extent = allocate_extent ();
|
|
1655
|
|
1656 assert (NILP (object) || STRINGP (object) ||
|
|
1657 (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))));
|
|
1658 extent_object (extent) = object;
|
|
1659 /* Now make sure the extent info exists. */
|
|
1660 if (!NILP (object))
|
|
1661 (void) buffer_or_string_extent_info_force (object);
|
|
1662 return extent;
|
|
1663 }
|
|
1664
|
|
1665 /* A "real" extent is any extent other than the internal (not-user-visible)
|
|
1666 extents used by `map-extents'. */
|
|
1667
|
|
1668 static EXTENT
|
|
1669 real_extent_at_forward (Extent_List *el, int pos, int endp)
|
|
1670 {
|
|
1671 for (; pos < extent_list_num_els (el); pos++)
|
|
1672 {
|
|
1673 EXTENT e = extent_list_at (el, pos, endp);
|
|
1674 if (!extent_internal_p (e))
|
|
1675 return e;
|
|
1676 }
|
|
1677 return 0;
|
|
1678 }
|
|
1679
|
|
1680 static EXTENT
|
|
1681 real_extent_at_backward (Extent_List *el, int pos, int endp)
|
|
1682 {
|
|
1683 for (; pos >= 0; pos--)
|
|
1684 {
|
|
1685 EXTENT e = extent_list_at (el, pos, endp);
|
|
1686 if (!extent_internal_p (e))
|
|
1687 return e;
|
|
1688 }
|
|
1689 return 0;
|
|
1690 }
|
|
1691
|
|
1692 static EXTENT
|
|
1693 extent_first (Lisp_Object obj)
|
|
1694 {
|
|
1695 Extent_List *el = buffer_or_string_extent_list (obj);
|
|
1696
|
|
1697 if (!el)
|
|
1698 return 0;
|
|
1699 return real_extent_at_forward (el, 0, 0);
|
|
1700 }
|
|
1701
|
|
1702 #ifdef DEBUG_XEMACS
|
|
1703 static EXTENT
|
|
1704 extent_e_first (Lisp_Object obj)
|
|
1705 {
|
|
1706 Extent_List *el = buffer_or_string_extent_list (obj);
|
|
1707
|
|
1708 if (!el)
|
|
1709 return 0;
|
|
1710 return real_extent_at_forward (el, 0, 1);
|
|
1711 }
|
|
1712 #endif
|
|
1713
|
|
1714 static EXTENT
|
|
1715 extent_next (EXTENT e)
|
|
1716 {
|
|
1717 Extent_List *el = extent_extent_list (e);
|
|
1718 int foundp;
|
|
1719 int pos;
|
|
1720
|
|
1721 pos = extent_list_locate (el, e, 0, &foundp);
|
|
1722 assert (foundp);
|
|
1723 return real_extent_at_forward (el, pos+1, 0);
|
|
1724 }
|
|
1725
|
|
1726 #ifdef DEBUG_XEMACS
|
|
1727 static EXTENT
|
|
1728 extent_e_next (EXTENT e)
|
|
1729 {
|
|
1730 Extent_List *el = extent_extent_list (e);
|
|
1731 int foundp;
|
|
1732 int pos;
|
|
1733
|
|
1734 pos = extent_list_locate (el, e, 1, &foundp);
|
|
1735 assert (foundp);
|
|
1736 return real_extent_at_forward (el, pos+1, 1);
|
|
1737 }
|
|
1738 #endif
|
|
1739
|
|
1740 static EXTENT
|
|
1741 extent_last (Lisp_Object obj)
|
|
1742 {
|
|
1743 Extent_List *el = buffer_or_string_extent_list (obj);
|
|
1744
|
|
1745 if (!el)
|
|
1746 return 0;
|
|
1747 return real_extent_at_backward (el, extent_list_num_els (el) - 1, 0);
|
|
1748 }
|
|
1749
|
|
1750 #ifdef DEBUG_XEMACS
|
|
1751 static EXTENT
|
|
1752 extent_e_last (Lisp_Object obj)
|
|
1753 {
|
|
1754 Extent_List *el = buffer_or_string_extent_list (obj);
|
|
1755
|
|
1756 if (!el)
|
|
1757 return 0;
|
|
1758 return real_extent_at_backward (el, extent_list_num_els (el) - 1, 1);
|
|
1759 }
|
|
1760 #endif
|
|
1761
|
|
1762 static EXTENT
|
|
1763 extent_previous (EXTENT e)
|
|
1764 {
|
|
1765 Extent_List *el = extent_extent_list (e);
|
|
1766 int foundp;
|
|
1767 int pos;
|
|
1768
|
|
1769 pos = extent_list_locate (el, e, 0, &foundp);
|
|
1770 assert (foundp);
|
|
1771 return real_extent_at_backward (el, pos-1, 0);
|
|
1772 }
|
|
1773
|
|
1774 #ifdef DEBUG_XEMACS
|
|
1775 static EXTENT
|
|
1776 extent_e_previous (EXTENT e)
|
|
1777 {
|
|
1778 Extent_List *el = extent_extent_list (e);
|
|
1779 int foundp;
|
|
1780 int pos;
|
|
1781
|
|
1782 pos = extent_list_locate (el, e, 1, &foundp);
|
|
1783 assert (foundp);
|
|
1784 return real_extent_at_backward (el, pos-1, 1);
|
|
1785 }
|
|
1786 #endif
|
|
1787
|
|
1788 static void
|
|
1789 extent_attach (EXTENT extent)
|
|
1790 {
|
|
1791 Extent_List *el = extent_extent_list (extent);
|
|
1792
|
|
1793 extent_list_insert (el, extent);
|
|
1794 soe_insert (extent_object (extent), extent);
|
|
1795 /* only this extent changed */
|
|
1796 extent_maybe_changed_for_redisplay (extent, 0);
|
|
1797 }
|
|
1798
|
|
1799 static void
|
|
1800 extent_detach (EXTENT extent)
|
|
1801 {
|
|
1802 Extent_List *el;
|
|
1803
|
|
1804 if (extent_detached_p (extent))
|
|
1805 return;
|
|
1806 el = extent_extent_list (extent);
|
|
1807
|
|
1808 /* call this before messing with the extent. */
|
|
1809 extent_maybe_changed_for_redisplay (extent, 0);
|
|
1810 extent_list_delete (el, extent);
|
|
1811 soe_delete (extent_object (extent), extent);
|
|
1812 set_extent_start (extent, -1);
|
|
1813 set_extent_end (extent, -1);
|
|
1814 }
|
|
1815
|
|
1816 /* ------------------------------- */
|
|
1817 /* map-extents et al. */
|
|
1818 /* ------------------------------- */
|
|
1819
|
|
1820 /* Returns true iff map_extents() would visit the given extent.
|
|
1821 See the comments at map_extents() for info on the overlap rule.
|
|
1822 Assumes that all validation on the extent and buffer positions has
|
|
1823 already been performed (see Fextent_in_region_p ()).
|
|
1824 */
|
|
1825 static int
|
|
1826 extent_in_region_p (EXTENT extent, Bytind from, Bytind to,
|
|
1827 unsigned int flags)
|
|
1828 {
|
|
1829 Lisp_Object obj = extent_object (extent);
|
|
1830 Endpoint_Index start, end, exs, exe;
|
|
1831 int start_open, end_open;
|
|
1832 unsigned int all_extents_flags = flags & ME_ALL_EXTENTS_MASK;
|
|
1833 unsigned int in_region_flags = flags & ME_IN_REGION_MASK;
|
|
1834 int retval;
|
|
1835
|
|
1836 /* A zero-length region is treated as closed-closed. */
|
|
1837 if (from == to)
|
|
1838 {
|
|
1839 flags |= ME_END_CLOSED;
|
|
1840 flags &= ~ME_START_OPEN;
|
|
1841 }
|
|
1842
|
|
1843 switch (all_extents_flags)
|
|
1844 {
|
|
1845 case ME_ALL_EXTENTS_CLOSED:
|
|
1846 start_open = end_open = 0; break;
|
|
1847 case ME_ALL_EXTENTS_OPEN:
|
|
1848 start_open = end_open = 1; break;
|
|
1849 case ME_ALL_EXTENTS_CLOSED_OPEN:
|
|
1850 start_open = 0; end_open = 1; break;
|
|
1851 case ME_ALL_EXTENTS_OPEN_CLOSED:
|
|
1852 start_open = 1; end_open = 0; break;
|
|
1853 default:
|
|
1854 start_open = extent_start_open_p (extent);
|
|
1855 end_open = extent_end_open_p (extent);
|
|
1856 break;
|
|
1857 }
|
|
1858
|
|
1859 /* So is a zero-length extent. */
|
|
1860 if (extent_start (extent) == extent_end (extent))
|
|
1861 start_open = end_open = 0;
|
|
1862
|
|
1863 start = buffer_or_string_bytind_to_startind (obj, from,
|
|
1864 flags & ME_START_OPEN);
|
|
1865 end = buffer_or_string_bytind_to_endind (obj, to, ! (flags & ME_END_CLOSED));
|
|
1866 exs = memind_to_startind (extent_start (extent), start_open);
|
|
1867 exe = memind_to_endind (extent_end (extent), end_open);
|
|
1868
|
|
1869 /* It's easy to determine whether an extent lies *outside* the
|
|
1870 region -- just determine whether it's completely before
|
|
1871 or completely after the region. Reject all such extents, so
|
|
1872 we're now left with only the extents that overlap the region.
|
|
1873 */
|
|
1874
|
|
1875 if (exs > end || exe < start)
|
|
1876 return 0;
|
|
1877
|
|
1878 /* See if any further restrictions are called for. */
|
|
1879 switch (in_region_flags)
|
|
1880 {
|
|
1881 case ME_START_IN_REGION:
|
|
1882 retval = start <= exs && exs <= end; break;
|
|
1883 case ME_END_IN_REGION:
|
|
1884 retval = start <= exe && exe <= end; break;
|
|
1885 case ME_START_AND_END_IN_REGION:
|
|
1886 retval = start <= exs && exe <= end; break;
|
|
1887 case ME_START_OR_END_IN_REGION:
|
|
1888 retval = (start <= exs && exs <= end) || (start <= exe && exe <= end);
|
|
1889 break;
|
|
1890 default:
|
|
1891 retval = 1; break;
|
|
1892 }
|
|
1893 return flags & ME_NEGATE_IN_REGION ? !retval : retval;
|
|
1894 }
|
|
1895
|
|
1896 struct map_extents_struct
|
|
1897 {
|
|
1898 Extent_List *el;
|
|
1899 Extent_List_Marker *mkr;
|
|
1900 EXTENT range;
|
|
1901 };
|
|
1902
|
|
1903 static Lisp_Object
|
|
1904 map_extents_unwind (Lisp_Object obj)
|
|
1905 {
|
|
1906 struct map_extents_struct *closure =
|
|
1907 (struct map_extents_struct *) get_opaque_ptr (obj);
|
|
1908 free_opaque_ptr (obj);
|
|
1909 if (closure->range)
|
|
1910 extent_detach (closure->range);
|
|
1911 if (closure->mkr)
|
|
1912 extent_list_delete_marker (closure->el, closure->mkr);
|
|
1913 return Qnil;
|
|
1914 }
|
|
1915
|
|
1916 /* This is the guts of `map-extents' and the other functions that
|
|
1917 map over extents. In theory the operation of this function is
|
|
1918 simple: just figure out what extents we're mapping over, and
|
|
1919 call the function on each one of them in the range. Unfortunately
|
|
1920 there are a wide variety of things that the mapping function
|
|
1921 might do, and we have to be very tricky to avoid getting messed
|
|
1922 up. Furthermore, this function needs to be very fast (it is
|
|
1923 called multiple times every time text is inserted or deleted
|
|
1924 from a buffer), and so we can't always afford the overhead of
|
|
1925 dealing with all the possible things that the mapping function
|
|
1926 might do; thus, there are many flags that can be specified
|
|
1927 indicating what the mapping function might or might not do.
|
|
1928
|
|
1929 The result of all this is that this is the most complicated
|
|
1930 function in this file. Change it at your own risk!
|
|
1931
|
|
1932 A potential simplification to the logic below is to determine
|
|
1933 all the extents that the mapping function should be called on
|
|
1934 before any calls are actually made and save them in an array.
|
|
1935 That introduces its own complications, however (the array
|
|
1936 needs to be marked for garbage-collection, and a static array
|
|
1937 cannot be used because map_extents() needs to be reentrant).
|
|
1938 Furthermore, the results might be a little less sensible than
|
|
1939 the logic below. */
|
|
1940
|
|
1941
|
|
1942 static void
|
|
1943 map_extents_bytind (Bytind from, Bytind to,
|
|
1944 int (*fn) (EXTENT extent, void *arg), void *arg,
|
|
1945 Lisp_Object obj, EXTENT after, unsigned int flags)
|
|
1946 {
|
|
1947 Memind st, en; /* range we're mapping over */
|
|
1948 EXTENT range = 0; /* extent for this, if ME_MIGHT_MODIFY_TEXT */
|
|
1949 Extent_List *el = 0; /* extent list we're iterating over */
|
|
1950 Extent_List_Marker *posm = 0; /* marker for extent list,
|
|
1951 if ME_MIGHT_MODIFY_EXTENTS */
|
|
1952 /* count and struct for unwind-protect, if ME_MIGHT_THROW */
|
|
1953 int count = 0;
|
|
1954 struct map_extents_struct closure;
|
|
1955
|
|
1956 #ifdef ERROR_CHECK_EXTENTS
|
|
1957 assert (from <= to);
|
|
1958 assert (from >= buffer_or_string_absolute_begin_byte (obj) &&
|
|
1959 from <= buffer_or_string_absolute_end_byte (obj) &&
|
|
1960 to >= buffer_or_string_absolute_begin_byte (obj) &&
|
|
1961 to <= buffer_or_string_absolute_end_byte (obj));
|
|
1962 #endif
|
|
1963
|
|
1964 if (after)
|
|
1965 {
|
|
1966 assert (EQ (obj, extent_object (after)));
|
|
1967 assert (!extent_detached_p (after));
|
|
1968 }
|
|
1969
|
14
|
1970 el = buffer_or_string_extent_list (obj);
|
|
1971 if (!el || !extent_list_num_els(el))
|
0
|
1972 return;
|
14
|
1973 el = 0;
|
0
|
1974
|
|
1975 st = buffer_or_string_bytind_to_memind (obj, from);
|
|
1976 en = buffer_or_string_bytind_to_memind (obj, to);
|
|
1977
|
|
1978 if (flags & ME_MIGHT_MODIFY_TEXT)
|
|
1979 {
|
|
1980 /* The mapping function might change the text in the buffer,
|
|
1981 so make an internal extent to hold the range we're mapping
|
|
1982 over. */
|
|
1983 range = make_extent_detached (obj);
|
|
1984 set_extent_start (range, st);
|
|
1985 set_extent_end (range, en);
|
|
1986 range->flags.start_open = flags & ME_START_OPEN;
|
|
1987 range->flags.end_open = !(flags & ME_END_CLOSED);
|
|
1988 range->flags.internal = 1;
|
|
1989 range->flags.detachable = 0;
|
|
1990 extent_attach (range);
|
|
1991 }
|
|
1992
|
|
1993 if (flags & ME_MIGHT_THROW)
|
|
1994 {
|
|
1995 /* The mapping function might throw past us so we need to use an
|
|
1996 unwind_protect() to eliminate the internal extent and range
|
|
1997 that we use. */
|
|
1998 count = specpdl_depth ();
|
|
1999 closure.range = range;
|
|
2000 closure.mkr = 0;
|
|
2001 record_unwind_protect (map_extents_unwind,
|
|
2002 make_opaque_ptr (&closure));
|
|
2003 }
|
|
2004
|
|
2005 /* ---------- Figure out where we start and what direction
|
|
2006 we move in. This is the trickiest part of this
|
|
2007 function. ---------- */
|
|
2008
|
|
2009 /* If ME_START_IN_REGION, ME_END_IN_REGION or ME_START_AND_END_IN_REGION
|
|
2010 was specified and ME_NEGATE_IN_REGION was not specified, our job
|
|
2011 is simple because of the presence of the display order and e-order.
|
|
2012 (Note that theoretically do something similar for
|
|
2013 ME_START_OR_END_IN_REGION, but that would require more trickiness
|
|
2014 than it's worth to avoid hitting the same extent twice.)
|
|
2015
|
|
2016 In the general case, all the extents that overlap a range can be
|
|
2017 divided into two classes: those whose start position lies within
|
|
2018 the range (including the range's end but not including the
|
|
2019 range's start), and those that overlap the start position,
|
|
2020 i.e. those in the SOE for the start position. Or equivalently,
|
|
2021 the extents can be divided into those whose end position lies
|
|
2022 within the range and those in the SOE for the end position. Note
|
|
2023 that for this purpose we treat both the range and all extents in
|
|
2024 the buffer as closed on both ends. If this is not what the ME_
|
|
2025 flags specified, then we've mapped over a few too many extents,
|
|
2026 but no big deal because extent_in_region_p() will filter them
|
|
2027 out. Ideally, we could move the SOE to the closer of the range's
|
|
2028 two ends and work forwards or backwards from there. However, in
|
|
2029 order to make the semantics of the AFTER argument work out, we
|
|
2030 have to always go in the same direction; so we choose to always
|
|
2031 move the SOE to the start position.
|
|
2032
|
|
2033 When it comes time to do the SOE stage, we first call soe_move()
|
|
2034 so that the SOE gets set up. Note that the SOE might get
|
|
2035 changed while we are mapping over its contents. If we can
|
|
2036 guarantee that the SOE won't get moved to a new position, we
|
|
2037 simply need to put a marker in the SOE and we will track deletions
|
|
2038 and insertions of extents in the SOE. If the SOE might get moved,
|
|
2039 however (this would happen as a result of a recursive invocation
|
|
2040 of map-extents or a call to a redisplay-type function), then
|
|
2041 trying to track its changes is hopeless, so we just keep a
|
|
2042 marker to the first (or last) extent in the SOE and use that as
|
|
2043 our bound.
|
|
2044
|
|
2045 Finally, if DONT_USE_SOE is defined, we don't use the SOE at all
|
|
2046 and instead just map from the beginning of the buffer. This is
|
|
2047 used for testing purposes and allows the SOE to be calculated
|
|
2048 using map_extents() instead of the other way around. */
|
|
2049
|
|
2050 {
|
|
2051 int range_flag; /* ME_*_IN_REGION subset of flags */
|
|
2052 int do_soe_stage = 0; /* Are we mapping over the SOE? */
|
|
2053 /* Does the range stage map over start or end positions? */
|
|
2054 int range_endp;
|
|
2055 /* If type == 0, we include the start position in the range stage mapping.
|
|
2056 If type == 1, we exclude the start position in the range stage mapping.
|
|
2057 If type == 2, we begin at range_start_pos, an extent-list position.
|
|
2058 */
|
|
2059 int range_start_type = 0;
|
|
2060 int range_start_pos = 0;
|
|
2061 int stage;
|
|
2062
|
|
2063 range_flag = flags & ME_IN_REGION_MASK;
|
|
2064 if ((range_flag == ME_START_IN_REGION ||
|
|
2065 range_flag == ME_START_AND_END_IN_REGION) &&
|
|
2066 !(flags & ME_NEGATE_IN_REGION))
|
|
2067 {
|
|
2068 /* map over start position in [range-start, range-end]. No SOE
|
|
2069 stage. */
|
|
2070 range_endp = 0;
|
|
2071 }
|
|
2072 else if (range_flag == ME_END_IN_REGION && !(flags & ME_NEGATE_IN_REGION))
|
|
2073 {
|
|
2074 /* map over end position in [range-start, range-end]. No SOE
|
|
2075 stage. */
|
|
2076 range_endp = 1;
|
|
2077 }
|
|
2078 else
|
|
2079 {
|
|
2080 /* Need to include the SOE extents. */
|
|
2081 #ifdef DONT_USE_SOE
|
|
2082 /* Just brute-force it: start from the beginning. */
|
|
2083 range_endp = 0;
|
|
2084 range_start_type = 2;
|
|
2085 range_start_pos = 0;
|
|
2086 #else
|
|
2087 Stack_Of_Extents *soe = buffer_or_string_stack_of_extents_force (obj);
|
|
2088 int numsoe;
|
|
2089
|
|
2090 /* Move the SOE to the closer end of the range. This dictates
|
|
2091 whether we map over start positions or end positions. */
|
|
2092 range_endp = 0;
|
|
2093 soe_move (obj, st);
|
|
2094 numsoe = extent_list_num_els (soe->extents);
|
|
2095 if (numsoe)
|
|
2096 {
|
|
2097 if (flags & ME_MIGHT_MOVE_SOE)
|
|
2098 {
|
|
2099 int foundp;
|
|
2100 /* Can't map over SOE, so just extend range to cover the
|
|
2101 SOE. */
|
|
2102 EXTENT e = extent_list_at (soe->extents, 0, 0);
|
|
2103 range_start_pos =
|
|
2104 extent_list_locate (buffer_or_string_extent_list (obj), e, 0,
|
|
2105 &foundp);
|
|
2106 assert (foundp);
|
|
2107 range_start_type = 2;
|
|
2108 }
|
|
2109 else
|
|
2110 {
|
|
2111 /* We can map over the SOE. */
|
|
2112 do_soe_stage = 1;
|
|
2113 range_start_type = 1;
|
|
2114 }
|
|
2115 }
|
|
2116 else
|
|
2117 {
|
|
2118 /* No extents in the SOE to map over, so we act just as if
|
|
2119 ME_START_IN_REGION or ME_END_IN_REGION was specified.
|
|
2120 RANGE_ENDP already specified so no need to do anything else. */
|
|
2121 }
|
|
2122 }
|
|
2123 #endif
|
|
2124
|
|
2125 /* ---------- Now loop over the extents. ---------- */
|
|
2126
|
|
2127 /* We combine the code for the two stages because much of it
|
|
2128 overlaps. */
|
|
2129 for (stage = 0; stage < 2; stage++)
|
|
2130 {
|
|
2131 int pos = 0; /* Position in extent list */
|
|
2132
|
|
2133 /* First set up start conditions */
|
|
2134 if (stage == 0)
|
|
2135 { /* The SOE stage */
|
|
2136 if (!do_soe_stage)
|
|
2137 continue;
|
|
2138 el = buffer_or_string_stack_of_extents_force (obj)->extents;
|
|
2139 /* We will always be looping over start extents here. */
|
|
2140 assert (!range_endp);
|
|
2141 pos = 0;
|
|
2142 }
|
|
2143 else
|
|
2144 { /* The range stage */
|
|
2145 el = buffer_or_string_extent_list (obj);
|
|
2146 switch (range_start_type)
|
|
2147 {
|
|
2148 case 0:
|
|
2149 pos = extent_list_locate_from_pos (el, st, range_endp);
|
|
2150 break;
|
|
2151 case 1:
|
|
2152 pos = extent_list_locate_from_pos (el, st + 1, range_endp);
|
|
2153 break;
|
|
2154 case 2:
|
|
2155 pos = range_start_pos;
|
|
2156 break;
|
|
2157 }
|
|
2158 }
|
|
2159
|
|
2160 if (flags & ME_MIGHT_MODIFY_EXTENTS)
|
|
2161 {
|
|
2162 /* Create a marker to track changes to the extent list */
|
|
2163 if (posm)
|
|
2164 /* Delete the marker used in the SOE stage. */
|
|
2165 extent_list_delete_marker
|
|
2166 (buffer_or_string_stack_of_extents_force (obj)->extents, posm);
|
|
2167 posm = extent_list_make_marker (el, pos, range_endp);
|
|
2168 /* tell the unwind function about the marker. */
|
|
2169 closure.el = el;
|
|
2170 closure.mkr = posm;
|
|
2171 }
|
|
2172
|
|
2173 /* Now loop! */
|
|
2174 for (;;)
|
|
2175 {
|
|
2176 EXTENT e;
|
|
2177 Lisp_Object obj2;
|
|
2178
|
|
2179 /* ----- update position in extent list
|
|
2180 and fetch next extent ----- */
|
|
2181
|
|
2182 if (posm)
|
|
2183 /* fetch POS again to track extent insertions or deletions */
|
|
2184 pos = extent_list_marker_pos (el, posm);
|
|
2185 if (pos >= extent_list_num_els (el))
|
|
2186 break;
|
|
2187 e = extent_list_at (el, pos, range_endp);
|
|
2188 pos++;
|
|
2189 if (posm)
|
|
2190 /* now point the marker to the next one we're going to process.
|
|
2191 This ensures graceful behavior if this extent is deleted. */
|
|
2192 extent_list_move_marker (el, posm, pos);
|
|
2193
|
|
2194 /* ----- deal with internal extents ----- */
|
|
2195
|
|
2196 if (extent_internal_p (e))
|
|
2197 {
|
|
2198 if (!(flags & ME_INCLUDE_INTERNAL))
|
|
2199 continue;
|
|
2200 else if (e == range)
|
|
2201 {
|
|
2202 /* We're processing internal extents and we've
|
|
2203 come across our own special range extent.
|
|
2204 (This happens only in adjust_extents*() and
|
|
2205 process_extents*(), which handle text
|
|
2206 insertion and deletion.) We need to omit
|
|
2207 processing of this extent; otherwise
|
|
2208 we will probably end up prematurely
|
|
2209 terminating this loop. */
|
|
2210 continue;
|
|
2211 }
|
|
2212 }
|
|
2213
|
|
2214 /* ----- deal with AFTER condition ----- */
|
|
2215
|
|
2216 if (after)
|
|
2217 {
|
|
2218 /* if e > after, then we can stop skipping extents. */
|
|
2219 if (EXTENT_LESS (after, e))
|
|
2220 after = 0;
|
|
2221 else /* otherwise, skip this extent. */
|
|
2222 continue;
|
|
2223 }
|
|
2224
|
|
2225 /* ----- stop if we're completely outside the range ----- */
|
|
2226
|
|
2227 /* fetch ST and EN again to track text insertions or deletions */
|
|
2228 if (range)
|
|
2229 {
|
|
2230 st = extent_start (range);
|
|
2231 en = extent_end (range);
|
|
2232 }
|
|
2233 if (extent_endpoint (e, range_endp) > en)
|
|
2234 {
|
|
2235 /* Can't be mapping over SOE because all extents in
|
|
2236 there should overlap ST */
|
|
2237 assert (stage == 1);
|
|
2238 break;
|
|
2239 }
|
|
2240
|
|
2241 /* ----- Now actually call the function ----- */
|
|
2242
|
|
2243 obj2 = extent_object (e);
|
|
2244 if (extent_in_region_p (e,
|
|
2245 buffer_or_string_memind_to_bytind (obj2,
|
|
2246 st),
|
|
2247 buffer_or_string_memind_to_bytind (obj2,
|
|
2248 en),
|
|
2249 flags))
|
|
2250 {
|
|
2251 if ((*fn)(e, arg))
|
|
2252 {
|
|
2253 /* Function wants us to stop mapping. */
|
|
2254 stage = 1; /* so outer for loop will terminate */
|
|
2255 break;
|
|
2256 }
|
|
2257 }
|
|
2258 }
|
|
2259 }
|
|
2260 /* ---------- Finished looping. ---------- */
|
|
2261 }
|
|
2262
|
|
2263 if (flags & ME_MIGHT_THROW)
|
|
2264 /* This deletes the range extent and frees the marker. */
|
|
2265 unbind_to (count, Qnil);
|
|
2266 else
|
|
2267 {
|
|
2268 /* Delete them ourselves */
|
|
2269 if (range)
|
|
2270 extent_detach (range);
|
|
2271 if (posm)
|
|
2272 extent_list_delete_marker (el, posm);
|
|
2273 }
|
|
2274 }
|
|
2275
|
|
2276 void
|
|
2277 map_extents (Bufpos from, Bufpos to, int (*fn) (EXTENT extent, void *arg),
|
|
2278 void *arg, Lisp_Object obj, EXTENT after, unsigned int flags)
|
|
2279 {
|
|
2280 map_extents_bytind (buffer_or_string_bufpos_to_bytind (obj, from),
|
|
2281 buffer_or_string_bufpos_to_bytind (obj, to), fn, arg,
|
|
2282 obj, after, flags);
|
|
2283 }
|
|
2284
|
|
2285 /* ------------------------------- */
|
|
2286 /* adjust_extents() */
|
|
2287 /* ------------------------------- */
|
|
2288
|
|
2289 /* Add AMOUNT to all extent endpoints in the range (FROM, TO]. This
|
|
2290 happens whenever the gap is moved or (under Mule) a character in a
|
|
2291 string is substituted for a different-length one. The reason for
|
|
2292 this is that extent endpoints behave just like markers (all memory
|
|
2293 indices do) and this adjustment correct for markers -- see
|
|
2294 adjust_markers(). Note that it is important that we visit all
|
|
2295 extent endpoints in the range, irrespective of whether the
|
|
2296 endpoints are open or closed.
|
|
2297
|
|
2298 We could use map_extents() for this (and in fact the function
|
|
2299 was originally written that way), but the gap is in an incoherent
|
|
2300 state when this function is called and this function plays
|
|
2301 around with extent endpoints without detaching and reattaching
|
|
2302 the extents (this is provably correct and saves lots of time),
|
|
2303 so for safety we make it just look at the extent lists directly. */
|
|
2304
|
|
2305 void
|
|
2306 adjust_extents (Lisp_Object obj, Memind from, Memind to, int amount)
|
|
2307 {
|
|
2308 int endp;
|
|
2309 int pos;
|
|
2310 int startpos[2];
|
|
2311 Extent_List *el;
|
|
2312 Stack_Of_Extents *soe;
|
|
2313
|
|
2314 #ifdef ERROR_CHECK_EXTENTS
|
|
2315 sledgehammer_extent_check (obj);
|
|
2316 #endif
|
|
2317 el = buffer_or_string_extent_list (obj);
|
|
2318
|
14
|
2319 if (!el || !extent_list_num_els(el))
|
0
|
2320 return;
|
14
|
2321
|
0
|
2322 /* IMPORTANT! Compute the starting positions of the extents to
|
|
2323 modify BEFORE doing any modification! Otherwise the starting
|
|
2324 position for the second time through the loop might get
|
|
2325 incorrectly calculated (I got bit by this bug real bad). */
|
|
2326 startpos[0] = extent_list_locate_from_pos (el, from+1, 0);
|
|
2327 startpos[1] = extent_list_locate_from_pos (el, from+1, 1);
|
|
2328 for (endp = 0; endp < 2; endp++)
|
|
2329 {
|
|
2330 for (pos = startpos[endp]; pos < extent_list_num_els (el);
|
|
2331 pos++)
|
|
2332 {
|
|
2333 EXTENT e = extent_list_at (el, pos, endp);
|
|
2334 if (extent_endpoint (e, endp) > to)
|
|
2335 break;
|
|
2336 set_extent_endpoint (e,
|
|
2337 do_marker_adjustment (extent_endpoint (e, endp),
|
|
2338 from, to, amount),
|
|
2339 endp);
|
|
2340 }
|
|
2341 }
|
|
2342
|
|
2343 /* The index for the buffer's SOE is a memory index and thus
|
|
2344 needs to be adjusted like a marker. */
|
|
2345 soe = buffer_or_string_stack_of_extents (obj);
|
|
2346 if (soe && soe->pos >= 0)
|
|
2347 soe->pos = do_marker_adjustment (soe->pos, from, to, amount);
|
|
2348 }
|
|
2349
|
|
2350 /* ------------------------------- */
|
|
2351 /* adjust_extents_for_deletion() */
|
|
2352 /* ------------------------------- */
|
|
2353
|
|
2354 struct adjust_extents_for_deletion_arg
|
|
2355 {
|
|
2356 extent_dynarr *list;
|
|
2357 };
|
|
2358
|
|
2359 static int
|
|
2360 adjust_extents_for_deletion_mapper (EXTENT extent, void *arg)
|
|
2361 {
|
|
2362 struct adjust_extents_for_deletion_arg *closure =
|
|
2363 (struct adjust_extents_for_deletion_arg *) arg;
|
|
2364
|
|
2365 Dynarr_add (closure->list, extent);
|
|
2366 return 0; /* continue mapping */
|
|
2367 }
|
|
2368
|
|
2369 /* For all extent endpoints in the range (FROM, TO], move them to the beginning
|
|
2370 of the new gap. Note that it is important that we visit all extent
|
|
2371 endpoints in the range, irrespective of whether the endpoints are open or
|
|
2372 closed.
|
|
2373
|
|
2374 This function deals with weird stuff such as the fact that extents
|
|
2375 may get reordered.
|
|
2376
|
|
2377 There is no string correspondent for this because you can't
|
|
2378 delete characters from a string.
|
|
2379 */
|
|
2380
|
|
2381 void
|
|
2382 adjust_extents_for_deletion (Lisp_Object object, Bytind from,
|
|
2383 Bytind to, int gapsize, int numdel)
|
|
2384 {
|
|
2385 struct adjust_extents_for_deletion_arg closure;
|
|
2386 int i;
|
|
2387 Memind oldsoe, newsoe;
|
|
2388 Stack_Of_Extents *soe = buffer_or_string_stack_of_extents (object);
|
|
2389
|
|
2390 #ifdef ERROR_CHECK_EXTENTS
|
|
2391 sledgehammer_extent_check (object);
|
|
2392 #endif
|
|
2393 closure.list = (extent_dynarr *) Dynarr_new (EXTENT);
|
|
2394
|
|
2395 /* We're going to be playing weird games below with extents and the SOE
|
|
2396 and such, so compute the list now of all the extents that we're going
|
|
2397 to muck with. If we do the mapping and adjusting together, things can
|
|
2398 get all screwed up. */
|
|
2399
|
|
2400 map_extents_bytind (from, to, adjust_extents_for_deletion_mapper,
|
|
2401 (void *) &closure, object, 0,
|
|
2402 /* extent endpoints move like markers regardless
|
|
2403 of their open/closeness. */
|
|
2404 ME_ALL_EXTENTS_CLOSED | ME_END_CLOSED |
|
|
2405 ME_START_OR_END_IN_REGION | ME_INCLUDE_INTERNAL);
|
|
2406
|
|
2407 /*
|
|
2408 Old and new values for the SOE's position. (It gets adjusted
|
|
2409 like a marker, just like extent endpoints.)
|
|
2410 */
|
|
2411
|
|
2412 if (soe)
|
|
2413 {
|
|
2414 oldsoe = soe->pos;
|
|
2415 if (soe->pos >= 0)
|
|
2416 newsoe = do_marker_adjustment (soe->pos,
|
|
2417 (Memind) (to + gapsize),
|
|
2418 (Memind) (to + gapsize),
|
|
2419 - numdel - gapsize);
|
|
2420 else
|
|
2421 newsoe = soe->pos;
|
|
2422 }
|
|
2423
|
|
2424 for (i = 0; i < Dynarr_length (closure.list); i++)
|
|
2425 {
|
|
2426 EXTENT extent = Dynarr_at (closure.list, i);
|
|
2427 Memind new_start, new_end;
|
|
2428
|
|
2429 /* do_marker_adjustment() will not adjust values that should not be
|
|
2430 adjusted. We're passing the same funky arguments to
|
|
2431 do_marker_adjustment() as buffer_delete_range() does. */
|
|
2432 new_start =
|
|
2433 do_marker_adjustment (extent_start (extent),
|
|
2434 (Memind) (to + gapsize),
|
|
2435 (Memind) (to + gapsize),
|
|
2436 - numdel - gapsize);
|
|
2437 new_end =
|
|
2438 do_marker_adjustment (extent_end (extent),
|
|
2439 (Memind) (to + gapsize),
|
|
2440 (Memind) (to + gapsize),
|
|
2441 - numdel - gapsize);
|
|
2442
|
|
2443 /* We need to be very careful here so that the SOE doesn't get
|
|
2444 corrupted. We are shrinking extents out of the deleted region
|
|
2445 and simultaneously moving the SOE's pos out of the deleted
|
|
2446 region, so the SOE should contain the same extents at the end
|
|
2447 as at the beginning. However, extents may get reordered
|
|
2448 by this process, so we have to operate by pulling the extents
|
|
2449 out of the buffer and SOE, changing their bounds, and then
|
|
2450 reinserting them. In order for the SOE not to get screwed up,
|
|
2451 we have to make sure that the SOE's pos points to its old
|
|
2452 location whenever we pull an extent out, and points to its
|
|
2453 new location whenever we put the extent back in.
|
|
2454 */
|
|
2455
|
|
2456 if (new_start != extent_start (extent) ||
|
|
2457 new_end != extent_end (extent))
|
|
2458 {
|
|
2459 extent_detach (extent);
|
|
2460 set_extent_start (extent, new_start);
|
|
2461 set_extent_end (extent, new_end);
|
|
2462 if (soe)
|
|
2463 soe->pos = newsoe;
|
|
2464 extent_attach (extent);
|
|
2465 if (soe)
|
|
2466 soe->pos = oldsoe;
|
|
2467 }
|
|
2468 }
|
|
2469
|
|
2470 if (soe)
|
|
2471 soe->pos = newsoe;
|
|
2472
|
|
2473 #ifdef ERROR_CHECK_EXTENTS
|
|
2474 sledgehammer_extent_check (object);
|
|
2475 #endif
|
|
2476 Dynarr_free (closure.list);
|
|
2477 }
|
|
2478
|
|
2479 /* ------------------------------- */
|
|
2480 /* extent fragments */
|
|
2481 /* ------------------------------- */
|
|
2482
|
|
2483 /* Imagine that the buffer is divided up into contiguous,
|
|
2484 nonoverlapping "runs" of text such that no extent
|
|
2485 starts or ends within a run (extents that abut the
|
|
2486 run don't count).
|
|
2487
|
|
2488 An extent fragment is a structure that holds data about
|
|
2489 the run that contains a particular buffer position (if
|
|
2490 the buffer position is at the junction of two runs, the
|
|
2491 run after the position is used) -- the beginning and
|
|
2492 end of the run, a list of all of the extents in that
|
|
2493 run, the "merged face" that results from merging all of
|
|
2494 the faces corresponding to those extents, the begin and
|
|
2495 end glyphs at the beginning of the run, etc. This is
|
|
2496 the information that redisplay needs in order to
|
|
2497 display this run.
|
|
2498
|
|
2499 Extent fragments have to be very quick to update to
|
|
2500 a new buffer position when moving linearly through
|
|
2501 the buffer. They rely on the stack-of-extents code,
|
|
2502 which does the heavy-duty algorithmic work of determining
|
|
2503 which extents overly a particular position. */
|
|
2504
|
|
2505 /* This function returns the position of the beginning of
|
|
2506 the first run that begins after POS, or returns POS if
|
|
2507 there are no such runs. */
|
|
2508
|
|
2509 static Bytind
|
|
2510 extent_find_end_of_run (Lisp_Object obj, Bytind pos, int outside_accessible)
|
|
2511 {
|
|
2512 Extent_List *sel;
|
|
2513 Extent_List *bel = buffer_or_string_extent_list (obj);
|
|
2514 Bytind pos1, pos2;
|
|
2515 int elind1, elind2;
|
|
2516 Memind mempos = buffer_or_string_bytind_to_memind (obj, pos);
|
|
2517 Bytind limit = outside_accessible ?
|
|
2518 buffer_or_string_absolute_end_byte (obj) :
|
|
2519 buffer_or_string_accessible_end_byte (obj);
|
|
2520
|
14
|
2521 if (!bel || !extent_list_num_els(bel))
|
0
|
2522 return limit;
|
|
2523
|
|
2524 sel = buffer_or_string_stack_of_extents_force (obj)->extents;
|
|
2525 soe_move (obj, mempos);
|
|
2526
|
|
2527 /* Find the first start position after POS. */
|
|
2528 elind1 = extent_list_locate_from_pos (bel, mempos+1, 0);
|
|
2529 if (elind1 < extent_list_num_els (bel))
|
|
2530 pos1 = buffer_or_string_memind_to_bytind
|
|
2531 (obj, extent_start (extent_list_at (bel, elind1, 0)));
|
|
2532 else
|
|
2533 pos1 = limit;
|
|
2534
|
|
2535 /* Find the first end position after POS. The extent corresponding
|
|
2536 to this position is either in the SOE or is greater than or
|
|
2537 equal to POS1, so we just have to look in the SOE. */
|
|
2538 elind2 = extent_list_locate_from_pos (sel, mempos+1, 1);
|
|
2539 if (elind2 < extent_list_num_els (sel))
|
|
2540 pos2 = buffer_or_string_memind_to_bytind
|
|
2541 (obj, extent_end (extent_list_at (sel, elind2, 1)));
|
|
2542 else
|
|
2543 pos2 = limit;
|
|
2544
|
|
2545 return min (min (pos1, pos2), limit);
|
|
2546 }
|
|
2547
|
|
2548 static Bytind
|
|
2549 extent_find_beginning_of_run (Lisp_Object obj, Bytind pos,
|
|
2550 int outside_accessible)
|
|
2551 {
|
|
2552 Extent_List *sel;
|
|
2553 Extent_List *bel = buffer_or_string_extent_list (obj);
|
|
2554 Bytind pos1, pos2;
|
|
2555 int elind1, elind2;
|
|
2556 Memind mempos = buffer_or_string_bytind_to_memind (obj, pos);
|
|
2557 Bytind limit = outside_accessible ?
|
|
2558 buffer_or_string_absolute_begin_byte (obj) :
|
|
2559 buffer_or_string_accessible_begin_byte (obj);
|
|
2560
|
14
|
2561 if (!bel || !extent_list_num_els(bel))
|
0
|
2562 return limit;
|
|
2563
|
|
2564 sel = buffer_or_string_stack_of_extents_force (obj)->extents;
|
|
2565 soe_move (obj, mempos);
|
|
2566
|
|
2567 /* Find the first end position before POS. */
|
|
2568 elind1 = extent_list_locate_from_pos (bel, mempos, 1);
|
|
2569 if (elind1 > 0)
|
|
2570 pos1 = buffer_or_string_memind_to_bytind
|
|
2571 (obj, extent_end (extent_list_at (bel, elind1 - 1, 1)));
|
|
2572 else
|
|
2573 pos1 = limit;
|
|
2574
|
|
2575 /* Find the first start position before POS. The extent corresponding
|
|
2576 to this position is either in the SOE or is less than or
|
|
2577 equal to POS1, so we just have to look in the SOE. */
|
|
2578 elind2 = extent_list_locate_from_pos (sel, mempos, 0);
|
|
2579 if (elind2 > 0)
|
|
2580 pos2 = buffer_or_string_memind_to_bytind
|
|
2581 (obj, extent_start (extent_list_at (sel, elind2 - 1, 0)));
|
|
2582 else
|
|
2583 pos2 = limit;
|
|
2584
|
|
2585 return max (max (pos1, pos2), limit);
|
|
2586 }
|
|
2587
|
|
2588 struct extent_fragment *
|
|
2589 extent_fragment_new (Lisp_Object buffer_or_string, struct frame *frm)
|
|
2590 {
|
|
2591 struct extent_fragment *ef = (struct extent_fragment *)
|
|
2592 xmalloc (sizeof (struct extent_fragment));
|
|
2593
|
|
2594 memset (ef, 0, sizeof (*ef));
|
|
2595 ef->object = buffer_or_string;
|
|
2596 ef->frm = frm;
|
|
2597 ef->extents = Dynarr_new (EXTENT);
|
|
2598 ef->begin_glyphs = Dynarr_new (struct glyph_block);
|
|
2599 ef->end_glyphs = Dynarr_new (struct glyph_block);
|
|
2600
|
|
2601 return ef;
|
|
2602 }
|
|
2603
|
|
2604 void
|
|
2605 extent_fragment_delete (struct extent_fragment *ef)
|
|
2606 {
|
|
2607 Dynarr_free (ef->extents);
|
|
2608 Dynarr_free (ef->begin_glyphs);
|
|
2609 Dynarr_free (ef->end_glyphs);
|
|
2610 xfree (ef);
|
|
2611 }
|
|
2612
|
|
2613 static int
|
|
2614 extent_priority_sort_function (const void *humpty, const void *dumpty)
|
|
2615 {
|
|
2616 CONST EXTENT foo = * (CONST EXTENT *) humpty;
|
|
2617 CONST EXTENT bar = * (CONST EXTENT *) dumpty;
|
|
2618 if (extent_priority (foo) < extent_priority (bar))
|
|
2619 return -1;
|
|
2620 return (extent_priority (foo) > extent_priority (bar));
|
|
2621 }
|
|
2622
|
|
2623 static void
|
|
2624 extent_fragment_sort_by_priority (extent_dynarr *extarr)
|
|
2625 {
|
|
2626 int i;
|
|
2627
|
|
2628 /* Sort our copy of the stack by extent_priority. We use a bubble
|
|
2629 sort here because it's going to be faster than qsort() for small
|
|
2630 numbers of extents (less than 10 or so), and 99.999% of the time
|
|
2631 there won't ever be more extents than this in the stack. */
|
|
2632 if (Dynarr_length (extarr) < 10)
|
|
2633 {
|
|
2634 for (i = 1; i < Dynarr_length (extarr); i++)
|
|
2635 {
|
|
2636 int j = i - 1;
|
|
2637 while (j >= 0 &&
|
|
2638 (extent_priority (Dynarr_at (extarr, j)) >
|
|
2639 extent_priority (Dynarr_at (extarr, j+1))))
|
|
2640 {
|
|
2641 EXTENT tmp = Dynarr_at (extarr, j);
|
|
2642 Dynarr_at (extarr, j) = Dynarr_at (extarr, j+1);
|
|
2643 Dynarr_at (extarr, j+1) = tmp;
|
|
2644 j--;
|
|
2645 }
|
|
2646 }
|
|
2647 }
|
|
2648 else
|
|
2649 /* But some loser programs mess up and may create a large number
|
|
2650 of extents overlapping the same spot. This will result in
|
|
2651 catastrophic behavior if we use the bubble sort above. */
|
|
2652 qsort (Dynarr_atp (extarr, 0), Dynarr_length (extarr),
|
|
2653 sizeof (EXTENT), extent_priority_sort_function);
|
|
2654 }
|
|
2655
|
|
2656 /* If PROP is the `invisible' property of an extent,
|
|
2657 this is 1 if the extent should be treated as invisible. */
|
|
2658
|
|
2659 #define EXTENT_PROP_MEANS_INVISIBLE(buf, prop) \
|
|
2660 (EQ (buf->invisibility_spec, Qt) \
|
|
2661 ? ! NILP (prop) \
|
|
2662 : invisible_p (prop, buf->invisibility_spec))
|
|
2663
|
|
2664 /* If PROP is the `invisible' property of a extent,
|
|
2665 this is 1 if the extent should be treated as invisible
|
|
2666 and should have an ellipsis. */
|
|
2667
|
|
2668 #define EXTENT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS(buf, prop) \
|
|
2669 (EQ (buf->invisibility_spec, Qt) \
|
|
2670 ? 0 \
|
|
2671 : invisible_ellipsis_p (prop, buf->invisibility_spec))
|
|
2672
|
|
2673 /* This is like a combination of memq and assq.
|
|
2674 Return 1 if PROPVAL appears as an element of LIST
|
|
2675 or as the car of an element of LIST.
|
|
2676 If PROPVAL is a list, compare each element against LIST
|
|
2677 in that way, and return 1 if any element of PROPVAL is found in LIST.
|
|
2678 Otherwise return 0.
|
|
2679 This function cannot quit. */
|
|
2680
|
|
2681 static int
|
|
2682 invisible_p (REGISTER Lisp_Object propval, Lisp_Object list)
|
|
2683 {
|
|
2684 REGISTER Lisp_Object tail, proptail;
|
|
2685 for (tail = list; CONSP (tail); tail = XCDR (tail))
|
|
2686 {
|
|
2687 REGISTER Lisp_Object tem;
|
|
2688 tem = XCAR (tail);
|
|
2689 if (EQ (propval, tem))
|
|
2690 return 1;
|
|
2691 if (CONSP (tem) && EQ (propval, XCAR (tem)))
|
|
2692 return 1;
|
|
2693 }
|
|
2694 if (CONSP (propval))
|
|
2695 for (proptail = propval; CONSP (proptail);
|
|
2696 proptail = XCDR (proptail))
|
|
2697 {
|
|
2698 Lisp_Object propelt;
|
|
2699 propelt = XCAR (proptail);
|
|
2700 for (tail = list; CONSP (tail); tail = XCDR (tail))
|
|
2701 {
|
|
2702 REGISTER Lisp_Object tem;
|
|
2703 tem = XCAR (tail);
|
|
2704 if (EQ (propelt, tem))
|
|
2705 return 1;
|
|
2706 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
|
|
2707 return 1;
|
|
2708 }
|
|
2709 }
|
|
2710 return 0;
|
|
2711 }
|
|
2712
|
|
2713 /* Return 1 if PROPVAL appears as the car of an element of LIST
|
|
2714 and the cdr of that element is non-nil.
|
|
2715 If PROPVAL is a list, check each element of PROPVAL in that way,
|
|
2716 and the first time some element is found,
|
|
2717 return 1 if the cdr of that element is non-nil.
|
|
2718 Otherwise return 0.
|
|
2719 This function cannot quit. */
|
|
2720
|
|
2721 static int
|
|
2722 invisible_ellipsis_p (REGISTER Lisp_Object propval, Lisp_Object list)
|
|
2723 {
|
|
2724 REGISTER Lisp_Object tail, proptail;
|
|
2725 for (tail = list; CONSP (tail); tail = XCDR (tail))
|
|
2726 {
|
|
2727 REGISTER Lisp_Object tem;
|
|
2728 tem = XCAR (tail);
|
|
2729 if (CONSP (tem) && EQ (propval, XCAR (tem)))
|
|
2730 return ! NILP (XCDR (tem));
|
|
2731 }
|
|
2732 if (CONSP (propval))
|
|
2733 for (proptail = propval; CONSP (proptail);
|
|
2734 proptail = XCDR (proptail))
|
|
2735 {
|
|
2736 Lisp_Object propelt;
|
|
2737 propelt = XCAR (proptail);
|
|
2738 for (tail = list; CONSP (tail); tail = XCDR (tail))
|
|
2739 {
|
|
2740 REGISTER Lisp_Object tem;
|
|
2741 tem = XCAR (tail);
|
|
2742 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
|
|
2743 return ! NILP (XCDR (tem));
|
|
2744 }
|
|
2745 }
|
|
2746 return 0;
|
|
2747 }
|
|
2748
|
|
2749 face_index
|
|
2750 extent_fragment_update (struct window *w, struct extent_fragment *ef,
|
|
2751 Bytind pos)
|
|
2752 {
|
|
2753 int i;
|
|
2754 Extent_List *sel =
|
|
2755 buffer_or_string_stack_of_extents_force (ef->object)->extents;
|
|
2756 EXTENT lhe = 0;
|
|
2757 struct extent dummy_lhe_extent;
|
|
2758 Memind mempos = buffer_or_string_bytind_to_memind (ef->object, pos);
|
|
2759
|
|
2760 #ifdef ERROR_CHECK_EXTENTS
|
|
2761 assert (pos >= buffer_or_string_accessible_begin_byte (ef->object)
|
|
2762 && pos <= buffer_or_string_accessible_end_byte (ef->object));
|
|
2763 #endif
|
|
2764
|
|
2765 Dynarr_reset (ef->extents);
|
|
2766 Dynarr_reset (ef->begin_glyphs);
|
|
2767 Dynarr_reset (ef->end_glyphs);
|
|
2768
|
|
2769 ef->previously_invisible = ef->invisible;
|
|
2770 if (ef->invisible)
|
|
2771 {
|
|
2772 if (ef->invisible_ellipses)
|
|
2773 ef->invisible_ellipses_already_displayed = 1;
|
|
2774 }
|
|
2775 else
|
|
2776 ef->invisible_ellipses_already_displayed = 0;
|
|
2777 ef->invisible = 0;
|
|
2778 ef->invisible_ellipses = 0;
|
|
2779
|
|
2780 /* Set up the begin and end positions. */
|
|
2781 ef->pos = pos;
|
|
2782 ef->end = extent_find_end_of_run (ef->object, pos, 0);
|
|
2783
|
|
2784 /* Note that extent_find_end_of_run() already moved the SOE for us. */
|
|
2785 /* soe_move (ef->object, mempos); */
|
|
2786
|
|
2787 /* Determine the begin glyphs at POS. */
|
|
2788 for (i = 0; i < extent_list_num_els (sel); i++)
|
|
2789 {
|
|
2790 EXTENT e = extent_list_at (sel, i, 0);
|
|
2791 if (extent_start (e) == mempos && !NILP (extent_begin_glyph (e)))
|
|
2792 {
|
|
2793 Lisp_Object glyph = extent_begin_glyph (e);
|
|
2794 struct glyph_block gb;
|
|
2795
|
|
2796 gb.glyph = glyph;
|
|
2797 gb.extent = Qnil;
|
|
2798 XSETEXTENT (gb.extent, e);
|
|
2799 Dynarr_add (ef->begin_glyphs, gb);
|
|
2800 }
|
|
2801 }
|
|
2802
|
|
2803 /* Determine the end glyphs at POS. */
|
|
2804 for (i = 0; i < extent_list_num_els (sel); i++)
|
|
2805 {
|
|
2806 EXTENT e = extent_list_at (sel, i, 1);
|
|
2807 if (extent_end (e) == mempos && !NILP (extent_end_glyph (e)))
|
|
2808 {
|
|
2809 Lisp_Object glyph = extent_end_glyph (e);
|
|
2810 struct glyph_block gb;
|
|
2811
|
|
2812 gb.glyph = glyph;
|
|
2813 gb.extent = Qnil;
|
|
2814 XSETEXTENT (gb.extent, e);
|
|
2815 Dynarr_add (ef->end_glyphs, gb);
|
|
2816 }
|
|
2817 }
|
|
2818
|
|
2819 /* We tried determining all the charsets used in the run here,
|
|
2820 but that fails even if we only do the current line -- display
|
|
2821 tables or non-printable characters might cause other charsets
|
|
2822 to be used. */
|
|
2823
|
|
2824 /* Determine whether the last-highlighted-extent is present. */
|
|
2825 if (EXTENTP (Vlast_highlighted_extent))
|
|
2826 lhe = XEXTENT (Vlast_highlighted_extent);
|
|
2827
|
|
2828 /* Now add all extents that overlap the character after POS and
|
|
2829 have a non-nil face. Also check if the character is invisible. */
|
|
2830 for (i = 0; i < extent_list_num_els (sel); i++)
|
|
2831 {
|
|
2832 EXTENT e = extent_list_at (sel, i, 0);
|
|
2833 if (extent_end (e) > mempos)
|
|
2834 {
|
|
2835 Lisp_Object invis_prop = extent_invisible (e);
|
|
2836
|
|
2837 if (!NILP (invis_prop))
|
|
2838 {
|
|
2839 if (!BUFFERP (ef->object))
|
|
2840 /* #### no `string-invisibility-spec' */
|
|
2841 ef->invisible = 1;
|
|
2842 else
|
|
2843 {
|
|
2844 if (!ef->invisible_ellipses_already_displayed &&
|
|
2845 EXTENT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS
|
|
2846 (XBUFFER (ef->object), invis_prop))
|
|
2847 {
|
|
2848 ef->invisible = 1;
|
|
2849 ef->invisible_ellipses = 1;
|
|
2850 }
|
|
2851 else if (EXTENT_PROP_MEANS_INVISIBLE
|
|
2852 (XBUFFER (ef->object), invis_prop))
|
|
2853 ef->invisible = 1;
|
|
2854 }
|
|
2855 }
|
|
2856
|
|
2857 /* Remember that one of the extents in the list might be our
|
|
2858 dummy extent representing the highlighting that is
|
|
2859 attached to some other extent that is currently
|
|
2860 mouse-highlighted. When an extent is mouse-highlighted,
|
|
2861 it is as if there are two extents there, of potentially
|
|
2862 different priorities: the extent being highlighted, with
|
|
2863 whatever face and priority it has; and an ephemeral
|
|
2864 extent in the `mouse-face' face with
|
|
2865 `mouse-highlight-priority'.
|
|
2866 */
|
|
2867
|
|
2868 if (!NILP (extent_face (e)))
|
|
2869 Dynarr_add (ef->extents, e);
|
|
2870 if (e == lhe)
|
|
2871 {
|
|
2872 /* memset isn't really necessary; we only deref `priority'
|
|
2873 and `face' */
|
|
2874 memset (&dummy_lhe_extent, 0, sizeof (dummy_lhe_extent));
|
|
2875 set_extent_priority (&dummy_lhe_extent,
|
|
2876 mouse_highlight_priority);
|
|
2877 extent_face (&dummy_lhe_extent) = extent_mouse_face (lhe);
|
|
2878 Dynarr_add (ef->extents, &dummy_lhe_extent);
|
|
2879 }
|
|
2880 }
|
|
2881 }
|
|
2882
|
|
2883 extent_fragment_sort_by_priority (ef->extents);
|
|
2884
|
|
2885 /* Now merge the faces together into a single face. The code to
|
|
2886 do this is in faces.c because it involves manipulating faces. */
|
|
2887 return get_extent_fragment_face_cache_index (w, ef);
|
|
2888 }
|
|
2889
|
|
2890
|
|
2891 /************************************************************************/
|
|
2892 /* extent-object methods */
|
|
2893 /************************************************************************/
|
|
2894
|
|
2895 /* These are the basic helper functions for handling the allocation of
|
|
2896 extent objects. They are similar to the functions for other
|
|
2897 lrecord objects. allocate_extent() is in alloc.c, not here. */
|
|
2898
|
|
2899 static Lisp_Object mark_extent (Lisp_Object, void (*) (Lisp_Object));
|
|
2900 static int extent_equal (Lisp_Object, Lisp_Object, int depth);
|
|
2901 static unsigned long extent_hash (Lisp_Object obj, int depth);
|
|
2902 static void print_extent (Lisp_Object obj, Lisp_Object printcharfun,
|
|
2903 int escapeflag);
|
|
2904 static Lisp_Object extent_getprop (Lisp_Object obj, Lisp_Object prop);
|
|
2905 static int extent_putprop (Lisp_Object obj, Lisp_Object prop,
|
|
2906 Lisp_Object value);
|
|
2907 static int extent_remprop (Lisp_Object obj, Lisp_Object prop);
|
|
2908 static Lisp_Object extent_plist (Lisp_Object obj);
|
|
2909
|
|
2910 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS ("extent", extent,
|
|
2911 mark_extent,
|
|
2912 print_extent,
|
|
2913 /* NOTE: If you declare a
|
|
2914 finalization method here,
|
|
2915 it will NOT be called.
|
|
2916 Shaft city. */
|
|
2917 0,
|
|
2918 extent_equal, extent_hash,
|
|
2919 extent_getprop, extent_putprop,
|
|
2920 extent_remprop, extent_plist,
|
|
2921 struct extent);
|
|
2922
|
|
2923 static Lisp_Object
|
|
2924 mark_extent (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
2925 {
|
|
2926 struct extent *extent = XEXTENT (obj);
|
|
2927
|
|
2928 ((markobj) (extent_object (extent)));
|
|
2929 ((markobj) (extent_no_chase_normal_field (extent, face)));
|
|
2930 return (extent->plist);
|
|
2931 }
|
|
2932
|
|
2933 static char *
|
|
2934 print_extent_1 (char *buf, Lisp_Object extent_obj)
|
|
2935 {
|
|
2936 EXTENT ext = XEXTENT (extent_obj);
|
|
2937 EXTENT anc = extent_ancestor (ext);
|
|
2938 char *bp = buf;
|
|
2939 Lisp_Object tail;
|
|
2940
|
|
2941 /* Retrieve the ancestor and use it, for faster retrieval of properties */
|
|
2942
|
|
2943 if (!NILP (extent_begin_glyph (anc))) *bp++ = '*';
|
|
2944 *bp++ = (extent_start_open_p (anc) ? '(': '[');
|
|
2945 if (extent_detached_p (ext))
|
|
2946 sprintf (bp, "detached");
|
|
2947 else
|
|
2948 {
|
|
2949 Bufpos from = XINT (Fextent_start_position (extent_obj));
|
|
2950 Bufpos to = XINT (Fextent_end_position (extent_obj));
|
|
2951 sprintf (bp, "%d, %d", from, to);
|
|
2952 }
|
|
2953 bp += strlen (bp);
|
|
2954 *bp++ = (extent_end_open_p (anc) ? ')': ']');
|
|
2955 if (!NILP (extent_end_glyph (anc))) *bp++ = '*';
|
|
2956 *bp++ = ' ';
|
|
2957
|
|
2958 if (!NILP (extent_read_only (anc))) *bp++ = '%';
|
|
2959 if (!NILP (extent_mouse_face (anc))) *bp++ = 'H';
|
|
2960 if (extent_unique_p (anc)) *bp++ = 'U';
|
|
2961 else if (extent_duplicable_p (anc)) *bp++ = 'D';
|
|
2962 if (!NILP (extent_invisible (anc))) *bp++ = 'I';
|
|
2963
|
|
2964 if (!NILP (extent_read_only (anc)) || !NILP (extent_mouse_face (anc)) ||
|
20
|
2965 extent_unique_p (anc) ||
|
0
|
2966 extent_duplicable_p (anc) || !NILP (extent_invisible (anc)))
|
|
2967 *bp++ = ' ';
|
|
2968
|
|
2969 tail = extent_plist_slot (anc);
|
|
2970
|
|
2971 for (; !NILP (tail); tail = Fcdr (Fcdr (tail)))
|
|
2972 {
|
|
2973 struct Lisp_String *k = XSYMBOL (XCAR (tail))->name;
|
|
2974 Lisp_Object v = XCAR (XCDR (tail));
|
|
2975 if (NILP (v)) continue;
|
|
2976 memcpy (bp, (char *) string_data (k), string_length (k));
|
|
2977 bp += string_length (k);
|
|
2978 *bp++ = ' ';
|
|
2979 }
|
|
2980
|
|
2981 sprintf (bp, "0x%lx", (long) ext);
|
|
2982 bp += strlen (bp);
|
|
2983
|
|
2984 *bp++ = 0;
|
|
2985 return buf;
|
|
2986 }
|
|
2987
|
|
2988 static void
|
|
2989 print_extent (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
2990 {
|
|
2991 char buf2[256];
|
|
2992
|
|
2993 if (escapeflag)
|
|
2994 {
|
|
2995 CONST char *title = "";
|
|
2996 CONST char *name = "";
|
|
2997 CONST char *posttitle = "";
|
|
2998 Lisp_Object obj2 = Qnil;
|
|
2999
|
|
3000 /* Destroyed extents have 't' in the object field, causing
|
|
3001 extent_object() to abort (maybe). */
|
|
3002 if (EXTENT_LIVE_P (XEXTENT (obj)))
|
|
3003 obj2 = extent_object (XEXTENT (obj));
|
|
3004
|
|
3005 if (NILP (obj2))
|
|
3006 title = "no buffer";
|
|
3007 else if (BUFFERP (obj2))
|
|
3008 {
|
|
3009 if (BUFFER_LIVE_P (XBUFFER (obj2)))
|
|
3010 {
|
|
3011 title = "buffer ";
|
16
|
3012 name = (char *) XSTRING_DATA (XBUFFER (obj2)->name);
|
0
|
3013 }
|
|
3014 else
|
|
3015 {
|
|
3016 title = "Killed Buffer";
|
|
3017 name = "";
|
|
3018 }
|
|
3019 }
|
|
3020 else
|
|
3021 {
|
|
3022 assert (STRINGP (obj2));
|
|
3023 title = "string \"";
|
|
3024 posttitle = "\"";
|
16
|
3025 name = (char *) XSTRING_DATA (obj2);
|
0
|
3026 }
|
|
3027
|
|
3028 if (print_readably)
|
|
3029 {
|
|
3030 if (!EXTENT_LIVE_P (XEXTENT (obj)))
|
|
3031 error ("printing unreadable object #<destroyed extent>");
|
|
3032 else
|
|
3033 error ("printing unreadable object #<extent %s>",
|
|
3034 print_extent_1 (buf2, obj));
|
|
3035 }
|
|
3036
|
|
3037 if (!EXTENT_LIVE_P (XEXTENT (obj)))
|
|
3038 write_c_string ("#<destroyed extent", printcharfun);
|
|
3039 else
|
|
3040 {
|
|
3041 char buf[256];
|
|
3042 write_c_string ("#<extent ", printcharfun);
|
|
3043 if (extent_detached_p (XEXTENT (obj)))
|
|
3044 sprintf (buf, "%s from %s%s%s",
|
|
3045 print_extent_1 (buf2, obj), title, name, posttitle);
|
|
3046 else
|
|
3047 sprintf (buf, "%s in %s%s%s",
|
|
3048 print_extent_1 (buf2, obj),
|
|
3049 title, name, posttitle);
|
|
3050 write_c_string (buf, printcharfun);
|
|
3051 }
|
|
3052 }
|
|
3053 else
|
|
3054 {
|
|
3055 if (print_readably)
|
|
3056 error ("printing unreadable object #<extent>");
|
|
3057 write_c_string ("#<extent", printcharfun);
|
|
3058 }
|
|
3059 write_c_string (">", printcharfun);
|
|
3060 }
|
|
3061
|
|
3062 static int
|
|
3063 properties_equal (EXTENT e1, EXTENT e2, int depth)
|
|
3064 {
|
|
3065 /* When this function is called, all indirections have been followed.
|
|
3066 Thus, the indirection checks in the various macros below will not
|
|
3067 amount to anything, and could be removed. However, the time
|
|
3068 savings would probably not be significant. */
|
|
3069 if (!(EQ (extent_face (e1), extent_face (e2)) &&
|
|
3070 extent_priority (e1) == extent_priority (e2) &&
|
|
3071 internal_equal (extent_begin_glyph (e1), extent_begin_glyph (e2),
|
|
3072 depth + 1) &&
|
|
3073 internal_equal (extent_end_glyph (e1), extent_end_glyph (e2),
|
|
3074 depth + 1)))
|
|
3075 return 0;
|
|
3076
|
|
3077 /* compare the bit flags. */
|
|
3078 {
|
|
3079 /* The has_aux field should not be relevant. */
|
|
3080 int e1_has_aux = e1->flags.has_aux;
|
|
3081 int e2_has_aux = e2->flags.has_aux;
|
|
3082 int value;
|
|
3083
|
|
3084 e1->flags.has_aux = e2->flags.has_aux = 0;
|
|
3085 value = memcmp (&e1->flags, &e2->flags, sizeof (e1->flags));
|
|
3086 e1->flags.has_aux = e1_has_aux;
|
|
3087 e2->flags.has_aux = e2_has_aux;
|
|
3088 if (value)
|
|
3089 return 0;
|
|
3090 }
|
|
3091
|
|
3092 /* compare the random elements of the plists. */
|
|
3093 return (!plists_differ (extent_no_chase_plist (e1),
|
|
3094 extent_no_chase_plist (e2),
|
|
3095 0, 0, depth + 1));
|
|
3096 }
|
|
3097
|
|
3098 static int
|
|
3099 extent_equal (Lisp_Object o1, Lisp_Object o2, int depth)
|
|
3100 {
|
|
3101 struct extent *e1 = XEXTENT (o1);
|
|
3102 struct extent *e2 = XEXTENT (o2);
|
|
3103 return
|
|
3104 (extent_start (e1) == extent_start (e2) &&
|
|
3105 extent_end (e1) == extent_end (e2) &&
|
|
3106 internal_equal (extent_object (e1), extent_object (e2), depth + 1) &&
|
|
3107 properties_equal (extent_ancestor (e1), extent_ancestor (e2),
|
|
3108 depth));
|
|
3109 }
|
|
3110
|
|
3111 static unsigned long
|
|
3112 extent_hash (Lisp_Object obj, int depth)
|
|
3113 {
|
|
3114 struct extent *e = XEXTENT (obj);
|
|
3115 /* No need to hash all of the elements; that would take too long.
|
|
3116 Just hash the most common ones. */
|
|
3117 return HASH3 (extent_start (e), extent_end (e),
|
|
3118 internal_hash (extent_object (e), depth + 1));
|
|
3119 }
|
|
3120
|
|
3121 static Lisp_Object
|
|
3122 extent_getprop (Lisp_Object obj, Lisp_Object prop)
|
|
3123 {
|
|
3124 return Fextent_property (obj, prop, Qunbound);
|
|
3125 }
|
|
3126
|
|
3127 static int
|
|
3128 extent_putprop (Lisp_Object obj, Lisp_Object prop, Lisp_Object value)
|
|
3129 {
|
|
3130 error ("Not yet implemented"); /* #### */
|
|
3131 return 0;
|
|
3132 }
|
|
3133
|
|
3134 static int
|
|
3135 extent_remprop (Lisp_Object obj, Lisp_Object prop)
|
|
3136 {
|
|
3137 error ("Not yet implemented"); /* #### */
|
|
3138 return 0;
|
|
3139 }
|
|
3140
|
|
3141 static Lisp_Object
|
|
3142 extent_plist (Lisp_Object obj)
|
|
3143 {
|
|
3144 return Fextent_properties (obj);
|
|
3145 }
|
|
3146
|
|
3147
|
|
3148 /************************************************************************/
|
|
3149 /* basic extent accessors */
|
|
3150 /************************************************************************/
|
|
3151
|
|
3152 /* These functions are for checking externally-passed extent objects
|
|
3153 and returning an extent's basic properties, which include the
|
|
3154 buffer the extent is associated with, the endpoints of the extent's
|
|
3155 range, the open/closed-ness of those endpoints, and whether the
|
|
3156 extent is detached. Manipulating these properties requires
|
|
3157 manipulating the ordered lists that hold extents; thus, functions
|
|
3158 to do that are in a later section. */
|
|
3159
|
|
3160 /* Given a Lisp_Object that is supposed to be an extent, make sure it
|
|
3161 is OK and return an extent pointer. Extents can be in one of four
|
|
3162 states:
|
|
3163
|
|
3164 1) destroyed
|
|
3165 2) detached and not associated with a buffer
|
|
3166 3) detached and associated with a buffer
|
|
3167 4) attached to a buffer
|
|
3168
|
|
3169 If FLAGS is 0, types 2-4 are allowed. If FLAGS is DE_MUST_HAVE_BUFFER,
|
|
3170 types 3-4 are allowed. If FLAGS is DE_MUST_BE_ATTACHED, only type 4
|
|
3171 is allowed.
|
|
3172 */
|
|
3173
|
|
3174 static EXTENT
|
|
3175 decode_extent (Lisp_Object extent_obj, unsigned int flags)
|
|
3176 {
|
|
3177 EXTENT extent;
|
|
3178 Lisp_Object obj;
|
|
3179
|
|
3180 CHECK_LIVE_EXTENT (extent_obj);
|
|
3181 extent = XEXTENT (extent_obj);
|
|
3182 obj = extent_object (extent);
|
|
3183
|
|
3184 /* the following condition will fail if we're dealing with a freed extent */
|
|
3185 assert (NILP (obj) || BUFFERP (obj) || STRINGP (obj));
|
|
3186
|
|
3187 if (flags & DE_MUST_BE_ATTACHED)
|
|
3188 flags |= DE_MUST_HAVE_BUFFER;
|
|
3189
|
|
3190 /* if buffer is dead, then convert extent to have no buffer. */
|
|
3191 if (BUFFERP (obj) && !BUFFER_LIVE_P (XBUFFER (obj)))
|
|
3192 obj = extent_object (extent) = Qnil;
|
|
3193
|
|
3194 assert (!NILP (obj) || extent_detached_p (extent));
|
|
3195
|
|
3196 if (NILP (obj) && (flags & DE_MUST_HAVE_BUFFER))
|
|
3197 {
|
|
3198 signal_simple_error ("extent doesn't belong to a buffer or string",
|
|
3199 extent_obj);
|
|
3200 }
|
|
3201
|
|
3202 if (extent_detached_p (extent) && (flags & DE_MUST_BE_ATTACHED))
|
|
3203 {
|
|
3204 signal_simple_error ("extent cannot be detached", extent_obj);
|
|
3205 }
|
|
3206
|
|
3207 return extent;
|
|
3208 }
|
|
3209
|
|
3210 /* Note that the returned value is a buffer position, not a byte index. */
|
|
3211
|
|
3212 static Lisp_Object
|
|
3213 extent_endpoint_external (Lisp_Object extent_obj, int endp)
|
|
3214 {
|
|
3215 EXTENT extent = decode_extent (extent_obj, 0);
|
|
3216
|
|
3217 if (extent_detached_p (extent))
|
|
3218 return Qnil;
|
|
3219 else
|
|
3220 return make_int (extent_endpoint_bufpos (extent, endp));
|
|
3221 }
|
|
3222
|
20
|
3223 DEFUN ("extentp", Fextentp, 1, 1, 0, /*
|
0
|
3224 T if OBJECT is an extent.
|
20
|
3225 */
|
|
3226 (object))
|
0
|
3227 {
|
|
3228 if (EXTENTP (object))
|
|
3229 return Qt;
|
|
3230 return Qnil;
|
|
3231 }
|
|
3232
|
20
|
3233 DEFUN ("extent-live-p", Fextent_live_p, 1, 1, 0, /*
|
0
|
3234 T if OBJECT is an extent and the extent has not been destroyed.
|
20
|
3235 */
|
|
3236 (object))
|
0
|
3237 {
|
|
3238 if (EXTENTP (object) && EXTENT_LIVE_P (XEXTENT (object)))
|
|
3239 return Qt;
|
|
3240 return Qnil;
|
|
3241 }
|
|
3242
|
20
|
3243 DEFUN ("extent-detached-p", Fextent_detached_p, 1, 1, 0, /*
|
0
|
3244 T if EXTENT is detached.
|
20
|
3245 */
|
|
3246 (extent))
|
0
|
3247 {
|
|
3248 if (extent_detached_p (decode_extent (extent, 0)))
|
|
3249 return Qt;
|
|
3250 return Qnil;
|
|
3251 }
|
|
3252
|
20
|
3253 DEFUN ("extent-object", Fextent_object, 1, 1, 0, /*
|
0
|
3254 Return object (buffer or string) EXTENT refers to.
|
20
|
3255 */
|
|
3256 (extent))
|
0
|
3257 {
|
|
3258 return extent_object (decode_extent (extent, 0));
|
|
3259 }
|
|
3260
|
20
|
3261 DEFUN ("extent-start-position", Fextent_start_position, 1, 1, 0, /*
|
0
|
3262 Return start position of EXTENT, or nil if EXTENT is detached.
|
20
|
3263 */
|
|
3264 (extent))
|
0
|
3265 {
|
|
3266 return extent_endpoint_external (extent, 0);
|
|
3267 }
|
|
3268
|
20
|
3269 DEFUN ("extent-end-position", Fextent_end_position, 1, 1, 0, /*
|
0
|
3270 Return end position of EXTENT, or nil if EXTENT is detached.
|
20
|
3271 */
|
|
3272 (extent))
|
0
|
3273 {
|
|
3274 return extent_endpoint_external (extent, 1);
|
|
3275 }
|
|
3276
|
20
|
3277 DEFUN ("extent-length", Fextent_length, 1, 1, 0, /*
|
0
|
3278 Return length of EXTENT in characters.
|
20
|
3279 */
|
|
3280 (extent))
|
0
|
3281 {
|
|
3282 EXTENT e = decode_extent (extent, DE_MUST_BE_ATTACHED);
|
|
3283 return make_int (extent_endpoint_bufpos (e, 1)
|
|
3284 - extent_endpoint_bufpos (e, 0));
|
|
3285 }
|
|
3286
|
20
|
3287 DEFUN ("next-extent", Fnext_extent, 1, 1, 0, /*
|
0
|
3288 Find next extent after EXTENT.
|
|
3289 If EXTENT is a buffer return the first extent in the buffer; likewise
|
|
3290 for strings.
|
|
3291 Extents in a buffer are ordered in what is called the \"display\"
|
|
3292 order, which sorts by increasing start positions and then by *decreasing*
|
|
3293 end positions.
|
|
3294 If you want to perform an operation on a series of extents, use
|
|
3295 `map-extents' instead of this function; it is much more efficient.
|
|
3296 The primary use of this function should be to enumerate all the
|
|
3297 extents in a buffer.
|
|
3298 Note: The display order is not necessarily the order that `map-extents'
|
|
3299 processes extents in!
|
20
|
3300 */
|
|
3301 (extent))
|
0
|
3302 {
|
|
3303 Lisp_Object val;
|
|
3304 EXTENT next;
|
|
3305
|
|
3306 if (EXTENTP (extent))
|
|
3307 next = extent_next (decode_extent (extent, DE_MUST_BE_ATTACHED));
|
|
3308 else
|
|
3309 next = extent_first (decode_buffer_or_string (extent));
|
|
3310
|
|
3311 if (!next)
|
|
3312 return (Qnil);
|
|
3313 XSETEXTENT (val, next);
|
|
3314 return (val);
|
|
3315 }
|
|
3316
|
20
|
3317 DEFUN ("previous-extent", Fprevious_extent, 1, 1, 0, /*
|
0
|
3318 Find last extent before EXTENT.
|
|
3319 If EXTENT is a buffer return the last extent in the buffer; likewise
|
|
3320 for strings.
|
|
3321 This function is analogous to `next-extent'.
|
20
|
3322 */
|
|
3323 (extent))
|
0
|
3324 {
|
|
3325 Lisp_Object val;
|
|
3326 EXTENT prev;
|
|
3327
|
|
3328 if (EXTENTP (extent))
|
|
3329 prev = extent_previous (decode_extent (extent, DE_MUST_BE_ATTACHED));
|
|
3330 else
|
|
3331 prev = extent_last (decode_buffer_or_string (extent));
|
|
3332
|
|
3333 if (!prev)
|
|
3334 return (Qnil);
|
|
3335 XSETEXTENT (val, prev);
|
|
3336 return (val);
|
|
3337 }
|
|
3338
|
|
3339 #ifdef DEBUG_XEMACS
|
|
3340
|
20
|
3341 DEFUN ("next-e-extent", Fnext_e_extent, 1, 1, 0, /*
|
0
|
3342 Find next extent after EXTENT using the \"e\" order.
|
|
3343 If EXTENT is a buffer return the first extent in the buffer; likewise
|
|
3344 for strings.
|
20
|
3345 */
|
|
3346 (extent))
|
0
|
3347 {
|
|
3348 Lisp_Object val;
|
|
3349 EXTENT next;
|
|
3350
|
|
3351 if (EXTENTP (extent))
|
|
3352 next = extent_e_next (decode_extent (extent, DE_MUST_BE_ATTACHED));
|
|
3353 else
|
|
3354 next = extent_e_first (decode_buffer_or_string (extent));
|
|
3355
|
|
3356 if (!next)
|
|
3357 return (Qnil);
|
|
3358 XSETEXTENT (val, next);
|
|
3359 return (val);
|
|
3360 }
|
|
3361
|
20
|
3362 DEFUN ("previous-e-extent", Fprevious_e_extent, 1, 1, 0, /*
|
0
|
3363 Find last extent before EXTENT using the \"e\" order.
|
|
3364 If EXTENT is a buffer return the last extent in the buffer; likewise
|
|
3365 for strings.
|
|
3366 This function is analogous to `next-e-extent'.
|
20
|
3367 */
|
|
3368 (extent))
|
0
|
3369 {
|
|
3370 Lisp_Object val;
|
|
3371 EXTENT prev;
|
|
3372
|
|
3373 if (EXTENTP (extent))
|
|
3374 prev = extent_e_previous (decode_extent (extent, DE_MUST_BE_ATTACHED));
|
|
3375 else
|
|
3376 prev = extent_e_last (decode_buffer_or_string (extent));
|
|
3377
|
|
3378 if (!prev)
|
|
3379 return (Qnil);
|
|
3380 XSETEXTENT (val, prev);
|
|
3381 return (val);
|
|
3382 }
|
|
3383
|
|
3384 #endif
|
|
3385
|
20
|
3386 DEFUN ("next-extent-change", Fnext_extent_change, 1, 2, 0, /*
|
0
|
3387 Return the next position after POS where an extent begins or ends.
|
|
3388 If POS is at the end of the buffer or string, POS will be returned;
|
|
3389 otherwise a position greater than POS will always be returned.
|
|
3390 If BUFFER is nil, the current buffer is assumed.
|
20
|
3391 */
|
|
3392 (pos, object))
|
0
|
3393 {
|
|
3394 Lisp_Object obj = decode_buffer_or_string (object);
|
|
3395 Bytind bpos;
|
|
3396
|
|
3397 bpos = get_buffer_or_string_pos_byte (obj, pos, GB_ALLOW_PAST_ACCESSIBLE);
|
|
3398 bpos = extent_find_end_of_run (obj, bpos, 1);
|
|
3399 return make_int (buffer_or_string_bytind_to_bufpos (obj, bpos));
|
|
3400 }
|
|
3401
|
20
|
3402 DEFUN ("previous-extent-change", Fprevious_extent_change, 1, 2, 0, /*
|
0
|
3403 Return the last position before POS where an extent begins or ends.
|
|
3404 If POS is at the beginning of the buffer or string, POS will be returned;
|
|
3405 otherwise a position less than POS will always be returned.
|
|
3406 If OBJECT is nil, the current buffer is assumed.
|
20
|
3407 */
|
|
3408 (pos, object))
|
0
|
3409 {
|
|
3410 Lisp_Object obj = decode_buffer_or_string (object);
|
|
3411 Bytind bpos;
|
|
3412
|
|
3413 bpos = get_buffer_or_string_pos_byte (obj, pos, GB_ALLOW_PAST_ACCESSIBLE);
|
|
3414 bpos = extent_find_beginning_of_run (obj, bpos, 1);
|
|
3415 return make_int (buffer_or_string_bytind_to_bufpos (obj, bpos));
|
|
3416 }
|
|
3417
|
|
3418
|
|
3419 /************************************************************************/
|
|
3420 /* parent and children stuff */
|
|
3421 /************************************************************************/
|
|
3422
|
20
|
3423 DEFUN ("extent-parent", Fextent_parent, 1, 1, 0, /*
|
0
|
3424 Return the parent (if any) of EXTENT.
|
|
3425 If an extent has a parent, it derives all its properties from that extent
|
|
3426 and has no properties of its own. (The only \"properties\" that the
|
|
3427 extent keeps are the buffer/string it refers to and the start and end
|
|
3428 points.) It is possible for an extent's parent to itself have a parent.
|
20
|
3429 */
|
|
3430 (extent))
|
0
|
3431 /* do I win the prize for the strangest split infinitive? */
|
|
3432 {
|
|
3433 EXTENT e = decode_extent (extent, 0);
|
|
3434 return extent_parent (e);
|
|
3435 }
|
|
3436
|
20
|
3437 DEFUN ("extent-children", Fextent_children, 1, 1, 0, /*
|
0
|
3438 Return a list of the children (if any) of EXTENT.
|
|
3439 The children of an extent are all those extents whose parent is that extent.
|
|
3440 This function does not recursively trace children of children.
|
|
3441 \(To do that, use `extent-descendants'.)
|
20
|
3442 */
|
|
3443 (extent))
|
0
|
3444 {
|
|
3445 EXTENT e = decode_extent (extent, 0);
|
|
3446 Lisp_Object children = extent_children (e);
|
|
3447
|
|
3448 if (!NILP (children))
|
|
3449 return Fcopy_sequence (XWEAK_LIST_LIST (children));
|
|
3450 else
|
|
3451 return Qnil;
|
|
3452 }
|
|
3453
|
|
3454 static void
|
|
3455 remove_extent_from_children_list (EXTENT e, Lisp_Object child)
|
|
3456 {
|
|
3457 Lisp_Object children = extent_children (e);
|
|
3458
|
|
3459 #ifdef ERROR_CHECK_EXTENTS
|
|
3460 assert (!NILP (memq_no_quit (child, XWEAK_LIST_LIST (children))));
|
|
3461 #endif
|
|
3462 XWEAK_LIST_LIST (children) =
|
|
3463 delq_no_quit (child, XWEAK_LIST_LIST (children));
|
|
3464 }
|
|
3465
|
|
3466 static void
|
|
3467 add_extent_to_children_list (EXTENT e, Lisp_Object child)
|
|
3468 {
|
|
3469 Lisp_Object children = extent_children (e);
|
|
3470
|
|
3471 if (NILP (children))
|
|
3472 {
|
|
3473 children = make_weak_list (WEAK_LIST_SIMPLE);
|
|
3474 set_extent_no_chase_aux_field (e, children, children);
|
|
3475 }
|
|
3476
|
|
3477 #ifdef ERROR_CHECK_EXTENTS
|
|
3478 assert (NILP (memq_no_quit (child, XWEAK_LIST_LIST (children))));
|
|
3479 #endif
|
|
3480 XWEAK_LIST_LIST (children) = Fcons (child, XWEAK_LIST_LIST (children));
|
|
3481 }
|
|
3482
|
20
|
3483 DEFUN ("set-extent-parent", Fset_extent_parent, 2, 2, 0, /*
|
0
|
3484 Set the parent of EXTENT to PARENT (may be nil).
|
|
3485 See `extent-parent'.
|
20
|
3486 */
|
|
3487 (extent, parent))
|
0
|
3488 {
|
|
3489 EXTENT e = decode_extent (extent, 0);
|
|
3490 Lisp_Object cur_parent = extent_parent (e);
|
|
3491 Lisp_Object rest;
|
|
3492
|
|
3493 XSETEXTENT (extent, e);
|
|
3494 if (!NILP (parent))
|
|
3495 CHECK_LIVE_EXTENT (parent);
|
|
3496 if (EQ (parent, cur_parent))
|
|
3497 return Qnil;
|
|
3498 for (rest = parent; !NILP (rest); rest = extent_parent (XEXTENT (rest)))
|
|
3499 if (EQ (rest, extent))
|
|
3500 signal_simple_error ("Circular parent chain would result", extent);
|
|
3501 if (NILP (parent))
|
|
3502 {
|
|
3503 remove_extent_from_children_list (XEXTENT (cur_parent), extent);
|
|
3504 set_extent_no_chase_aux_field (e, parent, Qnil);
|
|
3505 e->flags.has_parent = 0;
|
|
3506 }
|
|
3507 else
|
|
3508 {
|
|
3509 add_extent_to_children_list (XEXTENT (parent), extent);
|
|
3510 set_extent_no_chase_aux_field (e, parent, parent);
|
|
3511 e->flags.has_parent = 1;
|
|
3512 }
|
|
3513 /* changing the parent also changes the properties of all children. */
|
|
3514 extent_maybe_changed_for_redisplay (e, 1);
|
|
3515 return Qnil;
|
|
3516 }
|
|
3517
|
|
3518
|
|
3519 /************************************************************************/
|
|
3520 /* basic extent mutators */
|
|
3521 /************************************************************************/
|
|
3522
|
|
3523 /* Note: If you track non-duplicable extents by undo, you'll get bogus
|
|
3524 undo records for transient extents via update-extent.
|
|
3525 For example, query-replace will do this.
|
|
3526 */
|
|
3527
|
|
3528 static void
|
|
3529 set_extent_endpoints_1 (EXTENT extent, Memind start, Memind end)
|
|
3530 {
|
|
3531 #ifdef ERROR_CHECK_EXTENTS
|
|
3532 Lisp_Object obj = extent_object (extent);
|
|
3533
|
|
3534 assert (start <= end);
|
|
3535 if (BUFFERP (obj))
|
|
3536 {
|
|
3537 assert (valid_memind_p (XBUFFER (obj), start));
|
|
3538 assert (valid_memind_p (XBUFFER (obj), end));
|
|
3539 }
|
|
3540 #endif
|
|
3541
|
|
3542 /* Optimization: if the extent is already where we want it to be,
|
|
3543 do nothing. */
|
|
3544 if (!extent_detached_p (extent) && extent_start (extent) == start &&
|
|
3545 extent_end (extent) == end)
|
|
3546 return;
|
|
3547
|
|
3548 if (extent_detached_p (extent))
|
|
3549 {
|
|
3550 if (extent_duplicable_p (extent))
|
|
3551 {
|
|
3552 Lisp_Object extent_obj;
|
|
3553 XSETEXTENT (extent_obj, extent);
|
|
3554 record_extent (extent_obj, 1);
|
|
3555 }
|
|
3556 }
|
|
3557 else
|
|
3558 extent_detach (extent);
|
|
3559
|
|
3560 set_extent_start (extent, start);
|
|
3561 set_extent_end (extent, end);
|
|
3562 extent_attach (extent);
|
|
3563 }
|
|
3564
|
|
3565 /* Set extent's endpoints to S and E, and put extent in buffer or string
|
|
3566 OBJECT. (If OBJECT is nil, do not change the extent's object.) */
|
|
3567
|
|
3568 void
|
|
3569 set_extent_endpoints (EXTENT extent, Bytind s, Bytind e, Lisp_Object object)
|
|
3570 {
|
|
3571 Memind start, end;
|
|
3572
|
|
3573 if (NILP (object))
|
|
3574 {
|
|
3575 object = extent_object (extent);
|
|
3576 assert (!NILP (object));
|
|
3577 }
|
|
3578 else if (!EQ (object, extent_object (extent)))
|
|
3579 {
|
|
3580 extent_detach (extent);
|
|
3581 extent_object (extent) = object;
|
|
3582 }
|
|
3583
|
|
3584 start = s < 0 ? extent_start (extent) :
|
|
3585 buffer_or_string_bytind_to_memind (object, s);
|
|
3586 end = e < 0 ? extent_end (extent) :
|
|
3587 buffer_or_string_bytind_to_memind (object, e);
|
|
3588 set_extent_endpoints_1 (extent, start, end);
|
|
3589 }
|
|
3590
|
|
3591 static void
|
|
3592 set_extent_openness (EXTENT extent, int start_open, int end_open)
|
|
3593 {
|
|
3594 if (start_open == -1)
|
|
3595 start_open = extent_start_open_p (extent);
|
|
3596 if (end_open == -1)
|
|
3597 end_open = extent_end_open_p (extent);
|
|
3598 extent_start_open_p (extent) = start_open;
|
|
3599 extent_end_open_p (extent) = end_open;
|
|
3600 /* changing the open/closedness of an extent does not affect
|
|
3601 redisplay. */
|
|
3602 }
|
|
3603
|
|
3604 static EXTENT
|
|
3605 make_extent_internal (Lisp_Object object, Bytind from, Bytind to)
|
|
3606 {
|
|
3607 EXTENT extent;
|
|
3608
|
|
3609 extent = make_extent_detached (object);
|
|
3610 set_extent_endpoints (extent, from, to, Qnil);
|
|
3611 return extent;
|
|
3612 }
|
|
3613
|
|
3614 static EXTENT
|
|
3615 copy_extent (EXTENT original, Bytind from, Bytind to, Lisp_Object object)
|
|
3616 {
|
|
3617 EXTENT e;
|
|
3618
|
|
3619 e = make_extent_detached (object);
|
|
3620 if (from >= 0)
|
|
3621 set_extent_endpoints (e, from, to, Qnil);
|
|
3622
|
|
3623 e->plist = Fcopy_sequence (original->plist);
|
|
3624 memcpy (&e->flags, &original->flags, sizeof (e->flags));
|
|
3625 if (e->flags.has_aux)
|
|
3626 {
|
|
3627 /* also need to copy the aux struct. It won't work for
|
|
3628 this extent to share the same aux struct as the original
|
|
3629 one. */
|
|
3630 struct extent_auxiliary *data =
|
|
3631 alloc_lcrecord (sizeof (struct extent_auxiliary),
|
|
3632 lrecord_extent_auxiliary);
|
|
3633
|
|
3634 copy_lcrecord (data, XEXTENT_AUXILIARY (XCAR (original->plist)));
|
|
3635 XSETEXTENT_AUXILIARY (XCAR (e->plist), data);
|
|
3636 }
|
|
3637
|
|
3638 {
|
|
3639 /* we may have just added another child to the parent extent. */
|
|
3640 Lisp_Object parent = extent_parent (e);
|
|
3641 if (!NILP (parent))
|
|
3642 {
|
|
3643 Lisp_Object extent;
|
|
3644 XSETEXTENT (extent, e);
|
|
3645 add_extent_to_children_list (XEXTENT (parent), extent);
|
|
3646 }
|
|
3647 }
|
|
3648
|
|
3649 /* #### it's still unclear to me that this Energize-specific junk
|
|
3650 needs to be in here. Just use the general mechanisms, or fix
|
|
3651 them up! --ben */
|
|
3652 #ifdef ENERGIZE
|
|
3653 if (energize_extent_data (original))
|
|
3654 {
|
|
3655 extent_plist_slot (e) = Qnil; /* slightly antisocial... */
|
|
3656 restore_energize_extent_state (e);
|
|
3657 }
|
|
3658 #endif
|
|
3659
|
|
3660 return e;
|
|
3661 }
|
|
3662
|
|
3663 static void
|
|
3664 destroy_extent (EXTENT extent)
|
|
3665 {
|
|
3666 Lisp_Object rest, nextrest, children;
|
|
3667 Lisp_Object extent_obj = Qnil;
|
|
3668
|
|
3669 if (!extent_detached_p (extent))
|
|
3670 extent_detach (extent);
|
|
3671 /* disassociate the extent from its children and parent */
|
|
3672 children = extent_children (extent);
|
|
3673 if (!NILP (children))
|
|
3674 {
|
|
3675 LIST_LOOP_DELETING (rest, nextrest, XWEAK_LIST_LIST (children))
|
|
3676 Fset_extent_parent (XCAR (rest), Qnil);
|
|
3677 }
|
|
3678 XSETEXTENT (extent_obj, extent);
|
|
3679 Fset_extent_parent (extent_obj, Qnil);
|
|
3680 /* mark the extent as destroyed */
|
|
3681 extent_object (extent) = Qt;
|
|
3682 }
|
|
3683
|
20
|
3684 DEFUN ("make-extent", Fmake_extent, 2, 3, 0, /*
|
0
|
3685 Make an extent for the range [FROM, TO) in BUFFER-OR-STRING.
|
|
3686 BUFFER-OR-STRING defaults to the current buffer. Insertions at point
|
|
3687 TO will be outside of the extent; insertions at FROM will be inside the
|
|
3688 extent, causing the extent to grow. (This is the same way that markers
|
|
3689 behave.) You can change the behavior of insertions at the endpoints
|
|
3690 using `set-extent-property'. The extent is initially detached if both
|
|
3691 FROM and TO are nil, and in this case BUFFER-OR-STRING defaults to nil,
|
|
3692 meaning the extent is in no buffer and no string.
|
20
|
3693 */
|
|
3694 (from, to, buffer_or_string))
|
0
|
3695 {
|
|
3696 Lisp_Object extent_obj = Qnil;
|
|
3697 Lisp_Object obj;
|
|
3698
|
|
3699 obj = decode_buffer_or_string (buffer_or_string);
|
|
3700 if (NILP (from) && NILP (to))
|
|
3701 {
|
|
3702 if (NILP (buffer_or_string))
|
|
3703 obj = Qnil;
|
|
3704 XSETEXTENT (extent_obj, make_extent_detached (obj));
|
|
3705 }
|
|
3706 else
|
|
3707 {
|
|
3708 Bytind start, end;
|
|
3709
|
|
3710 get_buffer_or_string_range_byte (obj, from, to, &start, &end,
|
|
3711 GB_ALLOW_PAST_ACCESSIBLE);
|
|
3712 XSETEXTENT (extent_obj, make_extent_internal (obj, start, end));
|
|
3713 }
|
|
3714 return extent_obj;
|
|
3715 }
|
|
3716
|
20
|
3717 DEFUN ("copy-extent", Fcopy_extent, 1, 2, 0, /*
|
0
|
3718 Make a copy of EXTENT. It is initially detached.
|
|
3719 Optional argument BUFFER-OR-STRING defaults to EXTENT's buffer or string.
|
20
|
3720 */
|
|
3721 (extent, buffer_or_string))
|
0
|
3722 {
|
|
3723 EXTENT ext = decode_extent (extent, 0);
|
|
3724
|
|
3725 if (NILP (buffer_or_string))
|
|
3726 buffer_or_string = extent_object (ext);
|
|
3727 else
|
|
3728 buffer_or_string = decode_buffer_or_string (buffer_or_string);
|
|
3729
|
|
3730 XSETEXTENT (extent, copy_extent (ext, -1, -1, buffer_or_string));
|
|
3731 return extent;
|
|
3732 }
|
|
3733
|
20
|
3734 DEFUN ("delete-extent", Fdelete_extent, 1, 1, 0, /*
|
0
|
3735 Remove EXTENT from its buffer and destroy it.
|
|
3736 This does not modify the buffer's text, only its display properties.
|
|
3737 The extent cannot be used thereafter.
|
20
|
3738 */
|
|
3739 (extent))
|
0
|
3740 {
|
|
3741 EXTENT ext;
|
|
3742
|
|
3743 /* We do not call decode_extent() here because already-destroyed
|
|
3744 extents are OK. */
|
|
3745 CHECK_EXTENT (extent);
|
|
3746 ext = XEXTENT (extent);
|
|
3747
|
|
3748 if (!EXTENT_LIVE_P (ext))
|
|
3749 return Qnil;
|
|
3750 destroy_extent (ext);
|
|
3751 return Qnil;
|
|
3752 }
|
|
3753
|
20
|
3754 DEFUN ("detach-extent", Fdetach_extent, 1, 1, 0, /*
|
0
|
3755 Remove EXTENT from its buffer in such a way that it can be re-inserted.
|
|
3756 An extent is also detached when all of its characters are all killed by a
|
|
3757 deletion, unless its `detachable' property has been unset.
|
|
3758
|
|
3759 Extents which have the `duplicable' attribute are tracked by the undo
|
|
3760 mechanism. Detachment via `detach-extent' and string deletion is recorded,
|
|
3761 as is attachment via `insert-extent' and string insertion. Extent motion,
|
|
3762 face changes, and attachment via `make-extent' and `set-extent-endpoints'
|
|
3763 are not recorded. This means that extent changes which are to be undo-able
|
|
3764 must be performed by character editing, or by insertion and detachment of
|
|
3765 duplicable extents.
|
20
|
3766 */
|
|
3767 (extent))
|
0
|
3768 {
|
|
3769 EXTENT ext = decode_extent (extent, 0);
|
|
3770
|
|
3771 if (extent_detached_p (ext))
|
|
3772 return extent;
|
|
3773 if (extent_duplicable_p (ext))
|
|
3774 record_extent (extent, 0);
|
|
3775 extent_detach (ext);
|
|
3776
|
|
3777 return extent;
|
|
3778 }
|
|
3779
|
20
|
3780 DEFUN ("set-extent-endpoints", Fset_extent_endpoints, 3, 4, 0, /*
|
0
|
3781 Set the endpoints of EXTENT to START, END.
|
|
3782 If START and END are null, call detach-extent on EXTENT.
|
|
3783 BUFFER-OR-STRING specifies the new buffer or string that the extent should
|
|
3784 be in, and defaults to EXTENT's buffer or string. (If nil, and EXTENT
|
|
3785 is in no buffer and no string, it defaults to the current buffer.)
|
|
3786 See documentation on `detach-extent' for a discussion of undo recording.
|
20
|
3787 */
|
|
3788 (extent, start, end, buffer_or_string))
|
0
|
3789 {
|
|
3790 EXTENT ext;
|
|
3791 Bytind s, e;
|
|
3792
|
|
3793 ext = decode_extent (extent, 0);
|
|
3794
|
|
3795 if (NILP (buffer_or_string))
|
|
3796 {
|
|
3797 buffer_or_string = extent_object (ext);
|
|
3798 if (NILP (buffer_or_string))
|
|
3799 buffer_or_string = Fcurrent_buffer ();
|
|
3800 }
|
|
3801 else
|
|
3802 buffer_or_string = decode_buffer_or_string (buffer_or_string);
|
|
3803
|
|
3804 if (NILP (start) && NILP (end))
|
|
3805 return Fdetach_extent (extent);
|
|
3806
|
|
3807 get_buffer_or_string_range_byte (buffer_or_string, start, end, &s, &e,
|
|
3808 GB_ALLOW_PAST_ACCESSIBLE);
|
|
3809
|
|
3810 set_extent_endpoints (ext, s, e, buffer_or_string);
|
|
3811 return extent;
|
|
3812 }
|
|
3813
|
|
3814
|
|
3815 /************************************************************************/
|
|
3816 /* mapping over extents */
|
|
3817 /************************************************************************/
|
|
3818
|
|
3819 static unsigned int
|
|
3820 decode_map_extents_flags (Lisp_Object flags)
|
|
3821 {
|
|
3822 unsigned int retval = 0;
|
|
3823 unsigned int all_extents_specified = 0;
|
|
3824 unsigned int in_region_specified = 0;
|
|
3825
|
|
3826 if (EQ (flags, Qt)) /* obsoleteness compatibility */
|
|
3827 return ME_END_CLOSED;
|
|
3828 if (NILP (flags))
|
|
3829 return 0;
|
|
3830 if (SYMBOLP (flags))
|
|
3831 flags = Fcons (flags, Qnil);
|
|
3832 while (!NILP (flags))
|
|
3833 {
|
|
3834 Lisp_Object sym;
|
|
3835 CHECK_CONS (flags);
|
|
3836 sym = XCAR (flags);
|
|
3837 CHECK_SYMBOL (sym);
|
|
3838 if (EQ (sym, Qall_extents_closed) || EQ (sym, Qall_extents_open) ||
|
|
3839 EQ (sym, Qall_extents_closed_open) ||
|
|
3840 EQ (sym, Qall_extents_open_closed))
|
|
3841 {
|
|
3842 if (all_extents_specified)
|
|
3843 error ("Only one `all-extents-*' flag may be specified");
|
|
3844 all_extents_specified = 1;
|
|
3845 }
|
|
3846 if (EQ (sym, Qstart_in_region) || EQ (sym, Qend_in_region) ||
|
|
3847 EQ (sym, Qstart_and_end_in_region) ||
|
|
3848 EQ (sym, Qstart_or_end_in_region))
|
|
3849 {
|
|
3850 if (in_region_specified)
|
|
3851 error ("Only one `*-in-region' flag may be specified");
|
|
3852 in_region_specified = 1;
|
|
3853 }
|
|
3854
|
|
3855 /* I do so love that conditional operator ... */
|
|
3856 retval |=
|
|
3857 EQ (sym, Qend_closed) ? ME_END_CLOSED :
|
|
3858 EQ (sym, Qstart_open) ? ME_START_OPEN :
|
|
3859 EQ (sym, Qall_extents_closed) ? ME_ALL_EXTENTS_CLOSED :
|
|
3860 EQ (sym, Qall_extents_open) ? ME_ALL_EXTENTS_OPEN :
|
|
3861 EQ (sym, Qall_extents_closed_open) ? ME_ALL_EXTENTS_CLOSED_OPEN :
|
|
3862 EQ (sym, Qall_extents_open_closed) ? ME_ALL_EXTENTS_OPEN_CLOSED :
|
|
3863 EQ (sym, Qstart_in_region) ? ME_START_IN_REGION :
|
|
3864 EQ (sym, Qend_in_region) ? ME_END_IN_REGION :
|
|
3865 EQ (sym, Qstart_and_end_in_region) ? ME_START_AND_END_IN_REGION :
|
|
3866 EQ (sym, Qstart_or_end_in_region) ? ME_START_OR_END_IN_REGION :
|
|
3867 EQ (sym, Qnegate_in_region) ? ME_NEGATE_IN_REGION :
|
|
3868 (signal_simple_error ("Invalid `map-extents' flag", sym), 0);
|
|
3869
|
|
3870 flags = XCDR (flags);
|
|
3871 }
|
|
3872 return retval;
|
|
3873 }
|
|
3874
|
20
|
3875 DEFUN ("extent-in-region-p", Fextent_in_region_p, 1, 4, 0, /*
|
0
|
3876 Return whether EXTENT overlaps a specified region.
|
|
3877 This is equivalent to whether `map-extents' would visit EXTENT when called
|
|
3878 with these args.
|
20
|
3879 */
|
|
3880 (extent, from, to, flags))
|
0
|
3881 {
|
|
3882 EXTENT ext;
|
|
3883 Lisp_Object obj;
|
|
3884 Bytind start, end;
|
|
3885
|
|
3886 ext = decode_extent (extent, DE_MUST_BE_ATTACHED);
|
|
3887 obj = extent_object (ext);
|
|
3888 get_buffer_or_string_range_byte (obj, from, to, &start, &end, GB_ALLOW_NIL |
|
|
3889 GB_ALLOW_PAST_ACCESSIBLE);
|
|
3890
|
|
3891 if (extent_in_region_p (ext, start, end, decode_map_extents_flags (flags)))
|
|
3892 return Qt;
|
|
3893 return Qnil;
|
|
3894 }
|
|
3895
|
|
3896 struct slow_map_extents_arg
|
|
3897 {
|
|
3898 Lisp_Object map_arg;
|
|
3899 Lisp_Object map_routine;
|
|
3900 Lisp_Object result;
|
|
3901 Lisp_Object property;
|
|
3902 Lisp_Object value;
|
|
3903 };
|
|
3904
|
|
3905 static int
|
|
3906 slow_map_extents_function (EXTENT extent, void *arg)
|
|
3907 {
|
|
3908 /* This function can GC */
|
|
3909 struct slow_map_extents_arg *closure = (struct slow_map_extents_arg *) arg;
|
|
3910 Lisp_Object extent_obj;
|
|
3911
|
|
3912 XSETEXTENT (extent_obj, extent);
|
|
3913
|
|
3914 /* make sure this extent qualifies according to the PROPERTY
|
|
3915 and VALUE args */
|
|
3916
|
|
3917 if (!NILP (closure->property))
|
|
3918 {
|
|
3919 Lisp_Object value = Fextent_property (extent_obj, closure->property,
|
|
3920 Qnil);
|
|
3921 if ((NILP (closure->value) && NILP (value)) ||
|
|
3922 (!NILP (closure->value) && !EQ (value, closure->value)))
|
|
3923 return 0;
|
|
3924 }
|
|
3925
|
|
3926 closure->result = call2 (closure->map_routine, extent_obj,
|
|
3927 closure->map_arg);
|
|
3928 if (NILP (closure->result))
|
|
3929 return 0;
|
|
3930 else
|
|
3931 return 1;
|
|
3932 }
|
|
3933
|
20
|
3934 DEFUN ("map-extents", Fmap_extents, 1, 8, 0, /*
|
0
|
3935 Map FUNCTION over the extents which overlap a region in OBJECT.
|
|
3936 OBJECT is normally a buffer or string but could be an extent (see below).
|
|
3937 The region is normally bounded by [FROM, TO) (i.e. the beginning of the
|
|
3938 region is closed and the end of the region is open), but this can be
|
|
3939 changed with the FLAGS argument (see below for a complete discussion).
|
|
3940
|
|
3941 FUNCTION is called with the arguments (extent, MAPARG). The arguments
|
|
3942 OBJECT, FROM, TO, MAPARG, and FLAGS are all optional and default to
|
|
3943 the current buffer, the beginning of OBJECT, the end of OBJECT, nil,
|
|
3944 and nil, respectively. `map-extents' returns the first non-nil result
|
|
3945 produced by FUNCTION, and no more calls to FUNCTION are made after it
|
|
3946 returns non-nil.
|
|
3947
|
|
3948 If OBJECT is an extent, FROM and TO default to the extent's endpoints,
|
|
3949 and the mapping omits that extent and its predecessors. This feature
|
|
3950 supports restarting a loop based on `map-extents'. Note: OBJECT must
|
|
3951 be attached to a buffer or string, and the mapping is done over that
|
|
3952 buffer or string.
|
|
3953
|
|
3954 An extent overlaps the region if there is any point in the extent that is
|
|
3955 also in the region. (For the purpose of overlap, zero-length extents and
|
|
3956 regions are treated as closed on both ends regardless of their endpoints'
|
|
3957 specified open/closedness.) Note that the endpoints of an extent or region
|
|
3958 are considered to be in that extent or region if and only if the
|
|
3959 corresponding end is closed. For example, the extent [5,7] overlaps the
|
|
3960 region [2,5] because 5 is in both the extent and the region. However, (5,7]
|
|
3961 does not overlap [2,5] because 5 is not in the extent, and neither [5,7] nor
|
|
3962 \(5,7] overlaps the region [2,5) because 5 is not in the region.
|
|
3963
|
|
3964 The optional FLAGS can be a symbol or a list of one or more symbols,
|
|
3965 modifying the behavior of `map-extents'. Allowed symbols are:
|
|
3966
|
|
3967 end-closed The region's end is closed.
|
|
3968
|
|
3969 start-open The region's start is open.
|
|
3970
|
|
3971 all-extents-closed Treat all extents as closed on both ends for the
|
|
3972 purpose of determining whether they overlap the
|
|
3973 region, irrespective of their actual open- or
|
|
3974 closedness.
|
|
3975 all-extents-open Treat all extents as open on both ends.
|
|
3976 all-extents-closed-open Treat all extents as start-closed, end-open.
|
|
3977 all-extents-open-closed Treat all extents as start-open, end-closed.
|
|
3978
|
|
3979 start-in-region In addition to the above conditions for extent
|
|
3980 overlap, the extent's start position must lie within
|
|
3981 the specified region. Note that, for this
|
|
3982 condition, open start positions are treated as if
|
|
3983 0.5 was added to the endpoint's value, and open
|
|
3984 end positions are treated as if 0.5 was subtracted
|
|
3985 from the endpoint's value.
|
|
3986 end-in-region The extent's end position must lie within the
|
|
3987 region.
|
|
3988 start-and-end-in-region Both the extent's start and end positions must lie
|
|
3989 within the region.
|
|
3990 start-or-end-in-region Either the extent's start or end position must lie
|
|
3991 within the region.
|
|
3992
|
|
3993 negate-in-region The condition specified by a `*-in-region' flag
|
|
3994 must NOT hold for the extent to be considered.
|
|
3995
|
|
3996
|
|
3997 At most one of `all-extents-closed', `all-extents-open',
|
|
3998 `all-extents-closed-open', and `all-extents-open-closed' may be specified.
|
|
3999
|
|
4000 At most one of `start-in-region', `end-in-region',
|
|
4001 `start-and-end-in-region', and `start-or-end-in-region' may be specified.
|
|
4002
|
|
4003 If optional arg PROPERTY is non-nil, only extents with that property set
|
|
4004 on them will be visited. If optional arg VALUE is non-nil, only extents
|
|
4005 whose value for that property is `eq' to VALUE will be visited.
|
20
|
4006 */
|
|
4007 (function, object, from, to, maparg, flags, property, value))
|
0
|
4008 {
|
|
4009 /* This function can GC */
|
|
4010 struct slow_map_extents_arg closure;
|
|
4011 unsigned int me_flags;
|
|
4012 Bytind start, end;
|
|
4013 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
|
4014 EXTENT after = 0;
|
|
4015
|
|
4016 if (EXTENTP (object))
|
|
4017 {
|
|
4018 after = decode_extent (object, DE_MUST_BE_ATTACHED);
|
|
4019 if (NILP (from))
|
|
4020 from = Fextent_start_position (object);
|
|
4021 if (NILP (to))
|
|
4022 to = Fextent_end_position (object);
|
|
4023 object = extent_object (after);
|
|
4024 }
|
|
4025 else
|
|
4026 object = decode_buffer_or_string (object);
|
|
4027
|
|
4028 get_buffer_or_string_range_byte (object, from, to, &start, &end,
|
|
4029 GB_ALLOW_NIL | GB_ALLOW_PAST_ACCESSIBLE);
|
|
4030
|
|
4031 me_flags = decode_map_extents_flags (flags);
|
|
4032
|
|
4033 if (!NILP (property))
|
|
4034 {
|
|
4035 CHECK_SYMBOL (property);
|
|
4036 if (!NILP (value))
|
|
4037 value = canonicalize_extent_property (property, value);
|
|
4038 }
|
|
4039
|
|
4040 GCPRO5 (function, maparg, object, property, value);
|
|
4041
|
|
4042 closure.map_arg = maparg;
|
|
4043 closure.map_routine = function;
|
|
4044 closure.result = Qnil;
|
|
4045 closure.property = property;
|
|
4046 closure.value = value;
|
|
4047
|
|
4048 map_extents_bytind (start, end, slow_map_extents_function,
|
|
4049 (void *) &closure, object, after,
|
|
4050 /* You never know what the user might do ... */
|
|
4051 me_flags | ME_MIGHT_CALL_ELISP);
|
|
4052
|
|
4053 UNGCPRO;
|
|
4054 return closure.result;
|
|
4055 }
|
|
4056
|
|
4057
|
|
4058 /************************************************************************/
|
|
4059 /* mapping over extents -- other functions */
|
|
4060 /************************************************************************/
|
|
4061
|
|
4062 /* ------------------------------- */
|
|
4063 /* map-extent-children */
|
|
4064 /* ------------------------------- */
|
|
4065
|
|
4066 struct slow_map_extent_children_arg
|
|
4067 {
|
|
4068 Lisp_Object map_arg;
|
|
4069 Lisp_Object map_routine;
|
|
4070 Lisp_Object result;
|
|
4071 Lisp_Object property;
|
|
4072 Lisp_Object value;
|
|
4073 Bytind start_min;
|
|
4074 Bytind prev_start;
|
|
4075 Bytind prev_end;
|
|
4076 };
|
|
4077
|
|
4078 static int
|
|
4079 slow_map_extent_children_function (EXTENT extent, void *arg)
|
|
4080 {
|
|
4081 /* This function can GC */
|
|
4082 struct slow_map_extent_children_arg *closure =
|
|
4083 (struct slow_map_extent_children_arg *) arg;
|
|
4084 Lisp_Object extent_obj;
|
|
4085 Bytind start = extent_endpoint_bytind (extent, 0);
|
|
4086 Bytind end = extent_endpoint_bytind (extent, 1);
|
|
4087 /* Make sure the extent starts inside the region of interest,
|
|
4088 rather than just overlaps it.
|
|
4089 */
|
|
4090 if (start < closure->start_min)
|
|
4091 return 0;
|
|
4092 /* Make sure the extent is not a child of a previous visited one.
|
|
4093 We know already, because of extent ordering,
|
|
4094 that start >= prev_start, and that if
|
|
4095 start == prev_start, then end <= prev_end.
|
|
4096 */
|
|
4097 if (start == closure->prev_start)
|
|
4098 {
|
|
4099 if (end < closure->prev_end)
|
|
4100 return 0;
|
|
4101 }
|
|
4102 else /* start > prev_start */
|
|
4103 {
|
|
4104 if (start < closure->prev_end)
|
|
4105 return 0;
|
|
4106 /* corner case: prev_end can be -1 if there is no prev */
|
|
4107 }
|
|
4108 XSETEXTENT (extent_obj, extent);
|
|
4109
|
|
4110 /* make sure this extent qualifies according to the PROPERTY
|
|
4111 and VALUE args */
|
|
4112
|
|
4113 if (!NILP (closure->property))
|
|
4114 {
|
|
4115 Lisp_Object value = Fextent_property (extent_obj, closure->property,
|
|
4116 Qnil);
|
|
4117 if ((NILP (closure->value) && NILP (value)) ||
|
|
4118 (!NILP (closure->value) && !EQ (value, closure->value)))
|
|
4119 return 0;
|
|
4120 }
|
|
4121
|
|
4122 closure->result = call2 (closure->map_routine, extent_obj,
|
|
4123 closure->map_arg);
|
|
4124
|
|
4125 /* Since the callback may change the buffer, compute all stored
|
|
4126 buffer positions here.
|
|
4127 */
|
|
4128 closure->start_min = -1; /* no need for this any more */
|
|
4129 closure->prev_start = extent_endpoint_bytind (extent, 0);
|
|
4130 closure->prev_end = extent_endpoint_bytind (extent, 1);
|
|
4131
|
|
4132 if (NILP (closure->result))
|
|
4133 return 0;
|
|
4134 else
|
|
4135 return 1;
|
|
4136 }
|
|
4137
|
20
|
4138 DEFUN ("map-extent-children", Fmap_extent_children, 1, 8, 0, /*
|
0
|
4139 Map FUNCTION over the extents in the region from FROM to TO.
|
|
4140 FUNCTION is called with arguments (extent, MAPARG). See `map-extents'
|
|
4141 for a full discussion of the arguments FROM, TO, and FLAGS.
|
|
4142
|
|
4143 The arguments are the same as for `map-extents', but this function differs
|
|
4144 in that it only visits extents which start in the given region, and also
|
|
4145 in that, after visiting an extent E, it skips all other extents which start
|
|
4146 inside E but end before E's end.
|
|
4147
|
|
4148 Thus, this function may be used to walk a tree of extents in a buffer:
|
|
4149 (defun walk-extents (buffer &optional ignore)
|
|
4150 (map-extent-children 'walk-extents buffer))
|
20
|
4151 */
|
|
4152 (function, object, from, to, maparg, flags, property, value))
|
0
|
4153 {
|
|
4154 /* This function can GC */
|
|
4155 struct slow_map_extent_children_arg closure;
|
|
4156 unsigned int me_flags;
|
|
4157 Bytind start, end;
|
|
4158 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
|
4159 EXTENT after = 0;
|
|
4160
|
|
4161 if (EXTENTP (object))
|
|
4162 {
|
|
4163 after = decode_extent (object, DE_MUST_BE_ATTACHED);
|
|
4164 if (NILP (from))
|
|
4165 from = Fextent_start_position (object);
|
|
4166 if (NILP (to))
|
|
4167 to = Fextent_end_position (object);
|
|
4168 object = extent_object (after);
|
|
4169 }
|
|
4170 else
|
|
4171 object = decode_buffer_or_string (object);
|
|
4172
|
|
4173 get_buffer_or_string_range_byte (object, from, to, &start, &end,
|
|
4174 GB_ALLOW_NIL | GB_ALLOW_PAST_ACCESSIBLE);
|
|
4175
|
|
4176 me_flags = decode_map_extents_flags (flags);
|
|
4177
|
|
4178 if (!NILP (property))
|
|
4179 {
|
|
4180 CHECK_SYMBOL (property);
|
|
4181 if (!NILP (value))
|
|
4182 value = canonicalize_extent_property (property, value);
|
|
4183 }
|
|
4184
|
|
4185 GCPRO5 (function, maparg, object, property, value);
|
|
4186
|
|
4187 closure.map_arg = maparg;
|
|
4188 closure.map_routine = function;
|
|
4189 closure.result = Qnil;
|
|
4190 closure.property = property;
|
|
4191 closure.value = value;
|
|
4192 closure.start_min = start;
|
|
4193 closure.prev_start = -1;
|
|
4194 closure.prev_end = -1;
|
|
4195 map_extents_bytind (start, end, slow_map_extent_children_function,
|
|
4196 (void *) &closure, object, after,
|
|
4197 /* You never know what the user might do ... */
|
|
4198 me_flags | ME_MIGHT_CALL_ELISP);
|
|
4199
|
|
4200 UNGCPRO;
|
|
4201 return closure.result;
|
|
4202 }
|
|
4203
|
|
4204 /* ------------------------------- */
|
|
4205 /* extent-at */
|
|
4206 /* ------------------------------- */
|
|
4207
|
|
4208 /* find "smallest" matching extent containing pos -- (flag == 0) means
|
|
4209 all extents match, else (EXTENT_FLAGS (extent) & flag) must be true;
|
|
4210 for more than one matching extent with precisely the same endpoints,
|
|
4211 we choose the last extent in the extents_list.
|
|
4212 The search stops just before "before", if that is non-null.
|
|
4213 */
|
|
4214
|
|
4215 struct extent_at_arg
|
|
4216 {
|
|
4217 EXTENT best_match;
|
|
4218 Memind best_start;
|
|
4219 Memind best_end;
|
|
4220 Lisp_Object prop;
|
|
4221 EXTENT before;
|
|
4222 };
|
|
4223
|
|
4224 enum extent_at_flag
|
|
4225 {
|
|
4226 EXTENT_AT_AFTER,
|
|
4227 EXTENT_AT_BEFORE,
|
|
4228 EXTENT_AT_AT
|
|
4229 };
|
|
4230
|
|
4231 static enum extent_at_flag
|
|
4232 decode_extent_at_flag (Lisp_Object at_flag)
|
|
4233 {
|
|
4234 enum extent_at_flag fl;
|
|
4235
|
|
4236 if (NILP (at_flag))
|
|
4237 fl = EXTENT_AT_AFTER;
|
|
4238 else
|
|
4239 {
|
|
4240 CHECK_SYMBOL (at_flag);
|
|
4241 if (EQ (at_flag, Qafter))
|
|
4242 fl = EXTENT_AT_AFTER;
|
|
4243 else if (EQ (at_flag, Qbefore))
|
|
4244 fl = EXTENT_AT_BEFORE;
|
|
4245 else if (EQ (at_flag, Qat))
|
|
4246 fl = EXTENT_AT_AT;
|
|
4247 else
|
|
4248 signal_simple_error ("Invalid AT-FLAG in `extent-at'", at_flag);
|
|
4249 }
|
|
4250
|
|
4251 return fl;
|
|
4252 }
|
|
4253
|
|
4254 static int
|
|
4255 extent_at_mapper (EXTENT e, void *arg)
|
|
4256 {
|
|
4257 struct extent_at_arg *closure = (struct extent_at_arg *) arg;
|
|
4258
|
|
4259 if (e == closure->before)
|
|
4260 return 1;
|
|
4261
|
|
4262 /* If closure->prop is non-nil, then the extent is only acceptable
|
|
4263 if it has a non-nil value for that property. */
|
|
4264 if (!NILP (closure->prop))
|
|
4265 {
|
|
4266 Lisp_Object extent;
|
|
4267 XSETEXTENT (extent, e);
|
|
4268 if (NILP (Fextent_property (extent, closure->prop, Qnil)))
|
|
4269 return 0;
|
|
4270 }
|
|
4271
|
|
4272 {
|
|
4273 EXTENT current = closure->best_match;
|
|
4274
|
|
4275 if (!current)
|
|
4276 goto accept;
|
|
4277 /* redundant but quick test */
|
|
4278 else if (extent_start (current) > extent_start (e))
|
|
4279 return 0;
|
|
4280
|
|
4281 /* we return the "last" best fit, instead of the first --
|
|
4282 this is because then the glyph closest to two equivalent
|
|
4283 extents corresponds to the "extent-at" the text just past
|
|
4284 that same glyph */
|
|
4285 else if (!EXTENT_LESS_VALS (e, closure->best_start,
|
|
4286 closure->best_end))
|
|
4287 goto accept;
|
|
4288 else
|
|
4289 return 0;
|
|
4290 accept:
|
|
4291 closure->best_match = e;
|
|
4292 closure->best_start = extent_start (e);
|
|
4293 closure->best_end = extent_end (e);
|
|
4294 }
|
|
4295
|
|
4296 return 0;
|
|
4297 }
|
|
4298
|
|
4299 static Lisp_Object
|
|
4300 extent_at_bytind (Bytind position, Lisp_Object object, Lisp_Object property,
|
|
4301 EXTENT before, enum extent_at_flag at_flag)
|
|
4302 {
|
|
4303 struct extent_at_arg closure;
|
|
4304 Lisp_Object extent_obj = Qnil;
|
|
4305
|
|
4306 /* it might be argued that invalid positions should cause
|
|
4307 errors, but the principle of least surprise dictates that
|
|
4308 nil should be returned (extent-at is often used in
|
|
4309 response to a mouse event, and in many cases previous events
|
|
4310 have changed the buffer contents).
|
|
4311
|
|
4312 Also, the openness stuff in the text-property code currently
|
|
4313 does not check its limits and might go off the end. */
|
|
4314 if ((at_flag == EXTENT_AT_BEFORE
|
|
4315 ? position <= buffer_or_string_absolute_begin_byte (object)
|
|
4316 : position < buffer_or_string_absolute_begin_byte (object))
|
|
4317 || (at_flag == EXTENT_AT_AFTER
|
|
4318 ? position >= buffer_or_string_absolute_end_byte (object)
|
|
4319 : position > buffer_or_string_absolute_end_byte (object)))
|
|
4320 return Qnil;
|
|
4321
|
|
4322 closure.best_match = 0;
|
|
4323 closure.prop = property;
|
|
4324 closure.before = before;
|
|
4325
|
|
4326 map_extents_bytind (at_flag == EXTENT_AT_BEFORE ? position - 1 : position,
|
|
4327 at_flag == EXTENT_AT_AFTER ? position + 1 : position,
|
|
4328 extent_at_mapper, (void *) &closure, object, 0,
|
|
4329 ME_START_OPEN | ME_ALL_EXTENTS_CLOSED);
|
|
4330
|
|
4331 if (!closure.best_match)
|
|
4332 return Qnil;
|
|
4333
|
|
4334 XSETEXTENT (extent_obj, closure.best_match);
|
|
4335 return extent_obj;
|
|
4336 }
|
|
4337
|
20
|
4338 DEFUN ("extent-at", Fextent_at, 1, 5, 0, /*
|
0
|
4339 Find \"smallest\" extent at POS in OBJECT having PROPERTY set.
|
|
4340 Normally, an extent is \"at\" POS if it overlaps the region (POS, POS+1);
|
|
4341 i.e. if it covers the character after POS. (However, see the definition
|
|
4342 of AT-FLAG.) \"Smallest\" means the extent that comes last in the display
|
|
4343 order; this normally means the extent whose start position is closest to
|
|
4344 POS. See `next-extent' for more information.
|
|
4345 OBJECT specifies a buffer or string and defaults to the current buffer.
|
|
4346 PROPERTY defaults to nil, meaning that any extent will do.
|
|
4347 Properties are attached to extents with `set-extent-property', which see.
|
|
4348 Returns nil if POS is invalid or there is no matching extent at POS.
|
|
4349 If the fourth argument BEFORE is not nil, it must be an extent; any returned
|
|
4350 extent will precede that extent. This feature allows `extent-at' to be
|
|
4351 used by a loop over extents.
|
|
4352 AT-FLAG controls how end cases are handled, and should be one of:
|
|
4353
|
|
4354 nil or `after' An extent is at POS if it covers the character
|
|
4355 after POS. This is consistent with the way
|
|
4356 that text properties work.
|
|
4357 `before' An extent is at POS if it covers the character
|
|
4358 before POS.
|
|
4359 `at' An extent is at POS if it overlaps or abuts POS.
|
|
4360 This includes all zero-length extents at POS.
|
|
4361
|
|
4362 Note that in all cases, the start-openness and end-openness of the extents
|
|
4363 considered is ignored. If you want to pay attention to those properties,
|
|
4364 you should use `map-extents', which gives you more control.
|
20
|
4365 */
|
|
4366 (pos, object, property, before, at_flag))
|
0
|
4367 {
|
|
4368 Bytind position;
|
|
4369 EXTENT before_extent;
|
|
4370 enum extent_at_flag fl;
|
|
4371
|
|
4372 object = decode_buffer_or_string (object);
|
|
4373 position = get_buffer_or_string_pos_byte (object, pos, GB_NO_ERROR_IF_BAD);
|
|
4374 CHECK_SYMBOL (property);
|
|
4375 if (NILP (before))
|
|
4376 before_extent = 0;
|
|
4377 else
|
|
4378 before_extent = decode_extent (before, DE_MUST_BE_ATTACHED);
|
|
4379 if (before_extent && !EQ (object, extent_object (before_extent)))
|
|
4380 signal_simple_error ("extent not in specified buffer or string", object);
|
|
4381 fl = decode_extent_at_flag (at_flag);
|
|
4382
|
|
4383 return extent_at_bytind (position, object, property, before_extent, fl);
|
|
4384 }
|
|
4385
|
|
4386 /* ------------------------------- */
|
|
4387 /* verify_extent_modification() */
|
|
4388 /* ------------------------------- */
|
|
4389
|
|
4390 /* verify_extent_modification() is called when a buffer or string is
|
|
4391 modified to check whether the modification is occuring inside a
|
|
4392 read-only extent.
|
|
4393 */
|
|
4394
|
|
4395 struct verify_extents_arg
|
|
4396 {
|
|
4397 Lisp_Object object;
|
|
4398 Memind start;
|
|
4399 Memind end;
|
|
4400 Lisp_Object iro; /* value of inhibit-read-only */
|
|
4401 };
|
|
4402
|
|
4403 static int
|
|
4404 verify_extent_mapper (EXTENT extent, void *arg)
|
|
4405 {
|
|
4406 struct verify_extents_arg *closure = (struct verify_extents_arg *) arg;
|
|
4407 Lisp_Object prop = extent_read_only (extent);
|
|
4408
|
|
4409 if (NILP (prop))
|
|
4410 return 0;
|
|
4411
|
|
4412 if (CONSP (closure->iro) && !NILP (Fmemq (prop, closure->iro)))
|
|
4413 return 0;
|
|
4414
|
24
|
4415 #if 0 /* Nobody seems to care for this any more -sb */
|
0
|
4416 /* Allow deletion if the extent is completely contained in
|
|
4417 the region being deleted.
|
|
4418 This is important for supporting tokens which are internally
|
|
4419 write-protected, but which can be killed and yanked as a whole.
|
|
4420 Ignore open/closed distinctions at this point.
|
|
4421 -- Rose
|
|
4422 */
|
|
4423 if (closure->start != closure->end &&
|
|
4424 extent_start (extent) >= closure->start &&
|
|
4425 extent_end (extent) <= closure->end)
|
|
4426 return 0;
|
24
|
4427 #endif
|
0
|
4428
|
|
4429 while (1)
|
|
4430 Fsignal (Qbuffer_read_only, (list1 (closure->object)));
|
|
4431
|
|
4432 RETURN_NOT_REACHED(0)
|
|
4433 }
|
|
4434
|
|
4435 /* Value of Vinhibit_read_only is precomputed and passed in for
|
|
4436 efficiency */
|
|
4437
|
|
4438 void
|
|
4439 verify_extent_modification (Lisp_Object object, Bytind from, Bytind to,
|
|
4440 Lisp_Object inhibit_read_only_value)
|
|
4441 {
|
|
4442 int closed;
|
|
4443 struct verify_extents_arg closure;
|
|
4444
|
|
4445 /* If insertion, visit closed-endpoint extents touching the insertion
|
|
4446 point because the text would go inside those extents. If deletion,
|
|
4447 treat the range as open on both ends so that touching extents are not
|
|
4448 visited. Note that we assume that an insertion is occurring if the
|
|
4449 changed range has zero length, and a deletion otherwise. This
|
|
4450 fails if a change (i.e. non-insertion, non-deletion) is happening.
|
|
4451 As far as I know, this doesn't currently occur in XEmacs. --ben */
|
|
4452 closed = (from==to);
|
|
4453 closure.object = object;
|
|
4454 closure.start = buffer_or_string_bytind_to_memind (object, from);
|
|
4455 closure.end = buffer_or_string_bytind_to_memind (object, to);
|
|
4456 closure.iro = inhibit_read_only_value;
|
|
4457
|
|
4458 map_extents_bytind (from, to, verify_extent_mapper, (void *) &closure,
|
|
4459 object, 0, closed ? ME_END_CLOSED : ME_START_OPEN);
|
|
4460 }
|
|
4461
|
|
4462 /* ------------------------------------ */
|
|
4463 /* process_extents_for_insertion() */
|
|
4464 /* ------------------------------------ */
|
|
4465
|
|
4466 struct process_extents_for_insertion_arg
|
|
4467 {
|
|
4468 Bytind opoint;
|
|
4469 int length;
|
|
4470 Lisp_Object object;
|
|
4471 };
|
|
4472
|
|
4473 /* A region of length LENGTH was just inserted at OPOINT. Modify all
|
|
4474 of the extents as required for the insertion, based on their
|
|
4475 start-open/end-open properties.
|
|
4476 */
|
|
4477
|
|
4478 static int
|
|
4479 process_extents_for_insertion_mapper (EXTENT extent, void *arg)
|
|
4480 {
|
|
4481 struct process_extents_for_insertion_arg *closure =
|
|
4482 (struct process_extents_for_insertion_arg *) arg;
|
|
4483 Memind indecks = buffer_or_string_bytind_to_memind (closure->object,
|
|
4484 closure->opoint);
|
|
4485
|
|
4486 /* When this function is called, one end of the newly-inserted text should
|
|
4487 be adjacent to some endpoint of the extent, or disjoint from it. If
|
|
4488 the insertion overlaps any existing extent, something is wrong.
|
|
4489 */
|
|
4490 #ifdef ERROR_CHECK_EXTENTS
|
|
4491 if (extent_start (extent) > indecks &&
|
|
4492 extent_start (extent) < indecks + closure->length)
|
|
4493 abort ();
|
|
4494 if (extent_end (extent) > indecks &&
|
|
4495 extent_end (extent) < indecks + closure->length)
|
|
4496 abort ();
|
|
4497 #endif
|
|
4498
|
|
4499 /* The extent-adjustment code adjusted the extent's endpoints as if
|
|
4500 they were markers -- endpoints at the gap (i.e. the insertion
|
|
4501 point) go to the left of the insertion point, which is correct
|
|
4502 for [) extents. We need to fix the other kinds of extents.
|
|
4503
|
|
4504 Note that both conditions below will hold for zero-length (]
|
|
4505 extents at the gap. Zero-length () extents would get adjusted
|
|
4506 such that their start is greater than their end; we treat them
|
|
4507 as [) extents. This is unfortunately an inelegant part of the
|
|
4508 extent model, but there is no way around it. */
|
|
4509
|
|
4510 {
|
|
4511 Memind new_start, new_end;
|
|
4512
|
|
4513 new_start = extent_start (extent);
|
|
4514 new_end = extent_end (extent);
|
|
4515 if (indecks == extent_start (extent) && extent_start_open_p (extent) &&
|
|
4516 /* coerce zero-length () extents to [) */
|
|
4517 new_start != new_end)
|
|
4518 new_start += closure->length;
|
|
4519 if (indecks == extent_end (extent) && !extent_end_open_p (extent))
|
|
4520 new_end += closure->length;
|
|
4521 set_extent_endpoints_1 (extent, new_start, new_end);
|
|
4522 }
|
|
4523
|
|
4524 return 0;
|
|
4525 }
|
|
4526
|
|
4527 void
|
|
4528 process_extents_for_insertion (Lisp_Object object, Bytind opoint,
|
|
4529 Bytecount length)
|
|
4530 {
|
|
4531 struct process_extents_for_insertion_arg closure;
|
|
4532
|
|
4533 closure.opoint = opoint;
|
|
4534 closure.length = length;
|
|
4535 closure.object = object;
|
|
4536
|
|
4537 map_extents_bytind (opoint, opoint + length,
|
|
4538 process_extents_for_insertion_mapper,
|
|
4539 (void *) &closure, object, 0,
|
|
4540 ME_END_CLOSED | ME_MIGHT_MODIFY_EXTENTS |
|
|
4541 ME_INCLUDE_INTERNAL);
|
|
4542 }
|
|
4543
|
|
4544 /* ------------------------------------ */
|
|
4545 /* process_extents_for_deletion() */
|
|
4546 /* ------------------------------------ */
|
|
4547
|
|
4548 struct process_extents_for_deletion_arg
|
|
4549 {
|
|
4550 Memind start, end;
|
|
4551 int destroy_included_extents;
|
|
4552 };
|
|
4553
|
|
4554 /* This function is called when we're about to delete the range [from, to].
|
|
4555 Detach all of the extents that are completely inside the range [from, to],
|
|
4556 if they're detachable or open-open. */
|
|
4557
|
|
4558 static int
|
|
4559 process_extents_for_deletion_mapper (EXTENT extent, void *arg)
|
|
4560 {
|
|
4561 struct process_extents_for_deletion_arg *closure =
|
|
4562 (struct process_extents_for_deletion_arg *) arg;
|
|
4563
|
|
4564 /* If the extent lies completely within the range that
|
|
4565 is being deleted, then nuke the extent if it's detachable
|
|
4566 (otherwise, it will become a zero-length extent). */
|
|
4567
|
|
4568 if (closure->start <= extent_start (extent) &&
|
|
4569 extent_end (extent) <= closure->end)
|
|
4570 {
|
|
4571 if (extent_detachable_p (extent))
|
|
4572 {
|
|
4573 if (closure->destroy_included_extents)
|
|
4574 destroy_extent (extent);
|
|
4575 else
|
|
4576 extent_detach (extent);
|
|
4577 }
|
|
4578 }
|
|
4579
|
|
4580 return 0;
|
|
4581 }
|
|
4582
|
|
4583 /* DESTROY_THEM means destroy the extents instead of just deleting them.
|
|
4584 It is unused currently, but perhaps might be used (there used to
|
|
4585 be a function process_extents_for_destruction(), #if 0'd out,
|
|
4586 that did the equivalent). */
|
|
4587 void
|
|
4588 process_extents_for_deletion (Lisp_Object object, Bytind from,
|
|
4589 Bytind to, int destroy_them)
|
|
4590 {
|
|
4591 struct process_extents_for_deletion_arg closure;
|
|
4592
|
|
4593 closure.start = buffer_or_string_bytind_to_memind (object, from);
|
|
4594 closure.end = buffer_or_string_bytind_to_memind (object, to);
|
|
4595 closure.destroy_included_extents = destroy_them;
|
|
4596
|
|
4597 map_extents_bytind (from, to, process_extents_for_deletion_mapper,
|
|
4598 (void *) &closure, object, 0,
|
|
4599 ME_END_CLOSED | ME_MIGHT_MODIFY_EXTENTS);
|
|
4600 }
|
|
4601
|
|
4602
|
|
4603 /************************************************************************/
|
|
4604 /* extent properties */
|
|
4605 /************************************************************************/
|
|
4606
|
|
4607 static void
|
|
4608 set_extent_invisible (EXTENT extent, Lisp_Object value)
|
|
4609 {
|
|
4610 if (!EQ (extent_invisible (extent), value))
|
|
4611 {
|
|
4612 set_extent_invisible_1 (extent, value);
|
|
4613 extent_changed_for_redisplay (extent, 1);
|
|
4614 }
|
|
4615 }
|
|
4616
|
|
4617 /* This function does "memoization" -- similar to the interning
|
|
4618 that happens with symbols. Given a list of faces, an equivalent
|
|
4619 list is returned such that if this function is called twice with
|
|
4620 input that is `equal', the resulting outputs will be `eq'.
|
|
4621
|
|
4622 Note that the inputs and outputs are in general *not* `equal' --
|
|
4623 faces in symbol form become actual face objects in the output.
|
|
4624 This is necessary so that temporary faces stay around. */
|
|
4625
|
|
4626 static Lisp_Object
|
|
4627 memoize_extent_face_internal (Lisp_Object list)
|
|
4628 {
|
|
4629 int len;
|
|
4630 int thelen;
|
|
4631 Lisp_Object cons, thecons;
|
|
4632 Lisp_Object oldtail, tail;
|
|
4633 struct gcpro gcpro1;
|
|
4634
|
|
4635 if (NILP (list))
|
|
4636 return Qnil;
|
|
4637 if (!CONSP (list))
|
|
4638 return Fget_face (list);
|
|
4639
|
|
4640 /* To do the memoization, we use a hash table mapping from
|
|
4641 external lists to internal lists. We do `equal' comparisons
|
|
4642 on the keys so the memoization works correctly.
|
|
4643
|
|
4644 Note that we canonicalize things so that the keys in the
|
|
4645 hashtable (the external lists) always contain symbols and
|
|
4646 the values (the internal lists) always contain face objects.
|
|
4647
|
|
4648 We also maintain a "reverse" table that maps from the internal
|
|
4649 lists to the external equivalents. The idea here is twofold:
|
|
4650
|
|
4651 1) `extent-face' wants to return a list containing face symbols
|
|
4652 rather than face objects.
|
|
4653 2) We don't want things to get quite so messed up if the user
|
|
4654 maliciously side-effects the returned lists.
|
|
4655 */
|
|
4656
|
|
4657 len = XINT (Flength (list));
|
|
4658 thelen = XINT (Flength (Vextent_face_reusable_list));
|
|
4659 oldtail = Qnil;
|
|
4660 tail = Qnil;
|
|
4661 GCPRO1 (oldtail);
|
|
4662
|
|
4663 /* We canonicalize the given list into another list.
|
|
4664 We try to avoid consing except when necessary, so we have
|
|
4665 a reusable list.
|
|
4666 */
|
|
4667
|
|
4668 if (thelen < len)
|
|
4669 {
|
|
4670 cons = Vextent_face_reusable_list;
|
|
4671 while (!NILP (XCDR (cons)))
|
|
4672 cons = XCDR (cons);
|
|
4673 XCDR (cons) = Fmake_list (make_int (len - thelen), Qnil);
|
|
4674 }
|
|
4675 else if (thelen > len)
|
|
4676 {
|
|
4677 int i;
|
|
4678
|
|
4679 /* Truncate the list temporarily so it's the right length;
|
|
4680 remember the old tail. */
|
|
4681 cons = Vextent_face_reusable_list;
|
|
4682 for (i = 0; i < len - 1; i++)
|
|
4683 cons = XCDR (cons);
|
|
4684 tail = cons;
|
|
4685 oldtail = XCDR (cons);
|
|
4686 XCDR (cons) = Qnil;
|
|
4687 }
|
|
4688
|
|
4689 thecons = Vextent_face_reusable_list;
|
|
4690 EXTERNAL_LIST_LOOP (cons, list)
|
|
4691 {
|
|
4692 Lisp_Object face = Fget_face (XCAR (cons));
|
|
4693
|
|
4694 XCAR (thecons) = Fface_name (face);
|
|
4695 thecons = XCDR (thecons);
|
|
4696 }
|
|
4697
|
|
4698 list = Fgethash (Vextent_face_reusable_list, Vextent_face_memoize_hash_table,
|
|
4699 Qnil);
|
|
4700 if (NILP (list))
|
|
4701 {
|
|
4702 Lisp_Object symlist = Fcopy_sequence (Vextent_face_reusable_list);
|
|
4703 Lisp_Object facelist = Fcopy_sequence (Vextent_face_reusable_list);
|
|
4704
|
|
4705 LIST_LOOP (cons, facelist)
|
|
4706 {
|
|
4707 XCAR (cons) = Fget_face (XCAR (cons));
|
|
4708 }
|
|
4709 Fputhash (symlist, facelist, Vextent_face_memoize_hash_table);
|
|
4710 Fputhash (facelist, symlist, Vextent_face_reverse_memoize_hash_table);
|
|
4711 list = facelist;
|
|
4712 }
|
|
4713
|
|
4714 /* Now restore the truncated tail of the reusable list, if necessary. */
|
|
4715 if (!NILP (tail))
|
|
4716 XCDR (tail) = oldtail;
|
|
4717
|
|
4718 UNGCPRO;
|
|
4719 return list;
|
|
4720 }
|
|
4721
|
|
4722 static Lisp_Object
|
|
4723 external_of_internal_memoized_face (Lisp_Object face)
|
|
4724 {
|
|
4725 if (NILP (face))
|
|
4726 return Qnil;
|
|
4727 else if (!CONSP (face))
|
|
4728 return XFACE (face)->name;
|
|
4729 else
|
|
4730 {
|
|
4731 face = Fgethash (face, Vextent_face_reverse_memoize_hash_table,
|
|
4732 Qunbound);
|
|
4733 assert (!UNBOUNDP (face));
|
|
4734 return face;
|
|
4735 }
|
|
4736 }
|
|
4737
|
|
4738 static Lisp_Object
|
|
4739 canonicalize_extent_property (Lisp_Object prop, Lisp_Object value)
|
|
4740 {
|
|
4741 if (EQ (prop, Qface) || EQ (prop, Qmouse_face))
|
|
4742 value = (external_of_internal_memoized_face
|
|
4743 (memoize_extent_face_internal (value)));
|
|
4744 return value;
|
|
4745 }
|
|
4746
|
20
|
4747 DEFUN ("extent-face", Fextent_face, 1, 1, 0, /*
|
0
|
4748 Return the name of the face in which EXTENT is displayed, or nil
|
|
4749 if the extent's face is unspecified. This might also return a list
|
|
4750 of face names.
|
20
|
4751 */
|
|
4752 (extent))
|
0
|
4753 {
|
|
4754 Lisp_Object face;
|
|
4755
|
|
4756 CHECK_EXTENT (extent);
|
|
4757 face = extent_face (XEXTENT (extent));
|
|
4758
|
|
4759 return external_of_internal_memoized_face (face);
|
|
4760 }
|
|
4761
|
20
|
4762 DEFUN ("set-extent-face", Fset_extent_face, 2, 2, 0, /*
|
0
|
4763 Make the given EXTENT have the graphic attributes specified by FACE.
|
|
4764 FACE can also be a list of faces, and all faces listed will apply,
|
|
4765 with faces earlier in the list taking priority over those later in the
|
|
4766 list.
|
20
|
4767 */
|
|
4768 (extent, face))
|
0
|
4769 {
|
|
4770 EXTENT e;
|
|
4771 Lisp_Object orig_face = face;
|
|
4772
|
|
4773 CHECK_EXTENT (extent);
|
|
4774 e = XEXTENT (extent);
|
|
4775 /* retrieve the ancestor for efficiency and proper redisplay noting. */
|
|
4776 e = extent_ancestor (e);
|
|
4777
|
|
4778 face = memoize_extent_face_internal (face);
|
|
4779
|
|
4780 extent_face (e) = face;
|
|
4781 extent_changed_for_redisplay (e, 1);
|
|
4782
|
|
4783 return orig_face;
|
|
4784 }
|
|
4785
|
|
4786
|
20
|
4787 DEFUN ("extent-mouse-face", Fextent_mouse_face, 1, 1, 0, /*
|
0
|
4788 Return the face used to highlight EXTENT when the mouse passes over it.
|
|
4789 The return value will be a face name, a list of face names, or nil
|
|
4790 if the extent's mouse face is unspecified.
|
20
|
4791 */
|
|
4792 (extent))
|
0
|
4793 {
|
|
4794 Lisp_Object face;
|
|
4795
|
|
4796 CHECK_EXTENT (extent);
|
|
4797 face = extent_mouse_face (XEXTENT (extent));
|
|
4798
|
|
4799 return external_of_internal_memoized_face (face);
|
|
4800 }
|
|
4801
|
20
|
4802 DEFUN ("set-extent-mouse-face", Fset_extent_mouse_face, 2, 2, 0, /*
|
0
|
4803 Set the face used to highlight EXTENT when the mouse passes over it.
|
|
4804 FACE can also be a list of faces, and all faces listed will apply,
|
|
4805 with faces earlier in the list taking priority over those later in the
|
|
4806 list.
|
20
|
4807 */
|
|
4808 (extent, face))
|
0
|
4809 {
|
|
4810 EXTENT e;
|
|
4811 Lisp_Object orig_face = face;
|
|
4812
|
|
4813 CHECK_EXTENT (extent);
|
|
4814 e = XEXTENT (extent);
|
|
4815 /* retrieve the ancestor for efficiency and proper redisplay noting. */
|
|
4816 e = extent_ancestor (e);
|
|
4817
|
|
4818 face = memoize_extent_face_internal (face);
|
|
4819
|
|
4820 set_extent_mouse_face (e, face);
|
|
4821 extent_changed_for_redisplay (e, 1);
|
|
4822
|
|
4823 return orig_face;
|
|
4824 }
|
|
4825
|
|
4826 void
|
|
4827 set_extent_glyph (EXTENT extent, Lisp_Object glyph, int endp,
|
|
4828 unsigned int layout)
|
|
4829 {
|
|
4830 extent = extent_ancestor (extent);
|
|
4831
|
|
4832 if (!endp)
|
|
4833 {
|
|
4834 set_extent_begin_glyph (extent, glyph);
|
|
4835 extent_begin_glyph_layout (extent) = layout;
|
|
4836 }
|
|
4837 else
|
|
4838 {
|
|
4839 set_extent_end_glyph (extent, glyph);
|
|
4840 extent_end_glyph_layout (extent) = layout;
|
|
4841 }
|
|
4842
|
|
4843 extent_changed_for_redisplay (extent, 1);
|
|
4844 }
|
|
4845
|
|
4846 static Lisp_Object
|
|
4847 glyph_layout_to_symbol (unsigned int layout)
|
|
4848 {
|
|
4849 switch (layout)
|
|
4850 {
|
|
4851 case GL_TEXT: return Qtext;
|
|
4852 case GL_OUTSIDE_MARGIN: return Qoutside_margin;
|
|
4853 case GL_INSIDE_MARGIN: return Qinside_margin;
|
|
4854 case GL_WHITESPACE: return Qwhitespace;
|
|
4855 default: abort ();
|
|
4856 }
|
|
4857 return Qnil; /* shut up compiler */
|
|
4858 }
|
|
4859
|
|
4860 static unsigned int
|
|
4861 symbol_to_glyph_layout (Lisp_Object layout_obj)
|
|
4862 {
|
|
4863 unsigned int layout = 0;
|
|
4864
|
|
4865 if (NILP (layout_obj))
|
|
4866 layout = GL_TEXT;
|
|
4867 else
|
|
4868 {
|
|
4869 CHECK_SYMBOL (layout_obj);
|
|
4870 if (EQ (Qoutside_margin, layout_obj))
|
|
4871 layout = GL_OUTSIDE_MARGIN;
|
|
4872 else if (EQ (Qinside_margin, layout_obj))
|
|
4873 layout = GL_INSIDE_MARGIN;
|
|
4874 else if (EQ (Qwhitespace, layout_obj))
|
|
4875 layout = GL_WHITESPACE;
|
|
4876 else if (EQ (Qtext, layout_obj))
|
|
4877 layout = GL_TEXT;
|
|
4878 else
|
|
4879 signal_simple_error ("unknown glyph layout type", layout_obj);
|
|
4880 }
|
|
4881 return layout;
|
|
4882 }
|
|
4883
|
|
4884 static Lisp_Object
|
|
4885 set_extent_glyph_1 (Lisp_Object extent_obj, Lisp_Object glyph, int endp,
|
|
4886 Lisp_Object layout_obj)
|
|
4887 {
|
|
4888 EXTENT extent = decode_extent (extent_obj, DE_MUST_HAVE_BUFFER);
|
|
4889 unsigned int layout = symbol_to_glyph_layout (layout_obj);
|
|
4890
|
|
4891 /* Make sure we've actually been given a glyph or it's nil (meaning
|
|
4892 we're deleting a glyph from an extent). */
|
|
4893 if (!NILP (glyph))
|
|
4894 CHECK_GLYPH (glyph);
|
|
4895
|
|
4896 set_extent_glyph (extent, glyph, endp, layout);
|
|
4897 return glyph;
|
|
4898 }
|
|
4899
|
20
|
4900 DEFUN ("set-extent-begin-glyph", Fset_extent_begin_glyph, 2, 3, 0, /*
|
0
|
4901 Display a bitmap, subwindow or string at the beginning of EXTENT.
|
|
4902 BEGIN-GLYPH must be a glyph object. The layout policy defaults to `text'.
|
20
|
4903 */
|
|
4904 (extent, begin_glyph, layout))
|
0
|
4905 {
|
|
4906 return set_extent_glyph_1 (extent, begin_glyph, 0, layout);
|
|
4907 }
|
|
4908
|
20
|
4909 DEFUN ("set-extent-end-glyph", Fset_extent_end_glyph, 2, 3, 0, /*
|
0
|
4910 Display a bitmap, subwindow or string at the end of the EXTENT.
|
|
4911 END-GLYPH must be a glyph object. The layout policy defaults to `text'.
|
20
|
4912 */
|
|
4913 (extent, end_glyph, layout))
|
0
|
4914 {
|
|
4915 return set_extent_glyph_1 (extent, end_glyph, 1, layout);
|
|
4916 }
|
|
4917
|
20
|
4918 DEFUN ("extent-begin-glyph", Fextent_begin_glyph, 1, 1, 0, /*
|
0
|
4919 Return the glyph object displayed at the beginning of EXTENT.
|
|
4920 If there is none, nil is returned.
|
20
|
4921 */
|
|
4922 (extent_obj))
|
0
|
4923 {
|
|
4924 return extent_begin_glyph (decode_extent (extent_obj, 0));
|
|
4925 }
|
|
4926
|
20
|
4927 DEFUN ("extent-end-glyph", Fextent_end_glyph, 1, 1, 0, /*
|
0
|
4928 Return the glyph object displayed at the end of EXTENT.
|
|
4929 If there is none, nil is returned.
|
20
|
4930 */
|
|
4931 (extent_obj))
|
0
|
4932 {
|
|
4933 return extent_end_glyph (decode_extent (extent_obj, 0));
|
|
4934 }
|
|
4935
|
20
|
4936 DEFUN ("set-extent-begin-glyph-layout", Fset_extent_begin_glyph_layout, 2, 2, 0, /*
|
0
|
4937 Set the layout policy of the given extent's begin glyph.
|
|
4938 Access this using the `extent-begin-glyph-layout' function.
|
20
|
4939 */
|
|
4940 (extent, layout))
|
0
|
4941 {
|
|
4942 EXTENT e = decode_extent (extent, 0);
|
|
4943 e = extent_ancestor (e);
|
|
4944 extent_begin_glyph_layout (e) = symbol_to_glyph_layout (layout);
|
|
4945 extent_maybe_changed_for_redisplay (e, 1);
|
|
4946 return layout;
|
|
4947 }
|
|
4948
|
20
|
4949 DEFUN ("set-extent-end-glyph-layout", Fset_extent_end_glyph_layout, 2, 2, 0, /*
|
0
|
4950 Set the layout policy of the given extent's end glyph.
|
|
4951 Access this using the `extent-end-glyph-layout' function.
|
20
|
4952 */
|
|
4953 (extent, layout))
|
0
|
4954 {
|
|
4955 EXTENT e = decode_extent (extent, 0);
|
|
4956 e = extent_ancestor (e);
|
|
4957 extent_end_glyph_layout (e) = symbol_to_glyph_layout (layout);
|
|
4958 extent_maybe_changed_for_redisplay (e, 1);
|
|
4959 return layout;
|
|
4960 }
|
|
4961
|
20
|
4962 DEFUN ("extent-begin-glyph-layout", Fextent_begin_glyph_layout, 1, 1, 0, /*
|
0
|
4963 Return the layout policy associated with the given extent's begin glyph.
|
|
4964 Set this using the `set-extent-begin-glyph-layout' function.
|
20
|
4965 */
|
|
4966 (extent))
|
0
|
4967 {
|
|
4968 EXTENT e = decode_extent (extent, 0);
|
|
4969 return glyph_layout_to_symbol (extent_begin_glyph_layout (e));
|
|
4970 }
|
|
4971
|
20
|
4972 DEFUN ("extent-end-glyph-layout", Fextent_end_glyph_layout, 1, 1, 0, /*
|
0
|
4973 Return the layout policy associated with the given extent's end glyph.
|
|
4974 Set this using the `set-extent-end-glyph-layout' function.
|
20
|
4975 */
|
|
4976 (extent))
|
0
|
4977 {
|
|
4978 EXTENT e = decode_extent (extent, 0);
|
|
4979 return glyph_layout_to_symbol (extent_end_glyph_layout (e));
|
|
4980 }
|
|
4981
|
20
|
4982 DEFUN ("set-extent-priority", Fset_extent_priority, 2, 2, 0, /*
|
0
|
4983 Changes the display priority of EXTENT.
|
|
4984 When the extent attributes are being merged for display, the priority
|
|
4985 is used to determine which extent takes precedence in the event of a
|
|
4986 conflict (two extents whose faces both specify font, for example: the
|
|
4987 font of the extent with the higher priority will be used).
|
|
4988 Extents are created with priority 0; priorities may be negative.
|
20
|
4989 */
|
|
4990 (extent, pri))
|
0
|
4991 {
|
|
4992 EXTENT e = decode_extent (extent, 0);
|
|
4993
|
|
4994 CHECK_INT (pri);
|
|
4995 e = extent_ancestor (e);
|
|
4996 set_extent_priority (e, XINT (pri));
|
|
4997 extent_maybe_changed_for_redisplay (e, 1);
|
|
4998 return pri;
|
|
4999 }
|
|
5000
|
20
|
5001 DEFUN ("extent-priority", Fextent_priority, 1, 1, 0, /*
|
0
|
5002 Return the display priority of EXTENT; see `set-extent-priority'.
|
20
|
5003 */
|
|
5004 (extent))
|
0
|
5005 {
|
|
5006 EXTENT e = decode_extent (extent, 0);
|
|
5007 return make_int (extent_priority (e));
|
|
5008 }
|
|
5009
|
20
|
5010 DEFUN ("set-extent-property", Fset_extent_property, 3, 3, 0, /*
|
0
|
5011 Change a property of an extent.
|
|
5012 PROPERTY may be any symbol; the value stored may be accessed with
|
|
5013 the `extent-property' function.
|
|
5014 The following symbols have predefined meanings:
|
|
5015
|
|
5016 detached Removes the extent from its buffer; setting this is
|
|
5017 the same as calling `detach-extent'.
|
|
5018
|
|
5019 destroyed Removes the extent from its buffer, and makes it
|
|
5020 unusable in the future; this is the same calling
|
|
5021 `delete-extent'.
|
|
5022
|
|
5023 priority Change redisplay priority; same as `set-extent-priority'.
|
|
5024
|
|
5025 start-open Whether the set of characters within the extent is
|
|
5026 treated being open on the left, that is, whether
|
|
5027 the start position is an exclusive, rather than
|
|
5028 inclusive, boundary. If true, then characters
|
|
5029 inserted exactly at the beginning of the extent
|
|
5030 will remain outside of the extent; otherwise they
|
|
5031 will go into the extent, extending it.
|
|
5032
|
|
5033 end-open Whether the set of characters within the extent is
|
|
5034 treated being open on the right, that is, whether
|
|
5035 the end position is an exclusive, rather than
|
|
5036 inclusive, boundary. If true, then characters
|
|
5037 inserted exactly at the end of the extent will
|
|
5038 remain outside of the extent; otherwise they will
|
|
5039 go into the extent, extending it.
|
|
5040
|
|
5041 By default, extents have the `end-open' but not the
|
|
5042 `start-open' property set.
|
|
5043
|
|
5044 read-only Text within this extent will be unmodifiable.
|
|
5045
|
|
5046 detachable Whether the extent gets detached (as with
|
|
5047 `detach-extent') when all the text within the
|
|
5048 extent is deleted. This is true by default. If
|
|
5049 this property is not set, the extent becomes a
|
|
5050 zero-length extent when its text is deleted. (In
|
|
5051 such a case, the `start-open' property is
|
|
5052 automatically removed if both the `start-open' and
|
|
5053 `end-open' properties are set, since zero-length
|
|
5054 extents open on both ends are not allowed.)
|
|
5055
|
|
5056 face The face in which to display the text. Setting
|
|
5057 this is the same as calling `set-extent-face'.
|
|
5058
|
|
5059 mouse-face If non-nil, the extent will be highlighted in this
|
|
5060 face when the mouse moves over it.
|
|
5061
|
|
5062 pointer If non-nil, and a valid pointer glyph, this specifies
|
|
5063 the shape of the mouse pointer while over the extent.
|
|
5064
|
|
5065 highlight Obsolete: Setting this property is equivalent to
|
|
5066 setting a `mouse-face' property of `highlight'.
|
|
5067 Reading this property returns non-nil if
|
|
5068 the extent has a non-nil `mouse-face' property.
|
|
5069
|
|
5070 duplicable Whether this extent should be copied into strings,
|
|
5071 so that kill, yank, and undo commands will restore
|
|
5072 or copy it. `duplicable' extents are copied from
|
|
5073 an extent into a string when `buffer-substring' or
|
|
5074 a similar function creates a string. The extents
|
|
5075 in a string are copied into other strings created
|
|
5076 from the string using `concat' or `substring'.
|
|
5077 When `insert' or a similar function inserts the
|
|
5078 string into a buffer, the extents are copied back
|
|
5079 into the buffer.
|
|
5080
|
20
|
5081 unique Meaningful only in conjunction with `duplicable'.
|
|
5082 When this is set, there may be only one instance
|
|
5083 of this extent attached at a time: if it is copied
|
|
5084 to the kill ring and then yanked, the extent is
|
|
5085 not copied. If, however, it is killed (removed
|
|
5086 from the buffer) and then yanked, it will be
|
|
5087 re-attached at the new position.
|
0
|
5088
|
|
5089 invisible If the value is non-nil, text under this extent
|
|
5090 may be treated as not present for the purpose of
|
|
5091 redisplay, or may be displayed using an ellipsis
|
|
5092 or other marker; see `buffer-invisibility-spec'
|
|
5093 and `invisible-text-glyph'. In all cases,
|
|
5094 however, the text is still visible to other
|
|
5095 functions that examine a buffer's text.
|
|
5096
|
|
5097 keymap This keymap is consulted for mouse clicks on this
|
|
5098 extent, or keypresses made while point is within the
|
|
5099 extent.
|
|
5100
|
|
5101 copy-function This is a hook that is run when a duplicable extent
|
|
5102 is about to be copied from a buffer to a string (or
|
|
5103 the kill ring). It is called with three arguments,
|
|
5104 the extent, and the buffer-positions within it
|
|
5105 which are being copied. If this function returns
|
|
5106 nil, then the extent will not be copied; otherwise
|
|
5107 it will.
|
|
5108
|
|
5109 paste-function This is a hook that is run when a duplicable extent is
|
|
5110 about to be copied from a string (or the kill ring)
|
|
5111 into a buffer. It is called with three arguments,
|
|
5112 the original extent, and the buffer positions which
|
|
5113 the copied extent will occupy. (This hook is run
|
|
5114 after the corresponding text has already been
|
|
5115 inserted into the buffer.) Note that the extent
|
|
5116 argument may be detached when this function is run.
|
|
5117 If this function returns nil, no extent will be
|
|
5118 inserted. Otherwise, there will be an extent
|
|
5119 covering the range in question.
|
|
5120
|
|
5121 If the original extent is not attached to a buffer,
|
|
5122 then it will be re-attached at this range.
|
|
5123 Otherwise, a copy will be made, and that copy
|
|
5124 attached here.
|
|
5125
|
|
5126 The copy-function and paste-function are meaningful
|
|
5127 only for extents with the `duplicable' flag set,
|
|
5128 and if they are not specified, behave as if `t' was
|
|
5129 the returned value. When these hooks are invoked,
|
|
5130 the current buffer is the buffer which the extent
|
|
5131 is being copied from/to, respectively.
|
|
5132
|
|
5133 begin-glyph A glyph to be displayed at the beginning of the extent,
|
|
5134 or nil.
|
|
5135
|
|
5136 end-glyph A glyph to be displayed at the end of the extent,
|
|
5137 or nil.
|
|
5138
|
|
5139 begin-glyph-layout The layout policy (one of `text', `whitespace',
|
|
5140 `inside-margin', or `outside-margin') of the extent's
|
|
5141 begin glyph.
|
|
5142
|
22
|
5143 end-glyph-layout The layout policy of the extent's end glyph.
|
|
5144 */
|
20
|
5145 (extent, property, value))
|
0
|
5146 {
|
|
5147 /* This function can GC if property is `keymap' */
|
|
5148 EXTENT e = decode_extent (extent, 0);
|
|
5149 CHECK_SYMBOL (property);
|
|
5150
|
|
5151 if (EQ (property, Qread_only))
|
|
5152 set_extent_read_only (e, value);
|
|
5153 else if (EQ (property, Qunique))
|
|
5154 extent_unique_p (e) = !NILP (value);
|
|
5155 else if (EQ (property, Qduplicable))
|
|
5156 extent_duplicable_p (e) = !NILP (value);
|
|
5157 else if (EQ (property, Qinvisible))
|
|
5158 set_extent_invisible (e, value);
|
|
5159 else if (EQ (property, Qdetachable))
|
|
5160 extent_detachable_p (e) = !NILP (value);
|
|
5161
|
|
5162 else if (EQ (property, Qdetached))
|
|
5163 {
|
|
5164 if (NILP (value))
|
|
5165 error ("can only set `detached' to t");
|
|
5166 Fdetach_extent (extent);
|
|
5167 }
|
|
5168 else if (EQ (property, Qdestroyed))
|
|
5169 {
|
|
5170 if (NILP (value))
|
|
5171 error ("can only set `destroyed' to t");
|
|
5172 Fdelete_extent (extent);
|
|
5173 }
|
|
5174 else if (EQ (property, Qpriority))
|
|
5175 Fset_extent_priority (extent, value);
|
|
5176 else if (EQ (property, Qface))
|
|
5177 Fset_extent_face (extent, value);
|
|
5178 else if (EQ (property, Qmouse_face))
|
|
5179 Fset_extent_mouse_face (extent, value);
|
|
5180 /* Obsolete: */
|
|
5181 else if (EQ (property, Qhighlight))
|
|
5182 Fset_extent_mouse_face (extent, Qhighlight);
|
|
5183 else if (EQ (property, Qbegin_glyph_layout))
|
|
5184 Fset_extent_begin_glyph_layout (extent, value);
|
|
5185 else if (EQ (property, Qend_glyph_layout))
|
|
5186 Fset_extent_end_glyph_layout (extent, value);
|
|
5187 /* For backwards compatibility. We use begin glyph because it is by
|
|
5188 far the more used of the two. */
|
|
5189 else if (EQ (property, Qglyph_layout))
|
|
5190 Fset_extent_begin_glyph_layout (extent, value);
|
|
5191 else if (EQ (property, Qbegin_glyph))
|
|
5192 Fset_extent_begin_glyph (extent, value, Qnil);
|
|
5193 else if (EQ (property, Qend_glyph))
|
|
5194 Fset_extent_end_glyph (extent, value, Qnil);
|
|
5195 else if (EQ (property, Qstart_open) ||
|
|
5196 EQ (property, Qend_open) ||
|
|
5197 EQ (property, Qstart_closed) ||
|
|
5198 EQ (property, Qend_closed))
|
|
5199 {
|
|
5200 int start_open = -1, end_open = -1;
|
|
5201 if (EQ (property, Qstart_open))
|
|
5202 start_open = !NILP (value);
|
|
5203 else if (EQ (property, Qend_open))
|
|
5204 end_open = !NILP (value);
|
|
5205 /* Support (but don't document...) the obvious antonyms. */
|
|
5206 else if (EQ (property, Qstart_closed))
|
|
5207 start_open = NILP (value);
|
|
5208 else
|
|
5209 end_open = NILP (value);
|
|
5210 set_extent_openness (e, start_open, end_open);
|
|
5211 }
|
|
5212 else
|
|
5213 {
|
|
5214 if (EQ (property, Qkeymap))
|
|
5215 while (NILP (Fkeymapp (value)))
|
|
5216 value = wrong_type_argument (Qkeymapp, value);
|
|
5217
|
|
5218 external_plist_put (extent_plist_addr (e), property, value, 0, ERROR_ME);
|
|
5219 }
|
|
5220
|
|
5221 return value;
|
|
5222 }
|
|
5223
|
20
|
5224 DEFUN ("extent-property", Fextent_property, 2, 3, 0, /*
|
0
|
5225 Return EXTENT's value for property PROPERTY.
|
|
5226 See `set-extent-property' for the built-in property names.
|
20
|
5227 */
|
|
5228 (extent, property, defalt))
|
0
|
5229 {
|
|
5230 EXTENT e = decode_extent (extent, 0);
|
|
5231 CHECK_SYMBOL (property);
|
|
5232
|
|
5233 if (EQ (property, Qdetached))
|
|
5234 return (extent_detached_p (e) ? Qt : Qnil);
|
|
5235 else if (EQ (property, Qdestroyed))
|
|
5236 return (!EXTENT_LIVE_P (e) ? Qt : Qnil);
|
|
5237 #define RETURN_FLAG(flag) \
|
|
5238 return (extent_normal_field (e, flag) ? Qt : Qnil)
|
|
5239 else if (EQ (property, Qstart_open)) RETURN_FLAG (start_open);
|
|
5240 else if (EQ (property, Qend_open)) RETURN_FLAG (end_open);
|
|
5241 else if (EQ (property, Qunique)) RETURN_FLAG (unique);
|
|
5242 else if (EQ (property, Qduplicable)) RETURN_FLAG (duplicable);
|
|
5243 else if (EQ (property, Qdetachable)) RETURN_FLAG (detachable);
|
|
5244 #undef RETURN_FLAG
|
|
5245 /* Support (but don't document...) the obvious antonyms. */
|
|
5246 else if (EQ (property, Qstart_closed))
|
|
5247 return (extent_start_open_p (e) ? Qnil : Qt);
|
|
5248 else if (EQ (property, Qend_closed))
|
|
5249 return (extent_end_open_p (e) ? Qnil : Qt);
|
|
5250 else if (EQ (property, Qpriority))
|
|
5251 return make_int (extent_priority (e));
|
|
5252 else if (EQ (property, Qread_only))
|
|
5253 return extent_read_only (e);
|
|
5254 else if (EQ (property, Qinvisible))
|
|
5255 return extent_invisible (e);
|
|
5256 else if (EQ (property, Qface))
|
|
5257 return Fextent_face (extent);
|
|
5258 else if (EQ (property, Qmouse_face))
|
|
5259 return Fextent_mouse_face (extent);
|
|
5260 /* Obsolete: */
|
|
5261 else if (EQ (property, Qhighlight))
|
|
5262 return !NILP (Fextent_mouse_face (extent)) ? Qt : Qnil;
|
|
5263 else if (EQ (property, Qbegin_glyph_layout))
|
|
5264 return Fextent_begin_glyph_layout (extent);
|
|
5265 else if (EQ (property, Qend_glyph_layout))
|
|
5266 return Fextent_end_glyph_layout (extent);
|
|
5267 /* For backwards compatibility. We use begin glyph because it is by
|
|
5268 far the more used of the two. */
|
|
5269 else if (EQ (property, Qglyph_layout))
|
|
5270 return Fextent_begin_glyph_layout (extent);
|
|
5271 else if (EQ (property, Qbegin_glyph))
|
|
5272 return extent_begin_glyph (e);
|
|
5273 else if (EQ (property, Qend_glyph))
|
|
5274 return extent_end_glyph (e);
|
|
5275 else
|
|
5276 {
|
|
5277 Lisp_Object value;
|
|
5278
|
|
5279 value = external_plist_get (extent_plist_addr (e), property, 0,
|
|
5280 ERROR_ME);
|
|
5281 if (UNBOUNDP (value))
|
|
5282 return defalt;
|
|
5283 return value;
|
|
5284 }
|
|
5285 }
|
|
5286
|
20
|
5287 DEFUN ("extent-properties", Fextent_properties, 1, 1, 0, /*
|
0
|
5288 Return a property list of the attributes of the given extent.
|
|
5289 Do not modify this list; use `set-extent-property' instead.
|
20
|
5290 */
|
|
5291 (extent))
|
0
|
5292 {
|
|
5293 EXTENT e, anc;
|
|
5294 Lisp_Object result, face, anc_obj = Qnil;
|
|
5295
|
|
5296 CHECK_EXTENT (extent);
|
|
5297 e = XEXTENT (extent);
|
|
5298 if (!EXTENT_LIVE_P (e))
|
|
5299 return Fcons (Qdestroyed, Fcons (Qt, Qnil));
|
|
5300
|
|
5301 anc = extent_ancestor (e);
|
|
5302 XSETEXTENT (anc_obj, anc);
|
|
5303
|
|
5304 /* For efficiency, use the ancestor for all properties except detached */
|
|
5305
|
|
5306 result = extent_plist_slot (anc);
|
|
5307 face = Fextent_face (anc_obj);
|
|
5308 if (!NILP (face))
|
|
5309 result = Fcons (Qface, Fcons (face, result));
|
|
5310 face = Fextent_mouse_face (anc_obj);
|
|
5311 if (!NILP (face))
|
|
5312 result = Fcons (Qmouse_face, Fcons (face, result));
|
|
5313
|
|
5314 /* For now continue to include this for backwards compatibility. */
|
|
5315 if (extent_begin_glyph_layout (anc) != GL_TEXT)
|
|
5316 result = Fcons (Qglyph_layout,
|
|
5317 glyph_layout_to_symbol (extent_begin_glyph_layout (anc)));
|
|
5318
|
|
5319 if (extent_begin_glyph_layout (anc) != GL_TEXT)
|
|
5320 result = Fcons (Qbegin_glyph_layout,
|
|
5321 glyph_layout_to_symbol (extent_begin_glyph_layout (anc)));
|
|
5322 if (extent_end_glyph_layout (anc) != GL_TEXT)
|
|
5323 result = Fcons (Qend_glyph_layout,
|
|
5324 glyph_layout_to_symbol (extent_end_glyph_layout (anc)));
|
|
5325
|
|
5326 if (!NILP (extent_end_glyph (anc)))
|
|
5327 result = Fcons (Qend_glyph, Fcons (extent_end_glyph (anc), result));
|
|
5328 if (!NILP (extent_begin_glyph (anc)))
|
|
5329 result = Fcons (Qbegin_glyph, Fcons (extent_begin_glyph (anc), result));
|
|
5330
|
|
5331 if (extent_priority (anc) != 0)
|
|
5332 result = Fcons (Qpriority, Fcons (make_int (extent_priority (anc)),
|
|
5333 result));
|
|
5334
|
|
5335 if (!NILP (extent_invisible (anc)))
|
|
5336 result = Fcons (Qinvisible, Fcons (extent_invisible (anc), result));
|
|
5337
|
|
5338 if (!NILP (extent_read_only (anc)))
|
|
5339 result = Fcons (Qread_only, Fcons (extent_read_only (anc), result));
|
|
5340
|
|
5341 #define CONS_FLAG(flag, sym) if (extent_normal_field (anc, flag)) \
|
|
5342 result = Fcons (sym, Fcons (Qt, result))
|
|
5343 CONS_FLAG (end_open, Qend_open);
|
|
5344 CONS_FLAG (start_open, Qstart_open);
|
|
5345 CONS_FLAG (detachable, Qdetachable);
|
|
5346 CONS_FLAG (duplicable, Qduplicable);
|
|
5347 CONS_FLAG (unique, Qunique);
|
|
5348 #undef CONS_FLAG
|
|
5349
|
|
5350 /* detached is not an inherited property */
|
|
5351 if (extent_detached_p (e))
|
|
5352 result = Fcons (Qdetached, Fcons (Qt, result));
|
|
5353
|
|
5354 return result;
|
|
5355 }
|
|
5356
|
|
5357
|
|
5358 /************************************************************************/
|
|
5359 /* highlighting */
|
|
5360 /************************************************************************/
|
|
5361
|
|
5362 /* The display code looks into the Vlast_highlighted_extent variable to
|
|
5363 correctly display highlighted extents. This updates that variable,
|
|
5364 and marks the appropriate buffers as needing some redisplay.
|
|
5365 */
|
|
5366 static void
|
|
5367 do_highlight (Lisp_Object extent_obj, int highlight_p)
|
|
5368 {
|
|
5369 if (( highlight_p && (EQ (Vlast_highlighted_extent, extent_obj))) ||
|
|
5370 (!highlight_p && (EQ (Vlast_highlighted_extent, Qnil))))
|
|
5371 return;
|
|
5372 if (EXTENTP (Vlast_highlighted_extent) &&
|
|
5373 EXTENT_LIVE_P (XEXTENT (Vlast_highlighted_extent)))
|
|
5374 {
|
|
5375 /* do not recurse on descendants. Only one extent is highlighted
|
|
5376 at a time. */
|
|
5377 extent_changed_for_redisplay (XEXTENT (Vlast_highlighted_extent), 0);
|
|
5378 }
|
|
5379 Vlast_highlighted_extent = Qnil;
|
|
5380 if (!NILP (extent_obj)
|
|
5381 && BUFFERP (extent_object (XEXTENT (extent_obj)))
|
|
5382 && highlight_p)
|
|
5383 {
|
|
5384 extent_changed_for_redisplay (XEXTENT (extent_obj), 0);
|
|
5385 Vlast_highlighted_extent = extent_obj;
|
|
5386 }
|
|
5387 }
|
|
5388
|
20
|
5389 DEFUN ("force-highlight-extent", Fforce_highlight_extent, 1, 2, 0, /*
|
0
|
5390 Highlight or unhighlight the given extent.
|
|
5391 If the second arg is non-nil, it will be highlighted, else dehighlighted.
|
|
5392 This is the same as `highlight-extent', except that it will work even
|
|
5393 on extents without the `mouse-face' property.
|
20
|
5394 */
|
|
5395 (extent_obj, highlight_p))
|
0
|
5396 {
|
|
5397 if (NILP (extent_obj))
|
|
5398 highlight_p = Qnil;
|
|
5399 else
|
|
5400 XSETEXTENT (extent_obj, decode_extent (extent_obj, DE_MUST_BE_ATTACHED));
|
|
5401 do_highlight (extent_obj, !NILP (highlight_p));
|
|
5402 return Qnil;
|
|
5403 }
|
|
5404
|
20
|
5405 DEFUN ("highlight-extent", Fhighlight_extent, 1, 2, 0, /*
|
0
|
5406 Highlight the given extent, if it is highlightable
|
|
5407 \(that is, if it has the `mouse-face' property).
|
|
5408 If the second arg is non-nil, it will be highlighted, else dehighlighted.
|
|
5409 Highlighted extents are displayed as if they were merged with the face
|
|
5410 or faces specified by the `mouse-face' property.
|
20
|
5411 */
|
|
5412 (extent_obj, highlight_p))
|
0
|
5413 {
|
|
5414 if (EXTENTP (extent_obj) && NILP (extent_mouse_face (XEXTENT (extent_obj))))
|
|
5415 return Qnil;
|
|
5416 else
|
|
5417 return (Fforce_highlight_extent (extent_obj, highlight_p));
|
|
5418 }
|
|
5419
|
|
5420
|
|
5421 /************************************************************************/
|
|
5422 /* strings and extents */
|
|
5423 /************************************************************************/
|
|
5424
|
|
5425 /* copy/paste hooks */
|
|
5426
|
|
5427 static int
|
|
5428 run_extent_copy_paste_internal (EXTENT e, Bufpos from, Bufpos to,
|
|
5429 Lisp_Object object,
|
|
5430 Lisp_Object prop)
|
|
5431 {
|
|
5432 /* This function can GC */
|
|
5433 Lisp_Object extent;
|
|
5434 Lisp_Object copy_fn;
|
|
5435 XSETEXTENT (extent, e);
|
|
5436 copy_fn = Fextent_property (extent, prop, Qnil);
|
|
5437 if (!NILP (copy_fn))
|
|
5438 {
|
|
5439 Lisp_Object flag;
|
|
5440 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
5441 GCPRO3 (extent, copy_fn, object);
|
|
5442 if (BUFFERP (object))
|
|
5443 flag = call3_in_buffer (XBUFFER (object), copy_fn, extent,
|
|
5444 make_int (from), make_int (to));
|
|
5445 else
|
|
5446 flag = call3 (copy_fn, extent, make_int (from), make_int (to));
|
|
5447 UNGCPRO;
|
|
5448 if (NILP (flag) || !EXTENT_LIVE_P (XEXTENT (extent)))
|
|
5449 return 0;
|
|
5450 }
|
|
5451 return 1;
|
|
5452 }
|
|
5453
|
|
5454 static int
|
|
5455 run_extent_copy_function (EXTENT e, Bytind from, Bytind to)
|
|
5456 {
|
|
5457 Lisp_Object object = extent_object (e);
|
|
5458 /* This function can GC */
|
|
5459 return run_extent_copy_paste_internal
|
|
5460 (e, buffer_or_string_bytind_to_bufpos (object, from),
|
|
5461 buffer_or_string_bytind_to_bufpos (object, to), object,
|
|
5462 Qcopy_function);
|
|
5463 }
|
|
5464
|
|
5465 static int
|
|
5466 run_extent_paste_function (EXTENT e, Bytind from, Bytind to,
|
|
5467 Lisp_Object object)
|
|
5468 {
|
|
5469 /* This function can GC */
|
|
5470 return run_extent_copy_paste_internal
|
|
5471 (e, buffer_or_string_bytind_to_bufpos (object, from),
|
|
5472 buffer_or_string_bytind_to_bufpos (object, to), object,
|
|
5473 Qpaste_function);
|
|
5474 }
|
|
5475
|
|
5476 static void
|
|
5477 update_extent (EXTENT extent, Bytind from, Bytind to)
|
|
5478 {
|
|
5479 set_extent_endpoints (extent, from, to, Qnil);
|
|
5480 /* #### remove this crap */
|
|
5481 #ifdef ENERGIZE
|
|
5482 restore_energize_extent_state (extent);
|
|
5483 #endif
|
|
5484 }
|
|
5485
|
|
5486 /* Insert an extent, usually from the dup_list of a string which
|
|
5487 has just been inserted.
|
|
5488 This code does not handle the case of undo.
|
|
5489 */
|
|
5490 static Lisp_Object
|
|
5491 insert_extent (EXTENT extent, Bytind new_start, Bytind new_end,
|
|
5492 Lisp_Object object, int run_hooks)
|
|
5493 {
|
|
5494 /* This function can GC */
|
|
5495 Lisp_Object tmp;
|
|
5496
|
|
5497 if (!EQ (extent_object (extent), object))
|
|
5498 goto copy_it;
|
|
5499
|
|
5500 if (extent_detached_p (extent))
|
|
5501 {
|
|
5502 if (run_hooks &&
|
|
5503 !run_extent_paste_function (extent, new_start, new_end, object))
|
|
5504 /* The paste-function said don't re-attach this extent here. */
|
|
5505 return Qnil;
|
|
5506 else
|
|
5507 update_extent (extent, new_start, new_end);
|
|
5508 }
|
|
5509 else
|
|
5510 {
|
|
5511 Bytind exstart = extent_endpoint_bytind (extent, 0);
|
|
5512 Bytind exend = extent_endpoint_bytind (extent, 1);
|
|
5513
|
|
5514 if (exend < new_start || exstart > new_end)
|
|
5515 goto copy_it;
|
|
5516 else
|
|
5517 {
|
|
5518 new_start = min (exstart, new_start);
|
|
5519 new_end = max (exend, new_end);
|
|
5520 if (exstart != new_start || exend != new_end)
|
|
5521 update_extent (extent, new_start, new_end);
|
|
5522 }
|
|
5523 }
|
|
5524
|
|
5525 XSETEXTENT (tmp, extent);
|
|
5526 return tmp;
|
|
5527
|
|
5528 copy_it:
|
|
5529 if (run_hooks &&
|
|
5530 !run_extent_paste_function (extent, new_start, new_end, object))
|
|
5531 /* The paste-function said don't attach a copy of the extent here. */
|
|
5532 return Qnil;
|
|
5533 else
|
|
5534 {
|
|
5535 XSETEXTENT (tmp, copy_extent (extent, new_start, new_end, object));
|
|
5536 return tmp;
|
|
5537 }
|
|
5538 }
|
|
5539
|
20
|
5540 DEFUN ("insert-extent", Finsert_extent, 1, 5, 0, /*
|
0
|
5541 Insert EXTENT from START to END in BUFFER-OR-STRING.
|
|
5542 BUFFER-OR-STRING defaults to the current buffer if omitted.
|
|
5543 This operation does not insert any characters,
|
|
5544 but otherwise acts as if there were a replicating extent whose
|
|
5545 parent is EXTENT in some string that was just inserted.
|
|
5546 Returns the newly-inserted extent.
|
|
5547 The fourth arg, NO-HOOKS, can be used to inhibit the running of the
|
|
5548 extent's `paste-function' property if it has one.
|
|
5549 See documentation on `detach-extent' for a discussion of undo recording.
|
20
|
5550 */
|
|
5551 (extent, start, end, no_hooks, buffer_or_string))
|
0
|
5552 {
|
|
5553 EXTENT ext = decode_extent (extent, 0);
|
|
5554 Lisp_Object copy;
|
|
5555 Bytind s, e;
|
|
5556
|
|
5557 buffer_or_string = decode_buffer_or_string (buffer_or_string);
|
|
5558 get_buffer_or_string_range_byte (buffer_or_string, start, end, &s, &e,
|
|
5559 GB_ALLOW_PAST_ACCESSIBLE);
|
|
5560
|
|
5561 copy = insert_extent (ext, s, e, buffer_or_string, NILP (no_hooks));
|
|
5562 if (EXTENTP (copy))
|
|
5563 {
|
|
5564 if (extent_duplicable_p (XEXTENT (copy)))
|
|
5565 record_extent (copy, 1);
|
|
5566 }
|
|
5567 return copy;
|
|
5568 }
|
|
5569
|
|
5570
|
|
5571 /* adding buffer extents to a string */
|
|
5572
|
|
5573 struct add_string_extents_arg
|
|
5574 {
|
|
5575 Bytind from;
|
|
5576 Bytecount length;
|
|
5577 Lisp_Object string;
|
|
5578 };
|
|
5579
|
|
5580 static int
|
|
5581 add_string_extents_mapper (EXTENT extent, void *arg)
|
|
5582 {
|
|
5583 /* This function can GC */
|
|
5584 struct add_string_extents_arg *closure =
|
|
5585 (struct add_string_extents_arg *) arg;
|
|
5586 Bytecount start = extent_endpoint_bytind (extent, 0) - closure->from;
|
|
5587 Bytecount end = extent_endpoint_bytind (extent, 1) - closure->from;
|
|
5588
|
|
5589 if (extent_duplicable_p (extent))
|
|
5590 {
|
|
5591 EXTENT e;
|
|
5592
|
|
5593 start = max (start, 0);
|
|
5594 end = min (end, closure->length);
|
|
5595
|
|
5596 /* Run the copy-function to give an extent the option of
|
|
5597 not being copied into the string (or kill ring).
|
|
5598 */
|
|
5599 if (extent_duplicable_p (extent) &&
|
|
5600 !run_extent_copy_function (extent, start + closure->from,
|
|
5601 end + closure->from))
|
|
5602 return 0;
|
|
5603 e = copy_extent (extent, start, end, closure->string);
|
|
5604 }
|
|
5605
|
|
5606 return 0;
|
|
5607 }
|
|
5608
|
|
5609 /* Add the extents in buffer BUF from OPOINT to OPOINT+LENGTH to
|
|
5610 the string STRING. */
|
|
5611 void
|
|
5612 add_string_extents (Lisp_Object string, struct buffer *buf, Bytind opoint,
|
|
5613 Bytecount length)
|
|
5614 {
|
|
5615 /* This function can GC */
|
|
5616 struct add_string_extents_arg closure;
|
|
5617 struct gcpro gcpro1, gcpro2;
|
|
5618 Lisp_Object buffer;
|
|
5619
|
|
5620 closure.from = opoint;
|
|
5621 closure.length = length;
|
|
5622 closure.string = string;
|
|
5623 buffer = make_buffer (buf);
|
|
5624 GCPRO2 (buffer, string);
|
|
5625 map_extents_bytind (opoint, opoint + length, add_string_extents_mapper,
|
|
5626 (void *) &closure, buffer, 0,
|
|
5627 /* ignore extents that just abut the region */
|
|
5628 ME_END_CLOSED | ME_ALL_EXTENTS_OPEN |
|
|
5629 /* we are calling E-Lisp (the extent's copy function)
|
|
5630 so anything might happen */
|
|
5631 ME_MIGHT_CALL_ELISP);
|
|
5632 UNGCPRO;
|
|
5633 }
|
|
5634
|
|
5635 struct splice_in_string_extents_arg
|
|
5636 {
|
|
5637 Bytecount pos;
|
|
5638 Bytecount length;
|
|
5639 Bytind opoint;
|
|
5640 Lisp_Object buffer;
|
|
5641 };
|
|
5642
|
|
5643 static int
|
|
5644 splice_in_string_extents_mapper (EXTENT extent, void *arg)
|
|
5645 {
|
|
5646 /* This function can GC */
|
|
5647 struct splice_in_string_extents_arg *closure =
|
|
5648 (struct splice_in_string_extents_arg *) arg;
|
|
5649 /* BASE_START and BASE_END are the limits in the buffer of the string
|
|
5650 that was just inserted.
|
|
5651
|
|
5652 NEW_START and NEW_END are the prospective buffer positions of the
|
|
5653 extent that is going into the buffer. */
|
|
5654 Bytind base_start = closure->opoint;
|
|
5655 Bytind base_end = base_start + closure->length;
|
|
5656 Bytind new_start = (base_start + extent_endpoint_bytind (extent, 0) -
|
|
5657 closure->pos);
|
|
5658 Bytind new_end = (base_start + extent_endpoint_bytind (extent, 1) -
|
|
5659 closure->pos);
|
|
5660
|
|
5661 if (new_start < base_start)
|
|
5662 new_start = base_start;
|
|
5663 if (new_end > base_end)
|
|
5664 new_end = base_end;
|
|
5665 if (new_end <= new_start)
|
|
5666 return 0;
|
|
5667
|
|
5668 if (!extent_duplicable_p (extent))
|
|
5669 return 0;
|
|
5670
|
20
|
5671 if (!inside_undo &&
|
|
5672 !run_extent_paste_function (extent, new_start, new_end,
|
|
5673 closure->buffer))
|
|
5674 return 0;
|
|
5675 copy_extent (extent, new_start, new_end, closure->buffer);
|
|
5676
|
0
|
5677 return 0;
|
|
5678 }
|
|
5679
|
|
5680 /* We have just inserted a section of STRING (starting at POS, of
|
|
5681 length LENGTH) into buffer BUF at OPOINT. Do whatever is necessary
|
|
5682 to get the string's extents into the buffer. */
|
|
5683
|
|
5684 void
|
|
5685 splice_in_string_extents (Lisp_Object string, struct buffer *buf,
|
|
5686 Bytind opoint, Bytecount length, Bytecount pos)
|
|
5687 {
|
|
5688 struct splice_in_string_extents_arg closure;
|
|
5689 struct gcpro gcpro1, gcpro2;
|
|
5690 Lisp_Object buffer;
|
|
5691
|
|
5692 buffer = make_buffer (buf);
|
|
5693 closure.opoint = opoint;
|
|
5694 closure.pos = pos;
|
|
5695 closure.length = length;
|
|
5696 closure.buffer = buffer;
|
|
5697 GCPRO2 (buffer, string);
|
|
5698 map_extents_bytind (pos, pos + length,
|
|
5699 splice_in_string_extents_mapper,
|
|
5700 (void *) &closure, string, 0,
|
|
5701 /* ignore extents that just abut the region */
|
|
5702 ME_END_CLOSED | ME_ALL_EXTENTS_OPEN |
|
|
5703 /* we are calling E-Lisp (the extent's copy function)
|
|
5704 so anything might happen */
|
|
5705 ME_MIGHT_CALL_ELISP);
|
|
5706 UNGCPRO;
|
|
5707 }
|
|
5708
|
|
5709 struct copy_string_extents_arg
|
|
5710 {
|
|
5711 Bytecount new_pos;
|
|
5712 Bytecount old_pos;
|
|
5713 Bytecount length;
|
|
5714 Lisp_Object new_string;
|
|
5715 };
|
|
5716
|
|
5717 struct copy_string_extents_1_arg
|
|
5718 {
|
|
5719 Lisp_Object parent_in_question;
|
|
5720 EXTENT found_extent;
|
|
5721 };
|
|
5722
|
|
5723 static int
|
|
5724 copy_string_extents_1_mapper (EXTENT extent, void *arg)
|
|
5725 {
|
|
5726 struct copy_string_extents_1_arg *closure =
|
|
5727 (struct copy_string_extents_1_arg *) arg;
|
|
5728
|
|
5729 return 0;
|
|
5730 }
|
|
5731
|
|
5732 static int
|
|
5733 copy_string_extents_mapper (EXTENT extent, void *arg)
|
|
5734 {
|
|
5735 struct copy_string_extents_arg *closure =
|
|
5736 (struct copy_string_extents_arg *) arg;
|
|
5737 Bytecount old_start, old_end;
|
|
5738 Bytecount new_start, new_end;
|
|
5739
|
|
5740 old_start = extent_endpoint_bytind (extent, 0);
|
|
5741 old_end = extent_endpoint_bytind (extent, 1);
|
|
5742
|
|
5743 old_start = max (closure->old_pos, old_start);
|
|
5744 old_end = min (closure->old_pos + closure->length, old_end);
|
|
5745
|
|
5746 if (old_start >= old_end)
|
|
5747 return 0;
|
|
5748
|
|
5749 new_start = old_start + closure->new_pos - closure->old_pos;
|
|
5750 new_end = old_end + closure->new_pos - closure->old_pos;
|
|
5751
|
|
5752 copy_extent (extent,
|
|
5753 old_start + closure->new_pos - closure->old_pos,
|
|
5754 old_end + closure->new_pos - closure->old_pos,
|
|
5755 closure->new_string);
|
|
5756 return 0;
|
|
5757 }
|
|
5758
|
|
5759 /* The string NEW_STRING was partially constructed from OLD_STRING.
|
|
5760 In particular, the section of length LEN starting at NEW_POS in
|
|
5761 NEW_STRING came from the section of the same length starting at
|
|
5762 OLD_POS in OLD_STRING. Copy the extents as appropriate. */
|
|
5763
|
|
5764 void
|
|
5765 copy_string_extents (Lisp_Object new_string, Lisp_Object old_string,
|
|
5766 Bytecount new_pos, Bytecount old_pos,
|
|
5767 Bytecount length)
|
|
5768 {
|
|
5769 struct copy_string_extents_arg closure;
|
|
5770 struct gcpro gcpro1, gcpro2;
|
|
5771
|
|
5772 closure.new_pos = new_pos;
|
|
5773 closure.old_pos = old_pos;
|
|
5774 closure.new_string = new_string;
|
|
5775 closure.length = length;
|
|
5776 GCPRO2 (new_string, old_string);
|
|
5777 map_extents_bytind (old_pos, old_pos + length,
|
|
5778 copy_string_extents_mapper,
|
|
5779 (void *) &closure, old_string, 0,
|
|
5780 /* ignore extents that just abut the region */
|
|
5781 ME_END_CLOSED | ME_ALL_EXTENTS_OPEN |
|
|
5782 /* we are calling E-Lisp (the extent's copy function)
|
|
5783 so anything might happen */
|
|
5784 ME_MIGHT_CALL_ELISP);
|
|
5785 UNGCPRO;
|
|
5786 }
|
|
5787
|
|
5788 /* Checklist for sanity checking:
|
|
5789 - {kill, yank, copy} at {open, closed} {start, end} of {writable, read-only} extent
|
|
5790 - {kill, copy} & yank {once, repeatedly} duplicable extent in {same, different} buffer
|
|
5791 */
|
|
5792
|
|
5793
|
|
5794 /************************************************************************/
|
|
5795 /* text properties */
|
|
5796 /************************************************************************/
|
|
5797
|
|
5798 /* Text properties
|
|
5799 Originally this stuff was implemented in lisp (all of the functionality
|
|
5800 exists to make that possible) but speed was a problem.
|
|
5801 */
|
|
5802
|
|
5803 Lisp_Object Qtext_prop;
|
|
5804 Lisp_Object Qtext_prop_extent_paste_function;
|
|
5805
|
|
5806 static Lisp_Object
|
|
5807 get_text_property_bytind (Bytind position, Lisp_Object prop,
|
|
5808 Lisp_Object object, enum extent_at_flag fl,
|
|
5809 int text_props_only)
|
|
5810 {
|
|
5811 Lisp_Object extent;
|
|
5812
|
|
5813 /* text_props_only specifies whether we only consider text-property
|
|
5814 extents (those with the 'text-prop property set) or all extents. */
|
|
5815 if (!text_props_only)
|
|
5816 extent = extent_at_bytind (position, object, prop, 0, fl);
|
|
5817 else
|
|
5818 {
|
|
5819 EXTENT prior = 0;
|
|
5820 while (1)
|
|
5821 {
|
|
5822 extent = extent_at_bytind (position, object, Qtext_prop, prior,
|
|
5823 fl);
|
|
5824 if (NILP (extent))
|
|
5825 return Qnil;
|
|
5826 if (EQ (prop, Fextent_property (extent, Qtext_prop, Qnil)))
|
|
5827 break;
|
|
5828 prior = XEXTENT (extent);
|
|
5829 }
|
|
5830 }
|
|
5831
|
|
5832 if (!NILP (extent))
|
|
5833 return Fextent_property (extent, prop, Qnil);
|
|
5834 if (!NILP (Vdefault_text_properties))
|
|
5835 return Fplist_get (Vdefault_text_properties, prop, Qnil);
|
|
5836 return Qnil;
|
|
5837 }
|
|
5838
|
|
5839 static Lisp_Object
|
|
5840 get_text_property_1 (Lisp_Object pos, Lisp_Object prop, Lisp_Object object,
|
|
5841 Lisp_Object at_flag, int text_props_only)
|
|
5842 {
|
|
5843 Bytind position;
|
|
5844 int invert = 0;
|
|
5845
|
|
5846 object = decode_buffer_or_string (object);
|
|
5847 position = get_buffer_or_string_pos_byte (object, pos, GB_NO_ERROR_IF_BAD);
|
|
5848 CHECK_SYMBOL (prop);
|
|
5849
|
|
5850 /* We canonicalize the start/end-open/closed properties to the
|
|
5851 non-default version -- "adding" the default property really
|
|
5852 needs to remove the non-default one. See below for more
|
|
5853 on this. */
|
|
5854 if (EQ (prop, Qstart_closed))
|
|
5855 {
|
|
5856 prop = Qstart_open;
|
|
5857 invert = 1;
|
|
5858 }
|
|
5859
|
|
5860 if (EQ (prop, Qend_open))
|
|
5861 {
|
|
5862 prop = Qend_closed;
|
|
5863 invert = 1;
|
|
5864 }
|
|
5865
|
|
5866 {
|
|
5867 Lisp_Object val =
|
|
5868 get_text_property_bytind (position, prop, object,
|
|
5869 decode_extent_at_flag (at_flag),
|
|
5870 text_props_only);
|
|
5871 if (invert)
|
|
5872 val = NILP (val) ? Qt : Qnil;
|
|
5873 return val;
|
|
5874 }
|
|
5875 }
|
|
5876
|
20
|
5877 DEFUN ("get-text-property", Fget_text_property, 2, 4, 0, /*
|
0
|
5878 Returns the value of the PROP property at the given position.
|
|
5879 Optional arg OBJECT specifies the buffer or string to look in, and
|
|
5880 defaults to the current buffer.
|
|
5881 Optional arg AT-FLAG controls what it means for a property to be \"at\"
|
|
5882 a position, and has the same meaning as in `extent-at'.
|
|
5883 This examines only those properties added with `put-text-property'.
|
|
5884 See also `get-char-property'.
|
20
|
5885 */
|
|
5886 (pos, prop, object, at_flag))
|
0
|
5887 {
|
|
5888 return get_text_property_1 (pos, prop, object, at_flag, 1);
|
|
5889 }
|
|
5890
|
20
|
5891 DEFUN ("get-char-property", Fget_char_property, 2, 4, 0, /*
|
0
|
5892 Returns the value of the PROP property at the given position.
|
|
5893 Optional arg OBJECT specifies the buffer or string to look in, and
|
|
5894 defaults to the current buffer.
|
|
5895 Optional arg AT-FLAG controls what it means for a property to be \"at\"
|
|
5896 a position, and has the same meaning as in `extent-at'.
|
|
5897 This examines properties on all extents.
|
|
5898 See also `get-text-property'.
|
20
|
5899 */
|
|
5900 (pos, prop, object, at_flag))
|
0
|
5901 {
|
|
5902 return get_text_property_1 (pos, prop, object, at_flag, 0);
|
|
5903 }
|
|
5904
|
|
5905 /* About start/end-open/closed:
|
|
5906
|
|
5907 These properties have to be handled specially because of their
|
|
5908 strange behavior. If I put the "start-open" property on a region,
|
|
5909 then *all* text-property extents in the region have to have their
|
|
5910 start be open. This is unlike all other properties, which don't
|
|
5911 affect the extents of text properties other than their own.
|
|
5912
|
|
5913 So:
|
|
5914
|
|
5915 1) We have to map start-closed to (not start-open) and end-open
|
|
5916 to (not end-closed) -- i.e. adding the default is really the
|
|
5917 same as remove the non-default property. It won't work, for
|
|
5918 example, to have both "start-open" and "start-closed" on
|
|
5919 the same region.
|
|
5920 2) Whenever we add one of these properties, we go through all
|
|
5921 text-property extents in the region and set the appropriate
|
|
5922 open/closedness on them.
|
|
5923 3) Whenever we change a text-property extent for a property,
|
|
5924 we have to make sure we set the open/closedness properly.
|
|
5925
|
|
5926 (2) and (3) together rely on, and maintain, the invariant
|
|
5927 that the open/closedness of text-property extents is correct
|
|
5928 at the beginning and end of each operation.
|
|
5929 */
|
|
5930
|
|
5931 struct put_text_prop_arg
|
|
5932 {
|
|
5933 Lisp_Object prop, value; /* The property and value we are storing */
|
|
5934 Bytind start, end; /* The region into which we are storing it */
|
|
5935 Lisp_Object object;
|
|
5936 Lisp_Object the_extent; /* Our chosen extent; this is used for
|
|
5937 communication between subsequent passes. */
|
|
5938 int changed_p; /* Output: whether we have modified anything */
|
|
5939 };
|
|
5940
|
|
5941 static int
|
|
5942 put_text_prop_mapper (EXTENT e, void *arg)
|
|
5943 {
|
|
5944 struct put_text_prop_arg *closure = (struct put_text_prop_arg *) arg;
|
|
5945
|
|
5946 Lisp_Object object = closure->object;
|
|
5947 Lisp_Object value = closure->value;
|
|
5948 Bytind e_start, e_end;
|
|
5949 Bytind start = closure->start;
|
|
5950 Bytind end = closure->end;
|
|
5951 Lisp_Object extent, e_val;
|
|
5952 int is_eq;
|
|
5953
|
|
5954 XSETEXTENT (extent, e);
|
|
5955
|
|
5956 /* Note: in some cases when the property itself is 'start-open
|
|
5957 or 'end-closed, the checks to set the openness may do a bit
|
|
5958 of extra work; but it won't hurt because we then fix up the
|
2
|
5959 openness later on in put_text_prop_openness_mapper(). */
|
0
|
5960 if (!EQ (Fextent_property (extent, Qtext_prop, Qnil), closure->prop))
|
|
5961 /* It's not for this property; do nothing. */
|
|
5962 return 0;
|
|
5963
|
|
5964 e_start = extent_endpoint_bytind (e, 0);
|
|
5965 e_end = extent_endpoint_bytind (e, 1);
|
|
5966 e_val = Fextent_property (extent, closure->prop, Qnil);
|
|
5967 is_eq = EQ (value, e_val);
|
|
5968
|
|
5969 if (!NILP (value) && NILP (closure->the_extent) && is_eq)
|
|
5970 {
|
|
5971 /* We want there to be an extent here at the end, and we haven't picked
|
|
5972 one yet, so use this one. Extend it as necessary. We only reuse an
|
|
5973 extent which has an EQ value for the prop in question to avoid
|
|
5974 side-effecting the kill ring (that is, we never change the property
|
|
5975 on an extent after it has been created.)
|
|
5976 */
|
|
5977 if (e_start != start || e_end != end)
|
|
5978 {
|
|
5979 Bytind new_start = min (e_start, start);
|
|
5980 Bytind new_end = max (e_end, end);
|
|
5981 set_extent_endpoints (e, new_start, new_end, Qnil);
|
|
5982 /* If we changed the endpoint, then we need to set its
|
|
5983 openness. */
|
|
5984 set_extent_openness (e, new_start != e_start
|
|
5985 ? !NILP (get_text_property_bytind
|
|
5986 (start, Qstart_open, object,
|
|
5987 EXTENT_AT_AFTER, 1)) : -1,
|
|
5988 new_end != e_end
|
|
5989 ? NILP (get_text_property_bytind
|
|
5990 (end - 1, Qend_closed, object,
|
|
5991 EXTENT_AT_AFTER, 1))
|
|
5992 : -1);
|
|
5993 closure->changed_p = 1;
|
|
5994 }
|
|
5995 closure->the_extent = extent;
|
|
5996 }
|
|
5997
|
|
5998 /* Even if we're adding a prop, at this point, we want all other extents of
|
|
5999 this prop to go away (as now they overlap). So the theory here is that,
|
|
6000 when we are adding a prop to a region that has multiple (disjoint)
|
2
|
6001 occurrences of that prop in it already, we pick one of those and extend
|
0
|
6002 it, and remove the others.
|
|
6003 */
|
|
6004
|
|
6005 else if (EQ (extent, closure->the_extent))
|
|
6006 {
|
|
6007 /* just in case map-extents hits it again (does that happen?) */
|
|
6008 ;
|
|
6009 }
|
|
6010 else if (e_start >= start && e_end <= end)
|
|
6011 {
|
|
6012 /* Extent is contained in region; remove it. Don't destroy or modify
|
|
6013 it, because we don't want to change the attributes pointed to by the
|
|
6014 duplicates in the kill ring.
|
|
6015 */
|
|
6016 extent_detach (e);
|
|
6017 closure->changed_p = 1;
|
|
6018 }
|
|
6019 else if (!NILP (closure->the_extent) &&
|
|
6020 is_eq &&
|
|
6021 e_start <= end &&
|
|
6022 e_end >= start)
|
|
6023 {
|
|
6024 EXTENT te = XEXTENT (closure->the_extent);
|
|
6025 /* This extent overlaps, and has the same prop/value as the extent we've
|
|
6026 decided to reuse, so we can remove this existing extent as well (the
|
|
6027 whole thing, even the part outside of the region) and extend
|
|
6028 the-extent to cover it, resulting in the minimum number of extents in
|
|
6029 the buffer.
|
|
6030 */
|
|
6031 Bytind the_start = extent_endpoint_bytind (te, 0);
|
|
6032 Bytind the_end = extent_endpoint_bytind (te, 1);
|
|
6033 if (e_start != the_start && /* note AND not OR -- hmm, why is this
|
|
6034 the case? I think it's because the
|
|
6035 assumption that the text-property
|
|
6036 extents don't overlap makes it
|
|
6037 OK; changing it to an OR would
|
|
6038 result in changed_p sometimes getting
|
|
6039 falsely marked. Is this bad? */
|
|
6040 e_end != the_end)
|
|
6041 {
|
|
6042 Bytind new_start = min (e_start, the_start);
|
|
6043 Bytind new_end = max (e_end, the_end);
|
|
6044 set_extent_endpoints (te, new_start, new_end, Qnil);
|
|
6045 /* If we changed the endpoint, then we need to set its
|
|
6046 openness. We are setting the endpoint to be the same as
|
|
6047 that of the extent we're about to remove, and we assume
|
|
6048 (the invariant mentioned above) that extent has the
|
|
6049 proper endpoint setting, so we just use it. */
|
|
6050 set_extent_openness (te, new_start != e_start ?
|
|
6051 extent_start_open_p (e) : -1,
|
|
6052 new_end != e_end ?
|
|
6053 extent_end_open_p (e) : -1);
|
|
6054 closure->changed_p = 1;
|
|
6055 }
|
|
6056 extent_detach (e);
|
|
6057 }
|
|
6058 else if (e_end <= end)
|
|
6059 {
|
|
6060 /* Extent begins before start but ends before end, so we can just
|
|
6061 decrease its end position.
|
|
6062 */
|
|
6063 if (e_end != start)
|
|
6064 {
|
|
6065 set_extent_endpoints (e, e_start, start, Qnil);
|
|
6066 set_extent_openness (e, -1, NILP (get_text_property_bytind
|
|
6067 (start - 1, Qend_closed, object,
|
|
6068 EXTENT_AT_AFTER, 1)));
|
|
6069 closure->changed_p = 1;
|
|
6070 }
|
|
6071 }
|
|
6072 else if (e_start >= start)
|
|
6073 {
|
|
6074 /* Extent ends after end but begins after start, so we can just
|
|
6075 increase its start position.
|
|
6076 */
|
|
6077 if (e_start != end)
|
|
6078 {
|
|
6079 set_extent_endpoints (e, end, e_end, Qnil);
|
|
6080 set_extent_openness (e, !NILP (get_text_property_bytind
|
|
6081 (end, Qstart_open, object,
|
|
6082 EXTENT_AT_AFTER, 1)), -1);
|
|
6083 closure->changed_p = 1;
|
|
6084 }
|
|
6085 }
|
|
6086 else
|
|
6087 {
|
|
6088 /* Otherwise, `extent' straddles the region. We need to split it.
|
|
6089 */
|
|
6090 set_extent_endpoints (e, e_start, start, Qnil);
|
|
6091 set_extent_openness (e, -1, NILP (get_text_property_bytind
|
|
6092 (start - 1, Qend_closed, object,
|
|
6093 EXTENT_AT_AFTER, 1)));
|
|
6094 set_extent_openness (copy_extent (e, end, e_end, extent_object (e)),
|
|
6095 !NILP (get_text_property_bytind
|
|
6096 (end, Qstart_open, object,
|
|
6097 EXTENT_AT_AFTER, 1)), -1);
|
|
6098 closure->changed_p = 1;
|
|
6099 }
|
|
6100
|
|
6101 return 0; /* to continue mapping. */
|
|
6102 }
|
|
6103
|
|
6104 static int
|
|
6105 put_text_prop_openness_mapper (EXTENT e, void *arg)
|
|
6106 {
|
|
6107 struct put_text_prop_arg *closure = (struct put_text_prop_arg *) arg;
|
|
6108 Bytind e_start, e_end;
|
|
6109 Bytind start = closure->start;
|
|
6110 Bytind end = closure->end;
|
|
6111 Lisp_Object extent;
|
|
6112 XSETEXTENT (extent, e);
|
|
6113 e_start = extent_endpoint_bytind (e, 0);
|
|
6114 e_end = extent_endpoint_bytind (e, 1);
|
|
6115
|
|
6116 if (NILP (Fextent_property (extent, Qtext_prop, Qnil)))
|
|
6117 {
|
|
6118 /* It's not a text-property extent; do nothing. */
|
|
6119 ;
|
|
6120 }
|
|
6121 /* Note end conditions and NILP/!NILP's carefully. */
|
|
6122 else if (EQ (closure->prop, Qstart_open)
|
|
6123 && e_start >= start && e_start < end)
|
|
6124 set_extent_openness (e, !NILP (closure->value), -1);
|
|
6125 else if (EQ (closure->prop, Qend_closed)
|
|
6126 && e_end > start && e_end <= end)
|
|
6127 set_extent_openness (e, -1, NILP (closure->value));
|
|
6128
|
|
6129 return 0; /* to continue mapping. */
|
|
6130 }
|
|
6131
|
|
6132 static int
|
|
6133 put_text_prop (Bytind start, Bytind end, Lisp_Object object,
|
|
6134 Lisp_Object prop, Lisp_Object value,
|
|
6135 int duplicable_p)
|
|
6136 {
|
|
6137 /* This function can GC */
|
|
6138 struct put_text_prop_arg closure;
|
|
6139
|
|
6140 if (start == end) /* There are no characters in the region. */
|
|
6141 return 0;
|
|
6142
|
|
6143 /* convert to the non-default versions, since a nil property is
|
|
6144 the same as it not being present. */
|
|
6145 if (EQ (prop, Qstart_closed))
|
|
6146 {
|
|
6147 prop = Qstart_open;
|
|
6148 value = NILP (value) ? Qt : Qnil;
|
|
6149 }
|
|
6150 else if (EQ (prop, Qend_open))
|
|
6151 {
|
|
6152 prop = Qend_closed;
|
|
6153 value = NILP (value) ? Qt : Qnil;
|
|
6154 }
|
|
6155
|
|
6156 value = canonicalize_extent_property (prop, value);
|
|
6157
|
|
6158 closure.prop = prop;
|
|
6159 closure.value = value;
|
|
6160 closure.start = start;
|
|
6161 closure.end = end;
|
|
6162 closure.object = object;
|
|
6163 closure.changed_p = 0;
|
|
6164 closure.the_extent = Qnil;
|
|
6165
|
|
6166 map_extents_bytind (start, end,
|
|
6167 put_text_prop_mapper,
|
|
6168 (void *) &closure, object, 0,
|
|
6169 /* get all extents that abut the region */
|
|
6170 ME_ALL_EXTENTS_CLOSED | ME_END_CLOSED |
|
|
6171 /* it might QUIT or error if the user has
|
|
6172 fucked with the extent plist. */
|
|
6173 ME_MIGHT_THROW |
|
|
6174 ME_MIGHT_MODIFY_EXTENTS);
|
|
6175
|
|
6176 /* If we made it through the loop without reusing an extent
|
|
6177 (and we want there to be one) make it now.
|
|
6178 */
|
|
6179 if (!NILP (value) && NILP (closure.the_extent))
|
|
6180 {
|
|
6181 Lisp_Object extent = Qnil;
|
|
6182
|
|
6183 XSETEXTENT (extent, make_extent_internal (object, start, end));
|
|
6184 closure.changed_p = 1;
|
|
6185 Fset_extent_property (extent, Qtext_prop, prop);
|
|
6186 Fset_extent_property (extent, prop, value);
|
|
6187 if (duplicable_p)
|
|
6188 {
|
|
6189 extent_duplicable_p (XEXTENT (extent)) = 1;
|
|
6190 Fset_extent_property (extent, Qpaste_function,
|
|
6191 Qtext_prop_extent_paste_function);
|
|
6192 }
|
|
6193 set_extent_openness (XEXTENT (extent),
|
|
6194 !NILP (get_text_property_bytind
|
|
6195 (start, Qstart_open, object,
|
|
6196 EXTENT_AT_AFTER, 1)),
|
|
6197 NILP (get_text_property_bytind
|
|
6198 (end - 1, Qend_closed, object,
|
|
6199 EXTENT_AT_AFTER, 1)));
|
|
6200 }
|
|
6201
|
|
6202 if (EQ (prop, Qstart_open) || EQ (prop, Qend_closed))
|
|
6203 {
|
|
6204 map_extents_bytind (start, end,
|
|
6205 put_text_prop_openness_mapper,
|
|
6206 (void *) &closure, object, 0,
|
|
6207 /* get all extents that abut the region */
|
|
6208 ME_ALL_EXTENTS_CLOSED | ME_END_CLOSED |
|
|
6209 ME_MIGHT_MODIFY_EXTENTS);
|
|
6210 }
|
|
6211
|
|
6212 return closure.changed_p;
|
|
6213 }
|
|
6214
|
20
|
6215 DEFUN ("put-text-property", Fput_text_property, 4, 5, 0, /*
|
0
|
6216 Adds the given property/value to all characters in the specified region.
|
|
6217 The property is conceptually attached to the characters rather than the
|
|
6218 region. The properties are copied when the characters are copied/pasted.
|
|
6219 Fifth argument OBJECT is the buffer or string containing the text, and
|
|
6220 defaults to the current buffer.
|
20
|
6221 */
|
|
6222 (start, end, prop, value, object))
|
0
|
6223 {
|
|
6224 /* This function can GC */
|
|
6225 Bytind s, e;
|
|
6226
|
|
6227 object = decode_buffer_or_string (object);
|
|
6228 get_buffer_or_string_range_byte (object, start, end, &s, &e, 0);
|
|
6229 CHECK_SYMBOL (prop);
|
|
6230 put_text_prop (s, e, object, prop, value, 1);
|
|
6231 return prop;
|
|
6232 }
|
|
6233
|
20
|
6234 DEFUN ("put-nonduplicable-text-property",
|
|
6235 Fput_nonduplicable_text_property, 4, 5, 0, /*
|
0
|
6236 Adds the given property/value to all characters in the specified region.
|
|
6237 The property is conceptually attached to the characters rather than the
|
|
6238 region, however the properties will not be copied when the characters
|
|
6239 are copied.
|
|
6240 Fifth argument OBJECT is the buffer or string containing the text, and
|
|
6241 defaults to the current buffer.
|
20
|
6242 */
|
|
6243 (start, end, prop, value, object))
|
0
|
6244 {
|
|
6245 /* This function can GC */
|
|
6246 Bytind s, e;
|
|
6247
|
|
6248 object = decode_buffer_or_string (object);
|
|
6249 get_buffer_or_string_range_byte (object, start, end, &s, &e, 0);
|
|
6250 CHECK_SYMBOL (prop);
|
|
6251 put_text_prop (s, e, object, prop, value, 0);
|
|
6252 return prop;
|
|
6253 }
|
|
6254
|
20
|
6255 DEFUN ("add-text-properties", Fadd_text_properties, 3, 4, 0, /*
|
0
|
6256 Add properties to the characters from START to END.
|
|
6257 The third argument PROPS is a property list specifying the property values
|
|
6258 to add. The optional fourth argument, OBJECT, is the buffer or string
|
|
6259 containing the text and defaults to the current buffer. Returns t if
|
|
6260 any property was changed, nil otherwise.
|
20
|
6261 */
|
|
6262 (start, end, props, object))
|
0
|
6263 {
|
|
6264 /* This function can GC */
|
|
6265 int changed = 0;
|
|
6266 Bytind s, e;
|
|
6267
|
|
6268 object = decode_buffer_or_string (object);
|
|
6269 get_buffer_or_string_range_byte (object, start, end, &s, &e, 0);
|
|
6270 CHECK_LIST (props);
|
|
6271 for (; !NILP (props); props = Fcdr (Fcdr (props)))
|
|
6272 {
|
|
6273 Lisp_Object prop = XCAR (props);
|
|
6274 Lisp_Object value = Fcar (XCDR (props));
|
|
6275 CHECK_SYMBOL (prop);
|
|
6276 changed |= put_text_prop (s, e, object, prop, value, 1);
|
|
6277 }
|
|
6278 return (changed ? Qt : Qnil);
|
|
6279 }
|
|
6280
|
|
6281
|
|
6282 DEFUN ("add-nonduplicable-text-properties",
|
20
|
6283 Fadd_nonduplicable_text_properties, 3, 4, 0, /*
|
0
|
6284 Add nonduplicable properties to the characters from START to END.
|
|
6285 (The properties will not be copied when the characters are copied.)
|
|
6286 The third argument PROPS is a property list specifying the property values
|
|
6287 to add. The optional fourth argument, OBJECT, is the buffer or string
|
|
6288 containing the text and defaults to the current buffer. Returns t if
|
|
6289 any property was changed, nil otherwise.
|
20
|
6290 */
|
|
6291 (start, end, props, object))
|
0
|
6292 {
|
|
6293 /* This function can GC */
|
|
6294 int changed = 0;
|
|
6295 Bytind s, e;
|
|
6296
|
|
6297 object = decode_buffer_or_string (object);
|
|
6298 get_buffer_or_string_range_byte (object, start, end, &s, &e, 0);
|
|
6299 CHECK_LIST (props);
|
|
6300 for (; !NILP (props); props = Fcdr (Fcdr (props)))
|
|
6301 {
|
|
6302 Lisp_Object prop = XCAR (props);
|
|
6303 Lisp_Object value = Fcar (XCDR (props));
|
|
6304 CHECK_SYMBOL (prop);
|
|
6305 changed |= put_text_prop (s, e, object, prop, value, 0);
|
|
6306 }
|
|
6307 return (changed ? Qt : Qnil);
|
|
6308 }
|
|
6309
|
20
|
6310 DEFUN ("remove-text-properties", Fremove_text_properties, 3, 4, 0, /*
|
0
|
6311 Remove the given properties from all characters in the specified region.
|
|
6312 PROPS should be a plist, but the values in that plist are ignored (treated
|
|
6313 as nil). Returns t if any property was changed, nil otherwise.
|
|
6314 Fourth argument OBJECT is the buffer or string containing the text, and
|
|
6315 defaults to the current buffer.
|
20
|
6316 */
|
|
6317 (start, end, props, object))
|
0
|
6318 {
|
|
6319 /* This function can GC */
|
|
6320 int changed = 0;
|
|
6321 Bytind s, e;
|
|
6322
|
|
6323 object = decode_buffer_or_string (object);
|
|
6324 get_buffer_or_string_range_byte (object, start, end, &s, &e, 0);
|
|
6325 CHECK_LIST (props);
|
|
6326 for (; !NILP (props); props = Fcdr (Fcdr (props)))
|
|
6327 {
|
|
6328 Lisp_Object prop = XCAR (props);
|
|
6329 CHECK_SYMBOL (prop);
|
|
6330 changed |= put_text_prop (s, e, object, prop, Qnil, 1);
|
|
6331 }
|
|
6332 return (changed ? Qt : Qnil);
|
|
6333 }
|
|
6334
|
|
6335 /* Whenever a text-prop extent is pasted into a buffer (via `yank' or `insert'
|
|
6336 or whatever) we attach the properties to the buffer by calling
|
|
6337 `put-text-property' instead of by simply allowing the extent to be copied or
|
|
6338 re-attached. Then we return nil, telling the extents code not to attach it
|
|
6339 again. By handing the insertion hackery in this way, we make kill/yank
|
|
6340 behave consistently with put-text-property and not fragment the extents
|
|
6341 (since text-prop extents must partition, not overlap).
|
|
6342
|
|
6343 The lisp implementation of this was probably fast enough, but since I moved
|
|
6344 the rest of the put-text-prop code here, I moved this as well for
|
|
6345 completeness.
|
|
6346 */
|
20
|
6347 DEFUN ("text-prop-extent-paste-function",
|
|
6348 Ftext_prop_extent_paste_function, 3, 3, 0, /*
|
0
|
6349 Used as the `paste-function' property of `text-prop' extents.
|
20
|
6350 */
|
|
6351 (extent, from, to))
|
0
|
6352 {
|
|
6353 /* This function can GC */
|
|
6354 Lisp_Object prop, val;
|
|
6355
|
|
6356 prop = Fextent_property (extent, Qtext_prop, Qnil);
|
|
6357 if (NILP (prop))
|
|
6358 signal_simple_error ("internal error: no text-prop", extent);
|
|
6359 val = Fextent_property (extent, prop, Qnil);
|
20
|
6360 #if 0
|
|
6361 /* removed by bill perry, 2/9/97
|
|
6362 ** This little bit of code would not allow you to have a text property
|
|
6363 ** with a value of Qnil. This is bad bad bad.
|
|
6364 */
|
0
|
6365 if (NILP (val))
|
|
6366 signal_simple_error_2 ("internal error: no text-prop",
|
|
6367 extent, prop);
|
20
|
6368 #endif
|
0
|
6369 Fput_text_property (from, to, prop, val, Qnil);
|
|
6370 return Qnil; /* important! */
|
|
6371 }
|
|
6372
|
|
6373 /* This function could easily be written in Lisp but the C code wants
|
|
6374 to use it in connection with invisible extents (at least currently).
|
|
6375 If this changes, consider moving this back into Lisp. */
|
|
6376
|
20
|
6377 DEFUN ("next-single-property-change", Fnext_single_property_change, 2, 4, 0, /*
|
0
|
6378 Return the position of next property change for a specific property.
|
|
6379 Scans characters forward from POS till it finds a change in the PROP
|
|
6380 property, then returns the position of the change. The optional third
|
|
6381 argument OBJECT is the buffer or string to scan (defaults to the current
|
|
6382 buffer).
|
|
6383 The property values are compared with `eq'.
|
|
6384 Return nil if the property is constant all the way to the end of BUFFER.
|
|
6385 If the value is non-nil, it is a position greater than POS, never equal.
|
|
6386
|
|
6387 If the optional fourth argument LIMIT is non-nil, don't search
|
|
6388 past position LIMIT; return LIMIT if nothing is found before LIMIT.
|
|
6389 If two or more extents with conflicting non-nil values for PROP overlap
|
|
6390 a particular character, it is undefined which value is considered to be
|
|
6391 the value of PROP. (Note that this situation will not happen if you always
|
|
6392 use the text-property primitives.)
|
20
|
6393 */
|
|
6394 (pos, prop, object, limit))
|
0
|
6395 {
|
|
6396 Bufpos bpos;
|
|
6397 Bufpos blim;
|
|
6398 Lisp_Object extent, value;
|
|
6399 int limit_was_nil;
|
|
6400
|
|
6401 object = decode_buffer_or_string (object);
|
|
6402 bpos = get_buffer_or_string_pos_char (object, pos, 0);
|
|
6403 if (NILP (limit))
|
|
6404 {
|
|
6405 blim = buffer_or_string_accessible_end_char (object);
|
|
6406 limit_was_nil = 1;
|
|
6407 }
|
|
6408 else
|
|
6409 {
|
|
6410 blim = get_buffer_or_string_pos_char (object, limit, 0);
|
|
6411 limit_was_nil = 0;
|
|
6412 }
|
|
6413 CHECK_SYMBOL (prop);
|
|
6414
|
|
6415 extent = Fextent_at (make_int (bpos), object, prop, Qnil, Qnil);
|
|
6416 if (!NILP (extent))
|
|
6417 value = Fextent_property (extent, prop, Qnil);
|
|
6418 else
|
|
6419 value = Qnil;
|
|
6420
|
|
6421 while (1)
|
|
6422 {
|
|
6423 bpos = XINT (Fnext_extent_change (make_int (bpos), object));
|
|
6424 if (bpos >= blim)
|
|
6425 break; /* property is the same all the way to the end */
|
|
6426 extent = Fextent_at (make_int (bpos), object, prop, Qnil, Qnil);
|
|
6427 if ((NILP (extent) && !NILP (value)) ||
|
|
6428 (!NILP (extent) && !EQ (value,
|
|
6429 Fextent_property (extent, prop, Qnil))))
|
|
6430 return make_int (bpos);
|
|
6431 }
|
|
6432
|
|
6433 /* I think it's more sensible for this function to return nil always
|
|
6434 in this situation and it used to do it this way, but it's been changed
|
|
6435 for FSF compatibility. */
|
|
6436 if (limit_was_nil)
|
|
6437 return Qnil;
|
|
6438 else
|
|
6439 return make_int (blim);
|
|
6440 }
|
|
6441
|
|
6442 /* See comment on previous function about why this is written in C. */
|
|
6443
|
20
|
6444 DEFUN ("previous-single-property-change",
|
|
6445 Fprevious_single_property_change, 2, 4, 0, /*
|
0
|
6446 Return the position of next property change for a specific property.
|
|
6447 Scans characters backward from POS till it finds a change in the PROP
|
|
6448 property, then returns the position of the change. The optional third
|
|
6449 argument OBJECT is the buffer or string to scan (defaults to the current
|
|
6450 buffer).
|
|
6451 The property values are compared with `eq'.
|
|
6452 Return nil if the property is constant all the way to the start of BUFFER.
|
|
6453 If the value is non-nil, it is a position less than POS, never equal.
|
|
6454
|
|
6455 If the optional fourth argument LIMIT is non-nil, don't search back
|
|
6456 past position LIMIT; return LIMIT if nothing is found until LIMIT.
|
|
6457 If two or more extents with conflicting non-nil values for PROP overlap
|
|
6458 a particular character, it is undefined which value is considered to be
|
|
6459 the value of PROP. (Note that this situation will not happen if you always
|
|
6460 use the text-property primitives.)
|
20
|
6461 */
|
|
6462 (pos, prop, object, limit))
|
0
|
6463 {
|
|
6464 Bufpos bpos;
|
|
6465 Bufpos blim;
|
|
6466 Lisp_Object extent, value;
|
|
6467 int limit_was_nil;
|
|
6468
|
|
6469 object = decode_buffer_or_string (object);
|
|
6470 bpos = get_buffer_or_string_pos_char (object, pos, 0);
|
|
6471 if (NILP (limit))
|
|
6472 {
|
|
6473 blim = buffer_or_string_accessible_begin_char (object);
|
|
6474 limit_was_nil = 1;
|
|
6475 }
|
|
6476 else
|
|
6477 {
|
|
6478 blim = get_buffer_or_string_pos_char (object, limit, 0);
|
|
6479 limit_was_nil = 0;
|
|
6480 }
|
|
6481
|
|
6482 CHECK_SYMBOL (prop);
|
|
6483
|
|
6484 /* extent-at refers to the character AFTER bpos, but we want the
|
|
6485 character before bpos. Thus the - 1. extent-at simply
|
|
6486 returns nil on bogus positions, so not to worry. */
|
|
6487 extent = Fextent_at (make_int (bpos - 1), object, prop, Qnil, Qnil);
|
|
6488 if (!NILP (extent))
|
|
6489 value = Fextent_property (extent, prop, Qnil);
|
|
6490 else
|
|
6491 value = Qnil;
|
|
6492
|
|
6493 while (1)
|
|
6494 {
|
|
6495 bpos = XINT (Fprevious_extent_change (make_int (bpos), object));
|
|
6496 if (bpos <= blim)
|
|
6497 break; /* property is the same all the way to the beginning */
|
|
6498 extent = Fextent_at (make_int (bpos - 1), object, prop, Qnil, Qnil);
|
|
6499 if ((NILP (extent) && !NILP (value)) ||
|
|
6500 (!NILP (extent) && !EQ (value,
|
|
6501 Fextent_property (extent, prop, Qnil))))
|
|
6502 return make_int (bpos);
|
|
6503 }
|
|
6504
|
|
6505 /* I think it's more sensible for this function to return nil always
|
|
6506 in this situation and it used to do it this way, but it's been changed
|
|
6507 for FSF compatibility. */
|
|
6508 if (limit_was_nil)
|
|
6509 return Qnil;
|
|
6510 else
|
|
6511 return make_int (blim);
|
|
6512 }
|
|
6513
|
|
6514 #ifdef MEMORY_USAGE_STATS
|
|
6515
|
|
6516 int
|
|
6517 compute_buffer_extent_usage (struct buffer *b, struct overhead_stats *ovstats)
|
|
6518 {
|
|
6519 /* #### not yet written */
|
|
6520 return 0;
|
|
6521 }
|
|
6522
|
|
6523 #endif /* MEMORY_USAGE_STATS */
|
|
6524
|
|
6525
|
|
6526 /************************************************************************/
|
|
6527 /* initialization */
|
|
6528 /************************************************************************/
|
|
6529
|
|
6530 void
|
|
6531 syms_of_extents (void)
|
|
6532 {
|
|
6533 defsymbol (&Qextentp, "extentp");
|
|
6534 defsymbol (&Qextent_live_p, "extent-live-p");
|
|
6535
|
|
6536 defsymbol (&Qend_closed, "end-closed");
|
|
6537 defsymbol (&Qstart_open, "start-open");
|
|
6538 defsymbol (&Qall_extents_closed, "all-extents-closed");
|
|
6539 defsymbol (&Qall_extents_open, "all-extents-open");
|
|
6540 defsymbol (&Qall_extents_closed_open, "all-extents-closed-open");
|
|
6541 defsymbol (&Qall_extents_open_closed, "all-extents-open-closed");
|
|
6542 defsymbol (&Qstart_in_region, "start-in-region");
|
|
6543 defsymbol (&Qend_in_region, "end-in-region");
|
|
6544 defsymbol (&Qstart_and_end_in_region, "start-and-end-in-region");
|
|
6545 defsymbol (&Qstart_or_end_in_region, "start-or-end-in-region");
|
|
6546 defsymbol (&Qnegate_in_region, "negate-in-region");
|
|
6547
|
|
6548 defsymbol (&Qdetached, "detached");
|
|
6549 defsymbol (&Qdestroyed, "destroyed");
|
|
6550 defsymbol (&Qbegin_glyph, "begin-glyph");
|
|
6551 defsymbol (&Qend_glyph, "end-glyph");
|
|
6552 defsymbol (&Qstart_open, "start-open");
|
|
6553 defsymbol (&Qend_open, "end-open");
|
|
6554 defsymbol (&Qstart_closed, "start-closed");
|
|
6555 defsymbol (&Qend_closed, "end-closed");
|
|
6556 defsymbol (&Qread_only, "read-only");
|
|
6557 /* defsymbol (&Qhighlight, "highlight"); in faces.c */
|
|
6558 defsymbol (&Qunique, "unique");
|
|
6559 defsymbol (&Qduplicable, "duplicable");
|
|
6560 defsymbol (&Qdetachable, "detachable");
|
|
6561 defsymbol (&Qpriority, "priority");
|
|
6562 defsymbol (&Qmouse_face, "mouse-face");
|
|
6563
|
|
6564 defsymbol (&Qglyph_layout, "glyph-layout"); /* backwards compatibility */
|
|
6565 defsymbol (&Qbegin_glyph_layout, "begin-glyph-layout");
|
|
6566 defsymbol (&Qbegin_glyph_layout, "end-glyph-layout");
|
|
6567 defsymbol (&Qoutside_margin, "outside-margin");
|
|
6568 defsymbol (&Qinside_margin, "inside-margin");
|
|
6569 defsymbol (&Qwhitespace, "whitespace");
|
|
6570 /* Qtext defined in general.c */
|
|
6571
|
|
6572 defsymbol (&Qglyph_invisible, "glyph-invisible");
|
|
6573
|
|
6574 defsymbol (&Qpaste_function, "paste-function");
|
|
6575 defsymbol (&Qcopy_function, "copy-function");
|
|
6576
|
|
6577 defsymbol (&Qtext_prop, "text-prop");
|
|
6578 defsymbol (&Qtext_prop_extent_paste_function,
|
|
6579 "text-prop-extent-paste-function");
|
|
6580
|
20
|
6581 DEFSUBR (Fextentp);
|
|
6582 DEFSUBR (Fextent_live_p);
|
|
6583 DEFSUBR (Fextent_detached_p);
|
|
6584 DEFSUBR (Fextent_start_position);
|
|
6585 DEFSUBR (Fextent_end_position);
|
|
6586 DEFSUBR (Fextent_object);
|
|
6587 DEFSUBR (Fextent_length);
|
0
|
6588 #if 0
|
20
|
6589 DEFSUBR (Fstack_of_extents);
|
0
|
6590 #endif
|
|
6591
|
20
|
6592 DEFSUBR (Fmake_extent);
|
|
6593 DEFSUBR (Fcopy_extent);
|
|
6594 DEFSUBR (Fdelete_extent);
|
|
6595 DEFSUBR (Fdetach_extent);
|
|
6596 DEFSUBR (Fset_extent_endpoints);
|
|
6597 DEFSUBR (Fnext_extent);
|
|
6598 DEFSUBR (Fprevious_extent);
|
0
|
6599 #if DEBUG_XEMACS
|
20
|
6600 DEFSUBR (Fnext_e_extent);
|
|
6601 DEFSUBR (Fprevious_e_extent);
|
0
|
6602 #endif
|
20
|
6603 DEFSUBR (Fnext_extent_change);
|
|
6604 DEFSUBR (Fprevious_extent_change);
|
|
6605
|
|
6606 DEFSUBR (Fextent_parent);
|
|
6607 DEFSUBR (Fextent_children);
|
|
6608 DEFSUBR (Fset_extent_parent);
|
|
6609
|
|
6610 DEFSUBR (Fextent_in_region_p);
|
|
6611 DEFSUBR (Fmap_extents);
|
|
6612 DEFSUBR (Fmap_extent_children);
|
|
6613 DEFSUBR (Fextent_at);
|
|
6614
|
|
6615 DEFSUBR (Fextent_face);
|
|
6616 DEFSUBR (Fset_extent_face);
|
|
6617 DEFSUBR (Fextent_mouse_face);
|
|
6618 DEFSUBR (Fset_extent_mouse_face);
|
|
6619 DEFSUBR (Fset_extent_begin_glyph);
|
|
6620 DEFSUBR (Fset_extent_end_glyph);
|
|
6621 DEFSUBR (Fextent_begin_glyph);
|
|
6622 DEFSUBR (Fextent_end_glyph);
|
|
6623 DEFSUBR (Fset_extent_begin_glyph_layout);
|
|
6624 DEFSUBR (Fset_extent_end_glyph_layout);
|
|
6625 DEFSUBR (Fextent_begin_glyph_layout);
|
|
6626 DEFSUBR (Fextent_end_glyph_layout);
|
|
6627 DEFSUBR (Fset_extent_priority);
|
|
6628 DEFSUBR (Fextent_priority);
|
|
6629 DEFSUBR (Fset_extent_property);
|
|
6630 DEFSUBR (Fextent_property);
|
|
6631 DEFSUBR (Fextent_properties);
|
|
6632
|
|
6633 DEFSUBR (Fhighlight_extent);
|
|
6634 DEFSUBR (Fforce_highlight_extent);
|
|
6635
|
|
6636 DEFSUBR (Finsert_extent);
|
|
6637
|
|
6638 DEFSUBR (Fget_text_property);
|
|
6639 DEFSUBR (Fget_char_property);
|
|
6640 DEFSUBR (Fput_text_property);
|
|
6641 DEFSUBR (Fput_nonduplicable_text_property);
|
|
6642 DEFSUBR (Fadd_text_properties);
|
|
6643 DEFSUBR (Fadd_nonduplicable_text_properties);
|
|
6644 DEFSUBR (Fremove_text_properties);
|
|
6645 DEFSUBR (Ftext_prop_extent_paste_function);
|
|
6646 DEFSUBR (Fnext_single_property_change);
|
|
6647 DEFSUBR (Fprevious_single_property_change);
|
0
|
6648 }
|
|
6649
|
|
6650 void
|
|
6651 vars_of_extents (void)
|
|
6652 {
|
|
6653 DEFVAR_INT ("mouse-highlight-priority", &mouse_highlight_priority /*
|
|
6654 The priority to use for the mouse-highlighting pseudo-extent
|
|
6655 that is used to highlight extents with the `mouse-face' attribute set.
|
|
6656 See `set-extent-priority'.
|
|
6657 */ );
|
|
6658 /* Set mouse-highlight-priority (which ends up being used both for the
|
|
6659 mouse-highlighting pseudo-extent and the primary selection extent)
|
|
6660 to a very high value because very few extents should override it.
|
|
6661 1000 gives lots of room below it for different-prioritied extents.
|
|
6662 10 doesn't. ediff, for example, likes to use priorities around 100.
|
|
6663 --ben */
|
|
6664 mouse_highlight_priority = /* 10 */ 1000;
|
|
6665
|
|
6666 DEFVAR_LISP ("default-text-properties", &Vdefault_text_properties /*
|
|
6667 Property list giving default values for text properties.
|
|
6668 Whenever a character does not specify a value for a property, the value
|
|
6669 stored in this list is used instead. This only applies when the
|
|
6670 functions `get-text-property' or `get-char-property' are called.
|
|
6671 */ );
|
|
6672 Vdefault_text_properties = Qnil;
|
|
6673
|
|
6674 staticpro (&Vlast_highlighted_extent);
|
|
6675 Vlast_highlighted_extent = Qnil;
|
|
6676
|
|
6677 Vextent_face_reusable_list = Fcons (Qnil, Qnil);
|
|
6678 staticpro (&Vextent_face_reusable_list);
|
|
6679
|
|
6680 extent_auxiliary_defaults.begin_glyph = Qnil;
|
|
6681 extent_auxiliary_defaults.end_glyph = Qnil;
|
|
6682 extent_auxiliary_defaults.parent = Qnil;
|
|
6683 extent_auxiliary_defaults.children = Qnil;
|
|
6684 extent_auxiliary_defaults.priority = 0;
|
|
6685 extent_auxiliary_defaults.invisible = Qnil;
|
|
6686 extent_auxiliary_defaults.read_only = Qnil;
|
|
6687 extent_auxiliary_defaults.mouse_face = Qnil;
|
|
6688 }
|
|
6689
|
|
6690 void
|
|
6691 complex_vars_of_extents (void)
|
|
6692 {
|
|
6693 staticpro (&Vextent_face_memoize_hash_table);
|
|
6694 /* The memoize hash-table maps from lists of symbols to lists of
|
|
6695 faces. It needs to be `equal' to implement the memoization.
|
|
6696 The reverse table maps in the other direction and just needs
|
|
6697 to do `eq' comparison because the lists of faces are already
|
|
6698 memoized. */
|
|
6699 Vextent_face_memoize_hash_table =
|
|
6700 make_lisp_hashtable (100, HASHTABLE_VALUE_WEAK, HASHTABLE_EQUAL);
|
|
6701 staticpro (&Vextent_face_reverse_memoize_hash_table);
|
|
6702 Vextent_face_reverse_memoize_hash_table =
|
|
6703 make_lisp_hashtable (100, HASHTABLE_KEY_WEAK, HASHTABLE_EQ);
|
|
6704 }
|