home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / emacs-19.16 / src / textprop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-05  |  31.5 KB  |  1,155 lines

  1. /* Interface code for dealing with text properties.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21. #include "lisp.h"
  22. #include "intervals.h"
  23. #include "buffer.h"
  24.  
  25.  
  26. /* NOTES:  previous- and next- property change will have to skip
  27.   zero-length intervals if they are implemented.  This could be done
  28.   inside next_interval and previous_interval.
  29.  
  30.   set_properties needs to deal with the interval property cache.
  31.  
  32.   It is assumed that for any interval plist, a property appears
  33.   only once on the list.  Although some code i.e., remove_properties,
  34.   handles the more general case, the uniqueness of properties is
  35.   necessary for the system to remain consistent.  This requirement
  36.   is enforced by the subrs installing properties onto the intervals. */
  37.  
  38. /* The rest of the file is within this conditional */
  39. #ifdef USE_TEXT_PROPERTIES
  40.  
  41. /* Types of hooks. */
  42. Lisp_Object Qmouse_left;
  43. Lisp_Object Qmouse_entered;
  44. Lisp_Object Qpoint_left;
  45. Lisp_Object Qpoint_entered;
  46. Lisp_Object Qmodification_hooks;
  47. Lisp_Object Qcategory;
  48. Lisp_Object Qlocal_map;
  49.  
  50. /* Visual properties text (including strings) may have. */
  51. Lisp_Object Qforeground, Qbackground, Qfont, Qunderline, Qstipple;
  52. Lisp_Object Qinvisible, Qread_only;
  53.  
  54. /* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to
  55.    the o1's cdr.  Otherwise, return zero.  This is handy for
  56.    traversing plists.  */
  57. #define PLIST_ELT_P(o1, o2) (CONSP (o1) && CONSP ((o2) = XCONS (o1)->cdr))
  58.  
  59.  
  60. /* Extract the interval at the position pointed to by BEGIN from
  61.    OBJECT, a string or buffer.  Additionally, check that the positions
  62.    pointed to by BEGIN and END are within the bounds of OBJECT, and
  63.    reverse them if *BEGIN is greater than *END.  The objects pointed
  64.    to by BEGIN and END may be integers or markers; if the latter, they
  65.    are coerced to integers.
  66.  
  67.    When OBJECT is a string, we increment *BEGIN and *END
  68.    to make them origin-one.
  69.  
  70.    Note that buffer points don't correspond to interval indices.
  71.    For example, point-max is 1 greater than the index of the last
  72.    character.  This difference is handled in the caller, which uses
  73.    the validated points to determine a length, and operates on that.
  74.    Exceptions are Ftext_properties_at, Fnext_property_change, and
  75.    Fprevious_property_change which call this function with BEGIN == END.
  76.    Handle this case specially.
  77.  
  78.    If FORCE is soft (0), it's OK to return NULL_INTERVAL.  Otherwise,
  79.    create an interval tree for OBJECT if one doesn't exist, provided
  80.    the object actually contains text.  In the current design, if there
  81.    is no text, there can be no text properties.  */
  82.  
  83. #define soft 0
  84. #define hard 1
  85.  
  86. static INTERVAL
  87. validate_interval_range (object, begin, end, force)
  88.      Lisp_Object object, *begin, *end;
  89.      int force;
  90. {
  91.   register INTERVAL i;
  92.   int searchpos;
  93.  
  94.   CHECK_STRING_OR_BUFFER (object, 0);
  95.   CHECK_NUMBER_COERCE_MARKER (*begin, 0);
  96.   CHECK_NUMBER_COERCE_MARKER (*end, 0);
  97.  
  98.   /* If we are asked for a point, but from a subr which operates
  99.      on a range, then return nothing. */
  100.   if (*begin == *end && begin != end)
  101.     return NULL_INTERVAL;
  102.  
  103.   if (XINT (*begin) > XINT (*end))
  104.     {
  105.       Lisp_Object n;
  106.       n = *begin;
  107.       *begin = *end;
  108.       *end = n;
  109.     }
  110.  
  111.   if (XTYPE (object) == Lisp_Buffer)
  112.     {
  113.       register struct buffer *b = XBUFFER (object);
  114.  
  115.       if (!(BUF_BEGV (b) <= XINT (*begin) && XINT (*begin) <= XINT (*end)
  116.         && XINT (*end) <= BUF_ZV (b)))
  117.     args_out_of_range (*begin, *end);
  118.       i = b->intervals;
  119.  
  120.       /* If there's no text, there are no properties. */
  121.       if (BUF_BEGV (b) == BUF_ZV (b))
  122.     return NULL_INTERVAL;
  123.  
  124.       searchpos = XINT (*begin);
  125.     }
  126.   else
  127.     {
  128.       register struct Lisp_String *s = XSTRING (object);
  129.  
  130.       if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end)
  131.          && XINT (*end) <= s->size))
  132.     args_out_of_range (*begin, *end);
  133.       /* User-level Positions in strings start with 0,
  134.      but the interval code always wants positions starting with 1.  */
  135.       XFASTINT (*begin) += 1;
  136.       if (begin != end)
  137.     XFASTINT (*end) += 1;
  138.       i = s->intervals;
  139.  
  140.       if (s->size == 0)
  141.     return NULL_INTERVAL;
  142.  
  143.       searchpos = XINT (*begin);
  144.     }
  145.  
  146.   if (NULL_INTERVAL_P (i))
  147.     return (force ? create_root_interval (object) : i);
  148.     
  149.   return find_interval (i, searchpos);
  150. }
  151.  
  152. /* Validate LIST as a property list.  If LIST is not a list, then
  153.    make one consisting of (LIST nil).  Otherwise, verify that LIST
  154.    is even numbered and thus suitable as a plist. */
  155.  
  156. static Lisp_Object
  157. validate_plist (list)
  158. {
  159.   if (NILP (list))
  160.     return Qnil;
  161.  
  162.   if (CONSP (list))
  163.     {
  164.       register int i;
  165.       register Lisp_Object tail;
  166.       for (i = 0, tail = list; !NILP (tail); i++)
  167.     {
  168.       tail = Fcdr (tail);
  169.       QUIT;
  170.     }
  171.       if (i & 1)
  172.     error ("Odd length text property list");
  173.       return list;
  174.     }
  175.  
  176.   return Fcons (list, Fcons (Qnil, Qnil));
  177. }
  178.  
  179. /* Return nonzero if interval I has all the properties,
  180.    with the same values, of list PLIST. */
  181.  
  182. static int
  183. interval_has_all_properties (plist, i)
  184.      Lisp_Object plist;
  185.      INTERVAL i;
  186. {
  187.   register Lisp_Object tail1, tail2, sym1, sym2;
  188.   register int found;
  189.  
  190.   /* Go through each element of PLIST. */
  191.   for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
  192.     {
  193.       sym1 = Fcar (tail1);
  194.       found = 0;
  195.  
  196.       /* Go through I's plist, looking for sym1 */
  197.       for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
  198.     if (EQ (sym1, Fcar (tail2)))
  199.       {
  200.         /* Found the same property on both lists.  If the
  201.            values are unequal, return zero. */
  202.         if (! EQ (Fcar (Fcdr (tail1)), Fcar (Fcdr (tail2))))
  203.           return 0;
  204.  
  205.         /* Property has same value on both lists;  go to next one. */
  206.         found = 1;
  207.         break;
  208.       }
  209.  
  210.       if (! found)
  211.     return 0;
  212.     }
  213.  
  214.   return 1;
  215. }
  216.  
  217. /* Return nonzero if the plist of interval I has any of the
  218.    properties of PLIST, regardless of their values. */
  219.  
  220. static INLINE int
  221. interval_has_some_properties (plist, i)
  222.      Lisp_Object plist;
  223.      INTERVAL i;
  224. {
  225.   register Lisp_Object tail1, tail2, sym;
  226.  
  227.   /* Go through each element of PLIST. */
  228.   for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
  229.     {
  230.       sym = Fcar (tail1);
  231.  
  232.       /* Go through i's plist, looking for tail1 */
  233.       for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
  234.     if (EQ (sym, Fcar (tail2)))
  235.       return 1;
  236.     }
  237.  
  238.   return 0;
  239. }
  240.  
  241. /* Changing the plists of individual intervals.  */
  242.  
  243. /* Return the value of PROP in property-list PLIST, or Qunbound if it
  244.    has none.  */
  245. static int
  246. property_value (plist, prop)
  247. {
  248.   Lisp_Object value;
  249.  
  250.   while (PLIST_ELT_P (plist, value))
  251.     if (EQ (XCONS (plist)->car, prop))
  252.       return XCONS (value)->car;
  253.     else
  254.       plist = XCONS (value)->cdr;
  255.  
  256.   return Qunbound;
  257. }
  258.  
  259. /* Set the properties of INTERVAL to PROPERTIES,
  260.    and record undo info for the previous values.
  261.    OBJECT is the string or buffer that INTERVAL belongs to.  */
  262.  
  263. static void
  264. set_properties (properties, interval, object)
  265.      Lisp_Object properties, object;
  266.      INTERVAL interval;
  267. {
  268.   Lisp_Object sym, value;
  269.  
  270.   if (BUFFERP (object))
  271.     {
  272.       /* For each property in the old plist which is missing from PROPERTIES,
  273.      or has a different value in PROPERTIES, make an undo record.  */
  274.       for (sym = interval->plist;
  275.        PLIST_ELT_P (sym, value);
  276.        sym = XCONS (value)->cdr)
  277.     if (! EQ (property_value (properties, XCONS (sym)->car),
  278.           XCONS (value)->car))
  279.       record_property_change (interval->position, LENGTH (interval),
  280.                   XCONS (sym)->car, XCONS (value)->car,
  281.                   object);
  282.  
  283.       /* For each new property that has no value at all in the old plist,
  284.      make an undo record binding it to nil, so it will be removed.  */
  285.       for (sym = properties;
  286.        PLIST_ELT_P (sym, value);
  287.        sym = XCONS (value)->cdr)
  288.     if (EQ (property_value (interval->plist, XCONS (sym)->car), Qunbound))
  289.       record_property_change (interval->position, LENGTH (interval),
  290.                   XCONS (sym)->car, Qnil,
  291.                   object);
  292.     }
  293.  
  294.   /* Store new properties.  */
  295.   interval->plist = Fcopy_sequence (properties);
  296. }
  297.  
  298. /* Add the properties of PLIST to the interval I, or set
  299.    the value of I's property to the value of the property on PLIST
  300.    if they are different.
  301.  
  302.    OBJECT should be the string or buffer the interval is in.
  303.  
  304.    Return nonzero if this changes I (i.e., if any members of PLIST
  305.    are actually added to I's plist) */
  306.  
  307. static int
  308. add_properties (plist, i, object)
  309.      Lisp_Object plist;
  310.      INTERVAL i;
  311.      Lisp_Object object;
  312. {
  313.   register Lisp_Object tail1, tail2, sym1, val1;
  314.   register int changed = 0;
  315.   register int found;
  316.  
  317.   /* Go through each element of PLIST. */
  318.   for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
  319.     {
  320.       sym1 = Fcar (tail1);
  321.       val1 = Fcar (Fcdr (tail1));
  322.       found = 0;
  323.  
  324.       /* Go through I's plist, looking for sym1 */
  325.       for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
  326.     if (EQ (sym1, Fcar (tail2)))
  327.       {
  328.         register Lisp_Object this_cdr = Fcdr (tail2);
  329.  
  330.         /* Found the property.  Now check its value. */
  331.         found = 1;
  332.  
  333.         /* The properties have the same value on both lists.
  334.            Continue to the next property. */
  335.         if (EQ (val1, Fcar (this_cdr)))
  336.           break;
  337.  
  338.         /* Record this change in the buffer, for undo purposes.  */
  339.         if (XTYPE (object) == Lisp_Buffer)
  340.           {
  341.         record_property_change (i->position, LENGTH (i),
  342.                     sym1, Fcar (this_cdr), object);
  343.         modify_region (XBUFFER (object),
  344.                    make_number (i->position),
  345.                    make_number (i->position + LENGTH (i)));
  346.           }
  347.  
  348.         /* I's property has a different value -- change it */
  349.         Fsetcar (this_cdr, val1);
  350.         changed++;
  351.         break;
  352.       }
  353.  
  354.       if (! found)
  355.     {
  356.       /* Record this change in the buffer, for undo purposes.  */
  357.       if (XTYPE (object) == Lisp_Buffer)
  358.         {
  359.           record_property_change (i->position, LENGTH (i),
  360.                       sym1, Qnil, object);
  361.           modify_region (XBUFFER (object),
  362.                  make_number (i->position),
  363.                  make_number (i->position + LENGTH (i)));
  364.         }
  365.       i->plist = Fcons (sym1, Fcons (val1, i->plist));
  366.       changed++;
  367.     }
  368.     }
  369.  
  370.   return changed;
  371. }
  372.  
  373. /* For any members of PLIST which are properties of I, remove them
  374.    from I's plist.
  375.    OBJECT is the string or buffer containing I.  */
  376.  
  377. static int
  378. remove_properties (plist, i, object)
  379.      Lisp_Object plist;
  380.      INTERVAL i;
  381.      Lisp_Object object;
  382. {
  383.   register Lisp_Object tail1, tail2, sym;
  384.   register Lisp_Object current_plist = i->plist;
  385.   register int changed = 0;
  386.  
  387.   /* Go through each element of plist. */
  388.   for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
  389.     {
  390.       sym = Fcar (tail1);
  391.  
  392.       /* First, remove the symbol if its at the head of the list */
  393.       while (! NILP (current_plist) && EQ (sym, Fcar (current_plist)))
  394.     {
  395.       if (XTYPE (object) == Lisp_Buffer)
  396.         {
  397.           record_property_change (i->position, LENGTH (i),
  398.                       sym, Fcar (Fcdr (current_plist)),
  399.                       object);
  400.           modify_region (XBUFFER (object),
  401.                  make_number (i->position),
  402.                  make_number (i->position + LENGTH (i)));
  403.         }
  404.  
  405.       current_plist = Fcdr (Fcdr (current_plist));
  406.       changed++;
  407.     }
  408.  
  409.       /* Go through i's plist, looking for sym */
  410.       tail2 = current_plist;
  411.       while (! NILP (tail2))
  412.     {
  413.       register Lisp_Object this = Fcdr (Fcdr (tail2));
  414.       if (EQ (sym, Fcar (this)))
  415.         {
  416.           if (XTYPE (object) == Lisp_Buffer)
  417.         {
  418.           record_property_change (i->position, LENGTH (i),
  419.                       sym, Fcar (Fcdr (this)), object);
  420.           modify_region (XBUFFER (object),
  421.                  make_number (i->position),
  422.                  make_number (i->position + LENGTH (i)));
  423.         }
  424.  
  425.           Fsetcdr (Fcdr (tail2), Fcdr (Fcdr (this)));
  426.           changed++;
  427.         }
  428.       tail2 = this;
  429.     }
  430.     }
  431.  
  432.   if (changed)
  433.     i->plist = current_plist;
  434.   return changed;
  435. }
  436.  
  437. #if 0
  438. /* Remove all properties from interval I.  Return non-zero
  439.    if this changes the interval. */
  440.  
  441. static INLINE int
  442. erase_properties (i)
  443.      INTERVAL i;
  444. {
  445.   if (NILP (i->plist))
  446.     return 0;
  447.  
  448.   i->plist = Qnil;
  449.   return 1;
  450. }
  451. #endif
  452.  
  453. DEFUN ("text-properties-at", Ftext_properties_at,
  454.        Stext_properties_at, 1, 2, 0,
  455.   "Return the list of properties held by the character at POSITION\n\
  456. in optional argument OBJECT, a string or buffer.  If nil, OBJECT\n\
  457. defaults to the current buffer.\n\
  458. If POSITION is at the end of OBJECT, the value is nil.")
  459.   (pos, object)
  460.      Lisp_Object pos, object;
  461. {
  462.   register INTERVAL i;
  463.  
  464.   if (NILP (object))
  465.     XSET (object, Lisp_Buffer, current_buffer);
  466.  
  467.   i = validate_interval_range (object, &pos, &pos, soft);
  468.   if (NULL_INTERVAL_P (i))
  469.     return Qnil;
  470.   /* If POS is at the end of the interval,
  471.      it means it's the end of OBJECT.
  472.      There are no properties at the very end,
  473.      since no character follows.  */
  474.   if (XINT (pos) == LENGTH (i) + i->position)
  475.     return Qnil;
  476.  
  477.   return i->plist;
  478. }
  479.  
  480. DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
  481.   "Return the value of position POS's property PROP, in OBJECT.\n\
  482. OBJECT is optional and defaults to the current buffer.\n\
  483. If POSITION is at the end of OBJECT, the value is nil.")
  484.   (pos, prop, object)
  485.      Lisp_Object pos, object;
  486.      register Lisp_Object prop;
  487. {
  488.   register INTERVAL i;
  489.   register Lisp_Object tail;
  490.  
  491.   if (NILP (object))
  492.     XSET (object, Lisp_Buffer, current_buffer);
  493.   i = validate_interval_range (object, &pos, &pos, soft);
  494.   if (NULL_INTERVAL_P (i))
  495.     return Qnil;
  496.  
  497.   /* If POS is at the end of the interval,
  498.      it means it's the end of OBJECT.
  499.      There are no properties at the very end,
  500.      since no character follows.  */
  501.   if (XINT (pos) == LENGTH (i) + i->position)
  502.     return Qnil;
  503.  
  504.   return textget (i->plist, prop);
  505. }
  506.  
  507. DEFUN ("next-property-change", Fnext_property_change,
  508.        Snext_property_change, 1, 2, 0,
  509.   "Return the position of next property change.\n\
  510. Scans characters forward from POS in OBJECT till it finds\n\
  511. a change in some text property, then returns the position of the change.\n\
  512. The optional second argument OBJECT is the string or buffer to scan.\n\
  513. Return nil if the property is constant all the way to the end of OBJECT.\n\
  514. If the value is non-nil, it is a position greater than POS, never equal.")
  515.   (pos, object)
  516.      Lisp_Object pos, object;
  517. {
  518.   register INTERVAL i, next;
  519.  
  520.   if (NILP (object))
  521.     XSET (object, Lisp_Buffer, current_buffer);
  522.  
  523.   i = validate_interval_range (object, &pos, &pos, soft);
  524.   if (NULL_INTERVAL_P (i))
  525.     return Qnil;
  526.  
  527.   next = next_interval (i);
  528.   while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
  529.     next = next_interval (next);
  530.  
  531.   if (NULL_INTERVAL_P (next))
  532.     return Qnil;
  533.  
  534.   return next->position - (XTYPE (object) == Lisp_String);
  535. ;
  536. }
  537.  
  538. DEFUN ("next-single-property-change", Fnext_single_property_change,
  539.        Snext_single_property_change, 1, 3, 0,
  540.   "Return the position of next property change for a specific property.\n\
  541. Scans characters forward from POS till it finds\n\
  542. a change in the PROP property, then returns the position of the change.\n\
  543. The optional third argument OBJECT is the string or buffer to scan.\n\
  544. Return nil if the property is constant all the way to the end of OBJECT.\n\
  545. If the value is non-nil, it is a position greater than POS, never equal.")
  546.   (pos, prop, object)
  547.      Lisp_Object pos, prop, object;
  548. {
  549.   register INTERVAL i, next;
  550.   register Lisp_Object here_val;
  551.  
  552.   if (NILP (object))
  553.     XSET (object, Lisp_Buffer, current_buffer);
  554.  
  555.   i = validate_interval_range (object, &pos, &pos, soft);
  556.   if (NULL_INTERVAL_P (i))
  557.     return Qnil;
  558.  
  559.   here_val = textget (i->plist, prop);
  560.   next = next_interval (i);
  561.   while (! NULL_INTERVAL_P (next) 
  562.      && EQ (here_val, textget (next->plist, prop)))
  563.     next = next_interval (next);
  564.  
  565.   if (NULL_INTERVAL_P (next))
  566.     return Qnil;
  567.  
  568.   return next->position - (XTYPE (object) == Lisp_String);
  569. }
  570.  
  571. DEFUN ("previous-property-change", Fprevious_property_change,
  572.        Sprevious_property_change, 1, 2, 0,
  573.   "Return the position of previous property change.\n\
  574. Scans characters backwards from POS in OBJECT till it finds\n\
  575. a change in some text property, then returns the position of the change.\n\
  576. The optional second argument OBJECT is the string or buffer to scan.\n\
  577. Return nil if the property is constant all the way to the start of OBJECT.\n\
  578. If the value is non-nil, it is a position less than POS, never equal.")
  579.   (pos, object)
  580.      Lisp_Object pos, object;
  581. {
  582.   register INTERVAL i, previous;
  583.  
  584.   if (NILP (object))
  585.     XSET (object, Lisp_Buffer, current_buffer);
  586.  
  587.   i = validate_interval_range (object, &pos, &pos, soft);
  588.   if (NULL_INTERVAL_P (i))
  589.     return Qnil;
  590.  
  591.   previous = previous_interval (i);
  592.   while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i))
  593.     previous = previous_interval (previous);
  594.   if (NULL_INTERVAL_P (previous))
  595.     return Qnil;
  596.  
  597.   return (previous->position + LENGTH (previous) - 1
  598.       - (XTYPE (object) == Lisp_String));
  599. }
  600.  
  601. DEFUN ("previous-single-property-change", Fprevious_single_property_change,
  602.        Sprevious_single_property_change, 2, 3, 0,
  603.   "Return the position of previous property change for a specific property.\n\
  604. Scans characters backward from POS till it finds\n\
  605. a change in the PROP property, then returns the position of the change.\n\
  606. The optional third argument OBJECT is the string or buffer to scan.\n\
  607. Return nil if the property is constant all the way to the start of OBJECT.\n\
  608. If the value is non-nil, it is a position less than POS, never equal.")
  609.      (pos, prop, object)
  610.      Lisp_Object pos, prop, object;
  611. {
  612.   register INTERVAL i, previous;
  613.   register Lisp_Object here_val;
  614.  
  615.   if (NILP (object))
  616.     XSET (object, Lisp_Buffer, current_buffer);
  617.  
  618.   i = validate_interval_range (object, &pos, &pos, soft);
  619.   if (NULL_INTERVAL_P (i))
  620.     return Qnil;
  621.  
  622.   here_val = textget (i->plist, prop);
  623.   previous = previous_interval (i);
  624.   while (! NULL_INTERVAL_P (previous)
  625.      && EQ (here_val, textget (previous->plist, prop)))
  626.     previous = previous_interval (previous);
  627.   if (NULL_INTERVAL_P (previous))
  628.     return Qnil;
  629.  
  630.   return (previous->position + LENGTH (previous) - 1
  631.       - (XTYPE (object) == Lisp_String));
  632. }
  633.  
  634. DEFUN ("add-text-properties", Fadd_text_properties,
  635.        Sadd_text_properties, 3, 4, 0,
  636.   "Add properties to the text from START to END.\n\
  637. The third argument PROPS is a property list\n\
  638. specifying the property values to add.\n\
  639. The optional fourth argument, OBJECT,\n\
  640. is the string or buffer containing the text.\n\
  641. Return t if any property value actually changed, nil otherwise.")
  642.   (start, end, properties, object)
  643.      Lisp_Object start, end, properties, object;
  644. {
  645.   register INTERVAL i, unchanged;
  646.   register int s, len, modified = 0;
  647.  
  648.   properties = validate_plist (properties);
  649.   if (NILP (properties))
  650.     return Qnil;
  651.  
  652.   if (NILP (object))
  653.     XSET (object, Lisp_Buffer, current_buffer);
  654.  
  655.   i = validate_interval_range (object, &start, &end, hard);
  656.   if (NULL_INTERVAL_P (i))
  657.     return Qnil;
  658.  
  659.   s = XINT (start);
  660.   len = XINT (end) - s;
  661.  
  662.   /* If we're not starting on an interval boundary, we have to
  663.     split this interval. */
  664.   if (i->position != s)
  665.     {
  666.       /* If this interval already has the properties, we can
  667.          skip it. */
  668.       if (interval_has_all_properties (properties, i))
  669.     {
  670.       int got = (LENGTH (i) - (s - i->position));
  671.       if (got >= len)
  672.         return Qnil;
  673.       len -= got;
  674.       i = next_interval (i);
  675.     }
  676.       else
  677.     {
  678.       unchanged = i;
  679.       i = split_interval_right (unchanged, s - unchanged->position + 1);
  680.       copy_properties (unchanged, i);
  681.     }
  682.     }
  683.  
  684.   /* We are at the beginning of interval I, with LEN chars to scan.  */
  685.   for (;;)
  686.     {
  687.       if (i == 0)
  688.     abort ();
  689.  
  690.       if (LENGTH (i) >= len)
  691.     {
  692.       if (interval_has_all_properties (properties, i))
  693.         return modified ? Qt : Qnil;
  694.  
  695.       if (LENGTH (i) == len)
  696.         {
  697.           add_properties (properties, i, object);
  698.           return Qt;
  699.         }
  700.  
  701.       /* i doesn't have the properties, and goes past the change limit */
  702.       unchanged = i;
  703.       i = split_interval_left (unchanged, len + 1);
  704.       copy_properties (unchanged, i);
  705.       add_properties (properties, i, object);
  706.       return Qt;
  707.     }
  708.  
  709.       len -= LENGTH (i);
  710.       modified += add_properties (properties, i, object);
  711.       i = next_interval (i);
  712.     }
  713. }
  714.  
  715. DEFUN ("put-text-property", Fput_text_property,
  716.        Sput_text_property, 4, 5, 0,
  717.   "Set one property of the text from START to END.\n\
  718. The third and fourth arguments PROP and VALUE\n\
  719. specify the property to add.\n\
  720. The optional fifth argument, OBJECT,\n\
  721. is the string or buffer containing the text.")
  722.   (start, end, prop, value, object)
  723.      Lisp_Object start, end, prop, value, object;
  724. {
  725.   Fadd_text_properties (start, end,
  726.             Fcons (prop, Fcons (value, Qnil)),
  727.             object);
  728.   return Qnil;
  729. }
  730.  
  731. DEFUN ("set-text-properties", Fset_text_properties,
  732.        Sset_text_properties, 3, 4, 0,
  733.   "Completely replace properties of text from START to END.\n\
  734. The third argument PROPS is the new property list.\n\
  735. The optional fourth argument, OBJECT,\n\
  736. is the string or buffer containing the text.")
  737.   (start, end, props, object)
  738.      Lisp_Object start, end, props, object;
  739. {
  740.   register INTERVAL i, unchanged;
  741.   register INTERVAL prev_changed = NULL_INTERVAL;
  742.   register int s, len;
  743.  
  744.   props = validate_plist (props);
  745.  
  746.   if (NILP (object))
  747.     XSET (object, Lisp_Buffer, current_buffer);
  748.  
  749.   i = validate_interval_range (object, &start, &end, hard);
  750.   if (NULL_INTERVAL_P (i))
  751.     return Qnil;
  752.  
  753.   s = XINT (start);
  754.   len = XINT (end) - s;
  755.  
  756.   if (i->position != s)
  757.     {
  758.       unchanged = i;
  759.       i = split_interval_right (unchanged, s - unchanged->position + 1);
  760.  
  761.       if (LENGTH (i) > len)
  762.     {
  763.       copy_properties (unchanged, i);
  764.       i = split_interval_left (i, len + 1);
  765.       set_properties (props, i, object);
  766.       return Qt;
  767.     }
  768.  
  769.       set_properties (props, i, object);
  770.  
  771.       if (LENGTH (i) == len)
  772.     return Qt;
  773.  
  774.       prev_changed = i;
  775.       len -= LENGTH (i);
  776.       i = next_interval (i);
  777.     }
  778.  
  779.   /* We are starting at the beginning of an interval, I */
  780.   while (len > 0)
  781.     {
  782.       if (i == 0)
  783.     abort ();
  784.  
  785.       if (LENGTH (i) >= len)
  786.     {
  787.       if (LENGTH (i) > len)
  788.         i = split_interval_left (i, len + 1);
  789.  
  790.       if (NULL_INTERVAL_P (prev_changed))
  791.         set_properties (props, i, object);
  792.       else
  793.         merge_interval_left (i);
  794.       return Qt;
  795.     }
  796.  
  797.       len -= LENGTH (i);
  798.       if (NULL_INTERVAL_P (prev_changed))
  799.     {
  800.       set_properties (props, i, object);
  801.       prev_changed = i;
  802.     }
  803.       else
  804.     prev_changed = i = merge_interval_left (i);
  805.  
  806.       i = next_interval (i);
  807.     }
  808.  
  809.   return Qt;
  810. }
  811.  
  812. DEFUN ("remove-text-properties", Fremove_text_properties,
  813.        Sremove_text_properties, 3, 4, 0,
  814.   "Remove some properties from text from START to END.\n\
  815. The third argument PROPS is a property list\n\
  816. whose property names specify the properties to remove.\n\
  817. \(The values stored in PROPS are ignored.)\n\
  818. The optional fourth argument, OBJECT,\n\
  819. is the string or buffer containing the text.\n\
  820. Return t if any property was actually removed, nil otherwise.")
  821.   (start, end, props, object)
  822.      Lisp_Object start, end, props, object;
  823. {
  824.   register INTERVAL i, unchanged;
  825.   register int s, len, modified = 0;
  826.  
  827.   if (NILP (object))
  828.     XSET (object, Lisp_Buffer, current_buffer);
  829.  
  830.   i = validate_interval_range (object, &start, &end, soft);
  831.   if (NULL_INTERVAL_P (i))
  832.     return Qnil;
  833.  
  834.   s = XINT (start);
  835.   len = XINT (end) - s;
  836.  
  837.   if (i->position != s)
  838.     {
  839.       /* No properties on this first interval -- return if
  840.          it covers the entire region. */
  841.       if (! interval_has_some_properties (props, i))
  842.     {
  843.       int got = (LENGTH (i) - (s - i->position));
  844.       if (got >= len)
  845.         return Qnil;
  846.       len -= got;
  847.       i = next_interval (i);
  848.     }
  849.       /* Split away the beginning of this interval; what we don't
  850.      want to modify.  */
  851.       else
  852.     {
  853.       unchanged = i;
  854.       i = split_interval_right (unchanged, s - unchanged->position + 1);
  855.       copy_properties (unchanged, i);
  856.     }
  857.     }
  858.  
  859.   /* We are at the beginning of an interval, with len to scan */
  860.   for (;;)
  861.     {
  862.       if (i == 0)
  863.     abort ();
  864.  
  865.       if (LENGTH (i) >= len)
  866.     {
  867.       if (! interval_has_some_properties (props, i))
  868.         return modified ? Qt : Qnil;
  869.  
  870.       if (LENGTH (i) == len)
  871.         {
  872.           remove_properties (props, i, object);
  873.           return Qt;
  874.         }
  875.  
  876.       /* i has the properties, and goes past the change limit */
  877.       unchanged = i;
  878.       i = split_interval_left (i, len + 1);
  879.       copy_properties (unchanged, i);
  880.       remove_properties (props, i, object);
  881.       return Qt;
  882.     }
  883.  
  884.       len -= LENGTH (i);
  885.       modified += remove_properties (props, i, object);
  886.       i = next_interval (i);
  887.     }
  888. }
  889.  
  890. #if 0 /* You can use set-text-properties for this.  */
  891.  
  892. DEFUN ("erase-text-properties", Ferase_text_properties,
  893.        Serase_text_properties, 2, 3, 0,
  894.   "Remove all properties from the text from START to END.\n\
  895. The optional third argument, OBJECT,\n\
  896. is the string or buffer containing the text.")
  897.   (start, end, object)
  898.      Lisp_Object start, end, object;
  899. {
  900.   register INTERVAL i;
  901.   register INTERVAL prev_changed = NULL_INTERVAL;
  902.   register int s, len, modified;
  903.  
  904.   if (NILP (object))
  905.     XSET (object, Lisp_Buffer, current_buffer);
  906.  
  907.   i = validate_interval_range (object, &start, &end, soft);
  908.   if (NULL_INTERVAL_P (i))
  909.     return Qnil;
  910.  
  911.   s = XINT (start);
  912.   len = XINT (end) - s;
  913.  
  914.   if (i->position != s)
  915.     {
  916.       register int got;
  917.       register INTERVAL unchanged = i;
  918.  
  919.       /* If there are properties here, then this text will be modified. */
  920.       if (! NILP (i->plist))
  921.     {
  922.       i = split_interval_right (unchanged, s - unchanged->position + 1);
  923.       i->plist = Qnil;
  924.       modified++;
  925.  
  926.       if (LENGTH (i) > len)
  927.         {
  928.           i = split_interval_right (i, len + 1);
  929.           copy_properties (unchanged, i);
  930.           return Qt;
  931.         }
  932.  
  933.       if (LENGTH (i) == len)
  934.         return Qt;
  935.  
  936.       got = LENGTH (i);
  937.     }
  938.       /* If the text of I is without any properties, and contains
  939.          LEN or more characters, then we may return without changing
  940.      anything.*/
  941.       else if (LENGTH (i) - (s - i->position) <= len)
  942.     return Qnil;
  943.       /* The amount of text to change extends past I, so just note
  944.      how much we've gotten. */
  945.       else
  946.     got = LENGTH (i) - (s - i->position);
  947.  
  948.       len -= got;
  949.       prev_changed = i;
  950.       i = next_interval (i);
  951.     }
  952.  
  953.   /* We are starting at the beginning of an interval, I. */
  954.   while (len > 0)
  955.     {
  956.       if (LENGTH (i) >= len)
  957.     {
  958.       /* If I has no properties, simply merge it if possible.  */
  959.       if (NILP (i->plist))
  960.         {
  961.           if (! NULL_INTERVAL_P (prev_changed))
  962.         merge_interval_left (i);
  963.  
  964.           return modified ? Qt : Qnil;
  965.         }
  966.  
  967.           if (LENGTH (i) > len)
  968.             i = split_interval_left (i, len + 1);
  969.       if (! NULL_INTERVAL_P (prev_changed))
  970.         merge_interval_left (i);
  971.       else
  972.         i->plist = Qnil;
  973.  
  974.       return Qt;
  975.     }
  976.  
  977.       /* Here if we still need to erase past the end of I */
  978.       len -= LENGTH (i);
  979.       if (NULL_INTERVAL_P (prev_changed))
  980.     {
  981.       modified += erase_properties (i);
  982.       prev_changed = i;
  983.     }
  984.       else
  985.     {
  986.       modified += ! NILP (i->plist);
  987.       /* Merging I will give it the properties of PREV_CHANGED. */
  988.       prev_changed = i = merge_interval_left (i);
  989.     }
  990.  
  991.       i = next_interval (i);
  992.     }
  993.  
  994.   return modified ? Qt : Qnil;
  995. }
  996. #endif /* 0 */
  997.  
  998. /* I don't think this is the right interface to export; how often do you
  999.    want to do something like this, other than when you're copying objects
  1000.    around?
  1001.  
  1002.    I think it would be better to have a pair of functions, one which
  1003.    returns the text properties of a region as a list of ranges and
  1004.    plists, and another which applies such a list to another object.  */
  1005.  
  1006. /* DEFUN ("copy-text-properties", Fcopy_text_properties,
  1007.        Scopy_text_properties, 5, 6, 0,
  1008.   "Add properties from SRC-START to SRC-END of SRC at DEST-POS of DEST.\n\
  1009. SRC and DEST may each refer to strings or buffers.\n\
  1010. Optional sixth argument PROP causes only that property to be copied.\n\
  1011. Properties are copied to DEST as if by `add-text-properties'.\n\
  1012. Return t if any property value actually changed, nil otherwise.") */
  1013.  
  1014. Lisp_Object
  1015. copy_text_properties (start, end, src, pos, dest, prop)
  1016.        Lisp_Object start, end, src, pos, dest, prop;
  1017. {
  1018.   INTERVAL i;
  1019.   Lisp_Object res;
  1020.   Lisp_Object stuff;
  1021.   Lisp_Object plist;
  1022.   int s, e, e2, p, len, modified = 0;
  1023.  
  1024.   i = validate_interval_range (src, &start, &end, soft);
  1025.   if (NULL_INTERVAL_P (i))
  1026.     return Qnil;
  1027.  
  1028.   CHECK_NUMBER_COERCE_MARKER (pos, 0);
  1029.   {
  1030.     Lisp_Object dest_start, dest_end;
  1031.  
  1032.     dest_start = pos;
  1033.     XFASTINT (dest_end) = XINT (dest_start) + (XINT (end) - XINT (start));
  1034.     /* Apply this to a copy of pos; it will try to increment its arguments,
  1035.        which we don't want.  */
  1036.     validate_interval_range (dest, &dest_start, &dest_end, soft);
  1037.   }
  1038.  
  1039.   s = XINT (start);
  1040.   e = XINT (end);
  1041.   p = XINT (pos);
  1042.  
  1043.   stuff = Qnil;
  1044.  
  1045.   while (s < e)
  1046.     {
  1047.       e2 = i->position + LENGTH (i);
  1048.       if (e2 > e)
  1049.     e2 = e;
  1050.       len = e2 - s;
  1051.  
  1052.       plist = i->plist;
  1053.       if (! NILP (prop))
  1054.     while (! NILP (plist))
  1055.       {
  1056.         if (EQ (Fcar (plist), prop))
  1057.           {
  1058.         plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
  1059.         break;
  1060.           }
  1061.         plist = Fcdr (Fcdr (plist));
  1062.       }
  1063.       if (! NILP (plist))
  1064.     {
  1065.       /* Must defer modifications to the interval tree in case src
  1066.          and dest refer to the same string or buffer. */
  1067.       stuff = Fcons (Fcons (make_number (p),
  1068.                 Fcons (make_number (p + len),
  1069.                        Fcons (plist, Qnil))),
  1070.             stuff);
  1071.     }
  1072.  
  1073.       i = next_interval (i);
  1074.       if (NULL_INTERVAL_P (i))
  1075.     break;
  1076.  
  1077.       p += len;
  1078.       s = i->position;
  1079.     }
  1080.  
  1081.   while (! NILP (stuff))
  1082.     {
  1083.       res = Fcar (stuff);
  1084.       res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)),
  1085.                   Fcar (Fcdr (Fcdr (res))), dest);
  1086.       if (! NILP (res))
  1087.     modified++;
  1088.       stuff = Fcdr (stuff);
  1089.     }
  1090.  
  1091.   return modified ? Qt : Qnil;
  1092. }
  1093.  
  1094. void
  1095. syms_of_textprop ()
  1096. {
  1097.   DEFVAR_INT ("interval-balance-threshold", &interval_balance_threshold,
  1098.           "Threshold for rebalancing interval trees, expressed as the\n\
  1099. percentage by which the left interval tree should not differ from the right.");
  1100.   interval_balance_threshold = 8;
  1101.  
  1102.   /* Common attributes one might give text */
  1103.  
  1104.   staticpro (&Qforeground);
  1105.   Qforeground = intern ("foreground");
  1106.   staticpro (&Qbackground);
  1107.   Qbackground = intern ("background");
  1108.   staticpro (&Qfont);
  1109.   Qfont = intern ("font");
  1110.   staticpro (&Qstipple);
  1111.   Qstipple = intern ("stipple");
  1112.   staticpro (&Qunderline);
  1113.   Qunderline = intern ("underline");
  1114.   staticpro (&Qread_only);
  1115.   Qread_only = intern ("read-only");
  1116.   staticpro (&Qinvisible);
  1117.   Qinvisible = intern ("invisible");
  1118.   staticpro (&Qcategory);
  1119.   Qcategory = intern ("category");
  1120.   staticpro (&Qlocal_map);
  1121.   Qlocal_map = intern ("local-map");
  1122.  
  1123.   /* Properties that text might use to specify certain actions */
  1124.  
  1125.   staticpro (&Qmouse_left);
  1126.   Qmouse_left = intern ("mouse-left");
  1127.   staticpro (&Qmouse_entered);
  1128.   Qmouse_entered = intern ("mouse-entered");
  1129.   staticpro (&Qpoint_left);
  1130.   Qpoint_left = intern ("point-left");
  1131.   staticpro (&Qpoint_entered);
  1132.   Qpoint_entered = intern ("point-entered");
  1133.   staticpro (&Qmodification_hooks);
  1134.   Qmodification_hooks = intern ("modification-hooks");
  1135.  
  1136.   defsubr (&Stext_properties_at);
  1137.   defsubr (&Sget_text_property);
  1138.   defsubr (&Snext_property_change);
  1139.   defsubr (&Snext_single_property_change);
  1140.   defsubr (&Sprevious_property_change);
  1141.   defsubr (&Sprevious_single_property_change);
  1142.   defsubr (&Sadd_text_properties);
  1143.   defsubr (&Sput_text_property);
  1144.   defsubr (&Sset_text_properties);
  1145.   defsubr (&Sremove_text_properties);
  1146. /*  defsubr (&Serase_text_properties); */
  1147. /*  defsubr (&Scopy_text_properties); */
  1148. }
  1149.  
  1150. #else
  1151.  
  1152. lose -- this shouldn't be compiled if USE_TEXT_PROPERTIES isn't defined
  1153.  
  1154. #endif /* USE_TEXT_PROPERTIES */
  1155.