Update copyright.
[bpt/emacs.git] / src / textprop.c
1 /* Interface code for dealing with text properties.
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <config.h>
21 #include "lisp.h"
22 #include "intervals.h"
23 #include "buffer.h"
24 #include "window.h"
25 \f
26
27 /* NOTES: previous- and next- property change will have to skip
28 zero-length intervals if they are implemented. This could be done
29 inside next_interval and previous_interval.
30
31 set_properties needs to deal with the interval property cache.
32
33 It is assumed that for any interval plist, a property appears
34 only once on the list. Although some code i.e., remove_properties,
35 handles the more general case, the uniqueness of properties is
36 necessary for the system to remain consistent. This requirement
37 is enforced by the subrs installing properties onto the intervals. */
38
39 /* The rest of the file is within this conditional */
40 #ifdef USE_TEXT_PROPERTIES
41 \f
42 /* Types of hooks. */
43 Lisp_Object Qmouse_left;
44 Lisp_Object Qmouse_entered;
45 Lisp_Object Qpoint_left;
46 Lisp_Object Qpoint_entered;
47 Lisp_Object Qcategory;
48 Lisp_Object Qlocal_map;
49
50 /* Visual properties text (including strings) may have. */
51 Lisp_Object Qforeground, Qbackground, Qfont, Qunderline, Qstipple;
52 Lisp_Object Qinvisible, Qread_only, Qintangible;
53
54 /* Sticky properties */
55 Lisp_Object Qfront_sticky, Qrear_nonsticky;
56
57 /* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to
58 the o1's cdr. Otherwise, return zero. This is handy for
59 traversing plists. */
60 #define PLIST_ELT_P(o1, o2) (CONSP (o1) && CONSP ((o2) = XCONS (o1)->cdr))
61
62 Lisp_Object Vinhibit_point_motion_hooks;
63
64 \f
65 /* Extract the interval at the position pointed to by BEGIN from
66 OBJECT, a string or buffer. Additionally, check that the positions
67 pointed to by BEGIN and END are within the bounds of OBJECT, and
68 reverse them if *BEGIN is greater than *END. The objects pointed
69 to by BEGIN and END may be integers or markers; if the latter, they
70 are coerced to integers.
71
72 When OBJECT is a string, we increment *BEGIN and *END
73 to make them origin-one.
74
75 Note that buffer points don't correspond to interval indices.
76 For example, point-max is 1 greater than the index of the last
77 character. This difference is handled in the caller, which uses
78 the validated points to determine a length, and operates on that.
79 Exceptions are Ftext_properties_at, Fnext_property_change, and
80 Fprevious_property_change which call this function with BEGIN == END.
81 Handle this case specially.
82
83 If FORCE is soft (0), it's OK to return NULL_INTERVAL. Otherwise,
84 create an interval tree for OBJECT if one doesn't exist, provided
85 the object actually contains text. In the current design, if there
86 is no text, there can be no text properties. */
87
88 #define soft 0
89 #define hard 1
90
91 static INTERVAL
92 validate_interval_range (object, begin, end, force)
93 Lisp_Object object, *begin, *end;
94 int force;
95 {
96 register INTERVAL i;
97 int searchpos;
98
99 CHECK_STRING_OR_BUFFER (object, 0);
100 CHECK_NUMBER_COERCE_MARKER (*begin, 0);
101 CHECK_NUMBER_COERCE_MARKER (*end, 0);
102
103 /* If we are asked for a point, but from a subr which operates
104 on a range, then return nothing. */
105 if (*begin == *end && begin != end)
106 return NULL_INTERVAL;
107
108 if (XINT (*begin) > XINT (*end))
109 {
110 Lisp_Object n;
111 n = *begin;
112 *begin = *end;
113 *end = n;
114 }
115
116 if (XTYPE (object) == Lisp_Buffer)
117 {
118 register struct buffer *b = XBUFFER (object);
119
120 if (!(BUF_BEGV (b) <= XINT (*begin) && XINT (*begin) <= XINT (*end)
121 && XINT (*end) <= BUF_ZV (b)))
122 args_out_of_range (*begin, *end);
123 i = b->intervals;
124
125 /* If there's no text, there are no properties. */
126 if (BUF_BEGV (b) == BUF_ZV (b))
127 return NULL_INTERVAL;
128
129 searchpos = XINT (*begin);
130 }
131 else
132 {
133 register struct Lisp_String *s = XSTRING (object);
134
135 if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end)
136 && XINT (*end) <= s->size))
137 args_out_of_range (*begin, *end);
138 /* User-level Positions in strings start with 0,
139 but the interval code always wants positions starting with 1. */
140 XFASTINT (*begin) += 1;
141 if (begin != end)
142 XFASTINT (*end) += 1;
143 i = s->intervals;
144
145 if (s->size == 0)
146 return NULL_INTERVAL;
147
148 searchpos = XINT (*begin);
149 }
150
151 if (NULL_INTERVAL_P (i))
152 return (force ? create_root_interval (object) : i);
153
154 return find_interval (i, searchpos);
155 }
156
157 /* Validate LIST as a property list. If LIST is not a list, then
158 make one consisting of (LIST nil). Otherwise, verify that LIST
159 is even numbered and thus suitable as a plist. */
160
161 static Lisp_Object
162 validate_plist (list)
163 Lisp_Object list;
164 {
165 if (NILP (list))
166 return Qnil;
167
168 if (CONSP (list))
169 {
170 register int i;
171 register Lisp_Object tail;
172 for (i = 0, tail = list; !NILP (tail); i++)
173 {
174 tail = Fcdr (tail);
175 QUIT;
176 }
177 if (i & 1)
178 error ("Odd length text property list");
179 return list;
180 }
181
182 return Fcons (list, Fcons (Qnil, Qnil));
183 }
184
185 /* Return nonzero if interval I has all the properties,
186 with the same values, of list PLIST. */
187
188 static int
189 interval_has_all_properties (plist, i)
190 Lisp_Object plist;
191 INTERVAL i;
192 {
193 register Lisp_Object tail1, tail2, sym1, sym2;
194 register int found;
195
196 /* Go through each element of PLIST. */
197 for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
198 {
199 sym1 = Fcar (tail1);
200 found = 0;
201
202 /* Go through I's plist, looking for sym1 */
203 for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
204 if (EQ (sym1, Fcar (tail2)))
205 {
206 /* Found the same property on both lists. If the
207 values are unequal, return zero. */
208 if (! EQ (Fcar (Fcdr (tail1)), Fcar (Fcdr (tail2))))
209 return 0;
210
211 /* Property has same value on both lists; go to next one. */
212 found = 1;
213 break;
214 }
215
216 if (! found)
217 return 0;
218 }
219
220 return 1;
221 }
222
223 /* Return nonzero if the plist of interval I has any of the
224 properties of PLIST, regardless of their values. */
225
226 static INLINE int
227 interval_has_some_properties (plist, i)
228 Lisp_Object plist;
229 INTERVAL i;
230 {
231 register Lisp_Object tail1, tail2, sym;
232
233 /* Go through each element of PLIST. */
234 for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
235 {
236 sym = Fcar (tail1);
237
238 /* Go through i's plist, looking for tail1 */
239 for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
240 if (EQ (sym, Fcar (tail2)))
241 return 1;
242 }
243
244 return 0;
245 }
246 \f
247 /* Changing the plists of individual intervals. */
248
249 /* Return the value of PROP in property-list PLIST, or Qunbound if it
250 has none. */
251 static int
252 property_value (plist, prop)
253 {
254 Lisp_Object value;
255
256 while (PLIST_ELT_P (plist, value))
257 if (EQ (XCONS (plist)->car, prop))
258 return XCONS (value)->car;
259 else
260 plist = XCONS (value)->cdr;
261
262 return Qunbound;
263 }
264
265 /* Set the properties of INTERVAL to PROPERTIES,
266 and record undo info for the previous values.
267 OBJECT is the string or buffer that INTERVAL belongs to. */
268
269 static void
270 set_properties (properties, interval, object)
271 Lisp_Object properties, object;
272 INTERVAL interval;
273 {
274 Lisp_Object sym, value;
275
276 if (BUFFERP (object))
277 {
278 /* For each property in the old plist which is missing from PROPERTIES,
279 or has a different value in PROPERTIES, make an undo record. */
280 for (sym = interval->plist;
281 PLIST_ELT_P (sym, value);
282 sym = XCONS (value)->cdr)
283 if (! EQ (property_value (properties, XCONS (sym)->car),
284 XCONS (value)->car))
285 {
286 modify_region (XBUFFER (object),
287 make_number (interval->position),
288 make_number (interval->position + LENGTH (interval)));
289 record_property_change (interval->position, LENGTH (interval),
290 XCONS (sym)->car, XCONS (value)->car,
291 object);
292 }
293
294 /* For each new property that has no value at all in the old plist,
295 make an undo record binding it to nil, so it will be removed. */
296 for (sym = properties;
297 PLIST_ELT_P (sym, value);
298 sym = XCONS (value)->cdr)
299 if (EQ (property_value (interval->plist, XCONS (sym)->car), Qunbound))
300 {
301 modify_region (XBUFFER (object),
302 make_number (interval->position),
303 make_number (interval->position + LENGTH (interval)));
304 record_property_change (interval->position, LENGTH (interval),
305 XCONS (sym)->car, Qnil,
306 object);
307 }
308 }
309
310 /* Store new properties. */
311 interval->plist = Fcopy_sequence (properties);
312 }
313
314 /* Add the properties of PLIST to the interval I, or set
315 the value of I's property to the value of the property on PLIST
316 if they are different.
317
318 OBJECT should be the string or buffer the interval is in.
319
320 Return nonzero if this changes I (i.e., if any members of PLIST
321 are actually added to I's plist) */
322
323 static int
324 add_properties (plist, i, object)
325 Lisp_Object plist;
326 INTERVAL i;
327 Lisp_Object object;
328 {
329 register Lisp_Object tail1, tail2, sym1, val1;
330 register int changed = 0;
331 register int found;
332
333 /* Go through each element of PLIST. */
334 for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
335 {
336 sym1 = Fcar (tail1);
337 val1 = Fcar (Fcdr (tail1));
338 found = 0;
339
340 /* Go through I's plist, looking for sym1 */
341 for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
342 if (EQ (sym1, Fcar (tail2)))
343 {
344 register Lisp_Object this_cdr;
345
346 this_cdr = Fcdr (tail2);
347 /* Found the property. Now check its value. */
348 found = 1;
349
350 /* The properties have the same value on both lists.
351 Continue to the next property. */
352 if (EQ (val1, Fcar (this_cdr)))
353 break;
354
355 /* Record this change in the buffer, for undo purposes. */
356 if (XTYPE (object) == Lisp_Buffer)
357 {
358 modify_region (XBUFFER (object),
359 make_number (i->position),
360 make_number (i->position + LENGTH (i)));
361 record_property_change (i->position, LENGTH (i),
362 sym1, Fcar (this_cdr), object);
363 }
364
365 /* I's property has a different value -- change it */
366 Fsetcar (this_cdr, val1);
367 changed++;
368 break;
369 }
370
371 if (! found)
372 {
373 /* Record this change in the buffer, for undo purposes. */
374 if (XTYPE (object) == Lisp_Buffer)
375 {
376 modify_region (XBUFFER (object),
377 make_number (i->position),
378 make_number (i->position + LENGTH (i)));
379 record_property_change (i->position, LENGTH (i),
380 sym1, Qnil, object);
381 }
382 i->plist = Fcons (sym1, Fcons (val1, i->plist));
383 changed++;
384 }
385 }
386
387 return changed;
388 }
389
390 /* For any members of PLIST which are properties of I, remove them
391 from I's plist.
392 OBJECT is the string or buffer containing I. */
393
394 static int
395 remove_properties (plist, i, object)
396 Lisp_Object plist;
397 INTERVAL i;
398 Lisp_Object object;
399 {
400 register Lisp_Object tail1, tail2, sym, current_plist;
401 register int changed = 0;
402
403 current_plist = i->plist;
404 /* Go through each element of plist. */
405 for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
406 {
407 sym = Fcar (tail1);
408
409 /* First, remove the symbol if its at the head of the list */
410 while (! NILP (current_plist) && EQ (sym, Fcar (current_plist)))
411 {
412 if (XTYPE (object) == Lisp_Buffer)
413 {
414 modify_region (XBUFFER (object),
415 make_number (i->position),
416 make_number (i->position + LENGTH (i)));
417 record_property_change (i->position, LENGTH (i),
418 sym, Fcar (Fcdr (current_plist)),
419 object);
420 }
421
422 current_plist = Fcdr (Fcdr (current_plist));
423 changed++;
424 }
425
426 /* Go through i's plist, looking for sym */
427 tail2 = current_plist;
428 while (! NILP (tail2))
429 {
430 register Lisp_Object this;
431 this = Fcdr (Fcdr (tail2));
432 if (EQ (sym, Fcar (this)))
433 {
434 if (XTYPE (object) == Lisp_Buffer)
435 {
436 modify_region (XBUFFER (object),
437 make_number (i->position),
438 make_number (i->position + LENGTH (i)));
439 record_property_change (i->position, LENGTH (i),
440 sym, Fcar (Fcdr (this)), object);
441 }
442
443 Fsetcdr (Fcdr (tail2), Fcdr (Fcdr (this)));
444 changed++;
445 }
446 tail2 = this;
447 }
448 }
449
450 if (changed)
451 i->plist = current_plist;
452 return changed;
453 }
454
455 #if 0
456 /* Remove all properties from interval I. Return non-zero
457 if this changes the interval. */
458
459 static INLINE int
460 erase_properties (i)
461 INTERVAL i;
462 {
463 if (NILP (i->plist))
464 return 0;
465
466 i->plist = Qnil;
467 return 1;
468 }
469 #endif
470 \f
471 DEFUN ("text-properties-at", Ftext_properties_at,
472 Stext_properties_at, 1, 2, 0,
473 "Return the list of properties held by the character at POSITION\n\
474 in optional argument OBJECT, a string or buffer. If nil, OBJECT\n\
475 defaults to the current buffer.\n\
476 If POSITION is at the end of OBJECT, the value is nil.")
477 (pos, object)
478 Lisp_Object pos, object;
479 {
480 register INTERVAL i;
481
482 if (NILP (object))
483 XSET (object, Lisp_Buffer, current_buffer);
484
485 i = validate_interval_range (object, &pos, &pos, soft);
486 if (NULL_INTERVAL_P (i))
487 return Qnil;
488 /* If POS is at the end of the interval,
489 it means it's the end of OBJECT.
490 There are no properties at the very end,
491 since no character follows. */
492 if (XINT (pos) == LENGTH (i) + i->position)
493 return Qnil;
494
495 return i->plist;
496 }
497
498 DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
499 "Return the value of position POS's property PROP, in OBJECT.\n\
500 OBJECT is optional and defaults to the current buffer.\n\
501 If POSITION is at the end of OBJECT, the value is nil.")
502 (pos, prop, object)
503 Lisp_Object pos, object;
504 register Lisp_Object prop;
505 {
506 register INTERVAL i;
507 register Lisp_Object tail;
508
509 if (NILP (object))
510 XSET (object, Lisp_Buffer, current_buffer);
511 i = validate_interval_range (object, &pos, &pos, soft);
512 if (NULL_INTERVAL_P (i))
513 return Qnil;
514
515 /* If POS is at the end of the interval,
516 it means it's the end of OBJECT.
517 There are no properties at the very end,
518 since no character follows. */
519 if (XINT (pos) == LENGTH (i) + i->position)
520 return Qnil;
521
522 return textget (i->plist, prop);
523 }
524
525 DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
526 "Return the value of position POS's property PROP, in OBJECT.\n\
527 OBJECT is optional and defaults to the current buffer.\n\
528 If POS is at the end of OBJECT, the value is nil.\n\
529 If OBJECT is a buffer, then overlay properties are considered as well as\n\
530 text properties.\n\
531 If OBJECT is a window, then that window's buffer is used, but window-specific\n\
532 overlays are considered only if they are associated with OBJECT.")
533 (pos, prop, object)
534 Lisp_Object pos, object;
535 register Lisp_Object prop;
536 {
537 struct window *w = 0;
538
539 CHECK_NUMBER_COERCE_MARKER (pos, 0);
540
541 if (NILP (object))
542 XSET (object, Lisp_Buffer, current_buffer);
543
544 if (WINDOWP (object))
545 {
546 w = XWINDOW (object);
547 XSET (object, Lisp_Buffer, w->buffer);
548 }
549 if (BUFFERP (object))
550 {
551 int posn = XINT (pos);
552 int noverlays;
553 Lisp_Object *overlay_vec, tem;
554 int next_overlay;
555 int len;
556
557 /* First try with room for 40 overlays. */
558 len = 40;
559 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
560
561 noverlays = overlays_at (posn, 0, &overlay_vec, &len, &next_overlay);
562
563 /* If there are more than 40,
564 make enough space for all, and try again. */
565 if (noverlays > len)
566 {
567 len = noverlays;
568 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
569 noverlays = overlays_at (posn, 0, &overlay_vec, &len, &next_overlay);
570 }
571 noverlays = sort_overlays (overlay_vec, noverlays, w);
572
573 /* Now check the overlays in order of decreasing priority. */
574 while (--noverlays >= 0)
575 {
576 tem = Foverlay_get (overlay_vec[noverlays], prop);
577 if (!NILP (tem))
578 return (tem);
579 }
580 }
581 /* Not a buffer, or no appropriate overlay, so fall through to the
582 simpler case. */
583 return (Fget_text_property (pos, prop, object));
584 }
585
586 DEFUN ("next-property-change", Fnext_property_change,
587 Snext_property_change, 1, 3, 0,
588 "Return the position of next property change.\n\
589 Scans characters forward from POS in OBJECT till it finds\n\
590 a change in some text property, then returns the position of the change.\n\
591 The optional second argument OBJECT is the string or buffer to scan.\n\
592 Return nil if the property is constant all the way to the end of OBJECT.\n\
593 If the value is non-nil, it is a position greater than POS, never equal.\n\n\
594 If the optional third argument LIMIT is non-nil, don't search\n\
595 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
596 (pos, object, limit)
597 Lisp_Object pos, object, limit;
598 {
599 register INTERVAL i, next;
600
601 if (NILP (object))
602 XSET (object, Lisp_Buffer, current_buffer);
603
604 if (!NILP (limit))
605 CHECK_NUMBER_COERCE_MARKER (limit, 0);
606
607 i = validate_interval_range (object, &pos, &pos, soft);
608 if (NULL_INTERVAL_P (i))
609 return limit;
610
611 next = next_interval (i);
612 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next)
613 && (NILP (limit) || next->position < XFASTINT (limit)))
614 next = next_interval (next);
615
616 if (NULL_INTERVAL_P (next))
617 return limit;
618 if (! NILP (limit) && !(next->position < XFASTINT (limit)))
619 return limit;
620
621 return next->position - (XTYPE (object) == Lisp_String);
622 }
623
624 /* Return 1 if there's a change in some property between BEG and END. */
625
626 int
627 property_change_between_p (beg, end)
628 int beg, end;
629 {
630 register INTERVAL i, next;
631 Lisp_Object object, pos;
632
633 XSET (object, Lisp_Buffer, current_buffer);
634 XFASTINT (pos) = beg;
635
636 i = validate_interval_range (object, &pos, &pos, soft);
637 if (NULL_INTERVAL_P (i))
638 return 0;
639
640 next = next_interval (i);
641 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
642 {
643 next = next_interval (next);
644 if (NULL_INTERVAL_P (next))
645 return 0;
646 if (next->position >= end)
647 return 0;
648 }
649
650 if (NULL_INTERVAL_P (next))
651 return 0;
652
653 return 1;
654 }
655
656 DEFUN ("next-single-property-change", Fnext_single_property_change,
657 Snext_single_property_change, 2, 4, 0,
658 "Return the position of next property change for a specific property.\n\
659 Scans characters forward from POS till it finds\n\
660 a change in the PROP property, then returns the position of the change.\n\
661 The optional third argument OBJECT is the string or buffer to scan.\n\
662 The property values are compared with `eq'.\n\
663 Return nil if the property is constant all the way to the end of OBJECT.\n\
664 If the value is non-nil, it is a position greater than POS, never equal.\n\n\
665 If the optional fourth argument LIMIT is non-nil, don't search\n\
666 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
667 (pos, prop, object, limit)
668 Lisp_Object pos, prop, object, limit;
669 {
670 register INTERVAL i, next;
671 register Lisp_Object here_val;
672
673 if (NILP (object))
674 XSET (object, Lisp_Buffer, current_buffer);
675
676 if (!NILP (limit))
677 CHECK_NUMBER_COERCE_MARKER (limit, 0);
678
679 i = validate_interval_range (object, &pos, &pos, soft);
680 if (NULL_INTERVAL_P (i))
681 return limit;
682
683 here_val = textget (i->plist, prop);
684 next = next_interval (i);
685 while (! NULL_INTERVAL_P (next)
686 && EQ (here_val, textget (next->plist, prop))
687 && (NILP (limit) || next->position < XFASTINT (limit)))
688 next = next_interval (next);
689
690 if (NULL_INTERVAL_P (next))
691 return limit;
692 if (! NILP (limit) && !(next->position < XFASTINT (limit)))
693 return limit;
694
695 return next->position - (XTYPE (object) == Lisp_String);
696 }
697
698 DEFUN ("previous-property-change", Fprevious_property_change,
699 Sprevious_property_change, 1, 3, 0,
700 "Return the position of previous property change.\n\
701 Scans characters backwards from POS in OBJECT till it finds\n\
702 a change in some text property, then returns the position of the change.\n\
703 The optional second argument OBJECT is the string or buffer to scan.\n\
704 Return nil if the property is constant all the way to the start of OBJECT.\n\
705 If the value is non-nil, it is a position less than POS, never equal.\n\n\
706 If the optional third argument LIMIT is non-nil, don't search\n\
707 back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
708 (pos, object, limit)
709 Lisp_Object pos, object, limit;
710 {
711 register INTERVAL i, previous;
712
713 if (NILP (object))
714 XSET (object, Lisp_Buffer, current_buffer);
715
716 if (!NILP (limit))
717 CHECK_NUMBER_COERCE_MARKER (limit, 0);
718
719 i = validate_interval_range (object, &pos, &pos, soft);
720 if (NULL_INTERVAL_P (i))
721 return limit;
722
723 /* Start with the interval containing the char before point. */
724 if (i->position == XFASTINT (pos))
725 i = previous_interval (i);
726
727 previous = previous_interval (i);
728 while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i)
729 && (NILP (limit)
730 || previous->position + LENGTH (previous) > XFASTINT (limit)))
731 previous = previous_interval (previous);
732 if (NULL_INTERVAL_P (previous))
733 return limit;
734 if (!NILP (limit)
735 && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
736 return limit;
737
738 return (previous->position + LENGTH (previous)
739 - (XTYPE (object) == Lisp_String));
740 }
741
742 DEFUN ("previous-single-property-change", Fprevious_single_property_change,
743 Sprevious_single_property_change, 2, 4, 0,
744 "Return the position of previous property change for a specific property.\n\
745 Scans characters backward from POS till it finds\n\
746 a change in the PROP property, then returns the position of the change.\n\
747 The optional third argument OBJECT is the string or buffer to scan.\n\
748 The property values are compared with `eq'.\n\
749 Return nil if the property is constant all the way to the start of OBJECT.\n\
750 If the value is non-nil, it is a position less than POS, never equal.\n\n\
751 If the optional fourth argument LIMIT is non-nil, don't search\n\
752 back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
753 (pos, prop, object, limit)
754 Lisp_Object pos, prop, object, limit;
755 {
756 register INTERVAL i, previous;
757 register Lisp_Object here_val;
758
759 if (NILP (object))
760 XSET (object, Lisp_Buffer, current_buffer);
761
762 if (!NILP (limit))
763 CHECK_NUMBER_COERCE_MARKER (limit, 0);
764
765 i = validate_interval_range (object, &pos, &pos, soft);
766 if (NULL_INTERVAL_P (i))
767 return limit;
768
769 /* Start with the interval containing the char before point. */
770 if (i->position == XFASTINT (pos))
771 i = previous_interval (i);
772
773 here_val = textget (i->plist, prop);
774 previous = previous_interval (i);
775 while (! NULL_INTERVAL_P (previous)
776 && EQ (here_val, textget (previous->plist, prop))
777 && (NILP (limit)
778 || previous->position + LENGTH (previous) > XFASTINT (limit)))
779 previous = previous_interval (previous);
780 if (NULL_INTERVAL_P (previous))
781 return limit;
782 if (!NILP (limit)
783 && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
784 return limit;
785
786 return (previous->position + LENGTH (previous)
787 - (XTYPE (object) == Lisp_String));
788 }
789
790 DEFUN ("add-text-properties", Fadd_text_properties,
791 Sadd_text_properties, 3, 4, 0,
792 "Add properties to the text from START to END.\n\
793 The third argument PROPS is a property list\n\
794 specifying the property values to add.\n\
795 The optional fourth argument, OBJECT,\n\
796 is the string or buffer containing the text.\n\
797 Return t if any property value actually changed, nil otherwise.")
798 (start, end, properties, object)
799 Lisp_Object start, end, properties, object;
800 {
801 register INTERVAL i, unchanged;
802 register int s, len, modified = 0;
803
804 properties = validate_plist (properties);
805 if (NILP (properties))
806 return Qnil;
807
808 if (NILP (object))
809 XSET (object, Lisp_Buffer, current_buffer);
810
811 i = validate_interval_range (object, &start, &end, hard);
812 if (NULL_INTERVAL_P (i))
813 return Qnil;
814
815 s = XINT (start);
816 len = XINT (end) - s;
817
818 /* If we're not starting on an interval boundary, we have to
819 split this interval. */
820 if (i->position != s)
821 {
822 /* If this interval already has the properties, we can
823 skip it. */
824 if (interval_has_all_properties (properties, i))
825 {
826 int got = (LENGTH (i) - (s - i->position));
827 if (got >= len)
828 return Qnil;
829 len -= got;
830 i = next_interval (i);
831 }
832 else
833 {
834 unchanged = i;
835 i = split_interval_right (unchanged, s - unchanged->position);
836 copy_properties (unchanged, i);
837 }
838 }
839
840 /* We are at the beginning of interval I, with LEN chars to scan. */
841 for (;;)
842 {
843 if (i == 0)
844 abort ();
845
846 if (LENGTH (i) >= len)
847 {
848 if (interval_has_all_properties (properties, i))
849 return modified ? Qt : Qnil;
850
851 if (LENGTH (i) == len)
852 {
853 add_properties (properties, i, object);
854 return Qt;
855 }
856
857 /* i doesn't have the properties, and goes past the change limit */
858 unchanged = i;
859 i = split_interval_left (unchanged, len);
860 copy_properties (unchanged, i);
861 add_properties (properties, i, object);
862 return Qt;
863 }
864
865 len -= LENGTH (i);
866 modified += add_properties (properties, i, object);
867 i = next_interval (i);
868 }
869 }
870
871 DEFUN ("put-text-property", Fput_text_property,
872 Sput_text_property, 4, 5, 0,
873 "Set one property of the text from START to END.\n\
874 The third and fourth arguments PROP and VALUE\n\
875 specify the property to add.\n\
876 The optional fifth argument, OBJECT,\n\
877 is the string or buffer containing the text.")
878 (start, end, prop, value, object)
879 Lisp_Object start, end, prop, value, object;
880 {
881 Fadd_text_properties (start, end,
882 Fcons (prop, Fcons (value, Qnil)),
883 object);
884 return Qnil;
885 }
886
887 DEFUN ("set-text-properties", Fset_text_properties,
888 Sset_text_properties, 3, 4, 0,
889 "Completely replace properties of text from START to END.\n\
890 The third argument PROPS is the new property list.\n\
891 The optional fourth argument, OBJECT,\n\
892 is the string or buffer containing the text.")
893 (start, end, props, object)
894 Lisp_Object start, end, props, object;
895 {
896 register INTERVAL i, unchanged;
897 register INTERVAL prev_changed = NULL_INTERVAL;
898 register int s, len;
899
900 props = validate_plist (props);
901
902 if (NILP (object))
903 XSET (object, Lisp_Buffer, current_buffer);
904
905 i = validate_interval_range (object, &start, &end, hard);
906 if (NULL_INTERVAL_P (i))
907 return Qnil;
908
909 s = XINT (start);
910 len = XINT (end) - s;
911
912 if (i->position != s)
913 {
914 unchanged = i;
915 i = split_interval_right (unchanged, s - unchanged->position);
916
917 if (LENGTH (i) > len)
918 {
919 copy_properties (unchanged, i);
920 i = split_interval_left (i, len);
921 set_properties (props, i, object);
922 return Qt;
923 }
924
925 set_properties (props, i, object);
926
927 if (LENGTH (i) == len)
928 return Qt;
929
930 prev_changed = i;
931 len -= LENGTH (i);
932 i = next_interval (i);
933 }
934
935 /* We are starting at the beginning of an interval, I */
936 while (len > 0)
937 {
938 if (i == 0)
939 abort ();
940
941 if (LENGTH (i) >= len)
942 {
943 if (LENGTH (i) > len)
944 i = split_interval_left (i, len);
945
946 if (NULL_INTERVAL_P (prev_changed))
947 set_properties (props, i, object);
948 else
949 merge_interval_left (i);
950 return Qt;
951 }
952
953 len -= LENGTH (i);
954 if (NULL_INTERVAL_P (prev_changed))
955 {
956 set_properties (props, i, object);
957 prev_changed = i;
958 }
959 else
960 prev_changed = i = merge_interval_left (i);
961
962 i = next_interval (i);
963 }
964
965 return Qt;
966 }
967
968 DEFUN ("remove-text-properties", Fremove_text_properties,
969 Sremove_text_properties, 3, 4, 0,
970 "Remove some properties from text from START to END.\n\
971 The third argument PROPS is a property list\n\
972 whose property names specify the properties to remove.\n\
973 \(The values stored in PROPS are ignored.)\n\
974 The optional fourth argument, OBJECT,\n\
975 is the string or buffer containing the text.\n\
976 Return t if any property was actually removed, nil otherwise.")
977 (start, end, props, object)
978 Lisp_Object start, end, props, object;
979 {
980 register INTERVAL i, unchanged;
981 register int s, len, modified = 0;
982
983 if (NILP (object))
984 XSET (object, Lisp_Buffer, current_buffer);
985
986 i = validate_interval_range (object, &start, &end, soft);
987 if (NULL_INTERVAL_P (i))
988 return Qnil;
989
990 s = XINT (start);
991 len = XINT (end) - s;
992
993 if (i->position != s)
994 {
995 /* No properties on this first interval -- return if
996 it covers the entire region. */
997 if (! interval_has_some_properties (props, i))
998 {
999 int got = (LENGTH (i) - (s - i->position));
1000 if (got >= len)
1001 return Qnil;
1002 len -= got;
1003 i = next_interval (i);
1004 }
1005 /* Split away the beginning of this interval; what we don't
1006 want to modify. */
1007 else
1008 {
1009 unchanged = i;
1010 i = split_interval_right (unchanged, s - unchanged->position);
1011 copy_properties (unchanged, i);
1012 }
1013 }
1014
1015 /* We are at the beginning of an interval, with len to scan */
1016 for (;;)
1017 {
1018 if (i == 0)
1019 abort ();
1020
1021 if (LENGTH (i) >= len)
1022 {
1023 if (! interval_has_some_properties (props, i))
1024 return modified ? Qt : Qnil;
1025
1026 if (LENGTH (i) == len)
1027 {
1028 remove_properties (props, i, object);
1029 return Qt;
1030 }
1031
1032 /* i has the properties, and goes past the change limit */
1033 unchanged = i;
1034 i = split_interval_left (i, len);
1035 copy_properties (unchanged, i);
1036 remove_properties (props, i, object);
1037 return Qt;
1038 }
1039
1040 len -= LENGTH (i);
1041 modified += remove_properties (props, i, object);
1042 i = next_interval (i);
1043 }
1044 }
1045
1046 DEFUN ("text-property-any", Ftext_property_any,
1047 Stext_property_any, 4, 5, 0,
1048 "Check text from START to END to see if PROP is ever `eq' to VALUE.\n\
1049 If so, return the position of the first character whose PROP is `eq'\n\
1050 to VALUE. Otherwise return nil.\n\
1051 The optional fifth argument, OBJECT, is the string or buffer\n\
1052 containing the text.")
1053 (start, end, prop, value, object)
1054 Lisp_Object start, end, prop, value, object;
1055 {
1056 register INTERVAL i;
1057 register int e, pos;
1058
1059 if (NILP (object))
1060 XSET (object, Lisp_Buffer, current_buffer);
1061 i = validate_interval_range (object, &start, &end, soft);
1062 e = XINT (end);
1063
1064 while (! NULL_INTERVAL_P (i))
1065 {
1066 if (i->position >= e)
1067 break;
1068 if (EQ (textget (i->plist, prop), value))
1069 {
1070 pos = i->position;
1071 if (pos < XINT (start))
1072 pos = XINT (start);
1073 return make_number (pos - (XTYPE (object) == Lisp_String));
1074 }
1075 i = next_interval (i);
1076 }
1077 return Qnil;
1078 }
1079
1080 DEFUN ("text-property-not-all", Ftext_property_not_all,
1081 Stext_property_not_all, 4, 5, 0,
1082 "Check text from START to END to see if PROP is ever not `eq' to VALUE.\n\
1083 If so, return the position of the first character whose PROP is not\n\
1084 `eq' to VALUE. Otherwise, return nil.\n\
1085 The optional fifth argument, OBJECT, is the string or buffer\n\
1086 containing the text.")
1087 (start, end, prop, value, object)
1088 Lisp_Object start, end, prop, value, object;
1089 {
1090 register INTERVAL i;
1091 register int s, e;
1092
1093 if (NILP (object))
1094 XSET (object, Lisp_Buffer, current_buffer);
1095 i = validate_interval_range (object, &start, &end, soft);
1096 if (NULL_INTERVAL_P (i))
1097 return (NILP (value) || EQ (start, end)) ? Qnil : start;
1098 s = XINT (start);
1099 e = XINT (end);
1100
1101 while (! NULL_INTERVAL_P (i))
1102 {
1103 if (i->position >= e)
1104 break;
1105 if (! EQ (textget (i->plist, prop), value))
1106 {
1107 if (i->position > s)
1108 s = i->position;
1109 return make_number (s - (XTYPE (object) == Lisp_String));
1110 }
1111 i = next_interval (i);
1112 }
1113 return Qnil;
1114 }
1115
1116 #if 0 /* You can use set-text-properties for this. */
1117
1118 DEFUN ("erase-text-properties", Ferase_text_properties,
1119 Serase_text_properties, 2, 3, 0,
1120 "Remove all properties from the text from START to END.\n\
1121 The optional third argument, OBJECT,\n\
1122 is the string or buffer containing the text.")
1123 (start, end, object)
1124 Lisp_Object start, end, object;
1125 {
1126 register INTERVAL i;
1127 register INTERVAL prev_changed = NULL_INTERVAL;
1128 register int s, len, modified;
1129
1130 if (NILP (object))
1131 XSET (object, Lisp_Buffer, current_buffer);
1132
1133 i = validate_interval_range (object, &start, &end, soft);
1134 if (NULL_INTERVAL_P (i))
1135 return Qnil;
1136
1137 s = XINT (start);
1138 len = XINT (end) - s;
1139
1140 if (i->position != s)
1141 {
1142 register int got;
1143 register INTERVAL unchanged = i;
1144
1145 /* If there are properties here, then this text will be modified. */
1146 if (! NILP (i->plist))
1147 {
1148 i = split_interval_right (unchanged, s - unchanged->position);
1149 i->plist = Qnil;
1150 modified++;
1151
1152 if (LENGTH (i) > len)
1153 {
1154 i = split_interval_right (i, len);
1155 copy_properties (unchanged, i);
1156 return Qt;
1157 }
1158
1159 if (LENGTH (i) == len)
1160 return Qt;
1161
1162 got = LENGTH (i);
1163 }
1164 /* If the text of I is without any properties, and contains
1165 LEN or more characters, then we may return without changing
1166 anything.*/
1167 else if (LENGTH (i) - (s - i->position) <= len)
1168 return Qnil;
1169 /* The amount of text to change extends past I, so just note
1170 how much we've gotten. */
1171 else
1172 got = LENGTH (i) - (s - i->position);
1173
1174 len -= got;
1175 prev_changed = i;
1176 i = next_interval (i);
1177 }
1178
1179 /* We are starting at the beginning of an interval, I. */
1180 while (len > 0)
1181 {
1182 if (LENGTH (i) >= len)
1183 {
1184 /* If I has no properties, simply merge it if possible. */
1185 if (NILP (i->plist))
1186 {
1187 if (! NULL_INTERVAL_P (prev_changed))
1188 merge_interval_left (i);
1189
1190 return modified ? Qt : Qnil;
1191 }
1192
1193 if (LENGTH (i) > len)
1194 i = split_interval_left (i, len);
1195 if (! NULL_INTERVAL_P (prev_changed))
1196 merge_interval_left (i);
1197 else
1198 i->plist = Qnil;
1199
1200 return Qt;
1201 }
1202
1203 /* Here if we still need to erase past the end of I */
1204 len -= LENGTH (i);
1205 if (NULL_INTERVAL_P (prev_changed))
1206 {
1207 modified += erase_properties (i);
1208 prev_changed = i;
1209 }
1210 else
1211 {
1212 modified += ! NILP (i->plist);
1213 /* Merging I will give it the properties of PREV_CHANGED. */
1214 prev_changed = i = merge_interval_left (i);
1215 }
1216
1217 i = next_interval (i);
1218 }
1219
1220 return modified ? Qt : Qnil;
1221 }
1222 #endif /* 0 */
1223
1224 /* I don't think this is the right interface to export; how often do you
1225 want to do something like this, other than when you're copying objects
1226 around?
1227
1228 I think it would be better to have a pair of functions, one which
1229 returns the text properties of a region as a list of ranges and
1230 plists, and another which applies such a list to another object. */
1231
1232 /* DEFUN ("copy-text-properties", Fcopy_text_properties,
1233 Scopy_text_properties, 5, 6, 0,
1234 "Add properties from SRC-START to SRC-END of SRC at DEST-POS of DEST.\n\
1235 SRC and DEST may each refer to strings or buffers.\n\
1236 Optional sixth argument PROP causes only that property to be copied.\n\
1237 Properties are copied to DEST as if by `add-text-properties'.\n\
1238 Return t if any property value actually changed, nil otherwise.") */
1239
1240 Lisp_Object
1241 copy_text_properties (start, end, src, pos, dest, prop)
1242 Lisp_Object start, end, src, pos, dest, prop;
1243 {
1244 INTERVAL i;
1245 Lisp_Object res;
1246 Lisp_Object stuff;
1247 Lisp_Object plist;
1248 int s, e, e2, p, len, modified = 0;
1249
1250 i = validate_interval_range (src, &start, &end, soft);
1251 if (NULL_INTERVAL_P (i))
1252 return Qnil;
1253
1254 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1255 {
1256 Lisp_Object dest_start, dest_end;
1257
1258 dest_start = pos;
1259 XFASTINT (dest_end) = XINT (dest_start) + (XINT (end) - XINT (start));
1260 /* Apply this to a copy of pos; it will try to increment its arguments,
1261 which we don't want. */
1262 validate_interval_range (dest, &dest_start, &dest_end, soft);
1263 }
1264
1265 s = XINT (start);
1266 e = XINT (end);
1267 p = XINT (pos);
1268
1269 stuff = Qnil;
1270
1271 while (s < e)
1272 {
1273 e2 = i->position + LENGTH (i);
1274 if (e2 > e)
1275 e2 = e;
1276 len = e2 - s;
1277
1278 plist = i->plist;
1279 if (! NILP (prop))
1280 while (! NILP (plist))
1281 {
1282 if (EQ (Fcar (plist), prop))
1283 {
1284 plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
1285 break;
1286 }
1287 plist = Fcdr (Fcdr (plist));
1288 }
1289 if (! NILP (plist))
1290 {
1291 /* Must defer modifications to the interval tree in case src
1292 and dest refer to the same string or buffer. */
1293 stuff = Fcons (Fcons (make_number (p),
1294 Fcons (make_number (p + len),
1295 Fcons (plist, Qnil))),
1296 stuff);
1297 }
1298
1299 i = next_interval (i);
1300 if (NULL_INTERVAL_P (i))
1301 break;
1302
1303 p += len;
1304 s = i->position;
1305 }
1306
1307 while (! NILP (stuff))
1308 {
1309 res = Fcar (stuff);
1310 res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)),
1311 Fcar (Fcdr (Fcdr (res))), dest);
1312 if (! NILP (res))
1313 modified++;
1314 stuff = Fcdr (stuff);
1315 }
1316
1317 return modified ? Qt : Qnil;
1318 }
1319
1320 void
1321 syms_of_textprop ()
1322 {
1323 DEFVAR_INT ("interval-balance-threshold", &interval_balance_threshold,
1324 "Threshold for rebalancing interval trees, expressed as the\n\
1325 percentage by which the left interval tree should not differ from the right.");
1326 interval_balance_threshold = 8;
1327
1328 DEFVAR_LISP ("inhibit-point-motion-hooks", &Vinhibit_point_motion_hooks,
1329 "If non-nil, don't call the text property values of\n\
1330 `point-left' and `point-entered'.");
1331 Vinhibit_point_motion_hooks = Qnil;
1332
1333 /* Common attributes one might give text */
1334
1335 staticpro (&Qforeground);
1336 Qforeground = intern ("foreground");
1337 staticpro (&Qbackground);
1338 Qbackground = intern ("background");
1339 staticpro (&Qfont);
1340 Qfont = intern ("font");
1341 staticpro (&Qstipple);
1342 Qstipple = intern ("stipple");
1343 staticpro (&Qunderline);
1344 Qunderline = intern ("underline");
1345 staticpro (&Qread_only);
1346 Qread_only = intern ("read-only");
1347 staticpro (&Qinvisible);
1348 Qinvisible = intern ("invisible");
1349 staticpro (&Qintangible);
1350 Qintangible = intern ("intangible");
1351 staticpro (&Qcategory);
1352 Qcategory = intern ("category");
1353 staticpro (&Qlocal_map);
1354 Qlocal_map = intern ("local-map");
1355 staticpro (&Qfront_sticky);
1356 Qfront_sticky = intern ("front-sticky");
1357 staticpro (&Qrear_nonsticky);
1358 Qrear_nonsticky = intern ("rear-nonsticky");
1359
1360 /* Properties that text might use to specify certain actions */
1361
1362 staticpro (&Qmouse_left);
1363 Qmouse_left = intern ("mouse-left");
1364 staticpro (&Qmouse_entered);
1365 Qmouse_entered = intern ("mouse-entered");
1366 staticpro (&Qpoint_left);
1367 Qpoint_left = intern ("point-left");
1368 staticpro (&Qpoint_entered);
1369 Qpoint_entered = intern ("point-entered");
1370
1371 defsubr (&Stext_properties_at);
1372 defsubr (&Sget_text_property);
1373 defsubr (&Snext_property_change);
1374 defsubr (&Snext_single_property_change);
1375 defsubr (&Sprevious_property_change);
1376 defsubr (&Sprevious_single_property_change);
1377 defsubr (&Sadd_text_properties);
1378 defsubr (&Sput_text_property);
1379 defsubr (&Sset_text_properties);
1380 defsubr (&Sremove_text_properties);
1381 defsubr (&Stext_property_any);
1382 defsubr (&Stext_property_not_all);
1383 /* defsubr (&Serase_text_properties); */
1384 /* defsubr (&Scopy_text_properties); */
1385 }
1386
1387 #else
1388
1389 lose -- this shouldn't be compiled if USE_TEXT_PROPERTIES isn't defined
1390
1391 #endif /* USE_TEXT_PROPERTIES */