1 ;;; hi-lock.el --- minor mode for interactive automatic highlighting
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: David M. Koppelman, koppel@ee.lsu.edu
7 ;; Keywords: faces, minor-mode, matching, display
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; With the hi-lock commands text matching interactively entered
29 ;; regexp's can be highlighted. For example, `M-x highlight-regexp
30 ;; RET clearly RET RET' will highlight all occurrences of `clearly'
31 ;; using a yellow background face. New occurrences of `clearly' will
32 ;; be highlighted as they are typed. `M-x unhighlight-regexp RET'
33 ;; will remove the highlighting. Any existing face can be used for
34 ;; highlighting and a set of appropriate faces is provided. The
35 ;; regexps can be written into the current buffer in a form that will
36 ;; be recognized the next time the corresponding file is read.
40 ;; In program source code highlight a variable to quickly see all
41 ;; places it is modified or referenced:
42 ;; M-x highlight-regexp ground_contact_switches_closed RET RET
44 ;; In a shell or other buffer that is showing lots of program
45 ;; output, highlight the parts of the output you're interested in:
46 ;; M-x highlight-regexp Total execution time [0-9]+ RET hi-blue-b RET
48 ;; In buffers displaying tables, highlight the lines you're interested in:
49 ;; M-x highlight-lines-matching-regexp January 2000 RET hi-black-b RET
51 ;; When writing text, highlight personal cliches. This can be
53 ;; M-x highlight-phrase as can be seen RET RET
57 ;; Put the following code in your .emacs file. This turns on
58 ;; hi-lock mode and adds a "Regexp Highlighting" entry
61 ;; (global-hi-lock-mode 1)
63 ;; You might also want to bind the hi-lock commands to more
64 ;; finger-friendly sequences:
66 ;; (define-key hi-lock-map "\C-z\C-h" 'highlight-lines-matching-regexp)
67 ;; (define-key hi-lock-map "\C-zi" 'hi-lock-find-patterns)
68 ;; (define-key hi-lock-map "\C-zh" 'highlight-regexp)
69 ;; (define-key hi-lock-map "\C-zp" 'highlight-phrase)
70 ;; (define-key hi-lock-map "\C-zr" 'unhighlight-regexp)
71 ;; (define-key hi-lock-map "\C-zb" 'hi-lock-write-interactive-patterns))
73 ;; See the documentation for hi-lock-mode `C-h f hi-lock-mode' for
74 ;; additional instructions.
76 ;; Sample file patterns:
78 ; Hi-lock: (("^;;; .*" (0 (quote hi-black-hb) t)))
79 ; Hi-lock: ( ("make-variable-buffer-\\(local\\)" (0 font-lock-keyword-face)(1 'italic append)))))
88 "Interactively add and remove font-lock patterns for highlighting text."
89 :link
'(custom-manual "(emacs)Highlight Interactively")
92 (defcustom hi-lock-file-patterns-range
10000
93 "Limit of search in a buffer for hi-lock patterns.
94 When a file is visited and hi-lock mode is on patterns starting
95 up to this limit are added to font-lock's patterns. See documentation
96 of functions `hi-lock-mode' and `hi-lock-find-patterns'."
100 (defcustom hi-lock-exclude-modes
101 '(rmail-mode mime
/viewer-mode gnus-article-mode
)
102 "List of major modes in which hi-lock will not run.
103 For security reasons since font lock patterns can specify function
105 :type
'(repeat symbol
)
109 (defgroup hi-lock-faces nil
115 '((((min-colors 88) (background dark
))
116 (:background
"yellow1" :foreground
"black"))
117 (((background dark
)) (:background
"yellow" :foreground
"black"))
118 (((min-colors 88)) (:background
"yellow1"))
119 (t (:background
"yellow")))
120 "Default face for hi-lock mode."
121 :group
'hi-lock-faces
)
124 '((((background dark
)) (:background
"pink" :foreground
"black"))
125 (t (:background
"pink")))
126 "Face for hi-lock mode."
127 :group
'hi-lock-faces
)
130 '((((min-colors 88) (background dark
))
131 (:background
"green1" :foreground
"black"))
132 (((background dark
)) (:background
"green" :foreground
"black"))
133 (((min-colors 88)) (:background
"green1"))
134 (t (:background
"green")))
135 "Face for hi-lock mode."
136 :group
'hi-lock-faces
)
139 '((((background dark
)) (:background
"light blue" :foreground
"black"))
140 (t (:background
"light blue")))
141 "Face for hi-lock mode."
142 :group
'hi-lock-faces
)
145 '((t (:weight bold
)))
146 "Face for hi-lock mode."
147 :group
'hi-lock-faces
)
150 '((((min-colors 88)) (:weight bold
:foreground
"blue1"))
151 (t (:weight bold
:foreground
"blue")))
152 "Face for hi-lock mode."
153 :group
'hi-lock-faces
)
156 '((((min-colors 88)) (:weight bold
:foreground
"green1"))
157 (t (:weight bold
:foreground
"green")))
158 "Face for hi-lock mode."
159 :group
'hi-lock-faces
)
162 '((((min-colors 88)) (:weight bold
:foreground
"red1"))
163 (t (:weight bold
:foreground
"red")))
164 "Face for hi-lock mode."
165 :group
'hi-lock-faces
)
168 '((t (:weight bold
:height
1.67 :inherit variable-pitch
)))
169 "Face for hi-lock mode."
170 :group
'hi-lock-faces
)
172 (defvar hi-lock-file-patterns nil
173 "Patterns found in file for hi-lock. Should not be changed.")
175 (defvar hi-lock-interactive-patterns nil
176 "Patterns provided to hi-lock by user. Should not be changed.")
178 (defvar hi-lock-face-history
179 (list "hi-yellow" "hi-pink" "hi-green" "hi-blue" "hi-black-b"
180 "hi-blue-b" "hi-red-b" "hi-green-b" "hi-black-hb")
181 "History list of faces for hi-lock interactive functions.")
183 ;(dolist (f hi-lock-face-history) (unless (facep f) (error "%s not a face" f)))
185 (defvar hi-lock-regexp-history nil
186 "History of regexps used for interactive fontification.")
188 (defvar hi-lock-file-patterns-prefix
"Hi-lock"
189 "Regexp for finding hi-lock patterns at top of file.")
191 (defvar hi-lock-archaic-interface-message-used nil
192 "True if user alerted that global-hi-lock-mode is now the global switch.
193 Earlier versions of hi-lock used hi-lock-mode as the global switch,
194 the message is issued if it appears that hi-lock-mode is used assuming
195 that older functionality. This variable avoids multiple reminders.")
197 (defvar hi-lock-archaic-interface-deduce nil
198 "If non-nil, sometimes assume that hi-lock-mode means global-hi-lock-mode.
199 Assumption is made if hi-lock-mode used in the *scratch* buffer while
200 a library is being loaded.")
202 (make-variable-buffer-local 'hi-lock-interactive-patterns
)
203 (put 'hi-lock-interactive-patterns
'permanent-local t
)
204 (make-variable-buffer-local 'hi-lock-regexp-history
)
205 (put 'hi-lock-regexp-history
'permanent-local t
)
206 (make-variable-buffer-local 'hi-lock-file-patterns
)
207 (put 'hi-lock-file-patterns
'permanent-local t
)
209 (defvar hi-lock-menu
(make-sparse-keymap "Hi Lock")
210 "Menu for hi-lock mode.")
212 (define-key-after hi-lock-menu
[highlight-regexp
]
213 '(menu-item "Highlight Regexp..." highlight-regexp
214 :help
"Highlight text matching PATTERN (a regexp)."))
216 (define-key-after hi-lock-menu
[highlight-phrase
]
217 '(menu-item "Highlight Phrase..." highlight-phrase
218 :help
"Highlight text matching PATTERN (a regexp processed to match phrases)."))
220 (define-key-after hi-lock-menu
[highlight-lines-matching-regexp
]
221 '(menu-item "Highlight Lines..." highlight-lines-matching-regexp
222 :help
"Highlight lines containing match of PATTERN (a regexp).."))
224 (define-key-after hi-lock-menu
[unhighlight-regexp
]
225 '(menu-item "Remove Highlighting..." unhighlight-regexp
226 :help
"Remove previously entered highlighting pattern."
227 :enable hi-lock-interactive-patterns
))
229 (define-key-after hi-lock-menu
[hi-lock-write-interactive-patterns
]
230 '(menu-item "Patterns to Buffer" hi-lock-write-interactive-patterns
231 :help
"Insert interactively added REGEXPs into buffer at point."
232 :enable hi-lock-interactive-patterns
))
234 (define-key-after hi-lock-menu
[hi-lock-find-patterns
]
235 '(menu-item "Patterns from Buffer" hi-lock-find-patterns
236 :help
"Use patterns (if any) near top of buffer."))
238 (defvar hi-lock-map
(make-sparse-keymap "Hi Lock")
239 "Key map for hi-lock.")
241 (define-key hi-lock-map
"\C-xwi" 'hi-lock-find-patterns
)
242 (define-key hi-lock-map
"\C-xwl" 'highlight-lines-matching-regexp
)
243 (define-key hi-lock-map
"\C-xwp" 'highlight-phrase
)
244 (define-key hi-lock-map
"\C-xwh" 'highlight-regexp
)
245 (define-key hi-lock-map
"\C-xwr" 'unhighlight-regexp
)
246 (define-key hi-lock-map
"\C-xwb" 'hi-lock-write-interactive-patterns
)
252 (define-minor-mode hi-lock-mode
253 "Toggle minor mode for interactively adding font-lock highlighting patterns.
255 If ARG positive turn hi-lock on. Issuing a hi-lock command will also
256 turn hi-lock on; to turn hi-lock on in all buffers use
257 global-hi-lock-mode or in your .emacs file (global-hi-lock-mode 1).
258 When hi-lock is turned on, a \"Regexp Highlighting\" submenu is added
259 to the \"Edit\" menu. The commands in the submenu, which can be
260 called interactively, are:
262 \\[highlight-regexp] REGEXP FACE
263 Highlight matches of pattern REGEXP in current buffer with FACE.
265 \\[highlight-phrase] PHRASE FACE
266 Highlight matches of phrase PHRASE in current buffer with FACE.
267 (PHRASE can be any REGEXP, but spaces will be replaced by matches
268 to whitespace and initial lower-case letters will become case insensitive.)
270 \\[highlight-lines-matching-regexp] REGEXP FACE
271 Highlight lines containing matches of REGEXP in current buffer with FACE.
273 \\[unhighlight-regexp] REGEXP
274 Remove highlighting on matches of REGEXP in current buffer.
276 \\[hi-lock-write-interactive-patterns]
277 Write active REGEXPs into buffer as comments (if possible). They will
278 be read the next time file is loaded or when the \\[hi-lock-find-patterns] command
279 is issued. The inserted regexps are in the form of font lock keywords.
280 (See `font-lock-keywords') They may be edited and re-loaded with \\[hi-lock-find-patterns],
281 any valid `font-lock-keywords' form is acceptable.
283 \\[hi-lock-find-patterns]
284 Re-read patterns stored in buffer (in the format produced by \\[hi-lock-write-interactive-patterns]).
286 When hi-lock is started and if the mode is not excluded, the
287 beginning of the buffer is searched for lines of the form:
289 where FOO is a list of patterns. These are added to the font lock keywords
290 already present. The patterns must start before position (number
291 of characters into buffer) `hi-lock-file-patterns-range'. Patterns
294 is found. A mode is excluded if it's in the list `hi-lock-exclude-modes'."
299 (when (and (equal (buffer-name) "*scratch*")
301 (not (interactive-p))
302 (not hi-lock-archaic-interface-message-used
))
303 (setq hi-lock-archaic-interface-message-used t
)
304 (if hi-lock-archaic-interface-deduce
305 (global-hi-lock-mode hi-lock-mode
)
307 "Possible archaic use of (hi-lock-mode).
308 Use (global-hi-lock-mode 1) in .emacs to enable hi-lock for all buffers,
309 use (hi-lock-mode 1) for individual buffers. For compatibility with Emacs
310 versions before 22 use the following in your .emacs file:
312 (if (functionp 'global-hi-lock-mode)
313 (global-hi-lock-mode 1)
319 (unless font-lock-mode
(font-lock-mode 1))
320 (define-key-after menu-bar-edit-menu
[hi-lock
]
321 (cons "Regexp Highlighting" hi-lock-menu
))
322 (hi-lock-find-patterns)
323 (add-hook 'font-lock-mode-hook
'hi-lock-font-lock-hook nil t
))
325 (when (or hi-lock-interactive-patterns
326 hi-lock-file-patterns
)
327 (when hi-lock-interactive-patterns
328 (font-lock-remove-keywords nil hi-lock-interactive-patterns
)
329 (setq hi-lock-interactive-patterns nil
))
330 (when hi-lock-file-patterns
331 (font-lock-remove-keywords nil hi-lock-file-patterns
)
332 (setq hi-lock-file-patterns nil
))
334 (font-lock-fontify-buffer)))
335 (define-key-after menu-bar-edit-menu
[hi-lock
] nil
)
336 (remove-hook 'font-lock-mode-hook
'hi-lock-font-lock-hook t
)))
339 (define-global-minor-mode global-hi-lock-mode
340 hi-lock-mode turn-on-hi-lock-if-enabled
343 (defun turn-on-hi-lock-if-enabled ()
344 (setq hi-lock-archaic-interface-message-used t
)
345 (unless (memq major-mode hi-lock-exclude-modes
)
349 (defalias 'highlight-lines-matching-regexp
'hi-lock-line-face-buffer
)
351 (defun hi-lock-line-face-buffer (regexp &optional face
)
352 "Set face of all lines containing a match of REGEXP to FACE.
354 Interactively, prompt for REGEXP then FACE. Buffer-local history
355 list maintained for regexps, global history maintained for faces.
356 \\<minibuffer-local-map>Use \\[next-history-element] and \\[previous-history-element] to retrieve next or previous history item.
357 \(See info node `Minibuffer History')"
361 (read-from-minibuffer "Regexp to highlight line: "
362 (cons (or (car hi-lock-regexp-history
) "") 1 )
363 nil nil
'hi-lock-regexp-history
))
364 (hi-lock-read-face-name)))
365 (or (facep face
) (setq face
'hi-yellow
))
366 (unless hi-lock-mode
(hi-lock-mode 1))
368 ;; The \\(?:...\\) grouping construct ensures that a leading ^, +, * or ?
369 ;; or a trailing $ in REGEXP will be interpreted correctly.
370 (concat "^.*\\(?:" regexp
"\\).*$") face
))
374 (defalias 'highlight-regexp
'hi-lock-face-buffer
)
376 (defun hi-lock-face-buffer (regexp &optional face
)
377 "Set face of each match of REGEXP to FACE.
379 Interactively, prompt for REGEXP then FACE. Buffer-local history
380 list maintained for regexps, global history maintained for faces.
381 \\<minibuffer-local-map>Use \\[next-history-element] and \\[previous-history-element] to retrieve next or previous history item.
382 \(See info node `Minibuffer History')"
386 (read-from-minibuffer "Regexp to highlight: "
387 (cons (or (car hi-lock-regexp-history
) "") 1 )
388 nil nil
'hi-lock-regexp-history
))
389 (hi-lock-read-face-name)))
390 (or (facep face
) (setq face
'hi-yellow
))
391 (unless hi-lock-mode
(hi-lock-mode 1))
392 (hi-lock-set-pattern regexp face
))
395 (defalias 'highlight-phrase
'hi-lock-face-phrase-buffer
)
397 (defun hi-lock-face-phrase-buffer (regexp &optional face
)
398 "Set face of each match of phrase REGEXP to FACE.
400 Whitespace in REGEXP converted to arbitrary whitespace and initial
401 lower-case letters made case insensitive."
405 (hi-lock-process-phrase
406 (read-from-minibuffer "Phrase to highlight: "
407 (cons (or (car hi-lock-regexp-history
) "") 1 )
408 nil nil
'hi-lock-regexp-history
)))
409 (hi-lock-read-face-name)))
410 (or (facep face
) (setq face
'hi-yellow
))
411 (unless hi-lock-mode
(hi-lock-mode 1))
412 (hi-lock-set-pattern regexp face
))
415 (defalias 'unhighlight-regexp
'hi-lock-unface-buffer
)
417 (defun hi-lock-unface-buffer (regexp)
418 "Remove highlighting of each match to REGEXP set by hi-lock.
420 Interactively, prompt for REGEXP. Buffer-local history of inserted
421 regexp's maintained. Will accept only regexps inserted by hi-lock
422 interactive functions. \(See `hi-lock-interactive-patterns'.\)
423 \\<minibuffer-local-must-match-map>Use \\[minibuffer-complete] to complete a partially typed regexp.
424 \(See info node `Minibuffer History'.\)"
426 (if (and (display-popup-menus-p) (vectorp (this-command-keys)))
433 (cons "Select Pattern to Unhighlight"
434 (mapcar (lambda (pattern)
437 "%s (%s)" (car pattern
)
440 (cdr (car (cdr (car (cdr pattern
))))))))
443 hi-lock-interactive-patterns
))))
444 ;; If the user clicks outside the menu, meaning that they
445 ;; change their mind, x-popup-menu returns nil, and
446 ;; interactive signals a wrong number of arguments error.
447 ;; To prevent that, we return an empty string, which will
448 ;; effectively disable the rest of the function.
449 (throw 'snafu
'(""))))
450 (let ((history-list (mapcar (lambda (p) (car p
))
451 hi-lock-interactive-patterns
)))
452 (unless hi-lock-interactive-patterns
453 (error "No highlighting to remove"))
455 (completing-read "Regexp to unhighlight: "
456 hi-lock-interactive-patterns nil t
457 (car (car hi-lock-interactive-patterns
))
458 (cons 'history-list
1))))))
459 (let ((keyword (assoc regexp hi-lock-interactive-patterns
)))
461 (font-lock-remove-keywords nil
(list keyword
))
462 (setq hi-lock-interactive-patterns
463 (delq keyword hi-lock-interactive-patterns
))
464 (font-lock-fontify-buffer))))
467 (defun hi-lock-write-interactive-patterns ()
468 "Write interactively added patterns, if any, into buffer at point.
470 Interactively added patterns are those normally specified using
471 `highlight-regexp' and `highlight-lines-matching-regexp'; they can
472 be found in variable `hi-lock-interactive-patterns'."
474 (if (null hi-lock-interactive-patterns
)
475 (error "There are no interactive patterns"))
479 (insert (format "Hi-lock: (%s)\n" (prin1-to-string pattern
))))
480 hi-lock-interactive-patterns
)
481 (comment-region beg
(point)))
482 (when (> (point) hi-lock-file-patterns-range
)
483 (warn "Inserted keywords not close enough to top of file")))
485 ;; Implementation Functions
487 (defun hi-lock-process-phrase (phrase)
488 "Convert regexp PHRASE to a regexp that matches phrases.
490 Blanks in PHRASE replaced by regexp that matches arbitrary whitespace
491 and initial lower-case letters made case insensitive."
492 (let ((mod-phrase nil
))
494 (replace-regexp-in-string
495 "\\<[a-z]" (lambda (m) (format "[%s%s]" (upcase m
) m
)) phrase
))
497 (replace-regexp-in-string
498 "\\s-+" "[ \t\n]+" mod-phrase nil t
))))
500 (defun hi-lock-regexp-okay (regexp)
501 "Return REGEXP if it appears suitable for a font-lock pattern.
503 Otherwise signal an error. A pattern that matches the null string is
505 (if (string-match regexp
"")
506 (error "Regexp cannot match an empty string")
509 (defun hi-lock-read-face-name ()
510 "Read face name from minibuffer with completion and history."
511 (intern (completing-read
512 "Highlight using face: "
514 (cons (car hi-lock-face-history
)
517 (substring (car hi-lock-face-history
) 0 1)
518 (mapcar (lambda (f) (cons f f
))
519 hi-lock-face-history
))))
520 (if (and (stringp prefix
)
521 (not (equal prefix
(car hi-lock-face-history
))))
523 '(hi-lock-face-history .
0))))
525 (defun hi-lock-set-pattern (regexp face
)
526 "Highlight REGEXP with face FACE."
527 (let ((pattern (list regexp
(list 0 (list 'quote face
) t
))))
528 (unless (member pattern hi-lock-interactive-patterns
)
529 (font-lock-add-keywords nil
(list pattern
))
530 (push pattern hi-lock-interactive-patterns
)
531 (let ((buffer-undo-list t
)
532 (inhibit-read-only t
)
533 (mod (buffer-modified-p)))
535 (goto-char (point-min))
536 (while (re-search-forward regexp
(point-max) t
)
538 (match-beginning 0) (match-end 0) 'face face
)
539 (goto-char (match-end 0))))
540 (set-buffer-modified-p mod
)))))
542 (defun hi-lock-set-file-patterns (patterns)
543 "Replace file patterns list with PATTERNS and refontify."
544 (when (or hi-lock-file-patterns patterns
)
545 (font-lock-remove-keywords nil hi-lock-file-patterns
)
546 (setq hi-lock-file-patterns patterns
)
547 (font-lock-add-keywords nil hi-lock-file-patterns
)
548 (font-lock-fontify-buffer)))
550 (defun hi-lock-find-patterns ()
551 "Find patterns in current buffer for hi-lock."
553 (unless (memq major-mode hi-lock-exclude-modes
)
554 (let ((all-patterns nil
)
555 (target-regexp (concat "\\<" hi-lock-file-patterns-prefix
":")))
559 (goto-char (point-min))
560 (re-search-forward target-regexp
561 (+ (point) hi-lock-file-patterns-range
) t
)
563 (while (and (re-search-forward target-regexp
(+ (point) 100) t
)
564 (not (looking-at "\\s-*end")))
566 (setq all-patterns
(append (read (current-buffer)) all-patterns
))
567 (error (message "Invalid pattern list expression at %d"
568 (line-number-at-pos)))))))
569 (when hi-lock-mode
(hi-lock-set-file-patterns all-patterns
))
571 (message "Hi-lock added %d patterns." (length all-patterns
))))))
573 (defun hi-lock-font-lock-hook ()
574 "Add hi lock patterns to font-lock's."
576 (progn (font-lock-add-keywords nil hi-lock-file-patterns
)
577 (font-lock-add-keywords nil hi-lock-interactive-patterns
))
582 ;; arch-tag: d2e8fd07-4cc9-4c6f-a200-1e729bc54066
583 ;;; hi-lock.el ends here