summaryrefslogtreecommitdiff
path: root/devdocs/elisp/customizing-keywords.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/elisp/customizing-keywords.html
new repository
Diffstat (limited to 'devdocs/elisp/customizing-keywords.html')
-rw-r--r--devdocs/elisp/customizing-keywords.html23
1 files changed, 23 insertions, 0 deletions
diff --git a/devdocs/elisp/customizing-keywords.html b/devdocs/elisp/customizing-keywords.html
new file mode 100644
index 00000000..cd16a2df
--- /dev/null
+++ b/devdocs/elisp/customizing-keywords.html
@@ -0,0 +1,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>