Avoid (most) uses of XCAR/XCDR as lvalues, for flexibility in experimenting
[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, 0);
127 CHECK_NUMBER_COERCE_MARKER (*begin, 0);
128 CHECK_NUMBER_COERCE_MARKER (*end, 0);
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, 0);
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 "Return the list of properties of the character at POSITION in OBJECT.\n\
538 OBJECT is the string or buffer to look for the properties in;\n\
539 nil means the current buffer.\n\
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 "Return the value of POSITION's property PROP, in OBJECT.\n\
564 OBJECT is optional and defaults to the current buffer.\n\
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, 0);
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 "Return the value of POSITION's property PROP, in OBJECT.\n\
658 OBJECT is optional and defaults to the current buffer.\n\
659 If POSITION is at the end of OBJECT, the value is nil.\n\
660 If OBJECT is a buffer, then overlay properties are considered as well as\n\
661 text properties.\n\
662 If OBJECT is a window, then that window's buffer is used, but window-specific\n\
663 overlays are considered only if they are associated with OBJECT.")
664 (position, prop, object)
665 Lisp_Object position, object;
666 register Lisp_Object prop;
667 {
668 return get_char_property_and_overlay (position, prop, object, 0);
669 }
670 \f
671 DEFUN ("next-char-property-change", Fnext_char_property_change,
672 Snext_char_property_change, 1, 2, 0,
673 "Return the position of next text property or overlay change.\n\
674 This scans characters forward from POSITION till it finds a change in\n\
675 some text property, or the beginning or end of an overlay, and returns\n\
676 the position of that.\n\
677 If none is found, the function returns (point-max).\n\
678 \n\
679 If the optional third argument LIMIT is non-nil, don't search\n\
680 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
681 (position, limit)
682 Lisp_Object position, limit;
683 {
684 Lisp_Object temp;
685
686 temp = Fnext_overlay_change (position);
687 if (! NILP (limit))
688 {
689 CHECK_NUMBER (limit, 2);
690 if (XINT (limit) < XINT (temp))
691 temp = limit;
692 }
693 return Fnext_property_change (position, Qnil, temp);
694 }
695
696 DEFUN ("previous-char-property-change", Fprevious_char_property_change,
697 Sprevious_char_property_change, 1, 2, 0,
698 "Return the position of previous text property or overlay change.\n\
699 Scans characters backward from POSITION till it finds a change in some\n\
700 text property, or the beginning or end of an overlay, and returns the\n\
701 position of that.\n\
702 If none is found, the function returns (point-max).\n\
703 \n\
704 If the optional third argument LIMIT is non-nil, don't search\n\
705 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
706 (position, limit)
707 Lisp_Object position, limit;
708 {
709 Lisp_Object temp;
710
711 temp = Fprevious_overlay_change (position);
712 if (! NILP (limit))
713 {
714 CHECK_NUMBER (limit, 2);
715 if (XINT (limit) > XINT (temp))
716 temp = limit;
717 }
718 return Fprevious_property_change (position, Qnil, temp);
719 }
720
721
722 DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
723 Snext_single_char_property_change, 2, 4, 0,
724 "Return the position of next text property or overlay change for a specific property.\n\
725 Scans characters forward from POSITION till it finds\n\
726 a change in the PROP property, then returns the position of the change.\n\
727 The optional third argument OBJECT is the string or buffer to scan.\n\
728 The property values are compared with `eq'.\n\
729 If the property is constant all the way to the end of OBJECT, return the\n\
730 last valid position in OBJECT.\n\
731 If the optional fourth argument LIMIT is non-nil, don't search\n\
732 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
733 (position, prop, object, limit)
734 Lisp_Object prop, position, object, limit;
735 {
736 if (STRINGP (object))
737 {
738 position = Fnext_single_property_change (position, prop, object, limit);
739 if (NILP (position))
740 {
741 if (NILP (limit))
742 position = make_number (XSTRING (object)->size);
743 else
744 position = limit;
745 }
746 }
747 else
748 {
749 Lisp_Object initial_value, value;
750 int count = specpdl_ptr - specpdl;
751
752 if (! NILP (object))
753 CHECK_BUFFER (object, 0);
754
755 if (BUFFERP (object) && current_buffer != XBUFFER (object))
756 {
757 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
758 Fset_buffer (object);
759 }
760
761 initial_value = Fget_char_property (position, prop, object);
762
763 if (NILP (limit))
764 XSETFASTINT (limit, BUF_ZV (current_buffer));
765 else
766 CHECK_NUMBER_COERCE_MARKER (limit, 0);
767
768 for (;;)
769 {
770 position = Fnext_char_property_change (position, limit);
771 if (XFASTINT (position) >= XFASTINT (limit)) {
772 position = limit;
773 break;
774 }
775
776 value = Fget_char_property (position, prop, object);
777 if (!EQ (value, initial_value))
778 break;
779 }
780
781 unbind_to (count, Qnil);
782 }
783
784 return position;
785 }
786
787 DEFUN ("previous-single-char-property-change",
788 Fprevious_single_char_property_change,
789 Sprevious_single_char_property_change, 2, 4, 0,
790 "Return the position of previous text property or overlay change for a specific property.\n\
791 Scans characters backward from POSITION till it finds\n\
792 a change in the PROP property, then returns the position of the change.\n\
793 The optional third argument OBJECT is the string or buffer to scan.\n\
794 The property values are compared with `eq'.\n\
795 If the property is constant all the way to the start of OBJECT, return the\n\
796 first valid position in OBJECT.\n\
797 If the optional fourth argument LIMIT is non-nil, don't search\n\
798 back past position LIMIT; return LIMIT if nothing is found before LIMIT.")
799 (position, prop, object, limit)
800 Lisp_Object prop, position, object, limit;
801 {
802 if (STRINGP (object))
803 {
804 position = Fprevious_single_property_change (position, prop, object, limit);
805 if (NILP (position))
806 {
807 if (NILP (limit))
808 position = make_number (XSTRING (object)->size);
809 else
810 position = limit;
811 }
812 }
813 else
814 {
815 int count = specpdl_ptr - specpdl;
816
817 if (! NILP (object))
818 CHECK_BUFFER (object, 0);
819
820 if (BUFFERP (object) && current_buffer != XBUFFER (object))
821 {
822 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
823 Fset_buffer (object);
824 }
825
826 if (NILP (limit))
827 XSETFASTINT (limit, BUF_BEGV (current_buffer));
828 else
829 CHECK_NUMBER_COERCE_MARKER (limit, 0);
830
831 if (XFASTINT (position) <= XFASTINT (limit))
832 position = limit;
833 else
834 {
835 Lisp_Object initial_value =
836 Fget_char_property (make_number (XFASTINT (position) - 1),
837 prop, object);
838
839 for (;;)
840 {
841 position = Fprevious_char_property_change (position, limit);
842
843 if (XFASTINT (position) <= XFASTINT (limit))
844 {
845 position = limit;
846 break;
847 }
848 else
849 {
850 Lisp_Object value =
851 Fget_char_property (make_number (XFASTINT (position) - 1),
852 prop, object);
853
854 if (!EQ (value, initial_value))
855 break;
856 }
857 }
858 }
859
860 unbind_to (count, Qnil);
861 }
862
863 return position;
864 }
865 \f
866 DEFUN ("next-property-change", Fnext_property_change,
867 Snext_property_change, 1, 3, 0,
868 "Return the position of next property change.\n\
869 Scans characters forward from POSITION in OBJECT till it finds\n\
870 a change in some text property, then returns the position of the change.\n\
871 The optional second argument OBJECT is the string or buffer to scan.\n\
872 Return nil if the property is constant all the way to the end of OBJECT.\n\
873 If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\
874 If the optional third argument LIMIT is non-nil, don't search\n\
875 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
876 (position, object, limit)
877 Lisp_Object position, object, limit;
878 {
879 register INTERVAL i, next;
880
881 if (NILP (object))
882 XSETBUFFER (object, current_buffer);
883
884 if (! NILP (limit) && ! EQ (limit, Qt))
885 CHECK_NUMBER_COERCE_MARKER (limit, 0);
886
887 i = validate_interval_range (object, &position, &position, soft);
888
889 /* If LIMIT is t, return start of next interval--don't
890 bother checking further intervals. */
891 if (EQ (limit, Qt))
892 {
893 if (NULL_INTERVAL_P (i))
894 next = i;
895 else
896 next = next_interval (i);
897
898 if (NULL_INTERVAL_P (next))
899 XSETFASTINT (position, (STRINGP (object)
900 ? XSTRING (object)->size
901 : BUF_ZV (XBUFFER (object))));
902 else
903 XSETFASTINT (position, next->position);
904 return position;
905 }
906
907 if (NULL_INTERVAL_P (i))
908 return limit;
909
910 next = next_interval (i);
911
912 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next)
913 && (NILP (limit) || next->position < XFASTINT (limit)))
914 next = next_interval (next);
915
916 if (NULL_INTERVAL_P (next))
917 return limit;
918 if (! NILP (limit) && !(next->position < XFASTINT (limit)))
919 return limit;
920
921 XSETFASTINT (position, next->position);
922 return position;
923 }
924
925 /* Return 1 if there's a change in some property between BEG and END. */
926
927 int
928 property_change_between_p (beg, end)
929 int beg, end;
930 {
931 register INTERVAL i, next;
932 Lisp_Object object, pos;
933
934 XSETBUFFER (object, current_buffer);
935 XSETFASTINT (pos, beg);
936
937 i = validate_interval_range (object, &pos, &pos, soft);
938 if (NULL_INTERVAL_P (i))
939 return 0;
940
941 next = next_interval (i);
942 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
943 {
944 next = next_interval (next);
945 if (NULL_INTERVAL_P (next))
946 return 0;
947 if (next->position >= end)
948 return 0;
949 }
950
951 if (NULL_INTERVAL_P (next))
952 return 0;
953
954 return 1;
955 }
956
957 DEFUN ("next-single-property-change", Fnext_single_property_change,
958 Snext_single_property_change, 2, 4, 0,
959 "Return the position of next property change for a specific property.\n\
960 Scans characters forward from POSITION till it finds\n\
961 a change in the PROP property, then returns the position of the change.\n\
962 The optional third argument OBJECT is the string or buffer to scan.\n\
963 The property values are compared with `eq'.\n\
964 Return nil if the property is constant all the way to the end of OBJECT.\n\
965 If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\
966 If the optional fourth argument LIMIT is non-nil, don't search\n\
967 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
968 (position, prop, object, limit)
969 Lisp_Object position, prop, object, limit;
970 {
971 register INTERVAL i, next;
972 register Lisp_Object here_val;
973
974 if (NILP (object))
975 XSETBUFFER (object, current_buffer);
976
977 if (!NILP (limit))
978 CHECK_NUMBER_COERCE_MARKER (limit, 0);
979
980 i = validate_interval_range (object, &position, &position, soft);
981 if (NULL_INTERVAL_P (i))
982 return limit;
983
984 here_val = textget (i->plist, prop);
985 next = next_interval (i);
986 while (! NULL_INTERVAL_P (next)
987 && EQ (here_val, textget (next->plist, prop))
988 && (NILP (limit) || next->position < XFASTINT (limit)))
989 next = next_interval (next);
990
991 if (NULL_INTERVAL_P (next))
992 return limit;
993 if (! NILP (limit) && !(next->position < XFASTINT (limit)))
994 return limit;
995
996 return make_number (next->position);
997 }
998
999 DEFUN ("previous-property-change", Fprevious_property_change,
1000 Sprevious_property_change, 1, 3, 0,
1001 "Return the position of previous property change.\n\
1002 Scans characters backwards from POSITION in OBJECT till it finds\n\
1003 a change in some text property, then returns the position of the change.\n\
1004 The optional second argument OBJECT is the string or buffer to scan.\n\
1005 Return nil if the property is constant all the way to the start of OBJECT.\n\
1006 If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
1007 If the optional third argument LIMIT is non-nil, don't search\n\
1008 back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
1009 (position, object, limit)
1010 Lisp_Object position, object, limit;
1011 {
1012 register INTERVAL i, previous;
1013
1014 if (NILP (object))
1015 XSETBUFFER (object, current_buffer);
1016
1017 if (!NILP (limit))
1018 CHECK_NUMBER_COERCE_MARKER (limit, 0);
1019
1020 i = validate_interval_range (object, &position, &position, soft);
1021 if (NULL_INTERVAL_P (i))
1022 return limit;
1023
1024 /* Start with the interval containing the char before point. */
1025 if (i->position == XFASTINT (position))
1026 i = previous_interval (i);
1027
1028 previous = previous_interval (i);
1029 while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i)
1030 && (NILP (limit)
1031 || (previous->position + LENGTH (previous) > XFASTINT (limit))))
1032 previous = previous_interval (previous);
1033 if (NULL_INTERVAL_P (previous))
1034 return limit;
1035 if (!NILP (limit)
1036 && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
1037 return limit;
1038
1039 return make_number (previous->position + LENGTH (previous));
1040 }
1041
1042 DEFUN ("previous-single-property-change", Fprevious_single_property_change,
1043 Sprevious_single_property_change, 2, 4, 0,
1044 "Return the position of previous property change for a specific property.\n\
1045 Scans characters backward from POSITION till it finds\n\
1046 a change in the PROP property, then returns the position of the change.\n\
1047 The optional third argument OBJECT is the string or buffer to scan.\n\
1048 The property values are compared with `eq'.\n\
1049 Return nil if the property is constant all the way to the start of OBJECT.\n\
1050 If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
1051 If the optional fourth argument LIMIT is non-nil, don't search\n\
1052 back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
1053 (position, prop, object, limit)
1054 Lisp_Object position, prop, object, limit;
1055 {
1056 register INTERVAL i, previous;
1057 register Lisp_Object here_val;
1058
1059 if (NILP (object))
1060 XSETBUFFER (object, current_buffer);
1061
1062 if (!NILP (limit))
1063 CHECK_NUMBER_COERCE_MARKER (limit, 0);
1064
1065 i = validate_interval_range (object, &position, &position, soft);
1066
1067 /* Start with the interval containing the char before point. */
1068 if (! NULL_INTERVAL_P (i) && i->position == XFASTINT (position))
1069 i = previous_interval (i);
1070
1071 if (NULL_INTERVAL_P (i))
1072 return limit;
1073
1074 here_val = textget (i->plist, prop);
1075 previous = previous_interval (i);
1076 while (! NULL_INTERVAL_P (previous)
1077 && EQ (here_val, textget (previous->plist, prop))
1078 && (NILP (limit)
1079 || (previous->position + LENGTH (previous) > XFASTINT (limit))))
1080 previous = previous_interval (previous);
1081 if (NULL_INTERVAL_P (previous))
1082 return limit;
1083 if (!NILP (limit)
1084 && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
1085 return limit;
1086
1087 return make_number (previous->position + LENGTH (previous));
1088 }
1089 \f
1090 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
1091
1092 DEFUN ("add-text-properties", Fadd_text_properties,
1093 Sadd_text_properties, 3, 4, 0,
1094 "Add properties to the text from START to END.\n\
1095 The third argument PROPERTIES is a property list\n\
1096 specifying the property values to add.\n\
1097 The optional fourth argument, OBJECT,\n\
1098 is the string or buffer containing the text.\n\
1099 Return t if any property value actually changed, nil otherwise.")
1100 (start, end, properties, object)
1101 Lisp_Object start, end, properties, object;
1102 {
1103 register INTERVAL i, unchanged;
1104 register int s, len, modified = 0;
1105 struct gcpro gcpro1;
1106
1107 properties = validate_plist (properties);
1108 if (NILP (properties))
1109 return Qnil;
1110
1111 if (NILP (object))
1112 XSETBUFFER (object, current_buffer);
1113
1114 i = validate_interval_range (object, &start, &end, hard);
1115 if (NULL_INTERVAL_P (i))
1116 return Qnil;
1117
1118 s = XINT (start);
1119 len = XINT (end) - s;
1120
1121 /* No need to protect OBJECT, because we GC only if it's a buffer,
1122 and live buffers are always protected. */
1123 GCPRO1 (properties);
1124
1125 /* If we're not starting on an interval boundary, we have to
1126 split this interval. */
1127 if (i->position != s)
1128 {
1129 /* If this interval already has the properties, we can
1130 skip it. */
1131 if (interval_has_all_properties (properties, i))
1132 {
1133 int got = (LENGTH (i) - (s - i->position));
1134 if (got >= len)
1135 RETURN_UNGCPRO (Qnil);
1136 len -= got;
1137 i = next_interval (i);
1138 }
1139 else
1140 {
1141 unchanged = i;
1142 i = split_interval_right (unchanged, s - unchanged->position);
1143 copy_properties (unchanged, i);
1144 }
1145 }
1146
1147 if (BUFFERP (object))
1148 modify_region (XBUFFER (object), XINT (start), XINT (end));
1149
1150 /* We are at the beginning of interval I, with LEN chars to scan. */
1151 for (;;)
1152 {
1153 if (i == 0)
1154 abort ();
1155
1156 if (LENGTH (i) >= len)
1157 {
1158 /* We can UNGCPRO safely here, because there will be just
1159 one more chance to gc, in the next call to add_properties,
1160 and after that we will not need PROPERTIES or OBJECT again. */
1161 UNGCPRO;
1162
1163 if (interval_has_all_properties (properties, i))
1164 {
1165 if (BUFFERP (object))
1166 signal_after_change (XINT (start), XINT (end) - XINT (start),
1167 XINT (end) - XINT (start));
1168
1169 return modified ? Qt : Qnil;
1170 }
1171
1172 if (LENGTH (i) == len)
1173 {
1174 add_properties (properties, i, object);
1175 if (BUFFERP (object))
1176 signal_after_change (XINT (start), XINT (end) - XINT (start),
1177 XINT (end) - XINT (start));
1178 return Qt;
1179 }
1180
1181 /* i doesn't have the properties, and goes past the change limit */
1182 unchanged = i;
1183 i = split_interval_left (unchanged, len);
1184 copy_properties (unchanged, i);
1185 add_properties (properties, i, object);
1186 if (BUFFERP (object))
1187 signal_after_change (XINT (start), XINT (end) - XINT (start),
1188 XINT (end) - XINT (start));
1189 return Qt;
1190 }
1191
1192 len -= LENGTH (i);
1193 modified += add_properties (properties, i, object);
1194 i = next_interval (i);
1195 }
1196 }
1197
1198 /* Callers note, this can GC when OBJECT is a buffer (or nil). */
1199
1200 DEFUN ("put-text-property", Fput_text_property,
1201 Sput_text_property, 4, 5, 0,
1202 "Set one property of the text from START to END.\n\
1203 The third and fourth arguments PROPERTY and VALUE\n\
1204 specify the property to add.\n\
1205 The optional fifth argument, OBJECT,\n\
1206 is the string or buffer containing the text.")
1207 (start, end, property, value, object)
1208 Lisp_Object start, end, property, value, object;
1209 {
1210 Fadd_text_properties (start, end,
1211 Fcons (property, Fcons (value, Qnil)),
1212 object);
1213 return Qnil;
1214 }
1215
1216 DEFUN ("set-text-properties", Fset_text_properties,
1217 Sset_text_properties, 3, 4, 0,
1218 "Completely replace properties of text from START to END.\n\
1219 The third argument PROPERTIES is the new property list.\n\
1220 The optional fourth argument, OBJECT,\n\
1221 is the string or buffer containing the text.\n\
1222 If OBJECT is omitted or nil, it defaults to the current buffer.\n\
1223 If PROPERTIES is nil, the effect is to remove all properties from\n\
1224 the designated part of OBJECT.")
1225 (start, end, properties, object)
1226 Lisp_Object start, end, properties, object;
1227 {
1228 return set_text_properties (start, end, properties, object, Qt);
1229 }
1230
1231
1232 /* Replace properties of text from START to END with new list of
1233 properties PROPERTIES. OBJECT is the buffer or string containing
1234 the text. OBJECT nil means use the current buffer.
1235 SIGNAL_AFTER_CHANGE_P nil means don't signal after changes. Value
1236 is non-nil if properties were replaced; it is nil if there weren't
1237 any properties to replace. */
1238
1239 Lisp_Object
1240 set_text_properties (start, end, properties, object, signal_after_change_p)
1241 Lisp_Object start, end, properties, object, signal_after_change_p;
1242 {
1243 register INTERVAL i, unchanged;
1244 register INTERVAL prev_changed = NULL_INTERVAL;
1245 register int s, len;
1246 Lisp_Object ostart, oend;
1247
1248 ostart = start;
1249 oend = end;
1250
1251 properties = validate_plist (properties);
1252
1253 if (NILP (object))
1254 XSETBUFFER (object, current_buffer);
1255
1256 /* If we want no properties for a whole string,
1257 get rid of its intervals. */
1258 if (NILP (properties) && STRINGP (object)
1259 && XFASTINT (start) == 0
1260 && XFASTINT (end) == XSTRING (object)->size)
1261 {
1262 if (! XSTRING (object)->intervals)
1263 return Qt;
1264
1265 XSTRING (object)->intervals = 0;
1266 return Qt;
1267 }
1268
1269 i = validate_interval_range (object, &start, &end, soft);
1270
1271 if (NULL_INTERVAL_P (i))
1272 {
1273 /* If buffer has no properties, and we want none, return now. */
1274 if (NILP (properties))
1275 return Qnil;
1276
1277 /* Restore the original START and END values
1278 because validate_interval_range increments them for strings. */
1279 start = ostart;
1280 end = oend;
1281
1282 i = validate_interval_range (object, &start, &end, hard);
1283 /* This can return if start == end. */
1284 if (NULL_INTERVAL_P (i))
1285 return Qnil;
1286 }
1287
1288 s = XINT (start);
1289 len = XINT (end) - s;
1290
1291 if (BUFFERP (object))
1292 modify_region (XBUFFER (object), XINT (start), XINT (end));
1293
1294 if (i->position != s)
1295 {
1296 unchanged = i;
1297 i = split_interval_right (unchanged, s - unchanged->position);
1298
1299 if (LENGTH (i) > len)
1300 {
1301 copy_properties (unchanged, i);
1302 i = split_interval_left (i, len);
1303 set_properties (properties, i, object);
1304 if (BUFFERP (object) && !NILP (signal_after_change_p))
1305 signal_after_change (XINT (start), XINT (end) - XINT (start),
1306 XINT (end) - XINT (start));
1307
1308 return Qt;
1309 }
1310
1311 set_properties (properties, i, object);
1312
1313 if (LENGTH (i) == len)
1314 {
1315 if (BUFFERP (object) && !NILP (signal_after_change_p))
1316 signal_after_change (XINT (start), XINT (end) - XINT (start),
1317 XINT (end) - XINT (start));
1318
1319 return Qt;
1320 }
1321
1322 prev_changed = i;
1323 len -= LENGTH (i);
1324 i = next_interval (i);
1325 }
1326
1327 /* We are starting at the beginning of an interval, I */
1328 while (len > 0)
1329 {
1330 if (i == 0)
1331 abort ();
1332
1333 if (LENGTH (i) >= len)
1334 {
1335 if (LENGTH (i) > len)
1336 i = split_interval_left (i, len);
1337
1338 /* We have to call set_properties even if we are going to
1339 merge the intervals, so as to make the undo records
1340 and cause redisplay to happen. */
1341 set_properties (properties, i, object);
1342 if (!NULL_INTERVAL_P (prev_changed))
1343 merge_interval_left (i);
1344 if (BUFFERP (object) && !NILP (signal_after_change_p))
1345 signal_after_change (XINT (start), XINT (end) - XINT (start),
1346 XINT (end) - XINT (start));
1347 return Qt;
1348 }
1349
1350 len -= LENGTH (i);
1351
1352 /* We have to call set_properties even if we are going to
1353 merge the intervals, so as to make the undo records
1354 and cause redisplay to happen. */
1355 set_properties (properties, i, object);
1356 if (NULL_INTERVAL_P (prev_changed))
1357 prev_changed = i;
1358 else
1359 prev_changed = i = merge_interval_left (i);
1360
1361 i = next_interval (i);
1362 }
1363
1364 if (BUFFERP (object) && !NILP (signal_after_change_p))
1365 signal_after_change (XINT (start), XINT (end) - XINT (start),
1366 XINT (end) - XINT (start));
1367 return Qt;
1368 }
1369
1370 DEFUN ("remove-text-properties", Fremove_text_properties,
1371 Sremove_text_properties, 3, 4, 0,
1372 "Remove some properties from text from START to END.\n\
1373 The third argument PROPERTIES is a property list\n\
1374 whose property names specify the properties to remove.\n\
1375 \(The values stored in PROPERTIES are ignored.)\n\
1376 The optional fourth argument, OBJECT,\n\
1377 is the string or buffer containing the text.\n\
1378 Return t if any property was actually removed, nil otherwise.")
1379 (start, end, properties, object)
1380 Lisp_Object start, end, properties, object;
1381 {
1382 register INTERVAL i, unchanged;
1383 register int s, len, modified = 0;
1384
1385 if (NILP (object))
1386 XSETBUFFER (object, current_buffer);
1387
1388 i = validate_interval_range (object, &start, &end, soft);
1389 if (NULL_INTERVAL_P (i))
1390 return Qnil;
1391
1392 s = XINT (start);
1393 len = XINT (end) - s;
1394
1395 if (i->position != s)
1396 {
1397 /* No properties on this first interval -- return if
1398 it covers the entire region. */
1399 if (! interval_has_some_properties (properties, i))
1400 {
1401 int got = (LENGTH (i) - (s - i->position));
1402 if (got >= len)
1403 return Qnil;
1404 len -= got;
1405 i = next_interval (i);
1406 }
1407 /* Split away the beginning of this interval; what we don't
1408 want to modify. */
1409 else
1410 {
1411 unchanged = i;
1412 i = split_interval_right (unchanged, s - unchanged->position);
1413 copy_properties (unchanged, i);
1414 }
1415 }
1416
1417 if (BUFFERP (object))
1418 modify_region (XBUFFER (object), XINT (start), XINT (end));
1419
1420 /* We are at the beginning of an interval, with len to scan */
1421 for (;;)
1422 {
1423 if (i == 0)
1424 abort ();
1425
1426 if (LENGTH (i) >= len)
1427 {
1428 if (! interval_has_some_properties (properties, i))
1429 return modified ? Qt : Qnil;
1430
1431 if (LENGTH (i) == len)
1432 {
1433 remove_properties (properties, i, object);
1434 if (BUFFERP (object))
1435 signal_after_change (XINT (start), XINT (end) - XINT (start),
1436 XINT (end) - XINT (start));
1437 return Qt;
1438 }
1439
1440 /* i has the properties, and goes past the change limit */
1441 unchanged = i;
1442 i = split_interval_left (i, len);
1443 copy_properties (unchanged, i);
1444 remove_properties (properties, i, object);
1445 if (BUFFERP (object))
1446 signal_after_change (XINT (start), XINT (end) - XINT (start),
1447 XINT (end) - XINT (start));
1448 return Qt;
1449 }
1450
1451 len -= LENGTH (i);
1452 modified += remove_properties (properties, i, object);
1453 i = next_interval (i);
1454 }
1455 }
1456 \f
1457 DEFUN ("text-property-any", Ftext_property_any,
1458 Stext_property_any, 4, 5, 0,
1459 "Check text from START to END for property PROPERTY equalling VALUE.\n\
1460 If so, return the position of the first character whose property PROPERTY\n\
1461 is `eq' to VALUE. Otherwise return nil.\n\
1462 The optional fifth argument, OBJECT, is the string or buffer\n\
1463 containing the text.")
1464 (start, end, property, value, object)
1465 Lisp_Object start, end, property, value, object;
1466 {
1467 register INTERVAL i;
1468 register int e, pos;
1469
1470 if (NILP (object))
1471 XSETBUFFER (object, current_buffer);
1472 i = validate_interval_range (object, &start, &end, soft);
1473 if (NULL_INTERVAL_P (i))
1474 return (!NILP (value) || EQ (start, end) ? Qnil : start);
1475 e = XINT (end);
1476
1477 while (! NULL_INTERVAL_P (i))
1478 {
1479 if (i->position >= e)
1480 break;
1481 if (EQ (textget (i->plist, property), value))
1482 {
1483 pos = i->position;
1484 if (pos < XINT (start))
1485 pos = XINT (start);
1486 return make_number (pos);
1487 }
1488 i = next_interval (i);
1489 }
1490 return Qnil;
1491 }
1492
1493 DEFUN ("text-property-not-all", Ftext_property_not_all,
1494 Stext_property_not_all, 4, 5, 0,
1495 "Check text from START to END for property PROPERTY not equalling VALUE.\n\
1496 If so, return the position of the first character whose property PROPERTY\n\
1497 is not `eq' to VALUE. Otherwise, return nil.\n\
1498 The optional fifth argument, OBJECT, is the string or buffer\n\
1499 containing the text.")
1500 (start, end, property, value, object)
1501 Lisp_Object start, end, property, value, object;
1502 {
1503 register INTERVAL i;
1504 register int s, e;
1505
1506 if (NILP (object))
1507 XSETBUFFER (object, current_buffer);
1508 i = validate_interval_range (object, &start, &end, soft);
1509 if (NULL_INTERVAL_P (i))
1510 return (NILP (value) || EQ (start, end)) ? Qnil : start;
1511 s = XINT (start);
1512 e = XINT (end);
1513
1514 while (! NULL_INTERVAL_P (i))
1515 {
1516 if (i->position >= e)
1517 break;
1518 if (! EQ (textget (i->plist, property), value))
1519 {
1520 if (i->position > s)
1521 s = i->position;
1522 return make_number (s);
1523 }
1524 i = next_interval (i);
1525 }
1526 return Qnil;
1527 }
1528 \f
1529 /* I don't think this is the right interface to export; how often do you
1530 want to do something like this, other than when you're copying objects
1531 around?
1532
1533 I think it would be better to have a pair of functions, one which
1534 returns the text properties of a region as a list of ranges and
1535 plists, and another which applies such a list to another object. */
1536
1537 /* Add properties from SRC to SRC of SRC, starting at POS in DEST.
1538 SRC and DEST may each refer to strings or buffers.
1539 Optional sixth argument PROP causes only that property to be copied.
1540 Properties are copied to DEST as if by `add-text-properties'.
1541 Return t if any property value actually changed, nil otherwise. */
1542
1543 /* Note this can GC when DEST is a buffer. */
1544
1545 Lisp_Object
1546 copy_text_properties (start, end, src, pos, dest, prop)
1547 Lisp_Object start, end, src, pos, dest, prop;
1548 {
1549 INTERVAL i;
1550 Lisp_Object res;
1551 Lisp_Object stuff;
1552 Lisp_Object plist;
1553 int s, e, e2, p, len, modified = 0;
1554 struct gcpro gcpro1, gcpro2;
1555
1556 i = validate_interval_range (src, &start, &end, soft);
1557 if (NULL_INTERVAL_P (i))
1558 return Qnil;
1559
1560 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1561 {
1562 Lisp_Object dest_start, dest_end;
1563
1564 dest_start = pos;
1565 XSETFASTINT (dest_end, XINT (dest_start) + (XINT (end) - XINT (start)));
1566 /* Apply this to a copy of pos; it will try to increment its arguments,
1567 which we don't want. */
1568 validate_interval_range (dest, &dest_start, &dest_end, soft);
1569 }
1570
1571 s = XINT (start);
1572 e = XINT (end);
1573 p = XINT (pos);
1574
1575 stuff = Qnil;
1576
1577 while (s < e)
1578 {
1579 e2 = i->position + LENGTH (i);
1580 if (e2 > e)
1581 e2 = e;
1582 len = e2 - s;
1583
1584 plist = i->plist;
1585 if (! NILP (prop))
1586 while (! NILP (plist))
1587 {
1588 if (EQ (Fcar (plist), prop))
1589 {
1590 plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
1591 break;
1592 }
1593 plist = Fcdr (Fcdr (plist));
1594 }
1595 if (! NILP (plist))
1596 {
1597 /* Must defer modifications to the interval tree in case src
1598 and dest refer to the same string or buffer. */
1599 stuff = Fcons (Fcons (make_number (p),
1600 Fcons (make_number (p + len),
1601 Fcons (plist, Qnil))),
1602 stuff);
1603 }
1604
1605 i = next_interval (i);
1606 if (NULL_INTERVAL_P (i))
1607 break;
1608
1609 p += len;
1610 s = i->position;
1611 }
1612
1613 GCPRO2 (stuff, dest);
1614
1615 while (! NILP (stuff))
1616 {
1617 res = Fcar (stuff);
1618 res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)),
1619 Fcar (Fcdr (Fcdr (res))), dest);
1620 if (! NILP (res))
1621 modified++;
1622 stuff = Fcdr (stuff);
1623 }
1624
1625 UNGCPRO;
1626
1627 return modified ? Qt : Qnil;
1628 }
1629
1630
1631 /* Return a list representing the text properties of OBJECT between
1632 START and END. if PROP is non-nil, report only on that property.
1633 Each result list element has the form (S E PLIST), where S and E
1634 are positions in OBJECT and PLIST is a property list containing the
1635 text properties of OBJECT between S and E. Value is nil if OBJECT
1636 doesn't contain text properties between START and END. */
1637
1638 Lisp_Object
1639 text_property_list (object, start, end, prop)
1640 Lisp_Object object, start, end, prop;
1641 {
1642 struct interval *i;
1643 Lisp_Object result;
1644
1645 result = Qnil;
1646
1647 i = validate_interval_range (object, &start, &end, soft);
1648 if (!NULL_INTERVAL_P (i))
1649 {
1650 int s = XINT (start);
1651 int e = XINT (end);
1652
1653 while (s < e)
1654 {
1655 int interval_end, len;
1656 Lisp_Object plist;
1657
1658 interval_end = i->position + LENGTH (i);
1659 if (interval_end > e)
1660 interval_end = e;
1661 len = interval_end - s;
1662
1663 plist = i->plist;
1664
1665 if (!NILP (prop))
1666 for (; !NILP (plist); plist = Fcdr (Fcdr (plist)))
1667 if (EQ (Fcar (plist), prop))
1668 {
1669 plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
1670 break;
1671 }
1672
1673 if (!NILP (plist))
1674 result = Fcons (Fcons (make_number (s),
1675 Fcons (make_number (s + len),
1676 Fcons (plist, Qnil))),
1677 result);
1678
1679 i = next_interval (i);
1680 if (NULL_INTERVAL_P (i))
1681 break;
1682 s = i->position;
1683 }
1684 }
1685
1686 return result;
1687 }
1688
1689
1690 /* Add text properties to OBJECT from LIST. LIST is a list of triples
1691 (START END PLIST), where START and END are positions and PLIST is a
1692 property list containing the text properties to add. Adjust START
1693 and END positions by DELTA before adding properties. Value is
1694 non-zero if OBJECT was modified. */
1695
1696 int
1697 add_text_properties_from_list (object, list, delta)
1698 Lisp_Object object, list, delta;
1699 {
1700 struct gcpro gcpro1, gcpro2;
1701 int modified_p = 0;
1702
1703 GCPRO2 (list, object);
1704
1705 for (; CONSP (list); list = XCDR (list))
1706 {
1707 Lisp_Object item, start, end, plist, tem;
1708
1709 item = XCAR (list);
1710 start = make_number (XINT (XCAR (item)) + XINT (delta));
1711 end = make_number (XINT (XCAR (XCDR (item))) + XINT (delta));
1712 plist = XCAR (XCDR (XCDR (item)));
1713
1714 tem = Fadd_text_properties (start, end, plist, object);
1715 if (!NILP (tem))
1716 modified_p = 1;
1717 }
1718
1719 UNGCPRO;
1720 return modified_p;
1721 }
1722
1723
1724
1725 /* Modify end-points of ranges in LIST destructively. LIST is a list
1726 as returned from text_property_list. Change end-points equal to
1727 OLD_END to NEW_END. */
1728
1729 void
1730 extend_property_ranges (list, old_end, new_end)
1731 Lisp_Object list, old_end, new_end;
1732 {
1733 for (; CONSP (list); list = XCDR (list))
1734 {
1735 Lisp_Object item, end;
1736
1737 item = XCAR (list);
1738 end = XCAR (XCDR (item));
1739
1740 if (EQ (end, old_end))
1741 XSETCAR (XCDR (item), new_end);
1742 }
1743 }
1744
1745
1746 \f
1747 /* Call the modification hook functions in LIST, each with START and END. */
1748
1749 static void
1750 call_mod_hooks (list, start, end)
1751 Lisp_Object list, start, end;
1752 {
1753 struct gcpro gcpro1;
1754 GCPRO1 (list);
1755 while (!NILP (list))
1756 {
1757 call2 (Fcar (list), start, end);
1758 list = Fcdr (list);
1759 }
1760 UNGCPRO;
1761 }
1762
1763 /* Check for read-only intervals between character positions START ... END,
1764 in BUF, and signal an error if we find one.
1765
1766 Then check for any modification hooks in the range.
1767 Create a list of all these hooks in lexicographic order,
1768 eliminating consecutive extra copies of the same hook. Then call
1769 those hooks in order, with START and END - 1 as arguments. */
1770
1771 void
1772 verify_interval_modification (buf, start, end)
1773 struct buffer *buf;
1774 int start, end;
1775 {
1776 register INTERVAL intervals = BUF_INTERVALS (buf);
1777 register INTERVAL i;
1778 Lisp_Object hooks;
1779 register Lisp_Object prev_mod_hooks;
1780 Lisp_Object mod_hooks;
1781 struct gcpro gcpro1;
1782
1783 hooks = Qnil;
1784 prev_mod_hooks = Qnil;
1785 mod_hooks = Qnil;
1786
1787 interval_insert_behind_hooks = Qnil;
1788 interval_insert_in_front_hooks = Qnil;
1789
1790 if (NULL_INTERVAL_P (intervals))
1791 return;
1792
1793 if (start > end)
1794 {
1795 int temp = start;
1796 start = end;
1797 end = temp;
1798 }
1799
1800 /* For an insert operation, check the two chars around the position. */
1801 if (start == end)
1802 {
1803 INTERVAL prev = NULL;
1804 Lisp_Object before, after;
1805
1806 /* Set I to the interval containing the char after START,
1807 and PREV to the interval containing the char before START.
1808 Either one may be null. They may be equal. */
1809 i = find_interval (intervals, start);
1810
1811 if (start == BUF_BEGV (buf))
1812 prev = 0;
1813 else if (i->position == start)
1814 prev = previous_interval (i);
1815 else if (i->position < start)
1816 prev = i;
1817 if (start == BUF_ZV (buf))
1818 i = 0;
1819
1820 /* If Vinhibit_read_only is set and is not a list, we can
1821 skip the read_only checks. */
1822 if (NILP (Vinhibit_read_only) || CONSP (Vinhibit_read_only))
1823 {
1824 /* If I and PREV differ we need to check for the read-only
1825 property together with its stickiness. If either I or
1826 PREV are 0, this check is all we need.
1827 We have to take special care, since read-only may be
1828 indirectly defined via the category property. */
1829 if (i != prev)
1830 {
1831 if (! NULL_INTERVAL_P (i))
1832 {
1833 after = textget (i->plist, Qread_only);
1834
1835 /* If interval I is read-only and read-only is
1836 front-sticky, inhibit insertion.
1837 Check for read-only as well as category. */
1838 if (! NILP (after)
1839 && NILP (Fmemq (after, Vinhibit_read_only)))
1840 {
1841 Lisp_Object tem;
1842
1843 tem = textget (i->plist, Qfront_sticky);
1844 if (TMEM (Qread_only, tem)
1845 || (NILP (Fplist_get (i->plist, Qread_only))
1846 && TMEM (Qcategory, tem)))
1847 text_read_only ();
1848 }
1849 }
1850
1851 if (! NULL_INTERVAL_P (prev))
1852 {
1853 before = textget (prev->plist, Qread_only);
1854
1855 /* If interval PREV is read-only and read-only isn't
1856 rear-nonsticky, inhibit insertion.
1857 Check for read-only as well as category. */
1858 if (! NILP (before)
1859 && NILP (Fmemq (before, Vinhibit_read_only)))
1860 {
1861 Lisp_Object tem;
1862
1863 tem = textget (prev->plist, Qrear_nonsticky);
1864 if (! TMEM (Qread_only, tem)
1865 && (! NILP (Fplist_get (prev->plist,Qread_only))
1866 || ! TMEM (Qcategory, tem)))
1867 text_read_only ();
1868 }
1869 }
1870 }
1871 else if (! NULL_INTERVAL_P (i))
1872 {
1873 after = textget (i->plist, Qread_only);
1874
1875 /* If interval I is read-only and read-only is
1876 front-sticky, inhibit insertion.
1877 Check for read-only as well as category. */
1878 if (! NILP (after) && NILP (Fmemq (after, Vinhibit_read_only)))
1879 {
1880 Lisp_Object tem;
1881
1882 tem = textget (i->plist, Qfront_sticky);
1883 if (TMEM (Qread_only, tem)
1884 || (NILP (Fplist_get (i->plist, Qread_only))
1885 && TMEM (Qcategory, tem)))
1886 text_read_only ();
1887
1888 tem = textget (prev->plist, Qrear_nonsticky);
1889 if (! TMEM (Qread_only, tem)
1890 && (! NILP (Fplist_get (prev->plist, Qread_only))
1891 || ! TMEM (Qcategory, tem)))
1892 text_read_only ();
1893 }
1894 }
1895 }
1896
1897 /* Run both insert hooks (just once if they're the same). */
1898 if (!NULL_INTERVAL_P (prev))
1899 interval_insert_behind_hooks
1900 = textget (prev->plist, Qinsert_behind_hooks);
1901 if (!NULL_INTERVAL_P (i))
1902 interval_insert_in_front_hooks
1903 = textget (i->plist, Qinsert_in_front_hooks);
1904 }
1905 else
1906 {
1907 /* Loop over intervals on or next to START...END,
1908 collecting their hooks. */
1909
1910 i = find_interval (intervals, start);
1911 do
1912 {
1913 if (! INTERVAL_WRITABLE_P (i))
1914 text_read_only ();
1915
1916 if (!inhibit_modification_hooks)
1917 {
1918 mod_hooks = textget (i->plist, Qmodification_hooks);
1919 if (! NILP (mod_hooks) && ! EQ (mod_hooks, prev_mod_hooks))
1920 {
1921 hooks = Fcons (mod_hooks, hooks);
1922 prev_mod_hooks = mod_hooks;
1923 }
1924 }
1925
1926 i = next_interval (i);
1927 }
1928 /* Keep going thru the interval containing the char before END. */
1929 while (! NULL_INTERVAL_P (i) && i->position < end);
1930
1931 if (!inhibit_modification_hooks)
1932 {
1933 GCPRO1 (hooks);
1934 hooks = Fnreverse (hooks);
1935 while (! EQ (hooks, Qnil))
1936 {
1937 call_mod_hooks (Fcar (hooks), make_number (start),
1938 make_number (end));
1939 hooks = Fcdr (hooks);
1940 }
1941 UNGCPRO;
1942 }
1943 }
1944 }
1945
1946 /* Run the interval hooks for an insertion on character range START ... END.
1947 verify_interval_modification chose which hooks to run;
1948 this function is called after the insertion happens
1949 so it can indicate the range of inserted text. */
1950
1951 void
1952 report_interval_modification (start, end)
1953 Lisp_Object start, end;
1954 {
1955 if (! NILP (interval_insert_behind_hooks))
1956 call_mod_hooks (interval_insert_behind_hooks, start, end);
1957 if (! NILP (interval_insert_in_front_hooks)
1958 && ! EQ (interval_insert_in_front_hooks,
1959 interval_insert_behind_hooks))
1960 call_mod_hooks (interval_insert_in_front_hooks, start, end);
1961 }
1962 \f
1963 void
1964 syms_of_textprop ()
1965 {
1966 DEFVAR_LISP ("default-text-properties", &Vdefault_text_properties,
1967 "Property-list used as default values.\n\
1968 The value of a property in this list is seen as the value for every\n\
1969 character that does not have its own value for that property.");
1970 Vdefault_text_properties = Qnil;
1971
1972 DEFVAR_LISP ("inhibit-point-motion-hooks", &Vinhibit_point_motion_hooks,
1973 "If non-nil, don't run `point-left' and `point-entered' text properties.\n\
1974 This also inhibits the use of the `intangible' text property.");
1975 Vinhibit_point_motion_hooks = Qnil;
1976
1977 DEFVAR_LISP ("text-property-default-nonsticky",
1978 &Vtext_property_default_nonsticky,
1979 "Alist of properties vs the corresponding non-stickinesses.\n\
1980 Each element has the form (PROPERTY . NONSTICKINESS).\n\
1981 \n\
1982 If a character in a buffer has PROPERTY, new text inserted adjacent to\n\
1983 the character doesn't inherit PROPERTY if NONSTICKINESS is non-nil,\n\
1984 inherits it if NONSTICKINESS is nil. The front-sticky and\n\
1985 rear-nonsticky properties of the character overrides NONSTICKINESS.");
1986 Vtext_property_default_nonsticky = Qnil;
1987
1988 staticpro (&interval_insert_behind_hooks);
1989 staticpro (&interval_insert_in_front_hooks);
1990 interval_insert_behind_hooks = Qnil;
1991 interval_insert_in_front_hooks = Qnil;
1992
1993
1994 /* Common attributes one might give text */
1995
1996 staticpro (&Qforeground);
1997 Qforeground = intern ("foreground");
1998 staticpro (&Qbackground);
1999 Qbackground = intern ("background");
2000 staticpro (&Qfont);
2001 Qfont = intern ("font");
2002 staticpro (&Qstipple);
2003 Qstipple = intern ("stipple");
2004 staticpro (&Qunderline);
2005 Qunderline = intern ("underline");
2006 staticpro (&Qread_only);
2007 Qread_only = intern ("read-only");
2008 staticpro (&Qinvisible);
2009 Qinvisible = intern ("invisible");
2010 staticpro (&Qintangible);
2011 Qintangible = intern ("intangible");
2012 staticpro (&Qcategory);
2013 Qcategory = intern ("category");
2014 staticpro (&Qlocal_map);
2015 Qlocal_map = intern ("local-map");
2016 staticpro (&Qfront_sticky);
2017 Qfront_sticky = intern ("front-sticky");
2018 staticpro (&Qrear_nonsticky);
2019 Qrear_nonsticky = intern ("rear-nonsticky");
2020 staticpro (&Qmouse_face);
2021 Qmouse_face = intern ("mouse-face");
2022
2023 /* Properties that text might use to specify certain actions */
2024
2025 staticpro (&Qmouse_left);
2026 Qmouse_left = intern ("mouse-left");
2027 staticpro (&Qmouse_entered);
2028 Qmouse_entered = intern ("mouse-entered");
2029 staticpro (&Qpoint_left);
2030 Qpoint_left = intern ("point-left");
2031 staticpro (&Qpoint_entered);
2032 Qpoint_entered = intern ("point-entered");
2033
2034 defsubr (&Stext_properties_at);
2035 defsubr (&Sget_text_property);
2036 defsubr (&Sget_char_property);
2037 defsubr (&Snext_char_property_change);
2038 defsubr (&Sprevious_char_property_change);
2039 defsubr (&Snext_single_char_property_change);
2040 defsubr (&Sprevious_single_char_property_change);
2041 defsubr (&Snext_property_change);
2042 defsubr (&Snext_single_property_change);
2043 defsubr (&Sprevious_property_change);
2044 defsubr (&Sprevious_single_property_change);
2045 defsubr (&Sadd_text_properties);
2046 defsubr (&Sput_text_property);
2047 defsubr (&Sset_text_properties);
2048 defsubr (&Sremove_text_properties);
2049 defsubr (&Stext_property_any);
2050 defsubr (&Stext_property_not_all);
2051 /* defsubr (&Serase_text_properties); */
2052 /* defsubr (&Scopy_text_properties); */
2053 }
2054