14.13 Interactive Highlighting
Highlight Changes mode is a minor mode that highlights the parts of the buffer that were changed most recently, by giving that text a different face. To enable or disable Highlight Changes mode, use M-x highlight-changes-mode
.
Hi Lock mode is a minor mode that highlights text that matches regular expressions you specify. For example, you can use it to highlight all the references to a certain variable in a program source file, highlight certain parts in a voluminous output of some program, or highlight certain names in an article. To enable or disable Hi Lock mode, use the command M-x hi-lock-mode
. To enable Hi Lock mode for all buffers, use M-x global-hi-lock-mode
or place (global-hi-lock-mode 1)
in your .emacs
file.
Hi Lock mode works like Font Lock mode (see Font Lock), except that you specify explicitly the regular expressions to highlight. You can control them with the following commands. (The key bindings below that begin with C-x w
are deprecated in favor of the global M-s h
bindings, and will be removed in some future Emacs version.)
M-s h r regexp RET face RET
β
C-x w h regexp RET face RET
β
Highlight text that matches regexp
using face face
(highlight-regexp
). The highlighting will remain as long as the buffer is loaded. For example, to highlight all occurrences of the word βwhim" using the default face (a yellow background), type M-s h r whim RET RET
. Any face can be used for highlighting, Hi Lock provides several of its own and these are pre-loaded into a list of default values. While being prompted for a face use M-n
and M-p
to cycle through them. A prefix numeric argument limits the highlighting to the corresponding subexpression.
Setting the option hi-lock-auto-select-face
to a non-nil
value causes this command (and other Hi Lock commands that read faces) to automatically choose the next face from the default list without prompting.
You can use this command multiple times, specifying various regular expressions to highlight in different ways.
M-s h u regexp RET
β
C-x w r regexp RET
β
Unhighlight regexp
(unhighlight-regexp
). If you invoke this from the menu, you select the expression to unhighlight from a list. If you invoke this from the keyboard, you use the minibuffer. It will show the most recently added regular expression; use M-n
to show the next older expression and M-p
to select the next newer expression. (You can also type the expression by hand, with completion.) When the expression you want to unhighlight appears in the minibuffer, press RET
to exit the minibuffer and unhighlight it.
M-s h l regexp RET face RET
β
C-x w l regexp RET face RET
β
Highlight entire lines containing a match for regexp
, using face face
(highlight-lines-matching-regexp
).
M-s h p phrase RET face RET
β
C-x w p phrase RET face RET
β
Highlight matches of phrase
, using face face
(highlight-phrase
). phrase
can be any regexp, but spaces will be replaced by matches to whitespace and initial lower-case letters will become case insensitive.
M-s h .
β
C-x w .
β
Highlight the symbol found near point, using the next available face (highlight-symbol-at-point
).
M-s h w
β
C-x w b
β
Insert all the current highlighting regexp/face pairs into the buffer at point, with comment delimiters to prevent them from changing your program. (This key binding runs the hi-lock-write-interactive-patterns
command.)
These patterns are extracted from the comments, if appropriate, if you invoke M-x hi-lock-find-patterns
, or if you visit the file while Hi Lock mode is enabled (since that runs hi-lock-find-patterns
).
M-s h f
β
C-x w i
β
Extract regexp/face pairs from comments in the current buffer (hi-lock-find-patterns
). Thus, you can enter patterns interactively with highlight-regexp
, store them into the file with hi-lock-write-interactive-patterns
, edit them (perhaps including different faces for different parenthesized parts of the match), and finally use this command (hi-lock-find-patterns
) to have Hi Lock highlight the edited patterns.
The variable hi-lock-file-patterns-policy
controls whether Hi Lock mode should automatically extract and highlight patterns found in a file when it is visited. Its value can be nil
(never highlight), ask
(query the user), or a function. If it is a function, hi-lock-find-patterns
calls it with the patterns as argument; if the function returns non-nil
, the patterns are used. The default is ask
. Note that patterns are always highlighted if you call hi-lock-find-patterns
directly, regardless of the value of this variable.
Also, hi-lock-find-patterns
does nothing if the current major modeβs symbol is a member of the list hi-lock-exclude-modes
.