Commit | Line | Data |
---|---|---|
d418ef42 | 1 | /* Interface code for dealing with text properties. |
73b0cd50 | 2 | Copyright (C) 1993-1995, 1997, 1999-2011 Free Software Foundation, Inc. |
d418ef42 JA |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
9ec0b715 | 6 | GNU Emacs is free software: you can redistribute it and/or modify |
d418ef42 | 7 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) any later version. | |
d418ef42 JA |
10 | |
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
d418ef42 | 18 | |
18160b98 | 19 | #include <config.h> |
d7306fe6 | 20 | #include <setjmp.h> |
d418ef42 JA |
21 | #include "lisp.h" |
22 | #include "intervals.h" | |
23 | #include "buffer.h" | |
f5957179 | 24 | #include "window.h" |
59a486ab RS |
25 | |
26 | #ifndef NULL | |
27 | #define NULL (void *)0 | |
28 | #endif | |
318d2fa8 RS |
29 | |
30 | /* Test for membership, allowing for t (actually any non-cons) to mean the | |
31 | universal set. */ | |
32 | ||
33 | #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set)) | |
d418ef42 JA |
34 | \f |
35 | ||
36 | /* NOTES: previous- and next- property change will have to skip | |
37 | zero-length intervals if they are implemented. This could be done | |
38 | inside next_interval and previous_interval. | |
39 | ||
9c79dd1b JA |
40 | set_properties needs to deal with the interval property cache. |
41 | ||
d418ef42 | 42 | It is assumed that for any interval plist, a property appears |
d4b530ad | 43 | only once on the list. Although some code i.e., remove_properties, |
d418ef42 | 44 | handles the more general case, the uniqueness of properties is |
eb8c3be9 | 45 | necessary for the system to remain consistent. This requirement |
cdf3e5a2 | 46 | is enforced by the subrs installing properties onto the intervals. */ |
d418ef42 JA |
47 | |
48 | \f | |
cdf3e5a2 | 49 | /* Types of hooks. */ |
955cbe7b PE |
50 | static Lisp_Object Qmouse_left; |
51 | static Lisp_Object Qmouse_entered; | |
d418ef42 JA |
52 | Lisp_Object Qpoint_left; |
53 | Lisp_Object Qpoint_entered; | |
dc70cea7 RS |
54 | Lisp_Object Qcategory; |
55 | Lisp_Object Qlocal_map; | |
d418ef42 | 56 | |
cdf3e5a2 | 57 | /* Visual properties text (including strings) may have. */ |
955cbe7b PE |
58 | static Lisp_Object Qforeground, Qbackground, Qunderline; |
59 | Lisp_Object Qfont; | |
60 | static Lisp_Object Qstipple; | |
61 | Lisp_Object Qinvisible, Qintangible, Qmouse_face; | |
62 | static Lisp_Object Qread_only; | |
54b33868 | 63 | Lisp_Object Qminibuffer_prompt; |
19e1c426 RS |
64 | |
65 | /* Sticky properties */ | |
66 | Lisp_Object Qfront_sticky, Qrear_nonsticky; | |
d7b4e137 JB |
67 | |
68 | /* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to | |
69 | the o1's cdr. Otherwise, return zero. This is handy for | |
70 | traversing plists. */ | |
70949dac | 71 | #define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCDR (o1), CONSP (o2))) |
d7b4e137 | 72 | |
318d2fa8 RS |
73 | /* verify_interval_modification saves insertion hooks here |
74 | to be run later by report_interval_modification. */ | |
50436f33 PE |
75 | static Lisp_Object interval_insert_behind_hooks; |
76 | static Lisp_Object interval_insert_in_front_hooks; | |
7cb66899 | 77 | |
f57e2426 | 78 | static void text_read_only (Lisp_Object) NO_RETURN; |
cd64ea1d PE |
79 | static Lisp_Object Fprevious_property_change (Lisp_Object, Lisp_Object, |
80 | Lisp_Object); | |
2381d38d | 81 | |
7cb66899 GM |
82 | |
83 | /* Signal a `text-read-only' error. This function makes it easier | |
84 | to capture that error in GDB by putting a breakpoint on it. */ | |
85 | ||
86 | static void | |
971de7fb | 87 | text_read_only (Lisp_Object propval) |
7cb66899 | 88 | { |
d0a29e1d KS |
89 | if (STRINGP (propval)) |
90 | xsignal1 (Qtext_read_only, propval); | |
91 | ||
92 | xsignal0 (Qtext_read_only); | |
7cb66899 GM |
93 | } |
94 | ||
95 | ||
d418ef42 | 96 | \f |
ac876a79 JA |
97 | /* Extract the interval at the position pointed to by BEGIN from |
98 | OBJECT, a string or buffer. Additionally, check that the positions | |
99 | pointed to by BEGIN and END are within the bounds of OBJECT, and | |
100 | reverse them if *BEGIN is greater than *END. The objects pointed | |
101 | to by BEGIN and END may be integers or markers; if the latter, they | |
102 | are coerced to integers. | |
d418ef42 | 103 | |
d4b530ad RS |
104 | When OBJECT is a string, we increment *BEGIN and *END |
105 | to make them origin-one. | |
106 | ||
d418ef42 JA |
107 | Note that buffer points don't correspond to interval indices. |
108 | For example, point-max is 1 greater than the index of the last | |
109 | character. This difference is handled in the caller, which uses | |
110 | the validated points to determine a length, and operates on that. | |
111 | Exceptions are Ftext_properties_at, Fnext_property_change, and | |
112 | Fprevious_property_change which call this function with BEGIN == END. | |
113 | Handle this case specially. | |
114 | ||
115 | If FORCE is soft (0), it's OK to return NULL_INTERVAL. Otherwise, | |
ac876a79 JA |
116 | create an interval tree for OBJECT if one doesn't exist, provided |
117 | the object actually contains text. In the current design, if there | |
d4b530ad | 118 | is no text, there can be no text properties. */ |
d418ef42 JA |
119 | |
120 | #define soft 0 | |
121 | #define hard 1 | |
122 | ||
9dd7eec6 | 123 | INTERVAL |
971de7fb | 124 | validate_interval_range (Lisp_Object object, Lisp_Object *begin, Lisp_Object *end, int force) |
d418ef42 JA |
125 | { |
126 | register INTERVAL i; | |
2452438f | 127 | EMACS_INT searchpos; |
d4b530ad | 128 | |
b7826503 PJ |
129 | CHECK_STRING_OR_BUFFER (object); |
130 | CHECK_NUMBER_COERCE_MARKER (*begin); | |
131 | CHECK_NUMBER_COERCE_MARKER (*end); | |
d418ef42 JA |
132 | |
133 | /* If we are asked for a point, but from a subr which operates | |
cdf3e5a2 | 134 | on a range, then return nothing. */ |
64a49ca7 | 135 | if (EQ (*begin, *end) && begin != end) |
d418ef42 JA |
136 | return NULL_INTERVAL; |
137 | ||
138 | if (XINT (*begin) > XINT (*end)) | |
139 | { | |
d4b530ad RS |
140 | Lisp_Object n; |
141 | n = *begin; | |
d418ef42 | 142 | *begin = *end; |
d4b530ad | 143 | *end = n; |
d418ef42 JA |
144 | } |
145 | ||
5d2fa46f | 146 | if (BUFFERP (object)) |
d418ef42 JA |
147 | { |
148 | register struct buffer *b = XBUFFER (object); | |
149 | ||
d418ef42 JA |
150 | if (!(BUF_BEGV (b) <= XINT (*begin) && XINT (*begin) <= XINT (*end) |
151 | && XINT (*end) <= BUF_ZV (b))) | |
152 | args_out_of_range (*begin, *end); | |
866bf246 | 153 | i = BUF_INTERVALS (b); |
d418ef42 | 154 | |
cdf3e5a2 | 155 | /* If there's no text, there are no properties. */ |
d4b530ad RS |
156 | if (BUF_BEGV (b) == BUF_ZV (b)) |
157 | return NULL_INTERVAL; | |
158 | ||
159 | searchpos = XINT (*begin); | |
d418ef42 JA |
160 | } |
161 | else | |
162 | { | |
2452438f | 163 | EMACS_INT len = SCHARS (object); |
d418ef42 | 164 | |
d4b530ad | 165 | if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end) |
943afcc7 | 166 | && XINT (*end) <= len)) |
d418ef42 | 167 | args_out_of_range (*begin, *end); |
ad077db0 | 168 | XSETFASTINT (*begin, XFASTINT (*begin)); |
b1e94638 | 169 | if (begin != end) |
ad077db0 | 170 | XSETFASTINT (*end, XFASTINT (*end)); |
943afcc7 | 171 | i = STRING_INTERVALS (object); |
d4b530ad | 172 | |
943afcc7 | 173 | if (len == 0) |
d4b530ad RS |
174 | return NULL_INTERVAL; |
175 | ||
176 | searchpos = XINT (*begin); | |
d418ef42 JA |
177 | } |
178 | ||
179 | if (NULL_INTERVAL_P (i)) | |
180 | return (force ? create_root_interval (object) : i); | |
81f6e55f | 181 | |
d4b530ad | 182 | return find_interval (i, searchpos); |
d418ef42 JA |
183 | } |
184 | ||
185 | /* Validate LIST as a property list. If LIST is not a list, then | |
186 | make one consisting of (LIST nil). Otherwise, verify that LIST | |
cdf3e5a2 | 187 | is even numbered and thus suitable as a plist. */ |
d418ef42 JA |
188 | |
189 | static Lisp_Object | |
971de7fb | 190 | validate_plist (Lisp_Object list) |
d418ef42 JA |
191 | { |
192 | if (NILP (list)) | |
193 | return Qnil; | |
194 | ||
195 | if (CONSP (list)) | |
196 | { | |
197 | register int i; | |
198 | register Lisp_Object tail; | |
99784d63 | 199 | for (i = 0, tail = list; CONSP (tail); i++) |
b1e94638 | 200 | { |
99784d63 | 201 | tail = XCDR (tail); |
b1e94638 JB |
202 | QUIT; |
203 | } | |
d418ef42 JA |
204 | if (i & 1) |
205 | error ("Odd length text property list"); | |
206 | return list; | |
207 | } | |
208 | ||
209 | return Fcons (list, Fcons (Qnil, Qnil)); | |
210 | } | |
211 | ||
d418ef42 | 212 | /* Return nonzero if interval I has all the properties, |
cdf3e5a2 | 213 | with the same values, of list PLIST. */ |
d418ef42 JA |
214 | |
215 | static int | |
971de7fb | 216 | interval_has_all_properties (Lisp_Object plist, INTERVAL i) |
d418ef42 | 217 | { |
695f302f | 218 | register Lisp_Object tail1, tail2, sym1; |
d418ef42 JA |
219 | register int found; |
220 | ||
cdf3e5a2 | 221 | /* Go through each element of PLIST. */ |
99784d63 | 222 | for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1))) |
d418ef42 | 223 | { |
99784d63 | 224 | sym1 = XCAR (tail1); |
d418ef42 JA |
225 | found = 0; |
226 | ||
227 | /* Go through I's plist, looking for sym1 */ | |
99784d63 SM |
228 | for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2))) |
229 | if (EQ (sym1, XCAR (tail2))) | |
d418ef42 JA |
230 | { |
231 | /* Found the same property on both lists. If the | |
cdf3e5a2 | 232 | values are unequal, return zero. */ |
99784d63 | 233 | if (! EQ (Fcar (XCDR (tail1)), Fcar (XCDR (tail2)))) |
d418ef42 JA |
234 | return 0; |
235 | ||
224a3131 | 236 | /* Property has same value on both lists; go to next one. */ |
d418ef42 JA |
237 | found = 1; |
238 | break; | |
239 | } | |
240 | ||
241 | if (! found) | |
242 | return 0; | |
243 | } | |
244 | ||
245 | return 1; | |
246 | } | |
247 | ||
248 | /* Return nonzero if the plist of interval I has any of the | |
cdf3e5a2 | 249 | properties of PLIST, regardless of their values. */ |
d418ef42 | 250 | |
55d4c1b2 | 251 | static inline int |
971de7fb | 252 | interval_has_some_properties (Lisp_Object plist, INTERVAL i) |
d418ef42 JA |
253 | { |
254 | register Lisp_Object tail1, tail2, sym; | |
255 | ||
cdf3e5a2 | 256 | /* Go through each element of PLIST. */ |
99784d63 | 257 | for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1))) |
d418ef42 | 258 | { |
99784d63 | 259 | sym = XCAR (tail1); |
d418ef42 JA |
260 | |
261 | /* Go through i's plist, looking for tail1 */ | |
99784d63 SM |
262 | for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2))) |
263 | if (EQ (sym, XCAR (tail2))) | |
d418ef42 JA |
264 | return 1; |
265 | } | |
266 | ||
267 | return 0; | |
268 | } | |
11713b6d RS |
269 | |
270 | /* Return nonzero if the plist of interval I has any of the | |
271 | property names in LIST, regardless of their values. */ | |
272 | ||
55d4c1b2 | 273 | static inline int |
971de7fb | 274 | interval_has_some_properties_list (Lisp_Object list, INTERVAL i) |
11713b6d RS |
275 | { |
276 | register Lisp_Object tail1, tail2, sym; | |
277 | ||
278 | /* Go through each element of LIST. */ | |
99784d63 | 279 | for (tail1 = list; CONSP (tail1); tail1 = XCDR (tail1)) |
11713b6d RS |
280 | { |
281 | sym = Fcar (tail1); | |
282 | ||
283 | /* Go through i's plist, looking for tail1 */ | |
99784d63 | 284 | for (tail2 = i->plist; CONSP (tail2); tail2 = XCDR (XCDR (tail2))) |
11713b6d RS |
285 | if (EQ (sym, XCAR (tail2))) |
286 | return 1; | |
287 | } | |
288 | ||
289 | return 0; | |
290 | } | |
d4b530ad | 291 | \f |
d7b4e137 JB |
292 | /* Changing the plists of individual intervals. */ |
293 | ||
294 | /* Return the value of PROP in property-list PLIST, or Qunbound if it | |
295 | has none. */ | |
64a49ca7 | 296 | static Lisp_Object |
971de7fb | 297 | property_value (Lisp_Object plist, Lisp_Object prop) |
d7b4e137 JB |
298 | { |
299 | Lisp_Object value; | |
300 | ||
301 | while (PLIST_ELT_P (plist, value)) | |
70949dac KR |
302 | if (EQ (XCAR (plist), prop)) |
303 | return XCAR (value); | |
d7b4e137 | 304 | else |
70949dac | 305 | plist = XCDR (value); |
d7b4e137 JB |
306 | |
307 | return Qunbound; | |
308 | } | |
309 | ||
d4b530ad RS |
310 | /* Set the properties of INTERVAL to PROPERTIES, |
311 | and record undo info for the previous values. | |
312 | OBJECT is the string or buffer that INTERVAL belongs to. */ | |
313 | ||
314 | static void | |
971de7fb | 315 | set_properties (Lisp_Object properties, INTERVAL interval, Lisp_Object object) |
d4b530ad | 316 | { |
d7b4e137 | 317 | Lisp_Object sym, value; |
d4b530ad | 318 | |
d7b4e137 | 319 | if (BUFFERP (object)) |
d4b530ad | 320 | { |
d7b4e137 JB |
321 | /* For each property in the old plist which is missing from PROPERTIES, |
322 | or has a different value in PROPERTIES, make an undo record. */ | |
323 | for (sym = interval->plist; | |
324 | PLIST_ELT_P (sym, value); | |
70949dac KR |
325 | sym = XCDR (value)) |
326 | if (! EQ (property_value (properties, XCAR (sym)), | |
327 | XCAR (value))) | |
f7a9275a | 328 | { |
f7a9275a | 329 | record_property_change (interval->position, LENGTH (interval), |
70949dac | 330 | XCAR (sym), XCAR (value), |
f7a9275a RS |
331 | object); |
332 | } | |
d7b4e137 JB |
333 | |
334 | /* For each new property that has no value at all in the old plist, | |
335 | make an undo record binding it to nil, so it will be removed. */ | |
336 | for (sym = properties; | |
337 | PLIST_ELT_P (sym, value); | |
70949dac KR |
338 | sym = XCDR (value)) |
339 | if (EQ (property_value (interval->plist, XCAR (sym)), Qunbound)) | |
f7a9275a | 340 | { |
f7a9275a | 341 | record_property_change (interval->position, LENGTH (interval), |
70949dac | 342 | XCAR (sym), Qnil, |
f7a9275a RS |
343 | object); |
344 | } | |
d4b530ad RS |
345 | } |
346 | ||
347 | /* Store new properties. */ | |
348 | interval->plist = Fcopy_sequence (properties); | |
349 | } | |
d418ef42 JA |
350 | |
351 | /* Add the properties of PLIST to the interval I, or set | |
352 | the value of I's property to the value of the property on PLIST | |
353 | if they are different. | |
354 | ||
d4b530ad RS |
355 | OBJECT should be the string or buffer the interval is in. |
356 | ||
d418ef42 JA |
357 | Return nonzero if this changes I (i.e., if any members of PLIST |
358 | are actually added to I's plist) */ | |
359 | ||
d4b530ad | 360 | static int |
971de7fb | 361 | add_properties (Lisp_Object plist, INTERVAL i, Lisp_Object object) |
d418ef42 | 362 | { |
c98da214 | 363 | Lisp_Object tail1, tail2, sym1, val1; |
d418ef42 JA |
364 | register int changed = 0; |
365 | register int found; | |
c98da214 RS |
366 | struct gcpro gcpro1, gcpro2, gcpro3; |
367 | ||
368 | tail1 = plist; | |
369 | sym1 = Qnil; | |
370 | val1 = Qnil; | |
371 | /* No need to protect OBJECT, because we can GC only in the case | |
372 | where it is a buffer, and live buffers are always protected. | |
373 | I and its plist are also protected, via OBJECT. */ | |
374 | GCPRO3 (tail1, sym1, val1); | |
d418ef42 | 375 | |
cdf3e5a2 | 376 | /* Go through each element of PLIST. */ |
99784d63 | 377 | for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1))) |
d418ef42 | 378 | { |
99784d63 SM |
379 | sym1 = XCAR (tail1); |
380 | val1 = Fcar (XCDR (tail1)); | |
d418ef42 JA |
381 | found = 0; |
382 | ||
383 | /* Go through I's plist, looking for sym1 */ | |
99784d63 SM |
384 | for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2))) |
385 | if (EQ (sym1, XCAR (tail2))) | |
d418ef42 | 386 | { |
c98da214 RS |
387 | /* No need to gcpro, because tail2 protects this |
388 | and it must be a cons cell (we get an error otherwise). */ | |
3814ccf5 | 389 | register Lisp_Object this_cdr; |
d418ef42 | 390 | |
99784d63 | 391 | this_cdr = XCDR (tail2); |
cdf3e5a2 | 392 | /* Found the property. Now check its value. */ |
d418ef42 JA |
393 | found = 1; |
394 | ||
395 | /* The properties have the same value on both lists. | |
cdf3e5a2 | 396 | Continue to the next property. */ |
734c51b2 | 397 | if (EQ (val1, Fcar (this_cdr))) |
d418ef42 JA |
398 | break; |
399 | ||
d4b530ad | 400 | /* Record this change in the buffer, for undo purposes. */ |
5d2fa46f | 401 | if (BUFFERP (object)) |
d4b530ad | 402 | { |
f7a9275a RS |
403 | record_property_change (i->position, LENGTH (i), |
404 | sym1, Fcar (this_cdr), object); | |
d4b530ad RS |
405 | } |
406 | ||
d418ef42 JA |
407 | /* I's property has a different value -- change it */ |
408 | Fsetcar (this_cdr, val1); | |
409 | changed++; | |
410 | break; | |
411 | } | |
412 | ||
413 | if (! found) | |
414 | { | |
d4b530ad | 415 | /* Record this change in the buffer, for undo purposes. */ |
5d2fa46f | 416 | if (BUFFERP (object)) |
d4b530ad | 417 | { |
f7a9275a RS |
418 | record_property_change (i->position, LENGTH (i), |
419 | sym1, Qnil, object); | |
d4b530ad | 420 | } |
d418ef42 JA |
421 | i->plist = Fcons (sym1, Fcons (val1, i->plist)); |
422 | changed++; | |
423 | } | |
424 | } | |
425 | ||
c98da214 RS |
426 | UNGCPRO; |
427 | ||
d418ef42 JA |
428 | return changed; |
429 | } | |
430 | ||
11713b6d RS |
431 | /* For any members of PLIST, or LIST, |
432 | which are properties of I, remove them from I's plist. | |
433 | (If PLIST is non-nil, use that, otherwise use LIST.) | |
d4b530ad | 434 | OBJECT is the string or buffer containing I. */ |
d418ef42 | 435 | |
d4b530ad | 436 | static int |
971de7fb | 437 | remove_properties (Lisp_Object plist, Lisp_Object list, INTERVAL i, Lisp_Object object) |
d418ef42 | 438 | { |
3814ccf5 | 439 | register Lisp_Object tail1, tail2, sym, current_plist; |
d418ef42 JA |
440 | register int changed = 0; |
441 | ||
f25d60d6 KS |
442 | /* Nonzero means tail1 is a plist, otherwise it is a list. */ |
443 | int use_plist; | |
11713b6d | 444 | |
3814ccf5 | 445 | current_plist = i->plist; |
11713b6d RS |
446 | |
447 | if (! NILP (plist)) | |
f25d60d6 | 448 | tail1 = plist, use_plist = 1; |
11713b6d | 449 | else |
f25d60d6 | 450 | tail1 = list, use_plist = 0; |
11713b6d RS |
451 | |
452 | /* Go through each element of LIST or PLIST. */ | |
b079118d | 453 | while (CONSP (tail1)) |
d418ef42 | 454 | { |
11713b6d | 455 | sym = XCAR (tail1); |
d418ef42 | 456 | |
11713b6d | 457 | /* First, remove the symbol if it's at the head of the list */ |
b079118d | 458 | while (CONSP (current_plist) && EQ (sym, XCAR (current_plist))) |
d418ef42 | 459 | { |
5d2fa46f | 460 | if (BUFFERP (object)) |
11713b6d RS |
461 | record_property_change (i->position, LENGTH (i), |
462 | sym, XCAR (XCDR (current_plist)), | |
463 | object); | |
d4b530ad | 464 | |
11713b6d | 465 | current_plist = XCDR (XCDR (current_plist)); |
d418ef42 JA |
466 | changed++; |
467 | } | |
468 | ||
11713b6d | 469 | /* Go through I's plist, looking for SYM. */ |
d418ef42 JA |
470 | tail2 = current_plist; |
471 | while (! NILP (tail2)) | |
472 | { | |
3814ccf5 | 473 | register Lisp_Object this; |
11713b6d | 474 | this = XCDR (XCDR (tail2)); |
b079118d | 475 | if (CONSP (this) && EQ (sym, XCAR (this))) |
d418ef42 | 476 | { |
5d2fa46f | 477 | if (BUFFERP (object)) |
11713b6d RS |
478 | record_property_change (i->position, LENGTH (i), |
479 | sym, XCAR (XCDR (this)), object); | |
d4b530ad | 480 | |
11713b6d | 481 | Fsetcdr (XCDR (tail2), XCDR (XCDR (this))); |
d418ef42 JA |
482 | changed++; |
483 | } | |
484 | tail2 = this; | |
485 | } | |
11713b6d RS |
486 | |
487 | /* Advance thru TAIL1 one way or the other. */ | |
f25d60d6 KS |
488 | tail1 = XCDR (tail1); |
489 | if (use_plist && CONSP (tail1)) | |
11713b6d | 490 | tail1 = XCDR (tail1); |
d418ef42 JA |
491 | } |
492 | ||
493 | if (changed) | |
494 | i->plist = current_plist; | |
495 | return changed; | |
496 | } | |
497 | ||
d4b530ad | 498 | #if 0 |
d418ef42 | 499 | /* Remove all properties from interval I. Return non-zero |
cdf3e5a2 | 500 | if this changes the interval. */ |
d418ef42 | 501 | |
55d4c1b2 | 502 | static inline int |
59fc5cf9 | 503 | erase_properties (INTERVAL i) |
d418ef42 JA |
504 | { |
505 | if (NILP (i->plist)) | |
506 | return 0; | |
507 | ||
508 | i->plist = Qnil; | |
509 | return 1; | |
510 | } | |
d4b530ad | 511 | #endif |
d418ef42 | 512 | \f |
81f6e55f | 513 | /* Returns the interval of POSITION in OBJECT. |
cdf3e5a2 RS |
514 | POSITION is BEG-based. */ |
515 | ||
516 | INTERVAL | |
d1dfb56c | 517 | interval_of (EMACS_INT position, Lisp_Object object) |
cdf3e5a2 RS |
518 | { |
519 | register INTERVAL i; | |
2452438f | 520 | EMACS_INT beg, end; |
cdf3e5a2 RS |
521 | |
522 | if (NILP (object)) | |
523 | XSETBUFFER (object, current_buffer); | |
d0cb872a KH |
524 | else if (EQ (object, Qt)) |
525 | return NULL_INTERVAL; | |
cdf3e5a2 | 526 | |
b7826503 | 527 | CHECK_STRING_OR_BUFFER (object); |
cdf3e5a2 RS |
528 | |
529 | if (BUFFERP (object)) | |
530 | { | |
531 | register struct buffer *b = XBUFFER (object); | |
532 | ||
533 | beg = BUF_BEGV (b); | |
534 | end = BUF_ZV (b); | |
535 | i = BUF_INTERVALS (b); | |
536 | } | |
537 | else | |
538 | { | |
ad077db0 | 539 | beg = 0; |
943afcc7 KR |
540 | end = SCHARS (object); |
541 | i = STRING_INTERVALS (object); | |
cdf3e5a2 RS |
542 | } |
543 | ||
544 | if (!(beg <= position && position <= end)) | |
c7ef8e24 | 545 | args_out_of_range (make_number (position), make_number (position)); |
cdf3e5a2 RS |
546 | if (beg == end || NULL_INTERVAL_P (i)) |
547 | return NULL_INTERVAL; | |
81f6e55f | 548 | |
cdf3e5a2 RS |
549 | return find_interval (i, position); |
550 | } | |
551 | \f | |
a7ca3326 | 552 | DEFUN ("text-properties-at", Ftext_properties_at, |
d418ef42 | 553 | Stext_properties_at, 1, 2, 0, |
8c1a1077 | 554 | doc: /* Return the list of properties of the character at POSITION in OBJECT. |
81f6e55f FP |
555 | If the optional second argument OBJECT is a buffer (or nil, which means |
556 | the current buffer), POSITION is a buffer position (integer or marker). | |
557 | If OBJECT is a string, POSITION is a 0-based index into it. | |
8c1a1077 | 558 | If POSITION is at the end of OBJECT, the value is nil. */) |
5842a27b | 559 | (Lisp_Object position, Lisp_Object object) |
d418ef42 JA |
560 | { |
561 | register INTERVAL i; | |
d418ef42 JA |
562 | |
563 | if (NILP (object)) | |
c8a4fc3d | 564 | XSETBUFFER (object, current_buffer); |
d418ef42 | 565 | |
1f5e848a | 566 | i = validate_interval_range (object, &position, &position, soft); |
d418ef42 JA |
567 | if (NULL_INTERVAL_P (i)) |
568 | return Qnil; | |
1f5e848a | 569 | /* If POSITION is at the end of the interval, |
d4b530ad RS |
570 | it means it's the end of OBJECT. |
571 | There are no properties at the very end, | |
572 | since no character follows. */ | |
1f5e848a | 573 | if (XINT (position) == LENGTH (i) + i->position) |
d4b530ad | 574 | return Qnil; |
d418ef42 JA |
575 | |
576 | return i->plist; | |
577 | } | |
578 | ||
a7ca3326 | 579 | DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0, |
8c1a1077 PJ |
580 | doc: /* Return the value of POSITION's property PROP, in OBJECT. |
581 | OBJECT is optional and defaults to the current buffer. | |
582 | If POSITION is at the end of OBJECT, the value is nil. */) | |
5842a27b | 583 | (Lisp_Object position, Lisp_Object prop, Lisp_Object object) |
5fbe2a44 | 584 | { |
1f5e848a | 585 | return textget (Ftext_properties_at (position, object), prop); |
5fbe2a44 RS |
586 | } |
587 | ||
bcf97349 | 588 | /* Return the value of char's property PROP, in OBJECT at POSITION. |
8d41abc4 MB |
589 | OBJECT is optional and defaults to the current buffer. |
590 | If OVERLAY is non-0, then in the case that the returned property is from | |
591 | an overlay, the overlay found is returned in *OVERLAY, otherwise nil is | |
592 | returned in *OVERLAY. | |
593 | If POSITION is at the end of OBJECT, the value is nil. | |
594 | If OBJECT is a buffer, then overlay properties are considered as well as | |
595 | text properties. | |
596 | If OBJECT is a window, then that window's buffer is used, but | |
597 | window-specific overlays are considered only if they are associated | |
598 | with OBJECT. */ | |
599 | Lisp_Object | |
971de7fb | 600 | get_char_property_and_overlay (Lisp_Object position, register Lisp_Object prop, Lisp_Object object, Lisp_Object *overlay) |
f5957179 KH |
601 | { |
602 | struct window *w = 0; | |
603 | ||
b7826503 | 604 | CHECK_NUMBER_COERCE_MARKER (position); |
f5957179 KH |
605 | |
606 | if (NILP (object)) | |
c8a4fc3d | 607 | XSETBUFFER (object, current_buffer); |
f5957179 KH |
608 | |
609 | if (WINDOWP (object)) | |
610 | { | |
611 | w = XWINDOW (object); | |
64a49ca7 | 612 | object = w->buffer; |
f5957179 KH |
613 | } |
614 | if (BUFFERP (object)) | |
615 | { | |
b081724f | 616 | ptrdiff_t noverlays; |
b5be4dbe | 617 | Lisp_Object *overlay_vec; |
cbc55f55 RS |
618 | struct buffer *obuf = current_buffer; |
619 | ||
dd6f2802 RS |
620 | if (XINT (position) < BUF_BEGV (XBUFFER (object)) |
621 | || XINT (position) > BUF_ZV (XBUFFER (object))) | |
622 | xsignal1 (Qargs_out_of_range, position); | |
623 | ||
cbc55f55 | 624 | set_buffer_temp (XBUFFER (object)); |
f5957179 | 625 | |
b5be4dbe | 626 | GET_OVERLAYS_AT (XINT (position), overlay_vec, noverlays, NULL, 0); |
f5957179 KH |
627 | noverlays = sort_overlays (overlay_vec, noverlays, w); |
628 | ||
cbc55f55 RS |
629 | set_buffer_temp (obuf); |
630 | ||
f5957179 KH |
631 | /* Now check the overlays in order of decreasing priority. */ |
632 | while (--noverlays >= 0) | |
633 | { | |
b5be4dbe | 634 | Lisp_Object tem = Foverlay_get (overlay_vec[noverlays], prop); |
f5957179 | 635 | if (!NILP (tem)) |
8d41abc4 MB |
636 | { |
637 | if (overlay) | |
638 | /* Return the overlay we got the property from. */ | |
639 | *overlay = overlay_vec[noverlays]; | |
640 | return tem; | |
641 | } | |
f5957179 KH |
642 | } |
643 | } | |
8d41abc4 MB |
644 | |
645 | if (overlay) | |
646 | /* Indicate that the return value is not from an overlay. */ | |
647 | *overlay = Qnil; | |
648 | ||
f5957179 KH |
649 | /* Not a buffer, or no appropriate overlay, so fall through to the |
650 | simpler case. */ | |
8d41abc4 MB |
651 | return Fget_text_property (position, prop, object); |
652 | } | |
653 | ||
a7ca3326 | 654 | DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0, |
8c1a1077 | 655 | doc: /* Return the value of POSITION's property PROP, in OBJECT. |
8faef085 | 656 | Both overlay properties and text properties are checked. |
8c1a1077 PJ |
657 | OBJECT is optional and defaults to the current buffer. |
658 | If POSITION is at the end of OBJECT, the value is nil. | |
659 | If OBJECT is a buffer, then overlay properties are considered as well as | |
660 | text properties. | |
661 | If OBJECT is a window, then that window's buffer is used, but window-specific | |
662 | overlays are considered only if they are associated with OBJECT. */) | |
5842a27b | 663 | (Lisp_Object position, Lisp_Object prop, Lisp_Object object) |
8d41abc4 MB |
664 | { |
665 | return get_char_property_and_overlay (position, prop, object, 0); | |
f5957179 | 666 | } |
97a1bc63 LT |
667 | |
668 | DEFUN ("get-char-property-and-overlay", Fget_char_property_and_overlay, | |
669 | Sget_char_property_and_overlay, 2, 3, 0, | |
670 | doc: /* Like `get-char-property', but with extra overlay information. | |
95555145 RS |
671 | The value is a cons cell. Its car is the return value of `get-char-property' |
672 | with the same arguments--that is, the value of POSITION's property | |
673 | PROP in OBJECT. Its cdr is the overlay in which the property was | |
97a1bc63 | 674 | found, or nil, if it was found as a text property or not found at all. |
95555145 | 675 | |
97a1bc63 LT |
676 | OBJECT is optional and defaults to the current buffer. OBJECT may be |
677 | a string, a buffer or a window. For strings, the cdr of the return | |
678 | value is always nil, since strings do not have overlays. If OBJECT is | |
679 | a window, then that window's buffer is used, but window-specific | |
680 | overlays are considered only if they are associated with OBJECT. If | |
681 | POSITION is at the end of OBJECT, both car and cdr are nil. */) | |
5842a27b | 682 | (Lisp_Object position, Lisp_Object prop, Lisp_Object object) |
97a1bc63 LT |
683 | { |
684 | Lisp_Object overlay; | |
685 | Lisp_Object val | |
686 | = get_char_property_and_overlay (position, prop, object, &overlay); | |
6519d955 | 687 | return Fcons (val, overlay); |
97a1bc63 LT |
688 | } |
689 | ||
fcab51aa | 690 | \f |
a7ca3326 | 691 | DEFUN ("next-char-property-change", Fnext_char_property_change, |
fcab51aa | 692 | Snext_char_property_change, 1, 2, 0, |
8c1a1077 | 693 | doc: /* Return the position of next text property or overlay change. |
81f6e55f FP |
694 | This scans characters forward in the current buffer from POSITION till |
695 | it finds a change in some text property, or the beginning or end of an | |
696 | overlay, and returns the position of that. | |
85cc6738 | 697 | If none is found up to (point-max), the function returns (point-max). |
8c1a1077 | 698 | |
a41292c2 | 699 | If the optional second argument LIMIT is non-nil, don't search |
85cc6738 RS |
700 | past position LIMIT; return LIMIT if nothing is found before LIMIT. |
701 | LIMIT is a no-op if it is greater than (point-max). */) | |
5842a27b | 702 | (Lisp_Object position, Lisp_Object limit) |
fcab51aa RS |
703 | { |
704 | Lisp_Object temp; | |
705 | ||
706 | temp = Fnext_overlay_change (position); | |
707 | if (! NILP (limit)) | |
708 | { | |
d615870a | 709 | CHECK_NUMBER_COERCE_MARKER (limit); |
fcab51aa RS |
710 | if (XINT (limit) < XINT (temp)) |
711 | temp = limit; | |
712 | } | |
713 | return Fnext_property_change (position, Qnil, temp); | |
714 | } | |
715 | ||
a7ca3326 | 716 | DEFUN ("previous-char-property-change", Fprevious_char_property_change, |
fcab51aa | 717 | Sprevious_char_property_change, 1, 2, 0, |
8c1a1077 | 718 | doc: /* Return the position of previous text property or overlay change. |
81f6e55f FP |
719 | Scans characters backward in the current buffer from POSITION till it |
720 | finds a change in some text property, or the beginning or end of an | |
721 | overlay, and returns the position of that. | |
85cc6738 | 722 | If none is found since (point-min), the function returns (point-min). |
8c1a1077 | 723 | |
a41292c2 | 724 | If the optional second argument LIMIT is non-nil, don't search |
85cc6738 RS |
725 | past position LIMIT; return LIMIT if nothing is found before LIMIT. |
726 | LIMIT is a no-op if it is less than (point-min). */) | |
5842a27b | 727 | (Lisp_Object position, Lisp_Object limit) |
fcab51aa RS |
728 | { |
729 | Lisp_Object temp; | |
f5957179 | 730 | |
fcab51aa RS |
731 | temp = Fprevious_overlay_change (position); |
732 | if (! NILP (limit)) | |
733 | { | |
d615870a | 734 | CHECK_NUMBER_COERCE_MARKER (limit); |
fcab51aa RS |
735 | if (XINT (limit) > XINT (temp)) |
736 | temp = limit; | |
737 | } | |
738 | return Fprevious_property_change (position, Qnil, temp); | |
739 | } | |
0b0737d1 GM |
740 | |
741 | ||
a7ca3326 | 742 | DEFUN ("next-single-char-property-change", Fnext_single_char_property_change, |
b7e047fb | 743 | Snext_single_char_property_change, 2, 4, 0, |
8c1a1077 PJ |
744 | doc: /* Return the position of next text property or overlay change for a specific property. |
745 | Scans characters forward from POSITION till it finds | |
746 | a change in the PROP property, then returns the position of the change. | |
81f6e55f FP |
747 | If the optional third argument OBJECT is a buffer (or nil, which means |
748 | the current buffer), POSITION is a buffer position (integer or marker). | |
749 | If OBJECT is a string, POSITION is a 0-based index into it. | |
750 | ||
85cc6738 RS |
751 | In a string, scan runs to the end of the string. |
752 | In a buffer, it runs to (point-max), and the value cannot exceed that. | |
753 | ||
8c1a1077 PJ |
754 | The property values are compared with `eq'. |
755 | If the property is constant all the way to the end of OBJECT, return the | |
756 | last valid position in OBJECT. | |
757 | If the optional fourth argument LIMIT is non-nil, don't search | |
758 | past position LIMIT; return LIMIT if nothing is found before LIMIT. */) | |
5842a27b | 759 | (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit) |
0b0737d1 GM |
760 | { |
761 | if (STRINGP (object)) | |
762 | { | |
b7e047fb MB |
763 | position = Fnext_single_property_change (position, prop, object, limit); |
764 | if (NILP (position)) | |
0b0737d1 GM |
765 | { |
766 | if (NILP (limit)) | |
d5db4077 | 767 | position = make_number (SCHARS (object)); |
0b0737d1 | 768 | else |
d615870a DK |
769 | { |
770 | CHECK_NUMBER (limit); | |
771 | position = limit; | |
772 | } | |
0b0737d1 GM |
773 | } |
774 | } | |
775 | else | |
776 | { | |
777 | Lisp_Object initial_value, value; | |
aed13378 | 778 | int count = SPECPDL_INDEX (); |
0b0737d1 | 779 | |
b7e047fb | 780 | if (! NILP (object)) |
b7826503 | 781 | CHECK_BUFFER (object); |
81f6e55f | 782 | |
0b0737d1 GM |
783 | if (BUFFERP (object) && current_buffer != XBUFFER (object)) |
784 | { | |
785 | record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
786 | Fset_buffer (object); | |
787 | } | |
788 | ||
d615870a DK |
789 | CHECK_NUMBER_COERCE_MARKER (position); |
790 | ||
b7e047fb | 791 | initial_value = Fget_char_property (position, prop, object); |
81f6e55f | 792 | |
b7e047fb | 793 | if (NILP (limit)) |
85cc6738 | 794 | XSETFASTINT (limit, ZV); |
b7e047fb | 795 | else |
b7826503 | 796 | CHECK_NUMBER_COERCE_MARKER (limit); |
b7e047fb | 797 | |
85cc6738 | 798 | if (XFASTINT (position) >= XFASTINT (limit)) |
0b0737d1 | 799 | { |
85cc6738 RS |
800 | position = limit; |
801 | if (XFASTINT (position) > ZV) | |
802 | XSETFASTINT (position, ZV); | |
0b0737d1 | 803 | } |
85cc6738 RS |
804 | else |
805 | while (1) | |
806 | { | |
807 | position = Fnext_char_property_change (position, limit); | |
808 | if (XFASTINT (position) >= XFASTINT (limit)) | |
809 | { | |
810 | position = limit; | |
811 | break; | |
812 | } | |
813 | ||
814 | value = Fget_char_property (position, prop, object); | |
815 | if (!EQ (value, initial_value)) | |
816 | break; | |
817 | } | |
0b0737d1 GM |
818 | |
819 | unbind_to (count, Qnil); | |
820 | } | |
821 | ||
b7e047fb | 822 | return position; |
0b0737d1 GM |
823 | } |
824 | ||
a7ca3326 | 825 | DEFUN ("previous-single-char-property-change", |
b7e047fb MB |
826 | Fprevious_single_char_property_change, |
827 | Sprevious_single_char_property_change, 2, 4, 0, | |
8c1a1077 PJ |
828 | doc: /* Return the position of previous text property or overlay change for a specific property. |
829 | Scans characters backward from POSITION till it finds | |
830 | a change in the PROP property, then returns the position of the change. | |
81f6e55f FP |
831 | If the optional third argument OBJECT is a buffer (or nil, which means |
832 | the current buffer), POSITION is a buffer position (integer or marker). | |
833 | If OBJECT is a string, POSITION is a 0-based index into it. | |
834 | ||
85cc6738 RS |
835 | In a string, scan runs to the start of the string. |
836 | In a buffer, it runs to (point-min), and the value cannot be less than that. | |
837 | ||
8c1a1077 PJ |
838 | The property values are compared with `eq'. |
839 | If the property is constant all the way to the start of OBJECT, return the | |
840 | first valid position in OBJECT. | |
e531bdff DA |
841 | If the optional fourth argument LIMIT is non-nil, don't search back past |
842 | position LIMIT; return LIMIT if nothing is found before reaching LIMIT. */) | |
5842a27b | 843 | (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit) |
b7e047fb MB |
844 | { |
845 | if (STRINGP (object)) | |
846 | { | |
847 | position = Fprevious_single_property_change (position, prop, object, limit); | |
848 | if (NILP (position)) | |
849 | { | |
850 | if (NILP (limit)) | |
1e02f3cb | 851 | position = make_number (0); |
b7e047fb | 852 | else |
d615870a DK |
853 | { |
854 | CHECK_NUMBER (limit); | |
855 | position = limit; | |
856 | } | |
b7e047fb MB |
857 | } |
858 | } | |
859 | else | |
860 | { | |
aed13378 | 861 | int count = SPECPDL_INDEX (); |
b7e047fb MB |
862 | |
863 | if (! NILP (object)) | |
b7826503 | 864 | CHECK_BUFFER (object); |
81f6e55f | 865 | |
b7e047fb MB |
866 | if (BUFFERP (object) && current_buffer != XBUFFER (object)) |
867 | { | |
868 | record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
869 | Fset_buffer (object); | |
870 | } | |
81f6e55f | 871 | |
d615870a DK |
872 | CHECK_NUMBER_COERCE_MARKER (position); |
873 | ||
b7e047fb | 874 | if (NILP (limit)) |
85cc6738 | 875 | XSETFASTINT (limit, BEGV); |
b7e047fb | 876 | else |
b7826503 | 877 | CHECK_NUMBER_COERCE_MARKER (limit); |
b7e047fb | 878 | |
ce6b02e0 | 879 | if (XFASTINT (position) <= XFASTINT (limit)) |
85cc6738 RS |
880 | { |
881 | position = limit; | |
882 | if (XFASTINT (position) < BEGV) | |
883 | XSETFASTINT (position, BEGV); | |
884 | } | |
ce6b02e0 | 885 | else |
b7e047fb | 886 | { |
85cc6738 RS |
887 | Lisp_Object initial_value |
888 | = Fget_char_property (make_number (XFASTINT (position) - 1), | |
889 | prop, object); | |
81f6e55f | 890 | |
85cc6738 | 891 | while (1) |
ce6b02e0 MB |
892 | { |
893 | position = Fprevious_char_property_change (position, limit); | |
0b0737d1 | 894 | |
ce6b02e0 MB |
895 | if (XFASTINT (position) <= XFASTINT (limit)) |
896 | { | |
897 | position = limit; | |
898 | break; | |
899 | } | |
900 | else | |
901 | { | |
85cc6738 RS |
902 | Lisp_Object value |
903 | = Fget_char_property (make_number (XFASTINT (position) - 1), | |
904 | prop, object); | |
ce6b02e0 MB |
905 | |
906 | if (!EQ (value, initial_value)) | |
907 | break; | |
908 | } | |
909 | } | |
b7e047fb MB |
910 | } |
911 | ||
912 | unbind_to (count, Qnil); | |
913 | } | |
914 | ||
915 | return position; | |
916 | } | |
fcab51aa | 917 | \f |
a7ca3326 | 918 | DEFUN ("next-property-change", Fnext_property_change, |
111b637d | 919 | Snext_property_change, 1, 3, 0, |
8c1a1077 PJ |
920 | doc: /* Return the position of next property change. |
921 | Scans characters forward from POSITION in OBJECT till it finds | |
922 | a change in some text property, then returns the position of the change. | |
81f6e55f FP |
923 | If the optional second argument OBJECT is a buffer (or nil, which means |
924 | the current buffer), POSITION is a buffer position (integer or marker). | |
925 | If OBJECT is a string, POSITION is a 0-based index into it. | |
8c1a1077 PJ |
926 | Return nil if the property is constant all the way to the end of OBJECT. |
927 | If the value is non-nil, it is a position greater than POSITION, never equal. | |
928 | ||
929 | If the optional third argument LIMIT is non-nil, don't search | |
930 | past position LIMIT; return LIMIT if nothing is found before LIMIT. */) | |
5842a27b | 931 | (Lisp_Object position, Lisp_Object object, Lisp_Object limit) |
d418ef42 JA |
932 | { |
933 | register INTERVAL i, next; | |
934 | ||
5fbe2a44 | 935 | if (NILP (object)) |
c8a4fc3d | 936 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 937 | |
3a232704 | 938 | if (!NILP (limit) && !EQ (limit, Qt)) |
b7826503 | 939 | CHECK_NUMBER_COERCE_MARKER (limit); |
1387d54e | 940 | |
1f5e848a | 941 | i = validate_interval_range (object, &position, &position, soft); |
d418ef42 | 942 | |
041aa96f RS |
943 | /* If LIMIT is t, return start of next interval--don't |
944 | bother checking further intervals. */ | |
945 | if (EQ (limit, Qt)) | |
946 | { | |
44214c1b RS |
947 | if (NULL_INTERVAL_P (i)) |
948 | next = i; | |
949 | else | |
950 | next = next_interval (i); | |
81f6e55f | 951 | |
c7b6dfa6 | 952 | if (NULL_INTERVAL_P (next)) |
1f5e848a | 953 | XSETFASTINT (position, (STRINGP (object) |
d5db4077 | 954 | ? SCHARS (object) |
1f5e848a | 955 | : BUF_ZV (XBUFFER (object)))); |
c7b6dfa6 | 956 | else |
ad077db0 | 957 | XSETFASTINT (position, next->position); |
1f5e848a | 958 | return position; |
041aa96f RS |
959 | } |
960 | ||
44214c1b RS |
961 | if (NULL_INTERVAL_P (i)) |
962 | return limit; | |
963 | ||
964 | next = next_interval (i); | |
965 | ||
3a232704 | 966 | while (!NULL_INTERVAL_P (next) && intervals_equal (i, next) |
ad077db0 | 967 | && (NILP (limit) || next->position < XFASTINT (limit))) |
d418ef42 JA |
968 | next = next_interval (next); |
969 | ||
52c0f270 CY |
970 | if (NULL_INTERVAL_P (next) |
971 | || (next->position | |
972 | >= (INTEGERP (limit) | |
973 | ? XFASTINT (limit) | |
974 | : (STRINGP (object) | |
975 | ? SCHARS (object) | |
976 | : BUF_ZV (XBUFFER (object)))))) | |
111b637d | 977 | return limit; |
52c0f270 CY |
978 | else |
979 | return make_number (next->position); | |
19e1c426 RS |
980 | } |
981 | ||
a7ca3326 | 982 | DEFUN ("next-single-property-change", Fnext_single_property_change, |
111b637d | 983 | Snext_single_property_change, 2, 4, 0, |
8c1a1077 PJ |
984 | doc: /* Return the position of next property change for a specific property. |
985 | Scans characters forward from POSITION till it finds | |
986 | a change in the PROP property, then returns the position of the change. | |
81f6e55f FP |
987 | If the optional third argument OBJECT is a buffer (or nil, which means |
988 | the current buffer), POSITION is a buffer position (integer or marker). | |
989 | If OBJECT is a string, POSITION is a 0-based index into it. | |
8c1a1077 PJ |
990 | The property values are compared with `eq'. |
991 | Return nil if the property is constant all the way to the end of OBJECT. | |
992 | If the value is non-nil, it is a position greater than POSITION, never equal. | |
993 | ||
994 | If the optional fourth argument LIMIT is non-nil, don't search | |
995 | past position LIMIT; return LIMIT if nothing is found before LIMIT. */) | |
5842a27b | 996 | (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit) |
9c79dd1b JA |
997 | { |
998 | register INTERVAL i, next; | |
999 | register Lisp_Object here_val; | |
1000 | ||
5fbe2a44 | 1001 | if (NILP (object)) |
c8a4fc3d | 1002 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1003 | |
1387d54e | 1004 | if (!NILP (limit)) |
b7826503 | 1005 | CHECK_NUMBER_COERCE_MARKER (limit); |
1387d54e | 1006 | |
1f5e848a | 1007 | i = validate_interval_range (object, &position, &position, soft); |
9c79dd1b | 1008 | if (NULL_INTERVAL_P (i)) |
111b637d | 1009 | return limit; |
9c79dd1b | 1010 | |
6a0486dd | 1011 | here_val = textget (i->plist, prop); |
9c79dd1b | 1012 | next = next_interval (i); |
81f6e55f | 1013 | while (! NULL_INTERVAL_P (next) |
111b637d | 1014 | && EQ (here_val, textget (next->plist, prop)) |
ad077db0 | 1015 | && (NILP (limit) || next->position < XFASTINT (limit))) |
9c79dd1b JA |
1016 | next = next_interval (next); |
1017 | ||
52c0f270 CY |
1018 | if (NULL_INTERVAL_P (next) |
1019 | || (next->position | |
1020 | >= (INTEGERP (limit) | |
1021 | ? XFASTINT (limit) | |
1022 | : (STRINGP (object) | |
1023 | ? SCHARS (object) | |
1024 | : BUF_ZV (XBUFFER (object)))))) | |
111b637d | 1025 | return limit; |
52c0f270 CY |
1026 | else |
1027 | return make_number (next->position); | |
9c79dd1b JA |
1028 | } |
1029 | ||
a7ca3326 | 1030 | DEFUN ("previous-property-change", Fprevious_property_change, |
111b637d | 1031 | Sprevious_property_change, 1, 3, 0, |
8c1a1077 PJ |
1032 | doc: /* Return the position of previous property change. |
1033 | Scans characters backwards from POSITION in OBJECT till it finds | |
1034 | a change in some text property, then returns the position of the change. | |
81f6e55f FP |
1035 | If the optional second argument OBJECT is a buffer (or nil, which means |
1036 | the current buffer), POSITION is a buffer position (integer or marker). | |
1037 | If OBJECT is a string, POSITION is a 0-based index into it. | |
8c1a1077 PJ |
1038 | Return nil if the property is constant all the way to the start of OBJECT. |
1039 | If the value is non-nil, it is a position less than POSITION, never equal. | |
1040 | ||
1041 | If the optional third argument LIMIT is non-nil, don't search | |
1042 | back past position LIMIT; return LIMIT if nothing is found until LIMIT. */) | |
5842a27b | 1043 | (Lisp_Object position, Lisp_Object object, Lisp_Object limit) |
d418ef42 JA |
1044 | { |
1045 | register INTERVAL i, previous; | |
1046 | ||
5fbe2a44 | 1047 | if (NILP (object)) |
c8a4fc3d | 1048 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1049 | |
1387d54e | 1050 | if (!NILP (limit)) |
b7826503 | 1051 | CHECK_NUMBER_COERCE_MARKER (limit); |
1387d54e | 1052 | |
1f5e848a | 1053 | i = validate_interval_range (object, &position, &position, soft); |
d418ef42 | 1054 | if (NULL_INTERVAL_P (i)) |
111b637d | 1055 | return limit; |
d418ef42 | 1056 | |
53b7feec | 1057 | /* Start with the interval containing the char before point. */ |
1f5e848a | 1058 | if (i->position == XFASTINT (position)) |
53b7feec RS |
1059 | i = previous_interval (i); |
1060 | ||
d418ef42 | 1061 | previous = previous_interval (i); |
3a232704 | 1062 | while (!NULL_INTERVAL_P (previous) && intervals_equal (previous, i) |
111b637d | 1063 | && (NILP (limit) |
ad077db0 | 1064 | || (previous->position + LENGTH (previous) > XFASTINT (limit)))) |
d418ef42 | 1065 | previous = previous_interval (previous); |
d418ef42 | 1066 | |
52c0f270 CY |
1067 | if (NULL_INTERVAL_P (previous) |
1068 | || (previous->position + LENGTH (previous) | |
1069 | <= (INTEGERP (limit) | |
1070 | ? XFASTINT (limit) | |
1071 | : (STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object)))))) | |
1072 | return limit; | |
1073 | else | |
1074 | return make_number (previous->position + LENGTH (previous)); | |
d418ef42 JA |
1075 | } |
1076 | ||
a7ca3326 | 1077 | DEFUN ("previous-single-property-change", Fprevious_single_property_change, |
111b637d | 1078 | Sprevious_single_property_change, 2, 4, 0, |
8c1a1077 PJ |
1079 | doc: /* Return the position of previous property change for a specific property. |
1080 | Scans characters backward from POSITION till it finds | |
1081 | a change in the PROP property, then returns the position of the change. | |
81f6e55f FP |
1082 | If the optional third argument OBJECT is a buffer (or nil, which means |
1083 | the current buffer), POSITION is a buffer position (integer or marker). | |
1084 | If OBJECT is a string, POSITION is a 0-based index into it. | |
8c1a1077 PJ |
1085 | The property values are compared with `eq'. |
1086 | Return nil if the property is constant all the way to the start of OBJECT. | |
1087 | If the value is non-nil, it is a position less than POSITION, never equal. | |
1088 | ||
1089 | If the optional fourth argument LIMIT is non-nil, don't search | |
1090 | back past position LIMIT; return LIMIT if nothing is found until LIMIT. */) | |
5842a27b | 1091 | (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit) |
9c79dd1b JA |
1092 | { |
1093 | register INTERVAL i, previous; | |
1094 | register Lisp_Object here_val; | |
1095 | ||
5fbe2a44 | 1096 | if (NILP (object)) |
c8a4fc3d | 1097 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1098 | |
1387d54e | 1099 | if (!NILP (limit)) |
b7826503 | 1100 | CHECK_NUMBER_COERCE_MARKER (limit); |
1387d54e | 1101 | |
1f5e848a | 1102 | i = validate_interval_range (object, &position, &position, soft); |
9c79dd1b | 1103 | |
53b7feec | 1104 | /* Start with the interval containing the char before point. */ |
3a232704 | 1105 | if (!NULL_INTERVAL_P (i) && i->position == XFASTINT (position)) |
53b7feec RS |
1106 | i = previous_interval (i); |
1107 | ||
6873cfa3 KH |
1108 | if (NULL_INTERVAL_P (i)) |
1109 | return limit; | |
1110 | ||
6a0486dd | 1111 | here_val = textget (i->plist, prop); |
9c79dd1b | 1112 | previous = previous_interval (i); |
3a232704 | 1113 | while (!NULL_INTERVAL_P (previous) |
111b637d RS |
1114 | && EQ (here_val, textget (previous->plist, prop)) |
1115 | && (NILP (limit) | |
ad077db0 | 1116 | || (previous->position + LENGTH (previous) > XFASTINT (limit)))) |
9c79dd1b | 1117 | previous = previous_interval (previous); |
9c79dd1b | 1118 | |
52c0f270 CY |
1119 | if (NULL_INTERVAL_P (previous) |
1120 | || (previous->position + LENGTH (previous) | |
1121 | <= (INTEGERP (limit) | |
1122 | ? XFASTINT (limit) | |
1123 | : (STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object)))))) | |
1124 | return limit; | |
1125 | else | |
1126 | return make_number (previous->position + LENGTH (previous)); | |
9c79dd1b | 1127 | } |
fcab51aa | 1128 | \f |
c98da214 RS |
1129 | /* Callers note, this can GC when OBJECT is a buffer (or nil). */ |
1130 | ||
a7ca3326 | 1131 | DEFUN ("add-text-properties", Fadd_text_properties, |
5fbe2a44 | 1132 | Sadd_text_properties, 3, 4, 0, |
8c1a1077 PJ |
1133 | doc: /* Add properties to the text from START to END. |
1134 | The third argument PROPERTIES is a property list | |
81f6e55f FP |
1135 | specifying the property values to add. If the optional fourth argument |
1136 | OBJECT is a buffer (or nil, which means the current buffer), | |
1137 | START and END are buffer positions (integers or markers). | |
1138 | If OBJECT is a string, START and END are 0-based indices into it. | |
8c1a1077 | 1139 | Return t if any property value actually changed, nil otherwise. */) |
5842a27b | 1140 | (Lisp_Object start, Lisp_Object end, Lisp_Object properties, Lisp_Object object) |
d418ef42 JA |
1141 | { |
1142 | register INTERVAL i, unchanged; | |
2452438f EZ |
1143 | register EMACS_INT s, len; |
1144 | register int modified = 0; | |
c98da214 | 1145 | struct gcpro gcpro1; |
d418ef42 JA |
1146 | |
1147 | properties = validate_plist (properties); | |
1148 | if (NILP (properties)) | |
1149 | return Qnil; | |
1150 | ||
5fbe2a44 | 1151 | if (NILP (object)) |
c8a4fc3d | 1152 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1153 | |
d418ef42 JA |
1154 | i = validate_interval_range (object, &start, &end, hard); |
1155 | if (NULL_INTERVAL_P (i)) | |
1156 | return Qnil; | |
1157 | ||
1158 | s = XINT (start); | |
1159 | len = XINT (end) - s; | |
1160 | ||
c98da214 RS |
1161 | /* No need to protect OBJECT, because we GC only if it's a buffer, |
1162 | and live buffers are always protected. */ | |
1163 | GCPRO1 (properties); | |
1164 | ||
d418ef42 | 1165 | /* If we're not starting on an interval boundary, we have to |
cdf3e5a2 | 1166 | split this interval. */ |
d418ef42 JA |
1167 | if (i->position != s) |
1168 | { | |
1169 | /* If this interval already has the properties, we can | |
cdf3e5a2 | 1170 | skip it. */ |
d418ef42 JA |
1171 | if (interval_has_all_properties (properties, i)) |
1172 | { | |
2452438f | 1173 | EMACS_INT got = (LENGTH (i) - (s - i->position)); |
d418ef42 | 1174 | if (got >= len) |
64db1307 | 1175 | RETURN_UNGCPRO (Qnil); |
d418ef42 | 1176 | len -= got; |
05d5b93e | 1177 | i = next_interval (i); |
d418ef42 JA |
1178 | } |
1179 | else | |
1180 | { | |
1181 | unchanged = i; | |
ad9c1940 | 1182 | i = split_interval_right (unchanged, s - unchanged->position); |
d418ef42 | 1183 | copy_properties (unchanged, i); |
d418ef42 JA |
1184 | } |
1185 | } | |
1186 | ||
2a631db1 | 1187 | if (BUFFERP (object)) |
3e145152 | 1188 | modify_region (XBUFFER (object), XINT (start), XINT (end), 1); |
26c76ace | 1189 | |
daa5e28f | 1190 | /* We are at the beginning of interval I, with LEN chars to scan. */ |
caa31568 | 1191 | for (;;) |
d418ef42 | 1192 | { |
d4b530ad RS |
1193 | if (i == 0) |
1194 | abort (); | |
1195 | ||
d418ef42 JA |
1196 | if (LENGTH (i) >= len) |
1197 | { | |
c98da214 RS |
1198 | /* We can UNGCPRO safely here, because there will be just |
1199 | one more chance to gc, in the next call to add_properties, | |
1200 | and after that we will not need PROPERTIES or OBJECT again. */ | |
1201 | UNGCPRO; | |
1202 | ||
d418ef42 | 1203 | if (interval_has_all_properties (properties, i)) |
26c76ace | 1204 | { |
2a631db1 RS |
1205 | if (BUFFERP (object)) |
1206 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1207 | XINT (end) - XINT (start)); | |
26c76ace RS |
1208 | |
1209 | return modified ? Qt : Qnil; | |
1210 | } | |
d418ef42 JA |
1211 | |
1212 | if (LENGTH (i) == len) | |
1213 | { | |
d4b530ad | 1214 | add_properties (properties, i, object); |
2a631db1 RS |
1215 | if (BUFFERP (object)) |
1216 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1217 | XINT (end) - XINT (start)); | |
d418ef42 JA |
1218 | return Qt; |
1219 | } | |
1220 | ||
1221 | /* i doesn't have the properties, and goes past the change limit */ | |
1222 | unchanged = i; | |
ad9c1940 | 1223 | i = split_interval_left (unchanged, len); |
d418ef42 | 1224 | copy_properties (unchanged, i); |
d4b530ad | 1225 | add_properties (properties, i, object); |
2a631db1 RS |
1226 | if (BUFFERP (object)) |
1227 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1228 | XINT (end) - XINT (start)); | |
d418ef42 JA |
1229 | return Qt; |
1230 | } | |
1231 | ||
1232 | len -= LENGTH (i); | |
d4b530ad | 1233 | modified += add_properties (properties, i, object); |
d418ef42 JA |
1234 | i = next_interval (i); |
1235 | } | |
1236 | } | |
1237 | ||
c98da214 RS |
1238 | /* Callers note, this can GC when OBJECT is a buffer (or nil). */ |
1239 | ||
a7ca3326 | 1240 | DEFUN ("put-text-property", Fput_text_property, |
d4b530ad | 1241 | Sput_text_property, 4, 5, 0, |
8c1a1077 PJ |
1242 | doc: /* Set one property of the text from START to END. |
1243 | The third and fourth arguments PROPERTY and VALUE | |
1244 | specify the property to add. | |
81f6e55f FP |
1245 | If the optional fifth argument OBJECT is a buffer (or nil, which means |
1246 | the current buffer), START and END are buffer positions (integers or | |
1247 | markers). If OBJECT is a string, START and END are 0-based indices into it. */) | |
5842a27b | 1248 | (Lisp_Object start, Lisp_Object end, Lisp_Object property, Lisp_Object value, Lisp_Object object) |
d4b530ad RS |
1249 | { |
1250 | Fadd_text_properties (start, end, | |
1f5e848a | 1251 | Fcons (property, Fcons (value, Qnil)), |
d4b530ad RS |
1252 | object); |
1253 | return Qnil; | |
1254 | } | |
1255 | ||
a7ca3326 | 1256 | DEFUN ("set-text-properties", Fset_text_properties, |
5fbe2a44 | 1257 | Sset_text_properties, 3, 4, 0, |
8c1a1077 PJ |
1258 | doc: /* Completely replace properties of text from START to END. |
1259 | The third argument PROPERTIES is the new property list. | |
81f6e55f FP |
1260 | If the optional fourth argument OBJECT is a buffer (or nil, which means |
1261 | the current buffer), START and END are buffer positions (integers or | |
1262 | markers). If OBJECT is a string, START and END are 0-based indices into it. | |
8c1a1077 PJ |
1263 | If PROPERTIES is nil, the effect is to remove all properties from |
1264 | the designated part of OBJECT. */) | |
5842a27b | 1265 | (Lisp_Object start, Lisp_Object end, Lisp_Object properties, Lisp_Object object) |
0087ade6 GM |
1266 | { |
1267 | return set_text_properties (start, end, properties, object, Qt); | |
1268 | } | |
1269 | ||
1270 | ||
1271 | /* Replace properties of text from START to END with new list of | |
1272 | properties PROPERTIES. OBJECT is the buffer or string containing | |
1273 | the text. OBJECT nil means use the current buffer. | |
9c345213 AM |
1274 | COHERENT_CHANGE_P nil means this is being called as an internal |
1275 | subroutine, rather than as a change primitive with checking of | |
1276 | read-only, invoking change hooks, etc.. Value is nil if the | |
1277 | function _detected_ that it did not replace any properties, non-nil | |
1278 | otherwise. */ | |
0087ade6 GM |
1279 | |
1280 | Lisp_Object | |
971de7fb | 1281 | set_text_properties (Lisp_Object start, Lisp_Object end, Lisp_Object properties, Lisp_Object object, Lisp_Object coherent_change_p) |
d418ef42 | 1282 | { |
28ff4293 | 1283 | register INTERVAL i; |
33d7d0df RS |
1284 | Lisp_Object ostart, oend; |
1285 | ||
1286 | ostart = start; | |
1287 | oend = end; | |
d418ef42 | 1288 | |
1f5e848a | 1289 | properties = validate_plist (properties); |
d418ef42 | 1290 | |
5fbe2a44 | 1291 | if (NILP (object)) |
c8a4fc3d | 1292 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1293 | |
919fa9cb RS |
1294 | /* If we want no properties for a whole string, |
1295 | get rid of its intervals. */ | |
1f5e848a | 1296 | if (NILP (properties) && STRINGP (object) |
919fa9cb | 1297 | && XFASTINT (start) == 0 |
d5db4077 | 1298 | && XFASTINT (end) == SCHARS (object)) |
919fa9cb | 1299 | { |
d5db4077 | 1300 | if (! STRING_INTERVALS (object)) |
537562fa | 1301 | return Qnil; |
26c76ace | 1302 | |
9056febe | 1303 | STRING_SET_INTERVALS (object, NULL_INTERVAL); |
919fa9cb RS |
1304 | return Qt; |
1305 | } | |
1306 | ||
facc570e | 1307 | i = validate_interval_range (object, &start, &end, soft); |
919fa9cb | 1308 | |
d418ef42 | 1309 | if (NULL_INTERVAL_P (i)) |
facc570e | 1310 | { |
1f5e848a EN |
1311 | /* If buffer has no properties, and we want none, return now. */ |
1312 | if (NILP (properties)) | |
facc570e RS |
1313 | return Qnil; |
1314 | ||
33d7d0df RS |
1315 | /* Restore the original START and END values |
1316 | because validate_interval_range increments them for strings. */ | |
1317 | start = ostart; | |
1318 | end = oend; | |
1319 | ||
facc570e RS |
1320 | i = validate_interval_range (object, &start, &end, hard); |
1321 | /* This can return if start == end. */ | |
1322 | if (NULL_INTERVAL_P (i)) | |
1323 | return Qnil; | |
1324 | } | |
d418ef42 | 1325 | |
9c345213 | 1326 | if (BUFFERP (object) && !NILP (coherent_change_p)) |
3e145152 | 1327 | modify_region (XBUFFER (object), XINT (start), XINT (end), 1); |
26c76ace | 1328 | |
78ff4175 RS |
1329 | set_text_properties_1 (start, end, properties, object, i); |
1330 | ||
9c345213 | 1331 | if (BUFFERP (object) && !NILP (coherent_change_p)) |
78ff4175 RS |
1332 | signal_after_change (XINT (start), XINT (end) - XINT (start), |
1333 | XINT (end) - XINT (start)); | |
1334 | return Qt; | |
1335 | } | |
1336 | ||
1337 | /* Replace properties of text from START to END with new list of | |
1338 | properties PROPERTIES. BUFFER is the buffer containing | |
1339 | the text. This does not obey any hooks. | |
1340 | You can provide the interval that START is located in as I, | |
ce768453 | 1341 | or pass NULL for I and this function will find it. |
49f68fd2 | 1342 | START and END can be in any order. */ |
78ff4175 RS |
1343 | |
1344 | void | |
971de7fb | 1345 | set_text_properties_1 (Lisp_Object start, Lisp_Object end, Lisp_Object properties, Lisp_Object buffer, INTERVAL i) |
78ff4175 RS |
1346 | { |
1347 | register INTERVAL prev_changed = NULL_INTERVAL; | |
2452438f | 1348 | register EMACS_INT s, len; |
78ff4175 RS |
1349 | INTERVAL unchanged; |
1350 | ||
67769ffc | 1351 | if (XINT (start) < XINT (end)) |
49f68fd2 | 1352 | { |
67769ffc PE |
1353 | s = XINT (start); |
1354 | len = XINT (end) - s; | |
49f68fd2 | 1355 | } |
67769ffc PE |
1356 | else if (XINT (end) < XINT (start)) |
1357 | { | |
1358 | s = XINT (end); | |
1359 | len = XINT (start) - s; | |
1360 | } | |
1361 | else | |
1362 | return; | |
49f68fd2 | 1363 | |
78ff4175 RS |
1364 | if (i == 0) |
1365 | i = find_interval (BUF_INTERVALS (XBUFFER (buffer)), s); | |
1366 | ||
d418ef42 JA |
1367 | if (i->position != s) |
1368 | { | |
1369 | unchanged = i; | |
ad9c1940 | 1370 | i = split_interval_right (unchanged, s - unchanged->position); |
7855e674 | 1371 | |
d418ef42 JA |
1372 | if (LENGTH (i) > len) |
1373 | { | |
9c79dd1b | 1374 | copy_properties (unchanged, i); |
ad9c1940 | 1375 | i = split_interval_left (i, len); |
78ff4175 RS |
1376 | set_properties (properties, i, buffer); |
1377 | return; | |
d418ef42 JA |
1378 | } |
1379 | ||
78ff4175 | 1380 | set_properties (properties, i, buffer); |
daa5e28f | 1381 | |
9c79dd1b | 1382 | if (LENGTH (i) == len) |
78ff4175 | 1383 | return; |
9c79dd1b JA |
1384 | |
1385 | prev_changed = i; | |
d418ef42 JA |
1386 | len -= LENGTH (i); |
1387 | i = next_interval (i); | |
1388 | } | |
1389 | ||
1e792e4d PE |
1390 | /* We are starting at the beginning of an interval I. LEN is positive. */ |
1391 | do | |
d418ef42 | 1392 | { |
d4b530ad RS |
1393 | if (i == 0) |
1394 | abort (); | |
1395 | ||
d418ef42 JA |
1396 | if (LENGTH (i) >= len) |
1397 | { | |
cd7d971d | 1398 | if (LENGTH (i) > len) |
ad9c1940 | 1399 | i = split_interval_left (i, len); |
d418ef42 | 1400 | |
6f232881 RS |
1401 | /* We have to call set_properties even if we are going to |
1402 | merge the intervals, so as to make the undo records | |
1403 | and cause redisplay to happen. */ | |
78ff4175 | 1404 | set_properties (properties, i, buffer); |
6f232881 | 1405 | if (!NULL_INTERVAL_P (prev_changed)) |
9c79dd1b | 1406 | merge_interval_left (i); |
78ff4175 | 1407 | return; |
d418ef42 JA |
1408 | } |
1409 | ||
1410 | len -= LENGTH (i); | |
6f232881 RS |
1411 | |
1412 | /* We have to call set_properties even if we are going to | |
1413 | merge the intervals, so as to make the undo records | |
1414 | and cause redisplay to happen. */ | |
78ff4175 | 1415 | set_properties (properties, i, buffer); |
9c79dd1b | 1416 | if (NULL_INTERVAL_P (prev_changed)) |
6f232881 | 1417 | prev_changed = i; |
9c79dd1b JA |
1418 | else |
1419 | prev_changed = i = merge_interval_left (i); | |
1420 | ||
d418ef42 JA |
1421 | i = next_interval (i); |
1422 | } | |
1e792e4d | 1423 | while (len > 0); |
d418ef42 JA |
1424 | } |
1425 | ||
a7ca3326 | 1426 | DEFUN ("remove-text-properties", Fremove_text_properties, |
5fbe2a44 | 1427 | Sremove_text_properties, 3, 4, 0, |
8c1a1077 PJ |
1428 | doc: /* Remove some properties from text from START to END. |
1429 | The third argument PROPERTIES is a property list | |
1430 | whose property names specify the properties to remove. | |
1431 | \(The values stored in PROPERTIES are ignored.) | |
81f6e55f FP |
1432 | If the optional fourth argument OBJECT is a buffer (or nil, which means |
1433 | the current buffer), START and END are buffer positions (integers or | |
1434 | markers). If OBJECT is a string, START and END are 0-based indices into it. | |
1435 | Return t if any property was actually removed, nil otherwise. | |
1436 | ||
518c0b83 | 1437 | Use `set-text-properties' if you want to remove all text properties. */) |
5842a27b | 1438 | (Lisp_Object start, Lisp_Object end, Lisp_Object properties, Lisp_Object object) |
d418ef42 JA |
1439 | { |
1440 | register INTERVAL i, unchanged; | |
2452438f EZ |
1441 | register EMACS_INT s, len; |
1442 | register int modified = 0; | |
d418ef42 | 1443 | |
5fbe2a44 | 1444 | if (NILP (object)) |
c8a4fc3d | 1445 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1446 | |
d418ef42 JA |
1447 | i = validate_interval_range (object, &start, &end, soft); |
1448 | if (NULL_INTERVAL_P (i)) | |
1449 | return Qnil; | |
1450 | ||
1451 | s = XINT (start); | |
1452 | len = XINT (end) - s; | |
9c79dd1b | 1453 | |
d418ef42 JA |
1454 | if (i->position != s) |
1455 | { | |
1456 | /* No properties on this first interval -- return if | |
cdf3e5a2 | 1457 | it covers the entire region. */ |
1f5e848a | 1458 | if (! interval_has_some_properties (properties, i)) |
d418ef42 | 1459 | { |
2452438f | 1460 | EMACS_INT got = (LENGTH (i) - (s - i->position)); |
d418ef42 JA |
1461 | if (got >= len) |
1462 | return Qnil; | |
1463 | len -= got; | |
05d5b93e | 1464 | i = next_interval (i); |
d418ef42 | 1465 | } |
daa5e28f RS |
1466 | /* Split away the beginning of this interval; what we don't |
1467 | want to modify. */ | |
d418ef42 JA |
1468 | else |
1469 | { | |
1470 | unchanged = i; | |
ad9c1940 | 1471 | i = split_interval_right (unchanged, s - unchanged->position); |
d418ef42 | 1472 | copy_properties (unchanged, i); |
d418ef42 JA |
1473 | } |
1474 | } | |
1475 | ||
2a631db1 | 1476 | if (BUFFERP (object)) |
3e145152 | 1477 | modify_region (XBUFFER (object), XINT (start), XINT (end), 1); |
26c76ace | 1478 | |
d418ef42 | 1479 | /* We are at the beginning of an interval, with len to scan */ |
caa31568 | 1480 | for (;;) |
d418ef42 | 1481 | { |
d4b530ad RS |
1482 | if (i == 0) |
1483 | abort (); | |
1484 | ||
d418ef42 JA |
1485 | if (LENGTH (i) >= len) |
1486 | { | |
1f5e848a | 1487 | if (! interval_has_some_properties (properties, i)) |
d418ef42 JA |
1488 | return modified ? Qt : Qnil; |
1489 | ||
1490 | if (LENGTH (i) == len) | |
1491 | { | |
11713b6d RS |
1492 | remove_properties (properties, Qnil, i, object); |
1493 | if (BUFFERP (object)) | |
1494 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1495 | XINT (end) - XINT (start)); | |
1496 | return Qt; | |
1497 | } | |
1498 | ||
1499 | /* i has the properties, and goes past the change limit */ | |
1500 | unchanged = i; | |
1501 | i = split_interval_left (i, len); | |
1502 | copy_properties (unchanged, i); | |
1503 | remove_properties (properties, Qnil, i, object); | |
1504 | if (BUFFERP (object)) | |
1505 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1506 | XINT (end) - XINT (start)); | |
1507 | return Qt; | |
1508 | } | |
1509 | ||
1510 | len -= LENGTH (i); | |
1511 | modified += remove_properties (properties, Qnil, i, object); | |
1512 | i = next_interval (i); | |
1513 | } | |
1514 | } | |
1515 | ||
a7ca3326 | 1516 | DEFUN ("remove-list-of-text-properties", Fremove_list_of_text_properties, |
11713b6d RS |
1517 | Sremove_list_of_text_properties, 3, 4, 0, |
1518 | doc: /* Remove some properties from text from START to END. | |
1519 | The third argument LIST-OF-PROPERTIES is a list of property names to remove. | |
81f6e55f FP |
1520 | If the optional fourth argument OBJECT is a buffer (or nil, which means |
1521 | the current buffer), START and END are buffer positions (integers or | |
1522 | markers). If OBJECT is a string, START and END are 0-based indices into it. | |
11713b6d | 1523 | Return t if any property was actually removed, nil otherwise. */) |
5842a27b | 1524 | (Lisp_Object start, Lisp_Object end, Lisp_Object list_of_properties, Lisp_Object object) |
11713b6d RS |
1525 | { |
1526 | register INTERVAL i, unchanged; | |
2452438f EZ |
1527 | register EMACS_INT s, len; |
1528 | register int modified = 0; | |
11713b6d RS |
1529 | Lisp_Object properties; |
1530 | properties = list_of_properties; | |
1531 | ||
1532 | if (NILP (object)) | |
1533 | XSETBUFFER (object, current_buffer); | |
1534 | ||
1535 | i = validate_interval_range (object, &start, &end, soft); | |
1536 | if (NULL_INTERVAL_P (i)) | |
1537 | return Qnil; | |
1538 | ||
1539 | s = XINT (start); | |
1540 | len = XINT (end) - s; | |
1541 | ||
1542 | if (i->position != s) | |
1543 | { | |
1544 | /* No properties on this first interval -- return if | |
1545 | it covers the entire region. */ | |
1546 | if (! interval_has_some_properties_list (properties, i)) | |
1547 | { | |
2452438f | 1548 | EMACS_INT got = (LENGTH (i) - (s - i->position)); |
11713b6d RS |
1549 | if (got >= len) |
1550 | return Qnil; | |
1551 | len -= got; | |
1552 | i = next_interval (i); | |
1553 | } | |
1554 | /* Split away the beginning of this interval; what we don't | |
1555 | want to modify. */ | |
1556 | else | |
1557 | { | |
1558 | unchanged = i; | |
1559 | i = split_interval_right (unchanged, s - unchanged->position); | |
1560 | copy_properties (unchanged, i); | |
1561 | } | |
1562 | } | |
1563 | ||
9b17c9f5 LH |
1564 | /* We are at the beginning of an interval, with len to scan. |
1565 | The flag `modified' records if changes have been made. | |
1566 | When object is a buffer, we must call modify_region before changes are | |
1567 | made and signal_after_change when we are done. | |
e0f24100 GM |
1568 | We call modify_region before calling remove_properties if modified == 0, |
1569 | and we call signal_after_change before returning if modified != 0. */ | |
11713b6d RS |
1570 | for (;;) |
1571 | { | |
1572 | if (i == 0) | |
1573 | abort (); | |
1574 | ||
1575 | if (LENGTH (i) >= len) | |
1576 | { | |
1577 | if (! interval_has_some_properties_list (properties, i)) | |
ef1b0ba7 SM |
1578 | { |
1579 | if (modified) | |
1580 | { | |
1581 | if (BUFFERP (object)) | |
1582 | signal_after_change (XINT (start), | |
1583 | XINT (end) - XINT (start), | |
1584 | XINT (end) - XINT (start)); | |
1585 | return Qt; | |
1586 | } | |
1587 | else | |
1588 | return Qnil; | |
1589 | } | |
1590 | else if (LENGTH (i) == len) | |
11713b6d | 1591 | { |
9b17c9f5 | 1592 | if (!modified && BUFFERP (object)) |
3e145152 | 1593 | modify_region (XBUFFER (object), XINT (start), XINT (end), 1); |
11713b6d | 1594 | remove_properties (Qnil, properties, i, object); |
2a631db1 RS |
1595 | if (BUFFERP (object)) |
1596 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1597 | XINT (end) - XINT (start)); | |
d418ef42 JA |
1598 | return Qt; |
1599 | } | |
ef1b0ba7 SM |
1600 | else |
1601 | { /* i has the properties, and goes past the change limit. */ | |
1602 | unchanged = i; | |
1603 | i = split_interval_left (i, len); | |
1604 | copy_properties (unchanged, i); | |
1605 | if (!modified && BUFFERP (object)) | |
1606 | modify_region (XBUFFER (object), XINT (start), XINT (end), 1); | |
1607 | remove_properties (Qnil, properties, i, object); | |
1608 | if (BUFFERP (object)) | |
1609 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1610 | XINT (end) - XINT (start)); | |
1611 | return Qt; | |
1612 | } | |
d418ef42 | 1613 | } |
9b17c9f5 LH |
1614 | if (interval_has_some_properties_list (properties, i)) |
1615 | { | |
1616 | if (!modified && BUFFERP (object)) | |
3e145152 | 1617 | modify_region (XBUFFER (object), XINT (start), XINT (end), 1); |
9b17c9f5 LH |
1618 | remove_properties (Qnil, properties, i, object); |
1619 | modified = 1; | |
1620 | } | |
d418ef42 | 1621 | len -= LENGTH (i); |
d418ef42 JA |
1622 | i = next_interval (i); |
1623 | } | |
1624 | } | |
fcab51aa | 1625 | \f |
a7ca3326 | 1626 | DEFUN ("text-property-any", Ftext_property_any, |
ad9c1940 | 1627 | Stext_property_any, 4, 5, 0, |
8c1a1077 PJ |
1628 | doc: /* Check text from START to END for property PROPERTY equalling VALUE. |
1629 | If so, return the position of the first character whose property PROPERTY | |
1630 | is `eq' to VALUE. Otherwise return nil. | |
81f6e55f FP |
1631 | If the optional fifth argument OBJECT is a buffer (or nil, which means |
1632 | the current buffer), START and END are buffer positions (integers or | |
1633 | markers). If OBJECT is a string, START and END are 0-based indices into it. */) | |
5842a27b | 1634 | (Lisp_Object start, Lisp_Object end, Lisp_Object property, Lisp_Object value, Lisp_Object object) |
ad9c1940 JB |
1635 | { |
1636 | register INTERVAL i; | |
2452438f | 1637 | register EMACS_INT e, pos; |
ad9c1940 JB |
1638 | |
1639 | if (NILP (object)) | |
c8a4fc3d | 1640 | XSETBUFFER (object, current_buffer); |
ad9c1940 | 1641 | i = validate_interval_range (object, &start, &end, soft); |
2084fddb KH |
1642 | if (NULL_INTERVAL_P (i)) |
1643 | return (!NILP (value) || EQ (start, end) ? Qnil : start); | |
ad9c1940 JB |
1644 | e = XINT (end); |
1645 | ||
1646 | while (! NULL_INTERVAL_P (i)) | |
1647 | { | |
1648 | if (i->position >= e) | |
1649 | break; | |
1f5e848a | 1650 | if (EQ (textget (i->plist, property), value)) |
ad9c1940 JB |
1651 | { |
1652 | pos = i->position; | |
1653 | if (pos < XINT (start)) | |
1654 | pos = XINT (start); | |
ad077db0 | 1655 | return make_number (pos); |
ad9c1940 JB |
1656 | } |
1657 | i = next_interval (i); | |
1658 | } | |
1659 | return Qnil; | |
1660 | } | |
1661 | ||
1662 | DEFUN ("text-property-not-all", Ftext_property_not_all, | |
1663 | Stext_property_not_all, 4, 5, 0, | |
8c1a1077 PJ |
1664 | doc: /* Check text from START to END for property PROPERTY not equalling VALUE. |
1665 | If so, return the position of the first character whose property PROPERTY | |
1666 | is not `eq' to VALUE. Otherwise, return nil. | |
81f6e55f FP |
1667 | If the optional fifth argument OBJECT is a buffer (or nil, which means |
1668 | the current buffer), START and END are buffer positions (integers or | |
1669 | markers). If OBJECT is a string, START and END are 0-based indices into it. */) | |
5842a27b | 1670 | (Lisp_Object start, Lisp_Object end, Lisp_Object property, Lisp_Object value, Lisp_Object object) |
ad9c1940 JB |
1671 | { |
1672 | register INTERVAL i; | |
2452438f | 1673 | register EMACS_INT s, e; |
ad9c1940 JB |
1674 | |
1675 | if (NILP (object)) | |
c8a4fc3d | 1676 | XSETBUFFER (object, current_buffer); |
ad9c1940 JB |
1677 | i = validate_interval_range (object, &start, &end, soft); |
1678 | if (NULL_INTERVAL_P (i)) | |
916a3119 | 1679 | return (NILP (value) || EQ (start, end)) ? Qnil : start; |
ad9c1940 JB |
1680 | s = XINT (start); |
1681 | e = XINT (end); | |
1682 | ||
1683 | while (! NULL_INTERVAL_P (i)) | |
1684 | { | |
1685 | if (i->position >= e) | |
1686 | break; | |
1f5e848a | 1687 | if (! EQ (textget (i->plist, property), value)) |
ad9c1940 JB |
1688 | { |
1689 | if (i->position > s) | |
1690 | s = i->position; | |
ad077db0 | 1691 | return make_number (s); |
ad9c1940 JB |
1692 | } |
1693 | i = next_interval (i); | |
1694 | } | |
1695 | return Qnil; | |
1696 | } | |
e138dfdc MB |
1697 | |
1698 | \f | |
1699 | /* Return the direction from which the text-property PROP would be | |
1700 | inherited by any new text inserted at POS: 1 if it would be | |
1701 | inherited from the char after POS, -1 if it would be inherited from | |
6f716644 SM |
1702 | the char before POS, and 0 if from neither. |
1703 | BUFFER can be either a buffer or nil (meaning current buffer). */ | |
e138dfdc MB |
1704 | |
1705 | int | |
971de7fb | 1706 | text_property_stickiness (Lisp_Object prop, Lisp_Object pos, Lisp_Object buffer) |
e138dfdc MB |
1707 | { |
1708 | Lisp_Object prev_pos, front_sticky; | |
1709 | int is_rear_sticky = 1, is_front_sticky = 0; /* defaults */ | |
cabf1cac | 1710 | Lisp_Object defalt = Fassq (prop, Vtext_property_default_nonsticky); |
e138dfdc | 1711 | |
6f716644 SM |
1712 | if (NILP (buffer)) |
1713 | XSETBUFFER (buffer, current_buffer); | |
1714 | ||
cabf1cac SM |
1715 | if (CONSP (defalt) && !NILP (XCDR (defalt))) |
1716 | is_rear_sticky = 0; | |
1717 | ||
6f716644 | 1718 | if (XINT (pos) > BUF_BEGV (XBUFFER (buffer))) |
e138dfdc MB |
1719 | /* Consider previous character. */ |
1720 | { | |
1721 | Lisp_Object rear_non_sticky; | |
1722 | ||
1723 | prev_pos = make_number (XINT (pos) - 1); | |
6f716644 | 1724 | rear_non_sticky = Fget_text_property (prev_pos, Qrear_nonsticky, buffer); |
e138dfdc MB |
1725 | |
1726 | if (!NILP (CONSP (rear_non_sticky) | |
1727 | ? Fmemq (prop, rear_non_sticky) | |
1728 | : rear_non_sticky)) | |
1729 | /* PROP is rear-non-sticky. */ | |
1730 | is_rear_sticky = 0; | |
1731 | } | |
506d2f9a CY |
1732 | else |
1733 | return 0; | |
e138dfdc MB |
1734 | |
1735 | /* Consider following character. */ | |
506d2f9a CY |
1736 | /* This signals an arg-out-of-range error if pos is outside the |
1737 | buffer's accessible range. */ | |
6f716644 | 1738 | front_sticky = Fget_text_property (pos, Qfront_sticky, buffer); |
e138dfdc MB |
1739 | |
1740 | if (EQ (front_sticky, Qt) | |
1741 | || (CONSP (front_sticky) | |
1742 | && !NILP (Fmemq (prop, front_sticky)))) | |
1743 | /* PROP is inherited from after. */ | |
1744 | is_front_sticky = 1; | |
1745 | ||
1746 | /* Simple cases, where the properties are consistent. */ | |
1747 | if (is_rear_sticky && !is_front_sticky) | |
1748 | return -1; | |
1749 | else if (!is_rear_sticky && is_front_sticky) | |
1750 | return 1; | |
1751 | else if (!is_rear_sticky && !is_front_sticky) | |
1752 | return 0; | |
1753 | ||
1754 | /* The stickiness properties are inconsistent, so we have to | |
1755 | disambiguate. Basically, rear-sticky wins, _except_ if the | |
1756 | property that would be inherited has a value of nil, in which case | |
1757 | front-sticky wins. */ | |
6f716644 SM |
1758 | if (XINT (pos) == BUF_BEGV (XBUFFER (buffer)) |
1759 | || NILP (Fget_text_property (prev_pos, prop, buffer))) | |
e138dfdc MB |
1760 | return 1; |
1761 | else | |
1762 | return -1; | |
1763 | } | |
1764 | ||
fcab51aa | 1765 | \f |
e2ad650c | 1766 | /* Copying properties between objects. */ |
15e4954b | 1767 | |
e2ad650c | 1768 | /* Add properties from START to END of SRC, starting at POS in DEST. |
c98da214 RS |
1769 | SRC and DEST may each refer to strings or buffers. |
1770 | Optional sixth argument PROP causes only that property to be copied. | |
1771 | Properties are copied to DEST as if by `add-text-properties'. | |
1772 | Return t if any property value actually changed, nil otherwise. */ | |
1773 | ||
1774 | /* Note this can GC when DEST is a buffer. */ | |
ad077db0 | 1775 | |
15e4954b | 1776 | Lisp_Object |
971de7fb | 1777 | copy_text_properties (Lisp_Object start, Lisp_Object end, Lisp_Object src, Lisp_Object pos, Lisp_Object dest, Lisp_Object prop) |
15e4954b JB |
1778 | { |
1779 | INTERVAL i; | |
1780 | Lisp_Object res; | |
1781 | Lisp_Object stuff; | |
1782 | Lisp_Object plist; | |
2452438f EZ |
1783 | EMACS_INT s, e, e2, p, len; |
1784 | int modified = 0; | |
c98da214 | 1785 | struct gcpro gcpro1, gcpro2; |
15e4954b JB |
1786 | |
1787 | i = validate_interval_range (src, &start, &end, soft); | |
1788 | if (NULL_INTERVAL_P (i)) | |
1789 | return Qnil; | |
1790 | ||
b7826503 | 1791 | CHECK_NUMBER_COERCE_MARKER (pos); |
15e4954b JB |
1792 | { |
1793 | Lisp_Object dest_start, dest_end; | |
1794 | ||
1795 | dest_start = pos; | |
e9c4fbcd | 1796 | XSETFASTINT (dest_end, XINT (dest_start) + (XINT (end) - XINT (start))); |
15e4954b JB |
1797 | /* Apply this to a copy of pos; it will try to increment its arguments, |
1798 | which we don't want. */ | |
1799 | validate_interval_range (dest, &dest_start, &dest_end, soft); | |
1800 | } | |
1801 | ||
1802 | s = XINT (start); | |
1803 | e = XINT (end); | |
1804 | p = XINT (pos); | |
1805 | ||
1806 | stuff = Qnil; | |
1807 | ||
1808 | while (s < e) | |
1809 | { | |
1810 | e2 = i->position + LENGTH (i); | |
1811 | if (e2 > e) | |
1812 | e2 = e; | |
1813 | len = e2 - s; | |
1814 | ||
1815 | plist = i->plist; | |
1816 | if (! NILP (prop)) | |
1817 | while (! NILP (plist)) | |
1818 | { | |
1819 | if (EQ (Fcar (plist), prop)) | |
1820 | { | |
1821 | plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil)); | |
1822 | break; | |
1823 | } | |
1824 | plist = Fcdr (Fcdr (plist)); | |
1825 | } | |
1826 | if (! NILP (plist)) | |
1827 | { | |
1828 | /* Must defer modifications to the interval tree in case src | |
cdf3e5a2 | 1829 | and dest refer to the same string or buffer. */ |
15e4954b JB |
1830 | stuff = Fcons (Fcons (make_number (p), |
1831 | Fcons (make_number (p + len), | |
1832 | Fcons (plist, Qnil))), | |
1833 | stuff); | |
1834 | } | |
1835 | ||
1836 | i = next_interval (i); | |
1837 | if (NULL_INTERVAL_P (i)) | |
1838 | break; | |
1839 | ||
1840 | p += len; | |
1841 | s = i->position; | |
1842 | } | |
1843 | ||
c98da214 RS |
1844 | GCPRO2 (stuff, dest); |
1845 | ||
15e4954b JB |
1846 | while (! NILP (stuff)) |
1847 | { | |
1848 | res = Fcar (stuff); | |
1849 | res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)), | |
1850 | Fcar (Fcdr (Fcdr (res))), dest); | |
1851 | if (! NILP (res)) | |
1852 | modified++; | |
1853 | stuff = Fcdr (stuff); | |
1854 | } | |
1855 | ||
c98da214 RS |
1856 | UNGCPRO; |
1857 | ||
15e4954b JB |
1858 | return modified ? Qt : Qnil; |
1859 | } | |
9dd7eec6 GM |
1860 | |
1861 | ||
1862 | /* Return a list representing the text properties of OBJECT between | |
1863 | START and END. if PROP is non-nil, report only on that property. | |
1864 | Each result list element has the form (S E PLIST), where S and E | |
1865 | are positions in OBJECT and PLIST is a property list containing the | |
1866 | text properties of OBJECT between S and E. Value is nil if OBJECT | |
1867 | doesn't contain text properties between START and END. */ | |
1868 | ||
1869 | Lisp_Object | |
971de7fb | 1870 | text_property_list (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object prop) |
9dd7eec6 GM |
1871 | { |
1872 | struct interval *i; | |
1873 | Lisp_Object result; | |
9dd7eec6 GM |
1874 | |
1875 | result = Qnil; | |
81f6e55f | 1876 | |
9dd7eec6 GM |
1877 | i = validate_interval_range (object, &start, &end, soft); |
1878 | if (!NULL_INTERVAL_P (i)) | |
1879 | { | |
2452438f EZ |
1880 | EMACS_INT s = XINT (start); |
1881 | EMACS_INT e = XINT (end); | |
81f6e55f | 1882 | |
9dd7eec6 GM |
1883 | while (s < e) |
1884 | { | |
2452438f | 1885 | EMACS_INT interval_end, len; |
9dd7eec6 | 1886 | Lisp_Object plist; |
81f6e55f | 1887 | |
9dd7eec6 GM |
1888 | interval_end = i->position + LENGTH (i); |
1889 | if (interval_end > e) | |
1890 | interval_end = e; | |
1891 | len = interval_end - s; | |
81f6e55f | 1892 | |
9dd7eec6 GM |
1893 | plist = i->plist; |
1894 | ||
1895 | if (!NILP (prop)) | |
99784d63 SM |
1896 | for (; CONSP (plist); plist = Fcdr (XCDR (plist))) |
1897 | if (EQ (XCAR (plist), prop)) | |
9dd7eec6 | 1898 | { |
99784d63 | 1899 | plist = Fcons (prop, Fcons (Fcar (XCDR (plist)), Qnil)); |
9dd7eec6 GM |
1900 | break; |
1901 | } | |
1902 | ||
1903 | if (!NILP (plist)) | |
1904 | result = Fcons (Fcons (make_number (s), | |
1905 | Fcons (make_number (s + len), | |
1906 | Fcons (plist, Qnil))), | |
1907 | result); | |
81f6e55f | 1908 | |
9dd7eec6 GM |
1909 | i = next_interval (i); |
1910 | if (NULL_INTERVAL_P (i)) | |
1911 | break; | |
1912 | s = i->position; | |
1913 | } | |
1914 | } | |
81f6e55f | 1915 | |
9dd7eec6 GM |
1916 | return result; |
1917 | } | |
1918 | ||
1919 | ||
1920 | /* Add text properties to OBJECT from LIST. LIST is a list of triples | |
1921 | (START END PLIST), where START and END are positions and PLIST is a | |
1922 | property list containing the text properties to add. Adjust START | |
1923 | and END positions by DELTA before adding properties. Value is | |
1924 | non-zero if OBJECT was modified. */ | |
1925 | ||
1926 | int | |
971de7fb | 1927 | add_text_properties_from_list (Lisp_Object object, Lisp_Object list, Lisp_Object delta) |
9dd7eec6 GM |
1928 | { |
1929 | struct gcpro gcpro1, gcpro2; | |
1930 | int modified_p = 0; | |
81f6e55f | 1931 | |
9dd7eec6 | 1932 | GCPRO2 (list, object); |
81f6e55f | 1933 | |
9dd7eec6 GM |
1934 | for (; CONSP (list); list = XCDR (list)) |
1935 | { | |
1936 | Lisp_Object item, start, end, plist, tem; | |
81f6e55f | 1937 | |
9dd7eec6 GM |
1938 | item = XCAR (list); |
1939 | start = make_number (XINT (XCAR (item)) + XINT (delta)); | |
1940 | end = make_number (XINT (XCAR (XCDR (item))) + XINT (delta)); | |
1941 | plist = XCAR (XCDR (XCDR (item))); | |
81f6e55f | 1942 | |
9dd7eec6 GM |
1943 | tem = Fadd_text_properties (start, end, plist, object); |
1944 | if (!NILP (tem)) | |
1945 | modified_p = 1; | |
1946 | } | |
1947 | ||
1948 | UNGCPRO; | |
1949 | return modified_p; | |
1950 | } | |
1951 | ||
1952 | ||
1953 | ||
e398c61c CY |
1954 | /* Modify end-points of ranges in LIST destructively, and return the |
1955 | new list. LIST is a list as returned from text_property_list. | |
1956 | Discard properties that begin at or after NEW_END, and limit | |
1957 | end-points to NEW_END. */ | |
9dd7eec6 | 1958 | |
e398c61c | 1959 | Lisp_Object |
971de7fb | 1960 | extend_property_ranges (Lisp_Object list, Lisp_Object new_end) |
9dd7eec6 | 1961 | { |
e398c61c | 1962 | Lisp_Object prev = Qnil, head = list; |
2452438f | 1963 | EMACS_INT max = XINT (new_end); |
e398c61c CY |
1964 | |
1965 | for (; CONSP (list); prev = list, list = XCDR (list)) | |
9dd7eec6 | 1966 | { |
e398c61c | 1967 | Lisp_Object item, beg, end; |
81f6e55f | 1968 | |
9dd7eec6 | 1969 | item = XCAR (list); |
e398c61c | 1970 | beg = XCAR (item); |
9dd7eec6 GM |
1971 | end = XCAR (XCDR (item)); |
1972 | ||
e398c61c CY |
1973 | if (XINT (beg) >= max) |
1974 | { | |
1975 | /* The start-point is past the end of the new string. | |
1976 | Discard this property. */ | |
1977 | if (EQ (head, list)) | |
1978 | head = XCDR (list); | |
1979 | else | |
1980 | XSETCDR (prev, XCDR (list)); | |
1981 | } | |
1982 | else if (XINT (end) > max) | |
1983 | /* The end-point is past the end of the new string. */ | |
f3fbd155 | 1984 | XSETCAR (XCDR (item), new_end); |
9dd7eec6 | 1985 | } |
e398c61c CY |
1986 | |
1987 | return head; | |
9dd7eec6 GM |
1988 | } |
1989 | ||
1990 | ||
318d2fa8 RS |
1991 | \f |
1992 | /* Call the modification hook functions in LIST, each with START and END. */ | |
1993 | ||
1994 | static void | |
971de7fb | 1995 | call_mod_hooks (Lisp_Object list, Lisp_Object start, Lisp_Object end) |
318d2fa8 RS |
1996 | { |
1997 | struct gcpro gcpro1; | |
1998 | GCPRO1 (list); | |
1999 | while (!NILP (list)) | |
2000 | { | |
2001 | call2 (Fcar (list), start, end); | |
2002 | list = Fcdr (list); | |
2003 | } | |
2004 | UNGCPRO; | |
2005 | } | |
2006 | ||
96f90544 RS |
2007 | /* Check for read-only intervals between character positions START ... END, |
2008 | in BUF, and signal an error if we find one. | |
2009 | ||
2010 | Then check for any modification hooks in the range. | |
2011 | Create a list of all these hooks in lexicographic order, | |
2012 | eliminating consecutive extra copies of the same hook. Then call | |
2013 | those hooks in order, with START and END - 1 as arguments. */ | |
15e4954b | 2014 | |
318d2fa8 | 2015 | void |
d1dfb56c EZ |
2016 | verify_interval_modification (struct buffer *buf, |
2017 | EMACS_INT start, EMACS_INT end) | |
318d2fa8 RS |
2018 | { |
2019 | register INTERVAL intervals = BUF_INTERVALS (buf); | |
695f302f | 2020 | register INTERVAL i; |
318d2fa8 RS |
2021 | Lisp_Object hooks; |
2022 | register Lisp_Object prev_mod_hooks; | |
2023 | Lisp_Object mod_hooks; | |
2024 | struct gcpro gcpro1; | |
2025 | ||
2026 | hooks = Qnil; | |
2027 | prev_mod_hooks = Qnil; | |
2028 | mod_hooks = Qnil; | |
2029 | ||
2030 | interval_insert_behind_hooks = Qnil; | |
2031 | interval_insert_in_front_hooks = Qnil; | |
2032 | ||
2033 | if (NULL_INTERVAL_P (intervals)) | |
2034 | return; | |
2035 | ||
2036 | if (start > end) | |
2037 | { | |
2452438f | 2038 | EMACS_INT temp = start; |
318d2fa8 RS |
2039 | start = end; |
2040 | end = temp; | |
2041 | } | |
2042 | ||
2043 | /* For an insert operation, check the two chars around the position. */ | |
2044 | if (start == end) | |
2045 | { | |
7cb66899 | 2046 | INTERVAL prev = NULL; |
318d2fa8 RS |
2047 | Lisp_Object before, after; |
2048 | ||
2049 | /* Set I to the interval containing the char after START, | |
2050 | and PREV to the interval containing the char before START. | |
2051 | Either one may be null. They may be equal. */ | |
2052 | i = find_interval (intervals, start); | |
2053 | ||
2054 | if (start == BUF_BEGV (buf)) | |
2055 | prev = 0; | |
2056 | else if (i->position == start) | |
2057 | prev = previous_interval (i); | |
2058 | else if (i->position < start) | |
2059 | prev = i; | |
2060 | if (start == BUF_ZV (buf)) | |
2061 | i = 0; | |
2062 | ||
2063 | /* If Vinhibit_read_only is set and is not a list, we can | |
2064 | skip the read_only checks. */ | |
2065 | if (NILP (Vinhibit_read_only) || CONSP (Vinhibit_read_only)) | |
2066 | { | |
2067 | /* If I and PREV differ we need to check for the read-only | |
cdf3e5a2 | 2068 | property together with its stickiness. If either I or |
318d2fa8 RS |
2069 | PREV are 0, this check is all we need. |
2070 | We have to take special care, since read-only may be | |
2071 | indirectly defined via the category property. */ | |
2072 | if (i != prev) | |
2073 | { | |
2074 | if (! NULL_INTERVAL_P (i)) | |
2075 | { | |
2076 | after = textget (i->plist, Qread_only); | |
81f6e55f | 2077 | |
318d2fa8 RS |
2078 | /* If interval I is read-only and read-only is |
2079 | front-sticky, inhibit insertion. | |
2080 | Check for read-only as well as category. */ | |
2081 | if (! NILP (after) | |
2082 | && NILP (Fmemq (after, Vinhibit_read_only))) | |
2083 | { | |
2084 | Lisp_Object tem; | |
2085 | ||
2086 | tem = textget (i->plist, Qfront_sticky); | |
2087 | if (TMEM (Qread_only, tem) | |
2088 | || (NILP (Fplist_get (i->plist, Qread_only)) | |
2089 | && TMEM (Qcategory, tem))) | |
bcf97349 | 2090 | text_read_only (after); |
318d2fa8 RS |
2091 | } |
2092 | } | |
2093 | ||
2094 | if (! NULL_INTERVAL_P (prev)) | |
2095 | { | |
2096 | before = textget (prev->plist, Qread_only); | |
81f6e55f | 2097 | |
318d2fa8 RS |
2098 | /* If interval PREV is read-only and read-only isn't |
2099 | rear-nonsticky, inhibit insertion. | |
2100 | Check for read-only as well as category. */ | |
2101 | if (! NILP (before) | |
2102 | && NILP (Fmemq (before, Vinhibit_read_only))) | |
2103 | { | |
2104 | Lisp_Object tem; | |
2105 | ||
2106 | tem = textget (prev->plist, Qrear_nonsticky); | |
2107 | if (! TMEM (Qread_only, tem) | |
2108 | && (! NILP (Fplist_get (prev->plist,Qread_only)) | |
2109 | || ! TMEM (Qcategory, tem))) | |
bcf97349 | 2110 | text_read_only (before); |
318d2fa8 RS |
2111 | } |
2112 | } | |
2113 | } | |
2114 | else if (! NULL_INTERVAL_P (i)) | |
2115 | { | |
2116 | after = textget (i->plist, Qread_only); | |
81f6e55f | 2117 | |
318d2fa8 RS |
2118 | /* If interval I is read-only and read-only is |
2119 | front-sticky, inhibit insertion. | |
2120 | Check for read-only as well as category. */ | |
2121 | if (! NILP (after) && NILP (Fmemq (after, Vinhibit_read_only))) | |
2122 | { | |
2123 | Lisp_Object tem; | |
2124 | ||
2125 | tem = textget (i->plist, Qfront_sticky); | |
2126 | if (TMEM (Qread_only, tem) | |
2127 | || (NILP (Fplist_get (i->plist, Qread_only)) | |
2128 | && TMEM (Qcategory, tem))) | |
bcf97349 | 2129 | text_read_only (after); |
318d2fa8 RS |
2130 | |
2131 | tem = textget (prev->plist, Qrear_nonsticky); | |
2132 | if (! TMEM (Qread_only, tem) | |
2133 | && (! NILP (Fplist_get (prev->plist, Qread_only)) | |
2134 | || ! TMEM (Qcategory, tem))) | |
bcf97349 | 2135 | text_read_only (after); |
318d2fa8 RS |
2136 | } |
2137 | } | |
2138 | } | |
2139 | ||
2140 | /* Run both insert hooks (just once if they're the same). */ | |
2141 | if (!NULL_INTERVAL_P (prev)) | |
2142 | interval_insert_behind_hooks | |
2143 | = textget (prev->plist, Qinsert_behind_hooks); | |
2144 | if (!NULL_INTERVAL_P (i)) | |
2145 | interval_insert_in_front_hooks | |
2146 | = textget (i->plist, Qinsert_in_front_hooks); | |
2147 | } | |
0ba7995b | 2148 | else |
318d2fa8 RS |
2149 | { |
2150 | /* Loop over intervals on or next to START...END, | |
2151 | collecting their hooks. */ | |
2152 | ||
2153 | i = find_interval (intervals, start); | |
2154 | do | |
2155 | { | |
2156 | if (! INTERVAL_WRITABLE_P (i)) | |
bcf97349 | 2157 | text_read_only (textget (i->plist, Qread_only)); |
318d2fa8 | 2158 | |
0ba7995b | 2159 | if (!inhibit_modification_hooks) |
318d2fa8 | 2160 | { |
0ba7995b GM |
2161 | mod_hooks = textget (i->plist, Qmodification_hooks); |
2162 | if (! NILP (mod_hooks) && ! EQ (mod_hooks, prev_mod_hooks)) | |
2163 | { | |
2164 | hooks = Fcons (mod_hooks, hooks); | |
2165 | prev_mod_hooks = mod_hooks; | |
2166 | } | |
318d2fa8 RS |
2167 | } |
2168 | ||
2169 | i = next_interval (i); | |
2170 | } | |
2171 | /* Keep going thru the interval containing the char before END. */ | |
2172 | while (! NULL_INTERVAL_P (i) && i->position < end); | |
2173 | ||
0ba7995b | 2174 | if (!inhibit_modification_hooks) |
318d2fa8 | 2175 | { |
0ba7995b GM |
2176 | GCPRO1 (hooks); |
2177 | hooks = Fnreverse (hooks); | |
2178 | while (! EQ (hooks, Qnil)) | |
2179 | { | |
2180 | call_mod_hooks (Fcar (hooks), make_number (start), | |
2181 | make_number (end)); | |
2182 | hooks = Fcdr (hooks); | |
2183 | } | |
2184 | UNGCPRO; | |
318d2fa8 | 2185 | } |
318d2fa8 RS |
2186 | } |
2187 | } | |
2188 | ||
96f90544 | 2189 | /* Run the interval hooks for an insertion on character range START ... END. |
318d2fa8 RS |
2190 | verify_interval_modification chose which hooks to run; |
2191 | this function is called after the insertion happens | |
2192 | so it can indicate the range of inserted text. */ | |
2193 | ||
2194 | void | |
971de7fb | 2195 | report_interval_modification (Lisp_Object start, Lisp_Object end) |
318d2fa8 RS |
2196 | { |
2197 | if (! NILP (interval_insert_behind_hooks)) | |
2e34157c | 2198 | call_mod_hooks (interval_insert_behind_hooks, start, end); |
318d2fa8 RS |
2199 | if (! NILP (interval_insert_in_front_hooks) |
2200 | && ! EQ (interval_insert_in_front_hooks, | |
2201 | interval_insert_behind_hooks)) | |
2e34157c | 2202 | call_mod_hooks (interval_insert_in_front_hooks, start, end); |
318d2fa8 RS |
2203 | } |
2204 | \f | |
d418ef42 | 2205 | void |
971de7fb | 2206 | syms_of_textprop (void) |
d418ef42 | 2207 | { |
29208e82 | 2208 | DEFVAR_LISP ("default-text-properties", Vdefault_text_properties, |
8c1a1077 PJ |
2209 | doc: /* Property-list used as default values. |
2210 | The value of a property in this list is seen as the value for every | |
2211 | character that does not have its own value for that property. */); | |
ad1b2f20 | 2212 | Vdefault_text_properties = Qnil; |
c7dd82a3 | 2213 | |
29208e82 | 2214 | DEFVAR_LISP ("char-property-alias-alist", Vchar_property_alias_alist, |
49d110a8 CW |
2215 | doc: /* Alist of alternative properties for properties without a value. |
2216 | Each element should look like (PROPERTY ALTERNATIVE1 ALTERNATIVE2...). | |
2217 | If a piece of text has no direct value for a particular property, then | |
2218 | this alist is consulted. If that property appears in the alist, then | |
2219 | the first non-nil value from the associated alternative properties is | |
2220 | returned. */); | |
2221 | Vchar_property_alias_alist = Qnil; | |
2222 | ||
29208e82 | 2223 | DEFVAR_LISP ("inhibit-point-motion-hooks", Vinhibit_point_motion_hooks, |
8c1a1077 PJ |
2224 | doc: /* If non-nil, don't run `point-left' and `point-entered' text properties. |
2225 | This also inhibits the use of the `intangible' text property. */); | |
688a5a0f | 2226 | Vinhibit_point_motion_hooks = Qnil; |
318d2fa8 | 2227 | |
abc2f676 | 2228 | DEFVAR_LISP ("text-property-default-nonsticky", |
29208e82 | 2229 | Vtext_property_default_nonsticky, |
8c1a1077 PJ |
2230 | doc: /* Alist of properties vs the corresponding non-stickinesses. |
2231 | Each element has the form (PROPERTY . NONSTICKINESS). | |
2232 | ||
2233 | If a character in a buffer has PROPERTY, new text inserted adjacent to | |
2234 | the character doesn't inherit PROPERTY if NONSTICKINESS is non-nil, | |
518c0b83 JB |
2235 | inherits it if NONSTICKINESS is nil. The `front-sticky' and |
2236 | `rear-nonsticky' properties of the character override NONSTICKINESS. */); | |
cabf1cac SM |
2237 | /* Text properties `syntax-table'and `display' should be nonsticky |
2238 | by default. */ | |
98ebf860 | 2239 | Vtext_property_default_nonsticky |
cabf1cac SM |
2240 | = Fcons (Fcons (intern_c_string ("syntax-table"), Qt), |
2241 | Fcons (Fcons (intern_c_string ("display"), Qt), Qnil)); | |
abc2f676 | 2242 | |
318d2fa8 RS |
2243 | staticpro (&interval_insert_behind_hooks); |
2244 | staticpro (&interval_insert_in_front_hooks); | |
2245 | interval_insert_behind_hooks = Qnil; | |
2246 | interval_insert_in_front_hooks = Qnil; | |
2247 | ||
81f6e55f | 2248 | |
d418ef42 JA |
2249 | /* Common attributes one might give text */ |
2250 | ||
cd3520a4 JB |
2251 | DEFSYM (Qforeground, "foreground"); |
2252 | DEFSYM (Qbackground, "background"); | |
2253 | DEFSYM (Qfont, "font"); | |
2254 | DEFSYM (Qstipple, "stipple"); | |
2255 | DEFSYM (Qunderline, "underline"); | |
2256 | DEFSYM (Qread_only, "read-only"); | |
2257 | DEFSYM (Qinvisible, "invisible"); | |
2258 | DEFSYM (Qintangible, "intangible"); | |
2259 | DEFSYM (Qcategory, "category"); | |
2260 | DEFSYM (Qlocal_map, "local-map"); | |
2261 | DEFSYM (Qfront_sticky, "front-sticky"); | |
2262 | DEFSYM (Qrear_nonsticky, "rear-nonsticky"); | |
2263 | DEFSYM (Qmouse_face, "mouse-face"); | |
2264 | DEFSYM (Qminibuffer_prompt, "minibuffer-prompt"); | |
d418ef42 JA |
2265 | |
2266 | /* Properties that text might use to specify certain actions */ | |
2267 | ||
cd3520a4 JB |
2268 | DEFSYM (Qmouse_left, "mouse-left"); |
2269 | DEFSYM (Qmouse_entered, "mouse-entered"); | |
2270 | DEFSYM (Qpoint_left, "point-left"); | |
2271 | DEFSYM (Qpoint_entered, "point-entered"); | |
d418ef42 JA |
2272 | |
2273 | defsubr (&Stext_properties_at); | |
5fbe2a44 | 2274 | defsubr (&Sget_text_property); |
eb769fd7 | 2275 | defsubr (&Sget_char_property); |
97a1bc63 | 2276 | defsubr (&Sget_char_property_and_overlay); |
fcab51aa RS |
2277 | defsubr (&Snext_char_property_change); |
2278 | defsubr (&Sprevious_char_property_change); | |
b7e047fb MB |
2279 | defsubr (&Snext_single_char_property_change); |
2280 | defsubr (&Sprevious_single_char_property_change); | |
d418ef42 | 2281 | defsubr (&Snext_property_change); |
9c79dd1b | 2282 | defsubr (&Snext_single_property_change); |
d418ef42 | 2283 | defsubr (&Sprevious_property_change); |
9c79dd1b | 2284 | defsubr (&Sprevious_single_property_change); |
d418ef42 | 2285 | defsubr (&Sadd_text_properties); |
d4b530ad | 2286 | defsubr (&Sput_text_property); |
d418ef42 JA |
2287 | defsubr (&Sset_text_properties); |
2288 | defsubr (&Sremove_text_properties); | |
11713b6d | 2289 | defsubr (&Sremove_list_of_text_properties); |
ad9c1940 JB |
2290 | defsubr (&Stext_property_any); |
2291 | defsubr (&Stext_property_not_all); | |
d418ef42 | 2292 | } |