1;;; shadow.el --- locate Emacs Lisp file shadowings 2 3;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 4;; 2006, 2007 Free Software Foundation, Inc. 5 6;; Author: Terry Jones <terry@santafe.edu> 7;; Keywords: lisp 8;; Created: 15 December 1995 9 10;; This file is part of GNU Emacs. 11 12;; GNU Emacs is free software; you can redistribute it and/or modify 13;; it under the terms of the GNU General Public License as published by 14;; the Free Software Foundation; either version 2, or (at your option) 15;; any later version. 16 17;; GNU Emacs is distributed in the hope that it will be useful, 18;; but WITHOUT ANY WARRANTY; without even the implied warranty of 19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20;; GNU General Public License for more details. 21 22;; You should have received a copy of the GNU General Public License 23;; along with GNU Emacs; see the file COPYING. If not, write to the 24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 25;; Boston, MA 02110-1301, USA. 26 27;;; Commentary: 28 29;; The functions in this file detect (`find-emacs-lisp-shadows') 30;; and display (`list-load-path-shadows') potential load-path 31;; problems that arise when Emacs Lisp files "shadow" each other. 32;; 33;; For example, a file XXX.el early in one's load-path will shadow 34;; a file with the same name in a later load-path directory. When 35;; this is unintentional, it may result in problems that could have 36;; been easily avoided. This occurs often (to me) when installing a 37;; new version of emacs and something in the site-lisp directory 38;; has been updated and added to the emacs distribution. The old 39;; version, now outdated, shadows the new one. This is obviously 40;; undesirable. 41;; 42;; The `list-load-path-shadows' function was run when you installed 43;; this version of emacs. To run it by hand in emacs: 44;; 45;; M-x load-library RET shadow RET 46;; M-x list-load-path-shadows 47;; 48;; or run it non-interactively via: 49;; 50;; emacs -batch -l shadow.el -f list-load-path-shadows 51;; 52;; Thanks to Francesco Potorti` <pot@cnuce.cnr.it> for suggestions, 53;; rewritings & speedups. 54 55;;; Code: 56 57(defgroup lisp-shadow nil 58 "Locate Emacs Lisp file shadowings." 59 :prefix "shadows-" 60 :group 'lisp) 61 62(defcustom shadows-compare-text-p nil 63 "*If non-nil, then shadowing files are reported only if their text differs. 64This is slower, but filters out some innocuous shadowing." 65 :type 'boolean 66 :group 'lisp-shadow) 67 68(defun find-emacs-lisp-shadows (&optional path) 69 "Return a list of Emacs Lisp files that create shadows. 70This function does the work for `list-load-path-shadows'. 71 72We traverse PATH looking for shadows, and return a \(possibly empty\) 73even-length list of files. A file in this list at position 2i shadows 74the file in position 2i+1. Emacs Lisp file suffixes \(.el and .elc\) 75are stripped from the file names in the list. 76 77See the documentation for `list-load-path-shadows' for further information." 78 79 (or path (setq path load-path)) 80 81 (let (true-names ; List of dirs considered. 82 shadows ; List of shadowings, to be returned. 83 files ; File names ever seen, with dirs. 84 dir ; The dir being currently scanned. 85 curr-files ; This dir's Emacs Lisp files. 86 orig-dir ; Where the file was first seen. 87 files-seen-this-dir ; Files seen so far in this dir. 88 file) ; The current file. 89 90 91 (while path 92 93 (setq dir (directory-file-name (file-truename (or (car path) ".")))) 94 (if (member dir true-names) 95 ;; We have already considered this PATH redundant directory. 96 ;; Show the redundancy if we are interactive, unless the PATH 97 ;; dir is nil or "." (these redundant directories are just a 98 ;; result of the current working directory, and are therefore 99 ;; not always redundant). 100 (or noninteractive 101 (and (car path) 102 (not (string= (car path) ".")) 103 (message "Ignoring redundant directory %s" (car path)))) 104 105 (setq true-names (append true-names (list dir))) 106 (setq dir (directory-file-name (or (car path) "."))) 107 (setq curr-files (if (file-accessible-directory-p dir) 108 (directory-files dir nil ".\\.elc?\\(\\.gz\\)?$" t))) 109 (and curr-files 110 (not noninteractive) 111 (message "Checking %d files in %s..." (length curr-files) dir)) 112 113 (setq files-seen-this-dir nil) 114 115 (while curr-files 116 117 (setq file (car curr-files)) 118 (if (string-match "\\.gz$" file) 119 (setq file (substring file 0 -3))) 120 (setq file (substring 121 file 0 (if (string= (substring file -1) "c") -4 -3))) 122 123 ;; FILE now contains the current file name, with no suffix. 124 (unless (or (member file files-seen-this-dir) 125 ;; Ignore these files. 126 (member file '("subdirs"))) 127 ;; File has not been seen yet in this directory. 128 ;; This test prevents us declaring that XXX.el shadows 129 ;; XXX.elc (or vice-versa) when they are in the same directory. 130 (setq files-seen-this-dir (cons file files-seen-this-dir)) 131 132 (if (setq orig-dir (assoc file files)) 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) "/" file)) 136 (base2 (concat dir "/" file))) 137 (if (not (and shadows-compare-text-p 138 (shadow-same-file-or-nonexistent 139 (concat base1 ".el") (concat base2 ".el")) 140 ;; This is a bit strict, but safe. 141 (shadow-same-file-or-nonexistent 142 (concat base1 ".elc") (concat base2 ".elc")))) 143 (setq shadows 144 (append shadows (list base1 base2))))) 145 146 ;; Not seen before, add it to the list of seen files. 147 (setq files (cons (cons file dir) files)))) 148 149 (setq curr-files (cdr curr-files)))) 150 (setq path (cdr path))) 151 152 ;; Return the list of shadowings. 153 shadows)) 154 155;; Return true if neither file exists, or if both exist and have identical 156;; contents. 157(defun shadow-same-file-or-nonexistent (f1 f2) 158 (let ((exists1 (file-exists-p f1)) 159 (exists2 (file-exists-p f2))) 160 (or (and (not exists1) (not exists2)) 161 (and exists1 exists2 162 (or (equal (file-truename f1) (file-truename f2)) 163 ;; As a quick test, avoiding spawning a process, compare file 164 ;; sizes. 165 (and (= (nth 7 (file-attributes f1)) 166 (nth 7 (file-attributes f2))) 167 (eq 0 (call-process "cmp" nil nil nil "-s" f1 f2)))))))) 168 169;;;###autoload 170(defun list-load-path-shadows () 171 "Display a list of Emacs Lisp files that shadow other files. 172 173This function lists potential load path problems. Directories in 174the `load-path' variable are searched, in order, for Emacs Lisp 175files. When a previously encountered file name is found again, a 176message is displayed indicating that the later file is \"hidden\" by 177the earlier. 178 179For example, suppose `load-path' is set to 180 181\(\"/usr/gnu/emacs/site-lisp\" \"/usr/gnu/emacs/share/emacs/19.30/lisp\"\) 182 183and that each of these directories contains a file called XXX.el. Then 184XXX.el in the site-lisp directory is referred to by all of: 185\(require 'XXX\), \(autoload .... \"XXX\"\), \(load-library \"XXX\"\) etc. 186 187The first XXX.el file prevents Emacs from seeing the second \(unless 188the second is loaded explicitly via `load-file'\). 189 190When not intended, such shadowings can be the source of subtle 191problems. For example, the above situation may have arisen because the 192XXX package was not distributed with versions of Emacs prior to 19319.30. An Emacs maintainer downloaded XXX from elsewhere and installed 194it. Later, XXX was updated and included in the Emacs distribution. 195Unless the Emacs maintainer checks for this, the new version of XXX 196will be hidden behind the old \(which may no longer work with the new 197Emacs version\). 198 199This function performs these checks and flags all possible 200shadowings. Because a .el file may exist without a corresponding .elc 201\(or vice-versa\), these suffixes are essentially ignored. A file 202XXX.elc in an early directory \(that does not contain XXX.el\) is 203considered to shadow a later file XXX.el, and vice-versa. 204 205When run interactively, the shadowings \(if any\) are displayed in a 206buffer called `*Shadows*'. Shadowings are located by calling the 207\(non-interactive\) companion function, `find-emacs-lisp-shadows'." 208 209 (interactive) 210 (let* ((path (copy-sequence load-path)) 211 (tem path) 212 toplevs) 213 ;; If we can find simple.el in two places, 214 (while tem 215 (if (or (file-exists-p (expand-file-name "simple.el" (car tem))) 216 (file-exists-p (expand-file-name "simple.el.gz" (car tem)))) 217 (setq toplevs (cons (car tem) toplevs))) 218 (setq tem (cdr tem))) 219 (if (> (length toplevs) 1) 220 ;; Cut off our copy of load-path right before 221 ;; the last directory which has simple.el in it. 222 ;; This avoids loads of duplications between the source dir 223 ;; and the dir where these files were copied by installation. 224 (let ((break (car toplevs))) 225 (setq tem path) 226 (while tem 227 (if (eq (nth 1 tem) break) 228 (progn 229 (setcdr tem nil) 230 (setq tem nil))) 231 (setq tem (cdr tem))))) 232 233 (let* ((shadows (find-emacs-lisp-shadows path)) 234 (n (/ (length shadows) 2)) 235 (msg (format "%s Emacs Lisp load-path shadowing%s found" 236 (if (zerop n) "No" (concat "\n" (number-to-string n))) 237 (if (= n 1) " was" "s were")))) 238 (if (interactive-p) 239 (save-excursion 240 ;; We are interactive. 241 ;; Create the *Shadows* buffer and display shadowings there. 242 (let ((output-buffer (get-buffer-create "*Shadows*"))) 243 (display-buffer output-buffer) 244 (set-buffer output-buffer) 245 (erase-buffer) 246 (while shadows 247 (insert (format "%s hides %s\n" (car shadows) 248 (car (cdr shadows)))) 249 (setq shadows (cdr (cdr shadows)))) 250 (insert msg "\n"))) 251 ;; We are non-interactive, print shadows via message. 252 (when shadows 253 (message "This site has duplicate Lisp libraries with the same name. 254If a locally-installed Lisp library overrides a library in the Emacs release, 255that can cause trouble, and you should probably remove the locally-installed 256version unless you know what you are doing.\n") 257 (while shadows 258 (message "%s hides %s" (car shadows) (car (cdr shadows))) 259 (setq shadows (cdr (cdr shadows)))) 260 (message "%s" msg)))))) 261 262(provide 'shadow) 263 264;;; arch-tag: 0480e8a7-62ed-4a12-a9f6-f44ded9b0830 265;;; shadow.el ends here 266