* lisp/simple.el (mark-word): Mark more if repeated.
[emacs.git] / src / textprop.c
blob14fce396a2115201efa47cd50fca05b8acc29da8
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);
127 CHECK_NUMBER_COERCE_MARKER (*begin);
128 CHECK_NUMBER_COERCE_MARKER (*end);
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);
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);
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 Both overlay properties and text properties are checked.
659 OBJECT is optional and defaults to the current buffer.
660 If POSITION is at the end of OBJECT, the value is nil.
661 If OBJECT is a buffer, then overlay properties are considered as well as
662 text properties.
663 If OBJECT is a window, then that window's buffer is used, but window-specific
664 overlays are considered only if they are associated with OBJECT. */)
665 (position, prop, object)
666 Lisp_Object position, object;
667 register Lisp_Object prop;
669 return get_char_property_and_overlay (position, prop, object, 0);
672 DEFUN ("next-char-property-change", Fnext_char_property_change,
673 Snext_char_property_change, 1, 2, 0,
674 doc: /* Return the position of next text property or overlay change.
675 This scans characters forward from POSITION till it finds a change in
676 some text property, or the beginning or end of an overlay, and returns
677 the position of that.
678 If none is found, the function returns (point-max).
680 If the optional third argument LIMIT is non-nil, don't search
681 past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
682 (position, limit)
683 Lisp_Object position, limit;
685 Lisp_Object temp;
687 temp = Fnext_overlay_change (position);
688 if (! NILP (limit))
690 CHECK_NUMBER (limit);
691 if (XINT (limit) < XINT (temp))
692 temp = limit;
694 return Fnext_property_change (position, Qnil, temp);
697 DEFUN ("previous-char-property-change", Fprevious_char_property_change,
698 Sprevious_char_property_change, 1, 2, 0,
699 doc: /* Return the position of previous text property or overlay change.
700 Scans characters backward from POSITION till it finds a change in some
701 text property, or the beginning or end of an overlay, and returns the
702 position of that.
703 If none is found, the function returns (point-max).
705 If the optional third argument LIMIT is non-nil, don't search
706 past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
707 (position, limit)
708 Lisp_Object position, limit;
710 Lisp_Object temp;
712 temp = Fprevious_overlay_change (position);
713 if (! NILP (limit))
715 CHECK_NUMBER (limit);
716 if (XINT (limit) > XINT (temp))
717 temp = limit;
719 return Fprevious_property_change (position, Qnil, temp);
723 DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
724 Snext_single_char_property_change, 2, 4, 0,
725 doc: /* Return the position of next text property or overlay change for a specific property.
726 Scans characters forward from POSITION till it finds
727 a change in the PROP property, then returns the position of the change.
728 The optional third argument OBJECT is the string or buffer to scan.
729 The property values are compared with `eq'.
730 If the property is constant all the way to the end of OBJECT, return the
731 last valid position in OBJECT.
732 If the optional fourth argument LIMIT is non-nil, don't search
733 past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
734 (position, prop, object, limit)
735 Lisp_Object prop, position, object, limit;
737 if (STRINGP (object))
739 position = Fnext_single_property_change (position, prop, object, limit);
740 if (NILP (position))
742 if (NILP (limit))
743 position = make_number (XSTRING (object)->size);
744 else
745 position = limit;
748 else
750 Lisp_Object initial_value, value;
751 int count = specpdl_ptr - specpdl;
753 if (! NILP (object))
754 CHECK_BUFFER (object);
756 if (BUFFERP (object) && current_buffer != XBUFFER (object))
758 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
759 Fset_buffer (object);
762 initial_value = Fget_char_property (position, prop, object);
764 if (NILP (limit))
765 XSETFASTINT (limit, BUF_ZV (current_buffer));
766 else
767 CHECK_NUMBER_COERCE_MARKER (limit);
769 for (;;)
771 position = Fnext_char_property_change (position, limit);
772 if (XFASTINT (position) >= XFASTINT (limit)) {
773 position = limit;
774 break;
777 value = Fget_char_property (position, prop, object);
778 if (!EQ (value, initial_value))
779 break;
782 unbind_to (count, Qnil);
785 return position;
788 DEFUN ("previous-single-char-property-change",
789 Fprevious_single_char_property_change,
790 Sprevious_single_char_property_change, 2, 4, 0,
791 doc: /* Return the position of previous text property or overlay change for a specific property.
792 Scans characters backward from POSITION till it finds
793 a change in the PROP property, then returns the position of the change.
794 The optional third argument OBJECT is the string or buffer to scan.
795 The property values are compared with `eq'.
796 If the property is constant all the way to the start of OBJECT, return the
797 first valid position in OBJECT.
798 If the optional fourth argument LIMIT is non-nil, don't search
799 back past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
800 (position, prop, object, limit)
801 Lisp_Object prop, position, object, limit;
803 if (STRINGP (object))
805 position = Fprevious_single_property_change (position, prop, object, limit);
806 if (NILP (position))
808 if (NILP (limit))
809 position = make_number (XSTRING (object)->size);
810 else
811 position = limit;
814 else
816 int count = specpdl_ptr - specpdl;
818 if (! NILP (object))
819 CHECK_BUFFER (object);
821 if (BUFFERP (object) && current_buffer != XBUFFER (object))
823 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
824 Fset_buffer (object);
827 if (NILP (limit))
828 XSETFASTINT (limit, BUF_BEGV (current_buffer));
829 else
830 CHECK_NUMBER_COERCE_MARKER (limit);
832 if (XFASTINT (position) <= XFASTINT (limit))
833 position = limit;
834 else
836 Lisp_Object initial_value =
837 Fget_char_property (make_number (XFASTINT (position) - 1),
838 prop, object);
840 for (;;)
842 position = Fprevious_char_property_change (position, limit);
844 if (XFASTINT (position) <= XFASTINT (limit))
846 position = limit;
847 break;
849 else
851 Lisp_Object value =
852 Fget_char_property (make_number (XFASTINT (position) - 1),
853 prop, object);
855 if (!EQ (value, initial_value))
856 break;
861 unbind_to (count, Qnil);
864 return position;
867 DEFUN ("next-property-change", Fnext_property_change,
868 Snext_property_change, 1, 3, 0,
869 doc: /* Return the position of next property change.
870 Scans characters forward from POSITION in OBJECT till it finds
871 a change in some text property, then returns the position of the change.
872 The optional second argument OBJECT is the string or buffer to scan.
873 Return nil if the property is constant all the way to the end of OBJECT.
874 If the value is non-nil, it is a position greater than POSITION, never equal.
876 If the optional third argument LIMIT is non-nil, don't search
877 past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
878 (position, object, limit)
879 Lisp_Object position, object, limit;
881 register INTERVAL i, next;
883 if (NILP (object))
884 XSETBUFFER (object, current_buffer);
886 if (!NILP (limit) && !EQ (limit, Qt))
887 CHECK_NUMBER_COERCE_MARKER (limit);
889 i = validate_interval_range (object, &position, &position, soft);
891 /* If LIMIT is t, return start of next interval--don't
892 bother checking further intervals. */
893 if (EQ (limit, Qt))
895 if (NULL_INTERVAL_P (i))
896 next = i;
897 else
898 next = next_interval (i);
900 if (NULL_INTERVAL_P (next))
901 XSETFASTINT (position, (STRINGP (object)
902 ? XSTRING (object)->size
903 : BUF_ZV (XBUFFER (object))));
904 else
905 XSETFASTINT (position, next->position);
906 return position;
909 if (NULL_INTERVAL_P (i))
910 return limit;
912 next = next_interval (i);
914 while (!NULL_INTERVAL_P (next) && intervals_equal (i, next)
915 && (NILP (limit) || next->position < XFASTINT (limit)))
916 next = next_interval (next);
918 if (NULL_INTERVAL_P (next))
919 return limit;
920 if (NILP (limit))
921 XSETFASTINT (limit, (STRINGP (object)
922 ? XSTRING (object)->size
923 : BUF_ZV (XBUFFER (object))));
924 if (!(next->position < XFASTINT (limit)))
925 return limit;
927 XSETFASTINT (position, next->position);
928 return position;
931 /* Return 1 if there's a change in some property between BEG and END. */
934 property_change_between_p (beg, end)
935 int beg, end;
937 register INTERVAL i, next;
938 Lisp_Object object, pos;
940 XSETBUFFER (object, current_buffer);
941 XSETFASTINT (pos, beg);
943 i = validate_interval_range (object, &pos, &pos, soft);
944 if (NULL_INTERVAL_P (i))
945 return 0;
947 next = next_interval (i);
948 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
950 next = next_interval (next);
951 if (NULL_INTERVAL_P (next))
952 return 0;
953 if (next->position >= end)
954 return 0;
957 if (NULL_INTERVAL_P (next))
958 return 0;
960 return 1;
963 DEFUN ("next-single-property-change", Fnext_single_property_change,
964 Snext_single_property_change, 2, 4, 0,
965 doc: /* Return the position of next property change for a specific property.
966 Scans characters forward from POSITION till it finds
967 a change in the PROP property, then returns the position of the change.
968 The optional third argument OBJECT is the string or buffer to scan.
969 The property values are compared with `eq'.
970 Return nil if the property is constant all the way to the end of OBJECT.
971 If the value is non-nil, it is a position greater than POSITION, never equal.
973 If the optional fourth argument LIMIT is non-nil, don't search
974 past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
975 (position, prop, object, limit)
976 Lisp_Object position, prop, object, limit;
978 register INTERVAL i, next;
979 register Lisp_Object here_val;
981 if (NILP (object))
982 XSETBUFFER (object, current_buffer);
984 if (!NILP (limit))
985 CHECK_NUMBER_COERCE_MARKER (limit);
987 i = validate_interval_range (object, &position, &position, soft);
988 if (NULL_INTERVAL_P (i))
989 return limit;
991 here_val = textget (i->plist, prop);
992 next = next_interval (i);
993 while (! NULL_INTERVAL_P (next)
994 && EQ (here_val, textget (next->plist, prop))
995 && (NILP (limit) || next->position < XFASTINT (limit)))
996 next = next_interval (next);
998 if (NULL_INTERVAL_P (next))
999 return limit;
1000 if (NILP (limit))
1001 XSETFASTINT (limit, (STRINGP (object)
1002 ? XSTRING (object)->size
1003 : BUF_ZV (XBUFFER (object))));
1004 if (!(next->position < XFASTINT (limit)))
1005 return limit;
1007 return make_number (next->position);
1010 DEFUN ("previous-property-change", Fprevious_property_change,
1011 Sprevious_property_change, 1, 3, 0,
1012 doc: /* Return the position of previous property change.
1013 Scans characters backwards from POSITION in OBJECT till it finds
1014 a change in some text property, then returns the position of the change.
1015 The optional second argument OBJECT is the string or buffer to scan.
1016 Return nil if the property is constant all the way to the start of OBJECT.
1017 If the value is non-nil, it is a position less than POSITION, never equal.
1019 If the optional third argument LIMIT is non-nil, don't search
1020 back past position LIMIT; return LIMIT if nothing is found until LIMIT. */)
1021 (position, object, limit)
1022 Lisp_Object position, object, limit;
1024 register INTERVAL i, previous;
1026 if (NILP (object))
1027 XSETBUFFER (object, current_buffer);
1029 if (!NILP (limit))
1030 CHECK_NUMBER_COERCE_MARKER (limit);
1032 i = validate_interval_range (object, &position, &position, soft);
1033 if (NULL_INTERVAL_P (i))
1034 return limit;
1036 /* Start with the interval containing the char before point. */
1037 if (i->position == XFASTINT (position))
1038 i = previous_interval (i);
1040 previous = previous_interval (i);
1041 while (!NULL_INTERVAL_P (previous) && intervals_equal (previous, i)
1042 && (NILP (limit)
1043 || (previous->position + LENGTH (previous) > XFASTINT (limit))))
1044 previous = previous_interval (previous);
1045 if (NULL_INTERVAL_P (previous))
1046 return limit;
1047 if (NILP (limit))
1048 XSETFASTINT (limit, (STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object))));
1049 if (!(previous->position + LENGTH (previous) > XFASTINT (limit)))
1050 return limit;
1052 return make_number (previous->position + LENGTH (previous));
1055 DEFUN ("previous-single-property-change", Fprevious_single_property_change,
1056 Sprevious_single_property_change, 2, 4, 0,
1057 doc: /* Return the position of previous property change for a specific property.
1058 Scans characters backward from POSITION till it finds
1059 a change in the PROP property, then returns the position of the change.
1060 The optional third argument OBJECT is the string or buffer to scan.
1061 The property values are compared with `eq'.
1062 Return nil if the property is constant all the way to the start of OBJECT.
1063 If the value is non-nil, it is a position less than POSITION, never equal.
1065 If the optional fourth argument LIMIT is non-nil, don't search
1066 back past position LIMIT; return LIMIT if nothing is found until LIMIT. */)
1067 (position, prop, object, limit)
1068 Lisp_Object position, prop, object, limit;
1070 register INTERVAL i, previous;
1071 register Lisp_Object here_val;
1073 if (NILP (object))
1074 XSETBUFFER (object, current_buffer);
1076 if (!NILP (limit))
1077 CHECK_NUMBER_COERCE_MARKER (limit);
1079 i = validate_interval_range (object, &position, &position, soft);
1081 /* Start with the interval containing the char before point. */
1082 if (!NULL_INTERVAL_P (i) && i->position == XFASTINT (position))
1083 i = previous_interval (i);
1085 if (NULL_INTERVAL_P (i))
1086 return limit;
1088 here_val = textget (i->plist, prop);
1089 previous = previous_interval (i);
1090 while (!NULL_INTERVAL_P (previous)
1091 && EQ (here_val, textget (previous->plist, prop))
1092 && (NILP (limit)
1093 || (previous->position + LENGTH (previous) > XFASTINT (limit))))
1094 previous = previous_interval (previous);
1095 if (NULL_INTERVAL_P (previous))
1096 return limit;
1097 if (NILP (limit))
1098 XSETFASTINT (limit, (STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object))));
1099 if (!(previous->position + LENGTH (previous) > XFASTINT (limit)))
1100 return limit;
1102 return make_number (previous->position + LENGTH (previous));
1105 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
1107 DEFUN ("add-text-properties", Fadd_text_properties,
1108 Sadd_text_properties, 3, 4, 0,
1109 doc: /* Add properties to the text from START to END.
1110 The third argument PROPERTIES is a property list
1111 specifying the property values to add.
1112 The optional fourth argument, OBJECT,
1113 is the string or buffer containing the text.
1114 Return t if any property value actually changed, nil otherwise. */)
1115 (start, end, properties, object)
1116 Lisp_Object start, end, properties, object;
1118 register INTERVAL i, unchanged;
1119 register int s, len, modified = 0;
1120 struct gcpro gcpro1;
1122 properties = validate_plist (properties);
1123 if (NILP (properties))
1124 return Qnil;
1126 if (NILP (object))
1127 XSETBUFFER (object, current_buffer);
1129 i = validate_interval_range (object, &start, &end, hard);
1130 if (NULL_INTERVAL_P (i))
1131 return Qnil;
1133 s = XINT (start);
1134 len = XINT (end) - s;
1136 /* No need to protect OBJECT, because we GC only if it's a buffer,
1137 and live buffers are always protected. */
1138 GCPRO1 (properties);
1140 /* If we're not starting on an interval boundary, we have to
1141 split this interval. */
1142 if (i->position != s)
1144 /* If this interval already has the properties, we can
1145 skip it. */
1146 if (interval_has_all_properties (properties, i))
1148 int got = (LENGTH (i) - (s - i->position));
1149 if (got >= len)
1150 RETURN_UNGCPRO (Qnil);
1151 len -= got;
1152 i = next_interval (i);
1154 else
1156 unchanged = i;
1157 i = split_interval_right (unchanged, s - unchanged->position);
1158 copy_properties (unchanged, i);
1162 if (BUFFERP (object))
1163 modify_region (XBUFFER (object), XINT (start), XINT (end));
1165 /* We are at the beginning of interval I, with LEN chars to scan. */
1166 for (;;)
1168 if (i == 0)
1169 abort ();
1171 if (LENGTH (i) >= len)
1173 /* We can UNGCPRO safely here, because there will be just
1174 one more chance to gc, in the next call to add_properties,
1175 and after that we will not need PROPERTIES or OBJECT again. */
1176 UNGCPRO;
1178 if (interval_has_all_properties (properties, i))
1180 if (BUFFERP (object))
1181 signal_after_change (XINT (start), XINT (end) - XINT (start),
1182 XINT (end) - XINT (start));
1184 return modified ? Qt : Qnil;
1187 if (LENGTH (i) == len)
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 /* i doesn't have the properties, and goes past the change limit */
1197 unchanged = i;
1198 i = split_interval_left (unchanged, len);
1199 copy_properties (unchanged, i);
1200 add_properties (properties, i, object);
1201 if (BUFFERP (object))
1202 signal_after_change (XINT (start), XINT (end) - XINT (start),
1203 XINT (end) - XINT (start));
1204 return Qt;
1207 len -= LENGTH (i);
1208 modified += add_properties (properties, i, object);
1209 i = next_interval (i);
1213 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
1215 DEFUN ("put-text-property", Fput_text_property,
1216 Sput_text_property, 4, 5, 0,
1217 doc: /* Set one property of the text from START to END.
1218 The third and fourth arguments PROPERTY and VALUE
1219 specify the property to add.
1220 The optional fifth argument, OBJECT,
1221 is the string or buffer containing the text. */)
1222 (start, end, property, value, object)
1223 Lisp_Object start, end, property, value, object;
1225 Fadd_text_properties (start, end,
1226 Fcons (property, Fcons (value, Qnil)),
1227 object);
1228 return Qnil;
1231 DEFUN ("set-text-properties", Fset_text_properties,
1232 Sset_text_properties, 3, 4, 0,
1233 doc: /* Completely replace properties of text from START to END.
1234 The third argument PROPERTIES is the new property list.
1235 The optional fourth argument, OBJECT,
1236 is the string or buffer containing the text.
1237 If OBJECT is omitted or nil, it defaults to the current buffer.
1238 If PROPERTIES is nil, the effect is to remove all properties from
1239 the designated part of OBJECT. */)
1240 (start, end, properties, object)
1241 Lisp_Object start, end, properties, object;
1243 return set_text_properties (start, end, properties, object, Qt);
1247 /* Replace properties of text from START to END with new list of
1248 properties PROPERTIES. OBJECT is the buffer or string containing
1249 the text. OBJECT nil means use the current buffer.
1250 SIGNAL_AFTER_CHANGE_P nil means don't signal after changes. Value
1251 is non-nil if properties were replaced; it is nil if there weren't
1252 any properties to replace. */
1254 Lisp_Object
1255 set_text_properties (start, end, properties, object, signal_after_change_p)
1256 Lisp_Object start, end, properties, object, signal_after_change_p;
1258 register INTERVAL i;
1259 Lisp_Object ostart, oend;
1261 ostart = start;
1262 oend = end;
1264 properties = validate_plist (properties);
1266 if (NILP (object))
1267 XSETBUFFER (object, current_buffer);
1269 /* If we want no properties for a whole string,
1270 get rid of its intervals. */
1271 if (NILP (properties) && STRINGP (object)
1272 && XFASTINT (start) == 0
1273 && XFASTINT (end) == XSTRING (object)->size)
1275 if (! XSTRING (object)->intervals)
1276 return Qt;
1278 XSTRING (object)->intervals = 0;
1279 return Qt;
1282 i = validate_interval_range (object, &start, &end, soft);
1284 if (NULL_INTERVAL_P (i))
1286 /* If buffer has no properties, and we want none, return now. */
1287 if (NILP (properties))
1288 return Qnil;
1290 /* Restore the original START and END values
1291 because validate_interval_range increments them for strings. */
1292 start = ostart;
1293 end = oend;
1295 i = validate_interval_range (object, &start, &end, hard);
1296 /* This can return if start == end. */
1297 if (NULL_INTERVAL_P (i))
1298 return Qnil;
1301 if (BUFFERP (object))
1302 modify_region (XBUFFER (object), XINT (start), XINT (end));
1304 set_text_properties_1 (start, end, properties, object, i);
1306 if (BUFFERP (object) && !NILP (signal_after_change_p))
1307 signal_after_change (XINT (start), XINT (end) - XINT (start),
1308 XINT (end) - XINT (start));
1309 return Qt;
1312 /* Replace properties of text from START to END with new list of
1313 properties PROPERTIES. BUFFER is the buffer containing
1314 the text. This does not obey any hooks.
1315 You can provide the interval that START is located in as I,
1316 or pass NULL for I and this function will find it.
1317 START and END can be in any order. */
1319 void
1320 set_text_properties_1 (start, end, properties, buffer, i)
1321 Lisp_Object start, end, properties, buffer;
1322 INTERVAL i;
1324 register INTERVAL prev_changed = NULL_INTERVAL;
1325 register int s, len;
1326 INTERVAL unchanged;
1328 s = XINT (start);
1329 len = XINT (end) - s;
1330 if (len == 0)
1331 return;
1332 if (len < 0)
1334 s = s + len;
1335 len = - len;
1338 if (i == 0)
1339 i = find_interval (BUF_INTERVALS (XBUFFER (buffer)), s);
1341 if (i->position != s)
1343 unchanged = i;
1344 i = split_interval_right (unchanged, s - unchanged->position);
1346 if (LENGTH (i) > len)
1348 copy_properties (unchanged, i);
1349 i = split_interval_left (i, len);
1350 set_properties (properties, i, buffer);
1351 return;
1354 set_properties (properties, i, buffer);
1356 if (LENGTH (i) == len)
1357 return;
1359 prev_changed = i;
1360 len -= LENGTH (i);
1361 i = next_interval (i);
1364 /* We are starting at the beginning of an interval, I */
1365 while (len > 0)
1367 if (i == 0)
1368 abort ();
1370 if (LENGTH (i) >= len)
1372 if (LENGTH (i) > len)
1373 i = split_interval_left (i, len);
1375 /* We have to call set_properties even if we are going to
1376 merge the intervals, so as to make the undo records
1377 and cause redisplay to happen. */
1378 set_properties (properties, i, buffer);
1379 if (!NULL_INTERVAL_P (prev_changed))
1380 merge_interval_left (i);
1381 return;
1384 len -= LENGTH (i);
1386 /* We have to call set_properties even if we are going to
1387 merge the intervals, so as to make the undo records
1388 and cause redisplay to happen. */
1389 set_properties (properties, i, buffer);
1390 if (NULL_INTERVAL_P (prev_changed))
1391 prev_changed = i;
1392 else
1393 prev_changed = i = merge_interval_left (i);
1395 i = next_interval (i);
1399 DEFUN ("remove-text-properties", Fremove_text_properties,
1400 Sremove_text_properties, 3, 4, 0,
1401 doc: /* Remove some properties from text from START to END.
1402 The third argument PROPERTIES is a property list
1403 whose property names specify the properties to remove.
1404 \(The values stored in PROPERTIES are ignored.)
1405 The optional fourth argument, OBJECT,
1406 is the string or buffer containing the text.
1407 Return t if any property was actually removed, nil otherwise. */)
1408 (start, end, properties, object)
1409 Lisp_Object start, end, properties, object;
1411 register INTERVAL i, unchanged;
1412 register int s, len, modified = 0;
1414 if (NILP (object))
1415 XSETBUFFER (object, current_buffer);
1417 i = validate_interval_range (object, &start, &end, soft);
1418 if (NULL_INTERVAL_P (i))
1419 return Qnil;
1421 s = XINT (start);
1422 len = XINT (end) - s;
1424 if (i->position != s)
1426 /* No properties on this first interval -- return if
1427 it covers the entire region. */
1428 if (! interval_has_some_properties (properties, i))
1430 int got = (LENGTH (i) - (s - i->position));
1431 if (got >= len)
1432 return Qnil;
1433 len -= got;
1434 i = next_interval (i);
1436 /* Split away the beginning of this interval; what we don't
1437 want to modify. */
1438 else
1440 unchanged = i;
1441 i = split_interval_right (unchanged, s - unchanged->position);
1442 copy_properties (unchanged, i);
1446 if (BUFFERP (object))
1447 modify_region (XBUFFER (object), XINT (start), XINT (end));
1449 /* We are at the beginning of an interval, with len to scan */
1450 for (;;)
1452 if (i == 0)
1453 abort ();
1455 if (LENGTH (i) >= len)
1457 if (! interval_has_some_properties (properties, i))
1458 return modified ? Qt : Qnil;
1460 if (LENGTH (i) == len)
1462 remove_properties (properties, i, object);
1463 if (BUFFERP (object))
1464 signal_after_change (XINT (start), XINT (end) - XINT (start),
1465 XINT (end) - XINT (start));
1466 return Qt;
1469 /* i has the properties, and goes past the change limit */
1470 unchanged = i;
1471 i = split_interval_left (i, len);
1472 copy_properties (unchanged, i);
1473 remove_properties (properties, i, object);
1474 if (BUFFERP (object))
1475 signal_after_change (XINT (start), XINT (end) - XINT (start),
1476 XINT (end) - XINT (start));
1477 return Qt;
1480 len -= LENGTH (i);
1481 modified += remove_properties (properties, i, object);
1482 i = next_interval (i);
1486 DEFUN ("text-property-any", Ftext_property_any,
1487 Stext_property_any, 4, 5, 0,
1488 doc: /* Check text from START to END for property PROPERTY equalling VALUE.
1489 If so, return the position of the first character whose property PROPERTY
1490 is `eq' to VALUE. Otherwise return nil.
1491 The optional fifth argument, OBJECT, is the string or buffer
1492 containing the text. */)
1493 (start, end, property, value, object)
1494 Lisp_Object start, end, property, value, object;
1496 register INTERVAL i;
1497 register int e, pos;
1499 if (NILP (object))
1500 XSETBUFFER (object, current_buffer);
1501 i = validate_interval_range (object, &start, &end, soft);
1502 if (NULL_INTERVAL_P (i))
1503 return (!NILP (value) || EQ (start, end) ? Qnil : start);
1504 e = XINT (end);
1506 while (! NULL_INTERVAL_P (i))
1508 if (i->position >= e)
1509 break;
1510 if (EQ (textget (i->plist, property), value))
1512 pos = i->position;
1513 if (pos < XINT (start))
1514 pos = XINT (start);
1515 return make_number (pos);
1517 i = next_interval (i);
1519 return Qnil;
1522 DEFUN ("text-property-not-all", Ftext_property_not_all,
1523 Stext_property_not_all, 4, 5, 0,
1524 doc: /* Check text from START to END for property PROPERTY not equalling VALUE.
1525 If so, return the position of the first character whose property PROPERTY
1526 is not `eq' to VALUE. Otherwise, return nil.
1527 The optional fifth argument, OBJECT, is the string or buffer
1528 containing the text. */)
1529 (start, end, property, value, object)
1530 Lisp_Object start, end, property, value, object;
1532 register INTERVAL i;
1533 register int s, e;
1535 if (NILP (object))
1536 XSETBUFFER (object, current_buffer);
1537 i = validate_interval_range (object, &start, &end, soft);
1538 if (NULL_INTERVAL_P (i))
1539 return (NILP (value) || EQ (start, end)) ? Qnil : start;
1540 s = XINT (start);
1541 e = XINT (end);
1543 while (! NULL_INTERVAL_P (i))
1545 if (i->position >= e)
1546 break;
1547 if (! EQ (textget (i->plist, property), value))
1549 if (i->position > s)
1550 s = i->position;
1551 return make_number (s);
1553 i = next_interval (i);
1555 return Qnil;
1558 /* I don't think this is the right interface to export; how often do you
1559 want to do something like this, other than when you're copying objects
1560 around?
1562 I think it would be better to have a pair of functions, one which
1563 returns the text properties of a region as a list of ranges and
1564 plists, and another which applies such a list to another object. */
1566 /* Add properties from SRC to SRC of SRC, starting at POS in DEST.
1567 SRC and DEST may each refer to strings or buffers.
1568 Optional sixth argument PROP causes only that property to be copied.
1569 Properties are copied to DEST as if by `add-text-properties'.
1570 Return t if any property value actually changed, nil otherwise. */
1572 /* Note this can GC when DEST is a buffer. */
1574 Lisp_Object
1575 copy_text_properties (start, end, src, pos, dest, prop)
1576 Lisp_Object start, end, src, pos, dest, prop;
1578 INTERVAL i;
1579 Lisp_Object res;
1580 Lisp_Object stuff;
1581 Lisp_Object plist;
1582 int s, e, e2, p, len, modified = 0;
1583 struct gcpro gcpro1, gcpro2;
1585 i = validate_interval_range (src, &start, &end, soft);
1586 if (NULL_INTERVAL_P (i))
1587 return Qnil;
1589 CHECK_NUMBER_COERCE_MARKER (pos);
1591 Lisp_Object dest_start, dest_end;
1593 dest_start = pos;
1594 XSETFASTINT (dest_end, XINT (dest_start) + (XINT (end) - XINT (start)));
1595 /* Apply this to a copy of pos; it will try to increment its arguments,
1596 which we don't want. */
1597 validate_interval_range (dest, &dest_start, &dest_end, soft);
1600 s = XINT (start);
1601 e = XINT (end);
1602 p = XINT (pos);
1604 stuff = Qnil;
1606 while (s < e)
1608 e2 = i->position + LENGTH (i);
1609 if (e2 > e)
1610 e2 = e;
1611 len = e2 - s;
1613 plist = i->plist;
1614 if (! NILP (prop))
1615 while (! NILP (plist))
1617 if (EQ (Fcar (plist), prop))
1619 plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
1620 break;
1622 plist = Fcdr (Fcdr (plist));
1624 if (! NILP (plist))
1626 /* Must defer modifications to the interval tree in case src
1627 and dest refer to the same string or buffer. */
1628 stuff = Fcons (Fcons (make_number (p),
1629 Fcons (make_number (p + len),
1630 Fcons (plist, Qnil))),
1631 stuff);
1634 i = next_interval (i);
1635 if (NULL_INTERVAL_P (i))
1636 break;
1638 p += len;
1639 s = i->position;
1642 GCPRO2 (stuff, dest);
1644 while (! NILP (stuff))
1646 res = Fcar (stuff);
1647 res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)),
1648 Fcar (Fcdr (Fcdr (res))), dest);
1649 if (! NILP (res))
1650 modified++;
1651 stuff = Fcdr (stuff);
1654 UNGCPRO;
1656 return modified ? Qt : Qnil;
1660 /* Return a list representing the text properties of OBJECT between
1661 START and END. if PROP is non-nil, report only on that property.
1662 Each result list element has the form (S E PLIST), where S and E
1663 are positions in OBJECT and PLIST is a property list containing the
1664 text properties of OBJECT between S and E. Value is nil if OBJECT
1665 doesn't contain text properties between START and END. */
1667 Lisp_Object
1668 text_property_list (object, start, end, prop)
1669 Lisp_Object object, start, end, prop;
1671 struct interval *i;
1672 Lisp_Object result;
1674 result = Qnil;
1676 i = validate_interval_range (object, &start, &end, soft);
1677 if (!NULL_INTERVAL_P (i))
1679 int s = XINT (start);
1680 int e = XINT (end);
1682 while (s < e)
1684 int interval_end, len;
1685 Lisp_Object plist;
1687 interval_end = i->position + LENGTH (i);
1688 if (interval_end > e)
1689 interval_end = e;
1690 len = interval_end - s;
1692 plist = i->plist;
1694 if (!NILP (prop))
1695 for (; !NILP (plist); plist = Fcdr (Fcdr (plist)))
1696 if (EQ (Fcar (plist), prop))
1698 plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
1699 break;
1702 if (!NILP (plist))
1703 result = Fcons (Fcons (make_number (s),
1704 Fcons (make_number (s + len),
1705 Fcons (plist, Qnil))),
1706 result);
1708 i = next_interval (i);
1709 if (NULL_INTERVAL_P (i))
1710 break;
1711 s = i->position;
1715 return result;
1719 /* Add text properties to OBJECT from LIST. LIST is a list of triples
1720 (START END PLIST), where START and END are positions and PLIST is a
1721 property list containing the text properties to add. Adjust START
1722 and END positions by DELTA before adding properties. Value is
1723 non-zero if OBJECT was modified. */
1726 add_text_properties_from_list (object, list, delta)
1727 Lisp_Object object, list, delta;
1729 struct gcpro gcpro1, gcpro2;
1730 int modified_p = 0;
1732 GCPRO2 (list, object);
1734 for (; CONSP (list); list = XCDR (list))
1736 Lisp_Object item, start, end, plist, tem;
1738 item = XCAR (list);
1739 start = make_number (XINT (XCAR (item)) + XINT (delta));
1740 end = make_number (XINT (XCAR (XCDR (item))) + XINT (delta));
1741 plist = XCAR (XCDR (XCDR (item)));
1743 tem = Fadd_text_properties (start, end, plist, object);
1744 if (!NILP (tem))
1745 modified_p = 1;
1748 UNGCPRO;
1749 return modified_p;
1754 /* Modify end-points of ranges in LIST destructively. LIST is a list
1755 as returned from text_property_list. Change end-points equal to
1756 OLD_END to NEW_END. */
1758 void
1759 extend_property_ranges (list, old_end, new_end)
1760 Lisp_Object list, old_end, new_end;
1762 for (; CONSP (list); list = XCDR (list))
1764 Lisp_Object item, end;
1766 item = XCAR (list);
1767 end = XCAR (XCDR (item));
1769 if (EQ (end, old_end))
1770 XSETCAR (XCDR (item), new_end);
1776 /* Call the modification hook functions in LIST, each with START and END. */
1778 static void
1779 call_mod_hooks (list, start, end)
1780 Lisp_Object list, start, end;
1782 struct gcpro gcpro1;
1783 GCPRO1 (list);
1784 while (!NILP (list))
1786 call2 (Fcar (list), start, end);
1787 list = Fcdr (list);
1789 UNGCPRO;
1792 /* Check for read-only intervals between character positions START ... END,
1793 in BUF, and signal an error if we find one.
1795 Then check for any modification hooks in the range.
1796 Create a list of all these hooks in lexicographic order,
1797 eliminating consecutive extra copies of the same hook. Then call
1798 those hooks in order, with START and END - 1 as arguments. */
1800 void
1801 verify_interval_modification (buf, start, end)
1802 struct buffer *buf;
1803 int start, end;
1805 register INTERVAL intervals = BUF_INTERVALS (buf);
1806 register INTERVAL i;
1807 Lisp_Object hooks;
1808 register Lisp_Object prev_mod_hooks;
1809 Lisp_Object mod_hooks;
1810 struct gcpro gcpro1;
1812 hooks = Qnil;
1813 prev_mod_hooks = Qnil;
1814 mod_hooks = Qnil;
1816 interval_insert_behind_hooks = Qnil;
1817 interval_insert_in_front_hooks = Qnil;
1819 if (NULL_INTERVAL_P (intervals))
1820 return;
1822 if (start > end)
1824 int temp = start;
1825 start = end;
1826 end = temp;
1829 /* For an insert operation, check the two chars around the position. */
1830 if (start == end)
1832 INTERVAL prev = NULL;
1833 Lisp_Object before, after;
1835 /* Set I to the interval containing the char after START,
1836 and PREV to the interval containing the char before START.
1837 Either one may be null. They may be equal. */
1838 i = find_interval (intervals, start);
1840 if (start == BUF_BEGV (buf))
1841 prev = 0;
1842 else if (i->position == start)
1843 prev = previous_interval (i);
1844 else if (i->position < start)
1845 prev = i;
1846 if (start == BUF_ZV (buf))
1847 i = 0;
1849 /* If Vinhibit_read_only is set and is not a list, we can
1850 skip the read_only checks. */
1851 if (NILP (Vinhibit_read_only) || CONSP (Vinhibit_read_only))
1853 /* If I and PREV differ we need to check for the read-only
1854 property together with its stickiness. If either I or
1855 PREV are 0, this check is all we need.
1856 We have to take special care, since read-only may be
1857 indirectly defined via the category property. */
1858 if (i != prev)
1860 if (! NULL_INTERVAL_P (i))
1862 after = textget (i->plist, Qread_only);
1864 /* If interval I is read-only and read-only is
1865 front-sticky, inhibit insertion.
1866 Check for read-only as well as category. */
1867 if (! NILP (after)
1868 && NILP (Fmemq (after, Vinhibit_read_only)))
1870 Lisp_Object tem;
1872 tem = textget (i->plist, Qfront_sticky);
1873 if (TMEM (Qread_only, tem)
1874 || (NILP (Fplist_get (i->plist, Qread_only))
1875 && TMEM (Qcategory, tem)))
1876 text_read_only ();
1880 if (! NULL_INTERVAL_P (prev))
1882 before = textget (prev->plist, Qread_only);
1884 /* If interval PREV is read-only and read-only isn't
1885 rear-nonsticky, inhibit insertion.
1886 Check for read-only as well as category. */
1887 if (! NILP (before)
1888 && NILP (Fmemq (before, Vinhibit_read_only)))
1890 Lisp_Object tem;
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 ();
1900 else if (! NULL_INTERVAL_P (i))
1902 after = textget (i->plist, Qread_only);
1904 /* If interval I is read-only and read-only is
1905 front-sticky, inhibit insertion.
1906 Check for read-only as well as category. */
1907 if (! NILP (after) && NILP (Fmemq (after, Vinhibit_read_only)))
1909 Lisp_Object tem;
1911 tem = textget (i->plist, Qfront_sticky);
1912 if (TMEM (Qread_only, tem)
1913 || (NILP (Fplist_get (i->plist, Qread_only))
1914 && TMEM (Qcategory, tem)))
1915 text_read_only ();
1917 tem = textget (prev->plist, Qrear_nonsticky);
1918 if (! TMEM (Qread_only, tem)
1919 && (! NILP (Fplist_get (prev->plist, Qread_only))
1920 || ! TMEM (Qcategory, tem)))
1921 text_read_only ();
1926 /* Run both insert hooks (just once if they're the same). */
1927 if (!NULL_INTERVAL_P (prev))
1928 interval_insert_behind_hooks
1929 = textget (prev->plist, Qinsert_behind_hooks);
1930 if (!NULL_INTERVAL_P (i))
1931 interval_insert_in_front_hooks
1932 = textget (i->plist, Qinsert_in_front_hooks);
1934 else
1936 /* Loop over intervals on or next to START...END,
1937 collecting their hooks. */
1939 i = find_interval (intervals, start);
1942 if (! INTERVAL_WRITABLE_P (i))
1943 text_read_only ();
1945 if (!inhibit_modification_hooks)
1947 mod_hooks = textget (i->plist, Qmodification_hooks);
1948 if (! NILP (mod_hooks) && ! EQ (mod_hooks, prev_mod_hooks))
1950 hooks = Fcons (mod_hooks, hooks);
1951 prev_mod_hooks = mod_hooks;
1955 i = next_interval (i);
1957 /* Keep going thru the interval containing the char before END. */
1958 while (! NULL_INTERVAL_P (i) && i->position < end);
1960 if (!inhibit_modification_hooks)
1962 GCPRO1 (hooks);
1963 hooks = Fnreverse (hooks);
1964 while (! EQ (hooks, Qnil))
1966 call_mod_hooks (Fcar (hooks), make_number (start),
1967 make_number (end));
1968 hooks = Fcdr (hooks);
1970 UNGCPRO;
1975 /* Run the interval hooks for an insertion on character range START ... END.
1976 verify_interval_modification chose which hooks to run;
1977 this function is called after the insertion happens
1978 so it can indicate the range of inserted text. */
1980 void
1981 report_interval_modification (start, end)
1982 Lisp_Object start, end;
1984 if (! NILP (interval_insert_behind_hooks))
1985 call_mod_hooks (interval_insert_behind_hooks, start, end);
1986 if (! NILP (interval_insert_in_front_hooks)
1987 && ! EQ (interval_insert_in_front_hooks,
1988 interval_insert_behind_hooks))
1989 call_mod_hooks (interval_insert_in_front_hooks, start, end);
1992 void
1993 syms_of_textprop ()
1995 DEFVAR_LISP ("default-text-properties", &Vdefault_text_properties,
1996 doc: /* Property-list used as default values.
1997 The value of a property in this list is seen as the value for every
1998 character that does not have its own value for that property. */);
1999 Vdefault_text_properties = Qnil;
2001 DEFVAR_LISP ("inhibit-point-motion-hooks", &Vinhibit_point_motion_hooks,
2002 doc: /* If non-nil, don't run `point-left' and `point-entered' text properties.
2003 This also inhibits the use of the `intangible' text property. */);
2004 Vinhibit_point_motion_hooks = Qnil;
2006 DEFVAR_LISP ("text-property-default-nonsticky",
2007 &Vtext_property_default_nonsticky,
2008 doc: /* Alist of properties vs the corresponding non-stickinesses.
2009 Each element has the form (PROPERTY . NONSTICKINESS).
2011 If a character in a buffer has PROPERTY, new text inserted adjacent to
2012 the character doesn't inherit PROPERTY if NONSTICKINESS is non-nil,
2013 inherits it if NONSTICKINESS is nil. The front-sticky and
2014 rear-nonsticky properties of the character overrides NONSTICKINESS. */);
2015 Vtext_property_default_nonsticky = Qnil;
2017 staticpro (&interval_insert_behind_hooks);
2018 staticpro (&interval_insert_in_front_hooks);
2019 interval_insert_behind_hooks = Qnil;
2020 interval_insert_in_front_hooks = Qnil;
2023 /* Common attributes one might give text */
2025 staticpro (&Qforeground);
2026 Qforeground = intern ("foreground");
2027 staticpro (&Qbackground);
2028 Qbackground = intern ("background");
2029 staticpro (&Qfont);
2030 Qfont = intern ("font");
2031 staticpro (&Qstipple);
2032 Qstipple = intern ("stipple");
2033 staticpro (&Qunderline);
2034 Qunderline = intern ("underline");
2035 staticpro (&Qread_only);
2036 Qread_only = intern ("read-only");
2037 staticpro (&Qinvisible);
2038 Qinvisible = intern ("invisible");
2039 staticpro (&Qintangible);
2040 Qintangible = intern ("intangible");
2041 staticpro (&Qcategory);
2042 Qcategory = intern ("category");
2043 staticpro (&Qlocal_map);
2044 Qlocal_map = intern ("local-map");
2045 staticpro (&Qfront_sticky);
2046 Qfront_sticky = intern ("front-sticky");
2047 staticpro (&Qrear_nonsticky);
2048 Qrear_nonsticky = intern ("rear-nonsticky");
2049 staticpro (&Qmouse_face);
2050 Qmouse_face = intern ("mouse-face");
2052 /* Properties that text might use to specify certain actions */
2054 staticpro (&Qmouse_left);
2055 Qmouse_left = intern ("mouse-left");
2056 staticpro (&Qmouse_entered);
2057 Qmouse_entered = intern ("mouse-entered");
2058 staticpro (&Qpoint_left);
2059 Qpoint_left = intern ("point-left");
2060 staticpro (&Qpoint_entered);
2061 Qpoint_entered = intern ("point-entered");
2063 defsubr (&Stext_properties_at);
2064 defsubr (&Sget_text_property);
2065 defsubr (&Sget_char_property);
2066 defsubr (&Snext_char_property_change);
2067 defsubr (&Sprevious_char_property_change);
2068 defsubr (&Snext_single_char_property_change);
2069 defsubr (&Sprevious_single_char_property_change);
2070 defsubr (&Snext_property_change);
2071 defsubr (&Snext_single_property_change);
2072 defsubr (&Sprevious_property_change);
2073 defsubr (&Sprevious_single_property_change);
2074 defsubr (&Sadd_text_properties);
2075 defsubr (&Sput_text_property);
2076 defsubr (&Sset_text_properties);
2077 defsubr (&Sremove_text_properties);
2078 defsubr (&Stext_property_any);
2079 defsubr (&Stext_property_not_all);
2080 /* defsubr (&Serase_text_properties); */
2081 /* defsubr (&Scopy_text_properties); */