home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / font-lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  21.9 KB  |  691 lines

  1. /* Routines to compute the current syntactic context, for font-lock mode.
  2.    Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Copyright (C) 1995 Sun Microsystems.
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: Not in FSF. */
  22.  
  23. /* This code computes the syntactic context of the current point, that is,
  24.    whether point is within a comment, a string, what have you.  It does
  25.    this by picking a point "known" to be outside of any syntactic constructs
  26.    and moving forward, examining the syntax of each character.
  27.  
  28.    Two caches are used: one caches the last point computed, and the other
  29.    caches the last point at the beginning of a line.  This makes there
  30.    be little penalty for moving left-to-right on a line a character at a 
  31.    time; makes starting over on a line be cheap; and makes random-accessing
  32.    within a line relatively cheap.  
  33.  
  34.    When we move to a different line farther down in the file (but within the
  35.    current top-level form) we simply continue computing forward.  If we move
  36.    backward more than a line, or move beyond the end of the current tlf, or
  37.    switch buffers, then we call `beginning-of-defun' and start over from 
  38.    there.
  39.  
  40.    #### We should really rewrite this to keep extents over the buffer
  41.    that hold the current syntactic information.  This would be a big win.
  42.    This way there would be no guessing or incorrect results.
  43.  */
  44.  
  45. #include <config.h>
  46. #include "lisp.h"
  47.  
  48. #include "buffer.h"
  49. #include "insdel.h"
  50. #include "syntax.h"
  51.  
  52. Lisp_Object Qcomment;
  53. Lisp_Object Qblock_comment;
  54. Lisp_Object Qbeginning_of_defun;
  55.  
  56. enum syntactic_context {
  57.   context_none, context_string, context_comment, context_block_comment
  58. };
  59.  
  60. enum block_comment_context {
  61.   ccontext_none, ccontext_start1, ccontext_start2, ccontext_end1
  62. };
  63.  
  64. enum comment_style {
  65.   comment_style_none, comment_style_a, comment_style_b
  66. };
  67.  
  68. struct context_cache {
  69.   Bufpos start_point;            /* beginning of defun */
  70.   Bufpos cur_point;            /* cache location */
  71.   Bufpos end_point;            /* end of defun */
  72.   struct buffer *buffer;        /* does this need to be staticpro'd? */
  73.   enum syntactic_context context;    /* single-char-syntax state */
  74.   enum block_comment_context ccontext;    /* block-comment state */
  75.   enum comment_style style;        /* which comment group */
  76.   unsigned char scontext;        /* active string delimiter */
  77.   int depth;                /* depth in parens */
  78.   int backslash_p;            /* just read a backslash */
  79. };
  80.  
  81. /* We have two caches; one for the current point and one for
  82.    the beginning of line.  We used to rely on the caller to
  83.    tell us when to invalidate them, but now we do it ourselves;
  84.    it lets us be smarter. */
  85.  
  86. static struct context_cache context_cache;
  87.  
  88. static struct context_cache bol_context_cache;
  89.  
  90. int font_lock_debug;
  91.  
  92. #define reset_context_cache(cc) memset (cc, 0, sizeof (struct context_cache))
  93.  
  94. /* This function is called from signal_after_change() to tell us when
  95.    textual changes are made so we can flush our caches when necessary.
  96.  
  97.    We make the following somewhat heuristic assumptions:
  98.  
  99.      (remember that current_point is always >= start_point, but may be
  100.      less than or greater than end_point (we might not be inside any
  101.      top-level form)).
  102.  
  103.    1) Textual changes before the beginning of the current top-level form
  104.       don't affect anything; all we need to do is offset the caches
  105.       appropriately.
  106.    2) Textual changes right at the beginning of the current
  107.       top-level form messes things up and requires that we flush
  108.       the caches.
  109.    3) Textual changes after the beginning of the current top-level form
  110.       and before one or both or the caches invalidates the corresponding
  111.       cache(s).
  112.    4) Textual changes after the caches and before the end of the
  113.       current top-level form don't affect anything; all we need to do is
  114.       offset the caches appropriately.
  115.    5) Textual changes right at the end of the current top-level form
  116.       necessitate recomputing that end value.
  117.    6) Textual changes after the end of the current top-level form
  118.       are ignored. */
  119.  
  120.  
  121. void
  122. font_lock_maybe_update_syntactic_caches (struct buffer *buf, Bufpos start,
  123.                      Bufpos orig_end, Bufpos new_end)
  124. {
  125.   /* Note: either both context_cache and bol_context_cache are valid and
  126.      point to the same buffer, or both are invalid.  If we have to
  127.      invalidate just context_cache, we recopy it from bol_context_cache.
  128.    */
  129.   if (context_cache.buffer != buf)
  130.     /* caches don't apply */
  131.     return;
  132.   /* NOTE: The order of the if statements below is important.  If you
  133.      change them around unthinkingly, you will probably break something. */
  134.   if (orig_end <= context_cache.start_point - 1)
  135.     {
  136.       /* case 1: before the beginning of the current top-level form */
  137.       Charcount diff = new_end - orig_end;
  138.       if (font_lock_debug)
  139.     stderr_out ("font-lock; Case 1\n");
  140.       context_cache.start_point += diff;
  141.       context_cache.cur_point += diff;
  142.       context_cache.end_point += diff;
  143.       bol_context_cache.start_point += diff;
  144.         bol_context_cache.cur_point += diff;
  145.       bol_context_cache.end_point += diff;
  146.     }
  147.   else if (start <= context_cache.start_point)
  148.     {
  149.       if (font_lock_debug)
  150.     stderr_out ("font-lock; Case 2\n");
  151.       /* case 2: right at the current top-level form (paren that starts
  152.      top level form got deleted or moved away from the newline it
  153.      was touching) */
  154.       reset_context_cache (&context_cache);
  155.       reset_context_cache (&bol_context_cache);
  156.     }
  157.   /* OK, now we know that the start is after the beginning of the
  158.      current top-level form. */
  159.   else if (start < bol_context_cache.cur_point)
  160.     {
  161.       if (font_lock_debug)
  162.     stderr_out ("font-lock; Case 3 (1)\n");
  163.       /* case 3: after the beginning of the current top-level form
  164.      and before both of the caches */
  165.       reset_context_cache (&context_cache);
  166.       reset_context_cache (&bol_context_cache);
  167.     }
  168.   else if (start < context_cache.cur_point)
  169.     {
  170.       if (font_lock_debug)
  171.     stderr_out ("font-lock; Case 3 (2)\n");
  172.       /* case 3: but only need to invalidate one cache */
  173.       context_cache = bol_context_cache;
  174.     }
  175.   /* OK, now we know that the start is after the caches. */
  176.   else if (start >= context_cache.end_point)
  177.     {
  178.       if (font_lock_debug)
  179.     stderr_out ("font-lock; Case 6\n");
  180.       /* case 6: after the end of the current top-level form
  181.          and after the caches. */
  182.     }
  183.   else if (orig_end <= context_cache.end_point - 2)
  184.     {
  185.       /* case 4: after the caches and before the end of the
  186.      current top-level form */
  187.       Charcount diff = new_end - orig_end;
  188.       if (font_lock_debug)
  189.     stderr_out ("font-lock; Case 4\n");
  190.       context_cache.end_point += diff;
  191.       bol_context_cache.end_point += diff;
  192.     }
  193.   else
  194.     {
  195.       if (font_lock_debug)
  196.     stderr_out ("font-lock; Case 5\n");
  197.       /* case 5: right at the end of the current top-level form */
  198.       context_cache.end_point = context_cache.start_point - 1;
  199.       bol_context_cache.end_point = context_cache.start_point - 1;
  200.     }
  201. }
  202.  
  203. /* This function is called from Fkill_buffer(). */
  204.  
  205. void
  206. font_lock_buffer_was_killed (struct buffer *buf)
  207. {
  208.   if (context_cache.buffer == buf)
  209.     {
  210.       reset_context_cache (&context_cache);
  211.       reset_context_cache (&bol_context_cache);
  212.     }
  213. }
  214.  
  215. static Bufpos
  216. beginning_of_defun (struct buffer *buf, Bufpos pt)
  217. {
  218.   /* This function can GC */
  219.   Bufpos opt = BUF_PT (buf);
  220.   if (pt == BUF_BEGV (buf))
  221.     return pt;
  222.   BUF_SET_PT (buf, pt);
  223.   /* There used to be some kludginess to call c++-beginning-of-defun
  224.      if we're in C++ mode.  There's no point in this any more;
  225.      we're using cc-mode.  If you really want to get the old c++
  226.      mode working, fix it rather than the C code. */
  227.   call0_in_buffer (buf, Qbeginning_of_defun);
  228.   pt = BUF_PT (buf);
  229.   BUF_SET_PT (buf, opt);
  230.   return pt;
  231. }
  232.  
  233. static Bufpos
  234. end_of_defun (struct buffer *buf, Bufpos pt)
  235. {
  236.   Lisp_Object retval = scan_lists (buf, pt, 1, 0, 0, 1);
  237.   if (NILP (retval))
  238.     return BUF_ZV (buf);
  239.   else
  240.     return XINT (retval);
  241. }
  242.  
  243. /* Set up context_cache for attempting to determine the syntactic context
  244.    in buffer BUF at point PT. */
  245.  
  246. static void
  247. setup_context_cache (struct buffer *buf, Bufpos pt)
  248. {
  249.   /* This function can GC */
  250.   if (context_cache.buffer != buf || pt < context_cache.start_point)
  251.     {
  252. #if 0
  253.     start_over:
  254. #endif
  255.       if (font_lock_debug)
  256.     stderr_out ("reset context cache\n");
  257.       /* OK, completely invalid. */
  258.       reset_context_cache (&context_cache);
  259.       reset_context_cache (&bol_context_cache);
  260.     }
  261.   if (!context_cache.buffer)
  262.     {
  263.       /* Need to recompute the start point. */
  264.       if (font_lock_debug)
  265.     stderr_out ("recompute start\n");
  266.       context_cache.start_point = beginning_of_defun (buf, pt);
  267.       bol_context_cache.start_point = context_cache.start_point;
  268.       bol_context_cache.buffer = context_cache.buffer = buf;
  269.     }
  270.   if (context_cache.end_point < context_cache.start_point)
  271.     {
  272.       /* Need to recompute the end point. */
  273.       if (font_lock_debug)
  274.     stderr_out ("recompute end\n");
  275.       context_cache.end_point = end_of_defun (buf, context_cache.start_point);
  276.       bol_context_cache.end_point = context_cache.end_point;
  277.     }
  278.   if (bol_context_cache.cur_point == 0 ||
  279.       pt < bol_context_cache.cur_point)
  280.     {
  281.       if (font_lock_debug)
  282.     stderr_out ("reset to start\n");
  283.       pt = context_cache.start_point;
  284.       /* Reset current point to start of buffer. */
  285.       context_cache.cur_point = pt;
  286.       context_cache.context = context_none;
  287.       context_cache.ccontext = ccontext_none;
  288.       context_cache.style = comment_style_none;
  289.       context_cache.scontext = '\000';
  290.       context_cache.depth = 0;
  291.       context_cache.backslash_p = ((pt > 1) &&
  292.                    (BUF_FETCH_CHAR (buf, pt - 1) == '\\'));
  293.       bol_context_cache = context_cache;
  294.       return;
  295.     }
  296.   else if (pt < context_cache.cur_point)
  297.     {
  298.       if (font_lock_debug)
  299.     stderr_out ("reset to bol\n");
  300.       /* bol cache is OK but current_cache is not. */
  301.       context_cache = bol_context_cache;
  302.       return;
  303.     }
  304.   else if (pt <= context_cache.end_point)
  305.     {
  306.       if (font_lock_debug)
  307.     stderr_out ("everything is OK\n");
  308.       /* in same top-level form. */
  309.       return;
  310.     }
  311.   {
  312.     /* OK, we're past the end of the top-level form. */
  313.     Bufpos maxpt = max (context_cache.end_point, context_cache.cur_point);
  314. #if 0
  315.     int shortage;
  316. #endif
  317.  
  318.     if (font_lock_debug)
  319.       stderr_out ("past end\n");
  320.     if (pt <= maxpt)
  321.       /* OK, fine. */
  322.       return;
  323. #if 0
  324.     /* This appears to cause huge slowdowns in files like
  325.        emacsfns.h, which have no top-level forms.
  326.  
  327.        In any case, it's not really necessary that we know for
  328.        sure the top-level form we're in; if we're in a form
  329.        but the form we have recorded is the previous one,
  330.        it will be OK. */
  331.  
  332.     scan_buffer (buf, '\n', maxpt, pt, 1, &shortage, 1);
  333.     if (!shortage)
  334.       /* If there was a newline in the region past the known universe,
  335.      we might be inside another top-level form, so start over.
  336.      Otherwise, we're outside of any top-level forms and we know
  337.      the one directly before us, so it's OK. */
  338.       goto start_over;
  339. #endif
  340.   }
  341. }
  342.  
  343. #define SYNTAX_START_STYLE(table, c1, c2)                 \
  344.   (SYNTAX_STYLES_MATCH_START_P (table, c1, c2, SYNTAX_COMMENT_STYLE_A) ? \
  345.    comment_style_a :                             \
  346.    SYNTAX_STYLES_MATCH_START_P (table, c1, c2, SYNTAX_COMMENT_STYLE_B) ? \
  347.    comment_style_b :                             \
  348.    comment_style_none)
  349.  
  350. #define SYNTAX_END_STYLE(table, c1, c2)                    \
  351.   (SYNTAX_STYLES_MATCH_END_P (table, c1, c2, SYNTAX_COMMENT_STYLE_A) ?    \
  352.    comment_style_a :                            \
  353.    SYNTAX_STYLES_MATCH_END_P (table, c1, c2, SYNTAX_COMMENT_STYLE_B) ?    \
  354.    comment_style_b :                            \
  355.    comment_style_none)
  356.       
  357. #define SINGLE_SYNTAX_STYLE(table, c)                    \
  358.       (SYNTAX_STYLES_MATCH_1CHAR_P (table, c, SYNTAX_COMMENT_STYLE_A) ?    \
  359.        comment_style_a :                        \
  360.        SYNTAX_STYLES_MATCH_1CHAR_P (table, c, SYNTAX_COMMENT_STYLE_B) ?    \
  361.        comment_style_b :                        \
  362.        comment_style_none)
  363.  
  364. /* Set up context_cache for position PT in BUF. */
  365.  
  366. static void
  367. find_context (struct buffer *buf, Bufpos pt)
  368. {
  369.   /* This function can GC */
  370.   Lisp_Object syntax_table = buf->syntax_table;
  371.   Bufbyte prev_c, c;
  372.   Bufpos target = pt;
  373.   setup_context_cache (buf, pt);
  374.   pt = context_cache.cur_point;
  375.  
  376.   if (pt > BUF_BEGV (buf))
  377.     c = BUF_FETCH_CHAR (buf, pt - 1);
  378.   else
  379.     c = '\n'; /* to get bol_context_cache at point-min */
  380.  
  381.   for (; pt < target; pt++, context_cache.cur_point = pt)
  382.     {
  383.       prev_c = c;
  384.       c = BUF_FETCH_CHAR (buf, pt);
  385.  
  386.       if (prev_c == '\n')
  387.     bol_context_cache = context_cache;
  388.  
  389.       if (context_cache.backslash_p)
  390.     {
  391.       context_cache.backslash_p = 0;
  392.       continue;
  393.     }
  394.  
  395.       switch (SYNTAX (syntax_table, c))
  396.     {
  397.     case Sescape:
  398.       context_cache.backslash_p = 1;
  399.       break;
  400.  
  401.     case Sopen:
  402.       if (context_cache.context == context_none)
  403.         context_cache.depth++;
  404.       break;
  405.  
  406.     case Sclose:
  407.       if (context_cache.context == context_none)
  408.         context_cache.depth--;
  409.       break;
  410.  
  411.     case Scomment:
  412.       if (context_cache.context == context_none)
  413.         {
  414.           context_cache.context = context_comment;
  415.           context_cache.ccontext = ccontext_none;
  416.           context_cache.style = SINGLE_SYNTAX_STYLE (syntax_table, c);
  417.           if (context_cache.style == comment_style_none) abort ();
  418.         }
  419.       break;
  420.  
  421.     case Sendcomment:
  422.       if (context_cache.style != SINGLE_SYNTAX_STYLE (syntax_table, c))
  423.         ;
  424.       else
  425.            if (context_cache.context == context_comment)
  426.         {
  427.           context_cache.context = context_none;
  428.           context_cache.style = comment_style_none;
  429.         }
  430.       else if (context_cache.context == context_block_comment &&
  431.            (context_cache.ccontext == ccontext_start2 ||
  432.             context_cache.ccontext == ccontext_end1))
  433.         {
  434.           context_cache.context = context_none;
  435.           context_cache.ccontext = ccontext_none;
  436.           context_cache.style = comment_style_none;
  437.         }
  438.       break;
  439.  
  440.     case Sstring:
  441.           {
  442.             if (context_cache.context == context_string &&
  443.                 context_cache.scontext == c)
  444.         {
  445.           context_cache.context = context_none;
  446.           context_cache.scontext = '\000';
  447.         }
  448.             else if (context_cache.context == context_none)
  449.         {
  450.               unsigned char stringterm = SYNTAX_MATCH (syntax_table, c);
  451.               if (stringterm == 0) stringterm = c;
  452.           context_cache.context = context_string;
  453.           context_cache.scontext = stringterm;
  454.           context_cache.ccontext = ccontext_none;
  455.         }
  456.             break;
  457.           }
  458.     default:
  459.       ;
  460.     }
  461.  
  462.       /* That takes care of the characters with manifest syntax.
  463.      Now we've got to hack multi-char sequences that start
  464.      and end block comments.
  465.        */
  466.       if ((SYNTAX_COMMENT_BITS (syntax_table, c) &
  467.        SYNTAX_SECOND_CHAR_START) &&
  468.       context_cache.context == context_none &&
  469.       context_cache.ccontext == ccontext_start1 &&
  470.       SYNTAX_START_P (syntax_table, prev_c, c) /* the two chars match */
  471.       )
  472.     {
  473.       context_cache.ccontext = ccontext_start2;
  474.       context_cache.style = SYNTAX_START_STYLE (syntax_table, prev_c, c);
  475.       if (context_cache.style == comment_style_none) abort ();
  476.     }
  477.       else if ((SYNTAX_COMMENT_BITS (syntax_table, c) &
  478.         SYNTAX_FIRST_CHAR_START) &&
  479.            context_cache.context == context_none &&
  480.            (context_cache.ccontext == ccontext_none ||
  481.         context_cache.ccontext == ccontext_start1))
  482.     {
  483.       context_cache.ccontext = ccontext_start1;
  484.       context_cache.style = comment_style_none; /* should be this already*/
  485.     }
  486.       else if ((SYNTAX_COMMENT_BITS (syntax_table, c) &
  487.         SYNTAX_SECOND_CHAR_END) &&
  488.            context_cache.context == context_block_comment &&
  489.            context_cache.ccontext == ccontext_end1 &&
  490.            SYNTAX_END_P (syntax_table, prev_c, c) &&
  491.            /* the two chars match */
  492.            context_cache.style ==
  493.            SYNTAX_END_STYLE (syntax_table, prev_c, c)
  494.            )
  495.     {
  496.       context_cache.context = context_none;
  497.       context_cache.ccontext = ccontext_none;
  498.       context_cache.style = comment_style_none;
  499.     }
  500.       else if ((SYNTAX_COMMENT_BITS (syntax_table, c) &
  501.         SYNTAX_FIRST_CHAR_END) &&
  502.            context_cache.context == context_block_comment &&
  503.            (context_cache.style ==
  504.         SYNTAX_END_STYLE (syntax_table, c,
  505.                   BUF_FETCH_CHAR (buf, pt+1))) &&
  506.            (context_cache.ccontext == ccontext_start2 ||
  507.         context_cache.ccontext == ccontext_end1))
  508.     /* #### is it right to check for end1 here?? */
  509.     {
  510.       if (context_cache.style == comment_style_none) abort ();
  511.       context_cache.ccontext = ccontext_end1;
  512.     }
  513.  
  514.       else if (context_cache.ccontext == ccontext_start1)
  515.     {
  516.       if (context_cache.context != context_none) abort ();
  517.       context_cache.ccontext = ccontext_none;
  518.     }
  519.       else if (context_cache.ccontext == ccontext_end1)
  520.     {
  521.       if (context_cache.context != context_block_comment) abort ();
  522.       context_cache.context = context_none;
  523.       context_cache.ccontext = ccontext_start2;
  524.     }
  525.  
  526.       if (context_cache.ccontext == ccontext_start2 &&
  527.       context_cache.context == context_none)
  528.     {
  529.       context_cache.context = context_block_comment;
  530.       if (context_cache.style == comment_style_none) abort ();
  531.     }
  532.       else if (context_cache.ccontext == ccontext_none &&
  533.            context_cache.context == context_block_comment)
  534.     {
  535.       context_cache.context = context_none;
  536.     }
  537.     }
  538. }
  539.  
  540. static Lisp_Object
  541. context_to_symbol (enum syntactic_context context)
  542. {
  543.   switch (context)
  544.     {
  545.     case context_none:        return (Qnil);
  546.     case context_string:    return (Qstring);
  547.     case context_comment:    return (Qcomment);
  548.     case context_block_comment:    return (Qblock_comment);
  549.     default: abort ();
  550.     }
  551.   return Qnil;    /* suppress compiler warning */
  552. }
  553.  
  554. DEFUN ("buffer-syntactic-context", Fbuffer_syntactic_context,
  555.        Sbuffer_syntactic_context, 0, 1, 0,
  556.        "Return the syntactic context of BUFFER at point.\n\
  557. If BUFFER is nil or omitted, the current buffer is assumed.\n\
  558. The returned value is one of the following symbols:\n\
  559. \n\
  560.     nil        ; meaning no special interpretation\n\
  561.     string        ; meaning point is within a string\n\
  562.     comment        ; meaning point is within a line comment\n\
  563.     block-comment    ; meaning point is within a block comment\n\
  564. \n\
  565. See also the function `buffer-syntactic-context-depth', which returns\n\
  566. the current nesting-depth within all parenthesis-syntax delimiters\n\
  567. and the function `syntactically-sectionize', which will map a function\n\
  568. over each syntactic context in a region.\n\
  569. \n\
  570. WARNING: this may alter match-data.")
  571.      (buffer)
  572.      Lisp_Object buffer;
  573. {
  574.   /* This function can GC */
  575.   struct buffer *buf = decode_buffer (buffer, 0);
  576.   find_context (buf, BUF_PT (buf));
  577.   return context_to_symbol (context_cache.context);
  578. }
  579.  
  580. DEFUN ("buffer-syntactic-context-depth", Fbuffer_syntactic_context_depth,
  581.        Sbuffer_syntactic_context_depth, 0, 1, 0,
  582.    "Return the depth within all parenthesis-syntax delimiters at point.\n\
  583. If BUFFER is nil or omitted, the current buffer is assumed.\n\
  584. WARNING: this may alter match-data.")
  585.      (buffer)
  586.      Lisp_Object buffer;
  587. {
  588.   /* This function can GC */
  589.   struct buffer *buf = decode_buffer (buffer, 0);
  590.   find_context (buf, BUF_PT (buf));
  591.   return make_number (context_cache.depth);
  592. }
  593.  
  594.  
  595. DEFUN ("syntactically-sectionize", Fsyntactically_sectionize,
  596.        Ssyntactically_sectionize, 3, 4, 0,
  597.        "Calls FUNCTION for each contiguous syntactic context in the region.\n\
  598. Calls the given function with four arguments: the start and end of the\n\
  599. region, a symbol representing the syntactic context, and the current\n\
  600. depth (as returned by the functions `buffer-syntactic-context' and\n\
  601. `buffer-syntactic-context-depth').  When this function is called, the\n\
  602. current buffer will be set to BUFFER.\n\
  603. \n\
  604. WARNING: this may alter match-data.")
  605.     (function, start, end, buffer)
  606.     Lisp_Object function, start, end, buffer;
  607. {
  608.   /* This function can GC */
  609.   Bufpos pt, e;
  610.   int edepth;
  611.   enum syntactic_context this_context;
  612.   Lisp_Object extent = Qnil;
  613.   struct gcpro gcpro1;
  614.   struct buffer *buf = decode_buffer (buffer, 0);
  615.  
  616.   get_bufrange (buf, start, end, &pt, &e, 0);
  617.  
  618.   find_context (buf, pt);
  619.  
  620.   GCPRO1 (extent);
  621.   while (pt < e)
  622.     {
  623.       Bufpos estart, eend;
  624.       /* skip over "blank" areas, and bug out at end-of-buffer. */
  625.       while (context_cache.context == context_none)
  626.     {
  627.       pt++;
  628.       if (pt >= e) goto DONE_LABEL;
  629.       find_context (buf, pt);
  630.     }
  631.       /* We've found a non-blank area; keep going until we reach its end */
  632.       this_context = context_cache.context;
  633.       estart = pt;
  634.  
  635.       /* Minor kludge: consider the comment-start character(s) a part of
  636.      the comment.
  637.        */
  638.       if (this_context == context_block_comment &&
  639.       context_cache.ccontext == ccontext_start2)
  640.     estart -= 2;
  641.       else if (this_context == context_comment)
  642.     estart -= 1;
  643.  
  644.       edepth = context_cache.depth;
  645.       while (context_cache.context == this_context && pt < e)
  646.     {
  647.       pt++;
  648.       find_context (buf, pt);
  649.     }
  650.  
  651.       eend = pt;
  652.  
  653.       /* Minor kludge: consider the character which terminated the comment
  654.      a part of the comment.
  655.        */
  656.       if ((this_context == context_block_comment ||
  657.        this_context == context_comment)
  658.       && pt < e)
  659.     eend++;
  660.  
  661.       if (estart == eend)
  662.     continue;
  663.       call4_in_buffer (buf, function, make_number (estart),
  664.                make_number (eend == e ? e : eend - 1),
  665.                context_to_symbol (this_context),
  666.                make_number (edepth));
  667.     }
  668.  DONE_LABEL:
  669.   UNGCPRO;
  670.   return Qnil;
  671. }
  672.  
  673. void
  674. syms_of_font_lock (void)
  675. {
  676.   defsymbol (&Qcomment, "comment");
  677.   defsymbol (&Qblock_comment, "block-comment");
  678.   defsymbol (&Qbeginning_of_defun, "beginning-of-defun");
  679.  
  680.   defsubr (&Sbuffer_syntactic_context);
  681.   defsubr (&Sbuffer_syntactic_context_depth);
  682.   defsubr (&Ssyntactically_sectionize);
  683. }
  684.  
  685. void
  686. vars_of_font_lock (void)
  687. {
  688.   memset (&context_cache, 0, sizeof (context_cache));
  689.   memset (&bol_context_cache, 0, sizeof (bol_context_cache));
  690. }
  691.