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