emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: isearch multiple buffers


From: Juri Linkov
Subject: Re: isearch multiple buffers
Date: Sun, 20 Jul 2008 03:40:55 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (x86_64-pc-linux-gnu)

Using isearch on multiple buffers is a feature I'd like to finish.
The current design prevents implementing global isearch operations
on a list of given files because isearch-buffers-minor-mode is a
minor mode and even after creating a global mode it will conflict
with buffers where isearch-buffers-minor-mode is enabled.

So I completely redesigned this feature, and the resulting patch is
below.  It provides now a clean and convenient interface for activating
Isearch on a set of files or buffers.  The only configurable variable
that defines what to search is `multi-isearch-next-buffer-function'.
In some modes like changelog-mode it is set to a buffer-local value, and
in global isearch on a list of files it is let-bound before starting
Isearch.  Its value it saved to an internal variable to avoid harmful
effects when Isearch arrives to a buffer with the different value of
this variable.  This never happens with a new patch since it operates
with the initial value of `multi-isearch-next-buffer-function'.

There are four new top commands:

    multi-isearch-forward-buffers
    multi-isearch-forward-buffers-regexp
    multi-isearch-forward-files
    multi-isearch-forward-files-regexp

whose single input argument accepts a list of files/buffers to search.
They then switch to the first file/buffer from this list and start
Isearch in it.

The first packages to use this feature are dired and buff-menu.
In Dired a new key `M-s A' (mnemonic for the existing `A') uses
Isearch to search marked Dired files.  In the Buffer List (C-x C-b)
\M-smb and \M-smr start string/regexp Isearch with all of the buffers
marked with `>' in the order they appear in that list.

This change also simplifies changelog-mode since now there is no need
to enable a minor mode.

It also adds the Isearch indication "multi" in the echo area when
multi-search is active.

And finally the package name prefix was changed from `isearch-buffers-'
to the better prefix `multi-isearch-' (by analogy with `multi-occur',
`multi-replace' etc) and the file name was renamed from `isearch-multi.el'
to `misearch.el' to avoid dos file name clashes with `isearch-x.el'.
So the whole file misearch.el is attaches below.

Index: lisp/dired.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/dired.el,v
retrieving revision 1.402
diff -c -r1.402 dired.el
*** lisp/dired.el       19 Jul 2008 23:55:41 -0000      1.402
--- lisp/dired.el       20 Jul 2008 00:31:27 -0000
***************
*** 1196,1201 ****
--- 1197,1203 ----
      (define-key map "~" 'dired-flag-backup-files)
      (define-key map "&" 'dired-flag-garbage-files)
      ;; Upper case keys (except !) for operating on the marked files
+     (define-key map "\M-sA" 'dired-do-isearch-regexp)
      (define-key map "A" 'dired-do-search)
      (define-key map "C" 'dired-do-copy)
      (define-key map "B" 'dired-do-byte-compile)

Index: lisp/dired-aux.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/dired-aux.el,v
retrieving revision 1.170
diff -c -r1.170 dired-aux.el
*** lisp/dired-aux.el   6 May 2008 07:57:30 -0000       1.170
--- lisp/dired-aux.el   20 Jul 2008 00:32:42 -0000
***************
*** 2276,2281 ****
--- 2276,2295 ----
  ;; Functions for searching in tags style among marked files.
  
  ;;;###autoload
+ (defun dired-do-isearch ()
+   "Search for a string through all marked files using Isearch."
+   (interactive)
+   (multi-isearch-forward-files
+    (dired-get-marked-files nil nil 'dired-nondirectory-p)))
+ 
+ ;;;###autoload
+ (defun dired-do-isearch-regexp ()
+   "Search for a regexp through all marked files using Isearch."
+   (interactive)
+   (multi-isearch-forward-files-regexp
+    (dired-get-marked-files nil nil 'dired-nondirectory-p)))
+ 
+ ;;;###autoload
  (defun dired-do-search (regexp)
    "Search through all marked files for a match for REGEXP.
  Stops when a match is found.

Index: lisp/buff-menu.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/buff-menu.el,v
retrieving revision 1.114
diff -c -r1.114 buff-menu.el
*** lisp/buff-menu.el   25 Jun 2008 15:13:03 -0000      1.114
--- lisp/buff-menu.el   20 Jul 2008 00:29:01 -0000
***************
*** 154,159 ****
--- 154,181 ----
      map)
    "Local keymap for `Buffer-menu-mode' buffers.")
  
+ (define-key Buffer-menu-mode-map "\M-smb" 'Buffer-menu-isearch-buffers)
+ (define-key Buffer-menu-mode-map "\M-smr" 'Buffer-menu-isearch-buffers-regexp)
+ 
+ (defun Buffer-menu-marked-buffers ()
+   "Return a list of buffers marked with the 
\\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
+   (interactive)
+   (let (buffers)
+     (Buffer-menu-beginning)
+     (while (re-search-forward "^>" nil t)
+       (setq buffers (cons (Buffer-menu-buffer t) buffers)))
+     (nreverse buffers)))
+ 
+ (defun Buffer-menu-isearch-buffers ()
+   "Search for a string through all marked buffers using Isearch."
+   (interactive)
+   (multi-isearch-forward-buffers (Buffer-menu-marked-buffers)))
+ 
+ (defun Buffer-menu-isearch-buffers-regexp ()
+   "Search for a regexp through all marked buffers using Isearch."
+   (interactive)
+   (multi-isearch-forward-buffers-regexp (Buffer-menu-marked-buffers)))
+ 
  ;; Buffer Menu mode is suitable only for specially formatted data.
  (put 'Buffer-menu-mode 'mode-class 'special)
  
Index: lisp/add-log.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/add-log.el,v
retrieving revision 1.214
diff -c -r1.214 add-log.el
*** lisp/add-log.el     14 Jul 2008 08:15:30 -0000      1.214
--- lisp/add-log.el     20 Jul 2008 00:32:02 -0000
***************
*** 994,1006 ****
    (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
    (set (make-local-variable 'font-lock-defaults)
         '(change-log-font-lock-keywords t nil nil backward-paragraph))
!   (set (make-local-variable 'isearch-buffers-next-buffer-function)
         'change-log-next-buffer)
    (set (make-local-variable 'beginning-of-defun-function) 
         'change-log-beginning-of-defun)
    (set (make-local-variable 'end-of-defun-function) 
!        'change-log-end-of-defun)
!   (isearch-buffers-minor-mode))
  
  (defun change-log-next-buffer (&optional buffer wrap)
    "Return the next buffer in the series of ChangeLog file buffers.
--- 994,1005 ----
    (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
    (set (make-local-variable 'font-lock-defaults)
         '(change-log-font-lock-keywords t nil nil backward-paragraph))
!   (set (make-local-variable 'multi-isearch-next-buffer-function)
         'change-log-next-buffer)
    (set (make-local-variable 'beginning-of-defun-function) 
         'change-log-beginning-of-defun)
    (set (make-local-variable 'end-of-defun-function) 
!        'change-log-end-of-defun))
  
  (defun change-log-next-buffer (&optional buffer wrap)
    "Return the next buffer in the series of ChangeLog file buffers.

Index: lisp/isearch.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/isearch.el,v
retrieving revision 1.322
diff -c -r1.322 isearch.el
*** lisp/isearch.el     25 Jun 2008 20:21:36 -0000      1.322
--- lisp/isearch.el     20 Jul 2008 00:33:35 -0000
***************
*** 2111,2116 ****
--- 2111,2117 ----
                   (if isearch-wrapped "wrapped ")
                   (if isearch-word "word " "")
                   (if isearch-regexp "regexp " "")
+                  (if multi-isearch-next-buffer-current-function "multi " "")
                   (if nonincremental "search" "I-search")
                   (if isearch-forward "" " backward")
                   (if current-input-method
***************
*** 2179,2187 ****
      (when pos1
        ;; When using multiple buffers isearch, switch to the new buffer here,
        ;; because `save-excursion' above doesn't allow doing it inside funcall.
!       (if (and isearch-buffers-next-buffer-function
!              (buffer-live-p isearch-buffers-current-buffer))
!         (switch-to-buffer isearch-buffers-current-buffer))
        (goto-char pos1))
      pos1))
  
--- 2180,2188 ----
      (when pos1
        ;; When using multiple buffers isearch, switch to the new buffer here,
        ;; because `save-excursion' above doesn't allow doing it inside funcall.
!       (if (and multi-isearch-next-buffer-current-function
!              (buffer-live-p multi-isearch-current-buffer))
!         (switch-to-buffer multi-isearch-current-buffer))
        (goto-char pos1))
      pos1))
  
;;; misearch.el --- isearch extensions for multi-buffer search

;; Copyright (C) 2007, 2008  Free Software Foundation, Inc.

;; Author: Juri Linkov <address@hidden>
;; Keywords: matching

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This file adds more dimensions to the search space.  It implements
;; various features that extend isearch.  One of them is an ability to
;; search through multiple buffers.

;;; Code:

;;; Search multiple buffers

;;;###autoload (add-hook 'isearch-mode-hook 'multi-isearch-setup)

(defgroup multi-isearch nil
  "Using isearch to search through multiple buffers."
  :version "23.1"
  :group 'isearch)

(defcustom multi-isearch-search t
  "Non-nil enables searching multiple related buffers, in certain modes."
  :type 'boolean
  :version "23.1"
  :group 'multi-isearch)

(defcustom multi-isearch-pause t
  "A choice defining where to pause the search.
If the value is nil, don't pause before going to the next buffer.
If the value is `initial', pause only after a failing search in the
initial buffer.
If t, pause in all buffers that contain the search string."
  :type '(choice
          (const :tag "Don't pause" nil)
          (const :tag "Only in initial buffer" initial)
          (const :tag "All buffers" t))
  :version "23.1"
  :group 'multi-isearch)

;;;###autoload
(defvar multi-isearch-next-buffer-function nil
  "Function to call to get the next buffer to search.

When this variable is set to a function that returns a buffer, then
after typing another \\[isearch-forward] or \\[isearch-backward] \
at a failing search, the search goes
to the next buffer in the series and continues searching for the
next occurrence.

The first argument of this function is the current buffer where the
search is currently searching.  It defines the base buffer relative to
which this function should find the next buffer.  When the isearch
direction is backward (when `isearch-forward' is nil), this function
should return the previous buffer to search.  If the second argument of
this function WRAP is non-nil, then it should return the first buffer
in the series; and for the backward search, it should return the last
buffer in the series.")

;;;###autoload
(defvar multi-isearch-next-buffer-current-function nil
  "The currently active function to get the next buffer to search.
Initialized from `multi-isearch-next-buffer-function' when
Isearch starts.")

;;;###autoload
(defvar multi-isearch-current-buffer nil
  "The buffer where the search is currently searching.
The value is nil when the search still is in the initial buffer.")

(defvar multi-isearch-orig-search-fun nil)
(defvar multi-isearch-orig-wrap nil)
(defvar multi-isearch-orig-push-state nil)


;;;###autoload
(defun multi-isearch-setup ()
  "Set up isearch to search multiple buffers.
Intended to be added to `isearch-mode-hook'."
  (when (and multi-isearch-search
             multi-isearch-next-buffer-function)
    (setq multi-isearch-current-buffer nil
          multi-isearch-next-buffer-current-function
          multi-isearch-next-buffer-function
          multi-isearch-orig-search-fun
          (default-value 'isearch-search-fun-function)
          multi-isearch-orig-wrap
          (default-value 'isearch-wrap-function)
          multi-isearch-orig-push-state
          (default-value 'isearch-push-state-function))
    (setq-default isearch-search-fun-function 'multi-isearch-search-fun
                  isearch-wrap-function       'multi-isearch-wrap
                  isearch-push-state-function 'multi-isearch-push-state)
    (add-hook 'isearch-mode-end-hook  'multi-isearch-end)))

(defun multi-isearch-end ()
  "Clean up the multi-buffer search after terminating isearch."
  (setq multi-isearch-current-buffer nil
        multi-isearch-next-buffer-current-function nil)
  (setq-default isearch-search-fun-function multi-isearch-orig-search-fun
                isearch-wrap-function       multi-isearch-orig-wrap
                isearch-push-state-function multi-isearch-orig-push-state)
  (remove-hook 'isearch-mode-end-hook  'multi-isearch-end))

(defun multi-isearch-search-fun ()
  "Return the proper search function, for isearch in multiple buffers."
  (lambda (string bound noerror)
    (let ((search-fun
           ;; Use standard functions to search within one buffer
           (cond
            (isearch-word
             (if isearch-forward 'word-search-forward 'word-search-backward))
            (isearch-regexp
             (if isearch-forward 're-search-forward 're-search-backward))
            (t
             (if isearch-forward 'search-forward 'search-backward))))
          found buffer)
      (or
       ;; 1. First try searching in the initial buffer
       (let ((res (funcall search-fun string bound noerror)))
         ;; Reset wrapping for all-buffers pause after successful search
         (if (and res (eq multi-isearch-pause t))
             (setq multi-isearch-current-buffer nil))
         res)
       ;; 2. If the above search fails, start visiting next/prev buffers
       ;; successively, and search the string in them.  Do this only
       ;; when bound is nil (i.e. not while lazy-highlighting search
       ;; strings in the current buffer).
       (when (and (not bound) multi-isearch-search)
         ;; If no-pause or there was one attempt to leave the current buffer
         (if (or (null multi-isearch-pause)
                 (and multi-isearch-pause multi-isearch-current-buffer))
             (condition-case nil
                 (progn
                   (while (not found)
                     ;; Find the next buffer to search
                     (setq buffer (funcall 
multi-isearch-next-buffer-current-function
                                           buffer))
                     (with-current-buffer buffer
                       (goto-char (if isearch-forward (point-min) (point-max)))
                       (setq isearch-barrier (point) isearch-opoint (point))
                       ;; After visiting the next/prev buffer search the
                       ;; string in it again, until the function in
                       ;; multi-isearch-next-buffer-current-function raises
                       ;; an error at the beginning/end of the buffer sequence.
                       (setq found (funcall search-fun string bound noerror))))
                   ;; Set buffer for isearch-search-string to switch
                   (if buffer (setq multi-isearch-current-buffer buffer))
                   ;; Return point of the new search result
                   found)
               ;; Return nil when multi-isearch-next-buffer-current-function 
fails
               (error nil))
           (signal 'search-failed (list string "Repeat for next buffer"))))))))

(defun multi-isearch-wrap ()
  "Wrap the multiple buffers search when search is failed.
Switch buffer to the first buffer for a forward search,
or to the last buffer for a backward search.
Set `multi-isearch-current-buffer' to the current buffer to display
the isearch suffix message [initial buffer] only when isearch leaves
the initial buffer."
  (if (or (null multi-isearch-pause)
          (and multi-isearch-pause multi-isearch-current-buffer))
      (progn
        (switch-to-buffer
         (setq multi-isearch-current-buffer
               (funcall multi-isearch-next-buffer-current-function
                        (current-buffer) t)))
        (goto-char (if isearch-forward (point-min) (point-max))))
    (setq multi-isearch-current-buffer (current-buffer))
    (setq isearch-wrapped nil)))

(defun multi-isearch-push-state ()
  "Save a function restoring the state of multiple buffers search.
Save the current buffer to the additional state parameter in the
search status stack."
  `(lambda (cmd)
     (multi-isearch-pop-state cmd ,(current-buffer))))

(defun multi-isearch-pop-state (cmd buffer)
  "Restore the multiple buffers search state.
Switch to the buffer restored from the search status stack."
  (unless (equal buffer (current-buffer))
    (switch-to-buffer (setq multi-isearch-current-buffer buffer))))


;;; Global multi-buffer search invocations

(defvar multi-isearch-buffer-list nil)

(defun multi-isearch-next-buffer-from-list (&optional buffer wrap)
  "Return the next buffer in the series of ChangeLog file buffers.
This function is used for multiple buffers isearch.
A sequence of buffers is formed by ChangeLog files with decreasing
numeric file name suffixes in the directory of the initial ChangeLog
file were isearch was started."
  (let ((buffers (if isearch-forward
                     multi-isearch-buffer-list
                   (reverse multi-isearch-buffer-list))))
    (if wrap
        (car buffers)
      (cadr (member (or buffer (current-buffer)) buffers)))))

;;;###autoload
(defun multi-isearch-forward-buffers (buffers)
  "Start multi-buffer Isearch on a list of BUFFERS."
  (let ((multi-isearch-next-buffer-function
         'multi-isearch-next-buffer-from-list)
        (multi-isearch-buffer-list buffers))
    (switch-to-buffer (car buffers))
    (goto-char (if isearch-forward (point-min) (point-max)))
    (isearch-forward)))

;;;###autoload
(defun multi-isearch-forward-buffers-regexp (buffers)
  "Start multi-buffer regexp Isearch on a list of BUFFERS."
  (let ((multi-isearch-next-buffer-function
         'multi-isearch-next-buffer-from-list)
        (multi-isearch-buffer-list buffers))
    (switch-to-buffer (car buffers))
    (goto-char (if isearch-forward (point-min) (point-max)))
    (isearch-forward-regexp)))


;;; Global multi-file search invocations

(defvar multi-isearch-file-list nil)

(defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap)
  "Return the next buffer in the series of ChangeLog file buffers.
This function is used for multiple buffers isearch.
A sequence of buffers is formed by ChangeLog files with decreasing
numeric file name suffixes in the directory of the initial ChangeLog
file were isearch was started."
  (let ((files (if isearch-forward
                   multi-isearch-file-list
                 (reverse multi-isearch-file-list))))
    (find-file-noselect
     (if wrap
         (car files)
       (cadr (member (buffer-file-name buffer) files))))))

;;;###autoload
(defun multi-isearch-forward-files (files)
  "Start multi-buffer Isearch on a list of FILES."
  (let ((multi-isearch-next-buffer-function
         'multi-isearch-next-file-buffer-from-list)
        (multi-isearch-file-list files))
    (find-file (car files))
    (goto-char (if isearch-forward (point-min) (point-max)))
    (isearch-forward)))

;;;###autoload
(defun multi-isearch-forward-files-regexp (files)
  "Start multi-buffer regexp Isearch on a list of FILES."
  (let ((multi-isearch-next-buffer-function
         'multi-isearch-next-file-buffer-from-list)
        (multi-isearch-file-list files))
    (find-file (car files))
    (goto-char (if isearch-forward (point-min) (point-max)))
    (isearch-forward-regexp)))


(provide 'multi-isearch)

;; arch-tag: a6d38ffa-4d14-4e39-8ac6-46af9d6a6773
;;; misearch.el ends here

-- 
Juri Linkov
http://www.jurta.org/emacs/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]