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