find-dired.el 13 KB

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