Mercurial > hg > xemacs-beta
comparison lisp/shadow.el @ 217:d44af0c54775 r20-4b7
Import from CVS: tag r20-4b7
author | cvs |
---|---|
date | Mon, 13 Aug 2007 10:08:34 +0200 |
parents | |
children | 0e522484dd2a |
comparison
equal
deleted
inserted
replaced
216:43306a74e31c | 217:d44af0c54775 |
---|---|
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 | |
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. | |
25 | |
26 ;;; Commentary: | |
27 | |
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 | |
56 (defun find-emacs-lisp-shadows (&optional path) | |
57 "Return a list of Emacs Lisp files that create shadows. | |
58 This function does the work for `list-load-path-shadows'. | |
59 | |
60 We traverse PATH looking for shadows, and return a \(possibly empty\) | |
61 even-length list of files. A file in this list at position 2i shadows | |
62 the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\) | |
63 are stripped from the file names in the list. | |
64 | |
65 See the documentation for `list-load-path-shadows' for further information." | |
66 | |
67 (or path (setq path load-path)) | |
68 | |
69 (let (true-names ; List of dirs considered. | |
70 shadows ; List of shadowings, to be returned. | |
71 files ; File names ever seen, with dirs. | |
72 dir ; The dir being currently scanned. | |
73 curr-files ; This dir's Emacs Lisp files. | |
74 orig-dir ; Where the file was first seen. | |
75 files-seen-this-dir ; Files seen so far in this dir. | |
76 file) ; The current file. | |
77 | |
78 | |
79 (while path | |
80 | |
81 (setq dir (file-truename (or (car path) "."))) | |
82 (if (member dir true-names) | |
83 ;; We have already considered this PATH redundant directory. | |
84 ;; Show the redundancy if we are interactiver, unless the PATH | |
85 ;; dir is nil or "." (these redundant directories are just a | |
86 ;; result of the current working directory, and are therefore | |
87 ;; not always redundant). | |
88 (or noninteractive | |
89 (and (car path) | |
90 (not (string= (car path) ".")) | |
91 (message "Ignoring redundant directory %s" (car path)))) | |
92 | |
93 (setq true-names (append true-names (list dir))) | |
94 (setq dir (or (car path) ".")) | |
95 (setq curr-files (if (file-accessible-directory-p dir) | |
96 (directory-files dir nil ".\\.elc?$" t))) | |
97 (and curr-files | |
98 (not noninteractive) | |
99 (message "Checking %d files in %s..." (length curr-files) dir)) | |
100 | |
101 (setq files-seen-this-dir nil) | |
102 | |
103 (while curr-files | |
104 | |
105 (setq file (car curr-files)) | |
106 (setq file (substring | |
107 file 0 (if (string= (substring file -1) "c") -4 -3))) | |
108 | |
109 ;; FILE now contains the current file name, with no suffix. | |
110 (unless (or (member file files-seen-this-dir) | |
111 ;; Ignore these files. | |
112 (member file | |
113 '("subdirs" | |
114 "auto-autoloads" | |
115 "custom-load" | |
116 "lpath"))) | |
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. | |
124 (setq shadows | |
125 (append shadows | |
126 (list (concat (file-name-as-directory (cdr orig-dir)) | |
127 file) | |
128 (concat (file-name-as-directory dir) | |
129 file)))) | |
130 | |
131 ;; Not seen before, add it to the list of seen files. | |
132 (setq files (cons (cons file dir) files)))) | |
133 | |
134 (setq curr-files (cdr curr-files)))) | |
135 (setq path (cdr path))) | |
136 | |
137 ;; Return the list of shadowings. | |
138 shadows)) | |
139 | |
140 | |
141 ;;;###autoload | |
142 (defun list-load-path-shadows () | |
143 "Display a list of Emacs Lisp files that shadow other files. | |
144 | |
145 This function lists potential load-path problems. Directories in the | |
146 `load-path' variable are searched, in order, for Emacs Lisp | |
147 files. When a previously encountered file name is found again, a | |
148 message is displayed indicating that the later file is \"hidden\" by | |
149 the earlier. | |
150 | |
151 For example, suppose `load-path' is set to | |
152 | |
153 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\) | |
154 | |
155 and that each of these directories contains a file called XXX.el. Then | |
156 XXX.el in the site-lisp directory is referred to by all of: | |
157 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc. | |
158 | |
159 The first XXX.el file prevents emacs from seeing the second \(unless | |
160 the second is loaded explicitly via load-file\). | |
161 | |
162 When not intended, such shadowings can be the source of subtle | |
163 problems. For example, the above situation may have arisen because the | |
164 XXX package was not distributed with versions of emacs prior to | |
165 19.30. An emacs maintainer downloaded XXX from elsewhere and installed | |
166 it. Later, XXX was updated and included in the emacs distribution. | |
167 Unless the emacs maintainer checks for this, the new version of XXX | |
168 will be hidden behind the old \(which may no longer work with the new | |
169 emacs version\). | |
170 | |
171 This function performs these checks and flags all possible | |
172 shadowings. Because a .el file may exist without a corresponding .elc | |
173 \(or vice-versa\), these suffixes are essentially ignored. A file | |
174 XXX.elc in an early directory \(that does not contain XXX.el\) is | |
175 considered to shadow a later file XXX.el, and vice-versa. | |
176 | |
177 When run interactively, the shadowings \(if any\) are displayed in a | |
178 buffer called `*Shadows*'. Shadowings are located by calling the | |
179 \(non-interactive\) companion function, `find-emacs-lisp-shadows'." | |
180 | |
181 (interactive) | |
182 (let* ((path (copy-sequence load-path)) | |
183 (tem path) | |
184 toplevs) | |
185 ;; If we can find simple.el in two places, | |
186 (while tem | |
187 (if (file-exists-p (expand-file-name "simple.el" (car tem))) | |
188 (setq toplevs (cons (car tem) toplevs))) | |
189 (setq tem (cdr tem))) | |
190 (if (> (length toplevs) 1) | |
191 ;; Cut off our copy of load-path right before | |
192 ;; the second directory which has simple.el in it. | |
193 ;; This avoids loads of duplications between the source dir | |
194 ;; and the dir where these files were copied by installation. | |
195 (let ((break (nth (- (length toplevs) 2) toplevs))) | |
196 (setq tem path) | |
197 (while tem | |
198 (if (eq (nth 1 tem) break) | |
199 (progn | |
200 (setcdr tem nil) | |
201 (setq tem nil))) | |
202 (setq tem (cdr tem))))) | |
203 | |
204 (let* ((shadows (find-emacs-lisp-shadows path)) | |
205 (n (/ (length shadows) 2)) | |
206 (msg (format "%s Emacs Lisp load-path shadowing%s found" | |
207 (if (zerop n) "No" (concat "\n" (number-to-string n))) | |
208 (if (= n 1) " was" "s were")))) | |
209 (if (interactive-p) | |
210 (save-excursion | |
211 ;; We are interactive. | |
212 ;; Create the *Shadows* buffer and display shadowings there. | |
213 (let ((output-buffer (get-buffer-create "*Shadows*"))) | |
214 (display-buffer output-buffer) | |
215 (set-buffer output-buffer) | |
216 (erase-buffer) | |
217 (while shadows | |
218 (insert (format "%s hides %s\n" (car shadows) | |
219 (car (cdr shadows)))) | |
220 (setq shadows (cdr (cdr shadows)))) | |
221 (insert msg "\n"))) | |
222 ;; We are non-interactive, print shadows via message. | |
223 (when shadows | |
224 (message "This site has duplicate Lisp libraries with the same name. | |
225 If a locally-installed Lisp library overrides a library in the Emacs release, | |
226 that can cause trouble, and you should probably remove the locally-installed | |
227 version unless you know what you are doing.\n") | |
228 (while shadows | |
229 (message "%s hides %s" (car shadows) (car (cdr shadows))) | |
230 (setq shadows (cdr (cdr shadows)))) | |
231 (message "%s" msg)))))) | |
232 | |
233 (provide 'shadow) | |
234 | |
235 ;;; shadow.el ends here |