1 ;;; shadow.el --- locate Emacs Lisp file shadowings
3 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Terry Jones <terry@santafe.edu>
8 ;; Created: 15 December 1995
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; The functions in this file detect (`find-emacs-lisp-shadows')
28 ;; and display (`list-load-path-shadows') potential load-path
29 ;; problems that arise when Emacs Lisp files "shadow" each other.
31 ;; For example, a file XXX.el early in one's load-path will shadow
32 ;; a file with the same name in a later load-path directory. When
33 ;; this is unintentional, it may result in problems that could have
34 ;; been easily avoided. This occurs often (to me) when installing a
35 ;; new version of emacs and something in the site-lisp directory
36 ;; has been updated and added to the emacs distribution. The old
37 ;; version, now outdated, shadows the new one. This is obviously
40 ;; The `list-load-path-shadows' function was run when you installed
41 ;; this version of emacs. To run it by hand in emacs:
43 ;; M-x list-load-path-shadows
45 ;; or run it non-interactively via:
47 ;; emacs -batch -f list-load-path-shadows
49 ;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions,
50 ;; rewritings & speedups.
54 (defgroup lisp-shadow nil
55 "Locate Emacs Lisp file shadowings."
56 :prefix "load-path-shadows-"
59 (define-obsolete-variable-alias 'shadows-compare-text-p
60 'load-path-shadows-compare-text "23.3")
62 (defcustom load-path-shadows-compare-text nil
63 "If non-nil, then shadowing files are reported only if their text differs.
64 This is slower, but filters out some innocuous shadowing."
68 (defun find-emacs-lisp-shadows (&optional path)
69 "Return a list of Emacs Lisp files that create shadows.
70 This function does the work for `list-load-path-shadows'.
72 We traverse PATH looking for shadows, and return a \(possibly empty\)
73 even-length list of files. A file in this list at position 2i shadows
74 the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\)
75 are stripped from the file names in the list.
77 See the documentation for `list-load-path-shadows' for further information."
78 (let (true-names ; List of dirs considered.
79 shadows ; List of shadowings, to be returned.
80 files ; File names ever seen, with dirs.
81 dir ; The dir being currently scanned.
82 curr-files ; This dir's Emacs Lisp files.
83 orig-dir ; Where the file was first seen.
84 files-seen-this-dir ; Files seen so far in this dir.
85 file) ; The current file.
86 (dolist (pp (or path load-path))
87 (setq dir (directory-file-name (file-truename (or pp "."))))
88 (if (member dir true-names)
89 ;; We have already considered this PATH redundant directory.
90 ;; Show the redundancy if we are interactive, unless the PATH
91 ;; dir is nil or "." (these redundant directories are just a
92 ;; result of the current working directory, and are therefore
93 ;; not always redundant).
96 (not (string= pp "."))
97 (message "Ignoring redundant directory %s" pp)))
99 (setq true-names (append true-names (list dir)))
100 (setq dir (directory-file-name (or pp ".")))
101 (setq curr-files (if (file-accessible-directory-p dir)
102 (directory-files dir nil ".\\.elc?\\(\\.gz\\)?$" t)))
105 (message "Checking %d files in %s..." (length curr-files) dir))
107 (setq files-seen-this-dir nil)
109 (dolist (file curr-files)
111 (if (string-match "\\.gz$" file)
112 (setq file (substring file 0 -3)))
113 (setq file (substring
114 file 0 (if (string= (substring file -1) "c") -4 -3)))
116 ;; FILE now contains the current file name, with no suffix.
117 (unless (or (member file files-seen-this-dir)
118 ;; Ignore these files.
119 (member file '("subdirs")))
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 (setq files-seen-this-dir (cons file files-seen-this-dir))
125 (if (setq orig-dir (assoc file files))
126 ;; This file was seen before, we have a shadowing.
127 ;; Report it unless the files are identical.
128 (let ((base1 (concat (cdr orig-dir) "/" file))
129 (base2 (concat dir "/" file)))
130 (if (not (and load-path-shadows-compare-text
131 (load-path-shadows-same-file-or-nonexistent
132 (concat base1 ".el") (concat base2 ".el"))
133 ;; This is a bit strict, but safe.
134 (load-path-shadows-same-file-or-nonexistent
135 (concat base1 ".elc") (concat base2 ".elc"))))
137 (append shadows (list base1 base2)))))
139 ;; Not seen before, add it to the list of seen files.
140 (setq files (cons (cons file dir) files)))))))
141 ;; Return the list of shadowings.
144 ;; Return true if neither file exists, or if both exist and have identical
146 (defun load-path-shadows-same-file-or-nonexistent (f1 f2)
147 (let ((exists1 (file-exists-p f1))
148 (exists2 (file-exists-p f2)))
149 (or (and (not exists1) (not exists2))
151 (or (equal (file-truename f1) (file-truename f2))
152 ;; As a quick test, avoiding spawning a process, compare file
154 (and (= (nth 7 (file-attributes f1))
155 (nth 7 (file-attributes f2)))
156 (eq 0 (call-process "cmp" nil nil nil "-s" f1 f2))))))))
158 (defvar load-path-shadows-font-lock-keywords
159 `((,(format "hides \\(%s.*\\)"
160 (file-name-directory (locate-library "simple.el")))
161 . (1 font-lock-warning-face)))
162 "Keywords to highlight in `load-path-shadows-mode'.")
164 (define-derived-mode load-path-shadows-mode fundamental-mode "LP-Shadows"
165 "Major mode for load-path shadows buffer."
166 (set (make-local-variable 'font-lock-defaults)
167 '((load-path-shadows-font-lock-keywords)))
168 (setq buffer-undo-list t
171 ;; TODO use text-properties instead, a la dired.
173 (define-button-type 'load-path-shadows-find-file
176 'action (lambda (button)
177 (let ((file (concat (button-get button 'shadow-file) ".el")))
178 (or (file-exists-p file)
179 (setq file (concat file ".gz")))
180 (if (file-readable-p file)
181 (pop-to-buffer (find-file-noselect file))
182 (error "Cannot read file"))))
183 'help-echo "mouse-2, RET: find this file")
187 (defun list-load-path-shadows (&optional stringp)
188 "Display a list of Emacs Lisp files that shadow other files.
190 If STRINGP is non-nil, returns any shadows as a string.
191 Otherwise, if interactive shows any shadows in a `*Shadows*' buffer;
192 else prints messages listing any shadows.
194 This function lists potential load path problems. Directories in
195 the `load-path' variable are searched, in order, for Emacs Lisp
196 files. When a previously encountered file name is found again, a
197 message is displayed indicating that the later file is \"hidden\" by
200 For example, suppose `load-path' is set to
202 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\)
204 and that each of these directories contains a file called XXX.el. Then
205 XXX.el in the site-lisp directory is referred to by all of:
206 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc.
208 The first XXX.el file prevents Emacs from seeing the second \(unless
209 the second is loaded explicitly via `load-file'\).
211 When not intended, such shadowings can be the source of subtle
212 problems. For example, the above situation may have arisen because the
213 XXX package was not distributed with versions of Emacs prior to
214 19.30. An Emacs maintainer downloaded XXX from elsewhere and installed
215 it. Later, XXX was updated and included in the Emacs distribution.
216 Unless the Emacs maintainer checks for this, the new version of XXX
217 will be hidden behind the old \(which may no longer work with the new
220 This function performs these checks and flags all possible
221 shadowings. Because a .el file may exist without a corresponding .elc
222 \(or vice-versa\), these suffixes are essentially ignored. A file
223 XXX.elc in an early directory \(that does not contain XXX.el\) is
224 considered to shadow a later file XXX.el, and vice-versa.
226 Shadowings are located by calling the (non-interactive) companion
227 function, `find-emacs-lisp-shadows'."
229 (let* ((path (copy-sequence load-path))
232 ;; If we can find simple.el in two places,
234 (if (or (file-exists-p (expand-file-name "simple.el" tt))
235 (file-exists-p (expand-file-name "simple.el.gz" tt)))
236 (setq toplevs (cons tt toplevs))))
237 (if (> (length toplevs) 1)
238 ;; Cut off our copy of load-path right before
239 ;; the last directory which has simple.el in it.
240 ;; This avoids loads of duplications between the source dir
241 ;; and the dir where these files were copied by installation.
242 (let ((break (car toplevs)))
245 (if (eq (nth 1 tem) break)
249 (setq tem (cdr tem)))))
251 (let* ((shadows (find-emacs-lisp-shadows path))
252 (n (/ (length shadows) 2))
253 (msg (format "%s Emacs Lisp load-path shadowing%s found"
254 (if (zerop n) "No" (concat "\n" (number-to-string n)))
255 (if (= n 1) " was" "s were"))))
258 (insert (format "%s hides %s\n" (car shadows)
259 (car (cdr shadows))))
260 (setq shadows (cdr (cdr shadows))))
263 (if (called-interactively-p 'interactive)
264 ;; We are interactive.
265 ;; Create the *Shadows* buffer and display shadowings there.
266 (let ((string (buffer-string)))
267 (with-current-buffer (get-buffer-create "*Shadows*")
268 (display-buffer (current-buffer))
269 (load-path-shadows-mode) ; run after-change-major-mode-hook
270 (let ((inhibit-read-only t))
274 (while (re-search-backward "\\(^.*\\) hides \\(.*$\\)"
277 (make-button (match-beginning (1+ i))
279 'type 'load-path-shadows-find-file
281 (match-string (1+ i)))))
282 (goto-char (point-max)))))
283 ;; We are non-interactive, print shadows via message.
285 (message "This site has duplicate Lisp libraries with the same name.
286 If a locally-installed Lisp library overrides a library in the Emacs release,
287 that can cause trouble, and you should probably remove the locally-installed
288 version unless you know what you are doing.\n")
289 (goto-char (point-min))
290 ;; Mimic the previous behavior of using lots of messages.
291 ;; I think one single message would look better...
293 (message "%s" (buffer-substring (line-beginning-position)
294 (line-end-position)))
296 (message "%s" msg))))))))
300 ;;; shadow.el ends here