shadow.el 11 KB

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