Commit | Line | Data |
---|---|---|
d418ef42 | 1 | /* Interface code for dealing with text properties. |
cdf3e5a2 | 2 | Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc. |
d418ef42 JA |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
71dfa9f4 | 8 | the Free Software Foundation; either version 2, or (at your option) |
d418ef42 JA |
9 | any later version. |
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 | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
d418ef42 | 20 | |
18160b98 | 21 | #include <config.h> |
d418ef42 JA |
22 | #include "lisp.h" |
23 | #include "intervals.h" | |
24 | #include "buffer.h" | |
f5957179 | 25 | #include "window.h" |
59a486ab RS |
26 | |
27 | #ifndef NULL | |
28 | #define NULL (void *)0 | |
29 | #endif | |
318d2fa8 RS |
30 | |
31 | /* Test for membership, allowing for t (actually any non-cons) to mean the | |
32 | universal set. */ | |
33 | ||
34 | #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set)) | |
d418ef42 JA |
35 | \f |
36 | ||
37 | /* NOTES: previous- and next- property change will have to skip | |
38 | zero-length intervals if they are implemented. This could be done | |
39 | inside next_interval and previous_interval. | |
40 | ||
9c79dd1b JA |
41 | set_properties needs to deal with the interval property cache. |
42 | ||
d418ef42 | 43 | It is assumed that for any interval plist, a property appears |
d4b530ad | 44 | only once on the list. Although some code i.e., remove_properties, |
d418ef42 | 45 | handles the more general case, the uniqueness of properties is |
eb8c3be9 | 46 | necessary for the system to remain consistent. This requirement |
cdf3e5a2 | 47 | is enforced by the subrs installing properties onto the intervals. */ |
d418ef42 | 48 | |
25013c26 JA |
49 | /* The rest of the file is within this conditional */ |
50 | #ifdef USE_TEXT_PROPERTIES | |
d418ef42 | 51 | \f |
cdf3e5a2 | 52 | /* Types of hooks. */ |
d418ef42 JA |
53 | Lisp_Object Qmouse_left; |
54 | Lisp_Object Qmouse_entered; | |
55 | Lisp_Object Qpoint_left; | |
56 | Lisp_Object Qpoint_entered; | |
dc70cea7 RS |
57 | Lisp_Object Qcategory; |
58 | Lisp_Object Qlocal_map; | |
d418ef42 | 59 | |
cdf3e5a2 | 60 | /* Visual properties text (including strings) may have. */ |
d418ef42 | 61 | Lisp_Object Qforeground, Qbackground, Qfont, Qunderline, Qstipple; |
46b4e741 | 62 | Lisp_Object Qinvisible, Qread_only, Qintangible; |
19e1c426 RS |
63 | |
64 | /* Sticky properties */ | |
65 | Lisp_Object Qfront_sticky, Qrear_nonsticky; | |
d7b4e137 JB |
66 | |
67 | /* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to | |
68 | the o1's cdr. Otherwise, return zero. This is handy for | |
69 | traversing plists. */ | |
8c8c10fe | 70 | #define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCONS (o1)->cdr, CONSP (o2))) |
d7b4e137 | 71 | |
688a5a0f | 72 | Lisp_Object Vinhibit_point_motion_hooks; |
ad1b2f20 | 73 | Lisp_Object Vdefault_text_properties; |
688a5a0f | 74 | |
318d2fa8 RS |
75 | /* verify_interval_modification saves insertion hooks here |
76 | to be run later by report_interval_modification. */ | |
77 | Lisp_Object interval_insert_behind_hooks; | |
78 | Lisp_Object interval_insert_in_front_hooks; | |
d418ef42 | 79 | \f |
ac876a79 JA |
80 | /* Extract the interval at the position pointed to by BEGIN from |
81 | OBJECT, a string or buffer. Additionally, check that the positions | |
82 | pointed to by BEGIN and END are within the bounds of OBJECT, and | |
83 | reverse them if *BEGIN is greater than *END. The objects pointed | |
84 | to by BEGIN and END may be integers or markers; if the latter, they | |
85 | are coerced to integers. | |
d418ef42 | 86 | |
d4b530ad RS |
87 | When OBJECT is a string, we increment *BEGIN and *END |
88 | to make them origin-one. | |
89 | ||
d418ef42 JA |
90 | Note that buffer points don't correspond to interval indices. |
91 | For example, point-max is 1 greater than the index of the last | |
92 | character. This difference is handled in the caller, which uses | |
93 | the validated points to determine a length, and operates on that. | |
94 | Exceptions are Ftext_properties_at, Fnext_property_change, and | |
95 | Fprevious_property_change which call this function with BEGIN == END. | |
96 | Handle this case specially. | |
97 | ||
98 | If FORCE is soft (0), it's OK to return NULL_INTERVAL. Otherwise, | |
ac876a79 JA |
99 | create an interval tree for OBJECT if one doesn't exist, provided |
100 | the object actually contains text. In the current design, if there | |
d4b530ad | 101 | is no text, there can be no text properties. */ |
d418ef42 JA |
102 | |
103 | #define soft 0 | |
104 | #define hard 1 | |
105 | ||
106 | static INTERVAL | |
107 | validate_interval_range (object, begin, end, force) | |
108 | Lisp_Object object, *begin, *end; | |
109 | int force; | |
110 | { | |
111 | register INTERVAL i; | |
d4b530ad RS |
112 | int searchpos; |
113 | ||
d418ef42 JA |
114 | CHECK_STRING_OR_BUFFER (object, 0); |
115 | CHECK_NUMBER_COERCE_MARKER (*begin, 0); | |
116 | CHECK_NUMBER_COERCE_MARKER (*end, 0); | |
117 | ||
118 | /* If we are asked for a point, but from a subr which operates | |
cdf3e5a2 | 119 | on a range, then return nothing. */ |
64a49ca7 | 120 | if (EQ (*begin, *end) && begin != end) |
d418ef42 JA |
121 | return NULL_INTERVAL; |
122 | ||
123 | if (XINT (*begin) > XINT (*end)) | |
124 | { | |
d4b530ad RS |
125 | Lisp_Object n; |
126 | n = *begin; | |
d418ef42 | 127 | *begin = *end; |
d4b530ad | 128 | *end = n; |
d418ef42 JA |
129 | } |
130 | ||
5d2fa46f | 131 | if (BUFFERP (object)) |
d418ef42 JA |
132 | { |
133 | register struct buffer *b = XBUFFER (object); | |
134 | ||
d418ef42 JA |
135 | if (!(BUF_BEGV (b) <= XINT (*begin) && XINT (*begin) <= XINT (*end) |
136 | && XINT (*end) <= BUF_ZV (b))) | |
137 | args_out_of_range (*begin, *end); | |
866bf246 | 138 | i = BUF_INTERVALS (b); |
d418ef42 | 139 | |
cdf3e5a2 | 140 | /* If there's no text, there are no properties. */ |
d4b530ad RS |
141 | if (BUF_BEGV (b) == BUF_ZV (b)) |
142 | return NULL_INTERVAL; | |
143 | ||
144 | searchpos = XINT (*begin); | |
d418ef42 JA |
145 | } |
146 | else | |
147 | { | |
148 | register struct Lisp_String *s = XSTRING (object); | |
149 | ||
d4b530ad | 150 | if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end) |
d418ef42 JA |
151 | && XINT (*end) <= s->size)) |
152 | args_out_of_range (*begin, *end); | |
d4b530ad RS |
153 | /* User-level Positions in strings start with 0, |
154 | but the interval code always wants positions starting with 1. */ | |
330e7312 | 155 | XSETFASTINT (*begin, XFASTINT (*begin) + 1); |
b1e94638 | 156 | if (begin != end) |
330e7312 | 157 | XSETFASTINT (*end, XFASTINT (*end) + 1); |
d418ef42 | 158 | i = s->intervals; |
d4b530ad RS |
159 | |
160 | if (s->size == 0) | |
161 | return NULL_INTERVAL; | |
162 | ||
163 | searchpos = XINT (*begin); | |
d418ef42 JA |
164 | } |
165 | ||
166 | if (NULL_INTERVAL_P (i)) | |
167 | return (force ? create_root_interval (object) : i); | |
168 | ||
d4b530ad | 169 | return find_interval (i, searchpos); |
d418ef42 JA |
170 | } |
171 | ||
172 | /* Validate LIST as a property list. If LIST is not a list, then | |
173 | make one consisting of (LIST nil). Otherwise, verify that LIST | |
cdf3e5a2 | 174 | is even numbered and thus suitable as a plist. */ |
d418ef42 JA |
175 | |
176 | static Lisp_Object | |
177 | validate_plist (list) | |
4d780c76 | 178 | Lisp_Object list; |
d418ef42 JA |
179 | { |
180 | if (NILP (list)) | |
181 | return Qnil; | |
182 | ||
183 | if (CONSP (list)) | |
184 | { | |
185 | register int i; | |
186 | register Lisp_Object tail; | |
187 | for (i = 0, tail = list; !NILP (tail); i++) | |
b1e94638 JB |
188 | { |
189 | tail = Fcdr (tail); | |
190 | QUIT; | |
191 | } | |
d418ef42 JA |
192 | if (i & 1) |
193 | error ("Odd length text property list"); | |
194 | return list; | |
195 | } | |
196 | ||
197 | return Fcons (list, Fcons (Qnil, Qnil)); | |
198 | } | |
199 | ||
d418ef42 | 200 | /* Return nonzero if interval I has all the properties, |
cdf3e5a2 | 201 | with the same values, of list PLIST. */ |
d418ef42 JA |
202 | |
203 | static int | |
204 | interval_has_all_properties (plist, i) | |
205 | Lisp_Object plist; | |
206 | INTERVAL i; | |
207 | { | |
208 | register Lisp_Object tail1, tail2, sym1, sym2; | |
209 | register int found; | |
210 | ||
cdf3e5a2 | 211 | /* Go through each element of PLIST. */ |
d418ef42 JA |
212 | for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1))) |
213 | { | |
214 | sym1 = Fcar (tail1); | |
215 | found = 0; | |
216 | ||
217 | /* Go through I's plist, looking for sym1 */ | |
218 | for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2))) | |
219 | if (EQ (sym1, Fcar (tail2))) | |
220 | { | |
221 | /* Found the same property on both lists. If the | |
cdf3e5a2 | 222 | values are unequal, return zero. */ |
734c51b2 | 223 | if (! EQ (Fcar (Fcdr (tail1)), Fcar (Fcdr (tail2)))) |
d418ef42 JA |
224 | return 0; |
225 | ||
cdf3e5a2 | 226 | /* Property has same value on both lists; go to next one. */ |
d418ef42 JA |
227 | found = 1; |
228 | break; | |
229 | } | |
230 | ||
231 | if (! found) | |
232 | return 0; | |
233 | } | |
234 | ||
235 | return 1; | |
236 | } | |
237 | ||
238 | /* Return nonzero if the plist of interval I has any of the | |
cdf3e5a2 | 239 | properties of PLIST, regardless of their values. */ |
d418ef42 JA |
240 | |
241 | static INLINE int | |
242 | interval_has_some_properties (plist, i) | |
243 | Lisp_Object plist; | |
244 | INTERVAL i; | |
245 | { | |
246 | register Lisp_Object tail1, tail2, sym; | |
247 | ||
cdf3e5a2 | 248 | /* Go through each element of PLIST. */ |
d418ef42 JA |
249 | for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1))) |
250 | { | |
251 | sym = Fcar (tail1); | |
252 | ||
253 | /* Go through i's plist, looking for tail1 */ | |
254 | for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2))) | |
255 | if (EQ (sym, Fcar (tail2))) | |
256 | return 1; | |
257 | } | |
258 | ||
259 | return 0; | |
260 | } | |
d4b530ad | 261 | \f |
d7b4e137 JB |
262 | /* Changing the plists of individual intervals. */ |
263 | ||
264 | /* Return the value of PROP in property-list PLIST, or Qunbound if it | |
265 | has none. */ | |
64a49ca7 | 266 | static Lisp_Object |
d7b4e137 | 267 | property_value (plist, prop) |
33ca3504 | 268 | Lisp_Object plist, prop; |
d7b4e137 JB |
269 | { |
270 | Lisp_Object value; | |
271 | ||
272 | while (PLIST_ELT_P (plist, value)) | |
273 | if (EQ (XCONS (plist)->car, prop)) | |
274 | return XCONS (value)->car; | |
275 | else | |
276 | plist = XCONS (value)->cdr; | |
277 | ||
278 | return Qunbound; | |
279 | } | |
280 | ||
d4b530ad RS |
281 | /* Set the properties of INTERVAL to PROPERTIES, |
282 | and record undo info for the previous values. | |
283 | OBJECT is the string or buffer that INTERVAL belongs to. */ | |
284 | ||
285 | static void | |
286 | set_properties (properties, interval, object) | |
287 | Lisp_Object properties, object; | |
288 | INTERVAL interval; | |
289 | { | |
d7b4e137 | 290 | Lisp_Object sym, value; |
d4b530ad | 291 | |
d7b4e137 | 292 | if (BUFFERP (object)) |
d4b530ad | 293 | { |
d7b4e137 JB |
294 | /* For each property in the old plist which is missing from PROPERTIES, |
295 | or has a different value in PROPERTIES, make an undo record. */ | |
296 | for (sym = interval->plist; | |
297 | PLIST_ELT_P (sym, value); | |
298 | sym = XCONS (value)->cdr) | |
299 | if (! EQ (property_value (properties, XCONS (sym)->car), | |
300 | XCONS (value)->car)) | |
f7a9275a | 301 | { |
f7a9275a RS |
302 | record_property_change (interval->position, LENGTH (interval), |
303 | XCONS (sym)->car, XCONS (value)->car, | |
304 | object); | |
305 | } | |
d7b4e137 JB |
306 | |
307 | /* For each new property that has no value at all in the old plist, | |
308 | make an undo record binding it to nil, so it will be removed. */ | |
309 | for (sym = properties; | |
310 | PLIST_ELT_P (sym, value); | |
311 | sym = XCONS (value)->cdr) | |
312 | if (EQ (property_value (interval->plist, XCONS (sym)->car), Qunbound)) | |
f7a9275a | 313 | { |
f7a9275a RS |
314 | record_property_change (interval->position, LENGTH (interval), |
315 | XCONS (sym)->car, Qnil, | |
316 | object); | |
317 | } | |
d4b530ad RS |
318 | } |
319 | ||
320 | /* Store new properties. */ | |
321 | interval->plist = Fcopy_sequence (properties); | |
322 | } | |
d418ef42 JA |
323 | |
324 | /* Add the properties of PLIST to the interval I, or set | |
325 | the value of I's property to the value of the property on PLIST | |
326 | if they are different. | |
327 | ||
d4b530ad RS |
328 | OBJECT should be the string or buffer the interval is in. |
329 | ||
d418ef42 JA |
330 | Return nonzero if this changes I (i.e., if any members of PLIST |
331 | are actually added to I's plist) */ | |
332 | ||
d4b530ad RS |
333 | static int |
334 | add_properties (plist, i, object) | |
d418ef42 JA |
335 | Lisp_Object plist; |
336 | INTERVAL i; | |
d4b530ad | 337 | Lisp_Object object; |
d418ef42 | 338 | { |
c98da214 | 339 | Lisp_Object tail1, tail2, sym1, val1; |
d418ef42 JA |
340 | register int changed = 0; |
341 | register int found; | |
c98da214 RS |
342 | struct gcpro gcpro1, gcpro2, gcpro3; |
343 | ||
344 | tail1 = plist; | |
345 | sym1 = Qnil; | |
346 | val1 = Qnil; | |
347 | /* No need to protect OBJECT, because we can GC only in the case | |
348 | where it is a buffer, and live buffers are always protected. | |
349 | I and its plist are also protected, via OBJECT. */ | |
350 | GCPRO3 (tail1, sym1, val1); | |
d418ef42 | 351 | |
cdf3e5a2 | 352 | /* Go through each element of PLIST. */ |
d418ef42 JA |
353 | for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1))) |
354 | { | |
355 | sym1 = Fcar (tail1); | |
356 | val1 = Fcar (Fcdr (tail1)); | |
357 | found = 0; | |
358 | ||
359 | /* Go through I's plist, looking for sym1 */ | |
360 | for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2))) | |
361 | if (EQ (sym1, Fcar (tail2))) | |
362 | { | |
c98da214 RS |
363 | /* No need to gcpro, because tail2 protects this |
364 | and it must be a cons cell (we get an error otherwise). */ | |
3814ccf5 | 365 | register Lisp_Object this_cdr; |
d418ef42 | 366 | |
3814ccf5 | 367 | this_cdr = Fcdr (tail2); |
cdf3e5a2 | 368 | /* Found the property. Now check its value. */ |
d418ef42 JA |
369 | found = 1; |
370 | ||
371 | /* The properties have the same value on both lists. | |
cdf3e5a2 | 372 | Continue to the next property. */ |
734c51b2 | 373 | if (EQ (val1, Fcar (this_cdr))) |
d418ef42 JA |
374 | break; |
375 | ||
d4b530ad | 376 | /* Record this change in the buffer, for undo purposes. */ |
5d2fa46f | 377 | if (BUFFERP (object)) |
d4b530ad | 378 | { |
f7a9275a RS |
379 | record_property_change (i->position, LENGTH (i), |
380 | sym1, Fcar (this_cdr), object); | |
d4b530ad RS |
381 | } |
382 | ||
d418ef42 JA |
383 | /* I's property has a different value -- change it */ |
384 | Fsetcar (this_cdr, val1); | |
385 | changed++; | |
386 | break; | |
387 | } | |
388 | ||
389 | if (! found) | |
390 | { | |
d4b530ad | 391 | /* Record this change in the buffer, for undo purposes. */ |
5d2fa46f | 392 | if (BUFFERP (object)) |
d4b530ad | 393 | { |
f7a9275a RS |
394 | record_property_change (i->position, LENGTH (i), |
395 | sym1, Qnil, object); | |
d4b530ad | 396 | } |
d418ef42 JA |
397 | i->plist = Fcons (sym1, Fcons (val1, i->plist)); |
398 | changed++; | |
399 | } | |
400 | } | |
401 | ||
c98da214 RS |
402 | UNGCPRO; |
403 | ||
d418ef42 JA |
404 | return changed; |
405 | } | |
406 | ||
407 | /* For any members of PLIST which are properties of I, remove them | |
d4b530ad RS |
408 | from I's plist. |
409 | OBJECT is the string or buffer containing I. */ | |
d418ef42 | 410 | |
d4b530ad RS |
411 | static int |
412 | remove_properties (plist, i, object) | |
d418ef42 JA |
413 | Lisp_Object plist; |
414 | INTERVAL i; | |
d4b530ad | 415 | Lisp_Object object; |
d418ef42 | 416 | { |
3814ccf5 | 417 | register Lisp_Object tail1, tail2, sym, current_plist; |
d418ef42 JA |
418 | register int changed = 0; |
419 | ||
3814ccf5 | 420 | current_plist = i->plist; |
cdf3e5a2 | 421 | /* Go through each element of plist. */ |
d418ef42 JA |
422 | for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1))) |
423 | { | |
424 | sym = Fcar (tail1); | |
425 | ||
426 | /* First, remove the symbol if its at the head of the list */ | |
427 | while (! NILP (current_plist) && EQ (sym, Fcar (current_plist))) | |
428 | { | |
5d2fa46f | 429 | if (BUFFERP (object)) |
d4b530ad | 430 | { |
f7a9275a RS |
431 | record_property_change (i->position, LENGTH (i), |
432 | sym, Fcar (Fcdr (current_plist)), | |
433 | object); | |
d4b530ad RS |
434 | } |
435 | ||
d418ef42 JA |
436 | current_plist = Fcdr (Fcdr (current_plist)); |
437 | changed++; | |
438 | } | |
439 | ||
440 | /* Go through i's plist, looking for sym */ | |
441 | tail2 = current_plist; | |
442 | while (! NILP (tail2)) | |
443 | { | |
3814ccf5 KH |
444 | register Lisp_Object this; |
445 | this = Fcdr (Fcdr (tail2)); | |
d418ef42 JA |
446 | if (EQ (sym, Fcar (this))) |
447 | { | |
5d2fa46f | 448 | if (BUFFERP (object)) |
d4b530ad | 449 | { |
f7a9275a RS |
450 | record_property_change (i->position, LENGTH (i), |
451 | sym, Fcar (Fcdr (this)), object); | |
d4b530ad RS |
452 | } |
453 | ||
d418ef42 JA |
454 | Fsetcdr (Fcdr (tail2), Fcdr (Fcdr (this))); |
455 | changed++; | |
456 | } | |
457 | tail2 = this; | |
458 | } | |
459 | } | |
460 | ||
461 | if (changed) | |
462 | i->plist = current_plist; | |
463 | return changed; | |
464 | } | |
465 | ||
d4b530ad | 466 | #if 0 |
d418ef42 | 467 | /* Remove all properties from interval I. Return non-zero |
cdf3e5a2 | 468 | if this changes the interval. */ |
d418ef42 JA |
469 | |
470 | static INLINE int | |
471 | erase_properties (i) | |
472 | INTERVAL i; | |
473 | { | |
474 | if (NILP (i->plist)) | |
475 | return 0; | |
476 | ||
477 | i->plist = Qnil; | |
478 | return 1; | |
479 | } | |
d4b530ad | 480 | #endif |
d418ef42 | 481 | \f |
cdf3e5a2 RS |
482 | /* Returns the interval of the POSITION in OBJECT. |
483 | POSITION is BEG-based. */ | |
484 | ||
485 | INTERVAL | |
486 | interval_of (position, object) | |
487 | int position; | |
488 | Lisp_Object object; | |
489 | { | |
490 | register INTERVAL i; | |
491 | int beg, end; | |
492 | ||
493 | if (NILP (object)) | |
494 | XSETBUFFER (object, current_buffer); | |
495 | ||
496 | CHECK_STRING_OR_BUFFER (object, 0); | |
497 | ||
498 | if (BUFFERP (object)) | |
499 | { | |
500 | register struct buffer *b = XBUFFER (object); | |
501 | ||
502 | beg = BUF_BEGV (b); | |
503 | end = BUF_ZV (b); | |
504 | i = BUF_INTERVALS (b); | |
505 | } | |
506 | else | |
507 | { | |
508 | register struct Lisp_String *s = XSTRING (object); | |
509 | ||
510 | /* We expect position to be 1-based. */ | |
511 | beg = BEG; | |
512 | end = s->size + BEG; | |
513 | i = s->intervals; | |
514 | } | |
515 | ||
516 | if (!(beg <= position && position <= end)) | |
517 | args_out_of_range (position, position); | |
518 | if (beg == end || NULL_INTERVAL_P (i)) | |
519 | return NULL_INTERVAL; | |
520 | ||
521 | return find_interval (i, position); | |
522 | } | |
523 | \f | |
d418ef42 JA |
524 | DEFUN ("text-properties-at", Ftext_properties_at, |
525 | Stext_properties_at, 1, 2, 0, | |
526 | "Return the list of properties held by the character at POSITION\n\ | |
527 | in optional argument OBJECT, a string or buffer. If nil, OBJECT\n\ | |
d4b530ad RS |
528 | defaults to the current buffer.\n\ |
529 | If POSITION is at the end of OBJECT, the value is nil.") | |
1f5e848a EN |
530 | (position, object) |
531 | Lisp_Object position, object; | |
d418ef42 JA |
532 | { |
533 | register INTERVAL i; | |
d418ef42 JA |
534 | |
535 | if (NILP (object)) | |
c8a4fc3d | 536 | XSETBUFFER (object, current_buffer); |
d418ef42 | 537 | |
1f5e848a | 538 | i = validate_interval_range (object, &position, &position, soft); |
d418ef42 JA |
539 | if (NULL_INTERVAL_P (i)) |
540 | return Qnil; | |
1f5e848a | 541 | /* If POSITION is at the end of the interval, |
d4b530ad RS |
542 | it means it's the end of OBJECT. |
543 | There are no properties at the very end, | |
544 | since no character follows. */ | |
1f5e848a | 545 | if (XINT (position) == LENGTH (i) + i->position) |
d4b530ad | 546 | return Qnil; |
d418ef42 JA |
547 | |
548 | return i->plist; | |
549 | } | |
550 | ||
5fbe2a44 | 551 | DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0, |
1f5e848a | 552 | "Return the value of POSITION's property PROP, in OBJECT.\n\ |
d4b530ad RS |
553 | OBJECT is optional and defaults to the current buffer.\n\ |
554 | If POSITION is at the end of OBJECT, the value is nil.") | |
1f5e848a EN |
555 | (position, prop, object) |
556 | Lisp_Object position, object; | |
46bb7c2b | 557 | Lisp_Object prop; |
5fbe2a44 | 558 | { |
1f5e848a | 559 | return textget (Ftext_properties_at (position, object), prop); |
5fbe2a44 RS |
560 | } |
561 | ||
f5957179 | 562 | DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0, |
1f5e848a | 563 | "Return the value of POSITION's property PROP, in OBJECT.\n\ |
f5957179 | 564 | OBJECT is optional and defaults to the current buffer.\n\ |
1f5e848a | 565 | If POSITION is at the end of OBJECT, the value is nil.\n\ |
f5957179 | 566 | If OBJECT is a buffer, then overlay properties are considered as well as\n\ |
99830d63 KH |
567 | text properties.\n\ |
568 | If OBJECT is a window, then that window's buffer is used, but window-specific\n\ | |
f5957179 | 569 | overlays are considered only if they are associated with OBJECT.") |
1f5e848a EN |
570 | (position, prop, object) |
571 | Lisp_Object position, object; | |
f5957179 KH |
572 | register Lisp_Object prop; |
573 | { | |
574 | struct window *w = 0; | |
575 | ||
1f5e848a | 576 | CHECK_NUMBER_COERCE_MARKER (position, 0); |
f5957179 KH |
577 | |
578 | if (NILP (object)) | |
c8a4fc3d | 579 | XSETBUFFER (object, current_buffer); |
f5957179 KH |
580 | |
581 | if (WINDOWP (object)) | |
582 | { | |
583 | w = XWINDOW (object); | |
64a49ca7 | 584 | object = w->buffer; |
f5957179 KH |
585 | } |
586 | if (BUFFERP (object)) | |
587 | { | |
1f5e848a | 588 | int posn = XINT (position); |
f5957179 KH |
589 | int noverlays; |
590 | Lisp_Object *overlay_vec, tem; | |
591 | int next_overlay; | |
592 | int len; | |
cbc55f55 RS |
593 | struct buffer *obuf = current_buffer; |
594 | ||
595 | set_buffer_temp (XBUFFER (object)); | |
f5957179 KH |
596 | |
597 | /* First try with room for 40 overlays. */ | |
598 | len = 40; | |
599 | overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object)); | |
600 | ||
59a486ab RS |
601 | noverlays = overlays_at (posn, 0, &overlay_vec, &len, |
602 | &next_overlay, NULL); | |
f5957179 KH |
603 | |
604 | /* If there are more than 40, | |
605 | make enough space for all, and try again. */ | |
606 | if (noverlays > len) | |
607 | { | |
608 | len = noverlays; | |
609 | overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object)); | |
59a486ab RS |
610 | noverlays = overlays_at (posn, 0, &overlay_vec, &len, |
611 | &next_overlay, NULL); | |
f5957179 KH |
612 | } |
613 | noverlays = sort_overlays (overlay_vec, noverlays, w); | |
614 | ||
cbc55f55 RS |
615 | set_buffer_temp (obuf); |
616 | ||
f5957179 KH |
617 | /* Now check the overlays in order of decreasing priority. */ |
618 | while (--noverlays >= 0) | |
619 | { | |
620 | tem = Foverlay_get (overlay_vec[noverlays], prop); | |
621 | if (!NILP (tem)) | |
622 | return (tem); | |
623 | } | |
624 | } | |
625 | /* Not a buffer, or no appropriate overlay, so fall through to the | |
626 | simpler case. */ | |
1f5e848a | 627 | return (Fget_text_property (position, prop, object)); |
f5957179 | 628 | } |
fcab51aa RS |
629 | \f |
630 | DEFUN ("next-char-property-change", Fnext_char_property_change, | |
631 | Snext_char_property_change, 1, 2, 0, | |
632 | "Return the position of next text property or overlay change.\n\ | |
633 | This scans characters forward from POSITION in OBJECT till it finds\n\ | |
634 | a change in some text property, or the beginning or end of an overlay,\n\ | |
635 | and returns the position of that.\n\ | |
636 | If none is found, the function returns (point-max).\n\ | |
637 | \n\ | |
638 | If the optional third argument LIMIT is non-nil, don't search\n\ | |
639 | past position LIMIT; return LIMIT if nothing is found before LIMIT.") | |
640 | (position, limit) | |
641 | Lisp_Object position, limit; | |
642 | { | |
643 | Lisp_Object temp; | |
644 | ||
645 | temp = Fnext_overlay_change (position); | |
646 | if (! NILP (limit)) | |
647 | { | |
648 | CHECK_NUMBER (limit, 2); | |
649 | if (XINT (limit) < XINT (temp)) | |
650 | temp = limit; | |
651 | } | |
652 | return Fnext_property_change (position, Qnil, temp); | |
653 | } | |
654 | ||
655 | DEFUN ("previous-char-property-change", Fprevious_char_property_change, | |
656 | Sprevious_char_property_change, 1, 2, 0, | |
657 | "Return the position of previous text property or overlay change.\n\ | |
658 | Scans characters backward from POSITION in OBJECT till it finds\n\ | |
659 | a change in some text property, or the beginning or end of an overlay,\n\ | |
660 | and returns the position of that.\n\ | |
661 | If none is found, the function returns (point-max).\n\ | |
662 | \n\ | |
663 | If the optional third argument LIMIT is non-nil, don't search\n\ | |
664 | past position LIMIT; return LIMIT if nothing is found before LIMIT.") | |
665 | (position, limit) | |
666 | Lisp_Object position, limit; | |
667 | { | |
668 | Lisp_Object temp; | |
f5957179 | 669 | |
fcab51aa RS |
670 | temp = Fprevious_overlay_change (position); |
671 | if (! NILP (limit)) | |
672 | { | |
673 | CHECK_NUMBER (limit, 2); | |
674 | if (XINT (limit) > XINT (temp)) | |
675 | temp = limit; | |
676 | } | |
677 | return Fprevious_property_change (position, Qnil, temp); | |
678 | } | |
679 | \f | |
d418ef42 | 680 | DEFUN ("next-property-change", Fnext_property_change, |
111b637d | 681 | Snext_property_change, 1, 3, 0, |
5fbe2a44 | 682 | "Return the position of next property change.\n\ |
1f5e848a | 683 | Scans characters forward from POSITION in OBJECT till it finds\n\ |
5fbe2a44 RS |
684 | a change in some text property, then returns the position of the change.\n\ |
685 | The optional second argument OBJECT is the string or buffer to scan.\n\ | |
686 | Return nil if the property is constant all the way to the end of OBJECT.\n\ | |
1f5e848a | 687 | If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\ |
111b637d RS |
688 | If the optional third argument LIMIT is non-nil, don't search\n\ |
689 | past position LIMIT; return LIMIT if nothing is found before LIMIT.") | |
1f5e848a EN |
690 | (position, object, limit) |
691 | Lisp_Object position, object, limit; | |
d418ef42 JA |
692 | { |
693 | register INTERVAL i, next; | |
694 | ||
5fbe2a44 | 695 | if (NILP (object)) |
c8a4fc3d | 696 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 697 | |
041aa96f | 698 | if (! NILP (limit) && ! EQ (limit, Qt)) |
1387d54e KH |
699 | CHECK_NUMBER_COERCE_MARKER (limit, 0); |
700 | ||
1f5e848a | 701 | i = validate_interval_range (object, &position, &position, soft); |
d418ef42 | 702 | |
041aa96f RS |
703 | /* If LIMIT is t, return start of next interval--don't |
704 | bother checking further intervals. */ | |
705 | if (EQ (limit, Qt)) | |
706 | { | |
44214c1b RS |
707 | if (NULL_INTERVAL_P (i)) |
708 | next = i; | |
709 | else | |
710 | next = next_interval (i); | |
711 | ||
c7b6dfa6 | 712 | if (NULL_INTERVAL_P (next)) |
1f5e848a EN |
713 | XSETFASTINT (position, (STRINGP (object) |
714 | ? XSTRING (object)->size | |
715 | : BUF_ZV (XBUFFER (object)))); | |
c7b6dfa6 | 716 | else |
1f5e848a EN |
717 | XSETFASTINT (position, next->position - (STRINGP (object))); |
718 | return position; | |
041aa96f RS |
719 | } |
720 | ||
44214c1b RS |
721 | if (NULL_INTERVAL_P (i)) |
722 | return limit; | |
723 | ||
724 | next = next_interval (i); | |
725 | ||
111b637d RS |
726 | while (! NULL_INTERVAL_P (next) && intervals_equal (i, next) |
727 | && (NILP (limit) || next->position < XFASTINT (limit))) | |
d418ef42 JA |
728 | next = next_interval (next); |
729 | ||
730 | if (NULL_INTERVAL_P (next)) | |
111b637d RS |
731 | return limit; |
732 | if (! NILP (limit) && !(next->position < XFASTINT (limit))) | |
733 | return limit; | |
d418ef42 | 734 | |
1f5e848a EN |
735 | XSETFASTINT (position, next->position - (STRINGP (object))); |
736 | return position; | |
19e1c426 RS |
737 | } |
738 | ||
739 | /* Return 1 if there's a change in some property between BEG and END. */ | |
740 | ||
741 | int | |
742 | property_change_between_p (beg, end) | |
743 | int beg, end; | |
744 | { | |
745 | register INTERVAL i, next; | |
746 | Lisp_Object object, pos; | |
747 | ||
c8a4fc3d | 748 | XSETBUFFER (object, current_buffer); |
e9c4fbcd | 749 | XSETFASTINT (pos, beg); |
19e1c426 RS |
750 | |
751 | i = validate_interval_range (object, &pos, &pos, soft); | |
752 | if (NULL_INTERVAL_P (i)) | |
753 | return 0; | |
754 | ||
755 | next = next_interval (i); | |
756 | while (! NULL_INTERVAL_P (next) && intervals_equal (i, next)) | |
757 | { | |
758 | next = next_interval (next); | |
e050ef74 RS |
759 | if (NULL_INTERVAL_P (next)) |
760 | return 0; | |
19e1c426 RS |
761 | if (next->position >= end) |
762 | return 0; | |
763 | } | |
764 | ||
765 | if (NULL_INTERVAL_P (next)) | |
766 | return 0; | |
767 | ||
768 | return 1; | |
d418ef42 JA |
769 | } |
770 | ||
9c79dd1b | 771 | DEFUN ("next-single-property-change", Fnext_single_property_change, |
111b637d | 772 | Snext_single_property_change, 2, 4, 0, |
5fbe2a44 | 773 | "Return the position of next property change for a specific property.\n\ |
1f5e848a | 774 | Scans characters forward from POSITION till it finds\n\ |
5fbe2a44 RS |
775 | a change in the PROP property, then returns the position of the change.\n\ |
776 | The optional third argument OBJECT is the string or buffer to scan.\n\ | |
da625a3c | 777 | The property values are compared with `eq'.\n\ |
5fbe2a44 | 778 | Return nil if the property is constant all the way to the end of OBJECT.\n\ |
1f5e848a | 779 | If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\ |
111b637d | 780 | If the optional fourth argument LIMIT is non-nil, don't search\n\ |
5abb9556 | 781 | past position LIMIT; return LIMIT if nothing is found before LIMIT.") |
1f5e848a EN |
782 | (position, prop, object, limit) |
783 | Lisp_Object position, prop, object, limit; | |
9c79dd1b JA |
784 | { |
785 | register INTERVAL i, next; | |
786 | register Lisp_Object here_val; | |
787 | ||
5fbe2a44 | 788 | if (NILP (object)) |
c8a4fc3d | 789 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 790 | |
1387d54e KH |
791 | if (!NILP (limit)) |
792 | CHECK_NUMBER_COERCE_MARKER (limit, 0); | |
793 | ||
1f5e848a | 794 | i = validate_interval_range (object, &position, &position, soft); |
9c79dd1b | 795 | if (NULL_INTERVAL_P (i)) |
111b637d | 796 | return limit; |
9c79dd1b | 797 | |
6a0486dd | 798 | here_val = textget (i->plist, prop); |
9c79dd1b | 799 | next = next_interval (i); |
6a0486dd | 800 | while (! NULL_INTERVAL_P (next) |
111b637d RS |
801 | && EQ (here_val, textget (next->plist, prop)) |
802 | && (NILP (limit) || next->position < XFASTINT (limit))) | |
9c79dd1b JA |
803 | next = next_interval (next); |
804 | ||
805 | if (NULL_INTERVAL_P (next)) | |
111b637d RS |
806 | return limit; |
807 | if (! NILP (limit) && !(next->position < XFASTINT (limit))) | |
808 | return limit; | |
9c79dd1b | 809 | |
1f5e848a EN |
810 | XSETFASTINT (position, next->position - (STRINGP (object))); |
811 | return position; | |
9c79dd1b JA |
812 | } |
813 | ||
d418ef42 | 814 | DEFUN ("previous-property-change", Fprevious_property_change, |
111b637d | 815 | Sprevious_property_change, 1, 3, 0, |
5fbe2a44 | 816 | "Return the position of previous property change.\n\ |
1f5e848a | 817 | Scans characters backwards from POSITION in OBJECT till it finds\n\ |
5fbe2a44 RS |
818 | a change in some text property, then returns the position of the change.\n\ |
819 | The optional second argument OBJECT is the string or buffer to scan.\n\ | |
820 | Return nil if the property is constant all the way to the start of OBJECT.\n\ | |
1f5e848a | 821 | If the value is non-nil, it is a position less than POSITION, never equal.\n\n\ |
111b637d | 822 | If the optional third argument LIMIT is non-nil, don't search\n\ |
5abb9556 | 823 | back past position LIMIT; return LIMIT if nothing is found until LIMIT.") |
1f5e848a EN |
824 | (position, object, limit) |
825 | Lisp_Object position, object, limit; | |
d418ef42 JA |
826 | { |
827 | register INTERVAL i, previous; | |
828 | ||
5fbe2a44 | 829 | if (NILP (object)) |
c8a4fc3d | 830 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 831 | |
1387d54e KH |
832 | if (!NILP (limit)) |
833 | CHECK_NUMBER_COERCE_MARKER (limit, 0); | |
834 | ||
1f5e848a | 835 | i = validate_interval_range (object, &position, &position, soft); |
d418ef42 | 836 | if (NULL_INTERVAL_P (i)) |
111b637d | 837 | return limit; |
d418ef42 | 838 | |
53b7feec | 839 | /* Start with the interval containing the char before point. */ |
1f5e848a | 840 | if (i->position == XFASTINT (position)) |
53b7feec RS |
841 | i = previous_interval (i); |
842 | ||
d418ef42 | 843 | previous = previous_interval (i); |
111b637d RS |
844 | while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i) |
845 | && (NILP (limit) | |
846 | || previous->position + LENGTH (previous) > XFASTINT (limit))) | |
d418ef42 JA |
847 | previous = previous_interval (previous); |
848 | if (NULL_INTERVAL_P (previous)) | |
111b637d RS |
849 | return limit; |
850 | if (!NILP (limit) | |
851 | && !(previous->position + LENGTH (previous) > XFASTINT (limit))) | |
852 | return limit; | |
d418ef42 | 853 | |
1f5e848a | 854 | XSETFASTINT (position, (previous->position + LENGTH (previous) |
e9c4fbcd | 855 | - (STRINGP (object)))); |
1f5e848a | 856 | return position; |
d418ef42 JA |
857 | } |
858 | ||
9c79dd1b | 859 | DEFUN ("previous-single-property-change", Fprevious_single_property_change, |
111b637d | 860 | Sprevious_single_property_change, 2, 4, 0, |
5fbe2a44 | 861 | "Return the position of previous property change for a specific property.\n\ |
1f5e848a | 862 | Scans characters backward from POSITION till it finds\n\ |
5fbe2a44 RS |
863 | a change in the PROP property, then returns the position of the change.\n\ |
864 | The optional third argument OBJECT is the string or buffer to scan.\n\ | |
93fda178 | 865 | The property values are compared with `eq'.\n\ |
5fbe2a44 | 866 | Return nil if the property is constant all the way to the start of OBJECT.\n\ |
1f5e848a | 867 | If the value is non-nil, it is a position less than POSITION, never equal.\n\n\ |
111b637d | 868 | If the optional fourth argument LIMIT is non-nil, don't search\n\ |
5abb9556 | 869 | back past position LIMIT; return LIMIT if nothing is found until LIMIT.") |
1f5e848a EN |
870 | (position, prop, object, limit) |
871 | Lisp_Object position, prop, object, limit; | |
9c79dd1b JA |
872 | { |
873 | register INTERVAL i, previous; | |
874 | register Lisp_Object here_val; | |
875 | ||
5fbe2a44 | 876 | if (NILP (object)) |
c8a4fc3d | 877 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 878 | |
1387d54e KH |
879 | if (!NILP (limit)) |
880 | CHECK_NUMBER_COERCE_MARKER (limit, 0); | |
881 | ||
1f5e848a | 882 | i = validate_interval_range (object, &position, &position, soft); |
9c79dd1b | 883 | |
53b7feec | 884 | /* Start with the interval containing the char before point. */ |
1f5e848a | 885 | if (! NULL_INTERVAL_P (i) && i->position == XFASTINT (position)) |
53b7feec RS |
886 | i = previous_interval (i); |
887 | ||
6873cfa3 KH |
888 | if (NULL_INTERVAL_P (i)) |
889 | return limit; | |
890 | ||
6a0486dd | 891 | here_val = textget (i->plist, prop); |
9c79dd1b JA |
892 | previous = previous_interval (i); |
893 | while (! NULL_INTERVAL_P (previous) | |
111b637d RS |
894 | && EQ (here_val, textget (previous->plist, prop)) |
895 | && (NILP (limit) | |
896 | || previous->position + LENGTH (previous) > XFASTINT (limit))) | |
9c79dd1b JA |
897 | previous = previous_interval (previous); |
898 | if (NULL_INTERVAL_P (previous)) | |
111b637d RS |
899 | return limit; |
900 | if (!NILP (limit) | |
901 | && !(previous->position + LENGTH (previous) > XFASTINT (limit))) | |
902 | return limit; | |
9c79dd1b | 903 | |
1f5e848a | 904 | XSETFASTINT (position, (previous->position + LENGTH (previous) |
e9c4fbcd | 905 | - (STRINGP (object)))); |
1f5e848a | 906 | return position; |
9c79dd1b | 907 | } |
fcab51aa | 908 | \f |
c98da214 RS |
909 | /* Callers note, this can GC when OBJECT is a buffer (or nil). */ |
910 | ||
d418ef42 | 911 | DEFUN ("add-text-properties", Fadd_text_properties, |
5fbe2a44 RS |
912 | Sadd_text_properties, 3, 4, 0, |
913 | "Add properties to the text from START to END.\n\ | |
1f5e848a | 914 | The third argument PROPERTIES is a property list\n\ |
5fbe2a44 RS |
915 | specifying the property values to add.\n\ |
916 | The optional fourth argument, OBJECT,\n\ | |
917 | is the string or buffer containing the text.\n\ | |
918 | Return t if any property value actually changed, nil otherwise.") | |
919 | (start, end, properties, object) | |
920 | Lisp_Object start, end, properties, object; | |
d418ef42 JA |
921 | { |
922 | register INTERVAL i, unchanged; | |
caa31568 | 923 | register int s, len, modified = 0; |
c98da214 | 924 | struct gcpro gcpro1; |
d418ef42 JA |
925 | |
926 | properties = validate_plist (properties); | |
927 | if (NILP (properties)) | |
928 | return Qnil; | |
929 | ||
5fbe2a44 | 930 | if (NILP (object)) |
c8a4fc3d | 931 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 932 | |
d418ef42 JA |
933 | i = validate_interval_range (object, &start, &end, hard); |
934 | if (NULL_INTERVAL_P (i)) | |
935 | return Qnil; | |
936 | ||
937 | s = XINT (start); | |
938 | len = XINT (end) - s; | |
939 | ||
c98da214 RS |
940 | /* No need to protect OBJECT, because we GC only if it's a buffer, |
941 | and live buffers are always protected. */ | |
942 | GCPRO1 (properties); | |
943 | ||
d418ef42 | 944 | /* If we're not starting on an interval boundary, we have to |
cdf3e5a2 | 945 | split this interval. */ |
d418ef42 JA |
946 | if (i->position != s) |
947 | { | |
948 | /* If this interval already has the properties, we can | |
cdf3e5a2 | 949 | skip it. */ |
d418ef42 JA |
950 | if (interval_has_all_properties (properties, i)) |
951 | { | |
952 | int got = (LENGTH (i) - (s - i->position)); | |
953 | if (got >= len) | |
64db1307 | 954 | RETURN_UNGCPRO (Qnil); |
d418ef42 | 955 | len -= got; |
05d5b93e | 956 | i = next_interval (i); |
d418ef42 JA |
957 | } |
958 | else | |
959 | { | |
960 | unchanged = i; | |
ad9c1940 | 961 | i = split_interval_right (unchanged, s - unchanged->position); |
d418ef42 | 962 | copy_properties (unchanged, i); |
d418ef42 JA |
963 | } |
964 | } | |
965 | ||
2a631db1 RS |
966 | if (BUFFERP (object)) |
967 | modify_region (XBUFFER (object), XINT (start), XINT (end)); | |
26c76ace | 968 | |
daa5e28f | 969 | /* We are at the beginning of interval I, with LEN chars to scan. */ |
caa31568 | 970 | for (;;) |
d418ef42 | 971 | { |
d4b530ad RS |
972 | if (i == 0) |
973 | abort (); | |
974 | ||
d418ef42 JA |
975 | if (LENGTH (i) >= len) |
976 | { | |
c98da214 RS |
977 | /* We can UNGCPRO safely here, because there will be just |
978 | one more chance to gc, in the next call to add_properties, | |
979 | and after that we will not need PROPERTIES or OBJECT again. */ | |
980 | UNGCPRO; | |
981 | ||
d418ef42 | 982 | if (interval_has_all_properties (properties, i)) |
26c76ace | 983 | { |
2a631db1 RS |
984 | if (BUFFERP (object)) |
985 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
986 | XINT (end) - XINT (start)); | |
26c76ace RS |
987 | |
988 | return modified ? Qt : Qnil; | |
989 | } | |
d418ef42 JA |
990 | |
991 | if (LENGTH (i) == len) | |
992 | { | |
d4b530ad | 993 | add_properties (properties, i, object); |
2a631db1 RS |
994 | if (BUFFERP (object)) |
995 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
996 | XINT (end) - XINT (start)); | |
d418ef42 JA |
997 | return Qt; |
998 | } | |
999 | ||
1000 | /* i doesn't have the properties, and goes past the change limit */ | |
1001 | unchanged = i; | |
ad9c1940 | 1002 | i = split_interval_left (unchanged, len); |
d418ef42 | 1003 | copy_properties (unchanged, i); |
d4b530ad | 1004 | add_properties (properties, i, object); |
2a631db1 RS |
1005 | if (BUFFERP (object)) |
1006 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1007 | XINT (end) - XINT (start)); | |
d418ef42 JA |
1008 | return Qt; |
1009 | } | |
1010 | ||
1011 | len -= LENGTH (i); | |
d4b530ad | 1012 | modified += add_properties (properties, i, object); |
d418ef42 JA |
1013 | i = next_interval (i); |
1014 | } | |
1015 | } | |
1016 | ||
c98da214 RS |
1017 | /* Callers note, this can GC when OBJECT is a buffer (or nil). */ |
1018 | ||
d4b530ad RS |
1019 | DEFUN ("put-text-property", Fput_text_property, |
1020 | Sput_text_property, 4, 5, 0, | |
1021 | "Set one property of the text from START to END.\n\ | |
1f5e848a | 1022 | The third and fourth arguments PROPERTY and VALUE\n\ |
d4b530ad RS |
1023 | specify the property to add.\n\ |
1024 | The optional fifth argument, OBJECT,\n\ | |
1025 | is the string or buffer containing the text.") | |
1f5e848a EN |
1026 | (start, end, property, value, object) |
1027 | Lisp_Object start, end, property, value, object; | |
d4b530ad RS |
1028 | { |
1029 | Fadd_text_properties (start, end, | |
1f5e848a | 1030 | Fcons (property, Fcons (value, Qnil)), |
d4b530ad RS |
1031 | object); |
1032 | return Qnil; | |
1033 | } | |
1034 | ||
d418ef42 | 1035 | DEFUN ("set-text-properties", Fset_text_properties, |
5fbe2a44 RS |
1036 | Sset_text_properties, 3, 4, 0, |
1037 | "Completely replace properties of text from START to END.\n\ | |
1f5e848a | 1038 | The third argument PROPERTIES is the new property list.\n\ |
5fbe2a44 RS |
1039 | The optional fourth argument, OBJECT,\n\ |
1040 | is the string or buffer containing the text.") | |
1f5e848a EN |
1041 | (start, end, properties, object) |
1042 | Lisp_Object start, end, properties, object; | |
d418ef42 JA |
1043 | { |
1044 | register INTERVAL i, unchanged; | |
9c79dd1b | 1045 | register INTERVAL prev_changed = NULL_INTERVAL; |
d418ef42 | 1046 | register int s, len; |
33d7d0df | 1047 | Lisp_Object ostart, oend; |
26c76ace | 1048 | int have_modified = 0; |
33d7d0df RS |
1049 | |
1050 | ostart = start; | |
1051 | oend = end; | |
d418ef42 | 1052 | |
1f5e848a | 1053 | properties = validate_plist (properties); |
d418ef42 | 1054 | |
5fbe2a44 | 1055 | if (NILP (object)) |
c8a4fc3d | 1056 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1057 | |
919fa9cb RS |
1058 | /* If we want no properties for a whole string, |
1059 | get rid of its intervals. */ | |
1f5e848a | 1060 | if (NILP (properties) && STRINGP (object) |
919fa9cb RS |
1061 | && XFASTINT (start) == 0 |
1062 | && XFASTINT (end) == XSTRING (object)->size) | |
1063 | { | |
26c76ace RS |
1064 | if (! XSTRING (object)->intervals) |
1065 | return Qt; | |
1066 | ||
919fa9cb RS |
1067 | XSTRING (object)->intervals = 0; |
1068 | return Qt; | |
1069 | } | |
1070 | ||
facc570e | 1071 | i = validate_interval_range (object, &start, &end, soft); |
919fa9cb | 1072 | |
d418ef42 | 1073 | if (NULL_INTERVAL_P (i)) |
facc570e | 1074 | { |
1f5e848a EN |
1075 | /* If buffer has no properties, and we want none, return now. */ |
1076 | if (NILP (properties)) | |
facc570e RS |
1077 | return Qnil; |
1078 | ||
33d7d0df RS |
1079 | /* Restore the original START and END values |
1080 | because validate_interval_range increments them for strings. */ | |
1081 | start = ostart; | |
1082 | end = oend; | |
1083 | ||
facc570e RS |
1084 | i = validate_interval_range (object, &start, &end, hard); |
1085 | /* This can return if start == end. */ | |
1086 | if (NULL_INTERVAL_P (i)) | |
1087 | return Qnil; | |
1088 | } | |
d418ef42 JA |
1089 | |
1090 | s = XINT (start); | |
1091 | len = XINT (end) - s; | |
1092 | ||
2a631db1 RS |
1093 | if (BUFFERP (object)) |
1094 | modify_region (XBUFFER (object), XINT (start), XINT (end)); | |
26c76ace | 1095 | |
d418ef42 JA |
1096 | if (i->position != s) |
1097 | { | |
1098 | unchanged = i; | |
ad9c1940 | 1099 | i = split_interval_right (unchanged, s - unchanged->position); |
7855e674 | 1100 | |
d418ef42 JA |
1101 | if (LENGTH (i) > len) |
1102 | { | |
9c79dd1b | 1103 | copy_properties (unchanged, i); |
ad9c1940 | 1104 | i = split_interval_left (i, len); |
1f5e848a | 1105 | set_properties (properties, i, object); |
2a631db1 RS |
1106 | if (BUFFERP (object)) |
1107 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1108 | XINT (end) - XINT (start)); | |
26c76ace | 1109 | |
d418ef42 JA |
1110 | return Qt; |
1111 | } | |
1112 | ||
1f5e848a | 1113 | set_properties (properties, i, object); |
daa5e28f | 1114 | |
9c79dd1b | 1115 | if (LENGTH (i) == len) |
26c76ace | 1116 | { |
2a631db1 RS |
1117 | if (BUFFERP (object)) |
1118 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1119 | XINT (end) - XINT (start)); | |
26c76ace RS |
1120 | |
1121 | return Qt; | |
1122 | } | |
9c79dd1b JA |
1123 | |
1124 | prev_changed = i; | |
d418ef42 JA |
1125 | len -= LENGTH (i); |
1126 | i = next_interval (i); | |
1127 | } | |
1128 | ||
cd7d971d | 1129 | /* We are starting at the beginning of an interval, I */ |
7855e674 | 1130 | while (len > 0) |
d418ef42 | 1131 | { |
d4b530ad RS |
1132 | if (i == 0) |
1133 | abort (); | |
1134 | ||
d418ef42 JA |
1135 | if (LENGTH (i) >= len) |
1136 | { | |
cd7d971d | 1137 | if (LENGTH (i) > len) |
ad9c1940 | 1138 | i = split_interval_left (i, len); |
d418ef42 | 1139 | |
6f232881 RS |
1140 | /* We have to call set_properties even if we are going to |
1141 | merge the intervals, so as to make the undo records | |
1142 | and cause redisplay to happen. */ | |
1f5e848a | 1143 | set_properties (properties, i, object); |
6f232881 | 1144 | if (!NULL_INTERVAL_P (prev_changed)) |
9c79dd1b | 1145 | merge_interval_left (i); |
2a631db1 RS |
1146 | if (BUFFERP (object)) |
1147 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1148 | XINT (end) - XINT (start)); | |
d418ef42 JA |
1149 | return Qt; |
1150 | } | |
1151 | ||
1152 | len -= LENGTH (i); | |
6f232881 RS |
1153 | |
1154 | /* We have to call set_properties even if we are going to | |
1155 | merge the intervals, so as to make the undo records | |
1156 | and cause redisplay to happen. */ | |
1f5e848a | 1157 | set_properties (properties, i, object); |
9c79dd1b | 1158 | if (NULL_INTERVAL_P (prev_changed)) |
6f232881 | 1159 | prev_changed = i; |
9c79dd1b JA |
1160 | else |
1161 | prev_changed = i = merge_interval_left (i); | |
1162 | ||
d418ef42 JA |
1163 | i = next_interval (i); |
1164 | } | |
1165 | ||
2a631db1 RS |
1166 | if (BUFFERP (object)) |
1167 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1168 | XINT (end) - XINT (start)); | |
d418ef42 JA |
1169 | return Qt; |
1170 | } | |
1171 | ||
1172 | DEFUN ("remove-text-properties", Fremove_text_properties, | |
5fbe2a44 RS |
1173 | Sremove_text_properties, 3, 4, 0, |
1174 | "Remove some properties from text from START to END.\n\ | |
1f5e848a | 1175 | The third argument PROPERTIES is a property list\n\ |
5fbe2a44 | 1176 | whose property names specify the properties to remove.\n\ |
1f5e848a | 1177 | \(The values stored in PROPERTIES are ignored.)\n\ |
5fbe2a44 RS |
1178 | The optional fourth argument, OBJECT,\n\ |
1179 | is the string or buffer containing the text.\n\ | |
1180 | Return t if any property was actually removed, nil otherwise.") | |
1f5e848a EN |
1181 | (start, end, properties, object) |
1182 | Lisp_Object start, end, properties, object; | |
d418ef42 JA |
1183 | { |
1184 | register INTERVAL i, unchanged; | |
caa31568 | 1185 | register int s, len, modified = 0; |
d418ef42 | 1186 | |
5fbe2a44 | 1187 | if (NILP (object)) |
c8a4fc3d | 1188 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1189 | |
d418ef42 JA |
1190 | i = validate_interval_range (object, &start, &end, soft); |
1191 | if (NULL_INTERVAL_P (i)) | |
1192 | return Qnil; | |
1193 | ||
1194 | s = XINT (start); | |
1195 | len = XINT (end) - s; | |
9c79dd1b | 1196 | |
d418ef42 JA |
1197 | if (i->position != s) |
1198 | { | |
1199 | /* No properties on this first interval -- return if | |
cdf3e5a2 | 1200 | it covers the entire region. */ |
1f5e848a | 1201 | if (! interval_has_some_properties (properties, i)) |
d418ef42 JA |
1202 | { |
1203 | int got = (LENGTH (i) - (s - i->position)); | |
1204 | if (got >= len) | |
1205 | return Qnil; | |
1206 | len -= got; | |
05d5b93e | 1207 | i = next_interval (i); |
d418ef42 | 1208 | } |
daa5e28f RS |
1209 | /* Split away the beginning of this interval; what we don't |
1210 | want to modify. */ | |
d418ef42 JA |
1211 | else |
1212 | { | |
1213 | unchanged = i; | |
ad9c1940 | 1214 | i = split_interval_right (unchanged, s - unchanged->position); |
d418ef42 | 1215 | copy_properties (unchanged, i); |
d418ef42 JA |
1216 | } |
1217 | } | |
1218 | ||
2a631db1 RS |
1219 | if (BUFFERP (object)) |
1220 | modify_region (XBUFFER (object), XINT (start), XINT (end)); | |
26c76ace | 1221 | |
d418ef42 | 1222 | /* We are at the beginning of an interval, with len to scan */ |
caa31568 | 1223 | for (;;) |
d418ef42 | 1224 | { |
d4b530ad RS |
1225 | if (i == 0) |
1226 | abort (); | |
1227 | ||
d418ef42 JA |
1228 | if (LENGTH (i) >= len) |
1229 | { | |
1f5e848a | 1230 | if (! interval_has_some_properties (properties, i)) |
d418ef42 JA |
1231 | return modified ? Qt : Qnil; |
1232 | ||
1233 | if (LENGTH (i) == len) | |
1234 | { | |
1f5e848a | 1235 | remove_properties (properties, i, object); |
2a631db1 RS |
1236 | if (BUFFERP (object)) |
1237 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1238 | XINT (end) - XINT (start)); | |
d418ef42 JA |
1239 | return Qt; |
1240 | } | |
1241 | ||
1242 | /* i has the properties, and goes past the change limit */ | |
daa5e28f | 1243 | unchanged = i; |
ad9c1940 | 1244 | i = split_interval_left (i, len); |
d418ef42 | 1245 | copy_properties (unchanged, i); |
1f5e848a | 1246 | remove_properties (properties, i, object); |
2a631db1 RS |
1247 | if (BUFFERP (object)) |
1248 | signal_after_change (XINT (start), XINT (end) - XINT (start), | |
1249 | XINT (end) - XINT (start)); | |
d418ef42 JA |
1250 | return Qt; |
1251 | } | |
1252 | ||
1253 | len -= LENGTH (i); | |
1f5e848a | 1254 | modified += remove_properties (properties, i, object); |
d418ef42 JA |
1255 | i = next_interval (i); |
1256 | } | |
1257 | } | |
fcab51aa | 1258 | \f |
ad9c1940 JB |
1259 | DEFUN ("text-property-any", Ftext_property_any, |
1260 | Stext_property_any, 4, 5, 0, | |
1f5e848a EN |
1261 | "Check text from START to END for property PROPERTY equalling VALUE.\n\ |
1262 | If so, return the position of the first character whose property PROPERTY\n\ | |
1263 | is `eq' to VALUE. Otherwise return nil.\n\ | |
ad9c1940 JB |
1264 | The optional fifth argument, OBJECT, is the string or buffer\n\ |
1265 | containing the text.") | |
1f5e848a EN |
1266 | (start, end, property, value, object) |
1267 | Lisp_Object start, end, property, value, object; | |
ad9c1940 JB |
1268 | { |
1269 | register INTERVAL i; | |
1270 | register int e, pos; | |
1271 | ||
1272 | if (NILP (object)) | |
c8a4fc3d | 1273 | XSETBUFFER (object, current_buffer); |
ad9c1940 | 1274 | i = validate_interval_range (object, &start, &end, soft); |
2084fddb KH |
1275 | if (NULL_INTERVAL_P (i)) |
1276 | return (!NILP (value) || EQ (start, end) ? Qnil : start); | |
ad9c1940 JB |
1277 | e = XINT (end); |
1278 | ||
1279 | while (! NULL_INTERVAL_P (i)) | |
1280 | { | |
1281 | if (i->position >= e) | |
1282 | break; | |
1f5e848a | 1283 | if (EQ (textget (i->plist, property), value)) |
ad9c1940 JB |
1284 | { |
1285 | pos = i->position; | |
1286 | if (pos < XINT (start)) | |
1287 | pos = XINT (start); | |
5d2fa46f | 1288 | return make_number (pos - (STRINGP (object))); |
ad9c1940 JB |
1289 | } |
1290 | i = next_interval (i); | |
1291 | } | |
1292 | return Qnil; | |
1293 | } | |
1294 | ||
1295 | DEFUN ("text-property-not-all", Ftext_property_not_all, | |
1296 | Stext_property_not_all, 4, 5, 0, | |
1f5e848a EN |
1297 | "Check text from START to END for property PROPERTY not equalling VALUE.\n\ |
1298 | If so, return the position of the first character whose property PROPERTY\n\ | |
1299 | is not `eq' to VALUE. Otherwise, return nil.\n\ | |
ad9c1940 JB |
1300 | The optional fifth argument, OBJECT, is the string or buffer\n\ |
1301 | containing the text.") | |
1f5e848a EN |
1302 | (start, end, property, value, object) |
1303 | Lisp_Object start, end, property, value, object; | |
ad9c1940 JB |
1304 | { |
1305 | register INTERVAL i; | |
1306 | register int s, e; | |
1307 | ||
1308 | if (NILP (object)) | |
c8a4fc3d | 1309 | XSETBUFFER (object, current_buffer); |
ad9c1940 JB |
1310 | i = validate_interval_range (object, &start, &end, soft); |
1311 | if (NULL_INTERVAL_P (i)) | |
916a3119 | 1312 | return (NILP (value) || EQ (start, end)) ? Qnil : start; |
ad9c1940 JB |
1313 | s = XINT (start); |
1314 | e = XINT (end); | |
1315 | ||
1316 | while (! NULL_INTERVAL_P (i)) | |
1317 | { | |
1318 | if (i->position >= e) | |
1319 | break; | |
1f5e848a | 1320 | if (! EQ (textget (i->plist, property), value)) |
ad9c1940 JB |
1321 | { |
1322 | if (i->position > s) | |
1323 | s = i->position; | |
5d2fa46f | 1324 | return make_number (s - (STRINGP (object))); |
ad9c1940 JB |
1325 | } |
1326 | i = next_interval (i); | |
1327 | } | |
1328 | return Qnil; | |
1329 | } | |
fcab51aa | 1330 | \f |
5fbe2a44 RS |
1331 | #if 0 /* You can use set-text-properties for this. */ |
1332 | ||
d418ef42 | 1333 | DEFUN ("erase-text-properties", Ferase_text_properties, |
5fbe2a44 RS |
1334 | Serase_text_properties, 2, 3, 0, |
1335 | "Remove all properties from the text from START to END.\n\ | |
1336 | The optional third argument, OBJECT,\n\ | |
1337 | is the string or buffer containing the text.") | |
1338 | (start, end, object) | |
1339 | Lisp_Object start, end, object; | |
d418ef42 | 1340 | { |
cd7d971d | 1341 | register INTERVAL i; |
03ad6beb | 1342 | register INTERVAL prev_changed = NULL_INTERVAL; |
d418ef42 JA |
1343 | register int s, len, modified; |
1344 | ||
5fbe2a44 | 1345 | if (NILP (object)) |
c8a4fc3d | 1346 | XSETBUFFER (object, current_buffer); |
5fbe2a44 | 1347 | |
d418ef42 JA |
1348 | i = validate_interval_range (object, &start, &end, soft); |
1349 | if (NULL_INTERVAL_P (i)) | |
1350 | return Qnil; | |
1351 | ||
1352 | s = XINT (start); | |
1353 | len = XINT (end) - s; | |
7855e674 | 1354 | |
d418ef42 JA |
1355 | if (i->position != s) |
1356 | { | |
7855e674 | 1357 | register int got; |
cd7d971d | 1358 | register INTERVAL unchanged = i; |
d418ef42 | 1359 | |
cdf3e5a2 | 1360 | /* If there are properties here, then this text will be modified. */ |
cd7d971d | 1361 | if (! NILP (i->plist)) |
d418ef42 | 1362 | { |
ad9c1940 | 1363 | i = split_interval_right (unchanged, s - unchanged->position); |
7855e674 | 1364 | i->plist = Qnil; |
d418ef42 | 1365 | modified++; |
7855e674 JA |
1366 | |
1367 | if (LENGTH (i) > len) | |
1368 | { | |
ad9c1940 | 1369 | i = split_interval_right (i, len); |
7855e674 JA |
1370 | copy_properties (unchanged, i); |
1371 | return Qt; | |
1372 | } | |
1373 | ||
1374 | if (LENGTH (i) == len) | |
1375 | return Qt; | |
1376 | ||
1377 | got = LENGTH (i); | |
d418ef42 | 1378 | } |
cd7d971d JA |
1379 | /* If the text of I is without any properties, and contains |
1380 | LEN or more characters, then we may return without changing | |
1381 | anything.*/ | |
7855e674 JA |
1382 | else if (LENGTH (i) - (s - i->position) <= len) |
1383 | return Qnil; | |
cd7d971d | 1384 | /* The amount of text to change extends past I, so just note |
cdf3e5a2 | 1385 | how much we've gotten. */ |
7855e674 JA |
1386 | else |
1387 | got = LENGTH (i) - (s - i->position); | |
d418ef42 JA |
1388 | |
1389 | len -= got; | |
7855e674 | 1390 | prev_changed = i; |
d418ef42 JA |
1391 | i = next_interval (i); |
1392 | } | |
1393 | ||
cdf3e5a2 | 1394 | /* We are starting at the beginning of an interval, I. */ |
d418ef42 JA |
1395 | while (len > 0) |
1396 | { | |
7855e674 | 1397 | if (LENGTH (i) >= len) |
d418ef42 | 1398 | { |
cd7d971d JA |
1399 | /* If I has no properties, simply merge it if possible. */ |
1400 | if (NILP (i->plist)) | |
7855e674 JA |
1401 | { |
1402 | if (! NULL_INTERVAL_P (prev_changed)) | |
1403 | merge_interval_left (i); | |
d418ef42 | 1404 | |
7855e674 JA |
1405 | return modified ? Qt : Qnil; |
1406 | } | |
1407 | ||
cd7d971d | 1408 | if (LENGTH (i) > len) |
ad9c1940 | 1409 | i = split_interval_left (i, len); |
7855e674 JA |
1410 | if (! NULL_INTERVAL_P (prev_changed)) |
1411 | merge_interval_left (i); | |
cd7d971d JA |
1412 | else |
1413 | i->plist = Qnil; | |
7855e674 | 1414 | |
cd7d971d | 1415 | return Qt; |
d418ef42 JA |
1416 | } |
1417 | ||
cd7d971d | 1418 | /* Here if we still need to erase past the end of I */ |
d418ef42 | 1419 | len -= LENGTH (i); |
7855e674 JA |
1420 | if (NULL_INTERVAL_P (prev_changed)) |
1421 | { | |
1422 | modified += erase_properties (i); | |
1423 | prev_changed = i; | |
1424 | } | |
1425 | else | |
1426 | { | |
cd7d971d | 1427 | modified += ! NILP (i->plist); |
cdf3e5a2 | 1428 | /* Merging I will give it the properties of PREV_CHANGED. */ |
7855e674 JA |
1429 | prev_changed = i = merge_interval_left (i); |
1430 | } | |
1431 | ||
d418ef42 JA |
1432 | i = next_interval (i); |
1433 | } | |
1434 | ||
1435 | return modified ? Qt : Qnil; | |
1436 | } | |
5fbe2a44 | 1437 | #endif /* 0 */ |
d418ef42 | 1438 | |
15e4954b JB |
1439 | /* I don't think this is the right interface to export; how often do you |
1440 | want to do something like this, other than when you're copying objects | |
1441 | around? | |
1442 | ||
1443 | I think it would be better to have a pair of functions, one which | |
1444 | returns the text properties of a region as a list of ranges and | |
1445 | plists, and another which applies such a list to another object. */ | |
1446 | ||
c98da214 RS |
1447 | /* Add properties from SRC to SRC of SRC, starting at POS in DEST. |
1448 | SRC and DEST may each refer to strings or buffers. | |
1449 | Optional sixth argument PROP causes only that property to be copied. | |
1450 | Properties are copied to DEST as if by `add-text-properties'. | |
1451 | Return t if any property value actually changed, nil otherwise. */ | |
1452 | ||
1453 | /* Note this can GC when DEST is a buffer. */ | |
fcab51aa | 1454 | \f |
15e4954b JB |
1455 | Lisp_Object |
1456 | copy_text_properties (start, end, src, pos, dest, prop) | |
1457 | Lisp_Object start, end, src, pos, dest, prop; | |
1458 | { | |
1459 | INTERVAL i; | |
1460 | Lisp_Object res; | |
1461 | Lisp_Object stuff; | |
1462 | Lisp_Object plist; | |
1463 | int s, e, e2, p, len, modified = 0; | |
c98da214 | 1464 | struct gcpro gcpro1, gcpro2; |
15e4954b JB |
1465 | |
1466 | i = validate_interval_range (src, &start, &end, soft); | |
1467 | if (NULL_INTERVAL_P (i)) | |
1468 | return Qnil; | |
1469 | ||
1470 | CHECK_NUMBER_COERCE_MARKER (pos, 0); | |
1471 | { | |
1472 | Lisp_Object dest_start, dest_end; | |
1473 | ||
1474 | dest_start = pos; | |
e9c4fbcd | 1475 | XSETFASTINT (dest_end, XINT (dest_start) + (XINT (end) - XINT (start))); |
15e4954b JB |
1476 | /* Apply this to a copy of pos; it will try to increment its arguments, |
1477 | which we don't want. */ | |
1478 | validate_interval_range (dest, &dest_start, &dest_end, soft); | |
1479 | } | |
1480 | ||
1481 | s = XINT (start); | |
1482 | e = XINT (end); | |
1483 | p = XINT (pos); | |
1484 | ||
1485 | stuff = Qnil; | |
1486 | ||
1487 | while (s < e) | |
1488 | { | |
1489 | e2 = i->position + LENGTH (i); | |
1490 | if (e2 > e) | |
1491 | e2 = e; | |
1492 | len = e2 - s; | |
1493 | ||
1494 | plist = i->plist; | |
1495 | if (! NILP (prop)) | |
1496 | while (! NILP (plist)) | |
1497 | { | |
1498 | if (EQ (Fcar (plist), prop)) | |
1499 | { | |
1500 | plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil)); | |
1501 | break; | |
1502 | } | |
1503 | plist = Fcdr (Fcdr (plist)); | |
1504 | } | |
1505 | if (! NILP (plist)) | |
1506 | { | |
1507 | /* Must defer modifications to the interval tree in case src | |
cdf3e5a2 | 1508 | and dest refer to the same string or buffer. */ |
15e4954b JB |
1509 | stuff = Fcons (Fcons (make_number (p), |
1510 | Fcons (make_number (p + len), | |
1511 | Fcons (plist, Qnil))), | |
1512 | stuff); | |
1513 | } | |
1514 | ||
1515 | i = next_interval (i); | |
1516 | if (NULL_INTERVAL_P (i)) | |
1517 | break; | |
1518 | ||
1519 | p += len; | |
1520 | s = i->position; | |
1521 | } | |
1522 | ||
c98da214 RS |
1523 | GCPRO2 (stuff, dest); |
1524 | ||
15e4954b JB |
1525 | while (! NILP (stuff)) |
1526 | { | |
1527 | res = Fcar (stuff); | |
1528 | res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)), | |
1529 | Fcar (Fcdr (Fcdr (res))), dest); | |
1530 | if (! NILP (res)) | |
1531 | modified++; | |
1532 | stuff = Fcdr (stuff); | |
1533 | } | |
1534 | ||
c98da214 RS |
1535 | UNGCPRO; |
1536 | ||
15e4954b JB |
1537 | return modified ? Qt : Qnil; |
1538 | } | |
318d2fa8 RS |
1539 | \f |
1540 | /* Call the modification hook functions in LIST, each with START and END. */ | |
1541 | ||
1542 | static void | |
1543 | call_mod_hooks (list, start, end) | |
1544 | Lisp_Object list, start, end; | |
1545 | { | |
1546 | struct gcpro gcpro1; | |
1547 | GCPRO1 (list); | |
1548 | while (!NILP (list)) | |
1549 | { | |
1550 | call2 (Fcar (list), start, end); | |
1551 | list = Fcdr (list); | |
1552 | } | |
1553 | UNGCPRO; | |
1554 | } | |
1555 | ||
1556 | /* Check for read-only intervals and signal an error if we find one. | |
1557 | Then check for any modification hooks in the range START up to | |
1558 | (but not including) END. Create a list of all these hooks in | |
1559 | lexicographic order, eliminating consecutive extra copies of the | |
1560 | same hook. Then call those hooks in order, with START and END - 1 | |
1561 | as arguments. */ | |
15e4954b | 1562 | |
318d2fa8 RS |
1563 | void |
1564 | verify_interval_modification (buf, start, end) | |
1565 | struct buffer *buf; | |
1566 | int start, end; | |
1567 | { | |
1568 | register INTERVAL intervals = BUF_INTERVALS (buf); | |
1569 | register INTERVAL i, prev; | |
1570 | Lisp_Object hooks; | |
1571 | register Lisp_Object prev_mod_hooks; | |
1572 | Lisp_Object mod_hooks; | |
1573 | struct gcpro gcpro1; | |
1574 | ||
1575 | hooks = Qnil; | |
1576 | prev_mod_hooks = Qnil; | |
1577 | mod_hooks = Qnil; | |
1578 | ||
1579 | interval_insert_behind_hooks = Qnil; | |
1580 | interval_insert_in_front_hooks = Qnil; | |
1581 | ||
1582 | if (NULL_INTERVAL_P (intervals)) | |
1583 | return; | |
1584 | ||
1585 | if (start > end) | |
1586 | { | |
1587 | int temp = start; | |
1588 | start = end; | |
1589 | end = temp; | |
1590 | } | |
1591 | ||
1592 | /* For an insert operation, check the two chars around the position. */ | |
1593 | if (start == end) | |
1594 | { | |
1595 | INTERVAL prev; | |
1596 | Lisp_Object before, after; | |
1597 | ||
1598 | /* Set I to the interval containing the char after START, | |
1599 | and PREV to the interval containing the char before START. | |
1600 | Either one may be null. They may be equal. */ | |
1601 | i = find_interval (intervals, start); | |
1602 | ||
1603 | if (start == BUF_BEGV (buf)) | |
1604 | prev = 0; | |
1605 | else if (i->position == start) | |
1606 | prev = previous_interval (i); | |
1607 | else if (i->position < start) | |
1608 | prev = i; | |
1609 | if (start == BUF_ZV (buf)) | |
1610 | i = 0; | |
1611 | ||
1612 | /* If Vinhibit_read_only is set and is not a list, we can | |
1613 | skip the read_only checks. */ | |
1614 | if (NILP (Vinhibit_read_only) || CONSP (Vinhibit_read_only)) | |
1615 | { | |
1616 | /* If I and PREV differ we need to check for the read-only | |
cdf3e5a2 | 1617 | property together with its stickiness. If either I or |
318d2fa8 RS |
1618 | PREV are 0, this check is all we need. |
1619 | We have to take special care, since read-only may be | |
1620 | indirectly defined via the category property. */ | |
1621 | if (i != prev) | |
1622 | { | |
1623 | if (! NULL_INTERVAL_P (i)) | |
1624 | { | |
1625 | after = textget (i->plist, Qread_only); | |
1626 | ||
1627 | /* If interval I is read-only and read-only is | |
1628 | front-sticky, inhibit insertion. | |
1629 | Check for read-only as well as category. */ | |
1630 | if (! NILP (after) | |
1631 | && NILP (Fmemq (after, Vinhibit_read_only))) | |
1632 | { | |
1633 | Lisp_Object tem; | |
1634 | ||
1635 | tem = textget (i->plist, Qfront_sticky); | |
1636 | if (TMEM (Qread_only, tem) | |
1637 | || (NILP (Fplist_get (i->plist, Qread_only)) | |
1638 | && TMEM (Qcategory, tem))) | |
1639 | error ("Attempt to insert within read-only text"); | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | if (! NULL_INTERVAL_P (prev)) | |
1644 | { | |
1645 | before = textget (prev->plist, Qread_only); | |
1646 | ||
1647 | /* If interval PREV is read-only and read-only isn't | |
1648 | rear-nonsticky, inhibit insertion. | |
1649 | Check for read-only as well as category. */ | |
1650 | if (! NILP (before) | |
1651 | && NILP (Fmemq (before, Vinhibit_read_only))) | |
1652 | { | |
1653 | Lisp_Object tem; | |
1654 | ||
1655 | tem = textget (prev->plist, Qrear_nonsticky); | |
1656 | if (! TMEM (Qread_only, tem) | |
1657 | && (! NILP (Fplist_get (prev->plist,Qread_only)) | |
1658 | || ! TMEM (Qcategory, tem))) | |
1659 | error ("Attempt to insert within read-only text"); | |
1660 | } | |
1661 | } | |
1662 | } | |
1663 | else if (! NULL_INTERVAL_P (i)) | |
1664 | { | |
1665 | after = textget (i->plist, Qread_only); | |
1666 | ||
1667 | /* If interval I is read-only and read-only is | |
1668 | front-sticky, inhibit insertion. | |
1669 | Check for read-only as well as category. */ | |
1670 | if (! NILP (after) && NILP (Fmemq (after, Vinhibit_read_only))) | |
1671 | { | |
1672 | Lisp_Object tem; | |
1673 | ||
1674 | tem = textget (i->plist, Qfront_sticky); | |
1675 | if (TMEM (Qread_only, tem) | |
1676 | || (NILP (Fplist_get (i->plist, Qread_only)) | |
1677 | && TMEM (Qcategory, tem))) | |
1678 | error ("Attempt to insert within read-only text"); | |
1679 | ||
1680 | tem = textget (prev->plist, Qrear_nonsticky); | |
1681 | if (! TMEM (Qread_only, tem) | |
1682 | && (! NILP (Fplist_get (prev->plist, Qread_only)) | |
1683 | || ! TMEM (Qcategory, tem))) | |
1684 | error ("Attempt to insert within read-only text"); | |
1685 | } | |
1686 | } | |
1687 | } | |
1688 | ||
1689 | /* Run both insert hooks (just once if they're the same). */ | |
1690 | if (!NULL_INTERVAL_P (prev)) | |
1691 | interval_insert_behind_hooks | |
1692 | = textget (prev->plist, Qinsert_behind_hooks); | |
1693 | if (!NULL_INTERVAL_P (i)) | |
1694 | interval_insert_in_front_hooks | |
1695 | = textget (i->plist, Qinsert_in_front_hooks); | |
1696 | } | |
1697 | else | |
1698 | { | |
1699 | /* Loop over intervals on or next to START...END, | |
1700 | collecting their hooks. */ | |
1701 | ||
1702 | i = find_interval (intervals, start); | |
1703 | do | |
1704 | { | |
1705 | if (! INTERVAL_WRITABLE_P (i)) | |
1706 | error ("Attempt to modify read-only text"); | |
1707 | ||
1708 | mod_hooks = textget (i->plist, Qmodification_hooks); | |
1709 | if (! NILP (mod_hooks) && ! EQ (mod_hooks, prev_mod_hooks)) | |
1710 | { | |
1711 | hooks = Fcons (mod_hooks, hooks); | |
1712 | prev_mod_hooks = mod_hooks; | |
1713 | } | |
1714 | ||
1715 | i = next_interval (i); | |
1716 | } | |
1717 | /* Keep going thru the interval containing the char before END. */ | |
1718 | while (! NULL_INTERVAL_P (i) && i->position < end); | |
1719 | ||
1720 | GCPRO1 (hooks); | |
1721 | hooks = Fnreverse (hooks); | |
1722 | while (! EQ (hooks, Qnil)) | |
1723 | { | |
1724 | call_mod_hooks (Fcar (hooks), make_number (start), | |
1725 | make_number (end)); | |
1726 | hooks = Fcdr (hooks); | |
1727 | } | |
1728 | UNGCPRO; | |
1729 | } | |
1730 | } | |
1731 | ||
1732 | /* Run the interval hooks for an insertion. | |
1733 | verify_interval_modification chose which hooks to run; | |
1734 | this function is called after the insertion happens | |
1735 | so it can indicate the range of inserted text. */ | |
1736 | ||
1737 | void | |
1738 | report_interval_modification (start, end) | |
1739 | Lisp_Object start, end; | |
1740 | { | |
1741 | if (! NILP (interval_insert_behind_hooks)) | |
1742 | call_mod_hooks (interval_insert_behind_hooks, | |
1743 | make_number (start), make_number (end)); | |
1744 | if (! NILP (interval_insert_in_front_hooks) | |
1745 | && ! EQ (interval_insert_in_front_hooks, | |
1746 | interval_insert_behind_hooks)) | |
1747 | call_mod_hooks (interval_insert_in_front_hooks, | |
1748 | make_number (start), make_number (end)); | |
1749 | } | |
1750 | \f | |
d418ef42 JA |
1751 | void |
1752 | syms_of_textprop () | |
1753 | { | |
ad1b2f20 | 1754 | DEFVAR_LISP ("default-text-properties", &Vdefault_text_properties, |
c7dd82a3 | 1755 | "Property-list used as default values.\n\ |
ad1b2f20 BG |
1756 | The value of a property in this list is seen as the value for every\n\ |
1757 | character that does not have its own value for that property."); | |
1758 | Vdefault_text_properties = Qnil; | |
c7dd82a3 | 1759 | |
688a5a0f | 1760 | DEFVAR_LISP ("inhibit-point-motion-hooks", &Vinhibit_point_motion_hooks, |
33d7d0df RS |
1761 | "If non-nil, don't run `point-left' and `point-entered' text properties.\n\ |
1762 | This also inhibits the use of the `intangible' text property."); | |
688a5a0f | 1763 | Vinhibit_point_motion_hooks = Qnil; |
318d2fa8 RS |
1764 | |
1765 | staticpro (&interval_insert_behind_hooks); | |
1766 | staticpro (&interval_insert_in_front_hooks); | |
1767 | interval_insert_behind_hooks = Qnil; | |
1768 | interval_insert_in_front_hooks = Qnil; | |
1769 | ||
688a5a0f | 1770 | |
d418ef42 JA |
1771 | /* Common attributes one might give text */ |
1772 | ||
1773 | staticpro (&Qforeground); | |
1774 | Qforeground = intern ("foreground"); | |
1775 | staticpro (&Qbackground); | |
1776 | Qbackground = intern ("background"); | |
1777 | staticpro (&Qfont); | |
1778 | Qfont = intern ("font"); | |
1779 | staticpro (&Qstipple); | |
1780 | Qstipple = intern ("stipple"); | |
1781 | staticpro (&Qunderline); | |
1782 | Qunderline = intern ("underline"); | |
1783 | staticpro (&Qread_only); | |
1784 | Qread_only = intern ("read-only"); | |
1785 | staticpro (&Qinvisible); | |
1786 | Qinvisible = intern ("invisible"); | |
46b4e741 KH |
1787 | staticpro (&Qintangible); |
1788 | Qintangible = intern ("intangible"); | |
dc70cea7 RS |
1789 | staticpro (&Qcategory); |
1790 | Qcategory = intern ("category"); | |
1791 | staticpro (&Qlocal_map); | |
1792 | Qlocal_map = intern ("local-map"); | |
19e1c426 RS |
1793 | staticpro (&Qfront_sticky); |
1794 | Qfront_sticky = intern ("front-sticky"); | |
1795 | staticpro (&Qrear_nonsticky); | |
1796 | Qrear_nonsticky = intern ("rear-nonsticky"); | |
d418ef42 JA |
1797 | |
1798 | /* Properties that text might use to specify certain actions */ | |
1799 | ||
1800 | staticpro (&Qmouse_left); | |
1801 | Qmouse_left = intern ("mouse-left"); | |
1802 | staticpro (&Qmouse_entered); | |
1803 | Qmouse_entered = intern ("mouse-entered"); | |
1804 | staticpro (&Qpoint_left); | |
1805 | Qpoint_left = intern ("point-left"); | |
1806 | staticpro (&Qpoint_entered); | |
1807 | Qpoint_entered = intern ("point-entered"); | |
d418ef42 JA |
1808 | |
1809 | defsubr (&Stext_properties_at); | |
5fbe2a44 | 1810 | defsubr (&Sget_text_property); |
eb769fd7 | 1811 | defsubr (&Sget_char_property); |
fcab51aa RS |
1812 | defsubr (&Snext_char_property_change); |
1813 | defsubr (&Sprevious_char_property_change); | |
d418ef42 | 1814 | defsubr (&Snext_property_change); |
9c79dd1b | 1815 | defsubr (&Snext_single_property_change); |
d418ef42 | 1816 | defsubr (&Sprevious_property_change); |
9c79dd1b | 1817 | defsubr (&Sprevious_single_property_change); |
d418ef42 | 1818 | defsubr (&Sadd_text_properties); |
d4b530ad | 1819 | defsubr (&Sput_text_property); |
d418ef42 JA |
1820 | defsubr (&Sset_text_properties); |
1821 | defsubr (&Sremove_text_properties); | |
ad9c1940 JB |
1822 | defsubr (&Stext_property_any); |
1823 | defsubr (&Stext_property_not_all); | |
5fbe2a44 | 1824 | /* defsubr (&Serase_text_properties); */ |
15e4954b | 1825 | /* defsubr (&Scopy_text_properties); */ |
d418ef42 | 1826 | } |
25013c26 JA |
1827 | |
1828 | #else | |
1829 | ||
1830 | lose -- this shouldn't be compiled if USE_TEXT_PROPERTIES isn't defined | |
1831 | ||
1832 | #endif /* USE_TEXT_PROPERTIES */ |