Commit | Line | Data |
---|---|---|
2b86d62c | 1 | ;;; loadhist.el --- lisp functions for working with feature groups |
b578f267 | 2 | |
c90f2757 | 3 | ;; Copyright (C) 1995, 1998, 2000, 2001, 2002, 2003, 2004, |
d7a0267c | 4 | ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
2b86d62c RS |
5 | |
6 | ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> | |
53c1fa0f | 7 | ;; Maintainer: FSF |
2b86d62c RS |
8 | ;; Keywords: internal |
9 | ||
131a0c01 KH |
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 | |
b4aa6026 | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
131a0c01 KH |
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 | |
b578f267 | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
131a0c01 | 26 | |
2b86d62c RS |
27 | ;;; Commentary: |
28 | ||
29 | ;; These functions exploit the load-history system variable. | |
fde4581d RS |
30 | ;; Entry points include `unload-feature', `symbol-file', and |
31 | ;; `feature-file', documented in the Emacs Lisp manual. | |
2b86d62c RS |
32 | |
33 | ;;; Code: | |
34 | ||
ab215e72 SM |
35 | (eval-when-compile (require 'cl)) |
36 | ||
2b86d62c | 37 | (defun feature-symbols (feature) |
7dc2cc98 RS |
38 | "Return the file and list of definitions associated with FEATURE. |
39 | The value is actually the element of `load-history' | |
40 | for the file that did (provide FEATURE)." | |
2b86d62c | 41 | (catch 'foundit |
53c1fa0f DL |
42 | (mapc (lambda (x) |
43 | (if (member (cons 'provide feature) (cdr x)) | |
44 | (throw 'foundit x))) | |
45 | load-history) | |
2b86d62c RS |
46 | nil)) |
47 | ||
48 | (defun feature-file (feature) | |
49 | "Return the file name from which a given FEATURE was loaded. | |
50 | Actually, return the load argument, if any; this is sometimes the name of a | |
53c1fa0f DL |
51 | Lisp file without an extension. If the feature came from an `eval-buffer' on |
52 | a buffer with no associated file, or an `eval-region', return nil." | |
2b86d62c | 53 | (if (not (featurep feature)) |
53c1fa0f | 54 | (error "%S is not a currently loaded feature" feature) |
2b86d62c RS |
55 | (car (feature-symbols feature)))) |
56 | ||
27af61fe | 57 | (defun file-loadhist-lookup (file) |
1c11da91 RS |
58 | "Return the `load-history' element for FILE. |
59 | FILE can be a file name, or a library name. | |
60 | A library name is equivalent to the file name that `load-library' would load." | |
27af61fe RS |
61 | ;; First look for FILE as given. |
62 | (let ((symbols (assoc file load-history))) | |
63 | ;; Try converting a library name to an absolute file name. | |
64 | (and (null symbols) | |
e18ce91d LT |
65 | (let ((absname |
66 | (locate-file file load-path (get-load-suffixes)))) | |
1c11da91 RS |
67 | (and absname (not (equal absname file)) |
68 | (setq symbols (cdr (assoc absname load-history)))))) | |
27af61fe RS |
69 | symbols)) |
70 | ||
2b86d62c | 71 | (defun file-provides (file) |
1c11da91 RS |
72 | "Return the list of features provided by FILE as it was loaded. |
73 | FILE can be a file name, or a library name. | |
74 | A library name is equivalent to the file name that `load-library' would load." | |
27af61fe | 75 | (let ((symbols (file-loadhist-lookup file)) |
53c1fa0f DL |
76 | provides) |
77 | (mapc (lambda (x) | |
78 | (if (and (consp x) (eq (car x) 'provide)) | |
79 | (setq provides (cons (cdr x) provides)))) | |
80 | symbols) | |
81 | provides)) | |
2b86d62c RS |
82 | |
83 | (defun file-requires (file) | |
1c11da91 RS |
84 | "Return the list of features required by FILE as it was loaded. |
85 | FILE can be a file name, or a library name. | |
86 | A library name is equivalent to the file name that `load-library' would load." | |
27af61fe | 87 | (let ((symbols (file-loadhist-lookup file)) |
53c1fa0f DL |
88 | requires) |
89 | (mapc (lambda (x) | |
90 | (if (and (consp x) (eq (car x) 'require)) | |
91 | (setq requires (cons (cdr x) requires)))) | |
92 | symbols) | |
93 | requires)) | |
94 | ||
95 | (defsubst file-set-intersect (p q) | |
96 | "Return the set intersection of two lists." | |
2b86d62c | 97 | (let ((ret nil)) |
53c1fa0f DL |
98 | (dolist (x p ret) |
99 | (if (memq x q) (setq ret (cons x ret)))) | |
100 | ret)) | |
2b86d62c RS |
101 | |
102 | (defun file-dependents (file) | |
e01ac82c | 103 | "Return the list of loaded libraries that depend on FILE. |
1c11da91 RS |
104 | This can include FILE itself. |
105 | FILE can be a file name, or a library name. | |
106 | A library name is equivalent to the file name that `load-library' would load." | |
53c1fa0f DL |
107 | (let ((provides (file-provides file)) |
108 | (dependents nil)) | |
109 | (dolist (x load-history dependents) | |
110 | (if (file-set-intersect provides (file-requires (car x))) | |
111 | (setq dependents (cons (car x) dependents)))) | |
112 | dependents)) | |
2b86d62c | 113 | |
12d9b59e KS |
114 | (defun read-feature (prompt &optional loaded-p) |
115 | "Read feature name from the minibuffer, prompting with string PROMPT. | |
116 | If optional second arg LOADED-P is non-nil, the feature must be loaded | |
117 | from a file." | |
118 | (intern | |
119 | (completing-read prompt | |
120 | (cons nil features) | |
121 | (and loaded-p | |
122 | #'(lambda (f) | |
123 | (and f ; ignore nil | |
124 | (feature-file f)))) | |
125 | loaded-p))) | |
d4708676 | 126 | |
28d02da1 RS |
127 | (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks) |
128 | (defvar unload-feature-special-hooks | |
28d8dff1 | 129 | '(after-change-functions |
7dc2cc98 RS |
130 | after-insert-file-functions auto-fill-function |
131 | before-change-functions blink-paren-function | |
132 | buffer-access-fontify-functions command-line-functions | |
133 | comment-indent-function kill-buffer-query-functions | |
134 | kill-emacs-query-functions lisp-indent-function | |
135 | mouse-position-function | |
136 | redisplay-end-trigger-functions temp-buffer-show-function | |
137 | window-scroll-functions window-size-change-functions | |
138 | write-region-annotate-functions) | |
3eef341a | 139 | "A list of special hooks from Info node `(elisp)Standard Hooks'. |
fde4581d RS |
140 | |
141 | These are symbols with hook-type values whose names don't end in | |
142 | `-hook' or `-hooks', from which `unload-feature' tries to remove | |
143 | pertinent symbols.") | |
144 | ||
dffc4dfc EZ |
145 | (defvar unload-hook-features-list nil |
146 | "List of features of the package being unloaded. | |
147 | ||
148 | This is meant to be used by FEATURE-unload-hook hooks, see the | |
149 | documentation of `unload-feature' for details.") | |
150 | ||
2b86d62c RS |
151 | ;;;###autoload |
152 | (defun unload-feature (feature &optional force) | |
153 | "Unload the library that provided FEATURE, restoring all its autoloads. | |
4370a375 | 154 | If the feature is required by any other loaded code, and prefix arg FORCE |
8cb16ad1 EZ |
155 | is nil, raise an error. |
156 | ||
157 | This function tries to undo modifications made by the package to | |
158 | hooks. Packages may define a hook FEATURE-unload-hook that is called | |
159 | instead of the normal heuristics for doing this. Such a hook should | |
160 | undo all the relevant global state changes that may have been made by | |
161 | loading the package or executing functions in it. It has access to | |
162 | the package's feature list (before anything is unbound) in the | |
163 | variable `unload-hook-features-list' and could remove features from it | |
164 | in the event that the package has done something normally-ill-advised, | |
165 | such as redefining an Emacs function." | |
12d9b59e KS |
166 | (interactive |
167 | (list | |
5c34b94b KS |
168 | (read-feature "Unload feature: " t) |
169 | current-prefix-arg)) | |
191652f8 LK |
170 | (unless (featurep feature) |
171 | (error "%s is not a currently loaded feature" (symbol-name feature))) | |
172 | (unless force | |
173 | (let* ((file (feature-file feature)) | |
174 | (dependents (delete file (copy-sequence (file-dependents file))))) | |
175 | (when dependents | |
176 | (error "Loaded libraries %s depend on %s" | |
177 | (prin1-to-string dependents) file)))) | |
8cb16ad1 | 178 | (let* ((unload-hook-features-list (feature-symbols feature)) |
191652f8 | 179 | (file (pop unload-hook-features-list)) |
531309eb RS |
180 | ;; If non-nil, this is a symbol for which we should |
181 | ;; restore a previous autoload if possible. | |
182 | restore-autoload | |
fde4581d RS |
183 | (unload-hook (intern-soft (concat (symbol-name feature) |
184 | "-unload-hook")))) | |
185 | ;; Try to avoid losing badly when hooks installed in critical | |
186 | ;; places go away. (Some packages install things on | |
187 | ;; `kill-buffer-hook', `activate-menubar-hook' and the like.) | |
557be036 RS |
188 | ;; First off, provide a clean way for package FOO to arrange |
189 | ;; this by adding hooks on the variable `FOO-unload-hook'. | |
fde4581d RS |
190 | (if unload-hook |
191 | (run-hooks unload-hook) | |
192 | ;; Otherwise, do our best. Look through the obarray for symbols | |
193 | ;; which seem to be hook variables or special hook functions and | |
194 | ;; remove anything from them which matches the feature-symbols | |
195 | ;; about to get zapped. Obviously this won't get anonymous | |
196 | ;; functions which the package might just have installed, and | |
197 | ;; there might be other important state, but this tactic | |
198 | ;; normally works. | |
199 | (mapatoms | |
200 | (lambda (x) | |
191652f8 LK |
201 | (when (and (boundp x) |
202 | (or (and (consp (symbol-value x)) ; Random hooks. | |
203 | (string-match "-hooks?\\'" (symbol-name x))) | |
204 | (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc. | |
205 | (dolist (y unload-hook-features-list) | |
bd0f9535 LK |
206 | (when (and (eq (car-safe y) 'defun) |
207 | (not (get (cdr y) 'autoload))) | |
208 | (remove-hook x (cdr y))))))) | |
209 | ;; Remove any feature-symbols from auto-mode-alist as well. | |
210 | (dolist (y unload-hook-features-list) | |
211 | (when (and (eq (car-safe y) 'defun) | |
212 | (not (get (cdr y) 'autoload))) | |
213 | (setq auto-mode-alist | |
214 | (rassq-delete-all (cdr y) auto-mode-alist))))) | |
191652f8 LK |
215 | (when (fboundp 'elp-restore-function) ; remove ELP stuff first |
216 | (dolist (elt unload-hook-features-list) | |
217 | (when (symbolp elt) | |
218 | (elp-restore-function elt)))) | |
531309eb | 219 | |
191652f8 LK |
220 | (dolist (x unload-hook-features-list) |
221 | (if (consp x) | |
ab215e72 | 222 | (case (car x) |
bd0f9535 | 223 | ;; Remove any feature names that this file provided. |
ab215e72 | 224 | (provide |
bd0f9535 | 225 | (setq features (delq (cdr x) features))) |
54c6a1c4 | 226 | ((defun autoload) |
bd0f9535 LK |
227 | (let ((fun (cdr x))) |
228 | (when (fboundp fun) | |
229 | (when (fboundp 'ad-unadvise) | |
230 | (ad-unadvise fun)) | |
bd0f9535 | 231 | (let ((aload (get fun 'autoload))) |
531309eb | 232 | (if (and aload (eq fun restore-autoload)) |
ab215e72 SM |
233 | (fset fun (cons 'autoload aload)) |
234 | (fmakunbound fun)))))) | |
531309eb RS |
235 | ;; (t . SYMBOL) comes before (defun . SYMBOL) |
236 | ;; and says we should restore SYMBOL's autoload | |
237 | ;; when we undefine it. | |
238 | ((t) (setq restore-autoload (cdr x))) | |
239 | ((require defface) nil) | |
c888d352 | 240 | (t (message "Unexpected element %s in load-history" x))) |
191652f8 LK |
241 | ;; Kill local values as much as possible. |
242 | (dolist (buf (buffer-list)) | |
243 | (with-current-buffer buf | |
ab215e72 SM |
244 | (if (and (boundp x) (timerp (symbol-value x))) |
245 | (cancel-timer (symbol-value x))) | |
191652f8 | 246 | (kill-local-variable x))) |
ab215e72 SM |
247 | (if (and (boundp x) (timerp (symbol-value x))) |
248 | (cancel-timer (symbol-value x))) | |
191652f8 LK |
249 | ;; Get rid of the default binding if we can. |
250 | (unless (local-variable-if-set-p x) | |
251 | (makunbound x)))) | |
a5c31fa1 | 252 | ;; Delete the load-history element for this file. |
54c6a1c4 JB |
253 | (setq load-history (delq (assoc file load-history) load-history))) |
254 | ;; Don't return load-history, it is not useful. | |
255 | nil) | |
2b86d62c RS |
256 | |
257 | (provide 'loadhist) | |
258 | ||
ab215e72 | 259 | ;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798 |
2b86d62c | 260 | ;;; loadhist.el ends here |