Debug message fix
[sxemacs] / 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 SXEmacs.
10
11 ;; SXEmacs 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 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; SXEmacs 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 this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; The functions in this file detect (`find-emacs-lisp-shadows')
27 ;; and display (`list-load-path-shadows') potential load-path
28 ;; problems that arise when Emacs Lisp files "shadow" each other.
29 ;;
30 ;; For example, a file XXX.el early in one's load-path will shadow
31 ;; a file with the same name in a later load-path directory.  When
32 ;; this is unintentional, it may result in problems that could have
33 ;; been easily avoided.  This occurs often (to me) when installing a
34 ;; new version of emacs and something in the site-lisp directory
35 ;; has been updated and added to the emacs distribution.  The old
36 ;; version, now outdated, shadows the new one. This is obviously
37 ;; undesirable.
38 ;;
39 ;; The `list-load-path-shadows' function was run when you installed
40 ;; this version of emacs. To run it by hand in emacs:
41 ;;
42 ;;     M-x load-library RET shadow RET
43 ;;     M-x list-load-path-shadows
44 ;;
45 ;; or run it non-interactively via:
46 ;;
47 ;;     emacs -batch -l shadow.el -f list-load-path-shadows
48 ;;
49 ;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions,
50 ;; rewritings & speedups.
51
52 ;; 1998-08-15 Martin Buchholz: Speed up using hash tables instead of lists.
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   (let (shadows                         ; List of shadowings, to be returned.
68         dir                             ; The dir being currently scanned.
69         curr-files                      ; This dir's Emacs Lisp files.
70         orig-dir                        ; Where the file was first seen.
71         (file-dirs                      ; File names ever seen, with dirs.
72          (make-hash-table :size 2000 :test 'equal))
73         (true-names                     ; Dirs ever considered.
74          (make-hash-table :size 50 :test 'equal))
75         (files-seen-this-dir            ; Files seen so far in this dir.
76          (make-hash-table :size 100 :test 'equal))
77         )
78
79     (dolist (path-elt (or path load-path))
80
81       (setq dir (file-truename (or path-elt ".")))
82       (if (gethash dir true-names)
83           ;; We have already considered this PATH redundant directory.
84           ;; Show the redundancy if we are interactive, 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 path-elt
90                    (not (string= path-elt "."))
91                    (message "Ignoring redundant directory %s" path-elt)))
92
93         (puthash dir t true-names)
94         (setq dir (or path-elt "."))
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         (clrhash files-seen-this-dir)
102
103         (dolist (file curr-files)
104
105           (setq file (substring
106                       file 0 (if (string= (substring file -1) "c") -4 -3)))
107
108           ;; FILE now contains the current file name, with no suffix.
109           (unless (or (gethash file files-seen-this-dir)
110                       ;; Ignore these files.
111                       (member file
112                               (append
113                                '("subdirs"
114                                  "auto-autoloads"
115                                  "custom-load"
116                                  "custom-defines"
117                                  "dumped-lisp"
118                                  "_pkg"
119                                  "lpath")
120                                ;; ignore the package-suppress'd libs too.
121                                (mapfam
122                                 #'(lambda (e)
123                                     (file-basename (car e)))
124                                 :result-type #'list
125                                 load-suppress-alist))))
126             ;; File has not been seen yet in this directory.
127             ;; This test prevents us declaring that XXX.el shadows
128             ;; XXX.elc (or vice-versa) when they are in the same directory.
129             (puthash file t files-seen-this-dir)
130
131             (if (setq orig-dir (gethash file file-dirs))
132                 ;; This file was seen before, we have a shadowing.
133                 (setq shadows
134                       (nconc shadows
135                              (list (concat (file-name-as-directory orig-dir)
136                                            file)
137                                    (concat (file-name-as-directory dir)
138                                            file))))
139
140               ;; Not seen before, add it to the list of seen files.
141               (puthash file dir file-dirs))))))
142
143     ;; Return the list of shadowings.
144     shadows))
145
146 \f
147 ;;;###autoload
148 (defun list-load-path-shadows ()
149   "Display a list of Emacs Lisp files that shadow other files.
150
151 This function lists potential load-path problems.  Directories in the
152 `load-path' variable are searched, in order, for Emacs Lisp
153 files.  When a previously encountered file name is found again, a
154 message is displayed indicating that the later file is \"hidden\" by
155 the earlier.
156
157 For example, suppose `load-path' is set to
158
159 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\)
160
161 and that each of these directories contains a file called XXX.el.  Then
162 XXX.el in the site-lisp directory is referred to by all of:
163 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc.
164
165 The first XXX.el file prevents emacs from seeing the second \(unless
166 the second is loaded explicitly via load-file\).
167
168 When not intended, such shadowings can be the source of subtle
169 problems.  For example, the above situation may have arisen because the
170 XXX package was not distributed with versions of emacs prior to
171 19.30.  An emacs maintainer downloaded XXX from elsewhere and installed
172 it.  Later, XXX was updated and included in the emacs distribution.
173 Unless the emacs maintainer checks for this, the new version of XXX
174 will be hidden behind the old \(which may no longer work with the new
175 emacs version\).
176
177 This function performs these checks and flags all possible
178 shadowings.  Because a .el file may exist without a corresponding .elc
179 \(or vice-versa\), these suffixes are essentially ignored.  A file
180 XXX.elc in an early directory \(that does not contain XXX.el\) is
181 considered to shadow a later file XXX.el, and vice-versa.
182
183 When run interactively, the shadowings \(if any\) are displayed in a
184 buffer called `*Shadows*'.  Shadowings are located by calling the
185 \(non-interactive\) companion function, `find-emacs-lisp-shadows'."
186
187   (interactive)
188   (let* ((path (copy-sequence load-path))
189         (tem path)
190         toplevs)
191     ;; If we can find simple.el in two places,
192     (while tem
193       (if (file-exists-p (expand-file-name "simple.el" (car tem)))
194           (setq toplevs (cons (car tem) toplevs)))
195       (setq tem (cdr tem)))
196     (if (> (length toplevs) 1)
197         ;; Cut off our copy of load-path right before
198         ;; the second directory which has simple.el in it.
199         ;; This avoids loads of duplications between the source dir
200         ;; and the dir where these files were copied by installation.
201         (let ((break (nth (- (length toplevs) 2) toplevs)))
202           (setq tem path)
203           (while tem
204             (if (eq (nth 1 tem) break)
205                 (progn
206                   (setcdr tem nil)
207                   (setq tem nil)))
208             (setq tem (cdr tem)))))
209
210     (let* ((shadows (find-emacs-lisp-shadows path))
211            (n (/ (length shadows) 2))
212            (msg (format "%s Emacs Lisp load-path shadowing%s found"
213                         (if (zerop n) "No" (concat "\n" (number-to-string n)))
214                         (if (= n 1) " was" "s were"))))
215       (if (interactive-p)
216           (save-excursion
217             ;; We are interactive.
218             ;; Create the *Shadows* buffer and display shadowings there.
219             (let ((output-buffer (get-buffer-create "*Shadows*")))
220               (display-buffer output-buffer)
221               (set-buffer output-buffer)
222               (erase-buffer)
223               (while shadows
224                 (insert (format "%s hides %s\n" (car shadows)
225                                 (car (cdr shadows))))
226                 (setq shadows (cdr (cdr shadows))))
227               (insert msg "\n")))
228         ;; We are non-interactive, print shadows via message.
229         (when shadows
230           (message "This site has duplicate Lisp libraries with the same name.
231 If a locally-installed Lisp library overrides a library in the Emacs release,
232 that can cause trouble, and you should probably remove the locally-installed
233 version unless you know what you are doing.\n")
234           (while shadows
235             (message "%s hides %s" (car shadows) (car (cdr shadows)))
236             (setq shadows (cdr (cdr shadows))))
237           (message "%s" msg))))))
238
239 (provide 'shadow)
240
241 ;;; shadow.el ends here