textprop.c 68.5 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, 2003,
3
                 2004, 2005, 2006 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
Lute Kamstra's avatar
Lute Kamstra committed
19 20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, 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


/* 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
86 87
text_read_only (propval)
     Lisp_Object propval;
88
{
89
  Fsignal (Qtext_read_only, STRINGP (propval) ? Fcons (propval, Qnil) : Qnil);
90 91 92
}


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

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

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

#define soft 0
#define hard 1

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

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

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

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

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

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

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

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

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

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

  if (NULL_INTERVAL_P (i))
    return (force ? create_root_interval (object) : i);
180

181
  return find_interval (i, searchpos);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
182 183 184 185
}

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

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

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

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

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

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

      if (! found)
	return 0;
    }

  return 1;
}

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

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

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

/* 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;
}
297

298 299 300 301
/* Changing the plists of individual intervals.  */

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

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

  return Qunbound;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

438 439
  UNGCPRO;

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

443 444 445
/* 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.)
446
   OBJECT is the string or buffer containing I.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
447

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

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

460
  current_plist = i->plist;
461 462

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  if (!(beg <= position && position <= end))
563
    args_out_of_range (make_number (position), make_number (position));
564 565
  if (beg == end || NULL_INTERVAL_P (i))
    return NULL_INTERVAL;
566

567 568 569
  return find_interval (i, position);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
570 571
DEFUN ("text-properties-at", Ftext_properties_at,
       Stext_properties_at, 1, 2, 0,
572
       doc: /* Return the list of properties of the character at POSITION in OBJECT.
573 574 575
If the optional second argument OBJECT is a buffer (or nil, which means
the current buffer), POSITION is a buffer position (integer or marker).
If OBJECT is a string, POSITION is a 0-based index into it.
576 577
If POSITION is at the end of OBJECT, the value is nil.  */)
     (position, object)
578
     Lisp_Object position, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
579 580 581 582
{
  register INTERVAL i;

  if (NILP (object))
583
    XSETBUFFER (object, current_buffer);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
584

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

  return i->plist;
}

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

609
/* Return the value of char's property PROP, in OBJECT at POSITION.
610 611 612 613 614 615 616 617 618 619 620 621
   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)
622
     Lisp_Object position, object;
623
     register Lisp_Object prop;
624
     Lisp_Object *overlay;
625 626 627
{
  struct window *w = 0;

628
  CHECK_NUMBER_COERCE_MARKER (position);
629 630

  if (NILP (object))
631
    XSETBUFFER (object, current_buffer);
632 633 634 635

  if (WINDOWP (object))
    {
      w = XWINDOW (object);
636
      object = w->buffer;
637 638 639 640
    }
  if (BUFFERP (object))
    {
      int noverlays;
641
      Lisp_Object *overlay_vec;
642 643 644
      struct buffer *obuf = current_buffer;

      set_buffer_temp (XBUFFER (object));
645

646
      GET_OVERLAYS_AT (XINT (position), overlay_vec, noverlays, NULL, 0);
647 648
      noverlays = sort_overlays (overlay_vec, noverlays, w);

649 650
      set_buffer_temp (obuf);

651 652 653
      /* Now check the overlays in order of decreasing priority.  */
      while (--noverlays >= 0)
	{
654
	  Lisp_Object tem = Foverlay_get (overlay_vec[noverlays], prop);
655
	  if (!NILP (tem))
656 657 658 659 660 661
	    {
	      if (overlay)
		/* Return the overlay we got the property from.  */
		*overlay = overlay_vec[noverlays];
	      return tem;
	    }
662 663
	}
    }
664 665 666 667 668

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

669 670
  /* Not a buffer, or no appropriate overlay, so fall through to the
     simpler case.  */
671 672 673 674
  return Fget_text_property (position, prop, object);
}

DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
675
       doc: /* Return the value of POSITION's property PROP, in OBJECT.
676
Both overlay properties and text properties are checked.
677 678 679 680 681 682 683
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)
684 685 686 687
     Lisp_Object position, object;
     register Lisp_Object prop;
{
  return get_char_property_and_overlay (position, prop, object, 0);
688
}
689 690 691 692

DEFUN ("get-char-property-and-overlay", Fget_char_property_and_overlay,
       Sget_char_property_and_overlay, 2, 3, 0,
       doc: /* Like `get-char-property', but with extra overlay information.
693 694 695
The value is a cons cell.  Its car is the return value of `get-char-property'
with the same arguments--that is, the value of POSITION's property
PROP in OBJECT.  Its cdr is the overlay in which the property was
696
found, or nil, if it was found as a text property or not found at all.
697

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
OBJECT is optional and defaults to the current buffer.  OBJECT may be
a string, a buffer or a window.  For strings, the cdr of the return
value is always nil, since strings do not have overlays.  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.  If
POSITION is at the end of OBJECT, both car and cdr are nil.  */)
     (position, prop, object)
     Lisp_Object position, object;
     register Lisp_Object prop;
{
  Lisp_Object overlay;
  Lisp_Object val
    = get_char_property_and_overlay (position, prop, object, &overlay);
  return Fcons(val, overlay);
}

714 715 716

DEFUN ("next-char-property-change", Fnext_char_property_change,
       Snext_char_property_change, 1, 2, 0,
717
       doc: /* Return the position of next text property or overlay change.
718 719 720
This scans characters forward in the current buffer 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.
721
If none is found up to (point-max), the function returns (point-max).
722

723
If the optional second argument LIMIT is non-nil, don't search
724 725
past position LIMIT; return LIMIT if nothing is found before LIMIT.
LIMIT is a no-op if it is greater than (point-max).  */)
726
     (position, limit)
727 728 729 730 731 732 733
     Lisp_Object position, limit;
{
  Lisp_Object temp;

  temp = Fnext_overlay_change (position);
  if (! NILP (limit))
    {
David Kastrup's avatar
David Kastrup committed
734
      CHECK_NUMBER_COERCE_MARKER (limit);
735 736 737 738 739 740 741 742
      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,
743
       doc: /* Return the position of previous text property or overlay change.
744 745 746
Scans characters backward in the current buffer 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.
747
If none is found since (point-min), the function returns (point-min).
748

749
If the optional second argument LIMIT is non-nil, don't search
750 751
past position LIMIT; return LIMIT if nothing is found before LIMIT.
LIMIT is a no-op if it is less than (point-min).  */)
752
     (position, limit)
753 754 755
     Lisp_Object position, limit;
{
  Lisp_Object temp;
756

757 758 759
  temp = Fprevious_overlay_change (position);
  if (! NILP (limit))
    {
David Kastrup's avatar
David Kastrup committed
760
      CHECK_NUMBER_COERCE_MARKER (limit);
761 762 763 764 765
      if (XINT (limit) > XINT (temp))
	temp = limit;
    }
  return Fprevious_property_change (position, Qnil, temp);
}
766 767


768 769
DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
       Snext_single_char_property_change, 2, 4, 0,
770 771 772
       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.
773 774 775 776
If the optional third argument OBJECT is a buffer (or nil, which means
the current buffer), POSITION is a buffer position (integer or marker).
If OBJECT is a string, POSITION is a 0-based index into it.

777 778 779
In a string, scan runs to the end of the string.
In a buffer, it runs to (point-max), and the value cannot exceed that.

780 781 782 783 784 785
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)
786
     Lisp_Object prop, position, object, limit;
787 788 789
{
  if (STRINGP (object))
    {
790 791
      position = Fnext_single_property_change (position, prop, object, limit);
      if (NILP (position))
792 793
	{
	  if (NILP (limit))
794
	    position = make_number (SCHARS (object));
795
	  else
David Kastrup's avatar
David Kastrup committed
796 797 798 799
	    {
	      CHECK_NUMBER (limit);
	      position = limit;
	    }
800 801 802 803 804
	}
    }
  else
    {
      Lisp_Object initial_value, value;
Juanma Barranquero's avatar
Juanma Barranquero committed
805
      int count = SPECPDL_INDEX ();
806

807
      if (! NILP (object))
808
	CHECK_BUFFER (object);
809

810 811 812 813 814 815
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}

David Kastrup's avatar
David Kastrup committed
816 817
      CHECK_NUMBER_COERCE_MARKER (position);

818
      initial_value = Fget_char_property (position, prop, object);
819

820
      if (NILP (limit))
821
	XSETFASTINT (limit, ZV);
822
      else
823
	CHECK_NUMBER_COERCE_MARKER (limit);
824

825
      if (XFASTINT (position) >= XFASTINT (limit))
826
	{
827 828 829
	  position = limit;
	  if (XFASTINT (position) > ZV)
	    XSETFASTINT (position, ZV);
830
	}
831 832 833 834 835 836 837 838 839 840 841 842 843 844
      else
	while (1)
	  {
	    position = Fnext_char_property_change (position, limit);
	    if (XFASTINT (position) >= XFASTINT (limit))
	      {
		position = limit;
		break;
	      }

	    value = Fget_char_property (position, prop, object);
	    if (!EQ (value, initial_value))
	      break;
	  }
845 846 847 848

      unbind_to (count, Qnil);
    }

849
  return position;
850 851
}

852 853 854
DEFUN ("previous-single-char-property-change",
       Fprevious_single_char_property_change,
       Sprevious_single_char_property_change, 2, 4, 0,
855 856 857
       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.
858 859 860 861
If the optional third argument OBJECT is a buffer (or nil, which means
the current buffer), POSITION is a buffer position (integer or marker).
If OBJECT is a string, POSITION is a 0-based index into it.

862 863 864
In a string, scan runs to the start of the string.
In a buffer, it runs to (point-min), and the value cannot be less than that.

865 866 867 868 869 870
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)
871 872 873 874 875 876 877 878
     Lisp_Object prop, position, object, limit;
{
  if (STRINGP (object))
    {
      position = Fprevious_single_property_change (position, prop, object, limit);
      if (NILP (position))
	{
	  if (NILP (limit))
879
	    position = make_number (SCHARS (object));
880
	  else
David Kastrup's avatar
David Kastrup committed
881 882 883 884
	    {
	      CHECK_NUMBER (limit);
	      position = limit;
	    }
885 886 887 888
	}
    }
  else
    {
Juanma Barranquero's avatar
Juanma Barranquero committed
889
      int count = SPECPDL_INDEX ();
890 891

      if (! NILP (object))
892
	CHECK_BUFFER (object);
893

894 895 896 897 898
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}
899

David Kastrup's avatar
David Kastrup committed
900 901
      CHECK_NUMBER_COERCE_MARKER (position);

902
      if (NILP (limit))
903
	XSETFASTINT (limit, BEGV);
904
      else
905
	CHECK_NUMBER_COERCE_MARKER (limit);
906

907
      if (XFASTINT (position) <= XFASTINT (limit))
908 909 910 911 912
	{
	  position = limit;
	  if (XFASTINT (position) < BEGV)
	    XSETFASTINT (position, BEGV);
	}
913
      else
914
	{
915 916 917
	  Lisp_Object initial_value
	    = Fget_char_property (make_number (XFASTINT (position) - 1),
				  prop, object);
918

919
	  while (1)
920 921
	    {
	      position = Fprevious_char_property_change (position, limit);
922

923 924 925 926 927 928 929
	      if (XFASTINT (position) <= XFASTINT (limit))
		{
		  position = limit;
		  break;
		}
	      else
		{
930 931 932
		  Lisp_Object value
		    = Fget_char_property (make_number (XFASTINT (position) - 1),
					  prop, object);
933 934 935 936 937

		  if (!EQ (value, initial_value))
		    break;
		}
	    }
938 939 940 941 942 943 944
	}

      unbind_to (count, Qnil);
    }

  return position;
}
945

Joseph Arceneaux's avatar
Joseph Arceneaux committed
946
DEFUN ("next-property-change", Fnext_property_change,
947
       Snext_property_change, 1, 3, 0,
948 949 950
       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.
951 952 953
If the optional second argument OBJECT is a buffer (or nil, which means
the current buffer), POSITION is a buffer position (integer or marker).
If OBJECT is a string, POSITION is a 0-based index into it.
954 955 956 957 958 959
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)
960
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
961 962 963
{
  register INTERVAL i, next;

964
  if (NILP (object))
965
    XSETBUFFER (object, current_buffer);
966

967
  if (!NILP (limit) && !EQ (limit, Qt))
968
    CHECK_NUMBER_COERCE_MARKER (limit);
969

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

972 973 974 975
  /* If LIMIT is t, return start of next interval--don't
     bother checking further intervals.  */
  if (EQ (limit, Qt))
    {
976 977 978 979
      if (NULL_INTERVAL_P (i))
	next = i;
      else
	next = next_interval (i);
980

981
      if (NULL_INTERVAL_P (next))
982
	XSETFASTINT (position, (STRINGP (object)
983
				? SCHARS (object)
984
				: BUF_ZV (XBUFFER (object))));
985
      else
986
	XSETFASTINT (position, next->position);
987
      return position;
988 989
    }

990 991 992 993 994
  if (NULL_INTERVAL_P (i))
    return limit;

  next = next_interval (i);

995
  while (!NULL_INTERVAL_P (next) && intervals_equal (i, next)
996
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
997 998 999
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
1000
    return limit;
1001 1002
  if (NILP (limit))
    XSETFASTINT (limit, (STRINGP (object)
1003
			 ? SCHARS (object)
1004 1005
			 : BUF_ZV (XBUFFER (object))));
  if (!(next->position < XFASTINT (limit)))
1006
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1007

1008
  XSETFASTINT (position, next->position);
1009
  return position;
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
}

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

1021
  XSETBUFFER (object, current_buffer);
1022
  XSETFASTINT (pos, beg);
1023 1024 1025 1026 1027 1028 1029 1030 1031

  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);
1032 1033
      if (NULL_INTERVAL_P (next))
	return 0;
1034
      if (next->position >= end)
1035 1036 1037 1038 1039 1040 1041
	return 0;
    }

  if (NULL_INTERVAL_P (next))
    return 0;

  return 1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1042 1043
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1044
DEFUN ("next-single-property-change", Fnext_single_property_change,
1045
       Snext_single_property_change, 2, 4, 0,
1046 1047 1048
       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.
1049 1050 1051
If the optional third argument OBJECT is a buffer (or nil, which means
the current buffer), POSITION is a buffer position (integer or marker).
If OBJECT is a string, POSITION is a 0-based index into it.
1052 1053 1054 1055 1056 1057 1058
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)
1059
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1060 1061 1062 1063
{
  register INTERVAL i, next;
  register Lisp_Object here_val;

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

1067
  if (!NILP (limit))
1068
    CHECK_NUMBER_COERCE_MARKER (limit);
1069

1070
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1071
  if (NULL_INTERVAL_P (i))
1072
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1073

1074
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1075
  next = next_interval (i);
1076
  while (! NULL_INTERVAL_P (next)
1077
	 && EQ (here_val, textget (next->plist, prop))
1078
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1079 1080 1081
    next = next_interval (next);

  if (NULL_INTERVAL_P (next))
1082
    return limit;
1083 1084
  if (NILP (limit))
    XSETFASTINT (limit, (STRINGP (object)
1085
			 ? SCHARS (object)
1086 1087
			 : BUF_ZV (XBUFFER (object))));
  if (!(next->position < XFASTINT (limit)))
1088
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1089

1090
  return make_number (next->position);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1091 1092
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1093
DEFUN ("previous-property-change", Fprevious_property_change,
1094
       Sprevious_property_change, 1, 3, 0,
1095 1096 1097
       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.
1098 1099 1100
If the optional second argument OBJECT is a buffer (or nil, which means
the current buffer), POSITION is a buffer position (integer or marker).
If OBJECT is a string, POSITION is a 0-based index into it.
1101 1102 1103 1104 1105 1106
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)
1107
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1108 1109 1110
{
  register INTERVAL i, previous;

1111
  if (NILP (object))
1112
    XSETBUFFER (object, current_buffer);
1113

1114
  if (!NILP (limit))
1115
    CHECK_NUMBER_COERCE_MARKER (limit);
Karl Heuer's avatar