(mouse-buffer-menu-mode-groups): Added "Version Control" group.
[emacs.git] / src / textprop.c
blobf49d670267fa00c4e66f7a33f02f02d2947fde05
1 /* Interface code for dealing with text properties.
2 Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "intervals.h"
25 #include "buffer.h"
26 #include "window.h"
28 #ifndef NULL
29 #define NULL (void *)0
30 #endif
32 /* Test for membership, allowing for t (actually any non-cons) to mean the
33 universal set. */
35 #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
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.
42 set_properties needs to deal with the interval property cache.
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. */
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;
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;
63 /* Sticky properties */
64 Lisp_Object Qfront_sticky, Qrear_nonsticky;
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)))
71 Lisp_Object Vinhibit_point_motion_hooks;
72 Lisp_Object Vdefault_text_properties;
73 Lisp_Object Vtext_property_default_nonsticky;
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;
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. */
84 static void
85 text_read_only ()
87 Fsignal (Qtext_read_only, Qnil);
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.
99 When OBJECT is a string, we increment *BEGIN and *END
100 to make them origin-one.
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.
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. */
115 #define soft 0
116 #define hard 1
118 INTERVAL
119 validate_interval_range (object, begin, end, force)
120 Lisp_Object object, *begin, *end;
121 int force;
123 register INTERVAL i;
124 int searchpos;
126 CHECK_STRING_OR_BUFFER (object, 0);
127 CHECK_NUMBER_COERCE_MARKER (*begin, 0);
128 CHECK_NUMBER_COERCE_MARKER (*end, 0);
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;
135 if (XINT (*begin) > XINT (*end))
137 Lisp_Object n;
138 n = *begin;
139 *begin = *end;
140 *end = n;
143 if (BUFFERP (object))
145 register struct buffer *b = XBUFFER (object);
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);
152 /* If there's no text, there are no properties. */
153 if (BUF_BEGV (b) == BUF_ZV (b))
154 return NULL_INTERVAL;
156 searchpos = XINT (*begin);
158 else
160 register struct Lisp_String *s = XSTRING (object);
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;
170 if (s->size == 0)
171 return NULL_INTERVAL;
173 searchpos = XINT (*begin);
176 if (NULL_INTERVAL_P (i))
177 return (force ? create_root_interval (object) : i);
179 return find_interval (i, searchpos);
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. */
186 static Lisp_Object
187 validate_plist (list)
188 Lisp_Object list;
190 if (NILP (list))
191 return Qnil;
193 if (CONSP (list))
195 register int i;
196 register Lisp_Object tail;
197 for (i = 0, tail = list; !NILP (tail); i++)
199 tail = Fcdr (tail);
200 QUIT;
202 if (i & 1)
203 error ("Odd length text property list");
204 return list;
207 return Fcons (list, Fcons (Qnil, Qnil));
210 /* Return nonzero if interval I has all the properties,
211 with the same values, of list PLIST. */
213 static int
214 interval_has_all_properties (plist, i)
215 Lisp_Object plist;
216 INTERVAL i;
218 register Lisp_Object tail1, tail2, sym1;
219 register int found;
221 /* Go through each element of PLIST. */
222 for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
224 sym1 = Fcar (tail1);
225 found = 0;
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)))
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;
236 /* Property has same value on both lists; go to next one. */
237 found = 1;
238 break;
241 if (! found)
242 return 0;
245 return 1;
248 /* Return nonzero if the plist of interval I has any of the
249 properties of PLIST, regardless of their values. */
251 static INLINE int
252 interval_has_some_properties (plist, i)
253 Lisp_Object plist;
254 INTERVAL i;
256 register Lisp_Object tail1, tail2, sym;
258 /* Go through each element of PLIST. */
259 for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
261 sym = Fcar (tail1);
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;
269 return 0;
272 /* Changing the plists of individual intervals. */
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;
280 Lisp_Object value;
282 while (PLIST_ELT_P (plist, value))
283 if (EQ (XCAR (plist), prop))
284 return XCAR (value);
285 else
286 plist = XCDR (value);
288 return Qunbound;
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. */
295 static void
296 set_properties (properties, interval, object)
297 Lisp_Object properties, object;
298 INTERVAL interval;
300 Lisp_Object sym, value;
302 if (BUFFERP (object))
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)))
312 record_property_change (interval->position, LENGTH (interval),
313 XCAR (sym), XCAR (value),
314 object);
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))
324 record_property_change (interval->position, LENGTH (interval),
325 XCAR (sym), Qnil,
326 object);
330 /* Store new properties. */
331 interval->plist = Fcopy_sequence (properties);
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.
338 OBJECT should be the string or buffer the interval is in.
340 Return nonzero if this changes I (i.e., if any members of PLIST
341 are actually added to I's plist) */
343 static int
344 add_properties (plist, i, object)
345 Lisp_Object plist;
346 INTERVAL i;
347 Lisp_Object object;
349 Lisp_Object tail1, tail2, sym1, val1;
350 register int changed = 0;
351 register int found;
352 struct gcpro gcpro1, gcpro2, gcpro3;
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);
362 /* Go through each element of PLIST. */
363 for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
365 sym1 = Fcar (tail1);
366 val1 = Fcar (Fcdr (tail1));
367 found = 0;
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)))
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;
377 this_cdr = Fcdr (tail2);
378 /* Found the property. Now check its value. */
379 found = 1;
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;
386 /* Record this change in the buffer, for undo purposes. */
387 if (BUFFERP (object))
389 record_property_change (i->position, LENGTH (i),
390 sym1, Fcar (this_cdr), object);
393 /* I's property has a different value -- change it */
394 Fsetcar (this_cdr, val1);
395 changed++;
396 break;
399 if (! found)
401 /* Record this change in the buffer, for undo purposes. */
402 if (BUFFERP (object))
404 record_property_change (i->position, LENGTH (i),
405 sym1, Qnil, object);
407 i->plist = Fcons (sym1, Fcons (val1, i->plist));
408 changed++;
412 UNGCPRO;
414 return changed;
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. */
421 static int
422 remove_properties (plist, i, object)
423 Lisp_Object plist;
424 INTERVAL i;
425 Lisp_Object object;
427 register Lisp_Object tail1, tail2, sym, current_plist;
428 register int changed = 0;
430 current_plist = i->plist;
431 /* Go through each element of plist. */
432 for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
434 sym = Fcar (tail1);
436 /* First, remove the symbol if its at the head of the list */
437 while (! NILP (current_plist) && EQ (sym, Fcar (current_plist)))
439 if (BUFFERP (object))
441 record_property_change (i->position, LENGTH (i),
442 sym, Fcar (Fcdr (current_plist)),
443 object);
446 current_plist = Fcdr (Fcdr (current_plist));
447 changed++;
450 /* Go through i's plist, looking for sym */
451 tail2 = current_plist;
452 while (! NILP (tail2))
454 register Lisp_Object this;
455 this = Fcdr (Fcdr (tail2));
456 if (EQ (sym, Fcar (this)))
458 if (BUFFERP (object))
460 record_property_change (i->position, LENGTH (i),
461 sym, Fcar (Fcdr (this)), object);
464 Fsetcdr (Fcdr (tail2), Fcdr (Fcdr (this)));
465 changed++;
467 tail2 = this;
471 if (changed)
472 i->plist = current_plist;
473 return changed;
476 #if 0
477 /* Remove all properties from interval I. Return non-zero
478 if this changes the interval. */
480 static INLINE int
481 erase_properties (i)
482 INTERVAL i;
484 if (NILP (i->plist))
485 return 0;
487 i->plist = Qnil;
488 return 1;
490 #endif
492 /* Returns the interval of POSITION in OBJECT.
493 POSITION is BEG-based. */
495 INTERVAL
496 interval_of (position, object)
497 int position;
498 Lisp_Object object;
500 register INTERVAL i;
501 int beg, end;
503 if (NILP (object))
504 XSETBUFFER (object, current_buffer);
505 else if (EQ (object, Qt))
506 return NULL_INTERVAL;
508 CHECK_STRING_OR_BUFFER (object, 0);
510 if (BUFFERP (object))
512 register struct buffer *b = XBUFFER (object);
514 beg = BUF_BEGV (b);
515 end = BUF_ZV (b);
516 i = BUF_INTERVALS (b);
518 else
520 register struct Lisp_String *s = XSTRING (object);
522 beg = 0;
523 end = s->size;
524 i = s->intervals;
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;
532 return find_interval (i, position);
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;
544 register INTERVAL i;
546 if (NILP (object))
547 XSETBUFFER (object, current_buffer);
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;
559 return i->plist;
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;
570 return textget (Ftext_properties_at (position, object), prop);
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;
590 struct window *w = 0;
592 CHECK_NUMBER_COERCE_MARKER (position, 0);
594 if (NILP (object))
595 XSETBUFFER (object, current_buffer);
597 if (WINDOWP (object))
599 w = XWINDOW (object);
600 object = w->buffer;
602 if (BUFFERP (object))
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;
611 set_buffer_temp (XBUFFER (object));
613 /* First try with room for 40 overlays. */
614 len = 40;
615 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
617 noverlays = overlays_at (posn, 0, &overlay_vec, &len,
618 &next_overlay, NULL, 0);
620 /* If there are more than 40,
621 make enough space for all, and try again. */
622 if (noverlays > len)
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);
629 noverlays = sort_overlays (overlay_vec, noverlays, w);
631 set_buffer_temp (obuf);
633 /* Now check the overlays in order of decreasing priority. */
634 while (--noverlays >= 0)
636 tem = Foverlay_get (overlay_vec[noverlays], prop);
637 if (!NILP (tem))
639 if (overlay)
640 /* Return the overlay we got the property from. */
641 *overlay = overlay_vec[noverlays];
642 return tem;
647 if (overlay)
648 /* Indicate that the return value is not from an overlay. */
649 *overlay = Qnil;
651 /* Not a buffer, or no appropriate overlay, so fall through to the
652 simpler case. */
653 return Fget_text_property (position, prop, object);
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;
668 return get_char_property_and_overlay (position, prop, object, 0);
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).
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;
684 Lisp_Object temp;
686 temp = Fnext_overlay_change (position);
687 if (! NILP (limit))
689 CHECK_NUMBER (limit, 2);
690 if (XINT (limit) < XINT (temp))
691 temp = limit;
693 return Fnext_property_change (position, Qnil, temp);
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).
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;
709 Lisp_Object temp;
711 temp = Fprevious_overlay_change (position);
712 if (! NILP (limit))
714 CHECK_NUMBER (limit, 2);
715 if (XINT (limit) > XINT (temp))
716 temp = limit;
718 return Fprevious_property_change (position, Qnil, temp);
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;
736 if (STRINGP (object))
738 position = Fnext_single_property_change (position, prop, object, limit);
739 if (NILP (position))
741 if (NILP (limit))
742 position = make_number (XSTRING (object)->size);
743 else
744 position = limit;
747 else
749 Lisp_Object initial_value, value;
750 int count = specpdl_ptr - specpdl;
752 if (! NILP (object))
753 CHECK_BUFFER (object, 0);
755 if (BUFFERP (object) && current_buffer != XBUFFER (object))
757 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
758 Fset_buffer (object);
761 initial_value = Fget_char_property (position, prop, object);
763 if (NILP (limit))
764 XSETFASTINT (limit, BUF_ZV (current_buffer));
765 else
766 CHECK_NUMBER_COERCE_MARKER (limit, 0);
768 for (;;)
770 position = Fnext_char_property_change (position, limit);
771 if (XFASTINT (position) >= XFASTINT (limit)) {
772 position = limit;
773 break;
776 value = Fget_char_property (position, prop, object);
777 if (!EQ (value, initial_value))
778 break;
781 unbind_to (count, Qnil);
784 return position;
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;
802 if (STRINGP (object))
804 position = Fprevious_single_property_change (position, prop, object, limit);
805 if (NILP (position))
807 if (NILP (limit))
808 position = make_number (XSTRING (object)->size);
809 else
810 position = limit;
813 else
815 int count = specpdl_ptr - specpdl;
817 if (! NILP (object))
818 CHECK_BUFFER (object, 0);
820 if (BUFFERP (object) && current_buffer != XBUFFER (object))
822 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
823 Fset_buffer (object);
826 if (NILP (limit))
827 XSETFASTINT (limit, BUF_BEGV (current_buffer));
828 else
829 CHECK_NUMBER_COERCE_MARKER (limit, 0);
831 if (XFASTINT (position) <= XFASTINT (limit))
832 position = limit;
833 else
835 Lisp_Object initial_value =
836 Fget_char_property (make_number (XFASTINT (position) - 1),
837 prop, object);
839 for (;;)
841 position = Fprevious_char_property_change (position, limit);
843 if (XFASTINT (position) <= XFASTINT (limit))
845 position = limit;
846 break;
848 else
850 Lisp_Object value =
851 Fget_char_property (make_number (XFASTINT (position) - 1),
852 prop, object);
854 if (!EQ (value, initial_value))
855 break;
860 unbind_to (count, Qnil);
863 return position;
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.
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;
880 register INTERVAL i, next;
882 if (NILP (object))
883 XSETBUFFER (object, current_buffer);
885 if (! NILP (limit) && ! EQ (limit, Qt))
886 CHECK_NUMBER_COERCE_MARKER (limit, 0);
888 i = validate_interval_range (object, &position, &position, soft);
890 /* If LIMIT is t, return start of next interval--don't
891 bother checking further intervals. */
892 if (EQ (limit, Qt))
894 if (NULL_INTERVAL_P (i))
895 next = i;
896 else
897 next = next_interval (i);
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;
908 if (NULL_INTERVAL_P (i))
909 return limit;
911 next = next_interval (i);
913 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next)
914 && (NILP (limit) || next->position < XFASTINT (limit)))
915 next = next_interval (next);
917 if (NULL_INTERVAL_P (next))
918 return limit;
919 if (! NILP (limit) && !(next->position < XFASTINT (limit)))
920 return limit;
922 XSETFASTINT (position, next->position);
923 return position;
926 /* Return 1 if there's a change in some property between BEG and END. */
929 property_change_between_p (beg, end)
930 int beg, end;
932 register INTERVAL i, next;
933 Lisp_Object object, pos;
935 XSETBUFFER (object, current_buffer);
936 XSETFASTINT (pos, beg);
938 i = validate_interval_range (object, &pos, &pos, soft);
939 if (NULL_INTERVAL_P (i))
940 return 0;
942 next = next_interval (i);
943 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
945 next = next_interval (next);
946 if (NULL_INTERVAL_P (next))
947 return 0;
948 if (next->position >= end)
949 return 0;
952 if (NULL_INTERVAL_P (next))
953 return 0;
955 return 1;
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.
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;
973 register INTERVAL i, next;
974 register Lisp_Object here_val;
976 if (NILP (object))
977 XSETBUFFER (object, current_buffer);
979 if (!NILP (limit))
980 CHECK_NUMBER_COERCE_MARKER (limit, 0);
982 i = validate_interval_range (object, &position, &position, soft);
983 if (NULL_INTERVAL_P (i))
984 return limit;
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);
993 if (NULL_INTERVAL_P (next))
994 return limit;
995 if (! NILP (limit) && !(next->position < XFASTINT (limit)))
996 return limit;
998 return make_number (next->position);
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.
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;
1015 register INTERVAL i, previous;
1017 if (NILP (object))
1018 XSETBUFFER (object, current_buffer);
1020 if (!NILP (limit))
1021 CHECK_NUMBER_COERCE_MARKER (limit, 0);
1023 i = validate_interval_range (object, &position, &position, soft);
1024 if (NULL_INTERVAL_P (i))
1025 return limit;
1027 /* Start with the interval containing the char before point. */
1028 if (i->position == XFASTINT (position))
1029 i = previous_interval (i);
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;
1042 return make_number (previous->position + LENGTH (previous));
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.
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;
1060 register INTERVAL i, previous;
1061 register Lisp_Object here_val;
1063 if (NILP (object))
1064 XSETBUFFER (object, current_buffer);
1066 if (!NILP (limit))
1067 CHECK_NUMBER_COERCE_MARKER (limit, 0);
1069 i = validate_interval_range (object, &position, &position, soft);
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);
1075 if (NULL_INTERVAL_P (i))
1076 return limit;
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;
1091 return make_number (previous->position + LENGTH (previous));
1094 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
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;
1107 register INTERVAL i, unchanged;
1108 register int s, len, modified = 0;
1109 struct gcpro gcpro1;
1111 properties = validate_plist (properties);
1112 if (NILP (properties))
1113 return Qnil;
1115 if (NILP (object))
1116 XSETBUFFER (object, current_buffer);
1118 i = validate_interval_range (object, &start, &end, hard);
1119 if (NULL_INTERVAL_P (i))
1120 return Qnil;
1122 s = XINT (start);
1123 len = XINT (end) - s;
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);
1129 /* If we're not starting on an interval boundary, we have to
1130 split this interval. */
1131 if (i->position != s)
1133 /* If this interval already has the properties, we can
1134 skip it. */
1135 if (interval_has_all_properties (properties, i))
1137 int got = (LENGTH (i) - (s - i->position));
1138 if (got >= len)
1139 RETURN_UNGCPRO (Qnil);
1140 len -= got;
1141 i = next_interval (i);
1143 else
1145 unchanged = i;
1146 i = split_interval_right (unchanged, s - unchanged->position);
1147 copy_properties (unchanged, i);
1151 if (BUFFERP (object))
1152 modify_region (XBUFFER (object), XINT (start), XINT (end));
1154 /* We are at the beginning of interval I, with LEN chars to scan. */
1155 for (;;)
1157 if (i == 0)
1158 abort ();
1160 if (LENGTH (i) >= len)
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;
1167 if (interval_has_all_properties (properties, i))
1169 if (BUFFERP (object))
1170 signal_after_change (XINT (start), XINT (end) - XINT (start),
1171 XINT (end) - XINT (start));
1173 return modified ? Qt : Qnil;
1176 if (LENGTH (i) == len)
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;
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;
1196 len -= LENGTH (i);
1197 modified += add_properties (properties, i, object);
1198 i = next_interval (i);
1202 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
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;
1214 Fadd_text_properties (start, end,
1215 Fcons (property, Fcons (value, Qnil)),
1216 object);
1217 return Qnil;
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;
1232 return set_text_properties (start, end, properties, object, Qt);
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. */
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;
1247 register INTERVAL i, unchanged;
1248 register INTERVAL prev_changed = NULL_INTERVAL;
1249 register int s, len;
1250 Lisp_Object ostart, oend;
1252 ostart = start;
1253 oend = end;
1255 properties = validate_plist (properties);
1257 if (NILP (object))
1258 XSETBUFFER (object, current_buffer);
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)
1266 if (! XSTRING (object)->intervals)
1267 return Qt;
1269 XSTRING (object)->intervals = 0;
1270 return Qt;
1273 i = validate_interval_range (object, &start, &end, soft);
1275 if (NULL_INTERVAL_P (i))
1277 /* If buffer has no properties, and we want none, return now. */
1278 if (NILP (properties))
1279 return Qnil;
1281 /* Restore the original START and END values
1282 because validate_interval_range increments them for strings. */
1283 start = ostart;
1284 end = oend;
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;
1292 s = XINT (start);
1293 len = XINT (end) - s;
1295 if (BUFFERP (object))
1296 modify_region (XBUFFER (object), XINT (start), XINT (end));
1298 if (i->position != s)
1300 unchanged = i;
1301 i = split_interval_right (unchanged, s - unchanged->position);
1303 if (LENGTH (i) > len)
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));
1312 return Qt;
1315 set_properties (properties, i, object);
1317 if (LENGTH (i) == len)
1319 if (BUFFERP (object) && !NILP (signal_after_change_p))
1320 signal_after_change (XINT (start), XINT (end) - XINT (start),
1321 XINT (end) - XINT (start));
1323 return Qt;
1326 prev_changed = i;
1327 len -= LENGTH (i);
1328 i = next_interval (i);
1331 /* We are starting at the beginning of an interval, I */
1332 while (len > 0)
1334 if (i == 0)
1335 abort ();
1337 if (LENGTH (i) >= len)
1339 if (LENGTH (i) > len)
1340 i = split_interval_left (i, len);
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;
1354 len -= LENGTH (i);
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);
1365 i = next_interval (i);
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;
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;
1386 register INTERVAL i, unchanged;
1387 register int s, len, modified = 0;
1389 if (NILP (object))
1390 XSETBUFFER (object, current_buffer);
1392 i = validate_interval_range (object, &start, &end, soft);
1393 if (NULL_INTERVAL_P (i))
1394 return Qnil;
1396 s = XINT (start);
1397 len = XINT (end) - s;
1399 if (i->position != s)
1401 /* No properties on this first interval -- return if
1402 it covers the entire region. */
1403 if (! interval_has_some_properties (properties, i))
1405 int got = (LENGTH (i) - (s - i->position));
1406 if (got >= len)
1407 return Qnil;
1408 len -= got;
1409 i = next_interval (i);
1411 /* Split away the beginning of this interval; what we don't
1412 want to modify. */
1413 else
1415 unchanged = i;
1416 i = split_interval_right (unchanged, s - unchanged->position);
1417 copy_properties (unchanged, i);
1421 if (BUFFERP (object))
1422 modify_region (XBUFFER (object), XINT (start), XINT (end));
1424 /* We are at the beginning of an interval, with len to scan */
1425 for (;;)
1427 if (i == 0)
1428 abort ();
1430 if (LENGTH (i) >= len)
1432 if (! interval_has_some_properties (properties, i))
1433 return modified ? Qt : Qnil;
1435 if (LENGTH (i) == len)
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;
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;
1455 len -= LENGTH (i);
1456 modified += remove_properties (properties, i, object);
1457 i = next_interval (i);
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;
1471 register INTERVAL i;
1472 register int e, pos;
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);
1481 while (! NULL_INTERVAL_P (i))
1483 if (i->position >= e)
1484 break;
1485 if (EQ (textget (i->plist, property), value))
1487 pos = i->position;
1488 if (pos < XINT (start))
1489 pos = XINT (start);
1490 return make_number (pos);
1492 i = next_interval (i);
1494 return Qnil;
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;
1507 register INTERVAL i;
1508 register int s, e;
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);
1518 while (! NULL_INTERVAL_P (i))
1520 if (i->position >= e)
1521 break;
1522 if (! EQ (textget (i->plist, property), value))
1524 if (i->position > s)
1525 s = i->position;
1526 return make_number (s);
1528 i = next_interval (i);
1530 return Qnil;
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?
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. */
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. */
1547 /* Note this can GC when DEST is a buffer. */
1549 Lisp_Object
1550 copy_text_properties (start, end, src, pos, dest, prop)
1551 Lisp_Object start, end, src, pos, dest, prop;
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;
1560 i = validate_interval_range (src, &start, &end, soft);
1561 if (NULL_INTERVAL_P (i))
1562 return Qnil;
1564 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1566 Lisp_Object dest_start, dest_end;
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);
1575 s = XINT (start);
1576 e = XINT (end);
1577 p = XINT (pos);
1579 stuff = Qnil;
1581 while (s < e)
1583 e2 = i->position + LENGTH (i);
1584 if (e2 > e)
1585 e2 = e;
1586 len = e2 - s;
1588 plist = i->plist;
1589 if (! NILP (prop))
1590 while (! NILP (plist))
1592 if (EQ (Fcar (plist), prop))
1594 plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
1595 break;
1597 plist = Fcdr (Fcdr (plist));
1599 if (! NILP (plist))
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);
1609 i = next_interval (i);
1610 if (NULL_INTERVAL_P (i))
1611 break;
1613 p += len;
1614 s = i->position;
1617 GCPRO2 (stuff, dest);
1619 while (! NILP (stuff))
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);
1629 UNGCPRO;
1631 return modified ? Qt : Qnil;
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. */
1642 Lisp_Object
1643 text_property_list (object, start, end, prop)
1644 Lisp_Object object, start, end, prop;
1646 struct interval *i;
1647 Lisp_Object result;
1649 result = Qnil;
1651 i = validate_interval_range (object, &start, &end, soft);
1652 if (!NULL_INTERVAL_P (i))
1654 int s = XINT (start);
1655 int e = XINT (end);
1657 while (s < e)
1659 int interval_end, len;
1660 Lisp_Object plist;
1662 interval_end = i->position + LENGTH (i);
1663 if (interval_end > e)
1664 interval_end = e;
1665 len = interval_end - s;
1667 plist = i->plist;
1669 if (!NILP (prop))
1670 for (; !NILP (plist); plist = Fcdr (Fcdr (plist)))
1671 if (EQ (Fcar (plist), prop))
1673 plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
1674 break;
1677 if (!NILP (plist))
1678 result = Fcons (Fcons (make_number (s),
1679 Fcons (make_number (s + len),
1680 Fcons (plist, Qnil))),
1681 result);
1683 i = next_interval (i);
1684 if (NULL_INTERVAL_P (i))
1685 break;
1686 s = i->position;
1690 return result;
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. */
1701 add_text_properties_from_list (object, list, delta)
1702 Lisp_Object object, list, delta;
1704 struct gcpro gcpro1, gcpro2;
1705 int modified_p = 0;
1707 GCPRO2 (list, object);
1709 for (; CONSP (list); list = XCDR (list))
1711 Lisp_Object item, start, end, plist, tem;
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)));
1718 tem = Fadd_text_properties (start, end, plist, object);
1719 if (!NILP (tem))
1720 modified_p = 1;
1723 UNGCPRO;
1724 return modified_p;
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. */
1733 void
1734 extend_property_ranges (list, old_end, new_end)
1735 Lisp_Object list, old_end, new_end;
1737 for (; CONSP (list); list = XCDR (list))
1739 Lisp_Object item, end;
1741 item = XCAR (list);
1742 end = XCAR (XCDR (item));
1744 if (EQ (end, old_end))
1745 XSETCAR (XCDR (item), new_end);
1751 /* Call the modification hook functions in LIST, each with START and END. */
1753 static void
1754 call_mod_hooks (list, start, end)
1755 Lisp_Object list, start, end;
1757 struct gcpro gcpro1;
1758 GCPRO1 (list);
1759 while (!NILP (list))
1761 call2 (Fcar (list), start, end);
1762 list = Fcdr (list);
1764 UNGCPRO;
1767 /* Check for read-only intervals between character positions START ... END,
1768 in BUF, and signal an error if we find one.
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. */
1775 void
1776 verify_interval_modification (buf, start, end)
1777 struct buffer *buf;
1778 int start, end;
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;
1787 hooks = Qnil;
1788 prev_mod_hooks = Qnil;
1789 mod_hooks = Qnil;
1791 interval_insert_behind_hooks = Qnil;
1792 interval_insert_in_front_hooks = Qnil;
1794 if (NULL_INTERVAL_P (intervals))
1795 return;
1797 if (start > end)
1799 int temp = start;
1800 start = end;
1801 end = temp;
1804 /* For an insert operation, check the two chars around the position. */
1805 if (start == end)
1807 INTERVAL prev = NULL;
1808 Lisp_Object before, after;
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);
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;
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))
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)
1835 if (! NULL_INTERVAL_P (i))
1837 after = textget (i->plist, Qread_only);
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)))
1845 Lisp_Object tem;
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 ();
1855 if (! NULL_INTERVAL_P (prev))
1857 before = textget (prev->plist, Qread_only);
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)))
1865 Lisp_Object tem;
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 ();
1875 else if (! NULL_INTERVAL_P (i))
1877 after = textget (i->plist, Qread_only);
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)))
1884 Lisp_Object tem;
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 ();
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 ();
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);
1909 else
1911 /* Loop over intervals on or next to START...END,
1912 collecting their hooks. */
1914 i = find_interval (intervals, start);
1917 if (! INTERVAL_WRITABLE_P (i))
1918 text_read_only ();
1920 if (!inhibit_modification_hooks)
1922 mod_hooks = textget (i->plist, Qmodification_hooks);
1923 if (! NILP (mod_hooks) && ! EQ (mod_hooks, prev_mod_hooks))
1925 hooks = Fcons (mod_hooks, hooks);
1926 prev_mod_hooks = mod_hooks;
1930 i = next_interval (i);
1932 /* Keep going thru the interval containing the char before END. */
1933 while (! NULL_INTERVAL_P (i) && i->position < end);
1935 if (!inhibit_modification_hooks)
1937 GCPRO1 (hooks);
1938 hooks = Fnreverse (hooks);
1939 while (! EQ (hooks, Qnil))
1941 call_mod_hooks (Fcar (hooks), make_number (start),
1942 make_number (end));
1943 hooks = Fcdr (hooks);
1945 UNGCPRO;
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. */
1955 void
1956 report_interval_modification (start, end)
1957 Lisp_Object start, end;
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);
1967 void
1968 syms_of_textprop ()
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;
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;
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).
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;
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;
1998 /* Common attributes one might give text */
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");
2027 /* Properties that text might use to specify certain actions */
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");
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); */