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