*** empty log message ***
[emacs.git] / lisp / loadhist.el
blobcd8c8ef099b555664b0ba7cde44e38471c11a23e
1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Copyright (C) 1995, 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 (let ((element (cons 'provide feature)))
43 (dolist (x load-history nil)
44 (when (member element (cdr x))
45 (throw 'foundit x))))))
47 (defun feature-file (feature)
48 "Return the file name from which a given FEATURE was loaded.
49 Actually, return the load argument, if any; this is sometimes the name of a
50 Lisp file without an extension. If the feature came from an `eval-buffer' on
51 a buffer with no associated file, or an `eval-region', return nil."
52 (if (not (featurep feature))
53 (error "%S is not a currently loaded feature" feature)
54 (car (feature-symbols feature))))
56 (defun file-loadhist-lookup (file)
57 "Return the `load-history' element for FILE.
58 FILE can be a file name, or a library name.
59 A library name is equivalent to the file name that `load-library' would load."
60 ;; First look for FILE as given.
61 (let ((symbols (assoc file load-history)))
62 ;; Try converting a library name to an absolute file name.
63 (and (null symbols)
64 (let ((absname
65 (locate-file file load-path (get-load-suffixes))))
66 (and absname (not (equal absname file))
67 (setq symbols (cdr (assoc absname load-history))))))
68 symbols))
70 (defun file-provides (file)
71 "Return the list of features provided by FILE as it was loaded.
72 FILE can be a file name, or a library name.
73 A library name is equivalent to the file name that `load-library' would load."
74 (let (provides)
75 (dolist (x (file-loadhist-lookup file) provides)
76 (when (eq (car-safe x) 'provide)
77 (push x provides)))))
79 (defun file-requires (file)
80 "Return the list of features required by FILE as it was loaded.
81 FILE can be a file name, or a library name.
82 A library name is equivalent to the file name that `load-library' would load."
83 (let (requires)
84 (dolist (x (file-loadhist-lookup file) requires)
85 (when (eq (car-safe x) 'require)
86 (push x requires)))))
88 (defsubst file-set-intersect (p q)
89 "Return the set intersection of two lists."
90 (let (ret)
91 (dolist (x p ret)
92 (when (memq x q) (push x ret)))))
94 (defun file-dependents (file)
95 "Return the list of loaded libraries that depend on FILE.
96 This can include FILE itself.
97 FILE can be a file name, or a library name.
98 A library name is equivalent to the file name that `load-library' would load."
99 (let ((provides (file-provides file))
100 (dependents nil))
101 (dolist (x load-history dependents)
102 (when (file-set-intersect provides (file-requires (car x)))
103 (push (car x) dependents)))))
105 (defun read-feature (prompt &optional loaded-p)
106 "Read feature name from the minibuffer, prompting with string PROMPT.
107 If optional second arg LOADED-P is non-nil, the feature must be loaded
108 from a file."
109 (intern
110 (completing-read prompt
111 (cons nil features)
112 (and loaded-p
113 #'(lambda (f)
114 (and f ; ignore nil
115 (feature-file f))))
116 loaded-p)))
118 (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
119 (defvar unload-feature-special-hooks
120 '(after-change-functions after-insert-file-functions
121 after-make-frame-functions auto-fill-function before-change-functions
122 blink-paren-function buffer-access-fontify-functions command-line-functions
123 comment-indent-function compilation-finish-functions
124 disabled-command-function find-file-not-found-functions
125 font-lock-beginning-of-syntax-function font-lock-fontify-buffer-function
126 font-lock-fontify-region-function font-lock-mark-block-function
127 font-lock-syntactic-face-function font-lock-unfontify-buffer-function
128 font-lock-unfontify-region-function kill-buffer-query-functions
129 kill-emacs-query-functions lisp-indent-function mouse-position-function
130 redisplay-end-trigger-functions temp-buffer-show-function
131 window-scroll-functions window-size-change-functions
132 write-contents-functions write-file-functions
133 write-region-annotate-functions)
134 "A list of special hooks from Info node `(elisp)Standard Hooks'.
136 These are symbols with hook-type values whose names don't end in
137 `-hook' or `-hooks', from which `unload-feature' tries to remove
138 pertinent symbols.")
140 (defvar unload-function-features-list nil
141 "List of features of the package being unloaded.
143 This is meant to be used by FEATURE-unload-function, see the
144 documentation of `unload-feature' for details.")
145 (define-obsolete-variable-alias 'unload-hook-features-list
146 'unload-function-features-list "22.2")
148 ;;;###autoload
149 (defun unload-feature (feature &optional force)
150 "Unload the library that provided FEATURE, restoring all its autoloads.
151 If the feature is required by any other loaded code, and prefix arg FORCE
152 is nil, raise an error.
154 This function tries to undo any modifications that the package has
155 made to hook values in Emacs. Normally it does this using heuristics.
156 The packages may define a hook `FEATURE-unload-hook'; if that exists,
157 it is called instead of the normal heuristics.
159 Such a hook should undo all the relevant global state changes that may
160 have been made by loading the package or executing functions in it.
161 It has access to the package's feature list (before anything is unbound)
162 in the variable `unload-hook-features-list' and could remove features
163 from it in the event that the package has done something strange,
164 such as redefining an Emacs function."
165 (interactive
166 (list
167 (read-feature "Unload feature: " t)
168 current-prefix-arg))
169 (unless (featurep feature)
170 (error "%s is not a currently loaded feature" (symbol-name feature)))
171 (unless force
172 (let* ((file (feature-file feature))
173 (dependents (delete file (copy-sequence (file-dependents file)))))
174 (when dependents
175 (error "Loaded libraries %s depend on %s"
176 (prin1-to-string dependents) file))))
177 (let* ((unload-function-features-list (feature-symbols feature))
178 (file (pop unload-function-features-list))
179 ;; If non-nil, this is a symbol for which we should
180 ;; restore a previous autoload if possible.
181 restore-autoload
182 (name (symbol-name feature))
183 (unload-hook (intern-soft (concat name "-unload-hook")))
184 (unload-func (intern-soft (concat name "-unload-function"))))
185 ;; If FEATURE-unload-function is defined and returns non-nil,
186 ;; don't try to do anything more; otherwise proceed normally.
187 (unless (and (bound-and-true-p unload-func)
188 (funcall unload-func))
189 ;; Try to avoid losing badly when hooks installed in critical
190 ;; places go away. (Some packages install things on
191 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
192 (if unload-hook
193 ;; First off, provide a clean way for package FOO to arrange
194 ;; this by adding hooks on the variable `FOO-unload-hook'.
195 ;; This is obsolete; FEATURE-unload-function should be used now.
196 (run-hooks unload-hook)
197 ;; Otherwise, do our best. Look through the obarray for symbols
198 ;; which seem to be hook variables or special hook functions and
199 ;; remove anything from them which matches the feature-symbols
200 ;; about to get zapped. Obviously this won't get anonymous
201 ;; functions which the package might just have installed, and
202 ;; there might be other important state, but this tactic
203 ;; normally works.
204 (mapatoms
205 (lambda (x)
206 (when (and (boundp x)
207 (or (and (consp (symbol-value x)) ; Random hooks.
208 (string-match "-hooks?\\'" (symbol-name x)))
209 (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc.
210 (dolist (y unload-function-features-list)
211 (when (and (eq (car-safe y) 'defun)
212 (not (get (cdr y) 'autoload)))
213 (remove-hook x (cdr y)))))))
214 ;; Remove any feature-symbols from auto-mode-alist as well.
215 (dolist (y unload-function-features-list)
216 (when (and (eq (car-safe y) 'defun)
217 (not (get (cdr y) 'autoload)))
218 (setq auto-mode-alist
219 (rassq-delete-all (cdr y) auto-mode-alist)))))
220 (when (fboundp 'elp-restore-function) ; remove ELP stuff first
221 (dolist (elt unload-function-features-list)
222 (when (symbolp elt)
223 (elp-restore-function elt))))
225 (dolist (x unload-function-features-list)
226 (if (consp x)
227 (case (car x)
228 ;; Remove any feature names that this file provided.
229 (provide
230 (setq features (delq (cdr x) features)))
231 ((defun autoload)
232 (let ((fun (cdr x)))
233 (when (fboundp fun)
234 (when (fboundp 'ad-unadvise)
235 (ad-unadvise fun))
236 (let ((aload (get fun 'autoload)))
237 (if (and aload (eq fun restore-autoload))
238 (fset fun (cons 'autoload aload))
239 (fmakunbound fun))))))
240 ;; (t . SYMBOL) comes before (defun . SYMBOL)
241 ;; and says we should restore SYMBOL's autoload
242 ;; when we undefine it.
243 ((t) (setq restore-autoload (cdr x)))
244 ((require defface) nil)
245 (t (message "Unexpected element %s in load-history" x)))
246 ;; Kill local values as much as possible.
247 (dolist (buf (buffer-list))
248 (with-current-buffer buf
249 (if (and (boundp x) (timerp (symbol-value x)))
250 (cancel-timer (symbol-value x)))
251 (kill-local-variable x)))
252 (if (and (boundp x) (timerp (symbol-value x)))
253 (cancel-timer (symbol-value x)))
254 ;; Get rid of the default binding if we can.
255 (unless (local-variable-if-set-p x)
256 (makunbound x))))
257 ;; Delete the load-history element for this file.
258 (setq load-history (delq (assoc file load-history) load-history))))
259 ;; Don't return load-history, it is not useful.
260 nil)
262 (provide 'loadhist)
264 ;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798
265 ;;; loadhist.el ends here