(displaying-byte-compile-warnings): Show
[bpt/emacs.git] / lisp / emacs-lisp / shadow.el
CommitLineData
50584ac0
KH
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
b578f267
EN
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.
50584ac0
KH
25
26;;; Commentary:
b578f267 27
50584ac0
KH
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.
58This function does the work for `list-load-path-shadows'.
59
60We traverse PATH looking for shadows, and return a \(possibly empty\)
61even-length list of files. A file in this list at position 2i shadows
62the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\)
63are stripped from the file names in the list.
64
65See 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) "."))
c2b7bdc2 91 (message "Ignoring redundant directory %s" (car path))))
50584ac0
KH
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)
c2b7bdc2 99 (message "Checking %d files in %s..." (length curr-files) dir))
50584ac0
KH
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
5017dcaa
RS
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 '("subdirs")))
50584ac0
KH
113 ;; File has not been seen yet in this directory.
114 ;; This test prevents us declaring that XXX.el shadows
115 ;; XXX.elc (or vice-versa) when they are in the same directory.
116 (setq files-seen-this-dir (cons file files-seen-this-dir))
117
118 (if (setq orig-dir (assoc file files))
119 ;; This file was seen before, we have a shadowing.
120 (setq shadows
121 (append shadows
122 (list (concat (cdr orig-dir) "/" file)
123 (concat dir "/" file))))
124
125 ;; Not seen before, add it to the list of seen files.
126 (setq files (cons (cons file dir) files))))
127
128 (setq curr-files (cdr curr-files))))
129 (setq path (cdr path)))
130
131 ;; Return the list of shadowings.
132 shadows))
133
134\f
135;;;###autoload
136(defun list-load-path-shadows ()
0f93e41f 137 "Display a list of Emacs Lisp files that shadow other files.
50584ac0
KH
138
139This function lists potential load-path problems. Directories in the
140`load-path' variable are searched, in order, for Emacs Lisp
0f93e41f
RS
141files. When a previously encountered file name is found again, a
142message is displayed indicating that the later file is \"hidden\" by
50584ac0
KH
143the earlier.
144
145For example, suppose `load-path' is set to
146
147\(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\)
148
149and that each of these directories contains a file called XXX.el. Then
150XXX.el in the site-lisp directory is referred to by all of:
151\(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc.
152
153The first XXX.el file prevents emacs from seeing the second \(unless
154the second is loaded explicitly via load-file\).
155
156When not intended, such shadowings can be the source of subtle
157problems. For example, the above situation may have arisen because the
158XXX package was not distributed with versions of emacs prior to
15919.30. An emacs maintainer downloaded XXX from elsewhere and installed
160it. Later, XXX was updated and included in the emacs distribution.
161Unless the emacs maintainer checks for this, the new version of XXX
162will be hidden behind the old \(which may no longer work with the new
163emacs version\).
164
165This function performs these checks and flags all possible
166shadowings. Because a .el file may exist without a corresponding .elc
167\(or vice-versa\), these suffixes are essentially ignored. A file
168XXX.elc in an early directory \(that does not contain XXX.el\) is
169considered to shadow a later file XXX.el, and vice-versa.
170
171When run interactively, the shadowings \(if any\) are displayed in a
172buffer called `*Shadows*'. Shadowings are located by calling the
173\(non-interactive\) companion function, `find-emacs-lisp-shadows'."
174
175 (interactive)
0cdbb11d
RS
176 (let* ((path (copy-sequence load-path))
177 (tem path)
178 toplevs)
179 ;; If we can find simple.el in two places,
180 (while tem
181 (if (file-exists-p (expand-file-name "simple.el" (car tem)))
182 (setq toplevs (cons (car tem) toplevs)))
183 (setq tem (cdr tem)))
184 (if (> (length toplevs) 1)
185 ;; Cut off our copy of load-path right before
186 ;; the second directory which has simple.el in it.
187 ;; This avoids loads of duplications between the source dir
188 ;; and the dir where these files were copied by installation.
189 (let ((break (nth (- (length toplevs) 2) toplevs)))
190 (setq tem path)
191 (while tem
192 (if (eq (nth 1 tem) break)
193 (progn
194 (setcdr tem nil)
195 (setq tem nil)))
196 (setq tem (cdr tem)))))
197
198 (let* ((shadows (find-emacs-lisp-shadows path))
199 (n (/ (length shadows) 2))
200 (msg (format "%s Emacs Lisp load-path shadowing%s found"
201 (if (zerop n) "No" (concat "\n" (number-to-string n)))
202 (if (= n 1) " was" "s were"))))
203 (if (interactive-p)
204 (save-excursion
205 ;; We are interactive.
206 ;; Create the *Shadows* buffer and display shadowings there.
207 (let ((output-buffer (get-buffer-create "*Shadows*")))
208 (display-buffer output-buffer)
209 (set-buffer output-buffer)
210 (erase-buffer)
211 (while shadows
212 (insert (format "%s hides %s\n" (car shadows)
213 (car (cdr shadows))))
214 (setq shadows (cdr (cdr shadows))))
215 (insert msg "\n")))
216 ;; We are non-interactive, print shadows via message.
217 (when shadows
218 (message "This site has duplicate Lisp libraries with the same name.
5017dcaa
RS
219If a locally-installed Lisp library overrides a library in the Emacs release,
220that can cause trouble, and you should probably remove the locally-installed
221version unless you know what you are doing.\n"))
0cdbb11d
RS
222 (while shadows
223 (message "%s hides %s" (car shadows) (car (cdr shadows)))
224 (setq shadows (cdr (cdr shadows))))
225 (message "%s" msg)))))
50584ac0
KH
226
227(provide 'shadow)
228
229;;; shadow.el ends here