OccurFromIsearch

This page refers to an older version of Emacs!

These days, you can use ‘M-s o’ while using isearch to run occur on the current search term!


Create an OccurBuffer from IncrementalSearch using the current string (or regexp) being seached, to find all matches of the pattern in the current buffer.

    (defun isearch-occur ()
      "Invoke `occur' from within isearch."
      (interactive)
      (let ((case-fold-search isearch-case-fold-search))
        (occur (if isearch-regexp isearch-string (regexp-quote isearch-string)))))

Assign the command to a key in Isearch:

    (define-key isearch-mode-map (kbd "C-o") 'isearch-occur)

Note that in Emacs 22 all Isearch matches are highlighted anyway, but this can still be useful for accessing specific hits and for quickly seeing a summary of all the hits in a single buffer.

See also:

Cleanup When Quitting Isearch

When I found this I was quite happy but using it the occur buffer sometimes irritated me. The point is, when I start isearch, open the occur buffer and then quit the search I felt like the occur buffer should go away. Not only that, I wanted my old window configuration restored. Thus I came up with the following (with help from ScottJaderholm)

(defvar ska-isearch-occur-opened nil)
(defvar ska-isearch-window-configuration nil)

(defun ska-isearch-occur ()
  (interactive)
  (when (fboundp 'occur)
    (setq ska-isearch-occur-opened t)
    (let ((case-fold-search isearch-case-fold-search))
      (occur (if isearch-regexp isearch-string
               (regexp-quote isearch-string))))))

(defun ska-isearch-maybe-remove-occur-buffer ()
  "Restore window-configuration when quitting isearch.

This function is meant to be used together with a function storing the
window configuration into a variable and together with a setup opening
the occur buffer from within isearch.

This function ...

-  will do nothing if you you did not cancel the search,

- will kill the occur buffer if occur buffer was opened from
  isearch,

- will restore your old window configuration when you saved it in
  `isearch-mode-hook'."

  (interactive)
  (let ((occ-buffer (get-buffer "*Occur*")))
    (when (and ska-isearch-occur-opened
               isearch-mode-end-hook-quit
               (buffer-live-p occ-buffer))
      (kill-buffer occ-buffer)
      (when (and ska-isearch-window-configuration
                 (window-configuration-p (car ska-isearch-window-configuration)))
        (set-window-configuration (car ska-isearch-window-configuration))
        (goto-char (cadr ska-isearch-window-configuration))))))

(add-hook 'isearch-mode-hook 
          '(lambda ()
             (setq ska-isearch-window-configuration
                   (list (current-window-configuration) (point-marker)))))

(add-hook 'isearch-mode-end-hook 
          '(lambda ()
             (ska-isearch-maybe-remove-occur-buffer)
             (setq ska-isearch-occur-opened nil)))

(define-key isearch-mode-map (kbd "M-o") 'ska-isearch-occur)

Please note that at the time of this writing the code is very fresh not heavily tested.


CategorySearchAndReplace