comparison lispref/modes.texi @ 61377:d719a3a92aa5

(Search-based Fontification): Fix cross references. Use consistent terminology. Document anchored highlighting.
author Lute Kamstra <lute@gnu.org>
date Fri, 08 Apr 2005 09:28:08 +0000
parents 8ac9a1a8591f
children 3c8535295b32 02f1dbc4a199
comparison
equal deleted inserted replaced
61376:15f4c9e1a9b2 61377:d719a3a92aa5
1 @c -*-texinfo-*- 1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual. 2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999,
4 @c 2003, 2004, 2005 Free Software Foundation, Inc. 4 @c 2003, 2004, 2005 Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions. 5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/modes 6 @setfilename ../info/modes
7 @node Modes, Documentation, Keymaps, Top 7 @node Modes, Documentation, Keymaps, Top
8 @chapter Major and Minor Modes 8 @chapter Major and Minor Modes
2109 certain cases of text, and how to highlight those cases. Font Lock mode 2109 certain cases of text, and how to highlight those cases. Font Lock mode
2110 processes the elements of @code{font-lock-keywords} one by one, and for 2110 processes the elements of @code{font-lock-keywords} one by one, and for
2111 each element, it finds and handles all matches. Ordinarily, once 2111 each element, it finds and handles all matches. Ordinarily, once
2112 part of the text has been fontified already, this cannot be overridden 2112 part of the text has been fontified already, this cannot be overridden
2113 by a subsequent match in the same text; but you can specify different 2113 by a subsequent match in the same text; but you can specify different
2114 behavior using the @var{override} element of a @var{highlighter}. 2114 behavior using the @var{override} element of a @var{subexp-highlighter}.
2115 2115
2116 Each element of @code{font-lock-keywords} should have one of these 2116 Each element of @code{font-lock-keywords} should have one of these
2117 forms: 2117 forms:
2118 2118
2119 @table @code 2119 @table @code
2120 @item @var{regexp} 2120 @item @var{regexp}
2121 Highlight all matches for @var{regexp} using 2121 Highlight all matches for @var{regexp} using
2122 @code{font-lock-keyword-face}. For example, 2122 @code{font-lock-keyword-face}. For example,
2123 2123
2124 @example 2124 @example
2125 ;; @r{Highlight discrete occurrences of @samp{foo}} 2125 ;; @r{Highlight occurrences of the word @samp{foo}}
2126 ;; @r{using @code{font-lock-keyword-face}.} 2126 ;; @r{using @code{font-lock-keyword-face}.}
2127 "\\<foo\\>" 2127 "\\<foo\\>"
2128 @end example 2128 @end example
2129 2129
2130 The function @code{regexp-opt} (@pxref{Syntax of Regexps}) is useful for 2130 The function @code{regexp-opt} (@pxref{Regexp Functions}) is useful
2131 calculating optimal regular expressions to match a number of different 2131 for calculating optimal regular expressions to match a number of
2132 keywords. 2132 different keywords.
2133 2133
2134 @item @var{function} 2134 @item @var{function}
2135 Find text by calling @var{function}, and highlight the matches 2135 Find text by calling @var{function}, and highlight the matches
2136 it finds using @code{font-lock-keyword-face}. 2136 it finds using @code{font-lock-keyword-face}.
2137 2137
2144 Fontification will call @var{function} repeatedly with the same limit, 2144 Fontification will call @var{function} repeatedly with the same limit,
2145 and with point where the previous invocation left it, until 2145 and with point where the previous invocation left it, until
2146 @var{function} fails. On failure, @var{function} need not reset point 2146 @var{function} fails. On failure, @var{function} need not reset point
2147 in any particular way. 2147 in any particular way.
2148 2148
2149 @item (@var{matcher} . @var{match}) 2149 @item (@var{matcher} . @var{subexp})
2150 In this kind of element, @var{matcher} is either a regular 2150 In this kind of element, @var{matcher} is either a regular
2151 expression or a function, as described above. The @sc{cdr}, 2151 expression or a function, as described above. The @sc{cdr},
2152 @var{match}, specifies which subexpression of @var{matcher} should be 2152 @var{subexp}, specifies which subexpression of @var{matcher} should be
2153 highlighted (instead of the entire text that @var{matcher} matched). 2153 highlighted (instead of the entire text that @var{matcher} matched).
2154 2154
2155 @example 2155 @example
2156 ;; @r{Highlight the @samp{bar} in each occurrence of @samp{fubar},} 2156 ;; @r{Highlight the @samp{bar} in each occurrence of @samp{fubar},}
2157 ;; @r{using @code{font-lock-keyword-face}.} 2157 ;; @r{using @code{font-lock-keyword-face}.}
2158 ("fu\\(bar\\)" . 1) 2158 ("fu\\(bar\\)" . 1)
2159 @end example 2159 @end example
2160 2160
2161 If you use @code{regexp-opt} to produce the regular expression 2161 If you use @code{regexp-opt} to produce the regular expression
2162 @var{matcher}, then you can use @code{regexp-opt-depth} (@pxref{Syntax 2162 @var{matcher}, then you can use @code{regexp-opt-depth} (@pxref{Regexp
2163 of Regexps}) to calculate the value for @var{match}. 2163 Functions}) to calculate the value for @var{subexp}.
2164 2164
2165 @item (@var{matcher} . @var{facespec}) 2165 @item (@var{matcher} . @var{facespec})
2166 In this kind of element, @var{facespec} is an object which specifies 2166 In this kind of element, @var{facespec} is an object which specifies
2167 the face variable to use for highlighting. In the simplest case, it 2167 the face variable to use for highlighting. In the simplest case, it
2168 is a Lisp variable (a symbol), whose value should be a face name. 2168 is a Lisp variable (a symbol), whose value should be a face name.
2171 ;; @r{Highlight occurrences of @samp{fubar},} 2171 ;; @r{Highlight occurrences of @samp{fubar},}
2172 ;; @r{using the face which is the value of @code{fubar-face}.} 2172 ;; @r{using the face which is the value of @code{fubar-face}.}
2173 ("fubar" . fubar-face) 2173 ("fubar" . fubar-face)
2174 @end example 2174 @end example
2175 2175
2176 However, @var{facespec} can also be a list of the form 2176 However, @var{facespec} can also be a list of the form:
2177 2177
2178 @example 2178 @example
2179 (face @var{face} @var{prop1} @var{val1} @var{prop2} @var{val2}@dots{}) 2179 (face @var{face} @var{prop1} @var{val1} @var{prop2} @var{val2}@dots{})
2180 @end example 2180 @end example
2181 2181
2182 to specify various text properties to put on the text that matches. 2182 to specify the face @var{face} and various additional text properties
2183 If you do this, be sure to add the other text property names that you 2183 to put on the text that matches. If you do this, be sure to add the
2184 set in this way to the value of @code{font-lock-extra-managed-props} 2184 other text property names that you set in this way to the value of
2185 so that the properties will also be cleared out when they are no longer 2185 @code{font-lock-extra-managed-props} so that the properties will also
2186 appropriate. 2186 be cleared out when they are no longer appropriate. Alternatively,
2187 2187 you can set the variable @code{font-lock-unfontify-region-function} to
2188 @item (@var{matcher} . @var{highlighter}) 2188 a function that clears these properties.
2189 In this kind of element, @var{highlighter} is a list 2189
2190 @item (@var{matcher} . @var{subexp-highlighter})
2191 In this kind of element, @var{subexp-highlighter} is a list
2190 which specifies how to highlight matches found by @var{matcher}. 2192 which specifies how to highlight matches found by @var{matcher}.
2191 It has the form 2193 It has the form:
2192 2194
2193 @example 2195 @example
2194 (@var{subexp} @var{facespec} @var{override} @var{laxmatch}) 2196 (@var{subexp} @var{facespec} [[@var{override} [@var{laxmatch}]])
2195 @end example 2197 @end example
2196 2198
2197 The @sc{car}, @var{subexp}, is an integer specifying which subexpression 2199 The @sc{car}, @var{subexp}, is an integer specifying which subexpression
2198 of the match to fontify (0 means the entire matching text). The second 2200 of the match to fontify (0 means the entire matching text). The second
2199 subelement, @var{facespec}, specifies the face, as described above. 2201 subelement, @var{facespec}, specifies the face, as described above.
2200 2202
2201 The last two values in @var{highlighter}, @var{override} and 2203 The last two values in @var{subexp-highlighter}, @var{override} and
2202 @var{laxmatch}, are flags. If @var{override} is @code{t}, this 2204 @var{laxmatch}, are optional flags. If @var{override} is @code{t},
2203 element can override existing fontification made by previous elements 2205 this element can override existing fontification made by previous
2204 of @code{font-lock-keywords}. If it is @code{keep}, then each 2206 elements of @code{font-lock-keywords}. If it is @code{keep}, then
2205 character is fontified if it has not been fontified already by some 2207 each character is fontified if it has not been fontified already by
2206 other element. If it is @code{prepend}, the face specified by 2208 some other element. If it is @code{prepend}, the face specified by
2207 @var{facespec} is added to the beginning of the @code{font-lock-face} 2209 @var{facespec} is added to the beginning of the @code{font-lock-face}
2208 property. If it is @code{append}, the face is added to the end of the 2210 property. If it is @code{append}, the face is added to the end of the
2209 @code{font-lock-face} property. 2211 @code{font-lock-face} property.
2210 2212
2211 If @var{laxmatch} is non-@code{nil}, it means there should be no error 2213 If @var{laxmatch} is non-@code{nil}, it means there should be no error
2217 terminates search-based fontification. 2219 terminates search-based fontification.
2218 2220
2219 Here are some examples of elements of this kind, and what they do: 2221 Here are some examples of elements of this kind, and what they do:
2220 2222
2221 @smallexample 2223 @smallexample
2222 ;; @r{Highlight occurrences of either @samp{foo} or @samp{bar},} 2224 ;; @r{Highlight occurrences of either @samp{foo} or @samp{bar}, using}
2223 ;; @r{using @code{foo-bar-face}, even if they have already been highlighted.} 2225 ;; @r{@code{foo-bar-face}, even if they have already been highlighted.}
2224 ;; @r{@code{foo-bar-face} should be a variable whose value is a face.} 2226 ;; @r{@code{foo-bar-face} should be a variable whose value is a face.}
2225 ("foo\\|bar" 0 foo-bar-face t) 2227 ("foo\\|bar" 0 foo-bar-face t)
2226 2228
2227 ;; @r{Highlight the first subexpression within each occurrence} 2229 ;; @r{Highlight the first subexpression within each occurrence}
2228 ;; @r{that the function @code{fubar-match} finds,} 2230 ;; @r{that the function @code{fubar-match} finds,}
2229 ;; @r{using the face which is the value of @code{fubar-face}.} 2231 ;; @r{using the face which is the value of @code{fubar-face}.}
2230 (fubar-match 1 fubar-face) 2232 (fubar-match 1 fubar-face)
2231 @end smallexample 2233 @end smallexample
2232 2234
2235 @item (@var{matcher} . @var{anchored-highlighter})
2236 In this kind of element, @var{anchored-highlighter} specifies how to
2237 highlight text that follows a match found by @var{matcher}. So a
2238 match found by @var{matcher} acts as the anchor for further searches
2239 specified by @var{anchored-highlighter}. @var{anchored-highlighter}
2240 is a list of the following form:
2241
2242 @example
2243 (@var{anchored-matcher} @var{pre-form} @var{post-form}
2244 @var{subexp-highlighters}@dots{})
2245 @end example
2246
2247 Here, @var{anchored-matcher}, like @var{matcher}, is either a regular
2248 expression or a function. After a match of @var{matcher} is found,
2249 point is at the end of the match. Now, Font Lock evaluates the form
2250 @var{pre-form}. Then it searches for matches of
2251 @var{anchored-matcher} and uses @var{subexp-highlighters} to highlight
2252 these. A @var{subexp-highlighter} is as described above. Finally,
2253 Font Lock evaluates @var{post-form}.
2254
2255 The forms @var{pre-form} and @var{post-form} can be used to initialize
2256 before, and cleanup after, @var{anchored-matcher} is used. Typically,
2257 @var{pre-form} is used to move point to some position relative to the
2258 match of @var{matcher}, before starting with @var{anchored-matcher}.
2259 @var{post-form} might be used to move back, before resuming with
2260 @var{matcher}.
2261
2262 After Font Lock evaluates @var{pre-form}, it does not search for
2263 @var{anchored-matcher} beyond the end of the line. However, if
2264 @var{pre-form} returns a buffer position that is greater than the
2265 position of point after @var{pre-form} is evaluated, then the position
2266 returned by @var{pre-form} is used as the limit of the search instead.
2267 It is generally a bad idea to return a position greater than the end
2268 of the line; in other words, the @var{anchored-matcher} search should
2269 not span lines.
2270
2271 For example,
2272
2273 @smallexample
2274 ;; @r{Highlight occurrences of the word @samp{item} following}
2275 ;; @r{an occurrence of the word @samp{anchor} (on the same line)}
2276 ;; @r{in the value of @code{item-face}.}
2277 ("\\<anchor\\>" "\\<item\\>" nil nil (0 item-face))
2278 @end smallexample
2279
2280 Here, @var{pre-form} and @var{post-form} are @code{nil}. Therefore
2281 searching for @samp{item} starts at the end of the match of
2282 @samp{anchor}, and searching for subsequent instances of @samp{anchor}
2283 resumes from where searching for @samp{item} concluded.
2284
2233 @item (@var{matcher} @var{highlighters}@dots{}) 2285 @item (@var{matcher} @var{highlighters}@dots{})
2234 This sort of element specifies several @var{highlighter} lists for a 2286 This sort of element specifies several @var{highlighter} lists for a
2235 single @var{matcher}. In order for this to be useful, each 2287 single @var{matcher}. A @var{highlighter} list can be of the type
2236 @var{highlighter} should have a different value of @var{subexp}; that is, 2288 @var{subexp-highlighter} or @var{anchored-highlighter} as described
2237 each one should apply to a different subexpression of @var{matcher}. 2289 above.
2238 2290
2239 @ignore 2291 For example,
2240 @item (@var{matcher} . @var{anchored}) 2292
2241 In this kind of element, @var{anchored} acts much like a 2293 @smallexample
2242 @var{highlighter}, but it is more complex and can specify multiple 2294 ;; @r{Highlight occurrences of the word @samp{anchor} in the value}
2243 successive searches. 2295 ;; @r{of @code{anchor-face}, and subsequent occurrences of the word}
2244 2296 ;; @r{@samp{item} (on the same line) in the value of @code{item-face}.}
2245 For highlighting single items, typically only @var{highlighter} is 2297 ("\\<anchor\\>" (0 anchor-face)
2246 required. However, if an item or (typically) items are to be 2298 ("\\<item\\>" nil nil (0 item-face)))
2247 highlighted following the instance of another item (the anchor) then 2299 @end smallexample
2248 @var{anchored} may be required.
2249
2250 It has this format:
2251
2252 @example
2253 (@var{submatcher} @var{pre-match-form} @var{post-match-form} @var{highlighters}@dots{})
2254 @end example
2255
2256 @c I can't parse this text -- rms
2257 where @var{submatcher} is much like @var{matcher}, with one
2258 exception---see below. @var{pre-match-form} and @var{post-match-form}
2259 are evaluated before the first, and after the last, instance
2260 @var{anchored}'s @var{submatcher} is used. Therefore they can be used
2261 to initialize before, and cleanup after, @var{submatcher} is used.
2262 Typically, @var{pre-match-form} is used to move to some position
2263 relative to the original @var{submatcher}, before starting with
2264 @var{anchored}'s @var{submatcher}. @var{post-match-form} might be used
2265 to move, before resuming with @var{anchored}'s parent's @var{matcher}.
2266
2267 For example, an element of the form highlights (if not already highlighted):
2268
2269 @example
2270 ("\\<anchor\\>" (0 anchor-face) ("\\<item\\>" nil nil (0 item-face)))
2271 @end example
2272
2273 Discrete occurrences of @samp{anchor} in the value of
2274 @code{anchor-face}, and subsequent discrete occurrences of @samp{item}
2275 (on the same line) in the value of @code{item-face}. (Here
2276 @var{pre-match-form} and @var{post-match-form} are @code{nil}.
2277 Therefore @samp{item} is initially searched for starting from the end of
2278 the match of @samp{anchor}, and searching for subsequent instance of
2279 @samp{anchor} resumes from where searching for @samp{item} concluded.)
2280
2281 The above-mentioned exception is as follows. The limit of the
2282 @var{submatcher} search defaults to the end of the line after
2283 @var{pre-match-form} is evaluated. However, if @var{pre-match-form}
2284 returns a position greater than the position after @var{pre-match-form}
2285 is evaluated, that position is used as the limit of the search. It is
2286 generally a bad idea to return a position greater than the end of the
2287 line; in other words, the @var{submatcher} search should not span lines.
2288
2289 @item (@var{matcher} @var{highlighters-or-anchoreds} ...)
2290 @end ignore
2291 2300
2292 @item (eval . @var{form}) 2301 @item (eval . @var{form})
2293 Here @var{form} is an expression to be evaluated the first time 2302 Here @var{form} is an expression to be evaluated the first time
2294 this value of @code{font-lock-keywords} is used in a buffer. 2303 this value of @code{font-lock-keywords} is used in a buffer.
2295 Its value should have one of the forms described in this table. 2304 Its value should have one of the forms described in this table.
2299 to match text which spans lines; this does not work reliably. While 2308 to match text which spans lines; this does not work reliably. While
2300 @code{font-lock-fontify-buffer} handles multi-line patterns correctly, 2309 @code{font-lock-fontify-buffer} handles multi-line patterns correctly,
2301 updating when you edit the buffer does not, since it considers text one 2310 updating when you edit the buffer does not, since it considers text one
2302 line at a time. If you have patterns that typically only span one 2311 line at a time. If you have patterns that typically only span one
2303 line but can occasionally span two or three, such as 2312 line but can occasionally span two or three, such as
2304 @samp{<title>...</title>}, you can ask font-lock to be more careful by 2313 @samp{<title>...</title>}, you can ask Font Lock to be more careful by
2305 setting @code{font-lock-multiline} to @code{t}. But it still will not 2314 setting @code{font-lock-multiline} to @code{t}. But it still will not
2306 work in all cases. 2315 work in all cases.
2307 2316
2308 @node Other Font Lock Variables 2317 @node Other Font Lock Variables
2309 @subsection Other Font Lock Variables 2318 @subsection Other Font Lock Variables