1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<h4 class="subsection">Font Lock Basics</h4> <p>The Font Lock functionality is based on several basic functions. Each of these calls the function specified by the corresponding variable. This indirection allows major and minor modes to modify the way fontification works in the buffers of that mode, and even use the Font Lock mechanisms for features that have nothing to do with fontification. (This is why the description below says “should” when it describes what the functions do: the mode can customize the values of the corresponding variables to do something entirely different.) The variables mentioned below are described in <a href="other-font-lock-variables">Other Font Lock Variables</a>. </p> <dl compact> <dt>
<code>font-lock-fontify-buffer</code> </dt> <dd>
<p>This function should fontify the current buffer’s accessible portion, by calling the function specified by <code>font-lock-fontify-buffer-function</code>. </p> </dd> <dt>
<code>font-lock-unfontify-buffer</code> </dt> <dd>
<p>Used when turning Font Lock off to remove the fontification. Calls the function specified by <code>font-lock-unfontify-buffer-function</code>. </p> </dd> <dt>
<code>font-lock-fontify-region beg end &optional loudly</code> </dt> <dd>
<p>Should fontify the region between <var>beg</var> and <var>end</var>. If <var>loudly</var> is non-<code>nil</code>, should display status messages while fontifying. Calls the function specified by <code>font-lock-fontify-region-function</code>. </p> </dd> <dt>
<code>font-lock-unfontify-region beg end</code> </dt> <dd>
<p>Should remove fontification from the region between <var>beg</var> and <var>end</var>. Calls the function specified by <code>font-lock-unfontify-region-function</code>. </p> </dd> <dt>
<code>font-lock-flush &optional beg end</code> </dt> <dd>
<p>This function should mark the fontification of the region between <var>beg</var> and <var>end</var> as outdated. If not specified or <code>nil</code>, <var>beg</var> and <var>end</var> default to the beginning and end of the buffer’s accessible portion. Calls the function specified by <code>font-lock-flush-function</code>. </p> </dd> <dt>
<code>font-lock-ensure &optional beg end</code> </dt> <dd>
<p>This function should make sure the region between <var>beg</var> and <var>end</var> has been fontified. The optional arguments <var>beg</var> and <var>end</var> default to the beginning and the end of the buffer’s accessible portion. Calls the function specified by <code>font-lock-ensure-function</code>. </p> </dd> <dt>
<code>font-lock-debug-fontify</code> </dt> <dd><p>This is a convenience command meant to be used when developing font locking for a mode, and should not be called from Lisp code. It recomputes all the relevant variables and then calls <code>font-lock-fontify-region</code> on the entire buffer. </p></dd> </dl> <p>There are several variables that control how Font Lock mode highlights text. But major modes should not set any of these variables directly. Instead, they should set <code>font-lock-defaults</code> as a buffer-local variable. The value assigned to this variable is used, if and when Font Lock mode is enabled, to set all the other variables. </p> <dl> <dt id="font-lock-defaults">Variable: <strong>font-lock-defaults</strong>
</dt> <dd>
<p>This variable is set by modes to specify how to fontify text in that mode. It automatically becomes buffer-local when set. If its value is <code>nil</code>, Font Lock mode does no highlighting, and you can use the ‘<samp>Faces</samp>’ menu (under ‘<samp>Edit</samp>’ and then ‘<samp>Text Properties</samp>’ in the menu bar) to assign faces explicitly to text in the buffer. </p> <p>If non-<code>nil</code>, the value should look like this: </p> <div class="example"> <pre class="example">(<var>keywords</var> [<var>keywords-only</var> [<var>case-fold</var>
[<var>syntax-alist</var> <var>other-vars</var>…]]])
</pre>
</div> <p>The first element, <var>keywords</var>, indirectly specifies the value of <code>font-lock-keywords</code> which directs search-based fontification. It can be a symbol, a variable or a function whose value is the list to use for <code>font-lock-keywords</code>. It can also be a list of several such symbols, one for each possible level of fontification. The first symbol specifies the ‘<samp>mode default</samp>’ level of fontification, the next symbol level 1 fontification, the next level 2, and so on. The ‘<samp>mode default</samp>’ level is normally the same as level 1. It is used when <code>font-lock-maximum-decoration</code> has a <code>nil</code> value. See <a href="levels-of-font-lock">Levels of Font Lock</a>. </p> <p>The second element, <var>keywords-only</var>, specifies the value of the variable <code>font-lock-keywords-only</code>. If this is omitted or <code>nil</code>, syntactic fontification (of strings and comments) is also performed. If this is non-<code>nil</code>, syntactic fontification is not performed. See <a href="syntactic-font-lock">Syntactic Font Lock</a>. </p> <p>The third element, <var>case-fold</var>, specifies the value of <code>font-lock-keywords-case-fold-search</code>. If it is non-<code>nil</code>, Font Lock mode ignores case during search-based fontification. </p> <p>If the fourth element, <var>syntax-alist</var>, is non-<code>nil</code>, it should be a list of cons cells of the form <code>(<var>char-or-string</var>
. <var>string</var>)</code>. These are used to set up a syntax table for syntactic fontification; the resulting syntax table is stored in <code>font-lock-syntax-table</code>. If <var>syntax-alist</var> is omitted or <code>nil</code>, syntactic fontification uses the syntax table returned by the <code>syntax-table</code> function. See <a href="syntax-table-functions">Syntax Table Functions</a>. </p> <p>All the remaining elements (if any) are collectively called <var>other-vars</var>. Each of these elements should have the form <code>(<var>variable</var> . <var>value</var>)</code>—which means, make <var>variable</var> buffer-local and then set it to <var>value</var>. You can use these <var>other-vars</var> to set other variables that affect fontification, aside from those you can control with the first five elements. See <a href="other-font-lock-variables">Other Font Lock Variables</a>. </p>
</dd>
</dl> <p>If your mode fontifies text explicitly by adding <code>font-lock-face</code> properties, it can specify <code>(nil t)</code> for <code>font-lock-defaults</code> to turn off all automatic fontification. However, this is not required; it is possible to fontify some things using <code>font-lock-face</code> properties and set up automatic fontification for other parts of the text. </p><div class="_attribution">
<p class="_attribution-p">
Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
<a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Font-Lock-Basics.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Font-Lock-Basics.html</a>
</p>
</div>
|