summaryrefslogtreecommitdiff
path: root/devdocs/elisp/customizing-keywords.html
blob: cd16a2df98165b52b27dd84f0adc1a43134c4d39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 <h4 class="subsection">Customizing Search-Based Fontification</h4> <p>You can use <code>font-lock-add-keywords</code> to add additional search-based fontification rules to a major mode, and <code>font-lock-remove-keywords</code> to remove rules. </p> <dl> <dt id="font-lock-add-keywords">Function: <strong>font-lock-add-keywords</strong> <em>mode keywords &amp;optional how</em>
</dt> <dd>
<p>This function adds highlighting <var>keywords</var>, for the current buffer or for major mode <var>mode</var>. The argument <var>keywords</var> should be a list with the same format as the variable <code>font-lock-keywords</code>. </p> <p>If <var>mode</var> is a symbol which is a major mode command name, such as <code>c-mode</code>, the effect is that enabling Font Lock mode in <var>mode</var> will add <var>keywords</var> to <code>font-lock-keywords</code>. Calling with a non-<code>nil</code> value of <var>mode</var> is correct only in your <samp>~/.emacs</samp> file. </p> <p>If <var>mode</var> is <code>nil</code>, this function adds <var>keywords</var> to <code>font-lock-keywords</code> in the current buffer. This way of calling <code>font-lock-add-keywords</code> is usually used in mode hook functions. </p> <p>By default, <var>keywords</var> are added at the beginning of <code>font-lock-keywords</code>. If the optional argument <var>how</var> is <code>set</code>, they are used to replace the value of <code>font-lock-keywords</code>. If <var>how</var> is any other non-<code>nil</code> value, they are added at the end of <code>font-lock-keywords</code>. </p> <p>Some modes provide specialized support you can use in additional highlighting patterns. See the variables <code>c-font-lock-extra-types</code>, <code>c++-font-lock-extra-types</code>, and <code>java-font-lock-extra-types</code>, for example. </p> <p><strong>Warning:</strong> Major mode commands must not call <code>font-lock-add-keywords</code> under any circumstances, either directly or indirectly, except through their mode hooks. (Doing so would lead to incorrect behavior for some minor modes.) They should set up their rules for search-based fontification by setting <code>font-lock-keywords</code>. </p>
</dd>
</dl> <dl> <dt id="font-lock-remove-keywords">Function: <strong>font-lock-remove-keywords</strong> <em>mode keywords</em>
</dt> <dd><p>This function removes <var>keywords</var> from <code>font-lock-keywords</code> for the current buffer or for major mode <var>mode</var>. As in <code>font-lock-add-keywords</code>, <var>mode</var> should be a major mode command name or <code>nil</code>. All the caveats and requirements for <code>font-lock-add-keywords</code> apply here too. The argument <var>keywords</var> must exactly match the one used by the corresponding <code>font-lock-add-keywords</code>. </p></dd>
</dl> <p>For example, the following code adds two fontification patterns for C mode: one to fontify the word ‘<samp>FIXME</samp>’, even in comments, and another to fontify the words ‘<samp>and</samp>’, ‘<samp>or</samp>’ and ‘<samp>not</samp>’ as keywords. </p> <div class="example"> <pre class="example">(font-lock-add-keywords 'c-mode
 '(("\\&lt;\\(FIXME\\):" 1 font-lock-warning-face prepend)
   ("\\&lt;\\(and\\|or\\|not\\)\\&gt;" . font-lock-keyword-face)))
</pre>
</div> <p>This example affects only C mode proper. To add the same patterns to C mode <em>and</em> all modes derived from it, do this instead: </p> <div class="example"> <pre class="example">(add-hook 'c-mode-hook
 (lambda ()
  (font-lock-add-keywords nil
   '(("\\&lt;\\(FIXME\\):" 1 font-lock-warning-face prepend)
     ("\\&lt;\\(and\\|or\\|not\\)\\&gt;" .
      font-lock-keyword-face)))))
</pre>
</div><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 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/Customizing-Keywords.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Customizing-Keywords.html</a>
  </p>
</div>