Mercurial > emacs
comparison lisp/emacs-lisp/shadow.el @ 14058:be8c739308f7
Initial revision
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Mon, 08 Jan 1996 22:52:14 +0000 |
parents | |
children | 83f275dcd93a |
comparison
equal
deleted
inserted
replaced
14057:61ef36a83a98 | 14058:be8c739308f7 |
---|---|
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 | |
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | |
25 ;;; Commentary: | |
26 ;; | |
27 ;; The functions in this file detect (`find-emacs-lisp-shadows') | |
28 ;; and display (`list-load-path-shadows') potential load-path | |
29 ;; problems that arise when Emacs Lisp files "shadow" each other. | |
30 ;; | |
31 ;; For example, a file XXX.el early in one's load-path will shadow | |
32 ;; a file with the same name in a later load-path directory. When | |
33 ;; this is unintentional, it may result in problems that could have | |
34 ;; been easily avoided. This occurs often (to me) when installing a | |
35 ;; new version of emacs and something in the site-lisp directory | |
36 ;; has been updated and added to the emacs distribution. The old | |
37 ;; version, now outdated, shadows the new one. This is obviously | |
38 ;; undesirable. | |
39 ;; | |
40 ;; The `list-load-path-shadows' function was run when you installed | |
41 ;; this version of emacs. To run it by hand in emacs: | |
42 ;; | |
43 ;; M-x load-library RET shadow RET | |
44 ;; M-x list-load-path-shadows | |
45 ;; | |
46 ;; or run it non-interactively via: | |
47 ;; | |
48 ;; emacs -batch -l shadow.el -f list-load-path-shadows | |
49 ;; | |
50 ;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions, | |
51 ;; rewritings & speedups. | |
52 | |
53 ;;; Code: | |
54 | |
55 (defun find-emacs-lisp-shadows (&optional path) | |
56 "Return a list of Emacs Lisp files that create shadows. | |
57 This function does the work for `list-load-path-shadows'. | |
58 | |
59 We traverse PATH looking for shadows, and return a \(possibly empty\) | |
60 even-length list of files. A file in this list at position 2i shadows | |
61 the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\) | |
62 are stripped from the file names in the list. | |
63 | |
64 See the documentation for `list-load-path-shadows' for further information." | |
65 | |
66 (or path (setq path load-path)) | |
67 | |
68 (let (true-names ; List of dirs considered. | |
69 shadows ; List of shadowings, to be returned. | |
70 files ; File names ever seen, with dirs. | |
71 dir ; The dir being currently scanned. | |
72 curr-files ; This dir's Emacs Lisp files. | |
73 orig-dir ; Where the file was first seen. | |
74 files-seen-this-dir ; Files seen so far in this dir. | |
75 file) ; The current file. | |
76 | |
77 | |
78 (while path | |
79 | |
80 (setq dir (file-truename (or (car path) "."))) | |
81 (if (member dir true-names) | |
82 ;; We have already considered this PATH redundant directory. | |
83 ;; Show the redundancy if we are interactiver, unless the PATH | |
84 ;; dir is nil or "." (these redundant directories are just a | |
85 ;; result of the current working directory, and are therefore | |
86 ;; not always redundant). | |
87 (or noninteractive | |
88 (and (car path) | |
89 (not (string= (car path) ".")) | |
90 (message "Ignoring redundant directory '%s'." (car path)))) | |
91 | |
92 (setq true-names (append true-names (list dir))) | |
93 (setq dir (or (car path) ".")) | |
94 (setq curr-files (if (file-accessible-directory-p dir) | |
95 (directory-files dir nil ".\\.elc?$" t))) | |
96 (and curr-files | |
97 (not noninteractive) | |
98 (message "Checking %d files in '%s' ..." (length curr-files) dir)) | |
99 | |
100 (setq files-seen-this-dir nil) | |
101 | |
102 (while curr-files | |
103 | |
104 (setq file (car curr-files)) | |
105 (setq file (substring | |
106 file 0 (if (string= (substring file -1) "c") -4 -3))) | |
107 | |
108 ;; 'file' now contains the current file name, with no suffix. | |
109 (if (member file files-seen-this-dir) | |
110 nil | |
111 | |
112 ;; File has not been seen yet in this directory. | |
113 ;; This test prevents us declaring that XXX.el shadows | |
114 ;; XXX.elc (or vice-versa) when they are in the same directory. | |
115 (setq files-seen-this-dir (cons file files-seen-this-dir)) | |
116 | |
117 (if (setq orig-dir (assoc file files)) | |
118 ;; This file was seen before, we have a shadowing. | |
119 (setq shadows | |
120 (append shadows | |
121 (list (concat (cdr orig-dir) "/" file) | |
122 (concat dir "/" file)))) | |
123 | |
124 ;; Not seen before, add it to the list of seen files. | |
125 (setq files (cons (cons file dir) files)))) | |
126 | |
127 (setq curr-files (cdr curr-files)))) | |
128 (setq path (cdr path))) | |
129 | |
130 ;; Return the list of shadowings. | |
131 shadows)) | |
132 | |
133 | |
134 ;;;###autoload | |
135 (defun list-load-path-shadows () | |
136 | |
137 "Display a list of Emacs Lisp files that create shadows. | |
138 | |
139 This function lists potential load-path problems. Directories in the | |
140 `load-path' variable are searched, in order, for Emacs Lisp | |
141 files. When a previously encountered file name is re-located, a | |
142 message is displayed indicating that the later file is \"shadowed\" by | |
143 the earlier. | |
144 | |
145 For example, suppose `load-path' is set to | |
146 | |
147 \(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\) | |
148 | |
149 and that each of these directories contains a file called XXX.el. Then | |
150 XXX.el in the site-lisp directory is referred to by all of: | |
151 \(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc. | |
152 | |
153 The first XXX.el file prevents emacs from seeing the second \(unless | |
154 the second is loaded explicitly via load-file\). | |
155 | |
156 When not intended, such shadowings can be the source of subtle | |
157 problems. For example, the above situation may have arisen because the | |
158 XXX package was not distributed with versions of emacs prior to | |
159 19.30. An emacs maintainer downloaded XXX from elsewhere and installed | |
160 it. Later, XXX was updated and included in the emacs distribution. | |
161 Unless the emacs maintainer checks for this, the new version of XXX | |
162 will be hidden behind the old \(which may no longer work with the new | |
163 emacs version\). | |
164 | |
165 This function performs these checks and flags all possible | |
166 shadowings. Because a .el file may exist without a corresponding .elc | |
167 \(or vice-versa\), these suffixes are essentially ignored. A file | |
168 XXX.elc in an early directory \(that does not contain XXX.el\) is | |
169 considered to shadow a later file XXX.el, and vice-versa. | |
170 | |
171 When run interactively, the shadowings \(if any\) are displayed in a | |
172 buffer called `*Shadows*'. Shadowings are located by calling the | |
173 \(non-interactive\) companion function, `find-emacs-lisp-shadows'." | |
174 | |
175 (interactive) | |
176 (let* ((shadows (find-emacs-lisp-shadows)) | |
177 (n (/ (length shadows) 2)) | |
178 (msg (format "%s Emacs Lisp load-path shadowing%s found." | |
179 (if (zerop n) "No" (concat "\n" (number-to-string n))) | |
180 (if (= n 1) " was" "s were")))) | |
181 (if (interactive-p) | |
182 (save-excursion | |
183 ;; We are interactive. | |
184 ;; Create the *Shadows* buffer and display shadowings there. | |
185 (let ((output-buffer (get-buffer-create "*Shadows*"))) | |
186 (display-buffer output-buffer) | |
187 (set-buffer output-buffer) | |
188 (erase-buffer) | |
189 (while shadows | |
190 (insert (format "%s shadows %s\n" (car shadows) (car (cdr shadows)))) | |
191 (setq shadows (cdr (cdr shadows)))) | |
192 (insert msg "\n"))) | |
193 ;; We are non-interactive, print shadows via message. | |
194 (while shadows | |
195 (message (format "%s shadows %s" (car shadows) (car (cdr shadows)))) | |
196 (setq shadows (cdr (cdr shadows)))) | |
197 (message msg)))) | |
198 | |
199 (provide 'shadow) | |
200 | |
201 ;;; shadow.el ends here |