loadhist.el 13 KB

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