46d3a58c5467afd59b09ca4df4a91cf498d7fa6a
[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 GNU Emacs.
10
11 ;; GNU Emacs 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 ;;; Code:
55 \f
56 (defun find-emacs-lisp-shadows (&optional path)
57   "Return a list of Emacs Lisp files that create shadows.
58 This function does the work for `list-load-path-shadows'.
59
60 We traverse PATH looking for shadows, and return a \(possibly empty\)
61 even-length list of files.  A file in this list at position 2i shadows
62 the file in position 2i+1.  Emacs Lisp file suffixes \(.el and .elc\)
63 are stripped from the file names in the list.
64
65 See the documentation for `list-load-path-shadows' for further information."
66   
67   (or path (setq path load-path))
68
69   (let (true-names                      ; List of dirs considered.
70         shadows                         ; List of shadowings, to be returned.
71         files                           ; File names ever seen, with dirs.
72         dir                             ; The dir being currently scanned.
73         curr-files                      ; This dir's Emacs Lisp files.
74         orig-dir                        ; Where the file was first seen.
75         files-seen-this-dir             ; Files seen so far in this dir.
76         file)                           ; The current file.
77
78   
79     (while path
80
81       (setq dir (file-truename (or (car path) ".")))
82       (if (member dir true-names)
83           ;; We have already considered this PATH redundant directory.
84           ;; Show the redundancy if we are interactiver, unless the PATH
85           ;; dir is nil or "." (these redundant directories are just a
86           ;; result of the current working directory, and are therefore
87           ;; not always redundant).
88           (or noninteractive
89               (and (car path)
90                    (not (string= (car path) "."))
91                    (message "Ignoring redundant directory %s" (car path))))
92         
93         (setq true-names (append true-names (list dir)))
94         (setq dir (or (car path) "."))
95         (setq curr-files (if (file-accessible-directory-p dir)
96                                (directory-files dir nil ".\\.elc?$" t)))
97         (and curr-files
98              (not noninteractive)
99              (message "Checking %d files in %s..." (length curr-files) dir))
100         
101         (setq files-seen-this-dir nil)
102
103         (while curr-files
104
105           (setq file (car curr-files))
106           (setq file (substring
107                       file 0 (if (string= (substring file -1) "c") -4 -3)))
108
109           ;; FILE now contains the current file name, with no suffix.
110           (unless (or (member file files-seen-this-dir)
111                       ;; Ignore these files.
112                       (member file
113                               '("subdirs"
114                                 "auto-autoloads"
115                                 "custom-load"
116                                 "dumped-lisp"
117                                 "_pkg"
118                                 "lpath")))
119             ;; File has not been seen yet in this directory.
120             ;; This test prevents us declaring that XXX.el shadows
121             ;; XXX.elc (or vice-versa) when they are in the same directory.
122             (setq files-seen-this-dir (cons file files-seen-this-dir))
123               
124             (if (setq orig-dir (assoc file files))
125                 ;; This file was seen before, we have a shadowing.
126                 (setq shadows
127                       (append shadows
128                               (list (concat (file-name-as-directory (cdr orig-dir))
129                                             file)
130                                     (concat (file-name-as-directory dir)
131                                             file))))
132
133               ;; Not seen before, add it to the list of seen files.
134               (setq files (cons (cons file dir) files))))
135
136           (setq curr-files (cdr curr-files))))
137         (setq path (cdr path)))
138
139     ;; Return the list of shadowings.
140     shadows))
141
142 \f
143 ;;;###autoload
144 (defun list-load-path-shadows ()
145   "Display a list of Emacs Lisp files that shadow other files.
146
147 This function lists potential load-path problems.  Directories in the
148 `load-path' variable are searched, in order, for Emacs Lisp
149 files.  When a previously encountered file name is found again, a
150 message is displayed indicating that the later file is \"hidden\" by
151 the earlier.
152
153 For example, suppose `load-path' is set to
154
155 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\)
156
157 and that each of these directories contains a file called XXX.el.  Then
158 XXX.el in the site-lisp directory is referred to by all of:
159 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc.
160
161 The first XXX.el file prevents emacs from seeing the second \(unless
162 the second is loaded explicitly via load-file\).
163
164 When not intended, such shadowings can be the source of subtle
165 problems.  For example, the above situation may have arisen because the
166 XXX package was not distributed with versions of emacs prior to
167 19.30.  An emacs maintainer downloaded XXX from elsewhere and installed
168 it.  Later, XXX was updated and included in the emacs distribution.
169 Unless the emacs maintainer checks for this, the new version of XXX
170 will be hidden behind the old \(which may no longer work with the new
171 emacs version\).
172
173 This function performs these checks and flags all possible
174 shadowings.  Because a .el file may exist without a corresponding .elc
175 \(or vice-versa\), these suffixes are essentially ignored.  A file
176 XXX.elc in an early directory \(that does not contain XXX.el\) is
177 considered to shadow a later file XXX.el, and vice-versa.
178
179 When run interactively, the shadowings \(if any\) are displayed in a
180 buffer called `*Shadows*'.  Shadowings are located by calling the
181 \(non-interactive\) companion function, `find-emacs-lisp-shadows'."
182   
183   (interactive)
184   (let* ((path (copy-sequence load-path))
185         (tem path)
186         toplevs)
187     ;; If we can find simple.el in two places,
188     (while tem
189       (if (file-exists-p (expand-file-name "simple.el" (car tem)))
190           (setq toplevs (cons (car tem) toplevs)))
191       (setq tem (cdr tem)))
192     (if (> (length toplevs) 1)
193         ;; Cut off our copy of load-path right before
194         ;; the second directory which has simple.el in it.
195         ;; This avoids loads of duplications between the source dir
196         ;; and the dir where these files were copied by installation.
197         (let ((break (nth (- (length toplevs) 2) toplevs)))
198           (setq tem path)
199           (while tem
200             (if (eq (nth 1 tem) break)
201                 (progn
202                   (setcdr tem nil)
203                   (setq tem nil)))
204             (setq tem (cdr tem)))))
205
206     (let* ((shadows (find-emacs-lisp-shadows path))
207            (n (/ (length shadows) 2))
208            (msg (format "%s Emacs Lisp load-path shadowing%s found"
209                         (if (zerop n) "No" (concat "\n" (number-to-string n)))
210                         (if (= n 1) " was" "s were"))))
211       (if (interactive-p)
212           (save-excursion
213             ;; We are interactive.
214             ;; Create the *Shadows* buffer and display shadowings there.
215             (let ((output-buffer (get-buffer-create "*Shadows*")))
216               (display-buffer output-buffer)
217               (set-buffer output-buffer)
218               (erase-buffer)
219               (while shadows
220                 (insert (format "%s hides %s\n" (car shadows)
221                                 (car (cdr shadows))))
222                 (setq shadows (cdr (cdr shadows))))
223               (insert msg "\n")))
224         ;; We are non-interactive, print shadows via message.
225         (when shadows
226           (message "This site has duplicate Lisp libraries with the same name.
227 If a locally-installed Lisp library overrides a library in the Emacs release,
228 that can cause trouble, and you should probably remove the locally-installed
229 version unless you know what you are doing.\n")
230           (while shadows
231             (message "%s hides %s" (car shadows) (car (cdr shadows)))
232             (setq shadows (cdr (cdr shadows))))
233           (message "%s" msg))))))
234
235 (provide 'shadow)
236
237 ;;; shadow.el ends here