Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / emacs-lisp / shadow.el
CommitLineData
e8af40ee 1;;; shadow.el --- locate Emacs Lisp file shadowings
50584ac0 2
73b0cd50 3;; Copyright (C) 1995, 2001-2011 Free Software Foundation, Inc.
50584ac0
KH
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
d6cba7ae 11;; GNU Emacs is free software: you can redistribute it and/or modify
50584ac0 12;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
50584ac0
KH
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
d6cba7ae 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
50584ac0
KH
23
24;;; Commentary:
b578f267 25
d15f9a2b 26;; The functions in this file detect (`load-path-shadows-find')
50584ac0
KH
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;;
50584ac0
KH
42;; M-x list-load-path-shadows
43;;
44;; or run it non-interactively via:
45;;
7ba65108 46;; emacs -batch -f list-load-path-shadows
50584ac0
KH
47;;
48;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions,
49;; rewritings & speedups.
50
51;;; Code:
52\f
4e2ad9ea 53(defgroup lisp-shadow nil
666b9413 54 "Locate Emacs Lisp file shadowings."
e5d49589 55 :prefix "load-path-shadows-"
666b9413
SE
56 :group 'lisp)
57
e5d49589
GM
58(define-obsolete-variable-alias 'shadows-compare-text-p
59 'load-path-shadows-compare-text "23.3")
60
61(defcustom load-path-shadows-compare-text nil
7ba65108 62 "If non-nil, then shadowing files are reported only if their text differs.
666b9413
SE
63This is slower, but filters out some innocuous shadowing."
64 :type 'boolean
4e2ad9ea 65 :group 'lisp-shadow)
b7797a3e 66
d15f9a2b 67(defun load-path-shadows-find (&optional path)
50584ac0
KH
68 "Return a list of Emacs Lisp files that create shadows.
69This function does the work for `list-load-path-shadows'.
70
71We traverse PATH looking for shadows, and return a \(possibly empty\)
72even-length list of files. A file in this list at position 2i shadows
73the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\)
74are stripped from the file names in the list.
75
76See the documentation for `list-load-path-shadows' for further information."
50584ac0
KH
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.
b16ff465
GM
85 (dolist (pp (or path load-path))
86 (setq dir (directory-file-name (file-truename (or pp "."))))
50584ac0
KH
87 (if (member dir true-names)
88 ;; We have already considered this PATH redundant directory.
f5bb9196 89 ;; Show the redundancy if we are interactive, unless the PATH
50584ac0
KH
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
b16ff465
GM
94 (and pp
95 (not (string= pp "."))
96 (message "Ignoring redundant directory %s" pp)))
b7797a3e 97
50584ac0 98 (setq true-names (append true-names (list dir)))
b16ff465 99 (setq dir (directory-file-name (or pp ".")))
50584ac0 100 (setq curr-files (if (file-accessible-directory-p dir)
da49096f 101 (directory-files dir nil ".\\.elc?\\(\\.gz\\)?$" t)))
50584ac0
KH
102 (and curr-files
103 (not noninteractive)
c2b7bdc2 104 (message "Checking %d files in %s..." (length curr-files) dir))
b7797a3e 105
50584ac0
KH
106 (setq files-seen-this-dir nil)
107
b16ff465 108 (dolist (file curr-files)
50584ac0 109
da49096f
AS
110 (if (string-match "\\.gz$" file)
111 (setq file (substring file 0 -3)))
50584ac0
KH
112 (setq file (substring
113 file 0 (if (string= (substring file -1) "c") -4 -3)))
114
5017dcaa
RS
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")))
50584ac0
KH
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))
a1506d29 123
50584ac0
KH
124 (if (setq orig-dir (assoc file files))
125 ;; This file was seen before, we have a shadowing.
b7797a3e
KH
126 ;; Report it unless the files are identical.
127 (let ((base1 (concat (cdr orig-dir) "/" file))
128 (base2 (concat dir "/" file)))
e5d49589
GM
129 (if (not (and load-path-shadows-compare-text
130 (load-path-shadows-same-file-or-nonexistent
b7797a3e
KH
131 (concat base1 ".el") (concat base2 ".el"))
132 ;; This is a bit strict, but safe.
e5d49589 133 (load-path-shadows-same-file-or-nonexistent
b7797a3e 134 (concat base1 ".elc") (concat base2 ".elc"))))
3a6a40e5
RS
135 (setq shadows
136 (append shadows (list base1 base2)))))
50584ac0
KH
137
138 ;; Not seen before, add it to the list of seen files.
b16ff465 139 (setq files (cons (cons file dir) files)))))))
50584ac0
KH
140 ;; Return the list of shadowings.
141 shadows))
142
d15f9a2b
GM
143(define-obsolete-function-alias 'find-emacs-lisp-shadows
144 'load-path-shadows-find "23.3")
145
b7797a3e
KH
146;; Return true if neither file exists, or if both exist and have identical
147;; contents.
e5d49589 148(defun load-path-shadows-same-file-or-nonexistent (f1 f2)
b7797a3e
KH
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)))
15502042 158 (eq 0 (call-process "cmp" nil nil nil "-s" f1 f2))))))))
7b9235ad 159
06d9ef85 160(defvar load-path-shadows-font-lock-keywords
7b9235ad
GM
161 `((,(format "hides \\(%s.*\\)"
162 (file-name-directory (locate-library "simple.el")))
163 . (1 font-lock-warning-face)))
ac44263a 164 "Keywords to highlight in `load-path-shadows-mode'.")
7b9235ad 165
ac44263a 166(define-derived-mode load-path-shadows-mode fundamental-mode "LP-Shadows"
7b9235ad
GM
167 "Major mode for load-path shadows buffer."
168 (set (make-local-variable 'font-lock-defaults)
06d9ef85 169 '((load-path-shadows-font-lock-keywords)))
7b9235ad
GM
170 (setq buffer-undo-list t
171 buffer-read-only t))
172
173;; TODO use text-properties instead, a la dired.
174(require 'button)
06d9ef85 175(define-button-type 'load-path-shadows-find-file
7b9235ad
GM
176 'follow-link t
177;; 'face 'default
178 'action (lambda (button)
179 (let ((file (concat (button-get button 'shadow-file) ".el")))
180 (or (file-exists-p file)
181 (setq file (concat file ".gz")))
182 (if (file-readable-p file)
183 (pop-to-buffer (find-file-noselect file))
184 (error "Cannot read file"))))
185 'help-echo "mouse-2, RET: find this file")
186
50584ac0
KH
187\f
188;;;###autoload
7ba65108 189(defun list-load-path-shadows (&optional stringp)
0f93e41f 190 "Display a list of Emacs Lisp files that shadow other files.
50584ac0 191
7ba65108
GM
192If STRINGP is non-nil, returns any shadows as a string.
193Otherwise, if interactive shows any shadows in a `*Shadows*' buffer;
194else prints messages listing any shadows.
195
f5bb9196
JB
196This function lists potential load path problems. Directories in
197the `load-path' variable are searched, in order, for Emacs Lisp
0f93e41f
RS
198files. When a previously encountered file name is found again, a
199message is displayed indicating that the later file is \"hidden\" by
50584ac0
KH
200the earlier.
201
202For example, suppose `load-path' is set to
203
204\(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\)
205
206and that each of these directories contains a file called XXX.el. Then
207XXX.el in the site-lisp directory is referred to by all of:
208\(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc.
209
32a37bc6
JB
210The first XXX.el file prevents Emacs from seeing the second \(unless
211the second is loaded explicitly via `load-file'\).
50584ac0
KH
212
213When not intended, such shadowings can be the source of subtle
214problems. For example, the above situation may have arisen because the
32a37bc6
JB
215XXX package was not distributed with versions of Emacs prior to
21619.30. An Emacs maintainer downloaded XXX from elsewhere and installed
217it. Later, XXX was updated and included in the Emacs distribution.
218Unless the Emacs maintainer checks for this, the new version of XXX
50584ac0 219will be hidden behind the old \(which may no longer work with the new
32a37bc6 220Emacs version\).
50584ac0
KH
221
222This function performs these checks and flags all possible
223shadowings. Because a .el file may exist without a corresponding .elc
224\(or vice-versa\), these suffixes are essentially ignored. A file
225XXX.elc in an early directory \(that does not contain XXX.el\) is
226considered to shadow a later file XXX.el, and vice-versa.
227
7ba65108 228Shadowings are located by calling the (non-interactive) companion
d15f9a2b 229function, `load-path-shadows-find'."
50584ac0 230 (interactive)
0cdbb11d
RS
231 (let* ((path (copy-sequence load-path))
232 (tem path)
233 toplevs)
234 ;; If we can find simple.el in two places,
b16ff465
GM
235 (dolist (tt tem)
236 (if (or (file-exists-p (expand-file-name "simple.el" tt))
237 (file-exists-p (expand-file-name "simple.el.gz" tt)))
238 (setq toplevs (cons tt toplevs))))
0cdbb11d
RS
239 (if (> (length toplevs) 1)
240 ;; Cut off our copy of load-path right before
8ab4da6c 241 ;; the last directory which has simple.el in it.
0cdbb11d
RS
242 ;; This avoids loads of duplications between the source dir
243 ;; and the dir where these files were copied by installation.
8ab4da6c 244 (let ((break (car toplevs)))
0cdbb11d
RS
245 (setq tem path)
246 (while tem
247 (if (eq (nth 1 tem) break)
248 (progn
249 (setcdr tem nil)
250 (setq tem nil)))
251 (setq tem (cdr tem)))))
252
d15f9a2b 253 (let* ((shadows (load-path-shadows-find path))
0cdbb11d
RS
254 (n (/ (length shadows) 2))
255 (msg (format "%s Emacs Lisp load-path shadowing%s found"
256 (if (zerop n) "No" (concat "\n" (number-to-string n)))
257 (if (= n 1) " was" "s were"))))
7ba65108
GM
258 (with-temp-buffer
259 (while shadows
260 (insert (format "%s hides %s\n" (car shadows)
261 (car (cdr shadows))))
262 (setq shadows (cdr (cdr shadows))))
263 (if stringp
264 (buffer-string)
32226619 265 (if (called-interactively-p 'interactive)
b16ff465
GM
266 ;; We are interactive.
267 ;; Create the *Shadows* buffer and display shadowings there.
268 (let ((string (buffer-string)))
269 (with-current-buffer (get-buffer-create "*Shadows*")
270 (display-buffer (current-buffer))
ac44263a 271 (load-path-shadows-mode) ; run after-change-major-mode-hook
7b9235ad
GM
272 (let ((inhibit-read-only t))
273 (erase-buffer)
274 (insert string)
275 (insert msg "\n")
276 (while (re-search-backward "\\(^.*\\) hides \\(.*$\\)"
277 nil t)
278 (dotimes (i 2)
279 (make-button (match-beginning (1+ i))
280 (match-end (1+ i))
06d9ef85
GM
281 'type 'load-path-shadows-find-file
282 'shadow-file
7b9235ad
GM
283 (match-string (1+ i)))))
284 (goto-char (point-max)))))
7ba65108
GM
285 ;; We are non-interactive, print shadows via message.
286 (unless (zerop n)
287 (message "This site has duplicate Lisp libraries with the same name.
5017dcaa
RS
288If a locally-installed Lisp library overrides a library in the Emacs release,
289that can cause trouble, and you should probably remove the locally-installed
248a9f6d 290version unless you know what you are doing.\n")
7ba65108
GM
291 (goto-char (point-min))
292 ;; Mimic the previous behavior of using lots of messages.
293 ;; I think one single message would look better...
294 (while (not (eobp))
295 (message "%s" (buffer-substring (line-beginning-position)
296 (line-end-position)))
297 (forward-line 1))
298 (message "%s" msg))))))))
50584ac0
KH
299
300(provide 'shadow)
301
302;;; shadow.el ends here