XEmacs 21.2-b2
[chise/xemacs-chise.git.1] / lisp / shadow.el
1 ;;; shadow.el --- Locate Emacs Lisp file shadowings.
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4
5 ;; Author: Terry Jones <terry@santafe.edu>
6 ;; Keywords: lisp
7 ;; Created: 15 December 1995
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; The functions in this file detect (`find-emacs-lisp-shadows')
29 ;; and display (`list-load-path-shadows') potential load-path
30 ;; problems that arise when Emacs Lisp files "shadow" each other.
31 ;;
32 ;; For example, a file XXX.el early in one's load-path will shadow
33 ;; a file with the same name in a later load-path directory.  When
34 ;; this is unintentional, it may result in problems that could have
35 ;; been easily avoided.  This occurs often (to me) when installing a
36 ;; new version of emacs and something in the site-lisp directory
37 ;; has been updated and added to the emacs distribution.  The old
38 ;; version, now outdated, shadows the new one. This is obviously
39 ;; undesirable.
40 ;;
41 ;; The `list-load-path-shadows' function was run when you installed
42 ;; this version of emacs. To run it by hand in emacs:
43 ;;
44 ;;     M-x load-library RET shadow RET
45 ;;     M-x list-load-path-shadows
46 ;;
47 ;; or run it non-interactively via:
48 ;;
49 ;;     emacs -batch -l shadow.el -f list-load-path-shadows
50 ;;
51 ;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions,
52 ;; rewritings & speedups.
53
54 ;; 1998-08-15 Martin Buchholz: Speed up using hashtables instead of lists.
55
56 ;;; Code:
57 \f
58 (defun find-emacs-lisp-shadows (&optional path)
59   "Return a list of Emacs Lisp files that create shadows.
60 This function does the work for `list-load-path-shadows'.
61
62 We traverse PATH looking for shadows, and return a \(possibly empty\)
63 even-length list of files.  A file in this list at position 2i shadows
64 the file in position 2i+1.  Emacs Lisp file suffixes \(.el and .elc\)
65 are stripped from the file names in the list.
66
67 See the documentation for `list-load-path-shadows' for further information."
68   
69   (let (shadows                         ; List of shadowings, to be returned.
70         dir                             ; The dir being currently scanned.
71         curr-files                      ; This dir's Emacs Lisp files.
72         orig-dir                        ; Where the file was first seen.
73         (file-dirs
74          (make-hashtable 2000 'equal))  ; File names ever seen, with dirs.
75         (true-names
76          (make-hashtable 50 'equal))    ; Dirs ever considered.
77         (files-seen-this-dir
78          (make-hashtable 100 'equal))   ; Files seen so far in this dir.
79         )
80   
81     (dolist (path-elt (or path load-path))
82
83       (setq dir (file-truename (or path-elt ".")))
84       (if (gethash dir true-names)
85           ;; We have already considered this PATH redundant directory.
86           ;; Show the redundancy if we are interactive, unless the PATH
87           ;; dir is nil or "." (these redundant directories are just a
88           ;; result of the current working directory, and are therefore
89           ;; not always redundant).
90           (or noninteractive
91               (and path-elt
92                    (not (string= path-elt "."))
93                    (message "Ignoring redundant directory %s" path-elt)))
94
95         (puthash dir t true-names)
96         (setq dir (or path-elt "."))
97         (setq curr-files (if (file-accessible-directory-p dir)
98                                (directory-files dir nil ".\\.elc?$" t)))
99         (and curr-files
100              (not noninteractive)
101              (message "Checking %d files in %s..." (length curr-files) dir))
102         
103         (clrhash files-seen-this-dir)
104
105         (dolist (file curr-files)
106
107           (setq file (substring
108                       file 0 (if (string= (substring file -1) "c") -4 -3)))
109
110           ;; FILE now contains the current file name, with no suffix.
111           (unless (or (gethash file files-seen-this-dir)
112                       ;; Ignore these files.
113                       (member file
114                               '("subdirs"
115                                 "auto-autoloads"
116                                 "custom-load"
117                                 "dumped-lisp"
118                                 "_pkg"
119                                 "lpath")))
120             ;; File has not been seen yet in this directory.
121             ;; This test prevents us declaring that XXX.el shadows
122             ;; XXX.elc (or vice-versa) when they are in the same directory.
123             (puthash file t files-seen-this-dir)
124               
125             (if (setq orig-dir (gethash file file-dirs))
126                 ;; This file was seen before, we have a shadowing.
127                 (setq shadows
128                       (nconc shadows
129                              (list (concat (file-name-as-directory orig-dir)
130                                            file)
131                                    (concat (file-name-as-directory dir)
132                                            file))))
133
134               ;; Not seen before, add it to the list of seen files.
135               (puthash file dir file-dirs))))))
136
137     ;; Return the list of shadowings.
138     shadows))
139
140 \f
141 ;;;###autoload
142 (defun list-load-path-shadows ()
143   "Display a list of Emacs Lisp files that shadow other files.
144
145 This function lists potential load-path problems.  Directories in the
146 `load-path' variable are searched, in order, for Emacs Lisp
147 files.  When a previously encountered file name is found again, a
148 message is displayed indicating that the later file is \"hidden\" by
149 the earlier.
150
151 For example, suppose `load-path' is set to
152
153 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\)
154
155 and that each of these directories contains a file called XXX.el.  Then
156 XXX.el in the site-lisp directory is referred to by all of:
157 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc.
158
159 The first XXX.el file prevents emacs from seeing the second \(unless
160 the second is loaded explicitly via load-file\).
161
162 When not intended, such shadowings can be the source of subtle
163 problems.  For example, the above situation may have arisen because the
164 XXX package was not distributed with versions of emacs prior to
165 19.30.  An emacs maintainer downloaded XXX from elsewhere and installed
166 it.  Later, XXX was updated and included in the emacs distribution.
167 Unless the emacs maintainer checks for this, the new version of XXX
168 will be hidden behind the old \(which may no longer work with the new
169 emacs version\).
170
171 This function performs these checks and flags all possible
172 shadowings.  Because a .el file may exist without a corresponding .elc
173 \(or vice-versa\), these suffixes are essentially ignored.  A file
174 XXX.elc in an early directory \(that does not contain XXX.el\) is
175 considered to shadow a later file XXX.el, and vice-versa.
176
177 When run interactively, the shadowings \(if any\) are displayed in a
178 buffer called `*Shadows*'.  Shadowings are located by calling the
179 \(non-interactive\) companion function, `find-emacs-lisp-shadows'."
180   
181   (interactive)
182   (let* ((path (copy-sequence load-path))
183         (tem path)
184         toplevs)
185     ;; If we can find simple.el in two places,
186     (while tem
187       (if (file-exists-p (expand-file-name "simple.el" (car tem)))
188           (setq toplevs (cons (car tem) toplevs)))
189       (setq tem (cdr tem)))
190     (if (> (length toplevs) 1)
191         ;; Cut off our copy of load-path right before
192         ;; the second directory which has simple.el in it.
193         ;; This avoids loads of duplications between the source dir
194         ;; and the dir where these files were copied by installation.
195         (let ((break (nth (- (length toplevs) 2) toplevs)))
196           (setq tem path)
197           (while tem
198             (if (eq (nth 1 tem) break)
199                 (progn
200                   (setcdr tem nil)
201                   (setq tem nil)))
202             (setq tem (cdr tem)))))
203
204     (let* ((shadows (find-emacs-lisp-shadows path))
205            (n (/ (length shadows) 2))
206            (msg (format "%s Emacs Lisp load-path shadowing%s found"
207                         (if (zerop n) "No" (concat "\n" (number-to-string n)))
208                         (if (= n 1) " was" "s were"))))
209       (if (interactive-p)
210           (save-excursion
211             ;; We are interactive.
212             ;; Create the *Shadows* buffer and display shadowings there.
213             (let ((output-buffer (get-buffer-create "*Shadows*")))
214               (display-buffer output-buffer)
215               (set-buffer output-buffer)
216               (erase-buffer)
217               (while shadows
218                 (insert (format "%s hides %s\n" (car shadows)
219                                 (car (cdr shadows))))
220                 (setq shadows (cdr (cdr shadows))))
221               (insert msg "\n")))
222         ;; We are non-interactive, print shadows via message.
223         (when shadows
224           (message "This site has duplicate Lisp libraries with the same name.
225 If a locally-installed Lisp library overrides a library in the Emacs release,
226 that can cause trouble, and you should probably remove the locally-installed
227 version unless you know what you are doing.\n")
228           (while shadows
229             (message "%s hides %s" (car shadows) (car (cdr shadows)))
230             (setq shadows (cdr (cdr shadows))))
231           (message "%s" msg))))))
232
233 (provide 'shadow)
234
235 ;;; shadow.el ends here