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