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