1 /* Interface code for dealing with text properties.
2 Copyright (C) 1993-1995, 1997, 1999-2014 Free Software Foundation,
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 3 of the License, or
10 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
23 #include "intervals.h"
24 #include "character.h"
28 /* Test for membership, allowing for t (actually any non-cons) to mean the
31 #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
34 /* NOTES: previous- and next- property change will have to skip
35 zero-length intervals if they are implemented. This could be done
36 inside next_interval and previous_interval.
38 set_properties needs to deal with the interval property cache.
40 It is assumed that for any interval plist, a property appears
41 only once on the list. Although some code i.e., remove_properties,
42 handles the more general case, the uniqueness of properties is
43 necessary for the system to remain consistent. This requirement
44 is enforced by the subrs installing properties onto the intervals. */
48 static Lisp_Object Qmouse_left
;
49 static Lisp_Object Qmouse_entered
;
50 Lisp_Object Qpoint_left
;
51 Lisp_Object Qpoint_entered
;
52 Lisp_Object Qcategory
;
53 Lisp_Object Qlocal_map
;
55 /* Visual properties text (including strings) may have. */
56 static Lisp_Object Qforeground
, Qbackground
, Qunderline
;
58 static Lisp_Object Qstipple
;
59 Lisp_Object Qinvisible
, Qintangible
, Qmouse_face
;
60 static Lisp_Object Qread_only
;
61 Lisp_Object Qminibuffer_prompt
;
63 enum property_set_type
65 TEXT_PROPERTY_REPLACE
,
66 TEXT_PROPERTY_PREPEND
,
70 /* Sticky properties. */
71 Lisp_Object Qfront_sticky
, Qrear_nonsticky
;
73 /* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to
74 the o1's cdr. Otherwise, return zero. This is handy for
76 #define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCDR (o1), CONSP (o2)))
78 /* verify_interval_modification saves insertion hooks here
79 to be run later by report_interval_modification. */
80 static Lisp_Object interval_insert_behind_hooks
;
81 static Lisp_Object interval_insert_in_front_hooks
;
84 /* Signal a `text-read-only' error. This function makes it easier
85 to capture that error in GDB by putting a breakpoint on it. */
88 text_read_only (Lisp_Object propval
)
90 if (STRINGP (propval
))
91 xsignal1 (Qtext_read_only
, propval
);
93 xsignal0 (Qtext_read_only
);
96 /* Prepare to modify the text properties of BUFFER from START to END. */
99 modify_text_properties (Lisp_Object buffer
, Lisp_Object start
, Lisp_Object end
)
101 ptrdiff_t b
= XINT (start
), e
= XINT (end
);
102 struct buffer
*buf
= XBUFFER (buffer
), *old
= current_buffer
;
104 set_buffer_internal (buf
);
106 prepare_to_modify_buffer_1 (b
, e
, NULL
);
108 BUF_COMPUTE_UNCHANGED (buf
, b
- 1, e
);
109 if (MODIFF
<= SAVE_MODIFF
)
110 record_first_change ();
113 bset_point_before_scroll (current_buffer
, Qnil
);
115 set_buffer_internal (old
);
118 /* Complain if object is not string or buffer type. */
121 CHECK_STRING_OR_BUFFER (Lisp_Object x
)
123 CHECK_TYPE (STRINGP (x
) || BUFFERP (x
), Qbuffer_or_string_p
, x
);
126 /* Extract the interval at the position pointed to by BEGIN from
127 OBJECT, a string or buffer. Additionally, check that the positions
128 pointed to by BEGIN and END are within the bounds of OBJECT, and
129 reverse them if *BEGIN is greater than *END. The objects pointed
130 to by BEGIN and END may be integers or markers; if the latter, they
131 are coerced to integers.
133 When OBJECT is a string, we increment *BEGIN and *END
134 to make them origin-one.
136 Note that buffer points don't correspond to interval indices.
137 For example, point-max is 1 greater than the index of the last
138 character. This difference is handled in the caller, which uses
139 the validated points to determine a length, and operates on that.
140 Exceptions are Ftext_properties_at, Fnext_property_change, and
141 Fprevious_property_change which call this function with BEGIN == END.
142 Handle this case specially.
144 If FORCE is soft (0), it's OK to return NULL. Otherwise,
145 create an interval tree for OBJECT if one doesn't exist, provided
146 the object actually contains text. In the current design, if there
147 is no text, there can be no text properties. */
153 validate_interval_range (Lisp_Object object
, Lisp_Object
*begin
,
154 Lisp_Object
*end
, bool force
)
159 CHECK_STRING_OR_BUFFER (object
);
160 CHECK_NUMBER_COERCE_MARKER (*begin
);
161 CHECK_NUMBER_COERCE_MARKER (*end
);
163 /* If we are asked for a point, but from a subr which operates
164 on a range, then return nothing. */
165 if (EQ (*begin
, *end
) && begin
!= end
)
168 if (XINT (*begin
) > XINT (*end
))
176 if (BUFFERP (object
))
178 register struct buffer
*b
= XBUFFER (object
);
180 if (!(BUF_BEGV (b
) <= XINT (*begin
) && XINT (*begin
) <= XINT (*end
)
181 && XINT (*end
) <= BUF_ZV (b
)))
182 args_out_of_range (*begin
, *end
);
183 i
= buffer_intervals (b
);
185 /* If there's no text, there are no properties. */
186 if (BUF_BEGV (b
) == BUF_ZV (b
))
189 searchpos
= XINT (*begin
);
193 ptrdiff_t len
= SCHARS (object
);
195 if (! (0 <= XINT (*begin
) && XINT (*begin
) <= XINT (*end
)
196 && XINT (*end
) <= len
))
197 args_out_of_range (*begin
, *end
);
198 XSETFASTINT (*begin
, XFASTINT (*begin
));
200 XSETFASTINT (*end
, XFASTINT (*end
));
201 i
= string_intervals (object
);
206 searchpos
= XINT (*begin
);
210 return (force
? create_root_interval (object
) : i
);
212 return find_interval (i
, searchpos
);
215 /* Validate LIST as a property list. If LIST is not a list, then
216 make one consisting of (LIST nil). Otherwise, verify that LIST
217 is even numbered and thus suitable as a plist. */
220 validate_plist (Lisp_Object list
)
229 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
235 error ("Odd length text property list");
239 return list2 (list
, Qnil
);
242 /* Return true if interval I has all the properties,
243 with the same values, of list PLIST. */
246 interval_has_all_properties (Lisp_Object plist
, INTERVAL i
)
248 Lisp_Object tail1
, tail2
;
250 /* Go through each element of PLIST. */
251 for (tail1
= plist
; CONSP (tail1
); tail1
= Fcdr (XCDR (tail1
)))
253 Lisp_Object sym1
= XCAR (tail1
);
256 /* Go through I's plist, looking for sym1 */
257 for (tail2
= i
->plist
; CONSP (tail2
); tail2
= Fcdr (XCDR (tail2
)))
258 if (EQ (sym1
, XCAR (tail2
)))
260 /* Found the same property on both lists. If the
261 values are unequal, return zero. */
262 if (! EQ (Fcar (XCDR (tail1
)), Fcar (XCDR (tail2
))))
265 /* Property has same value on both lists; go to next one. */
277 /* Return true if the plist of interval I has any of the
278 properties of PLIST, regardless of their values. */
281 interval_has_some_properties (Lisp_Object plist
, INTERVAL i
)
283 Lisp_Object tail1
, tail2
, sym
;
285 /* Go through each element of PLIST. */
286 for (tail1
= plist
; CONSP (tail1
); tail1
= Fcdr (XCDR (tail1
)))
290 /* Go through i's plist, looking for tail1 */
291 for (tail2
= i
->plist
; CONSP (tail2
); tail2
= Fcdr (XCDR (tail2
)))
292 if (EQ (sym
, XCAR (tail2
)))
299 /* Return nonzero if the plist of interval I has any of the
300 property names in LIST, regardless of their values. */
303 interval_has_some_properties_list (Lisp_Object list
, INTERVAL i
)
305 Lisp_Object tail1
, tail2
, sym
;
307 /* Go through each element of LIST. */
308 for (tail1
= list
; CONSP (tail1
); tail1
= XCDR (tail1
))
312 /* Go through i's plist, looking for tail1 */
313 for (tail2
= i
->plist
; CONSP (tail2
); tail2
= XCDR (XCDR (tail2
)))
314 if (EQ (sym
, XCAR (tail2
)))
321 /* Changing the plists of individual intervals. */
323 /* Return the value of PROP in property-list PLIST, or Qunbound if it
326 property_value (Lisp_Object plist
, Lisp_Object prop
)
330 while (PLIST_ELT_P (plist
, value
))
331 if (EQ (XCAR (plist
), prop
))
334 plist
= XCDR (value
);
339 /* Set the properties of INTERVAL to PROPERTIES,
340 and record undo info for the previous values.
341 OBJECT is the string or buffer that INTERVAL belongs to. */
344 set_properties (Lisp_Object properties
, INTERVAL interval
, Lisp_Object object
)
346 Lisp_Object sym
, value
;
348 if (BUFFERP (object
))
350 /* For each property in the old plist which is missing from PROPERTIES,
351 or has a different value in PROPERTIES, make an undo record. */
352 for (sym
= interval
->plist
;
353 PLIST_ELT_P (sym
, value
);
355 if (! EQ (property_value (properties
, XCAR (sym
)),
358 record_property_change (interval
->position
, LENGTH (interval
),
359 XCAR (sym
), XCAR (value
),
363 /* For each new property that has no value at all in the old plist,
364 make an undo record binding it to nil, so it will be removed. */
365 for (sym
= properties
;
366 PLIST_ELT_P (sym
, value
);
368 if (EQ (property_value (interval
->plist
, XCAR (sym
)), Qunbound
))
370 record_property_change (interval
->position
, LENGTH (interval
),
376 /* Store new properties. */
377 set_interval_plist (interval
, Fcopy_sequence (properties
));
380 /* Add the properties of PLIST to the interval I, or set
381 the value of I's property to the value of the property on PLIST
382 if they are different.
384 OBJECT should be the string or buffer the interval is in.
386 Return true if this changes I (i.e., if any members of PLIST
387 are actually added to I's plist) */
390 add_properties (Lisp_Object plist
, INTERVAL i
, Lisp_Object object
,
391 enum property_set_type set_type
)
393 Lisp_Object tail1
, tail2
, sym1
, val1
;
395 struct gcpro gcpro1
, gcpro2
, gcpro3
;
400 /* No need to protect OBJECT, because we can GC only in the case
401 where it is a buffer, and live buffers are always protected.
402 I and its plist are also protected, via OBJECT. */
403 GCPRO3 (tail1
, sym1
, val1
);
405 /* Go through each element of PLIST. */
406 for (tail1
= plist
; CONSP (tail1
); tail1
= Fcdr (XCDR (tail1
)))
410 val1
= Fcar (XCDR (tail1
));
412 /* Go through I's plist, looking for sym1 */
413 for (tail2
= i
->plist
; CONSP (tail2
); tail2
= Fcdr (XCDR (tail2
)))
414 if (EQ (sym1
, XCAR (tail2
)))
416 /* No need to gcpro, because tail2 protects this
417 and it must be a cons cell (we get an error otherwise). */
418 register Lisp_Object this_cdr
;
420 this_cdr
= XCDR (tail2
);
421 /* Found the property. Now check its value. */
424 /* The properties have the same value on both lists.
425 Continue to the next property. */
426 if (EQ (val1
, Fcar (this_cdr
)))
429 /* Record this change in the buffer, for undo purposes. */
430 if (BUFFERP (object
))
432 record_property_change (i
->position
, LENGTH (i
),
433 sym1
, Fcar (this_cdr
), object
);
436 /* I's property has a different value -- change it */
437 if (set_type
== TEXT_PROPERTY_REPLACE
)
438 Fsetcar (this_cdr
, val1
);
440 if (CONSP (Fcar (this_cdr
)) &&
441 /* Special-case anonymous face properties. */
442 (! EQ (sym1
, Qface
) ||
443 NILP (Fkeywordp (Fcar (Fcar (this_cdr
))))))
444 /* The previous value is a list, so prepend (or
445 append) the new value to this list. */
446 if (set_type
== TEXT_PROPERTY_PREPEND
)
447 Fsetcar (this_cdr
, Fcons (val1
, Fcar (this_cdr
)));
449 nconc2 (Fcar (this_cdr
), list1 (val1
));
451 /* The previous value is a single value, so make it
453 if (set_type
== TEXT_PROPERTY_PREPEND
)
454 Fsetcar (this_cdr
, list2 (val1
, Fcar (this_cdr
)));
456 Fsetcar (this_cdr
, list2 (Fcar (this_cdr
), val1
));
465 /* Record this change in the buffer, for undo purposes. */
466 if (BUFFERP (object
))
468 record_property_change (i
->position
, LENGTH (i
),
471 set_interval_plist (i
, Fcons (sym1
, Fcons (val1
, i
->plist
)));
481 /* For any members of PLIST, or LIST,
482 which are properties of I, remove them from I's plist.
483 (If PLIST is non-nil, use that, otherwise use LIST.)
484 OBJECT is the string or buffer containing I. */
487 remove_properties (Lisp_Object plist
, Lisp_Object list
, INTERVAL i
, Lisp_Object object
)
489 Lisp_Object tail1
, tail2
, sym
, current_plist
;
492 /* True means tail1 is a plist, otherwise it is a list. */
495 current_plist
= i
->plist
;
498 tail1
= plist
, use_plist
= 1;
500 tail1
= list
, use_plist
= 0;
502 /* Go through each element of LIST or PLIST. */
503 while (CONSP (tail1
))
507 /* First, remove the symbol if it's at the head of the list */
508 while (CONSP (current_plist
) && EQ (sym
, XCAR (current_plist
)))
510 if (BUFFERP (object
))
511 record_property_change (i
->position
, LENGTH (i
),
512 sym
, XCAR (XCDR (current_plist
)),
515 current_plist
= XCDR (XCDR (current_plist
));
519 /* Go through I's plist, looking for SYM. */
520 tail2
= current_plist
;
521 while (! NILP (tail2
))
523 register Lisp_Object
this;
524 this = XCDR (XCDR (tail2
));
525 if (CONSP (this) && EQ (sym
, XCAR (this)))
527 if (BUFFERP (object
))
528 record_property_change (i
->position
, LENGTH (i
),
529 sym
, XCAR (XCDR (this)), object
);
531 Fsetcdr (XCDR (tail2
), XCDR (XCDR (this)));
537 /* Advance thru TAIL1 one way or the other. */
538 tail1
= XCDR (tail1
);
539 if (use_plist
&& CONSP (tail1
))
540 tail1
= XCDR (tail1
);
544 set_interval_plist (i
, current_plist
);
548 /* Returns the interval of POSITION in OBJECT.
549 POSITION is BEG-based. */
552 interval_of (ptrdiff_t position
, Lisp_Object object
)
558 XSETBUFFER (object
, current_buffer
);
559 else if (EQ (object
, Qt
))
562 CHECK_STRING_OR_BUFFER (object
);
564 if (BUFFERP (object
))
566 register struct buffer
*b
= XBUFFER (object
);
570 i
= buffer_intervals (b
);
575 end
= SCHARS (object
);
576 i
= string_intervals (object
);
579 if (!(beg
<= position
&& position
<= end
))
580 args_out_of_range (make_number (position
), make_number (position
));
581 if (beg
== end
|| !i
)
584 return find_interval (i
, position
);
587 DEFUN ("text-properties-at", Ftext_properties_at
,
588 Stext_properties_at
, 1, 2, 0,
589 doc
: /* Return the list of properties of the character at POSITION in OBJECT.
590 If the optional second argument OBJECT is a buffer (or nil, which means
591 the current buffer), POSITION is a buffer position (integer or marker).
592 If OBJECT is a string, POSITION is a 0-based index into it.
593 If POSITION is at the end of OBJECT, the value is nil. */)
594 (Lisp_Object position
, Lisp_Object object
)
599 XSETBUFFER (object
, current_buffer
);
601 i
= validate_interval_range (object
, &position
, &position
, soft
);
604 /* If POSITION is at the end of the interval,
605 it means it's the end of OBJECT.
606 There are no properties at the very end,
607 since no character follows. */
608 if (XINT (position
) == LENGTH (i
) + i
->position
)
614 DEFUN ("get-text-property", Fget_text_property
, Sget_text_property
, 2, 3, 0,
615 doc
: /* Return the value of POSITION's property PROP, in OBJECT.
616 OBJECT should be a buffer or a string; if omitted or nil, it defaults
617 to the current buffer.
618 If POSITION is at the end of OBJECT, the value is nil. */)
619 (Lisp_Object position
, Lisp_Object prop
, Lisp_Object object
)
621 return textget (Ftext_properties_at (position
, object
), prop
);
624 /* Return the value of char's property PROP, in OBJECT at POSITION.
625 OBJECT is optional and defaults to the current buffer.
626 If OVERLAY is non-0, then in the case that the returned property is from
627 an overlay, the overlay found is returned in *OVERLAY, otherwise nil is
628 returned in *OVERLAY.
629 If POSITION is at the end of OBJECT, the value is nil.
630 If OBJECT is a buffer, then overlay properties are considered as well as
632 If OBJECT is a window, then that window's buffer is used, but
633 window-specific overlays are considered only if they are associated
636 get_char_property_and_overlay (Lisp_Object position
, register Lisp_Object prop
, Lisp_Object object
, Lisp_Object
*overlay
)
638 struct window
*w
= 0;
640 CHECK_NUMBER_COERCE_MARKER (position
);
643 XSETBUFFER (object
, current_buffer
);
645 if (WINDOWP (object
))
647 CHECK_LIVE_WINDOW (object
);
648 w
= XWINDOW (object
);
649 object
= w
->contents
;
651 if (BUFFERP (object
))
654 Lisp_Object
*overlay_vec
;
655 struct buffer
*obuf
= current_buffer
;
657 if (XINT (position
) < BUF_BEGV (XBUFFER (object
))
658 || XINT (position
) > BUF_ZV (XBUFFER (object
)))
659 xsignal1 (Qargs_out_of_range
, position
);
661 set_buffer_temp (XBUFFER (object
));
664 GET_OVERLAYS_AT (XINT (position
), overlay_vec
, noverlays
, NULL
, 0);
665 noverlays
= sort_overlays (overlay_vec
, noverlays
, w
);
667 set_buffer_temp (obuf
);
669 /* Now check the overlays in order of decreasing priority. */
670 while (--noverlays
>= 0)
672 Lisp_Object tem
= Foverlay_get (overlay_vec
[noverlays
], prop
);
676 /* Return the overlay we got the property from. */
677 *overlay
= overlay_vec
[noverlays
];
686 /* Indicate that the return value is not from an overlay. */
689 /* Not a buffer, or no appropriate overlay, so fall through to the
691 return Fget_text_property (position
, prop
, object
);
694 DEFUN ("get-char-property", Fget_char_property
, Sget_char_property
, 2, 3, 0,
695 doc
: /* Return the value of POSITION's property PROP, in OBJECT.
696 Both overlay properties and text properties are checked.
697 OBJECT is optional and defaults to the current buffer.
698 If POSITION is at the end of OBJECT, the value is nil.
699 If OBJECT is a buffer, then overlay properties are considered as well as
701 If OBJECT is a window, then that window's buffer is used, but window-specific
702 overlays are considered only if they are associated with OBJECT. */)
703 (Lisp_Object position
, Lisp_Object prop
, Lisp_Object object
)
705 return get_char_property_and_overlay (position
, prop
, object
, 0);
708 DEFUN ("get-char-property-and-overlay", Fget_char_property_and_overlay
,
709 Sget_char_property_and_overlay
, 2, 3, 0,
710 doc
: /* Like `get-char-property', but with extra overlay information.
711 The value is a cons cell. Its car is the return value of `get-char-property'
712 with the same arguments--that is, the value of POSITION's property
713 PROP in OBJECT. Its cdr is the overlay in which the property was
714 found, or nil, if it was found as a text property or not found at all.
716 OBJECT is optional and defaults to the current buffer. OBJECT may be
717 a string, a buffer or a window. For strings, the cdr of the return
718 value is always nil, since strings do not have overlays. If OBJECT is
719 a window, then that window's buffer is used, but window-specific
720 overlays are considered only if they are associated with OBJECT. If
721 POSITION is at the end of OBJECT, both car and cdr are nil. */)
722 (Lisp_Object position
, Lisp_Object prop
, Lisp_Object object
)
726 = get_char_property_and_overlay (position
, prop
, object
, &overlay
);
727 return Fcons (val
, overlay
);
731 DEFUN ("next-char-property-change", Fnext_char_property_change
,
732 Snext_char_property_change
, 1, 2, 0,
733 doc
: /* Return the position of next text property or overlay change.
734 This scans characters forward in the current buffer from POSITION till
735 it finds a change in some text property, or the beginning or end of an
736 overlay, and returns the position of that.
737 If none is found up to (point-max), the function returns (point-max).
739 If the optional second argument LIMIT is non-nil, don't search
740 past position LIMIT; return LIMIT if nothing is found before LIMIT.
741 LIMIT is a no-op if it is greater than (point-max). */)
742 (Lisp_Object position
, Lisp_Object limit
)
746 temp
= Fnext_overlay_change (position
);
749 CHECK_NUMBER_COERCE_MARKER (limit
);
750 if (XINT (limit
) < XINT (temp
))
753 return Fnext_property_change (position
, Qnil
, temp
);
756 DEFUN ("previous-char-property-change", Fprevious_char_property_change
,
757 Sprevious_char_property_change
, 1, 2, 0,
758 doc
: /* Return the position of previous text property or overlay change.
759 Scans characters backward in the current buffer from POSITION till it
760 finds a change in some text property, or the beginning or end of an
761 overlay, and returns the position of that.
762 If none is found since (point-min), the function returns (point-min).
764 If the optional second argument LIMIT is non-nil, don't search
765 past position LIMIT; return LIMIT if nothing is found before LIMIT.
766 LIMIT is a no-op if it is less than (point-min). */)
767 (Lisp_Object position
, Lisp_Object limit
)
771 temp
= Fprevious_overlay_change (position
);
774 CHECK_NUMBER_COERCE_MARKER (limit
);
775 if (XINT (limit
) > XINT (temp
))
778 return Fprevious_property_change (position
, Qnil
, temp
);
782 DEFUN ("next-single-char-property-change", Fnext_single_char_property_change
,
783 Snext_single_char_property_change
, 2, 4, 0,
784 doc
: /* Return the position of next text property or overlay change for a specific property.
785 Scans characters forward from POSITION till it finds
786 a change in the PROP property, then returns the position of the change.
787 If the optional third argument OBJECT is a buffer (or nil, which means
788 the current buffer), POSITION is a buffer position (integer or marker).
789 If OBJECT is a string, POSITION is a 0-based index into it.
791 In a string, scan runs to the end of the string.
792 In a buffer, it runs to (point-max), and the value cannot exceed that.
794 The property values are compared with `eq'.
795 If the property is constant all the way to the end of OBJECT, return the
796 last valid position in OBJECT.
797 If the optional fourth argument LIMIT is non-nil, don't search
798 past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
799 (Lisp_Object position
, Lisp_Object prop
, Lisp_Object object
, Lisp_Object limit
)
801 if (STRINGP (object
))
803 position
= Fnext_single_property_change (position
, prop
, object
, limit
);
807 position
= make_number (SCHARS (object
));
810 CHECK_NUMBER (limit
);
817 Lisp_Object initial_value
, value
;
818 ptrdiff_t count
= SPECPDL_INDEX ();
821 CHECK_BUFFER (object
);
823 if (BUFFERP (object
) && current_buffer
!= XBUFFER (object
))
825 record_unwind_current_buffer ();
826 Fset_buffer (object
);
829 CHECK_NUMBER_COERCE_MARKER (position
);
831 initial_value
= Fget_char_property (position
, prop
, object
);
834 XSETFASTINT (limit
, ZV
);
836 CHECK_NUMBER_COERCE_MARKER (limit
);
838 if (XFASTINT (position
) >= XFASTINT (limit
))
841 if (XFASTINT (position
) > ZV
)
842 XSETFASTINT (position
, ZV
);
847 position
= Fnext_char_property_change (position
, limit
);
848 if (XFASTINT (position
) >= XFASTINT (limit
))
854 value
= Fget_char_property (position
, prop
, object
);
855 if (!EQ (value
, initial_value
))
859 unbind_to (count
, Qnil
);
865 DEFUN ("previous-single-char-property-change",
866 Fprevious_single_char_property_change
,
867 Sprevious_single_char_property_change
, 2, 4, 0,
868 doc
: /* Return the position of previous text property or overlay change for a specific property.
869 Scans characters backward from POSITION till it finds
870 a change in the PROP property, then returns the position of the change.
871 If the optional third argument OBJECT is a buffer (or nil, which means
872 the current buffer), POSITION is a buffer position (integer or marker).
873 If OBJECT is a string, POSITION is a 0-based index into it.
875 In a string, scan runs to the start of the string.
876 In a buffer, it runs to (point-min), and the value cannot be less than that.
878 The property values are compared with `eq'.
879 If the property is constant all the way to the start of OBJECT, return the
880 first valid position in OBJECT.
881 If the optional fourth argument LIMIT is non-nil, don't search back past
882 position LIMIT; return LIMIT if nothing is found before reaching LIMIT. */)
883 (Lisp_Object position
, Lisp_Object prop
, Lisp_Object object
, Lisp_Object limit
)
885 if (STRINGP (object
))
887 position
= Fprevious_single_property_change (position
, prop
, object
, limit
);
891 position
= make_number (0);
894 CHECK_NUMBER (limit
);
901 ptrdiff_t count
= SPECPDL_INDEX ();
904 CHECK_BUFFER (object
);
906 if (BUFFERP (object
) && current_buffer
!= XBUFFER (object
))
908 record_unwind_current_buffer ();
909 Fset_buffer (object
);
912 CHECK_NUMBER_COERCE_MARKER (position
);
915 XSETFASTINT (limit
, BEGV
);
917 CHECK_NUMBER_COERCE_MARKER (limit
);
919 if (XFASTINT (position
) <= XFASTINT (limit
))
922 if (XFASTINT (position
) < BEGV
)
923 XSETFASTINT (position
, BEGV
);
927 Lisp_Object initial_value
928 = Fget_char_property (make_number (XFASTINT (position
) - 1),
933 position
= Fprevious_char_property_change (position
, limit
);
935 if (XFASTINT (position
) <= XFASTINT (limit
))
943 = Fget_char_property (make_number (XFASTINT (position
) - 1),
946 if (!EQ (value
, initial_value
))
952 unbind_to (count
, Qnil
);
958 DEFUN ("next-property-change", Fnext_property_change
,
959 Snext_property_change
, 1, 3, 0,
960 doc
: /* Return the position of next property change.
961 Scans characters forward from POSITION in OBJECT till it finds
962 a change in some text property, then returns the position of the change.
963 If the optional second argument OBJECT is a buffer (or nil, which means
964 the current buffer), POSITION is a buffer position (integer or marker).
965 If OBJECT is a string, POSITION is a 0-based index into it.
966 Return nil if the property is constant all the way to the end of OBJECT.
967 If the value is non-nil, it is a position greater than POSITION, never equal.
969 If the optional third argument LIMIT is non-nil, don't search
970 past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
971 (Lisp_Object position
, Lisp_Object object
, Lisp_Object limit
)
973 register INTERVAL i
, next
;
976 XSETBUFFER (object
, current_buffer
);
978 if (!NILP (limit
) && !EQ (limit
, Qt
))
979 CHECK_NUMBER_COERCE_MARKER (limit
);
981 i
= validate_interval_range (object
, &position
, &position
, soft
);
983 /* If LIMIT is t, return start of next interval--don't
984 bother checking further intervals. */
990 next
= next_interval (i
);
993 XSETFASTINT (position
, (STRINGP (object
)
995 : BUF_ZV (XBUFFER (object
))));
997 XSETFASTINT (position
, next
->position
);
1004 next
= next_interval (i
);
1006 while (next
&& intervals_equal (i
, next
)
1007 && (NILP (limit
) || next
->position
< XFASTINT (limit
)))
1008 next
= next_interval (next
);
1012 >= (INTEGERP (limit
)
1016 : BUF_ZV (XBUFFER (object
))))))
1019 return make_number (next
->position
);
1022 DEFUN ("next-single-property-change", Fnext_single_property_change
,
1023 Snext_single_property_change
, 2, 4, 0,
1024 doc
: /* Return the position of next property change for a specific property.
1025 Scans characters forward from POSITION till it finds
1026 a change in the PROP property, then returns the position of the change.
1027 If the optional third argument OBJECT is a buffer (or nil, which means
1028 the current buffer), POSITION is a buffer position (integer or marker).
1029 If OBJECT is a string, POSITION is a 0-based index into it.
1030 The property values are compared with `eq'.
1031 Return nil if the property is constant all the way to the end of OBJECT.
1032 If the value is non-nil, it is a position greater than POSITION, never equal.
1034 If the optional fourth argument LIMIT is non-nil, don't search
1035 past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
1036 (Lisp_Object position
, Lisp_Object prop
, Lisp_Object object
, Lisp_Object limit
)
1038 register INTERVAL i
, next
;
1039 register Lisp_Object here_val
;
1042 XSETBUFFER (object
, current_buffer
);
1045 CHECK_NUMBER_COERCE_MARKER (limit
);
1047 i
= validate_interval_range (object
, &position
, &position
, soft
);
1051 here_val
= textget (i
->plist
, prop
);
1052 next
= next_interval (i
);
1054 && EQ (here_val
, textget (next
->plist
, prop
))
1055 && (NILP (limit
) || next
->position
< XFASTINT (limit
)))
1056 next
= next_interval (next
);
1060 >= (INTEGERP (limit
)
1064 : BUF_ZV (XBUFFER (object
))))))
1067 return make_number (next
->position
);
1070 DEFUN ("previous-property-change", Fprevious_property_change
,
1071 Sprevious_property_change
, 1, 3, 0,
1072 doc
: /* Return the position of previous property change.
1073 Scans characters backwards from POSITION in OBJECT till it finds
1074 a change in some text property, then returns the position of the change.
1075 If the optional second argument OBJECT is a buffer (or nil, which means
1076 the current buffer), POSITION is a buffer position (integer or marker).
1077 If OBJECT is a string, POSITION is a 0-based index into it.
1078 Return nil if the property is constant all the way to the start of OBJECT.
1079 If the value is non-nil, it is a position less than POSITION, never equal.
1081 If the optional third argument LIMIT is non-nil, don't search
1082 back past position LIMIT; return LIMIT if nothing is found until LIMIT. */)
1083 (Lisp_Object position
, Lisp_Object object
, Lisp_Object limit
)
1085 register INTERVAL i
, previous
;
1088 XSETBUFFER (object
, current_buffer
);
1091 CHECK_NUMBER_COERCE_MARKER (limit
);
1093 i
= validate_interval_range (object
, &position
, &position
, soft
);
1097 /* Start with the interval containing the char before point. */
1098 if (i
->position
== XFASTINT (position
))
1099 i
= previous_interval (i
);
1101 previous
= previous_interval (i
);
1102 while (previous
&& intervals_equal (previous
, i
)
1104 || (previous
->position
+ LENGTH (previous
) > XFASTINT (limit
))))
1105 previous
= previous_interval (previous
);
1108 || (previous
->position
+ LENGTH (previous
)
1109 <= (INTEGERP (limit
)
1111 : (STRINGP (object
) ? 0 : BUF_BEGV (XBUFFER (object
))))))
1114 return make_number (previous
->position
+ LENGTH (previous
));
1117 DEFUN ("previous-single-property-change", Fprevious_single_property_change
,
1118 Sprevious_single_property_change
, 2, 4, 0,
1119 doc
: /* Return the position of previous property change for a specific property.
1120 Scans characters backward from POSITION till it finds
1121 a change in the PROP property, then returns the position of the change.
1122 If the optional third argument OBJECT is a buffer (or nil, which means
1123 the current buffer), POSITION is a buffer position (integer or marker).
1124 If OBJECT is a string, POSITION is a 0-based index into it.
1125 The property values are compared with `eq'.
1126 Return nil if the property is constant all the way to the start of OBJECT.
1127 If the value is non-nil, it is a position less than POSITION, never equal.
1129 If the optional fourth argument LIMIT is non-nil, don't search
1130 back past position LIMIT; return LIMIT if nothing is found until LIMIT. */)
1131 (Lisp_Object position
, Lisp_Object prop
, Lisp_Object object
, Lisp_Object limit
)
1133 register INTERVAL i
, previous
;
1134 register Lisp_Object here_val
;
1137 XSETBUFFER (object
, current_buffer
);
1140 CHECK_NUMBER_COERCE_MARKER (limit
);
1142 i
= validate_interval_range (object
, &position
, &position
, soft
);
1144 /* Start with the interval containing the char before point. */
1145 if (i
&& i
->position
== XFASTINT (position
))
1146 i
= previous_interval (i
);
1151 here_val
= textget (i
->plist
, prop
);
1152 previous
= previous_interval (i
);
1154 && EQ (here_val
, textget (previous
->plist
, prop
))
1156 || (previous
->position
+ LENGTH (previous
) > XFASTINT (limit
))))
1157 previous
= previous_interval (previous
);
1160 || (previous
->position
+ LENGTH (previous
)
1161 <= (INTEGERP (limit
)
1163 : (STRINGP (object
) ? 0 : BUF_BEGV (XBUFFER (object
))))))
1166 return make_number (previous
->position
+ LENGTH (previous
));
1169 /* Used by add-text-properties and add-face-text-property. */
1172 add_text_properties_1 (Lisp_Object start
, Lisp_Object end
,
1173 Lisp_Object properties
, Lisp_Object object
,
1174 enum property_set_type set_type
) {
1175 INTERVAL i
, unchanged
;
1178 struct gcpro gcpro1
;
1179 bool first_time
= 1;
1181 properties
= validate_plist (properties
);
1182 if (NILP (properties
))
1186 XSETBUFFER (object
, current_buffer
);
1189 i
= validate_interval_range (object
, &start
, &end
, hard
);
1194 len
= XINT (end
) - s
;
1196 /* No need to protect OBJECT, because we GC only if it's a buffer,
1197 and live buffers are always protected. */
1198 GCPRO1 (properties
);
1200 /* If this interval already has the properties, we can skip it. */
1201 if (interval_has_all_properties (properties
, i
))
1203 ptrdiff_t got
= LENGTH (i
) - (s
- i
->position
);
1208 RETURN_UNGCPRO (Qnil
);
1210 i
= next_interval (i
);
1213 while (interval_has_all_properties (properties
, i
));
1215 else if (i
->position
!= s
)
1217 /* If we're not starting on an interval boundary, we have to
1218 split this interval. */
1220 i
= split_interval_right (unchanged
, s
- unchanged
->position
);
1221 copy_properties (unchanged
, i
);
1224 if (BUFFERP (object
) && first_time
)
1226 ptrdiff_t prev_total_length
= TOTAL_LENGTH (i
);
1227 ptrdiff_t prev_pos
= i
->position
;
1229 modify_text_properties (object
, start
, end
);
1230 /* If someone called us recursively as a side effect of
1231 modify_text_properties, and changed the intervals behind our back
1232 (could happen if lock_file, called by prepare_to_modify_buffer,
1233 triggers redisplay, and that calls add-text-properties again
1234 in the same buffer), we cannot continue with I, because its
1235 data changed. So we restart the interval analysis anew. */
1236 if (TOTAL_LENGTH (i
) != prev_total_length
1237 || i
->position
!= prev_pos
)
1244 /* We are at the beginning of interval I, with LEN chars to scan. */
1249 if (LENGTH (i
) >= len
)
1251 /* We can UNGCPRO safely here, because there will be just
1252 one more chance to gc, in the next call to add_properties,
1253 and after that we will not need PROPERTIES or OBJECT again. */
1256 if (interval_has_all_properties (properties
, i
))
1258 if (BUFFERP (object
))
1259 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1260 XINT (end
) - XINT (start
));
1266 if (LENGTH (i
) == len
)
1268 add_properties (properties
, i
, object
, set_type
);
1269 if (BUFFERP (object
))
1270 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1271 XINT (end
) - XINT (start
));
1275 /* i doesn't have the properties, and goes past the change limit */
1277 i
= split_interval_left (unchanged
, len
);
1278 copy_properties (unchanged
, i
);
1279 add_properties (properties
, i
, object
, set_type
);
1280 if (BUFFERP (object
))
1281 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1282 XINT (end
) - XINT (start
));
1287 modified
|= add_properties (properties
, i
, object
, set_type
);
1288 i
= next_interval (i
);
1292 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
1294 DEFUN ("add-text-properties", Fadd_text_properties
,
1295 Sadd_text_properties
, 3, 4, 0,
1296 doc
: /* Add properties to the text from START to END.
1297 The third argument PROPERTIES is a property list
1298 specifying the property values to add. If the optional fourth argument
1299 OBJECT is a buffer (or nil, which means the current buffer),
1300 START and END are buffer positions (integers or markers).
1301 If OBJECT is a string, START and END are 0-based indices into it.
1302 Return t if any property value actually changed, nil otherwise. */)
1303 (Lisp_Object start
, Lisp_Object end
, Lisp_Object properties
,
1306 return add_text_properties_1 (start
, end
, properties
, object
,
1307 TEXT_PROPERTY_REPLACE
);
1310 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
1312 DEFUN ("put-text-property", Fput_text_property
,
1313 Sput_text_property
, 4, 5, 0,
1314 doc
: /* Set one property of the text from START to END.
1315 The third and fourth arguments PROPERTY and VALUE
1316 specify the property to add.
1317 If the optional fifth argument OBJECT is a buffer (or nil, which means
1318 the current buffer), START and END are buffer positions (integers or
1319 markers). If OBJECT is a string, START and END are 0-based indices into it. */)
1320 (Lisp_Object start
, Lisp_Object end
, Lisp_Object property
,
1321 Lisp_Object value
, Lisp_Object object
)
1323 Fadd_text_properties (start
, end
, scoped_list2 (property
, value
), object
);
1327 DEFUN ("set-text-properties", Fset_text_properties
,
1328 Sset_text_properties
, 3, 4, 0,
1329 doc
: /* Completely replace properties of text from START to END.
1330 The third argument PROPERTIES is the new property list.
1331 If the optional fourth argument OBJECT is a buffer (or nil, which means
1332 the current buffer), START and END are buffer positions (integers or
1333 markers). If OBJECT is a string, START and END are 0-based indices into it.
1334 If PROPERTIES is nil, the effect is to remove all properties from
1335 the designated part of OBJECT. */)
1336 (Lisp_Object start
, Lisp_Object end
, Lisp_Object properties
, Lisp_Object object
)
1338 return set_text_properties (start
, end
, properties
, object
, Qt
);
1342 DEFUN ("add-face-text-property", Fadd_face_text_property
,
1343 Sadd_face_text_property
, 3, 5, 0,
1344 doc
: /* Add the face property to the text from START to END.
1345 FACE specifies the face to add. It should be a valid value of the
1346 `face' property (typically a face name or a plist of face attributes
1349 If any text in the region already has a non-nil `face' property, those
1350 face(s) are retained. This is done by setting the `face' property to
1351 a list of faces, with FACE as the first element (by default) and the
1352 pre-existing faces as the remaining elements.
1354 If optional fourth argument APPEND is non-nil, append FACE to the end
1355 of the face list instead.
1357 If optional fifth argument OBJECT is a buffer (or nil, which means the
1358 current buffer), START and END are buffer positions (integers or
1359 markers). If OBJECT is a string, START and END are 0-based indices
1361 (Lisp_Object start
, Lisp_Object end
, Lisp_Object face
,
1362 Lisp_Object append
, Lisp_Object object
)
1364 add_text_properties_1 (start
, end
, scoped_list2 (Qface
, face
), object
,
1366 ? TEXT_PROPERTY_PREPEND
1367 : TEXT_PROPERTY_APPEND
));
1371 /* Replace properties of text from START to END with new list of
1372 properties PROPERTIES. OBJECT is the buffer or string containing
1373 the text. OBJECT nil means use the current buffer.
1374 COHERENT_CHANGE_P nil means this is being called as an internal
1375 subroutine, rather than as a change primitive with checking of
1376 read-only, invoking change hooks, etc.. Value is nil if the
1377 function _detected_ that it did not replace any properties, non-nil
1381 set_text_properties (Lisp_Object start
, Lisp_Object end
, Lisp_Object properties
,
1382 Lisp_Object object
, Lisp_Object coherent_change_p
)
1384 register INTERVAL i
;
1385 Lisp_Object ostart
, oend
;
1390 properties
= validate_plist (properties
);
1393 XSETBUFFER (object
, current_buffer
);
1395 /* If we want no properties for a whole string,
1396 get rid of its intervals. */
1397 if (NILP (properties
) && STRINGP (object
)
1398 && XFASTINT (start
) == 0
1399 && XFASTINT (end
) == SCHARS (object
))
1401 if (!string_intervals (object
))
1404 set_string_intervals (object
, NULL
);
1408 i
= validate_interval_range (object
, &start
, &end
, soft
);
1412 /* If buffer has no properties, and we want none, return now. */
1413 if (NILP (properties
))
1416 /* Restore the original START and END values
1417 because validate_interval_range increments them for strings. */
1421 i
= validate_interval_range (object
, &start
, &end
, hard
);
1422 /* This can return if start == end. */
1427 if (BUFFERP (object
) && !NILP (coherent_change_p
))
1428 modify_text_properties (object
, start
, end
);
1430 set_text_properties_1 (start
, end
, properties
, object
, i
);
1432 if (BUFFERP (object
) && !NILP (coherent_change_p
))
1433 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1434 XINT (end
) - XINT (start
));
1438 /* Replace properties of text from START to END with new list of
1439 properties PROPERTIES. OBJECT is the buffer or string containing
1440 the text. This does not obey any hooks.
1441 You should provide the interval that START is located in as I.
1442 START and END can be in any order. */
1445 set_text_properties_1 (Lisp_Object start
, Lisp_Object end
, Lisp_Object properties
, Lisp_Object object
, INTERVAL i
)
1447 register INTERVAL prev_changed
= NULL
;
1448 register ptrdiff_t s
, len
;
1451 if (XINT (start
) < XINT (end
))
1454 len
= XINT (end
) - s
;
1456 else if (XINT (end
) < XINT (start
))
1459 len
= XINT (start
) - s
;
1466 if (i
->position
!= s
)
1469 i
= split_interval_right (unchanged
, s
- unchanged
->position
);
1471 if (LENGTH (i
) > len
)
1473 copy_properties (unchanged
, i
);
1474 i
= split_interval_left (i
, len
);
1475 set_properties (properties
, i
, object
);
1479 set_properties (properties
, i
, object
);
1481 if (LENGTH (i
) == len
)
1486 i
= next_interval (i
);
1489 /* We are starting at the beginning of an interval I. LEN is positive. */
1494 if (LENGTH (i
) >= len
)
1496 if (LENGTH (i
) > len
)
1497 i
= split_interval_left (i
, len
);
1499 /* We have to call set_properties even if we are going to
1500 merge the intervals, so as to make the undo records
1501 and cause redisplay to happen. */
1502 set_properties (properties
, i
, object
);
1504 merge_interval_left (i
);
1510 /* We have to call set_properties even if we are going to
1511 merge the intervals, so as to make the undo records
1512 and cause redisplay to happen. */
1513 set_properties (properties
, i
, object
);
1517 prev_changed
= i
= merge_interval_left (i
);
1519 i
= next_interval (i
);
1524 DEFUN ("remove-text-properties", Fremove_text_properties
,
1525 Sremove_text_properties
, 3, 4, 0,
1526 doc
: /* Remove some properties from text from START to END.
1527 The third argument PROPERTIES is a property list
1528 whose property names specify the properties to remove.
1529 \(The values stored in PROPERTIES are ignored.)
1530 If the optional fourth argument OBJECT is a buffer (or nil, which means
1531 the current buffer), START and END are buffer positions (integers or
1532 markers). If OBJECT is a string, START and END are 0-based indices into it.
1533 Return t if any property was actually removed, nil otherwise.
1535 Use `set-text-properties' if you want to remove all text properties. */)
1536 (Lisp_Object start
, Lisp_Object end
, Lisp_Object properties
, Lisp_Object object
)
1538 INTERVAL i
, unchanged
;
1541 bool first_time
= 1;
1544 XSETBUFFER (object
, current_buffer
);
1547 i
= validate_interval_range (object
, &start
, &end
, soft
);
1552 len
= XINT (end
) - s
;
1554 /* If there are no properties on this entire interval, return. */
1555 if (! interval_has_some_properties (properties
, i
))
1557 ptrdiff_t got
= LENGTH (i
) - (s
- i
->position
);
1564 i
= next_interval (i
);
1567 while (! interval_has_some_properties (properties
, i
));
1569 /* Split away the beginning of this interval; what we don't
1571 else if (i
->position
!= s
)
1574 i
= split_interval_right (unchanged
, s
- unchanged
->position
);
1575 copy_properties (unchanged
, i
);
1578 if (BUFFERP (object
) && first_time
)
1580 ptrdiff_t prev_total_length
= TOTAL_LENGTH (i
);
1581 ptrdiff_t prev_pos
= i
->position
;
1583 modify_text_properties (object
, start
, end
);
1584 /* If someone called us recursively as a side effect of
1585 modify_text_properties, and changed the intervals behind our back
1586 (could happen if lock_file, called by prepare_to_modify_buffer,
1587 triggers redisplay, and that calls add-text-properties again
1588 in the same buffer), we cannot continue with I, because its
1589 data changed. So we restart the interval analysis anew. */
1590 if (TOTAL_LENGTH (i
) != prev_total_length
1591 || i
->position
!= prev_pos
)
1598 /* We are at the beginning of an interval, with len to scan */
1603 if (LENGTH (i
) >= len
)
1605 if (! interval_has_some_properties (properties
, i
))
1608 if (BUFFERP (object
))
1609 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1610 XINT (end
) - XINT (start
));
1614 if (LENGTH (i
) == len
)
1616 remove_properties (properties
, Qnil
, i
, object
);
1617 if (BUFFERP (object
))
1618 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1619 XINT (end
) - XINT (start
));
1623 /* i has the properties, and goes past the change limit */
1625 i
= split_interval_left (i
, len
);
1626 copy_properties (unchanged
, i
);
1627 remove_properties (properties
, Qnil
, i
, object
);
1628 if (BUFFERP (object
))
1629 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1630 XINT (end
) - XINT (start
));
1635 modified
|= remove_properties (properties
, Qnil
, i
, object
);
1636 i
= next_interval (i
);
1640 DEFUN ("remove-list-of-text-properties", Fremove_list_of_text_properties
,
1641 Sremove_list_of_text_properties
, 3, 4, 0,
1642 doc
: /* Remove some properties from text from START to END.
1643 The third argument LIST-OF-PROPERTIES is a list of property names to remove.
1644 If the optional fourth argument OBJECT is a buffer (or nil, which means
1645 the current buffer), START and END are buffer positions (integers or
1646 markers). If OBJECT is a string, START and END are 0-based indices into it.
1647 Return t if any property was actually removed, nil otherwise. */)
1648 (Lisp_Object start
, Lisp_Object end
, Lisp_Object list_of_properties
, Lisp_Object object
)
1650 INTERVAL i
, unchanged
;
1653 Lisp_Object properties
;
1654 properties
= list_of_properties
;
1657 XSETBUFFER (object
, current_buffer
);
1659 i
= validate_interval_range (object
, &start
, &end
, soft
);
1664 len
= XINT (end
) - s
;
1666 /* If there are no properties on the interval, return. */
1667 if (! interval_has_some_properties_list (properties
, i
))
1669 ptrdiff_t got
= LENGTH (i
) - (s
- i
->position
);
1676 i
= next_interval (i
);
1679 while (! interval_has_some_properties_list (properties
, i
));
1681 /* Split away the beginning of this interval; what we don't
1683 else if (i
->position
!= s
)
1686 i
= split_interval_right (unchanged
, s
- unchanged
->position
);
1687 copy_properties (unchanged
, i
);
1690 /* We are at the beginning of an interval, with len to scan.
1691 The flag `modified' records if changes have been made.
1692 When object is a buffer, we must call modify_text_properties
1693 before changes are made and signal_after_change when we are done.
1694 We call modify_text_properties before calling remove_properties if modified == 0,
1695 and we call signal_after_change before returning if modified != 0. */
1700 if (LENGTH (i
) >= len
)
1702 if (! interval_has_some_properties_list (properties
, i
))
1706 if (BUFFERP (object
))
1707 signal_after_change (XINT (start
),
1708 XINT (end
) - XINT (start
),
1709 XINT (end
) - XINT (start
));
1715 else if (LENGTH (i
) == len
)
1717 if (!modified
&& BUFFERP (object
))
1718 modify_text_properties (object
, start
, end
);
1719 remove_properties (Qnil
, properties
, i
, object
);
1720 if (BUFFERP (object
))
1721 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1722 XINT (end
) - XINT (start
));
1726 { /* i has the properties, and goes past the change limit. */
1728 i
= split_interval_left (i
, len
);
1729 copy_properties (unchanged
, i
);
1730 if (!modified
&& BUFFERP (object
))
1731 modify_text_properties (object
, start
, end
);
1732 remove_properties (Qnil
, properties
, i
, object
);
1733 if (BUFFERP (object
))
1734 signal_after_change (XINT (start
), XINT (end
) - XINT (start
),
1735 XINT (end
) - XINT (start
));
1739 if (interval_has_some_properties_list (properties
, i
))
1741 if (!modified
&& BUFFERP (object
))
1742 modify_text_properties (object
, start
, end
);
1743 remove_properties (Qnil
, properties
, i
, object
);
1747 i
= next_interval (i
);
1752 if (BUFFERP (object
))
1753 signal_after_change (XINT (start
),
1754 XINT (end
) - XINT (start
),
1755 XINT (end
) - XINT (start
));
1764 DEFUN ("text-property-any", Ftext_property_any
,
1765 Stext_property_any
, 4, 5, 0,
1766 doc
: /* Check text from START to END for property PROPERTY equaling VALUE.
1767 If so, return the position of the first character whose property PROPERTY
1768 is `eq' to VALUE. Otherwise return nil.
1769 If the optional fifth argument OBJECT is a buffer (or nil, which means
1770 the current buffer), START and END are buffer positions (integers or
1771 markers). If OBJECT is a string, START and END are 0-based indices into it. */)
1772 (Lisp_Object start
, Lisp_Object end
, Lisp_Object property
, Lisp_Object value
, Lisp_Object object
)
1774 register INTERVAL i
;
1775 register ptrdiff_t e
, pos
;
1778 XSETBUFFER (object
, current_buffer
);
1779 i
= validate_interval_range (object
, &start
, &end
, soft
);
1781 return (!NILP (value
) || EQ (start
, end
) ? Qnil
: start
);
1786 if (i
->position
>= e
)
1788 if (EQ (textget (i
->plist
, property
), value
))
1791 if (pos
< XINT (start
))
1793 return make_number (pos
);
1795 i
= next_interval (i
);
1800 DEFUN ("text-property-not-all", Ftext_property_not_all
,
1801 Stext_property_not_all
, 4, 5, 0,
1802 doc
: /* Check text from START to END for property PROPERTY not equaling VALUE.
1803 If so, return the position of the first character whose property PROPERTY
1804 is not `eq' to VALUE. Otherwise, return nil.
1805 If the optional fifth argument OBJECT is a buffer (or nil, which means
1806 the current buffer), START and END are buffer positions (integers or
1807 markers). If OBJECT is a string, START and END are 0-based indices into it. */)
1808 (Lisp_Object start
, Lisp_Object end
, Lisp_Object property
, Lisp_Object value
, Lisp_Object object
)
1810 register INTERVAL i
;
1811 register ptrdiff_t s
, e
;
1814 XSETBUFFER (object
, current_buffer
);
1815 i
= validate_interval_range (object
, &start
, &end
, soft
);
1817 return (NILP (value
) || EQ (start
, end
)) ? Qnil
: start
;
1823 if (i
->position
>= e
)
1825 if (! EQ (textget (i
->plist
, property
), value
))
1827 if (i
->position
> s
)
1829 return make_number (s
);
1831 i
= next_interval (i
);
1837 /* Return the direction from which the text-property PROP would be
1838 inherited by any new text inserted at POS: 1 if it would be
1839 inherited from the char after POS, -1 if it would be inherited from
1840 the char before POS, and 0 if from neither.
1841 BUFFER can be either a buffer or nil (meaning current buffer). */
1844 text_property_stickiness (Lisp_Object prop
, Lisp_Object pos
, Lisp_Object buffer
)
1846 bool ignore_previous_character
;
1847 Lisp_Object prev_pos
= make_number (XINT (pos
) - 1);
1848 Lisp_Object front_sticky
;
1849 bool is_rear_sticky
= true, is_front_sticky
= false; /* defaults */
1850 Lisp_Object defalt
= Fassq (prop
, Vtext_property_default_nonsticky
);
1853 XSETBUFFER (buffer
, current_buffer
);
1855 ignore_previous_character
= XINT (pos
) <= BUF_BEGV (XBUFFER (buffer
));
1857 if (ignore_previous_character
|| (CONSP (defalt
) && !NILP (XCDR (defalt
))))
1858 is_rear_sticky
= false;
1861 Lisp_Object rear_non_sticky
1862 = Fget_text_property (prev_pos
, Qrear_nonsticky
, buffer
);
1864 if (!NILP (CONSP (rear_non_sticky
)
1865 ? Fmemq (prop
, rear_non_sticky
)
1867 /* PROP is rear-non-sticky. */
1868 is_rear_sticky
= false;
1871 /* Consider following character. */
1872 /* This signals an arg-out-of-range error if pos is outside the
1873 buffer's accessible range. */
1874 front_sticky
= Fget_text_property (pos
, Qfront_sticky
, buffer
);
1876 if (EQ (front_sticky
, Qt
)
1877 || (CONSP (front_sticky
)
1878 && !NILP (Fmemq (prop
, front_sticky
))))
1879 /* PROP is inherited from after. */
1880 is_front_sticky
= true;
1882 /* Simple cases, where the properties are consistent. */
1883 if (is_rear_sticky
&& !is_front_sticky
)
1885 else if (!is_rear_sticky
&& is_front_sticky
)
1887 else if (!is_rear_sticky
&& !is_front_sticky
)
1890 /* The stickiness properties are inconsistent, so we have to
1891 disambiguate. Basically, rear-sticky wins, _except_ if the
1892 property that would be inherited has a value of nil, in which case
1893 front-sticky wins. */
1894 if (ignore_previous_character
1895 || NILP (Fget_text_property (prev_pos
, prop
, buffer
)))
1902 /* Copying properties between objects. */
1904 /* Add properties from START to END of SRC, starting at POS in DEST.
1905 SRC and DEST may each refer to strings or buffers.
1906 Optional sixth argument PROP causes only that property to be copied.
1907 Properties are copied to DEST as if by `add-text-properties'.
1908 Return t if any property value actually changed, nil otherwise. */
1910 /* Note this can GC when DEST is a buffer. */
1913 copy_text_properties (Lisp_Object start
, Lisp_Object end
, Lisp_Object src
,
1914 Lisp_Object pos
, Lisp_Object dest
, Lisp_Object prop
)
1920 ptrdiff_t s
, e
, e2
, p
, len
;
1922 struct gcpro gcpro1
, gcpro2
;
1924 i
= validate_interval_range (src
, &start
, &end
, soft
);
1928 CHECK_NUMBER_COERCE_MARKER (pos
);
1930 Lisp_Object dest_start
, dest_end
;
1932 e
= XINT (pos
) + (XINT (end
) - XINT (start
));
1933 if (MOST_POSITIVE_FIXNUM
< e
)
1934 args_out_of_range (pos
, end
);
1936 XSETFASTINT (dest_end
, e
);
1937 /* Apply this to a copy of pos; it will try to increment its arguments,
1938 which we don't want. */
1939 validate_interval_range (dest
, &dest_start
, &dest_end
, soft
);
1950 e2
= i
->position
+ LENGTH (i
);
1957 while (! NILP (plist
))
1959 if (EQ (Fcar (plist
), prop
))
1961 plist
= list2 (prop
, Fcar (Fcdr (plist
)));
1964 plist
= Fcdr (Fcdr (plist
));
1967 /* Must defer modifications to the interval tree in case
1968 src and dest refer to the same string or buffer. */
1969 stuff
= Fcons (list3 (make_number (p
), make_number (p
+ len
), plist
),
1972 i
= next_interval (i
);
1980 GCPRO2 (stuff
, dest
);
1982 while (! NILP (stuff
))
1985 res
= Fadd_text_properties (Fcar (res
), Fcar (Fcdr (res
)),
1986 Fcar (Fcdr (Fcdr (res
))), dest
);
1989 stuff
= Fcdr (stuff
);
1994 return modified
? Qt
: Qnil
;
1998 /* Return a list representing the text properties of OBJECT between
1999 START and END. if PROP is non-nil, report only on that property.
2000 Each result list element has the form (S E PLIST), where S and E
2001 are positions in OBJECT and PLIST is a property list containing the
2002 text properties of OBJECT between S and E. Value is nil if OBJECT
2003 doesn't contain text properties between START and END. */
2006 text_property_list (Lisp_Object object
, Lisp_Object start
, Lisp_Object end
, Lisp_Object prop
)
2013 i
= validate_interval_range (object
, &start
, &end
, soft
);
2016 ptrdiff_t s
= XINT (start
);
2017 ptrdiff_t e
= XINT (end
);
2021 ptrdiff_t interval_end
, len
;
2024 interval_end
= i
->position
+ LENGTH (i
);
2025 if (interval_end
> e
)
2027 len
= interval_end
- s
;
2032 for (; CONSP (plist
); plist
= Fcdr (XCDR (plist
)))
2033 if (EQ (XCAR (plist
), prop
))
2035 plist
= list2 (prop
, Fcar (XCDR (plist
)));
2040 result
= Fcons (list3 (make_number (s
), make_number (s
+ len
),
2044 i
= next_interval (i
);
2055 /* Add text properties to OBJECT from LIST. LIST is a list of triples
2056 (START END PLIST), where START and END are positions and PLIST is a
2057 property list containing the text properties to add. Adjust START
2058 and END positions by DELTA before adding properties. */
2061 add_text_properties_from_list (Lisp_Object object
, Lisp_Object list
, Lisp_Object delta
)
2063 struct gcpro gcpro1
, gcpro2
;
2065 GCPRO2 (list
, object
);
2067 for (; CONSP (list
); list
= XCDR (list
))
2069 Lisp_Object item
, start
, end
, plist
;
2072 start
= make_number (XINT (XCAR (item
)) + XINT (delta
));
2073 end
= make_number (XINT (XCAR (XCDR (item
))) + XINT (delta
));
2074 plist
= XCAR (XCDR (XCDR (item
)));
2076 Fadd_text_properties (start
, end
, plist
, object
);
2084 /* Modify end-points of ranges in LIST destructively, and return the
2085 new list. LIST is a list as returned from text_property_list.
2086 Discard properties that begin at or after NEW_END, and limit
2087 end-points to NEW_END. */
2090 extend_property_ranges (Lisp_Object list
, Lisp_Object new_end
)
2092 Lisp_Object prev
= Qnil
, head
= list
;
2093 ptrdiff_t max
= XINT (new_end
);
2095 for (; CONSP (list
); prev
= list
, list
= XCDR (list
))
2097 Lisp_Object item
, beg
, end
;
2101 end
= XCAR (XCDR (item
));
2103 if (XINT (beg
) >= max
)
2105 /* The start-point is past the end of the new string.
2106 Discard this property. */
2107 if (EQ (head
, list
))
2110 XSETCDR (prev
, XCDR (list
));
2112 else if (XINT (end
) > max
)
2113 /* The end-point is past the end of the new string. */
2114 XSETCAR (XCDR (item
), new_end
);
2122 /* Call the modification hook functions in LIST, each with START and END. */
2125 call_mod_hooks (Lisp_Object list
, Lisp_Object start
, Lisp_Object end
)
2127 struct gcpro gcpro1
;
2129 while (!NILP (list
))
2131 call2 (Fcar (list
), start
, end
);
2137 /* Check for read-only intervals between character positions START ... END,
2138 in BUF, and signal an error if we find one.
2140 Then check for any modification hooks in the range.
2141 Create a list of all these hooks in lexicographic order,
2142 eliminating consecutive extra copies of the same hook. Then call
2143 those hooks in order, with START and END - 1 as arguments. */
2146 verify_interval_modification (struct buffer
*buf
,
2147 ptrdiff_t start
, ptrdiff_t end
)
2149 INTERVAL intervals
= buffer_intervals (buf
);
2152 Lisp_Object prev_mod_hooks
;
2153 Lisp_Object mod_hooks
;
2154 struct gcpro gcpro1
;
2157 prev_mod_hooks
= Qnil
;
2160 interval_insert_behind_hooks
= Qnil
;
2161 interval_insert_in_front_hooks
= Qnil
;
2168 ptrdiff_t temp
= start
;
2173 /* For an insert operation, check the two chars around the position. */
2176 INTERVAL prev
= NULL
;
2177 Lisp_Object before
, after
;
2179 /* Set I to the interval containing the char after START,
2180 and PREV to the interval containing the char before START.
2181 Either one may be null. They may be equal. */
2182 i
= find_interval (intervals
, start
);
2184 if (start
== BUF_BEGV (buf
))
2186 else if (i
->position
== start
)
2187 prev
= previous_interval (i
);
2188 else if (i
->position
< start
)
2190 if (start
== BUF_ZV (buf
))
2193 /* If Vinhibit_read_only is set and is not a list, we can
2194 skip the read_only checks. */
2195 if (NILP (Vinhibit_read_only
) || CONSP (Vinhibit_read_only
))
2197 /* If I and PREV differ we need to check for the read-only
2198 property together with its stickiness. If either I or
2199 PREV are 0, this check is all we need.
2200 We have to take special care, since read-only may be
2201 indirectly defined via the category property. */
2206 after
= textget (i
->plist
, Qread_only
);
2208 /* If interval I is read-only and read-only is
2209 front-sticky, inhibit insertion.
2210 Check for read-only as well as category. */
2212 && NILP (Fmemq (after
, Vinhibit_read_only
)))
2216 tem
= textget (i
->plist
, Qfront_sticky
);
2217 if (TMEM (Qread_only
, tem
)
2218 || (NILP (Fplist_get (i
->plist
, Qread_only
))
2219 && TMEM (Qcategory
, tem
)))
2220 text_read_only (after
);
2226 before
= textget (prev
->plist
, Qread_only
);
2228 /* If interval PREV is read-only and read-only isn't
2229 rear-nonsticky, inhibit insertion.
2230 Check for read-only as well as category. */
2232 && NILP (Fmemq (before
, Vinhibit_read_only
)))
2236 tem
= textget (prev
->plist
, Qrear_nonsticky
);
2237 if (! TMEM (Qread_only
, tem
)
2238 && (! NILP (Fplist_get (prev
->plist
,Qread_only
))
2239 || ! TMEM (Qcategory
, tem
)))
2240 text_read_only (before
);
2246 after
= textget (i
->plist
, Qread_only
);
2248 /* If interval I is read-only and read-only is
2249 front-sticky, inhibit insertion.
2250 Check for read-only as well as category. */
2251 if (! NILP (after
) && NILP (Fmemq (after
, Vinhibit_read_only
)))
2255 tem
= textget (i
->plist
, Qfront_sticky
);
2256 if (TMEM (Qread_only
, tem
)
2257 || (NILP (Fplist_get (i
->plist
, Qread_only
))
2258 && TMEM (Qcategory
, tem
)))
2259 text_read_only (after
);
2261 tem
= textget (prev
->plist
, Qrear_nonsticky
);
2262 if (! TMEM (Qread_only
, tem
)
2263 && (! NILP (Fplist_get (prev
->plist
, Qread_only
))
2264 || ! TMEM (Qcategory
, tem
)))
2265 text_read_only (after
);
2270 /* Run both insert hooks (just once if they're the same). */
2272 interval_insert_behind_hooks
2273 = textget (prev
->plist
, Qinsert_behind_hooks
);
2275 interval_insert_in_front_hooks
2276 = textget (i
->plist
, Qinsert_in_front_hooks
);
2280 /* Loop over intervals on or next to START...END,
2281 collecting their hooks. */
2283 i
= find_interval (intervals
, start
);
2286 if (! INTERVAL_WRITABLE_P (i
))
2287 text_read_only (textget (i
->plist
, Qread_only
));
2289 if (!inhibit_modification_hooks
)
2291 mod_hooks
= textget (i
->plist
, Qmodification_hooks
);
2292 if (! NILP (mod_hooks
) && ! EQ (mod_hooks
, prev_mod_hooks
))
2294 hooks
= Fcons (mod_hooks
, hooks
);
2295 prev_mod_hooks
= mod_hooks
;
2299 i
= next_interval (i
);
2301 /* Keep going thru the interval containing the char before END. */
2302 while (i
&& i
->position
< end
);
2304 if (!inhibit_modification_hooks
)
2307 hooks
= Fnreverse (hooks
);
2308 while (! EQ (hooks
, Qnil
))
2310 call_mod_hooks (Fcar (hooks
), make_number (start
),
2312 hooks
= Fcdr (hooks
);
2319 /* Run the interval hooks for an insertion on character range START ... END.
2320 verify_interval_modification chose which hooks to run;
2321 this function is called after the insertion happens
2322 so it can indicate the range of inserted text. */
2325 report_interval_modification (Lisp_Object start
, Lisp_Object end
)
2327 if (! NILP (interval_insert_behind_hooks
))
2328 call_mod_hooks (interval_insert_behind_hooks
, start
, end
);
2329 if (! NILP (interval_insert_in_front_hooks
)
2330 && ! EQ (interval_insert_in_front_hooks
,
2331 interval_insert_behind_hooks
))
2332 call_mod_hooks (interval_insert_in_front_hooks
, start
, end
);
2336 syms_of_textprop (void)
2338 DEFVAR_LISP ("default-text-properties", Vdefault_text_properties
,
2339 doc
: /* Property-list used as default values.
2340 The value of a property in this list is seen as the value for every
2341 character that does not have its own value for that property. */);
2342 Vdefault_text_properties
= Qnil
;
2344 DEFVAR_LISP ("char-property-alias-alist", Vchar_property_alias_alist
,
2345 doc
: /* Alist of alternative properties for properties without a value.
2346 Each element should look like (PROPERTY ALTERNATIVE1 ALTERNATIVE2...).
2347 If a piece of text has no direct value for a particular property, then
2348 this alist is consulted. If that property appears in the alist, then
2349 the first non-nil value from the associated alternative properties is
2351 Vchar_property_alias_alist
= Qnil
;
2353 DEFVAR_LISP ("inhibit-point-motion-hooks", Vinhibit_point_motion_hooks
,
2354 doc
: /* If non-nil, don't run `point-left' and `point-entered' text properties.
2355 This also inhibits the use of the `intangible' text property. */);
2356 Vinhibit_point_motion_hooks
= Qnil
;
2358 DEFVAR_LISP ("text-property-default-nonsticky",
2359 Vtext_property_default_nonsticky
,
2360 doc
: /* Alist of properties vs the corresponding non-stickiness.
2361 Each element has the form (PROPERTY . NONSTICKINESS).
2363 If a character in a buffer has PROPERTY, new text inserted adjacent to
2364 the character doesn't inherit PROPERTY if NONSTICKINESS is non-nil,
2365 inherits it if NONSTICKINESS is nil. The `front-sticky' and
2366 `rear-nonsticky' properties of the character override NONSTICKINESS. */);
2367 /* Text properties `syntax-table'and `display' should be nonsticky
2369 Vtext_property_default_nonsticky
2370 = list2 (Fcons (intern_c_string ("syntax-table"), Qt
),
2371 Fcons (intern_c_string ("display"), Qt
));
2373 staticpro (&interval_insert_behind_hooks
);
2374 staticpro (&interval_insert_in_front_hooks
);
2375 interval_insert_behind_hooks
= Qnil
;
2376 interval_insert_in_front_hooks
= Qnil
;
2379 /* Common attributes one might give text */
2381 DEFSYM (Qforeground
, "foreground");
2382 DEFSYM (Qbackground
, "background");
2383 DEFSYM (Qfont
, "font");
2384 DEFSYM (Qface
, "face");
2385 DEFSYM (Qstipple
, "stipple");
2386 DEFSYM (Qunderline
, "underline");
2387 DEFSYM (Qread_only
, "read-only");
2388 DEFSYM (Qinvisible
, "invisible");
2389 DEFSYM (Qintangible
, "intangible");
2390 DEFSYM (Qcategory
, "category");
2391 DEFSYM (Qlocal_map
, "local-map");
2392 DEFSYM (Qfront_sticky
, "front-sticky");
2393 DEFSYM (Qrear_nonsticky
, "rear-nonsticky");
2394 DEFSYM (Qmouse_face
, "mouse-face");
2395 DEFSYM (Qminibuffer_prompt
, "minibuffer-prompt");
2397 /* Properties that text might use to specify certain actions */
2399 DEFSYM (Qmouse_left
, "mouse-left");
2400 DEFSYM (Qmouse_entered
, "mouse-entered");
2401 DEFSYM (Qpoint_left
, "point-left");
2402 DEFSYM (Qpoint_entered
, "point-entered");
2404 defsubr (&Stext_properties_at
);
2405 defsubr (&Sget_text_property
);
2406 defsubr (&Sget_char_property
);
2407 defsubr (&Sget_char_property_and_overlay
);
2408 defsubr (&Snext_char_property_change
);
2409 defsubr (&Sprevious_char_property_change
);
2410 defsubr (&Snext_single_char_property_change
);
2411 defsubr (&Sprevious_single_char_property_change
);
2412 defsubr (&Snext_property_change
);
2413 defsubr (&Snext_single_property_change
);
2414 defsubr (&Sprevious_property_change
);
2415 defsubr (&Sprevious_single_property_change
);
2416 defsubr (&Sadd_text_properties
);
2417 defsubr (&Sput_text_property
);
2418 defsubr (&Sset_text_properties
);
2419 defsubr (&Sadd_face_text_property
);
2420 defsubr (&Sremove_text_properties
);
2421 defsubr (&Sremove_list_of_text_properties
);
2422 defsubr (&Stext_property_any
);
2423 defsubr (&Stext_property_not_all
);