Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / emacs-lisp / shadow.el
blobaba929035d1dc6a1026894d5d3ab61cb2fcd4005
1 ;;; shadow.el --- locate Emacs Lisp file shadowings
3 ;; Copyright (C) 1995, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Terry Jones <terry@santafe.edu>
6 ;; Keywords: lisp
7 ;; Created: 15 December 1995
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; The functions in this file detect (`load-path-shadows-find')
27 ;; and display (`list-load-path-shadows') potential load-path
28 ;; problems that arise when Emacs Lisp files "shadow" each other.
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.
39 ;; The `list-load-path-shadows' function was run when you installed
40 ;; this version of emacs. To run it by hand in emacs:
42 ;; M-x list-load-path-shadows
44 ;; or run it non-interactively via:
46 ;; emacs -batch -f list-load-path-shadows
48 ;; Thanks to Francesco Potortì <pot@cnuce.cnr.it> for suggestions,
49 ;; rewritings & speedups.
51 ;;; Code:
53 (defgroup lisp-shadow nil
54 "Locate Emacs Lisp file shadowings."
55 :prefix "load-path-shadows-"
56 :group 'lisp)
58 (define-obsolete-variable-alias 'shadows-compare-text-p
59 'load-path-shadows-compare-text "23.3")
61 (defcustom load-path-shadows-compare-text nil
62 "If non-nil, then shadowing files are reported only if their text differs.
63 This is slower, but filters out some innocuous shadowing."
64 :type 'boolean
65 :group 'lisp-shadow)
67 (defun load-path-shadows-find (&optional path)
68 "Return a list of Emacs Lisp files that create shadows.
69 This function does the work for `list-load-path-shadows'.
71 We traverse PATH looking for shadows, and return a \(possibly empty\)
72 even-length list of files. A file in this list at position 2i shadows
73 the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\)
74 are stripped from the file names in the list.
76 See the documentation for `list-load-path-shadows' for further information."
77 (let (true-names ; List of dirs considered.
78 shadows ; List of shadowings, to be returned.
79 files ; File names ever seen, with dirs.
80 dir ; The dir being currently scanned.
81 curr-files ; This dir's Emacs Lisp files.
82 orig-dir ; Where the file was first seen.
83 files-seen-this-dir ; Files seen so far in this dir.
84 file) ; The current file.
85 (dolist (pp (or path load-path))
86 (setq dir (directory-file-name (file-truename (or pp "."))))
87 (if (member dir true-names)
88 ;; We have already considered this PATH redundant directory.
89 ;; Show the redundancy if we are interactive, unless the PATH
90 ;; dir is nil or "." (these redundant directories are just a
91 ;; result of the current working directory, and are therefore
92 ;; not always redundant).
93 (or noninteractive
94 (and pp
95 (not (string= pp "."))
96 (message "Ignoring redundant directory %s" pp)))
98 (setq true-names (append true-names (list dir)))
99 (setq dir (directory-file-name (or pp ".")))
100 (setq curr-files (if (file-accessible-directory-p dir)
101 (directory-files dir nil ".\\.elc?\\(\\.gz\\)?$" t)))
102 (and curr-files
103 (not noninteractive)
104 (message "Checking %d files in %s..." (length curr-files) dir))
106 (setq files-seen-this-dir nil)
108 (dolist (file curr-files)
110 (if (string-match "\\.gz$" file)
111 (setq file (substring file 0 -3)))
112 (setq file (substring
113 file 0 (if (string= (substring file -1) "c") -4 -3)))
115 ;; FILE now contains the current file name, with no suffix.
116 (unless (or (member file files-seen-this-dir)
117 ;; Ignore these files.
118 (member file '("subdirs" "leim-list")))
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))
124 (if (setq orig-dir (assoc file files))
125 ;; This file was seen before, we have a shadowing.
126 ;; Report it unless the files are identical.
127 (let ((base1 (concat (cdr orig-dir) "/" file))
128 (base2 (concat dir "/" file)))
129 (if (not (and load-path-shadows-compare-text
130 (load-path-shadows-same-file-or-nonexistent
131 (concat base1 ".el") (concat base2 ".el"))
132 ;; This is a bit strict, but safe.
133 (load-path-shadows-same-file-or-nonexistent
134 (concat base1 ".elc") (concat base2 ".elc"))))
135 (setq shadows
136 (append shadows (list base1 base2)))))
138 ;; Not seen before, add it to the list of seen files.
139 (setq files (cons (cons file dir) files)))))))
140 ;; Return the list of shadowings.
141 shadows))
143 (define-obsolete-function-alias 'find-emacs-lisp-shadows
144 'load-path-shadows-find "23.3")
146 ;; Return true if neither file exists, or if both exist and have identical
147 ;; contents.
148 (defun load-path-shadows-same-file-or-nonexistent (f1 f2)
149 (let ((exists1 (file-exists-p f1))
150 (exists2 (file-exists-p f2)))
151 (or (and (not exists1) (not exists2))
152 (and exists1 exists2
153 (or (equal (file-truename f1) (file-truename f2))
154 ;; As a quick test, avoiding spawning a process, compare file
155 ;; sizes.
156 (and (= (nth 7 (file-attributes f1))
157 (nth 7 (file-attributes f2)))
158 (eq 0 (call-process "cmp" nil nil nil "-s" f1 f2))))))))
160 (defvar load-path-shadows-font-lock-keywords
161 ;; The idea is that shadows of files supplied with Emacs are more
162 ;; serious than various versions of external packages shadowing each
163 ;; other.
164 `((,(format "hides \\(%s.*\\)"
165 (file-name-directory
166 (or (locate-library "simple")
167 (file-name-as-directory
168 (expand-file-name "../lisp" data-directory)))))
169 . (1 font-lock-warning-face)))
170 "Keywords to highlight in `load-path-shadows-mode'.")
172 (define-derived-mode load-path-shadows-mode fundamental-mode "LP-Shadows"
173 "Major mode for load-path shadows buffer."
174 (set (make-local-variable 'font-lock-defaults)
175 '((load-path-shadows-font-lock-keywords)))
176 (setq buffer-undo-list t
177 buffer-read-only t))
179 ;; TODO use text-properties instead, a la dired.
180 (require 'button)
181 (define-button-type 'load-path-shadows-find-file
182 'follow-link t
183 ;; 'face 'default
184 'action (lambda (button)
185 (let ((file (concat (button-get button 'shadow-file) ".el")))
186 (or (file-exists-p file)
187 (setq file (concat file ".gz")))
188 (if (file-readable-p file)
189 (pop-to-buffer (find-file-noselect file))
190 (error "Cannot read file"))))
191 'help-echo "mouse-2, RET: find this file")
194 ;;;###autoload
195 (defun list-load-path-shadows (&optional stringp)
196 "Display a list of Emacs Lisp files that shadow other files.
198 If STRINGP is non-nil, returns any shadows as a string.
199 Otherwise, if interactive shows any shadows in a `*Shadows*' buffer;
200 else prints messages listing any shadows.
202 This function lists potential load path problems. Directories in
203 the `load-path' variable are searched, in order, for Emacs Lisp
204 files. When a previously encountered file name is found again, a
205 message is displayed indicating that the later file is \"hidden\" by
206 the earlier.
208 For example, suppose `load-path' is set to
210 \(\"/usr/share/emacs/site-lisp\" \"/usr/share/emacs/24.3/lisp\")
212 and that each of these directories contains a file called XXX.el. Then
213 XXX.el in the site-lisp directory is referred to by all of:
214 \(require 'XXX), (autoload .... \"XXX\"), (load-library \"XXX\") etc.
216 The first XXX.el file prevents Emacs from seeing the second (unless
217 the second is loaded explicitly via `load-file').
219 When not intended, such shadowings can be the source of subtle
220 problems. For example, the above situation may have arisen because the
221 XXX package was not distributed with versions of Emacs prior to
222 24.3. A system administrator downloaded XXX from elsewhere and installed
223 it. Later, XXX was updated and included in the Emacs distribution.
224 Unless the system administrator checks for this, the new version of XXX
225 will be hidden behind the old (which may no longer work with the new
226 Emacs version).
228 This function performs these checks and flags all possible
229 shadowings. Because a .el file may exist without a corresponding .elc
230 \(or vice-versa), these suffixes are essentially ignored. A file
231 XXX.elc in an early directory (that does not contain XXX.el) is
232 considered to shadow a later file XXX.el, and vice-versa.
234 Shadowings are located by calling the (non-interactive) companion
235 function, `load-path-shadows-find'."
236 (interactive)
237 (let* ((shadows (load-path-shadows-find load-path))
238 (n (/ (length shadows) 2))
239 (msg (format "%s Emacs Lisp load-path shadowing%s found"
240 (if (zerop n) "No" (concat "\n" (number-to-string n)))
241 (if (= n 1) " was" "s were"))))
242 (with-temp-buffer
243 (while shadows
244 (insert (format "%s hides %s\n" (car shadows)
245 (car (cdr shadows))))
246 (setq shadows (cdr (cdr shadows))))
247 (if stringp
248 (buffer-string)
249 (if (called-interactively-p 'interactive)
250 ;; We are interactive.
251 ;; Create the *Shadows* buffer and display shadowings there.
252 (let ((string (buffer-string)))
253 (with-current-buffer (get-buffer-create "*Shadows*")
254 (display-buffer (current-buffer))
255 (load-path-shadows-mode) ; run after-change-major-mode-hook
256 (let ((inhibit-read-only t))
257 (erase-buffer)
258 (insert string)
259 (insert msg "\n")
260 (while (re-search-backward "\\(^.*\\) hides \\(.*$\\)"
261 nil t)
262 (dotimes (i 2)
263 (make-button (match-beginning (1+ i))
264 (match-end (1+ i))
265 'type 'load-path-shadows-find-file
266 'shadow-file
267 (match-string (1+ i)))))
268 (goto-char (point-max)))))
269 ;; We are non-interactive, print shadows via message.
270 (unless (zerop n)
271 (message "This site has duplicate Lisp libraries with the same name.
272 If a locally-installed Lisp library overrides a library in the Emacs release,
273 that can cause trouble, and you should probably remove the locally-installed
274 version unless you know what you are doing.\n")
275 (goto-char (point-min))
276 ;; Mimic the previous behavior of using lots of messages.
277 ;; I think one single message would look better...
278 (while (not (eobp))
279 (message "%s" (buffer-substring (line-beginning-position)
280 (line-end-position)))
281 (forward-line 1))
282 (message "%s" msg)))))))
284 (provide 'shadow)
286 ;;; shadow.el ends here
288 ;; Local Variables:
289 ;; coding: utf-8
290 ;; End: