loadhist.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. ;;; loadhist.el --- lisp functions for working with feature groups
  2. ;; Copyright (C) 1995, 1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  4. ;; Maintainer: FSF
  5. ;; Keywords: internal
  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. ;; These functions exploit the load-history system variable.
  19. ;; Entry points include `unload-feature', `symbol-file', and
  20. ;; `feature-file', documented in the Emacs Lisp manual.
  21. ;;; Code:
  22. (eval-when-compile (require 'cl))
  23. (defun feature-symbols (feature)
  24. "Return the file and list of definitions associated with FEATURE.
  25. The value is actually the element of `load-history'
  26. for the file that did (provide FEATURE)."
  27. (catch 'foundit
  28. (let ((element (cons 'provide feature)))
  29. (dolist (x load-history nil)
  30. (when (member element (cdr x))
  31. (throw 'foundit x))))))
  32. (defun feature-file (feature)
  33. "Return the file name from which a given FEATURE was loaded.
  34. Actually, return the load argument, if any; this is sometimes the name of a
  35. Lisp file without an extension. If the feature came from an `eval-buffer' on
  36. a buffer with no associated file, or an `eval-region', return nil."
  37. (if (not (featurep feature))
  38. (error "%S is not a currently loaded feature" feature)
  39. (car (feature-symbols feature))))
  40. (defun file-loadhist-lookup (file)
  41. "Return the `load-history' element for FILE.
  42. FILE can be a file name, or a library name.
  43. A library name is equivalent to the file name that `load-library' would load."
  44. ;; First look for FILE as given.
  45. (let ((symbols (assoc file load-history)))
  46. ;; Try converting a library name to an absolute file name.
  47. (and (null symbols)
  48. (let ((absname
  49. (locate-file file load-path (get-load-suffixes))))
  50. (and absname (not (equal absname file))
  51. (setq symbols (cdr (assoc absname load-history))))))
  52. symbols))
  53. (defun file-provides (file)
  54. "Return the list of features provided by FILE as it was loaded.
  55. FILE can be a file name, or a library name.
  56. A library name is equivalent to the file name that `load-library' would load."
  57. (let (provides)
  58. (dolist (x (file-loadhist-lookup file) provides)
  59. (when (eq (car-safe x) 'provide)
  60. (push (cdr x) provides)))))
  61. (defun file-requires (file)
  62. "Return the list of features required by FILE as it was loaded.
  63. FILE can be a file name, or a library name.
  64. A library name is equivalent to the file name that `load-library' would load."
  65. (let (requires)
  66. (dolist (x (file-loadhist-lookup file) requires)
  67. (when (eq (car-safe x) 'require)
  68. (push (cdr x) requires)))))
  69. (defsubst file-set-intersect (p q)
  70. "Return the set intersection of two lists."
  71. (let (ret)
  72. (dolist (x p ret)
  73. (when (memq x q) (push x ret)))))
  74. (defun file-dependents (file)
  75. "Return the list of loaded libraries that depend on FILE.
  76. This can include FILE itself.
  77. FILE can be a file name, or a library name.
  78. A library name is equivalent to the file name that `load-library' would load."
  79. (let ((provides (file-provides file))
  80. (dependents nil))
  81. (dolist (x load-history dependents)
  82. (when (file-set-intersect provides (file-requires (car x)))
  83. (push (car x) dependents)))))
  84. (defun read-feature (prompt &optional loaded-p)
  85. "Read feature name from the minibuffer, prompting with string PROMPT.
  86. If optional second arg LOADED-P is non-nil, the feature must be loaded
  87. from a file."
  88. (intern
  89. (completing-read prompt
  90. (cons nil features)
  91. (and loaded-p
  92. #'(lambda (f)
  93. (and f ; ignore nil
  94. (feature-file f))))
  95. loaded-p)))
  96. (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
  97. (defvar unload-feature-special-hooks
  98. '(after-change-functions after-insert-file-functions
  99. after-make-frame-functions auto-coding-functions
  100. auto-fill-function before-change-functions
  101. blink-paren-function buffer-access-fontify-functions
  102. choose-completion-string-functions
  103. comint-output-filter-functions command-line-functions
  104. comment-indent-function compilation-finish-functions
  105. delete-frame-functions disabled-command-function
  106. fill-nobreak-predicate find-directory-functions
  107. find-file-not-found-functions
  108. font-lock-beginning-of-syntax-function
  109. font-lock-fontify-buffer-function
  110. font-lock-fontify-region-function
  111. font-lock-mark-block-function
  112. font-lock-syntactic-face-function
  113. font-lock-unfontify-buffer-function
  114. font-lock-unfontify-region-function
  115. kill-buffer-query-functions kill-emacs-query-functions
  116. lisp-indent-function mouse-position-function
  117. redisplay-end-trigger-functions suspend-tty-functions
  118. temp-buffer-show-function window-scroll-functions
  119. window-size-change-functions write-contents-functions
  120. write-file-functions write-region-annotate-functions)
  121. "A list of special hooks from Info node `(elisp)Standard Hooks'.
  122. These are symbols with hooklike values whose names don't end in
  123. `-hook' or `-hooks', from which `unload-feature' should try to remove
  124. pertinent symbols.")
  125. (defvar unload-function-defs-list nil
  126. "List of definitions in the Lisp library being unloaded.
  127. This is meant to be used by `FEATURE-unload-function'; see the
  128. documentation of `unload-feature' for details.")
  129. (define-obsolete-variable-alias 'unload-hook-features-list
  130. 'unload-function-defs-list "22.2")
  131. (defun unload--set-major-mode ()
  132. (save-current-buffer
  133. (dolist (buffer (buffer-list))
  134. (set-buffer buffer)
  135. (let ((proposed major-mode))
  136. ;; Look for a predecessor mode not defined in the feature we're processing
  137. (while (and proposed (rassq proposed unload-function-defs-list))
  138. (setq proposed (get proposed 'derived-mode-parent)))
  139. (unless (eq proposed major-mode)
  140. ;; Two cases: either proposed is nil, and we want to switch to fundamental
  141. ;; mode, or proposed is not nil and not major-mode, and so we use it.
  142. (funcall (or proposed 'fundamental-mode)))))))
  143. ;;;###autoload
  144. (defun unload-feature (feature &optional force)
  145. "Unload the library that provided FEATURE.
  146. If the feature is required by any other loaded code, and prefix arg FORCE
  147. is nil, raise an error.
  148. Standard unloading activities include restoring old autoloads for
  149. functions defined by the library, undoing any additions that the
  150. library has made to hook variables or to `auto-mode-alist', undoing
  151. ELP profiling of functions in that library, unproviding any features
  152. provided by the library, and canceling timers held in variables
  153. defined by the library.
  154. If a function `FEATURE-unload-function' is defined, this function
  155. calls it with no arguments, before doing anything else. That function
  156. can do whatever is appropriate to undo the loading of the library. If
  157. `FEATURE-unload-function' returns non-nil, that suppresses the
  158. standard unloading of the library. Otherwise the standard unloading
  159. proceeds.
  160. `FEATURE-unload-function' has access to the package's list of
  161. definitions in the variable `unload-function-defs-list' and could
  162. remove symbols from it in the event that the package has done
  163. something strange, such as redefining an Emacs function."
  164. (interactive
  165. (list
  166. (read-feature "Unload feature: " t)
  167. current-prefix-arg))
  168. (unless (featurep feature)
  169. (error "%s is not a currently loaded feature" (symbol-name feature)))
  170. (unless force
  171. (let* ((file (feature-file feature))
  172. (dependents (delete file (copy-sequence (file-dependents file)))))
  173. (when dependents
  174. (error "Loaded libraries %s depend on %s"
  175. (prin1-to-string dependents) file))))
  176. (let* ((unload-function-defs-list (feature-symbols feature))
  177. (file (pop unload-function-defs-list))
  178. ;; If non-nil, this is a symbol for which we should
  179. ;; restore a previous autoload if possible.
  180. restore-autoload
  181. (name (symbol-name feature))
  182. (unload-hook (intern-soft (concat name "-unload-hook")))
  183. (unload-func (intern-soft (concat name "-unload-function"))))
  184. ;; If FEATURE-unload-function is defined and returns non-nil,
  185. ;; don't try to do anything more; otherwise proceed normally.
  186. (unless (and (fboundp unload-func)
  187. (funcall unload-func))
  188. ;; Try to avoid losing badly when hooks installed in critical
  189. ;; places go away. (Some packages install things on
  190. ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
  191. (if unload-hook
  192. ;; First off, provide a clean way for package FOO to arrange
  193. ;; this by adding hooks on the variable `FOO-unload-hook'.
  194. ;; This is obsolete; FEATURE-unload-function should be used now.
  195. (run-hooks unload-hook)
  196. ;; Otherwise, do our best. Look through the obarray for symbols
  197. ;; which seem to be hook variables or special hook functions and
  198. ;; remove anything from them which matches the feature-symbols
  199. ;; about to get zapped. Obviously this won't get anonymous
  200. ;; functions which the package might just have installed, and
  201. ;; there might be other important state, but this tactic
  202. ;; normally works.
  203. (mapatoms
  204. (lambda (x)
  205. (when (and (boundp x)
  206. (or (and (consp (symbol-value x)) ; Random hooks.
  207. (string-match "-hooks?\\'" (symbol-name x)))
  208. (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc.
  209. (dolist (y unload-function-defs-list)
  210. (when (and (eq (car-safe y) 'defun)
  211. (not (get (cdr y) 'autoload)))
  212. (remove-hook x (cdr y)))))))
  213. ;; Remove any feature-symbols from auto-mode-alist as well.
  214. (dolist (y unload-function-defs-list)
  215. (when (and (eq (car-safe y) 'defun)
  216. (not (get (cdr y) 'autoload)))
  217. (setq auto-mode-alist
  218. (rassq-delete-all (cdr y) auto-mode-alist)))))
  219. ;; Change major mode in all buffers using one defined in the feature being unloaded.
  220. (unload--set-major-mode)
  221. (when (fboundp 'elp-restore-function) ; remove ELP stuff first
  222. (dolist (elt unload-function-defs-list)
  223. (when (symbolp elt)
  224. (elp-restore-function elt))))
  225. (dolist (x unload-function-defs-list)
  226. (if (consp x)
  227. (case (car x)
  228. ;; Remove any feature names that this file provided.
  229. (provide
  230. (setq features (delq (cdr x) features)))
  231. ((defun autoload)
  232. (let ((fun (cdr x)))
  233. (when (fboundp fun)
  234. (when (fboundp 'ad-unadvise)
  235. (ad-unadvise fun))
  236. (let ((aload (get fun 'autoload)))
  237. (if (and aload (eq fun restore-autoload))
  238. (fset fun (cons 'autoload aload))
  239. (fmakunbound fun))))))
  240. ;; (t . SYMBOL) comes before (defun . SYMBOL)
  241. ;; and says we should restore SYMBOL's autoload
  242. ;; when we undefine it.
  243. ((t) (setq restore-autoload (cdr x)))
  244. ((require defface) nil)
  245. (t (message "Unexpected element %s in load-history" x)))
  246. ;; Kill local values as much as possible.
  247. (dolist (buf (buffer-list))
  248. (with-current-buffer buf
  249. (if (and (boundp x) (timerp (symbol-value x)))
  250. (cancel-timer (symbol-value x)))
  251. (kill-local-variable x)))
  252. (if (and (boundp x) (timerp (symbol-value x)))
  253. (cancel-timer (symbol-value x)))
  254. ;; Get rid of the default binding if we can.
  255. (unless (local-variable-if-set-p x)
  256. (makunbound x))))
  257. ;; Delete the load-history element for this file.
  258. (setq load-history (delq (assoc file load-history) load-history))))
  259. ;; Don't return load-history, it is not useful.
  260. nil)
  261. (provide 'loadhist)
  262. ;;; loadhist.el ends here