home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / find-dir.el < prev    next >
Encoding:
Text File  |  1990-03-21  |  4.9 KB  |  118 lines

  1. ;From ark1!uakari.primate.wisc.edu!gem.mps.ohio-state.edu!tut.cis.ohio-state.edu!tds.kth.se!juha Thu Oct 26 11:00:57 1989
  2. ;Article 591 of gnu.emacs
  3. ;Path: ark1!uakari.primate.wisc.edu!gem.mps.ohio-state.edu!tut.cis.ohio-state.edu!tds.kth.se!juha
  4. ;>From juha@tds.kth.se (Juha Sarlin)
  5. ;Newsgroups: gnu.emacs
  6. ;Subject: Another way of finding a directory in Emacs
  7. ;Message-ID: <8910251857.AA09126@ttds.tds.kth.se>
  8. ;Date: 25 Oct 89 20:57:39 GMT
  9. ;Sender: daemon@tut.cis.ohio-state.edu
  10. ;Distribution: gnu
  11. ;Organization: GNUs Not Usenet
  12. ;Lines: 104
  13. ;
  14. ;Below is Emacs Lisp code for my version of a searching "cd" command.
  15. ;It differs from other similar versions I've seen in that it does not
  16. ;change the default directory of the current buffer, but instead
  17. ;creates a buffer in dired-mode for the directory.
  18. ;
  19. ;There are two versions; one that reads in the directory list and one
  20. ;that doesn't.  The latter can be useful because the reading is usually
  21. ;rather slow.
  22. ;
  23. ;-------------------- find-dir.el --------------------
  24. ;; Find directory using cd-path.
  25. ;; Copyright (C) 1989 Juha Sarlin    <juha@tds.kth.se>
  26.  
  27. ;; This file is an unofficial part of GNU Emacs.
  28.  
  29. ;; GNU Emacs is distributed in the hope that it will be useful,
  30. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  31. ;; accepts responsibility to anyone for the consequences of using it
  32. ;; or for whether it serves any particular purpose or works at all,
  33. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  34. ;; License for full details.
  35.  
  36. ;; Everyone is granted permission to copy, modify and redistribute
  37. ;; GNU Emacs, but only under the conditions described in the
  38. ;; GNU Emacs General Public License.   A copy of this license is
  39. ;; supposed to have been given to you along with GNU Emacs so you
  40. ;; can know your rights and responsibilities.  It should be in a
  41. ;; file named COPYING.  Among other things, the copyright notice
  42. ;; and this notice must be preserved on all copies.
  43.  
  44. ;; To use this you can for example add the following lines to your .emacs file:
  45. ;;    (autoload 'find-directory-noread "find-dir" nil t)
  46. ;;    (global-set-key "\C-C\C-D" 'find-directory-noread)
  47. ;; If you prefer to read in the directory list by default use this instead:
  48. ;;    (autoload 'find-directory "find-dir" nil t)
  49. ;;    (global-set-key "\C-C\C-D" 'find-directory)
  50. ;;
  51. ;; To define the search path you can either define the environment
  52. ;; variable CDPATH before starting emacs. In bash, for example:
  53. ;;    export CDPATH=.:/usr/gnu:/usr/local/src
  54. ;; Or you can define cd-path in your .emacs file:
  55. ;;    (setq cd-path '(nil "/usr/gnu" "/usr/local/src"))
  56. ;; Note the use of nil instead of ".". This is explained in the
  57. ;; documentation for cd-path below.
  58.  
  59. (defvar cd-path nil
  60.   "*List of directories to search through in dir-search.
  61. Use nil instead of \".\", because expand-file-name doesn't expand \".\"
  62. into an absolute path name.  If you don't set this variable yourself,
  63. its initialized from the CDPATH environment variable.")
  64.  
  65. (defun substring-list (regexp string)
  66.   "Return list of substrings matched by REGEXP in STRING."
  67.   (let (list)
  68.     (string-match "" "")        ;initialize match-end to 0
  69.     (while (and (string-match regexp string (match-end 0))
  70.         ;; avoid infinite looping when the match is an empty string
  71.         (/= (match-beginning 0) (match-end 0)))
  72.       (setq list (cons (substring string (match-beginning 0) (match-end 0))
  73.                list)))
  74.     (nreverse list)))
  75.  
  76. (defun dir-search (dir)
  77.   "Search for DIR in the directories listed in cd-path.
  78. If cd-path is nil, its initialized from a colon separated list of
  79. directories in the CDPATH environment variable.
  80. Returns directory name if one was found, else nil."
  81.   (if (not cd-path)
  82.       ;; Parse CDPATH and change "." to nil.
  83.       (setq cd-path (mapcar
  84.              '(lambda (name) (if (string-equal name ".") nil name))
  85.              (substring-list "[^:]+" (or (getenv "CDPATH") "")))))
  86.   (if (string-equal dir "")        ; Empty string means home directory
  87.       (expand-file-name "~")
  88.     (let ((path cd-path) name found)
  89.       (while (and (not found) path)
  90.     (setq name (expand-file-name dir (car path)))
  91.     (if (file-directory-p name)    
  92.         (setq found name))
  93.     (setq path (cdr path)))
  94.       found)))
  95.  
  96. (defun find-directory-noread (dir)
  97.   "Search for DIR in the directories listed in cd-path.
  98. If found, switch to a buffer in dired-mode for that directory.
  99. The directory list is not read in by default; you can read it
  100. with the normal dired-mode commands."
  101.   (interactive "sFind directory: ")
  102.   (require 'dired)
  103.   (let ((dirname (dir-search dir)))
  104.     (if dirname
  105.     (progn (setq dirname (file-name-as-directory dirname))
  106.            (switch-to-buffer (dired-find-buffer dirname))
  107.            (dired-mode dirname))
  108.       (error "Directory %s not found" dir))))
  109.  
  110. (defun find-directory (dir)
  111.   "Search for DIR in the directories listed in cd-path.
  112. If found, switch to a buffer in dired-mode for that directory."
  113.   (interactive "sFind directory: ")
  114.   (find-directory-noread dir)
  115.   (dired-revert))
  116. ;--
  117. ;Juha Sarlin    juha@tds.kth.se
  118.