Commit | Line | Data |
---|---|---|
50584ac0 KH |
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 | |
b578f267 EN |
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. | |
50584ac0 KH |
25 | |
26 | ;;; Commentary: | |
b578f267 | 27 | |
50584ac0 KH |
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 | \f | |
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 | (if (member file files-seen-this-dir) | |
111 | nil | |
112 | ||
113 | ;; File has not been seen yet in this directory. | |
114 | ;; This test prevents us declaring that XXX.el shadows | |
115 | ;; XXX.elc (or vice-versa) when they are in the same directory. | |
116 | (setq files-seen-this-dir (cons file files-seen-this-dir)) | |
117 | ||
118 | (if (setq orig-dir (assoc file files)) | |
119 | ;; This file was seen before, we have a shadowing. | |
120 | (setq shadows | |
121 | (append shadows | |
122 | (list (concat (cdr orig-dir) "/" file) | |
123 | (concat dir "/" file)))) | |
124 | ||
125 | ;; Not seen before, add it to the list of seen files. | |
126 | (setq files (cons (cons file dir) files)))) | |
127 | ||
128 | (setq curr-files (cdr curr-files)))) | |
129 | (setq path (cdr path))) | |
130 | ||
131 | ;; Return the list of shadowings. | |
132 | shadows)) | |
133 | ||
134 | \f | |
135 | ;;;###autoload | |
136 | (defun list-load-path-shadows () | |
137 | ||
138 | "Display a list of Emacs Lisp files that create shadows. | |
139 | ||
140 | This function lists potential load-path problems. Directories in the | |
141 | `load-path' variable are searched, in order, for Emacs Lisp | |
142 | files. When a previously encountered file name is re-located, a | |
143 | message is displayed indicating that the later file is \"shadowed\" by | |
144 | the earlier. | |
145 | ||
146 | For example, suppose `load-path' is set to | |
147 | ||
148 | \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\) | |
149 | ||
150 | and that each of these directories contains a file called XXX.el. Then | |
151 | XXX.el in the site-lisp directory is referred to by all of: | |
152 | \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc. | |
153 | ||
154 | The first XXX.el file prevents emacs from seeing the second \(unless | |
155 | the second is loaded explicitly via load-file\). | |
156 | ||
157 | When not intended, such shadowings can be the source of subtle | |
158 | problems. For example, the above situation may have arisen because the | |
159 | XXX package was not distributed with versions of emacs prior to | |
160 | 19.30. An emacs maintainer downloaded XXX from elsewhere and installed | |
161 | it. Later, XXX was updated and included in the emacs distribution. | |
162 | Unless the emacs maintainer checks for this, the new version of XXX | |
163 | will be hidden behind the old \(which may no longer work with the new | |
164 | emacs version\). | |
165 | ||
166 | This function performs these checks and flags all possible | |
167 | shadowings. Because a .el file may exist without a corresponding .elc | |
168 | \(or vice-versa\), these suffixes are essentially ignored. A file | |
169 | XXX.elc in an early directory \(that does not contain XXX.el\) is | |
170 | considered to shadow a later file XXX.el, and vice-versa. | |
171 | ||
172 | When run interactively, the shadowings \(if any\) are displayed in a | |
173 | buffer called `*Shadows*'. Shadowings are located by calling the | |
174 | \(non-interactive\) companion function, `find-emacs-lisp-shadows'." | |
175 | ||
176 | (interactive) | |
177 | (let* ((shadows (find-emacs-lisp-shadows)) | |
178 | (n (/ (length shadows) 2)) | |
179 | (msg (format "%s Emacs Lisp load-path shadowing%s found." | |
180 | (if (zerop n) "No" (concat "\n" (number-to-string n))) | |
181 | (if (= n 1) " was" "s were")))) | |
182 | (if (interactive-p) | |
183 | (save-excursion | |
184 | ;; We are interactive. | |
185 | ;; Create the *Shadows* buffer and display shadowings there. | |
186 | (let ((output-buffer (get-buffer-create "*Shadows*"))) | |
187 | (display-buffer output-buffer) | |
188 | (set-buffer output-buffer) | |
189 | (erase-buffer) | |
190 | (while shadows | |
191 | (insert (format "%s shadows %s\n" (car shadows) (car (cdr shadows)))) | |
192 | (setq shadows (cdr (cdr shadows)))) | |
193 | (insert msg "\n"))) | |
194 | ;; We are non-interactive, print shadows via message. | |
195 | (while shadows | |
196 | (message (format "%s shadows %s" (car shadows) (car (cdr shadows)))) | |
197 | (setq shadows (cdr (cdr shadows)))) | |
198 | (message msg)))) | |
199 | ||
200 | (provide 'shadow) | |
201 | ||
202 | ;;; shadow.el ends here |