aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2006-04-29 15:11:38 +0000
committerStefan Monnier2006-04-29 15:11:38 +0000
commit830951b02a12688c2515de9369f8efc971371027 (patch)
tree7dfa20b756b6b9dbf0bcfca870e8dceadfc5d956
parent20bfe3873f561e00fd59d6d0be339dfa131e8af7 (diff)
downloademacs-830951b02a12688c2515de9369f8efc971371027.tar.gz
emacs-830951b02a12688c2515de9369f8efc971371027.zip
Improve the documentation of how to hilight multiline elements.
-rw-r--r--lispref/modes.texi100
1 files changed, 82 insertions, 18 deletions
diff --git a/lispref/modes.texi b/lispref/modes.texi
index 3f56179231c..e0abc221ef3 100644
--- a/lispref/modes.texi
+++ b/lispref/modes.texi
@@ -3029,7 +3029,51 @@ a chunk should not straddle an element matched by
3029@code{font-lock-keywords}. The default heuristic used for this is to 3029@code{font-lock-keywords}. The default heuristic used for this is to
3030start and end chunks at the beginning resp. end of a line. 3030start and end chunks at the beginning resp. end of a line.
3031 3031
3032To work around this limitations, a few tools are provided. 3032In order for Font Lock to properly highlight elements that span
3033multiple lines, the package author has to ensure two things: correct
3034@emph{discovery} and correct @emph{re}highlighting. The first ensures
3035that Font Lock finds all multiline elements. The second ensures that
3036Font Lock will correctly re-highlight all the relevant text when
3037a multiline element is changed, e.g. causing some of the text that was
3038previously part of a multiline element to not be part of it any more.
3039The two aspects are closely related and often getting one of the two
3040to work will appear to make the other also work. But both aspects
3041have to be taken care of for the multiline elements to be
3042reliably highlighted.
3043
3044Correct @emph{re}highlighting of multiline elements can be done in the
3045following ways:
3046@itemize
3047@item
3048Apply the @code{font-lock-multiline} property to the element.
3049This will ensure that the whole element will always be immediately
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
3064@end itemize
3065
3066Discovery of new multiline elements can be done in the following ways:
3067@itemize
3068@item
3069Manually placing a @code{font-lock-multiline} or
3070@code{jit-lock-defer-multiline} property on the element when it is
3071added to the buffer.
3072@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
3033 3077
3034@menu 3078@menu
3035* Font Lock Multiline:: Marking multiline chunks with a text property 3079* Font Lock Multiline:: Marking multiline chunks with a text property
@@ -3040,24 +3084,24 @@ To work around this limitations, a few tools are provided.
3040@node Font Lock Multiline 3084@node Font Lock Multiline
3041@subsubsection Font Lock Multiline 3085@subsubsection Font Lock Multiline
3042 3086
3043In order to make it possible to properly highlight elements that span 3087In order to make it possible to properly @emph{re}highlight elements that
3044multiple lines, Font Lock obeys a special text property 3088span multiple lines, Font Lock obeys a special text property
3045@code{font-lock-multiline} which if non-@code{nil} indicates that this 3089@code{font-lock-multiline} which if non-@code{nil} indicates that this
3046piece of text is part of a multiline construct. So when Font Lock is 3090piece of text was highlighted as part of a multiline construct.
3047asked to highlight a region, it first verifies the two boundaries and 3091So when Font Lock is asked to rehighlight a region, it first verifies
3048extends them as needed so they do not fall in the middle of a piece of 3092the two boundaries and extends them as needed so they do not fall in
3049text marked with the @code{font-lock-multiline} property. 3093the middle of a piece of text marked with the
3050Immediately after that, it also erases all @code{font-lock-multiline} 3094@code{font-lock-multiline} property. Immediately after that, it also
3051properties from the region it is about to highlight, so it is the 3095erases all @code{font-lock-multiline} properties from the region it is
3052responsability of the highlighting specification (mostly 3096about to highlight, so it is the responsability of the highlighting
3053@code{font-lock-keywords}) to make sure that this property is re-added 3097specification (mostly @code{font-lock-keywords}) to make sure that
3054where needed so as to inform the next round of Font Locking of the 3098this property is re-added where needed so as to inform the next round
3055presence of a multiline construct. 3099of Font Locking of the presence of a multiline construct.
3056 3100
3057It is important to understand that the @code{font-lock-multiline} 3101It is important to understand that the @code{font-lock-multiline}
3058property should preferably only be used on Font Lock elements of 3102property should preferably only be used on Font Lock elements of
3059moderate size: every time that text is modified within the multiline 3103moderate size: every time that text is modified within a multiline
3060elements (or nearby), the whole multiline element will be completely 3104element (or nearby), the whole multiline element will be completely
3061re-highlighted, so if its size is large, the time to font-lock may 3105re-highlighted, so if its size is large, the time to font-lock may
3062render editing painfully slow. 3106render editing painfully slow.
3063 3107
@@ -3065,11 +3109,31 @@ render editing painfully slow.
3065If the @code{font-lock-multiline} variable is set to @code{t}, Font 3109If the @code{font-lock-multiline} variable is set to @code{t}, Font
3066Lock will try to automatically add the @code{font-lock-multiline} 3110Lock will try to automatically add the @code{font-lock-multiline}
3067property on the keywords that span several lines. This is no silver 3111property on the keywords that span several lines. This is no silver
3068bullet however since it slows down Font Lock somewhat, and still does 3112bullet however since it slows down Font Lock somewhat, and may miss
3069not always find all multiline constructs, especially when used with 3113some cases or make the property larger or smaller than necessary,
3070Jit Lock, which is enabled by default. 3114especially for keywords whose @var{MATCHER} is a function, in which
3115case the function needs to make sure that the submatch 0 covers the
3116whole relevant multiline entity even if only a small subpart will
3117be highlighted. It is often just as easy to add the
3118@code{font-lock-multiline} property by hand.
3071@end defvar 3119@end defvar
3072 3120
3121As mentioned, this property is mostly intended to ensure proper
3122@emph{re}fontification. It does not magically discover new
3123multiline elements. To discover new multiline elements, all that is
3124required is that font-lock operate on large enough chunks at a time.
3125This will happen by accident on many cases, which may give the
3126impression that multiline elements magically work. If you set the
3127@code{font-lock-multiline} variable, this impression will be even
3128stronger since the highlighting of those found elements will be
3129properly preserved from then on. But for such multiline elements to
3130be found reliably, you will need to either manually put the
3131@code{font-lock-multiline} property from some appropriate piece of
3132code run before Font Lock, or hook into
3133@code{font-lock-fontify-region-function} to manually extend the chunks
3134of text that Font Lock highlights so they never start or stop in the
3135middle of a multiline element.
3136
3073@node Region to Fontify 3137@node Region to Fontify
3074@subsubsection Region to Fontify after a Buffer Change 3138@subsubsection Region to Fontify after a Buffer Change
3075 3139