textprop.c 51.2 KB
Newer Older
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1
/* Interface code for dealing with text properties.
2
   Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
3 4 5 6 7

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Jim Blandy's avatar
Jim Blandy committed
8
the Free Software Foundation; either version 2, or (at your option)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
9 10 11 12 13 14 15 16 17
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
18 19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
20

21
#include <config.h>
Joseph Arceneaux's avatar
Joseph Arceneaux committed
22 23 24
#include "lisp.h"
#include "intervals.h"
#include "buffer.h"
25
#include "window.h"
26 27 28 29

#ifndef NULL
#define NULL (void *)0
#endif
30 31 32 33 34

/* Test for membership, allowing for t (actually any non-cons) to mean the
   universal set.  */

#define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
35 36 37 38 39 40


/* NOTES:  previous- and next- property change will have to skip
  zero-length intervals if they are implemented.  This could be done
  inside next_interval and previous_interval.

Joseph Arceneaux's avatar
Joseph Arceneaux committed
41 42
  set_properties needs to deal with the interval property cache.

Joseph Arceneaux's avatar
Joseph Arceneaux committed
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,
Joseph Arceneaux's avatar
Joseph Arceneaux committed
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.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
48

49 50
/* The rest of the file is within this conditional */
#ifdef USE_TEXT_PROPERTIES
Joseph Arceneaux's avatar
Joseph Arceneaux committed
51

52
/* Types of hooks.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
53 54 55 56
Lisp_Object Qmouse_left;
Lisp_Object Qmouse_entered;
Lisp_Object Qpoint_left;
Lisp_Object Qpoint_entered;
57 58
Lisp_Object Qcategory;
Lisp_Object Qlocal_map;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
59

60
/* Visual properties text (including strings) may have.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
61
Lisp_Object Qforeground, Qbackground, Qfont, Qunderline, Qstipple;
62
Lisp_Object Qinvisible, Qread_only, Qintangible, Qmouse_face;
63 64 65

/* Sticky properties */
Lisp_Object Qfront_sticky, Qrear_nonsticky;
66 67 68 69

/* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to
   the o1's cdr.  Otherwise, return zero.  This is handy for
   traversing plists.  */
70
#define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCDR (o1), CONSP (o2)))
71

72
Lisp_Object Vinhibit_point_motion_hooks;
73
Lisp_Object Vdefault_text_properties;
74

75 76 77 78
/* verify_interval_modification saves insertion hooks here
   to be run later by report_interval_modification.  */
Lisp_Object interval_insert_behind_hooks;
Lisp_Object interval_insert_in_front_hooks;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
79

Joseph Arceneaux's avatar
Joseph Arceneaux committed
80 81 82 83 84 85
/* Extract the interval at the position pointed to by BEGIN from
   OBJECT, a string or buffer.  Additionally, check that the positions
   pointed to by BEGIN and END are within the bounds of OBJECT, and
   reverse them if *BEGIN is greater than *END.  The objects pointed
   to by BEGIN and END may be integers or markers; if the latter, they
   are coerced to integers.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
86

87 88 89
   When OBJECT is a string, we increment *BEGIN and *END
   to make them origin-one.

Joseph Arceneaux's avatar
Joseph Arceneaux committed
90 91 92 93 94 95 96 97 98
   Note that buffer points don't correspond to interval indices.
   For example, point-max is 1 greater than the index of the last
   character.  This difference is handled in the caller, which uses
   the validated points to determine a length, and operates on that.
   Exceptions are Ftext_properties_at, Fnext_property_change, and
   Fprevious_property_change which call this function with BEGIN == END.
   Handle this case specially.

   If FORCE is soft (0), it's OK to return NULL_INTERVAL.  Otherwise,
Joseph Arceneaux's avatar
Joseph Arceneaux committed
99 100
   create an interval tree for OBJECT if one doesn't exist, provided
   the object actually contains text.  In the current design, if there
101
   is no text, there can be no text properties.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
102 103 104 105

#define soft 0
#define hard 1

Gerd Moellmann's avatar
Gerd Moellmann committed
106
INTERVAL
Joseph Arceneaux's avatar
Joseph Arceneaux committed
107 108 109 110 111
validate_interval_range (object, begin, end, force)
     Lisp_Object object, *begin, *end;
     int force;
{
  register INTERVAL i;
112 113
  int searchpos;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
114 115 116 117 118
  CHECK_STRING_OR_BUFFER (object, 0);
  CHECK_NUMBER_COERCE_MARKER (*begin, 0);
  CHECK_NUMBER_COERCE_MARKER (*end, 0);

  /* 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)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
121 122 123 124
    return NULL_INTERVAL;

  if (XINT (*begin) > XINT (*end))
    {
125 126
      Lisp_Object n;
      n = *begin;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
127
      *begin = *end;
128
      *end = n;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
129 130
    }

131
  if (BUFFERP (object))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
132 133 134 135 136 137
    {
      register struct buffer *b = XBUFFER (object);

      if (!(BUF_BEGV (b) <= XINT (*begin) && XINT (*begin) <= XINT (*end)
	    && XINT (*end) <= BUF_ZV (b)))
	args_out_of_range (*begin, *end);
138
      i = BUF_INTERVALS (b);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
139

140
      /* If there's no text, there are no properties.  */
141 142 143 144
      if (BUF_BEGV (b) == BUF_ZV (b))
	return NULL_INTERVAL;

      searchpos = XINT (*begin);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
145 146 147 148 149
    }
  else
    {
      register struct Lisp_String *s = XSTRING (object);

150
      if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
151 152
	     && XINT (*end) <= s->size))
	args_out_of_range (*begin, *end);
153
      XSETFASTINT (*begin, XFASTINT (*begin));
154
      if (begin != end)
155
	XSETFASTINT (*end, XFASTINT (*end));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
156
      i = s->intervals;
157 158 159 160 161

      if (s->size == 0)
	return NULL_INTERVAL;

      searchpos = XINT (*begin);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
162 163 164 165 166
    }

  if (NULL_INTERVAL_P (i))
    return (force ? create_root_interval (object) : i);
    
167
  return find_interval (i, searchpos);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
168 169 170 171
}

/* Validate LIST as a property list.  If LIST is not a list, then
   make one consisting of (LIST nil).  Otherwise, verify that LIST
172
   is even numbered and thus suitable as a plist.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
173 174 175

static Lisp_Object
validate_plist (list)
176
     Lisp_Object list;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
177 178 179 180 181 182 183 184 185
{
  if (NILP (list))
    return Qnil;

  if (CONSP (list))
    {
      register int i;
      register Lisp_Object tail;
      for (i = 0, tail = list; !NILP (tail); i++)
186 187 188 189
	{
	  tail = Fcdr (tail);
	  QUIT;
	}
Joseph Arceneaux's avatar
Joseph Arceneaux committed
190 191 192 193 194 195 196 197 198
      if (i & 1)
	error ("Odd length text property list");
      return list;
    }

  return Fcons (list, Fcons (Qnil, Qnil));
}

/* Return nonzero if interval I has all the properties,
199
   with the same values, of list PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
200 201 202 203 204 205

static int
interval_has_all_properties (plist, i)
     Lisp_Object plist;
     INTERVAL i;
{
206
  register Lisp_Object tail1, tail2, sym1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
207 208
  register int found;

209
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
210 211 212 213 214 215 216 217 218 219
  for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
    {
      sym1 = Fcar (tail1);
      found = 0;

      /* Go through I's plist, looking for sym1 */
      for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
	if (EQ (sym1, Fcar (tail2)))
	  {
	    /* Found the same property on both lists.  If the
220
	       values are unequal, return zero.  */
221
	    if (! EQ (Fcar (Fcdr (tail1)), Fcar (Fcdr (tail2))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
222 223
	      return 0;

224
	    /* Property has same value on both lists;  go to next one.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
225 226 227 228 229 230 231 232 233 234 235 236
	    found = 1;
	    break;
	  }

      if (! found)
	return 0;
    }

  return 1;
}

/* Return nonzero if the plist of interval I has any of the
237
   properties of PLIST, regardless of their values.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
238 239 240 241 242 243 244 245

static INLINE int
interval_has_some_properties (plist, i)
     Lisp_Object plist;
     INTERVAL i;
{
  register Lisp_Object tail1, tail2, sym;

246
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
247 248 249 250 251 252 253 254 255 256 257 258
  for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
    {
      sym = Fcar (tail1);

      /* Go through i's plist, looking for tail1 */
      for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
	if (EQ (sym, Fcar (tail2)))
	  return 1;
    }

  return 0;
}
259

260 261 262 263
/* Changing the plists of individual intervals.  */

/* Return the value of PROP in property-list PLIST, or Qunbound if it
   has none.  */
264
static Lisp_Object
265
property_value (plist, prop)
266
     Lisp_Object plist, prop;
267 268 269 270
{
  Lisp_Object value;

  while (PLIST_ELT_P (plist, value))
271 272
    if (EQ (XCAR (plist), prop))
      return XCAR (value);
273
    else
274
      plist = XCDR (value);
275 276 277 278

  return Qunbound;
}

279 280 281 282 283 284 285 286 287
/* Set the properties of INTERVAL to PROPERTIES,
   and record undo info for the previous values.
   OBJECT is the string or buffer that INTERVAL belongs to.  */

static void
set_properties (properties, interval, object)
     Lisp_Object properties, object;
     INTERVAL interval;
{
288
  Lisp_Object sym, value;
289

290
  if (BUFFERP (object))
291
    {
292 293 294 295
      /* For each property in the old plist which is missing from PROPERTIES,
	 or has a different value in PROPERTIES, make an undo record.  */
      for (sym = interval->plist;
	   PLIST_ELT_P (sym, value);
296 297 298
	   sym = XCDR (value))
	if (! EQ (property_value (properties, XCAR (sym)),
		  XCAR (value)))
299 300
	  {
	    record_property_change (interval->position, LENGTH (interval),
301
				    XCAR (sym), XCAR (value),
302 303
				    object);
	  }
304 305 306 307 308

      /* For each new property that has no value at all in the old plist,
	 make an undo record binding it to nil, so it will be removed.  */
      for (sym = properties;
	   PLIST_ELT_P (sym, value);
309 310
	   sym = XCDR (value))
	if (EQ (property_value (interval->plist, XCAR (sym)), Qunbound))
311 312
	  {
	    record_property_change (interval->position, LENGTH (interval),
313
				    XCAR (sym), Qnil,
314 315
				    object);
	  }
316 317 318 319 320
    }

  /* Store new properties.  */
  interval->plist = Fcopy_sequence (properties);
}
Joseph Arceneaux's avatar
Joseph Arceneaux committed
321 322 323 324 325

/* Add the properties of PLIST to the interval I, or set
   the value of I's property to the value of the property on PLIST
   if they are different.

326 327
   OBJECT should be the string or buffer the interval is in.

Joseph Arceneaux's avatar
Joseph Arceneaux committed
328 329 330
   Return nonzero if this changes I (i.e., if any members of PLIST
   are actually added to I's plist) */

331 332
static int
add_properties (plist, i, object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
333 334
     Lisp_Object plist;
     INTERVAL i;
335
     Lisp_Object object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
336
{
337
  Lisp_Object tail1, tail2, sym1, val1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
338 339
  register int changed = 0;
  register int found;
340 341 342 343 344 345 346 347 348
  struct gcpro gcpro1, gcpro2, gcpro3;

  tail1 = plist;
  sym1 = Qnil;
  val1 = Qnil;
  /* No need to protect OBJECT, because we can GC only in the case
     where it is a buffer, and live buffers are always protected.
     I and its plist are also protected, via OBJECT.  */
  GCPRO3 (tail1, sym1, val1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
349

350
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
351 352 353 354 355 356 357 358 359 360
  for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
    {
      sym1 = Fcar (tail1);
      val1 = Fcar (Fcdr (tail1));
      found = 0;

      /* Go through I's plist, looking for sym1 */
      for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
	if (EQ (sym1, Fcar (tail2)))
	  {
361 362
	    /* No need to gcpro, because tail2 protects this
	       and it must be a cons cell (we get an error otherwise).  */
363
	    register Lisp_Object this_cdr;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
364

365
	    this_cdr = Fcdr (tail2);
366
	    /* Found the property.  Now check its value.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
367 368 369
	    found = 1;

	    /* The properties have the same value on both lists.
370
	       Continue to the next property.  */
371
	    if (EQ (val1, Fcar (this_cdr)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
372 373
	      break;

374
	    /* Record this change in the buffer, for undo purposes.  */
375
	    if (BUFFERP (object))
376
	      {
377 378
		record_property_change (i->position, LENGTH (i),
					sym1, Fcar (this_cdr), object);
379 380
	      }

Joseph Arceneaux's avatar
Joseph Arceneaux committed
381 382 383 384 385 386 387 388
	    /* I's property has a different value -- change it */
	    Fsetcar (this_cdr, val1);
	    changed++;
	    break;
	  }

      if (! found)
	{
389
	  /* Record this change in the buffer, for undo purposes.  */
390
	  if (BUFFERP (object))
391
	    {
392 393
	      record_property_change (i->position, LENGTH (i),
				      sym1, Qnil, object);
394
	    }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
395 396 397 398 399
	  i->plist = Fcons (sym1, Fcons (val1, i->plist));
	  changed++;
	}
    }

400 401
  UNGCPRO;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
402 403 404 405
  return changed;
}

/* For any members of PLIST which are properties of I, remove them
406 407
   from I's plist.
   OBJECT is the string or buffer containing I.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
408

409 410
static int
remove_properties (plist, i, object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
411 412
     Lisp_Object plist;
     INTERVAL i;
413
     Lisp_Object object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
414
{
415
  register Lisp_Object tail1, tail2, sym, current_plist;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
416 417
  register int changed = 0;

418
  current_plist = i->plist;
419
  /* Go through each element of plist.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
420 421 422 423 424 425 426
  for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
    {
      sym = Fcar (tail1);

      /* First, remove the symbol if its at the head of the list */
      while (! NILP (current_plist) && EQ (sym, Fcar (current_plist)))
	{
427
	  if (BUFFERP (object))
428
	    {
429 430 431
	      record_property_change (i->position, LENGTH (i),
				      sym, Fcar (Fcdr (current_plist)),
				      object);
432 433
	    }

Joseph Arceneaux's avatar
Joseph Arceneaux committed
434 435 436 437 438 439 440 441
	  current_plist = Fcdr (Fcdr (current_plist));
	  changed++;
	}

      /* Go through i's plist, looking for sym */
      tail2 = current_plist;
      while (! NILP (tail2))
	{
442 443
	  register Lisp_Object this;
	  this = Fcdr (Fcdr (tail2));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
444 445
	  if (EQ (sym, Fcar (this)))
	    {
446
	      if (BUFFERP (object))
447
		{
448 449
		  record_property_change (i->position, LENGTH (i),
					  sym, Fcar (Fcdr (this)), object);
450 451
		}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
452 453 454 455 456 457 458 459 460 461 462 463
	      Fsetcdr (Fcdr (tail2), Fcdr (Fcdr (this)));
	      changed++;
	    }
	  tail2 = this;
	}
    }

  if (changed)
    i->plist = current_plist;
  return changed;
}

464
#if 0
Joseph Arceneaux's avatar
Joseph Arceneaux committed
465
/* Remove all properties from interval I.  Return non-zero
466
   if this changes the interval.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
467 468 469 470 471 472 473 474 475 476 477

static INLINE int
erase_properties (i)
     INTERVAL i;
{
  if (NILP (i->plist))
    return 0;

  i->plist = Qnil;
  return 1;
}
478
#endif
Joseph Arceneaux's avatar
Joseph Arceneaux committed
479

480
/* Returns the interval of POSITION in OBJECT. 
481 482 483 484 485 486 487 488 489 490 491 492
   POSITION is BEG-based.  */

INTERVAL
interval_of (position, object)
     int position;
     Lisp_Object object;
{
  register INTERVAL i;
  int beg, end;

  if (NILP (object))
    XSETBUFFER (object, current_buffer);
Karl Heuer's avatar
Karl Heuer committed
493 494
  else if (EQ (object, Qt))
    return NULL_INTERVAL;
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

  CHECK_STRING_OR_BUFFER (object, 0);

  if (BUFFERP (object))
    {
      register struct buffer *b = XBUFFER (object);

      beg = BUF_BEGV (b);
      end = BUF_ZV (b);
      i = BUF_INTERVALS (b);
    }
  else
    {
      register struct Lisp_String *s = XSTRING (object);

510 511
      beg = 0;
      end = s->size;
512 513 514 515
      i = s->intervals;
    }

  if (!(beg <= position && position <= end))
516
    args_out_of_range (make_number (position), make_number (position));
517 518 519 520 521 522
  if (beg == end || NULL_INTERVAL_P (i))
    return NULL_INTERVAL;
    
  return find_interval (i, position);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
523 524
DEFUN ("text-properties-at", Ftext_properties_at,
       Stext_properties_at, 1, 2, 0,
525 526 527
  "Return the list of properties of the character at POSITION in OBJECT.\n\
OBJECT is the string or buffer to look for the properties in;\n\
nil means the current buffer.\n\
528
If POSITION is at the end of OBJECT, the value is nil.")
529 530
  (position, object)
     Lisp_Object position, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
531 532 533 534
{
  register INTERVAL i;

  if (NILP (object))
535
    XSETBUFFER (object, current_buffer);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
536

537
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
538 539
  if (NULL_INTERVAL_P (i))
    return Qnil;
540
  /* If POSITION is at the end of the interval,
541 542 543
     it means it's the end of OBJECT.
     There are no properties at the very end,
     since no character follows.  */
544
  if (XINT (position) == LENGTH (i) + i->position)
545
    return Qnil;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
546 547 548 549

  return i->plist;
}

550
DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
551
  "Return the value of POSITION's property PROP, in OBJECT.\n\
552 553
OBJECT is optional and defaults to the current buffer.\n\
If POSITION is at the end of OBJECT, the value is nil.")
554 555
  (position, prop, object)
     Lisp_Object position, object;
556
     Lisp_Object prop;
557
{
558
  return textget (Ftext_properties_at (position, object), prop);
559 560
}

561
DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
562
  "Return the value of POSITION's property PROP, in OBJECT.\n\
563
OBJECT is optional and defaults to the current buffer.\n\
564
If POSITION is at the end of OBJECT, the value is nil.\n\
565
If OBJECT is a buffer, then overlay properties are considered as well as\n\
566 567
text properties.\n\
If OBJECT is a window, then that window's buffer is used, but window-specific\n\
568
overlays are considered only if they are associated with OBJECT.")
569 570
  (position, prop, object)
     Lisp_Object position, object;
571 572 573 574
     register Lisp_Object prop;
{
  struct window *w = 0;

575
  CHECK_NUMBER_COERCE_MARKER (position, 0);
576 577

  if (NILP (object))
578
    XSETBUFFER (object, current_buffer);
579 580 581 582

  if (WINDOWP (object))
    {
      w = XWINDOW (object);
583
      object = w->buffer;
584 585 586
    }
  if (BUFFERP (object))
    {
587
      int posn = XINT (position);
588 589 590 591
      int noverlays;
      Lisp_Object *overlay_vec, tem;
      int next_overlay;
      int len;
592 593 594
      struct buffer *obuf = current_buffer;

      set_buffer_temp (XBUFFER (object));
595 596 597 598 599

      /* First try with room for 40 overlays.  */
      len = 40;
      overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));

600 601
      noverlays = overlays_at (posn, 0, &overlay_vec, &len,
			       &next_overlay, NULL);
602 603 604 605 606 607 608

      /* If there are more than 40,
	 make enough space for all, and try again.  */
      if (noverlays > len)
	{
	  len = noverlays;
	  overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
609 610
	  noverlays = overlays_at (posn, 0, &overlay_vec, &len,
				   &next_overlay, NULL);
611 612 613
	}
      noverlays = sort_overlays (overlay_vec, noverlays, w);

614 615
      set_buffer_temp (obuf);

616 617 618 619 620 621 622 623 624 625
      /* Now check the overlays in order of decreasing priority.  */
      while (--noverlays >= 0)
	{
	  tem = Foverlay_get (overlay_vec[noverlays], prop);
	  if (!NILP (tem))
	    return (tem);
	}
    }
  /* Not a buffer, or no appropriate overlay, so fall through to the
     simpler case.  */
626
  return (Fget_text_property (position, prop, object));
627
}
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667

DEFUN ("next-char-property-change", Fnext_char_property_change,
       Snext_char_property_change, 1, 2, 0,
  "Return the position of next text property or overlay change.\n\
This scans characters forward from POSITION in OBJECT till it finds\n\
a change in some text property, or the beginning or end of an overlay,\n\
and returns the position of that.\n\
If none is found, the function returns (point-max).\n\
\n\
If the optional third argument LIMIT is non-nil, don't search\n\
past position LIMIT; return LIMIT if nothing is found before LIMIT.")
  (position, limit)
     Lisp_Object position, limit;
{
  Lisp_Object temp;

  temp = Fnext_overlay_change (position);
  if (! NILP (limit))
    {
      CHECK_NUMBER (limit, 2);
      if (XINT (limit) < XINT (temp))
	temp = limit;
    }
  return Fnext_property_change (position, Qnil, temp);
}

DEFUN ("previous-char-property-change", Fprevious_char_property_change,
       Sprevious_char_property_change, 1, 2, 0,
  "Return the position of previous text property or overlay change.\n\
Scans characters backward from POSITION in OBJECT till it finds\n\
a change in some text property, or the beginning or end of an overlay,\n\
and returns the position of that.\n\
If none is found, the function returns (point-max).\n\
\n\
If the optional third argument LIMIT is non-nil, don't search\n\
past position LIMIT; return LIMIT if nothing is found before LIMIT.")
  (position, limit)
     Lisp_Object position, limit;
{
  Lisp_Object temp;
668

669 670 671 672 673 674 675 676 677 678
  temp = Fprevious_overlay_change (position);
  if (! NILP (limit))
    {
      CHECK_NUMBER (limit, 2);
      if (XINT (limit) > XINT (temp))
	temp = limit;
    }
  return Fprevious_property_change (position, Qnil, temp);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
679
DEFUN ("next-property-change", Fnext_property_change,
680
       Snext_property_change, 1, 3, 0,
681
  "Return the position of next property change.\n\
682
Scans characters forward from POSITION in OBJECT till it finds\n\
683 684 685
a change in some text property, then returns the position of the change.\n\
The optional second argument OBJECT is the string or buffer to scan.\n\
Return nil if the property is constant all the way to the end of OBJECT.\n\
686
If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\
687 688
If the optional third argument LIMIT is non-nil, don't search\n\
past position LIMIT; return LIMIT if nothing is found before LIMIT.")
689 690
  (position, object, limit)
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
691 692 693
{
  register INTERVAL i, next;

694
  if (NILP (object))
695
    XSETBUFFER (object, current_buffer);
696

697
  if (! NILP (limit) && ! EQ (limit, Qt))
698 699
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

700
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
701

702 703 704 705
  /* If LIMIT is t, return start of next interval--don't
     bother checking further intervals.  */
  if (EQ (limit, Qt))
    {
706 707 708 709 710
      if (NULL_INTERVAL_P (i))
	next = i;
      else
	next = next_interval (i);
	
711
      if (NULL_INTERVAL_P (next))
712 713 714
	XSETFASTINT (position, (STRINGP (object)
				? XSTRING (object)->size
				: BUF_ZV (XBUFFER (object))));
715
      else
716
	XSETFASTINT (position, next->position);
717
      return position;
718 719
    }

720 721 722 723 724
  if (NULL_INTERVAL_P (i))
    return limit;

  next = next_interval (i);

725
  while (! NULL_INTERVAL_P (next) && intervals_equal (i, next)
726
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
727 728 729
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
730
    return limit;
731
  if (! NILP (limit) && !(next->position < XFASTINT (limit)))
732
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
733

734
  XSETFASTINT (position, next->position);
735
  return position;
736 737 738 739 740 741 742 743 744 745 746
}

/* Return 1 if there's a change in some property between BEG and END.  */

int
property_change_between_p (beg, end)
     int beg, end;
{
  register INTERVAL i, next;
  Lisp_Object object, pos;

747
  XSETBUFFER (object, current_buffer);
748
  XSETFASTINT (pos, beg);
749 750 751 752 753 754 755 756 757

  i = validate_interval_range (object, &pos, &pos, soft);
  if (NULL_INTERVAL_P (i))
    return 0;

  next = next_interval (i);
  while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
    {
      next = next_interval (next);
758 759
      if (NULL_INTERVAL_P (next))
	return 0;
760
      if (next->position >= end)
761 762 763 764 765 766 767
	return 0;
    }

  if (NULL_INTERVAL_P (next))
    return 0;

  return 1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
768 769
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
770
DEFUN ("next-single-property-change", Fnext_single_property_change,
771
       Snext_single_property_change, 2, 4, 0,
772
  "Return the position of next property change for a specific property.\n\
773
Scans characters forward from POSITION till it finds\n\
774 775
a change in the PROP property, then returns the position of the change.\n\
The optional third argument OBJECT is the string or buffer to scan.\n\
776
The property values are compared with `eq'.\n\
777
Return nil if the property is constant all the way to the end of OBJECT.\n\
778
If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\
779
If the optional fourth argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
780
past position LIMIT; return LIMIT if nothing is found before LIMIT.")
781 782
  (position, prop, object, limit)
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
783 784 785 786
{
  register INTERVAL i, next;
  register Lisp_Object here_val;

787
  if (NILP (object))
788
    XSETBUFFER (object, current_buffer);
789

790 791 792
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

793
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
794
  if (NULL_INTERVAL_P (i))
795
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
796

797
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
798
  next = next_interval (i);
799
  while (! NULL_INTERVAL_P (next) 
800
	 && EQ (here_val, textget (next->plist, prop))
801
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
802 803 804
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
805
    return limit;
806
  if (! NILP (limit) && !(next->position < XFASTINT (limit)))
807
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
808

809
  return make_number (next->position);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
810 811
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
812
DEFUN ("previous-property-change", Fprevious_property_change,
813
       Sprevious_property_change, 1, 3, 0,
814
  "Return the position of previous property change.\n\
815
Scans characters backwards from POSITION in OBJECT till it finds\n\
816 817 818
a change in some text property, then returns the position of the change.\n\
The optional second argument OBJECT is the string or buffer to scan.\n\
Return nil if the property is constant all the way to the start of OBJECT.\n\
819
If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
820
If the optional third argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
821
back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
822 823
  (position, object, limit)
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
824 825 826
{
  register INTERVAL i, previous;

827
  if (NILP (object))
828
    XSETBUFFER (object, current_buffer);
829

830 831 832
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

833
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
834
  if (NULL_INTERVAL_P (i))
835
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
836

837
  /* Start with the interval containing the char before point.  */
838
  if (i->position == XFASTINT (position))
839 840
    i = previous_interval (i);

Joseph Arceneaux's avatar
Joseph Arceneaux committed
841
  previous = previous_interval (i);
842 843
  while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i)
	 && (NILP (limit)
844
	     || (previous->position + LENGTH (previous) > XFASTINT (limit))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
845 846
    previous = previous_interval (previous);
  if (NULL_INTERVAL_P (previous))
847 848
    return limit;
  if (!NILP (limit)
849
      && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
850
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
851

852
  return make_number (previous->position + LENGTH (previous));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
853 854
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
855
DEFUN ("previous-single-property-change", Fprevious_single_property_change,
856
       Sprevious_single_property_change, 2, 4, 0,
857
  "Return the position of previous property change for a specific property.\n\
858
Scans characters backward from POSITION till it finds\n\
859 860
a change in the PROP property, then returns the position of the change.\n\
The optional third argument OBJECT is the string or buffer to scan.\n\
861
The property values are compared with `eq'.\n\
862
Return nil if the property is constant all the way to the start of OBJECT.\n\
863
If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
864
If the optional fourth argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
865
back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
866 867
     (position, prop, object, limit)
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
868 869 870 871
{
  register INTERVAL i, previous;
  register Lisp_Object here_val;

872
  if (NILP (object))
873
    XSETBUFFER (object, current_buffer);
874

875 876 877
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

878
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
879

880
  /* Start with the interval containing the char before point.  */
881
  if (! NULL_INTERVAL_P (i) && i->position == XFASTINT (position))
882 883
    i = previous_interval (i);

884 885 886
  if (NULL_INTERVAL_P (i))
    return limit;

887
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
888 889
  previous = previous_interval (i);
  while (! NULL_INTERVAL_P (previous)
890 891
	 && EQ (here_val, textget (previous->plist, prop))
	 && (NILP (limit)
892
	     || (previous->position + LENGTH (previous) > XFASTINT (limit))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
893 894
    previous = previous_interval (previous);
  if (NULL_INTERVAL_P (previous))
895 896
    return limit;
  if (!NILP (limit)
897
      && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
898
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
899

900
  return make_number (previous->position + LENGTH (previous));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
901
}
902

903 904
/* Callers note, this can GC when OBJECT is a buffer (or nil).  */

Joseph Arceneaux's avatar
Joseph Arceneaux committed
905
DEFUN ("add-text-properties", Fadd_text_properties,
906 907
       Sadd_text_properties, 3, 4, 0,
  "Add properties to the text from START to END.\n\
908
The third argument PROPERTIES is a property list\n\
909 910 911 912 913 914
specifying the property values to add.\n\
The optional fourth argument, OBJECT,\n\
is the string or buffer containing the text.\n\
Return t if any property value actually changed, nil otherwise.")
  (start, end, properties, object)
     Lisp_Object start, end, properties, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
915 916
{
  register INTERVAL i, unchanged;
917
  register int s, len, modified = 0;
918
  struct gcpro gcpro1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
919 920 921 922 923

  properties = validate_plist (properties);
  if (NILP (properties))
    return Qnil;

924
  if (NILP (object))
925
    XSETBUFFER (object, current_buffer);
926

Joseph Arceneaux's avatar
Joseph Arceneaux committed
927 928 929 930 931 932 933
  i = validate_interval_range (object, &start, &end, hard);
  if (NULL_INTERVAL_P (i))
    return Qnil;

  s = XINT (start);
  len = XINT (end) - s;

934 935 936 937
  /* No need to protect OBJECT, because we GC only if it's a buffer,
     and live buffers are always protected.  */
  GCPRO1 (properties);

Joseph Arceneaux's avatar
Joseph Arceneaux committed
938
  /* If we're not starting on an interval boundary, we have to
939
    split this interval.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
940 941 942
  if (i->position != s)
    {
      /* If this interval already has the properties, we can
943
         skip it.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
944 945 946 947
      if (interval_has_all_properties (properties, i))
	{
	  int got = (LENGTH (i) - (s - i->position));
	  if (got >= len)
948
	    RETURN_UNGCPRO (Qnil);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
949
	  len -= got;
950
	  i = next_interval (i);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
951 952 953 954
	}
      else
	{
	  unchanged = i;
955
	  i = split_interval_right (unchanged, s - unchanged->position);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
956 957 958 959
	  copy_properties (unchanged, i);
	}
    }

960 961
  if (BUFFERP (object))
    modify_region (XBUFFER (object), XINT (start), XINT (end));
962

963
  /* We are at the beginning of interval I, with LEN chars to scan.  */
964
  for (;;)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
965
    {
966 967 968
      if (i == 0)
	abort ();

Joseph Arceneaux's avatar
Joseph Arceneaux committed
969 970
      if (LENGTH (i) >= len)
	{
971 972 973 974 975
	  /* We can UNGCPRO safely here, because there will be just
	     one more chance to gc, in the next call to add_properties,
	     and after that we will not need PROPERTIES or OBJECT again.  */
	  UNGCPRO;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
976
	  if (interval_has_all_properties (properties, i))
977
	    {
978 979 980
	      if (BUFFERP (object))
		signal_after_change (XINT (start), XINT (end) - XINT (start),
				     XINT (end) - XINT (start));
981 982 983

	      return modified ? Qt : Qnil;
	    }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
984 985 986

	  if (LENGTH (i) == len)
	    {
987
	      add_properties (properties, i, object);
988 989 990
	      if (BUFFERP (object))
		signal_after_change (XINT (start), XINT (end) - XINT (start),
				     XINT (end) - XINT (start));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
991 992 993 994 995
	      return Qt;
	    }

	  /* i doesn't have the properties, and goes past the change limit */
	  unchanged = i;
996
	  i = split_interval_left (unchanged, len);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
997
	  copy_properties (unchanged, i);
998
	  add_properties (properties, i, object);
999 1000 1001
	  if (BUFFERP (object))
	    signal_after_change (XINT (start), XINT (end) - XINT (start),
				 XINT (end) - XINT (start));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1002 1003 1004 1005
	  return Qt;
	}

      len -= LENGTH (i);
1006
      modified += add_properties (properties, i, object);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1007 1008 1009 1010
      i = next_interval (i);
    }
}

1011 1012
/* Callers note, this can GC when OBJECT is a buffer (or nil).  */

1013 1014 1015
DEFUN ("put-text-property", Fput_text_property,
       Sput_text_property, 4, 5, 0,
  "Set one property of the text from START to END.\n\
1016
The third and fourth arguments PROPERTY and VALUE\n\
1017 1018 1019
specify the property to add.\n\
The optional fifth argument, OBJECT,\n\
is the string or buffer containing the text.")
1020 1021
  (start, end, property, value, object)
     Lisp_Object start, end, property, value, object;
1022 1023
{
  Fadd_text_properties (start, end,
1024
			Fcons (property, Fcons (value, Qnil)),
1025 1026 1027 1028
			object);
  return Qnil;
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1029
DEFUN ("set-text-properties", Fset_text_properties,
1030 1031
       Sset_text_properties, 3, 4, 0,
  "Completely replace properties of text from START to END.\n\
1032
The third argument PROPERTIES is the new property list.\n\
1033 1034
The optional fourth argument, OBJECT,\n\
is the string or buffer containing the text.")
1035 1036
  (start, end, properties, object)
     Lisp_Object start, end, properties, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1037 1038
{
  register INTERVAL i, unchanged;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1039
  register INTERVAL prev_changed = NULL_INTERVAL;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1040
  register int s, len;
1041 1042 1043 1044
  Lisp_Object ostart, oend;

  ostart = start;
  oend = end;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1045

1046
  properties = validate_plist (properties);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1047

1048
  if (NILP (object))
1049
    XSETBUFFER (object, current_buffer);
1050

1051 1052
  /* If we want no properties for a whole string,
     get rid of its intervals.  */
1053
  if (NILP (properties) && STRINGP (object)
1054 1055 1056
      && XFASTINT (start) == 0
      && XFASTINT (end) == XSTRING (object)->size)
    {
1057 1058 1059
      if (! XSTRING (object)->intervals)
	return Qt;

1060 1061 1062 1063
      XSTRING (object)->intervals = 0;
      return Qt;
    }

Richard M. Stallman's avatar
Richard M. Stallman committed
1064
  i = validate_interval_range (object, &start, &end, soft);
1065

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1066
  if (NULL_INTERVAL_P (i))
Richard M. Stallman's avatar
Richard M. Stallman committed
1067
    {
1068 1069
      /* If buffer has no properties, and we want none, return now.  */
      if (NILP (properties))
Richard M. Stallman's avatar
Richard M. Stallman committed
1070 1071
	return Qnil;

1072 1073 1074 1075 1076
      /* Restore the original START and END values
	 because validate_interval_range increments them for strings.  */
      start = ostart;
      end = oend;

Richard M. Stallman's avatar
Richard M. Stallman committed
1077 1078 1079 1080 1081
      i = validate_interval_range (object, &start, &end, hard);
      /* This can return if start == end.  */
      if (NULL_INTERVAL_P (i))
	return Qnil;
    }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1082 1083 1084 1085

  s = XINT (start);
  len = XINT (end) - s;

1086 1087
  if (BUFFERP (object))
    modify_region (XBUFFER (object), XINT (start), XINT (end));
1088

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1089 1090 1091
  if (i->position != s)
    {
      unchanged = i;
1092
      i = split_interval_right (unchanged, s - unchanged->position);