PageRenderTime 52ms CodeModel.GetById 18ms 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

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-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_inte

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