Mercurial > emacs
comparison lispref/modes.texi @ 70289:608b16c56304
(Multiline Font Lock): Renamed from Multi line Font Lock Elements.
Much clarification.
(Font Lock Multiline, Region to Fontify): Much clarification.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 30 Apr 2006 02:35:35 +0000 |
parents | 3d36e736dc2a |
children | 76fee807f4c0 2ecafc6d5db7 |
comparison
equal
deleted
inserted
replaced
70288:45b7cad845ee | 70289:608b16c56304 |
---|---|
2343 contents can also specify how to fontify it. | 2343 contents can also specify how to fontify it. |
2344 * Faces for Font Lock:: Special faces specifically for Font Lock. | 2344 * Faces for Font Lock:: Special faces specifically for Font Lock. |
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 |
2353 @subsection Font Lock Basics | 2353 @subsection Font Lock Basics |
2354 | 2354 |
2623 Its value should have one of the forms described in this table. | 2623 Its value should have one of the forms described in this table. |
2624 @end table | 2624 @end 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} |
2627 to match text which spans lines; this does not work reliably. | 2627 to match text which spans lines; this does not work reliably. |
2628 For details, see @xref{Multi line Font Lock Elements}. | 2628 For details, see @xref{Multiline Font Lock}. |
2629 | 2629 |
2630 You can use @var{case-fold} in @code{font-lock-defaults} to specify | 2630 You can use @var{case-fold} in @code{font-lock-defaults} to specify |
2631 the value of @code{font-lock-keywords-case-fold-search} which says | 2631 the value of @code{font-lock-keywords-case-fold-search} which says |
2632 whether search-based fontification should be case-insensitive. | 2632 whether search-based fontification should be case-insensitive. |
2633 | 2633 |
2942 value returns @code{font-lock-comment-face} for comments and | 2942 value returns @code{font-lock-comment-face} for comments and |
2943 @code{font-lock-string-face} for strings. | 2943 @code{font-lock-string-face} for strings. |
2944 | 2944 |
2945 This can be used to highlighting different kinds of strings or | 2945 This can be used to highlighting different kinds of strings or |
2946 comments differently. It is also sometimes abused together with | 2946 comments 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 |
2948 multiple lines, but this is too obscure to document in this manual. | 2948 multiple lines, but this is too esoteric to document here. |
2949 | 2949 |
2950 Specify this variable using @var{other-vars} in | 2950 Specify this variable using @var{other-vars} in |
2951 @code{font-lock-defaults}. | 2951 @code{font-lock-defaults}. |
2952 @end defvar | 2952 @end defvar |
2953 | 2953 |
3013 | 3013 |
3014 Major modes normally set this variable with @var{other-vars} in | 3014 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 |
3022 Normally, Font Lock elements specified via @code{font-lock-keywords} | 3022 Normally, elements of @code{font-lock-keywords} should not match |
3023 should not match across multiple lines. If they do, Font Lock may | 3023 across multiple lines; that doesn't work reliably, because Font Lock |
3024 fail to highlight them properly. This is fundamentally due to the | 3024 usually scans just part of the buffer, and it can miss a multi-line |
3025 fact that Font Lock does not always look at the whole buffer at | 3025 construct that crosses the line boundary where the scan starts. (The |
3026 a time, for obvious performance reasons, and instead only looks | 3026 scan normally starts at the beginning of a line.) |
3027 at a small chunk at a time. In order for the highlight to be correct, | 3027 |
3028 a 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 | 3029 two aspects: correct @emph{identification} and correct |
3030 start and end chunks at the beginning resp. end of a line. | 3030 @emph{rehighlighting}. The first means that Font Lock finds all |
3031 | 3031 multiline constructs. The second means that Font Lock will correctly |
3032 In order for Font Lock to properly highlight elements that span | 3032 rehighlight all the relevant text when a multiline construct is |
3033 multiple lines, the package author has to ensure two things: correct | 3033 changed---for example, if some of the text that was previously part of |
3034 @emph{discovery} and correct @emph{re}highlighting. The first ensures | 3034 a multiline construct ceases to be part of it. The two aspects are |
3035 that Font Lock finds all multiline elements. The second ensures that | 3035 closely related, and often getting one of them to work will appear to |
3036 Font Lock will correctly re-highlight all the relevant text when | 3036 make the other also work. However, for reliable results you must |
3037 a multiline element is changed, e.g. causing some of the text that was | 3037 attend explicitly to both aspects. |
3038 previously part of a multiline element to not be part of it any more. | 3038 |
3039 The two aspects are closely related and often getting one of the two | 3039 There are two ways to ensure correct identification of multiline |
3040 to work will appear to make the other also work. But both aspects | 3040 constructs: |
3041 have to be taken care of for the multiline elements to be | 3041 |
3042 reliably highlighted. | |
3043 | |
3044 Correct @emph{re}highlighting of multiline elements can be done in the | |
3045 following ways: | |
3046 @itemize | 3042 @itemize |
3047 @item | 3043 @item |
3048 Apply the @code{font-lock-multiline} property to the element. | 3044 Place a @code{font-lock-multiline} or @code{jit-lock-defer-multiline} |
3049 This will ensure that the whole element will always be immediately | 3045 property on the construct when it is added to the buffer. |
3050 rehighlighted if any part of it is changed. This can sometimes be | 3046 @item |
3051 done automatically by setting the @code{font-lock-multiline} variable. | 3047 Use @code{font-lock-fontify-region-function} hook to extend the scan |
3052 @item | 3048 so that the scanned text never starts or ends in the middle of a |
3053 Rely on @code{jit-lock-contextually}. This will only rehighlight the | 3049 multiline construct. |
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 | 3050 @end itemize |
3065 | 3051 |
3066 Discovery 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 |
3069 Manually placing a @code{font-lock-multiline} or | 3056 Place a @code{font-lock-multiline} property on the construct. This |
3070 @code{jit-lock-defer-multiline} property on the element when it is | 3057 will rehighlight the whole construct if any part of it is changed. In |
3071 added to the buffer. | 3058 some cases you can do this automatically by setting the |
3072 @item | 3059 @code{font-lock-multiline} variable. |
3073 Using the @code{font-lock-fontify-region-function} hook to extend the | 3060 @item |
3074 highlighted chunks so that they never start or end in the middle of | 3061 Use @code{jit-lock-contextually}. This will only rehighlight the part |
3075 multiline element. | 3062 of the construct that follows the actual change, and will do it after |
3063 a short delay. This only works if the highlighting of the various | |
3064 parts of your multiline construct never depends on text in subsequent | |
3065 lines. Since @code{jit-lock-contextually} is activated by default, | |
3066 this can be an attractive solution. | |
3067 @item | |
3068 Place a @code{jit-lock-defer-multiline} property on the construct. | |
3069 This works only if @code{jit-lock-contextually} is used, but it can | |
3070 handle the case where highlighting depends on subsequent lines. | |
3071 @item | |
3076 @end itemize | 3072 @end itemize |
3077 | 3073 |
3078 @menu | 3074 @menu |
3079 * Font Lock Multiline:: Marking multiline chunks with a text property | 3075 * Font Lock Multiline:: Marking multiline chunks with a text property |
3080 * Region to Fontify:: Controlling which region gets refontified | 3076 * Region to Fontify:: Controlling which region gets refontified |
3082 @end menu | 3078 @end menu |
3083 | 3079 |
3084 @node Font Lock Multiline | 3080 @node Font Lock Multiline |
3085 @subsubsection Font Lock Multiline | 3081 @subsubsection Font Lock Multiline |
3086 | 3082 |
3087 In order to make it possible to properly @emph{re}highlight elements that | 3083 One way to ensure reliable rehighlighting of multiline Font Lock |
3088 span multiple lines, Font Lock obeys a special text property | 3084 constructs is to put on the text property @code{font-lock-multiline}. |
3089 @code{font-lock-multiline} which if non-@code{nil} indicates that this | 3085 It should be present and non-@code{nil} for text that is part of a |
3090 piece of text was highlighted as part of a multiline construct. | 3086 multiline construct. |
3091 So when Font Lock is asked to rehighlight a region, it first verifies | 3087 |
3092 the 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 |
3093 the middle of a piece of text marked with the | 3089 extends the boundaries of the range as necessary so that they do not |
3094 @code{font-lock-multiline} property. Immediately after that, it also | 3090 fall within text marked with the @code{font-lock-multiline} property. |
3095 erases all @code{font-lock-multiline} properties from the region it is | 3091 Then it removes any @code{font-lock-multiline} properties from the |
3096 about to highlight, so it is the responsability of the highlighting | 3092 range, and highlights it. The highlighting specification (mostly |
3097 specification (mostly @code{font-lock-keywords}) to make sure that | 3093 @code{font-lock-keywords}) must reinstall this property each time, |
3098 this property is re-added where needed so as to inform the next round | 3094 whenever it is appropriate. |
3099 of Font Locking of the presence of a multiline construct. | 3095 |
3100 | 3096 @strong{Warning:} don't use the @code{font-lock-multiline} property |
3101 It is important to understand that the @code{font-lock-multiline} | 3097 on large ranges of text, because that will make rehighlighting slow. |
3102 property should preferably only be used on Font Lock elements of | |
3103 moderate size: every time that text is modified within a multiline | |
3104 element (or nearby), the whole multiline element will be completely | |
3105 re-highlighted, so if its size is large, the time to font-lock may | |
3106 render editing painfully slow. | |
3107 | 3098 |
3108 @defvar font-lock-multiline | 3099 @defvar font-lock-multiline |
3109 If the @code{font-lock-multiline} variable is set to @code{t}, Font | 3100 If the @code{font-lock-multiline} variable is set to @code{t}, Font |
3110 Lock will try to automatically add the @code{font-lock-multiline} | 3101 Lock will try to add the @code{font-lock-multiline} property |
3111 property on the keywords that span several lines. This is no silver | 3102 automatically on multiline constructs. This is not a universal |
3112 bullet however since it slows down Font Lock somewhat, and may miss | 3103 solution, however, since it slows down Font Lock somewhat. It can |
3113 some cases or make the property larger or smaller than necessary, | 3104 miss some multiline constructs, or make the property larger or smaller |
3114 especially for keywords whose @var{MATCHER} is a function, in which | 3105 than necessary. |
3115 case the function needs to make sure that the submatch 0 covers the | 3106 |
3116 whole relevant multiline entity even if only a small subpart will | 3107 For elements whose @var{matcher} is a function, the function should |
3117 be highlighted. It is often just as easy to add the | 3108 ensure that submatch 0 covers the whole relevant multiline construct, |
3118 @code{font-lock-multiline} property by hand. | 3109 even if only a small subpart will be highlighted. It is often just as |
3119 @end defvar | 3110 easy to add the @code{font-lock-multiline} property by hand. |
3120 | 3111 @end defvar |
3121 As mentioned, this property is mostly intended to ensure proper | 3112 |
3122 @emph{re}fontification. It does not magically discover new | 3113 The @code{font-lock-multiline} property is meant to ensure proper |
3123 multiline elements. To discover new multiline elements, all that is | 3114 refontification; it does not automatically identify new multiline |
3124 required is that font-lock operate on large enough chunks at a time. | 3115 constructs. Identifying the requires that Font-Lock operate on large |
3125 This will happen by accident on many cases, which may give the | 3116 enough chunks at a time. This will happen by accident on many cases, |
3126 impression that multiline elements magically work. If you set the | 3117 which may give the impression that multiline constructs magically work. |
3127 @code{font-lock-multiline} variable, this impression will be even | 3118 If you set the @code{font-lock-multiline} variable non-@code{nil}, |
3128 stronger since the highlighting of those found elements will be | 3119 this impression will be even stronger, since the highlighting of those |
3129 properly preserved from then on. But for such multiline elements to | 3120 constructs which are found will be properly updated from then on. |
3130 be found reliably, you will need to either manually put the | 3121 But that does not work reliably. |
3131 @code{font-lock-multiline} property from some appropriate piece of | 3122 |
3132 code run before Font Lock, or hook into | 3123 To find multiline constructs reliably, you must either manually |
3133 @code{font-lock-fontify-region-function} to manually extend the chunks | 3124 place the @code{font-lock-multiline} property on the text before |
3134 of text that Font Lock highlights so they never start or stop in the | 3125 Font-Lock looks at it, or use |
3135 middle of a multiline element. | 3126 @code{font-lock-fontify-region-function}. |
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 |
3141 default the smallest sequence of whole lines that spans the change. | 3132 by default the smallest sequence of whole lines that spans the change. |
3142 While this works well most of the time, sometimes it doesn't---for | 3133 While this works well most of the time, sometimes it doesn't---for |
3143 example, when a buffer change has changed the syntactic meaning of text | 3134 example, when a change alters the syntactic meaning of text on an |
3144 on an earlier line. | 3135 earlier line. |
3145 | 3136 |
3146 You 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 |
3147 the following variables: | 3138 one the following variables: |
3148 | 3139 |
3149 @defvar font-lock-extend-region-function | 3140 @defvar font-lock-extend-region-function |
3150 This buffer-local variable is either @code{nil} or is a function that | 3141 This buffer-local variable is either @code{nil} or a function for |
3151 determines the region to fontify, which Emacs then calls after each | 3142 Font-Lock to call to determine the region to scan and fontify. |
3152 buffer change. | |
3153 | 3143 |
3154 The function is given three parameters, the standard @var{beg}, | 3144 The 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 |
3156 Hooks}). It should return either a cons of the beginning and end buffer | 3146 (@pxref{Change Hooks}). It should return either a cons of the |
3157 positions (in that order) of the region to fontify, or @code{nil} (which | 3147 beginning and end buffer positions (in that order) of the region to |
3158 directs the caller to fontify the default region). This function needs | 3148 fontify, or @code{nil} (which means choose the region in the standard |
3159 to preserve point, the match-data, and the current restriction. | 3149 way). This function needs to preserve point, the match-data, and the |
3160 The region it returns may start or end in the middle of a line. | 3150 current restriction. The region it returns may start or end in the |
3151 middle of a line. | |
3161 | 3152 |
3162 Since this function is called after every buffer change, it should be | 3153 Since this function is called after every buffer change, it should be |
3163 reasonably fast. | 3154 reasonably fast. |
3164 @end defvar | 3155 @end defvar |
3165 | 3156 |