Use `called-interactively-p' instead of `interactive-p'.
[bpt/emacs.git] / lisp / emacs-lisp / shadow.el
1 ;;; shadow.el --- locate Emacs Lisp file shadowings
2
3 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009 Free Software Foundation, Inc.
5
6 ;; Author: Terry Jones <terry@santafe.edu>
7 ;; Keywords: lisp
8 ;; Created: 15 December 1995
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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.
30 ;;
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
38 ;; undesirable.
39 ;;
40 ;; The `list-load-path-shadows' function was run when you installed
41 ;; this version of emacs. To run it by hand in emacs:
42 ;;
43 ;; M-x list-load-path-shadows
44 ;;
45 ;; or run it non-interactively via:
46 ;;
47 ;; emacs -batch -f list-load-path-shadows
48 ;;
49 ;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions,
50 ;; rewritings & speedups.
51
52 ;;; Code:
53 \f
54 (defgroup lisp-shadow nil
55 "Locate Emacs Lisp file shadowings."
56 :prefix "shadows-"
57 :group 'lisp)
58
59 (defcustom shadows-compare-text-p nil
60 "If non-nil, then shadowing files are reported only if their text differs.
61 This is slower, but filters out some innocuous shadowing."
62 :type 'boolean
63 :group 'lisp-shadow)
64
65 (defun find-emacs-lisp-shadows (&optional path)
66 "Return a list of Emacs Lisp files that create shadows.
67 This function does the work for `list-load-path-shadows'.
68
69 We traverse PATH looking for shadows, and return a \(possibly empty\)
70 even-length list of files. A file in this list at position 2i shadows
71 the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\)
72 are stripped from the file names in the list.
73
74 See the documentation for `list-load-path-shadows' for further information."
75
76 (or path (setq path load-path))
77
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
87
88 (while path
89
90 (setq dir (directory-file-name (file-truename (or (car path) "."))))
91 (if (member dir true-names)
92 ;; We have already considered this PATH redundant directory.
93 ;; Show the redundancy if we are interactive, unless the PATH
94 ;; dir is nil or "." (these redundant directories are just a
95 ;; result of the current working directory, and are therefore
96 ;; not always redundant).
97 (or noninteractive
98 (and (car path)
99 (not (string= (car path) "."))
100 (message "Ignoring redundant directory %s" (car path))))
101
102 (setq true-names (append true-names (list dir)))
103 (setq dir (directory-file-name (or (car path) ".")))
104 (setq curr-files (if (file-accessible-directory-p dir)
105 (directory-files dir nil ".\\.elc?\\(\\.gz\\)?$" t)))
106 (and curr-files
107 (not noninteractive)
108 (message "Checking %d files in %s..." (length curr-files) dir))
109
110 (setq files-seen-this-dir nil)
111
112 (while curr-files
113
114 (setq file (car curr-files))
115 (if (string-match "\\.gz$" file)
116 (setq file (substring file 0 -3)))
117 (setq file (substring
118 file 0 (if (string= (substring file -1) "c") -4 -3)))
119
120 ;; FILE now contains the current file name, with no suffix.
121 (unless (or (member file files-seen-this-dir)
122 ;; Ignore these files.
123 (member file '("subdirs")))
124 ;; File has not been seen yet in this directory.
125 ;; This test prevents us declaring that XXX.el shadows
126 ;; XXX.elc (or vice-versa) when they are in the same directory.
127 (setq files-seen-this-dir (cons file files-seen-this-dir))
128
129 (if (setq orig-dir (assoc file files))
130 ;; This file was seen before, we have a shadowing.
131 ;; Report it unless the files are identical.
132 (let ((base1 (concat (cdr orig-dir) "/" file))
133 (base2 (concat dir "/" file)))
134 (if (not (and shadows-compare-text-p
135 (shadow-same-file-or-nonexistent
136 (concat base1 ".el") (concat base2 ".el"))
137 ;; This is a bit strict, but safe.
138 (shadow-same-file-or-nonexistent
139 (concat base1 ".elc") (concat base2 ".elc"))))
140 (setq shadows
141 (append shadows (list base1 base2)))))
142
143 ;; Not seen before, add it to the list of seen files.
144 (setq files (cons (cons file dir) files))))
145
146 (setq curr-files (cdr curr-files))))
147 (setq path (cdr path)))
148
149 ;; Return the list of shadowings.
150 shadows))
151
152 ;; Return true if neither file exists, or if both exist and have identical
153 ;; contents.
154 (defun shadow-same-file-or-nonexistent (f1 f2)
155 (let ((exists1 (file-exists-p f1))
156 (exists2 (file-exists-p f2)))
157 (or (and (not exists1) (not exists2))
158 (and exists1 exists2
159 (or (equal (file-truename f1) (file-truename f2))
160 ;; As a quick test, avoiding spawning a process, compare file
161 ;; sizes.
162 (and (= (nth 7 (file-attributes f1))
163 (nth 7 (file-attributes f2)))
164 (eq 0 (call-process "cmp" nil nil nil "-s" f1 f2))))))))
165 \f
166 ;;;###autoload
167 (defun list-load-path-shadows (&optional stringp)
168 "Display a list of Emacs Lisp files that shadow other files.
169
170 If STRINGP is non-nil, returns any shadows as a string.
171 Otherwise, if interactive shows any shadows in a `*Shadows*' buffer;
172 else prints messages listing any shadows.
173
174 This function lists potential load path problems. Directories in
175 the `load-path' variable are searched, in order, for Emacs Lisp
176 files. When a previously encountered file name is found again, a
177 message is displayed indicating that the later file is \"hidden\" by
178 the earlier.
179
180 For example, suppose `load-path' is set to
181
182 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\)
183
184 and that each of these directories contains a file called XXX.el. Then
185 XXX.el in the site-lisp directory is referred to by all of:
186 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc.
187
188 The first XXX.el file prevents Emacs from seeing the second \(unless
189 the second is loaded explicitly via `load-file'\).
190
191 When not intended, such shadowings can be the source of subtle
192 problems. For example, the above situation may have arisen because the
193 XXX package was not distributed with versions of Emacs prior to
194 19.30. An Emacs maintainer downloaded XXX from elsewhere and installed
195 it. Later, XXX was updated and included in the Emacs distribution.
196 Unless the Emacs maintainer checks for this, the new version of XXX
197 will be hidden behind the old \(which may no longer work with the new
198 Emacs version\).
199
200 This function performs these checks and flags all possible
201 shadowings. Because a .el file may exist without a corresponding .elc
202 \(or vice-versa\), these suffixes are essentially ignored. A file
203 XXX.elc in an early directory \(that does not contain XXX.el\) is
204 considered to shadow a later file XXX.el, and vice-versa.
205
206 Shadowings are located by calling the (non-interactive) companion
207 function, `find-emacs-lisp-shadows'."
208 (interactive)
209 (let* ((path (copy-sequence load-path))
210 (tem path)
211 toplevs)
212 ;; If we can find simple.el in two places,
213 (while tem
214 (if (or (file-exists-p (expand-file-name "simple.el" (car tem)))
215 (file-exists-p (expand-file-name "simple.el.gz" (car tem))))
216 (setq toplevs (cons (car tem) toplevs)))
217 (setq tem (cdr tem)))
218 (if (> (length toplevs) 1)
219 ;; Cut off our copy of load-path right before
220 ;; the last directory which has simple.el in it.
221 ;; This avoids loads of duplications between the source dir
222 ;; and the dir where these files were copied by installation.
223 (let ((break (car toplevs)))
224 (setq tem path)
225 (while tem
226 (if (eq (nth 1 tem) break)
227 (progn
228 (setcdr tem nil)
229 (setq tem nil)))
230 (setq tem (cdr tem)))))
231
232 (let* ((shadows (find-emacs-lisp-shadows path))
233 (n (/ (length shadows) 2))
234 (msg (format "%s Emacs Lisp load-path shadowing%s found"
235 (if (zerop n) "No" (concat "\n" (number-to-string n)))
236 (if (= n 1) " was" "s were"))))
237 (with-temp-buffer
238 (while shadows
239 (insert (format "%s hides %s\n" (car shadows)
240 (car (cdr shadows))))
241 (setq shadows (cdr (cdr shadows))))
242 (if stringp
243 (buffer-string)
244 (if (called-interactively-p 'interactive)
245 (save-excursion
246 ;; We are interactive.
247 ;; Create the *Shadows* buffer and display shadowings there.
248 (let ((string (buffer-string))
249 (output-buffer (get-buffer-create "*Shadows*")))
250 (display-buffer output-buffer)
251 (set-buffer output-buffer)
252 (erase-buffer)
253 (insert string)
254 (insert msg "\n")))
255 ;; We are non-interactive, print shadows via message.
256 (unless (zerop n)
257 (message "This site has duplicate Lisp libraries with the same name.
258 If a locally-installed Lisp library overrides a library in the Emacs release,
259 that can cause trouble, and you should probably remove the locally-installed
260 version unless you know what you are doing.\n")
261 (goto-char (point-min))
262 ;; Mimic the previous behavior of using lots of messages.
263 ;; I think one single message would look better...
264 (while (not (eobp))
265 (message "%s" (buffer-substring (line-beginning-position)
266 (line-end-position)))
267 (forward-line 1))
268 (message "%s" msg))))))))
269
270 (provide 'shadow)
271
272 ;; arch-tag: 0480e8a7-62ed-4a12-a9f6-f44ded9b0830
273 ;;; shadow.el ends here