Download
(require 'igrep)
(require 'custom)
(defcustom igrep-highlight t
"*If non-nil, highlight matched text in the `igrep-highlight' face."
:group 'igrep
:type '(boolean))
(defface igrep-highlight
'((t (:inherit highlight)))
"Face for highlighting `grep' matches."
:group 'igrep)
(defvar igrep-highlight-overlay nil
"The overlay used by \\[igrep-next-error] to highlight matched text.
To highlight matched text simultaneously in multiple buffers, use
\\[make-variable-buffer-local].")
(defun igrep-next-error (argp)
"Like `next-error', but also highlight the matched text.
Highlighting only occurs if the compilation buffer is an *igrep* buffer.
See the `igrep-highlight' option and the `igrep-highlight' face.
See also `igrep-highlight-overlay'."
(interactive "P")
(next-error argp)
(if (and igrep-highlight
(equal (save-excursion
(set-buffer compilation-last-buffer)
mode-name)
"igrep"))
(save-excursion
(beginning-of-line)
(if (re-search-forward (igrep-regex-to-emacs (car igrep-regex-history))
(if (fboundp 'line-end-position)
(line-end-position)
(save-excursion (end-of-line) (point))))
(if (and (overlayp igrep-highlight-overlay)
(not (local-variable-if-set-p 'igrep-highlight-overlay)))
(move-overlay igrep-highlight-overlay
(match-beginning 0) (match-end 0)
(current-buffer))
(progn
(setq igrep-highlight-overlay
(make-overlay (match-beginning 0) (match-end 0)))
(overlay-put igrep-highlight-overlay 'category 'igrep)
(overlay-put igrep-highlight-overlay 'face
'igrep-highlight)))))))
(defun igrep-regex-to-emacs (regex &optional program)
"Convert an `igrep-program' REGEX to an Emacs regexp."
(if (eq program "fgrep")
(regexp-quote regex)
regex))
(defun igrep-previous-error (&optional argp)
"Like `previous-error', but also highlight the matched text.
Highlighting only occurs if the compilation buffer is an *igrep* buffer.
See `igrep-next-error'."
(interactive "p")
(igrep-next-error (- argp)))
(defun igrep-first-error ()
"Like `first-error', but also highlight the matched text.
Highlighting only occurs if the compilation buffer is an *igrep* buffer.
See `igrep-next-error'."
(interactive)
(igrep-next-error '(4)))
(defun igrep-unhighlight ()
"Delete `igrep-highlight-overlay'."
(interactive)
(let ((buffers (if (local-variable-if-set-p 'igrep-highlight-overlay)
(buffer-list)
(list (current-buffer)))))
(while buffers
(save-excursion
(set-buffer (car buffers))
(if (overlayp igrep-highlight-overlay)
(delete-overlay igrep-highlight-overlay)))
(setq buffers (cdr buffers)))))
(defun igrep-kill-buffer-hook ()
"Delete the `igrep-highlight-overlay' when an *igrep* buffer is killed."
(if (or (equal mode-name "igrep")
(and (boundp 'name-of-mode)
(equal (symbol-value 'name-of-mode) "igrep")))
(add-hook 'kill-buffer-hook 'igrep-unhighlight nil t)))
(or (face-differs-from-default-p 'igrep-highlight)
(copy-face 'highlight 'igrep-highlight))
(add-hook 'compilation-mode-hook 'igrep-kill-buffer-hook)
(provide 'igrep-next-error)