Use external tool `w3m' for dumping html to plain texts.
[xwl-elisp.git] / dired-isearch.el
blob87a8598b96d3c6b7e72d2f1b914ce0deca627b4e
1 ;;; dired-isearch.el --- isearch in Dired
3 ;; Copyright (C) 2006, 2007 William Xu
5 ;; Author: William Xu <william.xwl@gmail.com>
6 ;; Version: 0.3
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3, or (at your option)
11 ;; any later version.
13 ;; EMMS is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with EMMS; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
23 ;;; Commentary:
25 ;; Do isearch in Dired but match only at file names.
27 ;; Recommended keybindings:
29 ;; (define-key dired-mode-map (kbd "C-s") 'dired-isearch-forward)
30 ;; (define-key dired-mode-map (kbd "C-r") 'dired-isearch-backward)
31 ;; (define-key dired-mode-map (kbd "ESC C-s") 'dired-isearch-forward-regexp)
32 ;; (define-key dired-mode-map (kbd "ESC C-r") 'dired-isearch-backward-regexp)
34 ;;; Bugs:
36 ;; - search string starting with a "^" causes some infinite loop
37 ;; - search string with ".*" matches at non-filenames
39 ;;; Code:
41 (require 'dired)
43 ;;; User Interfaces
45 ;;;###autoload
46 (defun dired-isearch-forward (&optional regexp-p no-recursive-edit)
47 "In Dired, run `isearch-forward' but match only at file names."
48 (interactive)
49 (let ((isearch-search-fun-function 'dired-isearch-search-fun-function))
50 (isearch-forward regexp-p no-recursive-edit)))
52 ;;;###autoload
53 (defun dired-isearch-backward (&optional regexp-p no-recursive-edit)
54 "In Dired, run `isearch-backward' but match only at file names."
55 (interactive)
56 (let ((isearch-search-fun-function 'dired-isearch-search-fun-function))
57 (isearch-backward regexp-p no-recursive-edit)))
59 ;;;###autoload
60 (defun dired-isearch-forward-regexp (&optional not-regexp no-recursive-edit)
61 "In Dired, run `isearch-forward-regexp' but match only at file names."
62 (interactive)
63 (let ((isearch-search-fun-function 'dired-isearch-search-fun-function))
64 (isearch-forward-regexp not-regexp no-recursive-edit)))
66 ;;;###autoload
67 (defun dired-isearch-backward-regexp (&optional not-regexp no-recursive-edit)
68 "In Dired, run `isearch-backward-regexp' but match only at file names."
69 (interactive)
70 (let ((isearch-search-fun-function 'dired-isearch-search-fun-function))
71 (isearch-backward-regexp not-regexp no-recursive-edit)))
74 ;;; Low Level Functions
76 ;; by Stefan Monnier
78 ;; (defun dired-isearch-search-fun-function () 'dired-isearch-search)
80 ;; (defun dired-isearch-search (&rest args)
81 ;; (let ((fun (let ((isearch-search-fun-function nil))
82 ;; (isearch-search-fun)))
83 ;; point)
84 ;; (while (and (setq point (apply fun args))
85 ;; ;; Use `help-echo' instead of `dired-filename' so as to
86 ;; ;; also work in Tramp dired buffers.
87 ;; (not (get-text-property (1- point) 'help-echo)))
88 ;; ;; (forward-char 1)
89 ;; )
90 ;; point))
92 ;; (define-minor-mode dired-isearch-filenames-mode
93 ;; "Only match filenames in isearch."
94 ;; :global nil
95 ;; (if dired-isearch-filenames-mode
96 ;; (set (make-local-variable 'isearch-search-fun-function)
97 ;; 'dired-isearch-search-fun-function)
98 ;; (kill-local-variable 'isearch-search-fun-function)))
100 (defun dired-isearch-search-fun-function ()
101 "Return the isearch function in Dired."
102 (cond
103 (isearch-word
104 (if isearch-forward 'dired-word-search-forward 'dired-word-search-backward))
105 (isearch-regexp
106 (if isearch-forward 'dired-re-search-forward 'dired-re-search-backward))
108 (if isearch-forward 'dired-search-forward 'dired-search-backward))))
110 (defun dired-search-forward (string &optional bound noerror count)
111 "In Dired, run `search-forward' but match only at file names."
112 (let ((point (search-forward string bound noerror count)))
113 ;; Use 'help-echo instead of 'dired-filename so as to also work
114 ;; in tramp dired buffers.
115 (while (and point (not (get-text-property (1- point) 'help-echo)))
116 (setq point (search-forward string bound noerror count)))
117 point))
119 (defun dired-search-backward (string &optional bound noerror count)
120 "In Dired, run `search-backward' but match only at file names."
121 (let ((point (search-backward string bound noerror count)))
122 (while (and point (not (get-text-property point 'help-echo)))
123 (setq point (search-backward string bound noerror count)))
124 point))
126 (defun dired-re-search-forward (regexp &optional bound noerror count)
127 "In Dired, run `re-search-forward' but match only at file names."
128 (let ((point (re-search-forward regexp bound noerror count)))
129 (while (and point (not (get-text-property (1- point) 'help-echo)))
130 (setq point (re-search-forward regexp bound noerror count)))
131 point))
133 (defun dired-re-search-backward (regexp &optional bound noerror count)
134 "In Dired, run `re-search-backward' but match only at file names."
135 (let ((point (re-search-backward regexp bound noerror count)))
136 (while (and point (not (get-text-property point 'help-echo)))
137 (setq point (re-search-backward regexp bound noerror count)))
138 point))
140 (defun dired-word-search-forward (string &optional bound noerror count)
141 "In Dired, run `word-search-forward' but match only at file names."
142 (let ((point (word-search-forward string bound noerror count)))
143 (while (and point (not (get-text-property (1- point) 'help-echo)))
144 (setq point (word-search-forward string bound noerror count)))
145 point))
147 (defun dired-word-search-backward (string &optional bound noerror count)
148 "In Dired, run `word-search-backward' but match only at file names."
149 (let ((point (word-search-backward string bound noerror count)))
150 (while (and point (not (get-text-property point 'help-echo)))
151 (setq point (word-search-backward string bound noerror count)))
152 point))
154 (provide 'dired-isearch)
156 ;;; dired-isearch.el ends here