123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- (require 'dired)
- (defgroup find-dired nil
- "Run a `find' command and dired the output."
- :group 'dired
- :prefix "find-")
- (defcustom find-exec-terminator
- (if (eq 0
- (ignore-errors
- (process-file find-program nil nil nil
- null-device "-exec" "echo" "{}" "+")))
- "+"
- (shell-quote-argument ";"))
- "String that terminates \"find -exec COMMAND {} \".
- The value should include any needed quoting for the shell.
- Common values are \"+\" and \"\\\\;\", with the former more efficient
- than the latter."
- :version "24.1"
- :group 'find-dired
- :type 'string)
- (defcustom find-ls-option
- (if (eq 0
- (ignore-errors
- (process-file find-program nil nil nil null-device "-ls")))
- (cons "-ls"
- (if (eq system-type 'berkeley-unix)
- "-gilsb"
- "-dilsb"))
- (cons
- (format "-exec ls -ld {} %s" find-exec-terminator)
- "-ld"))
- "A pair of options to produce and parse an `ls -l'-type list from `find'.
- This is a cons of two strings (FIND-OPTION . LS-SWITCHES).
- FIND-OPTION is the option (or options) passed to `find' to produce
- a file listing in the desired format. LS-SWITCHES is a set of
- `ls' switches that tell dired how to parse the output of `find'.
- The two options must be set to compatible values.
- For example, to use human-readable file sizes with GNU ls:
- \(\"-exec ls -ldh {} +\" . \"-ldh\")
- To use GNU find's inbuilt \"-ls\" option to list files:
- \(\"-ls\" . \"-dilsb\")
- since GNU find's output has the same format as using GNU ls with
- the options \"-dilsb\"."
- :version "24.1"
- :type '(cons (string :tag "Find Option")
- (string :tag "Ls Switches"))
- :group 'find-dired)
- (defcustom find-ls-subdir-switches
- (if (string-match "-[a-z]*b" (cdr find-ls-option))
- "-alb"
- "-al")
- "`ls' switches for inserting subdirectories in `*Find*' buffers.
- This should contain the \"-l\" switch.
- Use the \"-F\" or \"-b\" switches if and only if you also use
- them for `find-ls-option'."
- :version "24.1"
- :type 'string
- :group 'find-dired)
- (defcustom find-grep-options
- (if (or (eq system-type 'berkeley-unix)
- (string-match "solaris2\\|irix" system-configuration))
- "-s" "-q")
- "Option to grep to be as silent as possible.
- On Berkeley systems, this is `-s'; on Posix, and with GNU grep, `-q' does it.
- On other systems, the closest you can come is to use `-l'."
- :type 'string
- :group 'find-dired)
- (defcustom find-name-arg
- (if read-file-name-completion-ignore-case
- "-iname"
- "-name")
- "Argument used to specify file name pattern.
- If `read-file-name-completion-ignore-case' is non-nil, -iname is used so that
- find also ignores case. Otherwise, -name is used."
- :type 'string
- :group 'find-dired
- :version "22.2")
- (defvar find-args nil
- "Last arguments given to `find' by \\[find-dired].")
- (defvar find-args-history nil)
- (defvar dired-sort-inhibit)
- (defun find-dired (dir args)
- "Run `find' and go into Dired mode on a buffer of the output.
- The command run (after changing into DIR) is essentially
- find . \\( ARGS \\) -ls
- except that the car of the variable `find-ls-option' specifies what to
- use in place of \"-ls\" as the final argument."
- (interactive (list (read-directory-name "Run find in directory: " nil "" t)
- (read-string "Run find (with args): " find-args
- '(find-args-history . 1))))
- (let ((dired-buffers dired-buffers))
-
-
- (setq dir (file-name-as-directory (expand-file-name dir)))
-
- (or (file-directory-p dir)
- (error "find-dired needs a directory: %s" dir))
- (switch-to-buffer (get-buffer-create "*Find*"))
-
-
- (let ((find (get-buffer-process (current-buffer))))
- (when find
- (if (or (not (eq (process-status find) 'run))
- (yes-or-no-p "A `find' process is running; kill it? "))
- (condition-case nil
- (progn
- (interrupt-process find)
- (sit-for 1)
- (delete-process find))
- (error nil))
- (error "Cannot have two processes in `%s' at once" (buffer-name)))))
- (widen)
- (kill-all-local-variables)
- (setq buffer-read-only nil)
- (erase-buffer)
- (setq default-directory dir
- find-args args
- args (concat find-program " . "
- (if (string= args "")
- ""
- (concat
- (shell-quote-argument "(")
- " " args " "
- (shell-quote-argument ")")
- " "))
- (if (string-match "\\`\\(.*\\) {} \\(\\\\;\\|+\\)\\'"
- (car find-ls-option))
- (format "%s %s %s"
- (match-string 1 (car find-ls-option))
- (shell-quote-argument "{}")
- find-exec-terminator)
- (car find-ls-option))))
-
- (shell-command (concat args "&") (current-buffer))
-
- (dired-mode dir (cdr find-ls-option))
- (let ((map (make-sparse-keymap)))
- (set-keymap-parent map (current-local-map))
- (define-key map "\C-c\C-k" 'kill-find)
- (use-local-map map))
- (make-local-variable 'dired-sort-inhibit)
- (setq dired-sort-inhibit t)
- (set (make-local-variable 'revert-buffer-function)
- `(lambda (ignore-auto noconfirm)
- (find-dired ,dir ,find-args)))
-
- (if (fboundp 'dired-simple-subdir-alist)
-
-
- (dired-simple-subdir-alist)
-
-
- (set (make-local-variable 'dired-subdir-alist)
- (list (cons default-directory (point-min-marker)))))
- (set (make-local-variable 'dired-subdir-switches) find-ls-subdir-switches)
- (setq buffer-read-only nil)
-
-
- (insert " " dir ":\n")
-
-
- (insert " " args "\n")
- (setq buffer-read-only t)
- (let ((proc (get-buffer-process (current-buffer))))
- (set-process-filter proc (function find-dired-filter))
- (set-process-sentinel proc (function find-dired-sentinel))
-
- (move-marker (process-mark proc) 1 (current-buffer)))
- (setq mode-line-process '(":%s"))))
- (defun kill-find ()
- "Kill the `find' process running in the current buffer."
- (interactive)
- (let ((find (get-buffer-process (current-buffer))))
- (and find (eq (process-status find) 'run)
- (eq (process-filter find) (function find-dired-filter))
- (condition-case nil
- (delete-process find)
- (error nil)))))
- (defun find-name-dired (dir pattern)
- "Search DIR recursively for files matching the globbing pattern PATTERN,
- and run dired on those files.
- PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
- The command run (after changing into DIR) is
- find . -name 'PATTERN' -ls"
- (interactive
- "DFind-name (directory): \nsFind-name (filename wildcard): ")
- (find-dired dir (concat find-name-arg " " (shell-quote-argument pattern))))
- (defalias 'lookfor-dired 'find-grep-dired)
- (defun find-grep-dired (dir regexp)
- "Find files in DIR containing a regexp REGEXP and start Dired on output.
- The command run (after changing into DIR) is
- find . \\( -type f -exec `grep-program' `find-grep-options' \\
- -e REGEXP {} \\; \\) -ls
- where the car of the variable `find-ls-option' specifies what to
- use in place of \"-ls\" as the final argument."
-
-
-
- (interactive "DFind-grep (directory): \nsFind-grep (grep regexp): ")
-
-
-
-
-
- (find-dired dir
- (concat "-type f -exec " grep-program " " find-grep-options " -e "
- (shell-quote-argument regexp)
- " "
- (shell-quote-argument "{}")
- " "
-
- (shell-quote-argument ";"))))
- (defun find-dired-filter (proc string)
-
- (let ((buf (process-buffer proc))
- (inhibit-read-only t))
- (if (buffer-name buf)
- (with-current-buffer buf
- (save-excursion
- (save-restriction
- (widen)
- (let ((buffer-read-only nil)
- (beg (point-max))
- (l-opt (and (consp find-ls-option)
- (string-match "l" (cdr find-ls-option))))
- (ls-regexp (concat "^ +[^ \t\r\n]+\\( +[^ \t\r\n]+\\) +"
- "[^ \t\r\n]+ +[^ \t\r\n]+\\( +[0-9]+\\)")))
- (goto-char beg)
- (insert string)
- (goto-char beg)
- (or (looking-at "^")
- (forward-line 1))
- (while (looking-at "^")
- (insert " ")
- (forward-line 1))
-
-
-
- (goto-char (- beg 3))
- (while (search-forward " ./" nil t)
- (delete-region (point) (- (point) 2)))
-
-
-
- (when l-opt
- (goto-char beg)
- (goto-char (line-beginning-position))
- (while (re-search-forward ls-regexp nil t)
- (replace-match (format "%4s" (match-string 1))
- nil nil nil 1)
- (replace-match (format "%9s" (match-string 2))
- nil nil nil 2)
- (forward-line 1)))
-
-
- (goto-char (point-max))
- (if (search-backward "\n" (process-mark proc) t)
- (progn
- (dired-insert-set-properties (process-mark proc)
- (1+ (point)))
- (move-marker (process-mark proc) (1+ (point)))))))))
-
- (delete-process proc))))
- (defun find-dired-sentinel (proc state)
-
- (let ((buf (process-buffer proc))
- (inhibit-read-only t))
- (if (buffer-name buf)
- (with-current-buffer buf
- (let ((buffer-read-only nil))
- (save-excursion
- (goto-char (point-max))
- (insert "\n find " state)
- (forward-char -1)
- (insert " at " (substring (current-time-string) 0 19))
- (forward-char 1)
- (setq mode-line-process
- (concat ":"
- (symbol-name (process-status proc))))
-
-
-
- (delete-process proc)
- (force-mode-line-update)))
- (message "find-dired %s finished." (current-buffer))))))
- (provide 'find-dired)
|