textprop.c 68.6 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,
Glenn Morris's avatar
Glenn Morris committed
3
                 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Joseph Arceneaux's avatar
Joseph Arceneaux committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
11 12 13 14 15 16 17

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
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
19

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

#ifndef NULL
#define NULL (void *)0
#endif
29 30 31 32 33

/* 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
34 35 36 37 38 39


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

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

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

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

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

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

69
Lisp_Object Vinhibit_point_motion_hooks;
70
Lisp_Object Vdefault_text_properties;
71
Lisp_Object Vchar_property_alias_alist;
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
static void text_read_only P_ ((Lisp_Object)) NO_RETURN;

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 90 91 92
  if (STRINGP (propval))
    xsignal1 (Qtext_read_only, propval);

  xsignal0 (Qtext_read_only);
93 94 95
}


Joseph Arceneaux's avatar
Joseph Arceneaux committed
96

Joseph Arceneaux's avatar
Joseph Arceneaux committed
97 98 99 100 101 102
/* 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
103

104 105 106
   When OBJECT is a string, we increment *BEGIN and *END
   to make them origin-one.

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

#define soft 0
#define hard 1

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

131 132 133
  CHECK_STRING_OR_BUFFER (object);
  CHECK_NUMBER_COERCE_MARKER (*begin);
  CHECK_NUMBER_COERCE_MARKER (*end);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
134 135

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

  if (XINT (*begin) > XINT (*end))
    {
142 143
      Lisp_Object n;
      n = *begin;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
144
      *begin = *end;
145
      *end = n;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
146 147
    }

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

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

      searchpos = XINT (*begin);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
162 163 164
    }
  else
    {
165
      int len = SCHARS (object);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
166

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

175
      if (len == 0)
176 177 178
	return NULL_INTERVAL;

      searchpos = XINT (*begin);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
179 180 181 182
    }

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

184
  return find_interval (i, searchpos);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
185 186 187 188
}

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

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

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

static int
interval_has_all_properties (plist, i)
     Lisp_Object plist;
     INTERVAL i;
{
223
  register Lisp_Object tail1, tail2, sym1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
224 225
  register int found;

226
  /* Go through each element of PLIST.  */
227
  for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
228
    {
229
      sym1 = XCAR (tail1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
230 231 232
      found = 0;

      /* Go through I's plist, looking for sym1 */
233 234
      for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
	if (EQ (sym1, XCAR (tail2)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
235 236
	  {
	    /* Found the same property on both lists.  If the
237
	       values are unequal, return zero.  */
238
	    if (! EQ (Fcar (XCDR (tail1)), Fcar (XCDR (tail2))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
239 240
	      return 0;

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

      if (! found)
	return 0;
    }

  return 1;
}

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

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

263
  /* Go through each element of PLIST.  */
264
  for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
265
    {
266
      sym = XCAR (tail1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
267 268

      /* Go through i's plist, looking for tail1 */
269 270
      for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
	if (EQ (sym, XCAR (tail2)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
271 272 273 274 275
	  return 1;
    }

  return 0;
}
276 277 278 279 280 281 282 283 284 285 286 287

/* 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.  */
288
  for (tail1 = list; CONSP (tail1); tail1 = XCDR (tail1))
289 290 291 292
    {
      sym = Fcar (tail1);

      /* Go through i's plist, looking for tail1 */
293
      for (tail2 = i->plist; CONSP (tail2); tail2 = XCDR (XCDR (tail2)))
294 295 296 297 298 299
	if (EQ (sym, XCAR (tail2)))
	  return 1;
    }

  return 0;
}
300

301 302 303 304
/* Changing the plists of individual intervals.  */

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

  while (PLIST_ELT_P (plist, value))
312 313
    if (EQ (XCAR (plist), prop))
      return XCAR (value);
314
    else
315
      plist = XCDR (value);
316 317 318 319

  return Qunbound;
}

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

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

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

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

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

367 368
   OBJECT should be the string or buffer the interval is in.

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

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

391
  /* Go through each element of PLIST.  */
392
  for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
393
    {
394 395
      sym1 = XCAR (tail1);
      val1 = Fcar (XCDR (tail1));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
396 397 398
      found = 0;

      /* Go through I's plist, looking for sym1 */
399 400
      for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
	if (EQ (sym1, XCAR (tail2)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
401
	  {
402 403
	    /* No need to gcpro, because tail2 protects this
	       and it must be a cons cell (we get an error otherwise).  */
404
	    register Lisp_Object this_cdr;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
405

406
	    this_cdr = XCDR (tail2);
407
	    /* Found the property.  Now check its value.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
408 409 410
	    found = 1;

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

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

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

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

441 442
  UNGCPRO;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
443 444 445
  return changed;
}

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

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

460 461
  /* Nonzero means tail1 is a plist, otherwise it is a list.  */
  int use_plist;
462

463
  current_plist = i->plist;
464 465

  if (! NILP (plist))
466
    tail1 = plist, use_plist = 1;
467
  else
468
    tail1 = list, use_plist = 0;
469 470

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

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

483
	  current_plist = XCDR (XCDR (current_plist));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
484 485 486
	  changed++;
	}

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

499
	      Fsetcdr (XCDR (tail2), XCDR (XCDR (this)));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
500 501 502 503
	      changed++;
	    }
	  tail2 = this;
	}
504 505

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

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

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

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

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

548
  CHECK_STRING_OR_BUFFER (object);
549 550 551 552 553 554 555 556 557 558 559

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

      beg = BUF_BEGV (b);
      end = BUF_ZV (b);
      i = BUF_INTERVALS (b);
    }
  else
    {
560
      beg = 0;
561 562
      end = SCHARS (object);
      i = STRING_INTERVALS (object);
563 564 565
    }

  if (!(beg <= position && position <= end))
566
    args_out_of_range (make_number (position), make_number (position));
567 568
  if (beg == end || NULL_INTERVAL_P (i))
    return NULL_INTERVAL;
569

570 571 572
  return find_interval (i, position);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
573 574
DEFUN ("text-properties-at", Ftext_properties_at,
       Stext_properties_at, 1, 2, 0,
575
       doc: /* Return the list of properties of the character at POSITION in OBJECT.
576 577 578
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.
579 580
If POSITION is at the end of OBJECT, the value is nil.  */)
     (position, object)
581
     Lisp_Object position, object;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
582 583 584 585
{
  register INTERVAL i;

  if (NILP (object))
586
    XSETBUFFER (object, current_buffer);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
587

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

  return i->plist;
}

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

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

631
  CHECK_NUMBER_COERCE_MARKER (position);
632 633

  if (NILP (object))
634
    XSETBUFFER (object, current_buffer);
635 636 637 638

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

647 648 649 650
      if (XINT (position) < BUF_BEGV (XBUFFER (object))
	  || XINT (position) > BUF_ZV (XBUFFER (object)))
	xsignal1 (Qargs_out_of_range, position);

651
      set_buffer_temp (XBUFFER (object));
652

653
      GET_OVERLAYS_AT (XINT (position), overlay_vec, noverlays, NULL, 0);
654 655
      noverlays = sort_overlays (overlay_vec, noverlays, w);

656 657
      set_buffer_temp (obuf);

658 659 660
      /* Now check the overlays in order of decreasing priority.  */
      while (--noverlays >= 0)
	{
661
	  Lisp_Object tem = Foverlay_get (overlay_vec[noverlays], prop);
662
	  if (!NILP (tem))
663 664 665 666 667 668
	    {
	      if (overlay)
		/* Return the overlay we got the property from.  */
		*overlay = overlay_vec[noverlays];
	      return tem;
	    }
669 670
	}
    }
671 672 673 674 675

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

676 677
  /* Not a buffer, or no appropriate overlay, so fall through to the
     simpler case.  */
678 679 680 681
  return Fget_text_property (position, prop, object);
}

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

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.
700 701 702
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
703
found, or nil, if it was found as a text property or not found at all.
704

705 706 707 708 709 710 711 712 713 714 715 716 717
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);
Richard M. Stallman's avatar
Richard M. Stallman committed
718
  return Fcons (val, overlay);
719 720
}

721 722 723

DEFUN ("next-char-property-change", Fnext_char_property_change,
       Snext_char_property_change, 1, 2, 0,
724
       doc: /* Return the position of next text property or overlay change.
725 726 727
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.
728
If none is found up to (point-max), the function returns (point-max).
729

730
If the optional second argument LIMIT is non-nil, don't search
731 732
past position LIMIT; return LIMIT if nothing is found before LIMIT.
LIMIT is a no-op if it is greater than (point-max).  */)
733
     (position, limit)
734 735 736 737 738 739 740
     Lisp_Object position, limit;
{
  Lisp_Object temp;

  temp = Fnext_overlay_change (position);
  if (! NILP (limit))
    {
David Kastrup's avatar
David Kastrup committed
741
      CHECK_NUMBER_COERCE_MARKER (limit);
742 743 744 745 746 747 748 749
      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,
750
       doc: /* Return the position of previous text property or overlay change.
751 752 753
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.
754
If none is found since (point-min), the function returns (point-min).
755

756
If the optional second argument LIMIT is non-nil, don't search
757 758
past position LIMIT; return LIMIT if nothing is found before LIMIT.
LIMIT is a no-op if it is less than (point-min).  */)
759
     (position, limit)
760 761 762
     Lisp_Object position, limit;
{
  Lisp_Object temp;
763

764 765 766
  temp = Fprevious_overlay_change (position);
  if (! NILP (limit))
    {
David Kastrup's avatar
David Kastrup committed
767
      CHECK_NUMBER_COERCE_MARKER (limit);
768 769 770 771 772
      if (XINT (limit) > XINT (temp))
	temp = limit;
    }
  return Fprevious_property_change (position, Qnil, temp);
}
773 774


775 776
DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
       Snext_single_char_property_change, 2, 4, 0,
777 778 779
       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.
780 781 782 783
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.

784 785 786
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.

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

814
      if (! NILP (object))
815
	CHECK_BUFFER (object);
816

817 818 819 820 821 822
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}

David Kastrup's avatar
David Kastrup committed
823 824
      CHECK_NUMBER_COERCE_MARKER (position);

825
      initial_value = Fget_char_property (position, prop, object);
826

827
      if (NILP (limit))
828
	XSETFASTINT (limit, ZV);
829
      else
830
	CHECK_NUMBER_COERCE_MARKER (limit);
831

832
      if (XFASTINT (position) >= XFASTINT (limit))
833
	{
834 835 836
	  position = limit;
	  if (XFASTINT (position) > ZV)
	    XSETFASTINT (position, ZV);
837
	}
838 839 840 841 842 843 844 845 846 847 848 849 850 851
      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;
	  }
852 853 854 855

      unbind_to (count, Qnil);
    }

856
  return position;
857 858
}

859 860 861
DEFUN ("previous-single-char-property-change",
       Fprevious_single_char_property_change,
       Sprevious_single_char_property_change, 2, 4, 0,
862 863 864
       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.
865 866 867 868
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.

869 870 871
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.

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

      if (! NILP (object))
899
	CHECK_BUFFER (object);
900

901 902 903 904 905
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}
906

David Kastrup's avatar
David Kastrup committed
907 908
      CHECK_NUMBER_COERCE_MARKER (position);

909
      if (NILP (limit))
910
	XSETFASTINT (limit, BEGV);
911
      else
912
	CHECK_NUMBER_COERCE_MARKER (limit);
913

914
      if (XFASTINT (position) <= XFASTINT (limit))
915 916 917 918 919
	{
	  position = limit;
	  if (XFASTINT (position) < BEGV)
	    XSETFASTINT (position, BEGV);
	}
920
      else
921
	{
922 923 924
	  Lisp_Object initial_value
	    = Fget_char_property (make_number (XFASTINT (position) - 1),
				  prop, object);
925

926
	  while (1)
927 928
	    {
	      position = Fprevious_char_property_change (position, limit);
929

930 931 932 933 934 935 936
	      if (XFASTINT (position) <= XFASTINT (limit))
		{
		  position = limit;
		  break;
		}
	      else
		{
937 938 939
		  Lisp_Object value
		    = Fget_char_property (make_number (XFASTINT (position) - 1),
					  prop, object);
940 941 942 943 944

		  if (!EQ (value, initial_value))
		    break;
		}
	    }
945 946 947 948 949 950 951
	}

      unbind_to (count, Qnil);
    }

  return position;
}
952

Joseph Arceneaux's avatar
Joseph Arceneaux committed
953
DEFUN ("next-property-change", Fnext_property_change,
954
       Snext_property_change, 1, 3, 0,
955 956 957
       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.
958 959 960
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.
961 962 963 964 965 966
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)
967
     Lisp_Object position, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
968 969 970
{
  register INTERVAL i, next;

971
  if (NILP (object))
972
    XSETBUFFER (object, current_buffer);
973

974
  if (!NILP (limit) && !EQ (limit, Qt))
975
    CHECK_NUMBER_COERCE_MARKER (limit);
976

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

979 980 981 982
  /* If LIMIT is t, return start of next interval--don't
     bother checking further intervals.  */
  if (EQ (limit, Qt))
    {
983 984 985 986
      if (NULL_INTERVAL_P (i))
	next = i;
      else
	next = next_interval (i);
987

988
      if (NULL_INTERVAL_P (next))
989
	XSETFASTINT (position, (STRINGP (object)
990
				? SCHARS (object)
991
				: BUF_ZV (XBUFFER (object))));
992
      else
993
	XSETFASTINT (position, next->position);
994
      return position;
995 996
    }

997 998 999 1000 1001
  if (NULL_INTERVAL_P (i))
    return limit;

  next = next_interval (i);

1002
  while (!NULL_INTERVAL_P (next) && intervals_equal (i, next)
1003
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1004 1005
    next = next_interval (next);

1006 1007 1008 1009 1010 1011 1012
  if (NULL_INTERVAL_P (next)
      || (next->position
	  >= (INTEGERP (limit)
	      ? XFASTINT (limit)
	      : (STRINGP (object)
		 ? SCHARS (object)
		 : BUF_ZV (XBUFFER (object))))))
1013
    return limit;
1014 1015
  else
    return make_number (next->position);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
}

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

1027
  XSETBUFFER (object, current_buffer);
1028
  XSETFASTINT (pos, beg);
1029 1030 1031 1032 1033 1034 1035 1036 1037

  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);
1038 1039
      if (NULL_INTERVAL_P (next))
	return 0;
1040
      if (next->position >= end)
1041 1042 1043 1044 1045 1046 1047
	return 0;
    }

  if (NULL_INTERVAL_P (next))
    return 0;

  return 1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1048 1049
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
1050
DEFUN ("next-single-property-change", Fnext_single_property_change,
1051
       Snext_single_property_change, 2, 4, 0,
1052 1053 1054
       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.
1055 1056 1057
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.
1058 1059 1060 1061 1062 1063 1064
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)
1065
     Lisp_Object position, prop, object, limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1066 1067 1068 1069
{
  register INTERVAL i, next;
  register Lisp_Object here_val;

1070
  if (NILP (object))
1071
    XSETBUFFER (object, current_buffer);
1072

1073
  if (!NILP (limit))
1074
    CHECK_NUMBER_COERCE_MARKER (limit);
1075

1076
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1077
  if (NULL_INTERVAL_P (i))
1078
    return limit;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1079

1080
  here_val = textget (i->plist, prop);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1081
  next = next_interval (i);
1082
  while (! NULL_INTERVAL_P (next)
1083
	 && EQ (here_val, textget (next->plist, prop))
1084
	 && (NILP (limit) || next->position < XFASTINT (limit)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1085 1086
    next = next_interval (next);

1087 1088 1089 1090 1091 1092 1093
  if (NULL_INTERVAL_P