PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/textprop.c

http://github.com/jave/emacs
C | 2251 lines | 1501 code | 329 blank | 421 comment | 357 complexity | a794ac02739f81cde9f6009352973b8a MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /* Interface code for dealing with text properties.
  2. Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000, 2001, 2002, 2003,
  3. 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  4. This file is part of GNU Emacs.
  5. GNU Emacs is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. GNU Emacs is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #include "lisp.h"
  17. #include "intervals.h"
  18. #include "buffer.h"
  19. #include "window.h"
  20. #ifndef NULL
  21. #define NULL (void *)0
  22. #endif
  23. /* Test for membership, allowing for t (actually any non-cons) to mean the
  24. universal set. */
  25. #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
  26. /* NOTES: previous- and next- property change will have to skip
  27. zero-length intervals if they are implemented. This could be done
  28. inside next_interval and previous_interval.
  29. set_properties needs to deal with the interval property cache.
  30. It is assumed that for any interval plist, a property appears
  31. only once on the list. Although some code i.e., remove_properties,
  32. handles the more general case, the uniqueness of properties is
  33. necessary for the system to remain consistent. This requirement
  34. is enforced by the subrs installing properties onto the intervals. */
  35. /* Types of hooks. */
  36. Lisp_Object Qmouse_left;
  37. Lisp_Object Qmouse_entered;
  38. Lisp_Object Qpoint_left;
  39. Lisp_Object Qpoint_entered;
  40. Lisp_Object Qcategory;
  41. Lisp_Object Qlocal_map;
  42. /* Visual properties text (including strings) may have. */
  43. Lisp_Object Qforeground, Qbackground, Qfont, Qunderline, Qstipple;
  44. Lisp_Object Qinvisible, Qread_only, Qintangible, Qmouse_face;
  45. Lisp_Object Qminibuffer_prompt;
  46. /* Sticky properties */
  47. Lisp_Object Qfront_sticky, Qrear_nonsticky;
  48. /* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to
  49. the o1's cdr. Otherwise, return zero. This is handy for
  50. traversing plists. */
  51. #define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCDR (o1), CONSP (o2)))
  52. Lisp_Object Vinhibit_point_motion_hooks;
  53. Lisp_Object Vdefault_text_properties;
  54. Lisp_Object Vchar_property_alias_alist;
  55. Lisp_Object Vtext_property_default_nonsticky;
  56. /* verify_interval_modification saves insertion hooks here
  57. to be run later by report_interval_modification. */
  58. Lisp_Object interval_insert_behind_hooks;
  59. Lisp_Object interval_insert_in_front_hooks;
  60. static void text_read_only P_ ((Lisp_Object)) NO_RETURN;
  61. /* Signal a `text-read-only' error. This function makes it easier
  62. to capture that error in GDB by putting a breakpoint on it. */
  63. static void
  64. text_read_only (propval)
  65. Lisp_Object propval;
  66. {
  67. if (STRINGP (propval))
  68. xsignal1 (Qtext_read_only, propval);
  69. xsignal0 (Qtext_read_only);
  70. }
  71. /* Extract the interval at the position pointed to by BEGIN from
  72. OBJECT, a string or buffer. Additionally, check that the positions
  73. pointed to by BEGIN and END are within the bounds of OBJECT, and
  74. reverse them if *BEGIN is greater than *END. The objects pointed
  75. to by BEGIN and END may be integers or markers; if the latter, they
  76. are coerced to integers.
  77. When OBJECT is a string, we increment *BEGIN and *END
  78. to make them origin-one.
  79. Note that buffer points don't correspond to interval indices.
  80. For example, point-max is 1 greater than the index of the last
  81. character. This difference is handled in the caller, which uses
  82. the validated points to determine a length, and operates on that.
  83. Exceptions are Ftext_properties_at, Fnext_property_change, and
  84. Fprevious_property_change which call this function with BEGIN == END.
  85. Handle this case specially.
  86. If FORCE is soft (0), it's OK to return NULL_INTERVAL. Otherwise,
  87. create an interval tree for OBJECT if one doesn't exist, provided
  88. the object actually contains text. In the current design, if there
  89. is no text, there can be no text properties. */
  90. #define soft 0
  91. #define hard 1
  92. INTERVAL
  93. validate_interval_range (object, begin, end, force)
  94. Lisp_Object object, *begin, *end;
  95. int force;
  96. {
  97. register INTERVAL i;
  98. int searchpos;
  99. CHECK_STRING_OR_BUFFER (object);
  100. CHECK_NUMBER_COERCE_MARKER (*begin);
  101. CHECK_NUMBER_COERCE_MARKER (*end);
  102. /* If we are asked for a point, but from a subr which operates
  103. on a range, then return nothing. */
  104. if (EQ (*begin, *end) && begin != end)
  105. return NULL_INTERVAL;
  106. if (XINT (*begin) > XINT (*end))
  107. {
  108. Lisp_Object n;
  109. n = *begin;
  110. *begin = *end;
  111. *end = n;
  112. }
  113. if (BUFFERP (object))
  114. {
  115. register struct buffer *b = XBUFFER (object);
  116. if (!(BUF_BEGV (b) <= XINT (*begin) && XINT (*begin) <= XINT (*end)
  117. && XINT (*end) <= BUF_ZV (b)))
  118. args_out_of_range (*begin, *end);
  119. i = BUF_INTERVALS (b);
  120. /* If there's no text, there are no properties. */
  121. if (BUF_BEGV (b) == BUF_ZV (b))
  122. return NULL_INTERVAL;
  123. searchpos = XINT (*begin);
  124. }
  125. else
  126. {
  127. int len = SCHARS (object);
  128. if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end)
  129. && XINT (*end) <= len))
  130. args_out_of_range (*begin, *end);
  131. XSETFASTINT (*begin, XFASTINT (*begin));
  132. if (begin != end)
  133. XSETFASTINT (*end, XFASTINT (*end));
  134. i = STRING_INTERVALS (object);
  135. if (len == 0)
  136. return NULL_INTERVAL;
  137. searchpos = XINT (*begin);
  138. }
  139. if (NULL_INTERVAL_P (i))
  140. return (force ? create_root_interval (object) : i);
  141. return find_interval (i, searchpos);
  142. }
  143. /* Validate LIST as a property list. If LIST is not a list, then
  144. make one consisting of (LIST nil). Otherwise, verify that LIST
  145. is even numbered and thus suitable as a plist. */
  146. static Lisp_Object
  147. validate_plist (list)
  148. Lisp_Object list;
  149. {
  150. if (NILP (list))
  151. return Qnil;
  152. if (CONSP (list))
  153. {
  154. register int i;
  155. register Lisp_Object tail;
  156. for (i = 0, tail = list; CONSP (tail); i++)
  157. {
  158. tail = XCDR (tail);
  159. QUIT;
  160. }
  161. if (i & 1)
  162. error ("Odd length text property list");
  163. return list;
  164. }
  165. return Fcons (list, Fcons (Qnil, Qnil));
  166. }
  167. /* Return nonzero if interval I has all the properties,
  168. with the same values, of list PLIST. */
  169. static int
  170. interval_has_all_properties (plist, i)
  171. Lisp_Object plist;
  172. INTERVAL i;
  173. {
  174. register Lisp_Object tail1, tail2, sym1;
  175. register int found;
  176. /* Go through each element of PLIST. */
  177. for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
  178. {
  179. sym1 = XCAR (tail1);
  180. found = 0;
  181. /* Go through I's plist, looking for sym1 */
  182. for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
  183. if (EQ (sym1, XCAR (tail2)))
  184. {
  185. /* Found the same property on both lists. If the
  186. values are unequal, return zero. */
  187. if (! EQ (Fcar (XCDR (tail1)), Fcar (XCDR (tail2))))
  188. return 0;
  189. /* Property has same value on both lists; go to next one. */
  190. found = 1;
  191. break;
  192. }
  193. if (! found)
  194. return 0;
  195. }
  196. return 1;
  197. }
  198. /* Return nonzero if the plist of interval I has any of the
  199. properties of PLIST, regardless of their values. */
  200. static INLINE int
  201. interval_has_some_properties (plist, i)
  202. Lisp_Object plist;
  203. INTERVAL i;
  204. {
  205. register Lisp_Object tail1, tail2, sym;
  206. /* Go through each element of PLIST. */
  207. for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
  208. {
  209. sym = XCAR (tail1);
  210. /* Go through i's plist, looking for tail1 */
  211. for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
  212. if (EQ (sym, XCAR (tail2)))
  213. return 1;
  214. }
  215. return 0;
  216. }
  217. /* Return nonzero if the plist of interval I has any of the
  218. property names in LIST, regardless of their values. */
  219. static INLINE int
  220. interval_has_some_properties_list (list, i)
  221. Lisp_Object list;
  222. INTERVAL i;
  223. {
  224. register Lisp_Object tail1, tail2, sym;
  225. /* Go through each element of LIST. */
  226. for (tail1 = list; CONSP (tail1); tail1 = XCDR (tail1))
  227. {
  228. sym = Fcar (tail1);
  229. /* Go through i's plist, looking for tail1 */
  230. for (tail2 = i->plist; CONSP (tail2); tail2 = XCDR (XCDR (tail2)))
  231. if (EQ (sym, XCAR (tail2)))
  232. return 1;
  233. }
  234. return 0;
  235. }
  236. /* Changing the plists of individual intervals. */
  237. /* Return the value of PROP in property-list PLIST, or Qunbound if it
  238. has none. */
  239. static Lisp_Object
  240. property_value (plist, prop)
  241. Lisp_Object plist, prop;
  242. {
  243. Lisp_Object value;
  244. while (PLIST_ELT_P (plist, value))
  245. if (EQ (XCAR (plist), prop))
  246. return XCAR (value);
  247. else
  248. plist = XCDR (value);
  249. return Qunbound;
  250. }
  251. /* Set the properties of INTERVAL to PROPERTIES,
  252. and record undo info for the previous values.
  253. OBJECT is the string or buffer that INTERVAL belongs to. */
  254. static void
  255. set_properties (properties, interval, object)
  256. Lisp_Object properties, object;
  257. INTERVAL interval;
  258. {
  259. Lisp_Object sym, value;
  260. if (BUFFERP (object))
  261. {
  262. /* For each property in the old plist which is missing from PROPERTIES,
  263. or has a different value in PROPERTIES, make an undo record. */
  264. for (sym = interval->plist;
  265. PLIST_ELT_P (sym, value);
  266. sym = XCDR (value))
  267. if (! EQ (property_value (properties, XCAR (sym)),
  268. XCAR (value)))
  269. {
  270. record_property_change (interval->position, LENGTH (interval),
  271. XCAR (sym), XCAR (value),
  272. object);
  273. }
  274. /* For each new property that has no value at all in the old plist,
  275. make an undo record binding it to nil, so it will be removed. */
  276. for (sym = properties;
  277. PLIST_ELT_P (sym, value);
  278. sym = XCDR (value))
  279. if (EQ (property_value (interval->plist, XCAR (sym)), Qunbound))
  280. {
  281. record_property_change (interval->position, LENGTH (interval),
  282. XCAR (sym), Qnil,
  283. object);
  284. }
  285. }
  286. /* Store new properties. */
  287. interval->plist = Fcopy_sequence (properties);
  288. }
  289. /* Add the properties of PLIST to the interval I, or set
  290. the value of I's property to the value of the property on PLIST
  291. if they are different.
  292. OBJECT should be the string or buffer the interval is in.
  293. Return nonzero if this changes I (i.e., if any members of PLIST
  294. are actually added to I's plist) */
  295. static int
  296. add_properties (plist, i, object)
  297. Lisp_Object plist;
  298. INTERVAL i;
  299. Lisp_Object object;
  300. {
  301. Lisp_Object tail1, tail2, sym1, val1;
  302. register int changed = 0;
  303. register int found;
  304. struct gcpro gcpro1, gcpro2, gcpro3;
  305. tail1 = plist;
  306. sym1 = Qnil;
  307. val1 = Qnil;
  308. /* No need to protect OBJECT, because we can GC only in the case
  309. where it is a buffer, and live buffers are always protected.
  310. I and its plist are also protected, via OBJECT. */
  311. GCPRO3 (tail1, sym1, val1);
  312. /* Go through each element of PLIST. */
  313. for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
  314. {
  315. sym1 = XCAR (tail1);
  316. val1 = Fcar (XCDR (tail1));
  317. found = 0;
  318. /* Go through I's plist, looking for sym1 */
  319. for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
  320. if (EQ (sym1, XCAR (tail2)))
  321. {
  322. /* No need to gcpro, because tail2 protects this
  323. and it must be a cons cell (we get an error otherwise). */
  324. register Lisp_Object this_cdr;
  325. this_cdr = XCDR (tail2);
  326. /* Found the property. Now check its value. */
  327. found = 1;
  328. /* The properties have the same value on both lists.
  329. Continue to the next property. */
  330. if (EQ (val1, Fcar (this_cdr)))
  331. break;
  332. /* Record this change in the buffer, for undo purposes. */
  333. if (BUFFERP (object))
  334. {
  335. record_property_change (i->position, LENGTH (i),
  336. sym1, Fcar (this_cdr), object);
  337. }
  338. /* I's property has a different value -- change it */
  339. Fsetcar (this_cdr, val1);
  340. changed++;
  341. break;
  342. }
  343. if (! found)
  344. {
  345. /* Record this change in the buffer, for undo purposes. */
  346. if (BUFFERP (object))
  347. {
  348. record_property_change (i->position, LENGTH (i),
  349. sym1, Qnil, object);
  350. }
  351. i->plist = Fcons (sym1, Fcons (val1, i->plist));
  352. changed++;
  353. }
  354. }
  355. UNGCPRO;
  356. return changed;
  357. }
  358. /* For any members of PLIST, or LIST,
  359. which are properties of I, remove them from I's plist.
  360. (If PLIST is non-nil, use that, otherwise use LIST.)
  361. OBJECT is the string or buffer containing I. */
  362. static int
  363. remove_properties (plist, list, i, object)
  364. Lisp_Object plist, list;
  365. INTERVAL i;
  366. Lisp_Object object;
  367. {
  368. register Lisp_Object tail1, tail2, sym, current_plist;
  369. register int changed = 0;
  370. /* Nonzero means tail1 is a plist, otherwise it is a list. */
  371. int use_plist;
  372. current_plist = i->plist;
  373. if (! NILP (plist))
  374. tail1 = plist, use_plist = 1;
  375. else
  376. tail1 = list, use_plist = 0;
  377. /* Go through each element of LIST or PLIST. */
  378. while (CONSP (tail1))
  379. {
  380. sym = XCAR (tail1);
  381. /* First, remove the symbol if it's at the head of the list */
  382. while (CONSP (current_plist) && EQ (sym, XCAR (current_plist)))
  383. {
  384. if (BUFFERP (object))
  385. record_property_change (i->position, LENGTH (i),
  386. sym, XCAR (XCDR (current_plist)),
  387. object);
  388. current_plist = XCDR (XCDR (current_plist));
  389. changed++;
  390. }
  391. /* Go through I's plist, looking for SYM. */
  392. tail2 = current_plist;
  393. while (! NILP (tail2))
  394. {
  395. register Lisp_Object this;
  396. this = XCDR (XCDR (tail2));
  397. if (CONSP (this) && EQ (sym, XCAR (this)))
  398. {
  399. if (BUFFERP (object))
  400. record_property_change (i->position, LENGTH (i),
  401. sym, XCAR (XCDR (this)), object);
  402. Fsetcdr (XCDR (tail2), XCDR (XCDR (this)));
  403. changed++;
  404. }
  405. tail2 = this;
  406. }
  407. /* Advance thru TAIL1 one way or the other. */
  408. tail1 = XCDR (tail1);
  409. if (use_plist && CONSP (tail1))
  410. tail1 = XCDR (tail1);
  411. }
  412. if (changed)
  413. i->plist = current_plist;
  414. return changed;
  415. }
  416. #if 0
  417. /* Remove all properties from interval I. Return non-zero
  418. if this changes the interval. */
  419. static INLINE int
  420. erase_properties (i)
  421. INTERVAL i;
  422. {
  423. if (NILP (i->plist))
  424. return 0;
  425. i->plist = Qnil;
  426. return 1;
  427. }
  428. #endif
  429. /* Returns the interval of POSITION in OBJECT.
  430. POSITION is BEG-based. */
  431. INTERVAL
  432. interval_of (position, object)
  433. int position;
  434. Lisp_Object object;
  435. {
  436. register INTERVAL i;
  437. int beg, end;
  438. if (NILP (object))
  439. XSETBUFFER (object, current_buffer);
  440. else if (EQ (object, Qt))
  441. return NULL_INTERVAL;
  442. CHECK_STRING_OR_BUFFER (object);
  443. if (BUFFERP (object))
  444. {
  445. register struct buffer *b = XBUFFER (object);
  446. beg = BUF_BEGV (b);
  447. end = BUF_ZV (b);
  448. i = BUF_INTERVALS (b);
  449. }
  450. else
  451. {
  452. beg = 0;
  453. end = SCHARS (object);
  454. i = STRING_INTERVALS (object);
  455. }
  456. if (!(beg <= position && position <= end))
  457. args_out_of_range (make_number (position), make_number (position));
  458. if (beg == end || NULL_INTERVAL_P (i))
  459. return NULL_INTERVAL;
  460. return find_interval (i, position);
  461. }
  462. DEFUN ("text-properties-at", Ftext_properties_at,
  463. Stext_properties_at, 1, 2, 0,
  464. doc: /* Return the list of properties of the character at POSITION in OBJECT.
  465. If the optional second argument OBJECT is a buffer (or nil, which means
  466. the current buffer), POSITION is a buffer position (integer or marker).
  467. If OBJECT is a string, POSITION is a 0-based index into it.
  468. If POSITION is at the end of OBJECT, the value is nil. */)
  469. (position, object)
  470. Lisp_Object position, object;
  471. {
  472. register INTERVAL i;
  473. if (NILP (object))
  474. XSETBUFFER (object, current_buffer);
  475. i = validate_interval_range (object, &position, &position, soft);
  476. if (NULL_INTERVAL_P (i))
  477. return Qnil;
  478. /* If POSITION is at the end of the interval,
  479. it means it's the end of OBJECT.
  480. There are no properties at the very end,
  481. since no character follows. */
  482. if (XINT (position) == LENGTH (i) + i->position)
  483. return Qnil;
  484. return i->plist;
  485. }
  486. DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
  487. doc: /* Return the value of POSITION's property PROP, in OBJECT.
  488. OBJECT is optional and defaults to the current buffer.
  489. If POSITION is at the end of OBJECT, the value is nil. */)
  490. (position, prop, object)
  491. Lisp_Object position, object;
  492. Lisp_Object prop;
  493. {
  494. return textget (Ftext_properties_at (position, object), prop);
  495. }
  496. /* Return the value of char's property PROP, in OBJECT at POSITION.
  497. OBJECT is optional and defaults to the current buffer.
  498. If OVERLAY is non-0, then in the case that the returned property is from
  499. an overlay, the overlay found is returned in *OVERLAY, otherwise nil is
  500. returned in *OVERLAY.
  501. If POSITION is at the end of OBJECT, the value is nil.
  502. If OBJECT is a buffer, then overlay properties are considered as well as
  503. text properties.
  504. If OBJECT is a window, then that window's buffer is used, but
  505. window-specific overlays are considered only if they are associated
  506. with OBJECT. */
  507. Lisp_Object
  508. get_char_property_and_overlay (position, prop, object, overlay)
  509. Lisp_Object position, object;
  510. register Lisp_Object prop;
  511. Lisp_Object *overlay;
  512. {
  513. struct window *w = 0;
  514. CHECK_NUMBER_COERCE_MARKER (position);
  515. if (NILP (object))
  516. XSETBUFFER (object, current_buffer);
  517. if (WINDOWP (object))
  518. {
  519. w = XWINDOW (object);
  520. object = w->buffer;
  521. }
  522. if (BUFFERP (object))
  523. {
  524. int noverlays;
  525. Lisp_Object *overlay_vec;
  526. struct buffer *obuf = current_buffer;
  527. if (XINT (position) < BUF_BEGV (XBUFFER (object))
  528. || XINT (position) > BUF_ZV (XBUFFER (object)))
  529. xsignal1 (Qargs_out_of_range, position);
  530. set_buffer_temp (XBUFFER (object));
  531. GET_OVERLAYS_AT (XINT (position), overlay_vec, noverlays, NULL, 0);
  532. noverlays = sort_overlays (overlay_vec, noverlays, w);
  533. set_buffer_temp (obuf);
  534. /* Now check the overlays in order of decreasing priority. */
  535. while (--noverlays >= 0)
  536. {
  537. Lisp_Object tem = Foverlay_get (overlay_vec[noverlays], prop);
  538. if (!NILP (tem))
  539. {
  540. if (overlay)
  541. /* Return the overlay we got the property from. */
  542. *overlay = overlay_vec[noverlays];
  543. return tem;
  544. }
  545. }
  546. }
  547. if (overlay)
  548. /* Indicate that the return value is not from an overlay. */
  549. *overlay = Qnil;
  550. /* Not a buffer, or no appropriate overlay, so fall through to the
  551. simpler case. */
  552. return Fget_text_property (position, prop, object);
  553. }
  554. DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
  555. doc: /* Return the value of POSITION's property PROP, in OBJECT.
  556. Both overlay properties and text properties are checked.
  557. OBJECT is optional and defaults to the current buffer.
  558. If POSITION is at the end of OBJECT, the value is nil.
  559. If OBJECT is a buffer, then overlay properties are considered as well as
  560. text properties.
  561. If OBJECT is a window, then that window's buffer is used, but window-specific
  562. overlays are considered only if they are associated with OBJECT. */)
  563. (position, prop, object)
  564. Lisp_Object position, object;
  565. register Lisp_Object prop;
  566. {
  567. return get_char_property_and_overlay (position, prop, object, 0);
  568. }
  569. DEFUN ("get-char-property-and-overlay", Fget_char_property_and_overlay,
  570. Sget_char_property_and_overlay, 2, 3, 0,
  571. doc: /* Like `get-char-property', but with extra overlay information.
  572. The value is a cons cell. Its car is the return value of `get-char-property'
  573. with the same arguments--that is, the value of POSITION's property
  574. PROP in OBJECT. Its cdr is the overlay in which the property was
  575. found, or nil, if it was found as a text property or not found at all.
  576. OBJECT is optional and defaults to the current buffer. OBJECT may be
  577. a string, a buffer or a window. For strings, the cdr of the return
  578. value is always nil, since strings do not have overlays. If OBJECT is
  579. a window, then that window's buffer is used, but window-specific
  580. overlays are considered only if they are associated with OBJECT. If
  581. POSITION is at the end of OBJECT, both car and cdr are nil. */)
  582. (position, prop, object)
  583. Lisp_Object position, object;
  584. register Lisp_Object prop;
  585. {
  586. Lisp_Object overlay;
  587. Lisp_Object val
  588. = get_char_property_and_overlay (position, prop, object, &overlay);
  589. return Fcons (val, overlay);
  590. }
  591. DEFUN ("next-char-property-change", Fnext_char_property_change,
  592. Snext_char_property_change, 1, 2, 0,
  593. doc: /* Return the position of next text property or overlay change.
  594. This scans characters forward in the current buffer from POSITION till
  595. it finds a change in some text property, or the beginning or end of an
  596. overlay, and returns the position of that.
  597. If none is found up to (point-max), the function returns (point-max).
  598. If the optional second argument LIMIT is non-nil, don't search
  599. past position LIMIT; return LIMIT if nothing is found before LIMIT.
  600. LIMIT is a no-op if it is greater than (point-max). */)
  601. (position, limit)
  602. Lisp_Object position, limit;
  603. {
  604. Lisp_Object temp;
  605. temp = Fnext_overlay_change (position);
  606. if (! NILP (limit))
  607. {
  608. CHECK_NUMBER_COERCE_MARKER (limit);
  609. if (XINT (limit) < XINT (temp))
  610. temp = limit;
  611. }
  612. return Fnext_property_change (position, Qnil, temp);
  613. }
  614. DEFUN ("previous-char-property-change", Fprevious_char_property_change,
  615. Sprevious_char_property_change, 1, 2, 0,
  616. doc: /* Return the position of previous text property or overlay change.
  617. Scans characters backward in the current buffer from POSITION till it
  618. finds a change in some text property, or the beginning or end of an
  619. overlay, and returns the position of that.
  620. If none is found since (point-min), the function returns (point-min).
  621. If the optional second argument LIMIT is non-nil, don't search
  622. past position LIMIT; return LIMIT if nothing is found before LIMIT.
  623. LIMIT is a no-op if it is less than (point-min). */)
  624. (position, limit)
  625. Lisp_Object position, limit;
  626. {
  627. Lisp_Object temp;
  628. temp = Fprevious_overlay_change (position);
  629. if (! NILP (limit))
  630. {
  631. CHECK_NUMBER_COERCE_MARKER (limit);
  632. if (XINT (limit) > XINT (temp))
  633. temp = limit;
  634. }
  635. return Fprevious_property_change (position, Qnil, temp);
  636. }
  637. DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
  638. Snext_single_char_property_change, 2, 4, 0,
  639. doc: /* Return the position of next text property or overlay change for a specific property.
  640. Scans characters forward from POSITION till it finds
  641. a change in the PROP property, then returns the position of the change.
  642. If the optional third argument OBJECT is a buffer (or nil, which means
  643. the current buffer), POSITION is a buffer position (integer or marker).
  644. If OBJECT is a string, POSITION is a 0-based index into it.
  645. In a string, scan runs to the end of the string.
  646. In a buffer, it runs to (point-max), and the value cannot exceed that.
  647. The property values are compared with `eq'.
  648. If the property is constant all the way to the end of OBJECT, return the
  649. last valid position in OBJECT.
  650. If the optional fourth argument LIMIT is non-nil, don't search
  651. past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
  652. (position, prop, object, limit)
  653. Lisp_Object prop, position, object, limit;
  654. {
  655. if (STRINGP (object))
  656. {
  657. position = Fnext_single_property_change (position, prop, object, limit);
  658. if (NILP (position))
  659. {
  660. if (NILP (limit))
  661. position = make_number (SCHARS (object));
  662. else
  663. {
  664. CHECK_NUMBER (limit);
  665. position = limit;
  666. }
  667. }
  668. }
  669. else
  670. {
  671. Lisp_Object initial_value, value;
  672. int count = SPECPDL_INDEX ();
  673. if (! NILP (object))
  674. CHECK_BUFFER (object);
  675. if (BUFFERP (object) && current_buffer != XBUFFER (object))
  676. {
  677. record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  678. Fset_buffer (object);
  679. }
  680. CHECK_NUMBER_COERCE_MARKER (position);
  681. initial_value = Fget_char_property (position, prop, object);
  682. if (NILP (limit))
  683. XSETFASTINT (limit, ZV);
  684. else
  685. CHECK_NUMBER_COERCE_MARKER (limit);
  686. if (XFASTINT (position) >= XFASTINT (limit))
  687. {
  688. position = limit;
  689. if (XFASTINT (position) > ZV)
  690. XSETFASTINT (position, ZV);
  691. }
  692. else
  693. while (1)
  694. {
  695. position = Fnext_char_property_change (position, limit);
  696. if (XFASTINT (position) >= XFASTINT (limit))
  697. {
  698. position = limit;
  699. break;
  700. }
  701. value = Fget_char_property (position, prop, object);
  702. if (!EQ (value, initial_value))
  703. break;
  704. }
  705. unbind_to (count, Qnil);
  706. }
  707. return position;
  708. }
  709. DEFUN ("previous-single-char-property-change",
  710. Fprevious_single_char_property_change,
  711. Sprevious_single_char_property_change, 2, 4, 0,
  712. doc: /* Return the position of previous text property or overlay change for a specific property.
  713. Scans characters backward from POSITION till it finds
  714. a change in the PROP property, then returns the position of the change.
  715. If the optional third argument OBJECT is a buffer (or nil, which means
  716. the current buffer), POSITION is a buffer position (integer or marker).
  717. If OBJECT is a string, POSITION is a 0-based index into it.
  718. In a string, scan runs to the start of the string.
  719. In a buffer, it runs to (point-min), and the value cannot be less than that.
  720. The property values are compared with `eq'.
  721. If the property is constant all the way to the start of OBJECT, return the
  722. first valid position in OBJECT.
  723. If the optional fourth argument LIMIT is non-nil, don't search
  724. back past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
  725. (position, prop, object, limit)
  726. Lisp_Object prop, position, object, limit;
  727. {
  728. if (STRINGP (object))
  729. {
  730. position = Fprevious_single_property_change (position, prop, object, limit);
  731. if (NILP (position))
  732. {
  733. if (NILP (limit))
  734. position = make_number (0);
  735. else
  736. {
  737. CHECK_NUMBER (limit);
  738. position = limit;
  739. }
  740. }
  741. }
  742. else
  743. {
  744. int count = SPECPDL_INDEX ();
  745. if (! NILP (object))
  746. CHECK_BUFFER (object);
  747. if (BUFFERP (object) && current_buffer != XBUFFER (object))
  748. {
  749. record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  750. Fset_buffer (object);
  751. }
  752. CHECK_NUMBER_COERCE_MARKER (position);
  753. if (NILP (limit))
  754. XSETFASTINT (limit, BEGV);
  755. else
  756. CHECK_NUMBER_COERCE_MARKER (limit);
  757. if (XFASTINT (position) <= XFASTINT (limit))
  758. {
  759. position = limit;
  760. if (XFASTINT (position) < BEGV)
  761. XSETFASTINT (position, BEGV);
  762. }
  763. else
  764. {
  765. Lisp_Object initial_value
  766. = Fget_char_property (make_number (XFASTINT (position) - 1),
  767. prop, object);
  768. while (1)
  769. {
  770. position = Fprevious_char_property_change (position, limit);
  771. if (XFASTINT (position) <= XFASTINT (limit))
  772. {
  773. position = limit;
  774. break;
  775. }
  776. else
  777. {
  778. Lisp_Object value
  779. = Fget_char_property (make_number (XFASTINT (position) - 1),
  780. prop, object);
  781. if (!EQ (value, initial_value))
  782. break;
  783. }
  784. }
  785. }
  786. unbind_to (count, Qnil);
  787. }
  788. return position;
  789. }
  790. DEFUN ("next-property-change", Fnext_property_change,
  791. Snext_property_change, 1, 3, 0,
  792. doc: /* Return the position of next property change.
  793. Scans characters forward from POSITION in OBJECT till it finds
  794. a change in some text property, then returns the position of the change.
  795. If the optional second argument OBJECT is a buffer (or nil, which means
  796. the current buffer), POSITION is a buffer position (integer or marker).
  797. If OBJECT is a string, POSITION is a 0-based index into it.
  798. Return nil if the property is constant all the way to the end of OBJECT.
  799. If the value is non-nil, it is a position greater than POSITION, never equal.
  800. If the optional third argument LIMIT is non-nil, don't search
  801. past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
  802. (position, object, limit)
  803. Lisp_Object position, object, limit;
  804. {
  805. register INTERVAL i, next;
  806. if (NILP (object))
  807. XSETBUFFER (object, current_buffer);
  808. if (!NILP (limit) && !EQ (limit, Qt))
  809. CHECK_NUMBER_COERCE_MARKER (limit);
  810. i = validate_interval_range (object, &position, &position, soft);
  811. /* If LIMIT is t, return start of next interval--don't
  812. bother checking further intervals. */
  813. if (EQ (limit, Qt))
  814. {
  815. if (NULL_INTERVAL_P (i))
  816. next = i;
  817. else
  818. next = next_interval (i);
  819. if (NULL_INTERVAL_P (next))
  820. XSETFASTINT (position, (STRINGP (object)
  821. ? SCHARS (object)
  822. : BUF_ZV (XBUFFER (object))));
  823. else
  824. XSETFASTINT (position, next->position);
  825. return position;
  826. }
  827. if (NULL_INTERVAL_P (i))
  828. return limit;
  829. next = next_interval (i);
  830. while (!NULL_INTERVAL_P (next) && intervals_equal (i, next)
  831. && (NILP (limit) || next->position < XFASTINT (limit)))
  832. next = next_interval (next);
  833. if (NULL_INTERVAL_P (next)
  834. || (next->position
  835. >= (INTEGERP (limit)
  836. ? XFASTINT (limit)
  837. : (STRINGP (object)
  838. ? SCHARS (object)
  839. : BUF_ZV (XBUFFER (object))))))
  840. return limit;
  841. else
  842. return make_number (next->position);
  843. }
  844. /* Return 1 if there's a change in some property between BEG and END. */
  845. int
  846. property_change_between_p (beg, end)
  847. int beg, end;
  848. {
  849. register INTERVAL i, next;
  850. Lisp_Object object, pos;
  851. XSETBUFFER (object, current_buffer);
  852. XSETFASTINT (pos, beg);
  853. i = validate_interval_range (object, &pos, &pos, soft);
  854. if (NULL_INTERVAL_P (i))
  855. return 0;
  856. next = next_interval (i);
  857. while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
  858. {
  859. next = next_interval (next);
  860. if (NULL_INTERVAL_P (next))
  861. return 0;
  862. if (next->position >= end)
  863. return 0;
  864. }
  865. if (NULL_INTERVAL_P (next))
  866. return 0;
  867. return 1;
  868. }
  869. DEFUN ("next-single-property-change", Fnext_single_property_change,
  870. Snext_single_property_change, 2, 4, 0,
  871. doc: /* Return the position of next property change for a specific property.
  872. Scans characters forward from POSITION till it finds
  873. a change in the PROP property, then returns the position of the change.
  874. If the optional third argument OBJECT is a buffer (or nil, which means
  875. the current buffer), POSITION is a buffer position (integer or marker).
  876. If OBJECT is a string, POSITION is a 0-based index into it.
  877. The property values are compared with `eq'.
  878. Return nil if the property is constant all the way to the end of OBJECT.
  879. If the value is non-nil, it is a position greater than POSITION, never equal.
  880. If the optional fourth argument LIMIT is non-nil, don't search
  881. past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
  882. (position, prop, object, limit)
  883. Lisp_Object position, prop, object, limit;
  884. {
  885. register INTERVAL i, next;
  886. register Lisp_Object here_val;
  887. if (NILP (object))
  888. XSETBUFFER (object, current_buffer);
  889. if (!NILP (limit))
  890. CHECK_NUMBER_COERCE_MARKER (limit);
  891. i = validate_interval_range (object, &position, &position, soft);
  892. if (NULL_INTERVAL_P (i))
  893. return limit;
  894. here_val = textget (i->plist, prop);
  895. next = next_interval (i);
  896. while (! NULL_INTERVAL_P (next)
  897. && EQ (here_val, textget (next->plist, prop))
  898. && (NILP (limit) || next->position < XFASTINT (limit)))
  899. next = next_interval (next);
  900. if (NULL_INTERVAL_P (next)
  901. || (next->position
  902. >= (INTEGERP (limit)
  903. ? XFASTINT (limit)
  904. : (STRINGP (object)
  905. ? SCHARS (object)
  906. : BUF_ZV (XBUFFER (object))))))
  907. return limit;
  908. else
  909. return make_number (next->position);
  910. }
  911. DEFUN ("previous-property-change", Fprevious_property_change,
  912. Sprevious_property_change, 1, 3, 0,
  913. doc: /* Return the position of previous property change.
  914. Scans characters backwards from POSITION in OBJECT till it finds
  915. a change in some text property, then returns the position of the change.
  916. If the optional second argument OBJECT is a buffer (or nil, which means
  917. the current buffer), POSITION is a buffer position (integer or marker).
  918. If OBJECT is a string, POSITION is a 0-based index into it.
  919. Return nil if the property is constant all the way to the start of OBJECT.
  920. If the value is non-nil, it is a position less than POSITION, never equal.
  921. If the optional third argument LIMIT is non-nil, don't search
  922. back past position LIMIT; return LIMIT if nothing is found until LIMIT. */)
  923. (position, object, limit)
  924. Lisp_Object position, object, limit;
  925. {
  926. register INTERVAL i, previous;
  927. if (NILP (object))
  928. XSETBUFFER (object, current_buffer);
  929. if (!NILP (limit))
  930. CHECK_NUMBER_COERCE_MARKER (limit);
  931. i = validate_interval_range (object, &position, &position, soft);
  932. if (NULL_INTERVAL_P (i))
  933. return limit;
  934. /* Start with the interval containing the char before point. */
  935. if (i->position == XFASTINT (position))
  936. i = previous_interval (i);
  937. previous = previous_interval (i);
  938. while (!NULL_INTERVAL_P (previous) && intervals_equal (previous, i)
  939. && (NILP (limit)
  940. || (previous->position + LENGTH (previous) > XFASTINT (limit))))
  941. previous = previous_interval (previous);
  942. if (NULL_INTERVAL_P (previous)
  943. || (previous->position + LENGTH (previous)
  944. <= (INTEGERP (limit)
  945. ? XFASTINT (limit)
  946. : (STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object))))))
  947. return limit;
  948. else
  949. return make_number (previous->position + LENGTH (previous));
  950. }
  951. DEFUN ("previous-single-property-change", Fprevious_single_property_change,
  952. Sprevious_single_property_change, 2, 4, 0,
  953. doc: /* Return the position of previous property change for a specific property.
  954. Scans characters backward from POSITION till it finds
  955. a change in the PROP property, then returns the position of the change.
  956. If the optional third argument OBJECT is a buffer (or nil, which means
  957. the current buffer), POSITION is a buffer position (integer or marker).
  958. If OBJECT is a string, POSITION is a 0-based index into it.
  959. The property values are compared with `eq'.
  960. Return nil if the property is constant all the way to the start of OBJECT.
  961. If the value is non-nil, it is a position less than POSITION, never equal.
  962. If the optional fourth argument LIMIT is non-nil, don't search
  963. back past position LIMIT; return LIMIT if nothing is found until LIMIT. */)
  964. (position, prop, object, limit)
  965. Lisp_Object position, prop, object, limit;
  966. {
  967. register INTERVAL i, previous;
  968. register Lisp_Object here_val;
  969. if (NILP (object))
  970. XSETBUFFER (object, current_buffer);
  971. if (!NILP (limit))
  972. CHECK_NUMBER_COERCE_MARKER (limit);
  973. i = validate_interval_range (object, &position, &position, soft);
  974. /* Start with the interval containing the char before point. */
  975. if (!NULL_INTERVAL_P (i) && i->position == XFASTINT (position))
  976. i = previous_interval (i);
  977. if (NULL_INTERVAL_P (i))
  978. return limit;
  979. here_val = textget (i->plist, prop);
  980. previous = previous_interval (i);
  981. while (!NULL_INTERVAL_P (previous)
  982. && EQ (here_val, textget (previous->plist, prop))
  983. && (NILP (limit)
  984. || (previous->position + LENGTH (previous) > XFASTINT (limit))))
  985. previous = previous_interval (previous);
  986. if (NULL_INTERVAL_P (previous)
  987. || (previous->position + LENGTH (previous)
  988. <= (INTEGERP (limit)
  989. ? XFASTINT (limit)
  990. : (STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object))))))
  991. return limit;
  992. else
  993. return make_number (previous->position + LENGTH (previous));
  994. }
  995. /* Callers note, this can GC when OBJECT is a buffer (or nil). */
  996. DEFUN ("add-text-properties", Fadd_text_properties,
  997. Sadd_text_properties, 3, 4, 0,
  998. doc: /* Add properties to the text from START to END.
  999. The third argument PROPERTIES is a property list
  1000. specifying the property values to add. If the optional fourth argument
  1001. OBJECT is a buffer (or nil, which means the current buffer),
  1002. START and END are buffer positions (integers or markers).
  1003. If OBJECT is a string, START and END are 0-based indices into it.
  1004. Return t if any property value actually changed, nil otherwise. */)
  1005. (start, end, properties, object)
  1006. Lisp_Object start, end, properties, object;
  1007. {
  1008. register INTERVAL i, unchanged;
  1009. register int s, len, modified = 0;
  1010. struct gcpro gcpro1;
  1011. properties = validate_plist (properties);
  1012. if (NILP (properties))
  1013. return Qnil;
  1014. if (NILP (object))
  1015. XSETBUFFER (object, current_buffer);
  1016. i = validate_interval_range (object, &start, &end, hard);
  1017. if (NULL_INTERVAL_P (i))
  1018. return Qnil;
  1019. s = XINT (start);
  1020. len = XINT (end) - s;
  1021. /* No need to protect OBJECT, because we GC only if it's a buffer,
  1022. and live buffers are always protected. */
  1023. GCPRO1 (properties);
  1024. /* If we're not starting on an interval boundary, we have to
  1025. split this interval. */
  1026. if (i->position != s)
  1027. {
  1028. /* If this interval already has the properties, we can
  1029. skip it. */
  1030. if (interval_has_all_properties (properties, i))
  1031. {
  1032. int got = (LENGTH (i) - (s - i->position));
  1033. if (got >= len)
  1034. RETURN_UNGCPRO (Qnil);
  1035. len -= got;
  1036. i = next_interval (i);
  1037. }
  1038. else
  1039. {
  1040. unchanged = i;
  1041. i = split_interval_right (unchanged, s - unchanged->position);
  1042. copy_properties (unchanged, i);
  1043. }
  1044. }
  1045. if (BUFFERP (object))
  1046. modify_region (XBUFFER (object), XINT (start), XINT (end), 1);
  1047. /* We are at the beginning of interval I, with LEN chars to scan. */
  1048. for (;;)
  1049. {
  1050. if (i == 0)
  1051. abort ();
  1052. if (LENGTH (i) >= len)
  1053. {
  1054. /* We can UNGCPRO safely here, because there will be just
  1055. one more chance to gc, in the next call to add_properties,
  1056. and after that we will not need PROPERTIES or OBJECT again. */
  1057. UNGCPRO;
  1058. if (interval_has_all_properties (properties, i))
  1059. {
  1060. if (BUFFERP (object))
  1061. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1062. XINT (end) - XINT (start));
  1063. return modified ? Qt : Qnil;
  1064. }
  1065. if (LENGTH (i) == len)
  1066. {
  1067. add_properties (properties, i, object);
  1068. if (BUFFERP (object))
  1069. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1070. XINT (end) - XINT (start));
  1071. return Qt;
  1072. }
  1073. /* i doesn't have the properties, and goes past the change limit */
  1074. unchanged = i;
  1075. i = split_interval_left (unchanged, len);
  1076. copy_properties (unchanged, i);
  1077. add_properties (properties, i, object);
  1078. if (BUFFERP (object))
  1079. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1080. XINT (end) - XINT (start));
  1081. return Qt;
  1082. }
  1083. len -= LENGTH (i);
  1084. modified += add_properties (properties, i, object);
  1085. i = next_interval (i);
  1086. }
  1087. }
  1088. /* Callers note, this can GC when OBJECT is a buffer (or nil). */
  1089. DEFUN ("put-text-property", Fput_text_property,
  1090. Sput_text_property, 4, 5, 0,
  1091. doc: /* Set one property of the text from START to END.
  1092. The third and fourth arguments PROPERTY and VALUE
  1093. specify the property to add.
  1094. If the optional fifth argument OBJECT is a buffer (or nil, which means
  1095. the current buffer), START and END are buffer positions (integers or
  1096. markers). If OBJECT is a string, START and END are 0-based indices into it. */)
  1097. (start, end, property, value, object)
  1098. Lisp_Object start, end, property, value, object;
  1099. {
  1100. Fadd_text_properties (start, end,
  1101. Fcons (property, Fcons (value, Qnil)),
  1102. object);
  1103. return Qnil;
  1104. }
  1105. DEFUN ("set-text-properties", Fset_text_properties,
  1106. Sset_text_properties, 3, 4, 0,
  1107. doc: /* Completely replace properties of text from START to END.
  1108. The third argument PROPERTIES is the new property list.
  1109. If the optional fourth argument OBJECT is a buffer (or nil, which means
  1110. the current buffer), START and END are buffer positions (integers or
  1111. markers). If OBJECT is a string, START and END are 0-based indices into it.
  1112. If PROPERTIES is nil, the effect is to remove all properties from
  1113. the designated part of OBJECT. */)
  1114. (start, end, properties, object)
  1115. Lisp_Object start, end, properties, object;
  1116. {
  1117. return set_text_properties (start, end, properties, object, Qt);
  1118. }
  1119. /* Replace properties of text from START to END with new list of
  1120. properties PROPERTIES. OBJECT is the buffer or string containing
  1121. the text. OBJECT nil means use the current buffer.
  1122. SIGNAL_AFTER_CHANGE_P nil means don't signal after changes. Value
  1123. is nil if the function _detected_ that it did not replace any
  1124. properties, non-nil otherwise. */
  1125. Lisp_Object
  1126. set_text_properties (start, end, properties, object, signal_after_change_p)
  1127. Lisp_Object start, end, properties, object, signal_after_change_p;
  1128. {
  1129. register INTERVAL i;
  1130. Lisp_Object ostart, oend;
  1131. ostart = start;
  1132. oend = end;
  1133. properties = validate_plist (properties);
  1134. if (NILP (object))
  1135. XSETBUFFER (object, current_buffer);
  1136. /* If we want no properties for a whole string,
  1137. get rid of its intervals. */
  1138. if (NILP (properties) && STRINGP (object)
  1139. && XFASTINT (start) == 0
  1140. && XFASTINT (end) == SCHARS (object))
  1141. {
  1142. if (! STRING_INTERVALS (object))
  1143. return Qnil;
  1144. STRING_SET_INTERVALS (object, NULL_INTERVAL);
  1145. return Qt;
  1146. }
  1147. i = validate_interval_range (object, &start, &end, soft);
  1148. if (NULL_INTERVAL_P (i))
  1149. {
  1150. /* If buffer has no properties, and we want none, return now. */
  1151. if (NILP (properties))
  1152. return Qnil;
  1153. /* Restore the original START and END values
  1154. because validate_interval_range increments them for strings. */
  1155. start = ostart;
  1156. end = oend;
  1157. i = validate_interval_range (object, &start, &end, hard);
  1158. /* This can return if start == end. */
  1159. if (NULL_INTERVAL_P (i))
  1160. return Qnil;
  1161. }
  1162. if (BUFFERP (object))
  1163. modify_region (XBUFFER (object), XINT (start), XINT (end), 1);
  1164. set_text_properties_1 (start, end, properties, object, i);
  1165. if (BUFFERP (object) && !NILP (signal_after_change_p))
  1166. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1167. XINT (end) - XINT (start));
  1168. return Qt;
  1169. }
  1170. /* Replace properties of text from START to END with new list of
  1171. properties PROPERTIES. BUFFER is the buffer containing
  1172. the text. This does not obey any hooks.
  1173. You can provide the interval that START is located in as I,
  1174. or pass NULL for I and this function will find it.
  1175. START and END can be in any order. */
  1176. void
  1177. set_text_properties_1 (start, end, properties, buffer, i)
  1178. Lisp_Object start, end, properties, buffer;
  1179. INTERVAL i;
  1180. {
  1181. register INTERVAL prev_changed = NULL_INTERVAL;
  1182. register int s, len;
  1183. INTERVAL unchanged;
  1184. s = XINT (start);
  1185. len = XINT (end) - s;
  1186. if (len == 0)
  1187. return;
  1188. if (len < 0)
  1189. {
  1190. s = s + len;
  1191. len = - len;
  1192. }
  1193. if (i == 0)
  1194. i = find_interval (BUF_INTERVALS (XBUFFER (buffer)), s);
  1195. if (i->position != s)
  1196. {
  1197. unchanged = i;
  1198. i = split_interval_right (unchanged, s - unchanged->position);
  1199. if (LENGTH (i) > len)
  1200. {
  1201. copy_properties (unchanged, i);
  1202. i = split_interval_left (i, len);
  1203. set_properties (properties, i, buffer);
  1204. return;
  1205. }
  1206. set_properties (properties, i, buffer);
  1207. if (LENGTH (i) == len)
  1208. return;
  1209. prev_changed = i;
  1210. len -= LENGTH (i);
  1211. i = next_interval (i);
  1212. }
  1213. /* We are starting at the beginning of an interval, I */
  1214. while (len > 0)
  1215. {
  1216. if (i == 0)
  1217. abort ();
  1218. if (LENGTH (i) >= len)
  1219. {
  1220. if (LENGTH (i) > len)
  1221. i = split_interval_left (i, len);
  1222. /* We have to call set_properties even if we are going to
  1223. merge the intervals, so as to make the undo records
  1224. and cause redisplay to happen. */
  1225. set_properties (properties, i, buffer);
  1226. if (!NULL_INTERVAL_P (prev_changed))
  1227. merge_interval_left (i);
  1228. return;
  1229. }
  1230. len -= LENGTH (i);
  1231. /* We have to call set_properties even if we are going to
  1232. merge the intervals, so as to make the undo records
  1233. and cause redisplay to happen. */
  1234. set_properties (properties, i, buffer);
  1235. if (NULL_INTERVAL_P (prev_changed))
  1236. prev_changed = i;
  1237. else
  1238. prev_changed = i = merge_interval_left (i);
  1239. i = next_interval (i);
  1240. }
  1241. }
  1242. DEFUN ("remove-text-properties", Fremove_text_properties,
  1243. Sremove_text_properties, 3, 4, 0,
  1244. doc: /* Remove some properties from text from START to END.
  1245. The third argument PROPERTIES is a property list
  1246. whose property names specify the properties to remove.
  1247. \(The values stored in PROPERTIES are ignored.)
  1248. If the optional fourth argument OBJECT is a buffer (or nil, which means
  1249. the current buffer), START and END are buffer positions (integers or
  1250. markers). If OBJECT is a string, START and END are 0-based indices into it.
  1251. Return t if any property was actually removed, nil otherwise.
  1252. Use `set-text-properties' if you want to remove all text properties. */)
  1253. (start, end, properties, object)
  1254. Lisp_Object start, end, properties, object;
  1255. {
  1256. register INTERVAL i, unchanged;
  1257. register int s, len, modified = 0;
  1258. if (NILP (object))
  1259. XSETBUFFER (object, current_buffer);
  1260. i = validate_interval_range (object, &start, &end, soft);
  1261. if (NULL_INTERVAL_P (i))
  1262. return Qnil;
  1263. s = XINT (start);
  1264. len = XINT (end) - s;
  1265. if (i->position != s)
  1266. {
  1267. /* No properties on this first interval -- return if
  1268. it covers the entire region. */
  1269. if (! interval_has_some_properties (properties, i))
  1270. {
  1271. int got = (LENGTH (i) - (s - i->position));
  1272. if (got >= len)
  1273. return Qnil;
  1274. len -= got;
  1275. i = next_interval (i);
  1276. }
  1277. /* Split away the beginning of this interval; what we don't
  1278. want to modify. */
  1279. else
  1280. {
  1281. unchanged = i;
  1282. i = split_interval_right (unchanged, s - unchanged->position);
  1283. copy_properties (unchanged, i);
  1284. }
  1285. }
  1286. if (BUFFERP (object))
  1287. modify_region (XBUFFER (object), XINT (start), XINT (end), 1);
  1288. /* We are at the beginning of an interval, with len to scan */
  1289. for (;;)
  1290. {
  1291. if (i == 0)
  1292. abort ();
  1293. if (LENGTH (i) >= len)
  1294. {
  1295. if (! interval_has_some_properties (properties, i))
  1296. return modified ? Qt : Qnil;
  1297. if (LENGTH (i) == len)
  1298. {
  1299. remove_properties (properties, Qnil, i, object);
  1300. if (BUFFERP (object))
  1301. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1302. XINT (end) - XINT (start));
  1303. return Qt;
  1304. }
  1305. /* i has the properties, and goes past the change limit */
  1306. unchanged = i;
  1307. i = split_interval_left (i, len);
  1308. copy_properties (unchanged, i);
  1309. remove_properties (properties, Qnil, i, object);
  1310. if (BUFFERP (object))
  1311. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1312. XINT (end) - XINT (start));
  1313. return Qt;
  1314. }
  1315. len -= LENGTH (i);
  1316. modified += remove_properties (properties, Qnil, i, object);
  1317. i = next_interval (i);
  1318. }
  1319. }
  1320. DEFUN ("remove-list-of-text-properties", Fremove_list_of_text_properties,
  1321. Sremove_list_of_text_properties, 3, 4, 0,
  1322. doc: /* Remove some properties from text from START to END.
  1323. The third argument LIST-OF-PROPERTIES is a list of property names to remove.
  1324. If the optional fourth argument OBJECT is a buffer (or nil, which means
  1325. the current buffer), START and END are buffer positions (integers or
  1326. markers). If OBJECT is a string, START and END are 0-based indices into it.
  1327. Return t if any property was actually removed, nil otherwise. */)
  1328. (start, end, list_of_properties, object)
  1329. Lisp_Object start, end, list_of_properties, object;
  1330. {
  1331. register INTERVAL i, unchanged;
  1332. register int s, len, modified = 0;
  1333. Lisp_Object properties;
  1334. properties = list_of_properties;
  1335. if (NILP (object))
  1336. XSETBUFFER (object, current_buffer);
  1337. i = validate_interval_range (object, &start, &end, soft);
  1338. if (NULL_INTERVAL_P (i))
  1339. return Qnil;
  1340. s = XINT (start);
  1341. len = XINT (end) - s;
  1342. if (i->position != s)
  1343. {
  1344. /* No properties on this first interval -- return if
  1345. it covers the entire region. */
  1346. if (! interval_has_some_properties_list (properties, i))
  1347. {
  1348. int got = (LENGTH (i) - (s - i->position));
  1349. if (got >= len)
  1350. return Qnil;
  1351. len -= got;
  1352. i = next_interval (i);
  1353. }
  1354. /* Split away the beginning of this interval; what we don't
  1355. want to modify. */
  1356. else
  1357. {
  1358. unchanged = i;
  1359. i = split_interval_right (unchanged, s - unchanged->position);
  1360. copy_properties (unchanged, i);
  1361. }
  1362. }
  1363. /* We are at the beginning of an interval, with len to scan.
  1364. The flag `modified' records if changes have been made.
  1365. When object is a buffer, we must call modify_region before changes are
  1366. made and signal_after_change when we are done.
  1367. We call modify_region before calling remove_properties if modified == 0,
  1368. and we call signal_after_change before returning if modified != 0. */
  1369. for (;;)
  1370. {
  1371. if (i == 0)
  1372. abort ();
  1373. if (LENGTH (i) >= len)
  1374. {
  1375. if (! interval_has_some_properties_list (properties, i))
  1376. if (modified)
  1377. {
  1378. if (BUFFERP (object))
  1379. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1380. XINT (end) - XINT (start));
  1381. return Qt;
  1382. }
  1383. else
  1384. return Qnil;
  1385. if (LENGTH (i) == len)
  1386. {
  1387. if (!modified && BUFFERP (object))
  1388. modify_region (XBUFFER (object), XINT (start), XINT (end), 1);
  1389. remove_properties (Qnil, properties, i, object);
  1390. if (BUFFERP (object))
  1391. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1392. XINT (end) - XINT (start));
  1393. return Qt;
  1394. }
  1395. /* i has the properties, and goes past the change limit */
  1396. unchanged = i;
  1397. i = split_interval_left (i, len);
  1398. copy_properties (unchanged, i);
  1399. if (!modified && BUFFERP (object))
  1400. modify_region (XBUFFER (object), XINT (start), XINT (end), 1);
  1401. remove_properties (Qnil, properties, i, object);
  1402. if (BUFFERP (object))
  1403. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1404. XINT (end) - XINT (star

Large files files are truncated, but you can click here to view the full file