find-dired.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. ;;; find-dired.el --- run a `find' command and dired the output
  2. ;; Copyright (C) 1992, 1994-1995, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Roland McGrath <roland@gnu.org>,
  4. ;; Sebastian Kremer <sk@thp.uni-koeln.de>
  5. ;; Maintainer: FSF
  6. ;; Keywords: unix
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Code:
  20. (require 'dired)
  21. (defgroup find-dired nil
  22. "Run a `find' command and dired the output."
  23. :group 'dired
  24. :prefix "find-")
  25. ;; FIXME this option does not really belong in this file, it's more general.
  26. ;; Eg cf some tests in grep.el.
  27. (defcustom find-exec-terminator
  28. (if (eq 0
  29. (ignore-errors
  30. (process-file find-program nil nil nil
  31. null-device "-exec" "echo" "{}" "+")))
  32. "+"
  33. (shell-quote-argument ";"))
  34. "String that terminates \"find -exec COMMAND {} \".
  35. The value should include any needed quoting for the shell.
  36. Common values are \"+\" and \"\\\\;\", with the former more efficient
  37. than the latter."
  38. :version "24.1"
  39. :group 'find-dired
  40. :type 'string)
  41. ;; find's -ls corresponds to these switches.
  42. ;; Note -b, at least GNU find quotes spaces etc. in filenames
  43. (defcustom find-ls-option
  44. (if (eq 0
  45. (ignore-errors
  46. (process-file find-program nil nil nil null-device "-ls")))
  47. (cons "-ls"
  48. (if (eq system-type 'berkeley-unix)
  49. "-gilsb"
  50. "-dilsb"))
  51. (cons
  52. (format "-exec ls -ld {} %s" find-exec-terminator)
  53. "-ld"))
  54. "A pair of options to produce and parse an `ls -l'-type list from `find'.
  55. This is a cons of two strings (FIND-OPTION . LS-SWITCHES).
  56. FIND-OPTION is the option (or options) passed to `find' to produce
  57. a file listing in the desired format. LS-SWITCHES is a set of
  58. `ls' switches that tell dired how to parse the output of `find'.
  59. The two options must be set to compatible values.
  60. For example, to use human-readable file sizes with GNU ls:
  61. \(\"-exec ls -ldh {} +\" . \"-ldh\")
  62. To use GNU find's inbuilt \"-ls\" option to list files:
  63. \(\"-ls\" . \"-dilsb\")
  64. since GNU find's output has the same format as using GNU ls with
  65. the options \"-dilsb\"."
  66. :version "24.1" ; add tests for -ls and -exec + support
  67. :type '(cons (string :tag "Find Option")
  68. (string :tag "Ls Switches"))
  69. :group 'find-dired)
  70. (defcustom find-ls-subdir-switches
  71. (if (string-match "-[a-z]*b" (cdr find-ls-option))
  72. "-alb"
  73. "-al")
  74. "`ls' switches for inserting subdirectories in `*Find*' buffers.
  75. This should contain the \"-l\" switch.
  76. Use the \"-F\" or \"-b\" switches if and only if you also use
  77. them for `find-ls-option'."
  78. :version "24.1" ; add -b test
  79. :type 'string
  80. :group 'find-dired)
  81. (defcustom find-grep-options
  82. (if (or (eq system-type 'berkeley-unix)
  83. (string-match "solaris2\\|irix" system-configuration))
  84. "-s" "-q")
  85. "Option to grep to be as silent as possible.
  86. On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
  87. On other systems, the closest you can come is to use `-l'."
  88. :type 'string
  89. :group 'find-dired)
  90. ;; This used to be autoloaded (see bug#4387).
  91. (defcustom find-name-arg
  92. (if read-file-name-completion-ignore-case
  93. "-iname"
  94. "-name")
  95. "Argument used to specify file name pattern.
  96. If `read-file-name-completion-ignore-case' is non-nil, -iname is used so that
  97. find also ignores case. Otherwise, -name is used."
  98. :type 'string
  99. :group 'find-dired
  100. :version "22.2")
  101. (defvar find-args nil
  102. "Last arguments given to `find' by \\[find-dired].")
  103. ;; History of find-args values entered in the minibuffer.
  104. (defvar find-args-history nil)
  105. (defvar dired-sort-inhibit)
  106. ;;;###autoload
  107. (defun find-dired (dir args)
  108. "Run `find' and go into Dired mode on a buffer of the output.
  109. The command run (after changing into DIR) is essentially
  110. find . \\( ARGS \\) -ls
  111. except that the car of the variable `find-ls-option' specifies what to
  112. use in place of \"-ls\" as the final argument."
  113. (interactive (list (read-directory-name "Run find in directory: " nil "" t)
  114. (read-string "Run find (with args): " find-args
  115. '(find-args-history . 1))))
  116. (let ((dired-buffers dired-buffers))
  117. ;; Expand DIR ("" means default-directory), and make sure it has a
  118. ;; trailing slash.
  119. (setq dir (file-name-as-directory (expand-file-name dir)))
  120. ;; Check that it's really a directory.
  121. (or (file-directory-p dir)
  122. (error "find-dired needs a directory: %s" dir))
  123. (switch-to-buffer (get-buffer-create "*Find*"))
  124. ;; See if there's still a `find' running, and offer to kill
  125. ;; it first, if it is.
  126. (let ((find (get-buffer-process (current-buffer))))
  127. (when find
  128. (if (or (not (eq (process-status find) 'run))
  129. (yes-or-no-p "A `find' process is running; kill it? "))
  130. (condition-case nil
  131. (progn
  132. (interrupt-process find)
  133. (sit-for 1)
  134. (delete-process find))
  135. (error nil))
  136. (error "Cannot have two processes in `%s' at once" (buffer-name)))))
  137. (widen)
  138. (kill-all-local-variables)
  139. (setq buffer-read-only nil)
  140. (erase-buffer)
  141. (setq default-directory dir
  142. find-args args ; save for next interactive call
  143. args (concat find-program " . "
  144. (if (string= args "")
  145. ""
  146. (concat
  147. (shell-quote-argument "(")
  148. " " args " "
  149. (shell-quote-argument ")")
  150. " "))
  151. (if (string-match "\\`\\(.*\\) {} \\(\\\\;\\|+\\)\\'"
  152. (car find-ls-option))
  153. (format "%s %s %s"
  154. (match-string 1 (car find-ls-option))
  155. (shell-quote-argument "{}")
  156. find-exec-terminator)
  157. (car find-ls-option))))
  158. ;; Start the find process.
  159. (shell-command (concat args "&") (current-buffer))
  160. ;; The next statement will bomb in classic dired (no optional arg allowed)
  161. (dired-mode dir (cdr find-ls-option))
  162. (let ((map (make-sparse-keymap)))
  163. (set-keymap-parent map (current-local-map))
  164. (define-key map "\C-c\C-k" 'kill-find)
  165. (use-local-map map))
  166. (make-local-variable 'dired-sort-inhibit)
  167. (setq dired-sort-inhibit t)
  168. (set (make-local-variable 'revert-buffer-function)
  169. `(lambda (ignore-auto noconfirm)
  170. (find-dired ,dir ,find-args)))
  171. ;; Set subdir-alist so that Tree Dired will work:
  172. (if (fboundp 'dired-simple-subdir-alist)
  173. ;; will work even with nested dired format (dired-nstd.el,v 1.15
  174. ;; and later)
  175. (dired-simple-subdir-alist)
  176. ;; else we have an ancient tree dired (or classic dired, where
  177. ;; this does no harm)
  178. (set (make-local-variable 'dired-subdir-alist)
  179. (list (cons default-directory (point-min-marker)))))
  180. (set (make-local-variable 'dired-subdir-switches) find-ls-subdir-switches)
  181. (setq buffer-read-only nil)
  182. ;; Subdir headlerline must come first because the first marker in
  183. ;; subdir-alist points there.
  184. (insert " " dir ":\n")
  185. ;; Make second line a ``find'' line in analogy to the ``total'' or
  186. ;; ``wildcard'' line.
  187. (insert " " args "\n")
  188. (setq buffer-read-only t)
  189. (let ((proc (get-buffer-process (current-buffer))))
  190. (set-process-filter proc (function find-dired-filter))
  191. (set-process-sentinel proc (function find-dired-sentinel))
  192. ;; Initialize the process marker; it is used by the filter.
  193. (move-marker (process-mark proc) 1 (current-buffer)))
  194. (setq mode-line-process '(":%s"))))
  195. (defun kill-find ()
  196. "Kill the `find' process running in the current buffer."
  197. (interactive)
  198. (let ((find (get-buffer-process (current-buffer))))
  199. (and find (eq (process-status find) 'run)
  200. (eq (process-filter find) (function find-dired-filter))
  201. (condition-case nil
  202. (delete-process find)
  203. (error nil)))))
  204. ;;;###autoload
  205. (defun find-name-dired (dir pattern)
  206. "Search DIR recursively for files matching the globbing pattern PATTERN,
  207. and run dired on those files.
  208. PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
  209. The command run (after changing into DIR) is
  210. find . -name 'PATTERN' -ls"
  211. (interactive
  212. "DFind-name (directory): \nsFind-name (filename wildcard): ")
  213. (find-dired dir (concat find-name-arg " " (shell-quote-argument pattern))))
  214. ;; This functionality suggested by
  215. ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
  216. ;; Subject: find-dired, lookfor-dired
  217. ;; Date: 10 May 91 17:50:00 GMT
  218. ;; Organization: University of Waterloo
  219. (defalias 'lookfor-dired 'find-grep-dired)
  220. ;;;###autoload
  221. (defun find-grep-dired (dir regexp)
  222. "Find files in DIR containing a regexp REGEXP and start Dired on output.
  223. The command run (after changing into DIR) is
  224. find . \\( -type f -exec `grep-program' `find-grep-options' \\
  225. -e REGEXP {} \\; \\) -ls
  226. where the car of the variable `find-ls-option' specifies what to
  227. use in place of \"-ls\" as the final argument."
  228. ;; Doc used to say "Thus ARG can also contain additional grep options."
  229. ;; i) Presumably ARG == REGEXP?
  230. ;; ii) No it can't have options, since it gets shell-quoted.
  231. (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
  232. ;; find -exec doesn't allow shell i/o redirections in the command,
  233. ;; or we could use `grep -l >/dev/null'
  234. ;; We use -type f, not ! -type d, to avoid getting screwed
  235. ;; by FIFOs and devices. I'm not sure what's best to do
  236. ;; about symlinks, so as far as I know this is not wrong.
  237. (find-dired dir
  238. (concat "-type f -exec " grep-program " " find-grep-options " -e "
  239. (shell-quote-argument regexp)
  240. " "
  241. (shell-quote-argument "{}")
  242. " "
  243. ;; Doesn't work with "+".
  244. (shell-quote-argument ";"))))
  245. (defun find-dired-filter (proc string)
  246. ;; Filter for \\[find-dired] processes.
  247. (let ((buf (process-buffer proc))
  248. (inhibit-read-only t))
  249. (if (buffer-name buf)
  250. (with-current-buffer buf
  251. (save-excursion
  252. (save-restriction
  253. (widen)
  254. (let ((buffer-read-only nil)
  255. (beg (point-max))
  256. (l-opt (and (consp find-ls-option)
  257. (string-match "l" (cdr find-ls-option))))
  258. (ls-regexp (concat "^ +[^ \t\r\n]+\\( +[^ \t\r\n]+\\) +"
  259. "[^ \t\r\n]+ +[^ \t\r\n]+\\( +[0-9]+\\)")))
  260. (goto-char beg)
  261. (insert string)
  262. (goto-char beg)
  263. (or (looking-at "^")
  264. (forward-line 1))
  265. (while (looking-at "^")
  266. (insert " ")
  267. (forward-line 1))
  268. ;; Convert ` ./FILE' to ` FILE'
  269. ;; This would lose if the current chunk of output
  270. ;; starts or ends within the ` ./', so back up a bit:
  271. (goto-char (- beg 3)) ; no error if < 0
  272. (while (search-forward " ./" nil t)
  273. (delete-region (point) (- (point) 2)))
  274. ;; Pad the number of links and file size. This is a
  275. ;; quick and dirty way of getting the columns to line up
  276. ;; most of the time, but it's not foolproof.
  277. (when l-opt
  278. (goto-char beg)
  279. (goto-char (line-beginning-position))
  280. (while (re-search-forward ls-regexp nil t)
  281. (replace-match (format "%4s" (match-string 1))
  282. nil nil nil 1)
  283. (replace-match (format "%9s" (match-string 2))
  284. nil nil nil 2)
  285. (forward-line 1)))
  286. ;; Find all the complete lines in the unprocessed
  287. ;; output and process it to add text properties.
  288. (goto-char (point-max))
  289. (if (search-backward "\n" (process-mark proc) t)
  290. (progn
  291. (dired-insert-set-properties (process-mark proc)
  292. (1+ (point)))
  293. (move-marker (process-mark proc) (1+ (point)))))))))
  294. ;; The buffer has been killed.
  295. (delete-process proc))))
  296. (defun find-dired-sentinel (proc state)
  297. ;; Sentinel for \\[find-dired] processes.
  298. (let ((buf (process-buffer proc))
  299. (inhibit-read-only t))
  300. (if (buffer-name buf)
  301. (with-current-buffer buf
  302. (let ((buffer-read-only nil))
  303. (save-excursion
  304. (goto-char (point-max))
  305. (insert "\n find " state)
  306. (forward-char -1) ;Back up before \n at end of STATE.
  307. (insert " at " (substring (current-time-string) 0 19))
  308. (forward-char 1)
  309. (setq mode-line-process
  310. (concat ":"
  311. (symbol-name (process-status proc))))
  312. ;; Since the buffer and mode line will show that the
  313. ;; process is dead, we can delete it now. Otherwise it
  314. ;; will stay around until M-x list-processes.
  315. (delete-process proc)
  316. (force-mode-line-update)))
  317. (message "find-dired %s finished." (current-buffer))))))
  318. (provide 'find-dired)
  319. ;;; find-dired.el ends here