PageRenderTime 30ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

/src/textprop.c

http://github.com/davidswelt/aquamacs-emacs
C | 2380 lines | 1578 code | 335 blank | 467 comment | 386 complexity | 9d3d8d36db0a5cfd22763bee549656c3 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, GPL-2.0, AGPL-3.0
  1. /* Interface code for dealing with text properties.
  2. Copyright (C) 1993-1995, 1997, 1999-2016 Free Software Foundation,
  3. 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 (at
  8. 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. /* Test for membership, allowing for t (actually any non-cons) to mean the
  21. universal set. */
  22. #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
  23. /* NOTES: previous- and next- property change will have to skip
  24. zero-length intervals if they are implemented. This could be done
  25. inside next_interval and previous_interval.
  26. set_properties needs to deal with the interval property cache.
  27. It is assumed that for any interval plist, a property appears
  28. only once on the list. Although some code i.e., remove_properties,
  29. handles the more general case, the uniqueness of properties is
  30. necessary for the system to remain consistent. This requirement
  31. is enforced by the subrs installing properties onto the intervals. */
  32. enum property_set_type
  33. {
  34. TEXT_PROPERTY_REPLACE,
  35. TEXT_PROPERTY_PREPEND,
  36. TEXT_PROPERTY_APPEND
  37. };
  38. /* If o1 is a cons whose cdr is a cons, return true and set o2 to
  39. the o1's cdr. Otherwise, return false. This is handy for
  40. traversing plists. */
  41. #define PLIST_ELT_P(o1, o2) (CONSP (o1) && ((o2)=XCDR (o1), CONSP (o2)))
  42. /* verify_interval_modification saves insertion hooks here
  43. to be run later by report_interval_modification. */
  44. static Lisp_Object interval_insert_behind_hooks;
  45. static Lisp_Object interval_insert_in_front_hooks;
  46. /* Signal a `text-read-only' error. This function makes it easier
  47. to capture that error in GDB by putting a breakpoint on it. */
  48. static _Noreturn void
  49. text_read_only (Lisp_Object propval)
  50. {
  51. if (STRINGP (propval))
  52. xsignal1 (Qtext_read_only, propval);
  53. xsignal0 (Qtext_read_only);
  54. }
  55. /* Prepare to modify the text properties of BUFFER from START to END. */
  56. static void
  57. modify_text_properties (Lisp_Object buffer, Lisp_Object start, Lisp_Object end)
  58. {
  59. ptrdiff_t b = XINT (start), e = XINT (end);
  60. struct buffer *buf = XBUFFER (buffer), *old = current_buffer;
  61. set_buffer_internal (buf);
  62. prepare_to_modify_buffer_1 (b, e, NULL);
  63. BUF_COMPUTE_UNCHANGED (buf, b - 1, e);
  64. if (MODIFF <= SAVE_MODIFF)
  65. record_first_change ();
  66. MODIFF++;
  67. bset_point_before_scroll (current_buffer, Qnil);
  68. set_buffer_internal (old);
  69. }
  70. /* Complain if object is not string or buffer type. */
  71. static void
  72. CHECK_STRING_OR_BUFFER (Lisp_Object x)
  73. {
  74. CHECK_TYPE (STRINGP (x) || BUFFERP (x), Qbuffer_or_string_p, x);
  75. }
  76. /* Extract the interval at the position pointed to by BEGIN from
  77. OBJECT, a string or buffer. Additionally, check that the positions
  78. pointed to by BEGIN and END are within the bounds of OBJECT, and
  79. reverse them if *BEGIN is greater than *END. The objects pointed
  80. to by BEGIN and END may be integers or markers; if the latter, they
  81. are coerced to integers.
  82. When OBJECT is a string, we increment *BEGIN and *END
  83. to make them origin-one.
  84. Note that buffer points don't correspond to interval indices.
  85. For example, point-max is 1 greater than the index of the last
  86. character. This difference is handled in the caller, which uses
  87. the validated points to determine a length, and operates on that.
  88. Exceptions are Ftext_properties_at, Fnext_property_change, and
  89. Fprevious_property_change which call this function with BEGIN == END.
  90. Handle this case specially.
  91. If FORCE is soft (false), it's OK to return NULL. Otherwise,
  92. create an interval tree for OBJECT if one doesn't exist, provided
  93. the object actually contains text. In the current design, if there
  94. is no text, there can be no text properties. */
  95. enum { soft = false, hard = true };
  96. INTERVAL
  97. validate_interval_range (Lisp_Object object, Lisp_Object *begin,
  98. Lisp_Object *end, bool force)
  99. {
  100. INTERVAL i;
  101. ptrdiff_t searchpos;
  102. CHECK_STRING_OR_BUFFER (object);
  103. CHECK_NUMBER_COERCE_MARKER (*begin);
  104. CHECK_NUMBER_COERCE_MARKER (*end);
  105. /* If we are asked for a point, but from a subr which operates
  106. on a range, then return nothing. */
  107. if (EQ (*begin, *end) && begin != end)
  108. return NULL;
  109. if (XINT (*begin) > XINT (*end))
  110. {
  111. Lisp_Object n;
  112. n = *begin;
  113. *begin = *end;
  114. *end = n;
  115. }
  116. if (BUFFERP (object))
  117. {
  118. register struct buffer *b = XBUFFER (object);
  119. if (!(BUF_BEGV (b) <= XINT (*begin) && XINT (*begin) <= XINT (*end)
  120. && XINT (*end) <= BUF_ZV (b)))
  121. args_out_of_range (*begin, *end);
  122. i = buffer_intervals (b);
  123. /* If there's no text, there are no properties. */
  124. if (BUF_BEGV (b) == BUF_ZV (b))
  125. return NULL;
  126. searchpos = XINT (*begin);
  127. }
  128. else
  129. {
  130. ptrdiff_t len = SCHARS (object);
  131. if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end)
  132. && XINT (*end) <= len))
  133. args_out_of_range (*begin, *end);
  134. XSETFASTINT (*begin, XFASTINT (*begin));
  135. if (begin != end)
  136. XSETFASTINT (*end, XFASTINT (*end));
  137. i = string_intervals (object);
  138. if (len == 0)
  139. return NULL;
  140. searchpos = XINT (*begin);
  141. }
  142. if (!i)
  143. return (force ? create_root_interval (object) : i);
  144. return find_interval (i, searchpos);
  145. }
  146. /* Validate LIST as a property list. If LIST is not a list, then
  147. make one consisting of (LIST nil). Otherwise, verify that LIST
  148. is even numbered and thus suitable as a plist. */
  149. static Lisp_Object
  150. validate_plist (Lisp_Object list)
  151. {
  152. if (NILP (list))
  153. return Qnil;
  154. if (CONSP (list))
  155. {
  156. Lisp_Object tail = list;
  157. do
  158. {
  159. tail = XCDR (tail);
  160. if (! CONSP (tail))
  161. error ("Odd length text property list");
  162. tail = XCDR (tail);
  163. QUIT;
  164. }
  165. while (CONSP (tail));
  166. return list;
  167. }
  168. return list2 (list, Qnil);
  169. }
  170. /* Return true if interval I has all the properties,
  171. with the same values, of list PLIST. */
  172. static bool
  173. interval_has_all_properties (Lisp_Object plist, INTERVAL i)
  174. {
  175. Lisp_Object tail1, tail2;
  176. /* Go through each element of PLIST. */
  177. for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
  178. {
  179. Lisp_Object sym1 = XCAR (tail1);
  180. bool found = false;
  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 false. */
  187. if (! EQ (Fcar (XCDR (tail1)), Fcar (XCDR (tail2))))
  188. return false;
  189. /* Property has same value on both lists; go to next one. */
  190. found = true;
  191. break;
  192. }
  193. if (! found)
  194. return false;
  195. }
  196. return true;
  197. }
  198. /* Return true if the plist of interval I has any of the
  199. properties of PLIST, regardless of their values. */
  200. static bool
  201. interval_has_some_properties (Lisp_Object plist, INTERVAL i)
  202. {
  203. Lisp_Object tail1, tail2, sym;
  204. /* Go through each element of PLIST. */
  205. for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
  206. {
  207. sym = XCAR (tail1);
  208. /* Go through i's plist, looking for tail1 */
  209. for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
  210. if (EQ (sym, XCAR (tail2)))
  211. return true;
  212. }
  213. return false;
  214. }
  215. /* Return true if the plist of interval I has any of the
  216. property names in LIST, regardless of their values. */
  217. static bool
  218. interval_has_some_properties_list (Lisp_Object list, INTERVAL i)
  219. {
  220. Lisp_Object tail1, tail2, sym;
  221. /* Go through each element of LIST. */
  222. for (tail1 = list; CONSP (tail1); tail1 = XCDR (tail1))
  223. {
  224. sym = XCAR (tail1);
  225. /* Go through i's plist, looking for tail1 */
  226. for (tail2 = i->plist; CONSP (tail2); tail2 = XCDR (XCDR (tail2)))
  227. if (EQ (sym, XCAR (tail2)))
  228. return true;
  229. }
  230. return false;
  231. }
  232. /* Changing the plists of individual intervals. */
  233. /* Return the value of PROP in property-list PLIST, or Qunbound if it
  234. has none. */
  235. static Lisp_Object
  236. property_value (Lisp_Object plist, Lisp_Object prop)
  237. {
  238. Lisp_Object value;
  239. while (PLIST_ELT_P (plist, value))
  240. if (EQ (XCAR (plist), prop))
  241. return XCAR (value);
  242. else
  243. plist = XCDR (value);
  244. return Qunbound;
  245. }
  246. /* Set the properties of INTERVAL to PROPERTIES,
  247. and record undo info for the previous values.
  248. OBJECT is the string or buffer that INTERVAL belongs to. */
  249. static void
  250. set_properties (Lisp_Object properties, INTERVAL interval, Lisp_Object object)
  251. {
  252. Lisp_Object sym, value;
  253. if (BUFFERP (object))
  254. {
  255. /* For each property in the old plist which is missing from PROPERTIES,
  256. or has a different value in PROPERTIES, make an undo record. */
  257. for (sym = interval->plist;
  258. PLIST_ELT_P (sym, value);
  259. sym = XCDR (value))
  260. if (! EQ (property_value (properties, XCAR (sym)),
  261. XCAR (value)))
  262. {
  263. record_property_change (interval->position, LENGTH (interval),
  264. XCAR (sym), XCAR (value),
  265. object);
  266. }
  267. /* For each new property that has no value at all in the old plist,
  268. make an undo record binding it to nil, so it will be removed. */
  269. for (sym = properties;
  270. PLIST_ELT_P (sym, value);
  271. sym = XCDR (value))
  272. if (EQ (property_value (interval->plist, XCAR (sym)), Qunbound))
  273. {
  274. record_property_change (interval->position, LENGTH (interval),
  275. XCAR (sym), Qnil,
  276. object);
  277. }
  278. }
  279. /* Store new properties. */
  280. set_interval_plist (interval, Fcopy_sequence (properties));
  281. }
  282. /* Add the properties of PLIST to the interval I, or set
  283. the value of I's property to the value of the property on PLIST
  284. if they are different.
  285. OBJECT should be the string or buffer the interval is in.
  286. Return true if this changes I (i.e., if any members of PLIST
  287. are actually added to I's plist) */
  288. static bool
  289. add_properties (Lisp_Object plist, INTERVAL i, Lisp_Object object,
  290. enum property_set_type set_type)
  291. {
  292. Lisp_Object tail1, tail2, sym1, val1;
  293. bool changed = false;
  294. tail1 = plist;
  295. sym1 = Qnil;
  296. val1 = Qnil;
  297. /* Go through each element of PLIST. */
  298. for (tail1 = plist; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
  299. {
  300. bool found = false;
  301. sym1 = XCAR (tail1);
  302. val1 = Fcar (XCDR (tail1));
  303. /* Go through I's plist, looking for sym1 */
  304. for (tail2 = i->plist; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
  305. if (EQ (sym1, XCAR (tail2)))
  306. {
  307. Lisp_Object this_cdr;
  308. this_cdr = XCDR (tail2);
  309. /* Found the property. Now check its value. */
  310. found = true;
  311. /* The properties have the same value on both lists.
  312. Continue to the next property. */
  313. if (EQ (val1, Fcar (this_cdr)))
  314. break;
  315. /* Record this change in the buffer, for undo purposes. */
  316. if (BUFFERP (object))
  317. {
  318. record_property_change (i->position, LENGTH (i),
  319. sym1, Fcar (this_cdr), object);
  320. }
  321. /* I's property has a different value -- change it */
  322. if (set_type == TEXT_PROPERTY_REPLACE)
  323. Fsetcar (this_cdr, val1);
  324. else {
  325. if (CONSP (Fcar (this_cdr)) &&
  326. /* Special-case anonymous face properties. */
  327. (! EQ (sym1, Qface) ||
  328. NILP (Fkeywordp (Fcar (Fcar (this_cdr))))))
  329. /* The previous value is a list, so prepend (or
  330. append) the new value to this list. */
  331. if (set_type == TEXT_PROPERTY_PREPEND)
  332. Fsetcar (this_cdr, Fcons (val1, Fcar (this_cdr)));
  333. else
  334. nconc2 (Fcar (this_cdr), list1 (val1));
  335. else {
  336. /* The previous value is a single value, so make it
  337. into a list. */
  338. if (set_type == TEXT_PROPERTY_PREPEND)
  339. Fsetcar (this_cdr, list2 (val1, Fcar (this_cdr)));
  340. else
  341. Fsetcar (this_cdr, list2 (Fcar (this_cdr), val1));
  342. }
  343. }
  344. changed = true;
  345. break;
  346. }
  347. if (! found)
  348. {
  349. /* Record this change in the buffer, for undo purposes. */
  350. if (BUFFERP (object))
  351. {
  352. record_property_change (i->position, LENGTH (i),
  353. sym1, Qnil, object);
  354. }
  355. set_interval_plist (i, Fcons (sym1, Fcons (val1, i->plist)));
  356. changed = true;
  357. }
  358. }
  359. return changed;
  360. }
  361. /* For any members of PLIST, or LIST,
  362. which are properties of I, remove them from I's plist.
  363. (If PLIST is non-nil, use that, otherwise use LIST.)
  364. OBJECT is the string or buffer containing I. */
  365. static bool
  366. remove_properties (Lisp_Object plist, Lisp_Object list, INTERVAL i, Lisp_Object object)
  367. {
  368. bool changed = false;
  369. /* True means tail1 is a plist, otherwise it is a list. */
  370. bool use_plist = ! NILP (plist);
  371. Lisp_Object tail1 = use_plist ? plist : list;
  372. Lisp_Object current_plist = i->plist;
  373. /* Go through each element of LIST or PLIST. */
  374. while (CONSP (tail1))
  375. {
  376. Lisp_Object sym = XCAR (tail1);
  377. /* First, remove the symbol if it's at the head of the list */
  378. while (CONSP (current_plist) && EQ (sym, XCAR (current_plist)))
  379. {
  380. if (BUFFERP (object))
  381. record_property_change (i->position, LENGTH (i),
  382. sym, XCAR (XCDR (current_plist)),
  383. object);
  384. current_plist = XCDR (XCDR (current_plist));
  385. changed = true;
  386. }
  387. /* Go through I's plist, looking for SYM. */
  388. Lisp_Object tail2 = current_plist;
  389. while (! NILP (tail2))
  390. {
  391. Lisp_Object this = XCDR (XCDR (tail2));
  392. if (CONSP (this) && EQ (sym, XCAR (this)))
  393. {
  394. if (BUFFERP (object))
  395. record_property_change (i->position, LENGTH (i),
  396. sym, XCAR (XCDR (this)), object);
  397. Fsetcdr (XCDR (tail2), XCDR (XCDR (this)));
  398. changed = true;
  399. }
  400. tail2 = this;
  401. }
  402. /* Advance thru TAIL1 one way or the other. */
  403. tail1 = XCDR (tail1);
  404. if (use_plist && CONSP (tail1))
  405. tail1 = XCDR (tail1);
  406. }
  407. if (changed)
  408. set_interval_plist (i, current_plist);
  409. return changed;
  410. }
  411. /* Returns the interval of POSITION in OBJECT.
  412. POSITION is BEG-based. */
  413. INTERVAL
  414. interval_of (ptrdiff_t position, Lisp_Object object)
  415. {
  416. register INTERVAL i;
  417. ptrdiff_t beg, end;
  418. if (NILP (object))
  419. XSETBUFFER (object, current_buffer);
  420. else if (EQ (object, Qt))
  421. return NULL;
  422. CHECK_STRING_OR_BUFFER (object);
  423. if (BUFFERP (object))
  424. {
  425. register struct buffer *b = XBUFFER (object);
  426. beg = BUF_BEGV (b);
  427. end = BUF_ZV (b);
  428. i = buffer_intervals (b);
  429. }
  430. else
  431. {
  432. beg = 0;
  433. end = SCHARS (object);
  434. i = string_intervals (object);
  435. }
  436. if (!(beg <= position && position <= end))
  437. args_out_of_range (make_number (position), make_number (position));
  438. if (beg == end || !i)
  439. return NULL;
  440. return find_interval (i, position);
  441. }
  442. DEFUN ("text-properties-at", Ftext_properties_at,
  443. Stext_properties_at, 1, 2, 0,
  444. doc: /* Return the list of properties of the character at POSITION in OBJECT.
  445. If the optional second argument OBJECT is a buffer (or nil, which means
  446. the current buffer), POSITION is a buffer position (integer or marker).
  447. If OBJECT is a string, POSITION is a 0-based index into it.
  448. If POSITION is at the end of OBJECT, the value is nil. */)
  449. (Lisp_Object position, Lisp_Object object)
  450. {
  451. register INTERVAL i;
  452. if (NILP (object))
  453. XSETBUFFER (object, current_buffer);
  454. i = validate_interval_range (object, &position, &position, soft);
  455. if (!i)
  456. return Qnil;
  457. /* If POSITION is at the end of the interval,
  458. it means it's the end of OBJECT.
  459. There are no properties at the very end,
  460. since no character follows. */
  461. if (XINT (position) == LENGTH (i) + i->position)
  462. return Qnil;
  463. return i->plist;
  464. }
  465. DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
  466. doc: /* Return the value of POSITION's property PROP, in OBJECT.
  467. OBJECT should be a buffer or a string; if omitted or nil, it defaults
  468. to the current buffer.
  469. If POSITION is at the end of OBJECT, the value is nil. */)
  470. (Lisp_Object position, Lisp_Object prop, Lisp_Object object)
  471. {
  472. return textget (Ftext_properties_at (position, object), prop);
  473. }
  474. /* Return the value of char's property PROP, in OBJECT at POSITION.
  475. OBJECT is optional and defaults to the current buffer.
  476. If OVERLAY is non-0, then in the case that the returned property is from
  477. an overlay, the overlay found is returned in *OVERLAY, otherwise nil is
  478. returned in *OVERLAY.
  479. If POSITION is at the end of OBJECT, the value is nil.
  480. If OBJECT is a buffer, then overlay properties are considered as well as
  481. text properties.
  482. If OBJECT is a window, then that window's buffer is used, but
  483. window-specific overlays are considered only if they are associated
  484. with OBJECT. */
  485. Lisp_Object
  486. get_char_property_and_overlay (Lisp_Object position, register Lisp_Object prop, Lisp_Object object, Lisp_Object *overlay)
  487. {
  488. struct window *w = 0;
  489. CHECK_NUMBER_COERCE_MARKER (position);
  490. if (NILP (object))
  491. XSETBUFFER (object, current_buffer);
  492. if (WINDOWP (object))
  493. {
  494. CHECK_LIVE_WINDOW (object);
  495. w = XWINDOW (object);
  496. object = w->contents;
  497. }
  498. if (BUFFERP (object))
  499. {
  500. ptrdiff_t noverlays;
  501. Lisp_Object *overlay_vec;
  502. struct buffer *obuf = current_buffer;
  503. if (XINT (position) < BUF_BEGV (XBUFFER (object))
  504. || XINT (position) > BUF_ZV (XBUFFER (object)))
  505. xsignal1 (Qargs_out_of_range, position);
  506. set_buffer_temp (XBUFFER (object));
  507. USE_SAFE_ALLOCA;
  508. GET_OVERLAYS_AT (XINT (position), overlay_vec, noverlays, NULL, false);
  509. noverlays = sort_overlays (overlay_vec, noverlays, w);
  510. set_buffer_temp (obuf);
  511. /* Now check the overlays in order of decreasing priority. */
  512. while (--noverlays >= 0)
  513. {
  514. Lisp_Object tem = Foverlay_get (overlay_vec[noverlays], prop);
  515. if (!NILP (tem))
  516. {
  517. if (overlay)
  518. /* Return the overlay we got the property from. */
  519. *overlay = overlay_vec[noverlays];
  520. SAFE_FREE ();
  521. return tem;
  522. }
  523. }
  524. SAFE_FREE ();
  525. }
  526. if (overlay)
  527. /* Indicate that the return value is not from an overlay. */
  528. *overlay = Qnil;
  529. /* Not a buffer, or no appropriate overlay, so fall through to the
  530. simpler case. */
  531. return Fget_text_property (position, prop, object);
  532. }
  533. DEFUN ("get-char-property", Fget_char_property, Sget_char_property, 2, 3, 0,
  534. doc: /* Return the value of POSITION's property PROP, in OBJECT.
  535. Both overlay properties and text properties are checked.
  536. OBJECT is optional and defaults to the current buffer.
  537. If POSITION is at the end of OBJECT, the value is nil.
  538. If OBJECT is a buffer, then overlay properties are considered as well as
  539. text properties.
  540. If OBJECT is a window, then that window's buffer is used, but window-specific
  541. overlays are considered only if they are associated with OBJECT. */)
  542. (Lisp_Object position, Lisp_Object prop, Lisp_Object object)
  543. {
  544. return get_char_property_and_overlay (position, prop, object, 0);
  545. }
  546. DEFUN ("get-char-property-and-overlay", Fget_char_property_and_overlay,
  547. Sget_char_property_and_overlay, 2, 3, 0,
  548. doc: /* Like `get-char-property', but with extra overlay information.
  549. The value is a cons cell. Its car is the return value of `get-char-property'
  550. with the same arguments--that is, the value of POSITION's property
  551. PROP in OBJECT. Its cdr is the overlay in which the property was
  552. found, or nil, if it was found as a text property or not found at all.
  553. OBJECT is optional and defaults to the current buffer. OBJECT may be
  554. a string, a buffer or a window. For strings, the cdr of the return
  555. value is always nil, since strings do not have overlays. If OBJECT is
  556. a window, then that window's buffer is used, but window-specific
  557. overlays are considered only if they are associated with OBJECT. If
  558. POSITION is at the end of OBJECT, both car and cdr are nil. */)
  559. (Lisp_Object position, Lisp_Object prop, Lisp_Object object)
  560. {
  561. Lisp_Object overlay;
  562. Lisp_Object val
  563. = get_char_property_and_overlay (position, prop, object, &overlay);
  564. return Fcons (val, overlay);
  565. }
  566. DEFUN ("next-char-property-change", Fnext_char_property_change,
  567. Snext_char_property_change, 1, 2, 0,
  568. doc: /* Return the position of next text property or overlay change.
  569. This scans characters forward in the current buffer from POSITION till
  570. it finds a change in some text property, or the beginning or end of an
  571. overlay, and returns the position of that.
  572. If none is found, and LIMIT is nil or omitted, the function
  573. returns (point-max).
  574. If the optional second argument LIMIT is non-nil, the function doesn't
  575. search past position LIMIT, and returns LIMIT if nothing is found
  576. before LIMIT. LIMIT is a no-op if it is greater than (point-max). */)
  577. (Lisp_Object position, Lisp_Object limit)
  578. {
  579. Lisp_Object temp;
  580. temp = Fnext_overlay_change (position);
  581. if (! NILP (limit))
  582. {
  583. CHECK_NUMBER_COERCE_MARKER (limit);
  584. if (XINT (limit) < XINT (temp))
  585. temp = limit;
  586. }
  587. return Fnext_property_change (position, Qnil, temp);
  588. }
  589. DEFUN ("previous-char-property-change", Fprevious_char_property_change,
  590. Sprevious_char_property_change, 1, 2, 0,
  591. doc: /* Return the position of previous text property or overlay change.
  592. Scans characters backward in the current buffer from POSITION till it
  593. finds a change in some text property, or the beginning or end of an
  594. overlay, and returns the position of that.
  595. If none is found, and LIMIT is nil or omitted, the function
  596. returns (point-min).
  597. If the optional second argument LIMIT is non-nil, the function doesn't
  598. search before position LIMIT, and returns LIMIT if nothing is found
  599. before LIMIT. LIMIT is a no-op if it is less than (point-min). */)
  600. (Lisp_Object position, Lisp_Object limit)
  601. {
  602. Lisp_Object temp;
  603. temp = Fprevious_overlay_change (position);
  604. if (! NILP (limit))
  605. {
  606. CHECK_NUMBER_COERCE_MARKER (limit);
  607. if (XINT (limit) > XINT (temp))
  608. temp = limit;
  609. }
  610. return Fprevious_property_change (position, Qnil, temp);
  611. }
  612. DEFUN ("next-single-char-property-change", Fnext_single_char_property_change,
  613. Snext_single_char_property_change, 2, 4, 0,
  614. doc: /* Return the position of next text property or overlay change for a specific property.
  615. Scans characters forward from POSITION till it finds
  616. a change in the PROP property, then returns the position of the change.
  617. If the optional third argument OBJECT is a buffer (or nil, which means
  618. the current buffer), POSITION is a buffer position (integer or marker).
  619. If OBJECT is a string, POSITION is a 0-based index into it.
  620. In a string, scan runs to the end of the string, unless LIMIT is non-nil.
  621. In a buffer, if LIMIT is nil or omitted, it runs to (point-max), and the
  622. value cannot exceed that.
  623. If the optional fourth argument LIMIT is non-nil, don't search
  624. past position LIMIT; return LIMIT if nothing is found before LIMIT.
  625. The property values are compared with `eq'.
  626. If the property is constant all the way to the end of OBJECT, return the
  627. last valid position in OBJECT. */)
  628. (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit)
  629. {
  630. if (STRINGP (object))
  631. {
  632. position = Fnext_single_property_change (position, prop, object, limit);
  633. if (NILP (position))
  634. {
  635. if (NILP (limit))
  636. position = make_number (SCHARS (object));
  637. else
  638. {
  639. CHECK_NUMBER (limit);
  640. position = limit;
  641. }
  642. }
  643. }
  644. else
  645. {
  646. Lisp_Object initial_value, value;
  647. ptrdiff_t count = SPECPDL_INDEX ();
  648. if (! NILP (object))
  649. CHECK_BUFFER (object);
  650. if (BUFFERP (object) && current_buffer != XBUFFER (object))
  651. {
  652. record_unwind_current_buffer ();
  653. Fset_buffer (object);
  654. }
  655. CHECK_NUMBER_COERCE_MARKER (position);
  656. initial_value = Fget_char_property (position, prop, object);
  657. if (NILP (limit))
  658. XSETFASTINT (limit, ZV);
  659. else
  660. CHECK_NUMBER_COERCE_MARKER (limit);
  661. if (XFASTINT (position) >= XFASTINT (limit))
  662. {
  663. position = limit;
  664. if (XFASTINT (position) > ZV)
  665. XSETFASTINT (position, ZV);
  666. }
  667. else
  668. while (true)
  669. {
  670. position = Fnext_char_property_change (position, limit);
  671. if (XFASTINT (position) >= XFASTINT (limit))
  672. {
  673. position = limit;
  674. break;
  675. }
  676. value = Fget_char_property (position, prop, object);
  677. if (!EQ (value, initial_value))
  678. break;
  679. }
  680. unbind_to (count, Qnil);
  681. }
  682. return position;
  683. }
  684. DEFUN ("previous-single-char-property-change",
  685. Fprevious_single_char_property_change,
  686. Sprevious_single_char_property_change, 2, 4, 0,
  687. doc: /* Return the position of previous text property or overlay change for a specific property.
  688. Scans characters backward from POSITION till it finds
  689. a change in the PROP property, then returns the position of the change.
  690. If the optional third argument OBJECT is a buffer (or nil, which means
  691. the current buffer), POSITION is a buffer position (integer or marker).
  692. If OBJECT is a string, POSITION is a 0-based index into it.
  693. In a string, scan runs to the start of the string, unless LIMIT is non-nil.
  694. In a buffer, if LIMIT is nil or omitted, it runs to (point-min), and the
  695. value cannot be less than that.
  696. If the optional fourth argument LIMIT is non-nil, don't search back past
  697. position LIMIT; return LIMIT if nothing is found before reaching LIMIT.
  698. The property values are compared with `eq'.
  699. If the property is constant all the way to the start of OBJECT, return the
  700. first valid position in OBJECT. */)
  701. (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit)
  702. {
  703. if (STRINGP (object))
  704. {
  705. position = Fprevious_single_property_change (position, prop, object, limit);
  706. if (NILP (position))
  707. {
  708. if (NILP (limit))
  709. position = make_number (0);
  710. else
  711. {
  712. CHECK_NUMBER (limit);
  713. position = limit;
  714. }
  715. }
  716. }
  717. else
  718. {
  719. ptrdiff_t count = SPECPDL_INDEX ();
  720. if (! NILP (object))
  721. CHECK_BUFFER (object);
  722. if (BUFFERP (object) && current_buffer != XBUFFER (object))
  723. {
  724. record_unwind_current_buffer ();
  725. Fset_buffer (object);
  726. }
  727. CHECK_NUMBER_COERCE_MARKER (position);
  728. if (NILP (limit))
  729. XSETFASTINT (limit, BEGV);
  730. else
  731. CHECK_NUMBER_COERCE_MARKER (limit);
  732. if (XFASTINT (position) <= XFASTINT (limit))
  733. {
  734. position = limit;
  735. if (XFASTINT (position) < BEGV)
  736. XSETFASTINT (position, BEGV);
  737. }
  738. else
  739. {
  740. Lisp_Object initial_value
  741. = Fget_char_property (make_number (XFASTINT (position) - 1),
  742. prop, object);
  743. while (true)
  744. {
  745. position = Fprevious_char_property_change (position, limit);
  746. if (XFASTINT (position) <= XFASTINT (limit))
  747. {
  748. position = limit;
  749. break;
  750. }
  751. else
  752. {
  753. Lisp_Object value
  754. = Fget_char_property (make_number (XFASTINT (position) - 1),
  755. prop, object);
  756. if (!EQ (value, initial_value))
  757. break;
  758. }
  759. }
  760. }
  761. unbind_to (count, Qnil);
  762. }
  763. return position;
  764. }
  765. DEFUN ("next-property-change", Fnext_property_change,
  766. Snext_property_change, 1, 3, 0,
  767. doc: /* Return the position of next property change.
  768. Scans characters forward from POSITION in OBJECT till it finds
  769. a change in some text property, then returns the position of the change.
  770. If the optional second argument OBJECT is a buffer (or nil, which means
  771. the current buffer), POSITION is a buffer position (integer or marker).
  772. If OBJECT is a string, POSITION is a 0-based index into it.
  773. Return nil if LIMIT is nil or omitted, and the property is constant all
  774. the way to the end of OBJECT; if the value is non-nil, it is a position
  775. greater than POSITION, never equal.
  776. If the optional third argument LIMIT is non-nil, don't search
  777. past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
  778. (Lisp_Object position, Lisp_Object object, Lisp_Object limit)
  779. {
  780. register INTERVAL i, next;
  781. if (NILP (object))
  782. XSETBUFFER (object, current_buffer);
  783. if (!NILP (limit) && !EQ (limit, Qt))
  784. CHECK_NUMBER_COERCE_MARKER (limit);
  785. i = validate_interval_range (object, &position, &position, soft);
  786. /* If LIMIT is t, return start of next interval--don't
  787. bother checking further intervals. */
  788. if (EQ (limit, Qt))
  789. {
  790. if (!i)
  791. next = i;
  792. else
  793. next = next_interval (i);
  794. if (!next)
  795. XSETFASTINT (position, (STRINGP (object)
  796. ? SCHARS (object)
  797. : BUF_ZV (XBUFFER (object))));
  798. else
  799. XSETFASTINT (position, next->position);
  800. return position;
  801. }
  802. if (!i)
  803. return limit;
  804. next = next_interval (i);
  805. while (next && intervals_equal (i, next)
  806. && (NILP (limit) || next->position < XFASTINT (limit)))
  807. next = next_interval (next);
  808. if (!next
  809. || (next->position
  810. >= (INTEGERP (limit)
  811. ? XFASTINT (limit)
  812. : (STRINGP (object)
  813. ? SCHARS (object)
  814. : BUF_ZV (XBUFFER (object))))))
  815. return limit;
  816. else
  817. return make_number (next->position);
  818. }
  819. DEFUN ("next-single-property-change", Fnext_single_property_change,
  820. Snext_single_property_change, 2, 4, 0,
  821. doc: /* Return the position of next property change for a specific property.
  822. Scans characters forward from POSITION till it finds
  823. a change in the PROP property, then returns the position of the change.
  824. If the optional third argument OBJECT is a buffer (or nil, which means
  825. the current buffer), POSITION is a buffer position (integer or marker).
  826. If OBJECT is a string, POSITION is a 0-based index into it.
  827. The property values are compared with `eq'.
  828. Return nil if LIMIT is nil or omitted, and the property is constant all
  829. the way to the end of OBJECT; if the value is non-nil, it is a position
  830. greater than POSITION, never equal.
  831. If the optional fourth argument LIMIT is non-nil, don't search
  832. past position LIMIT; return LIMIT if nothing is found before LIMIT. */)
  833. (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit)
  834. {
  835. register INTERVAL i, next;
  836. register Lisp_Object here_val;
  837. if (NILP (object))
  838. XSETBUFFER (object, current_buffer);
  839. if (!NILP (limit))
  840. CHECK_NUMBER_COERCE_MARKER (limit);
  841. i = validate_interval_range (object, &position, &position, soft);
  842. if (!i)
  843. return limit;
  844. here_val = textget (i->plist, prop);
  845. next = next_interval (i);
  846. while (next
  847. && EQ (here_val, textget (next->plist, prop))
  848. && (NILP (limit) || next->position < XFASTINT (limit)))
  849. next = next_interval (next);
  850. if (!next
  851. || (next->position
  852. >= (INTEGERP (limit)
  853. ? XFASTINT (limit)
  854. : (STRINGP (object)
  855. ? SCHARS (object)
  856. : BUF_ZV (XBUFFER (object))))))
  857. return limit;
  858. else
  859. return make_number (next->position);
  860. }
  861. DEFUN ("previous-property-change", Fprevious_property_change,
  862. Sprevious_property_change, 1, 3, 0,
  863. doc: /* Return the position of previous property change.
  864. Scans characters backwards from POSITION in OBJECT till it finds
  865. a change in some text property, then returns the position of the change.
  866. If the optional second argument OBJECT is a buffer (or nil, which means
  867. the current buffer), POSITION is a buffer position (integer or marker).
  868. If OBJECT is a string, POSITION is a 0-based index into it.
  869. Return nil if LIMIT is nil or omitted, and the property is constant all
  870. the way to the start of OBJECT; if the value is non-nil, it is a position
  871. less than POSITION, never equal.
  872. If the optional third argument LIMIT is non-nil, don't search
  873. back past position LIMIT; return LIMIT if nothing is found until LIMIT. */)
  874. (Lisp_Object position, Lisp_Object object, Lisp_Object limit)
  875. {
  876. register INTERVAL i, previous;
  877. if (NILP (object))
  878. XSETBUFFER (object, current_buffer);
  879. if (!NILP (limit))
  880. CHECK_NUMBER_COERCE_MARKER (limit);
  881. i = validate_interval_range (object, &position, &position, soft);
  882. if (!i)
  883. return limit;
  884. /* Start with the interval containing the char before point. */
  885. if (i->position == XFASTINT (position))
  886. i = previous_interval (i);
  887. previous = previous_interval (i);
  888. while (previous && intervals_equal (previous, i)
  889. && (NILP (limit)
  890. || (previous->position + LENGTH (previous) > XFASTINT (limit))))
  891. previous = previous_interval (previous);
  892. if (!previous
  893. || (previous->position + LENGTH (previous)
  894. <= (INTEGERP (limit)
  895. ? XFASTINT (limit)
  896. : (STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object))))))
  897. return limit;
  898. else
  899. return make_number (previous->position + LENGTH (previous));
  900. }
  901. DEFUN ("previous-single-property-change", Fprevious_single_property_change,
  902. Sprevious_single_property_change, 2, 4, 0,
  903. doc: /* Return the position of previous property change for a specific property.
  904. Scans characters backward from POSITION till it finds
  905. a change in the PROP property, then returns the position of the change.
  906. If the optional third argument OBJECT is a buffer (or nil, which means
  907. the current buffer), POSITION is a buffer position (integer or marker).
  908. If OBJECT is a string, POSITION is a 0-based index into it.
  909. The property values are compared with `eq'.
  910. Return nil if LIMIT is nil or omitted, and the property is constant all
  911. the way to the start of OBJECT; if the value is non-nil, it is a position
  912. less than POSITION, never equal.
  913. If the optional fourth argument LIMIT is non-nil, don't search
  914. back past position LIMIT; return LIMIT if nothing is found until LIMIT. */)
  915. (Lisp_Object position, Lisp_Object prop, Lisp_Object object, Lisp_Object limit)
  916. {
  917. register INTERVAL i, previous;
  918. register Lisp_Object here_val;
  919. if (NILP (object))
  920. XSETBUFFER (object, current_buffer);
  921. if (!NILP (limit))
  922. CHECK_NUMBER_COERCE_MARKER (limit);
  923. i = validate_interval_range (object, &position, &position, soft);
  924. /* Start with the interval containing the char before point. */
  925. if (i && i->position == XFASTINT (position))
  926. i = previous_interval (i);
  927. if (!i)
  928. return limit;
  929. here_val = textget (i->plist, prop);
  930. previous = previous_interval (i);
  931. while (previous
  932. && EQ (here_val, textget (previous->plist, prop))
  933. && (NILP (limit)
  934. || (previous->position + LENGTH (previous) > XFASTINT (limit))))
  935. previous = previous_interval (previous);
  936. if (!previous
  937. || (previous->position + LENGTH (previous)
  938. <= (INTEGERP (limit)
  939. ? XFASTINT (limit)
  940. : (STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object))))))
  941. return limit;
  942. else
  943. return make_number (previous->position + LENGTH (previous));
  944. }
  945. /* Used by add-text-properties and add-face-text-property. */
  946. static Lisp_Object
  947. add_text_properties_1 (Lisp_Object start, Lisp_Object end,
  948. Lisp_Object properties, Lisp_Object object,
  949. enum property_set_type set_type) {
  950. INTERVAL i, unchanged;
  951. ptrdiff_t s, len;
  952. bool modified = false;
  953. bool first_time = true;
  954. properties = validate_plist (properties);
  955. if (NILP (properties))
  956. return Qnil;
  957. if (NILP (object))
  958. XSETBUFFER (object, current_buffer);
  959. retry:
  960. i = validate_interval_range (object, &start, &end, hard);
  961. if (!i)
  962. return Qnil;
  963. s = XINT (start);
  964. len = XINT (end) - s;
  965. /* If this interval already has the properties, we can skip it. */
  966. if (interval_has_all_properties (properties, i))
  967. {
  968. ptrdiff_t got = LENGTH (i) - (s - i->position);
  969. do
  970. {
  971. if (got >= len)
  972. return Qnil;
  973. len -= got;
  974. i = next_interval (i);
  975. got = LENGTH (i);
  976. }
  977. while (interval_has_all_properties (properties, i));
  978. }
  979. else if (i->position != s)
  980. {
  981. /* If we're not starting on an interval boundary, we have to
  982. split this interval. */
  983. unchanged = i;
  984. i = split_interval_right (unchanged, s - unchanged->position);
  985. copy_properties (unchanged, i);
  986. }
  987. if (BUFFERP (object) && first_time)
  988. {
  989. ptrdiff_t prev_total_length = TOTAL_LENGTH (i);
  990. ptrdiff_t prev_pos = i->position;
  991. modify_text_properties (object, start, end);
  992. /* If someone called us recursively as a side effect of
  993. modify_text_properties, and changed the intervals behind our back
  994. (could happen if lock_file, called by prepare_to_modify_buffer,
  995. triggers redisplay, and that calls add-text-properties again
  996. in the same buffer), we cannot continue with I, because its
  997. data changed. So we restart the interval analysis anew. */
  998. if (TOTAL_LENGTH (i) != prev_total_length
  999. || i->position != prev_pos)
  1000. {
  1001. first_time = false;
  1002. goto retry;
  1003. }
  1004. }
  1005. /* We are at the beginning of interval I, with LEN chars to scan. */
  1006. for (;;)
  1007. {
  1008. eassert (i != 0);
  1009. if (LENGTH (i) >= len)
  1010. {
  1011. if (interval_has_all_properties (properties, i))
  1012. {
  1013. if (BUFFERP (object))
  1014. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1015. XINT (end) - XINT (start));
  1016. eassert (modified);
  1017. return Qt;
  1018. }
  1019. if (LENGTH (i) == len)
  1020. {
  1021. add_properties (properties, i, object, set_type);
  1022. if (BUFFERP (object))
  1023. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1024. XINT (end) - XINT (start));
  1025. return Qt;
  1026. }
  1027. /* i doesn't have the properties, and goes past the change limit */
  1028. unchanged = i;
  1029. i = split_interval_left (unchanged, len);
  1030. copy_properties (unchanged, i);
  1031. add_properties (properties, i, object, set_type);
  1032. if (BUFFERP (object))
  1033. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1034. XINT (end) - XINT (start));
  1035. return Qt;
  1036. }
  1037. len -= LENGTH (i);
  1038. modified |= add_properties (properties, i, object, set_type);
  1039. i = next_interval (i);
  1040. }
  1041. }
  1042. /* Callers note, this can GC when OBJECT is a buffer (or nil). */
  1043. DEFUN ("add-text-properties", Fadd_text_properties,
  1044. Sadd_text_properties, 3, 4, 0,
  1045. doc: /* Add properties to the text from START to END.
  1046. The third argument PROPERTIES is a property list
  1047. specifying the property values to add. If the optional fourth argument
  1048. OBJECT is a buffer (or nil, which means the current buffer),
  1049. START and END are buffer positions (integers or markers).
  1050. If OBJECT is a string, START and END are 0-based indices into it.
  1051. Return t if any property value actually changed, nil otherwise. */)
  1052. (Lisp_Object start, Lisp_Object end, Lisp_Object properties,
  1053. Lisp_Object object)
  1054. {
  1055. return add_text_properties_1 (start, end, properties, object,
  1056. TEXT_PROPERTY_REPLACE);
  1057. }
  1058. /* Callers note, this can GC when OBJECT is a buffer (or nil). */
  1059. DEFUN ("put-text-property", Fput_text_property,
  1060. Sput_text_property, 4, 5, 0,
  1061. doc: /* Set one property of the text from START to END.
  1062. The third and fourth arguments PROPERTY and VALUE
  1063. specify the property to add.
  1064. If the optional fifth argument OBJECT is a buffer (or nil, which means
  1065. the current buffer), START and END are buffer positions (integers or
  1066. markers). If OBJECT is a string, START and END are 0-based indices into it. */)
  1067. (Lisp_Object start, Lisp_Object end, Lisp_Object property,
  1068. Lisp_Object value, Lisp_Object object)
  1069. {
  1070. AUTO_LIST2 (properties, property, value);
  1071. Fadd_text_properties (start, end, properties, object);
  1072. return Qnil;
  1073. }
  1074. DEFUN ("set-text-properties", Fset_text_properties,
  1075. Sset_text_properties, 3, 4, 0,
  1076. doc: /* Completely replace properties of text from START to END.
  1077. The third argument PROPERTIES is the new property list.
  1078. If the optional fourth argument OBJECT is a buffer (or nil, which means
  1079. the current buffer), START and END are buffer positions (integers or
  1080. markers). If OBJECT is a string, START and END are 0-based indices into it.
  1081. If PROPERTIES is nil, the effect is to remove all properties from
  1082. the designated part of OBJECT. */)
  1083. (Lisp_Object start, Lisp_Object end, Lisp_Object properties, Lisp_Object object)
  1084. {
  1085. return set_text_properties (start, end, properties, object, Qt);
  1086. }
  1087. DEFUN ("add-face-text-property", Fadd_face_text_property,
  1088. Sadd_face_text_property, 3, 5, 0,
  1089. doc: /* Add the face property to the text from START to END.
  1090. FACE specifies the face to add. It should be a valid value of the
  1091. `face' property (typically a face name or a plist of face attributes
  1092. and values).
  1093. If any text in the region already has a non-nil `face' property, those
  1094. face(s) are retained. This is done by setting the `face' property to
  1095. a list of faces, with FACE as the first element (by default) and the
  1096. pre-existing faces as the remaining elements.
  1097. If optional fourth argument APPEND is non-nil, append FACE to the end
  1098. of the face list instead.
  1099. If optional fifth argument OBJECT is a buffer (or nil, which means the
  1100. current buffer), START and END are buffer positions (integers or
  1101. markers). If OBJECT is a string, START and END are 0-based indices
  1102. into it. */)
  1103. (Lisp_Object start, Lisp_Object end, Lisp_Object face,
  1104. Lisp_Object append, Lisp_Object object)
  1105. {
  1106. AUTO_LIST2 (properties, Qface, face);
  1107. add_text_properties_1 (start, end, properties, object,
  1108. (NILP (append)
  1109. ? TEXT_PROPERTY_PREPEND
  1110. : TEXT_PROPERTY_APPEND));
  1111. return Qnil;
  1112. }
  1113. /* Replace properties of text from START to END with new list of
  1114. properties PROPERTIES. OBJECT is the buffer or string containing
  1115. the text. OBJECT nil means use the current buffer.
  1116. COHERENT_CHANGE_P nil means this is being called as an internal
  1117. subroutine, rather than as a change primitive with checking of
  1118. read-only, invoking change hooks, etc.. Value is nil if the
  1119. function _detected_ that it did not replace any properties, non-nil
  1120. otherwise. */
  1121. Lisp_Object
  1122. set_text_properties (Lisp_Object start, Lisp_Object end, Lisp_Object properties,
  1123. Lisp_Object object, Lisp_Object coherent_change_p)
  1124. {
  1125. register INTERVAL i;
  1126. Lisp_Object ostart, oend;
  1127. ostart = start;
  1128. oend = end;
  1129. properties = validate_plist (properties);
  1130. if (NILP (object))
  1131. XSETBUFFER (object, current_buffer);
  1132. /* If we want no properties for a whole string,
  1133. get rid of its intervals. */
  1134. if (NILP (properties) && STRINGP (object)
  1135. && XFASTINT (start) == 0
  1136. && XFASTINT (end) == SCHARS (object))
  1137. {
  1138. if (!string_intervals (object))
  1139. return Qnil;
  1140. set_string_intervals (object, NULL);
  1141. return Qt;
  1142. }
  1143. i = validate_interval_range (object, &start, &end, soft);
  1144. if (!i)
  1145. {
  1146. /* If buffer has no properties, and we want none, return now. */
  1147. if (NILP (properties))
  1148. return Qnil;
  1149. /* Restore the original START and END values
  1150. because validate_interval_range increments them for strings. */
  1151. start = ostart;
  1152. end = oend;
  1153. i = validate_interval_range (object, &start, &end, hard);
  1154. /* This can return if start == end. */
  1155. if (!i)
  1156. return Qnil;
  1157. }
  1158. if (BUFFERP (object) && !NILP (coherent_change_p))
  1159. modify_text_properties (object, start, end);
  1160. set_text_properties_1 (start, end, properties, object, i);
  1161. if (BUFFERP (object) && !NILP (coherent_change_p))
  1162. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1163. XINT (end) - XINT (start));
  1164. return Qt;
  1165. }
  1166. /* Replace properties of text from START to END with new list of
  1167. properties PROPERTIES. OBJECT is the buffer or string containing
  1168. the text. This does not obey any hooks.
  1169. You should provide the interval that START is located in as I.
  1170. START and END can be in any order. */
  1171. void
  1172. set_text_properties_1 (Lisp_Object start, Lisp_Object end, Lisp_Object properties, Lisp_Object object, INTERVAL i)
  1173. {
  1174. register INTERVAL prev_changed = NULL;
  1175. register ptrdiff_t s, len;
  1176. INTERVAL unchanged;
  1177. if (XINT (start) < XINT (end))
  1178. {
  1179. s = XINT (start);
  1180. len = XINT (end) - s;
  1181. }
  1182. else if (XINT (end) < XINT (start))
  1183. {
  1184. s = XINT (end);
  1185. len = XINT (start) - s;
  1186. }
  1187. else
  1188. return;
  1189. eassert (i);
  1190. if (i->position != s)
  1191. {
  1192. unchanged = i;
  1193. i = split_interval_right (unchanged, s - unchanged->position);
  1194. if (LENGTH (i) > len)
  1195. {
  1196. copy_properties (unchanged, i);
  1197. i = split_interval_left (i, len);
  1198. set_properties (properties, i, object);
  1199. return;
  1200. }
  1201. set_properties (properties, i, object);
  1202. if (LENGTH (i) == len)
  1203. return;
  1204. prev_changed = i;
  1205. len -= LENGTH (i);
  1206. i = next_interval (i);
  1207. }
  1208. /* We are starting at the beginning of an interval I. LEN is positive. */
  1209. do
  1210. {
  1211. eassert (i != 0);
  1212. if (LENGTH (i) >= len)
  1213. {
  1214. if (LENGTH (i) > len)
  1215. i = split_interval_left (i, len);
  1216. /* We have to call set_properties even if we are going to
  1217. merge the intervals, so as to make the undo records
  1218. and cause redisplay to happen. */
  1219. set_properties (properties, i, object);
  1220. if (prev_changed)
  1221. merge_interval_left (i);
  1222. return;
  1223. }
  1224. len -= LENGTH (i);
  1225. /* We have to call set_properties even if we are going to
  1226. merge the intervals, so as to make the undo records
  1227. and cause redisplay to happen. */
  1228. set_properties (properties, i, object);
  1229. if (!prev_changed)
  1230. prev_changed = i;
  1231. else
  1232. prev_changed = i = merge_interval_left (i);
  1233. i = next_interval (i);
  1234. }
  1235. while (len > 0);
  1236. }
  1237. DEFUN ("remove-text-properties", Fremove_text_properties,
  1238. Sremove_text_properties, 3, 4, 0,
  1239. doc: /* Remove some properties from text from START to END.
  1240. The third argument PROPERTIES is a property list
  1241. whose property names specify the properties to remove.
  1242. \(The values stored in PROPERTIES are ignored.)
  1243. If the optional fourth argument OBJECT is a buffer (or nil, which means
  1244. the current buffer), START and END are buffer positions (integers or
  1245. markers). If OBJECT is a string, START and END are 0-based indices into it.
  1246. Return t if any property was actually removed, nil otherwise.
  1247. Use `set-text-properties' if you want to remove all text properties. */)
  1248. (Lisp_Object start, Lisp_Object end, Lisp_Object properties, Lisp_Object object)
  1249. {
  1250. INTERVAL i, unchanged;
  1251. ptrdiff_t s, len;
  1252. bool modified = false;
  1253. bool first_time = true;
  1254. if (NILP (object))
  1255. XSETBUFFER (object, current_buffer);
  1256. retry:
  1257. i = validate_interval_range (object, &start, &end, soft);
  1258. if (!i)
  1259. return Qnil;
  1260. s = XINT (start);
  1261. len = XINT (end) - s;
  1262. /* If there are no properties on this entire interval, return. */
  1263. if (! interval_has_some_properties (properties, i))
  1264. {
  1265. ptrdiff_t got = LENGTH (i) - (s - i->position);
  1266. do
  1267. {
  1268. if (got >= len)
  1269. return Qnil;
  1270. len -= got;
  1271. i = next_interval (i);
  1272. got = LENGTH (i);
  1273. }
  1274. while (! interval_has_some_properties (properties, i));
  1275. }
  1276. /* Split away the beginning of this interval; what we don't
  1277. want to modify. */
  1278. else if (i->position != s)
  1279. {
  1280. unchanged = i;
  1281. i = split_interval_right (unchanged, s - unchanged->position);
  1282. copy_properties (unchanged, i);
  1283. }
  1284. if (BUFFERP (object) && first_time)
  1285. {
  1286. ptrdiff_t prev_total_length = TOTAL_LENGTH (i);
  1287. ptrdiff_t prev_pos = i->position;
  1288. modify_text_properties (object, start, end);
  1289. /* If someone called us recursively as a side effect of
  1290. modify_text_properties, and changed the intervals behind our back
  1291. (could happen if lock_file, called by prepare_to_modify_buffer,
  1292. triggers redisplay, and that calls add-text-properties again
  1293. in the same buffer), we cannot continue with I, because its
  1294. data changed. So we restart the interval analysis anew. */
  1295. if (TOTAL_LENGTH (i) != prev_total_length
  1296. || i->position != prev_pos)
  1297. {
  1298. first_time = false;
  1299. goto retry;
  1300. }
  1301. }
  1302. /* We are at the beginning of an interval, with len to scan */
  1303. for (;;)
  1304. {
  1305. eassert (i != 0);
  1306. if (LENGTH (i) >= len)
  1307. {
  1308. if (! interval_has_some_properties (properties, i))
  1309. {
  1310. eassert (modified);
  1311. if (BUFFERP (object))
  1312. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1313. XINT (end) - XINT (start));
  1314. return Qt;
  1315. }
  1316. if (LENGTH (i) == len)
  1317. {
  1318. remove_properties (properties, Qnil, i, object);
  1319. if (BUFFERP (object))
  1320. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1321. XINT (end) - XINT (start));
  1322. return Qt;
  1323. }
  1324. /* i has the properties, and goes past the change limit */
  1325. unchanged = i;
  1326. i = split_interval_left (i, len);
  1327. copy_properties (unchanged, i);
  1328. remove_properties (properties, Qnil, i, object);
  1329. if (BUFFERP (object))
  1330. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1331. XINT (end) - XINT (start));
  1332. return Qt;
  1333. }
  1334. len -= LENGTH (i);
  1335. modified |= remove_properties (properties, Qnil, i, object);
  1336. i = next_interval (i);
  1337. }
  1338. }
  1339. DEFUN ("remove-list-of-text-properties", Fremove_list_of_text_properties,
  1340. Sremove_list_of_text_properties, 3, 4, 0,
  1341. doc: /* Remove some properties from text from START to END.
  1342. The third argument LIST-OF-PROPERTIES is a list of property names to remove.
  1343. If the optional fourth argument OBJECT is a buffer (or nil, which means
  1344. the current buffer), START and END are buffer positions (integers or
  1345. markers). If OBJECT is a string, START and END are 0-based indices into it.
  1346. Return t if any property was actually removed, nil otherwise. */)
  1347. (Lisp_Object start, Lisp_Object end, Lisp_Object list_of_properties, Lisp_Object object)
  1348. {
  1349. INTERVAL i, unchanged;
  1350. ptrdiff_t s, len;
  1351. bool modified = false;
  1352. Lisp_Object properties;
  1353. properties = list_of_properties;
  1354. if (NILP (object))
  1355. XSETBUFFER (object, current_buffer);
  1356. i = validate_interval_range (object, &start, &end, soft);
  1357. if (!i)
  1358. return Qnil;
  1359. s = XINT (start);
  1360. len = XINT (end) - s;
  1361. /* If there are no properties on the interval, return. */
  1362. if (! interval_has_some_properties_list (properties, i))
  1363. {
  1364. ptrdiff_t got = LENGTH (i) - (s - i->position);
  1365. do
  1366. {
  1367. if (got >= len)
  1368. return Qnil;
  1369. len -= got;
  1370. i = next_interval (i);
  1371. got = LENGTH (i);
  1372. }
  1373. while (! interval_has_some_properties_list (properties, i));
  1374. }
  1375. /* Split away the beginning of this interval; what we don't
  1376. want to modify. */
  1377. else if (i->position != s)
  1378. {
  1379. unchanged = i;
  1380. i = split_interval_right (unchanged, s - unchanged->position);
  1381. copy_properties (unchanged, i);
  1382. }
  1383. /* We are at the beginning of an interval, with len to scan.
  1384. The flag MODIFIED records if changes have been made.
  1385. When object is a buffer, we must call modify_text_properties
  1386. before changes are made and signal_after_change when we are done.
  1387. Call modify_text_properties before calling remove_properties if !MODIFIED,
  1388. and call signal_after_change before returning if MODIFIED. */
  1389. for (;;)
  1390. {
  1391. eassert (i != 0);
  1392. if (LENGTH (i) >= len)
  1393. {
  1394. if (! interval_has_some_properties_list (properties, i))
  1395. {
  1396. if (modified)
  1397. {
  1398. if (BUFFERP (object))
  1399. signal_after_change (XINT (start),
  1400. XINT (end) - XINT (start),
  1401. XINT (end) - XINT (start));
  1402. return Qt;
  1403. }
  1404. else
  1405. return Qnil;
  1406. }
  1407. else if (LENGTH (i) == len)
  1408. {
  1409. if (!modified && BUFFERP (object))
  1410. modify_text_properties (object, start, end);
  1411. remove_properties (Qnil, properties, i, object);
  1412. if (BUFFERP (object))
  1413. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1414. XINT (end) - XINT (start));
  1415. return Qt;
  1416. }
  1417. else
  1418. { /* i has the properties, and goes past the change limit. */
  1419. unchanged = i;
  1420. i = split_interval_left (i, len);
  1421. copy_properties (unchanged, i);
  1422. if (!modified && BUFFERP (object))
  1423. modify_text_properties (object, start, end);
  1424. remove_properties (Qnil, properties, i, object);
  1425. if (BUFFERP (object))
  1426. signal_after_change (XINT (start), XINT (end) - XINT (start),
  1427. XINT (end) - XINT (start));
  1428. return Qt;
  1429. }
  1430. }
  1431. if (interval_has_some_properties_list (properties, i))
  1432. {
  1433. if (!modified && BUFFERP (object))
  1434. modify_text_properties (object, start, end);
  1435. remove_properties (Qnil, properties, i, object);
  1436. modified = true;
  1437. }
  1438. len -= LENGTH (i);
  1439. i = next_interval (i);
  1440. if (!i)
  1441. {
  1442. if (modified)
  1443. {
  1444. if (BUFFERP (object))
  1445. signal_after_change (XINT (start),
  1446. XINT (end) - XINT (start),
  1447. XINT (end) - XINT (start));
  1448. return Qt;
  1449. }
  1450. else
  1451. return Qnil;
  1452. }
  1453. }
  1454. }
  1455. DEFUN ("text-property-any", Ftext_property_any,
  1456. Stext_property_any, 4, 5, 0,
  1457. doc: /* Check text from START to END for property PROPERTY equaling VALUE.
  1458. If so, return the position of the first character whose property PROPERTY
  1459. is `eq' to VALUE. Otherwise return nil.
  1460. If the optional fifth argument OBJECT is a buffer (or nil, which means
  1461. the current buffer), START and END are buffer positions (integers or
  1462. markers). If OBJECT is a string, START and END are 0-based indices into it. */)
  1463. (Lisp_Object start, Lisp_Object end, Lisp_Object property, Lisp_Object value, Lisp_Object object)
  1464. {
  1465. register INTERVAL i;
  1466. register ptrdiff_t e, pos;
  1467. if (NILP (object))
  1468. XSETBUFFER (object, current_buffer);
  1469. i = validate_interval_range (object, &start, &end, soft);
  1470. if (!i)
  1471. return (!NILP (value) || EQ (start, end) ? Qnil : start);
  1472. e = XINT (end);
  1473. while (i)
  1474. {
  1475. if (i->position >= e)
  1476. break;
  1477. if (EQ (textget (i->plist, property), value))
  1478. {
  1479. pos = i->position;
  1480. if (pos < XINT (start))
  1481. pos = XINT (start);
  1482. return make_number (pos);
  1483. }
  1484. i = next_interval (i);
  1485. }
  1486. return Qnil;
  1487. }
  1488. DEFUN ("text-property-not-all", Ftext_property_not_all,
  1489. Stext_property_not_all, 4, 5, 0,
  1490. doc: /* Check text from START to END for property PROPERTY not equaling VALUE.
  1491. If so, return the position of the first character whose property PROPERTY
  1492. is not `eq' to VALUE. Otherwise, return nil.
  1493. If the optional fifth argument OBJECT is a buffer (or nil, which means
  1494. the current buffer), START and END are buffer positions (integers or
  1495. markers). If OBJECT is a string, START and END are 0-based indices into it. */)
  1496. (Lisp_Object start, Lisp_Object end, Lisp_Object property, Lisp_Object value, Lisp_Object object)
  1497. {
  1498. register INTERVAL i;
  1499. register ptrdiff_t s, e;
  1500. if (NILP (object))
  1501. XSETBUFFER (object, current_buffer);
  1502. i = validate_interval_range (object, &start, &end, soft);
  1503. if (!i)
  1504. return (NILP (value) || EQ (start, end)) ? Qnil : start;
  1505. s = XINT (start);
  1506. e = XINT (end);
  1507. while (i)
  1508. {
  1509. if (i->position >= e)
  1510. break;
  1511. if (! EQ (textget (i->plist, property), value))
  1512. {
  1513. if (i->position > s)
  1514. s = i->position;
  1515. return make_number (s);
  1516. }
  1517. i = next_interval (i);
  1518. }
  1519. return Qnil;
  1520. }
  1521. /* Return the direction from which the text-property PROP would be
  1522. inherited by any new text inserted at POS: 1 if it would be
  1523. inherited from the char after POS, -1 if it would be inherited from
  1524. the char before POS, and 0 if from neither.
  1525. BUFFER can be either a buffer or nil (meaning current buffer). */
  1526. int
  1527. text_property_stickiness (Lisp_Object prop, Lisp_Object pos, Lisp_Object buffer)
  1528. {
  1529. bool ignore_previous_character;
  1530. Lisp_Object prev_pos = make_number (XINT (pos) - 1);
  1531. Lisp_Object front_sticky;
  1532. bool is_rear_sticky = true, is_front_sticky = false; /* defaults */
  1533. Lisp_Object defalt = Fassq (prop, Vtext_property_default_nonsticky);
  1534. if (NILP (buffer))
  1535. XSETBUFFER (buffer, current_buffer);
  1536. ignore_previous_character = XINT (pos) <= BUF_BEGV (XBUFFER (buffer));
  1537. if (ignore_previous_character || (CONSP (defalt) && !NILP (XCDR (defalt))))
  1538. is_rear_sticky = false;
  1539. else
  1540. {
  1541. Lisp_Object rear_non_sticky
  1542. = Fget_text_property (prev_pos, Qrear_nonsticky, buffer);
  1543. if (!NILP (CONSP (rear_non_sticky)
  1544. ? Fmemq (prop, rear_non_sticky)
  1545. : rear_non_sticky))
  1546. /* PROP is rear-non-sticky. */
  1547. is_rear_sticky = false;
  1548. }
  1549. /* Consider following character. */
  1550. /* This signals an arg-out-of-range error if pos is outside the
  1551. buffer's accessible range. */
  1552. front_sticky = Fget_text_property (pos, Qfront_sticky, buffer);
  1553. if (EQ (front_sticky, Qt)
  1554. || (CONSP (front_sticky)
  1555. && !NILP (Fmemq (prop, front_sticky))))
  1556. /* PROP is inherited from after. */
  1557. is_front_sticky = true;
  1558. /* Simple cases, where the properties are consistent. */
  1559. if (is_rear_sticky && !is_front_sticky)
  1560. return -1;
  1561. else if (!is_rear_sticky && is_front_sticky)
  1562. return 1;
  1563. else if (!is_rear_sticky && !is_front_sticky)
  1564. return 0;
  1565. /* The stickiness properties are inconsistent, so we have to
  1566. disambiguate. Basically, rear-sticky wins, _except_ if the
  1567. property that would be inherited has a value of nil, in which case
  1568. front-sticky wins. */
  1569. if (ignore_previous_character
  1570. || NILP (Fget_text_property (prev_pos, prop, buffer)))
  1571. return 1;
  1572. else
  1573. return -1;
  1574. }
  1575. /* Copying properties between objects. */
  1576. /* Add properties from START to END of SRC, starting at POS in DEST.
  1577. SRC and DEST may each refer to strings or buffers.
  1578. Optional sixth argument PROP causes only that property to be copied.
  1579. Properties are copied to DEST as if by `add-text-properties'.
  1580. Return t if any property value actually changed, nil otherwise. */
  1581. /* Note this can GC when DEST is a buffer. */
  1582. Lisp_Object
  1583. copy_text_properties (Lisp_Object start, Lisp_Object end, Lisp_Object src,
  1584. Lisp_Object pos, Lisp_Object dest, Lisp_Object prop)
  1585. {
  1586. INTERVAL i;
  1587. Lisp_Object res;
  1588. Lisp_Object stuff;
  1589. Lisp_Object plist;
  1590. ptrdiff_t s, e, e2, p, len;
  1591. bool modified = false;
  1592. i = validate_interval_range (src, &start, &end, soft);
  1593. if (!i)
  1594. return Qnil;
  1595. CHECK_NUMBER_COERCE_MARKER (pos);
  1596. {
  1597. Lisp_Object dest_start, dest_end;
  1598. e = XINT (pos) + (XINT (end) - XINT (start));
  1599. if (MOST_POSITIVE_FIXNUM < e)
  1600. args_out_of_range (pos, end);
  1601. dest_start = pos;
  1602. XSETFASTINT (dest_end, e);
  1603. /* Apply this to a copy of pos; it will try to increment its arguments,
  1604. which we don't want. */
  1605. validate_interval_range (dest, &dest_start, &dest_end, soft);
  1606. }
  1607. s = XINT (start);
  1608. e = XINT (end);
  1609. p = XINT (pos);
  1610. stuff = Qnil;
  1611. while (s < e)
  1612. {
  1613. e2 = i->position + LENGTH (i);
  1614. if (e2 > e)
  1615. e2 = e;
  1616. len = e2 - s;
  1617. plist = i->plist;
  1618. if (! NILP (prop))
  1619. while (! NILP (plist))
  1620. {
  1621. if (EQ (Fcar (plist), prop))
  1622. {
  1623. plist = list2 (prop, Fcar (Fcdr (plist)));
  1624. break;
  1625. }
  1626. plist = Fcdr (Fcdr (plist));
  1627. }
  1628. if (! NILP (plist))
  1629. /* Must defer modifications to the interval tree in case
  1630. src and dest refer to the same string or buffer. */
  1631. stuff = Fcons (list3 (make_number (p), make_number (p + len), plist),
  1632. stuff);
  1633. i = next_interval (i);
  1634. if (!i)
  1635. break;
  1636. p += len;
  1637. s = i->position;
  1638. }
  1639. while (! NILP (stuff))
  1640. {
  1641. res = Fcar (stuff);
  1642. res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)),
  1643. Fcar (Fcdr (Fcdr (res))), dest);
  1644. if (! NILP (res))
  1645. modified = true;
  1646. stuff = Fcdr (stuff);
  1647. }
  1648. return modified ? Qt : Qnil;
  1649. }
  1650. /* Return a list representing the text properties of OBJECT between
  1651. START and END. if PROP is non-nil, report only on that property.
  1652. Each result list element has the form (S E PLIST), where S and E
  1653. are positions in OBJECT and PLIST is a property list containing the
  1654. text properties of OBJECT between S and E. Value is nil if OBJECT
  1655. doesn't contain text properties between START and END. */
  1656. Lisp_Object
  1657. text_property_list (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object prop)
  1658. {
  1659. struct interval *i;
  1660. Lisp_Object result;
  1661. result = Qnil;
  1662. i = validate_interval_range (object, &start, &end, soft);
  1663. if (i)
  1664. {
  1665. ptrdiff_t s = XINT (start);
  1666. ptrdiff_t e = XINT (end);
  1667. while (s < e)
  1668. {
  1669. ptrdiff_t interval_end, len;
  1670. Lisp_Object plist;
  1671. interval_end = i->position + LENGTH (i);
  1672. if (interval_end > e)
  1673. interval_end = e;
  1674. len = interval_end - s;
  1675. plist = i->plist;
  1676. if (!NILP (prop))
  1677. for (; CONSP (plist); plist = Fcdr (XCDR (plist)))
  1678. if (EQ (XCAR (plist), prop))
  1679. {
  1680. plist = list2 (prop, Fcar (XCDR (plist)));
  1681. break;
  1682. }
  1683. if (!NILP (plist))
  1684. result = Fcons (list3 (make_number (s), make_number (s + len),
  1685. plist),
  1686. result);
  1687. i = next_interval (i);
  1688. if (!i)
  1689. break;
  1690. s = i->position;
  1691. }
  1692. }
  1693. return result;
  1694. }
  1695. /* Add text properties to OBJECT from LIST. LIST is a list of triples
  1696. (START END PLIST), where START and END are positions and PLIST is a
  1697. property list containing the text properties to add. Adjust START
  1698. and END positions by DELTA before adding properties. */
  1699. void
  1700. add_text_properties_from_list (Lisp_Object object, Lisp_Object list, Lisp_Object delta)
  1701. {
  1702. for (; CONSP (list); list = XCDR (list))
  1703. {
  1704. Lisp_Object item, start, end, plist;
  1705. item = XCAR (list);
  1706. start = make_number (XINT (XCAR (item)) + XINT (delta));
  1707. end = make_number (XINT (XCAR (XCDR (item))) + XINT (delta));
  1708. plist = XCAR (XCDR (XCDR (item)));
  1709. Fadd_text_properties (start, end, plist, object);
  1710. }
  1711. }
  1712. /* Modify end-points of ranges in LIST destructively, and return the
  1713. new list. LIST is a list as returned from text_property_list.
  1714. Discard properties that begin at or after NEW_END, and limit
  1715. end-points to NEW_END. */
  1716. Lisp_Object
  1717. extend_property_ranges (Lisp_Object list, Lisp_Object new_end)
  1718. {
  1719. Lisp_Object prev = Qnil, head = list;
  1720. ptrdiff_t max = XINT (new_end);
  1721. for (; CONSP (list); prev = list, list = XCDR (list))
  1722. {
  1723. Lisp_Object item, beg, end;
  1724. item = XCAR (list);
  1725. beg = XCAR (item);
  1726. end = XCAR (XCDR (item));
  1727. if (XINT (beg) >= max)
  1728. {
  1729. /* The start-point is past the end of the new string.
  1730. Discard this property. */
  1731. if (EQ (head, list))
  1732. head = XCDR (list);
  1733. else
  1734. XSETCDR (prev, XCDR (list));
  1735. }
  1736. else if (XINT (end) > max)
  1737. /* The end-point is past the end of the new string. */
  1738. XSETCAR (XCDR (item), new_end);
  1739. }
  1740. return head;
  1741. }
  1742. /* Call the modification hook functions in LIST, each with START and END. */
  1743. static void
  1744. call_mod_hooks (Lisp_Object list, Lisp_Object start, Lisp_Object end)
  1745. {
  1746. while (!NILP (list))
  1747. {
  1748. call2 (Fcar (list), start, end);
  1749. list = Fcdr (list);
  1750. }
  1751. }
  1752. /* Check for read-only intervals between character positions START ... END,
  1753. in BUF, and signal an error if we find one.
  1754. Then check for any modification hooks in the range.
  1755. Create a list of all these hooks in lexicographic order,
  1756. eliminating consecutive extra copies of the same hook. Then call
  1757. those hooks in order, with START and END - 1 as arguments. */
  1758. void
  1759. verify_interval_modification (struct buffer *buf,
  1760. ptrdiff_t start, ptrdiff_t end)
  1761. {
  1762. INTERVAL intervals = buffer_intervals (buf);
  1763. INTERVAL i;
  1764. Lisp_Object hooks;
  1765. Lisp_Object prev_mod_hooks;
  1766. Lisp_Object mod_hooks;
  1767. hooks = Qnil;
  1768. prev_mod_hooks = Qnil;
  1769. mod_hooks = Qnil;
  1770. interval_insert_behind_hooks = Qnil;
  1771. interval_insert_in_front_hooks = Qnil;
  1772. if (!intervals)
  1773. return;
  1774. if (start > end)
  1775. {
  1776. ptrdiff_t temp = start;
  1777. start = end;
  1778. end = temp;
  1779. }
  1780. /* For an insert operation, check the two chars around the position. */
  1781. if (start == end)
  1782. {
  1783. INTERVAL prev = NULL;
  1784. Lisp_Object before, after;
  1785. /* Set I to the interval containing the char after START,
  1786. and PREV to the interval containing the char before START.
  1787. Either one may be null. They may be equal. */
  1788. i = find_interval (intervals, start);
  1789. if (start == BUF_BEGV (buf))
  1790. prev = 0;
  1791. else if (i->position == start)
  1792. prev = previous_interval (i);
  1793. else if (i->position < start)
  1794. prev = i;
  1795. if (start == BUF_ZV (buf))
  1796. i = 0;
  1797. /* If Vinhibit_read_only is set and is not a list, we can
  1798. skip the read_only checks. */
  1799. if (NILP (Vinhibit_read_only) || CONSP (Vinhibit_read_only))
  1800. {
  1801. /* If I and PREV differ we need to check for the read-only
  1802. property together with its stickiness. If either I or
  1803. PREV are 0, this check is all we need.
  1804. We have to take special care, since read-only may be
  1805. indirectly defined via the category property. */
  1806. if (i != prev)
  1807. {
  1808. if (i)
  1809. {
  1810. after = textget (i->plist, Qread_only);
  1811. /* If interval I is read-only and read-only is
  1812. front-sticky, inhibit insertion.
  1813. Check for read-only as well as category. */
  1814. if (! NILP (after)
  1815. && NILP (Fmemq (after, Vinhibit_read_only)))
  1816. {
  1817. Lisp_Object tem;
  1818. tem = textget (i->plist, Qfront_sticky);
  1819. if (TMEM (Qread_only, tem)
  1820. || (NILP (Fplist_get (i->plist, Qread_only))
  1821. && TMEM (Qcategory, tem)))
  1822. text_read_only (after);
  1823. }
  1824. }
  1825. if (prev)
  1826. {
  1827. before = textget (prev->plist, Qread_only);
  1828. /* If interval PREV is read-only and read-only isn't
  1829. rear-nonsticky, inhibit insertion.
  1830. Check for read-only as well as category. */
  1831. if (! NILP (before)
  1832. && NILP (Fmemq (before, Vinhibit_read_only)))
  1833. {
  1834. Lisp_Object tem;
  1835. tem = textget (prev->plist, Qrear_nonsticky);
  1836. if (! TMEM (Qread_only, tem)
  1837. && (! NILP (Fplist_get (prev->plist,Qread_only))
  1838. || ! TMEM (Qcategory, tem)))
  1839. text_read_only (before);
  1840. }
  1841. }
  1842. }
  1843. else if (i)
  1844. {
  1845. after = textget (i->plist, Qread_only);
  1846. /* If interval I is read-only and read-only is
  1847. front-sticky, inhibit insertion.
  1848. Check for read-only as well as category. */
  1849. if (! NILP (after) && NILP (Fmemq (after, Vinhibit_read_only)))
  1850. {
  1851. Lisp_Object tem;
  1852. tem = textget (i->plist, Qfront_sticky);
  1853. if (TMEM (Qread_only, tem)
  1854. || (NILP (Fplist_get (i->plist, Qread_only))
  1855. && TMEM (Qcategory, tem)))
  1856. text_read_only (after);
  1857. tem = textget (prev->plist, Qrear_nonsticky);
  1858. if (! TMEM (Qread_only, tem)
  1859. && (! NILP (Fplist_get (prev->plist, Qread_only))
  1860. || ! TMEM (Qcategory, tem)))
  1861. text_read_only (after);
  1862. }
  1863. }
  1864. }
  1865. /* Run both insert hooks (just once if they're the same). */
  1866. if (prev)
  1867. interval_insert_behind_hooks
  1868. = textget (prev->plist, Qinsert_behind_hooks);
  1869. if (i)
  1870. interval_insert_in_front_hooks
  1871. = textget (i->plist, Qinsert_in_front_hooks);
  1872. }
  1873. else
  1874. {
  1875. /* Loop over intervals on or next to START...END,
  1876. collecting their hooks. */
  1877. i = find_interval (intervals, start);
  1878. do
  1879. {
  1880. if (! INTERVAL_WRITABLE_P (i))
  1881. text_read_only (textget (i->plist, Qread_only));
  1882. if (!inhibit_modification_hooks)
  1883. {
  1884. mod_hooks = textget (i->plist, Qmodification_hooks);
  1885. if (! NILP (mod_hooks) && ! EQ (mod_hooks, prev_mod_hooks))
  1886. {
  1887. hooks = Fcons (mod_hooks, hooks);
  1888. prev_mod_hooks = mod_hooks;
  1889. }
  1890. }
  1891. if (i->position + LENGTH (i) < end
  1892. && (!NILP (BVAR (current_buffer, read_only))
  1893. && NILP (Vinhibit_read_only)))
  1894. xsignal1 (Qbuffer_read_only, Fcurrent_buffer ());
  1895. i = next_interval (i);
  1896. }
  1897. /* Keep going thru the interval containing the char before END. */
  1898. while (i && i->position < end);
  1899. if (!inhibit_modification_hooks)
  1900. {
  1901. hooks = Fnreverse (hooks);
  1902. while (! EQ (hooks, Qnil))
  1903. {
  1904. call_mod_hooks (Fcar (hooks), make_number (start),
  1905. make_number (end));
  1906. hooks = Fcdr (hooks);
  1907. }
  1908. }
  1909. }
  1910. }
  1911. /* Run the interval hooks for an insertion on character range START ... END.
  1912. verify_interval_modification chose which hooks to run;
  1913. this function is called after the insertion happens
  1914. so it can indicate the range of inserted text. */
  1915. void
  1916. report_interval_modification (Lisp_Object start, Lisp_Object end)
  1917. {
  1918. if (! NILP (interval_insert_behind_hooks))
  1919. call_mod_hooks (interval_insert_behind_hooks, start, end);
  1920. if (! NILP (interval_insert_in_front_hooks)
  1921. && ! EQ (interval_insert_in_front_hooks,
  1922. interval_insert_behind_hooks))
  1923. call_mod_hooks (interval_insert_in_front_hooks, start, end);
  1924. }
  1925. void
  1926. syms_of_textprop (void)
  1927. {
  1928. DEFVAR_LISP ("default-text-properties", Vdefault_text_properties,
  1929. doc: /* Property-list used as default values.
  1930. The value of a property in this list is seen as the value for every
  1931. character that does not have its own value for that property. */);
  1932. Vdefault_text_properties = Qnil;
  1933. DEFVAR_LISP ("char-property-alias-alist", Vchar_property_alias_alist,
  1934. doc: /* Alist of alternative properties for properties without a value.
  1935. Each element should look like (PROPERTY ALTERNATIVE1 ALTERNATIVE2...).
  1936. If a piece of text has no direct value for a particular property, then
  1937. this alist is consulted. If that property appears in the alist, then
  1938. the first non-nil value from the associated alternative properties is
  1939. returned. */);
  1940. Vchar_property_alias_alist = Qnil;
  1941. DEFVAR_LISP ("inhibit-point-motion-hooks", Vinhibit_point_motion_hooks,
  1942. doc: /* If non-nil, don't run `point-left' and `point-entered' text properties.
  1943. This also inhibits the use of the `intangible' text property.
  1944. This variable is obsolete since Emacs-25.1. Use `cursor-intangible-mode'
  1945. or `cursor-sensor-mode' instead. */);
  1946. /* FIXME: We should make-obsolete-variable, but that signals too many
  1947. warnings in code which does (let ((inhibit-point-motion-hooks t)) ...)
  1948. Ideally, make-obsolete-variable should let us specify that only the nil
  1949. value is obsolete, but that requires too many changes in bytecomp.el,
  1950. so for now we'll keep it "obsolete via the docstring". */
  1951. Vinhibit_point_motion_hooks = Qt;
  1952. DEFVAR_LISP ("text-property-default-nonsticky",
  1953. Vtext_property_default_nonsticky,
  1954. doc: /* Alist of properties vs the corresponding non-stickiness.
  1955. Each element has the form (PROPERTY . NONSTICKINESS).
  1956. If a character in a buffer has PROPERTY, new text inserted adjacent to
  1957. the character doesn't inherit PROPERTY if NONSTICKINESS is non-nil,
  1958. inherits it if NONSTICKINESS is nil. The `front-sticky' and
  1959. `rear-nonsticky' properties of the character override NONSTICKINESS. */);
  1960. /* Text properties `syntax-table'and `display' should be nonsticky
  1961. by default. */
  1962. Vtext_property_default_nonsticky
  1963. = list2 (Fcons (Qsyntax_table, Qt), Fcons (Qdisplay, Qt));
  1964. staticpro (&interval_insert_behind_hooks);
  1965. staticpro (&interval_insert_in_front_hooks);
  1966. interval_insert_behind_hooks = Qnil;
  1967. interval_insert_in_front_hooks = Qnil;
  1968. /* Common attributes one might give text. */
  1969. DEFSYM (Qfont, "font");
  1970. DEFSYM (Qface, "face");
  1971. DEFSYM (Qread_only, "read-only");
  1972. DEFSYM (Qinvisible, "invisible");
  1973. DEFSYM (Qintangible, "intangible");
  1974. DEFSYM (Qcategory, "category");
  1975. DEFSYM (Qlocal_map, "local-map");
  1976. DEFSYM (Qfront_sticky, "front-sticky");
  1977. DEFSYM (Qrear_nonsticky, "rear-nonsticky");
  1978. DEFSYM (Qmouse_face, "mouse-face");
  1979. DEFSYM (Qminibuffer_prompt, "minibuffer-prompt");
  1980. /* Properties that text might use to specify certain actions. */
  1981. DEFSYM (Qpoint_left, "point-left");
  1982. DEFSYM (Qpoint_entered, "point-entered");
  1983. defsubr (&Stext_properties_at);
  1984. defsubr (&Sget_text_property);
  1985. defsubr (&Sget_char_property);
  1986. defsubr (&Sget_char_property_and_overlay);
  1987. defsubr (&Snext_char_property_change);
  1988. defsubr (&Sprevious_char_property_change);
  1989. defsubr (&Snext_single_char_property_change);
  1990. defsubr (&Sprevious_single_char_property_change);
  1991. defsubr (&Snext_property_change);
  1992. defsubr (&Snext_single_property_change);
  1993. defsubr (&Sprevious_property_change);
  1994. defsubr (&Sprevious_single_property_change);
  1995. defsubr (&Sadd_text_properties);
  1996. defsubr (&Sput_text_property);
  1997. defsubr (&Sset_text_properties);
  1998. defsubr (&Sadd_face_text_property);
  1999. defsubr (&Sremove_text_properties);
  2000. defsubr (&Sremove_list_of_text_properties);
  2001. defsubr (&Stext_property_any);
  2002. defsubr (&Stext_property_not_all);
  2003. }