Document reserved keys
[emacs.git] / lisp / emacs-lisp / shadow.el
blob1788f0d71f7c969302aac7c77edd92e37a6fe288
1 ;;; shadow.el --- locate Emacs Lisp file shadowings
3 ;; Copyright (C) 1995, 2001-2018 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 3 of the License, or
14 ;; (at your option) 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. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; The functions in this file detect (`load-path-shadows-find')
27 ;; and display (`list-load-path-shadows') potential load-path
28 ;; problems that arise when Emacs Lisp files "shadow" each other.
30 ;; For example, a file XXX.el early in one's load-path will shadow
31 ;; a file with the same name in a later load-path directory. When
32 ;; this is unintentional, it may result in problems that could have
33 ;; been easily avoided. This occurs often (to me) when installing a
34 ;; new version of emacs and something in the site-lisp directory
35 ;; has been updated and added to the emacs distribution. The old
36 ;; version, now outdated, shadows the new one. This is obviously
37 ;; undesirable.
39 ;; The `list-load-path-shadows' function was run when you installed
40 ;; this version of emacs. To run it by hand in emacs:
42 ;; M-x list-load-path-shadows
44 ;; or run it non-interactively via:
46 ;; emacs -batch -f list-load-path-shadows
48 ;; Thanks to Francesco Potortì <pot@cnuce.cnr.it> for suggestions,
49 ;; rewritings & speedups.
51 ;;; Code:
53 (defgroup lisp-shadow nil
54 "Locate Emacs Lisp file shadowings."
55 :prefix "load-path-shadows-"
56 :group 'lisp)
58 (define-obsolete-variable-alias 'shadows-compare-text-p
59 'load-path-shadows-compare-text "23.3")
61 (defcustom load-path-shadows-compare-text 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 load-path-shadows-find (&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."
77 (let (true-names ; List of dirs considered.
78 shadows ; List of shadowings, to be returned.
79 files ; File names ever seen, with dirs.
80 dir ; The dir being currently scanned.
81 dir-case-insensitive ; `file-name-case-insentive-p' for dir.
82 curr-files ; This dir's Emacs Lisp files.
83 orig-dir ; Where the file was first seen.
84 files-seen-this-dir ; Files seen so far in this dir.
85 file) ; The current file.
86 (dolist (pp (or path load-path))
87 (setq dir (directory-file-name (file-truename (or pp "."))))
88 (if (member dir true-names)
89 ;; We have already considered this PATH redundant directory.
90 ;; Show the redundancy if we are interactive, unless the PATH
91 ;; dir is nil or "." (these redundant directories are just a
92 ;; result of the current working directory, and are therefore
93 ;; not always redundant).
94 (or noninteractive
95 (and pp
96 (not (string= pp "."))
97 (message "Ignoring redundant directory %s" pp)))
99 (setq true-names (append true-names (list dir)))
100 (setq dir (directory-file-name (or pp ".")))
101 (setq curr-files (if (file-accessible-directory-p dir)
102 (directory-files dir nil ".\\.elc?\\(\\.gz\\)?$" t)))
103 (and curr-files
104 (not noninteractive)
105 (message "Checking %d files in %s..." (length curr-files) dir))
107 (setq files-seen-this-dir nil)
108 ;; We assume that case sensitivity of a directory applies to
109 ;; its files.
110 (setq dir-case-insensitive (file-name-case-insensitive-p dir))
112 (dolist (file curr-files)
114 (if (string-match "\\.gz$" file)
115 (setq file (substring file 0 -3)))
116 (setq file (substring
117 file 0 (if (string= (substring file -1) "c") -4 -3)))
119 ;; FILE now contains the current file name, with no suffix.
120 (unless (or (member file files-seen-this-dir)
121 ;; Ignore these files.
122 (member file (list "subdirs" "leim-list"
123 (file-name-sans-extension
124 dir-locals-file))))
125 ;; File has not been seen yet in this directory.
126 ;; This test prevents us declaring that XXX.el shadows
127 ;; XXX.elc (or vice-versa) when they are in the same directory.
128 (setq files-seen-this-dir (cons file files-seen-this-dir))
130 (if (setq orig-dir (assoc file files
131 (when dir-case-insensitive
132 (lambda (f1 f2) (eq (compare-strings f1 nil nil f2 nil nil t) t)))))
133 ;; This file was seen before, we have a shadowing.
134 ;; Report it unless the files are identical.
135 (let ((base1 (concat (cdr orig-dir) "/" (car orig-dir)))
136 (base2 (concat dir "/" file)))
137 (if (not (and load-path-shadows-compare-text
138 (load-path-shadows-same-file-or-nonexistent
139 (concat base1 ".el") (concat base2 ".el"))
140 ;; This is a bit strict, but safe.
141 (load-path-shadows-same-file-or-nonexistent
142 (concat base1 ".elc") (concat base2 ".elc"))))
143 (setq shadows
144 (append shadows (list base1 base2)))))
146 ;; Not seen before, add it to the list of seen files.
147 (setq files (cons (cons file dir) files)))))))
148 ;; Return the list of shadowings.
149 shadows))
151 (define-obsolete-function-alias 'find-emacs-lisp-shadows
152 'load-path-shadows-find "23.3")
154 ;; Return true if neither file exists, or if both exist and have identical
155 ;; contents.
156 (defun load-path-shadows-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 (defvar load-path-shadows-font-lock-keywords
169 ;; The idea is that shadows of files supplied with Emacs are more
170 ;; serious than various versions of external packages shadowing each
171 ;; other.
172 `((,(format "hides \\(%s.*\\)"
173 (file-name-directory
174 (or (locate-library "simple")
175 (file-name-as-directory
176 (expand-file-name "../lisp" data-directory)))))
177 . (1 font-lock-warning-face)))
178 "Keywords to highlight in `load-path-shadows-mode'.")
180 (define-derived-mode load-path-shadows-mode fundamental-mode "LP-Shadows"
181 "Major mode for load-path shadows buffer."
182 (set (make-local-variable 'font-lock-defaults)
183 '((load-path-shadows-font-lock-keywords)))
184 (setq buffer-undo-list t
185 buffer-read-only t))
187 ;; TODO use text-properties instead, a la dired.
188 (require 'button)
189 (define-button-type 'load-path-shadows-find-file
190 'follow-link t
191 ;; 'face 'default
192 'action (lambda (button)
193 (let ((file (concat (button-get button 'shadow-file) ".el")))
194 (or (file-exists-p file)
195 (setq file (concat file ".gz")))
196 (if (file-readable-p file)
197 (pop-to-buffer (find-file-noselect file))
198 (error "Cannot read file"))))
199 'help-echo "mouse-2, RET: find this file")
202 ;;;###autoload
203 (defun list-load-path-shadows (&optional stringp)
204 "Display a list of Emacs Lisp files that shadow other files.
206 If STRINGP is non-nil, returns any shadows as a string.
207 Otherwise, if interactive shows any shadows in a `*Shadows*' buffer;
208 else prints messages listing any shadows.
210 This function lists potential load path problems. Directories in
211 the `load-path' variable are searched, in order, for Emacs Lisp
212 files. When a previously encountered file name is found again, a
213 message is displayed indicating that the later file is \"hidden\" by
214 the earlier.
216 For example, suppose `load-path' is set to
218 \(\"/usr/share/emacs/site-lisp\" \"/usr/share/emacs/24.3/lisp\")
220 and that each of these directories contains a file called XXX.el. Then
221 XXX.el in the site-lisp directory is referred to by all of:
222 \(require \\='XXX), (autoload .... \"XXX\"), (load-library \"XXX\") etc.
224 The first XXX.el file prevents Emacs from seeing the second (unless
225 the second is loaded explicitly via `load-file').
227 When not intended, such shadowings can be the source of subtle
228 problems. For example, the above situation may have arisen because the
229 XXX package was not distributed with versions of Emacs prior to
230 24.3. A system administrator downloaded XXX from elsewhere and installed
231 it. Later, XXX was updated and included in the Emacs distribution.
232 Unless the system administrator checks for this, the new version of XXX
233 will be hidden behind the old (which may no longer work with the new
234 Emacs version).
236 This function performs these checks and flags all possible
237 shadowings. Because a .el file may exist without a corresponding .elc
238 \(or vice-versa), these suffixes are essentially ignored. A file
239 XXX.elc in an early directory (that does not contain XXX.el) is
240 considered to shadow a later file XXX.el, and vice-versa.
242 Shadowings are located by calling the (non-interactive) companion
243 function, `load-path-shadows-find'."
244 (interactive)
245 (let* ((shadows (load-path-shadows-find load-path))
246 (n (/ (length shadows) 2))
247 (msg (format "%s Emacs Lisp load-path shadowing%s found"
248 (if (zerop n) "No" (concat "\n" (number-to-string n)))
249 (if (= n 1) " was" "s were"))))
250 (with-temp-buffer
251 (while shadows
252 (insert (format "%s hides %s\n" (car shadows)
253 (car (cdr shadows))))
254 (setq shadows (cdr (cdr shadows))))
255 (if stringp
256 (buffer-string)
257 (if (called-interactively-p 'interactive)
258 ;; We are interactive.
259 ;; Create the *Shadows* buffer and display shadowings there.
260 (let ((string (buffer-string)))
261 (with-current-buffer (get-buffer-create "*Shadows*")
262 (display-buffer (current-buffer))
263 (load-path-shadows-mode) ; run after-change-major-mode-hook
264 (let ((inhibit-read-only t))
265 (erase-buffer)
266 (insert string)
267 (insert msg "\n")
268 (while (re-search-backward "\\(^.*\\) hides \\(.*$\\)"
269 nil t)
270 (dotimes (i 2)
271 (make-button (match-beginning (1+ i))
272 (match-end (1+ i))
273 'type 'load-path-shadows-find-file
274 'shadow-file
275 (match-string (1+ i)))))
276 (goto-char (point-max)))))
277 ;; We are non-interactive, print shadows via message.
278 (unless (zerop n)
279 (message "This site has duplicate Lisp libraries with the same name.
280 If a locally-installed Lisp library overrides a library in the Emacs release,
281 that can cause trouble, and you should probably remove the locally-installed
282 version unless you know what you are doing.\n")
283 (goto-char (point-min))
284 ;; Mimic the previous behavior of using lots of messages.
285 ;; I think one single message would look better...
286 (while (not (eobp))
287 (message "%s" (buffer-substring (line-beginning-position)
288 (line-end-position)))
289 (forward-line 1))
290 (message "%s" msg)))))))
292 (provide 'shadow)
294 ;;; shadow.el ends here