textprop.c 57.7 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, 1999, 2000 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
Lisp_Object Vtext_property_default_nonsticky;
73

74 75 76 77
/* 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;
78 79 80 81 82 83 84 85 86 87 88 89


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

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

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

#define soft 0
#define hard 1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      if (! found)
	return 0;
    }

  return 1;
}

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

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

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

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

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

  return Qunbound;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

411 412
  UNGCPRO;

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

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

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

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

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

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

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

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

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

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

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

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

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

  if (!(beg <= position && position <= end))
527
    args_out_of_range (make_number (position), make_number (position));
528 529 530 531 532 533
  if (beg == end || NULL_INTERVAL_P (i))
    return NULL_INTERVAL;
    
  return find_interval (i, position);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
534 535
DEFUN ("text-properties-at", Ftext_properties_at,
       Stext_properties_at, 1, 2, 0,
536 537 538
  "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\
539
If POSITION is at the end of OBJECT, the value is nil.")
540 541
  (position, object)
     Lisp_Object position, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
542 543 544 545
{
  register INTERVAL i;

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

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

  return i->plist;
}

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

572 573 574 575 576 577 578 579 580 581 582 583 584
/* 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)
585
     Lisp_Object position, object;
586
     register Lisp_Object prop;
587
     Lisp_Object *overlay;
588 589 590
{
  struct window *w = 0;

591
  CHECK_NUMBER_COERCE_MARKER (position, 0);
592 593

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

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

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

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

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

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

630 631
      set_buffer_temp (obuf);

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

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

650 651
  /* Not a buffer, or no appropriate overlay, so fall through to the
     simpler case.  */
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
  return Fget_text_property (position, prop, object);
}

DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
  "Return the value of POSITION's property PROP, in OBJECT.\n\
OBJECT is optional and defaults to the current buffer.\n\
If POSITION is at the end of OBJECT, the value is nil.\n\
If OBJECT is a buffer, then overlay properties are considered as well as\n\
text properties.\n\
If OBJECT is a window, then that window's buffer is used, but window-specific\n\
overlays are considered only if they are associated with OBJECT.")
  (position, prop, object)
     Lisp_Object position, object;
     register Lisp_Object prop;
{
  return get_char_property_and_overlay (position, prop, object, 0);
668
}
669 670 671 672 673 674 675 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

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;
709

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


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

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

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

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

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

      unbind_to (count, Qnil);
    }

783
  return position;
784 785
}

786 787 788 789 790 791 792 793 794 795 796 797 798 799 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
DEFUN ("previous-single-char-property-change",
       Fprevious_single_char_property_change,
       Sprevious_single_char_property_change, 2, 4, 0,
  "Return the position of previous text property or overlay change for a specific property.\n\
Scans characters backward from POSITION till it finds\n\
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\
The property values are compared with `eq'.\n\
Return nil if the property is constant all the way to the start of OBJECT.\n\
If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
If the optional fourth argument LIMIT is non-nil, don't search\n\
back past position LIMIT; return LIMIT if nothing is found before LIMIT.")
  (position, prop, object, limit)
     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);

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

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

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

      unbind_to (count, Qnil);
    }

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

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

883
  if (! NILP (limit) && ! EQ (limit, Qt))
884 885
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

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

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

906 907 908 909 910
  if (NULL_INTERVAL_P (i))
    return limit;

  next = next_interval (i);

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

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

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

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

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

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

  if (NULL_INTERVAL_P (next))
    return 0;

  return 1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
954 955
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
956
DEFUN ("next-single-property-change", Fnext_single_property_change,
957
       Snext_single_property_change, 2, 4, 0,
958
  "Return the position of next property change for a specific property.\n\
959
Scans characters forward from POSITION till it finds\n\
960 961
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\
962
The property values are compared with `eq'.\n\
963
Return nil if the property is constant all the way to the end of OBJECT.\n\
964
If the value is non-nil, it is a position greater than POSITION, never equal.\n\n\
965
If the optional fourth argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
966
past position LIMIT; return LIMIT if nothing is found before LIMIT.")
967 968
  (position, prop, object, limit)
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
969 970 971 972
{
  register INTERVAL i, next;
  register Lisp_Object here_val;

973
  if (NILP (object))
974
    XSETBUFFER (object, current_buffer);
975

976 977 978
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

979
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
980
  if (NULL_INTERVAL_P (i))
981
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
982

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

  if (NULL_INTERVAL_P (next))
991
    return limit;
992
  if (! NILP (limit) && !(next->position < XFASTINT (limit)))
993
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
994

995
  return make_number (next->position);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
996 997
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
998
DEFUN ("previous-property-change", Fprevious_property_change,
999
       Sprevious_property_change, 1, 3, 0,
1000
  "Return the position of previous property change.\n\
1001
Scans characters backwards from POSITION in OBJECT till it finds\n\
1002 1003 1004
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\
1005
If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
1006
If the optional third argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
1007
back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
1008 1009
  (position, object, limit)
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1010 1011 1012
{
  register INTERVAL i, previous;

1013
  if (NILP (object))
1014
    XSETBUFFER (object, current_buffer);
1015

1016 1017 1018
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

1019
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1020
  if (NULL_INTERVAL_P (i))
1021
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1022

1023
  /* Start with the interval containing the char before point.  */
1024
  if (i->position == XFASTINT (position))
1025 1026
    i = previous_interval (i);

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

1038
  return make_number (previous->position + LENGTH (previous));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1039 1040
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1041
DEFUN ("previous-single-property-change", Fprevious_single_property_change,
1042
       Sprevious_single_property_change, 2, 4, 0,
1043
  "Return the position of previous property change for a specific property.\n\
1044
Scans characters backward from POSITION till it finds\n\
1045 1046
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\
1047
The property values are compared with `eq'.\n\
1048
Return nil if the property is constant all the way to the start of OBJECT.\n\
1049
If the value is non-nil, it is a position less than POSITION, never equal.\n\n\
1050
If the optional fourth argument LIMIT is non-nil, don't search\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
1051
back past position LIMIT; return LIMIT if nothing is found until LIMIT.")
1052 1053
     (position, prop, object, limit)
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1054 1055 1056 1057
{
  register INTERVAL i, previous;
  register Lisp_Object here_val;

1058
  if (NILP (object))
1059
    XSETBUFFER (object, current_buffer);
1060

1061 1062 1063
  if (!NILP (limit))
    CHECK_NUMBER_COERCE_MARKER (limit, 0);

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