home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / dired / find-dired.el < prev    next >
Encoding:
Text File  |  1992-08-18  |  7.7 KB  |  212 lines

  1. ;;; find-dired.el -- Run a `find' command and dired the output
  2. ;;; Copyright (C) 1991 Roland McGrath
  3.  
  4. (defconst find-dired-version (substring "!Revision: 1.14 !" 11 -2)
  5.   "Id: find-dired.el,v 1.14 1992/04/16 14:22:07 sk RelBeta ")
  6.  
  7. ;;; This program is free software; you can redistribute it and/or modify
  8. ;;; it under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 2, or (at your option)
  10. ;;; any later version.
  11. ;;;
  12. ;;; This program 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. ;;;
  17. ;;; A copy of the GNU General Public License can be obtained from this
  18. ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
  19. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  20. ;;; 02139, USA.
  21. ;;;
  22. ;; LISPDIR ENTRY for the Elisp Archive ===============================
  23. ;;    LCD Archive Entry:
  24. ;;    find-dired|Roland McGrath, Sebastian Kremer
  25. ;;    |roland@gnu.ai.mit.edu, sk@thp.uni-koeln.de
  26. ;;    |Run a `find' command and dired the output
  27. ;;    |Date: 1992/04/16 14:22:07 |Revision: 1.14 |
  28.  
  29. ;; INSTALLATION ======================================================
  30.  
  31. ;; To use this file, byte-compile it, install it somewhere in your
  32. ;; load-path, and put:
  33.  
  34. ;;   (autoload 'find-dired "find-dired" nil t)
  35. ;;   (autoload 'find-name-dired "find-dired" nil t)
  36. ;;   (autoload 'find-grep-dired "find-dired" nil t)
  37.  
  38. ;; in your ~/.emacs, or site-init.el, etc.
  39.  
  40. ;; To bind it to a key, put, e.g.:
  41. ;;
  42. ;;   (global-set-key "\C-cf" 'find-dired)
  43. ;;   (global-set-key "\C-cn" 'find-name-dired)
  44. ;;   (global-set-key "\C-cl" 'find-grep-dired)
  45. ;;
  46. ;; in your ~/.emacs.
  47.  
  48. (require 'dired)
  49. (provide 'find-dired)
  50.  
  51. ;;;###autoload
  52. (defvar find-ls-option (if (eq system-type 'berkeley-unix) "-ls"
  53.              "-exec ls -ldi {} \\;")
  54.   "*Option to `find' to produce an `ls -l'-type listing.")
  55.  
  56. ;;;###autoload
  57. (defvar find-grep-options (if (eq system-type 'berkeley-unix) "-s" "-l")
  58.   "*Option to grep to be as silent as possible.
  59. On Berkeley systems, this is `-s', for others it seems impossible to
  60. suppress all output, so `-l' is used to print nothing more than the
  61. file name.")
  62.  
  63. (defvar find-args nil
  64.   "Last arguments given to `find' by \\[find-dired].")
  65.  
  66. ;;;###autoload
  67. (defun find-dired (dir args)
  68.   "Run `find' and go into dired-mode on a buffer of the output.
  69. The command run (after changing into DIR) is
  70.  
  71.     find . \\( ARGS \\) -ls"
  72.   (interactive (list (read-file-name "Run find in directory: " nil "" t)
  73.              (if (featurep 'gmhist)
  74.              (read-with-history-in 'find-args-history
  75.                            "Run find (with args): ")
  76.                (read-string "Run find (with args): " find-args))))
  77.   ;; Expand DIR ("" means default-directory), and make sure it has a
  78.   ;; trailing slash.
  79.   (setq dir (file-name-as-directory (expand-file-name dir)))
  80.   ;; Check that it's really a directory.
  81.   (or (file-directory-p dir)
  82.       (error "find-dired needs a directory: %s" dir))
  83.   (switch-to-buffer (get-buffer-create "*Find*"))
  84.   (widen)
  85.   (kill-all-local-variables)
  86.   (setq buffer-read-only nil)
  87.   (erase-buffer)
  88.   (setq default-directory dir
  89.     find-args args            ; save for next interactive call
  90.     args (concat "find . "
  91.              (if (string= args "")
  92.              ""
  93.                (concat "\\( " args " \\) "))
  94.              find-ls-option))
  95.   ;; The next statement will bomb in classic dired (no optional arg allowed)
  96.   ;; find(1)'s -ls corresponds to these switches.
  97.   ;; Note -b, at least GNU find quotes spaces etc. in filenames
  98.   (dired-mode dir "-gilsb")
  99.   ;; Set subdir-alist so that Tree Dired will work:
  100.   (if (fboundp 'dired-simple-subdir-alist)
  101.       ;; will work even with nested dired format (dired-nstd.el,v 1.15
  102.       ;; and later)
  103.       (dired-simple-subdir-alist)
  104.     ;; else we have an ancient tree dired (or classic dired, where
  105.     ;; this does no harm) 
  106.     (set (make-local-variable 'dired-subdir-alist)
  107.      (list (cons default-directory (point-min-marker)))))
  108.   (setq buffer-read-only nil)
  109.   ;; Subdir headlerline must come first because the first marker in
  110.   ;; subdir-alist points there.
  111.   (insert "  " dir ":\n")
  112.   ;; Make second line a ``find'' line in analogy to the ``total'' or
  113.   ;; ``wildcard'' line. 
  114.   (insert "  " args "\n")
  115.   ;; Start the find process
  116.   (set-process-filter (start-process-shell-command "find"
  117.                            (current-buffer) args)
  118.               (function find-dired-filter))
  119.   (set-process-sentinel (get-buffer-process (current-buffer))
  120.             (function find-dired-sentinel))
  121.   (setq mode-line-process '(": %s")))
  122.  
  123. ;;;###autoload
  124. (defun find-name-dired (dir pattern)
  125.   "Search DIR recursively for files matching the globbing pattern PATTERN,
  126. and run dired on those files.
  127. PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
  128. The command run (after changing into DIR) is
  129.  
  130.     find . -name 'PATTERN' -ls"
  131.   (interactive
  132.    "DFind-name (directory): \nsFind-name (filename wildcard): ")
  133.   (find-dired dir (concat "-name '" pattern "'")))
  134.  
  135. ;; This functionality suggested by
  136. ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
  137. ;; Subject: find-dired, lookfor-dired
  138. ;; Date: 10 May 91 17:50:00 GMT
  139. ;; Organization: University of Waterloo
  140.  
  141. (fset 'lookfor-dired 'find-grep-dired)
  142. ;;;###autoload
  143. (defun find-grep-dired (dir args)
  144.   "Find files in DIR containing a regexp ARG and start Dired on output.
  145. The command run (after changing into DIR) is
  146.  
  147.     find . -exec grep -s ARG {} \\\; -ls
  148.  
  149. Thus ARG can also contain additional grep options."
  150.   (interactive "DFind-grep (directory): \nsFind-grep (grep args): ")
  151.   ;; find -exec doesn't allow shell i/o redirections in the command,
  152.   ;; or we could use `grep -l >/dev/null'
  153.   (find-dired dir
  154.           (concat "-exec grep " find-grep-options " " args " {} \\\; ")))
  155.  
  156. (defun find-dired-filter (proc string)
  157.   ;; Filter for \\[find-dired] processes.
  158.   (let ((buf (process-buffer proc)))
  159.     (if (buffer-name buf)        ; not killed?
  160.     (save-excursion
  161.       (set-buffer buf)
  162.       (save-restriction
  163.         (widen)
  164.         (save-excursion
  165.           (let ((buffer-read-only nil)
  166.             (end (point-max)))
  167.         (goto-char end)
  168.         (insert string)
  169.         (goto-char end)
  170.         (or (looking-at "^")
  171.             (forward-line 1))
  172.         (while (looking-at "^")
  173.           (insert "  ")
  174.           (forward-line 1))
  175.         ;; Convert ` ./FILE' to ` FILE'
  176.         ;; This would lose if the current chunk of output
  177.         ;; starts or ends within the ` ./', so backup up a bit:
  178.         (goto-char (- end 3))    ; no error if < 0
  179.         (while (search-forward " ./" nil t)
  180.           (delete-region (point) (- (point) 2)))))))
  181.       ;; The buffer has been killed.
  182.       (delete-process proc))))
  183.  
  184. (defun find-dired-sentinel (proc state)
  185.   ;; Sentinel for \\[find-dired] processes.
  186.   (let ((buf (process-buffer proc)))
  187.     (if (buffer-name buf)
  188.     (save-excursion
  189.       (set-buffer buf)
  190.       (setq mode-line-process nil)
  191.       (message "find-dired %s finished." (current-buffer))))))
  192.  
  193. (or (fboundp 'start-process-shell-command)
  194.     ;; From version 19 subr.el.
  195. (defun start-process-shell-command (name buffer &rest args)
  196.   "Start a program in a subprocess.  Return the process object for it.
  197. Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
  198. NAME is name for process.  It is modified if necessary to make it unique.
  199. BUFFER is the buffer or (buffer-name) to associate with the process.
  200.  Process output goes at end of that buffer, unless you specify
  201.  an output stream or filter function to handle the output.
  202.  BUFFER may be also nil, meaning that this process is not associated
  203.  with any buffer
  204. Third arg is command name, the name of a shell command.
  205. Remaining arguments are the arguments for the command.
  206. Wildcards and redirection are handle as usual in the shell."
  207.   (if (eq system-type 'vax-vms)
  208.       (apply 'start-process name buffer args)
  209.     (start-process name buffer shell-file-name "-c"
  210.            (concat "exec " (mapconcat 'identity args " "))))))
  211.  
  212.