textprop.c 63.8 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, 2001, 2002
3
   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 Vchar_property_alias_alist;
74
Lisp_Object Vtext_property_default_nonsticky;
75

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


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

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

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

#define soft 0
#define hard 1

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

127 128 129
  CHECK_STRING_OR_BUFFER (object);
  CHECK_NUMBER_COERCE_MARKER (*begin);
  CHECK_NUMBER_COERCE_MARKER (*end);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
130 131

  /* If we are asked for a point, but from a subr which operates
132
     on a range, then return nothing.  */
133
  if (EQ (*begin, *end) && begin != end)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
134 135 136 137
    return NULL_INTERVAL;

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

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

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

      searchpos = XINT (*begin);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
158 159 160
    }
  else
    {
161
      int len = SCHARS (object);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
162

163
      if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end)
164
	     && XINT (*end) <= len))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
165
	args_out_of_range (*begin, *end);
166
      XSETFASTINT (*begin, XFASTINT (*begin));
167
      if (begin != end)
168
	XSETFASTINT (*end, XFASTINT (*end));
169
      i = STRING_INTERVALS (object);
170

171
      if (len == 0)
172 173 174
	return NULL_INTERVAL;

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

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

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

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

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

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

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

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

      if (! found)
	return 0;
    }

  return 1;
}

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

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

259
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
260 261 262 263 264 265 266 267 268 269 270 271
  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;
}
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

/* Return nonzero if the plist of interval I has any of the
   property names in LIST, regardless of their values.  */

static INLINE int
interval_has_some_properties_list (list, i)
     Lisp_Object list;
     INTERVAL i;
{
  register Lisp_Object tail1, tail2, sym;

  /* Go through each element of LIST.  */
  for (tail1 = list; ! NILP (tail1); tail1 = XCDR (tail1))
    {
      sym = Fcar (tail1);

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

  return 0;
}
296

297 298 299 300
/* Changing the plists of individual intervals.  */

/* Return the value of PROP in property-list PLIST, or Qunbound if it
   has none.  */
301
static Lisp_Object
302
property_value (plist, prop)
303
     Lisp_Object plist, prop;
304 305 306 307
{
  Lisp_Object value;

  while (PLIST_ELT_P (plist, value))
308 309
    if (EQ (XCAR (plist), prop))
      return XCAR (value);
310
    else
311
      plist = XCDR (value);
312 313 314 315

  return Qunbound;
}

316 317 318 319 320 321 322 323 324
/* 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;
{
325
  Lisp_Object sym, value;
326

327
  if (BUFFERP (object))
328
    {
329 330 331 332
      /* 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);
333 334 335
	   sym = XCDR (value))
	if (! EQ (property_value (properties, XCAR (sym)),
		  XCAR (value)))
336 337
	  {
	    record_property_change (interval->position, LENGTH (interval),
338
				    XCAR (sym), XCAR (value),
339 340
				    object);
	  }
341 342 343 344 345

      /* 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);
346 347
	   sym = XCDR (value))
	if (EQ (property_value (interval->plist, XCAR (sym)), Qunbound))
348 349
	  {
	    record_property_change (interval->position, LENGTH (interval),
350
				    XCAR (sym), Qnil,
351 352
				    object);
	  }
353 354 355 356 357
    }

  /* Store new properties.  */
  interval->plist = Fcopy_sequence (properties);
}
Joseph Arceneaux's avatar
Joseph Arceneaux committed
358 359 360 361 362

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

363 364
   OBJECT should be the string or buffer the interval is in.

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

368 369
static int
add_properties (plist, i, object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
370 371
     Lisp_Object plist;
     INTERVAL i;
372
     Lisp_Object object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
373
{
374
  Lisp_Object tail1, tail2, sym1, val1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
375 376
  register int changed = 0;
  register int found;
377 378 379 380 381 382 383 384 385
  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
386

387
  /* Go through each element of PLIST.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
388 389 390 391 392 393 394 395 396 397
  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)))
	  {
398 399
	    /* No need to gcpro, because tail2 protects this
	       and it must be a cons cell (we get an error otherwise).  */
400
	    register Lisp_Object this_cdr;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
401

402
	    this_cdr = Fcdr (tail2);
403
	    /* Found the property.  Now check its value.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
404 405 406
	    found = 1;

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

411
	    /* Record this change in the buffer, for undo purposes.  */
412
	    if (BUFFERP (object))
413
	      {
414 415
		record_property_change (i->position, LENGTH (i),
					sym1, Fcar (this_cdr), object);
416 417
	      }

Joseph Arceneaux's avatar
Joseph Arceneaux committed
418 419 420 421 422 423 424 425
	    /* I's property has a different value -- change it */
	    Fsetcar (this_cdr, val1);
	    changed++;
	    break;
	  }

      if (! found)
	{
426
	  /* Record this change in the buffer, for undo purposes.  */
427
	  if (BUFFERP (object))
428
	    {
429 430
	      record_property_change (i->position, LENGTH (i),
				      sym1, Qnil, object);
431
	    }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
432 433 434 435 436
	  i->plist = Fcons (sym1, Fcons (val1, i->plist));
	  changed++;
	}
    }

437 438
  UNGCPRO;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
439 440 441
  return changed;
}

442 443 444
/* For any members of PLIST, or LIST,
   which are properties of I, remove them from I's plist.
   (If PLIST is non-nil, use that, otherwise use LIST.)
445
   OBJECT is the string or buffer containing I.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
446

447
static int
448 449
remove_properties (plist, list, i, object)
     Lisp_Object plist, list;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
450
     INTERVAL i;
451
     Lisp_Object object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
452
{
453
  register Lisp_Object tail1, tail2, sym, current_plist;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
454 455
  register int changed = 0;

456 457
  /* Nonzero means tail1 is a plist, otherwise it is a list.  */
  int use_plist;
458

459
  current_plist = i->plist;
460 461

  if (! NILP (plist))
462
    tail1 = plist, use_plist = 1;
463
  else
464
    tail1 = list, use_plist = 0;
465 466

  /* Go through each element of LIST or PLIST.  */
467
  while (CONSP (tail1))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
468
    {
469
      sym = XCAR (tail1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
470

471
      /* First, remove the symbol if it's at the head of the list */
472
      while (CONSP (current_plist) && EQ (sym, XCAR (current_plist)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
473
	{
474
	  if (BUFFERP (object))
475 476 477
	    record_property_change (i->position, LENGTH (i),
				    sym, XCAR (XCDR (current_plist)),
				    object);
478

479
	  current_plist = XCDR (XCDR (current_plist));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
480 481 482
	  changed++;
	}

483
      /* Go through I's plist, looking for SYM.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
484 485 486
      tail2 = current_plist;
      while (! NILP (tail2))
	{
487
	  register Lisp_Object this;
488
	  this = XCDR (XCDR (tail2));
489
	  if (CONSP (this) && EQ (sym, XCAR (this)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
490
	    {
491
	      if (BUFFERP (object))
492 493
		record_property_change (i->position, LENGTH (i),
					sym, XCAR (XCDR (this)), object);
494

495
	      Fsetcdr (XCDR (tail2), XCDR (XCDR (this)));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
496 497 498 499
	      changed++;
	    }
	  tail2 = this;
	}
500 501

      /* Advance thru TAIL1 one way or the other.  */
502 503
      tail1 = XCDR (tail1);
      if (use_plist && CONSP (tail1))
504
	tail1 = XCDR (tail1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
505 506 507 508 509 510 511
    }

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

512
#if 0
Joseph Arceneaux's avatar
Joseph Arceneaux committed
513
/* Remove all properties from interval I.  Return non-zero
514
   if this changes the interval.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
515 516 517 518 519 520 521 522 523 524 525

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

  i->plist = Qnil;
  return 1;
}
526
#endif
Joseph Arceneaux's avatar
Joseph Arceneaux committed
527

528
/* Returns the interval of POSITION in OBJECT. 
529 530 531 532 533 534 535 536 537 538 539 540
   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
541 542
  else if (EQ (object, Qt))
    return NULL_INTERVAL;
543

544
  CHECK_STRING_OR_BUFFER (object);
545 546 547 548 549 550 551 552 553 554 555

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

      beg = BUF_BEGV (b);
      end = BUF_ZV (b);
      i = BUF_INTERVALS (b);
    }
  else
    {
556
      beg = 0;
557 558
      end = SCHARS (object);
      i = STRING_INTERVALS (object);
559 560 561
    }

  if (!(beg <= position && position <= end))
562
    args_out_of_range (make_number (position), make_number (position));
563 564 565 566 567 568
  if (beg == end || NULL_INTERVAL_P (i))
    return NULL_INTERVAL;
    
  return find_interval (i, position);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
569 570
DEFUN ("text-properties-at", Ftext_properties_at,
       Stext_properties_at, 1, 2, 0,
571 572 573 574 575
       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)
576
     Lisp_Object position, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
577 578 579 580
{
  register INTERVAL i;

  if (NILP (object))
581
    XSETBUFFER (object, current_buffer);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
582

583
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
584 585
  if (NULL_INTERVAL_P (i))
    return Qnil;
586
  /* If POSITION is at the end of the interval,
587 588 589
     it means it's the end of OBJECT.
     There are no properties at the very end,
     since no character follows.  */
590
  if (XINT (position) == LENGTH (i) + i->position)
591
    return Qnil;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
592 593 594 595

  return i->plist;
}

596
DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
597 598 599 600
       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)
601
     Lisp_Object position, object;
602
     Lisp_Object prop;
603
{
604
  return textget (Ftext_properties_at (position, object), prop);
605 606
}

607 608 609 610 611 612 613 614 615 616 617 618 619
/* 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)
620
     Lisp_Object position, object;
621
     register Lisp_Object prop;
622
     Lisp_Object *overlay;
623 624 625
{
  struct window *w = 0;

626
  CHECK_NUMBER_COERCE_MARKER (position);
627 628

  if (NILP (object))
629
    XSETBUFFER (object, current_buffer);
630 631 632 633

  if (WINDOWP (object))
    {
      w = XWINDOW (object);
634
      object = w->buffer;
635 636 637
    }
  if (BUFFERP (object))
    {
638
      int posn = XINT (position);
639 640 641 642
      int noverlays;
      Lisp_Object *overlay_vec, tem;
      int next_overlay;
      int len;
643 644 645
      struct buffer *obuf = current_buffer;

      set_buffer_temp (XBUFFER (object));
646 647 648 649 650

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

651
      noverlays = overlays_at (posn, 0, &overlay_vec, &len,
652
			       &next_overlay, NULL, 0);
653 654 655 656 657 658 659

      /* 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));
660
	  noverlays = overlays_at (posn, 0, &overlay_vec, &len,
661
				   &next_overlay, NULL, 0);
662 663 664
	}
      noverlays = sort_overlays (overlay_vec, noverlays, w);

665 666
      set_buffer_temp (obuf);

667 668 669 670 671
      /* Now check the overlays in order of decreasing priority.  */
      while (--noverlays >= 0)
	{
	  tem = Foverlay_get (overlay_vec[noverlays], prop);
	  if (!NILP (tem))
672 673 674 675 676 677
	    {
	      if (overlay)
		/* Return the overlay we got the property from.  */
		*overlay = overlay_vec[noverlays];
	      return tem;
	    }
678 679
	}
    }
680 681 682 683 684

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

685 686
  /* Not a buffer, or no appropriate overlay, so fall through to the
     simpler case.  */
687 688 689 690
  return Fget_text_property (position, prop, object);
}

DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
691
       doc: /* Return the value of POSITION's property PROP, in OBJECT.
692
Both overlay properties and text properties are checked.
693 694 695 696 697 698 699
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)
700 701 702 703
     Lisp_Object position, object;
     register Lisp_Object prop;
{
  return get_char_property_and_overlay (position, prop, object, 0);
704
}
705 706 707

DEFUN ("next-char-property-change", Fnext_char_property_change,
       Snext_char_property_change, 1, 2, 0,
708 709 710 711 712 713 714 715 716
       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)
717 718 719 720 721 722 723
     Lisp_Object position, limit;
{
  Lisp_Object temp;

  temp = Fnext_overlay_change (position);
  if (! NILP (limit))
    {
724
      CHECK_NUMBER (limit);
725 726 727 728 729 730 731 732
      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,
733 734 735 736 737 738 739 740 741
       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)
742 743 744
     Lisp_Object position, limit;
{
  Lisp_Object temp;
745

746 747 748
  temp = Fprevious_overlay_change (position);
  if (! NILP (limit))
    {
749
      CHECK_NUMBER (limit);
750 751 752 753 754
      if (XINT (limit) > XINT (temp))
	temp = limit;
    }
  return Fprevious_property_change (position, Qnil, temp);
}
755 756


757 758
DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
       Snext_single_char_property_change, 2, 4, 0,
759 760 761 762 763 764 765 766 767 768
       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)
769
     Lisp_Object prop, position, object, limit;
770 771 772
{
  if (STRINGP (object))
    {
773 774
      position = Fnext_single_property_change (position, prop, object, limit);
      if (NILP (position))
775 776
	{
	  if (NILP (limit))
777
	    position = make_number (SCHARS (object));
778
	  else
779
	    position = limit;
780 781 782 783 784
	}
    }
  else
    {
      Lisp_Object initial_value, value;
Juanma Barranquero's avatar
Juanma Barranquero committed
785
      int count = SPECPDL_INDEX ();
786

787
      if (! NILP (object))
788
	CHECK_BUFFER (object);
789 790 791 792 793 794 795
      
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}

796
      initial_value = Fget_char_property (position, prop, object);
797
      
798 799 800
      if (NILP (limit))
	XSETFASTINT (limit, BUF_ZV (current_buffer));
      else
801
	CHECK_NUMBER_COERCE_MARKER (limit);
802 803

      for (;;)
804
	{
805 806 807 808 809 810 811
	  position = Fnext_char_property_change (position, limit);
	  if (XFASTINT (position) >= XFASTINT (limit)) {
	    position = limit;
	    break;
	  }

	  value = Fget_char_property (position, prop, object);
812 813 814 815 816 817 818
	  if (!EQ (value, initial_value))
	    break;
	}

      unbind_to (count, Qnil);
    }

819
  return position;
820 821
}

822 823 824
DEFUN ("previous-single-char-property-change",
       Fprevious_single_char_property_change,
       Sprevious_single_char_property_change, 2, 4, 0,
825 826 827 828 829 830 831 832 833 834
       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)
835 836 837 838 839 840 841 842
     Lisp_Object prop, position, object, limit;
{
  if (STRINGP (object))
    {
      position = Fprevious_single_property_change (position, prop, object, limit);
      if (NILP (position))
	{
	  if (NILP (limit))
843
	    position = make_number (SCHARS (object));
844 845 846 847 848 849
	  else
	    position = limit;
	}
    }
  else
    {
Juanma Barranquero's avatar
Juanma Barranquero committed
850
      int count = SPECPDL_INDEX ();
851 852

      if (! NILP (object))
853
	CHECK_BUFFER (object);
854 855 856 857 858 859 860 861 862 863
      
      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
864
	CHECK_NUMBER_COERCE_MARKER (limit);
865

866 867 868
      if (XFASTINT (position) <= XFASTINT (limit))
	position = limit;
      else
869
	{
870
	  Lisp_Object initial_value =
871 872
	    Fget_char_property (make_number (XFASTINT (position) - 1),
				prop, object);
873 874 875 876
      
	  for (;;)
	    {
	      position = Fprevious_char_property_change (position, limit);
877

878 879 880 881 882 883 884 885
	      if (XFASTINT (position) <= XFASTINT (limit))
		{
		  position = limit;
		  break;
		}
	      else
		{
		  Lisp_Object value =
886 887
		    Fget_char_property (make_number (XFASTINT (position) - 1),
					prop, object);
888 889 890 891 892

		  if (!EQ (value, initial_value))
		    break;
		}
	    }
893 894 895 896 897 898 899
	}

      unbind_to (count, Qnil);
    }

  return position;
}
900

Joseph Arceneaux's avatar
Joseph Arceneaux committed
901
DEFUN ("next-property-change", Fnext_property_change,
902
       Snext_property_change, 1, 3, 0,
903 904 905 906 907 908 909 910 911 912
       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)
913
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
914 915 916
{
  register INTERVAL i, next;

917
  if (NILP (object))
918
    XSETBUFFER (object, current_buffer);
919

920
  if (!NILP (limit) && !EQ (limit, Qt))
921
    CHECK_NUMBER_COERCE_MARKER (limit);
922

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

925 926 927 928
  /* If LIMIT is t, return start of next interval--don't
     bother checking further intervals.  */
  if (EQ (limit, Qt))
    {
929 930 931 932 933
      if (NULL_INTERVAL_P (i))
	next = i;
      else
	next = next_interval (i);
	
934
      if (NULL_INTERVAL_P (next))
935
	XSETFASTINT (position, (STRINGP (object)
936
				? SCHARS (object)
937
				: BUF_ZV (XBUFFER (object))));
938
      else
939
	XSETFASTINT (position, next->position);
940
      return position;
941 942
    }

943 944 945 946 947
  if (NULL_INTERVAL_P (i))
    return limit;

  next = next_interval (i);

948
  while (!NULL_INTERVAL_P (next) && intervals_equal (i, next)
949
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
950 951 952
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
953
    return limit;
954 955
  if (NILP (limit))
    XSETFASTINT (limit, (STRINGP (object)
956
			 ? SCHARS (object)
957 958
			 : BUF_ZV (XBUFFER (object))));
  if (!(next->position < XFASTINT (limit)))
959
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
960

961
  XSETFASTINT (position, next->position);
962
  return position;
963 964 965 966 967 968 969 970 971 972 973
}

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

974
  XSETBUFFER (object, current_buffer);
975
  XSETFASTINT (pos, beg);
976 977 978 979 980 981 982 983 984

  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);
985 986
      if (NULL_INTERVAL_P (next))
	return 0;
987
      if (next->position >= end)
988 989 990 991 992 993 994
	return 0;
    }

  if (NULL_INTERVAL_P (next))
    return 0;

  return 1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
995 996
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
997
DEFUN ("next-single-property-change", Fnext_single_property_change,
998
       Snext_single_property_change, 2, 4, 0,
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
       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)
1010
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1011 1012 1013 1014
{
  register INTERVAL i, next;
  register Lisp_Object here_val;

1015
  if (NILP (object))
1016
    XSETBUFFER (object, current_buffer);
1017

1018
  if (!NILP (limit))
1019
    CHECK_NUMBER_COERCE_MARKER (limit);
1020

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

1025
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1026
  next = next_interval (i);
1027
  while (! NULL_INTERVAL_P (next) 
1028
	 && EQ (here_val, textget (next->plist, prop))
1029
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1030 1031 1032
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
1033
    return limit;
1034 1035
  if (NILP (limit))
    XSETFASTINT (limit, (STRINGP (object)
1036
			 ? SCHARS (object)
1037 1038
			 : BUF_ZV (XBUFFER (object))));
  if (!(next->position < XFASTINT (limit)))
1039
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1040

1041
  return make_number (next->position);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1042 <