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