(Vafter_change_functions, Vbefore_change_functions): Declared.
[bpt/emacs.git] / src / textprop.c
1 /* Interface code for dealing with text properties.
2 Copyright (C) 1993 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 i = validate_interval_range (object, &pos, &pos, soft);
605 if (NULL_INTERVAL_P (i))
606 return limit;
607
608 next = next_interval (i);
609 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next)
610 && (NILP (limit) || next->position < XFASTINT (limit)))
611 next = next_interval (next);
612
613 if (NULL_INTERVAL_P (next))
614 return limit;
615 if (! NILP (limit) && !(next->position < XFASTINT (limit)))
616 return limit;
617
618 return next->position - (XTYPE (object) == Lisp_String);
619 }
620
621 /* Return 1 if there's a change in some property between BEG and END. */
622
623 int
624 property_change_between_p (beg, end)
625 int beg, end;
626 {
627 register INTERVAL i, next;
628 Lisp_Object object, pos;
629
630 XSET (object, Lisp_Buffer, current_buffer);
631 XFASTINT (pos) = beg;
632
633 i = validate_interval_range (object, &pos, &pos, soft);
634 if (NULL_INTERVAL_P (i))
635 return 0;
636
637 next = next_interval (i);
638 while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
639 {
640 next = next_interval (next);
641 if (NULL_INTERVAL_P (next))
642 return 0;
643 if (next->position >= end)
644 return 0;
645 }
646
647 if (NULL_INTERVAL_P (next))
648 return 0;
649
650 return 1;
651 }
652
653 DEFUN ("next-single-property-change", Fnext_single_property_change,
654 Snext_single_property_change, 2, 4, 0,
655 "Return the position of next property change for a specific property.\n\
656 Scans characters forward from POS till it finds\n\
657 a change in the PROP property, then returns the position of the change.\n\
658 The optional third argument OBJECT is the string or buffer to scan.\n\
659 The property values are compared with `eq'.\n\
660 Return nil if the property is constant all the way to the end of OBJECT.\n\
661 If the value is non-nil, it is a position greater than POS, never equal.\n\n\
662 If the optional fourth argument LIMIT is non-nil, don't search\n\
663 past position LIMIT; return LIMIT if nothing is found before LIMIT.")
664 (pos, prop, object, limit)
665 Lisp_Object pos, prop, object, limit;
666 {
667 register INTERVAL i, next;
668 register Lisp_Object here_val;
669
670 if (NILP (object))
671 XSET (object, Lisp_Buffer, current_buffer);
672
673 i = validate_interval_range (object, &pos, &pos, soft);
674 if (NULL_INTERVAL_P (i))
675 return limit;
676
677 here_val = textget (i->plist, prop);
678 next = next_interval (i);
679 while (! NULL_INTERVAL_P (next)
680 && EQ (here_val, textget (next->plist, prop))
681 && (NILP (limit) || next->position < XFASTINT (limit)))
682 next = next_interval (next);
683
684 if (NULL_INTERVAL_P (next))
685 return limit;
686 if (! NILP (limit) && !(next->position < XFASTINT (limit)))
687 return limit;
688
689 return next->position - (XTYPE (object) == Lisp_String);
690 }
691
692 DEFUN ("previous-property-change", Fprevious_property_change,
693 Sprevious_property_change, 1, 3, 0,
694 "Return the position of previous property change.\n\
695 Scans characters backwards from POS in OBJECT till it finds\n\
696 a change in some text property, then returns the position of the change.\n\
697 The optional second argument OBJECT is the string or buffer to scan.\n\
698 Return nil if the property is constant all the way to the start of OBJECT.\n\
699 If the value is non-nil, it is a position less than POS, never equal.\n\n\
700 If the optional third argument LIMIT is non-nil, don't search\n\
701 back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
702 (pos, object, limit)
703 Lisp_Object pos, object, limit;
704 {
705 register INTERVAL i, previous;
706
707 if (NILP (object))
708 XSET (object, Lisp_Buffer, current_buffer);
709
710 i = validate_interval_range (object, &pos, &pos, soft);
711 if (NULL_INTERVAL_P (i))
712 return limit;
713
714 /* Start with the interval containing the char before point. */
715 if (i->position == XFASTINT (pos))
716 i = previous_interval (i);
717
718 previous = previous_interval (i);
719 while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i)
720 && (NILP (limit)
721 || previous->position + LENGTH (previous) > XFASTINT (limit)))
722 previous = previous_interval (previous);
723 if (NULL_INTERVAL_P (previous))
724 return limit;
725 if (!NILP (limit)
726 && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
727 return limit;
728
729 return (previous->position + LENGTH (previous)
730 - (XTYPE (object) == Lisp_String));
731 }
732
733 DEFUN ("previous-single-property-change", Fprevious_single_property_change,
734 Sprevious_single_property_change, 2, 4, 0,
735 "Return the position of previous property change for a specific property.\n\
736 Scans characters backward from POS till it finds\n\
737 a change in the PROP property, then returns the position of the change.\n\
738 The optional third argument OBJECT is the string or buffer to scan.\n\
739 The property values are compared with `eq'.\n\
740 Return nil if the property is constant all the way to the start of OBJECT.\n\
741 If the value is non-nil, it is a position less than POS, never equal.\n\n\
742 If the optional fourth argument LIMIT is non-nil, don't search\n\
743 back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
744 (pos, prop, object, limit)
745 Lisp_Object pos, prop, object, limit;
746 {
747 register INTERVAL i, previous;
748 register Lisp_Object here_val;
749
750 if (NILP (object))
751 XSET (object, Lisp_Buffer, current_buffer);
752
753 i = validate_interval_range (object, &pos, &pos, soft);
754 if (NULL_INTERVAL_P (i))
755 return limit;
756
757 /* Start with the interval containing the char before point. */
758 if (i->position == XFASTINT (pos))
759 i = previous_interval (i);
760
761 here_val = textget (i->plist, prop);
762 previous = previous_interval (i);
763 while (! NULL_INTERVAL_P (previous)
764 && EQ (here_val, textget (previous->plist, prop))
765 && (NILP (limit)
766 || previous->position + LENGTH (previous) > XFASTINT (limit)))
767 previous = previous_interval (previous);
768 if (NULL_INTERVAL_P (previous))
769 return limit;
770 if (!NILP (limit)
771 && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
772 return limit;
773
774 return (previous->position + LENGTH (previous)
775 - (XTYPE (object) == Lisp_String));
776 }
777
778 DEFUN ("add-text-properties", Fadd_text_properties,
779 Sadd_text_properties, 3, 4, 0,
780 "Add properties to the text from START to END.\n\
781 The third argument PROPS is a property list\n\
782 specifying the property values to add.\n\
783 The optional fourth argument, OBJECT,\n\
784 is the string or buffer containing the text.\n\
785 Return t if any property value actually changed, nil otherwise.")
786 (start, end, properties, object)
787 Lisp_Object start, end, properties, object;
788 {
789 register INTERVAL i, unchanged;
790 register int s, len, modified = 0;
791
792 properties = validate_plist (properties);
793 if (NILP (properties))
794 return Qnil;
795
796 if (NILP (object))
797 XSET (object, Lisp_Buffer, current_buffer);
798
799 i = validate_interval_range (object, &start, &end, hard);
800 if (NULL_INTERVAL_P (i))
801 return Qnil;
802
803 s = XINT (start);
804 len = XINT (end) - s;
805
806 /* If we're not starting on an interval boundary, we have to
807 split this interval. */
808 if (i->position != s)
809 {
810 /* If this interval already has the properties, we can
811 skip it. */
812 if (interval_has_all_properties (properties, i))
813 {
814 int got = (LENGTH (i) - (s - i->position));
815 if (got >= len)
816 return Qnil;
817 len -= got;
818 i = next_interval (i);
819 }
820 else
821 {
822 unchanged = i;
823 i = split_interval_right (unchanged, s - unchanged->position);
824 copy_properties (unchanged, i);
825 }
826 }
827
828 /* We are at the beginning of interval I, with LEN chars to scan. */
829 for (;;)
830 {
831 if (i == 0)
832 abort ();
833
834 if (LENGTH (i) >= len)
835 {
836 if (interval_has_all_properties (properties, i))
837 return modified ? Qt : Qnil;
838
839 if (LENGTH (i) == len)
840 {
841 add_properties (properties, i, object);
842 return Qt;
843 }
844
845 /* i doesn't have the properties, and goes past the change limit */
846 unchanged = i;
847 i = split_interval_left (unchanged, len);
848 copy_properties (unchanged, i);
849 add_properties (properties, i, object);
850 return Qt;
851 }
852
853 len -= LENGTH (i);
854 modified += add_properties (properties, i, object);
855 i = next_interval (i);
856 }
857 }
858
859 DEFUN ("put-text-property", Fput_text_property,
860 Sput_text_property, 4, 5, 0,
861 "Set one property of the text from START to END.\n\
862 The third and fourth arguments PROP and VALUE\n\
863 specify the property to add.\n\
864 The optional fifth argument, OBJECT,\n\
865 is the string or buffer containing the text.")
866 (start, end, prop, value, object)
867 Lisp_Object start, end, prop, value, object;
868 {
869 Fadd_text_properties (start, end,
870 Fcons (prop, Fcons (value, Qnil)),
871 object);
872 return Qnil;
873 }
874
875 DEFUN ("set-text-properties", Fset_text_properties,
876 Sset_text_properties, 3, 4, 0,
877 "Completely replace properties of text from START to END.\n\
878 The third argument PROPS is the new property list.\n\
879 The optional fourth argument, OBJECT,\n\
880 is the string or buffer containing the text.")
881 (start, end, props, object)
882 Lisp_Object start, end, props, object;
883 {
884 register INTERVAL i, unchanged;
885 register INTERVAL prev_changed = NULL_INTERVAL;
886 register int s, len;
887
888 props = validate_plist (props);
889
890 if (NILP (object))
891 XSET (object, Lisp_Buffer, current_buffer);
892
893 i = validate_interval_range (object, &start, &end, hard);
894 if (NULL_INTERVAL_P (i))
895 return Qnil;
896
897 s = XINT (start);
898 len = XINT (end) - s;
899
900 if (i->position != s)
901 {
902 unchanged = i;
903 i = split_interval_right (unchanged, s - unchanged->position);
904
905 if (LENGTH (i) > len)
906 {
907 copy_properties (unchanged, i);
908 i = split_interval_left (i, len);
909 set_properties (props, i, object);
910 return Qt;
911 }
912
913 set_properties (props, i, object);
914
915 if (LENGTH (i) == len)
916 return Qt;
917
918 prev_changed = i;
919 len -= LENGTH (i);
920 i = next_interval (i);
921 }
922
923 /* We are starting at the beginning of an interval, I */
924 while (len > 0)
925 {
926 if (i == 0)
927 abort ();
928
929 if (LENGTH (i) >= len)
930 {
931 if (LENGTH (i) > len)
932 i = split_interval_left (i, len);
933
934 if (NULL_INTERVAL_P (prev_changed))
935 set_properties (props, i, object);
936 else
937 merge_interval_left (i);
938 return Qt;
939 }
940
941 len -= LENGTH (i);
942 if (NULL_INTERVAL_P (prev_changed))
943 {
944 set_properties (props, i, object);
945 prev_changed = i;
946 }
947 else
948 prev_changed = i = merge_interval_left (i);
949
950 i = next_interval (i);
951 }
952
953 return Qt;
954 }
955
956 DEFUN ("remove-text-properties", Fremove_text_properties,
957 Sremove_text_properties, 3, 4, 0,
958 "Remove some properties from text from START to END.\n\
959 The third argument PROPS is a property list\n\
960 whose property names specify the properties to remove.\n\
961 \(The values stored in PROPS are ignored.)\n\
962 The optional fourth argument, OBJECT,\n\
963 is the string or buffer containing the text.\n\
964 Return t if any property was actually removed, nil otherwise.")
965 (start, end, props, object)
966 Lisp_Object start, end, props, object;
967 {
968 register INTERVAL i, unchanged;
969 register int s, len, modified = 0;
970
971 if (NILP (object))
972 XSET (object, Lisp_Buffer, current_buffer);
973
974 i = validate_interval_range (object, &start, &end, soft);
975 if (NULL_INTERVAL_P (i))
976 return Qnil;
977
978 s = XINT (start);
979 len = XINT (end) - s;
980
981 if (i->position != s)
982 {
983 /* No properties on this first interval -- return if
984 it covers the entire region. */
985 if (! interval_has_some_properties (props, i))
986 {
987 int got = (LENGTH (i) - (s - i->position));
988 if (got >= len)
989 return Qnil;
990 len -= got;
991 i = next_interval (i);
992 }
993 /* Split away the beginning of this interval; what we don't
994 want to modify. */
995 else
996 {
997 unchanged = i;
998 i = split_interval_right (unchanged, s - unchanged->position);
999 copy_properties (unchanged, i);
1000 }
1001 }
1002
1003 /* We are at the beginning of an interval, with len to scan */
1004 for (;;)
1005 {
1006 if (i == 0)
1007 abort ();
1008
1009 if (LENGTH (i) >= len)
1010 {
1011 if (! interval_has_some_properties (props, i))
1012 return modified ? Qt : Qnil;
1013
1014 if (LENGTH (i) == len)
1015 {
1016 remove_properties (props, i, object);
1017 return Qt;
1018 }
1019
1020 /* i has the properties, and goes past the change limit */
1021 unchanged = i;
1022 i = split_interval_left (i, len);
1023 copy_properties (unchanged, i);
1024 remove_properties (props, i, object);
1025 return Qt;
1026 }
1027
1028 len -= LENGTH (i);
1029 modified += remove_properties (props, i, object);
1030 i = next_interval (i);
1031 }
1032 }
1033
1034 DEFUN ("text-property-any", Ftext_property_any,
1035 Stext_property_any, 4, 5, 0,
1036 "Check text from START to END to see if PROP is ever `eq' to VALUE.\n\
1037 If so, return the position of the first character whose PROP is `eq'\n\
1038 to VALUE. Otherwise return nil.\n\
1039 The optional fifth argument, OBJECT, is the string or buffer\n\
1040 containing the text.")
1041 (start, end, prop, value, object)
1042 Lisp_Object start, end, prop, value, object;
1043 {
1044 register INTERVAL i;
1045 register int e, pos;
1046
1047 if (NILP (object))
1048 XSET (object, Lisp_Buffer, current_buffer);
1049 i = validate_interval_range (object, &start, &end, soft);
1050 e = XINT (end);
1051
1052 while (! NULL_INTERVAL_P (i))
1053 {
1054 if (i->position >= e)
1055 break;
1056 if (EQ (textget (i->plist, prop), value))
1057 {
1058 pos = i->position;
1059 if (pos < XINT (start))
1060 pos = XINT (start);
1061 return make_number (pos - (XTYPE (object) == Lisp_String));
1062 }
1063 i = next_interval (i);
1064 }
1065 return Qnil;
1066 }
1067
1068 DEFUN ("text-property-not-all", Ftext_property_not_all,
1069 Stext_property_not_all, 4, 5, 0,
1070 "Check text from START to END to see if PROP is ever not `eq' to VALUE.\n\
1071 If so, return the position of the first character whose PROP is not\n\
1072 `eq' to VALUE. Otherwise, return nil.\n\
1073 The optional fifth argument, OBJECT, is the string or buffer\n\
1074 containing the text.")
1075 (start, end, prop, value, object)
1076 Lisp_Object start, end, prop, value, object;
1077 {
1078 register INTERVAL i;
1079 register int s, e;
1080
1081 if (NILP (object))
1082 XSET (object, Lisp_Buffer, current_buffer);
1083 i = validate_interval_range (object, &start, &end, soft);
1084 if (NULL_INTERVAL_P (i))
1085 return (NILP (value) || EQ (start, end)) ? Qnil : start;
1086 s = XINT (start);
1087 e = XINT (end);
1088
1089 while (! NULL_INTERVAL_P (i))
1090 {
1091 if (i->position >= e)
1092 break;
1093 if (! EQ (textget (i->plist, prop), value))
1094 {
1095 if (i->position > s)
1096 s = i->position;
1097 return make_number (s - (XTYPE (object) == Lisp_String));
1098 }
1099 i = next_interval (i);
1100 }
1101 return Qnil;
1102 }
1103
1104 #if 0 /* You can use set-text-properties for this. */
1105
1106 DEFUN ("erase-text-properties", Ferase_text_properties,
1107 Serase_text_properties, 2, 3, 0,
1108 "Remove all properties from the text from START to END.\n\
1109 The optional third argument, OBJECT,\n\
1110 is the string or buffer containing the text.")
1111 (start, end, object)
1112 Lisp_Object start, end, object;
1113 {
1114 register INTERVAL i;
1115 register INTERVAL prev_changed = NULL_INTERVAL;
1116 register int s, len, modified;
1117
1118 if (NILP (object))
1119 XSET (object, Lisp_Buffer, current_buffer);
1120
1121 i = validate_interval_range (object, &start, &end, soft);
1122 if (NULL_INTERVAL_P (i))
1123 return Qnil;
1124
1125 s = XINT (start);
1126 len = XINT (end) - s;
1127
1128 if (i->position != s)
1129 {
1130 register int got;
1131 register INTERVAL unchanged = i;
1132
1133 /* If there are properties here, then this text will be modified. */
1134 if (! NILP (i->plist))
1135 {
1136 i = split_interval_right (unchanged, s - unchanged->position);
1137 i->plist = Qnil;
1138 modified++;
1139
1140 if (LENGTH (i) > len)
1141 {
1142 i = split_interval_right (i, len);
1143 copy_properties (unchanged, i);
1144 return Qt;
1145 }
1146
1147 if (LENGTH (i) == len)
1148 return Qt;
1149
1150 got = LENGTH (i);
1151 }
1152 /* If the text of I is without any properties, and contains
1153 LEN or more characters, then we may return without changing
1154 anything.*/
1155 else if (LENGTH (i) - (s - i->position) <= len)
1156 return Qnil;
1157 /* The amount of text to change extends past I, so just note
1158 how much we've gotten. */
1159 else
1160 got = LENGTH (i) - (s - i->position);
1161
1162 len -= got;
1163 prev_changed = i;
1164 i = next_interval (i);
1165 }
1166
1167 /* We are starting at the beginning of an interval, I. */
1168 while (len > 0)
1169 {
1170 if (LENGTH (i) >= len)
1171 {
1172 /* If I has no properties, simply merge it if possible. */
1173 if (NILP (i->plist))
1174 {
1175 if (! NULL_INTERVAL_P (prev_changed))
1176 merge_interval_left (i);
1177
1178 return modified ? Qt : Qnil;
1179 }
1180
1181 if (LENGTH (i) > len)
1182 i = split_interval_left (i, len);
1183 if (! NULL_INTERVAL_P (prev_changed))
1184 merge_interval_left (i);
1185 else
1186 i->plist = Qnil;
1187
1188 return Qt;
1189 }
1190
1191 /* Here if we still need to erase past the end of I */
1192 len -= LENGTH (i);
1193 if (NULL_INTERVAL_P (prev_changed))
1194 {
1195 modified += erase_properties (i);
1196 prev_changed = i;
1197 }
1198 else
1199 {
1200 modified += ! NILP (i->plist);
1201 /* Merging I will give it the properties of PREV_CHANGED. */
1202 prev_changed = i = merge_interval_left (i);
1203 }
1204
1205 i = next_interval (i);
1206 }
1207
1208 return modified ? Qt : Qnil;
1209 }
1210 #endif /* 0 */
1211
1212 /* I don't think this is the right interface to export; how often do you
1213 want to do something like this, other than when you're copying objects
1214 around?
1215
1216 I think it would be better to have a pair of functions, one which
1217 returns the text properties of a region as a list of ranges and
1218 plists, and another which applies such a list to another object. */
1219
1220 /* DEFUN ("copy-text-properties", Fcopy_text_properties,
1221 Scopy_text_properties, 5, 6, 0,
1222 "Add properties from SRC-START to SRC-END of SRC at DEST-POS of DEST.\n\
1223 SRC and DEST may each refer to strings or buffers.\n\
1224 Optional sixth argument PROP causes only that property to be copied.\n\
1225 Properties are copied to DEST as if by `add-text-properties'.\n\
1226 Return t if any property value actually changed, nil otherwise.") */
1227
1228 Lisp_Object
1229 copy_text_properties (start, end, src, pos, dest, prop)
1230 Lisp_Object start, end, src, pos, dest, prop;
1231 {
1232 INTERVAL i;
1233 Lisp_Object res;
1234 Lisp_Object stuff;
1235 Lisp_Object plist;
1236 int s, e, e2, p, len, modified = 0;
1237
1238 i = validate_interval_range (src, &start, &end, soft);
1239 if (NULL_INTERVAL_P (i))
1240 return Qnil;
1241
1242 CHECK_NUMBER_COERCE_MARKER (pos, 0);
1243 {
1244 Lisp_Object dest_start, dest_end;
1245
1246 dest_start = pos;
1247 XFASTINT (dest_end) = XINT (dest_start) + (XINT (end) - XINT (start));
1248 /* Apply this to a copy of pos; it will try to increment its arguments,
1249 which we don't want. */
1250 validate_interval_range (dest, &dest_start, &dest_end, soft);
1251 }
1252
1253 s = XINT (start);
1254 e = XINT (end);
1255 p = XINT (pos);
1256
1257 stuff = Qnil;
1258
1259 while (s < e)
1260 {
1261 e2 = i->position + LENGTH (i);
1262 if (e2 > e)
1263 e2 = e;
1264 len = e2 - s;
1265
1266 plist = i->plist;
1267 if (! NILP (prop))
1268 while (! NILP (plist))
1269 {
1270 if (EQ (Fcar (plist), prop))
1271 {
1272 plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
1273 break;
1274 }
1275 plist = Fcdr (Fcdr (plist));
1276 }
1277 if (! NILP (plist))
1278 {
1279 /* Must defer modifications to the interval tree in case src
1280 and dest refer to the same string or buffer. */
1281 stuff = Fcons (Fcons (make_number (p),
1282 Fcons (make_number (p + len),
1283 Fcons (plist, Qnil))),
1284 stuff);
1285 }
1286
1287 i = next_interval (i);
1288 if (NULL_INTERVAL_P (i))
1289 break;
1290
1291 p += len;
1292 s = i->position;
1293 }
1294
1295 while (! NILP (stuff))
1296 {
1297 res = Fcar (stuff);
1298 res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)),
1299 Fcar (Fcdr (Fcdr (res))), dest);
1300 if (! NILP (res))
1301 modified++;
1302 stuff = Fcdr (stuff);
1303 }
1304
1305 return modified ? Qt : Qnil;
1306 }
1307
1308 void
1309 syms_of_textprop ()
1310 {
1311 DEFVAR_INT ("interval-balance-threshold", &interval_balance_threshold,
1312 "Threshold for rebalancing interval trees, expressed as the\n\
1313 percentage by which the left interval tree should not differ from the right.");
1314 interval_balance_threshold = 8;
1315
1316 DEFVAR_LISP ("inhibit-point-motion-hooks", &Vinhibit_point_motion_hooks,
1317 "If non-nil, don't call the text property values of\n\
1318 `point-left' and `point-entered'.");
1319 Vinhibit_point_motion_hooks = Qnil;
1320
1321 /* Common attributes one might give text */
1322
1323 staticpro (&Qforeground);
1324 Qforeground = intern ("foreground");
1325 staticpro (&Qbackground);
1326 Qbackground = intern ("background");
1327 staticpro (&Qfont);
1328 Qfont = intern ("font");
1329 staticpro (&Qstipple);
1330 Qstipple = intern ("stipple");
1331 staticpro (&Qunderline);
1332 Qunderline = intern ("underline");
1333 staticpro (&Qread_only);
1334 Qread_only = intern ("read-only");
1335 staticpro (&Qinvisible);
1336 Qinvisible = intern ("invisible");
1337 staticpro (&Qintangible);
1338 Qintangible = intern ("intangible");
1339 staticpro (&Qcategory);
1340 Qcategory = intern ("category");
1341 staticpro (&Qlocal_map);
1342 Qlocal_map = intern ("local-map");
1343 staticpro (&Qfront_sticky);
1344 Qfront_sticky = intern ("front-sticky");
1345 staticpro (&Qrear_nonsticky);
1346 Qrear_nonsticky = intern ("rear-nonsticky");
1347
1348 /* Properties that text might use to specify certain actions */
1349
1350 staticpro (&Qmouse_left);
1351 Qmouse_left = intern ("mouse-left");
1352 staticpro (&Qmouse_entered);
1353 Qmouse_entered = intern ("mouse-entered");
1354 staticpro (&Qpoint_left);
1355 Qpoint_left = intern ("point-left");
1356 staticpro (&Qpoint_entered);
1357 Qpoint_entered = intern ("point-entered");
1358
1359 defsubr (&Stext_properties_at);
1360 defsubr (&Sget_text_property);
1361 defsubr (&Snext_property_change);
1362 defsubr (&Snext_single_property_change);
1363 defsubr (&Sprevious_property_change);
1364 defsubr (&Sprevious_single_property_change);
1365 defsubr (&Sadd_text_properties);
1366 defsubr (&Sput_text_property);
1367 defsubr (&Sset_text_properties);
1368 defsubr (&Sremove_text_properties);
1369 defsubr (&Stext_property_any);
1370 defsubr (&Stext_property_not_all);
1371 /* defsubr (&Serase_text_properties); */
1372 /* defsubr (&Scopy_text_properties); */
1373 }
1374
1375 #else
1376
1377 lose -- this shouldn't be compiled if USE_TEXT_PROPERTIES isn't defined
1378
1379 #endif /* USE_TEXT_PROPERTIES */