1 /* Interface code for dealing with text properties.
2 Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "intervals.h"
27 #define NULL (void *)0
31 /* NOTES: previous- and next- property change will have to skip
32 zero-length intervals if they are implemented. This could be done
33 inside next_interval and previous_interval.
35 set_properties needs to deal with the interval property cache.
37 It is assumed that for any interval plist, a property appears
38 only once on the list. Although some code i.e., remove_properties,
39 handles the more general case, the uniqueness of properties is
40 necessary for the system to remain consistent. This requirement
41 is enforced by the subrs installing properties onto the intervals. */
43 /* The rest of the file is within this conditional */
44 #ifdef USE_TEXT_PROPERTIES
47 Lisp_Object Qmouse_left
;
48 Lisp_Object Qmouse_entered
;
49 Lisp_Object Qpoint_left
;
50 Lisp_Object Qpoint_entered
;
51 Lisp_Object Qcategory
;
52 Lisp_Object Qlocal_map
;
54 /* Visual properties text (including strings) may have. */
55 Lisp_Object Qforeground
, Qbackground
, Qfont
, Qunderline
, Qstipple
;
56 Lisp_Object Qinvisible
, Qread_only
, Qintangible
;
58 /* Sticky properties */
59 Lisp_Object Qfront_sticky
, Qrear_nonsticky
;
61 /* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to
62 the o1's cdr. Otherwise, return zero. This is handy for
64 #define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCONS (o1)->cdr, CONSP (o2)))
66 Lisp_Object Vinhibit_point_motion_hooks
;
67 Lisp_Object Vdefault_properties
;
70 /* Extract the interval at the position pointed to by BEGIN from
71 OBJECT, a string or buffer. Additionally, check that the positions
72 pointed to by BEGIN and END are within the bounds of OBJECT, and
73 reverse them if *BEGIN is greater than *END. The objects pointed
74 to by BEGIN and END may be integers or markers; if the latter, they
75 are coerced to integers.
77 When OBJECT is a string, we increment *BEGIN and *END
78 to make them origin-one.
80 Note that buffer points don't correspond to interval indices.
81 For example, point-max is 1 greater than the index of the last
82 character. This difference is handled in the caller, which uses
83 the validated points to determine a length, and operates on that.
84 Exceptions are Ftext_properties_at, Fnext_property_change, and
85 Fprevious_property_change which call this function with BEGIN == END.
86 Handle this case specially.
88 If FORCE is soft (0), it's OK to return NULL_INTERVAL. Otherwise,
89 create an interval tree for OBJECT if one doesn't exist, provided
90 the object actually contains text. In the current design, if there
91 is no text, there can be no text properties. */
97 validate_interval_range (object
, begin
, end
, force
)
98 Lisp_Object object
, *begin
, *end
;
104 CHECK_STRING_OR_BUFFER (object
, 0);
105 CHECK_NUMBER_COERCE_MARKER (*begin
, 0);
106 CHECK_NUMBER_COERCE_MARKER (*end
, 0);
108 /* If we are asked for a point, but from a subr which operates
109 on a range, then return nothing. */
110 if (EQ (*begin
, *end
) && begin
!= end
)
111 return NULL_INTERVAL
;
113 if (XINT (*begin
) > XINT (*end
))
121 if (BUFFERP (object
))
123 register struct buffer
*b
= XBUFFER (object
);
125 if (!(BUF_BEGV (b
) <= XINT (*begin
) && XINT (*begin
) <= XINT (*end
)
126 && XINT (*end
) <= BUF_ZV (b
)))
127 args_out_of_range (*begin
, *end
);
128 i
= BUF_INTERVALS (b
);
130 /* If there's no text, there are no properties. */
131 if (BUF_BEGV (b
) == BUF_ZV (b
))
132 return NULL_INTERVAL
;
134 searchpos
= XINT (*begin
);
138 register struct Lisp_String
*s
= XSTRING (object
);
140 if (! (0 <= XINT (*begin
) && XINT (*begin
) <= XINT (*end
)
141 && XINT (*end
) <= s
->size
))
142 args_out_of_range (*begin
, *end
);
143 /* User-level Positions in strings start with 0,
144 but the interval code always wants positions starting with 1. */
145 XSETFASTINT (*begin
, XFASTINT (*begin
) + 1);
147 XSETFASTINT (*end
, XFASTINT (*end
) + 1);
151 return NULL_INTERVAL
;
153 searchpos
= XINT (*begin
);
156 if (NULL_INTERVAL_P (i
))
157 return (force
? create_root_interval (object
) : i
);
159 return find_interval (i
, searchpos
);
162 /* Validate LIST as a property list. If LIST is not a list, then
163 make one consisting of (LIST nil). Otherwise, verify that LIST
164 is even numbered and thus suitable as a plist. */
167 validate_plist (list
)
176 register Lisp_Object tail
;
177 for (i
= 0, tail
= list
; !NILP (tail
); i
++)
183 error ("Odd length text property list");
187 return Fcons (list
, Fcons (Qnil
, Qnil
));
190 /* Return nonzero if interval I has all the properties,
191 with the same values, of list PLIST. */
194 interval_has_all_properties (plist
, i
)
198 register Lisp_Object tail1
, tail2
, sym1
, sym2
;
201 /* Go through each element of PLIST. */
202 for (tail1
= plist
; ! NILP (tail1
); tail1
= Fcdr (Fcdr (tail1
)))
207 /* Go through I's plist, looking for sym1 */
208 for (tail2
= i
->plist
; ! NILP (tail2
); tail2
= Fcdr (Fcdr (tail2
)))
209 if (EQ (sym1
, Fcar (tail2
)))
211 /* Found the same property on both lists. If the
212 values are unequal, return zero. */
213 if (! EQ (Fcar (Fcdr (tail1
)), Fcar (Fcdr (tail2
))))
216 /* Property has same value on both lists; go to next one. */
228 /* Return nonzero if the plist of interval I has any of the
229 properties of PLIST, regardless of their values. */
232 interval_has_some_properties (plist
, i
)
236 register Lisp_Object tail1
, tail2
, sym
;
238 /* Go through each element of PLIST. */
239 for (tail1
= plist
; ! NILP (tail1
); tail1
= Fcdr (Fcdr (tail1
)))
243 /* Go through i's plist, looking for tail1 */
244 for (tail2
= i
->plist
; ! NILP (tail2
); tail2
= Fcdr (Fcdr (tail2
)))
245 if (EQ (sym
, Fcar (tail2
)))
252 /* Changing the plists of individual intervals. */
254 /* Return the value of PROP in property-list PLIST, or Qunbound if it
257 property_value (plist
, prop
)
258 Lisp_Object plist
, prop
;
262 while (PLIST_ELT_P (plist
, value
))
263 if (EQ (XCONS (plist
)->car
, prop
))
264 return XCONS (value
)->car
;
266 plist
= XCONS (value
)->cdr
;
271 /* Set the properties of INTERVAL to PROPERTIES,
272 and record undo info for the previous values.
273 OBJECT is the string or buffer that INTERVAL belongs to. */
276 set_properties (properties
, interval
, object
)
277 Lisp_Object properties
, object
;
280 Lisp_Object sym
, value
;
282 if (BUFFERP (object
))
284 /* For each property in the old plist which is missing from PROPERTIES,
285 or has a different value in PROPERTIES, make an undo record. */
286 for (sym
= interval
->plist
;
287 PLIST_ELT_P (sym
, value
);
288 sym
= XCONS (value
)->cdr
)
289 if (! EQ (property_value (properties
, XCONS (sym
)->car
),
292 modify_region (XBUFFER (object
),
293 make_number (interval
->position
),
294 make_number (interval
->position
+ LENGTH (interval
)));
295 record_property_change (interval
->position
, LENGTH (interval
),
296 XCONS (sym
)->car
, XCONS (value
)->car
,
300 /* For each new property that has no value at all in the old plist,
301 make an undo record binding it to nil, so it will be removed. */
302 for (sym
= properties
;
303 PLIST_ELT_P (sym
, value
);
304 sym
= XCONS (value
)->cdr
)
305 if (EQ (property_value (interval
->plist
, XCONS (sym
)->car
), Qunbound
))
307 modify_region (XBUFFER (object
),
308 make_number (interval
->position
),
309 make_number (interval
->position
+ LENGTH (interval
)));
310 record_property_change (interval
->position
, LENGTH (interval
),
311 XCONS (sym
)->car
, Qnil
,
316 /* Store new properties. */
317 interval
->plist
= Fcopy_sequence (properties
);
320 /* Add the properties of PLIST to the interval I, or set
321 the value of I's property to the value of the property on PLIST
322 if they are different.
324 OBJECT should be the string or buffer the interval is in.
326 Return nonzero if this changes I (i.e., if any members of PLIST
327 are actually added to I's plist) */
330 add_properties (plist
, i
, object
)
335 Lisp_Object tail1
, tail2
, sym1
, val1
;
336 register int changed
= 0;
338 struct gcpro gcpro1
, gcpro2
, gcpro3
;
343 /* No need to protect OBJECT, because we can GC only in the case
344 where it is a buffer, and live buffers are always protected.
345 I and its plist are also protected, via OBJECT. */
346 GCPRO3 (tail1
, sym1
, val1
);
348 /* Go through each element of PLIST. */
349 for (tail1
= plist
; ! NILP (tail1
); tail1
= Fcdr (Fcdr (tail1
)))
352 val1
= Fcar (Fcdr (tail1
));
355 /* Go through I's plist, looking for sym1 */
356 for (tail2
= i
->plist
; ! NILP (tail2
); tail2
= Fcdr (Fcdr (tail2
)))
357 if (EQ (sym1
, Fcar (tail2
)))
359 /* No need to gcpro, because tail2 protects this
360 and it must be a cons cell (we get an error otherwise). */
361 register Lisp_Object this_cdr
;
363 this_cdr
= Fcdr (tail2
);
364 /* Found the property. Now check its value. */
367 /* The properties have the same value on both lists.
368 Continue to the next property. */
369 if (EQ (val1
, Fcar (this_cdr
)))
372 /* Record this change in the buffer, for undo purposes. */
373 if (BUFFERP (object
))
375 modify_region (XBUFFER (object
),
376 make_number (i
->position
),
377 make_number (i
->position
+ LENGTH (i
)));
378 record_property_change (i
->position
, LENGTH (i
),
379 sym1
, Fcar (this_cdr
), object
);
382 /* I's property has a different value -- change it */
383 Fsetcar (this_cdr
, val1
);
390 /* Record this change in the buffer, for undo purposes. */
391 if (BUFFERP (object
))
393 modify_region (XBUFFER (object
),
394 make_number (i
->position
),
395 make_number (i
->position
+ LENGTH (i
)));
396 record_property_change (i
->position
, LENGTH (i
),
399 i
->plist
= Fcons (sym1
, Fcons (val1
, i
->plist
));
409 /* For any members of PLIST which are properties of I, remove them
411 OBJECT is the string or buffer containing I. */
414 remove_properties (plist
, i
, object
)
419 register Lisp_Object tail1
, tail2
, sym
, current_plist
;
420 register int changed
= 0;
422 current_plist
= i
->plist
;
423 /* Go through each element of plist. */
424 for (tail1
= plist
; ! NILP (tail1
); tail1
= Fcdr (Fcdr (tail1
)))
428 /* First, remove the symbol if its at the head of the list */
429 while (! NILP (current_plist
) && EQ (sym
, Fcar (current_plist
)))
431 if (BUFFERP (object
))
433 modify_region (XBUFFER (object
),
434 make_number (i
->position
),
435 make_number (i
->position
+ LENGTH (i
)));
436 record_property_change (i
->position
, LENGTH (i
),
437 sym
, Fcar (Fcdr (current_plist
)),
441 current_plist
= Fcdr (Fcdr (current_plist
));
445 /* Go through i's plist, looking for sym */
446 tail2
= current_plist
;
447 while (! NILP (tail2
))
449 register Lisp_Object
this;
450 this = Fcdr (Fcdr (tail2
));
451 if (EQ (sym
, Fcar (this)))
453 if (BUFFERP (object
))
455 modify_region (XBUFFER (object
),
456 make_number (i
->position
),
457 make_number (i
->position
+ LENGTH (i
)));
458 record_property_change (i
->position
, LENGTH (i
),
459 sym
, Fcar (Fcdr (this)), object
);
462 Fsetcdr (Fcdr (tail2
), Fcdr (Fcdr (this)));
470 i
->plist
= current_plist
;
475 /* Remove all properties from interval I. Return non-zero
476 if this changes the interval. */
490 DEFUN ("text-properties-at", Ftext_properties_at
,
491 Stext_properties_at
, 1, 2, 0,
492 "Return the list of properties held by the character at POSITION\n\
493 in optional argument OBJECT, a string or buffer. If nil, OBJECT\n\
494 defaults to the current buffer.\n\
495 If POSITION is at the end of OBJECT, the value is nil.")
497 Lisp_Object pos
, object
;
502 XSETBUFFER (object
, current_buffer
);
504 i
= validate_interval_range (object
, &pos
, &pos
, soft
);
505 if (NULL_INTERVAL_P (i
))
507 /* If POS is at the end of the interval,
508 it means it's the end of OBJECT.
509 There are no properties at the very end,
510 since no character follows. */
511 if (XINT (pos
) == LENGTH (i
) + i
->position
)
517 DEFUN ("get-text-property", Fget_text_property
, Sget_text_property
, 2, 3, 0,
518 "Return the value of position POS's property PROP, in OBJECT.\n\
519 OBJECT is optional and defaults to the current buffer.\n\
520 If POSITION is at the end of OBJECT, the value is nil.")
522 Lisp_Object pos
, object
;
525 return textget (Ftext_properties_at (pos
, object
), prop
);
528 DEFUN ("get-char-property", Fget_char_property
, Sget_char_property
, 2, 3, 0,
529 "Return the value of position POS's property PROP, in OBJECT.\n\
530 OBJECT is optional and defaults to the current buffer.\n\
531 If POS is at the end of OBJECT, the value is nil.\n\
532 If OBJECT is a buffer, then overlay properties are considered as well as\n\
534 If OBJECT is a window, then that window's buffer is used, but window-specific\n\
535 overlays are considered only if they are associated with OBJECT.")
537 Lisp_Object pos
, object
;
538 register Lisp_Object prop
;
540 struct window
*w
= 0;
542 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
545 XSETBUFFER (object
, current_buffer
);
547 if (WINDOWP (object
))
549 w
= XWINDOW (object
);
552 if (BUFFERP (object
))
554 int posn
= XINT (pos
);
556 Lisp_Object
*overlay_vec
, tem
;
560 /* First try with room for 40 overlays. */
562 overlay_vec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
564 noverlays
= overlays_at (posn
, 0, &overlay_vec
, &len
,
565 &next_overlay
, NULL
);
567 /* If there are more than 40,
568 make enough space for all, and try again. */
572 overlay_vec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
573 noverlays
= overlays_at (posn
, 0, &overlay_vec
, &len
,
574 &next_overlay
, NULL
);
576 noverlays
= sort_overlays (overlay_vec
, noverlays
, w
);
578 /* Now check the overlays in order of decreasing priority. */
579 while (--noverlays
>= 0)
581 tem
= Foverlay_get (overlay_vec
[noverlays
], prop
);
586 /* Not a buffer, or no appropriate overlay, so fall through to the
588 return (Fget_text_property (pos
, prop
, object
));
591 DEFUN ("next-property-change", Fnext_property_change
,
592 Snext_property_change
, 1, 3, 0,
593 "Return the position of next property change.\n\
594 Scans characters forward from POS in OBJECT till it finds\n\
595 a change in some text property, then returns the position of the change.\n\
596 The optional second argument OBJECT is the string or buffer to scan.\n\
597 Return nil if the property is constant all the way to the end of OBJECT.\n\
598 If the value is non-nil, it is a position greater than POS, never equal.\n\n\
599 If the optional third argument LIMIT is non-nil, don't search\n\
600 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
602 Lisp_Object pos
, object
, limit
;
604 register INTERVAL i
, next
;
607 XSETBUFFER (object
, current_buffer
);
610 CHECK_NUMBER_COERCE_MARKER (limit
, 0);
612 i
= validate_interval_range (object
, &pos
, &pos
, soft
);
613 if (NULL_INTERVAL_P (i
))
616 next
= next_interval (i
);
617 while (! NULL_INTERVAL_P (next
) && intervals_equal (i
, next
)
618 && (NILP (limit
) || next
->position
< XFASTINT (limit
)))
619 next
= next_interval (next
);
621 if (NULL_INTERVAL_P (next
))
623 if (! NILP (limit
) && !(next
->position
< XFASTINT (limit
)))
626 XSETFASTINT (pos
, next
->position
- (STRINGP (object
)));
630 /* Return 1 if there's a change in some property between BEG and END. */
633 property_change_between_p (beg
, end
)
636 register INTERVAL i
, next
;
637 Lisp_Object object
, pos
;
639 XSETBUFFER (object
, current_buffer
);
640 XSETFASTINT (pos
, beg
);
642 i
= validate_interval_range (object
, &pos
, &pos
, soft
);
643 if (NULL_INTERVAL_P (i
))
646 next
= next_interval (i
);
647 while (! NULL_INTERVAL_P (next
) && intervals_equal (i
, next
))
649 next
= next_interval (next
);
650 if (NULL_INTERVAL_P (next
))
652 if (next
->position
>= end
)
656 if (NULL_INTERVAL_P (next
))
662 DEFUN ("next-single-property-change", Fnext_single_property_change
,
663 Snext_single_property_change
, 2, 4, 0,
664 "Return the position of next property change for a specific property.\n\
665 Scans characters forward from POS till it finds\n\
666 a change in the PROP property, then returns the position of the change.\n\
667 The optional third argument OBJECT is the string or buffer to scan.\n\
668 The property values are compared with `eq'.\n\
669 Return nil if the property is constant all the way to the end of OBJECT.\n\
670 If the value is non-nil, it is a position greater than POS, never equal.\n\n\
671 If the optional fourth argument LIMIT is non-nil, don't search\n\
672 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
673 (pos
, prop
, object
, limit
)
674 Lisp_Object pos
, prop
, object
, limit
;
676 register INTERVAL i
, next
;
677 register Lisp_Object here_val
;
680 XSETBUFFER (object
, current_buffer
);
683 CHECK_NUMBER_COERCE_MARKER (limit
, 0);
685 i
= validate_interval_range (object
, &pos
, &pos
, soft
);
686 if (NULL_INTERVAL_P (i
))
689 here_val
= textget (i
->plist
, prop
);
690 next
= next_interval (i
);
691 while (! NULL_INTERVAL_P (next
)
692 && EQ (here_val
, textget (next
->plist
, prop
))
693 && (NILP (limit
) || next
->position
< XFASTINT (limit
)))
694 next
= next_interval (next
);
696 if (NULL_INTERVAL_P (next
))
698 if (! NILP (limit
) && !(next
->position
< XFASTINT (limit
)))
701 XSETFASTINT (pos
, next
->position
- (STRINGP (object
)));
705 DEFUN ("previous-property-change", Fprevious_property_change
,
706 Sprevious_property_change
, 1, 3, 0,
707 "Return the position of previous property change.\n\
708 Scans characters backwards from POS in OBJECT till it finds\n\
709 a change in some text property, then returns the position of the change.\n\
710 The optional second argument OBJECT is the string or buffer to scan.\n\
711 Return nil if the property is constant all the way to the start of OBJECT.\n\
712 If the value is non-nil, it is a position less than POS, never equal.\n\n\
713 If the optional third argument LIMIT is non-nil, don't search\n\
714 back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
716 Lisp_Object pos
, object
, limit
;
718 register INTERVAL i
, previous
;
721 XSETBUFFER (object
, current_buffer
);
724 CHECK_NUMBER_COERCE_MARKER (limit
, 0);
726 i
= validate_interval_range (object
, &pos
, &pos
, soft
);
727 if (NULL_INTERVAL_P (i
))
730 /* Start with the interval containing the char before point. */
731 if (i
->position
== XFASTINT (pos
))
732 i
= previous_interval (i
);
734 previous
= previous_interval (i
);
735 while (! NULL_INTERVAL_P (previous
) && intervals_equal (previous
, i
)
737 || previous
->position
+ LENGTH (previous
) > XFASTINT (limit
)))
738 previous
= previous_interval (previous
);
739 if (NULL_INTERVAL_P (previous
))
742 && !(previous
->position
+ LENGTH (previous
) > XFASTINT (limit
)))
745 XSETFASTINT (pos
, (previous
->position
+ LENGTH (previous
)
746 - (STRINGP (object
))));
750 DEFUN ("previous-single-property-change", Fprevious_single_property_change
,
751 Sprevious_single_property_change
, 2, 4, 0,
752 "Return the position of previous property change for a specific property.\n\
753 Scans characters backward from POS till it finds\n\
754 a change in the PROP property, then returns the position of the change.\n\
755 The optional third argument OBJECT is the string or buffer to scan.\n\
756 The property values are compared with `eq'.\n\
757 Return nil if the property is constant all the way to the start of OBJECT.\n\
758 If the value is non-nil, it is a position less than POS, never equal.\n\n\
759 If the optional fourth argument LIMIT is non-nil, don't search\n\
760 back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
761 (pos
, prop
, object
, limit
)
762 Lisp_Object pos
, prop
, object
, limit
;
764 register INTERVAL i
, previous
;
765 register Lisp_Object here_val
;
768 XSETBUFFER (object
, current_buffer
);
771 CHECK_NUMBER_COERCE_MARKER (limit
, 0);
773 i
= validate_interval_range (object
, &pos
, &pos
, soft
);
775 /* Start with the interval containing the char before point. */
776 if (! NULL_INTERVAL_P (i
) && i
->position
== XFASTINT (pos
))
777 i
= previous_interval (i
);
779 if (NULL_INTERVAL_P (i
))
782 here_val
= textget (i
->plist
, prop
);
783 previous
= previous_interval (i
);
784 while (! NULL_INTERVAL_P (previous
)
785 && EQ (here_val
, textget (previous
->plist
, prop
))
787 || previous
->position
+ LENGTH (previous
) > XFASTINT (limit
)))
788 previous
= previous_interval (previous
);
789 if (NULL_INTERVAL_P (previous
))
792 && !(previous
->position
+ LENGTH (previous
) > XFASTINT (limit
)))
795 XSETFASTINT (pos
, (previous
->position
+ LENGTH (previous
)
796 - (STRINGP (object
))));
800 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
802 DEFUN ("add-text-properties", Fadd_text_properties
,
803 Sadd_text_properties
, 3, 4, 0,
804 "Add properties to the text from START to END.\n\
805 The third argument PROPS is a property list\n\
806 specifying the property values to add.\n\
807 The optional fourth argument, OBJECT,\n\
808 is the string or buffer containing the text.\n\
809 Return t if any property value actually changed, nil otherwise.")
810 (start
, end
, properties
, object
)
811 Lisp_Object start
, end
, properties
, object
;
813 register INTERVAL i
, unchanged
;
814 register int s
, len
, modified
= 0;
817 properties
= validate_plist (properties
);
818 if (NILP (properties
))
822 XSETBUFFER (object
, current_buffer
);
824 i
= validate_interval_range (object
, &start
, &end
, hard
);
825 if (NULL_INTERVAL_P (i
))
829 len
= XINT (end
) - s
;
831 /* No need to protect OBJECT, because we GC only if it's a buffer,
832 and live buffers are always protected. */
835 /* If we're not starting on an interval boundary, we have to
836 split this interval. */
837 if (i
->position
!= s
)
839 /* If this interval already has the properties, we can
841 if (interval_has_all_properties (properties
, i
))
843 int got
= (LENGTH (i
) - (s
- i
->position
));
847 i
= next_interval (i
);
852 i
= split_interval_right (unchanged
, s
- unchanged
->position
);
853 copy_properties (unchanged
, i
);
857 /* We are at the beginning of interval I, with LEN chars to scan. */
863 if (LENGTH (i
) >= len
)
865 /* We can UNGCPRO safely here, because there will be just
866 one more chance to gc, in the next call to add_properties,
867 and after that we will not need PROPERTIES or OBJECT again. */
870 if (interval_has_all_properties (properties
, i
))
871 return modified
? Qt
: Qnil
;
873 if (LENGTH (i
) == len
)
875 add_properties (properties
, i
, object
);
879 /* i doesn't have the properties, and goes past the change limit */
881 i
= split_interval_left (unchanged
, len
);
882 copy_properties (unchanged
, i
);
883 add_properties (properties
, i
, object
);
888 modified
+= add_properties (properties
, i
, object
);
889 i
= next_interval (i
);
893 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
895 DEFUN ("put-text-property", Fput_text_property
,
896 Sput_text_property
, 4, 5, 0,
897 "Set one property of the text from START to END.\n\
898 The third and fourth arguments PROP and VALUE\n\
899 specify the property to add.\n\
900 The optional fifth argument, OBJECT,\n\
901 is the string or buffer containing the text.")
902 (start
, end
, prop
, value
, object
)
903 Lisp_Object start
, end
, prop
, value
, object
;
905 Fadd_text_properties (start
, end
,
906 Fcons (prop
, Fcons (value
, Qnil
)),
911 DEFUN ("set-text-properties", Fset_text_properties
,
912 Sset_text_properties
, 3, 4, 0,
913 "Completely replace properties of text from START to END.\n\
914 The third argument PROPS is the new property list.\n\
915 The optional fourth argument, OBJECT,\n\
916 is the string or buffer containing the text.")
917 (start
, end
, props
, object
)
918 Lisp_Object start
, end
, props
, object
;
920 register INTERVAL i
, unchanged
;
921 register INTERVAL prev_changed
= NULL_INTERVAL
;
923 Lisp_Object ostart
, oend
;
928 props
= validate_plist (props
);
931 XSETBUFFER (object
, current_buffer
);
933 /* If we want no properties for a whole string,
934 get rid of its intervals. */
935 if (NILP (props
) && STRINGP (object
)
936 && XFASTINT (start
) == 0
937 && XFASTINT (end
) == XSTRING (object
)->size
)
939 XSTRING (object
)->intervals
= 0;
943 i
= validate_interval_range (object
, &start
, &end
, soft
);
945 if (NULL_INTERVAL_P (i
))
947 /* If buffer has no props, and we want none, return now. */
951 /* Restore the original START and END values
952 because validate_interval_range increments them for strings. */
956 i
= validate_interval_range (object
, &start
, &end
, hard
);
957 /* This can return if start == end. */
958 if (NULL_INTERVAL_P (i
))
963 len
= XINT (end
) - s
;
965 if (i
->position
!= s
)
968 i
= split_interval_right (unchanged
, s
- unchanged
->position
);
970 if (LENGTH (i
) > len
)
972 copy_properties (unchanged
, i
);
973 i
= split_interval_left (i
, len
);
974 set_properties (props
, i
, object
);
978 set_properties (props
, i
, object
);
980 if (LENGTH (i
) == len
)
985 i
= next_interval (i
);
988 /* We are starting at the beginning of an interval, I */
994 if (LENGTH (i
) >= len
)
996 if (LENGTH (i
) > len
)
997 i
= split_interval_left (i
, len
);
999 if (NULL_INTERVAL_P (prev_changed
))
1000 set_properties (props
, i
, object
);
1002 merge_interval_left (i
);
1007 if (NULL_INTERVAL_P (prev_changed
))
1009 set_properties (props
, i
, object
);
1013 prev_changed
= i
= merge_interval_left (i
);
1015 i
= next_interval (i
);
1021 DEFUN ("remove-text-properties", Fremove_text_properties
,
1022 Sremove_text_properties
, 3, 4, 0,
1023 "Remove some properties from text from START to END.\n\
1024 The third argument PROPS is a property list\n\
1025 whose property names specify the properties to remove.\n\
1026 \(The values stored in PROPS are ignored.)\n\
1027 The optional fourth argument, OBJECT,\n\
1028 is the string or buffer containing the text.\n\
1029 Return t if any property was actually removed, nil otherwise.")
1030 (start
, end
, props
, object
)
1031 Lisp_Object start
, end
, props
, object
;
1033 register INTERVAL i
, unchanged
;
1034 register int s
, len
, modified
= 0;
1037 XSETBUFFER (object
, current_buffer
);
1039 i
= validate_interval_range (object
, &start
, &end
, soft
);
1040 if (NULL_INTERVAL_P (i
))
1044 len
= XINT (end
) - s
;
1046 if (i
->position
!= s
)
1048 /* No properties on this first interval -- return if
1049 it covers the entire region. */
1050 if (! interval_has_some_properties (props
, i
))
1052 int got
= (LENGTH (i
) - (s
- i
->position
));
1056 i
= next_interval (i
);
1058 /* Split away the beginning of this interval; what we don't
1063 i
= split_interval_right (unchanged
, s
- unchanged
->position
);
1064 copy_properties (unchanged
, i
);
1068 /* We are at the beginning of an interval, with len to scan */
1074 if (LENGTH (i
) >= len
)
1076 if (! interval_has_some_properties (props
, i
))
1077 return modified
? Qt
: Qnil
;
1079 if (LENGTH (i
) == len
)
1081 remove_properties (props
, i
, object
);
1085 /* i has the properties, and goes past the change limit */
1087 i
= split_interval_left (i
, len
);
1088 copy_properties (unchanged
, i
);
1089 remove_properties (props
, i
, object
);
1094 modified
+= remove_properties (props
, i
, object
);
1095 i
= next_interval (i
);
1099 DEFUN ("text-property-any", Ftext_property_any
,
1100 Stext_property_any
, 4, 5, 0,
1101 "Check text from START to END to see if PROP is ever `eq' to VALUE.\n\
1102 If so, return the position of the first character whose PROP is `eq'\n\
1103 to VALUE. Otherwise return nil.\n\
1104 The optional fifth argument, OBJECT, is the string or buffer\n\
1105 containing the text.")
1106 (start
, end
, prop
, value
, object
)
1107 Lisp_Object start
, end
, prop
, value
, object
;
1109 register INTERVAL i
;
1110 register int e
, pos
;
1113 XSETBUFFER (object
, current_buffer
);
1114 i
= validate_interval_range (object
, &start
, &end
, soft
);
1115 if (NULL_INTERVAL_P (i
))
1116 return (!NILP (value
) || EQ (start
, end
) ? Qnil
: start
);
1119 while (! NULL_INTERVAL_P (i
))
1121 if (i
->position
>= e
)
1123 if (EQ (textget (i
->plist
, prop
), value
))
1126 if (pos
< XINT (start
))
1128 return make_number (pos
- (STRINGP (object
)));
1130 i
= next_interval (i
);
1135 DEFUN ("text-property-not-all", Ftext_property_not_all
,
1136 Stext_property_not_all
, 4, 5, 0,
1137 "Check text from START to END to see if PROP is ever not `eq' to VALUE.\n\
1138 If so, return the position of the first character whose PROP is not\n\
1139 `eq' to VALUE. Otherwise, return nil.\n\
1140 The optional fifth argument, OBJECT, is the string or buffer\n\
1141 containing the text.")
1142 (start
, end
, prop
, value
, object
)
1143 Lisp_Object start
, end
, prop
, value
, object
;
1145 register INTERVAL i
;
1149 XSETBUFFER (object
, current_buffer
);
1150 i
= validate_interval_range (object
, &start
, &end
, soft
);
1151 if (NULL_INTERVAL_P (i
))
1152 return (NILP (value
) || EQ (start
, end
)) ? Qnil
: start
;
1156 while (! NULL_INTERVAL_P (i
))
1158 if (i
->position
>= e
)
1160 if (! EQ (textget (i
->plist
, prop
), value
))
1162 if (i
->position
> s
)
1164 return make_number (s
- (STRINGP (object
)));
1166 i
= next_interval (i
);
1171 #if 0 /* You can use set-text-properties for this. */
1173 DEFUN ("erase-text-properties", Ferase_text_properties
,
1174 Serase_text_properties
, 2, 3, 0,
1175 "Remove all properties from the text from START to END.\n\
1176 The optional third argument, OBJECT,\n\
1177 is the string or buffer containing the text.")
1178 (start
, end
, object
)
1179 Lisp_Object start
, end
, object
;
1181 register INTERVAL i
;
1182 register INTERVAL prev_changed
= NULL_INTERVAL
;
1183 register int s
, len
, modified
;
1186 XSETBUFFER (object
, current_buffer
);
1188 i
= validate_interval_range (object
, &start
, &end
, soft
);
1189 if (NULL_INTERVAL_P (i
))
1193 len
= XINT (end
) - s
;
1195 if (i
->position
!= s
)
1198 register INTERVAL unchanged
= i
;
1200 /* If there are properties here, then this text will be modified. */
1201 if (! NILP (i
->plist
))
1203 i
= split_interval_right (unchanged
, s
- unchanged
->position
);
1207 if (LENGTH (i
) > len
)
1209 i
= split_interval_right (i
, len
);
1210 copy_properties (unchanged
, i
);
1214 if (LENGTH (i
) == len
)
1219 /* If the text of I is without any properties, and contains
1220 LEN or more characters, then we may return without changing
1222 else if (LENGTH (i
) - (s
- i
->position
) <= len
)
1224 /* The amount of text to change extends past I, so just note
1225 how much we've gotten. */
1227 got
= LENGTH (i
) - (s
- i
->position
);
1231 i
= next_interval (i
);
1234 /* We are starting at the beginning of an interval, I. */
1237 if (LENGTH (i
) >= len
)
1239 /* If I has no properties, simply merge it if possible. */
1240 if (NILP (i
->plist
))
1242 if (! NULL_INTERVAL_P (prev_changed
))
1243 merge_interval_left (i
);
1245 return modified
? Qt
: Qnil
;
1248 if (LENGTH (i
) > len
)
1249 i
= split_interval_left (i
, len
);
1250 if (! NULL_INTERVAL_P (prev_changed
))
1251 merge_interval_left (i
);
1258 /* Here if we still need to erase past the end of I */
1260 if (NULL_INTERVAL_P (prev_changed
))
1262 modified
+= erase_properties (i
);
1267 modified
+= ! NILP (i
->plist
);
1268 /* Merging I will give it the properties of PREV_CHANGED. */
1269 prev_changed
= i
= merge_interval_left (i
);
1272 i
= next_interval (i
);
1275 return modified
? Qt
: Qnil
;
1279 /* I don't think this is the right interface to export; how often do you
1280 want to do something like this, other than when you're copying objects
1283 I think it would be better to have a pair of functions, one which
1284 returns the text properties of a region as a list of ranges and
1285 plists, and another which applies such a list to another object. */
1287 /* Add properties from SRC to SRC of SRC, starting at POS in DEST.
1288 SRC and DEST may each refer to strings or buffers.
1289 Optional sixth argument PROP causes only that property to be copied.
1290 Properties are copied to DEST as if by `add-text-properties'.
1291 Return t if any property value actually changed, nil otherwise. */
1293 /* Note this can GC when DEST is a buffer. */
1296 copy_text_properties (start
, end
, src
, pos
, dest
, prop
)
1297 Lisp_Object start
, end
, src
, pos
, dest
, prop
;
1303 int s
, e
, e2
, p
, len
, modified
= 0;
1304 struct gcpro gcpro1
, gcpro2
;
1306 i
= validate_interval_range (src
, &start
, &end
, soft
);
1307 if (NULL_INTERVAL_P (i
))
1310 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
1312 Lisp_Object dest_start
, dest_end
;
1315 XSETFASTINT (dest_end
, XINT (dest_start
) + (XINT (end
) - XINT (start
)));
1316 /* Apply this to a copy of pos; it will try to increment its arguments,
1317 which we don't want. */
1318 validate_interval_range (dest
, &dest_start
, &dest_end
, soft
);
1329 e2
= i
->position
+ LENGTH (i
);
1336 while (! NILP (plist
))
1338 if (EQ (Fcar (plist
), prop
))
1340 plist
= Fcons (prop
, Fcons (Fcar (Fcdr (plist
)), Qnil
));
1343 plist
= Fcdr (Fcdr (plist
));
1347 /* Must defer modifications to the interval tree in case src
1348 and dest refer to the same string or buffer. */
1349 stuff
= Fcons (Fcons (make_number (p
),
1350 Fcons (make_number (p
+ len
),
1351 Fcons (plist
, Qnil
))),
1355 i
= next_interval (i
);
1356 if (NULL_INTERVAL_P (i
))
1363 GCPRO2 (stuff
, dest
);
1365 while (! NILP (stuff
))
1368 res
= Fadd_text_properties (Fcar (res
), Fcar (Fcdr (res
)),
1369 Fcar (Fcdr (Fcdr (res
))), dest
);
1372 stuff
= Fcdr (stuff
);
1377 return modified
? Qt
: Qnil
;
1383 DEFVAR_LISP ("default-properties", &Vdefault_properties
,
1384 "Property-list used as default values.\n\
1385 The value of a property in this list is seen as the value for every character\n\
1386 that does not have its own value for that property.");
1387 Vdefault_properties
= Qnil
;
1389 DEFVAR_LISP ("inhibit-point-motion-hooks", &Vinhibit_point_motion_hooks
,
1390 "If non-nil, don't run `point-left' and `point-entered' text properties.\n\
1391 This also inhibits the use of the `intangible' text property.");
1392 Vinhibit_point_motion_hooks
= Qnil
;
1394 /* Common attributes one might give text */
1396 staticpro (&Qforeground
);
1397 Qforeground
= intern ("foreground");
1398 staticpro (&Qbackground
);
1399 Qbackground
= intern ("background");
1401 Qfont
= intern ("font");
1402 staticpro (&Qstipple
);
1403 Qstipple
= intern ("stipple");
1404 staticpro (&Qunderline
);
1405 Qunderline
= intern ("underline");
1406 staticpro (&Qread_only
);
1407 Qread_only
= intern ("read-only");
1408 staticpro (&Qinvisible
);
1409 Qinvisible
= intern ("invisible");
1410 staticpro (&Qintangible
);
1411 Qintangible
= intern ("intangible");
1412 staticpro (&Qcategory
);
1413 Qcategory
= intern ("category");
1414 staticpro (&Qlocal_map
);
1415 Qlocal_map
= intern ("local-map");
1416 staticpro (&Qfront_sticky
);
1417 Qfront_sticky
= intern ("front-sticky");
1418 staticpro (&Qrear_nonsticky
);
1419 Qrear_nonsticky
= intern ("rear-nonsticky");
1421 /* Properties that text might use to specify certain actions */
1423 staticpro (&Qmouse_left
);
1424 Qmouse_left
= intern ("mouse-left");
1425 staticpro (&Qmouse_entered
);
1426 Qmouse_entered
= intern ("mouse-entered");
1427 staticpro (&Qpoint_left
);
1428 Qpoint_left
= intern ("point-left");
1429 staticpro (&Qpoint_entered
);
1430 Qpoint_entered
= intern ("point-entered");
1432 defsubr (&Stext_properties_at
);
1433 defsubr (&Sget_text_property
);
1434 defsubr (&Sget_char_property
);
1435 defsubr (&Snext_property_change
);
1436 defsubr (&Snext_single_property_change
);
1437 defsubr (&Sprevious_property_change
);
1438 defsubr (&Sprevious_single_property_change
);
1439 defsubr (&Sadd_text_properties
);
1440 defsubr (&Sput_text_property
);
1441 defsubr (&Sset_text_properties
);
1442 defsubr (&Sremove_text_properties
);
1443 defsubr (&Stext_property_any
);
1444 defsubr (&Stext_property_not_all
);
1445 /* defsubr (&Serase_text_properties); */
1446 /* defsubr (&Scopy_text_properties); */
1451 lose
-- this shouldn
't be compiled if USE_TEXT_PROPERTIES isn't defined
1453 #endif /* USE_TEXT_PROPERTIES */