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