textprop.c 68.7 KB
Newer Older
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1
/* Interface code for dealing with text properties.
2
   Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000, 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
Lisp_Object Qminibuffer_prompt;
61 62 63

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

/* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to
   the o1's cdr.  Otherwise, return zero.  This is handy for
   traversing plists.  */
68
#define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCDR (o1), CONSP (o2)))
69

70
Lisp_Object Vinhibit_point_motion_hooks;
71
Lisp_Object Vdefault_text_properties;
72
Lisp_Object Vchar_property_alias_alist;
73
Lisp_Object Vtext_property_default_nonsticky;
74

75 76 77 78
/* verify_interval_modification saves insertion hooks here
   to be run later by report_interval_modification.  */
Lisp_Object interval_insert_behind_hooks;
Lisp_Object interval_insert_in_front_hooks;
79

80 81
static void text_read_only P_ ((Lisp_Object)) NO_RETURN;

82 83 84 85 86

/* 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
87 88
text_read_only (propval)
     Lisp_Object propval;
89
{
90 91 92 93
  if (STRINGP (propval))
    xsignal1 (Qtext_read_only, propval);

  xsignal0 (Qtext_read_only);
94 95 96
}


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

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

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

#define soft 0
#define hard 1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      if (! found)
	return 0;
    }

  return 1;
}

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

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

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

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

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

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

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

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

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

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

  return Qunbound;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

442 443
  UNGCPRO;

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

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

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

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

464
  current_plist = i->plist;
465 466

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return i->plist;
}

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

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

632
  CHECK_NUMBER_COERCE_MARKER (position);
633 634

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

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

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

652
      set_buffer_temp (XBUFFER (object));
653

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

657 658
      set_buffer_temp (obuf);

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

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

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

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

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

706 707 708 709 710 711 712 713 714 715 716 717 718
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
719
  return Fcons (val, overlay);
720 721
}

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

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

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

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

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


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

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

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

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

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

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

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

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

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

      unbind_to (count, Qnil);
    }

857
  return position;
858 859
}

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

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

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

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

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

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

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

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

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

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

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

      unbind_to (count, Qnil);
    }

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

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

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