1 ;;; hi-lock.el --- Minor mode for interactive automatic highlighting.
3 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: David M. Koppelman, koppel@ee.lsu.edu
6 ;; Keywords: faces, minor-mode, matching, display
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; With the hi-lock commands text matching interactively entered
28 ;; regexp's can be highlighted. For example, `M-x highlight-regexp
29 ;; RET clearly RET RET' will highlight all occurrences of `clearly'
30 ;; using a yellow background face. New occurrences of `clearly' will
31 ;; be highlighted as they are typed. `M-x unhighlight-regexp RET'
32 ;; will remove the highlighting. Any existing face can be used for
33 ;; highlighting and a set of appropriate faces is provided. The
34 ;; regexps can be written into the current buffer in a form that will
35 ;; be recognized the next time the corresponding file is read.
39 ;; In program source code highlight a variable to quickly see all
40 ;; places it is modified or referenced:
41 ;; M-x highlight-regexp ground_contact_switches_closed RET RET
43 ;; In a shell or other buffer that is showing lots of program
44 ;; output, highlight the parts of the output you're interested in:
45 ;; M-x highlight-regexp Total execution time [0-9]+ RET hi-blue-b RET
47 ;; In buffers displaying tables, highlight the lines you're interested in:
48 ;; M-x highlight-lines-matching-regexp January 2000 RET hi-black-b RET
50 ;; When writing text, highlight personal cliches. This can be
52 ;; M-x highlight-phrase as can be seen RET RET
56 ;; Put the following code in your .emacs file. This turns on
57 ;; hi-lock mode and adds a "Regexp Highlighting" entry
62 ;; You might also want to bind the hi-lock commands to more
63 ;; finger-friendly sequences:
65 ;; (define-key hi-lock-map "\C-z\C-h" 'highlight-lines-matching-regexp)
66 ;; (define-key hi-lock-map "\C-zi" 'hi-lock-find-patterns)
67 ;; (define-key hi-lock-map "\C-zh" 'highlight-regexp)
68 ;; (define-key hi-lock-map "\C-zp" 'highlight-phrase)
69 ;; (define-key hi-lock-map "\C-zr" 'unhighlight-regexp)
70 ;; (define-key hi-lock-map "\C-zb" 'hi-lock-write-interactive-patterns))
72 ;; See the documentation for hi-lock-mode `C-h f hi-lock-mode' for
73 ;; additional instructions.
75 ;; Sample file patterns:
77 ; Hi-lock: (("^;;; .*" (0 (quote hi-black-hb) t)))
78 ; Hi-lock: ( ("make-variable-buffer-\\(local\\)" (0 font-lock-keyword-face)(1 'italic append)))))
87 (defgroup hi-lock-interactive-text-highlighting nil
88 "Interactively add and remove font-lock patterns for highlighting text."
92 (defcustom hi-lock-mode nil
93 "Toggle hi-lock, for interactively adding font-lock text-highlighting patterns."
94 :set
(lambda (symbol value
)
95 (hi-lock-mode (or value
0)))
96 :initialize
'custom-initialize-default
98 :group
'hi-lock-interactive-text-highlighting
101 (defcustom hi-lock-file-patterns-range
10000
102 "Limit of search in a buffer for hi-lock patterns.
103 When a file is visited and hi-lock mode is on patterns starting
104 up to this limit are added to font-lock's patterns. See documentation
105 of functions `hi-lock-mode' and `hi-lock-find-patterns'."
107 :group
'hi-lock-interactive-text-highlighting
)
109 (defcustom hi-lock-exclude-modes
110 '(rmail-mode mime
/viewer-mode gnus-article-mode
)
111 "List of major modes in which hi-lock will not run.
112 For security reasons since font lock patterns can specify function
114 :type
'(repeat symbol
)
115 :group
'hi-lock-interactive-text-highlighting
)
118 (defgroup hi-lock-faces nil
120 :group
'hi-lock-interactive-text-highlighting
)
123 '((((background dark
)) (:background
"yellow" :foreground
"black"))
124 (t (:background
"yellow")))
125 "Default face for hi-lock mode."
126 :group
'hi-lock-faces
)
129 '((((background dark
)) (:background
"pink" :foreground
"black"))
130 (t (:background
"pink")))
131 "Face for hi-lock mode."
132 :group
'hi-lock-faces
)
135 '((((background dark
)) (:background
"green" :foreground
"black"))
136 (t (:background
"green")))
137 "Face for hi-lock mode."
138 :group
'hi-lock-faces
)
141 '((((background dark
)) (:background
"light blue" :foreground
"black"))
142 (t (:background
"light blue")))
143 "Face for hi-lock mode."
144 :group
'hi-lock-faces
)
147 '((t (:weight bold
)))
148 "Face for hi-lock mode."
149 :group
'hi-lock-faces
)
152 '((t (:weight bold
:foreground
"blue")))
153 "Face for hi-lock mode."
154 :group
'hi-lock-faces
)
157 '((t (:weight bold
:foreground
"green")))
158 "Face for hi-lock mode."
159 :group
'hi-lock-faces
)
162 '((t (:weight bold
:foreground
"red")))
163 "Face for hi-lock mode."
164 :group
'hi-lock-faces
)
167 '((t (:weight bold
:height
1.67 :inherit variable-pitch
)))
168 "Face for hi-lock mode."
169 :group
'hi-lock-faces
)
171 (defvar hi-lock-file-patterns nil
172 "Patterns found in file for hi-lock. Should not be changed.")
174 (defvar hi-lock-interactive-patterns nil
175 "Patterns provided to hi-lock by user. Should not be changed.")
177 (defvar hi-lock-face-history
178 (list "hi-yellow" "hi-pink" "hi-green" "hi-blue" "hi-black-b"
179 "hi-blue-b" "hi-red-b" "hi-green-b" "hi-black-hb")
180 "History list of faces for hi-lock interactive functions.")
182 ;(dolist (f hi-lock-face-history) (unless (facep f) (error "%s not a face" f)))
184 (defvar hi-lock-regexp-history nil
185 "History of regexps used for interactive fontification.")
187 (defvar hi-lock-file-patterns-prefix
"Hi-lock"
188 "Regexp for finding hi-lock patterns at top of file.")
190 (make-variable-buffer-local 'hi-lock-interactive-patterns
)
191 (put 'hi-lock-interactive-patterns
'permanent-local t
)
192 (make-variable-buffer-local 'hi-lock-regexp-history
)
193 (put 'hi-lock-regexp-history
'permanent-local t
)
194 (make-variable-buffer-local 'hi-lock-file-patterns
)
195 (put 'hi-lock-file-patterns
'permanent-local t
)
197 (defvar hi-lock-menu
(make-sparse-keymap "Hi Lock")
198 "Menu for hi-lock mode.")
200 (define-key-after hi-lock-menu
[highlight-regexp
]
201 '(menu-item "Highlight Regexp..." highlight-regexp
202 :help
"Highlight text matching PATTERN (a regexp)."))
204 (define-key-after hi-lock-menu
[highlight-phrase
]
205 '(menu-item "Highlight Phrase..." highlight-phrase
206 :help
"Highlight text matching PATTERN (a regexp processed to match phrases)."))
208 (define-key-after hi-lock-menu
[highlight-lines-matching-regexp
]
209 '(menu-item "Highlight Lines..." highlight-lines-matching-regexp
210 :help
"Highlight lines containing match of PATTERN (a regexp).."))
212 (define-key-after hi-lock-menu
[unhighlight-regexp
]
213 '(menu-item "Remove Highlighting..." unhighlight-regexp
214 :help
"Remove previously entered highlighting pattern."
215 :enable hi-lock-interactive-patterns
))
217 (define-key-after hi-lock-menu
[hi-lock-write-interactive-patterns
]
218 '(menu-item "Patterns to Buffer" hi-lock-write-interactive-patterns
219 :help
"Insert interactively added REGEXPs into buffer at point."
220 :enable hi-lock-interactive-patterns
))
222 (define-key-after hi-lock-menu
[hi-lock-find-patterns
]
223 '(menu-item "Patterns from Buffer" hi-lock-find-patterns
224 :help
"Use patterns (if any) near top of buffer."))
226 (defvar hi-lock-map
(make-sparse-keymap "Hi Lock")
227 "Key map for hi-lock.")
229 (define-key hi-lock-map
"\C-xwi" 'hi-lock-find-patterns
)
230 (define-key hi-lock-map
"\C-xwl" 'highlight-lines-matching-regexp
)
231 (define-key hi-lock-map
"\C-xwp" 'highlight-phrase
)
232 (define-key hi-lock-map
"\C-xwh" 'highlight-regexp
)
233 (define-key hi-lock-map
"\C-xwr" 'unhighlight-regexp
)
234 (define-key hi-lock-map
"\C-xwb" 'hi-lock-write-interactive-patterns
)
236 (unless (assq 'hi-lock-mode minor-mode-map-alist
)
237 (setq minor-mode-map-alist
(cons (cons 'hi-lock-mode hi-lock-map
)
238 minor-mode-map-alist
)))
240 (unless (assq 'hi-lock-mode minor-mode-alist
)
241 (setq minor-mode-alist
(cons '(hi-lock-mode " H") minor-mode-alist
)))
248 (defun hi-lock-mode (&optional arg
)
249 "Toggle minor mode for interactively adding font-lock highlighting patterns.
251 If ARG positive turn hi-lock on. Issuing a hi-lock command will also
252 turn hi-lock on. When hi-lock is turned on, a \"Regexp Highlighting\"
253 submenu is added to the \"Edit\" menu. The commands in the submenu,
254 which can be called interactively, are:
256 \\[highlight-regexp] REGEXP FACE
257 Highlight matches of pattern REGEXP in current buffer with FACE.
259 \\[highlight-phrase] PHRASE FACE
260 Highlight matches of phrase PHRASE in current buffer with FACE.
261 (PHRASE can be any REGEXP, but spaces will be replaced by matches
262 to whitespace and initial lower-case letters will become case insensitive.)
264 \\[highlight-lines-matching-regexp] REGEXP FACE
265 Highlight lines containing matches of REGEXP in current buffer with FACE.
267 \\[unhighlight-regexp] REGEXP
268 Remove highlighting on matches of REGEXP in current buffer.
270 \\[hi-lock-write-interactive-patterns]
271 Write active REGEXPs into buffer as comments (if possible). They will
272 be read the next time file is loaded or when the \\[hi-lock-find-patterns] command
273 is issued. The inserted regexps are in the form of font lock keywords.
274 (See `font-lock-keywords') They may be edited and re-loaded with \\[hi-lock-find-patterns],
275 any valid `font-lock-keywords' form is acceptable.
277 \\[hi-lock-find-patterns]
278 Re-read patterns stored in buffer (in the format produced by \\[hi-lock-write-interactive-patterns]).
280 When hi-lock is started and if the mode is not excluded, the
281 beginning of the buffer is searched for lines of the form:
283 where FOO is a list of patterns. These are added to the font lock keywords
284 already present. The patterns must start before position (number
285 of characters into buffer) `hi-lock-file-patterns-range'. Patterns
288 is found. A mode is excluded if it's in the list `hi-lock-exclude-modes'."
290 (let ((hi-lock-mode-prev hi-lock-mode
))
292 (if (null arg
) (not hi-lock-mode
)
293 (> (prefix-numeric-value arg
) 0)))
295 (when (and (not hi-lock-mode-prev
) hi-lock-mode
)
296 (add-hook 'find-file-hooks
'hi-lock-find-file-hook
)
297 (add-hook 'font-lock-mode-hook
'hi-lock-font-lock-hook
)
298 (define-key-after menu-bar-edit-menu
[hi-lock
]
299 (cons "Regexp Highlighting" hi-lock-menu
))
300 (dolist (buffer (buffer-list))
301 (with-current-buffer buffer
(hi-lock-find-patterns))))
303 (when (and hi-lock-mode-prev
(not hi-lock-mode
))
304 (dolist (buffer (buffer-list))
305 (with-current-buffer buffer
306 (when (or hi-lock-interactive-patterns hi-lock-file-patterns
)
307 (font-lock-remove-keywords nil hi-lock-interactive-patterns
)
308 (font-lock-remove-keywords nil hi-lock-file-patterns
)
309 (setq hi-lock-interactive-patterns nil
310 hi-lock-file-patterns nil
)
311 (when font-lock-mode
(hi-lock-refontify)))))
312 (define-key-after menu-bar-edit-menu
[hi-lock
] nil
)
313 (remove-hook 'find-file-hooks
'hi-lock-find-file-hook
)
314 (remove-hook 'font-lock-mode-hook
'hi-lock-font-lock-hook
))))
318 (defalias 'highlight-lines-matching-regexp
'hi-lock-line-face-buffer
)
320 (defun hi-lock-line-face-buffer (regexp &optional face
)
321 "Set face of all lines containing a match of REGEXP to FACE.
323 Interactively, prompt for REGEXP then FACE. Buffer-local history
324 list maintained for regexps, global history maintained for faces.
325 \\<minibuffer-local-map>Use \\[next-history-element] and \\[previous-history-element] to retrieve next or previous history item.
326 \(See info node `Minibuffer History')"
330 (read-from-minibuffer "Regexp to highlight line: "
331 (cons (or (car hi-lock-regexp-history
) "") 1 )
332 nil nil
'hi-lock-regexp-history
))
333 (hi-lock-read-face-name)))
334 (unless hi-lock-mode
(hi-lock-mode))
335 (or (facep face
) (setq face
'rwl-yellow
))
337 ;; The \\(?:...\\) grouping construct ensures that a leading ^, +, * or ?
338 ;; or a trailing $ in REGEXP will be interpreted correctly.
339 (list (concat "^.*\\(?:" regexp
"\\).*$") (list 0 (list 'quote face
) t
))))
343 (defalias 'highlight-regexp
'hi-lock-face-buffer
)
345 (defun hi-lock-face-buffer (regexp &optional face
)
346 "Set face of each match of REGEXP to FACE.
348 Interactively, prompt for REGEXP then FACE. Buffer-local history
349 list maintained for regexps, global history maintained for faces.
350 \\<minibuffer-local-map>Use \\[next-history-element] and \\[previous-history-element] to retrieve next or previous history item.
351 \(See info node `Minibuffer History')"
355 (read-from-minibuffer "Regexp to highlight: "
356 (cons (or (car hi-lock-regexp-history
) "") 1 )
357 nil nil
'hi-lock-regexp-history
))
358 (hi-lock-read-face-name)))
359 (or (facep face
) (setq face
'rwl-yellow
))
360 (unless hi-lock-mode
(hi-lock-mode))
361 (hi-lock-set-pattern (list regexp
(list 0 (list 'quote face
) t
))))
364 (defalias 'highlight-phrase
'hi-lock-face-phrase-buffer
)
366 (defun hi-lock-face-phrase-buffer (regexp &optional face
)
367 "Set face of each match of phrase REGEXP to FACE.
369 Whitespace in REGEXP converted to arbitrary whitespace and initial
370 lower-case letters made case insensitive."
374 (hi-lock-process-phrase
375 (read-from-minibuffer "Phrase to highlight: "
376 (cons (or (car hi-lock-regexp-history
) "") 1 )
377 nil nil
'hi-lock-regexp-history
)))
378 (hi-lock-read-face-name)))
379 (or (facep face
) (setq face
'rwl-yellow
))
380 (unless hi-lock-mode
(hi-lock-mode))
381 (hi-lock-set-pattern (list regexp
(list 0 (list 'quote face
) t
))))
384 (defalias 'unhighlight-regexp
'hi-lock-unface-buffer
)
386 (defun hi-lock-unface-buffer (regexp)
387 "Remove highlighting of each match to REGEXP set by hi-lock.
389 Interactively, prompt for REGEXP. Buffer-local history of inserted
390 regexp's maintained. Will accept only regexps inserted by hi-lock
391 interactive functions. \(See `hi-lock-interactive-patterns'.\)
392 \\<minibuffer-local-must-match-map>Use \\[minibuffer-complete] to complete a partially typed regexp.
393 \(See info node `Minibuffer History'.\)"
395 (if (vectorp (this-command-keys))
402 (cons "Select Pattern to Unhighlight"
403 (mapcar (lambda (pattern)
406 "%s (%s)" (car pattern
)
409 (cdr (car (cdr (car (cdr pattern
))))))))
412 hi-lock-interactive-patterns
))))
413 ;; If the user clicks outside the menu, meaning that they
414 ;; change their mind, x-popup-menu returns nil, and
415 ;; interactive signals a wrong number of arguments error.
416 ;; To prevent that, we return an empty string, which will
417 ;; effectively disable the rest of the function.
418 (throw 'snafu
'(""))))
419 (let ((history-list (mapcar (lambda (p) (car p
))
420 hi-lock-interactive-patterns
)))
421 (unless hi-lock-interactive-patterns
422 (error "No highlighting to remove"))
424 (completing-read "Regexp to unhighlight: "
425 hi-lock-interactive-patterns nil t
426 (car (car hi-lock-interactive-patterns
))
427 (cons 'history-list
1))))))
428 (let ((keyword (assoc regexp hi-lock-interactive-patterns
)))
430 (font-lock-remove-keywords nil
(list keyword
))
431 (setq hi-lock-interactive-patterns
432 (delq keyword hi-lock-interactive-patterns
))
433 (hi-lock-refontify))))
436 (defun hi-lock-write-interactive-patterns ()
437 "Write interactively added patterns, if any, into buffer at point.
439 Interactively added patterns are those normally specified using
440 `highlight-regexp' and `highlight-lines-matching-regexp'; they can
441 be found in variable `hi-lock-interactive-patterns'."
443 (let ((prefix (format "%s %s:" (or comment-start
"") "Hi-lock")))
444 (when (> (+ (point) (length prefix
)) hi-lock-file-patterns-range
)
447 "Warning, inserted keywords not close enough to top of file."))
450 (insert (format "%s (%s) %s\n"
451 prefix
(prin1-to-string pattern
) (or comment-end
""))))
452 hi-lock-interactive-patterns
)))
455 ;; Implementation Functions
457 (defun hi-lock-process-phrase (phrase)
458 "Convert regexp PHRASE to a regexp that matches phrases.
460 Blanks in PHRASE replaced by regexp that matches arbitrary whitespace
461 and initial lower-case letters made case insensitive."
462 (let ((mod-phrase nil
))
464 (replace-regexp-in-string
465 "\\<[a-z]" (lambda (m) (format "[%s%s]" (upcase m
) m
)) phrase
))
467 (replace-regexp-in-string
468 "\\s-+" "[ \t\n]+" mod-phrase nil t
))))
470 (defun hi-lock-regexp-okay (regexp)
471 "Return REGEXP if it appears suitable for a font-lock pattern.
473 Otherwise signal an error. A pattern that matches the null string is
475 (if (string-match regexp
"")
476 (error "Regexp cannot match an empty string")
479 (defun hi-lock-read-face-name ()
480 "Read face name from minibuffer with completion and history."
481 (intern (completing-read
482 "Highlight using face: "
484 (cons (car hi-lock-face-history
)
487 (substring (car hi-lock-face-history
) 0 1)
488 (mapcar (lambda (f) (cons f f
))
489 hi-lock-face-history
))))
490 (if (and (stringp prefix
)
491 (not (equal prefix
(car hi-lock-face-history
))))
493 '(hi-lock-face-history .
0))))
495 (defun hi-lock-find-file-hook ()
496 "Add hi-lock patterns, if present."
497 (hi-lock-find-patterns))
499 (defun hi-lock-current-line (&optional end
)
500 "Return line number of line at point.
501 Optional argument END is maximum excursion."
505 (1+ (count-lines 1 (or end
(point))))))
507 (defun hi-lock-set-pattern (pattern)
508 "Add PATTERN to list of interactively highlighted patterns and refontify."
509 (hi-lock-set-patterns (list pattern
)))
511 (defun hi-lock-set-patterns (patterns)
512 "Add PATTERNS to list of interactively highlighted patterns and refontify.."
513 (dolist (pattern patterns
)
514 (unless (member pattern hi-lock-interactive-patterns
)
515 (font-lock-add-keywords nil
(list pattern
))
516 (add-to-list 'hi-lock-interactive-patterns pattern
)))
519 (defun hi-lock-set-file-patterns (patterns)
520 "Replace file patterns list with PATTERNS and refontify."
521 (when (or hi-lock-file-patterns patterns
)
522 (font-lock-remove-keywords nil hi-lock-file-patterns
)
523 (setq hi-lock-file-patterns patterns
)
524 (font-lock-add-keywords nil hi-lock-file-patterns
)
525 (hi-lock-refontify)))
527 (defun hi-lock-refontify ()
528 "Unfontify then refontify buffer. Used when hi-lock patterns change."
530 (unless font-lock-mode
(font-lock-mode 1))
531 (font-lock-fontify-buffer))
533 (defun hi-lock-find-patterns ()
534 "Find patterns in current buffer for hi-lock."
536 (unless (memq major-mode hi-lock-exclude-modes
)
537 (let ((all-patterns nil
)
538 (target-regexp (concat "\\<" hi-lock-file-patterns-prefix
":")))
541 (goto-char (point-min))
542 (re-search-forward target-regexp
543 (+ (point) hi-lock-file-patterns-range
) t
)
545 (while (and (re-search-forward target-regexp
(+ (point) 100) t
)
546 (not (looking-at "\\s-*end")))
549 (read (current-buffer))
551 (format "Could not read expression at %d"
552 (hi-lock-current-line))) nil
))))
554 (setq all-patterns
(append patterns all-patterns
))))))
555 (when hi-lock-mode
(hi-lock-set-file-patterns all-patterns
))
557 (message (format "Hi-lock added %d patterns." (length all-patterns
)))))))
559 (defun hi-lock-font-lock-hook ()
560 "Add hi lock patterns to font-lock's."
562 (font-lock-add-keywords nil hi-lock-file-patterns
)
563 (font-lock-add-keywords nil hi-lock-interactive-patterns
)))
567 ;;; hi-lock.el ends here