aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerd Moellmann2001-02-06 15:43:37 +0000
committerGerd Moellmann2001-02-06 15:43:37 +0000
commit108ee42bc786bc57c9398942088dd342f25db9dc (patch)
treecc485f82ae9c5bd7acbcf983b87e1561103b0ec6
parentc363a1d6fdd39d16401a6eee3d66c870ed7c251b (diff)
downloademacs-108ee42bc786bc57c9398942088dd342f25db9dc.tar.gz
emacs-108ee42bc786bc57c9398942088dd342f25db9dc.zip
(hi-lock-mode): Toggling hi-lock-mode now affects all
buffers. When hi-lock turned on rather than only checking current buffer for regexps, all buffers are checked. Moved activation of font-lock to hi-lock-refontify. When font-lock turned off rather than removing added highlighting just in current buffer, remove it in all buffers. Changed edit menu text from "Automatic Highlighting" to "Regexp Highlighting" Documentation for highlighting phrases, minor documentation changes. (hi-lock-set-file-patterns): Execute only if there are new or existing file patterns. (hi-lock-refontify): Assume font-lock-fontify-buffer will first unfontify and, if a support mode is active, will not refontify the whole buffer. If necessary, turn on font lock. (Removed font-lock-unfontify and font-lock support-mode-specific calls, such as lazy-lock-fontify-window.) (hi-lock-find-patterns): Do not turn on hi-lock-mode even if patterns are found. Not useful now since find-file-hook is removed if hi-lock is off, but may be needed for per-buffer hi-lock activation. (hi-lock-face-phrase-buffer): New function. Also added related menu item and keybinding. (highlight-phrase): New alias, to hi-lock-face-phrase-buffer. (hi-lock-process-phrase): New function. (hi-lock-line-face-buffer): Doc fixes. (hi-lock-face-buffer): Doc fixes. (hi-lock-unface-buffer): Doc fixes.
-rw-r--r--lisp/hi-lock.el128
1 files changed, 82 insertions, 46 deletions
diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el
index 22ae9e7afb2..0ae3dbddce6 100644
--- a/lisp/hi-lock.el
+++ b/lisp/hi-lock.el
@@ -49,12 +49,12 @@
49;; 49;;
50;; When writing text, highlight personal cliches. This can be 50;; When writing text, highlight personal cliches. This can be
51;; amusing. 51;; amusing.
52;; M-x highlight-regexp as can be seen RET RET 52;; M-x highlight-phrase as can be seen RET RET
53;; 53;;
54;; Setup 54;; Setup:
55;; 55;;
56;; Put the following code in your .emacs file. This turns on 56;; Put the following code in your .emacs file. This turns on
57;; hi-lock mode and adds an "Automatic Highlighting" entry 57;; hi-lock mode and adds a "Regexp Highlighting" entry
58;; to the edit menu. 58;; to the edit menu.
59;; 59;;
60;; (hi-lock-mode 1) 60;; (hi-lock-mode 1)
@@ -65,6 +65,7 @@
65;; (define-key hi-lock-map "\C-z\C-h" 'highlight-lines-matching-regexp) 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) 66;; (define-key hi-lock-map "\C-zi" 'hi-lock-find-patterns)
67;; (define-key hi-lock-map "\C-zh" 'highlight-regexp) 67;; (define-key hi-lock-map "\C-zh" 'highlight-regexp)
68;; (define-key hi-lock-map "\C-zp" 'highlight-phrase)
68;; (define-key hi-lock-map "\C-zr" 'unhighlight-regexp) 69;; (define-key hi-lock-map "\C-zr" 'unhighlight-regexp)
69;; (define-key hi-lock-map "\C-zb" 'hi-lock-write-interactive-patterns)) 70;; (define-key hi-lock-map "\C-zb" 'hi-lock-write-interactive-patterns))
70 71
@@ -200,6 +201,10 @@ calls."
200 '(menu-item "Highlight Regexp..." highlight-regexp 201 '(menu-item "Highlight Regexp..." highlight-regexp
201 :help "Highlight text matching PATTERN (a regexp).")) 202 :help "Highlight text matching PATTERN (a regexp)."))
202 203
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)."))
207
203(define-key-after hi-lock-menu [highlight-lines-matching-regexp] 208(define-key-after hi-lock-menu [highlight-lines-matching-regexp]
204 '(menu-item "Highlight Lines..." highlight-lines-matching-regexp 209 '(menu-item "Highlight Lines..." highlight-lines-matching-regexp
205 :help "Highlight lines containing match of PATTERN (a regexp)..")) 210 :help "Highlight lines containing match of PATTERN (a regexp).."))
@@ -223,6 +228,7 @@ calls."
223 228
224(define-key hi-lock-map "\C-xwi" 'hi-lock-find-patterns) 229(define-key hi-lock-map "\C-xwi" 'hi-lock-find-patterns)
225(define-key hi-lock-map "\C-xwl" 'highlight-lines-matching-regexp) 230(define-key hi-lock-map "\C-xwl" 'highlight-lines-matching-regexp)
231(define-key hi-lock-map "\C-xwp" 'highlight-phrase)
226(define-key hi-lock-map "\C-xwh" 'highlight-regexp) 232(define-key hi-lock-map "\C-xwh" 'highlight-regexp)
227(define-key hi-lock-map "\C-xwr" 'unhighlight-regexp) 233(define-key hi-lock-map "\C-xwr" 'unhighlight-regexp)
228(define-key hi-lock-map "\C-xwb" 'hi-lock-write-interactive-patterns) 234(define-key hi-lock-map "\C-xwb" 'hi-lock-write-interactive-patterns)
@@ -243,13 +249,18 @@ calls."
243 "Toggle minor mode for interactively adding font-lock highlighting patterns. 249 "Toggle minor mode for interactively adding font-lock highlighting patterns.
244 250
245If ARG positive turn hi-lock on. Issuing a hi-lock command will also 251If ARG positive turn hi-lock on. Issuing a hi-lock command will also
246turn hi-lock on. When hi-lock is turned on an \"Automatic Highlighting\" 252turn hi-lock on. When hi-lock is turned on, a \"Regexp Highlighting\"
247submenu is added to the \"Edit\" menu. The commands in the submenu, 253submenu is added to the \"Edit\" menu. The commands in the submenu,
248which can be called interactively, are: 254which can be called interactively, are:
249 255
250\\[highlight-regexp] REGEXP FACE 256\\[highlight-regexp] REGEXP FACE
251 Highlight matches of pattern REGEXP in current buffer with FACE. 257 Highlight matches of pattern REGEXP in current buffer with FACE.
252 258
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.)
263
253\\[highlight-lines-matching-regexp] REGEXP FACE 264\\[highlight-lines-matching-regexp] REGEXP FACE
254 Highlight lines containing matches of REGEXP in current buffer with FACE. 265 Highlight lines containing matches of REGEXP in current buffer with FACE.
255 266
@@ -278,22 +289,26 @@ is found. A mode is excluded if it's in the list `hi-lock-exclude-modes'."
278 (interactive) 289 (interactive)
279 (let ((hi-lock-mode-prev hi-lock-mode)) 290 (let ((hi-lock-mode-prev hi-lock-mode))
280 (setq hi-lock-mode 291 (setq hi-lock-mode
281 (if (null arg) (not hi-lock-mode) 292 (if (null arg) (not hi-lock-mode)
282 (> (prefix-numeric-value arg) 0))) 293 (> (prefix-numeric-value arg) 0)))
283 ;; Turned on. 294 ;; Turned on.
284 (when (and (not hi-lock-mode-prev) hi-lock-mode) 295 (when (and (not hi-lock-mode-prev) hi-lock-mode)
285 (if (not font-lock-mode) (turn-on-font-lock))
286 (add-hook 'find-file-hooks 'hi-lock-find-file-hook) 296 (add-hook 'find-file-hooks 'hi-lock-find-file-hook)
287 (add-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook) 297 (add-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook)
288 (define-key-after menu-bar-edit-menu [hi-lock] 298 (define-key-after menu-bar-edit-menu [hi-lock]
289 (cons "Automatic Highlighting" hi-lock-menu)) 299 (cons "Regexp Highlighting" hi-lock-menu))
290 (hi-lock-find-patterns)) 300 (dolist (buffer (buffer-list))
301 (with-current-buffer buffer (hi-lock-find-patterns))))
291 ;; Turned off. 302 ;; Turned off.
292 (when (and hi-lock-mode-prev (not hi-lock-mode)) 303 (when (and hi-lock-mode-prev (not hi-lock-mode))
293 (font-lock-remove-keywords nil hi-lock-interactive-patterns) 304 (dolist (buffer (buffer-list))
294 (font-lock-remove-keywords nil hi-lock-file-patterns) 305 (with-current-buffer buffer
295 (setq hi-lock-interactive-patterns nil) 306 (when (or hi-lock-interactive-patterns hi-lock-file-patterns)
296 (hi-lock-refontify) 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)))))
297 (define-key-after menu-bar-edit-menu [hi-lock] nil) 312 (define-key-after menu-bar-edit-menu [hi-lock] nil)
298 (remove-hook 'find-file-hooks 'hi-lock-find-file-hook) 313 (remove-hook 'find-file-hooks 'hi-lock-find-file-hook)
299 (remove-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook)))) 314 (remove-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook))))
@@ -303,7 +318,7 @@ is found. A mode is excluded if it's in the list `hi-lock-exclude-modes'."
303(defalias 'highlight-lines-matching-regexp 'hi-lock-line-face-buffer) 318(defalias 'highlight-lines-matching-regexp 'hi-lock-line-face-buffer)
304;;;###autoload 319;;;###autoload
305(defun hi-lock-line-face-buffer (regexp &optional face) 320(defun hi-lock-line-face-buffer (regexp &optional face)
306 "Set face of all lines containing matches of REGEXP to FACE. 321 "Set face of all lines containing a match of REGEXP to FACE.
307 322
308Interactively, prompt for REGEXP then FACE. Buffer-local history 323Interactively, prompt for REGEXP then FACE. Buffer-local history
309list maintained for regexps, global history maintained for faces. 324list maintained for regexps, global history maintained for faces.
@@ -321,11 +336,12 @@ list maintained for regexps, global history maintained for faces.
321 (hi-lock-set-pattern 336 (hi-lock-set-pattern
322 (list (concat "^.*" regexp ".*$") (list 0 (list 'quote face) t)))) 337 (list (concat "^.*" regexp ".*$") (list 0 (list 'quote face) t))))
323 338
339
324;;;###autoload 340;;;###autoload
325(defalias 'highlight-regexp 'hi-lock-face-buffer) 341(defalias 'highlight-regexp 'hi-lock-face-buffer)
326;;;###autoload 342;;;###autoload
327(defun hi-lock-face-buffer (regexp &optional face) 343(defun hi-lock-face-buffer (regexp &optional face)
328 "Set face of all matches of REGEXP to FACE. 344 "Set face of each match of REGEXP to FACE.
329 345
330Interactively, prompt for REGEXP then FACE. Buffer-local history 346Interactively, prompt for REGEXP then FACE. Buffer-local history
331list maintained for regexps, global history maintained for faces. 347list maintained for regexps, global history maintained for faces.
@@ -343,14 +359,34 @@ list maintained for regexps, global history maintained for faces.
343 (hi-lock-set-pattern (list regexp (list 0 (list 'quote face) t)))) 359 (hi-lock-set-pattern (list regexp (list 0 (list 'quote face) t))))
344 360
345;;;###autoload 361;;;###autoload
362(defalias 'highlight-phrase 'hi-lock-face-phrase-buffer)
363;;;###autoload
364(defun hi-lock-face-phrase-buffer (regexp &optional face)
365 "Set face of each match of phrase REGEXP to FACE.
366
367Whitespace in REGEXP converted to arbitrary whitespace and initial
368lower-case letters made case insensitive."
369 (interactive
370 (list
371 (hi-lock-regexp-okay
372 (hi-lock-process-phrase
373 (read-from-minibuffer "Phrase to highlight: "
374 (cons (or (car hi-lock-regexp-history) "") 1 )
375 nil nil 'hi-lock-regexp-history)))
376 (hi-lock-read-face-name)))
377 (or (facep face) (setq face 'rwl-yellow))
378 (unless hi-lock-mode (hi-lock-mode))
379 (hi-lock-set-pattern (list regexp (list 0 (list 'quote face) t))))
380
381;;;###autoload
346(defalias 'unhighlight-regexp 'hi-lock-unface-buffer) 382(defalias 'unhighlight-regexp 'hi-lock-unface-buffer)
347;;;###autoload 383;;;###autoload
348(defun hi-lock-unface-buffer (regexp) 384(defun hi-lock-unface-buffer (regexp)
349 "Remove highlighting of matches to REGEXP set by hi-lock. 385 "Remove highlighting of each match to REGEXP set by hi-lock.
350 386
351Interactively, prompt for REGEXP. Buffer-local history of inserted 387Interactively, prompt for REGEXP. Buffer-local history of inserted
352regexp's maintained. Will accept only regexps inserted by hi-lock 388regexp's maintained. Will accept only regexps inserted by hi-lock
353interactive functions. \(See `hi-lock-interactive-patterns'.\) 389interactive functions. \(See `hi-lock-interactive-patterns'.\)
354\\<minibuffer-local-must-match-map>Use \\[minibuffer-complete] to complete a partially typed regexp. 390\\<minibuffer-local-must-match-map>Use \\[minibuffer-complete] to complete a partially typed regexp.
355\(See info node `Minibuffer History'.\)" 391\(See info node `Minibuffer History'.\)"
356 (interactive 392 (interactive
@@ -416,6 +452,19 @@ be found in variable `hi-lock-interactive-patterns'."
416 452
417;; Implementation Functions 453;; Implementation Functions
418 454
455(defun hi-lock-process-phrase (phrase)
456 "Convert regexp PHRASE to a regexp that matches phrases.
457
458Blanks in PHRASE replaced by regexp that matches arbitrary whitespace
459and initial lower-case letters made case insensitive."
460 (let ((mod-phrase nil))
461 (setq mod-phrase
462 (replace-regexp-in-string
463 "\\<[a-z]" (lambda (m) (format "[%s%s]" (upcase m) m)) phrase))
464 (setq mod-phrase
465 (replace-regexp-in-string
466 "\\s-+" "[ \t\n]+" mod-phrase nil t))))
467
419(defun hi-lock-regexp-okay (regexp) 468(defun hi-lock-regexp-okay (regexp)
420 "Return REGEXP if it appears suitable for a font-lock pattern. 469 "Return REGEXP if it appears suitable for a font-lock pattern.
421 470
@@ -467,25 +516,17 @@ Optional argument END is maximum excursion."
467 516
468(defun hi-lock-set-file-patterns (patterns) 517(defun hi-lock-set-file-patterns (patterns)
469 "Replace file patterns list with PATTERNS and refontify." 518 "Replace file patterns list with PATTERNS and refontify."
470 (font-lock-remove-keywords nil hi-lock-file-patterns) 519 (when (or hi-lock-file-patterns patterns)
471 (setq hi-lock-file-patterns patterns) 520 (font-lock-remove-keywords nil hi-lock-file-patterns)
472 (font-lock-add-keywords nil hi-lock-file-patterns) 521 (setq hi-lock-file-patterns patterns)
473 (hi-lock-refontify)) 522 (font-lock-add-keywords nil hi-lock-file-patterns)
523 (hi-lock-refontify)))
474 524
475(defun hi-lock-refontify () 525(defun hi-lock-refontify ()
476 "Unfontify then refontify buffer. Used when hi-lock patterns change." 526 "Unfontify then refontify buffer. Used when hi-lock patterns change."
477 (interactive) 527 (interactive)
478 (font-lock-unfontify-buffer) 528 (unless font-lock-mode (font-lock-mode 1))
479 (cond 529 (font-lock-fontify-buffer))
480 (jit-lock-mode (jit-lock-refontify))
481 ;; Need a better way, since this assumes too much about lazy lock.
482 (lazy-lock-mode
483 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
484 (while windows
485 (lazy-lock-fontify-window (car windows))
486 (setq windows (cdr windows)))))
487 (t (font-lock-fontify-buffer))))
488
489 530
490(defun hi-lock-find-patterns () 531(defun hi-lock-find-patterns ()
491 "Find patterns in current buffer for hi-lock." 532 "Find patterns in current buffer for hi-lock."
@@ -499,23 +540,18 @@ Optional argument END is maximum excursion."
499 (re-search-forward target-regexp 540 (re-search-forward target-regexp
500 (+ (point) hi-lock-file-patterns-range) t) 541 (+ (point) hi-lock-file-patterns-range) t)
501 (beginning-of-line) 542 (beginning-of-line)
502 (while 543 (while (and (re-search-forward target-regexp (+ (point) 100) t)
503 (and 544 (not (looking-at "\\s-*end")))
504 (re-search-forward target-regexp (+ (point) 100) t) 545 (let ((patterns
505 (not (looking-at "\\s-*end"))) 546 (condition-case nil
506 (let 547 (read (current-buffer))
507 ((patterns 548 (error (message
508 (condition-case nil 549 (format "Could not read expression at %d"
509 (read (current-buffer)) 550 (hi-lock-current-line))) nil))))
510 (error (message
511 (format "Could not read expression at %d"
512 (hi-lock-current-line))) nil))))
513 (if patterns 551 (if patterns
514 (setq all-patterns (append patterns all-patterns)))))) 552 (setq all-patterns (append patterns all-patterns))))))
515 (if (and (not hi-lock-mode) all-patterns)
516 (hi-lock-mode 1))
517 (unless font-lock-mode (font-lock-mode)) 553 (unless font-lock-mode (font-lock-mode))
518 (if hi-lock-mode (hi-lock-set-file-patterns all-patterns)) 554 (when hi-lock-mode (hi-lock-set-file-patterns all-patterns))
519 (if (interactive-p) 555 (if (interactive-p)
520 (message (format "Hi-lock added %d patterns." (length all-patterns))))))) 556 (message (format "Hi-lock added %d patterns." (length all-patterns)))))))
521 557