(easy-mmode-define-navigation): Put `definition-name' properties on the
[emacs.git] / lisp / emacs-lisp / shadow.el
blob9e68fb0896753a8321f022d7f6f3d0d7e6a18d26
1 ;;; shadow.el --- locate Emacs Lisp file shadowings
3 ;; Copyright (C) 1995, 2002, 2003, 2004, 2005 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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
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.
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.
41 ;; The `list-load-path-shadows' function was run when you installed
42 ;; this version of emacs. To run it by hand in emacs:
44 ;; M-x load-library RET shadow RET
45 ;; M-x list-load-path-shadows
47 ;; or run it non-interactively via:
49 ;; emacs -batch -l shadow.el -f list-load-path-shadows
51 ;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions,
52 ;; rewritings & speedups.
54 ;;; Code:
56 (defgroup lisp-shadow nil
57 "Locate Emacs Lisp file shadowings."
58 :prefix "shadows-"
59 :group 'lisp)
61 (defcustom shadows-compare-text-p 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 find-emacs-lisp-shadows (&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."
78 (or path (setq path load-path))
80 (let (true-names ; List of dirs considered.
81 shadows ; List of shadowings, to be returned.
82 files ; File names ever seen, with dirs.
83 dir ; The dir being currently scanned.
84 curr-files ; This dir's Emacs Lisp files.
85 orig-dir ; Where the file was first seen.
86 files-seen-this-dir ; Files seen so far in this dir.
87 file) ; The current file.
90 (while path
92 (setq dir (directory-file-name (file-truename (or (car path) "."))))
93 (if (member dir true-names)
94 ;; We have already considered this PATH redundant directory.
95 ;; Show the redundancy if we are interactiver, unless the PATH
96 ;; dir is nil or "." (these redundant directories are just a
97 ;; result of the current working directory, and are therefore
98 ;; not always redundant).
99 (or noninteractive
100 (and (car path)
101 (not (string= (car path) "."))
102 (message "Ignoring redundant directory %s" (car path))))
104 (setq true-names (append true-names (list dir)))
105 (setq dir (directory-file-name (or (car path) ".")))
106 (setq curr-files (if (file-accessible-directory-p dir)
107 (directory-files dir nil ".\\.elc?\\(\\.gz\\)?$" t)))
108 (and curr-files
109 (not noninteractive)
110 (message "Checking %d files in %s..." (length curr-files) dir))
112 (setq files-seen-this-dir nil)
114 (while curr-files
116 (setq file (car curr-files))
117 (if (string-match "\\.gz$" file)
118 (setq file (substring file 0 -3)))
119 (setq file (substring
120 file 0 (if (string= (substring file -1) "c") -4 -3)))
122 ;; FILE now contains the current file name, with no suffix.
123 (unless (or (member file files-seen-this-dir)
124 ;; Ignore these files.
125 (member file '("subdirs")))
126 ;; File has not been seen yet in this directory.
127 ;; This test prevents us declaring that XXX.el shadows
128 ;; XXX.elc (or vice-versa) when they are in the same directory.
129 (setq files-seen-this-dir (cons file files-seen-this-dir))
131 (if (setq orig-dir (assoc file files))
132 ;; This file was seen before, we have a shadowing.
133 ;; Report it unless the files are identical.
134 (let ((base1 (concat (cdr orig-dir) "/" file))
135 (base2 (concat dir "/" file)))
136 (if (not (and shadows-compare-text-p
137 (shadow-same-file-or-nonexistent
138 (concat base1 ".el") (concat base2 ".el"))
139 ;; This is a bit strict, but safe.
140 (shadow-same-file-or-nonexistent
141 (concat base1 ".elc") (concat base2 ".elc"))))
142 (setq shadows
143 (append shadows (list base1 base2)))))
145 ;; Not seen before, add it to the list of seen files.
146 (setq files (cons (cons file dir) files))))
148 (setq curr-files (cdr curr-files))))
149 (setq path (cdr path)))
151 ;; Return the list of shadowings.
152 shadows))
154 ;; Return true if neither file exists, or if both exist and have identical
155 ;; contents.
156 (defun shadow-same-file-or-nonexistent (f1 f2)
157 (let ((exists1 (file-exists-p f1))
158 (exists2 (file-exists-p f2)))
159 (or (and (not exists1) (not exists2))
160 (and exists1 exists2
161 (or (equal (file-truename f1) (file-truename f2))
162 ;; As a quick test, avoiding spawning a process, compare file
163 ;; sizes.
164 (and (= (nth 7 (file-attributes f1))
165 (nth 7 (file-attributes f2)))
166 (eq 0 (call-process "cmp" nil nil nil "-s" f1 f2))))))))
168 ;;;###autoload
169 (defun list-load-path-shadows ()
170 "Display a list of Emacs Lisp files that shadow other files.
172 This function lists potential load-path problems. Directories in the
173 `load-path' variable are searched, in order, for Emacs Lisp
174 files. When a previously encountered file name is found again, a
175 message is displayed indicating that the later file is \"hidden\" by
176 the earlier.
178 For example, suppose `load-path' is set to
180 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\)
182 and that each of these directories contains a file called XXX.el. Then
183 XXX.el in the site-lisp directory is referred to by all of:
184 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc.
186 The first XXX.el file prevents emacs from seeing the second \(unless
187 the second is loaded explicitly via load-file\).
189 When not intended, such shadowings can be the source of subtle
190 problems. For example, the above situation may have arisen because the
191 XXX package was not distributed with versions of emacs prior to
192 19.30. An emacs maintainer downloaded XXX from elsewhere and installed
193 it. Later, XXX was updated and included in the emacs distribution.
194 Unless the emacs maintainer checks for this, the new version of XXX
195 will be hidden behind the old \(which may no longer work with the new
196 emacs version\).
198 This function performs these checks and flags all possible
199 shadowings. Because a .el file may exist without a corresponding .elc
200 \(or vice-versa\), these suffixes are essentially ignored. A file
201 XXX.elc in an early directory \(that does not contain XXX.el\) is
202 considered to shadow a later file XXX.el, and vice-versa.
204 When run interactively, the shadowings \(if any\) are displayed in a
205 buffer called `*Shadows*'. Shadowings are located by calling the
206 \(non-interactive\) companion 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)))))
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 (if (interactive-p)
238 (save-excursion
239 ;; We are interactive.
240 ;; Create the *Shadows* buffer and display shadowings there.
241 (let ((output-buffer (get-buffer-create "*Shadows*")))
242 (display-buffer output-buffer)
243 (set-buffer output-buffer)
244 (erase-buffer)
245 (while shadows
246 (insert (format "%s hides %s\n" (car shadows)
247 (car (cdr shadows))))
248 (setq shadows (cdr (cdr shadows))))
249 (insert msg "\n")))
250 ;; We are non-interactive, print shadows via message.
251 (when shadows
252 (message "This site has duplicate Lisp libraries with the same name.
253 If a locally-installed Lisp library overrides a library in the Emacs release,
254 that can cause trouble, and you should probably remove the locally-installed
255 version unless you know what you are doing.\n")
256 (while shadows
257 (message "%s hides %s" (car shadows) (car (cdr shadows)))
258 (setq shadows (cdr (cdr shadows))))
259 (message "%s" msg))))))
261 (provide 'shadow)
263 ;;; arch-tag: 0480e8a7-62ed-4a12-a9f6-f44ded9b0830
264 ;;; shadow.el ends here