[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: isearch in Dired
From: |
Juri Linkov |
Subject: |
Re: isearch in Dired |
Date: |
Wed, 23 Jul 2008 23:34:07 +0300 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (x86_64-pc-linux-gnu) |
Isearch in Dired limited to filenames only is a good feature, so I'd like
to finish it with the patch below.
The main point is that it doesn't change default Isearch key bindings
C-s and C-M-s. It provides two ways to limit Isearch to filenames:
1. new user option `dired-isearch-filenames'.
It is nil by default, but when custimized to non-nil,
C-s and C-M-s search only in filenames.
2. new commands `'dired-do-isearch-filenames' and
`dired-do-isearch-regexp-filenames' bound to the
keys `M-s f C-s' and `M-s f M-C-s'.
Two similar key bindings from another patch are for
demonstration purposes: `M-s a C-s' and `M-s a M-C-s'
are placed on the same prefix key and they start Isearch
in all marked files.
For this feature to work reliably it was necessary to put text
properties `filename' on file name areas. Tramp already puts it
in Tramp buffer, and it seems the `filename' property in all Dired
buffers doesn't conflict with Tramp.
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 23 Jul 2008 20:33:44 -0000
***************
*** 1068,1074 ****
(dired-move-to-end-of-filename)
(point))
'(mouse-face highlight
! help-echo "mouse-2: visit this file in other window")))
(error nil))
(forward-line 1))))
--- 1068,1075 ----
(dired-move-to-end-of-filename)
(point))
'(mouse-face highlight
! help-echo "mouse-2: visit this file in other window"
! filename t)))
(error nil))
(forward-line 1))))
***************
*** 1295,1300 ****
--- 1297,1307 ----
;; hiding
(define-key map "$" 'dired-hide-subdir)
(define-key map "\M-$" 'dired-hide-all)
+ ;; isearch
+ (define-key map (kbd "M-s a C-s") 'dired-do-isearch-marked-files)
+ (define-key map (kbd "M-s a M-C-s") 'dired-do-isearch-regexp-marked-files)
+ (define-key map (kbd "M-s f C-s") 'dired-do-isearch-filenames)
+ (define-key map (kbd "M-s f M-C-s") 'dired-do-isearch-regexp-filenames)
;; misc
(define-key map "\C-x\C-q" 'dired-toggle-read-only)
(define-key map "?" 'dired-summary)
***************
*** 1713,1718 ****
--- 1720,1726 ----
(when (featurep 'dnd)
(set (make-local-variable 'dnd-protocol-alist)
(append dired-dnd-protocol-alist dnd-protocol-alist)))
+ (add-hook 'isearch-mode-hook 'dired-isearch-filenames-setup nil t)
(run-mode-hooks 'dired-mode-hook))
;; Idiosyncratic dired commands that don't deal with marks.
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 23 Jul 2008 20:33:59 -0000
***************
*** 2273,2281 ****
--- 2293,2363 ----
;;;###end dired-ins.el
+ ;; Search only in file names in the Dired buffer.
+
+ (defcustom dired-isearch-filenames nil
+ "*If non-nil, Isearch in Dired matches only file names."
+ :version "23.1"
+ :type '(choice (const :tag "No restrictions" nil)
+ (const :tag "Isearch only in file names" filename))
+ :group 'dired)
+
+ (defvar dired-isearch-orig-success-function nil)
+
+ (defun dired-isearch-filenames-setup ()
+ "Set up isearch to search in Dired file names.
+ Intended to be added to `isearch-mode-hook'."
+ (when dired-isearch-filenames
+ (setq dired-isearch-orig-success-function
+ (default-value 'isearch-success-function))
+ (setq-default isearch-success-function 'dired-isearch-success-function)
+ (add-hook 'isearch-mode-end-hook 'dired-isearch-filenames-end nil t)))
+
+ (defun dired-isearch-filenames-end ()
+ "Clean up the Dired file name search after terminating isearch."
+ (setq-default isearch-success-function dired-isearch-orig-success-function)
+ (remove-hook 'isearch-mode-end-hook 'dired-isearch-filenames-end t))
+
+ (defun dired-isearch-success-function (beg end)
+ "Isearch only text that have a property specified in
`dired-isearch-filenames'."
+ (and (isearch-success-function-default beg end)
+ (if dired-isearch-filenames
+ (text-property-not-all (min beg end) (max beg end)
+ 'filename nil)
+ t)))
+
+ ;;;###autoload
+ (defun dired-do-isearch-filenames ()
+ "Search for a string only in file names in the Dired buffer."
+ (interactive)
+ (let ((dired-isearch-filenames t))
+ (isearch-forward)))
+
+ ;;;###autoload
+ (defun dired-do-isearch-regexp-filenames ()
+ "Search for a regexp only in file names in the Dired buffer."
+ (interactive)
+ (let ((dired-isearch-filenames t))
+ (isearch-forward-regexp)))
+
+
;; Functions for searching in tags style among marked files.
--
Juri Linkov
http://www.jurta.org/emacs/
- Re: isearch in Dired,
Juri Linkov <=