Mercurial > emacs
annotate lisp/emacs-lisp/shadow.el @ 20762:ec3a35db5352
(menu-bar-file-menu): Add Recover Session menu item.
(menu-bar-help-menu): Add Getting New Versions, Copying Conditions
and (Non)Warranty menu items.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 24 Jan 1998 02:23:15 +0000 |
parents | 199256234202 |
children | db005054f15d |
rev | line source |
---|---|
14058 | 1 ;;; shadow.el --- Locate Emacs Lisp file shadowings. |
2 | |
3 ;; Copyright (C) 1995 Free Software Foundation, Inc. | |
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 | |
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. | |
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 | |
14169 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
14058 | 25 |
26 ;;; Commentary: | |
14169 | 27 |
14058 | 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. | |
31 ;; | |
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. | |
40 ;; | |
41 ;; The `list-load-path-shadows' function was run when you installed | |
42 ;; this version of emacs. To run it by hand in emacs: | |
43 ;; | |
44 ;; M-x load-library RET shadow RET | |
45 ;; M-x list-load-path-shadows | |
46 ;; | |
47 ;; or run it non-interactively via: | |
48 ;; | |
49 ;; emacs -batch -l shadow.el -f list-load-path-shadows | |
50 ;; | |
51 ;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions, | |
52 ;; rewritings & speedups. | |
53 | |
54 ;;; Code: | |
55 | |
19982 | 56 (defvar shadows-compare-text-p nil |
57 "*If non-nil, then shadowing files are reported only if their text differs. | |
58 This is slower, but filters out some innocuous shadowing.") | |
59 | |
14058 | 60 (defun find-emacs-lisp-shadows (&optional path) |
61 "Return a list of Emacs Lisp files that create shadows. | |
62 This function does the work for `list-load-path-shadows'. | |
63 | |
64 We traverse PATH looking for shadows, and return a \(possibly empty\) | |
65 even-length list of files. A file in this list at position 2i shadows | |
66 the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\) | |
67 are stripped from the file names in the list. | |
68 | |
69 See the documentation for `list-load-path-shadows' for further information." | |
70 | |
71 (or path (setq path load-path)) | |
72 | |
73 (let (true-names ; List of dirs considered. | |
74 shadows ; List of shadowings, to be returned. | |
75 files ; File names ever seen, with dirs. | |
76 dir ; The dir being currently scanned. | |
77 curr-files ; This dir's Emacs Lisp files. | |
78 orig-dir ; Where the file was first seen. | |
79 files-seen-this-dir ; Files seen so far in this dir. | |
80 file) ; The current file. | |
81 | |
82 | |
83 (while path | |
84 | |
19982 | 85 (setq dir (directory-file-name (file-truename (or (car path) ".")))) |
14058 | 86 (if (member dir true-names) |
87 ;; We have already considered this PATH redundant directory. | |
88 ;; Show the redundancy if we are interactiver, unless the PATH | |
89 ;; dir is nil or "." (these redundant directories are just a | |
90 ;; result of the current working directory, and are therefore | |
91 ;; not always redundant). | |
92 (or noninteractive | |
93 (and (car path) | |
94 (not (string= (car path) ".")) | |
15736
73a325c414a5
(list-load-path-shadows): Fix ambiguous wording.
Karl Heuer <kwzh@gnu.org>
parents:
14348
diff
changeset
|
95 (message "Ignoring redundant directory %s" (car path)))) |
19982 | 96 |
14058 | 97 (setq true-names (append true-names (list dir))) |
19982 | 98 (setq dir (directory-file-name (or (car path) "."))) |
14058 | 99 (setq curr-files (if (file-accessible-directory-p dir) |
19982 | 100 (directory-files dir nil ".\\.elc?$" t))) |
14058 | 101 (and curr-files |
102 (not noninteractive) | |
15736
73a325c414a5
(list-load-path-shadows): Fix ambiguous wording.
Karl Heuer <kwzh@gnu.org>
parents:
14348
diff
changeset
|
103 (message "Checking %d files in %s..." (length curr-files) dir)) |
19982 | 104 |
14058 | 105 (setq files-seen-this-dir nil) |
106 | |
107 (while curr-files | |
108 | |
109 (setq file (car curr-files)) | |
110 (setq file (substring | |
111 file 0 (if (string= (substring file -1) "c") -4 -3))) | |
112 | |
19226
c160218de690
(find-emacs-lisp-shadows): Don't mention `subdirs.el'.
Richard M. Stallman <rms@gnu.org>
parents:
15756
diff
changeset
|
113 ;; FILE now contains the current file name, with no suffix. |
c160218de690
(find-emacs-lisp-shadows): Don't mention `subdirs.el'.
Richard M. Stallman <rms@gnu.org>
parents:
15756
diff
changeset
|
114 (unless (or (member file files-seen-this-dir) |
c160218de690
(find-emacs-lisp-shadows): Don't mention `subdirs.el'.
Richard M. Stallman <rms@gnu.org>
parents:
15756
diff
changeset
|
115 ;; Ignore these files. |
c160218de690
(find-emacs-lisp-shadows): Don't mention `subdirs.el'.
Richard M. Stallman <rms@gnu.org>
parents:
15756
diff
changeset
|
116 (member file '("subdirs"))) |
14058 | 117 ;; File has not been seen yet in this directory. |
118 ;; This test prevents us declaring that XXX.el shadows | |
119 ;; XXX.elc (or vice-versa) when they are in the same directory. | |
120 (setq files-seen-this-dir (cons file files-seen-this-dir)) | |
121 | |
122 (if (setq orig-dir (assoc file files)) | |
123 ;; This file was seen before, we have a shadowing. | |
19982 | 124 ;; Report it unless the files are identical. |
125 (let ((base1 (concat (cdr orig-dir) "/" file)) | |
126 (base2 (concat dir "/" file))) | |
127 (if (not (and shadows-compare-text-p | |
128 (shadow-same-file-or-nonexistent | |
129 (concat base1 ".el") (concat base2 ".el")) | |
130 ;; This is a bit strict, but safe. | |
131 (shadow-same-file-or-nonexistent | |
132 (concat base1 ".elc") (concat base2 ".elc")))) | |
14058 | 133 (setq shadows |
19982 | 134 (append shadows (list base1 base2))))) |
14058 | 135 |
136 ;; Not seen before, add it to the list of seen files. | |
137 (setq files (cons (cons file dir) files)))) | |
138 | |
139 (setq curr-files (cdr curr-files)))) | |
140 (setq path (cdr path))) | |
141 | |
142 ;; Return the list of shadowings. | |
143 shadows)) | |
144 | |
19982 | 145 ;; Return true if neither file exists, or if both exist and have identical |
146 ;; contents. | |
147 (defun shadow-same-file-or-nonexistent (f1 f2) | |
148 (let ((exists1 (file-exists-p f1)) | |
149 (exists2 (file-exists-p f2))) | |
150 (or (and (not exists1) (not exists2)) | |
151 (and exists1 exists2 | |
152 (or (equal (file-truename f1) (file-truename f2)) | |
153 ;; As a quick test, avoiding spawning a process, compare file | |
154 ;; sizes. | |
155 (and (= (nth 7 (file-attributes f1)) | |
156 (nth 7 (file-attributes f2))) | |
157 (zerop (call-process "cmp" nil nil nil "-s" f1 f2)))))))) | |
14058 | 158 |
159 ;;;###autoload | |
160 (defun list-load-path-shadows () | |
15756
30e9db641e6f
(list-load-path-shadows): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
15736
diff
changeset
|
161 "Display a list of Emacs Lisp files that shadow other files. |
14058 | 162 |
163 This function lists potential load-path problems. Directories in the | |
164 `load-path' variable are searched, in order, for Emacs Lisp | |
15756
30e9db641e6f
(list-load-path-shadows): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
15736
diff
changeset
|
165 files. When a previously encountered file name is found again, a |
30e9db641e6f
(list-load-path-shadows): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
15736
diff
changeset
|
166 message is displayed indicating that the later file is \"hidden\" by |
14058 | 167 the earlier. |
168 | |
169 For example, suppose `load-path' is set to | |
170 | |
171 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\) | |
172 | |
173 and that each of these directories contains a file called XXX.el. Then | |
174 XXX.el in the site-lisp directory is referred to by all of: | |
175 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc. | |
176 | |
177 The first XXX.el file prevents emacs from seeing the second \(unless | |
178 the second is loaded explicitly via load-file\). | |
179 | |
180 When not intended, such shadowings can be the source of subtle | |
181 problems. For example, the above situation may have arisen because the | |
182 XXX package was not distributed with versions of emacs prior to | |
183 19.30. An emacs maintainer downloaded XXX from elsewhere and installed | |
184 it. Later, XXX was updated and included in the emacs distribution. | |
185 Unless the emacs maintainer checks for this, the new version of XXX | |
186 will be hidden behind the old \(which may no longer work with the new | |
187 emacs version\). | |
188 | |
189 This function performs these checks and flags all possible | |
190 shadowings. Because a .el file may exist without a corresponding .elc | |
191 \(or vice-versa\), these suffixes are essentially ignored. A file | |
192 XXX.elc in an early directory \(that does not contain XXX.el\) is | |
193 considered to shadow a later file XXX.el, and vice-versa. | |
194 | |
195 When run interactively, the shadowings \(if any\) are displayed in a | |
196 buffer called `*Shadows*'. Shadowings are located by calling the | |
197 \(non-interactive\) companion function, `find-emacs-lisp-shadows'." | |
198 | |
199 (interactive) | |
19313
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
200 (let* ((path (copy-sequence load-path)) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
201 (tem path) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
202 toplevs) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
203 ;; If we can find simple.el in two places, |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
204 (while tem |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
205 (if (file-exists-p (expand-file-name "simple.el" (car tem))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
206 (setq toplevs (cons (car tem) toplevs))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
207 (setq tem (cdr tem))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
208 (if (> (length toplevs) 1) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
209 ;; Cut off our copy of load-path right before |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
210 ;; the second directory which has simple.el in it. |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
211 ;; This avoids loads of duplications between the source dir |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
212 ;; and the dir where these files were copied by installation. |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
213 (let ((break (nth (- (length toplevs) 2) toplevs))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
214 (setq tem path) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
215 (while tem |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
216 (if (eq (nth 1 tem) break) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
217 (progn |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
218 (setcdr tem nil) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
219 (setq tem nil))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
220 (setq tem (cdr tem))))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
221 |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
222 (let* ((shadows (find-emacs-lisp-shadows path)) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
223 (n (/ (length shadows) 2)) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
224 (msg (format "%s Emacs Lisp load-path shadowing%s found" |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
225 (if (zerop n) "No" (concat "\n" (number-to-string n))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
226 (if (= n 1) " was" "s were")))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
227 (if (interactive-p) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
228 (save-excursion |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
229 ;; We are interactive. |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
230 ;; Create the *Shadows* buffer and display shadowings there. |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
231 (let ((output-buffer (get-buffer-create "*Shadows*"))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
232 (display-buffer output-buffer) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
233 (set-buffer output-buffer) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
234 (erase-buffer) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
235 (while shadows |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
236 (insert (format "%s hides %s\n" (car shadows) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
237 (car (cdr shadows)))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
238 (setq shadows (cdr (cdr shadows)))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
239 (insert msg "\n"))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
240 ;; We are non-interactive, print shadows via message. |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
241 (when shadows |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
242 (message "This site has duplicate Lisp libraries with the same name. |
19226
c160218de690
(find-emacs-lisp-shadows): Don't mention `subdirs.el'.
Richard M. Stallman <rms@gnu.org>
parents:
15756
diff
changeset
|
243 If a locally-installed Lisp library overrides a library in the Emacs release, |
c160218de690
(find-emacs-lisp-shadows): Don't mention `subdirs.el'.
Richard M. Stallman <rms@gnu.org>
parents:
15756
diff
changeset
|
244 that can cause trouble, and you should probably remove the locally-installed |
c160218de690
(find-emacs-lisp-shadows): Don't mention `subdirs.el'.
Richard M. Stallman <rms@gnu.org>
parents:
15756
diff
changeset
|
245 version unless you know what you are doing.\n")) |
19313
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
246 (while shadows |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
247 (message "%s hides %s" (car shadows) (car (cdr shadows))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
248 (setq shadows (cdr (cdr shadows)))) |
04175c55c49b
(list-load-path-shadows): Exclude, from the path we search, all but
Richard M. Stallman <rms@gnu.org>
parents:
19226
diff
changeset
|
249 (message "%s" msg))))) |
14058 | 250 |
251 (provide 'shadow) | |
252 | |
253 ;;; shadow.el ends here |