textprop.c 52.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
/* Types of hooks.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
51 52 53 54
Lisp_Object Qmouse_left;
Lisp_Object Qmouse_entered;
Lisp_Object Qpoint_left;
Lisp_Object Qpoint_entered;
55 56
Lisp_Object Qcategory;
Lisp_Object Qlocal_map;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
57

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

/* Sticky properties */
Lisp_Object Qfront_sticky, Qrear_nonsticky;
64 65 66 67

/* 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.  */
68
#define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCDR (o1), CONSP (o2)))
69

70
Lisp_Object Vinhibit_point_motion_hooks;
71
Lisp_Object Vdefault_text_properties;
72

73 74 75 76
/* 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
77

Joseph Arceneaux's avatar
Joseph Arceneaux committed
78 79 80 81 82 83
/* 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
84

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

Joseph Arceneaux's avatar
Joseph Arceneaux committed
88 89 90 91 92 93 94 95 96
   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
97 98
   create an interval tree for OBJECT if one doesn't exist, provided
   the object actually contains text.  In the current design, if there
99
   is no text, there can be no text properties.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
100 101 102 103

#define soft 0
#define hard 1

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

Joseph Arceneaux's avatar
Joseph Arceneaux committed
112 113 114 115 116
  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
117
     on a range, then return nothing.  */
118
  if (EQ (*begin, *end) && begin != end)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
119 120 121 122
    return NULL_INTERVAL;

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

129
  if (BUFFERP (object))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
130 131 132 133 134 135
    {
      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);
136
      i = BUF_INTERVALS (b);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
137

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

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

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

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

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

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

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

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

  if (CONSP (list))
    {
      register int i;
      register Lisp_Object tail;
      for (i = 0, tail = list; !NILP (tail); i++)
184 185 186 187
	{
	  tail = Fcdr (tail);
	  QUIT;
	}
Joseph Arceneaux's avatar
Joseph Arceneaux committed
188 189 190 191 192 193 194 195 196
      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,
197
   with the same values, of list PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
198 199 200 201 202 203

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

207
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
208 209 210 211 212 213 214 215 216 217
  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
218
	       values are unequal, return zero.  */
219
	    if (! EQ (Fcar (Fcdr (tail1)), Fcar (Fcdr (tail2))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
220 221
	      return 0;

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

      if (! found)
	return 0;
    }

  return 1;
}

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

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

244
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
245 246 247 248 249 250 251 252 253 254 255 256
  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;
}
257

258 259 260 261
/* Changing the plists of individual intervals.  */

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

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

  return Qunbound;
}

277 278 279 280 281 282 283 284 285
/* 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;
{
286
  Lisp_Object sym, value;
287

288
  if (BUFFERP (object))
289
    {
290 291 292 293
      /* 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);
294 295 296
	   sym = XCDR (value))
	if (! EQ (property_value (properties, XCAR (sym)),
		  XCAR (value)))
297 298
	  {
	    record_property_change (interval->position, LENGTH (interval),
299
				    XCAR (sym), XCAR (value),
300 301
				    object);
	  }
302 303 304 305 306

      /* 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);
307 308
	   sym = XCDR (value))
	if (EQ (property_value (interval->plist, XCAR (sym)), Qunbound))
309 310
	  {
	    record_property_change (interval->position, LENGTH (interval),
311
				    XCAR (sym), Qnil,
312 313
				    object);
	  }
314 315 316 317 318
    }

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

/* 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.

324 325
   OBJECT should be the string or buffer the interval is in.

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

329 330
static int
add_properties (plist, i, object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
331 332
     Lisp_Object plist;
     INTERVAL i;
333
     Lisp_Object object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
334
{
335
  Lisp_Object tail1, tail2, sym1, val1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
336 337
  register int changed = 0;
  register int found;
338 339 340 341 342 343 344 345 346
  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
347

348
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
349 350 351 352 353 354 355 356 357 358
  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)))
	  {
359 360
	    /* No need to gcpro, because tail2 protects this
	       and it must be a cons cell (we get an error otherwise).  */
361
	    register Lisp_Object this_cdr;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
362

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

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

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

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

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

398 399
  UNGCPRO;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
400 401 402 403
  return changed;
}

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

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

416
  current_plist = i->plist;
417
  /* Go through each element of plist.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
418 419 420 421 422 423 424
  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)))
	{
425
	  if (BUFFERP (object))
426
	    {
427 428 429
	      record_property_change (i->position, LENGTH (i),
				      sym, Fcar (Fcdr (current_plist)),
				      object);
430 431
	    }

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

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

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

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

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

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

  i->plist = Qnil;
  return 1;
}
476
#endif
Joseph Arceneaux's avatar
Joseph Arceneaux committed
477

478
/* Returns the interval of POSITION in OBJECT. 
479 480 481 482 483 484 485 486 487 488 489 490
   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
491 492
  else if (EQ (object, Qt))
    return NULL_INTERVAL;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

  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);

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

  if (!(beg <= position && position <= end))
514
    args_out_of_range (make_number (position), make_number (position));
515 516 517 518 519 520
  if (beg == end || NULL_INTERVAL_P (i))
    return NULL_INTERVAL;
    
  return find_interval (i, position);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
521 522
DEFUN ("text-properties-at", Ftext_properties_at,
       Stext_properties_at, 1, 2, 0,
523 524 525
  "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\
526
If POSITION is at the end of OBJECT, the value is nil.")
527 528
  (position, object)
     Lisp_Object position, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
529 530 531 532
{
  register INTERVAL i;

  if (NILP (object))
533
    XSETBUFFER (object, current_buffer);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
534

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

  return i->plist;
}

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

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

573
  CHECK_NUMBER_COERCE_MARKER (position, 0);
574 575

  if (NILP (object))
576
    XSETBUFFER (object, current_buffer);
577 578 579 580

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

      set_buffer_temp (XBUFFER (object));
593 594 595 596 597

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

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

      /* 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));
607 608
	  noverlays = overlays_at (posn, 0, &overlay_vec, &len,
				   &next_overlay, NULL);
609 610 611
	}
      noverlays = sort_overlays (overlay_vec, noverlays, w);

612 613
      set_buffer_temp (obuf);

614 615 616 617 618 619 620 621 622 623
      /* 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.  */
624
  return (Fget_text_property (position, prop, object));
625
}
626 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

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;
666

667 668 669 670 671 672 673 674 675
  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);
}
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729


/* Value is the position in OBJECT after POS where the value of
   property PROP changes.  OBJECT must be a string or buffer.  If
   OBJECT is nil, use the current buffer.  LIMIT if not nil limits the
   search.  */

Lisp_Object
next_single_char_property_change (pos, prop, object, limit)
     Lisp_Object prop, pos, object, limit;
{
  if (STRINGP (object))
    {
      pos = Fnext_single_property_change (pos, prop, object, limit);
      if (NILP (pos))
	{
	  if (NILP (limit))
	    pos = make_number (XSTRING (object)->size);
	  else
	    pos = limit;
	}
    }
  else
    {
      Lisp_Object initial_value, value;
      struct buffer *old_current_buffer = NULL;
      int count = specpdl_ptr - specpdl;

      if (!NILP (object))
	CHECK_BUFFER (object, 0);
      
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}

      initial_value = Fget_char_property (pos, prop, object);
      
      while (XFASTINT (pos) < XFASTINT (limit))
	{
	  pos = Fnext_char_property_change (pos, limit);
	  value = Fget_char_property (pos, prop, object);
	  if (!EQ (value, initial_value))
	    break;
	}

      unbind_to (count, Qnil);
    }

  return pos;
}


730

Joseph Arceneaux's avatar
Joseph Arceneaux committed
731
DEFUN ("next-property-change", Fnext_property_change,
732
       Snext_property_change, 1, 3, 0,
733
  "Return the position of next property change.\n\
734
Scans characters forward from POSITION in OBJECT till it finds\n\
735 736 737
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\
738
If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\
739 740
If the optional third argument LIMIT is non-nil, don't search\n\
past position LIMIT; return LIMIT if nothing is found before LIMIT.")
741 742
  (position, object, limit)
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
743 744 745
{
  register INTERVAL i, next;

746
  if (NILP (object))
747
    XSETBUFFER (object, current_buffer);
748

749
  if (! NILP (limit) && ! EQ (limit, Qt))
750 751
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

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

754 755 756 757
  /* If LIMIT is t, return start of next interval--don't
     bother checking further intervals.  */
  if (EQ (limit, Qt))
    {
758 759 760 761 762
      if (NULL_INTERVAL_P (i))
	next = i;
      else
	next = next_interval (i);
	
763
      if (NULL_INTERVAL_P (next))
764 765 766
	XSETFASTINT (position, (STRINGP (object)
				? XSTRING (object)->size
				: BUF_ZV (XBUFFER (object))));
767
      else
768
	XSETFASTINT (position, next->position);
769
      return position;
770 771
    }

772 773 774 775 776
  if (NULL_INTERVAL_P (i))
    return limit;

  next = next_interval (i);

777
  while (! NULL_INTERVAL_P (next) && intervals_equal (i, next)
778
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
779 780 781
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
782
    return limit;
783
  if (! NILP (limit) && !(next->position < XFASTINT (limit)))
784
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
785

786
  XSETFASTINT (position, next->position);
787
  return position;
788 789 790 791 792 793 794 795 796 797 798
}

/* 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;

799
  XSETBUFFER (object, current_buffer);
800
  XSETFASTINT (pos, beg);
801 802 803 804 805 806 807 808 809

  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);
810 811
      if (NULL_INTERVAL_P (next))
	return 0;
812
      if (next->position >= end)
813 814 815 816 817 818 819
	return 0;
    }

  if (NULL_INTERVAL_P (next))
    return 0;

  return 1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
820 821
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
822
DEFUN ("next-single-property-change", Fnext_single_property_change,
823
       Snext_single_property_change, 2, 4, 0,
824
  "Return the position of next property change for a specific property.\n\
825
Scans characters forward from POSITION till it finds\n\
826 827
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\
828
The property values are compared with `eq'.\n\
829
Return nil if the property is constant all the way to the end of OBJECT.\n\
830
If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\
831
If the optional fourth argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
832
past position LIMIT; return LIMIT if nothing is found before LIMIT.")
833 834
  (position, prop, object, limit)
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
835 836 837 838
{
  register INTERVAL i, next;
  register Lisp_Object here_val;

839
  if (NILP (object))
840
    XSETBUFFER (object, current_buffer);
841

842 843 844
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

845
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
846
  if (NULL_INTERVAL_P (i))
847
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
848

849
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
850
  next = next_interval (i);
851
  while (! NULL_INTERVAL_P (next) 
852
	 && EQ (here_val, textget (next->plist, prop))
853
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
854 855 856
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
857
    return limit;
858
  if (! NILP (limit) && !(next->position < XFASTINT (limit)))
859
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
860

861
  return make_number (next->position);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
862 863
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
864
DEFUN ("previous-property-change", Fprevious_property_change,
865
       Sprevious_property_change, 1, 3, 0,
866
  "Return the position of previous property change.\n\
867
Scans characters backwards from POSITION in OBJECT till it finds\n\
868 869 870
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\
871
If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
872
If the optional third argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
873
back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
874 875
  (position, object, limit)
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
876 877 878
{
  register INTERVAL i, previous;

879
  if (NILP (object))
880
    XSETBUFFER (object, current_buffer);
881

882 883 884
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

885
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
886
  if (NULL_INTERVAL_P (i))
887
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
888

889
  /* Start with the interval containing the char before point.  */
890
  if (i->position == XFASTINT (position))
891 892
    i = previous_interval (i);

Joseph Arceneaux's avatar
Joseph Arceneaux committed
893
  previous = previous_interval (i);
894 895
  while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i)
	 && (NILP (limit)
896
	     || (previous->position + LENGTH (previous) > XFASTINT (limit))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
897 898
    previous = previous_interval (previous);
  if (NULL_INTERVAL_P (previous))
899 900
    return limit;
  if (!NILP (limit)
901
      && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
902
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
903

904
  return make_number (previous->position + LENGTH (previous));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
905 906
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
907
DEFUN ("previous-single-property-change", Fprevious_single_property_change,
908
       Sprevious_single_property_change, 2, 4, 0,
909
  "Return the position of previous property change for a specific property.\n\
910
Scans characters backward from POSITION till it finds\n\
911 912
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\
913
The property values are compared with `eq'.\n\
914
Return nil if the property is constant all the way to the start of OBJECT.\n\
915
If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
916
If the optional fourth argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
917
back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
918 919
     (position, prop, object, limit)
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
920 921 922 923
{
  register INTERVAL i, previous;
  register Lisp_Object here_val;

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

927 928 929
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

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

932
  /* Start with the interval containing the char before point.  */
933
  if (! NULL_INTERVAL_P (i) && i->position == XFASTINT (position))
934 935
    i = previous_interval (i);

936 937 938
  if (NULL_INTERVAL_P (i))
    return limit;

939
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
940 941
  previous = previous_interval (i);
  while (! NULL_INTERVAL_P (previous)
942 943
	 && EQ (here_val, textget (previous->plist, prop))
	 && (NILP (limit)
944
	     || (previous->position + LENGTH (previous) > XFASTINT (limit))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
945 946
    previous = previous_interval (previous);
  if (NULL_INTERVAL_P (previous))
947 948
    return limit;
  if (!NILP (limit)
949
      && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
950
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
951

952
  return make_number (previous->position + LENGTH (previous));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
953
}
954

955 956
/* Callers note, this can GC when OBJECT is a buffer (or nil).  */

Joseph Arceneaux's avatar
Joseph Arceneaux committed
957
DEFUN ("add-text-properties", Fadd_text_properties,
958 959
       Sadd_text_properties, 3, 4, 0,
  "Add properties to the text from START to END.\n\
960
The third argument PROPERTIES is a property list\n\
961 962 963 964 965 966
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
967 968
{
  register INTERVAL i, unchanged;
969
  register int s, len, modified = 0;
970
  struct gcpro gcpro1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
971 972 973 974 975

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

976
  if (NILP (object))
977
    XSETBUFFER (object, current_buffer);
978

Joseph Arceneaux's avatar
Joseph Arceneaux committed
979 980 981 982 983 984 985
  i = validate_interval_range (object, &start, &end, hard);
  if (NULL_INTERVAL_P (i))
    return Qnil;

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

986 987 988 989
  /* 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
990
  /* If we're not starting on an interval boundary, we have to
991
    split this interval.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
992 993 994
  if (i->position != s)
    {
      /* If this interval already has the properties, we can
995
         skip it.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
996 997 998 999
      if (interval_has_all_properties (properties, i))
	{
	  int got = (LENGTH (i) - (s - i->position));
	  if (got >= len)
1000
	    RETURN_UNGCPRO (Qnil);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1001
	  len -= got;
1002
	  i = next_interval (i);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1003 1004 1005 1006
	}
      else
	{
	  unchanged = i;
1007
	  i = split_interval_right (unchanged, s - unchanged->position);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1008 1009 1010 1011
	  copy_properties (unchanged, i);
	}
    }

1012 1013
  if (BUFFERP (object))
    modify_region (XBUFFER (object), XINT (start), XINT (end));
1014

1015
  /* We are at the beginning of interval I, with LEN chars to scan.  */
Jim Blandy's avatar