(temp-buffer-show-hook): Doc fix.
[emacs.git] / lisp / loadhist.el
blob2116ccfb5807c97d66acd7e54fa972a16b89bb34
1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Copyright (C) 1995, 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Maintainer: FSF
8 ;; Keywords: internal
10 ;; This file is part of GNU Emacs.
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
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; These functions exploit the load-history system variable.
30 ;; Entry points include `unload-feature', `symbol-file', and
31 ;; `feature-file', documented in the Emacs Lisp manual.
33 ;;; Code:
35 (eval-when-compile (require 'cl))
37 (defun feature-symbols (feature)
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)."
41 (catch 'foundit
42 (mapc (lambda (x)
43 (if (member (cons 'provide feature) (cdr x))
44 (throw 'foundit x)))
45 load-history)
46 nil))
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
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."
53 (if (not (featurep feature))
54 (error "%S is not a currently loaded feature" feature)
55 (car (feature-symbols feature))))
57 (defun file-loadhist-lookup (file)
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."
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)
65 (let ((absname
66 (locate-file file load-path (get-load-suffixes))))
67 (and absname (not (equal absname file))
68 (setq symbols (cdr (assoc absname load-history))))))
69 symbols))
71 (defun file-provides (file)
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."
75 (let ((symbols (file-loadhist-lookup file))
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))
83 (defun file-requires (file)
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."
87 (let ((symbols (file-loadhist-lookup file))
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))
95 (defsubst file-set-intersect (p q)
96 "Return the set intersection of two lists."
97 (let ((ret nil))
98 (dolist (x p ret)
99 (if (memq x q) (setq ret (cons x ret))))
100 ret))
102 (defun file-dependents (file)
103 "Return the list of loaded libraries that depend on FILE.
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."
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))
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)))
127 (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
128 (defvar unload-feature-special-hooks
129 '(after-change-functions after-insert-file-functions
130 after-make-frame-functions auto-fill-function before-change-functions
131 blink-paren-function buffer-access-fontify-functions command-line-functions
132 comment-indent-function compilation-finish-functions delete-frame-functions
133 disabled-command-function find-file-not-found-functions
134 font-lock-beginning-of-syntax-function font-lock-fontify-buffer-function
135 font-lock-fontify-region-function font-lock-mark-block-function
136 font-lock-syntactic-face-function font-lock-unfontify-buffer-function
137 font-lock-unfontify-region-function kill-buffer-query-functions
138 kill-emacs-query-functions lisp-indent-function mouse-position-function
139 redisplay-end-trigger-functions temp-buffer-show-function
140 window-scroll-functions window-size-change-functions
141 write-contents-functions write-file-functions
142 write-region-annotate-functions)
143 "A list of special hooks from Info node `(elisp)Standard Hooks'.
145 These are symbols with hooklike values whose names don't end in
146 `-hook' or `-hooks', from which `unload-feature' should try to remove
147 pertinent symbols.")
149 (defvar unload-function-defs-list nil
150 "List of defintions in the Lisp library being unloaded.
152 This is meant to be used by `FEATURE-unload-function'; see the
153 documentation of `unload-feature' for details.")
154 (define-obsolete-variable-alias 'unload-hook-features-list
155 'unload-function-defs-list "22.2")
157 ;;;###autoload
158 (defun unload-feature (feature &optional force)
159 "Unload the library that provided FEATURE.
160 If the feature is required by any other loaded code, and prefix arg FORCE
161 is nil, raise an error.
163 Standard unloading activities include restoring old autoloads for
164 functions defined by the library, undoing any additions that the
165 library has made to hook variables or to `auto-mode-alist', undoing
166 ELP profiling of functions in that library, unproviding any features
167 provided by the library, and canceling timers held in variables
168 defined by the library.
170 If a function `FEATURE-unload-function' is defined, this function
171 calls it with no arguments, before doing anything else. That function
172 can do whatever is appropriate to undo the loading of the library. If
173 `FEATURE-unload-function' returns non-nil, that suppresses the
174 standard unloading of the library. Otherwise the standard unloading
175 proceeds.
177 `FEATURE-unload-function' has access to the package's list of
178 definitions in the variable `unload-function-defs-list' and could
179 remove symbols from it in the event that the package has done
180 something strange, such as redefining an Emacs function."
181 (interactive
182 (list
183 (read-feature "Unload feature: " t)
184 current-prefix-arg))
185 (unless (featurep feature)
186 (error "%s is not a currently loaded feature" (symbol-name feature)))
187 (unless force
188 (let* ((file (feature-file feature))
189 (dependents (delete file (copy-sequence (file-dependents file)))))
190 (when dependents
191 (error "Loaded libraries %s depend on %s"
192 (prin1-to-string dependents) file))))
193 (let* ((unload-function-defs-list (feature-symbols feature))
194 (file (pop unload-function-defs-list))
195 ;; If non-nil, this is a symbol for which we should
196 ;; restore a previous autoload if possible.
197 restore-autoload
198 (name (symbol-name feature))
199 (unload-hook (intern-soft (concat name "-unload-hook")))
200 (unload-func (intern-soft (concat name "-unload-function"))))
201 ;; If FEATURE-unload-function is defined and returns non-nil,
202 ;; don't try to do anything more; otherwise proceed normally.
203 (unless (and (fboundp unload-func)
204 (funcall unload-func))
205 ;; Try to avoid losing badly when hooks installed in critical
206 ;; places go away. (Some packages install things on
207 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
208 (if unload-hook
209 ;; First off, provide a clean way for package FOO to arrange
210 ;; this by adding hooks on the variable `FOO-unload-hook'.
211 ;; This is obsolete; FEATURE-unload-function should be used now.
212 (run-hooks unload-hook)
213 ;; Otherwise, do our best. Look through the obarray for symbols
214 ;; which seem to be hook variables or special hook functions and
215 ;; remove anything from them which matches the feature-symbols
216 ;; about to get zapped. Obviously this won't get anonymous
217 ;; functions which the package might just have installed, and
218 ;; there might be other important state, but this tactic
219 ;; normally works.
220 (mapatoms
221 (lambda (x)
222 (when (and (boundp x)
223 (or (and (consp (symbol-value x)) ; Random hooks.
224 (string-match "-hooks?\\'" (symbol-name x)))
225 (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc.
226 (dolist (y unload-function-defs-list)
227 (when (and (eq (car-safe y) 'defun)
228 (not (get (cdr y) 'autoload)))
229 (remove-hook x (cdr y)))))))
230 ;; Remove any feature-symbols from auto-mode-alist as well.
231 (dolist (y unload-function-defs-list)
232 (when (and (eq (car-safe y) 'defun)
233 (not (get (cdr y) 'autoload)))
234 (setq auto-mode-alist
235 (rassq-delete-all (cdr y) auto-mode-alist)))))
236 (when (fboundp 'elp-restore-function) ; remove ELP stuff first
237 (dolist (elt unload-function-defs-list)
238 (when (symbolp elt)
239 (elp-restore-function elt))))
241 (dolist (x unload-function-defs-list)
242 (if (consp x)
243 (case (car x)
244 ;; Remove any feature names that this file provided.
245 (provide
246 (setq features (delq (cdr x) features)))
247 ((defun autoload)
248 (let ((fun (cdr x)))
249 (when (fboundp fun)
250 (when (fboundp 'ad-unadvise)
251 (ad-unadvise fun))
252 (let ((aload (get fun 'autoload)))
253 (if (and aload (eq fun restore-autoload))
254 (fset fun (cons 'autoload aload))
255 (fmakunbound fun))))))
256 ;; (t . SYMBOL) comes before (defun . SYMBOL)
257 ;; and says we should restore SYMBOL's autoload
258 ;; when we undefine it.
259 ((t) (setq restore-autoload (cdr x)))
260 ((require defface) nil)
261 (t (message "Unexpected element %s in load-history" x)))
262 ;; Kill local values as much as possible.
263 (dolist (buf (buffer-list))
264 (with-current-buffer buf
265 (if (and (boundp x) (timerp (symbol-value x)))
266 (cancel-timer (symbol-value x)))
267 (kill-local-variable x)))
268 (if (and (boundp x) (timerp (symbol-value x)))
269 (cancel-timer (symbol-value x)))
270 ;; Get rid of the default binding if we can.
271 (unless (local-variable-if-set-p x)
272 (makunbound x))))
273 ;; Delete the load-history element for this file.
274 (setq load-history (delq (assoc file load-history) load-history))))
275 ;; Don't return load-history, it is not useful.
276 nil)
278 (provide 'loadhist)
280 ;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798
281 ;;; loadhist.el ends here