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

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
9
the Free Software Foundation; either version 2, or (at your option)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
10 11 12 13 14 15 16 17 18
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
19 20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
21

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

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

/* 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
36 37 38 39 40 41


/* 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
42 43
  set_properties needs to deal with the interval property cache.

Joseph Arceneaux's avatar
Joseph Arceneaux committed
44
  It is assumed that for any interval plist, a property appears
45
  only once on the list.  Although some code i.e., remove_properties,
Joseph Arceneaux's avatar
Joseph Arceneaux committed
46
  handles the more general case, the uniqueness of properties is
47
  necessary for the system to remain consistent.  This requirement
48
  is enforced by the subrs installing properties onto the intervals.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
49 50


51
/* Types of hooks.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
52 53 54 55
Lisp_Object Qmouse_left;
Lisp_Object Qmouse_entered;
Lisp_Object Qpoint_left;
Lisp_Object Qpoint_entered;
56 57
Lisp_Object Qcategory;
Lisp_Object Qlocal_map;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
58

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

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

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

71
Lisp_Object Vinhibit_point_motion_hooks;
72
Lisp_Object Vdefault_text_properties;
73
Lisp_Object Vtext_property_default_nonsticky;
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;
79 80 81 82 83 84 85 86 87 88 89 90


/* Signal a `text-read-only' error.  This function makes it easier
   to capture that error in GDB by putting a breakpoint on it.  */

static void
text_read_only ()
{
  Fsignal (Qtext_read_only, Qnil);
}


Joseph Arceneaux's avatar
Joseph Arceneaux committed
91

Joseph Arceneaux's avatar
Joseph Arceneaux committed
92 93 94 95 96 97
/* 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
98

99 100 101
   When OBJECT is a string, we increment *BEGIN and *END
   to make them origin-one.

Joseph Arceneaux's avatar
Joseph Arceneaux committed
102 103 104 105 106 107 108 109 110
   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
111 112
   create an interval tree for OBJECT if one doesn't exist, provided
   the object actually contains text.  In the current design, if there
113
   is no text, there can be no text properties.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
114 115 116 117

#define soft 0
#define hard 1

Gerd Moellmann's avatar
Gerd Moellmann committed
118
INTERVAL
Joseph Arceneaux's avatar
Joseph Arceneaux committed
119 120 121 122 123
validate_interval_range (object, begin, end, force)
     Lisp_Object object, *begin, *end;
     int force;
{
  register INTERVAL i;
124 125
  int searchpos;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
126 127 128 129 130
  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
131
     on a range, then return nothing.  */
132
  if (EQ (*begin, *end) && begin != end)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
133 134 135 136
    return NULL_INTERVAL;

  if (XINT (*begin) > XINT (*end))
    {
137 138
      Lisp_Object n;
      n = *begin;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
139
      *begin = *end;
140
      *end = n;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
141 142
    }

143
  if (BUFFERP (object))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
144 145 146 147 148 149
    {
      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);
150
      i = BUF_INTERVALS (b);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
151

152
      /* If there's no text, there are no properties.  */
153 154 155 156
      if (BUF_BEGV (b) == BUF_ZV (b))
	return NULL_INTERVAL;

      searchpos = XINT (*begin);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
157 158 159 160 161
    }
  else
    {
      register struct Lisp_String *s = XSTRING (object);

162
      if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
163 164
	     && XINT (*end) <= s->size))
	args_out_of_range (*begin, *end);
165
      XSETFASTINT (*begin, XFASTINT (*begin));
166
      if (begin != end)
167
	XSETFASTINT (*end, XFASTINT (*end));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
168
      i = s->intervals;
169 170 171 172 173

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

      searchpos = XINT (*begin);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
174 175 176 177 178
    }

  if (NULL_INTERVAL_P (i))
    return (force ? create_root_interval (object) : i);
    
179
  return find_interval (i, searchpos);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
180 181 182 183
}

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

static Lisp_Object
validate_plist (list)
188
     Lisp_Object list;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
189 190 191 192 193 194 195 196 197
{
  if (NILP (list))
    return Qnil;

  if (CONSP (list))
    {
      register int i;
      register Lisp_Object tail;
      for (i = 0, tail = list; !NILP (tail); i++)
198 199 200 201
	{
	  tail = Fcdr (tail);
	  QUIT;
	}
Joseph Arceneaux's avatar
Joseph Arceneaux committed
202 203 204 205 206 207 208 209 210
      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,
211
   with the same values, of list PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
212 213 214 215 216 217

static int
interval_has_all_properties (plist, i)
     Lisp_Object plist;
     INTERVAL i;
{
218
  register Lisp_Object tail1, tail2, sym1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
219 220
  register int found;

221
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
222 223 224 225 226 227 228 229 230 231
  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
232
	       values are unequal, return zero.  */
233
	    if (! EQ (Fcar (Fcdr (tail1)), Fcar (Fcdr (tail2))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
234 235
	      return 0;

236
	    /* Property has same value on both lists;  go to next one.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
237 238 239 240 241 242 243 244 245 246 247 248
	    found = 1;
	    break;
	  }

      if (! found)
	return 0;
    }

  return 1;
}

/* Return nonzero if the plist of interval I has any of the
249
   properties of PLIST, regardless of their values.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
250 251 252 253 254 255 256 257

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

258
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
259 260 261 262 263 264 265 266 267 268 269 270
  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;
}
271

272 273 274 275
/* Changing the plists of individual intervals.  */

/* Return the value of PROP in property-list PLIST, or Qunbound if it
   has none.  */
276
static Lisp_Object
277
property_value (plist, prop)
278
     Lisp_Object plist, prop;
279 280 281 282
{
  Lisp_Object value;

  while (PLIST_ELT_P (plist, value))
283 284
    if (EQ (XCAR (plist), prop))
      return XCAR (value);
285
    else
286
      plist = XCDR (value);
287 288 289 290

  return Qunbound;
}

291 292 293 294 295 296 297 298 299
/* 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;
{
300
  Lisp_Object sym, value;
301

302
  if (BUFFERP (object))
303
    {
304 305 306 307
      /* 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);
308 309 310
	   sym = XCDR (value))
	if (! EQ (property_value (properties, XCAR (sym)),
		  XCAR (value)))
311 312
	  {
	    record_property_change (interval->position, LENGTH (interval),
313
				    XCAR (sym), XCAR (value),
314 315
				    object);
	  }
316 317 318 319 320

      /* 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);
321 322
	   sym = XCDR (value))
	if (EQ (property_value (interval->plist, XCAR (sym)), Qunbound))
323 324
	  {
	    record_property_change (interval->position, LENGTH (interval),
325
				    XCAR (sym), Qnil,
326 327
				    object);
	  }
328 329 330 331 332
    }

  /* Store new properties.  */
  interval->plist = Fcopy_sequence (properties);
}
Joseph Arceneaux's avatar
Joseph Arceneaux committed
333 334 335 336 337

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

338 339
   OBJECT should be the string or buffer the interval is in.

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

343 344
static int
add_properties (plist, i, object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
345 346
     Lisp_Object plist;
     INTERVAL i;
347
     Lisp_Object object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
348
{
349
  Lisp_Object tail1, tail2, sym1, val1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
350 351
  register int changed = 0;
  register int found;
352 353 354 355 356 357 358 359 360
  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
361

362
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
363 364 365 366 367 368 369 370 371 372
  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)))
	  {
373 374
	    /* No need to gcpro, because tail2 protects this
	       and it must be a cons cell (we get an error otherwise).  */
375
	    register Lisp_Object this_cdr;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
376

377
	    this_cdr = Fcdr (tail2);
378
	    /* Found the property.  Now check its value.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
379 380 381
	    found = 1;

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

386
	    /* Record this change in the buffer, for undo purposes.  */
387
	    if (BUFFERP (object))
388
	      {
389 390
		record_property_change (i->position, LENGTH (i),
					sym1, Fcar (this_cdr), object);
391 392
	      }

Joseph Arceneaux's avatar
Joseph Arceneaux committed
393 394 395 396 397 398 399 400
	    /* I's property has a different value -- change it */
	    Fsetcar (this_cdr, val1);
	    changed++;
	    break;
	  }

      if (! found)
	{
401
	  /* Record this change in the buffer, for undo purposes.  */
402
	  if (BUFFERP (object))
403
	    {
404 405
	      record_property_change (i->position, LENGTH (i),
				      sym1, Qnil, object);
406
	    }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
407 408 409 410 411
	  i->plist = Fcons (sym1, Fcons (val1, i->plist));
	  changed++;
	}
    }

412 413
  UNGCPRO;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
414 415 416 417
  return changed;
}

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

421 422
static int
remove_properties (plist, i, object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
423 424
     Lisp_Object plist;
     INTERVAL i;
425
     Lisp_Object object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
426
{
427
  register Lisp_Object tail1, tail2, sym, current_plist;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
428 429
  register int changed = 0;

430
  current_plist = i->plist;
431
  /* Go through each element of plist.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
432 433 434 435 436 437 438
  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)))
	{
439
	  if (BUFFERP (object))
440
	    {
441 442 443
	      record_property_change (i->position, LENGTH (i),
				      sym, Fcar (Fcdr (current_plist)),
				      object);
444 445
	    }

Joseph Arceneaux's avatar
Joseph Arceneaux committed
446 447 448 449 450 451 452 453
	  current_plist = Fcdr (Fcdr (current_plist));
	  changed++;
	}

      /* Go through i's plist, looking for sym */
      tail2 = current_plist;
      while (! NILP (tail2))
	{
454 455
	  register Lisp_Object this;
	  this = Fcdr (Fcdr (tail2));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
456 457
	  if (EQ (sym, Fcar (this)))
	    {
458
	      if (BUFFERP (object))
459
		{
460 461
		  record_property_change (i->position, LENGTH (i),
					  sym, Fcar (Fcdr (this)), object);
462 463
		}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
464 465 466 467 468 469 470 471 472 473 474 475
	      Fsetcdr (Fcdr (tail2), Fcdr (Fcdr (this)));
	      changed++;
	    }
	  tail2 = this;
	}
    }

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

476
#if 0
Joseph Arceneaux's avatar
Joseph Arceneaux committed
477
/* Remove all properties from interval I.  Return non-zero
478
   if this changes the interval.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
479 480 481 482 483 484 485 486 487 488 489

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

  i->plist = Qnil;
  return 1;
}
490
#endif
Joseph Arceneaux's avatar
Joseph Arceneaux committed
491

492
/* Returns the interval of POSITION in OBJECT. 
493 494 495 496 497 498 499 500 501 502 503 504
   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
505 506
  else if (EQ (object, Qt))
    return NULL_INTERVAL;
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

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

522 523
      beg = 0;
      end = s->size;
524 525 526 527
      i = s->intervals;
    }

  if (!(beg <= position && position <= end))
528
    args_out_of_range (make_number (position), make_number (position));
529 530 531 532 533 534
  if (beg == end || NULL_INTERVAL_P (i))
    return NULL_INTERVAL;
    
  return find_interval (i, position);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
535 536
DEFUN ("text-properties-at", Ftext_properties_at,
       Stext_properties_at, 1, 2, 0,
537 538 539 540 541
       doc: /* Return the list of properties of the character at POSITION in OBJECT.
OBJECT is the string or buffer to look for the properties in;
nil means the current buffer.
If POSITION is at the end of OBJECT, the value is nil.  */)
     (position, object)
542
     Lisp_Object position, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
543 544 545 546
{
  register INTERVAL i;

  if (NILP (object))
547
    XSETBUFFER (object, current_buffer);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
548

549
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
550 551
  if (NULL_INTERVAL_P (i))
    return Qnil;
552
  /* If POSITION is at the end of the interval,
553 554 555
     it means it's the end of OBJECT.
     There are no properties at the very end,
     since no character follows.  */
556
  if (XINT (position) == LENGTH (i) + i->position)
557
    return Qnil;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
558 559 560 561

  return i->plist;
}

562
DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
563 564 565 566
       doc: /* Return the value of POSITION's property PROP, in OBJECT.
OBJECT is optional and defaults to the current buffer.
If POSITION is at the end of OBJECT, the value is nil.  */)
     (position, prop, object)
567
     Lisp_Object position, object;
568
     Lisp_Object prop;
569
{
570
  return textget (Ftext_properties_at (position, object), prop);
571 572
}

573 574 575 576 577 578 579 580 581 582 583 584 585
/* Return the value of POSITION's property PROP, in OBJECT.
   OBJECT is optional and defaults to the current buffer.
   If OVERLAY is non-0, then in the case that the returned property is from
   an overlay, the overlay found is returned in *OVERLAY, otherwise nil is
   returned in *OVERLAY.
   If POSITION is at the end of OBJECT, the value is nil.
   If OBJECT is a buffer, then overlay properties are considered as well as
   text properties.
   If OBJECT is a window, then that window's buffer is used, but
   window-specific overlays are considered only if they are associated
   with OBJECT. */
Lisp_Object
get_char_property_and_overlay (position, prop, object, overlay)
586
     Lisp_Object position, object;
587
     register Lisp_Object prop;
588
     Lisp_Object *overlay;
589 590 591
{
  struct window *w = 0;

592
  CHECK_NUMBER_COERCE_MARKER (position, 0);
593 594

  if (NILP (object))
595
    XSETBUFFER (object, current_buffer);
596 597 598 599

  if (WINDOWP (object))
    {
      w = XWINDOW (object);
600
      object = w->buffer;
601 602 603
    }
  if (BUFFERP (object))
    {
604
      int posn = XINT (position);
605 606 607 608
      int noverlays;
      Lisp_Object *overlay_vec, tem;
      int next_overlay;
      int len;
609 610 611
      struct buffer *obuf = current_buffer;

      set_buffer_temp (XBUFFER (object));
612 613 614 615 616

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

617
      noverlays = overlays_at (posn, 0, &overlay_vec, &len,
618
			       &next_overlay, NULL, 0);
619 620 621 622 623 624 625

      /* 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));
626
	  noverlays = overlays_at (posn, 0, &overlay_vec, &len,
627
				   &next_overlay, NULL, 0);
628 629 630
	}
      noverlays = sort_overlays (overlay_vec, noverlays, w);

631 632
      set_buffer_temp (obuf);

633 634 635 636 637
      /* Now check the overlays in order of decreasing priority.  */
      while (--noverlays >= 0)
	{
	  tem = Foverlay_get (overlay_vec[noverlays], prop);
	  if (!NILP (tem))
638 639 640 641 642 643
	    {
	      if (overlay)
		/* Return the overlay we got the property from.  */
		*overlay = overlay_vec[noverlays];
	      return tem;
	    }
644 645
	}
    }
646 647 648 649 650

  if (overlay)
    /* Indicate that the return value is not from an overlay.  */
    *overlay = Qnil;

651 652
  /* Not a buffer, or no appropriate overlay, so fall through to the
     simpler case.  */
653 654 655 656
  return Fget_text_property (position, prop, object);
}

DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
657 658 659 660 661 662 663 664
       doc: /* Return the value of POSITION's property PROP, in OBJECT.
OBJECT is optional and defaults to the current buffer.
If POSITION is at the end of OBJECT, the value is nil.
If OBJECT is a buffer, then overlay properties are considered as well as
text properties.
If OBJECT is a window, then that window's buffer is used, but window-specific
overlays are considered only if they are associated with OBJECT.  */)
     (position, prop, object)
665 666 667 668
     Lisp_Object position, object;
     register Lisp_Object prop;
{
  return get_char_property_and_overlay (position, prop, object, 0);
669
}
670 671 672

DEFUN ("next-char-property-change", Fnext_char_property_change,
       Snext_char_property_change, 1, 2, 0,
673 674 675 676 677 678 679 680 681
       doc: /* Return the position of next text property or overlay change.
This scans characters forward from POSITION till it finds a change in
some text property, or the beginning or end of an overlay, and returns
the position of that.
If none is found, the function returns (point-max).

If the optional third argument LIMIT is non-nil, don't search
past position LIMIT; return LIMIT if nothing is found before LIMIT.  */)
     (position, limit)
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
     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,
698 699 700 701 702 703 704 705 706
       doc: /* Return the position of previous text property or overlay change.
Scans characters backward from POSITION till it finds a change in some
text property, or the beginning or end of an overlay, and returns the
position of that.
If none is found, the function returns (point-max).

If the optional third argument LIMIT is non-nil, don't search
past position LIMIT; return LIMIT if nothing is found before LIMIT.  */)
     (position, limit)
707 708 709
     Lisp_Object position, limit;
{
  Lisp_Object temp;
710

711 712 713 714 715 716 717 718 719
  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);
}
720 721


722 723
DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
       Snext_single_char_property_change, 2, 4, 0,
724 725 726 727 728 729 730 731 732 733
       doc: /* Return the position of next text property or overlay change for a specific property.
Scans characters forward from POSITION till it finds
a change in the PROP property, then returns the position of the change.
The optional third argument OBJECT is the string or buffer to scan.
The property values are compared with `eq'.
If the property is constant all the way to the end of OBJECT, return the
last valid position in OBJECT.
If the optional fourth argument LIMIT is non-nil, don't search
past position LIMIT; return LIMIT if nothing is found before LIMIT.  */)
     (position, prop, object, limit)
734
     Lisp_Object prop, position, object, limit;
735 736 737
{
  if (STRINGP (object))
    {
738 739
      position = Fnext_single_property_change (position, prop, object, limit);
      if (NILP (position))
740 741
	{
	  if (NILP (limit))
742
	    position = make_number (XSTRING (object)->size);
743
	  else
744
	    position = limit;
745 746 747 748 749 750 751
	}
    }
  else
    {
      Lisp_Object initial_value, value;
      int count = specpdl_ptr - specpdl;

752
      if (! NILP (object))
753 754 755 756 757 758 759 760
	CHECK_BUFFER (object, 0);
      
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}

761
      initial_value = Fget_char_property (position, prop, object);
762
      
763 764 765 766 767 768
      if (NILP (limit))
	XSETFASTINT (limit, BUF_ZV (current_buffer));
      else
	CHECK_NUMBER_COERCE_MARKER (limit, 0);

      for (;;)
769
	{
770 771 772 773 774 775 776
	  position = Fnext_char_property_change (position, limit);
	  if (XFASTINT (position) >= XFASTINT (limit)) {
	    position = limit;
	    break;
	  }

	  value = Fget_char_property (position, prop, object);
777 778 779 780 781 782 783
	  if (!EQ (value, initial_value))
	    break;
	}

      unbind_to (count, Qnil);
    }

784
  return position;
785 786
}

787 788 789
DEFUN ("previous-single-char-property-change",
       Fprevious_single_char_property_change,
       Sprevious_single_char_property_change, 2, 4, 0,
790 791 792 793 794 795 796 797 798 799
       doc: /* Return the position of previous text property or overlay change for a specific property.
Scans characters backward from POSITION till it finds
a change in the PROP property, then returns the position of the change.
The optional third argument OBJECT is the string or buffer to scan.
The property values are compared with `eq'.
If the property is constant all the way to the start of OBJECT, return the
first valid position in OBJECT.
If the optional fourth argument LIMIT is non-nil, don't search
back past position LIMIT; return LIMIT if nothing is found before LIMIT.  */)
     (position, prop, object, limit)
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
     Lisp_Object prop, position, object, limit;
{
  if (STRINGP (object))
    {
      position = Fprevious_single_property_change (position, prop, object, limit);
      if (NILP (position))
	{
	  if (NILP (limit))
	    position = make_number (XSTRING (object)->size);
	  else
	    position = limit;
	}
    }
  else
    {
      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);
	}
      
      if (NILP (limit))
	XSETFASTINT (limit, BUF_BEGV (current_buffer));
      else
	CHECK_NUMBER_COERCE_MARKER (limit, 0);

831 832 833
      if (XFASTINT (position) <= XFASTINT (limit))
	position = limit;
      else
834
	{
835
	  Lisp_Object initial_value =
836 837
	    Fget_char_property (make_number (XFASTINT (position) - 1),
				prop, object);
838 839 840 841
      
	  for (;;)
	    {
	      position = Fprevious_char_property_change (position, limit);
842

843 844 845 846 847 848 849 850
	      if (XFASTINT (position) <= XFASTINT (limit))
		{
		  position = limit;
		  break;
		}
	      else
		{
		  Lisp_Object value =
851 852
		    Fget_char_property (make_number (XFASTINT (position) - 1),
					prop, object);
853 854 855 856 857

		  if (!EQ (value, initial_value))
		    break;
		}
	    }
858 859 860 861 862 863 864
	}

      unbind_to (count, Qnil);
    }

  return position;
}
865

Joseph Arceneaux's avatar
Joseph Arceneaux committed
866
DEFUN ("next-property-change", Fnext_property_change,
867
       Snext_property_change, 1, 3, 0,
868 869 870 871 872 873 874 875 876 877
       doc: /* Return the position of next property change.
Scans characters forward from POSITION in OBJECT till it finds
a change in some text property, then returns the position of the change.
The optional second argument OBJECT is the string or buffer to scan.
Return nil if the property is constant all the way to the end of OBJECT.
If the value is non-nil, it is a position greater than POSITION, never equal.

If the optional third argument LIMIT is non-nil, don't search
past position LIMIT; return LIMIT if nothing is found before LIMIT.  */)
     (position, object, limit)
878
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
879 880 881
{
  register INTERVAL i, next;

882
  if (NILP (object))
883
    XSETBUFFER (object, current_buffer);
884

885
  if (! NILP (limit) && ! EQ (limit, Qt))
886 887
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

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

890 891 892 893
  /* If LIMIT is t, return start of next interval--don't
     bother checking further intervals.  */
  if (EQ (limit, Qt))
    {
894 895 896 897 898
      if (NULL_INTERVAL_P (i))
	next = i;
      else
	next = next_interval (i);
	
899
      if (NULL_INTERVAL_P (next))
900 901 902
	XSETFASTINT (position, (STRINGP (object)
				? XSTRING (object)->size
				: BUF_ZV (XBUFFER (object))));
903
      else
904
	XSETFASTINT (position, next->position);
905
      return position;
906 907
    }

908 909 910 911 912
  if (NULL_INTERVAL_P (i))
    return limit;

  next = next_interval (i);

913
  while (! NULL_INTERVAL_P (next) && intervals_equal (i, next)
914
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
915 916 917
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
918
    return limit;
919
  if (! NILP (limit) && !(next->position < XFASTINT (limit)))
920
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
921

922
  XSETFASTINT (position, next->position);
923
  return position;
924 925 926 927 928 929 930 931 932 933 934
}

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

935
  XSETBUFFER (object, current_buffer);
936
  XSETFASTINT (pos, beg);
937 938 939 940 941 942 943 944 945

  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);
946 947
      if (NULL_INTERVAL_P (next))
	return 0;
948
      if (next->position >= end)
949 950 951 952 953 954 955
	return 0;
    }

  if (NULL_INTERVAL_P (next))
    return 0;

  return 1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
956 957
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
958
DEFUN ("next-single-property-change", Fnext_single_property_change,
959
       Snext_single_property_change, 2, 4, 0,
960 961 962 963 964 965 966 967 968 969 970
       doc: /* Return the position of next property change for a specific property.
Scans characters forward from POSITION till it finds
a change in the PROP property, then returns the position of the change.
The optional third argument OBJECT is the string or buffer to scan.
The property values are compared with `eq'.
Return nil if the property is constant all the way to the end of OBJECT.
If the value is non-nil, it is a position greater than POSITION, never equal.

If the optional fourth argument LIMIT is non-nil, don't search
past position LIMIT; return LIMIT if nothing is found before LIMIT.  */)
     (position, prop, object, limit)
971
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
972 973 974 975
{
  register INTERVAL i, next;
  register Lisp_Object here_val;

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

979 980 981
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

982
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
983
  if (NULL_INTERVAL_P (i))
984
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
985

986
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
987
  next = next_interval (i);
988
  while (! NULL_INTERVAL_P (next) 
989
	 && EQ (here_val, textget (next->plist, prop))
990
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
991 992 993
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
994
    return limit;
995
  if (! NILP (limit) && !(next->position < XFASTINT (limit)))
996
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
997

998
  return make_number (next->position);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
999 1000
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1001
DEFUN ("previous-property-change", Fprevious_property_change,
1002
       Sprevious_property_change, 1, 3, 0,
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
       doc: /* Return the position of previous property change.
Scans characters backwards from POSITION in OBJECT till it finds
a change in some text property, then returns the position of the change.
The optional second argument OBJECT is the string or buffer to scan.
Return nil if the property is constant all the way to the start of OBJECT.
If the value is non-nil, it is a position less than POSITION, never equal.

If the optional third argument LIMIT is non-nil, don't search
back past position LIMIT; return LIMIT if nothing is found until LIMIT.  */)
     (position, object, limit)
1013
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1014 1015 1016
{
  register INTERVAL i, previous;

1017
  if (NILP (object))
1018
    XSETBUFFER (object, current_buffer);
1019

1020 1021 1022
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

1023
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1024
  if (NULL_INTERVAL_P (i))
1025
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1026

1027
  /* Start with the interval containing the char before point.  */
1028
  if (i->position == XFASTINT (position))
1029 1030
    i = previous_interval (i);

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1031
  previous = previous_interval (i);
1032 1033
  while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i)
	 && (NILP (limit)
1034
	     || (previous->position + LENGTH (previous) > XFASTINT (limit))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1035 1036
    previous = previous_interval (previous);
  if (NULL_INTERVAL_P (previous))
1037 1038
    return limit;
  if (!NILP (limit)
1039
      && !(previous->position + LENGTH (previous) > XFASTINT (limit)))
1040
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1041

1042
  return make_number (previous->position + LENGTH (previous));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1043 1044
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1045
DEFUN ("previous-single-property-change", Fprevious_single_property_change,
1046
       Sprevious_single_property_change, 2, 4, 0,
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
       doc: /* Return the position of previous property change for a specific property.
Scans characters backward from POSITION till it finds
a change in the PROP property, then returns the position of the change.
The optional third argument OBJECT is the string or buffer to scan.
The property values are compared with `eq'.
Return nil if the property is constant all the way to the start of OBJECT.
If the value is non-nil, it is a position less than POSITION, never equal.

If the optional fourth argument LIMIT is non-nil, don't search
back past position LIMIT; return LIMIT if nothing is found until LIMIT.  */)
1057 1058
     (position, prop, object, limit)
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1059 1060 1061 1062
{
  register INTERVAL i, previous;
  register Lisp_Object here_val;

1063
  if (NILP (object))
1064
    XSETBUFFER (object, current_buffer);
1065

1066 1067 1068
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

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

1071
  /* Start with the interval containing the char before point.  */
1072
  if (! NULL_INTERVAL_P (i) && i->position == XFASTINT (position))
1073 1074
    i = previous_interval (i);

1075 1076 1077
  if (NULL_INTERVAL_P (i))
    return limit;

1078
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1079 1080
  previous = previous_interval (i);
  while (! NULL_INTERVAL_P (previous)
1081 1082
	 && EQ (here_val, textget (previous->plist, prop))
	 && (NILP (limit)
1083
	     || (previous->position + LENGTH (previous) > XFASTINT (limit))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1084 1085
    previous = previous_interval (previous);
  if (NULL_INTERVAL_P (previous))
1086 1087
    return limit;
  if (!NILP (limit)