aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2006-04-30 02:35:35 +0000
committerRichard M. Stallman2006-04-30 02:35:35 +0000
commit03475b051a0a5c0a0409ac6a81d30b47b94a2be9 (patch)
tree49d09e015561330ee2d205a1a512516f80356370
parentd33c1fcea2d4ccbc45ceedc430337971ab087b9c (diff)
downloademacs-03475b051a0a5c0a0409ac6a81d30b47b94a2be9.tar.gz
emacs-03475b051a0a5c0a0409ac6a81d30b47b94a2be9.zip
(Multiline Font Lock): Renamed from Multi line Font Lock Elements.
Much clarification. (Font Lock Multiline, Region to Fontify): Much clarification.
-rw-r--r--lispref/modes.texi219
1 files changed, 105 insertions, 114 deletions
diff --git a/lispref/modes.texi b/lispref/modes.texi
index e0abc221ef3..9e55ca847fc 100644
--- a/lispref/modes.texi
+++ b/lispref/modes.texi
@@ -2345,8 +2345,8 @@ Search-based fontification happens second.
2345* Syntactic Font Lock:: Fontification based on syntax tables. 2345* Syntactic Font Lock:: Fontification based on syntax tables.
2346* Setting Syntax Properties:: Defining character syntax based on context 2346* Setting Syntax Properties:: Defining character syntax based on context
2347 using the Font Lock mechanism. 2347 using the Font Lock mechanism.
2348* Multi line Font Lock Elements:: How to coerce Font Lock into properly 2348* Multiline Font Lock:: How to coerce Font Lock into properly
2349 highlighting multiline elements. 2349 highlighting multiline constructs.
2350@end menu 2350@end menu
2351 2351
2352@node Font Lock Basics 2352@node Font Lock Basics
@@ -2625,7 +2625,7 @@ Its value should have one of the forms described in this table.
2625 2625
2626@strong{Warning:} Do not design an element of @code{font-lock-keywords} 2626@strong{Warning:} Do not design an element of @code{font-lock-keywords}
2627to match text which spans lines; this does not work reliably. 2627to match text which spans lines; this does not work reliably.
2628For details, see @xref{Multi line Font Lock Elements}. 2628For details, see @xref{Multiline Font Lock}.
2629 2629
2630You can use @var{case-fold} in @code{font-lock-defaults} to specify 2630You can use @var{case-fold} in @code{font-lock-defaults} to specify
2631the value of @code{font-lock-keywords-case-fold-search} which says 2631the value of @code{font-lock-keywords-case-fold-search} which says
@@ -2944,8 +2944,8 @@ value returns @code{font-lock-comment-face} for comments and
2944 2944
2945This can be used to highlighting different kinds of strings or 2945This can be used to highlighting different kinds of strings or
2946comments differently. It is also sometimes abused together with 2946comments differently. It is also sometimes abused together with
2947@code{font-lock-syntactic-keywords} to highlight elements that span 2947@code{font-lock-syntactic-keywords} to highlight constructs that span
2948multiple lines, but this is too obscure to document in this manual. 2948multiple lines, but this is too esoteric to document here.
2949 2949
2950Specify this variable using @var{other-vars} in 2950Specify this variable using @var{other-vars} in
2951@code{font-lock-defaults}. 2951@code{font-lock-defaults}.
@@ -3015,64 +3015,60 @@ Major modes normally set this variable with @var{other-vars} in
3015@code{font-lock-defaults}. 3015@code{font-lock-defaults}.
3016@end defvar 3016@end defvar
3017 3017
3018@node Multi line Font Lock Elements 3018@node Multiline Font Lock
3019@subsection Multi line Font Lock Elements 3019@subsection Multiline Font Lock Constructs
3020@cindex multi line font lock 3020@cindex multiline font lock
3021 3021
3022Normally, Font Lock elements specified via @code{font-lock-keywords} 3022 Normally, elements of @code{font-lock-keywords} should not match
3023should not match across multiple lines. If they do, Font Lock may 3023across multiple lines; that doesn't work reliably, because Font Lock
3024fail to highlight them properly. This is fundamentally due to the 3024usually scans just part of the buffer, and it can miss a multi-line
3025fact that Font Lock does not always look at the whole buffer at 3025construct that crosses the line boundary where the scan starts. (The
3026a time, for obvious performance reasons, and instead only looks 3026scan normally starts at the beginning of a line.)
3027at a small chunk at a time. In order for the highlight to be correct, 3027
3028a chunk should not straddle an element matched by 3028 Making elements that match multiline constructs work properly has
3029@code{font-lock-keywords}. The default heuristic used for this is to 3029two aspects: correct @emph{identification} and correct
3030start and end chunks at the beginning resp. end of a line. 3030@emph{rehighlighting}. The first means that Font Lock finds all
3031 3031multiline constructs. The second means that Font Lock will correctly
3032In order for Font Lock to properly highlight elements that span 3032rehighlight all the relevant text when a multiline construct is
3033multiple lines, the package author has to ensure two things: correct 3033changed---for example, if some of the text that was previously part of
3034@emph{discovery} and correct @emph{re}highlighting. The first ensures 3034a multiline construct ceases to be part of it. The two aspects are
3035that Font Lock finds all multiline elements. The second ensures that 3035closely related, and often getting one of them to work will appear to
3036Font Lock will correctly re-highlight all the relevant text when 3036make the other also work. However, for reliable results you must
3037a multiline element is changed, e.g. causing some of the text that was 3037attend explicitly to both aspects.
3038previously part of a multiline element to not be part of it any more. 3038
3039The two aspects are closely related and often getting one of the two 3039 There are two ways to ensure correct identification of multiline
3040to work will appear to make the other also work. But both aspects 3040constructs:
3041have to be taken care of for the multiline elements to be 3041
3042reliably highlighted.
3043
3044Correct @emph{re}highlighting of multiline elements can be done in the
3045following ways:
3046@itemize 3042@itemize
3047@item 3043@item
3048Apply the @code{font-lock-multiline} property to the element. 3044Place a @code{font-lock-multiline} or @code{jit-lock-defer-multiline}
3049This will ensure that the whole element will always be immediately 3045property on the construct when it is added to the buffer.
3050rehighlighted if any part of it is changed. This can sometimes be
3051done automatically by setting the @code{font-lock-multiline} variable.
3052@item
3053Rely on @code{jit-lock-contextually}. This will only rehighlight the
3054part of the element that follows the actual change, and will do it
3055after a short delay. This only works if the highlighting of the
3056various parts of your multiline element never depends on text in
3057subsequent lines. Since @code{jit-lock-contextually} is activated by
3058default, this can be an attractive solution.
3059@item
3060Apply the @code{jit-lock-defer-multiline} property to the element.
3061This works only if @code{jit-lock-contextually} is used and allows it
3062to also work when highlighting does depend on subsequent lines.
3063@item 3046@item
3047Use @code{font-lock-fontify-region-function} hook to extend the scan
3048so that the scanned text never starts or ends in the middle of a
3049multiline construct.
3064@end itemize 3050@end itemize
3065 3051
3066Discovery of new multiline elements can be done in the following ways: 3052 There are three ways to do rehighlighting of multiline constructs:
3053
3067@itemize 3054@itemize
3068@item 3055@item
3069Manually placing a @code{font-lock-multiline} or 3056Place a @code{font-lock-multiline} property on the construct. This
3070@code{jit-lock-defer-multiline} property on the element when it is 3057will rehighlight the whole construct if any part of it is changed. In
3071added to the buffer. 3058some cases you can do this automatically by setting the
3059@code{font-lock-multiline} variable.
3060@item
3061Use @code{jit-lock-contextually}. This will only rehighlight the part
3062of the construct that follows the actual change, and will do it after
3063a short delay. This only works if the highlighting of the various
3064parts of your multiline construct never depends on text in subsequent
3065lines. Since @code{jit-lock-contextually} is activated by default,
3066this can be an attractive solution.
3067@item
3068Place a @code{jit-lock-defer-multiline} property on the construct.
3069This works only if @code{jit-lock-contextually} is used, but it can
3070handle the case where highlighting depends on subsequent lines.
3072@item 3071@item
3073Using the @code{font-lock-fontify-region-function} hook to extend the
3074highlighted chunks so that they never start or end in the middle of
3075multiline element.
3076@end itemize 3072@end itemize
3077 3073
3078@menu 3074@menu
@@ -3084,80 +3080,75 @@ multiline element.
3084@node Font Lock Multiline 3080@node Font Lock Multiline
3085@subsubsection Font Lock Multiline 3081@subsubsection Font Lock Multiline
3086 3082
3087In order to make it possible to properly @emph{re}highlight elements that 3083 One way to ensure reliable rehighlighting of multiline Font Lock
3088span multiple lines, Font Lock obeys a special text property 3084constructs is to put on the text property @code{font-lock-multiline}.
3089@code{font-lock-multiline} which if non-@code{nil} indicates that this 3085It should be present and non-@code{nil} for text that is part of a
3090piece of text was highlighted as part of a multiline construct. 3086multiline construct.
3091So when Font Lock is asked to rehighlight a region, it first verifies 3087
3092the two boundaries and extends them as needed so they do not fall in 3088 When Font Lock is about to highlight a range of text, it first
3093the middle of a piece of text marked with the 3089extends the boundaries of the range as necessary so that they do not
3094@code{font-lock-multiline} property. Immediately after that, it also 3090fall within text marked with the @code{font-lock-multiline} property.
3095erases all @code{font-lock-multiline} properties from the region it is 3091Then it removes any @code{font-lock-multiline} properties from the
3096about to highlight, so it is the responsability of the highlighting 3092range, and highlights it. The highlighting specification (mostly
3097specification (mostly @code{font-lock-keywords}) to make sure that 3093@code{font-lock-keywords}) must reinstall this property each time,
3098this property is re-added where needed so as to inform the next round 3094whenever it is appropriate.
3099of Font Locking of the presence of a multiline construct. 3095
3100 3096 @strong{Warning:} don't use the @code{font-lock-multiline} property
3101It is important to understand that the @code{font-lock-multiline} 3097on large ranges of text, because that will make rehighlighting slow.
3102property should preferably only be used on Font Lock elements of
3103moderate size: every time that text is modified within a multiline
3104element (or nearby), the whole multiline element will be completely
3105re-highlighted, so if its size is large, the time to font-lock may
3106render editing painfully slow.
3107 3098
3108@defvar font-lock-multiline 3099@defvar font-lock-multiline
3109If the @code{font-lock-multiline} variable is set to @code{t}, Font 3100If the @code{font-lock-multiline} variable is set to @code{t}, Font
3110Lock will try to automatically add the @code{font-lock-multiline} 3101Lock will try to add the @code{font-lock-multiline} property
3111property on the keywords that span several lines. This is no silver 3102automatically on multiline constructs. This is not a universal
3112bullet however since it slows down Font Lock somewhat, and may miss 3103solution, however, since it slows down Font Lock somewhat. It can
3113some cases or make the property larger or smaller than necessary, 3104miss some multiline constructs, or make the property larger or smaller
3114especially for keywords whose @var{MATCHER} is a function, in which 3105than necessary.
3115case the function needs to make sure that the submatch 0 covers the 3106
3116whole relevant multiline entity even if only a small subpart will 3107For elements whose @var{matcher} is a function, the function should
3117be highlighted. It is often just as easy to add the 3108ensure that submatch 0 covers the whole relevant multiline construct,
3118@code{font-lock-multiline} property by hand. 3109even if only a small subpart will be highlighted. It is often just as
3110easy to add the @code{font-lock-multiline} property by hand.
3119@end defvar 3111@end defvar
3120 3112
3121As mentioned, this property is mostly intended to ensure proper 3113 The @code{font-lock-multiline} property is meant to ensure proper
3122@emph{re}fontification. It does not magically discover new 3114refontification; it does not automatically identify new multiline
3123multiline elements. To discover new multiline elements, all that is 3115constructs. Identifying the requires that Font-Lock operate on large
3124required is that font-lock operate on large enough chunks at a time. 3116enough chunks at a time. This will happen by accident on many cases,
3125This will happen by accident on many cases, which may give the 3117which may give the impression that multiline constructs magically work.
3126impression that multiline elements magically work. If you set the 3118If you set the @code{font-lock-multiline} variable non-@code{nil},
3127@code{font-lock-multiline} variable, this impression will be even 3119this impression will be even stronger, since the highlighting of those
3128stronger since the highlighting of those found elements will be 3120constructs which are found will be properly updated from then on.
3129properly preserved from then on. But for such multiline elements to 3121But that does not work reliably.
3130be found reliably, you will need to either manually put the 3122
3131@code{font-lock-multiline} property from some appropriate piece of 3123 To find multiline constructs reliably, you must either manually
3132code run before Font Lock, or hook into 3124place the @code{font-lock-multiline} property on the text before
3133@code{font-lock-fontify-region-function} to manually extend the chunks 3125Font-Lock looks at it, or use
3134of text that Font Lock highlights so they never start or stop in the 3126@code{font-lock-fontify-region-function}.
3135middle of a multiline element.
3136 3127
3137@node Region to Fontify 3128@node Region to Fontify
3138@subsubsection Region to Fontify after a Buffer Change 3129@subsubsection Region to Fontify after a Buffer Change
3139 3130
3140 When a buffer is changed, the region that Font Lock refontifies is by 3131 When a buffer is changed, the region that Font Lock refontifies is
3141default the smallest sequence of whole lines that spans the change. 3132by default the smallest sequence of whole lines that spans the change.
3142While this works well most of the time, sometimes it doesn't---for 3133While this works well most of the time, sometimes it doesn't---for
3143example, when a buffer change has changed the syntactic meaning of text 3134example, when a change alters the syntactic meaning of text on an
3144on an earlier line. 3135earlier line.
3145 3136
3146You can enlarge (or even reduce) the region to fontify by setting @c either of 3137 You can enlarge (or even reduce) the region to fontify by setting
3147the following variables: 3138one the following variables:
3148 3139
3149@defvar font-lock-extend-region-function 3140@defvar font-lock-extend-region-function
3150This buffer-local variable is either @code{nil} or is a function that 3141This buffer-local variable is either @code{nil} or a function for
3151determines the region to fontify, which Emacs then calls after each 3142Font-Lock to call to determine the region to scan and fontify.
3152buffer change.
3153 3143
3154The function is given three parameters, the standard @var{beg}, 3144The function is given three parameters, the standard @var{beg},
3155@var{end}, and @var{old-len} from after-change-functions (@pxref{Change 3145@var{end}, and @var{old-len} from after-change-functions
3156Hooks}). It should return either a cons of the beginning and end buffer 3146(@pxref{Change Hooks}). It should return either a cons of the
3157positions (in that order) of the region to fontify, or @code{nil} (which 3147beginning and end buffer positions (in that order) of the region to
3158directs the caller to fontify the default region). This function needs 3148fontify, or @code{nil} (which means choose the region in the standard
3159to preserve point, the match-data, and the current restriction. 3149way). This function needs to preserve point, the match-data, and the
3160The region it returns may start or end in the middle of a line. 3150current restriction. The region it returns may start or end in the
3151middle of a line.
3161 3152
3162Since this function is called after every buffer change, it should be 3153Since this function is called after every buffer change, it should be
3163reasonably fast. 3154reasonably fast.