home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / dired-links.el < prev    next >
Encoding:
Text File  |  1991-03-30  |  4.5 KB  |  132 lines

  1. ; Path: ark1!nems!mimsy!haven!uflorida!novavax!weiner
  2. ; >From weiner@novavax.UUCP (Bob Weiner)
  3. ; Newsgroups: comp.emacs
  4. ; Subject: dired-links.el - resolves links in GNU Emacs Dired
  5. ; Date: 26 Oct 89 02:00:55 GMT
  6. ; Organization: Motorola Inc.
  7. ;;!emacs
  8. ;;
  9. ;; FILE:         dired-links.el
  10. ;; SUMMARY:      Properly resolves UNIX (and Apollo variant) links under dired.
  11. ;; USAGE:        GNU Emacs Lisp Library
  12. ;;
  13. ;; AUTHOR:       Bob Weiner
  14. ;; ORG:          Motorola, Inc., Communications Sector, Applied Research
  15. ;; E-MAIL:       USENET:  weiner@novavax.UUCP
  16. ;;
  17. ;; ORIG-DATE:    09-May-89
  18. ;; LAST-MOD:     25-Oct-89 at 21:42:56 by Bob Weiner
  19. ;;
  20. ;; Copyright (C) 1989 Bob Weiner and Free Software Foundation, Inc.
  21. ;; Available for use and distribution under the same terms as GNU Emacs.
  22. ;;
  23. ;; This file is not part of GNU Emacs.
  24. ;;
  25. ;; DESCRIPTION:  
  26. ;;
  27. ;;   To install, simply load this file after loading dired.el, or create a
  28. ;;   dired hook which loads this file.
  29. ;;
  30. ;;   The changes below to 'dired-noselect' assume UNIX shell file abbreviation
  31. ;;   and UNIX file name conventions.
  32. ;;
  33. ;;   This modified version of the 'dired-noselect' function automatically
  34. ;;   resolves all recursive links properly and edits the actual directory
  35. ;;   pointed to.  It handles Apollo-isms such as '/usr/local ->
  36. ;;   $(SERVER_LOCAL)/usr/local', '/usr/bin -> ../$(SYSTYPE)/usr/bin' and '/tmp
  37. ;;   -> `node_data/tmp'.  It also handles relative links properly as in
  38. ;;   '/usr/local/emacs -> gnu/emacs' which must be resolved relative to the
  39. ;;   '/usr/local' directory.
  40. ;;
  41. ;; DESCRIP-END.
  42.  
  43.  
  44. ;; Normally, if you perform a dired multiple times on a directory which is a
  45. ;; link, a new buffer will be created each time.  This is due to the fact
  46. ;; that 'dired-find-buffer' is called in 'dired-noselect' before the link is
  47. ;; resolved.  The following code solves this problem by checking for a
  48. ;; previously existing buffer that is performing dired on the directory that
  49. ;; the link resolves to.  This is also done recursively.  If one is found,
  50. ;; the dired buffer that shows the link is killed and the previously existing
  51. ;; one is used and re-read in.
  52.  
  53. (defun dired-noselect (dirname)
  54.   "Like M-x dired but returns the dired buffer as value, does not select it."
  55.   (or dirname (setq dirname default-directory))
  56.   (setq dirname (directory-file-name dirname))
  57.   (if (file-directory-p dirname)
  58.       (setq dirname (file-name-as-directory dirname)))
  59.   (let ((buffer (dired-find-buffer dirname))
  60.     (actual-dir t)
  61.     (actual-buffer))
  62.     (save-excursion
  63.       (set-buffer buffer)
  64.       (while actual-dir
  65.     (if (= (aref dirname 0) ?~) (setq dirname (expand-file-name dirname)))
  66.     (dired-readin dirname buffer)
  67.     (if (setq actual-dir (dired-resolve-link dirname))
  68.         (progn (setq dirname (directory-file-name actual-dir)
  69.              actual-buffer (dired-find-buffer dirname))
  70.            (if (file-directory-p dirname)
  71.                (setq dirname (file-name-as-directory dirname)))))
  72.     (if actual-buffer
  73.         (progn (set-buffer actual-buffer)
  74.            (kill-buffer buffer)
  75.            (setq buffer actual-buffer
  76.              actual-buffer nil))))
  77.       (dired-move-to-filename)
  78.       (dired-mode dirname))
  79.     buffer))
  80.  
  81. (defconst dired-link-string " -> "
  82.   "String pattern in a dired listing line that indicates a link.")
  83.  
  84. ;;;
  85. ;;; Works with Apollo's variant links if variable is first part of the linkname
  86. ;;;
  87. (defun dired-resolve-link (dirname)
  88.   (save-excursion
  89.     (goto-char (point-min))
  90.     (end-of-line)
  91.     (let ((eol (point))
  92.       (actual-dir)
  93.       (buffer-read-only)
  94.       (var-link))
  95.       (goto-char (point-min))
  96.       (if (search-forward dired-link-string eol t)
  97.       (progn (delete-region (point-min) (point))
  98.          (save-excursion
  99.            (end-of-line)
  100.            (setq eol (point)))
  101.          (while (re-search-forward "\\$(\\([^\)]*\\))" eol t)
  102.            (progn (setq var-link (getenv (buffer-substring
  103.                            (match-beginning 1)
  104.                            (match-end 1))))
  105.               (delete-region (match-beginning 0) (match-end 0))
  106.               (insert var-link)))
  107.          (setq actual-dir (buffer-substring (point-min) (1- (point-max))))
  108.          ;; If not an absolute path
  109.          (let ((nd-abbrev (string-match "`node_data" actual-dir)))
  110.            (if (and nd-abbrev (= nd-abbrev 0))
  111.                (setq actual-dir (concat "/sys/"
  112.                         (substring actual-dir 1)))))
  113.          (if (/= (aref actual-dir 0) ?/)
  114.              (setq actual-dir
  115.                (concat 
  116.                  (file-name-directory
  117.                    (substring dirname 0 
  118.                       (if (= (aref dirname
  119.                                (1- (length dirname))) ?/)
  120.                           -1)))
  121.                  actual-dir)))
  122.          actual-dir
  123. )))))
  124.  
  125. (provide 'dired-links)
  126. ;-- 
  127. ;Bob Weiner, Motorola, Inc.,   USENET:  ...!gatech!uflorida!novavax!weiner
  128. ;(407) 738-2087
  129.  
  130.  
  131.