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