textprop.c 68.2 KB
Newer Older
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1
/* Interface code for dealing with text properties.
2
   Copyright (C) 1993-1995, 1997, 1999-2011 Free Software Foundation, Inc.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
3 4 5

This file is part of GNU Emacs.

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

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

19
#include <config.h>
20
#include <setjmp.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 71 72 73
/* 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;
74

75
static void text_read_only (Lisp_Object) NO_RETURN;
76

77 78 79 80 81

/* 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
82
text_read_only (Lisp_Object propval)
83
{
84 85 86 87
  if (STRINGP (propval))
    xsignal1 (Qtext_read_only, propval);

  xsignal0 (Qtext_read_only);
88 89 90
}


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

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

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

#define soft 0
#define hard 1

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

124 125 126
  CHECK_STRING_OR_BUFFER (object);
  CHECK_NUMBER_COERCE_MARKER (*begin);
  CHECK_NUMBER_COERCE_MARKER (*end);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
127 128

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

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

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

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

      searchpos = XINT (*begin);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
155 156 157
    }
  else
    {
158
      EMACS_INT len = SCHARS (object);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
159

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

168
      if (len == 0)
169 170 171
	return NULL_INTERVAL;

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

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

177
  return find_interval (i, searchpos);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
178 179 180 181
}

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

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

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

static int
211
interval_has_all_properties (Lisp_Object plist, INTERVAL i)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
212
{
213
  register Lisp_Object tail1, tail2, sym1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
214 215
  register int found;

216
  /* Go through each element of PLIST.  */
217
  for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
218
    {
219
      sym1 = XCAR (tail1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
220 221 222
      found = 0;

      /* Go through I's plist, looking for sym1 */
223 224
      for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
	if (EQ (sym1, XCAR (tail2)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
225 226
	  {
	    /* Found the same property on both lists.  If the
227
	       values are unequal, return zero.  */
228
	    if (! EQ (Fcar (XCDR (tail1)), Fcar (XCDR (tail2))))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
229 230
	      return 0;

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

      if (! found)
	return 0;
    }

  return 1;
}

/* Return nonzero if the plist of interval I has any of the
244
   properties of PLIST, regardless of their values.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
245 246

static INLINE int
247
interval_has_some_properties (Lisp_Object plist, INTERVAL i)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
248 249 250
{
  register Lisp_Object tail1, tail2, sym;

251
  /* Go through each element of PLIST.  */
252
  for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
253
    {
254
      sym = XCAR (tail1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
255 256

      /* Go through i's plist, looking for tail1 */
257 258
      for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
	if (EQ (sym, XCAR (tail2)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
259 260 261 262 263
	  return 1;
    }

  return 0;
}
264 265 266 267 268

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

static INLINE int
269
interval_has_some_properties_list (Lisp_Object list, INTERVAL i)
270 271 272 273
{
  register Lisp_Object tail1, tail2, sym;

  /* Go through each element of LIST.  */
274
  for (tail1 = list; CONSP (tail1); tail1 = XCDR (tail1))
275 276 277 278
    {
      sym = Fcar (tail1);

      /* Go through i's plist, looking for tail1 */
279
      for (tail2 = i->plist; CONSP (tail2); tail2 = XCDR (XCDR (tail2)))
280 281 282 283 284 285
	if (EQ (sym, XCAR (tail2)))
	  return 1;
    }

  return 0;
}
286

287 288 289 290
/* Changing the plists of individual intervals.  */

/* Return the value of PROP in property-list PLIST, or Qunbound if it
   has none.  */
291
static Lisp_Object
292
property_value (Lisp_Object plist, Lisp_Object prop)
293 294 295 296
{
  Lisp_Object value;

  while (PLIST_ELT_P (plist, value))
297 298
    if (EQ (XCAR (plist), prop))
      return XCAR (value);
299
    else
300
      plist = XCDR (value);
301 302 303 304

  return Qunbound;
}

305 306 307 308 309
/* 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
310
set_properties (Lisp_Object properties, INTERVAL interval, Lisp_Object object)
311
{
312
  Lisp_Object sym, value;
313

314
  if (BUFFERP (object))
315
    {
316 317 318 319
      /* 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);
320 321 322
	   sym = XCDR (value))
	if (! EQ (property_value (properties, XCAR (sym)),
		  XCAR (value)))
323 324
	  {
	    record_property_change (interval->position, LENGTH (interval),
325
				    XCAR (sym), XCAR (value),
326 327
				    object);
	  }
328 329 330 331 332

      /* 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);
333 334
	   sym = XCDR (value))
	if (EQ (property_value (interval->plist, XCAR (sym)), Qunbound))
335 336
	  {
	    record_property_change (interval->position, LENGTH (interval),
337
				    XCAR (sym), Qnil,
338 339
				    object);
	  }
340 341 342 343 344
    }

  /* Store new properties.  */
  interval->plist = Fcopy_sequence (properties);
}
Joseph Arceneaux's avatar
Joseph Arceneaux committed
345 346 347 348 349

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

350 351
   OBJECT should be the string or buffer the interval is in.

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

355
static int
356
add_properties (Lisp_Object plist, INTERVAL i, Lisp_Object object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
357
{
358
  Lisp_Object tail1, tail2, sym1, val1;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
359 360
  register int changed = 0;
  register int found;
361 362 363 364 365 366 367 368 369
  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
370

371
  /* Go through each element of PLIST.  */
372
  for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
373
    {
374 375
      sym1 = XCAR (tail1);
      val1 = Fcar (XCDR (tail1));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
376 377 378
      found = 0;

      /* Go through I's plist, looking for sym1 */
379 380
      for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
	if (EQ (sym1, XCAR (tail2)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
381
	  {
382 383
	    /* No need to gcpro, because tail2 protects this
	       and it must be a cons cell (we get an error otherwise).  */
384
	    register Lisp_Object this_cdr;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
385

386
	    this_cdr = XCDR (tail2);
387
	    /* Found the property.  Now check its value.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
388 389 390
	    found = 1;

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

395
	    /* Record this change in the buffer, for undo purposes.  */
396
	    if (BUFFERP (object))
397
	      {
398 399
		record_property_change (i->position, LENGTH (i),
					sym1, Fcar (this_cdr), object);
400 401
	      }

Joseph Arceneaux's avatar
Joseph Arceneaux committed
402 403 404 405 406 407 408 409
	    /* I's property has a different value -- change it */
	    Fsetcar (this_cdr, val1);
	    changed++;
	    break;
	  }

      if (! found)
	{
410
	  /* Record this change in the buffer, for undo purposes.  */
411
	  if (BUFFERP (object))
412
	    {
413 414
	      record_property_change (i->position, LENGTH (i),
				      sym1, Qnil, object);
415
	    }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
416 417 418 419 420
	  i->plist = Fcons (sym1, Fcons (val1, i->plist));
	  changed++;
	}
    }

421 422
  UNGCPRO;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
423 424 425
  return changed;
}

426 427 428
/* 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.)
429
   OBJECT is the string or buffer containing I.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
430

431
static int
432
remove_properties (Lisp_Object plist, Lisp_Object list, INTERVAL i, Lisp_Object object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
433
{
434
  register Lisp_Object tail1, tail2, sym, current_plist;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
435 436
  register int changed = 0;

437 438
  /* Nonzero means tail1 is a plist, otherwise it is a list.  */
  int use_plist;
439

440
  current_plist = i->plist;
441 442

  if (! NILP (plist))
443
    tail1 = plist, use_plist = 1;
444
  else
445
    tail1 = list, use_plist = 0;
446 447

  /* Go through each element of LIST or PLIST.  */
448
  while (CONSP (tail1))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
449
    {
450
      sym = XCAR (tail1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
451

452
      /* First, remove the symbol if it's at the head of the list */
453
      while (CONSP (current_plist) && EQ (sym, XCAR (current_plist)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
454
	{
455
	  if (BUFFERP (object))
456 457 458
	    record_property_change (i->position, LENGTH (i),
				    sym, XCAR (XCDR (current_plist)),
				    object);
459

460
	  current_plist = XCDR (XCDR (current_plist));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
461 462 463
	  changed++;
	}

464
      /* Go through I's plist, looking for SYM.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
465 466 467
      tail2 = current_plist;
      while (! NILP (tail2))
	{
468
	  register Lisp_Object this;
469
	  this = XCDR (XCDR (tail2));
470
	  if (CONSP (this) && EQ (sym, XCAR (this)))
Joseph Arceneaux's avatar
Joseph Arceneaux committed
471
	    {
472
	      if (BUFFERP (object))
473 474
		record_property_change (i->position, LENGTH (i),
					sym, XCAR (XCDR (this)), object);
475

476
	      Fsetcdr (XCDR (tail2), XCDR (XCDR (this)));
Joseph Arceneaux's avatar
Joseph Arceneaux committed
477 478 479 480
	      changed++;
	    }
	  tail2 = this;
	}
481 482

      /* Advance thru TAIL1 one way or the other.  */
483 484
      tail1 = XCDR (tail1);
      if (use_plist && CONSP (tail1))
485
	tail1 = XCDR (tail1);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
486 487 488 489 490 491 492
    }

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

493
#if 0
Joseph Arceneaux's avatar
Joseph Arceneaux committed
494
/* Remove all properties from interval I.  Return non-zero
495
   if this changes the interval.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
496 497

static INLINE int
Andreas Schwab's avatar
Andreas Schwab committed
498
erase_properties (INTERVAL i)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
499 500 501 502 503 504 505
{
  if (NILP (i->plist))
    return 0;

  i->plist = Qnil;
  return 1;
}
506
#endif
Joseph Arceneaux's avatar
Joseph Arceneaux committed
507

508
/* Returns the interval of POSITION in OBJECT.
509 510 511
   POSITION is BEG-based.  */

INTERVAL
512
interval_of (int position, Lisp_Object object)
513 514
{
  register INTERVAL i;
515
  EMACS_INT beg, end;
516 517 518

  if (NILP (object))
    XSETBUFFER (object, current_buffer);
Karl Heuer's avatar
Karl Heuer committed
519 520
  else if (EQ (object, Qt))
    return NULL_INTERVAL;
521

522
  CHECK_STRING_OR_BUFFER (object);
523 524 525 526 527 528 529 530 531 532 533

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

      beg = BUF_BEGV (b);
      end = BUF_ZV (b);
      i = BUF_INTERVALS (b);
    }
  else
    {
534
      beg = 0;
535 536
      end = SCHARS (object);
      i = STRING_INTERVALS (object);
537 538 539
    }

  if (!(beg <= position && position <= end))
540
    args_out_of_range (make_number (position), make_number (position));
541 542
  if (beg == end || NULL_INTERVAL_P (i))
    return NULL_INTERVAL;
543

544 545 546
  return find_interval (i, position);
}

Joseph Arceneaux's avatar
Joseph Arceneaux committed
547 548
DEFUN ("text-properties-at", Ftext_properties_at,
       Stext_properties_at, 1, 2, 0,
549
       doc: /* Return the list of properties of the character at POSITION in OBJECT.
550 551 552
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.
553
If POSITION is at the end of OBJECT, the value is nil.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
554
  (Lisp_Object position, Lisp_Object object)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
555 556 557 558
{
  register INTERVAL i;

  if (NILP (object))
559
    XSETBUFFER (object, current_buffer);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
560

561
  i = validate_interval_range (object, &position, &position, soft);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
562 563
  if (NULL_INTERVAL_P (i))
    return Qnil;
564
  /* If POSITION is at the end of the interval,
565 566 567
     it means it's the end of OBJECT.
     There are no properties at the very end,
     since no character follows.  */
568
  if (XINT (position) == LENGTH (i) + i->position)
569
    return Qnil;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
570 571 572 573

  return i->plist;
}

574
DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
575 576 577
       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.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
578
  (Lisp_Object position, Lisp_Object prop, Lisp_Object object)
579
{
580
  return textget (Ftext_properties_at (position, object), prop);
581 582
}

583
/* Return the value of char's property PROP, in OBJECT at POSITION.
584 585 586 587 588 589 590 591 592 593 594
   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
595
get_char_property_and_overlay (Lisp_Object position, register Lisp_Object prop, Lisp_Object object, Lisp_Object *overlay)
596 597 598
{
  struct window *w = 0;

599
  CHECK_NUMBER_COERCE_MARKER (position);
600 601

  if (NILP (object))
602
    XSETBUFFER (object, current_buffer);
603 604 605 606

  if (WINDOWP (object))
    {
      w = XWINDOW (object);
607
      object = w->buffer;
608 609 610 611
    }
  if (BUFFERP (object))
    {
      int noverlays;
612
      Lisp_Object *overlay_vec;
613 614
      struct buffer *obuf = current_buffer;

615 616 617 618
      if (XINT (position) < BUF_BEGV (XBUFFER (object))
	  || XINT (position) > BUF_ZV (XBUFFER (object)))
	xsignal1 (Qargs_out_of_range, position);

619
      set_buffer_temp (XBUFFER (object));
620

621
      GET_OVERLAYS_AT (XINT (position), overlay_vec, noverlays, NULL, 0);
622 623
      noverlays = sort_overlays (overlay_vec, noverlays, w);

624 625
      set_buffer_temp (obuf);

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

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

644 645
  /* Not a buffer, or no appropriate overlay, so fall through to the
     simpler case.  */
646 647 648 649
  return Fget_text_property (position, prop, object);
}

DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
650
       doc: /* Return the value of POSITION's property PROP, in OBJECT.
651
Both overlay properties and text properties are checked.
652 653 654 655 656 657
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.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
658
  (Lisp_Object position, Lisp_Object prop, Lisp_Object object)
659 660
{
  return get_char_property_and_overlay (position, prop, object, 0);
661
}
662 663 664 665

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.
666 667 668
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
669
found, or nil, if it was found as a text property or not found at all.
670

671 672 673 674 675 676
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.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
677
  (Lisp_Object position, Lisp_Object prop, Lisp_Object object)
678 679 680 681
{
  Lisp_Object overlay;
  Lisp_Object val
    = get_char_property_and_overlay (position, prop, object, &overlay);
Richard M. Stallman's avatar
Richard M. Stallman committed
682
  return Fcons (val, overlay);
683 684
}

685 686 687

DEFUN ("next-char-property-change", Fnext_char_property_change,
       Snext_char_property_change, 1, 2, 0,
688
       doc: /* Return the position of next text property or overlay change.
689 690 691
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.
692
If none is found up to (point-max), the function returns (point-max).
693

694
If the optional second argument LIMIT is non-nil, don't search
695 696
past position LIMIT; return LIMIT if nothing is found before LIMIT.
LIMIT is a no-op if it is greater than (point-max).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
697
  (Lisp_Object position, Lisp_Object limit)
698 699 700 701 702 703
{
  Lisp_Object temp;

  temp = Fnext_overlay_change (position);
  if (! NILP (limit))
    {
David Kastrup's avatar
David Kastrup committed
704
      CHECK_NUMBER_COERCE_MARKER (limit);
705 706 707 708 709 710 711 712
      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,
713
       doc: /* Return the position of previous text property or overlay change.
714 715 716
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.
717
If none is found since (point-min), the function returns (point-min).
718

719
If the optional second argument LIMIT is non-nil, don't search
720 721
past position LIMIT; return LIMIT if nothing is found before LIMIT.
LIMIT is a no-op if it is less than (point-min).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
722
  (Lisp_Object position, Lisp_Object limit)
723 724
{
  Lisp_Object temp;
725

726 727 728
  temp = Fprevious_overlay_change (position);
  if (! NILP (limit))
    {
David Kastrup's avatar
David Kastrup committed
729
      CHECK_NUMBER_COERCE_MARKER (limit);
730 731 732 733 734
      if (XINT (limit) > XINT (temp))
	temp = limit;
    }
  return Fprevious_property_change (position, Qnil, temp);
}
735 736


737 738
DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
       Snext_single_char_property_change, 2, 4, 0,
739 740 741
       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.
742 743 744 745
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.

746 747 748
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.

749 750 751 752 753
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.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
754
  (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit)
755 756 757
{
  if (STRINGP (object))
    {
758 759
      position = Fnext_single_property_change (position, prop, object, limit);
      if (NILP (position))
760 761
	{
	  if (NILP (limit))
762
	    position = make_number (SCHARS (object));
763
	  else
David Kastrup's avatar
David Kastrup committed
764 765 766 767
	    {
	      CHECK_NUMBER (limit);
	      position = limit;
	    }
768 769 770 771 772
	}
    }
  else
    {
      Lisp_Object initial_value, value;
Juanma Barranquero's avatar
Juanma Barranquero committed
773
      int count = SPECPDL_INDEX ();
774

775
      if (! NILP (object))
776
	CHECK_BUFFER (object);
777

778 779 780 781 782 783
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}

David Kastrup's avatar
David Kastrup committed
784 785
      CHECK_NUMBER_COERCE_MARKER (position);

786
      initial_value = Fget_char_property (position, prop, object);
787

788
      if (NILP (limit))
789
	XSETFASTINT (limit, ZV);
790
      else
791
	CHECK_NUMBER_COERCE_MARKER (limit);
792

793
      if (XFASTINT (position) >= XFASTINT (limit))
794
	{
795 796 797
	  position = limit;
	  if (XFASTINT (position) > ZV)
	    XSETFASTINT (position, ZV);
798
	}
799 800 801 802 803 804 805 806 807 808 809 810 811 812
      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;
	  }
813 814 815 816

      unbind_to (count, Qnil);
    }

817
  return position;
818 819
}

820 821 822
DEFUN ("previous-single-char-property-change",
       Fprevious_single_char_property_change,
       Sprevious_single_char_property_change, 2, 4, 0,
823 824 825
       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.
826 827 828 829
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.

830 831 832
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.

833 834 835 836 837
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.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
838
  (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit)
839 840 841 842 843 844 845
{
  if (STRINGP (object))
    {
      position = Fprevious_single_property_change (position, prop, object, limit);
      if (NILP (position))
	{
	  if (NILP (limit))
846
	    position = make_number (0);
847
	  else
David Kastrup's avatar
David Kastrup committed
848 849 850 851
	    {
	      CHECK_NUMBER (limit);
	      position = limit;
	    }
852 853 854 855
	}
    }
  else
    {
Juanma Barranquero's avatar
Juanma Barranquero committed
856
      int count = SPECPDL_INDEX ();
857 858

      if (! NILP (object))
859
	CHECK_BUFFER (object);
860

861 862 863 864 865
      if (BUFFERP (object) && current_buffer != XBUFFER (object))
	{
	  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
	  Fset_buffer (object);
	}
866

David Kastrup's avatar
David Kastrup committed
867 868
      CHECK_NUMBER_COERCE_MARKER (position);

869
      if (NILP (limit))
870
	XSETFASTINT (limit, BEGV);
871
      else
872
	CHECK_NUMBER_COERCE_MARKER (limit);
873

874
      if (XFASTINT (position) <= XFASTINT (limit))
875 876 877 878 879
	{
	  position = limit;
	  if (XFASTINT (position) < BEGV)
	    XSETFASTINT (position, BEGV);
	}
880
      else
881
	{
882 883 884
	  Lisp_Object initial_value
	    = Fget_char_property (make_number (XFASTINT (position) - 1),
				  prop, object);
885

886
	  while (1)
887 888
	    {
	      position = Fprevious_char_property_change (position, limit);
889

890 891 892 893 894 895 896
	      if (XFASTINT (position) <= XFASTINT (limit))
		{
		  position = limit;
		  break;
		}
	      else
		{
897 898 899
		  Lisp_Object value
		    = Fget_char_property (make_number (XFASTINT (position) - 1),
					  prop, object);
900 901 902 903 904

		  if (!EQ (value, initial_value))
		    break;
		}
	    }
905 906 907 908 909 910 911
	}

      unbind_to (count, Qnil);
    }

  return position;
}
912

Joseph Arceneaux's avatar
Joseph Arceneaux committed
913
DEFUN ("next-property-change", Fnext_property_change,
914
       Snext_property_change, 1, 3, 0,
915 916 917
       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.
918 919 920
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.
921 922 923 924 925
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.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
926
  (Lisp_Object position, Lisp_Object object, Lisp_Object limit)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
927 928 929
{
  register INTERVAL i, next;

930
  if (NILP (object))
931
    XSETBUFFER (object, current_buffer);
932

933
  if (!NILP (limit) && !EQ (limit, Qt))
934
    CHECK_NUMBER_COERCE_MARKER (limit);
935

936
  i = validate_interval_range (object, &position, &position, soft);