PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/src/textprop.c

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