=== modified file 'lisp/ChangeLog' --- lisp/ChangeLog 2012-10-11 20:39:05 +0000 +++ lisp/ChangeLog 2012-10-12 16:08:17 +0000 @@ -1,3 +1,10 @@ +2012-10-12 Jambunathan K + + * hi-lock.el (hi-lock-unface-buffer): Prompt user with useful + defaults. Specifically, if cursor is on an highlighted text, + offer a regexp that matches that text as the default. Update + docstring. + 2012-10-11 Jambunathan K * replace.el (read-regexp): Tighten the regexp that matches tag. === modified file 'lisp/hi-lock.el' --- lisp/hi-lock.el 2012-10-11 20:39:05 +0000 +++ lisp/hi-lock.el 2012-10-12 15:55:49 +0000 @@ -493,8 +493,15 @@ updated as you type." ;;;###autoload (defun hi-lock-unface-buffer (regexp) "Remove highlighting of each match to REGEXP set by hi-lock. -Interactively, prompt for REGEXP, accepting only regexps -previously inserted by hi-lock interactive functions." +Interactively, prompt for REGEXP. If the cursor is on a +previously highlighted text and if the associated regexp can be +inferred via simple heuristics, offer that regexp as default. +Otherwise, prompt for REGEXP with completion and limit the +choices to only those regexps used previously with hi-lock +commands. + +If this command is invoked via menu, make no attempt to infer +REGEXP from text at point." (interactive (if (and (display-popup-menus-p) (listp last-nonmenu-event) @@ -522,15 +529,51 @@ previously inserted by hi-lock interacti ;; To prevent that, we return an empty string, which will ;; effectively disable the rest of the function. (throw 'snafu '("")))) - (let ((history-list (mapcar (lambda (p) (car p)) - hi-lock-interactive-patterns))) + ;; Un-highlighting triggered via keyboard action. (unless hi-lock-interactive-patterns (error "No highlighting to remove")) + ;; Infer the regexp to un-highlight based on cursor position. + (let* (candidate-hi-lock-patterns + (default-regexp + (or + ;; When using overlays, there is no ambiguity on the best + ;; choice of regexp. + (let ((desired-serial (get-char-property + (point) 'hi-lock-overlay-regexp))) + (when desired-serial + (catch 'regexp + (maphash + (lambda (regexp serial) + (when (= serial desired-serial) + (throw 'regexp regexp))) + hi-lock-string-serialize-hash)))) + ;; With font-locking on, check if the cursor is on an + ;; highlighted text. Checking for hi-lock face is a + ;; good heuristic. + (and (string-match "\\`hi-lock-" (face-name (face-at-point))) + (let* ((hi-text + (buffer-substring-no-properties + (previous-single-property-change (point) 'face) + (next-single-property-change (point) 'face)))) + ;; Compute hi-lock patterns that match the + ;; highlighted text at point. Use this later in + ;; during completing-read. + (setq candidate-hi-lock-patterns + (delq nil + (mapcar + (lambda (hi-lock-pattern) + (let ((regexp (car hi-lock-pattern))) + (and (string-match regexp hi-text) + hi-lock-pattern))) + hi-lock-interactive-patterns))) + ;; Use regexp from the first matching pattern as + ;; a reasonable default. + (caar candidate-hi-lock-patterns)))))) (list (completing-read "Regexp to unhighlight: " - hi-lock-interactive-patterns nil t - (car (car hi-lock-interactive-patterns)) - (cons 'history-list 1)))))) + (or candidate-hi-lock-patterns + hi-lock-interactive-patterns) + nil t default-regexp))))) (let ((keyword (assoc regexp hi-lock-interactive-patterns))) (when keyword (font-lock-remove-keywords nil (list keyword))